Make queue_and_load_dwo_tu receive a dwarf2_cu
[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 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
109
110 /* The "aclass" indices for various kinds of computed DWARF symbols. */
111
112 static int dwarf2_locexpr_index;
113 static int dwarf2_loclist_index;
114 static int dwarf2_locexpr_block_index;
115 static int dwarf2_loclist_block_index;
116
117 /* Size of .debug_loclists section header for 32-bit DWARF format. */
118 #define LOCLIST_HEADER_SIZE32 12
119
120 /* Size of .debug_loclists section header for 64-bit DWARF format. */
121 #define LOCLIST_HEADER_SIZE64 20
122
123 /* An index into a (C++) symbol name component in a symbol name as
124 recorded in the mapped_index's symbol table. For each C++ symbol
125 in the symbol table, we record one entry for the start of each
126 component in the symbol in a table of name components, and then
127 sort the table, in order to be able to binary search symbol names,
128 ignoring leading namespaces, both completion and regular look up.
129 For example, for symbol "A::B::C", we'll have an entry that points
130 to "A::B::C", another that points to "B::C", and another for "C".
131 Note that function symbols in GDB index have no parameter
132 information, just the function/method names. You can convert a
133 name_component to a "const char *" using the
134 'mapped_index::symbol_name_at(offset_type)' method. */
135
136 struct name_component
137 {
138 /* Offset in the symbol name where the component starts. Stored as
139 a (32-bit) offset instead of a pointer to save memory and improve
140 locality on 64-bit architectures. */
141 offset_type name_offset;
142
143 /* The symbol's index in the symbol and constant pool tables of a
144 mapped_index. */
145 offset_type idx;
146 };
147
148 /* Base class containing bits shared by both .gdb_index and
149 .debug_name indexes. */
150
151 struct mapped_index_base
152 {
153 mapped_index_base () = default;
154 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
155
156 /* The name_component table (a sorted vector). See name_component's
157 description above. */
158 std::vector<name_component> name_components;
159
160 /* How NAME_COMPONENTS is sorted. */
161 enum case_sensitivity name_components_casing;
162
163 /* Return the number of names in the symbol table. */
164 virtual size_t symbol_name_count () const = 0;
165
166 /* Get the name of the symbol at IDX in the symbol table. */
167 virtual const char *symbol_name_at (offset_type idx) const = 0;
168
169 /* Return whether the name at IDX in the symbol table should be
170 ignored. */
171 virtual bool symbol_name_slot_invalid (offset_type idx) const
172 {
173 return false;
174 }
175
176 /* Build the symbol name component sorted vector, if we haven't
177 yet. */
178 void build_name_components ();
179
180 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
181 possible matches for LN_NO_PARAMS in the name component
182 vector. */
183 std::pair<std::vector<name_component>::const_iterator,
184 std::vector<name_component>::const_iterator>
185 find_name_components_bounds (const lookup_name_info &ln_no_params,
186 enum language lang) const;
187
188 /* Prevent deleting/destroying via a base class pointer. */
189 protected:
190 ~mapped_index_base() = default;
191 };
192
193 /* A description of the mapped index. The file format is described in
194 a comment by the code that writes the index. */
195 struct mapped_index final : public mapped_index_base
196 {
197 /* A slot/bucket in the symbol table hash. */
198 struct symbol_table_slot
199 {
200 const offset_type name;
201 const offset_type vec;
202 };
203
204 /* Index data format version. */
205 int version = 0;
206
207 /* The address table data. */
208 gdb::array_view<const gdb_byte> address_table;
209
210 /* The symbol table, implemented as a hash table. */
211 gdb::array_view<symbol_table_slot> symbol_table;
212
213 /* A pointer to the constant pool. */
214 const char *constant_pool = nullptr;
215
216 bool symbol_name_slot_invalid (offset_type idx) const override
217 {
218 const auto &bucket = this->symbol_table[idx];
219 return bucket.name == 0 && bucket.vec == 0;
220 }
221
222 /* Convenience method to get at the name of the symbol at IDX in the
223 symbol table. */
224 const char *symbol_name_at (offset_type idx) const override
225 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
226
227 size_t symbol_name_count () const override
228 { return this->symbol_table.size (); }
229 };
230
231 /* A description of the mapped .debug_names.
232 Uninitialized map has CU_COUNT 0. */
233 struct mapped_debug_names final : public mapped_index_base
234 {
235 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
236 : dwarf2_per_objfile (dwarf2_per_objfile_)
237 {}
238
239 struct dwarf2_per_objfile *dwarf2_per_objfile;
240 bfd_endian dwarf5_byte_order;
241 bool dwarf5_is_dwarf64;
242 bool augmentation_is_gdb;
243 uint8_t offset_size;
244 uint32_t cu_count = 0;
245 uint32_t tu_count, bucket_count, name_count;
246 const gdb_byte *cu_table_reordered, *tu_table_reordered;
247 const uint32_t *bucket_table_reordered, *hash_table_reordered;
248 const gdb_byte *name_table_string_offs_reordered;
249 const gdb_byte *name_table_entry_offs_reordered;
250 const gdb_byte *entry_pool;
251
252 struct index_val
253 {
254 ULONGEST dwarf_tag;
255 struct attr
256 {
257 /* Attribute name DW_IDX_*. */
258 ULONGEST dw_idx;
259
260 /* Attribute form DW_FORM_*. */
261 ULONGEST form;
262
263 /* Value if FORM is DW_FORM_implicit_const. */
264 LONGEST implicit_const;
265 };
266 std::vector<attr> attr_vec;
267 };
268
269 std::unordered_map<ULONGEST, index_val> abbrev_map;
270
271 const char *namei_to_name (uint32_t namei) const;
272
273 /* Implementation of the mapped_index_base virtual interface, for
274 the name_components cache. */
275
276 const char *symbol_name_at (offset_type idx) const override
277 { return namei_to_name (idx); }
278
279 size_t symbol_name_count () const override
280 { return this->name_count; }
281 };
282
283 /* See dwarf2read.h. */
284
285 dwarf2_per_objfile *
286 get_dwarf2_per_objfile (struct objfile *objfile)
287 {
288 return dwarf2_objfile_data_key.get (objfile);
289 }
290
291 /* Default names of the debugging sections. */
292
293 /* Note that if the debugging section has been compressed, it might
294 have a name like .zdebug_info. */
295
296 static const struct dwarf2_debug_sections dwarf2_elf_names =
297 {
298 { ".debug_info", ".zdebug_info" },
299 { ".debug_abbrev", ".zdebug_abbrev" },
300 { ".debug_line", ".zdebug_line" },
301 { ".debug_loc", ".zdebug_loc" },
302 { ".debug_loclists", ".zdebug_loclists" },
303 { ".debug_macinfo", ".zdebug_macinfo" },
304 { ".debug_macro", ".zdebug_macro" },
305 { ".debug_str", ".zdebug_str" },
306 { ".debug_str_offsets", ".zdebug_str_offsets" },
307 { ".debug_line_str", ".zdebug_line_str" },
308 { ".debug_ranges", ".zdebug_ranges" },
309 { ".debug_rnglists", ".zdebug_rnglists" },
310 { ".debug_types", ".zdebug_types" },
311 { ".debug_addr", ".zdebug_addr" },
312 { ".debug_frame", ".zdebug_frame" },
313 { ".eh_frame", NULL },
314 { ".gdb_index", ".zgdb_index" },
315 { ".debug_names", ".zdebug_names" },
316 { ".debug_aranges", ".zdebug_aranges" },
317 23
318 };
319
320 /* List of DWO/DWP sections. */
321
322 static const struct dwop_section_names
323 {
324 struct dwarf2_section_names abbrev_dwo;
325 struct dwarf2_section_names info_dwo;
326 struct dwarf2_section_names line_dwo;
327 struct dwarf2_section_names loc_dwo;
328 struct dwarf2_section_names loclists_dwo;
329 struct dwarf2_section_names macinfo_dwo;
330 struct dwarf2_section_names macro_dwo;
331 struct dwarf2_section_names str_dwo;
332 struct dwarf2_section_names str_offsets_dwo;
333 struct dwarf2_section_names types_dwo;
334 struct dwarf2_section_names cu_index;
335 struct dwarf2_section_names tu_index;
336 }
337 dwop_section_names =
338 {
339 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
340 { ".debug_info.dwo", ".zdebug_info.dwo" },
341 { ".debug_line.dwo", ".zdebug_line.dwo" },
342 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
343 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
344 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
345 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
346 { ".debug_str.dwo", ".zdebug_str.dwo" },
347 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
348 { ".debug_types.dwo", ".zdebug_types.dwo" },
349 { ".debug_cu_index", ".zdebug_cu_index" },
350 { ".debug_tu_index", ".zdebug_tu_index" },
351 };
352
353 /* local data types */
354
355 /* The location list section (.debug_loclists) begins with a header,
356 which contains the following information. */
357 struct loclist_header
358 {
359 /* A 4-byte or 12-byte length containing the length of the
360 set of entries for this compilation unit, not including the
361 length field itself. */
362 unsigned int length;
363
364 /* A 2-byte version identifier. */
365 short version;
366
367 /* A 1-byte unsigned integer containing the size in bytes of an address on
368 the target system. */
369 unsigned char addr_size;
370
371 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
372 on the target system. */
373 unsigned char segment_collector_size;
374
375 /* A 4-byte count of the number of offsets that follow the header. */
376 unsigned int offset_entry_count;
377 };
378
379 /* Type used for delaying computation of method physnames.
380 See comments for compute_delayed_physnames. */
381 struct delayed_method_info
382 {
383 /* The type to which the method is attached, i.e., its parent class. */
384 struct type *type;
385
386 /* The index of the method in the type's function fieldlists. */
387 int fnfield_index;
388
389 /* The index of the method in the fieldlist. */
390 int index;
391
392 /* The name of the DIE. */
393 const char *name;
394
395 /* The DIE associated with this method. */
396 struct die_info *die;
397 };
398
399 /* Internal state when decoding a particular compilation unit. */
400 struct dwarf2_cu
401 {
402 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
403 dwarf2_per_objfile *per_objfile);
404 ~dwarf2_cu ();
405
406 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
407
408 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
409 Create the set of symtabs used by this TU, or if this TU is sharing
410 symtabs with another TU and the symtabs have already been created
411 then restore those symtabs in the line header.
412 We don't need the pc/line-number mapping for type units. */
413 void setup_type_unit_groups (struct die_info *die);
414
415 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
416 buildsym_compunit constructor. */
417 struct compunit_symtab *start_symtab (const char *name,
418 const char *comp_dir,
419 CORE_ADDR low_pc);
420
421 /* Reset the builder. */
422 void reset_builder () { m_builder.reset (); }
423
424 /* The header of the compilation unit. */
425 struct comp_unit_head header {};
426
427 /* Base address of this compilation unit. */
428 gdb::optional<CORE_ADDR> base_address;
429
430 /* The language we are debugging. */
431 enum language language = language_unknown;
432 const struct language_defn *language_defn = nullptr;
433
434 const char *producer = nullptr;
435
436 private:
437 /* The symtab builder for this CU. This is only non-NULL when full
438 symbols are being read. */
439 std::unique_ptr<buildsym_compunit> m_builder;
440
441 public:
442 /* The generic symbol table building routines have separate lists for
443 file scope symbols and all all other scopes (local scopes). So
444 we need to select the right one to pass to add_symbol_to_list().
445 We do it by keeping a pointer to the correct list in list_in_scope.
446
447 FIXME: The original dwarf code just treated the file scope as the
448 first local scope, and all other local scopes as nested local
449 scopes, and worked fine. Check to see if we really need to
450 distinguish these in buildsym.c. */
451 struct pending **list_in_scope = nullptr;
452
453 /* Hash table holding all the loaded partial DIEs
454 with partial_die->offset.SECT_OFF as hash. */
455 htab_t partial_dies = nullptr;
456
457 /* Storage for things with the same lifetime as this read-in compilation
458 unit, including partial DIEs. */
459 auto_obstack comp_unit_obstack;
460
461 /* When multiple dwarf2_cu structures are living in memory, this field
462 chains them all together, so that they can be released efficiently.
463 We will probably also want a generation counter so that most-recently-used
464 compilation units are cached... */
465 struct dwarf2_per_cu_data *read_in_chain = nullptr;
466
467 /* Backlink to our per_cu entry. */
468 struct dwarf2_per_cu_data *per_cu;
469
470 /* The dwarf2_per_objfile that owns this. */
471 struct dwarf2_per_objfile *per_objfile;
472
473 /* How many compilation units ago was this CU last referenced? */
474 int last_used = 0;
475
476 /* A hash table of DIE cu_offset for following references with
477 die_info->offset.sect_off as hash. */
478 htab_t die_hash = nullptr;
479
480 /* Full DIEs if read in. */
481 struct die_info *dies = nullptr;
482
483 /* A set of pointers to dwarf2_per_cu_data objects for compilation
484 units referenced by this one. Only set during full symbol processing;
485 partial symbol tables do not have dependencies. */
486 htab_t dependencies = nullptr;
487
488 /* Header data from the line table, during full symbol processing. */
489 struct line_header *line_header = nullptr;
490 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
491 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
492 this is the DW_TAG_compile_unit die for this CU. We'll hold on
493 to the line header as long as this DIE is being processed. See
494 process_die_scope. */
495 die_info *line_header_die_owner = nullptr;
496
497 /* A list of methods which need to have physnames computed
498 after all type information has been read. */
499 std::vector<delayed_method_info> method_list;
500
501 /* To be copied to symtab->call_site_htab. */
502 htab_t call_site_htab = nullptr;
503
504 /* Non-NULL if this CU came from a DWO file.
505 There is an invariant here that is important to remember:
506 Except for attributes copied from the top level DIE in the "main"
507 (or "stub") file in preparation for reading the DWO file
508 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
509 Either there isn't a DWO file (in which case this is NULL and the point
510 is moot), or there is and either we're not going to read it (in which
511 case this is NULL) or there is and we are reading it (in which case this
512 is non-NULL). */
513 struct dwo_unit *dwo_unit = nullptr;
514
515 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
516 Note this value comes from the Fission stub CU/TU's DIE. */
517 gdb::optional<ULONGEST> addr_base;
518
519 /* The DW_AT_rnglists_base attribute if present.
520 Note this value comes from the Fission stub CU/TU's DIE.
521 Also note that the value is zero in the non-DWO case so this value can
522 be used without needing to know whether DWO files are in use or not.
523 N.B. This does not apply to DW_AT_ranges appearing in
524 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
525 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
526 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
527 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
528 ULONGEST ranges_base = 0;
529
530 /* The DW_AT_loclists_base attribute if present. */
531 ULONGEST loclist_base = 0;
532
533 /* When reading debug info generated by older versions of rustc, we
534 have to rewrite some union types to be struct types with a
535 variant part. This rewriting must be done after the CU is fully
536 read in, because otherwise at the point of rewriting some struct
537 type might not have been fully processed. So, we keep a list of
538 all such types here and process them after expansion. */
539 std::vector<struct type *> rust_unions;
540
541 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
542 files, the value is implicitly zero. For DWARF 5 version DWO files, the
543 value is often implicit and is the size of the header of
544 .debug_str_offsets section (8 or 4, depending on the address size). */
545 gdb::optional<ULONGEST> str_offsets_base;
546
547 /* Mark used when releasing cached dies. */
548 bool mark : 1;
549
550 /* This CU references .debug_loc. See the symtab->locations_valid field.
551 This test is imperfect as there may exist optimized debug code not using
552 any location list and still facing inlining issues if handled as
553 unoptimized code. For a future better test see GCC PR other/32998. */
554 bool has_loclist : 1;
555
556 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
557 if all the producer_is_* fields are valid. This information is cached
558 because profiling CU expansion showed excessive time spent in
559 producer_is_gxx_lt_4_6. */
560 bool checked_producer : 1;
561 bool producer_is_gxx_lt_4_6 : 1;
562 bool producer_is_gcc_lt_4_3 : 1;
563 bool producer_is_icc : 1;
564 bool producer_is_icc_lt_14 : 1;
565 bool producer_is_codewarrior : 1;
566
567 /* When true, the file that we're processing is known to have
568 debugging info for C++ namespaces. GCC 3.3.x did not produce
569 this information, but later versions do. */
570
571 bool processing_has_namespace_info : 1;
572
573 struct partial_die_info *find_partial_die (sect_offset sect_off);
574
575 /* If this CU was inherited by another CU (via specification,
576 abstract_origin, etc), this is the ancestor CU. */
577 dwarf2_cu *ancestor;
578
579 /* Get the buildsym_compunit for this CU. */
580 buildsym_compunit *get_builder ()
581 {
582 /* If this CU has a builder associated with it, use that. */
583 if (m_builder != nullptr)
584 return m_builder.get ();
585
586 /* Otherwise, search ancestors for a valid builder. */
587 if (ancestor != nullptr)
588 return ancestor->get_builder ();
589
590 return nullptr;
591 }
592 };
593
594 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
595 This includes type_unit_group and quick_file_names. */
596
597 struct stmt_list_hash
598 {
599 /* The DWO unit this table is from or NULL if there is none. */
600 struct dwo_unit *dwo_unit;
601
602 /* Offset in .debug_line or .debug_line.dwo. */
603 sect_offset line_sect_off;
604 };
605
606 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
607 an object of this type. */
608
609 struct type_unit_group
610 {
611 /* dwarf2read.c's main "handle" on a TU symtab.
612 To simplify things we create an artificial CU that "includes" all the
613 type units using this stmt_list so that the rest of the code still has
614 a "per_cu" handle on the symtab. */
615 struct dwarf2_per_cu_data per_cu;
616
617 /* The TUs that share this DW_AT_stmt_list entry.
618 This is added to while parsing type units to build partial symtabs,
619 and is deleted afterwards and not used again. */
620 std::vector<signatured_type *> *tus;
621
622 /* The compunit symtab.
623 Type units in a group needn't all be defined in the same source file,
624 so we create an essentially anonymous symtab as the compunit symtab. */
625 struct compunit_symtab *compunit_symtab;
626
627 /* The data used to construct the hash key. */
628 struct stmt_list_hash hash;
629
630 /* The symbol tables for this TU (obtained from the files listed in
631 DW_AT_stmt_list).
632 WARNING: The order of entries here must match the order of entries
633 in the line header. After the first TU using this type_unit_group, the
634 line header for the subsequent TUs is recreated from this. This is done
635 because we need to use the same symtabs for each TU using the same
636 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
637 there's no guarantee the line header doesn't have duplicate entries. */
638 struct symtab **symtabs;
639 };
640
641 /* These sections are what may appear in a (real or virtual) DWO file. */
642
643 struct dwo_sections
644 {
645 struct dwarf2_section_info abbrev;
646 struct dwarf2_section_info line;
647 struct dwarf2_section_info loc;
648 struct dwarf2_section_info loclists;
649 struct dwarf2_section_info macinfo;
650 struct dwarf2_section_info macro;
651 struct dwarf2_section_info str;
652 struct dwarf2_section_info str_offsets;
653 /* In the case of a virtual DWO file, these two are unused. */
654 struct dwarf2_section_info info;
655 std::vector<dwarf2_section_info> types;
656 };
657
658 /* CUs/TUs in DWP/DWO files. */
659
660 struct dwo_unit
661 {
662 /* Backlink to the containing struct dwo_file. */
663 struct dwo_file *dwo_file;
664
665 /* The "id" that distinguishes this CU/TU.
666 .debug_info calls this "dwo_id", .debug_types calls this "signature".
667 Since signatures came first, we stick with it for consistency. */
668 ULONGEST signature;
669
670 /* The section this CU/TU lives in, in the DWO file. */
671 struct dwarf2_section_info *section;
672
673 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
674 sect_offset sect_off;
675 unsigned int length;
676
677 /* For types, offset in the type's DIE of the type defined by this TU. */
678 cu_offset type_offset_in_tu;
679 };
680
681 /* include/dwarf2.h defines the DWP section codes.
682 It defines a max value but it doesn't define a min value, which we
683 use for error checking, so provide one. */
684
685 enum dwp_v2_section_ids
686 {
687 DW_SECT_MIN = 1
688 };
689
690 /* Data for one DWO file.
691
692 This includes virtual DWO files (a virtual DWO file is a DWO file as it
693 appears in a DWP file). DWP files don't really have DWO files per se -
694 comdat folding of types "loses" the DWO file they came from, and from
695 a high level view DWP files appear to contain a mass of random types.
696 However, to maintain consistency with the non-DWP case we pretend DWP
697 files contain virtual DWO files, and we assign each TU with one virtual
698 DWO file (generally based on the line and abbrev section offsets -
699 a heuristic that seems to work in practice). */
700
701 struct dwo_file
702 {
703 dwo_file () = default;
704 DISABLE_COPY_AND_ASSIGN (dwo_file);
705
706 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
707 For virtual DWO files the name is constructed from the section offsets
708 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
709 from related CU+TUs. */
710 const char *dwo_name = nullptr;
711
712 /* The DW_AT_comp_dir attribute. */
713 const char *comp_dir = nullptr;
714
715 /* The bfd, when the file is open. Otherwise this is NULL.
716 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
717 gdb_bfd_ref_ptr dbfd;
718
719 /* The sections that make up this DWO file.
720 Remember that for virtual DWO files in DWP V2, these are virtual
721 sections (for lack of a better name). */
722 struct dwo_sections sections {};
723
724 /* The CUs in the file.
725 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
726 an extension to handle LLVM's Link Time Optimization output (where
727 multiple source files may be compiled into a single object/dwo pair). */
728 htab_up cus;
729
730 /* Table of TUs in the file.
731 Each element is a struct dwo_unit. */
732 htab_up tus;
733 };
734
735 /* These sections are what may appear in a DWP file. */
736
737 struct dwp_sections
738 {
739 /* These are used by both DWP version 1 and 2. */
740 struct dwarf2_section_info str;
741 struct dwarf2_section_info cu_index;
742 struct dwarf2_section_info tu_index;
743
744 /* These are only used by DWP version 2 files.
745 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
746 sections are referenced by section number, and are not recorded here.
747 In DWP version 2 there is at most one copy of all these sections, each
748 section being (effectively) comprised of the concatenation of all of the
749 individual sections that exist in the version 1 format.
750 To keep the code simple we treat each of these concatenated pieces as a
751 section itself (a virtual section?). */
752 struct dwarf2_section_info abbrev;
753 struct dwarf2_section_info info;
754 struct dwarf2_section_info line;
755 struct dwarf2_section_info loc;
756 struct dwarf2_section_info macinfo;
757 struct dwarf2_section_info macro;
758 struct dwarf2_section_info str_offsets;
759 struct dwarf2_section_info types;
760 };
761
762 /* These sections are what may appear in a virtual DWO file in DWP version 1.
763 A virtual DWO file is a DWO file as it appears in a DWP file. */
764
765 struct virtual_v1_dwo_sections
766 {
767 struct dwarf2_section_info abbrev;
768 struct dwarf2_section_info line;
769 struct dwarf2_section_info loc;
770 struct dwarf2_section_info macinfo;
771 struct dwarf2_section_info macro;
772 struct dwarf2_section_info str_offsets;
773 /* Each DWP hash table entry records one CU or one TU.
774 That is recorded here, and copied to dwo_unit.section. */
775 struct dwarf2_section_info info_or_types;
776 };
777
778 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
779 In version 2, the sections of the DWO files are concatenated together
780 and stored in one section of that name. Thus each ELF section contains
781 several "virtual" sections. */
782
783 struct virtual_v2_dwo_sections
784 {
785 bfd_size_type abbrev_offset;
786 bfd_size_type abbrev_size;
787
788 bfd_size_type line_offset;
789 bfd_size_type line_size;
790
791 bfd_size_type loc_offset;
792 bfd_size_type loc_size;
793
794 bfd_size_type macinfo_offset;
795 bfd_size_type macinfo_size;
796
797 bfd_size_type macro_offset;
798 bfd_size_type macro_size;
799
800 bfd_size_type str_offsets_offset;
801 bfd_size_type str_offsets_size;
802
803 /* Each DWP hash table entry records one CU or one TU.
804 That is recorded here, and copied to dwo_unit.section. */
805 bfd_size_type info_or_types_offset;
806 bfd_size_type info_or_types_size;
807 };
808
809 /* Contents of DWP hash tables. */
810
811 struct dwp_hash_table
812 {
813 uint32_t version, nr_columns;
814 uint32_t nr_units, nr_slots;
815 const gdb_byte *hash_table, *unit_table;
816 union
817 {
818 struct
819 {
820 const gdb_byte *indices;
821 } v1;
822 struct
823 {
824 /* This is indexed by column number and gives the id of the section
825 in that column. */
826 #define MAX_NR_V2_DWO_SECTIONS \
827 (1 /* .debug_info or .debug_types */ \
828 + 1 /* .debug_abbrev */ \
829 + 1 /* .debug_line */ \
830 + 1 /* .debug_loc */ \
831 + 1 /* .debug_str_offsets */ \
832 + 1 /* .debug_macro or .debug_macinfo */)
833 int section_ids[MAX_NR_V2_DWO_SECTIONS];
834 const gdb_byte *offsets;
835 const gdb_byte *sizes;
836 } v2;
837 } section_pool;
838 };
839
840 /* Data for one DWP file. */
841
842 struct dwp_file
843 {
844 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
845 : name (name_),
846 dbfd (std::move (abfd))
847 {
848 }
849
850 /* Name of the file. */
851 const char *name;
852
853 /* File format version. */
854 int version = 0;
855
856 /* The bfd. */
857 gdb_bfd_ref_ptr dbfd;
858
859 /* Section info for this file. */
860 struct dwp_sections sections {};
861
862 /* Table of CUs in the file. */
863 const struct dwp_hash_table *cus = nullptr;
864
865 /* Table of TUs in the file. */
866 const struct dwp_hash_table *tus = nullptr;
867
868 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
869 htab_up loaded_cus;
870 htab_up loaded_tus;
871
872 /* Table to map ELF section numbers to their sections.
873 This is only needed for the DWP V1 file format. */
874 unsigned int num_sections = 0;
875 asection **elf_sections = nullptr;
876 };
877
878 /* Struct used to pass misc. parameters to read_die_and_children, et
879 al. which are used for both .debug_info and .debug_types dies.
880 All parameters here are unchanging for the life of the call. This
881 struct exists to abstract away the constant parameters of die reading. */
882
883 struct die_reader_specs
884 {
885 /* The bfd of die_section. */
886 bfd* abfd;
887
888 /* The CU of the DIE we are parsing. */
889 struct dwarf2_cu *cu;
890
891 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
892 struct dwo_file *dwo_file;
893
894 /* The section the die comes from.
895 This is either .debug_info or .debug_types, or the .dwo variants. */
896 struct dwarf2_section_info *die_section;
897
898 /* die_section->buffer. */
899 const gdb_byte *buffer;
900
901 /* The end of the buffer. */
902 const gdb_byte *buffer_end;
903
904 /* The abbreviation table to use when reading the DIEs. */
905 struct abbrev_table *abbrev_table;
906 };
907
908 /* A subclass of die_reader_specs that holds storage and has complex
909 constructor and destructor behavior. */
910
911 class cutu_reader : public die_reader_specs
912 {
913 public:
914
915 cutu_reader (dwarf2_per_cu_data *this_cu,
916 dwarf2_per_objfile *per_objfile,
917 struct abbrev_table *abbrev_table,
918 int use_existing_cu,
919 bool skip_partial);
920
921 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
922 dwarf2_per_objfile *per_objfile,
923 struct dwarf2_cu *parent_cu = nullptr,
924 struct dwo_file *dwo_file = nullptr);
925
926 DISABLE_COPY_AND_ASSIGN (cutu_reader);
927
928 const gdb_byte *info_ptr = nullptr;
929 struct die_info *comp_unit_die = nullptr;
930 bool dummy_p = false;
931
932 /* Release the new CU, putting it on the chain. This cannot be done
933 for dummy CUs. */
934 void keep ();
935
936 private:
937 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
938 dwarf2_per_objfile *per_objfile,
939 int use_existing_cu);
940
941 struct dwarf2_per_cu_data *m_this_cu;
942 std::unique_ptr<dwarf2_cu> m_new_cu;
943
944 /* The ordinary abbreviation table. */
945 abbrev_table_up m_abbrev_table_holder;
946
947 /* The DWO abbreviation table. */
948 abbrev_table_up m_dwo_abbrev_table;
949 };
950
951 /* When we construct a partial symbol table entry we only
952 need this much information. */
953 struct partial_die_info : public allocate_on_obstack
954 {
955 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
956
957 /* Disable assign but still keep copy ctor, which is needed
958 load_partial_dies. */
959 partial_die_info& operator=(const partial_die_info& rhs) = delete;
960
961 /* Adjust the partial die before generating a symbol for it. This
962 function may set the is_external flag or change the DIE's
963 name. */
964 void fixup (struct dwarf2_cu *cu);
965
966 /* Read a minimal amount of information into the minimal die
967 structure. */
968 const gdb_byte *read (const struct die_reader_specs *reader,
969 const struct abbrev_info &abbrev,
970 const gdb_byte *info_ptr);
971
972 /* Offset of this DIE. */
973 const sect_offset sect_off;
974
975 /* DWARF-2 tag for this DIE. */
976 const ENUM_BITFIELD(dwarf_tag) tag : 16;
977
978 /* Assorted flags describing the data found in this DIE. */
979 const unsigned int has_children : 1;
980
981 unsigned int is_external : 1;
982 unsigned int is_declaration : 1;
983 unsigned int has_type : 1;
984 unsigned int has_specification : 1;
985 unsigned int has_pc_info : 1;
986 unsigned int may_be_inlined : 1;
987
988 /* This DIE has been marked DW_AT_main_subprogram. */
989 unsigned int main_subprogram : 1;
990
991 /* Flag set if the SCOPE field of this structure has been
992 computed. */
993 unsigned int scope_set : 1;
994
995 /* Flag set if the DIE has a byte_size attribute. */
996 unsigned int has_byte_size : 1;
997
998 /* Flag set if the DIE has a DW_AT_const_value attribute. */
999 unsigned int has_const_value : 1;
1000
1001 /* Flag set if any of the DIE's children are template arguments. */
1002 unsigned int has_template_arguments : 1;
1003
1004 /* Flag set if fixup has been called on this die. */
1005 unsigned int fixup_called : 1;
1006
1007 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1008 unsigned int is_dwz : 1;
1009
1010 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1011 unsigned int spec_is_dwz : 1;
1012
1013 /* The name of this DIE. Normally the value of DW_AT_name, but
1014 sometimes a default name for unnamed DIEs. */
1015 const char *name = nullptr;
1016
1017 /* The linkage name, if present. */
1018 const char *linkage_name = nullptr;
1019
1020 /* The scope to prepend to our children. This is generally
1021 allocated on the comp_unit_obstack, so will disappear
1022 when this compilation unit leaves the cache. */
1023 const char *scope = nullptr;
1024
1025 /* Some data associated with the partial DIE. The tag determines
1026 which field is live. */
1027 union
1028 {
1029 /* The location description associated with this DIE, if any. */
1030 struct dwarf_block *locdesc;
1031 /* The offset of an import, for DW_TAG_imported_unit. */
1032 sect_offset sect_off;
1033 } d {};
1034
1035 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1036 CORE_ADDR lowpc = 0;
1037 CORE_ADDR highpc = 0;
1038
1039 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1040 DW_AT_sibling, if any. */
1041 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1042 could return DW_AT_sibling values to its caller load_partial_dies. */
1043 const gdb_byte *sibling = nullptr;
1044
1045 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1046 DW_AT_specification (or DW_AT_abstract_origin or
1047 DW_AT_extension). */
1048 sect_offset spec_offset {};
1049
1050 /* Pointers to this DIE's parent, first child, and next sibling,
1051 if any. */
1052 struct partial_die_info *die_parent = nullptr;
1053 struct partial_die_info *die_child = nullptr;
1054 struct partial_die_info *die_sibling = nullptr;
1055
1056 friend struct partial_die_info *
1057 dwarf2_cu::find_partial_die (sect_offset sect_off);
1058
1059 private:
1060 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1061 partial_die_info (sect_offset sect_off)
1062 : partial_die_info (sect_off, DW_TAG_padding, 0)
1063 {
1064 }
1065
1066 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1067 int has_children_)
1068 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1069 {
1070 is_external = 0;
1071 is_declaration = 0;
1072 has_type = 0;
1073 has_specification = 0;
1074 has_pc_info = 0;
1075 may_be_inlined = 0;
1076 main_subprogram = 0;
1077 scope_set = 0;
1078 has_byte_size = 0;
1079 has_const_value = 0;
1080 has_template_arguments = 0;
1081 fixup_called = 0;
1082 is_dwz = 0;
1083 spec_is_dwz = 0;
1084 }
1085 };
1086
1087 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1088 but this would require a corresponding change in unpack_field_as_long
1089 and friends. */
1090 static int bits_per_byte = 8;
1091
1092 struct variant_part_builder;
1093
1094 /* When reading a variant, we track a bit more information about the
1095 field, and store it in an object of this type. */
1096
1097 struct variant_field
1098 {
1099 int first_field = -1;
1100 int last_field = -1;
1101
1102 /* A variant can contain other variant parts. */
1103 std::vector<variant_part_builder> variant_parts;
1104
1105 /* If we see a DW_TAG_variant, then this will be set if this is the
1106 default branch. */
1107 bool default_branch = false;
1108 /* If we see a DW_AT_discr_value, then this will be the discriminant
1109 value. */
1110 ULONGEST discriminant_value = 0;
1111 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1112 data. */
1113 struct dwarf_block *discr_list_data = nullptr;
1114 };
1115
1116 /* This represents a DW_TAG_variant_part. */
1117
1118 struct variant_part_builder
1119 {
1120 /* The offset of the discriminant field. */
1121 sect_offset discriminant_offset {};
1122
1123 /* Variants that are direct children of this variant part. */
1124 std::vector<variant_field> variants;
1125
1126 /* True if we're currently reading a variant. */
1127 bool processing_variant = false;
1128 };
1129
1130 struct nextfield
1131 {
1132 int accessibility = 0;
1133 int virtuality = 0;
1134 /* Variant parts need to find the discriminant, which is a DIE
1135 reference. We track the section offset of each field to make
1136 this link. */
1137 sect_offset offset;
1138 struct field field {};
1139 };
1140
1141 struct fnfieldlist
1142 {
1143 const char *name = nullptr;
1144 std::vector<struct fn_field> fnfields;
1145 };
1146
1147 /* The routines that read and process dies for a C struct or C++ class
1148 pass lists of data member fields and lists of member function fields
1149 in an instance of a field_info structure, as defined below. */
1150 struct field_info
1151 {
1152 /* List of data member and baseclasses fields. */
1153 std::vector<struct nextfield> fields;
1154 std::vector<struct nextfield> baseclasses;
1155
1156 /* Set if the accessibility of one of the fields is not public. */
1157 int non_public_fields = 0;
1158
1159 /* Member function fieldlist array, contains name of possibly overloaded
1160 member function, number of overloaded member functions and a pointer
1161 to the head of the member function field chain. */
1162 std::vector<struct fnfieldlist> fnfieldlists;
1163
1164 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1165 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1166 std::vector<struct decl_field> typedef_field_list;
1167
1168 /* Nested types defined by this class and the number of elements in this
1169 list. */
1170 std::vector<struct decl_field> nested_types_list;
1171
1172 /* If non-null, this is the variant part we are currently
1173 reading. */
1174 variant_part_builder *current_variant_part = nullptr;
1175 /* This holds all the top-level variant parts attached to the type
1176 we're reading. */
1177 std::vector<variant_part_builder> variant_parts;
1178
1179 /* Return the total number of fields (including baseclasses). */
1180 int nfields () const
1181 {
1182 return fields.size () + baseclasses.size ();
1183 }
1184 };
1185
1186 /* Loaded secondary compilation units are kept in memory until they
1187 have not been referenced for the processing of this many
1188 compilation units. Set this to zero to disable caching. Cache
1189 sizes of up to at least twenty will improve startup time for
1190 typical inter-CU-reference binaries, at an obvious memory cost. */
1191 static int dwarf_max_cache_age = 5;
1192 static void
1193 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1194 struct cmd_list_element *c, const char *value)
1195 {
1196 fprintf_filtered (file, _("The upper bound on the age of cached "
1197 "DWARF compilation units is %s.\n"),
1198 value);
1199 }
1200 \f
1201 /* local function prototypes */
1202
1203 static void dwarf2_find_base_address (struct die_info *die,
1204 struct dwarf2_cu *cu);
1205
1206 static dwarf2_psymtab *create_partial_symtab
1207 (struct dwarf2_per_cu_data *per_cu, const char *name);
1208
1209 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1210 const gdb_byte *info_ptr,
1211 struct die_info *type_unit_die);
1212
1213 static void dwarf2_build_psymtabs_hard
1214 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1215
1216 static void scan_partial_symbols (struct partial_die_info *,
1217 CORE_ADDR *, CORE_ADDR *,
1218 int, struct dwarf2_cu *);
1219
1220 static void add_partial_symbol (struct partial_die_info *,
1221 struct dwarf2_cu *);
1222
1223 static void add_partial_namespace (struct partial_die_info *pdi,
1224 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1225 int set_addrmap, struct dwarf2_cu *cu);
1226
1227 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1228 CORE_ADDR *highpc, int set_addrmap,
1229 struct dwarf2_cu *cu);
1230
1231 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1232 struct dwarf2_cu *cu);
1233
1234 static void add_partial_subprogram (struct partial_die_info *pdi,
1235 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1236 int need_pc, struct dwarf2_cu *cu);
1237
1238 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1239
1240 static struct partial_die_info *load_partial_dies
1241 (const struct die_reader_specs *, const gdb_byte *, int);
1242
1243 /* A pair of partial_die_info and compilation unit. */
1244 struct cu_partial_die_info
1245 {
1246 /* The compilation unit of the partial_die_info. */
1247 struct dwarf2_cu *cu;
1248 /* A partial_die_info. */
1249 struct partial_die_info *pdi;
1250
1251 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1252 : cu (cu),
1253 pdi (pdi)
1254 { /* Nothing. */ }
1255
1256 private:
1257 cu_partial_die_info () = delete;
1258 };
1259
1260 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1261 struct dwarf2_cu *);
1262
1263 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1264 struct attribute *, struct attr_abbrev *,
1265 const gdb_byte *, bool *need_reprocess);
1266
1267 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1268 struct attribute *attr);
1269
1270 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1271
1272 static sect_offset read_abbrev_offset
1273 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1274 struct dwarf2_section_info *, sect_offset);
1275
1276 static const char *read_indirect_string
1277 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1278 const struct comp_unit_head *, unsigned int *);
1279
1280 static const char *read_indirect_string_at_offset
1281 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1282
1283 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1284 const gdb_byte *,
1285 unsigned int *);
1286
1287 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1288 ULONGEST str_index);
1289
1290 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1291 ULONGEST str_index);
1292
1293 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1294
1295 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1296 struct dwarf2_cu *);
1297
1298 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1299 struct dwarf2_cu *cu);
1300
1301 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1302
1303 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1304 struct dwarf2_cu *cu);
1305
1306 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1307
1308 static struct die_info *die_specification (struct die_info *die,
1309 struct dwarf2_cu **);
1310
1311 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1312 struct dwarf2_cu *cu);
1313
1314 static void dwarf_decode_lines (struct line_header *, const char *,
1315 struct dwarf2_cu *, dwarf2_psymtab *,
1316 CORE_ADDR, int decode_mapping);
1317
1318 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1319 const char *);
1320
1321 static struct symbol *new_symbol (struct die_info *, struct type *,
1322 struct dwarf2_cu *, struct symbol * = NULL);
1323
1324 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1325 struct dwarf2_cu *);
1326
1327 static void dwarf2_const_value_attr (const struct attribute *attr,
1328 struct type *type,
1329 const char *name,
1330 struct obstack *obstack,
1331 struct dwarf2_cu *cu, LONGEST *value,
1332 const gdb_byte **bytes,
1333 struct dwarf2_locexpr_baton **baton);
1334
1335 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1336
1337 static int need_gnat_info (struct dwarf2_cu *);
1338
1339 static struct type *die_descriptive_type (struct die_info *,
1340 struct dwarf2_cu *);
1341
1342 static void set_descriptive_type (struct type *, struct die_info *,
1343 struct dwarf2_cu *);
1344
1345 static struct type *die_containing_type (struct die_info *,
1346 struct dwarf2_cu *);
1347
1348 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1349 struct dwarf2_cu *);
1350
1351 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1352
1353 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1354
1355 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1356
1357 static char *typename_concat (struct obstack *obs, const char *prefix,
1358 const char *suffix, int physname,
1359 struct dwarf2_cu *cu);
1360
1361 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1362
1363 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1364
1365 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1366
1367 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1368
1369 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1370
1371 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1372
1373 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1374 struct dwarf2_cu *, dwarf2_psymtab *);
1375
1376 /* Return the .debug_loclists section to use for cu. */
1377 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1378
1379 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1380 values. Keep the items ordered with increasing constraints compliance. */
1381 enum pc_bounds_kind
1382 {
1383 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1384 PC_BOUNDS_NOT_PRESENT,
1385
1386 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1387 were present but they do not form a valid range of PC addresses. */
1388 PC_BOUNDS_INVALID,
1389
1390 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1391 PC_BOUNDS_RANGES,
1392
1393 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1394 PC_BOUNDS_HIGH_LOW,
1395 };
1396
1397 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1398 CORE_ADDR *, CORE_ADDR *,
1399 struct dwarf2_cu *,
1400 dwarf2_psymtab *);
1401
1402 static void get_scope_pc_bounds (struct die_info *,
1403 CORE_ADDR *, CORE_ADDR *,
1404 struct dwarf2_cu *);
1405
1406 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1407 CORE_ADDR, struct dwarf2_cu *);
1408
1409 static void dwarf2_add_field (struct field_info *, struct die_info *,
1410 struct dwarf2_cu *);
1411
1412 static void dwarf2_attach_fields_to_type (struct field_info *,
1413 struct type *, struct dwarf2_cu *);
1414
1415 static void dwarf2_add_member_fn (struct field_info *,
1416 struct die_info *, struct type *,
1417 struct dwarf2_cu *);
1418
1419 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1420 struct type *,
1421 struct dwarf2_cu *);
1422
1423 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1424
1425 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1426
1427 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1428
1429 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1430
1431 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1432
1433 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1434
1435 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1436
1437 static struct type *read_module_type (struct die_info *die,
1438 struct dwarf2_cu *cu);
1439
1440 static const char *namespace_name (struct die_info *die,
1441 int *is_anonymous, struct dwarf2_cu *);
1442
1443 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1444
1445 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1446 bool * = nullptr);
1447
1448 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1449 struct dwarf2_cu *);
1450
1451 static struct die_info *read_die_and_siblings_1
1452 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1453 struct die_info *);
1454
1455 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1456 const gdb_byte *info_ptr,
1457 const gdb_byte **new_info_ptr,
1458 struct die_info *parent);
1459
1460 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1461 struct die_info **, const gdb_byte *,
1462 int);
1463
1464 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1465 struct die_info **, const gdb_byte *);
1466
1467 static void process_die (struct die_info *, struct dwarf2_cu *);
1468
1469 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1470 struct objfile *);
1471
1472 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1473
1474 static const char *dwarf2_full_name (const char *name,
1475 struct die_info *die,
1476 struct dwarf2_cu *cu);
1477
1478 static const char *dwarf2_physname (const char *name, struct die_info *die,
1479 struct dwarf2_cu *cu);
1480
1481 static struct die_info *dwarf2_extension (struct die_info *die,
1482 struct dwarf2_cu **);
1483
1484 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1485
1486 static void dump_die_for_error (struct die_info *);
1487
1488 static void dump_die_1 (struct ui_file *, int level, int max_level,
1489 struct die_info *);
1490
1491 /*static*/ void dump_die (struct die_info *, int max_level);
1492
1493 static void store_in_ref_table (struct die_info *,
1494 struct dwarf2_cu *);
1495
1496 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1497 const struct attribute *,
1498 struct dwarf2_cu **);
1499
1500 static struct die_info *follow_die_ref (struct die_info *,
1501 const struct attribute *,
1502 struct dwarf2_cu **);
1503
1504 static struct die_info *follow_die_sig (struct die_info *,
1505 const struct attribute *,
1506 struct dwarf2_cu **);
1507
1508 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1509 struct dwarf2_cu *);
1510
1511 static struct type *get_DW_AT_signature_type (struct die_info *,
1512 const struct attribute *,
1513 struct dwarf2_cu *);
1514
1515 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1516 dwarf2_per_objfile *per_objfile);
1517
1518 static void read_signatured_type (signatured_type *sig_type,
1519 dwarf2_per_objfile *per_objfile);
1520
1521 static int attr_to_dynamic_prop (const struct attribute *attr,
1522 struct die_info *die, struct dwarf2_cu *cu,
1523 struct dynamic_prop *prop, struct type *type);
1524
1525 /* memory allocation interface */
1526
1527 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1528
1529 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1530
1531 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1532
1533 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1534 struct dwarf2_loclist_baton *baton,
1535 const struct attribute *attr);
1536
1537 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1538 struct symbol *sym,
1539 struct dwarf2_cu *cu,
1540 int is_block);
1541
1542 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1543 const gdb_byte *info_ptr,
1544 struct abbrev_info *abbrev);
1545
1546 static hashval_t partial_die_hash (const void *item);
1547
1548 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1549
1550 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1551 (sect_offset sect_off, unsigned int offset_in_dwz,
1552 struct dwarf2_per_objfile *dwarf2_per_objfile);
1553
1554 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1555 struct die_info *comp_unit_die,
1556 enum language pretend_language);
1557
1558 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1559
1560 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1561
1562 static struct type *set_die_type (struct die_info *, struct type *,
1563 struct dwarf2_cu *);
1564
1565 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1566
1567 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1568
1569 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1570 dwarf2_per_objfile *per_objfile,
1571 bool skip_partial,
1572 enum language pretend_language);
1573
1574 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1575 enum language);
1576
1577 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1578 enum language);
1579
1580 static void dwarf2_add_dependence (struct dwarf2_cu *,
1581 struct dwarf2_per_cu_data *);
1582
1583 static void dwarf2_mark (struct dwarf2_cu *);
1584
1585 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1586
1587 static struct type *get_die_type_at_offset (sect_offset,
1588 struct dwarf2_per_cu_data *);
1589
1590 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1591
1592 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1593 enum language pretend_language);
1594
1595 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1596
1597 /* Class, the destructor of which frees all allocated queue entries. This
1598 will only have work to do if an error was thrown while processing the
1599 dwarf. If no error was thrown then the queue entries should have all
1600 been processed, and freed, as we went along. */
1601
1602 class dwarf2_queue_guard
1603 {
1604 public:
1605 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1606 : m_per_objfile (per_objfile)
1607 {
1608 }
1609
1610 /* Free any entries remaining on the queue. There should only be
1611 entries left if we hit an error while processing the dwarf. */
1612 ~dwarf2_queue_guard ()
1613 {
1614 /* Ensure that no memory is allocated by the queue. */
1615 std::queue<dwarf2_queue_item> empty;
1616 std::swap (m_per_objfile->per_bfd->queue, empty);
1617 }
1618
1619 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1620
1621 private:
1622 dwarf2_per_objfile *m_per_objfile;
1623 };
1624
1625 dwarf2_queue_item::~dwarf2_queue_item ()
1626 {
1627 /* Anything still marked queued is likely to be in an
1628 inconsistent state, so discard it. */
1629 if (per_cu->queued)
1630 {
1631 if (per_cu->cu != NULL)
1632 free_one_cached_comp_unit (per_cu);
1633 per_cu->queued = 0;
1634 }
1635 }
1636
1637 /* The return type of find_file_and_directory. Note, the enclosed
1638 string pointers are only valid while this object is valid. */
1639
1640 struct file_and_directory
1641 {
1642 /* The filename. This is never NULL. */
1643 const char *name;
1644
1645 /* The compilation directory. NULL if not known. If we needed to
1646 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1647 points directly to the DW_AT_comp_dir string attribute owned by
1648 the obstack that owns the DIE. */
1649 const char *comp_dir;
1650
1651 /* If we needed to build a new string for comp_dir, this is what
1652 owns the storage. */
1653 std::string comp_dir_storage;
1654 };
1655
1656 static file_and_directory find_file_and_directory (struct die_info *die,
1657 struct dwarf2_cu *cu);
1658
1659 static htab_up allocate_signatured_type_table ();
1660
1661 static htab_up allocate_dwo_unit_table ();
1662
1663 static struct dwo_unit *lookup_dwo_unit_in_dwp
1664 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1665 struct dwp_file *dwp_file, const char *comp_dir,
1666 ULONGEST signature, int is_debug_types);
1667
1668 static struct dwp_file *get_dwp_file
1669 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1670
1671 static struct dwo_unit *lookup_dwo_comp_unit
1672 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1673
1674 static struct dwo_unit *lookup_dwo_type_unit
1675 (struct signatured_type *, const char *, const char *);
1676
1677 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1678
1679 /* A unique pointer to a dwo_file. */
1680
1681 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1682
1683 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1684
1685 static void check_producer (struct dwarf2_cu *cu);
1686
1687 static void free_line_header_voidp (void *arg);
1688 \f
1689 /* Various complaints about symbol reading that don't abort the process. */
1690
1691 static void
1692 dwarf2_debug_line_missing_file_complaint (void)
1693 {
1694 complaint (_(".debug_line section has line data without a file"));
1695 }
1696
1697 static void
1698 dwarf2_debug_line_missing_end_sequence_complaint (void)
1699 {
1700 complaint (_(".debug_line section has line "
1701 "program sequence without an end"));
1702 }
1703
1704 static void
1705 dwarf2_complex_location_expr_complaint (void)
1706 {
1707 complaint (_("location expression too complex"));
1708 }
1709
1710 static void
1711 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1712 int arg3)
1713 {
1714 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1715 arg1, arg2, arg3);
1716 }
1717
1718 static void
1719 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1720 {
1721 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1722 arg1, arg2);
1723 }
1724
1725 /* Hash function for line_header_hash. */
1726
1727 static hashval_t
1728 line_header_hash (const struct line_header *ofs)
1729 {
1730 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1731 }
1732
1733 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1734
1735 static hashval_t
1736 line_header_hash_voidp (const void *item)
1737 {
1738 const struct line_header *ofs = (const struct line_header *) item;
1739
1740 return line_header_hash (ofs);
1741 }
1742
1743 /* Equality function for line_header_hash. */
1744
1745 static int
1746 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1747 {
1748 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1749 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1750
1751 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1752 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1753 }
1754
1755 \f
1756
1757 /* See declaration. */
1758
1759 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1760 bool can_copy_)
1761 : obfd (obfd),
1762 can_copy (can_copy_)
1763 {
1764 if (names == NULL)
1765 names = &dwarf2_elf_names;
1766
1767 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1768 locate_sections (obfd, sec, *names);
1769 }
1770
1771 dwarf2_per_bfd::~dwarf2_per_bfd ()
1772 {
1773 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1774 free_cached_comp_units ();
1775
1776 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1777 per_cu->imported_symtabs_free ();
1778
1779 for (signatured_type *sig_type : all_type_units)
1780 sig_type->per_cu.imported_symtabs_free ();
1781
1782 /* Everything else should be on this->obstack. */
1783 }
1784
1785 /* See declaration. */
1786
1787 void
1788 dwarf2_per_bfd::free_cached_comp_units ()
1789 {
1790 dwarf2_per_cu_data *per_cu = read_in_chain;
1791 dwarf2_per_cu_data **last_chain = &read_in_chain;
1792 while (per_cu != NULL)
1793 {
1794 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1795
1796 delete per_cu->cu;
1797 *last_chain = next_cu;
1798 per_cu = next_cu;
1799 }
1800 }
1801
1802 /* A helper class that calls free_cached_comp_units on
1803 destruction. */
1804
1805 class free_cached_comp_units
1806 {
1807 public:
1808
1809 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1810 : m_per_objfile (per_objfile)
1811 {
1812 }
1813
1814 ~free_cached_comp_units ()
1815 {
1816 m_per_objfile->per_bfd->free_cached_comp_units ();
1817 }
1818
1819 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1820
1821 private:
1822
1823 dwarf2_per_objfile *m_per_objfile;
1824 };
1825
1826 /* See read.h. */
1827
1828 bool
1829 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1830 {
1831 gdb_assert (per_cu->index < this->m_symtabs.size ());
1832
1833 return this->m_symtabs[per_cu->index] != nullptr;
1834 }
1835
1836 /* See read.h. */
1837
1838 compunit_symtab *
1839 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1840 {
1841 gdb_assert (per_cu->index < this->m_symtabs.size ());
1842
1843 return this->m_symtabs[per_cu->index];
1844 }
1845
1846 /* See read.h. */
1847
1848 void
1849 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1850 compunit_symtab *symtab)
1851 {
1852 gdb_assert (per_cu->index < this->m_symtabs.size ());
1853 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1854
1855 this->m_symtabs[per_cu->index] = symtab;
1856 }
1857
1858 /* Try to locate the sections we need for DWARF 2 debugging
1859 information and return true if we have enough to do something.
1860 NAMES points to the dwarf2 section names, or is NULL if the standard
1861 ELF names are used. CAN_COPY is true for formats where symbol
1862 interposition is possible and so symbol values must follow copy
1863 relocation rules. */
1864
1865 int
1866 dwarf2_has_info (struct objfile *objfile,
1867 const struct dwarf2_debug_sections *names,
1868 bool can_copy)
1869 {
1870 if (objfile->flags & OBJF_READNEVER)
1871 return 0;
1872
1873 struct dwarf2_per_objfile *dwarf2_per_objfile
1874 = get_dwarf2_per_objfile (objfile);
1875
1876 if (dwarf2_per_objfile == NULL)
1877 {
1878 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1879 sharing yet). */
1880 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1881
1882 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1883 }
1884
1885 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1886 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1887 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1888 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
1889 }
1890
1891 /* When loading sections, we look either for uncompressed section or for
1892 compressed section names. */
1893
1894 static int
1895 section_is_p (const char *section_name,
1896 const struct dwarf2_section_names *names)
1897 {
1898 if (names->normal != NULL
1899 && strcmp (section_name, names->normal) == 0)
1900 return 1;
1901 if (names->compressed != NULL
1902 && strcmp (section_name, names->compressed) == 0)
1903 return 1;
1904 return 0;
1905 }
1906
1907 /* See declaration. */
1908
1909 void
1910 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1911 const dwarf2_debug_sections &names)
1912 {
1913 flagword aflag = bfd_section_flags (sectp);
1914
1915 if ((aflag & SEC_HAS_CONTENTS) == 0)
1916 {
1917 }
1918 else if (elf_section_data (sectp)->this_hdr.sh_size
1919 > bfd_get_file_size (abfd))
1920 {
1921 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1922 warning (_("Discarding section %s which has a section size (%s"
1923 ") larger than the file size [in module %s]"),
1924 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1925 bfd_get_filename (abfd));
1926 }
1927 else if (section_is_p (sectp->name, &names.info))
1928 {
1929 this->info.s.section = sectp;
1930 this->info.size = bfd_section_size (sectp);
1931 }
1932 else if (section_is_p (sectp->name, &names.abbrev))
1933 {
1934 this->abbrev.s.section = sectp;
1935 this->abbrev.size = bfd_section_size (sectp);
1936 }
1937 else if (section_is_p (sectp->name, &names.line))
1938 {
1939 this->line.s.section = sectp;
1940 this->line.size = bfd_section_size (sectp);
1941 }
1942 else if (section_is_p (sectp->name, &names.loc))
1943 {
1944 this->loc.s.section = sectp;
1945 this->loc.size = bfd_section_size (sectp);
1946 }
1947 else if (section_is_p (sectp->name, &names.loclists))
1948 {
1949 this->loclists.s.section = sectp;
1950 this->loclists.size = bfd_section_size (sectp);
1951 }
1952 else if (section_is_p (sectp->name, &names.macinfo))
1953 {
1954 this->macinfo.s.section = sectp;
1955 this->macinfo.size = bfd_section_size (sectp);
1956 }
1957 else if (section_is_p (sectp->name, &names.macro))
1958 {
1959 this->macro.s.section = sectp;
1960 this->macro.size = bfd_section_size (sectp);
1961 }
1962 else if (section_is_p (sectp->name, &names.str))
1963 {
1964 this->str.s.section = sectp;
1965 this->str.size = bfd_section_size (sectp);
1966 }
1967 else if (section_is_p (sectp->name, &names.str_offsets))
1968 {
1969 this->str_offsets.s.section = sectp;
1970 this->str_offsets.size = bfd_section_size (sectp);
1971 }
1972 else if (section_is_p (sectp->name, &names.line_str))
1973 {
1974 this->line_str.s.section = sectp;
1975 this->line_str.size = bfd_section_size (sectp);
1976 }
1977 else if (section_is_p (sectp->name, &names.addr))
1978 {
1979 this->addr.s.section = sectp;
1980 this->addr.size = bfd_section_size (sectp);
1981 }
1982 else if (section_is_p (sectp->name, &names.frame))
1983 {
1984 this->frame.s.section = sectp;
1985 this->frame.size = bfd_section_size (sectp);
1986 }
1987 else if (section_is_p (sectp->name, &names.eh_frame))
1988 {
1989 this->eh_frame.s.section = sectp;
1990 this->eh_frame.size = bfd_section_size (sectp);
1991 }
1992 else if (section_is_p (sectp->name, &names.ranges))
1993 {
1994 this->ranges.s.section = sectp;
1995 this->ranges.size = bfd_section_size (sectp);
1996 }
1997 else if (section_is_p (sectp->name, &names.rnglists))
1998 {
1999 this->rnglists.s.section = sectp;
2000 this->rnglists.size = bfd_section_size (sectp);
2001 }
2002 else if (section_is_p (sectp->name, &names.types))
2003 {
2004 struct dwarf2_section_info type_section;
2005
2006 memset (&type_section, 0, sizeof (type_section));
2007 type_section.s.section = sectp;
2008 type_section.size = bfd_section_size (sectp);
2009
2010 this->types.push_back (type_section);
2011 }
2012 else if (section_is_p (sectp->name, &names.gdb_index))
2013 {
2014 this->gdb_index.s.section = sectp;
2015 this->gdb_index.size = bfd_section_size (sectp);
2016 }
2017 else if (section_is_p (sectp->name, &names.debug_names))
2018 {
2019 this->debug_names.s.section = sectp;
2020 this->debug_names.size = bfd_section_size (sectp);
2021 }
2022 else if (section_is_p (sectp->name, &names.debug_aranges))
2023 {
2024 this->debug_aranges.s.section = sectp;
2025 this->debug_aranges.size = bfd_section_size (sectp);
2026 }
2027
2028 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2029 && bfd_section_vma (sectp) == 0)
2030 this->has_section_at_zero = true;
2031 }
2032
2033 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2034 SECTION_NAME. */
2035
2036 void
2037 dwarf2_get_section_info (struct objfile *objfile,
2038 enum dwarf2_section_enum sect,
2039 asection **sectp, const gdb_byte **bufp,
2040 bfd_size_type *sizep)
2041 {
2042 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2043 struct dwarf2_section_info *info;
2044
2045 /* We may see an objfile without any DWARF, in which case we just
2046 return nothing. */
2047 if (data == NULL)
2048 {
2049 *sectp = NULL;
2050 *bufp = NULL;
2051 *sizep = 0;
2052 return;
2053 }
2054 switch (sect)
2055 {
2056 case DWARF2_DEBUG_FRAME:
2057 info = &data->per_bfd->frame;
2058 break;
2059 case DWARF2_EH_FRAME:
2060 info = &data->per_bfd->eh_frame;
2061 break;
2062 default:
2063 gdb_assert_not_reached ("unexpected section");
2064 }
2065
2066 info->read (objfile);
2067
2068 *sectp = info->get_bfd_section ();
2069 *bufp = info->buffer;
2070 *sizep = info->size;
2071 }
2072
2073 /* A helper function to find the sections for a .dwz file. */
2074
2075 static void
2076 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2077 {
2078 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2079
2080 /* Note that we only support the standard ELF names, because .dwz
2081 is ELF-only (at the time of writing). */
2082 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2083 {
2084 dwz_file->abbrev.s.section = sectp;
2085 dwz_file->abbrev.size = bfd_section_size (sectp);
2086 }
2087 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2088 {
2089 dwz_file->info.s.section = sectp;
2090 dwz_file->info.size = bfd_section_size (sectp);
2091 }
2092 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2093 {
2094 dwz_file->str.s.section = sectp;
2095 dwz_file->str.size = bfd_section_size (sectp);
2096 }
2097 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2098 {
2099 dwz_file->line.s.section = sectp;
2100 dwz_file->line.size = bfd_section_size (sectp);
2101 }
2102 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2103 {
2104 dwz_file->macro.s.section = sectp;
2105 dwz_file->macro.size = bfd_section_size (sectp);
2106 }
2107 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2108 {
2109 dwz_file->gdb_index.s.section = sectp;
2110 dwz_file->gdb_index.size = bfd_section_size (sectp);
2111 }
2112 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2113 {
2114 dwz_file->debug_names.s.section = sectp;
2115 dwz_file->debug_names.size = bfd_section_size (sectp);
2116 }
2117 }
2118
2119 /* See dwarf2read.h. */
2120
2121 struct dwz_file *
2122 dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
2123 {
2124 const char *filename;
2125 bfd_size_type buildid_len_arg;
2126 size_t buildid_len;
2127 bfd_byte *buildid;
2128
2129 if (per_bfd->dwz_file != NULL)
2130 return per_bfd->dwz_file.get ();
2131
2132 bfd_set_error (bfd_error_no_error);
2133 gdb::unique_xmalloc_ptr<char> data
2134 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2135 &buildid_len_arg, &buildid));
2136 if (data == NULL)
2137 {
2138 if (bfd_get_error () == bfd_error_no_error)
2139 return NULL;
2140 error (_("could not read '.gnu_debugaltlink' section: %s"),
2141 bfd_errmsg (bfd_get_error ()));
2142 }
2143
2144 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2145
2146 buildid_len = (size_t) buildid_len_arg;
2147
2148 filename = data.get ();
2149
2150 std::string abs_storage;
2151 if (!IS_ABSOLUTE_PATH (filename))
2152 {
2153 gdb::unique_xmalloc_ptr<char> abs
2154 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2155
2156 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2157 filename = abs_storage.c_str ();
2158 }
2159
2160 /* First try the file name given in the section. If that doesn't
2161 work, try to use the build-id instead. */
2162 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
2163 if (dwz_bfd != NULL)
2164 {
2165 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2166 dwz_bfd.reset (nullptr);
2167 }
2168
2169 if (dwz_bfd == NULL)
2170 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2171
2172 if (dwz_bfd == nullptr)
2173 {
2174 gdb::unique_xmalloc_ptr<char> alt_filename;
2175 const char *origname = bfd_get_filename (per_bfd->obfd);
2176
2177 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2178 buildid_len,
2179 origname,
2180 &alt_filename));
2181
2182 if (fd.get () >= 0)
2183 {
2184 /* File successfully retrieved from server. */
2185 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2186
2187 if (dwz_bfd == nullptr)
2188 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2189 alt_filename.get ());
2190 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2191 dwz_bfd.reset (nullptr);
2192 }
2193 }
2194
2195 if (dwz_bfd == NULL)
2196 error (_("could not find '.gnu_debugaltlink' file for %s"),
2197 bfd_get_filename (per_bfd->obfd));
2198
2199 std::unique_ptr<struct dwz_file> result
2200 (new struct dwz_file (std::move (dwz_bfd)));
2201
2202 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2203 result.get ());
2204
2205 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2206 per_bfd->dwz_file = std::move (result);
2207 return per_bfd->dwz_file.get ();
2208 }
2209 \f
2210 /* DWARF quick_symbols_functions support. */
2211
2212 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2213 unique line tables, so we maintain a separate table of all .debug_line
2214 derived entries to support the sharing.
2215 All the quick functions need is the list of file names. We discard the
2216 line_header when we're done and don't need to record it here. */
2217 struct quick_file_names
2218 {
2219 /* The data used to construct the hash key. */
2220 struct stmt_list_hash hash;
2221
2222 /* The number of entries in file_names, real_names. */
2223 unsigned int num_file_names;
2224
2225 /* The file names from the line table, after being run through
2226 file_full_name. */
2227 const char **file_names;
2228
2229 /* The file names from the line table after being run through
2230 gdb_realpath. These are computed lazily. */
2231 const char **real_names;
2232 };
2233
2234 /* When using the index (and thus not using psymtabs), each CU has an
2235 object of this type. This is used to hold information needed by
2236 the various "quick" methods. */
2237 struct dwarf2_per_cu_quick_data
2238 {
2239 /* The file table. This can be NULL if there was no file table
2240 or it's currently not read in.
2241 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2242 struct quick_file_names *file_names;
2243
2244 /* A temporary mark bit used when iterating over all CUs in
2245 expand_symtabs_matching. */
2246 unsigned int mark : 1;
2247
2248 /* True if we've tried to read the file table and found there isn't one.
2249 There will be no point in trying to read it again next time. */
2250 unsigned int no_file_data : 1;
2251 };
2252
2253 /* Utility hash function for a stmt_list_hash. */
2254
2255 static hashval_t
2256 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2257 {
2258 hashval_t v = 0;
2259
2260 if (stmt_list_hash->dwo_unit != NULL)
2261 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2262 v += to_underlying (stmt_list_hash->line_sect_off);
2263 return v;
2264 }
2265
2266 /* Utility equality function for a stmt_list_hash. */
2267
2268 static int
2269 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2270 const struct stmt_list_hash *rhs)
2271 {
2272 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2273 return 0;
2274 if (lhs->dwo_unit != NULL
2275 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2276 return 0;
2277
2278 return lhs->line_sect_off == rhs->line_sect_off;
2279 }
2280
2281 /* Hash function for a quick_file_names. */
2282
2283 static hashval_t
2284 hash_file_name_entry (const void *e)
2285 {
2286 const struct quick_file_names *file_data
2287 = (const struct quick_file_names *) e;
2288
2289 return hash_stmt_list_entry (&file_data->hash);
2290 }
2291
2292 /* Equality function for a quick_file_names. */
2293
2294 static int
2295 eq_file_name_entry (const void *a, const void *b)
2296 {
2297 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2298 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2299
2300 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2301 }
2302
2303 /* Delete function for a quick_file_names. */
2304
2305 static void
2306 delete_file_name_entry (void *e)
2307 {
2308 struct quick_file_names *file_data = (struct quick_file_names *) e;
2309 int i;
2310
2311 for (i = 0; i < file_data->num_file_names; ++i)
2312 {
2313 xfree ((void*) file_data->file_names[i]);
2314 if (file_data->real_names)
2315 xfree ((void*) file_data->real_names[i]);
2316 }
2317
2318 /* The space for the struct itself lives on the obstack, so we don't
2319 free it here. */
2320 }
2321
2322 /* Create a quick_file_names hash table. */
2323
2324 static htab_up
2325 create_quick_file_names_table (unsigned int nr_initial_entries)
2326 {
2327 return htab_up (htab_create_alloc (nr_initial_entries,
2328 hash_file_name_entry, eq_file_name_entry,
2329 delete_file_name_entry, xcalloc, xfree));
2330 }
2331
2332 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2333 function is unrelated to symtabs, symtab would have to be created afterwards.
2334 You should call age_cached_comp_units after processing the CU. */
2335
2336 static void
2337 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2338 bool skip_partial)
2339 {
2340 if (per_cu->is_debug_types)
2341 load_full_type_unit (per_cu, per_objfile);
2342 else
2343 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
2344
2345 if (per_cu->cu == NULL)
2346 return; /* Dummy CU. */
2347
2348 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2349 }
2350
2351 /* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2352
2353 static void
2354 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2355 dwarf2_per_objfile *dwarf2_per_objfile,
2356 bool skip_partial)
2357 {
2358 /* Skip type_unit_groups, reading the type units they contain
2359 is handled elsewhere. */
2360 if (per_cu->type_unit_group_p ())
2361 return;
2362
2363 /* The destructor of dwarf2_queue_guard frees any entries left on
2364 the queue. After this point we're guaranteed to leave this function
2365 with the dwarf queue empty. */
2366 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2367
2368 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2369 {
2370 queue_comp_unit (per_cu, language_minimal);
2371 load_cu (per_cu, dwarf2_per_objfile, skip_partial);
2372
2373 /* If we just loaded a CU from a DWO, and we're working with an index
2374 that may badly handle TUs, load all the TUs in that DWO as well.
2375 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2376 if (!per_cu->is_debug_types
2377 && per_cu->cu != NULL
2378 && per_cu->cu->dwo_unit != NULL
2379 && dwarf2_per_objfile->per_bfd->index_table != NULL
2380 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
2381 /* DWP files aren't supported yet. */
2382 && get_dwp_file (dwarf2_per_objfile) == NULL)
2383 queue_and_load_all_dwo_tus (per_cu);
2384 }
2385
2386 process_queue (dwarf2_per_objfile);
2387
2388 /* Age the cache, releasing compilation units that have not
2389 been used recently. */
2390 age_cached_comp_units (dwarf2_per_objfile);
2391 }
2392
2393 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2394 the per-objfile for which this symtab is instantiated.
2395
2396 Returns the resulting symbol table. */
2397
2398 static struct compunit_symtab *
2399 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2400 dwarf2_per_objfile *dwarf2_per_objfile,
2401 bool skip_partial)
2402 {
2403 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
2404
2405 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2406 {
2407 free_cached_comp_units freer (dwarf2_per_objfile);
2408 scoped_restore decrementer = increment_reading_symtab ();
2409 dw2_do_instantiate_symtab (per_cu, dwarf2_per_objfile, skip_partial);
2410 process_cu_includes (dwarf2_per_objfile);
2411 }
2412
2413 return dwarf2_per_objfile->get_symtab (per_cu);
2414 }
2415
2416 /* See declaration. */
2417
2418 dwarf2_per_cu_data *
2419 dwarf2_per_bfd::get_cutu (int index)
2420 {
2421 if (index >= this->all_comp_units.size ())
2422 {
2423 index -= this->all_comp_units.size ();
2424 gdb_assert (index < this->all_type_units.size ());
2425 return &this->all_type_units[index]->per_cu;
2426 }
2427
2428 return this->all_comp_units[index];
2429 }
2430
2431 /* See declaration. */
2432
2433 dwarf2_per_cu_data *
2434 dwarf2_per_bfd::get_cu (int index)
2435 {
2436 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2437
2438 return this->all_comp_units[index];
2439 }
2440
2441 /* See declaration. */
2442
2443 signatured_type *
2444 dwarf2_per_bfd::get_tu (int index)
2445 {
2446 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2447
2448 return this->all_type_units[index];
2449 }
2450
2451 /* See read.h. */
2452
2453 dwarf2_per_cu_data *
2454 dwarf2_per_bfd::allocate_per_cu ()
2455 {
2456 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2457 result->per_bfd = this;
2458 result->index = m_num_psymtabs++;
2459 return result;
2460 }
2461
2462 /* See read.h. */
2463
2464 signatured_type *
2465 dwarf2_per_bfd::allocate_signatured_type ()
2466 {
2467 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2468 result->per_cu.per_bfd = this;
2469 result->per_cu.index = m_num_psymtabs++;
2470 return result;
2471 }
2472
2473 /* Return a new dwarf2_per_cu_data allocated on the dwarf2_per_objfile
2474 obstack, and constructed with the specified field values. */
2475
2476 static dwarf2_per_cu_data *
2477 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2478 struct dwarf2_section_info *section,
2479 int is_dwz,
2480 sect_offset sect_off, ULONGEST length)
2481 {
2482 dwarf2_per_cu_data *the_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
2483 the_cu->sect_off = sect_off;
2484 the_cu->length = length;
2485 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2486 the_cu->section = section;
2487 the_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2488 struct dwarf2_per_cu_quick_data);
2489 the_cu->is_dwz = is_dwz;
2490 return the_cu;
2491 }
2492
2493 /* A helper for create_cus_from_index that handles a given list of
2494 CUs. */
2495
2496 static void
2497 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2498 const gdb_byte *cu_list, offset_type n_elements,
2499 struct dwarf2_section_info *section,
2500 int is_dwz)
2501 {
2502 for (offset_type i = 0; i < n_elements; i += 2)
2503 {
2504 gdb_static_assert (sizeof (ULONGEST) >= 8);
2505
2506 sect_offset sect_off
2507 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2508 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2509 cu_list += 2 * 8;
2510
2511 dwarf2_per_cu_data *per_cu
2512 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2513 sect_off, length);
2514 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
2515 }
2516 }
2517
2518 /* Read the CU list from the mapped index, and use it to create all
2519 the CU objects for this objfile. */
2520
2521 static void
2522 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2523 const gdb_byte *cu_list, offset_type cu_list_elements,
2524 const gdb_byte *dwz_list, offset_type dwz_elements)
2525 {
2526 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
2527 dwarf2_per_objfile->per_bfd->all_comp_units.reserve
2528 ((cu_list_elements + dwz_elements) / 2);
2529
2530 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2531 &dwarf2_per_objfile->per_bfd->info, 0);
2532
2533 if (dwz_elements == 0)
2534 return;
2535
2536 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
2537 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2538 &dwz->info, 1);
2539 }
2540
2541 /* Create the signatured type hash table from the index. */
2542
2543 static void
2544 create_signatured_type_table_from_index
2545 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2546 struct dwarf2_section_info *section,
2547 const gdb_byte *bytes,
2548 offset_type elements)
2549 {
2550 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2551 dwarf2_per_objfile->per_bfd->all_type_units.reserve (elements / 3);
2552
2553 htab_up sig_types_hash = allocate_signatured_type_table ();
2554
2555 for (offset_type i = 0; i < elements; i += 3)
2556 {
2557 struct signatured_type *sig_type;
2558 ULONGEST signature;
2559 void **slot;
2560 cu_offset type_offset_in_tu;
2561
2562 gdb_static_assert (sizeof (ULONGEST) >= 8);
2563 sect_offset sect_off
2564 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2565 type_offset_in_tu
2566 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2567 BFD_ENDIAN_LITTLE);
2568 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2569 bytes += 3 * 8;
2570
2571 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2572 sig_type->signature = signature;
2573 sig_type->type_offset_in_tu = type_offset_in_tu;
2574 sig_type->per_cu.is_debug_types = 1;
2575 sig_type->per_cu.section = section;
2576 sig_type->per_cu.sect_off = sect_off;
2577 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2578 sig_type->per_cu.v.quick
2579 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2580 struct dwarf2_per_cu_quick_data);
2581
2582 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2583 *slot = sig_type;
2584
2585 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2586 }
2587
2588 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2589 }
2590
2591 /* Create the signatured type hash table from .debug_names. */
2592
2593 static void
2594 create_signatured_type_table_from_debug_names
2595 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2596 const mapped_debug_names &map,
2597 struct dwarf2_section_info *section,
2598 struct dwarf2_section_info *abbrev_section)
2599 {
2600 struct objfile *objfile = dwarf2_per_objfile->objfile;
2601
2602 section->read (objfile);
2603 abbrev_section->read (objfile);
2604
2605 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2606 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2607
2608 htab_up sig_types_hash = allocate_signatured_type_table ();
2609
2610 for (uint32_t i = 0; i < map.tu_count; ++i)
2611 {
2612 struct signatured_type *sig_type;
2613 void **slot;
2614
2615 sect_offset sect_off
2616 = (sect_offset) (extract_unsigned_integer
2617 (map.tu_table_reordered + i * map.offset_size,
2618 map.offset_size,
2619 map.dwarf5_byte_order));
2620
2621 comp_unit_head cu_header;
2622 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2623 abbrev_section,
2624 section->buffer + to_underlying (sect_off),
2625 rcuh_kind::TYPE);
2626
2627 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2628 sig_type->signature = cu_header.signature;
2629 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2630 sig_type->per_cu.is_debug_types = 1;
2631 sig_type->per_cu.section = section;
2632 sig_type->per_cu.sect_off = sect_off;
2633 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2634 sig_type->per_cu.v.quick
2635 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2636 struct dwarf2_per_cu_quick_data);
2637
2638 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2639 *slot = sig_type;
2640
2641 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2642 }
2643
2644 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2645 }
2646
2647 /* Read the address map data from the mapped index, and use it to
2648 populate the objfile's psymtabs_addrmap. */
2649
2650 static void
2651 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2652 struct mapped_index *index)
2653 {
2654 struct objfile *objfile = dwarf2_per_objfile->objfile;
2655 struct gdbarch *gdbarch = objfile->arch ();
2656 const gdb_byte *iter, *end;
2657 struct addrmap *mutable_map;
2658 CORE_ADDR baseaddr;
2659
2660 auto_obstack temp_obstack;
2661
2662 mutable_map = addrmap_create_mutable (&temp_obstack);
2663
2664 iter = index->address_table.data ();
2665 end = iter + index->address_table.size ();
2666
2667 baseaddr = objfile->text_section_offset ();
2668
2669 while (iter < end)
2670 {
2671 ULONGEST hi, lo, cu_index;
2672 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2673 iter += 8;
2674 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2675 iter += 8;
2676 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2677 iter += 4;
2678
2679 if (lo > hi)
2680 {
2681 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2682 hex_string (lo), hex_string (hi));
2683 continue;
2684 }
2685
2686 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
2687 {
2688 complaint (_(".gdb_index address table has invalid CU number %u"),
2689 (unsigned) cu_index);
2690 continue;
2691 }
2692
2693 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2694 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2695 addrmap_set_empty (mutable_map, lo, hi - 1,
2696 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
2697 }
2698
2699 objfile->partial_symtabs->psymtabs_addrmap
2700 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2701 }
2702
2703 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2704 populate the objfile's psymtabs_addrmap. */
2705
2706 static void
2707 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2708 struct dwarf2_section_info *section)
2709 {
2710 struct objfile *objfile = dwarf2_per_objfile->objfile;
2711 bfd *abfd = objfile->obfd;
2712 struct gdbarch *gdbarch = objfile->arch ();
2713 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2714
2715 auto_obstack temp_obstack;
2716 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2717
2718 std::unordered_map<sect_offset,
2719 dwarf2_per_cu_data *,
2720 gdb::hash_enum<sect_offset>>
2721 debug_info_offset_to_per_cu;
2722 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
2723 {
2724 const auto insertpair
2725 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2726 if (!insertpair.second)
2727 {
2728 warning (_("Section .debug_aranges in %s has duplicate "
2729 "debug_info_offset %s, ignoring .debug_aranges."),
2730 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2731 return;
2732 }
2733 }
2734
2735 section->read (objfile);
2736
2737 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2738
2739 const gdb_byte *addr = section->buffer;
2740
2741 while (addr < section->buffer + section->size)
2742 {
2743 const gdb_byte *const entry_addr = addr;
2744 unsigned int bytes_read;
2745
2746 const LONGEST entry_length = read_initial_length (abfd, addr,
2747 &bytes_read);
2748 addr += bytes_read;
2749
2750 const gdb_byte *const entry_end = addr + entry_length;
2751 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2752 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2753 if (addr + entry_length > section->buffer + section->size)
2754 {
2755 warning (_("Section .debug_aranges in %s entry at offset %s "
2756 "length %s exceeds section length %s, "
2757 "ignoring .debug_aranges."),
2758 objfile_name (objfile),
2759 plongest (entry_addr - section->buffer),
2760 plongest (bytes_read + entry_length),
2761 pulongest (section->size));
2762 return;
2763 }
2764
2765 /* The version number. */
2766 const uint16_t version = read_2_bytes (abfd, addr);
2767 addr += 2;
2768 if (version != 2)
2769 {
2770 warning (_("Section .debug_aranges in %s entry at offset %s "
2771 "has unsupported version %d, ignoring .debug_aranges."),
2772 objfile_name (objfile),
2773 plongest (entry_addr - section->buffer), version);
2774 return;
2775 }
2776
2777 const uint64_t debug_info_offset
2778 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2779 addr += offset_size;
2780 const auto per_cu_it
2781 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2782 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2783 {
2784 warning (_("Section .debug_aranges in %s entry at offset %s "
2785 "debug_info_offset %s does not exists, "
2786 "ignoring .debug_aranges."),
2787 objfile_name (objfile),
2788 plongest (entry_addr - section->buffer),
2789 pulongest (debug_info_offset));
2790 return;
2791 }
2792 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2793
2794 const uint8_t address_size = *addr++;
2795 if (address_size < 1 || address_size > 8)
2796 {
2797 warning (_("Section .debug_aranges in %s entry at offset %s "
2798 "address_size %u is invalid, ignoring .debug_aranges."),
2799 objfile_name (objfile),
2800 plongest (entry_addr - section->buffer), address_size);
2801 return;
2802 }
2803
2804 const uint8_t segment_selector_size = *addr++;
2805 if (segment_selector_size != 0)
2806 {
2807 warning (_("Section .debug_aranges in %s entry at offset %s "
2808 "segment_selector_size %u is not supported, "
2809 "ignoring .debug_aranges."),
2810 objfile_name (objfile),
2811 plongest (entry_addr - section->buffer),
2812 segment_selector_size);
2813 return;
2814 }
2815
2816 /* Must pad to an alignment boundary that is twice the address
2817 size. It is undocumented by the DWARF standard but GCC does
2818 use it. */
2819 for (size_t padding = ((-(addr - section->buffer))
2820 & (2 * address_size - 1));
2821 padding > 0; padding--)
2822 if (*addr++ != 0)
2823 {
2824 warning (_("Section .debug_aranges in %s entry at offset %s "
2825 "padding is not zero, ignoring .debug_aranges."),
2826 objfile_name (objfile),
2827 plongest (entry_addr - section->buffer));
2828 return;
2829 }
2830
2831 for (;;)
2832 {
2833 if (addr + 2 * address_size > entry_end)
2834 {
2835 warning (_("Section .debug_aranges in %s entry at offset %s "
2836 "address list is not properly terminated, "
2837 "ignoring .debug_aranges."),
2838 objfile_name (objfile),
2839 plongest (entry_addr - section->buffer));
2840 return;
2841 }
2842 ULONGEST start = extract_unsigned_integer (addr, address_size,
2843 dwarf5_byte_order);
2844 addr += address_size;
2845 ULONGEST length = extract_unsigned_integer (addr, address_size,
2846 dwarf5_byte_order);
2847 addr += address_size;
2848 if (start == 0 && length == 0)
2849 break;
2850 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
2851 {
2852 /* Symbol was eliminated due to a COMDAT group. */
2853 continue;
2854 }
2855 ULONGEST end = start + length;
2856 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2857 - baseaddr);
2858 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2859 - baseaddr);
2860 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2861 }
2862 }
2863
2864 objfile->partial_symtabs->psymtabs_addrmap
2865 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2866 }
2867
2868 /* Find a slot in the mapped index INDEX for the object named NAME.
2869 If NAME is found, set *VEC_OUT to point to the CU vector in the
2870 constant pool and return true. If NAME cannot be found, return
2871 false. */
2872
2873 static bool
2874 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2875 offset_type **vec_out)
2876 {
2877 offset_type hash;
2878 offset_type slot, step;
2879 int (*cmp) (const char *, const char *);
2880
2881 gdb::unique_xmalloc_ptr<char> without_params;
2882 if (current_language->la_language == language_cplus
2883 || current_language->la_language == language_fortran
2884 || current_language->la_language == language_d)
2885 {
2886 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2887 not contain any. */
2888
2889 if (strchr (name, '(') != NULL)
2890 {
2891 without_params = cp_remove_params (name);
2892
2893 if (without_params != NULL)
2894 name = without_params.get ();
2895 }
2896 }
2897
2898 /* Index version 4 did not support case insensitive searches. But the
2899 indices for case insensitive languages are built in lowercase, therefore
2900 simulate our NAME being searched is also lowercased. */
2901 hash = mapped_index_string_hash ((index->version == 4
2902 && case_sensitivity == case_sensitive_off
2903 ? 5 : index->version),
2904 name);
2905
2906 slot = hash & (index->symbol_table.size () - 1);
2907 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2908 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2909
2910 for (;;)
2911 {
2912 const char *str;
2913
2914 const auto &bucket = index->symbol_table[slot];
2915 if (bucket.name == 0 && bucket.vec == 0)
2916 return false;
2917
2918 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2919 if (!cmp (name, str))
2920 {
2921 *vec_out = (offset_type *) (index->constant_pool
2922 + MAYBE_SWAP (bucket.vec));
2923 return true;
2924 }
2925
2926 slot = (slot + step) & (index->symbol_table.size () - 1);
2927 }
2928 }
2929
2930 /* A helper function that reads the .gdb_index from BUFFER and fills
2931 in MAP. FILENAME is the name of the file containing the data;
2932 it is used for error reporting. DEPRECATED_OK is true if it is
2933 ok to use deprecated sections.
2934
2935 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2936 out parameters that are filled in with information about the CU and
2937 TU lists in the section.
2938
2939 Returns true if all went well, false otherwise. */
2940
2941 static bool
2942 read_gdb_index_from_buffer (const char *filename,
2943 bool deprecated_ok,
2944 gdb::array_view<const gdb_byte> buffer,
2945 struct mapped_index *map,
2946 const gdb_byte **cu_list,
2947 offset_type *cu_list_elements,
2948 const gdb_byte **types_list,
2949 offset_type *types_list_elements)
2950 {
2951 const gdb_byte *addr = &buffer[0];
2952
2953 /* Version check. */
2954 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2955 /* Versions earlier than 3 emitted every copy of a psymbol. This
2956 causes the index to behave very poorly for certain requests. Version 3
2957 contained incomplete addrmap. So, it seems better to just ignore such
2958 indices. */
2959 if (version < 4)
2960 {
2961 static int warning_printed = 0;
2962 if (!warning_printed)
2963 {
2964 warning (_("Skipping obsolete .gdb_index section in %s."),
2965 filename);
2966 warning_printed = 1;
2967 }
2968 return 0;
2969 }
2970 /* Index version 4 uses a different hash function than index version
2971 5 and later.
2972
2973 Versions earlier than 6 did not emit psymbols for inlined
2974 functions. Using these files will cause GDB not to be able to
2975 set breakpoints on inlined functions by name, so we ignore these
2976 indices unless the user has done
2977 "set use-deprecated-index-sections on". */
2978 if (version < 6 && !deprecated_ok)
2979 {
2980 static int warning_printed = 0;
2981 if (!warning_printed)
2982 {
2983 warning (_("\
2984 Skipping deprecated .gdb_index section in %s.\n\
2985 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2986 to use the section anyway."),
2987 filename);
2988 warning_printed = 1;
2989 }
2990 return 0;
2991 }
2992 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2993 of the TU (for symbols coming from TUs),
2994 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2995 Plus gold-generated indices can have duplicate entries for global symbols,
2996 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2997 These are just performance bugs, and we can't distinguish gdb-generated
2998 indices from gold-generated ones, so issue no warning here. */
2999
3000 /* Indexes with higher version than the one supported by GDB may be no
3001 longer backward compatible. */
3002 if (version > 8)
3003 return 0;
3004
3005 map->version = version;
3006
3007 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3008
3009 int i = 0;
3010 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3011 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3012 / 8);
3013 ++i;
3014
3015 *types_list = addr + MAYBE_SWAP (metadata[i]);
3016 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3017 - MAYBE_SWAP (metadata[i]))
3018 / 8);
3019 ++i;
3020
3021 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3022 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3023 map->address_table
3024 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3025 ++i;
3026
3027 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3028 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3029 map->symbol_table
3030 = gdb::array_view<mapped_index::symbol_table_slot>
3031 ((mapped_index::symbol_table_slot *) symbol_table,
3032 (mapped_index::symbol_table_slot *) symbol_table_end);
3033
3034 ++i;
3035 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3036
3037 return 1;
3038 }
3039
3040 /* Callback types for dwarf2_read_gdb_index. */
3041
3042 typedef gdb::function_view
3043 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3044 get_gdb_index_contents_ftype;
3045 typedef gdb::function_view
3046 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3047 get_gdb_index_contents_dwz_ftype;
3048
3049 /* Read .gdb_index. If everything went ok, initialize the "quick"
3050 elements of all the CUs and return 1. Otherwise, return 0. */
3051
3052 static int
3053 dwarf2_read_gdb_index
3054 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3055 get_gdb_index_contents_ftype get_gdb_index_contents,
3056 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3057 {
3058 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3059 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3060 struct dwz_file *dwz;
3061 struct objfile *objfile = dwarf2_per_objfile->objfile;
3062
3063 gdb::array_view<const gdb_byte> main_index_contents
3064 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
3065
3066 if (main_index_contents.empty ())
3067 return 0;
3068
3069 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3070 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3071 use_deprecated_index_sections,
3072 main_index_contents, map.get (), &cu_list,
3073 &cu_list_elements, &types_list,
3074 &types_list_elements))
3075 return 0;
3076
3077 /* Don't use the index if it's empty. */
3078 if (map->symbol_table.empty ())
3079 return 0;
3080
3081 /* If there is a .dwz file, read it so we can get its CU list as
3082 well. */
3083 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
3084 if (dwz != NULL)
3085 {
3086 struct mapped_index dwz_map;
3087 const gdb_byte *dwz_types_ignore;
3088 offset_type dwz_types_elements_ignore;
3089
3090 gdb::array_view<const gdb_byte> dwz_index_content
3091 = get_gdb_index_contents_dwz (objfile, dwz);
3092
3093 if (dwz_index_content.empty ())
3094 return 0;
3095
3096 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3097 1, dwz_index_content, &dwz_map,
3098 &dwz_list, &dwz_list_elements,
3099 &dwz_types_ignore,
3100 &dwz_types_elements_ignore))
3101 {
3102 warning (_("could not read '.gdb_index' section from %s; skipping"),
3103 bfd_get_filename (dwz->dwz_bfd.get ()));
3104 return 0;
3105 }
3106 }
3107
3108 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3109 dwz_list, dwz_list_elements);
3110
3111 if (types_list_elements)
3112 {
3113 /* We can only handle a single .debug_types when we have an
3114 index. */
3115 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
3116 return 0;
3117
3118 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
3119
3120 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3121 types_list, types_list_elements);
3122 }
3123
3124 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3125
3126 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3127 dwarf2_per_objfile->per_bfd->using_index = 1;
3128 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3129 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
3130
3131 return 1;
3132 }
3133
3134 /* die_reader_func for dw2_get_file_names. */
3135
3136 static void
3137 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3138 const gdb_byte *info_ptr,
3139 struct die_info *comp_unit_die)
3140 {
3141 struct dwarf2_cu *cu = reader->cu;
3142 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3143 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3144 struct dwarf2_per_cu_data *lh_cu;
3145 struct attribute *attr;
3146 void **slot;
3147 struct quick_file_names *qfn;
3148
3149 gdb_assert (! this_cu->is_debug_types);
3150
3151 /* Our callers never want to match partial units -- instead they
3152 will match the enclosing full CU. */
3153 if (comp_unit_die->tag == DW_TAG_partial_unit)
3154 {
3155 this_cu->v.quick->no_file_data = 1;
3156 return;
3157 }
3158
3159 lh_cu = this_cu;
3160 slot = NULL;
3161
3162 line_header_up lh;
3163 sect_offset line_offset {};
3164
3165 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3166 if (attr != nullptr)
3167 {
3168 struct quick_file_names find_entry;
3169
3170 line_offset = (sect_offset) DW_UNSND (attr);
3171
3172 /* We may have already read in this line header (TU line header sharing).
3173 If we have we're done. */
3174 find_entry.hash.dwo_unit = cu->dwo_unit;
3175 find_entry.hash.line_sect_off = line_offset;
3176 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3177 &find_entry, INSERT);
3178 if (*slot != NULL)
3179 {
3180 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3181 return;
3182 }
3183
3184 lh = dwarf_decode_line_header (line_offset, cu);
3185 }
3186 if (lh == NULL)
3187 {
3188 lh_cu->v.quick->no_file_data = 1;
3189 return;
3190 }
3191
3192 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
3193 qfn->hash.dwo_unit = cu->dwo_unit;
3194 qfn->hash.line_sect_off = line_offset;
3195 gdb_assert (slot != NULL);
3196 *slot = qfn;
3197
3198 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3199
3200 int offset = 0;
3201 if (strcmp (fnd.name, "<unknown>") != 0)
3202 ++offset;
3203
3204 qfn->num_file_names = offset + lh->file_names_size ();
3205 qfn->file_names =
3206 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
3207 qfn->num_file_names);
3208 if (offset != 0)
3209 qfn->file_names[0] = xstrdup (fnd.name);
3210 for (int i = 0; i < lh->file_names_size (); ++i)
3211 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3212 fnd.comp_dir).release ();
3213 qfn->real_names = NULL;
3214
3215 lh_cu->v.quick->file_names = qfn;
3216 }
3217
3218 /* A helper for the "quick" functions which attempts to read the line
3219 table for THIS_CU. */
3220
3221 static struct quick_file_names *
3222 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3223 dwarf2_per_objfile *per_objfile)
3224 {
3225 /* This should never be called for TUs. */
3226 gdb_assert (! this_cu->is_debug_types);
3227 /* Nor type unit groups. */
3228 gdb_assert (! this_cu->type_unit_group_p ());
3229
3230 if (this_cu->v.quick->file_names != NULL)
3231 return this_cu->v.quick->file_names;
3232 /* If we know there is no line data, no point in looking again. */
3233 if (this_cu->v.quick->no_file_data)
3234 return NULL;
3235
3236 cutu_reader reader (this_cu, per_objfile);
3237 if (!reader.dummy_p)
3238 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3239
3240 if (this_cu->v.quick->no_file_data)
3241 return NULL;
3242 return this_cu->v.quick->file_names;
3243 }
3244
3245 /* A helper for the "quick" functions which computes and caches the
3246 real path for a given file name from the line table. */
3247
3248 static const char *
3249 dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
3250 struct quick_file_names *qfn, int index)
3251 {
3252 if (qfn->real_names == NULL)
3253 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
3254 qfn->num_file_names, const char *);
3255
3256 if (qfn->real_names[index] == NULL)
3257 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3258
3259 return qfn->real_names[index];
3260 }
3261
3262 static struct symtab *
3263 dw2_find_last_source_symtab (struct objfile *objfile)
3264 {
3265 struct dwarf2_per_objfile *dwarf2_per_objfile
3266 = get_dwarf2_per_objfile (objfile);
3267 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
3268 compunit_symtab *cust
3269 = dw2_instantiate_symtab (dwarf_cu, dwarf2_per_objfile, false);
3270
3271 if (cust == NULL)
3272 return NULL;
3273
3274 return compunit_primary_filetab (cust);
3275 }
3276
3277 /* Traversal function for dw2_forget_cached_source_info. */
3278
3279 static int
3280 dw2_free_cached_file_names (void **slot, void *info)
3281 {
3282 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3283
3284 if (file_data->real_names)
3285 {
3286 int i;
3287
3288 for (i = 0; i < file_data->num_file_names; ++i)
3289 {
3290 xfree ((void*) file_data->real_names[i]);
3291 file_data->real_names[i] = NULL;
3292 }
3293 }
3294
3295 return 1;
3296 }
3297
3298 static void
3299 dw2_forget_cached_source_info (struct objfile *objfile)
3300 {
3301 struct dwarf2_per_objfile *dwarf2_per_objfile
3302 = get_dwarf2_per_objfile (objfile);
3303
3304 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3305 dw2_free_cached_file_names, NULL);
3306 }
3307
3308 /* Helper function for dw2_map_symtabs_matching_filename that expands
3309 the symtabs and calls the iterator. */
3310
3311 static int
3312 dw2_map_expand_apply (struct objfile *objfile,
3313 struct dwarf2_per_cu_data *per_cu,
3314 const char *name, const char *real_path,
3315 gdb::function_view<bool (symtab *)> callback)
3316 {
3317 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3318
3319 /* Don't visit already-expanded CUs. */
3320 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3321 if (per_objfile->symtab_set_p (per_cu))
3322 return 0;
3323
3324 /* This may expand more than one symtab, and we want to iterate over
3325 all of them. */
3326 dw2_instantiate_symtab (per_cu, per_objfile, false);
3327
3328 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3329 last_made, callback);
3330 }
3331
3332 /* Implementation of the map_symtabs_matching_filename method. */
3333
3334 static bool
3335 dw2_map_symtabs_matching_filename
3336 (struct objfile *objfile, const char *name, const char *real_path,
3337 gdb::function_view<bool (symtab *)> callback)
3338 {
3339 const char *name_basename = lbasename (name);
3340 struct dwarf2_per_objfile *dwarf2_per_objfile
3341 = get_dwarf2_per_objfile (objfile);
3342
3343 /* The rule is CUs specify all the files, including those used by
3344 any TU, so there's no need to scan TUs here. */
3345
3346 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3347 {
3348 /* We only need to look at symtabs not already expanded. */
3349 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3350 continue;
3351
3352 quick_file_names *file_data
3353 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3354 if (file_data == NULL)
3355 continue;
3356
3357 for (int j = 0; j < file_data->num_file_names; ++j)
3358 {
3359 const char *this_name = file_data->file_names[j];
3360 const char *this_real_name;
3361
3362 if (compare_filenames_for_search (this_name, name))
3363 {
3364 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3365 callback))
3366 return true;
3367 continue;
3368 }
3369
3370 /* Before we invoke realpath, which can get expensive when many
3371 files are involved, do a quick comparison of the basenames. */
3372 if (! basenames_may_differ
3373 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3374 continue;
3375
3376 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3377 file_data, j);
3378 if (compare_filenames_for_search (this_real_name, name))
3379 {
3380 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3381 callback))
3382 return true;
3383 continue;
3384 }
3385
3386 if (real_path != NULL)
3387 {
3388 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3389 gdb_assert (IS_ABSOLUTE_PATH (name));
3390 if (this_real_name != NULL
3391 && FILENAME_CMP (real_path, this_real_name) == 0)
3392 {
3393 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3394 callback))
3395 return true;
3396 continue;
3397 }
3398 }
3399 }
3400 }
3401
3402 return false;
3403 }
3404
3405 /* Struct used to manage iterating over all CUs looking for a symbol. */
3406
3407 struct dw2_symtab_iterator
3408 {
3409 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3410 struct dwarf2_per_objfile *dwarf2_per_objfile;
3411 /* If set, only look for symbols that match that block. Valid values are
3412 GLOBAL_BLOCK and STATIC_BLOCK. */
3413 gdb::optional<block_enum> block_index;
3414 /* The kind of symbol we're looking for. */
3415 domain_enum domain;
3416 /* The list of CUs from the index entry of the symbol,
3417 or NULL if not found. */
3418 offset_type *vec;
3419 /* The next element in VEC to look at. */
3420 int next;
3421 /* The number of elements in VEC, or zero if there is no match. */
3422 int length;
3423 /* Have we seen a global version of the symbol?
3424 If so we can ignore all further global instances.
3425 This is to work around gold/15646, inefficient gold-generated
3426 indices. */
3427 int global_seen;
3428 };
3429
3430 /* Initialize the index symtab iterator ITER. */
3431
3432 static void
3433 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3434 struct dwarf2_per_objfile *dwarf2_per_objfile,
3435 gdb::optional<block_enum> block_index,
3436 domain_enum domain,
3437 const char *name)
3438 {
3439 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3440 iter->block_index = block_index;
3441 iter->domain = domain;
3442 iter->next = 0;
3443 iter->global_seen = 0;
3444
3445 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
3446
3447 /* index is NULL if OBJF_READNOW. */
3448 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3449 iter->length = MAYBE_SWAP (*iter->vec);
3450 else
3451 {
3452 iter->vec = NULL;
3453 iter->length = 0;
3454 }
3455 }
3456
3457 /* Return the next matching CU or NULL if there are no more. */
3458
3459 static struct dwarf2_per_cu_data *
3460 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3461 {
3462 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3463
3464 for ( ; iter->next < iter->length; ++iter->next)
3465 {
3466 offset_type cu_index_and_attrs =
3467 MAYBE_SWAP (iter->vec[iter->next + 1]);
3468 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3469 gdb_index_symbol_kind symbol_kind =
3470 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3471 /* Only check the symbol attributes if they're present.
3472 Indices prior to version 7 don't record them,
3473 and indices >= 7 may elide them for certain symbols
3474 (gold does this). */
3475 int attrs_valid =
3476 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
3477 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3478
3479 /* Don't crash on bad data. */
3480 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3481 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3482 {
3483 complaint (_(".gdb_index entry has bad CU index"
3484 " [in module %s]"),
3485 objfile_name (dwarf2_per_objfile->objfile));
3486 continue;
3487 }
3488
3489 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3490
3491 /* Skip if already read in. */
3492 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3493 continue;
3494
3495 /* Check static vs global. */
3496 if (attrs_valid)
3497 {
3498 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3499
3500 if (iter->block_index.has_value ())
3501 {
3502 bool want_static = *iter->block_index == STATIC_BLOCK;
3503
3504 if (is_static != want_static)
3505 continue;
3506 }
3507
3508 /* Work around gold/15646. */
3509 if (!is_static && iter->global_seen)
3510 continue;
3511 if (!is_static)
3512 iter->global_seen = 1;
3513 }
3514
3515 /* Only check the symbol's kind if it has one. */
3516 if (attrs_valid)
3517 {
3518 switch (iter->domain)
3519 {
3520 case VAR_DOMAIN:
3521 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3522 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3523 /* Some types are also in VAR_DOMAIN. */
3524 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3525 continue;
3526 break;
3527 case STRUCT_DOMAIN:
3528 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3529 continue;
3530 break;
3531 case LABEL_DOMAIN:
3532 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3533 continue;
3534 break;
3535 case MODULE_DOMAIN:
3536 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3537 continue;
3538 break;
3539 default:
3540 break;
3541 }
3542 }
3543
3544 ++iter->next;
3545 return per_cu;
3546 }
3547
3548 return NULL;
3549 }
3550
3551 static struct compunit_symtab *
3552 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3553 const char *name, domain_enum domain)
3554 {
3555 struct compunit_symtab *stab_best = NULL;
3556 struct dwarf2_per_objfile *dwarf2_per_objfile
3557 = get_dwarf2_per_objfile (objfile);
3558
3559 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3560
3561 struct dw2_symtab_iterator iter;
3562 struct dwarf2_per_cu_data *per_cu;
3563
3564 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3565
3566 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3567 {
3568 struct symbol *sym, *with_opaque = NULL;
3569 struct compunit_symtab *stab
3570 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3571 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3572 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3573
3574 sym = block_find_symbol (block, name, domain,
3575 block_find_non_opaque_type_preferred,
3576 &with_opaque);
3577
3578 /* Some caution must be observed with overloaded functions
3579 and methods, since the index will not contain any overload
3580 information (but NAME might contain it). */
3581
3582 if (sym != NULL
3583 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3584 return stab;
3585 if (with_opaque != NULL
3586 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3587 stab_best = stab;
3588
3589 /* Keep looking through other CUs. */
3590 }
3591
3592 return stab_best;
3593 }
3594
3595 static void
3596 dw2_print_stats (struct objfile *objfile)
3597 {
3598 struct dwarf2_per_objfile *dwarf2_per_objfile
3599 = get_dwarf2_per_objfile (objfile);
3600 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3601 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3602 int count = 0;
3603
3604 for (int i = 0; i < total; ++i)
3605 {
3606 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3607
3608 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
3609 ++count;
3610 }
3611 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3612 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3613 }
3614
3615 /* This dumps minimal information about the index.
3616 It is called via "mt print objfiles".
3617 One use is to verify .gdb_index has been loaded by the
3618 gdb.dwarf2/gdb-index.exp testcase. */
3619
3620 static void
3621 dw2_dump (struct objfile *objfile)
3622 {
3623 struct dwarf2_per_objfile *dwarf2_per_objfile
3624 = get_dwarf2_per_objfile (objfile);
3625
3626 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
3627 printf_filtered (".gdb_index:");
3628 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
3629 {
3630 printf_filtered (" version %d\n",
3631 dwarf2_per_objfile->per_bfd->index_table->version);
3632 }
3633 else
3634 printf_filtered (" faked for \"readnow\"\n");
3635 printf_filtered ("\n");
3636 }
3637
3638 static void
3639 dw2_expand_symtabs_for_function (struct objfile *objfile,
3640 const char *func_name)
3641 {
3642 struct dwarf2_per_objfile *dwarf2_per_objfile
3643 = get_dwarf2_per_objfile (objfile);
3644
3645 struct dw2_symtab_iterator iter;
3646 struct dwarf2_per_cu_data *per_cu;
3647
3648 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3649
3650 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3651 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3652
3653 }
3654
3655 static void
3656 dw2_expand_all_symtabs (struct objfile *objfile)
3657 {
3658 struct dwarf2_per_objfile *dwarf2_per_objfile
3659 = get_dwarf2_per_objfile (objfile);
3660 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3661 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3662
3663 for (int i = 0; i < total_units; ++i)
3664 {
3665 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3666
3667 /* We don't want to directly expand a partial CU, because if we
3668 read it with the wrong language, then assertion failures can
3669 be triggered later on. See PR symtab/23010. So, tell
3670 dw2_instantiate_symtab to skip partial CUs -- any important
3671 partial CU will be read via DW_TAG_imported_unit anyway. */
3672 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, true);
3673 }
3674 }
3675
3676 static void
3677 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3678 const char *fullname)
3679 {
3680 struct dwarf2_per_objfile *dwarf2_per_objfile
3681 = get_dwarf2_per_objfile (objfile);
3682
3683 /* We don't need to consider type units here.
3684 This is only called for examining code, e.g. expand_line_sal.
3685 There can be an order of magnitude (or more) more type units
3686 than comp units, and we avoid them if we can. */
3687
3688 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3689 {
3690 /* We only need to look at symtabs not already expanded. */
3691 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3692 continue;
3693
3694 quick_file_names *file_data
3695 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3696 if (file_data == NULL)
3697 continue;
3698
3699 for (int j = 0; j < file_data->num_file_names; ++j)
3700 {
3701 const char *this_fullname = file_data->file_names[j];
3702
3703 if (filename_cmp (this_fullname, fullname) == 0)
3704 {
3705 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3706 break;
3707 }
3708 }
3709 }
3710 }
3711
3712 static void
3713 dw2_expand_symtabs_matching_symbol
3714 (mapped_index_base &index,
3715 const lookup_name_info &lookup_name_in,
3716 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3717 enum search_domain kind,
3718 gdb::function_view<bool (offset_type)> match_callback);
3719
3720 static void
3721 dw2_expand_symtabs_matching_one
3722 (dwarf2_per_cu_data *per_cu,
3723 dwarf2_per_objfile *per_objfile,
3724 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3725 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3726
3727 static void
3728 dw2_map_matching_symbols
3729 (struct objfile *objfile,
3730 const lookup_name_info &name, domain_enum domain,
3731 int global,
3732 gdb::function_view<symbol_found_callback_ftype> callback,
3733 symbol_compare_ftype *ordered_compare)
3734 {
3735 /* Used for Ada. */
3736 struct dwarf2_per_objfile *dwarf2_per_objfile
3737 = get_dwarf2_per_objfile (objfile);
3738
3739 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3740
3741 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
3742 {
3743 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3744 here though if the current language is Ada for a non-Ada objfile
3745 using GNU index. */
3746 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3747
3748 const char *match_name = name.ada ().lookup_name ().c_str ();
3749 auto matcher = [&] (const char *symname)
3750 {
3751 if (ordered_compare == nullptr)
3752 return true;
3753 return ordered_compare (symname, match_name) == 0;
3754 };
3755
3756 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3757 [&] (offset_type namei)
3758 {
3759 struct dw2_symtab_iterator iter;
3760 struct dwarf2_per_cu_data *per_cu;
3761
3762 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3763 match_name);
3764 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3765 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
3766 nullptr);
3767 return true;
3768 });
3769 }
3770 else
3771 {
3772 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3773 proceed assuming all symtabs have been read in. */
3774 }
3775
3776 for (compunit_symtab *cust : objfile->compunits ())
3777 {
3778 const struct block *block;
3779
3780 if (cust == NULL)
3781 continue;
3782 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3783 if (!iterate_over_symbols_terminated (block, name,
3784 domain, callback))
3785 return;
3786 }
3787 }
3788
3789 /* Starting from a search name, return the string that finds the upper
3790 bound of all strings that start with SEARCH_NAME in a sorted name
3791 list. Returns the empty string to indicate that the upper bound is
3792 the end of the list. */
3793
3794 static std::string
3795 make_sort_after_prefix_name (const char *search_name)
3796 {
3797 /* When looking to complete "func", we find the upper bound of all
3798 symbols that start with "func" by looking for where we'd insert
3799 the closest string that would follow "func" in lexicographical
3800 order. Usually, that's "func"-with-last-character-incremented,
3801 i.e. "fund". Mind non-ASCII characters, though. Usually those
3802 will be UTF-8 multi-byte sequences, but we can't be certain.
3803 Especially mind the 0xff character, which is a valid character in
3804 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3805 rule out compilers allowing it in identifiers. Note that
3806 conveniently, strcmp/strcasecmp are specified to compare
3807 characters interpreted as unsigned char. So what we do is treat
3808 the whole string as a base 256 number composed of a sequence of
3809 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3810 to 0, and carries 1 to the following more-significant position.
3811 If the very first character in SEARCH_NAME ends up incremented
3812 and carries/overflows, then the upper bound is the end of the
3813 list. The string after the empty string is also the empty
3814 string.
3815
3816 Some examples of this operation:
3817
3818 SEARCH_NAME => "+1" RESULT
3819
3820 "abc" => "abd"
3821 "ab\xff" => "ac"
3822 "\xff" "a" "\xff" => "\xff" "b"
3823 "\xff" => ""
3824 "\xff\xff" => ""
3825 "" => ""
3826
3827 Then, with these symbols for example:
3828
3829 func
3830 func1
3831 fund
3832
3833 completing "func" looks for symbols between "func" and
3834 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3835 which finds "func" and "func1", but not "fund".
3836
3837 And with:
3838
3839 funcÿ (Latin1 'ÿ' [0xff])
3840 funcÿ1
3841 fund
3842
3843 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3844 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3845
3846 And with:
3847
3848 ÿÿ (Latin1 'ÿ' [0xff])
3849 ÿÿ1
3850
3851 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3852 the end of the list.
3853 */
3854 std::string after = search_name;
3855 while (!after.empty () && (unsigned char) after.back () == 0xff)
3856 after.pop_back ();
3857 if (!after.empty ())
3858 after.back () = (unsigned char) after.back () + 1;
3859 return after;
3860 }
3861
3862 /* See declaration. */
3863
3864 std::pair<std::vector<name_component>::const_iterator,
3865 std::vector<name_component>::const_iterator>
3866 mapped_index_base::find_name_components_bounds
3867 (const lookup_name_info &lookup_name_without_params, language lang) const
3868 {
3869 auto *name_cmp
3870 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3871
3872 const char *lang_name
3873 = lookup_name_without_params.language_lookup_name (lang);
3874
3875 /* Comparison function object for lower_bound that matches against a
3876 given symbol name. */
3877 auto lookup_compare_lower = [&] (const name_component &elem,
3878 const char *name)
3879 {
3880 const char *elem_qualified = this->symbol_name_at (elem.idx);
3881 const char *elem_name = elem_qualified + elem.name_offset;
3882 return name_cmp (elem_name, name) < 0;
3883 };
3884
3885 /* Comparison function object for upper_bound that matches against a
3886 given symbol name. */
3887 auto lookup_compare_upper = [&] (const char *name,
3888 const name_component &elem)
3889 {
3890 const char *elem_qualified = this->symbol_name_at (elem.idx);
3891 const char *elem_name = elem_qualified + elem.name_offset;
3892 return name_cmp (name, elem_name) < 0;
3893 };
3894
3895 auto begin = this->name_components.begin ();
3896 auto end = this->name_components.end ();
3897
3898 /* Find the lower bound. */
3899 auto lower = [&] ()
3900 {
3901 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3902 return begin;
3903 else
3904 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3905 } ();
3906
3907 /* Find the upper bound. */
3908 auto upper = [&] ()
3909 {
3910 if (lookup_name_without_params.completion_mode ())
3911 {
3912 /* In completion mode, we want UPPER to point past all
3913 symbols names that have the same prefix. I.e., with
3914 these symbols, and completing "func":
3915
3916 function << lower bound
3917 function1
3918 other_function << upper bound
3919
3920 We find the upper bound by looking for the insertion
3921 point of "func"-with-last-character-incremented,
3922 i.e. "fund". */
3923 std::string after = make_sort_after_prefix_name (lang_name);
3924 if (after.empty ())
3925 return end;
3926 return std::lower_bound (lower, end, after.c_str (),
3927 lookup_compare_lower);
3928 }
3929 else
3930 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3931 } ();
3932
3933 return {lower, upper};
3934 }
3935
3936 /* See declaration. */
3937
3938 void
3939 mapped_index_base::build_name_components ()
3940 {
3941 if (!this->name_components.empty ())
3942 return;
3943
3944 this->name_components_casing = case_sensitivity;
3945 auto *name_cmp
3946 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3947
3948 /* The code below only knows how to break apart components of C++
3949 symbol names (and other languages that use '::' as
3950 namespace/module separator) and Ada symbol names. */
3951 auto count = this->symbol_name_count ();
3952 for (offset_type idx = 0; idx < count; idx++)
3953 {
3954 if (this->symbol_name_slot_invalid (idx))
3955 continue;
3956
3957 const char *name = this->symbol_name_at (idx);
3958
3959 /* Add each name component to the name component table. */
3960 unsigned int previous_len = 0;
3961
3962 if (strstr (name, "::") != nullptr)
3963 {
3964 for (unsigned int current_len = cp_find_first_component (name);
3965 name[current_len] != '\0';
3966 current_len += cp_find_first_component (name + current_len))
3967 {
3968 gdb_assert (name[current_len] == ':');
3969 this->name_components.push_back ({previous_len, idx});
3970 /* Skip the '::'. */
3971 current_len += 2;
3972 previous_len = current_len;
3973 }
3974 }
3975 else
3976 {
3977 /* Handle the Ada encoded (aka mangled) form here. */
3978 for (const char *iter = strstr (name, "__");
3979 iter != nullptr;
3980 iter = strstr (iter, "__"))
3981 {
3982 this->name_components.push_back ({previous_len, idx});
3983 iter += 2;
3984 previous_len = iter - name;
3985 }
3986 }
3987
3988 this->name_components.push_back ({previous_len, idx});
3989 }
3990
3991 /* Sort name_components elements by name. */
3992 auto name_comp_compare = [&] (const name_component &left,
3993 const name_component &right)
3994 {
3995 const char *left_qualified = this->symbol_name_at (left.idx);
3996 const char *right_qualified = this->symbol_name_at (right.idx);
3997
3998 const char *left_name = left_qualified + left.name_offset;
3999 const char *right_name = right_qualified + right.name_offset;
4000
4001 return name_cmp (left_name, right_name) < 0;
4002 };
4003
4004 std::sort (this->name_components.begin (),
4005 this->name_components.end (),
4006 name_comp_compare);
4007 }
4008
4009 /* Helper for dw2_expand_symtabs_matching that works with a
4010 mapped_index_base instead of the containing objfile. This is split
4011 to a separate function in order to be able to unit test the
4012 name_components matching using a mock mapped_index_base. For each
4013 symbol name that matches, calls MATCH_CALLBACK, passing it the
4014 symbol's index in the mapped_index_base symbol table. */
4015
4016 static void
4017 dw2_expand_symtabs_matching_symbol
4018 (mapped_index_base &index,
4019 const lookup_name_info &lookup_name_in,
4020 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4021 enum search_domain kind,
4022 gdb::function_view<bool (offset_type)> match_callback)
4023 {
4024 lookup_name_info lookup_name_without_params
4025 = lookup_name_in.make_ignore_params ();
4026
4027 /* Build the symbol name component sorted vector, if we haven't
4028 yet. */
4029 index.build_name_components ();
4030
4031 /* The same symbol may appear more than once in the range though.
4032 E.g., if we're looking for symbols that complete "w", and we have
4033 a symbol named "w1::w2", we'll find the two name components for
4034 that same symbol in the range. To be sure we only call the
4035 callback once per symbol, we first collect the symbol name
4036 indexes that matched in a temporary vector and ignore
4037 duplicates. */
4038 std::vector<offset_type> matches;
4039
4040 struct name_and_matcher
4041 {
4042 symbol_name_matcher_ftype *matcher;
4043 const char *name;
4044
4045 bool operator== (const name_and_matcher &other) const
4046 {
4047 return matcher == other.matcher && strcmp (name, other.name) == 0;
4048 }
4049 };
4050
4051 /* A vector holding all the different symbol name matchers, for all
4052 languages. */
4053 std::vector<name_and_matcher> matchers;
4054
4055 for (int i = 0; i < nr_languages; i++)
4056 {
4057 enum language lang_e = (enum language) i;
4058
4059 const language_defn *lang = language_def (lang_e);
4060 symbol_name_matcher_ftype *name_matcher
4061 = get_symbol_name_matcher (lang, lookup_name_without_params);
4062
4063 name_and_matcher key {
4064 name_matcher,
4065 lookup_name_without_params.language_lookup_name (lang_e)
4066 };
4067
4068 /* Don't insert the same comparison routine more than once.
4069 Note that we do this linear walk. This is not a problem in
4070 practice because the number of supported languages is
4071 low. */
4072 if (std::find (matchers.begin (), matchers.end (), key)
4073 != matchers.end ())
4074 continue;
4075 matchers.push_back (std::move (key));
4076
4077 auto bounds
4078 = index.find_name_components_bounds (lookup_name_without_params,
4079 lang_e);
4080
4081 /* Now for each symbol name in range, check to see if we have a name
4082 match, and if so, call the MATCH_CALLBACK callback. */
4083
4084 for (; bounds.first != bounds.second; ++bounds.first)
4085 {
4086 const char *qualified = index.symbol_name_at (bounds.first->idx);
4087
4088 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4089 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4090 continue;
4091
4092 matches.push_back (bounds.first->idx);
4093 }
4094 }
4095
4096 std::sort (matches.begin (), matches.end ());
4097
4098 /* Finally call the callback, once per match. */
4099 ULONGEST prev = -1;
4100 for (offset_type idx : matches)
4101 {
4102 if (prev != idx)
4103 {
4104 if (!match_callback (idx))
4105 break;
4106 prev = idx;
4107 }
4108 }
4109
4110 /* Above we use a type wider than idx's for 'prev', since 0 and
4111 (offset_type)-1 are both possible values. */
4112 static_assert (sizeof (prev) > sizeof (offset_type), "");
4113 }
4114
4115 #if GDB_SELF_TEST
4116
4117 namespace selftests { namespace dw2_expand_symtabs_matching {
4118
4119 /* A mock .gdb_index/.debug_names-like name index table, enough to
4120 exercise dw2_expand_symtabs_matching_symbol, which works with the
4121 mapped_index_base interface. Builds an index from the symbol list
4122 passed as parameter to the constructor. */
4123 class mock_mapped_index : public mapped_index_base
4124 {
4125 public:
4126 mock_mapped_index (gdb::array_view<const char *> symbols)
4127 : m_symbol_table (symbols)
4128 {}
4129
4130 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4131
4132 /* Return the number of names in the symbol table. */
4133 size_t symbol_name_count () const override
4134 {
4135 return m_symbol_table.size ();
4136 }
4137
4138 /* Get the name of the symbol at IDX in the symbol table. */
4139 const char *symbol_name_at (offset_type idx) const override
4140 {
4141 return m_symbol_table[idx];
4142 }
4143
4144 private:
4145 gdb::array_view<const char *> m_symbol_table;
4146 };
4147
4148 /* Convenience function that converts a NULL pointer to a "<null>"
4149 string, to pass to print routines. */
4150
4151 static const char *
4152 string_or_null (const char *str)
4153 {
4154 return str != NULL ? str : "<null>";
4155 }
4156
4157 /* Check if a lookup_name_info built from
4158 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4159 index. EXPECTED_LIST is the list of expected matches, in expected
4160 matching order. If no match expected, then an empty list is
4161 specified. Returns true on success. On failure prints a warning
4162 indicating the file:line that failed, and returns false. */
4163
4164 static bool
4165 check_match (const char *file, int line,
4166 mock_mapped_index &mock_index,
4167 const char *name, symbol_name_match_type match_type,
4168 bool completion_mode,
4169 std::initializer_list<const char *> expected_list)
4170 {
4171 lookup_name_info lookup_name (name, match_type, completion_mode);
4172
4173 bool matched = true;
4174
4175 auto mismatch = [&] (const char *expected_str,
4176 const char *got)
4177 {
4178 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4179 "expected=\"%s\", got=\"%s\"\n"),
4180 file, line,
4181 (match_type == symbol_name_match_type::FULL
4182 ? "FULL" : "WILD"),
4183 name, string_or_null (expected_str), string_or_null (got));
4184 matched = false;
4185 };
4186
4187 auto expected_it = expected_list.begin ();
4188 auto expected_end = expected_list.end ();
4189
4190 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4191 NULL, ALL_DOMAIN,
4192 [&] (offset_type idx)
4193 {
4194 const char *matched_name = mock_index.symbol_name_at (idx);
4195 const char *expected_str
4196 = expected_it == expected_end ? NULL : *expected_it++;
4197
4198 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4199 mismatch (expected_str, matched_name);
4200 return true;
4201 });
4202
4203 const char *expected_str
4204 = expected_it == expected_end ? NULL : *expected_it++;
4205 if (expected_str != NULL)
4206 mismatch (expected_str, NULL);
4207
4208 return matched;
4209 }
4210
4211 /* The symbols added to the mock mapped_index for testing (in
4212 canonical form). */
4213 static const char *test_symbols[] = {
4214 "function",
4215 "std::bar",
4216 "std::zfunction",
4217 "std::zfunction2",
4218 "w1::w2",
4219 "ns::foo<char*>",
4220 "ns::foo<int>",
4221 "ns::foo<long>",
4222 "ns2::tmpl<int>::foo2",
4223 "(anonymous namespace)::A::B::C",
4224
4225 /* These are used to check that the increment-last-char in the
4226 matching algorithm for completion doesn't match "t1_fund" when
4227 completing "t1_func". */
4228 "t1_func",
4229 "t1_func1",
4230 "t1_fund",
4231 "t1_fund1",
4232
4233 /* A UTF-8 name with multi-byte sequences to make sure that
4234 cp-name-parser understands this as a single identifier ("função"
4235 is "function" in PT). */
4236 u8"u8função",
4237
4238 /* \377 (0xff) is Latin1 'ÿ'. */
4239 "yfunc\377",
4240
4241 /* \377 (0xff) is Latin1 'ÿ'. */
4242 "\377",
4243 "\377\377123",
4244
4245 /* A name with all sorts of complications. Starts with "z" to make
4246 it easier for the completion tests below. */
4247 #define Z_SYM_NAME \
4248 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4249 "::tuple<(anonymous namespace)::ui*, " \
4250 "std::default_delete<(anonymous namespace)::ui>, void>"
4251
4252 Z_SYM_NAME
4253 };
4254
4255 /* Returns true if the mapped_index_base::find_name_component_bounds
4256 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4257 in completion mode. */
4258
4259 static bool
4260 check_find_bounds_finds (mapped_index_base &index,
4261 const char *search_name,
4262 gdb::array_view<const char *> expected_syms)
4263 {
4264 lookup_name_info lookup_name (search_name,
4265 symbol_name_match_type::FULL, true);
4266
4267 auto bounds = index.find_name_components_bounds (lookup_name,
4268 language_cplus);
4269
4270 size_t distance = std::distance (bounds.first, bounds.second);
4271 if (distance != expected_syms.size ())
4272 return false;
4273
4274 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4275 {
4276 auto nc_elem = bounds.first + exp_elem;
4277 const char *qualified = index.symbol_name_at (nc_elem->idx);
4278 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4279 return false;
4280 }
4281
4282 return true;
4283 }
4284
4285 /* Test the lower-level mapped_index::find_name_component_bounds
4286 method. */
4287
4288 static void
4289 test_mapped_index_find_name_component_bounds ()
4290 {
4291 mock_mapped_index mock_index (test_symbols);
4292
4293 mock_index.build_name_components ();
4294
4295 /* Test the lower-level mapped_index::find_name_component_bounds
4296 method in completion mode. */
4297 {
4298 static const char *expected_syms[] = {
4299 "t1_func",
4300 "t1_func1",
4301 };
4302
4303 SELF_CHECK (check_find_bounds_finds (mock_index,
4304 "t1_func", expected_syms));
4305 }
4306
4307 /* Check that the increment-last-char in the name matching algorithm
4308 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4309 {
4310 static const char *expected_syms1[] = {
4311 "\377",
4312 "\377\377123",
4313 };
4314 SELF_CHECK (check_find_bounds_finds (mock_index,
4315 "\377", expected_syms1));
4316
4317 static const char *expected_syms2[] = {
4318 "\377\377123",
4319 };
4320 SELF_CHECK (check_find_bounds_finds (mock_index,
4321 "\377\377", expected_syms2));
4322 }
4323 }
4324
4325 /* Test dw2_expand_symtabs_matching_symbol. */
4326
4327 static void
4328 test_dw2_expand_symtabs_matching_symbol ()
4329 {
4330 mock_mapped_index mock_index (test_symbols);
4331
4332 /* We let all tests run until the end even if some fails, for debug
4333 convenience. */
4334 bool any_mismatch = false;
4335
4336 /* Create the expected symbols list (an initializer_list). Needed
4337 because lists have commas, and we need to pass them to CHECK,
4338 which is a macro. */
4339 #define EXPECT(...) { __VA_ARGS__ }
4340
4341 /* Wrapper for check_match that passes down the current
4342 __FILE__/__LINE__. */
4343 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4344 any_mismatch |= !check_match (__FILE__, __LINE__, \
4345 mock_index, \
4346 NAME, MATCH_TYPE, COMPLETION_MODE, \
4347 EXPECTED_LIST)
4348
4349 /* Identity checks. */
4350 for (const char *sym : test_symbols)
4351 {
4352 /* Should be able to match all existing symbols. */
4353 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4354 EXPECT (sym));
4355
4356 /* Should be able to match all existing symbols with
4357 parameters. */
4358 std::string with_params = std::string (sym) + "(int)";
4359 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4360 EXPECT (sym));
4361
4362 /* Should be able to match all existing symbols with
4363 parameters and qualifiers. */
4364 with_params = std::string (sym) + " ( int ) const";
4365 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4366 EXPECT (sym));
4367
4368 /* This should really find sym, but cp-name-parser.y doesn't
4369 know about lvalue/rvalue qualifiers yet. */
4370 with_params = std::string (sym) + " ( int ) &&";
4371 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4372 {});
4373 }
4374
4375 /* Check that the name matching algorithm for completion doesn't get
4376 confused with Latin1 'ÿ' / 0xff. */
4377 {
4378 static const char str[] = "\377";
4379 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4380 EXPECT ("\377", "\377\377123"));
4381 }
4382
4383 /* Check that the increment-last-char in the matching algorithm for
4384 completion doesn't match "t1_fund" when completing "t1_func". */
4385 {
4386 static const char str[] = "t1_func";
4387 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4388 EXPECT ("t1_func", "t1_func1"));
4389 }
4390
4391 /* Check that completion mode works at each prefix of the expected
4392 symbol name. */
4393 {
4394 static const char str[] = "function(int)";
4395 size_t len = strlen (str);
4396 std::string lookup;
4397
4398 for (size_t i = 1; i < len; i++)
4399 {
4400 lookup.assign (str, i);
4401 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4402 EXPECT ("function"));
4403 }
4404 }
4405
4406 /* While "w" is a prefix of both components, the match function
4407 should still only be called once. */
4408 {
4409 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4410 EXPECT ("w1::w2"));
4411 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4412 EXPECT ("w1::w2"));
4413 }
4414
4415 /* Same, with a "complicated" symbol. */
4416 {
4417 static const char str[] = Z_SYM_NAME;
4418 size_t len = strlen (str);
4419 std::string lookup;
4420
4421 for (size_t i = 1; i < len; i++)
4422 {
4423 lookup.assign (str, i);
4424 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4425 EXPECT (Z_SYM_NAME));
4426 }
4427 }
4428
4429 /* In FULL mode, an incomplete symbol doesn't match. */
4430 {
4431 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4432 {});
4433 }
4434
4435 /* A complete symbol with parameters matches any overload, since the
4436 index has no overload info. */
4437 {
4438 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4439 EXPECT ("std::zfunction", "std::zfunction2"));
4440 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4441 EXPECT ("std::zfunction", "std::zfunction2"));
4442 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4443 EXPECT ("std::zfunction", "std::zfunction2"));
4444 }
4445
4446 /* Check that whitespace is ignored appropriately. A symbol with a
4447 template argument list. */
4448 {
4449 static const char expected[] = "ns::foo<int>";
4450 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4451 EXPECT (expected));
4452 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4453 EXPECT (expected));
4454 }
4455
4456 /* Check that whitespace is ignored appropriately. A symbol with a
4457 template argument list that includes a pointer. */
4458 {
4459 static const char expected[] = "ns::foo<char*>";
4460 /* Try both completion and non-completion modes. */
4461 static const bool completion_mode[2] = {false, true};
4462 for (size_t i = 0; i < 2; i++)
4463 {
4464 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4465 completion_mode[i], EXPECT (expected));
4466 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4467 completion_mode[i], EXPECT (expected));
4468
4469 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4470 completion_mode[i], EXPECT (expected));
4471 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4472 completion_mode[i], EXPECT (expected));
4473 }
4474 }
4475
4476 {
4477 /* Check method qualifiers are ignored. */
4478 static const char expected[] = "ns::foo<char*>";
4479 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4480 symbol_name_match_type::FULL, true, EXPECT (expected));
4481 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4482 symbol_name_match_type::FULL, true, EXPECT (expected));
4483 CHECK_MATCH ("foo < char * > ( int ) const",
4484 symbol_name_match_type::WILD, true, EXPECT (expected));
4485 CHECK_MATCH ("foo < char * > ( int ) &&",
4486 symbol_name_match_type::WILD, true, EXPECT (expected));
4487 }
4488
4489 /* Test lookup names that don't match anything. */
4490 {
4491 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4492 {});
4493
4494 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4495 {});
4496 }
4497
4498 /* Some wild matching tests, exercising "(anonymous namespace)",
4499 which should not be confused with a parameter list. */
4500 {
4501 static const char *syms[] = {
4502 "A::B::C",
4503 "B::C",
4504 "C",
4505 "A :: B :: C ( int )",
4506 "B :: C ( int )",
4507 "C ( int )",
4508 };
4509
4510 for (const char *s : syms)
4511 {
4512 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4513 EXPECT ("(anonymous namespace)::A::B::C"));
4514 }
4515 }
4516
4517 {
4518 static const char expected[] = "ns2::tmpl<int>::foo2";
4519 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4520 EXPECT (expected));
4521 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4522 EXPECT (expected));
4523 }
4524
4525 SELF_CHECK (!any_mismatch);
4526
4527 #undef EXPECT
4528 #undef CHECK_MATCH
4529 }
4530
4531 static void
4532 run_test ()
4533 {
4534 test_mapped_index_find_name_component_bounds ();
4535 test_dw2_expand_symtabs_matching_symbol ();
4536 }
4537
4538 }} // namespace selftests::dw2_expand_symtabs_matching
4539
4540 #endif /* GDB_SELF_TEST */
4541
4542 /* If FILE_MATCHER is NULL or if PER_CU has
4543 dwarf2_per_cu_quick_data::MARK set (see
4544 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4545 EXPANSION_NOTIFY on it. */
4546
4547 static void
4548 dw2_expand_symtabs_matching_one
4549 (dwarf2_per_cu_data *per_cu,
4550 dwarf2_per_objfile *per_objfile,
4551 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4552 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4553 {
4554 if (file_matcher == NULL || per_cu->v.quick->mark)
4555 {
4556 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4557
4558 compunit_symtab *symtab
4559 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4560 gdb_assert (symtab != nullptr);
4561
4562 if (expansion_notify != NULL && symtab_was_null)
4563 expansion_notify (symtab);
4564 }
4565 }
4566
4567 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4568 matched, to expand corresponding CUs that were marked. IDX is the
4569 index of the symbol name that matched. */
4570
4571 static void
4572 dw2_expand_marked_cus
4573 (dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4574 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4575 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4576 search_domain kind)
4577 {
4578 offset_type *vec, vec_len, vec_idx;
4579 bool global_seen = false;
4580 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4581
4582 vec = (offset_type *) (index.constant_pool
4583 + MAYBE_SWAP (index.symbol_table[idx].vec));
4584 vec_len = MAYBE_SWAP (vec[0]);
4585 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4586 {
4587 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4588 /* This value is only valid for index versions >= 7. */
4589 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4590 gdb_index_symbol_kind symbol_kind =
4591 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4592 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4593 /* Only check the symbol attributes if they're present.
4594 Indices prior to version 7 don't record them,
4595 and indices >= 7 may elide them for certain symbols
4596 (gold does this). */
4597 int attrs_valid =
4598 (index.version >= 7
4599 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4600
4601 /* Work around gold/15646. */
4602 if (attrs_valid)
4603 {
4604 if (!is_static && global_seen)
4605 continue;
4606 if (!is_static)
4607 global_seen = true;
4608 }
4609
4610 /* Only check the symbol's kind if it has one. */
4611 if (attrs_valid)
4612 {
4613 switch (kind)
4614 {
4615 case VARIABLES_DOMAIN:
4616 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4617 continue;
4618 break;
4619 case FUNCTIONS_DOMAIN:
4620 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4621 continue;
4622 break;
4623 case TYPES_DOMAIN:
4624 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4625 continue;
4626 break;
4627 case MODULES_DOMAIN:
4628 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4629 continue;
4630 break;
4631 default:
4632 break;
4633 }
4634 }
4635
4636 /* Don't crash on bad data. */
4637 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4638 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
4639 {
4640 complaint (_(".gdb_index entry has bad CU index"
4641 " [in module %s]"),
4642 objfile_name (dwarf2_per_objfile->objfile));
4643 continue;
4644 }
4645
4646 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
4647 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, file_matcher,
4648 expansion_notify);
4649 }
4650 }
4651
4652 /* If FILE_MATCHER is non-NULL, set all the
4653 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4654 that match FILE_MATCHER. */
4655
4656 static void
4657 dw_expand_symtabs_matching_file_matcher
4658 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4659 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4660 {
4661 if (file_matcher == NULL)
4662 return;
4663
4664 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4665 htab_eq_pointer,
4666 NULL, xcalloc, xfree));
4667 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4668 htab_eq_pointer,
4669 NULL, xcalloc, xfree));
4670
4671 /* The rule is CUs specify all the files, including those used by
4672 any TU, so there's no need to scan TUs here. */
4673
4674 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4675 {
4676 QUIT;
4677
4678 per_cu->v.quick->mark = 0;
4679
4680 /* We only need to look at symtabs not already expanded. */
4681 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4682 continue;
4683
4684 quick_file_names *file_data
4685 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4686 if (file_data == NULL)
4687 continue;
4688
4689 if (htab_find (visited_not_found.get (), file_data) != NULL)
4690 continue;
4691 else if (htab_find (visited_found.get (), file_data) != NULL)
4692 {
4693 per_cu->v.quick->mark = 1;
4694 continue;
4695 }
4696
4697 for (int j = 0; j < file_data->num_file_names; ++j)
4698 {
4699 const char *this_real_name;
4700
4701 if (file_matcher (file_data->file_names[j], false))
4702 {
4703 per_cu->v.quick->mark = 1;
4704 break;
4705 }
4706
4707 /* Before we invoke realpath, which can get expensive when many
4708 files are involved, do a quick comparison of the basenames. */
4709 if (!basenames_may_differ
4710 && !file_matcher (lbasename (file_data->file_names[j]),
4711 true))
4712 continue;
4713
4714 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4715 file_data, j);
4716 if (file_matcher (this_real_name, false))
4717 {
4718 per_cu->v.quick->mark = 1;
4719 break;
4720 }
4721 }
4722
4723 void **slot = htab_find_slot (per_cu->v.quick->mark
4724 ? visited_found.get ()
4725 : visited_not_found.get (),
4726 file_data, INSERT);
4727 *slot = file_data;
4728 }
4729 }
4730
4731 static void
4732 dw2_expand_symtabs_matching
4733 (struct objfile *objfile,
4734 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4735 const lookup_name_info *lookup_name,
4736 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4737 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4738 enum search_domain kind)
4739 {
4740 struct dwarf2_per_objfile *dwarf2_per_objfile
4741 = get_dwarf2_per_objfile (objfile);
4742
4743 /* index_table is NULL if OBJF_READNOW. */
4744 if (!dwarf2_per_objfile->per_bfd->index_table)
4745 return;
4746
4747 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4748
4749 if (symbol_matcher == NULL && lookup_name == NULL)
4750 {
4751 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4752 {
4753 QUIT;
4754
4755 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
4756 file_matcher, expansion_notify);
4757 }
4758 return;
4759 }
4760
4761 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4762
4763 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4764 symbol_matcher,
4765 kind, [&] (offset_type idx)
4766 {
4767 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4768 expansion_notify, kind);
4769 return true;
4770 });
4771 }
4772
4773 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4774 symtab. */
4775
4776 static struct compunit_symtab *
4777 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4778 CORE_ADDR pc)
4779 {
4780 int i;
4781
4782 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4783 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4784 return cust;
4785
4786 if (cust->includes == NULL)
4787 return NULL;
4788
4789 for (i = 0; cust->includes[i]; ++i)
4790 {
4791 struct compunit_symtab *s = cust->includes[i];
4792
4793 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4794 if (s != NULL)
4795 return s;
4796 }
4797
4798 return NULL;
4799 }
4800
4801 static struct compunit_symtab *
4802 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4803 struct bound_minimal_symbol msymbol,
4804 CORE_ADDR pc,
4805 struct obj_section *section,
4806 int warn_if_readin)
4807 {
4808 struct dwarf2_per_cu_data *data;
4809 struct compunit_symtab *result;
4810
4811 if (!objfile->partial_symtabs->psymtabs_addrmap)
4812 return NULL;
4813
4814 CORE_ADDR baseaddr = objfile->text_section_offset ();
4815 data = (struct dwarf2_per_cu_data *) addrmap_find
4816 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4817 if (!data)
4818 return NULL;
4819
4820 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4821 if (warn_if_readin && per_objfile->symtab_set_p (data))
4822 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4823 paddress (objfile->arch (), pc));
4824
4825 result = recursively_find_pc_sect_compunit_symtab
4826 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4827
4828 gdb_assert (result != NULL);
4829 return result;
4830 }
4831
4832 static void
4833 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4834 void *data, int need_fullname)
4835 {
4836 struct dwarf2_per_objfile *dwarf2_per_objfile
4837 = get_dwarf2_per_objfile (objfile);
4838
4839 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
4840 {
4841 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
4842
4843 htab_up visited (htab_create_alloc (10,
4844 htab_hash_pointer, htab_eq_pointer,
4845 NULL, xcalloc, xfree));
4846
4847 /* The rule is CUs specify all the files, including those used
4848 by any TU, so there's no need to scan TUs here. We can
4849 ignore file names coming from already-expanded CUs. */
4850
4851 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4852 {
4853 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4854 {
4855 void **slot = htab_find_slot (visited.get (),
4856 per_cu->v.quick->file_names,
4857 INSERT);
4858
4859 *slot = per_cu->v.quick->file_names;
4860 }
4861 }
4862
4863 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4864 {
4865 /* We only need to look at symtabs not already expanded. */
4866 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4867 continue;
4868
4869 quick_file_names *file_data
4870 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4871 if (file_data == NULL)
4872 continue;
4873
4874 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4875 if (*slot)
4876 {
4877 /* Already visited. */
4878 continue;
4879 }
4880 *slot = file_data;
4881
4882 for (int j = 0; j < file_data->num_file_names; ++j)
4883 {
4884 const char *filename = file_data->file_names[j];
4885 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
4886 }
4887 }
4888 }
4889
4890 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4891 {
4892 gdb::unique_xmalloc_ptr<char> this_real_name;
4893
4894 if (need_fullname)
4895 this_real_name = gdb_realpath (filename);
4896 (*fun) (filename, this_real_name.get (), data);
4897 });
4898 }
4899
4900 static int
4901 dw2_has_symbols (struct objfile *objfile)
4902 {
4903 return 1;
4904 }
4905
4906 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4907 {
4908 dw2_has_symbols,
4909 dw2_find_last_source_symtab,
4910 dw2_forget_cached_source_info,
4911 dw2_map_symtabs_matching_filename,
4912 dw2_lookup_symbol,
4913 NULL,
4914 dw2_print_stats,
4915 dw2_dump,
4916 dw2_expand_symtabs_for_function,
4917 dw2_expand_all_symtabs,
4918 dw2_expand_symtabs_with_fullname,
4919 dw2_map_matching_symbols,
4920 dw2_expand_symtabs_matching,
4921 dw2_find_pc_sect_compunit_symtab,
4922 NULL,
4923 dw2_map_symbol_filenames
4924 };
4925
4926 /* DWARF-5 debug_names reader. */
4927
4928 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4929 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4930
4931 /* A helper function that reads the .debug_names section in SECTION
4932 and fills in MAP. FILENAME is the name of the file containing the
4933 section; it is used for error reporting.
4934
4935 Returns true if all went well, false otherwise. */
4936
4937 static bool
4938 read_debug_names_from_section (struct objfile *objfile,
4939 const char *filename,
4940 struct dwarf2_section_info *section,
4941 mapped_debug_names &map)
4942 {
4943 if (section->empty ())
4944 return false;
4945
4946 /* Older elfutils strip versions could keep the section in the main
4947 executable while splitting it for the separate debug info file. */
4948 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4949 return false;
4950
4951 section->read (objfile);
4952
4953 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4954
4955 const gdb_byte *addr = section->buffer;
4956
4957 bfd *const abfd = section->get_bfd_owner ();
4958
4959 unsigned int bytes_read;
4960 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4961 addr += bytes_read;
4962
4963 map.dwarf5_is_dwarf64 = bytes_read != 4;
4964 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4965 if (bytes_read + length != section->size)
4966 {
4967 /* There may be multiple per-CU indices. */
4968 warning (_("Section .debug_names in %s length %s does not match "
4969 "section length %s, ignoring .debug_names."),
4970 filename, plongest (bytes_read + length),
4971 pulongest (section->size));
4972 return false;
4973 }
4974
4975 /* The version number. */
4976 uint16_t version = read_2_bytes (abfd, addr);
4977 addr += 2;
4978 if (version != 5)
4979 {
4980 warning (_("Section .debug_names in %s has unsupported version %d, "
4981 "ignoring .debug_names."),
4982 filename, version);
4983 return false;
4984 }
4985
4986 /* Padding. */
4987 uint16_t padding = read_2_bytes (abfd, addr);
4988 addr += 2;
4989 if (padding != 0)
4990 {
4991 warning (_("Section .debug_names in %s has unsupported padding %d, "
4992 "ignoring .debug_names."),
4993 filename, padding);
4994 return false;
4995 }
4996
4997 /* comp_unit_count - The number of CUs in the CU list. */
4998 map.cu_count = read_4_bytes (abfd, addr);
4999 addr += 4;
5000
5001 /* local_type_unit_count - The number of TUs in the local TU
5002 list. */
5003 map.tu_count = read_4_bytes (abfd, addr);
5004 addr += 4;
5005
5006 /* foreign_type_unit_count - The number of TUs in the foreign TU
5007 list. */
5008 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5009 addr += 4;
5010 if (foreign_tu_count != 0)
5011 {
5012 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5013 "ignoring .debug_names."),
5014 filename, static_cast<unsigned long> (foreign_tu_count));
5015 return false;
5016 }
5017
5018 /* bucket_count - The number of hash buckets in the hash lookup
5019 table. */
5020 map.bucket_count = read_4_bytes (abfd, addr);
5021 addr += 4;
5022
5023 /* name_count - The number of unique names in the index. */
5024 map.name_count = read_4_bytes (abfd, addr);
5025 addr += 4;
5026
5027 /* abbrev_table_size - The size in bytes of the abbreviations
5028 table. */
5029 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5030 addr += 4;
5031
5032 /* augmentation_string_size - The size in bytes of the augmentation
5033 string. This value is rounded up to a multiple of 4. */
5034 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5035 addr += 4;
5036 map.augmentation_is_gdb = ((augmentation_string_size
5037 == sizeof (dwarf5_augmentation))
5038 && memcmp (addr, dwarf5_augmentation,
5039 sizeof (dwarf5_augmentation)) == 0);
5040 augmentation_string_size += (-augmentation_string_size) & 3;
5041 addr += augmentation_string_size;
5042
5043 /* List of CUs */
5044 map.cu_table_reordered = addr;
5045 addr += map.cu_count * map.offset_size;
5046
5047 /* List of Local TUs */
5048 map.tu_table_reordered = addr;
5049 addr += map.tu_count * map.offset_size;
5050
5051 /* Hash Lookup Table */
5052 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5053 addr += map.bucket_count * 4;
5054 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5055 addr += map.name_count * 4;
5056
5057 /* Name Table */
5058 map.name_table_string_offs_reordered = addr;
5059 addr += map.name_count * map.offset_size;
5060 map.name_table_entry_offs_reordered = addr;
5061 addr += map.name_count * map.offset_size;
5062
5063 const gdb_byte *abbrev_table_start = addr;
5064 for (;;)
5065 {
5066 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5067 addr += bytes_read;
5068 if (index_num == 0)
5069 break;
5070
5071 const auto insertpair
5072 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5073 if (!insertpair.second)
5074 {
5075 warning (_("Section .debug_names in %s has duplicate index %s, "
5076 "ignoring .debug_names."),
5077 filename, pulongest (index_num));
5078 return false;
5079 }
5080 mapped_debug_names::index_val &indexval = insertpair.first->second;
5081 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5082 addr += bytes_read;
5083
5084 for (;;)
5085 {
5086 mapped_debug_names::index_val::attr attr;
5087 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5088 addr += bytes_read;
5089 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5090 addr += bytes_read;
5091 if (attr.form == DW_FORM_implicit_const)
5092 {
5093 attr.implicit_const = read_signed_leb128 (abfd, addr,
5094 &bytes_read);
5095 addr += bytes_read;
5096 }
5097 if (attr.dw_idx == 0 && attr.form == 0)
5098 break;
5099 indexval.attr_vec.push_back (std::move (attr));
5100 }
5101 }
5102 if (addr != abbrev_table_start + abbrev_table_size)
5103 {
5104 warning (_("Section .debug_names in %s has abbreviation_table "
5105 "of size %s vs. written as %u, ignoring .debug_names."),
5106 filename, plongest (addr - abbrev_table_start),
5107 abbrev_table_size);
5108 return false;
5109 }
5110 map.entry_pool = addr;
5111
5112 return true;
5113 }
5114
5115 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5116 list. */
5117
5118 static void
5119 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5120 const mapped_debug_names &map,
5121 dwarf2_section_info &section,
5122 bool is_dwz)
5123 {
5124 if (!map.augmentation_is_gdb)
5125 {
5126 for (uint32_t i = 0; i < map.cu_count; ++i)
5127 {
5128 sect_offset sect_off
5129 = (sect_offset) (extract_unsigned_integer
5130 (map.cu_table_reordered + i * map.offset_size,
5131 map.offset_size,
5132 map.dwarf5_byte_order));
5133 /* We don't know the length of the CU, because the CU list in a
5134 .debug_names index can be incomplete, so we can't use the start of
5135 the next CU as end of this CU. We create the CUs here with length 0,
5136 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5137 dwarf2_per_cu_data *per_cu
5138 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5139 sect_off, 0);
5140 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
5141 }
5142 }
5143
5144 sect_offset sect_off_prev;
5145 for (uint32_t i = 0; i <= map.cu_count; ++i)
5146 {
5147 sect_offset sect_off_next;
5148 if (i < map.cu_count)
5149 {
5150 sect_off_next
5151 = (sect_offset) (extract_unsigned_integer
5152 (map.cu_table_reordered + i * map.offset_size,
5153 map.offset_size,
5154 map.dwarf5_byte_order));
5155 }
5156 else
5157 sect_off_next = (sect_offset) section.size;
5158 if (i >= 1)
5159 {
5160 const ULONGEST length = sect_off_next - sect_off_prev;
5161 dwarf2_per_cu_data *per_cu
5162 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5163 sect_off_prev, length);
5164 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
5165 }
5166 sect_off_prev = sect_off_next;
5167 }
5168 }
5169
5170 /* Read the CU list from the mapped index, and use it to create all
5171 the CU objects for this dwarf2_per_objfile. */
5172
5173 static void
5174 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5175 const mapped_debug_names &map,
5176 const mapped_debug_names &dwz_map)
5177 {
5178 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
5179 dwarf2_per_objfile->per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5180
5181 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5182 dwarf2_per_objfile->per_bfd->info,
5183 false /* is_dwz */);
5184
5185 if (dwz_map.cu_count == 0)
5186 return;
5187
5188 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
5189 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5190 true /* is_dwz */);
5191 }
5192
5193 /* Read .debug_names. If everything went ok, initialize the "quick"
5194 elements of all the CUs and return true. Otherwise, return false. */
5195
5196 static bool
5197 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5198 {
5199 std::unique_ptr<mapped_debug_names> map
5200 (new mapped_debug_names (dwarf2_per_objfile));
5201 mapped_debug_names dwz_map (dwarf2_per_objfile);
5202 struct objfile *objfile = dwarf2_per_objfile->objfile;
5203
5204 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5205 &dwarf2_per_objfile->per_bfd->debug_names,
5206 *map))
5207 return false;
5208
5209 /* Don't use the index if it's empty. */
5210 if (map->name_count == 0)
5211 return false;
5212
5213 /* If there is a .dwz file, read it so we can get its CU list as
5214 well. */
5215 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
5216 if (dwz != NULL)
5217 {
5218 if (!read_debug_names_from_section (objfile,
5219 bfd_get_filename (dwz->dwz_bfd.get ()),
5220 &dwz->debug_names, dwz_map))
5221 {
5222 warning (_("could not read '.debug_names' section from %s; skipping"),
5223 bfd_get_filename (dwz->dwz_bfd.get ()));
5224 return false;
5225 }
5226 }
5227
5228 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5229
5230 if (map->tu_count != 0)
5231 {
5232 /* We can only handle a single .debug_types when we have an
5233 index. */
5234 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
5235 return false;
5236
5237 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
5238
5239 create_signatured_type_table_from_debug_names
5240 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
5241 }
5242
5243 create_addrmap_from_aranges (dwarf2_per_objfile,
5244 &dwarf2_per_objfile->per_bfd->debug_aranges);
5245
5246 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5247 dwarf2_per_objfile->per_bfd->using_index = 1;
5248 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5249 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5250
5251 return true;
5252 }
5253
5254 /* Type used to manage iterating over all CUs looking for a symbol for
5255 .debug_names. */
5256
5257 class dw2_debug_names_iterator
5258 {
5259 public:
5260 dw2_debug_names_iterator (const mapped_debug_names &map,
5261 gdb::optional<block_enum> block_index,
5262 domain_enum domain,
5263 const char *name)
5264 : m_map (map), m_block_index (block_index), m_domain (domain),
5265 m_addr (find_vec_in_debug_names (map, name))
5266 {}
5267
5268 dw2_debug_names_iterator (const mapped_debug_names &map,
5269 search_domain search, uint32_t namei)
5270 : m_map (map),
5271 m_search (search),
5272 m_addr (find_vec_in_debug_names (map, namei))
5273 {}
5274
5275 dw2_debug_names_iterator (const mapped_debug_names &map,
5276 block_enum block_index, domain_enum domain,
5277 uint32_t namei)
5278 : m_map (map), m_block_index (block_index), m_domain (domain),
5279 m_addr (find_vec_in_debug_names (map, namei))
5280 {}
5281
5282 /* Return the next matching CU or NULL if there are no more. */
5283 dwarf2_per_cu_data *next ();
5284
5285 private:
5286 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5287 const char *name);
5288 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5289 uint32_t namei);
5290
5291 /* The internalized form of .debug_names. */
5292 const mapped_debug_names &m_map;
5293
5294 /* If set, only look for symbols that match that block. Valid values are
5295 GLOBAL_BLOCK and STATIC_BLOCK. */
5296 const gdb::optional<block_enum> m_block_index;
5297
5298 /* The kind of symbol we're looking for. */
5299 const domain_enum m_domain = UNDEF_DOMAIN;
5300 const search_domain m_search = ALL_DOMAIN;
5301
5302 /* The list of CUs from the index entry of the symbol, or NULL if
5303 not found. */
5304 const gdb_byte *m_addr;
5305 };
5306
5307 const char *
5308 mapped_debug_names::namei_to_name (uint32_t namei) const
5309 {
5310 const ULONGEST namei_string_offs
5311 = extract_unsigned_integer ((name_table_string_offs_reordered
5312 + namei * offset_size),
5313 offset_size,
5314 dwarf5_byte_order);
5315 return read_indirect_string_at_offset (dwarf2_per_objfile,
5316 namei_string_offs);
5317 }
5318
5319 /* Find a slot in .debug_names for the object named NAME. If NAME is
5320 found, return pointer to its pool data. If NAME cannot be found,
5321 return NULL. */
5322
5323 const gdb_byte *
5324 dw2_debug_names_iterator::find_vec_in_debug_names
5325 (const mapped_debug_names &map, const char *name)
5326 {
5327 int (*cmp) (const char *, const char *);
5328
5329 gdb::unique_xmalloc_ptr<char> without_params;
5330 if (current_language->la_language == language_cplus
5331 || current_language->la_language == language_fortran
5332 || current_language->la_language == language_d)
5333 {
5334 /* NAME is already canonical. Drop any qualifiers as
5335 .debug_names does not contain any. */
5336
5337 if (strchr (name, '(') != NULL)
5338 {
5339 without_params = cp_remove_params (name);
5340 if (without_params != NULL)
5341 name = without_params.get ();
5342 }
5343 }
5344
5345 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5346
5347 const uint32_t full_hash = dwarf5_djb_hash (name);
5348 uint32_t namei
5349 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5350 (map.bucket_table_reordered
5351 + (full_hash % map.bucket_count)), 4,
5352 map.dwarf5_byte_order);
5353 if (namei == 0)
5354 return NULL;
5355 --namei;
5356 if (namei >= map.name_count)
5357 {
5358 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5359 "[in module %s]"),
5360 namei, map.name_count,
5361 objfile_name (map.dwarf2_per_objfile->objfile));
5362 return NULL;
5363 }
5364
5365 for (;;)
5366 {
5367 const uint32_t namei_full_hash
5368 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5369 (map.hash_table_reordered + namei), 4,
5370 map.dwarf5_byte_order);
5371 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5372 return NULL;
5373
5374 if (full_hash == namei_full_hash)
5375 {
5376 const char *const namei_string = map.namei_to_name (namei);
5377
5378 #if 0 /* An expensive sanity check. */
5379 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5380 {
5381 complaint (_("Wrong .debug_names hash for string at index %u "
5382 "[in module %s]"),
5383 namei, objfile_name (dwarf2_per_objfile->objfile));
5384 return NULL;
5385 }
5386 #endif
5387
5388 if (cmp (namei_string, name) == 0)
5389 {
5390 const ULONGEST namei_entry_offs
5391 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5392 + namei * map.offset_size),
5393 map.offset_size, map.dwarf5_byte_order);
5394 return map.entry_pool + namei_entry_offs;
5395 }
5396 }
5397
5398 ++namei;
5399 if (namei >= map.name_count)
5400 return NULL;
5401 }
5402 }
5403
5404 const gdb_byte *
5405 dw2_debug_names_iterator::find_vec_in_debug_names
5406 (const mapped_debug_names &map, uint32_t namei)
5407 {
5408 if (namei >= map.name_count)
5409 {
5410 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5411 "[in module %s]"),
5412 namei, map.name_count,
5413 objfile_name (map.dwarf2_per_objfile->objfile));
5414 return NULL;
5415 }
5416
5417 const ULONGEST namei_entry_offs
5418 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5419 + namei * map.offset_size),
5420 map.offset_size, map.dwarf5_byte_order);
5421 return map.entry_pool + namei_entry_offs;
5422 }
5423
5424 /* See dw2_debug_names_iterator. */
5425
5426 dwarf2_per_cu_data *
5427 dw2_debug_names_iterator::next ()
5428 {
5429 if (m_addr == NULL)
5430 return NULL;
5431
5432 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5433 struct objfile *objfile = dwarf2_per_objfile->objfile;
5434 bfd *const abfd = objfile->obfd;
5435
5436 again:
5437
5438 unsigned int bytes_read;
5439 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5440 m_addr += bytes_read;
5441 if (abbrev == 0)
5442 return NULL;
5443
5444 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5445 if (indexval_it == m_map.abbrev_map.cend ())
5446 {
5447 complaint (_("Wrong .debug_names undefined abbrev code %s "
5448 "[in module %s]"),
5449 pulongest (abbrev), objfile_name (objfile));
5450 return NULL;
5451 }
5452 const mapped_debug_names::index_val &indexval = indexval_it->second;
5453 enum class symbol_linkage {
5454 unknown,
5455 static_,
5456 extern_,
5457 } symbol_linkage_ = symbol_linkage::unknown;
5458 dwarf2_per_cu_data *per_cu = NULL;
5459 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5460 {
5461 ULONGEST ull;
5462 switch (attr.form)
5463 {
5464 case DW_FORM_implicit_const:
5465 ull = attr.implicit_const;
5466 break;
5467 case DW_FORM_flag_present:
5468 ull = 1;
5469 break;
5470 case DW_FORM_udata:
5471 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5472 m_addr += bytes_read;
5473 break;
5474 case DW_FORM_ref4:
5475 ull = read_4_bytes (abfd, m_addr);
5476 m_addr += 4;
5477 break;
5478 case DW_FORM_ref8:
5479 ull = read_8_bytes (abfd, m_addr);
5480 m_addr += 8;
5481 break;
5482 case DW_FORM_ref_sig8:
5483 ull = read_8_bytes (abfd, m_addr);
5484 m_addr += 8;
5485 break;
5486 default:
5487 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5488 dwarf_form_name (attr.form),
5489 objfile_name (objfile));
5490 return NULL;
5491 }
5492 switch (attr.dw_idx)
5493 {
5494 case DW_IDX_compile_unit:
5495 /* Don't crash on bad data. */
5496 if (ull >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
5497 {
5498 complaint (_(".debug_names entry has bad CU index %s"
5499 " [in module %s]"),
5500 pulongest (ull),
5501 objfile_name (dwarf2_per_objfile->objfile));
5502 continue;
5503 }
5504 per_cu = dwarf2_per_objfile->per_bfd->get_cutu (ull);
5505 break;
5506 case DW_IDX_type_unit:
5507 /* Don't crash on bad data. */
5508 if (ull >= dwarf2_per_objfile->per_bfd->all_type_units.size ())
5509 {
5510 complaint (_(".debug_names entry has bad TU index %s"
5511 " [in module %s]"),
5512 pulongest (ull),
5513 objfile_name (dwarf2_per_objfile->objfile));
5514 continue;
5515 }
5516 per_cu = &dwarf2_per_objfile->per_bfd->get_tu (ull)->per_cu;
5517 break;
5518 case DW_IDX_die_offset:
5519 /* In a per-CU index (as opposed to a per-module index), index
5520 entries without CU attribute implicitly refer to the single CU. */
5521 if (per_cu == NULL)
5522 per_cu = dwarf2_per_objfile->per_bfd->get_cu (0);
5523 break;
5524 case DW_IDX_GNU_internal:
5525 if (!m_map.augmentation_is_gdb)
5526 break;
5527 symbol_linkage_ = symbol_linkage::static_;
5528 break;
5529 case DW_IDX_GNU_external:
5530 if (!m_map.augmentation_is_gdb)
5531 break;
5532 symbol_linkage_ = symbol_linkage::extern_;
5533 break;
5534 }
5535 }
5536
5537 /* Skip if already read in. */
5538 if (dwarf2_per_objfile->symtab_set_p (per_cu))
5539 goto again;
5540
5541 /* Check static vs global. */
5542 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5543 {
5544 const bool want_static = *m_block_index == STATIC_BLOCK;
5545 const bool symbol_is_static =
5546 symbol_linkage_ == symbol_linkage::static_;
5547 if (want_static != symbol_is_static)
5548 goto again;
5549 }
5550
5551 /* Match dw2_symtab_iter_next, symbol_kind
5552 and debug_names::psymbol_tag. */
5553 switch (m_domain)
5554 {
5555 case VAR_DOMAIN:
5556 switch (indexval.dwarf_tag)
5557 {
5558 case DW_TAG_variable:
5559 case DW_TAG_subprogram:
5560 /* Some types are also in VAR_DOMAIN. */
5561 case DW_TAG_typedef:
5562 case DW_TAG_structure_type:
5563 break;
5564 default:
5565 goto again;
5566 }
5567 break;
5568 case STRUCT_DOMAIN:
5569 switch (indexval.dwarf_tag)
5570 {
5571 case DW_TAG_typedef:
5572 case DW_TAG_structure_type:
5573 break;
5574 default:
5575 goto again;
5576 }
5577 break;
5578 case LABEL_DOMAIN:
5579 switch (indexval.dwarf_tag)
5580 {
5581 case 0:
5582 case DW_TAG_variable:
5583 break;
5584 default:
5585 goto again;
5586 }
5587 break;
5588 case MODULE_DOMAIN:
5589 switch (indexval.dwarf_tag)
5590 {
5591 case DW_TAG_module:
5592 break;
5593 default:
5594 goto again;
5595 }
5596 break;
5597 default:
5598 break;
5599 }
5600
5601 /* Match dw2_expand_symtabs_matching, symbol_kind and
5602 debug_names::psymbol_tag. */
5603 switch (m_search)
5604 {
5605 case VARIABLES_DOMAIN:
5606 switch (indexval.dwarf_tag)
5607 {
5608 case DW_TAG_variable:
5609 break;
5610 default:
5611 goto again;
5612 }
5613 break;
5614 case FUNCTIONS_DOMAIN:
5615 switch (indexval.dwarf_tag)
5616 {
5617 case DW_TAG_subprogram:
5618 break;
5619 default:
5620 goto again;
5621 }
5622 break;
5623 case TYPES_DOMAIN:
5624 switch (indexval.dwarf_tag)
5625 {
5626 case DW_TAG_typedef:
5627 case DW_TAG_structure_type:
5628 break;
5629 default:
5630 goto again;
5631 }
5632 break;
5633 case MODULES_DOMAIN:
5634 switch (indexval.dwarf_tag)
5635 {
5636 case DW_TAG_module:
5637 break;
5638 default:
5639 goto again;
5640 }
5641 default:
5642 break;
5643 }
5644
5645 return per_cu;
5646 }
5647
5648 static struct compunit_symtab *
5649 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5650 const char *name, domain_enum domain)
5651 {
5652 struct dwarf2_per_objfile *dwarf2_per_objfile
5653 = get_dwarf2_per_objfile (objfile);
5654
5655 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
5656 if (!mapp)
5657 {
5658 /* index is NULL if OBJF_READNOW. */
5659 return NULL;
5660 }
5661 const auto &map = *mapp;
5662
5663 dw2_debug_names_iterator iter (map, block_index, domain, name);
5664
5665 struct compunit_symtab *stab_best = NULL;
5666 struct dwarf2_per_cu_data *per_cu;
5667 while ((per_cu = iter.next ()) != NULL)
5668 {
5669 struct symbol *sym, *with_opaque = NULL;
5670 compunit_symtab *stab
5671 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5672 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5673 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5674
5675 sym = block_find_symbol (block, name, domain,
5676 block_find_non_opaque_type_preferred,
5677 &with_opaque);
5678
5679 /* Some caution must be observed with overloaded functions and
5680 methods, since the index will not contain any overload
5681 information (but NAME might contain it). */
5682
5683 if (sym != NULL
5684 && strcmp_iw (sym->search_name (), name) == 0)
5685 return stab;
5686 if (with_opaque != NULL
5687 && strcmp_iw (with_opaque->search_name (), name) == 0)
5688 stab_best = stab;
5689
5690 /* Keep looking through other CUs. */
5691 }
5692
5693 return stab_best;
5694 }
5695
5696 /* This dumps minimal information about .debug_names. It is called
5697 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5698 uses this to verify that .debug_names has been loaded. */
5699
5700 static void
5701 dw2_debug_names_dump (struct objfile *objfile)
5702 {
5703 struct dwarf2_per_objfile *dwarf2_per_objfile
5704 = get_dwarf2_per_objfile (objfile);
5705
5706 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
5707 printf_filtered (".debug_names:");
5708 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5709 printf_filtered (" exists\n");
5710 else
5711 printf_filtered (" faked for \"readnow\"\n");
5712 printf_filtered ("\n");
5713 }
5714
5715 static void
5716 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5717 const char *func_name)
5718 {
5719 struct dwarf2_per_objfile *dwarf2_per_objfile
5720 = get_dwarf2_per_objfile (objfile);
5721
5722 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5723 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5724 {
5725 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5726
5727 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5728
5729 struct dwarf2_per_cu_data *per_cu;
5730 while ((per_cu = iter.next ()) != NULL)
5731 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5732 }
5733 }
5734
5735 static void
5736 dw2_debug_names_map_matching_symbols
5737 (struct objfile *objfile,
5738 const lookup_name_info &name, domain_enum domain,
5739 int global,
5740 gdb::function_view<symbol_found_callback_ftype> callback,
5741 symbol_compare_ftype *ordered_compare)
5742 {
5743 struct dwarf2_per_objfile *dwarf2_per_objfile
5744 = get_dwarf2_per_objfile (objfile);
5745
5746 /* debug_names_table is NULL if OBJF_READNOW. */
5747 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5748 return;
5749
5750 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5751 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5752
5753 const char *match_name = name.ada ().lookup_name ().c_str ();
5754 auto matcher = [&] (const char *symname)
5755 {
5756 if (ordered_compare == nullptr)
5757 return true;
5758 return ordered_compare (symname, match_name) == 0;
5759 };
5760
5761 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5762 [&] (offset_type namei)
5763 {
5764 /* The name was matched, now expand corresponding CUs that were
5765 marked. */
5766 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5767
5768 struct dwarf2_per_cu_data *per_cu;
5769 while ((per_cu = iter.next ()) != NULL)
5770 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
5771 nullptr);
5772 return true;
5773 });
5774
5775 /* It's a shame we couldn't do this inside the
5776 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5777 that have already been expanded. Instead, this loop matches what
5778 the psymtab code does. */
5779 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5780 {
5781 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5782 if (symtab != nullptr)
5783 {
5784 const struct block *block
5785 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5786 if (!iterate_over_symbols_terminated (block, name,
5787 domain, callback))
5788 break;
5789 }
5790 }
5791 }
5792
5793 static void
5794 dw2_debug_names_expand_symtabs_matching
5795 (struct objfile *objfile,
5796 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5797 const lookup_name_info *lookup_name,
5798 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5799 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5800 enum search_domain kind)
5801 {
5802 struct dwarf2_per_objfile *dwarf2_per_objfile
5803 = get_dwarf2_per_objfile (objfile);
5804
5805 /* debug_names_table is NULL if OBJF_READNOW. */
5806 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5807 return;
5808
5809 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5810
5811 if (symbol_matcher == NULL && lookup_name == NULL)
5812 {
5813 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5814 {
5815 QUIT;
5816
5817 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5818 file_matcher, expansion_notify);
5819 }
5820 return;
5821 }
5822
5823 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5824
5825 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5826 symbol_matcher,
5827 kind, [&] (offset_type namei)
5828 {
5829 /* The name was matched, now expand corresponding CUs that were
5830 marked. */
5831 dw2_debug_names_iterator iter (map, kind, namei);
5832
5833 struct dwarf2_per_cu_data *per_cu;
5834 while ((per_cu = iter.next ()) != NULL)
5835 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5836 file_matcher, expansion_notify);
5837 return true;
5838 });
5839 }
5840
5841 const struct quick_symbol_functions dwarf2_debug_names_functions =
5842 {
5843 dw2_has_symbols,
5844 dw2_find_last_source_symtab,
5845 dw2_forget_cached_source_info,
5846 dw2_map_symtabs_matching_filename,
5847 dw2_debug_names_lookup_symbol,
5848 NULL,
5849 dw2_print_stats,
5850 dw2_debug_names_dump,
5851 dw2_debug_names_expand_symtabs_for_function,
5852 dw2_expand_all_symtabs,
5853 dw2_expand_symtabs_with_fullname,
5854 dw2_debug_names_map_matching_symbols,
5855 dw2_debug_names_expand_symtabs_matching,
5856 dw2_find_pc_sect_compunit_symtab,
5857 NULL,
5858 dw2_map_symbol_filenames
5859 };
5860
5861 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5862 to either a dwarf2_per_bfd or dwz_file object. */
5863
5864 template <typename T>
5865 static gdb::array_view<const gdb_byte>
5866 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5867 {
5868 dwarf2_section_info *section = &section_owner->gdb_index;
5869
5870 if (section->empty ())
5871 return {};
5872
5873 /* Older elfutils strip versions could keep the section in the main
5874 executable while splitting it for the separate debug info file. */
5875 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5876 return {};
5877
5878 section->read (obj);
5879
5880 /* dwarf2_section_info::size is a bfd_size_type, while
5881 gdb::array_view works with size_t. On 32-bit hosts, with
5882 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5883 is 32-bit. So we need an explicit narrowing conversion here.
5884 This is fine, because it's impossible to allocate or mmap an
5885 array/buffer larger than what size_t can represent. */
5886 return gdb::make_array_view (section->buffer, section->size);
5887 }
5888
5889 /* Lookup the index cache for the contents of the index associated to
5890 DWARF2_OBJ. */
5891
5892 static gdb::array_view<const gdb_byte>
5893 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5894 {
5895 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5896 if (build_id == nullptr)
5897 return {};
5898
5899 return global_index_cache.lookup_gdb_index (build_id,
5900 &dwarf2_per_bfd->index_cache_res);
5901 }
5902
5903 /* Same as the above, but for DWZ. */
5904
5905 static gdb::array_view<const gdb_byte>
5906 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5907 {
5908 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5909 if (build_id == nullptr)
5910 return {};
5911
5912 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5913 }
5914
5915 /* See symfile.h. */
5916
5917 bool
5918 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5919 {
5920 struct dwarf2_per_objfile *dwarf2_per_objfile
5921 = get_dwarf2_per_objfile (objfile);
5922
5923 /* If we're about to read full symbols, don't bother with the
5924 indices. In this case we also don't care if some other debug
5925 format is making psymtabs, because they are all about to be
5926 expanded anyway. */
5927 if ((objfile->flags & OBJF_READNOW))
5928 {
5929 dwarf2_per_objfile->per_bfd->using_index = 1;
5930 create_all_comp_units (dwarf2_per_objfile);
5931 create_all_type_units (dwarf2_per_objfile);
5932 dwarf2_per_objfile->per_bfd->quick_file_names_table
5933 = create_quick_file_names_table
5934 (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5935 dwarf2_per_objfile->resize_symtabs ();
5936
5937 for (int i = 0; i < (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
5938 + dwarf2_per_objfile->per_bfd->all_type_units.size ()); ++i)
5939 {
5940 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
5941
5942 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
5943 struct dwarf2_per_cu_quick_data);
5944 }
5945
5946 /* Return 1 so that gdb sees the "quick" functions. However,
5947 these functions will be no-ops because we will have expanded
5948 all symtabs. */
5949 *index_kind = dw_index_kind::GDB_INDEX;
5950 return true;
5951 }
5952
5953 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5954 {
5955 *index_kind = dw_index_kind::DEBUG_NAMES;
5956 dwarf2_per_objfile->resize_symtabs ();
5957 return true;
5958 }
5959
5960 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5961 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5962 get_gdb_index_contents_from_section<dwz_file>))
5963 {
5964 *index_kind = dw_index_kind::GDB_INDEX;
5965 dwarf2_per_objfile->resize_symtabs ();
5966 return true;
5967 }
5968
5969 /* ... otherwise, try to find the index in the index cache. */
5970 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5971 get_gdb_index_contents_from_cache,
5972 get_gdb_index_contents_from_cache_dwz))
5973 {
5974 global_index_cache.hit ();
5975 *index_kind = dw_index_kind::GDB_INDEX;
5976 dwarf2_per_objfile->resize_symtabs ();
5977 return true;
5978 }
5979
5980 global_index_cache.miss ();
5981 return false;
5982 }
5983
5984 \f
5985
5986 /* Build a partial symbol table. */
5987
5988 void
5989 dwarf2_build_psymtabs (struct objfile *objfile)
5990 {
5991 struct dwarf2_per_objfile *dwarf2_per_objfile
5992 = get_dwarf2_per_objfile (objfile);
5993
5994 init_psymbol_list (objfile, 1024);
5995
5996 try
5997 {
5998 /* This isn't really ideal: all the data we allocate on the
5999 objfile's obstack is still uselessly kept around. However,
6000 freeing it seems unsafe. */
6001 psymtab_discarder psymtabs (objfile);
6002 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6003 psymtabs.keep ();
6004
6005 dwarf2_per_objfile->resize_symtabs ();
6006
6007 /* (maybe) store an index in the cache. */
6008 global_index_cache.store (dwarf2_per_objfile);
6009 }
6010 catch (const gdb_exception_error &except)
6011 {
6012 exception_print (gdb_stderr, except);
6013 }
6014 }
6015
6016 /* Find the base address of the compilation unit for range lists and
6017 location lists. It will normally be specified by DW_AT_low_pc.
6018 In DWARF-3 draft 4, the base address could be overridden by
6019 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6020 compilation units with discontinuous ranges. */
6021
6022 static void
6023 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6024 {
6025 struct attribute *attr;
6026
6027 cu->base_address.reset ();
6028
6029 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6030 if (attr != nullptr)
6031 cu->base_address = attr->value_as_address ();
6032 else
6033 {
6034 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6035 if (attr != nullptr)
6036 cu->base_address = attr->value_as_address ();
6037 }
6038 }
6039
6040 /* Helper function that returns the proper abbrev section for
6041 THIS_CU. */
6042
6043 static struct dwarf2_section_info *
6044 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6045 {
6046 struct dwarf2_section_info *abbrev;
6047 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6048
6049 if (this_cu->is_dwz)
6050 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6051 else
6052 abbrev = &per_bfd->abbrev;
6053
6054 return abbrev;
6055 }
6056
6057 /* Fetch the abbreviation table offset from a comp or type unit header. */
6058
6059 static sect_offset
6060 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6061 struct dwarf2_section_info *section,
6062 sect_offset sect_off)
6063 {
6064 bfd *abfd = section->get_bfd_owner ();
6065 const gdb_byte *info_ptr;
6066 unsigned int initial_length_size, offset_size;
6067 uint16_t version;
6068
6069 section->read (dwarf2_per_objfile->objfile);
6070 info_ptr = section->buffer + to_underlying (sect_off);
6071 read_initial_length (abfd, info_ptr, &initial_length_size);
6072 offset_size = initial_length_size == 4 ? 4 : 8;
6073 info_ptr += initial_length_size;
6074
6075 version = read_2_bytes (abfd, info_ptr);
6076 info_ptr += 2;
6077 if (version >= 5)
6078 {
6079 /* Skip unit type and address size. */
6080 info_ptr += 2;
6081 }
6082
6083 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6084 }
6085
6086 /* A partial symtab that is used only for include files. */
6087 struct dwarf2_include_psymtab : public partial_symtab
6088 {
6089 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6090 : partial_symtab (filename, objfile)
6091 {
6092 }
6093
6094 void read_symtab (struct objfile *objfile) override
6095 {
6096 /* It's an include file, no symbols to read for it.
6097 Everything is in the includer symtab. */
6098
6099 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6100 expansion of the includer psymtab. We use the dependencies[0] field to
6101 model the includer. But if we go the regular route of calling
6102 expand_psymtab here, and having expand_psymtab call expand_dependencies
6103 to expand the includer, we'll only use expand_psymtab on the includer
6104 (making it a non-toplevel psymtab), while if we expand the includer via
6105 another path, we'll use read_symtab (making it a toplevel psymtab).
6106 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6107 psymtab, and trigger read_symtab on the includer here directly. */
6108 includer ()->read_symtab (objfile);
6109 }
6110
6111 void expand_psymtab (struct objfile *objfile) override
6112 {
6113 /* This is not called by read_symtab, and should not be called by any
6114 expand_dependencies. */
6115 gdb_assert (false);
6116 }
6117
6118 bool readin_p (struct objfile *objfile) const override
6119 {
6120 return includer ()->readin_p (objfile);
6121 }
6122
6123 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6124 {
6125 return nullptr;
6126 }
6127
6128 private:
6129 partial_symtab *includer () const
6130 {
6131 /* An include psymtab has exactly one dependency: the psymtab that
6132 includes it. */
6133 gdb_assert (this->number_of_dependencies == 1);
6134 return this->dependencies[0];
6135 }
6136 };
6137
6138 /* Allocate a new partial symtab for file named NAME and mark this new
6139 partial symtab as being an include of PST. */
6140
6141 static void
6142 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6143 struct objfile *objfile)
6144 {
6145 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6146
6147 if (!IS_ABSOLUTE_PATH (subpst->filename))
6148 subpst->dirname = pst->dirname;
6149
6150 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6151 subpst->dependencies[0] = pst;
6152 subpst->number_of_dependencies = 1;
6153 }
6154
6155 /* Read the Line Number Program data and extract the list of files
6156 included by the source file represented by PST. Build an include
6157 partial symtab for each of these included files. */
6158
6159 static void
6160 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6161 struct die_info *die,
6162 dwarf2_psymtab *pst)
6163 {
6164 line_header_up lh;
6165 struct attribute *attr;
6166
6167 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6168 if (attr != nullptr)
6169 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6170 if (lh == NULL)
6171 return; /* No linetable, so no includes. */
6172
6173 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6174 that we pass in the raw text_low here; that is ok because we're
6175 only decoding the line table to make include partial symtabs, and
6176 so the addresses aren't really used. */
6177 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6178 pst->raw_text_low (), 1);
6179 }
6180
6181 static hashval_t
6182 hash_signatured_type (const void *item)
6183 {
6184 const struct signatured_type *sig_type
6185 = (const struct signatured_type *) item;
6186
6187 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6188 return sig_type->signature;
6189 }
6190
6191 static int
6192 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6193 {
6194 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6195 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6196
6197 return lhs->signature == rhs->signature;
6198 }
6199
6200 /* Allocate a hash table for signatured types. */
6201
6202 static htab_up
6203 allocate_signatured_type_table ()
6204 {
6205 return htab_up (htab_create_alloc (41,
6206 hash_signatured_type,
6207 eq_signatured_type,
6208 NULL, xcalloc, xfree));
6209 }
6210
6211 /* A helper function to add a signatured type CU to a table. */
6212
6213 static int
6214 add_signatured_type_cu_to_table (void **slot, void *datum)
6215 {
6216 struct signatured_type *sigt = (struct signatured_type *) *slot;
6217 std::vector<signatured_type *> *all_type_units
6218 = (std::vector<signatured_type *> *) datum;
6219
6220 all_type_units->push_back (sigt);
6221
6222 return 1;
6223 }
6224
6225 /* A helper for create_debug_types_hash_table. Read types from SECTION
6226 and fill them into TYPES_HTAB. It will process only type units,
6227 therefore DW_UT_type. */
6228
6229 static void
6230 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6231 struct dwo_file *dwo_file,
6232 dwarf2_section_info *section, htab_up &types_htab,
6233 rcuh_kind section_kind)
6234 {
6235 struct objfile *objfile = dwarf2_per_objfile->objfile;
6236 struct dwarf2_section_info *abbrev_section;
6237 bfd *abfd;
6238 const gdb_byte *info_ptr, *end_ptr;
6239
6240 abbrev_section = (dwo_file != NULL
6241 ? &dwo_file->sections.abbrev
6242 : &dwarf2_per_objfile->per_bfd->abbrev);
6243
6244 if (dwarf_read_debug)
6245 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6246 section->get_name (),
6247 abbrev_section->get_file_name ());
6248
6249 section->read (objfile);
6250 info_ptr = section->buffer;
6251
6252 if (info_ptr == NULL)
6253 return;
6254
6255 /* We can't set abfd until now because the section may be empty or
6256 not present, in which case the bfd is unknown. */
6257 abfd = section->get_bfd_owner ();
6258
6259 /* We don't use cutu_reader here because we don't need to read
6260 any dies: the signature is in the header. */
6261
6262 end_ptr = info_ptr + section->size;
6263 while (info_ptr < end_ptr)
6264 {
6265 struct signatured_type *sig_type;
6266 struct dwo_unit *dwo_tu;
6267 void **slot;
6268 const gdb_byte *ptr = info_ptr;
6269 struct comp_unit_head header;
6270 unsigned int length;
6271
6272 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6273
6274 /* Initialize it due to a false compiler warning. */
6275 header.signature = -1;
6276 header.type_cu_offset_in_tu = (cu_offset) -1;
6277
6278 /* We need to read the type's signature in order to build the hash
6279 table, but we don't need anything else just yet. */
6280
6281 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6282 abbrev_section, ptr, section_kind);
6283
6284 length = header.get_length ();
6285
6286 /* Skip dummy type units. */
6287 if (ptr >= info_ptr + length
6288 || peek_abbrev_code (abfd, ptr) == 0
6289 || header.unit_type != DW_UT_type)
6290 {
6291 info_ptr += length;
6292 continue;
6293 }
6294
6295 if (types_htab == NULL)
6296 {
6297 if (dwo_file)
6298 types_htab = allocate_dwo_unit_table ();
6299 else
6300 types_htab = allocate_signatured_type_table ();
6301 }
6302
6303 if (dwo_file)
6304 {
6305 sig_type = NULL;
6306 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6307 struct dwo_unit);
6308 dwo_tu->dwo_file = dwo_file;
6309 dwo_tu->signature = header.signature;
6310 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6311 dwo_tu->section = section;
6312 dwo_tu->sect_off = sect_off;
6313 dwo_tu->length = length;
6314 }
6315 else
6316 {
6317 /* N.B.: type_offset is not usable if this type uses a DWO file.
6318 The real type_offset is in the DWO file. */
6319 dwo_tu = NULL;
6320 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6321 sig_type->signature = header.signature;
6322 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6323 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6324 sig_type->per_cu.is_debug_types = 1;
6325 sig_type->per_cu.section = section;
6326 sig_type->per_cu.sect_off = sect_off;
6327 sig_type->per_cu.length = length;
6328 }
6329
6330 slot = htab_find_slot (types_htab.get (),
6331 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6332 INSERT);
6333 gdb_assert (slot != NULL);
6334 if (*slot != NULL)
6335 {
6336 sect_offset dup_sect_off;
6337
6338 if (dwo_file)
6339 {
6340 const struct dwo_unit *dup_tu
6341 = (const struct dwo_unit *) *slot;
6342
6343 dup_sect_off = dup_tu->sect_off;
6344 }
6345 else
6346 {
6347 const struct signatured_type *dup_tu
6348 = (const struct signatured_type *) *slot;
6349
6350 dup_sect_off = dup_tu->per_cu.sect_off;
6351 }
6352
6353 complaint (_("debug type entry at offset %s is duplicate to"
6354 " the entry at offset %s, signature %s"),
6355 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6356 hex_string (header.signature));
6357 }
6358 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6359
6360 if (dwarf_read_debug > 1)
6361 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6362 sect_offset_str (sect_off),
6363 hex_string (header.signature));
6364
6365 info_ptr += length;
6366 }
6367 }
6368
6369 /* Create the hash table of all entries in the .debug_types
6370 (or .debug_types.dwo) section(s).
6371 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6372 otherwise it is NULL.
6373
6374 The result is a pointer to the hash table or NULL if there are no types.
6375
6376 Note: This function processes DWO files only, not DWP files. */
6377
6378 static void
6379 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6380 struct dwo_file *dwo_file,
6381 gdb::array_view<dwarf2_section_info> type_sections,
6382 htab_up &types_htab)
6383 {
6384 for (dwarf2_section_info &section : type_sections)
6385 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6386 types_htab, rcuh_kind::TYPE);
6387 }
6388
6389 /* Create the hash table of all entries in the .debug_types section,
6390 and initialize all_type_units.
6391 The result is zero if there is an error (e.g. missing .debug_types section),
6392 otherwise non-zero. */
6393
6394 static int
6395 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6396 {
6397 htab_up types_htab;
6398
6399 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6400 &dwarf2_per_objfile->per_bfd->info, types_htab,
6401 rcuh_kind::COMPILE);
6402 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6403 dwarf2_per_objfile->per_bfd->types, types_htab);
6404 if (types_htab == NULL)
6405 {
6406 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
6407 return 0;
6408 }
6409
6410 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
6411
6412 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6413 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6414 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
6415
6416 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6417 add_signatured_type_cu_to_table,
6418 &dwarf2_per_objfile->per_bfd->all_type_units);
6419
6420 return 1;
6421 }
6422
6423 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6424 If SLOT is non-NULL, it is the entry to use in the hash table.
6425 Otherwise we find one. */
6426
6427 static struct signatured_type *
6428 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6429 void **slot)
6430 {
6431 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6432 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6433 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6434
6435 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6436
6437 dwarf2_per_objfile->resize_symtabs ();
6438
6439 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6440 sig_type->signature = sig;
6441 sig_type->per_cu.is_debug_types = 1;
6442 if (dwarf2_per_objfile->per_bfd->using_index)
6443 {
6444 sig_type->per_cu.v.quick =
6445 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6446 struct dwarf2_per_cu_quick_data);
6447 }
6448
6449 if (slot == NULL)
6450 {
6451 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6452 sig_type, INSERT);
6453 }
6454 gdb_assert (*slot == NULL);
6455 *slot = sig_type;
6456 /* The rest of sig_type must be filled in by the caller. */
6457 return sig_type;
6458 }
6459
6460 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6461 Fill in SIG_ENTRY with DWO_ENTRY. */
6462
6463 static void
6464 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6465 struct signatured_type *sig_entry,
6466 struct dwo_unit *dwo_entry)
6467 {
6468 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6469
6470 /* Make sure we're not clobbering something we don't expect to. */
6471 gdb_assert (! sig_entry->per_cu.queued);
6472 gdb_assert (sig_entry->per_cu.cu == NULL);
6473 if (per_bfd->using_index)
6474 {
6475 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6476 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6477 }
6478 else
6479 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6480 gdb_assert (sig_entry->signature == dwo_entry->signature);
6481 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6482 gdb_assert (sig_entry->type_unit_group == NULL);
6483 gdb_assert (sig_entry->dwo_unit == NULL);
6484
6485 sig_entry->per_cu.section = dwo_entry->section;
6486 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6487 sig_entry->per_cu.length = dwo_entry->length;
6488 sig_entry->per_cu.reading_dwo_directly = 1;
6489 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6490 sig_entry->per_cu.per_bfd = per_bfd;
6491 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6492 sig_entry->dwo_unit = dwo_entry;
6493 }
6494
6495 /* Subroutine of lookup_signatured_type.
6496 If we haven't read the TU yet, create the signatured_type data structure
6497 for a TU to be read in directly from a DWO file, bypassing the stub.
6498 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6499 using .gdb_index, then when reading a CU we want to stay in the DWO file
6500 containing that CU. Otherwise we could end up reading several other DWO
6501 files (due to comdat folding) to process the transitive closure of all the
6502 mentioned TUs, and that can be slow. The current DWO file will have every
6503 type signature that it needs.
6504 We only do this for .gdb_index because in the psymtab case we already have
6505 to read all the DWOs to build the type unit groups. */
6506
6507 static struct signatured_type *
6508 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6509 {
6510 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6511 struct dwo_file *dwo_file;
6512 struct dwo_unit find_dwo_entry, *dwo_entry;
6513 struct signatured_type find_sig_entry, *sig_entry;
6514 void **slot;
6515
6516 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6517
6518 /* If TU skeletons have been removed then we may not have read in any
6519 TUs yet. */
6520 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6521 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6522
6523 /* We only ever need to read in one copy of a signatured type.
6524 Use the global signatured_types array to do our own comdat-folding
6525 of types. If this is the first time we're reading this TU, and
6526 the TU has an entry in .gdb_index, replace the recorded data from
6527 .gdb_index with this TU. */
6528
6529 find_sig_entry.signature = sig;
6530 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6531 &find_sig_entry, INSERT);
6532 sig_entry = (struct signatured_type *) *slot;
6533
6534 /* We can get here with the TU already read, *or* in the process of being
6535 read. Don't reassign the global entry to point to this DWO if that's
6536 the case. Also note that if the TU is already being read, it may not
6537 have come from a DWO, the program may be a mix of Fission-compiled
6538 code and non-Fission-compiled code. */
6539
6540 /* Have we already tried to read this TU?
6541 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6542 needn't exist in the global table yet). */
6543 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6544 return sig_entry;
6545
6546 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6547 dwo_unit of the TU itself. */
6548 dwo_file = cu->dwo_unit->dwo_file;
6549
6550 /* Ok, this is the first time we're reading this TU. */
6551 if (dwo_file->tus == NULL)
6552 return NULL;
6553 find_dwo_entry.signature = sig;
6554 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6555 &find_dwo_entry);
6556 if (dwo_entry == NULL)
6557 return NULL;
6558
6559 /* If the global table doesn't have an entry for this TU, add one. */
6560 if (sig_entry == NULL)
6561 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6562
6563 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6564 sig_entry->per_cu.tu_read = 1;
6565 return sig_entry;
6566 }
6567
6568 /* Subroutine of lookup_signatured_type.
6569 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6570 then try the DWP file. If the TU stub (skeleton) has been removed then
6571 it won't be in .gdb_index. */
6572
6573 static struct signatured_type *
6574 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6575 {
6576 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6577 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6578 struct dwo_unit *dwo_entry;
6579 struct signatured_type find_sig_entry, *sig_entry;
6580 void **slot;
6581
6582 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6583 gdb_assert (dwp_file != NULL);
6584
6585 /* If TU skeletons have been removed then we may not have read in any
6586 TUs yet. */
6587 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6588 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6589
6590 find_sig_entry.signature = sig;
6591 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6592 &find_sig_entry, INSERT);
6593 sig_entry = (struct signatured_type *) *slot;
6594
6595 /* Have we already tried to read this TU?
6596 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6597 needn't exist in the global table yet). */
6598 if (sig_entry != NULL)
6599 return sig_entry;
6600
6601 if (dwp_file->tus == NULL)
6602 return NULL;
6603 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6604 sig, 1 /* is_debug_types */);
6605 if (dwo_entry == NULL)
6606 return NULL;
6607
6608 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6609 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6610
6611 return sig_entry;
6612 }
6613
6614 /* Lookup a signature based type for DW_FORM_ref_sig8.
6615 Returns NULL if signature SIG is not present in the table.
6616 It is up to the caller to complain about this. */
6617
6618 static struct signatured_type *
6619 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6620 {
6621 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6622
6623 if (cu->dwo_unit
6624 && dwarf2_per_objfile->per_bfd->using_index)
6625 {
6626 /* We're in a DWO/DWP file, and we're using .gdb_index.
6627 These cases require special processing. */
6628 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6629 return lookup_dwo_signatured_type (cu, sig);
6630 else
6631 return lookup_dwp_signatured_type (cu, sig);
6632 }
6633 else
6634 {
6635 struct signatured_type find_entry, *entry;
6636
6637 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6638 return NULL;
6639 find_entry.signature = sig;
6640 entry = ((struct signatured_type *)
6641 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6642 &find_entry));
6643 return entry;
6644 }
6645 }
6646
6647 /* Low level DIE reading support. */
6648
6649 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6650
6651 static void
6652 init_cu_die_reader (struct die_reader_specs *reader,
6653 struct dwarf2_cu *cu,
6654 struct dwarf2_section_info *section,
6655 struct dwo_file *dwo_file,
6656 struct abbrev_table *abbrev_table)
6657 {
6658 gdb_assert (section->readin && section->buffer != NULL);
6659 reader->abfd = section->get_bfd_owner ();
6660 reader->cu = cu;
6661 reader->dwo_file = dwo_file;
6662 reader->die_section = section;
6663 reader->buffer = section->buffer;
6664 reader->buffer_end = section->buffer + section->size;
6665 reader->abbrev_table = abbrev_table;
6666 }
6667
6668 /* Subroutine of cutu_reader to simplify it.
6669 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6670 There's just a lot of work to do, and cutu_reader is big enough
6671 already.
6672
6673 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6674 from it to the DIE in the DWO. If NULL we are skipping the stub.
6675 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6676 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6677 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6678 STUB_COMP_DIR may be non-NULL.
6679 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6680 are filled in with the info of the DIE from the DWO file.
6681 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6682 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6683 kept around for at least as long as *RESULT_READER.
6684
6685 The result is non-zero if a valid (non-dummy) DIE was found. */
6686
6687 static int
6688 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6689 struct dwo_unit *dwo_unit,
6690 struct die_info *stub_comp_unit_die,
6691 const char *stub_comp_dir,
6692 struct die_reader_specs *result_reader,
6693 const gdb_byte **result_info_ptr,
6694 struct die_info **result_comp_unit_die,
6695 abbrev_table_up *result_dwo_abbrev_table)
6696 {
6697 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6698 struct objfile *objfile = dwarf2_per_objfile->objfile;
6699 struct dwarf2_cu *cu = this_cu->cu;
6700 bfd *abfd;
6701 const gdb_byte *begin_info_ptr, *info_ptr;
6702 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6703 int i,num_extra_attrs;
6704 struct dwarf2_section_info *dwo_abbrev_section;
6705 struct die_info *comp_unit_die;
6706
6707 /* At most one of these may be provided. */
6708 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6709
6710 /* These attributes aren't processed until later:
6711 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6712 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6713 referenced later. However, these attributes are found in the stub
6714 which we won't have later. In order to not impose this complication
6715 on the rest of the code, we read them here and copy them to the
6716 DWO CU/TU die. */
6717
6718 stmt_list = NULL;
6719 low_pc = NULL;
6720 high_pc = NULL;
6721 ranges = NULL;
6722 comp_dir = NULL;
6723
6724 if (stub_comp_unit_die != NULL)
6725 {
6726 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6727 DWO file. */
6728 if (! this_cu->is_debug_types)
6729 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6730 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6731 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6732 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6733 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6734
6735 cu->addr_base = stub_comp_unit_die->addr_base ();
6736
6737 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6738 here (if needed). We need the value before we can process
6739 DW_AT_ranges. */
6740 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6741 }
6742 else if (stub_comp_dir != NULL)
6743 {
6744 /* Reconstruct the comp_dir attribute to simplify the code below. */
6745 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6746 comp_dir->name = DW_AT_comp_dir;
6747 comp_dir->form = DW_FORM_string;
6748 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6749 DW_STRING (comp_dir) = stub_comp_dir;
6750 }
6751
6752 /* Set up for reading the DWO CU/TU. */
6753 cu->dwo_unit = dwo_unit;
6754 dwarf2_section_info *section = dwo_unit->section;
6755 section->read (objfile);
6756 abfd = section->get_bfd_owner ();
6757 begin_info_ptr = info_ptr = (section->buffer
6758 + to_underlying (dwo_unit->sect_off));
6759 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6760
6761 if (this_cu->is_debug_types)
6762 {
6763 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6764
6765 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6766 &cu->header, section,
6767 dwo_abbrev_section,
6768 info_ptr, rcuh_kind::TYPE);
6769 /* This is not an assert because it can be caused by bad debug info. */
6770 if (sig_type->signature != cu->header.signature)
6771 {
6772 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6773 " TU at offset %s [in module %s]"),
6774 hex_string (sig_type->signature),
6775 hex_string (cu->header.signature),
6776 sect_offset_str (dwo_unit->sect_off),
6777 bfd_get_filename (abfd));
6778 }
6779 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6780 /* For DWOs coming from DWP files, we don't know the CU length
6781 nor the type's offset in the TU until now. */
6782 dwo_unit->length = cu->header.get_length ();
6783 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6784
6785 /* Establish the type offset that can be used to lookup the type.
6786 For DWO files, we don't know it until now. */
6787 sig_type->type_offset_in_section
6788 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6789 }
6790 else
6791 {
6792 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6793 &cu->header, section,
6794 dwo_abbrev_section,
6795 info_ptr, rcuh_kind::COMPILE);
6796 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6797 /* For DWOs coming from DWP files, we don't know the CU length
6798 until now. */
6799 dwo_unit->length = cu->header.get_length ();
6800 }
6801
6802 *result_dwo_abbrev_table
6803 = abbrev_table::read (objfile, dwo_abbrev_section,
6804 cu->header.abbrev_sect_off);
6805 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6806 result_dwo_abbrev_table->get ());
6807
6808 /* Read in the die, but leave space to copy over the attributes
6809 from the stub. This has the benefit of simplifying the rest of
6810 the code - all the work to maintain the illusion of a single
6811 DW_TAG_{compile,type}_unit DIE is done here. */
6812 num_extra_attrs = ((stmt_list != NULL)
6813 + (low_pc != NULL)
6814 + (high_pc != NULL)
6815 + (ranges != NULL)
6816 + (comp_dir != NULL));
6817 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6818 num_extra_attrs);
6819
6820 /* Copy over the attributes from the stub to the DIE we just read in. */
6821 comp_unit_die = *result_comp_unit_die;
6822 i = comp_unit_die->num_attrs;
6823 if (stmt_list != NULL)
6824 comp_unit_die->attrs[i++] = *stmt_list;
6825 if (low_pc != NULL)
6826 comp_unit_die->attrs[i++] = *low_pc;
6827 if (high_pc != NULL)
6828 comp_unit_die->attrs[i++] = *high_pc;
6829 if (ranges != NULL)
6830 comp_unit_die->attrs[i++] = *ranges;
6831 if (comp_dir != NULL)
6832 comp_unit_die->attrs[i++] = *comp_dir;
6833 comp_unit_die->num_attrs += num_extra_attrs;
6834
6835 if (dwarf_die_debug)
6836 {
6837 fprintf_unfiltered (gdb_stdlog,
6838 "Read die from %s@0x%x of %s:\n",
6839 section->get_name (),
6840 (unsigned) (begin_info_ptr - section->buffer),
6841 bfd_get_filename (abfd));
6842 dump_die (comp_unit_die, dwarf_die_debug);
6843 }
6844
6845 /* Skip dummy compilation units. */
6846 if (info_ptr >= begin_info_ptr + dwo_unit->length
6847 || peek_abbrev_code (abfd, info_ptr) == 0)
6848 return 0;
6849
6850 *result_info_ptr = info_ptr;
6851 return 1;
6852 }
6853
6854 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6855 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6856 signature is part of the header. */
6857 static gdb::optional<ULONGEST>
6858 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6859 {
6860 if (cu->header.version >= 5)
6861 return cu->header.signature;
6862 struct attribute *attr;
6863 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6864 if (attr == nullptr)
6865 return gdb::optional<ULONGEST> ();
6866 return DW_UNSND (attr);
6867 }
6868
6869 /* Subroutine of cutu_reader to simplify it.
6870 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6871 Returns NULL if the specified DWO unit cannot be found. */
6872
6873 static struct dwo_unit *
6874 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6875 struct die_info *comp_unit_die,
6876 const char *dwo_name)
6877 {
6878 struct dwarf2_cu *cu = this_cu->cu;
6879 struct dwo_unit *dwo_unit;
6880 const char *comp_dir;
6881
6882 gdb_assert (cu != NULL);
6883
6884 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6885 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6886 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6887
6888 if (this_cu->is_debug_types)
6889 {
6890 struct signatured_type *sig_type;
6891
6892 /* Since this_cu is the first member of struct signatured_type,
6893 we can go from a pointer to one to a pointer to the other. */
6894 sig_type = (struct signatured_type *) this_cu;
6895 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6896 }
6897 else
6898 {
6899 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6900 if (!signature.has_value ())
6901 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6902 " [in module %s]"),
6903 dwo_name, bfd_get_filename (this_cu->per_bfd->obfd));
6904 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6905 *signature);
6906 }
6907
6908 return dwo_unit;
6909 }
6910
6911 /* Subroutine of cutu_reader to simplify it.
6912 See it for a description of the parameters.
6913 Read a TU directly from a DWO file, bypassing the stub. */
6914
6915 void
6916 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6917 dwarf2_per_objfile *per_objfile,
6918 int use_existing_cu)
6919 {
6920 struct signatured_type *sig_type;
6921
6922 /* Verify we can do the following downcast, and that we have the
6923 data we need. */
6924 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6925 sig_type = (struct signatured_type *) this_cu;
6926 gdb_assert (sig_type->dwo_unit != NULL);
6927
6928 if (use_existing_cu && this_cu->cu != NULL)
6929 {
6930 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6931 /* There's no need to do the rereading_dwo_cu handling that
6932 cutu_reader does since we don't read the stub. */
6933 }
6934 else
6935 {
6936 /* If !use_existing_cu, this_cu->cu must be NULL. */
6937 gdb_assert (this_cu->cu == NULL);
6938 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6939 }
6940
6941 /* A future optimization, if needed, would be to use an existing
6942 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6943 could share abbrev tables. */
6944
6945 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6946 NULL /* stub_comp_unit_die */,
6947 sig_type->dwo_unit->dwo_file->comp_dir,
6948 this, &info_ptr,
6949 &comp_unit_die,
6950 &m_dwo_abbrev_table) == 0)
6951 {
6952 /* Dummy die. */
6953 dummy_p = true;
6954 }
6955 }
6956
6957 /* Initialize a CU (or TU) and read its DIEs.
6958 If the CU defers to a DWO file, read the DWO file as well.
6959
6960 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6961 Otherwise the table specified in the comp unit header is read in and used.
6962 This is an optimization for when we already have the abbrev table.
6963
6964 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6965 Otherwise, a new CU is allocated with xmalloc. */
6966
6967 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6968 dwarf2_per_objfile *dwarf2_per_objfile,
6969 struct abbrev_table *abbrev_table,
6970 int use_existing_cu,
6971 bool skip_partial)
6972 : die_reader_specs {},
6973 m_this_cu (this_cu)
6974 {
6975 struct objfile *objfile = dwarf2_per_objfile->objfile;
6976 struct dwarf2_section_info *section = this_cu->section;
6977 bfd *abfd = section->get_bfd_owner ();
6978 struct dwarf2_cu *cu;
6979 const gdb_byte *begin_info_ptr;
6980 struct signatured_type *sig_type = NULL;
6981 struct dwarf2_section_info *abbrev_section;
6982 /* Non-zero if CU currently points to a DWO file and we need to
6983 reread it. When this happens we need to reread the skeleton die
6984 before we can reread the DWO file (this only applies to CUs, not TUs). */
6985 int rereading_dwo_cu = 0;
6986
6987 if (dwarf_die_debug)
6988 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6989 this_cu->is_debug_types ? "type" : "comp",
6990 sect_offset_str (this_cu->sect_off));
6991
6992 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6993 file (instead of going through the stub), short-circuit all of this. */
6994 if (this_cu->reading_dwo_directly)
6995 {
6996 /* Narrow down the scope of possibilities to have to understand. */
6997 gdb_assert (this_cu->is_debug_types);
6998 gdb_assert (abbrev_table == NULL);
6999 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, use_existing_cu);
7000 return;
7001 }
7002
7003 /* This is cheap if the section is already read in. */
7004 section->read (objfile);
7005
7006 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7007
7008 abbrev_section = get_abbrev_section_for_cu (this_cu);
7009
7010 if (use_existing_cu && this_cu->cu != NULL)
7011 {
7012 cu = this_cu->cu;
7013 /* If this CU is from a DWO file we need to start over, we need to
7014 refetch the attributes from the skeleton CU.
7015 This could be optimized by retrieving those attributes from when we
7016 were here the first time: the previous comp_unit_die was stored in
7017 comp_unit_obstack. But there's no data yet that we need this
7018 optimization. */
7019 if (cu->dwo_unit != NULL)
7020 rereading_dwo_cu = 1;
7021 }
7022 else
7023 {
7024 /* If !use_existing_cu, this_cu->cu must be NULL. */
7025 gdb_assert (this_cu->cu == NULL);
7026 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7027 cu = m_new_cu.get ();
7028 }
7029
7030 /* Get the header. */
7031 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7032 {
7033 /* We already have the header, there's no need to read it in again. */
7034 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7035 }
7036 else
7037 {
7038 if (this_cu->is_debug_types)
7039 {
7040 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7041 &cu->header, section,
7042 abbrev_section, info_ptr,
7043 rcuh_kind::TYPE);
7044
7045 /* Since per_cu is the first member of struct signatured_type,
7046 we can go from a pointer to one to a pointer to the other. */
7047 sig_type = (struct signatured_type *) this_cu;
7048 gdb_assert (sig_type->signature == cu->header.signature);
7049 gdb_assert (sig_type->type_offset_in_tu
7050 == cu->header.type_cu_offset_in_tu);
7051 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7052
7053 /* LENGTH has not been set yet for type units if we're
7054 using .gdb_index. */
7055 this_cu->length = cu->header.get_length ();
7056
7057 /* Establish the type offset that can be used to lookup the type. */
7058 sig_type->type_offset_in_section =
7059 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7060
7061 this_cu->dwarf_version = cu->header.version;
7062 }
7063 else
7064 {
7065 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7066 &cu->header, section,
7067 abbrev_section,
7068 info_ptr,
7069 rcuh_kind::COMPILE);
7070
7071 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7072 if (this_cu->length == 0)
7073 this_cu->length = cu->header.get_length ();
7074 else
7075 gdb_assert (this_cu->length == cu->header.get_length ());
7076 this_cu->dwarf_version = cu->header.version;
7077 }
7078 }
7079
7080 /* Skip dummy compilation units. */
7081 if (info_ptr >= begin_info_ptr + this_cu->length
7082 || peek_abbrev_code (abfd, info_ptr) == 0)
7083 {
7084 dummy_p = true;
7085 return;
7086 }
7087
7088 /* If we don't have them yet, read the abbrevs for this compilation unit.
7089 And if we need to read them now, make sure they're freed when we're
7090 done. */
7091 if (abbrev_table != NULL)
7092 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7093 else
7094 {
7095 m_abbrev_table_holder
7096 = abbrev_table::read (objfile, abbrev_section,
7097 cu->header.abbrev_sect_off);
7098 abbrev_table = m_abbrev_table_holder.get ();
7099 }
7100
7101 /* Read the top level CU/TU die. */
7102 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7103 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7104
7105 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7106 {
7107 dummy_p = true;
7108 return;
7109 }
7110
7111 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7112 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7113 table from the DWO file and pass the ownership over to us. It will be
7114 referenced from READER, so we must make sure to free it after we're done
7115 with READER.
7116
7117 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7118 DWO CU, that this test will fail (the attribute will not be present). */
7119 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7120 if (dwo_name != nullptr)
7121 {
7122 struct dwo_unit *dwo_unit;
7123 struct die_info *dwo_comp_unit_die;
7124
7125 if (comp_unit_die->has_children)
7126 {
7127 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7128 " has children (offset %s) [in module %s]"),
7129 sect_offset_str (this_cu->sect_off),
7130 bfd_get_filename (abfd));
7131 }
7132 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
7133 if (dwo_unit != NULL)
7134 {
7135 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7136 comp_unit_die, NULL,
7137 this, &info_ptr,
7138 &dwo_comp_unit_die,
7139 &m_dwo_abbrev_table) == 0)
7140 {
7141 /* Dummy die. */
7142 dummy_p = true;
7143 return;
7144 }
7145 comp_unit_die = dwo_comp_unit_die;
7146 }
7147 else
7148 {
7149 /* Yikes, we couldn't find the rest of the DIE, we only have
7150 the stub. A complaint has already been logged. There's
7151 not much more we can do except pass on the stub DIE to
7152 die_reader_func. We don't want to throw an error on bad
7153 debug info. */
7154 }
7155 }
7156 }
7157
7158 void
7159 cutu_reader::keep ()
7160 {
7161 /* Done, clean up. */
7162 gdb_assert (!dummy_p);
7163 if (m_new_cu != NULL)
7164 {
7165 struct dwarf2_per_objfile *dwarf2_per_objfile
7166 = m_this_cu->dwarf2_per_objfile;
7167 /* Link this CU into read_in_chain. */
7168 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->per_bfd->read_in_chain;
7169 dwarf2_per_objfile->per_bfd->read_in_chain = m_this_cu;
7170 /* The chain owns it now. */
7171 m_new_cu.release ();
7172 }
7173 }
7174
7175 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7176 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7177 assumed to have already done the lookup to find the DWO file).
7178
7179 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7180 THIS_CU->is_debug_types, but nothing else.
7181
7182 We fill in THIS_CU->length.
7183
7184 THIS_CU->cu is always freed when done.
7185 This is done in order to not leave THIS_CU->cu in a state where we have
7186 to care whether it refers to the "main" CU or the DWO CU.
7187
7188 When parent_cu is passed, it is used to provide a default value for
7189 str_offsets_base and addr_base from the parent. */
7190
7191 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7192 dwarf2_per_objfile *dwarf2_per_objfile,
7193 struct dwarf2_cu *parent_cu,
7194 struct dwo_file *dwo_file)
7195 : die_reader_specs {},
7196 m_this_cu (this_cu)
7197 {
7198 struct objfile *objfile = dwarf2_per_objfile->objfile;
7199 struct dwarf2_section_info *section = this_cu->section;
7200 bfd *abfd = section->get_bfd_owner ();
7201 struct dwarf2_section_info *abbrev_section;
7202 const gdb_byte *begin_info_ptr, *info_ptr;
7203
7204 if (dwarf_die_debug)
7205 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7206 this_cu->is_debug_types ? "type" : "comp",
7207 sect_offset_str (this_cu->sect_off));
7208
7209 gdb_assert (this_cu->cu == NULL);
7210
7211 abbrev_section = (dwo_file != NULL
7212 ? &dwo_file->sections.abbrev
7213 : get_abbrev_section_for_cu (this_cu));
7214
7215 /* This is cheap if the section is already read in. */
7216 section->read (objfile);
7217
7218 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7219
7220 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7221 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7222 &m_new_cu->header, section,
7223 abbrev_section, info_ptr,
7224 (this_cu->is_debug_types
7225 ? rcuh_kind::TYPE
7226 : rcuh_kind::COMPILE));
7227
7228 if (parent_cu != nullptr)
7229 {
7230 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7231 m_new_cu->addr_base = parent_cu->addr_base;
7232 }
7233 this_cu->length = m_new_cu->header.get_length ();
7234
7235 /* Skip dummy compilation units. */
7236 if (info_ptr >= begin_info_ptr + this_cu->length
7237 || peek_abbrev_code (abfd, info_ptr) == 0)
7238 {
7239 dummy_p = true;
7240 return;
7241 }
7242
7243 m_abbrev_table_holder
7244 = abbrev_table::read (objfile, abbrev_section,
7245 m_new_cu->header.abbrev_sect_off);
7246
7247 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7248 m_abbrev_table_holder.get ());
7249 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7250 }
7251
7252 \f
7253 /* Type Unit Groups.
7254
7255 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7256 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7257 so that all types coming from the same compilation (.o file) are grouped
7258 together. A future step could be to put the types in the same symtab as
7259 the CU the types ultimately came from. */
7260
7261 static hashval_t
7262 hash_type_unit_group (const void *item)
7263 {
7264 const struct type_unit_group *tu_group
7265 = (const struct type_unit_group *) item;
7266
7267 return hash_stmt_list_entry (&tu_group->hash);
7268 }
7269
7270 static int
7271 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7272 {
7273 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7274 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7275
7276 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7277 }
7278
7279 /* Allocate a hash table for type unit groups. */
7280
7281 static htab_up
7282 allocate_type_unit_groups_table ()
7283 {
7284 return htab_up (htab_create_alloc (3,
7285 hash_type_unit_group,
7286 eq_type_unit_group,
7287 NULL, xcalloc, xfree));
7288 }
7289
7290 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7291 partial symtabs. We combine several TUs per psymtab to not let the size
7292 of any one psymtab grow too big. */
7293 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7294 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7295
7296 /* Helper routine for get_type_unit_group.
7297 Create the type_unit_group object used to hold one or more TUs. */
7298
7299 static struct type_unit_group *
7300 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7301 {
7302 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7303 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
7304 struct dwarf2_per_cu_data *per_cu;
7305 struct type_unit_group *tu_group;
7306
7307 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
7308 struct type_unit_group);
7309 per_cu = &tu_group->per_cu;
7310 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7311 per_cu->per_bfd = per_bfd;
7312
7313 if (per_bfd->using_index)
7314 {
7315 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7316 struct dwarf2_per_cu_quick_data);
7317 }
7318 else
7319 {
7320 unsigned int line_offset = to_underlying (line_offset_struct);
7321 dwarf2_psymtab *pst;
7322 std::string name;
7323
7324 /* Give the symtab a useful name for debug purposes. */
7325 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7326 name = string_printf ("<type_units_%d>",
7327 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7328 else
7329 name = string_printf ("<type_units_at_0x%x>", line_offset);
7330
7331 pst = create_partial_symtab (per_cu, name.c_str ());
7332 pst->anonymous = true;
7333 }
7334
7335 tu_group->hash.dwo_unit = cu->dwo_unit;
7336 tu_group->hash.line_sect_off = line_offset_struct;
7337
7338 return tu_group;
7339 }
7340
7341 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7342 STMT_LIST is a DW_AT_stmt_list attribute. */
7343
7344 static struct type_unit_group *
7345 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7346 {
7347 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7348 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7349 struct type_unit_group *tu_group;
7350 void **slot;
7351 unsigned int line_offset;
7352 struct type_unit_group type_unit_group_for_lookup;
7353
7354 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7355 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7356
7357 /* Do we need to create a new group, or can we use an existing one? */
7358
7359 if (stmt_list)
7360 {
7361 line_offset = DW_UNSND (stmt_list);
7362 ++tu_stats->nr_symtab_sharers;
7363 }
7364 else
7365 {
7366 /* Ugh, no stmt_list. Rare, but we have to handle it.
7367 We can do various things here like create one group per TU or
7368 spread them over multiple groups to split up the expansion work.
7369 To avoid worst case scenarios (too many groups or too large groups)
7370 we, umm, group them in bunches. */
7371 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7372 | (tu_stats->nr_stmt_less_type_units
7373 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7374 ++tu_stats->nr_stmt_less_type_units;
7375 }
7376
7377 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7378 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7379 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7380 &type_unit_group_for_lookup, INSERT);
7381 if (*slot != NULL)
7382 {
7383 tu_group = (struct type_unit_group *) *slot;
7384 gdb_assert (tu_group != NULL);
7385 }
7386 else
7387 {
7388 sect_offset line_offset_struct = (sect_offset) line_offset;
7389 tu_group = create_type_unit_group (cu, line_offset_struct);
7390 *slot = tu_group;
7391 ++tu_stats->nr_symtabs;
7392 }
7393
7394 return tu_group;
7395 }
7396 \f
7397 /* Partial symbol tables. */
7398
7399 /* Create a psymtab named NAME and assign it to PER_CU.
7400
7401 The caller must fill in the following details:
7402 dirname, textlow, texthigh. */
7403
7404 static dwarf2_psymtab *
7405 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7406 {
7407 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7408 dwarf2_psymtab *pst;
7409
7410 pst = new dwarf2_psymtab (name, objfile, per_cu);
7411
7412 pst->psymtabs_addrmap_supported = true;
7413
7414 /* This is the glue that links PST into GDB's symbol API. */
7415 per_cu->v.psymtab = pst;
7416
7417 return pst;
7418 }
7419
7420 /* DIE reader function for process_psymtab_comp_unit. */
7421
7422 static void
7423 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7424 const gdb_byte *info_ptr,
7425 struct die_info *comp_unit_die,
7426 enum language pretend_language)
7427 {
7428 struct dwarf2_cu *cu = reader->cu;
7429 struct objfile *objfile = cu->per_objfile->objfile;
7430 struct gdbarch *gdbarch = objfile->arch ();
7431 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7432 CORE_ADDR baseaddr;
7433 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7434 dwarf2_psymtab *pst;
7435 enum pc_bounds_kind cu_bounds_kind;
7436 const char *filename;
7437
7438 gdb_assert (! per_cu->is_debug_types);
7439
7440 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7441
7442 /* Allocate a new partial symbol table structure. */
7443 gdb::unique_xmalloc_ptr<char> debug_filename;
7444 static const char artificial[] = "<artificial>";
7445 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7446 if (filename == NULL)
7447 filename = "";
7448 else if (strcmp (filename, artificial) == 0)
7449 {
7450 debug_filename.reset (concat (artificial, "@",
7451 sect_offset_str (per_cu->sect_off),
7452 (char *) NULL));
7453 filename = debug_filename.get ();
7454 }
7455
7456 pst = create_partial_symtab (per_cu, filename);
7457
7458 /* This must be done before calling dwarf2_build_include_psymtabs. */
7459 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7460
7461 baseaddr = objfile->text_section_offset ();
7462
7463 dwarf2_find_base_address (comp_unit_die, cu);
7464
7465 /* Possibly set the default values of LOWPC and HIGHPC from
7466 `DW_AT_ranges'. */
7467 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7468 &best_highpc, cu, pst);
7469 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7470 {
7471 CORE_ADDR low
7472 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7473 - baseaddr);
7474 CORE_ADDR high
7475 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7476 - baseaddr - 1);
7477 /* Store the contiguous range if it is not empty; it can be
7478 empty for CUs with no code. */
7479 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7480 low, high, pst);
7481 }
7482
7483 /* Check if comp unit has_children.
7484 If so, read the rest of the partial symbols from this comp unit.
7485 If not, there's no more debug_info for this comp unit. */
7486 if (comp_unit_die->has_children)
7487 {
7488 struct partial_die_info *first_die;
7489 CORE_ADDR lowpc, highpc;
7490
7491 lowpc = ((CORE_ADDR) -1);
7492 highpc = ((CORE_ADDR) 0);
7493
7494 first_die = load_partial_dies (reader, info_ptr, 1);
7495
7496 scan_partial_symbols (first_die, &lowpc, &highpc,
7497 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7498
7499 /* If we didn't find a lowpc, set it to highpc to avoid
7500 complaints from `maint check'. */
7501 if (lowpc == ((CORE_ADDR) -1))
7502 lowpc = highpc;
7503
7504 /* If the compilation unit didn't have an explicit address range,
7505 then use the information extracted from its child dies. */
7506 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7507 {
7508 best_lowpc = lowpc;
7509 best_highpc = highpc;
7510 }
7511 }
7512 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7513 best_lowpc + baseaddr)
7514 - baseaddr);
7515 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7516 best_highpc + baseaddr)
7517 - baseaddr);
7518
7519 end_psymtab_common (objfile, pst);
7520
7521 if (!cu->per_cu->imported_symtabs_empty ())
7522 {
7523 int i;
7524 int len = cu->per_cu->imported_symtabs_size ();
7525
7526 /* Fill in 'dependencies' here; we fill in 'users' in a
7527 post-pass. */
7528 pst->number_of_dependencies = len;
7529 pst->dependencies
7530 = objfile->partial_symtabs->allocate_dependencies (len);
7531 for (i = 0; i < len; ++i)
7532 {
7533 pst->dependencies[i]
7534 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7535 }
7536
7537 cu->per_cu->imported_symtabs_free ();
7538 }
7539
7540 /* Get the list of files included in the current compilation unit,
7541 and build a psymtab for each of them. */
7542 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7543
7544 if (dwarf_read_debug)
7545 fprintf_unfiltered (gdb_stdlog,
7546 "Psymtab for %s unit @%s: %s - %s"
7547 ", %d global, %d static syms\n",
7548 per_cu->is_debug_types ? "type" : "comp",
7549 sect_offset_str (per_cu->sect_off),
7550 paddress (gdbarch, pst->text_low (objfile)),
7551 paddress (gdbarch, pst->text_high (objfile)),
7552 pst->n_global_syms, pst->n_static_syms);
7553 }
7554
7555 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7556 Process compilation unit THIS_CU for a psymtab. */
7557
7558 static void
7559 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7560 dwarf2_per_objfile *per_objfile,
7561 bool want_partial_unit,
7562 enum language pretend_language)
7563 {
7564 /* If this compilation unit was already read in, free the
7565 cached copy in order to read it in again. This is
7566 necessary because we skipped some symbols when we first
7567 read in the compilation unit (see load_partial_dies).
7568 This problem could be avoided, but the benefit is unclear. */
7569 if (this_cu->cu != NULL)
7570 free_one_cached_comp_unit (this_cu);
7571
7572 cutu_reader reader (this_cu, per_objfile, NULL, 0, false);
7573
7574 switch (reader.comp_unit_die->tag)
7575 {
7576 case DW_TAG_compile_unit:
7577 this_cu->unit_type = DW_UT_compile;
7578 break;
7579 case DW_TAG_partial_unit:
7580 this_cu->unit_type = DW_UT_partial;
7581 break;
7582 default:
7583 abort ();
7584 }
7585
7586 if (reader.dummy_p)
7587 {
7588 /* Nothing. */
7589 }
7590 else if (this_cu->is_debug_types)
7591 build_type_psymtabs_reader (&reader, reader.info_ptr,
7592 reader.comp_unit_die);
7593 else if (want_partial_unit
7594 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7595 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7596 reader.comp_unit_die,
7597 pretend_language);
7598
7599 this_cu->lang = this_cu->cu->language;
7600
7601 /* Age out any secondary CUs. */
7602 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7603 }
7604
7605 /* Reader function for build_type_psymtabs. */
7606
7607 static void
7608 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7609 const gdb_byte *info_ptr,
7610 struct die_info *type_unit_die)
7611 {
7612 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
7613 struct objfile *objfile = dwarf2_per_objfile->objfile;
7614 struct dwarf2_cu *cu = reader->cu;
7615 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7616 struct signatured_type *sig_type;
7617 struct type_unit_group *tu_group;
7618 struct attribute *attr;
7619 struct partial_die_info *first_die;
7620 CORE_ADDR lowpc, highpc;
7621 dwarf2_psymtab *pst;
7622
7623 gdb_assert (per_cu->is_debug_types);
7624 sig_type = (struct signatured_type *) per_cu;
7625
7626 if (! type_unit_die->has_children)
7627 return;
7628
7629 attr = type_unit_die->attr (DW_AT_stmt_list);
7630 tu_group = get_type_unit_group (cu, attr);
7631
7632 if (tu_group->tus == nullptr)
7633 tu_group->tus = new std::vector<signatured_type *>;
7634 tu_group->tus->push_back (sig_type);
7635
7636 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7637 pst = create_partial_symtab (per_cu, "");
7638 pst->anonymous = true;
7639
7640 first_die = load_partial_dies (reader, info_ptr, 1);
7641
7642 lowpc = (CORE_ADDR) -1;
7643 highpc = (CORE_ADDR) 0;
7644 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7645
7646 end_psymtab_common (objfile, pst);
7647 }
7648
7649 /* Struct used to sort TUs by their abbreviation table offset. */
7650
7651 struct tu_abbrev_offset
7652 {
7653 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7654 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7655 {}
7656
7657 signatured_type *sig_type;
7658 sect_offset abbrev_offset;
7659 };
7660
7661 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7662
7663 static bool
7664 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7665 const struct tu_abbrev_offset &b)
7666 {
7667 return a.abbrev_offset < b.abbrev_offset;
7668 }
7669
7670 /* Efficiently read all the type units.
7671 This does the bulk of the work for build_type_psymtabs.
7672
7673 The efficiency is because we sort TUs by the abbrev table they use and
7674 only read each abbrev table once. In one program there are 200K TUs
7675 sharing 8K abbrev tables.
7676
7677 The main purpose of this function is to support building the
7678 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7679 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7680 can collapse the search space by grouping them by stmt_list.
7681 The savings can be significant, in the same program from above the 200K TUs
7682 share 8K stmt_list tables.
7683
7684 FUNC is expected to call get_type_unit_group, which will create the
7685 struct type_unit_group if necessary and add it to
7686 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7687
7688 static void
7689 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7690 {
7691 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7692 abbrev_table_up abbrev_table;
7693 sect_offset abbrev_offset;
7694
7695 /* It's up to the caller to not call us multiple times. */
7696 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
7697
7698 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
7699 return;
7700
7701 /* TUs typically share abbrev tables, and there can be way more TUs than
7702 abbrev tables. Sort by abbrev table to reduce the number of times we
7703 read each abbrev table in.
7704 Alternatives are to punt or to maintain a cache of abbrev tables.
7705 This is simpler and efficient enough for now.
7706
7707 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7708 symtab to use). Typically TUs with the same abbrev offset have the same
7709 stmt_list value too so in practice this should work well.
7710
7711 The basic algorithm here is:
7712
7713 sort TUs by abbrev table
7714 for each TU with same abbrev table:
7715 read abbrev table if first user
7716 read TU top level DIE
7717 [IWBN if DWO skeletons had DW_AT_stmt_list]
7718 call FUNC */
7719
7720 if (dwarf_read_debug)
7721 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7722
7723 /* Sort in a separate table to maintain the order of all_type_units
7724 for .gdb_index: TU indices directly index all_type_units. */
7725 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7726 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
7727
7728 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
7729 sorted_by_abbrev.emplace_back
7730 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7731 sig_type->per_cu.section,
7732 sig_type->per_cu.sect_off));
7733
7734 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7735 sort_tu_by_abbrev_offset);
7736
7737 abbrev_offset = (sect_offset) ~(unsigned) 0;
7738
7739 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7740 {
7741 /* Switch to the next abbrev table if necessary. */
7742 if (abbrev_table == NULL
7743 || tu.abbrev_offset != abbrev_offset)
7744 {
7745 abbrev_offset = tu.abbrev_offset;
7746 abbrev_table =
7747 abbrev_table::read (dwarf2_per_objfile->objfile,
7748 &dwarf2_per_objfile->per_bfd->abbrev,
7749 abbrev_offset);
7750 ++tu_stats->nr_uniq_abbrev_tables;
7751 }
7752
7753 cutu_reader reader (&tu.sig_type->per_cu, dwarf2_per_objfile,
7754 abbrev_table.get (), 0, false);
7755 if (!reader.dummy_p)
7756 build_type_psymtabs_reader (&reader, reader.info_ptr,
7757 reader.comp_unit_die);
7758 }
7759 }
7760
7761 /* Print collected type unit statistics. */
7762
7763 static void
7764 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7765 {
7766 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7767
7768 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7769 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7770 dwarf2_per_objfile->per_bfd->all_type_units.size ());
7771 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7772 tu_stats->nr_uniq_abbrev_tables);
7773 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7774 tu_stats->nr_symtabs);
7775 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7776 tu_stats->nr_symtab_sharers);
7777 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7778 tu_stats->nr_stmt_less_type_units);
7779 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7780 tu_stats->nr_all_type_units_reallocs);
7781 }
7782
7783 /* Traversal function for build_type_psymtabs. */
7784
7785 static int
7786 build_type_psymtab_dependencies (void **slot, void *info)
7787 {
7788 struct dwarf2_per_objfile *dwarf2_per_objfile
7789 = (struct dwarf2_per_objfile *) info;
7790 struct objfile *objfile = dwarf2_per_objfile->objfile;
7791 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7792 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7793 dwarf2_psymtab *pst = per_cu->v.psymtab;
7794 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7795 int i;
7796
7797 gdb_assert (len > 0);
7798 gdb_assert (per_cu->type_unit_group_p ());
7799
7800 pst->number_of_dependencies = len;
7801 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7802 for (i = 0; i < len; ++i)
7803 {
7804 struct signatured_type *iter = tu_group->tus->at (i);
7805 gdb_assert (iter->per_cu.is_debug_types);
7806 pst->dependencies[i] = iter->per_cu.v.psymtab;
7807 iter->type_unit_group = tu_group;
7808 }
7809
7810 delete tu_group->tus;
7811 tu_group->tus = nullptr;
7812
7813 return 1;
7814 }
7815
7816 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7817 Build partial symbol tables for the .debug_types comp-units. */
7818
7819 static void
7820 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7821 {
7822 if (! create_all_type_units (dwarf2_per_objfile))
7823 return;
7824
7825 build_type_psymtabs_1 (dwarf2_per_objfile);
7826 }
7827
7828 /* Traversal function for process_skeletonless_type_unit.
7829 Read a TU in a DWO file and build partial symbols for it. */
7830
7831 static int
7832 process_skeletonless_type_unit (void **slot, void *info)
7833 {
7834 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7835 struct dwarf2_per_objfile *dwarf2_per_objfile
7836 = (struct dwarf2_per_objfile *) info;
7837 struct signatured_type find_entry, *entry;
7838
7839 /* If this TU doesn't exist in the global table, add it and read it in. */
7840
7841 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7842 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7843
7844 find_entry.signature = dwo_unit->signature;
7845 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
7846 &find_entry, INSERT);
7847 /* If we've already seen this type there's nothing to do. What's happening
7848 is we're doing our own version of comdat-folding here. */
7849 if (*slot != NULL)
7850 return 1;
7851
7852 /* This does the job that create_all_type_units would have done for
7853 this TU. */
7854 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7855 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7856 *slot = entry;
7857
7858 /* This does the job that build_type_psymtabs_1 would have done. */
7859 cutu_reader reader (&entry->per_cu, dwarf2_per_objfile, NULL, 0, false);
7860 if (!reader.dummy_p)
7861 build_type_psymtabs_reader (&reader, reader.info_ptr,
7862 reader.comp_unit_die);
7863
7864 return 1;
7865 }
7866
7867 /* Traversal function for process_skeletonless_type_units. */
7868
7869 static int
7870 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7871 {
7872 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7873
7874 if (dwo_file->tus != NULL)
7875 htab_traverse_noresize (dwo_file->tus.get (),
7876 process_skeletonless_type_unit, info);
7877
7878 return 1;
7879 }
7880
7881 /* Scan all TUs of DWO files, verifying we've processed them.
7882 This is needed in case a TU was emitted without its skeleton.
7883 Note: This can't be done until we know what all the DWO files are. */
7884
7885 static void
7886 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7887 {
7888 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7889 if (get_dwp_file (dwarf2_per_objfile) == NULL
7890 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
7891 {
7892 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
7893 process_dwo_file_for_skeletonless_type_units,
7894 dwarf2_per_objfile);
7895 }
7896 }
7897
7898 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7899
7900 static void
7901 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7902 {
7903 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7904 {
7905 dwarf2_psymtab *pst = per_cu->v.psymtab;
7906
7907 if (pst == NULL)
7908 continue;
7909
7910 for (int j = 0; j < pst->number_of_dependencies; ++j)
7911 {
7912 /* Set the 'user' field only if it is not already set. */
7913 if (pst->dependencies[j]->user == NULL)
7914 pst->dependencies[j]->user = pst;
7915 }
7916 }
7917 }
7918
7919 /* Build the partial symbol table by doing a quick pass through the
7920 .debug_info and .debug_abbrev sections. */
7921
7922 static void
7923 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7924 {
7925 struct objfile *objfile = dwarf2_per_objfile->objfile;
7926
7927 if (dwarf_read_debug)
7928 {
7929 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7930 objfile_name (objfile));
7931 }
7932
7933 scoped_restore restore_reading_psyms
7934 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
7935 true);
7936
7937 dwarf2_per_objfile->per_bfd->info.read (objfile);
7938
7939 /* Any cached compilation units will be linked by the per-objfile
7940 read_in_chain. Make sure to free them when we're done. */
7941 free_cached_comp_units freer (dwarf2_per_objfile);
7942
7943 build_type_psymtabs (dwarf2_per_objfile);
7944
7945 create_all_comp_units (dwarf2_per_objfile);
7946
7947 /* Create a temporary address map on a temporary obstack. We later
7948 copy this to the final obstack. */
7949 auto_obstack temp_obstack;
7950
7951 scoped_restore save_psymtabs_addrmap
7952 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7953 addrmap_create_mutable (&temp_obstack));
7954
7955 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7956 {
7957 if (per_cu->v.psymtab != NULL)
7958 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7959 continue;
7960 process_psymtab_comp_unit (per_cu, dwarf2_per_objfile, false,
7961 language_minimal);
7962 }
7963
7964 /* This has to wait until we read the CUs, we need the list of DWOs. */
7965 process_skeletonless_type_units (dwarf2_per_objfile);
7966
7967 /* Now that all TUs have been processed we can fill in the dependencies. */
7968 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
7969 {
7970 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7971 build_type_psymtab_dependencies, dwarf2_per_objfile);
7972 }
7973
7974 if (dwarf_read_debug)
7975 print_tu_stats (dwarf2_per_objfile);
7976
7977 set_partial_user (dwarf2_per_objfile);
7978
7979 objfile->partial_symtabs->psymtabs_addrmap
7980 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7981 objfile->partial_symtabs->obstack ());
7982 /* At this point we want to keep the address map. */
7983 save_psymtabs_addrmap.release ();
7984
7985 if (dwarf_read_debug)
7986 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7987 objfile_name (objfile));
7988 }
7989
7990 /* Load the partial DIEs for a secondary CU into memory.
7991 This is also used when rereading a primary CU with load_all_dies. */
7992
7993 static void
7994 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7995 dwarf2_per_objfile *per_objfile)
7996 {
7997 cutu_reader reader (this_cu, per_objfile, NULL, 1, false);
7998
7999 if (!reader.dummy_p)
8000 {
8001 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8002 language_minimal);
8003
8004 /* Check if comp unit has_children.
8005 If so, read the rest of the partial symbols from this comp unit.
8006 If not, there's no more debug_info for this comp unit. */
8007 if (reader.comp_unit_die->has_children)
8008 load_partial_dies (&reader, reader.info_ptr, 0);
8009
8010 reader.keep ();
8011 }
8012 }
8013
8014 static void
8015 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8016 struct dwarf2_section_info *section,
8017 struct dwarf2_section_info *abbrev_section,
8018 unsigned int is_dwz)
8019 {
8020 const gdb_byte *info_ptr;
8021 struct objfile *objfile = dwarf2_per_objfile->objfile;
8022
8023 if (dwarf_read_debug)
8024 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8025 section->get_name (),
8026 section->get_file_name ());
8027
8028 section->read (objfile);
8029
8030 info_ptr = section->buffer;
8031
8032 while (info_ptr < section->buffer + section->size)
8033 {
8034 struct dwarf2_per_cu_data *this_cu;
8035
8036 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8037
8038 comp_unit_head cu_header;
8039 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8040 abbrev_section, info_ptr,
8041 rcuh_kind::COMPILE);
8042
8043 /* Save the compilation unit for later lookup. */
8044 if (cu_header.unit_type != DW_UT_type)
8045 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
8046 else
8047 {
8048 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
8049 sig_type->signature = cu_header.signature;
8050 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8051 this_cu = &sig_type->per_cu;
8052 }
8053 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8054 this_cu->sect_off = sect_off;
8055 this_cu->length = cu_header.length + cu_header.initial_length_size;
8056 this_cu->is_dwz = is_dwz;
8057 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8058 this_cu->section = section;
8059
8060 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8061
8062 info_ptr = info_ptr + this_cu->length;
8063 }
8064 }
8065
8066 /* Create a list of all compilation units in OBJFILE.
8067 This is only done for -readnow and building partial symtabs. */
8068
8069 static void
8070 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8071 {
8072 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8073 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8074 &dwarf2_per_objfile->per_bfd->abbrev, 0);
8075
8076 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
8077 if (dwz != NULL)
8078 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8079 1);
8080 }
8081
8082 /* Process all loaded DIEs for compilation unit CU, starting at
8083 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8084 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8085 DW_AT_ranges). See the comments of add_partial_subprogram on how
8086 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8087
8088 static void
8089 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8090 CORE_ADDR *highpc, int set_addrmap,
8091 struct dwarf2_cu *cu)
8092 {
8093 struct partial_die_info *pdi;
8094
8095 /* Now, march along the PDI's, descending into ones which have
8096 interesting children but skipping the children of the other ones,
8097 until we reach the end of the compilation unit. */
8098
8099 pdi = first_die;
8100
8101 while (pdi != NULL)
8102 {
8103 pdi->fixup (cu);
8104
8105 /* Anonymous namespaces or modules have no name but have interesting
8106 children, so we need to look at them. Ditto for anonymous
8107 enums. */
8108
8109 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8110 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8111 || pdi->tag == DW_TAG_imported_unit
8112 || pdi->tag == DW_TAG_inlined_subroutine)
8113 {
8114 switch (pdi->tag)
8115 {
8116 case DW_TAG_subprogram:
8117 case DW_TAG_inlined_subroutine:
8118 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8119 break;
8120 case DW_TAG_constant:
8121 case DW_TAG_variable:
8122 case DW_TAG_typedef:
8123 case DW_TAG_union_type:
8124 if (!pdi->is_declaration
8125 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8126 {
8127 add_partial_symbol (pdi, cu);
8128 }
8129 break;
8130 case DW_TAG_class_type:
8131 case DW_TAG_interface_type:
8132 case DW_TAG_structure_type:
8133 if (!pdi->is_declaration)
8134 {
8135 add_partial_symbol (pdi, cu);
8136 }
8137 if ((cu->language == language_rust
8138 || cu->language == language_cplus) && pdi->has_children)
8139 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8140 set_addrmap, cu);
8141 break;
8142 case DW_TAG_enumeration_type:
8143 if (!pdi->is_declaration)
8144 add_partial_enumeration (pdi, cu);
8145 break;
8146 case DW_TAG_base_type:
8147 case DW_TAG_subrange_type:
8148 /* File scope base type definitions are added to the partial
8149 symbol table. */
8150 add_partial_symbol (pdi, cu);
8151 break;
8152 case DW_TAG_namespace:
8153 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8154 break;
8155 case DW_TAG_module:
8156 if (!pdi->is_declaration)
8157 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8158 break;
8159 case DW_TAG_imported_unit:
8160 {
8161 struct dwarf2_per_cu_data *per_cu;
8162
8163 /* For now we don't handle imported units in type units. */
8164 if (cu->per_cu->is_debug_types)
8165 {
8166 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8167 " supported in type units [in module %s]"),
8168 objfile_name (cu->per_objfile->objfile));
8169 }
8170
8171 per_cu = dwarf2_find_containing_comp_unit
8172 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8173
8174 /* Go read the partial unit, if needed. */
8175 if (per_cu->v.psymtab == NULL)
8176 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8177 cu->language);
8178
8179 cu->per_cu->imported_symtabs_push (per_cu);
8180 }
8181 break;
8182 case DW_TAG_imported_declaration:
8183 add_partial_symbol (pdi, cu);
8184 break;
8185 default:
8186 break;
8187 }
8188 }
8189
8190 /* If the die has a sibling, skip to the sibling. */
8191
8192 pdi = pdi->die_sibling;
8193 }
8194 }
8195
8196 /* Functions used to compute the fully scoped name of a partial DIE.
8197
8198 Normally, this is simple. For C++, the parent DIE's fully scoped
8199 name is concatenated with "::" and the partial DIE's name.
8200 Enumerators are an exception; they use the scope of their parent
8201 enumeration type, i.e. the name of the enumeration type is not
8202 prepended to the enumerator.
8203
8204 There are two complexities. One is DW_AT_specification; in this
8205 case "parent" means the parent of the target of the specification,
8206 instead of the direct parent of the DIE. The other is compilers
8207 which do not emit DW_TAG_namespace; in this case we try to guess
8208 the fully qualified name of structure types from their members'
8209 linkage names. This must be done using the DIE's children rather
8210 than the children of any DW_AT_specification target. We only need
8211 to do this for structures at the top level, i.e. if the target of
8212 any DW_AT_specification (if any; otherwise the DIE itself) does not
8213 have a parent. */
8214
8215 /* Compute the scope prefix associated with PDI's parent, in
8216 compilation unit CU. The result will be allocated on CU's
8217 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8218 field. NULL is returned if no prefix is necessary. */
8219 static const char *
8220 partial_die_parent_scope (struct partial_die_info *pdi,
8221 struct dwarf2_cu *cu)
8222 {
8223 const char *grandparent_scope;
8224 struct partial_die_info *parent, *real_pdi;
8225
8226 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8227 then this means the parent of the specification DIE. */
8228
8229 real_pdi = pdi;
8230 while (real_pdi->has_specification)
8231 {
8232 auto res = find_partial_die (real_pdi->spec_offset,
8233 real_pdi->spec_is_dwz, cu);
8234 real_pdi = res.pdi;
8235 cu = res.cu;
8236 }
8237
8238 parent = real_pdi->die_parent;
8239 if (parent == NULL)
8240 return NULL;
8241
8242 if (parent->scope_set)
8243 return parent->scope;
8244
8245 parent->fixup (cu);
8246
8247 grandparent_scope = partial_die_parent_scope (parent, cu);
8248
8249 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8250 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8251 Work around this problem here. */
8252 if (cu->language == language_cplus
8253 && parent->tag == DW_TAG_namespace
8254 && strcmp (parent->name, "::") == 0
8255 && grandparent_scope == NULL)
8256 {
8257 parent->scope = NULL;
8258 parent->scope_set = 1;
8259 return NULL;
8260 }
8261
8262 /* Nested subroutines in Fortran get a prefix. */
8263 if (pdi->tag == DW_TAG_enumerator)
8264 /* Enumerators should not get the name of the enumeration as a prefix. */
8265 parent->scope = grandparent_scope;
8266 else if (parent->tag == DW_TAG_namespace
8267 || parent->tag == DW_TAG_module
8268 || parent->tag == DW_TAG_structure_type
8269 || parent->tag == DW_TAG_class_type
8270 || parent->tag == DW_TAG_interface_type
8271 || parent->tag == DW_TAG_union_type
8272 || parent->tag == DW_TAG_enumeration_type
8273 || (cu->language == language_fortran
8274 && parent->tag == DW_TAG_subprogram
8275 && pdi->tag == DW_TAG_subprogram))
8276 {
8277 if (grandparent_scope == NULL)
8278 parent->scope = parent->name;
8279 else
8280 parent->scope = typename_concat (&cu->comp_unit_obstack,
8281 grandparent_scope,
8282 parent->name, 0, cu);
8283 }
8284 else
8285 {
8286 /* FIXME drow/2004-04-01: What should we be doing with
8287 function-local names? For partial symbols, we should probably be
8288 ignoring them. */
8289 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8290 dwarf_tag_name (parent->tag),
8291 sect_offset_str (pdi->sect_off));
8292 parent->scope = grandparent_scope;
8293 }
8294
8295 parent->scope_set = 1;
8296 return parent->scope;
8297 }
8298
8299 /* Return the fully scoped name associated with PDI, from compilation unit
8300 CU. The result will be allocated with malloc. */
8301
8302 static gdb::unique_xmalloc_ptr<char>
8303 partial_die_full_name (struct partial_die_info *pdi,
8304 struct dwarf2_cu *cu)
8305 {
8306 const char *parent_scope;
8307
8308 /* If this is a template instantiation, we can not work out the
8309 template arguments from partial DIEs. So, unfortunately, we have
8310 to go through the full DIEs. At least any work we do building
8311 types here will be reused if full symbols are loaded later. */
8312 if (pdi->has_template_arguments)
8313 {
8314 pdi->fixup (cu);
8315
8316 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8317 {
8318 struct die_info *die;
8319 struct attribute attr;
8320 struct dwarf2_cu *ref_cu = cu;
8321
8322 /* DW_FORM_ref_addr is using section offset. */
8323 attr.name = (enum dwarf_attribute) 0;
8324 attr.form = DW_FORM_ref_addr;
8325 attr.u.unsnd = to_underlying (pdi->sect_off);
8326 die = follow_die_ref (NULL, &attr, &ref_cu);
8327
8328 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8329 }
8330 }
8331
8332 parent_scope = partial_die_parent_scope (pdi, cu);
8333 if (parent_scope == NULL)
8334 return NULL;
8335 else
8336 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8337 pdi->name, 0, cu));
8338 }
8339
8340 static void
8341 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8342 {
8343 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
8344 struct objfile *objfile = dwarf2_per_objfile->objfile;
8345 struct gdbarch *gdbarch = objfile->arch ();
8346 CORE_ADDR addr = 0;
8347 const char *actual_name = NULL;
8348 CORE_ADDR baseaddr;
8349
8350 baseaddr = objfile->text_section_offset ();
8351
8352 gdb::unique_xmalloc_ptr<char> built_actual_name
8353 = partial_die_full_name (pdi, cu);
8354 if (built_actual_name != NULL)
8355 actual_name = built_actual_name.get ();
8356
8357 if (actual_name == NULL)
8358 actual_name = pdi->name;
8359
8360 partial_symbol psymbol;
8361 memset (&psymbol, 0, sizeof (psymbol));
8362 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8363 psymbol.ginfo.section = -1;
8364
8365 /* The code below indicates that the psymbol should be installed by
8366 setting this. */
8367 gdb::optional<psymbol_placement> where;
8368
8369 switch (pdi->tag)
8370 {
8371 case DW_TAG_inlined_subroutine:
8372 case DW_TAG_subprogram:
8373 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8374 - baseaddr);
8375 if (pdi->is_external
8376 || cu->language == language_ada
8377 || (cu->language == language_fortran
8378 && pdi->die_parent != NULL
8379 && pdi->die_parent->tag == DW_TAG_subprogram))
8380 {
8381 /* Normally, only "external" DIEs are part of the global scope.
8382 But in Ada and Fortran, we want to be able to access nested
8383 procedures globally. So all Ada and Fortran subprograms are
8384 stored in the global scope. */
8385 where = psymbol_placement::GLOBAL;
8386 }
8387 else
8388 where = psymbol_placement::STATIC;
8389
8390 psymbol.domain = VAR_DOMAIN;
8391 psymbol.aclass = LOC_BLOCK;
8392 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8393 psymbol.ginfo.value.address = addr;
8394
8395 if (pdi->main_subprogram && actual_name != NULL)
8396 set_objfile_main_name (objfile, actual_name, cu->language);
8397 break;
8398 case DW_TAG_constant:
8399 psymbol.domain = VAR_DOMAIN;
8400 psymbol.aclass = LOC_STATIC;
8401 where = (pdi->is_external
8402 ? psymbol_placement::GLOBAL
8403 : psymbol_placement::STATIC);
8404 break;
8405 case DW_TAG_variable:
8406 if (pdi->d.locdesc)
8407 addr = decode_locdesc (pdi->d.locdesc, cu);
8408
8409 if (pdi->d.locdesc
8410 && addr == 0
8411 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
8412 {
8413 /* A global or static variable may also have been stripped
8414 out by the linker if unused, in which case its address
8415 will be nullified; do not add such variables into partial
8416 symbol table then. */
8417 }
8418 else if (pdi->is_external)
8419 {
8420 /* Global Variable.
8421 Don't enter into the minimal symbol tables as there is
8422 a minimal symbol table entry from the ELF symbols already.
8423 Enter into partial symbol table if it has a location
8424 descriptor or a type.
8425 If the location descriptor is missing, new_symbol will create
8426 a LOC_UNRESOLVED symbol, the address of the variable will then
8427 be determined from the minimal symbol table whenever the variable
8428 is referenced.
8429 The address for the partial symbol table entry is not
8430 used by GDB, but it comes in handy for debugging partial symbol
8431 table building. */
8432
8433 if (pdi->d.locdesc || pdi->has_type)
8434 {
8435 psymbol.domain = VAR_DOMAIN;
8436 psymbol.aclass = LOC_STATIC;
8437 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8438 psymbol.ginfo.value.address = addr;
8439 where = psymbol_placement::GLOBAL;
8440 }
8441 }
8442 else
8443 {
8444 int has_loc = pdi->d.locdesc != NULL;
8445
8446 /* Static Variable. Skip symbols whose value we cannot know (those
8447 without location descriptors or constant values). */
8448 if (!has_loc && !pdi->has_const_value)
8449 return;
8450
8451 psymbol.domain = VAR_DOMAIN;
8452 psymbol.aclass = LOC_STATIC;
8453 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8454 if (has_loc)
8455 psymbol.ginfo.value.address = addr;
8456 where = psymbol_placement::STATIC;
8457 }
8458 break;
8459 case DW_TAG_typedef:
8460 case DW_TAG_base_type:
8461 case DW_TAG_subrange_type:
8462 psymbol.domain = VAR_DOMAIN;
8463 psymbol.aclass = LOC_TYPEDEF;
8464 where = psymbol_placement::STATIC;
8465 break;
8466 case DW_TAG_imported_declaration:
8467 case DW_TAG_namespace:
8468 psymbol.domain = VAR_DOMAIN;
8469 psymbol.aclass = LOC_TYPEDEF;
8470 where = psymbol_placement::GLOBAL;
8471 break;
8472 case DW_TAG_module:
8473 /* With Fortran 77 there might be a "BLOCK DATA" module
8474 available without any name. If so, we skip the module as it
8475 doesn't bring any value. */
8476 if (actual_name != nullptr)
8477 {
8478 psymbol.domain = MODULE_DOMAIN;
8479 psymbol.aclass = LOC_TYPEDEF;
8480 where = psymbol_placement::GLOBAL;
8481 }
8482 break;
8483 case DW_TAG_class_type:
8484 case DW_TAG_interface_type:
8485 case DW_TAG_structure_type:
8486 case DW_TAG_union_type:
8487 case DW_TAG_enumeration_type:
8488 /* Skip external references. The DWARF standard says in the section
8489 about "Structure, Union, and Class Type Entries": "An incomplete
8490 structure, union or class type is represented by a structure,
8491 union or class entry that does not have a byte size attribute
8492 and that has a DW_AT_declaration attribute." */
8493 if (!pdi->has_byte_size && pdi->is_declaration)
8494 return;
8495
8496 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8497 static vs. global. */
8498 psymbol.domain = STRUCT_DOMAIN;
8499 psymbol.aclass = LOC_TYPEDEF;
8500 where = (cu->language == language_cplus
8501 ? psymbol_placement::GLOBAL
8502 : psymbol_placement::STATIC);
8503 break;
8504 case DW_TAG_enumerator:
8505 psymbol.domain = VAR_DOMAIN;
8506 psymbol.aclass = LOC_CONST;
8507 where = (cu->language == language_cplus
8508 ? psymbol_placement::GLOBAL
8509 : psymbol_placement::STATIC);
8510 break;
8511 default:
8512 break;
8513 }
8514
8515 if (where.has_value ())
8516 {
8517 if (built_actual_name != nullptr)
8518 actual_name = objfile->intern (actual_name);
8519 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8520 psymbol.ginfo.set_linkage_name (actual_name);
8521 else
8522 {
8523 psymbol.ginfo.set_demangled_name (actual_name,
8524 &objfile->objfile_obstack);
8525 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8526 }
8527 add_psymbol_to_list (psymbol, *where, objfile);
8528 }
8529 }
8530
8531 /* Read a partial die corresponding to a namespace; also, add a symbol
8532 corresponding to that namespace to the symbol table. NAMESPACE is
8533 the name of the enclosing namespace. */
8534
8535 static void
8536 add_partial_namespace (struct partial_die_info *pdi,
8537 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8538 int set_addrmap, struct dwarf2_cu *cu)
8539 {
8540 /* Add a symbol for the namespace. */
8541
8542 add_partial_symbol (pdi, cu);
8543
8544 /* Now scan partial symbols in that namespace. */
8545
8546 if (pdi->has_children)
8547 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8548 }
8549
8550 /* Read a partial die corresponding to a Fortran module. */
8551
8552 static void
8553 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8554 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8555 {
8556 /* Add a symbol for the namespace. */
8557
8558 add_partial_symbol (pdi, cu);
8559
8560 /* Now scan partial symbols in that module. */
8561
8562 if (pdi->has_children)
8563 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8564 }
8565
8566 /* Read a partial die corresponding to a subprogram or an inlined
8567 subprogram and create a partial symbol for that subprogram.
8568 When the CU language allows it, this routine also defines a partial
8569 symbol for each nested subprogram that this subprogram contains.
8570 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8571 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8572
8573 PDI may also be a lexical block, in which case we simply search
8574 recursively for subprograms defined inside that lexical block.
8575 Again, this is only performed when the CU language allows this
8576 type of definitions. */
8577
8578 static void
8579 add_partial_subprogram (struct partial_die_info *pdi,
8580 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8581 int set_addrmap, struct dwarf2_cu *cu)
8582 {
8583 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8584 {
8585 if (pdi->has_pc_info)
8586 {
8587 if (pdi->lowpc < *lowpc)
8588 *lowpc = pdi->lowpc;
8589 if (pdi->highpc > *highpc)
8590 *highpc = pdi->highpc;
8591 if (set_addrmap)
8592 {
8593 struct objfile *objfile = cu->per_objfile->objfile;
8594 struct gdbarch *gdbarch = objfile->arch ();
8595 CORE_ADDR baseaddr;
8596 CORE_ADDR this_highpc;
8597 CORE_ADDR this_lowpc;
8598
8599 baseaddr = objfile->text_section_offset ();
8600 this_lowpc
8601 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8602 pdi->lowpc + baseaddr)
8603 - baseaddr);
8604 this_highpc
8605 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8606 pdi->highpc + baseaddr)
8607 - baseaddr);
8608 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8609 this_lowpc, this_highpc - 1,
8610 cu->per_cu->v.psymtab);
8611 }
8612 }
8613
8614 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8615 {
8616 if (!pdi->is_declaration)
8617 /* Ignore subprogram DIEs that do not have a name, they are
8618 illegal. Do not emit a complaint at this point, we will
8619 do so when we convert this psymtab into a symtab. */
8620 if (pdi->name)
8621 add_partial_symbol (pdi, cu);
8622 }
8623 }
8624
8625 if (! pdi->has_children)
8626 return;
8627
8628 if (cu->language == language_ada || cu->language == language_fortran)
8629 {
8630 pdi = pdi->die_child;
8631 while (pdi != NULL)
8632 {
8633 pdi->fixup (cu);
8634 if (pdi->tag == DW_TAG_subprogram
8635 || pdi->tag == DW_TAG_inlined_subroutine
8636 || pdi->tag == DW_TAG_lexical_block)
8637 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8638 pdi = pdi->die_sibling;
8639 }
8640 }
8641 }
8642
8643 /* Read a partial die corresponding to an enumeration type. */
8644
8645 static void
8646 add_partial_enumeration (struct partial_die_info *enum_pdi,
8647 struct dwarf2_cu *cu)
8648 {
8649 struct partial_die_info *pdi;
8650
8651 if (enum_pdi->name != NULL)
8652 add_partial_symbol (enum_pdi, cu);
8653
8654 pdi = enum_pdi->die_child;
8655 while (pdi)
8656 {
8657 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8658 complaint (_("malformed enumerator DIE ignored"));
8659 else
8660 add_partial_symbol (pdi, cu);
8661 pdi = pdi->die_sibling;
8662 }
8663 }
8664
8665 /* Return the initial uleb128 in the die at INFO_PTR. */
8666
8667 static unsigned int
8668 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8669 {
8670 unsigned int bytes_read;
8671
8672 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8673 }
8674
8675 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8676 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8677
8678 Return the corresponding abbrev, or NULL if the number is zero (indicating
8679 an empty DIE). In either case *BYTES_READ will be set to the length of
8680 the initial number. */
8681
8682 static struct abbrev_info *
8683 peek_die_abbrev (const die_reader_specs &reader,
8684 const gdb_byte *info_ptr, unsigned int *bytes_read)
8685 {
8686 dwarf2_cu *cu = reader.cu;
8687 bfd *abfd = cu->per_objfile->objfile->obfd;
8688 unsigned int abbrev_number
8689 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8690
8691 if (abbrev_number == 0)
8692 return NULL;
8693
8694 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8695 if (!abbrev)
8696 {
8697 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8698 " at offset %s [in module %s]"),
8699 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8700 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8701 }
8702
8703 return abbrev;
8704 }
8705
8706 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8707 Returns a pointer to the end of a series of DIEs, terminated by an empty
8708 DIE. Any children of the skipped DIEs will also be skipped. */
8709
8710 static const gdb_byte *
8711 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8712 {
8713 while (1)
8714 {
8715 unsigned int bytes_read;
8716 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8717
8718 if (abbrev == NULL)
8719 return info_ptr + bytes_read;
8720 else
8721 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8722 }
8723 }
8724
8725 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8726 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8727 abbrev corresponding to that skipped uleb128 should be passed in
8728 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8729 children. */
8730
8731 static const gdb_byte *
8732 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8733 struct abbrev_info *abbrev)
8734 {
8735 unsigned int bytes_read;
8736 struct attribute attr;
8737 bfd *abfd = reader->abfd;
8738 struct dwarf2_cu *cu = reader->cu;
8739 const gdb_byte *buffer = reader->buffer;
8740 const gdb_byte *buffer_end = reader->buffer_end;
8741 unsigned int form, i;
8742
8743 for (i = 0; i < abbrev->num_attrs; i++)
8744 {
8745 /* The only abbrev we care about is DW_AT_sibling. */
8746 if (abbrev->attrs[i].name == DW_AT_sibling)
8747 {
8748 bool ignored;
8749 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8750 &ignored);
8751 if (attr.form == DW_FORM_ref_addr)
8752 complaint (_("ignoring absolute DW_AT_sibling"));
8753 else
8754 {
8755 sect_offset off = attr.get_ref_die_offset ();
8756 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8757
8758 if (sibling_ptr < info_ptr)
8759 complaint (_("DW_AT_sibling points backwards"));
8760 else if (sibling_ptr > reader->buffer_end)
8761 reader->die_section->overflow_complaint ();
8762 else
8763 return sibling_ptr;
8764 }
8765 }
8766
8767 /* If it isn't DW_AT_sibling, skip this attribute. */
8768 form = abbrev->attrs[i].form;
8769 skip_attribute:
8770 switch (form)
8771 {
8772 case DW_FORM_ref_addr:
8773 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8774 and later it is offset sized. */
8775 if (cu->header.version == 2)
8776 info_ptr += cu->header.addr_size;
8777 else
8778 info_ptr += cu->header.offset_size;
8779 break;
8780 case DW_FORM_GNU_ref_alt:
8781 info_ptr += cu->header.offset_size;
8782 break;
8783 case DW_FORM_addr:
8784 info_ptr += cu->header.addr_size;
8785 break;
8786 case DW_FORM_data1:
8787 case DW_FORM_ref1:
8788 case DW_FORM_flag:
8789 case DW_FORM_strx1:
8790 info_ptr += 1;
8791 break;
8792 case DW_FORM_flag_present:
8793 case DW_FORM_implicit_const:
8794 break;
8795 case DW_FORM_data2:
8796 case DW_FORM_ref2:
8797 case DW_FORM_strx2:
8798 info_ptr += 2;
8799 break;
8800 case DW_FORM_strx3:
8801 info_ptr += 3;
8802 break;
8803 case DW_FORM_data4:
8804 case DW_FORM_ref4:
8805 case DW_FORM_strx4:
8806 info_ptr += 4;
8807 break;
8808 case DW_FORM_data8:
8809 case DW_FORM_ref8:
8810 case DW_FORM_ref_sig8:
8811 info_ptr += 8;
8812 break;
8813 case DW_FORM_data16:
8814 info_ptr += 16;
8815 break;
8816 case DW_FORM_string:
8817 read_direct_string (abfd, info_ptr, &bytes_read);
8818 info_ptr += bytes_read;
8819 break;
8820 case DW_FORM_sec_offset:
8821 case DW_FORM_strp:
8822 case DW_FORM_GNU_strp_alt:
8823 info_ptr += cu->header.offset_size;
8824 break;
8825 case DW_FORM_exprloc:
8826 case DW_FORM_block:
8827 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8828 info_ptr += bytes_read;
8829 break;
8830 case DW_FORM_block1:
8831 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8832 break;
8833 case DW_FORM_block2:
8834 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8835 break;
8836 case DW_FORM_block4:
8837 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8838 break;
8839 case DW_FORM_addrx:
8840 case DW_FORM_strx:
8841 case DW_FORM_sdata:
8842 case DW_FORM_udata:
8843 case DW_FORM_ref_udata:
8844 case DW_FORM_GNU_addr_index:
8845 case DW_FORM_GNU_str_index:
8846 case DW_FORM_rnglistx:
8847 case DW_FORM_loclistx:
8848 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8849 break;
8850 case DW_FORM_indirect:
8851 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8852 info_ptr += bytes_read;
8853 /* We need to continue parsing from here, so just go back to
8854 the top. */
8855 goto skip_attribute;
8856
8857 default:
8858 error (_("Dwarf Error: Cannot handle %s "
8859 "in DWARF reader [in module %s]"),
8860 dwarf_form_name (form),
8861 bfd_get_filename (abfd));
8862 }
8863 }
8864
8865 if (abbrev->has_children)
8866 return skip_children (reader, info_ptr);
8867 else
8868 return info_ptr;
8869 }
8870
8871 /* Locate ORIG_PDI's sibling.
8872 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8873
8874 static const gdb_byte *
8875 locate_pdi_sibling (const struct die_reader_specs *reader,
8876 struct partial_die_info *orig_pdi,
8877 const gdb_byte *info_ptr)
8878 {
8879 /* Do we know the sibling already? */
8880
8881 if (orig_pdi->sibling)
8882 return orig_pdi->sibling;
8883
8884 /* Are there any children to deal with? */
8885
8886 if (!orig_pdi->has_children)
8887 return info_ptr;
8888
8889 /* Skip the children the long way. */
8890
8891 return skip_children (reader, info_ptr);
8892 }
8893
8894 /* Expand this partial symbol table into a full symbol table. SELF is
8895 not NULL. */
8896
8897 void
8898 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8899 {
8900 struct dwarf2_per_objfile *dwarf2_per_objfile
8901 = get_dwarf2_per_objfile (objfile);
8902
8903 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8904
8905 /* If this psymtab is constructed from a debug-only objfile, the
8906 has_section_at_zero flag will not necessarily be correct. We
8907 can get the correct value for this flag by looking at the data
8908 associated with the (presumably stripped) associated objfile. */
8909 if (objfile->separate_debug_objfile_backlink)
8910 {
8911 struct dwarf2_per_objfile *dpo_backlink
8912 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8913
8914 dwarf2_per_objfile->per_bfd->has_section_at_zero
8915 = dpo_backlink->per_bfd->has_section_at_zero;
8916 }
8917
8918 expand_psymtab (objfile);
8919
8920 process_cu_includes (dwarf2_per_objfile);
8921 }
8922 \f
8923 /* Reading in full CUs. */
8924
8925 /* Add PER_CU to the queue. */
8926
8927 static void
8928 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8929 enum language pretend_language)
8930 {
8931 per_cu->queued = 1;
8932 per_cu->per_bfd->queue.emplace (per_cu, pretend_language);
8933 }
8934
8935 /* If PER_CU is not yet queued, add it to the queue.
8936 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8937 dependency.
8938 The result is non-zero if PER_CU was queued, otherwise the result is zero
8939 meaning either PER_CU is already queued or it is already loaded.
8940
8941 N.B. There is an invariant here that if a CU is queued then it is loaded.
8942 The caller is required to load PER_CU if we return non-zero. */
8943
8944 static int
8945 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8946 struct dwarf2_per_cu_data *per_cu,
8947 enum language pretend_language)
8948 {
8949 /* We may arrive here during partial symbol reading, if we need full
8950 DIEs to process an unusual case (e.g. template arguments). Do
8951 not queue PER_CU, just tell our caller to load its DIEs. */
8952 if (per_cu->per_bfd->reading_partial_symbols)
8953 {
8954 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8955 return 1;
8956 return 0;
8957 }
8958
8959 /* Mark the dependence relation so that we don't flush PER_CU
8960 too early. */
8961 if (dependent_cu != NULL)
8962 dwarf2_add_dependence (dependent_cu, per_cu);
8963
8964 /* If it's already on the queue, we have nothing to do. */
8965 if (per_cu->queued)
8966 return 0;
8967
8968 /* If the compilation unit is already loaded, just mark it as
8969 used. */
8970 if (per_cu->cu != NULL)
8971 {
8972 per_cu->cu->last_used = 0;
8973 return 0;
8974 }
8975
8976 /* Add it to the queue. */
8977 queue_comp_unit (per_cu, pretend_language);
8978
8979 return 1;
8980 }
8981
8982 /* Process the queue. */
8983
8984 static void
8985 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8986 {
8987 if (dwarf_read_debug)
8988 {
8989 fprintf_unfiltered (gdb_stdlog,
8990 "Expanding one or more symtabs of objfile %s ...\n",
8991 objfile_name (dwarf2_per_objfile->objfile));
8992 }
8993
8994 /* The queue starts out with one item, but following a DIE reference
8995 may load a new CU, adding it to the end of the queue. */
8996 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
8997 {
8998 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
8999
9000 if (!dwarf2_per_objfile->symtab_set_p (item.per_cu)
9001 /* Skip dummy CUs. */
9002 && item.per_cu->cu != NULL)
9003 {
9004 struct dwarf2_per_cu_data *per_cu = item.per_cu;
9005 unsigned int debug_print_threshold;
9006 char buf[100];
9007
9008 if (per_cu->is_debug_types)
9009 {
9010 struct signatured_type *sig_type =
9011 (struct signatured_type *) per_cu;
9012
9013 sprintf (buf, "TU %s at offset %s",
9014 hex_string (sig_type->signature),
9015 sect_offset_str (per_cu->sect_off));
9016 /* There can be 100s of TUs.
9017 Only print them in verbose mode. */
9018 debug_print_threshold = 2;
9019 }
9020 else
9021 {
9022 sprintf (buf, "CU at offset %s",
9023 sect_offset_str (per_cu->sect_off));
9024 debug_print_threshold = 1;
9025 }
9026
9027 if (dwarf_read_debug >= debug_print_threshold)
9028 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9029
9030 if (per_cu->is_debug_types)
9031 process_full_type_unit (per_cu, item.pretend_language);
9032 else
9033 process_full_comp_unit (per_cu, item.pretend_language);
9034
9035 if (dwarf_read_debug >= debug_print_threshold)
9036 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9037 }
9038
9039 item.per_cu->queued = 0;
9040 dwarf2_per_objfile->per_bfd->queue.pop ();
9041 }
9042
9043 if (dwarf_read_debug)
9044 {
9045 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9046 objfile_name (dwarf2_per_objfile->objfile));
9047 }
9048 }
9049
9050 /* Read in full symbols for PST, and anything it depends on. */
9051
9052 void
9053 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9054 {
9055 gdb_assert (!readin_p (objfile));
9056
9057 expand_dependencies (objfile);
9058
9059 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9060 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9061 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9062 }
9063
9064 /* See psympriv.h. */
9065
9066 bool
9067 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9068 {
9069 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9070 return per_objfile->symtab_set_p (per_cu_data);
9071 }
9072
9073 /* See psympriv.h. */
9074
9075 compunit_symtab *
9076 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9077 {
9078 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9079 return per_objfile->get_symtab (per_cu_data);
9080 }
9081
9082 /* Trivial hash function for die_info: the hash value of a DIE
9083 is its offset in .debug_info for this objfile. */
9084
9085 static hashval_t
9086 die_hash (const void *item)
9087 {
9088 const struct die_info *die = (const struct die_info *) item;
9089
9090 return to_underlying (die->sect_off);
9091 }
9092
9093 /* Trivial comparison function for die_info structures: two DIEs
9094 are equal if they have the same offset. */
9095
9096 static int
9097 die_eq (const void *item_lhs, const void *item_rhs)
9098 {
9099 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9100 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9101
9102 return die_lhs->sect_off == die_rhs->sect_off;
9103 }
9104
9105 /* Load the DIEs associated with PER_CU into memory. */
9106
9107 static void
9108 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9109 dwarf2_per_objfile *per_objfile,
9110 bool skip_partial,
9111 enum language pretend_language)
9112 {
9113 gdb_assert (! this_cu->is_debug_types);
9114
9115 cutu_reader reader (this_cu, per_objfile, NULL, 1, skip_partial);
9116 if (reader.dummy_p)
9117 return;
9118
9119 struct dwarf2_cu *cu = reader.cu;
9120 const gdb_byte *info_ptr = reader.info_ptr;
9121
9122 gdb_assert (cu->die_hash == NULL);
9123 cu->die_hash =
9124 htab_create_alloc_ex (cu->header.length / 12,
9125 die_hash,
9126 die_eq,
9127 NULL,
9128 &cu->comp_unit_obstack,
9129 hashtab_obstack_allocate,
9130 dummy_obstack_deallocate);
9131
9132 if (reader.comp_unit_die->has_children)
9133 reader.comp_unit_die->child
9134 = read_die_and_siblings (&reader, reader.info_ptr,
9135 &info_ptr, reader.comp_unit_die);
9136 cu->dies = reader.comp_unit_die;
9137 /* comp_unit_die is not stored in die_hash, no need. */
9138
9139 /* We try not to read any attributes in this function, because not
9140 all CUs needed for references have been loaded yet, and symbol
9141 table processing isn't initialized. But we have to set the CU language,
9142 or we won't be able to build types correctly.
9143 Similarly, if we do not read the producer, we can not apply
9144 producer-specific interpretation. */
9145 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9146
9147 reader.keep ();
9148 }
9149
9150 /* Add a DIE to the delayed physname list. */
9151
9152 static void
9153 add_to_method_list (struct type *type, int fnfield_index, int index,
9154 const char *name, struct die_info *die,
9155 struct dwarf2_cu *cu)
9156 {
9157 struct delayed_method_info mi;
9158 mi.type = type;
9159 mi.fnfield_index = fnfield_index;
9160 mi.index = index;
9161 mi.name = name;
9162 mi.die = die;
9163 cu->method_list.push_back (mi);
9164 }
9165
9166 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9167 "const" / "volatile". If so, decrements LEN by the length of the
9168 modifier and return true. Otherwise return false. */
9169
9170 template<size_t N>
9171 static bool
9172 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9173 {
9174 size_t mod_len = sizeof (mod) - 1;
9175 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9176 {
9177 len -= mod_len;
9178 return true;
9179 }
9180 return false;
9181 }
9182
9183 /* Compute the physnames of any methods on the CU's method list.
9184
9185 The computation of method physnames is delayed in order to avoid the
9186 (bad) condition that one of the method's formal parameters is of an as yet
9187 incomplete type. */
9188
9189 static void
9190 compute_delayed_physnames (struct dwarf2_cu *cu)
9191 {
9192 /* Only C++ delays computing physnames. */
9193 if (cu->method_list.empty ())
9194 return;
9195 gdb_assert (cu->language == language_cplus);
9196
9197 for (const delayed_method_info &mi : cu->method_list)
9198 {
9199 const char *physname;
9200 struct fn_fieldlist *fn_flp
9201 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9202 physname = dwarf2_physname (mi.name, mi.die, cu);
9203 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9204 = physname ? physname : "";
9205
9206 /* Since there's no tag to indicate whether a method is a
9207 const/volatile overload, extract that information out of the
9208 demangled name. */
9209 if (physname != NULL)
9210 {
9211 size_t len = strlen (physname);
9212
9213 while (1)
9214 {
9215 if (physname[len] == ')') /* shortcut */
9216 break;
9217 else if (check_modifier (physname, len, " const"))
9218 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9219 else if (check_modifier (physname, len, " volatile"))
9220 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9221 else
9222 break;
9223 }
9224 }
9225 }
9226
9227 /* The list is no longer needed. */
9228 cu->method_list.clear ();
9229 }
9230
9231 /* Go objects should be embedded in a DW_TAG_module DIE,
9232 and it's not clear if/how imported objects will appear.
9233 To keep Go support simple until that's worked out,
9234 go back through what we've read and create something usable.
9235 We could do this while processing each DIE, and feels kinda cleaner,
9236 but that way is more invasive.
9237 This is to, for example, allow the user to type "p var" or "b main"
9238 without having to specify the package name, and allow lookups
9239 of module.object to work in contexts that use the expression
9240 parser. */
9241
9242 static void
9243 fixup_go_packaging (struct dwarf2_cu *cu)
9244 {
9245 gdb::unique_xmalloc_ptr<char> package_name;
9246 struct pending *list;
9247 int i;
9248
9249 for (list = *cu->get_builder ()->get_global_symbols ();
9250 list != NULL;
9251 list = list->next)
9252 {
9253 for (i = 0; i < list->nsyms; ++i)
9254 {
9255 struct symbol *sym = list->symbol[i];
9256
9257 if (sym->language () == language_go
9258 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9259 {
9260 gdb::unique_xmalloc_ptr<char> this_package_name
9261 (go_symbol_package_name (sym));
9262
9263 if (this_package_name == NULL)
9264 continue;
9265 if (package_name == NULL)
9266 package_name = std::move (this_package_name);
9267 else
9268 {
9269 struct objfile *objfile = cu->per_objfile->objfile;
9270 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9271 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9272 (symbol_symtab (sym) != NULL
9273 ? symtab_to_filename_for_display
9274 (symbol_symtab (sym))
9275 : objfile_name (objfile)),
9276 this_package_name.get (), package_name.get ());
9277 }
9278 }
9279 }
9280 }
9281
9282 if (package_name != NULL)
9283 {
9284 struct objfile *objfile = cu->per_objfile->objfile;
9285 const char *saved_package_name = objfile->intern (package_name.get ());
9286 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9287 saved_package_name);
9288 struct symbol *sym;
9289
9290 sym = new (&objfile->objfile_obstack) symbol;
9291 sym->set_language (language_go, &objfile->objfile_obstack);
9292 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9293 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9294 e.g., "main" finds the "main" module and not C's main(). */
9295 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9296 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9297 SYMBOL_TYPE (sym) = type;
9298
9299 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9300 }
9301 }
9302
9303 /* Allocate a fully-qualified name consisting of the two parts on the
9304 obstack. */
9305
9306 static const char *
9307 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9308 {
9309 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9310 }
9311
9312 /* A helper that allocates a variant part to attach to a Rust enum
9313 type. OBSTACK is where the results should be allocated. TYPE is
9314 the type we're processing. DISCRIMINANT_INDEX is the index of the
9315 discriminant. It must be the index of one of the fields of TYPE.
9316 DEFAULT_INDEX is the index of the default field; or -1 if there is
9317 no default. RANGES is indexed by "effective" field number (the
9318 field index, but omitting the discriminant and default fields) and
9319 must hold the discriminant values used by the variants. Note that
9320 RANGES must have a lifetime at least as long as OBSTACK -- either
9321 already allocated on it, or static. */
9322
9323 static void
9324 alloc_rust_variant (struct obstack *obstack, struct type *type,
9325 int discriminant_index, int default_index,
9326 gdb::array_view<discriminant_range> ranges)
9327 {
9328 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9329 must be handled by the caller. */
9330 gdb_assert (discriminant_index >= 0
9331 && discriminant_index < type->num_fields ());
9332 gdb_assert (default_index == -1
9333 || (default_index >= 0 && default_index < type->num_fields ()));
9334
9335 /* We have one variant for each non-discriminant field. */
9336 int n_variants = type->num_fields () - 1;
9337
9338 variant *variants = new (obstack) variant[n_variants];
9339 int var_idx = 0;
9340 int range_idx = 0;
9341 for (int i = 0; i < type->num_fields (); ++i)
9342 {
9343 if (i == discriminant_index)
9344 continue;
9345
9346 variants[var_idx].first_field = i;
9347 variants[var_idx].last_field = i + 1;
9348
9349 /* The default field does not need a range, but other fields do.
9350 We skipped the discriminant above. */
9351 if (i != default_index)
9352 {
9353 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9354 ++range_idx;
9355 }
9356
9357 ++var_idx;
9358 }
9359
9360 gdb_assert (range_idx == ranges.size ());
9361 gdb_assert (var_idx == n_variants);
9362
9363 variant_part *part = new (obstack) variant_part;
9364 part->discriminant_index = discriminant_index;
9365 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9366 discriminant_index));
9367 part->variants = gdb::array_view<variant> (variants, n_variants);
9368
9369 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9370 gdb::array_view<variant_part> *prop_value
9371 = new (storage) gdb::array_view<variant_part> (part, 1);
9372
9373 struct dynamic_prop prop;
9374 prop.kind = PROP_VARIANT_PARTS;
9375 prop.data.variant_parts = prop_value;
9376
9377 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9378 }
9379
9380 /* Some versions of rustc emitted enums in an unusual way.
9381
9382 Ordinary enums were emitted as unions. The first element of each
9383 structure in the union was named "RUST$ENUM$DISR". This element
9384 held the discriminant.
9385
9386 These versions of Rust also implemented the "non-zero"
9387 optimization. When the enum had two values, and one is empty and
9388 the other holds a pointer that cannot be zero, the pointer is used
9389 as the discriminant, with a zero value meaning the empty variant.
9390 Here, the union's first member is of the form
9391 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9392 where the fieldnos are the indices of the fields that should be
9393 traversed in order to find the field (which may be several fields deep)
9394 and the variantname is the name of the variant of the case when the
9395 field is zero.
9396
9397 This function recognizes whether TYPE is of one of these forms,
9398 and, if so, smashes it to be a variant type. */
9399
9400 static void
9401 quirk_rust_enum (struct type *type, struct objfile *objfile)
9402 {
9403 gdb_assert (type->code () == TYPE_CODE_UNION);
9404
9405 /* We don't need to deal with empty enums. */
9406 if (type->num_fields () == 0)
9407 return;
9408
9409 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9410 if (type->num_fields () == 1
9411 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9412 {
9413 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9414
9415 /* Decode the field name to find the offset of the
9416 discriminant. */
9417 ULONGEST bit_offset = 0;
9418 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9419 while (name[0] >= '0' && name[0] <= '9')
9420 {
9421 char *tail;
9422 unsigned long index = strtoul (name, &tail, 10);
9423 name = tail;
9424 if (*name != '$'
9425 || index >= field_type->num_fields ()
9426 || (TYPE_FIELD_LOC_KIND (field_type, index)
9427 != FIELD_LOC_KIND_BITPOS))
9428 {
9429 complaint (_("Could not parse Rust enum encoding string \"%s\""
9430 "[in module %s]"),
9431 TYPE_FIELD_NAME (type, 0),
9432 objfile_name (objfile));
9433 return;
9434 }
9435 ++name;
9436
9437 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9438 field_type = TYPE_FIELD_TYPE (field_type, index);
9439 }
9440
9441 /* Smash this type to be a structure type. We have to do this
9442 because the type has already been recorded. */
9443 type->set_code (TYPE_CODE_STRUCT);
9444 type->set_num_fields (3);
9445 /* Save the field we care about. */
9446 struct field saved_field = type->field (0);
9447 type->set_fields
9448 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9449
9450 /* Put the discriminant at index 0. */
9451 TYPE_FIELD_TYPE (type, 0) = field_type;
9452 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9453 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9454 SET_FIELD_BITPOS (type->field (0), bit_offset);
9455
9456 /* The order of fields doesn't really matter, so put the real
9457 field at index 1 and the data-less field at index 2. */
9458 type->field (1) = saved_field;
9459 TYPE_FIELD_NAME (type, 1)
9460 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
9461 TYPE_FIELD_TYPE (type, 1)->set_name
9462 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9463 TYPE_FIELD_NAME (type, 1)));
9464
9465 const char *dataless_name
9466 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9467 name);
9468 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9469 dataless_name);
9470 TYPE_FIELD_TYPE (type, 2) = dataless_type;
9471 /* NAME points into the original discriminant name, which
9472 already has the correct lifetime. */
9473 TYPE_FIELD_NAME (type, 2) = name;
9474 SET_FIELD_BITPOS (type->field (2), 0);
9475
9476 /* Indicate that this is a variant type. */
9477 static discriminant_range ranges[1] = { { 0, 0 } };
9478 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9479 }
9480 /* A union with a single anonymous field is probably an old-style
9481 univariant enum. */
9482 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9483 {
9484 /* Smash this type to be a structure type. We have to do this
9485 because the type has already been recorded. */
9486 type->set_code (TYPE_CODE_STRUCT);
9487
9488 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9489 const char *variant_name
9490 = rust_last_path_segment (field_type->name ());
9491 TYPE_FIELD_NAME (type, 0) = variant_name;
9492 field_type->set_name
9493 (rust_fully_qualify (&objfile->objfile_obstack,
9494 type->name (), variant_name));
9495 }
9496 else
9497 {
9498 struct type *disr_type = nullptr;
9499 for (int i = 0; i < type->num_fields (); ++i)
9500 {
9501 disr_type = TYPE_FIELD_TYPE (type, i);
9502
9503 if (disr_type->code () != TYPE_CODE_STRUCT)
9504 {
9505 /* All fields of a true enum will be structs. */
9506 return;
9507 }
9508 else if (disr_type->num_fields () == 0)
9509 {
9510 /* Could be data-less variant, so keep going. */
9511 disr_type = nullptr;
9512 }
9513 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9514 "RUST$ENUM$DISR") != 0)
9515 {
9516 /* Not a Rust enum. */
9517 return;
9518 }
9519 else
9520 {
9521 /* Found one. */
9522 break;
9523 }
9524 }
9525
9526 /* If we got here without a discriminant, then it's probably
9527 just a union. */
9528 if (disr_type == nullptr)
9529 return;
9530
9531 /* Smash this type to be a structure type. We have to do this
9532 because the type has already been recorded. */
9533 type->set_code (TYPE_CODE_STRUCT);
9534
9535 /* Make space for the discriminant field. */
9536 struct field *disr_field = &disr_type->field (0);
9537 field *new_fields
9538 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9539 * sizeof (struct field)));
9540 memcpy (new_fields + 1, type->fields (),
9541 type->num_fields () * sizeof (struct field));
9542 type->set_fields (new_fields);
9543 type->set_num_fields (type->num_fields () + 1);
9544
9545 /* Install the discriminant at index 0 in the union. */
9546 type->field (0) = *disr_field;
9547 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9548 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9549
9550 /* We need a way to find the correct discriminant given a
9551 variant name. For convenience we build a map here. */
9552 struct type *enum_type = FIELD_TYPE (*disr_field);
9553 std::unordered_map<std::string, ULONGEST> discriminant_map;
9554 for (int i = 0; i < enum_type->num_fields (); ++i)
9555 {
9556 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9557 {
9558 const char *name
9559 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9560 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9561 }
9562 }
9563
9564 int n_fields = type->num_fields ();
9565 /* We don't need a range entry for the discriminant, but we do
9566 need one for every other field, as there is no default
9567 variant. */
9568 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9569 discriminant_range,
9570 n_fields - 1);
9571 /* Skip the discriminant here. */
9572 for (int i = 1; i < n_fields; ++i)
9573 {
9574 /* Find the final word in the name of this variant's type.
9575 That name can be used to look up the correct
9576 discriminant. */
9577 const char *variant_name
9578 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
9579
9580 auto iter = discriminant_map.find (variant_name);
9581 if (iter != discriminant_map.end ())
9582 {
9583 ranges[i].low = iter->second;
9584 ranges[i].high = iter->second;
9585 }
9586
9587 /* Remove the discriminant field, if it exists. */
9588 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
9589 if (sub_type->num_fields () > 0)
9590 {
9591 sub_type->set_num_fields (sub_type->num_fields () - 1);
9592 sub_type->set_fields (sub_type->fields () + 1);
9593 }
9594 TYPE_FIELD_NAME (type, i) = variant_name;
9595 sub_type->set_name
9596 (rust_fully_qualify (&objfile->objfile_obstack,
9597 type->name (), variant_name));
9598 }
9599
9600 /* Indicate that this is a variant type. */
9601 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9602 gdb::array_view<discriminant_range> (ranges,
9603 n_fields - 1));
9604 }
9605 }
9606
9607 /* Rewrite some Rust unions to be structures with variants parts. */
9608
9609 static void
9610 rust_union_quirks (struct dwarf2_cu *cu)
9611 {
9612 gdb_assert (cu->language == language_rust);
9613 for (type *type_ : cu->rust_unions)
9614 quirk_rust_enum (type_, cu->per_objfile->objfile);
9615 /* We don't need this any more. */
9616 cu->rust_unions.clear ();
9617 }
9618
9619 /* A helper function for computing the list of all symbol tables
9620 included by PER_CU. */
9621
9622 static void
9623 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9624 htab_t all_children, htab_t all_type_symtabs,
9625 struct dwarf2_per_cu_data *per_cu,
9626 struct compunit_symtab *immediate_parent)
9627 {
9628 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9629 if (*slot != NULL)
9630 {
9631 /* This inclusion and its children have been processed. */
9632 return;
9633 }
9634
9635 *slot = per_cu;
9636
9637 /* Only add a CU if it has a symbol table. */
9638 compunit_symtab *cust = per_cu->dwarf2_per_objfile->get_symtab (per_cu);
9639 if (cust != NULL)
9640 {
9641 /* If this is a type unit only add its symbol table if we haven't
9642 seen it yet (type unit per_cu's can share symtabs). */
9643 if (per_cu->is_debug_types)
9644 {
9645 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9646 if (*slot == NULL)
9647 {
9648 *slot = cust;
9649 result->push_back (cust);
9650 if (cust->user == NULL)
9651 cust->user = immediate_parent;
9652 }
9653 }
9654 else
9655 {
9656 result->push_back (cust);
9657 if (cust->user == NULL)
9658 cust->user = immediate_parent;
9659 }
9660 }
9661
9662 if (!per_cu->imported_symtabs_empty ())
9663 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9664 {
9665 recursively_compute_inclusions (result, all_children,
9666 all_type_symtabs, ptr, cust);
9667 }
9668 }
9669
9670 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9671 PER_CU. */
9672
9673 static void
9674 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9675 {
9676 gdb_assert (! per_cu->is_debug_types);
9677
9678 if (!per_cu->imported_symtabs_empty ())
9679 {
9680 int len;
9681 std::vector<compunit_symtab *> result_symtabs;
9682 htab_t all_children, all_type_symtabs;
9683 compunit_symtab *cust = per_cu->dwarf2_per_objfile->get_symtab (per_cu);
9684
9685 /* If we don't have a symtab, we can just skip this case. */
9686 if (cust == NULL)
9687 return;
9688
9689 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9690 NULL, xcalloc, xfree);
9691 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9692 NULL, xcalloc, xfree);
9693
9694 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9695 {
9696 recursively_compute_inclusions (&result_symtabs, all_children,
9697 all_type_symtabs, ptr, cust);
9698 }
9699
9700 /* Now we have a transitive closure of all the included symtabs. */
9701 len = result_symtabs.size ();
9702 cust->includes
9703 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9704 struct compunit_symtab *, len + 1);
9705 memcpy (cust->includes, result_symtabs.data (),
9706 len * sizeof (compunit_symtab *));
9707 cust->includes[len] = NULL;
9708
9709 htab_delete (all_children);
9710 htab_delete (all_type_symtabs);
9711 }
9712 }
9713
9714 /* Compute the 'includes' field for the symtabs of all the CUs we just
9715 read. */
9716
9717 static void
9718 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9719 {
9720 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
9721 {
9722 if (! iter->is_debug_types)
9723 compute_compunit_symtab_includes (iter);
9724 }
9725
9726 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
9727 }
9728
9729 /* Generate full symbol information for PER_CU, whose DIEs have
9730 already been loaded into memory. */
9731
9732 static void
9733 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9734 enum language pretend_language)
9735 {
9736 struct dwarf2_cu *cu = per_cu->cu;
9737 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9738 struct objfile *objfile = dwarf2_per_objfile->objfile;
9739 struct gdbarch *gdbarch = objfile->arch ();
9740 CORE_ADDR lowpc, highpc;
9741 struct compunit_symtab *cust;
9742 CORE_ADDR baseaddr;
9743 struct block *static_block;
9744 CORE_ADDR addr;
9745
9746 baseaddr = objfile->text_section_offset ();
9747
9748 /* Clear the list here in case something was left over. */
9749 cu->method_list.clear ();
9750
9751 cu->language = pretend_language;
9752 cu->language_defn = language_def (cu->language);
9753
9754 /* Do line number decoding in read_file_scope () */
9755 process_die (cu->dies, cu);
9756
9757 /* For now fudge the Go package. */
9758 if (cu->language == language_go)
9759 fixup_go_packaging (cu);
9760
9761 /* Now that we have processed all the DIEs in the CU, all the types
9762 should be complete, and it should now be safe to compute all of the
9763 physnames. */
9764 compute_delayed_physnames (cu);
9765
9766 if (cu->language == language_rust)
9767 rust_union_quirks (cu);
9768
9769 /* Some compilers don't define a DW_AT_high_pc attribute for the
9770 compilation unit. If the DW_AT_high_pc is missing, synthesize
9771 it, by scanning the DIE's below the compilation unit. */
9772 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9773
9774 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9775 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9776
9777 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9778 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9779 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9780 addrmap to help ensure it has an accurate map of pc values belonging to
9781 this comp unit. */
9782 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9783
9784 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9785 SECT_OFF_TEXT (objfile),
9786 0);
9787
9788 if (cust != NULL)
9789 {
9790 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9791
9792 /* Set symtab language to language from DW_AT_language. If the
9793 compilation is from a C file generated by language preprocessors, do
9794 not set the language if it was already deduced by start_subfile. */
9795 if (!(cu->language == language_c
9796 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9797 COMPUNIT_FILETABS (cust)->language = cu->language;
9798
9799 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9800 produce DW_AT_location with location lists but it can be possibly
9801 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9802 there were bugs in prologue debug info, fixed later in GCC-4.5
9803 by "unwind info for epilogues" patch (which is not directly related).
9804
9805 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9806 needed, it would be wrong due to missing DW_AT_producer there.
9807
9808 Still one can confuse GDB by using non-standard GCC compilation
9809 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9810 */
9811 if (cu->has_loclist && gcc_4_minor >= 5)
9812 cust->locations_valid = 1;
9813
9814 if (gcc_4_minor >= 5)
9815 cust->epilogue_unwind_valid = 1;
9816
9817 cust->call_site_htab = cu->call_site_htab;
9818 }
9819
9820 dwarf2_per_objfile->set_symtab (per_cu, cust);
9821
9822 /* Push it for inclusion processing later. */
9823 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (per_cu);
9824
9825 /* Not needed any more. */
9826 cu->reset_builder ();
9827 }
9828
9829 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9830 already been loaded into memory. */
9831
9832 static void
9833 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9834 enum language pretend_language)
9835 {
9836 struct dwarf2_cu *cu = per_cu->cu;
9837 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9838 struct objfile *objfile = dwarf2_per_objfile->objfile;
9839 struct compunit_symtab *cust;
9840 struct signatured_type *sig_type;
9841
9842 gdb_assert (per_cu->is_debug_types);
9843 sig_type = (struct signatured_type *) per_cu;
9844
9845 /* Clear the list here in case something was left over. */
9846 cu->method_list.clear ();
9847
9848 cu->language = pretend_language;
9849 cu->language_defn = language_def (cu->language);
9850
9851 /* The symbol tables are set up in read_type_unit_scope. */
9852 process_die (cu->dies, cu);
9853
9854 /* For now fudge the Go package. */
9855 if (cu->language == language_go)
9856 fixup_go_packaging (cu);
9857
9858 /* Now that we have processed all the DIEs in the CU, all the types
9859 should be complete, and it should now be safe to compute all of the
9860 physnames. */
9861 compute_delayed_physnames (cu);
9862
9863 if (cu->language == language_rust)
9864 rust_union_quirks (cu);
9865
9866 /* TUs share symbol tables.
9867 If this is the first TU to use this symtab, complete the construction
9868 of it with end_expandable_symtab. Otherwise, complete the addition of
9869 this TU's symbols to the existing symtab. */
9870 if (sig_type->type_unit_group->compunit_symtab == NULL)
9871 {
9872 buildsym_compunit *builder = cu->get_builder ();
9873 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9874 sig_type->type_unit_group->compunit_symtab = cust;
9875
9876 if (cust != NULL)
9877 {
9878 /* Set symtab language to language from DW_AT_language. If the
9879 compilation is from a C file generated by language preprocessors,
9880 do not set the language if it was already deduced by
9881 start_subfile. */
9882 if (!(cu->language == language_c
9883 && COMPUNIT_FILETABS (cust)->language != language_c))
9884 COMPUNIT_FILETABS (cust)->language = cu->language;
9885 }
9886 }
9887 else
9888 {
9889 cu->get_builder ()->augment_type_symtab ();
9890 cust = sig_type->type_unit_group->compunit_symtab;
9891 }
9892
9893 dwarf2_per_objfile->set_symtab (per_cu, cust);
9894
9895 /* Not needed any more. */
9896 cu->reset_builder ();
9897 }
9898
9899 /* Process an imported unit DIE. */
9900
9901 static void
9902 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9903 {
9904 struct attribute *attr;
9905
9906 /* For now we don't handle imported units in type units. */
9907 if (cu->per_cu->is_debug_types)
9908 {
9909 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9910 " supported in type units [in module %s]"),
9911 objfile_name (cu->per_objfile->objfile));
9912 }
9913
9914 attr = dwarf2_attr (die, DW_AT_import, cu);
9915 if (attr != NULL)
9916 {
9917 sect_offset sect_off = attr->get_ref_die_offset ();
9918 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9919 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9920 dwarf2_per_cu_data *per_cu
9921 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
9922
9923 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9924 into another compilation unit, at root level. Regard this as a hint,
9925 and ignore it. */
9926 if (die->parent && die->parent->parent == NULL
9927 && per_cu->unit_type == DW_UT_compile
9928 && per_cu->lang == language_cplus)
9929 return;
9930
9931 /* If necessary, add it to the queue and load its DIEs. */
9932 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9933 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
9934
9935 cu->per_cu->imported_symtabs_push (per_cu);
9936 }
9937 }
9938
9939 /* RAII object that represents a process_die scope: i.e.,
9940 starts/finishes processing a DIE. */
9941 class process_die_scope
9942 {
9943 public:
9944 process_die_scope (die_info *die, dwarf2_cu *cu)
9945 : m_die (die), m_cu (cu)
9946 {
9947 /* We should only be processing DIEs not already in process. */
9948 gdb_assert (!m_die->in_process);
9949 m_die->in_process = true;
9950 }
9951
9952 ~process_die_scope ()
9953 {
9954 m_die->in_process = false;
9955
9956 /* If we're done processing the DIE for the CU that owns the line
9957 header, we don't need the line header anymore. */
9958 if (m_cu->line_header_die_owner == m_die)
9959 {
9960 delete m_cu->line_header;
9961 m_cu->line_header = NULL;
9962 m_cu->line_header_die_owner = NULL;
9963 }
9964 }
9965
9966 private:
9967 die_info *m_die;
9968 dwarf2_cu *m_cu;
9969 };
9970
9971 /* Process a die and its children. */
9972
9973 static void
9974 process_die (struct die_info *die, struct dwarf2_cu *cu)
9975 {
9976 process_die_scope scope (die, cu);
9977
9978 switch (die->tag)
9979 {
9980 case DW_TAG_padding:
9981 break;
9982 case DW_TAG_compile_unit:
9983 case DW_TAG_partial_unit:
9984 read_file_scope (die, cu);
9985 break;
9986 case DW_TAG_type_unit:
9987 read_type_unit_scope (die, cu);
9988 break;
9989 case DW_TAG_subprogram:
9990 /* Nested subprograms in Fortran get a prefix. */
9991 if (cu->language == language_fortran
9992 && die->parent != NULL
9993 && die->parent->tag == DW_TAG_subprogram)
9994 cu->processing_has_namespace_info = true;
9995 /* Fall through. */
9996 case DW_TAG_inlined_subroutine:
9997 read_func_scope (die, cu);
9998 break;
9999 case DW_TAG_lexical_block:
10000 case DW_TAG_try_block:
10001 case DW_TAG_catch_block:
10002 read_lexical_block_scope (die, cu);
10003 break;
10004 case DW_TAG_call_site:
10005 case DW_TAG_GNU_call_site:
10006 read_call_site_scope (die, cu);
10007 break;
10008 case DW_TAG_class_type:
10009 case DW_TAG_interface_type:
10010 case DW_TAG_structure_type:
10011 case DW_TAG_union_type:
10012 process_structure_scope (die, cu);
10013 break;
10014 case DW_TAG_enumeration_type:
10015 process_enumeration_scope (die, cu);
10016 break;
10017
10018 /* These dies have a type, but processing them does not create
10019 a symbol or recurse to process the children. Therefore we can
10020 read them on-demand through read_type_die. */
10021 case DW_TAG_subroutine_type:
10022 case DW_TAG_set_type:
10023 case DW_TAG_array_type:
10024 case DW_TAG_pointer_type:
10025 case DW_TAG_ptr_to_member_type:
10026 case DW_TAG_reference_type:
10027 case DW_TAG_rvalue_reference_type:
10028 case DW_TAG_string_type:
10029 break;
10030
10031 case DW_TAG_base_type:
10032 case DW_TAG_subrange_type:
10033 case DW_TAG_typedef:
10034 /* Add a typedef symbol for the type definition, if it has a
10035 DW_AT_name. */
10036 new_symbol (die, read_type_die (die, cu), cu);
10037 break;
10038 case DW_TAG_common_block:
10039 read_common_block (die, cu);
10040 break;
10041 case DW_TAG_common_inclusion:
10042 break;
10043 case DW_TAG_namespace:
10044 cu->processing_has_namespace_info = true;
10045 read_namespace (die, cu);
10046 break;
10047 case DW_TAG_module:
10048 cu->processing_has_namespace_info = true;
10049 read_module (die, cu);
10050 break;
10051 case DW_TAG_imported_declaration:
10052 cu->processing_has_namespace_info = true;
10053 if (read_namespace_alias (die, cu))
10054 break;
10055 /* The declaration is not a global namespace alias. */
10056 /* Fall through. */
10057 case DW_TAG_imported_module:
10058 cu->processing_has_namespace_info = true;
10059 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10060 || cu->language != language_fortran))
10061 complaint (_("Tag '%s' has unexpected children"),
10062 dwarf_tag_name (die->tag));
10063 read_import_statement (die, cu);
10064 break;
10065
10066 case DW_TAG_imported_unit:
10067 process_imported_unit_die (die, cu);
10068 break;
10069
10070 case DW_TAG_variable:
10071 read_variable (die, cu);
10072 break;
10073
10074 default:
10075 new_symbol (die, NULL, cu);
10076 break;
10077 }
10078 }
10079 \f
10080 /* DWARF name computation. */
10081
10082 /* A helper function for dwarf2_compute_name which determines whether DIE
10083 needs to have the name of the scope prepended to the name listed in the
10084 die. */
10085
10086 static int
10087 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10088 {
10089 struct attribute *attr;
10090
10091 switch (die->tag)
10092 {
10093 case DW_TAG_namespace:
10094 case DW_TAG_typedef:
10095 case DW_TAG_class_type:
10096 case DW_TAG_interface_type:
10097 case DW_TAG_structure_type:
10098 case DW_TAG_union_type:
10099 case DW_TAG_enumeration_type:
10100 case DW_TAG_enumerator:
10101 case DW_TAG_subprogram:
10102 case DW_TAG_inlined_subroutine:
10103 case DW_TAG_member:
10104 case DW_TAG_imported_declaration:
10105 return 1;
10106
10107 case DW_TAG_variable:
10108 case DW_TAG_constant:
10109 /* We only need to prefix "globally" visible variables. These include
10110 any variable marked with DW_AT_external or any variable that
10111 lives in a namespace. [Variables in anonymous namespaces
10112 require prefixing, but they are not DW_AT_external.] */
10113
10114 if (dwarf2_attr (die, DW_AT_specification, cu))
10115 {
10116 struct dwarf2_cu *spec_cu = cu;
10117
10118 return die_needs_namespace (die_specification (die, &spec_cu),
10119 spec_cu);
10120 }
10121
10122 attr = dwarf2_attr (die, DW_AT_external, cu);
10123 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10124 && die->parent->tag != DW_TAG_module)
10125 return 0;
10126 /* A variable in a lexical block of some kind does not need a
10127 namespace, even though in C++ such variables may be external
10128 and have a mangled name. */
10129 if (die->parent->tag == DW_TAG_lexical_block
10130 || die->parent->tag == DW_TAG_try_block
10131 || die->parent->tag == DW_TAG_catch_block
10132 || die->parent->tag == DW_TAG_subprogram)
10133 return 0;
10134 return 1;
10135
10136 default:
10137 return 0;
10138 }
10139 }
10140
10141 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10142 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10143 defined for the given DIE. */
10144
10145 static struct attribute *
10146 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10147 {
10148 struct attribute *attr;
10149
10150 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10151 if (attr == NULL)
10152 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10153
10154 return attr;
10155 }
10156
10157 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10158 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10159 defined for the given DIE. */
10160
10161 static const char *
10162 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10163 {
10164 const char *linkage_name;
10165
10166 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10167 if (linkage_name == NULL)
10168 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10169
10170 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10171 See https://github.com/rust-lang/rust/issues/32925. */
10172 if (cu->language == language_rust && linkage_name != NULL
10173 && strchr (linkage_name, '{') != NULL)
10174 linkage_name = NULL;
10175
10176 return linkage_name;
10177 }
10178
10179 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10180 compute the physname for the object, which include a method's:
10181 - formal parameters (C++),
10182 - receiver type (Go),
10183
10184 The term "physname" is a bit confusing.
10185 For C++, for example, it is the demangled name.
10186 For Go, for example, it's the mangled name.
10187
10188 For Ada, return the DIE's linkage name rather than the fully qualified
10189 name. PHYSNAME is ignored..
10190
10191 The result is allocated on the objfile->per_bfd's obstack and
10192 canonicalized. */
10193
10194 static const char *
10195 dwarf2_compute_name (const char *name,
10196 struct die_info *die, struct dwarf2_cu *cu,
10197 int physname)
10198 {
10199 struct objfile *objfile = cu->per_objfile->objfile;
10200
10201 if (name == NULL)
10202 name = dwarf2_name (die, cu);
10203
10204 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10205 but otherwise compute it by typename_concat inside GDB.
10206 FIXME: Actually this is not really true, or at least not always true.
10207 It's all very confusing. compute_and_set_names doesn't try to demangle
10208 Fortran names because there is no mangling standard. So new_symbol
10209 will set the demangled name to the result of dwarf2_full_name, and it is
10210 the demangled name that GDB uses if it exists. */
10211 if (cu->language == language_ada
10212 || (cu->language == language_fortran && physname))
10213 {
10214 /* For Ada unit, we prefer the linkage name over the name, as
10215 the former contains the exported name, which the user expects
10216 to be able to reference. Ideally, we want the user to be able
10217 to reference this entity using either natural or linkage name,
10218 but we haven't started looking at this enhancement yet. */
10219 const char *linkage_name = dw2_linkage_name (die, cu);
10220
10221 if (linkage_name != NULL)
10222 return linkage_name;
10223 }
10224
10225 /* These are the only languages we know how to qualify names in. */
10226 if (name != NULL
10227 && (cu->language == language_cplus
10228 || cu->language == language_fortran || cu->language == language_d
10229 || cu->language == language_rust))
10230 {
10231 if (die_needs_namespace (die, cu))
10232 {
10233 const char *prefix;
10234 const char *canonical_name = NULL;
10235
10236 string_file buf;
10237
10238 prefix = determine_prefix (die, cu);
10239 if (*prefix != '\0')
10240 {
10241 gdb::unique_xmalloc_ptr<char> prefixed_name
10242 (typename_concat (NULL, prefix, name, physname, cu));
10243
10244 buf.puts (prefixed_name.get ());
10245 }
10246 else
10247 buf.puts (name);
10248
10249 /* Template parameters may be specified in the DIE's DW_AT_name, or
10250 as children with DW_TAG_template_type_param or
10251 DW_TAG_value_type_param. If the latter, add them to the name
10252 here. If the name already has template parameters, then
10253 skip this step; some versions of GCC emit both, and
10254 it is more efficient to use the pre-computed name.
10255
10256 Something to keep in mind about this process: it is very
10257 unlikely, or in some cases downright impossible, to produce
10258 something that will match the mangled name of a function.
10259 If the definition of the function has the same debug info,
10260 we should be able to match up with it anyway. But fallbacks
10261 using the minimal symbol, for instance to find a method
10262 implemented in a stripped copy of libstdc++, will not work.
10263 If we do not have debug info for the definition, we will have to
10264 match them up some other way.
10265
10266 When we do name matching there is a related problem with function
10267 templates; two instantiated function templates are allowed to
10268 differ only by their return types, which we do not add here. */
10269
10270 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10271 {
10272 struct attribute *attr;
10273 struct die_info *child;
10274 int first = 1;
10275
10276 die->building_fullname = 1;
10277
10278 for (child = die->child; child != NULL; child = child->sibling)
10279 {
10280 struct type *type;
10281 LONGEST value;
10282 const gdb_byte *bytes;
10283 struct dwarf2_locexpr_baton *baton;
10284 struct value *v;
10285
10286 if (child->tag != DW_TAG_template_type_param
10287 && child->tag != DW_TAG_template_value_param)
10288 continue;
10289
10290 if (first)
10291 {
10292 buf.puts ("<");
10293 first = 0;
10294 }
10295 else
10296 buf.puts (", ");
10297
10298 attr = dwarf2_attr (child, DW_AT_type, cu);
10299 if (attr == NULL)
10300 {
10301 complaint (_("template parameter missing DW_AT_type"));
10302 buf.puts ("UNKNOWN_TYPE");
10303 continue;
10304 }
10305 type = die_type (child, cu);
10306
10307 if (child->tag == DW_TAG_template_type_param)
10308 {
10309 c_print_type (type, "", &buf, -1, 0, cu->language,
10310 &type_print_raw_options);
10311 continue;
10312 }
10313
10314 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10315 if (attr == NULL)
10316 {
10317 complaint (_("template parameter missing "
10318 "DW_AT_const_value"));
10319 buf.puts ("UNKNOWN_VALUE");
10320 continue;
10321 }
10322
10323 dwarf2_const_value_attr (attr, type, name,
10324 &cu->comp_unit_obstack, cu,
10325 &value, &bytes, &baton);
10326
10327 if (TYPE_NOSIGN (type))
10328 /* GDB prints characters as NUMBER 'CHAR'. If that's
10329 changed, this can use value_print instead. */
10330 c_printchar (value, type, &buf);
10331 else
10332 {
10333 struct value_print_options opts;
10334
10335 if (baton != NULL)
10336 v = dwarf2_evaluate_loc_desc (type, NULL,
10337 baton->data,
10338 baton->size,
10339 baton->per_cu);
10340 else if (bytes != NULL)
10341 {
10342 v = allocate_value (type);
10343 memcpy (value_contents_writeable (v), bytes,
10344 TYPE_LENGTH (type));
10345 }
10346 else
10347 v = value_from_longest (type, value);
10348
10349 /* Specify decimal so that we do not depend on
10350 the radix. */
10351 get_formatted_print_options (&opts, 'd');
10352 opts.raw = 1;
10353 value_print (v, &buf, &opts);
10354 release_value (v);
10355 }
10356 }
10357
10358 die->building_fullname = 0;
10359
10360 if (!first)
10361 {
10362 /* Close the argument list, with a space if necessary
10363 (nested templates). */
10364 if (!buf.empty () && buf.string ().back () == '>')
10365 buf.puts (" >");
10366 else
10367 buf.puts (">");
10368 }
10369 }
10370
10371 /* For C++ methods, append formal parameter type
10372 information, if PHYSNAME. */
10373
10374 if (physname && die->tag == DW_TAG_subprogram
10375 && cu->language == language_cplus)
10376 {
10377 struct type *type = read_type_die (die, cu);
10378
10379 c_type_print_args (type, &buf, 1, cu->language,
10380 &type_print_raw_options);
10381
10382 if (cu->language == language_cplus)
10383 {
10384 /* Assume that an artificial first parameter is
10385 "this", but do not crash if it is not. RealView
10386 marks unnamed (and thus unused) parameters as
10387 artificial; there is no way to differentiate
10388 the two cases. */
10389 if (type->num_fields () > 0
10390 && TYPE_FIELD_ARTIFICIAL (type, 0)
10391 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
10392 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10393 0))))
10394 buf.puts (" const");
10395 }
10396 }
10397
10398 const std::string &intermediate_name = buf.string ();
10399
10400 if (cu->language == language_cplus)
10401 canonical_name
10402 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10403 objfile);
10404
10405 /* If we only computed INTERMEDIATE_NAME, or if
10406 INTERMEDIATE_NAME is already canonical, then we need to
10407 intern it. */
10408 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10409 name = objfile->intern (intermediate_name);
10410 else
10411 name = canonical_name;
10412 }
10413 }
10414
10415 return name;
10416 }
10417
10418 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10419 If scope qualifiers are appropriate they will be added. The result
10420 will be allocated on the storage_obstack, or NULL if the DIE does
10421 not have a name. NAME may either be from a previous call to
10422 dwarf2_name or NULL.
10423
10424 The output string will be canonicalized (if C++). */
10425
10426 static const char *
10427 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10428 {
10429 return dwarf2_compute_name (name, die, cu, 0);
10430 }
10431
10432 /* Construct a physname for the given DIE in CU. NAME may either be
10433 from a previous call to dwarf2_name or NULL. The result will be
10434 allocated on the objfile_objstack or NULL if the DIE does not have a
10435 name.
10436
10437 The output string will be canonicalized (if C++). */
10438
10439 static const char *
10440 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10441 {
10442 struct objfile *objfile = cu->per_objfile->objfile;
10443 const char *retval, *mangled = NULL, *canon = NULL;
10444 int need_copy = 1;
10445
10446 /* In this case dwarf2_compute_name is just a shortcut not building anything
10447 on its own. */
10448 if (!die_needs_namespace (die, cu))
10449 return dwarf2_compute_name (name, die, cu, 1);
10450
10451 if (cu->language != language_rust)
10452 mangled = dw2_linkage_name (die, cu);
10453
10454 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10455 has computed. */
10456 gdb::unique_xmalloc_ptr<char> demangled;
10457 if (mangled != NULL)
10458 {
10459
10460 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10461 {
10462 /* Do nothing (do not demangle the symbol name). */
10463 }
10464 else if (cu->language == language_go)
10465 {
10466 /* This is a lie, but we already lie to the caller new_symbol.
10467 new_symbol assumes we return the mangled name.
10468 This just undoes that lie until things are cleaned up. */
10469 }
10470 else
10471 {
10472 /* Use DMGL_RET_DROP for C++ template functions to suppress
10473 their return type. It is easier for GDB users to search
10474 for such functions as `name(params)' than `long name(params)'.
10475 In such case the minimal symbol names do not match the full
10476 symbol names but for template functions there is never a need
10477 to look up their definition from their declaration so
10478 the only disadvantage remains the minimal symbol variant
10479 `long name(params)' does not have the proper inferior type. */
10480 demangled.reset (gdb_demangle (mangled,
10481 (DMGL_PARAMS | DMGL_ANSI
10482 | DMGL_RET_DROP)));
10483 }
10484 if (demangled)
10485 canon = demangled.get ();
10486 else
10487 {
10488 canon = mangled;
10489 need_copy = 0;
10490 }
10491 }
10492
10493 if (canon == NULL || check_physname)
10494 {
10495 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10496
10497 if (canon != NULL && strcmp (physname, canon) != 0)
10498 {
10499 /* It may not mean a bug in GDB. The compiler could also
10500 compute DW_AT_linkage_name incorrectly. But in such case
10501 GDB would need to be bug-to-bug compatible. */
10502
10503 complaint (_("Computed physname <%s> does not match demangled <%s> "
10504 "(from linkage <%s>) - DIE at %s [in module %s]"),
10505 physname, canon, mangled, sect_offset_str (die->sect_off),
10506 objfile_name (objfile));
10507
10508 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10509 is available here - over computed PHYSNAME. It is safer
10510 against both buggy GDB and buggy compilers. */
10511
10512 retval = canon;
10513 }
10514 else
10515 {
10516 retval = physname;
10517 need_copy = 0;
10518 }
10519 }
10520 else
10521 retval = canon;
10522
10523 if (need_copy)
10524 retval = objfile->intern (retval);
10525
10526 return retval;
10527 }
10528
10529 /* Inspect DIE in CU for a namespace alias. If one exists, record
10530 a new symbol for it.
10531
10532 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10533
10534 static int
10535 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10536 {
10537 struct attribute *attr;
10538
10539 /* If the die does not have a name, this is not a namespace
10540 alias. */
10541 attr = dwarf2_attr (die, DW_AT_name, cu);
10542 if (attr != NULL)
10543 {
10544 int num;
10545 struct die_info *d = die;
10546 struct dwarf2_cu *imported_cu = cu;
10547
10548 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10549 keep inspecting DIEs until we hit the underlying import. */
10550 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10551 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10552 {
10553 attr = dwarf2_attr (d, DW_AT_import, cu);
10554 if (attr == NULL)
10555 break;
10556
10557 d = follow_die_ref (d, attr, &imported_cu);
10558 if (d->tag != DW_TAG_imported_declaration)
10559 break;
10560 }
10561
10562 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10563 {
10564 complaint (_("DIE at %s has too many recursively imported "
10565 "declarations"), sect_offset_str (d->sect_off));
10566 return 0;
10567 }
10568
10569 if (attr != NULL)
10570 {
10571 struct type *type;
10572 sect_offset sect_off = attr->get_ref_die_offset ();
10573
10574 type = get_die_type_at_offset (sect_off, cu->per_cu);
10575 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10576 {
10577 /* This declaration is a global namespace alias. Add
10578 a symbol for it whose type is the aliased namespace. */
10579 new_symbol (die, type, cu);
10580 return 1;
10581 }
10582 }
10583 }
10584
10585 return 0;
10586 }
10587
10588 /* Return the using directives repository (global or local?) to use in the
10589 current context for CU.
10590
10591 For Ada, imported declarations can materialize renamings, which *may* be
10592 global. However it is impossible (for now?) in DWARF to distinguish
10593 "external" imported declarations and "static" ones. As all imported
10594 declarations seem to be static in all other languages, make them all CU-wide
10595 global only in Ada. */
10596
10597 static struct using_direct **
10598 using_directives (struct dwarf2_cu *cu)
10599 {
10600 if (cu->language == language_ada
10601 && cu->get_builder ()->outermost_context_p ())
10602 return cu->get_builder ()->get_global_using_directives ();
10603 else
10604 return cu->get_builder ()->get_local_using_directives ();
10605 }
10606
10607 /* Read the import statement specified by the given die and record it. */
10608
10609 static void
10610 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10611 {
10612 struct objfile *objfile = cu->per_objfile->objfile;
10613 struct attribute *import_attr;
10614 struct die_info *imported_die, *child_die;
10615 struct dwarf2_cu *imported_cu;
10616 const char *imported_name;
10617 const char *imported_name_prefix;
10618 const char *canonical_name;
10619 const char *import_alias;
10620 const char *imported_declaration = NULL;
10621 const char *import_prefix;
10622 std::vector<const char *> excludes;
10623
10624 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10625 if (import_attr == NULL)
10626 {
10627 complaint (_("Tag '%s' has no DW_AT_import"),
10628 dwarf_tag_name (die->tag));
10629 return;
10630 }
10631
10632 imported_cu = cu;
10633 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10634 imported_name = dwarf2_name (imported_die, imported_cu);
10635 if (imported_name == NULL)
10636 {
10637 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10638
10639 The import in the following code:
10640 namespace A
10641 {
10642 typedef int B;
10643 }
10644
10645 int main ()
10646 {
10647 using A::B;
10648 B b;
10649 return b;
10650 }
10651
10652 ...
10653 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10654 <52> DW_AT_decl_file : 1
10655 <53> DW_AT_decl_line : 6
10656 <54> DW_AT_import : <0x75>
10657 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10658 <59> DW_AT_name : B
10659 <5b> DW_AT_decl_file : 1
10660 <5c> DW_AT_decl_line : 2
10661 <5d> DW_AT_type : <0x6e>
10662 ...
10663 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10664 <76> DW_AT_byte_size : 4
10665 <77> DW_AT_encoding : 5 (signed)
10666
10667 imports the wrong die ( 0x75 instead of 0x58 ).
10668 This case will be ignored until the gcc bug is fixed. */
10669 return;
10670 }
10671
10672 /* Figure out the local name after import. */
10673 import_alias = dwarf2_name (die, cu);
10674
10675 /* Figure out where the statement is being imported to. */
10676 import_prefix = determine_prefix (die, cu);
10677
10678 /* Figure out what the scope of the imported die is and prepend it
10679 to the name of the imported die. */
10680 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10681
10682 if (imported_die->tag != DW_TAG_namespace
10683 && imported_die->tag != DW_TAG_module)
10684 {
10685 imported_declaration = imported_name;
10686 canonical_name = imported_name_prefix;
10687 }
10688 else if (strlen (imported_name_prefix) > 0)
10689 canonical_name = obconcat (&objfile->objfile_obstack,
10690 imported_name_prefix,
10691 (cu->language == language_d ? "." : "::"),
10692 imported_name, (char *) NULL);
10693 else
10694 canonical_name = imported_name;
10695
10696 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10697 for (child_die = die->child; child_die && child_die->tag;
10698 child_die = child_die->sibling)
10699 {
10700 /* DWARF-4: A Fortran use statement with a “rename list” may be
10701 represented by an imported module entry with an import attribute
10702 referring to the module and owned entries corresponding to those
10703 entities that are renamed as part of being imported. */
10704
10705 if (child_die->tag != DW_TAG_imported_declaration)
10706 {
10707 complaint (_("child DW_TAG_imported_declaration expected "
10708 "- DIE at %s [in module %s]"),
10709 sect_offset_str (child_die->sect_off),
10710 objfile_name (objfile));
10711 continue;
10712 }
10713
10714 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10715 if (import_attr == NULL)
10716 {
10717 complaint (_("Tag '%s' has no DW_AT_import"),
10718 dwarf_tag_name (child_die->tag));
10719 continue;
10720 }
10721
10722 imported_cu = cu;
10723 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10724 &imported_cu);
10725 imported_name = dwarf2_name (imported_die, imported_cu);
10726 if (imported_name == NULL)
10727 {
10728 complaint (_("child DW_TAG_imported_declaration has unknown "
10729 "imported name - DIE at %s [in module %s]"),
10730 sect_offset_str (child_die->sect_off),
10731 objfile_name (objfile));
10732 continue;
10733 }
10734
10735 excludes.push_back (imported_name);
10736
10737 process_die (child_die, cu);
10738 }
10739
10740 add_using_directive (using_directives (cu),
10741 import_prefix,
10742 canonical_name,
10743 import_alias,
10744 imported_declaration,
10745 excludes,
10746 0,
10747 &objfile->objfile_obstack);
10748 }
10749
10750 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10751 types, but gives them a size of zero. Starting with version 14,
10752 ICC is compatible with GCC. */
10753
10754 static bool
10755 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10756 {
10757 if (!cu->checked_producer)
10758 check_producer (cu);
10759
10760 return cu->producer_is_icc_lt_14;
10761 }
10762
10763 /* ICC generates a DW_AT_type for C void functions. This was observed on
10764 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10765 which says that void functions should not have a DW_AT_type. */
10766
10767 static bool
10768 producer_is_icc (struct dwarf2_cu *cu)
10769 {
10770 if (!cu->checked_producer)
10771 check_producer (cu);
10772
10773 return cu->producer_is_icc;
10774 }
10775
10776 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10777 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10778 this, it was first present in GCC release 4.3.0. */
10779
10780 static bool
10781 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10782 {
10783 if (!cu->checked_producer)
10784 check_producer (cu);
10785
10786 return cu->producer_is_gcc_lt_4_3;
10787 }
10788
10789 static file_and_directory
10790 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10791 {
10792 file_and_directory res;
10793
10794 /* Find the filename. Do not use dwarf2_name here, since the filename
10795 is not a source language identifier. */
10796 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10797 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10798
10799 if (res.comp_dir == NULL
10800 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10801 && IS_ABSOLUTE_PATH (res.name))
10802 {
10803 res.comp_dir_storage = ldirname (res.name);
10804 if (!res.comp_dir_storage.empty ())
10805 res.comp_dir = res.comp_dir_storage.c_str ();
10806 }
10807 if (res.comp_dir != NULL)
10808 {
10809 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10810 directory, get rid of it. */
10811 const char *cp = strchr (res.comp_dir, ':');
10812
10813 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10814 res.comp_dir = cp + 1;
10815 }
10816
10817 if (res.name == NULL)
10818 res.name = "<unknown>";
10819
10820 return res;
10821 }
10822
10823 /* Handle DW_AT_stmt_list for a compilation unit.
10824 DIE is the DW_TAG_compile_unit die for CU.
10825 COMP_DIR is the compilation directory. LOWPC is passed to
10826 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10827
10828 static void
10829 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10830 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10831 {
10832 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10833 struct attribute *attr;
10834 struct line_header line_header_local;
10835 hashval_t line_header_local_hash;
10836 void **slot;
10837 int decode_mapping;
10838
10839 gdb_assert (! cu->per_cu->is_debug_types);
10840
10841 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10842 if (attr == NULL)
10843 return;
10844
10845 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10846
10847 /* The line header hash table is only created if needed (it exists to
10848 prevent redundant reading of the line table for partial_units).
10849 If we're given a partial_unit, we'll need it. If we're given a
10850 compile_unit, then use the line header hash table if it's already
10851 created, but don't create one just yet. */
10852
10853 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
10854 && die->tag == DW_TAG_partial_unit)
10855 {
10856 dwarf2_per_objfile->per_bfd->line_header_hash
10857 .reset (htab_create_alloc (127, line_header_hash_voidp,
10858 line_header_eq_voidp,
10859 free_line_header_voidp,
10860 xcalloc, xfree));
10861 }
10862
10863 line_header_local.sect_off = line_offset;
10864 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10865 line_header_local_hash = line_header_hash (&line_header_local);
10866 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
10867 {
10868 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10869 &line_header_local,
10870 line_header_local_hash, NO_INSERT);
10871
10872 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10873 is not present in *SLOT (since if there is something in *SLOT then
10874 it will be for a partial_unit). */
10875 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10876 {
10877 gdb_assert (*slot != NULL);
10878 cu->line_header = (struct line_header *) *slot;
10879 return;
10880 }
10881 }
10882
10883 /* dwarf_decode_line_header does not yet provide sufficient information.
10884 We always have to call also dwarf_decode_lines for it. */
10885 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10886 if (lh == NULL)
10887 return;
10888
10889 cu->line_header = lh.release ();
10890 cu->line_header_die_owner = die;
10891
10892 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
10893 slot = NULL;
10894 else
10895 {
10896 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10897 &line_header_local,
10898 line_header_local_hash, INSERT);
10899 gdb_assert (slot != NULL);
10900 }
10901 if (slot != NULL && *slot == NULL)
10902 {
10903 /* This newly decoded line number information unit will be owned
10904 by line_header_hash hash table. */
10905 *slot = cu->line_header;
10906 cu->line_header_die_owner = NULL;
10907 }
10908 else
10909 {
10910 /* We cannot free any current entry in (*slot) as that struct line_header
10911 may be already used by multiple CUs. Create only temporary decoded
10912 line_header for this CU - it may happen at most once for each line
10913 number information unit. And if we're not using line_header_hash
10914 then this is what we want as well. */
10915 gdb_assert (die->tag != DW_TAG_partial_unit);
10916 }
10917 decode_mapping = (die->tag != DW_TAG_partial_unit);
10918 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10919 decode_mapping);
10920
10921 }
10922
10923 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10924
10925 static void
10926 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10927 {
10928 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10929 struct objfile *objfile = dwarf2_per_objfile->objfile;
10930 struct gdbarch *gdbarch = objfile->arch ();
10931 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10932 CORE_ADDR highpc = ((CORE_ADDR) 0);
10933 struct attribute *attr;
10934 struct die_info *child_die;
10935 CORE_ADDR baseaddr;
10936
10937 prepare_one_comp_unit (cu, die, cu->language);
10938 baseaddr = objfile->text_section_offset ();
10939
10940 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10941
10942 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10943 from finish_block. */
10944 if (lowpc == ((CORE_ADDR) -1))
10945 lowpc = highpc;
10946 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10947
10948 file_and_directory fnd = find_file_and_directory (die, cu);
10949
10950 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10951 standardised yet. As a workaround for the language detection we fall
10952 back to the DW_AT_producer string. */
10953 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10954 cu->language = language_opencl;
10955
10956 /* Similar hack for Go. */
10957 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10958 set_cu_language (DW_LANG_Go, cu);
10959
10960 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10961
10962 /* Decode line number information if present. We do this before
10963 processing child DIEs, so that the line header table is available
10964 for DW_AT_decl_file. */
10965 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10966
10967 /* Process all dies in compilation unit. */
10968 if (die->child != NULL)
10969 {
10970 child_die = die->child;
10971 while (child_die && child_die->tag)
10972 {
10973 process_die (child_die, cu);
10974 child_die = child_die->sibling;
10975 }
10976 }
10977
10978 /* Decode macro information, if present. Dwarf 2 macro information
10979 refers to information in the line number info statement program
10980 header, so we can only read it if we've read the header
10981 successfully. */
10982 attr = dwarf2_attr (die, DW_AT_macros, cu);
10983 if (attr == NULL)
10984 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10985 if (attr && cu->line_header)
10986 {
10987 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10988 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10989
10990 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10991 }
10992 else
10993 {
10994 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10995 if (attr && cu->line_header)
10996 {
10997 unsigned int macro_offset = DW_UNSND (attr);
10998
10999 dwarf_decode_macros (cu, macro_offset, 0);
11000 }
11001 }
11002 }
11003
11004 void
11005 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11006 {
11007 struct type_unit_group *tu_group;
11008 int first_time;
11009 struct attribute *attr;
11010 unsigned int i;
11011 struct signatured_type *sig_type;
11012
11013 gdb_assert (per_cu->is_debug_types);
11014 sig_type = (struct signatured_type *) per_cu;
11015
11016 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11017
11018 /* If we're using .gdb_index (includes -readnow) then
11019 per_cu->type_unit_group may not have been set up yet. */
11020 if (sig_type->type_unit_group == NULL)
11021 sig_type->type_unit_group = get_type_unit_group (this, attr);
11022 tu_group = sig_type->type_unit_group;
11023
11024 /* If we've already processed this stmt_list there's no real need to
11025 do it again, we could fake it and just recreate the part we need
11026 (file name,index -> symtab mapping). If data shows this optimization
11027 is useful we can do it then. */
11028 first_time = tu_group->compunit_symtab == NULL;
11029
11030 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11031 debug info. */
11032 line_header_up lh;
11033 if (attr != NULL)
11034 {
11035 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11036 lh = dwarf_decode_line_header (line_offset, this);
11037 }
11038 if (lh == NULL)
11039 {
11040 if (first_time)
11041 start_symtab ("", NULL, 0);
11042 else
11043 {
11044 gdb_assert (tu_group->symtabs == NULL);
11045 gdb_assert (m_builder == nullptr);
11046 struct compunit_symtab *cust = tu_group->compunit_symtab;
11047 m_builder.reset (new struct buildsym_compunit
11048 (COMPUNIT_OBJFILE (cust), "",
11049 COMPUNIT_DIRNAME (cust),
11050 compunit_language (cust),
11051 0, cust));
11052 list_in_scope = get_builder ()->get_file_symbols ();
11053 }
11054 return;
11055 }
11056
11057 line_header = lh.release ();
11058 line_header_die_owner = die;
11059
11060 if (first_time)
11061 {
11062 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11063
11064 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11065 still initializing it, and our caller (a few levels up)
11066 process_full_type_unit still needs to know if this is the first
11067 time. */
11068
11069 tu_group->symtabs
11070 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11071 struct symtab *, line_header->file_names_size ());
11072
11073 auto &file_names = line_header->file_names ();
11074 for (i = 0; i < file_names.size (); ++i)
11075 {
11076 file_entry &fe = file_names[i];
11077 dwarf2_start_subfile (this, fe.name,
11078 fe.include_dir (line_header));
11079 buildsym_compunit *b = get_builder ();
11080 if (b->get_current_subfile ()->symtab == NULL)
11081 {
11082 /* NOTE: start_subfile will recognize when it's been
11083 passed a file it has already seen. So we can't
11084 assume there's a simple mapping from
11085 cu->line_header->file_names to subfiles, plus
11086 cu->line_header->file_names may contain dups. */
11087 b->get_current_subfile ()->symtab
11088 = allocate_symtab (cust, b->get_current_subfile ()->name);
11089 }
11090
11091 fe.symtab = b->get_current_subfile ()->symtab;
11092 tu_group->symtabs[i] = fe.symtab;
11093 }
11094 }
11095 else
11096 {
11097 gdb_assert (m_builder == nullptr);
11098 struct compunit_symtab *cust = tu_group->compunit_symtab;
11099 m_builder.reset (new struct buildsym_compunit
11100 (COMPUNIT_OBJFILE (cust), "",
11101 COMPUNIT_DIRNAME (cust),
11102 compunit_language (cust),
11103 0, cust));
11104 list_in_scope = get_builder ()->get_file_symbols ();
11105
11106 auto &file_names = line_header->file_names ();
11107 for (i = 0; i < file_names.size (); ++i)
11108 {
11109 file_entry &fe = file_names[i];
11110 fe.symtab = tu_group->symtabs[i];
11111 }
11112 }
11113
11114 /* The main symtab is allocated last. Type units don't have DW_AT_name
11115 so they don't have a "real" (so to speak) symtab anyway.
11116 There is later code that will assign the main symtab to all symbols
11117 that don't have one. We need to handle the case of a symbol with a
11118 missing symtab (DW_AT_decl_file) anyway. */
11119 }
11120
11121 /* Process DW_TAG_type_unit.
11122 For TUs we want to skip the first top level sibling if it's not the
11123 actual type being defined by this TU. In this case the first top
11124 level sibling is there to provide context only. */
11125
11126 static void
11127 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11128 {
11129 struct die_info *child_die;
11130
11131 prepare_one_comp_unit (cu, die, language_minimal);
11132
11133 /* Initialize (or reinitialize) the machinery for building symtabs.
11134 We do this before processing child DIEs, so that the line header table
11135 is available for DW_AT_decl_file. */
11136 cu->setup_type_unit_groups (die);
11137
11138 if (die->child != NULL)
11139 {
11140 child_die = die->child;
11141 while (child_die && child_die->tag)
11142 {
11143 process_die (child_die, cu);
11144 child_die = child_die->sibling;
11145 }
11146 }
11147 }
11148 \f
11149 /* DWO/DWP files.
11150
11151 http://gcc.gnu.org/wiki/DebugFission
11152 http://gcc.gnu.org/wiki/DebugFissionDWP
11153
11154 To simplify handling of both DWO files ("object" files with the DWARF info)
11155 and DWP files (a file with the DWOs packaged up into one file), we treat
11156 DWP files as having a collection of virtual DWO files. */
11157
11158 static hashval_t
11159 hash_dwo_file (const void *item)
11160 {
11161 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11162 hashval_t hash;
11163
11164 hash = htab_hash_string (dwo_file->dwo_name);
11165 if (dwo_file->comp_dir != NULL)
11166 hash += htab_hash_string (dwo_file->comp_dir);
11167 return hash;
11168 }
11169
11170 static int
11171 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11172 {
11173 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11174 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11175
11176 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11177 return 0;
11178 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11179 return lhs->comp_dir == rhs->comp_dir;
11180 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11181 }
11182
11183 /* Allocate a hash table for DWO files. */
11184
11185 static htab_up
11186 allocate_dwo_file_hash_table ()
11187 {
11188 auto delete_dwo_file = [] (void *item)
11189 {
11190 struct dwo_file *dwo_file = (struct dwo_file *) item;
11191
11192 delete dwo_file;
11193 };
11194
11195 return htab_up (htab_create_alloc (41,
11196 hash_dwo_file,
11197 eq_dwo_file,
11198 delete_dwo_file,
11199 xcalloc, xfree));
11200 }
11201
11202 /* Lookup DWO file DWO_NAME. */
11203
11204 static void **
11205 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11206 const char *dwo_name,
11207 const char *comp_dir)
11208 {
11209 struct dwo_file find_entry;
11210 void **slot;
11211
11212 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11213 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11214
11215 find_entry.dwo_name = dwo_name;
11216 find_entry.comp_dir = comp_dir;
11217 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
11218 INSERT);
11219
11220 return slot;
11221 }
11222
11223 static hashval_t
11224 hash_dwo_unit (const void *item)
11225 {
11226 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11227
11228 /* This drops the top 32 bits of the id, but is ok for a hash. */
11229 return dwo_unit->signature;
11230 }
11231
11232 static int
11233 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11234 {
11235 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11236 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11237
11238 /* The signature is assumed to be unique within the DWO file.
11239 So while object file CU dwo_id's always have the value zero,
11240 that's OK, assuming each object file DWO file has only one CU,
11241 and that's the rule for now. */
11242 return lhs->signature == rhs->signature;
11243 }
11244
11245 /* Allocate a hash table for DWO CUs,TUs.
11246 There is one of these tables for each of CUs,TUs for each DWO file. */
11247
11248 static htab_up
11249 allocate_dwo_unit_table ()
11250 {
11251 /* Start out with a pretty small number.
11252 Generally DWO files contain only one CU and maybe some TUs. */
11253 return htab_up (htab_create_alloc (3,
11254 hash_dwo_unit,
11255 eq_dwo_unit,
11256 NULL, xcalloc, xfree));
11257 }
11258
11259 /* die_reader_func for create_dwo_cu. */
11260
11261 static void
11262 create_dwo_cu_reader (const struct die_reader_specs *reader,
11263 const gdb_byte *info_ptr,
11264 struct die_info *comp_unit_die,
11265 struct dwo_file *dwo_file,
11266 struct dwo_unit *dwo_unit)
11267 {
11268 struct dwarf2_cu *cu = reader->cu;
11269 sect_offset sect_off = cu->per_cu->sect_off;
11270 struct dwarf2_section_info *section = cu->per_cu->section;
11271
11272 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11273 if (!signature.has_value ())
11274 {
11275 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11276 " its dwo_id [in module %s]"),
11277 sect_offset_str (sect_off), dwo_file->dwo_name);
11278 return;
11279 }
11280
11281 dwo_unit->dwo_file = dwo_file;
11282 dwo_unit->signature = *signature;
11283 dwo_unit->section = section;
11284 dwo_unit->sect_off = sect_off;
11285 dwo_unit->length = cu->per_cu->length;
11286
11287 if (dwarf_read_debug)
11288 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11289 sect_offset_str (sect_off),
11290 hex_string (dwo_unit->signature));
11291 }
11292
11293 /* Create the dwo_units for the CUs in a DWO_FILE.
11294 Note: This function processes DWO files only, not DWP files. */
11295
11296 static void
11297 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11298 dwarf2_cu *cu, struct dwo_file &dwo_file,
11299 dwarf2_section_info &section, htab_up &cus_htab)
11300 {
11301 struct objfile *objfile = dwarf2_per_objfile->objfile;
11302 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
11303 const gdb_byte *info_ptr, *end_ptr;
11304
11305 section.read (objfile);
11306 info_ptr = section.buffer;
11307
11308 if (info_ptr == NULL)
11309 return;
11310
11311 if (dwarf_read_debug)
11312 {
11313 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11314 section.get_name (),
11315 section.get_file_name ());
11316 }
11317
11318 end_ptr = info_ptr + section.size;
11319 while (info_ptr < end_ptr)
11320 {
11321 struct dwarf2_per_cu_data per_cu;
11322 struct dwo_unit read_unit {};
11323 struct dwo_unit *dwo_unit;
11324 void **slot;
11325 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11326
11327 memset (&per_cu, 0, sizeof (per_cu));
11328 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11329 per_cu.per_bfd = per_bfd;
11330 per_cu.is_debug_types = 0;
11331 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11332 per_cu.section = &section;
11333
11334 cutu_reader reader (&per_cu, dwarf2_per_objfile, cu, &dwo_file);
11335 if (!reader.dummy_p)
11336 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11337 &dwo_file, &read_unit);
11338 info_ptr += per_cu.length;
11339
11340 // If the unit could not be parsed, skip it.
11341 if (read_unit.dwo_file == NULL)
11342 continue;
11343
11344 if (cus_htab == NULL)
11345 cus_htab = allocate_dwo_unit_table ();
11346
11347 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11348 struct dwo_unit);
11349 *dwo_unit = read_unit;
11350 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11351 gdb_assert (slot != NULL);
11352 if (*slot != NULL)
11353 {
11354 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11355 sect_offset dup_sect_off = dup_cu->sect_off;
11356
11357 complaint (_("debug cu entry at offset %s is duplicate to"
11358 " the entry at offset %s, signature %s"),
11359 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11360 hex_string (dwo_unit->signature));
11361 }
11362 *slot = (void *)dwo_unit;
11363 }
11364 }
11365
11366 /* DWP file .debug_{cu,tu}_index section format:
11367 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11368
11369 DWP Version 1:
11370
11371 Both index sections have the same format, and serve to map a 64-bit
11372 signature to a set of section numbers. Each section begins with a header,
11373 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11374 indexes, and a pool of 32-bit section numbers. The index sections will be
11375 aligned at 8-byte boundaries in the file.
11376
11377 The index section header consists of:
11378
11379 V, 32 bit version number
11380 -, 32 bits unused
11381 N, 32 bit number of compilation units or type units in the index
11382 M, 32 bit number of slots in the hash table
11383
11384 Numbers are recorded using the byte order of the application binary.
11385
11386 The hash table begins at offset 16 in the section, and consists of an array
11387 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11388 order of the application binary). Unused slots in the hash table are 0.
11389 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11390
11391 The parallel table begins immediately after the hash table
11392 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11393 array of 32-bit indexes (using the byte order of the application binary),
11394 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11395 table contains a 32-bit index into the pool of section numbers. For unused
11396 hash table slots, the corresponding entry in the parallel table will be 0.
11397
11398 The pool of section numbers begins immediately following the hash table
11399 (at offset 16 + 12 * M from the beginning of the section). The pool of
11400 section numbers consists of an array of 32-bit words (using the byte order
11401 of the application binary). Each item in the array is indexed starting
11402 from 0. The hash table entry provides the index of the first section
11403 number in the set. Additional section numbers in the set follow, and the
11404 set is terminated by a 0 entry (section number 0 is not used in ELF).
11405
11406 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11407 section must be the first entry in the set, and the .debug_abbrev.dwo must
11408 be the second entry. Other members of the set may follow in any order.
11409
11410 ---
11411
11412 DWP Version 2:
11413
11414 DWP Version 2 combines all the .debug_info, etc. sections into one,
11415 and the entries in the index tables are now offsets into these sections.
11416 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11417 section.
11418
11419 Index Section Contents:
11420 Header
11421 Hash Table of Signatures dwp_hash_table.hash_table
11422 Parallel Table of Indices dwp_hash_table.unit_table
11423 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11424 Table of Section Sizes dwp_hash_table.v2.sizes
11425
11426 The index section header consists of:
11427
11428 V, 32 bit version number
11429 L, 32 bit number of columns in the table of section offsets
11430 N, 32 bit number of compilation units or type units in the index
11431 M, 32 bit number of slots in the hash table
11432
11433 Numbers are recorded using the byte order of the application binary.
11434
11435 The hash table has the same format as version 1.
11436 The parallel table of indices has the same format as version 1,
11437 except that the entries are origin-1 indices into the table of sections
11438 offsets and the table of section sizes.
11439
11440 The table of offsets begins immediately following the parallel table
11441 (at offset 16 + 12 * M from the beginning of the section). The table is
11442 a two-dimensional array of 32-bit words (using the byte order of the
11443 application binary), with L columns and N+1 rows, in row-major order.
11444 Each row in the array is indexed starting from 0. The first row provides
11445 a key to the remaining rows: each column in this row provides an identifier
11446 for a debug section, and the offsets in the same column of subsequent rows
11447 refer to that section. The section identifiers are:
11448
11449 DW_SECT_INFO 1 .debug_info.dwo
11450 DW_SECT_TYPES 2 .debug_types.dwo
11451 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11452 DW_SECT_LINE 4 .debug_line.dwo
11453 DW_SECT_LOC 5 .debug_loc.dwo
11454 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11455 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11456 DW_SECT_MACRO 8 .debug_macro.dwo
11457
11458 The offsets provided by the CU and TU index sections are the base offsets
11459 for the contributions made by each CU or TU to the corresponding section
11460 in the package file. Each CU and TU header contains an abbrev_offset
11461 field, used to find the abbreviations table for that CU or TU within the
11462 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11463 be interpreted as relative to the base offset given in the index section.
11464 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11465 should be interpreted as relative to the base offset for .debug_line.dwo,
11466 and offsets into other debug sections obtained from DWARF attributes should
11467 also be interpreted as relative to the corresponding base offset.
11468
11469 The table of sizes begins immediately following the table of offsets.
11470 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11471 with L columns and N rows, in row-major order. Each row in the array is
11472 indexed starting from 1 (row 0 is shared by the two tables).
11473
11474 ---
11475
11476 Hash table lookup is handled the same in version 1 and 2:
11477
11478 We assume that N and M will not exceed 2^32 - 1.
11479 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11480
11481 Given a 64-bit compilation unit signature or a type signature S, an entry
11482 in the hash table is located as follows:
11483
11484 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11485 the low-order k bits all set to 1.
11486
11487 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11488
11489 3) If the hash table entry at index H matches the signature, use that
11490 entry. If the hash table entry at index H is unused (all zeroes),
11491 terminate the search: the signature is not present in the table.
11492
11493 4) Let H = (H + H') modulo M. Repeat at Step 3.
11494
11495 Because M > N and H' and M are relatively prime, the search is guaranteed
11496 to stop at an unused slot or find the match. */
11497
11498 /* Create a hash table to map DWO IDs to their CU/TU entry in
11499 .debug_{info,types}.dwo in DWP_FILE.
11500 Returns NULL if there isn't one.
11501 Note: This function processes DWP files only, not DWO files. */
11502
11503 static struct dwp_hash_table *
11504 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11505 struct dwp_file *dwp_file, int is_debug_types)
11506 {
11507 struct objfile *objfile = dwarf2_per_objfile->objfile;
11508 bfd *dbfd = dwp_file->dbfd.get ();
11509 const gdb_byte *index_ptr, *index_end;
11510 struct dwarf2_section_info *index;
11511 uint32_t version, nr_columns, nr_units, nr_slots;
11512 struct dwp_hash_table *htab;
11513
11514 if (is_debug_types)
11515 index = &dwp_file->sections.tu_index;
11516 else
11517 index = &dwp_file->sections.cu_index;
11518
11519 if (index->empty ())
11520 return NULL;
11521 index->read (objfile);
11522
11523 index_ptr = index->buffer;
11524 index_end = index_ptr + index->size;
11525
11526 version = read_4_bytes (dbfd, index_ptr);
11527 index_ptr += 4;
11528 if (version == 2)
11529 nr_columns = read_4_bytes (dbfd, index_ptr);
11530 else
11531 nr_columns = 0;
11532 index_ptr += 4;
11533 nr_units = read_4_bytes (dbfd, index_ptr);
11534 index_ptr += 4;
11535 nr_slots = read_4_bytes (dbfd, index_ptr);
11536 index_ptr += 4;
11537
11538 if (version != 1 && version != 2)
11539 {
11540 error (_("Dwarf Error: unsupported DWP file version (%s)"
11541 " [in module %s]"),
11542 pulongest (version), dwp_file->name);
11543 }
11544 if (nr_slots != (nr_slots & -nr_slots))
11545 {
11546 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11547 " is not power of 2 [in module %s]"),
11548 pulongest (nr_slots), dwp_file->name);
11549 }
11550
11551 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
11552 htab->version = version;
11553 htab->nr_columns = nr_columns;
11554 htab->nr_units = nr_units;
11555 htab->nr_slots = nr_slots;
11556 htab->hash_table = index_ptr;
11557 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11558
11559 /* Exit early if the table is empty. */
11560 if (nr_slots == 0 || nr_units == 0
11561 || (version == 2 && nr_columns == 0))
11562 {
11563 /* All must be zero. */
11564 if (nr_slots != 0 || nr_units != 0
11565 || (version == 2 && nr_columns != 0))
11566 {
11567 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11568 " all zero [in modules %s]"),
11569 dwp_file->name);
11570 }
11571 return htab;
11572 }
11573
11574 if (version == 1)
11575 {
11576 htab->section_pool.v1.indices =
11577 htab->unit_table + sizeof (uint32_t) * nr_slots;
11578 /* It's harder to decide whether the section is too small in v1.
11579 V1 is deprecated anyway so we punt. */
11580 }
11581 else
11582 {
11583 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11584 int *ids = htab->section_pool.v2.section_ids;
11585 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11586 /* Reverse map for error checking. */
11587 int ids_seen[DW_SECT_MAX + 1];
11588 int i;
11589
11590 if (nr_columns < 2)
11591 {
11592 error (_("Dwarf Error: bad DWP hash table, too few columns"
11593 " in section table [in module %s]"),
11594 dwp_file->name);
11595 }
11596 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11597 {
11598 error (_("Dwarf Error: bad DWP hash table, too many columns"
11599 " in section table [in module %s]"),
11600 dwp_file->name);
11601 }
11602 memset (ids, 255, sizeof_ids);
11603 memset (ids_seen, 255, sizeof (ids_seen));
11604 for (i = 0; i < nr_columns; ++i)
11605 {
11606 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11607
11608 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11609 {
11610 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11611 " in section table [in module %s]"),
11612 id, dwp_file->name);
11613 }
11614 if (ids_seen[id] != -1)
11615 {
11616 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11617 " id %d in section table [in module %s]"),
11618 id, dwp_file->name);
11619 }
11620 ids_seen[id] = i;
11621 ids[i] = id;
11622 }
11623 /* Must have exactly one info or types section. */
11624 if (((ids_seen[DW_SECT_INFO] != -1)
11625 + (ids_seen[DW_SECT_TYPES] != -1))
11626 != 1)
11627 {
11628 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11629 " DWO info/types section [in module %s]"),
11630 dwp_file->name);
11631 }
11632 /* Must have an abbrev section. */
11633 if (ids_seen[DW_SECT_ABBREV] == -1)
11634 {
11635 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11636 " section [in module %s]"),
11637 dwp_file->name);
11638 }
11639 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11640 htab->section_pool.v2.sizes =
11641 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11642 * nr_units * nr_columns);
11643 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11644 * nr_units * nr_columns))
11645 > index_end)
11646 {
11647 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11648 " [in module %s]"),
11649 dwp_file->name);
11650 }
11651 }
11652
11653 return htab;
11654 }
11655
11656 /* Update SECTIONS with the data from SECTP.
11657
11658 This function is like the other "locate" section routines that are
11659 passed to bfd_map_over_sections, but in this context the sections to
11660 read comes from the DWP V1 hash table, not the full ELF section table.
11661
11662 The result is non-zero for success, or zero if an error was found. */
11663
11664 static int
11665 locate_v1_virtual_dwo_sections (asection *sectp,
11666 struct virtual_v1_dwo_sections *sections)
11667 {
11668 const struct dwop_section_names *names = &dwop_section_names;
11669
11670 if (section_is_p (sectp->name, &names->abbrev_dwo))
11671 {
11672 /* There can be only one. */
11673 if (sections->abbrev.s.section != NULL)
11674 return 0;
11675 sections->abbrev.s.section = sectp;
11676 sections->abbrev.size = bfd_section_size (sectp);
11677 }
11678 else if (section_is_p (sectp->name, &names->info_dwo)
11679 || section_is_p (sectp->name, &names->types_dwo))
11680 {
11681 /* There can be only one. */
11682 if (sections->info_or_types.s.section != NULL)
11683 return 0;
11684 sections->info_or_types.s.section = sectp;
11685 sections->info_or_types.size = bfd_section_size (sectp);
11686 }
11687 else if (section_is_p (sectp->name, &names->line_dwo))
11688 {
11689 /* There can be only one. */
11690 if (sections->line.s.section != NULL)
11691 return 0;
11692 sections->line.s.section = sectp;
11693 sections->line.size = bfd_section_size (sectp);
11694 }
11695 else if (section_is_p (sectp->name, &names->loc_dwo))
11696 {
11697 /* There can be only one. */
11698 if (sections->loc.s.section != NULL)
11699 return 0;
11700 sections->loc.s.section = sectp;
11701 sections->loc.size = bfd_section_size (sectp);
11702 }
11703 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11704 {
11705 /* There can be only one. */
11706 if (sections->macinfo.s.section != NULL)
11707 return 0;
11708 sections->macinfo.s.section = sectp;
11709 sections->macinfo.size = bfd_section_size (sectp);
11710 }
11711 else if (section_is_p (sectp->name, &names->macro_dwo))
11712 {
11713 /* There can be only one. */
11714 if (sections->macro.s.section != NULL)
11715 return 0;
11716 sections->macro.s.section = sectp;
11717 sections->macro.size = bfd_section_size (sectp);
11718 }
11719 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11720 {
11721 /* There can be only one. */
11722 if (sections->str_offsets.s.section != NULL)
11723 return 0;
11724 sections->str_offsets.s.section = sectp;
11725 sections->str_offsets.size = bfd_section_size (sectp);
11726 }
11727 else
11728 {
11729 /* No other kind of section is valid. */
11730 return 0;
11731 }
11732
11733 return 1;
11734 }
11735
11736 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11737 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11738 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11739 This is for DWP version 1 files. */
11740
11741 static struct dwo_unit *
11742 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11743 struct dwp_file *dwp_file,
11744 uint32_t unit_index,
11745 const char *comp_dir,
11746 ULONGEST signature, int is_debug_types)
11747 {
11748 const struct dwp_hash_table *dwp_htab =
11749 is_debug_types ? dwp_file->tus : dwp_file->cus;
11750 bfd *dbfd = dwp_file->dbfd.get ();
11751 const char *kind = is_debug_types ? "TU" : "CU";
11752 struct dwo_file *dwo_file;
11753 struct dwo_unit *dwo_unit;
11754 struct virtual_v1_dwo_sections sections;
11755 void **dwo_file_slot;
11756 int i;
11757
11758 gdb_assert (dwp_file->version == 1);
11759
11760 if (dwarf_read_debug)
11761 {
11762 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11763 kind,
11764 pulongest (unit_index), hex_string (signature),
11765 dwp_file->name);
11766 }
11767
11768 /* Fetch the sections of this DWO unit.
11769 Put a limit on the number of sections we look for so that bad data
11770 doesn't cause us to loop forever. */
11771
11772 #define MAX_NR_V1_DWO_SECTIONS \
11773 (1 /* .debug_info or .debug_types */ \
11774 + 1 /* .debug_abbrev */ \
11775 + 1 /* .debug_line */ \
11776 + 1 /* .debug_loc */ \
11777 + 1 /* .debug_str_offsets */ \
11778 + 1 /* .debug_macro or .debug_macinfo */ \
11779 + 1 /* trailing zero */)
11780
11781 memset (&sections, 0, sizeof (sections));
11782
11783 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11784 {
11785 asection *sectp;
11786 uint32_t section_nr =
11787 read_4_bytes (dbfd,
11788 dwp_htab->section_pool.v1.indices
11789 + (unit_index + i) * sizeof (uint32_t));
11790
11791 if (section_nr == 0)
11792 break;
11793 if (section_nr >= dwp_file->num_sections)
11794 {
11795 error (_("Dwarf Error: bad DWP hash table, section number too large"
11796 " [in module %s]"),
11797 dwp_file->name);
11798 }
11799
11800 sectp = dwp_file->elf_sections[section_nr];
11801 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11802 {
11803 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11804 " [in module %s]"),
11805 dwp_file->name);
11806 }
11807 }
11808
11809 if (i < 2
11810 || sections.info_or_types.empty ()
11811 || sections.abbrev.empty ())
11812 {
11813 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11814 " [in module %s]"),
11815 dwp_file->name);
11816 }
11817 if (i == MAX_NR_V1_DWO_SECTIONS)
11818 {
11819 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11820 " [in module %s]"),
11821 dwp_file->name);
11822 }
11823
11824 /* It's easier for the rest of the code if we fake a struct dwo_file and
11825 have dwo_unit "live" in that. At least for now.
11826
11827 The DWP file can be made up of a random collection of CUs and TUs.
11828 However, for each CU + set of TUs that came from the same original DWO
11829 file, we can combine them back into a virtual DWO file to save space
11830 (fewer struct dwo_file objects to allocate). Remember that for really
11831 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11832
11833 std::string virtual_dwo_name =
11834 string_printf ("virtual-dwo/%d-%d-%d-%d",
11835 sections.abbrev.get_id (),
11836 sections.line.get_id (),
11837 sections.loc.get_id (),
11838 sections.str_offsets.get_id ());
11839 /* Can we use an existing virtual DWO file? */
11840 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11841 virtual_dwo_name.c_str (),
11842 comp_dir);
11843 /* Create one if necessary. */
11844 if (*dwo_file_slot == NULL)
11845 {
11846 if (dwarf_read_debug)
11847 {
11848 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11849 virtual_dwo_name.c_str ());
11850 }
11851 dwo_file = new struct dwo_file;
11852 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
11853 dwo_file->comp_dir = comp_dir;
11854 dwo_file->sections.abbrev = sections.abbrev;
11855 dwo_file->sections.line = sections.line;
11856 dwo_file->sections.loc = sections.loc;
11857 dwo_file->sections.macinfo = sections.macinfo;
11858 dwo_file->sections.macro = sections.macro;
11859 dwo_file->sections.str_offsets = sections.str_offsets;
11860 /* The "str" section is global to the entire DWP file. */
11861 dwo_file->sections.str = dwp_file->sections.str;
11862 /* The info or types section is assigned below to dwo_unit,
11863 there's no need to record it in dwo_file.
11864 Also, we can't simply record type sections in dwo_file because
11865 we record a pointer into the vector in dwo_unit. As we collect more
11866 types we'll grow the vector and eventually have to reallocate space
11867 for it, invalidating all copies of pointers into the previous
11868 contents. */
11869 *dwo_file_slot = dwo_file;
11870 }
11871 else
11872 {
11873 if (dwarf_read_debug)
11874 {
11875 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11876 virtual_dwo_name.c_str ());
11877 }
11878 dwo_file = (struct dwo_file *) *dwo_file_slot;
11879 }
11880
11881 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
11882 dwo_unit->dwo_file = dwo_file;
11883 dwo_unit->signature = signature;
11884 dwo_unit->section =
11885 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11886 *dwo_unit->section = sections.info_or_types;
11887 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11888
11889 return dwo_unit;
11890 }
11891
11892 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11893 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11894 piece within that section used by a TU/CU, return a virtual section
11895 of just that piece. */
11896
11897 static struct dwarf2_section_info
11898 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11899 struct dwarf2_section_info *section,
11900 bfd_size_type offset, bfd_size_type size)
11901 {
11902 struct dwarf2_section_info result;
11903 asection *sectp;
11904
11905 gdb_assert (section != NULL);
11906 gdb_assert (!section->is_virtual);
11907
11908 memset (&result, 0, sizeof (result));
11909 result.s.containing_section = section;
11910 result.is_virtual = true;
11911
11912 if (size == 0)
11913 return result;
11914
11915 sectp = section->get_bfd_section ();
11916
11917 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11918 bounds of the real section. This is a pretty-rare event, so just
11919 flag an error (easier) instead of a warning and trying to cope. */
11920 if (sectp == NULL
11921 || offset + size > bfd_section_size (sectp))
11922 {
11923 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11924 " in section %s [in module %s]"),
11925 sectp ? bfd_section_name (sectp) : "<unknown>",
11926 objfile_name (dwarf2_per_objfile->objfile));
11927 }
11928
11929 result.virtual_offset = offset;
11930 result.size = size;
11931 return result;
11932 }
11933
11934 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11935 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11936 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11937 This is for DWP version 2 files. */
11938
11939 static struct dwo_unit *
11940 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11941 struct dwp_file *dwp_file,
11942 uint32_t unit_index,
11943 const char *comp_dir,
11944 ULONGEST signature, int is_debug_types)
11945 {
11946 const struct dwp_hash_table *dwp_htab =
11947 is_debug_types ? dwp_file->tus : dwp_file->cus;
11948 bfd *dbfd = dwp_file->dbfd.get ();
11949 const char *kind = is_debug_types ? "TU" : "CU";
11950 struct dwo_file *dwo_file;
11951 struct dwo_unit *dwo_unit;
11952 struct virtual_v2_dwo_sections sections;
11953 void **dwo_file_slot;
11954 int i;
11955
11956 gdb_assert (dwp_file->version == 2);
11957
11958 if (dwarf_read_debug)
11959 {
11960 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11961 kind,
11962 pulongest (unit_index), hex_string (signature),
11963 dwp_file->name);
11964 }
11965
11966 /* Fetch the section offsets of this DWO unit. */
11967
11968 memset (&sections, 0, sizeof (sections));
11969
11970 for (i = 0; i < dwp_htab->nr_columns; ++i)
11971 {
11972 uint32_t offset = read_4_bytes (dbfd,
11973 dwp_htab->section_pool.v2.offsets
11974 + (((unit_index - 1) * dwp_htab->nr_columns
11975 + i)
11976 * sizeof (uint32_t)));
11977 uint32_t size = read_4_bytes (dbfd,
11978 dwp_htab->section_pool.v2.sizes
11979 + (((unit_index - 1) * dwp_htab->nr_columns
11980 + i)
11981 * sizeof (uint32_t)));
11982
11983 switch (dwp_htab->section_pool.v2.section_ids[i])
11984 {
11985 case DW_SECT_INFO:
11986 case DW_SECT_TYPES:
11987 sections.info_or_types_offset = offset;
11988 sections.info_or_types_size = size;
11989 break;
11990 case DW_SECT_ABBREV:
11991 sections.abbrev_offset = offset;
11992 sections.abbrev_size = size;
11993 break;
11994 case DW_SECT_LINE:
11995 sections.line_offset = offset;
11996 sections.line_size = size;
11997 break;
11998 case DW_SECT_LOC:
11999 sections.loc_offset = offset;
12000 sections.loc_size = size;
12001 break;
12002 case DW_SECT_STR_OFFSETS:
12003 sections.str_offsets_offset = offset;
12004 sections.str_offsets_size = size;
12005 break;
12006 case DW_SECT_MACINFO:
12007 sections.macinfo_offset = offset;
12008 sections.macinfo_size = size;
12009 break;
12010 case DW_SECT_MACRO:
12011 sections.macro_offset = offset;
12012 sections.macro_size = size;
12013 break;
12014 }
12015 }
12016
12017 /* It's easier for the rest of the code if we fake a struct dwo_file and
12018 have dwo_unit "live" in that. At least for now.
12019
12020 The DWP file can be made up of a random collection of CUs and TUs.
12021 However, for each CU + set of TUs that came from the same original DWO
12022 file, we can combine them back into a virtual DWO file to save space
12023 (fewer struct dwo_file objects to allocate). Remember that for really
12024 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12025
12026 std::string virtual_dwo_name =
12027 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12028 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12029 (long) (sections.line_size ? sections.line_offset : 0),
12030 (long) (sections.loc_size ? sections.loc_offset : 0),
12031 (long) (sections.str_offsets_size
12032 ? sections.str_offsets_offset : 0));
12033 /* Can we use an existing virtual DWO file? */
12034 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12035 virtual_dwo_name.c_str (),
12036 comp_dir);
12037 /* Create one if necessary. */
12038 if (*dwo_file_slot == NULL)
12039 {
12040 if (dwarf_read_debug)
12041 {
12042 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12043 virtual_dwo_name.c_str ());
12044 }
12045 dwo_file = new struct dwo_file;
12046 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
12047 dwo_file->comp_dir = comp_dir;
12048 dwo_file->sections.abbrev =
12049 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12050 sections.abbrev_offset, sections.abbrev_size);
12051 dwo_file->sections.line =
12052 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12053 sections.line_offset, sections.line_size);
12054 dwo_file->sections.loc =
12055 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12056 sections.loc_offset, sections.loc_size);
12057 dwo_file->sections.macinfo =
12058 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12059 sections.macinfo_offset, sections.macinfo_size);
12060 dwo_file->sections.macro =
12061 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12062 sections.macro_offset, sections.macro_size);
12063 dwo_file->sections.str_offsets =
12064 create_dwp_v2_section (dwarf2_per_objfile,
12065 &dwp_file->sections.str_offsets,
12066 sections.str_offsets_offset,
12067 sections.str_offsets_size);
12068 /* The "str" section is global to the entire DWP file. */
12069 dwo_file->sections.str = dwp_file->sections.str;
12070 /* The info or types section is assigned below to dwo_unit,
12071 there's no need to record it in dwo_file.
12072 Also, we can't simply record type sections in dwo_file because
12073 we record a pointer into the vector in dwo_unit. As we collect more
12074 types we'll grow the vector and eventually have to reallocate space
12075 for it, invalidating all copies of pointers into the previous
12076 contents. */
12077 *dwo_file_slot = dwo_file;
12078 }
12079 else
12080 {
12081 if (dwarf_read_debug)
12082 {
12083 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12084 virtual_dwo_name.c_str ());
12085 }
12086 dwo_file = (struct dwo_file *) *dwo_file_slot;
12087 }
12088
12089 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
12090 dwo_unit->dwo_file = dwo_file;
12091 dwo_unit->signature = signature;
12092 dwo_unit->section =
12093 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12094 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12095 is_debug_types
12096 ? &dwp_file->sections.types
12097 : &dwp_file->sections.info,
12098 sections.info_or_types_offset,
12099 sections.info_or_types_size);
12100 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12101
12102 return dwo_unit;
12103 }
12104
12105 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12106 Returns NULL if the signature isn't found. */
12107
12108 static struct dwo_unit *
12109 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12110 struct dwp_file *dwp_file, const char *comp_dir,
12111 ULONGEST signature, int is_debug_types)
12112 {
12113 const struct dwp_hash_table *dwp_htab =
12114 is_debug_types ? dwp_file->tus : dwp_file->cus;
12115 bfd *dbfd = dwp_file->dbfd.get ();
12116 uint32_t mask = dwp_htab->nr_slots - 1;
12117 uint32_t hash = signature & mask;
12118 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12119 unsigned int i;
12120 void **slot;
12121 struct dwo_unit find_dwo_cu;
12122
12123 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12124 find_dwo_cu.signature = signature;
12125 slot = htab_find_slot (is_debug_types
12126 ? dwp_file->loaded_tus.get ()
12127 : dwp_file->loaded_cus.get (),
12128 &find_dwo_cu, INSERT);
12129
12130 if (*slot != NULL)
12131 return (struct dwo_unit *) *slot;
12132
12133 /* Use a for loop so that we don't loop forever on bad debug info. */
12134 for (i = 0; i < dwp_htab->nr_slots; ++i)
12135 {
12136 ULONGEST signature_in_table;
12137
12138 signature_in_table =
12139 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12140 if (signature_in_table == signature)
12141 {
12142 uint32_t unit_index =
12143 read_4_bytes (dbfd,
12144 dwp_htab->unit_table + hash * sizeof (uint32_t));
12145
12146 if (dwp_file->version == 1)
12147 {
12148 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12149 dwp_file, unit_index,
12150 comp_dir, signature,
12151 is_debug_types);
12152 }
12153 else
12154 {
12155 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12156 dwp_file, unit_index,
12157 comp_dir, signature,
12158 is_debug_types);
12159 }
12160 return (struct dwo_unit *) *slot;
12161 }
12162 if (signature_in_table == 0)
12163 return NULL;
12164 hash = (hash + hash2) & mask;
12165 }
12166
12167 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12168 " [in module %s]"),
12169 dwp_file->name);
12170 }
12171
12172 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12173 Open the file specified by FILE_NAME and hand it off to BFD for
12174 preliminary analysis. Return a newly initialized bfd *, which
12175 includes a canonicalized copy of FILE_NAME.
12176 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12177 SEARCH_CWD is true if the current directory is to be searched.
12178 It will be searched before debug-file-directory.
12179 If successful, the file is added to the bfd include table of the
12180 objfile's bfd (see gdb_bfd_record_inclusion).
12181 If unable to find/open the file, return NULL.
12182 NOTE: This function is derived from symfile_bfd_open. */
12183
12184 static gdb_bfd_ref_ptr
12185 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12186 const char *file_name, int is_dwp, int search_cwd)
12187 {
12188 int desc;
12189 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12190 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12191 to debug_file_directory. */
12192 const char *search_path;
12193 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12194
12195 gdb::unique_xmalloc_ptr<char> search_path_holder;
12196 if (search_cwd)
12197 {
12198 if (*debug_file_directory != '\0')
12199 {
12200 search_path_holder.reset (concat (".", dirname_separator_string,
12201 debug_file_directory,
12202 (char *) NULL));
12203 search_path = search_path_holder.get ();
12204 }
12205 else
12206 search_path = ".";
12207 }
12208 else
12209 search_path = debug_file_directory;
12210
12211 openp_flags flags = OPF_RETURN_REALPATH;
12212 if (is_dwp)
12213 flags |= OPF_SEARCH_IN_PATH;
12214
12215 gdb::unique_xmalloc_ptr<char> absolute_name;
12216 desc = openp (search_path, flags, file_name,
12217 O_RDONLY | O_BINARY, &absolute_name);
12218 if (desc < 0)
12219 return NULL;
12220
12221 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12222 gnutarget, desc));
12223 if (sym_bfd == NULL)
12224 return NULL;
12225 bfd_set_cacheable (sym_bfd.get (), 1);
12226
12227 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12228 return NULL;
12229
12230 /* Success. Record the bfd as having been included by the objfile's bfd.
12231 This is important because things like demangled_names_hash lives in the
12232 objfile's per_bfd space and may have references to things like symbol
12233 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12234 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12235
12236 return sym_bfd;
12237 }
12238
12239 /* Try to open DWO file FILE_NAME.
12240 COMP_DIR is the DW_AT_comp_dir attribute.
12241 The result is the bfd handle of the file.
12242 If there is a problem finding or opening the file, return NULL.
12243 Upon success, the canonicalized path of the file is stored in the bfd,
12244 same as symfile_bfd_open. */
12245
12246 static gdb_bfd_ref_ptr
12247 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12248 const char *file_name, const char *comp_dir)
12249 {
12250 if (IS_ABSOLUTE_PATH (file_name))
12251 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12252 0 /*is_dwp*/, 0 /*search_cwd*/);
12253
12254 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12255
12256 if (comp_dir != NULL)
12257 {
12258 gdb::unique_xmalloc_ptr<char> path_to_try
12259 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12260
12261 /* NOTE: If comp_dir is a relative path, this will also try the
12262 search path, which seems useful. */
12263 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12264 path_to_try.get (),
12265 0 /*is_dwp*/,
12266 1 /*search_cwd*/));
12267 if (abfd != NULL)
12268 return abfd;
12269 }
12270
12271 /* That didn't work, try debug-file-directory, which, despite its name,
12272 is a list of paths. */
12273
12274 if (*debug_file_directory == '\0')
12275 return NULL;
12276
12277 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12278 0 /*is_dwp*/, 1 /*search_cwd*/);
12279 }
12280
12281 /* This function is mapped across the sections and remembers the offset and
12282 size of each of the DWO debugging sections we are interested in. */
12283
12284 static void
12285 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12286 {
12287 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12288 const struct dwop_section_names *names = &dwop_section_names;
12289
12290 if (section_is_p (sectp->name, &names->abbrev_dwo))
12291 {
12292 dwo_sections->abbrev.s.section = sectp;
12293 dwo_sections->abbrev.size = bfd_section_size (sectp);
12294 }
12295 else if (section_is_p (sectp->name, &names->info_dwo))
12296 {
12297 dwo_sections->info.s.section = sectp;
12298 dwo_sections->info.size = bfd_section_size (sectp);
12299 }
12300 else if (section_is_p (sectp->name, &names->line_dwo))
12301 {
12302 dwo_sections->line.s.section = sectp;
12303 dwo_sections->line.size = bfd_section_size (sectp);
12304 }
12305 else if (section_is_p (sectp->name, &names->loc_dwo))
12306 {
12307 dwo_sections->loc.s.section = sectp;
12308 dwo_sections->loc.size = bfd_section_size (sectp);
12309 }
12310 else if (section_is_p (sectp->name, &names->loclists_dwo))
12311 {
12312 dwo_sections->loclists.s.section = sectp;
12313 dwo_sections->loclists.size = bfd_section_size (sectp);
12314 }
12315 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12316 {
12317 dwo_sections->macinfo.s.section = sectp;
12318 dwo_sections->macinfo.size = bfd_section_size (sectp);
12319 }
12320 else if (section_is_p (sectp->name, &names->macro_dwo))
12321 {
12322 dwo_sections->macro.s.section = sectp;
12323 dwo_sections->macro.size = bfd_section_size (sectp);
12324 }
12325 else if (section_is_p (sectp->name, &names->str_dwo))
12326 {
12327 dwo_sections->str.s.section = sectp;
12328 dwo_sections->str.size = bfd_section_size (sectp);
12329 }
12330 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12331 {
12332 dwo_sections->str_offsets.s.section = sectp;
12333 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12334 }
12335 else if (section_is_p (sectp->name, &names->types_dwo))
12336 {
12337 struct dwarf2_section_info type_section;
12338
12339 memset (&type_section, 0, sizeof (type_section));
12340 type_section.s.section = sectp;
12341 type_section.size = bfd_section_size (sectp);
12342 dwo_sections->types.push_back (type_section);
12343 }
12344 }
12345
12346 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12347 by PER_CU. This is for the non-DWP case.
12348 The result is NULL if DWO_NAME can't be found. */
12349
12350 static struct dwo_file *
12351 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12352 const char *dwo_name, const char *comp_dir)
12353 {
12354 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12355
12356 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12357 if (dbfd == NULL)
12358 {
12359 if (dwarf_read_debug)
12360 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12361 return NULL;
12362 }
12363
12364 dwo_file_up dwo_file (new struct dwo_file);
12365 dwo_file->dwo_name = dwo_name;
12366 dwo_file->comp_dir = comp_dir;
12367 dwo_file->dbfd = std::move (dbfd);
12368
12369 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12370 &dwo_file->sections);
12371
12372 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12373 dwo_file->sections.info, dwo_file->cus);
12374
12375 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12376 dwo_file->sections.types, dwo_file->tus);
12377
12378 if (dwarf_read_debug)
12379 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12380
12381 return dwo_file.release ();
12382 }
12383
12384 /* This function is mapped across the sections and remembers the offset and
12385 size of each of the DWP debugging sections common to version 1 and 2 that
12386 we are interested in. */
12387
12388 static void
12389 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12390 void *dwp_file_ptr)
12391 {
12392 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12393 const struct dwop_section_names *names = &dwop_section_names;
12394 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12395
12396 /* Record the ELF section number for later lookup: this is what the
12397 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12398 gdb_assert (elf_section_nr < dwp_file->num_sections);
12399 dwp_file->elf_sections[elf_section_nr] = sectp;
12400
12401 /* Look for specific sections that we need. */
12402 if (section_is_p (sectp->name, &names->str_dwo))
12403 {
12404 dwp_file->sections.str.s.section = sectp;
12405 dwp_file->sections.str.size = bfd_section_size (sectp);
12406 }
12407 else if (section_is_p (sectp->name, &names->cu_index))
12408 {
12409 dwp_file->sections.cu_index.s.section = sectp;
12410 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12411 }
12412 else if (section_is_p (sectp->name, &names->tu_index))
12413 {
12414 dwp_file->sections.tu_index.s.section = sectp;
12415 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12416 }
12417 }
12418
12419 /* This function is mapped across the sections and remembers the offset and
12420 size of each of the DWP version 2 debugging sections that we are interested
12421 in. This is split into a separate function because we don't know if we
12422 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12423
12424 static void
12425 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12426 {
12427 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12428 const struct dwop_section_names *names = &dwop_section_names;
12429 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12430
12431 /* Record the ELF section number for later lookup: this is what the
12432 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12433 gdb_assert (elf_section_nr < dwp_file->num_sections);
12434 dwp_file->elf_sections[elf_section_nr] = sectp;
12435
12436 /* Look for specific sections that we need. */
12437 if (section_is_p (sectp->name, &names->abbrev_dwo))
12438 {
12439 dwp_file->sections.abbrev.s.section = sectp;
12440 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12441 }
12442 else if (section_is_p (sectp->name, &names->info_dwo))
12443 {
12444 dwp_file->sections.info.s.section = sectp;
12445 dwp_file->sections.info.size = bfd_section_size (sectp);
12446 }
12447 else if (section_is_p (sectp->name, &names->line_dwo))
12448 {
12449 dwp_file->sections.line.s.section = sectp;
12450 dwp_file->sections.line.size = bfd_section_size (sectp);
12451 }
12452 else if (section_is_p (sectp->name, &names->loc_dwo))
12453 {
12454 dwp_file->sections.loc.s.section = sectp;
12455 dwp_file->sections.loc.size = bfd_section_size (sectp);
12456 }
12457 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12458 {
12459 dwp_file->sections.macinfo.s.section = sectp;
12460 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12461 }
12462 else if (section_is_p (sectp->name, &names->macro_dwo))
12463 {
12464 dwp_file->sections.macro.s.section = sectp;
12465 dwp_file->sections.macro.size = bfd_section_size (sectp);
12466 }
12467 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12468 {
12469 dwp_file->sections.str_offsets.s.section = sectp;
12470 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12471 }
12472 else if (section_is_p (sectp->name, &names->types_dwo))
12473 {
12474 dwp_file->sections.types.s.section = sectp;
12475 dwp_file->sections.types.size = bfd_section_size (sectp);
12476 }
12477 }
12478
12479 /* Hash function for dwp_file loaded CUs/TUs. */
12480
12481 static hashval_t
12482 hash_dwp_loaded_cutus (const void *item)
12483 {
12484 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12485
12486 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12487 return dwo_unit->signature;
12488 }
12489
12490 /* Equality function for dwp_file loaded CUs/TUs. */
12491
12492 static int
12493 eq_dwp_loaded_cutus (const void *a, const void *b)
12494 {
12495 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12496 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12497
12498 return dua->signature == dub->signature;
12499 }
12500
12501 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12502
12503 static htab_up
12504 allocate_dwp_loaded_cutus_table ()
12505 {
12506 return htab_up (htab_create_alloc (3,
12507 hash_dwp_loaded_cutus,
12508 eq_dwp_loaded_cutus,
12509 NULL, xcalloc, xfree));
12510 }
12511
12512 /* Try to open DWP file FILE_NAME.
12513 The result is the bfd handle of the file.
12514 If there is a problem finding or opening the file, return NULL.
12515 Upon success, the canonicalized path of the file is stored in the bfd,
12516 same as symfile_bfd_open. */
12517
12518 static gdb_bfd_ref_ptr
12519 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12520 const char *file_name)
12521 {
12522 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12523 1 /*is_dwp*/,
12524 1 /*search_cwd*/));
12525 if (abfd != NULL)
12526 return abfd;
12527
12528 /* Work around upstream bug 15652.
12529 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12530 [Whether that's a "bug" is debatable, but it is getting in our way.]
12531 We have no real idea where the dwp file is, because gdb's realpath-ing
12532 of the executable's path may have discarded the needed info.
12533 [IWBN if the dwp file name was recorded in the executable, akin to
12534 .gnu_debuglink, but that doesn't exist yet.]
12535 Strip the directory from FILE_NAME and search again. */
12536 if (*debug_file_directory != '\0')
12537 {
12538 /* Don't implicitly search the current directory here.
12539 If the user wants to search "." to handle this case,
12540 it must be added to debug-file-directory. */
12541 return try_open_dwop_file (dwarf2_per_objfile,
12542 lbasename (file_name), 1 /*is_dwp*/,
12543 0 /*search_cwd*/);
12544 }
12545
12546 return NULL;
12547 }
12548
12549 /* Initialize the use of the DWP file for the current objfile.
12550 By convention the name of the DWP file is ${objfile}.dwp.
12551 The result is NULL if it can't be found. */
12552
12553 static std::unique_ptr<struct dwp_file>
12554 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12555 {
12556 struct objfile *objfile = dwarf2_per_objfile->objfile;
12557
12558 /* Try to find first .dwp for the binary file before any symbolic links
12559 resolving. */
12560
12561 /* If the objfile is a debug file, find the name of the real binary
12562 file and get the name of dwp file from there. */
12563 std::string dwp_name;
12564 if (objfile->separate_debug_objfile_backlink != NULL)
12565 {
12566 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12567 const char *backlink_basename = lbasename (backlink->original_name);
12568
12569 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12570 }
12571 else
12572 dwp_name = objfile->original_name;
12573
12574 dwp_name += ".dwp";
12575
12576 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12577 if (dbfd == NULL
12578 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12579 {
12580 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12581 dwp_name = objfile_name (objfile);
12582 dwp_name += ".dwp";
12583 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12584 }
12585
12586 if (dbfd == NULL)
12587 {
12588 if (dwarf_read_debug)
12589 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12590 return std::unique_ptr<dwp_file> ();
12591 }
12592
12593 const char *name = bfd_get_filename (dbfd.get ());
12594 std::unique_ptr<struct dwp_file> dwp_file
12595 (new struct dwp_file (name, std::move (dbfd)));
12596
12597 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12598 dwp_file->elf_sections =
12599 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
12600 dwp_file->num_sections, asection *);
12601
12602 bfd_map_over_sections (dwp_file->dbfd.get (),
12603 dwarf2_locate_common_dwp_sections,
12604 dwp_file.get ());
12605
12606 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12607 0);
12608
12609 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12610 1);
12611
12612 /* The DWP file version is stored in the hash table. Oh well. */
12613 if (dwp_file->cus && dwp_file->tus
12614 && dwp_file->cus->version != dwp_file->tus->version)
12615 {
12616 /* Technically speaking, we should try to limp along, but this is
12617 pretty bizarre. We use pulongest here because that's the established
12618 portability solution (e.g, we cannot use %u for uint32_t). */
12619 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12620 " TU version %s [in DWP file %s]"),
12621 pulongest (dwp_file->cus->version),
12622 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12623 }
12624
12625 if (dwp_file->cus)
12626 dwp_file->version = dwp_file->cus->version;
12627 else if (dwp_file->tus)
12628 dwp_file->version = dwp_file->tus->version;
12629 else
12630 dwp_file->version = 2;
12631
12632 if (dwp_file->version == 2)
12633 bfd_map_over_sections (dwp_file->dbfd.get (),
12634 dwarf2_locate_v2_dwp_sections,
12635 dwp_file.get ());
12636
12637 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12638 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12639
12640 if (dwarf_read_debug)
12641 {
12642 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12643 fprintf_unfiltered (gdb_stdlog,
12644 " %s CUs, %s TUs\n",
12645 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12646 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12647 }
12648
12649 return dwp_file;
12650 }
12651
12652 /* Wrapper around open_and_init_dwp_file, only open it once. */
12653
12654 static struct dwp_file *
12655 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12656 {
12657 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
12658 {
12659 dwarf2_per_objfile->per_bfd->dwp_file
12660 = open_and_init_dwp_file (dwarf2_per_objfile);
12661 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
12662 }
12663 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
12664 }
12665
12666 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12667 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12668 or in the DWP file for the objfile, referenced by THIS_UNIT.
12669 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12670 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12671
12672 This is called, for example, when wanting to read a variable with a
12673 complex location. Therefore we don't want to do file i/o for every call.
12674 Therefore we don't want to look for a DWO file on every call.
12675 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12676 then we check if we've already seen DWO_NAME, and only THEN do we check
12677 for a DWO file.
12678
12679 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12680 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12681
12682 static struct dwo_unit *
12683 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12684 const char *dwo_name, const char *comp_dir,
12685 ULONGEST signature, int is_debug_types)
12686 {
12687 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12688 struct objfile *objfile = dwarf2_per_objfile->objfile;
12689 const char *kind = is_debug_types ? "TU" : "CU";
12690 void **dwo_file_slot;
12691 struct dwo_file *dwo_file;
12692 struct dwp_file *dwp_file;
12693
12694 /* First see if there's a DWP file.
12695 If we have a DWP file but didn't find the DWO inside it, don't
12696 look for the original DWO file. It makes gdb behave differently
12697 depending on whether one is debugging in the build tree. */
12698
12699 dwp_file = get_dwp_file (dwarf2_per_objfile);
12700 if (dwp_file != NULL)
12701 {
12702 const struct dwp_hash_table *dwp_htab =
12703 is_debug_types ? dwp_file->tus : dwp_file->cus;
12704
12705 if (dwp_htab != NULL)
12706 {
12707 struct dwo_unit *dwo_cutu =
12708 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12709 signature, is_debug_types);
12710
12711 if (dwo_cutu != NULL)
12712 {
12713 if (dwarf_read_debug)
12714 {
12715 fprintf_unfiltered (gdb_stdlog,
12716 "Virtual DWO %s %s found: @%s\n",
12717 kind, hex_string (signature),
12718 host_address_to_string (dwo_cutu));
12719 }
12720 return dwo_cutu;
12721 }
12722 }
12723 }
12724 else
12725 {
12726 /* No DWP file, look for the DWO file. */
12727
12728 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12729 dwo_name, comp_dir);
12730 if (*dwo_file_slot == NULL)
12731 {
12732 /* Read in the file and build a table of the CUs/TUs it contains. */
12733 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12734 }
12735 /* NOTE: This will be NULL if unable to open the file. */
12736 dwo_file = (struct dwo_file *) *dwo_file_slot;
12737
12738 if (dwo_file != NULL)
12739 {
12740 struct dwo_unit *dwo_cutu = NULL;
12741
12742 if (is_debug_types && dwo_file->tus)
12743 {
12744 struct dwo_unit find_dwo_cutu;
12745
12746 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12747 find_dwo_cutu.signature = signature;
12748 dwo_cutu
12749 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12750 &find_dwo_cutu);
12751 }
12752 else if (!is_debug_types && dwo_file->cus)
12753 {
12754 struct dwo_unit find_dwo_cutu;
12755
12756 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12757 find_dwo_cutu.signature = signature;
12758 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12759 &find_dwo_cutu);
12760 }
12761
12762 if (dwo_cutu != NULL)
12763 {
12764 if (dwarf_read_debug)
12765 {
12766 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12767 kind, dwo_name, hex_string (signature),
12768 host_address_to_string (dwo_cutu));
12769 }
12770 return dwo_cutu;
12771 }
12772 }
12773 }
12774
12775 /* We didn't find it. This could mean a dwo_id mismatch, or
12776 someone deleted the DWO/DWP file, or the search path isn't set up
12777 correctly to find the file. */
12778
12779 if (dwarf_read_debug)
12780 {
12781 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12782 kind, dwo_name, hex_string (signature));
12783 }
12784
12785 /* This is a warning and not a complaint because it can be caused by
12786 pilot error (e.g., user accidentally deleting the DWO). */
12787 {
12788 /* Print the name of the DWP file if we looked there, helps the user
12789 better diagnose the problem. */
12790 std::string dwp_text;
12791
12792 if (dwp_file != NULL)
12793 dwp_text = string_printf (" [in DWP file %s]",
12794 lbasename (dwp_file->name));
12795
12796 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12797 " [in module %s]"),
12798 kind, dwo_name, hex_string (signature),
12799 dwp_text.c_str (),
12800 this_unit->is_debug_types ? "TU" : "CU",
12801 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12802 }
12803 return NULL;
12804 }
12805
12806 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12807 See lookup_dwo_cutu_unit for details. */
12808
12809 static struct dwo_unit *
12810 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12811 const char *dwo_name, const char *comp_dir,
12812 ULONGEST signature)
12813 {
12814 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12815 }
12816
12817 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12818 See lookup_dwo_cutu_unit for details. */
12819
12820 static struct dwo_unit *
12821 lookup_dwo_type_unit (struct signatured_type *this_tu,
12822 const char *dwo_name, const char *comp_dir)
12823 {
12824 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12825 }
12826
12827 /* Traversal function for queue_and_load_all_dwo_tus. */
12828
12829 static int
12830 queue_and_load_dwo_tu (void **slot, void *info)
12831 {
12832 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12833 dwarf2_cu *cu = (dwarf2_cu *) info;
12834 ULONGEST signature = dwo_unit->signature;
12835 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12836
12837 if (sig_type != NULL)
12838 {
12839 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12840
12841 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12842 a real dependency of PER_CU on SIG_TYPE. That is detected later
12843 while processing PER_CU. */
12844 if (maybe_queue_comp_unit (NULL, sig_cu, cu->language))
12845 load_full_type_unit (sig_cu, cu->per_objfile);
12846 cu->per_cu->imported_symtabs_push (sig_cu);
12847 }
12848
12849 return 1;
12850 }
12851
12852 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12853 The DWO may have the only definition of the type, though it may not be
12854 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12855 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12856
12857 static void
12858 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12859 {
12860 struct dwo_unit *dwo_unit;
12861 struct dwo_file *dwo_file;
12862
12863 gdb_assert (!per_cu->is_debug_types);
12864 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12865 gdb_assert (per_cu->cu != NULL);
12866
12867 dwo_unit = per_cu->cu->dwo_unit;
12868 gdb_assert (dwo_unit != NULL);
12869
12870 dwo_file = dwo_unit->dwo_file;
12871 if (dwo_file->tus != NULL)
12872 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12873 per_cu->cu);
12874 }
12875
12876 /* Read in various DIEs. */
12877
12878 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12879 Inherit only the children of the DW_AT_abstract_origin DIE not being
12880 already referenced by DW_AT_abstract_origin from the children of the
12881 current DIE. */
12882
12883 static void
12884 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12885 {
12886 struct die_info *child_die;
12887 sect_offset *offsetp;
12888 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12889 struct die_info *origin_die;
12890 /* Iterator of the ORIGIN_DIE children. */
12891 struct die_info *origin_child_die;
12892 struct attribute *attr;
12893 struct dwarf2_cu *origin_cu;
12894 struct pending **origin_previous_list_in_scope;
12895
12896 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12897 if (!attr)
12898 return;
12899
12900 /* Note that following die references may follow to a die in a
12901 different cu. */
12902
12903 origin_cu = cu;
12904 origin_die = follow_die_ref (die, attr, &origin_cu);
12905
12906 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12907 symbols in. */
12908 origin_previous_list_in_scope = origin_cu->list_in_scope;
12909 origin_cu->list_in_scope = cu->list_in_scope;
12910
12911 if (die->tag != origin_die->tag
12912 && !(die->tag == DW_TAG_inlined_subroutine
12913 && origin_die->tag == DW_TAG_subprogram))
12914 complaint (_("DIE %s and its abstract origin %s have different tags"),
12915 sect_offset_str (die->sect_off),
12916 sect_offset_str (origin_die->sect_off));
12917
12918 std::vector<sect_offset> offsets;
12919
12920 for (child_die = die->child;
12921 child_die && child_die->tag;
12922 child_die = child_die->sibling)
12923 {
12924 struct die_info *child_origin_die;
12925 struct dwarf2_cu *child_origin_cu;
12926
12927 /* We are trying to process concrete instance entries:
12928 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12929 it's not relevant to our analysis here. i.e. detecting DIEs that are
12930 present in the abstract instance but not referenced in the concrete
12931 one. */
12932 if (child_die->tag == DW_TAG_call_site
12933 || child_die->tag == DW_TAG_GNU_call_site)
12934 continue;
12935
12936 /* For each CHILD_DIE, find the corresponding child of
12937 ORIGIN_DIE. If there is more than one layer of
12938 DW_AT_abstract_origin, follow them all; there shouldn't be,
12939 but GCC versions at least through 4.4 generate this (GCC PR
12940 40573). */
12941 child_origin_die = child_die;
12942 child_origin_cu = cu;
12943 while (1)
12944 {
12945 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12946 child_origin_cu);
12947 if (attr == NULL)
12948 break;
12949 child_origin_die = follow_die_ref (child_origin_die, attr,
12950 &child_origin_cu);
12951 }
12952
12953 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12954 counterpart may exist. */
12955 if (child_origin_die != child_die)
12956 {
12957 if (child_die->tag != child_origin_die->tag
12958 && !(child_die->tag == DW_TAG_inlined_subroutine
12959 && child_origin_die->tag == DW_TAG_subprogram))
12960 complaint (_("Child DIE %s and its abstract origin %s have "
12961 "different tags"),
12962 sect_offset_str (child_die->sect_off),
12963 sect_offset_str (child_origin_die->sect_off));
12964 if (child_origin_die->parent != origin_die)
12965 complaint (_("Child DIE %s and its abstract origin %s have "
12966 "different parents"),
12967 sect_offset_str (child_die->sect_off),
12968 sect_offset_str (child_origin_die->sect_off));
12969 else
12970 offsets.push_back (child_origin_die->sect_off);
12971 }
12972 }
12973 std::sort (offsets.begin (), offsets.end ());
12974 sect_offset *offsets_end = offsets.data () + offsets.size ();
12975 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12976 if (offsetp[-1] == *offsetp)
12977 complaint (_("Multiple children of DIE %s refer "
12978 "to DIE %s as their abstract origin"),
12979 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12980
12981 offsetp = offsets.data ();
12982 origin_child_die = origin_die->child;
12983 while (origin_child_die && origin_child_die->tag)
12984 {
12985 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12986 while (offsetp < offsets_end
12987 && *offsetp < origin_child_die->sect_off)
12988 offsetp++;
12989 if (offsetp >= offsets_end
12990 || *offsetp > origin_child_die->sect_off)
12991 {
12992 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12993 Check whether we're already processing ORIGIN_CHILD_DIE.
12994 This can happen with mutually referenced abstract_origins.
12995 PR 16581. */
12996 if (!origin_child_die->in_process)
12997 process_die (origin_child_die, origin_cu);
12998 }
12999 origin_child_die = origin_child_die->sibling;
13000 }
13001 origin_cu->list_in_scope = origin_previous_list_in_scope;
13002
13003 if (cu != origin_cu)
13004 compute_delayed_physnames (origin_cu);
13005 }
13006
13007 static void
13008 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13009 {
13010 struct objfile *objfile = cu->per_objfile->objfile;
13011 struct gdbarch *gdbarch = objfile->arch ();
13012 struct context_stack *newobj;
13013 CORE_ADDR lowpc;
13014 CORE_ADDR highpc;
13015 struct die_info *child_die;
13016 struct attribute *attr, *call_line, *call_file;
13017 const char *name;
13018 CORE_ADDR baseaddr;
13019 struct block *block;
13020 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13021 std::vector<struct symbol *> template_args;
13022 struct template_symbol *templ_func = NULL;
13023
13024 if (inlined_func)
13025 {
13026 /* If we do not have call site information, we can't show the
13027 caller of this inlined function. That's too confusing, so
13028 only use the scope for local variables. */
13029 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13030 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13031 if (call_line == NULL || call_file == NULL)
13032 {
13033 read_lexical_block_scope (die, cu);
13034 return;
13035 }
13036 }
13037
13038 baseaddr = objfile->text_section_offset ();
13039
13040 name = dwarf2_name (die, cu);
13041
13042 /* Ignore functions with missing or empty names. These are actually
13043 illegal according to the DWARF standard. */
13044 if (name == NULL)
13045 {
13046 complaint (_("missing name for subprogram DIE at %s"),
13047 sect_offset_str (die->sect_off));
13048 return;
13049 }
13050
13051 /* Ignore functions with missing or invalid low and high pc attributes. */
13052 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13053 <= PC_BOUNDS_INVALID)
13054 {
13055 attr = dwarf2_attr (die, DW_AT_external, cu);
13056 if (!attr || !DW_UNSND (attr))
13057 complaint (_("cannot get low and high bounds "
13058 "for subprogram DIE at %s"),
13059 sect_offset_str (die->sect_off));
13060 return;
13061 }
13062
13063 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13064 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13065
13066 /* If we have any template arguments, then we must allocate a
13067 different sort of symbol. */
13068 for (child_die = die->child; child_die; child_die = child_die->sibling)
13069 {
13070 if (child_die->tag == DW_TAG_template_type_param
13071 || child_die->tag == DW_TAG_template_value_param)
13072 {
13073 templ_func = new (&objfile->objfile_obstack) template_symbol;
13074 templ_func->subclass = SYMBOL_TEMPLATE;
13075 break;
13076 }
13077 }
13078
13079 newobj = cu->get_builder ()->push_context (0, lowpc);
13080 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13081 (struct symbol *) templ_func);
13082
13083 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13084 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13085 cu->language);
13086
13087 /* If there is a location expression for DW_AT_frame_base, record
13088 it. */
13089 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13090 if (attr != nullptr)
13091 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13092
13093 /* If there is a location for the static link, record it. */
13094 newobj->static_link = NULL;
13095 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13096 if (attr != nullptr)
13097 {
13098 newobj->static_link
13099 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13100 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13101 cu->per_cu->addr_type ());
13102 }
13103
13104 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13105
13106 if (die->child != NULL)
13107 {
13108 child_die = die->child;
13109 while (child_die && child_die->tag)
13110 {
13111 if (child_die->tag == DW_TAG_template_type_param
13112 || child_die->tag == DW_TAG_template_value_param)
13113 {
13114 struct symbol *arg = new_symbol (child_die, NULL, cu);
13115
13116 if (arg != NULL)
13117 template_args.push_back (arg);
13118 }
13119 else
13120 process_die (child_die, cu);
13121 child_die = child_die->sibling;
13122 }
13123 }
13124
13125 inherit_abstract_dies (die, cu);
13126
13127 /* If we have a DW_AT_specification, we might need to import using
13128 directives from the context of the specification DIE. See the
13129 comment in determine_prefix. */
13130 if (cu->language == language_cplus
13131 && dwarf2_attr (die, DW_AT_specification, cu))
13132 {
13133 struct dwarf2_cu *spec_cu = cu;
13134 struct die_info *spec_die = die_specification (die, &spec_cu);
13135
13136 while (spec_die)
13137 {
13138 child_die = spec_die->child;
13139 while (child_die && child_die->tag)
13140 {
13141 if (child_die->tag == DW_TAG_imported_module)
13142 process_die (child_die, spec_cu);
13143 child_die = child_die->sibling;
13144 }
13145
13146 /* In some cases, GCC generates specification DIEs that
13147 themselves contain DW_AT_specification attributes. */
13148 spec_die = die_specification (spec_die, &spec_cu);
13149 }
13150 }
13151
13152 struct context_stack cstk = cu->get_builder ()->pop_context ();
13153 /* Make a block for the local symbols within. */
13154 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13155 cstk.static_link, lowpc, highpc);
13156
13157 /* For C++, set the block's scope. */
13158 if ((cu->language == language_cplus
13159 || cu->language == language_fortran
13160 || cu->language == language_d
13161 || cu->language == language_rust)
13162 && cu->processing_has_namespace_info)
13163 block_set_scope (block, determine_prefix (die, cu),
13164 &objfile->objfile_obstack);
13165
13166 /* If we have address ranges, record them. */
13167 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13168
13169 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13170
13171 /* Attach template arguments to function. */
13172 if (!template_args.empty ())
13173 {
13174 gdb_assert (templ_func != NULL);
13175
13176 templ_func->n_template_arguments = template_args.size ();
13177 templ_func->template_arguments
13178 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13179 templ_func->n_template_arguments);
13180 memcpy (templ_func->template_arguments,
13181 template_args.data (),
13182 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13183
13184 /* Make sure that the symtab is set on the new symbols. Even
13185 though they don't appear in this symtab directly, other parts
13186 of gdb assume that symbols do, and this is reasonably
13187 true. */
13188 for (symbol *sym : template_args)
13189 symbol_set_symtab (sym, symbol_symtab (templ_func));
13190 }
13191
13192 /* In C++, we can have functions nested inside functions (e.g., when
13193 a function declares a class that has methods). This means that
13194 when we finish processing a function scope, we may need to go
13195 back to building a containing block's symbol lists. */
13196 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13197 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13198
13199 /* If we've finished processing a top-level function, subsequent
13200 symbols go in the file symbol list. */
13201 if (cu->get_builder ()->outermost_context_p ())
13202 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13203 }
13204
13205 /* Process all the DIES contained within a lexical block scope. Start
13206 a new scope, process the dies, and then close the scope. */
13207
13208 static void
13209 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13210 {
13211 struct objfile *objfile = cu->per_objfile->objfile;
13212 struct gdbarch *gdbarch = objfile->arch ();
13213 CORE_ADDR lowpc, highpc;
13214 struct die_info *child_die;
13215 CORE_ADDR baseaddr;
13216
13217 baseaddr = objfile->text_section_offset ();
13218
13219 /* Ignore blocks with missing or invalid low and high pc attributes. */
13220 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13221 as multiple lexical blocks? Handling children in a sane way would
13222 be nasty. Might be easier to properly extend generic blocks to
13223 describe ranges. */
13224 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13225 {
13226 case PC_BOUNDS_NOT_PRESENT:
13227 /* DW_TAG_lexical_block has no attributes, process its children as if
13228 there was no wrapping by that DW_TAG_lexical_block.
13229 GCC does no longer produces such DWARF since GCC r224161. */
13230 for (child_die = die->child;
13231 child_die != NULL && child_die->tag;
13232 child_die = child_die->sibling)
13233 {
13234 /* We might already be processing this DIE. This can happen
13235 in an unusual circumstance -- where a subroutine A
13236 appears lexically in another subroutine B, but A actually
13237 inlines B. The recursion is broken here, rather than in
13238 inherit_abstract_dies, because it seems better to simply
13239 drop concrete children here. */
13240 if (!child_die->in_process)
13241 process_die (child_die, cu);
13242 }
13243 return;
13244 case PC_BOUNDS_INVALID:
13245 return;
13246 }
13247 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13248 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13249
13250 cu->get_builder ()->push_context (0, lowpc);
13251 if (die->child != NULL)
13252 {
13253 child_die = die->child;
13254 while (child_die && child_die->tag)
13255 {
13256 process_die (child_die, cu);
13257 child_die = child_die->sibling;
13258 }
13259 }
13260 inherit_abstract_dies (die, cu);
13261 struct context_stack cstk = cu->get_builder ()->pop_context ();
13262
13263 if (*cu->get_builder ()->get_local_symbols () != NULL
13264 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13265 {
13266 struct block *block
13267 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13268 cstk.start_addr, highpc);
13269
13270 /* Note that recording ranges after traversing children, as we
13271 do here, means that recording a parent's ranges entails
13272 walking across all its children's ranges as they appear in
13273 the address map, which is quadratic behavior.
13274
13275 It would be nicer to record the parent's ranges before
13276 traversing its children, simply overriding whatever you find
13277 there. But since we don't even decide whether to create a
13278 block until after we've traversed its children, that's hard
13279 to do. */
13280 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13281 }
13282 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13283 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13284 }
13285
13286 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13287
13288 static void
13289 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13290 {
13291 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13292 struct objfile *objfile = per_objfile->objfile;
13293 struct gdbarch *gdbarch = objfile->arch ();
13294 CORE_ADDR pc, baseaddr;
13295 struct attribute *attr;
13296 struct call_site *call_site, call_site_local;
13297 void **slot;
13298 int nparams;
13299 struct die_info *child_die;
13300
13301 baseaddr = objfile->text_section_offset ();
13302
13303 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13304 if (attr == NULL)
13305 {
13306 /* This was a pre-DWARF-5 GNU extension alias
13307 for DW_AT_call_return_pc. */
13308 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13309 }
13310 if (!attr)
13311 {
13312 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13313 "DIE %s [in module %s]"),
13314 sect_offset_str (die->sect_off), objfile_name (objfile));
13315 return;
13316 }
13317 pc = attr->value_as_address () + baseaddr;
13318 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13319
13320 if (cu->call_site_htab == NULL)
13321 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13322 NULL, &objfile->objfile_obstack,
13323 hashtab_obstack_allocate, NULL);
13324 call_site_local.pc = pc;
13325 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13326 if (*slot != NULL)
13327 {
13328 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13329 "DIE %s [in module %s]"),
13330 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13331 objfile_name (objfile));
13332 return;
13333 }
13334
13335 /* Count parameters at the caller. */
13336
13337 nparams = 0;
13338 for (child_die = die->child; child_die && child_die->tag;
13339 child_die = child_die->sibling)
13340 {
13341 if (child_die->tag != DW_TAG_call_site_parameter
13342 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13343 {
13344 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13345 "DW_TAG_call_site child DIE %s [in module %s]"),
13346 child_die->tag, sect_offset_str (child_die->sect_off),
13347 objfile_name (objfile));
13348 continue;
13349 }
13350
13351 nparams++;
13352 }
13353
13354 call_site
13355 = ((struct call_site *)
13356 obstack_alloc (&objfile->objfile_obstack,
13357 sizeof (*call_site)
13358 + (sizeof (*call_site->parameter) * (nparams - 1))));
13359 *slot = call_site;
13360 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13361 call_site->pc = pc;
13362
13363 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13364 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13365 {
13366 struct die_info *func_die;
13367
13368 /* Skip also over DW_TAG_inlined_subroutine. */
13369 for (func_die = die->parent;
13370 func_die && func_die->tag != DW_TAG_subprogram
13371 && func_die->tag != DW_TAG_subroutine_type;
13372 func_die = func_die->parent);
13373
13374 /* DW_AT_call_all_calls is a superset
13375 of DW_AT_call_all_tail_calls. */
13376 if (func_die
13377 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13378 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13379 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13380 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13381 {
13382 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13383 not complete. But keep CALL_SITE for look ups via call_site_htab,
13384 both the initial caller containing the real return address PC and
13385 the final callee containing the current PC of a chain of tail
13386 calls do not need to have the tail call list complete. But any
13387 function candidate for a virtual tail call frame searched via
13388 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13389 determined unambiguously. */
13390 }
13391 else
13392 {
13393 struct type *func_type = NULL;
13394
13395 if (func_die)
13396 func_type = get_die_type (func_die, cu);
13397 if (func_type != NULL)
13398 {
13399 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13400
13401 /* Enlist this call site to the function. */
13402 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13403 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13404 }
13405 else
13406 complaint (_("Cannot find function owning DW_TAG_call_site "
13407 "DIE %s [in module %s]"),
13408 sect_offset_str (die->sect_off), objfile_name (objfile));
13409 }
13410 }
13411
13412 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13413 if (attr == NULL)
13414 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13415 if (attr == NULL)
13416 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13417 if (attr == NULL)
13418 {
13419 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13420 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13421 }
13422 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13423 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13424 /* Keep NULL DWARF_BLOCK. */;
13425 else if (attr->form_is_block ())
13426 {
13427 struct dwarf2_locexpr_baton *dlbaton;
13428
13429 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13430 dlbaton->data = DW_BLOCK (attr)->data;
13431 dlbaton->size = DW_BLOCK (attr)->size;
13432 dlbaton->per_objfile = per_objfile;
13433 dlbaton->per_cu = cu->per_cu;
13434
13435 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13436 }
13437 else if (attr->form_is_ref ())
13438 {
13439 struct dwarf2_cu *target_cu = cu;
13440 struct die_info *target_die;
13441
13442 target_die = follow_die_ref (die, attr, &target_cu);
13443 gdb_assert (target_cu->per_objfile->objfile == objfile);
13444 if (die_is_declaration (target_die, target_cu))
13445 {
13446 const char *target_physname;
13447
13448 /* Prefer the mangled name; otherwise compute the demangled one. */
13449 target_physname = dw2_linkage_name (target_die, target_cu);
13450 if (target_physname == NULL)
13451 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13452 if (target_physname == NULL)
13453 complaint (_("DW_AT_call_target target DIE has invalid "
13454 "physname, for referencing DIE %s [in module %s]"),
13455 sect_offset_str (die->sect_off), objfile_name (objfile));
13456 else
13457 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13458 }
13459 else
13460 {
13461 CORE_ADDR lowpc;
13462
13463 /* DW_AT_entry_pc should be preferred. */
13464 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13465 <= PC_BOUNDS_INVALID)
13466 complaint (_("DW_AT_call_target target DIE has invalid "
13467 "low pc, for referencing DIE %s [in module %s]"),
13468 sect_offset_str (die->sect_off), objfile_name (objfile));
13469 else
13470 {
13471 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13472 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13473 }
13474 }
13475 }
13476 else
13477 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13478 "block nor reference, for DIE %s [in module %s]"),
13479 sect_offset_str (die->sect_off), objfile_name (objfile));
13480
13481 call_site->per_cu = cu->per_cu;
13482
13483 for (child_die = die->child;
13484 child_die && child_die->tag;
13485 child_die = child_die->sibling)
13486 {
13487 struct call_site_parameter *parameter;
13488 struct attribute *loc, *origin;
13489
13490 if (child_die->tag != DW_TAG_call_site_parameter
13491 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13492 {
13493 /* Already printed the complaint above. */
13494 continue;
13495 }
13496
13497 gdb_assert (call_site->parameter_count < nparams);
13498 parameter = &call_site->parameter[call_site->parameter_count];
13499
13500 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13501 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13502 register is contained in DW_AT_call_value. */
13503
13504 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13505 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13506 if (origin == NULL)
13507 {
13508 /* This was a pre-DWARF-5 GNU extension alias
13509 for DW_AT_call_parameter. */
13510 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13511 }
13512 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13513 {
13514 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13515
13516 sect_offset sect_off = origin->get_ref_die_offset ();
13517 if (!cu->header.offset_in_cu_p (sect_off))
13518 {
13519 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13520 binding can be done only inside one CU. Such referenced DIE
13521 therefore cannot be even moved to DW_TAG_partial_unit. */
13522 complaint (_("DW_AT_call_parameter offset is not in CU for "
13523 "DW_TAG_call_site child DIE %s [in module %s]"),
13524 sect_offset_str (child_die->sect_off),
13525 objfile_name (objfile));
13526 continue;
13527 }
13528 parameter->u.param_cu_off
13529 = (cu_offset) (sect_off - cu->header.sect_off);
13530 }
13531 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13532 {
13533 complaint (_("No DW_FORM_block* DW_AT_location for "
13534 "DW_TAG_call_site child DIE %s [in module %s]"),
13535 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13536 continue;
13537 }
13538 else
13539 {
13540 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13541 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13542 if (parameter->u.dwarf_reg != -1)
13543 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13544 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13545 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13546 &parameter->u.fb_offset))
13547 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13548 else
13549 {
13550 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13551 "for DW_FORM_block* DW_AT_location is supported for "
13552 "DW_TAG_call_site child DIE %s "
13553 "[in module %s]"),
13554 sect_offset_str (child_die->sect_off),
13555 objfile_name (objfile));
13556 continue;
13557 }
13558 }
13559
13560 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13561 if (attr == NULL)
13562 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13563 if (attr == NULL || !attr->form_is_block ())
13564 {
13565 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13566 "DW_TAG_call_site child DIE %s [in module %s]"),
13567 sect_offset_str (child_die->sect_off),
13568 objfile_name (objfile));
13569 continue;
13570 }
13571 parameter->value = DW_BLOCK (attr)->data;
13572 parameter->value_size = DW_BLOCK (attr)->size;
13573
13574 /* Parameters are not pre-cleared by memset above. */
13575 parameter->data_value = NULL;
13576 parameter->data_value_size = 0;
13577 call_site->parameter_count++;
13578
13579 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13580 if (attr == NULL)
13581 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13582 if (attr != nullptr)
13583 {
13584 if (!attr->form_is_block ())
13585 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13586 "DW_TAG_call_site child DIE %s [in module %s]"),
13587 sect_offset_str (child_die->sect_off),
13588 objfile_name (objfile));
13589 else
13590 {
13591 parameter->data_value = DW_BLOCK (attr)->data;
13592 parameter->data_value_size = DW_BLOCK (attr)->size;
13593 }
13594 }
13595 }
13596 }
13597
13598 /* Helper function for read_variable. If DIE represents a virtual
13599 table, then return the type of the concrete object that is
13600 associated with the virtual table. Otherwise, return NULL. */
13601
13602 static struct type *
13603 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13604 {
13605 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13606 if (attr == NULL)
13607 return NULL;
13608
13609 /* Find the type DIE. */
13610 struct die_info *type_die = NULL;
13611 struct dwarf2_cu *type_cu = cu;
13612
13613 if (attr->form_is_ref ())
13614 type_die = follow_die_ref (die, attr, &type_cu);
13615 if (type_die == NULL)
13616 return NULL;
13617
13618 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13619 return NULL;
13620 return die_containing_type (type_die, type_cu);
13621 }
13622
13623 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13624
13625 static void
13626 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13627 {
13628 struct rust_vtable_symbol *storage = NULL;
13629
13630 if (cu->language == language_rust)
13631 {
13632 struct type *containing_type = rust_containing_type (die, cu);
13633
13634 if (containing_type != NULL)
13635 {
13636 struct objfile *objfile = cu->per_objfile->objfile;
13637
13638 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13639 storage->concrete_type = containing_type;
13640 storage->subclass = SYMBOL_RUST_VTABLE;
13641 }
13642 }
13643
13644 struct symbol *res = new_symbol (die, NULL, cu, storage);
13645 struct attribute *abstract_origin
13646 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13647 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13648 if (res == NULL && loc && abstract_origin)
13649 {
13650 /* We have a variable without a name, but with a location and an abstract
13651 origin. This may be a concrete instance of an abstract variable
13652 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13653 later. */
13654 struct dwarf2_cu *origin_cu = cu;
13655 struct die_info *origin_die
13656 = follow_die_ref (die, abstract_origin, &origin_cu);
13657 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13658 per_objfile->per_bfd->abstract_to_concrete
13659 [origin_die->sect_off].push_back (die->sect_off);
13660 }
13661 }
13662
13663 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13664 reading .debug_rnglists.
13665 Callback's type should be:
13666 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13667 Return true if the attributes are present and valid, otherwise,
13668 return false. */
13669
13670 template <typename Callback>
13671 static bool
13672 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13673 Callback &&callback)
13674 {
13675 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
13676 struct objfile *objfile = dwarf2_per_objfile->objfile;
13677 bfd *obfd = objfile->obfd;
13678 /* Base address selection entry. */
13679 gdb::optional<CORE_ADDR> base;
13680 const gdb_byte *buffer;
13681 CORE_ADDR baseaddr;
13682 bool overflow = false;
13683
13684 base = cu->base_address;
13685
13686 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13687 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
13688 {
13689 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13690 offset);
13691 return false;
13692 }
13693 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
13694
13695 baseaddr = objfile->text_section_offset ();
13696
13697 while (1)
13698 {
13699 /* Initialize it due to a false compiler warning. */
13700 CORE_ADDR range_beginning = 0, range_end = 0;
13701 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13702 + dwarf2_per_objfile->per_bfd->rnglists.size);
13703 unsigned int bytes_read;
13704
13705 if (buffer == buf_end)
13706 {
13707 overflow = true;
13708 break;
13709 }
13710 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13711 switch (rlet)
13712 {
13713 case DW_RLE_end_of_list:
13714 break;
13715 case DW_RLE_base_address:
13716 if (buffer + cu->header.addr_size > buf_end)
13717 {
13718 overflow = true;
13719 break;
13720 }
13721 base = cu->header.read_address (obfd, buffer, &bytes_read);
13722 buffer += bytes_read;
13723 break;
13724 case DW_RLE_start_length:
13725 if (buffer + cu->header.addr_size > buf_end)
13726 {
13727 overflow = true;
13728 break;
13729 }
13730 range_beginning = cu->header.read_address (obfd, buffer,
13731 &bytes_read);
13732 buffer += bytes_read;
13733 range_end = (range_beginning
13734 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13735 buffer += bytes_read;
13736 if (buffer > buf_end)
13737 {
13738 overflow = true;
13739 break;
13740 }
13741 break;
13742 case DW_RLE_offset_pair:
13743 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13744 buffer += bytes_read;
13745 if (buffer > buf_end)
13746 {
13747 overflow = true;
13748 break;
13749 }
13750 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13751 buffer += bytes_read;
13752 if (buffer > buf_end)
13753 {
13754 overflow = true;
13755 break;
13756 }
13757 break;
13758 case DW_RLE_start_end:
13759 if (buffer + 2 * cu->header.addr_size > buf_end)
13760 {
13761 overflow = true;
13762 break;
13763 }
13764 range_beginning = cu->header.read_address (obfd, buffer,
13765 &bytes_read);
13766 buffer += bytes_read;
13767 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13768 buffer += bytes_read;
13769 break;
13770 default:
13771 complaint (_("Invalid .debug_rnglists data (no base address)"));
13772 return false;
13773 }
13774 if (rlet == DW_RLE_end_of_list || overflow)
13775 break;
13776 if (rlet == DW_RLE_base_address)
13777 continue;
13778
13779 if (!base.has_value ())
13780 {
13781 /* We have no valid base address for the ranges
13782 data. */
13783 complaint (_("Invalid .debug_rnglists data (no base address)"));
13784 return false;
13785 }
13786
13787 if (range_beginning > range_end)
13788 {
13789 /* Inverted range entries are invalid. */
13790 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13791 return false;
13792 }
13793
13794 /* Empty range entries have no effect. */
13795 if (range_beginning == range_end)
13796 continue;
13797
13798 range_beginning += *base;
13799 range_end += *base;
13800
13801 /* A not-uncommon case of bad debug info.
13802 Don't pollute the addrmap with bad data. */
13803 if (range_beginning + baseaddr == 0
13804 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
13805 {
13806 complaint (_(".debug_rnglists entry has start address of zero"
13807 " [in module %s]"), objfile_name (objfile));
13808 continue;
13809 }
13810
13811 callback (range_beginning, range_end);
13812 }
13813
13814 if (overflow)
13815 {
13816 complaint (_("Offset %d is not terminated "
13817 "for DW_AT_ranges attribute"),
13818 offset);
13819 return false;
13820 }
13821
13822 return true;
13823 }
13824
13825 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13826 Callback's type should be:
13827 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13828 Return 1 if the attributes are present and valid, otherwise, return 0. */
13829
13830 template <typename Callback>
13831 static int
13832 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13833 Callback &&callback)
13834 {
13835 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13836 struct objfile *objfile = per_objfile->objfile;
13837 struct comp_unit_head *cu_header = &cu->header;
13838 bfd *obfd = objfile->obfd;
13839 unsigned int addr_size = cu_header->addr_size;
13840 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13841 /* Base address selection entry. */
13842 gdb::optional<CORE_ADDR> base;
13843 unsigned int dummy;
13844 const gdb_byte *buffer;
13845 CORE_ADDR baseaddr;
13846
13847 if (cu_header->version >= 5)
13848 return dwarf2_rnglists_process (offset, cu, callback);
13849
13850 base = cu->base_address;
13851
13852 per_objfile->per_bfd->ranges.read (objfile);
13853 if (offset >= per_objfile->per_bfd->ranges.size)
13854 {
13855 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13856 offset);
13857 return 0;
13858 }
13859 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13860
13861 baseaddr = objfile->text_section_offset ();
13862
13863 while (1)
13864 {
13865 CORE_ADDR range_beginning, range_end;
13866
13867 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13868 buffer += addr_size;
13869 range_end = cu->header.read_address (obfd, buffer, &dummy);
13870 buffer += addr_size;
13871 offset += 2 * addr_size;
13872
13873 /* An end of list marker is a pair of zero addresses. */
13874 if (range_beginning == 0 && range_end == 0)
13875 /* Found the end of list entry. */
13876 break;
13877
13878 /* Each base address selection entry is a pair of 2 values.
13879 The first is the largest possible address, the second is
13880 the base address. Check for a base address here. */
13881 if ((range_beginning & mask) == mask)
13882 {
13883 /* If we found the largest possible address, then we already
13884 have the base address in range_end. */
13885 base = range_end;
13886 continue;
13887 }
13888
13889 if (!base.has_value ())
13890 {
13891 /* We have no valid base address for the ranges
13892 data. */
13893 complaint (_("Invalid .debug_ranges data (no base address)"));
13894 return 0;
13895 }
13896
13897 if (range_beginning > range_end)
13898 {
13899 /* Inverted range entries are invalid. */
13900 complaint (_("Invalid .debug_ranges data (inverted range)"));
13901 return 0;
13902 }
13903
13904 /* Empty range entries have no effect. */
13905 if (range_beginning == range_end)
13906 continue;
13907
13908 range_beginning += *base;
13909 range_end += *base;
13910
13911 /* A not-uncommon case of bad debug info.
13912 Don't pollute the addrmap with bad data. */
13913 if (range_beginning + baseaddr == 0
13914 && !per_objfile->per_bfd->has_section_at_zero)
13915 {
13916 complaint (_(".debug_ranges entry has start address of zero"
13917 " [in module %s]"), objfile_name (objfile));
13918 continue;
13919 }
13920
13921 callback (range_beginning, range_end);
13922 }
13923
13924 return 1;
13925 }
13926
13927 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13928 Return 1 if the attributes are present and valid, otherwise, return 0.
13929 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13930
13931 static int
13932 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13933 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13934 dwarf2_psymtab *ranges_pst)
13935 {
13936 struct objfile *objfile = cu->per_objfile->objfile;
13937 struct gdbarch *gdbarch = objfile->arch ();
13938 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13939 int low_set = 0;
13940 CORE_ADDR low = 0;
13941 CORE_ADDR high = 0;
13942 int retval;
13943
13944 retval = dwarf2_ranges_process (offset, cu,
13945 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13946 {
13947 if (ranges_pst != NULL)
13948 {
13949 CORE_ADDR lowpc;
13950 CORE_ADDR highpc;
13951
13952 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13953 range_beginning + baseaddr)
13954 - baseaddr);
13955 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13956 range_end + baseaddr)
13957 - baseaddr);
13958 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13959 lowpc, highpc - 1, ranges_pst);
13960 }
13961
13962 /* FIXME: This is recording everything as a low-high
13963 segment of consecutive addresses. We should have a
13964 data structure for discontiguous block ranges
13965 instead. */
13966 if (! low_set)
13967 {
13968 low = range_beginning;
13969 high = range_end;
13970 low_set = 1;
13971 }
13972 else
13973 {
13974 if (range_beginning < low)
13975 low = range_beginning;
13976 if (range_end > high)
13977 high = range_end;
13978 }
13979 });
13980 if (!retval)
13981 return 0;
13982
13983 if (! low_set)
13984 /* If the first entry is an end-of-list marker, the range
13985 describes an empty scope, i.e. no instructions. */
13986 return 0;
13987
13988 if (low_return)
13989 *low_return = low;
13990 if (high_return)
13991 *high_return = high;
13992 return 1;
13993 }
13994
13995 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13996 definition for the return value. *LOWPC and *HIGHPC are set iff
13997 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13998
13999 static enum pc_bounds_kind
14000 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14001 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14002 dwarf2_psymtab *pst)
14003 {
14004 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
14005 struct attribute *attr;
14006 struct attribute *attr_high;
14007 CORE_ADDR low = 0;
14008 CORE_ADDR high = 0;
14009 enum pc_bounds_kind ret;
14010
14011 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14012 if (attr_high)
14013 {
14014 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14015 if (attr != nullptr)
14016 {
14017 low = attr->value_as_address ();
14018 high = attr_high->value_as_address ();
14019 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14020 high += low;
14021 }
14022 else
14023 /* Found high w/o low attribute. */
14024 return PC_BOUNDS_INVALID;
14025
14026 /* Found consecutive range of addresses. */
14027 ret = PC_BOUNDS_HIGH_LOW;
14028 }
14029 else
14030 {
14031 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14032 if (attr != NULL)
14033 {
14034 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14035 We take advantage of the fact that DW_AT_ranges does not appear
14036 in DW_TAG_compile_unit of DWO files. */
14037 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14038 unsigned int ranges_offset = (DW_UNSND (attr)
14039 + (need_ranges_base
14040 ? cu->ranges_base
14041 : 0));
14042
14043 /* Value of the DW_AT_ranges attribute is the offset in the
14044 .debug_ranges section. */
14045 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14046 return PC_BOUNDS_INVALID;
14047 /* Found discontinuous range of addresses. */
14048 ret = PC_BOUNDS_RANGES;
14049 }
14050 else
14051 return PC_BOUNDS_NOT_PRESENT;
14052 }
14053
14054 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14055 if (high <= low)
14056 return PC_BOUNDS_INVALID;
14057
14058 /* When using the GNU linker, .gnu.linkonce. sections are used to
14059 eliminate duplicate copies of functions and vtables and such.
14060 The linker will arbitrarily choose one and discard the others.
14061 The AT_*_pc values for such functions refer to local labels in
14062 these sections. If the section from that file was discarded, the
14063 labels are not in the output, so the relocs get a value of 0.
14064 If this is a discarded function, mark the pc bounds as invalid,
14065 so that GDB will ignore it. */
14066 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
14067 return PC_BOUNDS_INVALID;
14068
14069 *lowpc = low;
14070 if (highpc)
14071 *highpc = high;
14072 return ret;
14073 }
14074
14075 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14076 its low and high PC addresses. Do nothing if these addresses could not
14077 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14078 and HIGHPC to the high address if greater than HIGHPC. */
14079
14080 static void
14081 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14082 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14083 struct dwarf2_cu *cu)
14084 {
14085 CORE_ADDR low, high;
14086 struct die_info *child = die->child;
14087
14088 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14089 {
14090 *lowpc = std::min (*lowpc, low);
14091 *highpc = std::max (*highpc, high);
14092 }
14093
14094 /* If the language does not allow nested subprograms (either inside
14095 subprograms or lexical blocks), we're done. */
14096 if (cu->language != language_ada)
14097 return;
14098
14099 /* Check all the children of the given DIE. If it contains nested
14100 subprograms, then check their pc bounds. Likewise, we need to
14101 check lexical blocks as well, as they may also contain subprogram
14102 definitions. */
14103 while (child && child->tag)
14104 {
14105 if (child->tag == DW_TAG_subprogram
14106 || child->tag == DW_TAG_lexical_block)
14107 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14108 child = child->sibling;
14109 }
14110 }
14111
14112 /* Get the low and high pc's represented by the scope DIE, and store
14113 them in *LOWPC and *HIGHPC. If the correct values can't be
14114 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14115
14116 static void
14117 get_scope_pc_bounds (struct die_info *die,
14118 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14119 struct dwarf2_cu *cu)
14120 {
14121 CORE_ADDR best_low = (CORE_ADDR) -1;
14122 CORE_ADDR best_high = (CORE_ADDR) 0;
14123 CORE_ADDR current_low, current_high;
14124
14125 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14126 >= PC_BOUNDS_RANGES)
14127 {
14128 best_low = current_low;
14129 best_high = current_high;
14130 }
14131 else
14132 {
14133 struct die_info *child = die->child;
14134
14135 while (child && child->tag)
14136 {
14137 switch (child->tag) {
14138 case DW_TAG_subprogram:
14139 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14140 break;
14141 case DW_TAG_namespace:
14142 case DW_TAG_module:
14143 /* FIXME: carlton/2004-01-16: Should we do this for
14144 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14145 that current GCC's always emit the DIEs corresponding
14146 to definitions of methods of classes as children of a
14147 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14148 the DIEs giving the declarations, which could be
14149 anywhere). But I don't see any reason why the
14150 standards says that they have to be there. */
14151 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14152
14153 if (current_low != ((CORE_ADDR) -1))
14154 {
14155 best_low = std::min (best_low, current_low);
14156 best_high = std::max (best_high, current_high);
14157 }
14158 break;
14159 default:
14160 /* Ignore. */
14161 break;
14162 }
14163
14164 child = child->sibling;
14165 }
14166 }
14167
14168 *lowpc = best_low;
14169 *highpc = best_high;
14170 }
14171
14172 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14173 in DIE. */
14174
14175 static void
14176 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14177 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14178 {
14179 struct objfile *objfile = cu->per_objfile->objfile;
14180 struct gdbarch *gdbarch = objfile->arch ();
14181 struct attribute *attr;
14182 struct attribute *attr_high;
14183
14184 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14185 if (attr_high)
14186 {
14187 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14188 if (attr != nullptr)
14189 {
14190 CORE_ADDR low = attr->value_as_address ();
14191 CORE_ADDR high = attr_high->value_as_address ();
14192
14193 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14194 high += low;
14195
14196 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14197 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14198 cu->get_builder ()->record_block_range (block, low, high - 1);
14199 }
14200 }
14201
14202 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14203 if (attr != nullptr)
14204 {
14205 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14206 We take advantage of the fact that DW_AT_ranges does not appear
14207 in DW_TAG_compile_unit of DWO files. */
14208 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14209
14210 /* The value of the DW_AT_ranges attribute is the offset of the
14211 address range list in the .debug_ranges section. */
14212 unsigned long offset = (DW_UNSND (attr)
14213 + (need_ranges_base ? cu->ranges_base : 0));
14214
14215 std::vector<blockrange> blockvec;
14216 dwarf2_ranges_process (offset, cu,
14217 [&] (CORE_ADDR start, CORE_ADDR end)
14218 {
14219 start += baseaddr;
14220 end += baseaddr;
14221 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14222 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14223 cu->get_builder ()->record_block_range (block, start, end - 1);
14224 blockvec.emplace_back (start, end);
14225 });
14226
14227 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14228 }
14229 }
14230
14231 /* Check whether the producer field indicates either of GCC < 4.6, or the
14232 Intel C/C++ compiler, and cache the result in CU. */
14233
14234 static void
14235 check_producer (struct dwarf2_cu *cu)
14236 {
14237 int major, minor;
14238
14239 if (cu->producer == NULL)
14240 {
14241 /* For unknown compilers expect their behavior is DWARF version
14242 compliant.
14243
14244 GCC started to support .debug_types sections by -gdwarf-4 since
14245 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14246 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14247 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14248 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14249 }
14250 else if (producer_is_gcc (cu->producer, &major, &minor))
14251 {
14252 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14253 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14254 }
14255 else if (producer_is_icc (cu->producer, &major, &minor))
14256 {
14257 cu->producer_is_icc = true;
14258 cu->producer_is_icc_lt_14 = major < 14;
14259 }
14260 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14261 cu->producer_is_codewarrior = true;
14262 else
14263 {
14264 /* For other non-GCC compilers, expect their behavior is DWARF version
14265 compliant. */
14266 }
14267
14268 cu->checked_producer = true;
14269 }
14270
14271 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14272 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14273 during 4.6.0 experimental. */
14274
14275 static bool
14276 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14277 {
14278 if (!cu->checked_producer)
14279 check_producer (cu);
14280
14281 return cu->producer_is_gxx_lt_4_6;
14282 }
14283
14284
14285 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14286 with incorrect is_stmt attributes. */
14287
14288 static bool
14289 producer_is_codewarrior (struct dwarf2_cu *cu)
14290 {
14291 if (!cu->checked_producer)
14292 check_producer (cu);
14293
14294 return cu->producer_is_codewarrior;
14295 }
14296
14297 /* Return the default accessibility type if it is not overridden by
14298 DW_AT_accessibility. */
14299
14300 static enum dwarf_access_attribute
14301 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14302 {
14303 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14304 {
14305 /* The default DWARF 2 accessibility for members is public, the default
14306 accessibility for inheritance is private. */
14307
14308 if (die->tag != DW_TAG_inheritance)
14309 return DW_ACCESS_public;
14310 else
14311 return DW_ACCESS_private;
14312 }
14313 else
14314 {
14315 /* DWARF 3+ defines the default accessibility a different way. The same
14316 rules apply now for DW_TAG_inheritance as for the members and it only
14317 depends on the container kind. */
14318
14319 if (die->parent->tag == DW_TAG_class_type)
14320 return DW_ACCESS_private;
14321 else
14322 return DW_ACCESS_public;
14323 }
14324 }
14325
14326 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14327 offset. If the attribute was not found return 0, otherwise return
14328 1. If it was found but could not properly be handled, set *OFFSET
14329 to 0. */
14330
14331 static int
14332 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14333 LONGEST *offset)
14334 {
14335 struct attribute *attr;
14336
14337 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14338 if (attr != NULL)
14339 {
14340 *offset = 0;
14341
14342 /* Note that we do not check for a section offset first here.
14343 This is because DW_AT_data_member_location is new in DWARF 4,
14344 so if we see it, we can assume that a constant form is really
14345 a constant and not a section offset. */
14346 if (attr->form_is_constant ())
14347 *offset = attr->constant_value (0);
14348 else if (attr->form_is_section_offset ())
14349 dwarf2_complex_location_expr_complaint ();
14350 else if (attr->form_is_block ())
14351 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14352 else
14353 dwarf2_complex_location_expr_complaint ();
14354
14355 return 1;
14356 }
14357
14358 return 0;
14359 }
14360
14361 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14362
14363 static void
14364 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14365 struct field *field)
14366 {
14367 struct attribute *attr;
14368
14369 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14370 if (attr != NULL)
14371 {
14372 if (attr->form_is_constant ())
14373 {
14374 LONGEST offset = attr->constant_value (0);
14375 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14376 }
14377 else if (attr->form_is_section_offset ())
14378 dwarf2_complex_location_expr_complaint ();
14379 else if (attr->form_is_block ())
14380 {
14381 bool handled;
14382 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14383 if (handled)
14384 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14385 else
14386 {
14387 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14388 struct objfile *objfile = per_objfile->objfile;
14389 struct dwarf2_locexpr_baton *dlbaton
14390 = XOBNEW (&objfile->objfile_obstack,
14391 struct dwarf2_locexpr_baton);
14392 dlbaton->data = DW_BLOCK (attr)->data;
14393 dlbaton->size = DW_BLOCK (attr)->size;
14394 /* When using this baton, we want to compute the address
14395 of the field, not the value. This is why
14396 is_reference is set to false here. */
14397 dlbaton->is_reference = false;
14398 dlbaton->per_objfile = per_objfile;
14399 dlbaton->per_cu = cu->per_cu;
14400
14401 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14402 }
14403 }
14404 else
14405 dwarf2_complex_location_expr_complaint ();
14406 }
14407 }
14408
14409 /* Add an aggregate field to the field list. */
14410
14411 static void
14412 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14413 struct dwarf2_cu *cu)
14414 {
14415 struct objfile *objfile = cu->per_objfile->objfile;
14416 struct gdbarch *gdbarch = objfile->arch ();
14417 struct nextfield *new_field;
14418 struct attribute *attr;
14419 struct field *fp;
14420 const char *fieldname = "";
14421
14422 if (die->tag == DW_TAG_inheritance)
14423 {
14424 fip->baseclasses.emplace_back ();
14425 new_field = &fip->baseclasses.back ();
14426 }
14427 else
14428 {
14429 fip->fields.emplace_back ();
14430 new_field = &fip->fields.back ();
14431 }
14432
14433 new_field->offset = die->sect_off;
14434
14435 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14436 if (attr != nullptr)
14437 new_field->accessibility = DW_UNSND (attr);
14438 else
14439 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14440 if (new_field->accessibility != DW_ACCESS_public)
14441 fip->non_public_fields = 1;
14442
14443 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14444 if (attr != nullptr)
14445 new_field->virtuality = DW_UNSND (attr);
14446 else
14447 new_field->virtuality = DW_VIRTUALITY_none;
14448
14449 fp = &new_field->field;
14450
14451 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14452 {
14453 /* Data member other than a C++ static data member. */
14454
14455 /* Get type of field. */
14456 fp->type = die_type (die, cu);
14457
14458 SET_FIELD_BITPOS (*fp, 0);
14459
14460 /* Get bit size of field (zero if none). */
14461 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14462 if (attr != nullptr)
14463 {
14464 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14465 }
14466 else
14467 {
14468 FIELD_BITSIZE (*fp) = 0;
14469 }
14470
14471 /* Get bit offset of field. */
14472 handle_data_member_location (die, cu, fp);
14473 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14474 if (attr != nullptr)
14475 {
14476 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14477 {
14478 /* For big endian bits, the DW_AT_bit_offset gives the
14479 additional bit offset from the MSB of the containing
14480 anonymous object to the MSB of the field. We don't
14481 have to do anything special since we don't need to
14482 know the size of the anonymous object. */
14483 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14484 }
14485 else
14486 {
14487 /* For little endian bits, compute the bit offset to the
14488 MSB of the anonymous object, subtract off the number of
14489 bits from the MSB of the field to the MSB of the
14490 object, and then subtract off the number of bits of
14491 the field itself. The result is the bit offset of
14492 the LSB of the field. */
14493 int anonymous_size;
14494 int bit_offset = DW_UNSND (attr);
14495
14496 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14497 if (attr != nullptr)
14498 {
14499 /* The size of the anonymous object containing
14500 the bit field is explicit, so use the
14501 indicated size (in bytes). */
14502 anonymous_size = DW_UNSND (attr);
14503 }
14504 else
14505 {
14506 /* The size of the anonymous object containing
14507 the bit field must be inferred from the type
14508 attribute of the data member containing the
14509 bit field. */
14510 anonymous_size = TYPE_LENGTH (fp->type);
14511 }
14512 SET_FIELD_BITPOS (*fp,
14513 (FIELD_BITPOS (*fp)
14514 + anonymous_size * bits_per_byte
14515 - bit_offset - FIELD_BITSIZE (*fp)));
14516 }
14517 }
14518 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14519 if (attr != NULL)
14520 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14521 + attr->constant_value (0)));
14522
14523 /* Get name of field. */
14524 fieldname = dwarf2_name (die, cu);
14525 if (fieldname == NULL)
14526 fieldname = "";
14527
14528 /* The name is already allocated along with this objfile, so we don't
14529 need to duplicate it for the type. */
14530 fp->name = fieldname;
14531
14532 /* Change accessibility for artificial fields (e.g. virtual table
14533 pointer or virtual base class pointer) to private. */
14534 if (dwarf2_attr (die, DW_AT_artificial, cu))
14535 {
14536 FIELD_ARTIFICIAL (*fp) = 1;
14537 new_field->accessibility = DW_ACCESS_private;
14538 fip->non_public_fields = 1;
14539 }
14540 }
14541 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14542 {
14543 /* C++ static member. */
14544
14545 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14546 is a declaration, but all versions of G++ as of this writing
14547 (so through at least 3.2.1) incorrectly generate
14548 DW_TAG_variable tags. */
14549
14550 const char *physname;
14551
14552 /* Get name of field. */
14553 fieldname = dwarf2_name (die, cu);
14554 if (fieldname == NULL)
14555 return;
14556
14557 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14558 if (attr
14559 /* Only create a symbol if this is an external value.
14560 new_symbol checks this and puts the value in the global symbol
14561 table, which we want. If it is not external, new_symbol
14562 will try to put the value in cu->list_in_scope which is wrong. */
14563 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14564 {
14565 /* A static const member, not much different than an enum as far as
14566 we're concerned, except that we can support more types. */
14567 new_symbol (die, NULL, cu);
14568 }
14569
14570 /* Get physical name. */
14571 physname = dwarf2_physname (fieldname, die, cu);
14572
14573 /* The name is already allocated along with this objfile, so we don't
14574 need to duplicate it for the type. */
14575 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14576 FIELD_TYPE (*fp) = die_type (die, cu);
14577 FIELD_NAME (*fp) = fieldname;
14578 }
14579 else if (die->tag == DW_TAG_inheritance)
14580 {
14581 /* C++ base class field. */
14582 handle_data_member_location (die, cu, fp);
14583 FIELD_BITSIZE (*fp) = 0;
14584 FIELD_TYPE (*fp) = die_type (die, cu);
14585 FIELD_NAME (*fp) = fp->type->name ();
14586 }
14587 else
14588 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14589 }
14590
14591 /* Can the type given by DIE define another type? */
14592
14593 static bool
14594 type_can_define_types (const struct die_info *die)
14595 {
14596 switch (die->tag)
14597 {
14598 case DW_TAG_typedef:
14599 case DW_TAG_class_type:
14600 case DW_TAG_structure_type:
14601 case DW_TAG_union_type:
14602 case DW_TAG_enumeration_type:
14603 return true;
14604
14605 default:
14606 return false;
14607 }
14608 }
14609
14610 /* Add a type definition defined in the scope of the FIP's class. */
14611
14612 static void
14613 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14614 struct dwarf2_cu *cu)
14615 {
14616 struct decl_field fp;
14617 memset (&fp, 0, sizeof (fp));
14618
14619 gdb_assert (type_can_define_types (die));
14620
14621 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14622 fp.name = dwarf2_name (die, cu);
14623 fp.type = read_type_die (die, cu);
14624
14625 /* Save accessibility. */
14626 enum dwarf_access_attribute accessibility;
14627 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14628 if (attr != NULL)
14629 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14630 else
14631 accessibility = dwarf2_default_access_attribute (die, cu);
14632 switch (accessibility)
14633 {
14634 case DW_ACCESS_public:
14635 /* The assumed value if neither private nor protected. */
14636 break;
14637 case DW_ACCESS_private:
14638 fp.is_private = 1;
14639 break;
14640 case DW_ACCESS_protected:
14641 fp.is_protected = 1;
14642 break;
14643 default:
14644 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14645 }
14646
14647 if (die->tag == DW_TAG_typedef)
14648 fip->typedef_field_list.push_back (fp);
14649 else
14650 fip->nested_types_list.push_back (fp);
14651 }
14652
14653 /* A convenience typedef that's used when finding the discriminant
14654 field for a variant part. */
14655 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14656 offset_map_type;
14657
14658 /* Compute the discriminant range for a given variant. OBSTACK is
14659 where the results will be stored. VARIANT is the variant to
14660 process. IS_UNSIGNED indicates whether the discriminant is signed
14661 or unsigned. */
14662
14663 static const gdb::array_view<discriminant_range>
14664 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14665 bool is_unsigned)
14666 {
14667 std::vector<discriminant_range> ranges;
14668
14669 if (variant.default_branch)
14670 return {};
14671
14672 if (variant.discr_list_data == nullptr)
14673 {
14674 discriminant_range r
14675 = {variant.discriminant_value, variant.discriminant_value};
14676 ranges.push_back (r);
14677 }
14678 else
14679 {
14680 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14681 variant.discr_list_data->size);
14682 while (!data.empty ())
14683 {
14684 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14685 {
14686 complaint (_("invalid discriminant marker: %d"), data[0]);
14687 break;
14688 }
14689 bool is_range = data[0] == DW_DSC_range;
14690 data = data.slice (1);
14691
14692 ULONGEST low, high;
14693 unsigned int bytes_read;
14694
14695 if (data.empty ())
14696 {
14697 complaint (_("DW_AT_discr_list missing low value"));
14698 break;
14699 }
14700 if (is_unsigned)
14701 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14702 else
14703 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14704 &bytes_read);
14705 data = data.slice (bytes_read);
14706
14707 if (is_range)
14708 {
14709 if (data.empty ())
14710 {
14711 complaint (_("DW_AT_discr_list missing high value"));
14712 break;
14713 }
14714 if (is_unsigned)
14715 high = read_unsigned_leb128 (nullptr, data.data (),
14716 &bytes_read);
14717 else
14718 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14719 &bytes_read);
14720 data = data.slice (bytes_read);
14721 }
14722 else
14723 high = low;
14724
14725 ranges.push_back ({ low, high });
14726 }
14727 }
14728
14729 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14730 ranges.size ());
14731 std::copy (ranges.begin (), ranges.end (), result);
14732 return gdb::array_view<discriminant_range> (result, ranges.size ());
14733 }
14734
14735 static const gdb::array_view<variant_part> create_variant_parts
14736 (struct obstack *obstack,
14737 const offset_map_type &offset_map,
14738 struct field_info *fi,
14739 const std::vector<variant_part_builder> &variant_parts);
14740
14741 /* Fill in a "struct variant" for a given variant field. RESULT is
14742 the variant to fill in. OBSTACK is where any needed allocations
14743 will be done. OFFSET_MAP holds the mapping from section offsets to
14744 fields for the type. FI describes the fields of the type we're
14745 processing. FIELD is the variant field we're converting. */
14746
14747 static void
14748 create_one_variant (variant &result, struct obstack *obstack,
14749 const offset_map_type &offset_map,
14750 struct field_info *fi, const variant_field &field)
14751 {
14752 result.discriminants = convert_variant_range (obstack, field, false);
14753 result.first_field = field.first_field + fi->baseclasses.size ();
14754 result.last_field = field.last_field + fi->baseclasses.size ();
14755 result.parts = create_variant_parts (obstack, offset_map, fi,
14756 field.variant_parts);
14757 }
14758
14759 /* Fill in a "struct variant_part" for a given variant part. RESULT
14760 is the variant part to fill in. OBSTACK is where any needed
14761 allocations will be done. OFFSET_MAP holds the mapping from
14762 section offsets to fields for the type. FI describes the fields of
14763 the type we're processing. BUILDER is the variant part to be
14764 converted. */
14765
14766 static void
14767 create_one_variant_part (variant_part &result,
14768 struct obstack *obstack,
14769 const offset_map_type &offset_map,
14770 struct field_info *fi,
14771 const variant_part_builder &builder)
14772 {
14773 auto iter = offset_map.find (builder.discriminant_offset);
14774 if (iter == offset_map.end ())
14775 {
14776 result.discriminant_index = -1;
14777 /* Doesn't matter. */
14778 result.is_unsigned = false;
14779 }
14780 else
14781 {
14782 result.discriminant_index = iter->second;
14783 result.is_unsigned
14784 = TYPE_UNSIGNED (FIELD_TYPE
14785 (fi->fields[result.discriminant_index].field));
14786 }
14787
14788 size_t n = builder.variants.size ();
14789 variant *output = new (obstack) variant[n];
14790 for (size_t i = 0; i < n; ++i)
14791 create_one_variant (output[i], obstack, offset_map, fi,
14792 builder.variants[i]);
14793
14794 result.variants = gdb::array_view<variant> (output, n);
14795 }
14796
14797 /* Create a vector of variant parts that can be attached to a type.
14798 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14799 holds the mapping from section offsets to fields for the type. FI
14800 describes the fields of the type we're processing. VARIANT_PARTS
14801 is the vector to convert. */
14802
14803 static const gdb::array_view<variant_part>
14804 create_variant_parts (struct obstack *obstack,
14805 const offset_map_type &offset_map,
14806 struct field_info *fi,
14807 const std::vector<variant_part_builder> &variant_parts)
14808 {
14809 if (variant_parts.empty ())
14810 return {};
14811
14812 size_t n = variant_parts.size ();
14813 variant_part *result = new (obstack) variant_part[n];
14814 for (size_t i = 0; i < n; ++i)
14815 create_one_variant_part (result[i], obstack, offset_map, fi,
14816 variant_parts[i]);
14817
14818 return gdb::array_view<variant_part> (result, n);
14819 }
14820
14821 /* Compute the variant part vector for FIP, attaching it to TYPE when
14822 done. */
14823
14824 static void
14825 add_variant_property (struct field_info *fip, struct type *type,
14826 struct dwarf2_cu *cu)
14827 {
14828 /* Map section offsets of fields to their field index. Note the
14829 field index here does not take the number of baseclasses into
14830 account. */
14831 offset_map_type offset_map;
14832 for (int i = 0; i < fip->fields.size (); ++i)
14833 offset_map[fip->fields[i].offset] = i;
14834
14835 struct objfile *objfile = cu->per_objfile->objfile;
14836 gdb::array_view<variant_part> parts
14837 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14838 fip->variant_parts);
14839
14840 struct dynamic_prop prop;
14841 prop.kind = PROP_VARIANT_PARTS;
14842 prop.data.variant_parts
14843 = ((gdb::array_view<variant_part> *)
14844 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14845
14846 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14847 }
14848
14849 /* Create the vector of fields, and attach it to the type. */
14850
14851 static void
14852 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14853 struct dwarf2_cu *cu)
14854 {
14855 int nfields = fip->nfields ();
14856
14857 /* Record the field count, allocate space for the array of fields,
14858 and create blank accessibility bitfields if necessary. */
14859 type->set_num_fields (nfields);
14860 type->set_fields
14861 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14862
14863 if (fip->non_public_fields && cu->language != language_ada)
14864 {
14865 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14866
14867 TYPE_FIELD_PRIVATE_BITS (type) =
14868 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14869 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14870
14871 TYPE_FIELD_PROTECTED_BITS (type) =
14872 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14873 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14874
14875 TYPE_FIELD_IGNORE_BITS (type) =
14876 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14877 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14878 }
14879
14880 /* If the type has baseclasses, allocate and clear a bit vector for
14881 TYPE_FIELD_VIRTUAL_BITS. */
14882 if (!fip->baseclasses.empty () && cu->language != language_ada)
14883 {
14884 int num_bytes = B_BYTES (fip->baseclasses.size ());
14885 unsigned char *pointer;
14886
14887 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14888 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14889 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14890 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14891 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14892 }
14893
14894 if (!fip->variant_parts.empty ())
14895 add_variant_property (fip, type, cu);
14896
14897 /* Copy the saved-up fields into the field vector. */
14898 for (int i = 0; i < nfields; ++i)
14899 {
14900 struct nextfield &field
14901 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14902 : fip->fields[i - fip->baseclasses.size ()]);
14903
14904 type->field (i) = field.field;
14905 switch (field.accessibility)
14906 {
14907 case DW_ACCESS_private:
14908 if (cu->language != language_ada)
14909 SET_TYPE_FIELD_PRIVATE (type, i);
14910 break;
14911
14912 case DW_ACCESS_protected:
14913 if (cu->language != language_ada)
14914 SET_TYPE_FIELD_PROTECTED (type, i);
14915 break;
14916
14917 case DW_ACCESS_public:
14918 break;
14919
14920 default:
14921 /* Unknown accessibility. Complain and treat it as public. */
14922 {
14923 complaint (_("unsupported accessibility %d"),
14924 field.accessibility);
14925 }
14926 break;
14927 }
14928 if (i < fip->baseclasses.size ())
14929 {
14930 switch (field.virtuality)
14931 {
14932 case DW_VIRTUALITY_virtual:
14933 case DW_VIRTUALITY_pure_virtual:
14934 if (cu->language == language_ada)
14935 error (_("unexpected virtuality in component of Ada type"));
14936 SET_TYPE_FIELD_VIRTUAL (type, i);
14937 break;
14938 }
14939 }
14940 }
14941 }
14942
14943 /* Return true if this member function is a constructor, false
14944 otherwise. */
14945
14946 static int
14947 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14948 {
14949 const char *fieldname;
14950 const char *type_name;
14951 int len;
14952
14953 if (die->parent == NULL)
14954 return 0;
14955
14956 if (die->parent->tag != DW_TAG_structure_type
14957 && die->parent->tag != DW_TAG_union_type
14958 && die->parent->tag != DW_TAG_class_type)
14959 return 0;
14960
14961 fieldname = dwarf2_name (die, cu);
14962 type_name = dwarf2_name (die->parent, cu);
14963 if (fieldname == NULL || type_name == NULL)
14964 return 0;
14965
14966 len = strlen (fieldname);
14967 return (strncmp (fieldname, type_name, len) == 0
14968 && (type_name[len] == '\0' || type_name[len] == '<'));
14969 }
14970
14971 /* Check if the given VALUE is a recognized enum
14972 dwarf_defaulted_attribute constant according to DWARF5 spec,
14973 Table 7.24. */
14974
14975 static bool
14976 is_valid_DW_AT_defaulted (ULONGEST value)
14977 {
14978 switch (value)
14979 {
14980 case DW_DEFAULTED_no:
14981 case DW_DEFAULTED_in_class:
14982 case DW_DEFAULTED_out_of_class:
14983 return true;
14984 }
14985
14986 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14987 return false;
14988 }
14989
14990 /* Add a member function to the proper fieldlist. */
14991
14992 static void
14993 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14994 struct type *type, struct dwarf2_cu *cu)
14995 {
14996 struct objfile *objfile = cu->per_objfile->objfile;
14997 struct attribute *attr;
14998 int i;
14999 struct fnfieldlist *flp = nullptr;
15000 struct fn_field *fnp;
15001 const char *fieldname;
15002 struct type *this_type;
15003 enum dwarf_access_attribute accessibility;
15004
15005 if (cu->language == language_ada)
15006 error (_("unexpected member function in Ada type"));
15007
15008 /* Get name of member function. */
15009 fieldname = dwarf2_name (die, cu);
15010 if (fieldname == NULL)
15011 return;
15012
15013 /* Look up member function name in fieldlist. */
15014 for (i = 0; i < fip->fnfieldlists.size (); i++)
15015 {
15016 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15017 {
15018 flp = &fip->fnfieldlists[i];
15019 break;
15020 }
15021 }
15022
15023 /* Create a new fnfieldlist if necessary. */
15024 if (flp == nullptr)
15025 {
15026 fip->fnfieldlists.emplace_back ();
15027 flp = &fip->fnfieldlists.back ();
15028 flp->name = fieldname;
15029 i = fip->fnfieldlists.size () - 1;
15030 }
15031
15032 /* Create a new member function field and add it to the vector of
15033 fnfieldlists. */
15034 flp->fnfields.emplace_back ();
15035 fnp = &flp->fnfields.back ();
15036
15037 /* Delay processing of the physname until later. */
15038 if (cu->language == language_cplus)
15039 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15040 die, cu);
15041 else
15042 {
15043 const char *physname = dwarf2_physname (fieldname, die, cu);
15044 fnp->physname = physname ? physname : "";
15045 }
15046
15047 fnp->type = alloc_type (objfile);
15048 this_type = read_type_die (die, cu);
15049 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15050 {
15051 int nparams = this_type->num_fields ();
15052
15053 /* TYPE is the domain of this method, and THIS_TYPE is the type
15054 of the method itself (TYPE_CODE_METHOD). */
15055 smash_to_method_type (fnp->type, type,
15056 TYPE_TARGET_TYPE (this_type),
15057 this_type->fields (),
15058 this_type->num_fields (),
15059 TYPE_VARARGS (this_type));
15060
15061 /* Handle static member functions.
15062 Dwarf2 has no clean way to discern C++ static and non-static
15063 member functions. G++ helps GDB by marking the first
15064 parameter for non-static member functions (which is the this
15065 pointer) as artificial. We obtain this information from
15066 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15067 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15068 fnp->voffset = VOFFSET_STATIC;
15069 }
15070 else
15071 complaint (_("member function type missing for '%s'"),
15072 dwarf2_full_name (fieldname, die, cu));
15073
15074 /* Get fcontext from DW_AT_containing_type if present. */
15075 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15076 fnp->fcontext = die_containing_type (die, cu);
15077
15078 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15079 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15080
15081 /* Get accessibility. */
15082 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15083 if (attr != nullptr)
15084 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15085 else
15086 accessibility = dwarf2_default_access_attribute (die, cu);
15087 switch (accessibility)
15088 {
15089 case DW_ACCESS_private:
15090 fnp->is_private = 1;
15091 break;
15092 case DW_ACCESS_protected:
15093 fnp->is_protected = 1;
15094 break;
15095 }
15096
15097 /* Check for artificial methods. */
15098 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15099 if (attr && DW_UNSND (attr) != 0)
15100 fnp->is_artificial = 1;
15101
15102 /* Check for defaulted methods. */
15103 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15104 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15105 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15106
15107 /* Check for deleted methods. */
15108 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15109 if (attr != nullptr && DW_UNSND (attr) != 0)
15110 fnp->is_deleted = 1;
15111
15112 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15113
15114 /* Get index in virtual function table if it is a virtual member
15115 function. For older versions of GCC, this is an offset in the
15116 appropriate virtual table, as specified by DW_AT_containing_type.
15117 For everyone else, it is an expression to be evaluated relative
15118 to the object address. */
15119
15120 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15121 if (attr != nullptr)
15122 {
15123 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15124 {
15125 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15126 {
15127 /* Old-style GCC. */
15128 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15129 }
15130 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15131 || (DW_BLOCK (attr)->size > 1
15132 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15133 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15134 {
15135 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15136 if ((fnp->voffset % cu->header.addr_size) != 0)
15137 dwarf2_complex_location_expr_complaint ();
15138 else
15139 fnp->voffset /= cu->header.addr_size;
15140 fnp->voffset += 2;
15141 }
15142 else
15143 dwarf2_complex_location_expr_complaint ();
15144
15145 if (!fnp->fcontext)
15146 {
15147 /* If there is no `this' field and no DW_AT_containing_type,
15148 we cannot actually find a base class context for the
15149 vtable! */
15150 if (this_type->num_fields () == 0
15151 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15152 {
15153 complaint (_("cannot determine context for virtual member "
15154 "function \"%s\" (offset %s)"),
15155 fieldname, sect_offset_str (die->sect_off));
15156 }
15157 else
15158 {
15159 fnp->fcontext
15160 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15161 }
15162 }
15163 }
15164 else if (attr->form_is_section_offset ())
15165 {
15166 dwarf2_complex_location_expr_complaint ();
15167 }
15168 else
15169 {
15170 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15171 fieldname);
15172 }
15173 }
15174 else
15175 {
15176 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15177 if (attr && DW_UNSND (attr))
15178 {
15179 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15180 complaint (_("Member function \"%s\" (offset %s) is virtual "
15181 "but the vtable offset is not specified"),
15182 fieldname, sect_offset_str (die->sect_off));
15183 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15184 TYPE_CPLUS_DYNAMIC (type) = 1;
15185 }
15186 }
15187 }
15188
15189 /* Create the vector of member function fields, and attach it to the type. */
15190
15191 static void
15192 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15193 struct dwarf2_cu *cu)
15194 {
15195 if (cu->language == language_ada)
15196 error (_("unexpected member functions in Ada type"));
15197
15198 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15199 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15200 TYPE_ALLOC (type,
15201 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15202
15203 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15204 {
15205 struct fnfieldlist &nf = fip->fnfieldlists[i];
15206 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15207
15208 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15209 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15210 fn_flp->fn_fields = (struct fn_field *)
15211 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15212
15213 for (int k = 0; k < nf.fnfields.size (); ++k)
15214 fn_flp->fn_fields[k] = nf.fnfields[k];
15215 }
15216
15217 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15218 }
15219
15220 /* Returns non-zero if NAME is the name of a vtable member in CU's
15221 language, zero otherwise. */
15222 static int
15223 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15224 {
15225 static const char vptr[] = "_vptr";
15226
15227 /* Look for the C++ form of the vtable. */
15228 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15229 return 1;
15230
15231 return 0;
15232 }
15233
15234 /* GCC outputs unnamed structures that are really pointers to member
15235 functions, with the ABI-specified layout. If TYPE describes
15236 such a structure, smash it into a member function type.
15237
15238 GCC shouldn't do this; it should just output pointer to member DIEs.
15239 This is GCC PR debug/28767. */
15240
15241 static void
15242 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15243 {
15244 struct type *pfn_type, *self_type, *new_type;
15245
15246 /* Check for a structure with no name and two children. */
15247 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15248 return;
15249
15250 /* Check for __pfn and __delta members. */
15251 if (TYPE_FIELD_NAME (type, 0) == NULL
15252 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15253 || TYPE_FIELD_NAME (type, 1) == NULL
15254 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15255 return;
15256
15257 /* Find the type of the method. */
15258 pfn_type = TYPE_FIELD_TYPE (type, 0);
15259 if (pfn_type == NULL
15260 || pfn_type->code () != TYPE_CODE_PTR
15261 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15262 return;
15263
15264 /* Look for the "this" argument. */
15265 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15266 if (pfn_type->num_fields () == 0
15267 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15268 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
15269 return;
15270
15271 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15272 new_type = alloc_type (objfile);
15273 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15274 pfn_type->fields (), pfn_type->num_fields (),
15275 TYPE_VARARGS (pfn_type));
15276 smash_to_methodptr_type (type, new_type);
15277 }
15278
15279 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15280 appropriate error checking and issuing complaints if there is a
15281 problem. */
15282
15283 static ULONGEST
15284 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15285 {
15286 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15287
15288 if (attr == nullptr)
15289 return 0;
15290
15291 if (!attr->form_is_constant ())
15292 {
15293 complaint (_("DW_AT_alignment must have constant form"
15294 " - DIE at %s [in module %s]"),
15295 sect_offset_str (die->sect_off),
15296 objfile_name (cu->per_objfile->objfile));
15297 return 0;
15298 }
15299
15300 ULONGEST align;
15301 if (attr->form == DW_FORM_sdata)
15302 {
15303 LONGEST val = DW_SND (attr);
15304 if (val < 0)
15305 {
15306 complaint (_("DW_AT_alignment value must not be negative"
15307 " - DIE at %s [in module %s]"),
15308 sect_offset_str (die->sect_off),
15309 objfile_name (cu->per_objfile->objfile));
15310 return 0;
15311 }
15312 align = val;
15313 }
15314 else
15315 align = DW_UNSND (attr);
15316
15317 if (align == 0)
15318 {
15319 complaint (_("DW_AT_alignment value must not be zero"
15320 " - DIE at %s [in module %s]"),
15321 sect_offset_str (die->sect_off),
15322 objfile_name (cu->per_objfile->objfile));
15323 return 0;
15324 }
15325 if ((align & (align - 1)) != 0)
15326 {
15327 complaint (_("DW_AT_alignment value must be a power of 2"
15328 " - DIE at %s [in module %s]"),
15329 sect_offset_str (die->sect_off),
15330 objfile_name (cu->per_objfile->objfile));
15331 return 0;
15332 }
15333
15334 return align;
15335 }
15336
15337 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15338 the alignment for TYPE. */
15339
15340 static void
15341 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15342 struct type *type)
15343 {
15344 if (!set_type_align (type, get_alignment (cu, die)))
15345 complaint (_("DW_AT_alignment value too large"
15346 " - DIE at %s [in module %s]"),
15347 sect_offset_str (die->sect_off),
15348 objfile_name (cu->per_objfile->objfile));
15349 }
15350
15351 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15352 constant for a type, according to DWARF5 spec, Table 5.5. */
15353
15354 static bool
15355 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15356 {
15357 switch (value)
15358 {
15359 case DW_CC_normal:
15360 case DW_CC_pass_by_reference:
15361 case DW_CC_pass_by_value:
15362 return true;
15363
15364 default:
15365 complaint (_("unrecognized DW_AT_calling_convention value "
15366 "(%s) for a type"), pulongest (value));
15367 return false;
15368 }
15369 }
15370
15371 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15372 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15373 also according to GNU-specific values (see include/dwarf2.h). */
15374
15375 static bool
15376 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15377 {
15378 switch (value)
15379 {
15380 case DW_CC_normal:
15381 case DW_CC_program:
15382 case DW_CC_nocall:
15383 return true;
15384
15385 case DW_CC_GNU_renesas_sh:
15386 case DW_CC_GNU_borland_fastcall_i386:
15387 case DW_CC_GDB_IBM_OpenCL:
15388 return true;
15389
15390 default:
15391 complaint (_("unrecognized DW_AT_calling_convention value "
15392 "(%s) for a subroutine"), pulongest (value));
15393 return false;
15394 }
15395 }
15396
15397 /* Called when we find the DIE that starts a structure or union scope
15398 (definition) to create a type for the structure or union. Fill in
15399 the type's name and general properties; the members will not be
15400 processed until process_structure_scope. A symbol table entry for
15401 the type will also not be done until process_structure_scope (assuming
15402 the type has a name).
15403
15404 NOTE: we need to call these functions regardless of whether or not the
15405 DIE has a DW_AT_name attribute, since it might be an anonymous
15406 structure or union. This gets the type entered into our set of
15407 user defined types. */
15408
15409 static struct type *
15410 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15411 {
15412 struct objfile *objfile = cu->per_objfile->objfile;
15413 struct type *type;
15414 struct attribute *attr;
15415 const char *name;
15416
15417 /* If the definition of this type lives in .debug_types, read that type.
15418 Don't follow DW_AT_specification though, that will take us back up
15419 the chain and we want to go down. */
15420 attr = die->attr (DW_AT_signature);
15421 if (attr != nullptr)
15422 {
15423 type = get_DW_AT_signature_type (die, attr, cu);
15424
15425 /* The type's CU may not be the same as CU.
15426 Ensure TYPE is recorded with CU in die_type_hash. */
15427 return set_die_type (die, type, cu);
15428 }
15429
15430 type = alloc_type (objfile);
15431 INIT_CPLUS_SPECIFIC (type);
15432
15433 name = dwarf2_name (die, cu);
15434 if (name != NULL)
15435 {
15436 if (cu->language == language_cplus
15437 || cu->language == language_d
15438 || cu->language == language_rust)
15439 {
15440 const char *full_name = dwarf2_full_name (name, die, cu);
15441
15442 /* dwarf2_full_name might have already finished building the DIE's
15443 type. If so, there is no need to continue. */
15444 if (get_die_type (die, cu) != NULL)
15445 return get_die_type (die, cu);
15446
15447 type->set_name (full_name);
15448 }
15449 else
15450 {
15451 /* The name is already allocated along with this objfile, so
15452 we don't need to duplicate it for the type. */
15453 type->set_name (name);
15454 }
15455 }
15456
15457 if (die->tag == DW_TAG_structure_type)
15458 {
15459 type->set_code (TYPE_CODE_STRUCT);
15460 }
15461 else if (die->tag == DW_TAG_union_type)
15462 {
15463 type->set_code (TYPE_CODE_UNION);
15464 }
15465 else
15466 {
15467 type->set_code (TYPE_CODE_STRUCT);
15468 }
15469
15470 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15471 TYPE_DECLARED_CLASS (type) = 1;
15472
15473 /* Store the calling convention in the type if it's available in
15474 the die. Otherwise the calling convention remains set to
15475 the default value DW_CC_normal. */
15476 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15477 if (attr != nullptr
15478 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15479 {
15480 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15481 TYPE_CPLUS_CALLING_CONVENTION (type)
15482 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15483 }
15484
15485 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15486 if (attr != nullptr)
15487 {
15488 if (attr->form_is_constant ())
15489 TYPE_LENGTH (type) = DW_UNSND (attr);
15490 else
15491 {
15492 struct dynamic_prop prop;
15493 if (attr_to_dynamic_prop (attr, die, cu, &prop,
15494 cu->per_cu->addr_type ()))
15495 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15496 TYPE_LENGTH (type) = 0;
15497 }
15498 }
15499 else
15500 {
15501 TYPE_LENGTH (type) = 0;
15502 }
15503
15504 maybe_set_alignment (cu, die, type);
15505
15506 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15507 {
15508 /* ICC<14 does not output the required DW_AT_declaration on
15509 incomplete types, but gives them a size of zero. */
15510 TYPE_STUB (type) = 1;
15511 }
15512 else
15513 TYPE_STUB_SUPPORTED (type) = 1;
15514
15515 if (die_is_declaration (die, cu))
15516 TYPE_STUB (type) = 1;
15517 else if (attr == NULL && die->child == NULL
15518 && producer_is_realview (cu->producer))
15519 /* RealView does not output the required DW_AT_declaration
15520 on incomplete types. */
15521 TYPE_STUB (type) = 1;
15522
15523 /* We need to add the type field to the die immediately so we don't
15524 infinitely recurse when dealing with pointers to the structure
15525 type within the structure itself. */
15526 set_die_type (die, type, cu);
15527
15528 /* set_die_type should be already done. */
15529 set_descriptive_type (type, die, cu);
15530
15531 return type;
15532 }
15533
15534 static void handle_struct_member_die
15535 (struct die_info *child_die,
15536 struct type *type,
15537 struct field_info *fi,
15538 std::vector<struct symbol *> *template_args,
15539 struct dwarf2_cu *cu);
15540
15541 /* A helper for handle_struct_member_die that handles
15542 DW_TAG_variant_part. */
15543
15544 static void
15545 handle_variant_part (struct die_info *die, struct type *type,
15546 struct field_info *fi,
15547 std::vector<struct symbol *> *template_args,
15548 struct dwarf2_cu *cu)
15549 {
15550 variant_part_builder *new_part;
15551 if (fi->current_variant_part == nullptr)
15552 {
15553 fi->variant_parts.emplace_back ();
15554 new_part = &fi->variant_parts.back ();
15555 }
15556 else if (!fi->current_variant_part->processing_variant)
15557 {
15558 complaint (_("nested DW_TAG_variant_part seen "
15559 "- DIE at %s [in module %s]"),
15560 sect_offset_str (die->sect_off),
15561 objfile_name (cu->per_objfile->objfile));
15562 return;
15563 }
15564 else
15565 {
15566 variant_field &current = fi->current_variant_part->variants.back ();
15567 current.variant_parts.emplace_back ();
15568 new_part = &current.variant_parts.back ();
15569 }
15570
15571 /* When we recurse, we want callees to add to this new variant
15572 part. */
15573 scoped_restore save_current_variant_part
15574 = make_scoped_restore (&fi->current_variant_part, new_part);
15575
15576 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15577 if (discr == NULL)
15578 {
15579 /* It's a univariant form, an extension we support. */
15580 }
15581 else if (discr->form_is_ref ())
15582 {
15583 struct dwarf2_cu *target_cu = cu;
15584 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15585
15586 new_part->discriminant_offset = target_die->sect_off;
15587 }
15588 else
15589 {
15590 complaint (_("DW_AT_discr does not have DIE reference form"
15591 " - DIE at %s [in module %s]"),
15592 sect_offset_str (die->sect_off),
15593 objfile_name (cu->per_objfile->objfile));
15594 }
15595
15596 for (die_info *child_die = die->child;
15597 child_die != NULL;
15598 child_die = child_die->sibling)
15599 handle_struct_member_die (child_die, type, fi, template_args, cu);
15600 }
15601
15602 /* A helper for handle_struct_member_die that handles
15603 DW_TAG_variant. */
15604
15605 static void
15606 handle_variant (struct die_info *die, struct type *type,
15607 struct field_info *fi,
15608 std::vector<struct symbol *> *template_args,
15609 struct dwarf2_cu *cu)
15610 {
15611 if (fi->current_variant_part == nullptr)
15612 {
15613 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15614 "- DIE at %s [in module %s]"),
15615 sect_offset_str (die->sect_off),
15616 objfile_name (cu->per_objfile->objfile));
15617 return;
15618 }
15619 if (fi->current_variant_part->processing_variant)
15620 {
15621 complaint (_("nested DW_TAG_variant seen "
15622 "- DIE at %s [in module %s]"),
15623 sect_offset_str (die->sect_off),
15624 objfile_name (cu->per_objfile->objfile));
15625 return;
15626 }
15627
15628 scoped_restore save_processing_variant
15629 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15630 true);
15631
15632 fi->current_variant_part->variants.emplace_back ();
15633 variant_field &variant = fi->current_variant_part->variants.back ();
15634 variant.first_field = fi->fields.size ();
15635
15636 /* In a variant we want to get the discriminant and also add a
15637 field for our sole member child. */
15638 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15639 if (discr == nullptr)
15640 {
15641 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15642 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15643 variant.default_branch = true;
15644 else
15645 variant.discr_list_data = DW_BLOCK (discr);
15646 }
15647 else
15648 variant.discriminant_value = DW_UNSND (discr);
15649
15650 for (die_info *variant_child = die->child;
15651 variant_child != NULL;
15652 variant_child = variant_child->sibling)
15653 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15654
15655 variant.last_field = fi->fields.size ();
15656 }
15657
15658 /* A helper for process_structure_scope that handles a single member
15659 DIE. */
15660
15661 static void
15662 handle_struct_member_die (struct die_info *child_die, struct type *type,
15663 struct field_info *fi,
15664 std::vector<struct symbol *> *template_args,
15665 struct dwarf2_cu *cu)
15666 {
15667 if (child_die->tag == DW_TAG_member
15668 || child_die->tag == DW_TAG_variable)
15669 {
15670 /* NOTE: carlton/2002-11-05: A C++ static data member
15671 should be a DW_TAG_member that is a declaration, but
15672 all versions of G++ as of this writing (so through at
15673 least 3.2.1) incorrectly generate DW_TAG_variable
15674 tags for them instead. */
15675 dwarf2_add_field (fi, child_die, cu);
15676 }
15677 else if (child_die->tag == DW_TAG_subprogram)
15678 {
15679 /* Rust doesn't have member functions in the C++ sense.
15680 However, it does emit ordinary functions as children
15681 of a struct DIE. */
15682 if (cu->language == language_rust)
15683 read_func_scope (child_die, cu);
15684 else
15685 {
15686 /* C++ member function. */
15687 dwarf2_add_member_fn (fi, child_die, type, cu);
15688 }
15689 }
15690 else if (child_die->tag == DW_TAG_inheritance)
15691 {
15692 /* C++ base class field. */
15693 dwarf2_add_field (fi, child_die, cu);
15694 }
15695 else if (type_can_define_types (child_die))
15696 dwarf2_add_type_defn (fi, child_die, cu);
15697 else if (child_die->tag == DW_TAG_template_type_param
15698 || child_die->tag == DW_TAG_template_value_param)
15699 {
15700 struct symbol *arg = new_symbol (child_die, NULL, cu);
15701
15702 if (arg != NULL)
15703 template_args->push_back (arg);
15704 }
15705 else if (child_die->tag == DW_TAG_variant_part)
15706 handle_variant_part (child_die, type, fi, template_args, cu);
15707 else if (child_die->tag == DW_TAG_variant)
15708 handle_variant (child_die, type, fi, template_args, cu);
15709 }
15710
15711 /* Finish creating a structure or union type, including filling in
15712 its members and creating a symbol for it. */
15713
15714 static void
15715 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15716 {
15717 struct objfile *objfile = cu->per_objfile->objfile;
15718 struct die_info *child_die;
15719 struct type *type;
15720
15721 type = get_die_type (die, cu);
15722 if (type == NULL)
15723 type = read_structure_type (die, cu);
15724
15725 bool has_template_parameters = false;
15726 if (die->child != NULL && ! die_is_declaration (die, cu))
15727 {
15728 struct field_info fi;
15729 std::vector<struct symbol *> template_args;
15730
15731 child_die = die->child;
15732
15733 while (child_die && child_die->tag)
15734 {
15735 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15736 child_die = child_die->sibling;
15737 }
15738
15739 /* Attach template arguments to type. */
15740 if (!template_args.empty ())
15741 {
15742 has_template_parameters = true;
15743 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15744 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15745 TYPE_TEMPLATE_ARGUMENTS (type)
15746 = XOBNEWVEC (&objfile->objfile_obstack,
15747 struct symbol *,
15748 TYPE_N_TEMPLATE_ARGUMENTS (type));
15749 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15750 template_args.data (),
15751 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15752 * sizeof (struct symbol *)));
15753 }
15754
15755 /* Attach fields and member functions to the type. */
15756 if (fi.nfields () > 0)
15757 dwarf2_attach_fields_to_type (&fi, type, cu);
15758 if (!fi.fnfieldlists.empty ())
15759 {
15760 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15761
15762 /* Get the type which refers to the base class (possibly this
15763 class itself) which contains the vtable pointer for the current
15764 class from the DW_AT_containing_type attribute. This use of
15765 DW_AT_containing_type is a GNU extension. */
15766
15767 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15768 {
15769 struct type *t = die_containing_type (die, cu);
15770
15771 set_type_vptr_basetype (type, t);
15772 if (type == t)
15773 {
15774 int i;
15775
15776 /* Our own class provides vtbl ptr. */
15777 for (i = t->num_fields () - 1;
15778 i >= TYPE_N_BASECLASSES (t);
15779 --i)
15780 {
15781 const char *fieldname = TYPE_FIELD_NAME (t, i);
15782
15783 if (is_vtable_name (fieldname, cu))
15784 {
15785 set_type_vptr_fieldno (type, i);
15786 break;
15787 }
15788 }
15789
15790 /* Complain if virtual function table field not found. */
15791 if (i < TYPE_N_BASECLASSES (t))
15792 complaint (_("virtual function table pointer "
15793 "not found when defining class '%s'"),
15794 type->name () ? type->name () : "");
15795 }
15796 else
15797 {
15798 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15799 }
15800 }
15801 else if (cu->producer
15802 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15803 {
15804 /* The IBM XLC compiler does not provide direct indication
15805 of the containing type, but the vtable pointer is
15806 always named __vfp. */
15807
15808 int i;
15809
15810 for (i = type->num_fields () - 1;
15811 i >= TYPE_N_BASECLASSES (type);
15812 --i)
15813 {
15814 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15815 {
15816 set_type_vptr_fieldno (type, i);
15817 set_type_vptr_basetype (type, type);
15818 break;
15819 }
15820 }
15821 }
15822 }
15823
15824 /* Copy fi.typedef_field_list linked list elements content into the
15825 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15826 if (!fi.typedef_field_list.empty ())
15827 {
15828 int count = fi.typedef_field_list.size ();
15829
15830 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15831 TYPE_TYPEDEF_FIELD_ARRAY (type)
15832 = ((struct decl_field *)
15833 TYPE_ALLOC (type,
15834 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15835 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15836
15837 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15838 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15839 }
15840
15841 /* Copy fi.nested_types_list linked list elements content into the
15842 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15843 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15844 {
15845 int count = fi.nested_types_list.size ();
15846
15847 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15848 TYPE_NESTED_TYPES_ARRAY (type)
15849 = ((struct decl_field *)
15850 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15851 TYPE_NESTED_TYPES_COUNT (type) = count;
15852
15853 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15854 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15855 }
15856 }
15857
15858 quirk_gcc_member_function_pointer (type, objfile);
15859 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15860 cu->rust_unions.push_back (type);
15861
15862 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15863 snapshots) has been known to create a die giving a declaration
15864 for a class that has, as a child, a die giving a definition for a
15865 nested class. So we have to process our children even if the
15866 current die is a declaration. Normally, of course, a declaration
15867 won't have any children at all. */
15868
15869 child_die = die->child;
15870
15871 while (child_die != NULL && child_die->tag)
15872 {
15873 if (child_die->tag == DW_TAG_member
15874 || child_die->tag == DW_TAG_variable
15875 || child_die->tag == DW_TAG_inheritance
15876 || child_die->tag == DW_TAG_template_value_param
15877 || child_die->tag == DW_TAG_template_type_param)
15878 {
15879 /* Do nothing. */
15880 }
15881 else
15882 process_die (child_die, cu);
15883
15884 child_die = child_die->sibling;
15885 }
15886
15887 /* Do not consider external references. According to the DWARF standard,
15888 these DIEs are identified by the fact that they have no byte_size
15889 attribute, and a declaration attribute. */
15890 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15891 || !die_is_declaration (die, cu)
15892 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15893 {
15894 struct symbol *sym = new_symbol (die, type, cu);
15895
15896 if (has_template_parameters)
15897 {
15898 struct symtab *symtab;
15899 if (sym != nullptr)
15900 symtab = symbol_symtab (sym);
15901 else if (cu->line_header != nullptr)
15902 {
15903 /* Any related symtab will do. */
15904 symtab
15905 = cu->line_header->file_names ()[0].symtab;
15906 }
15907 else
15908 {
15909 symtab = nullptr;
15910 complaint (_("could not find suitable "
15911 "symtab for template parameter"
15912 " - DIE at %s [in module %s]"),
15913 sect_offset_str (die->sect_off),
15914 objfile_name (objfile));
15915 }
15916
15917 if (symtab != nullptr)
15918 {
15919 /* Make sure that the symtab is set on the new symbols.
15920 Even though they don't appear in this symtab directly,
15921 other parts of gdb assume that symbols do, and this is
15922 reasonably true. */
15923 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15924 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15925 }
15926 }
15927 }
15928 }
15929
15930 /* Assuming DIE is an enumeration type, and TYPE is its associated
15931 type, update TYPE using some information only available in DIE's
15932 children. In particular, the fields are computed. */
15933
15934 static void
15935 update_enumeration_type_from_children (struct die_info *die,
15936 struct type *type,
15937 struct dwarf2_cu *cu)
15938 {
15939 struct die_info *child_die;
15940 int unsigned_enum = 1;
15941 int flag_enum = 1;
15942
15943 auto_obstack obstack;
15944 std::vector<struct field> fields;
15945
15946 for (child_die = die->child;
15947 child_die != NULL && child_die->tag;
15948 child_die = child_die->sibling)
15949 {
15950 struct attribute *attr;
15951 LONGEST value;
15952 const gdb_byte *bytes;
15953 struct dwarf2_locexpr_baton *baton;
15954 const char *name;
15955
15956 if (child_die->tag != DW_TAG_enumerator)
15957 continue;
15958
15959 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15960 if (attr == NULL)
15961 continue;
15962
15963 name = dwarf2_name (child_die, cu);
15964 if (name == NULL)
15965 name = "<anonymous enumerator>";
15966
15967 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15968 &value, &bytes, &baton);
15969 if (value < 0)
15970 {
15971 unsigned_enum = 0;
15972 flag_enum = 0;
15973 }
15974 else
15975 {
15976 if (count_one_bits_ll (value) >= 2)
15977 flag_enum = 0;
15978 }
15979
15980 fields.emplace_back ();
15981 struct field &field = fields.back ();
15982 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
15983 SET_FIELD_ENUMVAL (field, value);
15984 }
15985
15986 if (!fields.empty ())
15987 {
15988 type->set_num_fields (fields.size ());
15989 type->set_fields
15990 ((struct field *)
15991 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
15992 memcpy (type->fields (), fields.data (),
15993 sizeof (struct field) * fields.size ());
15994 }
15995
15996 if (unsigned_enum)
15997 TYPE_UNSIGNED (type) = 1;
15998 if (flag_enum)
15999 TYPE_FLAG_ENUM (type) = 1;
16000 }
16001
16002 /* Given a DW_AT_enumeration_type die, set its type. We do not
16003 complete the type's fields yet, or create any symbols. */
16004
16005 static struct type *
16006 read_enumeration_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
16028 type->set_code (TYPE_CODE_ENUM);
16029 name = dwarf2_full_name (NULL, die, cu);
16030 if (name != NULL)
16031 type->set_name (name);
16032
16033 attr = dwarf2_attr (die, DW_AT_type, cu);
16034 if (attr != NULL)
16035 {
16036 struct type *underlying_type = die_type (die, cu);
16037
16038 TYPE_TARGET_TYPE (type) = underlying_type;
16039 }
16040
16041 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16042 if (attr != nullptr)
16043 {
16044 TYPE_LENGTH (type) = DW_UNSND (attr);
16045 }
16046 else
16047 {
16048 TYPE_LENGTH (type) = 0;
16049 }
16050
16051 maybe_set_alignment (cu, die, type);
16052
16053 /* The enumeration DIE can be incomplete. In Ada, any type can be
16054 declared as private in the package spec, and then defined only
16055 inside the package body. Such types are known as Taft Amendment
16056 Types. When another package uses such a type, an incomplete DIE
16057 may be generated by the compiler. */
16058 if (die_is_declaration (die, cu))
16059 TYPE_STUB (type) = 1;
16060
16061 /* If this type has an underlying type that is not a stub, then we
16062 may use its attributes. We always use the "unsigned" attribute
16063 in this situation, because ordinarily we guess whether the type
16064 is unsigned -- but the guess can be wrong and the underlying type
16065 can tell us the reality. However, we defer to a local size
16066 attribute if one exists, because this lets the compiler override
16067 the underlying type if needed. */
16068 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16069 {
16070 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16071 underlying_type = check_typedef (underlying_type);
16072 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
16073 if (TYPE_LENGTH (type) == 0)
16074 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16075 if (TYPE_RAW_ALIGN (type) == 0
16076 && TYPE_RAW_ALIGN (underlying_type) != 0)
16077 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16078 }
16079
16080 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16081
16082 set_die_type (die, type, cu);
16083
16084 /* Finish the creation of this type by using the enum's children.
16085 Note that, as usual, this must come after set_die_type to avoid
16086 infinite recursion when trying to compute the names of the
16087 enumerators. */
16088 update_enumeration_type_from_children (die, type, cu);
16089
16090 return type;
16091 }
16092
16093 /* Given a pointer to a die which begins an enumeration, process all
16094 the dies that define the members of the enumeration, and create the
16095 symbol for the enumeration type.
16096
16097 NOTE: We reverse the order of the element list. */
16098
16099 static void
16100 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16101 {
16102 struct type *this_type;
16103
16104 this_type = get_die_type (die, cu);
16105 if (this_type == NULL)
16106 this_type = read_enumeration_type (die, cu);
16107
16108 if (die->child != NULL)
16109 {
16110 struct die_info *child_die;
16111 const char *name;
16112
16113 child_die = die->child;
16114 while (child_die && child_die->tag)
16115 {
16116 if (child_die->tag != DW_TAG_enumerator)
16117 {
16118 process_die (child_die, cu);
16119 }
16120 else
16121 {
16122 name = dwarf2_name (child_die, cu);
16123 if (name)
16124 new_symbol (child_die, this_type, cu);
16125 }
16126
16127 child_die = child_die->sibling;
16128 }
16129 }
16130
16131 /* If we are reading an enum from a .debug_types unit, and the enum
16132 is a declaration, and the enum is not the signatured type in the
16133 unit, then we do not want to add a symbol for it. Adding a
16134 symbol would in some cases obscure the true definition of the
16135 enum, giving users an incomplete type when the definition is
16136 actually available. Note that we do not want to do this for all
16137 enums which are just declarations, because C++0x allows forward
16138 enum declarations. */
16139 if (cu->per_cu->is_debug_types
16140 && die_is_declaration (die, cu))
16141 {
16142 struct signatured_type *sig_type;
16143
16144 sig_type = (struct signatured_type *) cu->per_cu;
16145 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16146 if (sig_type->type_offset_in_section != die->sect_off)
16147 return;
16148 }
16149
16150 new_symbol (die, this_type, cu);
16151 }
16152
16153 /* Extract all information from a DW_TAG_array_type DIE and put it in
16154 the DIE's type field. For now, this only handles one dimensional
16155 arrays. */
16156
16157 static struct type *
16158 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16159 {
16160 struct objfile *objfile = cu->per_objfile->objfile;
16161 struct die_info *child_die;
16162 struct type *type;
16163 struct type *element_type, *range_type, *index_type;
16164 struct attribute *attr;
16165 const char *name;
16166 struct dynamic_prop *byte_stride_prop = NULL;
16167 unsigned int bit_stride = 0;
16168
16169 element_type = die_type (die, cu);
16170
16171 /* The die_type call above may have already set the type for this DIE. */
16172 type = get_die_type (die, cu);
16173 if (type)
16174 return type;
16175
16176 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16177 if (attr != NULL)
16178 {
16179 int stride_ok;
16180 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
16181
16182 byte_stride_prop
16183 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16184 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16185 prop_type);
16186 if (!stride_ok)
16187 {
16188 complaint (_("unable to read array DW_AT_byte_stride "
16189 " - DIE at %s [in module %s]"),
16190 sect_offset_str (die->sect_off),
16191 objfile_name (cu->per_objfile->objfile));
16192 /* Ignore this attribute. We will likely not be able to print
16193 arrays of this type correctly, but there is little we can do
16194 to help if we cannot read the attribute's value. */
16195 byte_stride_prop = NULL;
16196 }
16197 }
16198
16199 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16200 if (attr != NULL)
16201 bit_stride = DW_UNSND (attr);
16202
16203 /* Irix 6.2 native cc creates array types without children for
16204 arrays with unspecified length. */
16205 if (die->child == NULL)
16206 {
16207 index_type = objfile_type (objfile)->builtin_int;
16208 range_type = create_static_range_type (NULL, index_type, 0, -1);
16209 type = create_array_type_with_stride (NULL, element_type, range_type,
16210 byte_stride_prop, bit_stride);
16211 return set_die_type (die, type, cu);
16212 }
16213
16214 std::vector<struct type *> range_types;
16215 child_die = die->child;
16216 while (child_die && child_die->tag)
16217 {
16218 if (child_die->tag == DW_TAG_subrange_type)
16219 {
16220 struct type *child_type = read_type_die (child_die, cu);
16221
16222 if (child_type != NULL)
16223 {
16224 /* The range type was succesfully read. Save it for the
16225 array type creation. */
16226 range_types.push_back (child_type);
16227 }
16228 }
16229 child_die = child_die->sibling;
16230 }
16231
16232 /* Dwarf2 dimensions are output from left to right, create the
16233 necessary array types in backwards order. */
16234
16235 type = element_type;
16236
16237 if (read_array_order (die, cu) == DW_ORD_col_major)
16238 {
16239 int i = 0;
16240
16241 while (i < range_types.size ())
16242 type = create_array_type_with_stride (NULL, type, range_types[i++],
16243 byte_stride_prop, bit_stride);
16244 }
16245 else
16246 {
16247 size_t ndim = range_types.size ();
16248 while (ndim-- > 0)
16249 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16250 byte_stride_prop, bit_stride);
16251 }
16252
16253 /* Understand Dwarf2 support for vector types (like they occur on
16254 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16255 array type. This is not part of the Dwarf2/3 standard yet, but a
16256 custom vendor extension. The main difference between a regular
16257 array and the vector variant is that vectors are passed by value
16258 to functions. */
16259 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16260 if (attr != nullptr)
16261 make_vector_type (type);
16262
16263 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16264 implementation may choose to implement triple vectors using this
16265 attribute. */
16266 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16267 if (attr != nullptr)
16268 {
16269 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16270 TYPE_LENGTH (type) = DW_UNSND (attr);
16271 else
16272 complaint (_("DW_AT_byte_size for array type smaller "
16273 "than the total size of elements"));
16274 }
16275
16276 name = dwarf2_name (die, cu);
16277 if (name)
16278 type->set_name (name);
16279
16280 maybe_set_alignment (cu, die, type);
16281
16282 /* Install the type in the die. */
16283 set_die_type (die, type, cu);
16284
16285 /* set_die_type should be already done. */
16286 set_descriptive_type (type, die, cu);
16287
16288 return type;
16289 }
16290
16291 static enum dwarf_array_dim_ordering
16292 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16293 {
16294 struct attribute *attr;
16295
16296 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16297
16298 if (attr != nullptr)
16299 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16300
16301 /* GNU F77 is a special case, as at 08/2004 array type info is the
16302 opposite order to the dwarf2 specification, but data is still
16303 laid out as per normal fortran.
16304
16305 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16306 version checking. */
16307
16308 if (cu->language == language_fortran
16309 && cu->producer && strstr (cu->producer, "GNU F77"))
16310 {
16311 return DW_ORD_row_major;
16312 }
16313
16314 switch (cu->language_defn->la_array_ordering)
16315 {
16316 case array_column_major:
16317 return DW_ORD_col_major;
16318 case array_row_major:
16319 default:
16320 return DW_ORD_row_major;
16321 };
16322 }
16323
16324 /* Extract all information from a DW_TAG_set_type DIE and put it in
16325 the DIE's type field. */
16326
16327 static struct type *
16328 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16329 {
16330 struct type *domain_type, *set_type;
16331 struct attribute *attr;
16332
16333 domain_type = die_type (die, cu);
16334
16335 /* The die_type call above may have already set the type for this DIE. */
16336 set_type = get_die_type (die, cu);
16337 if (set_type)
16338 return set_type;
16339
16340 set_type = create_set_type (NULL, domain_type);
16341
16342 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16343 if (attr != nullptr)
16344 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16345
16346 maybe_set_alignment (cu, die, set_type);
16347
16348 return set_die_type (die, set_type, cu);
16349 }
16350
16351 /* A helper for read_common_block that creates a locexpr baton.
16352 SYM is the symbol which we are marking as computed.
16353 COMMON_DIE is the DIE for the common block.
16354 COMMON_LOC is the location expression attribute for the common
16355 block itself.
16356 MEMBER_LOC is the location expression attribute for the particular
16357 member of the common block that we are processing.
16358 CU is the CU from which the above come. */
16359
16360 static void
16361 mark_common_block_symbol_computed (struct symbol *sym,
16362 struct die_info *common_die,
16363 struct attribute *common_loc,
16364 struct attribute *member_loc,
16365 struct dwarf2_cu *cu)
16366 {
16367 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16368 struct objfile *objfile = per_objfile->objfile;
16369 struct dwarf2_locexpr_baton *baton;
16370 gdb_byte *ptr;
16371 unsigned int cu_off;
16372 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16373 LONGEST offset = 0;
16374
16375 gdb_assert (common_loc && member_loc);
16376 gdb_assert (common_loc->form_is_block ());
16377 gdb_assert (member_loc->form_is_block ()
16378 || member_loc->form_is_constant ());
16379
16380 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16381 baton->per_objfile = per_objfile;
16382 baton->per_cu = cu->per_cu;
16383 gdb_assert (baton->per_cu);
16384
16385 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16386
16387 if (member_loc->form_is_constant ())
16388 {
16389 offset = member_loc->constant_value (0);
16390 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16391 }
16392 else
16393 baton->size += DW_BLOCK (member_loc)->size;
16394
16395 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16396 baton->data = ptr;
16397
16398 *ptr++ = DW_OP_call4;
16399 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16400 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16401 ptr += 4;
16402
16403 if (member_loc->form_is_constant ())
16404 {
16405 *ptr++ = DW_OP_addr;
16406 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16407 ptr += cu->header.addr_size;
16408 }
16409 else
16410 {
16411 /* We have to copy the data here, because DW_OP_call4 will only
16412 use a DW_AT_location attribute. */
16413 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16414 ptr += DW_BLOCK (member_loc)->size;
16415 }
16416
16417 *ptr++ = DW_OP_plus;
16418 gdb_assert (ptr - baton->data == baton->size);
16419
16420 SYMBOL_LOCATION_BATON (sym) = baton;
16421 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16422 }
16423
16424 /* Create appropriate locally-scoped variables for all the
16425 DW_TAG_common_block entries. Also create a struct common_block
16426 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16427 is used to separate the common blocks name namespace from regular
16428 variable names. */
16429
16430 static void
16431 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16432 {
16433 struct attribute *attr;
16434
16435 attr = dwarf2_attr (die, DW_AT_location, cu);
16436 if (attr != nullptr)
16437 {
16438 /* Support the .debug_loc offsets. */
16439 if (attr->form_is_block ())
16440 {
16441 /* Ok. */
16442 }
16443 else if (attr->form_is_section_offset ())
16444 {
16445 dwarf2_complex_location_expr_complaint ();
16446 attr = NULL;
16447 }
16448 else
16449 {
16450 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16451 "common block member");
16452 attr = NULL;
16453 }
16454 }
16455
16456 if (die->child != NULL)
16457 {
16458 struct objfile *objfile = cu->per_objfile->objfile;
16459 struct die_info *child_die;
16460 size_t n_entries = 0, size;
16461 struct common_block *common_block;
16462 struct symbol *sym;
16463
16464 for (child_die = die->child;
16465 child_die && child_die->tag;
16466 child_die = child_die->sibling)
16467 ++n_entries;
16468
16469 size = (sizeof (struct common_block)
16470 + (n_entries - 1) * sizeof (struct symbol *));
16471 common_block
16472 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16473 size);
16474 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16475 common_block->n_entries = 0;
16476
16477 for (child_die = die->child;
16478 child_die && child_die->tag;
16479 child_die = child_die->sibling)
16480 {
16481 /* Create the symbol in the DW_TAG_common_block block in the current
16482 symbol scope. */
16483 sym = new_symbol (child_die, NULL, cu);
16484 if (sym != NULL)
16485 {
16486 struct attribute *member_loc;
16487
16488 common_block->contents[common_block->n_entries++] = sym;
16489
16490 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16491 cu);
16492 if (member_loc)
16493 {
16494 /* GDB has handled this for a long time, but it is
16495 not specified by DWARF. It seems to have been
16496 emitted by gfortran at least as recently as:
16497 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16498 complaint (_("Variable in common block has "
16499 "DW_AT_data_member_location "
16500 "- DIE at %s [in module %s]"),
16501 sect_offset_str (child_die->sect_off),
16502 objfile_name (objfile));
16503
16504 if (member_loc->form_is_section_offset ())
16505 dwarf2_complex_location_expr_complaint ();
16506 else if (member_loc->form_is_constant ()
16507 || member_loc->form_is_block ())
16508 {
16509 if (attr != nullptr)
16510 mark_common_block_symbol_computed (sym, die, attr,
16511 member_loc, cu);
16512 }
16513 else
16514 dwarf2_complex_location_expr_complaint ();
16515 }
16516 }
16517 }
16518
16519 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16520 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16521 }
16522 }
16523
16524 /* Create a type for a C++ namespace. */
16525
16526 static struct type *
16527 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16528 {
16529 struct objfile *objfile = cu->per_objfile->objfile;
16530 const char *previous_prefix, *name;
16531 int is_anonymous;
16532 struct type *type;
16533
16534 /* For extensions, reuse the type of the original namespace. */
16535 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16536 {
16537 struct die_info *ext_die;
16538 struct dwarf2_cu *ext_cu = cu;
16539
16540 ext_die = dwarf2_extension (die, &ext_cu);
16541 type = read_type_die (ext_die, ext_cu);
16542
16543 /* EXT_CU may not be the same as CU.
16544 Ensure TYPE is recorded with CU in die_type_hash. */
16545 return set_die_type (die, type, cu);
16546 }
16547
16548 name = namespace_name (die, &is_anonymous, cu);
16549
16550 /* Now build the name of the current namespace. */
16551
16552 previous_prefix = determine_prefix (die, cu);
16553 if (previous_prefix[0] != '\0')
16554 name = typename_concat (&objfile->objfile_obstack,
16555 previous_prefix, name, 0, cu);
16556
16557 /* Create the type. */
16558 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16559
16560 return set_die_type (die, type, cu);
16561 }
16562
16563 /* Read a namespace scope. */
16564
16565 static void
16566 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16567 {
16568 struct objfile *objfile = cu->per_objfile->objfile;
16569 int is_anonymous;
16570
16571 /* Add a symbol associated to this if we haven't seen the namespace
16572 before. Also, add a using directive if it's an anonymous
16573 namespace. */
16574
16575 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16576 {
16577 struct type *type;
16578
16579 type = read_type_die (die, cu);
16580 new_symbol (die, type, cu);
16581
16582 namespace_name (die, &is_anonymous, cu);
16583 if (is_anonymous)
16584 {
16585 const char *previous_prefix = determine_prefix (die, cu);
16586
16587 std::vector<const char *> excludes;
16588 add_using_directive (using_directives (cu),
16589 previous_prefix, type->name (), NULL,
16590 NULL, excludes, 0, &objfile->objfile_obstack);
16591 }
16592 }
16593
16594 if (die->child != NULL)
16595 {
16596 struct die_info *child_die = die->child;
16597
16598 while (child_die && child_die->tag)
16599 {
16600 process_die (child_die, cu);
16601 child_die = child_die->sibling;
16602 }
16603 }
16604 }
16605
16606 /* Read a Fortran module as type. This DIE can be only a declaration used for
16607 imported module. Still we need that type as local Fortran "use ... only"
16608 declaration imports depend on the created type in determine_prefix. */
16609
16610 static struct type *
16611 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16612 {
16613 struct objfile *objfile = cu->per_objfile->objfile;
16614 const char *module_name;
16615 struct type *type;
16616
16617 module_name = dwarf2_name (die, cu);
16618 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16619
16620 return set_die_type (die, type, cu);
16621 }
16622
16623 /* Read a Fortran module. */
16624
16625 static void
16626 read_module (struct die_info *die, struct dwarf2_cu *cu)
16627 {
16628 struct die_info *child_die = die->child;
16629 struct type *type;
16630
16631 type = read_type_die (die, cu);
16632 new_symbol (die, type, cu);
16633
16634 while (child_die && child_die->tag)
16635 {
16636 process_die (child_die, cu);
16637 child_die = child_die->sibling;
16638 }
16639 }
16640
16641 /* Return the name of the namespace represented by DIE. Set
16642 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16643 namespace. */
16644
16645 static const char *
16646 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16647 {
16648 struct die_info *current_die;
16649 const char *name = NULL;
16650
16651 /* Loop through the extensions until we find a name. */
16652
16653 for (current_die = die;
16654 current_die != NULL;
16655 current_die = dwarf2_extension (die, &cu))
16656 {
16657 /* We don't use dwarf2_name here so that we can detect the absence
16658 of a name -> anonymous namespace. */
16659 name = dwarf2_string_attr (die, DW_AT_name, cu);
16660
16661 if (name != NULL)
16662 break;
16663 }
16664
16665 /* Is it an anonymous namespace? */
16666
16667 *is_anonymous = (name == NULL);
16668 if (*is_anonymous)
16669 name = CP_ANONYMOUS_NAMESPACE_STR;
16670
16671 return name;
16672 }
16673
16674 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16675 the user defined type vector. */
16676
16677 static struct type *
16678 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16679 {
16680 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16681 struct comp_unit_head *cu_header = &cu->header;
16682 struct type *type;
16683 struct attribute *attr_byte_size;
16684 struct attribute *attr_address_class;
16685 int byte_size, addr_class;
16686 struct type *target_type;
16687
16688 target_type = die_type (die, cu);
16689
16690 /* The die_type call above may have already set the type for this DIE. */
16691 type = get_die_type (die, cu);
16692 if (type)
16693 return type;
16694
16695 type = lookup_pointer_type (target_type);
16696
16697 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16698 if (attr_byte_size)
16699 byte_size = DW_UNSND (attr_byte_size);
16700 else
16701 byte_size = cu_header->addr_size;
16702
16703 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16704 if (attr_address_class)
16705 addr_class = DW_UNSND (attr_address_class);
16706 else
16707 addr_class = DW_ADDR_none;
16708
16709 ULONGEST alignment = get_alignment (cu, die);
16710
16711 /* If the pointer size, alignment, or address class is different
16712 than the default, create a type variant marked as such and set
16713 the length accordingly. */
16714 if (TYPE_LENGTH (type) != byte_size
16715 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16716 && alignment != TYPE_RAW_ALIGN (type))
16717 || addr_class != DW_ADDR_none)
16718 {
16719 if (gdbarch_address_class_type_flags_p (gdbarch))
16720 {
16721 int type_flags;
16722
16723 type_flags = gdbarch_address_class_type_flags
16724 (gdbarch, byte_size, addr_class);
16725 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16726 == 0);
16727 type = make_type_with_address_space (type, type_flags);
16728 }
16729 else if (TYPE_LENGTH (type) != byte_size)
16730 {
16731 complaint (_("invalid pointer size %d"), byte_size);
16732 }
16733 else if (TYPE_RAW_ALIGN (type) != alignment)
16734 {
16735 complaint (_("Invalid DW_AT_alignment"
16736 " - DIE at %s [in module %s]"),
16737 sect_offset_str (die->sect_off),
16738 objfile_name (cu->per_objfile->objfile));
16739 }
16740 else
16741 {
16742 /* Should we also complain about unhandled address classes? */
16743 }
16744 }
16745
16746 TYPE_LENGTH (type) = byte_size;
16747 set_type_align (type, alignment);
16748 return set_die_type (die, type, cu);
16749 }
16750
16751 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16752 the user defined type vector. */
16753
16754 static struct type *
16755 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16756 {
16757 struct type *type;
16758 struct type *to_type;
16759 struct type *domain;
16760
16761 to_type = die_type (die, cu);
16762 domain = die_containing_type (die, cu);
16763
16764 /* The calls above may have already set the type for this DIE. */
16765 type = get_die_type (die, cu);
16766 if (type)
16767 return type;
16768
16769 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16770 type = lookup_methodptr_type (to_type);
16771 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16772 {
16773 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16774
16775 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16776 to_type->fields (), to_type->num_fields (),
16777 TYPE_VARARGS (to_type));
16778 type = lookup_methodptr_type (new_type);
16779 }
16780 else
16781 type = lookup_memberptr_type (to_type, domain);
16782
16783 return set_die_type (die, type, cu);
16784 }
16785
16786 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16787 the user defined type vector. */
16788
16789 static struct type *
16790 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16791 enum type_code refcode)
16792 {
16793 struct comp_unit_head *cu_header = &cu->header;
16794 struct type *type, *target_type;
16795 struct attribute *attr;
16796
16797 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16798
16799 target_type = die_type (die, cu);
16800
16801 /* The die_type call above may have already set the type for this DIE. */
16802 type = get_die_type (die, cu);
16803 if (type)
16804 return type;
16805
16806 type = lookup_reference_type (target_type, refcode);
16807 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16808 if (attr != nullptr)
16809 {
16810 TYPE_LENGTH (type) = DW_UNSND (attr);
16811 }
16812 else
16813 {
16814 TYPE_LENGTH (type) = cu_header->addr_size;
16815 }
16816 maybe_set_alignment (cu, die, type);
16817 return set_die_type (die, type, cu);
16818 }
16819
16820 /* Add the given cv-qualifiers to the element type of the array. GCC
16821 outputs DWARF type qualifiers that apply to an array, not the
16822 element type. But GDB relies on the array element type to carry
16823 the cv-qualifiers. This mimics section 6.7.3 of the C99
16824 specification. */
16825
16826 static struct type *
16827 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16828 struct type *base_type, int cnst, int voltl)
16829 {
16830 struct type *el_type, *inner_array;
16831
16832 base_type = copy_type (base_type);
16833 inner_array = base_type;
16834
16835 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16836 {
16837 TYPE_TARGET_TYPE (inner_array) =
16838 copy_type (TYPE_TARGET_TYPE (inner_array));
16839 inner_array = TYPE_TARGET_TYPE (inner_array);
16840 }
16841
16842 el_type = TYPE_TARGET_TYPE (inner_array);
16843 cnst |= TYPE_CONST (el_type);
16844 voltl |= TYPE_VOLATILE (el_type);
16845 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16846
16847 return set_die_type (die, base_type, cu);
16848 }
16849
16850 static struct type *
16851 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16852 {
16853 struct type *base_type, *cv_type;
16854
16855 base_type = die_type (die, cu);
16856
16857 /* The die_type call above may have already set the type for this DIE. */
16858 cv_type = get_die_type (die, cu);
16859 if (cv_type)
16860 return cv_type;
16861
16862 /* In case the const qualifier is applied to an array type, the element type
16863 is so qualified, not the array type (section 6.7.3 of C99). */
16864 if (base_type->code () == TYPE_CODE_ARRAY)
16865 return add_array_cv_type (die, cu, base_type, 1, 0);
16866
16867 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16868 return set_die_type (die, cv_type, cu);
16869 }
16870
16871 static struct type *
16872 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16873 {
16874 struct type *base_type, *cv_type;
16875
16876 base_type = die_type (die, cu);
16877
16878 /* The die_type call above may have already set the type for this DIE. */
16879 cv_type = get_die_type (die, cu);
16880 if (cv_type)
16881 return cv_type;
16882
16883 /* In case the volatile qualifier is applied to an array type, the
16884 element type is so qualified, not the array type (section 6.7.3
16885 of C99). */
16886 if (base_type->code () == TYPE_CODE_ARRAY)
16887 return add_array_cv_type (die, cu, base_type, 0, 1);
16888
16889 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16890 return set_die_type (die, cv_type, cu);
16891 }
16892
16893 /* Handle DW_TAG_restrict_type. */
16894
16895 static struct type *
16896 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16897 {
16898 struct type *base_type, *cv_type;
16899
16900 base_type = die_type (die, cu);
16901
16902 /* The die_type call above may have already set the type for this DIE. */
16903 cv_type = get_die_type (die, cu);
16904 if (cv_type)
16905 return cv_type;
16906
16907 cv_type = make_restrict_type (base_type);
16908 return set_die_type (die, cv_type, cu);
16909 }
16910
16911 /* Handle DW_TAG_atomic_type. */
16912
16913 static struct type *
16914 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16915 {
16916 struct type *base_type, *cv_type;
16917
16918 base_type = die_type (die, cu);
16919
16920 /* The die_type call above may have already set the type for this DIE. */
16921 cv_type = get_die_type (die, cu);
16922 if (cv_type)
16923 return cv_type;
16924
16925 cv_type = make_atomic_type (base_type);
16926 return set_die_type (die, cv_type, cu);
16927 }
16928
16929 /* Extract all information from a DW_TAG_string_type DIE and add to
16930 the user defined type vector. It isn't really a user defined type,
16931 but it behaves like one, with other DIE's using an AT_user_def_type
16932 attribute to reference it. */
16933
16934 static struct type *
16935 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16936 {
16937 struct objfile *objfile = cu->per_objfile->objfile;
16938 struct gdbarch *gdbarch = objfile->arch ();
16939 struct type *type, *range_type, *index_type, *char_type;
16940 struct attribute *attr;
16941 struct dynamic_prop prop;
16942 bool length_is_constant = true;
16943 LONGEST length;
16944
16945 /* There are a couple of places where bit sizes might be made use of
16946 when parsing a DW_TAG_string_type, however, no producer that we know
16947 of make use of these. Handling bit sizes that are a multiple of the
16948 byte size is easy enough, but what about other bit sizes? Lets deal
16949 with that problem when we have to. Warn about these attributes being
16950 unsupported, then parse the type and ignore them like we always
16951 have. */
16952 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16953 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16954 {
16955 static bool warning_printed = false;
16956 if (!warning_printed)
16957 {
16958 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16959 "currently supported on DW_TAG_string_type."));
16960 warning_printed = true;
16961 }
16962 }
16963
16964 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16965 if (attr != nullptr && !attr->form_is_constant ())
16966 {
16967 /* The string length describes the location at which the length of
16968 the string can be found. The size of the length field can be
16969 specified with one of the attributes below. */
16970 struct type *prop_type;
16971 struct attribute *len
16972 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16973 if (len == nullptr)
16974 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16975 if (len != nullptr && len->form_is_constant ())
16976 {
16977 /* Pass 0 as the default as we know this attribute is constant
16978 and the default value will not be returned. */
16979 LONGEST sz = len->constant_value (0);
16980 prop_type = cu->per_cu->int_type (sz, true);
16981 }
16982 else
16983 {
16984 /* If the size is not specified then we assume it is the size of
16985 an address on this target. */
16986 prop_type = cu->per_cu->addr_sized_int_type (true);
16987 }
16988
16989 /* Convert the attribute into a dynamic property. */
16990 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16991 length = 1;
16992 else
16993 length_is_constant = false;
16994 }
16995 else if (attr != nullptr)
16996 {
16997 /* This DW_AT_string_length just contains the length with no
16998 indirection. There's no need to create a dynamic property in this
16999 case. Pass 0 for the default value as we know it will not be
17000 returned in this case. */
17001 length = attr->constant_value (0);
17002 }
17003 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17004 {
17005 /* We don't currently support non-constant byte sizes for strings. */
17006 length = attr->constant_value (1);
17007 }
17008 else
17009 {
17010 /* Use 1 as a fallback length if we have nothing else. */
17011 length = 1;
17012 }
17013
17014 index_type = objfile_type (objfile)->builtin_int;
17015 if (length_is_constant)
17016 range_type = create_static_range_type (NULL, index_type, 1, length);
17017 else
17018 {
17019 struct dynamic_prop low_bound;
17020
17021 low_bound.kind = PROP_CONST;
17022 low_bound.data.const_val = 1;
17023 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17024 }
17025 char_type = language_string_char_type (cu->language_defn, gdbarch);
17026 type = create_string_type (NULL, char_type, range_type);
17027
17028 return set_die_type (die, type, cu);
17029 }
17030
17031 /* Assuming that DIE corresponds to a function, returns nonzero
17032 if the function is prototyped. */
17033
17034 static int
17035 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17036 {
17037 struct attribute *attr;
17038
17039 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17040 if (attr && (DW_UNSND (attr) != 0))
17041 return 1;
17042
17043 /* The DWARF standard implies that the DW_AT_prototyped attribute
17044 is only meaningful for C, but the concept also extends to other
17045 languages that allow unprototyped functions (Eg: Objective C).
17046 For all other languages, assume that functions are always
17047 prototyped. */
17048 if (cu->language != language_c
17049 && cu->language != language_objc
17050 && cu->language != language_opencl)
17051 return 1;
17052
17053 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17054 prototyped and unprototyped functions; default to prototyped,
17055 since that is more common in modern code (and RealView warns
17056 about unprototyped functions). */
17057 if (producer_is_realview (cu->producer))
17058 return 1;
17059
17060 return 0;
17061 }
17062
17063 /* Handle DIES due to C code like:
17064
17065 struct foo
17066 {
17067 int (*funcp)(int a, long l);
17068 int b;
17069 };
17070
17071 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17072
17073 static struct type *
17074 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17075 {
17076 struct objfile *objfile = cu->per_objfile->objfile;
17077 struct type *type; /* Type that this function returns. */
17078 struct type *ftype; /* Function that returns above type. */
17079 struct attribute *attr;
17080
17081 type = die_type (die, cu);
17082
17083 /* The die_type call above may have already set the type for this DIE. */
17084 ftype = get_die_type (die, cu);
17085 if (ftype)
17086 return ftype;
17087
17088 ftype = lookup_function_type (type);
17089
17090 if (prototyped_function_p (die, cu))
17091 TYPE_PROTOTYPED (ftype) = 1;
17092
17093 /* Store the calling convention in the type if it's available in
17094 the subroutine die. Otherwise set the calling convention to
17095 the default value DW_CC_normal. */
17096 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17097 if (attr != nullptr
17098 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17099 TYPE_CALLING_CONVENTION (ftype)
17100 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17101 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17102 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17103 else
17104 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17105
17106 /* Record whether the function returns normally to its caller or not
17107 if the DWARF producer set that information. */
17108 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17109 if (attr && (DW_UNSND (attr) != 0))
17110 TYPE_NO_RETURN (ftype) = 1;
17111
17112 /* We need to add the subroutine type to the die immediately so
17113 we don't infinitely recurse when dealing with parameters
17114 declared as the same subroutine type. */
17115 set_die_type (die, ftype, cu);
17116
17117 if (die->child != NULL)
17118 {
17119 struct type *void_type = objfile_type (objfile)->builtin_void;
17120 struct die_info *child_die;
17121 int nparams, iparams;
17122
17123 /* Count the number of parameters.
17124 FIXME: GDB currently ignores vararg functions, but knows about
17125 vararg member functions. */
17126 nparams = 0;
17127 child_die = die->child;
17128 while (child_die && child_die->tag)
17129 {
17130 if (child_die->tag == DW_TAG_formal_parameter)
17131 nparams++;
17132 else if (child_die->tag == DW_TAG_unspecified_parameters)
17133 TYPE_VARARGS (ftype) = 1;
17134 child_die = child_die->sibling;
17135 }
17136
17137 /* Allocate storage for parameters and fill them in. */
17138 ftype->set_num_fields (nparams);
17139 ftype->set_fields
17140 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17141
17142 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17143 even if we error out during the parameters reading below. */
17144 for (iparams = 0; iparams < nparams; iparams++)
17145 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17146
17147 iparams = 0;
17148 child_die = die->child;
17149 while (child_die && child_die->tag)
17150 {
17151 if (child_die->tag == DW_TAG_formal_parameter)
17152 {
17153 struct type *arg_type;
17154
17155 /* DWARF version 2 has no clean way to discern C++
17156 static and non-static member functions. G++ helps
17157 GDB by marking the first parameter for non-static
17158 member functions (which is the this pointer) as
17159 artificial. We pass this information to
17160 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17161
17162 DWARF version 3 added DW_AT_object_pointer, which GCC
17163 4.5 does not yet generate. */
17164 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17165 if (attr != nullptr)
17166 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17167 else
17168 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17169 arg_type = die_type (child_die, cu);
17170
17171 /* RealView does not mark THIS as const, which the testsuite
17172 expects. GCC marks THIS as const in method definitions,
17173 but not in the class specifications (GCC PR 43053). */
17174 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17175 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17176 {
17177 int is_this = 0;
17178 struct dwarf2_cu *arg_cu = cu;
17179 const char *name = dwarf2_name (child_die, cu);
17180
17181 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17182 if (attr != nullptr)
17183 {
17184 /* If the compiler emits this, use it. */
17185 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17186 is_this = 1;
17187 }
17188 else if (name && strcmp (name, "this") == 0)
17189 /* Function definitions will have the argument names. */
17190 is_this = 1;
17191 else if (name == NULL && iparams == 0)
17192 /* Declarations may not have the names, so like
17193 elsewhere in GDB, assume an artificial first
17194 argument is "this". */
17195 is_this = 1;
17196
17197 if (is_this)
17198 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17199 arg_type, 0);
17200 }
17201
17202 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17203 iparams++;
17204 }
17205 child_die = child_die->sibling;
17206 }
17207 }
17208
17209 return ftype;
17210 }
17211
17212 static struct type *
17213 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17214 {
17215 struct objfile *objfile = cu->per_objfile->objfile;
17216 const char *name = NULL;
17217 struct type *this_type, *target_type;
17218
17219 name = dwarf2_full_name (NULL, die, cu);
17220 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17221 TYPE_TARGET_STUB (this_type) = 1;
17222 set_die_type (die, this_type, cu);
17223 target_type = die_type (die, cu);
17224 if (target_type != this_type)
17225 TYPE_TARGET_TYPE (this_type) = target_type;
17226 else
17227 {
17228 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17229 spec and cause infinite loops in GDB. */
17230 complaint (_("Self-referential DW_TAG_typedef "
17231 "- DIE at %s [in module %s]"),
17232 sect_offset_str (die->sect_off), objfile_name (objfile));
17233 TYPE_TARGET_TYPE (this_type) = NULL;
17234 }
17235 if (name == NULL)
17236 {
17237 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17238 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17239 Handle these by just returning the target type, rather than
17240 constructing an anonymous typedef type and trying to handle this
17241 elsewhere. */
17242 set_die_type (die, target_type, cu);
17243 return target_type;
17244 }
17245 return this_type;
17246 }
17247
17248 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17249 (which may be different from NAME) to the architecture back-end to allow
17250 it to guess the correct format if necessary. */
17251
17252 static struct type *
17253 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17254 const char *name_hint, enum bfd_endian byte_order)
17255 {
17256 struct gdbarch *gdbarch = objfile->arch ();
17257 const struct floatformat **format;
17258 struct type *type;
17259
17260 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17261 if (format)
17262 type = init_float_type (objfile, bits, name, format, byte_order);
17263 else
17264 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17265
17266 return type;
17267 }
17268
17269 /* Allocate an integer type of size BITS and name NAME. */
17270
17271 static struct type *
17272 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17273 int bits, int unsigned_p, const char *name)
17274 {
17275 struct type *type;
17276
17277 /* Versions of Intel's C Compiler generate an integer type called "void"
17278 instead of using DW_TAG_unspecified_type. This has been seen on
17279 at least versions 14, 17, and 18. */
17280 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17281 && strcmp (name, "void") == 0)
17282 type = objfile_type (objfile)->builtin_void;
17283 else
17284 type = init_integer_type (objfile, bits, unsigned_p, name);
17285
17286 return type;
17287 }
17288
17289 /* Initialise and return a floating point type of size BITS suitable for
17290 use as a component of a complex number. The NAME_HINT is passed through
17291 when initialising the floating point type and is the name of the complex
17292 type.
17293
17294 As DWARF doesn't currently provide an explicit name for the components
17295 of a complex number, but it can be helpful to have these components
17296 named, we try to select a suitable name based on the size of the
17297 component. */
17298 static struct type *
17299 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17300 struct objfile *objfile,
17301 int bits, const char *name_hint,
17302 enum bfd_endian byte_order)
17303 {
17304 gdbarch *gdbarch = objfile->arch ();
17305 struct type *tt = nullptr;
17306
17307 /* Try to find a suitable floating point builtin type of size BITS.
17308 We're going to use the name of this type as the name for the complex
17309 target type that we are about to create. */
17310 switch (cu->language)
17311 {
17312 case language_fortran:
17313 switch (bits)
17314 {
17315 case 32:
17316 tt = builtin_f_type (gdbarch)->builtin_real;
17317 break;
17318 case 64:
17319 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17320 break;
17321 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17322 case 128:
17323 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17324 break;
17325 }
17326 break;
17327 default:
17328 switch (bits)
17329 {
17330 case 32:
17331 tt = builtin_type (gdbarch)->builtin_float;
17332 break;
17333 case 64:
17334 tt = builtin_type (gdbarch)->builtin_double;
17335 break;
17336 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17337 case 128:
17338 tt = builtin_type (gdbarch)->builtin_long_double;
17339 break;
17340 }
17341 break;
17342 }
17343
17344 /* If the type we found doesn't match the size we were looking for, then
17345 pretend we didn't find a type at all, the complex target type we
17346 create will then be nameless. */
17347 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17348 tt = nullptr;
17349
17350 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17351 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17352 }
17353
17354 /* Find a representation of a given base type and install
17355 it in the TYPE field of the die. */
17356
17357 static struct type *
17358 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17359 {
17360 struct objfile *objfile = cu->per_objfile->objfile;
17361 struct type *type;
17362 struct attribute *attr;
17363 int encoding = 0, bits = 0;
17364 const char *name;
17365 gdbarch *arch;
17366
17367 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17368 if (attr != nullptr)
17369 encoding = DW_UNSND (attr);
17370 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17371 if (attr != nullptr)
17372 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17373 name = dwarf2_name (die, cu);
17374 if (!name)
17375 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17376
17377 arch = objfile->arch ();
17378 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17379
17380 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17381 if (attr)
17382 {
17383 int endianity = DW_UNSND (attr);
17384
17385 switch (endianity)
17386 {
17387 case DW_END_big:
17388 byte_order = BFD_ENDIAN_BIG;
17389 break;
17390 case DW_END_little:
17391 byte_order = BFD_ENDIAN_LITTLE;
17392 break;
17393 default:
17394 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17395 break;
17396 }
17397 }
17398
17399 switch (encoding)
17400 {
17401 case DW_ATE_address:
17402 /* Turn DW_ATE_address into a void * pointer. */
17403 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17404 type = init_pointer_type (objfile, bits, name, type);
17405 break;
17406 case DW_ATE_boolean:
17407 type = init_boolean_type (objfile, bits, 1, name);
17408 break;
17409 case DW_ATE_complex_float:
17410 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17411 byte_order);
17412 if (type->code () == TYPE_CODE_ERROR)
17413 {
17414 if (name == nullptr)
17415 {
17416 struct obstack *obstack
17417 = &cu->per_objfile->objfile->objfile_obstack;
17418 name = obconcat (obstack, "_Complex ", type->name (),
17419 nullptr);
17420 }
17421 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17422 }
17423 else
17424 type = init_complex_type (name, type);
17425 break;
17426 case DW_ATE_decimal_float:
17427 type = init_decfloat_type (objfile, bits, name);
17428 break;
17429 case DW_ATE_float:
17430 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17431 break;
17432 case DW_ATE_signed:
17433 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17434 break;
17435 case DW_ATE_unsigned:
17436 if (cu->language == language_fortran
17437 && name
17438 && startswith (name, "character("))
17439 type = init_character_type (objfile, bits, 1, name);
17440 else
17441 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17442 break;
17443 case DW_ATE_signed_char:
17444 if (cu->language == language_ada || cu->language == language_m2
17445 || cu->language == language_pascal
17446 || cu->language == language_fortran)
17447 type = init_character_type (objfile, bits, 0, name);
17448 else
17449 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17450 break;
17451 case DW_ATE_unsigned_char:
17452 if (cu->language == language_ada || cu->language == language_m2
17453 || cu->language == language_pascal
17454 || cu->language == language_fortran
17455 || cu->language == language_rust)
17456 type = init_character_type (objfile, bits, 1, name);
17457 else
17458 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17459 break;
17460 case DW_ATE_UTF:
17461 {
17462 if (bits == 16)
17463 type = builtin_type (arch)->builtin_char16;
17464 else if (bits == 32)
17465 type = builtin_type (arch)->builtin_char32;
17466 else
17467 {
17468 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17469 bits);
17470 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17471 }
17472 return set_die_type (die, type, cu);
17473 }
17474 break;
17475
17476 default:
17477 complaint (_("unsupported DW_AT_encoding: '%s'"),
17478 dwarf_type_encoding_name (encoding));
17479 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17480 break;
17481 }
17482
17483 if (name && strcmp (name, "char") == 0)
17484 TYPE_NOSIGN (type) = 1;
17485
17486 maybe_set_alignment (cu, die, type);
17487
17488 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17489
17490 return set_die_type (die, type, cu);
17491 }
17492
17493 /* Parse dwarf attribute if it's a block, reference or constant and put the
17494 resulting value of the attribute into struct bound_prop.
17495 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17496
17497 static int
17498 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17499 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17500 struct type *default_type)
17501 {
17502 struct dwarf2_property_baton *baton;
17503 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17504 struct objfile *objfile = per_objfile->objfile;
17505 struct obstack *obstack = &objfile->objfile_obstack;
17506
17507 gdb_assert (default_type != NULL);
17508
17509 if (attr == NULL || prop == NULL)
17510 return 0;
17511
17512 if (attr->form_is_block ())
17513 {
17514 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17515 baton->property_type = default_type;
17516 baton->locexpr.per_cu = cu->per_cu;
17517 baton->locexpr.per_objfile = per_objfile;
17518 baton->locexpr.size = DW_BLOCK (attr)->size;
17519 baton->locexpr.data = DW_BLOCK (attr)->data;
17520 switch (attr->name)
17521 {
17522 case DW_AT_string_length:
17523 baton->locexpr.is_reference = true;
17524 break;
17525 default:
17526 baton->locexpr.is_reference = false;
17527 break;
17528 }
17529 prop->data.baton = baton;
17530 prop->kind = PROP_LOCEXPR;
17531 gdb_assert (prop->data.baton != NULL);
17532 }
17533 else if (attr->form_is_ref ())
17534 {
17535 struct dwarf2_cu *target_cu = cu;
17536 struct die_info *target_die;
17537 struct attribute *target_attr;
17538
17539 target_die = follow_die_ref (die, attr, &target_cu);
17540 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17541 if (target_attr == NULL)
17542 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17543 target_cu);
17544 if (target_attr == NULL)
17545 return 0;
17546
17547 switch (target_attr->name)
17548 {
17549 case DW_AT_location:
17550 if (target_attr->form_is_section_offset ())
17551 {
17552 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17553 baton->property_type = die_type (target_die, target_cu);
17554 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17555 prop->data.baton = baton;
17556 prop->kind = PROP_LOCLIST;
17557 gdb_assert (prop->data.baton != NULL);
17558 }
17559 else if (target_attr->form_is_block ())
17560 {
17561 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17562 baton->property_type = die_type (target_die, target_cu);
17563 baton->locexpr.per_cu = cu->per_cu;
17564 baton->locexpr.per_objfile = per_objfile;
17565 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17566 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17567 baton->locexpr.is_reference = true;
17568 prop->data.baton = baton;
17569 prop->kind = PROP_LOCEXPR;
17570 gdb_assert (prop->data.baton != NULL);
17571 }
17572 else
17573 {
17574 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17575 "dynamic property");
17576 return 0;
17577 }
17578 break;
17579 case DW_AT_data_member_location:
17580 {
17581 LONGEST offset;
17582
17583 if (!handle_data_member_location (target_die, target_cu,
17584 &offset))
17585 return 0;
17586
17587 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17588 baton->property_type = read_type_die (target_die->parent,
17589 target_cu);
17590 baton->offset_info.offset = offset;
17591 baton->offset_info.type = die_type (target_die, target_cu);
17592 prop->data.baton = baton;
17593 prop->kind = PROP_ADDR_OFFSET;
17594 break;
17595 }
17596 }
17597 }
17598 else if (attr->form_is_constant ())
17599 {
17600 prop->data.const_val = attr->constant_value (0);
17601 prop->kind = PROP_CONST;
17602 }
17603 else
17604 {
17605 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17606 dwarf2_name (die, cu));
17607 return 0;
17608 }
17609
17610 return 1;
17611 }
17612
17613 /* See read.h. */
17614
17615 struct type *
17616 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17617 {
17618 struct objfile *objfile = dwarf2_per_objfile->objfile;
17619 struct type *int_type;
17620
17621 /* Helper macro to examine the various builtin types. */
17622 #define TRY_TYPE(F) \
17623 int_type = (unsigned_p \
17624 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17625 : objfile_type (objfile)->builtin_ ## F); \
17626 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17627 return int_type
17628
17629 TRY_TYPE (char);
17630 TRY_TYPE (short);
17631 TRY_TYPE (int);
17632 TRY_TYPE (long);
17633 TRY_TYPE (long_long);
17634
17635 #undef TRY_TYPE
17636
17637 gdb_assert_not_reached ("unable to find suitable integer type");
17638 }
17639
17640 /* See read.h. */
17641
17642 struct type *
17643 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17644 {
17645 int addr_size = this->addr_size ();
17646 return int_type (addr_size, unsigned_p);
17647 }
17648
17649 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17650 present (which is valid) then compute the default type based on the
17651 compilation units address size. */
17652
17653 static struct type *
17654 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17655 {
17656 struct type *index_type = die_type (die, cu);
17657
17658 /* Dwarf-2 specifications explicitly allows to create subrange types
17659 without specifying a base type.
17660 In that case, the base type must be set to the type of
17661 the lower bound, upper bound or count, in that order, if any of these
17662 three attributes references an object that has a type.
17663 If no base type is found, the Dwarf-2 specifications say that
17664 a signed integer type of size equal to the size of an address should
17665 be used.
17666 For the following C code: `extern char gdb_int [];'
17667 GCC produces an empty range DIE.
17668 FIXME: muller/2010-05-28: Possible references to object for low bound,
17669 high bound or count are not yet handled by this code. */
17670 if (index_type->code () == TYPE_CODE_VOID)
17671 index_type = cu->per_cu->addr_sized_int_type (false);
17672
17673 return index_type;
17674 }
17675
17676 /* Read the given DW_AT_subrange DIE. */
17677
17678 static struct type *
17679 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17680 {
17681 struct type *base_type, *orig_base_type;
17682 struct type *range_type;
17683 struct attribute *attr;
17684 struct dynamic_prop low, high;
17685 int low_default_is_valid;
17686 int high_bound_is_count = 0;
17687 const char *name;
17688 ULONGEST negative_mask;
17689
17690 orig_base_type = read_subrange_index_type (die, cu);
17691
17692 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17693 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17694 creating the range type, but we use the result of check_typedef
17695 when examining properties of the type. */
17696 base_type = check_typedef (orig_base_type);
17697
17698 /* The die_type call above may have already set the type for this DIE. */
17699 range_type = get_die_type (die, cu);
17700 if (range_type)
17701 return range_type;
17702
17703 low.kind = PROP_CONST;
17704 high.kind = PROP_CONST;
17705 high.data.const_val = 0;
17706
17707 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17708 omitting DW_AT_lower_bound. */
17709 switch (cu->language)
17710 {
17711 case language_c:
17712 case language_cplus:
17713 low.data.const_val = 0;
17714 low_default_is_valid = 1;
17715 break;
17716 case language_fortran:
17717 low.data.const_val = 1;
17718 low_default_is_valid = 1;
17719 break;
17720 case language_d:
17721 case language_objc:
17722 case language_rust:
17723 low.data.const_val = 0;
17724 low_default_is_valid = (cu->header.version >= 4);
17725 break;
17726 case language_ada:
17727 case language_m2:
17728 case language_pascal:
17729 low.data.const_val = 1;
17730 low_default_is_valid = (cu->header.version >= 4);
17731 break;
17732 default:
17733 low.data.const_val = 0;
17734 low_default_is_valid = 0;
17735 break;
17736 }
17737
17738 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17739 if (attr != nullptr)
17740 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17741 else if (!low_default_is_valid)
17742 complaint (_("Missing DW_AT_lower_bound "
17743 "- DIE at %s [in module %s]"),
17744 sect_offset_str (die->sect_off),
17745 objfile_name (cu->per_objfile->objfile));
17746
17747 struct attribute *attr_ub, *attr_count;
17748 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17749 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17750 {
17751 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17752 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17753 {
17754 /* If bounds are constant do the final calculation here. */
17755 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17756 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17757 else
17758 high_bound_is_count = 1;
17759 }
17760 else
17761 {
17762 if (attr_ub != NULL)
17763 complaint (_("Unresolved DW_AT_upper_bound "
17764 "- DIE at %s [in module %s]"),
17765 sect_offset_str (die->sect_off),
17766 objfile_name (cu->per_objfile->objfile));
17767 if (attr_count != NULL)
17768 complaint (_("Unresolved DW_AT_count "
17769 "- DIE at %s [in module %s]"),
17770 sect_offset_str (die->sect_off),
17771 objfile_name (cu->per_objfile->objfile));
17772 }
17773 }
17774
17775 LONGEST bias = 0;
17776 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17777 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17778 bias = bias_attr->constant_value (0);
17779
17780 /* Normally, the DWARF producers are expected to use a signed
17781 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17782 But this is unfortunately not always the case, as witnessed
17783 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17784 is used instead. To work around that ambiguity, we treat
17785 the bounds as signed, and thus sign-extend their values, when
17786 the base type is signed. */
17787 negative_mask =
17788 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17789 if (low.kind == PROP_CONST
17790 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17791 low.data.const_val |= negative_mask;
17792 if (high.kind == PROP_CONST
17793 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17794 high.data.const_val |= negative_mask;
17795
17796 /* Check for bit and byte strides. */
17797 struct dynamic_prop byte_stride_prop;
17798 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17799 if (attr_byte_stride != nullptr)
17800 {
17801 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17802 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17803 prop_type);
17804 }
17805
17806 struct dynamic_prop bit_stride_prop;
17807 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17808 if (attr_bit_stride != nullptr)
17809 {
17810 /* It only makes sense to have either a bit or byte stride. */
17811 if (attr_byte_stride != nullptr)
17812 {
17813 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17814 "- DIE at %s [in module %s]"),
17815 sect_offset_str (die->sect_off),
17816 objfile_name (cu->per_objfile->objfile));
17817 attr_bit_stride = nullptr;
17818 }
17819 else
17820 {
17821 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17822 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17823 prop_type);
17824 }
17825 }
17826
17827 if (attr_byte_stride != nullptr
17828 || attr_bit_stride != nullptr)
17829 {
17830 bool byte_stride_p = (attr_byte_stride != nullptr);
17831 struct dynamic_prop *stride
17832 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17833
17834 range_type
17835 = create_range_type_with_stride (NULL, orig_base_type, &low,
17836 &high, bias, stride, byte_stride_p);
17837 }
17838 else
17839 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17840
17841 if (high_bound_is_count)
17842 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17843
17844 /* Ada expects an empty array on no boundary attributes. */
17845 if (attr == NULL && cu->language != language_ada)
17846 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17847
17848 name = dwarf2_name (die, cu);
17849 if (name)
17850 range_type->set_name (name);
17851
17852 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17853 if (attr != nullptr)
17854 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17855
17856 maybe_set_alignment (cu, die, range_type);
17857
17858 set_die_type (die, range_type, cu);
17859
17860 /* set_die_type should be already done. */
17861 set_descriptive_type (range_type, die, cu);
17862
17863 return range_type;
17864 }
17865
17866 static struct type *
17867 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17868 {
17869 struct type *type;
17870
17871 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17872 type->set_name (dwarf2_name (die, cu));
17873
17874 /* In Ada, an unspecified type is typically used when the description
17875 of the type is deferred to a different unit. When encountering
17876 such a type, we treat it as a stub, and try to resolve it later on,
17877 when needed. */
17878 if (cu->language == language_ada)
17879 TYPE_STUB (type) = 1;
17880
17881 return set_die_type (die, type, cu);
17882 }
17883
17884 /* Read a single die and all its descendents. Set the die's sibling
17885 field to NULL; set other fields in the die correctly, and set all
17886 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17887 location of the info_ptr after reading all of those dies. PARENT
17888 is the parent of the die in question. */
17889
17890 static struct die_info *
17891 read_die_and_children (const struct die_reader_specs *reader,
17892 const gdb_byte *info_ptr,
17893 const gdb_byte **new_info_ptr,
17894 struct die_info *parent)
17895 {
17896 struct die_info *die;
17897 const gdb_byte *cur_ptr;
17898
17899 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17900 if (die == NULL)
17901 {
17902 *new_info_ptr = cur_ptr;
17903 return NULL;
17904 }
17905 store_in_ref_table (die, reader->cu);
17906
17907 if (die->has_children)
17908 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17909 else
17910 {
17911 die->child = NULL;
17912 *new_info_ptr = cur_ptr;
17913 }
17914
17915 die->sibling = NULL;
17916 die->parent = parent;
17917 return die;
17918 }
17919
17920 /* Read a die, all of its descendents, and all of its siblings; set
17921 all of the fields of all of the dies correctly. Arguments are as
17922 in read_die_and_children. */
17923
17924 static struct die_info *
17925 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17926 const gdb_byte *info_ptr,
17927 const gdb_byte **new_info_ptr,
17928 struct die_info *parent)
17929 {
17930 struct die_info *first_die, *last_sibling;
17931 const gdb_byte *cur_ptr;
17932
17933 cur_ptr = info_ptr;
17934 first_die = last_sibling = NULL;
17935
17936 while (1)
17937 {
17938 struct die_info *die
17939 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17940
17941 if (die == NULL)
17942 {
17943 *new_info_ptr = cur_ptr;
17944 return first_die;
17945 }
17946
17947 if (!first_die)
17948 first_die = die;
17949 else
17950 last_sibling->sibling = die;
17951
17952 last_sibling = die;
17953 }
17954 }
17955
17956 /* Read a die, all of its descendents, and all of its siblings; set
17957 all of the fields of all of the dies correctly. Arguments are as
17958 in read_die_and_children.
17959 This the main entry point for reading a DIE and all its children. */
17960
17961 static struct die_info *
17962 read_die_and_siblings (const struct die_reader_specs *reader,
17963 const gdb_byte *info_ptr,
17964 const gdb_byte **new_info_ptr,
17965 struct die_info *parent)
17966 {
17967 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17968 new_info_ptr, parent);
17969
17970 if (dwarf_die_debug)
17971 {
17972 fprintf_unfiltered (gdb_stdlog,
17973 "Read die from %s@0x%x of %s:\n",
17974 reader->die_section->get_name (),
17975 (unsigned) (info_ptr - reader->die_section->buffer),
17976 bfd_get_filename (reader->abfd));
17977 dump_die (die, dwarf_die_debug);
17978 }
17979
17980 return die;
17981 }
17982
17983 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17984 attributes.
17985 The caller is responsible for filling in the extra attributes
17986 and updating (*DIEP)->num_attrs.
17987 Set DIEP to point to a newly allocated die with its information,
17988 except for its child, sibling, and parent fields. */
17989
17990 static const gdb_byte *
17991 read_full_die_1 (const struct die_reader_specs *reader,
17992 struct die_info **diep, const gdb_byte *info_ptr,
17993 int num_extra_attrs)
17994 {
17995 unsigned int abbrev_number, bytes_read, i;
17996 struct abbrev_info *abbrev;
17997 struct die_info *die;
17998 struct dwarf2_cu *cu = reader->cu;
17999 bfd *abfd = reader->abfd;
18000
18001 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18002 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18003 info_ptr += bytes_read;
18004 if (!abbrev_number)
18005 {
18006 *diep = NULL;
18007 return info_ptr;
18008 }
18009
18010 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18011 if (!abbrev)
18012 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18013 abbrev_number,
18014 bfd_get_filename (abfd));
18015
18016 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18017 die->sect_off = sect_off;
18018 die->tag = abbrev->tag;
18019 die->abbrev = abbrev_number;
18020 die->has_children = abbrev->has_children;
18021
18022 /* Make the result usable.
18023 The caller needs to update num_attrs after adding the extra
18024 attributes. */
18025 die->num_attrs = abbrev->num_attrs;
18026
18027 std::vector<int> indexes_that_need_reprocess;
18028 for (i = 0; i < abbrev->num_attrs; ++i)
18029 {
18030 bool need_reprocess;
18031 info_ptr =
18032 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18033 info_ptr, &need_reprocess);
18034 if (need_reprocess)
18035 indexes_that_need_reprocess.push_back (i);
18036 }
18037
18038 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18039 if (attr != nullptr)
18040 cu->str_offsets_base = DW_UNSND (attr);
18041
18042 attr = die->attr (DW_AT_loclists_base);
18043 if (attr != nullptr)
18044 cu->loclist_base = DW_UNSND (attr);
18045
18046 auto maybe_addr_base = die->addr_base ();
18047 if (maybe_addr_base.has_value ())
18048 cu->addr_base = *maybe_addr_base;
18049 for (int index : indexes_that_need_reprocess)
18050 read_attribute_reprocess (reader, &die->attrs[index]);
18051 *diep = die;
18052 return info_ptr;
18053 }
18054
18055 /* Read a die and all its attributes.
18056 Set DIEP to point to a newly allocated die with its information,
18057 except for its child, sibling, and parent fields. */
18058
18059 static const gdb_byte *
18060 read_full_die (const struct die_reader_specs *reader,
18061 struct die_info **diep, const gdb_byte *info_ptr)
18062 {
18063 const gdb_byte *result;
18064
18065 result = read_full_die_1 (reader, diep, info_ptr, 0);
18066
18067 if (dwarf_die_debug)
18068 {
18069 fprintf_unfiltered (gdb_stdlog,
18070 "Read die from %s@0x%x of %s:\n",
18071 reader->die_section->get_name (),
18072 (unsigned) (info_ptr - reader->die_section->buffer),
18073 bfd_get_filename (reader->abfd));
18074 dump_die (*diep, dwarf_die_debug);
18075 }
18076
18077 return result;
18078 }
18079 \f
18080
18081 /* Returns nonzero if TAG represents a type that we might generate a partial
18082 symbol for. */
18083
18084 static int
18085 is_type_tag_for_partial (int tag)
18086 {
18087 switch (tag)
18088 {
18089 #if 0
18090 /* Some types that would be reasonable to generate partial symbols for,
18091 that we don't at present. */
18092 case DW_TAG_array_type:
18093 case DW_TAG_file_type:
18094 case DW_TAG_ptr_to_member_type:
18095 case DW_TAG_set_type:
18096 case DW_TAG_string_type:
18097 case DW_TAG_subroutine_type:
18098 #endif
18099 case DW_TAG_base_type:
18100 case DW_TAG_class_type:
18101 case DW_TAG_interface_type:
18102 case DW_TAG_enumeration_type:
18103 case DW_TAG_structure_type:
18104 case DW_TAG_subrange_type:
18105 case DW_TAG_typedef:
18106 case DW_TAG_union_type:
18107 return 1;
18108 default:
18109 return 0;
18110 }
18111 }
18112
18113 /* Load all DIEs that are interesting for partial symbols into memory. */
18114
18115 static struct partial_die_info *
18116 load_partial_dies (const struct die_reader_specs *reader,
18117 const gdb_byte *info_ptr, int building_psymtab)
18118 {
18119 struct dwarf2_cu *cu = reader->cu;
18120 struct objfile *objfile = cu->per_objfile->objfile;
18121 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18122 unsigned int bytes_read;
18123 unsigned int load_all = 0;
18124 int nesting_level = 1;
18125
18126 parent_die = NULL;
18127 last_die = NULL;
18128
18129 gdb_assert (cu->per_cu != NULL);
18130 if (cu->per_cu->load_all_dies)
18131 load_all = 1;
18132
18133 cu->partial_dies
18134 = htab_create_alloc_ex (cu->header.length / 12,
18135 partial_die_hash,
18136 partial_die_eq,
18137 NULL,
18138 &cu->comp_unit_obstack,
18139 hashtab_obstack_allocate,
18140 dummy_obstack_deallocate);
18141
18142 while (1)
18143 {
18144 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18145
18146 /* A NULL abbrev means the end of a series of children. */
18147 if (abbrev == NULL)
18148 {
18149 if (--nesting_level == 0)
18150 return first_die;
18151
18152 info_ptr += bytes_read;
18153 last_die = parent_die;
18154 parent_die = parent_die->die_parent;
18155 continue;
18156 }
18157
18158 /* Check for template arguments. We never save these; if
18159 they're seen, we just mark the parent, and go on our way. */
18160 if (parent_die != NULL
18161 && cu->language == language_cplus
18162 && (abbrev->tag == DW_TAG_template_type_param
18163 || abbrev->tag == DW_TAG_template_value_param))
18164 {
18165 parent_die->has_template_arguments = 1;
18166
18167 if (!load_all)
18168 {
18169 /* We don't need a partial DIE for the template argument. */
18170 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18171 continue;
18172 }
18173 }
18174
18175 /* We only recurse into c++ subprograms looking for template arguments.
18176 Skip their other children. */
18177 if (!load_all
18178 && cu->language == language_cplus
18179 && parent_die != NULL
18180 && parent_die->tag == DW_TAG_subprogram)
18181 {
18182 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18183 continue;
18184 }
18185
18186 /* Check whether this DIE is interesting enough to save. Normally
18187 we would not be interested in members here, but there may be
18188 later variables referencing them via DW_AT_specification (for
18189 static members). */
18190 if (!load_all
18191 && !is_type_tag_for_partial (abbrev->tag)
18192 && abbrev->tag != DW_TAG_constant
18193 && abbrev->tag != DW_TAG_enumerator
18194 && abbrev->tag != DW_TAG_subprogram
18195 && abbrev->tag != DW_TAG_inlined_subroutine
18196 && abbrev->tag != DW_TAG_lexical_block
18197 && abbrev->tag != DW_TAG_variable
18198 && abbrev->tag != DW_TAG_namespace
18199 && abbrev->tag != DW_TAG_module
18200 && abbrev->tag != DW_TAG_member
18201 && abbrev->tag != DW_TAG_imported_unit
18202 && abbrev->tag != DW_TAG_imported_declaration)
18203 {
18204 /* Otherwise we skip to the next sibling, if any. */
18205 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18206 continue;
18207 }
18208
18209 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18210 abbrev);
18211
18212 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18213
18214 /* This two-pass algorithm for processing partial symbols has a
18215 high cost in cache pressure. Thus, handle some simple cases
18216 here which cover the majority of C partial symbols. DIEs
18217 which neither have specification tags in them, nor could have
18218 specification tags elsewhere pointing at them, can simply be
18219 processed and discarded.
18220
18221 This segment is also optional; scan_partial_symbols and
18222 add_partial_symbol will handle these DIEs if we chain
18223 them in normally. When compilers which do not emit large
18224 quantities of duplicate debug information are more common,
18225 this code can probably be removed. */
18226
18227 /* Any complete simple types at the top level (pretty much all
18228 of them, for a language without namespaces), can be processed
18229 directly. */
18230 if (parent_die == NULL
18231 && pdi.has_specification == 0
18232 && pdi.is_declaration == 0
18233 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18234 || pdi.tag == DW_TAG_base_type
18235 || pdi.tag == DW_TAG_subrange_type))
18236 {
18237 if (building_psymtab && pdi.name != NULL)
18238 add_psymbol_to_list (pdi.name, false,
18239 VAR_DOMAIN, LOC_TYPEDEF, -1,
18240 psymbol_placement::STATIC,
18241 0, cu->language, objfile);
18242 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18243 continue;
18244 }
18245
18246 /* The exception for DW_TAG_typedef with has_children above is
18247 a workaround of GCC PR debug/47510. In the case of this complaint
18248 type_name_or_error will error on such types later.
18249
18250 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18251 it could not find the child DIEs referenced later, this is checked
18252 above. In correct DWARF DW_TAG_typedef should have no children. */
18253
18254 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18255 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18256 "- DIE at %s [in module %s]"),
18257 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18258
18259 /* If we're at the second level, and we're an enumerator, and
18260 our parent has no specification (meaning possibly lives in a
18261 namespace elsewhere), then we can add the partial symbol now
18262 instead of queueing it. */
18263 if (pdi.tag == DW_TAG_enumerator
18264 && parent_die != NULL
18265 && parent_die->die_parent == NULL
18266 && parent_die->tag == DW_TAG_enumeration_type
18267 && parent_die->has_specification == 0)
18268 {
18269 if (pdi.name == NULL)
18270 complaint (_("malformed enumerator DIE ignored"));
18271 else if (building_psymtab)
18272 add_psymbol_to_list (pdi.name, false,
18273 VAR_DOMAIN, LOC_CONST, -1,
18274 cu->language == language_cplus
18275 ? psymbol_placement::GLOBAL
18276 : psymbol_placement::STATIC,
18277 0, cu->language, objfile);
18278
18279 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18280 continue;
18281 }
18282
18283 struct partial_die_info *part_die
18284 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18285
18286 /* We'll save this DIE so link it in. */
18287 part_die->die_parent = parent_die;
18288 part_die->die_sibling = NULL;
18289 part_die->die_child = NULL;
18290
18291 if (last_die && last_die == parent_die)
18292 last_die->die_child = part_die;
18293 else if (last_die)
18294 last_die->die_sibling = part_die;
18295
18296 last_die = part_die;
18297
18298 if (first_die == NULL)
18299 first_die = part_die;
18300
18301 /* Maybe add the DIE to the hash table. Not all DIEs that we
18302 find interesting need to be in the hash table, because we
18303 also have the parent/sibling/child chains; only those that we
18304 might refer to by offset later during partial symbol reading.
18305
18306 For now this means things that might have be the target of a
18307 DW_AT_specification, DW_AT_abstract_origin, or
18308 DW_AT_extension. DW_AT_extension will refer only to
18309 namespaces; DW_AT_abstract_origin refers to functions (and
18310 many things under the function DIE, but we do not recurse
18311 into function DIEs during partial symbol reading) and
18312 possibly variables as well; DW_AT_specification refers to
18313 declarations. Declarations ought to have the DW_AT_declaration
18314 flag. It happens that GCC forgets to put it in sometimes, but
18315 only for functions, not for types.
18316
18317 Adding more things than necessary to the hash table is harmless
18318 except for the performance cost. Adding too few will result in
18319 wasted time in find_partial_die, when we reread the compilation
18320 unit with load_all_dies set. */
18321
18322 if (load_all
18323 || abbrev->tag == DW_TAG_constant
18324 || abbrev->tag == DW_TAG_subprogram
18325 || abbrev->tag == DW_TAG_variable
18326 || abbrev->tag == DW_TAG_namespace
18327 || part_die->is_declaration)
18328 {
18329 void **slot;
18330
18331 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18332 to_underlying (part_die->sect_off),
18333 INSERT);
18334 *slot = part_die;
18335 }
18336
18337 /* For some DIEs we want to follow their children (if any). For C
18338 we have no reason to follow the children of structures; for other
18339 languages we have to, so that we can get at method physnames
18340 to infer fully qualified class names, for DW_AT_specification,
18341 and for C++ template arguments. For C++, we also look one level
18342 inside functions to find template arguments (if the name of the
18343 function does not already contain the template arguments).
18344
18345 For Ada and Fortran, we need to scan the children of subprograms
18346 and lexical blocks as well because these languages allow the
18347 definition of nested entities that could be interesting for the
18348 debugger, such as nested subprograms for instance. */
18349 if (last_die->has_children
18350 && (load_all
18351 || last_die->tag == DW_TAG_namespace
18352 || last_die->tag == DW_TAG_module
18353 || last_die->tag == DW_TAG_enumeration_type
18354 || (cu->language == language_cplus
18355 && last_die->tag == DW_TAG_subprogram
18356 && (last_die->name == NULL
18357 || strchr (last_die->name, '<') == NULL))
18358 || (cu->language != language_c
18359 && (last_die->tag == DW_TAG_class_type
18360 || last_die->tag == DW_TAG_interface_type
18361 || last_die->tag == DW_TAG_structure_type
18362 || last_die->tag == DW_TAG_union_type))
18363 || ((cu->language == language_ada
18364 || cu->language == language_fortran)
18365 && (last_die->tag == DW_TAG_subprogram
18366 || last_die->tag == DW_TAG_lexical_block))))
18367 {
18368 nesting_level++;
18369 parent_die = last_die;
18370 continue;
18371 }
18372
18373 /* Otherwise we skip to the next sibling, if any. */
18374 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18375
18376 /* Back to the top, do it again. */
18377 }
18378 }
18379
18380 partial_die_info::partial_die_info (sect_offset sect_off_,
18381 struct abbrev_info *abbrev)
18382 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18383 {
18384 }
18385
18386 /* Read a minimal amount of information into the minimal die structure.
18387 INFO_PTR should point just after the initial uleb128 of a DIE. */
18388
18389 const gdb_byte *
18390 partial_die_info::read (const struct die_reader_specs *reader,
18391 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18392 {
18393 struct dwarf2_cu *cu = reader->cu;
18394 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18395 unsigned int i;
18396 int has_low_pc_attr = 0;
18397 int has_high_pc_attr = 0;
18398 int high_pc_relative = 0;
18399
18400 for (i = 0; i < abbrev.num_attrs; ++i)
18401 {
18402 attribute attr;
18403 bool need_reprocess;
18404 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18405 info_ptr, &need_reprocess);
18406 /* String and address offsets that need to do the reprocessing have
18407 already been read at this point, so there is no need to wait until
18408 the loop terminates to do the reprocessing. */
18409 if (need_reprocess)
18410 read_attribute_reprocess (reader, &attr);
18411 /* Store the data if it is of an attribute we want to keep in a
18412 partial symbol table. */
18413 switch (attr.name)
18414 {
18415 case DW_AT_name:
18416 switch (tag)
18417 {
18418 case DW_TAG_compile_unit:
18419 case DW_TAG_partial_unit:
18420 case DW_TAG_type_unit:
18421 /* Compilation units have a DW_AT_name that is a filename, not
18422 a source language identifier. */
18423 case DW_TAG_enumeration_type:
18424 case DW_TAG_enumerator:
18425 /* These tags always have simple identifiers already; no need
18426 to canonicalize them. */
18427 name = DW_STRING (&attr);
18428 break;
18429 default:
18430 {
18431 struct objfile *objfile = dwarf2_per_objfile->objfile;
18432
18433 name
18434 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18435 }
18436 break;
18437 }
18438 break;
18439 case DW_AT_linkage_name:
18440 case DW_AT_MIPS_linkage_name:
18441 /* Note that both forms of linkage name might appear. We
18442 assume they will be the same, and we only store the last
18443 one we see. */
18444 linkage_name = attr.value_as_string ();
18445 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18446 See https://github.com/rust-lang/rust/issues/32925. */
18447 if (cu->language == language_rust && linkage_name != NULL
18448 && strchr (linkage_name, '{') != NULL)
18449 linkage_name = NULL;
18450 break;
18451 case DW_AT_low_pc:
18452 has_low_pc_attr = 1;
18453 lowpc = attr.value_as_address ();
18454 break;
18455 case DW_AT_high_pc:
18456 has_high_pc_attr = 1;
18457 highpc = attr.value_as_address ();
18458 if (cu->header.version >= 4 && attr.form_is_constant ())
18459 high_pc_relative = 1;
18460 break;
18461 case DW_AT_location:
18462 /* Support the .debug_loc offsets. */
18463 if (attr.form_is_block ())
18464 {
18465 d.locdesc = DW_BLOCK (&attr);
18466 }
18467 else if (attr.form_is_section_offset ())
18468 {
18469 dwarf2_complex_location_expr_complaint ();
18470 }
18471 else
18472 {
18473 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18474 "partial symbol information");
18475 }
18476 break;
18477 case DW_AT_external:
18478 is_external = DW_UNSND (&attr);
18479 break;
18480 case DW_AT_declaration:
18481 is_declaration = DW_UNSND (&attr);
18482 break;
18483 case DW_AT_type:
18484 has_type = 1;
18485 break;
18486 case DW_AT_abstract_origin:
18487 case DW_AT_specification:
18488 case DW_AT_extension:
18489 has_specification = 1;
18490 spec_offset = attr.get_ref_die_offset ();
18491 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18492 || cu->per_cu->is_dwz);
18493 break;
18494 case DW_AT_sibling:
18495 /* Ignore absolute siblings, they might point outside of
18496 the current compile unit. */
18497 if (attr.form == DW_FORM_ref_addr)
18498 complaint (_("ignoring absolute DW_AT_sibling"));
18499 else
18500 {
18501 const gdb_byte *buffer = reader->buffer;
18502 sect_offset off = attr.get_ref_die_offset ();
18503 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18504
18505 if (sibling_ptr < info_ptr)
18506 complaint (_("DW_AT_sibling points backwards"));
18507 else if (sibling_ptr > reader->buffer_end)
18508 reader->die_section->overflow_complaint ();
18509 else
18510 sibling = sibling_ptr;
18511 }
18512 break;
18513 case DW_AT_byte_size:
18514 has_byte_size = 1;
18515 break;
18516 case DW_AT_const_value:
18517 has_const_value = 1;
18518 break;
18519 case DW_AT_calling_convention:
18520 /* DWARF doesn't provide a way to identify a program's source-level
18521 entry point. DW_AT_calling_convention attributes are only meant
18522 to describe functions' calling conventions.
18523
18524 However, because it's a necessary piece of information in
18525 Fortran, and before DWARF 4 DW_CC_program was the only
18526 piece of debugging information whose definition refers to
18527 a 'main program' at all, several compilers marked Fortran
18528 main programs with DW_CC_program --- even when those
18529 functions use the standard calling conventions.
18530
18531 Although DWARF now specifies a way to provide this
18532 information, we support this practice for backward
18533 compatibility. */
18534 if (DW_UNSND (&attr) == DW_CC_program
18535 && cu->language == language_fortran)
18536 main_subprogram = 1;
18537 break;
18538 case DW_AT_inline:
18539 if (DW_UNSND (&attr) == DW_INL_inlined
18540 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18541 may_be_inlined = 1;
18542 break;
18543
18544 case DW_AT_import:
18545 if (tag == DW_TAG_imported_unit)
18546 {
18547 d.sect_off = attr.get_ref_die_offset ();
18548 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18549 || cu->per_cu->is_dwz);
18550 }
18551 break;
18552
18553 case DW_AT_main_subprogram:
18554 main_subprogram = DW_UNSND (&attr);
18555 break;
18556
18557 case DW_AT_ranges:
18558 {
18559 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18560 but that requires a full DIE, so instead we just
18561 reimplement it. */
18562 int need_ranges_base = tag != DW_TAG_compile_unit;
18563 unsigned int ranges_offset = (DW_UNSND (&attr)
18564 + (need_ranges_base
18565 ? cu->ranges_base
18566 : 0));
18567
18568 /* Value of the DW_AT_ranges attribute is the offset in the
18569 .debug_ranges section. */
18570 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18571 nullptr))
18572 has_pc_info = 1;
18573 }
18574 break;
18575
18576 default:
18577 break;
18578 }
18579 }
18580
18581 /* For Ada, if both the name and the linkage name appear, we prefer
18582 the latter. This lets "catch exception" work better, regardless
18583 of the order in which the name and linkage name were emitted.
18584 Really, though, this is just a workaround for the fact that gdb
18585 doesn't store both the name and the linkage name. */
18586 if (cu->language == language_ada && linkage_name != nullptr)
18587 name = linkage_name;
18588
18589 if (high_pc_relative)
18590 highpc += lowpc;
18591
18592 if (has_low_pc_attr && has_high_pc_attr)
18593 {
18594 /* When using the GNU linker, .gnu.linkonce. sections are used to
18595 eliminate duplicate copies of functions and vtables and such.
18596 The linker will arbitrarily choose one and discard the others.
18597 The AT_*_pc values for such functions refer to local labels in
18598 these sections. If the section from that file was discarded, the
18599 labels are not in the output, so the relocs get a value of 0.
18600 If this is a discarded function, mark the pc bounds as invalid,
18601 so that GDB will ignore it. */
18602 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
18603 {
18604 struct objfile *objfile = dwarf2_per_objfile->objfile;
18605 struct gdbarch *gdbarch = objfile->arch ();
18606
18607 complaint (_("DW_AT_low_pc %s is zero "
18608 "for DIE at %s [in module %s]"),
18609 paddress (gdbarch, lowpc),
18610 sect_offset_str (sect_off),
18611 objfile_name (objfile));
18612 }
18613 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18614 else if (lowpc >= highpc)
18615 {
18616 struct objfile *objfile = dwarf2_per_objfile->objfile;
18617 struct gdbarch *gdbarch = objfile->arch ();
18618
18619 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18620 "for DIE at %s [in module %s]"),
18621 paddress (gdbarch, lowpc),
18622 paddress (gdbarch, highpc),
18623 sect_offset_str (sect_off),
18624 objfile_name (objfile));
18625 }
18626 else
18627 has_pc_info = 1;
18628 }
18629
18630 return info_ptr;
18631 }
18632
18633 /* Find a cached partial DIE at OFFSET in CU. */
18634
18635 struct partial_die_info *
18636 dwarf2_cu::find_partial_die (sect_offset sect_off)
18637 {
18638 struct partial_die_info *lookup_die = NULL;
18639 struct partial_die_info part_die (sect_off);
18640
18641 lookup_die = ((struct partial_die_info *)
18642 htab_find_with_hash (partial_dies, &part_die,
18643 to_underlying (sect_off)));
18644
18645 return lookup_die;
18646 }
18647
18648 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18649 except in the case of .debug_types DIEs which do not reference
18650 outside their CU (they do however referencing other types via
18651 DW_FORM_ref_sig8). */
18652
18653 static const struct cu_partial_die_info
18654 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18655 {
18656 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18657 struct objfile *objfile = dwarf2_per_objfile->objfile;
18658 struct dwarf2_per_cu_data *per_cu = NULL;
18659 struct partial_die_info *pd = NULL;
18660
18661 if (offset_in_dwz == cu->per_cu->is_dwz
18662 && cu->header.offset_in_cu_p (sect_off))
18663 {
18664 pd = cu->find_partial_die (sect_off);
18665 if (pd != NULL)
18666 return { cu, pd };
18667 /* We missed recording what we needed.
18668 Load all dies and try again. */
18669 per_cu = cu->per_cu;
18670 }
18671 else
18672 {
18673 /* TUs don't reference other CUs/TUs (except via type signatures). */
18674 if (cu->per_cu->is_debug_types)
18675 {
18676 error (_("Dwarf Error: Type Unit at offset %s contains"
18677 " external reference to offset %s [in module %s].\n"),
18678 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18679 bfd_get_filename (objfile->obfd));
18680 }
18681 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18682 dwarf2_per_objfile);
18683
18684 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18685 load_partial_comp_unit (per_cu, cu->per_objfile);
18686
18687 per_cu->cu->last_used = 0;
18688 pd = per_cu->cu->find_partial_die (sect_off);
18689 }
18690
18691 /* If we didn't find it, and not all dies have been loaded,
18692 load them all and try again. */
18693
18694 if (pd == NULL && per_cu->load_all_dies == 0)
18695 {
18696 per_cu->load_all_dies = 1;
18697
18698 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18699 THIS_CU->cu may already be in use. So we can't just free it and
18700 replace its DIEs with the ones we read in. Instead, we leave those
18701 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18702 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18703 set. */
18704 load_partial_comp_unit (per_cu, cu->per_objfile);
18705
18706 pd = per_cu->cu->find_partial_die (sect_off);
18707 }
18708
18709 if (pd == NULL)
18710 internal_error (__FILE__, __LINE__,
18711 _("could not find partial DIE %s "
18712 "in cache [from module %s]\n"),
18713 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18714 return { per_cu->cu, pd };
18715 }
18716
18717 /* See if we can figure out if the class lives in a namespace. We do
18718 this by looking for a member function; its demangled name will
18719 contain namespace info, if there is any. */
18720
18721 static void
18722 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18723 struct dwarf2_cu *cu)
18724 {
18725 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18726 what template types look like, because the demangler
18727 frequently doesn't give the same name as the debug info. We
18728 could fix this by only using the demangled name to get the
18729 prefix (but see comment in read_structure_type). */
18730
18731 struct partial_die_info *real_pdi;
18732 struct partial_die_info *child_pdi;
18733
18734 /* If this DIE (this DIE's specification, if any) has a parent, then
18735 we should not do this. We'll prepend the parent's fully qualified
18736 name when we create the partial symbol. */
18737
18738 real_pdi = struct_pdi;
18739 while (real_pdi->has_specification)
18740 {
18741 auto res = find_partial_die (real_pdi->spec_offset,
18742 real_pdi->spec_is_dwz, cu);
18743 real_pdi = res.pdi;
18744 cu = res.cu;
18745 }
18746
18747 if (real_pdi->die_parent != NULL)
18748 return;
18749
18750 for (child_pdi = struct_pdi->die_child;
18751 child_pdi != NULL;
18752 child_pdi = child_pdi->die_sibling)
18753 {
18754 if (child_pdi->tag == DW_TAG_subprogram
18755 && child_pdi->linkage_name != NULL)
18756 {
18757 gdb::unique_xmalloc_ptr<char> actual_class_name
18758 (language_class_name_from_physname (cu->language_defn,
18759 child_pdi->linkage_name));
18760 if (actual_class_name != NULL)
18761 {
18762 struct objfile *objfile = cu->per_objfile->objfile;
18763 struct_pdi->name = objfile->intern (actual_class_name.get ());
18764 }
18765 break;
18766 }
18767 }
18768 }
18769
18770 /* Return true if a DIE with TAG may have the DW_AT_const_value
18771 attribute. */
18772
18773 static bool
18774 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18775 {
18776 switch (tag)
18777 {
18778 case DW_TAG_constant:
18779 case DW_TAG_enumerator:
18780 case DW_TAG_formal_parameter:
18781 case DW_TAG_template_value_param:
18782 case DW_TAG_variable:
18783 return true;
18784 }
18785
18786 return false;
18787 }
18788
18789 void
18790 partial_die_info::fixup (struct dwarf2_cu *cu)
18791 {
18792 /* Once we've fixed up a die, there's no point in doing so again.
18793 This also avoids a memory leak if we were to call
18794 guess_partial_die_structure_name multiple times. */
18795 if (fixup_called)
18796 return;
18797
18798 /* If we found a reference attribute and the DIE has no name, try
18799 to find a name in the referred to DIE. */
18800
18801 if (name == NULL && has_specification)
18802 {
18803 struct partial_die_info *spec_die;
18804
18805 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18806 spec_die = res.pdi;
18807 cu = res.cu;
18808
18809 spec_die->fixup (cu);
18810
18811 if (spec_die->name)
18812 {
18813 name = spec_die->name;
18814
18815 /* Copy DW_AT_external attribute if it is set. */
18816 if (spec_die->is_external)
18817 is_external = spec_die->is_external;
18818 }
18819 }
18820
18821 if (!has_const_value && has_specification
18822 && can_have_DW_AT_const_value_p (tag))
18823 {
18824 struct partial_die_info *spec_die;
18825
18826 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18827 spec_die = res.pdi;
18828 cu = res.cu;
18829
18830 spec_die->fixup (cu);
18831
18832 if (spec_die->has_const_value)
18833 {
18834 /* Copy DW_AT_const_value attribute if it is set. */
18835 has_const_value = spec_die->has_const_value;
18836 }
18837 }
18838
18839 /* Set default names for some unnamed DIEs. */
18840
18841 if (name == NULL && tag == DW_TAG_namespace)
18842 name = CP_ANONYMOUS_NAMESPACE_STR;
18843
18844 /* If there is no parent die to provide a namespace, and there are
18845 children, see if we can determine the namespace from their linkage
18846 name. */
18847 if (cu->language == language_cplus
18848 && !cu->per_objfile->per_bfd->types.empty ()
18849 && die_parent == NULL
18850 && has_children
18851 && (tag == DW_TAG_class_type
18852 || tag == DW_TAG_structure_type
18853 || tag == DW_TAG_union_type))
18854 guess_partial_die_structure_name (this, cu);
18855
18856 /* GCC might emit a nameless struct or union that has a linkage
18857 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18858 if (name == NULL
18859 && (tag == DW_TAG_class_type
18860 || tag == DW_TAG_interface_type
18861 || tag == DW_TAG_structure_type
18862 || tag == DW_TAG_union_type)
18863 && linkage_name != NULL)
18864 {
18865 gdb::unique_xmalloc_ptr<char> demangled
18866 (gdb_demangle (linkage_name, DMGL_TYPES));
18867 if (demangled != nullptr)
18868 {
18869 const char *base;
18870
18871 /* Strip any leading namespaces/classes, keep only the base name.
18872 DW_AT_name for named DIEs does not contain the prefixes. */
18873 base = strrchr (demangled.get (), ':');
18874 if (base && base > demangled.get () && base[-1] == ':')
18875 base++;
18876 else
18877 base = demangled.get ();
18878
18879 struct objfile *objfile = cu->per_objfile->objfile;
18880 name = objfile->intern (base);
18881 }
18882 }
18883
18884 fixup_called = 1;
18885 }
18886
18887 /* Read the .debug_loclists header contents from the given SECTION in the
18888 HEADER. */
18889 static void
18890 read_loclist_header (struct loclist_header *header,
18891 struct dwarf2_section_info *section)
18892 {
18893 unsigned int bytes_read;
18894 bfd *abfd = section->get_bfd_owner ();
18895 const gdb_byte *info_ptr = section->buffer;
18896 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18897 info_ptr += bytes_read;
18898 header->version = read_2_bytes (abfd, info_ptr);
18899 info_ptr += 2;
18900 header->addr_size = read_1_byte (abfd, info_ptr);
18901 info_ptr += 1;
18902 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18903 info_ptr += 1;
18904 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18905 }
18906
18907 /* Return the DW_AT_loclists_base value for the CU. */
18908 static ULONGEST
18909 lookup_loclist_base (struct dwarf2_cu *cu)
18910 {
18911 /* For the .dwo unit, the loclist_base points to the first offset following
18912 the header. The header consists of the following entities-
18913 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18914 bit format)
18915 2. version (2 bytes)
18916 3. address size (1 byte)
18917 4. segment selector size (1 byte)
18918 5. offset entry count (4 bytes)
18919 These sizes are derived as per the DWARFv5 standard. */
18920 if (cu->dwo_unit != nullptr)
18921 {
18922 if (cu->header.initial_length_size == 4)
18923 return LOCLIST_HEADER_SIZE32;
18924 return LOCLIST_HEADER_SIZE64;
18925 }
18926 return cu->loclist_base;
18927 }
18928
18929 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18930 array of offsets in the .debug_loclists section. */
18931 static CORE_ADDR
18932 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18933 {
18934 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18935 struct objfile *objfile = dwarf2_per_objfile->objfile;
18936 bfd *abfd = objfile->obfd;
18937 ULONGEST loclist_base = lookup_loclist_base (cu);
18938 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18939
18940 section->read (objfile);
18941 if (section->buffer == NULL)
18942 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18943 "section [in module %s]"), objfile_name (objfile));
18944 struct loclist_header header;
18945 read_loclist_header (&header, section);
18946 if (loclist_index >= header.offset_entry_count)
18947 complaint (_("DW_FORM_loclistx pointing outside of "
18948 ".debug_loclists offset array [in module %s]"),
18949 objfile_name (objfile));
18950 if (loclist_base + loclist_index * cu->header.offset_size
18951 >= section->size)
18952 complaint (_("DW_FORM_loclistx pointing outside of "
18953 ".debug_loclists section [in module %s]"),
18954 objfile_name (objfile));
18955 const gdb_byte *info_ptr
18956 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18957
18958 if (cu->header.offset_size == 4)
18959 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18960 else
18961 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18962 }
18963
18964 /* Process the attributes that had to be skipped in the first round. These
18965 attributes are the ones that need str_offsets_base or addr_base attributes.
18966 They could not have been processed in the first round, because at the time
18967 the values of str_offsets_base or addr_base may not have been known. */
18968 static void
18969 read_attribute_reprocess (const struct die_reader_specs *reader,
18970 struct attribute *attr)
18971 {
18972 struct dwarf2_cu *cu = reader->cu;
18973 switch (attr->form)
18974 {
18975 case DW_FORM_addrx:
18976 case DW_FORM_GNU_addr_index:
18977 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18978 break;
18979 case DW_FORM_loclistx:
18980 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18981 break;
18982 case DW_FORM_strx:
18983 case DW_FORM_strx1:
18984 case DW_FORM_strx2:
18985 case DW_FORM_strx3:
18986 case DW_FORM_strx4:
18987 case DW_FORM_GNU_str_index:
18988 {
18989 unsigned int str_index = DW_UNSND (attr);
18990 if (reader->dwo_file != NULL)
18991 {
18992 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18993 DW_STRING_IS_CANONICAL (attr) = 0;
18994 }
18995 else
18996 {
18997 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18998 DW_STRING_IS_CANONICAL (attr) = 0;
18999 }
19000 break;
19001 }
19002 default:
19003 gdb_assert_not_reached (_("Unexpected DWARF form."));
19004 }
19005 }
19006
19007 /* Read an attribute value described by an attribute form. */
19008
19009 static const gdb_byte *
19010 read_attribute_value (const struct die_reader_specs *reader,
19011 struct attribute *attr, unsigned form,
19012 LONGEST implicit_const, const gdb_byte *info_ptr,
19013 bool *need_reprocess)
19014 {
19015 struct dwarf2_cu *cu = reader->cu;
19016 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19017 struct objfile *objfile = dwarf2_per_objfile->objfile;
19018 bfd *abfd = reader->abfd;
19019 struct comp_unit_head *cu_header = &cu->header;
19020 unsigned int bytes_read;
19021 struct dwarf_block *blk;
19022 *need_reprocess = false;
19023
19024 attr->form = (enum dwarf_form) form;
19025 switch (form)
19026 {
19027 case DW_FORM_ref_addr:
19028 if (cu->header.version == 2)
19029 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19030 &bytes_read);
19031 else
19032 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19033 &bytes_read);
19034 info_ptr += bytes_read;
19035 break;
19036 case DW_FORM_GNU_ref_alt:
19037 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19038 info_ptr += bytes_read;
19039 break;
19040 case DW_FORM_addr:
19041 {
19042 struct gdbarch *gdbarch = objfile->arch ();
19043 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19044 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19045 info_ptr += bytes_read;
19046 }
19047 break;
19048 case DW_FORM_block2:
19049 blk = dwarf_alloc_block (cu);
19050 blk->size = read_2_bytes (abfd, info_ptr);
19051 info_ptr += 2;
19052 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19053 info_ptr += blk->size;
19054 DW_BLOCK (attr) = blk;
19055 break;
19056 case DW_FORM_block4:
19057 blk = dwarf_alloc_block (cu);
19058 blk->size = read_4_bytes (abfd, info_ptr);
19059 info_ptr += 4;
19060 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19061 info_ptr += blk->size;
19062 DW_BLOCK (attr) = blk;
19063 break;
19064 case DW_FORM_data2:
19065 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19066 info_ptr += 2;
19067 break;
19068 case DW_FORM_data4:
19069 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19070 info_ptr += 4;
19071 break;
19072 case DW_FORM_data8:
19073 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19074 info_ptr += 8;
19075 break;
19076 case DW_FORM_data16:
19077 blk = dwarf_alloc_block (cu);
19078 blk->size = 16;
19079 blk->data = read_n_bytes (abfd, info_ptr, 16);
19080 info_ptr += 16;
19081 DW_BLOCK (attr) = blk;
19082 break;
19083 case DW_FORM_sec_offset:
19084 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19085 info_ptr += bytes_read;
19086 break;
19087 case DW_FORM_loclistx:
19088 {
19089 *need_reprocess = true;
19090 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19091 info_ptr += bytes_read;
19092 }
19093 break;
19094 case DW_FORM_string:
19095 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19096 DW_STRING_IS_CANONICAL (attr) = 0;
19097 info_ptr += bytes_read;
19098 break;
19099 case DW_FORM_strp:
19100 if (!cu->per_cu->is_dwz)
19101 {
19102 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19103 abfd, info_ptr, cu_header,
19104 &bytes_read);
19105 DW_STRING_IS_CANONICAL (attr) = 0;
19106 info_ptr += bytes_read;
19107 break;
19108 }
19109 /* FALLTHROUGH */
19110 case DW_FORM_line_strp:
19111 if (!cu->per_cu->is_dwz)
19112 {
19113 DW_STRING (attr)
19114 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19115 &bytes_read);
19116 DW_STRING_IS_CANONICAL (attr) = 0;
19117 info_ptr += bytes_read;
19118 break;
19119 }
19120 /* FALLTHROUGH */
19121 case DW_FORM_GNU_strp_alt:
19122 {
19123 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19124 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19125 &bytes_read);
19126
19127 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
19128 DW_STRING_IS_CANONICAL (attr) = 0;
19129 info_ptr += bytes_read;
19130 }
19131 break;
19132 case DW_FORM_exprloc:
19133 case DW_FORM_block:
19134 blk = dwarf_alloc_block (cu);
19135 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19136 info_ptr += bytes_read;
19137 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19138 info_ptr += blk->size;
19139 DW_BLOCK (attr) = blk;
19140 break;
19141 case DW_FORM_block1:
19142 blk = dwarf_alloc_block (cu);
19143 blk->size = read_1_byte (abfd, info_ptr);
19144 info_ptr += 1;
19145 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19146 info_ptr += blk->size;
19147 DW_BLOCK (attr) = blk;
19148 break;
19149 case DW_FORM_data1:
19150 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19151 info_ptr += 1;
19152 break;
19153 case DW_FORM_flag:
19154 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19155 info_ptr += 1;
19156 break;
19157 case DW_FORM_flag_present:
19158 DW_UNSND (attr) = 1;
19159 break;
19160 case DW_FORM_sdata:
19161 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19162 info_ptr += bytes_read;
19163 break;
19164 case DW_FORM_udata:
19165 case DW_FORM_rnglistx:
19166 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19167 info_ptr += bytes_read;
19168 break;
19169 case DW_FORM_ref1:
19170 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19171 + read_1_byte (abfd, info_ptr));
19172 info_ptr += 1;
19173 break;
19174 case DW_FORM_ref2:
19175 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19176 + read_2_bytes (abfd, info_ptr));
19177 info_ptr += 2;
19178 break;
19179 case DW_FORM_ref4:
19180 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19181 + read_4_bytes (abfd, info_ptr));
19182 info_ptr += 4;
19183 break;
19184 case DW_FORM_ref8:
19185 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19186 + read_8_bytes (abfd, info_ptr));
19187 info_ptr += 8;
19188 break;
19189 case DW_FORM_ref_sig8:
19190 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19191 info_ptr += 8;
19192 break;
19193 case DW_FORM_ref_udata:
19194 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19195 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19196 info_ptr += bytes_read;
19197 break;
19198 case DW_FORM_indirect:
19199 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19200 info_ptr += bytes_read;
19201 if (form == DW_FORM_implicit_const)
19202 {
19203 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19204 info_ptr += bytes_read;
19205 }
19206 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19207 info_ptr, need_reprocess);
19208 break;
19209 case DW_FORM_implicit_const:
19210 DW_SND (attr) = implicit_const;
19211 break;
19212 case DW_FORM_addrx:
19213 case DW_FORM_GNU_addr_index:
19214 *need_reprocess = true;
19215 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19216 info_ptr += bytes_read;
19217 break;
19218 case DW_FORM_strx:
19219 case DW_FORM_strx1:
19220 case DW_FORM_strx2:
19221 case DW_FORM_strx3:
19222 case DW_FORM_strx4:
19223 case DW_FORM_GNU_str_index:
19224 {
19225 ULONGEST str_index;
19226 if (form == DW_FORM_strx1)
19227 {
19228 str_index = read_1_byte (abfd, info_ptr);
19229 info_ptr += 1;
19230 }
19231 else if (form == DW_FORM_strx2)
19232 {
19233 str_index = read_2_bytes (abfd, info_ptr);
19234 info_ptr += 2;
19235 }
19236 else if (form == DW_FORM_strx3)
19237 {
19238 str_index = read_3_bytes (abfd, info_ptr);
19239 info_ptr += 3;
19240 }
19241 else if (form == DW_FORM_strx4)
19242 {
19243 str_index = read_4_bytes (abfd, info_ptr);
19244 info_ptr += 4;
19245 }
19246 else
19247 {
19248 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19249 info_ptr += bytes_read;
19250 }
19251 *need_reprocess = true;
19252 DW_UNSND (attr) = str_index;
19253 }
19254 break;
19255 default:
19256 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19257 dwarf_form_name (form),
19258 bfd_get_filename (abfd));
19259 }
19260
19261 /* Super hack. */
19262 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19263 attr->form = DW_FORM_GNU_ref_alt;
19264
19265 /* We have seen instances where the compiler tried to emit a byte
19266 size attribute of -1 which ended up being encoded as an unsigned
19267 0xffffffff. Although 0xffffffff is technically a valid size value,
19268 an object of this size seems pretty unlikely so we can relatively
19269 safely treat these cases as if the size attribute was invalid and
19270 treat them as zero by default. */
19271 if (attr->name == DW_AT_byte_size
19272 && form == DW_FORM_data4
19273 && DW_UNSND (attr) >= 0xffffffff)
19274 {
19275 complaint
19276 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19277 hex_string (DW_UNSND (attr)));
19278 DW_UNSND (attr) = 0;
19279 }
19280
19281 return info_ptr;
19282 }
19283
19284 /* Read an attribute described by an abbreviated attribute. */
19285
19286 static const gdb_byte *
19287 read_attribute (const struct die_reader_specs *reader,
19288 struct attribute *attr, struct attr_abbrev *abbrev,
19289 const gdb_byte *info_ptr, bool *need_reprocess)
19290 {
19291 attr->name = abbrev->name;
19292 return read_attribute_value (reader, attr, abbrev->form,
19293 abbrev->implicit_const, info_ptr,
19294 need_reprocess);
19295 }
19296
19297 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19298
19299 static const char *
19300 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19301 LONGEST str_offset)
19302 {
19303 return dwarf2_per_objfile->per_bfd->str.read_string
19304 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
19305 }
19306
19307 /* Return pointer to string at .debug_str offset as read from BUF.
19308 BUF is assumed to be in a compilation unit described by CU_HEADER.
19309 Return *BYTES_READ_PTR count of bytes read from BUF. */
19310
19311 static const char *
19312 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19313 const gdb_byte *buf,
19314 const struct comp_unit_head *cu_header,
19315 unsigned int *bytes_read_ptr)
19316 {
19317 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19318
19319 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
19320 }
19321
19322 /* See read.h. */
19323
19324 const char *
19325 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19326 const struct comp_unit_head *cu_header,
19327 unsigned int *bytes_read_ptr)
19328 {
19329 bfd *abfd = objfile->obfd;
19330 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19331
19332 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19333 }
19334
19335 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19336 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19337 ADDR_SIZE is the size of addresses from the CU header. */
19338
19339 static CORE_ADDR
19340 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19341 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19342 int addr_size)
19343 {
19344 struct objfile *objfile = dwarf2_per_objfile->objfile;
19345 bfd *abfd = objfile->obfd;
19346 const gdb_byte *info_ptr;
19347 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19348
19349 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19350 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
19351 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19352 objfile_name (objfile));
19353 if (addr_base_or_zero + addr_index * addr_size
19354 >= dwarf2_per_objfile->per_bfd->addr.size)
19355 error (_("DW_FORM_addr_index pointing outside of "
19356 ".debug_addr section [in module %s]"),
19357 objfile_name (objfile));
19358 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
19359 + addr_base_or_zero + addr_index * addr_size);
19360 if (addr_size == 4)
19361 return bfd_get_32 (abfd, info_ptr);
19362 else
19363 return bfd_get_64 (abfd, info_ptr);
19364 }
19365
19366 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19367
19368 static CORE_ADDR
19369 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19370 {
19371 return read_addr_index_1 (cu->per_objfile, addr_index,
19372 cu->addr_base, cu->header.addr_size);
19373 }
19374
19375 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19376
19377 static CORE_ADDR
19378 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19379 unsigned int *bytes_read)
19380 {
19381 bfd *abfd = cu->per_objfile->objfile->obfd;
19382 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19383
19384 return read_addr_index (cu, addr_index);
19385 }
19386
19387 /* See read.h. */
19388
19389 CORE_ADDR
19390 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
19391 {
19392 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19393 struct dwarf2_cu *cu = per_cu->cu;
19394 gdb::optional<ULONGEST> addr_base;
19395 int addr_size;
19396
19397 /* We need addr_base and addr_size.
19398 If we don't have PER_CU->cu, we have to get it.
19399 Nasty, but the alternative is storing the needed info in PER_CU,
19400 which at this point doesn't seem justified: it's not clear how frequently
19401 it would get used and it would increase the size of every PER_CU.
19402 Entry points like dwarf2_per_cu_addr_size do a similar thing
19403 so we're not in uncharted territory here.
19404 Alas we need to be a bit more complicated as addr_base is contained
19405 in the DIE.
19406
19407 We don't need to read the entire CU(/TU).
19408 We just need the header and top level die.
19409
19410 IWBN to use the aging mechanism to let us lazily later discard the CU.
19411 For now we skip this optimization. */
19412
19413 if (cu != NULL)
19414 {
19415 addr_base = cu->addr_base;
19416 addr_size = cu->header.addr_size;
19417 }
19418 else
19419 {
19420 cutu_reader reader (per_cu, dwarf2_per_objfile, NULL, 0, false);
19421 addr_base = reader.cu->addr_base;
19422 addr_size = reader.cu->header.addr_size;
19423 }
19424
19425 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19426 addr_size);
19427 }
19428
19429 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19430 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19431 DWO file. */
19432
19433 static const char *
19434 read_str_index (struct dwarf2_cu *cu,
19435 struct dwarf2_section_info *str_section,
19436 struct dwarf2_section_info *str_offsets_section,
19437 ULONGEST str_offsets_base, ULONGEST str_index)
19438 {
19439 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19440 struct objfile *objfile = dwarf2_per_objfile->objfile;
19441 const char *objf_name = objfile_name (objfile);
19442 bfd *abfd = objfile->obfd;
19443 const gdb_byte *info_ptr;
19444 ULONGEST str_offset;
19445 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19446
19447 str_section->read (objfile);
19448 str_offsets_section->read (objfile);
19449 if (str_section->buffer == NULL)
19450 error (_("%s used without %s section"
19451 " in CU at offset %s [in module %s]"),
19452 form_name, str_section->get_name (),
19453 sect_offset_str (cu->header.sect_off), objf_name);
19454 if (str_offsets_section->buffer == NULL)
19455 error (_("%s used without %s section"
19456 " in CU at offset %s [in module %s]"),
19457 form_name, str_section->get_name (),
19458 sect_offset_str (cu->header.sect_off), objf_name);
19459 info_ptr = (str_offsets_section->buffer
19460 + str_offsets_base
19461 + str_index * cu->header.offset_size);
19462 if (cu->header.offset_size == 4)
19463 str_offset = bfd_get_32 (abfd, info_ptr);
19464 else
19465 str_offset = bfd_get_64 (abfd, info_ptr);
19466 if (str_offset >= str_section->size)
19467 error (_("Offset from %s pointing outside of"
19468 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19469 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19470 return (const char *) (str_section->buffer + str_offset);
19471 }
19472
19473 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19474
19475 static const char *
19476 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19477 {
19478 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19479 ? reader->cu->header.addr_size : 0;
19480 return read_str_index (reader->cu,
19481 &reader->dwo_file->sections.str,
19482 &reader->dwo_file->sections.str_offsets,
19483 str_offsets_base, str_index);
19484 }
19485
19486 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19487
19488 static const char *
19489 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19490 {
19491 struct objfile *objfile = cu->per_objfile->objfile;
19492 const char *objf_name = objfile_name (objfile);
19493 static const char form_name[] = "DW_FORM_GNU_str_index";
19494 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19495
19496 if (!cu->str_offsets_base.has_value ())
19497 error (_("%s used in Fission stub without %s"
19498 " in CU at offset 0x%lx [in module %s]"),
19499 form_name, str_offsets_attr_name,
19500 (long) cu->header.offset_size, objf_name);
19501
19502 return read_str_index (cu,
19503 &cu->per_objfile->per_bfd->str,
19504 &cu->per_objfile->per_bfd->str_offsets,
19505 *cu->str_offsets_base, str_index);
19506 }
19507
19508 /* Return the length of an LEB128 number in BUF. */
19509
19510 static int
19511 leb128_size (const gdb_byte *buf)
19512 {
19513 const gdb_byte *begin = buf;
19514 gdb_byte byte;
19515
19516 while (1)
19517 {
19518 byte = *buf++;
19519 if ((byte & 128) == 0)
19520 return buf - begin;
19521 }
19522 }
19523
19524 static void
19525 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19526 {
19527 switch (lang)
19528 {
19529 case DW_LANG_C89:
19530 case DW_LANG_C99:
19531 case DW_LANG_C11:
19532 case DW_LANG_C:
19533 case DW_LANG_UPC:
19534 cu->language = language_c;
19535 break;
19536 case DW_LANG_Java:
19537 case DW_LANG_C_plus_plus:
19538 case DW_LANG_C_plus_plus_11:
19539 case DW_LANG_C_plus_plus_14:
19540 cu->language = language_cplus;
19541 break;
19542 case DW_LANG_D:
19543 cu->language = language_d;
19544 break;
19545 case DW_LANG_Fortran77:
19546 case DW_LANG_Fortran90:
19547 case DW_LANG_Fortran95:
19548 case DW_LANG_Fortran03:
19549 case DW_LANG_Fortran08:
19550 cu->language = language_fortran;
19551 break;
19552 case DW_LANG_Go:
19553 cu->language = language_go;
19554 break;
19555 case DW_LANG_Mips_Assembler:
19556 cu->language = language_asm;
19557 break;
19558 case DW_LANG_Ada83:
19559 case DW_LANG_Ada95:
19560 cu->language = language_ada;
19561 break;
19562 case DW_LANG_Modula2:
19563 cu->language = language_m2;
19564 break;
19565 case DW_LANG_Pascal83:
19566 cu->language = language_pascal;
19567 break;
19568 case DW_LANG_ObjC:
19569 cu->language = language_objc;
19570 break;
19571 case DW_LANG_Rust:
19572 case DW_LANG_Rust_old:
19573 cu->language = language_rust;
19574 break;
19575 case DW_LANG_Cobol74:
19576 case DW_LANG_Cobol85:
19577 default:
19578 cu->language = language_minimal;
19579 break;
19580 }
19581 cu->language_defn = language_def (cu->language);
19582 }
19583
19584 /* Return the named attribute or NULL if not there. */
19585
19586 static struct attribute *
19587 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19588 {
19589 for (;;)
19590 {
19591 unsigned int i;
19592 struct attribute *spec = NULL;
19593
19594 for (i = 0; i < die->num_attrs; ++i)
19595 {
19596 if (die->attrs[i].name == name)
19597 return &die->attrs[i];
19598 if (die->attrs[i].name == DW_AT_specification
19599 || die->attrs[i].name == DW_AT_abstract_origin)
19600 spec = &die->attrs[i];
19601 }
19602
19603 if (!spec)
19604 break;
19605
19606 die = follow_die_ref (die, spec, &cu);
19607 }
19608
19609 return NULL;
19610 }
19611
19612 /* Return the string associated with a string-typed attribute, or NULL if it
19613 is either not found or is of an incorrect type. */
19614
19615 static const char *
19616 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19617 {
19618 struct attribute *attr;
19619 const char *str = NULL;
19620
19621 attr = dwarf2_attr (die, name, cu);
19622
19623 if (attr != NULL)
19624 {
19625 str = attr->value_as_string ();
19626 if (str == nullptr)
19627 complaint (_("string type expected for attribute %s for "
19628 "DIE at %s in module %s"),
19629 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19630 objfile_name (cu->per_objfile->objfile));
19631 }
19632
19633 return str;
19634 }
19635
19636 /* Return the dwo name or NULL if not present. If present, it is in either
19637 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19638 static const char *
19639 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19640 {
19641 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19642 if (dwo_name == nullptr)
19643 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19644 return dwo_name;
19645 }
19646
19647 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19648 and holds a non-zero value. This function should only be used for
19649 DW_FORM_flag or DW_FORM_flag_present attributes. */
19650
19651 static int
19652 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19653 {
19654 struct attribute *attr = dwarf2_attr (die, name, cu);
19655
19656 return (attr && DW_UNSND (attr));
19657 }
19658
19659 static int
19660 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19661 {
19662 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19663 which value is non-zero. However, we have to be careful with
19664 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19665 (via dwarf2_flag_true_p) follows this attribute. So we may
19666 end up accidently finding a declaration attribute that belongs
19667 to a different DIE referenced by the specification attribute,
19668 even though the given DIE does not have a declaration attribute. */
19669 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19670 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19671 }
19672
19673 /* Return the die giving the specification for DIE, if there is
19674 one. *SPEC_CU is the CU containing DIE on input, and the CU
19675 containing the return value on output. If there is no
19676 specification, but there is an abstract origin, that is
19677 returned. */
19678
19679 static struct die_info *
19680 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19681 {
19682 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19683 *spec_cu);
19684
19685 if (spec_attr == NULL)
19686 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19687
19688 if (spec_attr == NULL)
19689 return NULL;
19690 else
19691 return follow_die_ref (die, spec_attr, spec_cu);
19692 }
19693
19694 /* Stub for free_line_header to match void * callback types. */
19695
19696 static void
19697 free_line_header_voidp (void *arg)
19698 {
19699 struct line_header *lh = (struct line_header *) arg;
19700
19701 delete lh;
19702 }
19703
19704 /* A convenience function to find the proper .debug_line section for a CU. */
19705
19706 static struct dwarf2_section_info *
19707 get_debug_line_section (struct dwarf2_cu *cu)
19708 {
19709 struct dwarf2_section_info *section;
19710 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19711
19712 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19713 DWO file. */
19714 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19715 section = &cu->dwo_unit->dwo_file->sections.line;
19716 else if (cu->per_cu->is_dwz)
19717 {
19718 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19719
19720 section = &dwz->line;
19721 }
19722 else
19723 section = &dwarf2_per_objfile->per_bfd->line;
19724
19725 return section;
19726 }
19727
19728 /* Read the statement program header starting at OFFSET in
19729 .debug_line, or .debug_line.dwo. Return a pointer
19730 to a struct line_header, allocated using xmalloc.
19731 Returns NULL if there is a problem reading the header, e.g., if it
19732 has a version we don't understand.
19733
19734 NOTE: the strings in the include directory and file name tables of
19735 the returned object point into the dwarf line section buffer,
19736 and must not be freed. */
19737
19738 static line_header_up
19739 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19740 {
19741 struct dwarf2_section_info *section;
19742 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19743
19744 section = get_debug_line_section (cu);
19745 section->read (dwarf2_per_objfile->objfile);
19746 if (section->buffer == NULL)
19747 {
19748 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19749 complaint (_("missing .debug_line.dwo section"));
19750 else
19751 complaint (_("missing .debug_line section"));
19752 return 0;
19753 }
19754
19755 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19756 dwarf2_per_objfile, section,
19757 &cu->header);
19758 }
19759
19760 /* Subroutine of dwarf_decode_lines to simplify it.
19761 Return the file name of the psymtab for the given file_entry.
19762 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19763 If space for the result is malloc'd, *NAME_HOLDER will be set.
19764 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19765
19766 static const char *
19767 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19768 const dwarf2_psymtab *pst,
19769 const char *comp_dir,
19770 gdb::unique_xmalloc_ptr<char> *name_holder)
19771 {
19772 const char *include_name = fe.name;
19773 const char *include_name_to_compare = include_name;
19774 const char *pst_filename;
19775 int file_is_pst;
19776
19777 const char *dir_name = fe.include_dir (lh);
19778
19779 gdb::unique_xmalloc_ptr<char> hold_compare;
19780 if (!IS_ABSOLUTE_PATH (include_name)
19781 && (dir_name != NULL || comp_dir != NULL))
19782 {
19783 /* Avoid creating a duplicate psymtab for PST.
19784 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19785 Before we do the comparison, however, we need to account
19786 for DIR_NAME and COMP_DIR.
19787 First prepend dir_name (if non-NULL). If we still don't
19788 have an absolute path prepend comp_dir (if non-NULL).
19789 However, the directory we record in the include-file's
19790 psymtab does not contain COMP_DIR (to match the
19791 corresponding symtab(s)).
19792
19793 Example:
19794
19795 bash$ cd /tmp
19796 bash$ gcc -g ./hello.c
19797 include_name = "hello.c"
19798 dir_name = "."
19799 DW_AT_comp_dir = comp_dir = "/tmp"
19800 DW_AT_name = "./hello.c"
19801
19802 */
19803
19804 if (dir_name != NULL)
19805 {
19806 name_holder->reset (concat (dir_name, SLASH_STRING,
19807 include_name, (char *) NULL));
19808 include_name = name_holder->get ();
19809 include_name_to_compare = include_name;
19810 }
19811 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19812 {
19813 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19814 include_name, (char *) NULL));
19815 include_name_to_compare = hold_compare.get ();
19816 }
19817 }
19818
19819 pst_filename = pst->filename;
19820 gdb::unique_xmalloc_ptr<char> copied_name;
19821 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19822 {
19823 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19824 pst_filename, (char *) NULL));
19825 pst_filename = copied_name.get ();
19826 }
19827
19828 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19829
19830 if (file_is_pst)
19831 return NULL;
19832 return include_name;
19833 }
19834
19835 /* State machine to track the state of the line number program. */
19836
19837 class lnp_state_machine
19838 {
19839 public:
19840 /* Initialize a machine state for the start of a line number
19841 program. */
19842 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19843 bool record_lines_p);
19844
19845 file_entry *current_file ()
19846 {
19847 /* lh->file_names is 0-based, but the file name numbers in the
19848 statement program are 1-based. */
19849 return m_line_header->file_name_at (m_file);
19850 }
19851
19852 /* Record the line in the state machine. END_SEQUENCE is true if
19853 we're processing the end of a sequence. */
19854 void record_line (bool end_sequence);
19855
19856 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19857 nop-out rest of the lines in this sequence. */
19858 void check_line_address (struct dwarf2_cu *cu,
19859 const gdb_byte *line_ptr,
19860 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19861
19862 void handle_set_discriminator (unsigned int discriminator)
19863 {
19864 m_discriminator = discriminator;
19865 m_line_has_non_zero_discriminator |= discriminator != 0;
19866 }
19867
19868 /* Handle DW_LNE_set_address. */
19869 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19870 {
19871 m_op_index = 0;
19872 address += baseaddr;
19873 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19874 }
19875
19876 /* Handle DW_LNS_advance_pc. */
19877 void handle_advance_pc (CORE_ADDR adjust);
19878
19879 /* Handle a special opcode. */
19880 void handle_special_opcode (unsigned char op_code);
19881
19882 /* Handle DW_LNS_advance_line. */
19883 void handle_advance_line (int line_delta)
19884 {
19885 advance_line (line_delta);
19886 }
19887
19888 /* Handle DW_LNS_set_file. */
19889 void handle_set_file (file_name_index file);
19890
19891 /* Handle DW_LNS_negate_stmt. */
19892 void handle_negate_stmt ()
19893 {
19894 m_is_stmt = !m_is_stmt;
19895 }
19896
19897 /* Handle DW_LNS_const_add_pc. */
19898 void handle_const_add_pc ();
19899
19900 /* Handle DW_LNS_fixed_advance_pc. */
19901 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19902 {
19903 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19904 m_op_index = 0;
19905 }
19906
19907 /* Handle DW_LNS_copy. */
19908 void handle_copy ()
19909 {
19910 record_line (false);
19911 m_discriminator = 0;
19912 }
19913
19914 /* Handle DW_LNE_end_sequence. */
19915 void handle_end_sequence ()
19916 {
19917 m_currently_recording_lines = true;
19918 }
19919
19920 private:
19921 /* Advance the line by LINE_DELTA. */
19922 void advance_line (int line_delta)
19923 {
19924 m_line += line_delta;
19925
19926 if (line_delta != 0)
19927 m_line_has_non_zero_discriminator = m_discriminator != 0;
19928 }
19929
19930 struct dwarf2_cu *m_cu;
19931
19932 gdbarch *m_gdbarch;
19933
19934 /* True if we're recording lines.
19935 Otherwise we're building partial symtabs and are just interested in
19936 finding include files mentioned by the line number program. */
19937 bool m_record_lines_p;
19938
19939 /* The line number header. */
19940 line_header *m_line_header;
19941
19942 /* These are part of the standard DWARF line number state machine,
19943 and initialized according to the DWARF spec. */
19944
19945 unsigned char m_op_index = 0;
19946 /* The line table index of the current file. */
19947 file_name_index m_file = 1;
19948 unsigned int m_line = 1;
19949
19950 /* These are initialized in the constructor. */
19951
19952 CORE_ADDR m_address;
19953 bool m_is_stmt;
19954 unsigned int m_discriminator;
19955
19956 /* Additional bits of state we need to track. */
19957
19958 /* The last file that we called dwarf2_start_subfile for.
19959 This is only used for TLLs. */
19960 unsigned int m_last_file = 0;
19961 /* The last file a line number was recorded for. */
19962 struct subfile *m_last_subfile = NULL;
19963
19964 /* When true, record the lines we decode. */
19965 bool m_currently_recording_lines = false;
19966
19967 /* The last line number that was recorded, used to coalesce
19968 consecutive entries for the same line. This can happen, for
19969 example, when discriminators are present. PR 17276. */
19970 unsigned int m_last_line = 0;
19971 bool m_line_has_non_zero_discriminator = false;
19972 };
19973
19974 void
19975 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19976 {
19977 CORE_ADDR addr_adj = (((m_op_index + adjust)
19978 / m_line_header->maximum_ops_per_instruction)
19979 * m_line_header->minimum_instruction_length);
19980 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19981 m_op_index = ((m_op_index + adjust)
19982 % m_line_header->maximum_ops_per_instruction);
19983 }
19984
19985 void
19986 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19987 {
19988 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19989 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19990 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19991 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19992 / m_line_header->maximum_ops_per_instruction)
19993 * m_line_header->minimum_instruction_length);
19994 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19995 m_op_index = ((m_op_index + adj_opcode_d)
19996 % m_line_header->maximum_ops_per_instruction);
19997
19998 int line_delta = m_line_header->line_base + adj_opcode_r;
19999 advance_line (line_delta);
20000 record_line (false);
20001 m_discriminator = 0;
20002 }
20003
20004 void
20005 lnp_state_machine::handle_set_file (file_name_index file)
20006 {
20007 m_file = file;
20008
20009 const file_entry *fe = current_file ();
20010 if (fe == NULL)
20011 dwarf2_debug_line_missing_file_complaint ();
20012 else if (m_record_lines_p)
20013 {
20014 const char *dir = fe->include_dir (m_line_header);
20015
20016 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20017 m_line_has_non_zero_discriminator = m_discriminator != 0;
20018 dwarf2_start_subfile (m_cu, fe->name, dir);
20019 }
20020 }
20021
20022 void
20023 lnp_state_machine::handle_const_add_pc ()
20024 {
20025 CORE_ADDR adjust
20026 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20027
20028 CORE_ADDR addr_adj
20029 = (((m_op_index + adjust)
20030 / m_line_header->maximum_ops_per_instruction)
20031 * m_line_header->minimum_instruction_length);
20032
20033 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20034 m_op_index = ((m_op_index + adjust)
20035 % m_line_header->maximum_ops_per_instruction);
20036 }
20037
20038 /* Return non-zero if we should add LINE to the line number table.
20039 LINE is the line to add, LAST_LINE is the last line that was added,
20040 LAST_SUBFILE is the subfile for LAST_LINE.
20041 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20042 had a non-zero discriminator.
20043
20044 We have to be careful in the presence of discriminators.
20045 E.g., for this line:
20046
20047 for (i = 0; i < 100000; i++);
20048
20049 clang can emit four line number entries for that one line,
20050 each with a different discriminator.
20051 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20052
20053 However, we want gdb to coalesce all four entries into one.
20054 Otherwise the user could stepi into the middle of the line and
20055 gdb would get confused about whether the pc really was in the
20056 middle of the line.
20057
20058 Things are further complicated by the fact that two consecutive
20059 line number entries for the same line is a heuristic used by gcc
20060 to denote the end of the prologue. So we can't just discard duplicate
20061 entries, we have to be selective about it. The heuristic we use is
20062 that we only collapse consecutive entries for the same line if at least
20063 one of those entries has a non-zero discriminator. PR 17276.
20064
20065 Note: Addresses in the line number state machine can never go backwards
20066 within one sequence, thus this coalescing is ok. */
20067
20068 static int
20069 dwarf_record_line_p (struct dwarf2_cu *cu,
20070 unsigned int line, unsigned int last_line,
20071 int line_has_non_zero_discriminator,
20072 struct subfile *last_subfile)
20073 {
20074 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20075 return 1;
20076 if (line != last_line)
20077 return 1;
20078 /* Same line for the same file that we've seen already.
20079 As a last check, for pr 17276, only record the line if the line
20080 has never had a non-zero discriminator. */
20081 if (!line_has_non_zero_discriminator)
20082 return 1;
20083 return 0;
20084 }
20085
20086 /* Use the CU's builder to record line number LINE beginning at
20087 address ADDRESS in the line table of subfile SUBFILE. */
20088
20089 static void
20090 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20091 unsigned int line, CORE_ADDR address, bool is_stmt,
20092 struct dwarf2_cu *cu)
20093 {
20094 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20095
20096 if (dwarf_line_debug)
20097 {
20098 fprintf_unfiltered (gdb_stdlog,
20099 "Recording line %u, file %s, address %s\n",
20100 line, lbasename (subfile->name),
20101 paddress (gdbarch, address));
20102 }
20103
20104 if (cu != nullptr)
20105 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20106 }
20107
20108 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20109 Mark the end of a set of line number records.
20110 The arguments are the same as for dwarf_record_line_1.
20111 If SUBFILE is NULL the request is ignored. */
20112
20113 static void
20114 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20115 CORE_ADDR address, struct dwarf2_cu *cu)
20116 {
20117 if (subfile == NULL)
20118 return;
20119
20120 if (dwarf_line_debug)
20121 {
20122 fprintf_unfiltered (gdb_stdlog,
20123 "Finishing current line, file %s, address %s\n",
20124 lbasename (subfile->name),
20125 paddress (gdbarch, address));
20126 }
20127
20128 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20129 }
20130
20131 void
20132 lnp_state_machine::record_line (bool end_sequence)
20133 {
20134 if (dwarf_line_debug)
20135 {
20136 fprintf_unfiltered (gdb_stdlog,
20137 "Processing actual line %u: file %u,"
20138 " address %s, is_stmt %u, discrim %u%s\n",
20139 m_line, m_file,
20140 paddress (m_gdbarch, m_address),
20141 m_is_stmt, m_discriminator,
20142 (end_sequence ? "\t(end sequence)" : ""));
20143 }
20144
20145 file_entry *fe = current_file ();
20146
20147 if (fe == NULL)
20148 dwarf2_debug_line_missing_file_complaint ();
20149 /* For now we ignore lines not starting on an instruction boundary.
20150 But not when processing end_sequence for compatibility with the
20151 previous version of the code. */
20152 else if (m_op_index == 0 || end_sequence)
20153 {
20154 fe->included_p = 1;
20155 if (m_record_lines_p)
20156 {
20157 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20158 || end_sequence)
20159 {
20160 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20161 m_currently_recording_lines ? m_cu : nullptr);
20162 }
20163
20164 if (!end_sequence)
20165 {
20166 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20167
20168 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20169 m_line_has_non_zero_discriminator,
20170 m_last_subfile))
20171 {
20172 buildsym_compunit *builder = m_cu->get_builder ();
20173 dwarf_record_line_1 (m_gdbarch,
20174 builder->get_current_subfile (),
20175 m_line, m_address, is_stmt,
20176 m_currently_recording_lines ? m_cu : nullptr);
20177 }
20178 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20179 m_last_line = m_line;
20180 }
20181 }
20182 }
20183 }
20184
20185 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20186 line_header *lh, bool record_lines_p)
20187 {
20188 m_cu = cu;
20189 m_gdbarch = arch;
20190 m_record_lines_p = record_lines_p;
20191 m_line_header = lh;
20192
20193 m_currently_recording_lines = true;
20194
20195 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20196 was a line entry for it so that the backend has a chance to adjust it
20197 and also record it in case it needs it. This is currently used by MIPS
20198 code, cf. `mips_adjust_dwarf2_line'. */
20199 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20200 m_is_stmt = lh->default_is_stmt;
20201 m_discriminator = 0;
20202 }
20203
20204 void
20205 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20206 const gdb_byte *line_ptr,
20207 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20208 {
20209 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20210 the pc range of the CU. However, we restrict the test to only ADDRESS
20211 values of zero to preserve GDB's previous behaviour which is to handle
20212 the specific case of a function being GC'd by the linker. */
20213
20214 if (address == 0 && address < unrelocated_lowpc)
20215 {
20216 /* This line table is for a function which has been
20217 GCd by the linker. Ignore it. PR gdb/12528 */
20218
20219 struct objfile *objfile = cu->per_objfile->objfile;
20220 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20221
20222 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20223 line_offset, objfile_name (objfile));
20224 m_currently_recording_lines = false;
20225 /* Note: m_currently_recording_lines is left as false until we see
20226 DW_LNE_end_sequence. */
20227 }
20228 }
20229
20230 /* Subroutine of dwarf_decode_lines to simplify it.
20231 Process the line number information in LH.
20232 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20233 program in order to set included_p for every referenced header. */
20234
20235 static void
20236 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20237 const int decode_for_pst_p, CORE_ADDR lowpc)
20238 {
20239 const gdb_byte *line_ptr, *extended_end;
20240 const gdb_byte *line_end;
20241 unsigned int bytes_read, extended_len;
20242 unsigned char op_code, extended_op;
20243 CORE_ADDR baseaddr;
20244 struct objfile *objfile = cu->per_objfile->objfile;
20245 bfd *abfd = objfile->obfd;
20246 struct gdbarch *gdbarch = objfile->arch ();
20247 /* True if we're recording line info (as opposed to building partial
20248 symtabs and just interested in finding include files mentioned by
20249 the line number program). */
20250 bool record_lines_p = !decode_for_pst_p;
20251
20252 baseaddr = objfile->text_section_offset ();
20253
20254 line_ptr = lh->statement_program_start;
20255 line_end = lh->statement_program_end;
20256
20257 /* Read the statement sequences until there's nothing left. */
20258 while (line_ptr < line_end)
20259 {
20260 /* The DWARF line number program state machine. Reset the state
20261 machine at the start of each sequence. */
20262 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20263 bool end_sequence = false;
20264
20265 if (record_lines_p)
20266 {
20267 /* Start a subfile for the current file of the state
20268 machine. */
20269 const file_entry *fe = state_machine.current_file ();
20270
20271 if (fe != NULL)
20272 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20273 }
20274
20275 /* Decode the table. */
20276 while (line_ptr < line_end && !end_sequence)
20277 {
20278 op_code = read_1_byte (abfd, line_ptr);
20279 line_ptr += 1;
20280
20281 if (op_code >= lh->opcode_base)
20282 {
20283 /* Special opcode. */
20284 state_machine.handle_special_opcode (op_code);
20285 }
20286 else switch (op_code)
20287 {
20288 case DW_LNS_extended_op:
20289 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20290 &bytes_read);
20291 line_ptr += bytes_read;
20292 extended_end = line_ptr + extended_len;
20293 extended_op = read_1_byte (abfd, line_ptr);
20294 line_ptr += 1;
20295 switch (extended_op)
20296 {
20297 case DW_LNE_end_sequence:
20298 state_machine.handle_end_sequence ();
20299 end_sequence = true;
20300 break;
20301 case DW_LNE_set_address:
20302 {
20303 CORE_ADDR address
20304 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20305 line_ptr += bytes_read;
20306
20307 state_machine.check_line_address (cu, line_ptr,
20308 lowpc - baseaddr, address);
20309 state_machine.handle_set_address (baseaddr, address);
20310 }
20311 break;
20312 case DW_LNE_define_file:
20313 {
20314 const char *cur_file;
20315 unsigned int mod_time, length;
20316 dir_index dindex;
20317
20318 cur_file = read_direct_string (abfd, line_ptr,
20319 &bytes_read);
20320 line_ptr += bytes_read;
20321 dindex = (dir_index)
20322 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20323 line_ptr += bytes_read;
20324 mod_time =
20325 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20326 line_ptr += bytes_read;
20327 length =
20328 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20329 line_ptr += bytes_read;
20330 lh->add_file_name (cur_file, dindex, mod_time, length);
20331 }
20332 break;
20333 case DW_LNE_set_discriminator:
20334 {
20335 /* The discriminator is not interesting to the
20336 debugger; just ignore it. We still need to
20337 check its value though:
20338 if there are consecutive entries for the same
20339 (non-prologue) line we want to coalesce them.
20340 PR 17276. */
20341 unsigned int discr
20342 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20343 line_ptr += bytes_read;
20344
20345 state_machine.handle_set_discriminator (discr);
20346 }
20347 break;
20348 default:
20349 complaint (_("mangled .debug_line section"));
20350 return;
20351 }
20352 /* Make sure that we parsed the extended op correctly. If e.g.
20353 we expected a different address size than the producer used,
20354 we may have read the wrong number of bytes. */
20355 if (line_ptr != extended_end)
20356 {
20357 complaint (_("mangled .debug_line section"));
20358 return;
20359 }
20360 break;
20361 case DW_LNS_copy:
20362 state_machine.handle_copy ();
20363 break;
20364 case DW_LNS_advance_pc:
20365 {
20366 CORE_ADDR adjust
20367 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20368 line_ptr += bytes_read;
20369
20370 state_machine.handle_advance_pc (adjust);
20371 }
20372 break;
20373 case DW_LNS_advance_line:
20374 {
20375 int line_delta
20376 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20377 line_ptr += bytes_read;
20378
20379 state_machine.handle_advance_line (line_delta);
20380 }
20381 break;
20382 case DW_LNS_set_file:
20383 {
20384 file_name_index file
20385 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20386 &bytes_read);
20387 line_ptr += bytes_read;
20388
20389 state_machine.handle_set_file (file);
20390 }
20391 break;
20392 case DW_LNS_set_column:
20393 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20394 line_ptr += bytes_read;
20395 break;
20396 case DW_LNS_negate_stmt:
20397 state_machine.handle_negate_stmt ();
20398 break;
20399 case DW_LNS_set_basic_block:
20400 break;
20401 /* Add to the address register of the state machine the
20402 address increment value corresponding to special opcode
20403 255. I.e., this value is scaled by the minimum
20404 instruction length since special opcode 255 would have
20405 scaled the increment. */
20406 case DW_LNS_const_add_pc:
20407 state_machine.handle_const_add_pc ();
20408 break;
20409 case DW_LNS_fixed_advance_pc:
20410 {
20411 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20412 line_ptr += 2;
20413
20414 state_machine.handle_fixed_advance_pc (addr_adj);
20415 }
20416 break;
20417 default:
20418 {
20419 /* Unknown standard opcode, ignore it. */
20420 int i;
20421
20422 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20423 {
20424 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20425 line_ptr += bytes_read;
20426 }
20427 }
20428 }
20429 }
20430
20431 if (!end_sequence)
20432 dwarf2_debug_line_missing_end_sequence_complaint ();
20433
20434 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20435 in which case we still finish recording the last line). */
20436 state_machine.record_line (true);
20437 }
20438 }
20439
20440 /* Decode the Line Number Program (LNP) for the given line_header
20441 structure and CU. The actual information extracted and the type
20442 of structures created from the LNP depends on the value of PST.
20443
20444 1. If PST is NULL, then this procedure uses the data from the program
20445 to create all necessary symbol tables, and their linetables.
20446
20447 2. If PST is not NULL, this procedure reads the program to determine
20448 the list of files included by the unit represented by PST, and
20449 builds all the associated partial symbol tables.
20450
20451 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20452 It is used for relative paths in the line table.
20453 NOTE: When processing partial symtabs (pst != NULL),
20454 comp_dir == pst->dirname.
20455
20456 NOTE: It is important that psymtabs have the same file name (via strcmp)
20457 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20458 symtab we don't use it in the name of the psymtabs we create.
20459 E.g. expand_line_sal requires this when finding psymtabs to expand.
20460 A good testcase for this is mb-inline.exp.
20461
20462 LOWPC is the lowest address in CU (or 0 if not known).
20463
20464 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20465 for its PC<->lines mapping information. Otherwise only the filename
20466 table is read in. */
20467
20468 static void
20469 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20470 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20471 CORE_ADDR lowpc, int decode_mapping)
20472 {
20473 struct objfile *objfile = cu->per_objfile->objfile;
20474 const int decode_for_pst_p = (pst != NULL);
20475
20476 if (decode_mapping)
20477 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20478
20479 if (decode_for_pst_p)
20480 {
20481 /* Now that we're done scanning the Line Header Program, we can
20482 create the psymtab of each included file. */
20483 for (auto &file_entry : lh->file_names ())
20484 if (file_entry.included_p == 1)
20485 {
20486 gdb::unique_xmalloc_ptr<char> name_holder;
20487 const char *include_name =
20488 psymtab_include_file_name (lh, file_entry, pst,
20489 comp_dir, &name_holder);
20490 if (include_name != NULL)
20491 dwarf2_create_include_psymtab (include_name, pst, objfile);
20492 }
20493 }
20494 else
20495 {
20496 /* Make sure a symtab is created for every file, even files
20497 which contain only variables (i.e. no code with associated
20498 line numbers). */
20499 buildsym_compunit *builder = cu->get_builder ();
20500 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20501
20502 for (auto &fe : lh->file_names ())
20503 {
20504 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20505 if (builder->get_current_subfile ()->symtab == NULL)
20506 {
20507 builder->get_current_subfile ()->symtab
20508 = allocate_symtab (cust,
20509 builder->get_current_subfile ()->name);
20510 }
20511 fe.symtab = builder->get_current_subfile ()->symtab;
20512 }
20513 }
20514 }
20515
20516 /* Start a subfile for DWARF. FILENAME is the name of the file and
20517 DIRNAME the name of the source directory which contains FILENAME
20518 or NULL if not known.
20519 This routine tries to keep line numbers from identical absolute and
20520 relative file names in a common subfile.
20521
20522 Using the `list' example from the GDB testsuite, which resides in
20523 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20524 of /srcdir/list0.c yields the following debugging information for list0.c:
20525
20526 DW_AT_name: /srcdir/list0.c
20527 DW_AT_comp_dir: /compdir
20528 files.files[0].name: list0.h
20529 files.files[0].dir: /srcdir
20530 files.files[1].name: list0.c
20531 files.files[1].dir: /srcdir
20532
20533 The line number information for list0.c has to end up in a single
20534 subfile, so that `break /srcdir/list0.c:1' works as expected.
20535 start_subfile will ensure that this happens provided that we pass the
20536 concatenation of files.files[1].dir and files.files[1].name as the
20537 subfile's name. */
20538
20539 static void
20540 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20541 const char *dirname)
20542 {
20543 gdb::unique_xmalloc_ptr<char> copy;
20544
20545 /* In order not to lose the line information directory,
20546 we concatenate it to the filename when it makes sense.
20547 Note that the Dwarf3 standard says (speaking of filenames in line
20548 information): ``The directory index is ignored for file names
20549 that represent full path names''. Thus ignoring dirname in the
20550 `else' branch below isn't an issue. */
20551
20552 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20553 {
20554 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20555 filename = copy.get ();
20556 }
20557
20558 cu->get_builder ()->start_subfile (filename);
20559 }
20560
20561 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20562 buildsym_compunit constructor. */
20563
20564 struct compunit_symtab *
20565 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20566 CORE_ADDR low_pc)
20567 {
20568 gdb_assert (m_builder == nullptr);
20569
20570 m_builder.reset (new struct buildsym_compunit
20571 (per_cu->dwarf2_per_objfile->objfile,
20572 name, comp_dir, language, low_pc));
20573
20574 list_in_scope = get_builder ()->get_file_symbols ();
20575
20576 get_builder ()->record_debugformat ("DWARF 2");
20577 get_builder ()->record_producer (producer);
20578
20579 processing_has_namespace_info = false;
20580
20581 return get_builder ()->get_compunit_symtab ();
20582 }
20583
20584 static void
20585 var_decode_location (struct attribute *attr, struct symbol *sym,
20586 struct dwarf2_cu *cu)
20587 {
20588 struct objfile *objfile = cu->per_objfile->objfile;
20589 struct comp_unit_head *cu_header = &cu->header;
20590
20591 /* NOTE drow/2003-01-30: There used to be a comment and some special
20592 code here to turn a symbol with DW_AT_external and a
20593 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20594 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20595 with some versions of binutils) where shared libraries could have
20596 relocations against symbols in their debug information - the
20597 minimal symbol would have the right address, but the debug info
20598 would not. It's no longer necessary, because we will explicitly
20599 apply relocations when we read in the debug information now. */
20600
20601 /* A DW_AT_location attribute with no contents indicates that a
20602 variable has been optimized away. */
20603 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20604 {
20605 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20606 return;
20607 }
20608
20609 /* Handle one degenerate form of location expression specially, to
20610 preserve GDB's previous behavior when section offsets are
20611 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20612 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20613
20614 if (attr->form_is_block ()
20615 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20616 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20617 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20618 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20619 && (DW_BLOCK (attr)->size
20620 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20621 {
20622 unsigned int dummy;
20623
20624 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20625 SET_SYMBOL_VALUE_ADDRESS
20626 (sym, cu->header.read_address (objfile->obfd,
20627 DW_BLOCK (attr)->data + 1,
20628 &dummy));
20629 else
20630 SET_SYMBOL_VALUE_ADDRESS
20631 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20632 &dummy));
20633 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20634 fixup_symbol_section (sym, objfile);
20635 SET_SYMBOL_VALUE_ADDRESS
20636 (sym,
20637 SYMBOL_VALUE_ADDRESS (sym)
20638 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20639 return;
20640 }
20641
20642 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20643 expression evaluator, and use LOC_COMPUTED only when necessary
20644 (i.e. when the value of a register or memory location is
20645 referenced, or a thread-local block, etc.). Then again, it might
20646 not be worthwhile. I'm assuming that it isn't unless performance
20647 or memory numbers show me otherwise. */
20648
20649 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20650
20651 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20652 cu->has_loclist = true;
20653 }
20654
20655 /* Given a pointer to a DWARF information entry, figure out if we need
20656 to make a symbol table entry for it, and if so, create a new entry
20657 and return a pointer to it.
20658 If TYPE is NULL, determine symbol type from the die, otherwise
20659 used the passed type.
20660 If SPACE is not NULL, use it to hold the new symbol. If it is
20661 NULL, allocate a new symbol on the objfile's obstack. */
20662
20663 static struct symbol *
20664 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20665 struct symbol *space)
20666 {
20667 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20668 struct objfile *objfile = dwarf2_per_objfile->objfile;
20669 struct gdbarch *gdbarch = objfile->arch ();
20670 struct symbol *sym = NULL;
20671 const char *name;
20672 struct attribute *attr = NULL;
20673 struct attribute *attr2 = NULL;
20674 CORE_ADDR baseaddr;
20675 struct pending **list_to_add = NULL;
20676
20677 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20678
20679 baseaddr = objfile->text_section_offset ();
20680
20681 name = dwarf2_name (die, cu);
20682 if (name)
20683 {
20684 int suppress_add = 0;
20685
20686 if (space)
20687 sym = space;
20688 else
20689 sym = new (&objfile->objfile_obstack) symbol;
20690 OBJSTAT (objfile, n_syms++);
20691
20692 /* Cache this symbol's name and the name's demangled form (if any). */
20693 sym->set_language (cu->language, &objfile->objfile_obstack);
20694 /* Fortran does not have mangling standard and the mangling does differ
20695 between gfortran, iFort etc. */
20696 const char *physname
20697 = (cu->language == language_fortran
20698 ? dwarf2_full_name (name, die, cu)
20699 : dwarf2_physname (name, die, cu));
20700 const char *linkagename = dw2_linkage_name (die, cu);
20701
20702 if (linkagename == nullptr || cu->language == language_ada)
20703 sym->set_linkage_name (physname);
20704 else
20705 {
20706 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20707 sym->set_linkage_name (linkagename);
20708 }
20709
20710 /* Default assumptions.
20711 Use the passed type or decode it from the die. */
20712 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20713 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20714 if (type != NULL)
20715 SYMBOL_TYPE (sym) = type;
20716 else
20717 SYMBOL_TYPE (sym) = die_type (die, cu);
20718 attr = dwarf2_attr (die,
20719 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20720 cu);
20721 if (attr != nullptr)
20722 {
20723 SYMBOL_LINE (sym) = DW_UNSND (attr);
20724 }
20725
20726 attr = dwarf2_attr (die,
20727 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20728 cu);
20729 if (attr != nullptr)
20730 {
20731 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20732 struct file_entry *fe;
20733
20734 if (cu->line_header != NULL)
20735 fe = cu->line_header->file_name_at (file_index);
20736 else
20737 fe = NULL;
20738
20739 if (fe == NULL)
20740 complaint (_("file index out of range"));
20741 else
20742 symbol_set_symtab (sym, fe->symtab);
20743 }
20744
20745 switch (die->tag)
20746 {
20747 case DW_TAG_label:
20748 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20749 if (attr != nullptr)
20750 {
20751 CORE_ADDR addr;
20752
20753 addr = attr->value_as_address ();
20754 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20755 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20756 }
20757 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20758 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20759 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20760 add_symbol_to_list (sym, cu->list_in_scope);
20761 break;
20762 case DW_TAG_subprogram:
20763 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20764 finish_block. */
20765 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20766 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20767 if ((attr2 && (DW_UNSND (attr2) != 0))
20768 || cu->language == language_ada
20769 || cu->language == language_fortran)
20770 {
20771 /* Subprograms marked external are stored as a global symbol.
20772 Ada and Fortran subprograms, whether marked external or
20773 not, are always stored as a global symbol, because we want
20774 to be able to access them globally. For instance, we want
20775 to be able to break on a nested subprogram without having
20776 to specify the context. */
20777 list_to_add = cu->get_builder ()->get_global_symbols ();
20778 }
20779 else
20780 {
20781 list_to_add = cu->list_in_scope;
20782 }
20783 break;
20784 case DW_TAG_inlined_subroutine:
20785 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20786 finish_block. */
20787 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20788 SYMBOL_INLINED (sym) = 1;
20789 list_to_add = cu->list_in_scope;
20790 break;
20791 case DW_TAG_template_value_param:
20792 suppress_add = 1;
20793 /* Fall through. */
20794 case DW_TAG_constant:
20795 case DW_TAG_variable:
20796 case DW_TAG_member:
20797 /* Compilation with minimal debug info may result in
20798 variables with missing type entries. Change the
20799 misleading `void' type to something sensible. */
20800 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
20801 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20802
20803 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20804 /* In the case of DW_TAG_member, we should only be called for
20805 static const members. */
20806 if (die->tag == DW_TAG_member)
20807 {
20808 /* dwarf2_add_field uses die_is_declaration,
20809 so we do the same. */
20810 gdb_assert (die_is_declaration (die, cu));
20811 gdb_assert (attr);
20812 }
20813 if (attr != nullptr)
20814 {
20815 dwarf2_const_value (attr, sym, cu);
20816 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20817 if (!suppress_add)
20818 {
20819 if (attr2 && (DW_UNSND (attr2) != 0))
20820 list_to_add = cu->get_builder ()->get_global_symbols ();
20821 else
20822 list_to_add = cu->list_in_scope;
20823 }
20824 break;
20825 }
20826 attr = dwarf2_attr (die, DW_AT_location, cu);
20827 if (attr != nullptr)
20828 {
20829 var_decode_location (attr, sym, cu);
20830 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20831
20832 /* Fortran explicitly imports any global symbols to the local
20833 scope by DW_TAG_common_block. */
20834 if (cu->language == language_fortran && die->parent
20835 && die->parent->tag == DW_TAG_common_block)
20836 attr2 = NULL;
20837
20838 if (SYMBOL_CLASS (sym) == LOC_STATIC
20839 && SYMBOL_VALUE_ADDRESS (sym) == 0
20840 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
20841 {
20842 /* When a static variable is eliminated by the linker,
20843 the corresponding debug information is not stripped
20844 out, but the variable address is set to null;
20845 do not add such variables into symbol table. */
20846 }
20847 else if (attr2 && (DW_UNSND (attr2) != 0))
20848 {
20849 if (SYMBOL_CLASS (sym) == LOC_STATIC
20850 && (objfile->flags & OBJF_MAINLINE) == 0
20851 && dwarf2_per_objfile->per_bfd->can_copy)
20852 {
20853 /* A global static variable might be subject to
20854 copy relocation. We first check for a local
20855 minsym, though, because maybe the symbol was
20856 marked hidden, in which case this would not
20857 apply. */
20858 bound_minimal_symbol found
20859 = (lookup_minimal_symbol_linkage
20860 (sym->linkage_name (), objfile));
20861 if (found.minsym != nullptr)
20862 sym->maybe_copied = 1;
20863 }
20864
20865 /* A variable with DW_AT_external is never static,
20866 but it may be block-scoped. */
20867 list_to_add
20868 = ((cu->list_in_scope
20869 == cu->get_builder ()->get_file_symbols ())
20870 ? cu->get_builder ()->get_global_symbols ()
20871 : cu->list_in_scope);
20872 }
20873 else
20874 list_to_add = cu->list_in_scope;
20875 }
20876 else
20877 {
20878 /* We do not know the address of this symbol.
20879 If it is an external symbol and we have type information
20880 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20881 The address of the variable will then be determined from
20882 the minimal symbol table whenever the variable is
20883 referenced. */
20884 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20885
20886 /* Fortran explicitly imports any global symbols to the local
20887 scope by DW_TAG_common_block. */
20888 if (cu->language == language_fortran && die->parent
20889 && die->parent->tag == DW_TAG_common_block)
20890 {
20891 /* SYMBOL_CLASS doesn't matter here because
20892 read_common_block is going to reset it. */
20893 if (!suppress_add)
20894 list_to_add = cu->list_in_scope;
20895 }
20896 else if (attr2 && (DW_UNSND (attr2) != 0)
20897 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20898 {
20899 /* A variable with DW_AT_external is never static, but it
20900 may be block-scoped. */
20901 list_to_add
20902 = ((cu->list_in_scope
20903 == cu->get_builder ()->get_file_symbols ())
20904 ? cu->get_builder ()->get_global_symbols ()
20905 : cu->list_in_scope);
20906
20907 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20908 }
20909 else if (!die_is_declaration (die, cu))
20910 {
20911 /* Use the default LOC_OPTIMIZED_OUT class. */
20912 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20913 if (!suppress_add)
20914 list_to_add = cu->list_in_scope;
20915 }
20916 }
20917 break;
20918 case DW_TAG_formal_parameter:
20919 {
20920 /* If we are inside a function, mark this as an argument. If
20921 not, we might be looking at an argument to an inlined function
20922 when we do not have enough information to show inlined frames;
20923 pretend it's a local variable in that case so that the user can
20924 still see it. */
20925 struct context_stack *curr
20926 = cu->get_builder ()->get_current_context_stack ();
20927 if (curr != nullptr && curr->name != nullptr)
20928 SYMBOL_IS_ARGUMENT (sym) = 1;
20929 attr = dwarf2_attr (die, DW_AT_location, cu);
20930 if (attr != nullptr)
20931 {
20932 var_decode_location (attr, sym, cu);
20933 }
20934 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20935 if (attr != nullptr)
20936 {
20937 dwarf2_const_value (attr, sym, cu);
20938 }
20939
20940 list_to_add = cu->list_in_scope;
20941 }
20942 break;
20943 case DW_TAG_unspecified_parameters:
20944 /* From varargs functions; gdb doesn't seem to have any
20945 interest in this information, so just ignore it for now.
20946 (FIXME?) */
20947 break;
20948 case DW_TAG_template_type_param:
20949 suppress_add = 1;
20950 /* Fall through. */
20951 case DW_TAG_class_type:
20952 case DW_TAG_interface_type:
20953 case DW_TAG_structure_type:
20954 case DW_TAG_union_type:
20955 case DW_TAG_set_type:
20956 case DW_TAG_enumeration_type:
20957 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20958 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20959
20960 {
20961 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20962 really ever be static objects: otherwise, if you try
20963 to, say, break of a class's method and you're in a file
20964 which doesn't mention that class, it won't work unless
20965 the check for all static symbols in lookup_symbol_aux
20966 saves you. See the OtherFileClass tests in
20967 gdb.c++/namespace.exp. */
20968
20969 if (!suppress_add)
20970 {
20971 buildsym_compunit *builder = cu->get_builder ();
20972 list_to_add
20973 = (cu->list_in_scope == builder->get_file_symbols ()
20974 && cu->language == language_cplus
20975 ? builder->get_global_symbols ()
20976 : cu->list_in_scope);
20977
20978 /* The semantics of C++ state that "struct foo {
20979 ... }" also defines a typedef for "foo". */
20980 if (cu->language == language_cplus
20981 || cu->language == language_ada
20982 || cu->language == language_d
20983 || cu->language == language_rust)
20984 {
20985 /* The symbol's name is already allocated along
20986 with this objfile, so we don't need to
20987 duplicate it for the type. */
20988 if (SYMBOL_TYPE (sym)->name () == 0)
20989 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
20990 }
20991 }
20992 }
20993 break;
20994 case DW_TAG_typedef:
20995 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20996 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20997 list_to_add = cu->list_in_scope;
20998 break;
20999 case DW_TAG_base_type:
21000 case DW_TAG_subrange_type:
21001 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21002 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21003 list_to_add = cu->list_in_scope;
21004 break;
21005 case DW_TAG_enumerator:
21006 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21007 if (attr != nullptr)
21008 {
21009 dwarf2_const_value (attr, sym, cu);
21010 }
21011 {
21012 /* NOTE: carlton/2003-11-10: See comment above in the
21013 DW_TAG_class_type, etc. block. */
21014
21015 list_to_add
21016 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21017 && cu->language == language_cplus
21018 ? cu->get_builder ()->get_global_symbols ()
21019 : cu->list_in_scope);
21020 }
21021 break;
21022 case DW_TAG_imported_declaration:
21023 case DW_TAG_namespace:
21024 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21025 list_to_add = cu->get_builder ()->get_global_symbols ();
21026 break;
21027 case DW_TAG_module:
21028 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21029 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21030 list_to_add = cu->get_builder ()->get_global_symbols ();
21031 break;
21032 case DW_TAG_common_block:
21033 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21034 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21035 add_symbol_to_list (sym, cu->list_in_scope);
21036 break;
21037 default:
21038 /* Not a tag we recognize. Hopefully we aren't processing
21039 trash data, but since we must specifically ignore things
21040 we don't recognize, there is nothing else we should do at
21041 this point. */
21042 complaint (_("unsupported tag: '%s'"),
21043 dwarf_tag_name (die->tag));
21044 break;
21045 }
21046
21047 if (suppress_add)
21048 {
21049 sym->hash_next = objfile->template_symbols;
21050 objfile->template_symbols = sym;
21051 list_to_add = NULL;
21052 }
21053
21054 if (list_to_add != NULL)
21055 add_symbol_to_list (sym, list_to_add);
21056
21057 /* For the benefit of old versions of GCC, check for anonymous
21058 namespaces based on the demangled name. */
21059 if (!cu->processing_has_namespace_info
21060 && cu->language == language_cplus)
21061 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21062 }
21063 return (sym);
21064 }
21065
21066 /* Given an attr with a DW_FORM_dataN value in host byte order,
21067 zero-extend it as appropriate for the symbol's type. The DWARF
21068 standard (v4) is not entirely clear about the meaning of using
21069 DW_FORM_dataN for a constant with a signed type, where the type is
21070 wider than the data. The conclusion of a discussion on the DWARF
21071 list was that this is unspecified. We choose to always zero-extend
21072 because that is the interpretation long in use by GCC. */
21073
21074 static gdb_byte *
21075 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21076 struct dwarf2_cu *cu, LONGEST *value, int bits)
21077 {
21078 struct objfile *objfile = cu->per_objfile->objfile;
21079 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21080 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21081 LONGEST l = DW_UNSND (attr);
21082
21083 if (bits < sizeof (*value) * 8)
21084 {
21085 l &= ((LONGEST) 1 << bits) - 1;
21086 *value = l;
21087 }
21088 else if (bits == sizeof (*value) * 8)
21089 *value = l;
21090 else
21091 {
21092 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21093 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21094 return bytes;
21095 }
21096
21097 return NULL;
21098 }
21099
21100 /* Read a constant value from an attribute. Either set *VALUE, or if
21101 the value does not fit in *VALUE, set *BYTES - either already
21102 allocated on the objfile obstack, or newly allocated on OBSTACK,
21103 or, set *BATON, if we translated the constant to a location
21104 expression. */
21105
21106 static void
21107 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21108 const char *name, struct obstack *obstack,
21109 struct dwarf2_cu *cu,
21110 LONGEST *value, const gdb_byte **bytes,
21111 struct dwarf2_locexpr_baton **baton)
21112 {
21113 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21114 struct objfile *objfile = per_objfile->objfile;
21115 struct comp_unit_head *cu_header = &cu->header;
21116 struct dwarf_block *blk;
21117 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21118 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21119
21120 *value = 0;
21121 *bytes = NULL;
21122 *baton = NULL;
21123
21124 switch (attr->form)
21125 {
21126 case DW_FORM_addr:
21127 case DW_FORM_addrx:
21128 case DW_FORM_GNU_addr_index:
21129 {
21130 gdb_byte *data;
21131
21132 if (TYPE_LENGTH (type) != cu_header->addr_size)
21133 dwarf2_const_value_length_mismatch_complaint (name,
21134 cu_header->addr_size,
21135 TYPE_LENGTH (type));
21136 /* Symbols of this form are reasonably rare, so we just
21137 piggyback on the existing location code rather than writing
21138 a new implementation of symbol_computed_ops. */
21139 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21140 (*baton)->per_objfile = per_objfile;
21141 (*baton)->per_cu = cu->per_cu;
21142 gdb_assert ((*baton)->per_cu);
21143
21144 (*baton)->size = 2 + cu_header->addr_size;
21145 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21146 (*baton)->data = data;
21147
21148 data[0] = DW_OP_addr;
21149 store_unsigned_integer (&data[1], cu_header->addr_size,
21150 byte_order, DW_ADDR (attr));
21151 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21152 }
21153 break;
21154 case DW_FORM_string:
21155 case DW_FORM_strp:
21156 case DW_FORM_strx:
21157 case DW_FORM_GNU_str_index:
21158 case DW_FORM_GNU_strp_alt:
21159 /* DW_STRING is already allocated on the objfile obstack, point
21160 directly to it. */
21161 *bytes = (const gdb_byte *) DW_STRING (attr);
21162 break;
21163 case DW_FORM_block1:
21164 case DW_FORM_block2:
21165 case DW_FORM_block4:
21166 case DW_FORM_block:
21167 case DW_FORM_exprloc:
21168 case DW_FORM_data16:
21169 blk = DW_BLOCK (attr);
21170 if (TYPE_LENGTH (type) != blk->size)
21171 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21172 TYPE_LENGTH (type));
21173 *bytes = blk->data;
21174 break;
21175
21176 /* The DW_AT_const_value attributes are supposed to carry the
21177 symbol's value "represented as it would be on the target
21178 architecture." By the time we get here, it's already been
21179 converted to host endianness, so we just need to sign- or
21180 zero-extend it as appropriate. */
21181 case DW_FORM_data1:
21182 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21183 break;
21184 case DW_FORM_data2:
21185 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21186 break;
21187 case DW_FORM_data4:
21188 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21189 break;
21190 case DW_FORM_data8:
21191 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21192 break;
21193
21194 case DW_FORM_sdata:
21195 case DW_FORM_implicit_const:
21196 *value = DW_SND (attr);
21197 break;
21198
21199 case DW_FORM_udata:
21200 *value = DW_UNSND (attr);
21201 break;
21202
21203 default:
21204 complaint (_("unsupported const value attribute form: '%s'"),
21205 dwarf_form_name (attr->form));
21206 *value = 0;
21207 break;
21208 }
21209 }
21210
21211
21212 /* Copy constant value from an attribute to a symbol. */
21213
21214 static void
21215 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21216 struct dwarf2_cu *cu)
21217 {
21218 struct objfile *objfile = cu->per_objfile->objfile;
21219 LONGEST value;
21220 const gdb_byte *bytes;
21221 struct dwarf2_locexpr_baton *baton;
21222
21223 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21224 sym->print_name (),
21225 &objfile->objfile_obstack, cu,
21226 &value, &bytes, &baton);
21227
21228 if (baton != NULL)
21229 {
21230 SYMBOL_LOCATION_BATON (sym) = baton;
21231 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21232 }
21233 else if (bytes != NULL)
21234 {
21235 SYMBOL_VALUE_BYTES (sym) = bytes;
21236 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21237 }
21238 else
21239 {
21240 SYMBOL_VALUE (sym) = value;
21241 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21242 }
21243 }
21244
21245 /* Return the type of the die in question using its DW_AT_type attribute. */
21246
21247 static struct type *
21248 die_type (struct die_info *die, struct dwarf2_cu *cu)
21249 {
21250 struct attribute *type_attr;
21251
21252 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21253 if (!type_attr)
21254 {
21255 struct objfile *objfile = cu->per_objfile->objfile;
21256 /* A missing DW_AT_type represents a void type. */
21257 return objfile_type (objfile)->builtin_void;
21258 }
21259
21260 return lookup_die_type (die, type_attr, cu);
21261 }
21262
21263 /* True iff CU's producer generates GNAT Ada auxiliary information
21264 that allows to find parallel types through that information instead
21265 of having to do expensive parallel lookups by type name. */
21266
21267 static int
21268 need_gnat_info (struct dwarf2_cu *cu)
21269 {
21270 /* Assume that the Ada compiler was GNAT, which always produces
21271 the auxiliary information. */
21272 return (cu->language == language_ada);
21273 }
21274
21275 /* Return the auxiliary type of the die in question using its
21276 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21277 attribute is not present. */
21278
21279 static struct type *
21280 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21281 {
21282 struct attribute *type_attr;
21283
21284 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21285 if (!type_attr)
21286 return NULL;
21287
21288 return lookup_die_type (die, type_attr, cu);
21289 }
21290
21291 /* If DIE has a descriptive_type attribute, then set the TYPE's
21292 descriptive type accordingly. */
21293
21294 static void
21295 set_descriptive_type (struct type *type, struct die_info *die,
21296 struct dwarf2_cu *cu)
21297 {
21298 struct type *descriptive_type = die_descriptive_type (die, cu);
21299
21300 if (descriptive_type)
21301 {
21302 ALLOCATE_GNAT_AUX_TYPE (type);
21303 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21304 }
21305 }
21306
21307 /* Return the containing type of the die in question using its
21308 DW_AT_containing_type attribute. */
21309
21310 static struct type *
21311 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21312 {
21313 struct attribute *type_attr;
21314 struct objfile *objfile = cu->per_objfile->objfile;
21315
21316 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21317 if (!type_attr)
21318 error (_("Dwarf Error: Problem turning containing type into gdb type "
21319 "[in module %s]"), objfile_name (objfile));
21320
21321 return lookup_die_type (die, type_attr, cu);
21322 }
21323
21324 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21325
21326 static struct type *
21327 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21328 {
21329 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21330 struct objfile *objfile = dwarf2_per_objfile->objfile;
21331 char *saved;
21332
21333 std::string message
21334 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21335 objfile_name (objfile),
21336 sect_offset_str (cu->header.sect_off),
21337 sect_offset_str (die->sect_off));
21338 saved = obstack_strdup (&objfile->objfile_obstack, message);
21339
21340 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21341 }
21342
21343 /* Look up the type of DIE in CU using its type attribute ATTR.
21344 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21345 DW_AT_containing_type.
21346 If there is no type substitute an error marker. */
21347
21348 static struct type *
21349 lookup_die_type (struct die_info *die, const struct attribute *attr,
21350 struct dwarf2_cu *cu)
21351 {
21352 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21353 struct objfile *objfile = dwarf2_per_objfile->objfile;
21354 struct type *this_type;
21355
21356 gdb_assert (attr->name == DW_AT_type
21357 || attr->name == DW_AT_GNAT_descriptive_type
21358 || attr->name == DW_AT_containing_type);
21359
21360 /* First see if we have it cached. */
21361
21362 if (attr->form == DW_FORM_GNU_ref_alt)
21363 {
21364 struct dwarf2_per_cu_data *per_cu;
21365 sect_offset sect_off = attr->get_ref_die_offset ();
21366
21367 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21368 dwarf2_per_objfile);
21369 this_type = get_die_type_at_offset (sect_off, per_cu);
21370 }
21371 else if (attr->form_is_ref ())
21372 {
21373 sect_offset sect_off = attr->get_ref_die_offset ();
21374
21375 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21376 }
21377 else if (attr->form == DW_FORM_ref_sig8)
21378 {
21379 ULONGEST signature = DW_SIGNATURE (attr);
21380
21381 return get_signatured_type (die, signature, cu);
21382 }
21383 else
21384 {
21385 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21386 " at %s [in module %s]"),
21387 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21388 objfile_name (objfile));
21389 return build_error_marker_type (cu, die);
21390 }
21391
21392 /* If not cached we need to read it in. */
21393
21394 if (this_type == NULL)
21395 {
21396 struct die_info *type_die = NULL;
21397 struct dwarf2_cu *type_cu = cu;
21398
21399 if (attr->form_is_ref ())
21400 type_die = follow_die_ref (die, attr, &type_cu);
21401 if (type_die == NULL)
21402 return build_error_marker_type (cu, die);
21403 /* If we find the type now, it's probably because the type came
21404 from an inter-CU reference and the type's CU got expanded before
21405 ours. */
21406 this_type = read_type_die (type_die, type_cu);
21407 }
21408
21409 /* If we still don't have a type use an error marker. */
21410
21411 if (this_type == NULL)
21412 return build_error_marker_type (cu, die);
21413
21414 return this_type;
21415 }
21416
21417 /* Return the type in DIE, CU.
21418 Returns NULL for invalid types.
21419
21420 This first does a lookup in die_type_hash,
21421 and only reads the die in if necessary.
21422
21423 NOTE: This can be called when reading in partial or full symbols. */
21424
21425 static struct type *
21426 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21427 {
21428 struct type *this_type;
21429
21430 this_type = get_die_type (die, cu);
21431 if (this_type)
21432 return this_type;
21433
21434 return read_type_die_1 (die, cu);
21435 }
21436
21437 /* Read the type in DIE, CU.
21438 Returns NULL for invalid types. */
21439
21440 static struct type *
21441 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21442 {
21443 struct type *this_type = NULL;
21444
21445 switch (die->tag)
21446 {
21447 case DW_TAG_class_type:
21448 case DW_TAG_interface_type:
21449 case DW_TAG_structure_type:
21450 case DW_TAG_union_type:
21451 this_type = read_structure_type (die, cu);
21452 break;
21453 case DW_TAG_enumeration_type:
21454 this_type = read_enumeration_type (die, cu);
21455 break;
21456 case DW_TAG_subprogram:
21457 case DW_TAG_subroutine_type:
21458 case DW_TAG_inlined_subroutine:
21459 this_type = read_subroutine_type (die, cu);
21460 break;
21461 case DW_TAG_array_type:
21462 this_type = read_array_type (die, cu);
21463 break;
21464 case DW_TAG_set_type:
21465 this_type = read_set_type (die, cu);
21466 break;
21467 case DW_TAG_pointer_type:
21468 this_type = read_tag_pointer_type (die, cu);
21469 break;
21470 case DW_TAG_ptr_to_member_type:
21471 this_type = read_tag_ptr_to_member_type (die, cu);
21472 break;
21473 case DW_TAG_reference_type:
21474 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21475 break;
21476 case DW_TAG_rvalue_reference_type:
21477 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21478 break;
21479 case DW_TAG_const_type:
21480 this_type = read_tag_const_type (die, cu);
21481 break;
21482 case DW_TAG_volatile_type:
21483 this_type = read_tag_volatile_type (die, cu);
21484 break;
21485 case DW_TAG_restrict_type:
21486 this_type = read_tag_restrict_type (die, cu);
21487 break;
21488 case DW_TAG_string_type:
21489 this_type = read_tag_string_type (die, cu);
21490 break;
21491 case DW_TAG_typedef:
21492 this_type = read_typedef (die, cu);
21493 break;
21494 case DW_TAG_subrange_type:
21495 this_type = read_subrange_type (die, cu);
21496 break;
21497 case DW_TAG_base_type:
21498 this_type = read_base_type (die, cu);
21499 break;
21500 case DW_TAG_unspecified_type:
21501 this_type = read_unspecified_type (die, cu);
21502 break;
21503 case DW_TAG_namespace:
21504 this_type = read_namespace_type (die, cu);
21505 break;
21506 case DW_TAG_module:
21507 this_type = read_module_type (die, cu);
21508 break;
21509 case DW_TAG_atomic_type:
21510 this_type = read_tag_atomic_type (die, cu);
21511 break;
21512 default:
21513 complaint (_("unexpected tag in read_type_die: '%s'"),
21514 dwarf_tag_name (die->tag));
21515 break;
21516 }
21517
21518 return this_type;
21519 }
21520
21521 /* See if we can figure out if the class lives in a namespace. We do
21522 this by looking for a member function; its demangled name will
21523 contain namespace info, if there is any.
21524 Return the computed name or NULL.
21525 Space for the result is allocated on the objfile's obstack.
21526 This is the full-die version of guess_partial_die_structure_name.
21527 In this case we know DIE has no useful parent. */
21528
21529 static const char *
21530 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21531 {
21532 struct die_info *spec_die;
21533 struct dwarf2_cu *spec_cu;
21534 struct die_info *child;
21535 struct objfile *objfile = cu->per_objfile->objfile;
21536
21537 spec_cu = cu;
21538 spec_die = die_specification (die, &spec_cu);
21539 if (spec_die != NULL)
21540 {
21541 die = spec_die;
21542 cu = spec_cu;
21543 }
21544
21545 for (child = die->child;
21546 child != NULL;
21547 child = child->sibling)
21548 {
21549 if (child->tag == DW_TAG_subprogram)
21550 {
21551 const char *linkage_name = dw2_linkage_name (child, cu);
21552
21553 if (linkage_name != NULL)
21554 {
21555 gdb::unique_xmalloc_ptr<char> actual_name
21556 (language_class_name_from_physname (cu->language_defn,
21557 linkage_name));
21558 const char *name = NULL;
21559
21560 if (actual_name != NULL)
21561 {
21562 const char *die_name = dwarf2_name (die, cu);
21563
21564 if (die_name != NULL
21565 && strcmp (die_name, actual_name.get ()) != 0)
21566 {
21567 /* Strip off the class name from the full name.
21568 We want the prefix. */
21569 int die_name_len = strlen (die_name);
21570 int actual_name_len = strlen (actual_name.get ());
21571 const char *ptr = actual_name.get ();
21572
21573 /* Test for '::' as a sanity check. */
21574 if (actual_name_len > die_name_len + 2
21575 && ptr[actual_name_len - die_name_len - 1] == ':')
21576 name = obstack_strndup (
21577 &objfile->per_bfd->storage_obstack,
21578 ptr, actual_name_len - die_name_len - 2);
21579 }
21580 }
21581 return name;
21582 }
21583 }
21584 }
21585
21586 return NULL;
21587 }
21588
21589 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21590 prefix part in such case. See
21591 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21592
21593 static const char *
21594 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21595 {
21596 struct attribute *attr;
21597 const char *base;
21598
21599 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21600 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21601 return NULL;
21602
21603 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21604 return NULL;
21605
21606 attr = dw2_linkage_name_attr (die, cu);
21607 if (attr == NULL || DW_STRING (attr) == NULL)
21608 return NULL;
21609
21610 /* dwarf2_name had to be already called. */
21611 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21612
21613 /* Strip the base name, keep any leading namespaces/classes. */
21614 base = strrchr (DW_STRING (attr), ':');
21615 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21616 return "";
21617
21618 struct objfile *objfile = cu->per_objfile->objfile;
21619 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21620 DW_STRING (attr),
21621 &base[-1] - DW_STRING (attr));
21622 }
21623
21624 /* Return the name of the namespace/class that DIE is defined within,
21625 or "" if we can't tell. The caller should not xfree the result.
21626
21627 For example, if we're within the method foo() in the following
21628 code:
21629
21630 namespace N {
21631 class C {
21632 void foo () {
21633 }
21634 };
21635 }
21636
21637 then determine_prefix on foo's die will return "N::C". */
21638
21639 static const char *
21640 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21641 {
21642 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21643 struct die_info *parent, *spec_die;
21644 struct dwarf2_cu *spec_cu;
21645 struct type *parent_type;
21646 const char *retval;
21647
21648 if (cu->language != language_cplus
21649 && cu->language != language_fortran && cu->language != language_d
21650 && cu->language != language_rust)
21651 return "";
21652
21653 retval = anonymous_struct_prefix (die, cu);
21654 if (retval)
21655 return retval;
21656
21657 /* We have to be careful in the presence of DW_AT_specification.
21658 For example, with GCC 3.4, given the code
21659
21660 namespace N {
21661 void foo() {
21662 // Definition of N::foo.
21663 }
21664 }
21665
21666 then we'll have a tree of DIEs like this:
21667
21668 1: DW_TAG_compile_unit
21669 2: DW_TAG_namespace // N
21670 3: DW_TAG_subprogram // declaration of N::foo
21671 4: DW_TAG_subprogram // definition of N::foo
21672 DW_AT_specification // refers to die #3
21673
21674 Thus, when processing die #4, we have to pretend that we're in
21675 the context of its DW_AT_specification, namely the contex of die
21676 #3. */
21677 spec_cu = cu;
21678 spec_die = die_specification (die, &spec_cu);
21679 if (spec_die == NULL)
21680 parent = die->parent;
21681 else
21682 {
21683 parent = spec_die->parent;
21684 cu = spec_cu;
21685 }
21686
21687 if (parent == NULL)
21688 return "";
21689 else if (parent->building_fullname)
21690 {
21691 const char *name;
21692 const char *parent_name;
21693
21694 /* It has been seen on RealView 2.2 built binaries,
21695 DW_TAG_template_type_param types actually _defined_ as
21696 children of the parent class:
21697
21698 enum E {};
21699 template class <class Enum> Class{};
21700 Class<enum E> class_e;
21701
21702 1: DW_TAG_class_type (Class)
21703 2: DW_TAG_enumeration_type (E)
21704 3: DW_TAG_enumerator (enum1:0)
21705 3: DW_TAG_enumerator (enum2:1)
21706 ...
21707 2: DW_TAG_template_type_param
21708 DW_AT_type DW_FORM_ref_udata (E)
21709
21710 Besides being broken debug info, it can put GDB into an
21711 infinite loop. Consider:
21712
21713 When we're building the full name for Class<E>, we'll start
21714 at Class, and go look over its template type parameters,
21715 finding E. We'll then try to build the full name of E, and
21716 reach here. We're now trying to build the full name of E,
21717 and look over the parent DIE for containing scope. In the
21718 broken case, if we followed the parent DIE of E, we'd again
21719 find Class, and once again go look at its template type
21720 arguments, etc., etc. Simply don't consider such parent die
21721 as source-level parent of this die (it can't be, the language
21722 doesn't allow it), and break the loop here. */
21723 name = dwarf2_name (die, cu);
21724 parent_name = dwarf2_name (parent, cu);
21725 complaint (_("template param type '%s' defined within parent '%s'"),
21726 name ? name : "<unknown>",
21727 parent_name ? parent_name : "<unknown>");
21728 return "";
21729 }
21730 else
21731 switch (parent->tag)
21732 {
21733 case DW_TAG_namespace:
21734 parent_type = read_type_die (parent, cu);
21735 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21736 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21737 Work around this problem here. */
21738 if (cu->language == language_cplus
21739 && strcmp (parent_type->name (), "::") == 0)
21740 return "";
21741 /* We give a name to even anonymous namespaces. */
21742 return parent_type->name ();
21743 case DW_TAG_class_type:
21744 case DW_TAG_interface_type:
21745 case DW_TAG_structure_type:
21746 case DW_TAG_union_type:
21747 case DW_TAG_module:
21748 parent_type = read_type_die (parent, cu);
21749 if (parent_type->name () != NULL)
21750 return parent_type->name ();
21751 else
21752 /* An anonymous structure is only allowed non-static data
21753 members; no typedefs, no member functions, et cetera.
21754 So it does not need a prefix. */
21755 return "";
21756 case DW_TAG_compile_unit:
21757 case DW_TAG_partial_unit:
21758 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21759 if (cu->language == language_cplus
21760 && !dwarf2_per_objfile->per_bfd->types.empty ()
21761 && die->child != NULL
21762 && (die->tag == DW_TAG_class_type
21763 || die->tag == DW_TAG_structure_type
21764 || die->tag == DW_TAG_union_type))
21765 {
21766 const char *name = guess_full_die_structure_name (die, cu);
21767 if (name != NULL)
21768 return name;
21769 }
21770 return "";
21771 case DW_TAG_subprogram:
21772 /* Nested subroutines in Fortran get a prefix with the name
21773 of the parent's subroutine. */
21774 if (cu->language == language_fortran)
21775 {
21776 if ((die->tag == DW_TAG_subprogram)
21777 && (dwarf2_name (parent, cu) != NULL))
21778 return dwarf2_name (parent, cu);
21779 }
21780 return determine_prefix (parent, cu);
21781 case DW_TAG_enumeration_type:
21782 parent_type = read_type_die (parent, cu);
21783 if (TYPE_DECLARED_CLASS (parent_type))
21784 {
21785 if (parent_type->name () != NULL)
21786 return parent_type->name ();
21787 return "";
21788 }
21789 /* Fall through. */
21790 default:
21791 return determine_prefix (parent, cu);
21792 }
21793 }
21794
21795 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21796 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21797 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21798 an obconcat, otherwise allocate storage for the result. The CU argument is
21799 used to determine the language and hence, the appropriate separator. */
21800
21801 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21802
21803 static char *
21804 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21805 int physname, struct dwarf2_cu *cu)
21806 {
21807 const char *lead = "";
21808 const char *sep;
21809
21810 if (suffix == NULL || suffix[0] == '\0'
21811 || prefix == NULL || prefix[0] == '\0')
21812 sep = "";
21813 else if (cu->language == language_d)
21814 {
21815 /* For D, the 'main' function could be defined in any module, but it
21816 should never be prefixed. */
21817 if (strcmp (suffix, "D main") == 0)
21818 {
21819 prefix = "";
21820 sep = "";
21821 }
21822 else
21823 sep = ".";
21824 }
21825 else if (cu->language == language_fortran && physname)
21826 {
21827 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21828 DW_AT_MIPS_linkage_name is preferred and used instead. */
21829
21830 lead = "__";
21831 sep = "_MOD_";
21832 }
21833 else
21834 sep = "::";
21835
21836 if (prefix == NULL)
21837 prefix = "";
21838 if (suffix == NULL)
21839 suffix = "";
21840
21841 if (obs == NULL)
21842 {
21843 char *retval
21844 = ((char *)
21845 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21846
21847 strcpy (retval, lead);
21848 strcat (retval, prefix);
21849 strcat (retval, sep);
21850 strcat (retval, suffix);
21851 return retval;
21852 }
21853 else
21854 {
21855 /* We have an obstack. */
21856 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21857 }
21858 }
21859
21860 /* Get name of a die, return NULL if not found. */
21861
21862 static const char *
21863 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21864 struct objfile *objfile)
21865 {
21866 if (name && cu->language == language_cplus)
21867 {
21868 gdb::unique_xmalloc_ptr<char> canon_name
21869 = cp_canonicalize_string (name);
21870
21871 if (canon_name != nullptr)
21872 name = objfile->intern (canon_name.get ());
21873 }
21874
21875 return name;
21876 }
21877
21878 /* Get name of a die, return NULL if not found.
21879 Anonymous namespaces are converted to their magic string. */
21880
21881 static const char *
21882 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21883 {
21884 struct attribute *attr;
21885 struct objfile *objfile = cu->per_objfile->objfile;
21886
21887 attr = dwarf2_attr (die, DW_AT_name, cu);
21888 if ((!attr || !DW_STRING (attr))
21889 && die->tag != DW_TAG_namespace
21890 && die->tag != DW_TAG_class_type
21891 && die->tag != DW_TAG_interface_type
21892 && die->tag != DW_TAG_structure_type
21893 && die->tag != DW_TAG_union_type)
21894 return NULL;
21895
21896 switch (die->tag)
21897 {
21898 case DW_TAG_compile_unit:
21899 case DW_TAG_partial_unit:
21900 /* Compilation units have a DW_AT_name that is a filename, not
21901 a source language identifier. */
21902 case DW_TAG_enumeration_type:
21903 case DW_TAG_enumerator:
21904 /* These tags always have simple identifiers already; no need
21905 to canonicalize them. */
21906 return DW_STRING (attr);
21907
21908 case DW_TAG_namespace:
21909 if (attr != NULL && DW_STRING (attr) != NULL)
21910 return DW_STRING (attr);
21911 return CP_ANONYMOUS_NAMESPACE_STR;
21912
21913 case DW_TAG_class_type:
21914 case DW_TAG_interface_type:
21915 case DW_TAG_structure_type:
21916 case DW_TAG_union_type:
21917 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21918 structures or unions. These were of the form "._%d" in GCC 4.1,
21919 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21920 and GCC 4.4. We work around this problem by ignoring these. */
21921 if (attr && DW_STRING (attr)
21922 && (startswith (DW_STRING (attr), "._")
21923 || startswith (DW_STRING (attr), "<anonymous")))
21924 return NULL;
21925
21926 /* GCC might emit a nameless typedef that has a linkage name. See
21927 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21928 if (!attr || DW_STRING (attr) == NULL)
21929 {
21930 attr = dw2_linkage_name_attr (die, cu);
21931 if (attr == NULL || DW_STRING (attr) == NULL)
21932 return NULL;
21933
21934 /* Avoid demangling DW_STRING (attr) the second time on a second
21935 call for the same DIE. */
21936 if (!DW_STRING_IS_CANONICAL (attr))
21937 {
21938 gdb::unique_xmalloc_ptr<char> demangled
21939 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21940 if (demangled == nullptr)
21941 return nullptr;
21942
21943 DW_STRING (attr) = objfile->intern (demangled.get ());
21944 DW_STRING_IS_CANONICAL (attr) = 1;
21945 }
21946
21947 /* Strip any leading namespaces/classes, keep only the base name.
21948 DW_AT_name for named DIEs does not contain the prefixes. */
21949 const char *base = strrchr (DW_STRING (attr), ':');
21950 if (base && base > DW_STRING (attr) && base[-1] == ':')
21951 return &base[1];
21952 else
21953 return DW_STRING (attr);
21954 }
21955 break;
21956
21957 default:
21958 break;
21959 }
21960
21961 if (!DW_STRING_IS_CANONICAL (attr))
21962 {
21963 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21964 objfile);
21965 DW_STRING_IS_CANONICAL (attr) = 1;
21966 }
21967 return DW_STRING (attr);
21968 }
21969
21970 /* Return the die that this die in an extension of, or NULL if there
21971 is none. *EXT_CU is the CU containing DIE on input, and the CU
21972 containing the return value on output. */
21973
21974 static struct die_info *
21975 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21976 {
21977 struct attribute *attr;
21978
21979 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21980 if (attr == NULL)
21981 return NULL;
21982
21983 return follow_die_ref (die, attr, ext_cu);
21984 }
21985
21986 static void
21987 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21988 {
21989 unsigned int i;
21990
21991 print_spaces (indent, f);
21992 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21993 dwarf_tag_name (die->tag), die->abbrev,
21994 sect_offset_str (die->sect_off));
21995
21996 if (die->parent != NULL)
21997 {
21998 print_spaces (indent, f);
21999 fprintf_unfiltered (f, " parent at offset: %s\n",
22000 sect_offset_str (die->parent->sect_off));
22001 }
22002
22003 print_spaces (indent, f);
22004 fprintf_unfiltered (f, " has children: %s\n",
22005 dwarf_bool_name (die->child != NULL));
22006
22007 print_spaces (indent, f);
22008 fprintf_unfiltered (f, " attributes:\n");
22009
22010 for (i = 0; i < die->num_attrs; ++i)
22011 {
22012 print_spaces (indent, f);
22013 fprintf_unfiltered (f, " %s (%s) ",
22014 dwarf_attr_name (die->attrs[i].name),
22015 dwarf_form_name (die->attrs[i].form));
22016
22017 switch (die->attrs[i].form)
22018 {
22019 case DW_FORM_addr:
22020 case DW_FORM_addrx:
22021 case DW_FORM_GNU_addr_index:
22022 fprintf_unfiltered (f, "address: ");
22023 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22024 break;
22025 case DW_FORM_block2:
22026 case DW_FORM_block4:
22027 case DW_FORM_block:
22028 case DW_FORM_block1:
22029 fprintf_unfiltered (f, "block: size %s",
22030 pulongest (DW_BLOCK (&die->attrs[i])->size));
22031 break;
22032 case DW_FORM_exprloc:
22033 fprintf_unfiltered (f, "expression: size %s",
22034 pulongest (DW_BLOCK (&die->attrs[i])->size));
22035 break;
22036 case DW_FORM_data16:
22037 fprintf_unfiltered (f, "constant of 16 bytes");
22038 break;
22039 case DW_FORM_ref_addr:
22040 fprintf_unfiltered (f, "ref address: ");
22041 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22042 break;
22043 case DW_FORM_GNU_ref_alt:
22044 fprintf_unfiltered (f, "alt ref address: ");
22045 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22046 break;
22047 case DW_FORM_ref1:
22048 case DW_FORM_ref2:
22049 case DW_FORM_ref4:
22050 case DW_FORM_ref8:
22051 case DW_FORM_ref_udata:
22052 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22053 (long) (DW_UNSND (&die->attrs[i])));
22054 break;
22055 case DW_FORM_data1:
22056 case DW_FORM_data2:
22057 case DW_FORM_data4:
22058 case DW_FORM_data8:
22059 case DW_FORM_udata:
22060 case DW_FORM_sdata:
22061 fprintf_unfiltered (f, "constant: %s",
22062 pulongest (DW_UNSND (&die->attrs[i])));
22063 break;
22064 case DW_FORM_sec_offset:
22065 fprintf_unfiltered (f, "section offset: %s",
22066 pulongest (DW_UNSND (&die->attrs[i])));
22067 break;
22068 case DW_FORM_ref_sig8:
22069 fprintf_unfiltered (f, "signature: %s",
22070 hex_string (DW_SIGNATURE (&die->attrs[i])));
22071 break;
22072 case DW_FORM_string:
22073 case DW_FORM_strp:
22074 case DW_FORM_line_strp:
22075 case DW_FORM_strx:
22076 case DW_FORM_GNU_str_index:
22077 case DW_FORM_GNU_strp_alt:
22078 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22079 DW_STRING (&die->attrs[i])
22080 ? DW_STRING (&die->attrs[i]) : "",
22081 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22082 break;
22083 case DW_FORM_flag:
22084 if (DW_UNSND (&die->attrs[i]))
22085 fprintf_unfiltered (f, "flag: TRUE");
22086 else
22087 fprintf_unfiltered (f, "flag: FALSE");
22088 break;
22089 case DW_FORM_flag_present:
22090 fprintf_unfiltered (f, "flag: TRUE");
22091 break;
22092 case DW_FORM_indirect:
22093 /* The reader will have reduced the indirect form to
22094 the "base form" so this form should not occur. */
22095 fprintf_unfiltered (f,
22096 "unexpected attribute form: DW_FORM_indirect");
22097 break;
22098 case DW_FORM_implicit_const:
22099 fprintf_unfiltered (f, "constant: %s",
22100 plongest (DW_SND (&die->attrs[i])));
22101 break;
22102 default:
22103 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22104 die->attrs[i].form);
22105 break;
22106 }
22107 fprintf_unfiltered (f, "\n");
22108 }
22109 }
22110
22111 static void
22112 dump_die_for_error (struct die_info *die)
22113 {
22114 dump_die_shallow (gdb_stderr, 0, die);
22115 }
22116
22117 static void
22118 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22119 {
22120 int indent = level * 4;
22121
22122 gdb_assert (die != NULL);
22123
22124 if (level >= max_level)
22125 return;
22126
22127 dump_die_shallow (f, indent, die);
22128
22129 if (die->child != NULL)
22130 {
22131 print_spaces (indent, f);
22132 fprintf_unfiltered (f, " Children:");
22133 if (level + 1 < max_level)
22134 {
22135 fprintf_unfiltered (f, "\n");
22136 dump_die_1 (f, level + 1, max_level, die->child);
22137 }
22138 else
22139 {
22140 fprintf_unfiltered (f,
22141 " [not printed, max nesting level reached]\n");
22142 }
22143 }
22144
22145 if (die->sibling != NULL && level > 0)
22146 {
22147 dump_die_1 (f, level, max_level, die->sibling);
22148 }
22149 }
22150
22151 /* This is called from the pdie macro in gdbinit.in.
22152 It's not static so gcc will keep a copy callable from gdb. */
22153
22154 void
22155 dump_die (struct die_info *die, int max_level)
22156 {
22157 dump_die_1 (gdb_stdlog, 0, max_level, die);
22158 }
22159
22160 static void
22161 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22162 {
22163 void **slot;
22164
22165 slot = htab_find_slot_with_hash (cu->die_hash, die,
22166 to_underlying (die->sect_off),
22167 INSERT);
22168
22169 *slot = die;
22170 }
22171
22172 /* Follow reference or signature attribute ATTR of SRC_DIE.
22173 On entry *REF_CU is the CU of SRC_DIE.
22174 On exit *REF_CU is the CU of the result. */
22175
22176 static struct die_info *
22177 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22178 struct dwarf2_cu **ref_cu)
22179 {
22180 struct die_info *die;
22181
22182 if (attr->form_is_ref ())
22183 die = follow_die_ref (src_die, attr, ref_cu);
22184 else if (attr->form == DW_FORM_ref_sig8)
22185 die = follow_die_sig (src_die, attr, ref_cu);
22186 else
22187 {
22188 dump_die_for_error (src_die);
22189 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22190 objfile_name ((*ref_cu)->per_objfile->objfile));
22191 }
22192
22193 return die;
22194 }
22195
22196 /* Follow reference OFFSET.
22197 On entry *REF_CU is the CU of the source die referencing OFFSET.
22198 On exit *REF_CU is the CU of the result.
22199 Returns NULL if OFFSET is invalid. */
22200
22201 static struct die_info *
22202 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22203 struct dwarf2_cu **ref_cu)
22204 {
22205 struct die_info temp_die;
22206 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22207 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22208
22209 gdb_assert (cu->per_cu != NULL);
22210
22211 target_cu = cu;
22212
22213 if (cu->per_cu->is_debug_types)
22214 {
22215 /* .debug_types CUs cannot reference anything outside their CU.
22216 If they need to, they have to reference a signatured type via
22217 DW_FORM_ref_sig8. */
22218 if (!cu->header.offset_in_cu_p (sect_off))
22219 return NULL;
22220 }
22221 else if (offset_in_dwz != cu->per_cu->is_dwz
22222 || !cu->header.offset_in_cu_p (sect_off))
22223 {
22224 struct dwarf2_per_cu_data *per_cu;
22225
22226 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22227 dwarf2_per_objfile);
22228
22229 /* If necessary, add it to the queue and load its DIEs. */
22230 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22231 load_full_comp_unit (per_cu, dwarf2_per_objfile, false, cu->language);
22232
22233 target_cu = per_cu->cu;
22234 }
22235 else if (cu->dies == NULL)
22236 {
22237 /* We're loading full DIEs during partial symbol reading. */
22238 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
22239 load_full_comp_unit (cu->per_cu, dwarf2_per_objfile, false,
22240 language_minimal);
22241 }
22242
22243 *ref_cu = target_cu;
22244 temp_die.sect_off = sect_off;
22245
22246 if (target_cu != cu)
22247 target_cu->ancestor = cu;
22248
22249 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22250 &temp_die,
22251 to_underlying (sect_off));
22252 }
22253
22254 /* Follow reference attribute ATTR of SRC_DIE.
22255 On entry *REF_CU is the CU of SRC_DIE.
22256 On exit *REF_CU is the CU of the result. */
22257
22258 static struct die_info *
22259 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22260 struct dwarf2_cu **ref_cu)
22261 {
22262 sect_offset sect_off = attr->get_ref_die_offset ();
22263 struct dwarf2_cu *cu = *ref_cu;
22264 struct die_info *die;
22265
22266 die = follow_die_offset (sect_off,
22267 (attr->form == DW_FORM_GNU_ref_alt
22268 || cu->per_cu->is_dwz),
22269 ref_cu);
22270 if (!die)
22271 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22272 "at %s [in module %s]"),
22273 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22274 objfile_name (cu->per_objfile->objfile));
22275
22276 return die;
22277 }
22278
22279 /* See read.h. */
22280
22281 struct dwarf2_locexpr_baton
22282 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22283 dwarf2_per_cu_data *per_cu,
22284 CORE_ADDR (*get_frame_pc) (void *baton),
22285 void *baton, bool resolve_abstract_p)
22286 {
22287 struct dwarf2_cu *cu;
22288 struct die_info *die;
22289 struct attribute *attr;
22290 struct dwarf2_locexpr_baton retval;
22291 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22292 struct objfile *objfile = dwarf2_per_objfile->objfile;
22293
22294 if (per_cu->cu == NULL)
22295 load_cu (per_cu, dwarf2_per_objfile, false);
22296 cu = per_cu->cu;
22297 if (cu == NULL)
22298 {
22299 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22300 Instead just throw an error, not much else we can do. */
22301 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22302 sect_offset_str (sect_off), objfile_name (objfile));
22303 }
22304
22305 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22306 if (!die)
22307 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22308 sect_offset_str (sect_off), objfile_name (objfile));
22309
22310 attr = dwarf2_attr (die, DW_AT_location, cu);
22311 if (!attr && resolve_abstract_p
22312 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22313 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
22314 {
22315 CORE_ADDR pc = (*get_frame_pc) (baton);
22316 CORE_ADDR baseaddr = objfile->text_section_offset ();
22317 struct gdbarch *gdbarch = objfile->arch ();
22318
22319 for (const auto &cand_off
22320 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22321 {
22322 struct dwarf2_cu *cand_cu = cu;
22323 struct die_info *cand
22324 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22325 if (!cand
22326 || !cand->parent
22327 || cand->parent->tag != DW_TAG_subprogram)
22328 continue;
22329
22330 CORE_ADDR pc_low, pc_high;
22331 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22332 if (pc_low == ((CORE_ADDR) -1))
22333 continue;
22334 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22335 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22336 if (!(pc_low <= pc && pc < pc_high))
22337 continue;
22338
22339 die = cand;
22340 attr = dwarf2_attr (die, DW_AT_location, cu);
22341 break;
22342 }
22343 }
22344
22345 if (!attr)
22346 {
22347 /* DWARF: "If there is no such attribute, then there is no effect.".
22348 DATA is ignored if SIZE is 0. */
22349
22350 retval.data = NULL;
22351 retval.size = 0;
22352 }
22353 else if (attr->form_is_section_offset ())
22354 {
22355 struct dwarf2_loclist_baton loclist_baton;
22356 CORE_ADDR pc = (*get_frame_pc) (baton);
22357 size_t size;
22358
22359 fill_in_loclist_baton (cu, &loclist_baton, attr);
22360
22361 retval.data = dwarf2_find_location_expression (&loclist_baton,
22362 &size, pc);
22363 retval.size = size;
22364 }
22365 else
22366 {
22367 if (!attr->form_is_block ())
22368 error (_("Dwarf Error: DIE at %s referenced in module %s "
22369 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22370 sect_offset_str (sect_off), objfile_name (objfile));
22371
22372 retval.data = DW_BLOCK (attr)->data;
22373 retval.size = DW_BLOCK (attr)->size;
22374 }
22375 retval.per_objfile = dwarf2_per_objfile;
22376 retval.per_cu = cu->per_cu;
22377
22378 age_cached_comp_units (dwarf2_per_objfile);
22379
22380 return retval;
22381 }
22382
22383 /* See read.h. */
22384
22385 struct dwarf2_locexpr_baton
22386 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22387 dwarf2_per_cu_data *per_cu,
22388 CORE_ADDR (*get_frame_pc) (void *baton),
22389 void *baton)
22390 {
22391 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22392
22393 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22394 }
22395
22396 /* Write a constant of a given type as target-ordered bytes into
22397 OBSTACK. */
22398
22399 static const gdb_byte *
22400 write_constant_as_bytes (struct obstack *obstack,
22401 enum bfd_endian byte_order,
22402 struct type *type,
22403 ULONGEST value,
22404 LONGEST *len)
22405 {
22406 gdb_byte *result;
22407
22408 *len = TYPE_LENGTH (type);
22409 result = (gdb_byte *) obstack_alloc (obstack, *len);
22410 store_unsigned_integer (result, *len, byte_order, value);
22411
22412 return result;
22413 }
22414
22415 /* See read.h. */
22416
22417 const gdb_byte *
22418 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22419 dwarf2_per_cu_data *per_cu,
22420 obstack *obstack,
22421 LONGEST *len)
22422 {
22423 struct dwarf2_cu *cu;
22424 struct die_info *die;
22425 struct attribute *attr;
22426 const gdb_byte *result = NULL;
22427 struct type *type;
22428 LONGEST value;
22429 enum bfd_endian byte_order;
22430 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22431
22432 if (per_cu->cu == NULL)
22433 load_cu (per_cu, per_cu->dwarf2_per_objfile, false);
22434 cu = per_cu->cu;
22435 if (cu == NULL)
22436 {
22437 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22438 Instead just throw an error, not much else we can do. */
22439 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22440 sect_offset_str (sect_off), objfile_name (objfile));
22441 }
22442
22443 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22444 if (!die)
22445 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22446 sect_offset_str (sect_off), objfile_name (objfile));
22447
22448 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22449 if (attr == NULL)
22450 return NULL;
22451
22452 byte_order = (bfd_big_endian (objfile->obfd)
22453 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22454
22455 switch (attr->form)
22456 {
22457 case DW_FORM_addr:
22458 case DW_FORM_addrx:
22459 case DW_FORM_GNU_addr_index:
22460 {
22461 gdb_byte *tem;
22462
22463 *len = cu->header.addr_size;
22464 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22465 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22466 result = tem;
22467 }
22468 break;
22469 case DW_FORM_string:
22470 case DW_FORM_strp:
22471 case DW_FORM_strx:
22472 case DW_FORM_GNU_str_index:
22473 case DW_FORM_GNU_strp_alt:
22474 /* DW_STRING is already allocated on the objfile obstack, point
22475 directly to it. */
22476 result = (const gdb_byte *) DW_STRING (attr);
22477 *len = strlen (DW_STRING (attr));
22478 break;
22479 case DW_FORM_block1:
22480 case DW_FORM_block2:
22481 case DW_FORM_block4:
22482 case DW_FORM_block:
22483 case DW_FORM_exprloc:
22484 case DW_FORM_data16:
22485 result = DW_BLOCK (attr)->data;
22486 *len = DW_BLOCK (attr)->size;
22487 break;
22488
22489 /* The DW_AT_const_value attributes are supposed to carry the
22490 symbol's value "represented as it would be on the target
22491 architecture." By the time we get here, it's already been
22492 converted to host endianness, so we just need to sign- or
22493 zero-extend it as appropriate. */
22494 case DW_FORM_data1:
22495 type = die_type (die, cu);
22496 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22497 if (result == NULL)
22498 result = write_constant_as_bytes (obstack, byte_order,
22499 type, value, len);
22500 break;
22501 case DW_FORM_data2:
22502 type = die_type (die, cu);
22503 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22504 if (result == NULL)
22505 result = write_constant_as_bytes (obstack, byte_order,
22506 type, value, len);
22507 break;
22508 case DW_FORM_data4:
22509 type = die_type (die, cu);
22510 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22511 if (result == NULL)
22512 result = write_constant_as_bytes (obstack, byte_order,
22513 type, value, len);
22514 break;
22515 case DW_FORM_data8:
22516 type = die_type (die, cu);
22517 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22518 if (result == NULL)
22519 result = write_constant_as_bytes (obstack, byte_order,
22520 type, value, len);
22521 break;
22522
22523 case DW_FORM_sdata:
22524 case DW_FORM_implicit_const:
22525 type = die_type (die, cu);
22526 result = write_constant_as_bytes (obstack, byte_order,
22527 type, DW_SND (attr), len);
22528 break;
22529
22530 case DW_FORM_udata:
22531 type = die_type (die, cu);
22532 result = write_constant_as_bytes (obstack, byte_order,
22533 type, DW_UNSND (attr), len);
22534 break;
22535
22536 default:
22537 complaint (_("unsupported const value attribute form: '%s'"),
22538 dwarf_form_name (attr->form));
22539 break;
22540 }
22541
22542 return result;
22543 }
22544
22545 /* See read.h. */
22546
22547 struct type *
22548 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22549 dwarf2_per_cu_data *per_cu)
22550 {
22551 struct dwarf2_cu *cu;
22552 struct die_info *die;
22553
22554 if (per_cu->cu == NULL)
22555 load_cu (per_cu, per_cu->dwarf2_per_objfile, false);
22556 cu = per_cu->cu;
22557 if (!cu)
22558 return NULL;
22559
22560 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22561 if (!die)
22562 return NULL;
22563
22564 return die_type (die, cu);
22565 }
22566
22567 /* See read.h. */
22568
22569 struct type *
22570 dwarf2_get_die_type (cu_offset die_offset,
22571 struct dwarf2_per_cu_data *per_cu)
22572 {
22573 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22574 return get_die_type_at_offset (die_offset_sect, per_cu);
22575 }
22576
22577 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22578 On entry *REF_CU is the CU of SRC_DIE.
22579 On exit *REF_CU is the CU of the result.
22580 Returns NULL if the referenced DIE isn't found. */
22581
22582 static struct die_info *
22583 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22584 struct dwarf2_cu **ref_cu)
22585 {
22586 struct die_info temp_die;
22587 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22588 struct die_info *die;
22589
22590 /* While it might be nice to assert sig_type->type == NULL here,
22591 we can get here for DW_AT_imported_declaration where we need
22592 the DIE not the type. */
22593
22594 /* If necessary, add it to the queue and load its DIEs. */
22595
22596 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22597 read_signatured_type (sig_type, (*ref_cu)->per_objfile);
22598
22599 sig_cu = sig_type->per_cu.cu;
22600 gdb_assert (sig_cu != NULL);
22601 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22602 temp_die.sect_off = sig_type->type_offset_in_section;
22603 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22604 to_underlying (temp_die.sect_off));
22605 if (die)
22606 {
22607 struct dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
22608
22609 /* For .gdb_index version 7 keep track of included TUs.
22610 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22611 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22612 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
22613 {
22614 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22615 }
22616
22617 *ref_cu = sig_cu;
22618 if (sig_cu != cu)
22619 sig_cu->ancestor = cu;
22620
22621 return die;
22622 }
22623
22624 return NULL;
22625 }
22626
22627 /* Follow signatured type referenced by ATTR in SRC_DIE.
22628 On entry *REF_CU is the CU of SRC_DIE.
22629 On exit *REF_CU is the CU of the result.
22630 The result is the DIE of the type.
22631 If the referenced type cannot be found an error is thrown. */
22632
22633 static struct die_info *
22634 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22635 struct dwarf2_cu **ref_cu)
22636 {
22637 ULONGEST signature = DW_SIGNATURE (attr);
22638 struct signatured_type *sig_type;
22639 struct die_info *die;
22640
22641 gdb_assert (attr->form == DW_FORM_ref_sig8);
22642
22643 sig_type = lookup_signatured_type (*ref_cu, signature);
22644 /* sig_type will be NULL if the signatured type is missing from
22645 the debug info. */
22646 if (sig_type == NULL)
22647 {
22648 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22649 " from DIE at %s [in module %s]"),
22650 hex_string (signature), sect_offset_str (src_die->sect_off),
22651 objfile_name ((*ref_cu)->per_objfile->objfile));
22652 }
22653
22654 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22655 if (die == NULL)
22656 {
22657 dump_die_for_error (src_die);
22658 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22659 " from DIE at %s [in module %s]"),
22660 hex_string (signature), sect_offset_str (src_die->sect_off),
22661 objfile_name ((*ref_cu)->per_objfile->objfile));
22662 }
22663
22664 return die;
22665 }
22666
22667 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22668 reading in and processing the type unit if necessary. */
22669
22670 static struct type *
22671 get_signatured_type (struct die_info *die, ULONGEST signature,
22672 struct dwarf2_cu *cu)
22673 {
22674 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22675 struct signatured_type *sig_type;
22676 struct dwarf2_cu *type_cu;
22677 struct die_info *type_die;
22678 struct type *type;
22679
22680 sig_type = lookup_signatured_type (cu, signature);
22681 /* sig_type will be NULL if the signatured type is missing from
22682 the debug info. */
22683 if (sig_type == NULL)
22684 {
22685 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22686 " from DIE at %s [in module %s]"),
22687 hex_string (signature), sect_offset_str (die->sect_off),
22688 objfile_name (dwarf2_per_objfile->objfile));
22689 return build_error_marker_type (cu, die);
22690 }
22691
22692 /* If we already know the type we're done. */
22693 if (sig_type->type != NULL)
22694 return sig_type->type;
22695
22696 type_cu = cu;
22697 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22698 if (type_die != NULL)
22699 {
22700 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22701 is created. This is important, for example, because for c++ classes
22702 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22703 type = read_type_die (type_die, type_cu);
22704 if (type == NULL)
22705 {
22706 complaint (_("Dwarf Error: Cannot build signatured type %s"
22707 " referenced from DIE at %s [in module %s]"),
22708 hex_string (signature), sect_offset_str (die->sect_off),
22709 objfile_name (dwarf2_per_objfile->objfile));
22710 type = build_error_marker_type (cu, die);
22711 }
22712 }
22713 else
22714 {
22715 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22716 " from DIE at %s [in module %s]"),
22717 hex_string (signature), sect_offset_str (die->sect_off),
22718 objfile_name (dwarf2_per_objfile->objfile));
22719 type = build_error_marker_type (cu, die);
22720 }
22721 sig_type->type = type;
22722
22723 return type;
22724 }
22725
22726 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22727 reading in and processing the type unit if necessary. */
22728
22729 static struct type *
22730 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22731 struct dwarf2_cu *cu) /* ARI: editCase function */
22732 {
22733 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22734 if (attr->form_is_ref ())
22735 {
22736 struct dwarf2_cu *type_cu = cu;
22737 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22738
22739 return read_type_die (type_die, type_cu);
22740 }
22741 else if (attr->form == DW_FORM_ref_sig8)
22742 {
22743 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22744 }
22745 else
22746 {
22747 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22748
22749 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22750 " at %s [in module %s]"),
22751 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22752 objfile_name (dwarf2_per_objfile->objfile));
22753 return build_error_marker_type (cu, die);
22754 }
22755 }
22756
22757 /* Load the DIEs associated with type unit PER_CU into memory. */
22758
22759 static void
22760 load_full_type_unit (dwarf2_per_cu_data *per_cu,
22761 dwarf2_per_objfile *per_objfile)
22762 {
22763 struct signatured_type *sig_type;
22764
22765 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22766 gdb_assert (! per_cu->type_unit_group_p ());
22767
22768 /* We have the per_cu, but we need the signatured_type.
22769 Fortunately this is an easy translation. */
22770 gdb_assert (per_cu->is_debug_types);
22771 sig_type = (struct signatured_type *) per_cu;
22772
22773 gdb_assert (per_cu->cu == NULL);
22774
22775 read_signatured_type (sig_type, per_objfile);
22776
22777 gdb_assert (per_cu->cu != NULL);
22778 }
22779
22780 /* Read in a signatured type and build its CU and DIEs.
22781 If the type is a stub for the real type in a DWO file,
22782 read in the real type from the DWO file as well. */
22783
22784 static void
22785 read_signatured_type (signatured_type *sig_type,
22786 dwarf2_per_objfile *per_objfile)
22787 {
22788 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22789
22790 gdb_assert (per_cu->is_debug_types);
22791 gdb_assert (per_cu->cu == NULL);
22792
22793 cutu_reader reader (per_cu, per_objfile, NULL, 0, false);
22794
22795 if (!reader.dummy_p)
22796 {
22797 struct dwarf2_cu *cu = reader.cu;
22798 const gdb_byte *info_ptr = reader.info_ptr;
22799
22800 gdb_assert (cu->die_hash == NULL);
22801 cu->die_hash =
22802 htab_create_alloc_ex (cu->header.length / 12,
22803 die_hash,
22804 die_eq,
22805 NULL,
22806 &cu->comp_unit_obstack,
22807 hashtab_obstack_allocate,
22808 dummy_obstack_deallocate);
22809
22810 if (reader.comp_unit_die->has_children)
22811 reader.comp_unit_die->child
22812 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22813 reader.comp_unit_die);
22814 cu->dies = reader.comp_unit_die;
22815 /* comp_unit_die is not stored in die_hash, no need. */
22816
22817 /* We try not to read any attributes in this function, because
22818 not all CUs needed for references have been loaded yet, and
22819 symbol table processing isn't initialized. But we have to
22820 set the CU language, or we won't be able to build types
22821 correctly. Similarly, if we do not read the producer, we can
22822 not apply producer-specific interpretation. */
22823 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22824
22825 reader.keep ();
22826 }
22827
22828 sig_type->per_cu.tu_read = 1;
22829 }
22830
22831 /* Decode simple location descriptions.
22832 Given a pointer to a dwarf block that defines a location, compute
22833 the location and return the value. If COMPUTED is non-null, it is
22834 set to true to indicate that decoding was successful, and false
22835 otherwise. If COMPUTED is null, then this function may emit a
22836 complaint. */
22837
22838 static CORE_ADDR
22839 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22840 {
22841 struct objfile *objfile = cu->per_objfile->objfile;
22842 size_t i;
22843 size_t size = blk->size;
22844 const gdb_byte *data = blk->data;
22845 CORE_ADDR stack[64];
22846 int stacki;
22847 unsigned int bytes_read, unsnd;
22848 gdb_byte op;
22849
22850 if (computed != nullptr)
22851 *computed = false;
22852
22853 i = 0;
22854 stacki = 0;
22855 stack[stacki] = 0;
22856 stack[++stacki] = 0;
22857
22858 while (i < size)
22859 {
22860 op = data[i++];
22861 switch (op)
22862 {
22863 case DW_OP_lit0:
22864 case DW_OP_lit1:
22865 case DW_OP_lit2:
22866 case DW_OP_lit3:
22867 case DW_OP_lit4:
22868 case DW_OP_lit5:
22869 case DW_OP_lit6:
22870 case DW_OP_lit7:
22871 case DW_OP_lit8:
22872 case DW_OP_lit9:
22873 case DW_OP_lit10:
22874 case DW_OP_lit11:
22875 case DW_OP_lit12:
22876 case DW_OP_lit13:
22877 case DW_OP_lit14:
22878 case DW_OP_lit15:
22879 case DW_OP_lit16:
22880 case DW_OP_lit17:
22881 case DW_OP_lit18:
22882 case DW_OP_lit19:
22883 case DW_OP_lit20:
22884 case DW_OP_lit21:
22885 case DW_OP_lit22:
22886 case DW_OP_lit23:
22887 case DW_OP_lit24:
22888 case DW_OP_lit25:
22889 case DW_OP_lit26:
22890 case DW_OP_lit27:
22891 case DW_OP_lit28:
22892 case DW_OP_lit29:
22893 case DW_OP_lit30:
22894 case DW_OP_lit31:
22895 stack[++stacki] = op - DW_OP_lit0;
22896 break;
22897
22898 case DW_OP_reg0:
22899 case DW_OP_reg1:
22900 case DW_OP_reg2:
22901 case DW_OP_reg3:
22902 case DW_OP_reg4:
22903 case DW_OP_reg5:
22904 case DW_OP_reg6:
22905 case DW_OP_reg7:
22906 case DW_OP_reg8:
22907 case DW_OP_reg9:
22908 case DW_OP_reg10:
22909 case DW_OP_reg11:
22910 case DW_OP_reg12:
22911 case DW_OP_reg13:
22912 case DW_OP_reg14:
22913 case DW_OP_reg15:
22914 case DW_OP_reg16:
22915 case DW_OP_reg17:
22916 case DW_OP_reg18:
22917 case DW_OP_reg19:
22918 case DW_OP_reg20:
22919 case DW_OP_reg21:
22920 case DW_OP_reg22:
22921 case DW_OP_reg23:
22922 case DW_OP_reg24:
22923 case DW_OP_reg25:
22924 case DW_OP_reg26:
22925 case DW_OP_reg27:
22926 case DW_OP_reg28:
22927 case DW_OP_reg29:
22928 case DW_OP_reg30:
22929 case DW_OP_reg31:
22930 stack[++stacki] = op - DW_OP_reg0;
22931 if (i < size)
22932 {
22933 if (computed == nullptr)
22934 dwarf2_complex_location_expr_complaint ();
22935 else
22936 return 0;
22937 }
22938 break;
22939
22940 case DW_OP_regx:
22941 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22942 i += bytes_read;
22943 stack[++stacki] = unsnd;
22944 if (i < size)
22945 {
22946 if (computed == nullptr)
22947 dwarf2_complex_location_expr_complaint ();
22948 else
22949 return 0;
22950 }
22951 break;
22952
22953 case DW_OP_addr:
22954 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22955 &bytes_read);
22956 i += bytes_read;
22957 break;
22958
22959 case DW_OP_const1u:
22960 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22961 i += 1;
22962 break;
22963
22964 case DW_OP_const1s:
22965 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22966 i += 1;
22967 break;
22968
22969 case DW_OP_const2u:
22970 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22971 i += 2;
22972 break;
22973
22974 case DW_OP_const2s:
22975 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22976 i += 2;
22977 break;
22978
22979 case DW_OP_const4u:
22980 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22981 i += 4;
22982 break;
22983
22984 case DW_OP_const4s:
22985 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22986 i += 4;
22987 break;
22988
22989 case DW_OP_const8u:
22990 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22991 i += 8;
22992 break;
22993
22994 case DW_OP_constu:
22995 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22996 &bytes_read);
22997 i += bytes_read;
22998 break;
22999
23000 case DW_OP_consts:
23001 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23002 i += bytes_read;
23003 break;
23004
23005 case DW_OP_dup:
23006 stack[stacki + 1] = stack[stacki];
23007 stacki++;
23008 break;
23009
23010 case DW_OP_plus:
23011 stack[stacki - 1] += stack[stacki];
23012 stacki--;
23013 break;
23014
23015 case DW_OP_plus_uconst:
23016 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23017 &bytes_read);
23018 i += bytes_read;
23019 break;
23020
23021 case DW_OP_minus:
23022 stack[stacki - 1] -= stack[stacki];
23023 stacki--;
23024 break;
23025
23026 case DW_OP_deref:
23027 /* If we're not the last op, then we definitely can't encode
23028 this using GDB's address_class enum. This is valid for partial
23029 global symbols, although the variable's address will be bogus
23030 in the psymtab. */
23031 if (i < size)
23032 {
23033 if (computed == nullptr)
23034 dwarf2_complex_location_expr_complaint ();
23035 else
23036 return 0;
23037 }
23038 break;
23039
23040 case DW_OP_GNU_push_tls_address:
23041 case DW_OP_form_tls_address:
23042 /* The top of the stack has the offset from the beginning
23043 of the thread control block at which the variable is located. */
23044 /* Nothing should follow this operator, so the top of stack would
23045 be returned. */
23046 /* This is valid for partial global symbols, but the variable's
23047 address will be bogus in the psymtab. Make it always at least
23048 non-zero to not look as a variable garbage collected by linker
23049 which have DW_OP_addr 0. */
23050 if (i < size)
23051 {
23052 if (computed == nullptr)
23053 dwarf2_complex_location_expr_complaint ();
23054 else
23055 return 0;
23056 }
23057 stack[stacki]++;
23058 break;
23059
23060 case DW_OP_GNU_uninit:
23061 if (computed != nullptr)
23062 return 0;
23063 break;
23064
23065 case DW_OP_addrx:
23066 case DW_OP_GNU_addr_index:
23067 case DW_OP_GNU_const_index:
23068 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23069 &bytes_read);
23070 i += bytes_read;
23071 break;
23072
23073 default:
23074 if (computed == nullptr)
23075 {
23076 const char *name = get_DW_OP_name (op);
23077
23078 if (name)
23079 complaint (_("unsupported stack op: '%s'"),
23080 name);
23081 else
23082 complaint (_("unsupported stack op: '%02x'"),
23083 op);
23084 }
23085
23086 return (stack[stacki]);
23087 }
23088
23089 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23090 outside of the allocated space. Also enforce minimum>0. */
23091 if (stacki >= ARRAY_SIZE (stack) - 1)
23092 {
23093 if (computed == nullptr)
23094 complaint (_("location description stack overflow"));
23095 return 0;
23096 }
23097
23098 if (stacki <= 0)
23099 {
23100 if (computed == nullptr)
23101 complaint (_("location description stack underflow"));
23102 return 0;
23103 }
23104 }
23105
23106 if (computed != nullptr)
23107 *computed = true;
23108 return (stack[stacki]);
23109 }
23110
23111 /* memory allocation interface */
23112
23113 static struct dwarf_block *
23114 dwarf_alloc_block (struct dwarf2_cu *cu)
23115 {
23116 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23117 }
23118
23119 static struct die_info *
23120 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23121 {
23122 struct die_info *die;
23123 size_t size = sizeof (struct die_info);
23124
23125 if (num_attrs > 1)
23126 size += (num_attrs - 1) * sizeof (struct attribute);
23127
23128 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23129 memset (die, 0, sizeof (struct die_info));
23130 return (die);
23131 }
23132
23133 \f
23134
23135 /* Macro support. */
23136
23137 /* An overload of dwarf_decode_macros that finds the correct section
23138 and ensures it is read in before calling the other overload. */
23139
23140 static void
23141 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23142 int section_is_gnu)
23143 {
23144 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23145 struct objfile *objfile = dwarf2_per_objfile->objfile;
23146 const struct line_header *lh = cu->line_header;
23147 unsigned int offset_size = cu->header.offset_size;
23148 struct dwarf2_section_info *section;
23149 const char *section_name;
23150
23151 if (cu->dwo_unit != nullptr)
23152 {
23153 if (section_is_gnu)
23154 {
23155 section = &cu->dwo_unit->dwo_file->sections.macro;
23156 section_name = ".debug_macro.dwo";
23157 }
23158 else
23159 {
23160 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23161 section_name = ".debug_macinfo.dwo";
23162 }
23163 }
23164 else
23165 {
23166 if (section_is_gnu)
23167 {
23168 section = &dwarf2_per_objfile->per_bfd->macro;
23169 section_name = ".debug_macro";
23170 }
23171 else
23172 {
23173 section = &dwarf2_per_objfile->per_bfd->macinfo;
23174 section_name = ".debug_macinfo";
23175 }
23176 }
23177
23178 section->read (objfile);
23179 if (section->buffer == nullptr)
23180 {
23181 complaint (_("missing %s section"), section_name);
23182 return;
23183 }
23184
23185 buildsym_compunit *builder = cu->get_builder ();
23186
23187 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23188 offset_size, offset, section_is_gnu);
23189 }
23190
23191 /* Return the .debug_loc section to use for CU.
23192 For DWO files use .debug_loc.dwo. */
23193
23194 static struct dwarf2_section_info *
23195 cu_debug_loc_section (struct dwarf2_cu *cu)
23196 {
23197 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23198
23199 if (cu->dwo_unit)
23200 {
23201 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23202
23203 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23204 }
23205 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23206 : &dwarf2_per_objfile->per_bfd->loc);
23207 }
23208
23209 /* A helper function that fills in a dwarf2_loclist_baton. */
23210
23211 static void
23212 fill_in_loclist_baton (struct dwarf2_cu *cu,
23213 struct dwarf2_loclist_baton *baton,
23214 const struct attribute *attr)
23215 {
23216 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23217 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23218
23219 section->read (dwarf2_per_objfile->objfile);
23220
23221 baton->per_objfile = dwarf2_per_objfile;
23222 baton->per_cu = cu->per_cu;
23223 gdb_assert (baton->per_cu);
23224 /* We don't know how long the location list is, but make sure we
23225 don't run off the edge of the section. */
23226 baton->size = section->size - DW_UNSND (attr);
23227 baton->data = section->buffer + DW_UNSND (attr);
23228 if (cu->base_address.has_value ())
23229 baton->base_address = *cu->base_address;
23230 else
23231 baton->base_address = 0;
23232 baton->from_dwo = cu->dwo_unit != NULL;
23233 }
23234
23235 static void
23236 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23237 struct dwarf2_cu *cu, int is_block)
23238 {
23239 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23240 struct objfile *objfile = dwarf2_per_objfile->objfile;
23241 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23242
23243 if (attr->form_is_section_offset ()
23244 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23245 the section. If so, fall through to the complaint in the
23246 other branch. */
23247 && DW_UNSND (attr) < section->get_size (objfile))
23248 {
23249 struct dwarf2_loclist_baton *baton;
23250
23251 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23252
23253 fill_in_loclist_baton (cu, baton, attr);
23254
23255 if (!cu->base_address.has_value ())
23256 complaint (_("Location list used without "
23257 "specifying the CU base address."));
23258
23259 SYMBOL_ACLASS_INDEX (sym) = (is_block
23260 ? dwarf2_loclist_block_index
23261 : dwarf2_loclist_index);
23262 SYMBOL_LOCATION_BATON (sym) = baton;
23263 }
23264 else
23265 {
23266 struct dwarf2_locexpr_baton *baton;
23267
23268 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23269 baton->per_objfile = dwarf2_per_objfile;
23270 baton->per_cu = cu->per_cu;
23271 gdb_assert (baton->per_cu);
23272
23273 if (attr->form_is_block ())
23274 {
23275 /* Note that we're just copying the block's data pointer
23276 here, not the actual data. We're still pointing into the
23277 info_buffer for SYM's objfile; right now we never release
23278 that buffer, but when we do clean up properly this may
23279 need to change. */
23280 baton->size = DW_BLOCK (attr)->size;
23281 baton->data = DW_BLOCK (attr)->data;
23282 }
23283 else
23284 {
23285 dwarf2_invalid_attrib_class_complaint ("location description",
23286 sym->natural_name ());
23287 baton->size = 0;
23288 }
23289
23290 SYMBOL_ACLASS_INDEX (sym) = (is_block
23291 ? dwarf2_locexpr_block_index
23292 : dwarf2_locexpr_index);
23293 SYMBOL_LOCATION_BATON (sym) = baton;
23294 }
23295 }
23296
23297 /* See read.h. */
23298
23299 struct objfile *
23300 dwarf2_per_cu_data::objfile () const
23301 {
23302 struct objfile *objfile = dwarf2_per_objfile->objfile;
23303
23304 /* Return the master objfile, so that we can report and look up the
23305 correct file containing this variable. */
23306 if (objfile->separate_debug_objfile_backlink)
23307 objfile = objfile->separate_debug_objfile_backlink;
23308
23309 return objfile;
23310 }
23311
23312 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23313 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23314 CU_HEADERP first. */
23315
23316 static const struct comp_unit_head *
23317 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23318 const struct dwarf2_per_cu_data *per_cu)
23319 {
23320 const gdb_byte *info_ptr;
23321
23322 if (per_cu->cu)
23323 return &per_cu->cu->header;
23324
23325 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
23326
23327 memset (cu_headerp, 0, sizeof (*cu_headerp));
23328 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23329 rcuh_kind::COMPILE);
23330
23331 return cu_headerp;
23332 }
23333
23334 /* See read.h. */
23335
23336 int
23337 dwarf2_per_cu_data::addr_size () const
23338 {
23339 struct comp_unit_head cu_header_local;
23340 const struct comp_unit_head *cu_headerp;
23341
23342 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23343
23344 return cu_headerp->addr_size;
23345 }
23346
23347 /* See read.h. */
23348
23349 int
23350 dwarf2_per_cu_data::offset_size () const
23351 {
23352 struct comp_unit_head cu_header_local;
23353 const struct comp_unit_head *cu_headerp;
23354
23355 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23356
23357 return cu_headerp->offset_size;
23358 }
23359
23360 /* See read.h. */
23361
23362 int
23363 dwarf2_per_cu_data::ref_addr_size () const
23364 {
23365 struct comp_unit_head cu_header_local;
23366 const struct comp_unit_head *cu_headerp;
23367
23368 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23369
23370 if (cu_headerp->version == 2)
23371 return cu_headerp->addr_size;
23372 else
23373 return cu_headerp->offset_size;
23374 }
23375
23376 /* See read.h. */
23377
23378 CORE_ADDR
23379 dwarf2_per_cu_data::text_offset () const
23380 {
23381 struct objfile *objfile = dwarf2_per_objfile->objfile;
23382
23383 return objfile->text_section_offset ();
23384 }
23385
23386 /* See read.h. */
23387
23388 struct type *
23389 dwarf2_per_cu_data::addr_type () const
23390 {
23391 struct objfile *objfile = dwarf2_per_objfile->objfile;
23392 struct type *void_type = objfile_type (objfile)->builtin_void;
23393 struct type *addr_type = lookup_pointer_type (void_type);
23394 int addr_size = this->addr_size ();
23395
23396 if (TYPE_LENGTH (addr_type) == addr_size)
23397 return addr_type;
23398
23399 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23400 return addr_type;
23401 }
23402
23403 /* A helper function for dwarf2_find_containing_comp_unit that returns
23404 the index of the result, and that searches a vector. It will
23405 return a result even if the offset in question does not actually
23406 occur in any CU. This is separate so that it can be unit
23407 tested. */
23408
23409 static int
23410 dwarf2_find_containing_comp_unit
23411 (sect_offset sect_off,
23412 unsigned int offset_in_dwz,
23413 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23414 {
23415 int low, high;
23416
23417 low = 0;
23418 high = all_comp_units.size () - 1;
23419 while (high > low)
23420 {
23421 struct dwarf2_per_cu_data *mid_cu;
23422 int mid = low + (high - low) / 2;
23423
23424 mid_cu = all_comp_units[mid];
23425 if (mid_cu->is_dwz > offset_in_dwz
23426 || (mid_cu->is_dwz == offset_in_dwz
23427 && mid_cu->sect_off + mid_cu->length > sect_off))
23428 high = mid;
23429 else
23430 low = mid + 1;
23431 }
23432 gdb_assert (low == high);
23433 return low;
23434 }
23435
23436 /* Locate the .debug_info compilation unit from CU's objfile which contains
23437 the DIE at OFFSET. Raises an error on failure. */
23438
23439 static struct dwarf2_per_cu_data *
23440 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23441 unsigned int offset_in_dwz,
23442 struct dwarf2_per_objfile *dwarf2_per_objfile)
23443 {
23444 int low
23445 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23446 dwarf2_per_objfile->per_bfd->all_comp_units);
23447 struct dwarf2_per_cu_data *this_cu
23448 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
23449
23450 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23451 {
23452 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23453 error (_("Dwarf Error: could not find partial DIE containing "
23454 "offset %s [in module %s]"),
23455 sect_offset_str (sect_off),
23456 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23457
23458 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
23459 <= sect_off);
23460 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
23461 }
23462 else
23463 {
23464 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
23465 && sect_off >= this_cu->sect_off + this_cu->length)
23466 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23467 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23468 return this_cu;
23469 }
23470 }
23471
23472 #if GDB_SELF_TEST
23473
23474 namespace selftests {
23475 namespace find_containing_comp_unit {
23476
23477 static void
23478 run_test ()
23479 {
23480 struct dwarf2_per_cu_data one {};
23481 struct dwarf2_per_cu_data two {};
23482 struct dwarf2_per_cu_data three {};
23483 struct dwarf2_per_cu_data four {};
23484
23485 one.length = 5;
23486 two.sect_off = sect_offset (one.length);
23487 two.length = 7;
23488
23489 three.length = 5;
23490 three.is_dwz = 1;
23491 four.sect_off = sect_offset (three.length);
23492 four.length = 7;
23493 four.is_dwz = 1;
23494
23495 std::vector<dwarf2_per_cu_data *> units;
23496 units.push_back (&one);
23497 units.push_back (&two);
23498 units.push_back (&three);
23499 units.push_back (&four);
23500
23501 int result;
23502
23503 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23504 SELF_CHECK (units[result] == &one);
23505 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23506 SELF_CHECK (units[result] == &one);
23507 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23508 SELF_CHECK (units[result] == &two);
23509
23510 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23511 SELF_CHECK (units[result] == &three);
23512 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23513 SELF_CHECK (units[result] == &three);
23514 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23515 SELF_CHECK (units[result] == &four);
23516 }
23517
23518 }
23519 }
23520
23521 #endif /* GDB_SELF_TEST */
23522
23523 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
23524
23525 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23526 dwarf2_per_objfile *per_objfile)
23527 : per_cu (per_cu),
23528 per_objfile (per_objfile),
23529 mark (false),
23530 has_loclist (false),
23531 checked_producer (false),
23532 producer_is_gxx_lt_4_6 (false),
23533 producer_is_gcc_lt_4_3 (false),
23534 producer_is_icc (false),
23535 producer_is_icc_lt_14 (false),
23536 producer_is_codewarrior (false),
23537 processing_has_namespace_info (false)
23538 {
23539 per_cu->cu = this;
23540 }
23541
23542 /* Destroy a dwarf2_cu. */
23543
23544 dwarf2_cu::~dwarf2_cu ()
23545 {
23546 per_cu->cu = NULL;
23547 }
23548
23549 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23550
23551 static void
23552 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23553 enum language pretend_language)
23554 {
23555 struct attribute *attr;
23556
23557 /* Set the language we're debugging. */
23558 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23559 if (attr != nullptr)
23560 set_cu_language (DW_UNSND (attr), cu);
23561 else
23562 {
23563 cu->language = pretend_language;
23564 cu->language_defn = language_def (cu->language);
23565 }
23566
23567 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23568 }
23569
23570 /* Increase the age counter on each cached compilation unit, and free
23571 any that are too old. */
23572
23573 static void
23574 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23575 {
23576 struct dwarf2_per_cu_data *per_cu, **last_chain;
23577
23578 dwarf2_clear_marks (dwarf2_per_objfile->per_bfd->read_in_chain);
23579 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23580 while (per_cu != NULL)
23581 {
23582 per_cu->cu->last_used ++;
23583 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23584 dwarf2_mark (per_cu->cu);
23585 per_cu = per_cu->cu->read_in_chain;
23586 }
23587
23588 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23589 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23590 while (per_cu != NULL)
23591 {
23592 struct dwarf2_per_cu_data *next_cu;
23593
23594 next_cu = per_cu->cu->read_in_chain;
23595
23596 if (!per_cu->cu->mark)
23597 {
23598 delete per_cu->cu;
23599 *last_chain = next_cu;
23600 }
23601 else
23602 last_chain = &per_cu->cu->read_in_chain;
23603
23604 per_cu = next_cu;
23605 }
23606 }
23607
23608 /* Remove a single compilation unit from the cache. */
23609
23610 static void
23611 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23612 {
23613 struct dwarf2_per_cu_data *per_cu, **last_chain;
23614 struct dwarf2_per_objfile *dwarf2_per_objfile
23615 = target_per_cu->dwarf2_per_objfile;
23616
23617 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23618 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23619 while (per_cu != NULL)
23620 {
23621 struct dwarf2_per_cu_data *next_cu;
23622
23623 next_cu = per_cu->cu->read_in_chain;
23624
23625 if (per_cu == target_per_cu)
23626 {
23627 delete per_cu->cu;
23628 per_cu->cu = NULL;
23629 *last_chain = next_cu;
23630 break;
23631 }
23632 else
23633 last_chain = &per_cu->cu->read_in_chain;
23634
23635 per_cu = next_cu;
23636 }
23637 }
23638
23639 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23640 We store these in a hash table separate from the DIEs, and preserve them
23641 when the DIEs are flushed out of cache.
23642
23643 The CU "per_cu" pointer is needed because offset alone is not enough to
23644 uniquely identify the type. A file may have multiple .debug_types sections,
23645 or the type may come from a DWO file. Furthermore, while it's more logical
23646 to use per_cu->section+offset, with Fission the section with the data is in
23647 the DWO file but we don't know that section at the point we need it.
23648 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23649 because we can enter the lookup routine, get_die_type_at_offset, from
23650 outside this file, and thus won't necessarily have PER_CU->cu.
23651 Fortunately, PER_CU is stable for the life of the objfile. */
23652
23653 struct dwarf2_per_cu_offset_and_type
23654 {
23655 const struct dwarf2_per_cu_data *per_cu;
23656 sect_offset sect_off;
23657 struct type *type;
23658 };
23659
23660 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23661
23662 static hashval_t
23663 per_cu_offset_and_type_hash (const void *item)
23664 {
23665 const struct dwarf2_per_cu_offset_and_type *ofs
23666 = (const struct dwarf2_per_cu_offset_and_type *) item;
23667
23668 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23669 }
23670
23671 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23672
23673 static int
23674 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23675 {
23676 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23677 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23678 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23679 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23680
23681 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23682 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23683 }
23684
23685 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23686 table if necessary. For convenience, return TYPE.
23687
23688 The DIEs reading must have careful ordering to:
23689 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23690 reading current DIE.
23691 * Not trying to dereference contents of still incompletely read in types
23692 while reading in other DIEs.
23693 * Enable referencing still incompletely read in types just by a pointer to
23694 the type without accessing its fields.
23695
23696 Therefore caller should follow these rules:
23697 * Try to fetch any prerequisite types we may need to build this DIE type
23698 before building the type and calling set_die_type.
23699 * After building type call set_die_type for current DIE as soon as
23700 possible before fetching more types to complete the current type.
23701 * Make the type as complete as possible before fetching more types. */
23702
23703 static struct type *
23704 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23705 {
23706 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23707 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23708 struct objfile *objfile = dwarf2_per_objfile->objfile;
23709 struct attribute *attr;
23710 struct dynamic_prop prop;
23711
23712 /* For Ada types, make sure that the gnat-specific data is always
23713 initialized (if not already set). There are a few types where
23714 we should not be doing so, because the type-specific area is
23715 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23716 where the type-specific area is used to store the floatformat).
23717 But this is not a problem, because the gnat-specific information
23718 is actually not needed for these types. */
23719 if (need_gnat_info (cu)
23720 && type->code () != TYPE_CODE_FUNC
23721 && type->code () != TYPE_CODE_FLT
23722 && type->code () != TYPE_CODE_METHODPTR
23723 && type->code () != TYPE_CODE_MEMBERPTR
23724 && type->code () != TYPE_CODE_METHOD
23725 && !HAVE_GNAT_AUX_INFO (type))
23726 INIT_GNAT_SPECIFIC (type);
23727
23728 /* Read DW_AT_allocated and set in type. */
23729 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23730 if (attr != NULL && attr->form_is_block ())
23731 {
23732 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23733 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23734 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23735 }
23736 else if (attr != NULL)
23737 {
23738 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23739 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23740 sect_offset_str (die->sect_off));
23741 }
23742
23743 /* Read DW_AT_associated and set in type. */
23744 attr = dwarf2_attr (die, DW_AT_associated, cu);
23745 if (attr != NULL && attr->form_is_block ())
23746 {
23747 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23748 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23749 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23750 }
23751 else if (attr != NULL)
23752 {
23753 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23754 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23755 sect_offset_str (die->sect_off));
23756 }
23757
23758 /* Read DW_AT_data_location and set in type. */
23759 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23760 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23761 cu->per_cu->addr_type ()))
23762 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23763
23764 if (dwarf2_per_objfile->die_type_hash == NULL)
23765 dwarf2_per_objfile->die_type_hash
23766 = htab_up (htab_create_alloc (127,
23767 per_cu_offset_and_type_hash,
23768 per_cu_offset_and_type_eq,
23769 NULL, xcalloc, xfree));
23770
23771 ofs.per_cu = cu->per_cu;
23772 ofs.sect_off = die->sect_off;
23773 ofs.type = type;
23774 slot = (struct dwarf2_per_cu_offset_and_type **)
23775 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23776 if (*slot)
23777 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23778 sect_offset_str (die->sect_off));
23779 *slot = XOBNEW (&objfile->objfile_obstack,
23780 struct dwarf2_per_cu_offset_and_type);
23781 **slot = ofs;
23782 return type;
23783 }
23784
23785 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23786 or return NULL if the die does not have a saved type. */
23787
23788 static struct type *
23789 get_die_type_at_offset (sect_offset sect_off,
23790 struct dwarf2_per_cu_data *per_cu)
23791 {
23792 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23793 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23794
23795 if (dwarf2_per_objfile->die_type_hash == NULL)
23796 return NULL;
23797
23798 ofs.per_cu = per_cu;
23799 ofs.sect_off = sect_off;
23800 slot = ((struct dwarf2_per_cu_offset_and_type *)
23801 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23802 if (slot)
23803 return slot->type;
23804 else
23805 return NULL;
23806 }
23807
23808 /* Look up the type for DIE in CU in die_type_hash,
23809 or return NULL if DIE does not have a saved type. */
23810
23811 static struct type *
23812 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23813 {
23814 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23815 }
23816
23817 /* Add a dependence relationship from CU to REF_PER_CU. */
23818
23819 static void
23820 dwarf2_add_dependence (struct dwarf2_cu *cu,
23821 struct dwarf2_per_cu_data *ref_per_cu)
23822 {
23823 void **slot;
23824
23825 if (cu->dependencies == NULL)
23826 cu->dependencies
23827 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23828 NULL, &cu->comp_unit_obstack,
23829 hashtab_obstack_allocate,
23830 dummy_obstack_deallocate);
23831
23832 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23833 if (*slot == NULL)
23834 *slot = ref_per_cu;
23835 }
23836
23837 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23838 Set the mark field in every compilation unit in the
23839 cache that we must keep because we are keeping CU. */
23840
23841 static int
23842 dwarf2_mark_helper (void **slot, void *data)
23843 {
23844 struct dwarf2_per_cu_data *per_cu;
23845
23846 per_cu = (struct dwarf2_per_cu_data *) *slot;
23847
23848 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23849 reading of the chain. As such dependencies remain valid it is not much
23850 useful to track and undo them during QUIT cleanups. */
23851 if (per_cu->cu == NULL)
23852 return 1;
23853
23854 if (per_cu->cu->mark)
23855 return 1;
23856 per_cu->cu->mark = true;
23857
23858 if (per_cu->cu->dependencies != NULL)
23859 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23860
23861 return 1;
23862 }
23863
23864 /* Set the mark field in CU and in every other compilation unit in the
23865 cache that we must keep because we are keeping CU. */
23866
23867 static void
23868 dwarf2_mark (struct dwarf2_cu *cu)
23869 {
23870 if (cu->mark)
23871 return;
23872 cu->mark = true;
23873 if (cu->dependencies != NULL)
23874 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23875 }
23876
23877 static void
23878 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23879 {
23880 while (per_cu)
23881 {
23882 per_cu->cu->mark = false;
23883 per_cu = per_cu->cu->read_in_chain;
23884 }
23885 }
23886
23887 /* Trivial hash function for partial_die_info: the hash value of a DIE
23888 is its offset in .debug_info for this objfile. */
23889
23890 static hashval_t
23891 partial_die_hash (const void *item)
23892 {
23893 const struct partial_die_info *part_die
23894 = (const struct partial_die_info *) item;
23895
23896 return to_underlying (part_die->sect_off);
23897 }
23898
23899 /* Trivial comparison function for partial_die_info structures: two DIEs
23900 are equal if they have the same offset. */
23901
23902 static int
23903 partial_die_eq (const void *item_lhs, const void *item_rhs)
23904 {
23905 const struct partial_die_info *part_die_lhs
23906 = (const struct partial_die_info *) item_lhs;
23907 const struct partial_die_info *part_die_rhs
23908 = (const struct partial_die_info *) item_rhs;
23909
23910 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23911 }
23912
23913 struct cmd_list_element *set_dwarf_cmdlist;
23914 struct cmd_list_element *show_dwarf_cmdlist;
23915
23916 static void
23917 show_check_physname (struct ui_file *file, int from_tty,
23918 struct cmd_list_element *c, const char *value)
23919 {
23920 fprintf_filtered (file,
23921 _("Whether to check \"physname\" is %s.\n"),
23922 value);
23923 }
23924
23925 void _initialize_dwarf2_read ();
23926 void
23927 _initialize_dwarf2_read ()
23928 {
23929 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
23930 Set DWARF specific variables.\n\
23931 Configure DWARF variables such as the cache size."),
23932 &set_dwarf_cmdlist, "maintenance set dwarf ",
23933 0/*allow-unknown*/, &maintenance_set_cmdlist);
23934
23935 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
23936 Show DWARF specific variables.\n\
23937 Show DWARF variables such as the cache size."),
23938 &show_dwarf_cmdlist, "maintenance show dwarf ",
23939 0/*allow-unknown*/, &maintenance_show_cmdlist);
23940
23941 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23942 &dwarf_max_cache_age, _("\
23943 Set the upper bound on the age of cached DWARF compilation units."), _("\
23944 Show the upper bound on the age of cached DWARF compilation units."), _("\
23945 A higher limit means that cached compilation units will be stored\n\
23946 in memory longer, and more total memory will be used. Zero disables\n\
23947 caching, which can slow down startup."),
23948 NULL,
23949 show_dwarf_max_cache_age,
23950 &set_dwarf_cmdlist,
23951 &show_dwarf_cmdlist);
23952
23953 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23954 Set debugging of the DWARF reader."), _("\
23955 Show debugging of the DWARF reader."), _("\
23956 When enabled (non-zero), debugging messages are printed during DWARF\n\
23957 reading and symtab expansion. A value of 1 (one) provides basic\n\
23958 information. A value greater than 1 provides more verbose information."),
23959 NULL,
23960 NULL,
23961 &setdebuglist, &showdebuglist);
23962
23963 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23964 Set debugging of the DWARF DIE reader."), _("\
23965 Show debugging of the DWARF DIE reader."), _("\
23966 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23967 The value is the maximum depth to print."),
23968 NULL,
23969 NULL,
23970 &setdebuglist, &showdebuglist);
23971
23972 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23973 Set debugging of the dwarf line reader."), _("\
23974 Show debugging of the dwarf line reader."), _("\
23975 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23976 A value of 1 (one) provides basic information.\n\
23977 A value greater than 1 provides more verbose information."),
23978 NULL,
23979 NULL,
23980 &setdebuglist, &showdebuglist);
23981
23982 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23983 Set cross-checking of \"physname\" code against demangler."), _("\
23984 Show cross-checking of \"physname\" code against demangler."), _("\
23985 When enabled, GDB's internal \"physname\" code is checked against\n\
23986 the demangler."),
23987 NULL, show_check_physname,
23988 &setdebuglist, &showdebuglist);
23989
23990 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23991 no_class, &use_deprecated_index_sections, _("\
23992 Set whether to use deprecated gdb_index sections."), _("\
23993 Show whether to use deprecated gdb_index sections."), _("\
23994 When enabled, deprecated .gdb_index sections are used anyway.\n\
23995 Normally they are ignored either because of a missing feature or\n\
23996 performance issue.\n\
23997 Warning: This option must be enabled before gdb reads the file."),
23998 NULL,
23999 NULL,
24000 &setlist, &showlist);
24001
24002 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24003 &dwarf2_locexpr_funcs);
24004 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24005 &dwarf2_loclist_funcs);
24006
24007 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24008 &dwarf2_block_frame_base_locexpr_funcs);
24009 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24010 &dwarf2_block_frame_base_loclist_funcs);
24011
24012 #if GDB_SELF_TEST
24013 selftests::register_test ("dw2_expand_symtabs_matching",
24014 selftests::dw2_expand_symtabs_matching::run_test);
24015 selftests::register_test ("dwarf2_find_containing_comp_unit",
24016 selftests::find_containing_comp_unit::run_test);
24017 #endif
24018 }
This page took 0.624938 seconds and 4 git commands to generate.