Only use stride for final element type
[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 *read_subrange_index_type (struct die_info *die,
1377 struct dwarf2_cu *cu);
1378
1379 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1380
1381 static int need_gnat_info (struct dwarf2_cu *);
1382
1383 static struct type *die_descriptive_type (struct die_info *,
1384 struct dwarf2_cu *);
1385
1386 static void set_descriptive_type (struct type *, struct die_info *,
1387 struct dwarf2_cu *);
1388
1389 static struct type *die_containing_type (struct die_info *,
1390 struct dwarf2_cu *);
1391
1392 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1393 struct dwarf2_cu *);
1394
1395 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1396
1397 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1398
1399 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1400
1401 static char *typename_concat (struct obstack *obs, const char *prefix,
1402 const char *suffix, int physname,
1403 struct dwarf2_cu *cu);
1404
1405 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1406
1407 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1408
1409 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1410
1411 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1412
1413 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1414
1415 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1416
1417 /* Return the .debug_loclists section to use for cu. */
1418 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1419
1420 /* Return the .debug_rnglists section to use for cu. */
1421 static struct dwarf2_section_info *cu_debug_rnglists_section
1422 (struct dwarf2_cu *cu, dwarf_tag tag);
1423
1424 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1425 values. Keep the items ordered with increasing constraints compliance. */
1426 enum pc_bounds_kind
1427 {
1428 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1429 PC_BOUNDS_NOT_PRESENT,
1430
1431 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1432 were present but they do not form a valid range of PC addresses. */
1433 PC_BOUNDS_INVALID,
1434
1435 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1436 PC_BOUNDS_RANGES,
1437
1438 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1439 PC_BOUNDS_HIGH_LOW,
1440 };
1441
1442 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1443 CORE_ADDR *, CORE_ADDR *,
1444 struct dwarf2_cu *,
1445 dwarf2_psymtab *);
1446
1447 static void get_scope_pc_bounds (struct die_info *,
1448 CORE_ADDR *, CORE_ADDR *,
1449 struct dwarf2_cu *);
1450
1451 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1452 CORE_ADDR, struct dwarf2_cu *);
1453
1454 static void dwarf2_add_field (struct field_info *, struct die_info *,
1455 struct dwarf2_cu *);
1456
1457 static void dwarf2_attach_fields_to_type (struct field_info *,
1458 struct type *, struct dwarf2_cu *);
1459
1460 static void dwarf2_add_member_fn (struct field_info *,
1461 struct die_info *, struct type *,
1462 struct dwarf2_cu *);
1463
1464 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1465 struct type *,
1466 struct dwarf2_cu *);
1467
1468 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1469
1470 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1471
1472 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1473
1474 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1475
1476 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1477
1478 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1479
1480 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1481
1482 static struct type *read_module_type (struct die_info *die,
1483 struct dwarf2_cu *cu);
1484
1485 static const char *namespace_name (struct die_info *die,
1486 int *is_anonymous, struct dwarf2_cu *);
1487
1488 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1489
1490 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1491 bool * = nullptr);
1492
1493 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1494 struct dwarf2_cu *);
1495
1496 static struct die_info *read_die_and_siblings_1
1497 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1498 struct die_info *);
1499
1500 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1501 const gdb_byte *info_ptr,
1502 const gdb_byte **new_info_ptr,
1503 struct die_info *parent);
1504
1505 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1506 struct die_info **, const gdb_byte *,
1507 int);
1508
1509 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1510 struct die_info **, const gdb_byte *);
1511
1512 static void process_die (struct die_info *, struct dwarf2_cu *);
1513
1514 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1515 struct objfile *);
1516
1517 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1518
1519 static const char *dwarf2_full_name (const char *name,
1520 struct die_info *die,
1521 struct dwarf2_cu *cu);
1522
1523 static const char *dwarf2_physname (const char *name, struct die_info *die,
1524 struct dwarf2_cu *cu);
1525
1526 static struct die_info *dwarf2_extension (struct die_info *die,
1527 struct dwarf2_cu **);
1528
1529 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1530
1531 static void dump_die_for_error (struct die_info *);
1532
1533 static void dump_die_1 (struct ui_file *, int level, int max_level,
1534 struct die_info *);
1535
1536 /*static*/ void dump_die (struct die_info *, int max_level);
1537
1538 static void store_in_ref_table (struct die_info *,
1539 struct dwarf2_cu *);
1540
1541 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1542 const struct attribute *,
1543 struct dwarf2_cu **);
1544
1545 static struct die_info *follow_die_ref (struct die_info *,
1546 const struct attribute *,
1547 struct dwarf2_cu **);
1548
1549 static struct die_info *follow_die_sig (struct die_info *,
1550 const struct attribute *,
1551 struct dwarf2_cu **);
1552
1553 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1554 struct dwarf2_cu *);
1555
1556 static struct type *get_DW_AT_signature_type (struct die_info *,
1557 const struct attribute *,
1558 struct dwarf2_cu *);
1559
1560 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1561 dwarf2_per_objfile *per_objfile);
1562
1563 static void read_signatured_type (signatured_type *sig_type,
1564 dwarf2_per_objfile *per_objfile);
1565
1566 static int attr_to_dynamic_prop (const struct attribute *attr,
1567 struct die_info *die, struct dwarf2_cu *cu,
1568 struct dynamic_prop *prop, struct type *type);
1569
1570 /* memory allocation interface */
1571
1572 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1573
1574 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1575
1576 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1577
1578 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1579 struct dwarf2_loclist_baton *baton,
1580 const struct attribute *attr);
1581
1582 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1583 struct symbol *sym,
1584 struct dwarf2_cu *cu,
1585 int is_block);
1586
1587 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1588 const gdb_byte *info_ptr,
1589 struct abbrev_info *abbrev);
1590
1591 static hashval_t partial_die_hash (const void *item);
1592
1593 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1594
1595 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1596 (sect_offset sect_off, unsigned int offset_in_dwz,
1597 dwarf2_per_objfile *per_objfile);
1598
1599 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1600 struct die_info *comp_unit_die,
1601 enum language pretend_language);
1602
1603 static struct type *set_die_type (struct die_info *, struct type *,
1604 struct dwarf2_cu *, bool = false);
1605
1606 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1607
1608 static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1609
1610 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1611 dwarf2_per_objfile *per_objfile,
1612 dwarf2_cu *existing_cu,
1613 bool skip_partial,
1614 enum language pretend_language);
1615
1616 static void process_full_comp_unit (dwarf2_cu *cu,
1617 enum language pretend_language);
1618
1619 static void process_full_type_unit (dwarf2_cu *cu,
1620 enum language pretend_language);
1621
1622 static void dwarf2_add_dependence (struct dwarf2_cu *,
1623 struct dwarf2_per_cu_data *);
1624
1625 static void dwarf2_mark (struct dwarf2_cu *);
1626
1627 static struct type *get_die_type_at_offset (sect_offset,
1628 dwarf2_per_cu_data *per_cu,
1629 dwarf2_per_objfile *per_objfile);
1630
1631 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1632
1633 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1634 dwarf2_per_objfile *per_objfile,
1635 enum language pretend_language);
1636
1637 static void process_queue (dwarf2_per_objfile *per_objfile);
1638
1639 /* Class, the destructor of which frees all allocated queue entries. This
1640 will only have work to do if an error was thrown while processing the
1641 dwarf. If no error was thrown then the queue entries should have all
1642 been processed, and freed, as we went along. */
1643
1644 class dwarf2_queue_guard
1645 {
1646 public:
1647 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1648 : m_per_objfile (per_objfile)
1649 {
1650 }
1651
1652 /* Free any entries remaining on the queue. There should only be
1653 entries left if we hit an error while processing the dwarf. */
1654 ~dwarf2_queue_guard ()
1655 {
1656 /* Ensure that no memory is allocated by the queue. */
1657 std::queue<dwarf2_queue_item> empty;
1658 std::swap (m_per_objfile->per_bfd->queue, empty);
1659 }
1660
1661 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1662
1663 private:
1664 dwarf2_per_objfile *m_per_objfile;
1665 };
1666
1667 dwarf2_queue_item::~dwarf2_queue_item ()
1668 {
1669 /* Anything still marked queued is likely to be in an
1670 inconsistent state, so discard it. */
1671 if (per_cu->queued)
1672 {
1673 per_objfile->remove_cu (per_cu);
1674 per_cu->queued = 0;
1675 }
1676 }
1677
1678 /* The return type of find_file_and_directory. Note, the enclosed
1679 string pointers are only valid while this object is valid. */
1680
1681 struct file_and_directory
1682 {
1683 /* The filename. This is never NULL. */
1684 const char *name;
1685
1686 /* The compilation directory. NULL if not known. If we needed to
1687 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1688 points directly to the DW_AT_comp_dir string attribute owned by
1689 the obstack that owns the DIE. */
1690 const char *comp_dir;
1691
1692 /* If we needed to build a new string for comp_dir, this is what
1693 owns the storage. */
1694 std::string comp_dir_storage;
1695 };
1696
1697 static file_and_directory find_file_and_directory (struct die_info *die,
1698 struct dwarf2_cu *cu);
1699
1700 static htab_up allocate_signatured_type_table ();
1701
1702 static htab_up allocate_dwo_unit_table ();
1703
1704 static struct dwo_unit *lookup_dwo_unit_in_dwp
1705 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1706 const char *comp_dir, ULONGEST signature, int is_debug_types);
1707
1708 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1709
1710 static struct dwo_unit *lookup_dwo_comp_unit
1711 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1712 ULONGEST signature);
1713
1714 static struct dwo_unit *lookup_dwo_type_unit
1715 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1716
1717 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1718
1719 /* A unique pointer to a dwo_file. */
1720
1721 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1722
1723 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1724
1725 static void check_producer (struct dwarf2_cu *cu);
1726
1727 static void free_line_header_voidp (void *arg);
1728 \f
1729 /* Various complaints about symbol reading that don't abort the process. */
1730
1731 static void
1732 dwarf2_debug_line_missing_file_complaint (void)
1733 {
1734 complaint (_(".debug_line section has line data without a file"));
1735 }
1736
1737 static void
1738 dwarf2_debug_line_missing_end_sequence_complaint (void)
1739 {
1740 complaint (_(".debug_line section has line "
1741 "program sequence without an end"));
1742 }
1743
1744 static void
1745 dwarf2_complex_location_expr_complaint (void)
1746 {
1747 complaint (_("location expression too complex"));
1748 }
1749
1750 static void
1751 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1752 int arg3)
1753 {
1754 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1755 arg1, arg2, arg3);
1756 }
1757
1758 static void
1759 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1760 {
1761 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1762 arg1, arg2);
1763 }
1764
1765 /* Hash function for line_header_hash. */
1766
1767 static hashval_t
1768 line_header_hash (const struct line_header *ofs)
1769 {
1770 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1771 }
1772
1773 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1774
1775 static hashval_t
1776 line_header_hash_voidp (const void *item)
1777 {
1778 const struct line_header *ofs = (const struct line_header *) item;
1779
1780 return line_header_hash (ofs);
1781 }
1782
1783 /* Equality function for line_header_hash. */
1784
1785 static int
1786 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1787 {
1788 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1789 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1790
1791 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1792 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1793 }
1794
1795 \f
1796
1797 /* See declaration. */
1798
1799 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1800 bool can_copy_)
1801 : obfd (obfd),
1802 can_copy (can_copy_)
1803 {
1804 if (names == NULL)
1805 names = &dwarf2_elf_names;
1806
1807 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1808 locate_sections (obfd, sec, *names);
1809 }
1810
1811 dwarf2_per_bfd::~dwarf2_per_bfd ()
1812 {
1813 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1814 per_cu->imported_symtabs_free ();
1815
1816 for (signatured_type *sig_type : all_type_units)
1817 sig_type->per_cu.imported_symtabs_free ();
1818
1819 /* Everything else should be on this->obstack. */
1820 }
1821
1822 /* See read.h. */
1823
1824 void
1825 dwarf2_per_objfile::remove_all_cus ()
1826 {
1827 for (auto pair : m_dwarf2_cus)
1828 delete pair.second;
1829
1830 m_dwarf2_cus.clear ();
1831 }
1832
1833 /* A helper class that calls free_cached_comp_units on
1834 destruction. */
1835
1836 class free_cached_comp_units
1837 {
1838 public:
1839
1840 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1841 : m_per_objfile (per_objfile)
1842 {
1843 }
1844
1845 ~free_cached_comp_units ()
1846 {
1847 m_per_objfile->remove_all_cus ();
1848 }
1849
1850 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1851
1852 private:
1853
1854 dwarf2_per_objfile *m_per_objfile;
1855 };
1856
1857 /* See read.h. */
1858
1859 bool
1860 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1861 {
1862 gdb_assert (per_cu->index < this->m_symtabs.size ());
1863
1864 return this->m_symtabs[per_cu->index] != nullptr;
1865 }
1866
1867 /* See read.h. */
1868
1869 compunit_symtab *
1870 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1871 {
1872 gdb_assert (per_cu->index < this->m_symtabs.size ());
1873
1874 return this->m_symtabs[per_cu->index];
1875 }
1876
1877 /* See read.h. */
1878
1879 void
1880 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1881 compunit_symtab *symtab)
1882 {
1883 gdb_assert (per_cu->index < this->m_symtabs.size ());
1884 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1885
1886 this->m_symtabs[per_cu->index] = symtab;
1887 }
1888
1889 /* Try to locate the sections we need for DWARF 2 debugging
1890 information and return true if we have enough to do something.
1891 NAMES points to the dwarf2 section names, or is NULL if the standard
1892 ELF names are used. CAN_COPY is true for formats where symbol
1893 interposition is possible and so symbol values must follow copy
1894 relocation rules. */
1895
1896 int
1897 dwarf2_has_info (struct objfile *objfile,
1898 const struct dwarf2_debug_sections *names,
1899 bool can_copy)
1900 {
1901 if (objfile->flags & OBJF_READNEVER)
1902 return 0;
1903
1904 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1905
1906 if (per_objfile == NULL)
1907 {
1908 dwarf2_per_bfd *per_bfd;
1909
1910 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
1911 doesn't require relocations and if there aren't partial symbols
1912 from some other reader. */
1913 if (!objfile_has_partial_symbols (objfile)
1914 && !gdb_bfd_requires_relocations (objfile->obfd))
1915 {
1916 /* See if one has been created for this BFD yet. */
1917 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1918
1919 if (per_bfd == nullptr)
1920 {
1921 /* No, create it now. */
1922 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1923 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1924 }
1925 }
1926 else
1927 {
1928 /* No sharing possible, create one specifically for this objfile. */
1929 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1930 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1931 }
1932
1933 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1934 }
1935
1936 return (!per_objfile->per_bfd->info.is_virtual
1937 && per_objfile->per_bfd->info.s.section != NULL
1938 && !per_objfile->per_bfd->abbrev.is_virtual
1939 && per_objfile->per_bfd->abbrev.s.section != NULL);
1940 }
1941
1942 /* When loading sections, we look either for uncompressed section or for
1943 compressed section names. */
1944
1945 static int
1946 section_is_p (const char *section_name,
1947 const struct dwarf2_section_names *names)
1948 {
1949 if (names->normal != NULL
1950 && strcmp (section_name, names->normal) == 0)
1951 return 1;
1952 if (names->compressed != NULL
1953 && strcmp (section_name, names->compressed) == 0)
1954 return 1;
1955 return 0;
1956 }
1957
1958 /* See declaration. */
1959
1960 void
1961 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1962 const dwarf2_debug_sections &names)
1963 {
1964 flagword aflag = bfd_section_flags (sectp);
1965
1966 if ((aflag & SEC_HAS_CONTENTS) == 0)
1967 {
1968 }
1969 else if (elf_section_data (sectp)->this_hdr.sh_size
1970 > bfd_get_file_size (abfd))
1971 {
1972 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1973 warning (_("Discarding section %s which has a section size (%s"
1974 ") larger than the file size [in module %s]"),
1975 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1976 bfd_get_filename (abfd));
1977 }
1978 else if (section_is_p (sectp->name, &names.info))
1979 {
1980 this->info.s.section = sectp;
1981 this->info.size = bfd_section_size (sectp);
1982 }
1983 else if (section_is_p (sectp->name, &names.abbrev))
1984 {
1985 this->abbrev.s.section = sectp;
1986 this->abbrev.size = bfd_section_size (sectp);
1987 }
1988 else if (section_is_p (sectp->name, &names.line))
1989 {
1990 this->line.s.section = sectp;
1991 this->line.size = bfd_section_size (sectp);
1992 }
1993 else if (section_is_p (sectp->name, &names.loc))
1994 {
1995 this->loc.s.section = sectp;
1996 this->loc.size = bfd_section_size (sectp);
1997 }
1998 else if (section_is_p (sectp->name, &names.loclists))
1999 {
2000 this->loclists.s.section = sectp;
2001 this->loclists.size = bfd_section_size (sectp);
2002 }
2003 else if (section_is_p (sectp->name, &names.macinfo))
2004 {
2005 this->macinfo.s.section = sectp;
2006 this->macinfo.size = bfd_section_size (sectp);
2007 }
2008 else if (section_is_p (sectp->name, &names.macro))
2009 {
2010 this->macro.s.section = sectp;
2011 this->macro.size = bfd_section_size (sectp);
2012 }
2013 else if (section_is_p (sectp->name, &names.str))
2014 {
2015 this->str.s.section = sectp;
2016 this->str.size = bfd_section_size (sectp);
2017 }
2018 else if (section_is_p (sectp->name, &names.str_offsets))
2019 {
2020 this->str_offsets.s.section = sectp;
2021 this->str_offsets.size = bfd_section_size (sectp);
2022 }
2023 else if (section_is_p (sectp->name, &names.line_str))
2024 {
2025 this->line_str.s.section = sectp;
2026 this->line_str.size = bfd_section_size (sectp);
2027 }
2028 else if (section_is_p (sectp->name, &names.addr))
2029 {
2030 this->addr.s.section = sectp;
2031 this->addr.size = bfd_section_size (sectp);
2032 }
2033 else if (section_is_p (sectp->name, &names.frame))
2034 {
2035 this->frame.s.section = sectp;
2036 this->frame.size = bfd_section_size (sectp);
2037 }
2038 else if (section_is_p (sectp->name, &names.eh_frame))
2039 {
2040 this->eh_frame.s.section = sectp;
2041 this->eh_frame.size = bfd_section_size (sectp);
2042 }
2043 else if (section_is_p (sectp->name, &names.ranges))
2044 {
2045 this->ranges.s.section = sectp;
2046 this->ranges.size = bfd_section_size (sectp);
2047 }
2048 else if (section_is_p (sectp->name, &names.rnglists))
2049 {
2050 this->rnglists.s.section = sectp;
2051 this->rnglists.size = bfd_section_size (sectp);
2052 }
2053 else if (section_is_p (sectp->name, &names.types))
2054 {
2055 struct dwarf2_section_info type_section;
2056
2057 memset (&type_section, 0, sizeof (type_section));
2058 type_section.s.section = sectp;
2059 type_section.size = bfd_section_size (sectp);
2060
2061 this->types.push_back (type_section);
2062 }
2063 else if (section_is_p (sectp->name, &names.gdb_index))
2064 {
2065 this->gdb_index.s.section = sectp;
2066 this->gdb_index.size = bfd_section_size (sectp);
2067 }
2068 else if (section_is_p (sectp->name, &names.debug_names))
2069 {
2070 this->debug_names.s.section = sectp;
2071 this->debug_names.size = bfd_section_size (sectp);
2072 }
2073 else if (section_is_p (sectp->name, &names.debug_aranges))
2074 {
2075 this->debug_aranges.s.section = sectp;
2076 this->debug_aranges.size = bfd_section_size (sectp);
2077 }
2078
2079 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2080 && bfd_section_vma (sectp) == 0)
2081 this->has_section_at_zero = true;
2082 }
2083
2084 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2085 SECTION_NAME. */
2086
2087 void
2088 dwarf2_get_section_info (struct objfile *objfile,
2089 enum dwarf2_section_enum sect,
2090 asection **sectp, const gdb_byte **bufp,
2091 bfd_size_type *sizep)
2092 {
2093 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2094 struct dwarf2_section_info *info;
2095
2096 /* We may see an objfile without any DWARF, in which case we just
2097 return nothing. */
2098 if (per_objfile == NULL)
2099 {
2100 *sectp = NULL;
2101 *bufp = NULL;
2102 *sizep = 0;
2103 return;
2104 }
2105 switch (sect)
2106 {
2107 case DWARF2_DEBUG_FRAME:
2108 info = &per_objfile->per_bfd->frame;
2109 break;
2110 case DWARF2_EH_FRAME:
2111 info = &per_objfile->per_bfd->eh_frame;
2112 break;
2113 default:
2114 gdb_assert_not_reached ("unexpected section");
2115 }
2116
2117 info->read (objfile);
2118
2119 *sectp = info->get_bfd_section ();
2120 *bufp = info->buffer;
2121 *sizep = info->size;
2122 }
2123
2124 /* A helper function to find the sections for a .dwz file. */
2125
2126 static void
2127 locate_dwz_sections (bfd *abfd, asection *sectp, dwz_file *dwz_file)
2128 {
2129 /* Note that we only support the standard ELF names, because .dwz
2130 is ELF-only (at the time of writing). */
2131 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2132 {
2133 dwz_file->abbrev.s.section = sectp;
2134 dwz_file->abbrev.size = bfd_section_size (sectp);
2135 }
2136 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2137 {
2138 dwz_file->info.s.section = sectp;
2139 dwz_file->info.size = bfd_section_size (sectp);
2140 }
2141 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2142 {
2143 dwz_file->str.s.section = sectp;
2144 dwz_file->str.size = bfd_section_size (sectp);
2145 }
2146 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2147 {
2148 dwz_file->line.s.section = sectp;
2149 dwz_file->line.size = bfd_section_size (sectp);
2150 }
2151 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2152 {
2153 dwz_file->macro.s.section = sectp;
2154 dwz_file->macro.size = bfd_section_size (sectp);
2155 }
2156 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2157 {
2158 dwz_file->gdb_index.s.section = sectp;
2159 dwz_file->gdb_index.size = bfd_section_size (sectp);
2160 }
2161 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2162 {
2163 dwz_file->debug_names.s.section = sectp;
2164 dwz_file->debug_names.size = bfd_section_size (sectp);
2165 }
2166 }
2167
2168 /* See dwarf2read.h. */
2169
2170 struct dwz_file *
2171 dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
2172 {
2173 const char *filename;
2174 bfd_size_type buildid_len_arg;
2175 size_t buildid_len;
2176 bfd_byte *buildid;
2177
2178 if (per_bfd->dwz_file != NULL)
2179 return per_bfd->dwz_file.get ();
2180
2181 bfd_set_error (bfd_error_no_error);
2182 gdb::unique_xmalloc_ptr<char> data
2183 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2184 &buildid_len_arg, &buildid));
2185 if (data == NULL)
2186 {
2187 if (bfd_get_error () == bfd_error_no_error)
2188 return NULL;
2189 error (_("could not read '.gnu_debugaltlink' section: %s"),
2190 bfd_errmsg (bfd_get_error ()));
2191 }
2192
2193 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2194
2195 buildid_len = (size_t) buildid_len_arg;
2196
2197 filename = data.get ();
2198
2199 std::string abs_storage;
2200 if (!IS_ABSOLUTE_PATH (filename))
2201 {
2202 gdb::unique_xmalloc_ptr<char> abs
2203 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2204
2205 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2206 filename = abs_storage.c_str ();
2207 }
2208
2209 /* First try the file name given in the section. If that doesn't
2210 work, try to use the build-id instead. */
2211 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
2212 if (dwz_bfd != NULL)
2213 {
2214 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2215 dwz_bfd.reset (nullptr);
2216 }
2217
2218 if (dwz_bfd == NULL)
2219 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2220
2221 if (dwz_bfd == nullptr)
2222 {
2223 gdb::unique_xmalloc_ptr<char> alt_filename;
2224 const char *origname = bfd_get_filename (per_bfd->obfd);
2225
2226 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2227 buildid_len,
2228 origname,
2229 &alt_filename));
2230
2231 if (fd.get () >= 0)
2232 {
2233 /* File successfully retrieved from server. */
2234 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2235
2236 if (dwz_bfd == nullptr)
2237 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2238 alt_filename.get ());
2239 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2240 dwz_bfd.reset (nullptr);
2241 }
2242 }
2243
2244 if (dwz_bfd == NULL)
2245 error (_("could not find '.gnu_debugaltlink' file for %s"),
2246 bfd_get_filename (per_bfd->obfd));
2247
2248 std::unique_ptr<struct dwz_file> result
2249 (new struct dwz_file (std::move (dwz_bfd)));
2250
2251 for (asection *sec : gdb_bfd_sections (result->dwz_bfd))
2252 locate_dwz_sections (result->dwz_bfd.get (), sec, result.get ());
2253
2254 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2255 per_bfd->dwz_file = std::move (result);
2256 return per_bfd->dwz_file.get ();
2257 }
2258 \f
2259 /* DWARF quick_symbols_functions support. */
2260
2261 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2262 unique line tables, so we maintain a separate table of all .debug_line
2263 derived entries to support the sharing.
2264 All the quick functions need is the list of file names. We discard the
2265 line_header when we're done and don't need to record it here. */
2266 struct quick_file_names
2267 {
2268 /* The data used to construct the hash key. */
2269 struct stmt_list_hash hash;
2270
2271 /* The number of entries in file_names, real_names. */
2272 unsigned int num_file_names;
2273
2274 /* The file names from the line table, after being run through
2275 file_full_name. */
2276 const char **file_names;
2277
2278 /* The file names from the line table after being run through
2279 gdb_realpath. These are computed lazily. */
2280 const char **real_names;
2281 };
2282
2283 /* When using the index (and thus not using psymtabs), each CU has an
2284 object of this type. This is used to hold information needed by
2285 the various "quick" methods. */
2286 struct dwarf2_per_cu_quick_data
2287 {
2288 /* The file table. This can be NULL if there was no file table
2289 or it's currently not read in.
2290 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2291 struct quick_file_names *file_names;
2292
2293 /* A temporary mark bit used when iterating over all CUs in
2294 expand_symtabs_matching. */
2295 unsigned int mark : 1;
2296
2297 /* True if we've tried to read the file table and found there isn't one.
2298 There will be no point in trying to read it again next time. */
2299 unsigned int no_file_data : 1;
2300 };
2301
2302 /* Utility hash function for a stmt_list_hash. */
2303
2304 static hashval_t
2305 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2306 {
2307 hashval_t v = 0;
2308
2309 if (stmt_list_hash->dwo_unit != NULL)
2310 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2311 v += to_underlying (stmt_list_hash->line_sect_off);
2312 return v;
2313 }
2314
2315 /* Utility equality function for a stmt_list_hash. */
2316
2317 static int
2318 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2319 const struct stmt_list_hash *rhs)
2320 {
2321 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2322 return 0;
2323 if (lhs->dwo_unit != NULL
2324 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2325 return 0;
2326
2327 return lhs->line_sect_off == rhs->line_sect_off;
2328 }
2329
2330 /* Hash function for a quick_file_names. */
2331
2332 static hashval_t
2333 hash_file_name_entry (const void *e)
2334 {
2335 const struct quick_file_names *file_data
2336 = (const struct quick_file_names *) e;
2337
2338 return hash_stmt_list_entry (&file_data->hash);
2339 }
2340
2341 /* Equality function for a quick_file_names. */
2342
2343 static int
2344 eq_file_name_entry (const void *a, const void *b)
2345 {
2346 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2347 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2348
2349 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2350 }
2351
2352 /* Delete function for a quick_file_names. */
2353
2354 static void
2355 delete_file_name_entry (void *e)
2356 {
2357 struct quick_file_names *file_data = (struct quick_file_names *) e;
2358 int i;
2359
2360 for (i = 0; i < file_data->num_file_names; ++i)
2361 {
2362 xfree ((void*) file_data->file_names[i]);
2363 if (file_data->real_names)
2364 xfree ((void*) file_data->real_names[i]);
2365 }
2366
2367 /* The space for the struct itself lives on the obstack, so we don't
2368 free it here. */
2369 }
2370
2371 /* Create a quick_file_names hash table. */
2372
2373 static htab_up
2374 create_quick_file_names_table (unsigned int nr_initial_entries)
2375 {
2376 return htab_up (htab_create_alloc (nr_initial_entries,
2377 hash_file_name_entry, eq_file_name_entry,
2378 delete_file_name_entry, xcalloc, xfree));
2379 }
2380
2381 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2382 function is unrelated to symtabs, symtab would have to be created afterwards.
2383 You should call age_cached_comp_units after processing the CU. */
2384
2385 static dwarf2_cu *
2386 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2387 bool skip_partial)
2388 {
2389 if (per_cu->is_debug_types)
2390 load_full_type_unit (per_cu, per_objfile);
2391 else
2392 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2393 skip_partial, language_minimal);
2394
2395 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2396 if (cu == nullptr)
2397 return nullptr; /* Dummy CU. */
2398
2399 dwarf2_find_base_address (cu->dies, cu);
2400
2401 return cu;
2402 }
2403
2404 /* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2405
2406 static void
2407 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2408 dwarf2_per_objfile *per_objfile, bool skip_partial)
2409 {
2410 /* Skip type_unit_groups, reading the type units they contain
2411 is handled elsewhere. */
2412 if (per_cu->type_unit_group_p ())
2413 return;
2414
2415 /* The destructor of dwarf2_queue_guard frees any entries left on
2416 the queue. After this point we're guaranteed to leave this function
2417 with the dwarf queue empty. */
2418 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2419
2420 if (!per_objfile->symtab_set_p (per_cu))
2421 {
2422 queue_comp_unit (per_cu, per_objfile, language_minimal);
2423 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2424
2425 /* If we just loaded a CU from a DWO, and we're working with an index
2426 that may badly handle TUs, load all the TUs in that DWO as well.
2427 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2428 if (!per_cu->is_debug_types
2429 && cu != NULL
2430 && cu->dwo_unit != NULL
2431 && per_objfile->per_bfd->index_table != NULL
2432 && per_objfile->per_bfd->index_table->version <= 7
2433 /* DWP files aren't supported yet. */
2434 && get_dwp_file (per_objfile) == NULL)
2435 queue_and_load_all_dwo_tus (cu);
2436 }
2437
2438 process_queue (per_objfile);
2439
2440 /* Age the cache, releasing compilation units that have not
2441 been used recently. */
2442 per_objfile->age_comp_units ();
2443 }
2444
2445 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2446 the per-objfile for which this symtab is instantiated.
2447
2448 Returns the resulting symbol table. */
2449
2450 static struct compunit_symtab *
2451 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2452 dwarf2_per_objfile *per_objfile,
2453 bool skip_partial)
2454 {
2455 gdb_assert (per_objfile->per_bfd->using_index);
2456
2457 if (!per_objfile->symtab_set_p (per_cu))
2458 {
2459 free_cached_comp_units freer (per_objfile);
2460 scoped_restore decrementer = increment_reading_symtab ();
2461 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2462 process_cu_includes (per_objfile);
2463 }
2464
2465 return per_objfile->get_symtab (per_cu);
2466 }
2467
2468 /* See declaration. */
2469
2470 dwarf2_per_cu_data *
2471 dwarf2_per_bfd::get_cutu (int index)
2472 {
2473 if (index >= this->all_comp_units.size ())
2474 {
2475 index -= this->all_comp_units.size ();
2476 gdb_assert (index < this->all_type_units.size ());
2477 return &this->all_type_units[index]->per_cu;
2478 }
2479
2480 return this->all_comp_units[index];
2481 }
2482
2483 /* See declaration. */
2484
2485 dwarf2_per_cu_data *
2486 dwarf2_per_bfd::get_cu (int index)
2487 {
2488 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2489
2490 return this->all_comp_units[index];
2491 }
2492
2493 /* See declaration. */
2494
2495 signatured_type *
2496 dwarf2_per_bfd::get_tu (int index)
2497 {
2498 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2499
2500 return this->all_type_units[index];
2501 }
2502
2503 /* See read.h. */
2504
2505 dwarf2_per_cu_data *
2506 dwarf2_per_bfd::allocate_per_cu ()
2507 {
2508 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2509 result->per_bfd = this;
2510 result->index = m_num_psymtabs++;
2511 return result;
2512 }
2513
2514 /* See read.h. */
2515
2516 signatured_type *
2517 dwarf2_per_bfd::allocate_signatured_type ()
2518 {
2519 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2520 result->per_cu.per_bfd = this;
2521 result->per_cu.index = m_num_psymtabs++;
2522 return result;
2523 }
2524
2525 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2526 obstack, and constructed with the specified field values. */
2527
2528 static dwarf2_per_cu_data *
2529 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2530 struct dwarf2_section_info *section,
2531 int is_dwz,
2532 sect_offset sect_off, ULONGEST length)
2533 {
2534 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
2535 the_cu->sect_off = sect_off;
2536 the_cu->length = length;
2537 the_cu->section = section;
2538 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2539 struct dwarf2_per_cu_quick_data);
2540 the_cu->is_dwz = is_dwz;
2541 return the_cu;
2542 }
2543
2544 /* A helper for create_cus_from_index that handles a given list of
2545 CUs. */
2546
2547 static void
2548 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2549 const gdb_byte *cu_list, offset_type n_elements,
2550 struct dwarf2_section_info *section,
2551 int is_dwz)
2552 {
2553 for (offset_type i = 0; i < n_elements; i += 2)
2554 {
2555 gdb_static_assert (sizeof (ULONGEST) >= 8);
2556
2557 sect_offset sect_off
2558 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2559 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2560 cu_list += 2 * 8;
2561
2562 dwarf2_per_cu_data *per_cu
2563 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2564 length);
2565 per_bfd->all_comp_units.push_back (per_cu);
2566 }
2567 }
2568
2569 /* Read the CU list from the mapped index, and use it to create all
2570 the CU objects for PER_BFD. */
2571
2572 static void
2573 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2574 const gdb_byte *cu_list, offset_type cu_list_elements,
2575 const gdb_byte *dwz_list, offset_type dwz_elements)
2576 {
2577 gdb_assert (per_bfd->all_comp_units.empty ());
2578 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2579
2580 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2581 &per_bfd->info, 0);
2582
2583 if (dwz_elements == 0)
2584 return;
2585
2586 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2587 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2588 &dwz->info, 1);
2589 }
2590
2591 /* Create the signatured type hash table from the index. */
2592
2593 static void
2594 create_signatured_type_table_from_index
2595 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2596 const gdb_byte *bytes, offset_type elements)
2597 {
2598 gdb_assert (per_bfd->all_type_units.empty ());
2599 per_bfd->all_type_units.reserve (elements / 3);
2600
2601 htab_up sig_types_hash = allocate_signatured_type_table ();
2602
2603 for (offset_type i = 0; i < elements; i += 3)
2604 {
2605 struct signatured_type *sig_type;
2606 ULONGEST signature;
2607 void **slot;
2608 cu_offset type_offset_in_tu;
2609
2610 gdb_static_assert (sizeof (ULONGEST) >= 8);
2611 sect_offset sect_off
2612 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2613 type_offset_in_tu
2614 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2615 BFD_ENDIAN_LITTLE);
2616 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2617 bytes += 3 * 8;
2618
2619 sig_type = per_bfd->allocate_signatured_type ();
2620 sig_type->signature = signature;
2621 sig_type->type_offset_in_tu = type_offset_in_tu;
2622 sig_type->per_cu.is_debug_types = 1;
2623 sig_type->per_cu.section = section;
2624 sig_type->per_cu.sect_off = sect_off;
2625 sig_type->per_cu.v.quick
2626 = OBSTACK_ZALLOC (&per_bfd->obstack,
2627 struct dwarf2_per_cu_quick_data);
2628
2629 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2630 *slot = sig_type;
2631
2632 per_bfd->all_type_units.push_back (sig_type);
2633 }
2634
2635 per_bfd->signatured_types = std::move (sig_types_hash);
2636 }
2637
2638 /* Create the signatured type hash table from .debug_names. */
2639
2640 static void
2641 create_signatured_type_table_from_debug_names
2642 (dwarf2_per_objfile *per_objfile,
2643 const mapped_debug_names &map,
2644 struct dwarf2_section_info *section,
2645 struct dwarf2_section_info *abbrev_section)
2646 {
2647 struct objfile *objfile = per_objfile->objfile;
2648
2649 section->read (objfile);
2650 abbrev_section->read (objfile);
2651
2652 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2653 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2654
2655 htab_up sig_types_hash = allocate_signatured_type_table ();
2656
2657 for (uint32_t i = 0; i < map.tu_count; ++i)
2658 {
2659 struct signatured_type *sig_type;
2660 void **slot;
2661
2662 sect_offset sect_off
2663 = (sect_offset) (extract_unsigned_integer
2664 (map.tu_table_reordered + i * map.offset_size,
2665 map.offset_size,
2666 map.dwarf5_byte_order));
2667
2668 comp_unit_head cu_header;
2669 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2670 abbrev_section,
2671 section->buffer + to_underlying (sect_off),
2672 rcuh_kind::TYPE);
2673
2674 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
2675 sig_type->signature = cu_header.signature;
2676 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2677 sig_type->per_cu.is_debug_types = 1;
2678 sig_type->per_cu.section = section;
2679 sig_type->per_cu.sect_off = sect_off;
2680 sig_type->per_cu.v.quick
2681 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2682 struct dwarf2_per_cu_quick_data);
2683
2684 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2685 *slot = sig_type;
2686
2687 per_objfile->per_bfd->all_type_units.push_back (sig_type);
2688 }
2689
2690 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2691 }
2692
2693 /* Read the address map data from the mapped index, and use it to
2694 populate the objfile's psymtabs_addrmap. */
2695
2696 static void
2697 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2698 struct mapped_index *index)
2699 {
2700 struct objfile *objfile = per_objfile->objfile;
2701 struct gdbarch *gdbarch = objfile->arch ();
2702 const gdb_byte *iter, *end;
2703 struct addrmap *mutable_map;
2704 CORE_ADDR baseaddr;
2705
2706 auto_obstack temp_obstack;
2707
2708 mutable_map = addrmap_create_mutable (&temp_obstack);
2709
2710 iter = index->address_table.data ();
2711 end = iter + index->address_table.size ();
2712
2713 baseaddr = objfile->text_section_offset ();
2714
2715 while (iter < end)
2716 {
2717 ULONGEST hi, lo, cu_index;
2718 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2719 iter += 8;
2720 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2721 iter += 8;
2722 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2723 iter += 4;
2724
2725 if (lo > hi)
2726 {
2727 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2728 hex_string (lo), hex_string (hi));
2729 continue;
2730 }
2731
2732 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
2733 {
2734 complaint (_(".gdb_index address table has invalid CU number %u"),
2735 (unsigned) cu_index);
2736 continue;
2737 }
2738
2739 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2740 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2741 addrmap_set_empty (mutable_map, lo, hi - 1,
2742 per_objfile->per_bfd->get_cu (cu_index));
2743 }
2744
2745 objfile->partial_symtabs->psymtabs_addrmap
2746 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2747 }
2748
2749 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2750 populate the objfile's psymtabs_addrmap. */
2751
2752 static void
2753 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2754 struct dwarf2_section_info *section)
2755 {
2756 struct objfile *objfile = per_objfile->objfile;
2757 bfd *abfd = objfile->obfd;
2758 struct gdbarch *gdbarch = objfile->arch ();
2759 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2760
2761 auto_obstack temp_obstack;
2762 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2763
2764 std::unordered_map<sect_offset,
2765 dwarf2_per_cu_data *,
2766 gdb::hash_enum<sect_offset>>
2767 debug_info_offset_to_per_cu;
2768 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
2769 {
2770 const auto insertpair
2771 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2772 if (!insertpair.second)
2773 {
2774 warning (_("Section .debug_aranges in %s has duplicate "
2775 "debug_info_offset %s, ignoring .debug_aranges."),
2776 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2777 return;
2778 }
2779 }
2780
2781 section->read (objfile);
2782
2783 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2784
2785 const gdb_byte *addr = section->buffer;
2786
2787 while (addr < section->buffer + section->size)
2788 {
2789 const gdb_byte *const entry_addr = addr;
2790 unsigned int bytes_read;
2791
2792 const LONGEST entry_length = read_initial_length (abfd, addr,
2793 &bytes_read);
2794 addr += bytes_read;
2795
2796 const gdb_byte *const entry_end = addr + entry_length;
2797 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2798 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2799 if (addr + entry_length > section->buffer + section->size)
2800 {
2801 warning (_("Section .debug_aranges in %s entry at offset %s "
2802 "length %s exceeds section length %s, "
2803 "ignoring .debug_aranges."),
2804 objfile_name (objfile),
2805 plongest (entry_addr - section->buffer),
2806 plongest (bytes_read + entry_length),
2807 pulongest (section->size));
2808 return;
2809 }
2810
2811 /* The version number. */
2812 const uint16_t version = read_2_bytes (abfd, addr);
2813 addr += 2;
2814 if (version != 2)
2815 {
2816 warning (_("Section .debug_aranges in %s entry at offset %s "
2817 "has unsupported version %d, ignoring .debug_aranges."),
2818 objfile_name (objfile),
2819 plongest (entry_addr - section->buffer), version);
2820 return;
2821 }
2822
2823 const uint64_t debug_info_offset
2824 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2825 addr += offset_size;
2826 const auto per_cu_it
2827 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2828 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2829 {
2830 warning (_("Section .debug_aranges in %s entry at offset %s "
2831 "debug_info_offset %s does not exists, "
2832 "ignoring .debug_aranges."),
2833 objfile_name (objfile),
2834 plongest (entry_addr - section->buffer),
2835 pulongest (debug_info_offset));
2836 return;
2837 }
2838 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2839
2840 const uint8_t address_size = *addr++;
2841 if (address_size < 1 || address_size > 8)
2842 {
2843 warning (_("Section .debug_aranges in %s entry at offset %s "
2844 "address_size %u is invalid, ignoring .debug_aranges."),
2845 objfile_name (objfile),
2846 plongest (entry_addr - section->buffer), address_size);
2847 return;
2848 }
2849
2850 const uint8_t segment_selector_size = *addr++;
2851 if (segment_selector_size != 0)
2852 {
2853 warning (_("Section .debug_aranges in %s entry at offset %s "
2854 "segment_selector_size %u is not supported, "
2855 "ignoring .debug_aranges."),
2856 objfile_name (objfile),
2857 plongest (entry_addr - section->buffer),
2858 segment_selector_size);
2859 return;
2860 }
2861
2862 /* Must pad to an alignment boundary that is twice the address
2863 size. It is undocumented by the DWARF standard but GCC does
2864 use it. */
2865 for (size_t padding = ((-(addr - section->buffer))
2866 & (2 * address_size - 1));
2867 padding > 0; padding--)
2868 if (*addr++ != 0)
2869 {
2870 warning (_("Section .debug_aranges in %s entry at offset %s "
2871 "padding is not zero, ignoring .debug_aranges."),
2872 objfile_name (objfile),
2873 plongest (entry_addr - section->buffer));
2874 return;
2875 }
2876
2877 for (;;)
2878 {
2879 if (addr + 2 * address_size > entry_end)
2880 {
2881 warning (_("Section .debug_aranges in %s entry at offset %s "
2882 "address list is not properly terminated, "
2883 "ignoring .debug_aranges."),
2884 objfile_name (objfile),
2885 plongest (entry_addr - section->buffer));
2886 return;
2887 }
2888 ULONGEST start = extract_unsigned_integer (addr, address_size,
2889 dwarf5_byte_order);
2890 addr += address_size;
2891 ULONGEST length = extract_unsigned_integer (addr, address_size,
2892 dwarf5_byte_order);
2893 addr += address_size;
2894 if (start == 0 && length == 0)
2895 break;
2896 if (start == 0 && !per_objfile->per_bfd->has_section_at_zero)
2897 {
2898 /* Symbol was eliminated due to a COMDAT group. */
2899 continue;
2900 }
2901 ULONGEST end = start + length;
2902 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2903 - baseaddr);
2904 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2905 - baseaddr);
2906 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2907 }
2908 }
2909
2910 objfile->partial_symtabs->psymtabs_addrmap
2911 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2912 }
2913
2914 /* Find a slot in the mapped index INDEX for the object named NAME.
2915 If NAME is found, set *VEC_OUT to point to the CU vector in the
2916 constant pool and return true. If NAME cannot be found, return
2917 false. */
2918
2919 static bool
2920 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2921 offset_type **vec_out)
2922 {
2923 offset_type hash;
2924 offset_type slot, step;
2925 int (*cmp) (const char *, const char *);
2926
2927 gdb::unique_xmalloc_ptr<char> without_params;
2928 if (current_language->la_language == language_cplus
2929 || current_language->la_language == language_fortran
2930 || current_language->la_language == language_d)
2931 {
2932 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2933 not contain any. */
2934
2935 if (strchr (name, '(') != NULL)
2936 {
2937 without_params = cp_remove_params (name);
2938
2939 if (without_params != NULL)
2940 name = without_params.get ();
2941 }
2942 }
2943
2944 /* Index version 4 did not support case insensitive searches. But the
2945 indices for case insensitive languages are built in lowercase, therefore
2946 simulate our NAME being searched is also lowercased. */
2947 hash = mapped_index_string_hash ((index->version == 4
2948 && case_sensitivity == case_sensitive_off
2949 ? 5 : index->version),
2950 name);
2951
2952 slot = hash & (index->symbol_table.size () - 1);
2953 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2954 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2955
2956 for (;;)
2957 {
2958 const char *str;
2959
2960 const auto &bucket = index->symbol_table[slot];
2961 if (bucket.name == 0 && bucket.vec == 0)
2962 return false;
2963
2964 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2965 if (!cmp (name, str))
2966 {
2967 *vec_out = (offset_type *) (index->constant_pool
2968 + MAYBE_SWAP (bucket.vec));
2969 return true;
2970 }
2971
2972 slot = (slot + step) & (index->symbol_table.size () - 1);
2973 }
2974 }
2975
2976 /* A helper function that reads the .gdb_index from BUFFER and fills
2977 in MAP. FILENAME is the name of the file containing the data;
2978 it is used for error reporting. DEPRECATED_OK is true if it is
2979 ok to use deprecated sections.
2980
2981 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2982 out parameters that are filled in with information about the CU and
2983 TU lists in the section.
2984
2985 Returns true if all went well, false otherwise. */
2986
2987 static bool
2988 read_gdb_index_from_buffer (const char *filename,
2989 bool deprecated_ok,
2990 gdb::array_view<const gdb_byte> buffer,
2991 struct mapped_index *map,
2992 const gdb_byte **cu_list,
2993 offset_type *cu_list_elements,
2994 const gdb_byte **types_list,
2995 offset_type *types_list_elements)
2996 {
2997 const gdb_byte *addr = &buffer[0];
2998
2999 /* Version check. */
3000 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3001 /* Versions earlier than 3 emitted every copy of a psymbol. This
3002 causes the index to behave very poorly for certain requests. Version 3
3003 contained incomplete addrmap. So, it seems better to just ignore such
3004 indices. */
3005 if (version < 4)
3006 {
3007 static int warning_printed = 0;
3008 if (!warning_printed)
3009 {
3010 warning (_("Skipping obsolete .gdb_index section in %s."),
3011 filename);
3012 warning_printed = 1;
3013 }
3014 return 0;
3015 }
3016 /* Index version 4 uses a different hash function than index version
3017 5 and later.
3018
3019 Versions earlier than 6 did not emit psymbols for inlined
3020 functions. Using these files will cause GDB not to be able to
3021 set breakpoints on inlined functions by name, so we ignore these
3022 indices unless the user has done
3023 "set use-deprecated-index-sections on". */
3024 if (version < 6 && !deprecated_ok)
3025 {
3026 static int warning_printed = 0;
3027 if (!warning_printed)
3028 {
3029 warning (_("\
3030 Skipping deprecated .gdb_index section in %s.\n\
3031 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3032 to use the section anyway."),
3033 filename);
3034 warning_printed = 1;
3035 }
3036 return 0;
3037 }
3038 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3039 of the TU (for symbols coming from TUs),
3040 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3041 Plus gold-generated indices can have duplicate entries for global symbols,
3042 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3043 These are just performance bugs, and we can't distinguish gdb-generated
3044 indices from gold-generated ones, so issue no warning here. */
3045
3046 /* Indexes with higher version than the one supported by GDB may be no
3047 longer backward compatible. */
3048 if (version > 8)
3049 return 0;
3050
3051 map->version = version;
3052
3053 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3054
3055 int i = 0;
3056 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3057 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3058 / 8);
3059 ++i;
3060
3061 *types_list = addr + MAYBE_SWAP (metadata[i]);
3062 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3063 - MAYBE_SWAP (metadata[i]))
3064 / 8);
3065 ++i;
3066
3067 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3068 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3069 map->address_table
3070 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3071 ++i;
3072
3073 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3074 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3075 map->symbol_table
3076 = gdb::array_view<mapped_index::symbol_table_slot>
3077 ((mapped_index::symbol_table_slot *) symbol_table,
3078 (mapped_index::symbol_table_slot *) symbol_table_end);
3079
3080 ++i;
3081 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3082
3083 return 1;
3084 }
3085
3086 /* Callback types for dwarf2_read_gdb_index. */
3087
3088 typedef gdb::function_view
3089 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3090 get_gdb_index_contents_ftype;
3091 typedef gdb::function_view
3092 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3093 get_gdb_index_contents_dwz_ftype;
3094
3095 /* Read .gdb_index. If everything went ok, initialize the "quick"
3096 elements of all the CUs and return 1. Otherwise, return 0. */
3097
3098 static int
3099 dwarf2_read_gdb_index
3100 (dwarf2_per_objfile *per_objfile,
3101 get_gdb_index_contents_ftype get_gdb_index_contents,
3102 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3103 {
3104 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3105 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3106 struct dwz_file *dwz;
3107 struct objfile *objfile = per_objfile->objfile;
3108 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3109
3110 gdb::array_view<const gdb_byte> main_index_contents
3111 = get_gdb_index_contents (objfile, per_bfd);
3112
3113 if (main_index_contents.empty ())
3114 return 0;
3115
3116 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3117 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3118 use_deprecated_index_sections,
3119 main_index_contents, map.get (), &cu_list,
3120 &cu_list_elements, &types_list,
3121 &types_list_elements))
3122 return 0;
3123
3124 /* Don't use the index if it's empty. */
3125 if (map->symbol_table.empty ())
3126 return 0;
3127
3128 /* If there is a .dwz file, read it so we can get its CU list as
3129 well. */
3130 dwz = dwarf2_get_dwz_file (per_bfd);
3131 if (dwz != NULL)
3132 {
3133 struct mapped_index dwz_map;
3134 const gdb_byte *dwz_types_ignore;
3135 offset_type dwz_types_elements_ignore;
3136
3137 gdb::array_view<const gdb_byte> dwz_index_content
3138 = get_gdb_index_contents_dwz (objfile, dwz);
3139
3140 if (dwz_index_content.empty ())
3141 return 0;
3142
3143 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3144 1, dwz_index_content, &dwz_map,
3145 &dwz_list, &dwz_list_elements,
3146 &dwz_types_ignore,
3147 &dwz_types_elements_ignore))
3148 {
3149 warning (_("could not read '.gdb_index' section from %s; skipping"),
3150 bfd_get_filename (dwz->dwz_bfd.get ()));
3151 return 0;
3152 }
3153 }
3154
3155 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3156 dwz_list_elements);
3157
3158 if (types_list_elements)
3159 {
3160 /* We can only handle a single .debug_types when we have an
3161 index. */
3162 if (per_bfd->types.size () != 1)
3163 return 0;
3164
3165 dwarf2_section_info *section = &per_bfd->types[0];
3166
3167 create_signatured_type_table_from_index (per_bfd, section, types_list,
3168 types_list_elements);
3169 }
3170
3171 create_addrmap_from_index (per_objfile, map.get ());
3172
3173 per_bfd->index_table = std::move (map);
3174 per_bfd->using_index = 1;
3175 per_bfd->quick_file_names_table =
3176 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3177
3178 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
3179 objfiles using the same BFD. */
3180 gdb_assert (per_bfd->partial_symtabs == nullptr);
3181 per_bfd->partial_symtabs = objfile->partial_symtabs;
3182
3183 return 1;
3184 }
3185
3186 /* die_reader_func for dw2_get_file_names. */
3187
3188 static void
3189 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3190 const gdb_byte *info_ptr,
3191 struct die_info *comp_unit_die)
3192 {
3193 struct dwarf2_cu *cu = reader->cu;
3194 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3195 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3196 struct dwarf2_per_cu_data *lh_cu;
3197 struct attribute *attr;
3198 void **slot;
3199 struct quick_file_names *qfn;
3200
3201 gdb_assert (! this_cu->is_debug_types);
3202
3203 /* Our callers never want to match partial units -- instead they
3204 will match the enclosing full CU. */
3205 if (comp_unit_die->tag == DW_TAG_partial_unit)
3206 {
3207 this_cu->v.quick->no_file_data = 1;
3208 return;
3209 }
3210
3211 lh_cu = this_cu;
3212 slot = NULL;
3213
3214 line_header_up lh;
3215 sect_offset line_offset {};
3216
3217 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3218 if (attr != nullptr && attr->form_is_unsigned ())
3219 {
3220 struct quick_file_names find_entry;
3221
3222 line_offset = (sect_offset) attr->as_unsigned ();
3223
3224 /* We may have already read in this line header (TU line header sharing).
3225 If we have we're done. */
3226 find_entry.hash.dwo_unit = cu->dwo_unit;
3227 find_entry.hash.line_sect_off = line_offset;
3228 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3229 &find_entry, INSERT);
3230 if (*slot != NULL)
3231 {
3232 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3233 return;
3234 }
3235
3236 lh = dwarf_decode_line_header (line_offset, cu);
3237 }
3238 if (lh == NULL)
3239 {
3240 lh_cu->v.quick->no_file_data = 1;
3241 return;
3242 }
3243
3244 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3245 qfn->hash.dwo_unit = cu->dwo_unit;
3246 qfn->hash.line_sect_off = line_offset;
3247 gdb_assert (slot != NULL);
3248 *slot = qfn;
3249
3250 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3251
3252 int offset = 0;
3253 if (strcmp (fnd.name, "<unknown>") != 0)
3254 ++offset;
3255
3256 qfn->num_file_names = offset + lh->file_names_size ();
3257 qfn->file_names =
3258 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3259 qfn->num_file_names);
3260 if (offset != 0)
3261 qfn->file_names[0] = xstrdup (fnd.name);
3262 for (int i = 0; i < lh->file_names_size (); ++i)
3263 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3264 fnd.comp_dir).release ();
3265 qfn->real_names = NULL;
3266
3267 lh_cu->v.quick->file_names = qfn;
3268 }
3269
3270 /* A helper for the "quick" functions which attempts to read the line
3271 table for THIS_CU. */
3272
3273 static struct quick_file_names *
3274 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3275 dwarf2_per_objfile *per_objfile)
3276 {
3277 /* This should never be called for TUs. */
3278 gdb_assert (! this_cu->is_debug_types);
3279 /* Nor type unit groups. */
3280 gdb_assert (! this_cu->type_unit_group_p ());
3281
3282 if (this_cu->v.quick->file_names != NULL)
3283 return this_cu->v.quick->file_names;
3284 /* If we know there is no line data, no point in looking again. */
3285 if (this_cu->v.quick->no_file_data)
3286 return NULL;
3287
3288 cutu_reader reader (this_cu, per_objfile);
3289 if (!reader.dummy_p)
3290 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3291
3292 if (this_cu->v.quick->no_file_data)
3293 return NULL;
3294 return this_cu->v.quick->file_names;
3295 }
3296
3297 /* A helper for the "quick" functions which computes and caches the
3298 real path for a given file name from the line table. */
3299
3300 static const char *
3301 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3302 struct quick_file_names *qfn, int index)
3303 {
3304 if (qfn->real_names == NULL)
3305 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3306 qfn->num_file_names, const char *);
3307
3308 if (qfn->real_names[index] == NULL)
3309 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3310
3311 return qfn->real_names[index];
3312 }
3313
3314 static struct symtab *
3315 dw2_find_last_source_symtab (struct objfile *objfile)
3316 {
3317 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3318 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3319 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3320
3321 if (cust == NULL)
3322 return NULL;
3323
3324 return compunit_primary_filetab (cust);
3325 }
3326
3327 /* Traversal function for dw2_forget_cached_source_info. */
3328
3329 static int
3330 dw2_free_cached_file_names (void **slot, void *info)
3331 {
3332 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3333
3334 if (file_data->real_names)
3335 {
3336 int i;
3337
3338 for (i = 0; i < file_data->num_file_names; ++i)
3339 {
3340 xfree ((void*) file_data->real_names[i]);
3341 file_data->real_names[i] = NULL;
3342 }
3343 }
3344
3345 return 1;
3346 }
3347
3348 static void
3349 dw2_forget_cached_source_info (struct objfile *objfile)
3350 {
3351 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3352
3353 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3354 dw2_free_cached_file_names, NULL);
3355 }
3356
3357 /* Helper function for dw2_map_symtabs_matching_filename that expands
3358 the symtabs and calls the iterator. */
3359
3360 static int
3361 dw2_map_expand_apply (struct objfile *objfile,
3362 struct dwarf2_per_cu_data *per_cu,
3363 const char *name, const char *real_path,
3364 gdb::function_view<bool (symtab *)> callback)
3365 {
3366 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3367
3368 /* Don't visit already-expanded CUs. */
3369 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3370 if (per_objfile->symtab_set_p (per_cu))
3371 return 0;
3372
3373 /* This may expand more than one symtab, and we want to iterate over
3374 all of them. */
3375 dw2_instantiate_symtab (per_cu, per_objfile, false);
3376
3377 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3378 last_made, callback);
3379 }
3380
3381 /* Implementation of the map_symtabs_matching_filename method. */
3382
3383 static bool
3384 dw2_map_symtabs_matching_filename
3385 (struct objfile *objfile, const char *name, const char *real_path,
3386 gdb::function_view<bool (symtab *)> callback)
3387 {
3388 const char *name_basename = lbasename (name);
3389 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3390
3391 /* The rule is CUs specify all the files, including those used by
3392 any TU, so there's no need to scan TUs here. */
3393
3394 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3395 {
3396 /* We only need to look at symtabs not already expanded. */
3397 if (per_objfile->symtab_set_p (per_cu))
3398 continue;
3399
3400 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3401 if (file_data == NULL)
3402 continue;
3403
3404 for (int j = 0; j < file_data->num_file_names; ++j)
3405 {
3406 const char *this_name = file_data->file_names[j];
3407 const char *this_real_name;
3408
3409 if (compare_filenames_for_search (this_name, name))
3410 {
3411 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3412 callback))
3413 return true;
3414 continue;
3415 }
3416
3417 /* Before we invoke realpath, which can get expensive when many
3418 files are involved, do a quick comparison of the basenames. */
3419 if (! basenames_may_differ
3420 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3421 continue;
3422
3423 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
3424 if (compare_filenames_for_search (this_real_name, name))
3425 {
3426 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3427 callback))
3428 return true;
3429 continue;
3430 }
3431
3432 if (real_path != NULL)
3433 {
3434 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3435 gdb_assert (IS_ABSOLUTE_PATH (name));
3436 if (this_real_name != NULL
3437 && FILENAME_CMP (real_path, this_real_name) == 0)
3438 {
3439 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3440 callback))
3441 return true;
3442 continue;
3443 }
3444 }
3445 }
3446 }
3447
3448 return false;
3449 }
3450
3451 /* Struct used to manage iterating over all CUs looking for a symbol. */
3452
3453 struct dw2_symtab_iterator
3454 {
3455 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3456 dwarf2_per_objfile *per_objfile;
3457 /* If set, only look for symbols that match that block. Valid values are
3458 GLOBAL_BLOCK and STATIC_BLOCK. */
3459 gdb::optional<block_enum> block_index;
3460 /* The kind of symbol we're looking for. */
3461 domain_enum domain;
3462 /* The list of CUs from the index entry of the symbol,
3463 or NULL if not found. */
3464 offset_type *vec;
3465 /* The next element in VEC to look at. */
3466 int next;
3467 /* The number of elements in VEC, or zero if there is no match. */
3468 int length;
3469 /* Have we seen a global version of the symbol?
3470 If so we can ignore all further global instances.
3471 This is to work around gold/15646, inefficient gold-generated
3472 indices. */
3473 int global_seen;
3474 };
3475
3476 /* Initialize the index symtab iterator ITER, common part. */
3477
3478 static void
3479 dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3480 dwarf2_per_objfile *per_objfile,
3481 gdb::optional<block_enum> block_index,
3482 domain_enum domain)
3483 {
3484 iter->per_objfile = per_objfile;
3485 iter->block_index = block_index;
3486 iter->domain = domain;
3487 iter->next = 0;
3488 iter->global_seen = 0;
3489 iter->vec = NULL;
3490 iter->length = 0;
3491 }
3492
3493 /* Initialize the index symtab iterator ITER, const char *NAME variant. */
3494
3495 static void
3496 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3497 dwarf2_per_objfile *per_objfile,
3498 gdb::optional<block_enum> block_index,
3499 domain_enum domain,
3500 const char *name)
3501 {
3502 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3503
3504 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3505 /* index is NULL if OBJF_READNOW. */
3506 if (index == NULL)
3507 return;
3508
3509 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3510 iter->length = MAYBE_SWAP (*iter->vec);
3511 }
3512
3513 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3514
3515 static void
3516 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3517 dwarf2_per_objfile *per_objfile,
3518 gdb::optional<block_enum> block_index,
3519 domain_enum domain, offset_type namei)
3520 {
3521 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3522
3523 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3524 /* index is NULL if OBJF_READNOW. */
3525 if (index == NULL)
3526 return;
3527
3528 gdb_assert (!index->symbol_name_slot_invalid (namei));
3529 const auto &bucket = index->symbol_table[namei];
3530
3531 iter->vec = (offset_type *) (index->constant_pool
3532 + MAYBE_SWAP (bucket.vec));
3533 iter->length = MAYBE_SWAP (*iter->vec);
3534 }
3535
3536 /* Return the next matching CU or NULL if there are no more. */
3537
3538 static struct dwarf2_per_cu_data *
3539 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3540 {
3541 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3542
3543 for ( ; iter->next < iter->length; ++iter->next)
3544 {
3545 offset_type cu_index_and_attrs =
3546 MAYBE_SWAP (iter->vec[iter->next + 1]);
3547 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3548 gdb_index_symbol_kind symbol_kind =
3549 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3550 /* Only check the symbol attributes if they're present.
3551 Indices prior to version 7 don't record them,
3552 and indices >= 7 may elide them for certain symbols
3553 (gold does this). */
3554 int attrs_valid =
3555 (per_objfile->per_bfd->index_table->version >= 7
3556 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3557
3558 /* Don't crash on bad data. */
3559 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3560 + per_objfile->per_bfd->all_type_units.size ()))
3561 {
3562 complaint (_(".gdb_index entry has bad CU index"
3563 " [in module %s]"), objfile_name (per_objfile->objfile));
3564 continue;
3565 }
3566
3567 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3568
3569 /* Skip if already read in. */
3570 if (per_objfile->symtab_set_p (per_cu))
3571 continue;
3572
3573 /* Check static vs global. */
3574 if (attrs_valid)
3575 {
3576 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3577
3578 if (iter->block_index.has_value ())
3579 {
3580 bool want_static = *iter->block_index == STATIC_BLOCK;
3581
3582 if (is_static != want_static)
3583 continue;
3584 }
3585
3586 /* Work around gold/15646. */
3587 if (!is_static
3588 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3589 {
3590 if (iter->global_seen)
3591 continue;
3592
3593 iter->global_seen = 1;
3594 }
3595 }
3596
3597 /* Only check the symbol's kind if it has one. */
3598 if (attrs_valid)
3599 {
3600 switch (iter->domain)
3601 {
3602 case VAR_DOMAIN:
3603 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3604 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3605 /* Some types are also in VAR_DOMAIN. */
3606 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3607 continue;
3608 break;
3609 case STRUCT_DOMAIN:
3610 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3611 continue;
3612 break;
3613 case LABEL_DOMAIN:
3614 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3615 continue;
3616 break;
3617 case MODULE_DOMAIN:
3618 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3619 continue;
3620 break;
3621 default:
3622 break;
3623 }
3624 }
3625
3626 ++iter->next;
3627 return per_cu;
3628 }
3629
3630 return NULL;
3631 }
3632
3633 static struct compunit_symtab *
3634 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3635 const char *name, domain_enum domain)
3636 {
3637 struct compunit_symtab *stab_best = NULL;
3638 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3639
3640 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3641
3642 struct dw2_symtab_iterator iter;
3643 struct dwarf2_per_cu_data *per_cu;
3644
3645 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
3646
3647 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3648 {
3649 struct symbol *sym, *with_opaque = NULL;
3650 struct compunit_symtab *stab
3651 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3652 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3653 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3654
3655 sym = block_find_symbol (block, name, domain,
3656 block_find_non_opaque_type_preferred,
3657 &with_opaque);
3658
3659 /* Some caution must be observed with overloaded functions
3660 and methods, since the index will not contain any overload
3661 information (but NAME might contain it). */
3662
3663 if (sym != NULL
3664 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3665 return stab;
3666 if (with_opaque != NULL
3667 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3668 stab_best = stab;
3669
3670 /* Keep looking through other CUs. */
3671 }
3672
3673 return stab_best;
3674 }
3675
3676 static void
3677 dw2_print_stats (struct objfile *objfile)
3678 {
3679 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3680 int total = (per_objfile->per_bfd->all_comp_units.size ()
3681 + per_objfile->per_bfd->all_type_units.size ());
3682 int count = 0;
3683
3684 for (int i = 0; i < total; ++i)
3685 {
3686 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3687
3688 if (!per_objfile->symtab_set_p (per_cu))
3689 ++count;
3690 }
3691 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3692 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3693 }
3694
3695 /* This dumps minimal information about the index.
3696 It is called via "mt print objfiles".
3697 One use is to verify .gdb_index has been loaded by the
3698 gdb.dwarf2/gdb-index.exp testcase. */
3699
3700 static void
3701 dw2_dump (struct objfile *objfile)
3702 {
3703 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3704
3705 gdb_assert (per_objfile->per_bfd->using_index);
3706 printf_filtered (".gdb_index:");
3707 if (per_objfile->per_bfd->index_table != NULL)
3708 {
3709 printf_filtered (" version %d\n",
3710 per_objfile->per_bfd->index_table->version);
3711 }
3712 else
3713 printf_filtered (" faked for \"readnow\"\n");
3714 printf_filtered ("\n");
3715 }
3716
3717 static void
3718 dw2_expand_symtabs_for_function (struct objfile *objfile,
3719 const char *func_name)
3720 {
3721 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3722
3723 struct dw2_symtab_iterator iter;
3724 struct dwarf2_per_cu_data *per_cu;
3725
3726 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
3727
3728 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3729 dw2_instantiate_symtab (per_cu, per_objfile, false);
3730
3731 }
3732
3733 static void
3734 dw2_expand_all_symtabs (struct objfile *objfile)
3735 {
3736 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3737 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3738 + per_objfile->per_bfd->all_type_units.size ());
3739
3740 for (int i = 0; i < total_units; ++i)
3741 {
3742 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3743
3744 /* We don't want to directly expand a partial CU, because if we
3745 read it with the wrong language, then assertion failures can
3746 be triggered later on. See PR symtab/23010. So, tell
3747 dw2_instantiate_symtab to skip partial CUs -- any important
3748 partial CU will be read via DW_TAG_imported_unit anyway. */
3749 dw2_instantiate_symtab (per_cu, per_objfile, true);
3750 }
3751 }
3752
3753 static void
3754 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3755 const char *fullname)
3756 {
3757 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3758
3759 /* We don't need to consider type units here.
3760 This is only called for examining code, e.g. expand_line_sal.
3761 There can be an order of magnitude (or more) more type units
3762 than comp units, and we avoid them if we can. */
3763
3764 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3765 {
3766 /* We only need to look at symtabs not already expanded. */
3767 if (per_objfile->symtab_set_p (per_cu))
3768 continue;
3769
3770 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3771 if (file_data == NULL)
3772 continue;
3773
3774 for (int j = 0; j < file_data->num_file_names; ++j)
3775 {
3776 const char *this_fullname = file_data->file_names[j];
3777
3778 if (filename_cmp (this_fullname, fullname) == 0)
3779 {
3780 dw2_instantiate_symtab (per_cu, per_objfile, false);
3781 break;
3782 }
3783 }
3784 }
3785 }
3786
3787 static void
3788 dw2_expand_symtabs_matching_symbol
3789 (mapped_index_base &index,
3790 const lookup_name_info &lookup_name_in,
3791 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3792 enum search_domain kind,
3793 gdb::function_view<bool (offset_type)> match_callback,
3794 dwarf2_per_objfile *per_objfile);
3795
3796 static void
3797 dw2_expand_symtabs_matching_one
3798 (dwarf2_per_cu_data *per_cu,
3799 dwarf2_per_objfile *per_objfile,
3800 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3801 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3802
3803 static void
3804 dw2_map_matching_symbols
3805 (struct objfile *objfile,
3806 const lookup_name_info &name, domain_enum domain,
3807 int global,
3808 gdb::function_view<symbol_found_callback_ftype> callback,
3809 symbol_compare_ftype *ordered_compare)
3810 {
3811 /* Used for Ada. */
3812 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3813
3814 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3815
3816 if (per_objfile->per_bfd->index_table != nullptr)
3817 {
3818 mapped_index &index = *per_objfile->per_bfd->index_table;
3819
3820 const char *match_name = name.ada ().lookup_name ().c_str ();
3821 auto matcher = [&] (const char *symname)
3822 {
3823 if (ordered_compare == nullptr)
3824 return true;
3825 return ordered_compare (symname, match_name) == 0;
3826 };
3827
3828 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3829 [&] (offset_type namei)
3830 {
3831 struct dw2_symtab_iterator iter;
3832 struct dwarf2_per_cu_data *per_cu;
3833
3834 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3835 namei);
3836 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3837 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3838 nullptr);
3839 return true;
3840 }, per_objfile);
3841 }
3842 else
3843 {
3844 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3845 proceed assuming all symtabs have been read in. */
3846 }
3847
3848 for (compunit_symtab *cust : objfile->compunits ())
3849 {
3850 const struct block *block;
3851
3852 if (cust == NULL)
3853 continue;
3854 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3855 if (!iterate_over_symbols_terminated (block, name,
3856 domain, callback))
3857 return;
3858 }
3859 }
3860
3861 /* Starting from a search name, return the string that finds the upper
3862 bound of all strings that start with SEARCH_NAME in a sorted name
3863 list. Returns the empty string to indicate that the upper bound is
3864 the end of the list. */
3865
3866 static std::string
3867 make_sort_after_prefix_name (const char *search_name)
3868 {
3869 /* When looking to complete "func", we find the upper bound of all
3870 symbols that start with "func" by looking for where we'd insert
3871 the closest string that would follow "func" in lexicographical
3872 order. Usually, that's "func"-with-last-character-incremented,
3873 i.e. "fund". Mind non-ASCII characters, though. Usually those
3874 will be UTF-8 multi-byte sequences, but we can't be certain.
3875 Especially mind the 0xff character, which is a valid character in
3876 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3877 rule out compilers allowing it in identifiers. Note that
3878 conveniently, strcmp/strcasecmp are specified to compare
3879 characters interpreted as unsigned char. So what we do is treat
3880 the whole string as a base 256 number composed of a sequence of
3881 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3882 to 0, and carries 1 to the following more-significant position.
3883 If the very first character in SEARCH_NAME ends up incremented
3884 and carries/overflows, then the upper bound is the end of the
3885 list. The string after the empty string is also the empty
3886 string.
3887
3888 Some examples of this operation:
3889
3890 SEARCH_NAME => "+1" RESULT
3891
3892 "abc" => "abd"
3893 "ab\xff" => "ac"
3894 "\xff" "a" "\xff" => "\xff" "b"
3895 "\xff" => ""
3896 "\xff\xff" => ""
3897 "" => ""
3898
3899 Then, with these symbols for example:
3900
3901 func
3902 func1
3903 fund
3904
3905 completing "func" looks for symbols between "func" and
3906 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3907 which finds "func" and "func1", but not "fund".
3908
3909 And with:
3910
3911 funcÿ (Latin1 'ÿ' [0xff])
3912 funcÿ1
3913 fund
3914
3915 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3916 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3917
3918 And with:
3919
3920 ÿÿ (Latin1 'ÿ' [0xff])
3921 ÿÿ1
3922
3923 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3924 the end of the list.
3925 */
3926 std::string after = search_name;
3927 while (!after.empty () && (unsigned char) after.back () == 0xff)
3928 after.pop_back ();
3929 if (!after.empty ())
3930 after.back () = (unsigned char) after.back () + 1;
3931 return after;
3932 }
3933
3934 /* See declaration. */
3935
3936 std::pair<std::vector<name_component>::const_iterator,
3937 std::vector<name_component>::const_iterator>
3938 mapped_index_base::find_name_components_bounds
3939 (const lookup_name_info &lookup_name_without_params, language lang,
3940 dwarf2_per_objfile *per_objfile) const
3941 {
3942 auto *name_cmp
3943 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3944
3945 const char *lang_name
3946 = lookup_name_without_params.language_lookup_name (lang);
3947
3948 /* Comparison function object for lower_bound that matches against a
3949 given symbol name. */
3950 auto lookup_compare_lower = [&] (const name_component &elem,
3951 const char *name)
3952 {
3953 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3954 const char *elem_name = elem_qualified + elem.name_offset;
3955 return name_cmp (elem_name, name) < 0;
3956 };
3957
3958 /* Comparison function object for upper_bound that matches against a
3959 given symbol name. */
3960 auto lookup_compare_upper = [&] (const char *name,
3961 const name_component &elem)
3962 {
3963 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3964 const char *elem_name = elem_qualified + elem.name_offset;
3965 return name_cmp (name, elem_name) < 0;
3966 };
3967
3968 auto begin = this->name_components.begin ();
3969 auto end = this->name_components.end ();
3970
3971 /* Find the lower bound. */
3972 auto lower = [&] ()
3973 {
3974 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3975 return begin;
3976 else
3977 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3978 } ();
3979
3980 /* Find the upper bound. */
3981 auto upper = [&] ()
3982 {
3983 if (lookup_name_without_params.completion_mode ())
3984 {
3985 /* In completion mode, we want UPPER to point past all
3986 symbols names that have the same prefix. I.e., with
3987 these symbols, and completing "func":
3988
3989 function << lower bound
3990 function1
3991 other_function << upper bound
3992
3993 We find the upper bound by looking for the insertion
3994 point of "func"-with-last-character-incremented,
3995 i.e. "fund". */
3996 std::string after = make_sort_after_prefix_name (lang_name);
3997 if (after.empty ())
3998 return end;
3999 return std::lower_bound (lower, end, after.c_str (),
4000 lookup_compare_lower);
4001 }
4002 else
4003 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4004 } ();
4005
4006 return {lower, upper};
4007 }
4008
4009 /* See declaration. */
4010
4011 void
4012 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
4013 {
4014 if (!this->name_components.empty ())
4015 return;
4016
4017 this->name_components_casing = case_sensitivity;
4018 auto *name_cmp
4019 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4020
4021 /* The code below only knows how to break apart components of C++
4022 symbol names (and other languages that use '::' as
4023 namespace/module separator) and Ada symbol names. */
4024 auto count = this->symbol_name_count ();
4025 for (offset_type idx = 0; idx < count; idx++)
4026 {
4027 if (this->symbol_name_slot_invalid (idx))
4028 continue;
4029
4030 const char *name = this->symbol_name_at (idx, per_objfile);
4031
4032 /* Add each name component to the name component table. */
4033 unsigned int previous_len = 0;
4034
4035 if (strstr (name, "::") != nullptr)
4036 {
4037 for (unsigned int current_len = cp_find_first_component (name);
4038 name[current_len] != '\0';
4039 current_len += cp_find_first_component (name + current_len))
4040 {
4041 gdb_assert (name[current_len] == ':');
4042 this->name_components.push_back ({previous_len, idx});
4043 /* Skip the '::'. */
4044 current_len += 2;
4045 previous_len = current_len;
4046 }
4047 }
4048 else
4049 {
4050 /* Handle the Ada encoded (aka mangled) form here. */
4051 for (const char *iter = strstr (name, "__");
4052 iter != nullptr;
4053 iter = strstr (iter, "__"))
4054 {
4055 this->name_components.push_back ({previous_len, idx});
4056 iter += 2;
4057 previous_len = iter - name;
4058 }
4059 }
4060
4061 this->name_components.push_back ({previous_len, idx});
4062 }
4063
4064 /* Sort name_components elements by name. */
4065 auto name_comp_compare = [&] (const name_component &left,
4066 const name_component &right)
4067 {
4068 const char *left_qualified
4069 = this->symbol_name_at (left.idx, per_objfile);
4070 const char *right_qualified
4071 = this->symbol_name_at (right.idx, per_objfile);
4072
4073 const char *left_name = left_qualified + left.name_offset;
4074 const char *right_name = right_qualified + right.name_offset;
4075
4076 return name_cmp (left_name, right_name) < 0;
4077 };
4078
4079 std::sort (this->name_components.begin (),
4080 this->name_components.end (),
4081 name_comp_compare);
4082 }
4083
4084 /* Helper for dw2_expand_symtabs_matching that works with a
4085 mapped_index_base instead of the containing objfile. This is split
4086 to a separate function in order to be able to unit test the
4087 name_components matching using a mock mapped_index_base. For each
4088 symbol name that matches, calls MATCH_CALLBACK, passing it the
4089 symbol's index in the mapped_index_base symbol table. */
4090
4091 static void
4092 dw2_expand_symtabs_matching_symbol
4093 (mapped_index_base &index,
4094 const lookup_name_info &lookup_name_in,
4095 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4096 enum search_domain kind,
4097 gdb::function_view<bool (offset_type)> match_callback,
4098 dwarf2_per_objfile *per_objfile)
4099 {
4100 lookup_name_info lookup_name_without_params
4101 = lookup_name_in.make_ignore_params ();
4102
4103 /* Build the symbol name component sorted vector, if we haven't
4104 yet. */
4105 index.build_name_components (per_objfile);
4106
4107 /* The same symbol may appear more than once in the range though.
4108 E.g., if we're looking for symbols that complete "w", and we have
4109 a symbol named "w1::w2", we'll find the two name components for
4110 that same symbol in the range. To be sure we only call the
4111 callback once per symbol, we first collect the symbol name
4112 indexes that matched in a temporary vector and ignore
4113 duplicates. */
4114 std::vector<offset_type> matches;
4115
4116 struct name_and_matcher
4117 {
4118 symbol_name_matcher_ftype *matcher;
4119 const char *name;
4120
4121 bool operator== (const name_and_matcher &other) const
4122 {
4123 return matcher == other.matcher && strcmp (name, other.name) == 0;
4124 }
4125 };
4126
4127 /* A vector holding all the different symbol name matchers, for all
4128 languages. */
4129 std::vector<name_and_matcher> matchers;
4130
4131 for (int i = 0; i < nr_languages; i++)
4132 {
4133 enum language lang_e = (enum language) i;
4134
4135 const language_defn *lang = language_def (lang_e);
4136 symbol_name_matcher_ftype *name_matcher
4137 = lang->get_symbol_name_matcher (lookup_name_without_params);
4138
4139 name_and_matcher key {
4140 name_matcher,
4141 lookup_name_without_params.language_lookup_name (lang_e)
4142 };
4143
4144 /* Don't insert the same comparison routine more than once.
4145 Note that we do this linear walk. This is not a problem in
4146 practice because the number of supported languages is
4147 low. */
4148 if (std::find (matchers.begin (), matchers.end (), key)
4149 != matchers.end ())
4150 continue;
4151 matchers.push_back (std::move (key));
4152
4153 auto bounds
4154 = index.find_name_components_bounds (lookup_name_without_params,
4155 lang_e, per_objfile);
4156
4157 /* Now for each symbol name in range, check to see if we have a name
4158 match, and if so, call the MATCH_CALLBACK callback. */
4159
4160 for (; bounds.first != bounds.second; ++bounds.first)
4161 {
4162 const char *qualified
4163 = index.symbol_name_at (bounds.first->idx, per_objfile);
4164
4165 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4166 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4167 continue;
4168
4169 matches.push_back (bounds.first->idx);
4170 }
4171 }
4172
4173 std::sort (matches.begin (), matches.end ());
4174
4175 /* Finally call the callback, once per match. */
4176 ULONGEST prev = -1;
4177 for (offset_type idx : matches)
4178 {
4179 if (prev != idx)
4180 {
4181 if (!match_callback (idx))
4182 break;
4183 prev = idx;
4184 }
4185 }
4186
4187 /* Above we use a type wider than idx's for 'prev', since 0 and
4188 (offset_type)-1 are both possible values. */
4189 static_assert (sizeof (prev) > sizeof (offset_type), "");
4190 }
4191
4192 #if GDB_SELF_TEST
4193
4194 namespace selftests { namespace dw2_expand_symtabs_matching {
4195
4196 /* A mock .gdb_index/.debug_names-like name index table, enough to
4197 exercise dw2_expand_symtabs_matching_symbol, which works with the
4198 mapped_index_base interface. Builds an index from the symbol list
4199 passed as parameter to the constructor. */
4200 class mock_mapped_index : public mapped_index_base
4201 {
4202 public:
4203 mock_mapped_index (gdb::array_view<const char *> symbols)
4204 : m_symbol_table (symbols)
4205 {}
4206
4207 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4208
4209 /* Return the number of names in the symbol table. */
4210 size_t symbol_name_count () const override
4211 {
4212 return m_symbol_table.size ();
4213 }
4214
4215 /* Get the name of the symbol at IDX in the symbol table. */
4216 const char *symbol_name_at
4217 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
4218 {
4219 return m_symbol_table[idx];
4220 }
4221
4222 private:
4223 gdb::array_view<const char *> m_symbol_table;
4224 };
4225
4226 /* Convenience function that converts a NULL pointer to a "<null>"
4227 string, to pass to print routines. */
4228
4229 static const char *
4230 string_or_null (const char *str)
4231 {
4232 return str != NULL ? str : "<null>";
4233 }
4234
4235 /* Check if a lookup_name_info built from
4236 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4237 index. EXPECTED_LIST is the list of expected matches, in expected
4238 matching order. If no match expected, then an empty list is
4239 specified. Returns true on success. On failure prints a warning
4240 indicating the file:line that failed, and returns false. */
4241
4242 static bool
4243 check_match (const char *file, int line,
4244 mock_mapped_index &mock_index,
4245 const char *name, symbol_name_match_type match_type,
4246 bool completion_mode,
4247 std::initializer_list<const char *> expected_list,
4248 dwarf2_per_objfile *per_objfile)
4249 {
4250 lookup_name_info lookup_name (name, match_type, completion_mode);
4251
4252 bool matched = true;
4253
4254 auto mismatch = [&] (const char *expected_str,
4255 const char *got)
4256 {
4257 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4258 "expected=\"%s\", got=\"%s\"\n"),
4259 file, line,
4260 (match_type == symbol_name_match_type::FULL
4261 ? "FULL" : "WILD"),
4262 name, string_or_null (expected_str), string_or_null (got));
4263 matched = false;
4264 };
4265
4266 auto expected_it = expected_list.begin ();
4267 auto expected_end = expected_list.end ();
4268
4269 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4270 NULL, ALL_DOMAIN,
4271 [&] (offset_type idx)
4272 {
4273 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
4274 const char *expected_str
4275 = expected_it == expected_end ? NULL : *expected_it++;
4276
4277 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4278 mismatch (expected_str, matched_name);
4279 return true;
4280 }, per_objfile);
4281
4282 const char *expected_str
4283 = expected_it == expected_end ? NULL : *expected_it++;
4284 if (expected_str != NULL)
4285 mismatch (expected_str, NULL);
4286
4287 return matched;
4288 }
4289
4290 /* The symbols added to the mock mapped_index for testing (in
4291 canonical form). */
4292 static const char *test_symbols[] = {
4293 "function",
4294 "std::bar",
4295 "std::zfunction",
4296 "std::zfunction2",
4297 "w1::w2",
4298 "ns::foo<char*>",
4299 "ns::foo<int>",
4300 "ns::foo<long>",
4301 "ns2::tmpl<int>::foo2",
4302 "(anonymous namespace)::A::B::C",
4303
4304 /* These are used to check that the increment-last-char in the
4305 matching algorithm for completion doesn't match "t1_fund" when
4306 completing "t1_func". */
4307 "t1_func",
4308 "t1_func1",
4309 "t1_fund",
4310 "t1_fund1",
4311
4312 /* A UTF-8 name with multi-byte sequences to make sure that
4313 cp-name-parser understands this as a single identifier ("função"
4314 is "function" in PT). */
4315 u8"u8função",
4316
4317 /* \377 (0xff) is Latin1 'ÿ'. */
4318 "yfunc\377",
4319
4320 /* \377 (0xff) is Latin1 'ÿ'. */
4321 "\377",
4322 "\377\377123",
4323
4324 /* A name with all sorts of complications. Starts with "z" to make
4325 it easier for the completion tests below. */
4326 #define Z_SYM_NAME \
4327 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4328 "::tuple<(anonymous namespace)::ui*, " \
4329 "std::default_delete<(anonymous namespace)::ui>, void>"
4330
4331 Z_SYM_NAME
4332 };
4333
4334 /* Returns true if the mapped_index_base::find_name_component_bounds
4335 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4336 in completion mode. */
4337
4338 static bool
4339 check_find_bounds_finds (mapped_index_base &index,
4340 const char *search_name,
4341 gdb::array_view<const char *> expected_syms,
4342 dwarf2_per_objfile *per_objfile)
4343 {
4344 lookup_name_info lookup_name (search_name,
4345 symbol_name_match_type::FULL, true);
4346
4347 auto bounds = index.find_name_components_bounds (lookup_name,
4348 language_cplus,
4349 per_objfile);
4350
4351 size_t distance = std::distance (bounds.first, bounds.second);
4352 if (distance != expected_syms.size ())
4353 return false;
4354
4355 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4356 {
4357 auto nc_elem = bounds.first + exp_elem;
4358 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4359 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4360 return false;
4361 }
4362
4363 return true;
4364 }
4365
4366 /* Test the lower-level mapped_index::find_name_component_bounds
4367 method. */
4368
4369 static void
4370 test_mapped_index_find_name_component_bounds ()
4371 {
4372 mock_mapped_index mock_index (test_symbols);
4373
4374 mock_index.build_name_components (NULL /* per_objfile */);
4375
4376 /* Test the lower-level mapped_index::find_name_component_bounds
4377 method in completion mode. */
4378 {
4379 static const char *expected_syms[] = {
4380 "t1_func",
4381 "t1_func1",
4382 };
4383
4384 SELF_CHECK (check_find_bounds_finds
4385 (mock_index, "t1_func", expected_syms,
4386 NULL /* per_objfile */));
4387 }
4388
4389 /* Check that the increment-last-char in the name matching algorithm
4390 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4391 {
4392 static const char *expected_syms1[] = {
4393 "\377",
4394 "\377\377123",
4395 };
4396 SELF_CHECK (check_find_bounds_finds
4397 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4398
4399 static const char *expected_syms2[] = {
4400 "\377\377123",
4401 };
4402 SELF_CHECK (check_find_bounds_finds
4403 (mock_index, "\377\377", expected_syms2,
4404 NULL /* per_objfile */));
4405 }
4406 }
4407
4408 /* Test dw2_expand_symtabs_matching_symbol. */
4409
4410 static void
4411 test_dw2_expand_symtabs_matching_symbol ()
4412 {
4413 mock_mapped_index mock_index (test_symbols);
4414
4415 /* We let all tests run until the end even if some fails, for debug
4416 convenience. */
4417 bool any_mismatch = false;
4418
4419 /* Create the expected symbols list (an initializer_list). Needed
4420 because lists have commas, and we need to pass them to CHECK,
4421 which is a macro. */
4422 #define EXPECT(...) { __VA_ARGS__ }
4423
4424 /* Wrapper for check_match that passes down the current
4425 __FILE__/__LINE__. */
4426 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4427 any_mismatch |= !check_match (__FILE__, __LINE__, \
4428 mock_index, \
4429 NAME, MATCH_TYPE, COMPLETION_MODE, \
4430 EXPECTED_LIST, NULL)
4431
4432 /* Identity checks. */
4433 for (const char *sym : test_symbols)
4434 {
4435 /* Should be able to match all existing symbols. */
4436 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4437 EXPECT (sym));
4438
4439 /* Should be able to match all existing symbols with
4440 parameters. */
4441 std::string with_params = std::string (sym) + "(int)";
4442 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4443 EXPECT (sym));
4444
4445 /* Should be able to match all existing symbols with
4446 parameters and qualifiers. */
4447 with_params = std::string (sym) + " ( int ) const";
4448 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4449 EXPECT (sym));
4450
4451 /* This should really find sym, but cp-name-parser.y doesn't
4452 know about lvalue/rvalue qualifiers yet. */
4453 with_params = std::string (sym) + " ( int ) &&";
4454 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4455 {});
4456 }
4457
4458 /* Check that the name matching algorithm for completion doesn't get
4459 confused with Latin1 'ÿ' / 0xff. */
4460 {
4461 static const char str[] = "\377";
4462 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4463 EXPECT ("\377", "\377\377123"));
4464 }
4465
4466 /* Check that the increment-last-char in the matching algorithm for
4467 completion doesn't match "t1_fund" when completing "t1_func". */
4468 {
4469 static const char str[] = "t1_func";
4470 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4471 EXPECT ("t1_func", "t1_func1"));
4472 }
4473
4474 /* Check that completion mode works at each prefix of the expected
4475 symbol name. */
4476 {
4477 static const char str[] = "function(int)";
4478 size_t len = strlen (str);
4479 std::string lookup;
4480
4481 for (size_t i = 1; i < len; i++)
4482 {
4483 lookup.assign (str, i);
4484 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4485 EXPECT ("function"));
4486 }
4487 }
4488
4489 /* While "w" is a prefix of both components, the match function
4490 should still only be called once. */
4491 {
4492 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4493 EXPECT ("w1::w2"));
4494 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4495 EXPECT ("w1::w2"));
4496 }
4497
4498 /* Same, with a "complicated" symbol. */
4499 {
4500 static const char str[] = Z_SYM_NAME;
4501 size_t len = strlen (str);
4502 std::string lookup;
4503
4504 for (size_t i = 1; i < len; i++)
4505 {
4506 lookup.assign (str, i);
4507 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4508 EXPECT (Z_SYM_NAME));
4509 }
4510 }
4511
4512 /* In FULL mode, an incomplete symbol doesn't match. */
4513 {
4514 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4515 {});
4516 }
4517
4518 /* A complete symbol with parameters matches any overload, since the
4519 index has no overload info. */
4520 {
4521 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4522 EXPECT ("std::zfunction", "std::zfunction2"));
4523 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4524 EXPECT ("std::zfunction", "std::zfunction2"));
4525 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4526 EXPECT ("std::zfunction", "std::zfunction2"));
4527 }
4528
4529 /* Check that whitespace is ignored appropriately. A symbol with a
4530 template argument list. */
4531 {
4532 static const char expected[] = "ns::foo<int>";
4533 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4534 EXPECT (expected));
4535 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4536 EXPECT (expected));
4537 }
4538
4539 /* Check that whitespace is ignored appropriately. A symbol with a
4540 template argument list that includes a pointer. */
4541 {
4542 static const char expected[] = "ns::foo<char*>";
4543 /* Try both completion and non-completion modes. */
4544 static const bool completion_mode[2] = {false, true};
4545 for (size_t i = 0; i < 2; i++)
4546 {
4547 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4548 completion_mode[i], EXPECT (expected));
4549 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4550 completion_mode[i], EXPECT (expected));
4551
4552 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4553 completion_mode[i], EXPECT (expected));
4554 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4555 completion_mode[i], EXPECT (expected));
4556 }
4557 }
4558
4559 {
4560 /* Check method qualifiers are ignored. */
4561 static const char expected[] = "ns::foo<char*>";
4562 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4563 symbol_name_match_type::FULL, true, EXPECT (expected));
4564 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4565 symbol_name_match_type::FULL, true, EXPECT (expected));
4566 CHECK_MATCH ("foo < char * > ( int ) const",
4567 symbol_name_match_type::WILD, true, EXPECT (expected));
4568 CHECK_MATCH ("foo < char * > ( int ) &&",
4569 symbol_name_match_type::WILD, true, EXPECT (expected));
4570 }
4571
4572 /* Test lookup names that don't match anything. */
4573 {
4574 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4575 {});
4576
4577 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4578 {});
4579 }
4580
4581 /* Some wild matching tests, exercising "(anonymous namespace)",
4582 which should not be confused with a parameter list. */
4583 {
4584 static const char *syms[] = {
4585 "A::B::C",
4586 "B::C",
4587 "C",
4588 "A :: B :: C ( int )",
4589 "B :: C ( int )",
4590 "C ( int )",
4591 };
4592
4593 for (const char *s : syms)
4594 {
4595 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4596 EXPECT ("(anonymous namespace)::A::B::C"));
4597 }
4598 }
4599
4600 {
4601 static const char expected[] = "ns2::tmpl<int>::foo2";
4602 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4603 EXPECT (expected));
4604 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4605 EXPECT (expected));
4606 }
4607
4608 SELF_CHECK (!any_mismatch);
4609
4610 #undef EXPECT
4611 #undef CHECK_MATCH
4612 }
4613
4614 static void
4615 run_test ()
4616 {
4617 test_mapped_index_find_name_component_bounds ();
4618 test_dw2_expand_symtabs_matching_symbol ();
4619 }
4620
4621 }} // namespace selftests::dw2_expand_symtabs_matching
4622
4623 #endif /* GDB_SELF_TEST */
4624
4625 /* If FILE_MATCHER is NULL or if PER_CU has
4626 dwarf2_per_cu_quick_data::MARK set (see
4627 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4628 EXPANSION_NOTIFY on it. */
4629
4630 static void
4631 dw2_expand_symtabs_matching_one
4632 (dwarf2_per_cu_data *per_cu,
4633 dwarf2_per_objfile *per_objfile,
4634 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4635 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4636 {
4637 if (file_matcher == NULL || per_cu->v.quick->mark)
4638 {
4639 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4640
4641 compunit_symtab *symtab
4642 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4643 gdb_assert (symtab != nullptr);
4644
4645 if (expansion_notify != NULL && symtab_was_null)
4646 expansion_notify (symtab);
4647 }
4648 }
4649
4650 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4651 matched, to expand corresponding CUs that were marked. IDX is the
4652 index of the symbol name that matched. */
4653
4654 static void
4655 dw2_expand_marked_cus
4656 (dwarf2_per_objfile *per_objfile, offset_type idx,
4657 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4658 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4659 search_domain kind)
4660 {
4661 offset_type *vec, vec_len, vec_idx;
4662 bool global_seen = false;
4663 mapped_index &index = *per_objfile->per_bfd->index_table;
4664
4665 vec = (offset_type *) (index.constant_pool
4666 + MAYBE_SWAP (index.symbol_table[idx].vec));
4667 vec_len = MAYBE_SWAP (vec[0]);
4668 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4669 {
4670 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4671 /* This value is only valid for index versions >= 7. */
4672 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4673 gdb_index_symbol_kind symbol_kind =
4674 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4675 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4676 /* Only check the symbol attributes if they're present.
4677 Indices prior to version 7 don't record them,
4678 and indices >= 7 may elide them for certain symbols
4679 (gold does this). */
4680 int attrs_valid =
4681 (index.version >= 7
4682 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4683
4684 /* Work around gold/15646. */
4685 if (attrs_valid
4686 && !is_static
4687 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4688 {
4689 if (global_seen)
4690 continue;
4691
4692 global_seen = true;
4693 }
4694
4695 /* Only check the symbol's kind if it has one. */
4696 if (attrs_valid)
4697 {
4698 switch (kind)
4699 {
4700 case VARIABLES_DOMAIN:
4701 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4702 continue;
4703 break;
4704 case FUNCTIONS_DOMAIN:
4705 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4706 continue;
4707 break;
4708 case TYPES_DOMAIN:
4709 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4710 continue;
4711 break;
4712 case MODULES_DOMAIN:
4713 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4714 continue;
4715 break;
4716 default:
4717 break;
4718 }
4719 }
4720
4721 /* Don't crash on bad data. */
4722 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4723 + per_objfile->per_bfd->all_type_units.size ()))
4724 {
4725 complaint (_(".gdb_index entry has bad CU index"
4726 " [in module %s]"), objfile_name (per_objfile->objfile));
4727 continue;
4728 }
4729
4730 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4731 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4732 expansion_notify);
4733 }
4734 }
4735
4736 /* If FILE_MATCHER is non-NULL, set all the
4737 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4738 that match FILE_MATCHER. */
4739
4740 static void
4741 dw_expand_symtabs_matching_file_matcher
4742 (dwarf2_per_objfile *per_objfile,
4743 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4744 {
4745 if (file_matcher == NULL)
4746 return;
4747
4748 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4749 htab_eq_pointer,
4750 NULL, xcalloc, xfree));
4751 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4752 htab_eq_pointer,
4753 NULL, xcalloc, xfree));
4754
4755 /* The rule is CUs specify all the files, including those used by
4756 any TU, so there's no need to scan TUs here. */
4757
4758 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4759 {
4760 QUIT;
4761
4762 per_cu->v.quick->mark = 0;
4763
4764 /* We only need to look at symtabs not already expanded. */
4765 if (per_objfile->symtab_set_p (per_cu))
4766 continue;
4767
4768 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4769 if (file_data == NULL)
4770 continue;
4771
4772 if (htab_find (visited_not_found.get (), file_data) != NULL)
4773 continue;
4774 else if (htab_find (visited_found.get (), file_data) != NULL)
4775 {
4776 per_cu->v.quick->mark = 1;
4777 continue;
4778 }
4779
4780 for (int j = 0; j < file_data->num_file_names; ++j)
4781 {
4782 const char *this_real_name;
4783
4784 if (file_matcher (file_data->file_names[j], false))
4785 {
4786 per_cu->v.quick->mark = 1;
4787 break;
4788 }
4789
4790 /* Before we invoke realpath, which can get expensive when many
4791 files are involved, do a quick comparison of the basenames. */
4792 if (!basenames_may_differ
4793 && !file_matcher (lbasename (file_data->file_names[j]),
4794 true))
4795 continue;
4796
4797 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4798 if (file_matcher (this_real_name, false))
4799 {
4800 per_cu->v.quick->mark = 1;
4801 break;
4802 }
4803 }
4804
4805 void **slot = htab_find_slot (per_cu->v.quick->mark
4806 ? visited_found.get ()
4807 : visited_not_found.get (),
4808 file_data, INSERT);
4809 *slot = file_data;
4810 }
4811 }
4812
4813 static void
4814 dw2_expand_symtabs_matching
4815 (struct objfile *objfile,
4816 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4817 const lookup_name_info *lookup_name,
4818 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4819 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4820 enum search_domain kind)
4821 {
4822 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4823
4824 /* index_table is NULL if OBJF_READNOW. */
4825 if (!per_objfile->per_bfd->index_table)
4826 return;
4827
4828 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4829
4830 if (symbol_matcher == NULL && lookup_name == NULL)
4831 {
4832 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4833 {
4834 QUIT;
4835
4836 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4837 file_matcher, expansion_notify);
4838 }
4839 return;
4840 }
4841
4842 mapped_index &index = *per_objfile->per_bfd->index_table;
4843
4844 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4845 symbol_matcher,
4846 kind, [&] (offset_type idx)
4847 {
4848 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4849 kind);
4850 return true;
4851 }, per_objfile);
4852 }
4853
4854 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4855 symtab. */
4856
4857 static struct compunit_symtab *
4858 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4859 CORE_ADDR pc)
4860 {
4861 int i;
4862
4863 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4864 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4865 return cust;
4866
4867 if (cust->includes == NULL)
4868 return NULL;
4869
4870 for (i = 0; cust->includes[i]; ++i)
4871 {
4872 struct compunit_symtab *s = cust->includes[i];
4873
4874 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4875 if (s != NULL)
4876 return s;
4877 }
4878
4879 return NULL;
4880 }
4881
4882 static struct compunit_symtab *
4883 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4884 struct bound_minimal_symbol msymbol,
4885 CORE_ADDR pc,
4886 struct obj_section *section,
4887 int warn_if_readin)
4888 {
4889 struct dwarf2_per_cu_data *data;
4890 struct compunit_symtab *result;
4891
4892 if (!objfile->partial_symtabs->psymtabs_addrmap)
4893 return NULL;
4894
4895 CORE_ADDR baseaddr = objfile->text_section_offset ();
4896 data = (struct dwarf2_per_cu_data *) addrmap_find
4897 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4898 if (!data)
4899 return NULL;
4900
4901 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4902 if (warn_if_readin && per_objfile->symtab_set_p (data))
4903 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4904 paddress (objfile->arch (), pc));
4905
4906 result = recursively_find_pc_sect_compunit_symtab
4907 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4908
4909 gdb_assert (result != NULL);
4910 return result;
4911 }
4912
4913 static void
4914 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4915 void *data, int need_fullname)
4916 {
4917 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4918
4919 if (!per_objfile->per_bfd->filenames_cache)
4920 {
4921 per_objfile->per_bfd->filenames_cache.emplace ();
4922
4923 htab_up visited (htab_create_alloc (10,
4924 htab_hash_pointer, htab_eq_pointer,
4925 NULL, xcalloc, xfree));
4926
4927 /* The rule is CUs specify all the files, including those used
4928 by any TU, so there's no need to scan TUs here. We can
4929 ignore file names coming from already-expanded CUs. */
4930
4931 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4932 {
4933 if (per_objfile->symtab_set_p (per_cu))
4934 {
4935 void **slot = htab_find_slot (visited.get (),
4936 per_cu->v.quick->file_names,
4937 INSERT);
4938
4939 *slot = per_cu->v.quick->file_names;
4940 }
4941 }
4942
4943 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4944 {
4945 /* We only need to look at symtabs not already expanded. */
4946 if (per_objfile->symtab_set_p (per_cu))
4947 continue;
4948
4949 quick_file_names *file_data
4950 = dw2_get_file_names (per_cu, per_objfile);
4951 if (file_data == NULL)
4952 continue;
4953
4954 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4955 if (*slot)
4956 {
4957 /* Already visited. */
4958 continue;
4959 }
4960 *slot = file_data;
4961
4962 for (int j = 0; j < file_data->num_file_names; ++j)
4963 {
4964 const char *filename = file_data->file_names[j];
4965 per_objfile->per_bfd->filenames_cache->seen (filename);
4966 }
4967 }
4968 }
4969
4970 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4971 {
4972 gdb::unique_xmalloc_ptr<char> this_real_name;
4973
4974 if (need_fullname)
4975 this_real_name = gdb_realpath (filename);
4976 (*fun) (filename, this_real_name.get (), data);
4977 });
4978 }
4979
4980 static int
4981 dw2_has_symbols (struct objfile *objfile)
4982 {
4983 return 1;
4984 }
4985
4986 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4987 {
4988 dw2_has_symbols,
4989 dw2_find_last_source_symtab,
4990 dw2_forget_cached_source_info,
4991 dw2_map_symtabs_matching_filename,
4992 dw2_lookup_symbol,
4993 NULL,
4994 dw2_print_stats,
4995 dw2_dump,
4996 dw2_expand_symtabs_for_function,
4997 dw2_expand_all_symtabs,
4998 dw2_expand_symtabs_with_fullname,
4999 dw2_map_matching_symbols,
5000 dw2_expand_symtabs_matching,
5001 dw2_find_pc_sect_compunit_symtab,
5002 NULL,
5003 dw2_map_symbol_filenames
5004 };
5005
5006 /* DWARF-5 debug_names reader. */
5007
5008 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5009 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5010
5011 /* A helper function that reads the .debug_names section in SECTION
5012 and fills in MAP. FILENAME is the name of the file containing the
5013 section; it is used for error reporting.
5014
5015 Returns true if all went well, false otherwise. */
5016
5017 static bool
5018 read_debug_names_from_section (struct objfile *objfile,
5019 const char *filename,
5020 struct dwarf2_section_info *section,
5021 mapped_debug_names &map)
5022 {
5023 if (section->empty ())
5024 return false;
5025
5026 /* Older elfutils strip versions could keep the section in the main
5027 executable while splitting it for the separate debug info file. */
5028 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5029 return false;
5030
5031 section->read (objfile);
5032
5033 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
5034
5035 const gdb_byte *addr = section->buffer;
5036
5037 bfd *const abfd = section->get_bfd_owner ();
5038
5039 unsigned int bytes_read;
5040 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5041 addr += bytes_read;
5042
5043 map.dwarf5_is_dwarf64 = bytes_read != 4;
5044 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5045 if (bytes_read + length != section->size)
5046 {
5047 /* There may be multiple per-CU indices. */
5048 warning (_("Section .debug_names in %s length %s does not match "
5049 "section length %s, ignoring .debug_names."),
5050 filename, plongest (bytes_read + length),
5051 pulongest (section->size));
5052 return false;
5053 }
5054
5055 /* The version number. */
5056 uint16_t version = read_2_bytes (abfd, addr);
5057 addr += 2;
5058 if (version != 5)
5059 {
5060 warning (_("Section .debug_names in %s has unsupported version %d, "
5061 "ignoring .debug_names."),
5062 filename, version);
5063 return false;
5064 }
5065
5066 /* Padding. */
5067 uint16_t padding = read_2_bytes (abfd, addr);
5068 addr += 2;
5069 if (padding != 0)
5070 {
5071 warning (_("Section .debug_names in %s has unsupported padding %d, "
5072 "ignoring .debug_names."),
5073 filename, padding);
5074 return false;
5075 }
5076
5077 /* comp_unit_count - The number of CUs in the CU list. */
5078 map.cu_count = read_4_bytes (abfd, addr);
5079 addr += 4;
5080
5081 /* local_type_unit_count - The number of TUs in the local TU
5082 list. */
5083 map.tu_count = read_4_bytes (abfd, addr);
5084 addr += 4;
5085
5086 /* foreign_type_unit_count - The number of TUs in the foreign TU
5087 list. */
5088 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5089 addr += 4;
5090 if (foreign_tu_count != 0)
5091 {
5092 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5093 "ignoring .debug_names."),
5094 filename, static_cast<unsigned long> (foreign_tu_count));
5095 return false;
5096 }
5097
5098 /* bucket_count - The number of hash buckets in the hash lookup
5099 table. */
5100 map.bucket_count = read_4_bytes (abfd, addr);
5101 addr += 4;
5102
5103 /* name_count - The number of unique names in the index. */
5104 map.name_count = read_4_bytes (abfd, addr);
5105 addr += 4;
5106
5107 /* abbrev_table_size - The size in bytes of the abbreviations
5108 table. */
5109 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5110 addr += 4;
5111
5112 /* augmentation_string_size - The size in bytes of the augmentation
5113 string. This value is rounded up to a multiple of 4. */
5114 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5115 addr += 4;
5116 map.augmentation_is_gdb = ((augmentation_string_size
5117 == sizeof (dwarf5_augmentation))
5118 && memcmp (addr, dwarf5_augmentation,
5119 sizeof (dwarf5_augmentation)) == 0);
5120 augmentation_string_size += (-augmentation_string_size) & 3;
5121 addr += augmentation_string_size;
5122
5123 /* List of CUs */
5124 map.cu_table_reordered = addr;
5125 addr += map.cu_count * map.offset_size;
5126
5127 /* List of Local TUs */
5128 map.tu_table_reordered = addr;
5129 addr += map.tu_count * map.offset_size;
5130
5131 /* Hash Lookup Table */
5132 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5133 addr += map.bucket_count * 4;
5134 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5135 addr += map.name_count * 4;
5136
5137 /* Name Table */
5138 map.name_table_string_offs_reordered = addr;
5139 addr += map.name_count * map.offset_size;
5140 map.name_table_entry_offs_reordered = addr;
5141 addr += map.name_count * map.offset_size;
5142
5143 const gdb_byte *abbrev_table_start = addr;
5144 for (;;)
5145 {
5146 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5147 addr += bytes_read;
5148 if (index_num == 0)
5149 break;
5150
5151 const auto insertpair
5152 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5153 if (!insertpair.second)
5154 {
5155 warning (_("Section .debug_names in %s has duplicate index %s, "
5156 "ignoring .debug_names."),
5157 filename, pulongest (index_num));
5158 return false;
5159 }
5160 mapped_debug_names::index_val &indexval = insertpair.first->second;
5161 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5162 addr += bytes_read;
5163
5164 for (;;)
5165 {
5166 mapped_debug_names::index_val::attr attr;
5167 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5168 addr += bytes_read;
5169 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5170 addr += bytes_read;
5171 if (attr.form == DW_FORM_implicit_const)
5172 {
5173 attr.implicit_const = read_signed_leb128 (abfd, addr,
5174 &bytes_read);
5175 addr += bytes_read;
5176 }
5177 if (attr.dw_idx == 0 && attr.form == 0)
5178 break;
5179 indexval.attr_vec.push_back (std::move (attr));
5180 }
5181 }
5182 if (addr != abbrev_table_start + abbrev_table_size)
5183 {
5184 warning (_("Section .debug_names in %s has abbreviation_table "
5185 "of size %s vs. written as %u, ignoring .debug_names."),
5186 filename, plongest (addr - abbrev_table_start),
5187 abbrev_table_size);
5188 return false;
5189 }
5190 map.entry_pool = addr;
5191
5192 return true;
5193 }
5194
5195 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5196 list. */
5197
5198 static void
5199 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
5200 const mapped_debug_names &map,
5201 dwarf2_section_info &section,
5202 bool is_dwz)
5203 {
5204 if (!map.augmentation_is_gdb)
5205 {
5206 for (uint32_t i = 0; i < map.cu_count; ++i)
5207 {
5208 sect_offset sect_off
5209 = (sect_offset) (extract_unsigned_integer
5210 (map.cu_table_reordered + i * map.offset_size,
5211 map.offset_size,
5212 map.dwarf5_byte_order));
5213 /* We don't know the length of the CU, because the CU list in a
5214 .debug_names index can be incomplete, so we can't use the start of
5215 the next CU as end of this CU. We create the CUs here with length 0,
5216 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5217 dwarf2_per_cu_data *per_cu
5218 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5219 per_bfd->all_comp_units.push_back (per_cu);
5220 }
5221 }
5222
5223 sect_offset sect_off_prev;
5224 for (uint32_t i = 0; i <= map.cu_count; ++i)
5225 {
5226 sect_offset sect_off_next;
5227 if (i < map.cu_count)
5228 {
5229 sect_off_next
5230 = (sect_offset) (extract_unsigned_integer
5231 (map.cu_table_reordered + i * map.offset_size,
5232 map.offset_size,
5233 map.dwarf5_byte_order));
5234 }
5235 else
5236 sect_off_next = (sect_offset) section.size;
5237 if (i >= 1)
5238 {
5239 const ULONGEST length = sect_off_next - sect_off_prev;
5240 dwarf2_per_cu_data *per_cu
5241 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5242 sect_off_prev, length);
5243 per_bfd->all_comp_units.push_back (per_cu);
5244 }
5245 sect_off_prev = sect_off_next;
5246 }
5247 }
5248
5249 /* Read the CU list from the mapped index, and use it to create all
5250 the CU objects for this dwarf2_per_objfile. */
5251
5252 static void
5253 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5254 const mapped_debug_names &map,
5255 const mapped_debug_names &dwz_map)
5256 {
5257 gdb_assert (per_bfd->all_comp_units.empty ());
5258 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5259
5260 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5261 false /* is_dwz */);
5262
5263 if (dwz_map.cu_count == 0)
5264 return;
5265
5266 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5267 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5268 true /* is_dwz */);
5269 }
5270
5271 /* Read .debug_names. If everything went ok, initialize the "quick"
5272 elements of all the CUs and return true. Otherwise, return false. */
5273
5274 static bool
5275 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
5276 {
5277 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5278 mapped_debug_names dwz_map;
5279 struct objfile *objfile = per_objfile->objfile;
5280 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5281
5282 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5283 &per_objfile->per_bfd->debug_names, *map))
5284 return false;
5285
5286 /* Don't use the index if it's empty. */
5287 if (map->name_count == 0)
5288 return false;
5289
5290 /* If there is a .dwz file, read it so we can get its CU list as
5291 well. */
5292 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5293 if (dwz != NULL)
5294 {
5295 if (!read_debug_names_from_section (objfile,
5296 bfd_get_filename (dwz->dwz_bfd.get ()),
5297 &dwz->debug_names, dwz_map))
5298 {
5299 warning (_("could not read '.debug_names' section from %s; skipping"),
5300 bfd_get_filename (dwz->dwz_bfd.get ()));
5301 return false;
5302 }
5303 }
5304
5305 create_cus_from_debug_names (per_bfd, *map, dwz_map);
5306
5307 if (map->tu_count != 0)
5308 {
5309 /* We can only handle a single .debug_types when we have an
5310 index. */
5311 if (per_bfd->types.size () != 1)
5312 return false;
5313
5314 dwarf2_section_info *section = &per_bfd->types[0];
5315
5316 create_signatured_type_table_from_debug_names
5317 (per_objfile, *map, section, &per_bfd->abbrev);
5318 }
5319
5320 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
5321
5322 per_bfd->debug_names_table = std::move (map);
5323 per_bfd->using_index = 1;
5324 per_bfd->quick_file_names_table =
5325 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
5326
5327 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
5328 objfiles using the same BFD. */
5329 gdb_assert (per_bfd->partial_symtabs == nullptr);
5330 per_bfd->partial_symtabs = objfile->partial_symtabs;
5331
5332 return true;
5333 }
5334
5335 /* Type used to manage iterating over all CUs looking for a symbol for
5336 .debug_names. */
5337
5338 class dw2_debug_names_iterator
5339 {
5340 public:
5341 dw2_debug_names_iterator (const mapped_debug_names &map,
5342 gdb::optional<block_enum> block_index,
5343 domain_enum domain,
5344 const char *name, dwarf2_per_objfile *per_objfile)
5345 : m_map (map), m_block_index (block_index), m_domain (domain),
5346 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5347 m_per_objfile (per_objfile)
5348 {}
5349
5350 dw2_debug_names_iterator (const mapped_debug_names &map,
5351 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
5352 : m_map (map),
5353 m_search (search),
5354 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5355 m_per_objfile (per_objfile)
5356 {}
5357
5358 dw2_debug_names_iterator (const mapped_debug_names &map,
5359 block_enum block_index, domain_enum domain,
5360 uint32_t namei, dwarf2_per_objfile *per_objfile)
5361 : m_map (map), m_block_index (block_index), m_domain (domain),
5362 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5363 m_per_objfile (per_objfile)
5364 {}
5365
5366 /* Return the next matching CU or NULL if there are no more. */
5367 dwarf2_per_cu_data *next ();
5368
5369 private:
5370 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5371 const char *name,
5372 dwarf2_per_objfile *per_objfile);
5373 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5374 uint32_t namei,
5375 dwarf2_per_objfile *per_objfile);
5376
5377 /* The internalized form of .debug_names. */
5378 const mapped_debug_names &m_map;
5379
5380 /* If set, only look for symbols that match that block. Valid values are
5381 GLOBAL_BLOCK and STATIC_BLOCK. */
5382 const gdb::optional<block_enum> m_block_index;
5383
5384 /* The kind of symbol we're looking for. */
5385 const domain_enum m_domain = UNDEF_DOMAIN;
5386 const search_domain m_search = ALL_DOMAIN;
5387
5388 /* The list of CUs from the index entry of the symbol, or NULL if
5389 not found. */
5390 const gdb_byte *m_addr;
5391
5392 dwarf2_per_objfile *m_per_objfile;
5393 };
5394
5395 const char *
5396 mapped_debug_names::namei_to_name
5397 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5398 {
5399 const ULONGEST namei_string_offs
5400 = extract_unsigned_integer ((name_table_string_offs_reordered
5401 + namei * offset_size),
5402 offset_size,
5403 dwarf5_byte_order);
5404 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5405 }
5406
5407 /* Find a slot in .debug_names for the object named NAME. If NAME is
5408 found, return pointer to its pool data. If NAME cannot be found,
5409 return NULL. */
5410
5411 const gdb_byte *
5412 dw2_debug_names_iterator::find_vec_in_debug_names
5413 (const mapped_debug_names &map, const char *name,
5414 dwarf2_per_objfile *per_objfile)
5415 {
5416 int (*cmp) (const char *, const char *);
5417
5418 gdb::unique_xmalloc_ptr<char> without_params;
5419 if (current_language->la_language == language_cplus
5420 || current_language->la_language == language_fortran
5421 || current_language->la_language == language_d)
5422 {
5423 /* NAME is already canonical. Drop any qualifiers as
5424 .debug_names does not contain any. */
5425
5426 if (strchr (name, '(') != NULL)
5427 {
5428 without_params = cp_remove_params (name);
5429 if (without_params != NULL)
5430 name = without_params.get ();
5431 }
5432 }
5433
5434 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5435
5436 const uint32_t full_hash = dwarf5_djb_hash (name);
5437 uint32_t namei
5438 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5439 (map.bucket_table_reordered
5440 + (full_hash % map.bucket_count)), 4,
5441 map.dwarf5_byte_order);
5442 if (namei == 0)
5443 return NULL;
5444 --namei;
5445 if (namei >= map.name_count)
5446 {
5447 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5448 "[in module %s]"),
5449 namei, map.name_count,
5450 objfile_name (per_objfile->objfile));
5451 return NULL;
5452 }
5453
5454 for (;;)
5455 {
5456 const uint32_t namei_full_hash
5457 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5458 (map.hash_table_reordered + namei), 4,
5459 map.dwarf5_byte_order);
5460 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5461 return NULL;
5462
5463 if (full_hash == namei_full_hash)
5464 {
5465 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5466
5467 #if 0 /* An expensive sanity check. */
5468 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5469 {
5470 complaint (_("Wrong .debug_names hash for string at index %u "
5471 "[in module %s]"),
5472 namei, objfile_name (dwarf2_per_objfile->objfile));
5473 return NULL;
5474 }
5475 #endif
5476
5477 if (cmp (namei_string, name) == 0)
5478 {
5479 const ULONGEST namei_entry_offs
5480 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5481 + namei * map.offset_size),
5482 map.offset_size, map.dwarf5_byte_order);
5483 return map.entry_pool + namei_entry_offs;
5484 }
5485 }
5486
5487 ++namei;
5488 if (namei >= map.name_count)
5489 return NULL;
5490 }
5491 }
5492
5493 const gdb_byte *
5494 dw2_debug_names_iterator::find_vec_in_debug_names
5495 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5496 {
5497 if (namei >= map.name_count)
5498 {
5499 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5500 "[in module %s]"),
5501 namei, map.name_count,
5502 objfile_name (per_objfile->objfile));
5503 return NULL;
5504 }
5505
5506 const ULONGEST namei_entry_offs
5507 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5508 + namei * map.offset_size),
5509 map.offset_size, map.dwarf5_byte_order);
5510 return map.entry_pool + namei_entry_offs;
5511 }
5512
5513 /* See dw2_debug_names_iterator. */
5514
5515 dwarf2_per_cu_data *
5516 dw2_debug_names_iterator::next ()
5517 {
5518 if (m_addr == NULL)
5519 return NULL;
5520
5521 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5522 struct objfile *objfile = m_per_objfile->objfile;
5523 bfd *const abfd = objfile->obfd;
5524
5525 again:
5526
5527 unsigned int bytes_read;
5528 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5529 m_addr += bytes_read;
5530 if (abbrev == 0)
5531 return NULL;
5532
5533 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5534 if (indexval_it == m_map.abbrev_map.cend ())
5535 {
5536 complaint (_("Wrong .debug_names undefined abbrev code %s "
5537 "[in module %s]"),
5538 pulongest (abbrev), objfile_name (objfile));
5539 return NULL;
5540 }
5541 const mapped_debug_names::index_val &indexval = indexval_it->second;
5542 enum class symbol_linkage {
5543 unknown,
5544 static_,
5545 extern_,
5546 } symbol_linkage_ = symbol_linkage::unknown;
5547 dwarf2_per_cu_data *per_cu = NULL;
5548 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5549 {
5550 ULONGEST ull;
5551 switch (attr.form)
5552 {
5553 case DW_FORM_implicit_const:
5554 ull = attr.implicit_const;
5555 break;
5556 case DW_FORM_flag_present:
5557 ull = 1;
5558 break;
5559 case DW_FORM_udata:
5560 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5561 m_addr += bytes_read;
5562 break;
5563 case DW_FORM_ref4:
5564 ull = read_4_bytes (abfd, m_addr);
5565 m_addr += 4;
5566 break;
5567 case DW_FORM_ref8:
5568 ull = read_8_bytes (abfd, m_addr);
5569 m_addr += 8;
5570 break;
5571 case DW_FORM_ref_sig8:
5572 ull = read_8_bytes (abfd, m_addr);
5573 m_addr += 8;
5574 break;
5575 default:
5576 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5577 dwarf_form_name (attr.form),
5578 objfile_name (objfile));
5579 return NULL;
5580 }
5581 switch (attr.dw_idx)
5582 {
5583 case DW_IDX_compile_unit:
5584 /* Don't crash on bad data. */
5585 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
5586 {
5587 complaint (_(".debug_names entry has bad CU index %s"
5588 " [in module %s]"),
5589 pulongest (ull),
5590 objfile_name (objfile));
5591 continue;
5592 }
5593 per_cu = per_bfd->get_cutu (ull);
5594 break;
5595 case DW_IDX_type_unit:
5596 /* Don't crash on bad data. */
5597 if (ull >= per_bfd->all_type_units.size ())
5598 {
5599 complaint (_(".debug_names entry has bad TU index %s"
5600 " [in module %s]"),
5601 pulongest (ull),
5602 objfile_name (objfile));
5603 continue;
5604 }
5605 per_cu = &per_bfd->get_tu (ull)->per_cu;
5606 break;
5607 case DW_IDX_die_offset:
5608 /* In a per-CU index (as opposed to a per-module index), index
5609 entries without CU attribute implicitly refer to the single CU. */
5610 if (per_cu == NULL)
5611 per_cu = per_bfd->get_cu (0);
5612 break;
5613 case DW_IDX_GNU_internal:
5614 if (!m_map.augmentation_is_gdb)
5615 break;
5616 symbol_linkage_ = symbol_linkage::static_;
5617 break;
5618 case DW_IDX_GNU_external:
5619 if (!m_map.augmentation_is_gdb)
5620 break;
5621 symbol_linkage_ = symbol_linkage::extern_;
5622 break;
5623 }
5624 }
5625
5626 /* Skip if already read in. */
5627 if (m_per_objfile->symtab_set_p (per_cu))
5628 goto again;
5629
5630 /* Check static vs global. */
5631 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5632 {
5633 const bool want_static = *m_block_index == STATIC_BLOCK;
5634 const bool symbol_is_static =
5635 symbol_linkage_ == symbol_linkage::static_;
5636 if (want_static != symbol_is_static)
5637 goto again;
5638 }
5639
5640 /* Match dw2_symtab_iter_next, symbol_kind
5641 and debug_names::psymbol_tag. */
5642 switch (m_domain)
5643 {
5644 case VAR_DOMAIN:
5645 switch (indexval.dwarf_tag)
5646 {
5647 case DW_TAG_variable:
5648 case DW_TAG_subprogram:
5649 /* Some types are also in VAR_DOMAIN. */
5650 case DW_TAG_typedef:
5651 case DW_TAG_structure_type:
5652 break;
5653 default:
5654 goto again;
5655 }
5656 break;
5657 case STRUCT_DOMAIN:
5658 switch (indexval.dwarf_tag)
5659 {
5660 case DW_TAG_typedef:
5661 case DW_TAG_structure_type:
5662 break;
5663 default:
5664 goto again;
5665 }
5666 break;
5667 case LABEL_DOMAIN:
5668 switch (indexval.dwarf_tag)
5669 {
5670 case 0:
5671 case DW_TAG_variable:
5672 break;
5673 default:
5674 goto again;
5675 }
5676 break;
5677 case MODULE_DOMAIN:
5678 switch (indexval.dwarf_tag)
5679 {
5680 case DW_TAG_module:
5681 break;
5682 default:
5683 goto again;
5684 }
5685 break;
5686 default:
5687 break;
5688 }
5689
5690 /* Match dw2_expand_symtabs_matching, symbol_kind and
5691 debug_names::psymbol_tag. */
5692 switch (m_search)
5693 {
5694 case VARIABLES_DOMAIN:
5695 switch (indexval.dwarf_tag)
5696 {
5697 case DW_TAG_variable:
5698 break;
5699 default:
5700 goto again;
5701 }
5702 break;
5703 case FUNCTIONS_DOMAIN:
5704 switch (indexval.dwarf_tag)
5705 {
5706 case DW_TAG_subprogram:
5707 break;
5708 default:
5709 goto again;
5710 }
5711 break;
5712 case TYPES_DOMAIN:
5713 switch (indexval.dwarf_tag)
5714 {
5715 case DW_TAG_typedef:
5716 case DW_TAG_structure_type:
5717 break;
5718 default:
5719 goto again;
5720 }
5721 break;
5722 case MODULES_DOMAIN:
5723 switch (indexval.dwarf_tag)
5724 {
5725 case DW_TAG_module:
5726 break;
5727 default:
5728 goto again;
5729 }
5730 default:
5731 break;
5732 }
5733
5734 return per_cu;
5735 }
5736
5737 static struct compunit_symtab *
5738 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5739 const char *name, domain_enum domain)
5740 {
5741 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5742
5743 const auto &mapp = per_objfile->per_bfd->debug_names_table;
5744 if (!mapp)
5745 {
5746 /* index is NULL if OBJF_READNOW. */
5747 return NULL;
5748 }
5749 const auto &map = *mapp;
5750
5751 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
5752
5753 struct compunit_symtab *stab_best = NULL;
5754 struct dwarf2_per_cu_data *per_cu;
5755 while ((per_cu = iter.next ()) != NULL)
5756 {
5757 struct symbol *sym, *with_opaque = NULL;
5758 compunit_symtab *stab
5759 = dw2_instantiate_symtab (per_cu, per_objfile, false);
5760 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5761 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5762
5763 sym = block_find_symbol (block, name, domain,
5764 block_find_non_opaque_type_preferred,
5765 &with_opaque);
5766
5767 /* Some caution must be observed with overloaded functions and
5768 methods, since the index will not contain any overload
5769 information (but NAME might contain it). */
5770
5771 if (sym != NULL
5772 && strcmp_iw (sym->search_name (), name) == 0)
5773 return stab;
5774 if (with_opaque != NULL
5775 && strcmp_iw (with_opaque->search_name (), name) == 0)
5776 stab_best = stab;
5777
5778 /* Keep looking through other CUs. */
5779 }
5780
5781 return stab_best;
5782 }
5783
5784 /* This dumps minimal information about .debug_names. It is called
5785 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5786 uses this to verify that .debug_names has been loaded. */
5787
5788 static void
5789 dw2_debug_names_dump (struct objfile *objfile)
5790 {
5791 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5792
5793 gdb_assert (per_objfile->per_bfd->using_index);
5794 printf_filtered (".debug_names:");
5795 if (per_objfile->per_bfd->debug_names_table)
5796 printf_filtered (" exists\n");
5797 else
5798 printf_filtered (" faked for \"readnow\"\n");
5799 printf_filtered ("\n");
5800 }
5801
5802 static void
5803 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5804 const char *func_name)
5805 {
5806 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5807
5808 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5809 if (per_objfile->per_bfd->debug_names_table)
5810 {
5811 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5812
5813 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
5814 per_objfile);
5815
5816 struct dwarf2_per_cu_data *per_cu;
5817 while ((per_cu = iter.next ()) != NULL)
5818 dw2_instantiate_symtab (per_cu, per_objfile, false);
5819 }
5820 }
5821
5822 static void
5823 dw2_debug_names_map_matching_symbols
5824 (struct objfile *objfile,
5825 const lookup_name_info &name, domain_enum domain,
5826 int global,
5827 gdb::function_view<symbol_found_callback_ftype> callback,
5828 symbol_compare_ftype *ordered_compare)
5829 {
5830 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5831
5832 /* debug_names_table is NULL if OBJF_READNOW. */
5833 if (!per_objfile->per_bfd->debug_names_table)
5834 return;
5835
5836 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5837 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5838
5839 const char *match_name = name.ada ().lookup_name ().c_str ();
5840 auto matcher = [&] (const char *symname)
5841 {
5842 if (ordered_compare == nullptr)
5843 return true;
5844 return ordered_compare (symname, match_name) == 0;
5845 };
5846
5847 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5848 [&] (offset_type namei)
5849 {
5850 /* The name was matched, now expand corresponding CUs that were
5851 marked. */
5852 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
5853 per_objfile);
5854
5855 struct dwarf2_per_cu_data *per_cu;
5856 while ((per_cu = iter.next ()) != NULL)
5857 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5858 nullptr);
5859 return true;
5860 }, per_objfile);
5861
5862 /* It's a shame we couldn't do this inside the
5863 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5864 that have already been expanded. Instead, this loop matches what
5865 the psymtab code does. */
5866 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5867 {
5868 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
5869 if (symtab != nullptr)
5870 {
5871 const struct block *block
5872 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5873 if (!iterate_over_symbols_terminated (block, name,
5874 domain, callback))
5875 break;
5876 }
5877 }
5878 }
5879
5880 static void
5881 dw2_debug_names_expand_symtabs_matching
5882 (struct objfile *objfile,
5883 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5884 const lookup_name_info *lookup_name,
5885 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5886 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5887 enum search_domain kind)
5888 {
5889 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5890
5891 /* debug_names_table is NULL if OBJF_READNOW. */
5892 if (!per_objfile->per_bfd->debug_names_table)
5893 return;
5894
5895 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5896
5897 if (symbol_matcher == NULL && lookup_name == NULL)
5898 {
5899 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5900 {
5901 QUIT;
5902
5903 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5904 expansion_notify);
5905 }
5906 return;
5907 }
5908
5909 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5910
5911 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5912 symbol_matcher,
5913 kind, [&] (offset_type namei)
5914 {
5915 /* The name was matched, now expand corresponding CUs that were
5916 marked. */
5917 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
5918
5919 struct dwarf2_per_cu_data *per_cu;
5920 while ((per_cu = iter.next ()) != NULL)
5921 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5922 expansion_notify);
5923 return true;
5924 }, per_objfile);
5925 }
5926
5927 const struct quick_symbol_functions dwarf2_debug_names_functions =
5928 {
5929 dw2_has_symbols,
5930 dw2_find_last_source_symtab,
5931 dw2_forget_cached_source_info,
5932 dw2_map_symtabs_matching_filename,
5933 dw2_debug_names_lookup_symbol,
5934 NULL,
5935 dw2_print_stats,
5936 dw2_debug_names_dump,
5937 dw2_debug_names_expand_symtabs_for_function,
5938 dw2_expand_all_symtabs,
5939 dw2_expand_symtabs_with_fullname,
5940 dw2_debug_names_map_matching_symbols,
5941 dw2_debug_names_expand_symtabs_matching,
5942 dw2_find_pc_sect_compunit_symtab,
5943 NULL,
5944 dw2_map_symbol_filenames
5945 };
5946
5947 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5948 to either a dwarf2_per_bfd or dwz_file object. */
5949
5950 template <typename T>
5951 static gdb::array_view<const gdb_byte>
5952 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5953 {
5954 dwarf2_section_info *section = &section_owner->gdb_index;
5955
5956 if (section->empty ())
5957 return {};
5958
5959 /* Older elfutils strip versions could keep the section in the main
5960 executable while splitting it for the separate debug info file. */
5961 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5962 return {};
5963
5964 section->read (obj);
5965
5966 /* dwarf2_section_info::size is a bfd_size_type, while
5967 gdb::array_view works with size_t. On 32-bit hosts, with
5968 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5969 is 32-bit. So we need an explicit narrowing conversion here.
5970 This is fine, because it's impossible to allocate or mmap an
5971 array/buffer larger than what size_t can represent. */
5972 return gdb::make_array_view (section->buffer, section->size);
5973 }
5974
5975 /* Lookup the index cache for the contents of the index associated to
5976 DWARF2_OBJ. */
5977
5978 static gdb::array_view<const gdb_byte>
5979 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5980 {
5981 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5982 if (build_id == nullptr)
5983 return {};
5984
5985 return global_index_cache.lookup_gdb_index (build_id,
5986 &dwarf2_per_bfd->index_cache_res);
5987 }
5988
5989 /* Same as the above, but for DWZ. */
5990
5991 static gdb::array_view<const gdb_byte>
5992 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5993 {
5994 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5995 if (build_id == nullptr)
5996 return {};
5997
5998 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5999 }
6000
6001 /* See symfile.h. */
6002
6003 bool
6004 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6005 {
6006 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6007 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6008
6009 /* If we're about to read full symbols, don't bother with the
6010 indices. In this case we also don't care if some other debug
6011 format is making psymtabs, because they are all about to be
6012 expanded anyway. */
6013 if ((objfile->flags & OBJF_READNOW))
6014 {
6015 /* When using READNOW, the using_index flag (set below) indicates that
6016 PER_BFD was already initialized, when we loaded some other objfile. */
6017 if (per_bfd->using_index)
6018 {
6019 *index_kind = dw_index_kind::GDB_INDEX;
6020 per_objfile->resize_symtabs ();
6021 return true;
6022 }
6023
6024 per_bfd->using_index = 1;
6025 create_all_comp_units (per_objfile);
6026 create_all_type_units (per_objfile);
6027 per_bfd->quick_file_names_table
6028 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
6029 per_objfile->resize_symtabs ();
6030
6031 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6032 + per_bfd->all_type_units.size ()); ++i)
6033 {
6034 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
6035
6036 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6037 struct dwarf2_per_cu_quick_data);
6038 }
6039
6040 /* Return 1 so that gdb sees the "quick" functions. However,
6041 these functions will be no-ops because we will have expanded
6042 all symtabs. */
6043 *index_kind = dw_index_kind::GDB_INDEX;
6044 return true;
6045 }
6046
6047 /* Was a debug names index already read when we processed an objfile sharing
6048 PER_BFD? */
6049 if (per_bfd->debug_names_table != nullptr)
6050 {
6051 *index_kind = dw_index_kind::DEBUG_NAMES;
6052 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
6053 per_objfile->resize_symtabs ();
6054 return true;
6055 }
6056
6057 /* Was a GDB index already read when we processed an objfile sharing
6058 PER_BFD? */
6059 if (per_bfd->index_table != nullptr)
6060 {
6061 *index_kind = dw_index_kind::GDB_INDEX;
6062 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
6063 per_objfile->resize_symtabs ();
6064 return true;
6065 }
6066
6067 /* There might already be partial symtabs built for this BFD. This happens
6068 when loading the same binary twice with the index-cache enabled. If so,
6069 don't try to read an index. The objfile / per_objfile initialization will
6070 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6071 code path. */
6072 if (per_bfd->partial_symtabs != nullptr)
6073 return false;
6074
6075 if (dwarf2_read_debug_names (per_objfile))
6076 {
6077 *index_kind = dw_index_kind::DEBUG_NAMES;
6078 per_objfile->resize_symtabs ();
6079 return true;
6080 }
6081
6082 if (dwarf2_read_gdb_index (per_objfile,
6083 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
6084 get_gdb_index_contents_from_section<dwz_file>))
6085 {
6086 *index_kind = dw_index_kind::GDB_INDEX;
6087 per_objfile->resize_symtabs ();
6088 return true;
6089 }
6090
6091 /* ... otherwise, try to find the index in the index cache. */
6092 if (dwarf2_read_gdb_index (per_objfile,
6093 get_gdb_index_contents_from_cache,
6094 get_gdb_index_contents_from_cache_dwz))
6095 {
6096 global_index_cache.hit ();
6097 *index_kind = dw_index_kind::GDB_INDEX;
6098 per_objfile->resize_symtabs ();
6099 return true;
6100 }
6101
6102 global_index_cache.miss ();
6103 return false;
6104 }
6105
6106 \f
6107
6108 /* Build a partial symbol table. */
6109
6110 void
6111 dwarf2_build_psymtabs (struct objfile *objfile)
6112 {
6113 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6114 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6115
6116 if (per_bfd->partial_symtabs != nullptr)
6117 {
6118 /* Partial symbols were already read, so now we can simply
6119 attach them. */
6120 objfile->partial_symtabs = per_bfd->partial_symtabs;
6121 per_objfile->resize_symtabs ();
6122 return;
6123 }
6124
6125 try
6126 {
6127 /* This isn't really ideal: all the data we allocate on the
6128 objfile's obstack is still uselessly kept around. However,
6129 freeing it seems unsafe. */
6130 psymtab_discarder psymtabs (objfile);
6131 dwarf2_build_psymtabs_hard (per_objfile);
6132 psymtabs.keep ();
6133
6134 per_objfile->resize_symtabs ();
6135
6136 /* (maybe) store an index in the cache. */
6137 global_index_cache.store (per_objfile);
6138 }
6139 catch (const gdb_exception_error &except)
6140 {
6141 exception_print (gdb_stderr, except);
6142 }
6143
6144 /* Finish by setting the local reference to partial symtabs, so that
6145 we don't try to read them again if reading another objfile with the same
6146 BFD. If we can't in fact share, this won't make a difference anyway as
6147 the dwarf2_per_bfd object won't be shared. */
6148 per_bfd->partial_symtabs = objfile->partial_symtabs;
6149 }
6150
6151 /* Find the base address of the compilation unit for range lists and
6152 location lists. It will normally be specified by DW_AT_low_pc.
6153 In DWARF-3 draft 4, the base address could be overridden by
6154 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6155 compilation units with discontinuous ranges. */
6156
6157 static void
6158 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6159 {
6160 struct attribute *attr;
6161
6162 cu->base_address.reset ();
6163
6164 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6165 if (attr != nullptr)
6166 cu->base_address = attr->as_address ();
6167 else
6168 {
6169 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6170 if (attr != nullptr)
6171 cu->base_address = attr->as_address ();
6172 }
6173 }
6174
6175 /* Helper function that returns the proper abbrev section for
6176 THIS_CU. */
6177
6178 static struct dwarf2_section_info *
6179 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6180 {
6181 struct dwarf2_section_info *abbrev;
6182 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6183
6184 if (this_cu->is_dwz)
6185 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6186 else
6187 abbrev = &per_bfd->abbrev;
6188
6189 return abbrev;
6190 }
6191
6192 /* Fetch the abbreviation table offset from a comp or type unit header. */
6193
6194 static sect_offset
6195 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
6196 struct dwarf2_section_info *section,
6197 sect_offset sect_off)
6198 {
6199 bfd *abfd = section->get_bfd_owner ();
6200 const gdb_byte *info_ptr;
6201 unsigned int initial_length_size, offset_size;
6202 uint16_t version;
6203
6204 section->read (per_objfile->objfile);
6205 info_ptr = section->buffer + to_underlying (sect_off);
6206 read_initial_length (abfd, info_ptr, &initial_length_size);
6207 offset_size = initial_length_size == 4 ? 4 : 8;
6208 info_ptr += initial_length_size;
6209
6210 version = read_2_bytes (abfd, info_ptr);
6211 info_ptr += 2;
6212 if (version >= 5)
6213 {
6214 /* Skip unit type and address size. */
6215 info_ptr += 2;
6216 }
6217
6218 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6219 }
6220
6221 /* A partial symtab that is used only for include files. */
6222 struct dwarf2_include_psymtab : public partial_symtab
6223 {
6224 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6225 : partial_symtab (filename, objfile)
6226 {
6227 }
6228
6229 void read_symtab (struct objfile *objfile) override
6230 {
6231 /* It's an include file, no symbols to read for it.
6232 Everything is in the includer symtab. */
6233
6234 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6235 expansion of the includer psymtab. We use the dependencies[0] field to
6236 model the includer. But if we go the regular route of calling
6237 expand_psymtab here, and having expand_psymtab call expand_dependencies
6238 to expand the includer, we'll only use expand_psymtab on the includer
6239 (making it a non-toplevel psymtab), while if we expand the includer via
6240 another path, we'll use read_symtab (making it a toplevel psymtab).
6241 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6242 psymtab, and trigger read_symtab on the includer here directly. */
6243 includer ()->read_symtab (objfile);
6244 }
6245
6246 void expand_psymtab (struct objfile *objfile) override
6247 {
6248 /* This is not called by read_symtab, and should not be called by any
6249 expand_dependencies. */
6250 gdb_assert (false);
6251 }
6252
6253 bool readin_p (struct objfile *objfile) const override
6254 {
6255 return includer ()->readin_p (objfile);
6256 }
6257
6258 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6259 {
6260 return nullptr;
6261 }
6262
6263 private:
6264 partial_symtab *includer () const
6265 {
6266 /* An include psymtab has exactly one dependency: the psymtab that
6267 includes it. */
6268 gdb_assert (this->number_of_dependencies == 1);
6269 return this->dependencies[0];
6270 }
6271 };
6272
6273 /* Allocate a new partial symtab for file named NAME and mark this new
6274 partial symtab as being an include of PST. */
6275
6276 static void
6277 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6278 struct objfile *objfile)
6279 {
6280 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6281
6282 if (!IS_ABSOLUTE_PATH (subpst->filename))
6283 subpst->dirname = pst->dirname;
6284
6285 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6286 subpst->dependencies[0] = pst;
6287 subpst->number_of_dependencies = 1;
6288 }
6289
6290 /* Read the Line Number Program data and extract the list of files
6291 included by the source file represented by PST. Build an include
6292 partial symtab for each of these included files. */
6293
6294 static void
6295 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6296 struct die_info *die,
6297 dwarf2_psymtab *pst)
6298 {
6299 line_header_up lh;
6300 struct attribute *attr;
6301
6302 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6303 if (attr != nullptr && attr->form_is_unsigned ())
6304 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
6305 if (lh == NULL)
6306 return; /* No linetable, so no includes. */
6307
6308 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6309 that we pass in the raw text_low here; that is ok because we're
6310 only decoding the line table to make include partial symtabs, and
6311 so the addresses aren't really used. */
6312 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6313 pst->raw_text_low (), 1);
6314 }
6315
6316 static hashval_t
6317 hash_signatured_type (const void *item)
6318 {
6319 const struct signatured_type *sig_type
6320 = (const struct signatured_type *) item;
6321
6322 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6323 return sig_type->signature;
6324 }
6325
6326 static int
6327 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6328 {
6329 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6330 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6331
6332 return lhs->signature == rhs->signature;
6333 }
6334
6335 /* Allocate a hash table for signatured types. */
6336
6337 static htab_up
6338 allocate_signatured_type_table ()
6339 {
6340 return htab_up (htab_create_alloc (41,
6341 hash_signatured_type,
6342 eq_signatured_type,
6343 NULL, xcalloc, xfree));
6344 }
6345
6346 /* A helper function to add a signatured type CU to a table. */
6347
6348 static int
6349 add_signatured_type_cu_to_table (void **slot, void *datum)
6350 {
6351 struct signatured_type *sigt = (struct signatured_type *) *slot;
6352 std::vector<signatured_type *> *all_type_units
6353 = (std::vector<signatured_type *> *) datum;
6354
6355 all_type_units->push_back (sigt);
6356
6357 return 1;
6358 }
6359
6360 /* A helper for create_debug_types_hash_table. Read types from SECTION
6361 and fill them into TYPES_HTAB. It will process only type units,
6362 therefore DW_UT_type. */
6363
6364 static void
6365 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
6366 struct dwo_file *dwo_file,
6367 dwarf2_section_info *section, htab_up &types_htab,
6368 rcuh_kind section_kind)
6369 {
6370 struct objfile *objfile = per_objfile->objfile;
6371 struct dwarf2_section_info *abbrev_section;
6372 bfd *abfd;
6373 const gdb_byte *info_ptr, *end_ptr;
6374
6375 abbrev_section = (dwo_file != NULL
6376 ? &dwo_file->sections.abbrev
6377 : &per_objfile->per_bfd->abbrev);
6378
6379 if (dwarf_read_debug)
6380 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6381 section->get_name (),
6382 abbrev_section->get_file_name ());
6383
6384 section->read (objfile);
6385 info_ptr = section->buffer;
6386
6387 if (info_ptr == NULL)
6388 return;
6389
6390 /* We can't set abfd until now because the section may be empty or
6391 not present, in which case the bfd is unknown. */
6392 abfd = section->get_bfd_owner ();
6393
6394 /* We don't use cutu_reader here because we don't need to read
6395 any dies: the signature is in the header. */
6396
6397 end_ptr = info_ptr + section->size;
6398 while (info_ptr < end_ptr)
6399 {
6400 struct signatured_type *sig_type;
6401 struct dwo_unit *dwo_tu;
6402 void **slot;
6403 const gdb_byte *ptr = info_ptr;
6404 struct comp_unit_head header;
6405 unsigned int length;
6406
6407 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6408
6409 /* Initialize it due to a false compiler warning. */
6410 header.signature = -1;
6411 header.type_cu_offset_in_tu = (cu_offset) -1;
6412
6413 /* We need to read the type's signature in order to build the hash
6414 table, but we don't need anything else just yet. */
6415
6416 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6417 abbrev_section, ptr, section_kind);
6418
6419 length = header.get_length ();
6420
6421 /* Skip dummy type units. */
6422 if (ptr >= info_ptr + length
6423 || peek_abbrev_code (abfd, ptr) == 0
6424 || (header.unit_type != DW_UT_type
6425 && header.unit_type != DW_UT_split_type))
6426 {
6427 info_ptr += length;
6428 continue;
6429 }
6430
6431 if (types_htab == NULL)
6432 {
6433 if (dwo_file)
6434 types_htab = allocate_dwo_unit_table ();
6435 else
6436 types_htab = allocate_signatured_type_table ();
6437 }
6438
6439 if (dwo_file)
6440 {
6441 sig_type = NULL;
6442 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6443 dwo_tu->dwo_file = dwo_file;
6444 dwo_tu->signature = header.signature;
6445 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6446 dwo_tu->section = section;
6447 dwo_tu->sect_off = sect_off;
6448 dwo_tu->length = length;
6449 }
6450 else
6451 {
6452 /* N.B.: type_offset is not usable if this type uses a DWO file.
6453 The real type_offset is in the DWO file. */
6454 dwo_tu = NULL;
6455 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6456 sig_type->signature = header.signature;
6457 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6458 sig_type->per_cu.is_debug_types = 1;
6459 sig_type->per_cu.section = section;
6460 sig_type->per_cu.sect_off = sect_off;
6461 sig_type->per_cu.length = length;
6462 }
6463
6464 slot = htab_find_slot (types_htab.get (),
6465 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6466 INSERT);
6467 gdb_assert (slot != NULL);
6468 if (*slot != NULL)
6469 {
6470 sect_offset dup_sect_off;
6471
6472 if (dwo_file)
6473 {
6474 const struct dwo_unit *dup_tu
6475 = (const struct dwo_unit *) *slot;
6476
6477 dup_sect_off = dup_tu->sect_off;
6478 }
6479 else
6480 {
6481 const struct signatured_type *dup_tu
6482 = (const struct signatured_type *) *slot;
6483
6484 dup_sect_off = dup_tu->per_cu.sect_off;
6485 }
6486
6487 complaint (_("debug type entry at offset %s is duplicate to"
6488 " the entry at offset %s, signature %s"),
6489 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6490 hex_string (header.signature));
6491 }
6492 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6493
6494 if (dwarf_read_debug > 1)
6495 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6496 sect_offset_str (sect_off),
6497 hex_string (header.signature));
6498
6499 info_ptr += length;
6500 }
6501 }
6502
6503 /* Create the hash table of all entries in the .debug_types
6504 (or .debug_types.dwo) section(s).
6505 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6506 otherwise it is NULL.
6507
6508 The result is a pointer to the hash table or NULL if there are no types.
6509
6510 Note: This function processes DWO files only, not DWP files. */
6511
6512 static void
6513 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6514 struct dwo_file *dwo_file,
6515 gdb::array_view<dwarf2_section_info> type_sections,
6516 htab_up &types_htab)
6517 {
6518 for (dwarf2_section_info &section : type_sections)
6519 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6520 rcuh_kind::TYPE);
6521 }
6522
6523 /* Create the hash table of all entries in the .debug_types section,
6524 and initialize all_type_units.
6525 The result is zero if there is an error (e.g. missing .debug_types section),
6526 otherwise non-zero. */
6527
6528 static int
6529 create_all_type_units (dwarf2_per_objfile *per_objfile)
6530 {
6531 htab_up types_htab;
6532
6533 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6534 types_htab, rcuh_kind::COMPILE);
6535 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6536 types_htab);
6537 if (types_htab == NULL)
6538 {
6539 per_objfile->per_bfd->signatured_types = NULL;
6540 return 0;
6541 }
6542
6543 per_objfile->per_bfd->signatured_types = std::move (types_htab);
6544
6545 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6546 per_objfile->per_bfd->all_type_units.reserve
6547 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
6548
6549 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
6550 add_signatured_type_cu_to_table,
6551 &per_objfile->per_bfd->all_type_units);
6552
6553 return 1;
6554 }
6555
6556 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6557 If SLOT is non-NULL, it is the entry to use in the hash table.
6558 Otherwise we find one. */
6559
6560 static struct signatured_type *
6561 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6562 {
6563 if (per_objfile->per_bfd->all_type_units.size ()
6564 == per_objfile->per_bfd->all_type_units.capacity ())
6565 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6566
6567 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6568
6569 per_objfile->resize_symtabs ();
6570
6571 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6572 sig_type->signature = sig;
6573 sig_type->per_cu.is_debug_types = 1;
6574 if (per_objfile->per_bfd->using_index)
6575 {
6576 sig_type->per_cu.v.quick =
6577 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6578 struct dwarf2_per_cu_quick_data);
6579 }
6580
6581 if (slot == NULL)
6582 {
6583 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6584 sig_type, INSERT);
6585 }
6586 gdb_assert (*slot == NULL);
6587 *slot = sig_type;
6588 /* The rest of sig_type must be filled in by the caller. */
6589 return sig_type;
6590 }
6591
6592 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6593 Fill in SIG_ENTRY with DWO_ENTRY. */
6594
6595 static void
6596 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6597 struct signatured_type *sig_entry,
6598 struct dwo_unit *dwo_entry)
6599 {
6600 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6601
6602 /* Make sure we're not clobbering something we don't expect to. */
6603 gdb_assert (! sig_entry->per_cu.queued);
6604 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6605 if (per_bfd->using_index)
6606 {
6607 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6608 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6609 }
6610 else
6611 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6612 gdb_assert (sig_entry->signature == dwo_entry->signature);
6613 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6614 gdb_assert (sig_entry->type_unit_group == NULL);
6615 gdb_assert (sig_entry->dwo_unit == NULL);
6616
6617 sig_entry->per_cu.section = dwo_entry->section;
6618 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6619 sig_entry->per_cu.length = dwo_entry->length;
6620 sig_entry->per_cu.reading_dwo_directly = 1;
6621 sig_entry->per_cu.per_bfd = per_bfd;
6622 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6623 sig_entry->dwo_unit = dwo_entry;
6624 }
6625
6626 /* Subroutine of lookup_signatured_type.
6627 If we haven't read the TU yet, create the signatured_type data structure
6628 for a TU to be read in directly from a DWO file, bypassing the stub.
6629 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6630 using .gdb_index, then when reading a CU we want to stay in the DWO file
6631 containing that CU. Otherwise we could end up reading several other DWO
6632 files (due to comdat folding) to process the transitive closure of all the
6633 mentioned TUs, and that can be slow. The current DWO file will have every
6634 type signature that it needs.
6635 We only do this for .gdb_index because in the psymtab case we already have
6636 to read all the DWOs to build the type unit groups. */
6637
6638 static struct signatured_type *
6639 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6640 {
6641 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6642 struct dwo_file *dwo_file;
6643 struct dwo_unit find_dwo_entry, *dwo_entry;
6644 struct signatured_type find_sig_entry, *sig_entry;
6645 void **slot;
6646
6647 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6648
6649 /* If TU skeletons have been removed then we may not have read in any
6650 TUs yet. */
6651 if (per_objfile->per_bfd->signatured_types == NULL)
6652 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6653
6654 /* We only ever need to read in one copy of a signatured type.
6655 Use the global signatured_types array to do our own comdat-folding
6656 of types. If this is the first time we're reading this TU, and
6657 the TU has an entry in .gdb_index, replace the recorded data from
6658 .gdb_index with this TU. */
6659
6660 find_sig_entry.signature = sig;
6661 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6662 &find_sig_entry, INSERT);
6663 sig_entry = (struct signatured_type *) *slot;
6664
6665 /* We can get here with the TU already read, *or* in the process of being
6666 read. Don't reassign the global entry to point to this DWO if that's
6667 the case. Also note that if the TU is already being read, it may not
6668 have come from a DWO, the program may be a mix of Fission-compiled
6669 code and non-Fission-compiled code. */
6670
6671 /* Have we already tried to read this TU?
6672 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6673 needn't exist in the global table yet). */
6674 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6675 return sig_entry;
6676
6677 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6678 dwo_unit of the TU itself. */
6679 dwo_file = cu->dwo_unit->dwo_file;
6680
6681 /* Ok, this is the first time we're reading this TU. */
6682 if (dwo_file->tus == NULL)
6683 return NULL;
6684 find_dwo_entry.signature = sig;
6685 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6686 &find_dwo_entry);
6687 if (dwo_entry == NULL)
6688 return NULL;
6689
6690 /* If the global table doesn't have an entry for this TU, add one. */
6691 if (sig_entry == NULL)
6692 sig_entry = add_type_unit (per_objfile, sig, slot);
6693
6694 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6695 sig_entry->per_cu.tu_read = 1;
6696 return sig_entry;
6697 }
6698
6699 /* Subroutine of lookup_signatured_type.
6700 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6701 then try the DWP file. If the TU stub (skeleton) has been removed then
6702 it won't be in .gdb_index. */
6703
6704 static struct signatured_type *
6705 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6706 {
6707 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6708 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6709 struct dwo_unit *dwo_entry;
6710 struct signatured_type find_sig_entry, *sig_entry;
6711 void **slot;
6712
6713 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6714 gdb_assert (dwp_file != NULL);
6715
6716 /* If TU skeletons have been removed then we may not have read in any
6717 TUs yet. */
6718 if (per_objfile->per_bfd->signatured_types == NULL)
6719 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6720
6721 find_sig_entry.signature = sig;
6722 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6723 &find_sig_entry, INSERT);
6724 sig_entry = (struct signatured_type *) *slot;
6725
6726 /* Have we already tried to read this TU?
6727 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6728 needn't exist in the global table yet). */
6729 if (sig_entry != NULL)
6730 return sig_entry;
6731
6732 if (dwp_file->tus == NULL)
6733 return NULL;
6734 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6735 1 /* is_debug_types */);
6736 if (dwo_entry == NULL)
6737 return NULL;
6738
6739 sig_entry = add_type_unit (per_objfile, sig, slot);
6740 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6741
6742 return sig_entry;
6743 }
6744
6745 /* Lookup a signature based type for DW_FORM_ref_sig8.
6746 Returns NULL if signature SIG is not present in the table.
6747 It is up to the caller to complain about this. */
6748
6749 static struct signatured_type *
6750 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6751 {
6752 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6753
6754 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6755 {
6756 /* We're in a DWO/DWP file, and we're using .gdb_index.
6757 These cases require special processing. */
6758 if (get_dwp_file (per_objfile) == NULL)
6759 return lookup_dwo_signatured_type (cu, sig);
6760 else
6761 return lookup_dwp_signatured_type (cu, sig);
6762 }
6763 else
6764 {
6765 struct signatured_type find_entry, *entry;
6766
6767 if (per_objfile->per_bfd->signatured_types == NULL)
6768 return NULL;
6769 find_entry.signature = sig;
6770 entry = ((struct signatured_type *)
6771 htab_find (per_objfile->per_bfd->signatured_types.get (),
6772 &find_entry));
6773 return entry;
6774 }
6775 }
6776
6777 /* Low level DIE reading support. */
6778
6779 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6780
6781 static void
6782 init_cu_die_reader (struct die_reader_specs *reader,
6783 struct dwarf2_cu *cu,
6784 struct dwarf2_section_info *section,
6785 struct dwo_file *dwo_file,
6786 struct abbrev_table *abbrev_table)
6787 {
6788 gdb_assert (section->readin && section->buffer != NULL);
6789 reader->abfd = section->get_bfd_owner ();
6790 reader->cu = cu;
6791 reader->dwo_file = dwo_file;
6792 reader->die_section = section;
6793 reader->buffer = section->buffer;
6794 reader->buffer_end = section->buffer + section->size;
6795 reader->abbrev_table = abbrev_table;
6796 }
6797
6798 /* Subroutine of cutu_reader to simplify it.
6799 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6800 There's just a lot of work to do, and cutu_reader is big enough
6801 already.
6802
6803 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6804 from it to the DIE in the DWO. If NULL we are skipping the stub.
6805 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6806 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6807 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6808 STUB_COMP_DIR may be non-NULL.
6809 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6810 are filled in with the info of the DIE from the DWO file.
6811 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6812 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6813 kept around for at least as long as *RESULT_READER.
6814
6815 The result is non-zero if a valid (non-dummy) DIE was found. */
6816
6817 static int
6818 read_cutu_die_from_dwo (dwarf2_cu *cu,
6819 struct dwo_unit *dwo_unit,
6820 struct die_info *stub_comp_unit_die,
6821 const char *stub_comp_dir,
6822 struct die_reader_specs *result_reader,
6823 const gdb_byte **result_info_ptr,
6824 struct die_info **result_comp_unit_die,
6825 abbrev_table_up *result_dwo_abbrev_table)
6826 {
6827 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6828 dwarf2_per_cu_data *per_cu = cu->per_cu;
6829 struct objfile *objfile = per_objfile->objfile;
6830 bfd *abfd;
6831 const gdb_byte *begin_info_ptr, *info_ptr;
6832 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6833 int i,num_extra_attrs;
6834 struct dwarf2_section_info *dwo_abbrev_section;
6835 struct die_info *comp_unit_die;
6836
6837 /* At most one of these may be provided. */
6838 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6839
6840 /* These attributes aren't processed until later:
6841 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6842 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6843 referenced later. However, these attributes are found in the stub
6844 which we won't have later. In order to not impose this complication
6845 on the rest of the code, we read them here and copy them to the
6846 DWO CU/TU die. */
6847
6848 stmt_list = NULL;
6849 low_pc = NULL;
6850 high_pc = NULL;
6851 ranges = NULL;
6852 comp_dir = NULL;
6853
6854 if (stub_comp_unit_die != NULL)
6855 {
6856 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6857 DWO file. */
6858 if (!per_cu->is_debug_types)
6859 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6860 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6861 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6862 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6863 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6864
6865 cu->addr_base = stub_comp_unit_die->addr_base ();
6866
6867 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6868 here (if needed). We need the value before we can process
6869 DW_AT_ranges. */
6870 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6871 }
6872 else if (stub_comp_dir != NULL)
6873 {
6874 /* Reconstruct the comp_dir attribute to simplify the code below. */
6875 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6876 comp_dir->name = DW_AT_comp_dir;
6877 comp_dir->form = DW_FORM_string;
6878 comp_dir->set_string_noncanonical (stub_comp_dir);
6879 }
6880
6881 /* Set up for reading the DWO CU/TU. */
6882 cu->dwo_unit = dwo_unit;
6883 dwarf2_section_info *section = dwo_unit->section;
6884 section->read (objfile);
6885 abfd = section->get_bfd_owner ();
6886 begin_info_ptr = info_ptr = (section->buffer
6887 + to_underlying (dwo_unit->sect_off));
6888 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6889
6890 if (per_cu->is_debug_types)
6891 {
6892 signatured_type *sig_type = (struct signatured_type *) per_cu;
6893
6894 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6895 section, dwo_abbrev_section,
6896 info_ptr, rcuh_kind::TYPE);
6897 /* This is not an assert because it can be caused by bad debug info. */
6898 if (sig_type->signature != cu->header.signature)
6899 {
6900 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6901 " TU at offset %s [in module %s]"),
6902 hex_string (sig_type->signature),
6903 hex_string (cu->header.signature),
6904 sect_offset_str (dwo_unit->sect_off),
6905 bfd_get_filename (abfd));
6906 }
6907 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6908 /* For DWOs coming from DWP files, we don't know the CU length
6909 nor the type's offset in the TU until now. */
6910 dwo_unit->length = cu->header.get_length ();
6911 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6912
6913 /* Establish the type offset that can be used to lookup the type.
6914 For DWO files, we don't know it until now. */
6915 sig_type->type_offset_in_section
6916 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6917 }
6918 else
6919 {
6920 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6921 section, dwo_abbrev_section,
6922 info_ptr, rcuh_kind::COMPILE);
6923 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6924 /* For DWOs coming from DWP files, we don't know the CU length
6925 until now. */
6926 dwo_unit->length = cu->header.get_length ();
6927 }
6928
6929 *result_dwo_abbrev_table
6930 = abbrev_table::read (objfile, dwo_abbrev_section,
6931 cu->header.abbrev_sect_off);
6932 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6933 result_dwo_abbrev_table->get ());
6934
6935 /* Read in the die, but leave space to copy over the attributes
6936 from the stub. This has the benefit of simplifying the rest of
6937 the code - all the work to maintain the illusion of a single
6938 DW_TAG_{compile,type}_unit DIE is done here. */
6939 num_extra_attrs = ((stmt_list != NULL)
6940 + (low_pc != NULL)
6941 + (high_pc != NULL)
6942 + (ranges != NULL)
6943 + (comp_dir != NULL));
6944 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6945 num_extra_attrs);
6946
6947 /* Copy over the attributes from the stub to the DIE we just read in. */
6948 comp_unit_die = *result_comp_unit_die;
6949 i = comp_unit_die->num_attrs;
6950 if (stmt_list != NULL)
6951 comp_unit_die->attrs[i++] = *stmt_list;
6952 if (low_pc != NULL)
6953 comp_unit_die->attrs[i++] = *low_pc;
6954 if (high_pc != NULL)
6955 comp_unit_die->attrs[i++] = *high_pc;
6956 if (ranges != NULL)
6957 comp_unit_die->attrs[i++] = *ranges;
6958 if (comp_dir != NULL)
6959 comp_unit_die->attrs[i++] = *comp_dir;
6960 comp_unit_die->num_attrs += num_extra_attrs;
6961
6962 if (dwarf_die_debug)
6963 {
6964 fprintf_unfiltered (gdb_stdlog,
6965 "Read die from %s@0x%x of %s:\n",
6966 section->get_name (),
6967 (unsigned) (begin_info_ptr - section->buffer),
6968 bfd_get_filename (abfd));
6969 dump_die (comp_unit_die, dwarf_die_debug);
6970 }
6971
6972 /* Skip dummy compilation units. */
6973 if (info_ptr >= begin_info_ptr + dwo_unit->length
6974 || peek_abbrev_code (abfd, info_ptr) == 0)
6975 return 0;
6976
6977 *result_info_ptr = info_ptr;
6978 return 1;
6979 }
6980
6981 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6982 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6983 signature is part of the header. */
6984 static gdb::optional<ULONGEST>
6985 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6986 {
6987 if (cu->header.version >= 5)
6988 return cu->header.signature;
6989 struct attribute *attr;
6990 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6991 if (attr == nullptr || !attr->form_is_unsigned ())
6992 return gdb::optional<ULONGEST> ();
6993 return attr->as_unsigned ();
6994 }
6995
6996 /* Subroutine of cutu_reader to simplify it.
6997 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6998 Returns NULL if the specified DWO unit cannot be found. */
6999
7000 static struct dwo_unit *
7001 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
7002 {
7003 dwarf2_per_cu_data *per_cu = cu->per_cu;
7004 struct dwo_unit *dwo_unit;
7005 const char *comp_dir;
7006
7007 gdb_assert (cu != NULL);
7008
7009 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7010 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7011 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7012
7013 if (per_cu->is_debug_types)
7014 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
7015 else
7016 {
7017 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7018
7019 if (!signature.has_value ())
7020 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7021 " [in module %s]"),
7022 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7023
7024 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
7025 }
7026
7027 return dwo_unit;
7028 }
7029
7030 /* Subroutine of cutu_reader to simplify it.
7031 See it for a description of the parameters.
7032 Read a TU directly from a DWO file, bypassing the stub. */
7033
7034 void
7035 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7036 dwarf2_per_objfile *per_objfile,
7037 dwarf2_cu *existing_cu)
7038 {
7039 struct signatured_type *sig_type;
7040
7041 /* Verify we can do the following downcast, and that we have the
7042 data we need. */
7043 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7044 sig_type = (struct signatured_type *) this_cu;
7045 gdb_assert (sig_type->dwo_unit != NULL);
7046
7047 dwarf2_cu *cu;
7048
7049 if (existing_cu != nullptr)
7050 {
7051 cu = existing_cu;
7052 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
7053 /* There's no need to do the rereading_dwo_cu handling that
7054 cutu_reader does since we don't read the stub. */
7055 }
7056 else
7057 {
7058 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7059 in per_objfile yet. */
7060 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7061 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7062 cu = m_new_cu.get ();
7063 }
7064
7065 /* A future optimization, if needed, would be to use an existing
7066 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7067 could share abbrev tables. */
7068
7069 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
7070 NULL /* stub_comp_unit_die */,
7071 sig_type->dwo_unit->dwo_file->comp_dir,
7072 this, &info_ptr,
7073 &comp_unit_die,
7074 &m_dwo_abbrev_table) == 0)
7075 {
7076 /* Dummy die. */
7077 dummy_p = true;
7078 }
7079 }
7080
7081 /* Initialize a CU (or TU) and read its DIEs.
7082 If the CU defers to a DWO file, read the DWO file as well.
7083
7084 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7085 Otherwise the table specified in the comp unit header is read in and used.
7086 This is an optimization for when we already have the abbrev table.
7087
7088 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7089 allocated. */
7090
7091 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7092 dwarf2_per_objfile *per_objfile,
7093 struct abbrev_table *abbrev_table,
7094 dwarf2_cu *existing_cu,
7095 bool skip_partial)
7096 : die_reader_specs {},
7097 m_this_cu (this_cu)
7098 {
7099 struct objfile *objfile = per_objfile->objfile;
7100 struct dwarf2_section_info *section = this_cu->section;
7101 bfd *abfd = section->get_bfd_owner ();
7102 const gdb_byte *begin_info_ptr;
7103 struct signatured_type *sig_type = NULL;
7104 struct dwarf2_section_info *abbrev_section;
7105 /* Non-zero if CU currently points to a DWO file and we need to
7106 reread it. When this happens we need to reread the skeleton die
7107 before we can reread the DWO file (this only applies to CUs, not TUs). */
7108 int rereading_dwo_cu = 0;
7109
7110 if (dwarf_die_debug)
7111 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7112 this_cu->is_debug_types ? "type" : "comp",
7113 sect_offset_str (this_cu->sect_off));
7114
7115 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7116 file (instead of going through the stub), short-circuit all of this. */
7117 if (this_cu->reading_dwo_directly)
7118 {
7119 /* Narrow down the scope of possibilities to have to understand. */
7120 gdb_assert (this_cu->is_debug_types);
7121 gdb_assert (abbrev_table == NULL);
7122 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
7123 return;
7124 }
7125
7126 /* This is cheap if the section is already read in. */
7127 section->read (objfile);
7128
7129 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7130
7131 abbrev_section = get_abbrev_section_for_cu (this_cu);
7132
7133 dwarf2_cu *cu;
7134
7135 if (existing_cu != nullptr)
7136 {
7137 cu = existing_cu;
7138 /* If this CU is from a DWO file we need to start over, we need to
7139 refetch the attributes from the skeleton CU.
7140 This could be optimized by retrieving those attributes from when we
7141 were here the first time: the previous comp_unit_die was stored in
7142 comp_unit_obstack. But there's no data yet that we need this
7143 optimization. */
7144 if (cu->dwo_unit != NULL)
7145 rereading_dwo_cu = 1;
7146 }
7147 else
7148 {
7149 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7150 in per_objfile yet. */
7151 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7152 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7153 cu = m_new_cu.get ();
7154 }
7155
7156 /* Get the header. */
7157 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7158 {
7159 /* We already have the header, there's no need to read it in again. */
7160 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7161 }
7162 else
7163 {
7164 if (this_cu->is_debug_types)
7165 {
7166 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7167 section, abbrev_section,
7168 info_ptr, rcuh_kind::TYPE);
7169
7170 /* Since per_cu is the first member of struct signatured_type,
7171 we can go from a pointer to one to a pointer to the other. */
7172 sig_type = (struct signatured_type *) this_cu;
7173 gdb_assert (sig_type->signature == cu->header.signature);
7174 gdb_assert (sig_type->type_offset_in_tu
7175 == cu->header.type_cu_offset_in_tu);
7176 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7177
7178 /* LENGTH has not been set yet for type units if we're
7179 using .gdb_index. */
7180 this_cu->length = cu->header.get_length ();
7181
7182 /* Establish the type offset that can be used to lookup the type. */
7183 sig_type->type_offset_in_section =
7184 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7185
7186 this_cu->dwarf_version = cu->header.version;
7187 }
7188 else
7189 {
7190 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7191 section, abbrev_section,
7192 info_ptr,
7193 rcuh_kind::COMPILE);
7194
7195 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7196 if (this_cu->length == 0)
7197 this_cu->length = cu->header.get_length ();
7198 else
7199 gdb_assert (this_cu->length == cu->header.get_length ());
7200 this_cu->dwarf_version = cu->header.version;
7201 }
7202 }
7203
7204 /* Skip dummy compilation units. */
7205 if (info_ptr >= begin_info_ptr + this_cu->length
7206 || peek_abbrev_code (abfd, info_ptr) == 0)
7207 {
7208 dummy_p = true;
7209 return;
7210 }
7211
7212 /* If we don't have them yet, read the abbrevs for this compilation unit.
7213 And if we need to read them now, make sure they're freed when we're
7214 done. */
7215 if (abbrev_table != NULL)
7216 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7217 else
7218 {
7219 m_abbrev_table_holder
7220 = abbrev_table::read (objfile, abbrev_section,
7221 cu->header.abbrev_sect_off);
7222 abbrev_table = m_abbrev_table_holder.get ();
7223 }
7224
7225 /* Read the top level CU/TU die. */
7226 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7227 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7228
7229 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7230 {
7231 dummy_p = true;
7232 return;
7233 }
7234
7235 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7236 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7237 table from the DWO file and pass the ownership over to us. It will be
7238 referenced from READER, so we must make sure to free it after we're done
7239 with READER.
7240
7241 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7242 DWO CU, that this test will fail (the attribute will not be present). */
7243 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7244 if (dwo_name != nullptr)
7245 {
7246 struct dwo_unit *dwo_unit;
7247 struct die_info *dwo_comp_unit_die;
7248
7249 if (comp_unit_die->has_children)
7250 {
7251 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7252 " has children (offset %s) [in module %s]"),
7253 sect_offset_str (this_cu->sect_off),
7254 bfd_get_filename (abfd));
7255 }
7256 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7257 if (dwo_unit != NULL)
7258 {
7259 if (read_cutu_die_from_dwo (cu, dwo_unit,
7260 comp_unit_die, NULL,
7261 this, &info_ptr,
7262 &dwo_comp_unit_die,
7263 &m_dwo_abbrev_table) == 0)
7264 {
7265 /* Dummy die. */
7266 dummy_p = true;
7267 return;
7268 }
7269 comp_unit_die = dwo_comp_unit_die;
7270 }
7271 else
7272 {
7273 /* Yikes, we couldn't find the rest of the DIE, we only have
7274 the stub. A complaint has already been logged. There's
7275 not much more we can do except pass on the stub DIE to
7276 die_reader_func. We don't want to throw an error on bad
7277 debug info. */
7278 }
7279 }
7280 }
7281
7282 void
7283 cutu_reader::keep ()
7284 {
7285 /* Done, clean up. */
7286 gdb_assert (!dummy_p);
7287 if (m_new_cu != NULL)
7288 {
7289 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7290 now. */
7291 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7292 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7293 }
7294 }
7295
7296 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7297 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7298 assumed to have already done the lookup to find the DWO file).
7299
7300 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7301 THIS_CU->is_debug_types, but nothing else.
7302
7303 We fill in THIS_CU->length.
7304
7305 THIS_CU->cu is always freed when done.
7306 This is done in order to not leave THIS_CU->cu in a state where we have
7307 to care whether it refers to the "main" CU or the DWO CU.
7308
7309 When parent_cu is passed, it is used to provide a default value for
7310 str_offsets_base and addr_base from the parent. */
7311
7312 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7313 dwarf2_per_objfile *per_objfile,
7314 struct dwarf2_cu *parent_cu,
7315 struct dwo_file *dwo_file)
7316 : die_reader_specs {},
7317 m_this_cu (this_cu)
7318 {
7319 struct objfile *objfile = per_objfile->objfile;
7320 struct dwarf2_section_info *section = this_cu->section;
7321 bfd *abfd = section->get_bfd_owner ();
7322 struct dwarf2_section_info *abbrev_section;
7323 const gdb_byte *begin_info_ptr, *info_ptr;
7324
7325 if (dwarf_die_debug)
7326 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7327 this_cu->is_debug_types ? "type" : "comp",
7328 sect_offset_str (this_cu->sect_off));
7329
7330 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7331
7332 abbrev_section = (dwo_file != NULL
7333 ? &dwo_file->sections.abbrev
7334 : get_abbrev_section_for_cu (this_cu));
7335
7336 /* This is cheap if the section is already read in. */
7337 section->read (objfile);
7338
7339 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7340
7341 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7342 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7343 section, abbrev_section, info_ptr,
7344 (this_cu->is_debug_types
7345 ? rcuh_kind::TYPE
7346 : rcuh_kind::COMPILE));
7347
7348 if (parent_cu != nullptr)
7349 {
7350 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7351 m_new_cu->addr_base = parent_cu->addr_base;
7352 }
7353 this_cu->length = m_new_cu->header.get_length ();
7354
7355 /* Skip dummy compilation units. */
7356 if (info_ptr >= begin_info_ptr + this_cu->length
7357 || peek_abbrev_code (abfd, info_ptr) == 0)
7358 {
7359 dummy_p = true;
7360 return;
7361 }
7362
7363 m_abbrev_table_holder
7364 = abbrev_table::read (objfile, abbrev_section,
7365 m_new_cu->header.abbrev_sect_off);
7366
7367 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7368 m_abbrev_table_holder.get ());
7369 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7370 }
7371
7372 \f
7373 /* Type Unit Groups.
7374
7375 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7376 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7377 so that all types coming from the same compilation (.o file) are grouped
7378 together. A future step could be to put the types in the same symtab as
7379 the CU the types ultimately came from. */
7380
7381 static hashval_t
7382 hash_type_unit_group (const void *item)
7383 {
7384 const struct type_unit_group *tu_group
7385 = (const struct type_unit_group *) item;
7386
7387 return hash_stmt_list_entry (&tu_group->hash);
7388 }
7389
7390 static int
7391 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7392 {
7393 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7394 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7395
7396 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7397 }
7398
7399 /* Allocate a hash table for type unit groups. */
7400
7401 static htab_up
7402 allocate_type_unit_groups_table ()
7403 {
7404 return htab_up (htab_create_alloc (3,
7405 hash_type_unit_group,
7406 eq_type_unit_group,
7407 NULL, xcalloc, xfree));
7408 }
7409
7410 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7411 partial symtabs. We combine several TUs per psymtab to not let the size
7412 of any one psymtab grow too big. */
7413 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7414 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7415
7416 /* Helper routine for get_type_unit_group.
7417 Create the type_unit_group object used to hold one or more TUs. */
7418
7419 static struct type_unit_group *
7420 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7421 {
7422 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7423 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7424 struct dwarf2_per_cu_data *per_cu;
7425 struct type_unit_group *tu_group;
7426
7427 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
7428 per_cu = &tu_group->per_cu;
7429 per_cu->per_bfd = per_bfd;
7430
7431 if (per_bfd->using_index)
7432 {
7433 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7434 struct dwarf2_per_cu_quick_data);
7435 }
7436 else
7437 {
7438 unsigned int line_offset = to_underlying (line_offset_struct);
7439 dwarf2_psymtab *pst;
7440 std::string name;
7441
7442 /* Give the symtab a useful name for debug purposes. */
7443 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7444 name = string_printf ("<type_units_%d>",
7445 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7446 else
7447 name = string_printf ("<type_units_at_0x%x>", line_offset);
7448
7449 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
7450 pst->anonymous = true;
7451 }
7452
7453 tu_group->hash.dwo_unit = cu->dwo_unit;
7454 tu_group->hash.line_sect_off = line_offset_struct;
7455
7456 return tu_group;
7457 }
7458
7459 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7460 STMT_LIST is a DW_AT_stmt_list attribute. */
7461
7462 static struct type_unit_group *
7463 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7464 {
7465 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7466 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7467 struct type_unit_group *tu_group;
7468 void **slot;
7469 unsigned int line_offset;
7470 struct type_unit_group type_unit_group_for_lookup;
7471
7472 if (per_objfile->per_bfd->type_unit_groups == NULL)
7473 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7474
7475 /* Do we need to create a new group, or can we use an existing one? */
7476
7477 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
7478 {
7479 line_offset = stmt_list->as_unsigned ();
7480 ++tu_stats->nr_symtab_sharers;
7481 }
7482 else
7483 {
7484 /* Ugh, no stmt_list. Rare, but we have to handle it.
7485 We can do various things here like create one group per TU or
7486 spread them over multiple groups to split up the expansion work.
7487 To avoid worst case scenarios (too many groups or too large groups)
7488 we, umm, group them in bunches. */
7489 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7490 | (tu_stats->nr_stmt_less_type_units
7491 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7492 ++tu_stats->nr_stmt_less_type_units;
7493 }
7494
7495 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7496 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7497 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7498 &type_unit_group_for_lookup, INSERT);
7499 if (*slot != NULL)
7500 {
7501 tu_group = (struct type_unit_group *) *slot;
7502 gdb_assert (tu_group != NULL);
7503 }
7504 else
7505 {
7506 sect_offset line_offset_struct = (sect_offset) line_offset;
7507 tu_group = create_type_unit_group (cu, line_offset_struct);
7508 *slot = tu_group;
7509 ++tu_stats->nr_symtabs;
7510 }
7511
7512 return tu_group;
7513 }
7514 \f
7515 /* Partial symbol tables. */
7516
7517 /* Create a psymtab named NAME and assign it to PER_CU.
7518
7519 The caller must fill in the following details:
7520 dirname, textlow, texthigh. */
7521
7522 static dwarf2_psymtab *
7523 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7524 dwarf2_per_objfile *per_objfile,
7525 const char *name)
7526 {
7527 struct objfile *objfile = per_objfile->objfile;
7528 dwarf2_psymtab *pst;
7529
7530 pst = new dwarf2_psymtab (name, objfile, per_cu);
7531
7532 pst->psymtabs_addrmap_supported = true;
7533
7534 /* This is the glue that links PST into GDB's symbol API. */
7535 per_cu->v.psymtab = pst;
7536
7537 return pst;
7538 }
7539
7540 /* DIE reader function for process_psymtab_comp_unit. */
7541
7542 static void
7543 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7544 const gdb_byte *info_ptr,
7545 struct die_info *comp_unit_die,
7546 enum language pretend_language)
7547 {
7548 struct dwarf2_cu *cu = reader->cu;
7549 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7550 struct objfile *objfile = per_objfile->objfile;
7551 struct gdbarch *gdbarch = objfile->arch ();
7552 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7553 CORE_ADDR baseaddr;
7554 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7555 dwarf2_psymtab *pst;
7556 enum pc_bounds_kind cu_bounds_kind;
7557 const char *filename;
7558
7559 gdb_assert (! per_cu->is_debug_types);
7560
7561 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7562
7563 /* Allocate a new partial symbol table structure. */
7564 gdb::unique_xmalloc_ptr<char> debug_filename;
7565 static const char artificial[] = "<artificial>";
7566 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7567 if (filename == NULL)
7568 filename = "";
7569 else if (strcmp (filename, artificial) == 0)
7570 {
7571 debug_filename.reset (concat (artificial, "@",
7572 sect_offset_str (per_cu->sect_off),
7573 (char *) NULL));
7574 filename = debug_filename.get ();
7575 }
7576
7577 pst = create_partial_symtab (per_cu, per_objfile, filename);
7578
7579 /* This must be done before calling dwarf2_build_include_psymtabs. */
7580 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7581
7582 baseaddr = objfile->text_section_offset ();
7583
7584 dwarf2_find_base_address (comp_unit_die, cu);
7585
7586 /* Possibly set the default values of LOWPC and HIGHPC from
7587 `DW_AT_ranges'. */
7588 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7589 &best_highpc, cu, pst);
7590 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7591 {
7592 CORE_ADDR low
7593 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7594 - baseaddr);
7595 CORE_ADDR high
7596 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7597 - baseaddr - 1);
7598 /* Store the contiguous range if it is not empty; it can be
7599 empty for CUs with no code. */
7600 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7601 low, high, pst);
7602 }
7603
7604 /* Check if comp unit has_children.
7605 If so, read the rest of the partial symbols from this comp unit.
7606 If not, there's no more debug_info for this comp unit. */
7607 if (comp_unit_die->has_children)
7608 {
7609 struct partial_die_info *first_die;
7610 CORE_ADDR lowpc, highpc;
7611
7612 lowpc = ((CORE_ADDR) -1);
7613 highpc = ((CORE_ADDR) 0);
7614
7615 first_die = load_partial_dies (reader, info_ptr, 1);
7616
7617 scan_partial_symbols (first_die, &lowpc, &highpc,
7618 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7619
7620 /* If we didn't find a lowpc, set it to highpc to avoid
7621 complaints from `maint check'. */
7622 if (lowpc == ((CORE_ADDR) -1))
7623 lowpc = highpc;
7624
7625 /* If the compilation unit didn't have an explicit address range,
7626 then use the information extracted from its child dies. */
7627 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7628 {
7629 best_lowpc = lowpc;
7630 best_highpc = highpc;
7631 }
7632 }
7633 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7634 best_lowpc + baseaddr)
7635 - baseaddr);
7636 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7637 best_highpc + baseaddr)
7638 - baseaddr);
7639
7640 pst->end ();
7641
7642 if (!cu->per_cu->imported_symtabs_empty ())
7643 {
7644 int i;
7645 int len = cu->per_cu->imported_symtabs_size ();
7646
7647 /* Fill in 'dependencies' here; we fill in 'users' in a
7648 post-pass. */
7649 pst->number_of_dependencies = len;
7650 pst->dependencies
7651 = objfile->partial_symtabs->allocate_dependencies (len);
7652 for (i = 0; i < len; ++i)
7653 {
7654 pst->dependencies[i]
7655 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7656 }
7657
7658 cu->per_cu->imported_symtabs_free ();
7659 }
7660
7661 /* Get the list of files included in the current compilation unit,
7662 and build a psymtab for each of them. */
7663 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7664
7665 if (dwarf_read_debug)
7666 fprintf_unfiltered (gdb_stdlog,
7667 "Psymtab for %s unit @%s: %s - %s"
7668 ", %d global, %d static syms\n",
7669 per_cu->is_debug_types ? "type" : "comp",
7670 sect_offset_str (per_cu->sect_off),
7671 paddress (gdbarch, pst->text_low (objfile)),
7672 paddress (gdbarch, pst->text_high (objfile)),
7673 (int) pst->global_psymbols.size (),
7674 (int) pst->static_psymbols.size ());
7675 }
7676
7677 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7678 Process compilation unit THIS_CU for a psymtab. */
7679
7680 static void
7681 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7682 dwarf2_per_objfile *per_objfile,
7683 bool want_partial_unit,
7684 enum language pretend_language)
7685 {
7686 /* If this compilation unit was already read in, free the
7687 cached copy in order to read it in again. This is
7688 necessary because we skipped some symbols when we first
7689 read in the compilation unit (see load_partial_dies).
7690 This problem could be avoided, but the benefit is unclear. */
7691 per_objfile->remove_cu (this_cu);
7692
7693 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7694
7695 switch (reader.comp_unit_die->tag)
7696 {
7697 case DW_TAG_compile_unit:
7698 this_cu->unit_type = DW_UT_compile;
7699 break;
7700 case DW_TAG_partial_unit:
7701 this_cu->unit_type = DW_UT_partial;
7702 break;
7703 default:
7704 abort ();
7705 }
7706
7707 if (reader.dummy_p)
7708 {
7709 /* Nothing. */
7710 }
7711 else if (this_cu->is_debug_types)
7712 build_type_psymtabs_reader (&reader, reader.info_ptr,
7713 reader.comp_unit_die);
7714 else if (want_partial_unit
7715 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7716 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7717 reader.comp_unit_die,
7718 pretend_language);
7719
7720 this_cu->lang = reader.cu->language;
7721
7722 /* Age out any secondary CUs. */
7723 per_objfile->age_comp_units ();
7724 }
7725
7726 /* Reader function for build_type_psymtabs. */
7727
7728 static void
7729 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7730 const gdb_byte *info_ptr,
7731 struct die_info *type_unit_die)
7732 {
7733 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7734 struct dwarf2_cu *cu = reader->cu;
7735 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7736 struct signatured_type *sig_type;
7737 struct type_unit_group *tu_group;
7738 struct attribute *attr;
7739 struct partial_die_info *first_die;
7740 CORE_ADDR lowpc, highpc;
7741 dwarf2_psymtab *pst;
7742
7743 gdb_assert (per_cu->is_debug_types);
7744 sig_type = (struct signatured_type *) per_cu;
7745
7746 if (! type_unit_die->has_children)
7747 return;
7748
7749 attr = type_unit_die->attr (DW_AT_stmt_list);
7750 tu_group = get_type_unit_group (cu, attr);
7751
7752 if (tu_group->tus == nullptr)
7753 tu_group->tus = new std::vector<signatured_type *>;
7754 tu_group->tus->push_back (sig_type);
7755
7756 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7757 pst = create_partial_symtab (per_cu, per_objfile, "");
7758 pst->anonymous = true;
7759
7760 first_die = load_partial_dies (reader, info_ptr, 1);
7761
7762 lowpc = (CORE_ADDR) -1;
7763 highpc = (CORE_ADDR) 0;
7764 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7765
7766 pst->end ();
7767 }
7768
7769 /* Struct used to sort TUs by their abbreviation table offset. */
7770
7771 struct tu_abbrev_offset
7772 {
7773 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7774 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7775 {}
7776
7777 signatured_type *sig_type;
7778 sect_offset abbrev_offset;
7779 };
7780
7781 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7782
7783 static bool
7784 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7785 const struct tu_abbrev_offset &b)
7786 {
7787 return a.abbrev_offset < b.abbrev_offset;
7788 }
7789
7790 /* Efficiently read all the type units.
7791 This does the bulk of the work for build_type_psymtabs.
7792
7793 The efficiency is because we sort TUs by the abbrev table they use and
7794 only read each abbrev table once. In one program there are 200K TUs
7795 sharing 8K abbrev tables.
7796
7797 The main purpose of this function is to support building the
7798 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7799 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7800 can collapse the search space by grouping them by stmt_list.
7801 The savings can be significant, in the same program from above the 200K TUs
7802 share 8K stmt_list tables.
7803
7804 FUNC is expected to call get_type_unit_group, which will create the
7805 struct type_unit_group if necessary and add it to
7806 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7807
7808 static void
7809 build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
7810 {
7811 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7812 abbrev_table_up abbrev_table;
7813 sect_offset abbrev_offset;
7814
7815 /* It's up to the caller to not call us multiple times. */
7816 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7817
7818 if (per_objfile->per_bfd->all_type_units.empty ())
7819 return;
7820
7821 /* TUs typically share abbrev tables, and there can be way more TUs than
7822 abbrev tables. Sort by abbrev table to reduce the number of times we
7823 read each abbrev table in.
7824 Alternatives are to punt or to maintain a cache of abbrev tables.
7825 This is simpler and efficient enough for now.
7826
7827 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7828 symtab to use). Typically TUs with the same abbrev offset have the same
7829 stmt_list value too so in practice this should work well.
7830
7831 The basic algorithm here is:
7832
7833 sort TUs by abbrev table
7834 for each TU with same abbrev table:
7835 read abbrev table if first user
7836 read TU top level DIE
7837 [IWBN if DWO skeletons had DW_AT_stmt_list]
7838 call FUNC */
7839
7840 if (dwarf_read_debug)
7841 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7842
7843 /* Sort in a separate table to maintain the order of all_type_units
7844 for .gdb_index: TU indices directly index all_type_units. */
7845 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7846 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
7847
7848 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
7849 sorted_by_abbrev.emplace_back
7850 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
7851 sig_type->per_cu.sect_off));
7852
7853 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7854 sort_tu_by_abbrev_offset);
7855
7856 abbrev_offset = (sect_offset) ~(unsigned) 0;
7857
7858 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7859 {
7860 /* Switch to the next abbrev table if necessary. */
7861 if (abbrev_table == NULL
7862 || tu.abbrev_offset != abbrev_offset)
7863 {
7864 abbrev_offset = tu.abbrev_offset;
7865 abbrev_table =
7866 abbrev_table::read (per_objfile->objfile,
7867 &per_objfile->per_bfd->abbrev, abbrev_offset);
7868 ++tu_stats->nr_uniq_abbrev_tables;
7869 }
7870
7871 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
7872 abbrev_table.get (), nullptr, false);
7873 if (!reader.dummy_p)
7874 build_type_psymtabs_reader (&reader, reader.info_ptr,
7875 reader.comp_unit_die);
7876 }
7877 }
7878
7879 /* Print collected type unit statistics. */
7880
7881 static void
7882 print_tu_stats (dwarf2_per_objfile *per_objfile)
7883 {
7884 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7885
7886 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7887 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7888 per_objfile->per_bfd->all_type_units.size ());
7889 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7890 tu_stats->nr_uniq_abbrev_tables);
7891 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7892 tu_stats->nr_symtabs);
7893 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7894 tu_stats->nr_symtab_sharers);
7895 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7896 tu_stats->nr_stmt_less_type_units);
7897 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7898 tu_stats->nr_all_type_units_reallocs);
7899 }
7900
7901 /* Traversal function for build_type_psymtabs. */
7902
7903 static int
7904 build_type_psymtab_dependencies (void **slot, void *info)
7905 {
7906 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7907 struct objfile *objfile = per_objfile->objfile;
7908 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7909 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7910 dwarf2_psymtab *pst = per_cu->v.psymtab;
7911 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7912 int i;
7913
7914 gdb_assert (len > 0);
7915 gdb_assert (per_cu->type_unit_group_p ());
7916
7917 pst->number_of_dependencies = len;
7918 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7919 for (i = 0; i < len; ++i)
7920 {
7921 struct signatured_type *iter = tu_group->tus->at (i);
7922 gdb_assert (iter->per_cu.is_debug_types);
7923 pst->dependencies[i] = iter->per_cu.v.psymtab;
7924 iter->type_unit_group = tu_group;
7925 }
7926
7927 delete tu_group->tus;
7928 tu_group->tus = nullptr;
7929
7930 return 1;
7931 }
7932
7933 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7934 Build partial symbol tables for the .debug_types comp-units. */
7935
7936 static void
7937 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7938 {
7939 if (! create_all_type_units (per_objfile))
7940 return;
7941
7942 build_type_psymtabs_1 (per_objfile);
7943 }
7944
7945 /* Traversal function for process_skeletonless_type_unit.
7946 Read a TU in a DWO file and build partial symbols for it. */
7947
7948 static int
7949 process_skeletonless_type_unit (void **slot, void *info)
7950 {
7951 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7952 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7953 struct signatured_type find_entry, *entry;
7954
7955 /* If this TU doesn't exist in the global table, add it and read it in. */
7956
7957 if (per_objfile->per_bfd->signatured_types == NULL)
7958 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7959
7960 find_entry.signature = dwo_unit->signature;
7961 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7962 &find_entry, INSERT);
7963 /* If we've already seen this type there's nothing to do. What's happening
7964 is we're doing our own version of comdat-folding here. */
7965 if (*slot != NULL)
7966 return 1;
7967
7968 /* This does the job that create_all_type_units would have done for
7969 this TU. */
7970 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7971 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7972 *slot = entry;
7973
7974 /* This does the job that build_type_psymtabs_1 would have done. */
7975 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
7976 if (!reader.dummy_p)
7977 build_type_psymtabs_reader (&reader, reader.info_ptr,
7978 reader.comp_unit_die);
7979
7980 return 1;
7981 }
7982
7983 /* Traversal function for process_skeletonless_type_units. */
7984
7985 static int
7986 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7987 {
7988 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7989
7990 if (dwo_file->tus != NULL)
7991 htab_traverse_noresize (dwo_file->tus.get (),
7992 process_skeletonless_type_unit, info);
7993
7994 return 1;
7995 }
7996
7997 /* Scan all TUs of DWO files, verifying we've processed them.
7998 This is needed in case a TU was emitted without its skeleton.
7999 Note: This can't be done until we know what all the DWO files are. */
8000
8001 static void
8002 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
8003 {
8004 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8005 if (get_dwp_file (per_objfile) == NULL
8006 && per_objfile->per_bfd->dwo_files != NULL)
8007 {
8008 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
8009 process_dwo_file_for_skeletonless_type_units,
8010 per_objfile);
8011 }
8012 }
8013
8014 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8015
8016 static void
8017 set_partial_user (dwarf2_per_objfile *per_objfile)
8018 {
8019 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8020 {
8021 dwarf2_psymtab *pst = per_cu->v.psymtab;
8022
8023 if (pst == NULL)
8024 continue;
8025
8026 for (int j = 0; j < pst->number_of_dependencies; ++j)
8027 {
8028 /* Set the 'user' field only if it is not already set. */
8029 if (pst->dependencies[j]->user == NULL)
8030 pst->dependencies[j]->user = pst;
8031 }
8032 }
8033 }
8034
8035 /* Build the partial symbol table by doing a quick pass through the
8036 .debug_info and .debug_abbrev sections. */
8037
8038 static void
8039 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
8040 {
8041 struct objfile *objfile = per_objfile->objfile;
8042
8043 if (dwarf_read_debug)
8044 {
8045 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8046 objfile_name (objfile));
8047 }
8048
8049 scoped_restore restore_reading_psyms
8050 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
8051 true);
8052
8053 per_objfile->per_bfd->info.read (objfile);
8054
8055 /* Any cached compilation units will be linked by the per-objfile
8056 read_in_chain. Make sure to free them when we're done. */
8057 free_cached_comp_units freer (per_objfile);
8058
8059 build_type_psymtabs (per_objfile);
8060
8061 create_all_comp_units (per_objfile);
8062
8063 /* Create a temporary address map on a temporary obstack. We later
8064 copy this to the final obstack. */
8065 auto_obstack temp_obstack;
8066
8067 scoped_restore save_psymtabs_addrmap
8068 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8069 addrmap_create_mutable (&temp_obstack));
8070
8071 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8072 {
8073 if (per_cu->v.psymtab != NULL)
8074 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8075 continue;
8076 process_psymtab_comp_unit (per_cu, per_objfile, false,
8077 language_minimal);
8078 }
8079
8080 /* This has to wait until we read the CUs, we need the list of DWOs. */
8081 process_skeletonless_type_units (per_objfile);
8082
8083 /* Now that all TUs have been processed we can fill in the dependencies. */
8084 if (per_objfile->per_bfd->type_unit_groups != NULL)
8085 {
8086 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8087 build_type_psymtab_dependencies, per_objfile);
8088 }
8089
8090 if (dwarf_read_debug)
8091 print_tu_stats (per_objfile);
8092
8093 set_partial_user (per_objfile);
8094
8095 objfile->partial_symtabs->psymtabs_addrmap
8096 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8097 objfile->partial_symtabs->obstack ());
8098 /* At this point we want to keep the address map. */
8099 save_psymtabs_addrmap.release ();
8100
8101 if (dwarf_read_debug)
8102 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8103 objfile_name (objfile));
8104 }
8105
8106 /* Load the partial DIEs for a secondary CU into memory.
8107 This is also used when rereading a primary CU with load_all_dies. */
8108
8109 static void
8110 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8111 dwarf2_per_objfile *per_objfile,
8112 dwarf2_cu *existing_cu)
8113 {
8114 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
8115
8116 if (!reader.dummy_p)
8117 {
8118 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8119 language_minimal);
8120
8121 /* Check if comp unit has_children.
8122 If so, read the rest of the partial symbols from this comp unit.
8123 If not, there's no more debug_info for this comp unit. */
8124 if (reader.comp_unit_die->has_children)
8125 load_partial_dies (&reader, reader.info_ptr, 0);
8126
8127 reader.keep ();
8128 }
8129 }
8130
8131 static void
8132 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
8133 struct dwarf2_section_info *section,
8134 struct dwarf2_section_info *abbrev_section,
8135 unsigned int is_dwz)
8136 {
8137 const gdb_byte *info_ptr;
8138 struct objfile *objfile = per_objfile->objfile;
8139
8140 if (dwarf_read_debug)
8141 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8142 section->get_name (),
8143 section->get_file_name ());
8144
8145 section->read (objfile);
8146
8147 info_ptr = section->buffer;
8148
8149 while (info_ptr < section->buffer + section->size)
8150 {
8151 struct dwarf2_per_cu_data *this_cu;
8152
8153 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8154
8155 comp_unit_head cu_header;
8156 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
8157 abbrev_section, info_ptr,
8158 rcuh_kind::COMPILE);
8159
8160 /* Save the compilation unit for later lookup. */
8161 if (cu_header.unit_type != DW_UT_type)
8162 this_cu = per_objfile->per_bfd->allocate_per_cu ();
8163 else
8164 {
8165 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
8166 sig_type->signature = cu_header.signature;
8167 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8168 this_cu = &sig_type->per_cu;
8169 }
8170 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8171 this_cu->sect_off = sect_off;
8172 this_cu->length = cu_header.length + cu_header.initial_length_size;
8173 this_cu->is_dwz = is_dwz;
8174 this_cu->section = section;
8175
8176 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8177
8178 info_ptr = info_ptr + this_cu->length;
8179 }
8180 }
8181
8182 /* Create a list of all compilation units in OBJFILE.
8183 This is only done for -readnow and building partial symtabs. */
8184
8185 static void
8186 create_all_comp_units (dwarf2_per_objfile *per_objfile)
8187 {
8188 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8189 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8190 &per_objfile->per_bfd->abbrev, 0);
8191
8192 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8193 if (dwz != NULL)
8194 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
8195 }
8196
8197 /* Process all loaded DIEs for compilation unit CU, starting at
8198 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8199 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8200 DW_AT_ranges). See the comments of add_partial_subprogram on how
8201 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8202
8203 static void
8204 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8205 CORE_ADDR *highpc, int set_addrmap,
8206 struct dwarf2_cu *cu)
8207 {
8208 struct partial_die_info *pdi;
8209
8210 /* Now, march along the PDI's, descending into ones which have
8211 interesting children but skipping the children of the other ones,
8212 until we reach the end of the compilation unit. */
8213
8214 pdi = first_die;
8215
8216 while (pdi != NULL)
8217 {
8218 pdi->fixup (cu);
8219
8220 /* Anonymous namespaces or modules have no name but have interesting
8221 children, so we need to look at them. Ditto for anonymous
8222 enums. */
8223
8224 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
8225 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8226 || pdi->tag == DW_TAG_imported_unit
8227 || pdi->tag == DW_TAG_inlined_subroutine)
8228 {
8229 switch (pdi->tag)
8230 {
8231 case DW_TAG_subprogram:
8232 case DW_TAG_inlined_subroutine:
8233 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8234 if (cu->language == language_cplus)
8235 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8236 set_addrmap, cu);
8237 break;
8238 case DW_TAG_constant:
8239 case DW_TAG_variable:
8240 case DW_TAG_typedef:
8241 case DW_TAG_union_type:
8242 if (!pdi->is_declaration
8243 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8244 {
8245 add_partial_symbol (pdi, cu);
8246 }
8247 break;
8248 case DW_TAG_class_type:
8249 case DW_TAG_interface_type:
8250 case DW_TAG_structure_type:
8251 if (!pdi->is_declaration)
8252 {
8253 add_partial_symbol (pdi, cu);
8254 }
8255 if ((cu->language == language_rust
8256 || cu->language == language_cplus) && pdi->has_children)
8257 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8258 set_addrmap, cu);
8259 break;
8260 case DW_TAG_enumeration_type:
8261 if (!pdi->is_declaration)
8262 add_partial_enumeration (pdi, cu);
8263 break;
8264 case DW_TAG_base_type:
8265 case DW_TAG_subrange_type:
8266 /* File scope base type definitions are added to the partial
8267 symbol table. */
8268 add_partial_symbol (pdi, cu);
8269 break;
8270 case DW_TAG_namespace:
8271 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8272 break;
8273 case DW_TAG_module:
8274 if (!pdi->is_declaration)
8275 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8276 break;
8277 case DW_TAG_imported_unit:
8278 {
8279 struct dwarf2_per_cu_data *per_cu;
8280
8281 /* For now we don't handle imported units in type units. */
8282 if (cu->per_cu->is_debug_types)
8283 {
8284 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8285 " supported in type units [in module %s]"),
8286 objfile_name (cu->per_objfile->objfile));
8287 }
8288
8289 per_cu = dwarf2_find_containing_comp_unit
8290 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8291
8292 /* Go read the partial unit, if needed. */
8293 if (per_cu->v.psymtab == NULL)
8294 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8295 cu->language);
8296
8297 cu->per_cu->imported_symtabs_push (per_cu);
8298 }
8299 break;
8300 case DW_TAG_imported_declaration:
8301 add_partial_symbol (pdi, cu);
8302 break;
8303 default:
8304 break;
8305 }
8306 }
8307
8308 /* If the die has a sibling, skip to the sibling. */
8309
8310 pdi = pdi->die_sibling;
8311 }
8312 }
8313
8314 /* Functions used to compute the fully scoped name of a partial DIE.
8315
8316 Normally, this is simple. For C++, the parent DIE's fully scoped
8317 name is concatenated with "::" and the partial DIE's name.
8318 Enumerators are an exception; they use the scope of their parent
8319 enumeration type, i.e. the name of the enumeration type is not
8320 prepended to the enumerator.
8321
8322 There are two complexities. One is DW_AT_specification; in this
8323 case "parent" means the parent of the target of the specification,
8324 instead of the direct parent of the DIE. The other is compilers
8325 which do not emit DW_TAG_namespace; in this case we try to guess
8326 the fully qualified name of structure types from their members'
8327 linkage names. This must be done using the DIE's children rather
8328 than the children of any DW_AT_specification target. We only need
8329 to do this for structures at the top level, i.e. if the target of
8330 any DW_AT_specification (if any; otherwise the DIE itself) does not
8331 have a parent. */
8332
8333 /* Compute the scope prefix associated with PDI's parent, in
8334 compilation unit CU. The result will be allocated on CU's
8335 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8336 field. NULL is returned if no prefix is necessary. */
8337 static const char *
8338 partial_die_parent_scope (struct partial_die_info *pdi,
8339 struct dwarf2_cu *cu)
8340 {
8341 const char *grandparent_scope;
8342 struct partial_die_info *parent, *real_pdi;
8343
8344 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8345 then this means the parent of the specification DIE. */
8346
8347 real_pdi = pdi;
8348 while (real_pdi->has_specification)
8349 {
8350 auto res = find_partial_die (real_pdi->spec_offset,
8351 real_pdi->spec_is_dwz, cu);
8352 real_pdi = res.pdi;
8353 cu = res.cu;
8354 }
8355
8356 parent = real_pdi->die_parent;
8357 if (parent == NULL)
8358 return NULL;
8359
8360 if (parent->scope_set)
8361 return parent->scope;
8362
8363 parent->fixup (cu);
8364
8365 grandparent_scope = partial_die_parent_scope (parent, cu);
8366
8367 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8368 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8369 Work around this problem here. */
8370 if (cu->language == language_cplus
8371 && parent->tag == DW_TAG_namespace
8372 && strcmp (parent->name (cu), "::") == 0
8373 && grandparent_scope == NULL)
8374 {
8375 parent->scope = NULL;
8376 parent->scope_set = 1;
8377 return NULL;
8378 }
8379
8380 /* Nested subroutines in Fortran get a prefix. */
8381 if (pdi->tag == DW_TAG_enumerator)
8382 /* Enumerators should not get the name of the enumeration as a prefix. */
8383 parent->scope = grandparent_scope;
8384 else if (parent->tag == DW_TAG_namespace
8385 || parent->tag == DW_TAG_module
8386 || parent->tag == DW_TAG_structure_type
8387 || parent->tag == DW_TAG_class_type
8388 || parent->tag == DW_TAG_interface_type
8389 || parent->tag == DW_TAG_union_type
8390 || parent->tag == DW_TAG_enumeration_type
8391 || (cu->language == language_fortran
8392 && parent->tag == DW_TAG_subprogram
8393 && pdi->tag == DW_TAG_subprogram))
8394 {
8395 if (grandparent_scope == NULL)
8396 parent->scope = parent->name (cu);
8397 else
8398 parent->scope = typename_concat (&cu->comp_unit_obstack,
8399 grandparent_scope,
8400 parent->name (cu), 0, cu);
8401 }
8402 else
8403 {
8404 /* FIXME drow/2004-04-01: What should we be doing with
8405 function-local names? For partial symbols, we should probably be
8406 ignoring them. */
8407 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8408 dwarf_tag_name (parent->tag),
8409 sect_offset_str (pdi->sect_off));
8410 parent->scope = grandparent_scope;
8411 }
8412
8413 parent->scope_set = 1;
8414 return parent->scope;
8415 }
8416
8417 /* Return the fully scoped name associated with PDI, from compilation unit
8418 CU. The result will be allocated with malloc. */
8419
8420 static gdb::unique_xmalloc_ptr<char>
8421 partial_die_full_name (struct partial_die_info *pdi,
8422 struct dwarf2_cu *cu)
8423 {
8424 const char *parent_scope;
8425
8426 /* If this is a template instantiation, we can not work out the
8427 template arguments from partial DIEs. So, unfortunately, we have
8428 to go through the full DIEs. At least any work we do building
8429 types here will be reused if full symbols are loaded later. */
8430 if (pdi->has_template_arguments)
8431 {
8432 pdi->fixup (cu);
8433
8434 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8435 {
8436 struct die_info *die;
8437 struct attribute attr;
8438 struct dwarf2_cu *ref_cu = cu;
8439
8440 /* DW_FORM_ref_addr is using section offset. */
8441 attr.name = (enum dwarf_attribute) 0;
8442 attr.form = DW_FORM_ref_addr;
8443 attr.u.unsnd = to_underlying (pdi->sect_off);
8444 die = follow_die_ref (NULL, &attr, &ref_cu);
8445
8446 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8447 }
8448 }
8449
8450 parent_scope = partial_die_parent_scope (pdi, cu);
8451 if (parent_scope == NULL)
8452 return NULL;
8453 else
8454 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8455 pdi->name (cu),
8456 0, cu));
8457 }
8458
8459 static void
8460 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8461 {
8462 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8463 struct objfile *objfile = per_objfile->objfile;
8464 struct gdbarch *gdbarch = objfile->arch ();
8465 CORE_ADDR addr = 0;
8466 const char *actual_name = NULL;
8467 CORE_ADDR baseaddr;
8468
8469 baseaddr = objfile->text_section_offset ();
8470
8471 gdb::unique_xmalloc_ptr<char> built_actual_name
8472 = partial_die_full_name (pdi, cu);
8473 if (built_actual_name != NULL)
8474 actual_name = built_actual_name.get ();
8475
8476 if (actual_name == NULL)
8477 actual_name = pdi->name (cu);
8478
8479 partial_symbol psymbol;
8480 memset (&psymbol, 0, sizeof (psymbol));
8481 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8482 psymbol.ginfo.section = -1;
8483
8484 /* The code below indicates that the psymbol should be installed by
8485 setting this. */
8486 gdb::optional<psymbol_placement> where;
8487
8488 switch (pdi->tag)
8489 {
8490 case DW_TAG_inlined_subroutine:
8491 case DW_TAG_subprogram:
8492 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8493 - baseaddr);
8494 if (pdi->is_external
8495 || cu->language == language_ada
8496 || (cu->language == language_fortran
8497 && pdi->die_parent != NULL
8498 && pdi->die_parent->tag == DW_TAG_subprogram))
8499 {
8500 /* Normally, only "external" DIEs are part of the global scope.
8501 But in Ada and Fortran, we want to be able to access nested
8502 procedures globally. So all Ada and Fortran subprograms are
8503 stored in the global scope. */
8504 where = psymbol_placement::GLOBAL;
8505 }
8506 else
8507 where = psymbol_placement::STATIC;
8508
8509 psymbol.domain = VAR_DOMAIN;
8510 psymbol.aclass = LOC_BLOCK;
8511 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8512 psymbol.ginfo.value.address = addr;
8513
8514 if (pdi->main_subprogram && actual_name != NULL)
8515 set_objfile_main_name (objfile, actual_name, cu->language);
8516 break;
8517 case DW_TAG_constant:
8518 psymbol.domain = VAR_DOMAIN;
8519 psymbol.aclass = LOC_STATIC;
8520 where = (pdi->is_external
8521 ? psymbol_placement::GLOBAL
8522 : psymbol_placement::STATIC);
8523 break;
8524 case DW_TAG_variable:
8525 if (pdi->d.locdesc)
8526 addr = decode_locdesc (pdi->d.locdesc, cu);
8527
8528 if (pdi->d.locdesc
8529 && addr == 0
8530 && !per_objfile->per_bfd->has_section_at_zero)
8531 {
8532 /* A global or static variable may also have been stripped
8533 out by the linker if unused, in which case its address
8534 will be nullified; do not add such variables into partial
8535 symbol table then. */
8536 }
8537 else if (pdi->is_external)
8538 {
8539 /* Global Variable.
8540 Don't enter into the minimal symbol tables as there is
8541 a minimal symbol table entry from the ELF symbols already.
8542 Enter into partial symbol table if it has a location
8543 descriptor or a type.
8544 If the location descriptor is missing, new_symbol will create
8545 a LOC_UNRESOLVED symbol, the address of the variable will then
8546 be determined from the minimal symbol table whenever the variable
8547 is referenced.
8548 The address for the partial symbol table entry is not
8549 used by GDB, but it comes in handy for debugging partial symbol
8550 table building. */
8551
8552 if (pdi->d.locdesc || pdi->has_type)
8553 {
8554 psymbol.domain = VAR_DOMAIN;
8555 psymbol.aclass = LOC_STATIC;
8556 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8557 psymbol.ginfo.value.address = addr;
8558 where = psymbol_placement::GLOBAL;
8559 }
8560 }
8561 else
8562 {
8563 int has_loc = pdi->d.locdesc != NULL;
8564
8565 /* Static Variable. Skip symbols whose value we cannot know (those
8566 without location descriptors or constant values). */
8567 if (!has_loc && !pdi->has_const_value)
8568 return;
8569
8570 psymbol.domain = VAR_DOMAIN;
8571 psymbol.aclass = LOC_STATIC;
8572 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8573 if (has_loc)
8574 psymbol.ginfo.value.address = addr;
8575 where = psymbol_placement::STATIC;
8576 }
8577 break;
8578 case DW_TAG_typedef:
8579 case DW_TAG_base_type:
8580 case DW_TAG_subrange_type:
8581 psymbol.domain = VAR_DOMAIN;
8582 psymbol.aclass = LOC_TYPEDEF;
8583 where = psymbol_placement::STATIC;
8584 break;
8585 case DW_TAG_imported_declaration:
8586 case DW_TAG_namespace:
8587 psymbol.domain = VAR_DOMAIN;
8588 psymbol.aclass = LOC_TYPEDEF;
8589 where = psymbol_placement::GLOBAL;
8590 break;
8591 case DW_TAG_module:
8592 /* With Fortran 77 there might be a "BLOCK DATA" module
8593 available without any name. If so, we skip the module as it
8594 doesn't bring any value. */
8595 if (actual_name != nullptr)
8596 {
8597 psymbol.domain = MODULE_DOMAIN;
8598 psymbol.aclass = LOC_TYPEDEF;
8599 where = psymbol_placement::GLOBAL;
8600 }
8601 break;
8602 case DW_TAG_class_type:
8603 case DW_TAG_interface_type:
8604 case DW_TAG_structure_type:
8605 case DW_TAG_union_type:
8606 case DW_TAG_enumeration_type:
8607 /* Skip external references. The DWARF standard says in the section
8608 about "Structure, Union, and Class Type Entries": "An incomplete
8609 structure, union or class type is represented by a structure,
8610 union or class entry that does not have a byte size attribute
8611 and that has a DW_AT_declaration attribute." */
8612 if (!pdi->has_byte_size && pdi->is_declaration)
8613 return;
8614
8615 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8616 static vs. global. */
8617 psymbol.domain = STRUCT_DOMAIN;
8618 psymbol.aclass = LOC_TYPEDEF;
8619 where = (cu->language == language_cplus
8620 ? psymbol_placement::GLOBAL
8621 : psymbol_placement::STATIC);
8622 break;
8623 case DW_TAG_enumerator:
8624 psymbol.domain = VAR_DOMAIN;
8625 psymbol.aclass = LOC_CONST;
8626 where = (cu->language == language_cplus
8627 ? psymbol_placement::GLOBAL
8628 : psymbol_placement::STATIC);
8629 break;
8630 default:
8631 break;
8632 }
8633
8634 if (where.has_value ())
8635 {
8636 if (built_actual_name != nullptr)
8637 actual_name = objfile->intern (actual_name);
8638 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8639 psymbol.ginfo.set_linkage_name (actual_name);
8640 else
8641 {
8642 psymbol.ginfo.set_demangled_name (actual_name,
8643 &objfile->objfile_obstack);
8644 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8645 }
8646 cu->per_cu->v.psymtab->add_psymbol (psymbol, *where, objfile);
8647 }
8648 }
8649
8650 /* Read a partial die corresponding to a namespace; also, add a symbol
8651 corresponding to that namespace to the symbol table. NAMESPACE is
8652 the name of the enclosing namespace. */
8653
8654 static void
8655 add_partial_namespace (struct partial_die_info *pdi,
8656 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8657 int set_addrmap, struct dwarf2_cu *cu)
8658 {
8659 /* Add a symbol for the namespace. */
8660
8661 add_partial_symbol (pdi, cu);
8662
8663 /* Now scan partial symbols in that namespace. */
8664
8665 if (pdi->has_children)
8666 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8667 }
8668
8669 /* Read a partial die corresponding to a Fortran module. */
8670
8671 static void
8672 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8673 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8674 {
8675 /* Add a symbol for the namespace. */
8676
8677 add_partial_symbol (pdi, cu);
8678
8679 /* Now scan partial symbols in that module. */
8680
8681 if (pdi->has_children)
8682 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8683 }
8684
8685 /* Read a partial die corresponding to a subprogram or an inlined
8686 subprogram and create a partial symbol for that subprogram.
8687 When the CU language allows it, this routine also defines a partial
8688 symbol for each nested subprogram that this subprogram contains.
8689 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8690 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8691
8692 PDI may also be a lexical block, in which case we simply search
8693 recursively for subprograms defined inside that lexical block.
8694 Again, this is only performed when the CU language allows this
8695 type of definitions. */
8696
8697 static void
8698 add_partial_subprogram (struct partial_die_info *pdi,
8699 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8700 int set_addrmap, struct dwarf2_cu *cu)
8701 {
8702 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8703 {
8704 if (pdi->has_pc_info)
8705 {
8706 if (pdi->lowpc < *lowpc)
8707 *lowpc = pdi->lowpc;
8708 if (pdi->highpc > *highpc)
8709 *highpc = pdi->highpc;
8710 if (set_addrmap)
8711 {
8712 struct objfile *objfile = cu->per_objfile->objfile;
8713 struct gdbarch *gdbarch = objfile->arch ();
8714 CORE_ADDR baseaddr;
8715 CORE_ADDR this_highpc;
8716 CORE_ADDR this_lowpc;
8717
8718 baseaddr = objfile->text_section_offset ();
8719 this_lowpc
8720 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8721 pdi->lowpc + baseaddr)
8722 - baseaddr);
8723 this_highpc
8724 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8725 pdi->highpc + baseaddr)
8726 - baseaddr);
8727 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8728 this_lowpc, this_highpc - 1,
8729 cu->per_cu->v.psymtab);
8730 }
8731 }
8732
8733 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8734 {
8735 if (!pdi->is_declaration)
8736 /* Ignore subprogram DIEs that do not have a name, they are
8737 illegal. Do not emit a complaint at this point, we will
8738 do so when we convert this psymtab into a symtab. */
8739 if (pdi->name (cu))
8740 add_partial_symbol (pdi, cu);
8741 }
8742 }
8743
8744 if (! pdi->has_children)
8745 return;
8746
8747 if (cu->language == language_ada || cu->language == language_fortran)
8748 {
8749 pdi = pdi->die_child;
8750 while (pdi != NULL)
8751 {
8752 pdi->fixup (cu);
8753 if (pdi->tag == DW_TAG_subprogram
8754 || pdi->tag == DW_TAG_inlined_subroutine
8755 || pdi->tag == DW_TAG_lexical_block)
8756 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8757 pdi = pdi->die_sibling;
8758 }
8759 }
8760 }
8761
8762 /* Read a partial die corresponding to an enumeration type. */
8763
8764 static void
8765 add_partial_enumeration (struct partial_die_info *enum_pdi,
8766 struct dwarf2_cu *cu)
8767 {
8768 struct partial_die_info *pdi;
8769
8770 if (enum_pdi->name (cu) != NULL)
8771 add_partial_symbol (enum_pdi, cu);
8772
8773 pdi = enum_pdi->die_child;
8774 while (pdi)
8775 {
8776 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8777 complaint (_("malformed enumerator DIE ignored"));
8778 else
8779 add_partial_symbol (pdi, cu);
8780 pdi = pdi->die_sibling;
8781 }
8782 }
8783
8784 /* Return the initial uleb128 in the die at INFO_PTR. */
8785
8786 static unsigned int
8787 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8788 {
8789 unsigned int bytes_read;
8790
8791 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8792 }
8793
8794 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8795 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8796
8797 Return the corresponding abbrev, or NULL if the number is zero (indicating
8798 an empty DIE). In either case *BYTES_READ will be set to the length of
8799 the initial number. */
8800
8801 static struct abbrev_info *
8802 peek_die_abbrev (const die_reader_specs &reader,
8803 const gdb_byte *info_ptr, unsigned int *bytes_read)
8804 {
8805 dwarf2_cu *cu = reader.cu;
8806 bfd *abfd = cu->per_objfile->objfile->obfd;
8807 unsigned int abbrev_number
8808 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8809
8810 if (abbrev_number == 0)
8811 return NULL;
8812
8813 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8814 if (!abbrev)
8815 {
8816 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8817 " at offset %s [in module %s]"),
8818 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8819 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8820 }
8821
8822 return abbrev;
8823 }
8824
8825 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8826 Returns a pointer to the end of a series of DIEs, terminated by an empty
8827 DIE. Any children of the skipped DIEs will also be skipped. */
8828
8829 static const gdb_byte *
8830 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8831 {
8832 while (1)
8833 {
8834 unsigned int bytes_read;
8835 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8836
8837 if (abbrev == NULL)
8838 return info_ptr + bytes_read;
8839 else
8840 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8841 }
8842 }
8843
8844 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8845 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8846 abbrev corresponding to that skipped uleb128 should be passed in
8847 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8848 children. */
8849
8850 static const gdb_byte *
8851 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8852 struct abbrev_info *abbrev)
8853 {
8854 unsigned int bytes_read;
8855 struct attribute attr;
8856 bfd *abfd = reader->abfd;
8857 struct dwarf2_cu *cu = reader->cu;
8858 const gdb_byte *buffer = reader->buffer;
8859 const gdb_byte *buffer_end = reader->buffer_end;
8860 unsigned int form, i;
8861
8862 for (i = 0; i < abbrev->num_attrs; i++)
8863 {
8864 /* The only abbrev we care about is DW_AT_sibling. */
8865 if (abbrev->attrs[i].name == DW_AT_sibling)
8866 {
8867 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8868 if (attr.form == DW_FORM_ref_addr)
8869 complaint (_("ignoring absolute DW_AT_sibling"));
8870 else
8871 {
8872 sect_offset off = attr.get_ref_die_offset ();
8873 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8874
8875 if (sibling_ptr < info_ptr)
8876 complaint (_("DW_AT_sibling points backwards"));
8877 else if (sibling_ptr > reader->buffer_end)
8878 reader->die_section->overflow_complaint ();
8879 else
8880 return sibling_ptr;
8881 }
8882 }
8883
8884 /* If it isn't DW_AT_sibling, skip this attribute. */
8885 form = abbrev->attrs[i].form;
8886 skip_attribute:
8887 switch (form)
8888 {
8889 case DW_FORM_ref_addr:
8890 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8891 and later it is offset sized. */
8892 if (cu->header.version == 2)
8893 info_ptr += cu->header.addr_size;
8894 else
8895 info_ptr += cu->header.offset_size;
8896 break;
8897 case DW_FORM_GNU_ref_alt:
8898 info_ptr += cu->header.offset_size;
8899 break;
8900 case DW_FORM_addr:
8901 info_ptr += cu->header.addr_size;
8902 break;
8903 case DW_FORM_data1:
8904 case DW_FORM_ref1:
8905 case DW_FORM_flag:
8906 case DW_FORM_strx1:
8907 info_ptr += 1;
8908 break;
8909 case DW_FORM_flag_present:
8910 case DW_FORM_implicit_const:
8911 break;
8912 case DW_FORM_data2:
8913 case DW_FORM_ref2:
8914 case DW_FORM_strx2:
8915 info_ptr += 2;
8916 break;
8917 case DW_FORM_strx3:
8918 info_ptr += 3;
8919 break;
8920 case DW_FORM_data4:
8921 case DW_FORM_ref4:
8922 case DW_FORM_strx4:
8923 info_ptr += 4;
8924 break;
8925 case DW_FORM_data8:
8926 case DW_FORM_ref8:
8927 case DW_FORM_ref_sig8:
8928 info_ptr += 8;
8929 break;
8930 case DW_FORM_data16:
8931 info_ptr += 16;
8932 break;
8933 case DW_FORM_string:
8934 read_direct_string (abfd, info_ptr, &bytes_read);
8935 info_ptr += bytes_read;
8936 break;
8937 case DW_FORM_sec_offset:
8938 case DW_FORM_strp:
8939 case DW_FORM_GNU_strp_alt:
8940 info_ptr += cu->header.offset_size;
8941 break;
8942 case DW_FORM_exprloc:
8943 case DW_FORM_block:
8944 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8945 info_ptr += bytes_read;
8946 break;
8947 case DW_FORM_block1:
8948 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8949 break;
8950 case DW_FORM_block2:
8951 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8952 break;
8953 case DW_FORM_block4:
8954 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8955 break;
8956 case DW_FORM_addrx:
8957 case DW_FORM_strx:
8958 case DW_FORM_sdata:
8959 case DW_FORM_udata:
8960 case DW_FORM_ref_udata:
8961 case DW_FORM_GNU_addr_index:
8962 case DW_FORM_GNU_str_index:
8963 case DW_FORM_rnglistx:
8964 case DW_FORM_loclistx:
8965 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8966 break;
8967 case DW_FORM_indirect:
8968 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8969 info_ptr += bytes_read;
8970 /* We need to continue parsing from here, so just go back to
8971 the top. */
8972 goto skip_attribute;
8973
8974 default:
8975 error (_("Dwarf Error: Cannot handle %s "
8976 "in DWARF reader [in module %s]"),
8977 dwarf_form_name (form),
8978 bfd_get_filename (abfd));
8979 }
8980 }
8981
8982 if (abbrev->has_children)
8983 return skip_children (reader, info_ptr);
8984 else
8985 return info_ptr;
8986 }
8987
8988 /* Locate ORIG_PDI's sibling.
8989 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8990
8991 static const gdb_byte *
8992 locate_pdi_sibling (const struct die_reader_specs *reader,
8993 struct partial_die_info *orig_pdi,
8994 const gdb_byte *info_ptr)
8995 {
8996 /* Do we know the sibling already? */
8997
8998 if (orig_pdi->sibling)
8999 return orig_pdi->sibling;
9000
9001 /* Are there any children to deal with? */
9002
9003 if (!orig_pdi->has_children)
9004 return info_ptr;
9005
9006 /* Skip the children the long way. */
9007
9008 return skip_children (reader, info_ptr);
9009 }
9010
9011 /* Expand this partial symbol table into a full symbol table. SELF is
9012 not NULL. */
9013
9014 void
9015 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9016 {
9017 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9018
9019 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
9020
9021 /* If this psymtab is constructed from a debug-only objfile, the
9022 has_section_at_zero flag will not necessarily be correct. We
9023 can get the correct value for this flag by looking at the data
9024 associated with the (presumably stripped) associated objfile. */
9025 if (objfile->separate_debug_objfile_backlink)
9026 {
9027 dwarf2_per_objfile *per_objfile_backlink
9028 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9029
9030 per_objfile->per_bfd->has_section_at_zero
9031 = per_objfile_backlink->per_bfd->has_section_at_zero;
9032 }
9033
9034 expand_psymtab (objfile);
9035
9036 process_cu_includes (per_objfile);
9037 }
9038 \f
9039 /* Reading in full CUs. */
9040
9041 /* Add PER_CU to the queue. */
9042
9043 static void
9044 queue_comp_unit (dwarf2_per_cu_data *per_cu,
9045 dwarf2_per_objfile *per_objfile,
9046 enum language pretend_language)
9047 {
9048 per_cu->queued = 1;
9049 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
9050 }
9051
9052 /* If PER_CU is not yet queued, add it to the queue.
9053 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9054 dependency.
9055 The result is non-zero if PER_CU was queued, otherwise the result is zero
9056 meaning either PER_CU is already queued or it is already loaded.
9057
9058 N.B. There is an invariant here that if a CU is queued then it is loaded.
9059 The caller is required to load PER_CU if we return non-zero. */
9060
9061 static int
9062 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9063 dwarf2_per_cu_data *per_cu,
9064 dwarf2_per_objfile *per_objfile,
9065 enum language pretend_language)
9066 {
9067 /* We may arrive here during partial symbol reading, if we need full
9068 DIEs to process an unusual case (e.g. template arguments). Do
9069 not queue PER_CU, just tell our caller to load its DIEs. */
9070 if (per_cu->per_bfd->reading_partial_symbols)
9071 {
9072 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9073
9074 if (cu == NULL || cu->dies == NULL)
9075 return 1;
9076 return 0;
9077 }
9078
9079 /* Mark the dependence relation so that we don't flush PER_CU
9080 too early. */
9081 if (dependent_cu != NULL)
9082 dwarf2_add_dependence (dependent_cu, per_cu);
9083
9084 /* If it's already on the queue, we have nothing to do. */
9085 if (per_cu->queued)
9086 return 0;
9087
9088 /* If the compilation unit is already loaded, just mark it as
9089 used. */
9090 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9091 if (cu != nullptr)
9092 {
9093 cu->last_used = 0;
9094 return 0;
9095 }
9096
9097 /* Add it to the queue. */
9098 queue_comp_unit (per_cu, per_objfile, pretend_language);
9099
9100 return 1;
9101 }
9102
9103 /* Process the queue. */
9104
9105 static void
9106 process_queue (dwarf2_per_objfile *per_objfile)
9107 {
9108 if (dwarf_read_debug)
9109 {
9110 fprintf_unfiltered (gdb_stdlog,
9111 "Expanding one or more symtabs of objfile %s ...\n",
9112 objfile_name (per_objfile->objfile));
9113 }
9114
9115 /* The queue starts out with one item, but following a DIE reference
9116 may load a new CU, adding it to the end of the queue. */
9117 while (!per_objfile->per_bfd->queue.empty ())
9118 {
9119 dwarf2_queue_item &item = per_objfile->per_bfd->queue.front ();
9120 dwarf2_per_cu_data *per_cu = item.per_cu;
9121
9122 if (!per_objfile->symtab_set_p (per_cu))
9123 {
9124 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9125
9126 /* Skip dummy CUs. */
9127 if (cu != nullptr)
9128 {
9129 unsigned int debug_print_threshold;
9130 char buf[100];
9131
9132 if (per_cu->is_debug_types)
9133 {
9134 struct signatured_type *sig_type =
9135 (struct signatured_type *) per_cu;
9136
9137 sprintf (buf, "TU %s at offset %s",
9138 hex_string (sig_type->signature),
9139 sect_offset_str (per_cu->sect_off));
9140 /* There can be 100s of TUs.
9141 Only print them in verbose mode. */
9142 debug_print_threshold = 2;
9143 }
9144 else
9145 {
9146 sprintf (buf, "CU at offset %s",
9147 sect_offset_str (per_cu->sect_off));
9148 debug_print_threshold = 1;
9149 }
9150
9151 if (dwarf_read_debug >= debug_print_threshold)
9152 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9153
9154 if (per_cu->is_debug_types)
9155 process_full_type_unit (cu, item.pretend_language);
9156 else
9157 process_full_comp_unit (cu, item.pretend_language);
9158
9159 if (dwarf_read_debug >= debug_print_threshold)
9160 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9161 }
9162 }
9163
9164 per_cu->queued = 0;
9165 per_objfile->per_bfd->queue.pop ();
9166 }
9167
9168 if (dwarf_read_debug)
9169 {
9170 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9171 objfile_name (per_objfile->objfile));
9172 }
9173 }
9174
9175 /* Read in full symbols for PST, and anything it depends on. */
9176
9177 void
9178 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9179 {
9180 gdb_assert (!readin_p (objfile));
9181
9182 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9183 free_cached_comp_units freer (per_objfile);
9184 expand_dependencies (objfile);
9185
9186 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9187 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9188 }
9189
9190 /* See psympriv.h. */
9191
9192 bool
9193 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9194 {
9195 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9196 return per_objfile->symtab_set_p (per_cu_data);
9197 }
9198
9199 /* See psympriv.h. */
9200
9201 compunit_symtab *
9202 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9203 {
9204 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9205 return per_objfile->get_symtab (per_cu_data);
9206 }
9207
9208 /* Trivial hash function for die_info: the hash value of a DIE
9209 is its offset in .debug_info for this objfile. */
9210
9211 static hashval_t
9212 die_hash (const void *item)
9213 {
9214 const struct die_info *die = (const struct die_info *) item;
9215
9216 return to_underlying (die->sect_off);
9217 }
9218
9219 /* Trivial comparison function for die_info structures: two DIEs
9220 are equal if they have the same offset. */
9221
9222 static int
9223 die_eq (const void *item_lhs, const void *item_rhs)
9224 {
9225 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9226 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9227
9228 return die_lhs->sect_off == die_rhs->sect_off;
9229 }
9230
9231 /* Load the DIEs associated with PER_CU into memory.
9232
9233 In some cases, the caller, while reading partial symbols, will need to load
9234 the full symbols for the CU for some reason. It will already have a
9235 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9236 rather than creating a new one. */
9237
9238 static void
9239 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9240 dwarf2_per_objfile *per_objfile,
9241 dwarf2_cu *existing_cu,
9242 bool skip_partial,
9243 enum language pretend_language)
9244 {
9245 gdb_assert (! this_cu->is_debug_types);
9246
9247 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9248 if (reader.dummy_p)
9249 return;
9250
9251 struct dwarf2_cu *cu = reader.cu;
9252 const gdb_byte *info_ptr = reader.info_ptr;
9253
9254 gdb_assert (cu->die_hash == NULL);
9255 cu->die_hash =
9256 htab_create_alloc_ex (cu->header.length / 12,
9257 die_hash,
9258 die_eq,
9259 NULL,
9260 &cu->comp_unit_obstack,
9261 hashtab_obstack_allocate,
9262 dummy_obstack_deallocate);
9263
9264 if (reader.comp_unit_die->has_children)
9265 reader.comp_unit_die->child
9266 = read_die_and_siblings (&reader, reader.info_ptr,
9267 &info_ptr, reader.comp_unit_die);
9268 cu->dies = reader.comp_unit_die;
9269 /* comp_unit_die is not stored in die_hash, no need. */
9270
9271 /* We try not to read any attributes in this function, because not
9272 all CUs needed for references have been loaded yet, and symbol
9273 table processing isn't initialized. But we have to set the CU language,
9274 or we won't be able to build types correctly.
9275 Similarly, if we do not read the producer, we can not apply
9276 producer-specific interpretation. */
9277 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9278
9279 reader.keep ();
9280 }
9281
9282 /* Add a DIE to the delayed physname list. */
9283
9284 static void
9285 add_to_method_list (struct type *type, int fnfield_index, int index,
9286 const char *name, struct die_info *die,
9287 struct dwarf2_cu *cu)
9288 {
9289 struct delayed_method_info mi;
9290 mi.type = type;
9291 mi.fnfield_index = fnfield_index;
9292 mi.index = index;
9293 mi.name = name;
9294 mi.die = die;
9295 cu->method_list.push_back (mi);
9296 }
9297
9298 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9299 "const" / "volatile". If so, decrements LEN by the length of the
9300 modifier and return true. Otherwise return false. */
9301
9302 template<size_t N>
9303 static bool
9304 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9305 {
9306 size_t mod_len = sizeof (mod) - 1;
9307 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9308 {
9309 len -= mod_len;
9310 return true;
9311 }
9312 return false;
9313 }
9314
9315 /* Compute the physnames of any methods on the CU's method list.
9316
9317 The computation of method physnames is delayed in order to avoid the
9318 (bad) condition that one of the method's formal parameters is of an as yet
9319 incomplete type. */
9320
9321 static void
9322 compute_delayed_physnames (struct dwarf2_cu *cu)
9323 {
9324 /* Only C++ delays computing physnames. */
9325 if (cu->method_list.empty ())
9326 return;
9327 gdb_assert (cu->language == language_cplus);
9328
9329 for (const delayed_method_info &mi : cu->method_list)
9330 {
9331 const char *physname;
9332 struct fn_fieldlist *fn_flp
9333 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9334 physname = dwarf2_physname (mi.name, mi.die, cu);
9335 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9336 = physname ? physname : "";
9337
9338 /* Since there's no tag to indicate whether a method is a
9339 const/volatile overload, extract that information out of the
9340 demangled name. */
9341 if (physname != NULL)
9342 {
9343 size_t len = strlen (physname);
9344
9345 while (1)
9346 {
9347 if (physname[len] == ')') /* shortcut */
9348 break;
9349 else if (check_modifier (physname, len, " const"))
9350 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9351 else if (check_modifier (physname, len, " volatile"))
9352 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9353 else
9354 break;
9355 }
9356 }
9357 }
9358
9359 /* The list is no longer needed. */
9360 cu->method_list.clear ();
9361 }
9362
9363 /* Go objects should be embedded in a DW_TAG_module DIE,
9364 and it's not clear if/how imported objects will appear.
9365 To keep Go support simple until that's worked out,
9366 go back through what we've read and create something usable.
9367 We could do this while processing each DIE, and feels kinda cleaner,
9368 but that way is more invasive.
9369 This is to, for example, allow the user to type "p var" or "b main"
9370 without having to specify the package name, and allow lookups
9371 of module.object to work in contexts that use the expression
9372 parser. */
9373
9374 static void
9375 fixup_go_packaging (struct dwarf2_cu *cu)
9376 {
9377 gdb::unique_xmalloc_ptr<char> package_name;
9378 struct pending *list;
9379 int i;
9380
9381 for (list = *cu->get_builder ()->get_global_symbols ();
9382 list != NULL;
9383 list = list->next)
9384 {
9385 for (i = 0; i < list->nsyms; ++i)
9386 {
9387 struct symbol *sym = list->symbol[i];
9388
9389 if (sym->language () == language_go
9390 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9391 {
9392 gdb::unique_xmalloc_ptr<char> this_package_name
9393 (go_symbol_package_name (sym));
9394
9395 if (this_package_name == NULL)
9396 continue;
9397 if (package_name == NULL)
9398 package_name = std::move (this_package_name);
9399 else
9400 {
9401 struct objfile *objfile = cu->per_objfile->objfile;
9402 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9403 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9404 (symbol_symtab (sym) != NULL
9405 ? symtab_to_filename_for_display
9406 (symbol_symtab (sym))
9407 : objfile_name (objfile)),
9408 this_package_name.get (), package_name.get ());
9409 }
9410 }
9411 }
9412 }
9413
9414 if (package_name != NULL)
9415 {
9416 struct objfile *objfile = cu->per_objfile->objfile;
9417 const char *saved_package_name = objfile->intern (package_name.get ());
9418 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9419 saved_package_name);
9420 struct symbol *sym;
9421
9422 sym = new (&objfile->objfile_obstack) symbol;
9423 sym->set_language (language_go, &objfile->objfile_obstack);
9424 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9425 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9426 e.g., "main" finds the "main" module and not C's main(). */
9427 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9428 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9429 SYMBOL_TYPE (sym) = type;
9430
9431 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9432 }
9433 }
9434
9435 /* Allocate a fully-qualified name consisting of the two parts on the
9436 obstack. */
9437
9438 static const char *
9439 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9440 {
9441 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9442 }
9443
9444 /* A helper that allocates a variant part to attach to a Rust enum
9445 type. OBSTACK is where the results should be allocated. TYPE is
9446 the type we're processing. DISCRIMINANT_INDEX is the index of the
9447 discriminant. It must be the index of one of the fields of TYPE,
9448 or -1 to mean there is no discriminant (univariant enum).
9449 DEFAULT_INDEX is the index of the default field; or -1 if there is
9450 no default. RANGES is indexed by "effective" field number (the
9451 field index, but omitting the discriminant and default fields) and
9452 must hold the discriminant values used by the variants. Note that
9453 RANGES must have a lifetime at least as long as OBSTACK -- either
9454 already allocated on it, or static. */
9455
9456 static void
9457 alloc_rust_variant (struct obstack *obstack, struct type *type,
9458 int discriminant_index, int default_index,
9459 gdb::array_view<discriminant_range> ranges)
9460 {
9461 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9462 gdb_assert (discriminant_index == -1
9463 || (discriminant_index >= 0
9464 && discriminant_index < type->num_fields ()));
9465 gdb_assert (default_index == -1
9466 || (default_index >= 0 && default_index < type->num_fields ()));
9467
9468 /* We have one variant for each non-discriminant field. */
9469 int n_variants = type->num_fields ();
9470 if (discriminant_index != -1)
9471 --n_variants;
9472
9473 variant *variants = new (obstack) variant[n_variants];
9474 int var_idx = 0;
9475 int range_idx = 0;
9476 for (int i = 0; i < type->num_fields (); ++i)
9477 {
9478 if (i == discriminant_index)
9479 continue;
9480
9481 variants[var_idx].first_field = i;
9482 variants[var_idx].last_field = i + 1;
9483
9484 /* The default field does not need a range, but other fields do.
9485 We skipped the discriminant above. */
9486 if (i != default_index)
9487 {
9488 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9489 ++range_idx;
9490 }
9491
9492 ++var_idx;
9493 }
9494
9495 gdb_assert (range_idx == ranges.size ());
9496 gdb_assert (var_idx == n_variants);
9497
9498 variant_part *part = new (obstack) variant_part;
9499 part->discriminant_index = discriminant_index;
9500 /* If there is no discriminant, then whether it is signed is of no
9501 consequence. */
9502 part->is_unsigned
9503 = (discriminant_index == -1
9504 ? false
9505 : type->field (discriminant_index).type ()->is_unsigned ());
9506 part->variants = gdb::array_view<variant> (variants, n_variants);
9507
9508 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9509 gdb::array_view<variant_part> *prop_value
9510 = new (storage) gdb::array_view<variant_part> (part, 1);
9511
9512 struct dynamic_prop prop;
9513 prop.set_variant_parts (prop_value);
9514
9515 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9516 }
9517
9518 /* Some versions of rustc emitted enums in an unusual way.
9519
9520 Ordinary enums were emitted as unions. The first element of each
9521 structure in the union was named "RUST$ENUM$DISR". This element
9522 held the discriminant.
9523
9524 These versions of Rust also implemented the "non-zero"
9525 optimization. When the enum had two values, and one is empty and
9526 the other holds a pointer that cannot be zero, the pointer is used
9527 as the discriminant, with a zero value meaning the empty variant.
9528 Here, the union's first member is of the form
9529 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9530 where the fieldnos are the indices of the fields that should be
9531 traversed in order to find the field (which may be several fields deep)
9532 and the variantname is the name of the variant of the case when the
9533 field is zero.
9534
9535 This function recognizes whether TYPE is of one of these forms,
9536 and, if so, smashes it to be a variant type. */
9537
9538 static void
9539 quirk_rust_enum (struct type *type, struct objfile *objfile)
9540 {
9541 gdb_assert (type->code () == TYPE_CODE_UNION);
9542
9543 /* We don't need to deal with empty enums. */
9544 if (type->num_fields () == 0)
9545 return;
9546
9547 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9548 if (type->num_fields () == 1
9549 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9550 {
9551 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9552
9553 /* Decode the field name to find the offset of the
9554 discriminant. */
9555 ULONGEST bit_offset = 0;
9556 struct type *field_type = type->field (0).type ();
9557 while (name[0] >= '0' && name[0] <= '9')
9558 {
9559 char *tail;
9560 unsigned long index = strtoul (name, &tail, 10);
9561 name = tail;
9562 if (*name != '$'
9563 || index >= field_type->num_fields ()
9564 || (TYPE_FIELD_LOC_KIND (field_type, index)
9565 != FIELD_LOC_KIND_BITPOS))
9566 {
9567 complaint (_("Could not parse Rust enum encoding string \"%s\""
9568 "[in module %s]"),
9569 TYPE_FIELD_NAME (type, 0),
9570 objfile_name (objfile));
9571 return;
9572 }
9573 ++name;
9574
9575 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9576 field_type = field_type->field (index).type ();
9577 }
9578
9579 /* Smash this type to be a structure type. We have to do this
9580 because the type has already been recorded. */
9581 type->set_code (TYPE_CODE_STRUCT);
9582 type->set_num_fields (3);
9583 /* Save the field we care about. */
9584 struct field saved_field = type->field (0);
9585 type->set_fields
9586 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9587
9588 /* Put the discriminant at index 0. */
9589 type->field (0).set_type (field_type);
9590 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9591 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9592 SET_FIELD_BITPOS (type->field (0), bit_offset);
9593
9594 /* The order of fields doesn't really matter, so put the real
9595 field at index 1 and the data-less field at index 2. */
9596 type->field (1) = saved_field;
9597 TYPE_FIELD_NAME (type, 1)
9598 = rust_last_path_segment (type->field (1).type ()->name ());
9599 type->field (1).type ()->set_name
9600 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9601 TYPE_FIELD_NAME (type, 1)));
9602
9603 const char *dataless_name
9604 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9605 name);
9606 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9607 dataless_name);
9608 type->field (2).set_type (dataless_type);
9609 /* NAME points into the original discriminant name, which
9610 already has the correct lifetime. */
9611 TYPE_FIELD_NAME (type, 2) = name;
9612 SET_FIELD_BITPOS (type->field (2), 0);
9613
9614 /* Indicate that this is a variant type. */
9615 static discriminant_range ranges[1] = { { 0, 0 } };
9616 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9617 }
9618 /* A union with a single anonymous field is probably an old-style
9619 univariant enum. */
9620 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9621 {
9622 /* Smash this type to be a structure type. We have to do this
9623 because the type has already been recorded. */
9624 type->set_code (TYPE_CODE_STRUCT);
9625
9626 struct type *field_type = type->field (0).type ();
9627 const char *variant_name
9628 = rust_last_path_segment (field_type->name ());
9629 TYPE_FIELD_NAME (type, 0) = variant_name;
9630 field_type->set_name
9631 (rust_fully_qualify (&objfile->objfile_obstack,
9632 type->name (), variant_name));
9633
9634 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9635 }
9636 else
9637 {
9638 struct type *disr_type = nullptr;
9639 for (int i = 0; i < type->num_fields (); ++i)
9640 {
9641 disr_type = type->field (i).type ();
9642
9643 if (disr_type->code () != TYPE_CODE_STRUCT)
9644 {
9645 /* All fields of a true enum will be structs. */
9646 return;
9647 }
9648 else if (disr_type->num_fields () == 0)
9649 {
9650 /* Could be data-less variant, so keep going. */
9651 disr_type = nullptr;
9652 }
9653 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9654 "RUST$ENUM$DISR") != 0)
9655 {
9656 /* Not a Rust enum. */
9657 return;
9658 }
9659 else
9660 {
9661 /* Found one. */
9662 break;
9663 }
9664 }
9665
9666 /* If we got here without a discriminant, then it's probably
9667 just a union. */
9668 if (disr_type == nullptr)
9669 return;
9670
9671 /* Smash this type to be a structure type. We have to do this
9672 because the type has already been recorded. */
9673 type->set_code (TYPE_CODE_STRUCT);
9674
9675 /* Make space for the discriminant field. */
9676 struct field *disr_field = &disr_type->field (0);
9677 field *new_fields
9678 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9679 * sizeof (struct field)));
9680 memcpy (new_fields + 1, type->fields (),
9681 type->num_fields () * sizeof (struct field));
9682 type->set_fields (new_fields);
9683 type->set_num_fields (type->num_fields () + 1);
9684
9685 /* Install the discriminant at index 0 in the union. */
9686 type->field (0) = *disr_field;
9687 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9688 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9689
9690 /* We need a way to find the correct discriminant given a
9691 variant name. For convenience we build a map here. */
9692 struct type *enum_type = disr_field->type ();
9693 std::unordered_map<std::string, ULONGEST> discriminant_map;
9694 for (int i = 0; i < enum_type->num_fields (); ++i)
9695 {
9696 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9697 {
9698 const char *name
9699 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9700 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9701 }
9702 }
9703
9704 int n_fields = type->num_fields ();
9705 /* We don't need a range entry for the discriminant, but we do
9706 need one for every other field, as there is no default
9707 variant. */
9708 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9709 discriminant_range,
9710 n_fields - 1);
9711 /* Skip the discriminant here. */
9712 for (int i = 1; i < n_fields; ++i)
9713 {
9714 /* Find the final word in the name of this variant's type.
9715 That name can be used to look up the correct
9716 discriminant. */
9717 const char *variant_name
9718 = rust_last_path_segment (type->field (i).type ()->name ());
9719
9720 auto iter = discriminant_map.find (variant_name);
9721 if (iter != discriminant_map.end ())
9722 {
9723 ranges[i - 1].low = iter->second;
9724 ranges[i - 1].high = iter->second;
9725 }
9726
9727 /* In Rust, each element should have the size of the
9728 enclosing enum. */
9729 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9730
9731 /* Remove the discriminant field, if it exists. */
9732 struct type *sub_type = type->field (i).type ();
9733 if (sub_type->num_fields () > 0)
9734 {
9735 sub_type->set_num_fields (sub_type->num_fields () - 1);
9736 sub_type->set_fields (sub_type->fields () + 1);
9737 }
9738 TYPE_FIELD_NAME (type, i) = variant_name;
9739 sub_type->set_name
9740 (rust_fully_qualify (&objfile->objfile_obstack,
9741 type->name (), variant_name));
9742 }
9743
9744 /* Indicate that this is a variant type. */
9745 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9746 gdb::array_view<discriminant_range> (ranges,
9747 n_fields - 1));
9748 }
9749 }
9750
9751 /* Rewrite some Rust unions to be structures with variants parts. */
9752
9753 static void
9754 rust_union_quirks (struct dwarf2_cu *cu)
9755 {
9756 gdb_assert (cu->language == language_rust);
9757 for (type *type_ : cu->rust_unions)
9758 quirk_rust_enum (type_, cu->per_objfile->objfile);
9759 /* We don't need this any more. */
9760 cu->rust_unions.clear ();
9761 }
9762
9763 /* See read.h. */
9764
9765 type_unit_group_unshareable *
9766 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9767 {
9768 auto iter = this->m_type_units.find (tu_group);
9769 if (iter != this->m_type_units.end ())
9770 return iter->second.get ();
9771
9772 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9773 type_unit_group_unshareable *result = uniq.get ();
9774 this->m_type_units[tu_group] = std::move (uniq);
9775 return result;
9776 }
9777
9778 struct type *
9779 dwarf2_per_objfile::get_type_for_signatured_type
9780 (signatured_type *sig_type) const
9781 {
9782 auto iter = this->m_type_map.find (sig_type);
9783 if (iter == this->m_type_map.end ())
9784 return nullptr;
9785
9786 return iter->second;
9787 }
9788
9789 void dwarf2_per_objfile::set_type_for_signatured_type
9790 (signatured_type *sig_type, struct type *type)
9791 {
9792 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9793
9794 this->m_type_map[sig_type] = type;
9795 }
9796
9797 /* A helper function for computing the list of all symbol tables
9798 included by PER_CU. */
9799
9800 static void
9801 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9802 htab_t all_children, htab_t all_type_symtabs,
9803 dwarf2_per_cu_data *per_cu,
9804 dwarf2_per_objfile *per_objfile,
9805 struct compunit_symtab *immediate_parent)
9806 {
9807 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9808 if (*slot != NULL)
9809 {
9810 /* This inclusion and its children have been processed. */
9811 return;
9812 }
9813
9814 *slot = per_cu;
9815
9816 /* Only add a CU if it has a symbol table. */
9817 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9818 if (cust != NULL)
9819 {
9820 /* If this is a type unit only add its symbol table if we haven't
9821 seen it yet (type unit per_cu's can share symtabs). */
9822 if (per_cu->is_debug_types)
9823 {
9824 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9825 if (*slot == NULL)
9826 {
9827 *slot = cust;
9828 result->push_back (cust);
9829 if (cust->user == NULL)
9830 cust->user = immediate_parent;
9831 }
9832 }
9833 else
9834 {
9835 result->push_back (cust);
9836 if (cust->user == NULL)
9837 cust->user = immediate_parent;
9838 }
9839 }
9840
9841 if (!per_cu->imported_symtabs_empty ())
9842 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9843 {
9844 recursively_compute_inclusions (result, all_children,
9845 all_type_symtabs, ptr, per_objfile,
9846 cust);
9847 }
9848 }
9849
9850 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9851 PER_CU. */
9852
9853 static void
9854 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9855 dwarf2_per_objfile *per_objfile)
9856 {
9857 gdb_assert (! per_cu->is_debug_types);
9858
9859 if (!per_cu->imported_symtabs_empty ())
9860 {
9861 int len;
9862 std::vector<compunit_symtab *> result_symtabs;
9863 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9864
9865 /* If we don't have a symtab, we can just skip this case. */
9866 if (cust == NULL)
9867 return;
9868
9869 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9870 htab_eq_pointer,
9871 NULL, xcalloc, xfree));
9872 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9873 htab_eq_pointer,
9874 NULL, xcalloc, xfree));
9875
9876 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9877 {
9878 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9879 all_type_symtabs.get (), ptr,
9880 per_objfile, cust);
9881 }
9882
9883 /* Now we have a transitive closure of all the included symtabs. */
9884 len = result_symtabs.size ();
9885 cust->includes
9886 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9887 struct compunit_symtab *, len + 1);
9888 memcpy (cust->includes, result_symtabs.data (),
9889 len * sizeof (compunit_symtab *));
9890 cust->includes[len] = NULL;
9891 }
9892 }
9893
9894 /* Compute the 'includes' field for the symtabs of all the CUs we just
9895 read. */
9896
9897 static void
9898 process_cu_includes (dwarf2_per_objfile *per_objfile)
9899 {
9900 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9901 {
9902 if (! iter->is_debug_types)
9903 compute_compunit_symtab_includes (iter, per_objfile);
9904 }
9905
9906 per_objfile->per_bfd->just_read_cus.clear ();
9907 }
9908
9909 /* Generate full symbol information for CU, whose DIEs have
9910 already been loaded into memory. */
9911
9912 static void
9913 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9914 {
9915 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9916 struct objfile *objfile = per_objfile->objfile;
9917 struct gdbarch *gdbarch = objfile->arch ();
9918 CORE_ADDR lowpc, highpc;
9919 struct compunit_symtab *cust;
9920 CORE_ADDR baseaddr;
9921 struct block *static_block;
9922 CORE_ADDR addr;
9923
9924 baseaddr = objfile->text_section_offset ();
9925
9926 /* Clear the list here in case something was left over. */
9927 cu->method_list.clear ();
9928
9929 cu->language = pretend_language;
9930 cu->language_defn = language_def (cu->language);
9931
9932 dwarf2_find_base_address (cu->dies, cu);
9933
9934 /* Do line number decoding in read_file_scope () */
9935 process_die (cu->dies, cu);
9936
9937 /* For now fudge the Go package. */
9938 if (cu->language == language_go)
9939 fixup_go_packaging (cu);
9940
9941 /* Now that we have processed all the DIEs in the CU, all the types
9942 should be complete, and it should now be safe to compute all of the
9943 physnames. */
9944 compute_delayed_physnames (cu);
9945
9946 if (cu->language == language_rust)
9947 rust_union_quirks (cu);
9948
9949 /* Some compilers don't define a DW_AT_high_pc attribute for the
9950 compilation unit. If the DW_AT_high_pc is missing, synthesize
9951 it, by scanning the DIE's below the compilation unit. */
9952 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9953
9954 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9955 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9956
9957 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9958 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9959 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9960 addrmap to help ensure it has an accurate map of pc values belonging to
9961 this comp unit. */
9962 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9963
9964 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9965 SECT_OFF_TEXT (objfile),
9966 0);
9967
9968 if (cust != NULL)
9969 {
9970 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9971
9972 /* Set symtab language to language from DW_AT_language. If the
9973 compilation is from a C file generated by language preprocessors, do
9974 not set the language if it was already deduced by start_subfile. */
9975 if (!(cu->language == language_c
9976 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9977 COMPUNIT_FILETABS (cust)->language = cu->language;
9978
9979 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9980 produce DW_AT_location with location lists but it can be possibly
9981 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9982 there were bugs in prologue debug info, fixed later in GCC-4.5
9983 by "unwind info for epilogues" patch (which is not directly related).
9984
9985 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9986 needed, it would be wrong due to missing DW_AT_producer there.
9987
9988 Still one can confuse GDB by using non-standard GCC compilation
9989 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9990 */
9991 if (cu->has_loclist && gcc_4_minor >= 5)
9992 cust->locations_valid = 1;
9993
9994 if (gcc_4_minor >= 5)
9995 cust->epilogue_unwind_valid = 1;
9996
9997 cust->call_site_htab = cu->call_site_htab;
9998 }
9999
10000 per_objfile->set_symtab (cu->per_cu, cust);
10001
10002 /* Push it for inclusion processing later. */
10003 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
10004
10005 /* Not needed any more. */
10006 cu->reset_builder ();
10007 }
10008
10009 /* Generate full symbol information for type unit CU, whose DIEs have
10010 already been loaded into memory. */
10011
10012 static void
10013 process_full_type_unit (dwarf2_cu *cu,
10014 enum language pretend_language)
10015 {
10016 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10017 struct objfile *objfile = per_objfile->objfile;
10018 struct compunit_symtab *cust;
10019 struct signatured_type *sig_type;
10020
10021 gdb_assert (cu->per_cu->is_debug_types);
10022 sig_type = (struct signatured_type *) cu->per_cu;
10023
10024 /* Clear the list here in case something was left over. */
10025 cu->method_list.clear ();
10026
10027 cu->language = pretend_language;
10028 cu->language_defn = language_def (cu->language);
10029
10030 /* The symbol tables are set up in read_type_unit_scope. */
10031 process_die (cu->dies, cu);
10032
10033 /* For now fudge the Go package. */
10034 if (cu->language == language_go)
10035 fixup_go_packaging (cu);
10036
10037 /* Now that we have processed all the DIEs in the CU, all the types
10038 should be complete, and it should now be safe to compute all of the
10039 physnames. */
10040 compute_delayed_physnames (cu);
10041
10042 if (cu->language == language_rust)
10043 rust_union_quirks (cu);
10044
10045 /* TUs share symbol tables.
10046 If this is the first TU to use this symtab, complete the construction
10047 of it with end_expandable_symtab. Otherwise, complete the addition of
10048 this TU's symbols to the existing symtab. */
10049 type_unit_group_unshareable *tug_unshare =
10050 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
10051 if (tug_unshare->compunit_symtab == NULL)
10052 {
10053 buildsym_compunit *builder = cu->get_builder ();
10054 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10055 tug_unshare->compunit_symtab = cust;
10056
10057 if (cust != NULL)
10058 {
10059 /* Set symtab language to language from DW_AT_language. If the
10060 compilation is from a C file generated by language preprocessors,
10061 do not set the language if it was already deduced by
10062 start_subfile. */
10063 if (!(cu->language == language_c
10064 && COMPUNIT_FILETABS (cust)->language != language_c))
10065 COMPUNIT_FILETABS (cust)->language = cu->language;
10066 }
10067 }
10068 else
10069 {
10070 cu->get_builder ()->augment_type_symtab ();
10071 cust = tug_unshare->compunit_symtab;
10072 }
10073
10074 per_objfile->set_symtab (cu->per_cu, cust);
10075
10076 /* Not needed any more. */
10077 cu->reset_builder ();
10078 }
10079
10080 /* Process an imported unit DIE. */
10081
10082 static void
10083 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10084 {
10085 struct attribute *attr;
10086
10087 /* For now we don't handle imported units in type units. */
10088 if (cu->per_cu->is_debug_types)
10089 {
10090 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10091 " supported in type units [in module %s]"),
10092 objfile_name (cu->per_objfile->objfile));
10093 }
10094
10095 attr = dwarf2_attr (die, DW_AT_import, cu);
10096 if (attr != NULL)
10097 {
10098 sect_offset sect_off = attr->get_ref_die_offset ();
10099 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10100 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10101 dwarf2_per_cu_data *per_cu
10102 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
10103
10104 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10105 into another compilation unit, at root level. Regard this as a hint,
10106 and ignore it. */
10107 if (die->parent && die->parent->parent == NULL
10108 && per_cu->unit_type == DW_UT_compile
10109 && per_cu->lang == language_cplus)
10110 return;
10111
10112 /* If necessary, add it to the queue and load its DIEs. */
10113 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
10114 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10115 false, cu->language);
10116
10117 cu->per_cu->imported_symtabs_push (per_cu);
10118 }
10119 }
10120
10121 /* RAII object that represents a process_die scope: i.e.,
10122 starts/finishes processing a DIE. */
10123 class process_die_scope
10124 {
10125 public:
10126 process_die_scope (die_info *die, dwarf2_cu *cu)
10127 : m_die (die), m_cu (cu)
10128 {
10129 /* We should only be processing DIEs not already in process. */
10130 gdb_assert (!m_die->in_process);
10131 m_die->in_process = true;
10132 }
10133
10134 ~process_die_scope ()
10135 {
10136 m_die->in_process = false;
10137
10138 /* If we're done processing the DIE for the CU that owns the line
10139 header, we don't need the line header anymore. */
10140 if (m_cu->line_header_die_owner == m_die)
10141 {
10142 delete m_cu->line_header;
10143 m_cu->line_header = NULL;
10144 m_cu->line_header_die_owner = NULL;
10145 }
10146 }
10147
10148 private:
10149 die_info *m_die;
10150 dwarf2_cu *m_cu;
10151 };
10152
10153 /* Process a die and its children. */
10154
10155 static void
10156 process_die (struct die_info *die, struct dwarf2_cu *cu)
10157 {
10158 process_die_scope scope (die, cu);
10159
10160 switch (die->tag)
10161 {
10162 case DW_TAG_padding:
10163 break;
10164 case DW_TAG_compile_unit:
10165 case DW_TAG_partial_unit:
10166 read_file_scope (die, cu);
10167 break;
10168 case DW_TAG_type_unit:
10169 read_type_unit_scope (die, cu);
10170 break;
10171 case DW_TAG_subprogram:
10172 /* Nested subprograms in Fortran get a prefix. */
10173 if (cu->language == language_fortran
10174 && die->parent != NULL
10175 && die->parent->tag == DW_TAG_subprogram)
10176 cu->processing_has_namespace_info = true;
10177 /* Fall through. */
10178 case DW_TAG_inlined_subroutine:
10179 read_func_scope (die, cu);
10180 break;
10181 case DW_TAG_lexical_block:
10182 case DW_TAG_try_block:
10183 case DW_TAG_catch_block:
10184 read_lexical_block_scope (die, cu);
10185 break;
10186 case DW_TAG_call_site:
10187 case DW_TAG_GNU_call_site:
10188 read_call_site_scope (die, cu);
10189 break;
10190 case DW_TAG_class_type:
10191 case DW_TAG_interface_type:
10192 case DW_TAG_structure_type:
10193 case DW_TAG_union_type:
10194 process_structure_scope (die, cu);
10195 break;
10196 case DW_TAG_enumeration_type:
10197 process_enumeration_scope (die, cu);
10198 break;
10199
10200 /* These dies have a type, but processing them does not create
10201 a symbol or recurse to process the children. Therefore we can
10202 read them on-demand through read_type_die. */
10203 case DW_TAG_subroutine_type:
10204 case DW_TAG_set_type:
10205 case DW_TAG_array_type:
10206 case DW_TAG_pointer_type:
10207 case DW_TAG_ptr_to_member_type:
10208 case DW_TAG_reference_type:
10209 case DW_TAG_rvalue_reference_type:
10210 case DW_TAG_string_type:
10211 break;
10212
10213 case DW_TAG_base_type:
10214 case DW_TAG_subrange_type:
10215 case DW_TAG_typedef:
10216 /* Add a typedef symbol for the type definition, if it has a
10217 DW_AT_name. */
10218 new_symbol (die, read_type_die (die, cu), cu);
10219 break;
10220 case DW_TAG_common_block:
10221 read_common_block (die, cu);
10222 break;
10223 case DW_TAG_common_inclusion:
10224 break;
10225 case DW_TAG_namespace:
10226 cu->processing_has_namespace_info = true;
10227 read_namespace (die, cu);
10228 break;
10229 case DW_TAG_module:
10230 cu->processing_has_namespace_info = true;
10231 read_module (die, cu);
10232 break;
10233 case DW_TAG_imported_declaration:
10234 cu->processing_has_namespace_info = true;
10235 if (read_namespace_alias (die, cu))
10236 break;
10237 /* The declaration is not a global namespace alias. */
10238 /* Fall through. */
10239 case DW_TAG_imported_module:
10240 cu->processing_has_namespace_info = true;
10241 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10242 || cu->language != language_fortran))
10243 complaint (_("Tag '%s' has unexpected children"),
10244 dwarf_tag_name (die->tag));
10245 read_import_statement (die, cu);
10246 break;
10247
10248 case DW_TAG_imported_unit:
10249 process_imported_unit_die (die, cu);
10250 break;
10251
10252 case DW_TAG_variable:
10253 read_variable (die, cu);
10254 break;
10255
10256 default:
10257 new_symbol (die, NULL, cu);
10258 break;
10259 }
10260 }
10261 \f
10262 /* DWARF name computation. */
10263
10264 /* A helper function for dwarf2_compute_name which determines whether DIE
10265 needs to have the name of the scope prepended to the name listed in the
10266 die. */
10267
10268 static int
10269 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10270 {
10271 struct attribute *attr;
10272
10273 switch (die->tag)
10274 {
10275 case DW_TAG_namespace:
10276 case DW_TAG_typedef:
10277 case DW_TAG_class_type:
10278 case DW_TAG_interface_type:
10279 case DW_TAG_structure_type:
10280 case DW_TAG_union_type:
10281 case DW_TAG_enumeration_type:
10282 case DW_TAG_enumerator:
10283 case DW_TAG_subprogram:
10284 case DW_TAG_inlined_subroutine:
10285 case DW_TAG_member:
10286 case DW_TAG_imported_declaration:
10287 return 1;
10288
10289 case DW_TAG_variable:
10290 case DW_TAG_constant:
10291 /* We only need to prefix "globally" visible variables. These include
10292 any variable marked with DW_AT_external or any variable that
10293 lives in a namespace. [Variables in anonymous namespaces
10294 require prefixing, but they are not DW_AT_external.] */
10295
10296 if (dwarf2_attr (die, DW_AT_specification, cu))
10297 {
10298 struct dwarf2_cu *spec_cu = cu;
10299
10300 return die_needs_namespace (die_specification (die, &spec_cu),
10301 spec_cu);
10302 }
10303
10304 attr = dwarf2_attr (die, DW_AT_external, cu);
10305 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10306 && die->parent->tag != DW_TAG_module)
10307 return 0;
10308 /* A variable in a lexical block of some kind does not need a
10309 namespace, even though in C++ such variables may be external
10310 and have a mangled name. */
10311 if (die->parent->tag == DW_TAG_lexical_block
10312 || die->parent->tag == DW_TAG_try_block
10313 || die->parent->tag == DW_TAG_catch_block
10314 || die->parent->tag == DW_TAG_subprogram)
10315 return 0;
10316 return 1;
10317
10318 default:
10319 return 0;
10320 }
10321 }
10322
10323 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10324 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10325 defined for the given DIE. */
10326
10327 static struct attribute *
10328 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10329 {
10330 struct attribute *attr;
10331
10332 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10333 if (attr == NULL)
10334 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10335
10336 return attr;
10337 }
10338
10339 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10340 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10341 defined for the given DIE. */
10342
10343 static const char *
10344 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10345 {
10346 const char *linkage_name;
10347
10348 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10349 if (linkage_name == NULL)
10350 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10351
10352 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10353 See https://github.com/rust-lang/rust/issues/32925. */
10354 if (cu->language == language_rust && linkage_name != NULL
10355 && strchr (linkage_name, '{') != NULL)
10356 linkage_name = NULL;
10357
10358 return linkage_name;
10359 }
10360
10361 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10362 compute the physname for the object, which include a method's:
10363 - formal parameters (C++),
10364 - receiver type (Go),
10365
10366 The term "physname" is a bit confusing.
10367 For C++, for example, it is the demangled name.
10368 For Go, for example, it's the mangled name.
10369
10370 For Ada, return the DIE's linkage name rather than the fully qualified
10371 name. PHYSNAME is ignored..
10372
10373 The result is allocated on the objfile->per_bfd's obstack and
10374 canonicalized. */
10375
10376 static const char *
10377 dwarf2_compute_name (const char *name,
10378 struct die_info *die, struct dwarf2_cu *cu,
10379 int physname)
10380 {
10381 struct objfile *objfile = cu->per_objfile->objfile;
10382
10383 if (name == NULL)
10384 name = dwarf2_name (die, cu);
10385
10386 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10387 but otherwise compute it by typename_concat inside GDB.
10388 FIXME: Actually this is not really true, or at least not always true.
10389 It's all very confusing. compute_and_set_names doesn't try to demangle
10390 Fortran names because there is no mangling standard. So new_symbol
10391 will set the demangled name to the result of dwarf2_full_name, and it is
10392 the demangled name that GDB uses if it exists. */
10393 if (cu->language == language_ada
10394 || (cu->language == language_fortran && physname))
10395 {
10396 /* For Ada unit, we prefer the linkage name over the name, as
10397 the former contains the exported name, which the user expects
10398 to be able to reference. Ideally, we want the user to be able
10399 to reference this entity using either natural or linkage name,
10400 but we haven't started looking at this enhancement yet. */
10401 const char *linkage_name = dw2_linkage_name (die, cu);
10402
10403 if (linkage_name != NULL)
10404 return linkage_name;
10405 }
10406
10407 /* These are the only languages we know how to qualify names in. */
10408 if (name != NULL
10409 && (cu->language == language_cplus
10410 || cu->language == language_fortran || cu->language == language_d
10411 || cu->language == language_rust))
10412 {
10413 if (die_needs_namespace (die, cu))
10414 {
10415 const char *prefix;
10416 const char *canonical_name = NULL;
10417
10418 string_file buf;
10419
10420 prefix = determine_prefix (die, cu);
10421 if (*prefix != '\0')
10422 {
10423 gdb::unique_xmalloc_ptr<char> prefixed_name
10424 (typename_concat (NULL, prefix, name, physname, cu));
10425
10426 buf.puts (prefixed_name.get ());
10427 }
10428 else
10429 buf.puts (name);
10430
10431 /* Template parameters may be specified in the DIE's DW_AT_name, or
10432 as children with DW_TAG_template_type_param or
10433 DW_TAG_value_type_param. If the latter, add them to the name
10434 here. If the name already has template parameters, then
10435 skip this step; some versions of GCC emit both, and
10436 it is more efficient to use the pre-computed name.
10437
10438 Something to keep in mind about this process: it is very
10439 unlikely, or in some cases downright impossible, to produce
10440 something that will match the mangled name of a function.
10441 If the definition of the function has the same debug info,
10442 we should be able to match up with it anyway. But fallbacks
10443 using the minimal symbol, for instance to find a method
10444 implemented in a stripped copy of libstdc++, will not work.
10445 If we do not have debug info for the definition, we will have to
10446 match them up some other way.
10447
10448 When we do name matching there is a related problem with function
10449 templates; two instantiated function templates are allowed to
10450 differ only by their return types, which we do not add here. */
10451
10452 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10453 {
10454 struct attribute *attr;
10455 struct die_info *child;
10456 int first = 1;
10457
10458 die->building_fullname = 1;
10459
10460 for (child = die->child; child != NULL; child = child->sibling)
10461 {
10462 struct type *type;
10463 LONGEST value;
10464 const gdb_byte *bytes;
10465 struct dwarf2_locexpr_baton *baton;
10466 struct value *v;
10467
10468 if (child->tag != DW_TAG_template_type_param
10469 && child->tag != DW_TAG_template_value_param)
10470 continue;
10471
10472 if (first)
10473 {
10474 buf.puts ("<");
10475 first = 0;
10476 }
10477 else
10478 buf.puts (", ");
10479
10480 attr = dwarf2_attr (child, DW_AT_type, cu);
10481 if (attr == NULL)
10482 {
10483 complaint (_("template parameter missing DW_AT_type"));
10484 buf.puts ("UNKNOWN_TYPE");
10485 continue;
10486 }
10487 type = die_type (child, cu);
10488
10489 if (child->tag == DW_TAG_template_type_param)
10490 {
10491 c_print_type (type, "", &buf, -1, 0, cu->language,
10492 &type_print_raw_options);
10493 continue;
10494 }
10495
10496 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10497 if (attr == NULL)
10498 {
10499 complaint (_("template parameter missing "
10500 "DW_AT_const_value"));
10501 buf.puts ("UNKNOWN_VALUE");
10502 continue;
10503 }
10504
10505 dwarf2_const_value_attr (attr, type, name,
10506 &cu->comp_unit_obstack, cu,
10507 &value, &bytes, &baton);
10508
10509 if (type->has_no_signedness ())
10510 /* GDB prints characters as NUMBER 'CHAR'. If that's
10511 changed, this can use value_print instead. */
10512 c_printchar (value, type, &buf);
10513 else
10514 {
10515 struct value_print_options opts;
10516
10517 if (baton != NULL)
10518 v = dwarf2_evaluate_loc_desc (type, NULL,
10519 baton->data,
10520 baton->size,
10521 baton->per_cu,
10522 baton->per_objfile);
10523 else if (bytes != NULL)
10524 {
10525 v = allocate_value (type);
10526 memcpy (value_contents_writeable (v), bytes,
10527 TYPE_LENGTH (type));
10528 }
10529 else
10530 v = value_from_longest (type, value);
10531
10532 /* Specify decimal so that we do not depend on
10533 the radix. */
10534 get_formatted_print_options (&opts, 'd');
10535 opts.raw = 1;
10536 value_print (v, &buf, &opts);
10537 release_value (v);
10538 }
10539 }
10540
10541 die->building_fullname = 0;
10542
10543 if (!first)
10544 {
10545 /* Close the argument list, with a space if necessary
10546 (nested templates). */
10547 if (!buf.empty () && buf.string ().back () == '>')
10548 buf.puts (" >");
10549 else
10550 buf.puts (">");
10551 }
10552 }
10553
10554 /* For C++ methods, append formal parameter type
10555 information, if PHYSNAME. */
10556
10557 if (physname && die->tag == DW_TAG_subprogram
10558 && cu->language == language_cplus)
10559 {
10560 struct type *type = read_type_die (die, cu);
10561
10562 c_type_print_args (type, &buf, 1, cu->language,
10563 &type_print_raw_options);
10564
10565 if (cu->language == language_cplus)
10566 {
10567 /* Assume that an artificial first parameter is
10568 "this", but do not crash if it is not. RealView
10569 marks unnamed (and thus unused) parameters as
10570 artificial; there is no way to differentiate
10571 the two cases. */
10572 if (type->num_fields () > 0
10573 && TYPE_FIELD_ARTIFICIAL (type, 0)
10574 && type->field (0).type ()->code () == TYPE_CODE_PTR
10575 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10576 buf.puts (" const");
10577 }
10578 }
10579
10580 const std::string &intermediate_name = buf.string ();
10581
10582 if (cu->language == language_cplus)
10583 canonical_name
10584 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10585 objfile);
10586
10587 /* If we only computed INTERMEDIATE_NAME, or if
10588 INTERMEDIATE_NAME is already canonical, then we need to
10589 intern it. */
10590 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10591 name = objfile->intern (intermediate_name);
10592 else
10593 name = canonical_name;
10594 }
10595 }
10596
10597 return name;
10598 }
10599
10600 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10601 If scope qualifiers are appropriate they will be added. The result
10602 will be allocated on the storage_obstack, or NULL if the DIE does
10603 not have a name. NAME may either be from a previous call to
10604 dwarf2_name or NULL.
10605
10606 The output string will be canonicalized (if C++). */
10607
10608 static const char *
10609 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10610 {
10611 return dwarf2_compute_name (name, die, cu, 0);
10612 }
10613
10614 /* Construct a physname for the given DIE in CU. NAME may either be
10615 from a previous call to dwarf2_name or NULL. The result will be
10616 allocated on the objfile_objstack or NULL if the DIE does not have a
10617 name.
10618
10619 The output string will be canonicalized (if C++). */
10620
10621 static const char *
10622 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10623 {
10624 struct objfile *objfile = cu->per_objfile->objfile;
10625 const char *retval, *mangled = NULL, *canon = NULL;
10626 int need_copy = 1;
10627
10628 /* In this case dwarf2_compute_name is just a shortcut not building anything
10629 on its own. */
10630 if (!die_needs_namespace (die, cu))
10631 return dwarf2_compute_name (name, die, cu, 1);
10632
10633 if (cu->language != language_rust)
10634 mangled = dw2_linkage_name (die, cu);
10635
10636 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10637 has computed. */
10638 gdb::unique_xmalloc_ptr<char> demangled;
10639 if (mangled != NULL)
10640 {
10641
10642 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
10643 {
10644 /* Do nothing (do not demangle the symbol name). */
10645 }
10646 else
10647 {
10648 /* Use DMGL_RET_DROP for C++ template functions to suppress
10649 their return type. It is easier for GDB users to search
10650 for such functions as `name(params)' than `long name(params)'.
10651 In such case the minimal symbol names do not match the full
10652 symbol names but for template functions there is never a need
10653 to look up their definition from their declaration so
10654 the only disadvantage remains the minimal symbol variant
10655 `long name(params)' does not have the proper inferior type. */
10656 demangled.reset (gdb_demangle (mangled,
10657 (DMGL_PARAMS | DMGL_ANSI
10658 | DMGL_RET_DROP)));
10659 }
10660 if (demangled)
10661 canon = demangled.get ();
10662 else
10663 {
10664 canon = mangled;
10665 need_copy = 0;
10666 }
10667 }
10668
10669 if (canon == NULL || check_physname)
10670 {
10671 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10672
10673 if (canon != NULL && strcmp (physname, canon) != 0)
10674 {
10675 /* It may not mean a bug in GDB. The compiler could also
10676 compute DW_AT_linkage_name incorrectly. But in such case
10677 GDB would need to be bug-to-bug compatible. */
10678
10679 complaint (_("Computed physname <%s> does not match demangled <%s> "
10680 "(from linkage <%s>) - DIE at %s [in module %s]"),
10681 physname, canon, mangled, sect_offset_str (die->sect_off),
10682 objfile_name (objfile));
10683
10684 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10685 is available here - over computed PHYSNAME. It is safer
10686 against both buggy GDB and buggy compilers. */
10687
10688 retval = canon;
10689 }
10690 else
10691 {
10692 retval = physname;
10693 need_copy = 0;
10694 }
10695 }
10696 else
10697 retval = canon;
10698
10699 if (need_copy)
10700 retval = objfile->intern (retval);
10701
10702 return retval;
10703 }
10704
10705 /* Inspect DIE in CU for a namespace alias. If one exists, record
10706 a new symbol for it.
10707
10708 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10709
10710 static int
10711 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10712 {
10713 struct attribute *attr;
10714
10715 /* If the die does not have a name, this is not a namespace
10716 alias. */
10717 attr = dwarf2_attr (die, DW_AT_name, cu);
10718 if (attr != NULL)
10719 {
10720 int num;
10721 struct die_info *d = die;
10722 struct dwarf2_cu *imported_cu = cu;
10723
10724 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10725 keep inspecting DIEs until we hit the underlying import. */
10726 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10727 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10728 {
10729 attr = dwarf2_attr (d, DW_AT_import, cu);
10730 if (attr == NULL)
10731 break;
10732
10733 d = follow_die_ref (d, attr, &imported_cu);
10734 if (d->tag != DW_TAG_imported_declaration)
10735 break;
10736 }
10737
10738 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10739 {
10740 complaint (_("DIE at %s has too many recursively imported "
10741 "declarations"), sect_offset_str (d->sect_off));
10742 return 0;
10743 }
10744
10745 if (attr != NULL)
10746 {
10747 struct type *type;
10748 sect_offset sect_off = attr->get_ref_die_offset ();
10749
10750 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10751 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10752 {
10753 /* This declaration is a global namespace alias. Add
10754 a symbol for it whose type is the aliased namespace. */
10755 new_symbol (die, type, cu);
10756 return 1;
10757 }
10758 }
10759 }
10760
10761 return 0;
10762 }
10763
10764 /* Return the using directives repository (global or local?) to use in the
10765 current context for CU.
10766
10767 For Ada, imported declarations can materialize renamings, which *may* be
10768 global. However it is impossible (for now?) in DWARF to distinguish
10769 "external" imported declarations and "static" ones. As all imported
10770 declarations seem to be static in all other languages, make them all CU-wide
10771 global only in Ada. */
10772
10773 static struct using_direct **
10774 using_directives (struct dwarf2_cu *cu)
10775 {
10776 if (cu->language == language_ada
10777 && cu->get_builder ()->outermost_context_p ())
10778 return cu->get_builder ()->get_global_using_directives ();
10779 else
10780 return cu->get_builder ()->get_local_using_directives ();
10781 }
10782
10783 /* Read the import statement specified by the given die and record it. */
10784
10785 static void
10786 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10787 {
10788 struct objfile *objfile = cu->per_objfile->objfile;
10789 struct attribute *import_attr;
10790 struct die_info *imported_die, *child_die;
10791 struct dwarf2_cu *imported_cu;
10792 const char *imported_name;
10793 const char *imported_name_prefix;
10794 const char *canonical_name;
10795 const char *import_alias;
10796 const char *imported_declaration = NULL;
10797 const char *import_prefix;
10798 std::vector<const char *> excludes;
10799
10800 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10801 if (import_attr == NULL)
10802 {
10803 complaint (_("Tag '%s' has no DW_AT_import"),
10804 dwarf_tag_name (die->tag));
10805 return;
10806 }
10807
10808 imported_cu = cu;
10809 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10810 imported_name = dwarf2_name (imported_die, imported_cu);
10811 if (imported_name == NULL)
10812 {
10813 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10814
10815 The import in the following code:
10816 namespace A
10817 {
10818 typedef int B;
10819 }
10820
10821 int main ()
10822 {
10823 using A::B;
10824 B b;
10825 return b;
10826 }
10827
10828 ...
10829 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10830 <52> DW_AT_decl_file : 1
10831 <53> DW_AT_decl_line : 6
10832 <54> DW_AT_import : <0x75>
10833 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10834 <59> DW_AT_name : B
10835 <5b> DW_AT_decl_file : 1
10836 <5c> DW_AT_decl_line : 2
10837 <5d> DW_AT_type : <0x6e>
10838 ...
10839 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10840 <76> DW_AT_byte_size : 4
10841 <77> DW_AT_encoding : 5 (signed)
10842
10843 imports the wrong die ( 0x75 instead of 0x58 ).
10844 This case will be ignored until the gcc bug is fixed. */
10845 return;
10846 }
10847
10848 /* Figure out the local name after import. */
10849 import_alias = dwarf2_name (die, cu);
10850
10851 /* Figure out where the statement is being imported to. */
10852 import_prefix = determine_prefix (die, cu);
10853
10854 /* Figure out what the scope of the imported die is and prepend it
10855 to the name of the imported die. */
10856 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10857
10858 if (imported_die->tag != DW_TAG_namespace
10859 && imported_die->tag != DW_TAG_module)
10860 {
10861 imported_declaration = imported_name;
10862 canonical_name = imported_name_prefix;
10863 }
10864 else if (strlen (imported_name_prefix) > 0)
10865 canonical_name = obconcat (&objfile->objfile_obstack,
10866 imported_name_prefix,
10867 (cu->language == language_d ? "." : "::"),
10868 imported_name, (char *) NULL);
10869 else
10870 canonical_name = imported_name;
10871
10872 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10873 for (child_die = die->child; child_die && child_die->tag;
10874 child_die = child_die->sibling)
10875 {
10876 /* DWARF-4: A Fortran use statement with a “rename list” may be
10877 represented by an imported module entry with an import attribute
10878 referring to the module and owned entries corresponding to those
10879 entities that are renamed as part of being imported. */
10880
10881 if (child_die->tag != DW_TAG_imported_declaration)
10882 {
10883 complaint (_("child DW_TAG_imported_declaration expected "
10884 "- DIE at %s [in module %s]"),
10885 sect_offset_str (child_die->sect_off),
10886 objfile_name (objfile));
10887 continue;
10888 }
10889
10890 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10891 if (import_attr == NULL)
10892 {
10893 complaint (_("Tag '%s' has no DW_AT_import"),
10894 dwarf_tag_name (child_die->tag));
10895 continue;
10896 }
10897
10898 imported_cu = cu;
10899 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10900 &imported_cu);
10901 imported_name = dwarf2_name (imported_die, imported_cu);
10902 if (imported_name == NULL)
10903 {
10904 complaint (_("child DW_TAG_imported_declaration has unknown "
10905 "imported name - DIE at %s [in module %s]"),
10906 sect_offset_str (child_die->sect_off),
10907 objfile_name (objfile));
10908 continue;
10909 }
10910
10911 excludes.push_back (imported_name);
10912
10913 process_die (child_die, cu);
10914 }
10915
10916 add_using_directive (using_directives (cu),
10917 import_prefix,
10918 canonical_name,
10919 import_alias,
10920 imported_declaration,
10921 excludes,
10922 0,
10923 &objfile->objfile_obstack);
10924 }
10925
10926 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10927 types, but gives them a size of zero. Starting with version 14,
10928 ICC is compatible with GCC. */
10929
10930 static bool
10931 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10932 {
10933 if (!cu->checked_producer)
10934 check_producer (cu);
10935
10936 return cu->producer_is_icc_lt_14;
10937 }
10938
10939 /* ICC generates a DW_AT_type for C void functions. This was observed on
10940 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10941 which says that void functions should not have a DW_AT_type. */
10942
10943 static bool
10944 producer_is_icc (struct dwarf2_cu *cu)
10945 {
10946 if (!cu->checked_producer)
10947 check_producer (cu);
10948
10949 return cu->producer_is_icc;
10950 }
10951
10952 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10953 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10954 this, it was first present in GCC release 4.3.0. */
10955
10956 static bool
10957 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10958 {
10959 if (!cu->checked_producer)
10960 check_producer (cu);
10961
10962 return cu->producer_is_gcc_lt_4_3;
10963 }
10964
10965 static file_and_directory
10966 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10967 {
10968 file_and_directory res;
10969
10970 /* Find the filename. Do not use dwarf2_name here, since the filename
10971 is not a source language identifier. */
10972 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10973 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10974
10975 if (res.comp_dir == NULL
10976 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10977 && IS_ABSOLUTE_PATH (res.name))
10978 {
10979 res.comp_dir_storage = ldirname (res.name);
10980 if (!res.comp_dir_storage.empty ())
10981 res.comp_dir = res.comp_dir_storage.c_str ();
10982 }
10983 if (res.comp_dir != NULL)
10984 {
10985 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10986 directory, get rid of it. */
10987 const char *cp = strchr (res.comp_dir, ':');
10988
10989 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10990 res.comp_dir = cp + 1;
10991 }
10992
10993 if (res.name == NULL)
10994 res.name = "<unknown>";
10995
10996 return res;
10997 }
10998
10999 /* Handle DW_AT_stmt_list for a compilation unit.
11000 DIE is the DW_TAG_compile_unit die for CU.
11001 COMP_DIR is the compilation directory. LOWPC is passed to
11002 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11003
11004 static void
11005 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11006 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11007 {
11008 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11009 struct attribute *attr;
11010 struct line_header line_header_local;
11011 hashval_t line_header_local_hash;
11012 void **slot;
11013 int decode_mapping;
11014
11015 gdb_assert (! cu->per_cu->is_debug_types);
11016
11017 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11018 if (attr == NULL || !attr->form_is_unsigned ())
11019 return;
11020
11021 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11022
11023 /* The line header hash table is only created if needed (it exists to
11024 prevent redundant reading of the line table for partial_units).
11025 If we're given a partial_unit, we'll need it. If we're given a
11026 compile_unit, then use the line header hash table if it's already
11027 created, but don't create one just yet. */
11028
11029 if (per_objfile->line_header_hash == NULL
11030 && die->tag == DW_TAG_partial_unit)
11031 {
11032 per_objfile->line_header_hash
11033 .reset (htab_create_alloc (127, line_header_hash_voidp,
11034 line_header_eq_voidp,
11035 free_line_header_voidp,
11036 xcalloc, xfree));
11037 }
11038
11039 line_header_local.sect_off = line_offset;
11040 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11041 line_header_local_hash = line_header_hash (&line_header_local);
11042 if (per_objfile->line_header_hash != NULL)
11043 {
11044 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11045 &line_header_local,
11046 line_header_local_hash, NO_INSERT);
11047
11048 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11049 is not present in *SLOT (since if there is something in *SLOT then
11050 it will be for a partial_unit). */
11051 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11052 {
11053 gdb_assert (*slot != NULL);
11054 cu->line_header = (struct line_header *) *slot;
11055 return;
11056 }
11057 }
11058
11059 /* dwarf_decode_line_header does not yet provide sufficient information.
11060 We always have to call also dwarf_decode_lines for it. */
11061 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11062 if (lh == NULL)
11063 return;
11064
11065 cu->line_header = lh.release ();
11066 cu->line_header_die_owner = die;
11067
11068 if (per_objfile->line_header_hash == NULL)
11069 slot = NULL;
11070 else
11071 {
11072 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11073 &line_header_local,
11074 line_header_local_hash, INSERT);
11075 gdb_assert (slot != NULL);
11076 }
11077 if (slot != NULL && *slot == NULL)
11078 {
11079 /* This newly decoded line number information unit will be owned
11080 by line_header_hash hash table. */
11081 *slot = cu->line_header;
11082 cu->line_header_die_owner = NULL;
11083 }
11084 else
11085 {
11086 /* We cannot free any current entry in (*slot) as that struct line_header
11087 may be already used by multiple CUs. Create only temporary decoded
11088 line_header for this CU - it may happen at most once for each line
11089 number information unit. And if we're not using line_header_hash
11090 then this is what we want as well. */
11091 gdb_assert (die->tag != DW_TAG_partial_unit);
11092 }
11093 decode_mapping = (die->tag != DW_TAG_partial_unit);
11094 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11095 decode_mapping);
11096
11097 }
11098
11099 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11100
11101 static void
11102 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11103 {
11104 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11105 struct objfile *objfile = per_objfile->objfile;
11106 struct gdbarch *gdbarch = objfile->arch ();
11107 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11108 CORE_ADDR highpc = ((CORE_ADDR) 0);
11109 struct attribute *attr;
11110 struct die_info *child_die;
11111 CORE_ADDR baseaddr;
11112
11113 prepare_one_comp_unit (cu, die, cu->language);
11114 baseaddr = objfile->text_section_offset ();
11115
11116 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11117
11118 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11119 from finish_block. */
11120 if (lowpc == ((CORE_ADDR) -1))
11121 lowpc = highpc;
11122 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11123
11124 file_and_directory fnd = find_file_and_directory (die, cu);
11125
11126 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11127 standardised yet. As a workaround for the language detection we fall
11128 back to the DW_AT_producer string. */
11129 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11130 cu->language = language_opencl;
11131
11132 /* Similar hack for Go. */
11133 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11134 set_cu_language (DW_LANG_Go, cu);
11135
11136 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11137
11138 /* Decode line number information if present. We do this before
11139 processing child DIEs, so that the line header table is available
11140 for DW_AT_decl_file. */
11141 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11142
11143 /* Process all dies in compilation unit. */
11144 if (die->child != NULL)
11145 {
11146 child_die = die->child;
11147 while (child_die && child_die->tag)
11148 {
11149 process_die (child_die, cu);
11150 child_die = child_die->sibling;
11151 }
11152 }
11153
11154 /* Decode macro information, if present. Dwarf 2 macro information
11155 refers to information in the line number info statement program
11156 header, so we can only read it if we've read the header
11157 successfully. */
11158 attr = dwarf2_attr (die, DW_AT_macros, cu);
11159 if (attr == NULL)
11160 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11161 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11162 {
11163 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11164 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11165
11166 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
11167 }
11168 else
11169 {
11170 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11171 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11172 {
11173 unsigned int macro_offset = attr->as_unsigned ();
11174
11175 dwarf_decode_macros (cu, macro_offset, 0);
11176 }
11177 }
11178 }
11179
11180 void
11181 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11182 {
11183 struct type_unit_group *tu_group;
11184 int first_time;
11185 struct attribute *attr;
11186 unsigned int i;
11187 struct signatured_type *sig_type;
11188
11189 gdb_assert (per_cu->is_debug_types);
11190 sig_type = (struct signatured_type *) per_cu;
11191
11192 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11193
11194 /* If we're using .gdb_index (includes -readnow) then
11195 per_cu->type_unit_group may not have been set up yet. */
11196 if (sig_type->type_unit_group == NULL)
11197 sig_type->type_unit_group = get_type_unit_group (this, attr);
11198 tu_group = sig_type->type_unit_group;
11199
11200 /* If we've already processed this stmt_list there's no real need to
11201 do it again, we could fake it and just recreate the part we need
11202 (file name,index -> symtab mapping). If data shows this optimization
11203 is useful we can do it then. */
11204 type_unit_group_unshareable *tug_unshare
11205 = per_objfile->get_type_unit_group_unshareable (tu_group);
11206 first_time = tug_unshare->compunit_symtab == NULL;
11207
11208 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11209 debug info. */
11210 line_header_up lh;
11211 if (attr != NULL && attr->form_is_unsigned ())
11212 {
11213 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11214 lh = dwarf_decode_line_header (line_offset, this);
11215 }
11216 if (lh == NULL)
11217 {
11218 if (first_time)
11219 start_symtab ("", NULL, 0);
11220 else
11221 {
11222 gdb_assert (tug_unshare->symtabs == NULL);
11223 gdb_assert (m_builder == nullptr);
11224 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11225 m_builder.reset (new struct buildsym_compunit
11226 (COMPUNIT_OBJFILE (cust), "",
11227 COMPUNIT_DIRNAME (cust),
11228 compunit_language (cust),
11229 0, cust));
11230 list_in_scope = get_builder ()->get_file_symbols ();
11231 }
11232 return;
11233 }
11234
11235 line_header = lh.release ();
11236 line_header_die_owner = die;
11237
11238 if (first_time)
11239 {
11240 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11241
11242 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11243 still initializing it, and our caller (a few levels up)
11244 process_full_type_unit still needs to know if this is the first
11245 time. */
11246
11247 tug_unshare->symtabs
11248 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11249 struct symtab *, line_header->file_names_size ());
11250
11251 auto &file_names = line_header->file_names ();
11252 for (i = 0; i < file_names.size (); ++i)
11253 {
11254 file_entry &fe = file_names[i];
11255 dwarf2_start_subfile (this, fe.name,
11256 fe.include_dir (line_header));
11257 buildsym_compunit *b = get_builder ();
11258 if (b->get_current_subfile ()->symtab == NULL)
11259 {
11260 /* NOTE: start_subfile will recognize when it's been
11261 passed a file it has already seen. So we can't
11262 assume there's a simple mapping from
11263 cu->line_header->file_names to subfiles, plus
11264 cu->line_header->file_names may contain dups. */
11265 b->get_current_subfile ()->symtab
11266 = allocate_symtab (cust, b->get_current_subfile ()->name);
11267 }
11268
11269 fe.symtab = b->get_current_subfile ()->symtab;
11270 tug_unshare->symtabs[i] = fe.symtab;
11271 }
11272 }
11273 else
11274 {
11275 gdb_assert (m_builder == nullptr);
11276 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11277 m_builder.reset (new struct buildsym_compunit
11278 (COMPUNIT_OBJFILE (cust), "",
11279 COMPUNIT_DIRNAME (cust),
11280 compunit_language (cust),
11281 0, cust));
11282 list_in_scope = get_builder ()->get_file_symbols ();
11283
11284 auto &file_names = line_header->file_names ();
11285 for (i = 0; i < file_names.size (); ++i)
11286 {
11287 file_entry &fe = file_names[i];
11288 fe.symtab = tug_unshare->symtabs[i];
11289 }
11290 }
11291
11292 /* The main symtab is allocated last. Type units don't have DW_AT_name
11293 so they don't have a "real" (so to speak) symtab anyway.
11294 There is later code that will assign the main symtab to all symbols
11295 that don't have one. We need to handle the case of a symbol with a
11296 missing symtab (DW_AT_decl_file) anyway. */
11297 }
11298
11299 /* Process DW_TAG_type_unit.
11300 For TUs we want to skip the first top level sibling if it's not the
11301 actual type being defined by this TU. In this case the first top
11302 level sibling is there to provide context only. */
11303
11304 static void
11305 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11306 {
11307 struct die_info *child_die;
11308
11309 prepare_one_comp_unit (cu, die, language_minimal);
11310
11311 /* Initialize (or reinitialize) the machinery for building symtabs.
11312 We do this before processing child DIEs, so that the line header table
11313 is available for DW_AT_decl_file. */
11314 cu->setup_type_unit_groups (die);
11315
11316 if (die->child != NULL)
11317 {
11318 child_die = die->child;
11319 while (child_die && child_die->tag)
11320 {
11321 process_die (child_die, cu);
11322 child_die = child_die->sibling;
11323 }
11324 }
11325 }
11326 \f
11327 /* DWO/DWP files.
11328
11329 http://gcc.gnu.org/wiki/DebugFission
11330 http://gcc.gnu.org/wiki/DebugFissionDWP
11331
11332 To simplify handling of both DWO files ("object" files with the DWARF info)
11333 and DWP files (a file with the DWOs packaged up into one file), we treat
11334 DWP files as having a collection of virtual DWO files. */
11335
11336 static hashval_t
11337 hash_dwo_file (const void *item)
11338 {
11339 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11340 hashval_t hash;
11341
11342 hash = htab_hash_string (dwo_file->dwo_name);
11343 if (dwo_file->comp_dir != NULL)
11344 hash += htab_hash_string (dwo_file->comp_dir);
11345 return hash;
11346 }
11347
11348 static int
11349 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11350 {
11351 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11352 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11353
11354 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11355 return 0;
11356 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11357 return lhs->comp_dir == rhs->comp_dir;
11358 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11359 }
11360
11361 /* Allocate a hash table for DWO files. */
11362
11363 static htab_up
11364 allocate_dwo_file_hash_table ()
11365 {
11366 auto delete_dwo_file = [] (void *item)
11367 {
11368 struct dwo_file *dwo_file = (struct dwo_file *) item;
11369
11370 delete dwo_file;
11371 };
11372
11373 return htab_up (htab_create_alloc (41,
11374 hash_dwo_file,
11375 eq_dwo_file,
11376 delete_dwo_file,
11377 xcalloc, xfree));
11378 }
11379
11380 /* Lookup DWO file DWO_NAME. */
11381
11382 static void **
11383 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11384 const char *dwo_name,
11385 const char *comp_dir)
11386 {
11387 struct dwo_file find_entry;
11388 void **slot;
11389
11390 if (per_objfile->per_bfd->dwo_files == NULL)
11391 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11392
11393 find_entry.dwo_name = dwo_name;
11394 find_entry.comp_dir = comp_dir;
11395 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11396 INSERT);
11397
11398 return slot;
11399 }
11400
11401 static hashval_t
11402 hash_dwo_unit (const void *item)
11403 {
11404 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11405
11406 /* This drops the top 32 bits of the id, but is ok for a hash. */
11407 return dwo_unit->signature;
11408 }
11409
11410 static int
11411 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11412 {
11413 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11414 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11415
11416 /* The signature is assumed to be unique within the DWO file.
11417 So while object file CU dwo_id's always have the value zero,
11418 that's OK, assuming each object file DWO file has only one CU,
11419 and that's the rule for now. */
11420 return lhs->signature == rhs->signature;
11421 }
11422
11423 /* Allocate a hash table for DWO CUs,TUs.
11424 There is one of these tables for each of CUs,TUs for each DWO file. */
11425
11426 static htab_up
11427 allocate_dwo_unit_table ()
11428 {
11429 /* Start out with a pretty small number.
11430 Generally DWO files contain only one CU and maybe some TUs. */
11431 return htab_up (htab_create_alloc (3,
11432 hash_dwo_unit,
11433 eq_dwo_unit,
11434 NULL, xcalloc, xfree));
11435 }
11436
11437 /* die_reader_func for create_dwo_cu. */
11438
11439 static void
11440 create_dwo_cu_reader (const struct die_reader_specs *reader,
11441 const gdb_byte *info_ptr,
11442 struct die_info *comp_unit_die,
11443 struct dwo_file *dwo_file,
11444 struct dwo_unit *dwo_unit)
11445 {
11446 struct dwarf2_cu *cu = reader->cu;
11447 sect_offset sect_off = cu->per_cu->sect_off;
11448 struct dwarf2_section_info *section = cu->per_cu->section;
11449
11450 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11451 if (!signature.has_value ())
11452 {
11453 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11454 " its dwo_id [in module %s]"),
11455 sect_offset_str (sect_off), dwo_file->dwo_name);
11456 return;
11457 }
11458
11459 dwo_unit->dwo_file = dwo_file;
11460 dwo_unit->signature = *signature;
11461 dwo_unit->section = section;
11462 dwo_unit->sect_off = sect_off;
11463 dwo_unit->length = cu->per_cu->length;
11464
11465 if (dwarf_read_debug)
11466 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11467 sect_offset_str (sect_off),
11468 hex_string (dwo_unit->signature));
11469 }
11470
11471 /* Create the dwo_units for the CUs in a DWO_FILE.
11472 Note: This function processes DWO files only, not DWP files. */
11473
11474 static void
11475 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11476 dwarf2_cu *cu, struct dwo_file &dwo_file,
11477 dwarf2_section_info &section, htab_up &cus_htab)
11478 {
11479 struct objfile *objfile = per_objfile->objfile;
11480 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11481 const gdb_byte *info_ptr, *end_ptr;
11482
11483 section.read (objfile);
11484 info_ptr = section.buffer;
11485
11486 if (info_ptr == NULL)
11487 return;
11488
11489 if (dwarf_read_debug)
11490 {
11491 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11492 section.get_name (),
11493 section.get_file_name ());
11494 }
11495
11496 end_ptr = info_ptr + section.size;
11497 while (info_ptr < end_ptr)
11498 {
11499 struct dwarf2_per_cu_data per_cu;
11500 struct dwo_unit read_unit {};
11501 struct dwo_unit *dwo_unit;
11502 void **slot;
11503 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11504
11505 memset (&per_cu, 0, sizeof (per_cu));
11506 per_cu.per_bfd = per_bfd;
11507 per_cu.is_debug_types = 0;
11508 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11509 per_cu.section = &section;
11510
11511 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11512 if (!reader.dummy_p)
11513 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11514 &dwo_file, &read_unit);
11515 info_ptr += per_cu.length;
11516
11517 // If the unit could not be parsed, skip it.
11518 if (read_unit.dwo_file == NULL)
11519 continue;
11520
11521 if (cus_htab == NULL)
11522 cus_htab = allocate_dwo_unit_table ();
11523
11524 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11525 struct dwo_unit);
11526 *dwo_unit = read_unit;
11527 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11528 gdb_assert (slot != NULL);
11529 if (*slot != NULL)
11530 {
11531 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11532 sect_offset dup_sect_off = dup_cu->sect_off;
11533
11534 complaint (_("debug cu entry at offset %s is duplicate to"
11535 " the entry at offset %s, signature %s"),
11536 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11537 hex_string (dwo_unit->signature));
11538 }
11539 *slot = (void *)dwo_unit;
11540 }
11541 }
11542
11543 /* DWP file .debug_{cu,tu}_index section format:
11544 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11545 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11546
11547 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11548 officially standard DWP format was published with DWARF v5 and is called
11549 Version 5. There are no versions 3 or 4.
11550
11551 DWP Version 1:
11552
11553 Both index sections have the same format, and serve to map a 64-bit
11554 signature to a set of section numbers. Each section begins with a header,
11555 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11556 indexes, and a pool of 32-bit section numbers. The index sections will be
11557 aligned at 8-byte boundaries in the file.
11558
11559 The index section header consists of:
11560
11561 V, 32 bit version number
11562 -, 32 bits unused
11563 N, 32 bit number of compilation units or type units in the index
11564 M, 32 bit number of slots in the hash table
11565
11566 Numbers are recorded using the byte order of the application binary.
11567
11568 The hash table begins at offset 16 in the section, and consists of an array
11569 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11570 order of the application binary). Unused slots in the hash table are 0.
11571 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11572
11573 The parallel table begins immediately after the hash table
11574 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11575 array of 32-bit indexes (using the byte order of the application binary),
11576 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11577 table contains a 32-bit index into the pool of section numbers. For unused
11578 hash table slots, the corresponding entry in the parallel table will be 0.
11579
11580 The pool of section numbers begins immediately following the hash table
11581 (at offset 16 + 12 * M from the beginning of the section). The pool of
11582 section numbers consists of an array of 32-bit words (using the byte order
11583 of the application binary). Each item in the array is indexed starting
11584 from 0. The hash table entry provides the index of the first section
11585 number in the set. Additional section numbers in the set follow, and the
11586 set is terminated by a 0 entry (section number 0 is not used in ELF).
11587
11588 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11589 section must be the first entry in the set, and the .debug_abbrev.dwo must
11590 be the second entry. Other members of the set may follow in any order.
11591
11592 ---
11593
11594 DWP Versions 2 and 5:
11595
11596 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11597 and the entries in the index tables are now offsets into these sections.
11598 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11599 section.
11600
11601 Index Section Contents:
11602 Header
11603 Hash Table of Signatures dwp_hash_table.hash_table
11604 Parallel Table of Indices dwp_hash_table.unit_table
11605 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11606 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11607
11608 The index section header consists of:
11609
11610 V, 32 bit version number
11611 L, 32 bit number of columns in the table of section offsets
11612 N, 32 bit number of compilation units or type units in the index
11613 M, 32 bit number of slots in the hash table
11614
11615 Numbers are recorded using the byte order of the application binary.
11616
11617 The hash table has the same format as version 1.
11618 The parallel table of indices has the same format as version 1,
11619 except that the entries are origin-1 indices into the table of sections
11620 offsets and the table of section sizes.
11621
11622 The table of offsets begins immediately following the parallel table
11623 (at offset 16 + 12 * M from the beginning of the section). The table is
11624 a two-dimensional array of 32-bit words (using the byte order of the
11625 application binary), with L columns and N+1 rows, in row-major order.
11626 Each row in the array is indexed starting from 0. The first row provides
11627 a key to the remaining rows: each column in this row provides an identifier
11628 for a debug section, and the offsets in the same column of subsequent rows
11629 refer to that section. The section identifiers for Version 2 are:
11630
11631 DW_SECT_INFO 1 .debug_info.dwo
11632 DW_SECT_TYPES 2 .debug_types.dwo
11633 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11634 DW_SECT_LINE 4 .debug_line.dwo
11635 DW_SECT_LOC 5 .debug_loc.dwo
11636 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11637 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11638 DW_SECT_MACRO 8 .debug_macro.dwo
11639
11640 The section identifiers for Version 5 are:
11641
11642 DW_SECT_INFO_V5 1 .debug_info.dwo
11643 DW_SECT_RESERVED_V5 2 --
11644 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11645 DW_SECT_LINE_V5 4 .debug_line.dwo
11646 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11647 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11648 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11649 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11650
11651 The offsets provided by the CU and TU index sections are the base offsets
11652 for the contributions made by each CU or TU to the corresponding section
11653 in the package file. Each CU and TU header contains an abbrev_offset
11654 field, used to find the abbreviations table for that CU or TU within the
11655 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11656 be interpreted as relative to the base offset given in the index section.
11657 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11658 should be interpreted as relative to the base offset for .debug_line.dwo,
11659 and offsets into other debug sections obtained from DWARF attributes should
11660 also be interpreted as relative to the corresponding base offset.
11661
11662 The table of sizes begins immediately following the table of offsets.
11663 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11664 with L columns and N rows, in row-major order. Each row in the array is
11665 indexed starting from 1 (row 0 is shared by the two tables).
11666
11667 ---
11668
11669 Hash table lookup is handled the same in version 1 and 2:
11670
11671 We assume that N and M will not exceed 2^32 - 1.
11672 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11673
11674 Given a 64-bit compilation unit signature or a type signature S, an entry
11675 in the hash table is located as follows:
11676
11677 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11678 the low-order k bits all set to 1.
11679
11680 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11681
11682 3) If the hash table entry at index H matches the signature, use that
11683 entry. If the hash table entry at index H is unused (all zeroes),
11684 terminate the search: the signature is not present in the table.
11685
11686 4) Let H = (H + H') modulo M. Repeat at Step 3.
11687
11688 Because M > N and H' and M are relatively prime, the search is guaranteed
11689 to stop at an unused slot or find the match. */
11690
11691 /* Create a hash table to map DWO IDs to their CU/TU entry in
11692 .debug_{info,types}.dwo in DWP_FILE.
11693 Returns NULL if there isn't one.
11694 Note: This function processes DWP files only, not DWO files. */
11695
11696 static struct dwp_hash_table *
11697 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11698 struct dwp_file *dwp_file, int is_debug_types)
11699 {
11700 struct objfile *objfile = per_objfile->objfile;
11701 bfd *dbfd = dwp_file->dbfd.get ();
11702 const gdb_byte *index_ptr, *index_end;
11703 struct dwarf2_section_info *index;
11704 uint32_t version, nr_columns, nr_units, nr_slots;
11705 struct dwp_hash_table *htab;
11706
11707 if (is_debug_types)
11708 index = &dwp_file->sections.tu_index;
11709 else
11710 index = &dwp_file->sections.cu_index;
11711
11712 if (index->empty ())
11713 return NULL;
11714 index->read (objfile);
11715
11716 index_ptr = index->buffer;
11717 index_end = index_ptr + index->size;
11718
11719 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11720 For now it's safe to just read 4 bytes (particularly as it's difficult to
11721 tell if you're dealing with Version 5 before you've read the version). */
11722 version = read_4_bytes (dbfd, index_ptr);
11723 index_ptr += 4;
11724 if (version == 2 || version == 5)
11725 nr_columns = read_4_bytes (dbfd, index_ptr);
11726 else
11727 nr_columns = 0;
11728 index_ptr += 4;
11729 nr_units = read_4_bytes (dbfd, index_ptr);
11730 index_ptr += 4;
11731 nr_slots = read_4_bytes (dbfd, index_ptr);
11732 index_ptr += 4;
11733
11734 if (version != 1 && version != 2 && version != 5)
11735 {
11736 error (_("Dwarf Error: unsupported DWP file version (%s)"
11737 " [in module %s]"),
11738 pulongest (version), dwp_file->name);
11739 }
11740 if (nr_slots != (nr_slots & -nr_slots))
11741 {
11742 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11743 " is not power of 2 [in module %s]"),
11744 pulongest (nr_slots), dwp_file->name);
11745 }
11746
11747 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11748 htab->version = version;
11749 htab->nr_columns = nr_columns;
11750 htab->nr_units = nr_units;
11751 htab->nr_slots = nr_slots;
11752 htab->hash_table = index_ptr;
11753 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11754
11755 /* Exit early if the table is empty. */
11756 if (nr_slots == 0 || nr_units == 0
11757 || (version == 2 && nr_columns == 0)
11758 || (version == 5 && nr_columns == 0))
11759 {
11760 /* All must be zero. */
11761 if (nr_slots != 0 || nr_units != 0
11762 || (version == 2 && nr_columns != 0)
11763 || (version == 5 && nr_columns != 0))
11764 {
11765 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11766 " all zero [in modules %s]"),
11767 dwp_file->name);
11768 }
11769 return htab;
11770 }
11771
11772 if (version == 1)
11773 {
11774 htab->section_pool.v1.indices =
11775 htab->unit_table + sizeof (uint32_t) * nr_slots;
11776 /* It's harder to decide whether the section is too small in v1.
11777 V1 is deprecated anyway so we punt. */
11778 }
11779 else if (version == 2)
11780 {
11781 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11782 int *ids = htab->section_pool.v2.section_ids;
11783 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11784 /* Reverse map for error checking. */
11785 int ids_seen[DW_SECT_MAX + 1];
11786 int i;
11787
11788 if (nr_columns < 2)
11789 {
11790 error (_("Dwarf Error: bad DWP hash table, too few columns"
11791 " in section table [in module %s]"),
11792 dwp_file->name);
11793 }
11794 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11795 {
11796 error (_("Dwarf Error: bad DWP hash table, too many columns"
11797 " in section table [in module %s]"),
11798 dwp_file->name);
11799 }
11800 memset (ids, 255, sizeof_ids);
11801 memset (ids_seen, 255, sizeof (ids_seen));
11802 for (i = 0; i < nr_columns; ++i)
11803 {
11804 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11805
11806 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11807 {
11808 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11809 " in section table [in module %s]"),
11810 id, dwp_file->name);
11811 }
11812 if (ids_seen[id] != -1)
11813 {
11814 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11815 " id %d in section table [in module %s]"),
11816 id, dwp_file->name);
11817 }
11818 ids_seen[id] = i;
11819 ids[i] = id;
11820 }
11821 /* Must have exactly one info or types section. */
11822 if (((ids_seen[DW_SECT_INFO] != -1)
11823 + (ids_seen[DW_SECT_TYPES] != -1))
11824 != 1)
11825 {
11826 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11827 " DWO info/types section [in module %s]"),
11828 dwp_file->name);
11829 }
11830 /* Must have an abbrev section. */
11831 if (ids_seen[DW_SECT_ABBREV] == -1)
11832 {
11833 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11834 " section [in module %s]"),
11835 dwp_file->name);
11836 }
11837 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11838 htab->section_pool.v2.sizes =
11839 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11840 * nr_units * nr_columns);
11841 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11842 * nr_units * nr_columns))
11843 > index_end)
11844 {
11845 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11846 " [in module %s]"),
11847 dwp_file->name);
11848 }
11849 }
11850 else /* version == 5 */
11851 {
11852 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11853 int *ids = htab->section_pool.v5.section_ids;
11854 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11855 /* Reverse map for error checking. */
11856 int ids_seen[DW_SECT_MAX_V5 + 1];
11857
11858 if (nr_columns < 2)
11859 {
11860 error (_("Dwarf Error: bad DWP hash table, too few columns"
11861 " in section table [in module %s]"),
11862 dwp_file->name);
11863 }
11864 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11865 {
11866 error (_("Dwarf Error: bad DWP hash table, too many columns"
11867 " in section table [in module %s]"),
11868 dwp_file->name);
11869 }
11870 memset (ids, 255, sizeof_ids);
11871 memset (ids_seen, 255, sizeof (ids_seen));
11872 for (int i = 0; i < nr_columns; ++i)
11873 {
11874 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11875
11876 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11877 {
11878 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11879 " in section table [in module %s]"),
11880 id, dwp_file->name);
11881 }
11882 if (ids_seen[id] != -1)
11883 {
11884 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11885 " id %d in section table [in module %s]"),
11886 id, dwp_file->name);
11887 }
11888 ids_seen[id] = i;
11889 ids[i] = id;
11890 }
11891 /* Must have seen an info section. */
11892 if (ids_seen[DW_SECT_INFO_V5] == -1)
11893 {
11894 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11895 " DWO info/types section [in module %s]"),
11896 dwp_file->name);
11897 }
11898 /* Must have an abbrev section. */
11899 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11900 {
11901 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11902 " section [in module %s]"),
11903 dwp_file->name);
11904 }
11905 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11906 htab->section_pool.v5.sizes
11907 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11908 * nr_units * nr_columns);
11909 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11910 * nr_units * nr_columns))
11911 > index_end)
11912 {
11913 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11914 " [in module %s]"),
11915 dwp_file->name);
11916 }
11917 }
11918
11919 return htab;
11920 }
11921
11922 /* Update SECTIONS with the data from SECTP.
11923
11924 This function is like the other "locate" section routines, but in
11925 this context the sections to read comes from the DWP V1 hash table,
11926 not the full ELF section table.
11927
11928 The result is non-zero for success, or zero if an error was found. */
11929
11930 static int
11931 locate_v1_virtual_dwo_sections (asection *sectp,
11932 struct virtual_v1_dwo_sections *sections)
11933 {
11934 const struct dwop_section_names *names = &dwop_section_names;
11935
11936 if (section_is_p (sectp->name, &names->abbrev_dwo))
11937 {
11938 /* There can be only one. */
11939 if (sections->abbrev.s.section != NULL)
11940 return 0;
11941 sections->abbrev.s.section = sectp;
11942 sections->abbrev.size = bfd_section_size (sectp);
11943 }
11944 else if (section_is_p (sectp->name, &names->info_dwo)
11945 || section_is_p (sectp->name, &names->types_dwo))
11946 {
11947 /* There can be only one. */
11948 if (sections->info_or_types.s.section != NULL)
11949 return 0;
11950 sections->info_or_types.s.section = sectp;
11951 sections->info_or_types.size = bfd_section_size (sectp);
11952 }
11953 else if (section_is_p (sectp->name, &names->line_dwo))
11954 {
11955 /* There can be only one. */
11956 if (sections->line.s.section != NULL)
11957 return 0;
11958 sections->line.s.section = sectp;
11959 sections->line.size = bfd_section_size (sectp);
11960 }
11961 else if (section_is_p (sectp->name, &names->loc_dwo))
11962 {
11963 /* There can be only one. */
11964 if (sections->loc.s.section != NULL)
11965 return 0;
11966 sections->loc.s.section = sectp;
11967 sections->loc.size = bfd_section_size (sectp);
11968 }
11969 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11970 {
11971 /* There can be only one. */
11972 if (sections->macinfo.s.section != NULL)
11973 return 0;
11974 sections->macinfo.s.section = sectp;
11975 sections->macinfo.size = bfd_section_size (sectp);
11976 }
11977 else if (section_is_p (sectp->name, &names->macro_dwo))
11978 {
11979 /* There can be only one. */
11980 if (sections->macro.s.section != NULL)
11981 return 0;
11982 sections->macro.s.section = sectp;
11983 sections->macro.size = bfd_section_size (sectp);
11984 }
11985 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11986 {
11987 /* There can be only one. */
11988 if (sections->str_offsets.s.section != NULL)
11989 return 0;
11990 sections->str_offsets.s.section = sectp;
11991 sections->str_offsets.size = bfd_section_size (sectp);
11992 }
11993 else
11994 {
11995 /* No other kind of section is valid. */
11996 return 0;
11997 }
11998
11999 return 1;
12000 }
12001
12002 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12003 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12004 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12005 This is for DWP version 1 files. */
12006
12007 static struct dwo_unit *
12008 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
12009 struct dwp_file *dwp_file,
12010 uint32_t unit_index,
12011 const char *comp_dir,
12012 ULONGEST signature, int is_debug_types)
12013 {
12014 const struct dwp_hash_table *dwp_htab =
12015 is_debug_types ? dwp_file->tus : dwp_file->cus;
12016 bfd *dbfd = dwp_file->dbfd.get ();
12017 const char *kind = is_debug_types ? "TU" : "CU";
12018 struct dwo_file *dwo_file;
12019 struct dwo_unit *dwo_unit;
12020 struct virtual_v1_dwo_sections sections;
12021 void **dwo_file_slot;
12022 int i;
12023
12024 gdb_assert (dwp_file->version == 1);
12025
12026 if (dwarf_read_debug)
12027 {
12028 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12029 kind,
12030 pulongest (unit_index), hex_string (signature),
12031 dwp_file->name);
12032 }
12033
12034 /* Fetch the sections of this DWO unit.
12035 Put a limit on the number of sections we look for so that bad data
12036 doesn't cause us to loop forever. */
12037
12038 #define MAX_NR_V1_DWO_SECTIONS \
12039 (1 /* .debug_info or .debug_types */ \
12040 + 1 /* .debug_abbrev */ \
12041 + 1 /* .debug_line */ \
12042 + 1 /* .debug_loc */ \
12043 + 1 /* .debug_str_offsets */ \
12044 + 1 /* .debug_macro or .debug_macinfo */ \
12045 + 1 /* trailing zero */)
12046
12047 memset (&sections, 0, sizeof (sections));
12048
12049 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12050 {
12051 asection *sectp;
12052 uint32_t section_nr =
12053 read_4_bytes (dbfd,
12054 dwp_htab->section_pool.v1.indices
12055 + (unit_index + i) * sizeof (uint32_t));
12056
12057 if (section_nr == 0)
12058 break;
12059 if (section_nr >= dwp_file->num_sections)
12060 {
12061 error (_("Dwarf Error: bad DWP hash table, section number too large"
12062 " [in module %s]"),
12063 dwp_file->name);
12064 }
12065
12066 sectp = dwp_file->elf_sections[section_nr];
12067 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12068 {
12069 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12070 " [in module %s]"),
12071 dwp_file->name);
12072 }
12073 }
12074
12075 if (i < 2
12076 || sections.info_or_types.empty ()
12077 || sections.abbrev.empty ())
12078 {
12079 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12080 " [in module %s]"),
12081 dwp_file->name);
12082 }
12083 if (i == MAX_NR_V1_DWO_SECTIONS)
12084 {
12085 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12086 " [in module %s]"),
12087 dwp_file->name);
12088 }
12089
12090 /* It's easier for the rest of the code if we fake a struct dwo_file and
12091 have dwo_unit "live" in that. At least for now.
12092
12093 The DWP file can be made up of a random collection of CUs and TUs.
12094 However, for each CU + set of TUs that came from the same original DWO
12095 file, we can combine them back into a virtual DWO file to save space
12096 (fewer struct dwo_file objects to allocate). Remember that for really
12097 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12098
12099 std::string virtual_dwo_name =
12100 string_printf ("virtual-dwo/%d-%d-%d-%d",
12101 sections.abbrev.get_id (),
12102 sections.line.get_id (),
12103 sections.loc.get_id (),
12104 sections.str_offsets.get_id ());
12105 /* Can we use an existing virtual DWO file? */
12106 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12107 comp_dir);
12108 /* Create one if necessary. */
12109 if (*dwo_file_slot == NULL)
12110 {
12111 if (dwarf_read_debug)
12112 {
12113 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12114 virtual_dwo_name.c_str ());
12115 }
12116 dwo_file = new struct dwo_file;
12117 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12118 dwo_file->comp_dir = comp_dir;
12119 dwo_file->sections.abbrev = sections.abbrev;
12120 dwo_file->sections.line = sections.line;
12121 dwo_file->sections.loc = sections.loc;
12122 dwo_file->sections.macinfo = sections.macinfo;
12123 dwo_file->sections.macro = sections.macro;
12124 dwo_file->sections.str_offsets = sections.str_offsets;
12125 /* The "str" section is global to the entire DWP file. */
12126 dwo_file->sections.str = dwp_file->sections.str;
12127 /* The info or types section is assigned below to dwo_unit,
12128 there's no need to record it in dwo_file.
12129 Also, we can't simply record type sections in dwo_file because
12130 we record a pointer into the vector in dwo_unit. As we collect more
12131 types we'll grow the vector and eventually have to reallocate space
12132 for it, invalidating all copies of pointers into the previous
12133 contents. */
12134 *dwo_file_slot = dwo_file;
12135 }
12136 else
12137 {
12138 if (dwarf_read_debug)
12139 {
12140 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12141 virtual_dwo_name.c_str ());
12142 }
12143 dwo_file = (struct dwo_file *) *dwo_file_slot;
12144 }
12145
12146 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12147 dwo_unit->dwo_file = dwo_file;
12148 dwo_unit->signature = signature;
12149 dwo_unit->section =
12150 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12151 *dwo_unit->section = sections.info_or_types;
12152 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12153
12154 return dwo_unit;
12155 }
12156
12157 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12158 simplify them. Given a pointer to the containing section SECTION, and
12159 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12160 virtual section of just that piece. */
12161
12162 static struct dwarf2_section_info
12163 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12164 struct dwarf2_section_info *section,
12165 bfd_size_type offset, bfd_size_type size)
12166 {
12167 struct dwarf2_section_info result;
12168 asection *sectp;
12169
12170 gdb_assert (section != NULL);
12171 gdb_assert (!section->is_virtual);
12172
12173 memset (&result, 0, sizeof (result));
12174 result.s.containing_section = section;
12175 result.is_virtual = true;
12176
12177 if (size == 0)
12178 return result;
12179
12180 sectp = section->get_bfd_section ();
12181
12182 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12183 bounds of the real section. This is a pretty-rare event, so just
12184 flag an error (easier) instead of a warning and trying to cope. */
12185 if (sectp == NULL
12186 || offset + size > bfd_section_size (sectp))
12187 {
12188 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
12189 " in section %s [in module %s]"),
12190 sectp ? bfd_section_name (sectp) : "<unknown>",
12191 objfile_name (per_objfile->objfile));
12192 }
12193
12194 result.virtual_offset = offset;
12195 result.size = size;
12196 return result;
12197 }
12198
12199 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12200 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12201 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12202 This is for DWP version 2 files. */
12203
12204 static struct dwo_unit *
12205 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
12206 struct dwp_file *dwp_file,
12207 uint32_t unit_index,
12208 const char *comp_dir,
12209 ULONGEST signature, int is_debug_types)
12210 {
12211 const struct dwp_hash_table *dwp_htab =
12212 is_debug_types ? dwp_file->tus : dwp_file->cus;
12213 bfd *dbfd = dwp_file->dbfd.get ();
12214 const char *kind = is_debug_types ? "TU" : "CU";
12215 struct dwo_file *dwo_file;
12216 struct dwo_unit *dwo_unit;
12217 struct virtual_v2_or_v5_dwo_sections sections;
12218 void **dwo_file_slot;
12219 int i;
12220
12221 gdb_assert (dwp_file->version == 2);
12222
12223 if (dwarf_read_debug)
12224 {
12225 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12226 kind,
12227 pulongest (unit_index), hex_string (signature),
12228 dwp_file->name);
12229 }
12230
12231 /* Fetch the section offsets of this DWO unit. */
12232
12233 memset (&sections, 0, sizeof (sections));
12234
12235 for (i = 0; i < dwp_htab->nr_columns; ++i)
12236 {
12237 uint32_t offset = read_4_bytes (dbfd,
12238 dwp_htab->section_pool.v2.offsets
12239 + (((unit_index - 1) * dwp_htab->nr_columns
12240 + i)
12241 * sizeof (uint32_t)));
12242 uint32_t size = read_4_bytes (dbfd,
12243 dwp_htab->section_pool.v2.sizes
12244 + (((unit_index - 1) * dwp_htab->nr_columns
12245 + i)
12246 * sizeof (uint32_t)));
12247
12248 switch (dwp_htab->section_pool.v2.section_ids[i])
12249 {
12250 case DW_SECT_INFO:
12251 case DW_SECT_TYPES:
12252 sections.info_or_types_offset = offset;
12253 sections.info_or_types_size = size;
12254 break;
12255 case DW_SECT_ABBREV:
12256 sections.abbrev_offset = offset;
12257 sections.abbrev_size = size;
12258 break;
12259 case DW_SECT_LINE:
12260 sections.line_offset = offset;
12261 sections.line_size = size;
12262 break;
12263 case DW_SECT_LOC:
12264 sections.loc_offset = offset;
12265 sections.loc_size = size;
12266 break;
12267 case DW_SECT_STR_OFFSETS:
12268 sections.str_offsets_offset = offset;
12269 sections.str_offsets_size = size;
12270 break;
12271 case DW_SECT_MACINFO:
12272 sections.macinfo_offset = offset;
12273 sections.macinfo_size = size;
12274 break;
12275 case DW_SECT_MACRO:
12276 sections.macro_offset = offset;
12277 sections.macro_size = size;
12278 break;
12279 }
12280 }
12281
12282 /* It's easier for the rest of the code if we fake a struct dwo_file and
12283 have dwo_unit "live" in that. At least for now.
12284
12285 The DWP file can be made up of a random collection of CUs and TUs.
12286 However, for each CU + set of TUs that came from the same original DWO
12287 file, we can combine them back into a virtual DWO file to save space
12288 (fewer struct dwo_file objects to allocate). Remember that for really
12289 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12290
12291 std::string virtual_dwo_name =
12292 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12293 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12294 (long) (sections.line_size ? sections.line_offset : 0),
12295 (long) (sections.loc_size ? sections.loc_offset : 0),
12296 (long) (sections.str_offsets_size
12297 ? sections.str_offsets_offset : 0));
12298 /* Can we use an existing virtual DWO file? */
12299 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12300 comp_dir);
12301 /* Create one if necessary. */
12302 if (*dwo_file_slot == NULL)
12303 {
12304 if (dwarf_read_debug)
12305 {
12306 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12307 virtual_dwo_name.c_str ());
12308 }
12309 dwo_file = new struct dwo_file;
12310 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12311 dwo_file->comp_dir = comp_dir;
12312 dwo_file->sections.abbrev =
12313 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12314 sections.abbrev_offset,
12315 sections.abbrev_size);
12316 dwo_file->sections.line =
12317 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12318 sections.line_offset,
12319 sections.line_size);
12320 dwo_file->sections.loc =
12321 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12322 sections.loc_offset, sections.loc_size);
12323 dwo_file->sections.macinfo =
12324 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12325 sections.macinfo_offset,
12326 sections.macinfo_size);
12327 dwo_file->sections.macro =
12328 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12329 sections.macro_offset,
12330 sections.macro_size);
12331 dwo_file->sections.str_offsets =
12332 create_dwp_v2_or_v5_section (per_objfile,
12333 &dwp_file->sections.str_offsets,
12334 sections.str_offsets_offset,
12335 sections.str_offsets_size);
12336 /* The "str" section is global to the entire DWP file. */
12337 dwo_file->sections.str = dwp_file->sections.str;
12338 /* The info or types section is assigned below to dwo_unit,
12339 there's no need to record it in dwo_file.
12340 Also, we can't simply record type sections in dwo_file because
12341 we record a pointer into the vector in dwo_unit. As we collect more
12342 types we'll grow the vector and eventually have to reallocate space
12343 for it, invalidating all copies of pointers into the previous
12344 contents. */
12345 *dwo_file_slot = dwo_file;
12346 }
12347 else
12348 {
12349 if (dwarf_read_debug)
12350 {
12351 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12352 virtual_dwo_name.c_str ());
12353 }
12354 dwo_file = (struct dwo_file *) *dwo_file_slot;
12355 }
12356
12357 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12358 dwo_unit->dwo_file = dwo_file;
12359 dwo_unit->signature = signature;
12360 dwo_unit->section =
12361 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12362 *dwo_unit->section = create_dwp_v2_or_v5_section
12363 (per_objfile,
12364 is_debug_types
12365 ? &dwp_file->sections.types
12366 : &dwp_file->sections.info,
12367 sections.info_or_types_offset,
12368 sections.info_or_types_size);
12369 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12370
12371 return dwo_unit;
12372 }
12373
12374 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12375 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12376 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12377 This is for DWP version 5 files. */
12378
12379 static struct dwo_unit *
12380 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12381 struct dwp_file *dwp_file,
12382 uint32_t unit_index,
12383 const char *comp_dir,
12384 ULONGEST signature, int is_debug_types)
12385 {
12386 const struct dwp_hash_table *dwp_htab
12387 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12388 bfd *dbfd = dwp_file->dbfd.get ();
12389 const char *kind = is_debug_types ? "TU" : "CU";
12390 struct dwo_file *dwo_file;
12391 struct dwo_unit *dwo_unit;
12392 struct virtual_v2_or_v5_dwo_sections sections {};
12393 void **dwo_file_slot;
12394
12395 gdb_assert (dwp_file->version == 5);
12396
12397 if (dwarf_read_debug)
12398 {
12399 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V5 file: %s\n",
12400 kind,
12401 pulongest (unit_index), hex_string (signature),
12402 dwp_file->name);
12403 }
12404
12405 /* Fetch the section offsets of this DWO unit. */
12406
12407 /* memset (&sections, 0, sizeof (sections)); */
12408
12409 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12410 {
12411 uint32_t offset = read_4_bytes (dbfd,
12412 dwp_htab->section_pool.v5.offsets
12413 + (((unit_index - 1)
12414 * dwp_htab->nr_columns
12415 + i)
12416 * sizeof (uint32_t)));
12417 uint32_t size = read_4_bytes (dbfd,
12418 dwp_htab->section_pool.v5.sizes
12419 + (((unit_index - 1) * dwp_htab->nr_columns
12420 + i)
12421 * sizeof (uint32_t)));
12422
12423 switch (dwp_htab->section_pool.v5.section_ids[i])
12424 {
12425 case DW_SECT_ABBREV_V5:
12426 sections.abbrev_offset = offset;
12427 sections.abbrev_size = size;
12428 break;
12429 case DW_SECT_INFO_V5:
12430 sections.info_or_types_offset = offset;
12431 sections.info_or_types_size = size;
12432 break;
12433 case DW_SECT_LINE_V5:
12434 sections.line_offset = offset;
12435 sections.line_size = size;
12436 break;
12437 case DW_SECT_LOCLISTS_V5:
12438 sections.loclists_offset = offset;
12439 sections.loclists_size = size;
12440 break;
12441 case DW_SECT_MACRO_V5:
12442 sections.macro_offset = offset;
12443 sections.macro_size = size;
12444 break;
12445 case DW_SECT_RNGLISTS_V5:
12446 sections.rnglists_offset = offset;
12447 sections.rnglists_size = size;
12448 break;
12449 case DW_SECT_STR_OFFSETS_V5:
12450 sections.str_offsets_offset = offset;
12451 sections.str_offsets_size = size;
12452 break;
12453 case DW_SECT_RESERVED_V5:
12454 default:
12455 break;
12456 }
12457 }
12458
12459 /* It's easier for the rest of the code if we fake a struct dwo_file and
12460 have dwo_unit "live" in that. At least for now.
12461
12462 The DWP file can be made up of a random collection of CUs and TUs.
12463 However, for each CU + set of TUs that came from the same original DWO
12464 file, we can combine them back into a virtual DWO file to save space
12465 (fewer struct dwo_file objects to allocate). Remember that for really
12466 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12467
12468 std::string virtual_dwo_name =
12469 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
12470 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12471 (long) (sections.line_size ? sections.line_offset : 0),
12472 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12473 (long) (sections.str_offsets_size
12474 ? sections.str_offsets_offset : 0),
12475 (long) (sections.macro_size ? sections.macro_offset : 0),
12476 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
12477 /* Can we use an existing virtual DWO file? */
12478 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
12479 virtual_dwo_name.c_str (),
12480 comp_dir);
12481 /* Create one if necessary. */
12482 if (*dwo_file_slot == NULL)
12483 {
12484 if (dwarf_read_debug)
12485 {
12486 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12487 virtual_dwo_name.c_str ());
12488 }
12489 dwo_file = new struct dwo_file;
12490 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12491 dwo_file->comp_dir = comp_dir;
12492 dwo_file->sections.abbrev =
12493 create_dwp_v2_or_v5_section (per_objfile,
12494 &dwp_file->sections.abbrev,
12495 sections.abbrev_offset,
12496 sections.abbrev_size);
12497 dwo_file->sections.line =
12498 create_dwp_v2_or_v5_section (per_objfile,
12499 &dwp_file->sections.line,
12500 sections.line_offset, sections.line_size);
12501 dwo_file->sections.macro =
12502 create_dwp_v2_or_v5_section (per_objfile,
12503 &dwp_file->sections.macro,
12504 sections.macro_offset,
12505 sections.macro_size);
12506 dwo_file->sections.loclists =
12507 create_dwp_v2_or_v5_section (per_objfile,
12508 &dwp_file->sections.loclists,
12509 sections.loclists_offset,
12510 sections.loclists_size);
12511 dwo_file->sections.rnglists =
12512 create_dwp_v2_or_v5_section (per_objfile,
12513 &dwp_file->sections.rnglists,
12514 sections.rnglists_offset,
12515 sections.rnglists_size);
12516 dwo_file->sections.str_offsets =
12517 create_dwp_v2_or_v5_section (per_objfile,
12518 &dwp_file->sections.str_offsets,
12519 sections.str_offsets_offset,
12520 sections.str_offsets_size);
12521 /* The "str" section is global to the entire DWP file. */
12522 dwo_file->sections.str = dwp_file->sections.str;
12523 /* The info or types section is assigned below to dwo_unit,
12524 there's no need to record it in dwo_file.
12525 Also, we can't simply record type sections in dwo_file because
12526 we record a pointer into the vector in dwo_unit. As we collect more
12527 types we'll grow the vector and eventually have to reallocate space
12528 for it, invalidating all copies of pointers into the previous
12529 contents. */
12530 *dwo_file_slot = dwo_file;
12531 }
12532 else
12533 {
12534 if (dwarf_read_debug)
12535 {
12536 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12537 virtual_dwo_name.c_str ());
12538 }
12539 dwo_file = (struct dwo_file *) *dwo_file_slot;
12540 }
12541
12542 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12543 dwo_unit->dwo_file = dwo_file;
12544 dwo_unit->signature = signature;
12545 dwo_unit->section
12546 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12547 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12548 &dwp_file->sections.info,
12549 sections.info_or_types_offset,
12550 sections.info_or_types_size);
12551 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12552
12553 return dwo_unit;
12554 }
12555
12556 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12557 Returns NULL if the signature isn't found. */
12558
12559 static struct dwo_unit *
12560 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12561 struct dwp_file *dwp_file, const char *comp_dir,
12562 ULONGEST signature, int is_debug_types)
12563 {
12564 const struct dwp_hash_table *dwp_htab =
12565 is_debug_types ? dwp_file->tus : dwp_file->cus;
12566 bfd *dbfd = dwp_file->dbfd.get ();
12567 uint32_t mask = dwp_htab->nr_slots - 1;
12568 uint32_t hash = signature & mask;
12569 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12570 unsigned int i;
12571 void **slot;
12572 struct dwo_unit find_dwo_cu;
12573
12574 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12575 find_dwo_cu.signature = signature;
12576 slot = htab_find_slot (is_debug_types
12577 ? dwp_file->loaded_tus.get ()
12578 : dwp_file->loaded_cus.get (),
12579 &find_dwo_cu, INSERT);
12580
12581 if (*slot != NULL)
12582 return (struct dwo_unit *) *slot;
12583
12584 /* Use a for loop so that we don't loop forever on bad debug info. */
12585 for (i = 0; i < dwp_htab->nr_slots; ++i)
12586 {
12587 ULONGEST signature_in_table;
12588
12589 signature_in_table =
12590 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12591 if (signature_in_table == signature)
12592 {
12593 uint32_t unit_index =
12594 read_4_bytes (dbfd,
12595 dwp_htab->unit_table + hash * sizeof (uint32_t));
12596
12597 if (dwp_file->version == 1)
12598 {
12599 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12600 unit_index, comp_dir,
12601 signature, is_debug_types);
12602 }
12603 else if (dwp_file->version == 2)
12604 {
12605 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12606 unit_index, comp_dir,
12607 signature, is_debug_types);
12608 }
12609 else /* version == 5 */
12610 {
12611 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12612 unit_index, comp_dir,
12613 signature, is_debug_types);
12614 }
12615 return (struct dwo_unit *) *slot;
12616 }
12617 if (signature_in_table == 0)
12618 return NULL;
12619 hash = (hash + hash2) & mask;
12620 }
12621
12622 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12623 " [in module %s]"),
12624 dwp_file->name);
12625 }
12626
12627 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12628 Open the file specified by FILE_NAME and hand it off to BFD for
12629 preliminary analysis. Return a newly initialized bfd *, which
12630 includes a canonicalized copy of FILE_NAME.
12631 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12632 SEARCH_CWD is true if the current directory is to be searched.
12633 It will be searched before debug-file-directory.
12634 If successful, the file is added to the bfd include table of the
12635 objfile's bfd (see gdb_bfd_record_inclusion).
12636 If unable to find/open the file, return NULL.
12637 NOTE: This function is derived from symfile_bfd_open. */
12638
12639 static gdb_bfd_ref_ptr
12640 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12641 const char *file_name, int is_dwp, int search_cwd)
12642 {
12643 int desc;
12644 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12645 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12646 to debug_file_directory. */
12647 const char *search_path;
12648 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12649
12650 gdb::unique_xmalloc_ptr<char> search_path_holder;
12651 if (search_cwd)
12652 {
12653 if (*debug_file_directory != '\0')
12654 {
12655 search_path_holder.reset (concat (".", dirname_separator_string,
12656 debug_file_directory,
12657 (char *) NULL));
12658 search_path = search_path_holder.get ();
12659 }
12660 else
12661 search_path = ".";
12662 }
12663 else
12664 search_path = debug_file_directory;
12665
12666 openp_flags flags = OPF_RETURN_REALPATH;
12667 if (is_dwp)
12668 flags |= OPF_SEARCH_IN_PATH;
12669
12670 gdb::unique_xmalloc_ptr<char> absolute_name;
12671 desc = openp (search_path, flags, file_name,
12672 O_RDONLY | O_BINARY, &absolute_name);
12673 if (desc < 0)
12674 return NULL;
12675
12676 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12677 gnutarget, desc));
12678 if (sym_bfd == NULL)
12679 return NULL;
12680 bfd_set_cacheable (sym_bfd.get (), 1);
12681
12682 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12683 return NULL;
12684
12685 /* Success. Record the bfd as having been included by the objfile's bfd.
12686 This is important because things like demangled_names_hash lives in the
12687 objfile's per_bfd space and may have references to things like symbol
12688 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12689 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12690
12691 return sym_bfd;
12692 }
12693
12694 /* Try to open DWO file FILE_NAME.
12695 COMP_DIR is the DW_AT_comp_dir attribute.
12696 The result is the bfd handle of the file.
12697 If there is a problem finding or opening the file, return NULL.
12698 Upon success, the canonicalized path of the file is stored in the bfd,
12699 same as symfile_bfd_open. */
12700
12701 static gdb_bfd_ref_ptr
12702 open_dwo_file (dwarf2_per_objfile *per_objfile,
12703 const char *file_name, const char *comp_dir)
12704 {
12705 if (IS_ABSOLUTE_PATH (file_name))
12706 return try_open_dwop_file (per_objfile, file_name,
12707 0 /*is_dwp*/, 0 /*search_cwd*/);
12708
12709 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12710
12711 if (comp_dir != NULL)
12712 {
12713 gdb::unique_xmalloc_ptr<char> path_to_try
12714 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12715
12716 /* NOTE: If comp_dir is a relative path, this will also try the
12717 search path, which seems useful. */
12718 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12719 0 /*is_dwp*/,
12720 1 /*search_cwd*/));
12721 if (abfd != NULL)
12722 return abfd;
12723 }
12724
12725 /* That didn't work, try debug-file-directory, which, despite its name,
12726 is a list of paths. */
12727
12728 if (*debug_file_directory == '\0')
12729 return NULL;
12730
12731 return try_open_dwop_file (per_objfile, file_name,
12732 0 /*is_dwp*/, 1 /*search_cwd*/);
12733 }
12734
12735 /* This function is mapped across the sections and remembers the offset and
12736 size of each of the DWO debugging sections we are interested in. */
12737
12738 static void
12739 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12740 dwo_sections *dwo_sections)
12741 {
12742 const struct dwop_section_names *names = &dwop_section_names;
12743
12744 if (section_is_p (sectp->name, &names->abbrev_dwo))
12745 {
12746 dwo_sections->abbrev.s.section = sectp;
12747 dwo_sections->abbrev.size = bfd_section_size (sectp);
12748 }
12749 else if (section_is_p (sectp->name, &names->info_dwo))
12750 {
12751 dwo_sections->info.s.section = sectp;
12752 dwo_sections->info.size = bfd_section_size (sectp);
12753 }
12754 else if (section_is_p (sectp->name, &names->line_dwo))
12755 {
12756 dwo_sections->line.s.section = sectp;
12757 dwo_sections->line.size = bfd_section_size (sectp);
12758 }
12759 else if (section_is_p (sectp->name, &names->loc_dwo))
12760 {
12761 dwo_sections->loc.s.section = sectp;
12762 dwo_sections->loc.size = bfd_section_size (sectp);
12763 }
12764 else if (section_is_p (sectp->name, &names->loclists_dwo))
12765 {
12766 dwo_sections->loclists.s.section = sectp;
12767 dwo_sections->loclists.size = bfd_section_size (sectp);
12768 }
12769 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12770 {
12771 dwo_sections->macinfo.s.section = sectp;
12772 dwo_sections->macinfo.size = bfd_section_size (sectp);
12773 }
12774 else if (section_is_p (sectp->name, &names->macro_dwo))
12775 {
12776 dwo_sections->macro.s.section = sectp;
12777 dwo_sections->macro.size = bfd_section_size (sectp);
12778 }
12779 else if (section_is_p (sectp->name, &names->rnglists_dwo))
12780 {
12781 dwo_sections->rnglists.s.section = sectp;
12782 dwo_sections->rnglists.size = bfd_section_size (sectp);
12783 }
12784 else if (section_is_p (sectp->name, &names->str_dwo))
12785 {
12786 dwo_sections->str.s.section = sectp;
12787 dwo_sections->str.size = bfd_section_size (sectp);
12788 }
12789 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12790 {
12791 dwo_sections->str_offsets.s.section = sectp;
12792 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12793 }
12794 else if (section_is_p (sectp->name, &names->types_dwo))
12795 {
12796 struct dwarf2_section_info type_section;
12797
12798 memset (&type_section, 0, sizeof (type_section));
12799 type_section.s.section = sectp;
12800 type_section.size = bfd_section_size (sectp);
12801 dwo_sections->types.push_back (type_section);
12802 }
12803 }
12804
12805 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12806 by PER_CU. This is for the non-DWP case.
12807 The result is NULL if DWO_NAME can't be found. */
12808
12809 static struct dwo_file *
12810 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12811 const char *comp_dir)
12812 {
12813 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12814
12815 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12816 if (dbfd == NULL)
12817 {
12818 if (dwarf_read_debug)
12819 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12820 return NULL;
12821 }
12822
12823 dwo_file_up dwo_file (new struct dwo_file);
12824 dwo_file->dwo_name = dwo_name;
12825 dwo_file->comp_dir = comp_dir;
12826 dwo_file->dbfd = std::move (dbfd);
12827
12828 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12829 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12830 &dwo_file->sections);
12831
12832 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12833 dwo_file->cus);
12834
12835 if (cu->per_cu->dwarf_version < 5)
12836 {
12837 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12838 dwo_file->sections.types, dwo_file->tus);
12839 }
12840 else
12841 {
12842 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12843 &dwo_file->sections.info, dwo_file->tus,
12844 rcuh_kind::TYPE);
12845 }
12846
12847 if (dwarf_read_debug)
12848 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12849
12850 return dwo_file.release ();
12851 }
12852
12853 /* This function is mapped across the sections and remembers the offset and
12854 size of each of the DWP debugging sections common to version 1 and 2 that
12855 we are interested in. */
12856
12857 static void
12858 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12859 dwp_file *dwp_file)
12860 {
12861 const struct dwop_section_names *names = &dwop_section_names;
12862 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12863
12864 /* Record the ELF section number for later lookup: this is what the
12865 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12866 gdb_assert (elf_section_nr < dwp_file->num_sections);
12867 dwp_file->elf_sections[elf_section_nr] = sectp;
12868
12869 /* Look for specific sections that we need. */
12870 if (section_is_p (sectp->name, &names->str_dwo))
12871 {
12872 dwp_file->sections.str.s.section = sectp;
12873 dwp_file->sections.str.size = bfd_section_size (sectp);
12874 }
12875 else if (section_is_p (sectp->name, &names->cu_index))
12876 {
12877 dwp_file->sections.cu_index.s.section = sectp;
12878 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12879 }
12880 else if (section_is_p (sectp->name, &names->tu_index))
12881 {
12882 dwp_file->sections.tu_index.s.section = sectp;
12883 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12884 }
12885 }
12886
12887 /* This function is mapped across the sections and remembers the offset and
12888 size of each of the DWP version 2 debugging sections that we are interested
12889 in. This is split into a separate function because we don't know if we
12890 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12891
12892 static void
12893 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12894 {
12895 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12896 const struct dwop_section_names *names = &dwop_section_names;
12897 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12898
12899 /* Record the ELF section number for later lookup: this is what the
12900 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12901 gdb_assert (elf_section_nr < dwp_file->num_sections);
12902 dwp_file->elf_sections[elf_section_nr] = sectp;
12903
12904 /* Look for specific sections that we need. */
12905 if (section_is_p (sectp->name, &names->abbrev_dwo))
12906 {
12907 dwp_file->sections.abbrev.s.section = sectp;
12908 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12909 }
12910 else if (section_is_p (sectp->name, &names->info_dwo))
12911 {
12912 dwp_file->sections.info.s.section = sectp;
12913 dwp_file->sections.info.size = bfd_section_size (sectp);
12914 }
12915 else if (section_is_p (sectp->name, &names->line_dwo))
12916 {
12917 dwp_file->sections.line.s.section = sectp;
12918 dwp_file->sections.line.size = bfd_section_size (sectp);
12919 }
12920 else if (section_is_p (sectp->name, &names->loc_dwo))
12921 {
12922 dwp_file->sections.loc.s.section = sectp;
12923 dwp_file->sections.loc.size = bfd_section_size (sectp);
12924 }
12925 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12926 {
12927 dwp_file->sections.macinfo.s.section = sectp;
12928 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12929 }
12930 else if (section_is_p (sectp->name, &names->macro_dwo))
12931 {
12932 dwp_file->sections.macro.s.section = sectp;
12933 dwp_file->sections.macro.size = bfd_section_size (sectp);
12934 }
12935 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12936 {
12937 dwp_file->sections.str_offsets.s.section = sectp;
12938 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12939 }
12940 else if (section_is_p (sectp->name, &names->types_dwo))
12941 {
12942 dwp_file->sections.types.s.section = sectp;
12943 dwp_file->sections.types.size = bfd_section_size (sectp);
12944 }
12945 }
12946
12947 /* This function is mapped across the sections and remembers the offset and
12948 size of each of the DWP version 5 debugging sections that we are interested
12949 in. This is split into a separate function because we don't know if we
12950 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12951
12952 static void
12953 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12954 {
12955 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12956 const struct dwop_section_names *names = &dwop_section_names;
12957 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12958
12959 /* Record the ELF section number for later lookup: this is what the
12960 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12961 gdb_assert (elf_section_nr < dwp_file->num_sections);
12962 dwp_file->elf_sections[elf_section_nr] = sectp;
12963
12964 /* Look for specific sections that we need. */
12965 if (section_is_p (sectp->name, &names->abbrev_dwo))
12966 {
12967 dwp_file->sections.abbrev.s.section = sectp;
12968 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12969 }
12970 else if (section_is_p (sectp->name, &names->info_dwo))
12971 {
12972 dwp_file->sections.info.s.section = sectp;
12973 dwp_file->sections.info.size = bfd_section_size (sectp);
12974 }
12975 else if (section_is_p (sectp->name, &names->line_dwo))
12976 {
12977 dwp_file->sections.line.s.section = sectp;
12978 dwp_file->sections.line.size = bfd_section_size (sectp);
12979 }
12980 else if (section_is_p (sectp->name, &names->loclists_dwo))
12981 {
12982 dwp_file->sections.loclists.s.section = sectp;
12983 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12984 }
12985 else if (section_is_p (sectp->name, &names->macro_dwo))
12986 {
12987 dwp_file->sections.macro.s.section = sectp;
12988 dwp_file->sections.macro.size = bfd_section_size (sectp);
12989 }
12990 else if (section_is_p (sectp->name, &names->rnglists_dwo))
12991 {
12992 dwp_file->sections.rnglists.s.section = sectp;
12993 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12994 }
12995 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12996 {
12997 dwp_file->sections.str_offsets.s.section = sectp;
12998 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12999 }
13000 }
13001
13002 /* Hash function for dwp_file loaded CUs/TUs. */
13003
13004 static hashval_t
13005 hash_dwp_loaded_cutus (const void *item)
13006 {
13007 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13008
13009 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13010 return dwo_unit->signature;
13011 }
13012
13013 /* Equality function for dwp_file loaded CUs/TUs. */
13014
13015 static int
13016 eq_dwp_loaded_cutus (const void *a, const void *b)
13017 {
13018 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13019 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13020
13021 return dua->signature == dub->signature;
13022 }
13023
13024 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13025
13026 static htab_up
13027 allocate_dwp_loaded_cutus_table ()
13028 {
13029 return htab_up (htab_create_alloc (3,
13030 hash_dwp_loaded_cutus,
13031 eq_dwp_loaded_cutus,
13032 NULL, xcalloc, xfree));
13033 }
13034
13035 /* Try to open DWP file FILE_NAME.
13036 The result is the bfd handle of the file.
13037 If there is a problem finding or opening the file, return NULL.
13038 Upon success, the canonicalized path of the file is stored in the bfd,
13039 same as symfile_bfd_open. */
13040
13041 static gdb_bfd_ref_ptr
13042 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
13043 {
13044 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
13045 1 /*is_dwp*/,
13046 1 /*search_cwd*/));
13047 if (abfd != NULL)
13048 return abfd;
13049
13050 /* Work around upstream bug 15652.
13051 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13052 [Whether that's a "bug" is debatable, but it is getting in our way.]
13053 We have no real idea where the dwp file is, because gdb's realpath-ing
13054 of the executable's path may have discarded the needed info.
13055 [IWBN if the dwp file name was recorded in the executable, akin to
13056 .gnu_debuglink, but that doesn't exist yet.]
13057 Strip the directory from FILE_NAME and search again. */
13058 if (*debug_file_directory != '\0')
13059 {
13060 /* Don't implicitly search the current directory here.
13061 If the user wants to search "." to handle this case,
13062 it must be added to debug-file-directory. */
13063 return try_open_dwop_file (per_objfile, lbasename (file_name),
13064 1 /*is_dwp*/,
13065 0 /*search_cwd*/);
13066 }
13067
13068 return NULL;
13069 }
13070
13071 /* Initialize the use of the DWP file for the current objfile.
13072 By convention the name of the DWP file is ${objfile}.dwp.
13073 The result is NULL if it can't be found. */
13074
13075 static std::unique_ptr<struct dwp_file>
13076 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
13077 {
13078 struct objfile *objfile = per_objfile->objfile;
13079
13080 /* Try to find first .dwp for the binary file before any symbolic links
13081 resolving. */
13082
13083 /* If the objfile is a debug file, find the name of the real binary
13084 file and get the name of dwp file from there. */
13085 std::string dwp_name;
13086 if (objfile->separate_debug_objfile_backlink != NULL)
13087 {
13088 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13089 const char *backlink_basename = lbasename (backlink->original_name);
13090
13091 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13092 }
13093 else
13094 dwp_name = objfile->original_name;
13095
13096 dwp_name += ".dwp";
13097
13098 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
13099 if (dbfd == NULL
13100 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13101 {
13102 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13103 dwp_name = objfile_name (objfile);
13104 dwp_name += ".dwp";
13105 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
13106 }
13107
13108 if (dbfd == NULL)
13109 {
13110 if (dwarf_read_debug)
13111 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13112 return std::unique_ptr<dwp_file> ();
13113 }
13114
13115 const char *name = bfd_get_filename (dbfd.get ());
13116 std::unique_ptr<struct dwp_file> dwp_file
13117 (new struct dwp_file (name, std::move (dbfd)));
13118
13119 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13120 dwp_file->elf_sections =
13121 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
13122 dwp_file->num_sections, asection *);
13123
13124 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13125 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13126 dwp_file.get ());
13127
13128 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
13129
13130 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
13131
13132 /* The DWP file version is stored in the hash table. Oh well. */
13133 if (dwp_file->cus && dwp_file->tus
13134 && dwp_file->cus->version != dwp_file->tus->version)
13135 {
13136 /* Technically speaking, we should try to limp along, but this is
13137 pretty bizarre. We use pulongest here because that's the established
13138 portability solution (e.g, we cannot use %u for uint32_t). */
13139 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13140 " TU version %s [in DWP file %s]"),
13141 pulongest (dwp_file->cus->version),
13142 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13143 }
13144
13145 if (dwp_file->cus)
13146 dwp_file->version = dwp_file->cus->version;
13147 else if (dwp_file->tus)
13148 dwp_file->version = dwp_file->tus->version;
13149 else
13150 dwp_file->version = 2;
13151
13152 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13153 {
13154 if (dwp_file->version == 2)
13155 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13156 dwp_file.get ());
13157 else
13158 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13159 dwp_file.get ());
13160 }
13161
13162 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13163 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
13164
13165 if (dwarf_read_debug)
13166 {
13167 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13168 fprintf_unfiltered (gdb_stdlog,
13169 " %s CUs, %s TUs\n",
13170 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13171 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13172 }
13173
13174 return dwp_file;
13175 }
13176
13177 /* Wrapper around open_and_init_dwp_file, only open it once. */
13178
13179 static struct dwp_file *
13180 get_dwp_file (dwarf2_per_objfile *per_objfile)
13181 {
13182 if (!per_objfile->per_bfd->dwp_checked)
13183 {
13184 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13185 per_objfile->per_bfd->dwp_checked = 1;
13186 }
13187 return per_objfile->per_bfd->dwp_file.get ();
13188 }
13189
13190 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13191 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13192 or in the DWP file for the objfile, referenced by THIS_UNIT.
13193 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13194 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13195
13196 This is called, for example, when wanting to read a variable with a
13197 complex location. Therefore we don't want to do file i/o for every call.
13198 Therefore we don't want to look for a DWO file on every call.
13199 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13200 then we check if we've already seen DWO_NAME, and only THEN do we check
13201 for a DWO file.
13202
13203 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13204 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13205
13206 static struct dwo_unit *
13207 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13208 ULONGEST signature, int is_debug_types)
13209 {
13210 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13211 struct objfile *objfile = per_objfile->objfile;
13212 const char *kind = is_debug_types ? "TU" : "CU";
13213 void **dwo_file_slot;
13214 struct dwo_file *dwo_file;
13215 struct dwp_file *dwp_file;
13216
13217 /* First see if there's a DWP file.
13218 If we have a DWP file but didn't find the DWO inside it, don't
13219 look for the original DWO file. It makes gdb behave differently
13220 depending on whether one is debugging in the build tree. */
13221
13222 dwp_file = get_dwp_file (per_objfile);
13223 if (dwp_file != NULL)
13224 {
13225 const struct dwp_hash_table *dwp_htab =
13226 is_debug_types ? dwp_file->tus : dwp_file->cus;
13227
13228 if (dwp_htab != NULL)
13229 {
13230 struct dwo_unit *dwo_cutu =
13231 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13232 is_debug_types);
13233
13234 if (dwo_cutu != NULL)
13235 {
13236 if (dwarf_read_debug)
13237 {
13238 fprintf_unfiltered (gdb_stdlog,
13239 "Virtual DWO %s %s found: @%s\n",
13240 kind, hex_string (signature),
13241 host_address_to_string (dwo_cutu));
13242 }
13243 return dwo_cutu;
13244 }
13245 }
13246 }
13247 else
13248 {
13249 /* No DWP file, look for the DWO file. */
13250
13251 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
13252 if (*dwo_file_slot == NULL)
13253 {
13254 /* Read in the file and build a table of the CUs/TUs it contains. */
13255 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
13256 }
13257 /* NOTE: This will be NULL if unable to open the file. */
13258 dwo_file = (struct dwo_file *) *dwo_file_slot;
13259
13260 if (dwo_file != NULL)
13261 {
13262 struct dwo_unit *dwo_cutu = NULL;
13263
13264 if (is_debug_types && dwo_file->tus)
13265 {
13266 struct dwo_unit find_dwo_cutu;
13267
13268 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13269 find_dwo_cutu.signature = signature;
13270 dwo_cutu
13271 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13272 &find_dwo_cutu);
13273 }
13274 else if (!is_debug_types && dwo_file->cus)
13275 {
13276 struct dwo_unit find_dwo_cutu;
13277
13278 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13279 find_dwo_cutu.signature = signature;
13280 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
13281 &find_dwo_cutu);
13282 }
13283
13284 if (dwo_cutu != NULL)
13285 {
13286 if (dwarf_read_debug)
13287 {
13288 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13289 kind, dwo_name, hex_string (signature),
13290 host_address_to_string (dwo_cutu));
13291 }
13292 return dwo_cutu;
13293 }
13294 }
13295 }
13296
13297 /* We didn't find it. This could mean a dwo_id mismatch, or
13298 someone deleted the DWO/DWP file, or the search path isn't set up
13299 correctly to find the file. */
13300
13301 if (dwarf_read_debug)
13302 {
13303 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13304 kind, dwo_name, hex_string (signature));
13305 }
13306
13307 /* This is a warning and not a complaint because it can be caused by
13308 pilot error (e.g., user accidentally deleting the DWO). */
13309 {
13310 /* Print the name of the DWP file if we looked there, helps the user
13311 better diagnose the problem. */
13312 std::string dwp_text;
13313
13314 if (dwp_file != NULL)
13315 dwp_text = string_printf (" [in DWP file %s]",
13316 lbasename (dwp_file->name));
13317
13318 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13319 " [in module %s]"),
13320 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13321 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
13322 }
13323 return NULL;
13324 }
13325
13326 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13327 See lookup_dwo_cutu_unit for details. */
13328
13329 static struct dwo_unit *
13330 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13331 ULONGEST signature)
13332 {
13333 gdb_assert (!cu->per_cu->is_debug_types);
13334
13335 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
13336 }
13337
13338 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13339 See lookup_dwo_cutu_unit for details. */
13340
13341 static struct dwo_unit *
13342 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
13343 {
13344 gdb_assert (cu->per_cu->is_debug_types);
13345
13346 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13347
13348 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
13349 }
13350
13351 /* Traversal function for queue_and_load_all_dwo_tus. */
13352
13353 static int
13354 queue_and_load_dwo_tu (void **slot, void *info)
13355 {
13356 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13357 dwarf2_cu *cu = (dwarf2_cu *) info;
13358 ULONGEST signature = dwo_unit->signature;
13359 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
13360
13361 if (sig_type != NULL)
13362 {
13363 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13364
13365 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13366 a real dependency of PER_CU on SIG_TYPE. That is detected later
13367 while processing PER_CU. */
13368 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
13369 load_full_type_unit (sig_cu, cu->per_objfile);
13370 cu->per_cu->imported_symtabs_push (sig_cu);
13371 }
13372
13373 return 1;
13374 }
13375
13376 /* Queue all TUs contained in the DWO of CU to be read in.
13377 The DWO may have the only definition of the type, though it may not be
13378 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13379 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13380
13381 static void
13382 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
13383 {
13384 struct dwo_unit *dwo_unit;
13385 struct dwo_file *dwo_file;
13386
13387 gdb_assert (cu != nullptr);
13388 gdb_assert (!cu->per_cu->is_debug_types);
13389 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
13390
13391 dwo_unit = cu->dwo_unit;
13392 gdb_assert (dwo_unit != NULL);
13393
13394 dwo_file = dwo_unit->dwo_file;
13395 if (dwo_file->tus != NULL)
13396 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
13397 }
13398
13399 /* Read in various DIEs. */
13400
13401 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13402 Inherit only the children of the DW_AT_abstract_origin DIE not being
13403 already referenced by DW_AT_abstract_origin from the children of the
13404 current DIE. */
13405
13406 static void
13407 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13408 {
13409 struct die_info *child_die;
13410 sect_offset *offsetp;
13411 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13412 struct die_info *origin_die;
13413 /* Iterator of the ORIGIN_DIE children. */
13414 struct die_info *origin_child_die;
13415 struct attribute *attr;
13416 struct dwarf2_cu *origin_cu;
13417 struct pending **origin_previous_list_in_scope;
13418
13419 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13420 if (!attr)
13421 return;
13422
13423 /* Note that following die references may follow to a die in a
13424 different cu. */
13425
13426 origin_cu = cu;
13427 origin_die = follow_die_ref (die, attr, &origin_cu);
13428
13429 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13430 symbols in. */
13431 origin_previous_list_in_scope = origin_cu->list_in_scope;
13432 origin_cu->list_in_scope = cu->list_in_scope;
13433
13434 if (die->tag != origin_die->tag
13435 && !(die->tag == DW_TAG_inlined_subroutine
13436 && origin_die->tag == DW_TAG_subprogram))
13437 complaint (_("DIE %s and its abstract origin %s have different tags"),
13438 sect_offset_str (die->sect_off),
13439 sect_offset_str (origin_die->sect_off));
13440
13441 std::vector<sect_offset> offsets;
13442
13443 for (child_die = die->child;
13444 child_die && child_die->tag;
13445 child_die = child_die->sibling)
13446 {
13447 struct die_info *child_origin_die;
13448 struct dwarf2_cu *child_origin_cu;
13449
13450 /* We are trying to process concrete instance entries:
13451 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13452 it's not relevant to our analysis here. i.e. detecting DIEs that are
13453 present in the abstract instance but not referenced in the concrete
13454 one. */
13455 if (child_die->tag == DW_TAG_call_site
13456 || child_die->tag == DW_TAG_GNU_call_site)
13457 continue;
13458
13459 /* For each CHILD_DIE, find the corresponding child of
13460 ORIGIN_DIE. If there is more than one layer of
13461 DW_AT_abstract_origin, follow them all; there shouldn't be,
13462 but GCC versions at least through 4.4 generate this (GCC PR
13463 40573). */
13464 child_origin_die = child_die;
13465 child_origin_cu = cu;
13466 while (1)
13467 {
13468 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13469 child_origin_cu);
13470 if (attr == NULL)
13471 break;
13472 child_origin_die = follow_die_ref (child_origin_die, attr,
13473 &child_origin_cu);
13474 }
13475
13476 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13477 counterpart may exist. */
13478 if (child_origin_die != child_die)
13479 {
13480 if (child_die->tag != child_origin_die->tag
13481 && !(child_die->tag == DW_TAG_inlined_subroutine
13482 && child_origin_die->tag == DW_TAG_subprogram))
13483 complaint (_("Child DIE %s and its abstract origin %s have "
13484 "different tags"),
13485 sect_offset_str (child_die->sect_off),
13486 sect_offset_str (child_origin_die->sect_off));
13487 if (child_origin_die->parent != origin_die)
13488 complaint (_("Child DIE %s and its abstract origin %s have "
13489 "different parents"),
13490 sect_offset_str (child_die->sect_off),
13491 sect_offset_str (child_origin_die->sect_off));
13492 else
13493 offsets.push_back (child_origin_die->sect_off);
13494 }
13495 }
13496 std::sort (offsets.begin (), offsets.end ());
13497 sect_offset *offsets_end = offsets.data () + offsets.size ();
13498 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13499 if (offsetp[-1] == *offsetp)
13500 complaint (_("Multiple children of DIE %s refer "
13501 "to DIE %s as their abstract origin"),
13502 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13503
13504 offsetp = offsets.data ();
13505 origin_child_die = origin_die->child;
13506 while (origin_child_die && origin_child_die->tag)
13507 {
13508 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13509 while (offsetp < offsets_end
13510 && *offsetp < origin_child_die->sect_off)
13511 offsetp++;
13512 if (offsetp >= offsets_end
13513 || *offsetp > origin_child_die->sect_off)
13514 {
13515 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13516 Check whether we're already processing ORIGIN_CHILD_DIE.
13517 This can happen with mutually referenced abstract_origins.
13518 PR 16581. */
13519 if (!origin_child_die->in_process)
13520 process_die (origin_child_die, origin_cu);
13521 }
13522 origin_child_die = origin_child_die->sibling;
13523 }
13524 origin_cu->list_in_scope = origin_previous_list_in_scope;
13525
13526 if (cu != origin_cu)
13527 compute_delayed_physnames (origin_cu);
13528 }
13529
13530 static void
13531 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13532 {
13533 struct objfile *objfile = cu->per_objfile->objfile;
13534 struct gdbarch *gdbarch = objfile->arch ();
13535 struct context_stack *newobj;
13536 CORE_ADDR lowpc;
13537 CORE_ADDR highpc;
13538 struct die_info *child_die;
13539 struct attribute *attr, *call_line, *call_file;
13540 const char *name;
13541 CORE_ADDR baseaddr;
13542 struct block *block;
13543 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13544 std::vector<struct symbol *> template_args;
13545 struct template_symbol *templ_func = NULL;
13546
13547 if (inlined_func)
13548 {
13549 /* If we do not have call site information, we can't show the
13550 caller of this inlined function. That's too confusing, so
13551 only use the scope for local variables. */
13552 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13553 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13554 if (call_line == NULL || call_file == NULL)
13555 {
13556 read_lexical_block_scope (die, cu);
13557 return;
13558 }
13559 }
13560
13561 baseaddr = objfile->text_section_offset ();
13562
13563 name = dwarf2_name (die, cu);
13564
13565 /* Ignore functions with missing or empty names. These are actually
13566 illegal according to the DWARF standard. */
13567 if (name == NULL)
13568 {
13569 complaint (_("missing name for subprogram DIE at %s"),
13570 sect_offset_str (die->sect_off));
13571 return;
13572 }
13573
13574 /* Ignore functions with missing or invalid low and high pc attributes. */
13575 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13576 <= PC_BOUNDS_INVALID)
13577 {
13578 attr = dwarf2_attr (die, DW_AT_external, cu);
13579 if (attr == nullptr || !attr->as_boolean ())
13580 complaint (_("cannot get low and high bounds "
13581 "for subprogram DIE at %s"),
13582 sect_offset_str (die->sect_off));
13583 return;
13584 }
13585
13586 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13587 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13588
13589 /* If we have any template arguments, then we must allocate a
13590 different sort of symbol. */
13591 for (child_die = die->child; child_die; child_die = child_die->sibling)
13592 {
13593 if (child_die->tag == DW_TAG_template_type_param
13594 || child_die->tag == DW_TAG_template_value_param)
13595 {
13596 templ_func = new (&objfile->objfile_obstack) template_symbol;
13597 templ_func->subclass = SYMBOL_TEMPLATE;
13598 break;
13599 }
13600 }
13601
13602 newobj = cu->get_builder ()->push_context (0, lowpc);
13603 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13604 (struct symbol *) templ_func);
13605
13606 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13607 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13608 cu->language);
13609
13610 /* If there is a location expression for DW_AT_frame_base, record
13611 it. */
13612 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13613 if (attr != nullptr)
13614 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13615
13616 /* If there is a location for the static link, record it. */
13617 newobj->static_link = NULL;
13618 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13619 if (attr != nullptr)
13620 {
13621 newobj->static_link
13622 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13623 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13624 cu->addr_type ());
13625 }
13626
13627 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13628
13629 if (die->child != NULL)
13630 {
13631 child_die = die->child;
13632 while (child_die && child_die->tag)
13633 {
13634 if (child_die->tag == DW_TAG_template_type_param
13635 || child_die->tag == DW_TAG_template_value_param)
13636 {
13637 struct symbol *arg = new_symbol (child_die, NULL, cu);
13638
13639 if (arg != NULL)
13640 template_args.push_back (arg);
13641 }
13642 else
13643 process_die (child_die, cu);
13644 child_die = child_die->sibling;
13645 }
13646 }
13647
13648 inherit_abstract_dies (die, cu);
13649
13650 /* If we have a DW_AT_specification, we might need to import using
13651 directives from the context of the specification DIE. See the
13652 comment in determine_prefix. */
13653 if (cu->language == language_cplus
13654 && dwarf2_attr (die, DW_AT_specification, cu))
13655 {
13656 struct dwarf2_cu *spec_cu = cu;
13657 struct die_info *spec_die = die_specification (die, &spec_cu);
13658
13659 while (spec_die)
13660 {
13661 child_die = spec_die->child;
13662 while (child_die && child_die->tag)
13663 {
13664 if (child_die->tag == DW_TAG_imported_module)
13665 process_die (child_die, spec_cu);
13666 child_die = child_die->sibling;
13667 }
13668
13669 /* In some cases, GCC generates specification DIEs that
13670 themselves contain DW_AT_specification attributes. */
13671 spec_die = die_specification (spec_die, &spec_cu);
13672 }
13673 }
13674
13675 struct context_stack cstk = cu->get_builder ()->pop_context ();
13676 /* Make a block for the local symbols within. */
13677 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13678 cstk.static_link, lowpc, highpc);
13679
13680 /* For C++, set the block's scope. */
13681 if ((cu->language == language_cplus
13682 || cu->language == language_fortran
13683 || cu->language == language_d
13684 || cu->language == language_rust)
13685 && cu->processing_has_namespace_info)
13686 block_set_scope (block, determine_prefix (die, cu),
13687 &objfile->objfile_obstack);
13688
13689 /* If we have address ranges, record them. */
13690 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13691
13692 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13693
13694 /* Attach template arguments to function. */
13695 if (!template_args.empty ())
13696 {
13697 gdb_assert (templ_func != NULL);
13698
13699 templ_func->n_template_arguments = template_args.size ();
13700 templ_func->template_arguments
13701 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13702 templ_func->n_template_arguments);
13703 memcpy (templ_func->template_arguments,
13704 template_args.data (),
13705 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13706
13707 /* Make sure that the symtab is set on the new symbols. Even
13708 though they don't appear in this symtab directly, other parts
13709 of gdb assume that symbols do, and this is reasonably
13710 true. */
13711 for (symbol *sym : template_args)
13712 symbol_set_symtab (sym, symbol_symtab (templ_func));
13713 }
13714
13715 /* In C++, we can have functions nested inside functions (e.g., when
13716 a function declares a class that has methods). This means that
13717 when we finish processing a function scope, we may need to go
13718 back to building a containing block's symbol lists. */
13719 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13720 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13721
13722 /* If we've finished processing a top-level function, subsequent
13723 symbols go in the file symbol list. */
13724 if (cu->get_builder ()->outermost_context_p ())
13725 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13726 }
13727
13728 /* Process all the DIES contained within a lexical block scope. Start
13729 a new scope, process the dies, and then close the scope. */
13730
13731 static void
13732 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13733 {
13734 struct objfile *objfile = cu->per_objfile->objfile;
13735 struct gdbarch *gdbarch = objfile->arch ();
13736 CORE_ADDR lowpc, highpc;
13737 struct die_info *child_die;
13738 CORE_ADDR baseaddr;
13739
13740 baseaddr = objfile->text_section_offset ();
13741
13742 /* Ignore blocks with missing or invalid low and high pc attributes. */
13743 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13744 as multiple lexical blocks? Handling children in a sane way would
13745 be nasty. Might be easier to properly extend generic blocks to
13746 describe ranges. */
13747 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13748 {
13749 case PC_BOUNDS_NOT_PRESENT:
13750 /* DW_TAG_lexical_block has no attributes, process its children as if
13751 there was no wrapping by that DW_TAG_lexical_block.
13752 GCC does no longer produces such DWARF since GCC r224161. */
13753 for (child_die = die->child;
13754 child_die != NULL && child_die->tag;
13755 child_die = child_die->sibling)
13756 {
13757 /* We might already be processing this DIE. This can happen
13758 in an unusual circumstance -- where a subroutine A
13759 appears lexically in another subroutine B, but A actually
13760 inlines B. The recursion is broken here, rather than in
13761 inherit_abstract_dies, because it seems better to simply
13762 drop concrete children here. */
13763 if (!child_die->in_process)
13764 process_die (child_die, cu);
13765 }
13766 return;
13767 case PC_BOUNDS_INVALID:
13768 return;
13769 }
13770 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13771 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13772
13773 cu->get_builder ()->push_context (0, lowpc);
13774 if (die->child != NULL)
13775 {
13776 child_die = die->child;
13777 while (child_die && child_die->tag)
13778 {
13779 process_die (child_die, cu);
13780 child_die = child_die->sibling;
13781 }
13782 }
13783 inherit_abstract_dies (die, cu);
13784 struct context_stack cstk = cu->get_builder ()->pop_context ();
13785
13786 if (*cu->get_builder ()->get_local_symbols () != NULL
13787 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13788 {
13789 struct block *block
13790 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13791 cstk.start_addr, highpc);
13792
13793 /* Note that recording ranges after traversing children, as we
13794 do here, means that recording a parent's ranges entails
13795 walking across all its children's ranges as they appear in
13796 the address map, which is quadratic behavior.
13797
13798 It would be nicer to record the parent's ranges before
13799 traversing its children, simply overriding whatever you find
13800 there. But since we don't even decide whether to create a
13801 block until after we've traversed its children, that's hard
13802 to do. */
13803 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13804 }
13805 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13806 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13807 }
13808
13809 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13810
13811 static void
13812 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13813 {
13814 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13815 struct objfile *objfile = per_objfile->objfile;
13816 struct gdbarch *gdbarch = objfile->arch ();
13817 CORE_ADDR pc, baseaddr;
13818 struct attribute *attr;
13819 struct call_site *call_site, call_site_local;
13820 void **slot;
13821 int nparams;
13822 struct die_info *child_die;
13823
13824 baseaddr = objfile->text_section_offset ();
13825
13826 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13827 if (attr == NULL)
13828 {
13829 /* This was a pre-DWARF-5 GNU extension alias
13830 for DW_AT_call_return_pc. */
13831 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13832 }
13833 if (!attr)
13834 {
13835 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13836 "DIE %s [in module %s]"),
13837 sect_offset_str (die->sect_off), objfile_name (objfile));
13838 return;
13839 }
13840 pc = attr->as_address () + baseaddr;
13841 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13842
13843 if (cu->call_site_htab == NULL)
13844 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13845 NULL, &objfile->objfile_obstack,
13846 hashtab_obstack_allocate, NULL);
13847 call_site_local.pc = pc;
13848 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13849 if (*slot != NULL)
13850 {
13851 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13852 "DIE %s [in module %s]"),
13853 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13854 objfile_name (objfile));
13855 return;
13856 }
13857
13858 /* Count parameters at the caller. */
13859
13860 nparams = 0;
13861 for (child_die = die->child; child_die && child_die->tag;
13862 child_die = child_die->sibling)
13863 {
13864 if (child_die->tag != DW_TAG_call_site_parameter
13865 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13866 {
13867 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13868 "DW_TAG_call_site child DIE %s [in module %s]"),
13869 child_die->tag, sect_offset_str (child_die->sect_off),
13870 objfile_name (objfile));
13871 continue;
13872 }
13873
13874 nparams++;
13875 }
13876
13877 call_site
13878 = ((struct call_site *)
13879 obstack_alloc (&objfile->objfile_obstack,
13880 sizeof (*call_site)
13881 + (sizeof (*call_site->parameter) * (nparams - 1))));
13882 *slot = call_site;
13883 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13884 call_site->pc = pc;
13885
13886 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13887 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13888 {
13889 struct die_info *func_die;
13890
13891 /* Skip also over DW_TAG_inlined_subroutine. */
13892 for (func_die = die->parent;
13893 func_die && func_die->tag != DW_TAG_subprogram
13894 && func_die->tag != DW_TAG_subroutine_type;
13895 func_die = func_die->parent);
13896
13897 /* DW_AT_call_all_calls is a superset
13898 of DW_AT_call_all_tail_calls. */
13899 if (func_die
13900 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13901 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13902 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13903 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13904 {
13905 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13906 not complete. But keep CALL_SITE for look ups via call_site_htab,
13907 both the initial caller containing the real return address PC and
13908 the final callee containing the current PC of a chain of tail
13909 calls do not need to have the tail call list complete. But any
13910 function candidate for a virtual tail call frame searched via
13911 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13912 determined unambiguously. */
13913 }
13914 else
13915 {
13916 struct type *func_type = NULL;
13917
13918 if (func_die)
13919 func_type = get_die_type (func_die, cu);
13920 if (func_type != NULL)
13921 {
13922 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13923
13924 /* Enlist this call site to the function. */
13925 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13926 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13927 }
13928 else
13929 complaint (_("Cannot find function owning DW_TAG_call_site "
13930 "DIE %s [in module %s]"),
13931 sect_offset_str (die->sect_off), objfile_name (objfile));
13932 }
13933 }
13934
13935 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13936 if (attr == NULL)
13937 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13938 if (attr == NULL)
13939 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13940 if (attr == NULL)
13941 {
13942 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13943 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13944 }
13945 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13946 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
13947 /* Keep NULL DWARF_BLOCK. */;
13948 else if (attr->form_is_block ())
13949 {
13950 struct dwarf2_locexpr_baton *dlbaton;
13951 struct dwarf_block *block = attr->as_block ();
13952
13953 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13954 dlbaton->data = block->data;
13955 dlbaton->size = block->size;
13956 dlbaton->per_objfile = per_objfile;
13957 dlbaton->per_cu = cu->per_cu;
13958
13959 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13960 }
13961 else if (attr->form_is_ref ())
13962 {
13963 struct dwarf2_cu *target_cu = cu;
13964 struct die_info *target_die;
13965
13966 target_die = follow_die_ref (die, attr, &target_cu);
13967 gdb_assert (target_cu->per_objfile->objfile == objfile);
13968 if (die_is_declaration (target_die, target_cu))
13969 {
13970 const char *target_physname;
13971
13972 /* Prefer the mangled name; otherwise compute the demangled one. */
13973 target_physname = dw2_linkage_name (target_die, target_cu);
13974 if (target_physname == NULL)
13975 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13976 if (target_physname == NULL)
13977 complaint (_("DW_AT_call_target target DIE has invalid "
13978 "physname, for referencing DIE %s [in module %s]"),
13979 sect_offset_str (die->sect_off), objfile_name (objfile));
13980 else
13981 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13982 }
13983 else
13984 {
13985 CORE_ADDR lowpc;
13986
13987 /* DW_AT_entry_pc should be preferred. */
13988 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13989 <= PC_BOUNDS_INVALID)
13990 complaint (_("DW_AT_call_target target DIE has invalid "
13991 "low pc, for referencing DIE %s [in module %s]"),
13992 sect_offset_str (die->sect_off), objfile_name (objfile));
13993 else
13994 {
13995 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13996 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13997 }
13998 }
13999 }
14000 else
14001 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14002 "block nor reference, for DIE %s [in module %s]"),
14003 sect_offset_str (die->sect_off), objfile_name (objfile));
14004
14005 call_site->per_cu = cu->per_cu;
14006 call_site->per_objfile = per_objfile;
14007
14008 for (child_die = die->child;
14009 child_die && child_die->tag;
14010 child_die = child_die->sibling)
14011 {
14012 struct call_site_parameter *parameter;
14013 struct attribute *loc, *origin;
14014
14015 if (child_die->tag != DW_TAG_call_site_parameter
14016 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14017 {
14018 /* Already printed the complaint above. */
14019 continue;
14020 }
14021
14022 gdb_assert (call_site->parameter_count < nparams);
14023 parameter = &call_site->parameter[call_site->parameter_count];
14024
14025 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14026 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14027 register is contained in DW_AT_call_value. */
14028
14029 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14030 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14031 if (origin == NULL)
14032 {
14033 /* This was a pre-DWARF-5 GNU extension alias
14034 for DW_AT_call_parameter. */
14035 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14036 }
14037 if (loc == NULL && origin != NULL && origin->form_is_ref ())
14038 {
14039 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14040
14041 sect_offset sect_off = origin->get_ref_die_offset ();
14042 if (!cu->header.offset_in_cu_p (sect_off))
14043 {
14044 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14045 binding can be done only inside one CU. Such referenced DIE
14046 therefore cannot be even moved to DW_TAG_partial_unit. */
14047 complaint (_("DW_AT_call_parameter offset is not in CU for "
14048 "DW_TAG_call_site child DIE %s [in module %s]"),
14049 sect_offset_str (child_die->sect_off),
14050 objfile_name (objfile));
14051 continue;
14052 }
14053 parameter->u.param_cu_off
14054 = (cu_offset) (sect_off - cu->header.sect_off);
14055 }
14056 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
14057 {
14058 complaint (_("No DW_FORM_block* DW_AT_location for "
14059 "DW_TAG_call_site child DIE %s [in module %s]"),
14060 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14061 continue;
14062 }
14063 else
14064 {
14065 struct dwarf_block *block = loc->as_block ();
14066
14067 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14068 (block->data, &block->data[block->size]);
14069 if (parameter->u.dwarf_reg != -1)
14070 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14071 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14072 &block->data[block->size],
14073 &parameter->u.fb_offset))
14074 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14075 else
14076 {
14077 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14078 "for DW_FORM_block* DW_AT_location is supported for "
14079 "DW_TAG_call_site child DIE %s "
14080 "[in module %s]"),
14081 sect_offset_str (child_die->sect_off),
14082 objfile_name (objfile));
14083 continue;
14084 }
14085 }
14086
14087 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14088 if (attr == NULL)
14089 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14090 if (attr == NULL || !attr->form_is_block ())
14091 {
14092 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14093 "DW_TAG_call_site child DIE %s [in module %s]"),
14094 sect_offset_str (child_die->sect_off),
14095 objfile_name (objfile));
14096 continue;
14097 }
14098
14099 struct dwarf_block *block = attr->as_block ();
14100 parameter->value = block->data;
14101 parameter->value_size = block->size;
14102
14103 /* Parameters are not pre-cleared by memset above. */
14104 parameter->data_value = NULL;
14105 parameter->data_value_size = 0;
14106 call_site->parameter_count++;
14107
14108 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14109 if (attr == NULL)
14110 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14111 if (attr != nullptr)
14112 {
14113 if (!attr->form_is_block ())
14114 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14115 "DW_TAG_call_site child DIE %s [in module %s]"),
14116 sect_offset_str (child_die->sect_off),
14117 objfile_name (objfile));
14118 else
14119 {
14120 block = attr->as_block ();
14121 parameter->data_value = block->data;
14122 parameter->data_value_size = block->size;
14123 }
14124 }
14125 }
14126 }
14127
14128 /* Helper function for read_variable. If DIE represents a virtual
14129 table, then return the type of the concrete object that is
14130 associated with the virtual table. Otherwise, return NULL. */
14131
14132 static struct type *
14133 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14134 {
14135 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14136 if (attr == NULL)
14137 return NULL;
14138
14139 /* Find the type DIE. */
14140 struct die_info *type_die = NULL;
14141 struct dwarf2_cu *type_cu = cu;
14142
14143 if (attr->form_is_ref ())
14144 type_die = follow_die_ref (die, attr, &type_cu);
14145 if (type_die == NULL)
14146 return NULL;
14147
14148 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14149 return NULL;
14150 return die_containing_type (type_die, type_cu);
14151 }
14152
14153 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14154
14155 static void
14156 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14157 {
14158 struct rust_vtable_symbol *storage = NULL;
14159
14160 if (cu->language == language_rust)
14161 {
14162 struct type *containing_type = rust_containing_type (die, cu);
14163
14164 if (containing_type != NULL)
14165 {
14166 struct objfile *objfile = cu->per_objfile->objfile;
14167
14168 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
14169 storage->concrete_type = containing_type;
14170 storage->subclass = SYMBOL_RUST_VTABLE;
14171 }
14172 }
14173
14174 struct symbol *res = new_symbol (die, NULL, cu, storage);
14175 struct attribute *abstract_origin
14176 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14177 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14178 if (res == NULL && loc && abstract_origin)
14179 {
14180 /* We have a variable without a name, but with a location and an abstract
14181 origin. This may be a concrete instance of an abstract variable
14182 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14183 later. */
14184 struct dwarf2_cu *origin_cu = cu;
14185 struct die_info *origin_die
14186 = follow_die_ref (die, abstract_origin, &origin_cu);
14187 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14188 per_objfile->per_bfd->abstract_to_concrete
14189 [origin_die->sect_off].push_back (die->sect_off);
14190 }
14191 }
14192
14193 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14194 reading .debug_rnglists.
14195 Callback's type should be:
14196 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14197 Return true if the attributes are present and valid, otherwise,
14198 return false. */
14199
14200 template <typename Callback>
14201 static bool
14202 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14203 dwarf_tag tag, Callback &&callback)
14204 {
14205 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14206 struct objfile *objfile = per_objfile->objfile;
14207 bfd *obfd = objfile->obfd;
14208 /* Base address selection entry. */
14209 gdb::optional<CORE_ADDR> base;
14210 const gdb_byte *buffer;
14211 CORE_ADDR baseaddr;
14212 bool overflow = false;
14213 ULONGEST addr_index;
14214 struct dwarf2_section_info *rnglists_section;
14215
14216 base = cu->base_address;
14217 rnglists_section = cu_debug_rnglists_section (cu, tag);
14218 rnglists_section->read (objfile);
14219
14220 if (offset >= rnglists_section->size)
14221 {
14222 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14223 offset);
14224 return false;
14225 }
14226 buffer = rnglists_section->buffer + offset;
14227
14228 baseaddr = objfile->text_section_offset ();
14229
14230 while (1)
14231 {
14232 /* Initialize it due to a false compiler warning. */
14233 CORE_ADDR range_beginning = 0, range_end = 0;
14234 const gdb_byte *buf_end = (rnglists_section->buffer
14235 + rnglists_section->size);
14236 unsigned int bytes_read;
14237
14238 if (buffer == buf_end)
14239 {
14240 overflow = true;
14241 break;
14242 }
14243 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14244 switch (rlet)
14245 {
14246 case DW_RLE_end_of_list:
14247 break;
14248 case DW_RLE_base_address:
14249 if (buffer + cu->header.addr_size > buf_end)
14250 {
14251 overflow = true;
14252 break;
14253 }
14254 base = cu->header.read_address (obfd, buffer, &bytes_read);
14255 buffer += bytes_read;
14256 break;
14257 case DW_RLE_base_addressx:
14258 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14259 buffer += bytes_read;
14260 base = read_addr_index (cu, addr_index);
14261 break;
14262 case DW_RLE_start_length:
14263 if (buffer + cu->header.addr_size > buf_end)
14264 {
14265 overflow = true;
14266 break;
14267 }
14268 range_beginning = cu->header.read_address (obfd, buffer,
14269 &bytes_read);
14270 buffer += bytes_read;
14271 range_end = (range_beginning
14272 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14273 buffer += bytes_read;
14274 if (buffer > buf_end)
14275 {
14276 overflow = true;
14277 break;
14278 }
14279 break;
14280 case DW_RLE_startx_length:
14281 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14282 buffer += bytes_read;
14283 range_beginning = read_addr_index (cu, addr_index);
14284 if (buffer > buf_end)
14285 {
14286 overflow = true;
14287 break;
14288 }
14289 range_end = (range_beginning
14290 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14291 buffer += bytes_read;
14292 break;
14293 case DW_RLE_offset_pair:
14294 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14295 buffer += bytes_read;
14296 if (buffer > buf_end)
14297 {
14298 overflow = true;
14299 break;
14300 }
14301 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14302 buffer += bytes_read;
14303 if (buffer > buf_end)
14304 {
14305 overflow = true;
14306 break;
14307 }
14308 break;
14309 case DW_RLE_start_end:
14310 if (buffer + 2 * cu->header.addr_size > buf_end)
14311 {
14312 overflow = true;
14313 break;
14314 }
14315 range_beginning = cu->header.read_address (obfd, buffer,
14316 &bytes_read);
14317 buffer += bytes_read;
14318 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
14319 buffer += bytes_read;
14320 break;
14321 case DW_RLE_startx_endx:
14322 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14323 buffer += bytes_read;
14324 range_beginning = read_addr_index (cu, addr_index);
14325 if (buffer > buf_end)
14326 {
14327 overflow = true;
14328 break;
14329 }
14330 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14331 buffer += bytes_read;
14332 range_end = read_addr_index (cu, addr_index);
14333 break;
14334 default:
14335 complaint (_("Invalid .debug_rnglists data (no base address)"));
14336 return false;
14337 }
14338 if (rlet == DW_RLE_end_of_list || overflow)
14339 break;
14340 if (rlet == DW_RLE_base_address)
14341 continue;
14342
14343 if (range_beginning > range_end)
14344 {
14345 /* Inverted range entries are invalid. */
14346 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14347 return false;
14348 }
14349
14350 /* Empty range entries have no effect. */
14351 if (range_beginning == range_end)
14352 continue;
14353
14354 /* Only DW_RLE_offset_pair needs the base address added. */
14355 if (rlet == DW_RLE_offset_pair)
14356 {
14357 if (!base.has_value ())
14358 {
14359 /* We have no valid base address for the DW_RLE_offset_pair. */
14360 complaint (_("Invalid .debug_rnglists data (no base address for "
14361 "DW_RLE_offset_pair)"));
14362 return false;
14363 }
14364
14365 range_beginning += *base;
14366 range_end += *base;
14367 }
14368
14369 /* A not-uncommon case of bad debug info.
14370 Don't pollute the addrmap with bad data. */
14371 if (range_beginning + baseaddr == 0
14372 && !per_objfile->per_bfd->has_section_at_zero)
14373 {
14374 complaint (_(".debug_rnglists entry has start address of zero"
14375 " [in module %s]"), objfile_name (objfile));
14376 continue;
14377 }
14378
14379 callback (range_beginning, range_end);
14380 }
14381
14382 if (overflow)
14383 {
14384 complaint (_("Offset %d is not terminated "
14385 "for DW_AT_ranges attribute"),
14386 offset);
14387 return false;
14388 }
14389
14390 return true;
14391 }
14392
14393 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14394 Callback's type should be:
14395 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14396 Return 1 if the attributes are present and valid, otherwise, return 0. */
14397
14398 template <typename Callback>
14399 static int
14400 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
14401 Callback &&callback)
14402 {
14403 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14404 struct objfile *objfile = per_objfile->objfile;
14405 struct comp_unit_head *cu_header = &cu->header;
14406 bfd *obfd = objfile->obfd;
14407 unsigned int addr_size = cu_header->addr_size;
14408 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14409 /* Base address selection entry. */
14410 gdb::optional<CORE_ADDR> base;
14411 unsigned int dummy;
14412 const gdb_byte *buffer;
14413 CORE_ADDR baseaddr;
14414
14415 if (cu_header->version >= 5)
14416 return dwarf2_rnglists_process (offset, cu, tag, callback);
14417
14418 base = cu->base_address;
14419
14420 per_objfile->per_bfd->ranges.read (objfile);
14421 if (offset >= per_objfile->per_bfd->ranges.size)
14422 {
14423 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14424 offset);
14425 return 0;
14426 }
14427 buffer = per_objfile->per_bfd->ranges.buffer + offset;
14428
14429 baseaddr = objfile->text_section_offset ();
14430
14431 while (1)
14432 {
14433 CORE_ADDR range_beginning, range_end;
14434
14435 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
14436 buffer += addr_size;
14437 range_end = cu->header.read_address (obfd, buffer, &dummy);
14438 buffer += addr_size;
14439 offset += 2 * addr_size;
14440
14441 /* An end of list marker is a pair of zero addresses. */
14442 if (range_beginning == 0 && range_end == 0)
14443 /* Found the end of list entry. */
14444 break;
14445
14446 /* Each base address selection entry is a pair of 2 values.
14447 The first is the largest possible address, the second is
14448 the base address. Check for a base address here. */
14449 if ((range_beginning & mask) == mask)
14450 {
14451 /* If we found the largest possible address, then we already
14452 have the base address in range_end. */
14453 base = range_end;
14454 continue;
14455 }
14456
14457 if (!base.has_value ())
14458 {
14459 /* We have no valid base address for the ranges
14460 data. */
14461 complaint (_("Invalid .debug_ranges data (no base address)"));
14462 return 0;
14463 }
14464
14465 if (range_beginning > range_end)
14466 {
14467 /* Inverted range entries are invalid. */
14468 complaint (_("Invalid .debug_ranges data (inverted range)"));
14469 return 0;
14470 }
14471
14472 /* Empty range entries have no effect. */
14473 if (range_beginning == range_end)
14474 continue;
14475
14476 range_beginning += *base;
14477 range_end += *base;
14478
14479 /* A not-uncommon case of bad debug info.
14480 Don't pollute the addrmap with bad data. */
14481 if (range_beginning + baseaddr == 0
14482 && !per_objfile->per_bfd->has_section_at_zero)
14483 {
14484 complaint (_(".debug_ranges entry has start address of zero"
14485 " [in module %s]"), objfile_name (objfile));
14486 continue;
14487 }
14488
14489 callback (range_beginning, range_end);
14490 }
14491
14492 return 1;
14493 }
14494
14495 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14496 Return 1 if the attributes are present and valid, otherwise, return 0.
14497 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14498
14499 static int
14500 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14501 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14502 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14503 {
14504 struct objfile *objfile = cu->per_objfile->objfile;
14505 struct gdbarch *gdbarch = objfile->arch ();
14506 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14507 int low_set = 0;
14508 CORE_ADDR low = 0;
14509 CORE_ADDR high = 0;
14510 int retval;
14511
14512 retval = dwarf2_ranges_process (offset, cu, tag,
14513 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14514 {
14515 if (ranges_pst != NULL)
14516 {
14517 CORE_ADDR lowpc;
14518 CORE_ADDR highpc;
14519
14520 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14521 range_beginning + baseaddr)
14522 - baseaddr);
14523 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14524 range_end + baseaddr)
14525 - baseaddr);
14526 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14527 lowpc, highpc - 1, ranges_pst);
14528 }
14529
14530 /* FIXME: This is recording everything as a low-high
14531 segment of consecutive addresses. We should have a
14532 data structure for discontiguous block ranges
14533 instead. */
14534 if (! low_set)
14535 {
14536 low = range_beginning;
14537 high = range_end;
14538 low_set = 1;
14539 }
14540 else
14541 {
14542 if (range_beginning < low)
14543 low = range_beginning;
14544 if (range_end > high)
14545 high = range_end;
14546 }
14547 });
14548 if (!retval)
14549 return 0;
14550
14551 if (! low_set)
14552 /* If the first entry is an end-of-list marker, the range
14553 describes an empty scope, i.e. no instructions. */
14554 return 0;
14555
14556 if (low_return)
14557 *low_return = low;
14558 if (high_return)
14559 *high_return = high;
14560 return 1;
14561 }
14562
14563 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14564 definition for the return value. *LOWPC and *HIGHPC are set iff
14565 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14566
14567 static enum pc_bounds_kind
14568 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14569 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14570 dwarf2_psymtab *pst)
14571 {
14572 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14573 struct attribute *attr;
14574 struct attribute *attr_high;
14575 CORE_ADDR low = 0;
14576 CORE_ADDR high = 0;
14577 enum pc_bounds_kind ret;
14578
14579 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14580 if (attr_high)
14581 {
14582 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14583 if (attr != nullptr)
14584 {
14585 low = attr->as_address ();
14586 high = attr_high->as_address ();
14587 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14588 high += low;
14589 }
14590 else
14591 /* Found high w/o low attribute. */
14592 return PC_BOUNDS_INVALID;
14593
14594 /* Found consecutive range of addresses. */
14595 ret = PC_BOUNDS_HIGH_LOW;
14596 }
14597 else
14598 {
14599 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14600 if (attr != nullptr && attr->form_is_unsigned ())
14601 {
14602 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14603 We take advantage of the fact that DW_AT_ranges does not appear
14604 in DW_TAG_compile_unit of DWO files.
14605
14606 Attributes of the form DW_FORM_rnglistx have already had their
14607 value changed by read_rnglist_index and already include
14608 DW_AT_rnglists_base, so don't need to add the ranges base,
14609 either. */
14610 int need_ranges_base = (die->tag != DW_TAG_compile_unit
14611 && attr->form != DW_FORM_rnglistx);
14612 unsigned int ranges_offset = (attr->as_unsigned ()
14613 + (need_ranges_base
14614 ? cu->ranges_base
14615 : 0));
14616
14617 /* Value of the DW_AT_ranges attribute is the offset in the
14618 .debug_ranges section. */
14619 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14620 die->tag))
14621 return PC_BOUNDS_INVALID;
14622 /* Found discontinuous range of addresses. */
14623 ret = PC_BOUNDS_RANGES;
14624 }
14625 else
14626 return PC_BOUNDS_NOT_PRESENT;
14627 }
14628
14629 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14630 if (high <= low)
14631 return PC_BOUNDS_INVALID;
14632
14633 /* When using the GNU linker, .gnu.linkonce. sections are used to
14634 eliminate duplicate copies of functions and vtables and such.
14635 The linker will arbitrarily choose one and discard the others.
14636 The AT_*_pc values for such functions refer to local labels in
14637 these sections. If the section from that file was discarded, the
14638 labels are not in the output, so the relocs get a value of 0.
14639 If this is a discarded function, mark the pc bounds as invalid,
14640 so that GDB will ignore it. */
14641 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14642 return PC_BOUNDS_INVALID;
14643
14644 *lowpc = low;
14645 if (highpc)
14646 *highpc = high;
14647 return ret;
14648 }
14649
14650 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14651 its low and high PC addresses. Do nothing if these addresses could not
14652 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14653 and HIGHPC to the high address if greater than HIGHPC. */
14654
14655 static void
14656 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14657 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14658 struct dwarf2_cu *cu)
14659 {
14660 CORE_ADDR low, high;
14661 struct die_info *child = die->child;
14662
14663 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14664 {
14665 *lowpc = std::min (*lowpc, low);
14666 *highpc = std::max (*highpc, high);
14667 }
14668
14669 /* If the language does not allow nested subprograms (either inside
14670 subprograms or lexical blocks), we're done. */
14671 if (cu->language != language_ada)
14672 return;
14673
14674 /* Check all the children of the given DIE. If it contains nested
14675 subprograms, then check their pc bounds. Likewise, we need to
14676 check lexical blocks as well, as they may also contain subprogram
14677 definitions. */
14678 while (child && child->tag)
14679 {
14680 if (child->tag == DW_TAG_subprogram
14681 || child->tag == DW_TAG_lexical_block)
14682 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14683 child = child->sibling;
14684 }
14685 }
14686
14687 /* Get the low and high pc's represented by the scope DIE, and store
14688 them in *LOWPC and *HIGHPC. If the correct values can't be
14689 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14690
14691 static void
14692 get_scope_pc_bounds (struct die_info *die,
14693 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14694 struct dwarf2_cu *cu)
14695 {
14696 CORE_ADDR best_low = (CORE_ADDR) -1;
14697 CORE_ADDR best_high = (CORE_ADDR) 0;
14698 CORE_ADDR current_low, current_high;
14699
14700 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14701 >= PC_BOUNDS_RANGES)
14702 {
14703 best_low = current_low;
14704 best_high = current_high;
14705 }
14706 else
14707 {
14708 struct die_info *child = die->child;
14709
14710 while (child && child->tag)
14711 {
14712 switch (child->tag) {
14713 case DW_TAG_subprogram:
14714 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14715 break;
14716 case DW_TAG_namespace:
14717 case DW_TAG_module:
14718 /* FIXME: carlton/2004-01-16: Should we do this for
14719 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14720 that current GCC's always emit the DIEs corresponding
14721 to definitions of methods of classes as children of a
14722 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14723 the DIEs giving the declarations, which could be
14724 anywhere). But I don't see any reason why the
14725 standards says that they have to be there. */
14726 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14727
14728 if (current_low != ((CORE_ADDR) -1))
14729 {
14730 best_low = std::min (best_low, current_low);
14731 best_high = std::max (best_high, current_high);
14732 }
14733 break;
14734 default:
14735 /* Ignore. */
14736 break;
14737 }
14738
14739 child = child->sibling;
14740 }
14741 }
14742
14743 *lowpc = best_low;
14744 *highpc = best_high;
14745 }
14746
14747 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14748 in DIE. */
14749
14750 static void
14751 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14752 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14753 {
14754 struct objfile *objfile = cu->per_objfile->objfile;
14755 struct gdbarch *gdbarch = objfile->arch ();
14756 struct attribute *attr;
14757 struct attribute *attr_high;
14758
14759 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14760 if (attr_high)
14761 {
14762 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14763 if (attr != nullptr)
14764 {
14765 CORE_ADDR low = attr->as_address ();
14766 CORE_ADDR high = attr_high->as_address ();
14767
14768 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14769 high += low;
14770
14771 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14772 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14773 cu->get_builder ()->record_block_range (block, low, high - 1);
14774 }
14775 }
14776
14777 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14778 if (attr != nullptr && attr->form_is_unsigned ())
14779 {
14780 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14781 We take advantage of the fact that DW_AT_ranges does not appear
14782 in DW_TAG_compile_unit of DWO files.
14783
14784 Attributes of the form DW_FORM_rnglistx have already had their
14785 value changed by read_rnglist_index and already include
14786 DW_AT_rnglists_base, so don't need to add the ranges base,
14787 either. */
14788 int need_ranges_base = (die->tag != DW_TAG_compile_unit
14789 && attr->form != DW_FORM_rnglistx);
14790
14791 /* The value of the DW_AT_ranges attribute is the offset of the
14792 address range list in the .debug_ranges section. */
14793 unsigned long offset = (attr->as_unsigned ()
14794 + (need_ranges_base ? cu->ranges_base : 0));
14795
14796 std::vector<blockrange> blockvec;
14797 dwarf2_ranges_process (offset, cu, die->tag,
14798 [&] (CORE_ADDR start, CORE_ADDR end)
14799 {
14800 start += baseaddr;
14801 end += baseaddr;
14802 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14803 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14804 cu->get_builder ()->record_block_range (block, start, end - 1);
14805 blockvec.emplace_back (start, end);
14806 });
14807
14808 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14809 }
14810 }
14811
14812 /* Check whether the producer field indicates either of GCC < 4.6, or the
14813 Intel C/C++ compiler, and cache the result in CU. */
14814
14815 static void
14816 check_producer (struct dwarf2_cu *cu)
14817 {
14818 int major, minor;
14819
14820 if (cu->producer == NULL)
14821 {
14822 /* For unknown compilers expect their behavior is DWARF version
14823 compliant.
14824
14825 GCC started to support .debug_types sections by -gdwarf-4 since
14826 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14827 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14828 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14829 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14830 }
14831 else if (producer_is_gcc (cu->producer, &major, &minor))
14832 {
14833 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14834 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14835 }
14836 else if (producer_is_icc (cu->producer, &major, &minor))
14837 {
14838 cu->producer_is_icc = true;
14839 cu->producer_is_icc_lt_14 = major < 14;
14840 }
14841 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14842 cu->producer_is_codewarrior = true;
14843 else
14844 {
14845 /* For other non-GCC compilers, expect their behavior is DWARF version
14846 compliant. */
14847 }
14848
14849 cu->checked_producer = true;
14850 }
14851
14852 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14853 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14854 during 4.6.0 experimental. */
14855
14856 static bool
14857 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14858 {
14859 if (!cu->checked_producer)
14860 check_producer (cu);
14861
14862 return cu->producer_is_gxx_lt_4_6;
14863 }
14864
14865
14866 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14867 with incorrect is_stmt attributes. */
14868
14869 static bool
14870 producer_is_codewarrior (struct dwarf2_cu *cu)
14871 {
14872 if (!cu->checked_producer)
14873 check_producer (cu);
14874
14875 return cu->producer_is_codewarrior;
14876 }
14877
14878 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14879 If that attribute is not available, return the appropriate
14880 default. */
14881
14882 static enum dwarf_access_attribute
14883 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14884 {
14885 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14886 if (attr != nullptr)
14887 {
14888 LONGEST value = attr->constant_value (-1);
14889 if (value == DW_ACCESS_public
14890 || value == DW_ACCESS_protected
14891 || value == DW_ACCESS_private)
14892 return (dwarf_access_attribute) value;
14893 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14894 plongest (value));
14895 }
14896
14897 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14898 {
14899 /* The default DWARF 2 accessibility for members is public, the default
14900 accessibility for inheritance is private. */
14901
14902 if (die->tag != DW_TAG_inheritance)
14903 return DW_ACCESS_public;
14904 else
14905 return DW_ACCESS_private;
14906 }
14907 else
14908 {
14909 /* DWARF 3+ defines the default accessibility a different way. The same
14910 rules apply now for DW_TAG_inheritance as for the members and it only
14911 depends on the container kind. */
14912
14913 if (die->parent->tag == DW_TAG_class_type)
14914 return DW_ACCESS_private;
14915 else
14916 return DW_ACCESS_public;
14917 }
14918 }
14919
14920 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14921 offset. If the attribute was not found return 0, otherwise return
14922 1. If it was found but could not properly be handled, set *OFFSET
14923 to 0. */
14924
14925 static int
14926 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14927 LONGEST *offset)
14928 {
14929 struct attribute *attr;
14930
14931 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14932 if (attr != NULL)
14933 {
14934 *offset = 0;
14935
14936 /* Note that we do not check for a section offset first here.
14937 This is because DW_AT_data_member_location is new in DWARF 4,
14938 so if we see it, we can assume that a constant form is really
14939 a constant and not a section offset. */
14940 if (attr->form_is_constant ())
14941 *offset = attr->constant_value (0);
14942 else if (attr->form_is_section_offset ())
14943 dwarf2_complex_location_expr_complaint ();
14944 else if (attr->form_is_block ())
14945 *offset = decode_locdesc (attr->as_block (), cu);
14946 else
14947 dwarf2_complex_location_expr_complaint ();
14948
14949 return 1;
14950 }
14951
14952 return 0;
14953 }
14954
14955 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14956
14957 static void
14958 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14959 struct field *field)
14960 {
14961 struct attribute *attr;
14962
14963 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14964 if (attr != NULL)
14965 {
14966 if (attr->form_is_constant ())
14967 {
14968 LONGEST offset = attr->constant_value (0);
14969 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14970 }
14971 else if (attr->form_is_section_offset ())
14972 dwarf2_complex_location_expr_complaint ();
14973 else if (attr->form_is_block ())
14974 {
14975 bool handled;
14976 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
14977 if (handled)
14978 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14979 else
14980 {
14981 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14982 struct objfile *objfile = per_objfile->objfile;
14983 struct dwarf2_locexpr_baton *dlbaton
14984 = XOBNEW (&objfile->objfile_obstack,
14985 struct dwarf2_locexpr_baton);
14986 dlbaton->data = attr->as_block ()->data;
14987 dlbaton->size = attr->as_block ()->size;
14988 /* When using this baton, we want to compute the address
14989 of the field, not the value. This is why
14990 is_reference is set to false here. */
14991 dlbaton->is_reference = false;
14992 dlbaton->per_objfile = per_objfile;
14993 dlbaton->per_cu = cu->per_cu;
14994
14995 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14996 }
14997 }
14998 else
14999 dwarf2_complex_location_expr_complaint ();
15000 }
15001 }
15002
15003 /* Add an aggregate field to the field list. */
15004
15005 static void
15006 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15007 struct dwarf2_cu *cu)
15008 {
15009 struct objfile *objfile = cu->per_objfile->objfile;
15010 struct gdbarch *gdbarch = objfile->arch ();
15011 struct nextfield *new_field;
15012 struct attribute *attr;
15013 struct field *fp;
15014 const char *fieldname = "";
15015
15016 if (die->tag == DW_TAG_inheritance)
15017 {
15018 fip->baseclasses.emplace_back ();
15019 new_field = &fip->baseclasses.back ();
15020 }
15021 else
15022 {
15023 fip->fields.emplace_back ();
15024 new_field = &fip->fields.back ();
15025 }
15026
15027 new_field->offset = die->sect_off;
15028
15029 new_field->accessibility = dwarf2_access_attribute (die, cu);
15030 if (new_field->accessibility != DW_ACCESS_public)
15031 fip->non_public_fields = true;
15032
15033 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15034 if (attr != nullptr)
15035 new_field->virtuality = attr->as_virtuality ();
15036 else
15037 new_field->virtuality = DW_VIRTUALITY_none;
15038
15039 fp = &new_field->field;
15040
15041 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15042 {
15043 /* Data member other than a C++ static data member. */
15044
15045 /* Get type of field. */
15046 fp->set_type (die_type (die, cu));
15047
15048 SET_FIELD_BITPOS (*fp, 0);
15049
15050 /* Get bit size of field (zero if none). */
15051 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15052 if (attr != nullptr)
15053 {
15054 FIELD_BITSIZE (*fp) = attr->constant_value (0);
15055 }
15056 else
15057 {
15058 FIELD_BITSIZE (*fp) = 0;
15059 }
15060
15061 /* Get bit offset of field. */
15062 handle_data_member_location (die, cu, fp);
15063 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15064 if (attr != nullptr && attr->form_is_constant ())
15065 {
15066 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15067 {
15068 /* For big endian bits, the DW_AT_bit_offset gives the
15069 additional bit offset from the MSB of the containing
15070 anonymous object to the MSB of the field. We don't
15071 have to do anything special since we don't need to
15072 know the size of the anonymous object. */
15073 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15074 + attr->constant_value (0)));
15075 }
15076 else
15077 {
15078 /* For little endian bits, compute the bit offset to the
15079 MSB of the anonymous object, subtract off the number of
15080 bits from the MSB of the field to the MSB of the
15081 object, and then subtract off the number of bits of
15082 the field itself. The result is the bit offset of
15083 the LSB of the field. */
15084 int anonymous_size;
15085 int bit_offset = attr->constant_value (0);
15086
15087 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15088 if (attr != nullptr && attr->form_is_constant ())
15089 {
15090 /* The size of the anonymous object containing
15091 the bit field is explicit, so use the
15092 indicated size (in bytes). */
15093 anonymous_size = attr->constant_value (0);
15094 }
15095 else
15096 {
15097 /* The size of the anonymous object containing
15098 the bit field must be inferred from the type
15099 attribute of the data member containing the
15100 bit field. */
15101 anonymous_size = TYPE_LENGTH (fp->type ());
15102 }
15103 SET_FIELD_BITPOS (*fp,
15104 (FIELD_BITPOS (*fp)
15105 + anonymous_size * bits_per_byte
15106 - bit_offset - FIELD_BITSIZE (*fp)));
15107 }
15108 }
15109 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15110 if (attr != NULL)
15111 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15112 + attr->constant_value (0)));
15113
15114 /* Get name of field. */
15115 fieldname = dwarf2_name (die, cu);
15116 if (fieldname == NULL)
15117 fieldname = "";
15118
15119 /* The name is already allocated along with this objfile, so we don't
15120 need to duplicate it for the type. */
15121 fp->name = fieldname;
15122
15123 /* Change accessibility for artificial fields (e.g. virtual table
15124 pointer or virtual base class pointer) to private. */
15125 if (dwarf2_attr (die, DW_AT_artificial, cu))
15126 {
15127 FIELD_ARTIFICIAL (*fp) = 1;
15128 new_field->accessibility = DW_ACCESS_private;
15129 fip->non_public_fields = true;
15130 }
15131 }
15132 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15133 {
15134 /* C++ static member. */
15135
15136 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15137 is a declaration, but all versions of G++ as of this writing
15138 (so through at least 3.2.1) incorrectly generate
15139 DW_TAG_variable tags. */
15140
15141 const char *physname;
15142
15143 /* Get name of field. */
15144 fieldname = dwarf2_name (die, cu);
15145 if (fieldname == NULL)
15146 return;
15147
15148 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15149 if (attr
15150 /* Only create a symbol if this is an external value.
15151 new_symbol checks this and puts the value in the global symbol
15152 table, which we want. If it is not external, new_symbol
15153 will try to put the value in cu->list_in_scope which is wrong. */
15154 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15155 {
15156 /* A static const member, not much different than an enum as far as
15157 we're concerned, except that we can support more types. */
15158 new_symbol (die, NULL, cu);
15159 }
15160
15161 /* Get physical name. */
15162 physname = dwarf2_physname (fieldname, die, cu);
15163
15164 /* The name is already allocated along with this objfile, so we don't
15165 need to duplicate it for the type. */
15166 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15167 fp->set_type (die_type (die, cu));
15168 FIELD_NAME (*fp) = fieldname;
15169 }
15170 else if (die->tag == DW_TAG_inheritance)
15171 {
15172 /* C++ base class field. */
15173 handle_data_member_location (die, cu, fp);
15174 FIELD_BITSIZE (*fp) = 0;
15175 fp->set_type (die_type (die, cu));
15176 FIELD_NAME (*fp) = fp->type ()->name ();
15177 }
15178 else
15179 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15180 }
15181
15182 /* Can the type given by DIE define another type? */
15183
15184 static bool
15185 type_can_define_types (const struct die_info *die)
15186 {
15187 switch (die->tag)
15188 {
15189 case DW_TAG_typedef:
15190 case DW_TAG_class_type:
15191 case DW_TAG_structure_type:
15192 case DW_TAG_union_type:
15193 case DW_TAG_enumeration_type:
15194 return true;
15195
15196 default:
15197 return false;
15198 }
15199 }
15200
15201 /* Add a type definition defined in the scope of the FIP's class. */
15202
15203 static void
15204 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15205 struct dwarf2_cu *cu)
15206 {
15207 struct decl_field fp;
15208 memset (&fp, 0, sizeof (fp));
15209
15210 gdb_assert (type_can_define_types (die));
15211
15212 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15213 fp.name = dwarf2_name (die, cu);
15214 fp.type = read_type_die (die, cu);
15215
15216 /* Save accessibility. */
15217 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15218 switch (accessibility)
15219 {
15220 case DW_ACCESS_public:
15221 /* The assumed value if neither private nor protected. */
15222 break;
15223 case DW_ACCESS_private:
15224 fp.is_private = 1;
15225 break;
15226 case DW_ACCESS_protected:
15227 fp.is_protected = 1;
15228 break;
15229 }
15230
15231 if (die->tag == DW_TAG_typedef)
15232 fip->typedef_field_list.push_back (fp);
15233 else
15234 fip->nested_types_list.push_back (fp);
15235 }
15236
15237 /* A convenience typedef that's used when finding the discriminant
15238 field for a variant part. */
15239 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15240 offset_map_type;
15241
15242 /* Compute the discriminant range for a given variant. OBSTACK is
15243 where the results will be stored. VARIANT is the variant to
15244 process. IS_UNSIGNED indicates whether the discriminant is signed
15245 or unsigned. */
15246
15247 static const gdb::array_view<discriminant_range>
15248 convert_variant_range (struct obstack *obstack, const variant_field &variant,
15249 bool is_unsigned)
15250 {
15251 std::vector<discriminant_range> ranges;
15252
15253 if (variant.default_branch)
15254 return {};
15255
15256 if (variant.discr_list_data == nullptr)
15257 {
15258 discriminant_range r
15259 = {variant.discriminant_value, variant.discriminant_value};
15260 ranges.push_back (r);
15261 }
15262 else
15263 {
15264 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15265 variant.discr_list_data->size);
15266 while (!data.empty ())
15267 {
15268 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15269 {
15270 complaint (_("invalid discriminant marker: %d"), data[0]);
15271 break;
15272 }
15273 bool is_range = data[0] == DW_DSC_range;
15274 data = data.slice (1);
15275
15276 ULONGEST low, high;
15277 unsigned int bytes_read;
15278
15279 if (data.empty ())
15280 {
15281 complaint (_("DW_AT_discr_list missing low value"));
15282 break;
15283 }
15284 if (is_unsigned)
15285 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15286 else
15287 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15288 &bytes_read);
15289 data = data.slice (bytes_read);
15290
15291 if (is_range)
15292 {
15293 if (data.empty ())
15294 {
15295 complaint (_("DW_AT_discr_list missing high value"));
15296 break;
15297 }
15298 if (is_unsigned)
15299 high = read_unsigned_leb128 (nullptr, data.data (),
15300 &bytes_read);
15301 else
15302 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15303 &bytes_read);
15304 data = data.slice (bytes_read);
15305 }
15306 else
15307 high = low;
15308
15309 ranges.push_back ({ low, high });
15310 }
15311 }
15312
15313 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15314 ranges.size ());
15315 std::copy (ranges.begin (), ranges.end (), result);
15316 return gdb::array_view<discriminant_range> (result, ranges.size ());
15317 }
15318
15319 static const gdb::array_view<variant_part> create_variant_parts
15320 (struct obstack *obstack,
15321 const offset_map_type &offset_map,
15322 struct field_info *fi,
15323 const std::vector<variant_part_builder> &variant_parts);
15324
15325 /* Fill in a "struct variant" for a given variant field. RESULT is
15326 the variant to fill in. OBSTACK is where any needed allocations
15327 will be done. OFFSET_MAP holds the mapping from section offsets to
15328 fields for the type. FI describes the fields of the type we're
15329 processing. FIELD is the variant field we're converting. */
15330
15331 static void
15332 create_one_variant (variant &result, struct obstack *obstack,
15333 const offset_map_type &offset_map,
15334 struct field_info *fi, const variant_field &field)
15335 {
15336 result.discriminants = convert_variant_range (obstack, field, false);
15337 result.first_field = field.first_field + fi->baseclasses.size ();
15338 result.last_field = field.last_field + fi->baseclasses.size ();
15339 result.parts = create_variant_parts (obstack, offset_map, fi,
15340 field.variant_parts);
15341 }
15342
15343 /* Fill in a "struct variant_part" for a given variant part. RESULT
15344 is the variant part to fill in. OBSTACK is where any needed
15345 allocations will be done. OFFSET_MAP holds the mapping from
15346 section offsets to fields for the type. FI describes the fields of
15347 the type we're processing. BUILDER is the variant part to be
15348 converted. */
15349
15350 static void
15351 create_one_variant_part (variant_part &result,
15352 struct obstack *obstack,
15353 const offset_map_type &offset_map,
15354 struct field_info *fi,
15355 const variant_part_builder &builder)
15356 {
15357 auto iter = offset_map.find (builder.discriminant_offset);
15358 if (iter == offset_map.end ())
15359 {
15360 result.discriminant_index = -1;
15361 /* Doesn't matter. */
15362 result.is_unsigned = false;
15363 }
15364 else
15365 {
15366 result.discriminant_index = iter->second;
15367 result.is_unsigned
15368 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
15369 }
15370
15371 size_t n = builder.variants.size ();
15372 variant *output = new (obstack) variant[n];
15373 for (size_t i = 0; i < n; ++i)
15374 create_one_variant (output[i], obstack, offset_map, fi,
15375 builder.variants[i]);
15376
15377 result.variants = gdb::array_view<variant> (output, n);
15378 }
15379
15380 /* Create a vector of variant parts that can be attached to a type.
15381 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15382 holds the mapping from section offsets to fields for the type. FI
15383 describes the fields of the type we're processing. VARIANT_PARTS
15384 is the vector to convert. */
15385
15386 static const gdb::array_view<variant_part>
15387 create_variant_parts (struct obstack *obstack,
15388 const offset_map_type &offset_map,
15389 struct field_info *fi,
15390 const std::vector<variant_part_builder> &variant_parts)
15391 {
15392 if (variant_parts.empty ())
15393 return {};
15394
15395 size_t n = variant_parts.size ();
15396 variant_part *result = new (obstack) variant_part[n];
15397 for (size_t i = 0; i < n; ++i)
15398 create_one_variant_part (result[i], obstack, offset_map, fi,
15399 variant_parts[i]);
15400
15401 return gdb::array_view<variant_part> (result, n);
15402 }
15403
15404 /* Compute the variant part vector for FIP, attaching it to TYPE when
15405 done. */
15406
15407 static void
15408 add_variant_property (struct field_info *fip, struct type *type,
15409 struct dwarf2_cu *cu)
15410 {
15411 /* Map section offsets of fields to their field index. Note the
15412 field index here does not take the number of baseclasses into
15413 account. */
15414 offset_map_type offset_map;
15415 for (int i = 0; i < fip->fields.size (); ++i)
15416 offset_map[fip->fields[i].offset] = i;
15417
15418 struct objfile *objfile = cu->per_objfile->objfile;
15419 gdb::array_view<variant_part> parts
15420 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15421 fip->variant_parts);
15422
15423 struct dynamic_prop prop;
15424 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15425 obstack_copy (&objfile->objfile_obstack, &parts,
15426 sizeof (parts)));
15427
15428 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
15429 }
15430
15431 /* Create the vector of fields, and attach it to the type. */
15432
15433 static void
15434 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15435 struct dwarf2_cu *cu)
15436 {
15437 int nfields = fip->nfields ();
15438
15439 /* Record the field count, allocate space for the array of fields,
15440 and create blank accessibility bitfields if necessary. */
15441 type->set_num_fields (nfields);
15442 type->set_fields
15443 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15444
15445 if (fip->non_public_fields && cu->language != language_ada)
15446 {
15447 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15448
15449 TYPE_FIELD_PRIVATE_BITS (type) =
15450 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15451 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15452
15453 TYPE_FIELD_PROTECTED_BITS (type) =
15454 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15455 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15456
15457 TYPE_FIELD_IGNORE_BITS (type) =
15458 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15459 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15460 }
15461
15462 /* If the type has baseclasses, allocate and clear a bit vector for
15463 TYPE_FIELD_VIRTUAL_BITS. */
15464 if (!fip->baseclasses.empty () && cu->language != language_ada)
15465 {
15466 int num_bytes = B_BYTES (fip->baseclasses.size ());
15467 unsigned char *pointer;
15468
15469 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15470 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15471 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15472 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15473 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15474 }
15475
15476 if (!fip->variant_parts.empty ())
15477 add_variant_property (fip, type, cu);
15478
15479 /* Copy the saved-up fields into the field vector. */
15480 for (int i = 0; i < nfields; ++i)
15481 {
15482 struct nextfield &field
15483 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15484 : fip->fields[i - fip->baseclasses.size ()]);
15485
15486 type->field (i) = field.field;
15487 switch (field.accessibility)
15488 {
15489 case DW_ACCESS_private:
15490 if (cu->language != language_ada)
15491 SET_TYPE_FIELD_PRIVATE (type, i);
15492 break;
15493
15494 case DW_ACCESS_protected:
15495 if (cu->language != language_ada)
15496 SET_TYPE_FIELD_PROTECTED (type, i);
15497 break;
15498
15499 case DW_ACCESS_public:
15500 break;
15501
15502 default:
15503 /* Unknown accessibility. Complain and treat it as public. */
15504 {
15505 complaint (_("unsupported accessibility %d"),
15506 field.accessibility);
15507 }
15508 break;
15509 }
15510 if (i < fip->baseclasses.size ())
15511 {
15512 switch (field.virtuality)
15513 {
15514 case DW_VIRTUALITY_virtual:
15515 case DW_VIRTUALITY_pure_virtual:
15516 if (cu->language == language_ada)
15517 error (_("unexpected virtuality in component of Ada type"));
15518 SET_TYPE_FIELD_VIRTUAL (type, i);
15519 break;
15520 }
15521 }
15522 }
15523 }
15524
15525 /* Return true if this member function is a constructor, false
15526 otherwise. */
15527
15528 static int
15529 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15530 {
15531 const char *fieldname;
15532 const char *type_name;
15533 int len;
15534
15535 if (die->parent == NULL)
15536 return 0;
15537
15538 if (die->parent->tag != DW_TAG_structure_type
15539 && die->parent->tag != DW_TAG_union_type
15540 && die->parent->tag != DW_TAG_class_type)
15541 return 0;
15542
15543 fieldname = dwarf2_name (die, cu);
15544 type_name = dwarf2_name (die->parent, cu);
15545 if (fieldname == NULL || type_name == NULL)
15546 return 0;
15547
15548 len = strlen (fieldname);
15549 return (strncmp (fieldname, type_name, len) == 0
15550 && (type_name[len] == '\0' || type_name[len] == '<'));
15551 }
15552
15553 /* Add a member function to the proper fieldlist. */
15554
15555 static void
15556 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15557 struct type *type, struct dwarf2_cu *cu)
15558 {
15559 struct objfile *objfile = cu->per_objfile->objfile;
15560 struct attribute *attr;
15561 int i;
15562 struct fnfieldlist *flp = nullptr;
15563 struct fn_field *fnp;
15564 const char *fieldname;
15565 struct type *this_type;
15566
15567 if (cu->language == language_ada)
15568 error (_("unexpected member function in Ada type"));
15569
15570 /* Get name of member function. */
15571 fieldname = dwarf2_name (die, cu);
15572 if (fieldname == NULL)
15573 return;
15574
15575 /* Look up member function name in fieldlist. */
15576 for (i = 0; i < fip->fnfieldlists.size (); i++)
15577 {
15578 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15579 {
15580 flp = &fip->fnfieldlists[i];
15581 break;
15582 }
15583 }
15584
15585 /* Create a new fnfieldlist if necessary. */
15586 if (flp == nullptr)
15587 {
15588 fip->fnfieldlists.emplace_back ();
15589 flp = &fip->fnfieldlists.back ();
15590 flp->name = fieldname;
15591 i = fip->fnfieldlists.size () - 1;
15592 }
15593
15594 /* Create a new member function field and add it to the vector of
15595 fnfieldlists. */
15596 flp->fnfields.emplace_back ();
15597 fnp = &flp->fnfields.back ();
15598
15599 /* Delay processing of the physname until later. */
15600 if (cu->language == language_cplus)
15601 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15602 die, cu);
15603 else
15604 {
15605 const char *physname = dwarf2_physname (fieldname, die, cu);
15606 fnp->physname = physname ? physname : "";
15607 }
15608
15609 fnp->type = alloc_type (objfile);
15610 this_type = read_type_die (die, cu);
15611 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15612 {
15613 int nparams = this_type->num_fields ();
15614
15615 /* TYPE is the domain of this method, and THIS_TYPE is the type
15616 of the method itself (TYPE_CODE_METHOD). */
15617 smash_to_method_type (fnp->type, type,
15618 TYPE_TARGET_TYPE (this_type),
15619 this_type->fields (),
15620 this_type->num_fields (),
15621 this_type->has_varargs ());
15622
15623 /* Handle static member functions.
15624 Dwarf2 has no clean way to discern C++ static and non-static
15625 member functions. G++ helps GDB by marking the first
15626 parameter for non-static member functions (which is the this
15627 pointer) as artificial. We obtain this information from
15628 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15629 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15630 fnp->voffset = VOFFSET_STATIC;
15631 }
15632 else
15633 complaint (_("member function type missing for '%s'"),
15634 dwarf2_full_name (fieldname, die, cu));
15635
15636 /* Get fcontext from DW_AT_containing_type if present. */
15637 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15638 fnp->fcontext = die_containing_type (die, cu);
15639
15640 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15641 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15642
15643 /* Get accessibility. */
15644 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15645 switch (accessibility)
15646 {
15647 case DW_ACCESS_private:
15648 fnp->is_private = 1;
15649 break;
15650 case DW_ACCESS_protected:
15651 fnp->is_protected = 1;
15652 break;
15653 }
15654
15655 /* Check for artificial methods. */
15656 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15657 if (attr && attr->as_boolean ())
15658 fnp->is_artificial = 1;
15659
15660 /* Check for defaulted methods. */
15661 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15662 if (attr != nullptr)
15663 fnp->defaulted = attr->defaulted ();
15664
15665 /* Check for deleted methods. */
15666 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15667 if (attr != nullptr && attr->as_boolean ())
15668 fnp->is_deleted = 1;
15669
15670 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15671
15672 /* Get index in virtual function table if it is a virtual member
15673 function. For older versions of GCC, this is an offset in the
15674 appropriate virtual table, as specified by DW_AT_containing_type.
15675 For everyone else, it is an expression to be evaluated relative
15676 to the object address. */
15677
15678 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15679 if (attr != nullptr)
15680 {
15681 if (attr->form_is_block () && attr->as_block ()->size > 0)
15682 {
15683 struct dwarf_block *block = attr->as_block ();
15684
15685 if (block->data[0] == DW_OP_constu)
15686 {
15687 /* Old-style GCC. */
15688 fnp->voffset = decode_locdesc (block, cu) + 2;
15689 }
15690 else if (block->data[0] == DW_OP_deref
15691 || (block->size > 1
15692 && block->data[0] == DW_OP_deref_size
15693 && block->data[1] == cu->header.addr_size))
15694 {
15695 fnp->voffset = decode_locdesc (block, cu);
15696 if ((fnp->voffset % cu->header.addr_size) != 0)
15697 dwarf2_complex_location_expr_complaint ();
15698 else
15699 fnp->voffset /= cu->header.addr_size;
15700 fnp->voffset += 2;
15701 }
15702 else
15703 dwarf2_complex_location_expr_complaint ();
15704
15705 if (!fnp->fcontext)
15706 {
15707 /* If there is no `this' field and no DW_AT_containing_type,
15708 we cannot actually find a base class context for the
15709 vtable! */
15710 if (this_type->num_fields () == 0
15711 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15712 {
15713 complaint (_("cannot determine context for virtual member "
15714 "function \"%s\" (offset %s)"),
15715 fieldname, sect_offset_str (die->sect_off));
15716 }
15717 else
15718 {
15719 fnp->fcontext
15720 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15721 }
15722 }
15723 }
15724 else if (attr->form_is_section_offset ())
15725 {
15726 dwarf2_complex_location_expr_complaint ();
15727 }
15728 else
15729 {
15730 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15731 fieldname);
15732 }
15733 }
15734 else
15735 {
15736 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15737 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15738 {
15739 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15740 complaint (_("Member function \"%s\" (offset %s) is virtual "
15741 "but the vtable offset is not specified"),
15742 fieldname, sect_offset_str (die->sect_off));
15743 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15744 TYPE_CPLUS_DYNAMIC (type) = 1;
15745 }
15746 }
15747 }
15748
15749 /* Create the vector of member function fields, and attach it to the type. */
15750
15751 static void
15752 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15753 struct dwarf2_cu *cu)
15754 {
15755 if (cu->language == language_ada)
15756 error (_("unexpected member functions in Ada type"));
15757
15758 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15759 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15760 TYPE_ALLOC (type,
15761 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15762
15763 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15764 {
15765 struct fnfieldlist &nf = fip->fnfieldlists[i];
15766 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15767
15768 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15769 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15770 fn_flp->fn_fields = (struct fn_field *)
15771 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15772
15773 for (int k = 0; k < nf.fnfields.size (); ++k)
15774 fn_flp->fn_fields[k] = nf.fnfields[k];
15775 }
15776
15777 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15778 }
15779
15780 /* Returns non-zero if NAME is the name of a vtable member in CU's
15781 language, zero otherwise. */
15782 static int
15783 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15784 {
15785 static const char vptr[] = "_vptr";
15786
15787 /* Look for the C++ form of the vtable. */
15788 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15789 return 1;
15790
15791 return 0;
15792 }
15793
15794 /* GCC outputs unnamed structures that are really pointers to member
15795 functions, with the ABI-specified layout. If TYPE describes
15796 such a structure, smash it into a member function type.
15797
15798 GCC shouldn't do this; it should just output pointer to member DIEs.
15799 This is GCC PR debug/28767. */
15800
15801 static void
15802 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15803 {
15804 struct type *pfn_type, *self_type, *new_type;
15805
15806 /* Check for a structure with no name and two children. */
15807 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15808 return;
15809
15810 /* Check for __pfn and __delta members. */
15811 if (TYPE_FIELD_NAME (type, 0) == NULL
15812 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15813 || TYPE_FIELD_NAME (type, 1) == NULL
15814 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15815 return;
15816
15817 /* Find the type of the method. */
15818 pfn_type = type->field (0).type ();
15819 if (pfn_type == NULL
15820 || pfn_type->code () != TYPE_CODE_PTR
15821 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15822 return;
15823
15824 /* Look for the "this" argument. */
15825 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15826 if (pfn_type->num_fields () == 0
15827 /* || pfn_type->field (0).type () == NULL */
15828 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15829 return;
15830
15831 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15832 new_type = alloc_type (objfile);
15833 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15834 pfn_type->fields (), pfn_type->num_fields (),
15835 pfn_type->has_varargs ());
15836 smash_to_methodptr_type (type, new_type);
15837 }
15838
15839 /* While some versions of GCC will generate complicated DWARF for an
15840 array (see quirk_ada_thick_pointer), more recent versions were
15841 modified to emit an explicit thick pointer structure. However, in
15842 this case, the array still has DWARF expressions for its ranges,
15843 and these must be ignored. */
15844
15845 static void
15846 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15847 struct type *type)
15848 {
15849 gdb_assert (cu->language == language_ada);
15850
15851 /* Check for a structure with two children. */
15852 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15853 return;
15854
15855 /* Check for P_ARRAY and P_BOUNDS members. */
15856 if (TYPE_FIELD_NAME (type, 0) == NULL
15857 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15858 || TYPE_FIELD_NAME (type, 1) == NULL
15859 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15860 return;
15861
15862 /* Make sure we're looking at a pointer to an array. */
15863 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15864 return;
15865 struct type *ary_type = TYPE_TARGET_TYPE (type->field (0).type ());
15866
15867 while (ary_type->code () == TYPE_CODE_ARRAY)
15868 {
15869 /* The Ada code already knows how to handle these types, so all
15870 that we need to do is turn the bounds into static bounds. */
15871 struct type *index_type = ary_type->index_type ();
15872
15873 index_type->bounds ()->low.set_const_val (1);
15874 index_type->bounds ()->high.set_const_val (0);
15875
15876 /* Handle multi-dimensional arrays. */
15877 ary_type = TYPE_TARGET_TYPE (ary_type);
15878 }
15879 }
15880
15881 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15882 appropriate error checking and issuing complaints if there is a
15883 problem. */
15884
15885 static ULONGEST
15886 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15887 {
15888 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15889
15890 if (attr == nullptr)
15891 return 0;
15892
15893 if (!attr->form_is_constant ())
15894 {
15895 complaint (_("DW_AT_alignment must have constant form"
15896 " - DIE at %s [in module %s]"),
15897 sect_offset_str (die->sect_off),
15898 objfile_name (cu->per_objfile->objfile));
15899 return 0;
15900 }
15901
15902 LONGEST val = attr->constant_value (0);
15903 if (val < 0)
15904 {
15905 complaint (_("DW_AT_alignment value must not be negative"
15906 " - DIE at %s [in module %s]"),
15907 sect_offset_str (die->sect_off),
15908 objfile_name (cu->per_objfile->objfile));
15909 return 0;
15910 }
15911 ULONGEST align = val;
15912
15913 if (align == 0)
15914 {
15915 complaint (_("DW_AT_alignment value must not be zero"
15916 " - DIE at %s [in module %s]"),
15917 sect_offset_str (die->sect_off),
15918 objfile_name (cu->per_objfile->objfile));
15919 return 0;
15920 }
15921 if ((align & (align - 1)) != 0)
15922 {
15923 complaint (_("DW_AT_alignment value must be a power of 2"
15924 " - DIE at %s [in module %s]"),
15925 sect_offset_str (die->sect_off),
15926 objfile_name (cu->per_objfile->objfile));
15927 return 0;
15928 }
15929
15930 return align;
15931 }
15932
15933 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15934 the alignment for TYPE. */
15935
15936 static void
15937 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15938 struct type *type)
15939 {
15940 if (!set_type_align (type, get_alignment (cu, die)))
15941 complaint (_("DW_AT_alignment value too large"
15942 " - DIE at %s [in module %s]"),
15943 sect_offset_str (die->sect_off),
15944 objfile_name (cu->per_objfile->objfile));
15945 }
15946
15947 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15948 constant for a type, according to DWARF5 spec, Table 5.5. */
15949
15950 static bool
15951 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15952 {
15953 switch (value)
15954 {
15955 case DW_CC_normal:
15956 case DW_CC_pass_by_reference:
15957 case DW_CC_pass_by_value:
15958 return true;
15959
15960 default:
15961 complaint (_("unrecognized DW_AT_calling_convention value "
15962 "(%s) for a type"), pulongest (value));
15963 return false;
15964 }
15965 }
15966
15967 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15968 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15969 also according to GNU-specific values (see include/dwarf2.h). */
15970
15971 static bool
15972 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15973 {
15974 switch (value)
15975 {
15976 case DW_CC_normal:
15977 case DW_CC_program:
15978 case DW_CC_nocall:
15979 return true;
15980
15981 case DW_CC_GNU_renesas_sh:
15982 case DW_CC_GNU_borland_fastcall_i386:
15983 case DW_CC_GDB_IBM_OpenCL:
15984 return true;
15985
15986 default:
15987 complaint (_("unrecognized DW_AT_calling_convention value "
15988 "(%s) for a subroutine"), pulongest (value));
15989 return false;
15990 }
15991 }
15992
15993 /* Called when we find the DIE that starts a structure or union scope
15994 (definition) to create a type for the structure or union. Fill in
15995 the type's name and general properties; the members will not be
15996 processed until process_structure_scope. A symbol table entry for
15997 the type will also not be done until process_structure_scope (assuming
15998 the type has a name).
15999
16000 NOTE: we need to call these functions regardless of whether or not the
16001 DIE has a DW_AT_name attribute, since it might be an anonymous
16002 structure or union. This gets the type entered into our set of
16003 user defined types. */
16004
16005 static struct type *
16006 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
16007 {
16008 struct objfile *objfile = cu->per_objfile->objfile;
16009 struct type *type;
16010 struct attribute *attr;
16011 const char *name;
16012
16013 /* If the definition of this type lives in .debug_types, read that type.
16014 Don't follow DW_AT_specification though, that will take us back up
16015 the chain and we want to go down. */
16016 attr = die->attr (DW_AT_signature);
16017 if (attr != nullptr)
16018 {
16019 type = get_DW_AT_signature_type (die, attr, cu);
16020
16021 /* The type's CU may not be the same as CU.
16022 Ensure TYPE is recorded with CU in die_type_hash. */
16023 return set_die_type (die, type, cu);
16024 }
16025
16026 type = alloc_type (objfile);
16027 INIT_CPLUS_SPECIFIC (type);
16028
16029 name = dwarf2_name (die, cu);
16030 if (name != NULL)
16031 {
16032 if (cu->language == language_cplus
16033 || cu->language == language_d
16034 || cu->language == language_rust)
16035 {
16036 const char *full_name = dwarf2_full_name (name, die, cu);
16037
16038 /* dwarf2_full_name might have already finished building the DIE's
16039 type. If so, there is no need to continue. */
16040 if (get_die_type (die, cu) != NULL)
16041 return get_die_type (die, cu);
16042
16043 type->set_name (full_name);
16044 }
16045 else
16046 {
16047 /* The name is already allocated along with this objfile, so
16048 we don't need to duplicate it for the type. */
16049 type->set_name (name);
16050 }
16051 }
16052
16053 if (die->tag == DW_TAG_structure_type)
16054 {
16055 type->set_code (TYPE_CODE_STRUCT);
16056 }
16057 else if (die->tag == DW_TAG_union_type)
16058 {
16059 type->set_code (TYPE_CODE_UNION);
16060 }
16061 else
16062 {
16063 type->set_code (TYPE_CODE_STRUCT);
16064 }
16065
16066 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16067 TYPE_DECLARED_CLASS (type) = 1;
16068
16069 /* Store the calling convention in the type if it's available in
16070 the die. Otherwise the calling convention remains set to
16071 the default value DW_CC_normal. */
16072 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16073 if (attr != nullptr
16074 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
16075 {
16076 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16077 TYPE_CPLUS_CALLING_CONVENTION (type)
16078 = (enum dwarf_calling_convention) (attr->constant_value (0));
16079 }
16080
16081 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16082 if (attr != nullptr)
16083 {
16084 if (attr->form_is_constant ())
16085 TYPE_LENGTH (type) = attr->constant_value (0);
16086 else
16087 {
16088 struct dynamic_prop prop;
16089 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
16090 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
16091 TYPE_LENGTH (type) = 0;
16092 }
16093 }
16094 else
16095 {
16096 TYPE_LENGTH (type) = 0;
16097 }
16098
16099 maybe_set_alignment (cu, die, type);
16100
16101 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16102 {
16103 /* ICC<14 does not output the required DW_AT_declaration on
16104 incomplete types, but gives them a size of zero. */
16105 type->set_is_stub (true);
16106 }
16107 else
16108 type->set_stub_is_supported (true);
16109
16110 if (die_is_declaration (die, cu))
16111 type->set_is_stub (true);
16112 else if (attr == NULL && die->child == NULL
16113 && producer_is_realview (cu->producer))
16114 /* RealView does not output the required DW_AT_declaration
16115 on incomplete types. */
16116 type->set_is_stub (true);
16117
16118 /* We need to add the type field to the die immediately so we don't
16119 infinitely recurse when dealing with pointers to the structure
16120 type within the structure itself. */
16121 set_die_type (die, type, cu);
16122
16123 /* set_die_type should be already done. */
16124 set_descriptive_type (type, die, cu);
16125
16126 return type;
16127 }
16128
16129 static void handle_struct_member_die
16130 (struct die_info *child_die,
16131 struct type *type,
16132 struct field_info *fi,
16133 std::vector<struct symbol *> *template_args,
16134 struct dwarf2_cu *cu);
16135
16136 /* A helper for handle_struct_member_die that handles
16137 DW_TAG_variant_part. */
16138
16139 static void
16140 handle_variant_part (struct die_info *die, struct type *type,
16141 struct field_info *fi,
16142 std::vector<struct symbol *> *template_args,
16143 struct dwarf2_cu *cu)
16144 {
16145 variant_part_builder *new_part;
16146 if (fi->current_variant_part == nullptr)
16147 {
16148 fi->variant_parts.emplace_back ();
16149 new_part = &fi->variant_parts.back ();
16150 }
16151 else if (!fi->current_variant_part->processing_variant)
16152 {
16153 complaint (_("nested DW_TAG_variant_part seen "
16154 "- DIE at %s [in module %s]"),
16155 sect_offset_str (die->sect_off),
16156 objfile_name (cu->per_objfile->objfile));
16157 return;
16158 }
16159 else
16160 {
16161 variant_field &current = fi->current_variant_part->variants.back ();
16162 current.variant_parts.emplace_back ();
16163 new_part = &current.variant_parts.back ();
16164 }
16165
16166 /* When we recurse, we want callees to add to this new variant
16167 part. */
16168 scoped_restore save_current_variant_part
16169 = make_scoped_restore (&fi->current_variant_part, new_part);
16170
16171 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16172 if (discr == NULL)
16173 {
16174 /* It's a univariant form, an extension we support. */
16175 }
16176 else if (discr->form_is_ref ())
16177 {
16178 struct dwarf2_cu *target_cu = cu;
16179 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16180
16181 new_part->discriminant_offset = target_die->sect_off;
16182 }
16183 else
16184 {
16185 complaint (_("DW_AT_discr does not have DIE reference form"
16186 " - DIE at %s [in module %s]"),
16187 sect_offset_str (die->sect_off),
16188 objfile_name (cu->per_objfile->objfile));
16189 }
16190
16191 for (die_info *child_die = die->child;
16192 child_die != NULL;
16193 child_die = child_die->sibling)
16194 handle_struct_member_die (child_die, type, fi, template_args, cu);
16195 }
16196
16197 /* A helper for handle_struct_member_die that handles
16198 DW_TAG_variant. */
16199
16200 static void
16201 handle_variant (struct die_info *die, struct type *type,
16202 struct field_info *fi,
16203 std::vector<struct symbol *> *template_args,
16204 struct dwarf2_cu *cu)
16205 {
16206 if (fi->current_variant_part == nullptr)
16207 {
16208 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16209 "- DIE at %s [in module %s]"),
16210 sect_offset_str (die->sect_off),
16211 objfile_name (cu->per_objfile->objfile));
16212 return;
16213 }
16214 if (fi->current_variant_part->processing_variant)
16215 {
16216 complaint (_("nested DW_TAG_variant seen "
16217 "- DIE at %s [in module %s]"),
16218 sect_offset_str (die->sect_off),
16219 objfile_name (cu->per_objfile->objfile));
16220 return;
16221 }
16222
16223 scoped_restore save_processing_variant
16224 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16225 true);
16226
16227 fi->current_variant_part->variants.emplace_back ();
16228 variant_field &variant = fi->current_variant_part->variants.back ();
16229 variant.first_field = fi->fields.size ();
16230
16231 /* In a variant we want to get the discriminant and also add a
16232 field for our sole member child. */
16233 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
16234 if (discr == nullptr || !discr->form_is_constant ())
16235 {
16236 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
16237 if (discr == nullptr || discr->as_block ()->size == 0)
16238 variant.default_branch = true;
16239 else
16240 variant.discr_list_data = discr->as_block ();
16241 }
16242 else
16243 variant.discriminant_value = discr->constant_value (0);
16244
16245 for (die_info *variant_child = die->child;
16246 variant_child != NULL;
16247 variant_child = variant_child->sibling)
16248 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16249
16250 variant.last_field = fi->fields.size ();
16251 }
16252
16253 /* A helper for process_structure_scope that handles a single member
16254 DIE. */
16255
16256 static void
16257 handle_struct_member_die (struct die_info *child_die, struct type *type,
16258 struct field_info *fi,
16259 std::vector<struct symbol *> *template_args,
16260 struct dwarf2_cu *cu)
16261 {
16262 if (child_die->tag == DW_TAG_member
16263 || child_die->tag == DW_TAG_variable)
16264 {
16265 /* NOTE: carlton/2002-11-05: A C++ static data member
16266 should be a DW_TAG_member that is a declaration, but
16267 all versions of G++ as of this writing (so through at
16268 least 3.2.1) incorrectly generate DW_TAG_variable
16269 tags for them instead. */
16270 dwarf2_add_field (fi, child_die, cu);
16271 }
16272 else if (child_die->tag == DW_TAG_subprogram)
16273 {
16274 /* Rust doesn't have member functions in the C++ sense.
16275 However, it does emit ordinary functions as children
16276 of a struct DIE. */
16277 if (cu->language == language_rust)
16278 read_func_scope (child_die, cu);
16279 else
16280 {
16281 /* C++ member function. */
16282 dwarf2_add_member_fn (fi, child_die, type, cu);
16283 }
16284 }
16285 else if (child_die->tag == DW_TAG_inheritance)
16286 {
16287 /* C++ base class field. */
16288 dwarf2_add_field (fi, child_die, cu);
16289 }
16290 else if (type_can_define_types (child_die))
16291 dwarf2_add_type_defn (fi, child_die, cu);
16292 else if (child_die->tag == DW_TAG_template_type_param
16293 || child_die->tag == DW_TAG_template_value_param)
16294 {
16295 struct symbol *arg = new_symbol (child_die, NULL, cu);
16296
16297 if (arg != NULL)
16298 template_args->push_back (arg);
16299 }
16300 else if (child_die->tag == DW_TAG_variant_part)
16301 handle_variant_part (child_die, type, fi, template_args, cu);
16302 else if (child_die->tag == DW_TAG_variant)
16303 handle_variant (child_die, type, fi, template_args, cu);
16304 }
16305
16306 /* Finish creating a structure or union type, including filling in
16307 its members and creating a symbol for it. */
16308
16309 static void
16310 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16311 {
16312 struct objfile *objfile = cu->per_objfile->objfile;
16313 struct die_info *child_die;
16314 struct type *type;
16315
16316 type = get_die_type (die, cu);
16317 if (type == NULL)
16318 type = read_structure_type (die, cu);
16319
16320 bool has_template_parameters = false;
16321 if (die->child != NULL && ! die_is_declaration (die, cu))
16322 {
16323 struct field_info fi;
16324 std::vector<struct symbol *> template_args;
16325
16326 child_die = die->child;
16327
16328 while (child_die && child_die->tag)
16329 {
16330 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16331 child_die = child_die->sibling;
16332 }
16333
16334 /* Attach template arguments to type. */
16335 if (!template_args.empty ())
16336 {
16337 has_template_parameters = true;
16338 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16339 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16340 TYPE_TEMPLATE_ARGUMENTS (type)
16341 = XOBNEWVEC (&objfile->objfile_obstack,
16342 struct symbol *,
16343 TYPE_N_TEMPLATE_ARGUMENTS (type));
16344 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16345 template_args.data (),
16346 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16347 * sizeof (struct symbol *)));
16348 }
16349
16350 /* Attach fields and member functions to the type. */
16351 if (fi.nfields () > 0)
16352 dwarf2_attach_fields_to_type (&fi, type, cu);
16353 if (!fi.fnfieldlists.empty ())
16354 {
16355 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16356
16357 /* Get the type which refers to the base class (possibly this
16358 class itself) which contains the vtable pointer for the current
16359 class from the DW_AT_containing_type attribute. This use of
16360 DW_AT_containing_type is a GNU extension. */
16361
16362 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16363 {
16364 struct type *t = die_containing_type (die, cu);
16365
16366 set_type_vptr_basetype (type, t);
16367 if (type == t)
16368 {
16369 int i;
16370
16371 /* Our own class provides vtbl ptr. */
16372 for (i = t->num_fields () - 1;
16373 i >= TYPE_N_BASECLASSES (t);
16374 --i)
16375 {
16376 const char *fieldname = TYPE_FIELD_NAME (t, i);
16377
16378 if (is_vtable_name (fieldname, cu))
16379 {
16380 set_type_vptr_fieldno (type, i);
16381 break;
16382 }
16383 }
16384
16385 /* Complain if virtual function table field not found. */
16386 if (i < TYPE_N_BASECLASSES (t))
16387 complaint (_("virtual function table pointer "
16388 "not found when defining class '%s'"),
16389 type->name () ? type->name () : "");
16390 }
16391 else
16392 {
16393 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16394 }
16395 }
16396 else if (cu->producer
16397 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16398 {
16399 /* The IBM XLC compiler does not provide direct indication
16400 of the containing type, but the vtable pointer is
16401 always named __vfp. */
16402
16403 int i;
16404
16405 for (i = type->num_fields () - 1;
16406 i >= TYPE_N_BASECLASSES (type);
16407 --i)
16408 {
16409 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16410 {
16411 set_type_vptr_fieldno (type, i);
16412 set_type_vptr_basetype (type, type);
16413 break;
16414 }
16415 }
16416 }
16417 }
16418
16419 /* Copy fi.typedef_field_list linked list elements content into the
16420 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16421 if (!fi.typedef_field_list.empty ())
16422 {
16423 int count = fi.typedef_field_list.size ();
16424
16425 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16426 TYPE_TYPEDEF_FIELD_ARRAY (type)
16427 = ((struct decl_field *)
16428 TYPE_ALLOC (type,
16429 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16430 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16431
16432 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16433 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16434 }
16435
16436 /* Copy fi.nested_types_list linked list elements content into the
16437 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16438 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16439 {
16440 int count = fi.nested_types_list.size ();
16441
16442 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16443 TYPE_NESTED_TYPES_ARRAY (type)
16444 = ((struct decl_field *)
16445 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16446 TYPE_NESTED_TYPES_COUNT (type) = count;
16447
16448 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16449 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16450 }
16451 }
16452
16453 quirk_gcc_member_function_pointer (type, objfile);
16454 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16455 cu->rust_unions.push_back (type);
16456 else if (cu->language == language_ada)
16457 quirk_ada_thick_pointer_struct (die, cu, type);
16458
16459 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16460 snapshots) has been known to create a die giving a declaration
16461 for a class that has, as a child, a die giving a definition for a
16462 nested class. So we have to process our children even if the
16463 current die is a declaration. Normally, of course, a declaration
16464 won't have any children at all. */
16465
16466 child_die = die->child;
16467
16468 while (child_die != NULL && child_die->tag)
16469 {
16470 if (child_die->tag == DW_TAG_member
16471 || child_die->tag == DW_TAG_variable
16472 || child_die->tag == DW_TAG_inheritance
16473 || child_die->tag == DW_TAG_template_value_param
16474 || child_die->tag == DW_TAG_template_type_param)
16475 {
16476 /* Do nothing. */
16477 }
16478 else
16479 process_die (child_die, cu);
16480
16481 child_die = child_die->sibling;
16482 }
16483
16484 /* Do not consider external references. According to the DWARF standard,
16485 these DIEs are identified by the fact that they have no byte_size
16486 attribute, and a declaration attribute. */
16487 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16488 || !die_is_declaration (die, cu)
16489 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16490 {
16491 struct symbol *sym = new_symbol (die, type, cu);
16492
16493 if (has_template_parameters)
16494 {
16495 struct symtab *symtab;
16496 if (sym != nullptr)
16497 symtab = symbol_symtab (sym);
16498 else if (cu->line_header != nullptr)
16499 {
16500 /* Any related symtab will do. */
16501 symtab
16502 = cu->line_header->file_names ()[0].symtab;
16503 }
16504 else
16505 {
16506 symtab = nullptr;
16507 complaint (_("could not find suitable "
16508 "symtab for template parameter"
16509 " - DIE at %s [in module %s]"),
16510 sect_offset_str (die->sect_off),
16511 objfile_name (objfile));
16512 }
16513
16514 if (symtab != nullptr)
16515 {
16516 /* Make sure that the symtab is set on the new symbols.
16517 Even though they don't appear in this symtab directly,
16518 other parts of gdb assume that symbols do, and this is
16519 reasonably true. */
16520 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16521 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16522 }
16523 }
16524 }
16525 }
16526
16527 /* Assuming DIE is an enumeration type, and TYPE is its associated
16528 type, update TYPE using some information only available in DIE's
16529 children. In particular, the fields are computed. */
16530
16531 static void
16532 update_enumeration_type_from_children (struct die_info *die,
16533 struct type *type,
16534 struct dwarf2_cu *cu)
16535 {
16536 struct die_info *child_die;
16537 int unsigned_enum = 1;
16538 int flag_enum = 1;
16539
16540 auto_obstack obstack;
16541 std::vector<struct field> fields;
16542
16543 for (child_die = die->child;
16544 child_die != NULL && child_die->tag;
16545 child_die = child_die->sibling)
16546 {
16547 struct attribute *attr;
16548 LONGEST value;
16549 const gdb_byte *bytes;
16550 struct dwarf2_locexpr_baton *baton;
16551 const char *name;
16552
16553 if (child_die->tag != DW_TAG_enumerator)
16554 continue;
16555
16556 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16557 if (attr == NULL)
16558 continue;
16559
16560 name = dwarf2_name (child_die, cu);
16561 if (name == NULL)
16562 name = "<anonymous enumerator>";
16563
16564 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16565 &value, &bytes, &baton);
16566 if (value < 0)
16567 {
16568 unsigned_enum = 0;
16569 flag_enum = 0;
16570 }
16571 else
16572 {
16573 if (count_one_bits_ll (value) >= 2)
16574 flag_enum = 0;
16575 }
16576
16577 fields.emplace_back ();
16578 struct field &field = fields.back ();
16579 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16580 SET_FIELD_ENUMVAL (field, value);
16581 }
16582
16583 if (!fields.empty ())
16584 {
16585 type->set_num_fields (fields.size ());
16586 type->set_fields
16587 ((struct field *)
16588 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16589 memcpy (type->fields (), fields.data (),
16590 sizeof (struct field) * fields.size ());
16591 }
16592
16593 if (unsigned_enum)
16594 type->set_is_unsigned (true);
16595
16596 if (flag_enum)
16597 TYPE_FLAG_ENUM (type) = 1;
16598 }
16599
16600 /* Given a DW_AT_enumeration_type die, set its type. We do not
16601 complete the type's fields yet, or create any symbols. */
16602
16603 static struct type *
16604 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16605 {
16606 struct objfile *objfile = cu->per_objfile->objfile;
16607 struct type *type;
16608 struct attribute *attr;
16609 const char *name;
16610
16611 /* If the definition of this type lives in .debug_types, read that type.
16612 Don't follow DW_AT_specification though, that will take us back up
16613 the chain and we want to go down. */
16614 attr = die->attr (DW_AT_signature);
16615 if (attr != nullptr)
16616 {
16617 type = get_DW_AT_signature_type (die, attr, cu);
16618
16619 /* The type's CU may not be the same as CU.
16620 Ensure TYPE is recorded with CU in die_type_hash. */
16621 return set_die_type (die, type, cu);
16622 }
16623
16624 type = alloc_type (objfile);
16625
16626 type->set_code (TYPE_CODE_ENUM);
16627 name = dwarf2_full_name (NULL, die, cu);
16628 if (name != NULL)
16629 type->set_name (name);
16630
16631 attr = dwarf2_attr (die, DW_AT_type, cu);
16632 if (attr != NULL)
16633 {
16634 struct type *underlying_type = die_type (die, cu);
16635
16636 TYPE_TARGET_TYPE (type) = underlying_type;
16637 }
16638
16639 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16640 if (attr != nullptr)
16641 {
16642 TYPE_LENGTH (type) = attr->constant_value (0);
16643 }
16644 else
16645 {
16646 TYPE_LENGTH (type) = 0;
16647 }
16648
16649 maybe_set_alignment (cu, die, type);
16650
16651 /* The enumeration DIE can be incomplete. In Ada, any type can be
16652 declared as private in the package spec, and then defined only
16653 inside the package body. Such types are known as Taft Amendment
16654 Types. When another package uses such a type, an incomplete DIE
16655 may be generated by the compiler. */
16656 if (die_is_declaration (die, cu))
16657 type->set_is_stub (true);
16658
16659 /* If this type has an underlying type that is not a stub, then we
16660 may use its attributes. We always use the "unsigned" attribute
16661 in this situation, because ordinarily we guess whether the type
16662 is unsigned -- but the guess can be wrong and the underlying type
16663 can tell us the reality. However, we defer to a local size
16664 attribute if one exists, because this lets the compiler override
16665 the underlying type if needed. */
16666 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16667 {
16668 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16669 underlying_type = check_typedef (underlying_type);
16670
16671 type->set_is_unsigned (underlying_type->is_unsigned ());
16672
16673 if (TYPE_LENGTH (type) == 0)
16674 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16675
16676 if (TYPE_RAW_ALIGN (type) == 0
16677 && TYPE_RAW_ALIGN (underlying_type) != 0)
16678 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16679 }
16680
16681 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16682
16683 set_die_type (die, type, cu);
16684
16685 /* Finish the creation of this type by using the enum's children.
16686 Note that, as usual, this must come after set_die_type to avoid
16687 infinite recursion when trying to compute the names of the
16688 enumerators. */
16689 update_enumeration_type_from_children (die, type, cu);
16690
16691 return type;
16692 }
16693
16694 /* Given a pointer to a die which begins an enumeration, process all
16695 the dies that define the members of the enumeration, and create the
16696 symbol for the enumeration type.
16697
16698 NOTE: We reverse the order of the element list. */
16699
16700 static void
16701 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16702 {
16703 struct type *this_type;
16704
16705 this_type = get_die_type (die, cu);
16706 if (this_type == NULL)
16707 this_type = read_enumeration_type (die, cu);
16708
16709 if (die->child != NULL)
16710 {
16711 struct die_info *child_die;
16712 const char *name;
16713
16714 child_die = die->child;
16715 while (child_die && child_die->tag)
16716 {
16717 if (child_die->tag != DW_TAG_enumerator)
16718 {
16719 process_die (child_die, cu);
16720 }
16721 else
16722 {
16723 name = dwarf2_name (child_die, cu);
16724 if (name)
16725 new_symbol (child_die, this_type, cu);
16726 }
16727
16728 child_die = child_die->sibling;
16729 }
16730 }
16731
16732 /* If we are reading an enum from a .debug_types unit, and the enum
16733 is a declaration, and the enum is not the signatured type in the
16734 unit, then we do not want to add a symbol for it. Adding a
16735 symbol would in some cases obscure the true definition of the
16736 enum, giving users an incomplete type when the definition is
16737 actually available. Note that we do not want to do this for all
16738 enums which are just declarations, because C++0x allows forward
16739 enum declarations. */
16740 if (cu->per_cu->is_debug_types
16741 && die_is_declaration (die, cu))
16742 {
16743 struct signatured_type *sig_type;
16744
16745 sig_type = (struct signatured_type *) cu->per_cu;
16746 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16747 if (sig_type->type_offset_in_section != die->sect_off)
16748 return;
16749 }
16750
16751 new_symbol (die, this_type, cu);
16752 }
16753
16754 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16755 expression for an index type and finds the corresponding field
16756 offset in the hidden "P_BOUNDS" structure. Returns true on success
16757 and updates *FIELD, false if it fails to recognize an
16758 expression. */
16759
16760 static bool
16761 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16762 int *bounds_offset, struct field *field,
16763 struct dwarf2_cu *cu)
16764 {
16765 struct attribute *attr = dwarf2_attr (die, name, cu);
16766 if (attr == nullptr || !attr->form_is_block ())
16767 return false;
16768
16769 const struct dwarf_block *block = attr->as_block ();
16770 const gdb_byte *start = block->data;
16771 const gdb_byte *end = block->data + block->size;
16772
16773 /* The expression to recognize generally looks like:
16774
16775 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16776 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16777
16778 However, the second "plus_uconst" may be missing:
16779
16780 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16781 DW_OP_deref_size: 4)
16782
16783 This happens when the field is at the start of the structure.
16784
16785 Also, the final deref may not be sized:
16786
16787 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16788 DW_OP_deref)
16789
16790 This happens when the size of the index type happens to be the
16791 same as the architecture's word size. This can occur with or
16792 without the second plus_uconst. */
16793
16794 if (end - start < 2)
16795 return false;
16796 if (*start++ != DW_OP_push_object_address)
16797 return false;
16798 if (*start++ != DW_OP_plus_uconst)
16799 return false;
16800
16801 uint64_t this_bound_off;
16802 start = gdb_read_uleb128 (start, end, &this_bound_off);
16803 if (start == nullptr || (int) this_bound_off != this_bound_off)
16804 return false;
16805 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16806 is consistent among all bounds. */
16807 if (*bounds_offset == -1)
16808 *bounds_offset = this_bound_off;
16809 else if (*bounds_offset != this_bound_off)
16810 return false;
16811
16812 if (start == end || *start++ != DW_OP_deref)
16813 return false;
16814
16815 int offset = 0;
16816 if (start ==end)
16817 return false;
16818 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16819 {
16820 /* This means an offset of 0. */
16821 }
16822 else if (*start++ != DW_OP_plus_uconst)
16823 return false;
16824 else
16825 {
16826 /* The size is the parameter to DW_OP_plus_uconst. */
16827 uint64_t val;
16828 start = gdb_read_uleb128 (start, end, &val);
16829 if (start == nullptr)
16830 return false;
16831 if ((int) val != val)
16832 return false;
16833 offset = val;
16834 }
16835
16836 if (start == end)
16837 return false;
16838
16839 uint64_t size;
16840 if (*start == DW_OP_deref_size)
16841 {
16842 start = gdb_read_uleb128 (start + 1, end, &size);
16843 if (start == nullptr)
16844 return false;
16845 }
16846 else if (*start == DW_OP_deref)
16847 {
16848 size = cu->header.addr_size;
16849 ++start;
16850 }
16851 else
16852 return false;
16853
16854 SET_FIELD_BITPOS (*field, 8 * offset);
16855 if (size != TYPE_LENGTH (field->type ()))
16856 FIELD_BITSIZE (*field) = 8 * size;
16857
16858 return true;
16859 }
16860
16861 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16862 some kinds of Ada arrays:
16863
16864 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16865 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16866 <11e0> DW_AT_data_location: 2 byte block: 97 6
16867 (DW_OP_push_object_address; DW_OP_deref)
16868 <11e3> DW_AT_type : <0x1173>
16869 <11e7> DW_AT_sibling : <0x1201>
16870 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16871 <11ec> DW_AT_type : <0x1206>
16872 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16873 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16874 DW_OP_deref_size: 4)
16875 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16876 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16877 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16878
16879 This actually represents a "thick pointer", which is a structure
16880 with two elements: one that is a pointer to the array data, and one
16881 that is a pointer to another structure; this second structure holds
16882 the array bounds.
16883
16884 This returns a new type on success, or nullptr if this didn't
16885 recognize the type. */
16886
16887 static struct type *
16888 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16889 struct type *type)
16890 {
16891 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16892 /* So far we've only seen this with block form. */
16893 if (attr == nullptr || !attr->form_is_block ())
16894 return nullptr;
16895
16896 /* Note that this will fail if the structure layout is changed by
16897 the compiler. However, we have no good way to recognize some
16898 other layout, because we don't know what expression the compiler
16899 might choose to emit should this happen. */
16900 struct dwarf_block *blk = attr->as_block ();
16901 if (blk->size != 2
16902 || blk->data[0] != DW_OP_push_object_address
16903 || blk->data[1] != DW_OP_deref)
16904 return nullptr;
16905
16906 int bounds_offset = -1;
16907 int max_align = -1;
16908 std::vector<struct field> range_fields;
16909 for (struct die_info *child_die = die->child;
16910 child_die;
16911 child_die = child_die->sibling)
16912 {
16913 if (child_die->tag == DW_TAG_subrange_type)
16914 {
16915 struct type *underlying = read_subrange_index_type (child_die, cu);
16916
16917 int this_align = type_align (underlying);
16918 if (this_align > max_align)
16919 max_align = this_align;
16920
16921 range_fields.emplace_back ();
16922 range_fields.emplace_back ();
16923
16924 struct field &lower = range_fields[range_fields.size () - 2];
16925 struct field &upper = range_fields[range_fields.size () - 1];
16926
16927 lower.set_type (underlying);
16928 FIELD_ARTIFICIAL (lower) = 1;
16929
16930 upper.set_type (underlying);
16931 FIELD_ARTIFICIAL (upper) = 1;
16932
16933 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16934 &bounds_offset, &lower, cu)
16935 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16936 &bounds_offset, &upper, cu))
16937 return nullptr;
16938 }
16939 }
16940
16941 /* This shouldn't really happen, but double-check that we found
16942 where the bounds are stored. */
16943 if (bounds_offset == -1)
16944 return nullptr;
16945
16946 struct objfile *objfile = cu->per_objfile->objfile;
16947 for (int i = 0; i < range_fields.size (); i += 2)
16948 {
16949 char name[20];
16950
16951 /* Set the name of each field in the bounds. */
16952 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16953 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16954 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16955 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16956 }
16957
16958 struct type *bounds = alloc_type (objfile);
16959 bounds->set_code (TYPE_CODE_STRUCT);
16960
16961 bounds->set_num_fields (range_fields.size ());
16962 bounds->set_fields
16963 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16964 * sizeof (struct field))));
16965 memcpy (bounds->fields (), range_fields.data (),
16966 bounds->num_fields () * sizeof (struct field));
16967
16968 int last_fieldno = range_fields.size () - 1;
16969 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
16970 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16971 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16972
16973 /* Rewrite the existing array type in place. Specifically, we
16974 remove any dynamic properties we might have read, and we replace
16975 the index types. */
16976 struct type *iter = type;
16977 for (int i = 0; i < range_fields.size (); i += 2)
16978 {
16979 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16980 iter->main_type->dyn_prop_list = nullptr;
16981 iter->set_index_type
16982 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16983 iter = TYPE_TARGET_TYPE (iter);
16984 }
16985
16986 struct type *result = alloc_type (objfile);
16987 result->set_code (TYPE_CODE_STRUCT);
16988
16989 result->set_num_fields (2);
16990 result->set_fields
16991 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16992 * sizeof (struct field))));
16993
16994 /* The names are chosen to coincide with what the compiler does with
16995 -fgnat-encodings=all, which the Ada code in gdb already
16996 understands. */
16997 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
16998 result->field (0).set_type (lookup_pointer_type (type));
16999
17000 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
17001 result->field (1).set_type (lookup_pointer_type (bounds));
17002 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
17003
17004 result->set_name (type->name ());
17005 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
17006 + TYPE_LENGTH (result->field (1).type ()));
17007
17008 return result;
17009 }
17010
17011 /* Extract all information from a DW_TAG_array_type DIE and put it in
17012 the DIE's type field. For now, this only handles one dimensional
17013 arrays. */
17014
17015 static struct type *
17016 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
17017 {
17018 struct objfile *objfile = cu->per_objfile->objfile;
17019 struct die_info *child_die;
17020 struct type *type;
17021 struct type *element_type, *range_type, *index_type;
17022 struct attribute *attr;
17023 const char *name;
17024 struct dynamic_prop *byte_stride_prop = NULL;
17025 unsigned int bit_stride = 0;
17026
17027 element_type = die_type (die, cu);
17028
17029 /* The die_type call above may have already set the type for this DIE. */
17030 type = get_die_type (die, cu);
17031 if (type)
17032 return type;
17033
17034 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
17035 if (attr != NULL)
17036 {
17037 int stride_ok;
17038 struct type *prop_type = cu->addr_sized_int_type (false);
17039
17040 byte_stride_prop
17041 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
17042 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
17043 prop_type);
17044 if (!stride_ok)
17045 {
17046 complaint (_("unable to read array DW_AT_byte_stride "
17047 " - DIE at %s [in module %s]"),
17048 sect_offset_str (die->sect_off),
17049 objfile_name (cu->per_objfile->objfile));
17050 /* Ignore this attribute. We will likely not be able to print
17051 arrays of this type correctly, but there is little we can do
17052 to help if we cannot read the attribute's value. */
17053 byte_stride_prop = NULL;
17054 }
17055 }
17056
17057 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
17058 if (attr != NULL)
17059 bit_stride = attr->constant_value (0);
17060
17061 /* Irix 6.2 native cc creates array types without children for
17062 arrays with unspecified length. */
17063 if (die->child == NULL)
17064 {
17065 index_type = objfile_type (objfile)->builtin_int;
17066 range_type = create_static_range_type (NULL, index_type, 0, -1);
17067 type = create_array_type_with_stride (NULL, element_type, range_type,
17068 byte_stride_prop, bit_stride);
17069 return set_die_type (die, type, cu);
17070 }
17071
17072 std::vector<struct type *> range_types;
17073 child_die = die->child;
17074 while (child_die && child_die->tag)
17075 {
17076 if (child_die->tag == DW_TAG_subrange_type)
17077 {
17078 struct type *child_type = read_type_die (child_die, cu);
17079
17080 if (child_type != NULL)
17081 {
17082 /* The range type was succesfully read. Save it for the
17083 array type creation. */
17084 range_types.push_back (child_type);
17085 }
17086 }
17087 child_die = child_die->sibling;
17088 }
17089
17090 /* Dwarf2 dimensions are output from left to right, create the
17091 necessary array types in backwards order. */
17092
17093 type = element_type;
17094
17095 if (read_array_order (die, cu) == DW_ORD_col_major)
17096 {
17097 int i = 0;
17098
17099 while (i < range_types.size ())
17100 {
17101 type = create_array_type_with_stride (NULL, type, range_types[i++],
17102 byte_stride_prop, bit_stride);
17103 bit_stride = 0;
17104 byte_stride_prop = nullptr;
17105 }
17106 }
17107 else
17108 {
17109 size_t ndim = range_types.size ();
17110 while (ndim-- > 0)
17111 {
17112 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17113 byte_stride_prop, bit_stride);
17114 bit_stride = 0;
17115 byte_stride_prop = nullptr;
17116 }
17117 }
17118
17119 /* Understand Dwarf2 support for vector types (like they occur on
17120 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17121 array type. This is not part of the Dwarf2/3 standard yet, but a
17122 custom vendor extension. The main difference between a regular
17123 array and the vector variant is that vectors are passed by value
17124 to functions. */
17125 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
17126 if (attr != nullptr)
17127 make_vector_type (type);
17128
17129 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17130 implementation may choose to implement triple vectors using this
17131 attribute. */
17132 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17133 if (attr != nullptr && attr->form_is_unsigned ())
17134 {
17135 if (attr->as_unsigned () >= TYPE_LENGTH (type))
17136 TYPE_LENGTH (type) = attr->as_unsigned ();
17137 else
17138 complaint (_("DW_AT_byte_size for array type smaller "
17139 "than the total size of elements"));
17140 }
17141
17142 name = dwarf2_name (die, cu);
17143 if (name)
17144 type->set_name (name);
17145
17146 maybe_set_alignment (cu, die, type);
17147
17148 struct type *replacement_type = nullptr;
17149 if (cu->language == language_ada)
17150 {
17151 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17152 if (replacement_type != nullptr)
17153 type = replacement_type;
17154 }
17155
17156 /* Install the type in the die. */
17157 set_die_type (die, type, cu, replacement_type != nullptr);
17158
17159 /* set_die_type should be already done. */
17160 set_descriptive_type (type, die, cu);
17161
17162 return type;
17163 }
17164
17165 static enum dwarf_array_dim_ordering
17166 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
17167 {
17168 struct attribute *attr;
17169
17170 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17171
17172 if (attr != nullptr)
17173 {
17174 LONGEST val = attr->constant_value (-1);
17175 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17176 return (enum dwarf_array_dim_ordering) val;
17177 }
17178
17179 /* GNU F77 is a special case, as at 08/2004 array type info is the
17180 opposite order to the dwarf2 specification, but data is still
17181 laid out as per normal fortran.
17182
17183 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17184 version checking. */
17185
17186 if (cu->language == language_fortran
17187 && cu->producer && strstr (cu->producer, "GNU F77"))
17188 {
17189 return DW_ORD_row_major;
17190 }
17191
17192 switch (cu->language_defn->array_ordering ())
17193 {
17194 case array_column_major:
17195 return DW_ORD_col_major;
17196 case array_row_major:
17197 default:
17198 return DW_ORD_row_major;
17199 };
17200 }
17201
17202 /* Extract all information from a DW_TAG_set_type DIE and put it in
17203 the DIE's type field. */
17204
17205 static struct type *
17206 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17207 {
17208 struct type *domain_type, *set_type;
17209 struct attribute *attr;
17210
17211 domain_type = die_type (die, cu);
17212
17213 /* The die_type call above may have already set the type for this DIE. */
17214 set_type = get_die_type (die, cu);
17215 if (set_type)
17216 return set_type;
17217
17218 set_type = create_set_type (NULL, domain_type);
17219
17220 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17221 if (attr != nullptr && attr->form_is_unsigned ())
17222 TYPE_LENGTH (set_type) = attr->as_unsigned ();
17223
17224 maybe_set_alignment (cu, die, set_type);
17225
17226 return set_die_type (die, set_type, cu);
17227 }
17228
17229 /* A helper for read_common_block that creates a locexpr baton.
17230 SYM is the symbol which we are marking as computed.
17231 COMMON_DIE is the DIE for the common block.
17232 COMMON_LOC is the location expression attribute for the common
17233 block itself.
17234 MEMBER_LOC is the location expression attribute for the particular
17235 member of the common block that we are processing.
17236 CU is the CU from which the above come. */
17237
17238 static void
17239 mark_common_block_symbol_computed (struct symbol *sym,
17240 struct die_info *common_die,
17241 struct attribute *common_loc,
17242 struct attribute *member_loc,
17243 struct dwarf2_cu *cu)
17244 {
17245 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17246 struct objfile *objfile = per_objfile->objfile;
17247 struct dwarf2_locexpr_baton *baton;
17248 gdb_byte *ptr;
17249 unsigned int cu_off;
17250 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
17251 LONGEST offset = 0;
17252
17253 gdb_assert (common_loc && member_loc);
17254 gdb_assert (common_loc->form_is_block ());
17255 gdb_assert (member_loc->form_is_block ()
17256 || member_loc->form_is_constant ());
17257
17258 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
17259 baton->per_objfile = per_objfile;
17260 baton->per_cu = cu->per_cu;
17261 gdb_assert (baton->per_cu);
17262
17263 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17264
17265 if (member_loc->form_is_constant ())
17266 {
17267 offset = member_loc->constant_value (0);
17268 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17269 }
17270 else
17271 baton->size += member_loc->as_block ()->size;
17272
17273 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
17274 baton->data = ptr;
17275
17276 *ptr++ = DW_OP_call4;
17277 cu_off = common_die->sect_off - cu->per_cu->sect_off;
17278 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17279 ptr += 4;
17280
17281 if (member_loc->form_is_constant ())
17282 {
17283 *ptr++ = DW_OP_addr;
17284 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17285 ptr += cu->header.addr_size;
17286 }
17287 else
17288 {
17289 /* We have to copy the data here, because DW_OP_call4 will only
17290 use a DW_AT_location attribute. */
17291 struct dwarf_block *block = member_loc->as_block ();
17292 memcpy (ptr, block->data, block->size);
17293 ptr += block->size;
17294 }
17295
17296 *ptr++ = DW_OP_plus;
17297 gdb_assert (ptr - baton->data == baton->size);
17298
17299 SYMBOL_LOCATION_BATON (sym) = baton;
17300 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17301 }
17302
17303 /* Create appropriate locally-scoped variables for all the
17304 DW_TAG_common_block entries. Also create a struct common_block
17305 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17306 is used to separate the common blocks name namespace from regular
17307 variable names. */
17308
17309 static void
17310 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17311 {
17312 struct attribute *attr;
17313
17314 attr = dwarf2_attr (die, DW_AT_location, cu);
17315 if (attr != nullptr)
17316 {
17317 /* Support the .debug_loc offsets. */
17318 if (attr->form_is_block ())
17319 {
17320 /* Ok. */
17321 }
17322 else if (attr->form_is_section_offset ())
17323 {
17324 dwarf2_complex_location_expr_complaint ();
17325 attr = NULL;
17326 }
17327 else
17328 {
17329 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17330 "common block member");
17331 attr = NULL;
17332 }
17333 }
17334
17335 if (die->child != NULL)
17336 {
17337 struct objfile *objfile = cu->per_objfile->objfile;
17338 struct die_info *child_die;
17339 size_t n_entries = 0, size;
17340 struct common_block *common_block;
17341 struct symbol *sym;
17342
17343 for (child_die = die->child;
17344 child_die && child_die->tag;
17345 child_die = child_die->sibling)
17346 ++n_entries;
17347
17348 size = (sizeof (struct common_block)
17349 + (n_entries - 1) * sizeof (struct symbol *));
17350 common_block
17351 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17352 size);
17353 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17354 common_block->n_entries = 0;
17355
17356 for (child_die = die->child;
17357 child_die && child_die->tag;
17358 child_die = child_die->sibling)
17359 {
17360 /* Create the symbol in the DW_TAG_common_block block in the current
17361 symbol scope. */
17362 sym = new_symbol (child_die, NULL, cu);
17363 if (sym != NULL)
17364 {
17365 struct attribute *member_loc;
17366
17367 common_block->contents[common_block->n_entries++] = sym;
17368
17369 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17370 cu);
17371 if (member_loc)
17372 {
17373 /* GDB has handled this for a long time, but it is
17374 not specified by DWARF. It seems to have been
17375 emitted by gfortran at least as recently as:
17376 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17377 complaint (_("Variable in common block has "
17378 "DW_AT_data_member_location "
17379 "- DIE at %s [in module %s]"),
17380 sect_offset_str (child_die->sect_off),
17381 objfile_name (objfile));
17382
17383 if (member_loc->form_is_section_offset ())
17384 dwarf2_complex_location_expr_complaint ();
17385 else if (member_loc->form_is_constant ()
17386 || member_loc->form_is_block ())
17387 {
17388 if (attr != nullptr)
17389 mark_common_block_symbol_computed (sym, die, attr,
17390 member_loc, cu);
17391 }
17392 else
17393 dwarf2_complex_location_expr_complaint ();
17394 }
17395 }
17396 }
17397
17398 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17399 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17400 }
17401 }
17402
17403 /* Create a type for a C++ namespace. */
17404
17405 static struct type *
17406 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17407 {
17408 struct objfile *objfile = cu->per_objfile->objfile;
17409 const char *previous_prefix, *name;
17410 int is_anonymous;
17411 struct type *type;
17412
17413 /* For extensions, reuse the type of the original namespace. */
17414 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17415 {
17416 struct die_info *ext_die;
17417 struct dwarf2_cu *ext_cu = cu;
17418
17419 ext_die = dwarf2_extension (die, &ext_cu);
17420 type = read_type_die (ext_die, ext_cu);
17421
17422 /* EXT_CU may not be the same as CU.
17423 Ensure TYPE is recorded with CU in die_type_hash. */
17424 return set_die_type (die, type, cu);
17425 }
17426
17427 name = namespace_name (die, &is_anonymous, cu);
17428
17429 /* Now build the name of the current namespace. */
17430
17431 previous_prefix = determine_prefix (die, cu);
17432 if (previous_prefix[0] != '\0')
17433 name = typename_concat (&objfile->objfile_obstack,
17434 previous_prefix, name, 0, cu);
17435
17436 /* Create the type. */
17437 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17438
17439 return set_die_type (die, type, cu);
17440 }
17441
17442 /* Read a namespace scope. */
17443
17444 static void
17445 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17446 {
17447 struct objfile *objfile = cu->per_objfile->objfile;
17448 int is_anonymous;
17449
17450 /* Add a symbol associated to this if we haven't seen the namespace
17451 before. Also, add a using directive if it's an anonymous
17452 namespace. */
17453
17454 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17455 {
17456 struct type *type;
17457
17458 type = read_type_die (die, cu);
17459 new_symbol (die, type, cu);
17460
17461 namespace_name (die, &is_anonymous, cu);
17462 if (is_anonymous)
17463 {
17464 const char *previous_prefix = determine_prefix (die, cu);
17465
17466 std::vector<const char *> excludes;
17467 add_using_directive (using_directives (cu),
17468 previous_prefix, type->name (), NULL,
17469 NULL, excludes, 0, &objfile->objfile_obstack);
17470 }
17471 }
17472
17473 if (die->child != NULL)
17474 {
17475 struct die_info *child_die = die->child;
17476
17477 while (child_die && child_die->tag)
17478 {
17479 process_die (child_die, cu);
17480 child_die = child_die->sibling;
17481 }
17482 }
17483 }
17484
17485 /* Read a Fortran module as type. This DIE can be only a declaration used for
17486 imported module. Still we need that type as local Fortran "use ... only"
17487 declaration imports depend on the created type in determine_prefix. */
17488
17489 static struct type *
17490 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17491 {
17492 struct objfile *objfile = cu->per_objfile->objfile;
17493 const char *module_name;
17494 struct type *type;
17495
17496 module_name = dwarf2_name (die, cu);
17497 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17498
17499 return set_die_type (die, type, cu);
17500 }
17501
17502 /* Read a Fortran module. */
17503
17504 static void
17505 read_module (struct die_info *die, struct dwarf2_cu *cu)
17506 {
17507 struct die_info *child_die = die->child;
17508 struct type *type;
17509
17510 type = read_type_die (die, cu);
17511 new_symbol (die, type, cu);
17512
17513 while (child_die && child_die->tag)
17514 {
17515 process_die (child_die, cu);
17516 child_die = child_die->sibling;
17517 }
17518 }
17519
17520 /* Return the name of the namespace represented by DIE. Set
17521 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17522 namespace. */
17523
17524 static const char *
17525 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17526 {
17527 struct die_info *current_die;
17528 const char *name = NULL;
17529
17530 /* Loop through the extensions until we find a name. */
17531
17532 for (current_die = die;
17533 current_die != NULL;
17534 current_die = dwarf2_extension (die, &cu))
17535 {
17536 /* We don't use dwarf2_name here so that we can detect the absence
17537 of a name -> anonymous namespace. */
17538 name = dwarf2_string_attr (die, DW_AT_name, cu);
17539
17540 if (name != NULL)
17541 break;
17542 }
17543
17544 /* Is it an anonymous namespace? */
17545
17546 *is_anonymous = (name == NULL);
17547 if (*is_anonymous)
17548 name = CP_ANONYMOUS_NAMESPACE_STR;
17549
17550 return name;
17551 }
17552
17553 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17554 the user defined type vector. */
17555
17556 static struct type *
17557 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17558 {
17559 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17560 struct comp_unit_head *cu_header = &cu->header;
17561 struct type *type;
17562 struct attribute *attr_byte_size;
17563 struct attribute *attr_address_class;
17564 int byte_size, addr_class;
17565 struct type *target_type;
17566
17567 target_type = die_type (die, cu);
17568
17569 /* The die_type call above may have already set the type for this DIE. */
17570 type = get_die_type (die, cu);
17571 if (type)
17572 return type;
17573
17574 type = lookup_pointer_type (target_type);
17575
17576 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17577 if (attr_byte_size)
17578 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17579 else
17580 byte_size = cu_header->addr_size;
17581
17582 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17583 if (attr_address_class)
17584 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17585 else
17586 addr_class = DW_ADDR_none;
17587
17588 ULONGEST alignment = get_alignment (cu, die);
17589
17590 /* If the pointer size, alignment, or address class is different
17591 than the default, create a type variant marked as such and set
17592 the length accordingly. */
17593 if (TYPE_LENGTH (type) != byte_size
17594 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17595 && alignment != TYPE_RAW_ALIGN (type))
17596 || addr_class != DW_ADDR_none)
17597 {
17598 if (gdbarch_address_class_type_flags_p (gdbarch))
17599 {
17600 type_instance_flags type_flags
17601 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17602 addr_class);
17603 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17604 == 0);
17605 type = make_type_with_address_space (type, type_flags);
17606 }
17607 else if (TYPE_LENGTH (type) != byte_size)
17608 {
17609 complaint (_("invalid pointer size %d"), byte_size);
17610 }
17611 else if (TYPE_RAW_ALIGN (type) != alignment)
17612 {
17613 complaint (_("Invalid DW_AT_alignment"
17614 " - DIE at %s [in module %s]"),
17615 sect_offset_str (die->sect_off),
17616 objfile_name (cu->per_objfile->objfile));
17617 }
17618 else
17619 {
17620 /* Should we also complain about unhandled address classes? */
17621 }
17622 }
17623
17624 TYPE_LENGTH (type) = byte_size;
17625 set_type_align (type, alignment);
17626 return set_die_type (die, type, cu);
17627 }
17628
17629 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17630 the user defined type vector. */
17631
17632 static struct type *
17633 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17634 {
17635 struct type *type;
17636 struct type *to_type;
17637 struct type *domain;
17638
17639 to_type = die_type (die, cu);
17640 domain = die_containing_type (die, cu);
17641
17642 /* The calls above may have already set the type for this DIE. */
17643 type = get_die_type (die, cu);
17644 if (type)
17645 return type;
17646
17647 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17648 type = lookup_methodptr_type (to_type);
17649 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17650 {
17651 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17652
17653 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17654 to_type->fields (), to_type->num_fields (),
17655 to_type->has_varargs ());
17656 type = lookup_methodptr_type (new_type);
17657 }
17658 else
17659 type = lookup_memberptr_type (to_type, domain);
17660
17661 return set_die_type (die, type, cu);
17662 }
17663
17664 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17665 the user defined type vector. */
17666
17667 static struct type *
17668 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17669 enum type_code refcode)
17670 {
17671 struct comp_unit_head *cu_header = &cu->header;
17672 struct type *type, *target_type;
17673 struct attribute *attr;
17674
17675 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17676
17677 target_type = die_type (die, cu);
17678
17679 /* The die_type call above may have already set the type for this DIE. */
17680 type = get_die_type (die, cu);
17681 if (type)
17682 return type;
17683
17684 type = lookup_reference_type (target_type, refcode);
17685 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17686 if (attr != nullptr)
17687 {
17688 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17689 }
17690 else
17691 {
17692 TYPE_LENGTH (type) = cu_header->addr_size;
17693 }
17694 maybe_set_alignment (cu, die, type);
17695 return set_die_type (die, type, cu);
17696 }
17697
17698 /* Add the given cv-qualifiers to the element type of the array. GCC
17699 outputs DWARF type qualifiers that apply to an array, not the
17700 element type. But GDB relies on the array element type to carry
17701 the cv-qualifiers. This mimics section 6.7.3 of the C99
17702 specification. */
17703
17704 static struct type *
17705 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17706 struct type *base_type, int cnst, int voltl)
17707 {
17708 struct type *el_type, *inner_array;
17709
17710 base_type = copy_type (base_type);
17711 inner_array = base_type;
17712
17713 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17714 {
17715 TYPE_TARGET_TYPE (inner_array) =
17716 copy_type (TYPE_TARGET_TYPE (inner_array));
17717 inner_array = TYPE_TARGET_TYPE (inner_array);
17718 }
17719
17720 el_type = TYPE_TARGET_TYPE (inner_array);
17721 cnst |= TYPE_CONST (el_type);
17722 voltl |= TYPE_VOLATILE (el_type);
17723 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17724
17725 return set_die_type (die, base_type, cu);
17726 }
17727
17728 static struct type *
17729 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17730 {
17731 struct type *base_type, *cv_type;
17732
17733 base_type = die_type (die, cu);
17734
17735 /* The die_type call above may have already set the type for this DIE. */
17736 cv_type = get_die_type (die, cu);
17737 if (cv_type)
17738 return cv_type;
17739
17740 /* In case the const qualifier is applied to an array type, the element type
17741 is so qualified, not the array type (section 6.7.3 of C99). */
17742 if (base_type->code () == TYPE_CODE_ARRAY)
17743 return add_array_cv_type (die, cu, base_type, 1, 0);
17744
17745 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17746 return set_die_type (die, cv_type, cu);
17747 }
17748
17749 static struct type *
17750 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17751 {
17752 struct type *base_type, *cv_type;
17753
17754 base_type = die_type (die, cu);
17755
17756 /* The die_type call above may have already set the type for this DIE. */
17757 cv_type = get_die_type (die, cu);
17758 if (cv_type)
17759 return cv_type;
17760
17761 /* In case the volatile qualifier is applied to an array type, the
17762 element type is so qualified, not the array type (section 6.7.3
17763 of C99). */
17764 if (base_type->code () == TYPE_CODE_ARRAY)
17765 return add_array_cv_type (die, cu, base_type, 0, 1);
17766
17767 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17768 return set_die_type (die, cv_type, cu);
17769 }
17770
17771 /* Handle DW_TAG_restrict_type. */
17772
17773 static struct type *
17774 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17775 {
17776 struct type *base_type, *cv_type;
17777
17778 base_type = die_type (die, cu);
17779
17780 /* The die_type call above may have already set the type for this DIE. */
17781 cv_type = get_die_type (die, cu);
17782 if (cv_type)
17783 return cv_type;
17784
17785 cv_type = make_restrict_type (base_type);
17786 return set_die_type (die, cv_type, cu);
17787 }
17788
17789 /* Handle DW_TAG_atomic_type. */
17790
17791 static struct type *
17792 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17793 {
17794 struct type *base_type, *cv_type;
17795
17796 base_type = die_type (die, cu);
17797
17798 /* The die_type call above may have already set the type for this DIE. */
17799 cv_type = get_die_type (die, cu);
17800 if (cv_type)
17801 return cv_type;
17802
17803 cv_type = make_atomic_type (base_type);
17804 return set_die_type (die, cv_type, cu);
17805 }
17806
17807 /* Extract all information from a DW_TAG_string_type DIE and add to
17808 the user defined type vector. It isn't really a user defined type,
17809 but it behaves like one, with other DIE's using an AT_user_def_type
17810 attribute to reference it. */
17811
17812 static struct type *
17813 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17814 {
17815 struct objfile *objfile = cu->per_objfile->objfile;
17816 struct gdbarch *gdbarch = objfile->arch ();
17817 struct type *type, *range_type, *index_type, *char_type;
17818 struct attribute *attr;
17819 struct dynamic_prop prop;
17820 bool length_is_constant = true;
17821 LONGEST length;
17822
17823 /* There are a couple of places where bit sizes might be made use of
17824 when parsing a DW_TAG_string_type, however, no producer that we know
17825 of make use of these. Handling bit sizes that are a multiple of the
17826 byte size is easy enough, but what about other bit sizes? Lets deal
17827 with that problem when we have to. Warn about these attributes being
17828 unsupported, then parse the type and ignore them like we always
17829 have. */
17830 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17831 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17832 {
17833 static bool warning_printed = false;
17834 if (!warning_printed)
17835 {
17836 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17837 "currently supported on DW_TAG_string_type."));
17838 warning_printed = true;
17839 }
17840 }
17841
17842 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17843 if (attr != nullptr && !attr->form_is_constant ())
17844 {
17845 /* The string length describes the location at which the length of
17846 the string can be found. The size of the length field can be
17847 specified with one of the attributes below. */
17848 struct type *prop_type;
17849 struct attribute *len
17850 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17851 if (len == nullptr)
17852 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17853 if (len != nullptr && len->form_is_constant ())
17854 {
17855 /* Pass 0 as the default as we know this attribute is constant
17856 and the default value will not be returned. */
17857 LONGEST sz = len->constant_value (0);
17858 prop_type = cu->per_objfile->int_type (sz, true);
17859 }
17860 else
17861 {
17862 /* If the size is not specified then we assume it is the size of
17863 an address on this target. */
17864 prop_type = cu->addr_sized_int_type (true);
17865 }
17866
17867 /* Convert the attribute into a dynamic property. */
17868 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17869 length = 1;
17870 else
17871 length_is_constant = false;
17872 }
17873 else if (attr != nullptr)
17874 {
17875 /* This DW_AT_string_length just contains the length with no
17876 indirection. There's no need to create a dynamic property in this
17877 case. Pass 0 for the default value as we know it will not be
17878 returned in this case. */
17879 length = attr->constant_value (0);
17880 }
17881 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17882 {
17883 /* We don't currently support non-constant byte sizes for strings. */
17884 length = attr->constant_value (1);
17885 }
17886 else
17887 {
17888 /* Use 1 as a fallback length if we have nothing else. */
17889 length = 1;
17890 }
17891
17892 index_type = objfile_type (objfile)->builtin_int;
17893 if (length_is_constant)
17894 range_type = create_static_range_type (NULL, index_type, 1, length);
17895 else
17896 {
17897 struct dynamic_prop low_bound;
17898
17899 low_bound.set_const_val (1);
17900 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17901 }
17902 char_type = language_string_char_type (cu->language_defn, gdbarch);
17903 type = create_string_type (NULL, char_type, range_type);
17904
17905 return set_die_type (die, type, cu);
17906 }
17907
17908 /* Assuming that DIE corresponds to a function, returns nonzero
17909 if the function is prototyped. */
17910
17911 static int
17912 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17913 {
17914 struct attribute *attr;
17915
17916 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17917 if (attr && attr->as_boolean ())
17918 return 1;
17919
17920 /* The DWARF standard implies that the DW_AT_prototyped attribute
17921 is only meaningful for C, but the concept also extends to other
17922 languages that allow unprototyped functions (Eg: Objective C).
17923 For all other languages, assume that functions are always
17924 prototyped. */
17925 if (cu->language != language_c
17926 && cu->language != language_objc
17927 && cu->language != language_opencl)
17928 return 1;
17929
17930 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17931 prototyped and unprototyped functions; default to prototyped,
17932 since that is more common in modern code (and RealView warns
17933 about unprototyped functions). */
17934 if (producer_is_realview (cu->producer))
17935 return 1;
17936
17937 return 0;
17938 }
17939
17940 /* Handle DIES due to C code like:
17941
17942 struct foo
17943 {
17944 int (*funcp)(int a, long l);
17945 int b;
17946 };
17947
17948 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17949
17950 static struct type *
17951 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17952 {
17953 struct objfile *objfile = cu->per_objfile->objfile;
17954 struct type *type; /* Type that this function returns. */
17955 struct type *ftype; /* Function that returns above type. */
17956 struct attribute *attr;
17957
17958 type = die_type (die, cu);
17959
17960 /* The die_type call above may have already set the type for this DIE. */
17961 ftype = get_die_type (die, cu);
17962 if (ftype)
17963 return ftype;
17964
17965 ftype = lookup_function_type (type);
17966
17967 if (prototyped_function_p (die, cu))
17968 ftype->set_is_prototyped (true);
17969
17970 /* Store the calling convention in the type if it's available in
17971 the subroutine die. Otherwise set the calling convention to
17972 the default value DW_CC_normal. */
17973 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17974 if (attr != nullptr
17975 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
17976 TYPE_CALLING_CONVENTION (ftype)
17977 = (enum dwarf_calling_convention) attr->constant_value (0);
17978 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17979 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17980 else
17981 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17982
17983 /* Record whether the function returns normally to its caller or not
17984 if the DWARF producer set that information. */
17985 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17986 if (attr && attr->as_boolean ())
17987 TYPE_NO_RETURN (ftype) = 1;
17988
17989 /* We need to add the subroutine type to the die immediately so
17990 we don't infinitely recurse when dealing with parameters
17991 declared as the same subroutine type. */
17992 set_die_type (die, ftype, cu);
17993
17994 if (die->child != NULL)
17995 {
17996 struct type *void_type = objfile_type (objfile)->builtin_void;
17997 struct die_info *child_die;
17998 int nparams, iparams;
17999
18000 /* Count the number of parameters.
18001 FIXME: GDB currently ignores vararg functions, but knows about
18002 vararg member functions. */
18003 nparams = 0;
18004 child_die = die->child;
18005 while (child_die && child_die->tag)
18006 {
18007 if (child_die->tag == DW_TAG_formal_parameter)
18008 nparams++;
18009 else if (child_die->tag == DW_TAG_unspecified_parameters)
18010 ftype->set_has_varargs (true);
18011
18012 child_die = child_die->sibling;
18013 }
18014
18015 /* Allocate storage for parameters and fill them in. */
18016 ftype->set_num_fields (nparams);
18017 ftype->set_fields
18018 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
18019
18020 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
18021 even if we error out during the parameters reading below. */
18022 for (iparams = 0; iparams < nparams; iparams++)
18023 ftype->field (iparams).set_type (void_type);
18024
18025 iparams = 0;
18026 child_die = die->child;
18027 while (child_die && child_die->tag)
18028 {
18029 if (child_die->tag == DW_TAG_formal_parameter)
18030 {
18031 struct type *arg_type;
18032
18033 /* DWARF version 2 has no clean way to discern C++
18034 static and non-static member functions. G++ helps
18035 GDB by marking the first parameter for non-static
18036 member functions (which is the this pointer) as
18037 artificial. We pass this information to
18038 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
18039
18040 DWARF version 3 added DW_AT_object_pointer, which GCC
18041 4.5 does not yet generate. */
18042 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
18043 if (attr != nullptr)
18044 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
18045 else
18046 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
18047 arg_type = die_type (child_die, cu);
18048
18049 /* RealView does not mark THIS as const, which the testsuite
18050 expects. GCC marks THIS as const in method definitions,
18051 but not in the class specifications (GCC PR 43053). */
18052 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
18053 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
18054 {
18055 int is_this = 0;
18056 struct dwarf2_cu *arg_cu = cu;
18057 const char *name = dwarf2_name (child_die, cu);
18058
18059 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
18060 if (attr != nullptr)
18061 {
18062 /* If the compiler emits this, use it. */
18063 if (follow_die_ref (die, attr, &arg_cu) == child_die)
18064 is_this = 1;
18065 }
18066 else if (name && strcmp (name, "this") == 0)
18067 /* Function definitions will have the argument names. */
18068 is_this = 1;
18069 else if (name == NULL && iparams == 0)
18070 /* Declarations may not have the names, so like
18071 elsewhere in GDB, assume an artificial first
18072 argument is "this". */
18073 is_this = 1;
18074
18075 if (is_this)
18076 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
18077 arg_type, 0);
18078 }
18079
18080 ftype->field (iparams).set_type (arg_type);
18081 iparams++;
18082 }
18083 child_die = child_die->sibling;
18084 }
18085 }
18086
18087 return ftype;
18088 }
18089
18090 static struct type *
18091 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
18092 {
18093 struct objfile *objfile = cu->per_objfile->objfile;
18094 const char *name = NULL;
18095 struct type *this_type, *target_type;
18096
18097 name = dwarf2_full_name (NULL, die, cu);
18098 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
18099 this_type->set_target_is_stub (true);
18100 set_die_type (die, this_type, cu);
18101 target_type = die_type (die, cu);
18102 if (target_type != this_type)
18103 TYPE_TARGET_TYPE (this_type) = target_type;
18104 else
18105 {
18106 /* Self-referential typedefs are, it seems, not allowed by the DWARF
18107 spec and cause infinite loops in GDB. */
18108 complaint (_("Self-referential DW_TAG_typedef "
18109 "- DIE at %s [in module %s]"),
18110 sect_offset_str (die->sect_off), objfile_name (objfile));
18111 TYPE_TARGET_TYPE (this_type) = NULL;
18112 }
18113 if (name == NULL)
18114 {
18115 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
18116 anonymous typedefs, which is, strictly speaking, invalid DWARF.
18117 Handle these by just returning the target type, rather than
18118 constructing an anonymous typedef type and trying to handle this
18119 elsewhere. */
18120 set_die_type (die, target_type, cu);
18121 return target_type;
18122 }
18123 return this_type;
18124 }
18125
18126 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18127 (which may be different from NAME) to the architecture back-end to allow
18128 it to guess the correct format if necessary. */
18129
18130 static struct type *
18131 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
18132 const char *name_hint, enum bfd_endian byte_order)
18133 {
18134 struct gdbarch *gdbarch = objfile->arch ();
18135 const struct floatformat **format;
18136 struct type *type;
18137
18138 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18139 if (format)
18140 type = init_float_type (objfile, bits, name, format, byte_order);
18141 else
18142 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18143
18144 return type;
18145 }
18146
18147 /* Allocate an integer type of size BITS and name NAME. */
18148
18149 static struct type *
18150 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18151 int bits, int unsigned_p, const char *name)
18152 {
18153 struct type *type;
18154
18155 /* Versions of Intel's C Compiler generate an integer type called "void"
18156 instead of using DW_TAG_unspecified_type. This has been seen on
18157 at least versions 14, 17, and 18. */
18158 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18159 && strcmp (name, "void") == 0)
18160 type = objfile_type (objfile)->builtin_void;
18161 else
18162 type = init_integer_type (objfile, bits, unsigned_p, name);
18163
18164 return type;
18165 }
18166
18167 /* Initialise and return a floating point type of size BITS suitable for
18168 use as a component of a complex number. The NAME_HINT is passed through
18169 when initialising the floating point type and is the name of the complex
18170 type.
18171
18172 As DWARF doesn't currently provide an explicit name for the components
18173 of a complex number, but it can be helpful to have these components
18174 named, we try to select a suitable name based on the size of the
18175 component. */
18176 static struct type *
18177 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18178 struct objfile *objfile,
18179 int bits, const char *name_hint,
18180 enum bfd_endian byte_order)
18181 {
18182 gdbarch *gdbarch = objfile->arch ();
18183 struct type *tt = nullptr;
18184
18185 /* Try to find a suitable floating point builtin type of size BITS.
18186 We're going to use the name of this type as the name for the complex
18187 target type that we are about to create. */
18188 switch (cu->language)
18189 {
18190 case language_fortran:
18191 switch (bits)
18192 {
18193 case 32:
18194 tt = builtin_f_type (gdbarch)->builtin_real;
18195 break;
18196 case 64:
18197 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18198 break;
18199 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18200 case 128:
18201 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18202 break;
18203 }
18204 break;
18205 default:
18206 switch (bits)
18207 {
18208 case 32:
18209 tt = builtin_type (gdbarch)->builtin_float;
18210 break;
18211 case 64:
18212 tt = builtin_type (gdbarch)->builtin_double;
18213 break;
18214 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18215 case 128:
18216 tt = builtin_type (gdbarch)->builtin_long_double;
18217 break;
18218 }
18219 break;
18220 }
18221
18222 /* If the type we found doesn't match the size we were looking for, then
18223 pretend we didn't find a type at all, the complex target type we
18224 create will then be nameless. */
18225 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18226 tt = nullptr;
18227
18228 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18229 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18230 }
18231
18232 /* Find a representation of a given base type and install
18233 it in the TYPE field of the die. */
18234
18235 static struct type *
18236 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18237 {
18238 struct objfile *objfile = cu->per_objfile->objfile;
18239 struct type *type;
18240 struct attribute *attr;
18241 int encoding = 0, bits = 0;
18242 const char *name;
18243 gdbarch *arch;
18244
18245 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18246 if (attr != nullptr && attr->form_is_constant ())
18247 encoding = attr->constant_value (0);
18248 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18249 if (attr != nullptr)
18250 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18251 name = dwarf2_name (die, cu);
18252 if (!name)
18253 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18254
18255 arch = objfile->arch ();
18256 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18257
18258 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18259 if (attr != nullptr && attr->form_is_constant ())
18260 {
18261 int endianity = attr->constant_value (0);
18262
18263 switch (endianity)
18264 {
18265 case DW_END_big:
18266 byte_order = BFD_ENDIAN_BIG;
18267 break;
18268 case DW_END_little:
18269 byte_order = BFD_ENDIAN_LITTLE;
18270 break;
18271 default:
18272 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18273 break;
18274 }
18275 }
18276
18277 switch (encoding)
18278 {
18279 case DW_ATE_address:
18280 /* Turn DW_ATE_address into a void * pointer. */
18281 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18282 type = init_pointer_type (objfile, bits, name, type);
18283 break;
18284 case DW_ATE_boolean:
18285 type = init_boolean_type (objfile, bits, 1, name);
18286 break;
18287 case DW_ATE_complex_float:
18288 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18289 byte_order);
18290 if (type->code () == TYPE_CODE_ERROR)
18291 {
18292 if (name == nullptr)
18293 {
18294 struct obstack *obstack
18295 = &cu->per_objfile->objfile->objfile_obstack;
18296 name = obconcat (obstack, "_Complex ", type->name (),
18297 nullptr);
18298 }
18299 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18300 }
18301 else
18302 type = init_complex_type (name, type);
18303 break;
18304 case DW_ATE_decimal_float:
18305 type = init_decfloat_type (objfile, bits, name);
18306 break;
18307 case DW_ATE_float:
18308 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18309 break;
18310 case DW_ATE_signed:
18311 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18312 break;
18313 case DW_ATE_unsigned:
18314 if (cu->language == language_fortran
18315 && name
18316 && startswith (name, "character("))
18317 type = init_character_type (objfile, bits, 1, name);
18318 else
18319 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18320 break;
18321 case DW_ATE_signed_char:
18322 if (cu->language == language_ada || cu->language == language_m2
18323 || cu->language == language_pascal
18324 || cu->language == language_fortran)
18325 type = init_character_type (objfile, bits, 0, name);
18326 else
18327 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18328 break;
18329 case DW_ATE_unsigned_char:
18330 if (cu->language == language_ada || cu->language == language_m2
18331 || cu->language == language_pascal
18332 || cu->language == language_fortran
18333 || cu->language == language_rust)
18334 type = init_character_type (objfile, bits, 1, name);
18335 else
18336 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18337 break;
18338 case DW_ATE_UTF:
18339 {
18340 if (bits == 16)
18341 type = builtin_type (arch)->builtin_char16;
18342 else if (bits == 32)
18343 type = builtin_type (arch)->builtin_char32;
18344 else
18345 {
18346 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18347 bits);
18348 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18349 }
18350 return set_die_type (die, type, cu);
18351 }
18352 break;
18353
18354 default:
18355 complaint (_("unsupported DW_AT_encoding: '%s'"),
18356 dwarf_type_encoding_name (encoding));
18357 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18358 break;
18359 }
18360
18361 if (name && strcmp (name, "char") == 0)
18362 type->set_has_no_signedness (true);
18363
18364 maybe_set_alignment (cu, die, type);
18365
18366 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18367
18368 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18369 {
18370 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18371 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18372 {
18373 unsigned real_bit_size = attr->as_unsigned ();
18374 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18375 /* Only use the attributes if they make sense together. */
18376 if (attr == nullptr
18377 || (attr->as_unsigned () + real_bit_size
18378 <= 8 * TYPE_LENGTH (type)))
18379 {
18380 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18381 = real_bit_size;
18382 if (attr != nullptr)
18383 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18384 = attr->as_unsigned ();
18385 }
18386 }
18387 }
18388
18389 return set_die_type (die, type, cu);
18390 }
18391
18392 /* Parse dwarf attribute if it's a block, reference or constant and put the
18393 resulting value of the attribute into struct bound_prop.
18394 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18395
18396 static int
18397 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18398 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18399 struct type *default_type)
18400 {
18401 struct dwarf2_property_baton *baton;
18402 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18403 struct objfile *objfile = per_objfile->objfile;
18404 struct obstack *obstack = &objfile->objfile_obstack;
18405
18406 gdb_assert (default_type != NULL);
18407
18408 if (attr == NULL || prop == NULL)
18409 return 0;
18410
18411 if (attr->form_is_block ())
18412 {
18413 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18414 baton->property_type = default_type;
18415 baton->locexpr.per_cu = cu->per_cu;
18416 baton->locexpr.per_objfile = per_objfile;
18417
18418 struct dwarf_block *block = attr->as_block ();
18419 baton->locexpr.size = block->size;
18420 baton->locexpr.data = block->data;
18421 switch (attr->name)
18422 {
18423 case DW_AT_string_length:
18424 baton->locexpr.is_reference = true;
18425 break;
18426 default:
18427 baton->locexpr.is_reference = false;
18428 break;
18429 }
18430
18431 prop->set_locexpr (baton);
18432 gdb_assert (prop->baton () != NULL);
18433 }
18434 else if (attr->form_is_ref ())
18435 {
18436 struct dwarf2_cu *target_cu = cu;
18437 struct die_info *target_die;
18438 struct attribute *target_attr;
18439
18440 target_die = follow_die_ref (die, attr, &target_cu);
18441 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18442 if (target_attr == NULL)
18443 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18444 target_cu);
18445 if (target_attr == NULL)
18446 return 0;
18447
18448 switch (target_attr->name)
18449 {
18450 case DW_AT_location:
18451 if (target_attr->form_is_section_offset ())
18452 {
18453 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18454 baton->property_type = die_type (target_die, target_cu);
18455 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18456 prop->set_loclist (baton);
18457 gdb_assert (prop->baton () != NULL);
18458 }
18459 else if (target_attr->form_is_block ())
18460 {
18461 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18462 baton->property_type = die_type (target_die, target_cu);
18463 baton->locexpr.per_cu = cu->per_cu;
18464 baton->locexpr.per_objfile = per_objfile;
18465 struct dwarf_block *block = target_attr->as_block ();
18466 baton->locexpr.size = block->size;
18467 baton->locexpr.data = block->data;
18468 baton->locexpr.is_reference = true;
18469 prop->set_locexpr (baton);
18470 gdb_assert (prop->baton () != NULL);
18471 }
18472 else
18473 {
18474 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18475 "dynamic property");
18476 return 0;
18477 }
18478 break;
18479 case DW_AT_data_member_location:
18480 {
18481 LONGEST offset;
18482
18483 if (!handle_data_member_location (target_die, target_cu,
18484 &offset))
18485 return 0;
18486
18487 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18488 baton->property_type = read_type_die (target_die->parent,
18489 target_cu);
18490 baton->offset_info.offset = offset;
18491 baton->offset_info.type = die_type (target_die, target_cu);
18492 prop->set_addr_offset (baton);
18493 break;
18494 }
18495 }
18496 }
18497 else if (attr->form_is_constant ())
18498 prop->set_const_val (attr->constant_value (0));
18499 else
18500 {
18501 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18502 dwarf2_name (die, cu));
18503 return 0;
18504 }
18505
18506 return 1;
18507 }
18508
18509 /* See read.h. */
18510
18511 struct type *
18512 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
18513 {
18514 struct type *int_type;
18515
18516 /* Helper macro to examine the various builtin types. */
18517 #define TRY_TYPE(F) \
18518 int_type = (unsigned_p \
18519 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18520 : objfile_type (objfile)->builtin_ ## F); \
18521 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18522 return int_type
18523
18524 TRY_TYPE (char);
18525 TRY_TYPE (short);
18526 TRY_TYPE (int);
18527 TRY_TYPE (long);
18528 TRY_TYPE (long_long);
18529
18530 #undef TRY_TYPE
18531
18532 gdb_assert_not_reached ("unable to find suitable integer type");
18533 }
18534
18535 /* See read.h. */
18536
18537 struct type *
18538 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
18539 {
18540 int addr_size = this->per_cu->addr_size ();
18541 return this->per_objfile->int_type (addr_size, unsigned_p);
18542 }
18543
18544 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18545 present (which is valid) then compute the default type based on the
18546 compilation units address size. */
18547
18548 static struct type *
18549 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18550 {
18551 struct type *index_type = die_type (die, cu);
18552
18553 /* Dwarf-2 specifications explicitly allows to create subrange types
18554 without specifying a base type.
18555 In that case, the base type must be set to the type of
18556 the lower bound, upper bound or count, in that order, if any of these
18557 three attributes references an object that has a type.
18558 If no base type is found, the Dwarf-2 specifications say that
18559 a signed integer type of size equal to the size of an address should
18560 be used.
18561 For the following C code: `extern char gdb_int [];'
18562 GCC produces an empty range DIE.
18563 FIXME: muller/2010-05-28: Possible references to object for low bound,
18564 high bound or count are not yet handled by this code. */
18565 if (index_type->code () == TYPE_CODE_VOID)
18566 index_type = cu->addr_sized_int_type (false);
18567
18568 return index_type;
18569 }
18570
18571 /* Read the given DW_AT_subrange DIE. */
18572
18573 static struct type *
18574 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18575 {
18576 struct type *base_type, *orig_base_type;
18577 struct type *range_type;
18578 struct attribute *attr;
18579 struct dynamic_prop low, high;
18580 int low_default_is_valid;
18581 int high_bound_is_count = 0;
18582 const char *name;
18583 ULONGEST negative_mask;
18584
18585 orig_base_type = read_subrange_index_type (die, cu);
18586
18587 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18588 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18589 creating the range type, but we use the result of check_typedef
18590 when examining properties of the type. */
18591 base_type = check_typedef (orig_base_type);
18592
18593 /* The die_type call above may have already set the type for this DIE. */
18594 range_type = get_die_type (die, cu);
18595 if (range_type)
18596 return range_type;
18597
18598 high.set_const_val (0);
18599
18600 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18601 omitting DW_AT_lower_bound. */
18602 switch (cu->language)
18603 {
18604 case language_c:
18605 case language_cplus:
18606 low.set_const_val (0);
18607 low_default_is_valid = 1;
18608 break;
18609 case language_fortran:
18610 low.set_const_val (1);
18611 low_default_is_valid = 1;
18612 break;
18613 case language_d:
18614 case language_objc:
18615 case language_rust:
18616 low.set_const_val (0);
18617 low_default_is_valid = (cu->header.version >= 4);
18618 break;
18619 case language_ada:
18620 case language_m2:
18621 case language_pascal:
18622 low.set_const_val (1);
18623 low_default_is_valid = (cu->header.version >= 4);
18624 break;
18625 default:
18626 low.set_const_val (0);
18627 low_default_is_valid = 0;
18628 break;
18629 }
18630
18631 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18632 if (attr != nullptr)
18633 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18634 else if (!low_default_is_valid)
18635 complaint (_("Missing DW_AT_lower_bound "
18636 "- DIE at %s [in module %s]"),
18637 sect_offset_str (die->sect_off),
18638 objfile_name (cu->per_objfile->objfile));
18639
18640 struct attribute *attr_ub, *attr_count;
18641 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18642 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18643 {
18644 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18645 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18646 {
18647 /* If bounds are constant do the final calculation here. */
18648 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18649 high.set_const_val (low.const_val () + high.const_val () - 1);
18650 else
18651 high_bound_is_count = 1;
18652 }
18653 else
18654 {
18655 if (attr_ub != NULL)
18656 complaint (_("Unresolved DW_AT_upper_bound "
18657 "- DIE at %s [in module %s]"),
18658 sect_offset_str (die->sect_off),
18659 objfile_name (cu->per_objfile->objfile));
18660 if (attr_count != NULL)
18661 complaint (_("Unresolved DW_AT_count "
18662 "- DIE at %s [in module %s]"),
18663 sect_offset_str (die->sect_off),
18664 objfile_name (cu->per_objfile->objfile));
18665 }
18666 }
18667
18668 LONGEST bias = 0;
18669 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18670 if (bias_attr != nullptr && bias_attr->form_is_constant ())
18671 bias = bias_attr->constant_value (0);
18672
18673 /* Normally, the DWARF producers are expected to use a signed
18674 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18675 But this is unfortunately not always the case, as witnessed
18676 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18677 is used instead. To work around that ambiguity, we treat
18678 the bounds as signed, and thus sign-extend their values, when
18679 the base type is signed. */
18680 negative_mask =
18681 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18682 if (low.kind () == PROP_CONST
18683 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
18684 low.set_const_val (low.const_val () | negative_mask);
18685 if (high.kind () == PROP_CONST
18686 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
18687 high.set_const_val (high.const_val () | negative_mask);
18688
18689 /* Check for bit and byte strides. */
18690 struct dynamic_prop byte_stride_prop;
18691 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18692 if (attr_byte_stride != nullptr)
18693 {
18694 struct type *prop_type = cu->addr_sized_int_type (false);
18695 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18696 prop_type);
18697 }
18698
18699 struct dynamic_prop bit_stride_prop;
18700 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18701 if (attr_bit_stride != nullptr)
18702 {
18703 /* It only makes sense to have either a bit or byte stride. */
18704 if (attr_byte_stride != nullptr)
18705 {
18706 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18707 "- DIE at %s [in module %s]"),
18708 sect_offset_str (die->sect_off),
18709 objfile_name (cu->per_objfile->objfile));
18710 attr_bit_stride = nullptr;
18711 }
18712 else
18713 {
18714 struct type *prop_type = cu->addr_sized_int_type (false);
18715 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18716 prop_type);
18717 }
18718 }
18719
18720 if (attr_byte_stride != nullptr
18721 || attr_bit_stride != nullptr)
18722 {
18723 bool byte_stride_p = (attr_byte_stride != nullptr);
18724 struct dynamic_prop *stride
18725 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18726
18727 range_type
18728 = create_range_type_with_stride (NULL, orig_base_type, &low,
18729 &high, bias, stride, byte_stride_p);
18730 }
18731 else
18732 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18733
18734 if (high_bound_is_count)
18735 range_type->bounds ()->flag_upper_bound_is_count = 1;
18736
18737 /* Ada expects an empty array on no boundary attributes. */
18738 if (attr == NULL && cu->language != language_ada)
18739 range_type->bounds ()->high.set_undefined ();
18740
18741 name = dwarf2_name (die, cu);
18742 if (name)
18743 range_type->set_name (name);
18744
18745 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18746 if (attr != nullptr)
18747 TYPE_LENGTH (range_type) = attr->constant_value (0);
18748
18749 maybe_set_alignment (cu, die, range_type);
18750
18751 set_die_type (die, range_type, cu);
18752
18753 /* set_die_type should be already done. */
18754 set_descriptive_type (range_type, die, cu);
18755
18756 return range_type;
18757 }
18758
18759 static struct type *
18760 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18761 {
18762 struct type *type;
18763
18764 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
18765 type->set_name (dwarf2_name (die, cu));
18766
18767 /* In Ada, an unspecified type is typically used when the description
18768 of the type is deferred to a different unit. When encountering
18769 such a type, we treat it as a stub, and try to resolve it later on,
18770 when needed. */
18771 if (cu->language == language_ada)
18772 type->set_is_stub (true);
18773
18774 return set_die_type (die, type, cu);
18775 }
18776
18777 /* Read a single die and all its descendents. Set the die's sibling
18778 field to NULL; set other fields in the die correctly, and set all
18779 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18780 location of the info_ptr after reading all of those dies. PARENT
18781 is the parent of the die in question. */
18782
18783 static struct die_info *
18784 read_die_and_children (const struct die_reader_specs *reader,
18785 const gdb_byte *info_ptr,
18786 const gdb_byte **new_info_ptr,
18787 struct die_info *parent)
18788 {
18789 struct die_info *die;
18790 const gdb_byte *cur_ptr;
18791
18792 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18793 if (die == NULL)
18794 {
18795 *new_info_ptr = cur_ptr;
18796 return NULL;
18797 }
18798 store_in_ref_table (die, reader->cu);
18799
18800 if (die->has_children)
18801 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18802 else
18803 {
18804 die->child = NULL;
18805 *new_info_ptr = cur_ptr;
18806 }
18807
18808 die->sibling = NULL;
18809 die->parent = parent;
18810 return die;
18811 }
18812
18813 /* Read a die, all of its descendents, and all of its siblings; set
18814 all of the fields of all of the dies correctly. Arguments are as
18815 in read_die_and_children. */
18816
18817 static struct die_info *
18818 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18819 const gdb_byte *info_ptr,
18820 const gdb_byte **new_info_ptr,
18821 struct die_info *parent)
18822 {
18823 struct die_info *first_die, *last_sibling;
18824 const gdb_byte *cur_ptr;
18825
18826 cur_ptr = info_ptr;
18827 first_die = last_sibling = NULL;
18828
18829 while (1)
18830 {
18831 struct die_info *die
18832 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18833
18834 if (die == NULL)
18835 {
18836 *new_info_ptr = cur_ptr;
18837 return first_die;
18838 }
18839
18840 if (!first_die)
18841 first_die = die;
18842 else
18843 last_sibling->sibling = die;
18844
18845 last_sibling = die;
18846 }
18847 }
18848
18849 /* Read a die, all of its descendents, and all of its siblings; set
18850 all of the fields of all of the dies correctly. Arguments are as
18851 in read_die_and_children.
18852 This the main entry point for reading a DIE and all its children. */
18853
18854 static struct die_info *
18855 read_die_and_siblings (const struct die_reader_specs *reader,
18856 const gdb_byte *info_ptr,
18857 const gdb_byte **new_info_ptr,
18858 struct die_info *parent)
18859 {
18860 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18861 new_info_ptr, parent);
18862
18863 if (dwarf_die_debug)
18864 {
18865 fprintf_unfiltered (gdb_stdlog,
18866 "Read die from %s@0x%x of %s:\n",
18867 reader->die_section->get_name (),
18868 (unsigned) (info_ptr - reader->die_section->buffer),
18869 bfd_get_filename (reader->abfd));
18870 dump_die (die, dwarf_die_debug);
18871 }
18872
18873 return die;
18874 }
18875
18876 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18877 attributes.
18878 The caller is responsible for filling in the extra attributes
18879 and updating (*DIEP)->num_attrs.
18880 Set DIEP to point to a newly allocated die with its information,
18881 except for its child, sibling, and parent fields. */
18882
18883 static const gdb_byte *
18884 read_full_die_1 (const struct die_reader_specs *reader,
18885 struct die_info **diep, const gdb_byte *info_ptr,
18886 int num_extra_attrs)
18887 {
18888 unsigned int abbrev_number, bytes_read, i;
18889 struct abbrev_info *abbrev;
18890 struct die_info *die;
18891 struct dwarf2_cu *cu = reader->cu;
18892 bfd *abfd = reader->abfd;
18893
18894 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18895 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18896 info_ptr += bytes_read;
18897 if (!abbrev_number)
18898 {
18899 *diep = NULL;
18900 return info_ptr;
18901 }
18902
18903 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18904 if (!abbrev)
18905 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18906 abbrev_number,
18907 bfd_get_filename (abfd));
18908
18909 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18910 die->sect_off = sect_off;
18911 die->tag = abbrev->tag;
18912 die->abbrev = abbrev_number;
18913 die->has_children = abbrev->has_children;
18914
18915 /* Make the result usable.
18916 The caller needs to update num_attrs after adding the extra
18917 attributes. */
18918 die->num_attrs = abbrev->num_attrs;
18919
18920 bool any_need_reprocess = false;
18921 for (i = 0; i < abbrev->num_attrs; ++i)
18922 {
18923 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18924 info_ptr);
18925 if (die->attrs[i].requires_reprocessing_p ())
18926 any_need_reprocess = true;
18927 }
18928
18929 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18930 if (attr != nullptr && attr->form_is_unsigned ())
18931 cu->str_offsets_base = attr->as_unsigned ();
18932
18933 attr = die->attr (DW_AT_loclists_base);
18934 if (attr != nullptr)
18935 cu->loclist_base = attr->as_unsigned ();
18936
18937 auto maybe_addr_base = die->addr_base ();
18938 if (maybe_addr_base.has_value ())
18939 cu->addr_base = *maybe_addr_base;
18940
18941 attr = die->attr (DW_AT_rnglists_base);
18942 if (attr != nullptr)
18943 cu->ranges_base = attr->as_unsigned ();
18944
18945 if (any_need_reprocess)
18946 {
18947 for (i = 0; i < abbrev->num_attrs; ++i)
18948 {
18949 if (die->attrs[i].requires_reprocessing_p ())
18950 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
18951 }
18952 }
18953 *diep = die;
18954 return info_ptr;
18955 }
18956
18957 /* Read a die and all its attributes.
18958 Set DIEP to point to a newly allocated die with its information,
18959 except for its child, sibling, and parent fields. */
18960
18961 static const gdb_byte *
18962 read_full_die (const struct die_reader_specs *reader,
18963 struct die_info **diep, const gdb_byte *info_ptr)
18964 {
18965 const gdb_byte *result;
18966
18967 result = read_full_die_1 (reader, diep, info_ptr, 0);
18968
18969 if (dwarf_die_debug)
18970 {
18971 fprintf_unfiltered (gdb_stdlog,
18972 "Read die from %s@0x%x of %s:\n",
18973 reader->die_section->get_name (),
18974 (unsigned) (info_ptr - reader->die_section->buffer),
18975 bfd_get_filename (reader->abfd));
18976 dump_die (*diep, dwarf_die_debug);
18977 }
18978
18979 return result;
18980 }
18981 \f
18982
18983 /* Returns nonzero if TAG represents a type that we might generate a partial
18984 symbol for. */
18985
18986 static int
18987 is_type_tag_for_partial (int tag)
18988 {
18989 switch (tag)
18990 {
18991 #if 0
18992 /* Some types that would be reasonable to generate partial symbols for,
18993 that we don't at present. */
18994 case DW_TAG_array_type:
18995 case DW_TAG_file_type:
18996 case DW_TAG_ptr_to_member_type:
18997 case DW_TAG_set_type:
18998 case DW_TAG_string_type:
18999 case DW_TAG_subroutine_type:
19000 #endif
19001 case DW_TAG_base_type:
19002 case DW_TAG_class_type:
19003 case DW_TAG_interface_type:
19004 case DW_TAG_enumeration_type:
19005 case DW_TAG_structure_type:
19006 case DW_TAG_subrange_type:
19007 case DW_TAG_typedef:
19008 case DW_TAG_union_type:
19009 return 1;
19010 default:
19011 return 0;
19012 }
19013 }
19014
19015 /* Load all DIEs that are interesting for partial symbols into memory. */
19016
19017 static struct partial_die_info *
19018 load_partial_dies (const struct die_reader_specs *reader,
19019 const gdb_byte *info_ptr, int building_psymtab)
19020 {
19021 struct dwarf2_cu *cu = reader->cu;
19022 struct objfile *objfile = cu->per_objfile->objfile;
19023 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
19024 unsigned int bytes_read;
19025 unsigned int load_all = 0;
19026 int nesting_level = 1;
19027
19028 parent_die = NULL;
19029 last_die = NULL;
19030
19031 gdb_assert (cu->per_cu != NULL);
19032 if (cu->per_cu->load_all_dies)
19033 load_all = 1;
19034
19035 cu->partial_dies
19036 = htab_create_alloc_ex (cu->header.length / 12,
19037 partial_die_hash,
19038 partial_die_eq,
19039 NULL,
19040 &cu->comp_unit_obstack,
19041 hashtab_obstack_allocate,
19042 dummy_obstack_deallocate);
19043
19044 while (1)
19045 {
19046 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
19047
19048 /* A NULL abbrev means the end of a series of children. */
19049 if (abbrev == NULL)
19050 {
19051 if (--nesting_level == 0)
19052 return first_die;
19053
19054 info_ptr += bytes_read;
19055 last_die = parent_die;
19056 parent_die = parent_die->die_parent;
19057 continue;
19058 }
19059
19060 /* Check for template arguments. We never save these; if
19061 they're seen, we just mark the parent, and go on our way. */
19062 if (parent_die != NULL
19063 && cu->language == language_cplus
19064 && (abbrev->tag == DW_TAG_template_type_param
19065 || abbrev->tag == DW_TAG_template_value_param))
19066 {
19067 parent_die->has_template_arguments = 1;
19068
19069 if (!load_all)
19070 {
19071 /* We don't need a partial DIE for the template argument. */
19072 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19073 continue;
19074 }
19075 }
19076
19077 /* We only recurse into c++ subprograms looking for template arguments.
19078 Skip their other children. */
19079 if (!load_all
19080 && cu->language == language_cplus
19081 && parent_die != NULL
19082 && parent_die->tag == DW_TAG_subprogram
19083 && abbrev->tag != DW_TAG_inlined_subroutine)
19084 {
19085 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19086 continue;
19087 }
19088
19089 /* Check whether this DIE is interesting enough to save. Normally
19090 we would not be interested in members here, but there may be
19091 later variables referencing them via DW_AT_specification (for
19092 static members). */
19093 if (!load_all
19094 && !is_type_tag_for_partial (abbrev->tag)
19095 && abbrev->tag != DW_TAG_constant
19096 && abbrev->tag != DW_TAG_enumerator
19097 && abbrev->tag != DW_TAG_subprogram
19098 && abbrev->tag != DW_TAG_inlined_subroutine
19099 && abbrev->tag != DW_TAG_lexical_block
19100 && abbrev->tag != DW_TAG_variable
19101 && abbrev->tag != DW_TAG_namespace
19102 && abbrev->tag != DW_TAG_module
19103 && abbrev->tag != DW_TAG_member
19104 && abbrev->tag != DW_TAG_imported_unit
19105 && abbrev->tag != DW_TAG_imported_declaration)
19106 {
19107 /* Otherwise we skip to the next sibling, if any. */
19108 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19109 continue;
19110 }
19111
19112 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19113 abbrev);
19114
19115 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19116
19117 /* This two-pass algorithm for processing partial symbols has a
19118 high cost in cache pressure. Thus, handle some simple cases
19119 here which cover the majority of C partial symbols. DIEs
19120 which neither have specification tags in them, nor could have
19121 specification tags elsewhere pointing at them, can simply be
19122 processed and discarded.
19123
19124 This segment is also optional; scan_partial_symbols and
19125 add_partial_symbol will handle these DIEs if we chain
19126 them in normally. When compilers which do not emit large
19127 quantities of duplicate debug information are more common,
19128 this code can probably be removed. */
19129
19130 /* Any complete simple types at the top level (pretty much all
19131 of them, for a language without namespaces), can be processed
19132 directly. */
19133 if (parent_die == NULL
19134 && pdi.has_specification == 0
19135 && pdi.is_declaration == 0
19136 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19137 || pdi.tag == DW_TAG_base_type
19138 || pdi.tag == DW_TAG_subrange_type))
19139 {
19140 if (building_psymtab && pdi.raw_name != NULL)
19141 add_partial_symbol (&pdi, cu);
19142
19143 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19144 continue;
19145 }
19146
19147 /* The exception for DW_TAG_typedef with has_children above is
19148 a workaround of GCC PR debug/47510. In the case of this complaint
19149 type_name_or_error will error on such types later.
19150
19151 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19152 it could not find the child DIEs referenced later, this is checked
19153 above. In correct DWARF DW_TAG_typedef should have no children. */
19154
19155 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19156 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19157 "- DIE at %s [in module %s]"),
19158 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19159
19160 /* If we're at the second level, and we're an enumerator, and
19161 our parent has no specification (meaning possibly lives in a
19162 namespace elsewhere), then we can add the partial symbol now
19163 instead of queueing it. */
19164 if (pdi.tag == DW_TAG_enumerator
19165 && parent_die != NULL
19166 && parent_die->die_parent == NULL
19167 && parent_die->tag == DW_TAG_enumeration_type
19168 && parent_die->has_specification == 0)
19169 {
19170 if (pdi.raw_name == NULL)
19171 complaint (_("malformed enumerator DIE ignored"));
19172 else if (building_psymtab)
19173 add_partial_symbol (&pdi, cu);
19174
19175 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19176 continue;
19177 }
19178
19179 struct partial_die_info *part_die
19180 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19181
19182 /* We'll save this DIE so link it in. */
19183 part_die->die_parent = parent_die;
19184 part_die->die_sibling = NULL;
19185 part_die->die_child = NULL;
19186
19187 if (last_die && last_die == parent_die)
19188 last_die->die_child = part_die;
19189 else if (last_die)
19190 last_die->die_sibling = part_die;
19191
19192 last_die = part_die;
19193
19194 if (first_die == NULL)
19195 first_die = part_die;
19196
19197 /* Maybe add the DIE to the hash table. Not all DIEs that we
19198 find interesting need to be in the hash table, because we
19199 also have the parent/sibling/child chains; only those that we
19200 might refer to by offset later during partial symbol reading.
19201
19202 For now this means things that might have be the target of a
19203 DW_AT_specification, DW_AT_abstract_origin, or
19204 DW_AT_extension. DW_AT_extension will refer only to
19205 namespaces; DW_AT_abstract_origin refers to functions (and
19206 many things under the function DIE, but we do not recurse
19207 into function DIEs during partial symbol reading) and
19208 possibly variables as well; DW_AT_specification refers to
19209 declarations. Declarations ought to have the DW_AT_declaration
19210 flag. It happens that GCC forgets to put it in sometimes, but
19211 only for functions, not for types.
19212
19213 Adding more things than necessary to the hash table is harmless
19214 except for the performance cost. Adding too few will result in
19215 wasted time in find_partial_die, when we reread the compilation
19216 unit with load_all_dies set. */
19217
19218 if (load_all
19219 || abbrev->tag == DW_TAG_constant
19220 || abbrev->tag == DW_TAG_subprogram
19221 || abbrev->tag == DW_TAG_variable
19222 || abbrev->tag == DW_TAG_namespace
19223 || part_die->is_declaration)
19224 {
19225 void **slot;
19226
19227 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19228 to_underlying (part_die->sect_off),
19229 INSERT);
19230 *slot = part_die;
19231 }
19232
19233 /* For some DIEs we want to follow their children (if any). For C
19234 we have no reason to follow the children of structures; for other
19235 languages we have to, so that we can get at method physnames
19236 to infer fully qualified class names, for DW_AT_specification,
19237 and for C++ template arguments. For C++, we also look one level
19238 inside functions to find template arguments (if the name of the
19239 function does not already contain the template arguments).
19240
19241 For Ada and Fortran, we need to scan the children of subprograms
19242 and lexical blocks as well because these languages allow the
19243 definition of nested entities that could be interesting for the
19244 debugger, such as nested subprograms for instance. */
19245 if (last_die->has_children
19246 && (load_all
19247 || last_die->tag == DW_TAG_namespace
19248 || last_die->tag == DW_TAG_module
19249 || last_die->tag == DW_TAG_enumeration_type
19250 || (cu->language == language_cplus
19251 && last_die->tag == DW_TAG_subprogram
19252 && (last_die->raw_name == NULL
19253 || strchr (last_die->raw_name, '<') == NULL))
19254 || (cu->language != language_c
19255 && (last_die->tag == DW_TAG_class_type
19256 || last_die->tag == DW_TAG_interface_type
19257 || last_die->tag == DW_TAG_structure_type
19258 || last_die->tag == DW_TAG_union_type))
19259 || ((cu->language == language_ada
19260 || cu->language == language_fortran)
19261 && (last_die->tag == DW_TAG_subprogram
19262 || last_die->tag == DW_TAG_lexical_block))))
19263 {
19264 nesting_level++;
19265 parent_die = last_die;
19266 continue;
19267 }
19268
19269 /* Otherwise we skip to the next sibling, if any. */
19270 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19271
19272 /* Back to the top, do it again. */
19273 }
19274 }
19275
19276 partial_die_info::partial_die_info (sect_offset sect_off_,
19277 struct abbrev_info *abbrev)
19278 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19279 {
19280 }
19281
19282 /* See class definition. */
19283
19284 const char *
19285 partial_die_info::name (dwarf2_cu *cu)
19286 {
19287 if (!canonical_name && raw_name != nullptr)
19288 {
19289 struct objfile *objfile = cu->per_objfile->objfile;
19290 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19291 canonical_name = 1;
19292 }
19293
19294 return raw_name;
19295 }
19296
19297 /* Read a minimal amount of information into the minimal die structure.
19298 INFO_PTR should point just after the initial uleb128 of a DIE. */
19299
19300 const gdb_byte *
19301 partial_die_info::read (const struct die_reader_specs *reader,
19302 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19303 {
19304 struct dwarf2_cu *cu = reader->cu;
19305 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19306 unsigned int i;
19307 int has_low_pc_attr = 0;
19308 int has_high_pc_attr = 0;
19309 int high_pc_relative = 0;
19310
19311 for (i = 0; i < abbrev.num_attrs; ++i)
19312 {
19313 attribute attr;
19314 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19315 /* String and address offsets that need to do the reprocessing have
19316 already been read at this point, so there is no need to wait until
19317 the loop terminates to do the reprocessing. */
19318 if (attr.requires_reprocessing_p ())
19319 read_attribute_reprocess (reader, &attr, tag);
19320 /* Store the data if it is of an attribute we want to keep in a
19321 partial symbol table. */
19322 switch (attr.name)
19323 {
19324 case DW_AT_name:
19325 switch (tag)
19326 {
19327 case DW_TAG_compile_unit:
19328 case DW_TAG_partial_unit:
19329 case DW_TAG_type_unit:
19330 /* Compilation units have a DW_AT_name that is a filename, not
19331 a source language identifier. */
19332 case DW_TAG_enumeration_type:
19333 case DW_TAG_enumerator:
19334 /* These tags always have simple identifiers already; no need
19335 to canonicalize them. */
19336 canonical_name = 1;
19337 raw_name = attr.as_string ();
19338 break;
19339 default:
19340 canonical_name = 0;
19341 raw_name = attr.as_string ();
19342 break;
19343 }
19344 break;
19345 case DW_AT_linkage_name:
19346 case DW_AT_MIPS_linkage_name:
19347 /* Note that both forms of linkage name might appear. We
19348 assume they will be the same, and we only store the last
19349 one we see. */
19350 linkage_name = attr.as_string ();
19351 break;
19352 case DW_AT_low_pc:
19353 has_low_pc_attr = 1;
19354 lowpc = attr.as_address ();
19355 break;
19356 case DW_AT_high_pc:
19357 has_high_pc_attr = 1;
19358 highpc = attr.as_address ();
19359 if (cu->header.version >= 4 && attr.form_is_constant ())
19360 high_pc_relative = 1;
19361 break;
19362 case DW_AT_location:
19363 /* Support the .debug_loc offsets. */
19364 if (attr.form_is_block ())
19365 {
19366 d.locdesc = attr.as_block ();
19367 }
19368 else if (attr.form_is_section_offset ())
19369 {
19370 dwarf2_complex_location_expr_complaint ();
19371 }
19372 else
19373 {
19374 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19375 "partial symbol information");
19376 }
19377 break;
19378 case DW_AT_external:
19379 is_external = attr.as_boolean ();
19380 break;
19381 case DW_AT_declaration:
19382 is_declaration = attr.as_boolean ();
19383 break;
19384 case DW_AT_type:
19385 has_type = 1;
19386 break;
19387 case DW_AT_abstract_origin:
19388 case DW_AT_specification:
19389 case DW_AT_extension:
19390 has_specification = 1;
19391 spec_offset = attr.get_ref_die_offset ();
19392 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19393 || cu->per_cu->is_dwz);
19394 break;
19395 case DW_AT_sibling:
19396 /* Ignore absolute siblings, they might point outside of
19397 the current compile unit. */
19398 if (attr.form == DW_FORM_ref_addr)
19399 complaint (_("ignoring absolute DW_AT_sibling"));
19400 else
19401 {
19402 const gdb_byte *buffer = reader->buffer;
19403 sect_offset off = attr.get_ref_die_offset ();
19404 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19405
19406 if (sibling_ptr < info_ptr)
19407 complaint (_("DW_AT_sibling points backwards"));
19408 else if (sibling_ptr > reader->buffer_end)
19409 reader->die_section->overflow_complaint ();
19410 else
19411 sibling = sibling_ptr;
19412 }
19413 break;
19414 case DW_AT_byte_size:
19415 has_byte_size = 1;
19416 break;
19417 case DW_AT_const_value:
19418 has_const_value = 1;
19419 break;
19420 case DW_AT_calling_convention:
19421 /* DWARF doesn't provide a way to identify a program's source-level
19422 entry point. DW_AT_calling_convention attributes are only meant
19423 to describe functions' calling conventions.
19424
19425 However, because it's a necessary piece of information in
19426 Fortran, and before DWARF 4 DW_CC_program was the only
19427 piece of debugging information whose definition refers to
19428 a 'main program' at all, several compilers marked Fortran
19429 main programs with DW_CC_program --- even when those
19430 functions use the standard calling conventions.
19431
19432 Although DWARF now specifies a way to provide this
19433 information, we support this practice for backward
19434 compatibility. */
19435 if (attr.constant_value (0) == DW_CC_program
19436 && cu->language == language_fortran)
19437 main_subprogram = 1;
19438 break;
19439 case DW_AT_inline:
19440 {
19441 LONGEST value = attr.constant_value (-1);
19442 if (value == DW_INL_inlined
19443 || value == DW_INL_declared_inlined)
19444 may_be_inlined = 1;
19445 }
19446 break;
19447
19448 case DW_AT_import:
19449 if (tag == DW_TAG_imported_unit)
19450 {
19451 d.sect_off = attr.get_ref_die_offset ();
19452 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19453 || cu->per_cu->is_dwz);
19454 }
19455 break;
19456
19457 case DW_AT_main_subprogram:
19458 main_subprogram = attr.as_boolean ();
19459 break;
19460
19461 case DW_AT_ranges:
19462 {
19463 /* DW_AT_rnglists_base does not apply to DIEs from the DWO
19464 skeleton. We take advantage of the fact the DW_AT_ranges
19465 does not appear in DW_TAG_compile_unit of DWO files.
19466
19467 Attributes of the form DW_FORM_rnglistx have already had
19468 their value changed by read_rnglist_index and already
19469 include DW_AT_rnglists_base, so don't need to add the ranges
19470 base, either. */
19471 int need_ranges_base = (tag != DW_TAG_compile_unit
19472 && attr.form != DW_FORM_rnglistx);
19473 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19474 but that requires a full DIE, so instead we just
19475 reimplement it. */
19476 unsigned int ranges_offset = (attr.constant_value (0)
19477 + (need_ranges_base
19478 ? cu->ranges_base
19479 : 0));
19480
19481 /* Value of the DW_AT_ranges attribute is the offset in the
19482 .debug_ranges section. */
19483 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19484 nullptr, tag))
19485 has_pc_info = 1;
19486 }
19487 break;
19488
19489 default:
19490 break;
19491 }
19492 }
19493
19494 /* For Ada, if both the name and the linkage name appear, we prefer
19495 the latter. This lets "catch exception" work better, regardless
19496 of the order in which the name and linkage name were emitted.
19497 Really, though, this is just a workaround for the fact that gdb
19498 doesn't store both the name and the linkage name. */
19499 if (cu->language == language_ada && linkage_name != nullptr)
19500 raw_name = linkage_name;
19501
19502 if (high_pc_relative)
19503 highpc += lowpc;
19504
19505 if (has_low_pc_attr && has_high_pc_attr)
19506 {
19507 /* When using the GNU linker, .gnu.linkonce. sections are used to
19508 eliminate duplicate copies of functions and vtables and such.
19509 The linker will arbitrarily choose one and discard the others.
19510 The AT_*_pc values for such functions refer to local labels in
19511 these sections. If the section from that file was discarded, the
19512 labels are not in the output, so the relocs get a value of 0.
19513 If this is a discarded function, mark the pc bounds as invalid,
19514 so that GDB will ignore it. */
19515 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19516 {
19517 struct objfile *objfile = per_objfile->objfile;
19518 struct gdbarch *gdbarch = objfile->arch ();
19519
19520 complaint (_("DW_AT_low_pc %s is zero "
19521 "for DIE at %s [in module %s]"),
19522 paddress (gdbarch, lowpc),
19523 sect_offset_str (sect_off),
19524 objfile_name (objfile));
19525 }
19526 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19527 else if (lowpc >= highpc)
19528 {
19529 struct objfile *objfile = per_objfile->objfile;
19530 struct gdbarch *gdbarch = objfile->arch ();
19531
19532 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19533 "for DIE at %s [in module %s]"),
19534 paddress (gdbarch, lowpc),
19535 paddress (gdbarch, highpc),
19536 sect_offset_str (sect_off),
19537 objfile_name (objfile));
19538 }
19539 else
19540 has_pc_info = 1;
19541 }
19542
19543 return info_ptr;
19544 }
19545
19546 /* Find a cached partial DIE at OFFSET in CU. */
19547
19548 struct partial_die_info *
19549 dwarf2_cu::find_partial_die (sect_offset sect_off)
19550 {
19551 struct partial_die_info *lookup_die = NULL;
19552 struct partial_die_info part_die (sect_off);
19553
19554 lookup_die = ((struct partial_die_info *)
19555 htab_find_with_hash (partial_dies, &part_die,
19556 to_underlying (sect_off)));
19557
19558 return lookup_die;
19559 }
19560
19561 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19562 except in the case of .debug_types DIEs which do not reference
19563 outside their CU (they do however referencing other types via
19564 DW_FORM_ref_sig8). */
19565
19566 static const struct cu_partial_die_info
19567 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19568 {
19569 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19570 struct objfile *objfile = per_objfile->objfile;
19571 struct partial_die_info *pd = NULL;
19572
19573 if (offset_in_dwz == cu->per_cu->is_dwz
19574 && cu->header.offset_in_cu_p (sect_off))
19575 {
19576 pd = cu->find_partial_die (sect_off);
19577 if (pd != NULL)
19578 return { cu, pd };
19579 /* We missed recording what we needed.
19580 Load all dies and try again. */
19581 }
19582 else
19583 {
19584 /* TUs don't reference other CUs/TUs (except via type signatures). */
19585 if (cu->per_cu->is_debug_types)
19586 {
19587 error (_("Dwarf Error: Type Unit at offset %s contains"
19588 " external reference to offset %s [in module %s].\n"),
19589 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19590 bfd_get_filename (objfile->obfd));
19591 }
19592 dwarf2_per_cu_data *per_cu
19593 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19594 per_objfile);
19595
19596 cu = per_objfile->get_cu (per_cu);
19597 if (cu == NULL || cu->partial_dies == NULL)
19598 load_partial_comp_unit (per_cu, per_objfile, nullptr);
19599
19600 cu = per_objfile->get_cu (per_cu);
19601
19602 cu->last_used = 0;
19603 pd = cu->find_partial_die (sect_off);
19604 }
19605
19606 /* If we didn't find it, and not all dies have been loaded,
19607 load them all and try again. */
19608
19609 if (pd == NULL && cu->per_cu->load_all_dies == 0)
19610 {
19611 cu->per_cu->load_all_dies = 1;
19612
19613 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19614 THIS_CU->cu may already be in use. So we can't just free it and
19615 replace its DIEs with the ones we read in. Instead, we leave those
19616 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19617 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19618 set. */
19619 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
19620
19621 pd = cu->find_partial_die (sect_off);
19622 }
19623
19624 if (pd == NULL)
19625 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
19626 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19627 return { cu, pd };
19628 }
19629
19630 /* See if we can figure out if the class lives in a namespace. We do
19631 this by looking for a member function; its demangled name will
19632 contain namespace info, if there is any. */
19633
19634 static void
19635 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19636 struct dwarf2_cu *cu)
19637 {
19638 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19639 what template types look like, because the demangler
19640 frequently doesn't give the same name as the debug info. We
19641 could fix this by only using the demangled name to get the
19642 prefix (but see comment in read_structure_type). */
19643
19644 struct partial_die_info *real_pdi;
19645 struct partial_die_info *child_pdi;
19646
19647 /* If this DIE (this DIE's specification, if any) has a parent, then
19648 we should not do this. We'll prepend the parent's fully qualified
19649 name when we create the partial symbol. */
19650
19651 real_pdi = struct_pdi;
19652 while (real_pdi->has_specification)
19653 {
19654 auto res = find_partial_die (real_pdi->spec_offset,
19655 real_pdi->spec_is_dwz, cu);
19656 real_pdi = res.pdi;
19657 cu = res.cu;
19658 }
19659
19660 if (real_pdi->die_parent != NULL)
19661 return;
19662
19663 for (child_pdi = struct_pdi->die_child;
19664 child_pdi != NULL;
19665 child_pdi = child_pdi->die_sibling)
19666 {
19667 if (child_pdi->tag == DW_TAG_subprogram
19668 && child_pdi->linkage_name != NULL)
19669 {
19670 gdb::unique_xmalloc_ptr<char> actual_class_name
19671 (cu->language_defn->class_name_from_physname
19672 (child_pdi->linkage_name));
19673 if (actual_class_name != NULL)
19674 {
19675 struct objfile *objfile = cu->per_objfile->objfile;
19676 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19677 struct_pdi->canonical_name = 1;
19678 }
19679 break;
19680 }
19681 }
19682 }
19683
19684 /* Return true if a DIE with TAG may have the DW_AT_const_value
19685 attribute. */
19686
19687 static bool
19688 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19689 {
19690 switch (tag)
19691 {
19692 case DW_TAG_constant:
19693 case DW_TAG_enumerator:
19694 case DW_TAG_formal_parameter:
19695 case DW_TAG_template_value_param:
19696 case DW_TAG_variable:
19697 return true;
19698 }
19699
19700 return false;
19701 }
19702
19703 void
19704 partial_die_info::fixup (struct dwarf2_cu *cu)
19705 {
19706 /* Once we've fixed up a die, there's no point in doing so again.
19707 This also avoids a memory leak if we were to call
19708 guess_partial_die_structure_name multiple times. */
19709 if (fixup_called)
19710 return;
19711
19712 /* If we found a reference attribute and the DIE has no name, try
19713 to find a name in the referred to DIE. */
19714
19715 if (raw_name == NULL && has_specification)
19716 {
19717 struct partial_die_info *spec_die;
19718
19719 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19720 spec_die = res.pdi;
19721 cu = res.cu;
19722
19723 spec_die->fixup (cu);
19724
19725 if (spec_die->raw_name)
19726 {
19727 raw_name = spec_die->raw_name;
19728 canonical_name = spec_die->canonical_name;
19729
19730 /* Copy DW_AT_external attribute if it is set. */
19731 if (spec_die->is_external)
19732 is_external = spec_die->is_external;
19733 }
19734 }
19735
19736 if (!has_const_value && has_specification
19737 && can_have_DW_AT_const_value_p (tag))
19738 {
19739 struct partial_die_info *spec_die;
19740
19741 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19742 spec_die = res.pdi;
19743 cu = res.cu;
19744
19745 spec_die->fixup (cu);
19746
19747 if (spec_die->has_const_value)
19748 {
19749 /* Copy DW_AT_const_value attribute if it is set. */
19750 has_const_value = spec_die->has_const_value;
19751 }
19752 }
19753
19754 /* Set default names for some unnamed DIEs. */
19755
19756 if (raw_name == NULL && tag == DW_TAG_namespace)
19757 {
19758 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19759 canonical_name = 1;
19760 }
19761
19762 /* If there is no parent die to provide a namespace, and there are
19763 children, see if we can determine the namespace from their linkage
19764 name. */
19765 if (cu->language == language_cplus
19766 && !cu->per_objfile->per_bfd->types.empty ()
19767 && die_parent == NULL
19768 && has_children
19769 && (tag == DW_TAG_class_type
19770 || tag == DW_TAG_structure_type
19771 || tag == DW_TAG_union_type))
19772 guess_partial_die_structure_name (this, cu);
19773
19774 /* GCC might emit a nameless struct or union that has a linkage
19775 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19776 if (raw_name == NULL
19777 && (tag == DW_TAG_class_type
19778 || tag == DW_TAG_interface_type
19779 || tag == DW_TAG_structure_type
19780 || tag == DW_TAG_union_type)
19781 && linkage_name != NULL)
19782 {
19783 gdb::unique_xmalloc_ptr<char> demangled
19784 (gdb_demangle (linkage_name, DMGL_TYPES));
19785 if (demangled != nullptr)
19786 {
19787 const char *base;
19788
19789 /* Strip any leading namespaces/classes, keep only the base name.
19790 DW_AT_name for named DIEs does not contain the prefixes. */
19791 base = strrchr (demangled.get (), ':');
19792 if (base && base > demangled.get () && base[-1] == ':')
19793 base++;
19794 else
19795 base = demangled.get ();
19796
19797 struct objfile *objfile = cu->per_objfile->objfile;
19798 raw_name = objfile->intern (base);
19799 canonical_name = 1;
19800 }
19801 }
19802
19803 fixup_called = 1;
19804 }
19805
19806 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
19807 contents from the given SECTION in the HEADER. */
19808 static void
19809 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
19810 struct dwarf2_section_info *section)
19811 {
19812 unsigned int bytes_read;
19813 bfd *abfd = section->get_bfd_owner ();
19814 const gdb_byte *info_ptr = section->buffer;
19815 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19816 info_ptr += bytes_read;
19817 header->version = read_2_bytes (abfd, info_ptr);
19818 info_ptr += 2;
19819 header->addr_size = read_1_byte (abfd, info_ptr);
19820 info_ptr += 1;
19821 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19822 info_ptr += 1;
19823 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19824 }
19825
19826 /* Return the DW_AT_loclists_base value for the CU. */
19827 static ULONGEST
19828 lookup_loclist_base (struct dwarf2_cu *cu)
19829 {
19830 /* For the .dwo unit, the loclist_base points to the first offset following
19831 the header. The header consists of the following entities-
19832 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19833 bit format)
19834 2. version (2 bytes)
19835 3. address size (1 byte)
19836 4. segment selector size (1 byte)
19837 5. offset entry count (4 bytes)
19838 These sizes are derived as per the DWARFv5 standard. */
19839 if (cu->dwo_unit != nullptr)
19840 {
19841 if (cu->header.initial_length_size == 4)
19842 return LOCLIST_HEADER_SIZE32;
19843 return LOCLIST_HEADER_SIZE64;
19844 }
19845 return cu->loclist_base;
19846 }
19847
19848 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19849 array of offsets in the .debug_loclists section. */
19850 static CORE_ADDR
19851 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19852 {
19853 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19854 struct objfile *objfile = per_objfile->objfile;
19855 bfd *abfd = objfile->obfd;
19856 ULONGEST loclist_base = lookup_loclist_base (cu);
19857 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19858
19859 section->read (objfile);
19860 if (section->buffer == NULL)
19861 complaint (_("DW_FORM_loclistx used without .debug_loclists "
19862 "section [in module %s]"), objfile_name (objfile));
19863 struct loclists_rnglists_header header;
19864 read_loclists_rnglists_header (&header, section);
19865 if (loclist_index >= header.offset_entry_count)
19866 complaint (_("DW_FORM_loclistx pointing outside of "
19867 ".debug_loclists offset array [in module %s]"),
19868 objfile_name (objfile));
19869 if (loclist_base + loclist_index * cu->header.offset_size
19870 >= section->size)
19871 complaint (_("DW_FORM_loclistx pointing outside of "
19872 ".debug_loclists section [in module %s]"),
19873 objfile_name (objfile));
19874 const gdb_byte *info_ptr
19875 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
19876
19877 if (cu->header.offset_size == 4)
19878 return bfd_get_32 (abfd, info_ptr) + loclist_base;
19879 else
19880 return bfd_get_64 (abfd, info_ptr) + loclist_base;
19881 }
19882
19883 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19884 array of offsets in the .debug_rnglists section. */
19885 static CORE_ADDR
19886 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19887 dwarf_tag tag)
19888 {
19889 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19890 struct objfile *objfile = dwarf2_per_objfile->objfile;
19891 bfd *abfd = objfile->obfd;
19892 ULONGEST rnglist_header_size =
19893 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19894 : RNGLIST_HEADER_SIZE64);
19895 ULONGEST rnglist_base =
19896 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->ranges_base;
19897 ULONGEST start_offset =
19898 rnglist_base + rnglist_index * cu->header.offset_size;
19899
19900 /* Get rnglists section. */
19901 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19902
19903 /* Read the rnglists section content. */
19904 section->read (objfile);
19905 if (section->buffer == nullptr)
19906 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19907 "[in module %s]"),
19908 objfile_name (objfile));
19909
19910 /* Verify the rnglist index is valid. */
19911 struct loclists_rnglists_header header;
19912 read_loclists_rnglists_header (&header, section);
19913 if (rnglist_index >= header.offset_entry_count)
19914 error (_("DW_FORM_rnglistx index pointing outside of "
19915 ".debug_rnglists offset array [in module %s]"),
19916 objfile_name (objfile));
19917
19918 /* Validate that the offset is within the section's range. */
19919 if (start_offset >= section->size)
19920 error (_("DW_FORM_rnglistx pointing outside of "
19921 ".debug_rnglists section [in module %s]"),
19922 objfile_name (objfile));
19923
19924 /* Validate that reading won't go beyond the end of the section. */
19925 if (start_offset + cu->header.offset_size > rnglist_base + section->size)
19926 error (_("Reading DW_FORM_rnglistx index beyond end of"
19927 ".debug_rnglists section [in module %s]"),
19928 objfile_name (objfile));
19929
19930 const gdb_byte *info_ptr = section->buffer + start_offset;
19931
19932 if (cu->header.offset_size == 4)
19933 return read_4_bytes (abfd, info_ptr) + rnglist_base;
19934 else
19935 return read_8_bytes (abfd, info_ptr) + rnglist_base;
19936 }
19937
19938 /* Process the attributes that had to be skipped in the first round. These
19939 attributes are the ones that need str_offsets_base or addr_base attributes.
19940 They could not have been processed in the first round, because at the time
19941 the values of str_offsets_base or addr_base may not have been known. */
19942 static void
19943 read_attribute_reprocess (const struct die_reader_specs *reader,
19944 struct attribute *attr, dwarf_tag tag)
19945 {
19946 struct dwarf2_cu *cu = reader->cu;
19947 switch (attr->form)
19948 {
19949 case DW_FORM_addrx:
19950 case DW_FORM_GNU_addr_index:
19951 attr->set_address (read_addr_index (cu,
19952 attr->as_unsigned_reprocess ()));
19953 break;
19954 case DW_FORM_loclistx:
19955 attr->set_address (read_loclist_index (cu, attr->as_unsigned ()));
19956 break;
19957 case DW_FORM_rnglistx:
19958 attr->set_address (read_rnglist_index (cu, attr->as_unsigned (), tag));
19959 break;
19960 case DW_FORM_strx:
19961 case DW_FORM_strx1:
19962 case DW_FORM_strx2:
19963 case DW_FORM_strx3:
19964 case DW_FORM_strx4:
19965 case DW_FORM_GNU_str_index:
19966 {
19967 unsigned int str_index = attr->as_unsigned_reprocess ();
19968 gdb_assert (!attr->canonical_string_p ());
19969 if (reader->dwo_file != NULL)
19970 attr->set_string_noncanonical (read_dwo_str_index (reader,
19971 str_index));
19972 else
19973 attr->set_string_noncanonical (read_stub_str_index (cu,
19974 str_index));
19975 break;
19976 }
19977 default:
19978 gdb_assert_not_reached (_("Unexpected DWARF form."));
19979 }
19980 }
19981
19982 /* Read an attribute value described by an attribute form. */
19983
19984 static const gdb_byte *
19985 read_attribute_value (const struct die_reader_specs *reader,
19986 struct attribute *attr, unsigned form,
19987 LONGEST implicit_const, const gdb_byte *info_ptr)
19988 {
19989 struct dwarf2_cu *cu = reader->cu;
19990 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19991 struct objfile *objfile = per_objfile->objfile;
19992 bfd *abfd = reader->abfd;
19993 struct comp_unit_head *cu_header = &cu->header;
19994 unsigned int bytes_read;
19995 struct dwarf_block *blk;
19996
19997 attr->form = (enum dwarf_form) form;
19998 switch (form)
19999 {
20000 case DW_FORM_ref_addr:
20001 if (cu->header.version == 2)
20002 attr->set_unsigned (cu->header.read_address (abfd, info_ptr,
20003 &bytes_read));
20004 else
20005 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20006 &bytes_read));
20007 info_ptr += bytes_read;
20008 break;
20009 case DW_FORM_GNU_ref_alt:
20010 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20011 &bytes_read));
20012 info_ptr += bytes_read;
20013 break;
20014 case DW_FORM_addr:
20015 {
20016 struct gdbarch *gdbarch = objfile->arch ();
20017 CORE_ADDR addr = cu->header.read_address (abfd, info_ptr, &bytes_read);
20018 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20019 attr->set_address (addr);
20020 info_ptr += bytes_read;
20021 }
20022 break;
20023 case DW_FORM_block2:
20024 blk = dwarf_alloc_block (cu);
20025 blk->size = read_2_bytes (abfd, info_ptr);
20026 info_ptr += 2;
20027 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20028 info_ptr += blk->size;
20029 attr->set_block (blk);
20030 break;
20031 case DW_FORM_block4:
20032 blk = dwarf_alloc_block (cu);
20033 blk->size = read_4_bytes (abfd, info_ptr);
20034 info_ptr += 4;
20035 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20036 info_ptr += blk->size;
20037 attr->set_block (blk);
20038 break;
20039 case DW_FORM_data2:
20040 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20041 info_ptr += 2;
20042 break;
20043 case DW_FORM_data4:
20044 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20045 info_ptr += 4;
20046 break;
20047 case DW_FORM_data8:
20048 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20049 info_ptr += 8;
20050 break;
20051 case DW_FORM_data16:
20052 blk = dwarf_alloc_block (cu);
20053 blk->size = 16;
20054 blk->data = read_n_bytes (abfd, info_ptr, 16);
20055 info_ptr += 16;
20056 attr->set_block (blk);
20057 break;
20058 case DW_FORM_sec_offset:
20059 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20060 &bytes_read));
20061 info_ptr += bytes_read;
20062 break;
20063 case DW_FORM_loclistx:
20064 {
20065 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20066 &bytes_read));
20067 info_ptr += bytes_read;
20068 }
20069 break;
20070 case DW_FORM_string:
20071 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20072 &bytes_read));
20073 info_ptr += bytes_read;
20074 break;
20075 case DW_FORM_strp:
20076 if (!cu->per_cu->is_dwz)
20077 {
20078 attr->set_string_noncanonical
20079 (read_indirect_string (per_objfile,
20080 abfd, info_ptr, cu_header,
20081 &bytes_read));
20082 info_ptr += bytes_read;
20083 break;
20084 }
20085 /* FALLTHROUGH */
20086 case DW_FORM_line_strp:
20087 if (!cu->per_cu->is_dwz)
20088 {
20089 attr->set_string_noncanonical
20090 (per_objfile->read_line_string (info_ptr, cu_header,
20091 &bytes_read));
20092 info_ptr += bytes_read;
20093 break;
20094 }
20095 /* FALLTHROUGH */
20096 case DW_FORM_GNU_strp_alt:
20097 {
20098 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
20099 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20100 &bytes_read);
20101
20102 attr->set_string_noncanonical
20103 (dwz->read_string (objfile, str_offset));
20104 info_ptr += bytes_read;
20105 }
20106 break;
20107 case DW_FORM_exprloc:
20108 case DW_FORM_block:
20109 blk = dwarf_alloc_block (cu);
20110 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20111 info_ptr += bytes_read;
20112 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20113 info_ptr += blk->size;
20114 attr->set_block (blk);
20115 break;
20116 case DW_FORM_block1:
20117 blk = dwarf_alloc_block (cu);
20118 blk->size = read_1_byte (abfd, info_ptr);
20119 info_ptr += 1;
20120 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20121 info_ptr += blk->size;
20122 attr->set_block (blk);
20123 break;
20124 case DW_FORM_data1:
20125 case DW_FORM_flag:
20126 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20127 info_ptr += 1;
20128 break;
20129 case DW_FORM_flag_present:
20130 attr->set_unsigned (1);
20131 break;
20132 case DW_FORM_sdata:
20133 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20134 info_ptr += bytes_read;
20135 break;
20136 case DW_FORM_rnglistx:
20137 {
20138 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20139 &bytes_read));
20140 info_ptr += bytes_read;
20141 }
20142 break;
20143 case DW_FORM_udata:
20144 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20145 info_ptr += bytes_read;
20146 break;
20147 case DW_FORM_ref1:
20148 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20149 + read_1_byte (abfd, info_ptr)));
20150 info_ptr += 1;
20151 break;
20152 case DW_FORM_ref2:
20153 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20154 + read_2_bytes (abfd, info_ptr)));
20155 info_ptr += 2;
20156 break;
20157 case DW_FORM_ref4:
20158 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20159 + read_4_bytes (abfd, info_ptr)));
20160 info_ptr += 4;
20161 break;
20162 case DW_FORM_ref8:
20163 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20164 + read_8_bytes (abfd, info_ptr)));
20165 info_ptr += 8;
20166 break;
20167 case DW_FORM_ref_sig8:
20168 attr->set_signature (read_8_bytes (abfd, info_ptr));
20169 info_ptr += 8;
20170 break;
20171 case DW_FORM_ref_udata:
20172 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20173 + read_unsigned_leb128 (abfd, info_ptr,
20174 &bytes_read)));
20175 info_ptr += bytes_read;
20176 break;
20177 case DW_FORM_indirect:
20178 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20179 info_ptr += bytes_read;
20180 if (form == DW_FORM_implicit_const)
20181 {
20182 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20183 info_ptr += bytes_read;
20184 }
20185 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20186 info_ptr);
20187 break;
20188 case DW_FORM_implicit_const:
20189 attr->set_signed (implicit_const);
20190 break;
20191 case DW_FORM_addrx:
20192 case DW_FORM_GNU_addr_index:
20193 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20194 &bytes_read));
20195 info_ptr += bytes_read;
20196 break;
20197 case DW_FORM_strx:
20198 case DW_FORM_strx1:
20199 case DW_FORM_strx2:
20200 case DW_FORM_strx3:
20201 case DW_FORM_strx4:
20202 case DW_FORM_GNU_str_index:
20203 {
20204 ULONGEST str_index;
20205 if (form == DW_FORM_strx1)
20206 {
20207 str_index = read_1_byte (abfd, info_ptr);
20208 info_ptr += 1;
20209 }
20210 else if (form == DW_FORM_strx2)
20211 {
20212 str_index = read_2_bytes (abfd, info_ptr);
20213 info_ptr += 2;
20214 }
20215 else if (form == DW_FORM_strx3)
20216 {
20217 str_index = read_3_bytes (abfd, info_ptr);
20218 info_ptr += 3;
20219 }
20220 else if (form == DW_FORM_strx4)
20221 {
20222 str_index = read_4_bytes (abfd, info_ptr);
20223 info_ptr += 4;
20224 }
20225 else
20226 {
20227 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20228 info_ptr += bytes_read;
20229 }
20230 attr->set_unsigned_reprocess (str_index);
20231 }
20232 break;
20233 default:
20234 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20235 dwarf_form_name (form),
20236 bfd_get_filename (abfd));
20237 }
20238
20239 /* Super hack. */
20240 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20241 attr->form = DW_FORM_GNU_ref_alt;
20242
20243 /* We have seen instances where the compiler tried to emit a byte
20244 size attribute of -1 which ended up being encoded as an unsigned
20245 0xffffffff. Although 0xffffffff is technically a valid size value,
20246 an object of this size seems pretty unlikely so we can relatively
20247 safely treat these cases as if the size attribute was invalid and
20248 treat them as zero by default. */
20249 if (attr->name == DW_AT_byte_size
20250 && form == DW_FORM_data4
20251 && attr->as_unsigned () >= 0xffffffff)
20252 {
20253 complaint
20254 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20255 hex_string (attr->as_unsigned ()));
20256 attr->set_unsigned (0);
20257 }
20258
20259 return info_ptr;
20260 }
20261
20262 /* Read an attribute described by an abbreviated attribute. */
20263
20264 static const gdb_byte *
20265 read_attribute (const struct die_reader_specs *reader,
20266 struct attribute *attr, struct attr_abbrev *abbrev,
20267 const gdb_byte *info_ptr)
20268 {
20269 attr->name = abbrev->name;
20270 attr->string_is_canonical = 0;
20271 attr->requires_reprocessing = 0;
20272 return read_attribute_value (reader, attr, abbrev->form,
20273 abbrev->implicit_const, info_ptr);
20274 }
20275
20276 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20277
20278 static const char *
20279 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20280 LONGEST str_offset)
20281 {
20282 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20283 str_offset, "DW_FORM_strp");
20284 }
20285
20286 /* Return pointer to string at .debug_str offset as read from BUF.
20287 BUF is assumed to be in a compilation unit described by CU_HEADER.
20288 Return *BYTES_READ_PTR count of bytes read from BUF. */
20289
20290 static const char *
20291 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20292 const gdb_byte *buf,
20293 const struct comp_unit_head *cu_header,
20294 unsigned int *bytes_read_ptr)
20295 {
20296 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20297
20298 return read_indirect_string_at_offset (per_objfile, str_offset);
20299 }
20300
20301 /* See read.h. */
20302
20303 const char *
20304 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20305 const struct comp_unit_head *cu_header,
20306 unsigned int *bytes_read_ptr)
20307 {
20308 bfd *abfd = objfile->obfd;
20309 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20310
20311 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20312 }
20313
20314 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20315 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20316 ADDR_SIZE is the size of addresses from the CU header. */
20317
20318 static CORE_ADDR
20319 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20320 gdb::optional<ULONGEST> addr_base, int addr_size)
20321 {
20322 struct objfile *objfile = per_objfile->objfile;
20323 bfd *abfd = objfile->obfd;
20324 const gdb_byte *info_ptr;
20325 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20326
20327 per_objfile->per_bfd->addr.read (objfile);
20328 if (per_objfile->per_bfd->addr.buffer == NULL)
20329 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20330 objfile_name (objfile));
20331 if (addr_base_or_zero + addr_index * addr_size
20332 >= per_objfile->per_bfd->addr.size)
20333 error (_("DW_FORM_addr_index pointing outside of "
20334 ".debug_addr section [in module %s]"),
20335 objfile_name (objfile));
20336 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20337 + addr_index * addr_size);
20338 if (addr_size == 4)
20339 return bfd_get_32 (abfd, info_ptr);
20340 else
20341 return bfd_get_64 (abfd, info_ptr);
20342 }
20343
20344 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20345
20346 static CORE_ADDR
20347 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20348 {
20349 return read_addr_index_1 (cu->per_objfile, addr_index,
20350 cu->addr_base, cu->header.addr_size);
20351 }
20352
20353 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20354
20355 static CORE_ADDR
20356 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20357 unsigned int *bytes_read)
20358 {
20359 bfd *abfd = cu->per_objfile->objfile->obfd;
20360 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20361
20362 return read_addr_index (cu, addr_index);
20363 }
20364
20365 /* See read.h. */
20366
20367 CORE_ADDR
20368 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20369 dwarf2_per_objfile *per_objfile,
20370 unsigned int addr_index)
20371 {
20372 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20373 gdb::optional<ULONGEST> addr_base;
20374 int addr_size;
20375
20376 /* We need addr_base and addr_size.
20377 If we don't have PER_CU->cu, we have to get it.
20378 Nasty, but the alternative is storing the needed info in PER_CU,
20379 which at this point doesn't seem justified: it's not clear how frequently
20380 it would get used and it would increase the size of every PER_CU.
20381 Entry points like dwarf2_per_cu_addr_size do a similar thing
20382 so we're not in uncharted territory here.
20383 Alas we need to be a bit more complicated as addr_base is contained
20384 in the DIE.
20385
20386 We don't need to read the entire CU(/TU).
20387 We just need the header and top level die.
20388
20389 IWBN to use the aging mechanism to let us lazily later discard the CU.
20390 For now we skip this optimization. */
20391
20392 if (cu != NULL)
20393 {
20394 addr_base = cu->addr_base;
20395 addr_size = cu->header.addr_size;
20396 }
20397 else
20398 {
20399 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20400 addr_base = reader.cu->addr_base;
20401 addr_size = reader.cu->header.addr_size;
20402 }
20403
20404 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20405 }
20406
20407 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20408 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20409 DWO file. */
20410
20411 static const char *
20412 read_str_index (struct dwarf2_cu *cu,
20413 struct dwarf2_section_info *str_section,
20414 struct dwarf2_section_info *str_offsets_section,
20415 ULONGEST str_offsets_base, ULONGEST str_index)
20416 {
20417 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20418 struct objfile *objfile = per_objfile->objfile;
20419 const char *objf_name = objfile_name (objfile);
20420 bfd *abfd = objfile->obfd;
20421 const gdb_byte *info_ptr;
20422 ULONGEST str_offset;
20423 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20424
20425 str_section->read (objfile);
20426 str_offsets_section->read (objfile);
20427 if (str_section->buffer == NULL)
20428 error (_("%s used without %s section"
20429 " in CU at offset %s [in module %s]"),
20430 form_name, str_section->get_name (),
20431 sect_offset_str (cu->header.sect_off), objf_name);
20432 if (str_offsets_section->buffer == NULL)
20433 error (_("%s used without %s section"
20434 " in CU at offset %s [in module %s]"),
20435 form_name, str_section->get_name (),
20436 sect_offset_str (cu->header.sect_off), objf_name);
20437 info_ptr = (str_offsets_section->buffer
20438 + str_offsets_base
20439 + str_index * cu->header.offset_size);
20440 if (cu->header.offset_size == 4)
20441 str_offset = bfd_get_32 (abfd, info_ptr);
20442 else
20443 str_offset = bfd_get_64 (abfd, info_ptr);
20444 if (str_offset >= str_section->size)
20445 error (_("Offset from %s pointing outside of"
20446 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20447 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20448 return (const char *) (str_section->buffer + str_offset);
20449 }
20450
20451 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20452
20453 static const char *
20454 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20455 {
20456 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20457 ? reader->cu->header.addr_size : 0;
20458 return read_str_index (reader->cu,
20459 &reader->dwo_file->sections.str,
20460 &reader->dwo_file->sections.str_offsets,
20461 str_offsets_base, str_index);
20462 }
20463
20464 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20465
20466 static const char *
20467 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20468 {
20469 struct objfile *objfile = cu->per_objfile->objfile;
20470 const char *objf_name = objfile_name (objfile);
20471 static const char form_name[] = "DW_FORM_GNU_str_index";
20472 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20473
20474 if (!cu->str_offsets_base.has_value ())
20475 error (_("%s used in Fission stub without %s"
20476 " in CU at offset 0x%lx [in module %s]"),
20477 form_name, str_offsets_attr_name,
20478 (long) cu->header.offset_size, objf_name);
20479
20480 return read_str_index (cu,
20481 &cu->per_objfile->per_bfd->str,
20482 &cu->per_objfile->per_bfd->str_offsets,
20483 *cu->str_offsets_base, str_index);
20484 }
20485
20486 /* Return the length of an LEB128 number in BUF. */
20487
20488 static int
20489 leb128_size (const gdb_byte *buf)
20490 {
20491 const gdb_byte *begin = buf;
20492 gdb_byte byte;
20493
20494 while (1)
20495 {
20496 byte = *buf++;
20497 if ((byte & 128) == 0)
20498 return buf - begin;
20499 }
20500 }
20501
20502 static void
20503 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20504 {
20505 switch (lang)
20506 {
20507 case DW_LANG_C89:
20508 case DW_LANG_C99:
20509 case DW_LANG_C11:
20510 case DW_LANG_C:
20511 case DW_LANG_UPC:
20512 cu->language = language_c;
20513 break;
20514 case DW_LANG_Java:
20515 case DW_LANG_C_plus_plus:
20516 case DW_LANG_C_plus_plus_11:
20517 case DW_LANG_C_plus_plus_14:
20518 cu->language = language_cplus;
20519 break;
20520 case DW_LANG_D:
20521 cu->language = language_d;
20522 break;
20523 case DW_LANG_Fortran77:
20524 case DW_LANG_Fortran90:
20525 case DW_LANG_Fortran95:
20526 case DW_LANG_Fortran03:
20527 case DW_LANG_Fortran08:
20528 cu->language = language_fortran;
20529 break;
20530 case DW_LANG_Go:
20531 cu->language = language_go;
20532 break;
20533 case DW_LANG_Mips_Assembler:
20534 cu->language = language_asm;
20535 break;
20536 case DW_LANG_Ada83:
20537 case DW_LANG_Ada95:
20538 cu->language = language_ada;
20539 break;
20540 case DW_LANG_Modula2:
20541 cu->language = language_m2;
20542 break;
20543 case DW_LANG_Pascal83:
20544 cu->language = language_pascal;
20545 break;
20546 case DW_LANG_ObjC:
20547 cu->language = language_objc;
20548 break;
20549 case DW_LANG_Rust:
20550 case DW_LANG_Rust_old:
20551 cu->language = language_rust;
20552 break;
20553 case DW_LANG_Cobol74:
20554 case DW_LANG_Cobol85:
20555 default:
20556 cu->language = language_minimal;
20557 break;
20558 }
20559 cu->language_defn = language_def (cu->language);
20560 }
20561
20562 /* Return the named attribute or NULL if not there. */
20563
20564 static struct attribute *
20565 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20566 {
20567 for (;;)
20568 {
20569 unsigned int i;
20570 struct attribute *spec = NULL;
20571
20572 for (i = 0; i < die->num_attrs; ++i)
20573 {
20574 if (die->attrs[i].name == name)
20575 return &die->attrs[i];
20576 if (die->attrs[i].name == DW_AT_specification
20577 || die->attrs[i].name == DW_AT_abstract_origin)
20578 spec = &die->attrs[i];
20579 }
20580
20581 if (!spec)
20582 break;
20583
20584 die = follow_die_ref (die, spec, &cu);
20585 }
20586
20587 return NULL;
20588 }
20589
20590 /* Return the string associated with a string-typed attribute, or NULL if it
20591 is either not found or is of an incorrect type. */
20592
20593 static const char *
20594 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20595 {
20596 struct attribute *attr;
20597 const char *str = NULL;
20598
20599 attr = dwarf2_attr (die, name, cu);
20600
20601 if (attr != NULL)
20602 {
20603 str = attr->as_string ();
20604 if (str == nullptr)
20605 complaint (_("string type expected for attribute %s for "
20606 "DIE at %s in module %s"),
20607 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20608 objfile_name (cu->per_objfile->objfile));
20609 }
20610
20611 return str;
20612 }
20613
20614 /* Return the dwo name or NULL if not present. If present, it is in either
20615 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20616 static const char *
20617 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20618 {
20619 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20620 if (dwo_name == nullptr)
20621 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20622 return dwo_name;
20623 }
20624
20625 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20626 and holds a non-zero value. This function should only be used for
20627 DW_FORM_flag or DW_FORM_flag_present attributes. */
20628
20629 static int
20630 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20631 {
20632 struct attribute *attr = dwarf2_attr (die, name, cu);
20633
20634 return attr != nullptr && attr->as_boolean ();
20635 }
20636
20637 static int
20638 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20639 {
20640 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20641 which value is non-zero. However, we have to be careful with
20642 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20643 (via dwarf2_flag_true_p) follows this attribute. So we may
20644 end up accidently finding a declaration attribute that belongs
20645 to a different DIE referenced by the specification attribute,
20646 even though the given DIE does not have a declaration attribute. */
20647 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20648 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20649 }
20650
20651 /* Return the die giving the specification for DIE, if there is
20652 one. *SPEC_CU is the CU containing DIE on input, and the CU
20653 containing the return value on output. If there is no
20654 specification, but there is an abstract origin, that is
20655 returned. */
20656
20657 static struct die_info *
20658 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20659 {
20660 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20661 *spec_cu);
20662
20663 if (spec_attr == NULL)
20664 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20665
20666 if (spec_attr == NULL)
20667 return NULL;
20668 else
20669 return follow_die_ref (die, spec_attr, spec_cu);
20670 }
20671
20672 /* Stub for free_line_header to match void * callback types. */
20673
20674 static void
20675 free_line_header_voidp (void *arg)
20676 {
20677 struct line_header *lh = (struct line_header *) arg;
20678
20679 delete lh;
20680 }
20681
20682 /* A convenience function to find the proper .debug_line section for a CU. */
20683
20684 static struct dwarf2_section_info *
20685 get_debug_line_section (struct dwarf2_cu *cu)
20686 {
20687 struct dwarf2_section_info *section;
20688 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20689
20690 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20691 DWO file. */
20692 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20693 section = &cu->dwo_unit->dwo_file->sections.line;
20694 else if (cu->per_cu->is_dwz)
20695 {
20696 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
20697
20698 section = &dwz->line;
20699 }
20700 else
20701 section = &per_objfile->per_bfd->line;
20702
20703 return section;
20704 }
20705
20706 /* Read the statement program header starting at OFFSET in
20707 .debug_line, or .debug_line.dwo. Return a pointer
20708 to a struct line_header, allocated using xmalloc.
20709 Returns NULL if there is a problem reading the header, e.g., if it
20710 has a version we don't understand.
20711
20712 NOTE: the strings in the include directory and file name tables of
20713 the returned object point into the dwarf line section buffer,
20714 and must not be freed. */
20715
20716 static line_header_up
20717 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20718 {
20719 struct dwarf2_section_info *section;
20720 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20721
20722 section = get_debug_line_section (cu);
20723 section->read (per_objfile->objfile);
20724 if (section->buffer == NULL)
20725 {
20726 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20727 complaint (_("missing .debug_line.dwo section"));
20728 else
20729 complaint (_("missing .debug_line section"));
20730 return 0;
20731 }
20732
20733 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
20734 per_objfile, section, &cu->header);
20735 }
20736
20737 /* Subroutine of dwarf_decode_lines to simplify it.
20738 Return the file name of the psymtab for the given file_entry.
20739 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20740 If space for the result is malloc'd, *NAME_HOLDER will be set.
20741 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20742
20743 static const char *
20744 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20745 const dwarf2_psymtab *pst,
20746 const char *comp_dir,
20747 gdb::unique_xmalloc_ptr<char> *name_holder)
20748 {
20749 const char *include_name = fe.name;
20750 const char *include_name_to_compare = include_name;
20751 const char *pst_filename;
20752 int file_is_pst;
20753
20754 const char *dir_name = fe.include_dir (lh);
20755
20756 gdb::unique_xmalloc_ptr<char> hold_compare;
20757 if (!IS_ABSOLUTE_PATH (include_name)
20758 && (dir_name != NULL || comp_dir != NULL))
20759 {
20760 /* Avoid creating a duplicate psymtab for PST.
20761 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20762 Before we do the comparison, however, we need to account
20763 for DIR_NAME and COMP_DIR.
20764 First prepend dir_name (if non-NULL). If we still don't
20765 have an absolute path prepend comp_dir (if non-NULL).
20766 However, the directory we record in the include-file's
20767 psymtab does not contain COMP_DIR (to match the
20768 corresponding symtab(s)).
20769
20770 Example:
20771
20772 bash$ cd /tmp
20773 bash$ gcc -g ./hello.c
20774 include_name = "hello.c"
20775 dir_name = "."
20776 DW_AT_comp_dir = comp_dir = "/tmp"
20777 DW_AT_name = "./hello.c"
20778
20779 */
20780
20781 if (dir_name != NULL)
20782 {
20783 name_holder->reset (concat (dir_name, SLASH_STRING,
20784 include_name, (char *) NULL));
20785 include_name = name_holder->get ();
20786 include_name_to_compare = include_name;
20787 }
20788 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20789 {
20790 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20791 include_name, (char *) NULL));
20792 include_name_to_compare = hold_compare.get ();
20793 }
20794 }
20795
20796 pst_filename = pst->filename;
20797 gdb::unique_xmalloc_ptr<char> copied_name;
20798 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20799 {
20800 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20801 pst_filename, (char *) NULL));
20802 pst_filename = copied_name.get ();
20803 }
20804
20805 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20806
20807 if (file_is_pst)
20808 return NULL;
20809 return include_name;
20810 }
20811
20812 /* State machine to track the state of the line number program. */
20813
20814 class lnp_state_machine
20815 {
20816 public:
20817 /* Initialize a machine state for the start of a line number
20818 program. */
20819 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20820 bool record_lines_p);
20821
20822 file_entry *current_file ()
20823 {
20824 /* lh->file_names is 0-based, but the file name numbers in the
20825 statement program are 1-based. */
20826 return m_line_header->file_name_at (m_file);
20827 }
20828
20829 /* Record the line in the state machine. END_SEQUENCE is true if
20830 we're processing the end of a sequence. */
20831 void record_line (bool end_sequence);
20832
20833 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
20834 nop-out rest of the lines in this sequence. */
20835 void check_line_address (struct dwarf2_cu *cu,
20836 const gdb_byte *line_ptr,
20837 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20838
20839 void handle_set_discriminator (unsigned int discriminator)
20840 {
20841 m_discriminator = discriminator;
20842 m_line_has_non_zero_discriminator |= discriminator != 0;
20843 }
20844
20845 /* Handle DW_LNE_set_address. */
20846 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20847 {
20848 m_op_index = 0;
20849 address += baseaddr;
20850 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20851 }
20852
20853 /* Handle DW_LNS_advance_pc. */
20854 void handle_advance_pc (CORE_ADDR adjust);
20855
20856 /* Handle a special opcode. */
20857 void handle_special_opcode (unsigned char op_code);
20858
20859 /* Handle DW_LNS_advance_line. */
20860 void handle_advance_line (int line_delta)
20861 {
20862 advance_line (line_delta);
20863 }
20864
20865 /* Handle DW_LNS_set_file. */
20866 void handle_set_file (file_name_index file);
20867
20868 /* Handle DW_LNS_negate_stmt. */
20869 void handle_negate_stmt ()
20870 {
20871 m_is_stmt = !m_is_stmt;
20872 }
20873
20874 /* Handle DW_LNS_const_add_pc. */
20875 void handle_const_add_pc ();
20876
20877 /* Handle DW_LNS_fixed_advance_pc. */
20878 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20879 {
20880 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20881 m_op_index = 0;
20882 }
20883
20884 /* Handle DW_LNS_copy. */
20885 void handle_copy ()
20886 {
20887 record_line (false);
20888 m_discriminator = 0;
20889 }
20890
20891 /* Handle DW_LNE_end_sequence. */
20892 void handle_end_sequence ()
20893 {
20894 m_currently_recording_lines = true;
20895 }
20896
20897 private:
20898 /* Advance the line by LINE_DELTA. */
20899 void advance_line (int line_delta)
20900 {
20901 m_line += line_delta;
20902
20903 if (line_delta != 0)
20904 m_line_has_non_zero_discriminator = m_discriminator != 0;
20905 }
20906
20907 struct dwarf2_cu *m_cu;
20908
20909 gdbarch *m_gdbarch;
20910
20911 /* True if we're recording lines.
20912 Otherwise we're building partial symtabs and are just interested in
20913 finding include files mentioned by the line number program. */
20914 bool m_record_lines_p;
20915
20916 /* The line number header. */
20917 line_header *m_line_header;
20918
20919 /* These are part of the standard DWARF line number state machine,
20920 and initialized according to the DWARF spec. */
20921
20922 unsigned char m_op_index = 0;
20923 /* The line table index of the current file. */
20924 file_name_index m_file = 1;
20925 unsigned int m_line = 1;
20926
20927 /* These are initialized in the constructor. */
20928
20929 CORE_ADDR m_address;
20930 bool m_is_stmt;
20931 unsigned int m_discriminator;
20932
20933 /* Additional bits of state we need to track. */
20934
20935 /* The last file that we called dwarf2_start_subfile for.
20936 This is only used for TLLs. */
20937 unsigned int m_last_file = 0;
20938 /* The last file a line number was recorded for. */
20939 struct subfile *m_last_subfile = NULL;
20940
20941 /* The address of the last line entry. */
20942 CORE_ADDR m_last_address;
20943
20944 /* Set to true when a previous line at the same address (using
20945 m_last_address) had m_is_stmt true. This is reset to false when a
20946 line entry at a new address (m_address different to m_last_address) is
20947 processed. */
20948 bool m_stmt_at_address = false;
20949
20950 /* When true, record the lines we decode. */
20951 bool m_currently_recording_lines = false;
20952
20953 /* The last line number that was recorded, used to coalesce
20954 consecutive entries for the same line. This can happen, for
20955 example, when discriminators are present. PR 17276. */
20956 unsigned int m_last_line = 0;
20957 bool m_line_has_non_zero_discriminator = false;
20958 };
20959
20960 void
20961 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20962 {
20963 CORE_ADDR addr_adj = (((m_op_index + adjust)
20964 / m_line_header->maximum_ops_per_instruction)
20965 * m_line_header->minimum_instruction_length);
20966 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20967 m_op_index = ((m_op_index + adjust)
20968 % m_line_header->maximum_ops_per_instruction);
20969 }
20970
20971 void
20972 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20973 {
20974 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20975 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20976 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20977 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20978 / m_line_header->maximum_ops_per_instruction)
20979 * m_line_header->minimum_instruction_length);
20980 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20981 m_op_index = ((m_op_index + adj_opcode_d)
20982 % m_line_header->maximum_ops_per_instruction);
20983
20984 int line_delta = m_line_header->line_base + adj_opcode_r;
20985 advance_line (line_delta);
20986 record_line (false);
20987 m_discriminator = 0;
20988 }
20989
20990 void
20991 lnp_state_machine::handle_set_file (file_name_index file)
20992 {
20993 m_file = file;
20994
20995 const file_entry *fe = current_file ();
20996 if (fe == NULL)
20997 dwarf2_debug_line_missing_file_complaint ();
20998 else if (m_record_lines_p)
20999 {
21000 const char *dir = fe->include_dir (m_line_header);
21001
21002 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21003 m_line_has_non_zero_discriminator = m_discriminator != 0;
21004 dwarf2_start_subfile (m_cu, fe->name, dir);
21005 }
21006 }
21007
21008 void
21009 lnp_state_machine::handle_const_add_pc ()
21010 {
21011 CORE_ADDR adjust
21012 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21013
21014 CORE_ADDR addr_adj
21015 = (((m_op_index + adjust)
21016 / m_line_header->maximum_ops_per_instruction)
21017 * m_line_header->minimum_instruction_length);
21018
21019 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21020 m_op_index = ((m_op_index + adjust)
21021 % m_line_header->maximum_ops_per_instruction);
21022 }
21023
21024 /* Return non-zero if we should add LINE to the line number table.
21025 LINE is the line to add, LAST_LINE is the last line that was added,
21026 LAST_SUBFILE is the subfile for LAST_LINE.
21027 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21028 had a non-zero discriminator.
21029
21030 We have to be careful in the presence of discriminators.
21031 E.g., for this line:
21032
21033 for (i = 0; i < 100000; i++);
21034
21035 clang can emit four line number entries for that one line,
21036 each with a different discriminator.
21037 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21038
21039 However, we want gdb to coalesce all four entries into one.
21040 Otherwise the user could stepi into the middle of the line and
21041 gdb would get confused about whether the pc really was in the
21042 middle of the line.
21043
21044 Things are further complicated by the fact that two consecutive
21045 line number entries for the same line is a heuristic used by gcc
21046 to denote the end of the prologue. So we can't just discard duplicate
21047 entries, we have to be selective about it. The heuristic we use is
21048 that we only collapse consecutive entries for the same line if at least
21049 one of those entries has a non-zero discriminator. PR 17276.
21050
21051 Note: Addresses in the line number state machine can never go backwards
21052 within one sequence, thus this coalescing is ok. */
21053
21054 static int
21055 dwarf_record_line_p (struct dwarf2_cu *cu,
21056 unsigned int line, unsigned int last_line,
21057 int line_has_non_zero_discriminator,
21058 struct subfile *last_subfile)
21059 {
21060 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21061 return 1;
21062 if (line != last_line)
21063 return 1;
21064 /* Same line for the same file that we've seen already.
21065 As a last check, for pr 17276, only record the line if the line
21066 has never had a non-zero discriminator. */
21067 if (!line_has_non_zero_discriminator)
21068 return 1;
21069 return 0;
21070 }
21071
21072 /* Use the CU's builder to record line number LINE beginning at
21073 address ADDRESS in the line table of subfile SUBFILE. */
21074
21075 static void
21076 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21077 unsigned int line, CORE_ADDR address, bool is_stmt,
21078 struct dwarf2_cu *cu)
21079 {
21080 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21081
21082 if (dwarf_line_debug)
21083 {
21084 fprintf_unfiltered (gdb_stdlog,
21085 "Recording line %u, file %s, address %s\n",
21086 line, lbasename (subfile->name),
21087 paddress (gdbarch, address));
21088 }
21089
21090 if (cu != nullptr)
21091 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
21092 }
21093
21094 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21095 Mark the end of a set of line number records.
21096 The arguments are the same as for dwarf_record_line_1.
21097 If SUBFILE is NULL the request is ignored. */
21098
21099 static void
21100 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21101 CORE_ADDR address, struct dwarf2_cu *cu)
21102 {
21103 if (subfile == NULL)
21104 return;
21105
21106 if (dwarf_line_debug)
21107 {
21108 fprintf_unfiltered (gdb_stdlog,
21109 "Finishing current line, file %s, address %s\n",
21110 lbasename (subfile->name),
21111 paddress (gdbarch, address));
21112 }
21113
21114 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
21115 }
21116
21117 void
21118 lnp_state_machine::record_line (bool end_sequence)
21119 {
21120 if (dwarf_line_debug)
21121 {
21122 fprintf_unfiltered (gdb_stdlog,
21123 "Processing actual line %u: file %u,"
21124 " address %s, is_stmt %u, discrim %u%s\n",
21125 m_line, m_file,
21126 paddress (m_gdbarch, m_address),
21127 m_is_stmt, m_discriminator,
21128 (end_sequence ? "\t(end sequence)" : ""));
21129 }
21130
21131 file_entry *fe = current_file ();
21132
21133 if (fe == NULL)
21134 dwarf2_debug_line_missing_file_complaint ();
21135 /* For now we ignore lines not starting on an instruction boundary.
21136 But not when processing end_sequence for compatibility with the
21137 previous version of the code. */
21138 else if (m_op_index == 0 || end_sequence)
21139 {
21140 fe->included_p = 1;
21141 if (m_record_lines_p)
21142 {
21143 /* When we switch files we insert an end maker in the first file,
21144 switch to the second file and add a new line entry. The
21145 problem is that the end marker inserted in the first file will
21146 discard any previous line entries at the same address. If the
21147 line entries in the first file are marked as is-stmt, while
21148 the new line in the second file is non-stmt, then this means
21149 the end marker will discard is-stmt lines so we can have a
21150 non-stmt line. This means that there are less addresses at
21151 which the user can insert a breakpoint.
21152
21153 To improve this we track the last address in m_last_address,
21154 and whether we have seen an is-stmt at this address. Then
21155 when switching files, if we have seen a stmt at the current
21156 address, and we are switching to create a non-stmt line, then
21157 discard the new line. */
21158 bool file_changed
21159 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21160 bool ignore_this_line
21161 = ((file_changed && !end_sequence && m_last_address == m_address
21162 && !m_is_stmt && m_stmt_at_address)
21163 || (!end_sequence && m_line == 0));
21164
21165 if ((file_changed && !ignore_this_line) || end_sequence)
21166 {
21167 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21168 m_currently_recording_lines ? m_cu : nullptr);
21169 }
21170
21171 if (!end_sequence && !ignore_this_line)
21172 {
21173 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21174
21175 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21176 m_line_has_non_zero_discriminator,
21177 m_last_subfile))
21178 {
21179 buildsym_compunit *builder = m_cu->get_builder ();
21180 dwarf_record_line_1 (m_gdbarch,
21181 builder->get_current_subfile (),
21182 m_line, m_address, is_stmt,
21183 m_currently_recording_lines ? m_cu : nullptr);
21184 }
21185 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21186 m_last_line = m_line;
21187 }
21188 }
21189 }
21190
21191 /* Track whether we have seen any m_is_stmt true at m_address in case we
21192 have multiple line table entries all at m_address. */
21193 if (m_last_address != m_address)
21194 {
21195 m_stmt_at_address = false;
21196 m_last_address = m_address;
21197 }
21198 m_stmt_at_address |= m_is_stmt;
21199 }
21200
21201 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21202 line_header *lh, bool record_lines_p)
21203 {
21204 m_cu = cu;
21205 m_gdbarch = arch;
21206 m_record_lines_p = record_lines_p;
21207 m_line_header = lh;
21208
21209 m_currently_recording_lines = true;
21210
21211 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21212 was a line entry for it so that the backend has a chance to adjust it
21213 and also record it in case it needs it. This is currently used by MIPS
21214 code, cf. `mips_adjust_dwarf2_line'. */
21215 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21216 m_is_stmt = lh->default_is_stmt;
21217 m_discriminator = 0;
21218
21219 m_last_address = m_address;
21220 m_stmt_at_address = false;
21221 }
21222
21223 void
21224 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21225 const gdb_byte *line_ptr,
21226 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21227 {
21228 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21229 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21230 located at 0x0. In this case, additionally check that if
21231 ADDRESS < UNRELOCATED_LOWPC. */
21232
21233 if ((address == 0 && address < unrelocated_lowpc)
21234 || address == (CORE_ADDR) -1)
21235 {
21236 /* This line table is for a function which has been
21237 GCd by the linker. Ignore it. PR gdb/12528 */
21238
21239 struct objfile *objfile = cu->per_objfile->objfile;
21240 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21241
21242 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21243 line_offset, objfile_name (objfile));
21244 m_currently_recording_lines = false;
21245 /* Note: m_currently_recording_lines is left as false until we see
21246 DW_LNE_end_sequence. */
21247 }
21248 }
21249
21250 /* Subroutine of dwarf_decode_lines to simplify it.
21251 Process the line number information in LH.
21252 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21253 program in order to set included_p for every referenced header. */
21254
21255 static void
21256 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21257 const int decode_for_pst_p, CORE_ADDR lowpc)
21258 {
21259 const gdb_byte *line_ptr, *extended_end;
21260 const gdb_byte *line_end;
21261 unsigned int bytes_read, extended_len;
21262 unsigned char op_code, extended_op;
21263 CORE_ADDR baseaddr;
21264 struct objfile *objfile = cu->per_objfile->objfile;
21265 bfd *abfd = objfile->obfd;
21266 struct gdbarch *gdbarch = objfile->arch ();
21267 /* True if we're recording line info (as opposed to building partial
21268 symtabs and just interested in finding include files mentioned by
21269 the line number program). */
21270 bool record_lines_p = !decode_for_pst_p;
21271
21272 baseaddr = objfile->text_section_offset ();
21273
21274 line_ptr = lh->statement_program_start;
21275 line_end = lh->statement_program_end;
21276
21277 /* Read the statement sequences until there's nothing left. */
21278 while (line_ptr < line_end)
21279 {
21280 /* The DWARF line number program state machine. Reset the state
21281 machine at the start of each sequence. */
21282 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21283 bool end_sequence = false;
21284
21285 if (record_lines_p)
21286 {
21287 /* Start a subfile for the current file of the state
21288 machine. */
21289 const file_entry *fe = state_machine.current_file ();
21290
21291 if (fe != NULL)
21292 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21293 }
21294
21295 /* Decode the table. */
21296 while (line_ptr < line_end && !end_sequence)
21297 {
21298 op_code = read_1_byte (abfd, line_ptr);
21299 line_ptr += 1;
21300
21301 if (op_code >= lh->opcode_base)
21302 {
21303 /* Special opcode. */
21304 state_machine.handle_special_opcode (op_code);
21305 }
21306 else switch (op_code)
21307 {
21308 case DW_LNS_extended_op:
21309 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21310 &bytes_read);
21311 line_ptr += bytes_read;
21312 extended_end = line_ptr + extended_len;
21313 extended_op = read_1_byte (abfd, line_ptr);
21314 line_ptr += 1;
21315 if (DW_LNE_lo_user <= extended_op
21316 && extended_op <= DW_LNE_hi_user)
21317 {
21318 /* Vendor extension, ignore. */
21319 line_ptr = extended_end;
21320 break;
21321 }
21322 switch (extended_op)
21323 {
21324 case DW_LNE_end_sequence:
21325 state_machine.handle_end_sequence ();
21326 end_sequence = true;
21327 break;
21328 case DW_LNE_set_address:
21329 {
21330 CORE_ADDR address
21331 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21332 line_ptr += bytes_read;
21333
21334 state_machine.check_line_address (cu, line_ptr,
21335 lowpc - baseaddr, address);
21336 state_machine.handle_set_address (baseaddr, address);
21337 }
21338 break;
21339 case DW_LNE_define_file:
21340 {
21341 const char *cur_file;
21342 unsigned int mod_time, length;
21343 dir_index dindex;
21344
21345 cur_file = read_direct_string (abfd, line_ptr,
21346 &bytes_read);
21347 line_ptr += bytes_read;
21348 dindex = (dir_index)
21349 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21350 line_ptr += bytes_read;
21351 mod_time =
21352 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21353 line_ptr += bytes_read;
21354 length =
21355 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21356 line_ptr += bytes_read;
21357 lh->add_file_name (cur_file, dindex, mod_time, length);
21358 }
21359 break;
21360 case DW_LNE_set_discriminator:
21361 {
21362 /* The discriminator is not interesting to the
21363 debugger; just ignore it. We still need to
21364 check its value though:
21365 if there are consecutive entries for the same
21366 (non-prologue) line we want to coalesce them.
21367 PR 17276. */
21368 unsigned int discr
21369 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21370 line_ptr += bytes_read;
21371
21372 state_machine.handle_set_discriminator (discr);
21373 }
21374 break;
21375 default:
21376 complaint (_("mangled .debug_line section"));
21377 return;
21378 }
21379 /* Make sure that we parsed the extended op correctly. If e.g.
21380 we expected a different address size than the producer used,
21381 we may have read the wrong number of bytes. */
21382 if (line_ptr != extended_end)
21383 {
21384 complaint (_("mangled .debug_line section"));
21385 return;
21386 }
21387 break;
21388 case DW_LNS_copy:
21389 state_machine.handle_copy ();
21390 break;
21391 case DW_LNS_advance_pc:
21392 {
21393 CORE_ADDR adjust
21394 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21395 line_ptr += bytes_read;
21396
21397 state_machine.handle_advance_pc (adjust);
21398 }
21399 break;
21400 case DW_LNS_advance_line:
21401 {
21402 int line_delta
21403 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21404 line_ptr += bytes_read;
21405
21406 state_machine.handle_advance_line (line_delta);
21407 }
21408 break;
21409 case DW_LNS_set_file:
21410 {
21411 file_name_index file
21412 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21413 &bytes_read);
21414 line_ptr += bytes_read;
21415
21416 state_machine.handle_set_file (file);
21417 }
21418 break;
21419 case DW_LNS_set_column:
21420 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21421 line_ptr += bytes_read;
21422 break;
21423 case DW_LNS_negate_stmt:
21424 state_machine.handle_negate_stmt ();
21425 break;
21426 case DW_LNS_set_basic_block:
21427 break;
21428 /* Add to the address register of the state machine the
21429 address increment value corresponding to special opcode
21430 255. I.e., this value is scaled by the minimum
21431 instruction length since special opcode 255 would have
21432 scaled the increment. */
21433 case DW_LNS_const_add_pc:
21434 state_machine.handle_const_add_pc ();
21435 break;
21436 case DW_LNS_fixed_advance_pc:
21437 {
21438 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21439 line_ptr += 2;
21440
21441 state_machine.handle_fixed_advance_pc (addr_adj);
21442 }
21443 break;
21444 default:
21445 {
21446 /* Unknown standard opcode, ignore it. */
21447 int i;
21448
21449 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21450 {
21451 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21452 line_ptr += bytes_read;
21453 }
21454 }
21455 }
21456 }
21457
21458 if (!end_sequence)
21459 dwarf2_debug_line_missing_end_sequence_complaint ();
21460
21461 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21462 in which case we still finish recording the last line). */
21463 state_machine.record_line (true);
21464 }
21465 }
21466
21467 /* Decode the Line Number Program (LNP) for the given line_header
21468 structure and CU. The actual information extracted and the type
21469 of structures created from the LNP depends on the value of PST.
21470
21471 1. If PST is NULL, then this procedure uses the data from the program
21472 to create all necessary symbol tables, and their linetables.
21473
21474 2. If PST is not NULL, this procedure reads the program to determine
21475 the list of files included by the unit represented by PST, and
21476 builds all the associated partial symbol tables.
21477
21478 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21479 It is used for relative paths in the line table.
21480 NOTE: When processing partial symtabs (pst != NULL),
21481 comp_dir == pst->dirname.
21482
21483 NOTE: It is important that psymtabs have the same file name (via strcmp)
21484 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21485 symtab we don't use it in the name of the psymtabs we create.
21486 E.g. expand_line_sal requires this when finding psymtabs to expand.
21487 A good testcase for this is mb-inline.exp.
21488
21489 LOWPC is the lowest address in CU (or 0 if not known).
21490
21491 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21492 for its PC<->lines mapping information. Otherwise only the filename
21493 table is read in. */
21494
21495 static void
21496 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21497 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21498 CORE_ADDR lowpc, int decode_mapping)
21499 {
21500 struct objfile *objfile = cu->per_objfile->objfile;
21501 const int decode_for_pst_p = (pst != NULL);
21502
21503 if (decode_mapping)
21504 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21505
21506 if (decode_for_pst_p)
21507 {
21508 /* Now that we're done scanning the Line Header Program, we can
21509 create the psymtab of each included file. */
21510 for (auto &file_entry : lh->file_names ())
21511 if (file_entry.included_p == 1)
21512 {
21513 gdb::unique_xmalloc_ptr<char> name_holder;
21514 const char *include_name =
21515 psymtab_include_file_name (lh, file_entry, pst,
21516 comp_dir, &name_holder);
21517 if (include_name != NULL)
21518 dwarf2_create_include_psymtab (include_name, pst, objfile);
21519 }
21520 }
21521 else
21522 {
21523 /* Make sure a symtab is created for every file, even files
21524 which contain only variables (i.e. no code with associated
21525 line numbers). */
21526 buildsym_compunit *builder = cu->get_builder ();
21527 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21528
21529 for (auto &fe : lh->file_names ())
21530 {
21531 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21532 if (builder->get_current_subfile ()->symtab == NULL)
21533 {
21534 builder->get_current_subfile ()->symtab
21535 = allocate_symtab (cust,
21536 builder->get_current_subfile ()->name);
21537 }
21538 fe.symtab = builder->get_current_subfile ()->symtab;
21539 }
21540 }
21541 }
21542
21543 /* Start a subfile for DWARF. FILENAME is the name of the file and
21544 DIRNAME the name of the source directory which contains FILENAME
21545 or NULL if not known.
21546 This routine tries to keep line numbers from identical absolute and
21547 relative file names in a common subfile.
21548
21549 Using the `list' example from the GDB testsuite, which resides in
21550 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21551 of /srcdir/list0.c yields the following debugging information for list0.c:
21552
21553 DW_AT_name: /srcdir/list0.c
21554 DW_AT_comp_dir: /compdir
21555 files.files[0].name: list0.h
21556 files.files[0].dir: /srcdir
21557 files.files[1].name: list0.c
21558 files.files[1].dir: /srcdir
21559
21560 The line number information for list0.c has to end up in a single
21561 subfile, so that `break /srcdir/list0.c:1' works as expected.
21562 start_subfile will ensure that this happens provided that we pass the
21563 concatenation of files.files[1].dir and files.files[1].name as the
21564 subfile's name. */
21565
21566 static void
21567 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21568 const char *dirname)
21569 {
21570 gdb::unique_xmalloc_ptr<char> copy;
21571
21572 /* In order not to lose the line information directory,
21573 we concatenate it to the filename when it makes sense.
21574 Note that the Dwarf3 standard says (speaking of filenames in line
21575 information): ``The directory index is ignored for file names
21576 that represent full path names''. Thus ignoring dirname in the
21577 `else' branch below isn't an issue. */
21578
21579 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21580 {
21581 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21582 filename = copy.get ();
21583 }
21584
21585 cu->get_builder ()->start_subfile (filename);
21586 }
21587
21588 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21589 buildsym_compunit constructor. */
21590
21591 struct compunit_symtab *
21592 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21593 CORE_ADDR low_pc)
21594 {
21595 gdb_assert (m_builder == nullptr);
21596
21597 m_builder.reset (new struct buildsym_compunit
21598 (this->per_objfile->objfile,
21599 name, comp_dir, language, low_pc));
21600
21601 list_in_scope = get_builder ()->get_file_symbols ();
21602
21603 get_builder ()->record_debugformat ("DWARF 2");
21604 get_builder ()->record_producer (producer);
21605
21606 processing_has_namespace_info = false;
21607
21608 return get_builder ()->get_compunit_symtab ();
21609 }
21610
21611 static void
21612 var_decode_location (struct attribute *attr, struct symbol *sym,
21613 struct dwarf2_cu *cu)
21614 {
21615 struct objfile *objfile = cu->per_objfile->objfile;
21616 struct comp_unit_head *cu_header = &cu->header;
21617
21618 /* NOTE drow/2003-01-30: There used to be a comment and some special
21619 code here to turn a symbol with DW_AT_external and a
21620 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21621 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21622 with some versions of binutils) where shared libraries could have
21623 relocations against symbols in their debug information - the
21624 minimal symbol would have the right address, but the debug info
21625 would not. It's no longer necessary, because we will explicitly
21626 apply relocations when we read in the debug information now. */
21627
21628 /* A DW_AT_location attribute with no contents indicates that a
21629 variable has been optimized away. */
21630 if (attr->form_is_block () && attr->as_block ()->size == 0)
21631 {
21632 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21633 return;
21634 }
21635
21636 /* Handle one degenerate form of location expression specially, to
21637 preserve GDB's previous behavior when section offsets are
21638 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21639 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21640
21641 if (attr->form_is_block ())
21642 {
21643 struct dwarf_block *block = attr->as_block ();
21644
21645 if ((block->data[0] == DW_OP_addr
21646 && block->size == 1 + cu_header->addr_size)
21647 || ((block->data[0] == DW_OP_GNU_addr_index
21648 || block->data[0] == DW_OP_addrx)
21649 && (block->size
21650 == 1 + leb128_size (&block->data[1]))))
21651 {
21652 unsigned int dummy;
21653
21654 if (block->data[0] == DW_OP_addr)
21655 SET_SYMBOL_VALUE_ADDRESS
21656 (sym, cu->header.read_address (objfile->obfd,
21657 block->data + 1,
21658 &dummy));
21659 else
21660 SET_SYMBOL_VALUE_ADDRESS
21661 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21662 &dummy));
21663 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21664 fixup_symbol_section (sym, objfile);
21665 SET_SYMBOL_VALUE_ADDRESS
21666 (sym,
21667 SYMBOL_VALUE_ADDRESS (sym)
21668 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
21669 return;
21670 }
21671 }
21672
21673 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21674 expression evaluator, and use LOC_COMPUTED only when necessary
21675 (i.e. when the value of a register or memory location is
21676 referenced, or a thread-local block, etc.). Then again, it might
21677 not be worthwhile. I'm assuming that it isn't unless performance
21678 or memory numbers show me otherwise. */
21679
21680 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21681
21682 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21683 cu->has_loclist = true;
21684 }
21685
21686 /* Given a pointer to a DWARF information entry, figure out if we need
21687 to make a symbol table entry for it, and if so, create a new entry
21688 and return a pointer to it.
21689 If TYPE is NULL, determine symbol type from the die, otherwise
21690 used the passed type.
21691 If SPACE is not NULL, use it to hold the new symbol. If it is
21692 NULL, allocate a new symbol on the objfile's obstack. */
21693
21694 static struct symbol *
21695 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21696 struct symbol *space)
21697 {
21698 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21699 struct objfile *objfile = per_objfile->objfile;
21700 struct gdbarch *gdbarch = objfile->arch ();
21701 struct symbol *sym = NULL;
21702 const char *name;
21703 struct attribute *attr = NULL;
21704 struct attribute *attr2 = NULL;
21705 CORE_ADDR baseaddr;
21706 struct pending **list_to_add = NULL;
21707
21708 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21709
21710 baseaddr = objfile->text_section_offset ();
21711
21712 name = dwarf2_name (die, cu);
21713 if (name)
21714 {
21715 int suppress_add = 0;
21716
21717 if (space)
21718 sym = space;
21719 else
21720 sym = new (&objfile->objfile_obstack) symbol;
21721 OBJSTAT (objfile, n_syms++);
21722
21723 /* Cache this symbol's name and the name's demangled form (if any). */
21724 sym->set_language (cu->language, &objfile->objfile_obstack);
21725 /* Fortran does not have mangling standard and the mangling does differ
21726 between gfortran, iFort etc. */
21727 const char *physname
21728 = (cu->language == language_fortran
21729 ? dwarf2_full_name (name, die, cu)
21730 : dwarf2_physname (name, die, cu));
21731 const char *linkagename = dw2_linkage_name (die, cu);
21732
21733 if (linkagename == nullptr || cu->language == language_ada)
21734 sym->set_linkage_name (physname);
21735 else
21736 {
21737 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21738 sym->set_linkage_name (linkagename);
21739 }
21740
21741 /* Default assumptions.
21742 Use the passed type or decode it from the die. */
21743 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21744 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21745 if (type != NULL)
21746 SYMBOL_TYPE (sym) = type;
21747 else
21748 SYMBOL_TYPE (sym) = die_type (die, cu);
21749 attr = dwarf2_attr (die,
21750 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21751 cu);
21752 if (attr != nullptr)
21753 SYMBOL_LINE (sym) = attr->constant_value (0);
21754
21755 attr = dwarf2_attr (die,
21756 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21757 cu);
21758 if (attr != nullptr && attr->form_is_unsigned ())
21759 {
21760 file_name_index file_index
21761 = (file_name_index) attr->as_unsigned ();
21762 struct file_entry *fe;
21763
21764 if (cu->line_header != NULL)
21765 fe = cu->line_header->file_name_at (file_index);
21766 else
21767 fe = NULL;
21768
21769 if (fe == NULL)
21770 complaint (_("file index out of range"));
21771 else
21772 symbol_set_symtab (sym, fe->symtab);
21773 }
21774
21775 switch (die->tag)
21776 {
21777 case DW_TAG_label:
21778 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21779 if (attr != nullptr)
21780 {
21781 CORE_ADDR addr;
21782
21783 addr = attr->as_address ();
21784 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21785 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21786 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21787 }
21788 else
21789 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21790 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21791 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21792 add_symbol_to_list (sym, cu->list_in_scope);
21793 break;
21794 case DW_TAG_subprogram:
21795 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21796 finish_block. */
21797 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21798 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21799 if ((attr2 != nullptr && attr2->as_boolean ())
21800 || cu->language == language_ada
21801 || cu->language == language_fortran)
21802 {
21803 /* Subprograms marked external are stored as a global symbol.
21804 Ada and Fortran subprograms, whether marked external or
21805 not, are always stored as a global symbol, because we want
21806 to be able to access them globally. For instance, we want
21807 to be able to break on a nested subprogram without having
21808 to specify the context. */
21809 list_to_add = cu->get_builder ()->get_global_symbols ();
21810 }
21811 else
21812 {
21813 list_to_add = cu->list_in_scope;
21814 }
21815 break;
21816 case DW_TAG_inlined_subroutine:
21817 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21818 finish_block. */
21819 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21820 SYMBOL_INLINED (sym) = 1;
21821 list_to_add = cu->list_in_scope;
21822 break;
21823 case DW_TAG_template_value_param:
21824 suppress_add = 1;
21825 /* Fall through. */
21826 case DW_TAG_constant:
21827 case DW_TAG_variable:
21828 case DW_TAG_member:
21829 /* Compilation with minimal debug info may result in
21830 variables with missing type entries. Change the
21831 misleading `void' type to something sensible. */
21832 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
21833 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21834
21835 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21836 /* In the case of DW_TAG_member, we should only be called for
21837 static const members. */
21838 if (die->tag == DW_TAG_member)
21839 {
21840 /* dwarf2_add_field uses die_is_declaration,
21841 so we do the same. */
21842 gdb_assert (die_is_declaration (die, cu));
21843 gdb_assert (attr);
21844 }
21845 if (attr != nullptr)
21846 {
21847 dwarf2_const_value (attr, sym, cu);
21848 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21849 if (!suppress_add)
21850 {
21851 if (attr2 != nullptr && attr2->as_boolean ())
21852 list_to_add = cu->get_builder ()->get_global_symbols ();
21853 else
21854 list_to_add = cu->list_in_scope;
21855 }
21856 break;
21857 }
21858 attr = dwarf2_attr (die, DW_AT_location, cu);
21859 if (attr != nullptr)
21860 {
21861 var_decode_location (attr, sym, cu);
21862 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21863
21864 /* Fortran explicitly imports any global symbols to the local
21865 scope by DW_TAG_common_block. */
21866 if (cu->language == language_fortran && die->parent
21867 && die->parent->tag == DW_TAG_common_block)
21868 attr2 = NULL;
21869
21870 if (SYMBOL_CLASS (sym) == LOC_STATIC
21871 && SYMBOL_VALUE_ADDRESS (sym) == 0
21872 && !per_objfile->per_bfd->has_section_at_zero)
21873 {
21874 /* When a static variable is eliminated by the linker,
21875 the corresponding debug information is not stripped
21876 out, but the variable address is set to null;
21877 do not add such variables into symbol table. */
21878 }
21879 else if (attr2 != nullptr && attr2->as_boolean ())
21880 {
21881 if (SYMBOL_CLASS (sym) == LOC_STATIC
21882 && (objfile->flags & OBJF_MAINLINE) == 0
21883 && per_objfile->per_bfd->can_copy)
21884 {
21885 /* A global static variable might be subject to
21886 copy relocation. We first check for a local
21887 minsym, though, because maybe the symbol was
21888 marked hidden, in which case this would not
21889 apply. */
21890 bound_minimal_symbol found
21891 = (lookup_minimal_symbol_linkage
21892 (sym->linkage_name (), objfile));
21893 if (found.minsym != nullptr)
21894 sym->maybe_copied = 1;
21895 }
21896
21897 /* A variable with DW_AT_external is never static,
21898 but it may be block-scoped. */
21899 list_to_add
21900 = ((cu->list_in_scope
21901 == cu->get_builder ()->get_file_symbols ())
21902 ? cu->get_builder ()->get_global_symbols ()
21903 : cu->list_in_scope);
21904 }
21905 else
21906 list_to_add = cu->list_in_scope;
21907 }
21908 else
21909 {
21910 /* We do not know the address of this symbol.
21911 If it is an external symbol and we have type information
21912 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21913 The address of the variable will then be determined from
21914 the minimal symbol table whenever the variable is
21915 referenced. */
21916 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21917
21918 /* Fortran explicitly imports any global symbols to the local
21919 scope by DW_TAG_common_block. */
21920 if (cu->language == language_fortran && die->parent
21921 && die->parent->tag == DW_TAG_common_block)
21922 {
21923 /* SYMBOL_CLASS doesn't matter here because
21924 read_common_block is going to reset it. */
21925 if (!suppress_add)
21926 list_to_add = cu->list_in_scope;
21927 }
21928 else if (attr2 != nullptr && attr2->as_boolean ()
21929 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21930 {
21931 /* A variable with DW_AT_external is never static, but it
21932 may be block-scoped. */
21933 list_to_add
21934 = ((cu->list_in_scope
21935 == cu->get_builder ()->get_file_symbols ())
21936 ? cu->get_builder ()->get_global_symbols ()
21937 : cu->list_in_scope);
21938
21939 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21940 }
21941 else if (!die_is_declaration (die, cu))
21942 {
21943 /* Use the default LOC_OPTIMIZED_OUT class. */
21944 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21945 if (!suppress_add)
21946 list_to_add = cu->list_in_scope;
21947 }
21948 }
21949 break;
21950 case DW_TAG_formal_parameter:
21951 {
21952 /* If we are inside a function, mark this as an argument. If
21953 not, we might be looking at an argument to an inlined function
21954 when we do not have enough information to show inlined frames;
21955 pretend it's a local variable in that case so that the user can
21956 still see it. */
21957 struct context_stack *curr
21958 = cu->get_builder ()->get_current_context_stack ();
21959 if (curr != nullptr && curr->name != nullptr)
21960 SYMBOL_IS_ARGUMENT (sym) = 1;
21961 attr = dwarf2_attr (die, DW_AT_location, cu);
21962 if (attr != nullptr)
21963 {
21964 var_decode_location (attr, sym, cu);
21965 }
21966 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21967 if (attr != nullptr)
21968 {
21969 dwarf2_const_value (attr, sym, cu);
21970 }
21971
21972 list_to_add = cu->list_in_scope;
21973 }
21974 break;
21975 case DW_TAG_unspecified_parameters:
21976 /* From varargs functions; gdb doesn't seem to have any
21977 interest in this information, so just ignore it for now.
21978 (FIXME?) */
21979 break;
21980 case DW_TAG_template_type_param:
21981 suppress_add = 1;
21982 /* Fall through. */
21983 case DW_TAG_class_type:
21984 case DW_TAG_interface_type:
21985 case DW_TAG_structure_type:
21986 case DW_TAG_union_type:
21987 case DW_TAG_set_type:
21988 case DW_TAG_enumeration_type:
21989 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21990 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21991
21992 {
21993 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21994 really ever be static objects: otherwise, if you try
21995 to, say, break of a class's method and you're in a file
21996 which doesn't mention that class, it won't work unless
21997 the check for all static symbols in lookup_symbol_aux
21998 saves you. See the OtherFileClass tests in
21999 gdb.c++/namespace.exp. */
22000
22001 if (!suppress_add)
22002 {
22003 buildsym_compunit *builder = cu->get_builder ();
22004 list_to_add
22005 = (cu->list_in_scope == builder->get_file_symbols ()
22006 && cu->language == language_cplus
22007 ? builder->get_global_symbols ()
22008 : cu->list_in_scope);
22009
22010 /* The semantics of C++ state that "struct foo {
22011 ... }" also defines a typedef for "foo". */
22012 if (cu->language == language_cplus
22013 || cu->language == language_ada
22014 || cu->language == language_d
22015 || cu->language == language_rust)
22016 {
22017 /* The symbol's name is already allocated along
22018 with this objfile, so we don't need to
22019 duplicate it for the type. */
22020 if (SYMBOL_TYPE (sym)->name () == 0)
22021 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
22022 }
22023 }
22024 }
22025 break;
22026 case DW_TAG_typedef:
22027 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22028 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22029 list_to_add = cu->list_in_scope;
22030 break;
22031 case DW_TAG_base_type:
22032 case DW_TAG_subrange_type:
22033 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22034 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22035 list_to_add = cu->list_in_scope;
22036 break;
22037 case DW_TAG_enumerator:
22038 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22039 if (attr != nullptr)
22040 {
22041 dwarf2_const_value (attr, sym, cu);
22042 }
22043 {
22044 /* NOTE: carlton/2003-11-10: See comment above in the
22045 DW_TAG_class_type, etc. block. */
22046
22047 list_to_add
22048 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22049 && cu->language == language_cplus
22050 ? cu->get_builder ()->get_global_symbols ()
22051 : cu->list_in_scope);
22052 }
22053 break;
22054 case DW_TAG_imported_declaration:
22055 case DW_TAG_namespace:
22056 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22057 list_to_add = cu->get_builder ()->get_global_symbols ();
22058 break;
22059 case DW_TAG_module:
22060 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22061 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22062 list_to_add = cu->get_builder ()->get_global_symbols ();
22063 break;
22064 case DW_TAG_common_block:
22065 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22066 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22067 add_symbol_to_list (sym, cu->list_in_scope);
22068 break;
22069 default:
22070 /* Not a tag we recognize. Hopefully we aren't processing
22071 trash data, but since we must specifically ignore things
22072 we don't recognize, there is nothing else we should do at
22073 this point. */
22074 complaint (_("unsupported tag: '%s'"),
22075 dwarf_tag_name (die->tag));
22076 break;
22077 }
22078
22079 if (suppress_add)
22080 {
22081 sym->hash_next = objfile->template_symbols;
22082 objfile->template_symbols = sym;
22083 list_to_add = NULL;
22084 }
22085
22086 if (list_to_add != NULL)
22087 add_symbol_to_list (sym, list_to_add);
22088
22089 /* For the benefit of old versions of GCC, check for anonymous
22090 namespaces based on the demangled name. */
22091 if (!cu->processing_has_namespace_info
22092 && cu->language == language_cplus)
22093 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22094 }
22095 return (sym);
22096 }
22097
22098 /* Given an attr with a DW_FORM_dataN value in host byte order,
22099 zero-extend it as appropriate for the symbol's type. The DWARF
22100 standard (v4) is not entirely clear about the meaning of using
22101 DW_FORM_dataN for a constant with a signed type, where the type is
22102 wider than the data. The conclusion of a discussion on the DWARF
22103 list was that this is unspecified. We choose to always zero-extend
22104 because that is the interpretation long in use by GCC. */
22105
22106 static gdb_byte *
22107 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22108 struct dwarf2_cu *cu, LONGEST *value, int bits)
22109 {
22110 struct objfile *objfile = cu->per_objfile->objfile;
22111 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22112 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22113 LONGEST l = attr->constant_value (0);
22114
22115 if (bits < sizeof (*value) * 8)
22116 {
22117 l &= ((LONGEST) 1 << bits) - 1;
22118 *value = l;
22119 }
22120 else if (bits == sizeof (*value) * 8)
22121 *value = l;
22122 else
22123 {
22124 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22125 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22126 return bytes;
22127 }
22128
22129 return NULL;
22130 }
22131
22132 /* Read a constant value from an attribute. Either set *VALUE, or if
22133 the value does not fit in *VALUE, set *BYTES - either already
22134 allocated on the objfile obstack, or newly allocated on OBSTACK,
22135 or, set *BATON, if we translated the constant to a location
22136 expression. */
22137
22138 static void
22139 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22140 const char *name, struct obstack *obstack,
22141 struct dwarf2_cu *cu,
22142 LONGEST *value, const gdb_byte **bytes,
22143 struct dwarf2_locexpr_baton **baton)
22144 {
22145 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22146 struct objfile *objfile = per_objfile->objfile;
22147 struct comp_unit_head *cu_header = &cu->header;
22148 struct dwarf_block *blk;
22149 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22150 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22151
22152 *value = 0;
22153 *bytes = NULL;
22154 *baton = NULL;
22155
22156 switch (attr->form)
22157 {
22158 case DW_FORM_addr:
22159 case DW_FORM_addrx:
22160 case DW_FORM_GNU_addr_index:
22161 {
22162 gdb_byte *data;
22163
22164 if (TYPE_LENGTH (type) != cu_header->addr_size)
22165 dwarf2_const_value_length_mismatch_complaint (name,
22166 cu_header->addr_size,
22167 TYPE_LENGTH (type));
22168 /* Symbols of this form are reasonably rare, so we just
22169 piggyback on the existing location code rather than writing
22170 a new implementation of symbol_computed_ops. */
22171 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22172 (*baton)->per_objfile = per_objfile;
22173 (*baton)->per_cu = cu->per_cu;
22174 gdb_assert ((*baton)->per_cu);
22175
22176 (*baton)->size = 2 + cu_header->addr_size;
22177 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22178 (*baton)->data = data;
22179
22180 data[0] = DW_OP_addr;
22181 store_unsigned_integer (&data[1], cu_header->addr_size,
22182 byte_order, attr->as_address ());
22183 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22184 }
22185 break;
22186 case DW_FORM_string:
22187 case DW_FORM_strp:
22188 case DW_FORM_strx:
22189 case DW_FORM_GNU_str_index:
22190 case DW_FORM_GNU_strp_alt:
22191 /* The string is already allocated on the objfile obstack, point
22192 directly to it. */
22193 *bytes = (const gdb_byte *) attr->as_string ();
22194 break;
22195 case DW_FORM_block1:
22196 case DW_FORM_block2:
22197 case DW_FORM_block4:
22198 case DW_FORM_block:
22199 case DW_FORM_exprloc:
22200 case DW_FORM_data16:
22201 blk = attr->as_block ();
22202 if (TYPE_LENGTH (type) != blk->size)
22203 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22204 TYPE_LENGTH (type));
22205 *bytes = blk->data;
22206 break;
22207
22208 /* The DW_AT_const_value attributes are supposed to carry the
22209 symbol's value "represented as it would be on the target
22210 architecture." By the time we get here, it's already been
22211 converted to host endianness, so we just need to sign- or
22212 zero-extend it as appropriate. */
22213 case DW_FORM_data1:
22214 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22215 break;
22216 case DW_FORM_data2:
22217 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22218 break;
22219 case DW_FORM_data4:
22220 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22221 break;
22222 case DW_FORM_data8:
22223 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22224 break;
22225
22226 case DW_FORM_sdata:
22227 case DW_FORM_implicit_const:
22228 *value = attr->as_signed ();
22229 break;
22230
22231 case DW_FORM_udata:
22232 *value = attr->as_unsigned ();
22233 break;
22234
22235 default:
22236 complaint (_("unsupported const value attribute form: '%s'"),
22237 dwarf_form_name (attr->form));
22238 *value = 0;
22239 break;
22240 }
22241 }
22242
22243
22244 /* Copy constant value from an attribute to a symbol. */
22245
22246 static void
22247 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22248 struct dwarf2_cu *cu)
22249 {
22250 struct objfile *objfile = cu->per_objfile->objfile;
22251 LONGEST value;
22252 const gdb_byte *bytes;
22253 struct dwarf2_locexpr_baton *baton;
22254
22255 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22256 sym->print_name (),
22257 &objfile->objfile_obstack, cu,
22258 &value, &bytes, &baton);
22259
22260 if (baton != NULL)
22261 {
22262 SYMBOL_LOCATION_BATON (sym) = baton;
22263 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22264 }
22265 else if (bytes != NULL)
22266 {
22267 SYMBOL_VALUE_BYTES (sym) = bytes;
22268 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22269 }
22270 else
22271 {
22272 SYMBOL_VALUE (sym) = value;
22273 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22274 }
22275 }
22276
22277 /* Return the type of the die in question using its DW_AT_type attribute. */
22278
22279 static struct type *
22280 die_type (struct die_info *die, struct dwarf2_cu *cu)
22281 {
22282 struct attribute *type_attr;
22283
22284 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22285 if (!type_attr)
22286 {
22287 struct objfile *objfile = cu->per_objfile->objfile;
22288 /* A missing DW_AT_type represents a void type. */
22289 return objfile_type (objfile)->builtin_void;
22290 }
22291
22292 return lookup_die_type (die, type_attr, cu);
22293 }
22294
22295 /* True iff CU's producer generates GNAT Ada auxiliary information
22296 that allows to find parallel types through that information instead
22297 of having to do expensive parallel lookups by type name. */
22298
22299 static int
22300 need_gnat_info (struct dwarf2_cu *cu)
22301 {
22302 /* Assume that the Ada compiler was GNAT, which always produces
22303 the auxiliary information. */
22304 return (cu->language == language_ada);
22305 }
22306
22307 /* Return the auxiliary type of the die in question using its
22308 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22309 attribute is not present. */
22310
22311 static struct type *
22312 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22313 {
22314 struct attribute *type_attr;
22315
22316 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22317 if (!type_attr)
22318 return NULL;
22319
22320 return lookup_die_type (die, type_attr, cu);
22321 }
22322
22323 /* If DIE has a descriptive_type attribute, then set the TYPE's
22324 descriptive type accordingly. */
22325
22326 static void
22327 set_descriptive_type (struct type *type, struct die_info *die,
22328 struct dwarf2_cu *cu)
22329 {
22330 struct type *descriptive_type = die_descriptive_type (die, cu);
22331
22332 if (descriptive_type)
22333 {
22334 ALLOCATE_GNAT_AUX_TYPE (type);
22335 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22336 }
22337 }
22338
22339 /* Return the containing type of the die in question using its
22340 DW_AT_containing_type attribute. */
22341
22342 static struct type *
22343 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22344 {
22345 struct attribute *type_attr;
22346 struct objfile *objfile = cu->per_objfile->objfile;
22347
22348 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22349 if (!type_attr)
22350 error (_("Dwarf Error: Problem turning containing type into gdb type "
22351 "[in module %s]"), objfile_name (objfile));
22352
22353 return lookup_die_type (die, type_attr, cu);
22354 }
22355
22356 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22357
22358 static struct type *
22359 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22360 {
22361 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22362 struct objfile *objfile = per_objfile->objfile;
22363 char *saved;
22364
22365 std::string message
22366 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22367 objfile_name (objfile),
22368 sect_offset_str (cu->header.sect_off),
22369 sect_offset_str (die->sect_off));
22370 saved = obstack_strdup (&objfile->objfile_obstack, message);
22371
22372 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22373 }
22374
22375 /* Look up the type of DIE in CU using its type attribute ATTR.
22376 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22377 DW_AT_containing_type.
22378 If there is no type substitute an error marker. */
22379
22380 static struct type *
22381 lookup_die_type (struct die_info *die, const struct attribute *attr,
22382 struct dwarf2_cu *cu)
22383 {
22384 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22385 struct objfile *objfile = per_objfile->objfile;
22386 struct type *this_type;
22387
22388 gdb_assert (attr->name == DW_AT_type
22389 || attr->name == DW_AT_GNAT_descriptive_type
22390 || attr->name == DW_AT_containing_type);
22391
22392 /* First see if we have it cached. */
22393
22394 if (attr->form == DW_FORM_GNU_ref_alt)
22395 {
22396 struct dwarf2_per_cu_data *per_cu;
22397 sect_offset sect_off = attr->get_ref_die_offset ();
22398
22399 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22400 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22401 }
22402 else if (attr->form_is_ref ())
22403 {
22404 sect_offset sect_off = attr->get_ref_die_offset ();
22405
22406 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22407 }
22408 else if (attr->form == DW_FORM_ref_sig8)
22409 {
22410 ULONGEST signature = attr->as_signature ();
22411
22412 return get_signatured_type (die, signature, cu);
22413 }
22414 else
22415 {
22416 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22417 " at %s [in module %s]"),
22418 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22419 objfile_name (objfile));
22420 return build_error_marker_type (cu, die);
22421 }
22422
22423 /* If not cached we need to read it in. */
22424
22425 if (this_type == NULL)
22426 {
22427 struct die_info *type_die = NULL;
22428 struct dwarf2_cu *type_cu = cu;
22429
22430 if (attr->form_is_ref ())
22431 type_die = follow_die_ref (die, attr, &type_cu);
22432 if (type_die == NULL)
22433 return build_error_marker_type (cu, die);
22434 /* If we find the type now, it's probably because the type came
22435 from an inter-CU reference and the type's CU got expanded before
22436 ours. */
22437 this_type = read_type_die (type_die, type_cu);
22438 }
22439
22440 /* If we still don't have a type use an error marker. */
22441
22442 if (this_type == NULL)
22443 return build_error_marker_type (cu, die);
22444
22445 return this_type;
22446 }
22447
22448 /* Return the type in DIE, CU.
22449 Returns NULL for invalid types.
22450
22451 This first does a lookup in die_type_hash,
22452 and only reads the die in if necessary.
22453
22454 NOTE: This can be called when reading in partial or full symbols. */
22455
22456 static struct type *
22457 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22458 {
22459 struct type *this_type;
22460
22461 this_type = get_die_type (die, cu);
22462 if (this_type)
22463 return this_type;
22464
22465 return read_type_die_1 (die, cu);
22466 }
22467
22468 /* Read the type in DIE, CU.
22469 Returns NULL for invalid types. */
22470
22471 static struct type *
22472 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22473 {
22474 struct type *this_type = NULL;
22475
22476 switch (die->tag)
22477 {
22478 case DW_TAG_class_type:
22479 case DW_TAG_interface_type:
22480 case DW_TAG_structure_type:
22481 case DW_TAG_union_type:
22482 this_type = read_structure_type (die, cu);
22483 break;
22484 case DW_TAG_enumeration_type:
22485 this_type = read_enumeration_type (die, cu);
22486 break;
22487 case DW_TAG_subprogram:
22488 case DW_TAG_subroutine_type:
22489 case DW_TAG_inlined_subroutine:
22490 this_type = read_subroutine_type (die, cu);
22491 break;
22492 case DW_TAG_array_type:
22493 this_type = read_array_type (die, cu);
22494 break;
22495 case DW_TAG_set_type:
22496 this_type = read_set_type (die, cu);
22497 break;
22498 case DW_TAG_pointer_type:
22499 this_type = read_tag_pointer_type (die, cu);
22500 break;
22501 case DW_TAG_ptr_to_member_type:
22502 this_type = read_tag_ptr_to_member_type (die, cu);
22503 break;
22504 case DW_TAG_reference_type:
22505 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22506 break;
22507 case DW_TAG_rvalue_reference_type:
22508 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22509 break;
22510 case DW_TAG_const_type:
22511 this_type = read_tag_const_type (die, cu);
22512 break;
22513 case DW_TAG_volatile_type:
22514 this_type = read_tag_volatile_type (die, cu);
22515 break;
22516 case DW_TAG_restrict_type:
22517 this_type = read_tag_restrict_type (die, cu);
22518 break;
22519 case DW_TAG_string_type:
22520 this_type = read_tag_string_type (die, cu);
22521 break;
22522 case DW_TAG_typedef:
22523 this_type = read_typedef (die, cu);
22524 break;
22525 case DW_TAG_subrange_type:
22526 this_type = read_subrange_type (die, cu);
22527 break;
22528 case DW_TAG_base_type:
22529 this_type = read_base_type (die, cu);
22530 break;
22531 case DW_TAG_unspecified_type:
22532 this_type = read_unspecified_type (die, cu);
22533 break;
22534 case DW_TAG_namespace:
22535 this_type = read_namespace_type (die, cu);
22536 break;
22537 case DW_TAG_module:
22538 this_type = read_module_type (die, cu);
22539 break;
22540 case DW_TAG_atomic_type:
22541 this_type = read_tag_atomic_type (die, cu);
22542 break;
22543 default:
22544 complaint (_("unexpected tag in read_type_die: '%s'"),
22545 dwarf_tag_name (die->tag));
22546 break;
22547 }
22548
22549 return this_type;
22550 }
22551
22552 /* See if we can figure out if the class lives in a namespace. We do
22553 this by looking for a member function; its demangled name will
22554 contain namespace info, if there is any.
22555 Return the computed name or NULL.
22556 Space for the result is allocated on the objfile's obstack.
22557 This is the full-die version of guess_partial_die_structure_name.
22558 In this case we know DIE has no useful parent. */
22559
22560 static const char *
22561 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22562 {
22563 struct die_info *spec_die;
22564 struct dwarf2_cu *spec_cu;
22565 struct die_info *child;
22566 struct objfile *objfile = cu->per_objfile->objfile;
22567
22568 spec_cu = cu;
22569 spec_die = die_specification (die, &spec_cu);
22570 if (spec_die != NULL)
22571 {
22572 die = spec_die;
22573 cu = spec_cu;
22574 }
22575
22576 for (child = die->child;
22577 child != NULL;
22578 child = child->sibling)
22579 {
22580 if (child->tag == DW_TAG_subprogram)
22581 {
22582 const char *linkage_name = dw2_linkage_name (child, cu);
22583
22584 if (linkage_name != NULL)
22585 {
22586 gdb::unique_xmalloc_ptr<char> actual_name
22587 (cu->language_defn->class_name_from_physname (linkage_name));
22588 const char *name = NULL;
22589
22590 if (actual_name != NULL)
22591 {
22592 const char *die_name = dwarf2_name (die, cu);
22593
22594 if (die_name != NULL
22595 && strcmp (die_name, actual_name.get ()) != 0)
22596 {
22597 /* Strip off the class name from the full name.
22598 We want the prefix. */
22599 int die_name_len = strlen (die_name);
22600 int actual_name_len = strlen (actual_name.get ());
22601 const char *ptr = actual_name.get ();
22602
22603 /* Test for '::' as a sanity check. */
22604 if (actual_name_len > die_name_len + 2
22605 && ptr[actual_name_len - die_name_len - 1] == ':')
22606 name = obstack_strndup (
22607 &objfile->per_bfd->storage_obstack,
22608 ptr, actual_name_len - die_name_len - 2);
22609 }
22610 }
22611 return name;
22612 }
22613 }
22614 }
22615
22616 return NULL;
22617 }
22618
22619 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22620 prefix part in such case. See
22621 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22622
22623 static const char *
22624 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22625 {
22626 struct attribute *attr;
22627 const char *base;
22628
22629 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22630 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22631 return NULL;
22632
22633 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22634 return NULL;
22635
22636 attr = dw2_linkage_name_attr (die, cu);
22637 const char *attr_name = attr->as_string ();
22638 if (attr == NULL || attr_name == NULL)
22639 return NULL;
22640
22641 /* dwarf2_name had to be already called. */
22642 gdb_assert (attr->canonical_string_p ());
22643
22644 /* Strip the base name, keep any leading namespaces/classes. */
22645 base = strrchr (attr_name, ':');
22646 if (base == NULL || base == attr_name || base[-1] != ':')
22647 return "";
22648
22649 struct objfile *objfile = cu->per_objfile->objfile;
22650 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22651 attr_name,
22652 &base[-1] - attr_name);
22653 }
22654
22655 /* Return the name of the namespace/class that DIE is defined within,
22656 or "" if we can't tell. The caller should not xfree the result.
22657
22658 For example, if we're within the method foo() in the following
22659 code:
22660
22661 namespace N {
22662 class C {
22663 void foo () {
22664 }
22665 };
22666 }
22667
22668 then determine_prefix on foo's die will return "N::C". */
22669
22670 static const char *
22671 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22672 {
22673 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22674 struct die_info *parent, *spec_die;
22675 struct dwarf2_cu *spec_cu;
22676 struct type *parent_type;
22677 const char *retval;
22678
22679 if (cu->language != language_cplus
22680 && cu->language != language_fortran && cu->language != language_d
22681 && cu->language != language_rust)
22682 return "";
22683
22684 retval = anonymous_struct_prefix (die, cu);
22685 if (retval)
22686 return retval;
22687
22688 /* We have to be careful in the presence of DW_AT_specification.
22689 For example, with GCC 3.4, given the code
22690
22691 namespace N {
22692 void foo() {
22693 // Definition of N::foo.
22694 }
22695 }
22696
22697 then we'll have a tree of DIEs like this:
22698
22699 1: DW_TAG_compile_unit
22700 2: DW_TAG_namespace // N
22701 3: DW_TAG_subprogram // declaration of N::foo
22702 4: DW_TAG_subprogram // definition of N::foo
22703 DW_AT_specification // refers to die #3
22704
22705 Thus, when processing die #4, we have to pretend that we're in
22706 the context of its DW_AT_specification, namely the contex of die
22707 #3. */
22708 spec_cu = cu;
22709 spec_die = die_specification (die, &spec_cu);
22710 if (spec_die == NULL)
22711 parent = die->parent;
22712 else
22713 {
22714 parent = spec_die->parent;
22715 cu = spec_cu;
22716 }
22717
22718 if (parent == NULL)
22719 return "";
22720 else if (parent->building_fullname)
22721 {
22722 const char *name;
22723 const char *parent_name;
22724
22725 /* It has been seen on RealView 2.2 built binaries,
22726 DW_TAG_template_type_param types actually _defined_ as
22727 children of the parent class:
22728
22729 enum E {};
22730 template class <class Enum> Class{};
22731 Class<enum E> class_e;
22732
22733 1: DW_TAG_class_type (Class)
22734 2: DW_TAG_enumeration_type (E)
22735 3: DW_TAG_enumerator (enum1:0)
22736 3: DW_TAG_enumerator (enum2:1)
22737 ...
22738 2: DW_TAG_template_type_param
22739 DW_AT_type DW_FORM_ref_udata (E)
22740
22741 Besides being broken debug info, it can put GDB into an
22742 infinite loop. Consider:
22743
22744 When we're building the full name for Class<E>, we'll start
22745 at Class, and go look over its template type parameters,
22746 finding E. We'll then try to build the full name of E, and
22747 reach here. We're now trying to build the full name of E,
22748 and look over the parent DIE for containing scope. In the
22749 broken case, if we followed the parent DIE of E, we'd again
22750 find Class, and once again go look at its template type
22751 arguments, etc., etc. Simply don't consider such parent die
22752 as source-level parent of this die (it can't be, the language
22753 doesn't allow it), and break the loop here. */
22754 name = dwarf2_name (die, cu);
22755 parent_name = dwarf2_name (parent, cu);
22756 complaint (_("template param type '%s' defined within parent '%s'"),
22757 name ? name : "<unknown>",
22758 parent_name ? parent_name : "<unknown>");
22759 return "";
22760 }
22761 else
22762 switch (parent->tag)
22763 {
22764 case DW_TAG_namespace:
22765 parent_type = read_type_die (parent, cu);
22766 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22767 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22768 Work around this problem here. */
22769 if (cu->language == language_cplus
22770 && strcmp (parent_type->name (), "::") == 0)
22771 return "";
22772 /* We give a name to even anonymous namespaces. */
22773 return parent_type->name ();
22774 case DW_TAG_class_type:
22775 case DW_TAG_interface_type:
22776 case DW_TAG_structure_type:
22777 case DW_TAG_union_type:
22778 case DW_TAG_module:
22779 parent_type = read_type_die (parent, cu);
22780 if (parent_type->name () != NULL)
22781 return parent_type->name ();
22782 else
22783 /* An anonymous structure is only allowed non-static data
22784 members; no typedefs, no member functions, et cetera.
22785 So it does not need a prefix. */
22786 return "";
22787 case DW_TAG_compile_unit:
22788 case DW_TAG_partial_unit:
22789 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22790 if (cu->language == language_cplus
22791 && !per_objfile->per_bfd->types.empty ()
22792 && die->child != NULL
22793 && (die->tag == DW_TAG_class_type
22794 || die->tag == DW_TAG_structure_type
22795 || die->tag == DW_TAG_union_type))
22796 {
22797 const char *name = guess_full_die_structure_name (die, cu);
22798 if (name != NULL)
22799 return name;
22800 }
22801 return "";
22802 case DW_TAG_subprogram:
22803 /* Nested subroutines in Fortran get a prefix with the name
22804 of the parent's subroutine. */
22805 if (cu->language == language_fortran)
22806 {
22807 if ((die->tag == DW_TAG_subprogram)
22808 && (dwarf2_name (parent, cu) != NULL))
22809 return dwarf2_name (parent, cu);
22810 }
22811 return determine_prefix (parent, cu);
22812 case DW_TAG_enumeration_type:
22813 parent_type = read_type_die (parent, cu);
22814 if (TYPE_DECLARED_CLASS (parent_type))
22815 {
22816 if (parent_type->name () != NULL)
22817 return parent_type->name ();
22818 return "";
22819 }
22820 /* Fall through. */
22821 default:
22822 return determine_prefix (parent, cu);
22823 }
22824 }
22825
22826 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22827 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22828 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22829 an obconcat, otherwise allocate storage for the result. The CU argument is
22830 used to determine the language and hence, the appropriate separator. */
22831
22832 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22833
22834 static char *
22835 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22836 int physname, struct dwarf2_cu *cu)
22837 {
22838 const char *lead = "";
22839 const char *sep;
22840
22841 if (suffix == NULL || suffix[0] == '\0'
22842 || prefix == NULL || prefix[0] == '\0')
22843 sep = "";
22844 else if (cu->language == language_d)
22845 {
22846 /* For D, the 'main' function could be defined in any module, but it
22847 should never be prefixed. */
22848 if (strcmp (suffix, "D main") == 0)
22849 {
22850 prefix = "";
22851 sep = "";
22852 }
22853 else
22854 sep = ".";
22855 }
22856 else if (cu->language == language_fortran && physname)
22857 {
22858 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22859 DW_AT_MIPS_linkage_name is preferred and used instead. */
22860
22861 lead = "__";
22862 sep = "_MOD_";
22863 }
22864 else
22865 sep = "::";
22866
22867 if (prefix == NULL)
22868 prefix = "";
22869 if (suffix == NULL)
22870 suffix = "";
22871
22872 if (obs == NULL)
22873 {
22874 char *retval
22875 = ((char *)
22876 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22877
22878 strcpy (retval, lead);
22879 strcat (retval, prefix);
22880 strcat (retval, sep);
22881 strcat (retval, suffix);
22882 return retval;
22883 }
22884 else
22885 {
22886 /* We have an obstack. */
22887 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22888 }
22889 }
22890
22891 /* Get name of a die, return NULL if not found. */
22892
22893 static const char *
22894 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22895 struct objfile *objfile)
22896 {
22897 if (name && cu->language == language_cplus)
22898 {
22899 gdb::unique_xmalloc_ptr<char> canon_name
22900 = cp_canonicalize_string (name);
22901
22902 if (canon_name != nullptr)
22903 name = objfile->intern (canon_name.get ());
22904 }
22905
22906 return name;
22907 }
22908
22909 /* Get name of a die, return NULL if not found.
22910 Anonymous namespaces are converted to their magic string. */
22911
22912 static const char *
22913 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22914 {
22915 struct attribute *attr;
22916 struct objfile *objfile = cu->per_objfile->objfile;
22917
22918 attr = dwarf2_attr (die, DW_AT_name, cu);
22919 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
22920 if (attr_name == nullptr
22921 && die->tag != DW_TAG_namespace
22922 && die->tag != DW_TAG_class_type
22923 && die->tag != DW_TAG_interface_type
22924 && die->tag != DW_TAG_structure_type
22925 && die->tag != DW_TAG_union_type)
22926 return NULL;
22927
22928 switch (die->tag)
22929 {
22930 case DW_TAG_compile_unit:
22931 case DW_TAG_partial_unit:
22932 /* Compilation units have a DW_AT_name that is a filename, not
22933 a source language identifier. */
22934 case DW_TAG_enumeration_type:
22935 case DW_TAG_enumerator:
22936 /* These tags always have simple identifiers already; no need
22937 to canonicalize them. */
22938 return attr_name;
22939
22940 case DW_TAG_namespace:
22941 if (attr_name != nullptr)
22942 return attr_name;
22943 return CP_ANONYMOUS_NAMESPACE_STR;
22944
22945 case DW_TAG_class_type:
22946 case DW_TAG_interface_type:
22947 case DW_TAG_structure_type:
22948 case DW_TAG_union_type:
22949 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22950 structures or unions. These were of the form "._%d" in GCC 4.1,
22951 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22952 and GCC 4.4. We work around this problem by ignoring these. */
22953 if (attr_name != nullptr
22954 && (startswith (attr_name, "._")
22955 || startswith (attr_name, "<anonymous")))
22956 return NULL;
22957
22958 /* GCC might emit a nameless typedef that has a linkage name. See
22959 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22960 if (!attr || attr_name == NULL)
22961 {
22962 attr = dw2_linkage_name_attr (die, cu);
22963 attr_name = attr == nullptr ? nullptr : attr->as_string ();
22964 if (attr == NULL || attr_name == NULL)
22965 return NULL;
22966
22967 /* Avoid demangling attr_name the second time on a second
22968 call for the same DIE. */
22969 if (!attr->canonical_string_p ())
22970 {
22971 gdb::unique_xmalloc_ptr<char> demangled
22972 (gdb_demangle (attr_name, DMGL_TYPES));
22973 if (demangled == nullptr)
22974 return nullptr;
22975
22976 attr->set_string_canonical (objfile->intern (demangled.get ()));
22977 attr_name = attr->as_string ();
22978 }
22979
22980 /* Strip any leading namespaces/classes, keep only the
22981 base name. DW_AT_name for named DIEs does not
22982 contain the prefixes. */
22983 const char *base = strrchr (attr_name, ':');
22984 if (base && base > attr_name && base[-1] == ':')
22985 return &base[1];
22986 else
22987 return attr_name;
22988 }
22989 break;
22990
22991 default:
22992 break;
22993 }
22994
22995 if (!attr->canonical_string_p ())
22996 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
22997 objfile));
22998 return attr->as_string ();
22999 }
23000
23001 /* Return the die that this die in an extension of, or NULL if there
23002 is none. *EXT_CU is the CU containing DIE on input, and the CU
23003 containing the return value on output. */
23004
23005 static struct die_info *
23006 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23007 {
23008 struct attribute *attr;
23009
23010 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23011 if (attr == NULL)
23012 return NULL;
23013
23014 return follow_die_ref (die, attr, ext_cu);
23015 }
23016
23017 static void
23018 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23019 {
23020 unsigned int i;
23021
23022 print_spaces (indent, f);
23023 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23024 dwarf_tag_name (die->tag), die->abbrev,
23025 sect_offset_str (die->sect_off));
23026
23027 if (die->parent != NULL)
23028 {
23029 print_spaces (indent, f);
23030 fprintf_unfiltered (f, " parent at offset: %s\n",
23031 sect_offset_str (die->parent->sect_off));
23032 }
23033
23034 print_spaces (indent, f);
23035 fprintf_unfiltered (f, " has children: %s\n",
23036 dwarf_bool_name (die->child != NULL));
23037
23038 print_spaces (indent, f);
23039 fprintf_unfiltered (f, " attributes:\n");
23040
23041 for (i = 0; i < die->num_attrs; ++i)
23042 {
23043 print_spaces (indent, f);
23044 fprintf_unfiltered (f, " %s (%s) ",
23045 dwarf_attr_name (die->attrs[i].name),
23046 dwarf_form_name (die->attrs[i].form));
23047
23048 switch (die->attrs[i].form)
23049 {
23050 case DW_FORM_addr:
23051 case DW_FORM_addrx:
23052 case DW_FORM_GNU_addr_index:
23053 fprintf_unfiltered (f, "address: ");
23054 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
23055 break;
23056 case DW_FORM_block2:
23057 case DW_FORM_block4:
23058 case DW_FORM_block:
23059 case DW_FORM_block1:
23060 fprintf_unfiltered (f, "block: size %s",
23061 pulongest (die->attrs[i].as_block ()->size));
23062 break;
23063 case DW_FORM_exprloc:
23064 fprintf_unfiltered (f, "expression: size %s",
23065 pulongest (die->attrs[i].as_block ()->size));
23066 break;
23067 case DW_FORM_data16:
23068 fprintf_unfiltered (f, "constant of 16 bytes");
23069 break;
23070 case DW_FORM_ref_addr:
23071 fprintf_unfiltered (f, "ref address: ");
23072 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23073 break;
23074 case DW_FORM_GNU_ref_alt:
23075 fprintf_unfiltered (f, "alt ref address: ");
23076 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23077 break;
23078 case DW_FORM_ref1:
23079 case DW_FORM_ref2:
23080 case DW_FORM_ref4:
23081 case DW_FORM_ref8:
23082 case DW_FORM_ref_udata:
23083 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23084 (long) (die->attrs[i].as_unsigned ()));
23085 break;
23086 case DW_FORM_data1:
23087 case DW_FORM_data2:
23088 case DW_FORM_data4:
23089 case DW_FORM_data8:
23090 case DW_FORM_udata:
23091 fprintf_unfiltered (f, "constant: %s",
23092 pulongest (die->attrs[i].as_unsigned ()));
23093 break;
23094 case DW_FORM_sec_offset:
23095 fprintf_unfiltered (f, "section offset: %s",
23096 pulongest (die->attrs[i].as_unsigned ()));
23097 break;
23098 case DW_FORM_ref_sig8:
23099 fprintf_unfiltered (f, "signature: %s",
23100 hex_string (die->attrs[i].as_signature ()));
23101 break;
23102 case DW_FORM_string:
23103 case DW_FORM_strp:
23104 case DW_FORM_line_strp:
23105 case DW_FORM_strx:
23106 case DW_FORM_GNU_str_index:
23107 case DW_FORM_GNU_strp_alt:
23108 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23109 die->attrs[i].as_string ()
23110 ? die->attrs[i].as_string () : "",
23111 die->attrs[i].canonical_string_p () ? "is" : "not");
23112 break;
23113 case DW_FORM_flag:
23114 if (die->attrs[i].as_boolean ())
23115 fprintf_unfiltered (f, "flag: TRUE");
23116 else
23117 fprintf_unfiltered (f, "flag: FALSE");
23118 break;
23119 case DW_FORM_flag_present:
23120 fprintf_unfiltered (f, "flag: TRUE");
23121 break;
23122 case DW_FORM_indirect:
23123 /* The reader will have reduced the indirect form to
23124 the "base form" so this form should not occur. */
23125 fprintf_unfiltered (f,
23126 "unexpected attribute form: DW_FORM_indirect");
23127 break;
23128 case DW_FORM_sdata:
23129 case DW_FORM_implicit_const:
23130 fprintf_unfiltered (f, "constant: %s",
23131 plongest (die->attrs[i].as_signed ()));
23132 break;
23133 default:
23134 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23135 die->attrs[i].form);
23136 break;
23137 }
23138 fprintf_unfiltered (f, "\n");
23139 }
23140 }
23141
23142 static void
23143 dump_die_for_error (struct die_info *die)
23144 {
23145 dump_die_shallow (gdb_stderr, 0, die);
23146 }
23147
23148 static void
23149 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23150 {
23151 int indent = level * 4;
23152
23153 gdb_assert (die != NULL);
23154
23155 if (level >= max_level)
23156 return;
23157
23158 dump_die_shallow (f, indent, die);
23159
23160 if (die->child != NULL)
23161 {
23162 print_spaces (indent, f);
23163 fprintf_unfiltered (f, " Children:");
23164 if (level + 1 < max_level)
23165 {
23166 fprintf_unfiltered (f, "\n");
23167 dump_die_1 (f, level + 1, max_level, die->child);
23168 }
23169 else
23170 {
23171 fprintf_unfiltered (f,
23172 " [not printed, max nesting level reached]\n");
23173 }
23174 }
23175
23176 if (die->sibling != NULL && level > 0)
23177 {
23178 dump_die_1 (f, level, max_level, die->sibling);
23179 }
23180 }
23181
23182 /* This is called from the pdie macro in gdbinit.in.
23183 It's not static so gcc will keep a copy callable from gdb. */
23184
23185 void
23186 dump_die (struct die_info *die, int max_level)
23187 {
23188 dump_die_1 (gdb_stdlog, 0, max_level, die);
23189 }
23190
23191 static void
23192 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23193 {
23194 void **slot;
23195
23196 slot = htab_find_slot_with_hash (cu->die_hash, die,
23197 to_underlying (die->sect_off),
23198 INSERT);
23199
23200 *slot = die;
23201 }
23202
23203 /* Follow reference or signature attribute ATTR of SRC_DIE.
23204 On entry *REF_CU is the CU of SRC_DIE.
23205 On exit *REF_CU is the CU of the result. */
23206
23207 static struct die_info *
23208 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23209 struct dwarf2_cu **ref_cu)
23210 {
23211 struct die_info *die;
23212
23213 if (attr->form_is_ref ())
23214 die = follow_die_ref (src_die, attr, ref_cu);
23215 else if (attr->form == DW_FORM_ref_sig8)
23216 die = follow_die_sig (src_die, attr, ref_cu);
23217 else
23218 {
23219 dump_die_for_error (src_die);
23220 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23221 objfile_name ((*ref_cu)->per_objfile->objfile));
23222 }
23223
23224 return die;
23225 }
23226
23227 /* Follow reference OFFSET.
23228 On entry *REF_CU is the CU of the source die referencing OFFSET.
23229 On exit *REF_CU is the CU of the result.
23230 Returns NULL if OFFSET is invalid. */
23231
23232 static struct die_info *
23233 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23234 struct dwarf2_cu **ref_cu)
23235 {
23236 struct die_info temp_die;
23237 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23238 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23239
23240 gdb_assert (cu->per_cu != NULL);
23241
23242 target_cu = cu;
23243
23244 if (cu->per_cu->is_debug_types)
23245 {
23246 /* .debug_types CUs cannot reference anything outside their CU.
23247 If they need to, they have to reference a signatured type via
23248 DW_FORM_ref_sig8. */
23249 if (!cu->header.offset_in_cu_p (sect_off))
23250 return NULL;
23251 }
23252 else if (offset_in_dwz != cu->per_cu->is_dwz
23253 || !cu->header.offset_in_cu_p (sect_off))
23254 {
23255 struct dwarf2_per_cu_data *per_cu;
23256
23257 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23258 per_objfile);
23259
23260 /* If necessary, add it to the queue and load its DIEs. */
23261 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
23262 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23263 false, cu->language);
23264
23265 target_cu = per_objfile->get_cu (per_cu);
23266 }
23267 else if (cu->dies == NULL)
23268 {
23269 /* We're loading full DIEs during partial symbol reading. */
23270 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23271 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23272 language_minimal);
23273 }
23274
23275 *ref_cu = target_cu;
23276 temp_die.sect_off = sect_off;
23277
23278 if (target_cu != cu)
23279 target_cu->ancestor = cu;
23280
23281 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23282 &temp_die,
23283 to_underlying (sect_off));
23284 }
23285
23286 /* Follow reference attribute ATTR of SRC_DIE.
23287 On entry *REF_CU is the CU of SRC_DIE.
23288 On exit *REF_CU is the CU of the result. */
23289
23290 static struct die_info *
23291 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23292 struct dwarf2_cu **ref_cu)
23293 {
23294 sect_offset sect_off = attr->get_ref_die_offset ();
23295 struct dwarf2_cu *cu = *ref_cu;
23296 struct die_info *die;
23297
23298 die = follow_die_offset (sect_off,
23299 (attr->form == DW_FORM_GNU_ref_alt
23300 || cu->per_cu->is_dwz),
23301 ref_cu);
23302 if (!die)
23303 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23304 "at %s [in module %s]"),
23305 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23306 objfile_name (cu->per_objfile->objfile));
23307
23308 return die;
23309 }
23310
23311 /* See read.h. */
23312
23313 struct dwarf2_locexpr_baton
23314 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23315 dwarf2_per_cu_data *per_cu,
23316 dwarf2_per_objfile *per_objfile,
23317 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23318 bool resolve_abstract_p)
23319 {
23320 struct die_info *die;
23321 struct attribute *attr;
23322 struct dwarf2_locexpr_baton retval;
23323 struct objfile *objfile = per_objfile->objfile;
23324
23325 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23326 if (cu == nullptr)
23327 cu = load_cu (per_cu, per_objfile, false);
23328
23329 if (cu == nullptr)
23330 {
23331 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23332 Instead just throw an error, not much else we can do. */
23333 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23334 sect_offset_str (sect_off), objfile_name (objfile));
23335 }
23336
23337 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23338 if (!die)
23339 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23340 sect_offset_str (sect_off), objfile_name (objfile));
23341
23342 attr = dwarf2_attr (die, DW_AT_location, cu);
23343 if (!attr && resolve_abstract_p
23344 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23345 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23346 {
23347 CORE_ADDR pc = get_frame_pc ();
23348 CORE_ADDR baseaddr = objfile->text_section_offset ();
23349 struct gdbarch *gdbarch = objfile->arch ();
23350
23351 for (const auto &cand_off
23352 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23353 {
23354 struct dwarf2_cu *cand_cu = cu;
23355 struct die_info *cand
23356 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23357 if (!cand
23358 || !cand->parent
23359 || cand->parent->tag != DW_TAG_subprogram)
23360 continue;
23361
23362 CORE_ADDR pc_low, pc_high;
23363 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23364 if (pc_low == ((CORE_ADDR) -1))
23365 continue;
23366 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23367 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23368 if (!(pc_low <= pc && pc < pc_high))
23369 continue;
23370
23371 die = cand;
23372 attr = dwarf2_attr (die, DW_AT_location, cu);
23373 break;
23374 }
23375 }
23376
23377 if (!attr)
23378 {
23379 /* DWARF: "If there is no such attribute, then there is no effect.".
23380 DATA is ignored if SIZE is 0. */
23381
23382 retval.data = NULL;
23383 retval.size = 0;
23384 }
23385 else if (attr->form_is_section_offset ())
23386 {
23387 struct dwarf2_loclist_baton loclist_baton;
23388 CORE_ADDR pc = get_frame_pc ();
23389 size_t size;
23390
23391 fill_in_loclist_baton (cu, &loclist_baton, attr);
23392
23393 retval.data = dwarf2_find_location_expression (&loclist_baton,
23394 &size, pc);
23395 retval.size = size;
23396 }
23397 else
23398 {
23399 if (!attr->form_is_block ())
23400 error (_("Dwarf Error: DIE at %s referenced in module %s "
23401 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23402 sect_offset_str (sect_off), objfile_name (objfile));
23403
23404 struct dwarf_block *block = attr->as_block ();
23405 retval.data = block->data;
23406 retval.size = block->size;
23407 }
23408 retval.per_objfile = per_objfile;
23409 retval.per_cu = cu->per_cu;
23410
23411 per_objfile->age_comp_units ();
23412
23413 return retval;
23414 }
23415
23416 /* See read.h. */
23417
23418 struct dwarf2_locexpr_baton
23419 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23420 dwarf2_per_cu_data *per_cu,
23421 dwarf2_per_objfile *per_objfile,
23422 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23423 {
23424 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23425
23426 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23427 get_frame_pc);
23428 }
23429
23430 /* Write a constant of a given type as target-ordered bytes into
23431 OBSTACK. */
23432
23433 static const gdb_byte *
23434 write_constant_as_bytes (struct obstack *obstack,
23435 enum bfd_endian byte_order,
23436 struct type *type,
23437 ULONGEST value,
23438 LONGEST *len)
23439 {
23440 gdb_byte *result;
23441
23442 *len = TYPE_LENGTH (type);
23443 result = (gdb_byte *) obstack_alloc (obstack, *len);
23444 store_unsigned_integer (result, *len, byte_order, value);
23445
23446 return result;
23447 }
23448
23449 /* See read.h. */
23450
23451 const gdb_byte *
23452 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23453 dwarf2_per_cu_data *per_cu,
23454 dwarf2_per_objfile *per_objfile,
23455 obstack *obstack,
23456 LONGEST *len)
23457 {
23458 struct die_info *die;
23459 struct attribute *attr;
23460 const gdb_byte *result = NULL;
23461 struct type *type;
23462 LONGEST value;
23463 enum bfd_endian byte_order;
23464 struct objfile *objfile = per_objfile->objfile;
23465
23466 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23467 if (cu == nullptr)
23468 cu = load_cu (per_cu, per_objfile, false);
23469
23470 if (cu == nullptr)
23471 {
23472 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23473 Instead just throw an error, not much else we can do. */
23474 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23475 sect_offset_str (sect_off), objfile_name (objfile));
23476 }
23477
23478 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23479 if (!die)
23480 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23481 sect_offset_str (sect_off), objfile_name (objfile));
23482
23483 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23484 if (attr == NULL)
23485 return NULL;
23486
23487 byte_order = (bfd_big_endian (objfile->obfd)
23488 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23489
23490 switch (attr->form)
23491 {
23492 case DW_FORM_addr:
23493 case DW_FORM_addrx:
23494 case DW_FORM_GNU_addr_index:
23495 {
23496 gdb_byte *tem;
23497
23498 *len = cu->header.addr_size;
23499 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23500 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23501 result = tem;
23502 }
23503 break;
23504 case DW_FORM_string:
23505 case DW_FORM_strp:
23506 case DW_FORM_strx:
23507 case DW_FORM_GNU_str_index:
23508 case DW_FORM_GNU_strp_alt:
23509 /* The string is already allocated on the objfile obstack, point
23510 directly to it. */
23511 {
23512 const char *attr_name = attr->as_string ();
23513 result = (const gdb_byte *) attr_name;
23514 *len = strlen (attr_name);
23515 }
23516 break;
23517 case DW_FORM_block1:
23518 case DW_FORM_block2:
23519 case DW_FORM_block4:
23520 case DW_FORM_block:
23521 case DW_FORM_exprloc:
23522 case DW_FORM_data16:
23523 {
23524 struct dwarf_block *block = attr->as_block ();
23525 result = block->data;
23526 *len = block->size;
23527 }
23528 break;
23529
23530 /* The DW_AT_const_value attributes are supposed to carry the
23531 symbol's value "represented as it would be on the target
23532 architecture." By the time we get here, it's already been
23533 converted to host endianness, so we just need to sign- or
23534 zero-extend it as appropriate. */
23535 case DW_FORM_data1:
23536 type = die_type (die, cu);
23537 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23538 if (result == NULL)
23539 result = write_constant_as_bytes (obstack, byte_order,
23540 type, value, len);
23541 break;
23542 case DW_FORM_data2:
23543 type = die_type (die, cu);
23544 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23545 if (result == NULL)
23546 result = write_constant_as_bytes (obstack, byte_order,
23547 type, value, len);
23548 break;
23549 case DW_FORM_data4:
23550 type = die_type (die, cu);
23551 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23552 if (result == NULL)
23553 result = write_constant_as_bytes (obstack, byte_order,
23554 type, value, len);
23555 break;
23556 case DW_FORM_data8:
23557 type = die_type (die, cu);
23558 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23559 if (result == NULL)
23560 result = write_constant_as_bytes (obstack, byte_order,
23561 type, value, len);
23562 break;
23563
23564 case DW_FORM_sdata:
23565 case DW_FORM_implicit_const:
23566 type = die_type (die, cu);
23567 result = write_constant_as_bytes (obstack, byte_order,
23568 type, attr->as_signed (), len);
23569 break;
23570
23571 case DW_FORM_udata:
23572 type = die_type (die, cu);
23573 result = write_constant_as_bytes (obstack, byte_order,
23574 type, attr->as_unsigned (), len);
23575 break;
23576
23577 default:
23578 complaint (_("unsupported const value attribute form: '%s'"),
23579 dwarf_form_name (attr->form));
23580 break;
23581 }
23582
23583 return result;
23584 }
23585
23586 /* See read.h. */
23587
23588 struct type *
23589 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23590 dwarf2_per_cu_data *per_cu,
23591 dwarf2_per_objfile *per_objfile)
23592 {
23593 struct die_info *die;
23594
23595 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23596 if (cu == nullptr)
23597 cu = load_cu (per_cu, per_objfile, false);
23598
23599 if (cu == nullptr)
23600 return nullptr;
23601
23602 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23603 if (!die)
23604 return NULL;
23605
23606 return die_type (die, cu);
23607 }
23608
23609 /* See read.h. */
23610
23611 struct type *
23612 dwarf2_get_die_type (cu_offset die_offset,
23613 dwarf2_per_cu_data *per_cu,
23614 dwarf2_per_objfile *per_objfile)
23615 {
23616 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23617 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
23618 }
23619
23620 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23621 On entry *REF_CU is the CU of SRC_DIE.
23622 On exit *REF_CU is the CU of the result.
23623 Returns NULL if the referenced DIE isn't found. */
23624
23625 static struct die_info *
23626 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23627 struct dwarf2_cu **ref_cu)
23628 {
23629 struct die_info temp_die;
23630 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23631 struct die_info *die;
23632 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
23633
23634
23635 /* While it might be nice to assert sig_type->type == NULL here,
23636 we can get here for DW_AT_imported_declaration where we need
23637 the DIE not the type. */
23638
23639 /* If necessary, add it to the queue and load its DIEs. */
23640
23641 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
23642 language_minimal))
23643 read_signatured_type (sig_type, per_objfile);
23644
23645 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
23646 gdb_assert (sig_cu != NULL);
23647 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23648 temp_die.sect_off = sig_type->type_offset_in_section;
23649 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23650 to_underlying (temp_die.sect_off));
23651 if (die)
23652 {
23653 /* For .gdb_index version 7 keep track of included TUs.
23654 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23655 if (per_objfile->per_bfd->index_table != NULL
23656 && per_objfile->per_bfd->index_table->version <= 7)
23657 {
23658 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23659 }
23660
23661 *ref_cu = sig_cu;
23662 if (sig_cu != cu)
23663 sig_cu->ancestor = cu;
23664
23665 return die;
23666 }
23667
23668 return NULL;
23669 }
23670
23671 /* Follow signatured type referenced by ATTR in SRC_DIE.
23672 On entry *REF_CU is the CU of SRC_DIE.
23673 On exit *REF_CU is the CU of the result.
23674 The result is the DIE of the type.
23675 If the referenced type cannot be found an error is thrown. */
23676
23677 static struct die_info *
23678 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23679 struct dwarf2_cu **ref_cu)
23680 {
23681 ULONGEST signature = attr->as_signature ();
23682 struct signatured_type *sig_type;
23683 struct die_info *die;
23684
23685 gdb_assert (attr->form == DW_FORM_ref_sig8);
23686
23687 sig_type = lookup_signatured_type (*ref_cu, signature);
23688 /* sig_type will be NULL if the signatured type is missing from
23689 the debug info. */
23690 if (sig_type == NULL)
23691 {
23692 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23693 " from DIE at %s [in module %s]"),
23694 hex_string (signature), sect_offset_str (src_die->sect_off),
23695 objfile_name ((*ref_cu)->per_objfile->objfile));
23696 }
23697
23698 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23699 if (die == NULL)
23700 {
23701 dump_die_for_error (src_die);
23702 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23703 " from DIE at %s [in module %s]"),
23704 hex_string (signature), sect_offset_str (src_die->sect_off),
23705 objfile_name ((*ref_cu)->per_objfile->objfile));
23706 }
23707
23708 return die;
23709 }
23710
23711 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23712 reading in and processing the type unit if necessary. */
23713
23714 static struct type *
23715 get_signatured_type (struct die_info *die, ULONGEST signature,
23716 struct dwarf2_cu *cu)
23717 {
23718 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23719 struct signatured_type *sig_type;
23720 struct dwarf2_cu *type_cu;
23721 struct die_info *type_die;
23722 struct type *type;
23723
23724 sig_type = lookup_signatured_type (cu, signature);
23725 /* sig_type will be NULL if the signatured type is missing from
23726 the debug info. */
23727 if (sig_type == NULL)
23728 {
23729 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23730 " from DIE at %s [in module %s]"),
23731 hex_string (signature), sect_offset_str (die->sect_off),
23732 objfile_name (per_objfile->objfile));
23733 return build_error_marker_type (cu, die);
23734 }
23735
23736 /* If we already know the type we're done. */
23737 type = per_objfile->get_type_for_signatured_type (sig_type);
23738 if (type != nullptr)
23739 return type;
23740
23741 type_cu = cu;
23742 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23743 if (type_die != NULL)
23744 {
23745 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23746 is created. This is important, for example, because for c++ classes
23747 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23748 type = read_type_die (type_die, type_cu);
23749 if (type == NULL)
23750 {
23751 complaint (_("Dwarf Error: Cannot build signatured type %s"
23752 " referenced from DIE at %s [in module %s]"),
23753 hex_string (signature), sect_offset_str (die->sect_off),
23754 objfile_name (per_objfile->objfile));
23755 type = build_error_marker_type (cu, die);
23756 }
23757 }
23758 else
23759 {
23760 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23761 " from DIE at %s [in module %s]"),
23762 hex_string (signature), sect_offset_str (die->sect_off),
23763 objfile_name (per_objfile->objfile));
23764 type = build_error_marker_type (cu, die);
23765 }
23766
23767 per_objfile->set_type_for_signatured_type (sig_type, type);
23768
23769 return type;
23770 }
23771
23772 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23773 reading in and processing the type unit if necessary. */
23774
23775 static struct type *
23776 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23777 struct dwarf2_cu *cu) /* ARI: editCase function */
23778 {
23779 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23780 if (attr->form_is_ref ())
23781 {
23782 struct dwarf2_cu *type_cu = cu;
23783 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23784
23785 return read_type_die (type_die, type_cu);
23786 }
23787 else if (attr->form == DW_FORM_ref_sig8)
23788 {
23789 return get_signatured_type (die, attr->as_signature (), cu);
23790 }
23791 else
23792 {
23793 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23794
23795 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23796 " at %s [in module %s]"),
23797 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23798 objfile_name (per_objfile->objfile));
23799 return build_error_marker_type (cu, die);
23800 }
23801 }
23802
23803 /* Load the DIEs associated with type unit PER_CU into memory. */
23804
23805 static void
23806 load_full_type_unit (dwarf2_per_cu_data *per_cu,
23807 dwarf2_per_objfile *per_objfile)
23808 {
23809 struct signatured_type *sig_type;
23810
23811 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23812 gdb_assert (! per_cu->type_unit_group_p ());
23813
23814 /* We have the per_cu, but we need the signatured_type.
23815 Fortunately this is an easy translation. */
23816 gdb_assert (per_cu->is_debug_types);
23817 sig_type = (struct signatured_type *) per_cu;
23818
23819 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
23820
23821 read_signatured_type (sig_type, per_objfile);
23822
23823 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
23824 }
23825
23826 /* Read in a signatured type and build its CU and DIEs.
23827 If the type is a stub for the real type in a DWO file,
23828 read in the real type from the DWO file as well. */
23829
23830 static void
23831 read_signatured_type (signatured_type *sig_type,
23832 dwarf2_per_objfile *per_objfile)
23833 {
23834 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23835
23836 gdb_assert (per_cu->is_debug_types);
23837 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
23838
23839 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
23840
23841 if (!reader.dummy_p)
23842 {
23843 struct dwarf2_cu *cu = reader.cu;
23844 const gdb_byte *info_ptr = reader.info_ptr;
23845
23846 gdb_assert (cu->die_hash == NULL);
23847 cu->die_hash =
23848 htab_create_alloc_ex (cu->header.length / 12,
23849 die_hash,
23850 die_eq,
23851 NULL,
23852 &cu->comp_unit_obstack,
23853 hashtab_obstack_allocate,
23854 dummy_obstack_deallocate);
23855
23856 if (reader.comp_unit_die->has_children)
23857 reader.comp_unit_die->child
23858 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23859 reader.comp_unit_die);
23860 cu->dies = reader.comp_unit_die;
23861 /* comp_unit_die is not stored in die_hash, no need. */
23862
23863 /* We try not to read any attributes in this function, because
23864 not all CUs needed for references have been loaded yet, and
23865 symbol table processing isn't initialized. But we have to
23866 set the CU language, or we won't be able to build types
23867 correctly. Similarly, if we do not read the producer, we can
23868 not apply producer-specific interpretation. */
23869 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23870
23871 reader.keep ();
23872 }
23873
23874 sig_type->per_cu.tu_read = 1;
23875 }
23876
23877 /* Decode simple location descriptions.
23878 Given a pointer to a dwarf block that defines a location, compute
23879 the location and return the value. If COMPUTED is non-null, it is
23880 set to true to indicate that decoding was successful, and false
23881 otherwise. If COMPUTED is null, then this function may emit a
23882 complaint. */
23883
23884 static CORE_ADDR
23885 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
23886 {
23887 struct objfile *objfile = cu->per_objfile->objfile;
23888 size_t i;
23889 size_t size = blk->size;
23890 const gdb_byte *data = blk->data;
23891 CORE_ADDR stack[64];
23892 int stacki;
23893 unsigned int bytes_read, unsnd;
23894 gdb_byte op;
23895
23896 if (computed != nullptr)
23897 *computed = false;
23898
23899 i = 0;
23900 stacki = 0;
23901 stack[stacki] = 0;
23902 stack[++stacki] = 0;
23903
23904 while (i < size)
23905 {
23906 op = data[i++];
23907 switch (op)
23908 {
23909 case DW_OP_lit0:
23910 case DW_OP_lit1:
23911 case DW_OP_lit2:
23912 case DW_OP_lit3:
23913 case DW_OP_lit4:
23914 case DW_OP_lit5:
23915 case DW_OP_lit6:
23916 case DW_OP_lit7:
23917 case DW_OP_lit8:
23918 case DW_OP_lit9:
23919 case DW_OP_lit10:
23920 case DW_OP_lit11:
23921 case DW_OP_lit12:
23922 case DW_OP_lit13:
23923 case DW_OP_lit14:
23924 case DW_OP_lit15:
23925 case DW_OP_lit16:
23926 case DW_OP_lit17:
23927 case DW_OP_lit18:
23928 case DW_OP_lit19:
23929 case DW_OP_lit20:
23930 case DW_OP_lit21:
23931 case DW_OP_lit22:
23932 case DW_OP_lit23:
23933 case DW_OP_lit24:
23934 case DW_OP_lit25:
23935 case DW_OP_lit26:
23936 case DW_OP_lit27:
23937 case DW_OP_lit28:
23938 case DW_OP_lit29:
23939 case DW_OP_lit30:
23940 case DW_OP_lit31:
23941 stack[++stacki] = op - DW_OP_lit0;
23942 break;
23943
23944 case DW_OP_reg0:
23945 case DW_OP_reg1:
23946 case DW_OP_reg2:
23947 case DW_OP_reg3:
23948 case DW_OP_reg4:
23949 case DW_OP_reg5:
23950 case DW_OP_reg6:
23951 case DW_OP_reg7:
23952 case DW_OP_reg8:
23953 case DW_OP_reg9:
23954 case DW_OP_reg10:
23955 case DW_OP_reg11:
23956 case DW_OP_reg12:
23957 case DW_OP_reg13:
23958 case DW_OP_reg14:
23959 case DW_OP_reg15:
23960 case DW_OP_reg16:
23961 case DW_OP_reg17:
23962 case DW_OP_reg18:
23963 case DW_OP_reg19:
23964 case DW_OP_reg20:
23965 case DW_OP_reg21:
23966 case DW_OP_reg22:
23967 case DW_OP_reg23:
23968 case DW_OP_reg24:
23969 case DW_OP_reg25:
23970 case DW_OP_reg26:
23971 case DW_OP_reg27:
23972 case DW_OP_reg28:
23973 case DW_OP_reg29:
23974 case DW_OP_reg30:
23975 case DW_OP_reg31:
23976 stack[++stacki] = op - DW_OP_reg0;
23977 if (i < size)
23978 {
23979 if (computed == nullptr)
23980 dwarf2_complex_location_expr_complaint ();
23981 else
23982 return 0;
23983 }
23984 break;
23985
23986 case DW_OP_regx:
23987 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23988 i += bytes_read;
23989 stack[++stacki] = unsnd;
23990 if (i < size)
23991 {
23992 if (computed == nullptr)
23993 dwarf2_complex_location_expr_complaint ();
23994 else
23995 return 0;
23996 }
23997 break;
23998
23999 case DW_OP_addr:
24000 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24001 &bytes_read);
24002 i += bytes_read;
24003 break;
24004
24005 case DW_OP_const1u:
24006 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24007 i += 1;
24008 break;
24009
24010 case DW_OP_const1s:
24011 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24012 i += 1;
24013 break;
24014
24015 case DW_OP_const2u:
24016 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24017 i += 2;
24018 break;
24019
24020 case DW_OP_const2s:
24021 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24022 i += 2;
24023 break;
24024
24025 case DW_OP_const4u:
24026 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24027 i += 4;
24028 break;
24029
24030 case DW_OP_const4s:
24031 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24032 i += 4;
24033 break;
24034
24035 case DW_OP_const8u:
24036 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24037 i += 8;
24038 break;
24039
24040 case DW_OP_constu:
24041 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24042 &bytes_read);
24043 i += bytes_read;
24044 break;
24045
24046 case DW_OP_consts:
24047 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24048 i += bytes_read;
24049 break;
24050
24051 case DW_OP_dup:
24052 stack[stacki + 1] = stack[stacki];
24053 stacki++;
24054 break;
24055
24056 case DW_OP_plus:
24057 stack[stacki - 1] += stack[stacki];
24058 stacki--;
24059 break;
24060
24061 case DW_OP_plus_uconst:
24062 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24063 &bytes_read);
24064 i += bytes_read;
24065 break;
24066
24067 case DW_OP_minus:
24068 stack[stacki - 1] -= stack[stacki];
24069 stacki--;
24070 break;
24071
24072 case DW_OP_deref:
24073 /* If we're not the last op, then we definitely can't encode
24074 this using GDB's address_class enum. This is valid for partial
24075 global symbols, although the variable's address will be bogus
24076 in the psymtab. */
24077 if (i < size)
24078 {
24079 if (computed == nullptr)
24080 dwarf2_complex_location_expr_complaint ();
24081 else
24082 return 0;
24083 }
24084 break;
24085
24086 case DW_OP_GNU_push_tls_address:
24087 case DW_OP_form_tls_address:
24088 /* The top of the stack has the offset from the beginning
24089 of the thread control block at which the variable is located. */
24090 /* Nothing should follow this operator, so the top of stack would
24091 be returned. */
24092 /* This is valid for partial global symbols, but the variable's
24093 address will be bogus in the psymtab. Make it always at least
24094 non-zero to not look as a variable garbage collected by linker
24095 which have DW_OP_addr 0. */
24096 if (i < size)
24097 {
24098 if (computed == nullptr)
24099 dwarf2_complex_location_expr_complaint ();
24100 else
24101 return 0;
24102 }
24103 stack[stacki]++;
24104 break;
24105
24106 case DW_OP_GNU_uninit:
24107 if (computed != nullptr)
24108 return 0;
24109 break;
24110
24111 case DW_OP_addrx:
24112 case DW_OP_GNU_addr_index:
24113 case DW_OP_GNU_const_index:
24114 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24115 &bytes_read);
24116 i += bytes_read;
24117 break;
24118
24119 default:
24120 if (computed == nullptr)
24121 {
24122 const char *name = get_DW_OP_name (op);
24123
24124 if (name)
24125 complaint (_("unsupported stack op: '%s'"),
24126 name);
24127 else
24128 complaint (_("unsupported stack op: '%02x'"),
24129 op);
24130 }
24131
24132 return (stack[stacki]);
24133 }
24134
24135 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24136 outside of the allocated space. Also enforce minimum>0. */
24137 if (stacki >= ARRAY_SIZE (stack) - 1)
24138 {
24139 if (computed == nullptr)
24140 complaint (_("location description stack overflow"));
24141 return 0;
24142 }
24143
24144 if (stacki <= 0)
24145 {
24146 if (computed == nullptr)
24147 complaint (_("location description stack underflow"));
24148 return 0;
24149 }
24150 }
24151
24152 if (computed != nullptr)
24153 *computed = true;
24154 return (stack[stacki]);
24155 }
24156
24157 /* memory allocation interface */
24158
24159 static struct dwarf_block *
24160 dwarf_alloc_block (struct dwarf2_cu *cu)
24161 {
24162 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24163 }
24164
24165 static struct die_info *
24166 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24167 {
24168 struct die_info *die;
24169 size_t size = sizeof (struct die_info);
24170
24171 if (num_attrs > 1)
24172 size += (num_attrs - 1) * sizeof (struct attribute);
24173
24174 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24175 memset (die, 0, sizeof (struct die_info));
24176 return (die);
24177 }
24178
24179 \f
24180
24181 /* Macro support. */
24182
24183 /* An overload of dwarf_decode_macros that finds the correct section
24184 and ensures it is read in before calling the other overload. */
24185
24186 static void
24187 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24188 int section_is_gnu)
24189 {
24190 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24191 struct objfile *objfile = per_objfile->objfile;
24192 const struct line_header *lh = cu->line_header;
24193 unsigned int offset_size = cu->header.offset_size;
24194 struct dwarf2_section_info *section;
24195 const char *section_name;
24196
24197 if (cu->dwo_unit != nullptr)
24198 {
24199 if (section_is_gnu)
24200 {
24201 section = &cu->dwo_unit->dwo_file->sections.macro;
24202 section_name = ".debug_macro.dwo";
24203 }
24204 else
24205 {
24206 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24207 section_name = ".debug_macinfo.dwo";
24208 }
24209 }
24210 else
24211 {
24212 if (section_is_gnu)
24213 {
24214 section = &per_objfile->per_bfd->macro;
24215 section_name = ".debug_macro";
24216 }
24217 else
24218 {
24219 section = &per_objfile->per_bfd->macinfo;
24220 section_name = ".debug_macinfo";
24221 }
24222 }
24223
24224 section->read (objfile);
24225 if (section->buffer == nullptr)
24226 {
24227 complaint (_("missing %s section"), section_name);
24228 return;
24229 }
24230
24231 buildsym_compunit *builder = cu->get_builder ();
24232
24233 struct dwarf2_section_info *str_offsets_section;
24234 struct dwarf2_section_info *str_section;
24235 ULONGEST str_offsets_base;
24236
24237 if (cu->dwo_unit != nullptr)
24238 {
24239 str_offsets_section = &cu->dwo_unit->dwo_file
24240 ->sections.str_offsets;
24241 str_section = &cu->dwo_unit->dwo_file->sections.str;
24242 str_offsets_base = cu->header.addr_size;
24243 }
24244 else
24245 {
24246 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24247 str_section = &per_objfile->per_bfd->str;
24248 str_offsets_base = *cu->str_offsets_base;
24249 }
24250
24251 dwarf_decode_macros (per_objfile, builder, section, lh,
24252 offset_size, offset, str_section, str_offsets_section,
24253 str_offsets_base, section_is_gnu);
24254 }
24255
24256 /* Return the .debug_loc section to use for CU.
24257 For DWO files use .debug_loc.dwo. */
24258
24259 static struct dwarf2_section_info *
24260 cu_debug_loc_section (struct dwarf2_cu *cu)
24261 {
24262 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24263
24264 if (cu->dwo_unit)
24265 {
24266 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24267
24268 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24269 }
24270 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24271 : &per_objfile->per_bfd->loc);
24272 }
24273
24274 /* Return the .debug_rnglists section to use for CU. */
24275 static struct dwarf2_section_info *
24276 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24277 {
24278 if (cu->header.version < 5)
24279 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24280 cu->header.version);
24281 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24282
24283 /* Make sure we read the .debug_rnglists section from the file that
24284 contains the DW_AT_ranges attribute we are reading. Normally that
24285 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24286 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24287 program. */
24288 if (cu->dwo_unit != nullptr
24289 && tag != DW_TAG_compile_unit
24290 && tag != DW_TAG_skeleton_unit)
24291 {
24292 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24293
24294 if (sections->rnglists.size > 0)
24295 return &sections->rnglists;
24296 else
24297 error (_(".debug_rnglists section is missing from .dwo file."));
24298 }
24299 return &dwarf2_per_objfile->per_bfd->rnglists;
24300 }
24301
24302 /* A helper function that fills in a dwarf2_loclist_baton. */
24303
24304 static void
24305 fill_in_loclist_baton (struct dwarf2_cu *cu,
24306 struct dwarf2_loclist_baton *baton,
24307 const struct attribute *attr)
24308 {
24309 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24310 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24311
24312 section->read (per_objfile->objfile);
24313
24314 baton->per_objfile = per_objfile;
24315 baton->per_cu = cu->per_cu;
24316 gdb_assert (baton->per_cu);
24317 /* We don't know how long the location list is, but make sure we
24318 don't run off the edge of the section. */
24319 baton->size = section->size - attr->as_unsigned ();
24320 baton->data = section->buffer + attr->as_unsigned ();
24321 if (cu->base_address.has_value ())
24322 baton->base_address = *cu->base_address;
24323 else
24324 baton->base_address = 0;
24325 baton->from_dwo = cu->dwo_unit != NULL;
24326 }
24327
24328 static void
24329 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24330 struct dwarf2_cu *cu, int is_block)
24331 {
24332 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24333 struct objfile *objfile = per_objfile->objfile;
24334 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24335
24336 if (attr->form_is_section_offset ()
24337 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24338 the section. If so, fall through to the complaint in the
24339 other branch. */
24340 && attr->as_unsigned () < section->get_size (objfile))
24341 {
24342 struct dwarf2_loclist_baton *baton;
24343
24344 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24345
24346 fill_in_loclist_baton (cu, baton, attr);
24347
24348 if (!cu->base_address.has_value ())
24349 complaint (_("Location list used without "
24350 "specifying the CU base address."));
24351
24352 SYMBOL_ACLASS_INDEX (sym) = (is_block
24353 ? dwarf2_loclist_block_index
24354 : dwarf2_loclist_index);
24355 SYMBOL_LOCATION_BATON (sym) = baton;
24356 }
24357 else
24358 {
24359 struct dwarf2_locexpr_baton *baton;
24360
24361 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24362 baton->per_objfile = per_objfile;
24363 baton->per_cu = cu->per_cu;
24364 gdb_assert (baton->per_cu);
24365
24366 if (attr->form_is_block ())
24367 {
24368 /* Note that we're just copying the block's data pointer
24369 here, not the actual data. We're still pointing into the
24370 info_buffer for SYM's objfile; right now we never release
24371 that buffer, but when we do clean up properly this may
24372 need to change. */
24373 struct dwarf_block *block = attr->as_block ();
24374 baton->size = block->size;
24375 baton->data = block->data;
24376 }
24377 else
24378 {
24379 dwarf2_invalid_attrib_class_complaint ("location description",
24380 sym->natural_name ());
24381 baton->size = 0;
24382 }
24383
24384 SYMBOL_ACLASS_INDEX (sym) = (is_block
24385 ? dwarf2_locexpr_block_index
24386 : dwarf2_locexpr_index);
24387 SYMBOL_LOCATION_BATON (sym) = baton;
24388 }
24389 }
24390
24391 /* See read.h. */
24392
24393 const comp_unit_head *
24394 dwarf2_per_cu_data::get_header () const
24395 {
24396 if (!m_header_read_in)
24397 {
24398 const gdb_byte *info_ptr
24399 = this->section->buffer + to_underlying (this->sect_off);
24400
24401 memset (&m_header, 0, sizeof (m_header));
24402
24403 read_comp_unit_head (&m_header, info_ptr, this->section,
24404 rcuh_kind::COMPILE);
24405 }
24406
24407 return &m_header;
24408 }
24409
24410 /* See read.h. */
24411
24412 int
24413 dwarf2_per_cu_data::addr_size () const
24414 {
24415 return this->get_header ()->addr_size;
24416 }
24417
24418 /* See read.h. */
24419
24420 int
24421 dwarf2_per_cu_data::offset_size () const
24422 {
24423 return this->get_header ()->offset_size;
24424 }
24425
24426 /* See read.h. */
24427
24428 int
24429 dwarf2_per_cu_data::ref_addr_size () const
24430 {
24431 const comp_unit_head *header = this->get_header ();
24432
24433 if (header->version == 2)
24434 return header->addr_size;
24435 else
24436 return header->offset_size;
24437 }
24438
24439 /* See read.h. */
24440
24441 struct type *
24442 dwarf2_cu::addr_type () const
24443 {
24444 struct objfile *objfile = this->per_objfile->objfile;
24445 struct type *void_type = objfile_type (objfile)->builtin_void;
24446 struct type *addr_type = lookup_pointer_type (void_type);
24447 int addr_size = this->per_cu->addr_size ();
24448
24449 if (TYPE_LENGTH (addr_type) == addr_size)
24450 return addr_type;
24451
24452 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
24453 return addr_type;
24454 }
24455
24456 /* A helper function for dwarf2_find_containing_comp_unit that returns
24457 the index of the result, and that searches a vector. It will
24458 return a result even if the offset in question does not actually
24459 occur in any CU. This is separate so that it can be unit
24460 tested. */
24461
24462 static int
24463 dwarf2_find_containing_comp_unit
24464 (sect_offset sect_off,
24465 unsigned int offset_in_dwz,
24466 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
24467 {
24468 int low, high;
24469
24470 low = 0;
24471 high = all_comp_units.size () - 1;
24472 while (high > low)
24473 {
24474 struct dwarf2_per_cu_data *mid_cu;
24475 int mid = low + (high - low) / 2;
24476
24477 mid_cu = all_comp_units[mid];
24478 if (mid_cu->is_dwz > offset_in_dwz
24479 || (mid_cu->is_dwz == offset_in_dwz
24480 && mid_cu->sect_off + mid_cu->length > sect_off))
24481 high = mid;
24482 else
24483 low = mid + 1;
24484 }
24485 gdb_assert (low == high);
24486 return low;
24487 }
24488
24489 /* Locate the .debug_info compilation unit from CU's objfile which contains
24490 the DIE at OFFSET. Raises an error on failure. */
24491
24492 static struct dwarf2_per_cu_data *
24493 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24494 unsigned int offset_in_dwz,
24495 dwarf2_per_objfile *per_objfile)
24496 {
24497 int low = dwarf2_find_containing_comp_unit
24498 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24499 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
24500
24501 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24502 {
24503 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24504 error (_("Dwarf Error: could not find partial DIE containing "
24505 "offset %s [in module %s]"),
24506 sect_offset_str (sect_off),
24507 bfd_get_filename (per_objfile->objfile->obfd));
24508
24509 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
24510 <= sect_off);
24511 return per_objfile->per_bfd->all_comp_units[low-1];
24512 }
24513 else
24514 {
24515 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
24516 && sect_off >= this_cu->sect_off + this_cu->length)
24517 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24518 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24519 return this_cu;
24520 }
24521 }
24522
24523 #if GDB_SELF_TEST
24524
24525 namespace selftests {
24526 namespace find_containing_comp_unit {
24527
24528 static void
24529 run_test ()
24530 {
24531 struct dwarf2_per_cu_data one {};
24532 struct dwarf2_per_cu_data two {};
24533 struct dwarf2_per_cu_data three {};
24534 struct dwarf2_per_cu_data four {};
24535
24536 one.length = 5;
24537 two.sect_off = sect_offset (one.length);
24538 two.length = 7;
24539
24540 three.length = 5;
24541 three.is_dwz = 1;
24542 four.sect_off = sect_offset (three.length);
24543 four.length = 7;
24544 four.is_dwz = 1;
24545
24546 std::vector<dwarf2_per_cu_data *> units;
24547 units.push_back (&one);
24548 units.push_back (&two);
24549 units.push_back (&three);
24550 units.push_back (&four);
24551
24552 int result;
24553
24554 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24555 SELF_CHECK (units[result] == &one);
24556 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24557 SELF_CHECK (units[result] == &one);
24558 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24559 SELF_CHECK (units[result] == &two);
24560
24561 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24562 SELF_CHECK (units[result] == &three);
24563 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24564 SELF_CHECK (units[result] == &three);
24565 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24566 SELF_CHECK (units[result] == &four);
24567 }
24568
24569 }
24570 }
24571
24572 #endif /* GDB_SELF_TEST */
24573
24574 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
24575
24576 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
24577 dwarf2_per_objfile *per_objfile)
24578 : per_cu (per_cu),
24579 per_objfile (per_objfile),
24580 mark (false),
24581 has_loclist (false),
24582 checked_producer (false),
24583 producer_is_gxx_lt_4_6 (false),
24584 producer_is_gcc_lt_4_3 (false),
24585 producer_is_icc (false),
24586 producer_is_icc_lt_14 (false),
24587 producer_is_codewarrior (false),
24588 processing_has_namespace_info (false)
24589 {
24590 }
24591
24592 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24593
24594 static void
24595 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24596 enum language pretend_language)
24597 {
24598 struct attribute *attr;
24599
24600 /* Set the language we're debugging. */
24601 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24602 if (attr != nullptr)
24603 set_cu_language (attr->constant_value (0), cu);
24604 else
24605 {
24606 cu->language = pretend_language;
24607 cu->language_defn = language_def (cu->language);
24608 }
24609
24610 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24611 }
24612
24613 /* See read.h. */
24614
24615 dwarf2_cu *
24616 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
24617 {
24618 auto it = m_dwarf2_cus.find (per_cu);
24619 if (it == m_dwarf2_cus.end ())
24620 return nullptr;
24621
24622 return it->second;
24623 }
24624
24625 /* See read.h. */
24626
24627 void
24628 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24629 {
24630 gdb_assert (this->get_cu (per_cu) == nullptr);
24631
24632 m_dwarf2_cus[per_cu] = cu;
24633 }
24634
24635 /* See read.h. */
24636
24637 void
24638 dwarf2_per_objfile::age_comp_units ()
24639 {
24640 /* Start by clearing all marks. */
24641 for (auto pair : m_dwarf2_cus)
24642 pair.second->mark = false;
24643
24644 /* Traverse all CUs, mark them and their dependencies if used recently
24645 enough. */
24646 for (auto pair : m_dwarf2_cus)
24647 {
24648 dwarf2_cu *cu = pair.second;
24649
24650 cu->last_used++;
24651 if (cu->last_used <= dwarf_max_cache_age)
24652 dwarf2_mark (cu);
24653 }
24654
24655 /* Delete all CUs still not marked. */
24656 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
24657 {
24658 dwarf2_cu *cu = it->second;
24659
24660 if (!cu->mark)
24661 {
24662 delete cu;
24663 it = m_dwarf2_cus.erase (it);
24664 }
24665 else
24666 it++;
24667 }
24668 }
24669
24670 /* See read.h. */
24671
24672 void
24673 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
24674 {
24675 auto it = m_dwarf2_cus.find (per_cu);
24676 if (it == m_dwarf2_cus.end ())
24677 return;
24678
24679 delete it->second;
24680
24681 m_dwarf2_cus.erase (it);
24682 }
24683
24684 dwarf2_per_objfile::~dwarf2_per_objfile ()
24685 {
24686 remove_all_cus ();
24687 }
24688
24689 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24690 We store these in a hash table separate from the DIEs, and preserve them
24691 when the DIEs are flushed out of cache.
24692
24693 The CU "per_cu" pointer is needed because offset alone is not enough to
24694 uniquely identify the type. A file may have multiple .debug_types sections,
24695 or the type may come from a DWO file. Furthermore, while it's more logical
24696 to use per_cu->section+offset, with Fission the section with the data is in
24697 the DWO file but we don't know that section at the point we need it.
24698 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24699 because we can enter the lookup routine, get_die_type_at_offset, from
24700 outside this file, and thus won't necessarily have PER_CU->cu.
24701 Fortunately, PER_CU is stable for the life of the objfile. */
24702
24703 struct dwarf2_per_cu_offset_and_type
24704 {
24705 const struct dwarf2_per_cu_data *per_cu;
24706 sect_offset sect_off;
24707 struct type *type;
24708 };
24709
24710 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24711
24712 static hashval_t
24713 per_cu_offset_and_type_hash (const void *item)
24714 {
24715 const struct dwarf2_per_cu_offset_and_type *ofs
24716 = (const struct dwarf2_per_cu_offset_and_type *) item;
24717
24718 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24719 }
24720
24721 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24722
24723 static int
24724 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24725 {
24726 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24727 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24728 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24729 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24730
24731 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24732 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24733 }
24734
24735 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24736 table if necessary. For convenience, return TYPE.
24737
24738 The DIEs reading must have careful ordering to:
24739 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24740 reading current DIE.
24741 * Not trying to dereference contents of still incompletely read in types
24742 while reading in other DIEs.
24743 * Enable referencing still incompletely read in types just by a pointer to
24744 the type without accessing its fields.
24745
24746 Therefore caller should follow these rules:
24747 * Try to fetch any prerequisite types we may need to build this DIE type
24748 before building the type and calling set_die_type.
24749 * After building type call set_die_type for current DIE as soon as
24750 possible before fetching more types to complete the current type.
24751 * Make the type as complete as possible before fetching more types. */
24752
24753 static struct type *
24754 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
24755 bool skip_data_location)
24756 {
24757 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24758 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24759 struct objfile *objfile = per_objfile->objfile;
24760 struct attribute *attr;
24761 struct dynamic_prop prop;
24762
24763 /* For Ada types, make sure that the gnat-specific data is always
24764 initialized (if not already set). There are a few types where
24765 we should not be doing so, because the type-specific area is
24766 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24767 where the type-specific area is used to store the floatformat).
24768 But this is not a problem, because the gnat-specific information
24769 is actually not needed for these types. */
24770 if (need_gnat_info (cu)
24771 && type->code () != TYPE_CODE_FUNC
24772 && type->code () != TYPE_CODE_FLT
24773 && type->code () != TYPE_CODE_METHODPTR
24774 && type->code () != TYPE_CODE_MEMBERPTR
24775 && type->code () != TYPE_CODE_METHOD
24776 && !HAVE_GNAT_AUX_INFO (type))
24777 INIT_GNAT_SPECIFIC (type);
24778
24779 /* Read DW_AT_allocated and set in type. */
24780 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24781 if (attr != NULL)
24782 {
24783 struct type *prop_type = cu->addr_sized_int_type (false);
24784 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24785 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
24786 }
24787
24788 /* Read DW_AT_associated and set in type. */
24789 attr = dwarf2_attr (die, DW_AT_associated, cu);
24790 if (attr != NULL)
24791 {
24792 struct type *prop_type = cu->addr_sized_int_type (false);
24793 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24794 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
24795 }
24796
24797 /* Read DW_AT_data_location and set in type. */
24798 if (!skip_data_location)
24799 {
24800 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24801 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24802 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24803 }
24804
24805 if (per_objfile->die_type_hash == NULL)
24806 per_objfile->die_type_hash
24807 = htab_up (htab_create_alloc (127,
24808 per_cu_offset_and_type_hash,
24809 per_cu_offset_and_type_eq,
24810 NULL, xcalloc, xfree));
24811
24812 ofs.per_cu = cu->per_cu;
24813 ofs.sect_off = die->sect_off;
24814 ofs.type = type;
24815 slot = (struct dwarf2_per_cu_offset_and_type **)
24816 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
24817 if (*slot)
24818 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24819 sect_offset_str (die->sect_off));
24820 *slot = XOBNEW (&objfile->objfile_obstack,
24821 struct dwarf2_per_cu_offset_and_type);
24822 **slot = ofs;
24823 return type;
24824 }
24825
24826 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24827 or return NULL if the die does not have a saved type. */
24828
24829 static struct type *
24830 get_die_type_at_offset (sect_offset sect_off,
24831 dwarf2_per_cu_data *per_cu,
24832 dwarf2_per_objfile *per_objfile)
24833 {
24834 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24835
24836 if (per_objfile->die_type_hash == NULL)
24837 return NULL;
24838
24839 ofs.per_cu = per_cu;
24840 ofs.sect_off = sect_off;
24841 slot = ((struct dwarf2_per_cu_offset_and_type *)
24842 htab_find (per_objfile->die_type_hash.get (), &ofs));
24843 if (slot)
24844 return slot->type;
24845 else
24846 return NULL;
24847 }
24848
24849 /* Look up the type for DIE in CU in die_type_hash,
24850 or return NULL if DIE does not have a saved type. */
24851
24852 static struct type *
24853 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24854 {
24855 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
24856 }
24857
24858 /* Add a dependence relationship from CU to REF_PER_CU. */
24859
24860 static void
24861 dwarf2_add_dependence (struct dwarf2_cu *cu,
24862 struct dwarf2_per_cu_data *ref_per_cu)
24863 {
24864 void **slot;
24865
24866 if (cu->dependencies == NULL)
24867 cu->dependencies
24868 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24869 NULL, &cu->comp_unit_obstack,
24870 hashtab_obstack_allocate,
24871 dummy_obstack_deallocate);
24872
24873 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24874 if (*slot == NULL)
24875 *slot = ref_per_cu;
24876 }
24877
24878 /* Subroutine of dwarf2_mark to pass to htab_traverse.
24879 Set the mark field in every compilation unit in the
24880 cache that we must keep because we are keeping CU.
24881
24882 DATA is the dwarf2_per_objfile object in which to look up CUs. */
24883
24884 static int
24885 dwarf2_mark_helper (void **slot, void *data)
24886 {
24887 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
24888 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
24889 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
24890
24891 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24892 reading of the chain. As such dependencies remain valid it is not much
24893 useful to track and undo them during QUIT cleanups. */
24894 if (cu == nullptr)
24895 return 1;
24896
24897 if (cu->mark)
24898 return 1;
24899
24900 cu->mark = true;
24901
24902 if (cu->dependencies != nullptr)
24903 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
24904
24905 return 1;
24906 }
24907
24908 /* Set the mark field in CU and in every other compilation unit in the
24909 cache that we must keep because we are keeping CU. */
24910
24911 static void
24912 dwarf2_mark (struct dwarf2_cu *cu)
24913 {
24914 if (cu->mark)
24915 return;
24916
24917 cu->mark = true;
24918
24919 if (cu->dependencies != nullptr)
24920 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
24921 }
24922
24923 /* Trivial hash function for partial_die_info: the hash value of a DIE
24924 is its offset in .debug_info for this objfile. */
24925
24926 static hashval_t
24927 partial_die_hash (const void *item)
24928 {
24929 const struct partial_die_info *part_die
24930 = (const struct partial_die_info *) item;
24931
24932 return to_underlying (part_die->sect_off);
24933 }
24934
24935 /* Trivial comparison function for partial_die_info structures: two DIEs
24936 are equal if they have the same offset. */
24937
24938 static int
24939 partial_die_eq (const void *item_lhs, const void *item_rhs)
24940 {
24941 const struct partial_die_info *part_die_lhs
24942 = (const struct partial_die_info *) item_lhs;
24943 const struct partial_die_info *part_die_rhs
24944 = (const struct partial_die_info *) item_rhs;
24945
24946 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24947 }
24948
24949 struct cmd_list_element *set_dwarf_cmdlist;
24950 struct cmd_list_element *show_dwarf_cmdlist;
24951
24952 static void
24953 show_check_physname (struct ui_file *file, int from_tty,
24954 struct cmd_list_element *c, const char *value)
24955 {
24956 fprintf_filtered (file,
24957 _("Whether to check \"physname\" is %s.\n"),
24958 value);
24959 }
24960
24961 void _initialize_dwarf2_read ();
24962 void
24963 _initialize_dwarf2_read ()
24964 {
24965 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
24966 Set DWARF specific variables.\n\
24967 Configure DWARF variables such as the cache size."),
24968 &set_dwarf_cmdlist, "maintenance set dwarf ",
24969 0/*allow-unknown*/, &maintenance_set_cmdlist);
24970
24971 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
24972 Show DWARF specific variables.\n\
24973 Show DWARF variables such as the cache size."),
24974 &show_dwarf_cmdlist, "maintenance show dwarf ",
24975 0/*allow-unknown*/, &maintenance_show_cmdlist);
24976
24977 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24978 &dwarf_max_cache_age, _("\
24979 Set the upper bound on the age of cached DWARF compilation units."), _("\
24980 Show the upper bound on the age of cached DWARF compilation units."), _("\
24981 A higher limit means that cached compilation units will be stored\n\
24982 in memory longer, and more total memory will be used. Zero disables\n\
24983 caching, which can slow down startup."),
24984 NULL,
24985 show_dwarf_max_cache_age,
24986 &set_dwarf_cmdlist,
24987 &show_dwarf_cmdlist);
24988
24989 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24990 Set debugging of the DWARF reader."), _("\
24991 Show debugging of the DWARF reader."), _("\
24992 When enabled (non-zero), debugging messages are printed during DWARF\n\
24993 reading and symtab expansion. A value of 1 (one) provides basic\n\
24994 information. A value greater than 1 provides more verbose information."),
24995 NULL,
24996 NULL,
24997 &setdebuglist, &showdebuglist);
24998
24999 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25000 Set debugging of the DWARF DIE reader."), _("\
25001 Show debugging of the DWARF DIE reader."), _("\
25002 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25003 The value is the maximum depth to print."),
25004 NULL,
25005 NULL,
25006 &setdebuglist, &showdebuglist);
25007
25008 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25009 Set debugging of the dwarf line reader."), _("\
25010 Show debugging of the dwarf line reader."), _("\
25011 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25012 A value of 1 (one) provides basic information.\n\
25013 A value greater than 1 provides more verbose information."),
25014 NULL,
25015 NULL,
25016 &setdebuglist, &showdebuglist);
25017
25018 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25019 Set cross-checking of \"physname\" code against demangler."), _("\
25020 Show cross-checking of \"physname\" code against demangler."), _("\
25021 When enabled, GDB's internal \"physname\" code is checked against\n\
25022 the demangler."),
25023 NULL, show_check_physname,
25024 &setdebuglist, &showdebuglist);
25025
25026 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25027 no_class, &use_deprecated_index_sections, _("\
25028 Set whether to use deprecated gdb_index sections."), _("\
25029 Show whether to use deprecated gdb_index sections."), _("\
25030 When enabled, deprecated .gdb_index sections are used anyway.\n\
25031 Normally they are ignored either because of a missing feature or\n\
25032 performance issue.\n\
25033 Warning: This option must be enabled before gdb reads the file."),
25034 NULL,
25035 NULL,
25036 &setlist, &showlist);
25037
25038 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25039 &dwarf2_locexpr_funcs);
25040 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25041 &dwarf2_loclist_funcs);
25042
25043 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25044 &dwarf2_block_frame_base_locexpr_funcs);
25045 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25046 &dwarf2_block_frame_base_loclist_funcs);
25047
25048 #if GDB_SELF_TEST
25049 selftests::register_test ("dw2_expand_symtabs_matching",
25050 selftests::dw2_expand_symtabs_matching::run_test);
25051 selftests::register_test ("dwarf2_find_containing_comp_unit",
25052 selftests::find_containing_comp_unit::run_test);
25053 #endif
25054 }
This page took 0.588467 seconds and 5 git commands to generate.