[gdb] Expand symbolless symtabs using maint expand-symtabs
[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 (struct dwarf2_per_cu_data *per_cu);
403 ~dwarf2_cu ();
404
405 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
406
407 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
408 Create the set of symtabs used by this TU, or if this TU is sharing
409 symtabs with another TU and the symtabs have already been created
410 then restore those symtabs in the line header.
411 We don't need the pc/line-number mapping for type units. */
412 void setup_type_unit_groups (struct die_info *die);
413
414 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
415 buildsym_compunit constructor. */
416 struct compunit_symtab *start_symtab (const char *name,
417 const char *comp_dir,
418 CORE_ADDR low_pc);
419
420 /* Reset the builder. */
421 void reset_builder () { m_builder.reset (); }
422
423 /* The header of the compilation unit. */
424 struct comp_unit_head header {};
425
426 /* Base address of this compilation unit. */
427 gdb::optional<CORE_ADDR> base_address;
428
429 /* The language we are debugging. */
430 enum language language = language_unknown;
431 const struct language_defn *language_defn = nullptr;
432
433 const char *producer = nullptr;
434
435 private:
436 /* The symtab builder for this CU. This is only non-NULL when full
437 symbols are being read. */
438 std::unique_ptr<buildsym_compunit> m_builder;
439
440 public:
441 /* The generic symbol table building routines have separate lists for
442 file scope symbols and all all other scopes (local scopes). So
443 we need to select the right one to pass to add_symbol_to_list().
444 We do it by keeping a pointer to the correct list in list_in_scope.
445
446 FIXME: The original dwarf code just treated the file scope as the
447 first local scope, and all other local scopes as nested local
448 scopes, and worked fine. Check to see if we really need to
449 distinguish these in buildsym.c. */
450 struct pending **list_in_scope = nullptr;
451
452 /* Hash table holding all the loaded partial DIEs
453 with partial_die->offset.SECT_OFF as hash. */
454 htab_t partial_dies = nullptr;
455
456 /* Storage for things with the same lifetime as this read-in compilation
457 unit, including partial DIEs. */
458 auto_obstack comp_unit_obstack;
459
460 /* When multiple dwarf2_cu structures are living in memory, this field
461 chains them all together, so that they can be released efficiently.
462 We will probably also want a generation counter so that most-recently-used
463 compilation units are cached... */
464 struct dwarf2_per_cu_data *read_in_chain = nullptr;
465
466 /* Backlink to our per_cu entry. */
467 struct dwarf2_per_cu_data *per_cu;
468
469 /* How many compilation units ago was this CU last referenced? */
470 int last_used = 0;
471
472 /* A hash table of DIE cu_offset for following references with
473 die_info->offset.sect_off as hash. */
474 htab_t die_hash = nullptr;
475
476 /* Full DIEs if read in. */
477 struct die_info *dies = nullptr;
478
479 /* A set of pointers to dwarf2_per_cu_data objects for compilation
480 units referenced by this one. Only set during full symbol processing;
481 partial symbol tables do not have dependencies. */
482 htab_t dependencies = nullptr;
483
484 /* Header data from the line table, during full symbol processing. */
485 struct line_header *line_header = nullptr;
486 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
487 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
488 this is the DW_TAG_compile_unit die for this CU. We'll hold on
489 to the line header as long as this DIE is being processed. See
490 process_die_scope. */
491 die_info *line_header_die_owner = nullptr;
492
493 /* A list of methods which need to have physnames computed
494 after all type information has been read. */
495 std::vector<delayed_method_info> method_list;
496
497 /* To be copied to symtab->call_site_htab. */
498 htab_t call_site_htab = nullptr;
499
500 /* Non-NULL if this CU came from a DWO file.
501 There is an invariant here that is important to remember:
502 Except for attributes copied from the top level DIE in the "main"
503 (or "stub") file in preparation for reading the DWO file
504 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
505 Either there isn't a DWO file (in which case this is NULL and the point
506 is moot), or there is and either we're not going to read it (in which
507 case this is NULL) or there is and we are reading it (in which case this
508 is non-NULL). */
509 struct dwo_unit *dwo_unit = nullptr;
510
511 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
512 Note this value comes from the Fission stub CU/TU's DIE. */
513 gdb::optional<ULONGEST> addr_base;
514
515 /* The DW_AT_rnglists_base attribute if present.
516 Note this value comes from the Fission stub CU/TU's DIE.
517 Also note that the value is zero in the non-DWO case so this value can
518 be used without needing to know whether DWO files are in use or not.
519 N.B. This does not apply to DW_AT_ranges appearing in
520 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
521 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
522 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
523 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
524 ULONGEST ranges_base = 0;
525
526 /* The DW_AT_loclists_base attribute if present. */
527 ULONGEST loclist_base = 0;
528
529 /* When reading debug info generated by older versions of rustc, we
530 have to rewrite some union types to be struct types with a
531 variant part. This rewriting must be done after the CU is fully
532 read in, because otherwise at the point of rewriting some struct
533 type might not have been fully processed. So, we keep a list of
534 all such types here and process them after expansion. */
535 std::vector<struct type *> rust_unions;
536
537 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
538 files, the value is implicitly zero. For DWARF 5 version DWO files, the
539 value is often implicit and is the size of the header of
540 .debug_str_offsets section (8 or 4, depending on the address size). */
541 gdb::optional<ULONGEST> str_offsets_base;
542
543 /* Mark used when releasing cached dies. */
544 bool mark : 1;
545
546 /* This CU references .debug_loc. See the symtab->locations_valid field.
547 This test is imperfect as there may exist optimized debug code not using
548 any location list and still facing inlining issues if handled as
549 unoptimized code. For a future better test see GCC PR other/32998. */
550 bool has_loclist : 1;
551
552 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
553 if all the producer_is_* fields are valid. This information is cached
554 because profiling CU expansion showed excessive time spent in
555 producer_is_gxx_lt_4_6. */
556 bool checked_producer : 1;
557 bool producer_is_gxx_lt_4_6 : 1;
558 bool producer_is_gcc_lt_4_3 : 1;
559 bool producer_is_icc : 1;
560 bool producer_is_icc_lt_14 : 1;
561 bool producer_is_codewarrior : 1;
562
563 /* When true, the file that we're processing is known to have
564 debugging info for C++ namespaces. GCC 3.3.x did not produce
565 this information, but later versions do. */
566
567 bool processing_has_namespace_info : 1;
568
569 struct partial_die_info *find_partial_die (sect_offset sect_off);
570
571 /* If this CU was inherited by another CU (via specification,
572 abstract_origin, etc), this is the ancestor CU. */
573 dwarf2_cu *ancestor;
574
575 /* Get the buildsym_compunit for this CU. */
576 buildsym_compunit *get_builder ()
577 {
578 /* If this CU has a builder associated with it, use that. */
579 if (m_builder != nullptr)
580 return m_builder.get ();
581
582 /* Otherwise, search ancestors for a valid builder. */
583 if (ancestor != nullptr)
584 return ancestor->get_builder ();
585
586 return nullptr;
587 }
588 };
589
590 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
591 This includes type_unit_group and quick_file_names. */
592
593 struct stmt_list_hash
594 {
595 /* The DWO unit this table is from or NULL if there is none. */
596 struct dwo_unit *dwo_unit;
597
598 /* Offset in .debug_line or .debug_line.dwo. */
599 sect_offset line_sect_off;
600 };
601
602 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
603 an object of this type. */
604
605 struct type_unit_group
606 {
607 /* dwarf2read.c's main "handle" on a TU symtab.
608 To simplify things we create an artificial CU that "includes" all the
609 type units using this stmt_list so that the rest of the code still has
610 a "per_cu" handle on the symtab. */
611 struct dwarf2_per_cu_data per_cu;
612
613 /* The TUs that share this DW_AT_stmt_list entry.
614 This is added to while parsing type units to build partial symtabs,
615 and is deleted afterwards and not used again. */
616 std::vector<signatured_type *> *tus;
617
618 /* The compunit symtab.
619 Type units in a group needn't all be defined in the same source file,
620 so we create an essentially anonymous symtab as the compunit symtab. */
621 struct compunit_symtab *compunit_symtab;
622
623 /* The data used to construct the hash key. */
624 struct stmt_list_hash hash;
625
626 /* The symbol tables for this TU (obtained from the files listed in
627 DW_AT_stmt_list).
628 WARNING: The order of entries here must match the order of entries
629 in the line header. After the first TU using this type_unit_group, the
630 line header for the subsequent TUs is recreated from this. This is done
631 because we need to use the same symtabs for each TU using the same
632 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
633 there's no guarantee the line header doesn't have duplicate entries. */
634 struct symtab **symtabs;
635 };
636
637 /* These sections are what may appear in a (real or virtual) DWO file. */
638
639 struct dwo_sections
640 {
641 struct dwarf2_section_info abbrev;
642 struct dwarf2_section_info line;
643 struct dwarf2_section_info loc;
644 struct dwarf2_section_info loclists;
645 struct dwarf2_section_info macinfo;
646 struct dwarf2_section_info macro;
647 struct dwarf2_section_info str;
648 struct dwarf2_section_info str_offsets;
649 /* In the case of a virtual DWO file, these two are unused. */
650 struct dwarf2_section_info info;
651 std::vector<dwarf2_section_info> types;
652 };
653
654 /* CUs/TUs in DWP/DWO files. */
655
656 struct dwo_unit
657 {
658 /* Backlink to the containing struct dwo_file. */
659 struct dwo_file *dwo_file;
660
661 /* The "id" that distinguishes this CU/TU.
662 .debug_info calls this "dwo_id", .debug_types calls this "signature".
663 Since signatures came first, we stick with it for consistency. */
664 ULONGEST signature;
665
666 /* The section this CU/TU lives in, in the DWO file. */
667 struct dwarf2_section_info *section;
668
669 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
670 sect_offset sect_off;
671 unsigned int length;
672
673 /* For types, offset in the type's DIE of the type defined by this TU. */
674 cu_offset type_offset_in_tu;
675 };
676
677 /* include/dwarf2.h defines the DWP section codes.
678 It defines a max value but it doesn't define a min value, which we
679 use for error checking, so provide one. */
680
681 enum dwp_v2_section_ids
682 {
683 DW_SECT_MIN = 1
684 };
685
686 /* Data for one DWO file.
687
688 This includes virtual DWO files (a virtual DWO file is a DWO file as it
689 appears in a DWP file). DWP files don't really have DWO files per se -
690 comdat folding of types "loses" the DWO file they came from, and from
691 a high level view DWP files appear to contain a mass of random types.
692 However, to maintain consistency with the non-DWP case we pretend DWP
693 files contain virtual DWO files, and we assign each TU with one virtual
694 DWO file (generally based on the line and abbrev section offsets -
695 a heuristic that seems to work in practice). */
696
697 struct dwo_file
698 {
699 dwo_file () = default;
700 DISABLE_COPY_AND_ASSIGN (dwo_file);
701
702 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
703 For virtual DWO files the name is constructed from the section offsets
704 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
705 from related CU+TUs. */
706 const char *dwo_name = nullptr;
707
708 /* The DW_AT_comp_dir attribute. */
709 const char *comp_dir = nullptr;
710
711 /* The bfd, when the file is open. Otherwise this is NULL.
712 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
713 gdb_bfd_ref_ptr dbfd;
714
715 /* The sections that make up this DWO file.
716 Remember that for virtual DWO files in DWP V2, these are virtual
717 sections (for lack of a better name). */
718 struct dwo_sections sections {};
719
720 /* The CUs in the file.
721 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
722 an extension to handle LLVM's Link Time Optimization output (where
723 multiple source files may be compiled into a single object/dwo pair). */
724 htab_up cus;
725
726 /* Table of TUs in the file.
727 Each element is a struct dwo_unit. */
728 htab_up tus;
729 };
730
731 /* These sections are what may appear in a DWP file. */
732
733 struct dwp_sections
734 {
735 /* These are used by both DWP version 1 and 2. */
736 struct dwarf2_section_info str;
737 struct dwarf2_section_info cu_index;
738 struct dwarf2_section_info tu_index;
739
740 /* These are only used by DWP version 2 files.
741 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
742 sections are referenced by section number, and are not recorded here.
743 In DWP version 2 there is at most one copy of all these sections, each
744 section being (effectively) comprised of the concatenation of all of the
745 individual sections that exist in the version 1 format.
746 To keep the code simple we treat each of these concatenated pieces as a
747 section itself (a virtual section?). */
748 struct dwarf2_section_info abbrev;
749 struct dwarf2_section_info info;
750 struct dwarf2_section_info line;
751 struct dwarf2_section_info loc;
752 struct dwarf2_section_info macinfo;
753 struct dwarf2_section_info macro;
754 struct dwarf2_section_info str_offsets;
755 struct dwarf2_section_info types;
756 };
757
758 /* These sections are what may appear in a virtual DWO file in DWP version 1.
759 A virtual DWO file is a DWO file as it appears in a DWP file. */
760
761 struct virtual_v1_dwo_sections
762 {
763 struct dwarf2_section_info abbrev;
764 struct dwarf2_section_info line;
765 struct dwarf2_section_info loc;
766 struct dwarf2_section_info macinfo;
767 struct dwarf2_section_info macro;
768 struct dwarf2_section_info str_offsets;
769 /* Each DWP hash table entry records one CU or one TU.
770 That is recorded here, and copied to dwo_unit.section. */
771 struct dwarf2_section_info info_or_types;
772 };
773
774 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
775 In version 2, the sections of the DWO files are concatenated together
776 and stored in one section of that name. Thus each ELF section contains
777 several "virtual" sections. */
778
779 struct virtual_v2_dwo_sections
780 {
781 bfd_size_type abbrev_offset;
782 bfd_size_type abbrev_size;
783
784 bfd_size_type line_offset;
785 bfd_size_type line_size;
786
787 bfd_size_type loc_offset;
788 bfd_size_type loc_size;
789
790 bfd_size_type macinfo_offset;
791 bfd_size_type macinfo_size;
792
793 bfd_size_type macro_offset;
794 bfd_size_type macro_size;
795
796 bfd_size_type str_offsets_offset;
797 bfd_size_type str_offsets_size;
798
799 /* Each DWP hash table entry records one CU or one TU.
800 That is recorded here, and copied to dwo_unit.section. */
801 bfd_size_type info_or_types_offset;
802 bfd_size_type info_or_types_size;
803 };
804
805 /* Contents of DWP hash tables. */
806
807 struct dwp_hash_table
808 {
809 uint32_t version, nr_columns;
810 uint32_t nr_units, nr_slots;
811 const gdb_byte *hash_table, *unit_table;
812 union
813 {
814 struct
815 {
816 const gdb_byte *indices;
817 } v1;
818 struct
819 {
820 /* This is indexed by column number and gives the id of the section
821 in that column. */
822 #define MAX_NR_V2_DWO_SECTIONS \
823 (1 /* .debug_info or .debug_types */ \
824 + 1 /* .debug_abbrev */ \
825 + 1 /* .debug_line */ \
826 + 1 /* .debug_loc */ \
827 + 1 /* .debug_str_offsets */ \
828 + 1 /* .debug_macro or .debug_macinfo */)
829 int section_ids[MAX_NR_V2_DWO_SECTIONS];
830 const gdb_byte *offsets;
831 const gdb_byte *sizes;
832 } v2;
833 } section_pool;
834 };
835
836 /* Data for one DWP file. */
837
838 struct dwp_file
839 {
840 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
841 : name (name_),
842 dbfd (std::move (abfd))
843 {
844 }
845
846 /* Name of the file. */
847 const char *name;
848
849 /* File format version. */
850 int version = 0;
851
852 /* The bfd. */
853 gdb_bfd_ref_ptr dbfd;
854
855 /* Section info for this file. */
856 struct dwp_sections sections {};
857
858 /* Table of CUs in the file. */
859 const struct dwp_hash_table *cus = nullptr;
860
861 /* Table of TUs in the file. */
862 const struct dwp_hash_table *tus = nullptr;
863
864 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
865 htab_up loaded_cus;
866 htab_up loaded_tus;
867
868 /* Table to map ELF section numbers to their sections.
869 This is only needed for the DWP V1 file format. */
870 unsigned int num_sections = 0;
871 asection **elf_sections = nullptr;
872 };
873
874 /* Struct used to pass misc. parameters to read_die_and_children, et
875 al. which are used for both .debug_info and .debug_types dies.
876 All parameters here are unchanging for the life of the call. This
877 struct exists to abstract away the constant parameters of die reading. */
878
879 struct die_reader_specs
880 {
881 /* The bfd of die_section. */
882 bfd* abfd;
883
884 /* The CU of the DIE we are parsing. */
885 struct dwarf2_cu *cu;
886
887 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
888 struct dwo_file *dwo_file;
889
890 /* The section the die comes from.
891 This is either .debug_info or .debug_types, or the .dwo variants. */
892 struct dwarf2_section_info *die_section;
893
894 /* die_section->buffer. */
895 const gdb_byte *buffer;
896
897 /* The end of the buffer. */
898 const gdb_byte *buffer_end;
899
900 /* The abbreviation table to use when reading the DIEs. */
901 struct abbrev_table *abbrev_table;
902 };
903
904 /* A subclass of die_reader_specs that holds storage and has complex
905 constructor and destructor behavior. */
906
907 class cutu_reader : public die_reader_specs
908 {
909 public:
910
911 cutu_reader (struct dwarf2_per_cu_data *this_cu,
912 struct abbrev_table *abbrev_table,
913 int use_existing_cu,
914 bool skip_partial);
915
916 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
917 struct dwarf2_cu *parent_cu = nullptr,
918 struct dwo_file *dwo_file = nullptr);
919
920 DISABLE_COPY_AND_ASSIGN (cutu_reader);
921
922 const gdb_byte *info_ptr = nullptr;
923 struct die_info *comp_unit_die = nullptr;
924 bool dummy_p = false;
925
926 /* Release the new CU, putting it on the chain. This cannot be done
927 for dummy CUs. */
928 void keep ();
929
930 private:
931 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
932 int use_existing_cu);
933
934 struct dwarf2_per_cu_data *m_this_cu;
935 std::unique_ptr<dwarf2_cu> m_new_cu;
936
937 /* The ordinary abbreviation table. */
938 abbrev_table_up m_abbrev_table_holder;
939
940 /* The DWO abbreviation table. */
941 abbrev_table_up m_dwo_abbrev_table;
942 };
943
944 /* When we construct a partial symbol table entry we only
945 need this much information. */
946 struct partial_die_info : public allocate_on_obstack
947 {
948 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
949
950 /* Disable assign but still keep copy ctor, which is needed
951 load_partial_dies. */
952 partial_die_info& operator=(const partial_die_info& rhs) = delete;
953
954 /* Adjust the partial die before generating a symbol for it. This
955 function may set the is_external flag or change the DIE's
956 name. */
957 void fixup (struct dwarf2_cu *cu);
958
959 /* Read a minimal amount of information into the minimal die
960 structure. */
961 const gdb_byte *read (const struct die_reader_specs *reader,
962 const struct abbrev_info &abbrev,
963 const gdb_byte *info_ptr);
964
965 /* Offset of this DIE. */
966 const sect_offset sect_off;
967
968 /* DWARF-2 tag for this DIE. */
969 const ENUM_BITFIELD(dwarf_tag) tag : 16;
970
971 /* Assorted flags describing the data found in this DIE. */
972 const unsigned int has_children : 1;
973
974 unsigned int is_external : 1;
975 unsigned int is_declaration : 1;
976 unsigned int has_type : 1;
977 unsigned int has_specification : 1;
978 unsigned int has_pc_info : 1;
979 unsigned int may_be_inlined : 1;
980
981 /* This DIE has been marked DW_AT_main_subprogram. */
982 unsigned int main_subprogram : 1;
983
984 /* Flag set if the SCOPE field of this structure has been
985 computed. */
986 unsigned int scope_set : 1;
987
988 /* Flag set if the DIE has a byte_size attribute. */
989 unsigned int has_byte_size : 1;
990
991 /* Flag set if the DIE has a DW_AT_const_value attribute. */
992 unsigned int has_const_value : 1;
993
994 /* Flag set if any of the DIE's children are template arguments. */
995 unsigned int has_template_arguments : 1;
996
997 /* Flag set if fixup has been called on this die. */
998 unsigned int fixup_called : 1;
999
1000 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1001 unsigned int is_dwz : 1;
1002
1003 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1004 unsigned int spec_is_dwz : 1;
1005
1006 /* The name of this DIE. Normally the value of DW_AT_name, but
1007 sometimes a default name for unnamed DIEs. */
1008 const char *name = nullptr;
1009
1010 /* The linkage name, if present. */
1011 const char *linkage_name = nullptr;
1012
1013 /* The scope to prepend to our children. This is generally
1014 allocated on the comp_unit_obstack, so will disappear
1015 when this compilation unit leaves the cache. */
1016 const char *scope = nullptr;
1017
1018 /* Some data associated with the partial DIE. The tag determines
1019 which field is live. */
1020 union
1021 {
1022 /* The location description associated with this DIE, if any. */
1023 struct dwarf_block *locdesc;
1024 /* The offset of an import, for DW_TAG_imported_unit. */
1025 sect_offset sect_off;
1026 } d {};
1027
1028 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1029 CORE_ADDR lowpc = 0;
1030 CORE_ADDR highpc = 0;
1031
1032 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1033 DW_AT_sibling, if any. */
1034 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1035 could return DW_AT_sibling values to its caller load_partial_dies. */
1036 const gdb_byte *sibling = nullptr;
1037
1038 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1039 DW_AT_specification (or DW_AT_abstract_origin or
1040 DW_AT_extension). */
1041 sect_offset spec_offset {};
1042
1043 /* Pointers to this DIE's parent, first child, and next sibling,
1044 if any. */
1045 struct partial_die_info *die_parent = nullptr;
1046 struct partial_die_info *die_child = nullptr;
1047 struct partial_die_info *die_sibling = nullptr;
1048
1049 friend struct partial_die_info *
1050 dwarf2_cu::find_partial_die (sect_offset sect_off);
1051
1052 private:
1053 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1054 partial_die_info (sect_offset sect_off)
1055 : partial_die_info (sect_off, DW_TAG_padding, 0)
1056 {
1057 }
1058
1059 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1060 int has_children_)
1061 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1062 {
1063 is_external = 0;
1064 is_declaration = 0;
1065 has_type = 0;
1066 has_specification = 0;
1067 has_pc_info = 0;
1068 may_be_inlined = 0;
1069 main_subprogram = 0;
1070 scope_set = 0;
1071 has_byte_size = 0;
1072 has_const_value = 0;
1073 has_template_arguments = 0;
1074 fixup_called = 0;
1075 is_dwz = 0;
1076 spec_is_dwz = 0;
1077 }
1078 };
1079
1080 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1081 but this would require a corresponding change in unpack_field_as_long
1082 and friends. */
1083 static int bits_per_byte = 8;
1084
1085 /* When reading a variant or variant part, we track a bit more
1086 information about the field, and store it in an object of this
1087 type. */
1088
1089 struct variant_field
1090 {
1091 /* If we see a DW_TAG_variant, then this will be the discriminant
1092 value. */
1093 ULONGEST discriminant_value;
1094 /* If we see a DW_TAG_variant, then this will be set if this is the
1095 default branch. */
1096 bool default_branch;
1097 /* While reading a DW_TAG_variant_part, this will be set if this
1098 field is the discriminant. */
1099 bool is_discriminant;
1100 };
1101
1102 struct nextfield
1103 {
1104 int accessibility = 0;
1105 int virtuality = 0;
1106 /* Extra information to describe a variant or variant part. */
1107 struct variant_field variant {};
1108 struct field field {};
1109 };
1110
1111 struct fnfieldlist
1112 {
1113 const char *name = nullptr;
1114 std::vector<struct fn_field> fnfields;
1115 };
1116
1117 /* The routines that read and process dies for a C struct or C++ class
1118 pass lists of data member fields and lists of member function fields
1119 in an instance of a field_info structure, as defined below. */
1120 struct field_info
1121 {
1122 /* List of data member and baseclasses fields. */
1123 std::vector<struct nextfield> fields;
1124 std::vector<struct nextfield> baseclasses;
1125
1126 /* Set if the accessibility of one of the fields is not public. */
1127 int non_public_fields = 0;
1128
1129 /* Member function fieldlist array, contains name of possibly overloaded
1130 member function, number of overloaded member functions and a pointer
1131 to the head of the member function field chain. */
1132 std::vector<struct fnfieldlist> fnfieldlists;
1133
1134 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1135 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1136 std::vector<struct decl_field> typedef_field_list;
1137
1138 /* Nested types defined by this class and the number of elements in this
1139 list. */
1140 std::vector<struct decl_field> nested_types_list;
1141
1142 /* Return the total number of fields (including baseclasses). */
1143 int nfields () const
1144 {
1145 return fields.size () + baseclasses.size ();
1146 }
1147 };
1148
1149 /* Loaded secondary compilation units are kept in memory until they
1150 have not been referenced for the processing of this many
1151 compilation units. Set this to zero to disable caching. Cache
1152 sizes of up to at least twenty will improve startup time for
1153 typical inter-CU-reference binaries, at an obvious memory cost. */
1154 static int dwarf_max_cache_age = 5;
1155 static void
1156 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1157 struct cmd_list_element *c, const char *value)
1158 {
1159 fprintf_filtered (file, _("The upper bound on the age of cached "
1160 "DWARF compilation units is %s.\n"),
1161 value);
1162 }
1163 \f
1164 /* local function prototypes */
1165
1166 static void dwarf2_find_base_address (struct die_info *die,
1167 struct dwarf2_cu *cu);
1168
1169 static dwarf2_psymtab *create_partial_symtab
1170 (struct dwarf2_per_cu_data *per_cu, const char *name);
1171
1172 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1173 const gdb_byte *info_ptr,
1174 struct die_info *type_unit_die);
1175
1176 static void dwarf2_build_psymtabs_hard
1177 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1178
1179 static void scan_partial_symbols (struct partial_die_info *,
1180 CORE_ADDR *, CORE_ADDR *,
1181 int, struct dwarf2_cu *);
1182
1183 static void add_partial_symbol (struct partial_die_info *,
1184 struct dwarf2_cu *);
1185
1186 static void add_partial_namespace (struct partial_die_info *pdi,
1187 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1188 int set_addrmap, struct dwarf2_cu *cu);
1189
1190 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1191 CORE_ADDR *highpc, int set_addrmap,
1192 struct dwarf2_cu *cu);
1193
1194 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1195 struct dwarf2_cu *cu);
1196
1197 static void add_partial_subprogram (struct partial_die_info *pdi,
1198 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1199 int need_pc, struct dwarf2_cu *cu);
1200
1201 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1202
1203 static struct partial_die_info *load_partial_dies
1204 (const struct die_reader_specs *, const gdb_byte *, int);
1205
1206 /* A pair of partial_die_info and compilation unit. */
1207 struct cu_partial_die_info
1208 {
1209 /* The compilation unit of the partial_die_info. */
1210 struct dwarf2_cu *cu;
1211 /* A partial_die_info. */
1212 struct partial_die_info *pdi;
1213
1214 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1215 : cu (cu),
1216 pdi (pdi)
1217 { /* Nothing. */ }
1218
1219 private:
1220 cu_partial_die_info () = delete;
1221 };
1222
1223 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1224 struct dwarf2_cu *);
1225
1226 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1227 struct attribute *, struct attr_abbrev *,
1228 const gdb_byte *, bool *need_reprocess);
1229
1230 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1231 struct attribute *attr);
1232
1233 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1234
1235 static sect_offset read_abbrev_offset
1236 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1237 struct dwarf2_section_info *, sect_offset);
1238
1239 static const char *read_indirect_string
1240 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1241 const struct comp_unit_head *, unsigned int *);
1242
1243 static const char *read_indirect_string_at_offset
1244 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1245
1246 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1247 const gdb_byte *,
1248 unsigned int *);
1249
1250 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1251 ULONGEST str_index);
1252
1253 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1254 ULONGEST str_index);
1255
1256 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1257
1258 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1259 struct dwarf2_cu *);
1260
1261 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1262 struct dwarf2_cu *cu);
1263
1264 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1265
1266 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1267 struct dwarf2_cu *cu);
1268
1269 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1270
1271 static struct die_info *die_specification (struct die_info *die,
1272 struct dwarf2_cu **);
1273
1274 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1275 struct dwarf2_cu *cu);
1276
1277 static void dwarf_decode_lines (struct line_header *, const char *,
1278 struct dwarf2_cu *, dwarf2_psymtab *,
1279 CORE_ADDR, int decode_mapping);
1280
1281 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1282 const char *);
1283
1284 static struct symbol *new_symbol (struct die_info *, struct type *,
1285 struct dwarf2_cu *, struct symbol * = NULL);
1286
1287 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1288 struct dwarf2_cu *);
1289
1290 static void dwarf2_const_value_attr (const struct attribute *attr,
1291 struct type *type,
1292 const char *name,
1293 struct obstack *obstack,
1294 struct dwarf2_cu *cu, LONGEST *value,
1295 const gdb_byte **bytes,
1296 struct dwarf2_locexpr_baton **baton);
1297
1298 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1299
1300 static int need_gnat_info (struct dwarf2_cu *);
1301
1302 static struct type *die_descriptive_type (struct die_info *,
1303 struct dwarf2_cu *);
1304
1305 static void set_descriptive_type (struct type *, struct die_info *,
1306 struct dwarf2_cu *);
1307
1308 static struct type *die_containing_type (struct die_info *,
1309 struct dwarf2_cu *);
1310
1311 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1312 struct dwarf2_cu *);
1313
1314 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1315
1316 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1317
1318 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1319
1320 static char *typename_concat (struct obstack *obs, const char *prefix,
1321 const char *suffix, int physname,
1322 struct dwarf2_cu *cu);
1323
1324 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1325
1326 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1327
1328 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1329
1330 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1331
1332 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1333
1334 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1335
1336 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1337 struct dwarf2_cu *, dwarf2_psymtab *);
1338
1339 /* Return the .debug_loclists section to use for cu. */
1340 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1341
1342 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1343 values. Keep the items ordered with increasing constraints compliance. */
1344 enum pc_bounds_kind
1345 {
1346 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1347 PC_BOUNDS_NOT_PRESENT,
1348
1349 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1350 were present but they do not form a valid range of PC addresses. */
1351 PC_BOUNDS_INVALID,
1352
1353 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1354 PC_BOUNDS_RANGES,
1355
1356 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1357 PC_BOUNDS_HIGH_LOW,
1358 };
1359
1360 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1361 CORE_ADDR *, CORE_ADDR *,
1362 struct dwarf2_cu *,
1363 dwarf2_psymtab *);
1364
1365 static void get_scope_pc_bounds (struct die_info *,
1366 CORE_ADDR *, CORE_ADDR *,
1367 struct dwarf2_cu *);
1368
1369 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1370 CORE_ADDR, struct dwarf2_cu *);
1371
1372 static void dwarf2_add_field (struct field_info *, struct die_info *,
1373 struct dwarf2_cu *);
1374
1375 static void dwarf2_attach_fields_to_type (struct field_info *,
1376 struct type *, struct dwarf2_cu *);
1377
1378 static void dwarf2_add_member_fn (struct field_info *,
1379 struct die_info *, struct type *,
1380 struct dwarf2_cu *);
1381
1382 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1383 struct type *,
1384 struct dwarf2_cu *);
1385
1386 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1387
1388 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1389
1390 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1391
1392 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1393
1394 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1395
1396 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1397
1398 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1399
1400 static struct type *read_module_type (struct die_info *die,
1401 struct dwarf2_cu *cu);
1402
1403 static const char *namespace_name (struct die_info *die,
1404 int *is_anonymous, struct dwarf2_cu *);
1405
1406 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1407
1408 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1409
1410 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1411 struct dwarf2_cu *);
1412
1413 static struct die_info *read_die_and_siblings_1
1414 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1415 struct die_info *);
1416
1417 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1418 const gdb_byte *info_ptr,
1419 const gdb_byte **new_info_ptr,
1420 struct die_info *parent);
1421
1422 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1423 struct die_info **, const gdb_byte *,
1424 int);
1425
1426 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1427 struct die_info **, const gdb_byte *);
1428
1429 static void process_die (struct die_info *, struct dwarf2_cu *);
1430
1431 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1432 struct objfile *);
1433
1434 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1435
1436 static const char *dwarf2_full_name (const char *name,
1437 struct die_info *die,
1438 struct dwarf2_cu *cu);
1439
1440 static const char *dwarf2_physname (const char *name, struct die_info *die,
1441 struct dwarf2_cu *cu);
1442
1443 static struct die_info *dwarf2_extension (struct die_info *die,
1444 struct dwarf2_cu **);
1445
1446 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1447
1448 static void dump_die_for_error (struct die_info *);
1449
1450 static void dump_die_1 (struct ui_file *, int level, int max_level,
1451 struct die_info *);
1452
1453 /*static*/ void dump_die (struct die_info *, int max_level);
1454
1455 static void store_in_ref_table (struct die_info *,
1456 struct dwarf2_cu *);
1457
1458 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1459 const struct attribute *,
1460 struct dwarf2_cu **);
1461
1462 static struct die_info *follow_die_ref (struct die_info *,
1463 const struct attribute *,
1464 struct dwarf2_cu **);
1465
1466 static struct die_info *follow_die_sig (struct die_info *,
1467 const struct attribute *,
1468 struct dwarf2_cu **);
1469
1470 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1471 struct dwarf2_cu *);
1472
1473 static struct type *get_DW_AT_signature_type (struct die_info *,
1474 const struct attribute *,
1475 struct dwarf2_cu *);
1476
1477 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1478
1479 static void read_signatured_type (struct signatured_type *);
1480
1481 static int attr_to_dynamic_prop (const struct attribute *attr,
1482 struct die_info *die, struct dwarf2_cu *cu,
1483 struct dynamic_prop *prop, struct type *type);
1484
1485 /* memory allocation interface */
1486
1487 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1488
1489 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1490
1491 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1492
1493 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1494 struct dwarf2_loclist_baton *baton,
1495 const struct attribute *attr);
1496
1497 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1498 struct symbol *sym,
1499 struct dwarf2_cu *cu,
1500 int is_block);
1501
1502 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1503 const gdb_byte *info_ptr,
1504 struct abbrev_info *abbrev);
1505
1506 static hashval_t partial_die_hash (const void *item);
1507
1508 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1509
1510 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1511 (sect_offset sect_off, unsigned int offset_in_dwz,
1512 struct dwarf2_per_objfile *dwarf2_per_objfile);
1513
1514 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1515 struct die_info *comp_unit_die,
1516 enum language pretend_language);
1517
1518 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1519
1520 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1521
1522 static struct type *set_die_type (struct die_info *, struct type *,
1523 struct dwarf2_cu *);
1524
1525 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1526
1527 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1528
1529 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1530 enum language);
1531
1532 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1533 enum language);
1534
1535 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1536 enum language);
1537
1538 static void dwarf2_add_dependence (struct dwarf2_cu *,
1539 struct dwarf2_per_cu_data *);
1540
1541 static void dwarf2_mark (struct dwarf2_cu *);
1542
1543 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1544
1545 static struct type *get_die_type_at_offset (sect_offset,
1546 struct dwarf2_per_cu_data *);
1547
1548 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1549
1550 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1551 enum language pretend_language);
1552
1553 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1554
1555 /* Class, the destructor of which frees all allocated queue entries. This
1556 will only have work to do if an error was thrown while processing the
1557 dwarf. If no error was thrown then the queue entries should have all
1558 been processed, and freed, as we went along. */
1559
1560 class dwarf2_queue_guard
1561 {
1562 public:
1563 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1564 : m_per_objfile (per_objfile)
1565 {
1566 }
1567
1568 /* Free any entries remaining on the queue. There should only be
1569 entries left if we hit an error while processing the dwarf. */
1570 ~dwarf2_queue_guard ()
1571 {
1572 /* Ensure that no memory is allocated by the queue. */
1573 std::queue<dwarf2_queue_item> empty;
1574 std::swap (m_per_objfile->queue, empty);
1575 }
1576
1577 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1578
1579 private:
1580 dwarf2_per_objfile *m_per_objfile;
1581 };
1582
1583 dwarf2_queue_item::~dwarf2_queue_item ()
1584 {
1585 /* Anything still marked queued is likely to be in an
1586 inconsistent state, so discard it. */
1587 if (per_cu->queued)
1588 {
1589 if (per_cu->cu != NULL)
1590 free_one_cached_comp_unit (per_cu);
1591 per_cu->queued = 0;
1592 }
1593 }
1594
1595 /* The return type of find_file_and_directory. Note, the enclosed
1596 string pointers are only valid while this object is valid. */
1597
1598 struct file_and_directory
1599 {
1600 /* The filename. This is never NULL. */
1601 const char *name;
1602
1603 /* The compilation directory. NULL if not known. If we needed to
1604 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1605 points directly to the DW_AT_comp_dir string attribute owned by
1606 the obstack that owns the DIE. */
1607 const char *comp_dir;
1608
1609 /* If we needed to build a new string for comp_dir, this is what
1610 owns the storage. */
1611 std::string comp_dir_storage;
1612 };
1613
1614 static file_and_directory find_file_and_directory (struct die_info *die,
1615 struct dwarf2_cu *cu);
1616
1617 static htab_up allocate_signatured_type_table ();
1618
1619 static htab_up allocate_dwo_unit_table ();
1620
1621 static struct dwo_unit *lookup_dwo_unit_in_dwp
1622 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1623 struct dwp_file *dwp_file, const char *comp_dir,
1624 ULONGEST signature, int is_debug_types);
1625
1626 static struct dwp_file *get_dwp_file
1627 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1628
1629 static struct dwo_unit *lookup_dwo_comp_unit
1630 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1631
1632 static struct dwo_unit *lookup_dwo_type_unit
1633 (struct signatured_type *, const char *, const char *);
1634
1635 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1636
1637 /* A unique pointer to a dwo_file. */
1638
1639 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1640
1641 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1642
1643 static void check_producer (struct dwarf2_cu *cu);
1644
1645 static void free_line_header_voidp (void *arg);
1646 \f
1647 /* Various complaints about symbol reading that don't abort the process. */
1648
1649 static void
1650 dwarf2_debug_line_missing_file_complaint (void)
1651 {
1652 complaint (_(".debug_line section has line data without a file"));
1653 }
1654
1655 static void
1656 dwarf2_debug_line_missing_end_sequence_complaint (void)
1657 {
1658 complaint (_(".debug_line section has line "
1659 "program sequence without an end"));
1660 }
1661
1662 static void
1663 dwarf2_complex_location_expr_complaint (void)
1664 {
1665 complaint (_("location expression too complex"));
1666 }
1667
1668 static void
1669 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1670 int arg3)
1671 {
1672 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1673 arg1, arg2, arg3);
1674 }
1675
1676 static void
1677 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1678 {
1679 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1680 arg1, arg2);
1681 }
1682
1683 /* Hash function for line_header_hash. */
1684
1685 static hashval_t
1686 line_header_hash (const struct line_header *ofs)
1687 {
1688 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1689 }
1690
1691 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1692
1693 static hashval_t
1694 line_header_hash_voidp (const void *item)
1695 {
1696 const struct line_header *ofs = (const struct line_header *) item;
1697
1698 return line_header_hash (ofs);
1699 }
1700
1701 /* Equality function for line_header_hash. */
1702
1703 static int
1704 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1705 {
1706 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1707 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1708
1709 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1710 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1711 }
1712
1713 \f
1714
1715 /* See declaration. */
1716
1717 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
1718 const dwarf2_debug_sections *names,
1719 bool can_copy_)
1720 : objfile (objfile_),
1721 can_copy (can_copy_)
1722 {
1723 if (names == NULL)
1724 names = &dwarf2_elf_names;
1725
1726 bfd *obfd = objfile->obfd;
1727
1728 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1729 locate_sections (obfd, sec, *names);
1730 }
1731
1732 dwarf2_per_objfile::~dwarf2_per_objfile ()
1733 {
1734 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1735 free_cached_comp_units ();
1736
1737 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1738 per_cu->imported_symtabs_free ();
1739
1740 for (signatured_type *sig_type : all_type_units)
1741 sig_type->per_cu.imported_symtabs_free ();
1742
1743 /* Everything else should be on the objfile obstack. */
1744 }
1745
1746 /* See declaration. */
1747
1748 void
1749 dwarf2_per_objfile::free_cached_comp_units ()
1750 {
1751 dwarf2_per_cu_data *per_cu = read_in_chain;
1752 dwarf2_per_cu_data **last_chain = &read_in_chain;
1753 while (per_cu != NULL)
1754 {
1755 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1756
1757 delete per_cu->cu;
1758 *last_chain = next_cu;
1759 per_cu = next_cu;
1760 }
1761 }
1762
1763 /* A helper class that calls free_cached_comp_units on
1764 destruction. */
1765
1766 class free_cached_comp_units
1767 {
1768 public:
1769
1770 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1771 : m_per_objfile (per_objfile)
1772 {
1773 }
1774
1775 ~free_cached_comp_units ()
1776 {
1777 m_per_objfile->free_cached_comp_units ();
1778 }
1779
1780 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1781
1782 private:
1783
1784 dwarf2_per_objfile *m_per_objfile;
1785 };
1786
1787 /* Try to locate the sections we need for DWARF 2 debugging
1788 information and return true if we have enough to do something.
1789 NAMES points to the dwarf2 section names, or is NULL if the standard
1790 ELF names are used. CAN_COPY is true for formats where symbol
1791 interposition is possible and so symbol values must follow copy
1792 relocation rules. */
1793
1794 int
1795 dwarf2_has_info (struct objfile *objfile,
1796 const struct dwarf2_debug_sections *names,
1797 bool can_copy)
1798 {
1799 if (objfile->flags & OBJF_READNEVER)
1800 return 0;
1801
1802 struct dwarf2_per_objfile *dwarf2_per_objfile
1803 = get_dwarf2_per_objfile (objfile);
1804
1805 if (dwarf2_per_objfile == NULL)
1806 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
1807 names,
1808 can_copy);
1809
1810 return (!dwarf2_per_objfile->info.is_virtual
1811 && dwarf2_per_objfile->info.s.section != NULL
1812 && !dwarf2_per_objfile->abbrev.is_virtual
1813 && dwarf2_per_objfile->abbrev.s.section != NULL);
1814 }
1815
1816 /* When loading sections, we look either for uncompressed section or for
1817 compressed section names. */
1818
1819 static int
1820 section_is_p (const char *section_name,
1821 const struct dwarf2_section_names *names)
1822 {
1823 if (names->normal != NULL
1824 && strcmp (section_name, names->normal) == 0)
1825 return 1;
1826 if (names->compressed != NULL
1827 && strcmp (section_name, names->compressed) == 0)
1828 return 1;
1829 return 0;
1830 }
1831
1832 /* See declaration. */
1833
1834 void
1835 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1836 const dwarf2_debug_sections &names)
1837 {
1838 flagword aflag = bfd_section_flags (sectp);
1839
1840 if ((aflag & SEC_HAS_CONTENTS) == 0)
1841 {
1842 }
1843 else if (elf_section_data (sectp)->this_hdr.sh_size
1844 > bfd_get_file_size (abfd))
1845 {
1846 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1847 warning (_("Discarding section %s which has a section size (%s"
1848 ") larger than the file size [in module %s]"),
1849 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1850 bfd_get_filename (abfd));
1851 }
1852 else if (section_is_p (sectp->name, &names.info))
1853 {
1854 this->info.s.section = sectp;
1855 this->info.size = bfd_section_size (sectp);
1856 }
1857 else if (section_is_p (sectp->name, &names.abbrev))
1858 {
1859 this->abbrev.s.section = sectp;
1860 this->abbrev.size = bfd_section_size (sectp);
1861 }
1862 else if (section_is_p (sectp->name, &names.line))
1863 {
1864 this->line.s.section = sectp;
1865 this->line.size = bfd_section_size (sectp);
1866 }
1867 else if (section_is_p (sectp->name, &names.loc))
1868 {
1869 this->loc.s.section = sectp;
1870 this->loc.size = bfd_section_size (sectp);
1871 }
1872 else if (section_is_p (sectp->name, &names.loclists))
1873 {
1874 this->loclists.s.section = sectp;
1875 this->loclists.size = bfd_section_size (sectp);
1876 }
1877 else if (section_is_p (sectp->name, &names.macinfo))
1878 {
1879 this->macinfo.s.section = sectp;
1880 this->macinfo.size = bfd_section_size (sectp);
1881 }
1882 else if (section_is_p (sectp->name, &names.macro))
1883 {
1884 this->macro.s.section = sectp;
1885 this->macro.size = bfd_section_size (sectp);
1886 }
1887 else if (section_is_p (sectp->name, &names.str))
1888 {
1889 this->str.s.section = sectp;
1890 this->str.size = bfd_section_size (sectp);
1891 }
1892 else if (section_is_p (sectp->name, &names.str_offsets))
1893 {
1894 this->str_offsets.s.section = sectp;
1895 this->str_offsets.size = bfd_section_size (sectp);
1896 }
1897 else if (section_is_p (sectp->name, &names.line_str))
1898 {
1899 this->line_str.s.section = sectp;
1900 this->line_str.size = bfd_section_size (sectp);
1901 }
1902 else if (section_is_p (sectp->name, &names.addr))
1903 {
1904 this->addr.s.section = sectp;
1905 this->addr.size = bfd_section_size (sectp);
1906 }
1907 else if (section_is_p (sectp->name, &names.frame))
1908 {
1909 this->frame.s.section = sectp;
1910 this->frame.size = bfd_section_size (sectp);
1911 }
1912 else if (section_is_p (sectp->name, &names.eh_frame))
1913 {
1914 this->eh_frame.s.section = sectp;
1915 this->eh_frame.size = bfd_section_size (sectp);
1916 }
1917 else if (section_is_p (sectp->name, &names.ranges))
1918 {
1919 this->ranges.s.section = sectp;
1920 this->ranges.size = bfd_section_size (sectp);
1921 }
1922 else if (section_is_p (sectp->name, &names.rnglists))
1923 {
1924 this->rnglists.s.section = sectp;
1925 this->rnglists.size = bfd_section_size (sectp);
1926 }
1927 else if (section_is_p (sectp->name, &names.types))
1928 {
1929 struct dwarf2_section_info type_section;
1930
1931 memset (&type_section, 0, sizeof (type_section));
1932 type_section.s.section = sectp;
1933 type_section.size = bfd_section_size (sectp);
1934
1935 this->types.push_back (type_section);
1936 }
1937 else if (section_is_p (sectp->name, &names.gdb_index))
1938 {
1939 this->gdb_index.s.section = sectp;
1940 this->gdb_index.size = bfd_section_size (sectp);
1941 }
1942 else if (section_is_p (sectp->name, &names.debug_names))
1943 {
1944 this->debug_names.s.section = sectp;
1945 this->debug_names.size = bfd_section_size (sectp);
1946 }
1947 else if (section_is_p (sectp->name, &names.debug_aranges))
1948 {
1949 this->debug_aranges.s.section = sectp;
1950 this->debug_aranges.size = bfd_section_size (sectp);
1951 }
1952
1953 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1954 && bfd_section_vma (sectp) == 0)
1955 this->has_section_at_zero = true;
1956 }
1957
1958 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1959 SECTION_NAME. */
1960
1961 void
1962 dwarf2_get_section_info (struct objfile *objfile,
1963 enum dwarf2_section_enum sect,
1964 asection **sectp, const gdb_byte **bufp,
1965 bfd_size_type *sizep)
1966 {
1967 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
1968 struct dwarf2_section_info *info;
1969
1970 /* We may see an objfile without any DWARF, in which case we just
1971 return nothing. */
1972 if (data == NULL)
1973 {
1974 *sectp = NULL;
1975 *bufp = NULL;
1976 *sizep = 0;
1977 return;
1978 }
1979 switch (sect)
1980 {
1981 case DWARF2_DEBUG_FRAME:
1982 info = &data->frame;
1983 break;
1984 case DWARF2_EH_FRAME:
1985 info = &data->eh_frame;
1986 break;
1987 default:
1988 gdb_assert_not_reached ("unexpected section");
1989 }
1990
1991 info->read (objfile);
1992
1993 *sectp = info->get_bfd_section ();
1994 *bufp = info->buffer;
1995 *sizep = info->size;
1996 }
1997
1998 /* A helper function to find the sections for a .dwz file. */
1999
2000 static void
2001 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2002 {
2003 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2004
2005 /* Note that we only support the standard ELF names, because .dwz
2006 is ELF-only (at the time of writing). */
2007 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2008 {
2009 dwz_file->abbrev.s.section = sectp;
2010 dwz_file->abbrev.size = bfd_section_size (sectp);
2011 }
2012 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2013 {
2014 dwz_file->info.s.section = sectp;
2015 dwz_file->info.size = bfd_section_size (sectp);
2016 }
2017 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2018 {
2019 dwz_file->str.s.section = sectp;
2020 dwz_file->str.size = bfd_section_size (sectp);
2021 }
2022 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2023 {
2024 dwz_file->line.s.section = sectp;
2025 dwz_file->line.size = bfd_section_size (sectp);
2026 }
2027 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2028 {
2029 dwz_file->macro.s.section = sectp;
2030 dwz_file->macro.size = bfd_section_size (sectp);
2031 }
2032 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2033 {
2034 dwz_file->gdb_index.s.section = sectp;
2035 dwz_file->gdb_index.size = bfd_section_size (sectp);
2036 }
2037 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2038 {
2039 dwz_file->debug_names.s.section = sectp;
2040 dwz_file->debug_names.size = bfd_section_size (sectp);
2041 }
2042 }
2043
2044 /* See dwarf2read.h. */
2045
2046 struct dwz_file *
2047 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2048 {
2049 const char *filename;
2050 bfd_size_type buildid_len_arg;
2051 size_t buildid_len;
2052 bfd_byte *buildid;
2053
2054 if (dwarf2_per_objfile->dwz_file != NULL)
2055 return dwarf2_per_objfile->dwz_file.get ();
2056
2057 bfd_set_error (bfd_error_no_error);
2058 gdb::unique_xmalloc_ptr<char> data
2059 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2060 &buildid_len_arg, &buildid));
2061 if (data == NULL)
2062 {
2063 if (bfd_get_error () == bfd_error_no_error)
2064 return NULL;
2065 error (_("could not read '.gnu_debugaltlink' section: %s"),
2066 bfd_errmsg (bfd_get_error ()));
2067 }
2068
2069 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2070
2071 buildid_len = (size_t) buildid_len_arg;
2072
2073 filename = data.get ();
2074
2075 std::string abs_storage;
2076 if (!IS_ABSOLUTE_PATH (filename))
2077 {
2078 gdb::unique_xmalloc_ptr<char> abs
2079 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2080
2081 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2082 filename = abs_storage.c_str ();
2083 }
2084
2085 /* First try the file name given in the section. If that doesn't
2086 work, try to use the build-id instead. */
2087 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2088 if (dwz_bfd != NULL)
2089 {
2090 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2091 dwz_bfd.reset (nullptr);
2092 }
2093
2094 if (dwz_bfd == NULL)
2095 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2096
2097 if (dwz_bfd == nullptr)
2098 {
2099 gdb::unique_xmalloc_ptr<char> alt_filename;
2100 const char *origname = dwarf2_per_objfile->objfile->original_name;
2101
2102 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2103 buildid_len,
2104 origname,
2105 &alt_filename));
2106
2107 if (fd.get () >= 0)
2108 {
2109 /* File successfully retrieved from server. */
2110 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2111
2112 if (dwz_bfd == nullptr)
2113 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2114 alt_filename.get ());
2115 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2116 dwz_bfd.reset (nullptr);
2117 }
2118 }
2119
2120 if (dwz_bfd == NULL)
2121 error (_("could not find '.gnu_debugaltlink' file for %s"),
2122 objfile_name (dwarf2_per_objfile->objfile));
2123
2124 std::unique_ptr<struct dwz_file> result
2125 (new struct dwz_file (std::move (dwz_bfd)));
2126
2127 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2128 result.get ());
2129
2130 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2131 result->dwz_bfd.get ());
2132 dwarf2_per_objfile->dwz_file = std::move (result);
2133 return dwarf2_per_objfile->dwz_file.get ();
2134 }
2135 \f
2136 /* DWARF quick_symbols_functions support. */
2137
2138 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2139 unique line tables, so we maintain a separate table of all .debug_line
2140 derived entries to support the sharing.
2141 All the quick functions need is the list of file names. We discard the
2142 line_header when we're done and don't need to record it here. */
2143 struct quick_file_names
2144 {
2145 /* The data used to construct the hash key. */
2146 struct stmt_list_hash hash;
2147
2148 /* The number of entries in file_names, real_names. */
2149 unsigned int num_file_names;
2150
2151 /* The file names from the line table, after being run through
2152 file_full_name. */
2153 const char **file_names;
2154
2155 /* The file names from the line table after being run through
2156 gdb_realpath. These are computed lazily. */
2157 const char **real_names;
2158 };
2159
2160 /* When using the index (and thus not using psymtabs), each CU has an
2161 object of this type. This is used to hold information needed by
2162 the various "quick" methods. */
2163 struct dwarf2_per_cu_quick_data
2164 {
2165 /* The file table. This can be NULL if there was no file table
2166 or it's currently not read in.
2167 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2168 struct quick_file_names *file_names;
2169
2170 /* The corresponding symbol table. This is NULL if symbols for this
2171 CU have not yet been read. */
2172 struct compunit_symtab *compunit_symtab;
2173
2174 /* A temporary mark bit used when iterating over all CUs in
2175 expand_symtabs_matching. */
2176 unsigned int mark : 1;
2177
2178 /* True if we've tried to read the file table and found there isn't one.
2179 There will be no point in trying to read it again next time. */
2180 unsigned int no_file_data : 1;
2181 };
2182
2183 /* Utility hash function for a stmt_list_hash. */
2184
2185 static hashval_t
2186 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2187 {
2188 hashval_t v = 0;
2189
2190 if (stmt_list_hash->dwo_unit != NULL)
2191 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2192 v += to_underlying (stmt_list_hash->line_sect_off);
2193 return v;
2194 }
2195
2196 /* Utility equality function for a stmt_list_hash. */
2197
2198 static int
2199 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2200 const struct stmt_list_hash *rhs)
2201 {
2202 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2203 return 0;
2204 if (lhs->dwo_unit != NULL
2205 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2206 return 0;
2207
2208 return lhs->line_sect_off == rhs->line_sect_off;
2209 }
2210
2211 /* Hash function for a quick_file_names. */
2212
2213 static hashval_t
2214 hash_file_name_entry (const void *e)
2215 {
2216 const struct quick_file_names *file_data
2217 = (const struct quick_file_names *) e;
2218
2219 return hash_stmt_list_entry (&file_data->hash);
2220 }
2221
2222 /* Equality function for a quick_file_names. */
2223
2224 static int
2225 eq_file_name_entry (const void *a, const void *b)
2226 {
2227 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2228 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2229
2230 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2231 }
2232
2233 /* Delete function for a quick_file_names. */
2234
2235 static void
2236 delete_file_name_entry (void *e)
2237 {
2238 struct quick_file_names *file_data = (struct quick_file_names *) e;
2239 int i;
2240
2241 for (i = 0; i < file_data->num_file_names; ++i)
2242 {
2243 xfree ((void*) file_data->file_names[i]);
2244 if (file_data->real_names)
2245 xfree ((void*) file_data->real_names[i]);
2246 }
2247
2248 /* The space for the struct itself lives on objfile_obstack,
2249 so we don't free it here. */
2250 }
2251
2252 /* Create a quick_file_names hash table. */
2253
2254 static htab_up
2255 create_quick_file_names_table (unsigned int nr_initial_entries)
2256 {
2257 return htab_up (htab_create_alloc (nr_initial_entries,
2258 hash_file_name_entry, eq_file_name_entry,
2259 delete_file_name_entry, xcalloc, xfree));
2260 }
2261
2262 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2263 have to be created afterwards. You should call age_cached_comp_units after
2264 processing PER_CU->CU. dw2_setup must have been already called. */
2265
2266 static void
2267 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2268 {
2269 if (per_cu->is_debug_types)
2270 load_full_type_unit (per_cu);
2271 else
2272 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2273
2274 if (per_cu->cu == NULL)
2275 return; /* Dummy CU. */
2276
2277 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2278 }
2279
2280 /* Read in the symbols for PER_CU. */
2281
2282 static void
2283 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2284 {
2285 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2286
2287 /* Skip type_unit_groups, reading the type units they contain
2288 is handled elsewhere. */
2289 if (per_cu->type_unit_group_p ())
2290 return;
2291
2292 /* The destructor of dwarf2_queue_guard frees any entries left on
2293 the queue. After this point we're guaranteed to leave this function
2294 with the dwarf queue empty. */
2295 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2296
2297 if (dwarf2_per_objfile->using_index
2298 ? per_cu->v.quick->compunit_symtab == NULL
2299 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2300 {
2301 queue_comp_unit (per_cu, language_minimal);
2302 load_cu (per_cu, skip_partial);
2303
2304 /* If we just loaded a CU from a DWO, and we're working with an index
2305 that may badly handle TUs, load all the TUs in that DWO as well.
2306 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2307 if (!per_cu->is_debug_types
2308 && per_cu->cu != NULL
2309 && per_cu->cu->dwo_unit != NULL
2310 && dwarf2_per_objfile->index_table != NULL
2311 && dwarf2_per_objfile->index_table->version <= 7
2312 /* DWP files aren't supported yet. */
2313 && get_dwp_file (dwarf2_per_objfile) == NULL)
2314 queue_and_load_all_dwo_tus (per_cu);
2315 }
2316
2317 process_queue (dwarf2_per_objfile);
2318
2319 /* Age the cache, releasing compilation units that have not
2320 been used recently. */
2321 age_cached_comp_units (dwarf2_per_objfile);
2322 }
2323
2324 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2325 the objfile from which this CU came. Returns the resulting symbol
2326 table. */
2327
2328 static struct compunit_symtab *
2329 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2330 {
2331 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2332
2333 gdb_assert (dwarf2_per_objfile->using_index);
2334 if (!per_cu->v.quick->compunit_symtab)
2335 {
2336 free_cached_comp_units freer (dwarf2_per_objfile);
2337 scoped_restore decrementer = increment_reading_symtab ();
2338 dw2_do_instantiate_symtab (per_cu, skip_partial);
2339 process_cu_includes (dwarf2_per_objfile);
2340 }
2341
2342 return per_cu->v.quick->compunit_symtab;
2343 }
2344
2345 /* See declaration. */
2346
2347 dwarf2_per_cu_data *
2348 dwarf2_per_objfile::get_cutu (int index)
2349 {
2350 if (index >= this->all_comp_units.size ())
2351 {
2352 index -= this->all_comp_units.size ();
2353 gdb_assert (index < this->all_type_units.size ());
2354 return &this->all_type_units[index]->per_cu;
2355 }
2356
2357 return this->all_comp_units[index];
2358 }
2359
2360 /* See declaration. */
2361
2362 dwarf2_per_cu_data *
2363 dwarf2_per_objfile::get_cu (int index)
2364 {
2365 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2366
2367 return this->all_comp_units[index];
2368 }
2369
2370 /* See declaration. */
2371
2372 signatured_type *
2373 dwarf2_per_objfile::get_tu (int index)
2374 {
2375 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2376
2377 return this->all_type_units[index];
2378 }
2379
2380 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2381 objfile_obstack, and constructed with the specified field
2382 values. */
2383
2384 static dwarf2_per_cu_data *
2385 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2386 struct dwarf2_section_info *section,
2387 int is_dwz,
2388 sect_offset sect_off, ULONGEST length)
2389 {
2390 struct objfile *objfile = dwarf2_per_objfile->objfile;
2391 dwarf2_per_cu_data *the_cu
2392 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2393 struct dwarf2_per_cu_data);
2394 the_cu->sect_off = sect_off;
2395 the_cu->length = length;
2396 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2397 the_cu->section = section;
2398 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2399 struct dwarf2_per_cu_quick_data);
2400 the_cu->is_dwz = is_dwz;
2401 return the_cu;
2402 }
2403
2404 /* A helper for create_cus_from_index that handles a given list of
2405 CUs. */
2406
2407 static void
2408 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2409 const gdb_byte *cu_list, offset_type n_elements,
2410 struct dwarf2_section_info *section,
2411 int is_dwz)
2412 {
2413 for (offset_type i = 0; i < n_elements; i += 2)
2414 {
2415 gdb_static_assert (sizeof (ULONGEST) >= 8);
2416
2417 sect_offset sect_off
2418 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2419 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2420 cu_list += 2 * 8;
2421
2422 dwarf2_per_cu_data *per_cu
2423 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2424 sect_off, length);
2425 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2426 }
2427 }
2428
2429 /* Read the CU list from the mapped index, and use it to create all
2430 the CU objects for this objfile. */
2431
2432 static void
2433 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2434 const gdb_byte *cu_list, offset_type cu_list_elements,
2435 const gdb_byte *dwz_list, offset_type dwz_elements)
2436 {
2437 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2438 dwarf2_per_objfile->all_comp_units.reserve
2439 ((cu_list_elements + dwz_elements) / 2);
2440
2441 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2442 &dwarf2_per_objfile->info, 0);
2443
2444 if (dwz_elements == 0)
2445 return;
2446
2447 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2448 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2449 &dwz->info, 1);
2450 }
2451
2452 /* Create the signatured type hash table from the index. */
2453
2454 static void
2455 create_signatured_type_table_from_index
2456 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2457 struct dwarf2_section_info *section,
2458 const gdb_byte *bytes,
2459 offset_type elements)
2460 {
2461 struct objfile *objfile = dwarf2_per_objfile->objfile;
2462
2463 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2464 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2465
2466 htab_up sig_types_hash = allocate_signatured_type_table ();
2467
2468 for (offset_type i = 0; i < elements; i += 3)
2469 {
2470 struct signatured_type *sig_type;
2471 ULONGEST signature;
2472 void **slot;
2473 cu_offset type_offset_in_tu;
2474
2475 gdb_static_assert (sizeof (ULONGEST) >= 8);
2476 sect_offset sect_off
2477 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2478 type_offset_in_tu
2479 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2480 BFD_ENDIAN_LITTLE);
2481 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2482 bytes += 3 * 8;
2483
2484 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2485 struct signatured_type);
2486 sig_type->signature = signature;
2487 sig_type->type_offset_in_tu = type_offset_in_tu;
2488 sig_type->per_cu.is_debug_types = 1;
2489 sig_type->per_cu.section = section;
2490 sig_type->per_cu.sect_off = sect_off;
2491 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2492 sig_type->per_cu.v.quick
2493 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2494 struct dwarf2_per_cu_quick_data);
2495
2496 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2497 *slot = sig_type;
2498
2499 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2500 }
2501
2502 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2503 }
2504
2505 /* Create the signatured type hash table from .debug_names. */
2506
2507 static void
2508 create_signatured_type_table_from_debug_names
2509 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2510 const mapped_debug_names &map,
2511 struct dwarf2_section_info *section,
2512 struct dwarf2_section_info *abbrev_section)
2513 {
2514 struct objfile *objfile = dwarf2_per_objfile->objfile;
2515
2516 section->read (objfile);
2517 abbrev_section->read (objfile);
2518
2519 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2520 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2521
2522 htab_up sig_types_hash = allocate_signatured_type_table ();
2523
2524 for (uint32_t i = 0; i < map.tu_count; ++i)
2525 {
2526 struct signatured_type *sig_type;
2527 void **slot;
2528
2529 sect_offset sect_off
2530 = (sect_offset) (extract_unsigned_integer
2531 (map.tu_table_reordered + i * map.offset_size,
2532 map.offset_size,
2533 map.dwarf5_byte_order));
2534
2535 comp_unit_head cu_header;
2536 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2537 abbrev_section,
2538 section->buffer + to_underlying (sect_off),
2539 rcuh_kind::TYPE);
2540
2541 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2542 struct signatured_type);
2543 sig_type->signature = cu_header.signature;
2544 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2545 sig_type->per_cu.is_debug_types = 1;
2546 sig_type->per_cu.section = section;
2547 sig_type->per_cu.sect_off = sect_off;
2548 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2549 sig_type->per_cu.v.quick
2550 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2551 struct dwarf2_per_cu_quick_data);
2552
2553 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2554 *slot = sig_type;
2555
2556 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2557 }
2558
2559 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2560 }
2561
2562 /* Read the address map data from the mapped index, and use it to
2563 populate the objfile's psymtabs_addrmap. */
2564
2565 static void
2566 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2567 struct mapped_index *index)
2568 {
2569 struct objfile *objfile = dwarf2_per_objfile->objfile;
2570 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2571 const gdb_byte *iter, *end;
2572 struct addrmap *mutable_map;
2573 CORE_ADDR baseaddr;
2574
2575 auto_obstack temp_obstack;
2576
2577 mutable_map = addrmap_create_mutable (&temp_obstack);
2578
2579 iter = index->address_table.data ();
2580 end = iter + index->address_table.size ();
2581
2582 baseaddr = objfile->text_section_offset ();
2583
2584 while (iter < end)
2585 {
2586 ULONGEST hi, lo, cu_index;
2587 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2588 iter += 8;
2589 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2590 iter += 8;
2591 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2592 iter += 4;
2593
2594 if (lo > hi)
2595 {
2596 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2597 hex_string (lo), hex_string (hi));
2598 continue;
2599 }
2600
2601 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2602 {
2603 complaint (_(".gdb_index address table has invalid CU number %u"),
2604 (unsigned) cu_index);
2605 continue;
2606 }
2607
2608 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2609 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2610 addrmap_set_empty (mutable_map, lo, hi - 1,
2611 dwarf2_per_objfile->get_cu (cu_index));
2612 }
2613
2614 objfile->partial_symtabs->psymtabs_addrmap
2615 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2616 }
2617
2618 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2619 populate the objfile's psymtabs_addrmap. */
2620
2621 static void
2622 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2623 struct dwarf2_section_info *section)
2624 {
2625 struct objfile *objfile = dwarf2_per_objfile->objfile;
2626 bfd *abfd = objfile->obfd;
2627 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2628 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2629
2630 auto_obstack temp_obstack;
2631 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2632
2633 std::unordered_map<sect_offset,
2634 dwarf2_per_cu_data *,
2635 gdb::hash_enum<sect_offset>>
2636 debug_info_offset_to_per_cu;
2637 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2638 {
2639 const auto insertpair
2640 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2641 if (!insertpair.second)
2642 {
2643 warning (_("Section .debug_aranges in %s has duplicate "
2644 "debug_info_offset %s, ignoring .debug_aranges."),
2645 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2646 return;
2647 }
2648 }
2649
2650 section->read (objfile);
2651
2652 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2653
2654 const gdb_byte *addr = section->buffer;
2655
2656 while (addr < section->buffer + section->size)
2657 {
2658 const gdb_byte *const entry_addr = addr;
2659 unsigned int bytes_read;
2660
2661 const LONGEST entry_length = read_initial_length (abfd, addr,
2662 &bytes_read);
2663 addr += bytes_read;
2664
2665 const gdb_byte *const entry_end = addr + entry_length;
2666 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2667 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2668 if (addr + entry_length > section->buffer + section->size)
2669 {
2670 warning (_("Section .debug_aranges in %s entry at offset %s "
2671 "length %s exceeds section length %s, "
2672 "ignoring .debug_aranges."),
2673 objfile_name (objfile),
2674 plongest (entry_addr - section->buffer),
2675 plongest (bytes_read + entry_length),
2676 pulongest (section->size));
2677 return;
2678 }
2679
2680 /* The version number. */
2681 const uint16_t version = read_2_bytes (abfd, addr);
2682 addr += 2;
2683 if (version != 2)
2684 {
2685 warning (_("Section .debug_aranges in %s entry at offset %s "
2686 "has unsupported version %d, ignoring .debug_aranges."),
2687 objfile_name (objfile),
2688 plongest (entry_addr - section->buffer), version);
2689 return;
2690 }
2691
2692 const uint64_t debug_info_offset
2693 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2694 addr += offset_size;
2695 const auto per_cu_it
2696 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2697 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2698 {
2699 warning (_("Section .debug_aranges in %s entry at offset %s "
2700 "debug_info_offset %s does not exists, "
2701 "ignoring .debug_aranges."),
2702 objfile_name (objfile),
2703 plongest (entry_addr - section->buffer),
2704 pulongest (debug_info_offset));
2705 return;
2706 }
2707 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2708
2709 const uint8_t address_size = *addr++;
2710 if (address_size < 1 || address_size > 8)
2711 {
2712 warning (_("Section .debug_aranges in %s entry at offset %s "
2713 "address_size %u is invalid, ignoring .debug_aranges."),
2714 objfile_name (objfile),
2715 plongest (entry_addr - section->buffer), address_size);
2716 return;
2717 }
2718
2719 const uint8_t segment_selector_size = *addr++;
2720 if (segment_selector_size != 0)
2721 {
2722 warning (_("Section .debug_aranges in %s entry at offset %s "
2723 "segment_selector_size %u is not supported, "
2724 "ignoring .debug_aranges."),
2725 objfile_name (objfile),
2726 plongest (entry_addr - section->buffer),
2727 segment_selector_size);
2728 return;
2729 }
2730
2731 /* Must pad to an alignment boundary that is twice the address
2732 size. It is undocumented by the DWARF standard but GCC does
2733 use it. */
2734 for (size_t padding = ((-(addr - section->buffer))
2735 & (2 * address_size - 1));
2736 padding > 0; padding--)
2737 if (*addr++ != 0)
2738 {
2739 warning (_("Section .debug_aranges in %s entry at offset %s "
2740 "padding is not zero, ignoring .debug_aranges."),
2741 objfile_name (objfile),
2742 plongest (entry_addr - section->buffer));
2743 return;
2744 }
2745
2746 for (;;)
2747 {
2748 if (addr + 2 * address_size > entry_end)
2749 {
2750 warning (_("Section .debug_aranges in %s entry at offset %s "
2751 "address list is not properly terminated, "
2752 "ignoring .debug_aranges."),
2753 objfile_name (objfile),
2754 plongest (entry_addr - section->buffer));
2755 return;
2756 }
2757 ULONGEST start = extract_unsigned_integer (addr, address_size,
2758 dwarf5_byte_order);
2759 addr += address_size;
2760 ULONGEST length = extract_unsigned_integer (addr, address_size,
2761 dwarf5_byte_order);
2762 addr += address_size;
2763 if (start == 0 && length == 0)
2764 break;
2765 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2766 {
2767 /* Symbol was eliminated due to a COMDAT group. */
2768 continue;
2769 }
2770 ULONGEST end = start + length;
2771 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2772 - baseaddr);
2773 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2774 - baseaddr);
2775 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2776 }
2777 }
2778
2779 objfile->partial_symtabs->psymtabs_addrmap
2780 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2781 }
2782
2783 /* Find a slot in the mapped index INDEX for the object named NAME.
2784 If NAME is found, set *VEC_OUT to point to the CU vector in the
2785 constant pool and return true. If NAME cannot be found, return
2786 false. */
2787
2788 static bool
2789 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2790 offset_type **vec_out)
2791 {
2792 offset_type hash;
2793 offset_type slot, step;
2794 int (*cmp) (const char *, const char *);
2795
2796 gdb::unique_xmalloc_ptr<char> without_params;
2797 if (current_language->la_language == language_cplus
2798 || current_language->la_language == language_fortran
2799 || current_language->la_language == language_d)
2800 {
2801 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2802 not contain any. */
2803
2804 if (strchr (name, '(') != NULL)
2805 {
2806 without_params = cp_remove_params (name);
2807
2808 if (without_params != NULL)
2809 name = without_params.get ();
2810 }
2811 }
2812
2813 /* Index version 4 did not support case insensitive searches. But the
2814 indices for case insensitive languages are built in lowercase, therefore
2815 simulate our NAME being searched is also lowercased. */
2816 hash = mapped_index_string_hash ((index->version == 4
2817 && case_sensitivity == case_sensitive_off
2818 ? 5 : index->version),
2819 name);
2820
2821 slot = hash & (index->symbol_table.size () - 1);
2822 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2823 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2824
2825 for (;;)
2826 {
2827 const char *str;
2828
2829 const auto &bucket = index->symbol_table[slot];
2830 if (bucket.name == 0 && bucket.vec == 0)
2831 return false;
2832
2833 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2834 if (!cmp (name, str))
2835 {
2836 *vec_out = (offset_type *) (index->constant_pool
2837 + MAYBE_SWAP (bucket.vec));
2838 return true;
2839 }
2840
2841 slot = (slot + step) & (index->symbol_table.size () - 1);
2842 }
2843 }
2844
2845 /* A helper function that reads the .gdb_index from BUFFER and fills
2846 in MAP. FILENAME is the name of the file containing the data;
2847 it is used for error reporting. DEPRECATED_OK is true if it is
2848 ok to use deprecated sections.
2849
2850 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2851 out parameters that are filled in with information about the CU and
2852 TU lists in the section.
2853
2854 Returns true if all went well, false otherwise. */
2855
2856 static bool
2857 read_gdb_index_from_buffer (const char *filename,
2858 bool deprecated_ok,
2859 gdb::array_view<const gdb_byte> buffer,
2860 struct mapped_index *map,
2861 const gdb_byte **cu_list,
2862 offset_type *cu_list_elements,
2863 const gdb_byte **types_list,
2864 offset_type *types_list_elements)
2865 {
2866 const gdb_byte *addr = &buffer[0];
2867
2868 /* Version check. */
2869 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2870 /* Versions earlier than 3 emitted every copy of a psymbol. This
2871 causes the index to behave very poorly for certain requests. Version 3
2872 contained incomplete addrmap. So, it seems better to just ignore such
2873 indices. */
2874 if (version < 4)
2875 {
2876 static int warning_printed = 0;
2877 if (!warning_printed)
2878 {
2879 warning (_("Skipping obsolete .gdb_index section in %s."),
2880 filename);
2881 warning_printed = 1;
2882 }
2883 return 0;
2884 }
2885 /* Index version 4 uses a different hash function than index version
2886 5 and later.
2887
2888 Versions earlier than 6 did not emit psymbols for inlined
2889 functions. Using these files will cause GDB not to be able to
2890 set breakpoints on inlined functions by name, so we ignore these
2891 indices unless the user has done
2892 "set use-deprecated-index-sections on". */
2893 if (version < 6 && !deprecated_ok)
2894 {
2895 static int warning_printed = 0;
2896 if (!warning_printed)
2897 {
2898 warning (_("\
2899 Skipping deprecated .gdb_index section in %s.\n\
2900 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2901 to use the section anyway."),
2902 filename);
2903 warning_printed = 1;
2904 }
2905 return 0;
2906 }
2907 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2908 of the TU (for symbols coming from TUs),
2909 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2910 Plus gold-generated indices can have duplicate entries for global symbols,
2911 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2912 These are just performance bugs, and we can't distinguish gdb-generated
2913 indices from gold-generated ones, so issue no warning here. */
2914
2915 /* Indexes with higher version than the one supported by GDB may be no
2916 longer backward compatible. */
2917 if (version > 8)
2918 return 0;
2919
2920 map->version = version;
2921
2922 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2923
2924 int i = 0;
2925 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2926 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2927 / 8);
2928 ++i;
2929
2930 *types_list = addr + MAYBE_SWAP (metadata[i]);
2931 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2932 - MAYBE_SWAP (metadata[i]))
2933 / 8);
2934 ++i;
2935
2936 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2937 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2938 map->address_table
2939 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2940 ++i;
2941
2942 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2943 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2944 map->symbol_table
2945 = gdb::array_view<mapped_index::symbol_table_slot>
2946 ((mapped_index::symbol_table_slot *) symbol_table,
2947 (mapped_index::symbol_table_slot *) symbol_table_end);
2948
2949 ++i;
2950 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2951
2952 return 1;
2953 }
2954
2955 /* Callback types for dwarf2_read_gdb_index. */
2956
2957 typedef gdb::function_view
2958 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2959 get_gdb_index_contents_ftype;
2960 typedef gdb::function_view
2961 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2962 get_gdb_index_contents_dwz_ftype;
2963
2964 /* Read .gdb_index. If everything went ok, initialize the "quick"
2965 elements of all the CUs and return 1. Otherwise, return 0. */
2966
2967 static int
2968 dwarf2_read_gdb_index
2969 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2970 get_gdb_index_contents_ftype get_gdb_index_contents,
2971 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2972 {
2973 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2974 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2975 struct dwz_file *dwz;
2976 struct objfile *objfile = dwarf2_per_objfile->objfile;
2977
2978 gdb::array_view<const gdb_byte> main_index_contents
2979 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
2980
2981 if (main_index_contents.empty ())
2982 return 0;
2983
2984 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2985 if (!read_gdb_index_from_buffer (objfile_name (objfile),
2986 use_deprecated_index_sections,
2987 main_index_contents, map.get (), &cu_list,
2988 &cu_list_elements, &types_list,
2989 &types_list_elements))
2990 return 0;
2991
2992 /* Don't use the index if it's empty. */
2993 if (map->symbol_table.empty ())
2994 return 0;
2995
2996 /* If there is a .dwz file, read it so we can get its CU list as
2997 well. */
2998 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2999 if (dwz != NULL)
3000 {
3001 struct mapped_index dwz_map;
3002 const gdb_byte *dwz_types_ignore;
3003 offset_type dwz_types_elements_ignore;
3004
3005 gdb::array_view<const gdb_byte> dwz_index_content
3006 = get_gdb_index_contents_dwz (objfile, dwz);
3007
3008 if (dwz_index_content.empty ())
3009 return 0;
3010
3011 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3012 1, dwz_index_content, &dwz_map,
3013 &dwz_list, &dwz_list_elements,
3014 &dwz_types_ignore,
3015 &dwz_types_elements_ignore))
3016 {
3017 warning (_("could not read '.gdb_index' section from %s; skipping"),
3018 bfd_get_filename (dwz->dwz_bfd.get ()));
3019 return 0;
3020 }
3021 }
3022
3023 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3024 dwz_list, dwz_list_elements);
3025
3026 if (types_list_elements)
3027 {
3028 /* We can only handle a single .debug_types when we have an
3029 index. */
3030 if (dwarf2_per_objfile->types.size () != 1)
3031 return 0;
3032
3033 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3034
3035 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3036 types_list, types_list_elements);
3037 }
3038
3039 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3040
3041 dwarf2_per_objfile->index_table = std::move (map);
3042 dwarf2_per_objfile->using_index = 1;
3043 dwarf2_per_objfile->quick_file_names_table =
3044 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3045
3046 return 1;
3047 }
3048
3049 /* die_reader_func for dw2_get_file_names. */
3050
3051 static void
3052 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3053 const gdb_byte *info_ptr,
3054 struct die_info *comp_unit_die)
3055 {
3056 struct dwarf2_cu *cu = reader->cu;
3057 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3058 struct dwarf2_per_objfile *dwarf2_per_objfile
3059 = cu->per_cu->dwarf2_per_objfile;
3060 struct objfile *objfile = dwarf2_per_objfile->objfile;
3061 struct dwarf2_per_cu_data *lh_cu;
3062 struct attribute *attr;
3063 void **slot;
3064 struct quick_file_names *qfn;
3065
3066 gdb_assert (! this_cu->is_debug_types);
3067
3068 /* Our callers never want to match partial units -- instead they
3069 will match the enclosing full CU. */
3070 if (comp_unit_die->tag == DW_TAG_partial_unit)
3071 {
3072 this_cu->v.quick->no_file_data = 1;
3073 return;
3074 }
3075
3076 lh_cu = this_cu;
3077 slot = NULL;
3078
3079 line_header_up lh;
3080 sect_offset line_offset {};
3081
3082 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3083 if (attr != nullptr)
3084 {
3085 struct quick_file_names find_entry;
3086
3087 line_offset = (sect_offset) DW_UNSND (attr);
3088
3089 /* We may have already read in this line header (TU line header sharing).
3090 If we have we're done. */
3091 find_entry.hash.dwo_unit = cu->dwo_unit;
3092 find_entry.hash.line_sect_off = line_offset;
3093 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3094 &find_entry, INSERT);
3095 if (*slot != NULL)
3096 {
3097 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3098 return;
3099 }
3100
3101 lh = dwarf_decode_line_header (line_offset, cu);
3102 }
3103 if (lh == NULL)
3104 {
3105 lh_cu->v.quick->no_file_data = 1;
3106 return;
3107 }
3108
3109 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3110 qfn->hash.dwo_unit = cu->dwo_unit;
3111 qfn->hash.line_sect_off = line_offset;
3112 gdb_assert (slot != NULL);
3113 *slot = qfn;
3114
3115 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3116
3117 int offset = 0;
3118 if (strcmp (fnd.name, "<unknown>") != 0)
3119 ++offset;
3120
3121 qfn->num_file_names = offset + lh->file_names_size ();
3122 qfn->file_names =
3123 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3124 if (offset != 0)
3125 qfn->file_names[0] = xstrdup (fnd.name);
3126 for (int i = 0; i < lh->file_names_size (); ++i)
3127 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3128 fnd.comp_dir).release ();
3129 qfn->real_names = NULL;
3130
3131 lh_cu->v.quick->file_names = qfn;
3132 }
3133
3134 /* A helper for the "quick" functions which attempts to read the line
3135 table for THIS_CU. */
3136
3137 static struct quick_file_names *
3138 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3139 {
3140 /* This should never be called for TUs. */
3141 gdb_assert (! this_cu->is_debug_types);
3142 /* Nor type unit groups. */
3143 gdb_assert (! this_cu->type_unit_group_p ());
3144
3145 if (this_cu->v.quick->file_names != NULL)
3146 return this_cu->v.quick->file_names;
3147 /* If we know there is no line data, no point in looking again. */
3148 if (this_cu->v.quick->no_file_data)
3149 return NULL;
3150
3151 cutu_reader reader (this_cu);
3152 if (!reader.dummy_p)
3153 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3154
3155 if (this_cu->v.quick->no_file_data)
3156 return NULL;
3157 return this_cu->v.quick->file_names;
3158 }
3159
3160 /* A helper for the "quick" functions which computes and caches the
3161 real path for a given file name from the line table. */
3162
3163 static const char *
3164 dw2_get_real_path (struct objfile *objfile,
3165 struct quick_file_names *qfn, int index)
3166 {
3167 if (qfn->real_names == NULL)
3168 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3169 qfn->num_file_names, const char *);
3170
3171 if (qfn->real_names[index] == NULL)
3172 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3173
3174 return qfn->real_names[index];
3175 }
3176
3177 static struct symtab *
3178 dw2_find_last_source_symtab (struct objfile *objfile)
3179 {
3180 struct dwarf2_per_objfile *dwarf2_per_objfile
3181 = get_dwarf2_per_objfile (objfile);
3182 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3183 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3184
3185 if (cust == NULL)
3186 return NULL;
3187
3188 return compunit_primary_filetab (cust);
3189 }
3190
3191 /* Traversal function for dw2_forget_cached_source_info. */
3192
3193 static int
3194 dw2_free_cached_file_names (void **slot, void *info)
3195 {
3196 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3197
3198 if (file_data->real_names)
3199 {
3200 int i;
3201
3202 for (i = 0; i < file_data->num_file_names; ++i)
3203 {
3204 xfree ((void*) file_data->real_names[i]);
3205 file_data->real_names[i] = NULL;
3206 }
3207 }
3208
3209 return 1;
3210 }
3211
3212 static void
3213 dw2_forget_cached_source_info (struct objfile *objfile)
3214 {
3215 struct dwarf2_per_objfile *dwarf2_per_objfile
3216 = get_dwarf2_per_objfile (objfile);
3217
3218 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3219 dw2_free_cached_file_names, NULL);
3220 }
3221
3222 /* Helper function for dw2_map_symtabs_matching_filename that expands
3223 the symtabs and calls the iterator. */
3224
3225 static int
3226 dw2_map_expand_apply (struct objfile *objfile,
3227 struct dwarf2_per_cu_data *per_cu,
3228 const char *name, const char *real_path,
3229 gdb::function_view<bool (symtab *)> callback)
3230 {
3231 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3232
3233 /* Don't visit already-expanded CUs. */
3234 if (per_cu->v.quick->compunit_symtab)
3235 return 0;
3236
3237 /* This may expand more than one symtab, and we want to iterate over
3238 all of them. */
3239 dw2_instantiate_symtab (per_cu, false);
3240
3241 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3242 last_made, callback);
3243 }
3244
3245 /* Implementation of the map_symtabs_matching_filename method. */
3246
3247 static bool
3248 dw2_map_symtabs_matching_filename
3249 (struct objfile *objfile, const char *name, const char *real_path,
3250 gdb::function_view<bool (symtab *)> callback)
3251 {
3252 const char *name_basename = lbasename (name);
3253 struct dwarf2_per_objfile *dwarf2_per_objfile
3254 = get_dwarf2_per_objfile (objfile);
3255
3256 /* The rule is CUs specify all the files, including those used by
3257 any TU, so there's no need to scan TUs here. */
3258
3259 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3260 {
3261 /* We only need to look at symtabs not already expanded. */
3262 if (per_cu->v.quick->compunit_symtab)
3263 continue;
3264
3265 quick_file_names *file_data = dw2_get_file_names (per_cu);
3266 if (file_data == NULL)
3267 continue;
3268
3269 for (int j = 0; j < file_data->num_file_names; ++j)
3270 {
3271 const char *this_name = file_data->file_names[j];
3272 const char *this_real_name;
3273
3274 if (compare_filenames_for_search (this_name, name))
3275 {
3276 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3277 callback))
3278 return true;
3279 continue;
3280 }
3281
3282 /* Before we invoke realpath, which can get expensive when many
3283 files are involved, do a quick comparison of the basenames. */
3284 if (! basenames_may_differ
3285 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3286 continue;
3287
3288 this_real_name = dw2_get_real_path (objfile, file_data, j);
3289 if (compare_filenames_for_search (this_real_name, name))
3290 {
3291 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3292 callback))
3293 return true;
3294 continue;
3295 }
3296
3297 if (real_path != NULL)
3298 {
3299 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3300 gdb_assert (IS_ABSOLUTE_PATH (name));
3301 if (this_real_name != NULL
3302 && FILENAME_CMP (real_path, this_real_name) == 0)
3303 {
3304 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3305 callback))
3306 return true;
3307 continue;
3308 }
3309 }
3310 }
3311 }
3312
3313 return false;
3314 }
3315
3316 /* Struct used to manage iterating over all CUs looking for a symbol. */
3317
3318 struct dw2_symtab_iterator
3319 {
3320 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3321 struct dwarf2_per_objfile *dwarf2_per_objfile;
3322 /* If set, only look for symbols that match that block. Valid values are
3323 GLOBAL_BLOCK and STATIC_BLOCK. */
3324 gdb::optional<block_enum> block_index;
3325 /* The kind of symbol we're looking for. */
3326 domain_enum domain;
3327 /* The list of CUs from the index entry of the symbol,
3328 or NULL if not found. */
3329 offset_type *vec;
3330 /* The next element in VEC to look at. */
3331 int next;
3332 /* The number of elements in VEC, or zero if there is no match. */
3333 int length;
3334 /* Have we seen a global version of the symbol?
3335 If so we can ignore all further global instances.
3336 This is to work around gold/15646, inefficient gold-generated
3337 indices. */
3338 int global_seen;
3339 };
3340
3341 /* Initialize the index symtab iterator ITER. */
3342
3343 static void
3344 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3345 struct dwarf2_per_objfile *dwarf2_per_objfile,
3346 gdb::optional<block_enum> block_index,
3347 domain_enum domain,
3348 const char *name)
3349 {
3350 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3351 iter->block_index = block_index;
3352 iter->domain = domain;
3353 iter->next = 0;
3354 iter->global_seen = 0;
3355
3356 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3357
3358 /* index is NULL if OBJF_READNOW. */
3359 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3360 iter->length = MAYBE_SWAP (*iter->vec);
3361 else
3362 {
3363 iter->vec = NULL;
3364 iter->length = 0;
3365 }
3366 }
3367
3368 /* Return the next matching CU or NULL if there are no more. */
3369
3370 static struct dwarf2_per_cu_data *
3371 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3372 {
3373 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3374
3375 for ( ; iter->next < iter->length; ++iter->next)
3376 {
3377 offset_type cu_index_and_attrs =
3378 MAYBE_SWAP (iter->vec[iter->next + 1]);
3379 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3380 gdb_index_symbol_kind symbol_kind =
3381 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3382 /* Only check the symbol attributes if they're present.
3383 Indices prior to version 7 don't record them,
3384 and indices >= 7 may elide them for certain symbols
3385 (gold does this). */
3386 int attrs_valid =
3387 (dwarf2_per_objfile->index_table->version >= 7
3388 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3389
3390 /* Don't crash on bad data. */
3391 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3392 + dwarf2_per_objfile->all_type_units.size ()))
3393 {
3394 complaint (_(".gdb_index entry has bad CU index"
3395 " [in module %s]"),
3396 objfile_name (dwarf2_per_objfile->objfile));
3397 continue;
3398 }
3399
3400 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3401
3402 /* Skip if already read in. */
3403 if (per_cu->v.quick->compunit_symtab)
3404 continue;
3405
3406 /* Check static vs global. */
3407 if (attrs_valid)
3408 {
3409 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3410
3411 if (iter->block_index.has_value ())
3412 {
3413 bool want_static = *iter->block_index == STATIC_BLOCK;
3414
3415 if (is_static != want_static)
3416 continue;
3417 }
3418
3419 /* Work around gold/15646. */
3420 if (!is_static && iter->global_seen)
3421 continue;
3422 if (!is_static)
3423 iter->global_seen = 1;
3424 }
3425
3426 /* Only check the symbol's kind if it has one. */
3427 if (attrs_valid)
3428 {
3429 switch (iter->domain)
3430 {
3431 case VAR_DOMAIN:
3432 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3433 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3434 /* Some types are also in VAR_DOMAIN. */
3435 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3436 continue;
3437 break;
3438 case STRUCT_DOMAIN:
3439 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3440 continue;
3441 break;
3442 case LABEL_DOMAIN:
3443 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3444 continue;
3445 break;
3446 case MODULE_DOMAIN:
3447 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3448 continue;
3449 break;
3450 default:
3451 break;
3452 }
3453 }
3454
3455 ++iter->next;
3456 return per_cu;
3457 }
3458
3459 return NULL;
3460 }
3461
3462 static struct compunit_symtab *
3463 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3464 const char *name, domain_enum domain)
3465 {
3466 struct compunit_symtab *stab_best = NULL;
3467 struct dwarf2_per_objfile *dwarf2_per_objfile
3468 = get_dwarf2_per_objfile (objfile);
3469
3470 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3471
3472 struct dw2_symtab_iterator iter;
3473 struct dwarf2_per_cu_data *per_cu;
3474
3475 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3476
3477 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3478 {
3479 struct symbol *sym, *with_opaque = NULL;
3480 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3481 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3482 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3483
3484 sym = block_find_symbol (block, name, domain,
3485 block_find_non_opaque_type_preferred,
3486 &with_opaque);
3487
3488 /* Some caution must be observed with overloaded functions
3489 and methods, since the index will not contain any overload
3490 information (but NAME might contain it). */
3491
3492 if (sym != NULL
3493 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3494 return stab;
3495 if (with_opaque != NULL
3496 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3497 stab_best = stab;
3498
3499 /* Keep looking through other CUs. */
3500 }
3501
3502 return stab_best;
3503 }
3504
3505 static void
3506 dw2_print_stats (struct objfile *objfile)
3507 {
3508 struct dwarf2_per_objfile *dwarf2_per_objfile
3509 = get_dwarf2_per_objfile (objfile);
3510 int total = (dwarf2_per_objfile->all_comp_units.size ()
3511 + dwarf2_per_objfile->all_type_units.size ());
3512 int count = 0;
3513
3514 for (int i = 0; i < total; ++i)
3515 {
3516 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3517
3518 if (!per_cu->v.quick->compunit_symtab)
3519 ++count;
3520 }
3521 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3522 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3523 }
3524
3525 /* This dumps minimal information about the index.
3526 It is called via "mt print objfiles".
3527 One use is to verify .gdb_index has been loaded by the
3528 gdb.dwarf2/gdb-index.exp testcase. */
3529
3530 static void
3531 dw2_dump (struct objfile *objfile)
3532 {
3533 struct dwarf2_per_objfile *dwarf2_per_objfile
3534 = get_dwarf2_per_objfile (objfile);
3535
3536 gdb_assert (dwarf2_per_objfile->using_index);
3537 printf_filtered (".gdb_index:");
3538 if (dwarf2_per_objfile->index_table != NULL)
3539 {
3540 printf_filtered (" version %d\n",
3541 dwarf2_per_objfile->index_table->version);
3542 }
3543 else
3544 printf_filtered (" faked for \"readnow\"\n");
3545 printf_filtered ("\n");
3546 }
3547
3548 static void
3549 dw2_expand_symtabs_for_function (struct objfile *objfile,
3550 const char *func_name)
3551 {
3552 struct dwarf2_per_objfile *dwarf2_per_objfile
3553 = get_dwarf2_per_objfile (objfile);
3554
3555 struct dw2_symtab_iterator iter;
3556 struct dwarf2_per_cu_data *per_cu;
3557
3558 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3559
3560 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3561 dw2_instantiate_symtab (per_cu, false);
3562
3563 }
3564
3565 static void
3566 dw2_expand_all_symtabs (struct objfile *objfile)
3567 {
3568 struct dwarf2_per_objfile *dwarf2_per_objfile
3569 = get_dwarf2_per_objfile (objfile);
3570 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3571 + dwarf2_per_objfile->all_type_units.size ());
3572
3573 for (int i = 0; i < total_units; ++i)
3574 {
3575 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3576
3577 /* We don't want to directly expand a partial CU, because if we
3578 read it with the wrong language, then assertion failures can
3579 be triggered later on. See PR symtab/23010. So, tell
3580 dw2_instantiate_symtab to skip partial CUs -- any important
3581 partial CU will be read via DW_TAG_imported_unit anyway. */
3582 dw2_instantiate_symtab (per_cu, true);
3583 }
3584 }
3585
3586 static void
3587 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3588 const char *fullname)
3589 {
3590 struct dwarf2_per_objfile *dwarf2_per_objfile
3591 = get_dwarf2_per_objfile (objfile);
3592
3593 /* We don't need to consider type units here.
3594 This is only called for examining code, e.g. expand_line_sal.
3595 There can be an order of magnitude (or more) more type units
3596 than comp units, and we avoid them if we can. */
3597
3598 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3599 {
3600 /* We only need to look at symtabs not already expanded. */
3601 if (per_cu->v.quick->compunit_symtab)
3602 continue;
3603
3604 quick_file_names *file_data = dw2_get_file_names (per_cu);
3605 if (file_data == NULL)
3606 continue;
3607
3608 for (int j = 0; j < file_data->num_file_names; ++j)
3609 {
3610 const char *this_fullname = file_data->file_names[j];
3611
3612 if (filename_cmp (this_fullname, fullname) == 0)
3613 {
3614 dw2_instantiate_symtab (per_cu, false);
3615 break;
3616 }
3617 }
3618 }
3619 }
3620
3621 static void
3622 dw2_map_matching_symbols
3623 (struct objfile *objfile,
3624 const lookup_name_info &name, domain_enum domain,
3625 int global,
3626 gdb::function_view<symbol_found_callback_ftype> callback,
3627 symbol_compare_ftype *ordered_compare)
3628 {
3629 /* Used for Ada. */
3630 struct dwarf2_per_objfile *dwarf2_per_objfile
3631 = get_dwarf2_per_objfile (objfile);
3632
3633 if (dwarf2_per_objfile->index_table != nullptr)
3634 {
3635 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3636 here though if the current language is Ada for a non-Ada objfile
3637 using GNU index. As Ada does not look for non-Ada symbols this
3638 function should just return. */
3639 return;
3640 }
3641
3642 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3643 inline psym_map_matching_symbols here, assuming all partial symtabs have
3644 been read in. */
3645 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3646
3647 for (compunit_symtab *cust : objfile->compunits ())
3648 {
3649 const struct block *block;
3650
3651 if (cust == NULL)
3652 continue;
3653 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3654 if (!iterate_over_symbols_terminated (block, name,
3655 domain, callback))
3656 return;
3657 }
3658 }
3659
3660 /* Starting from a search name, return the string that finds the upper
3661 bound of all strings that start with SEARCH_NAME in a sorted name
3662 list. Returns the empty string to indicate that the upper bound is
3663 the end of the list. */
3664
3665 static std::string
3666 make_sort_after_prefix_name (const char *search_name)
3667 {
3668 /* When looking to complete "func", we find the upper bound of all
3669 symbols that start with "func" by looking for where we'd insert
3670 the closest string that would follow "func" in lexicographical
3671 order. Usually, that's "func"-with-last-character-incremented,
3672 i.e. "fund". Mind non-ASCII characters, though. Usually those
3673 will be UTF-8 multi-byte sequences, but we can't be certain.
3674 Especially mind the 0xff character, which is a valid character in
3675 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3676 rule out compilers allowing it in identifiers. Note that
3677 conveniently, strcmp/strcasecmp are specified to compare
3678 characters interpreted as unsigned char. So what we do is treat
3679 the whole string as a base 256 number composed of a sequence of
3680 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3681 to 0, and carries 1 to the following more-significant position.
3682 If the very first character in SEARCH_NAME ends up incremented
3683 and carries/overflows, then the upper bound is the end of the
3684 list. The string after the empty string is also the empty
3685 string.
3686
3687 Some examples of this operation:
3688
3689 SEARCH_NAME => "+1" RESULT
3690
3691 "abc" => "abd"
3692 "ab\xff" => "ac"
3693 "\xff" "a" "\xff" => "\xff" "b"
3694 "\xff" => ""
3695 "\xff\xff" => ""
3696 "" => ""
3697
3698 Then, with these symbols for example:
3699
3700 func
3701 func1
3702 fund
3703
3704 completing "func" looks for symbols between "func" and
3705 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3706 which finds "func" and "func1", but not "fund".
3707
3708 And with:
3709
3710 funcÿ (Latin1 'ÿ' [0xff])
3711 funcÿ1
3712 fund
3713
3714 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3715 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3716
3717 And with:
3718
3719 ÿÿ (Latin1 'ÿ' [0xff])
3720 ÿÿ1
3721
3722 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3723 the end of the list.
3724 */
3725 std::string after = search_name;
3726 while (!after.empty () && (unsigned char) after.back () == 0xff)
3727 after.pop_back ();
3728 if (!after.empty ())
3729 after.back () = (unsigned char) after.back () + 1;
3730 return after;
3731 }
3732
3733 /* See declaration. */
3734
3735 std::pair<std::vector<name_component>::const_iterator,
3736 std::vector<name_component>::const_iterator>
3737 mapped_index_base::find_name_components_bounds
3738 (const lookup_name_info &lookup_name_without_params, language lang) const
3739 {
3740 auto *name_cmp
3741 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3742
3743 const char *lang_name
3744 = lookup_name_without_params.language_lookup_name (lang);
3745
3746 /* Comparison function object for lower_bound that matches against a
3747 given symbol name. */
3748 auto lookup_compare_lower = [&] (const name_component &elem,
3749 const char *name)
3750 {
3751 const char *elem_qualified = this->symbol_name_at (elem.idx);
3752 const char *elem_name = elem_qualified + elem.name_offset;
3753 return name_cmp (elem_name, name) < 0;
3754 };
3755
3756 /* Comparison function object for upper_bound that matches against a
3757 given symbol name. */
3758 auto lookup_compare_upper = [&] (const char *name,
3759 const name_component &elem)
3760 {
3761 const char *elem_qualified = this->symbol_name_at (elem.idx);
3762 const char *elem_name = elem_qualified + elem.name_offset;
3763 return name_cmp (name, elem_name) < 0;
3764 };
3765
3766 auto begin = this->name_components.begin ();
3767 auto end = this->name_components.end ();
3768
3769 /* Find the lower bound. */
3770 auto lower = [&] ()
3771 {
3772 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3773 return begin;
3774 else
3775 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3776 } ();
3777
3778 /* Find the upper bound. */
3779 auto upper = [&] ()
3780 {
3781 if (lookup_name_without_params.completion_mode ())
3782 {
3783 /* In completion mode, we want UPPER to point past all
3784 symbols names that have the same prefix. I.e., with
3785 these symbols, and completing "func":
3786
3787 function << lower bound
3788 function1
3789 other_function << upper bound
3790
3791 We find the upper bound by looking for the insertion
3792 point of "func"-with-last-character-incremented,
3793 i.e. "fund". */
3794 std::string after = make_sort_after_prefix_name (lang_name);
3795 if (after.empty ())
3796 return end;
3797 return std::lower_bound (lower, end, after.c_str (),
3798 lookup_compare_lower);
3799 }
3800 else
3801 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3802 } ();
3803
3804 return {lower, upper};
3805 }
3806
3807 /* See declaration. */
3808
3809 void
3810 mapped_index_base::build_name_components ()
3811 {
3812 if (!this->name_components.empty ())
3813 return;
3814
3815 this->name_components_casing = case_sensitivity;
3816 auto *name_cmp
3817 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3818
3819 /* The code below only knows how to break apart components of C++
3820 symbol names (and other languages that use '::' as
3821 namespace/module separator) and Ada symbol names. */
3822 auto count = this->symbol_name_count ();
3823 for (offset_type idx = 0; idx < count; idx++)
3824 {
3825 if (this->symbol_name_slot_invalid (idx))
3826 continue;
3827
3828 const char *name = this->symbol_name_at (idx);
3829
3830 /* Add each name component to the name component table. */
3831 unsigned int previous_len = 0;
3832
3833 if (strstr (name, "::") != nullptr)
3834 {
3835 for (unsigned int current_len = cp_find_first_component (name);
3836 name[current_len] != '\0';
3837 current_len += cp_find_first_component (name + current_len))
3838 {
3839 gdb_assert (name[current_len] == ':');
3840 this->name_components.push_back ({previous_len, idx});
3841 /* Skip the '::'. */
3842 current_len += 2;
3843 previous_len = current_len;
3844 }
3845 }
3846 else
3847 {
3848 /* Handle the Ada encoded (aka mangled) form here. */
3849 for (const char *iter = strstr (name, "__");
3850 iter != nullptr;
3851 iter = strstr (iter, "__"))
3852 {
3853 this->name_components.push_back ({previous_len, idx});
3854 iter += 2;
3855 previous_len = iter - name;
3856 }
3857 }
3858
3859 this->name_components.push_back ({previous_len, idx});
3860 }
3861
3862 /* Sort name_components elements by name. */
3863 auto name_comp_compare = [&] (const name_component &left,
3864 const name_component &right)
3865 {
3866 const char *left_qualified = this->symbol_name_at (left.idx);
3867 const char *right_qualified = this->symbol_name_at (right.idx);
3868
3869 const char *left_name = left_qualified + left.name_offset;
3870 const char *right_name = right_qualified + right.name_offset;
3871
3872 return name_cmp (left_name, right_name) < 0;
3873 };
3874
3875 std::sort (this->name_components.begin (),
3876 this->name_components.end (),
3877 name_comp_compare);
3878 }
3879
3880 /* Helper for dw2_expand_symtabs_matching that works with a
3881 mapped_index_base instead of the containing objfile. This is split
3882 to a separate function in order to be able to unit test the
3883 name_components matching using a mock mapped_index_base. For each
3884 symbol name that matches, calls MATCH_CALLBACK, passing it the
3885 symbol's index in the mapped_index_base symbol table. */
3886
3887 static void
3888 dw2_expand_symtabs_matching_symbol
3889 (mapped_index_base &index,
3890 const lookup_name_info &lookup_name_in,
3891 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3892 enum search_domain kind,
3893 gdb::function_view<bool (offset_type)> match_callback)
3894 {
3895 lookup_name_info lookup_name_without_params
3896 = lookup_name_in.make_ignore_params ();
3897
3898 /* Build the symbol name component sorted vector, if we haven't
3899 yet. */
3900 index.build_name_components ();
3901
3902 /* The same symbol may appear more than once in the range though.
3903 E.g., if we're looking for symbols that complete "w", and we have
3904 a symbol named "w1::w2", we'll find the two name components for
3905 that same symbol in the range. To be sure we only call the
3906 callback once per symbol, we first collect the symbol name
3907 indexes that matched in a temporary vector and ignore
3908 duplicates. */
3909 std::vector<offset_type> matches;
3910
3911 struct name_and_matcher
3912 {
3913 symbol_name_matcher_ftype *matcher;
3914 const std::string &name;
3915
3916 bool operator== (const name_and_matcher &other) const
3917 {
3918 return matcher == other.matcher && name == other.name;
3919 }
3920 };
3921
3922 /* A vector holding all the different symbol name matchers, for all
3923 languages. */
3924 std::vector<name_and_matcher> matchers;
3925
3926 for (int i = 0; i < nr_languages; i++)
3927 {
3928 enum language lang_e = (enum language) i;
3929
3930 const language_defn *lang = language_def (lang_e);
3931 symbol_name_matcher_ftype *name_matcher
3932 = get_symbol_name_matcher (lang, lookup_name_without_params);
3933
3934 name_and_matcher key {
3935 name_matcher,
3936 lookup_name_without_params.language_lookup_name (lang_e)
3937 };
3938
3939 /* Don't insert the same comparison routine more than once.
3940 Note that we do this linear walk. This is not a problem in
3941 practice because the number of supported languages is
3942 low. */
3943 if (std::find (matchers.begin (), matchers.end (), key)
3944 != matchers.end ())
3945 continue;
3946 matchers.push_back (std::move (key));
3947
3948 auto bounds
3949 = index.find_name_components_bounds (lookup_name_without_params,
3950 lang_e);
3951
3952 /* Now for each symbol name in range, check to see if we have a name
3953 match, and if so, call the MATCH_CALLBACK callback. */
3954
3955 for (; bounds.first != bounds.second; ++bounds.first)
3956 {
3957 const char *qualified = index.symbol_name_at (bounds.first->idx);
3958
3959 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3960 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3961 continue;
3962
3963 matches.push_back (bounds.first->idx);
3964 }
3965 }
3966
3967 std::sort (matches.begin (), matches.end ());
3968
3969 /* Finally call the callback, once per match. */
3970 ULONGEST prev = -1;
3971 for (offset_type idx : matches)
3972 {
3973 if (prev != idx)
3974 {
3975 if (!match_callback (idx))
3976 break;
3977 prev = idx;
3978 }
3979 }
3980
3981 /* Above we use a type wider than idx's for 'prev', since 0 and
3982 (offset_type)-1 are both possible values. */
3983 static_assert (sizeof (prev) > sizeof (offset_type), "");
3984 }
3985
3986 #if GDB_SELF_TEST
3987
3988 namespace selftests { namespace dw2_expand_symtabs_matching {
3989
3990 /* A mock .gdb_index/.debug_names-like name index table, enough to
3991 exercise dw2_expand_symtabs_matching_symbol, which works with the
3992 mapped_index_base interface. Builds an index from the symbol list
3993 passed as parameter to the constructor. */
3994 class mock_mapped_index : public mapped_index_base
3995 {
3996 public:
3997 mock_mapped_index (gdb::array_view<const char *> symbols)
3998 : m_symbol_table (symbols)
3999 {}
4000
4001 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4002
4003 /* Return the number of names in the symbol table. */
4004 size_t symbol_name_count () const override
4005 {
4006 return m_symbol_table.size ();
4007 }
4008
4009 /* Get the name of the symbol at IDX in the symbol table. */
4010 const char *symbol_name_at (offset_type idx) const override
4011 {
4012 return m_symbol_table[idx];
4013 }
4014
4015 private:
4016 gdb::array_view<const char *> m_symbol_table;
4017 };
4018
4019 /* Convenience function that converts a NULL pointer to a "<null>"
4020 string, to pass to print routines. */
4021
4022 static const char *
4023 string_or_null (const char *str)
4024 {
4025 return str != NULL ? str : "<null>";
4026 }
4027
4028 /* Check if a lookup_name_info built from
4029 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4030 index. EXPECTED_LIST is the list of expected matches, in expected
4031 matching order. If no match expected, then an empty list is
4032 specified. Returns true on success. On failure prints a warning
4033 indicating the file:line that failed, and returns false. */
4034
4035 static bool
4036 check_match (const char *file, int line,
4037 mock_mapped_index &mock_index,
4038 const char *name, symbol_name_match_type match_type,
4039 bool completion_mode,
4040 std::initializer_list<const char *> expected_list)
4041 {
4042 lookup_name_info lookup_name (name, match_type, completion_mode);
4043
4044 bool matched = true;
4045
4046 auto mismatch = [&] (const char *expected_str,
4047 const char *got)
4048 {
4049 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4050 "expected=\"%s\", got=\"%s\"\n"),
4051 file, line,
4052 (match_type == symbol_name_match_type::FULL
4053 ? "FULL" : "WILD"),
4054 name, string_or_null (expected_str), string_or_null (got));
4055 matched = false;
4056 };
4057
4058 auto expected_it = expected_list.begin ();
4059 auto expected_end = expected_list.end ();
4060
4061 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4062 NULL, ALL_DOMAIN,
4063 [&] (offset_type idx)
4064 {
4065 const char *matched_name = mock_index.symbol_name_at (idx);
4066 const char *expected_str
4067 = expected_it == expected_end ? NULL : *expected_it++;
4068
4069 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4070 mismatch (expected_str, matched_name);
4071 return true;
4072 });
4073
4074 const char *expected_str
4075 = expected_it == expected_end ? NULL : *expected_it++;
4076 if (expected_str != NULL)
4077 mismatch (expected_str, NULL);
4078
4079 return matched;
4080 }
4081
4082 /* The symbols added to the mock mapped_index for testing (in
4083 canonical form). */
4084 static const char *test_symbols[] = {
4085 "function",
4086 "std::bar",
4087 "std::zfunction",
4088 "std::zfunction2",
4089 "w1::w2",
4090 "ns::foo<char*>",
4091 "ns::foo<int>",
4092 "ns::foo<long>",
4093 "ns2::tmpl<int>::foo2",
4094 "(anonymous namespace)::A::B::C",
4095
4096 /* These are used to check that the increment-last-char in the
4097 matching algorithm for completion doesn't match "t1_fund" when
4098 completing "t1_func". */
4099 "t1_func",
4100 "t1_func1",
4101 "t1_fund",
4102 "t1_fund1",
4103
4104 /* A UTF-8 name with multi-byte sequences to make sure that
4105 cp-name-parser understands this as a single identifier ("função"
4106 is "function" in PT). */
4107 u8"u8função",
4108
4109 /* \377 (0xff) is Latin1 'ÿ'. */
4110 "yfunc\377",
4111
4112 /* \377 (0xff) is Latin1 'ÿ'. */
4113 "\377",
4114 "\377\377123",
4115
4116 /* A name with all sorts of complications. Starts with "z" to make
4117 it easier for the completion tests below. */
4118 #define Z_SYM_NAME \
4119 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4120 "::tuple<(anonymous namespace)::ui*, " \
4121 "std::default_delete<(anonymous namespace)::ui>, void>"
4122
4123 Z_SYM_NAME
4124 };
4125
4126 /* Returns true if the mapped_index_base::find_name_component_bounds
4127 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4128 in completion mode. */
4129
4130 static bool
4131 check_find_bounds_finds (mapped_index_base &index,
4132 const char *search_name,
4133 gdb::array_view<const char *> expected_syms)
4134 {
4135 lookup_name_info lookup_name (search_name,
4136 symbol_name_match_type::FULL, true);
4137
4138 auto bounds = index.find_name_components_bounds (lookup_name,
4139 language_cplus);
4140
4141 size_t distance = std::distance (bounds.first, bounds.second);
4142 if (distance != expected_syms.size ())
4143 return false;
4144
4145 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4146 {
4147 auto nc_elem = bounds.first + exp_elem;
4148 const char *qualified = index.symbol_name_at (nc_elem->idx);
4149 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4150 return false;
4151 }
4152
4153 return true;
4154 }
4155
4156 /* Test the lower-level mapped_index::find_name_component_bounds
4157 method. */
4158
4159 static void
4160 test_mapped_index_find_name_component_bounds ()
4161 {
4162 mock_mapped_index mock_index (test_symbols);
4163
4164 mock_index.build_name_components ();
4165
4166 /* Test the lower-level mapped_index::find_name_component_bounds
4167 method in completion mode. */
4168 {
4169 static const char *expected_syms[] = {
4170 "t1_func",
4171 "t1_func1",
4172 };
4173
4174 SELF_CHECK (check_find_bounds_finds (mock_index,
4175 "t1_func", expected_syms));
4176 }
4177
4178 /* Check that the increment-last-char in the name matching algorithm
4179 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4180 {
4181 static const char *expected_syms1[] = {
4182 "\377",
4183 "\377\377123",
4184 };
4185 SELF_CHECK (check_find_bounds_finds (mock_index,
4186 "\377", expected_syms1));
4187
4188 static const char *expected_syms2[] = {
4189 "\377\377123",
4190 };
4191 SELF_CHECK (check_find_bounds_finds (mock_index,
4192 "\377\377", expected_syms2));
4193 }
4194 }
4195
4196 /* Test dw2_expand_symtabs_matching_symbol. */
4197
4198 static void
4199 test_dw2_expand_symtabs_matching_symbol ()
4200 {
4201 mock_mapped_index mock_index (test_symbols);
4202
4203 /* We let all tests run until the end even if some fails, for debug
4204 convenience. */
4205 bool any_mismatch = false;
4206
4207 /* Create the expected symbols list (an initializer_list). Needed
4208 because lists have commas, and we need to pass them to CHECK,
4209 which is a macro. */
4210 #define EXPECT(...) { __VA_ARGS__ }
4211
4212 /* Wrapper for check_match that passes down the current
4213 __FILE__/__LINE__. */
4214 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4215 any_mismatch |= !check_match (__FILE__, __LINE__, \
4216 mock_index, \
4217 NAME, MATCH_TYPE, COMPLETION_MODE, \
4218 EXPECTED_LIST)
4219
4220 /* Identity checks. */
4221 for (const char *sym : test_symbols)
4222 {
4223 /* Should be able to match all existing symbols. */
4224 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4225 EXPECT (sym));
4226
4227 /* Should be able to match all existing symbols with
4228 parameters. */
4229 std::string with_params = std::string (sym) + "(int)";
4230 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4231 EXPECT (sym));
4232
4233 /* Should be able to match all existing symbols with
4234 parameters and qualifiers. */
4235 with_params = std::string (sym) + " ( int ) const";
4236 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4237 EXPECT (sym));
4238
4239 /* This should really find sym, but cp-name-parser.y doesn't
4240 know about lvalue/rvalue qualifiers yet. */
4241 with_params = std::string (sym) + " ( int ) &&";
4242 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4243 {});
4244 }
4245
4246 /* Check that the name matching algorithm for completion doesn't get
4247 confused with Latin1 'ÿ' / 0xff. */
4248 {
4249 static const char str[] = "\377";
4250 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4251 EXPECT ("\377", "\377\377123"));
4252 }
4253
4254 /* Check that the increment-last-char in the matching algorithm for
4255 completion doesn't match "t1_fund" when completing "t1_func". */
4256 {
4257 static const char str[] = "t1_func";
4258 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4259 EXPECT ("t1_func", "t1_func1"));
4260 }
4261
4262 /* Check that completion mode works at each prefix of the expected
4263 symbol name. */
4264 {
4265 static const char str[] = "function(int)";
4266 size_t len = strlen (str);
4267 std::string lookup;
4268
4269 for (size_t i = 1; i < len; i++)
4270 {
4271 lookup.assign (str, i);
4272 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4273 EXPECT ("function"));
4274 }
4275 }
4276
4277 /* While "w" is a prefix of both components, the match function
4278 should still only be called once. */
4279 {
4280 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4281 EXPECT ("w1::w2"));
4282 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4283 EXPECT ("w1::w2"));
4284 }
4285
4286 /* Same, with a "complicated" symbol. */
4287 {
4288 static const char str[] = Z_SYM_NAME;
4289 size_t len = strlen (str);
4290 std::string lookup;
4291
4292 for (size_t i = 1; i < len; i++)
4293 {
4294 lookup.assign (str, i);
4295 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4296 EXPECT (Z_SYM_NAME));
4297 }
4298 }
4299
4300 /* In FULL mode, an incomplete symbol doesn't match. */
4301 {
4302 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4303 {});
4304 }
4305
4306 /* A complete symbol with parameters matches any overload, since the
4307 index has no overload info. */
4308 {
4309 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4310 EXPECT ("std::zfunction", "std::zfunction2"));
4311 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4312 EXPECT ("std::zfunction", "std::zfunction2"));
4313 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4314 EXPECT ("std::zfunction", "std::zfunction2"));
4315 }
4316
4317 /* Check that whitespace is ignored appropriately. A symbol with a
4318 template argument list. */
4319 {
4320 static const char expected[] = "ns::foo<int>";
4321 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4322 EXPECT (expected));
4323 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4324 EXPECT (expected));
4325 }
4326
4327 /* Check that whitespace is ignored appropriately. A symbol with a
4328 template argument list that includes a pointer. */
4329 {
4330 static const char expected[] = "ns::foo<char*>";
4331 /* Try both completion and non-completion modes. */
4332 static const bool completion_mode[2] = {false, true};
4333 for (size_t i = 0; i < 2; i++)
4334 {
4335 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4336 completion_mode[i], EXPECT (expected));
4337 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4338 completion_mode[i], EXPECT (expected));
4339
4340 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4341 completion_mode[i], EXPECT (expected));
4342 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4343 completion_mode[i], EXPECT (expected));
4344 }
4345 }
4346
4347 {
4348 /* Check method qualifiers are ignored. */
4349 static const char expected[] = "ns::foo<char*>";
4350 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4351 symbol_name_match_type::FULL, true, EXPECT (expected));
4352 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4353 symbol_name_match_type::FULL, true, EXPECT (expected));
4354 CHECK_MATCH ("foo < char * > ( int ) const",
4355 symbol_name_match_type::WILD, true, EXPECT (expected));
4356 CHECK_MATCH ("foo < char * > ( int ) &&",
4357 symbol_name_match_type::WILD, true, EXPECT (expected));
4358 }
4359
4360 /* Test lookup names that don't match anything. */
4361 {
4362 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4363 {});
4364
4365 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4366 {});
4367 }
4368
4369 /* Some wild matching tests, exercising "(anonymous namespace)",
4370 which should not be confused with a parameter list. */
4371 {
4372 static const char *syms[] = {
4373 "A::B::C",
4374 "B::C",
4375 "C",
4376 "A :: B :: C ( int )",
4377 "B :: C ( int )",
4378 "C ( int )",
4379 };
4380
4381 for (const char *s : syms)
4382 {
4383 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4384 EXPECT ("(anonymous namespace)::A::B::C"));
4385 }
4386 }
4387
4388 {
4389 static const char expected[] = "ns2::tmpl<int>::foo2";
4390 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4391 EXPECT (expected));
4392 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4393 EXPECT (expected));
4394 }
4395
4396 SELF_CHECK (!any_mismatch);
4397
4398 #undef EXPECT
4399 #undef CHECK_MATCH
4400 }
4401
4402 static void
4403 run_test ()
4404 {
4405 test_mapped_index_find_name_component_bounds ();
4406 test_dw2_expand_symtabs_matching_symbol ();
4407 }
4408
4409 }} // namespace selftests::dw2_expand_symtabs_matching
4410
4411 #endif /* GDB_SELF_TEST */
4412
4413 /* If FILE_MATCHER is NULL or if PER_CU has
4414 dwarf2_per_cu_quick_data::MARK set (see
4415 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4416 EXPANSION_NOTIFY on it. */
4417
4418 static void
4419 dw2_expand_symtabs_matching_one
4420 (struct dwarf2_per_cu_data *per_cu,
4421 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4422 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4423 {
4424 if (file_matcher == NULL || per_cu->v.quick->mark)
4425 {
4426 bool symtab_was_null
4427 = (per_cu->v.quick->compunit_symtab == NULL);
4428
4429 dw2_instantiate_symtab (per_cu, false);
4430
4431 if (expansion_notify != NULL
4432 && symtab_was_null
4433 && per_cu->v.quick->compunit_symtab != NULL)
4434 expansion_notify (per_cu->v.quick->compunit_symtab);
4435 }
4436 }
4437
4438 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4439 matched, to expand corresponding CUs that were marked. IDX is the
4440 index of the symbol name that matched. */
4441
4442 static void
4443 dw2_expand_marked_cus
4444 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4445 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4446 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4447 search_domain kind)
4448 {
4449 offset_type *vec, vec_len, vec_idx;
4450 bool global_seen = false;
4451 mapped_index &index = *dwarf2_per_objfile->index_table;
4452
4453 vec = (offset_type *) (index.constant_pool
4454 + MAYBE_SWAP (index.symbol_table[idx].vec));
4455 vec_len = MAYBE_SWAP (vec[0]);
4456 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4457 {
4458 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4459 /* This value is only valid for index versions >= 7. */
4460 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4461 gdb_index_symbol_kind symbol_kind =
4462 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4463 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4464 /* Only check the symbol attributes if they're present.
4465 Indices prior to version 7 don't record them,
4466 and indices >= 7 may elide them for certain symbols
4467 (gold does this). */
4468 int attrs_valid =
4469 (index.version >= 7
4470 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4471
4472 /* Work around gold/15646. */
4473 if (attrs_valid)
4474 {
4475 if (!is_static && global_seen)
4476 continue;
4477 if (!is_static)
4478 global_seen = true;
4479 }
4480
4481 /* Only check the symbol's kind if it has one. */
4482 if (attrs_valid)
4483 {
4484 switch (kind)
4485 {
4486 case VARIABLES_DOMAIN:
4487 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4488 continue;
4489 break;
4490 case FUNCTIONS_DOMAIN:
4491 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4492 continue;
4493 break;
4494 case TYPES_DOMAIN:
4495 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4496 continue;
4497 break;
4498 case MODULES_DOMAIN:
4499 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4500 continue;
4501 break;
4502 default:
4503 break;
4504 }
4505 }
4506
4507 /* Don't crash on bad data. */
4508 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4509 + dwarf2_per_objfile->all_type_units.size ()))
4510 {
4511 complaint (_(".gdb_index entry has bad CU index"
4512 " [in module %s]"),
4513 objfile_name (dwarf2_per_objfile->objfile));
4514 continue;
4515 }
4516
4517 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4518 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4519 expansion_notify);
4520 }
4521 }
4522
4523 /* If FILE_MATCHER is non-NULL, set all the
4524 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4525 that match FILE_MATCHER. */
4526
4527 static void
4528 dw_expand_symtabs_matching_file_matcher
4529 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4530 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4531 {
4532 if (file_matcher == NULL)
4533 return;
4534
4535 objfile *const objfile = dwarf2_per_objfile->objfile;
4536
4537 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4538 htab_eq_pointer,
4539 NULL, xcalloc, xfree));
4540 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4541 htab_eq_pointer,
4542 NULL, xcalloc, xfree));
4543
4544 /* The rule is CUs specify all the files, including those used by
4545 any TU, so there's no need to scan TUs here. */
4546
4547 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4548 {
4549 QUIT;
4550
4551 per_cu->v.quick->mark = 0;
4552
4553 /* We only need to look at symtabs not already expanded. */
4554 if (per_cu->v.quick->compunit_symtab)
4555 continue;
4556
4557 quick_file_names *file_data = dw2_get_file_names (per_cu);
4558 if (file_data == NULL)
4559 continue;
4560
4561 if (htab_find (visited_not_found.get (), file_data) != NULL)
4562 continue;
4563 else if (htab_find (visited_found.get (), file_data) != NULL)
4564 {
4565 per_cu->v.quick->mark = 1;
4566 continue;
4567 }
4568
4569 for (int j = 0; j < file_data->num_file_names; ++j)
4570 {
4571 const char *this_real_name;
4572
4573 if (file_matcher (file_data->file_names[j], false))
4574 {
4575 per_cu->v.quick->mark = 1;
4576 break;
4577 }
4578
4579 /* Before we invoke realpath, which can get expensive when many
4580 files are involved, do a quick comparison of the basenames. */
4581 if (!basenames_may_differ
4582 && !file_matcher (lbasename (file_data->file_names[j]),
4583 true))
4584 continue;
4585
4586 this_real_name = dw2_get_real_path (objfile, file_data, j);
4587 if (file_matcher (this_real_name, false))
4588 {
4589 per_cu->v.quick->mark = 1;
4590 break;
4591 }
4592 }
4593
4594 void **slot = htab_find_slot (per_cu->v.quick->mark
4595 ? visited_found.get ()
4596 : visited_not_found.get (),
4597 file_data, INSERT);
4598 *slot = file_data;
4599 }
4600 }
4601
4602 static void
4603 dw2_expand_symtabs_matching
4604 (struct objfile *objfile,
4605 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4606 const lookup_name_info *lookup_name,
4607 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4608 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4609 enum search_domain kind)
4610 {
4611 struct dwarf2_per_objfile *dwarf2_per_objfile
4612 = get_dwarf2_per_objfile (objfile);
4613
4614 /* index_table is NULL if OBJF_READNOW. */
4615 if (!dwarf2_per_objfile->index_table)
4616 return;
4617
4618 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4619
4620 if (symbol_matcher == NULL && lookup_name == NULL)
4621 {
4622 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4623 {
4624 QUIT;
4625
4626 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4627 expansion_notify);
4628 }
4629 return;
4630 }
4631
4632 mapped_index &index = *dwarf2_per_objfile->index_table;
4633
4634 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4635 symbol_matcher,
4636 kind, [&] (offset_type idx)
4637 {
4638 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4639 expansion_notify, kind);
4640 return true;
4641 });
4642 }
4643
4644 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4645 symtab. */
4646
4647 static struct compunit_symtab *
4648 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4649 CORE_ADDR pc)
4650 {
4651 int i;
4652
4653 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4654 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4655 return cust;
4656
4657 if (cust->includes == NULL)
4658 return NULL;
4659
4660 for (i = 0; cust->includes[i]; ++i)
4661 {
4662 struct compunit_symtab *s = cust->includes[i];
4663
4664 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4665 if (s != NULL)
4666 return s;
4667 }
4668
4669 return NULL;
4670 }
4671
4672 static struct compunit_symtab *
4673 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4674 struct bound_minimal_symbol msymbol,
4675 CORE_ADDR pc,
4676 struct obj_section *section,
4677 int warn_if_readin)
4678 {
4679 struct dwarf2_per_cu_data *data;
4680 struct compunit_symtab *result;
4681
4682 if (!objfile->partial_symtabs->psymtabs_addrmap)
4683 return NULL;
4684
4685 CORE_ADDR baseaddr = objfile->text_section_offset ();
4686 data = (struct dwarf2_per_cu_data *) addrmap_find
4687 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4688 if (!data)
4689 return NULL;
4690
4691 if (warn_if_readin && data->v.quick->compunit_symtab)
4692 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4693 paddress (get_objfile_arch (objfile), pc));
4694
4695 result
4696 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4697 false),
4698 pc);
4699 gdb_assert (result != NULL);
4700 return result;
4701 }
4702
4703 static void
4704 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4705 void *data, int need_fullname)
4706 {
4707 struct dwarf2_per_objfile *dwarf2_per_objfile
4708 = get_dwarf2_per_objfile (objfile);
4709
4710 if (!dwarf2_per_objfile->filenames_cache)
4711 {
4712 dwarf2_per_objfile->filenames_cache.emplace ();
4713
4714 htab_up visited (htab_create_alloc (10,
4715 htab_hash_pointer, htab_eq_pointer,
4716 NULL, xcalloc, xfree));
4717
4718 /* The rule is CUs specify all the files, including those used
4719 by any TU, so there's no need to scan TUs here. We can
4720 ignore file names coming from already-expanded CUs. */
4721
4722 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4723 {
4724 if (per_cu->v.quick->compunit_symtab)
4725 {
4726 void **slot = htab_find_slot (visited.get (),
4727 per_cu->v.quick->file_names,
4728 INSERT);
4729
4730 *slot = per_cu->v.quick->file_names;
4731 }
4732 }
4733
4734 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4735 {
4736 /* We only need to look at symtabs not already expanded. */
4737 if (per_cu->v.quick->compunit_symtab)
4738 continue;
4739
4740 quick_file_names *file_data = dw2_get_file_names (per_cu);
4741 if (file_data == NULL)
4742 continue;
4743
4744 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4745 if (*slot)
4746 {
4747 /* Already visited. */
4748 continue;
4749 }
4750 *slot = file_data;
4751
4752 for (int j = 0; j < file_data->num_file_names; ++j)
4753 {
4754 const char *filename = file_data->file_names[j];
4755 dwarf2_per_objfile->filenames_cache->seen (filename);
4756 }
4757 }
4758 }
4759
4760 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4761 {
4762 gdb::unique_xmalloc_ptr<char> this_real_name;
4763
4764 if (need_fullname)
4765 this_real_name = gdb_realpath (filename);
4766 (*fun) (filename, this_real_name.get (), data);
4767 });
4768 }
4769
4770 static int
4771 dw2_has_symbols (struct objfile *objfile)
4772 {
4773 return 1;
4774 }
4775
4776 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4777 {
4778 dw2_has_symbols,
4779 dw2_find_last_source_symtab,
4780 dw2_forget_cached_source_info,
4781 dw2_map_symtabs_matching_filename,
4782 dw2_lookup_symbol,
4783 NULL,
4784 dw2_print_stats,
4785 dw2_dump,
4786 dw2_expand_symtabs_for_function,
4787 dw2_expand_all_symtabs,
4788 dw2_expand_symtabs_with_fullname,
4789 dw2_map_matching_symbols,
4790 dw2_expand_symtabs_matching,
4791 dw2_find_pc_sect_compunit_symtab,
4792 NULL,
4793 dw2_map_symbol_filenames
4794 };
4795
4796 /* DWARF-5 debug_names reader. */
4797
4798 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4799 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4800
4801 /* A helper function that reads the .debug_names section in SECTION
4802 and fills in MAP. FILENAME is the name of the file containing the
4803 section; it is used for error reporting.
4804
4805 Returns true if all went well, false otherwise. */
4806
4807 static bool
4808 read_debug_names_from_section (struct objfile *objfile,
4809 const char *filename,
4810 struct dwarf2_section_info *section,
4811 mapped_debug_names &map)
4812 {
4813 if (section->empty ())
4814 return false;
4815
4816 /* Older elfutils strip versions could keep the section in the main
4817 executable while splitting it for the separate debug info file. */
4818 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4819 return false;
4820
4821 section->read (objfile);
4822
4823 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4824
4825 const gdb_byte *addr = section->buffer;
4826
4827 bfd *const abfd = section->get_bfd_owner ();
4828
4829 unsigned int bytes_read;
4830 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4831 addr += bytes_read;
4832
4833 map.dwarf5_is_dwarf64 = bytes_read != 4;
4834 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4835 if (bytes_read + length != section->size)
4836 {
4837 /* There may be multiple per-CU indices. */
4838 warning (_("Section .debug_names in %s length %s does not match "
4839 "section length %s, ignoring .debug_names."),
4840 filename, plongest (bytes_read + length),
4841 pulongest (section->size));
4842 return false;
4843 }
4844
4845 /* The version number. */
4846 uint16_t version = read_2_bytes (abfd, addr);
4847 addr += 2;
4848 if (version != 5)
4849 {
4850 warning (_("Section .debug_names in %s has unsupported version %d, "
4851 "ignoring .debug_names."),
4852 filename, version);
4853 return false;
4854 }
4855
4856 /* Padding. */
4857 uint16_t padding = read_2_bytes (abfd, addr);
4858 addr += 2;
4859 if (padding != 0)
4860 {
4861 warning (_("Section .debug_names in %s has unsupported padding %d, "
4862 "ignoring .debug_names."),
4863 filename, padding);
4864 return false;
4865 }
4866
4867 /* comp_unit_count - The number of CUs in the CU list. */
4868 map.cu_count = read_4_bytes (abfd, addr);
4869 addr += 4;
4870
4871 /* local_type_unit_count - The number of TUs in the local TU
4872 list. */
4873 map.tu_count = read_4_bytes (abfd, addr);
4874 addr += 4;
4875
4876 /* foreign_type_unit_count - The number of TUs in the foreign TU
4877 list. */
4878 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4879 addr += 4;
4880 if (foreign_tu_count != 0)
4881 {
4882 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4883 "ignoring .debug_names."),
4884 filename, static_cast<unsigned long> (foreign_tu_count));
4885 return false;
4886 }
4887
4888 /* bucket_count - The number of hash buckets in the hash lookup
4889 table. */
4890 map.bucket_count = read_4_bytes (abfd, addr);
4891 addr += 4;
4892
4893 /* name_count - The number of unique names in the index. */
4894 map.name_count = read_4_bytes (abfd, addr);
4895 addr += 4;
4896
4897 /* abbrev_table_size - The size in bytes of the abbreviations
4898 table. */
4899 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4900 addr += 4;
4901
4902 /* augmentation_string_size - The size in bytes of the augmentation
4903 string. This value is rounded up to a multiple of 4. */
4904 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4905 addr += 4;
4906 map.augmentation_is_gdb = ((augmentation_string_size
4907 == sizeof (dwarf5_augmentation))
4908 && memcmp (addr, dwarf5_augmentation,
4909 sizeof (dwarf5_augmentation)) == 0);
4910 augmentation_string_size += (-augmentation_string_size) & 3;
4911 addr += augmentation_string_size;
4912
4913 /* List of CUs */
4914 map.cu_table_reordered = addr;
4915 addr += map.cu_count * map.offset_size;
4916
4917 /* List of Local TUs */
4918 map.tu_table_reordered = addr;
4919 addr += map.tu_count * map.offset_size;
4920
4921 /* Hash Lookup Table */
4922 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4923 addr += map.bucket_count * 4;
4924 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4925 addr += map.name_count * 4;
4926
4927 /* Name Table */
4928 map.name_table_string_offs_reordered = addr;
4929 addr += map.name_count * map.offset_size;
4930 map.name_table_entry_offs_reordered = addr;
4931 addr += map.name_count * map.offset_size;
4932
4933 const gdb_byte *abbrev_table_start = addr;
4934 for (;;)
4935 {
4936 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4937 addr += bytes_read;
4938 if (index_num == 0)
4939 break;
4940
4941 const auto insertpair
4942 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4943 if (!insertpair.second)
4944 {
4945 warning (_("Section .debug_names in %s has duplicate index %s, "
4946 "ignoring .debug_names."),
4947 filename, pulongest (index_num));
4948 return false;
4949 }
4950 mapped_debug_names::index_val &indexval = insertpair.first->second;
4951 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4952 addr += bytes_read;
4953
4954 for (;;)
4955 {
4956 mapped_debug_names::index_val::attr attr;
4957 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4958 addr += bytes_read;
4959 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4960 addr += bytes_read;
4961 if (attr.form == DW_FORM_implicit_const)
4962 {
4963 attr.implicit_const = read_signed_leb128 (abfd, addr,
4964 &bytes_read);
4965 addr += bytes_read;
4966 }
4967 if (attr.dw_idx == 0 && attr.form == 0)
4968 break;
4969 indexval.attr_vec.push_back (std::move (attr));
4970 }
4971 }
4972 if (addr != abbrev_table_start + abbrev_table_size)
4973 {
4974 warning (_("Section .debug_names in %s has abbreviation_table "
4975 "of size %s vs. written as %u, ignoring .debug_names."),
4976 filename, plongest (addr - abbrev_table_start),
4977 abbrev_table_size);
4978 return false;
4979 }
4980 map.entry_pool = addr;
4981
4982 return true;
4983 }
4984
4985 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4986 list. */
4987
4988 static void
4989 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4990 const mapped_debug_names &map,
4991 dwarf2_section_info &section,
4992 bool is_dwz)
4993 {
4994 sect_offset sect_off_prev;
4995 for (uint32_t i = 0; i <= map.cu_count; ++i)
4996 {
4997 sect_offset sect_off_next;
4998 if (i < map.cu_count)
4999 {
5000 sect_off_next
5001 = (sect_offset) (extract_unsigned_integer
5002 (map.cu_table_reordered + i * map.offset_size,
5003 map.offset_size,
5004 map.dwarf5_byte_order));
5005 }
5006 else
5007 sect_off_next = (sect_offset) section.size;
5008 if (i >= 1)
5009 {
5010 const ULONGEST length = sect_off_next - sect_off_prev;
5011 dwarf2_per_cu_data *per_cu
5012 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5013 sect_off_prev, length);
5014 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5015 }
5016 sect_off_prev = sect_off_next;
5017 }
5018 }
5019
5020 /* Read the CU list from the mapped index, and use it to create all
5021 the CU objects for this dwarf2_per_objfile. */
5022
5023 static void
5024 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5025 const mapped_debug_names &map,
5026 const mapped_debug_names &dwz_map)
5027 {
5028 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5029 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5030
5031 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5032 dwarf2_per_objfile->info,
5033 false /* is_dwz */);
5034
5035 if (dwz_map.cu_count == 0)
5036 return;
5037
5038 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5039 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5040 true /* is_dwz */);
5041 }
5042
5043 /* Read .debug_names. If everything went ok, initialize the "quick"
5044 elements of all the CUs and return true. Otherwise, return false. */
5045
5046 static bool
5047 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5048 {
5049 std::unique_ptr<mapped_debug_names> map
5050 (new mapped_debug_names (dwarf2_per_objfile));
5051 mapped_debug_names dwz_map (dwarf2_per_objfile);
5052 struct objfile *objfile = dwarf2_per_objfile->objfile;
5053
5054 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5055 &dwarf2_per_objfile->debug_names,
5056 *map))
5057 return false;
5058
5059 /* Don't use the index if it's empty. */
5060 if (map->name_count == 0)
5061 return false;
5062
5063 /* If there is a .dwz file, read it so we can get its CU list as
5064 well. */
5065 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5066 if (dwz != NULL)
5067 {
5068 if (!read_debug_names_from_section (objfile,
5069 bfd_get_filename (dwz->dwz_bfd.get ()),
5070 &dwz->debug_names, dwz_map))
5071 {
5072 warning (_("could not read '.debug_names' section from %s; skipping"),
5073 bfd_get_filename (dwz->dwz_bfd.get ()));
5074 return false;
5075 }
5076 }
5077
5078 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5079
5080 if (map->tu_count != 0)
5081 {
5082 /* We can only handle a single .debug_types when we have an
5083 index. */
5084 if (dwarf2_per_objfile->types.size () != 1)
5085 return false;
5086
5087 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5088
5089 create_signatured_type_table_from_debug_names
5090 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5091 }
5092
5093 create_addrmap_from_aranges (dwarf2_per_objfile,
5094 &dwarf2_per_objfile->debug_aranges);
5095
5096 dwarf2_per_objfile->debug_names_table = std::move (map);
5097 dwarf2_per_objfile->using_index = 1;
5098 dwarf2_per_objfile->quick_file_names_table =
5099 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5100
5101 return true;
5102 }
5103
5104 /* Type used to manage iterating over all CUs looking for a symbol for
5105 .debug_names. */
5106
5107 class dw2_debug_names_iterator
5108 {
5109 public:
5110 dw2_debug_names_iterator (const mapped_debug_names &map,
5111 gdb::optional<block_enum> block_index,
5112 domain_enum domain,
5113 const char *name)
5114 : m_map (map), m_block_index (block_index), m_domain (domain),
5115 m_addr (find_vec_in_debug_names (map, name))
5116 {}
5117
5118 dw2_debug_names_iterator (const mapped_debug_names &map,
5119 search_domain search, uint32_t namei)
5120 : m_map (map),
5121 m_search (search),
5122 m_addr (find_vec_in_debug_names (map, namei))
5123 {}
5124
5125 dw2_debug_names_iterator (const mapped_debug_names &map,
5126 block_enum block_index, domain_enum domain,
5127 uint32_t namei)
5128 : m_map (map), m_block_index (block_index), m_domain (domain),
5129 m_addr (find_vec_in_debug_names (map, namei))
5130 {}
5131
5132 /* Return the next matching CU or NULL if there are no more. */
5133 dwarf2_per_cu_data *next ();
5134
5135 private:
5136 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5137 const char *name);
5138 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5139 uint32_t namei);
5140
5141 /* The internalized form of .debug_names. */
5142 const mapped_debug_names &m_map;
5143
5144 /* If set, only look for symbols that match that block. Valid values are
5145 GLOBAL_BLOCK and STATIC_BLOCK. */
5146 const gdb::optional<block_enum> m_block_index;
5147
5148 /* The kind of symbol we're looking for. */
5149 const domain_enum m_domain = UNDEF_DOMAIN;
5150 const search_domain m_search = ALL_DOMAIN;
5151
5152 /* The list of CUs from the index entry of the symbol, or NULL if
5153 not found. */
5154 const gdb_byte *m_addr;
5155 };
5156
5157 const char *
5158 mapped_debug_names::namei_to_name (uint32_t namei) const
5159 {
5160 const ULONGEST namei_string_offs
5161 = extract_unsigned_integer ((name_table_string_offs_reordered
5162 + namei * offset_size),
5163 offset_size,
5164 dwarf5_byte_order);
5165 return read_indirect_string_at_offset (dwarf2_per_objfile,
5166 namei_string_offs);
5167 }
5168
5169 /* Find a slot in .debug_names for the object named NAME. If NAME is
5170 found, return pointer to its pool data. If NAME cannot be found,
5171 return NULL. */
5172
5173 const gdb_byte *
5174 dw2_debug_names_iterator::find_vec_in_debug_names
5175 (const mapped_debug_names &map, const char *name)
5176 {
5177 int (*cmp) (const char *, const char *);
5178
5179 gdb::unique_xmalloc_ptr<char> without_params;
5180 if (current_language->la_language == language_cplus
5181 || current_language->la_language == language_fortran
5182 || current_language->la_language == language_d)
5183 {
5184 /* NAME is already canonical. Drop any qualifiers as
5185 .debug_names does not contain any. */
5186
5187 if (strchr (name, '(') != NULL)
5188 {
5189 without_params = cp_remove_params (name);
5190 if (without_params != NULL)
5191 name = without_params.get ();
5192 }
5193 }
5194
5195 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5196
5197 const uint32_t full_hash = dwarf5_djb_hash (name);
5198 uint32_t namei
5199 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5200 (map.bucket_table_reordered
5201 + (full_hash % map.bucket_count)), 4,
5202 map.dwarf5_byte_order);
5203 if (namei == 0)
5204 return NULL;
5205 --namei;
5206 if (namei >= map.name_count)
5207 {
5208 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5209 "[in module %s]"),
5210 namei, map.name_count,
5211 objfile_name (map.dwarf2_per_objfile->objfile));
5212 return NULL;
5213 }
5214
5215 for (;;)
5216 {
5217 const uint32_t namei_full_hash
5218 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5219 (map.hash_table_reordered + namei), 4,
5220 map.dwarf5_byte_order);
5221 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5222 return NULL;
5223
5224 if (full_hash == namei_full_hash)
5225 {
5226 const char *const namei_string = map.namei_to_name (namei);
5227
5228 #if 0 /* An expensive sanity check. */
5229 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5230 {
5231 complaint (_("Wrong .debug_names hash for string at index %u "
5232 "[in module %s]"),
5233 namei, objfile_name (dwarf2_per_objfile->objfile));
5234 return NULL;
5235 }
5236 #endif
5237
5238 if (cmp (namei_string, name) == 0)
5239 {
5240 const ULONGEST namei_entry_offs
5241 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5242 + namei * map.offset_size),
5243 map.offset_size, map.dwarf5_byte_order);
5244 return map.entry_pool + namei_entry_offs;
5245 }
5246 }
5247
5248 ++namei;
5249 if (namei >= map.name_count)
5250 return NULL;
5251 }
5252 }
5253
5254 const gdb_byte *
5255 dw2_debug_names_iterator::find_vec_in_debug_names
5256 (const mapped_debug_names &map, uint32_t namei)
5257 {
5258 if (namei >= map.name_count)
5259 {
5260 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5261 "[in module %s]"),
5262 namei, map.name_count,
5263 objfile_name (map.dwarf2_per_objfile->objfile));
5264 return NULL;
5265 }
5266
5267 const ULONGEST namei_entry_offs
5268 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5269 + namei * map.offset_size),
5270 map.offset_size, map.dwarf5_byte_order);
5271 return map.entry_pool + namei_entry_offs;
5272 }
5273
5274 /* See dw2_debug_names_iterator. */
5275
5276 dwarf2_per_cu_data *
5277 dw2_debug_names_iterator::next ()
5278 {
5279 if (m_addr == NULL)
5280 return NULL;
5281
5282 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5283 struct objfile *objfile = dwarf2_per_objfile->objfile;
5284 bfd *const abfd = objfile->obfd;
5285
5286 again:
5287
5288 unsigned int bytes_read;
5289 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5290 m_addr += bytes_read;
5291 if (abbrev == 0)
5292 return NULL;
5293
5294 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5295 if (indexval_it == m_map.abbrev_map.cend ())
5296 {
5297 complaint (_("Wrong .debug_names undefined abbrev code %s "
5298 "[in module %s]"),
5299 pulongest (abbrev), objfile_name (objfile));
5300 return NULL;
5301 }
5302 const mapped_debug_names::index_val &indexval = indexval_it->second;
5303 enum class symbol_linkage {
5304 unknown,
5305 static_,
5306 extern_,
5307 } symbol_linkage_ = symbol_linkage::unknown;
5308 dwarf2_per_cu_data *per_cu = NULL;
5309 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5310 {
5311 ULONGEST ull;
5312 switch (attr.form)
5313 {
5314 case DW_FORM_implicit_const:
5315 ull = attr.implicit_const;
5316 break;
5317 case DW_FORM_flag_present:
5318 ull = 1;
5319 break;
5320 case DW_FORM_udata:
5321 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5322 m_addr += bytes_read;
5323 break;
5324 default:
5325 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5326 dwarf_form_name (attr.form),
5327 objfile_name (objfile));
5328 return NULL;
5329 }
5330 switch (attr.dw_idx)
5331 {
5332 case DW_IDX_compile_unit:
5333 /* Don't crash on bad data. */
5334 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5335 {
5336 complaint (_(".debug_names entry has bad CU index %s"
5337 " [in module %s]"),
5338 pulongest (ull),
5339 objfile_name (dwarf2_per_objfile->objfile));
5340 continue;
5341 }
5342 per_cu = dwarf2_per_objfile->get_cutu (ull);
5343 break;
5344 case DW_IDX_type_unit:
5345 /* Don't crash on bad data. */
5346 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5347 {
5348 complaint (_(".debug_names entry has bad TU index %s"
5349 " [in module %s]"),
5350 pulongest (ull),
5351 objfile_name (dwarf2_per_objfile->objfile));
5352 continue;
5353 }
5354 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5355 break;
5356 case DW_IDX_GNU_internal:
5357 if (!m_map.augmentation_is_gdb)
5358 break;
5359 symbol_linkage_ = symbol_linkage::static_;
5360 break;
5361 case DW_IDX_GNU_external:
5362 if (!m_map.augmentation_is_gdb)
5363 break;
5364 symbol_linkage_ = symbol_linkage::extern_;
5365 break;
5366 }
5367 }
5368
5369 /* Skip if already read in. */
5370 if (per_cu->v.quick->compunit_symtab)
5371 goto again;
5372
5373 /* Check static vs global. */
5374 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5375 {
5376 const bool want_static = *m_block_index == STATIC_BLOCK;
5377 const bool symbol_is_static =
5378 symbol_linkage_ == symbol_linkage::static_;
5379 if (want_static != symbol_is_static)
5380 goto again;
5381 }
5382
5383 /* Match dw2_symtab_iter_next, symbol_kind
5384 and debug_names::psymbol_tag. */
5385 switch (m_domain)
5386 {
5387 case VAR_DOMAIN:
5388 switch (indexval.dwarf_tag)
5389 {
5390 case DW_TAG_variable:
5391 case DW_TAG_subprogram:
5392 /* Some types are also in VAR_DOMAIN. */
5393 case DW_TAG_typedef:
5394 case DW_TAG_structure_type:
5395 break;
5396 default:
5397 goto again;
5398 }
5399 break;
5400 case STRUCT_DOMAIN:
5401 switch (indexval.dwarf_tag)
5402 {
5403 case DW_TAG_typedef:
5404 case DW_TAG_structure_type:
5405 break;
5406 default:
5407 goto again;
5408 }
5409 break;
5410 case LABEL_DOMAIN:
5411 switch (indexval.dwarf_tag)
5412 {
5413 case 0:
5414 case DW_TAG_variable:
5415 break;
5416 default:
5417 goto again;
5418 }
5419 break;
5420 case MODULE_DOMAIN:
5421 switch (indexval.dwarf_tag)
5422 {
5423 case DW_TAG_module:
5424 break;
5425 default:
5426 goto again;
5427 }
5428 break;
5429 default:
5430 break;
5431 }
5432
5433 /* Match dw2_expand_symtabs_matching, symbol_kind and
5434 debug_names::psymbol_tag. */
5435 switch (m_search)
5436 {
5437 case VARIABLES_DOMAIN:
5438 switch (indexval.dwarf_tag)
5439 {
5440 case DW_TAG_variable:
5441 break;
5442 default:
5443 goto again;
5444 }
5445 break;
5446 case FUNCTIONS_DOMAIN:
5447 switch (indexval.dwarf_tag)
5448 {
5449 case DW_TAG_subprogram:
5450 break;
5451 default:
5452 goto again;
5453 }
5454 break;
5455 case TYPES_DOMAIN:
5456 switch (indexval.dwarf_tag)
5457 {
5458 case DW_TAG_typedef:
5459 case DW_TAG_structure_type:
5460 break;
5461 default:
5462 goto again;
5463 }
5464 break;
5465 case MODULES_DOMAIN:
5466 switch (indexval.dwarf_tag)
5467 {
5468 case DW_TAG_module:
5469 break;
5470 default:
5471 goto again;
5472 }
5473 default:
5474 break;
5475 }
5476
5477 return per_cu;
5478 }
5479
5480 static struct compunit_symtab *
5481 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5482 const char *name, domain_enum domain)
5483 {
5484 struct dwarf2_per_objfile *dwarf2_per_objfile
5485 = get_dwarf2_per_objfile (objfile);
5486
5487 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5488 if (!mapp)
5489 {
5490 /* index is NULL if OBJF_READNOW. */
5491 return NULL;
5492 }
5493 const auto &map = *mapp;
5494
5495 dw2_debug_names_iterator iter (map, block_index, domain, name);
5496
5497 struct compunit_symtab *stab_best = NULL;
5498 struct dwarf2_per_cu_data *per_cu;
5499 while ((per_cu = iter.next ()) != NULL)
5500 {
5501 struct symbol *sym, *with_opaque = NULL;
5502 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5503 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5504 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5505
5506 sym = block_find_symbol (block, name, domain,
5507 block_find_non_opaque_type_preferred,
5508 &with_opaque);
5509
5510 /* Some caution must be observed with overloaded functions and
5511 methods, since the index will not contain any overload
5512 information (but NAME might contain it). */
5513
5514 if (sym != NULL
5515 && strcmp_iw (sym->search_name (), name) == 0)
5516 return stab;
5517 if (with_opaque != NULL
5518 && strcmp_iw (with_opaque->search_name (), name) == 0)
5519 stab_best = stab;
5520
5521 /* Keep looking through other CUs. */
5522 }
5523
5524 return stab_best;
5525 }
5526
5527 /* This dumps minimal information about .debug_names. It is called
5528 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5529 uses this to verify that .debug_names has been loaded. */
5530
5531 static void
5532 dw2_debug_names_dump (struct objfile *objfile)
5533 {
5534 struct dwarf2_per_objfile *dwarf2_per_objfile
5535 = get_dwarf2_per_objfile (objfile);
5536
5537 gdb_assert (dwarf2_per_objfile->using_index);
5538 printf_filtered (".debug_names:");
5539 if (dwarf2_per_objfile->debug_names_table)
5540 printf_filtered (" exists\n");
5541 else
5542 printf_filtered (" faked for \"readnow\"\n");
5543 printf_filtered ("\n");
5544 }
5545
5546 static void
5547 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5548 const char *func_name)
5549 {
5550 struct dwarf2_per_objfile *dwarf2_per_objfile
5551 = get_dwarf2_per_objfile (objfile);
5552
5553 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5554 if (dwarf2_per_objfile->debug_names_table)
5555 {
5556 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5557
5558 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5559
5560 struct dwarf2_per_cu_data *per_cu;
5561 while ((per_cu = iter.next ()) != NULL)
5562 dw2_instantiate_symtab (per_cu, false);
5563 }
5564 }
5565
5566 static void
5567 dw2_debug_names_map_matching_symbols
5568 (struct objfile *objfile,
5569 const lookup_name_info &name, domain_enum domain,
5570 int global,
5571 gdb::function_view<symbol_found_callback_ftype> callback,
5572 symbol_compare_ftype *ordered_compare)
5573 {
5574 struct dwarf2_per_objfile *dwarf2_per_objfile
5575 = get_dwarf2_per_objfile (objfile);
5576
5577 /* debug_names_table is NULL if OBJF_READNOW. */
5578 if (!dwarf2_per_objfile->debug_names_table)
5579 return;
5580
5581 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5582 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5583
5584 const char *match_name = name.ada ().lookup_name ().c_str ();
5585 auto matcher = [&] (const char *symname)
5586 {
5587 if (ordered_compare == nullptr)
5588 return true;
5589 return ordered_compare (symname, match_name) == 0;
5590 };
5591
5592 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5593 [&] (offset_type namei)
5594 {
5595 /* The name was matched, now expand corresponding CUs that were
5596 marked. */
5597 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5598
5599 struct dwarf2_per_cu_data *per_cu;
5600 while ((per_cu = iter.next ()) != NULL)
5601 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5602 return true;
5603 });
5604
5605 /* It's a shame we couldn't do this inside the
5606 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5607 that have already been expanded. Instead, this loop matches what
5608 the psymtab code does. */
5609 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5610 {
5611 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5612 if (cust != nullptr)
5613 {
5614 const struct block *block
5615 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5616 if (!iterate_over_symbols_terminated (block, name,
5617 domain, callback))
5618 break;
5619 }
5620 }
5621 }
5622
5623 static void
5624 dw2_debug_names_expand_symtabs_matching
5625 (struct objfile *objfile,
5626 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5627 const lookup_name_info *lookup_name,
5628 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5629 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5630 enum search_domain kind)
5631 {
5632 struct dwarf2_per_objfile *dwarf2_per_objfile
5633 = get_dwarf2_per_objfile (objfile);
5634
5635 /* debug_names_table is NULL if OBJF_READNOW. */
5636 if (!dwarf2_per_objfile->debug_names_table)
5637 return;
5638
5639 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5640
5641 if (symbol_matcher == NULL && lookup_name == NULL)
5642 {
5643 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5644 {
5645 QUIT;
5646
5647 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5648 expansion_notify);
5649 }
5650 return;
5651 }
5652
5653 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5654
5655 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5656 symbol_matcher,
5657 kind, [&] (offset_type namei)
5658 {
5659 /* The name was matched, now expand corresponding CUs that were
5660 marked. */
5661 dw2_debug_names_iterator iter (map, kind, namei);
5662
5663 struct dwarf2_per_cu_data *per_cu;
5664 while ((per_cu = iter.next ()) != NULL)
5665 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5666 expansion_notify);
5667 return true;
5668 });
5669 }
5670
5671 const struct quick_symbol_functions dwarf2_debug_names_functions =
5672 {
5673 dw2_has_symbols,
5674 dw2_find_last_source_symtab,
5675 dw2_forget_cached_source_info,
5676 dw2_map_symtabs_matching_filename,
5677 dw2_debug_names_lookup_symbol,
5678 NULL,
5679 dw2_print_stats,
5680 dw2_debug_names_dump,
5681 dw2_debug_names_expand_symtabs_for_function,
5682 dw2_expand_all_symtabs,
5683 dw2_expand_symtabs_with_fullname,
5684 dw2_debug_names_map_matching_symbols,
5685 dw2_debug_names_expand_symtabs_matching,
5686 dw2_find_pc_sect_compunit_symtab,
5687 NULL,
5688 dw2_map_symbol_filenames
5689 };
5690
5691 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5692 to either a dwarf2_per_objfile or dwz_file object. */
5693
5694 template <typename T>
5695 static gdb::array_view<const gdb_byte>
5696 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5697 {
5698 dwarf2_section_info *section = &section_owner->gdb_index;
5699
5700 if (section->empty ())
5701 return {};
5702
5703 /* Older elfutils strip versions could keep the section in the main
5704 executable while splitting it for the separate debug info file. */
5705 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5706 return {};
5707
5708 section->read (obj);
5709
5710 /* dwarf2_section_info::size is a bfd_size_type, while
5711 gdb::array_view works with size_t. On 32-bit hosts, with
5712 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5713 is 32-bit. So we need an explicit narrowing conversion here.
5714 This is fine, because it's impossible to allocate or mmap an
5715 array/buffer larger than what size_t can represent. */
5716 return gdb::make_array_view (section->buffer, section->size);
5717 }
5718
5719 /* Lookup the index cache for the contents of the index associated to
5720 DWARF2_OBJ. */
5721
5722 static gdb::array_view<const gdb_byte>
5723 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5724 {
5725 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5726 if (build_id == nullptr)
5727 return {};
5728
5729 return global_index_cache.lookup_gdb_index (build_id,
5730 &dwarf2_obj->index_cache_res);
5731 }
5732
5733 /* Same as the above, but for DWZ. */
5734
5735 static gdb::array_view<const gdb_byte>
5736 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5737 {
5738 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5739 if (build_id == nullptr)
5740 return {};
5741
5742 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5743 }
5744
5745 /* See symfile.h. */
5746
5747 bool
5748 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5749 {
5750 struct dwarf2_per_objfile *dwarf2_per_objfile
5751 = get_dwarf2_per_objfile (objfile);
5752
5753 /* If we're about to read full symbols, don't bother with the
5754 indices. In this case we also don't care if some other debug
5755 format is making psymtabs, because they are all about to be
5756 expanded anyway. */
5757 if ((objfile->flags & OBJF_READNOW))
5758 {
5759 dwarf2_per_objfile->using_index = 1;
5760 create_all_comp_units (dwarf2_per_objfile);
5761 create_all_type_units (dwarf2_per_objfile);
5762 dwarf2_per_objfile->quick_file_names_table
5763 = create_quick_file_names_table
5764 (dwarf2_per_objfile->all_comp_units.size ());
5765
5766 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5767 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5768 {
5769 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5770
5771 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5772 struct dwarf2_per_cu_quick_data);
5773 }
5774
5775 /* Return 1 so that gdb sees the "quick" functions. However,
5776 these functions will be no-ops because we will have expanded
5777 all symtabs. */
5778 *index_kind = dw_index_kind::GDB_INDEX;
5779 return true;
5780 }
5781
5782 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5783 {
5784 *index_kind = dw_index_kind::DEBUG_NAMES;
5785 return true;
5786 }
5787
5788 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5789 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5790 get_gdb_index_contents_from_section<dwz_file>))
5791 {
5792 *index_kind = dw_index_kind::GDB_INDEX;
5793 return true;
5794 }
5795
5796 /* ... otherwise, try to find the index in the index cache. */
5797 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5798 get_gdb_index_contents_from_cache,
5799 get_gdb_index_contents_from_cache_dwz))
5800 {
5801 global_index_cache.hit ();
5802 *index_kind = dw_index_kind::GDB_INDEX;
5803 return true;
5804 }
5805
5806 global_index_cache.miss ();
5807 return false;
5808 }
5809
5810 \f
5811
5812 /* Build a partial symbol table. */
5813
5814 void
5815 dwarf2_build_psymtabs (struct objfile *objfile)
5816 {
5817 struct dwarf2_per_objfile *dwarf2_per_objfile
5818 = get_dwarf2_per_objfile (objfile);
5819
5820 init_psymbol_list (objfile, 1024);
5821
5822 try
5823 {
5824 /* This isn't really ideal: all the data we allocate on the
5825 objfile's obstack is still uselessly kept around. However,
5826 freeing it seems unsafe. */
5827 psymtab_discarder psymtabs (objfile);
5828 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5829 psymtabs.keep ();
5830
5831 /* (maybe) store an index in the cache. */
5832 global_index_cache.store (dwarf2_per_objfile);
5833 }
5834 catch (const gdb_exception_error &except)
5835 {
5836 exception_print (gdb_stderr, except);
5837 }
5838 }
5839
5840 /* Find the base address of the compilation unit for range lists and
5841 location lists. It will normally be specified by DW_AT_low_pc.
5842 In DWARF-3 draft 4, the base address could be overridden by
5843 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5844 compilation units with discontinuous ranges. */
5845
5846 static void
5847 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5848 {
5849 struct attribute *attr;
5850
5851 cu->base_address.reset ();
5852
5853 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5854 if (attr != nullptr)
5855 cu->base_address = attr->value_as_address ();
5856 else
5857 {
5858 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5859 if (attr != nullptr)
5860 cu->base_address = attr->value_as_address ();
5861 }
5862 }
5863
5864 /* Helper function that returns the proper abbrev section for
5865 THIS_CU. */
5866
5867 static struct dwarf2_section_info *
5868 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5869 {
5870 struct dwarf2_section_info *abbrev;
5871 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5872
5873 if (this_cu->is_dwz)
5874 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5875 else
5876 abbrev = &dwarf2_per_objfile->abbrev;
5877
5878 return abbrev;
5879 }
5880
5881 /* Fetch the abbreviation table offset from a comp or type unit header. */
5882
5883 static sect_offset
5884 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5885 struct dwarf2_section_info *section,
5886 sect_offset sect_off)
5887 {
5888 bfd *abfd = section->get_bfd_owner ();
5889 const gdb_byte *info_ptr;
5890 unsigned int initial_length_size, offset_size;
5891 uint16_t version;
5892
5893 section->read (dwarf2_per_objfile->objfile);
5894 info_ptr = section->buffer + to_underlying (sect_off);
5895 read_initial_length (abfd, info_ptr, &initial_length_size);
5896 offset_size = initial_length_size == 4 ? 4 : 8;
5897 info_ptr += initial_length_size;
5898
5899 version = read_2_bytes (abfd, info_ptr);
5900 info_ptr += 2;
5901 if (version >= 5)
5902 {
5903 /* Skip unit type and address size. */
5904 info_ptr += 2;
5905 }
5906
5907 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5908 }
5909
5910 /* A partial symtab that is used only for include files. */
5911 struct dwarf2_include_psymtab : public partial_symtab
5912 {
5913 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5914 : partial_symtab (filename, objfile)
5915 {
5916 }
5917
5918 void read_symtab (struct objfile *objfile) override
5919 {
5920 expand_psymtab (objfile);
5921 }
5922
5923 void expand_psymtab (struct objfile *objfile) override
5924 {
5925 if (m_readin)
5926 return;
5927 /* It's an include file, no symbols to read for it.
5928 Everything is in the parent symtab. */
5929 expand_dependencies (objfile);
5930 m_readin = true;
5931 }
5932
5933 bool readin_p () const override
5934 {
5935 return m_readin;
5936 }
5937
5938 struct compunit_symtab *get_compunit_symtab () const override
5939 {
5940 return nullptr;
5941 }
5942
5943 private:
5944
5945 bool m_readin = false;
5946 };
5947
5948 /* Allocate a new partial symtab for file named NAME and mark this new
5949 partial symtab as being an include of PST. */
5950
5951 static void
5952 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5953 struct objfile *objfile)
5954 {
5955 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
5956
5957 if (!IS_ABSOLUTE_PATH (subpst->filename))
5958 {
5959 /* It shares objfile->objfile_obstack. */
5960 subpst->dirname = pst->dirname;
5961 }
5962
5963 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
5964 subpst->dependencies[0] = pst;
5965 subpst->number_of_dependencies = 1;
5966 }
5967
5968 /* Read the Line Number Program data and extract the list of files
5969 included by the source file represented by PST. Build an include
5970 partial symtab for each of these included files. */
5971
5972 static void
5973 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5974 struct die_info *die,
5975 dwarf2_psymtab *pst)
5976 {
5977 line_header_up lh;
5978 struct attribute *attr;
5979
5980 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5981 if (attr != nullptr)
5982 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5983 if (lh == NULL)
5984 return; /* No linetable, so no includes. */
5985
5986 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5987 that we pass in the raw text_low here; that is ok because we're
5988 only decoding the line table to make include partial symtabs, and
5989 so the addresses aren't really used. */
5990 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5991 pst->raw_text_low (), 1);
5992 }
5993
5994 static hashval_t
5995 hash_signatured_type (const void *item)
5996 {
5997 const struct signatured_type *sig_type
5998 = (const struct signatured_type *) item;
5999
6000 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6001 return sig_type->signature;
6002 }
6003
6004 static int
6005 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6006 {
6007 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6008 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6009
6010 return lhs->signature == rhs->signature;
6011 }
6012
6013 /* Allocate a hash table for signatured types. */
6014
6015 static htab_up
6016 allocate_signatured_type_table ()
6017 {
6018 return htab_up (htab_create_alloc (41,
6019 hash_signatured_type,
6020 eq_signatured_type,
6021 NULL, xcalloc, xfree));
6022 }
6023
6024 /* A helper function to add a signatured type CU to a table. */
6025
6026 static int
6027 add_signatured_type_cu_to_table (void **slot, void *datum)
6028 {
6029 struct signatured_type *sigt = (struct signatured_type *) *slot;
6030 std::vector<signatured_type *> *all_type_units
6031 = (std::vector<signatured_type *> *) datum;
6032
6033 all_type_units->push_back (sigt);
6034
6035 return 1;
6036 }
6037
6038 /* A helper for create_debug_types_hash_table. Read types from SECTION
6039 and fill them into TYPES_HTAB. It will process only type units,
6040 therefore DW_UT_type. */
6041
6042 static void
6043 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6044 struct dwo_file *dwo_file,
6045 dwarf2_section_info *section, htab_up &types_htab,
6046 rcuh_kind section_kind)
6047 {
6048 struct objfile *objfile = dwarf2_per_objfile->objfile;
6049 struct dwarf2_section_info *abbrev_section;
6050 bfd *abfd;
6051 const gdb_byte *info_ptr, *end_ptr;
6052
6053 abbrev_section = (dwo_file != NULL
6054 ? &dwo_file->sections.abbrev
6055 : &dwarf2_per_objfile->abbrev);
6056
6057 if (dwarf_read_debug)
6058 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6059 section->get_name (),
6060 abbrev_section->get_file_name ());
6061
6062 section->read (objfile);
6063 info_ptr = section->buffer;
6064
6065 if (info_ptr == NULL)
6066 return;
6067
6068 /* We can't set abfd until now because the section may be empty or
6069 not present, in which case the bfd is unknown. */
6070 abfd = section->get_bfd_owner ();
6071
6072 /* We don't use cutu_reader here because we don't need to read
6073 any dies: the signature is in the header. */
6074
6075 end_ptr = info_ptr + section->size;
6076 while (info_ptr < end_ptr)
6077 {
6078 struct signatured_type *sig_type;
6079 struct dwo_unit *dwo_tu;
6080 void **slot;
6081 const gdb_byte *ptr = info_ptr;
6082 struct comp_unit_head header;
6083 unsigned int length;
6084
6085 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6086
6087 /* Initialize it due to a false compiler warning. */
6088 header.signature = -1;
6089 header.type_cu_offset_in_tu = (cu_offset) -1;
6090
6091 /* We need to read the type's signature in order to build the hash
6092 table, but we don't need anything else just yet. */
6093
6094 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6095 abbrev_section, ptr, section_kind);
6096
6097 length = header.get_length ();
6098
6099 /* Skip dummy type units. */
6100 if (ptr >= info_ptr + length
6101 || peek_abbrev_code (abfd, ptr) == 0
6102 || header.unit_type != DW_UT_type)
6103 {
6104 info_ptr += length;
6105 continue;
6106 }
6107
6108 if (types_htab == NULL)
6109 {
6110 if (dwo_file)
6111 types_htab = allocate_dwo_unit_table ();
6112 else
6113 types_htab = allocate_signatured_type_table ();
6114 }
6115
6116 if (dwo_file)
6117 {
6118 sig_type = NULL;
6119 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6120 struct dwo_unit);
6121 dwo_tu->dwo_file = dwo_file;
6122 dwo_tu->signature = header.signature;
6123 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6124 dwo_tu->section = section;
6125 dwo_tu->sect_off = sect_off;
6126 dwo_tu->length = length;
6127 }
6128 else
6129 {
6130 /* N.B.: type_offset is not usable if this type uses a DWO file.
6131 The real type_offset is in the DWO file. */
6132 dwo_tu = NULL;
6133 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6134 struct signatured_type);
6135 sig_type->signature = header.signature;
6136 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6137 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6138 sig_type->per_cu.is_debug_types = 1;
6139 sig_type->per_cu.section = section;
6140 sig_type->per_cu.sect_off = sect_off;
6141 sig_type->per_cu.length = length;
6142 }
6143
6144 slot = htab_find_slot (types_htab.get (),
6145 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6146 INSERT);
6147 gdb_assert (slot != NULL);
6148 if (*slot != NULL)
6149 {
6150 sect_offset dup_sect_off;
6151
6152 if (dwo_file)
6153 {
6154 const struct dwo_unit *dup_tu
6155 = (const struct dwo_unit *) *slot;
6156
6157 dup_sect_off = dup_tu->sect_off;
6158 }
6159 else
6160 {
6161 const struct signatured_type *dup_tu
6162 = (const struct signatured_type *) *slot;
6163
6164 dup_sect_off = dup_tu->per_cu.sect_off;
6165 }
6166
6167 complaint (_("debug type entry at offset %s is duplicate to"
6168 " the entry at offset %s, signature %s"),
6169 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6170 hex_string (header.signature));
6171 }
6172 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6173
6174 if (dwarf_read_debug > 1)
6175 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6176 sect_offset_str (sect_off),
6177 hex_string (header.signature));
6178
6179 info_ptr += length;
6180 }
6181 }
6182
6183 /* Create the hash table of all entries in the .debug_types
6184 (or .debug_types.dwo) section(s).
6185 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6186 otherwise it is NULL.
6187
6188 The result is a pointer to the hash table or NULL if there are no types.
6189
6190 Note: This function processes DWO files only, not DWP files. */
6191
6192 static void
6193 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6194 struct dwo_file *dwo_file,
6195 gdb::array_view<dwarf2_section_info> type_sections,
6196 htab_up &types_htab)
6197 {
6198 for (dwarf2_section_info &section : type_sections)
6199 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6200 types_htab, rcuh_kind::TYPE);
6201 }
6202
6203 /* Create the hash table of all entries in the .debug_types section,
6204 and initialize all_type_units.
6205 The result is zero if there is an error (e.g. missing .debug_types section),
6206 otherwise non-zero. */
6207
6208 static int
6209 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6210 {
6211 htab_up types_htab;
6212
6213 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6214 &dwarf2_per_objfile->info, types_htab,
6215 rcuh_kind::COMPILE);
6216 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6217 dwarf2_per_objfile->types, types_htab);
6218 if (types_htab == NULL)
6219 {
6220 dwarf2_per_objfile->signatured_types = NULL;
6221 return 0;
6222 }
6223
6224 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6225
6226 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6227 dwarf2_per_objfile->all_type_units.reserve
6228 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6229
6230 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6231 add_signatured_type_cu_to_table,
6232 &dwarf2_per_objfile->all_type_units);
6233
6234 return 1;
6235 }
6236
6237 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6238 If SLOT is non-NULL, it is the entry to use in the hash table.
6239 Otherwise we find one. */
6240
6241 static struct signatured_type *
6242 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6243 void **slot)
6244 {
6245 struct objfile *objfile = dwarf2_per_objfile->objfile;
6246
6247 if (dwarf2_per_objfile->all_type_units.size ()
6248 == dwarf2_per_objfile->all_type_units.capacity ())
6249 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6250
6251 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6252 struct signatured_type);
6253
6254 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6255 sig_type->signature = sig;
6256 sig_type->per_cu.is_debug_types = 1;
6257 if (dwarf2_per_objfile->using_index)
6258 {
6259 sig_type->per_cu.v.quick =
6260 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6261 struct dwarf2_per_cu_quick_data);
6262 }
6263
6264 if (slot == NULL)
6265 {
6266 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6267 sig_type, INSERT);
6268 }
6269 gdb_assert (*slot == NULL);
6270 *slot = sig_type;
6271 /* The rest of sig_type must be filled in by the caller. */
6272 return sig_type;
6273 }
6274
6275 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6276 Fill in SIG_ENTRY with DWO_ENTRY. */
6277
6278 static void
6279 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6280 struct signatured_type *sig_entry,
6281 struct dwo_unit *dwo_entry)
6282 {
6283 /* Make sure we're not clobbering something we don't expect to. */
6284 gdb_assert (! sig_entry->per_cu.queued);
6285 gdb_assert (sig_entry->per_cu.cu == NULL);
6286 if (dwarf2_per_objfile->using_index)
6287 {
6288 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6289 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6290 }
6291 else
6292 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6293 gdb_assert (sig_entry->signature == dwo_entry->signature);
6294 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6295 gdb_assert (sig_entry->type_unit_group == NULL);
6296 gdb_assert (sig_entry->dwo_unit == NULL);
6297
6298 sig_entry->per_cu.section = dwo_entry->section;
6299 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6300 sig_entry->per_cu.length = dwo_entry->length;
6301 sig_entry->per_cu.reading_dwo_directly = 1;
6302 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6303 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6304 sig_entry->dwo_unit = dwo_entry;
6305 }
6306
6307 /* Subroutine of lookup_signatured_type.
6308 If we haven't read the TU yet, create the signatured_type data structure
6309 for a TU to be read in directly from a DWO file, bypassing the stub.
6310 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6311 using .gdb_index, then when reading a CU we want to stay in the DWO file
6312 containing that CU. Otherwise we could end up reading several other DWO
6313 files (due to comdat folding) to process the transitive closure of all the
6314 mentioned TUs, and that can be slow. The current DWO file will have every
6315 type signature that it needs.
6316 We only do this for .gdb_index because in the psymtab case we already have
6317 to read all the DWOs to build the type unit groups. */
6318
6319 static struct signatured_type *
6320 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6321 {
6322 struct dwarf2_per_objfile *dwarf2_per_objfile
6323 = cu->per_cu->dwarf2_per_objfile;
6324 struct dwo_file *dwo_file;
6325 struct dwo_unit find_dwo_entry, *dwo_entry;
6326 struct signatured_type find_sig_entry, *sig_entry;
6327 void **slot;
6328
6329 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6330
6331 /* If TU skeletons have been removed then we may not have read in any
6332 TUs yet. */
6333 if (dwarf2_per_objfile->signatured_types == NULL)
6334 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6335
6336 /* We only ever need to read in one copy of a signatured type.
6337 Use the global signatured_types array to do our own comdat-folding
6338 of types. If this is the first time we're reading this TU, and
6339 the TU has an entry in .gdb_index, replace the recorded data from
6340 .gdb_index with this TU. */
6341
6342 find_sig_entry.signature = sig;
6343 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6344 &find_sig_entry, INSERT);
6345 sig_entry = (struct signatured_type *) *slot;
6346
6347 /* We can get here with the TU already read, *or* in the process of being
6348 read. Don't reassign the global entry to point to this DWO if that's
6349 the case. Also note that if the TU is already being read, it may not
6350 have come from a DWO, the program may be a mix of Fission-compiled
6351 code and non-Fission-compiled code. */
6352
6353 /* Have we already tried to read this TU?
6354 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6355 needn't exist in the global table yet). */
6356 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6357 return sig_entry;
6358
6359 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6360 dwo_unit of the TU itself. */
6361 dwo_file = cu->dwo_unit->dwo_file;
6362
6363 /* Ok, this is the first time we're reading this TU. */
6364 if (dwo_file->tus == NULL)
6365 return NULL;
6366 find_dwo_entry.signature = sig;
6367 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6368 &find_dwo_entry);
6369 if (dwo_entry == NULL)
6370 return NULL;
6371
6372 /* If the global table doesn't have an entry for this TU, add one. */
6373 if (sig_entry == NULL)
6374 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6375
6376 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6377 sig_entry->per_cu.tu_read = 1;
6378 return sig_entry;
6379 }
6380
6381 /* Subroutine of lookup_signatured_type.
6382 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6383 then try the DWP file. If the TU stub (skeleton) has been removed then
6384 it won't be in .gdb_index. */
6385
6386 static struct signatured_type *
6387 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6388 {
6389 struct dwarf2_per_objfile *dwarf2_per_objfile
6390 = cu->per_cu->dwarf2_per_objfile;
6391 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6392 struct dwo_unit *dwo_entry;
6393 struct signatured_type find_sig_entry, *sig_entry;
6394 void **slot;
6395
6396 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6397 gdb_assert (dwp_file != NULL);
6398
6399 /* If TU skeletons have been removed then we may not have read in any
6400 TUs yet. */
6401 if (dwarf2_per_objfile->signatured_types == NULL)
6402 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6403
6404 find_sig_entry.signature = sig;
6405 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6406 &find_sig_entry, INSERT);
6407 sig_entry = (struct signatured_type *) *slot;
6408
6409 /* Have we already tried to read this TU?
6410 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6411 needn't exist in the global table yet). */
6412 if (sig_entry != NULL)
6413 return sig_entry;
6414
6415 if (dwp_file->tus == NULL)
6416 return NULL;
6417 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6418 sig, 1 /* is_debug_types */);
6419 if (dwo_entry == NULL)
6420 return NULL;
6421
6422 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6423 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6424
6425 return sig_entry;
6426 }
6427
6428 /* Lookup a signature based type for DW_FORM_ref_sig8.
6429 Returns NULL if signature SIG is not present in the table.
6430 It is up to the caller to complain about this. */
6431
6432 static struct signatured_type *
6433 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6434 {
6435 struct dwarf2_per_objfile *dwarf2_per_objfile
6436 = cu->per_cu->dwarf2_per_objfile;
6437
6438 if (cu->dwo_unit
6439 && dwarf2_per_objfile->using_index)
6440 {
6441 /* We're in a DWO/DWP file, and we're using .gdb_index.
6442 These cases require special processing. */
6443 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6444 return lookup_dwo_signatured_type (cu, sig);
6445 else
6446 return lookup_dwp_signatured_type (cu, sig);
6447 }
6448 else
6449 {
6450 struct signatured_type find_entry, *entry;
6451
6452 if (dwarf2_per_objfile->signatured_types == NULL)
6453 return NULL;
6454 find_entry.signature = sig;
6455 entry = ((struct signatured_type *)
6456 htab_find (dwarf2_per_objfile->signatured_types.get (),
6457 &find_entry));
6458 return entry;
6459 }
6460 }
6461
6462 /* Low level DIE reading support. */
6463
6464 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6465
6466 static void
6467 init_cu_die_reader (struct die_reader_specs *reader,
6468 struct dwarf2_cu *cu,
6469 struct dwarf2_section_info *section,
6470 struct dwo_file *dwo_file,
6471 struct abbrev_table *abbrev_table)
6472 {
6473 gdb_assert (section->readin && section->buffer != NULL);
6474 reader->abfd = section->get_bfd_owner ();
6475 reader->cu = cu;
6476 reader->dwo_file = dwo_file;
6477 reader->die_section = section;
6478 reader->buffer = section->buffer;
6479 reader->buffer_end = section->buffer + section->size;
6480 reader->abbrev_table = abbrev_table;
6481 }
6482
6483 /* Subroutine of cutu_reader to simplify it.
6484 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6485 There's just a lot of work to do, and cutu_reader is big enough
6486 already.
6487
6488 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6489 from it to the DIE in the DWO. If NULL we are skipping the stub.
6490 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6491 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6492 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6493 STUB_COMP_DIR may be non-NULL.
6494 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6495 are filled in with the info of the DIE from the DWO file.
6496 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6497 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6498 kept around for at least as long as *RESULT_READER.
6499
6500 The result is non-zero if a valid (non-dummy) DIE was found. */
6501
6502 static int
6503 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6504 struct dwo_unit *dwo_unit,
6505 struct die_info *stub_comp_unit_die,
6506 const char *stub_comp_dir,
6507 struct die_reader_specs *result_reader,
6508 const gdb_byte **result_info_ptr,
6509 struct die_info **result_comp_unit_die,
6510 abbrev_table_up *result_dwo_abbrev_table)
6511 {
6512 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6513 struct objfile *objfile = dwarf2_per_objfile->objfile;
6514 struct dwarf2_cu *cu = this_cu->cu;
6515 bfd *abfd;
6516 const gdb_byte *begin_info_ptr, *info_ptr;
6517 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6518 int i,num_extra_attrs;
6519 struct dwarf2_section_info *dwo_abbrev_section;
6520 struct die_info *comp_unit_die;
6521
6522 /* At most one of these may be provided. */
6523 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6524
6525 /* These attributes aren't processed until later:
6526 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6527 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6528 referenced later. However, these attributes are found in the stub
6529 which we won't have later. In order to not impose this complication
6530 on the rest of the code, we read them here and copy them to the
6531 DWO CU/TU die. */
6532
6533 stmt_list = NULL;
6534 low_pc = NULL;
6535 high_pc = NULL;
6536 ranges = NULL;
6537 comp_dir = NULL;
6538
6539 if (stub_comp_unit_die != NULL)
6540 {
6541 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6542 DWO file. */
6543 if (! this_cu->is_debug_types)
6544 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6545 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6546 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6547 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6548 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6549
6550 cu->addr_base = stub_comp_unit_die->addr_base ();
6551
6552 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6553 here (if needed). We need the value before we can process
6554 DW_AT_ranges. */
6555 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6556 }
6557 else if (stub_comp_dir != NULL)
6558 {
6559 /* Reconstruct the comp_dir attribute to simplify the code below. */
6560 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6561 comp_dir->name = DW_AT_comp_dir;
6562 comp_dir->form = DW_FORM_string;
6563 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6564 DW_STRING (comp_dir) = stub_comp_dir;
6565 }
6566
6567 /* Set up for reading the DWO CU/TU. */
6568 cu->dwo_unit = dwo_unit;
6569 dwarf2_section_info *section = dwo_unit->section;
6570 section->read (objfile);
6571 abfd = section->get_bfd_owner ();
6572 begin_info_ptr = info_ptr = (section->buffer
6573 + to_underlying (dwo_unit->sect_off));
6574 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6575
6576 if (this_cu->is_debug_types)
6577 {
6578 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6579
6580 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6581 &cu->header, section,
6582 dwo_abbrev_section,
6583 info_ptr, rcuh_kind::TYPE);
6584 /* This is not an assert because it can be caused by bad debug info. */
6585 if (sig_type->signature != cu->header.signature)
6586 {
6587 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6588 " TU at offset %s [in module %s]"),
6589 hex_string (sig_type->signature),
6590 hex_string (cu->header.signature),
6591 sect_offset_str (dwo_unit->sect_off),
6592 bfd_get_filename (abfd));
6593 }
6594 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6595 /* For DWOs coming from DWP files, we don't know the CU length
6596 nor the type's offset in the TU until now. */
6597 dwo_unit->length = cu->header.get_length ();
6598 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6599
6600 /* Establish the type offset that can be used to lookup the type.
6601 For DWO files, we don't know it until now. */
6602 sig_type->type_offset_in_section
6603 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6604 }
6605 else
6606 {
6607 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6608 &cu->header, section,
6609 dwo_abbrev_section,
6610 info_ptr, rcuh_kind::COMPILE);
6611 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6612 /* For DWOs coming from DWP files, we don't know the CU length
6613 until now. */
6614 dwo_unit->length = cu->header.get_length ();
6615 }
6616
6617 *result_dwo_abbrev_table
6618 = abbrev_table::read (objfile, dwo_abbrev_section,
6619 cu->header.abbrev_sect_off);
6620 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6621 result_dwo_abbrev_table->get ());
6622
6623 /* Read in the die, but leave space to copy over the attributes
6624 from the stub. This has the benefit of simplifying the rest of
6625 the code - all the work to maintain the illusion of a single
6626 DW_TAG_{compile,type}_unit DIE is done here. */
6627 num_extra_attrs = ((stmt_list != NULL)
6628 + (low_pc != NULL)
6629 + (high_pc != NULL)
6630 + (ranges != NULL)
6631 + (comp_dir != NULL));
6632 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6633 num_extra_attrs);
6634
6635 /* Copy over the attributes from the stub to the DIE we just read in. */
6636 comp_unit_die = *result_comp_unit_die;
6637 i = comp_unit_die->num_attrs;
6638 if (stmt_list != NULL)
6639 comp_unit_die->attrs[i++] = *stmt_list;
6640 if (low_pc != NULL)
6641 comp_unit_die->attrs[i++] = *low_pc;
6642 if (high_pc != NULL)
6643 comp_unit_die->attrs[i++] = *high_pc;
6644 if (ranges != NULL)
6645 comp_unit_die->attrs[i++] = *ranges;
6646 if (comp_dir != NULL)
6647 comp_unit_die->attrs[i++] = *comp_dir;
6648 comp_unit_die->num_attrs += num_extra_attrs;
6649
6650 if (dwarf_die_debug)
6651 {
6652 fprintf_unfiltered (gdb_stdlog,
6653 "Read die from %s@0x%x of %s:\n",
6654 section->get_name (),
6655 (unsigned) (begin_info_ptr - section->buffer),
6656 bfd_get_filename (abfd));
6657 dump_die (comp_unit_die, dwarf_die_debug);
6658 }
6659
6660 /* Skip dummy compilation units. */
6661 if (info_ptr >= begin_info_ptr + dwo_unit->length
6662 || peek_abbrev_code (abfd, info_ptr) == 0)
6663 return 0;
6664
6665 *result_info_ptr = info_ptr;
6666 return 1;
6667 }
6668
6669 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6670 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6671 signature is part of the header. */
6672 static gdb::optional<ULONGEST>
6673 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6674 {
6675 if (cu->header.version >= 5)
6676 return cu->header.signature;
6677 struct attribute *attr;
6678 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6679 if (attr == nullptr)
6680 return gdb::optional<ULONGEST> ();
6681 return DW_UNSND (attr);
6682 }
6683
6684 /* Subroutine of cutu_reader to simplify it.
6685 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6686 Returns NULL if the specified DWO unit cannot be found. */
6687
6688 static struct dwo_unit *
6689 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6690 struct die_info *comp_unit_die,
6691 const char *dwo_name)
6692 {
6693 struct dwarf2_cu *cu = this_cu->cu;
6694 struct dwo_unit *dwo_unit;
6695 const char *comp_dir;
6696
6697 gdb_assert (cu != NULL);
6698
6699 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6700 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6701 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6702
6703 if (this_cu->is_debug_types)
6704 {
6705 struct signatured_type *sig_type;
6706
6707 /* Since this_cu is the first member of struct signatured_type,
6708 we can go from a pointer to one to a pointer to the other. */
6709 sig_type = (struct signatured_type *) this_cu;
6710 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6711 }
6712 else
6713 {
6714 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6715 if (!signature.has_value ())
6716 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6717 " [in module %s]"),
6718 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6719 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6720 *signature);
6721 }
6722
6723 return dwo_unit;
6724 }
6725
6726 /* Subroutine of cutu_reader to simplify it.
6727 See it for a description of the parameters.
6728 Read a TU directly from a DWO file, bypassing the stub. */
6729
6730 void
6731 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6732 int use_existing_cu)
6733 {
6734 struct signatured_type *sig_type;
6735
6736 /* Verify we can do the following downcast, and that we have the
6737 data we need. */
6738 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6739 sig_type = (struct signatured_type *) this_cu;
6740 gdb_assert (sig_type->dwo_unit != NULL);
6741
6742 if (use_existing_cu && this_cu->cu != NULL)
6743 {
6744 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6745 /* There's no need to do the rereading_dwo_cu handling that
6746 cutu_reader does since we don't read the stub. */
6747 }
6748 else
6749 {
6750 /* If !use_existing_cu, this_cu->cu must be NULL. */
6751 gdb_assert (this_cu->cu == NULL);
6752 m_new_cu.reset (new dwarf2_cu (this_cu));
6753 }
6754
6755 /* A future optimization, if needed, would be to use an existing
6756 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6757 could share abbrev tables. */
6758
6759 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6760 NULL /* stub_comp_unit_die */,
6761 sig_type->dwo_unit->dwo_file->comp_dir,
6762 this, &info_ptr,
6763 &comp_unit_die,
6764 &m_dwo_abbrev_table) == 0)
6765 {
6766 /* Dummy die. */
6767 dummy_p = true;
6768 }
6769 }
6770
6771 /* Initialize a CU (or TU) and read its DIEs.
6772 If the CU defers to a DWO file, read the DWO file as well.
6773
6774 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6775 Otherwise the table specified in the comp unit header is read in and used.
6776 This is an optimization for when we already have the abbrev table.
6777
6778 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6779 Otherwise, a new CU is allocated with xmalloc. */
6780
6781 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6782 struct abbrev_table *abbrev_table,
6783 int use_existing_cu,
6784 bool skip_partial)
6785 : die_reader_specs {},
6786 m_this_cu (this_cu)
6787 {
6788 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6789 struct objfile *objfile = dwarf2_per_objfile->objfile;
6790 struct dwarf2_section_info *section = this_cu->section;
6791 bfd *abfd = section->get_bfd_owner ();
6792 struct dwarf2_cu *cu;
6793 const gdb_byte *begin_info_ptr;
6794 struct signatured_type *sig_type = NULL;
6795 struct dwarf2_section_info *abbrev_section;
6796 /* Non-zero if CU currently points to a DWO file and we need to
6797 reread it. When this happens we need to reread the skeleton die
6798 before we can reread the DWO file (this only applies to CUs, not TUs). */
6799 int rereading_dwo_cu = 0;
6800
6801 if (dwarf_die_debug)
6802 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6803 this_cu->is_debug_types ? "type" : "comp",
6804 sect_offset_str (this_cu->sect_off));
6805
6806 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6807 file (instead of going through the stub), short-circuit all of this. */
6808 if (this_cu->reading_dwo_directly)
6809 {
6810 /* Narrow down the scope of possibilities to have to understand. */
6811 gdb_assert (this_cu->is_debug_types);
6812 gdb_assert (abbrev_table == NULL);
6813 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6814 return;
6815 }
6816
6817 /* This is cheap if the section is already read in. */
6818 section->read (objfile);
6819
6820 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6821
6822 abbrev_section = get_abbrev_section_for_cu (this_cu);
6823
6824 if (use_existing_cu && this_cu->cu != NULL)
6825 {
6826 cu = this_cu->cu;
6827 /* If this CU is from a DWO file we need to start over, we need to
6828 refetch the attributes from the skeleton CU.
6829 This could be optimized by retrieving those attributes from when we
6830 were here the first time: the previous comp_unit_die was stored in
6831 comp_unit_obstack. But there's no data yet that we need this
6832 optimization. */
6833 if (cu->dwo_unit != NULL)
6834 rereading_dwo_cu = 1;
6835 }
6836 else
6837 {
6838 /* If !use_existing_cu, this_cu->cu must be NULL. */
6839 gdb_assert (this_cu->cu == NULL);
6840 m_new_cu.reset (new dwarf2_cu (this_cu));
6841 cu = m_new_cu.get ();
6842 }
6843
6844 /* Get the header. */
6845 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6846 {
6847 /* We already have the header, there's no need to read it in again. */
6848 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6849 }
6850 else
6851 {
6852 if (this_cu->is_debug_types)
6853 {
6854 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6855 &cu->header, section,
6856 abbrev_section, info_ptr,
6857 rcuh_kind::TYPE);
6858
6859 /* Since per_cu is the first member of struct signatured_type,
6860 we can go from a pointer to one to a pointer to the other. */
6861 sig_type = (struct signatured_type *) this_cu;
6862 gdb_assert (sig_type->signature == cu->header.signature);
6863 gdb_assert (sig_type->type_offset_in_tu
6864 == cu->header.type_cu_offset_in_tu);
6865 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6866
6867 /* LENGTH has not been set yet for type units if we're
6868 using .gdb_index. */
6869 this_cu->length = cu->header.get_length ();
6870
6871 /* Establish the type offset that can be used to lookup the type. */
6872 sig_type->type_offset_in_section =
6873 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6874
6875 this_cu->dwarf_version = cu->header.version;
6876 }
6877 else
6878 {
6879 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6880 &cu->header, section,
6881 abbrev_section,
6882 info_ptr,
6883 rcuh_kind::COMPILE);
6884
6885 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6886 gdb_assert (this_cu->length == cu->header.get_length ());
6887 this_cu->dwarf_version = cu->header.version;
6888 }
6889 }
6890
6891 /* Skip dummy compilation units. */
6892 if (info_ptr >= begin_info_ptr + this_cu->length
6893 || peek_abbrev_code (abfd, info_ptr) == 0)
6894 {
6895 dummy_p = true;
6896 return;
6897 }
6898
6899 /* If we don't have them yet, read the abbrevs for this compilation unit.
6900 And if we need to read them now, make sure they're freed when we're
6901 done. */
6902 if (abbrev_table != NULL)
6903 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6904 else
6905 {
6906 m_abbrev_table_holder
6907 = abbrev_table::read (objfile, abbrev_section,
6908 cu->header.abbrev_sect_off);
6909 abbrev_table = m_abbrev_table_holder.get ();
6910 }
6911
6912 /* Read the top level CU/TU die. */
6913 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6914 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6915
6916 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6917 {
6918 dummy_p = true;
6919 return;
6920 }
6921
6922 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6923 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6924 table from the DWO file and pass the ownership over to us. It will be
6925 referenced from READER, so we must make sure to free it after we're done
6926 with READER.
6927
6928 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6929 DWO CU, that this test will fail (the attribute will not be present). */
6930 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6931 if (dwo_name != nullptr)
6932 {
6933 struct dwo_unit *dwo_unit;
6934 struct die_info *dwo_comp_unit_die;
6935
6936 if (comp_unit_die->has_children)
6937 {
6938 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6939 " has children (offset %s) [in module %s]"),
6940 sect_offset_str (this_cu->sect_off),
6941 bfd_get_filename (abfd));
6942 }
6943 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6944 if (dwo_unit != NULL)
6945 {
6946 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6947 comp_unit_die, NULL,
6948 this, &info_ptr,
6949 &dwo_comp_unit_die,
6950 &m_dwo_abbrev_table) == 0)
6951 {
6952 /* Dummy die. */
6953 dummy_p = true;
6954 return;
6955 }
6956 comp_unit_die = dwo_comp_unit_die;
6957 }
6958 else
6959 {
6960 /* Yikes, we couldn't find the rest of the DIE, we only have
6961 the stub. A complaint has already been logged. There's
6962 not much more we can do except pass on the stub DIE to
6963 die_reader_func. We don't want to throw an error on bad
6964 debug info. */
6965 }
6966 }
6967 }
6968
6969 void
6970 cutu_reader::keep ()
6971 {
6972 /* Done, clean up. */
6973 gdb_assert (!dummy_p);
6974 if (m_new_cu != NULL)
6975 {
6976 struct dwarf2_per_objfile *dwarf2_per_objfile
6977 = m_this_cu->dwarf2_per_objfile;
6978 /* Link this CU into read_in_chain. */
6979 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6980 dwarf2_per_objfile->read_in_chain = m_this_cu;
6981 /* The chain owns it now. */
6982 m_new_cu.release ();
6983 }
6984 }
6985
6986 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6987 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6988 assumed to have already done the lookup to find the DWO file).
6989
6990 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6991 THIS_CU->is_debug_types, but nothing else.
6992
6993 We fill in THIS_CU->length.
6994
6995 THIS_CU->cu is always freed when done.
6996 This is done in order to not leave THIS_CU->cu in a state where we have
6997 to care whether it refers to the "main" CU or the DWO CU.
6998
6999 When parent_cu is passed, it is used to provide a default value for
7000 str_offsets_base and addr_base from the parent. */
7001
7002 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7003 struct dwarf2_cu *parent_cu,
7004 struct dwo_file *dwo_file)
7005 : die_reader_specs {},
7006 m_this_cu (this_cu)
7007 {
7008 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7009 struct objfile *objfile = dwarf2_per_objfile->objfile;
7010 struct dwarf2_section_info *section = this_cu->section;
7011 bfd *abfd = section->get_bfd_owner ();
7012 struct dwarf2_section_info *abbrev_section;
7013 const gdb_byte *begin_info_ptr, *info_ptr;
7014
7015 if (dwarf_die_debug)
7016 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7017 this_cu->is_debug_types ? "type" : "comp",
7018 sect_offset_str (this_cu->sect_off));
7019
7020 gdb_assert (this_cu->cu == NULL);
7021
7022 abbrev_section = (dwo_file != NULL
7023 ? &dwo_file->sections.abbrev
7024 : get_abbrev_section_for_cu (this_cu));
7025
7026 /* This is cheap if the section is already read in. */
7027 section->read (objfile);
7028
7029 m_new_cu.reset (new dwarf2_cu (this_cu));
7030
7031 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7032 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7033 &m_new_cu->header, section,
7034 abbrev_section, info_ptr,
7035 (this_cu->is_debug_types
7036 ? rcuh_kind::TYPE
7037 : rcuh_kind::COMPILE));
7038
7039 if (parent_cu != nullptr)
7040 {
7041 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7042 m_new_cu->addr_base = parent_cu->addr_base;
7043 }
7044 this_cu->length = m_new_cu->header.get_length ();
7045
7046 /* Skip dummy compilation units. */
7047 if (info_ptr >= begin_info_ptr + this_cu->length
7048 || peek_abbrev_code (abfd, info_ptr) == 0)
7049 {
7050 dummy_p = true;
7051 return;
7052 }
7053
7054 m_abbrev_table_holder
7055 = abbrev_table::read (objfile, abbrev_section,
7056 m_new_cu->header.abbrev_sect_off);
7057
7058 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7059 m_abbrev_table_holder.get ());
7060 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7061 }
7062
7063 \f
7064 /* Type Unit Groups.
7065
7066 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7067 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7068 so that all types coming from the same compilation (.o file) are grouped
7069 together. A future step could be to put the types in the same symtab as
7070 the CU the types ultimately came from. */
7071
7072 static hashval_t
7073 hash_type_unit_group (const void *item)
7074 {
7075 const struct type_unit_group *tu_group
7076 = (const struct type_unit_group *) item;
7077
7078 return hash_stmt_list_entry (&tu_group->hash);
7079 }
7080
7081 static int
7082 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7083 {
7084 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7085 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7086
7087 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7088 }
7089
7090 /* Allocate a hash table for type unit groups. */
7091
7092 static htab_up
7093 allocate_type_unit_groups_table ()
7094 {
7095 return htab_up (htab_create_alloc (3,
7096 hash_type_unit_group,
7097 eq_type_unit_group,
7098 NULL, xcalloc, xfree));
7099 }
7100
7101 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7102 partial symtabs. We combine several TUs per psymtab to not let the size
7103 of any one psymtab grow too big. */
7104 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7105 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7106
7107 /* Helper routine for get_type_unit_group.
7108 Create the type_unit_group object used to hold one or more TUs. */
7109
7110 static struct type_unit_group *
7111 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7112 {
7113 struct dwarf2_per_objfile *dwarf2_per_objfile
7114 = cu->per_cu->dwarf2_per_objfile;
7115 struct objfile *objfile = dwarf2_per_objfile->objfile;
7116 struct dwarf2_per_cu_data *per_cu;
7117 struct type_unit_group *tu_group;
7118
7119 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7120 struct type_unit_group);
7121 per_cu = &tu_group->per_cu;
7122 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7123
7124 if (dwarf2_per_objfile->using_index)
7125 {
7126 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7127 struct dwarf2_per_cu_quick_data);
7128 }
7129 else
7130 {
7131 unsigned int line_offset = to_underlying (line_offset_struct);
7132 dwarf2_psymtab *pst;
7133 std::string name;
7134
7135 /* Give the symtab a useful name for debug purposes. */
7136 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7137 name = string_printf ("<type_units_%d>",
7138 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7139 else
7140 name = string_printf ("<type_units_at_0x%x>", line_offset);
7141
7142 pst = create_partial_symtab (per_cu, name.c_str ());
7143 pst->anonymous = true;
7144 }
7145
7146 tu_group->hash.dwo_unit = cu->dwo_unit;
7147 tu_group->hash.line_sect_off = line_offset_struct;
7148
7149 return tu_group;
7150 }
7151
7152 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7153 STMT_LIST is a DW_AT_stmt_list attribute. */
7154
7155 static struct type_unit_group *
7156 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7157 {
7158 struct dwarf2_per_objfile *dwarf2_per_objfile
7159 = cu->per_cu->dwarf2_per_objfile;
7160 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7161 struct type_unit_group *tu_group;
7162 void **slot;
7163 unsigned int line_offset;
7164 struct type_unit_group type_unit_group_for_lookup;
7165
7166 if (dwarf2_per_objfile->type_unit_groups == NULL)
7167 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
7168
7169 /* Do we need to create a new group, or can we use an existing one? */
7170
7171 if (stmt_list)
7172 {
7173 line_offset = DW_UNSND (stmt_list);
7174 ++tu_stats->nr_symtab_sharers;
7175 }
7176 else
7177 {
7178 /* Ugh, no stmt_list. Rare, but we have to handle it.
7179 We can do various things here like create one group per TU or
7180 spread them over multiple groups to split up the expansion work.
7181 To avoid worst case scenarios (too many groups or too large groups)
7182 we, umm, group them in bunches. */
7183 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7184 | (tu_stats->nr_stmt_less_type_units
7185 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7186 ++tu_stats->nr_stmt_less_type_units;
7187 }
7188
7189 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7190 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7191 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7192 &type_unit_group_for_lookup, INSERT);
7193 if (*slot != NULL)
7194 {
7195 tu_group = (struct type_unit_group *) *slot;
7196 gdb_assert (tu_group != NULL);
7197 }
7198 else
7199 {
7200 sect_offset line_offset_struct = (sect_offset) line_offset;
7201 tu_group = create_type_unit_group (cu, line_offset_struct);
7202 *slot = tu_group;
7203 ++tu_stats->nr_symtabs;
7204 }
7205
7206 return tu_group;
7207 }
7208 \f
7209 /* Partial symbol tables. */
7210
7211 /* Create a psymtab named NAME and assign it to PER_CU.
7212
7213 The caller must fill in the following details:
7214 dirname, textlow, texthigh. */
7215
7216 static dwarf2_psymtab *
7217 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7218 {
7219 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7220 dwarf2_psymtab *pst;
7221
7222 pst = new dwarf2_psymtab (name, objfile, per_cu);
7223
7224 pst->psymtabs_addrmap_supported = true;
7225
7226 /* This is the glue that links PST into GDB's symbol API. */
7227 per_cu->v.psymtab = pst;
7228
7229 return pst;
7230 }
7231
7232 /* DIE reader function for process_psymtab_comp_unit. */
7233
7234 static void
7235 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7236 const gdb_byte *info_ptr,
7237 struct die_info *comp_unit_die,
7238 enum language pretend_language)
7239 {
7240 struct dwarf2_cu *cu = reader->cu;
7241 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7242 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7243 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7244 CORE_ADDR baseaddr;
7245 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7246 dwarf2_psymtab *pst;
7247 enum pc_bounds_kind cu_bounds_kind;
7248 const char *filename;
7249
7250 gdb_assert (! per_cu->is_debug_types);
7251
7252 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7253
7254 /* Allocate a new partial symbol table structure. */
7255 gdb::unique_xmalloc_ptr<char> debug_filename;
7256 static const char artificial[] = "<artificial>";
7257 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7258 if (filename == NULL)
7259 filename = "";
7260 else if (strcmp (filename, artificial) == 0)
7261 {
7262 debug_filename.reset (concat (artificial, "@",
7263 sect_offset_str (per_cu->sect_off),
7264 (char *) NULL));
7265 filename = debug_filename.get ();
7266 }
7267
7268 pst = create_partial_symtab (per_cu, filename);
7269
7270 /* This must be done before calling dwarf2_build_include_psymtabs. */
7271 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7272
7273 baseaddr = objfile->text_section_offset ();
7274
7275 dwarf2_find_base_address (comp_unit_die, cu);
7276
7277 /* Possibly set the default values of LOWPC and HIGHPC from
7278 `DW_AT_ranges'. */
7279 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7280 &best_highpc, cu, pst);
7281 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7282 {
7283 CORE_ADDR low
7284 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7285 - baseaddr);
7286 CORE_ADDR high
7287 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7288 - baseaddr - 1);
7289 /* Store the contiguous range if it is not empty; it can be
7290 empty for CUs with no code. */
7291 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7292 low, high, pst);
7293 }
7294
7295 /* Check if comp unit has_children.
7296 If so, read the rest of the partial symbols from this comp unit.
7297 If not, there's no more debug_info for this comp unit. */
7298 if (comp_unit_die->has_children)
7299 {
7300 struct partial_die_info *first_die;
7301 CORE_ADDR lowpc, highpc;
7302
7303 lowpc = ((CORE_ADDR) -1);
7304 highpc = ((CORE_ADDR) 0);
7305
7306 first_die = load_partial_dies (reader, info_ptr, 1);
7307
7308 scan_partial_symbols (first_die, &lowpc, &highpc,
7309 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7310
7311 /* If we didn't find a lowpc, set it to highpc to avoid
7312 complaints from `maint check'. */
7313 if (lowpc == ((CORE_ADDR) -1))
7314 lowpc = highpc;
7315
7316 /* If the compilation unit didn't have an explicit address range,
7317 then use the information extracted from its child dies. */
7318 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7319 {
7320 best_lowpc = lowpc;
7321 best_highpc = highpc;
7322 }
7323 }
7324 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7325 best_lowpc + baseaddr)
7326 - baseaddr);
7327 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7328 best_highpc + baseaddr)
7329 - baseaddr);
7330
7331 end_psymtab_common (objfile, pst);
7332
7333 if (!cu->per_cu->imported_symtabs_empty ())
7334 {
7335 int i;
7336 int len = cu->per_cu->imported_symtabs_size ();
7337
7338 /* Fill in 'dependencies' here; we fill in 'users' in a
7339 post-pass. */
7340 pst->number_of_dependencies = len;
7341 pst->dependencies
7342 = objfile->partial_symtabs->allocate_dependencies (len);
7343 for (i = 0; i < len; ++i)
7344 {
7345 pst->dependencies[i]
7346 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7347 }
7348
7349 cu->per_cu->imported_symtabs_free ();
7350 }
7351
7352 /* Get the list of files included in the current compilation unit,
7353 and build a psymtab for each of them. */
7354 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7355
7356 if (dwarf_read_debug)
7357 fprintf_unfiltered (gdb_stdlog,
7358 "Psymtab for %s unit @%s: %s - %s"
7359 ", %d global, %d static syms\n",
7360 per_cu->is_debug_types ? "type" : "comp",
7361 sect_offset_str (per_cu->sect_off),
7362 paddress (gdbarch, pst->text_low (objfile)),
7363 paddress (gdbarch, pst->text_high (objfile)),
7364 pst->n_global_syms, pst->n_static_syms);
7365 }
7366
7367 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7368 Process compilation unit THIS_CU for a psymtab. */
7369
7370 static void
7371 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7372 bool want_partial_unit,
7373 enum language pretend_language)
7374 {
7375 /* If this compilation unit was already read in, free the
7376 cached copy in order to read it in again. This is
7377 necessary because we skipped some symbols when we first
7378 read in the compilation unit (see load_partial_dies).
7379 This problem could be avoided, but the benefit is unclear. */
7380 if (this_cu->cu != NULL)
7381 free_one_cached_comp_unit (this_cu);
7382
7383 cutu_reader reader (this_cu, NULL, 0, false);
7384
7385 switch (reader.comp_unit_die->tag)
7386 {
7387 case DW_TAG_compile_unit:
7388 this_cu->unit_type = DW_UT_compile;
7389 break;
7390 case DW_TAG_partial_unit:
7391 this_cu->unit_type = DW_UT_partial;
7392 break;
7393 default:
7394 abort ();
7395 }
7396
7397 if (reader.dummy_p)
7398 {
7399 /* Nothing. */
7400 }
7401 else if (this_cu->is_debug_types)
7402 build_type_psymtabs_reader (&reader, reader.info_ptr,
7403 reader.comp_unit_die);
7404 else if (want_partial_unit
7405 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7406 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7407 reader.comp_unit_die,
7408 pretend_language);
7409
7410 this_cu->lang = this_cu->cu->language;
7411
7412 /* Age out any secondary CUs. */
7413 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7414 }
7415
7416 /* Reader function for build_type_psymtabs. */
7417
7418 static void
7419 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7420 const gdb_byte *info_ptr,
7421 struct die_info *type_unit_die)
7422 {
7423 struct dwarf2_per_objfile *dwarf2_per_objfile
7424 = reader->cu->per_cu->dwarf2_per_objfile;
7425 struct objfile *objfile = dwarf2_per_objfile->objfile;
7426 struct dwarf2_cu *cu = reader->cu;
7427 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7428 struct signatured_type *sig_type;
7429 struct type_unit_group *tu_group;
7430 struct attribute *attr;
7431 struct partial_die_info *first_die;
7432 CORE_ADDR lowpc, highpc;
7433 dwarf2_psymtab *pst;
7434
7435 gdb_assert (per_cu->is_debug_types);
7436 sig_type = (struct signatured_type *) per_cu;
7437
7438 if (! type_unit_die->has_children)
7439 return;
7440
7441 attr = type_unit_die->attr (DW_AT_stmt_list);
7442 tu_group = get_type_unit_group (cu, attr);
7443
7444 if (tu_group->tus == nullptr)
7445 tu_group->tus = new std::vector<signatured_type *>;
7446 tu_group->tus->push_back (sig_type);
7447
7448 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7449 pst = create_partial_symtab (per_cu, "");
7450 pst->anonymous = true;
7451
7452 first_die = load_partial_dies (reader, info_ptr, 1);
7453
7454 lowpc = (CORE_ADDR) -1;
7455 highpc = (CORE_ADDR) 0;
7456 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7457
7458 end_psymtab_common (objfile, pst);
7459 }
7460
7461 /* Struct used to sort TUs by their abbreviation table offset. */
7462
7463 struct tu_abbrev_offset
7464 {
7465 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7466 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7467 {}
7468
7469 signatured_type *sig_type;
7470 sect_offset abbrev_offset;
7471 };
7472
7473 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7474
7475 static bool
7476 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7477 const struct tu_abbrev_offset &b)
7478 {
7479 return a.abbrev_offset < b.abbrev_offset;
7480 }
7481
7482 /* Efficiently read all the type units.
7483 This does the bulk of the work for build_type_psymtabs.
7484
7485 The efficiency is because we sort TUs by the abbrev table they use and
7486 only read each abbrev table once. In one program there are 200K TUs
7487 sharing 8K abbrev tables.
7488
7489 The main purpose of this function is to support building the
7490 dwarf2_per_objfile->type_unit_groups table.
7491 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7492 can collapse the search space by grouping them by stmt_list.
7493 The savings can be significant, in the same program from above the 200K TUs
7494 share 8K stmt_list tables.
7495
7496 FUNC is expected to call get_type_unit_group, which will create the
7497 struct type_unit_group if necessary and add it to
7498 dwarf2_per_objfile->type_unit_groups. */
7499
7500 static void
7501 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7502 {
7503 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7504 abbrev_table_up abbrev_table;
7505 sect_offset abbrev_offset;
7506
7507 /* It's up to the caller to not call us multiple times. */
7508 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7509
7510 if (dwarf2_per_objfile->all_type_units.empty ())
7511 return;
7512
7513 /* TUs typically share abbrev tables, and there can be way more TUs than
7514 abbrev tables. Sort by abbrev table to reduce the number of times we
7515 read each abbrev table in.
7516 Alternatives are to punt or to maintain a cache of abbrev tables.
7517 This is simpler and efficient enough for now.
7518
7519 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7520 symtab to use). Typically TUs with the same abbrev offset have the same
7521 stmt_list value too so in practice this should work well.
7522
7523 The basic algorithm here is:
7524
7525 sort TUs by abbrev table
7526 for each TU with same abbrev table:
7527 read abbrev table if first user
7528 read TU top level DIE
7529 [IWBN if DWO skeletons had DW_AT_stmt_list]
7530 call FUNC */
7531
7532 if (dwarf_read_debug)
7533 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7534
7535 /* Sort in a separate table to maintain the order of all_type_units
7536 for .gdb_index: TU indices directly index all_type_units. */
7537 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7538 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7539
7540 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7541 sorted_by_abbrev.emplace_back
7542 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7543 sig_type->per_cu.section,
7544 sig_type->per_cu.sect_off));
7545
7546 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7547 sort_tu_by_abbrev_offset);
7548
7549 abbrev_offset = (sect_offset) ~(unsigned) 0;
7550
7551 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7552 {
7553 /* Switch to the next abbrev table if necessary. */
7554 if (abbrev_table == NULL
7555 || tu.abbrev_offset != abbrev_offset)
7556 {
7557 abbrev_offset = tu.abbrev_offset;
7558 abbrev_table =
7559 abbrev_table::read (dwarf2_per_objfile->objfile,
7560 &dwarf2_per_objfile->abbrev,
7561 abbrev_offset);
7562 ++tu_stats->nr_uniq_abbrev_tables;
7563 }
7564
7565 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7566 0, false);
7567 if (!reader.dummy_p)
7568 build_type_psymtabs_reader (&reader, reader.info_ptr,
7569 reader.comp_unit_die);
7570 }
7571 }
7572
7573 /* Print collected type unit statistics. */
7574
7575 static void
7576 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7577 {
7578 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7579
7580 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7581 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7582 dwarf2_per_objfile->all_type_units.size ());
7583 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7584 tu_stats->nr_uniq_abbrev_tables);
7585 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7586 tu_stats->nr_symtabs);
7587 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7588 tu_stats->nr_symtab_sharers);
7589 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7590 tu_stats->nr_stmt_less_type_units);
7591 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7592 tu_stats->nr_all_type_units_reallocs);
7593 }
7594
7595 /* Traversal function for build_type_psymtabs. */
7596
7597 static int
7598 build_type_psymtab_dependencies (void **slot, void *info)
7599 {
7600 struct dwarf2_per_objfile *dwarf2_per_objfile
7601 = (struct dwarf2_per_objfile *) info;
7602 struct objfile *objfile = dwarf2_per_objfile->objfile;
7603 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7604 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7605 dwarf2_psymtab *pst = per_cu->v.psymtab;
7606 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7607 int i;
7608
7609 gdb_assert (len > 0);
7610 gdb_assert (per_cu->type_unit_group_p ());
7611
7612 pst->number_of_dependencies = len;
7613 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7614 for (i = 0; i < len; ++i)
7615 {
7616 struct signatured_type *iter = tu_group->tus->at (i);
7617 gdb_assert (iter->per_cu.is_debug_types);
7618 pst->dependencies[i] = iter->per_cu.v.psymtab;
7619 iter->type_unit_group = tu_group;
7620 }
7621
7622 delete tu_group->tus;
7623 tu_group->tus = nullptr;
7624
7625 return 1;
7626 }
7627
7628 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7629 Build partial symbol tables for the .debug_types comp-units. */
7630
7631 static void
7632 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7633 {
7634 if (! create_all_type_units (dwarf2_per_objfile))
7635 return;
7636
7637 build_type_psymtabs_1 (dwarf2_per_objfile);
7638 }
7639
7640 /* Traversal function for process_skeletonless_type_unit.
7641 Read a TU in a DWO file and build partial symbols for it. */
7642
7643 static int
7644 process_skeletonless_type_unit (void **slot, void *info)
7645 {
7646 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7647 struct dwarf2_per_objfile *dwarf2_per_objfile
7648 = (struct dwarf2_per_objfile *) info;
7649 struct signatured_type find_entry, *entry;
7650
7651 /* If this TU doesn't exist in the global table, add it and read it in. */
7652
7653 if (dwarf2_per_objfile->signatured_types == NULL)
7654 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
7655
7656 find_entry.signature = dwo_unit->signature;
7657 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7658 &find_entry, INSERT);
7659 /* If we've already seen this type there's nothing to do. What's happening
7660 is we're doing our own version of comdat-folding here. */
7661 if (*slot != NULL)
7662 return 1;
7663
7664 /* This does the job that create_all_type_units would have done for
7665 this TU. */
7666 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7667 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7668 *slot = entry;
7669
7670 /* This does the job that build_type_psymtabs_1 would have done. */
7671 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7672 if (!reader.dummy_p)
7673 build_type_psymtabs_reader (&reader, reader.info_ptr,
7674 reader.comp_unit_die);
7675
7676 return 1;
7677 }
7678
7679 /* Traversal function for process_skeletonless_type_units. */
7680
7681 static int
7682 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7683 {
7684 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7685
7686 if (dwo_file->tus != NULL)
7687 htab_traverse_noresize (dwo_file->tus.get (),
7688 process_skeletonless_type_unit, info);
7689
7690 return 1;
7691 }
7692
7693 /* Scan all TUs of DWO files, verifying we've processed them.
7694 This is needed in case a TU was emitted without its skeleton.
7695 Note: This can't be done until we know what all the DWO files are. */
7696
7697 static void
7698 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7699 {
7700 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7701 if (get_dwp_file (dwarf2_per_objfile) == NULL
7702 && dwarf2_per_objfile->dwo_files != NULL)
7703 {
7704 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7705 process_dwo_file_for_skeletonless_type_units,
7706 dwarf2_per_objfile);
7707 }
7708 }
7709
7710 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7711
7712 static void
7713 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7714 {
7715 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7716 {
7717 dwarf2_psymtab *pst = per_cu->v.psymtab;
7718
7719 if (pst == NULL)
7720 continue;
7721
7722 for (int j = 0; j < pst->number_of_dependencies; ++j)
7723 {
7724 /* Set the 'user' field only if it is not already set. */
7725 if (pst->dependencies[j]->user == NULL)
7726 pst->dependencies[j]->user = pst;
7727 }
7728 }
7729 }
7730
7731 /* Build the partial symbol table by doing a quick pass through the
7732 .debug_info and .debug_abbrev sections. */
7733
7734 static void
7735 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7736 {
7737 struct objfile *objfile = dwarf2_per_objfile->objfile;
7738
7739 if (dwarf_read_debug)
7740 {
7741 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7742 objfile_name (objfile));
7743 }
7744
7745 scoped_restore restore_reading_psyms
7746 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7747 true);
7748
7749 dwarf2_per_objfile->info.read (objfile);
7750
7751 /* Any cached compilation units will be linked by the per-objfile
7752 read_in_chain. Make sure to free them when we're done. */
7753 free_cached_comp_units freer (dwarf2_per_objfile);
7754
7755 build_type_psymtabs (dwarf2_per_objfile);
7756
7757 create_all_comp_units (dwarf2_per_objfile);
7758
7759 /* Create a temporary address map on a temporary obstack. We later
7760 copy this to the final obstack. */
7761 auto_obstack temp_obstack;
7762
7763 scoped_restore save_psymtabs_addrmap
7764 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7765 addrmap_create_mutable (&temp_obstack));
7766
7767 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7768 process_psymtab_comp_unit (per_cu, false, language_minimal);
7769
7770 /* This has to wait until we read the CUs, we need the list of DWOs. */
7771 process_skeletonless_type_units (dwarf2_per_objfile);
7772
7773 /* Now that all TUs have been processed we can fill in the dependencies. */
7774 if (dwarf2_per_objfile->type_unit_groups != NULL)
7775 {
7776 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7777 build_type_psymtab_dependencies, dwarf2_per_objfile);
7778 }
7779
7780 if (dwarf_read_debug)
7781 print_tu_stats (dwarf2_per_objfile);
7782
7783 set_partial_user (dwarf2_per_objfile);
7784
7785 objfile->partial_symtabs->psymtabs_addrmap
7786 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7787 objfile->partial_symtabs->obstack ());
7788 /* At this point we want to keep the address map. */
7789 save_psymtabs_addrmap.release ();
7790
7791 if (dwarf_read_debug)
7792 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7793 objfile_name (objfile));
7794 }
7795
7796 /* Load the partial DIEs for a secondary CU into memory.
7797 This is also used when rereading a primary CU with load_all_dies. */
7798
7799 static void
7800 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7801 {
7802 cutu_reader reader (this_cu, NULL, 1, false);
7803
7804 if (!reader.dummy_p)
7805 {
7806 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7807 language_minimal);
7808
7809 /* Check if comp unit has_children.
7810 If so, read the rest of the partial symbols from this comp unit.
7811 If not, there's no more debug_info for this comp unit. */
7812 if (reader.comp_unit_die->has_children)
7813 load_partial_dies (&reader, reader.info_ptr, 0);
7814
7815 reader.keep ();
7816 }
7817 }
7818
7819 static void
7820 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7821 struct dwarf2_section_info *section,
7822 struct dwarf2_section_info *abbrev_section,
7823 unsigned int is_dwz)
7824 {
7825 const gdb_byte *info_ptr;
7826 struct objfile *objfile = dwarf2_per_objfile->objfile;
7827
7828 if (dwarf_read_debug)
7829 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7830 section->get_name (),
7831 section->get_file_name ());
7832
7833 section->read (objfile);
7834
7835 info_ptr = section->buffer;
7836
7837 while (info_ptr < section->buffer + section->size)
7838 {
7839 struct dwarf2_per_cu_data *this_cu;
7840
7841 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7842
7843 comp_unit_head cu_header;
7844 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7845 abbrev_section, info_ptr,
7846 rcuh_kind::COMPILE);
7847
7848 /* Save the compilation unit for later lookup. */
7849 if (cu_header.unit_type != DW_UT_type)
7850 {
7851 this_cu = XOBNEW (&objfile->objfile_obstack,
7852 struct dwarf2_per_cu_data);
7853 memset (this_cu, 0, sizeof (*this_cu));
7854 }
7855 else
7856 {
7857 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7858 struct signatured_type);
7859 memset (sig_type, 0, sizeof (*sig_type));
7860 sig_type->signature = cu_header.signature;
7861 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7862 this_cu = &sig_type->per_cu;
7863 }
7864 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7865 this_cu->sect_off = sect_off;
7866 this_cu->length = cu_header.length + cu_header.initial_length_size;
7867 this_cu->is_dwz = is_dwz;
7868 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7869 this_cu->section = section;
7870
7871 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7872
7873 info_ptr = info_ptr + this_cu->length;
7874 }
7875 }
7876
7877 /* Create a list of all compilation units in OBJFILE.
7878 This is only done for -readnow and building partial symtabs. */
7879
7880 static void
7881 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7882 {
7883 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7884 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7885 &dwarf2_per_objfile->abbrev, 0);
7886
7887 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7888 if (dwz != NULL)
7889 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7890 1);
7891 }
7892
7893 /* Process all loaded DIEs for compilation unit CU, starting at
7894 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7895 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7896 DW_AT_ranges). See the comments of add_partial_subprogram on how
7897 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7898
7899 static void
7900 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7901 CORE_ADDR *highpc, int set_addrmap,
7902 struct dwarf2_cu *cu)
7903 {
7904 struct partial_die_info *pdi;
7905
7906 /* Now, march along the PDI's, descending into ones which have
7907 interesting children but skipping the children of the other ones,
7908 until we reach the end of the compilation unit. */
7909
7910 pdi = first_die;
7911
7912 while (pdi != NULL)
7913 {
7914 pdi->fixup (cu);
7915
7916 /* Anonymous namespaces or modules have no name but have interesting
7917 children, so we need to look at them. Ditto for anonymous
7918 enums. */
7919
7920 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7921 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7922 || pdi->tag == DW_TAG_imported_unit
7923 || pdi->tag == DW_TAG_inlined_subroutine)
7924 {
7925 switch (pdi->tag)
7926 {
7927 case DW_TAG_subprogram:
7928 case DW_TAG_inlined_subroutine:
7929 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7930 break;
7931 case DW_TAG_constant:
7932 case DW_TAG_variable:
7933 case DW_TAG_typedef:
7934 case DW_TAG_union_type:
7935 if (!pdi->is_declaration)
7936 {
7937 add_partial_symbol (pdi, cu);
7938 }
7939 break;
7940 case DW_TAG_class_type:
7941 case DW_TAG_interface_type:
7942 case DW_TAG_structure_type:
7943 if (!pdi->is_declaration)
7944 {
7945 add_partial_symbol (pdi, cu);
7946 }
7947 if ((cu->language == language_rust
7948 || cu->language == language_cplus) && pdi->has_children)
7949 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7950 set_addrmap, cu);
7951 break;
7952 case DW_TAG_enumeration_type:
7953 if (!pdi->is_declaration)
7954 add_partial_enumeration (pdi, cu);
7955 break;
7956 case DW_TAG_base_type:
7957 case DW_TAG_subrange_type:
7958 /* File scope base type definitions are added to the partial
7959 symbol table. */
7960 add_partial_symbol (pdi, cu);
7961 break;
7962 case DW_TAG_namespace:
7963 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7964 break;
7965 case DW_TAG_module:
7966 if (!pdi->is_declaration)
7967 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7968 break;
7969 case DW_TAG_imported_unit:
7970 {
7971 struct dwarf2_per_cu_data *per_cu;
7972
7973 /* For now we don't handle imported units in type units. */
7974 if (cu->per_cu->is_debug_types)
7975 {
7976 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7977 " supported in type units [in module %s]"),
7978 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7979 }
7980
7981 per_cu = dwarf2_find_containing_comp_unit
7982 (pdi->d.sect_off, pdi->is_dwz,
7983 cu->per_cu->dwarf2_per_objfile);
7984
7985 /* Go read the partial unit, if needed. */
7986 if (per_cu->v.psymtab == NULL)
7987 process_psymtab_comp_unit (per_cu, true, cu->language);
7988
7989 cu->per_cu->imported_symtabs_push (per_cu);
7990 }
7991 break;
7992 case DW_TAG_imported_declaration:
7993 add_partial_symbol (pdi, cu);
7994 break;
7995 default:
7996 break;
7997 }
7998 }
7999
8000 /* If the die has a sibling, skip to the sibling. */
8001
8002 pdi = pdi->die_sibling;
8003 }
8004 }
8005
8006 /* Functions used to compute the fully scoped name of a partial DIE.
8007
8008 Normally, this is simple. For C++, the parent DIE's fully scoped
8009 name is concatenated with "::" and the partial DIE's name.
8010 Enumerators are an exception; they use the scope of their parent
8011 enumeration type, i.e. the name of the enumeration type is not
8012 prepended to the enumerator.
8013
8014 There are two complexities. One is DW_AT_specification; in this
8015 case "parent" means the parent of the target of the specification,
8016 instead of the direct parent of the DIE. The other is compilers
8017 which do not emit DW_TAG_namespace; in this case we try to guess
8018 the fully qualified name of structure types from their members'
8019 linkage names. This must be done using the DIE's children rather
8020 than the children of any DW_AT_specification target. We only need
8021 to do this for structures at the top level, i.e. if the target of
8022 any DW_AT_specification (if any; otherwise the DIE itself) does not
8023 have a parent. */
8024
8025 /* Compute the scope prefix associated with PDI's parent, in
8026 compilation unit CU. The result will be allocated on CU's
8027 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8028 field. NULL is returned if no prefix is necessary. */
8029 static const char *
8030 partial_die_parent_scope (struct partial_die_info *pdi,
8031 struct dwarf2_cu *cu)
8032 {
8033 const char *grandparent_scope;
8034 struct partial_die_info *parent, *real_pdi;
8035
8036 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8037 then this means the parent of the specification DIE. */
8038
8039 real_pdi = pdi;
8040 while (real_pdi->has_specification)
8041 {
8042 auto res = find_partial_die (real_pdi->spec_offset,
8043 real_pdi->spec_is_dwz, cu);
8044 real_pdi = res.pdi;
8045 cu = res.cu;
8046 }
8047
8048 parent = real_pdi->die_parent;
8049 if (parent == NULL)
8050 return NULL;
8051
8052 if (parent->scope_set)
8053 return parent->scope;
8054
8055 parent->fixup (cu);
8056
8057 grandparent_scope = partial_die_parent_scope (parent, cu);
8058
8059 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8060 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8061 Work around this problem here. */
8062 if (cu->language == language_cplus
8063 && parent->tag == DW_TAG_namespace
8064 && strcmp (parent->name, "::") == 0
8065 && grandparent_scope == NULL)
8066 {
8067 parent->scope = NULL;
8068 parent->scope_set = 1;
8069 return NULL;
8070 }
8071
8072 /* Nested subroutines in Fortran get a prefix. */
8073 if (pdi->tag == DW_TAG_enumerator)
8074 /* Enumerators should not get the name of the enumeration as a prefix. */
8075 parent->scope = grandparent_scope;
8076 else if (parent->tag == DW_TAG_namespace
8077 || parent->tag == DW_TAG_module
8078 || parent->tag == DW_TAG_structure_type
8079 || parent->tag == DW_TAG_class_type
8080 || parent->tag == DW_TAG_interface_type
8081 || parent->tag == DW_TAG_union_type
8082 || parent->tag == DW_TAG_enumeration_type
8083 || (cu->language == language_fortran
8084 && parent->tag == DW_TAG_subprogram
8085 && pdi->tag == DW_TAG_subprogram))
8086 {
8087 if (grandparent_scope == NULL)
8088 parent->scope = parent->name;
8089 else
8090 parent->scope = typename_concat (&cu->comp_unit_obstack,
8091 grandparent_scope,
8092 parent->name, 0, cu);
8093 }
8094 else
8095 {
8096 /* FIXME drow/2004-04-01: What should we be doing with
8097 function-local names? For partial symbols, we should probably be
8098 ignoring them. */
8099 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8100 dwarf_tag_name (parent->tag),
8101 sect_offset_str (pdi->sect_off));
8102 parent->scope = grandparent_scope;
8103 }
8104
8105 parent->scope_set = 1;
8106 return parent->scope;
8107 }
8108
8109 /* Return the fully scoped name associated with PDI, from compilation unit
8110 CU. The result will be allocated with malloc. */
8111
8112 static gdb::unique_xmalloc_ptr<char>
8113 partial_die_full_name (struct partial_die_info *pdi,
8114 struct dwarf2_cu *cu)
8115 {
8116 const char *parent_scope;
8117
8118 /* If this is a template instantiation, we can not work out the
8119 template arguments from partial DIEs. So, unfortunately, we have
8120 to go through the full DIEs. At least any work we do building
8121 types here will be reused if full symbols are loaded later. */
8122 if (pdi->has_template_arguments)
8123 {
8124 pdi->fixup (cu);
8125
8126 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8127 {
8128 struct die_info *die;
8129 struct attribute attr;
8130 struct dwarf2_cu *ref_cu = cu;
8131
8132 /* DW_FORM_ref_addr is using section offset. */
8133 attr.name = (enum dwarf_attribute) 0;
8134 attr.form = DW_FORM_ref_addr;
8135 attr.u.unsnd = to_underlying (pdi->sect_off);
8136 die = follow_die_ref (NULL, &attr, &ref_cu);
8137
8138 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8139 }
8140 }
8141
8142 parent_scope = partial_die_parent_scope (pdi, cu);
8143 if (parent_scope == NULL)
8144 return NULL;
8145 else
8146 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8147 pdi->name, 0, cu));
8148 }
8149
8150 static void
8151 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8152 {
8153 struct dwarf2_per_objfile *dwarf2_per_objfile
8154 = cu->per_cu->dwarf2_per_objfile;
8155 struct objfile *objfile = dwarf2_per_objfile->objfile;
8156 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8157 CORE_ADDR addr = 0;
8158 const char *actual_name = NULL;
8159 CORE_ADDR baseaddr;
8160
8161 baseaddr = objfile->text_section_offset ();
8162
8163 gdb::unique_xmalloc_ptr<char> built_actual_name
8164 = partial_die_full_name (pdi, cu);
8165 if (built_actual_name != NULL)
8166 actual_name = built_actual_name.get ();
8167
8168 if (actual_name == NULL)
8169 actual_name = pdi->name;
8170
8171 switch (pdi->tag)
8172 {
8173 case DW_TAG_inlined_subroutine:
8174 case DW_TAG_subprogram:
8175 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8176 - baseaddr);
8177 if (pdi->is_external
8178 || cu->language == language_ada
8179 || (cu->language == language_fortran
8180 && pdi->die_parent != NULL
8181 && pdi->die_parent->tag == DW_TAG_subprogram))
8182 {
8183 /* Normally, only "external" DIEs are part of the global scope.
8184 But in Ada and Fortran, we want to be able to access nested
8185 procedures globally. So all Ada and Fortran subprograms are
8186 stored in the global scope. */
8187 add_psymbol_to_list (actual_name,
8188 built_actual_name != NULL,
8189 VAR_DOMAIN, LOC_BLOCK,
8190 SECT_OFF_TEXT (objfile),
8191 psymbol_placement::GLOBAL,
8192 addr,
8193 cu->language, objfile);
8194 }
8195 else
8196 {
8197 add_psymbol_to_list (actual_name,
8198 built_actual_name != NULL,
8199 VAR_DOMAIN, LOC_BLOCK,
8200 SECT_OFF_TEXT (objfile),
8201 psymbol_placement::STATIC,
8202 addr, cu->language, objfile);
8203 }
8204
8205 if (pdi->main_subprogram && actual_name != NULL)
8206 set_objfile_main_name (objfile, actual_name, cu->language);
8207 break;
8208 case DW_TAG_constant:
8209 add_psymbol_to_list (actual_name,
8210 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8211 -1, (pdi->is_external
8212 ? psymbol_placement::GLOBAL
8213 : psymbol_placement::STATIC),
8214 0, cu->language, objfile);
8215 break;
8216 case DW_TAG_variable:
8217 if (pdi->d.locdesc)
8218 addr = decode_locdesc (pdi->d.locdesc, cu);
8219
8220 if (pdi->d.locdesc
8221 && addr == 0
8222 && !dwarf2_per_objfile->has_section_at_zero)
8223 {
8224 /* A global or static variable may also have been stripped
8225 out by the linker if unused, in which case its address
8226 will be nullified; do not add such variables into partial
8227 symbol table then. */
8228 }
8229 else if (pdi->is_external)
8230 {
8231 /* Global Variable.
8232 Don't enter into the minimal symbol tables as there is
8233 a minimal symbol table entry from the ELF symbols already.
8234 Enter into partial symbol table if it has a location
8235 descriptor or a type.
8236 If the location descriptor is missing, new_symbol will create
8237 a LOC_UNRESOLVED symbol, the address of the variable will then
8238 be determined from the minimal symbol table whenever the variable
8239 is referenced.
8240 The address for the partial symbol table entry is not
8241 used by GDB, but it comes in handy for debugging partial symbol
8242 table building. */
8243
8244 if (pdi->d.locdesc || pdi->has_type)
8245 add_psymbol_to_list (actual_name,
8246 built_actual_name != NULL,
8247 VAR_DOMAIN, LOC_STATIC,
8248 SECT_OFF_TEXT (objfile),
8249 psymbol_placement::GLOBAL,
8250 addr, cu->language, objfile);
8251 }
8252 else
8253 {
8254 int has_loc = pdi->d.locdesc != NULL;
8255
8256 /* Static Variable. Skip symbols whose value we cannot know (those
8257 without location descriptors or constant values). */
8258 if (!has_loc && !pdi->has_const_value)
8259 return;
8260
8261 add_psymbol_to_list (actual_name,
8262 built_actual_name != NULL,
8263 VAR_DOMAIN, LOC_STATIC,
8264 SECT_OFF_TEXT (objfile),
8265 psymbol_placement::STATIC,
8266 has_loc ? addr : 0,
8267 cu->language, objfile);
8268 }
8269 break;
8270 case DW_TAG_typedef:
8271 case DW_TAG_base_type:
8272 case DW_TAG_subrange_type:
8273 add_psymbol_to_list (actual_name,
8274 built_actual_name != NULL,
8275 VAR_DOMAIN, LOC_TYPEDEF, -1,
8276 psymbol_placement::STATIC,
8277 0, cu->language, objfile);
8278 break;
8279 case DW_TAG_imported_declaration:
8280 case DW_TAG_namespace:
8281 add_psymbol_to_list (actual_name,
8282 built_actual_name != NULL,
8283 VAR_DOMAIN, LOC_TYPEDEF, -1,
8284 psymbol_placement::GLOBAL,
8285 0, cu->language, objfile);
8286 break;
8287 case DW_TAG_module:
8288 /* With Fortran 77 there might be a "BLOCK DATA" module
8289 available without any name. If so, we skip the module as it
8290 doesn't bring any value. */
8291 if (actual_name != nullptr)
8292 add_psymbol_to_list (actual_name,
8293 built_actual_name != NULL,
8294 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8295 psymbol_placement::GLOBAL,
8296 0, cu->language, objfile);
8297 break;
8298 case DW_TAG_class_type:
8299 case DW_TAG_interface_type:
8300 case DW_TAG_structure_type:
8301 case DW_TAG_union_type:
8302 case DW_TAG_enumeration_type:
8303 /* Skip external references. The DWARF standard says in the section
8304 about "Structure, Union, and Class Type Entries": "An incomplete
8305 structure, union or class type is represented by a structure,
8306 union or class entry that does not have a byte size attribute
8307 and that has a DW_AT_declaration attribute." */
8308 if (!pdi->has_byte_size && pdi->is_declaration)
8309 return;
8310
8311 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8312 static vs. global. */
8313 add_psymbol_to_list (actual_name,
8314 built_actual_name != NULL,
8315 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8316 cu->language == language_cplus
8317 ? psymbol_placement::GLOBAL
8318 : psymbol_placement::STATIC,
8319 0, cu->language, objfile);
8320
8321 break;
8322 case DW_TAG_enumerator:
8323 add_psymbol_to_list (actual_name,
8324 built_actual_name != NULL,
8325 VAR_DOMAIN, LOC_CONST, -1,
8326 cu->language == language_cplus
8327 ? psymbol_placement::GLOBAL
8328 : psymbol_placement::STATIC,
8329 0, cu->language, objfile);
8330 break;
8331 default:
8332 break;
8333 }
8334 }
8335
8336 /* Read a partial die corresponding to a namespace; also, add a symbol
8337 corresponding to that namespace to the symbol table. NAMESPACE is
8338 the name of the enclosing namespace. */
8339
8340 static void
8341 add_partial_namespace (struct partial_die_info *pdi,
8342 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8343 int set_addrmap, struct dwarf2_cu *cu)
8344 {
8345 /* Add a symbol for the namespace. */
8346
8347 add_partial_symbol (pdi, cu);
8348
8349 /* Now scan partial symbols in that namespace. */
8350
8351 if (pdi->has_children)
8352 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8353 }
8354
8355 /* Read a partial die corresponding to a Fortran module. */
8356
8357 static void
8358 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8359 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8360 {
8361 /* Add a symbol for the namespace. */
8362
8363 add_partial_symbol (pdi, cu);
8364
8365 /* Now scan partial symbols in that module. */
8366
8367 if (pdi->has_children)
8368 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8369 }
8370
8371 /* Read a partial die corresponding to a subprogram or an inlined
8372 subprogram and create a partial symbol for that subprogram.
8373 When the CU language allows it, this routine also defines a partial
8374 symbol for each nested subprogram that this subprogram contains.
8375 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8376 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8377
8378 PDI may also be a lexical block, in which case we simply search
8379 recursively for subprograms defined inside that lexical block.
8380 Again, this is only performed when the CU language allows this
8381 type of definitions. */
8382
8383 static void
8384 add_partial_subprogram (struct partial_die_info *pdi,
8385 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8386 int set_addrmap, struct dwarf2_cu *cu)
8387 {
8388 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8389 {
8390 if (pdi->has_pc_info)
8391 {
8392 if (pdi->lowpc < *lowpc)
8393 *lowpc = pdi->lowpc;
8394 if (pdi->highpc > *highpc)
8395 *highpc = pdi->highpc;
8396 if (set_addrmap)
8397 {
8398 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8399 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8400 CORE_ADDR baseaddr;
8401 CORE_ADDR this_highpc;
8402 CORE_ADDR this_lowpc;
8403
8404 baseaddr = objfile->text_section_offset ();
8405 this_lowpc
8406 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8407 pdi->lowpc + baseaddr)
8408 - baseaddr);
8409 this_highpc
8410 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8411 pdi->highpc + baseaddr)
8412 - baseaddr);
8413 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8414 this_lowpc, this_highpc - 1,
8415 cu->per_cu->v.psymtab);
8416 }
8417 }
8418
8419 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8420 {
8421 if (!pdi->is_declaration)
8422 /* Ignore subprogram DIEs that do not have a name, they are
8423 illegal. Do not emit a complaint at this point, we will
8424 do so when we convert this psymtab into a symtab. */
8425 if (pdi->name)
8426 add_partial_symbol (pdi, cu);
8427 }
8428 }
8429
8430 if (! pdi->has_children)
8431 return;
8432
8433 if (cu->language == language_ada || cu->language == language_fortran)
8434 {
8435 pdi = pdi->die_child;
8436 while (pdi != NULL)
8437 {
8438 pdi->fixup (cu);
8439 if (pdi->tag == DW_TAG_subprogram
8440 || pdi->tag == DW_TAG_inlined_subroutine
8441 || pdi->tag == DW_TAG_lexical_block)
8442 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8443 pdi = pdi->die_sibling;
8444 }
8445 }
8446 }
8447
8448 /* Read a partial die corresponding to an enumeration type. */
8449
8450 static void
8451 add_partial_enumeration (struct partial_die_info *enum_pdi,
8452 struct dwarf2_cu *cu)
8453 {
8454 struct partial_die_info *pdi;
8455
8456 if (enum_pdi->name != NULL)
8457 add_partial_symbol (enum_pdi, cu);
8458
8459 pdi = enum_pdi->die_child;
8460 while (pdi)
8461 {
8462 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8463 complaint (_("malformed enumerator DIE ignored"));
8464 else
8465 add_partial_symbol (pdi, cu);
8466 pdi = pdi->die_sibling;
8467 }
8468 }
8469
8470 /* Return the initial uleb128 in the die at INFO_PTR. */
8471
8472 static unsigned int
8473 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8474 {
8475 unsigned int bytes_read;
8476
8477 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8478 }
8479
8480 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8481 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8482
8483 Return the corresponding abbrev, or NULL if the number is zero (indicating
8484 an empty DIE). In either case *BYTES_READ will be set to the length of
8485 the initial number. */
8486
8487 static struct abbrev_info *
8488 peek_die_abbrev (const die_reader_specs &reader,
8489 const gdb_byte *info_ptr, unsigned int *bytes_read)
8490 {
8491 dwarf2_cu *cu = reader.cu;
8492 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8493 unsigned int abbrev_number
8494 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8495
8496 if (abbrev_number == 0)
8497 return NULL;
8498
8499 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8500 if (!abbrev)
8501 {
8502 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8503 " at offset %s [in module %s]"),
8504 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8505 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8506 }
8507
8508 return abbrev;
8509 }
8510
8511 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8512 Returns a pointer to the end of a series of DIEs, terminated by an empty
8513 DIE. Any children of the skipped DIEs will also be skipped. */
8514
8515 static const gdb_byte *
8516 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8517 {
8518 while (1)
8519 {
8520 unsigned int bytes_read;
8521 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8522
8523 if (abbrev == NULL)
8524 return info_ptr + bytes_read;
8525 else
8526 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8527 }
8528 }
8529
8530 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8531 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8532 abbrev corresponding to that skipped uleb128 should be passed in
8533 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8534 children. */
8535
8536 static const gdb_byte *
8537 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8538 struct abbrev_info *abbrev)
8539 {
8540 unsigned int bytes_read;
8541 struct attribute attr;
8542 bfd *abfd = reader->abfd;
8543 struct dwarf2_cu *cu = reader->cu;
8544 const gdb_byte *buffer = reader->buffer;
8545 const gdb_byte *buffer_end = reader->buffer_end;
8546 unsigned int form, i;
8547
8548 for (i = 0; i < abbrev->num_attrs; i++)
8549 {
8550 /* The only abbrev we care about is DW_AT_sibling. */
8551 if (abbrev->attrs[i].name == DW_AT_sibling)
8552 {
8553 bool ignored;
8554 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8555 &ignored);
8556 if (attr.form == DW_FORM_ref_addr)
8557 complaint (_("ignoring absolute DW_AT_sibling"));
8558 else
8559 {
8560 sect_offset off = attr.get_ref_die_offset ();
8561 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8562
8563 if (sibling_ptr < info_ptr)
8564 complaint (_("DW_AT_sibling points backwards"));
8565 else if (sibling_ptr > reader->buffer_end)
8566 reader->die_section->overflow_complaint ();
8567 else
8568 return sibling_ptr;
8569 }
8570 }
8571
8572 /* If it isn't DW_AT_sibling, skip this attribute. */
8573 form = abbrev->attrs[i].form;
8574 skip_attribute:
8575 switch (form)
8576 {
8577 case DW_FORM_ref_addr:
8578 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8579 and later it is offset sized. */
8580 if (cu->header.version == 2)
8581 info_ptr += cu->header.addr_size;
8582 else
8583 info_ptr += cu->header.offset_size;
8584 break;
8585 case DW_FORM_GNU_ref_alt:
8586 info_ptr += cu->header.offset_size;
8587 break;
8588 case DW_FORM_addr:
8589 info_ptr += cu->header.addr_size;
8590 break;
8591 case DW_FORM_data1:
8592 case DW_FORM_ref1:
8593 case DW_FORM_flag:
8594 case DW_FORM_strx1:
8595 info_ptr += 1;
8596 break;
8597 case DW_FORM_flag_present:
8598 case DW_FORM_implicit_const:
8599 break;
8600 case DW_FORM_data2:
8601 case DW_FORM_ref2:
8602 case DW_FORM_strx2:
8603 info_ptr += 2;
8604 break;
8605 case DW_FORM_strx3:
8606 info_ptr += 3;
8607 break;
8608 case DW_FORM_data4:
8609 case DW_FORM_ref4:
8610 case DW_FORM_strx4:
8611 info_ptr += 4;
8612 break;
8613 case DW_FORM_data8:
8614 case DW_FORM_ref8:
8615 case DW_FORM_ref_sig8:
8616 info_ptr += 8;
8617 break;
8618 case DW_FORM_data16:
8619 info_ptr += 16;
8620 break;
8621 case DW_FORM_string:
8622 read_direct_string (abfd, info_ptr, &bytes_read);
8623 info_ptr += bytes_read;
8624 break;
8625 case DW_FORM_sec_offset:
8626 case DW_FORM_strp:
8627 case DW_FORM_GNU_strp_alt:
8628 info_ptr += cu->header.offset_size;
8629 break;
8630 case DW_FORM_exprloc:
8631 case DW_FORM_block:
8632 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8633 info_ptr += bytes_read;
8634 break;
8635 case DW_FORM_block1:
8636 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8637 break;
8638 case DW_FORM_block2:
8639 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8640 break;
8641 case DW_FORM_block4:
8642 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8643 break;
8644 case DW_FORM_addrx:
8645 case DW_FORM_strx:
8646 case DW_FORM_sdata:
8647 case DW_FORM_udata:
8648 case DW_FORM_ref_udata:
8649 case DW_FORM_GNU_addr_index:
8650 case DW_FORM_GNU_str_index:
8651 case DW_FORM_rnglistx:
8652 case DW_FORM_loclistx:
8653 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8654 break;
8655 case DW_FORM_indirect:
8656 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8657 info_ptr += bytes_read;
8658 /* We need to continue parsing from here, so just go back to
8659 the top. */
8660 goto skip_attribute;
8661
8662 default:
8663 error (_("Dwarf Error: Cannot handle %s "
8664 "in DWARF reader [in module %s]"),
8665 dwarf_form_name (form),
8666 bfd_get_filename (abfd));
8667 }
8668 }
8669
8670 if (abbrev->has_children)
8671 return skip_children (reader, info_ptr);
8672 else
8673 return info_ptr;
8674 }
8675
8676 /* Locate ORIG_PDI's sibling.
8677 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8678
8679 static const gdb_byte *
8680 locate_pdi_sibling (const struct die_reader_specs *reader,
8681 struct partial_die_info *orig_pdi,
8682 const gdb_byte *info_ptr)
8683 {
8684 /* Do we know the sibling already? */
8685
8686 if (orig_pdi->sibling)
8687 return orig_pdi->sibling;
8688
8689 /* Are there any children to deal with? */
8690
8691 if (!orig_pdi->has_children)
8692 return info_ptr;
8693
8694 /* Skip the children the long way. */
8695
8696 return skip_children (reader, info_ptr);
8697 }
8698
8699 /* Expand this partial symbol table into a full symbol table. SELF is
8700 not NULL. */
8701
8702 void
8703 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8704 {
8705 struct dwarf2_per_objfile *dwarf2_per_objfile
8706 = get_dwarf2_per_objfile (objfile);
8707
8708 gdb_assert (!readin);
8709 /* If this psymtab is constructed from a debug-only objfile, the
8710 has_section_at_zero flag will not necessarily be correct. We
8711 can get the correct value for this flag by looking at the data
8712 associated with the (presumably stripped) associated objfile. */
8713 if (objfile->separate_debug_objfile_backlink)
8714 {
8715 struct dwarf2_per_objfile *dpo_backlink
8716 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8717
8718 dwarf2_per_objfile->has_section_at_zero
8719 = dpo_backlink->has_section_at_zero;
8720 }
8721
8722 expand_psymtab (objfile);
8723
8724 process_cu_includes (dwarf2_per_objfile);
8725 }
8726 \f
8727 /* Reading in full CUs. */
8728
8729 /* Add PER_CU to the queue. */
8730
8731 static void
8732 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8733 enum language pretend_language)
8734 {
8735 per_cu->queued = 1;
8736 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8737 }
8738
8739 /* If PER_CU is not yet queued, add it to the queue.
8740 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8741 dependency.
8742 The result is non-zero if PER_CU was queued, otherwise the result is zero
8743 meaning either PER_CU is already queued or it is already loaded.
8744
8745 N.B. There is an invariant here that if a CU is queued then it is loaded.
8746 The caller is required to load PER_CU if we return non-zero. */
8747
8748 static int
8749 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8750 struct dwarf2_per_cu_data *per_cu,
8751 enum language pretend_language)
8752 {
8753 /* We may arrive here during partial symbol reading, if we need full
8754 DIEs to process an unusual case (e.g. template arguments). Do
8755 not queue PER_CU, just tell our caller to load its DIEs. */
8756 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8757 {
8758 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8759 return 1;
8760 return 0;
8761 }
8762
8763 /* Mark the dependence relation so that we don't flush PER_CU
8764 too early. */
8765 if (dependent_cu != NULL)
8766 dwarf2_add_dependence (dependent_cu, per_cu);
8767
8768 /* If it's already on the queue, we have nothing to do. */
8769 if (per_cu->queued)
8770 return 0;
8771
8772 /* If the compilation unit is already loaded, just mark it as
8773 used. */
8774 if (per_cu->cu != NULL)
8775 {
8776 per_cu->cu->last_used = 0;
8777 return 0;
8778 }
8779
8780 /* Add it to the queue. */
8781 queue_comp_unit (per_cu, pretend_language);
8782
8783 return 1;
8784 }
8785
8786 /* Process the queue. */
8787
8788 static void
8789 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8790 {
8791 if (dwarf_read_debug)
8792 {
8793 fprintf_unfiltered (gdb_stdlog,
8794 "Expanding one or more symtabs of objfile %s ...\n",
8795 objfile_name (dwarf2_per_objfile->objfile));
8796 }
8797
8798 /* The queue starts out with one item, but following a DIE reference
8799 may load a new CU, adding it to the end of the queue. */
8800 while (!dwarf2_per_objfile->queue.empty ())
8801 {
8802 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8803
8804 if ((dwarf2_per_objfile->using_index
8805 ? !item.per_cu->v.quick->compunit_symtab
8806 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8807 /* Skip dummy CUs. */
8808 && item.per_cu->cu != NULL)
8809 {
8810 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8811 unsigned int debug_print_threshold;
8812 char buf[100];
8813
8814 if (per_cu->is_debug_types)
8815 {
8816 struct signatured_type *sig_type =
8817 (struct signatured_type *) per_cu;
8818
8819 sprintf (buf, "TU %s at offset %s",
8820 hex_string (sig_type->signature),
8821 sect_offset_str (per_cu->sect_off));
8822 /* There can be 100s of TUs.
8823 Only print them in verbose mode. */
8824 debug_print_threshold = 2;
8825 }
8826 else
8827 {
8828 sprintf (buf, "CU at offset %s",
8829 sect_offset_str (per_cu->sect_off));
8830 debug_print_threshold = 1;
8831 }
8832
8833 if (dwarf_read_debug >= debug_print_threshold)
8834 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8835
8836 if (per_cu->is_debug_types)
8837 process_full_type_unit (per_cu, item.pretend_language);
8838 else
8839 process_full_comp_unit (per_cu, item.pretend_language);
8840
8841 if (dwarf_read_debug >= debug_print_threshold)
8842 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8843 }
8844
8845 item.per_cu->queued = 0;
8846 dwarf2_per_objfile->queue.pop ();
8847 }
8848
8849 if (dwarf_read_debug)
8850 {
8851 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8852 objfile_name (dwarf2_per_objfile->objfile));
8853 }
8854 }
8855
8856 /* Read in full symbols for PST, and anything it depends on. */
8857
8858 void
8859 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8860 {
8861 if (readin)
8862 return;
8863
8864 expand_dependencies (objfile);
8865
8866 dw2_do_instantiate_symtab (per_cu_data, false);
8867 gdb_assert (get_compunit_symtab () != nullptr);
8868 }
8869
8870 /* Trivial hash function for die_info: the hash value of a DIE
8871 is its offset in .debug_info for this objfile. */
8872
8873 static hashval_t
8874 die_hash (const void *item)
8875 {
8876 const struct die_info *die = (const struct die_info *) item;
8877
8878 return to_underlying (die->sect_off);
8879 }
8880
8881 /* Trivial comparison function for die_info structures: two DIEs
8882 are equal if they have the same offset. */
8883
8884 static int
8885 die_eq (const void *item_lhs, const void *item_rhs)
8886 {
8887 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8888 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8889
8890 return die_lhs->sect_off == die_rhs->sect_off;
8891 }
8892
8893 /* Load the DIEs associated with PER_CU into memory. */
8894
8895 static void
8896 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8897 bool skip_partial,
8898 enum language pretend_language)
8899 {
8900 gdb_assert (! this_cu->is_debug_types);
8901
8902 cutu_reader reader (this_cu, NULL, 1, skip_partial);
8903 if (reader.dummy_p)
8904 return;
8905
8906 struct dwarf2_cu *cu = reader.cu;
8907 const gdb_byte *info_ptr = reader.info_ptr;
8908
8909 gdb_assert (cu->die_hash == NULL);
8910 cu->die_hash =
8911 htab_create_alloc_ex (cu->header.length / 12,
8912 die_hash,
8913 die_eq,
8914 NULL,
8915 &cu->comp_unit_obstack,
8916 hashtab_obstack_allocate,
8917 dummy_obstack_deallocate);
8918
8919 if (reader.comp_unit_die->has_children)
8920 reader.comp_unit_die->child
8921 = read_die_and_siblings (&reader, reader.info_ptr,
8922 &info_ptr, reader.comp_unit_die);
8923 cu->dies = reader.comp_unit_die;
8924 /* comp_unit_die is not stored in die_hash, no need. */
8925
8926 /* We try not to read any attributes in this function, because not
8927 all CUs needed for references have been loaded yet, and symbol
8928 table processing isn't initialized. But we have to set the CU language,
8929 or we won't be able to build types correctly.
8930 Similarly, if we do not read the producer, we can not apply
8931 producer-specific interpretation. */
8932 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8933
8934 reader.keep ();
8935 }
8936
8937 /* Add a DIE to the delayed physname list. */
8938
8939 static void
8940 add_to_method_list (struct type *type, int fnfield_index, int index,
8941 const char *name, struct die_info *die,
8942 struct dwarf2_cu *cu)
8943 {
8944 struct delayed_method_info mi;
8945 mi.type = type;
8946 mi.fnfield_index = fnfield_index;
8947 mi.index = index;
8948 mi.name = name;
8949 mi.die = die;
8950 cu->method_list.push_back (mi);
8951 }
8952
8953 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8954 "const" / "volatile". If so, decrements LEN by the length of the
8955 modifier and return true. Otherwise return false. */
8956
8957 template<size_t N>
8958 static bool
8959 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8960 {
8961 size_t mod_len = sizeof (mod) - 1;
8962 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8963 {
8964 len -= mod_len;
8965 return true;
8966 }
8967 return false;
8968 }
8969
8970 /* Compute the physnames of any methods on the CU's method list.
8971
8972 The computation of method physnames is delayed in order to avoid the
8973 (bad) condition that one of the method's formal parameters is of an as yet
8974 incomplete type. */
8975
8976 static void
8977 compute_delayed_physnames (struct dwarf2_cu *cu)
8978 {
8979 /* Only C++ delays computing physnames. */
8980 if (cu->method_list.empty ())
8981 return;
8982 gdb_assert (cu->language == language_cplus);
8983
8984 for (const delayed_method_info &mi : cu->method_list)
8985 {
8986 const char *physname;
8987 struct fn_fieldlist *fn_flp
8988 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8989 physname = dwarf2_physname (mi.name, mi.die, cu);
8990 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8991 = physname ? physname : "";
8992
8993 /* Since there's no tag to indicate whether a method is a
8994 const/volatile overload, extract that information out of the
8995 demangled name. */
8996 if (physname != NULL)
8997 {
8998 size_t len = strlen (physname);
8999
9000 while (1)
9001 {
9002 if (physname[len] == ')') /* shortcut */
9003 break;
9004 else if (check_modifier (physname, len, " const"))
9005 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9006 else if (check_modifier (physname, len, " volatile"))
9007 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9008 else
9009 break;
9010 }
9011 }
9012 }
9013
9014 /* The list is no longer needed. */
9015 cu->method_list.clear ();
9016 }
9017
9018 /* Go objects should be embedded in a DW_TAG_module DIE,
9019 and it's not clear if/how imported objects will appear.
9020 To keep Go support simple until that's worked out,
9021 go back through what we've read and create something usable.
9022 We could do this while processing each DIE, and feels kinda cleaner,
9023 but that way is more invasive.
9024 This is to, for example, allow the user to type "p var" or "b main"
9025 without having to specify the package name, and allow lookups
9026 of module.object to work in contexts that use the expression
9027 parser. */
9028
9029 static void
9030 fixup_go_packaging (struct dwarf2_cu *cu)
9031 {
9032 gdb::unique_xmalloc_ptr<char> package_name;
9033 struct pending *list;
9034 int i;
9035
9036 for (list = *cu->get_builder ()->get_global_symbols ();
9037 list != NULL;
9038 list = list->next)
9039 {
9040 for (i = 0; i < list->nsyms; ++i)
9041 {
9042 struct symbol *sym = list->symbol[i];
9043
9044 if (sym->language () == language_go
9045 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9046 {
9047 gdb::unique_xmalloc_ptr<char> this_package_name
9048 (go_symbol_package_name (sym));
9049
9050 if (this_package_name == NULL)
9051 continue;
9052 if (package_name == NULL)
9053 package_name = std::move (this_package_name);
9054 else
9055 {
9056 struct objfile *objfile
9057 = cu->per_cu->dwarf2_per_objfile->objfile;
9058 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9059 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9060 (symbol_symtab (sym) != NULL
9061 ? symtab_to_filename_for_display
9062 (symbol_symtab (sym))
9063 : objfile_name (objfile)),
9064 this_package_name.get (), package_name.get ());
9065 }
9066 }
9067 }
9068 }
9069
9070 if (package_name != NULL)
9071 {
9072 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9073 const char *saved_package_name = objfile->intern (package_name.get ());
9074 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9075 saved_package_name);
9076 struct symbol *sym;
9077
9078 sym = allocate_symbol (objfile);
9079 sym->set_language (language_go, &objfile->objfile_obstack);
9080 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9081 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9082 e.g., "main" finds the "main" module and not C's main(). */
9083 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9084 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9085 SYMBOL_TYPE (sym) = type;
9086
9087 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9088 }
9089 }
9090
9091 /* Allocate a fully-qualified name consisting of the two parts on the
9092 obstack. */
9093
9094 static const char *
9095 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9096 {
9097 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9098 }
9099
9100 /* A helper that allocates a struct discriminant_info to attach to a
9101 union type. */
9102
9103 static struct discriminant_info *
9104 alloc_discriminant_info (struct type *type, int discriminant_index,
9105 int default_index)
9106 {
9107 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9108 gdb_assert (discriminant_index == -1
9109 || (discriminant_index >= 0
9110 && discriminant_index < TYPE_NFIELDS (type)));
9111 gdb_assert (default_index == -1
9112 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9113
9114 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9115
9116 struct discriminant_info *disc
9117 = ((struct discriminant_info *)
9118 TYPE_ZALLOC (type,
9119 offsetof (struct discriminant_info, discriminants)
9120 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9121 disc->default_index = default_index;
9122 disc->discriminant_index = discriminant_index;
9123
9124 struct dynamic_prop prop;
9125 prop.kind = PROP_UNDEFINED;
9126 prop.data.baton = disc;
9127
9128 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9129
9130 return disc;
9131 }
9132
9133 /* Some versions of rustc emitted enums in an unusual way.
9134
9135 Ordinary enums were emitted as unions. The first element of each
9136 structure in the union was named "RUST$ENUM$DISR". This element
9137 held the discriminant.
9138
9139 These versions of Rust also implemented the "non-zero"
9140 optimization. When the enum had two values, and one is empty and
9141 the other holds a pointer that cannot be zero, the pointer is used
9142 as the discriminant, with a zero value meaning the empty variant.
9143 Here, the union's first member is of the form
9144 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9145 where the fieldnos are the indices of the fields that should be
9146 traversed in order to find the field (which may be several fields deep)
9147 and the variantname is the name of the variant of the case when the
9148 field is zero.
9149
9150 This function recognizes whether TYPE is of one of these forms,
9151 and, if so, smashes it to be a variant type. */
9152
9153 static void
9154 quirk_rust_enum (struct type *type, struct objfile *objfile)
9155 {
9156 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9157
9158 /* We don't need to deal with empty enums. */
9159 if (TYPE_NFIELDS (type) == 0)
9160 return;
9161
9162 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9163 if (TYPE_NFIELDS (type) == 1
9164 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9165 {
9166 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9167
9168 /* Decode the field name to find the offset of the
9169 discriminant. */
9170 ULONGEST bit_offset = 0;
9171 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9172 while (name[0] >= '0' && name[0] <= '9')
9173 {
9174 char *tail;
9175 unsigned long index = strtoul (name, &tail, 10);
9176 name = tail;
9177 if (*name != '$'
9178 || index >= TYPE_NFIELDS (field_type)
9179 || (TYPE_FIELD_LOC_KIND (field_type, index)
9180 != FIELD_LOC_KIND_BITPOS))
9181 {
9182 complaint (_("Could not parse Rust enum encoding string \"%s\""
9183 "[in module %s]"),
9184 TYPE_FIELD_NAME (type, 0),
9185 objfile_name (objfile));
9186 return;
9187 }
9188 ++name;
9189
9190 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9191 field_type = TYPE_FIELD_TYPE (field_type, index);
9192 }
9193
9194 /* Make a union to hold the variants. */
9195 struct type *union_type = alloc_type (objfile);
9196 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9197 TYPE_NFIELDS (union_type) = 3;
9198 TYPE_FIELDS (union_type)
9199 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9200 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9201 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9202
9203 /* Put the discriminant must at index 0. */
9204 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9205 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9206 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9207 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9208
9209 /* The order of fields doesn't really matter, so put the real
9210 field at index 1 and the data-less field at index 2. */
9211 struct discriminant_info *disc
9212 = alloc_discriminant_info (union_type, 0, 1);
9213 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9214 TYPE_FIELD_NAME (union_type, 1)
9215 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9216 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9217 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9218 TYPE_FIELD_NAME (union_type, 1));
9219
9220 const char *dataless_name
9221 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9222 name);
9223 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9224 dataless_name);
9225 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9226 /* NAME points into the original discriminant name, which
9227 already has the correct lifetime. */
9228 TYPE_FIELD_NAME (union_type, 2) = name;
9229 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9230 disc->discriminants[2] = 0;
9231
9232 /* Smash this type to be a structure type. We have to do this
9233 because the type has already been recorded. */
9234 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9235 TYPE_NFIELDS (type) = 1;
9236 TYPE_FIELDS (type)
9237 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9238
9239 /* Install the variant part. */
9240 TYPE_FIELD_TYPE (type, 0) = union_type;
9241 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9242 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9243 }
9244 /* A union with a single anonymous field is probably an old-style
9245 univariant enum. */
9246 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9247 {
9248 /* Smash this type to be a structure type. We have to do this
9249 because the type has already been recorded. */
9250 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9251
9252 /* Make a union to hold the variants. */
9253 struct type *union_type = alloc_type (objfile);
9254 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9255 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9256 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9257 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9258 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9259
9260 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9261 const char *variant_name
9262 = rust_last_path_segment (TYPE_NAME (field_type));
9263 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9264 TYPE_NAME (field_type)
9265 = rust_fully_qualify (&objfile->objfile_obstack,
9266 TYPE_NAME (type), variant_name);
9267
9268 /* Install the union in the outer struct type. */
9269 TYPE_NFIELDS (type) = 1;
9270 TYPE_FIELDS (type)
9271 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9272 TYPE_FIELD_TYPE (type, 0) = union_type;
9273 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9274 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9275
9276 alloc_discriminant_info (union_type, -1, 0);
9277 }
9278 else
9279 {
9280 struct type *disr_type = nullptr;
9281 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9282 {
9283 disr_type = TYPE_FIELD_TYPE (type, i);
9284
9285 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9286 {
9287 /* All fields of a true enum will be structs. */
9288 return;
9289 }
9290 else if (TYPE_NFIELDS (disr_type) == 0)
9291 {
9292 /* Could be data-less variant, so keep going. */
9293 disr_type = nullptr;
9294 }
9295 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9296 "RUST$ENUM$DISR") != 0)
9297 {
9298 /* Not a Rust enum. */
9299 return;
9300 }
9301 else
9302 {
9303 /* Found one. */
9304 break;
9305 }
9306 }
9307
9308 /* If we got here without a discriminant, then it's probably
9309 just a union. */
9310 if (disr_type == nullptr)
9311 return;
9312
9313 /* Smash this type to be a structure type. We have to do this
9314 because the type has already been recorded. */
9315 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9316
9317 /* Make a union to hold the variants. */
9318 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9319 struct type *union_type = alloc_type (objfile);
9320 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9321 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9322 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9323 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9324 TYPE_FIELDS (union_type)
9325 = (struct field *) TYPE_ZALLOC (union_type,
9326 (TYPE_NFIELDS (union_type)
9327 * sizeof (struct field)));
9328
9329 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9330 TYPE_NFIELDS (type) * sizeof (struct field));
9331
9332 /* Install the discriminant at index 0 in the union. */
9333 TYPE_FIELD (union_type, 0) = *disr_field;
9334 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9335 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9336
9337 /* Install the union in the outer struct type. */
9338 TYPE_FIELD_TYPE (type, 0) = union_type;
9339 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9340 TYPE_NFIELDS (type) = 1;
9341
9342 /* Set the size and offset of the union type. */
9343 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9344
9345 /* We need a way to find the correct discriminant given a
9346 variant name. For convenience we build a map here. */
9347 struct type *enum_type = FIELD_TYPE (*disr_field);
9348 std::unordered_map<std::string, ULONGEST> discriminant_map;
9349 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9350 {
9351 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9352 {
9353 const char *name
9354 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9355 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9356 }
9357 }
9358
9359 int n_fields = TYPE_NFIELDS (union_type);
9360 struct discriminant_info *disc
9361 = alloc_discriminant_info (union_type, 0, -1);
9362 /* Skip the discriminant here. */
9363 for (int i = 1; i < n_fields; ++i)
9364 {
9365 /* Find the final word in the name of this variant's type.
9366 That name can be used to look up the correct
9367 discriminant. */
9368 const char *variant_name
9369 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9370 i)));
9371
9372 auto iter = discriminant_map.find (variant_name);
9373 if (iter != discriminant_map.end ())
9374 disc->discriminants[i] = iter->second;
9375
9376 /* Remove the discriminant field, if it exists. */
9377 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9378 if (TYPE_NFIELDS (sub_type) > 0)
9379 {
9380 --TYPE_NFIELDS (sub_type);
9381 ++TYPE_FIELDS (sub_type);
9382 }
9383 TYPE_FIELD_NAME (union_type, i) = variant_name;
9384 TYPE_NAME (sub_type)
9385 = rust_fully_qualify (&objfile->objfile_obstack,
9386 TYPE_NAME (type), variant_name);
9387 }
9388 }
9389 }
9390
9391 /* Rewrite some Rust unions to be structures with variants parts. */
9392
9393 static void
9394 rust_union_quirks (struct dwarf2_cu *cu)
9395 {
9396 gdb_assert (cu->language == language_rust);
9397 for (type *type_ : cu->rust_unions)
9398 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9399 /* We don't need this any more. */
9400 cu->rust_unions.clear ();
9401 }
9402
9403 /* Return the symtab for PER_CU. This works properly regardless of
9404 whether we're using the index or psymtabs. */
9405
9406 static struct compunit_symtab *
9407 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9408 {
9409 return (per_cu->dwarf2_per_objfile->using_index
9410 ? per_cu->v.quick->compunit_symtab
9411 : per_cu->v.psymtab->compunit_symtab);
9412 }
9413
9414 /* A helper function for computing the list of all symbol tables
9415 included by PER_CU. */
9416
9417 static void
9418 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9419 htab_t all_children, htab_t all_type_symtabs,
9420 struct dwarf2_per_cu_data *per_cu,
9421 struct compunit_symtab *immediate_parent)
9422 {
9423 void **slot;
9424 struct compunit_symtab *cust;
9425
9426 slot = htab_find_slot (all_children, per_cu, INSERT);
9427 if (*slot != NULL)
9428 {
9429 /* This inclusion and its children have been processed. */
9430 return;
9431 }
9432
9433 *slot = per_cu;
9434 /* Only add a CU if it has a symbol table. */
9435 cust = get_compunit_symtab (per_cu);
9436 if (cust != NULL)
9437 {
9438 /* If this is a type unit only add its symbol table if we haven't
9439 seen it yet (type unit per_cu's can share symtabs). */
9440 if (per_cu->is_debug_types)
9441 {
9442 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9443 if (*slot == NULL)
9444 {
9445 *slot = cust;
9446 result->push_back (cust);
9447 if (cust->user == NULL)
9448 cust->user = immediate_parent;
9449 }
9450 }
9451 else
9452 {
9453 result->push_back (cust);
9454 if (cust->user == NULL)
9455 cust->user = immediate_parent;
9456 }
9457 }
9458
9459 if (!per_cu->imported_symtabs_empty ())
9460 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9461 {
9462 recursively_compute_inclusions (result, all_children,
9463 all_type_symtabs, ptr, cust);
9464 }
9465 }
9466
9467 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9468 PER_CU. */
9469
9470 static void
9471 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9472 {
9473 gdb_assert (! per_cu->is_debug_types);
9474
9475 if (!per_cu->imported_symtabs_empty ())
9476 {
9477 int len;
9478 std::vector<compunit_symtab *> result_symtabs;
9479 htab_t all_children, all_type_symtabs;
9480 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9481
9482 /* If we don't have a symtab, we can just skip this case. */
9483 if (cust == NULL)
9484 return;
9485
9486 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9487 NULL, xcalloc, xfree);
9488 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9489 NULL, xcalloc, xfree);
9490
9491 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9492 {
9493 recursively_compute_inclusions (&result_symtabs, all_children,
9494 all_type_symtabs, ptr, cust);
9495 }
9496
9497 /* Now we have a transitive closure of all the included symtabs. */
9498 len = result_symtabs.size ();
9499 cust->includes
9500 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9501 struct compunit_symtab *, len + 1);
9502 memcpy (cust->includes, result_symtabs.data (),
9503 len * sizeof (compunit_symtab *));
9504 cust->includes[len] = NULL;
9505
9506 htab_delete (all_children);
9507 htab_delete (all_type_symtabs);
9508 }
9509 }
9510
9511 /* Compute the 'includes' field for the symtabs of all the CUs we just
9512 read. */
9513
9514 static void
9515 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9516 {
9517 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9518 {
9519 if (! iter->is_debug_types)
9520 compute_compunit_symtab_includes (iter);
9521 }
9522
9523 dwarf2_per_objfile->just_read_cus.clear ();
9524 }
9525
9526 /* Generate full symbol information for PER_CU, whose DIEs have
9527 already been loaded into memory. */
9528
9529 static void
9530 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9531 enum language pretend_language)
9532 {
9533 struct dwarf2_cu *cu = per_cu->cu;
9534 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9535 struct objfile *objfile = dwarf2_per_objfile->objfile;
9536 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9537 CORE_ADDR lowpc, highpc;
9538 struct compunit_symtab *cust;
9539 CORE_ADDR baseaddr;
9540 struct block *static_block;
9541 CORE_ADDR addr;
9542
9543 baseaddr = objfile->text_section_offset ();
9544
9545 /* Clear the list here in case something was left over. */
9546 cu->method_list.clear ();
9547
9548 cu->language = pretend_language;
9549 cu->language_defn = language_def (cu->language);
9550
9551 /* Do line number decoding in read_file_scope () */
9552 process_die (cu->dies, cu);
9553
9554 /* For now fudge the Go package. */
9555 if (cu->language == language_go)
9556 fixup_go_packaging (cu);
9557
9558 /* Now that we have processed all the DIEs in the CU, all the types
9559 should be complete, and it should now be safe to compute all of the
9560 physnames. */
9561 compute_delayed_physnames (cu);
9562
9563 if (cu->language == language_rust)
9564 rust_union_quirks (cu);
9565
9566 /* Some compilers don't define a DW_AT_high_pc attribute for the
9567 compilation unit. If the DW_AT_high_pc is missing, synthesize
9568 it, by scanning the DIE's below the compilation unit. */
9569 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9570
9571 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9572 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9573
9574 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9575 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9576 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9577 addrmap to help ensure it has an accurate map of pc values belonging to
9578 this comp unit. */
9579 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9580
9581 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9582 SECT_OFF_TEXT (objfile),
9583 0);
9584
9585 if (cust != NULL)
9586 {
9587 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9588
9589 /* Set symtab language to language from DW_AT_language. If the
9590 compilation is from a C file generated by language preprocessors, do
9591 not set the language if it was already deduced by start_subfile. */
9592 if (!(cu->language == language_c
9593 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9594 COMPUNIT_FILETABS (cust)->language = cu->language;
9595
9596 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9597 produce DW_AT_location with location lists but it can be possibly
9598 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9599 there were bugs in prologue debug info, fixed later in GCC-4.5
9600 by "unwind info for epilogues" patch (which is not directly related).
9601
9602 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9603 needed, it would be wrong due to missing DW_AT_producer there.
9604
9605 Still one can confuse GDB by using non-standard GCC compilation
9606 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9607 */
9608 if (cu->has_loclist && gcc_4_minor >= 5)
9609 cust->locations_valid = 1;
9610
9611 if (gcc_4_minor >= 5)
9612 cust->epilogue_unwind_valid = 1;
9613
9614 cust->call_site_htab = cu->call_site_htab;
9615 }
9616
9617 if (dwarf2_per_objfile->using_index)
9618 per_cu->v.quick->compunit_symtab = cust;
9619 else
9620 {
9621 dwarf2_psymtab *pst = per_cu->v.psymtab;
9622 pst->compunit_symtab = cust;
9623 pst->readin = true;
9624 }
9625
9626 /* Push it for inclusion processing later. */
9627 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9628
9629 /* Not needed any more. */
9630 cu->reset_builder ();
9631 }
9632
9633 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9634 already been loaded into memory. */
9635
9636 static void
9637 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9638 enum language pretend_language)
9639 {
9640 struct dwarf2_cu *cu = per_cu->cu;
9641 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9642 struct objfile *objfile = dwarf2_per_objfile->objfile;
9643 struct compunit_symtab *cust;
9644 struct signatured_type *sig_type;
9645
9646 gdb_assert (per_cu->is_debug_types);
9647 sig_type = (struct signatured_type *) per_cu;
9648
9649 /* Clear the list here in case something was left over. */
9650 cu->method_list.clear ();
9651
9652 cu->language = pretend_language;
9653 cu->language_defn = language_def (cu->language);
9654
9655 /* The symbol tables are set up in read_type_unit_scope. */
9656 process_die (cu->dies, cu);
9657
9658 /* For now fudge the Go package. */
9659 if (cu->language == language_go)
9660 fixup_go_packaging (cu);
9661
9662 /* Now that we have processed all the DIEs in the CU, all the types
9663 should be complete, and it should now be safe to compute all of the
9664 physnames. */
9665 compute_delayed_physnames (cu);
9666
9667 if (cu->language == language_rust)
9668 rust_union_quirks (cu);
9669
9670 /* TUs share symbol tables.
9671 If this is the first TU to use this symtab, complete the construction
9672 of it with end_expandable_symtab. Otherwise, complete the addition of
9673 this TU's symbols to the existing symtab. */
9674 if (sig_type->type_unit_group->compunit_symtab == NULL)
9675 {
9676 buildsym_compunit *builder = cu->get_builder ();
9677 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9678 sig_type->type_unit_group->compunit_symtab = cust;
9679
9680 if (cust != NULL)
9681 {
9682 /* Set symtab language to language from DW_AT_language. If the
9683 compilation is from a C file generated by language preprocessors,
9684 do not set the language if it was already deduced by
9685 start_subfile. */
9686 if (!(cu->language == language_c
9687 && COMPUNIT_FILETABS (cust)->language != language_c))
9688 COMPUNIT_FILETABS (cust)->language = cu->language;
9689 }
9690 }
9691 else
9692 {
9693 cu->get_builder ()->augment_type_symtab ();
9694 cust = sig_type->type_unit_group->compunit_symtab;
9695 }
9696
9697 if (dwarf2_per_objfile->using_index)
9698 per_cu->v.quick->compunit_symtab = cust;
9699 else
9700 {
9701 dwarf2_psymtab *pst = per_cu->v.psymtab;
9702 pst->compunit_symtab = cust;
9703 pst->readin = true;
9704 }
9705
9706 /* Not needed any more. */
9707 cu->reset_builder ();
9708 }
9709
9710 /* Process an imported unit DIE. */
9711
9712 static void
9713 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9714 {
9715 struct attribute *attr;
9716
9717 /* For now we don't handle imported units in type units. */
9718 if (cu->per_cu->is_debug_types)
9719 {
9720 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9721 " supported in type units [in module %s]"),
9722 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9723 }
9724
9725 attr = dwarf2_attr (die, DW_AT_import, cu);
9726 if (attr != NULL)
9727 {
9728 sect_offset sect_off = attr->get_ref_die_offset ();
9729 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9730 dwarf2_per_cu_data *per_cu
9731 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9732 cu->per_cu->dwarf2_per_objfile);
9733
9734 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9735 into another compilation unit, at root level. Regard this as a hint,
9736 and ignore it. */
9737 if (die->parent && die->parent->parent == NULL
9738 && per_cu->unit_type == DW_UT_compile
9739 && per_cu->lang == language_cplus)
9740 return;
9741
9742 /* If necessary, add it to the queue and load its DIEs. */
9743 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9744 load_full_comp_unit (per_cu, false, cu->language);
9745
9746 cu->per_cu->imported_symtabs_push (per_cu);
9747 }
9748 }
9749
9750 /* RAII object that represents a process_die scope: i.e.,
9751 starts/finishes processing a DIE. */
9752 class process_die_scope
9753 {
9754 public:
9755 process_die_scope (die_info *die, dwarf2_cu *cu)
9756 : m_die (die), m_cu (cu)
9757 {
9758 /* We should only be processing DIEs not already in process. */
9759 gdb_assert (!m_die->in_process);
9760 m_die->in_process = true;
9761 }
9762
9763 ~process_die_scope ()
9764 {
9765 m_die->in_process = false;
9766
9767 /* If we're done processing the DIE for the CU that owns the line
9768 header, we don't need the line header anymore. */
9769 if (m_cu->line_header_die_owner == m_die)
9770 {
9771 delete m_cu->line_header;
9772 m_cu->line_header = NULL;
9773 m_cu->line_header_die_owner = NULL;
9774 }
9775 }
9776
9777 private:
9778 die_info *m_die;
9779 dwarf2_cu *m_cu;
9780 };
9781
9782 /* Process a die and its children. */
9783
9784 static void
9785 process_die (struct die_info *die, struct dwarf2_cu *cu)
9786 {
9787 process_die_scope scope (die, cu);
9788
9789 switch (die->tag)
9790 {
9791 case DW_TAG_padding:
9792 break;
9793 case DW_TAG_compile_unit:
9794 case DW_TAG_partial_unit:
9795 read_file_scope (die, cu);
9796 break;
9797 case DW_TAG_type_unit:
9798 read_type_unit_scope (die, cu);
9799 break;
9800 case DW_TAG_subprogram:
9801 /* Nested subprograms in Fortran get a prefix. */
9802 if (cu->language == language_fortran
9803 && die->parent != NULL
9804 && die->parent->tag == DW_TAG_subprogram)
9805 cu->processing_has_namespace_info = true;
9806 /* Fall through. */
9807 case DW_TAG_inlined_subroutine:
9808 read_func_scope (die, cu);
9809 break;
9810 case DW_TAG_lexical_block:
9811 case DW_TAG_try_block:
9812 case DW_TAG_catch_block:
9813 read_lexical_block_scope (die, cu);
9814 break;
9815 case DW_TAG_call_site:
9816 case DW_TAG_GNU_call_site:
9817 read_call_site_scope (die, cu);
9818 break;
9819 case DW_TAG_class_type:
9820 case DW_TAG_interface_type:
9821 case DW_TAG_structure_type:
9822 case DW_TAG_union_type:
9823 process_structure_scope (die, cu);
9824 break;
9825 case DW_TAG_enumeration_type:
9826 process_enumeration_scope (die, cu);
9827 break;
9828
9829 /* These dies have a type, but processing them does not create
9830 a symbol or recurse to process the children. Therefore we can
9831 read them on-demand through read_type_die. */
9832 case DW_TAG_subroutine_type:
9833 case DW_TAG_set_type:
9834 case DW_TAG_array_type:
9835 case DW_TAG_pointer_type:
9836 case DW_TAG_ptr_to_member_type:
9837 case DW_TAG_reference_type:
9838 case DW_TAG_rvalue_reference_type:
9839 case DW_TAG_string_type:
9840 break;
9841
9842 case DW_TAG_base_type:
9843 case DW_TAG_subrange_type:
9844 case DW_TAG_typedef:
9845 /* Add a typedef symbol for the type definition, if it has a
9846 DW_AT_name. */
9847 new_symbol (die, read_type_die (die, cu), cu);
9848 break;
9849 case DW_TAG_common_block:
9850 read_common_block (die, cu);
9851 break;
9852 case DW_TAG_common_inclusion:
9853 break;
9854 case DW_TAG_namespace:
9855 cu->processing_has_namespace_info = true;
9856 read_namespace (die, cu);
9857 break;
9858 case DW_TAG_module:
9859 cu->processing_has_namespace_info = true;
9860 read_module (die, cu);
9861 break;
9862 case DW_TAG_imported_declaration:
9863 cu->processing_has_namespace_info = true;
9864 if (read_namespace_alias (die, cu))
9865 break;
9866 /* The declaration is not a global namespace alias. */
9867 /* Fall through. */
9868 case DW_TAG_imported_module:
9869 cu->processing_has_namespace_info = true;
9870 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9871 || cu->language != language_fortran))
9872 complaint (_("Tag '%s' has unexpected children"),
9873 dwarf_tag_name (die->tag));
9874 read_import_statement (die, cu);
9875 break;
9876
9877 case DW_TAG_imported_unit:
9878 process_imported_unit_die (die, cu);
9879 break;
9880
9881 case DW_TAG_variable:
9882 read_variable (die, cu);
9883 break;
9884
9885 default:
9886 new_symbol (die, NULL, cu);
9887 break;
9888 }
9889 }
9890 \f
9891 /* DWARF name computation. */
9892
9893 /* A helper function for dwarf2_compute_name which determines whether DIE
9894 needs to have the name of the scope prepended to the name listed in the
9895 die. */
9896
9897 static int
9898 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9899 {
9900 struct attribute *attr;
9901
9902 switch (die->tag)
9903 {
9904 case DW_TAG_namespace:
9905 case DW_TAG_typedef:
9906 case DW_TAG_class_type:
9907 case DW_TAG_interface_type:
9908 case DW_TAG_structure_type:
9909 case DW_TAG_union_type:
9910 case DW_TAG_enumeration_type:
9911 case DW_TAG_enumerator:
9912 case DW_TAG_subprogram:
9913 case DW_TAG_inlined_subroutine:
9914 case DW_TAG_member:
9915 case DW_TAG_imported_declaration:
9916 return 1;
9917
9918 case DW_TAG_variable:
9919 case DW_TAG_constant:
9920 /* We only need to prefix "globally" visible variables. These include
9921 any variable marked with DW_AT_external or any variable that
9922 lives in a namespace. [Variables in anonymous namespaces
9923 require prefixing, but they are not DW_AT_external.] */
9924
9925 if (dwarf2_attr (die, DW_AT_specification, cu))
9926 {
9927 struct dwarf2_cu *spec_cu = cu;
9928
9929 return die_needs_namespace (die_specification (die, &spec_cu),
9930 spec_cu);
9931 }
9932
9933 attr = dwarf2_attr (die, DW_AT_external, cu);
9934 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9935 && die->parent->tag != DW_TAG_module)
9936 return 0;
9937 /* A variable in a lexical block of some kind does not need a
9938 namespace, even though in C++ such variables may be external
9939 and have a mangled name. */
9940 if (die->parent->tag == DW_TAG_lexical_block
9941 || die->parent->tag == DW_TAG_try_block
9942 || die->parent->tag == DW_TAG_catch_block
9943 || die->parent->tag == DW_TAG_subprogram)
9944 return 0;
9945 return 1;
9946
9947 default:
9948 return 0;
9949 }
9950 }
9951
9952 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9953 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9954 defined for the given DIE. */
9955
9956 static struct attribute *
9957 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9958 {
9959 struct attribute *attr;
9960
9961 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9962 if (attr == NULL)
9963 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9964
9965 return attr;
9966 }
9967
9968 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9969 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9970 defined for the given DIE. */
9971
9972 static const char *
9973 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9974 {
9975 const char *linkage_name;
9976
9977 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9978 if (linkage_name == NULL)
9979 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9980
9981 return linkage_name;
9982 }
9983
9984 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9985 compute the physname for the object, which include a method's:
9986 - formal parameters (C++),
9987 - receiver type (Go),
9988
9989 The term "physname" is a bit confusing.
9990 For C++, for example, it is the demangled name.
9991 For Go, for example, it's the mangled name.
9992
9993 For Ada, return the DIE's linkage name rather than the fully qualified
9994 name. PHYSNAME is ignored..
9995
9996 The result is allocated on the objfile_obstack and canonicalized. */
9997
9998 static const char *
9999 dwarf2_compute_name (const char *name,
10000 struct die_info *die, struct dwarf2_cu *cu,
10001 int physname)
10002 {
10003 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10004
10005 if (name == NULL)
10006 name = dwarf2_name (die, cu);
10007
10008 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10009 but otherwise compute it by typename_concat inside GDB.
10010 FIXME: Actually this is not really true, or at least not always true.
10011 It's all very confusing. compute_and_set_names doesn't try to demangle
10012 Fortran names because there is no mangling standard. So new_symbol
10013 will set the demangled name to the result of dwarf2_full_name, and it is
10014 the demangled name that GDB uses if it exists. */
10015 if (cu->language == language_ada
10016 || (cu->language == language_fortran && physname))
10017 {
10018 /* For Ada unit, we prefer the linkage name over the name, as
10019 the former contains the exported name, which the user expects
10020 to be able to reference. Ideally, we want the user to be able
10021 to reference this entity using either natural or linkage name,
10022 but we haven't started looking at this enhancement yet. */
10023 const char *linkage_name = dw2_linkage_name (die, cu);
10024
10025 if (linkage_name != NULL)
10026 return linkage_name;
10027 }
10028
10029 /* These are the only languages we know how to qualify names in. */
10030 if (name != NULL
10031 && (cu->language == language_cplus
10032 || cu->language == language_fortran || cu->language == language_d
10033 || cu->language == language_rust))
10034 {
10035 if (die_needs_namespace (die, cu))
10036 {
10037 const char *prefix;
10038 const char *canonical_name = NULL;
10039
10040 string_file buf;
10041
10042 prefix = determine_prefix (die, cu);
10043 if (*prefix != '\0')
10044 {
10045 gdb::unique_xmalloc_ptr<char> prefixed_name
10046 (typename_concat (NULL, prefix, name, physname, cu));
10047
10048 buf.puts (prefixed_name.get ());
10049 }
10050 else
10051 buf.puts (name);
10052
10053 /* Template parameters may be specified in the DIE's DW_AT_name, or
10054 as children with DW_TAG_template_type_param or
10055 DW_TAG_value_type_param. If the latter, add them to the name
10056 here. If the name already has template parameters, then
10057 skip this step; some versions of GCC emit both, and
10058 it is more efficient to use the pre-computed name.
10059
10060 Something to keep in mind about this process: it is very
10061 unlikely, or in some cases downright impossible, to produce
10062 something that will match the mangled name of a function.
10063 If the definition of the function has the same debug info,
10064 we should be able to match up with it anyway. But fallbacks
10065 using the minimal symbol, for instance to find a method
10066 implemented in a stripped copy of libstdc++, will not work.
10067 If we do not have debug info for the definition, we will have to
10068 match them up some other way.
10069
10070 When we do name matching there is a related problem with function
10071 templates; two instantiated function templates are allowed to
10072 differ only by their return types, which we do not add here. */
10073
10074 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10075 {
10076 struct attribute *attr;
10077 struct die_info *child;
10078 int first = 1;
10079
10080 die->building_fullname = 1;
10081
10082 for (child = die->child; child != NULL; child = child->sibling)
10083 {
10084 struct type *type;
10085 LONGEST value;
10086 const gdb_byte *bytes;
10087 struct dwarf2_locexpr_baton *baton;
10088 struct value *v;
10089
10090 if (child->tag != DW_TAG_template_type_param
10091 && child->tag != DW_TAG_template_value_param)
10092 continue;
10093
10094 if (first)
10095 {
10096 buf.puts ("<");
10097 first = 0;
10098 }
10099 else
10100 buf.puts (", ");
10101
10102 attr = dwarf2_attr (child, DW_AT_type, cu);
10103 if (attr == NULL)
10104 {
10105 complaint (_("template parameter missing DW_AT_type"));
10106 buf.puts ("UNKNOWN_TYPE");
10107 continue;
10108 }
10109 type = die_type (child, cu);
10110
10111 if (child->tag == DW_TAG_template_type_param)
10112 {
10113 c_print_type (type, "", &buf, -1, 0, cu->language,
10114 &type_print_raw_options);
10115 continue;
10116 }
10117
10118 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10119 if (attr == NULL)
10120 {
10121 complaint (_("template parameter missing "
10122 "DW_AT_const_value"));
10123 buf.puts ("UNKNOWN_VALUE");
10124 continue;
10125 }
10126
10127 dwarf2_const_value_attr (attr, type, name,
10128 &cu->comp_unit_obstack, cu,
10129 &value, &bytes, &baton);
10130
10131 if (TYPE_NOSIGN (type))
10132 /* GDB prints characters as NUMBER 'CHAR'. If that's
10133 changed, this can use value_print instead. */
10134 c_printchar (value, type, &buf);
10135 else
10136 {
10137 struct value_print_options opts;
10138
10139 if (baton != NULL)
10140 v = dwarf2_evaluate_loc_desc (type, NULL,
10141 baton->data,
10142 baton->size,
10143 baton->per_cu);
10144 else if (bytes != NULL)
10145 {
10146 v = allocate_value (type);
10147 memcpy (value_contents_writeable (v), bytes,
10148 TYPE_LENGTH (type));
10149 }
10150 else
10151 v = value_from_longest (type, value);
10152
10153 /* Specify decimal so that we do not depend on
10154 the radix. */
10155 get_formatted_print_options (&opts, 'd');
10156 opts.raw = 1;
10157 value_print (v, &buf, &opts);
10158 release_value (v);
10159 }
10160 }
10161
10162 die->building_fullname = 0;
10163
10164 if (!first)
10165 {
10166 /* Close the argument list, with a space if necessary
10167 (nested templates). */
10168 if (!buf.empty () && buf.string ().back () == '>')
10169 buf.puts (" >");
10170 else
10171 buf.puts (">");
10172 }
10173 }
10174
10175 /* For C++ methods, append formal parameter type
10176 information, if PHYSNAME. */
10177
10178 if (physname && die->tag == DW_TAG_subprogram
10179 && cu->language == language_cplus)
10180 {
10181 struct type *type = read_type_die (die, cu);
10182
10183 c_type_print_args (type, &buf, 1, cu->language,
10184 &type_print_raw_options);
10185
10186 if (cu->language == language_cplus)
10187 {
10188 /* Assume that an artificial first parameter is
10189 "this", but do not crash if it is not. RealView
10190 marks unnamed (and thus unused) parameters as
10191 artificial; there is no way to differentiate
10192 the two cases. */
10193 if (TYPE_NFIELDS (type) > 0
10194 && TYPE_FIELD_ARTIFICIAL (type, 0)
10195 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10196 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10197 0))))
10198 buf.puts (" const");
10199 }
10200 }
10201
10202 const std::string &intermediate_name = buf.string ();
10203
10204 if (cu->language == language_cplus)
10205 canonical_name
10206 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10207 objfile);
10208
10209 /* If we only computed INTERMEDIATE_NAME, or if
10210 INTERMEDIATE_NAME is already canonical, then we need to
10211 intern it. */
10212 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10213 name = objfile->intern (intermediate_name);
10214 else
10215 name = canonical_name;
10216 }
10217 }
10218
10219 return name;
10220 }
10221
10222 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10223 If scope qualifiers are appropriate they will be added. The result
10224 will be allocated on the storage_obstack, or NULL if the DIE does
10225 not have a name. NAME may either be from a previous call to
10226 dwarf2_name or NULL.
10227
10228 The output string will be canonicalized (if C++). */
10229
10230 static const char *
10231 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10232 {
10233 return dwarf2_compute_name (name, die, cu, 0);
10234 }
10235
10236 /* Construct a physname for the given DIE in CU. NAME may either be
10237 from a previous call to dwarf2_name or NULL. The result will be
10238 allocated on the objfile_objstack or NULL if the DIE does not have a
10239 name.
10240
10241 The output string will be canonicalized (if C++). */
10242
10243 static const char *
10244 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10245 {
10246 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10247 const char *retval, *mangled = NULL, *canon = NULL;
10248 int need_copy = 1;
10249
10250 /* In this case dwarf2_compute_name is just a shortcut not building anything
10251 on its own. */
10252 if (!die_needs_namespace (die, cu))
10253 return dwarf2_compute_name (name, die, cu, 1);
10254
10255 mangled = dw2_linkage_name (die, cu);
10256
10257 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10258 See https://github.com/rust-lang/rust/issues/32925. */
10259 if (cu->language == language_rust && mangled != NULL
10260 && strchr (mangled, '{') != NULL)
10261 mangled = NULL;
10262
10263 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10264 has computed. */
10265 gdb::unique_xmalloc_ptr<char> demangled;
10266 if (mangled != NULL)
10267 {
10268
10269 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10270 {
10271 /* Do nothing (do not demangle the symbol name). */
10272 }
10273 else if (cu->language == language_go)
10274 {
10275 /* This is a lie, but we already lie to the caller new_symbol.
10276 new_symbol assumes we return the mangled name.
10277 This just undoes that lie until things are cleaned up. */
10278 }
10279 else
10280 {
10281 /* Use DMGL_RET_DROP for C++ template functions to suppress
10282 their return type. It is easier for GDB users to search
10283 for such functions as `name(params)' than `long name(params)'.
10284 In such case the minimal symbol names do not match the full
10285 symbol names but for template functions there is never a need
10286 to look up their definition from their declaration so
10287 the only disadvantage remains the minimal symbol variant
10288 `long name(params)' does not have the proper inferior type. */
10289 demangled.reset (gdb_demangle (mangled,
10290 (DMGL_PARAMS | DMGL_ANSI
10291 | DMGL_RET_DROP)));
10292 }
10293 if (demangled)
10294 canon = demangled.get ();
10295 else
10296 {
10297 canon = mangled;
10298 need_copy = 0;
10299 }
10300 }
10301
10302 if (canon == NULL || check_physname)
10303 {
10304 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10305
10306 if (canon != NULL && strcmp (physname, canon) != 0)
10307 {
10308 /* It may not mean a bug in GDB. The compiler could also
10309 compute DW_AT_linkage_name incorrectly. But in such case
10310 GDB would need to be bug-to-bug compatible. */
10311
10312 complaint (_("Computed physname <%s> does not match demangled <%s> "
10313 "(from linkage <%s>) - DIE at %s [in module %s]"),
10314 physname, canon, mangled, sect_offset_str (die->sect_off),
10315 objfile_name (objfile));
10316
10317 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10318 is available here - over computed PHYSNAME. It is safer
10319 against both buggy GDB and buggy compilers. */
10320
10321 retval = canon;
10322 }
10323 else
10324 {
10325 retval = physname;
10326 need_copy = 0;
10327 }
10328 }
10329 else
10330 retval = canon;
10331
10332 if (need_copy)
10333 retval = objfile->intern (retval);
10334
10335 return retval;
10336 }
10337
10338 /* Inspect DIE in CU for a namespace alias. If one exists, record
10339 a new symbol for it.
10340
10341 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10342
10343 static int
10344 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10345 {
10346 struct attribute *attr;
10347
10348 /* If the die does not have a name, this is not a namespace
10349 alias. */
10350 attr = dwarf2_attr (die, DW_AT_name, cu);
10351 if (attr != NULL)
10352 {
10353 int num;
10354 struct die_info *d = die;
10355 struct dwarf2_cu *imported_cu = cu;
10356
10357 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10358 keep inspecting DIEs until we hit the underlying import. */
10359 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10360 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10361 {
10362 attr = dwarf2_attr (d, DW_AT_import, cu);
10363 if (attr == NULL)
10364 break;
10365
10366 d = follow_die_ref (d, attr, &imported_cu);
10367 if (d->tag != DW_TAG_imported_declaration)
10368 break;
10369 }
10370
10371 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10372 {
10373 complaint (_("DIE at %s has too many recursively imported "
10374 "declarations"), sect_offset_str (d->sect_off));
10375 return 0;
10376 }
10377
10378 if (attr != NULL)
10379 {
10380 struct type *type;
10381 sect_offset sect_off = attr->get_ref_die_offset ();
10382
10383 type = get_die_type_at_offset (sect_off, cu->per_cu);
10384 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10385 {
10386 /* This declaration is a global namespace alias. Add
10387 a symbol for it whose type is the aliased namespace. */
10388 new_symbol (die, type, cu);
10389 return 1;
10390 }
10391 }
10392 }
10393
10394 return 0;
10395 }
10396
10397 /* Return the using directives repository (global or local?) to use in the
10398 current context for CU.
10399
10400 For Ada, imported declarations can materialize renamings, which *may* be
10401 global. However it is impossible (for now?) in DWARF to distinguish
10402 "external" imported declarations and "static" ones. As all imported
10403 declarations seem to be static in all other languages, make them all CU-wide
10404 global only in Ada. */
10405
10406 static struct using_direct **
10407 using_directives (struct dwarf2_cu *cu)
10408 {
10409 if (cu->language == language_ada
10410 && cu->get_builder ()->outermost_context_p ())
10411 return cu->get_builder ()->get_global_using_directives ();
10412 else
10413 return cu->get_builder ()->get_local_using_directives ();
10414 }
10415
10416 /* Read the import statement specified by the given die and record it. */
10417
10418 static void
10419 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10420 {
10421 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10422 struct attribute *import_attr;
10423 struct die_info *imported_die, *child_die;
10424 struct dwarf2_cu *imported_cu;
10425 const char *imported_name;
10426 const char *imported_name_prefix;
10427 const char *canonical_name;
10428 const char *import_alias;
10429 const char *imported_declaration = NULL;
10430 const char *import_prefix;
10431 std::vector<const char *> excludes;
10432
10433 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10434 if (import_attr == NULL)
10435 {
10436 complaint (_("Tag '%s' has no DW_AT_import"),
10437 dwarf_tag_name (die->tag));
10438 return;
10439 }
10440
10441 imported_cu = cu;
10442 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10443 imported_name = dwarf2_name (imported_die, imported_cu);
10444 if (imported_name == NULL)
10445 {
10446 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10447
10448 The import in the following code:
10449 namespace A
10450 {
10451 typedef int B;
10452 }
10453
10454 int main ()
10455 {
10456 using A::B;
10457 B b;
10458 return b;
10459 }
10460
10461 ...
10462 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10463 <52> DW_AT_decl_file : 1
10464 <53> DW_AT_decl_line : 6
10465 <54> DW_AT_import : <0x75>
10466 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10467 <59> DW_AT_name : B
10468 <5b> DW_AT_decl_file : 1
10469 <5c> DW_AT_decl_line : 2
10470 <5d> DW_AT_type : <0x6e>
10471 ...
10472 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10473 <76> DW_AT_byte_size : 4
10474 <77> DW_AT_encoding : 5 (signed)
10475
10476 imports the wrong die ( 0x75 instead of 0x58 ).
10477 This case will be ignored until the gcc bug is fixed. */
10478 return;
10479 }
10480
10481 /* Figure out the local name after import. */
10482 import_alias = dwarf2_name (die, cu);
10483
10484 /* Figure out where the statement is being imported to. */
10485 import_prefix = determine_prefix (die, cu);
10486
10487 /* Figure out what the scope of the imported die is and prepend it
10488 to the name of the imported die. */
10489 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10490
10491 if (imported_die->tag != DW_TAG_namespace
10492 && imported_die->tag != DW_TAG_module)
10493 {
10494 imported_declaration = imported_name;
10495 canonical_name = imported_name_prefix;
10496 }
10497 else if (strlen (imported_name_prefix) > 0)
10498 canonical_name = obconcat (&objfile->objfile_obstack,
10499 imported_name_prefix,
10500 (cu->language == language_d ? "." : "::"),
10501 imported_name, (char *) NULL);
10502 else
10503 canonical_name = imported_name;
10504
10505 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10506 for (child_die = die->child; child_die && child_die->tag;
10507 child_die = child_die->sibling)
10508 {
10509 /* DWARF-4: A Fortran use statement with a “rename list” may be
10510 represented by an imported module entry with an import attribute
10511 referring to the module and owned entries corresponding to those
10512 entities that are renamed as part of being imported. */
10513
10514 if (child_die->tag != DW_TAG_imported_declaration)
10515 {
10516 complaint (_("child DW_TAG_imported_declaration expected "
10517 "- DIE at %s [in module %s]"),
10518 sect_offset_str (child_die->sect_off),
10519 objfile_name (objfile));
10520 continue;
10521 }
10522
10523 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10524 if (import_attr == NULL)
10525 {
10526 complaint (_("Tag '%s' has no DW_AT_import"),
10527 dwarf_tag_name (child_die->tag));
10528 continue;
10529 }
10530
10531 imported_cu = cu;
10532 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10533 &imported_cu);
10534 imported_name = dwarf2_name (imported_die, imported_cu);
10535 if (imported_name == NULL)
10536 {
10537 complaint (_("child DW_TAG_imported_declaration has unknown "
10538 "imported name - DIE at %s [in module %s]"),
10539 sect_offset_str (child_die->sect_off),
10540 objfile_name (objfile));
10541 continue;
10542 }
10543
10544 excludes.push_back (imported_name);
10545
10546 process_die (child_die, cu);
10547 }
10548
10549 add_using_directive (using_directives (cu),
10550 import_prefix,
10551 canonical_name,
10552 import_alias,
10553 imported_declaration,
10554 excludes,
10555 0,
10556 &objfile->objfile_obstack);
10557 }
10558
10559 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10560 types, but gives them a size of zero. Starting with version 14,
10561 ICC is compatible with GCC. */
10562
10563 static bool
10564 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10565 {
10566 if (!cu->checked_producer)
10567 check_producer (cu);
10568
10569 return cu->producer_is_icc_lt_14;
10570 }
10571
10572 /* ICC generates a DW_AT_type for C void functions. This was observed on
10573 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10574 which says that void functions should not have a DW_AT_type. */
10575
10576 static bool
10577 producer_is_icc (struct dwarf2_cu *cu)
10578 {
10579 if (!cu->checked_producer)
10580 check_producer (cu);
10581
10582 return cu->producer_is_icc;
10583 }
10584
10585 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10586 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10587 this, it was first present in GCC release 4.3.0. */
10588
10589 static bool
10590 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10591 {
10592 if (!cu->checked_producer)
10593 check_producer (cu);
10594
10595 return cu->producer_is_gcc_lt_4_3;
10596 }
10597
10598 static file_and_directory
10599 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10600 {
10601 file_and_directory res;
10602
10603 /* Find the filename. Do not use dwarf2_name here, since the filename
10604 is not a source language identifier. */
10605 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10606 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10607
10608 if (res.comp_dir == NULL
10609 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10610 && IS_ABSOLUTE_PATH (res.name))
10611 {
10612 res.comp_dir_storage = ldirname (res.name);
10613 if (!res.comp_dir_storage.empty ())
10614 res.comp_dir = res.comp_dir_storage.c_str ();
10615 }
10616 if (res.comp_dir != NULL)
10617 {
10618 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10619 directory, get rid of it. */
10620 const char *cp = strchr (res.comp_dir, ':');
10621
10622 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10623 res.comp_dir = cp + 1;
10624 }
10625
10626 if (res.name == NULL)
10627 res.name = "<unknown>";
10628
10629 return res;
10630 }
10631
10632 /* Handle DW_AT_stmt_list for a compilation unit.
10633 DIE is the DW_TAG_compile_unit die for CU.
10634 COMP_DIR is the compilation directory. LOWPC is passed to
10635 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10636
10637 static void
10638 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10639 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10640 {
10641 struct dwarf2_per_objfile *dwarf2_per_objfile
10642 = cu->per_cu->dwarf2_per_objfile;
10643 struct attribute *attr;
10644 struct line_header line_header_local;
10645 hashval_t line_header_local_hash;
10646 void **slot;
10647 int decode_mapping;
10648
10649 gdb_assert (! cu->per_cu->is_debug_types);
10650
10651 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10652 if (attr == NULL)
10653 return;
10654
10655 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10656
10657 /* The line header hash table is only created if needed (it exists to
10658 prevent redundant reading of the line table for partial_units).
10659 If we're given a partial_unit, we'll need it. If we're given a
10660 compile_unit, then use the line header hash table if it's already
10661 created, but don't create one just yet. */
10662
10663 if (dwarf2_per_objfile->line_header_hash == NULL
10664 && die->tag == DW_TAG_partial_unit)
10665 {
10666 dwarf2_per_objfile->line_header_hash
10667 .reset (htab_create_alloc (127, line_header_hash_voidp,
10668 line_header_eq_voidp,
10669 free_line_header_voidp,
10670 xcalloc, xfree));
10671 }
10672
10673 line_header_local.sect_off = line_offset;
10674 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10675 line_header_local_hash = line_header_hash (&line_header_local);
10676 if (dwarf2_per_objfile->line_header_hash != NULL)
10677 {
10678 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10679 &line_header_local,
10680 line_header_local_hash, NO_INSERT);
10681
10682 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10683 is not present in *SLOT (since if there is something in *SLOT then
10684 it will be for a partial_unit). */
10685 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10686 {
10687 gdb_assert (*slot != NULL);
10688 cu->line_header = (struct line_header *) *slot;
10689 return;
10690 }
10691 }
10692
10693 /* dwarf_decode_line_header does not yet provide sufficient information.
10694 We always have to call also dwarf_decode_lines for it. */
10695 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10696 if (lh == NULL)
10697 return;
10698
10699 cu->line_header = lh.release ();
10700 cu->line_header_die_owner = die;
10701
10702 if (dwarf2_per_objfile->line_header_hash == NULL)
10703 slot = NULL;
10704 else
10705 {
10706 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10707 &line_header_local,
10708 line_header_local_hash, INSERT);
10709 gdb_assert (slot != NULL);
10710 }
10711 if (slot != NULL && *slot == NULL)
10712 {
10713 /* This newly decoded line number information unit will be owned
10714 by line_header_hash hash table. */
10715 *slot = cu->line_header;
10716 cu->line_header_die_owner = NULL;
10717 }
10718 else
10719 {
10720 /* We cannot free any current entry in (*slot) as that struct line_header
10721 may be already used by multiple CUs. Create only temporary decoded
10722 line_header for this CU - it may happen at most once for each line
10723 number information unit. And if we're not using line_header_hash
10724 then this is what we want as well. */
10725 gdb_assert (die->tag != DW_TAG_partial_unit);
10726 }
10727 decode_mapping = (die->tag != DW_TAG_partial_unit);
10728 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10729 decode_mapping);
10730
10731 }
10732
10733 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10734
10735 static void
10736 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10737 {
10738 struct dwarf2_per_objfile *dwarf2_per_objfile
10739 = cu->per_cu->dwarf2_per_objfile;
10740 struct objfile *objfile = dwarf2_per_objfile->objfile;
10741 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10742 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10743 CORE_ADDR highpc = ((CORE_ADDR) 0);
10744 struct attribute *attr;
10745 struct die_info *child_die;
10746 CORE_ADDR baseaddr;
10747
10748 prepare_one_comp_unit (cu, die, cu->language);
10749 baseaddr = objfile->text_section_offset ();
10750
10751 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10752
10753 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10754 from finish_block. */
10755 if (lowpc == ((CORE_ADDR) -1))
10756 lowpc = highpc;
10757 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10758
10759 file_and_directory fnd = find_file_and_directory (die, cu);
10760
10761 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10762 standardised yet. As a workaround for the language detection we fall
10763 back to the DW_AT_producer string. */
10764 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10765 cu->language = language_opencl;
10766
10767 /* Similar hack for Go. */
10768 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10769 set_cu_language (DW_LANG_Go, cu);
10770
10771 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10772
10773 /* Decode line number information if present. We do this before
10774 processing child DIEs, so that the line header table is available
10775 for DW_AT_decl_file. */
10776 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10777
10778 /* Process all dies in compilation unit. */
10779 if (die->child != NULL)
10780 {
10781 child_die = die->child;
10782 while (child_die && child_die->tag)
10783 {
10784 process_die (child_die, cu);
10785 child_die = child_die->sibling;
10786 }
10787 }
10788
10789 /* Decode macro information, if present. Dwarf 2 macro information
10790 refers to information in the line number info statement program
10791 header, so we can only read it if we've read the header
10792 successfully. */
10793 attr = dwarf2_attr (die, DW_AT_macros, cu);
10794 if (attr == NULL)
10795 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10796 if (attr && cu->line_header)
10797 {
10798 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10799 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10800
10801 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10802 }
10803 else
10804 {
10805 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10806 if (attr && cu->line_header)
10807 {
10808 unsigned int macro_offset = DW_UNSND (attr);
10809
10810 dwarf_decode_macros (cu, macro_offset, 0);
10811 }
10812 }
10813 }
10814
10815 void
10816 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10817 {
10818 struct type_unit_group *tu_group;
10819 int first_time;
10820 struct attribute *attr;
10821 unsigned int i;
10822 struct signatured_type *sig_type;
10823
10824 gdb_assert (per_cu->is_debug_types);
10825 sig_type = (struct signatured_type *) per_cu;
10826
10827 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10828
10829 /* If we're using .gdb_index (includes -readnow) then
10830 per_cu->type_unit_group may not have been set up yet. */
10831 if (sig_type->type_unit_group == NULL)
10832 sig_type->type_unit_group = get_type_unit_group (this, attr);
10833 tu_group = sig_type->type_unit_group;
10834
10835 /* If we've already processed this stmt_list there's no real need to
10836 do it again, we could fake it and just recreate the part we need
10837 (file name,index -> symtab mapping). If data shows this optimization
10838 is useful we can do it then. */
10839 first_time = tu_group->compunit_symtab == NULL;
10840
10841 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10842 debug info. */
10843 line_header_up lh;
10844 if (attr != NULL)
10845 {
10846 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10847 lh = dwarf_decode_line_header (line_offset, this);
10848 }
10849 if (lh == NULL)
10850 {
10851 if (first_time)
10852 start_symtab ("", NULL, 0);
10853 else
10854 {
10855 gdb_assert (tu_group->symtabs == NULL);
10856 gdb_assert (m_builder == nullptr);
10857 struct compunit_symtab *cust = tu_group->compunit_symtab;
10858 m_builder.reset (new struct buildsym_compunit
10859 (COMPUNIT_OBJFILE (cust), "",
10860 COMPUNIT_DIRNAME (cust),
10861 compunit_language (cust),
10862 0, cust));
10863 }
10864 return;
10865 }
10866
10867 line_header = lh.release ();
10868 line_header_die_owner = die;
10869
10870 if (first_time)
10871 {
10872 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10873
10874 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10875 still initializing it, and our caller (a few levels up)
10876 process_full_type_unit still needs to know if this is the first
10877 time. */
10878
10879 tu_group->symtabs
10880 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10881 struct symtab *, line_header->file_names_size ());
10882
10883 auto &file_names = line_header->file_names ();
10884 for (i = 0; i < file_names.size (); ++i)
10885 {
10886 file_entry &fe = file_names[i];
10887 dwarf2_start_subfile (this, fe.name,
10888 fe.include_dir (line_header));
10889 buildsym_compunit *b = get_builder ();
10890 if (b->get_current_subfile ()->symtab == NULL)
10891 {
10892 /* NOTE: start_subfile will recognize when it's been
10893 passed a file it has already seen. So we can't
10894 assume there's a simple mapping from
10895 cu->line_header->file_names to subfiles, plus
10896 cu->line_header->file_names may contain dups. */
10897 b->get_current_subfile ()->symtab
10898 = allocate_symtab (cust, b->get_current_subfile ()->name);
10899 }
10900
10901 fe.symtab = b->get_current_subfile ()->symtab;
10902 tu_group->symtabs[i] = fe.symtab;
10903 }
10904 }
10905 else
10906 {
10907 gdb_assert (m_builder == nullptr);
10908 struct compunit_symtab *cust = tu_group->compunit_symtab;
10909 m_builder.reset (new struct buildsym_compunit
10910 (COMPUNIT_OBJFILE (cust), "",
10911 COMPUNIT_DIRNAME (cust),
10912 compunit_language (cust),
10913 0, cust));
10914
10915 auto &file_names = line_header->file_names ();
10916 for (i = 0; i < file_names.size (); ++i)
10917 {
10918 file_entry &fe = file_names[i];
10919 fe.symtab = tu_group->symtabs[i];
10920 }
10921 }
10922
10923 /* The main symtab is allocated last. Type units don't have DW_AT_name
10924 so they don't have a "real" (so to speak) symtab anyway.
10925 There is later code that will assign the main symtab to all symbols
10926 that don't have one. We need to handle the case of a symbol with a
10927 missing symtab (DW_AT_decl_file) anyway. */
10928 }
10929
10930 /* Process DW_TAG_type_unit.
10931 For TUs we want to skip the first top level sibling if it's not the
10932 actual type being defined by this TU. In this case the first top
10933 level sibling is there to provide context only. */
10934
10935 static void
10936 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10937 {
10938 struct die_info *child_die;
10939
10940 prepare_one_comp_unit (cu, die, language_minimal);
10941
10942 /* Initialize (or reinitialize) the machinery for building symtabs.
10943 We do this before processing child DIEs, so that the line header table
10944 is available for DW_AT_decl_file. */
10945 cu->setup_type_unit_groups (die);
10946
10947 if (die->child != NULL)
10948 {
10949 child_die = die->child;
10950 while (child_die && child_die->tag)
10951 {
10952 process_die (child_die, cu);
10953 child_die = child_die->sibling;
10954 }
10955 }
10956 }
10957 \f
10958 /* DWO/DWP files.
10959
10960 http://gcc.gnu.org/wiki/DebugFission
10961 http://gcc.gnu.org/wiki/DebugFissionDWP
10962
10963 To simplify handling of both DWO files ("object" files with the DWARF info)
10964 and DWP files (a file with the DWOs packaged up into one file), we treat
10965 DWP files as having a collection of virtual DWO files. */
10966
10967 static hashval_t
10968 hash_dwo_file (const void *item)
10969 {
10970 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10971 hashval_t hash;
10972
10973 hash = htab_hash_string (dwo_file->dwo_name);
10974 if (dwo_file->comp_dir != NULL)
10975 hash += htab_hash_string (dwo_file->comp_dir);
10976 return hash;
10977 }
10978
10979 static int
10980 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10981 {
10982 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10983 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10984
10985 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10986 return 0;
10987 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10988 return lhs->comp_dir == rhs->comp_dir;
10989 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10990 }
10991
10992 /* Allocate a hash table for DWO files. */
10993
10994 static htab_up
10995 allocate_dwo_file_hash_table ()
10996 {
10997 auto delete_dwo_file = [] (void *item)
10998 {
10999 struct dwo_file *dwo_file = (struct dwo_file *) item;
11000
11001 delete dwo_file;
11002 };
11003
11004 return htab_up (htab_create_alloc (41,
11005 hash_dwo_file,
11006 eq_dwo_file,
11007 delete_dwo_file,
11008 xcalloc, xfree));
11009 }
11010
11011 /* Lookup DWO file DWO_NAME. */
11012
11013 static void **
11014 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11015 const char *dwo_name,
11016 const char *comp_dir)
11017 {
11018 struct dwo_file find_entry;
11019 void **slot;
11020
11021 if (dwarf2_per_objfile->dwo_files == NULL)
11022 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
11023
11024 find_entry.dwo_name = dwo_name;
11025 find_entry.comp_dir = comp_dir;
11026 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11027 INSERT);
11028
11029 return slot;
11030 }
11031
11032 static hashval_t
11033 hash_dwo_unit (const void *item)
11034 {
11035 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11036
11037 /* This drops the top 32 bits of the id, but is ok for a hash. */
11038 return dwo_unit->signature;
11039 }
11040
11041 static int
11042 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11043 {
11044 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11045 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11046
11047 /* The signature is assumed to be unique within the DWO file.
11048 So while object file CU dwo_id's always have the value zero,
11049 that's OK, assuming each object file DWO file has only one CU,
11050 and that's the rule for now. */
11051 return lhs->signature == rhs->signature;
11052 }
11053
11054 /* Allocate a hash table for DWO CUs,TUs.
11055 There is one of these tables for each of CUs,TUs for each DWO file. */
11056
11057 static htab_up
11058 allocate_dwo_unit_table ()
11059 {
11060 /* Start out with a pretty small number.
11061 Generally DWO files contain only one CU and maybe some TUs. */
11062 return htab_up (htab_create_alloc (3,
11063 hash_dwo_unit,
11064 eq_dwo_unit,
11065 NULL, xcalloc, xfree));
11066 }
11067
11068 /* die_reader_func for create_dwo_cu. */
11069
11070 static void
11071 create_dwo_cu_reader (const struct die_reader_specs *reader,
11072 const gdb_byte *info_ptr,
11073 struct die_info *comp_unit_die,
11074 struct dwo_file *dwo_file,
11075 struct dwo_unit *dwo_unit)
11076 {
11077 struct dwarf2_cu *cu = reader->cu;
11078 sect_offset sect_off = cu->per_cu->sect_off;
11079 struct dwarf2_section_info *section = cu->per_cu->section;
11080
11081 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11082 if (!signature.has_value ())
11083 {
11084 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11085 " its dwo_id [in module %s]"),
11086 sect_offset_str (sect_off), dwo_file->dwo_name);
11087 return;
11088 }
11089
11090 dwo_unit->dwo_file = dwo_file;
11091 dwo_unit->signature = *signature;
11092 dwo_unit->section = section;
11093 dwo_unit->sect_off = sect_off;
11094 dwo_unit->length = cu->per_cu->length;
11095
11096 if (dwarf_read_debug)
11097 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11098 sect_offset_str (sect_off),
11099 hex_string (dwo_unit->signature));
11100 }
11101
11102 /* Create the dwo_units for the CUs in a DWO_FILE.
11103 Note: This function processes DWO files only, not DWP files. */
11104
11105 static void
11106 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11107 dwarf2_cu *cu, struct dwo_file &dwo_file,
11108 dwarf2_section_info &section, htab_up &cus_htab)
11109 {
11110 struct objfile *objfile = dwarf2_per_objfile->objfile;
11111 const gdb_byte *info_ptr, *end_ptr;
11112
11113 section.read (objfile);
11114 info_ptr = section.buffer;
11115
11116 if (info_ptr == NULL)
11117 return;
11118
11119 if (dwarf_read_debug)
11120 {
11121 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11122 section.get_name (),
11123 section.get_file_name ());
11124 }
11125
11126 end_ptr = info_ptr + section.size;
11127 while (info_ptr < end_ptr)
11128 {
11129 struct dwarf2_per_cu_data per_cu;
11130 struct dwo_unit read_unit {};
11131 struct dwo_unit *dwo_unit;
11132 void **slot;
11133 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11134
11135 memset (&per_cu, 0, sizeof (per_cu));
11136 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11137 per_cu.is_debug_types = 0;
11138 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11139 per_cu.section = &section;
11140
11141 cutu_reader reader (&per_cu, cu, &dwo_file);
11142 if (!reader.dummy_p)
11143 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11144 &dwo_file, &read_unit);
11145 info_ptr += per_cu.length;
11146
11147 // If the unit could not be parsed, skip it.
11148 if (read_unit.dwo_file == NULL)
11149 continue;
11150
11151 if (cus_htab == NULL)
11152 cus_htab = allocate_dwo_unit_table ();
11153
11154 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11155 *dwo_unit = read_unit;
11156 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11157 gdb_assert (slot != NULL);
11158 if (*slot != NULL)
11159 {
11160 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11161 sect_offset dup_sect_off = dup_cu->sect_off;
11162
11163 complaint (_("debug cu entry at offset %s is duplicate to"
11164 " the entry at offset %s, signature %s"),
11165 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11166 hex_string (dwo_unit->signature));
11167 }
11168 *slot = (void *)dwo_unit;
11169 }
11170 }
11171
11172 /* DWP file .debug_{cu,tu}_index section format:
11173 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11174
11175 DWP Version 1:
11176
11177 Both index sections have the same format, and serve to map a 64-bit
11178 signature to a set of section numbers. Each section begins with a header,
11179 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11180 indexes, and a pool of 32-bit section numbers. The index sections will be
11181 aligned at 8-byte boundaries in the file.
11182
11183 The index section header consists of:
11184
11185 V, 32 bit version number
11186 -, 32 bits unused
11187 N, 32 bit number of compilation units or type units in the index
11188 M, 32 bit number of slots in the hash table
11189
11190 Numbers are recorded using the byte order of the application binary.
11191
11192 The hash table begins at offset 16 in the section, and consists of an array
11193 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11194 order of the application binary). Unused slots in the hash table are 0.
11195 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11196
11197 The parallel table begins immediately after the hash table
11198 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11199 array of 32-bit indexes (using the byte order of the application binary),
11200 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11201 table contains a 32-bit index into the pool of section numbers. For unused
11202 hash table slots, the corresponding entry in the parallel table will be 0.
11203
11204 The pool of section numbers begins immediately following the hash table
11205 (at offset 16 + 12 * M from the beginning of the section). The pool of
11206 section numbers consists of an array of 32-bit words (using the byte order
11207 of the application binary). Each item in the array is indexed starting
11208 from 0. The hash table entry provides the index of the first section
11209 number in the set. Additional section numbers in the set follow, and the
11210 set is terminated by a 0 entry (section number 0 is not used in ELF).
11211
11212 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11213 section must be the first entry in the set, and the .debug_abbrev.dwo must
11214 be the second entry. Other members of the set may follow in any order.
11215
11216 ---
11217
11218 DWP Version 2:
11219
11220 DWP Version 2 combines all the .debug_info, etc. sections into one,
11221 and the entries in the index tables are now offsets into these sections.
11222 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11223 section.
11224
11225 Index Section Contents:
11226 Header
11227 Hash Table of Signatures dwp_hash_table.hash_table
11228 Parallel Table of Indices dwp_hash_table.unit_table
11229 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11230 Table of Section Sizes dwp_hash_table.v2.sizes
11231
11232 The index section header consists of:
11233
11234 V, 32 bit version number
11235 L, 32 bit number of columns in the table of section offsets
11236 N, 32 bit number of compilation units or type units in the index
11237 M, 32 bit number of slots in the hash table
11238
11239 Numbers are recorded using the byte order of the application binary.
11240
11241 The hash table has the same format as version 1.
11242 The parallel table of indices has the same format as version 1,
11243 except that the entries are origin-1 indices into the table of sections
11244 offsets and the table of section sizes.
11245
11246 The table of offsets begins immediately following the parallel table
11247 (at offset 16 + 12 * M from the beginning of the section). The table is
11248 a two-dimensional array of 32-bit words (using the byte order of the
11249 application binary), with L columns and N+1 rows, in row-major order.
11250 Each row in the array is indexed starting from 0. The first row provides
11251 a key to the remaining rows: each column in this row provides an identifier
11252 for a debug section, and the offsets in the same column of subsequent rows
11253 refer to that section. The section identifiers are:
11254
11255 DW_SECT_INFO 1 .debug_info.dwo
11256 DW_SECT_TYPES 2 .debug_types.dwo
11257 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11258 DW_SECT_LINE 4 .debug_line.dwo
11259 DW_SECT_LOC 5 .debug_loc.dwo
11260 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11261 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11262 DW_SECT_MACRO 8 .debug_macro.dwo
11263
11264 The offsets provided by the CU and TU index sections are the base offsets
11265 for the contributions made by each CU or TU to the corresponding section
11266 in the package file. Each CU and TU header contains an abbrev_offset
11267 field, used to find the abbreviations table for that CU or TU within the
11268 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11269 be interpreted as relative to the base offset given in the index section.
11270 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11271 should be interpreted as relative to the base offset for .debug_line.dwo,
11272 and offsets into other debug sections obtained from DWARF attributes should
11273 also be interpreted as relative to the corresponding base offset.
11274
11275 The table of sizes begins immediately following the table of offsets.
11276 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11277 with L columns and N rows, in row-major order. Each row in the array is
11278 indexed starting from 1 (row 0 is shared by the two tables).
11279
11280 ---
11281
11282 Hash table lookup is handled the same in version 1 and 2:
11283
11284 We assume that N and M will not exceed 2^32 - 1.
11285 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11286
11287 Given a 64-bit compilation unit signature or a type signature S, an entry
11288 in the hash table is located as follows:
11289
11290 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11291 the low-order k bits all set to 1.
11292
11293 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11294
11295 3) If the hash table entry at index H matches the signature, use that
11296 entry. If the hash table entry at index H is unused (all zeroes),
11297 terminate the search: the signature is not present in the table.
11298
11299 4) Let H = (H + H') modulo M. Repeat at Step 3.
11300
11301 Because M > N and H' and M are relatively prime, the search is guaranteed
11302 to stop at an unused slot or find the match. */
11303
11304 /* Create a hash table to map DWO IDs to their CU/TU entry in
11305 .debug_{info,types}.dwo in DWP_FILE.
11306 Returns NULL if there isn't one.
11307 Note: This function processes DWP files only, not DWO files. */
11308
11309 static struct dwp_hash_table *
11310 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11311 struct dwp_file *dwp_file, int is_debug_types)
11312 {
11313 struct objfile *objfile = dwarf2_per_objfile->objfile;
11314 bfd *dbfd = dwp_file->dbfd.get ();
11315 const gdb_byte *index_ptr, *index_end;
11316 struct dwarf2_section_info *index;
11317 uint32_t version, nr_columns, nr_units, nr_slots;
11318 struct dwp_hash_table *htab;
11319
11320 if (is_debug_types)
11321 index = &dwp_file->sections.tu_index;
11322 else
11323 index = &dwp_file->sections.cu_index;
11324
11325 if (index->empty ())
11326 return NULL;
11327 index->read (objfile);
11328
11329 index_ptr = index->buffer;
11330 index_end = index_ptr + index->size;
11331
11332 version = read_4_bytes (dbfd, index_ptr);
11333 index_ptr += 4;
11334 if (version == 2)
11335 nr_columns = read_4_bytes (dbfd, index_ptr);
11336 else
11337 nr_columns = 0;
11338 index_ptr += 4;
11339 nr_units = read_4_bytes (dbfd, index_ptr);
11340 index_ptr += 4;
11341 nr_slots = read_4_bytes (dbfd, index_ptr);
11342 index_ptr += 4;
11343
11344 if (version != 1 && version != 2)
11345 {
11346 error (_("Dwarf Error: unsupported DWP file version (%s)"
11347 " [in module %s]"),
11348 pulongest (version), dwp_file->name);
11349 }
11350 if (nr_slots != (nr_slots & -nr_slots))
11351 {
11352 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11353 " is not power of 2 [in module %s]"),
11354 pulongest (nr_slots), dwp_file->name);
11355 }
11356
11357 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11358 htab->version = version;
11359 htab->nr_columns = nr_columns;
11360 htab->nr_units = nr_units;
11361 htab->nr_slots = nr_slots;
11362 htab->hash_table = index_ptr;
11363 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11364
11365 /* Exit early if the table is empty. */
11366 if (nr_slots == 0 || nr_units == 0
11367 || (version == 2 && nr_columns == 0))
11368 {
11369 /* All must be zero. */
11370 if (nr_slots != 0 || nr_units != 0
11371 || (version == 2 && nr_columns != 0))
11372 {
11373 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11374 " all zero [in modules %s]"),
11375 dwp_file->name);
11376 }
11377 return htab;
11378 }
11379
11380 if (version == 1)
11381 {
11382 htab->section_pool.v1.indices =
11383 htab->unit_table + sizeof (uint32_t) * nr_slots;
11384 /* It's harder to decide whether the section is too small in v1.
11385 V1 is deprecated anyway so we punt. */
11386 }
11387 else
11388 {
11389 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11390 int *ids = htab->section_pool.v2.section_ids;
11391 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11392 /* Reverse map for error checking. */
11393 int ids_seen[DW_SECT_MAX + 1];
11394 int i;
11395
11396 if (nr_columns < 2)
11397 {
11398 error (_("Dwarf Error: bad DWP hash table, too few columns"
11399 " in section table [in module %s]"),
11400 dwp_file->name);
11401 }
11402 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11403 {
11404 error (_("Dwarf Error: bad DWP hash table, too many columns"
11405 " in section table [in module %s]"),
11406 dwp_file->name);
11407 }
11408 memset (ids, 255, sizeof_ids);
11409 memset (ids_seen, 255, sizeof (ids_seen));
11410 for (i = 0; i < nr_columns; ++i)
11411 {
11412 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11413
11414 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11415 {
11416 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11417 " in section table [in module %s]"),
11418 id, dwp_file->name);
11419 }
11420 if (ids_seen[id] != -1)
11421 {
11422 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11423 " id %d in section table [in module %s]"),
11424 id, dwp_file->name);
11425 }
11426 ids_seen[id] = i;
11427 ids[i] = id;
11428 }
11429 /* Must have exactly one info or types section. */
11430 if (((ids_seen[DW_SECT_INFO] != -1)
11431 + (ids_seen[DW_SECT_TYPES] != -1))
11432 != 1)
11433 {
11434 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11435 " DWO info/types section [in module %s]"),
11436 dwp_file->name);
11437 }
11438 /* Must have an abbrev section. */
11439 if (ids_seen[DW_SECT_ABBREV] == -1)
11440 {
11441 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11442 " section [in module %s]"),
11443 dwp_file->name);
11444 }
11445 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11446 htab->section_pool.v2.sizes =
11447 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11448 * nr_units * nr_columns);
11449 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11450 * nr_units * nr_columns))
11451 > index_end)
11452 {
11453 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11454 " [in module %s]"),
11455 dwp_file->name);
11456 }
11457 }
11458
11459 return htab;
11460 }
11461
11462 /* Update SECTIONS with the data from SECTP.
11463
11464 This function is like the other "locate" section routines that are
11465 passed to bfd_map_over_sections, but in this context the sections to
11466 read comes from the DWP V1 hash table, not the full ELF section table.
11467
11468 The result is non-zero for success, or zero if an error was found. */
11469
11470 static int
11471 locate_v1_virtual_dwo_sections (asection *sectp,
11472 struct virtual_v1_dwo_sections *sections)
11473 {
11474 const struct dwop_section_names *names = &dwop_section_names;
11475
11476 if (section_is_p (sectp->name, &names->abbrev_dwo))
11477 {
11478 /* There can be only one. */
11479 if (sections->abbrev.s.section != NULL)
11480 return 0;
11481 sections->abbrev.s.section = sectp;
11482 sections->abbrev.size = bfd_section_size (sectp);
11483 }
11484 else if (section_is_p (sectp->name, &names->info_dwo)
11485 || section_is_p (sectp->name, &names->types_dwo))
11486 {
11487 /* There can be only one. */
11488 if (sections->info_or_types.s.section != NULL)
11489 return 0;
11490 sections->info_or_types.s.section = sectp;
11491 sections->info_or_types.size = bfd_section_size (sectp);
11492 }
11493 else if (section_is_p (sectp->name, &names->line_dwo))
11494 {
11495 /* There can be only one. */
11496 if (sections->line.s.section != NULL)
11497 return 0;
11498 sections->line.s.section = sectp;
11499 sections->line.size = bfd_section_size (sectp);
11500 }
11501 else if (section_is_p (sectp->name, &names->loc_dwo))
11502 {
11503 /* There can be only one. */
11504 if (sections->loc.s.section != NULL)
11505 return 0;
11506 sections->loc.s.section = sectp;
11507 sections->loc.size = bfd_section_size (sectp);
11508 }
11509 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11510 {
11511 /* There can be only one. */
11512 if (sections->macinfo.s.section != NULL)
11513 return 0;
11514 sections->macinfo.s.section = sectp;
11515 sections->macinfo.size = bfd_section_size (sectp);
11516 }
11517 else if (section_is_p (sectp->name, &names->macro_dwo))
11518 {
11519 /* There can be only one. */
11520 if (sections->macro.s.section != NULL)
11521 return 0;
11522 sections->macro.s.section = sectp;
11523 sections->macro.size = bfd_section_size (sectp);
11524 }
11525 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11526 {
11527 /* There can be only one. */
11528 if (sections->str_offsets.s.section != NULL)
11529 return 0;
11530 sections->str_offsets.s.section = sectp;
11531 sections->str_offsets.size = bfd_section_size (sectp);
11532 }
11533 else
11534 {
11535 /* No other kind of section is valid. */
11536 return 0;
11537 }
11538
11539 return 1;
11540 }
11541
11542 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11543 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11544 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11545 This is for DWP version 1 files. */
11546
11547 static struct dwo_unit *
11548 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11549 struct dwp_file *dwp_file,
11550 uint32_t unit_index,
11551 const char *comp_dir,
11552 ULONGEST signature, int is_debug_types)
11553 {
11554 struct objfile *objfile = dwarf2_per_objfile->objfile;
11555 const struct dwp_hash_table *dwp_htab =
11556 is_debug_types ? dwp_file->tus : dwp_file->cus;
11557 bfd *dbfd = dwp_file->dbfd.get ();
11558 const char *kind = is_debug_types ? "TU" : "CU";
11559 struct dwo_file *dwo_file;
11560 struct dwo_unit *dwo_unit;
11561 struct virtual_v1_dwo_sections sections;
11562 void **dwo_file_slot;
11563 int i;
11564
11565 gdb_assert (dwp_file->version == 1);
11566
11567 if (dwarf_read_debug)
11568 {
11569 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11570 kind,
11571 pulongest (unit_index), hex_string (signature),
11572 dwp_file->name);
11573 }
11574
11575 /* Fetch the sections of this DWO unit.
11576 Put a limit on the number of sections we look for so that bad data
11577 doesn't cause us to loop forever. */
11578
11579 #define MAX_NR_V1_DWO_SECTIONS \
11580 (1 /* .debug_info or .debug_types */ \
11581 + 1 /* .debug_abbrev */ \
11582 + 1 /* .debug_line */ \
11583 + 1 /* .debug_loc */ \
11584 + 1 /* .debug_str_offsets */ \
11585 + 1 /* .debug_macro or .debug_macinfo */ \
11586 + 1 /* trailing zero */)
11587
11588 memset (&sections, 0, sizeof (sections));
11589
11590 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11591 {
11592 asection *sectp;
11593 uint32_t section_nr =
11594 read_4_bytes (dbfd,
11595 dwp_htab->section_pool.v1.indices
11596 + (unit_index + i) * sizeof (uint32_t));
11597
11598 if (section_nr == 0)
11599 break;
11600 if (section_nr >= dwp_file->num_sections)
11601 {
11602 error (_("Dwarf Error: bad DWP hash table, section number too large"
11603 " [in module %s]"),
11604 dwp_file->name);
11605 }
11606
11607 sectp = dwp_file->elf_sections[section_nr];
11608 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11609 {
11610 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11611 " [in module %s]"),
11612 dwp_file->name);
11613 }
11614 }
11615
11616 if (i < 2
11617 || sections.info_or_types.empty ()
11618 || sections.abbrev.empty ())
11619 {
11620 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11621 " [in module %s]"),
11622 dwp_file->name);
11623 }
11624 if (i == MAX_NR_V1_DWO_SECTIONS)
11625 {
11626 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11627 " [in module %s]"),
11628 dwp_file->name);
11629 }
11630
11631 /* It's easier for the rest of the code if we fake a struct dwo_file and
11632 have dwo_unit "live" in that. At least for now.
11633
11634 The DWP file can be made up of a random collection of CUs and TUs.
11635 However, for each CU + set of TUs that came from the same original DWO
11636 file, we can combine them back into a virtual DWO file to save space
11637 (fewer struct dwo_file objects to allocate). Remember that for really
11638 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11639
11640 std::string virtual_dwo_name =
11641 string_printf ("virtual-dwo/%d-%d-%d-%d",
11642 sections.abbrev.get_id (),
11643 sections.line.get_id (),
11644 sections.loc.get_id (),
11645 sections.str_offsets.get_id ());
11646 /* Can we use an existing virtual DWO file? */
11647 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11648 virtual_dwo_name.c_str (),
11649 comp_dir);
11650 /* Create one if necessary. */
11651 if (*dwo_file_slot == NULL)
11652 {
11653 if (dwarf_read_debug)
11654 {
11655 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11656 virtual_dwo_name.c_str ());
11657 }
11658 dwo_file = new struct dwo_file;
11659 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11660 dwo_file->comp_dir = comp_dir;
11661 dwo_file->sections.abbrev = sections.abbrev;
11662 dwo_file->sections.line = sections.line;
11663 dwo_file->sections.loc = sections.loc;
11664 dwo_file->sections.macinfo = sections.macinfo;
11665 dwo_file->sections.macro = sections.macro;
11666 dwo_file->sections.str_offsets = sections.str_offsets;
11667 /* The "str" section is global to the entire DWP file. */
11668 dwo_file->sections.str = dwp_file->sections.str;
11669 /* The info or types section is assigned below to dwo_unit,
11670 there's no need to record it in dwo_file.
11671 Also, we can't simply record type sections in dwo_file because
11672 we record a pointer into the vector in dwo_unit. As we collect more
11673 types we'll grow the vector and eventually have to reallocate space
11674 for it, invalidating all copies of pointers into the previous
11675 contents. */
11676 *dwo_file_slot = dwo_file;
11677 }
11678 else
11679 {
11680 if (dwarf_read_debug)
11681 {
11682 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11683 virtual_dwo_name.c_str ());
11684 }
11685 dwo_file = (struct dwo_file *) *dwo_file_slot;
11686 }
11687
11688 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11689 dwo_unit->dwo_file = dwo_file;
11690 dwo_unit->signature = signature;
11691 dwo_unit->section =
11692 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11693 *dwo_unit->section = sections.info_or_types;
11694 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11695
11696 return dwo_unit;
11697 }
11698
11699 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11700 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11701 piece within that section used by a TU/CU, return a virtual section
11702 of just that piece. */
11703
11704 static struct dwarf2_section_info
11705 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11706 struct dwarf2_section_info *section,
11707 bfd_size_type offset, bfd_size_type size)
11708 {
11709 struct dwarf2_section_info result;
11710 asection *sectp;
11711
11712 gdb_assert (section != NULL);
11713 gdb_assert (!section->is_virtual);
11714
11715 memset (&result, 0, sizeof (result));
11716 result.s.containing_section = section;
11717 result.is_virtual = true;
11718
11719 if (size == 0)
11720 return result;
11721
11722 sectp = section->get_bfd_section ();
11723
11724 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11725 bounds of the real section. This is a pretty-rare event, so just
11726 flag an error (easier) instead of a warning and trying to cope. */
11727 if (sectp == NULL
11728 || offset + size > bfd_section_size (sectp))
11729 {
11730 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11731 " in section %s [in module %s]"),
11732 sectp ? bfd_section_name (sectp) : "<unknown>",
11733 objfile_name (dwarf2_per_objfile->objfile));
11734 }
11735
11736 result.virtual_offset = offset;
11737 result.size = size;
11738 return result;
11739 }
11740
11741 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11742 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11743 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11744 This is for DWP version 2 files. */
11745
11746 static struct dwo_unit *
11747 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11748 struct dwp_file *dwp_file,
11749 uint32_t unit_index,
11750 const char *comp_dir,
11751 ULONGEST signature, int is_debug_types)
11752 {
11753 struct objfile *objfile = dwarf2_per_objfile->objfile;
11754 const struct dwp_hash_table *dwp_htab =
11755 is_debug_types ? dwp_file->tus : dwp_file->cus;
11756 bfd *dbfd = dwp_file->dbfd.get ();
11757 const char *kind = is_debug_types ? "TU" : "CU";
11758 struct dwo_file *dwo_file;
11759 struct dwo_unit *dwo_unit;
11760 struct virtual_v2_dwo_sections sections;
11761 void **dwo_file_slot;
11762 int i;
11763
11764 gdb_assert (dwp_file->version == 2);
11765
11766 if (dwarf_read_debug)
11767 {
11768 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11769 kind,
11770 pulongest (unit_index), hex_string (signature),
11771 dwp_file->name);
11772 }
11773
11774 /* Fetch the section offsets of this DWO unit. */
11775
11776 memset (&sections, 0, sizeof (sections));
11777
11778 for (i = 0; i < dwp_htab->nr_columns; ++i)
11779 {
11780 uint32_t offset = read_4_bytes (dbfd,
11781 dwp_htab->section_pool.v2.offsets
11782 + (((unit_index - 1) * dwp_htab->nr_columns
11783 + i)
11784 * sizeof (uint32_t)));
11785 uint32_t size = read_4_bytes (dbfd,
11786 dwp_htab->section_pool.v2.sizes
11787 + (((unit_index - 1) * dwp_htab->nr_columns
11788 + i)
11789 * sizeof (uint32_t)));
11790
11791 switch (dwp_htab->section_pool.v2.section_ids[i])
11792 {
11793 case DW_SECT_INFO:
11794 case DW_SECT_TYPES:
11795 sections.info_or_types_offset = offset;
11796 sections.info_or_types_size = size;
11797 break;
11798 case DW_SECT_ABBREV:
11799 sections.abbrev_offset = offset;
11800 sections.abbrev_size = size;
11801 break;
11802 case DW_SECT_LINE:
11803 sections.line_offset = offset;
11804 sections.line_size = size;
11805 break;
11806 case DW_SECT_LOC:
11807 sections.loc_offset = offset;
11808 sections.loc_size = size;
11809 break;
11810 case DW_SECT_STR_OFFSETS:
11811 sections.str_offsets_offset = offset;
11812 sections.str_offsets_size = size;
11813 break;
11814 case DW_SECT_MACINFO:
11815 sections.macinfo_offset = offset;
11816 sections.macinfo_size = size;
11817 break;
11818 case DW_SECT_MACRO:
11819 sections.macro_offset = offset;
11820 sections.macro_size = size;
11821 break;
11822 }
11823 }
11824
11825 /* It's easier for the rest of the code if we fake a struct dwo_file and
11826 have dwo_unit "live" in that. At least for now.
11827
11828 The DWP file can be made up of a random collection of CUs and TUs.
11829 However, for each CU + set of TUs that came from the same original DWO
11830 file, we can combine them back into a virtual DWO file to save space
11831 (fewer struct dwo_file objects to allocate). Remember that for really
11832 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11833
11834 std::string virtual_dwo_name =
11835 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11836 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11837 (long) (sections.line_size ? sections.line_offset : 0),
11838 (long) (sections.loc_size ? sections.loc_offset : 0),
11839 (long) (sections.str_offsets_size
11840 ? sections.str_offsets_offset : 0));
11841 /* Can we use an existing virtual DWO file? */
11842 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11843 virtual_dwo_name.c_str (),
11844 comp_dir);
11845 /* Create one if necessary. */
11846 if (*dwo_file_slot == NULL)
11847 {
11848 if (dwarf_read_debug)
11849 {
11850 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11851 virtual_dwo_name.c_str ());
11852 }
11853 dwo_file = new struct dwo_file;
11854 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11855 dwo_file->comp_dir = comp_dir;
11856 dwo_file->sections.abbrev =
11857 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11858 sections.abbrev_offset, sections.abbrev_size);
11859 dwo_file->sections.line =
11860 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11861 sections.line_offset, sections.line_size);
11862 dwo_file->sections.loc =
11863 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11864 sections.loc_offset, sections.loc_size);
11865 dwo_file->sections.macinfo =
11866 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11867 sections.macinfo_offset, sections.macinfo_size);
11868 dwo_file->sections.macro =
11869 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11870 sections.macro_offset, sections.macro_size);
11871 dwo_file->sections.str_offsets =
11872 create_dwp_v2_section (dwarf2_per_objfile,
11873 &dwp_file->sections.str_offsets,
11874 sections.str_offsets_offset,
11875 sections.str_offsets_size);
11876 /* The "str" section is global to the entire DWP file. */
11877 dwo_file->sections.str = dwp_file->sections.str;
11878 /* The info or types section is assigned below to dwo_unit,
11879 there's no need to record it in dwo_file.
11880 Also, we can't simply record type sections in dwo_file because
11881 we record a pointer into the vector in dwo_unit. As we collect more
11882 types we'll grow the vector and eventually have to reallocate space
11883 for it, invalidating all copies of pointers into the previous
11884 contents. */
11885 *dwo_file_slot = dwo_file;
11886 }
11887 else
11888 {
11889 if (dwarf_read_debug)
11890 {
11891 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11892 virtual_dwo_name.c_str ());
11893 }
11894 dwo_file = (struct dwo_file *) *dwo_file_slot;
11895 }
11896
11897 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11898 dwo_unit->dwo_file = dwo_file;
11899 dwo_unit->signature = signature;
11900 dwo_unit->section =
11901 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11902 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11903 is_debug_types
11904 ? &dwp_file->sections.types
11905 : &dwp_file->sections.info,
11906 sections.info_or_types_offset,
11907 sections.info_or_types_size);
11908 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11909
11910 return dwo_unit;
11911 }
11912
11913 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11914 Returns NULL if the signature isn't found. */
11915
11916 static struct dwo_unit *
11917 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11918 struct dwp_file *dwp_file, const char *comp_dir,
11919 ULONGEST signature, int is_debug_types)
11920 {
11921 const struct dwp_hash_table *dwp_htab =
11922 is_debug_types ? dwp_file->tus : dwp_file->cus;
11923 bfd *dbfd = dwp_file->dbfd.get ();
11924 uint32_t mask = dwp_htab->nr_slots - 1;
11925 uint32_t hash = signature & mask;
11926 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11927 unsigned int i;
11928 void **slot;
11929 struct dwo_unit find_dwo_cu;
11930
11931 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11932 find_dwo_cu.signature = signature;
11933 slot = htab_find_slot (is_debug_types
11934 ? dwp_file->loaded_tus.get ()
11935 : dwp_file->loaded_cus.get (),
11936 &find_dwo_cu, INSERT);
11937
11938 if (*slot != NULL)
11939 return (struct dwo_unit *) *slot;
11940
11941 /* Use a for loop so that we don't loop forever on bad debug info. */
11942 for (i = 0; i < dwp_htab->nr_slots; ++i)
11943 {
11944 ULONGEST signature_in_table;
11945
11946 signature_in_table =
11947 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11948 if (signature_in_table == signature)
11949 {
11950 uint32_t unit_index =
11951 read_4_bytes (dbfd,
11952 dwp_htab->unit_table + hash * sizeof (uint32_t));
11953
11954 if (dwp_file->version == 1)
11955 {
11956 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11957 dwp_file, unit_index,
11958 comp_dir, signature,
11959 is_debug_types);
11960 }
11961 else
11962 {
11963 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11964 dwp_file, unit_index,
11965 comp_dir, signature,
11966 is_debug_types);
11967 }
11968 return (struct dwo_unit *) *slot;
11969 }
11970 if (signature_in_table == 0)
11971 return NULL;
11972 hash = (hash + hash2) & mask;
11973 }
11974
11975 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11976 " [in module %s]"),
11977 dwp_file->name);
11978 }
11979
11980 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11981 Open the file specified by FILE_NAME and hand it off to BFD for
11982 preliminary analysis. Return a newly initialized bfd *, which
11983 includes a canonicalized copy of FILE_NAME.
11984 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11985 SEARCH_CWD is true if the current directory is to be searched.
11986 It will be searched before debug-file-directory.
11987 If successful, the file is added to the bfd include table of the
11988 objfile's bfd (see gdb_bfd_record_inclusion).
11989 If unable to find/open the file, return NULL.
11990 NOTE: This function is derived from symfile_bfd_open. */
11991
11992 static gdb_bfd_ref_ptr
11993 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11994 const char *file_name, int is_dwp, int search_cwd)
11995 {
11996 int desc;
11997 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11998 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11999 to debug_file_directory. */
12000 const char *search_path;
12001 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12002
12003 gdb::unique_xmalloc_ptr<char> search_path_holder;
12004 if (search_cwd)
12005 {
12006 if (*debug_file_directory != '\0')
12007 {
12008 search_path_holder.reset (concat (".", dirname_separator_string,
12009 debug_file_directory,
12010 (char *) NULL));
12011 search_path = search_path_holder.get ();
12012 }
12013 else
12014 search_path = ".";
12015 }
12016 else
12017 search_path = debug_file_directory;
12018
12019 openp_flags flags = OPF_RETURN_REALPATH;
12020 if (is_dwp)
12021 flags |= OPF_SEARCH_IN_PATH;
12022
12023 gdb::unique_xmalloc_ptr<char> absolute_name;
12024 desc = openp (search_path, flags, file_name,
12025 O_RDONLY | O_BINARY, &absolute_name);
12026 if (desc < 0)
12027 return NULL;
12028
12029 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12030 gnutarget, desc));
12031 if (sym_bfd == NULL)
12032 return NULL;
12033 bfd_set_cacheable (sym_bfd.get (), 1);
12034
12035 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12036 return NULL;
12037
12038 /* Success. Record the bfd as having been included by the objfile's bfd.
12039 This is important because things like demangled_names_hash lives in the
12040 objfile's per_bfd space and may have references to things like symbol
12041 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12042 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12043
12044 return sym_bfd;
12045 }
12046
12047 /* Try to open DWO file FILE_NAME.
12048 COMP_DIR is the DW_AT_comp_dir attribute.
12049 The result is the bfd handle of the file.
12050 If there is a problem finding or opening the file, return NULL.
12051 Upon success, the canonicalized path of the file is stored in the bfd,
12052 same as symfile_bfd_open. */
12053
12054 static gdb_bfd_ref_ptr
12055 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12056 const char *file_name, const char *comp_dir)
12057 {
12058 if (IS_ABSOLUTE_PATH (file_name))
12059 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12060 0 /*is_dwp*/, 0 /*search_cwd*/);
12061
12062 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12063
12064 if (comp_dir != NULL)
12065 {
12066 gdb::unique_xmalloc_ptr<char> path_to_try
12067 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12068
12069 /* NOTE: If comp_dir is a relative path, this will also try the
12070 search path, which seems useful. */
12071 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12072 path_to_try.get (),
12073 0 /*is_dwp*/,
12074 1 /*search_cwd*/));
12075 if (abfd != NULL)
12076 return abfd;
12077 }
12078
12079 /* That didn't work, try debug-file-directory, which, despite its name,
12080 is a list of paths. */
12081
12082 if (*debug_file_directory == '\0')
12083 return NULL;
12084
12085 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12086 0 /*is_dwp*/, 1 /*search_cwd*/);
12087 }
12088
12089 /* This function is mapped across the sections and remembers the offset and
12090 size of each of the DWO debugging sections we are interested in. */
12091
12092 static void
12093 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12094 {
12095 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12096 const struct dwop_section_names *names = &dwop_section_names;
12097
12098 if (section_is_p (sectp->name, &names->abbrev_dwo))
12099 {
12100 dwo_sections->abbrev.s.section = sectp;
12101 dwo_sections->abbrev.size = bfd_section_size (sectp);
12102 }
12103 else if (section_is_p (sectp->name, &names->info_dwo))
12104 {
12105 dwo_sections->info.s.section = sectp;
12106 dwo_sections->info.size = bfd_section_size (sectp);
12107 }
12108 else if (section_is_p (sectp->name, &names->line_dwo))
12109 {
12110 dwo_sections->line.s.section = sectp;
12111 dwo_sections->line.size = bfd_section_size (sectp);
12112 }
12113 else if (section_is_p (sectp->name, &names->loc_dwo))
12114 {
12115 dwo_sections->loc.s.section = sectp;
12116 dwo_sections->loc.size = bfd_section_size (sectp);
12117 }
12118 else if (section_is_p (sectp->name, &names->loclists_dwo))
12119 {
12120 dwo_sections->loclists.s.section = sectp;
12121 dwo_sections->loclists.size = bfd_section_size (sectp);
12122 }
12123 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12124 {
12125 dwo_sections->macinfo.s.section = sectp;
12126 dwo_sections->macinfo.size = bfd_section_size (sectp);
12127 }
12128 else if (section_is_p (sectp->name, &names->macro_dwo))
12129 {
12130 dwo_sections->macro.s.section = sectp;
12131 dwo_sections->macro.size = bfd_section_size (sectp);
12132 }
12133 else if (section_is_p (sectp->name, &names->str_dwo))
12134 {
12135 dwo_sections->str.s.section = sectp;
12136 dwo_sections->str.size = bfd_section_size (sectp);
12137 }
12138 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12139 {
12140 dwo_sections->str_offsets.s.section = sectp;
12141 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12142 }
12143 else if (section_is_p (sectp->name, &names->types_dwo))
12144 {
12145 struct dwarf2_section_info type_section;
12146
12147 memset (&type_section, 0, sizeof (type_section));
12148 type_section.s.section = sectp;
12149 type_section.size = bfd_section_size (sectp);
12150 dwo_sections->types.push_back (type_section);
12151 }
12152 }
12153
12154 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12155 by PER_CU. This is for the non-DWP case.
12156 The result is NULL if DWO_NAME can't be found. */
12157
12158 static struct dwo_file *
12159 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12160 const char *dwo_name, const char *comp_dir)
12161 {
12162 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12163
12164 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12165 if (dbfd == NULL)
12166 {
12167 if (dwarf_read_debug)
12168 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12169 return NULL;
12170 }
12171
12172 dwo_file_up dwo_file (new struct dwo_file);
12173 dwo_file->dwo_name = dwo_name;
12174 dwo_file->comp_dir = comp_dir;
12175 dwo_file->dbfd = std::move (dbfd);
12176
12177 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12178 &dwo_file->sections);
12179
12180 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12181 dwo_file->sections.info, dwo_file->cus);
12182
12183 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12184 dwo_file->sections.types, dwo_file->tus);
12185
12186 if (dwarf_read_debug)
12187 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12188
12189 return dwo_file.release ();
12190 }
12191
12192 /* This function is mapped across the sections and remembers the offset and
12193 size of each of the DWP debugging sections common to version 1 and 2 that
12194 we are interested in. */
12195
12196 static void
12197 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12198 void *dwp_file_ptr)
12199 {
12200 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12201 const struct dwop_section_names *names = &dwop_section_names;
12202 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12203
12204 /* Record the ELF section number for later lookup: this is what the
12205 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12206 gdb_assert (elf_section_nr < dwp_file->num_sections);
12207 dwp_file->elf_sections[elf_section_nr] = sectp;
12208
12209 /* Look for specific sections that we need. */
12210 if (section_is_p (sectp->name, &names->str_dwo))
12211 {
12212 dwp_file->sections.str.s.section = sectp;
12213 dwp_file->sections.str.size = bfd_section_size (sectp);
12214 }
12215 else if (section_is_p (sectp->name, &names->cu_index))
12216 {
12217 dwp_file->sections.cu_index.s.section = sectp;
12218 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12219 }
12220 else if (section_is_p (sectp->name, &names->tu_index))
12221 {
12222 dwp_file->sections.tu_index.s.section = sectp;
12223 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12224 }
12225 }
12226
12227 /* This function is mapped across the sections and remembers the offset and
12228 size of each of the DWP version 2 debugging sections that we are interested
12229 in. This is split into a separate function because we don't know if we
12230 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12231
12232 static void
12233 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12234 {
12235 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12236 const struct dwop_section_names *names = &dwop_section_names;
12237 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12238
12239 /* Record the ELF section number for later lookup: this is what the
12240 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12241 gdb_assert (elf_section_nr < dwp_file->num_sections);
12242 dwp_file->elf_sections[elf_section_nr] = sectp;
12243
12244 /* Look for specific sections that we need. */
12245 if (section_is_p (sectp->name, &names->abbrev_dwo))
12246 {
12247 dwp_file->sections.abbrev.s.section = sectp;
12248 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12249 }
12250 else if (section_is_p (sectp->name, &names->info_dwo))
12251 {
12252 dwp_file->sections.info.s.section = sectp;
12253 dwp_file->sections.info.size = bfd_section_size (sectp);
12254 }
12255 else if (section_is_p (sectp->name, &names->line_dwo))
12256 {
12257 dwp_file->sections.line.s.section = sectp;
12258 dwp_file->sections.line.size = bfd_section_size (sectp);
12259 }
12260 else if (section_is_p (sectp->name, &names->loc_dwo))
12261 {
12262 dwp_file->sections.loc.s.section = sectp;
12263 dwp_file->sections.loc.size = bfd_section_size (sectp);
12264 }
12265 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12266 {
12267 dwp_file->sections.macinfo.s.section = sectp;
12268 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12269 }
12270 else if (section_is_p (sectp->name, &names->macro_dwo))
12271 {
12272 dwp_file->sections.macro.s.section = sectp;
12273 dwp_file->sections.macro.size = bfd_section_size (sectp);
12274 }
12275 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12276 {
12277 dwp_file->sections.str_offsets.s.section = sectp;
12278 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12279 }
12280 else if (section_is_p (sectp->name, &names->types_dwo))
12281 {
12282 dwp_file->sections.types.s.section = sectp;
12283 dwp_file->sections.types.size = bfd_section_size (sectp);
12284 }
12285 }
12286
12287 /* Hash function for dwp_file loaded CUs/TUs. */
12288
12289 static hashval_t
12290 hash_dwp_loaded_cutus (const void *item)
12291 {
12292 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12293
12294 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12295 return dwo_unit->signature;
12296 }
12297
12298 /* Equality function for dwp_file loaded CUs/TUs. */
12299
12300 static int
12301 eq_dwp_loaded_cutus (const void *a, const void *b)
12302 {
12303 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12304 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12305
12306 return dua->signature == dub->signature;
12307 }
12308
12309 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12310
12311 static htab_up
12312 allocate_dwp_loaded_cutus_table ()
12313 {
12314 return htab_up (htab_create_alloc (3,
12315 hash_dwp_loaded_cutus,
12316 eq_dwp_loaded_cutus,
12317 NULL, xcalloc, xfree));
12318 }
12319
12320 /* Try to open DWP file FILE_NAME.
12321 The result is the bfd handle of the file.
12322 If there is a problem finding or opening the file, return NULL.
12323 Upon success, the canonicalized path of the file is stored in the bfd,
12324 same as symfile_bfd_open. */
12325
12326 static gdb_bfd_ref_ptr
12327 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12328 const char *file_name)
12329 {
12330 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12331 1 /*is_dwp*/,
12332 1 /*search_cwd*/));
12333 if (abfd != NULL)
12334 return abfd;
12335
12336 /* Work around upstream bug 15652.
12337 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12338 [Whether that's a "bug" is debatable, but it is getting in our way.]
12339 We have no real idea where the dwp file is, because gdb's realpath-ing
12340 of the executable's path may have discarded the needed info.
12341 [IWBN if the dwp file name was recorded in the executable, akin to
12342 .gnu_debuglink, but that doesn't exist yet.]
12343 Strip the directory from FILE_NAME and search again. */
12344 if (*debug_file_directory != '\0')
12345 {
12346 /* Don't implicitly search the current directory here.
12347 If the user wants to search "." to handle this case,
12348 it must be added to debug-file-directory. */
12349 return try_open_dwop_file (dwarf2_per_objfile,
12350 lbasename (file_name), 1 /*is_dwp*/,
12351 0 /*search_cwd*/);
12352 }
12353
12354 return NULL;
12355 }
12356
12357 /* Initialize the use of the DWP file for the current objfile.
12358 By convention the name of the DWP file is ${objfile}.dwp.
12359 The result is NULL if it can't be found. */
12360
12361 static std::unique_ptr<struct dwp_file>
12362 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12363 {
12364 struct objfile *objfile = dwarf2_per_objfile->objfile;
12365
12366 /* Try to find first .dwp for the binary file before any symbolic links
12367 resolving. */
12368
12369 /* If the objfile is a debug file, find the name of the real binary
12370 file and get the name of dwp file from there. */
12371 std::string dwp_name;
12372 if (objfile->separate_debug_objfile_backlink != NULL)
12373 {
12374 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12375 const char *backlink_basename = lbasename (backlink->original_name);
12376
12377 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12378 }
12379 else
12380 dwp_name = objfile->original_name;
12381
12382 dwp_name += ".dwp";
12383
12384 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12385 if (dbfd == NULL
12386 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12387 {
12388 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12389 dwp_name = objfile_name (objfile);
12390 dwp_name += ".dwp";
12391 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12392 }
12393
12394 if (dbfd == NULL)
12395 {
12396 if (dwarf_read_debug)
12397 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12398 return std::unique_ptr<dwp_file> ();
12399 }
12400
12401 const char *name = bfd_get_filename (dbfd.get ());
12402 std::unique_ptr<struct dwp_file> dwp_file
12403 (new struct dwp_file (name, std::move (dbfd)));
12404
12405 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12406 dwp_file->elf_sections =
12407 OBSTACK_CALLOC (&objfile->objfile_obstack,
12408 dwp_file->num_sections, asection *);
12409
12410 bfd_map_over_sections (dwp_file->dbfd.get (),
12411 dwarf2_locate_common_dwp_sections,
12412 dwp_file.get ());
12413
12414 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12415 0);
12416
12417 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12418 1);
12419
12420 /* The DWP file version is stored in the hash table. Oh well. */
12421 if (dwp_file->cus && dwp_file->tus
12422 && dwp_file->cus->version != dwp_file->tus->version)
12423 {
12424 /* Technically speaking, we should try to limp along, but this is
12425 pretty bizarre. We use pulongest here because that's the established
12426 portability solution (e.g, we cannot use %u for uint32_t). */
12427 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12428 " TU version %s [in DWP file %s]"),
12429 pulongest (dwp_file->cus->version),
12430 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12431 }
12432
12433 if (dwp_file->cus)
12434 dwp_file->version = dwp_file->cus->version;
12435 else if (dwp_file->tus)
12436 dwp_file->version = dwp_file->tus->version;
12437 else
12438 dwp_file->version = 2;
12439
12440 if (dwp_file->version == 2)
12441 bfd_map_over_sections (dwp_file->dbfd.get (),
12442 dwarf2_locate_v2_dwp_sections,
12443 dwp_file.get ());
12444
12445 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12446 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12447
12448 if (dwarf_read_debug)
12449 {
12450 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12451 fprintf_unfiltered (gdb_stdlog,
12452 " %s CUs, %s TUs\n",
12453 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12454 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12455 }
12456
12457 return dwp_file;
12458 }
12459
12460 /* Wrapper around open_and_init_dwp_file, only open it once. */
12461
12462 static struct dwp_file *
12463 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12464 {
12465 if (! dwarf2_per_objfile->dwp_checked)
12466 {
12467 dwarf2_per_objfile->dwp_file
12468 = open_and_init_dwp_file (dwarf2_per_objfile);
12469 dwarf2_per_objfile->dwp_checked = 1;
12470 }
12471 return dwarf2_per_objfile->dwp_file.get ();
12472 }
12473
12474 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12475 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12476 or in the DWP file for the objfile, referenced by THIS_UNIT.
12477 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12478 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12479
12480 This is called, for example, when wanting to read a variable with a
12481 complex location. Therefore we don't want to do file i/o for every call.
12482 Therefore we don't want to look for a DWO file on every call.
12483 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12484 then we check if we've already seen DWO_NAME, and only THEN do we check
12485 for a DWO file.
12486
12487 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12488 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12489
12490 static struct dwo_unit *
12491 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12492 const char *dwo_name, const char *comp_dir,
12493 ULONGEST signature, int is_debug_types)
12494 {
12495 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12496 struct objfile *objfile = dwarf2_per_objfile->objfile;
12497 const char *kind = is_debug_types ? "TU" : "CU";
12498 void **dwo_file_slot;
12499 struct dwo_file *dwo_file;
12500 struct dwp_file *dwp_file;
12501
12502 /* First see if there's a DWP file.
12503 If we have a DWP file but didn't find the DWO inside it, don't
12504 look for the original DWO file. It makes gdb behave differently
12505 depending on whether one is debugging in the build tree. */
12506
12507 dwp_file = get_dwp_file (dwarf2_per_objfile);
12508 if (dwp_file != NULL)
12509 {
12510 const struct dwp_hash_table *dwp_htab =
12511 is_debug_types ? dwp_file->tus : dwp_file->cus;
12512
12513 if (dwp_htab != NULL)
12514 {
12515 struct dwo_unit *dwo_cutu =
12516 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12517 signature, is_debug_types);
12518
12519 if (dwo_cutu != NULL)
12520 {
12521 if (dwarf_read_debug)
12522 {
12523 fprintf_unfiltered (gdb_stdlog,
12524 "Virtual DWO %s %s found: @%s\n",
12525 kind, hex_string (signature),
12526 host_address_to_string (dwo_cutu));
12527 }
12528 return dwo_cutu;
12529 }
12530 }
12531 }
12532 else
12533 {
12534 /* No DWP file, look for the DWO file. */
12535
12536 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12537 dwo_name, comp_dir);
12538 if (*dwo_file_slot == NULL)
12539 {
12540 /* Read in the file and build a table of the CUs/TUs it contains. */
12541 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12542 }
12543 /* NOTE: This will be NULL if unable to open the file. */
12544 dwo_file = (struct dwo_file *) *dwo_file_slot;
12545
12546 if (dwo_file != NULL)
12547 {
12548 struct dwo_unit *dwo_cutu = NULL;
12549
12550 if (is_debug_types && dwo_file->tus)
12551 {
12552 struct dwo_unit find_dwo_cutu;
12553
12554 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12555 find_dwo_cutu.signature = signature;
12556 dwo_cutu
12557 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12558 &find_dwo_cutu);
12559 }
12560 else if (!is_debug_types && dwo_file->cus)
12561 {
12562 struct dwo_unit find_dwo_cutu;
12563
12564 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12565 find_dwo_cutu.signature = signature;
12566 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12567 &find_dwo_cutu);
12568 }
12569
12570 if (dwo_cutu != NULL)
12571 {
12572 if (dwarf_read_debug)
12573 {
12574 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12575 kind, dwo_name, hex_string (signature),
12576 host_address_to_string (dwo_cutu));
12577 }
12578 return dwo_cutu;
12579 }
12580 }
12581 }
12582
12583 /* We didn't find it. This could mean a dwo_id mismatch, or
12584 someone deleted the DWO/DWP file, or the search path isn't set up
12585 correctly to find the file. */
12586
12587 if (dwarf_read_debug)
12588 {
12589 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12590 kind, dwo_name, hex_string (signature));
12591 }
12592
12593 /* This is a warning and not a complaint because it can be caused by
12594 pilot error (e.g., user accidentally deleting the DWO). */
12595 {
12596 /* Print the name of the DWP file if we looked there, helps the user
12597 better diagnose the problem. */
12598 std::string dwp_text;
12599
12600 if (dwp_file != NULL)
12601 dwp_text = string_printf (" [in DWP file %s]",
12602 lbasename (dwp_file->name));
12603
12604 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12605 " [in module %s]"),
12606 kind, dwo_name, hex_string (signature),
12607 dwp_text.c_str (),
12608 this_unit->is_debug_types ? "TU" : "CU",
12609 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12610 }
12611 return NULL;
12612 }
12613
12614 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12615 See lookup_dwo_cutu_unit for details. */
12616
12617 static struct dwo_unit *
12618 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12619 const char *dwo_name, const char *comp_dir,
12620 ULONGEST signature)
12621 {
12622 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12623 }
12624
12625 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12626 See lookup_dwo_cutu_unit for details. */
12627
12628 static struct dwo_unit *
12629 lookup_dwo_type_unit (struct signatured_type *this_tu,
12630 const char *dwo_name, const char *comp_dir)
12631 {
12632 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12633 }
12634
12635 /* Traversal function for queue_and_load_all_dwo_tus. */
12636
12637 static int
12638 queue_and_load_dwo_tu (void **slot, void *info)
12639 {
12640 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12641 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12642 ULONGEST signature = dwo_unit->signature;
12643 struct signatured_type *sig_type =
12644 lookup_dwo_signatured_type (per_cu->cu, signature);
12645
12646 if (sig_type != NULL)
12647 {
12648 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12649
12650 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12651 a real dependency of PER_CU on SIG_TYPE. That is detected later
12652 while processing PER_CU. */
12653 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12654 load_full_type_unit (sig_cu);
12655 per_cu->imported_symtabs_push (sig_cu);
12656 }
12657
12658 return 1;
12659 }
12660
12661 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12662 The DWO may have the only definition of the type, though it may not be
12663 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12664 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12665
12666 static void
12667 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12668 {
12669 struct dwo_unit *dwo_unit;
12670 struct dwo_file *dwo_file;
12671
12672 gdb_assert (!per_cu->is_debug_types);
12673 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12674 gdb_assert (per_cu->cu != NULL);
12675
12676 dwo_unit = per_cu->cu->dwo_unit;
12677 gdb_assert (dwo_unit != NULL);
12678
12679 dwo_file = dwo_unit->dwo_file;
12680 if (dwo_file->tus != NULL)
12681 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12682 per_cu);
12683 }
12684
12685 /* Read in various DIEs. */
12686
12687 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12688 Inherit only the children of the DW_AT_abstract_origin DIE not being
12689 already referenced by DW_AT_abstract_origin from the children of the
12690 current DIE. */
12691
12692 static void
12693 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12694 {
12695 struct die_info *child_die;
12696 sect_offset *offsetp;
12697 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12698 struct die_info *origin_die;
12699 /* Iterator of the ORIGIN_DIE children. */
12700 struct die_info *origin_child_die;
12701 struct attribute *attr;
12702 struct dwarf2_cu *origin_cu;
12703 struct pending **origin_previous_list_in_scope;
12704
12705 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12706 if (!attr)
12707 return;
12708
12709 /* Note that following die references may follow to a die in a
12710 different cu. */
12711
12712 origin_cu = cu;
12713 origin_die = follow_die_ref (die, attr, &origin_cu);
12714
12715 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12716 symbols in. */
12717 origin_previous_list_in_scope = origin_cu->list_in_scope;
12718 origin_cu->list_in_scope = cu->list_in_scope;
12719
12720 if (die->tag != origin_die->tag
12721 && !(die->tag == DW_TAG_inlined_subroutine
12722 && origin_die->tag == DW_TAG_subprogram))
12723 complaint (_("DIE %s and its abstract origin %s have different tags"),
12724 sect_offset_str (die->sect_off),
12725 sect_offset_str (origin_die->sect_off));
12726
12727 std::vector<sect_offset> offsets;
12728
12729 for (child_die = die->child;
12730 child_die && child_die->tag;
12731 child_die = child_die->sibling)
12732 {
12733 struct die_info *child_origin_die;
12734 struct dwarf2_cu *child_origin_cu;
12735
12736 /* We are trying to process concrete instance entries:
12737 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12738 it's not relevant to our analysis here. i.e. detecting DIEs that are
12739 present in the abstract instance but not referenced in the concrete
12740 one. */
12741 if (child_die->tag == DW_TAG_call_site
12742 || child_die->tag == DW_TAG_GNU_call_site)
12743 continue;
12744
12745 /* For each CHILD_DIE, find the corresponding child of
12746 ORIGIN_DIE. If there is more than one layer of
12747 DW_AT_abstract_origin, follow them all; there shouldn't be,
12748 but GCC versions at least through 4.4 generate this (GCC PR
12749 40573). */
12750 child_origin_die = child_die;
12751 child_origin_cu = cu;
12752 while (1)
12753 {
12754 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12755 child_origin_cu);
12756 if (attr == NULL)
12757 break;
12758 child_origin_die = follow_die_ref (child_origin_die, attr,
12759 &child_origin_cu);
12760 }
12761
12762 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12763 counterpart may exist. */
12764 if (child_origin_die != child_die)
12765 {
12766 if (child_die->tag != child_origin_die->tag
12767 && !(child_die->tag == DW_TAG_inlined_subroutine
12768 && child_origin_die->tag == DW_TAG_subprogram))
12769 complaint (_("Child DIE %s and its abstract origin %s have "
12770 "different tags"),
12771 sect_offset_str (child_die->sect_off),
12772 sect_offset_str (child_origin_die->sect_off));
12773 if (child_origin_die->parent != origin_die)
12774 complaint (_("Child DIE %s and its abstract origin %s have "
12775 "different parents"),
12776 sect_offset_str (child_die->sect_off),
12777 sect_offset_str (child_origin_die->sect_off));
12778 else
12779 offsets.push_back (child_origin_die->sect_off);
12780 }
12781 }
12782 std::sort (offsets.begin (), offsets.end ());
12783 sect_offset *offsets_end = offsets.data () + offsets.size ();
12784 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12785 if (offsetp[-1] == *offsetp)
12786 complaint (_("Multiple children of DIE %s refer "
12787 "to DIE %s as their abstract origin"),
12788 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12789
12790 offsetp = offsets.data ();
12791 origin_child_die = origin_die->child;
12792 while (origin_child_die && origin_child_die->tag)
12793 {
12794 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12795 while (offsetp < offsets_end
12796 && *offsetp < origin_child_die->sect_off)
12797 offsetp++;
12798 if (offsetp >= offsets_end
12799 || *offsetp > origin_child_die->sect_off)
12800 {
12801 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12802 Check whether we're already processing ORIGIN_CHILD_DIE.
12803 This can happen with mutually referenced abstract_origins.
12804 PR 16581. */
12805 if (!origin_child_die->in_process)
12806 process_die (origin_child_die, origin_cu);
12807 }
12808 origin_child_die = origin_child_die->sibling;
12809 }
12810 origin_cu->list_in_scope = origin_previous_list_in_scope;
12811
12812 if (cu != origin_cu)
12813 compute_delayed_physnames (origin_cu);
12814 }
12815
12816 static void
12817 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12818 {
12819 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12820 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12821 struct context_stack *newobj;
12822 CORE_ADDR lowpc;
12823 CORE_ADDR highpc;
12824 struct die_info *child_die;
12825 struct attribute *attr, *call_line, *call_file;
12826 const char *name;
12827 CORE_ADDR baseaddr;
12828 struct block *block;
12829 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12830 std::vector<struct symbol *> template_args;
12831 struct template_symbol *templ_func = NULL;
12832
12833 if (inlined_func)
12834 {
12835 /* If we do not have call site information, we can't show the
12836 caller of this inlined function. That's too confusing, so
12837 only use the scope for local variables. */
12838 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12839 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12840 if (call_line == NULL || call_file == NULL)
12841 {
12842 read_lexical_block_scope (die, cu);
12843 return;
12844 }
12845 }
12846
12847 baseaddr = objfile->text_section_offset ();
12848
12849 name = dwarf2_name (die, cu);
12850
12851 /* Ignore functions with missing or empty names. These are actually
12852 illegal according to the DWARF standard. */
12853 if (name == NULL)
12854 {
12855 complaint (_("missing name for subprogram DIE at %s"),
12856 sect_offset_str (die->sect_off));
12857 return;
12858 }
12859
12860 /* Ignore functions with missing or invalid low and high pc attributes. */
12861 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12862 <= PC_BOUNDS_INVALID)
12863 {
12864 attr = dwarf2_attr (die, DW_AT_external, cu);
12865 if (!attr || !DW_UNSND (attr))
12866 complaint (_("cannot get low and high bounds "
12867 "for subprogram DIE at %s"),
12868 sect_offset_str (die->sect_off));
12869 return;
12870 }
12871
12872 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12873 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12874
12875 /* If we have any template arguments, then we must allocate a
12876 different sort of symbol. */
12877 for (child_die = die->child; child_die; child_die = child_die->sibling)
12878 {
12879 if (child_die->tag == DW_TAG_template_type_param
12880 || child_die->tag == DW_TAG_template_value_param)
12881 {
12882 templ_func = allocate_template_symbol (objfile);
12883 templ_func->subclass = SYMBOL_TEMPLATE;
12884 break;
12885 }
12886 }
12887
12888 newobj = cu->get_builder ()->push_context (0, lowpc);
12889 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12890 (struct symbol *) templ_func);
12891
12892 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12893 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12894 cu->language);
12895
12896 /* If there is a location expression for DW_AT_frame_base, record
12897 it. */
12898 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12899 if (attr != nullptr)
12900 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12901
12902 /* If there is a location for the static link, record it. */
12903 newobj->static_link = NULL;
12904 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12905 if (attr != nullptr)
12906 {
12907 newobj->static_link
12908 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12909 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12910 cu->per_cu->addr_type ());
12911 }
12912
12913 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12914
12915 if (die->child != NULL)
12916 {
12917 child_die = die->child;
12918 while (child_die && child_die->tag)
12919 {
12920 if (child_die->tag == DW_TAG_template_type_param
12921 || child_die->tag == DW_TAG_template_value_param)
12922 {
12923 struct symbol *arg = new_symbol (child_die, NULL, cu);
12924
12925 if (arg != NULL)
12926 template_args.push_back (arg);
12927 }
12928 else
12929 process_die (child_die, cu);
12930 child_die = child_die->sibling;
12931 }
12932 }
12933
12934 inherit_abstract_dies (die, cu);
12935
12936 /* If we have a DW_AT_specification, we might need to import using
12937 directives from the context of the specification DIE. See the
12938 comment in determine_prefix. */
12939 if (cu->language == language_cplus
12940 && dwarf2_attr (die, DW_AT_specification, cu))
12941 {
12942 struct dwarf2_cu *spec_cu = cu;
12943 struct die_info *spec_die = die_specification (die, &spec_cu);
12944
12945 while (spec_die)
12946 {
12947 child_die = spec_die->child;
12948 while (child_die && child_die->tag)
12949 {
12950 if (child_die->tag == DW_TAG_imported_module)
12951 process_die (child_die, spec_cu);
12952 child_die = child_die->sibling;
12953 }
12954
12955 /* In some cases, GCC generates specification DIEs that
12956 themselves contain DW_AT_specification attributes. */
12957 spec_die = die_specification (spec_die, &spec_cu);
12958 }
12959 }
12960
12961 struct context_stack cstk = cu->get_builder ()->pop_context ();
12962 /* Make a block for the local symbols within. */
12963 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12964 cstk.static_link, lowpc, highpc);
12965
12966 /* For C++, set the block's scope. */
12967 if ((cu->language == language_cplus
12968 || cu->language == language_fortran
12969 || cu->language == language_d
12970 || cu->language == language_rust)
12971 && cu->processing_has_namespace_info)
12972 block_set_scope (block, determine_prefix (die, cu),
12973 &objfile->objfile_obstack);
12974
12975 /* If we have address ranges, record them. */
12976 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12977
12978 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12979
12980 /* Attach template arguments to function. */
12981 if (!template_args.empty ())
12982 {
12983 gdb_assert (templ_func != NULL);
12984
12985 templ_func->n_template_arguments = template_args.size ();
12986 templ_func->template_arguments
12987 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12988 templ_func->n_template_arguments);
12989 memcpy (templ_func->template_arguments,
12990 template_args.data (),
12991 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12992
12993 /* Make sure that the symtab is set on the new symbols. Even
12994 though they don't appear in this symtab directly, other parts
12995 of gdb assume that symbols do, and this is reasonably
12996 true. */
12997 for (symbol *sym : template_args)
12998 symbol_set_symtab (sym, symbol_symtab (templ_func));
12999 }
13000
13001 /* In C++, we can have functions nested inside functions (e.g., when
13002 a function declares a class that has methods). This means that
13003 when we finish processing a function scope, we may need to go
13004 back to building a containing block's symbol lists. */
13005 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13006 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13007
13008 /* If we've finished processing a top-level function, subsequent
13009 symbols go in the file symbol list. */
13010 if (cu->get_builder ()->outermost_context_p ())
13011 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13012 }
13013
13014 /* Process all the DIES contained within a lexical block scope. Start
13015 a new scope, process the dies, and then close the scope. */
13016
13017 static void
13018 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13019 {
13020 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13021 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13022 CORE_ADDR lowpc, highpc;
13023 struct die_info *child_die;
13024 CORE_ADDR baseaddr;
13025
13026 baseaddr = objfile->text_section_offset ();
13027
13028 /* Ignore blocks with missing or invalid low and high pc attributes. */
13029 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13030 as multiple lexical blocks? Handling children in a sane way would
13031 be nasty. Might be easier to properly extend generic blocks to
13032 describe ranges. */
13033 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13034 {
13035 case PC_BOUNDS_NOT_PRESENT:
13036 /* DW_TAG_lexical_block has no attributes, process its children as if
13037 there was no wrapping by that DW_TAG_lexical_block.
13038 GCC does no longer produces such DWARF since GCC r224161. */
13039 for (child_die = die->child;
13040 child_die != NULL && child_die->tag;
13041 child_die = child_die->sibling)
13042 process_die (child_die, cu);
13043 return;
13044 case PC_BOUNDS_INVALID:
13045 return;
13046 }
13047 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13048 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13049
13050 cu->get_builder ()->push_context (0, lowpc);
13051 if (die->child != NULL)
13052 {
13053 child_die = die->child;
13054 while (child_die && child_die->tag)
13055 {
13056 process_die (child_die, cu);
13057 child_die = child_die->sibling;
13058 }
13059 }
13060 inherit_abstract_dies (die, cu);
13061 struct context_stack cstk = cu->get_builder ()->pop_context ();
13062
13063 if (*cu->get_builder ()->get_local_symbols () != NULL
13064 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13065 {
13066 struct block *block
13067 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13068 cstk.start_addr, highpc);
13069
13070 /* Note that recording ranges after traversing children, as we
13071 do here, means that recording a parent's ranges entails
13072 walking across all its children's ranges as they appear in
13073 the address map, which is quadratic behavior.
13074
13075 It would be nicer to record the parent's ranges before
13076 traversing its children, simply overriding whatever you find
13077 there. But since we don't even decide whether to create a
13078 block until after we've traversed its children, that's hard
13079 to do. */
13080 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13081 }
13082 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13083 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13084 }
13085
13086 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13087
13088 static void
13089 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13090 {
13091 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13092 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13093 CORE_ADDR pc, baseaddr;
13094 struct attribute *attr;
13095 struct call_site *call_site, call_site_local;
13096 void **slot;
13097 int nparams;
13098 struct die_info *child_die;
13099
13100 baseaddr = objfile->text_section_offset ();
13101
13102 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13103 if (attr == NULL)
13104 {
13105 /* This was a pre-DWARF-5 GNU extension alias
13106 for DW_AT_call_return_pc. */
13107 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13108 }
13109 if (!attr)
13110 {
13111 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13112 "DIE %s [in module %s]"),
13113 sect_offset_str (die->sect_off), objfile_name (objfile));
13114 return;
13115 }
13116 pc = attr->value_as_address () + baseaddr;
13117 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13118
13119 if (cu->call_site_htab == NULL)
13120 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13121 NULL, &objfile->objfile_obstack,
13122 hashtab_obstack_allocate, NULL);
13123 call_site_local.pc = pc;
13124 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13125 if (*slot != NULL)
13126 {
13127 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13128 "DIE %s [in module %s]"),
13129 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13130 objfile_name (objfile));
13131 return;
13132 }
13133
13134 /* Count parameters at the caller. */
13135
13136 nparams = 0;
13137 for (child_die = die->child; child_die && child_die->tag;
13138 child_die = child_die->sibling)
13139 {
13140 if (child_die->tag != DW_TAG_call_site_parameter
13141 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13142 {
13143 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13144 "DW_TAG_call_site child DIE %s [in module %s]"),
13145 child_die->tag, sect_offset_str (child_die->sect_off),
13146 objfile_name (objfile));
13147 continue;
13148 }
13149
13150 nparams++;
13151 }
13152
13153 call_site
13154 = ((struct call_site *)
13155 obstack_alloc (&objfile->objfile_obstack,
13156 sizeof (*call_site)
13157 + (sizeof (*call_site->parameter) * (nparams - 1))));
13158 *slot = call_site;
13159 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13160 call_site->pc = pc;
13161
13162 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13163 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13164 {
13165 struct die_info *func_die;
13166
13167 /* Skip also over DW_TAG_inlined_subroutine. */
13168 for (func_die = die->parent;
13169 func_die && func_die->tag != DW_TAG_subprogram
13170 && func_die->tag != DW_TAG_subroutine_type;
13171 func_die = func_die->parent);
13172
13173 /* DW_AT_call_all_calls is a superset
13174 of DW_AT_call_all_tail_calls. */
13175 if (func_die
13176 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13177 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13178 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13179 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13180 {
13181 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13182 not complete. But keep CALL_SITE for look ups via call_site_htab,
13183 both the initial caller containing the real return address PC and
13184 the final callee containing the current PC of a chain of tail
13185 calls do not need to have the tail call list complete. But any
13186 function candidate for a virtual tail call frame searched via
13187 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13188 determined unambiguously. */
13189 }
13190 else
13191 {
13192 struct type *func_type = NULL;
13193
13194 if (func_die)
13195 func_type = get_die_type (func_die, cu);
13196 if (func_type != NULL)
13197 {
13198 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13199
13200 /* Enlist this call site to the function. */
13201 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13202 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13203 }
13204 else
13205 complaint (_("Cannot find function owning DW_TAG_call_site "
13206 "DIE %s [in module %s]"),
13207 sect_offset_str (die->sect_off), objfile_name (objfile));
13208 }
13209 }
13210
13211 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13212 if (attr == NULL)
13213 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13214 if (attr == NULL)
13215 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13216 if (attr == NULL)
13217 {
13218 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13219 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13220 }
13221 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13222 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13223 /* Keep NULL DWARF_BLOCK. */;
13224 else if (attr->form_is_block ())
13225 {
13226 struct dwarf2_locexpr_baton *dlbaton;
13227
13228 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13229 dlbaton->data = DW_BLOCK (attr)->data;
13230 dlbaton->size = DW_BLOCK (attr)->size;
13231 dlbaton->per_cu = cu->per_cu;
13232
13233 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13234 }
13235 else if (attr->form_is_ref ())
13236 {
13237 struct dwarf2_cu *target_cu = cu;
13238 struct die_info *target_die;
13239
13240 target_die = follow_die_ref (die, attr, &target_cu);
13241 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13242 if (die_is_declaration (target_die, target_cu))
13243 {
13244 const char *target_physname;
13245
13246 /* Prefer the mangled name; otherwise compute the demangled one. */
13247 target_physname = dw2_linkage_name (target_die, target_cu);
13248 if (target_physname == NULL)
13249 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13250 if (target_physname == NULL)
13251 complaint (_("DW_AT_call_target target DIE has invalid "
13252 "physname, for referencing DIE %s [in module %s]"),
13253 sect_offset_str (die->sect_off), objfile_name (objfile));
13254 else
13255 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13256 }
13257 else
13258 {
13259 CORE_ADDR lowpc;
13260
13261 /* DW_AT_entry_pc should be preferred. */
13262 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13263 <= PC_BOUNDS_INVALID)
13264 complaint (_("DW_AT_call_target target DIE has invalid "
13265 "low pc, for referencing DIE %s [in module %s]"),
13266 sect_offset_str (die->sect_off), objfile_name (objfile));
13267 else
13268 {
13269 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13270 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13271 }
13272 }
13273 }
13274 else
13275 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13276 "block nor reference, for DIE %s [in module %s]"),
13277 sect_offset_str (die->sect_off), objfile_name (objfile));
13278
13279 call_site->per_cu = cu->per_cu;
13280
13281 for (child_die = die->child;
13282 child_die && child_die->tag;
13283 child_die = child_die->sibling)
13284 {
13285 struct call_site_parameter *parameter;
13286 struct attribute *loc, *origin;
13287
13288 if (child_die->tag != DW_TAG_call_site_parameter
13289 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13290 {
13291 /* Already printed the complaint above. */
13292 continue;
13293 }
13294
13295 gdb_assert (call_site->parameter_count < nparams);
13296 parameter = &call_site->parameter[call_site->parameter_count];
13297
13298 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13299 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13300 register is contained in DW_AT_call_value. */
13301
13302 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13303 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13304 if (origin == NULL)
13305 {
13306 /* This was a pre-DWARF-5 GNU extension alias
13307 for DW_AT_call_parameter. */
13308 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13309 }
13310 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13311 {
13312 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13313
13314 sect_offset sect_off = origin->get_ref_die_offset ();
13315 if (!cu->header.offset_in_cu_p (sect_off))
13316 {
13317 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13318 binding can be done only inside one CU. Such referenced DIE
13319 therefore cannot be even moved to DW_TAG_partial_unit. */
13320 complaint (_("DW_AT_call_parameter offset is not in CU for "
13321 "DW_TAG_call_site child DIE %s [in module %s]"),
13322 sect_offset_str (child_die->sect_off),
13323 objfile_name (objfile));
13324 continue;
13325 }
13326 parameter->u.param_cu_off
13327 = (cu_offset) (sect_off - cu->header.sect_off);
13328 }
13329 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13330 {
13331 complaint (_("No DW_FORM_block* DW_AT_location for "
13332 "DW_TAG_call_site child DIE %s [in module %s]"),
13333 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13334 continue;
13335 }
13336 else
13337 {
13338 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13339 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13340 if (parameter->u.dwarf_reg != -1)
13341 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13342 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13343 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13344 &parameter->u.fb_offset))
13345 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13346 else
13347 {
13348 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13349 "for DW_FORM_block* DW_AT_location is supported for "
13350 "DW_TAG_call_site child DIE %s "
13351 "[in module %s]"),
13352 sect_offset_str (child_die->sect_off),
13353 objfile_name (objfile));
13354 continue;
13355 }
13356 }
13357
13358 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13359 if (attr == NULL)
13360 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13361 if (attr == NULL || !attr->form_is_block ())
13362 {
13363 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13364 "DW_TAG_call_site child DIE %s [in module %s]"),
13365 sect_offset_str (child_die->sect_off),
13366 objfile_name (objfile));
13367 continue;
13368 }
13369 parameter->value = DW_BLOCK (attr)->data;
13370 parameter->value_size = DW_BLOCK (attr)->size;
13371
13372 /* Parameters are not pre-cleared by memset above. */
13373 parameter->data_value = NULL;
13374 parameter->data_value_size = 0;
13375 call_site->parameter_count++;
13376
13377 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13378 if (attr == NULL)
13379 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13380 if (attr != nullptr)
13381 {
13382 if (!attr->form_is_block ())
13383 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13384 "DW_TAG_call_site child DIE %s [in module %s]"),
13385 sect_offset_str (child_die->sect_off),
13386 objfile_name (objfile));
13387 else
13388 {
13389 parameter->data_value = DW_BLOCK (attr)->data;
13390 parameter->data_value_size = DW_BLOCK (attr)->size;
13391 }
13392 }
13393 }
13394 }
13395
13396 /* Helper function for read_variable. If DIE represents a virtual
13397 table, then return the type of the concrete object that is
13398 associated with the virtual table. Otherwise, return NULL. */
13399
13400 static struct type *
13401 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13402 {
13403 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13404 if (attr == NULL)
13405 return NULL;
13406
13407 /* Find the type DIE. */
13408 struct die_info *type_die = NULL;
13409 struct dwarf2_cu *type_cu = cu;
13410
13411 if (attr->form_is_ref ())
13412 type_die = follow_die_ref (die, attr, &type_cu);
13413 if (type_die == NULL)
13414 return NULL;
13415
13416 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13417 return NULL;
13418 return die_containing_type (type_die, type_cu);
13419 }
13420
13421 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13422
13423 static void
13424 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13425 {
13426 struct rust_vtable_symbol *storage = NULL;
13427
13428 if (cu->language == language_rust)
13429 {
13430 struct type *containing_type = rust_containing_type (die, cu);
13431
13432 if (containing_type != NULL)
13433 {
13434 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13435
13436 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13437 initialize_objfile_symbol (storage);
13438 storage->concrete_type = containing_type;
13439 storage->subclass = SYMBOL_RUST_VTABLE;
13440 }
13441 }
13442
13443 struct symbol *res = new_symbol (die, NULL, cu, storage);
13444 struct attribute *abstract_origin
13445 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13446 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13447 if (res == NULL && loc && abstract_origin)
13448 {
13449 /* We have a variable without a name, but with a location and an abstract
13450 origin. This may be a concrete instance of an abstract variable
13451 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13452 later. */
13453 struct dwarf2_cu *origin_cu = cu;
13454 struct die_info *origin_die
13455 = follow_die_ref (die, abstract_origin, &origin_cu);
13456 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13457 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13458 }
13459 }
13460
13461 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13462 reading .debug_rnglists.
13463 Callback's type should be:
13464 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13465 Return true if the attributes are present and valid, otherwise,
13466 return false. */
13467
13468 template <typename Callback>
13469 static bool
13470 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13471 Callback &&callback)
13472 {
13473 struct dwarf2_per_objfile *dwarf2_per_objfile
13474 = cu->per_cu->dwarf2_per_objfile;
13475 struct objfile *objfile = dwarf2_per_objfile->objfile;
13476 bfd *obfd = objfile->obfd;
13477 /* Base address selection entry. */
13478 gdb::optional<CORE_ADDR> base;
13479 const gdb_byte *buffer;
13480 CORE_ADDR baseaddr;
13481 bool overflow = false;
13482
13483 base = cu->base_address;
13484
13485 dwarf2_per_objfile->rnglists.read (objfile);
13486 if (offset >= dwarf2_per_objfile->rnglists.size)
13487 {
13488 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13489 offset);
13490 return false;
13491 }
13492 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13493
13494 baseaddr = objfile->text_section_offset ();
13495
13496 while (1)
13497 {
13498 /* Initialize it due to a false compiler warning. */
13499 CORE_ADDR range_beginning = 0, range_end = 0;
13500 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13501 + dwarf2_per_objfile->rnglists.size);
13502 unsigned int bytes_read;
13503
13504 if (buffer == buf_end)
13505 {
13506 overflow = true;
13507 break;
13508 }
13509 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13510 switch (rlet)
13511 {
13512 case DW_RLE_end_of_list:
13513 break;
13514 case DW_RLE_base_address:
13515 if (buffer + cu->header.addr_size > buf_end)
13516 {
13517 overflow = true;
13518 break;
13519 }
13520 base = cu->header.read_address (obfd, buffer, &bytes_read);
13521 buffer += bytes_read;
13522 break;
13523 case DW_RLE_start_length:
13524 if (buffer + cu->header.addr_size > buf_end)
13525 {
13526 overflow = true;
13527 break;
13528 }
13529 range_beginning = cu->header.read_address (obfd, buffer,
13530 &bytes_read);
13531 buffer += bytes_read;
13532 range_end = (range_beginning
13533 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13534 buffer += bytes_read;
13535 if (buffer > buf_end)
13536 {
13537 overflow = true;
13538 break;
13539 }
13540 break;
13541 case DW_RLE_offset_pair:
13542 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13543 buffer += bytes_read;
13544 if (buffer > buf_end)
13545 {
13546 overflow = true;
13547 break;
13548 }
13549 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13550 buffer += bytes_read;
13551 if (buffer > buf_end)
13552 {
13553 overflow = true;
13554 break;
13555 }
13556 break;
13557 case DW_RLE_start_end:
13558 if (buffer + 2 * cu->header.addr_size > buf_end)
13559 {
13560 overflow = true;
13561 break;
13562 }
13563 range_beginning = cu->header.read_address (obfd, buffer,
13564 &bytes_read);
13565 buffer += bytes_read;
13566 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13567 buffer += bytes_read;
13568 break;
13569 default:
13570 complaint (_("Invalid .debug_rnglists data (no base address)"));
13571 return false;
13572 }
13573 if (rlet == DW_RLE_end_of_list || overflow)
13574 break;
13575 if (rlet == DW_RLE_base_address)
13576 continue;
13577
13578 if (!base.has_value ())
13579 {
13580 /* We have no valid base address for the ranges
13581 data. */
13582 complaint (_("Invalid .debug_rnglists data (no base address)"));
13583 return false;
13584 }
13585
13586 if (range_beginning > range_end)
13587 {
13588 /* Inverted range entries are invalid. */
13589 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13590 return false;
13591 }
13592
13593 /* Empty range entries have no effect. */
13594 if (range_beginning == range_end)
13595 continue;
13596
13597 range_beginning += *base;
13598 range_end += *base;
13599
13600 /* A not-uncommon case of bad debug info.
13601 Don't pollute the addrmap with bad data. */
13602 if (range_beginning + baseaddr == 0
13603 && !dwarf2_per_objfile->has_section_at_zero)
13604 {
13605 complaint (_(".debug_rnglists entry has start address of zero"
13606 " [in module %s]"), objfile_name (objfile));
13607 continue;
13608 }
13609
13610 callback (range_beginning, range_end);
13611 }
13612
13613 if (overflow)
13614 {
13615 complaint (_("Offset %d is not terminated "
13616 "for DW_AT_ranges attribute"),
13617 offset);
13618 return false;
13619 }
13620
13621 return true;
13622 }
13623
13624 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13625 Callback's type should be:
13626 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13627 Return 1 if the attributes are present and valid, otherwise, return 0. */
13628
13629 template <typename Callback>
13630 static int
13631 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13632 Callback &&callback)
13633 {
13634 struct dwarf2_per_objfile *dwarf2_per_objfile
13635 = cu->per_cu->dwarf2_per_objfile;
13636 struct objfile *objfile = dwarf2_per_objfile->objfile;
13637 struct comp_unit_head *cu_header = &cu->header;
13638 bfd *obfd = objfile->obfd;
13639 unsigned int addr_size = cu_header->addr_size;
13640 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13641 /* Base address selection entry. */
13642 gdb::optional<CORE_ADDR> base;
13643 unsigned int dummy;
13644 const gdb_byte *buffer;
13645 CORE_ADDR baseaddr;
13646
13647 if (cu_header->version >= 5)
13648 return dwarf2_rnglists_process (offset, cu, callback);
13649
13650 base = cu->base_address;
13651
13652 dwarf2_per_objfile->ranges.read (objfile);
13653 if (offset >= dwarf2_per_objfile->ranges.size)
13654 {
13655 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13656 offset);
13657 return 0;
13658 }
13659 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13660
13661 baseaddr = objfile->text_section_offset ();
13662
13663 while (1)
13664 {
13665 CORE_ADDR range_beginning, range_end;
13666
13667 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13668 buffer += addr_size;
13669 range_end = cu->header.read_address (obfd, buffer, &dummy);
13670 buffer += addr_size;
13671 offset += 2 * addr_size;
13672
13673 /* An end of list marker is a pair of zero addresses. */
13674 if (range_beginning == 0 && range_end == 0)
13675 /* Found the end of list entry. */
13676 break;
13677
13678 /* Each base address selection entry is a pair of 2 values.
13679 The first is the largest possible address, the second is
13680 the base address. Check for a base address here. */
13681 if ((range_beginning & mask) == mask)
13682 {
13683 /* If we found the largest possible address, then we already
13684 have the base address in range_end. */
13685 base = range_end;
13686 continue;
13687 }
13688
13689 if (!base.has_value ())
13690 {
13691 /* We have no valid base address for the ranges
13692 data. */
13693 complaint (_("Invalid .debug_ranges data (no base address)"));
13694 return 0;
13695 }
13696
13697 if (range_beginning > range_end)
13698 {
13699 /* Inverted range entries are invalid. */
13700 complaint (_("Invalid .debug_ranges data (inverted range)"));
13701 return 0;
13702 }
13703
13704 /* Empty range entries have no effect. */
13705 if (range_beginning == range_end)
13706 continue;
13707
13708 range_beginning += *base;
13709 range_end += *base;
13710
13711 /* A not-uncommon case of bad debug info.
13712 Don't pollute the addrmap with bad data. */
13713 if (range_beginning + baseaddr == 0
13714 && !dwarf2_per_objfile->has_section_at_zero)
13715 {
13716 complaint (_(".debug_ranges entry has start address of zero"
13717 " [in module %s]"), objfile_name (objfile));
13718 continue;
13719 }
13720
13721 callback (range_beginning, range_end);
13722 }
13723
13724 return 1;
13725 }
13726
13727 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13728 Return 1 if the attributes are present and valid, otherwise, return 0.
13729 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13730
13731 static int
13732 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13733 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13734 dwarf2_psymtab *ranges_pst)
13735 {
13736 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13737 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13738 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13739 int low_set = 0;
13740 CORE_ADDR low = 0;
13741 CORE_ADDR high = 0;
13742 int retval;
13743
13744 retval = dwarf2_ranges_process (offset, cu,
13745 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13746 {
13747 if (ranges_pst != NULL)
13748 {
13749 CORE_ADDR lowpc;
13750 CORE_ADDR highpc;
13751
13752 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13753 range_beginning + baseaddr)
13754 - baseaddr);
13755 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13756 range_end + baseaddr)
13757 - baseaddr);
13758 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13759 lowpc, highpc - 1, ranges_pst);
13760 }
13761
13762 /* FIXME: This is recording everything as a low-high
13763 segment of consecutive addresses. We should have a
13764 data structure for discontiguous block ranges
13765 instead. */
13766 if (! low_set)
13767 {
13768 low = range_beginning;
13769 high = range_end;
13770 low_set = 1;
13771 }
13772 else
13773 {
13774 if (range_beginning < low)
13775 low = range_beginning;
13776 if (range_end > high)
13777 high = range_end;
13778 }
13779 });
13780 if (!retval)
13781 return 0;
13782
13783 if (! low_set)
13784 /* If the first entry is an end-of-list marker, the range
13785 describes an empty scope, i.e. no instructions. */
13786 return 0;
13787
13788 if (low_return)
13789 *low_return = low;
13790 if (high_return)
13791 *high_return = high;
13792 return 1;
13793 }
13794
13795 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13796 definition for the return value. *LOWPC and *HIGHPC are set iff
13797 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13798
13799 static enum pc_bounds_kind
13800 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13801 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13802 dwarf2_psymtab *pst)
13803 {
13804 struct dwarf2_per_objfile *dwarf2_per_objfile
13805 = cu->per_cu->dwarf2_per_objfile;
13806 struct attribute *attr;
13807 struct attribute *attr_high;
13808 CORE_ADDR low = 0;
13809 CORE_ADDR high = 0;
13810 enum pc_bounds_kind ret;
13811
13812 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13813 if (attr_high)
13814 {
13815 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13816 if (attr != nullptr)
13817 {
13818 low = attr->value_as_address ();
13819 high = attr_high->value_as_address ();
13820 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13821 high += low;
13822 }
13823 else
13824 /* Found high w/o low attribute. */
13825 return PC_BOUNDS_INVALID;
13826
13827 /* Found consecutive range of addresses. */
13828 ret = PC_BOUNDS_HIGH_LOW;
13829 }
13830 else
13831 {
13832 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13833 if (attr != NULL)
13834 {
13835 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13836 We take advantage of the fact that DW_AT_ranges does not appear
13837 in DW_TAG_compile_unit of DWO files. */
13838 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13839 unsigned int ranges_offset = (DW_UNSND (attr)
13840 + (need_ranges_base
13841 ? cu->ranges_base
13842 : 0));
13843
13844 /* Value of the DW_AT_ranges attribute is the offset in the
13845 .debug_ranges section. */
13846 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13847 return PC_BOUNDS_INVALID;
13848 /* Found discontinuous range of addresses. */
13849 ret = PC_BOUNDS_RANGES;
13850 }
13851 else
13852 return PC_BOUNDS_NOT_PRESENT;
13853 }
13854
13855 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13856 if (high <= low)
13857 return PC_BOUNDS_INVALID;
13858
13859 /* When using the GNU linker, .gnu.linkonce. sections are used to
13860 eliminate duplicate copies of functions and vtables and such.
13861 The linker will arbitrarily choose one and discard the others.
13862 The AT_*_pc values for such functions refer to local labels in
13863 these sections. If the section from that file was discarded, the
13864 labels are not in the output, so the relocs get a value of 0.
13865 If this is a discarded function, mark the pc bounds as invalid,
13866 so that GDB will ignore it. */
13867 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13868 return PC_BOUNDS_INVALID;
13869
13870 *lowpc = low;
13871 if (highpc)
13872 *highpc = high;
13873 return ret;
13874 }
13875
13876 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13877 its low and high PC addresses. Do nothing if these addresses could not
13878 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13879 and HIGHPC to the high address if greater than HIGHPC. */
13880
13881 static void
13882 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13883 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13884 struct dwarf2_cu *cu)
13885 {
13886 CORE_ADDR low, high;
13887 struct die_info *child = die->child;
13888
13889 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13890 {
13891 *lowpc = std::min (*lowpc, low);
13892 *highpc = std::max (*highpc, high);
13893 }
13894
13895 /* If the language does not allow nested subprograms (either inside
13896 subprograms or lexical blocks), we're done. */
13897 if (cu->language != language_ada)
13898 return;
13899
13900 /* Check all the children of the given DIE. If it contains nested
13901 subprograms, then check their pc bounds. Likewise, we need to
13902 check lexical blocks as well, as they may also contain subprogram
13903 definitions. */
13904 while (child && child->tag)
13905 {
13906 if (child->tag == DW_TAG_subprogram
13907 || child->tag == DW_TAG_lexical_block)
13908 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13909 child = child->sibling;
13910 }
13911 }
13912
13913 /* Get the low and high pc's represented by the scope DIE, and store
13914 them in *LOWPC and *HIGHPC. If the correct values can't be
13915 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13916
13917 static void
13918 get_scope_pc_bounds (struct die_info *die,
13919 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13920 struct dwarf2_cu *cu)
13921 {
13922 CORE_ADDR best_low = (CORE_ADDR) -1;
13923 CORE_ADDR best_high = (CORE_ADDR) 0;
13924 CORE_ADDR current_low, current_high;
13925
13926 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13927 >= PC_BOUNDS_RANGES)
13928 {
13929 best_low = current_low;
13930 best_high = current_high;
13931 }
13932 else
13933 {
13934 struct die_info *child = die->child;
13935
13936 while (child && child->tag)
13937 {
13938 switch (child->tag) {
13939 case DW_TAG_subprogram:
13940 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13941 break;
13942 case DW_TAG_namespace:
13943 case DW_TAG_module:
13944 /* FIXME: carlton/2004-01-16: Should we do this for
13945 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13946 that current GCC's always emit the DIEs corresponding
13947 to definitions of methods of classes as children of a
13948 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13949 the DIEs giving the declarations, which could be
13950 anywhere). But I don't see any reason why the
13951 standards says that they have to be there. */
13952 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13953
13954 if (current_low != ((CORE_ADDR) -1))
13955 {
13956 best_low = std::min (best_low, current_low);
13957 best_high = std::max (best_high, current_high);
13958 }
13959 break;
13960 default:
13961 /* Ignore. */
13962 break;
13963 }
13964
13965 child = child->sibling;
13966 }
13967 }
13968
13969 *lowpc = best_low;
13970 *highpc = best_high;
13971 }
13972
13973 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13974 in DIE. */
13975
13976 static void
13977 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13978 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13979 {
13980 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13981 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13982 struct attribute *attr;
13983 struct attribute *attr_high;
13984
13985 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13986 if (attr_high)
13987 {
13988 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13989 if (attr != nullptr)
13990 {
13991 CORE_ADDR low = attr->value_as_address ();
13992 CORE_ADDR high = attr_high->value_as_address ();
13993
13994 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13995 high += low;
13996
13997 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13998 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13999 cu->get_builder ()->record_block_range (block, low, high - 1);
14000 }
14001 }
14002
14003 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14004 if (attr != nullptr)
14005 {
14006 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14007 We take advantage of the fact that DW_AT_ranges does not appear
14008 in DW_TAG_compile_unit of DWO files. */
14009 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14010
14011 /* The value of the DW_AT_ranges attribute is the offset of the
14012 address range list in the .debug_ranges section. */
14013 unsigned long offset = (DW_UNSND (attr)
14014 + (need_ranges_base ? cu->ranges_base : 0));
14015
14016 std::vector<blockrange> blockvec;
14017 dwarf2_ranges_process (offset, cu,
14018 [&] (CORE_ADDR start, CORE_ADDR end)
14019 {
14020 start += baseaddr;
14021 end += baseaddr;
14022 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14023 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14024 cu->get_builder ()->record_block_range (block, start, end - 1);
14025 blockvec.emplace_back (start, end);
14026 });
14027
14028 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14029 }
14030 }
14031
14032 /* Check whether the producer field indicates either of GCC < 4.6, or the
14033 Intel C/C++ compiler, and cache the result in CU. */
14034
14035 static void
14036 check_producer (struct dwarf2_cu *cu)
14037 {
14038 int major, minor;
14039
14040 if (cu->producer == NULL)
14041 {
14042 /* For unknown compilers expect their behavior is DWARF version
14043 compliant.
14044
14045 GCC started to support .debug_types sections by -gdwarf-4 since
14046 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14047 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14048 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14049 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14050 }
14051 else if (producer_is_gcc (cu->producer, &major, &minor))
14052 {
14053 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14054 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14055 }
14056 else if (producer_is_icc (cu->producer, &major, &minor))
14057 {
14058 cu->producer_is_icc = true;
14059 cu->producer_is_icc_lt_14 = major < 14;
14060 }
14061 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14062 cu->producer_is_codewarrior = true;
14063 else
14064 {
14065 /* For other non-GCC compilers, expect their behavior is DWARF version
14066 compliant. */
14067 }
14068
14069 cu->checked_producer = true;
14070 }
14071
14072 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14073 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14074 during 4.6.0 experimental. */
14075
14076 static bool
14077 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14078 {
14079 if (!cu->checked_producer)
14080 check_producer (cu);
14081
14082 return cu->producer_is_gxx_lt_4_6;
14083 }
14084
14085
14086 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14087 with incorrect is_stmt attributes. */
14088
14089 static bool
14090 producer_is_codewarrior (struct dwarf2_cu *cu)
14091 {
14092 if (!cu->checked_producer)
14093 check_producer (cu);
14094
14095 return cu->producer_is_codewarrior;
14096 }
14097
14098 /* Return the default accessibility type if it is not overridden by
14099 DW_AT_accessibility. */
14100
14101 static enum dwarf_access_attribute
14102 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14103 {
14104 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14105 {
14106 /* The default DWARF 2 accessibility for members is public, the default
14107 accessibility for inheritance is private. */
14108
14109 if (die->tag != DW_TAG_inheritance)
14110 return DW_ACCESS_public;
14111 else
14112 return DW_ACCESS_private;
14113 }
14114 else
14115 {
14116 /* DWARF 3+ defines the default accessibility a different way. The same
14117 rules apply now for DW_TAG_inheritance as for the members and it only
14118 depends on the container kind. */
14119
14120 if (die->parent->tag == DW_TAG_class_type)
14121 return DW_ACCESS_private;
14122 else
14123 return DW_ACCESS_public;
14124 }
14125 }
14126
14127 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14128 offset. If the attribute was not found return 0, otherwise return
14129 1. If it was found but could not properly be handled, set *OFFSET
14130 to 0. */
14131
14132 static int
14133 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14134 LONGEST *offset)
14135 {
14136 struct attribute *attr;
14137
14138 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14139 if (attr != NULL)
14140 {
14141 *offset = 0;
14142
14143 /* Note that we do not check for a section offset first here.
14144 This is because DW_AT_data_member_location is new in DWARF 4,
14145 so if we see it, we can assume that a constant form is really
14146 a constant and not a section offset. */
14147 if (attr->form_is_constant ())
14148 *offset = attr->constant_value (0);
14149 else if (attr->form_is_section_offset ())
14150 dwarf2_complex_location_expr_complaint ();
14151 else if (attr->form_is_block ())
14152 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14153 else
14154 dwarf2_complex_location_expr_complaint ();
14155
14156 return 1;
14157 }
14158
14159 return 0;
14160 }
14161
14162 /* Add an aggregate field to the field list. */
14163
14164 static void
14165 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14166 struct dwarf2_cu *cu)
14167 {
14168 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14169 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14170 struct nextfield *new_field;
14171 struct attribute *attr;
14172 struct field *fp;
14173 const char *fieldname = "";
14174
14175 if (die->tag == DW_TAG_inheritance)
14176 {
14177 fip->baseclasses.emplace_back ();
14178 new_field = &fip->baseclasses.back ();
14179 }
14180 else
14181 {
14182 fip->fields.emplace_back ();
14183 new_field = &fip->fields.back ();
14184 }
14185
14186 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14187 if (attr != nullptr)
14188 new_field->accessibility = DW_UNSND (attr);
14189 else
14190 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14191 if (new_field->accessibility != DW_ACCESS_public)
14192 fip->non_public_fields = 1;
14193
14194 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14195 if (attr != nullptr)
14196 new_field->virtuality = DW_UNSND (attr);
14197 else
14198 new_field->virtuality = DW_VIRTUALITY_none;
14199
14200 fp = &new_field->field;
14201
14202 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14203 {
14204 LONGEST offset;
14205
14206 /* Data member other than a C++ static data member. */
14207
14208 /* Get type of field. */
14209 fp->type = die_type (die, cu);
14210
14211 SET_FIELD_BITPOS (*fp, 0);
14212
14213 /* Get bit size of field (zero if none). */
14214 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14215 if (attr != nullptr)
14216 {
14217 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14218 }
14219 else
14220 {
14221 FIELD_BITSIZE (*fp) = 0;
14222 }
14223
14224 /* Get bit offset of field. */
14225 if (handle_data_member_location (die, cu, &offset))
14226 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14227 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14228 if (attr != nullptr)
14229 {
14230 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14231 {
14232 /* For big endian bits, the DW_AT_bit_offset gives the
14233 additional bit offset from the MSB of the containing
14234 anonymous object to the MSB of the field. We don't
14235 have to do anything special since we don't need to
14236 know the size of the anonymous object. */
14237 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14238 }
14239 else
14240 {
14241 /* For little endian bits, compute the bit offset to the
14242 MSB of the anonymous object, subtract off the number of
14243 bits from the MSB of the field to the MSB of the
14244 object, and then subtract off the number of bits of
14245 the field itself. The result is the bit offset of
14246 the LSB of the field. */
14247 int anonymous_size;
14248 int bit_offset = DW_UNSND (attr);
14249
14250 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14251 if (attr != nullptr)
14252 {
14253 /* The size of the anonymous object containing
14254 the bit field is explicit, so use the
14255 indicated size (in bytes). */
14256 anonymous_size = DW_UNSND (attr);
14257 }
14258 else
14259 {
14260 /* The size of the anonymous object containing
14261 the bit field must be inferred from the type
14262 attribute of the data member containing the
14263 bit field. */
14264 anonymous_size = TYPE_LENGTH (fp->type);
14265 }
14266 SET_FIELD_BITPOS (*fp,
14267 (FIELD_BITPOS (*fp)
14268 + anonymous_size * bits_per_byte
14269 - bit_offset - FIELD_BITSIZE (*fp)));
14270 }
14271 }
14272 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14273 if (attr != NULL)
14274 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14275 + attr->constant_value (0)));
14276
14277 /* Get name of field. */
14278 fieldname = dwarf2_name (die, cu);
14279 if (fieldname == NULL)
14280 fieldname = "";
14281
14282 /* The name is already allocated along with this objfile, so we don't
14283 need to duplicate it for the type. */
14284 fp->name = fieldname;
14285
14286 /* Change accessibility for artificial fields (e.g. virtual table
14287 pointer or virtual base class pointer) to private. */
14288 if (dwarf2_attr (die, DW_AT_artificial, cu))
14289 {
14290 FIELD_ARTIFICIAL (*fp) = 1;
14291 new_field->accessibility = DW_ACCESS_private;
14292 fip->non_public_fields = 1;
14293 }
14294 }
14295 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14296 {
14297 /* C++ static member. */
14298
14299 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14300 is a declaration, but all versions of G++ as of this writing
14301 (so through at least 3.2.1) incorrectly generate
14302 DW_TAG_variable tags. */
14303
14304 const char *physname;
14305
14306 /* Get name of field. */
14307 fieldname = dwarf2_name (die, cu);
14308 if (fieldname == NULL)
14309 return;
14310
14311 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14312 if (attr
14313 /* Only create a symbol if this is an external value.
14314 new_symbol checks this and puts the value in the global symbol
14315 table, which we want. If it is not external, new_symbol
14316 will try to put the value in cu->list_in_scope which is wrong. */
14317 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14318 {
14319 /* A static const member, not much different than an enum as far as
14320 we're concerned, except that we can support more types. */
14321 new_symbol (die, NULL, cu);
14322 }
14323
14324 /* Get physical name. */
14325 physname = dwarf2_physname (fieldname, die, cu);
14326
14327 /* The name is already allocated along with this objfile, so we don't
14328 need to duplicate it for the type. */
14329 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14330 FIELD_TYPE (*fp) = die_type (die, cu);
14331 FIELD_NAME (*fp) = fieldname;
14332 }
14333 else if (die->tag == DW_TAG_inheritance)
14334 {
14335 LONGEST offset;
14336
14337 /* C++ base class field. */
14338 if (handle_data_member_location (die, cu, &offset))
14339 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14340 FIELD_BITSIZE (*fp) = 0;
14341 FIELD_TYPE (*fp) = die_type (die, cu);
14342 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14343 }
14344 else if (die->tag == DW_TAG_variant_part)
14345 {
14346 /* process_structure_scope will treat this DIE as a union. */
14347 process_structure_scope (die, cu);
14348
14349 /* The variant part is relative to the start of the enclosing
14350 structure. */
14351 SET_FIELD_BITPOS (*fp, 0);
14352 fp->type = get_die_type (die, cu);
14353 fp->artificial = 1;
14354 fp->name = "<<variant>>";
14355
14356 /* Normally a DW_TAG_variant_part won't have a size, but our
14357 representation requires one, so set it to the maximum of the
14358 child sizes, being sure to account for the offset at which
14359 each child is seen. */
14360 if (TYPE_LENGTH (fp->type) == 0)
14361 {
14362 unsigned max = 0;
14363 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14364 {
14365 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14366 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14367 if (len > max)
14368 max = len;
14369 }
14370 TYPE_LENGTH (fp->type) = max;
14371 }
14372 }
14373 else
14374 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14375 }
14376
14377 /* Can the type given by DIE define another type? */
14378
14379 static bool
14380 type_can_define_types (const struct die_info *die)
14381 {
14382 switch (die->tag)
14383 {
14384 case DW_TAG_typedef:
14385 case DW_TAG_class_type:
14386 case DW_TAG_structure_type:
14387 case DW_TAG_union_type:
14388 case DW_TAG_enumeration_type:
14389 return true;
14390
14391 default:
14392 return false;
14393 }
14394 }
14395
14396 /* Add a type definition defined in the scope of the FIP's class. */
14397
14398 static void
14399 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14400 struct dwarf2_cu *cu)
14401 {
14402 struct decl_field fp;
14403 memset (&fp, 0, sizeof (fp));
14404
14405 gdb_assert (type_can_define_types (die));
14406
14407 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14408 fp.name = dwarf2_name (die, cu);
14409 fp.type = read_type_die (die, cu);
14410
14411 /* Save accessibility. */
14412 enum dwarf_access_attribute accessibility;
14413 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14414 if (attr != NULL)
14415 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14416 else
14417 accessibility = dwarf2_default_access_attribute (die, cu);
14418 switch (accessibility)
14419 {
14420 case DW_ACCESS_public:
14421 /* The assumed value if neither private nor protected. */
14422 break;
14423 case DW_ACCESS_private:
14424 fp.is_private = 1;
14425 break;
14426 case DW_ACCESS_protected:
14427 fp.is_protected = 1;
14428 break;
14429 default:
14430 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14431 }
14432
14433 if (die->tag == DW_TAG_typedef)
14434 fip->typedef_field_list.push_back (fp);
14435 else
14436 fip->nested_types_list.push_back (fp);
14437 }
14438
14439 /* Create the vector of fields, and attach it to the type. */
14440
14441 static void
14442 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14443 struct dwarf2_cu *cu)
14444 {
14445 int nfields = fip->nfields ();
14446
14447 /* Record the field count, allocate space for the array of fields,
14448 and create blank accessibility bitfields if necessary. */
14449 TYPE_NFIELDS (type) = nfields;
14450 TYPE_FIELDS (type) = (struct field *)
14451 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14452
14453 if (fip->non_public_fields && cu->language != language_ada)
14454 {
14455 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14456
14457 TYPE_FIELD_PRIVATE_BITS (type) =
14458 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14459 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14460
14461 TYPE_FIELD_PROTECTED_BITS (type) =
14462 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14463 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14464
14465 TYPE_FIELD_IGNORE_BITS (type) =
14466 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14467 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14468 }
14469
14470 /* If the type has baseclasses, allocate and clear a bit vector for
14471 TYPE_FIELD_VIRTUAL_BITS. */
14472 if (!fip->baseclasses.empty () && cu->language != language_ada)
14473 {
14474 int num_bytes = B_BYTES (fip->baseclasses.size ());
14475 unsigned char *pointer;
14476
14477 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14478 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14479 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14480 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14481 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14482 }
14483
14484 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14485 {
14486 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14487
14488 for (int index = 0; index < nfields; ++index)
14489 {
14490 struct nextfield &field = fip->fields[index];
14491
14492 if (field.variant.is_discriminant)
14493 di->discriminant_index = index;
14494 else if (field.variant.default_branch)
14495 di->default_index = index;
14496 else
14497 di->discriminants[index] = field.variant.discriminant_value;
14498 }
14499 }
14500
14501 /* Copy the saved-up fields into the field vector. */
14502 for (int i = 0; i < nfields; ++i)
14503 {
14504 struct nextfield &field
14505 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14506 : fip->fields[i - fip->baseclasses.size ()]);
14507
14508 TYPE_FIELD (type, i) = field.field;
14509 switch (field.accessibility)
14510 {
14511 case DW_ACCESS_private:
14512 if (cu->language != language_ada)
14513 SET_TYPE_FIELD_PRIVATE (type, i);
14514 break;
14515
14516 case DW_ACCESS_protected:
14517 if (cu->language != language_ada)
14518 SET_TYPE_FIELD_PROTECTED (type, i);
14519 break;
14520
14521 case DW_ACCESS_public:
14522 break;
14523
14524 default:
14525 /* Unknown accessibility. Complain and treat it as public. */
14526 {
14527 complaint (_("unsupported accessibility %d"),
14528 field.accessibility);
14529 }
14530 break;
14531 }
14532 if (i < fip->baseclasses.size ())
14533 {
14534 switch (field.virtuality)
14535 {
14536 case DW_VIRTUALITY_virtual:
14537 case DW_VIRTUALITY_pure_virtual:
14538 if (cu->language == language_ada)
14539 error (_("unexpected virtuality in component of Ada type"));
14540 SET_TYPE_FIELD_VIRTUAL (type, i);
14541 break;
14542 }
14543 }
14544 }
14545 }
14546
14547 /* Return true if this member function is a constructor, false
14548 otherwise. */
14549
14550 static int
14551 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14552 {
14553 const char *fieldname;
14554 const char *type_name;
14555 int len;
14556
14557 if (die->parent == NULL)
14558 return 0;
14559
14560 if (die->parent->tag != DW_TAG_structure_type
14561 && die->parent->tag != DW_TAG_union_type
14562 && die->parent->tag != DW_TAG_class_type)
14563 return 0;
14564
14565 fieldname = dwarf2_name (die, cu);
14566 type_name = dwarf2_name (die->parent, cu);
14567 if (fieldname == NULL || type_name == NULL)
14568 return 0;
14569
14570 len = strlen (fieldname);
14571 return (strncmp (fieldname, type_name, len) == 0
14572 && (type_name[len] == '\0' || type_name[len] == '<'));
14573 }
14574
14575 /* Check if the given VALUE is a recognized enum
14576 dwarf_defaulted_attribute constant according to DWARF5 spec,
14577 Table 7.24. */
14578
14579 static bool
14580 is_valid_DW_AT_defaulted (ULONGEST value)
14581 {
14582 switch (value)
14583 {
14584 case DW_DEFAULTED_no:
14585 case DW_DEFAULTED_in_class:
14586 case DW_DEFAULTED_out_of_class:
14587 return true;
14588 }
14589
14590 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14591 return false;
14592 }
14593
14594 /* Add a member function to the proper fieldlist. */
14595
14596 static void
14597 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14598 struct type *type, struct dwarf2_cu *cu)
14599 {
14600 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14601 struct attribute *attr;
14602 int i;
14603 struct fnfieldlist *flp = nullptr;
14604 struct fn_field *fnp;
14605 const char *fieldname;
14606 struct type *this_type;
14607 enum dwarf_access_attribute accessibility;
14608
14609 if (cu->language == language_ada)
14610 error (_("unexpected member function in Ada type"));
14611
14612 /* Get name of member function. */
14613 fieldname = dwarf2_name (die, cu);
14614 if (fieldname == NULL)
14615 return;
14616
14617 /* Look up member function name in fieldlist. */
14618 for (i = 0; i < fip->fnfieldlists.size (); i++)
14619 {
14620 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14621 {
14622 flp = &fip->fnfieldlists[i];
14623 break;
14624 }
14625 }
14626
14627 /* Create a new fnfieldlist if necessary. */
14628 if (flp == nullptr)
14629 {
14630 fip->fnfieldlists.emplace_back ();
14631 flp = &fip->fnfieldlists.back ();
14632 flp->name = fieldname;
14633 i = fip->fnfieldlists.size () - 1;
14634 }
14635
14636 /* Create a new member function field and add it to the vector of
14637 fnfieldlists. */
14638 flp->fnfields.emplace_back ();
14639 fnp = &flp->fnfields.back ();
14640
14641 /* Delay processing of the physname until later. */
14642 if (cu->language == language_cplus)
14643 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14644 die, cu);
14645 else
14646 {
14647 const char *physname = dwarf2_physname (fieldname, die, cu);
14648 fnp->physname = physname ? physname : "";
14649 }
14650
14651 fnp->type = alloc_type (objfile);
14652 this_type = read_type_die (die, cu);
14653 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14654 {
14655 int nparams = TYPE_NFIELDS (this_type);
14656
14657 /* TYPE is the domain of this method, and THIS_TYPE is the type
14658 of the method itself (TYPE_CODE_METHOD). */
14659 smash_to_method_type (fnp->type, type,
14660 TYPE_TARGET_TYPE (this_type),
14661 TYPE_FIELDS (this_type),
14662 TYPE_NFIELDS (this_type),
14663 TYPE_VARARGS (this_type));
14664
14665 /* Handle static member functions.
14666 Dwarf2 has no clean way to discern C++ static and non-static
14667 member functions. G++ helps GDB by marking the first
14668 parameter for non-static member functions (which is the this
14669 pointer) as artificial. We obtain this information from
14670 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14671 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14672 fnp->voffset = VOFFSET_STATIC;
14673 }
14674 else
14675 complaint (_("member function type missing for '%s'"),
14676 dwarf2_full_name (fieldname, die, cu));
14677
14678 /* Get fcontext from DW_AT_containing_type if present. */
14679 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14680 fnp->fcontext = die_containing_type (die, cu);
14681
14682 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14683 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14684
14685 /* Get accessibility. */
14686 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14687 if (attr != nullptr)
14688 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14689 else
14690 accessibility = dwarf2_default_access_attribute (die, cu);
14691 switch (accessibility)
14692 {
14693 case DW_ACCESS_private:
14694 fnp->is_private = 1;
14695 break;
14696 case DW_ACCESS_protected:
14697 fnp->is_protected = 1;
14698 break;
14699 }
14700
14701 /* Check for artificial methods. */
14702 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14703 if (attr && DW_UNSND (attr) != 0)
14704 fnp->is_artificial = 1;
14705
14706 /* Check for defaulted methods. */
14707 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14708 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14709 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14710
14711 /* Check for deleted methods. */
14712 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14713 if (attr != nullptr && DW_UNSND (attr) != 0)
14714 fnp->is_deleted = 1;
14715
14716 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14717
14718 /* Get index in virtual function table if it is a virtual member
14719 function. For older versions of GCC, this is an offset in the
14720 appropriate virtual table, as specified by DW_AT_containing_type.
14721 For everyone else, it is an expression to be evaluated relative
14722 to the object address. */
14723
14724 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14725 if (attr != nullptr)
14726 {
14727 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14728 {
14729 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14730 {
14731 /* Old-style GCC. */
14732 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14733 }
14734 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14735 || (DW_BLOCK (attr)->size > 1
14736 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14737 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14738 {
14739 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14740 if ((fnp->voffset % cu->header.addr_size) != 0)
14741 dwarf2_complex_location_expr_complaint ();
14742 else
14743 fnp->voffset /= cu->header.addr_size;
14744 fnp->voffset += 2;
14745 }
14746 else
14747 dwarf2_complex_location_expr_complaint ();
14748
14749 if (!fnp->fcontext)
14750 {
14751 /* If there is no `this' field and no DW_AT_containing_type,
14752 we cannot actually find a base class context for the
14753 vtable! */
14754 if (TYPE_NFIELDS (this_type) == 0
14755 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14756 {
14757 complaint (_("cannot determine context for virtual member "
14758 "function \"%s\" (offset %s)"),
14759 fieldname, sect_offset_str (die->sect_off));
14760 }
14761 else
14762 {
14763 fnp->fcontext
14764 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14765 }
14766 }
14767 }
14768 else if (attr->form_is_section_offset ())
14769 {
14770 dwarf2_complex_location_expr_complaint ();
14771 }
14772 else
14773 {
14774 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14775 fieldname);
14776 }
14777 }
14778 else
14779 {
14780 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14781 if (attr && DW_UNSND (attr))
14782 {
14783 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14784 complaint (_("Member function \"%s\" (offset %s) is virtual "
14785 "but the vtable offset is not specified"),
14786 fieldname, sect_offset_str (die->sect_off));
14787 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14788 TYPE_CPLUS_DYNAMIC (type) = 1;
14789 }
14790 }
14791 }
14792
14793 /* Create the vector of member function fields, and attach it to the type. */
14794
14795 static void
14796 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14797 struct dwarf2_cu *cu)
14798 {
14799 if (cu->language == language_ada)
14800 error (_("unexpected member functions in Ada type"));
14801
14802 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14803 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14804 TYPE_ALLOC (type,
14805 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14806
14807 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14808 {
14809 struct fnfieldlist &nf = fip->fnfieldlists[i];
14810 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14811
14812 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14813 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14814 fn_flp->fn_fields = (struct fn_field *)
14815 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14816
14817 for (int k = 0; k < nf.fnfields.size (); ++k)
14818 fn_flp->fn_fields[k] = nf.fnfields[k];
14819 }
14820
14821 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14822 }
14823
14824 /* Returns non-zero if NAME is the name of a vtable member in CU's
14825 language, zero otherwise. */
14826 static int
14827 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14828 {
14829 static const char vptr[] = "_vptr";
14830
14831 /* Look for the C++ form of the vtable. */
14832 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14833 return 1;
14834
14835 return 0;
14836 }
14837
14838 /* GCC outputs unnamed structures that are really pointers to member
14839 functions, with the ABI-specified layout. If TYPE describes
14840 such a structure, smash it into a member function type.
14841
14842 GCC shouldn't do this; it should just output pointer to member DIEs.
14843 This is GCC PR debug/28767. */
14844
14845 static void
14846 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14847 {
14848 struct type *pfn_type, *self_type, *new_type;
14849
14850 /* Check for a structure with no name and two children. */
14851 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14852 return;
14853
14854 /* Check for __pfn and __delta members. */
14855 if (TYPE_FIELD_NAME (type, 0) == NULL
14856 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14857 || TYPE_FIELD_NAME (type, 1) == NULL
14858 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14859 return;
14860
14861 /* Find the type of the method. */
14862 pfn_type = TYPE_FIELD_TYPE (type, 0);
14863 if (pfn_type == NULL
14864 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14865 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14866 return;
14867
14868 /* Look for the "this" argument. */
14869 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14870 if (TYPE_NFIELDS (pfn_type) == 0
14871 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14872 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14873 return;
14874
14875 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14876 new_type = alloc_type (objfile);
14877 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14878 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14879 TYPE_VARARGS (pfn_type));
14880 smash_to_methodptr_type (type, new_type);
14881 }
14882
14883 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14884 appropriate error checking and issuing complaints if there is a
14885 problem. */
14886
14887 static ULONGEST
14888 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14889 {
14890 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14891
14892 if (attr == nullptr)
14893 return 0;
14894
14895 if (!attr->form_is_constant ())
14896 {
14897 complaint (_("DW_AT_alignment must have constant form"
14898 " - DIE at %s [in module %s]"),
14899 sect_offset_str (die->sect_off),
14900 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14901 return 0;
14902 }
14903
14904 ULONGEST align;
14905 if (attr->form == DW_FORM_sdata)
14906 {
14907 LONGEST val = DW_SND (attr);
14908 if (val < 0)
14909 {
14910 complaint (_("DW_AT_alignment value must not be negative"
14911 " - DIE at %s [in module %s]"),
14912 sect_offset_str (die->sect_off),
14913 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14914 return 0;
14915 }
14916 align = val;
14917 }
14918 else
14919 align = DW_UNSND (attr);
14920
14921 if (align == 0)
14922 {
14923 complaint (_("DW_AT_alignment value must not be zero"
14924 " - DIE at %s [in module %s]"),
14925 sect_offset_str (die->sect_off),
14926 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14927 return 0;
14928 }
14929 if ((align & (align - 1)) != 0)
14930 {
14931 complaint (_("DW_AT_alignment value must be a power of 2"
14932 " - DIE at %s [in module %s]"),
14933 sect_offset_str (die->sect_off),
14934 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14935 return 0;
14936 }
14937
14938 return align;
14939 }
14940
14941 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14942 the alignment for TYPE. */
14943
14944 static void
14945 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14946 struct type *type)
14947 {
14948 if (!set_type_align (type, get_alignment (cu, die)))
14949 complaint (_("DW_AT_alignment value too large"
14950 " - DIE at %s [in module %s]"),
14951 sect_offset_str (die->sect_off),
14952 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14953 }
14954
14955 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14956 constant for a type, according to DWARF5 spec, Table 5.5. */
14957
14958 static bool
14959 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14960 {
14961 switch (value)
14962 {
14963 case DW_CC_normal:
14964 case DW_CC_pass_by_reference:
14965 case DW_CC_pass_by_value:
14966 return true;
14967
14968 default:
14969 complaint (_("unrecognized DW_AT_calling_convention value "
14970 "(%s) for a type"), pulongest (value));
14971 return false;
14972 }
14973 }
14974
14975 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14976 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14977 also according to GNU-specific values (see include/dwarf2.h). */
14978
14979 static bool
14980 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14981 {
14982 switch (value)
14983 {
14984 case DW_CC_normal:
14985 case DW_CC_program:
14986 case DW_CC_nocall:
14987 return true;
14988
14989 case DW_CC_GNU_renesas_sh:
14990 case DW_CC_GNU_borland_fastcall_i386:
14991 case DW_CC_GDB_IBM_OpenCL:
14992 return true;
14993
14994 default:
14995 complaint (_("unrecognized DW_AT_calling_convention value "
14996 "(%s) for a subroutine"), pulongest (value));
14997 return false;
14998 }
14999 }
15000
15001 /* Called when we find the DIE that starts a structure or union scope
15002 (definition) to create a type for the structure or union. Fill in
15003 the type's name and general properties; the members will not be
15004 processed until process_structure_scope. A symbol table entry for
15005 the type will also not be done until process_structure_scope (assuming
15006 the type has a name).
15007
15008 NOTE: we need to call these functions regardless of whether or not the
15009 DIE has a DW_AT_name attribute, since it might be an anonymous
15010 structure or union. This gets the type entered into our set of
15011 user defined types. */
15012
15013 static struct type *
15014 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15015 {
15016 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15017 struct type *type;
15018 struct attribute *attr;
15019 const char *name;
15020
15021 /* If the definition of this type lives in .debug_types, read that type.
15022 Don't follow DW_AT_specification though, that will take us back up
15023 the chain and we want to go down. */
15024 attr = die->attr (DW_AT_signature);
15025 if (attr != nullptr)
15026 {
15027 type = get_DW_AT_signature_type (die, attr, cu);
15028
15029 /* The type's CU may not be the same as CU.
15030 Ensure TYPE is recorded with CU in die_type_hash. */
15031 return set_die_type (die, type, cu);
15032 }
15033
15034 type = alloc_type (objfile);
15035 INIT_CPLUS_SPECIFIC (type);
15036
15037 name = dwarf2_name (die, cu);
15038 if (name != NULL)
15039 {
15040 if (cu->language == language_cplus
15041 || cu->language == language_d
15042 || cu->language == language_rust)
15043 {
15044 const char *full_name = dwarf2_full_name (name, die, cu);
15045
15046 /* dwarf2_full_name might have already finished building the DIE's
15047 type. If so, there is no need to continue. */
15048 if (get_die_type (die, cu) != NULL)
15049 return get_die_type (die, cu);
15050
15051 TYPE_NAME (type) = full_name;
15052 }
15053 else
15054 {
15055 /* The name is already allocated along with this objfile, so
15056 we don't need to duplicate it for the type. */
15057 TYPE_NAME (type) = name;
15058 }
15059 }
15060
15061 if (die->tag == DW_TAG_structure_type)
15062 {
15063 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15064 }
15065 else if (die->tag == DW_TAG_union_type)
15066 {
15067 TYPE_CODE (type) = TYPE_CODE_UNION;
15068 }
15069 else if (die->tag == DW_TAG_variant_part)
15070 {
15071 TYPE_CODE (type) = TYPE_CODE_UNION;
15072 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15073 }
15074 else
15075 {
15076 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15077 }
15078
15079 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15080 TYPE_DECLARED_CLASS (type) = 1;
15081
15082 /* Store the calling convention in the type if it's available in
15083 the die. Otherwise the calling convention remains set to
15084 the default value DW_CC_normal. */
15085 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15086 if (attr != nullptr
15087 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15088 {
15089 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15090 TYPE_CPLUS_CALLING_CONVENTION (type)
15091 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15092 }
15093
15094 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15095 if (attr != nullptr)
15096 {
15097 if (attr->form_is_constant ())
15098 TYPE_LENGTH (type) = DW_UNSND (attr);
15099 else
15100 {
15101 /* For the moment, dynamic type sizes are not supported
15102 by GDB's struct type. The actual size is determined
15103 on-demand when resolving the type of a given object,
15104 so set the type's length to zero for now. Otherwise,
15105 we record an expression as the length, and that expression
15106 could lead to a very large value, which could eventually
15107 lead to us trying to allocate that much memory when creating
15108 a value of that type. */
15109 TYPE_LENGTH (type) = 0;
15110 }
15111 }
15112 else
15113 {
15114 TYPE_LENGTH (type) = 0;
15115 }
15116
15117 maybe_set_alignment (cu, die, type);
15118
15119 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15120 {
15121 /* ICC<14 does not output the required DW_AT_declaration on
15122 incomplete types, but gives them a size of zero. */
15123 TYPE_STUB (type) = 1;
15124 }
15125 else
15126 TYPE_STUB_SUPPORTED (type) = 1;
15127
15128 if (die_is_declaration (die, cu))
15129 TYPE_STUB (type) = 1;
15130 else if (attr == NULL && die->child == NULL
15131 && producer_is_realview (cu->producer))
15132 /* RealView does not output the required DW_AT_declaration
15133 on incomplete types. */
15134 TYPE_STUB (type) = 1;
15135
15136 /* We need to add the type field to the die immediately so we don't
15137 infinitely recurse when dealing with pointers to the structure
15138 type within the structure itself. */
15139 set_die_type (die, type, cu);
15140
15141 /* set_die_type should be already done. */
15142 set_descriptive_type (type, die, cu);
15143
15144 return type;
15145 }
15146
15147 /* A helper for process_structure_scope that handles a single member
15148 DIE. */
15149
15150 static void
15151 handle_struct_member_die (struct die_info *child_die, struct type *type,
15152 struct field_info *fi,
15153 std::vector<struct symbol *> *template_args,
15154 struct dwarf2_cu *cu)
15155 {
15156 if (child_die->tag == DW_TAG_member
15157 || child_die->tag == DW_TAG_variable
15158 || child_die->tag == DW_TAG_variant_part)
15159 {
15160 /* NOTE: carlton/2002-11-05: A C++ static data member
15161 should be a DW_TAG_member that is a declaration, but
15162 all versions of G++ as of this writing (so through at
15163 least 3.2.1) incorrectly generate DW_TAG_variable
15164 tags for them instead. */
15165 dwarf2_add_field (fi, child_die, cu);
15166 }
15167 else if (child_die->tag == DW_TAG_subprogram)
15168 {
15169 /* Rust doesn't have member functions in the C++ sense.
15170 However, it does emit ordinary functions as children
15171 of a struct DIE. */
15172 if (cu->language == language_rust)
15173 read_func_scope (child_die, cu);
15174 else
15175 {
15176 /* C++ member function. */
15177 dwarf2_add_member_fn (fi, child_die, type, cu);
15178 }
15179 }
15180 else if (child_die->tag == DW_TAG_inheritance)
15181 {
15182 /* C++ base class field. */
15183 dwarf2_add_field (fi, child_die, cu);
15184 }
15185 else if (type_can_define_types (child_die))
15186 dwarf2_add_type_defn (fi, child_die, cu);
15187 else if (child_die->tag == DW_TAG_template_type_param
15188 || child_die->tag == DW_TAG_template_value_param)
15189 {
15190 struct symbol *arg = new_symbol (child_die, NULL, cu);
15191
15192 if (arg != NULL)
15193 template_args->push_back (arg);
15194 }
15195 else if (child_die->tag == DW_TAG_variant)
15196 {
15197 /* In a variant we want to get the discriminant and also add a
15198 field for our sole member child. */
15199 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15200
15201 for (die_info *variant_child = child_die->child;
15202 variant_child != NULL;
15203 variant_child = variant_child->sibling)
15204 {
15205 if (variant_child->tag == DW_TAG_member)
15206 {
15207 handle_struct_member_die (variant_child, type, fi,
15208 template_args, cu);
15209 /* Only handle the one. */
15210 break;
15211 }
15212 }
15213
15214 /* We don't handle this but we might as well report it if we see
15215 it. */
15216 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15217 complaint (_("DW_AT_discr_list is not supported yet"
15218 " - DIE at %s [in module %s]"),
15219 sect_offset_str (child_die->sect_off),
15220 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15221
15222 /* The first field was just added, so we can stash the
15223 discriminant there. */
15224 gdb_assert (!fi->fields.empty ());
15225 if (discr == NULL)
15226 fi->fields.back ().variant.default_branch = true;
15227 else
15228 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15229 }
15230 }
15231
15232 /* Finish creating a structure or union type, including filling in
15233 its members and creating a symbol for it. */
15234
15235 static void
15236 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15237 {
15238 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15239 struct die_info *child_die;
15240 struct type *type;
15241
15242 type = get_die_type (die, cu);
15243 if (type == NULL)
15244 type = read_structure_type (die, cu);
15245
15246 /* When reading a DW_TAG_variant_part, we need to notice when we
15247 read the discriminant member, so we can record it later in the
15248 discriminant_info. */
15249 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15250 sect_offset discr_offset {};
15251 bool has_template_parameters = false;
15252
15253 if (is_variant_part)
15254 {
15255 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15256 if (discr == NULL)
15257 {
15258 /* Maybe it's a univariant form, an extension we support.
15259 In this case arrange not to check the offset. */
15260 is_variant_part = false;
15261 }
15262 else if (discr->form_is_ref ())
15263 {
15264 struct dwarf2_cu *target_cu = cu;
15265 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15266
15267 discr_offset = target_die->sect_off;
15268 }
15269 else
15270 {
15271 complaint (_("DW_AT_discr does not have DIE reference form"
15272 " - DIE at %s [in module %s]"),
15273 sect_offset_str (die->sect_off),
15274 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15275 is_variant_part = false;
15276 }
15277 }
15278
15279 if (die->child != NULL && ! die_is_declaration (die, cu))
15280 {
15281 struct field_info fi;
15282 std::vector<struct symbol *> template_args;
15283
15284 child_die = die->child;
15285
15286 while (child_die && child_die->tag)
15287 {
15288 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15289
15290 if (is_variant_part && discr_offset == child_die->sect_off)
15291 fi.fields.back ().variant.is_discriminant = true;
15292
15293 child_die = child_die->sibling;
15294 }
15295
15296 /* Attach template arguments to type. */
15297 if (!template_args.empty ())
15298 {
15299 has_template_parameters = true;
15300 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15301 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15302 TYPE_TEMPLATE_ARGUMENTS (type)
15303 = XOBNEWVEC (&objfile->objfile_obstack,
15304 struct symbol *,
15305 TYPE_N_TEMPLATE_ARGUMENTS (type));
15306 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15307 template_args.data (),
15308 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15309 * sizeof (struct symbol *)));
15310 }
15311
15312 /* Attach fields and member functions to the type. */
15313 if (fi.nfields () > 0)
15314 dwarf2_attach_fields_to_type (&fi, type, cu);
15315 if (!fi.fnfieldlists.empty ())
15316 {
15317 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15318
15319 /* Get the type which refers to the base class (possibly this
15320 class itself) which contains the vtable pointer for the current
15321 class from the DW_AT_containing_type attribute. This use of
15322 DW_AT_containing_type is a GNU extension. */
15323
15324 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15325 {
15326 struct type *t = die_containing_type (die, cu);
15327
15328 set_type_vptr_basetype (type, t);
15329 if (type == t)
15330 {
15331 int i;
15332
15333 /* Our own class provides vtbl ptr. */
15334 for (i = TYPE_NFIELDS (t) - 1;
15335 i >= TYPE_N_BASECLASSES (t);
15336 --i)
15337 {
15338 const char *fieldname = TYPE_FIELD_NAME (t, i);
15339
15340 if (is_vtable_name (fieldname, cu))
15341 {
15342 set_type_vptr_fieldno (type, i);
15343 break;
15344 }
15345 }
15346
15347 /* Complain if virtual function table field not found. */
15348 if (i < TYPE_N_BASECLASSES (t))
15349 complaint (_("virtual function table pointer "
15350 "not found when defining class '%s'"),
15351 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15352 }
15353 else
15354 {
15355 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15356 }
15357 }
15358 else if (cu->producer
15359 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15360 {
15361 /* The IBM XLC compiler does not provide direct indication
15362 of the containing type, but the vtable pointer is
15363 always named __vfp. */
15364
15365 int i;
15366
15367 for (i = TYPE_NFIELDS (type) - 1;
15368 i >= TYPE_N_BASECLASSES (type);
15369 --i)
15370 {
15371 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15372 {
15373 set_type_vptr_fieldno (type, i);
15374 set_type_vptr_basetype (type, type);
15375 break;
15376 }
15377 }
15378 }
15379 }
15380
15381 /* Copy fi.typedef_field_list linked list elements content into the
15382 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15383 if (!fi.typedef_field_list.empty ())
15384 {
15385 int count = fi.typedef_field_list.size ();
15386
15387 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15388 TYPE_TYPEDEF_FIELD_ARRAY (type)
15389 = ((struct decl_field *)
15390 TYPE_ALLOC (type,
15391 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15392 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15393
15394 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15395 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15396 }
15397
15398 /* Copy fi.nested_types_list linked list elements content into the
15399 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15400 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15401 {
15402 int count = fi.nested_types_list.size ();
15403
15404 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15405 TYPE_NESTED_TYPES_ARRAY (type)
15406 = ((struct decl_field *)
15407 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15408 TYPE_NESTED_TYPES_COUNT (type) = count;
15409
15410 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15411 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15412 }
15413 }
15414
15415 quirk_gcc_member_function_pointer (type, objfile);
15416 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15417 cu->rust_unions.push_back (type);
15418
15419 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15420 snapshots) has been known to create a die giving a declaration
15421 for a class that has, as a child, a die giving a definition for a
15422 nested class. So we have to process our children even if the
15423 current die is a declaration. Normally, of course, a declaration
15424 won't have any children at all. */
15425
15426 child_die = die->child;
15427
15428 while (child_die != NULL && child_die->tag)
15429 {
15430 if (child_die->tag == DW_TAG_member
15431 || child_die->tag == DW_TAG_variable
15432 || child_die->tag == DW_TAG_inheritance
15433 || child_die->tag == DW_TAG_template_value_param
15434 || child_die->tag == DW_TAG_template_type_param)
15435 {
15436 /* Do nothing. */
15437 }
15438 else
15439 process_die (child_die, cu);
15440
15441 child_die = child_die->sibling;
15442 }
15443
15444 /* Do not consider external references. According to the DWARF standard,
15445 these DIEs are identified by the fact that they have no byte_size
15446 attribute, and a declaration attribute. */
15447 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15448 || !die_is_declaration (die, cu))
15449 {
15450 struct symbol *sym = new_symbol (die, type, cu);
15451
15452 if (has_template_parameters)
15453 {
15454 struct symtab *symtab;
15455 if (sym != nullptr)
15456 symtab = symbol_symtab (sym);
15457 else if (cu->line_header != nullptr)
15458 {
15459 /* Any related symtab will do. */
15460 symtab
15461 = cu->line_header->file_names ()[0].symtab;
15462 }
15463 else
15464 {
15465 symtab = nullptr;
15466 complaint (_("could not find suitable "
15467 "symtab for template parameter"
15468 " - DIE at %s [in module %s]"),
15469 sect_offset_str (die->sect_off),
15470 objfile_name (objfile));
15471 }
15472
15473 if (symtab != nullptr)
15474 {
15475 /* Make sure that the symtab is set on the new symbols.
15476 Even though they don't appear in this symtab directly,
15477 other parts of gdb assume that symbols do, and this is
15478 reasonably true. */
15479 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15480 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15481 }
15482 }
15483 }
15484 }
15485
15486 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15487 update TYPE using some information only available in DIE's children. */
15488
15489 static void
15490 update_enumeration_type_from_children (struct die_info *die,
15491 struct type *type,
15492 struct dwarf2_cu *cu)
15493 {
15494 struct die_info *child_die;
15495 int unsigned_enum = 1;
15496 int flag_enum = 1;
15497
15498 auto_obstack obstack;
15499
15500 for (child_die = die->child;
15501 child_die != NULL && child_die->tag;
15502 child_die = child_die->sibling)
15503 {
15504 struct attribute *attr;
15505 LONGEST value;
15506 const gdb_byte *bytes;
15507 struct dwarf2_locexpr_baton *baton;
15508 const char *name;
15509
15510 if (child_die->tag != DW_TAG_enumerator)
15511 continue;
15512
15513 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15514 if (attr == NULL)
15515 continue;
15516
15517 name = dwarf2_name (child_die, cu);
15518 if (name == NULL)
15519 name = "<anonymous enumerator>";
15520
15521 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15522 &value, &bytes, &baton);
15523 if (value < 0)
15524 {
15525 unsigned_enum = 0;
15526 flag_enum = 0;
15527 }
15528 else
15529 {
15530 if (count_one_bits_ll (value) >= 2)
15531 flag_enum = 0;
15532 }
15533
15534 /* If we already know that the enum type is neither unsigned, nor
15535 a flag type, no need to look at the rest of the enumerates. */
15536 if (!unsigned_enum && !flag_enum)
15537 break;
15538 }
15539
15540 if (unsigned_enum)
15541 TYPE_UNSIGNED (type) = 1;
15542 if (flag_enum)
15543 TYPE_FLAG_ENUM (type) = 1;
15544 }
15545
15546 /* Given a DW_AT_enumeration_type die, set its type. We do not
15547 complete the type's fields yet, or create any symbols. */
15548
15549 static struct type *
15550 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15551 {
15552 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15553 struct type *type;
15554 struct attribute *attr;
15555 const char *name;
15556
15557 /* If the definition of this type lives in .debug_types, read that type.
15558 Don't follow DW_AT_specification though, that will take us back up
15559 the chain and we want to go down. */
15560 attr = die->attr (DW_AT_signature);
15561 if (attr != nullptr)
15562 {
15563 type = get_DW_AT_signature_type (die, attr, cu);
15564
15565 /* The type's CU may not be the same as CU.
15566 Ensure TYPE is recorded with CU in die_type_hash. */
15567 return set_die_type (die, type, cu);
15568 }
15569
15570 type = alloc_type (objfile);
15571
15572 TYPE_CODE (type) = TYPE_CODE_ENUM;
15573 name = dwarf2_full_name (NULL, die, cu);
15574 if (name != NULL)
15575 TYPE_NAME (type) = name;
15576
15577 attr = dwarf2_attr (die, DW_AT_type, cu);
15578 if (attr != NULL)
15579 {
15580 struct type *underlying_type = die_type (die, cu);
15581
15582 TYPE_TARGET_TYPE (type) = underlying_type;
15583 }
15584
15585 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15586 if (attr != nullptr)
15587 {
15588 TYPE_LENGTH (type) = DW_UNSND (attr);
15589 }
15590 else
15591 {
15592 TYPE_LENGTH (type) = 0;
15593 }
15594
15595 maybe_set_alignment (cu, die, type);
15596
15597 /* The enumeration DIE can be incomplete. In Ada, any type can be
15598 declared as private in the package spec, and then defined only
15599 inside the package body. Such types are known as Taft Amendment
15600 Types. When another package uses such a type, an incomplete DIE
15601 may be generated by the compiler. */
15602 if (die_is_declaration (die, cu))
15603 TYPE_STUB (type) = 1;
15604
15605 /* Finish the creation of this type by using the enum's children.
15606 We must call this even when the underlying type has been provided
15607 so that we can determine if we're looking at a "flag" enum. */
15608 update_enumeration_type_from_children (die, type, cu);
15609
15610 /* If this type has an underlying type that is not a stub, then we
15611 may use its attributes. We always use the "unsigned" attribute
15612 in this situation, because ordinarily we guess whether the type
15613 is unsigned -- but the guess can be wrong and the underlying type
15614 can tell us the reality. However, we defer to a local size
15615 attribute if one exists, because this lets the compiler override
15616 the underlying type if needed. */
15617 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15618 {
15619 struct type *underlying_type = TYPE_TARGET_TYPE (type);
15620 underlying_type = check_typedef (underlying_type);
15621 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
15622 if (TYPE_LENGTH (type) == 0)
15623 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
15624 if (TYPE_RAW_ALIGN (type) == 0
15625 && TYPE_RAW_ALIGN (underlying_type) != 0)
15626 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
15627 }
15628
15629 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15630
15631 return set_die_type (die, type, cu);
15632 }
15633
15634 /* Given a pointer to a die which begins an enumeration, process all
15635 the dies that define the members of the enumeration, and create the
15636 symbol for the enumeration type.
15637
15638 NOTE: We reverse the order of the element list. */
15639
15640 static void
15641 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15642 {
15643 struct type *this_type;
15644
15645 this_type = get_die_type (die, cu);
15646 if (this_type == NULL)
15647 this_type = read_enumeration_type (die, cu);
15648
15649 if (die->child != NULL)
15650 {
15651 struct die_info *child_die;
15652 struct symbol *sym;
15653 std::vector<struct field> fields;
15654 const char *name;
15655
15656 child_die = die->child;
15657 while (child_die && child_die->tag)
15658 {
15659 if (child_die->tag != DW_TAG_enumerator)
15660 {
15661 process_die (child_die, cu);
15662 }
15663 else
15664 {
15665 name = dwarf2_name (child_die, cu);
15666 if (name)
15667 {
15668 sym = new_symbol (child_die, this_type, cu);
15669
15670 fields.emplace_back ();
15671 struct field &field = fields.back ();
15672
15673 FIELD_NAME (field) = sym->linkage_name ();
15674 FIELD_TYPE (field) = NULL;
15675 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15676 FIELD_BITSIZE (field) = 0;
15677 }
15678 }
15679
15680 child_die = child_die->sibling;
15681 }
15682
15683 if (!fields.empty ())
15684 {
15685 TYPE_NFIELDS (this_type) = fields.size ();
15686 TYPE_FIELDS (this_type) = (struct field *)
15687 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15688 memcpy (TYPE_FIELDS (this_type), fields.data (),
15689 sizeof (struct field) * fields.size ());
15690 }
15691 }
15692
15693 /* If we are reading an enum from a .debug_types unit, and the enum
15694 is a declaration, and the enum is not the signatured type in the
15695 unit, then we do not want to add a symbol for it. Adding a
15696 symbol would in some cases obscure the true definition of the
15697 enum, giving users an incomplete type when the definition is
15698 actually available. Note that we do not want to do this for all
15699 enums which are just declarations, because C++0x allows forward
15700 enum declarations. */
15701 if (cu->per_cu->is_debug_types
15702 && die_is_declaration (die, cu))
15703 {
15704 struct signatured_type *sig_type;
15705
15706 sig_type = (struct signatured_type *) cu->per_cu;
15707 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15708 if (sig_type->type_offset_in_section != die->sect_off)
15709 return;
15710 }
15711
15712 new_symbol (die, this_type, cu);
15713 }
15714
15715 /* Extract all information from a DW_TAG_array_type DIE and put it in
15716 the DIE's type field. For now, this only handles one dimensional
15717 arrays. */
15718
15719 static struct type *
15720 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15721 {
15722 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15723 struct die_info *child_die;
15724 struct type *type;
15725 struct type *element_type, *range_type, *index_type;
15726 struct attribute *attr;
15727 const char *name;
15728 struct dynamic_prop *byte_stride_prop = NULL;
15729 unsigned int bit_stride = 0;
15730
15731 element_type = die_type (die, cu);
15732
15733 /* The die_type call above may have already set the type for this DIE. */
15734 type = get_die_type (die, cu);
15735 if (type)
15736 return type;
15737
15738 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15739 if (attr != NULL)
15740 {
15741 int stride_ok;
15742 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
15743
15744 byte_stride_prop
15745 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15746 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15747 prop_type);
15748 if (!stride_ok)
15749 {
15750 complaint (_("unable to read array DW_AT_byte_stride "
15751 " - DIE at %s [in module %s]"),
15752 sect_offset_str (die->sect_off),
15753 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15754 /* Ignore this attribute. We will likely not be able to print
15755 arrays of this type correctly, but there is little we can do
15756 to help if we cannot read the attribute's value. */
15757 byte_stride_prop = NULL;
15758 }
15759 }
15760
15761 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15762 if (attr != NULL)
15763 bit_stride = DW_UNSND (attr);
15764
15765 /* Irix 6.2 native cc creates array types without children for
15766 arrays with unspecified length. */
15767 if (die->child == NULL)
15768 {
15769 index_type = objfile_type (objfile)->builtin_int;
15770 range_type = create_static_range_type (NULL, index_type, 0, -1);
15771 type = create_array_type_with_stride (NULL, element_type, range_type,
15772 byte_stride_prop, bit_stride);
15773 return set_die_type (die, type, cu);
15774 }
15775
15776 std::vector<struct type *> range_types;
15777 child_die = die->child;
15778 while (child_die && child_die->tag)
15779 {
15780 if (child_die->tag == DW_TAG_subrange_type)
15781 {
15782 struct type *child_type = read_type_die (child_die, cu);
15783
15784 if (child_type != NULL)
15785 {
15786 /* The range type was succesfully read. Save it for the
15787 array type creation. */
15788 range_types.push_back (child_type);
15789 }
15790 }
15791 child_die = child_die->sibling;
15792 }
15793
15794 /* Dwarf2 dimensions are output from left to right, create the
15795 necessary array types in backwards order. */
15796
15797 type = element_type;
15798
15799 if (read_array_order (die, cu) == DW_ORD_col_major)
15800 {
15801 int i = 0;
15802
15803 while (i < range_types.size ())
15804 type = create_array_type_with_stride (NULL, type, range_types[i++],
15805 byte_stride_prop, bit_stride);
15806 }
15807 else
15808 {
15809 size_t ndim = range_types.size ();
15810 while (ndim-- > 0)
15811 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15812 byte_stride_prop, bit_stride);
15813 }
15814
15815 /* Understand Dwarf2 support for vector types (like they occur on
15816 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15817 array type. This is not part of the Dwarf2/3 standard yet, but a
15818 custom vendor extension. The main difference between a regular
15819 array and the vector variant is that vectors are passed by value
15820 to functions. */
15821 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15822 if (attr != nullptr)
15823 make_vector_type (type);
15824
15825 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15826 implementation may choose to implement triple vectors using this
15827 attribute. */
15828 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15829 if (attr != nullptr)
15830 {
15831 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15832 TYPE_LENGTH (type) = DW_UNSND (attr);
15833 else
15834 complaint (_("DW_AT_byte_size for array type smaller "
15835 "than the total size of elements"));
15836 }
15837
15838 name = dwarf2_name (die, cu);
15839 if (name)
15840 TYPE_NAME (type) = name;
15841
15842 maybe_set_alignment (cu, die, type);
15843
15844 /* Install the type in the die. */
15845 set_die_type (die, type, cu);
15846
15847 /* set_die_type should be already done. */
15848 set_descriptive_type (type, die, cu);
15849
15850 return type;
15851 }
15852
15853 static enum dwarf_array_dim_ordering
15854 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15855 {
15856 struct attribute *attr;
15857
15858 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15859
15860 if (attr != nullptr)
15861 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15862
15863 /* GNU F77 is a special case, as at 08/2004 array type info is the
15864 opposite order to the dwarf2 specification, but data is still
15865 laid out as per normal fortran.
15866
15867 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15868 version checking. */
15869
15870 if (cu->language == language_fortran
15871 && cu->producer && strstr (cu->producer, "GNU F77"))
15872 {
15873 return DW_ORD_row_major;
15874 }
15875
15876 switch (cu->language_defn->la_array_ordering)
15877 {
15878 case array_column_major:
15879 return DW_ORD_col_major;
15880 case array_row_major:
15881 default:
15882 return DW_ORD_row_major;
15883 };
15884 }
15885
15886 /* Extract all information from a DW_TAG_set_type DIE and put it in
15887 the DIE's type field. */
15888
15889 static struct type *
15890 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15891 {
15892 struct type *domain_type, *set_type;
15893 struct attribute *attr;
15894
15895 domain_type = die_type (die, cu);
15896
15897 /* The die_type call above may have already set the type for this DIE. */
15898 set_type = get_die_type (die, cu);
15899 if (set_type)
15900 return set_type;
15901
15902 set_type = create_set_type (NULL, domain_type);
15903
15904 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15905 if (attr != nullptr)
15906 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15907
15908 maybe_set_alignment (cu, die, set_type);
15909
15910 return set_die_type (die, set_type, cu);
15911 }
15912
15913 /* A helper for read_common_block that creates a locexpr baton.
15914 SYM is the symbol which we are marking as computed.
15915 COMMON_DIE is the DIE for the common block.
15916 COMMON_LOC is the location expression attribute for the common
15917 block itself.
15918 MEMBER_LOC is the location expression attribute for the particular
15919 member of the common block that we are processing.
15920 CU is the CU from which the above come. */
15921
15922 static void
15923 mark_common_block_symbol_computed (struct symbol *sym,
15924 struct die_info *common_die,
15925 struct attribute *common_loc,
15926 struct attribute *member_loc,
15927 struct dwarf2_cu *cu)
15928 {
15929 struct dwarf2_per_objfile *dwarf2_per_objfile
15930 = cu->per_cu->dwarf2_per_objfile;
15931 struct objfile *objfile = dwarf2_per_objfile->objfile;
15932 struct dwarf2_locexpr_baton *baton;
15933 gdb_byte *ptr;
15934 unsigned int cu_off;
15935 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15936 LONGEST offset = 0;
15937
15938 gdb_assert (common_loc && member_loc);
15939 gdb_assert (common_loc->form_is_block ());
15940 gdb_assert (member_loc->form_is_block ()
15941 || member_loc->form_is_constant ());
15942
15943 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15944 baton->per_cu = cu->per_cu;
15945 gdb_assert (baton->per_cu);
15946
15947 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15948
15949 if (member_loc->form_is_constant ())
15950 {
15951 offset = member_loc->constant_value (0);
15952 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15953 }
15954 else
15955 baton->size += DW_BLOCK (member_loc)->size;
15956
15957 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15958 baton->data = ptr;
15959
15960 *ptr++ = DW_OP_call4;
15961 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15962 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15963 ptr += 4;
15964
15965 if (member_loc->form_is_constant ())
15966 {
15967 *ptr++ = DW_OP_addr;
15968 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15969 ptr += cu->header.addr_size;
15970 }
15971 else
15972 {
15973 /* We have to copy the data here, because DW_OP_call4 will only
15974 use a DW_AT_location attribute. */
15975 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15976 ptr += DW_BLOCK (member_loc)->size;
15977 }
15978
15979 *ptr++ = DW_OP_plus;
15980 gdb_assert (ptr - baton->data == baton->size);
15981
15982 SYMBOL_LOCATION_BATON (sym) = baton;
15983 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
15984 }
15985
15986 /* Create appropriate locally-scoped variables for all the
15987 DW_TAG_common_block entries. Also create a struct common_block
15988 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15989 is used to separate the common blocks name namespace from regular
15990 variable names. */
15991
15992 static void
15993 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15994 {
15995 struct attribute *attr;
15996
15997 attr = dwarf2_attr (die, DW_AT_location, cu);
15998 if (attr != nullptr)
15999 {
16000 /* Support the .debug_loc offsets. */
16001 if (attr->form_is_block ())
16002 {
16003 /* Ok. */
16004 }
16005 else if (attr->form_is_section_offset ())
16006 {
16007 dwarf2_complex_location_expr_complaint ();
16008 attr = NULL;
16009 }
16010 else
16011 {
16012 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16013 "common block member");
16014 attr = NULL;
16015 }
16016 }
16017
16018 if (die->child != NULL)
16019 {
16020 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16021 struct die_info *child_die;
16022 size_t n_entries = 0, size;
16023 struct common_block *common_block;
16024 struct symbol *sym;
16025
16026 for (child_die = die->child;
16027 child_die && child_die->tag;
16028 child_die = child_die->sibling)
16029 ++n_entries;
16030
16031 size = (sizeof (struct common_block)
16032 + (n_entries - 1) * sizeof (struct symbol *));
16033 common_block
16034 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16035 size);
16036 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16037 common_block->n_entries = 0;
16038
16039 for (child_die = die->child;
16040 child_die && child_die->tag;
16041 child_die = child_die->sibling)
16042 {
16043 /* Create the symbol in the DW_TAG_common_block block in the current
16044 symbol scope. */
16045 sym = new_symbol (child_die, NULL, cu);
16046 if (sym != NULL)
16047 {
16048 struct attribute *member_loc;
16049
16050 common_block->contents[common_block->n_entries++] = sym;
16051
16052 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16053 cu);
16054 if (member_loc)
16055 {
16056 /* GDB has handled this for a long time, but it is
16057 not specified by DWARF. It seems to have been
16058 emitted by gfortran at least as recently as:
16059 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16060 complaint (_("Variable in common block has "
16061 "DW_AT_data_member_location "
16062 "- DIE at %s [in module %s]"),
16063 sect_offset_str (child_die->sect_off),
16064 objfile_name (objfile));
16065
16066 if (member_loc->form_is_section_offset ())
16067 dwarf2_complex_location_expr_complaint ();
16068 else if (member_loc->form_is_constant ()
16069 || member_loc->form_is_block ())
16070 {
16071 if (attr != nullptr)
16072 mark_common_block_symbol_computed (sym, die, attr,
16073 member_loc, cu);
16074 }
16075 else
16076 dwarf2_complex_location_expr_complaint ();
16077 }
16078 }
16079 }
16080
16081 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16082 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16083 }
16084 }
16085
16086 /* Create a type for a C++ namespace. */
16087
16088 static struct type *
16089 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16090 {
16091 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16092 const char *previous_prefix, *name;
16093 int is_anonymous;
16094 struct type *type;
16095
16096 /* For extensions, reuse the type of the original namespace. */
16097 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16098 {
16099 struct die_info *ext_die;
16100 struct dwarf2_cu *ext_cu = cu;
16101
16102 ext_die = dwarf2_extension (die, &ext_cu);
16103 type = read_type_die (ext_die, ext_cu);
16104
16105 /* EXT_CU may not be the same as CU.
16106 Ensure TYPE is recorded with CU in die_type_hash. */
16107 return set_die_type (die, type, cu);
16108 }
16109
16110 name = namespace_name (die, &is_anonymous, cu);
16111
16112 /* Now build the name of the current namespace. */
16113
16114 previous_prefix = determine_prefix (die, cu);
16115 if (previous_prefix[0] != '\0')
16116 name = typename_concat (&objfile->objfile_obstack,
16117 previous_prefix, name, 0, cu);
16118
16119 /* Create the type. */
16120 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16121
16122 return set_die_type (die, type, cu);
16123 }
16124
16125 /* Read a namespace scope. */
16126
16127 static void
16128 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16129 {
16130 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16131 int is_anonymous;
16132
16133 /* Add a symbol associated to this if we haven't seen the namespace
16134 before. Also, add a using directive if it's an anonymous
16135 namespace. */
16136
16137 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16138 {
16139 struct type *type;
16140
16141 type = read_type_die (die, cu);
16142 new_symbol (die, type, cu);
16143
16144 namespace_name (die, &is_anonymous, cu);
16145 if (is_anonymous)
16146 {
16147 const char *previous_prefix = determine_prefix (die, cu);
16148
16149 std::vector<const char *> excludes;
16150 add_using_directive (using_directives (cu),
16151 previous_prefix, TYPE_NAME (type), NULL,
16152 NULL, excludes, 0, &objfile->objfile_obstack);
16153 }
16154 }
16155
16156 if (die->child != NULL)
16157 {
16158 struct die_info *child_die = die->child;
16159
16160 while (child_die && child_die->tag)
16161 {
16162 process_die (child_die, cu);
16163 child_die = child_die->sibling;
16164 }
16165 }
16166 }
16167
16168 /* Read a Fortran module as type. This DIE can be only a declaration used for
16169 imported module. Still we need that type as local Fortran "use ... only"
16170 declaration imports depend on the created type in determine_prefix. */
16171
16172 static struct type *
16173 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16174 {
16175 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16176 const char *module_name;
16177 struct type *type;
16178
16179 module_name = dwarf2_name (die, cu);
16180 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16181
16182 return set_die_type (die, type, cu);
16183 }
16184
16185 /* Read a Fortran module. */
16186
16187 static void
16188 read_module (struct die_info *die, struct dwarf2_cu *cu)
16189 {
16190 struct die_info *child_die = die->child;
16191 struct type *type;
16192
16193 type = read_type_die (die, cu);
16194 new_symbol (die, type, cu);
16195
16196 while (child_die && child_die->tag)
16197 {
16198 process_die (child_die, cu);
16199 child_die = child_die->sibling;
16200 }
16201 }
16202
16203 /* Return the name of the namespace represented by DIE. Set
16204 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16205 namespace. */
16206
16207 static const char *
16208 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16209 {
16210 struct die_info *current_die;
16211 const char *name = NULL;
16212
16213 /* Loop through the extensions until we find a name. */
16214
16215 for (current_die = die;
16216 current_die != NULL;
16217 current_die = dwarf2_extension (die, &cu))
16218 {
16219 /* We don't use dwarf2_name here so that we can detect the absence
16220 of a name -> anonymous namespace. */
16221 name = dwarf2_string_attr (die, DW_AT_name, cu);
16222
16223 if (name != NULL)
16224 break;
16225 }
16226
16227 /* Is it an anonymous namespace? */
16228
16229 *is_anonymous = (name == NULL);
16230 if (*is_anonymous)
16231 name = CP_ANONYMOUS_NAMESPACE_STR;
16232
16233 return name;
16234 }
16235
16236 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16237 the user defined type vector. */
16238
16239 static struct type *
16240 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16241 {
16242 struct gdbarch *gdbarch
16243 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16244 struct comp_unit_head *cu_header = &cu->header;
16245 struct type *type;
16246 struct attribute *attr_byte_size;
16247 struct attribute *attr_address_class;
16248 int byte_size, addr_class;
16249 struct type *target_type;
16250
16251 target_type = die_type (die, cu);
16252
16253 /* The die_type call above may have already set the type for this DIE. */
16254 type = get_die_type (die, cu);
16255 if (type)
16256 return type;
16257
16258 type = lookup_pointer_type (target_type);
16259
16260 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16261 if (attr_byte_size)
16262 byte_size = DW_UNSND (attr_byte_size);
16263 else
16264 byte_size = cu_header->addr_size;
16265
16266 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16267 if (attr_address_class)
16268 addr_class = DW_UNSND (attr_address_class);
16269 else
16270 addr_class = DW_ADDR_none;
16271
16272 ULONGEST alignment = get_alignment (cu, die);
16273
16274 /* If the pointer size, alignment, or address class is different
16275 than the default, create a type variant marked as such and set
16276 the length accordingly. */
16277 if (TYPE_LENGTH (type) != byte_size
16278 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16279 && alignment != TYPE_RAW_ALIGN (type))
16280 || addr_class != DW_ADDR_none)
16281 {
16282 if (gdbarch_address_class_type_flags_p (gdbarch))
16283 {
16284 int type_flags;
16285
16286 type_flags = gdbarch_address_class_type_flags
16287 (gdbarch, byte_size, addr_class);
16288 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16289 == 0);
16290 type = make_type_with_address_space (type, type_flags);
16291 }
16292 else if (TYPE_LENGTH (type) != byte_size)
16293 {
16294 complaint (_("invalid pointer size %d"), byte_size);
16295 }
16296 else if (TYPE_RAW_ALIGN (type) != alignment)
16297 {
16298 complaint (_("Invalid DW_AT_alignment"
16299 " - DIE at %s [in module %s]"),
16300 sect_offset_str (die->sect_off),
16301 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16302 }
16303 else
16304 {
16305 /* Should we also complain about unhandled address classes? */
16306 }
16307 }
16308
16309 TYPE_LENGTH (type) = byte_size;
16310 set_type_align (type, alignment);
16311 return set_die_type (die, type, cu);
16312 }
16313
16314 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16315 the user defined type vector. */
16316
16317 static struct type *
16318 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16319 {
16320 struct type *type;
16321 struct type *to_type;
16322 struct type *domain;
16323
16324 to_type = die_type (die, cu);
16325 domain = die_containing_type (die, cu);
16326
16327 /* The calls above may have already set the type for this DIE. */
16328 type = get_die_type (die, cu);
16329 if (type)
16330 return type;
16331
16332 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16333 type = lookup_methodptr_type (to_type);
16334 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16335 {
16336 struct type *new_type
16337 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16338
16339 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16340 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16341 TYPE_VARARGS (to_type));
16342 type = lookup_methodptr_type (new_type);
16343 }
16344 else
16345 type = lookup_memberptr_type (to_type, domain);
16346
16347 return set_die_type (die, type, cu);
16348 }
16349
16350 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16351 the user defined type vector. */
16352
16353 static struct type *
16354 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16355 enum type_code refcode)
16356 {
16357 struct comp_unit_head *cu_header = &cu->header;
16358 struct type *type, *target_type;
16359 struct attribute *attr;
16360
16361 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16362
16363 target_type = die_type (die, cu);
16364
16365 /* The die_type call above may have already set the type for this DIE. */
16366 type = get_die_type (die, cu);
16367 if (type)
16368 return type;
16369
16370 type = lookup_reference_type (target_type, refcode);
16371 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16372 if (attr != nullptr)
16373 {
16374 TYPE_LENGTH (type) = DW_UNSND (attr);
16375 }
16376 else
16377 {
16378 TYPE_LENGTH (type) = cu_header->addr_size;
16379 }
16380 maybe_set_alignment (cu, die, type);
16381 return set_die_type (die, type, cu);
16382 }
16383
16384 /* Add the given cv-qualifiers to the element type of the array. GCC
16385 outputs DWARF type qualifiers that apply to an array, not the
16386 element type. But GDB relies on the array element type to carry
16387 the cv-qualifiers. This mimics section 6.7.3 of the C99
16388 specification. */
16389
16390 static struct type *
16391 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16392 struct type *base_type, int cnst, int voltl)
16393 {
16394 struct type *el_type, *inner_array;
16395
16396 base_type = copy_type (base_type);
16397 inner_array = base_type;
16398
16399 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16400 {
16401 TYPE_TARGET_TYPE (inner_array) =
16402 copy_type (TYPE_TARGET_TYPE (inner_array));
16403 inner_array = TYPE_TARGET_TYPE (inner_array);
16404 }
16405
16406 el_type = TYPE_TARGET_TYPE (inner_array);
16407 cnst |= TYPE_CONST (el_type);
16408 voltl |= TYPE_VOLATILE (el_type);
16409 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16410
16411 return set_die_type (die, base_type, cu);
16412 }
16413
16414 static struct type *
16415 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16416 {
16417 struct type *base_type, *cv_type;
16418
16419 base_type = die_type (die, cu);
16420
16421 /* The die_type call above may have already set the type for this DIE. */
16422 cv_type = get_die_type (die, cu);
16423 if (cv_type)
16424 return cv_type;
16425
16426 /* In case the const qualifier is applied to an array type, the element type
16427 is so qualified, not the array type (section 6.7.3 of C99). */
16428 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16429 return add_array_cv_type (die, cu, base_type, 1, 0);
16430
16431 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16432 return set_die_type (die, cv_type, cu);
16433 }
16434
16435 static struct type *
16436 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16437 {
16438 struct type *base_type, *cv_type;
16439
16440 base_type = die_type (die, cu);
16441
16442 /* The die_type call above may have already set the type for this DIE. */
16443 cv_type = get_die_type (die, cu);
16444 if (cv_type)
16445 return cv_type;
16446
16447 /* In case the volatile qualifier is applied to an array type, the
16448 element type is so qualified, not the array type (section 6.7.3
16449 of C99). */
16450 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16451 return add_array_cv_type (die, cu, base_type, 0, 1);
16452
16453 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16454 return set_die_type (die, cv_type, cu);
16455 }
16456
16457 /* Handle DW_TAG_restrict_type. */
16458
16459 static struct type *
16460 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16461 {
16462 struct type *base_type, *cv_type;
16463
16464 base_type = die_type (die, cu);
16465
16466 /* The die_type call above may have already set the type for this DIE. */
16467 cv_type = get_die_type (die, cu);
16468 if (cv_type)
16469 return cv_type;
16470
16471 cv_type = make_restrict_type (base_type);
16472 return set_die_type (die, cv_type, cu);
16473 }
16474
16475 /* Handle DW_TAG_atomic_type. */
16476
16477 static struct type *
16478 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16479 {
16480 struct type *base_type, *cv_type;
16481
16482 base_type = die_type (die, cu);
16483
16484 /* The die_type call above may have already set the type for this DIE. */
16485 cv_type = get_die_type (die, cu);
16486 if (cv_type)
16487 return cv_type;
16488
16489 cv_type = make_atomic_type (base_type);
16490 return set_die_type (die, cv_type, cu);
16491 }
16492
16493 /* Extract all information from a DW_TAG_string_type DIE and add to
16494 the user defined type vector. It isn't really a user defined type,
16495 but it behaves like one, with other DIE's using an AT_user_def_type
16496 attribute to reference it. */
16497
16498 static struct type *
16499 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16500 {
16501 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16502 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16503 struct type *type, *range_type, *index_type, *char_type;
16504 struct attribute *attr;
16505 struct dynamic_prop prop;
16506 bool length_is_constant = true;
16507 LONGEST length;
16508
16509 /* There are a couple of places where bit sizes might be made use of
16510 when parsing a DW_TAG_string_type, however, no producer that we know
16511 of make use of these. Handling bit sizes that are a multiple of the
16512 byte size is easy enough, but what about other bit sizes? Lets deal
16513 with that problem when we have to. Warn about these attributes being
16514 unsupported, then parse the type and ignore them like we always
16515 have. */
16516 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16517 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16518 {
16519 static bool warning_printed = false;
16520 if (!warning_printed)
16521 {
16522 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16523 "currently supported on DW_TAG_string_type."));
16524 warning_printed = true;
16525 }
16526 }
16527
16528 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16529 if (attr != nullptr && !attr->form_is_constant ())
16530 {
16531 /* The string length describes the location at which the length of
16532 the string can be found. The size of the length field can be
16533 specified with one of the attributes below. */
16534 struct type *prop_type;
16535 struct attribute *len
16536 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16537 if (len == nullptr)
16538 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16539 if (len != nullptr && len->form_is_constant ())
16540 {
16541 /* Pass 0 as the default as we know this attribute is constant
16542 and the default value will not be returned. */
16543 LONGEST sz = len->constant_value (0);
16544 prop_type = cu->per_cu->int_type (sz, true);
16545 }
16546 else
16547 {
16548 /* If the size is not specified then we assume it is the size of
16549 an address on this target. */
16550 prop_type = cu->per_cu->addr_sized_int_type (true);
16551 }
16552
16553 /* Convert the attribute into a dynamic property. */
16554 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16555 length = 1;
16556 else
16557 length_is_constant = false;
16558 }
16559 else if (attr != nullptr)
16560 {
16561 /* This DW_AT_string_length just contains the length with no
16562 indirection. There's no need to create a dynamic property in this
16563 case. Pass 0 for the default value as we know it will not be
16564 returned in this case. */
16565 length = attr->constant_value (0);
16566 }
16567 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16568 {
16569 /* We don't currently support non-constant byte sizes for strings. */
16570 length = attr->constant_value (1);
16571 }
16572 else
16573 {
16574 /* Use 1 as a fallback length if we have nothing else. */
16575 length = 1;
16576 }
16577
16578 index_type = objfile_type (objfile)->builtin_int;
16579 if (length_is_constant)
16580 range_type = create_static_range_type (NULL, index_type, 1, length);
16581 else
16582 {
16583 struct dynamic_prop low_bound;
16584
16585 low_bound.kind = PROP_CONST;
16586 low_bound.data.const_val = 1;
16587 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16588 }
16589 char_type = language_string_char_type (cu->language_defn, gdbarch);
16590 type = create_string_type (NULL, char_type, range_type);
16591
16592 return set_die_type (die, type, cu);
16593 }
16594
16595 /* Assuming that DIE corresponds to a function, returns nonzero
16596 if the function is prototyped. */
16597
16598 static int
16599 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16600 {
16601 struct attribute *attr;
16602
16603 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16604 if (attr && (DW_UNSND (attr) != 0))
16605 return 1;
16606
16607 /* The DWARF standard implies that the DW_AT_prototyped attribute
16608 is only meaningful for C, but the concept also extends to other
16609 languages that allow unprototyped functions (Eg: Objective C).
16610 For all other languages, assume that functions are always
16611 prototyped. */
16612 if (cu->language != language_c
16613 && cu->language != language_objc
16614 && cu->language != language_opencl)
16615 return 1;
16616
16617 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16618 prototyped and unprototyped functions; default to prototyped,
16619 since that is more common in modern code (and RealView warns
16620 about unprototyped functions). */
16621 if (producer_is_realview (cu->producer))
16622 return 1;
16623
16624 return 0;
16625 }
16626
16627 /* Handle DIES due to C code like:
16628
16629 struct foo
16630 {
16631 int (*funcp)(int a, long l);
16632 int b;
16633 };
16634
16635 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16636
16637 static struct type *
16638 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16639 {
16640 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16641 struct type *type; /* Type that this function returns. */
16642 struct type *ftype; /* Function that returns above type. */
16643 struct attribute *attr;
16644
16645 type = die_type (die, cu);
16646
16647 /* The die_type call above may have already set the type for this DIE. */
16648 ftype = get_die_type (die, cu);
16649 if (ftype)
16650 return ftype;
16651
16652 ftype = lookup_function_type (type);
16653
16654 if (prototyped_function_p (die, cu))
16655 TYPE_PROTOTYPED (ftype) = 1;
16656
16657 /* Store the calling convention in the type if it's available in
16658 the subroutine die. Otherwise set the calling convention to
16659 the default value DW_CC_normal. */
16660 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16661 if (attr != nullptr
16662 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16663 TYPE_CALLING_CONVENTION (ftype)
16664 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16665 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16666 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16667 else
16668 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16669
16670 /* Record whether the function returns normally to its caller or not
16671 if the DWARF producer set that information. */
16672 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16673 if (attr && (DW_UNSND (attr) != 0))
16674 TYPE_NO_RETURN (ftype) = 1;
16675
16676 /* We need to add the subroutine type to the die immediately so
16677 we don't infinitely recurse when dealing with parameters
16678 declared as the same subroutine type. */
16679 set_die_type (die, ftype, cu);
16680
16681 if (die->child != NULL)
16682 {
16683 struct type *void_type = objfile_type (objfile)->builtin_void;
16684 struct die_info *child_die;
16685 int nparams, iparams;
16686
16687 /* Count the number of parameters.
16688 FIXME: GDB currently ignores vararg functions, but knows about
16689 vararg member functions. */
16690 nparams = 0;
16691 child_die = die->child;
16692 while (child_die && child_die->tag)
16693 {
16694 if (child_die->tag == DW_TAG_formal_parameter)
16695 nparams++;
16696 else if (child_die->tag == DW_TAG_unspecified_parameters)
16697 TYPE_VARARGS (ftype) = 1;
16698 child_die = child_die->sibling;
16699 }
16700
16701 /* Allocate storage for parameters and fill them in. */
16702 TYPE_NFIELDS (ftype) = nparams;
16703 TYPE_FIELDS (ftype) = (struct field *)
16704 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16705
16706 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16707 even if we error out during the parameters reading below. */
16708 for (iparams = 0; iparams < nparams; iparams++)
16709 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16710
16711 iparams = 0;
16712 child_die = die->child;
16713 while (child_die && child_die->tag)
16714 {
16715 if (child_die->tag == DW_TAG_formal_parameter)
16716 {
16717 struct type *arg_type;
16718
16719 /* DWARF version 2 has no clean way to discern C++
16720 static and non-static member functions. G++ helps
16721 GDB by marking the first parameter for non-static
16722 member functions (which is the this pointer) as
16723 artificial. We pass this information to
16724 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16725
16726 DWARF version 3 added DW_AT_object_pointer, which GCC
16727 4.5 does not yet generate. */
16728 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16729 if (attr != nullptr)
16730 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16731 else
16732 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16733 arg_type = die_type (child_die, cu);
16734
16735 /* RealView does not mark THIS as const, which the testsuite
16736 expects. GCC marks THIS as const in method definitions,
16737 but not in the class specifications (GCC PR 43053). */
16738 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16739 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16740 {
16741 int is_this = 0;
16742 struct dwarf2_cu *arg_cu = cu;
16743 const char *name = dwarf2_name (child_die, cu);
16744
16745 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16746 if (attr != nullptr)
16747 {
16748 /* If the compiler emits this, use it. */
16749 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16750 is_this = 1;
16751 }
16752 else if (name && strcmp (name, "this") == 0)
16753 /* Function definitions will have the argument names. */
16754 is_this = 1;
16755 else if (name == NULL && iparams == 0)
16756 /* Declarations may not have the names, so like
16757 elsewhere in GDB, assume an artificial first
16758 argument is "this". */
16759 is_this = 1;
16760
16761 if (is_this)
16762 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16763 arg_type, 0);
16764 }
16765
16766 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16767 iparams++;
16768 }
16769 child_die = child_die->sibling;
16770 }
16771 }
16772
16773 return ftype;
16774 }
16775
16776 static struct type *
16777 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16778 {
16779 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16780 const char *name = NULL;
16781 struct type *this_type, *target_type;
16782
16783 name = dwarf2_full_name (NULL, die, cu);
16784 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16785 TYPE_TARGET_STUB (this_type) = 1;
16786 set_die_type (die, this_type, cu);
16787 target_type = die_type (die, cu);
16788 if (target_type != this_type)
16789 TYPE_TARGET_TYPE (this_type) = target_type;
16790 else
16791 {
16792 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16793 spec and cause infinite loops in GDB. */
16794 complaint (_("Self-referential DW_TAG_typedef "
16795 "- DIE at %s [in module %s]"),
16796 sect_offset_str (die->sect_off), objfile_name (objfile));
16797 TYPE_TARGET_TYPE (this_type) = NULL;
16798 }
16799 if (name == NULL)
16800 {
16801 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16802 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16803 Handle these by just returning the target type, rather than
16804 constructing an anonymous typedef type and trying to handle this
16805 elsewhere. */
16806 set_die_type (die, target_type, cu);
16807 return target_type;
16808 }
16809 return this_type;
16810 }
16811
16812 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16813 (which may be different from NAME) to the architecture back-end to allow
16814 it to guess the correct format if necessary. */
16815
16816 static struct type *
16817 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16818 const char *name_hint, enum bfd_endian byte_order)
16819 {
16820 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16821 const struct floatformat **format;
16822 struct type *type;
16823
16824 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16825 if (format)
16826 type = init_float_type (objfile, bits, name, format, byte_order);
16827 else
16828 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16829
16830 return type;
16831 }
16832
16833 /* Allocate an integer type of size BITS and name NAME. */
16834
16835 static struct type *
16836 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16837 int bits, int unsigned_p, const char *name)
16838 {
16839 struct type *type;
16840
16841 /* Versions of Intel's C Compiler generate an integer type called "void"
16842 instead of using DW_TAG_unspecified_type. This has been seen on
16843 at least versions 14, 17, and 18. */
16844 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16845 && strcmp (name, "void") == 0)
16846 type = objfile_type (objfile)->builtin_void;
16847 else
16848 type = init_integer_type (objfile, bits, unsigned_p, name);
16849
16850 return type;
16851 }
16852
16853 /* Initialise and return a floating point type of size BITS suitable for
16854 use as a component of a complex number. The NAME_HINT is passed through
16855 when initialising the floating point type and is the name of the complex
16856 type.
16857
16858 As DWARF doesn't currently provide an explicit name for the components
16859 of a complex number, but it can be helpful to have these components
16860 named, we try to select a suitable name based on the size of the
16861 component. */
16862 static struct type *
16863 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16864 struct objfile *objfile,
16865 int bits, const char *name_hint,
16866 enum bfd_endian byte_order)
16867 {
16868 gdbarch *gdbarch = get_objfile_arch (objfile);
16869 struct type *tt = nullptr;
16870
16871 /* Try to find a suitable floating point builtin type of size BITS.
16872 We're going to use the name of this type as the name for the complex
16873 target type that we are about to create. */
16874 switch (cu->language)
16875 {
16876 case language_fortran:
16877 switch (bits)
16878 {
16879 case 32:
16880 tt = builtin_f_type (gdbarch)->builtin_real;
16881 break;
16882 case 64:
16883 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16884 break;
16885 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16886 case 128:
16887 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16888 break;
16889 }
16890 break;
16891 default:
16892 switch (bits)
16893 {
16894 case 32:
16895 tt = builtin_type (gdbarch)->builtin_float;
16896 break;
16897 case 64:
16898 tt = builtin_type (gdbarch)->builtin_double;
16899 break;
16900 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16901 case 128:
16902 tt = builtin_type (gdbarch)->builtin_long_double;
16903 break;
16904 }
16905 break;
16906 }
16907
16908 /* If the type we found doesn't match the size we were looking for, then
16909 pretend we didn't find a type at all, the complex target type we
16910 create will then be nameless. */
16911 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
16912 tt = nullptr;
16913
16914 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
16915 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
16916 }
16917
16918 /* Find a representation of a given base type and install
16919 it in the TYPE field of the die. */
16920
16921 static struct type *
16922 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16923 {
16924 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16925 struct type *type;
16926 struct attribute *attr;
16927 int encoding = 0, bits = 0;
16928 const char *name;
16929 gdbarch *arch;
16930
16931 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16932 if (attr != nullptr)
16933 encoding = DW_UNSND (attr);
16934 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16935 if (attr != nullptr)
16936 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16937 name = dwarf2_name (die, cu);
16938 if (!name)
16939 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
16940
16941 arch = get_objfile_arch (objfile);
16942 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16943
16944 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16945 if (attr)
16946 {
16947 int endianity = DW_UNSND (attr);
16948
16949 switch (endianity)
16950 {
16951 case DW_END_big:
16952 byte_order = BFD_ENDIAN_BIG;
16953 break;
16954 case DW_END_little:
16955 byte_order = BFD_ENDIAN_LITTLE;
16956 break;
16957 default:
16958 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16959 break;
16960 }
16961 }
16962
16963 switch (encoding)
16964 {
16965 case DW_ATE_address:
16966 /* Turn DW_ATE_address into a void * pointer. */
16967 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16968 type = init_pointer_type (objfile, bits, name, type);
16969 break;
16970 case DW_ATE_boolean:
16971 type = init_boolean_type (objfile, bits, 1, name);
16972 break;
16973 case DW_ATE_complex_float:
16974 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16975 byte_order);
16976 if (TYPE_CODE (type) == TYPE_CODE_ERROR)
16977 {
16978 if (name == nullptr)
16979 {
16980 struct obstack *obstack
16981 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
16982 name = obconcat (obstack, "_Complex ", TYPE_NAME (type),
16983 nullptr);
16984 }
16985 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16986 }
16987 else
16988 type = init_complex_type (name, type);
16989 break;
16990 case DW_ATE_decimal_float:
16991 type = init_decfloat_type (objfile, bits, name);
16992 break;
16993 case DW_ATE_float:
16994 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
16995 break;
16996 case DW_ATE_signed:
16997 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16998 break;
16999 case DW_ATE_unsigned:
17000 if (cu->language == language_fortran
17001 && name
17002 && startswith (name, "character("))
17003 type = init_character_type (objfile, bits, 1, name);
17004 else
17005 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17006 break;
17007 case DW_ATE_signed_char:
17008 if (cu->language == language_ada || cu->language == language_m2
17009 || cu->language == language_pascal
17010 || cu->language == language_fortran)
17011 type = init_character_type (objfile, bits, 0, name);
17012 else
17013 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17014 break;
17015 case DW_ATE_unsigned_char:
17016 if (cu->language == language_ada || cu->language == language_m2
17017 || cu->language == language_pascal
17018 || cu->language == language_fortran
17019 || cu->language == language_rust)
17020 type = init_character_type (objfile, bits, 1, name);
17021 else
17022 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17023 break;
17024 case DW_ATE_UTF:
17025 {
17026 if (bits == 16)
17027 type = builtin_type (arch)->builtin_char16;
17028 else if (bits == 32)
17029 type = builtin_type (arch)->builtin_char32;
17030 else
17031 {
17032 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17033 bits);
17034 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17035 }
17036 return set_die_type (die, type, cu);
17037 }
17038 break;
17039
17040 default:
17041 complaint (_("unsupported DW_AT_encoding: '%s'"),
17042 dwarf_type_encoding_name (encoding));
17043 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17044 break;
17045 }
17046
17047 if (name && strcmp (name, "char") == 0)
17048 TYPE_NOSIGN (type) = 1;
17049
17050 maybe_set_alignment (cu, die, type);
17051
17052 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17053
17054 return set_die_type (die, type, cu);
17055 }
17056
17057 /* Parse dwarf attribute if it's a block, reference or constant and put the
17058 resulting value of the attribute into struct bound_prop.
17059 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17060
17061 static int
17062 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17063 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17064 struct type *default_type)
17065 {
17066 struct dwarf2_property_baton *baton;
17067 struct obstack *obstack
17068 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17069
17070 gdb_assert (default_type != NULL);
17071
17072 if (attr == NULL || prop == NULL)
17073 return 0;
17074
17075 if (attr->form_is_block ())
17076 {
17077 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17078 baton->property_type = default_type;
17079 baton->locexpr.per_cu = cu->per_cu;
17080 baton->locexpr.size = DW_BLOCK (attr)->size;
17081 baton->locexpr.data = DW_BLOCK (attr)->data;
17082 switch (attr->name)
17083 {
17084 case DW_AT_string_length:
17085 baton->locexpr.is_reference = true;
17086 break;
17087 default:
17088 baton->locexpr.is_reference = false;
17089 break;
17090 }
17091 prop->data.baton = baton;
17092 prop->kind = PROP_LOCEXPR;
17093 gdb_assert (prop->data.baton != NULL);
17094 }
17095 else if (attr->form_is_ref ())
17096 {
17097 struct dwarf2_cu *target_cu = cu;
17098 struct die_info *target_die;
17099 struct attribute *target_attr;
17100
17101 target_die = follow_die_ref (die, attr, &target_cu);
17102 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17103 if (target_attr == NULL)
17104 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17105 target_cu);
17106 if (target_attr == NULL)
17107 return 0;
17108
17109 switch (target_attr->name)
17110 {
17111 case DW_AT_location:
17112 if (target_attr->form_is_section_offset ())
17113 {
17114 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17115 baton->property_type = die_type (target_die, target_cu);
17116 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17117 prop->data.baton = baton;
17118 prop->kind = PROP_LOCLIST;
17119 gdb_assert (prop->data.baton != NULL);
17120 }
17121 else if (target_attr->form_is_block ())
17122 {
17123 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17124 baton->property_type = die_type (target_die, target_cu);
17125 baton->locexpr.per_cu = cu->per_cu;
17126 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17127 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17128 baton->locexpr.is_reference = true;
17129 prop->data.baton = baton;
17130 prop->kind = PROP_LOCEXPR;
17131 gdb_assert (prop->data.baton != NULL);
17132 }
17133 else
17134 {
17135 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17136 "dynamic property");
17137 return 0;
17138 }
17139 break;
17140 case DW_AT_data_member_location:
17141 {
17142 LONGEST offset;
17143
17144 if (!handle_data_member_location (target_die, target_cu,
17145 &offset))
17146 return 0;
17147
17148 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17149 baton->property_type = read_type_die (target_die->parent,
17150 target_cu);
17151 baton->offset_info.offset = offset;
17152 baton->offset_info.type = die_type (target_die, target_cu);
17153 prop->data.baton = baton;
17154 prop->kind = PROP_ADDR_OFFSET;
17155 break;
17156 }
17157 }
17158 }
17159 else if (attr->form_is_constant ())
17160 {
17161 prop->data.const_val = attr->constant_value (0);
17162 prop->kind = PROP_CONST;
17163 }
17164 else
17165 {
17166 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17167 dwarf2_name (die, cu));
17168 return 0;
17169 }
17170
17171 return 1;
17172 }
17173
17174 /* See read.h. */
17175
17176 struct type *
17177 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17178 {
17179 struct objfile *objfile = dwarf2_per_objfile->objfile;
17180 struct type *int_type;
17181
17182 /* Helper macro to examine the various builtin types. */
17183 #define TRY_TYPE(F) \
17184 int_type = (unsigned_p \
17185 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17186 : objfile_type (objfile)->builtin_ ## F); \
17187 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17188 return int_type
17189
17190 TRY_TYPE (char);
17191 TRY_TYPE (short);
17192 TRY_TYPE (int);
17193 TRY_TYPE (long);
17194 TRY_TYPE (long_long);
17195
17196 #undef TRY_TYPE
17197
17198 gdb_assert_not_reached ("unable to find suitable integer type");
17199 }
17200
17201 /* See read.h. */
17202
17203 struct type *
17204 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17205 {
17206 int addr_size = this->addr_size ();
17207 return int_type (addr_size, unsigned_p);
17208 }
17209
17210 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17211 present (which is valid) then compute the default type based on the
17212 compilation units address size. */
17213
17214 static struct type *
17215 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17216 {
17217 struct type *index_type = die_type (die, cu);
17218
17219 /* Dwarf-2 specifications explicitly allows to create subrange types
17220 without specifying a base type.
17221 In that case, the base type must be set to the type of
17222 the lower bound, upper bound or count, in that order, if any of these
17223 three attributes references an object that has a type.
17224 If no base type is found, the Dwarf-2 specifications say that
17225 a signed integer type of size equal to the size of an address should
17226 be used.
17227 For the following C code: `extern char gdb_int [];'
17228 GCC produces an empty range DIE.
17229 FIXME: muller/2010-05-28: Possible references to object for low bound,
17230 high bound or count are not yet handled by this code. */
17231 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17232 index_type = cu->per_cu->addr_sized_int_type (false);
17233
17234 return index_type;
17235 }
17236
17237 /* Read the given DW_AT_subrange DIE. */
17238
17239 static struct type *
17240 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17241 {
17242 struct type *base_type, *orig_base_type;
17243 struct type *range_type;
17244 struct attribute *attr;
17245 struct dynamic_prop low, high;
17246 int low_default_is_valid;
17247 int high_bound_is_count = 0;
17248 const char *name;
17249 ULONGEST negative_mask;
17250
17251 orig_base_type = read_subrange_index_type (die, cu);
17252
17253 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17254 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17255 creating the range type, but we use the result of check_typedef
17256 when examining properties of the type. */
17257 base_type = check_typedef (orig_base_type);
17258
17259 /* The die_type call above may have already set the type for this DIE. */
17260 range_type = get_die_type (die, cu);
17261 if (range_type)
17262 return range_type;
17263
17264 low.kind = PROP_CONST;
17265 high.kind = PROP_CONST;
17266 high.data.const_val = 0;
17267
17268 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17269 omitting DW_AT_lower_bound. */
17270 switch (cu->language)
17271 {
17272 case language_c:
17273 case language_cplus:
17274 low.data.const_val = 0;
17275 low_default_is_valid = 1;
17276 break;
17277 case language_fortran:
17278 low.data.const_val = 1;
17279 low_default_is_valid = 1;
17280 break;
17281 case language_d:
17282 case language_objc:
17283 case language_rust:
17284 low.data.const_val = 0;
17285 low_default_is_valid = (cu->header.version >= 4);
17286 break;
17287 case language_ada:
17288 case language_m2:
17289 case language_pascal:
17290 low.data.const_val = 1;
17291 low_default_is_valid = (cu->header.version >= 4);
17292 break;
17293 default:
17294 low.data.const_val = 0;
17295 low_default_is_valid = 0;
17296 break;
17297 }
17298
17299 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17300 if (attr != nullptr)
17301 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17302 else if (!low_default_is_valid)
17303 complaint (_("Missing DW_AT_lower_bound "
17304 "- DIE at %s [in module %s]"),
17305 sect_offset_str (die->sect_off),
17306 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17307
17308 struct attribute *attr_ub, *attr_count;
17309 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17310 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17311 {
17312 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17313 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17314 {
17315 /* If bounds are constant do the final calculation here. */
17316 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17317 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17318 else
17319 high_bound_is_count = 1;
17320 }
17321 else
17322 {
17323 if (attr_ub != NULL)
17324 complaint (_("Unresolved DW_AT_upper_bound "
17325 "- DIE at %s [in module %s]"),
17326 sect_offset_str (die->sect_off),
17327 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17328 if (attr_count != NULL)
17329 complaint (_("Unresolved DW_AT_count "
17330 "- DIE at %s [in module %s]"),
17331 sect_offset_str (die->sect_off),
17332 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17333 }
17334 }
17335
17336 LONGEST bias = 0;
17337 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17338 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17339 bias = bias_attr->constant_value (0);
17340
17341 /* Normally, the DWARF producers are expected to use a signed
17342 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17343 But this is unfortunately not always the case, as witnessed
17344 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17345 is used instead. To work around that ambiguity, we treat
17346 the bounds as signed, and thus sign-extend their values, when
17347 the base type is signed. */
17348 negative_mask =
17349 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17350 if (low.kind == PROP_CONST
17351 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17352 low.data.const_val |= negative_mask;
17353 if (high.kind == PROP_CONST
17354 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17355 high.data.const_val |= negative_mask;
17356
17357 /* Check for bit and byte strides. */
17358 struct dynamic_prop byte_stride_prop;
17359 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17360 if (attr_byte_stride != nullptr)
17361 {
17362 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17363 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17364 prop_type);
17365 }
17366
17367 struct dynamic_prop bit_stride_prop;
17368 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17369 if (attr_bit_stride != nullptr)
17370 {
17371 /* It only makes sense to have either a bit or byte stride. */
17372 if (attr_byte_stride != nullptr)
17373 {
17374 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17375 "- DIE at %s [in module %s]"),
17376 sect_offset_str (die->sect_off),
17377 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17378 attr_bit_stride = nullptr;
17379 }
17380 else
17381 {
17382 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17383 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17384 prop_type);
17385 }
17386 }
17387
17388 if (attr_byte_stride != nullptr
17389 || attr_bit_stride != nullptr)
17390 {
17391 bool byte_stride_p = (attr_byte_stride != nullptr);
17392 struct dynamic_prop *stride
17393 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17394
17395 range_type
17396 = create_range_type_with_stride (NULL, orig_base_type, &low,
17397 &high, bias, stride, byte_stride_p);
17398 }
17399 else
17400 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17401
17402 if (high_bound_is_count)
17403 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17404
17405 /* Ada expects an empty array on no boundary attributes. */
17406 if (attr == NULL && cu->language != language_ada)
17407 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17408
17409 name = dwarf2_name (die, cu);
17410 if (name)
17411 TYPE_NAME (range_type) = name;
17412
17413 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17414 if (attr != nullptr)
17415 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17416
17417 maybe_set_alignment (cu, die, range_type);
17418
17419 set_die_type (die, range_type, cu);
17420
17421 /* set_die_type should be already done. */
17422 set_descriptive_type (range_type, die, cu);
17423
17424 return range_type;
17425 }
17426
17427 static struct type *
17428 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17429 {
17430 struct type *type;
17431
17432 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17433 NULL);
17434 TYPE_NAME (type) = dwarf2_name (die, cu);
17435
17436 /* In Ada, an unspecified type is typically used when the description
17437 of the type is deferred to a different unit. When encountering
17438 such a type, we treat it as a stub, and try to resolve it later on,
17439 when needed. */
17440 if (cu->language == language_ada)
17441 TYPE_STUB (type) = 1;
17442
17443 return set_die_type (die, type, cu);
17444 }
17445
17446 /* Read a single die and all its descendents. Set the die's sibling
17447 field to NULL; set other fields in the die correctly, and set all
17448 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17449 location of the info_ptr after reading all of those dies. PARENT
17450 is the parent of the die in question. */
17451
17452 static struct die_info *
17453 read_die_and_children (const struct die_reader_specs *reader,
17454 const gdb_byte *info_ptr,
17455 const gdb_byte **new_info_ptr,
17456 struct die_info *parent)
17457 {
17458 struct die_info *die;
17459 const gdb_byte *cur_ptr;
17460
17461 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17462 if (die == NULL)
17463 {
17464 *new_info_ptr = cur_ptr;
17465 return NULL;
17466 }
17467 store_in_ref_table (die, reader->cu);
17468
17469 if (die->has_children)
17470 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17471 else
17472 {
17473 die->child = NULL;
17474 *new_info_ptr = cur_ptr;
17475 }
17476
17477 die->sibling = NULL;
17478 die->parent = parent;
17479 return die;
17480 }
17481
17482 /* Read a die, all of its descendents, and all of its siblings; set
17483 all of the fields of all of the dies correctly. Arguments are as
17484 in read_die_and_children. */
17485
17486 static struct die_info *
17487 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17488 const gdb_byte *info_ptr,
17489 const gdb_byte **new_info_ptr,
17490 struct die_info *parent)
17491 {
17492 struct die_info *first_die, *last_sibling;
17493 const gdb_byte *cur_ptr;
17494
17495 cur_ptr = info_ptr;
17496 first_die = last_sibling = NULL;
17497
17498 while (1)
17499 {
17500 struct die_info *die
17501 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17502
17503 if (die == NULL)
17504 {
17505 *new_info_ptr = cur_ptr;
17506 return first_die;
17507 }
17508
17509 if (!first_die)
17510 first_die = die;
17511 else
17512 last_sibling->sibling = die;
17513
17514 last_sibling = die;
17515 }
17516 }
17517
17518 /* Read a die, all of its descendents, and all of its siblings; set
17519 all of the fields of all of the dies correctly. Arguments are as
17520 in read_die_and_children.
17521 This the main entry point for reading a DIE and all its children. */
17522
17523 static struct die_info *
17524 read_die_and_siblings (const struct die_reader_specs *reader,
17525 const gdb_byte *info_ptr,
17526 const gdb_byte **new_info_ptr,
17527 struct die_info *parent)
17528 {
17529 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17530 new_info_ptr, parent);
17531
17532 if (dwarf_die_debug)
17533 {
17534 fprintf_unfiltered (gdb_stdlog,
17535 "Read die from %s@0x%x of %s:\n",
17536 reader->die_section->get_name (),
17537 (unsigned) (info_ptr - reader->die_section->buffer),
17538 bfd_get_filename (reader->abfd));
17539 dump_die (die, dwarf_die_debug);
17540 }
17541
17542 return die;
17543 }
17544
17545 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17546 attributes.
17547 The caller is responsible for filling in the extra attributes
17548 and updating (*DIEP)->num_attrs.
17549 Set DIEP to point to a newly allocated die with its information,
17550 except for its child, sibling, and parent fields. */
17551
17552 static const gdb_byte *
17553 read_full_die_1 (const struct die_reader_specs *reader,
17554 struct die_info **diep, const gdb_byte *info_ptr,
17555 int num_extra_attrs)
17556 {
17557 unsigned int abbrev_number, bytes_read, i;
17558 struct abbrev_info *abbrev;
17559 struct die_info *die;
17560 struct dwarf2_cu *cu = reader->cu;
17561 bfd *abfd = reader->abfd;
17562
17563 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17564 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17565 info_ptr += bytes_read;
17566 if (!abbrev_number)
17567 {
17568 *diep = NULL;
17569 return info_ptr;
17570 }
17571
17572 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17573 if (!abbrev)
17574 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17575 abbrev_number,
17576 bfd_get_filename (abfd));
17577
17578 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17579 die->sect_off = sect_off;
17580 die->tag = abbrev->tag;
17581 die->abbrev = abbrev_number;
17582 die->has_children = abbrev->has_children;
17583
17584 /* Make the result usable.
17585 The caller needs to update num_attrs after adding the extra
17586 attributes. */
17587 die->num_attrs = abbrev->num_attrs;
17588
17589 std::vector<int> indexes_that_need_reprocess;
17590 for (i = 0; i < abbrev->num_attrs; ++i)
17591 {
17592 bool need_reprocess;
17593 info_ptr =
17594 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17595 info_ptr, &need_reprocess);
17596 if (need_reprocess)
17597 indexes_that_need_reprocess.push_back (i);
17598 }
17599
17600 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
17601 if (attr != nullptr)
17602 cu->str_offsets_base = DW_UNSND (attr);
17603
17604 attr = die->attr (DW_AT_loclists_base);
17605 if (attr != nullptr)
17606 cu->loclist_base = DW_UNSND (attr);
17607
17608 auto maybe_addr_base = die->addr_base ();
17609 if (maybe_addr_base.has_value ())
17610 cu->addr_base = *maybe_addr_base;
17611 for (int index : indexes_that_need_reprocess)
17612 read_attribute_reprocess (reader, &die->attrs[index]);
17613 *diep = die;
17614 return info_ptr;
17615 }
17616
17617 /* Read a die and all its attributes.
17618 Set DIEP to point to a newly allocated die with its information,
17619 except for its child, sibling, and parent fields. */
17620
17621 static const gdb_byte *
17622 read_full_die (const struct die_reader_specs *reader,
17623 struct die_info **diep, const gdb_byte *info_ptr)
17624 {
17625 const gdb_byte *result;
17626
17627 result = read_full_die_1 (reader, diep, info_ptr, 0);
17628
17629 if (dwarf_die_debug)
17630 {
17631 fprintf_unfiltered (gdb_stdlog,
17632 "Read die from %s@0x%x of %s:\n",
17633 reader->die_section->get_name (),
17634 (unsigned) (info_ptr - reader->die_section->buffer),
17635 bfd_get_filename (reader->abfd));
17636 dump_die (*diep, dwarf_die_debug);
17637 }
17638
17639 return result;
17640 }
17641 \f
17642
17643 /* Returns nonzero if TAG represents a type that we might generate a partial
17644 symbol for. */
17645
17646 static int
17647 is_type_tag_for_partial (int tag)
17648 {
17649 switch (tag)
17650 {
17651 #if 0
17652 /* Some types that would be reasonable to generate partial symbols for,
17653 that we don't at present. */
17654 case DW_TAG_array_type:
17655 case DW_TAG_file_type:
17656 case DW_TAG_ptr_to_member_type:
17657 case DW_TAG_set_type:
17658 case DW_TAG_string_type:
17659 case DW_TAG_subroutine_type:
17660 #endif
17661 case DW_TAG_base_type:
17662 case DW_TAG_class_type:
17663 case DW_TAG_interface_type:
17664 case DW_TAG_enumeration_type:
17665 case DW_TAG_structure_type:
17666 case DW_TAG_subrange_type:
17667 case DW_TAG_typedef:
17668 case DW_TAG_union_type:
17669 return 1;
17670 default:
17671 return 0;
17672 }
17673 }
17674
17675 /* Load all DIEs that are interesting for partial symbols into memory. */
17676
17677 static struct partial_die_info *
17678 load_partial_dies (const struct die_reader_specs *reader,
17679 const gdb_byte *info_ptr, int building_psymtab)
17680 {
17681 struct dwarf2_cu *cu = reader->cu;
17682 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17683 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17684 unsigned int bytes_read;
17685 unsigned int load_all = 0;
17686 int nesting_level = 1;
17687
17688 parent_die = NULL;
17689 last_die = NULL;
17690
17691 gdb_assert (cu->per_cu != NULL);
17692 if (cu->per_cu->load_all_dies)
17693 load_all = 1;
17694
17695 cu->partial_dies
17696 = htab_create_alloc_ex (cu->header.length / 12,
17697 partial_die_hash,
17698 partial_die_eq,
17699 NULL,
17700 &cu->comp_unit_obstack,
17701 hashtab_obstack_allocate,
17702 dummy_obstack_deallocate);
17703
17704 while (1)
17705 {
17706 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17707
17708 /* A NULL abbrev means the end of a series of children. */
17709 if (abbrev == NULL)
17710 {
17711 if (--nesting_level == 0)
17712 return first_die;
17713
17714 info_ptr += bytes_read;
17715 last_die = parent_die;
17716 parent_die = parent_die->die_parent;
17717 continue;
17718 }
17719
17720 /* Check for template arguments. We never save these; if
17721 they're seen, we just mark the parent, and go on our way. */
17722 if (parent_die != NULL
17723 && cu->language == language_cplus
17724 && (abbrev->tag == DW_TAG_template_type_param
17725 || abbrev->tag == DW_TAG_template_value_param))
17726 {
17727 parent_die->has_template_arguments = 1;
17728
17729 if (!load_all)
17730 {
17731 /* We don't need a partial DIE for the template argument. */
17732 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17733 continue;
17734 }
17735 }
17736
17737 /* We only recurse into c++ subprograms looking for template arguments.
17738 Skip their other children. */
17739 if (!load_all
17740 && cu->language == language_cplus
17741 && parent_die != NULL
17742 && parent_die->tag == DW_TAG_subprogram)
17743 {
17744 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17745 continue;
17746 }
17747
17748 /* Check whether this DIE is interesting enough to save. Normally
17749 we would not be interested in members here, but there may be
17750 later variables referencing them via DW_AT_specification (for
17751 static members). */
17752 if (!load_all
17753 && !is_type_tag_for_partial (abbrev->tag)
17754 && abbrev->tag != DW_TAG_constant
17755 && abbrev->tag != DW_TAG_enumerator
17756 && abbrev->tag != DW_TAG_subprogram
17757 && abbrev->tag != DW_TAG_inlined_subroutine
17758 && abbrev->tag != DW_TAG_lexical_block
17759 && abbrev->tag != DW_TAG_variable
17760 && abbrev->tag != DW_TAG_namespace
17761 && abbrev->tag != DW_TAG_module
17762 && abbrev->tag != DW_TAG_member
17763 && abbrev->tag != DW_TAG_imported_unit
17764 && abbrev->tag != DW_TAG_imported_declaration)
17765 {
17766 /* Otherwise we skip to the next sibling, if any. */
17767 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17768 continue;
17769 }
17770
17771 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17772 abbrev);
17773
17774 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
17775
17776 /* This two-pass algorithm for processing partial symbols has a
17777 high cost in cache pressure. Thus, handle some simple cases
17778 here which cover the majority of C partial symbols. DIEs
17779 which neither have specification tags in them, nor could have
17780 specification tags elsewhere pointing at them, can simply be
17781 processed and discarded.
17782
17783 This segment is also optional; scan_partial_symbols and
17784 add_partial_symbol will handle these DIEs if we chain
17785 them in normally. When compilers which do not emit large
17786 quantities of duplicate debug information are more common,
17787 this code can probably be removed. */
17788
17789 /* Any complete simple types at the top level (pretty much all
17790 of them, for a language without namespaces), can be processed
17791 directly. */
17792 if (parent_die == NULL
17793 && pdi.has_specification == 0
17794 && pdi.is_declaration == 0
17795 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17796 || pdi.tag == DW_TAG_base_type
17797 || pdi.tag == DW_TAG_subrange_type))
17798 {
17799 if (building_psymtab && pdi.name != NULL)
17800 add_psymbol_to_list (pdi.name, false,
17801 VAR_DOMAIN, LOC_TYPEDEF, -1,
17802 psymbol_placement::STATIC,
17803 0, cu->language, objfile);
17804 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17805 continue;
17806 }
17807
17808 /* The exception for DW_TAG_typedef with has_children above is
17809 a workaround of GCC PR debug/47510. In the case of this complaint
17810 type_name_or_error will error on such types later.
17811
17812 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17813 it could not find the child DIEs referenced later, this is checked
17814 above. In correct DWARF DW_TAG_typedef should have no children. */
17815
17816 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
17817 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17818 "- DIE at %s [in module %s]"),
17819 sect_offset_str (pdi.sect_off), objfile_name (objfile));
17820
17821 /* If we're at the second level, and we're an enumerator, and
17822 our parent has no specification (meaning possibly lives in a
17823 namespace elsewhere), then we can add the partial symbol now
17824 instead of queueing it. */
17825 if (pdi.tag == DW_TAG_enumerator
17826 && parent_die != NULL
17827 && parent_die->die_parent == NULL
17828 && parent_die->tag == DW_TAG_enumeration_type
17829 && parent_die->has_specification == 0)
17830 {
17831 if (pdi.name == NULL)
17832 complaint (_("malformed enumerator DIE ignored"));
17833 else if (building_psymtab)
17834 add_psymbol_to_list (pdi.name, false,
17835 VAR_DOMAIN, LOC_CONST, -1,
17836 cu->language == language_cplus
17837 ? psymbol_placement::GLOBAL
17838 : psymbol_placement::STATIC,
17839 0, cu->language, objfile);
17840
17841 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17842 continue;
17843 }
17844
17845 struct partial_die_info *part_die
17846 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
17847
17848 /* We'll save this DIE so link it in. */
17849 part_die->die_parent = parent_die;
17850 part_die->die_sibling = NULL;
17851 part_die->die_child = NULL;
17852
17853 if (last_die && last_die == parent_die)
17854 last_die->die_child = part_die;
17855 else if (last_die)
17856 last_die->die_sibling = part_die;
17857
17858 last_die = part_die;
17859
17860 if (first_die == NULL)
17861 first_die = part_die;
17862
17863 /* Maybe add the DIE to the hash table. Not all DIEs that we
17864 find interesting need to be in the hash table, because we
17865 also have the parent/sibling/child chains; only those that we
17866 might refer to by offset later during partial symbol reading.
17867
17868 For now this means things that might have be the target of a
17869 DW_AT_specification, DW_AT_abstract_origin, or
17870 DW_AT_extension. DW_AT_extension will refer only to
17871 namespaces; DW_AT_abstract_origin refers to functions (and
17872 many things under the function DIE, but we do not recurse
17873 into function DIEs during partial symbol reading) and
17874 possibly variables as well; DW_AT_specification refers to
17875 declarations. Declarations ought to have the DW_AT_declaration
17876 flag. It happens that GCC forgets to put it in sometimes, but
17877 only for functions, not for types.
17878
17879 Adding more things than necessary to the hash table is harmless
17880 except for the performance cost. Adding too few will result in
17881 wasted time in find_partial_die, when we reread the compilation
17882 unit with load_all_dies set. */
17883
17884 if (load_all
17885 || abbrev->tag == DW_TAG_constant
17886 || abbrev->tag == DW_TAG_subprogram
17887 || abbrev->tag == DW_TAG_variable
17888 || abbrev->tag == DW_TAG_namespace
17889 || part_die->is_declaration)
17890 {
17891 void **slot;
17892
17893 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17894 to_underlying (part_die->sect_off),
17895 INSERT);
17896 *slot = part_die;
17897 }
17898
17899 /* For some DIEs we want to follow their children (if any). For C
17900 we have no reason to follow the children of structures; for other
17901 languages we have to, so that we can get at method physnames
17902 to infer fully qualified class names, for DW_AT_specification,
17903 and for C++ template arguments. For C++, we also look one level
17904 inside functions to find template arguments (if the name of the
17905 function does not already contain the template arguments).
17906
17907 For Ada and Fortran, we need to scan the children of subprograms
17908 and lexical blocks as well because these languages allow the
17909 definition of nested entities that could be interesting for the
17910 debugger, such as nested subprograms for instance. */
17911 if (last_die->has_children
17912 && (load_all
17913 || last_die->tag == DW_TAG_namespace
17914 || last_die->tag == DW_TAG_module
17915 || last_die->tag == DW_TAG_enumeration_type
17916 || (cu->language == language_cplus
17917 && last_die->tag == DW_TAG_subprogram
17918 && (last_die->name == NULL
17919 || strchr (last_die->name, '<') == NULL))
17920 || (cu->language != language_c
17921 && (last_die->tag == DW_TAG_class_type
17922 || last_die->tag == DW_TAG_interface_type
17923 || last_die->tag == DW_TAG_structure_type
17924 || last_die->tag == DW_TAG_union_type))
17925 || ((cu->language == language_ada
17926 || cu->language == language_fortran)
17927 && (last_die->tag == DW_TAG_subprogram
17928 || last_die->tag == DW_TAG_lexical_block))))
17929 {
17930 nesting_level++;
17931 parent_die = last_die;
17932 continue;
17933 }
17934
17935 /* Otherwise we skip to the next sibling, if any. */
17936 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17937
17938 /* Back to the top, do it again. */
17939 }
17940 }
17941
17942 partial_die_info::partial_die_info (sect_offset sect_off_,
17943 struct abbrev_info *abbrev)
17944 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17945 {
17946 }
17947
17948 /* Read a minimal amount of information into the minimal die structure.
17949 INFO_PTR should point just after the initial uleb128 of a DIE. */
17950
17951 const gdb_byte *
17952 partial_die_info::read (const struct die_reader_specs *reader,
17953 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
17954 {
17955 struct dwarf2_cu *cu = reader->cu;
17956 struct dwarf2_per_objfile *dwarf2_per_objfile
17957 = cu->per_cu->dwarf2_per_objfile;
17958 unsigned int i;
17959 int has_low_pc_attr = 0;
17960 int has_high_pc_attr = 0;
17961 int high_pc_relative = 0;
17962
17963 for (i = 0; i < abbrev.num_attrs; ++i)
17964 {
17965 attribute attr;
17966 bool need_reprocess;
17967 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
17968 info_ptr, &need_reprocess);
17969 /* String and address offsets that need to do the reprocessing have
17970 already been read at this point, so there is no need to wait until
17971 the loop terminates to do the reprocessing. */
17972 if (need_reprocess)
17973 read_attribute_reprocess (reader, &attr);
17974 /* Store the data if it is of an attribute we want to keep in a
17975 partial symbol table. */
17976 switch (attr.name)
17977 {
17978 case DW_AT_name:
17979 switch (tag)
17980 {
17981 case DW_TAG_compile_unit:
17982 case DW_TAG_partial_unit:
17983 case DW_TAG_type_unit:
17984 /* Compilation units have a DW_AT_name that is a filename, not
17985 a source language identifier. */
17986 case DW_TAG_enumeration_type:
17987 case DW_TAG_enumerator:
17988 /* These tags always have simple identifiers already; no need
17989 to canonicalize them. */
17990 name = DW_STRING (&attr);
17991 break;
17992 default:
17993 {
17994 struct objfile *objfile = dwarf2_per_objfile->objfile;
17995
17996 name
17997 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
17998 }
17999 break;
18000 }
18001 break;
18002 case DW_AT_linkage_name:
18003 case DW_AT_MIPS_linkage_name:
18004 /* Note that both forms of linkage name might appear. We
18005 assume they will be the same, and we only store the last
18006 one we see. */
18007 linkage_name = DW_STRING (&attr);
18008 break;
18009 case DW_AT_low_pc:
18010 has_low_pc_attr = 1;
18011 lowpc = attr.value_as_address ();
18012 break;
18013 case DW_AT_high_pc:
18014 has_high_pc_attr = 1;
18015 highpc = attr.value_as_address ();
18016 if (cu->header.version >= 4 && attr.form_is_constant ())
18017 high_pc_relative = 1;
18018 break;
18019 case DW_AT_location:
18020 /* Support the .debug_loc offsets. */
18021 if (attr.form_is_block ())
18022 {
18023 d.locdesc = DW_BLOCK (&attr);
18024 }
18025 else if (attr.form_is_section_offset ())
18026 {
18027 dwarf2_complex_location_expr_complaint ();
18028 }
18029 else
18030 {
18031 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18032 "partial symbol information");
18033 }
18034 break;
18035 case DW_AT_external:
18036 is_external = DW_UNSND (&attr);
18037 break;
18038 case DW_AT_declaration:
18039 is_declaration = DW_UNSND (&attr);
18040 break;
18041 case DW_AT_type:
18042 has_type = 1;
18043 break;
18044 case DW_AT_abstract_origin:
18045 case DW_AT_specification:
18046 case DW_AT_extension:
18047 has_specification = 1;
18048 spec_offset = attr.get_ref_die_offset ();
18049 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18050 || cu->per_cu->is_dwz);
18051 break;
18052 case DW_AT_sibling:
18053 /* Ignore absolute siblings, they might point outside of
18054 the current compile unit. */
18055 if (attr.form == DW_FORM_ref_addr)
18056 complaint (_("ignoring absolute DW_AT_sibling"));
18057 else
18058 {
18059 const gdb_byte *buffer = reader->buffer;
18060 sect_offset off = attr.get_ref_die_offset ();
18061 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18062
18063 if (sibling_ptr < info_ptr)
18064 complaint (_("DW_AT_sibling points backwards"));
18065 else if (sibling_ptr > reader->buffer_end)
18066 reader->die_section->overflow_complaint ();
18067 else
18068 sibling = sibling_ptr;
18069 }
18070 break;
18071 case DW_AT_byte_size:
18072 has_byte_size = 1;
18073 break;
18074 case DW_AT_const_value:
18075 has_const_value = 1;
18076 break;
18077 case DW_AT_calling_convention:
18078 /* DWARF doesn't provide a way to identify a program's source-level
18079 entry point. DW_AT_calling_convention attributes are only meant
18080 to describe functions' calling conventions.
18081
18082 However, because it's a necessary piece of information in
18083 Fortran, and before DWARF 4 DW_CC_program was the only
18084 piece of debugging information whose definition refers to
18085 a 'main program' at all, several compilers marked Fortran
18086 main programs with DW_CC_program --- even when those
18087 functions use the standard calling conventions.
18088
18089 Although DWARF now specifies a way to provide this
18090 information, we support this practice for backward
18091 compatibility. */
18092 if (DW_UNSND (&attr) == DW_CC_program
18093 && cu->language == language_fortran)
18094 main_subprogram = 1;
18095 break;
18096 case DW_AT_inline:
18097 if (DW_UNSND (&attr) == DW_INL_inlined
18098 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18099 may_be_inlined = 1;
18100 break;
18101
18102 case DW_AT_import:
18103 if (tag == DW_TAG_imported_unit)
18104 {
18105 d.sect_off = attr.get_ref_die_offset ();
18106 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18107 || cu->per_cu->is_dwz);
18108 }
18109 break;
18110
18111 case DW_AT_main_subprogram:
18112 main_subprogram = DW_UNSND (&attr);
18113 break;
18114
18115 case DW_AT_ranges:
18116 {
18117 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18118 but that requires a full DIE, so instead we just
18119 reimplement it. */
18120 int need_ranges_base = tag != DW_TAG_compile_unit;
18121 unsigned int ranges_offset = (DW_UNSND (&attr)
18122 + (need_ranges_base
18123 ? cu->ranges_base
18124 : 0));
18125
18126 /* Value of the DW_AT_ranges attribute is the offset in the
18127 .debug_ranges section. */
18128 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18129 nullptr))
18130 has_pc_info = 1;
18131 }
18132 break;
18133
18134 default:
18135 break;
18136 }
18137 }
18138
18139 /* For Ada, if both the name and the linkage name appear, we prefer
18140 the latter. This lets "catch exception" work better, regardless
18141 of the order in which the name and linkage name were emitted.
18142 Really, though, this is just a workaround for the fact that gdb
18143 doesn't store both the name and the linkage name. */
18144 if (cu->language == language_ada && linkage_name != nullptr)
18145 name = linkage_name;
18146
18147 if (high_pc_relative)
18148 highpc += lowpc;
18149
18150 if (has_low_pc_attr && has_high_pc_attr)
18151 {
18152 /* When using the GNU linker, .gnu.linkonce. sections are used to
18153 eliminate duplicate copies of functions and vtables and such.
18154 The linker will arbitrarily choose one and discard the others.
18155 The AT_*_pc values for such functions refer to local labels in
18156 these sections. If the section from that file was discarded, the
18157 labels are not in the output, so the relocs get a value of 0.
18158 If this is a discarded function, mark the pc bounds as invalid,
18159 so that GDB will ignore it. */
18160 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18161 {
18162 struct objfile *objfile = dwarf2_per_objfile->objfile;
18163 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18164
18165 complaint (_("DW_AT_low_pc %s is zero "
18166 "for DIE at %s [in module %s]"),
18167 paddress (gdbarch, lowpc),
18168 sect_offset_str (sect_off),
18169 objfile_name (objfile));
18170 }
18171 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18172 else if (lowpc >= highpc)
18173 {
18174 struct objfile *objfile = dwarf2_per_objfile->objfile;
18175 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18176
18177 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18178 "for DIE at %s [in module %s]"),
18179 paddress (gdbarch, lowpc),
18180 paddress (gdbarch, highpc),
18181 sect_offset_str (sect_off),
18182 objfile_name (objfile));
18183 }
18184 else
18185 has_pc_info = 1;
18186 }
18187
18188 return info_ptr;
18189 }
18190
18191 /* Find a cached partial DIE at OFFSET in CU. */
18192
18193 struct partial_die_info *
18194 dwarf2_cu::find_partial_die (sect_offset sect_off)
18195 {
18196 struct partial_die_info *lookup_die = NULL;
18197 struct partial_die_info part_die (sect_off);
18198
18199 lookup_die = ((struct partial_die_info *)
18200 htab_find_with_hash (partial_dies, &part_die,
18201 to_underlying (sect_off)));
18202
18203 return lookup_die;
18204 }
18205
18206 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18207 except in the case of .debug_types DIEs which do not reference
18208 outside their CU (they do however referencing other types via
18209 DW_FORM_ref_sig8). */
18210
18211 static const struct cu_partial_die_info
18212 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18213 {
18214 struct dwarf2_per_objfile *dwarf2_per_objfile
18215 = cu->per_cu->dwarf2_per_objfile;
18216 struct objfile *objfile = dwarf2_per_objfile->objfile;
18217 struct dwarf2_per_cu_data *per_cu = NULL;
18218 struct partial_die_info *pd = NULL;
18219
18220 if (offset_in_dwz == cu->per_cu->is_dwz
18221 && cu->header.offset_in_cu_p (sect_off))
18222 {
18223 pd = cu->find_partial_die (sect_off);
18224 if (pd != NULL)
18225 return { cu, pd };
18226 /* We missed recording what we needed.
18227 Load all dies and try again. */
18228 per_cu = cu->per_cu;
18229 }
18230 else
18231 {
18232 /* TUs don't reference other CUs/TUs (except via type signatures). */
18233 if (cu->per_cu->is_debug_types)
18234 {
18235 error (_("Dwarf Error: Type Unit at offset %s contains"
18236 " external reference to offset %s [in module %s].\n"),
18237 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18238 bfd_get_filename (objfile->obfd));
18239 }
18240 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18241 dwarf2_per_objfile);
18242
18243 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18244 load_partial_comp_unit (per_cu);
18245
18246 per_cu->cu->last_used = 0;
18247 pd = per_cu->cu->find_partial_die (sect_off);
18248 }
18249
18250 /* If we didn't find it, and not all dies have been loaded,
18251 load them all and try again. */
18252
18253 if (pd == NULL && per_cu->load_all_dies == 0)
18254 {
18255 per_cu->load_all_dies = 1;
18256
18257 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18258 THIS_CU->cu may already be in use. So we can't just free it and
18259 replace its DIEs with the ones we read in. Instead, we leave those
18260 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18261 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18262 set. */
18263 load_partial_comp_unit (per_cu);
18264
18265 pd = per_cu->cu->find_partial_die (sect_off);
18266 }
18267
18268 if (pd == NULL)
18269 internal_error (__FILE__, __LINE__,
18270 _("could not find partial DIE %s "
18271 "in cache [from module %s]\n"),
18272 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18273 return { per_cu->cu, pd };
18274 }
18275
18276 /* See if we can figure out if the class lives in a namespace. We do
18277 this by looking for a member function; its demangled name will
18278 contain namespace info, if there is any. */
18279
18280 static void
18281 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18282 struct dwarf2_cu *cu)
18283 {
18284 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18285 what template types look like, because the demangler
18286 frequently doesn't give the same name as the debug info. We
18287 could fix this by only using the demangled name to get the
18288 prefix (but see comment in read_structure_type). */
18289
18290 struct partial_die_info *real_pdi;
18291 struct partial_die_info *child_pdi;
18292
18293 /* If this DIE (this DIE's specification, if any) has a parent, then
18294 we should not do this. We'll prepend the parent's fully qualified
18295 name when we create the partial symbol. */
18296
18297 real_pdi = struct_pdi;
18298 while (real_pdi->has_specification)
18299 {
18300 auto res = find_partial_die (real_pdi->spec_offset,
18301 real_pdi->spec_is_dwz, cu);
18302 real_pdi = res.pdi;
18303 cu = res.cu;
18304 }
18305
18306 if (real_pdi->die_parent != NULL)
18307 return;
18308
18309 for (child_pdi = struct_pdi->die_child;
18310 child_pdi != NULL;
18311 child_pdi = child_pdi->die_sibling)
18312 {
18313 if (child_pdi->tag == DW_TAG_subprogram
18314 && child_pdi->linkage_name != NULL)
18315 {
18316 gdb::unique_xmalloc_ptr<char> actual_class_name
18317 (language_class_name_from_physname (cu->language_defn,
18318 child_pdi->linkage_name));
18319 if (actual_class_name != NULL)
18320 {
18321 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18322 struct_pdi->name = objfile->intern (actual_class_name.get ());
18323 }
18324 break;
18325 }
18326 }
18327 }
18328
18329 /* Return true if a DIE with TAG may have the DW_AT_const_value
18330 attribute. */
18331
18332 static bool
18333 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18334 {
18335 switch (tag)
18336 {
18337 case DW_TAG_constant:
18338 case DW_TAG_enumerator:
18339 case DW_TAG_formal_parameter:
18340 case DW_TAG_template_value_param:
18341 case DW_TAG_variable:
18342 return true;
18343 }
18344
18345 return false;
18346 }
18347
18348 void
18349 partial_die_info::fixup (struct dwarf2_cu *cu)
18350 {
18351 /* Once we've fixed up a die, there's no point in doing so again.
18352 This also avoids a memory leak if we were to call
18353 guess_partial_die_structure_name multiple times. */
18354 if (fixup_called)
18355 return;
18356
18357 /* If we found a reference attribute and the DIE has no name, try
18358 to find a name in the referred to DIE. */
18359
18360 if (name == NULL && has_specification)
18361 {
18362 struct partial_die_info *spec_die;
18363
18364 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18365 spec_die = res.pdi;
18366 cu = res.cu;
18367
18368 spec_die->fixup (cu);
18369
18370 if (spec_die->name)
18371 {
18372 name = spec_die->name;
18373
18374 /* Copy DW_AT_external attribute if it is set. */
18375 if (spec_die->is_external)
18376 is_external = spec_die->is_external;
18377 }
18378 }
18379
18380 if (!has_const_value && has_specification
18381 && can_have_DW_AT_const_value_p (tag))
18382 {
18383 struct partial_die_info *spec_die;
18384
18385 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18386 spec_die = res.pdi;
18387 cu = res.cu;
18388
18389 spec_die->fixup (cu);
18390
18391 if (spec_die->has_const_value)
18392 {
18393 /* Copy DW_AT_const_value attribute if it is set. */
18394 has_const_value = spec_die->has_const_value;
18395 }
18396 }
18397
18398 /* Set default names for some unnamed DIEs. */
18399
18400 if (name == NULL && tag == DW_TAG_namespace)
18401 name = CP_ANONYMOUS_NAMESPACE_STR;
18402
18403 /* If there is no parent die to provide a namespace, and there are
18404 children, see if we can determine the namespace from their linkage
18405 name. */
18406 if (cu->language == language_cplus
18407 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18408 && die_parent == NULL
18409 && has_children
18410 && (tag == DW_TAG_class_type
18411 || tag == DW_TAG_structure_type
18412 || tag == DW_TAG_union_type))
18413 guess_partial_die_structure_name (this, cu);
18414
18415 /* GCC might emit a nameless struct or union that has a linkage
18416 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18417 if (name == NULL
18418 && (tag == DW_TAG_class_type
18419 || tag == DW_TAG_interface_type
18420 || tag == DW_TAG_structure_type
18421 || tag == DW_TAG_union_type)
18422 && linkage_name != NULL)
18423 {
18424 gdb::unique_xmalloc_ptr<char> demangled
18425 (gdb_demangle (linkage_name, DMGL_TYPES));
18426 if (demangled != nullptr)
18427 {
18428 const char *base;
18429
18430 /* Strip any leading namespaces/classes, keep only the base name.
18431 DW_AT_name for named DIEs does not contain the prefixes. */
18432 base = strrchr (demangled.get (), ':');
18433 if (base && base > demangled.get () && base[-1] == ':')
18434 base++;
18435 else
18436 base = demangled.get ();
18437
18438 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18439 name = objfile->intern (base);
18440 }
18441 }
18442
18443 fixup_called = 1;
18444 }
18445
18446 /* Read the .debug_loclists header contents from the given SECTION in the
18447 HEADER. */
18448 static void
18449 read_loclist_header (struct loclist_header *header,
18450 struct dwarf2_section_info *section)
18451 {
18452 unsigned int bytes_read;
18453 bfd *abfd = section->get_bfd_owner ();
18454 const gdb_byte *info_ptr = section->buffer;
18455 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18456 info_ptr += bytes_read;
18457 header->version = read_2_bytes (abfd, info_ptr);
18458 info_ptr += 2;
18459 header->addr_size = read_1_byte (abfd, info_ptr);
18460 info_ptr += 1;
18461 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18462 info_ptr += 1;
18463 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18464 }
18465
18466 /* Return the DW_AT_loclists_base value for the CU. */
18467 static ULONGEST
18468 lookup_loclist_base (struct dwarf2_cu *cu)
18469 {
18470 /* For the .dwo unit, the loclist_base points to the first offset following
18471 the header. The header consists of the following entities-
18472 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18473 bit format)
18474 2. version (2 bytes)
18475 3. address size (1 byte)
18476 4. segment selector size (1 byte)
18477 5. offset entry count (4 bytes)
18478 These sizes are derived as per the DWARFv5 standard. */
18479 if (cu->dwo_unit != nullptr)
18480 {
18481 if (cu->header.initial_length_size == 4)
18482 return LOCLIST_HEADER_SIZE32;
18483 return LOCLIST_HEADER_SIZE64;
18484 }
18485 return cu->loclist_base;
18486 }
18487
18488 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18489 array of offsets in the .debug_loclists section. */
18490 static CORE_ADDR
18491 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18492 {
18493 struct dwarf2_per_objfile *dwarf2_per_objfile
18494 = cu->per_cu->dwarf2_per_objfile;
18495 struct objfile *objfile = dwarf2_per_objfile->objfile;
18496 bfd *abfd = objfile->obfd;
18497 ULONGEST loclist_base = lookup_loclist_base (cu);
18498 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18499
18500 section->read (objfile);
18501 if (section->buffer == NULL)
18502 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18503 "section [in module %s]"), objfile_name (objfile));
18504 struct loclist_header header;
18505 read_loclist_header (&header, section);
18506 if (loclist_index >= header.offset_entry_count)
18507 complaint (_("DW_FORM_loclistx pointing outside of "
18508 ".debug_loclists offset array [in module %s]"),
18509 objfile_name (objfile));
18510 if (loclist_base + loclist_index * cu->header.offset_size
18511 >= section->size)
18512 complaint (_("DW_FORM_loclistx pointing outside of "
18513 ".debug_loclists section [in module %s]"),
18514 objfile_name (objfile));
18515 const gdb_byte *info_ptr
18516 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18517
18518 if (cu->header.offset_size == 4)
18519 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18520 else
18521 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18522 }
18523
18524 /* Process the attributes that had to be skipped in the first round. These
18525 attributes are the ones that need str_offsets_base or addr_base attributes.
18526 They could not have been processed in the first round, because at the time
18527 the values of str_offsets_base or addr_base may not have been known. */
18528 static void
18529 read_attribute_reprocess (const struct die_reader_specs *reader,
18530 struct attribute *attr)
18531 {
18532 struct dwarf2_cu *cu = reader->cu;
18533 switch (attr->form)
18534 {
18535 case DW_FORM_addrx:
18536 case DW_FORM_GNU_addr_index:
18537 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18538 break;
18539 case DW_FORM_loclistx:
18540 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18541 break;
18542 case DW_FORM_strx:
18543 case DW_FORM_strx1:
18544 case DW_FORM_strx2:
18545 case DW_FORM_strx3:
18546 case DW_FORM_strx4:
18547 case DW_FORM_GNU_str_index:
18548 {
18549 unsigned int str_index = DW_UNSND (attr);
18550 if (reader->dwo_file != NULL)
18551 {
18552 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18553 DW_STRING_IS_CANONICAL (attr) = 0;
18554 }
18555 else
18556 {
18557 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18558 DW_STRING_IS_CANONICAL (attr) = 0;
18559 }
18560 break;
18561 }
18562 default:
18563 gdb_assert_not_reached (_("Unexpected DWARF form."));
18564 }
18565 }
18566
18567 /* Read an attribute value described by an attribute form. */
18568
18569 static const gdb_byte *
18570 read_attribute_value (const struct die_reader_specs *reader,
18571 struct attribute *attr, unsigned form,
18572 LONGEST implicit_const, const gdb_byte *info_ptr,
18573 bool *need_reprocess)
18574 {
18575 struct dwarf2_cu *cu = reader->cu;
18576 struct dwarf2_per_objfile *dwarf2_per_objfile
18577 = cu->per_cu->dwarf2_per_objfile;
18578 struct objfile *objfile = dwarf2_per_objfile->objfile;
18579 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18580 bfd *abfd = reader->abfd;
18581 struct comp_unit_head *cu_header = &cu->header;
18582 unsigned int bytes_read;
18583 struct dwarf_block *blk;
18584 *need_reprocess = false;
18585
18586 attr->form = (enum dwarf_form) form;
18587 switch (form)
18588 {
18589 case DW_FORM_ref_addr:
18590 if (cu->header.version == 2)
18591 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18592 &bytes_read);
18593 else
18594 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18595 &bytes_read);
18596 info_ptr += bytes_read;
18597 break;
18598 case DW_FORM_GNU_ref_alt:
18599 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18600 info_ptr += bytes_read;
18601 break;
18602 case DW_FORM_addr:
18603 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18604 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18605 info_ptr += bytes_read;
18606 break;
18607 case DW_FORM_block2:
18608 blk = dwarf_alloc_block (cu);
18609 blk->size = read_2_bytes (abfd, info_ptr);
18610 info_ptr += 2;
18611 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18612 info_ptr += blk->size;
18613 DW_BLOCK (attr) = blk;
18614 break;
18615 case DW_FORM_block4:
18616 blk = dwarf_alloc_block (cu);
18617 blk->size = read_4_bytes (abfd, info_ptr);
18618 info_ptr += 4;
18619 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18620 info_ptr += blk->size;
18621 DW_BLOCK (attr) = blk;
18622 break;
18623 case DW_FORM_data2:
18624 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18625 info_ptr += 2;
18626 break;
18627 case DW_FORM_data4:
18628 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18629 info_ptr += 4;
18630 break;
18631 case DW_FORM_data8:
18632 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18633 info_ptr += 8;
18634 break;
18635 case DW_FORM_data16:
18636 blk = dwarf_alloc_block (cu);
18637 blk->size = 16;
18638 blk->data = read_n_bytes (abfd, info_ptr, 16);
18639 info_ptr += 16;
18640 DW_BLOCK (attr) = blk;
18641 break;
18642 case DW_FORM_sec_offset:
18643 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18644 info_ptr += bytes_read;
18645 break;
18646 case DW_FORM_loclistx:
18647 {
18648 *need_reprocess = true;
18649 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18650 info_ptr += bytes_read;
18651 }
18652 break;
18653 case DW_FORM_string:
18654 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18655 DW_STRING_IS_CANONICAL (attr) = 0;
18656 info_ptr += bytes_read;
18657 break;
18658 case DW_FORM_strp:
18659 if (!cu->per_cu->is_dwz)
18660 {
18661 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18662 abfd, info_ptr, cu_header,
18663 &bytes_read);
18664 DW_STRING_IS_CANONICAL (attr) = 0;
18665 info_ptr += bytes_read;
18666 break;
18667 }
18668 /* FALLTHROUGH */
18669 case DW_FORM_line_strp:
18670 if (!cu->per_cu->is_dwz)
18671 {
18672 DW_STRING (attr)
18673 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
18674 &bytes_read);
18675 DW_STRING_IS_CANONICAL (attr) = 0;
18676 info_ptr += bytes_read;
18677 break;
18678 }
18679 /* FALLTHROUGH */
18680 case DW_FORM_GNU_strp_alt:
18681 {
18682 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18683 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18684 &bytes_read);
18685
18686 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
18687 DW_STRING_IS_CANONICAL (attr) = 0;
18688 info_ptr += bytes_read;
18689 }
18690 break;
18691 case DW_FORM_exprloc:
18692 case DW_FORM_block:
18693 blk = dwarf_alloc_block (cu);
18694 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18695 info_ptr += bytes_read;
18696 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18697 info_ptr += blk->size;
18698 DW_BLOCK (attr) = blk;
18699 break;
18700 case DW_FORM_block1:
18701 blk = dwarf_alloc_block (cu);
18702 blk->size = read_1_byte (abfd, info_ptr);
18703 info_ptr += 1;
18704 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18705 info_ptr += blk->size;
18706 DW_BLOCK (attr) = blk;
18707 break;
18708 case DW_FORM_data1:
18709 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18710 info_ptr += 1;
18711 break;
18712 case DW_FORM_flag:
18713 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18714 info_ptr += 1;
18715 break;
18716 case DW_FORM_flag_present:
18717 DW_UNSND (attr) = 1;
18718 break;
18719 case DW_FORM_sdata:
18720 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18721 info_ptr += bytes_read;
18722 break;
18723 case DW_FORM_udata:
18724 case DW_FORM_rnglistx:
18725 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18726 info_ptr += bytes_read;
18727 break;
18728 case DW_FORM_ref1:
18729 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18730 + read_1_byte (abfd, info_ptr));
18731 info_ptr += 1;
18732 break;
18733 case DW_FORM_ref2:
18734 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18735 + read_2_bytes (abfd, info_ptr));
18736 info_ptr += 2;
18737 break;
18738 case DW_FORM_ref4:
18739 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18740 + read_4_bytes (abfd, info_ptr));
18741 info_ptr += 4;
18742 break;
18743 case DW_FORM_ref8:
18744 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18745 + read_8_bytes (abfd, info_ptr));
18746 info_ptr += 8;
18747 break;
18748 case DW_FORM_ref_sig8:
18749 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18750 info_ptr += 8;
18751 break;
18752 case DW_FORM_ref_udata:
18753 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18754 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18755 info_ptr += bytes_read;
18756 break;
18757 case DW_FORM_indirect:
18758 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18759 info_ptr += bytes_read;
18760 if (form == DW_FORM_implicit_const)
18761 {
18762 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18763 info_ptr += bytes_read;
18764 }
18765 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18766 info_ptr, need_reprocess);
18767 break;
18768 case DW_FORM_implicit_const:
18769 DW_SND (attr) = implicit_const;
18770 break;
18771 case DW_FORM_addrx:
18772 case DW_FORM_GNU_addr_index:
18773 *need_reprocess = true;
18774 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18775 info_ptr += bytes_read;
18776 break;
18777 case DW_FORM_strx:
18778 case DW_FORM_strx1:
18779 case DW_FORM_strx2:
18780 case DW_FORM_strx3:
18781 case DW_FORM_strx4:
18782 case DW_FORM_GNU_str_index:
18783 {
18784 ULONGEST str_index;
18785 if (form == DW_FORM_strx1)
18786 {
18787 str_index = read_1_byte (abfd, info_ptr);
18788 info_ptr += 1;
18789 }
18790 else if (form == DW_FORM_strx2)
18791 {
18792 str_index = read_2_bytes (abfd, info_ptr);
18793 info_ptr += 2;
18794 }
18795 else if (form == DW_FORM_strx3)
18796 {
18797 str_index = read_3_bytes (abfd, info_ptr);
18798 info_ptr += 3;
18799 }
18800 else if (form == DW_FORM_strx4)
18801 {
18802 str_index = read_4_bytes (abfd, info_ptr);
18803 info_ptr += 4;
18804 }
18805 else
18806 {
18807 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18808 info_ptr += bytes_read;
18809 }
18810 *need_reprocess = true;
18811 DW_UNSND (attr) = str_index;
18812 }
18813 break;
18814 default:
18815 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
18816 dwarf_form_name (form),
18817 bfd_get_filename (abfd));
18818 }
18819
18820 /* Super hack. */
18821 if (cu->per_cu->is_dwz && attr->form_is_ref ())
18822 attr->form = DW_FORM_GNU_ref_alt;
18823
18824 /* We have seen instances where the compiler tried to emit a byte
18825 size attribute of -1 which ended up being encoded as an unsigned
18826 0xffffffff. Although 0xffffffff is technically a valid size value,
18827 an object of this size seems pretty unlikely so we can relatively
18828 safely treat these cases as if the size attribute was invalid and
18829 treat them as zero by default. */
18830 if (attr->name == DW_AT_byte_size
18831 && form == DW_FORM_data4
18832 && DW_UNSND (attr) >= 0xffffffff)
18833 {
18834 complaint
18835 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
18836 hex_string (DW_UNSND (attr)));
18837 DW_UNSND (attr) = 0;
18838 }
18839
18840 return info_ptr;
18841 }
18842
18843 /* Read an attribute described by an abbreviated attribute. */
18844
18845 static const gdb_byte *
18846 read_attribute (const struct die_reader_specs *reader,
18847 struct attribute *attr, struct attr_abbrev *abbrev,
18848 const gdb_byte *info_ptr, bool *need_reprocess)
18849 {
18850 attr->name = abbrev->name;
18851 return read_attribute_value (reader, attr, abbrev->form,
18852 abbrev->implicit_const, info_ptr,
18853 need_reprocess);
18854 }
18855
18856 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18857
18858 static const char *
18859 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18860 LONGEST str_offset)
18861 {
18862 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
18863 str_offset, "DW_FORM_strp");
18864 }
18865
18866 /* Return pointer to string at .debug_str offset as read from BUF.
18867 BUF is assumed to be in a compilation unit described by CU_HEADER.
18868 Return *BYTES_READ_PTR count of bytes read from BUF. */
18869
18870 static const char *
18871 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18872 const gdb_byte *buf,
18873 const struct comp_unit_head *cu_header,
18874 unsigned int *bytes_read_ptr)
18875 {
18876 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18877
18878 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
18879 }
18880
18881 /* See read.h. */
18882
18883 const char *
18884 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
18885 const struct comp_unit_head *cu_header,
18886 unsigned int *bytes_read_ptr)
18887 {
18888 bfd *abfd = objfile->obfd;
18889 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18890
18891 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
18892 }
18893
18894 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18895 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
18896 ADDR_SIZE is the size of addresses from the CU header. */
18897
18898 static CORE_ADDR
18899 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18900 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18901 int addr_size)
18902 {
18903 struct objfile *objfile = dwarf2_per_objfile->objfile;
18904 bfd *abfd = objfile->obfd;
18905 const gdb_byte *info_ptr;
18906 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
18907
18908 dwarf2_per_objfile->addr.read (objfile);
18909 if (dwarf2_per_objfile->addr.buffer == NULL)
18910 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18911 objfile_name (objfile));
18912 if (addr_base_or_zero + addr_index * addr_size
18913 >= dwarf2_per_objfile->addr.size)
18914 error (_("DW_FORM_addr_index pointing outside of "
18915 ".debug_addr section [in module %s]"),
18916 objfile_name (objfile));
18917 info_ptr = (dwarf2_per_objfile->addr.buffer
18918 + addr_base_or_zero + addr_index * addr_size);
18919 if (addr_size == 4)
18920 return bfd_get_32 (abfd, info_ptr);
18921 else
18922 return bfd_get_64 (abfd, info_ptr);
18923 }
18924
18925 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18926
18927 static CORE_ADDR
18928 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18929 {
18930 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18931 cu->addr_base, cu->header.addr_size);
18932 }
18933
18934 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18935
18936 static CORE_ADDR
18937 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18938 unsigned int *bytes_read)
18939 {
18940 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
18941 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18942
18943 return read_addr_index (cu, addr_index);
18944 }
18945
18946 /* See read.h. */
18947
18948 CORE_ADDR
18949 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
18950 {
18951 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
18952 struct dwarf2_cu *cu = per_cu->cu;
18953 gdb::optional<ULONGEST> addr_base;
18954 int addr_size;
18955
18956 /* We need addr_base and addr_size.
18957 If we don't have PER_CU->cu, we have to get it.
18958 Nasty, but the alternative is storing the needed info in PER_CU,
18959 which at this point doesn't seem justified: it's not clear how frequently
18960 it would get used and it would increase the size of every PER_CU.
18961 Entry points like dwarf2_per_cu_addr_size do a similar thing
18962 so we're not in uncharted territory here.
18963 Alas we need to be a bit more complicated as addr_base is contained
18964 in the DIE.
18965
18966 We don't need to read the entire CU(/TU).
18967 We just need the header and top level die.
18968
18969 IWBN to use the aging mechanism to let us lazily later discard the CU.
18970 For now we skip this optimization. */
18971
18972 if (cu != NULL)
18973 {
18974 addr_base = cu->addr_base;
18975 addr_size = cu->header.addr_size;
18976 }
18977 else
18978 {
18979 cutu_reader reader (per_cu, NULL, 0, false);
18980 addr_base = reader.cu->addr_base;
18981 addr_size = reader.cu->header.addr_size;
18982 }
18983
18984 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18985 addr_size);
18986 }
18987
18988 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18989 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18990 DWO file. */
18991
18992 static const char *
18993 read_str_index (struct dwarf2_cu *cu,
18994 struct dwarf2_section_info *str_section,
18995 struct dwarf2_section_info *str_offsets_section,
18996 ULONGEST str_offsets_base, ULONGEST str_index)
18997 {
18998 struct dwarf2_per_objfile *dwarf2_per_objfile
18999 = cu->per_cu->dwarf2_per_objfile;
19000 struct objfile *objfile = dwarf2_per_objfile->objfile;
19001 const char *objf_name = objfile_name (objfile);
19002 bfd *abfd = objfile->obfd;
19003 const gdb_byte *info_ptr;
19004 ULONGEST str_offset;
19005 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19006
19007 str_section->read (objfile);
19008 str_offsets_section->read (objfile);
19009 if (str_section->buffer == NULL)
19010 error (_("%s used without %s section"
19011 " in CU at offset %s [in module %s]"),
19012 form_name, str_section->get_name (),
19013 sect_offset_str (cu->header.sect_off), objf_name);
19014 if (str_offsets_section->buffer == NULL)
19015 error (_("%s used without %s section"
19016 " in CU at offset %s [in module %s]"),
19017 form_name, str_section->get_name (),
19018 sect_offset_str (cu->header.sect_off), objf_name);
19019 info_ptr = (str_offsets_section->buffer
19020 + str_offsets_base
19021 + str_index * cu->header.offset_size);
19022 if (cu->header.offset_size == 4)
19023 str_offset = bfd_get_32 (abfd, info_ptr);
19024 else
19025 str_offset = bfd_get_64 (abfd, info_ptr);
19026 if (str_offset >= str_section->size)
19027 error (_("Offset from %s pointing outside of"
19028 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19029 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19030 return (const char *) (str_section->buffer + str_offset);
19031 }
19032
19033 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19034
19035 static const char *
19036 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19037 {
19038 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19039 ? reader->cu->header.addr_size : 0;
19040 return read_str_index (reader->cu,
19041 &reader->dwo_file->sections.str,
19042 &reader->dwo_file->sections.str_offsets,
19043 str_offsets_base, str_index);
19044 }
19045
19046 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19047
19048 static const char *
19049 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19050 {
19051 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19052 const char *objf_name = objfile_name (objfile);
19053 static const char form_name[] = "DW_FORM_GNU_str_index";
19054 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19055
19056 if (!cu->str_offsets_base.has_value ())
19057 error (_("%s used in Fission stub without %s"
19058 " in CU at offset 0x%lx [in module %s]"),
19059 form_name, str_offsets_attr_name,
19060 (long) cu->header.offset_size, objf_name);
19061
19062 return read_str_index (cu,
19063 &cu->per_cu->dwarf2_per_objfile->str,
19064 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19065 *cu->str_offsets_base, str_index);
19066 }
19067
19068 /* Return the length of an LEB128 number in BUF. */
19069
19070 static int
19071 leb128_size (const gdb_byte *buf)
19072 {
19073 const gdb_byte *begin = buf;
19074 gdb_byte byte;
19075
19076 while (1)
19077 {
19078 byte = *buf++;
19079 if ((byte & 128) == 0)
19080 return buf - begin;
19081 }
19082 }
19083
19084 static void
19085 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19086 {
19087 switch (lang)
19088 {
19089 case DW_LANG_C89:
19090 case DW_LANG_C99:
19091 case DW_LANG_C11:
19092 case DW_LANG_C:
19093 case DW_LANG_UPC:
19094 cu->language = language_c;
19095 break;
19096 case DW_LANG_Java:
19097 case DW_LANG_C_plus_plus:
19098 case DW_LANG_C_plus_plus_11:
19099 case DW_LANG_C_plus_plus_14:
19100 cu->language = language_cplus;
19101 break;
19102 case DW_LANG_D:
19103 cu->language = language_d;
19104 break;
19105 case DW_LANG_Fortran77:
19106 case DW_LANG_Fortran90:
19107 case DW_LANG_Fortran95:
19108 case DW_LANG_Fortran03:
19109 case DW_LANG_Fortran08:
19110 cu->language = language_fortran;
19111 break;
19112 case DW_LANG_Go:
19113 cu->language = language_go;
19114 break;
19115 case DW_LANG_Mips_Assembler:
19116 cu->language = language_asm;
19117 break;
19118 case DW_LANG_Ada83:
19119 case DW_LANG_Ada95:
19120 cu->language = language_ada;
19121 break;
19122 case DW_LANG_Modula2:
19123 cu->language = language_m2;
19124 break;
19125 case DW_LANG_Pascal83:
19126 cu->language = language_pascal;
19127 break;
19128 case DW_LANG_ObjC:
19129 cu->language = language_objc;
19130 break;
19131 case DW_LANG_Rust:
19132 case DW_LANG_Rust_old:
19133 cu->language = language_rust;
19134 break;
19135 case DW_LANG_Cobol74:
19136 case DW_LANG_Cobol85:
19137 default:
19138 cu->language = language_minimal;
19139 break;
19140 }
19141 cu->language_defn = language_def (cu->language);
19142 }
19143
19144 /* Return the named attribute or NULL if not there. */
19145
19146 static struct attribute *
19147 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19148 {
19149 for (;;)
19150 {
19151 unsigned int i;
19152 struct attribute *spec = NULL;
19153
19154 for (i = 0; i < die->num_attrs; ++i)
19155 {
19156 if (die->attrs[i].name == name)
19157 return &die->attrs[i];
19158 if (die->attrs[i].name == DW_AT_specification
19159 || die->attrs[i].name == DW_AT_abstract_origin)
19160 spec = &die->attrs[i];
19161 }
19162
19163 if (!spec)
19164 break;
19165
19166 die = follow_die_ref (die, spec, &cu);
19167 }
19168
19169 return NULL;
19170 }
19171
19172 /* Return the string associated with a string-typed attribute, or NULL if it
19173 is either not found or is of an incorrect type. */
19174
19175 static const char *
19176 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19177 {
19178 struct attribute *attr;
19179 const char *str = NULL;
19180
19181 attr = dwarf2_attr (die, name, cu);
19182
19183 if (attr != NULL)
19184 {
19185 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19186 || attr->form == DW_FORM_string
19187 || attr->form == DW_FORM_strx
19188 || attr->form == DW_FORM_strx1
19189 || attr->form == DW_FORM_strx2
19190 || attr->form == DW_FORM_strx3
19191 || attr->form == DW_FORM_strx4
19192 || attr->form == DW_FORM_GNU_str_index
19193 || attr->form == DW_FORM_GNU_strp_alt)
19194 str = DW_STRING (attr);
19195 else
19196 complaint (_("string type expected for attribute %s for "
19197 "DIE at %s in module %s"),
19198 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19199 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19200 }
19201
19202 return str;
19203 }
19204
19205 /* Return the dwo name or NULL if not present. If present, it is in either
19206 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19207 static const char *
19208 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19209 {
19210 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19211 if (dwo_name == nullptr)
19212 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19213 return dwo_name;
19214 }
19215
19216 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19217 and holds a non-zero value. This function should only be used for
19218 DW_FORM_flag or DW_FORM_flag_present attributes. */
19219
19220 static int
19221 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19222 {
19223 struct attribute *attr = dwarf2_attr (die, name, cu);
19224
19225 return (attr && DW_UNSND (attr));
19226 }
19227
19228 static int
19229 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19230 {
19231 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19232 which value is non-zero. However, we have to be careful with
19233 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19234 (via dwarf2_flag_true_p) follows this attribute. So we may
19235 end up accidently finding a declaration attribute that belongs
19236 to a different DIE referenced by the specification attribute,
19237 even though the given DIE does not have a declaration attribute. */
19238 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19239 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19240 }
19241
19242 /* Return the die giving the specification for DIE, if there is
19243 one. *SPEC_CU is the CU containing DIE on input, and the CU
19244 containing the return value on output. If there is no
19245 specification, but there is an abstract origin, that is
19246 returned. */
19247
19248 static struct die_info *
19249 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19250 {
19251 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19252 *spec_cu);
19253
19254 if (spec_attr == NULL)
19255 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19256
19257 if (spec_attr == NULL)
19258 return NULL;
19259 else
19260 return follow_die_ref (die, spec_attr, spec_cu);
19261 }
19262
19263 /* Stub for free_line_header to match void * callback types. */
19264
19265 static void
19266 free_line_header_voidp (void *arg)
19267 {
19268 struct line_header *lh = (struct line_header *) arg;
19269
19270 delete lh;
19271 }
19272
19273 /* A convenience function to find the proper .debug_line section for a CU. */
19274
19275 static struct dwarf2_section_info *
19276 get_debug_line_section (struct dwarf2_cu *cu)
19277 {
19278 struct dwarf2_section_info *section;
19279 struct dwarf2_per_objfile *dwarf2_per_objfile
19280 = cu->per_cu->dwarf2_per_objfile;
19281
19282 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19283 DWO file. */
19284 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19285 section = &cu->dwo_unit->dwo_file->sections.line;
19286 else if (cu->per_cu->is_dwz)
19287 {
19288 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19289
19290 section = &dwz->line;
19291 }
19292 else
19293 section = &dwarf2_per_objfile->line;
19294
19295 return section;
19296 }
19297
19298 /* Read the statement program header starting at OFFSET in
19299 .debug_line, or .debug_line.dwo. Return a pointer
19300 to a struct line_header, allocated using xmalloc.
19301 Returns NULL if there is a problem reading the header, e.g., if it
19302 has a version we don't understand.
19303
19304 NOTE: the strings in the include directory and file name tables of
19305 the returned object point into the dwarf line section buffer,
19306 and must not be freed. */
19307
19308 static line_header_up
19309 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19310 {
19311 struct dwarf2_section_info *section;
19312 struct dwarf2_per_objfile *dwarf2_per_objfile
19313 = cu->per_cu->dwarf2_per_objfile;
19314
19315 section = get_debug_line_section (cu);
19316 section->read (dwarf2_per_objfile->objfile);
19317 if (section->buffer == NULL)
19318 {
19319 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19320 complaint (_("missing .debug_line.dwo section"));
19321 else
19322 complaint (_("missing .debug_line section"));
19323 return 0;
19324 }
19325
19326 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19327 dwarf2_per_objfile, section,
19328 &cu->header);
19329 }
19330
19331 /* Subroutine of dwarf_decode_lines to simplify it.
19332 Return the file name of the psymtab for the given file_entry.
19333 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19334 If space for the result is malloc'd, *NAME_HOLDER will be set.
19335 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19336
19337 static const char *
19338 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19339 const dwarf2_psymtab *pst,
19340 const char *comp_dir,
19341 gdb::unique_xmalloc_ptr<char> *name_holder)
19342 {
19343 const char *include_name = fe.name;
19344 const char *include_name_to_compare = include_name;
19345 const char *pst_filename;
19346 int file_is_pst;
19347
19348 const char *dir_name = fe.include_dir (lh);
19349
19350 gdb::unique_xmalloc_ptr<char> hold_compare;
19351 if (!IS_ABSOLUTE_PATH (include_name)
19352 && (dir_name != NULL || comp_dir != NULL))
19353 {
19354 /* Avoid creating a duplicate psymtab for PST.
19355 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19356 Before we do the comparison, however, we need to account
19357 for DIR_NAME and COMP_DIR.
19358 First prepend dir_name (if non-NULL). If we still don't
19359 have an absolute path prepend comp_dir (if non-NULL).
19360 However, the directory we record in the include-file's
19361 psymtab does not contain COMP_DIR (to match the
19362 corresponding symtab(s)).
19363
19364 Example:
19365
19366 bash$ cd /tmp
19367 bash$ gcc -g ./hello.c
19368 include_name = "hello.c"
19369 dir_name = "."
19370 DW_AT_comp_dir = comp_dir = "/tmp"
19371 DW_AT_name = "./hello.c"
19372
19373 */
19374
19375 if (dir_name != NULL)
19376 {
19377 name_holder->reset (concat (dir_name, SLASH_STRING,
19378 include_name, (char *) NULL));
19379 include_name = name_holder->get ();
19380 include_name_to_compare = include_name;
19381 }
19382 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19383 {
19384 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19385 include_name, (char *) NULL));
19386 include_name_to_compare = hold_compare.get ();
19387 }
19388 }
19389
19390 pst_filename = pst->filename;
19391 gdb::unique_xmalloc_ptr<char> copied_name;
19392 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19393 {
19394 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19395 pst_filename, (char *) NULL));
19396 pst_filename = copied_name.get ();
19397 }
19398
19399 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19400
19401 if (file_is_pst)
19402 return NULL;
19403 return include_name;
19404 }
19405
19406 /* State machine to track the state of the line number program. */
19407
19408 class lnp_state_machine
19409 {
19410 public:
19411 /* Initialize a machine state for the start of a line number
19412 program. */
19413 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19414 bool record_lines_p);
19415
19416 file_entry *current_file ()
19417 {
19418 /* lh->file_names is 0-based, but the file name numbers in the
19419 statement program are 1-based. */
19420 return m_line_header->file_name_at (m_file);
19421 }
19422
19423 /* Record the line in the state machine. END_SEQUENCE is true if
19424 we're processing the end of a sequence. */
19425 void record_line (bool end_sequence);
19426
19427 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19428 nop-out rest of the lines in this sequence. */
19429 void check_line_address (struct dwarf2_cu *cu,
19430 const gdb_byte *line_ptr,
19431 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19432
19433 void handle_set_discriminator (unsigned int discriminator)
19434 {
19435 m_discriminator = discriminator;
19436 m_line_has_non_zero_discriminator |= discriminator != 0;
19437 }
19438
19439 /* Handle DW_LNE_set_address. */
19440 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19441 {
19442 m_op_index = 0;
19443 address += baseaddr;
19444 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19445 }
19446
19447 /* Handle DW_LNS_advance_pc. */
19448 void handle_advance_pc (CORE_ADDR adjust);
19449
19450 /* Handle a special opcode. */
19451 void handle_special_opcode (unsigned char op_code);
19452
19453 /* Handle DW_LNS_advance_line. */
19454 void handle_advance_line (int line_delta)
19455 {
19456 advance_line (line_delta);
19457 }
19458
19459 /* Handle DW_LNS_set_file. */
19460 void handle_set_file (file_name_index file);
19461
19462 /* Handle DW_LNS_negate_stmt. */
19463 void handle_negate_stmt ()
19464 {
19465 m_is_stmt = !m_is_stmt;
19466 }
19467
19468 /* Handle DW_LNS_const_add_pc. */
19469 void handle_const_add_pc ();
19470
19471 /* Handle DW_LNS_fixed_advance_pc. */
19472 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19473 {
19474 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19475 m_op_index = 0;
19476 }
19477
19478 /* Handle DW_LNS_copy. */
19479 void handle_copy ()
19480 {
19481 record_line (false);
19482 m_discriminator = 0;
19483 }
19484
19485 /* Handle DW_LNE_end_sequence. */
19486 void handle_end_sequence ()
19487 {
19488 m_currently_recording_lines = true;
19489 }
19490
19491 private:
19492 /* Advance the line by LINE_DELTA. */
19493 void advance_line (int line_delta)
19494 {
19495 m_line += line_delta;
19496
19497 if (line_delta != 0)
19498 m_line_has_non_zero_discriminator = m_discriminator != 0;
19499 }
19500
19501 struct dwarf2_cu *m_cu;
19502
19503 gdbarch *m_gdbarch;
19504
19505 /* True if we're recording lines.
19506 Otherwise we're building partial symtabs and are just interested in
19507 finding include files mentioned by the line number program. */
19508 bool m_record_lines_p;
19509
19510 /* The line number header. */
19511 line_header *m_line_header;
19512
19513 /* These are part of the standard DWARF line number state machine,
19514 and initialized according to the DWARF spec. */
19515
19516 unsigned char m_op_index = 0;
19517 /* The line table index of the current file. */
19518 file_name_index m_file = 1;
19519 unsigned int m_line = 1;
19520
19521 /* These are initialized in the constructor. */
19522
19523 CORE_ADDR m_address;
19524 bool m_is_stmt;
19525 unsigned int m_discriminator;
19526
19527 /* Additional bits of state we need to track. */
19528
19529 /* The last file that we called dwarf2_start_subfile for.
19530 This is only used for TLLs. */
19531 unsigned int m_last_file = 0;
19532 /* The last file a line number was recorded for. */
19533 struct subfile *m_last_subfile = NULL;
19534
19535 /* When true, record the lines we decode. */
19536 bool m_currently_recording_lines = false;
19537
19538 /* The last line number that was recorded, used to coalesce
19539 consecutive entries for the same line. This can happen, for
19540 example, when discriminators are present. PR 17276. */
19541 unsigned int m_last_line = 0;
19542 bool m_line_has_non_zero_discriminator = false;
19543 };
19544
19545 void
19546 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19547 {
19548 CORE_ADDR addr_adj = (((m_op_index + adjust)
19549 / m_line_header->maximum_ops_per_instruction)
19550 * m_line_header->minimum_instruction_length);
19551 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19552 m_op_index = ((m_op_index + adjust)
19553 % m_line_header->maximum_ops_per_instruction);
19554 }
19555
19556 void
19557 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19558 {
19559 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19560 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19561 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19562 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19563 / m_line_header->maximum_ops_per_instruction)
19564 * m_line_header->minimum_instruction_length);
19565 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19566 m_op_index = ((m_op_index + adj_opcode_d)
19567 % m_line_header->maximum_ops_per_instruction);
19568
19569 int line_delta = m_line_header->line_base + adj_opcode_r;
19570 advance_line (line_delta);
19571 record_line (false);
19572 m_discriminator = 0;
19573 }
19574
19575 void
19576 lnp_state_machine::handle_set_file (file_name_index file)
19577 {
19578 m_file = file;
19579
19580 const file_entry *fe = current_file ();
19581 if (fe == NULL)
19582 dwarf2_debug_line_missing_file_complaint ();
19583 else if (m_record_lines_p)
19584 {
19585 const char *dir = fe->include_dir (m_line_header);
19586
19587 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19588 m_line_has_non_zero_discriminator = m_discriminator != 0;
19589 dwarf2_start_subfile (m_cu, fe->name, dir);
19590 }
19591 }
19592
19593 void
19594 lnp_state_machine::handle_const_add_pc ()
19595 {
19596 CORE_ADDR adjust
19597 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19598
19599 CORE_ADDR addr_adj
19600 = (((m_op_index + adjust)
19601 / m_line_header->maximum_ops_per_instruction)
19602 * m_line_header->minimum_instruction_length);
19603
19604 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19605 m_op_index = ((m_op_index + adjust)
19606 % m_line_header->maximum_ops_per_instruction);
19607 }
19608
19609 /* Return non-zero if we should add LINE to the line number table.
19610 LINE is the line to add, LAST_LINE is the last line that was added,
19611 LAST_SUBFILE is the subfile for LAST_LINE.
19612 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19613 had a non-zero discriminator.
19614
19615 We have to be careful in the presence of discriminators.
19616 E.g., for this line:
19617
19618 for (i = 0; i < 100000; i++);
19619
19620 clang can emit four line number entries for that one line,
19621 each with a different discriminator.
19622 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19623
19624 However, we want gdb to coalesce all four entries into one.
19625 Otherwise the user could stepi into the middle of the line and
19626 gdb would get confused about whether the pc really was in the
19627 middle of the line.
19628
19629 Things are further complicated by the fact that two consecutive
19630 line number entries for the same line is a heuristic used by gcc
19631 to denote the end of the prologue. So we can't just discard duplicate
19632 entries, we have to be selective about it. The heuristic we use is
19633 that we only collapse consecutive entries for the same line if at least
19634 one of those entries has a non-zero discriminator. PR 17276.
19635
19636 Note: Addresses in the line number state machine can never go backwards
19637 within one sequence, thus this coalescing is ok. */
19638
19639 static int
19640 dwarf_record_line_p (struct dwarf2_cu *cu,
19641 unsigned int line, unsigned int last_line,
19642 int line_has_non_zero_discriminator,
19643 struct subfile *last_subfile)
19644 {
19645 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19646 return 1;
19647 if (line != last_line)
19648 return 1;
19649 /* Same line for the same file that we've seen already.
19650 As a last check, for pr 17276, only record the line if the line
19651 has never had a non-zero discriminator. */
19652 if (!line_has_non_zero_discriminator)
19653 return 1;
19654 return 0;
19655 }
19656
19657 /* Use the CU's builder to record line number LINE beginning at
19658 address ADDRESS in the line table of subfile SUBFILE. */
19659
19660 static void
19661 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19662 unsigned int line, CORE_ADDR address, bool is_stmt,
19663 struct dwarf2_cu *cu)
19664 {
19665 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19666
19667 if (dwarf_line_debug)
19668 {
19669 fprintf_unfiltered (gdb_stdlog,
19670 "Recording line %u, file %s, address %s\n",
19671 line, lbasename (subfile->name),
19672 paddress (gdbarch, address));
19673 }
19674
19675 if (cu != nullptr)
19676 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
19677 }
19678
19679 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19680 Mark the end of a set of line number records.
19681 The arguments are the same as for dwarf_record_line_1.
19682 If SUBFILE is NULL the request is ignored. */
19683
19684 static void
19685 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19686 CORE_ADDR address, struct dwarf2_cu *cu)
19687 {
19688 if (subfile == NULL)
19689 return;
19690
19691 if (dwarf_line_debug)
19692 {
19693 fprintf_unfiltered (gdb_stdlog,
19694 "Finishing current line, file %s, address %s\n",
19695 lbasename (subfile->name),
19696 paddress (gdbarch, address));
19697 }
19698
19699 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
19700 }
19701
19702 void
19703 lnp_state_machine::record_line (bool end_sequence)
19704 {
19705 if (dwarf_line_debug)
19706 {
19707 fprintf_unfiltered (gdb_stdlog,
19708 "Processing actual line %u: file %u,"
19709 " address %s, is_stmt %u, discrim %u%s\n",
19710 m_line, m_file,
19711 paddress (m_gdbarch, m_address),
19712 m_is_stmt, m_discriminator,
19713 (end_sequence ? "\t(end sequence)" : ""));
19714 }
19715
19716 file_entry *fe = current_file ();
19717
19718 if (fe == NULL)
19719 dwarf2_debug_line_missing_file_complaint ();
19720 /* For now we ignore lines not starting on an instruction boundary.
19721 But not when processing end_sequence for compatibility with the
19722 previous version of the code. */
19723 else if (m_op_index == 0 || end_sequence)
19724 {
19725 fe->included_p = 1;
19726 if (m_record_lines_p)
19727 {
19728 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
19729 || end_sequence)
19730 {
19731 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19732 m_currently_recording_lines ? m_cu : nullptr);
19733 }
19734
19735 if (!end_sequence)
19736 {
19737 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
19738
19739 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
19740 m_line_has_non_zero_discriminator,
19741 m_last_subfile))
19742 {
19743 buildsym_compunit *builder = m_cu->get_builder ();
19744 dwarf_record_line_1 (m_gdbarch,
19745 builder->get_current_subfile (),
19746 m_line, m_address, is_stmt,
19747 m_currently_recording_lines ? m_cu : nullptr);
19748 }
19749 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19750 m_last_line = m_line;
19751 }
19752 }
19753 }
19754 }
19755
19756 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
19757 line_header *lh, bool record_lines_p)
19758 {
19759 m_cu = cu;
19760 m_gdbarch = arch;
19761 m_record_lines_p = record_lines_p;
19762 m_line_header = lh;
19763
19764 m_currently_recording_lines = true;
19765
19766 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19767 was a line entry for it so that the backend has a chance to adjust it
19768 and also record it in case it needs it. This is currently used by MIPS
19769 code, cf. `mips_adjust_dwarf2_line'. */
19770 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19771 m_is_stmt = lh->default_is_stmt;
19772 m_discriminator = 0;
19773 }
19774
19775 void
19776 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19777 const gdb_byte *line_ptr,
19778 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
19779 {
19780 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
19781 the pc range of the CU. However, we restrict the test to only ADDRESS
19782 values of zero to preserve GDB's previous behaviour which is to handle
19783 the specific case of a function being GC'd by the linker. */
19784
19785 if (address == 0 && address < unrelocated_lowpc)
19786 {
19787 /* This line table is for a function which has been
19788 GCd by the linker. Ignore it. PR gdb/12528 */
19789
19790 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19791 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19792
19793 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
19794 line_offset, objfile_name (objfile));
19795 m_currently_recording_lines = false;
19796 /* Note: m_currently_recording_lines is left as false until we see
19797 DW_LNE_end_sequence. */
19798 }
19799 }
19800
19801 /* Subroutine of dwarf_decode_lines to simplify it.
19802 Process the line number information in LH.
19803 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19804 program in order to set included_p for every referenced header. */
19805
19806 static void
19807 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19808 const int decode_for_pst_p, CORE_ADDR lowpc)
19809 {
19810 const gdb_byte *line_ptr, *extended_end;
19811 const gdb_byte *line_end;
19812 unsigned int bytes_read, extended_len;
19813 unsigned char op_code, extended_op;
19814 CORE_ADDR baseaddr;
19815 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19816 bfd *abfd = objfile->obfd;
19817 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19818 /* True if we're recording line info (as opposed to building partial
19819 symtabs and just interested in finding include files mentioned by
19820 the line number program). */
19821 bool record_lines_p = !decode_for_pst_p;
19822
19823 baseaddr = objfile->text_section_offset ();
19824
19825 line_ptr = lh->statement_program_start;
19826 line_end = lh->statement_program_end;
19827
19828 /* Read the statement sequences until there's nothing left. */
19829 while (line_ptr < line_end)
19830 {
19831 /* The DWARF line number program state machine. Reset the state
19832 machine at the start of each sequence. */
19833 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
19834 bool end_sequence = false;
19835
19836 if (record_lines_p)
19837 {
19838 /* Start a subfile for the current file of the state
19839 machine. */
19840 const file_entry *fe = state_machine.current_file ();
19841
19842 if (fe != NULL)
19843 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
19844 }
19845
19846 /* Decode the table. */
19847 while (line_ptr < line_end && !end_sequence)
19848 {
19849 op_code = read_1_byte (abfd, line_ptr);
19850 line_ptr += 1;
19851
19852 if (op_code >= lh->opcode_base)
19853 {
19854 /* Special opcode. */
19855 state_machine.handle_special_opcode (op_code);
19856 }
19857 else switch (op_code)
19858 {
19859 case DW_LNS_extended_op:
19860 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19861 &bytes_read);
19862 line_ptr += bytes_read;
19863 extended_end = line_ptr + extended_len;
19864 extended_op = read_1_byte (abfd, line_ptr);
19865 line_ptr += 1;
19866 switch (extended_op)
19867 {
19868 case DW_LNE_end_sequence:
19869 state_machine.handle_end_sequence ();
19870 end_sequence = true;
19871 break;
19872 case DW_LNE_set_address:
19873 {
19874 CORE_ADDR address
19875 = cu->header.read_address (abfd, line_ptr, &bytes_read);
19876 line_ptr += bytes_read;
19877
19878 state_machine.check_line_address (cu, line_ptr,
19879 lowpc - baseaddr, address);
19880 state_machine.handle_set_address (baseaddr, address);
19881 }
19882 break;
19883 case DW_LNE_define_file:
19884 {
19885 const char *cur_file;
19886 unsigned int mod_time, length;
19887 dir_index dindex;
19888
19889 cur_file = read_direct_string (abfd, line_ptr,
19890 &bytes_read);
19891 line_ptr += bytes_read;
19892 dindex = (dir_index)
19893 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19894 line_ptr += bytes_read;
19895 mod_time =
19896 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19897 line_ptr += bytes_read;
19898 length =
19899 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19900 line_ptr += bytes_read;
19901 lh->add_file_name (cur_file, dindex, mod_time, length);
19902 }
19903 break;
19904 case DW_LNE_set_discriminator:
19905 {
19906 /* The discriminator is not interesting to the
19907 debugger; just ignore it. We still need to
19908 check its value though:
19909 if there are consecutive entries for the same
19910 (non-prologue) line we want to coalesce them.
19911 PR 17276. */
19912 unsigned int discr
19913 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19914 line_ptr += bytes_read;
19915
19916 state_machine.handle_set_discriminator (discr);
19917 }
19918 break;
19919 default:
19920 complaint (_("mangled .debug_line section"));
19921 return;
19922 }
19923 /* Make sure that we parsed the extended op correctly. If e.g.
19924 we expected a different address size than the producer used,
19925 we may have read the wrong number of bytes. */
19926 if (line_ptr != extended_end)
19927 {
19928 complaint (_("mangled .debug_line section"));
19929 return;
19930 }
19931 break;
19932 case DW_LNS_copy:
19933 state_machine.handle_copy ();
19934 break;
19935 case DW_LNS_advance_pc:
19936 {
19937 CORE_ADDR adjust
19938 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19939 line_ptr += bytes_read;
19940
19941 state_machine.handle_advance_pc (adjust);
19942 }
19943 break;
19944 case DW_LNS_advance_line:
19945 {
19946 int line_delta
19947 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
19948 line_ptr += bytes_read;
19949
19950 state_machine.handle_advance_line (line_delta);
19951 }
19952 break;
19953 case DW_LNS_set_file:
19954 {
19955 file_name_index file
19956 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19957 &bytes_read);
19958 line_ptr += bytes_read;
19959
19960 state_machine.handle_set_file (file);
19961 }
19962 break;
19963 case DW_LNS_set_column:
19964 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19965 line_ptr += bytes_read;
19966 break;
19967 case DW_LNS_negate_stmt:
19968 state_machine.handle_negate_stmt ();
19969 break;
19970 case DW_LNS_set_basic_block:
19971 break;
19972 /* Add to the address register of the state machine the
19973 address increment value corresponding to special opcode
19974 255. I.e., this value is scaled by the minimum
19975 instruction length since special opcode 255 would have
19976 scaled the increment. */
19977 case DW_LNS_const_add_pc:
19978 state_machine.handle_const_add_pc ();
19979 break;
19980 case DW_LNS_fixed_advance_pc:
19981 {
19982 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
19983 line_ptr += 2;
19984
19985 state_machine.handle_fixed_advance_pc (addr_adj);
19986 }
19987 break;
19988 default:
19989 {
19990 /* Unknown standard opcode, ignore it. */
19991 int i;
19992
19993 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
19994 {
19995 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19996 line_ptr += bytes_read;
19997 }
19998 }
19999 }
20000 }
20001
20002 if (!end_sequence)
20003 dwarf2_debug_line_missing_end_sequence_complaint ();
20004
20005 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20006 in which case we still finish recording the last line). */
20007 state_machine.record_line (true);
20008 }
20009 }
20010
20011 /* Decode the Line Number Program (LNP) for the given line_header
20012 structure and CU. The actual information extracted and the type
20013 of structures created from the LNP depends on the value of PST.
20014
20015 1. If PST is NULL, then this procedure uses the data from the program
20016 to create all necessary symbol tables, and their linetables.
20017
20018 2. If PST is not NULL, this procedure reads the program to determine
20019 the list of files included by the unit represented by PST, and
20020 builds all the associated partial symbol tables.
20021
20022 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20023 It is used for relative paths in the line table.
20024 NOTE: When processing partial symtabs (pst != NULL),
20025 comp_dir == pst->dirname.
20026
20027 NOTE: It is important that psymtabs have the same file name (via strcmp)
20028 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20029 symtab we don't use it in the name of the psymtabs we create.
20030 E.g. expand_line_sal requires this when finding psymtabs to expand.
20031 A good testcase for this is mb-inline.exp.
20032
20033 LOWPC is the lowest address in CU (or 0 if not known).
20034
20035 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20036 for its PC<->lines mapping information. Otherwise only the filename
20037 table is read in. */
20038
20039 static void
20040 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20041 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20042 CORE_ADDR lowpc, int decode_mapping)
20043 {
20044 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20045 const int decode_for_pst_p = (pst != NULL);
20046
20047 if (decode_mapping)
20048 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20049
20050 if (decode_for_pst_p)
20051 {
20052 /* Now that we're done scanning the Line Header Program, we can
20053 create the psymtab of each included file. */
20054 for (auto &file_entry : lh->file_names ())
20055 if (file_entry.included_p == 1)
20056 {
20057 gdb::unique_xmalloc_ptr<char> name_holder;
20058 const char *include_name =
20059 psymtab_include_file_name (lh, file_entry, pst,
20060 comp_dir, &name_holder);
20061 if (include_name != NULL)
20062 dwarf2_create_include_psymtab (include_name, pst, objfile);
20063 }
20064 }
20065 else
20066 {
20067 /* Make sure a symtab is created for every file, even files
20068 which contain only variables (i.e. no code with associated
20069 line numbers). */
20070 buildsym_compunit *builder = cu->get_builder ();
20071 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20072
20073 for (auto &fe : lh->file_names ())
20074 {
20075 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20076 if (builder->get_current_subfile ()->symtab == NULL)
20077 {
20078 builder->get_current_subfile ()->symtab
20079 = allocate_symtab (cust,
20080 builder->get_current_subfile ()->name);
20081 }
20082 fe.symtab = builder->get_current_subfile ()->symtab;
20083 }
20084 }
20085 }
20086
20087 /* Start a subfile for DWARF. FILENAME is the name of the file and
20088 DIRNAME the name of the source directory which contains FILENAME
20089 or NULL if not known.
20090 This routine tries to keep line numbers from identical absolute and
20091 relative file names in a common subfile.
20092
20093 Using the `list' example from the GDB testsuite, which resides in
20094 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20095 of /srcdir/list0.c yields the following debugging information for list0.c:
20096
20097 DW_AT_name: /srcdir/list0.c
20098 DW_AT_comp_dir: /compdir
20099 files.files[0].name: list0.h
20100 files.files[0].dir: /srcdir
20101 files.files[1].name: list0.c
20102 files.files[1].dir: /srcdir
20103
20104 The line number information for list0.c has to end up in a single
20105 subfile, so that `break /srcdir/list0.c:1' works as expected.
20106 start_subfile will ensure that this happens provided that we pass the
20107 concatenation of files.files[1].dir and files.files[1].name as the
20108 subfile's name. */
20109
20110 static void
20111 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20112 const char *dirname)
20113 {
20114 gdb::unique_xmalloc_ptr<char> copy;
20115
20116 /* In order not to lose the line information directory,
20117 we concatenate it to the filename when it makes sense.
20118 Note that the Dwarf3 standard says (speaking of filenames in line
20119 information): ``The directory index is ignored for file names
20120 that represent full path names''. Thus ignoring dirname in the
20121 `else' branch below isn't an issue. */
20122
20123 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20124 {
20125 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20126 filename = copy.get ();
20127 }
20128
20129 cu->get_builder ()->start_subfile (filename);
20130 }
20131
20132 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20133 buildsym_compunit constructor. */
20134
20135 struct compunit_symtab *
20136 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20137 CORE_ADDR low_pc)
20138 {
20139 gdb_assert (m_builder == nullptr);
20140
20141 m_builder.reset (new struct buildsym_compunit
20142 (per_cu->dwarf2_per_objfile->objfile,
20143 name, comp_dir, language, low_pc));
20144
20145 list_in_scope = get_builder ()->get_file_symbols ();
20146
20147 get_builder ()->record_debugformat ("DWARF 2");
20148 get_builder ()->record_producer (producer);
20149
20150 processing_has_namespace_info = false;
20151
20152 return get_builder ()->get_compunit_symtab ();
20153 }
20154
20155 static void
20156 var_decode_location (struct attribute *attr, struct symbol *sym,
20157 struct dwarf2_cu *cu)
20158 {
20159 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20160 struct comp_unit_head *cu_header = &cu->header;
20161
20162 /* NOTE drow/2003-01-30: There used to be a comment and some special
20163 code here to turn a symbol with DW_AT_external and a
20164 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20165 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20166 with some versions of binutils) where shared libraries could have
20167 relocations against symbols in their debug information - the
20168 minimal symbol would have the right address, but the debug info
20169 would not. It's no longer necessary, because we will explicitly
20170 apply relocations when we read in the debug information now. */
20171
20172 /* A DW_AT_location attribute with no contents indicates that a
20173 variable has been optimized away. */
20174 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20175 {
20176 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20177 return;
20178 }
20179
20180 /* Handle one degenerate form of location expression specially, to
20181 preserve GDB's previous behavior when section offsets are
20182 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20183 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20184
20185 if (attr->form_is_block ()
20186 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20187 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20188 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20189 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20190 && (DW_BLOCK (attr)->size
20191 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20192 {
20193 unsigned int dummy;
20194
20195 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20196 SET_SYMBOL_VALUE_ADDRESS
20197 (sym, cu->header.read_address (objfile->obfd,
20198 DW_BLOCK (attr)->data + 1,
20199 &dummy));
20200 else
20201 SET_SYMBOL_VALUE_ADDRESS
20202 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20203 &dummy));
20204 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20205 fixup_symbol_section (sym, objfile);
20206 SET_SYMBOL_VALUE_ADDRESS
20207 (sym,
20208 SYMBOL_VALUE_ADDRESS (sym)
20209 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20210 return;
20211 }
20212
20213 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20214 expression evaluator, and use LOC_COMPUTED only when necessary
20215 (i.e. when the value of a register or memory location is
20216 referenced, or a thread-local block, etc.). Then again, it might
20217 not be worthwhile. I'm assuming that it isn't unless performance
20218 or memory numbers show me otherwise. */
20219
20220 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20221
20222 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20223 cu->has_loclist = true;
20224 }
20225
20226 /* Given a pointer to a DWARF information entry, figure out if we need
20227 to make a symbol table entry for it, and if so, create a new entry
20228 and return a pointer to it.
20229 If TYPE is NULL, determine symbol type from the die, otherwise
20230 used the passed type.
20231 If SPACE is not NULL, use it to hold the new symbol. If it is
20232 NULL, allocate a new symbol on the objfile's obstack. */
20233
20234 static struct symbol *
20235 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20236 struct symbol *space)
20237 {
20238 struct dwarf2_per_objfile *dwarf2_per_objfile
20239 = cu->per_cu->dwarf2_per_objfile;
20240 struct objfile *objfile = dwarf2_per_objfile->objfile;
20241 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20242 struct symbol *sym = NULL;
20243 const char *name;
20244 struct attribute *attr = NULL;
20245 struct attribute *attr2 = NULL;
20246 CORE_ADDR baseaddr;
20247 struct pending **list_to_add = NULL;
20248
20249 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20250
20251 baseaddr = objfile->text_section_offset ();
20252
20253 name = dwarf2_name (die, cu);
20254 if (name)
20255 {
20256 const char *linkagename;
20257 int suppress_add = 0;
20258
20259 if (space)
20260 sym = space;
20261 else
20262 sym = allocate_symbol (objfile);
20263 OBJSTAT (objfile, n_syms++);
20264
20265 /* Cache this symbol's name and the name's demangled form (if any). */
20266 sym->set_language (cu->language, &objfile->objfile_obstack);
20267 linkagename = dwarf2_physname (name, die, cu);
20268 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20269
20270 /* Fortran does not have mangling standard and the mangling does differ
20271 between gfortran, iFort etc. */
20272 if (cu->language == language_fortran
20273 && symbol_get_demangled_name (sym) == NULL)
20274 symbol_set_demangled_name (sym,
20275 dwarf2_full_name (name, die, cu),
20276 NULL);
20277
20278 /* Default assumptions.
20279 Use the passed type or decode it from the die. */
20280 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20281 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20282 if (type != NULL)
20283 SYMBOL_TYPE (sym) = type;
20284 else
20285 SYMBOL_TYPE (sym) = die_type (die, cu);
20286 attr = dwarf2_attr (die,
20287 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20288 cu);
20289 if (attr != nullptr)
20290 {
20291 SYMBOL_LINE (sym) = DW_UNSND (attr);
20292 }
20293
20294 attr = dwarf2_attr (die,
20295 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20296 cu);
20297 if (attr != nullptr)
20298 {
20299 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20300 struct file_entry *fe;
20301
20302 if (cu->line_header != NULL)
20303 fe = cu->line_header->file_name_at (file_index);
20304 else
20305 fe = NULL;
20306
20307 if (fe == NULL)
20308 complaint (_("file index out of range"));
20309 else
20310 symbol_set_symtab (sym, fe->symtab);
20311 }
20312
20313 switch (die->tag)
20314 {
20315 case DW_TAG_label:
20316 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20317 if (attr != nullptr)
20318 {
20319 CORE_ADDR addr;
20320
20321 addr = attr->value_as_address ();
20322 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20323 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20324 }
20325 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20326 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20327 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20328 add_symbol_to_list (sym, cu->list_in_scope);
20329 break;
20330 case DW_TAG_subprogram:
20331 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20332 finish_block. */
20333 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20334 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20335 if ((attr2 && (DW_UNSND (attr2) != 0))
20336 || cu->language == language_ada
20337 || cu->language == language_fortran)
20338 {
20339 /* Subprograms marked external are stored as a global symbol.
20340 Ada and Fortran subprograms, whether marked external or
20341 not, are always stored as a global symbol, because we want
20342 to be able to access them globally. For instance, we want
20343 to be able to break on a nested subprogram without having
20344 to specify the context. */
20345 list_to_add = cu->get_builder ()->get_global_symbols ();
20346 }
20347 else
20348 {
20349 list_to_add = cu->list_in_scope;
20350 }
20351 break;
20352 case DW_TAG_inlined_subroutine:
20353 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20354 finish_block. */
20355 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20356 SYMBOL_INLINED (sym) = 1;
20357 list_to_add = cu->list_in_scope;
20358 break;
20359 case DW_TAG_template_value_param:
20360 suppress_add = 1;
20361 /* Fall through. */
20362 case DW_TAG_constant:
20363 case DW_TAG_variable:
20364 case DW_TAG_member:
20365 /* Compilation with minimal debug info may result in
20366 variables with missing type entries. Change the
20367 misleading `void' type to something sensible. */
20368 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20369 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20370
20371 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20372 /* In the case of DW_TAG_member, we should only be called for
20373 static const members. */
20374 if (die->tag == DW_TAG_member)
20375 {
20376 /* dwarf2_add_field uses die_is_declaration,
20377 so we do the same. */
20378 gdb_assert (die_is_declaration (die, cu));
20379 gdb_assert (attr);
20380 }
20381 if (attr != nullptr)
20382 {
20383 dwarf2_const_value (attr, sym, cu);
20384 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20385 if (!suppress_add)
20386 {
20387 if (attr2 && (DW_UNSND (attr2) != 0))
20388 list_to_add = cu->get_builder ()->get_global_symbols ();
20389 else
20390 list_to_add = cu->list_in_scope;
20391 }
20392 break;
20393 }
20394 attr = dwarf2_attr (die, DW_AT_location, cu);
20395 if (attr != nullptr)
20396 {
20397 var_decode_location (attr, sym, cu);
20398 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20399
20400 /* Fortran explicitly imports any global symbols to the local
20401 scope by DW_TAG_common_block. */
20402 if (cu->language == language_fortran && die->parent
20403 && die->parent->tag == DW_TAG_common_block)
20404 attr2 = NULL;
20405
20406 if (SYMBOL_CLASS (sym) == LOC_STATIC
20407 && SYMBOL_VALUE_ADDRESS (sym) == 0
20408 && !dwarf2_per_objfile->has_section_at_zero)
20409 {
20410 /* When a static variable is eliminated by the linker,
20411 the corresponding debug information is not stripped
20412 out, but the variable address is set to null;
20413 do not add such variables into symbol table. */
20414 }
20415 else if (attr2 && (DW_UNSND (attr2) != 0))
20416 {
20417 if (SYMBOL_CLASS (sym) == LOC_STATIC
20418 && (objfile->flags & OBJF_MAINLINE) == 0
20419 && dwarf2_per_objfile->can_copy)
20420 {
20421 /* A global static variable might be subject to
20422 copy relocation. We first check for a local
20423 minsym, though, because maybe the symbol was
20424 marked hidden, in which case this would not
20425 apply. */
20426 bound_minimal_symbol found
20427 = (lookup_minimal_symbol_linkage
20428 (sym->linkage_name (), objfile));
20429 if (found.minsym != nullptr)
20430 sym->maybe_copied = 1;
20431 }
20432
20433 /* A variable with DW_AT_external is never static,
20434 but it may be block-scoped. */
20435 list_to_add
20436 = ((cu->list_in_scope
20437 == cu->get_builder ()->get_file_symbols ())
20438 ? cu->get_builder ()->get_global_symbols ()
20439 : cu->list_in_scope);
20440 }
20441 else
20442 list_to_add = cu->list_in_scope;
20443 }
20444 else
20445 {
20446 /* We do not know the address of this symbol.
20447 If it is an external symbol and we have type information
20448 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20449 The address of the variable will then be determined from
20450 the minimal symbol table whenever the variable is
20451 referenced. */
20452 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20453
20454 /* Fortran explicitly imports any global symbols to the local
20455 scope by DW_TAG_common_block. */
20456 if (cu->language == language_fortran && die->parent
20457 && die->parent->tag == DW_TAG_common_block)
20458 {
20459 /* SYMBOL_CLASS doesn't matter here because
20460 read_common_block is going to reset it. */
20461 if (!suppress_add)
20462 list_to_add = cu->list_in_scope;
20463 }
20464 else if (attr2 && (DW_UNSND (attr2) != 0)
20465 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20466 {
20467 /* A variable with DW_AT_external is never static, but it
20468 may be block-scoped. */
20469 list_to_add
20470 = ((cu->list_in_scope
20471 == cu->get_builder ()->get_file_symbols ())
20472 ? cu->get_builder ()->get_global_symbols ()
20473 : cu->list_in_scope);
20474
20475 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20476 }
20477 else if (!die_is_declaration (die, cu))
20478 {
20479 /* Use the default LOC_OPTIMIZED_OUT class. */
20480 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20481 if (!suppress_add)
20482 list_to_add = cu->list_in_scope;
20483 }
20484 }
20485 break;
20486 case DW_TAG_formal_parameter:
20487 {
20488 /* If we are inside a function, mark this as an argument. If
20489 not, we might be looking at an argument to an inlined function
20490 when we do not have enough information to show inlined frames;
20491 pretend it's a local variable in that case so that the user can
20492 still see it. */
20493 struct context_stack *curr
20494 = cu->get_builder ()->get_current_context_stack ();
20495 if (curr != nullptr && curr->name != nullptr)
20496 SYMBOL_IS_ARGUMENT (sym) = 1;
20497 attr = dwarf2_attr (die, DW_AT_location, cu);
20498 if (attr != nullptr)
20499 {
20500 var_decode_location (attr, sym, cu);
20501 }
20502 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20503 if (attr != nullptr)
20504 {
20505 dwarf2_const_value (attr, sym, cu);
20506 }
20507
20508 list_to_add = cu->list_in_scope;
20509 }
20510 break;
20511 case DW_TAG_unspecified_parameters:
20512 /* From varargs functions; gdb doesn't seem to have any
20513 interest in this information, so just ignore it for now.
20514 (FIXME?) */
20515 break;
20516 case DW_TAG_template_type_param:
20517 suppress_add = 1;
20518 /* Fall through. */
20519 case DW_TAG_class_type:
20520 case DW_TAG_interface_type:
20521 case DW_TAG_structure_type:
20522 case DW_TAG_union_type:
20523 case DW_TAG_set_type:
20524 case DW_TAG_enumeration_type:
20525 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20526 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20527
20528 {
20529 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20530 really ever be static objects: otherwise, if you try
20531 to, say, break of a class's method and you're in a file
20532 which doesn't mention that class, it won't work unless
20533 the check for all static symbols in lookup_symbol_aux
20534 saves you. See the OtherFileClass tests in
20535 gdb.c++/namespace.exp. */
20536
20537 if (!suppress_add)
20538 {
20539 buildsym_compunit *builder = cu->get_builder ();
20540 list_to_add
20541 = (cu->list_in_scope == builder->get_file_symbols ()
20542 && cu->language == language_cplus
20543 ? builder->get_global_symbols ()
20544 : cu->list_in_scope);
20545
20546 /* The semantics of C++ state that "struct foo {
20547 ... }" also defines a typedef for "foo". */
20548 if (cu->language == language_cplus
20549 || cu->language == language_ada
20550 || cu->language == language_d
20551 || cu->language == language_rust)
20552 {
20553 /* The symbol's name is already allocated along
20554 with this objfile, so we don't need to
20555 duplicate it for the type. */
20556 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20557 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20558 }
20559 }
20560 }
20561 break;
20562 case DW_TAG_typedef:
20563 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20564 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20565 list_to_add = cu->list_in_scope;
20566 break;
20567 case DW_TAG_base_type:
20568 case DW_TAG_subrange_type:
20569 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20570 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20571 list_to_add = cu->list_in_scope;
20572 break;
20573 case DW_TAG_enumerator:
20574 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20575 if (attr != nullptr)
20576 {
20577 dwarf2_const_value (attr, sym, cu);
20578 }
20579 {
20580 /* NOTE: carlton/2003-11-10: See comment above in the
20581 DW_TAG_class_type, etc. block. */
20582
20583 list_to_add
20584 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20585 && cu->language == language_cplus
20586 ? cu->get_builder ()->get_global_symbols ()
20587 : cu->list_in_scope);
20588 }
20589 break;
20590 case DW_TAG_imported_declaration:
20591 case DW_TAG_namespace:
20592 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20593 list_to_add = cu->get_builder ()->get_global_symbols ();
20594 break;
20595 case DW_TAG_module:
20596 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20597 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20598 list_to_add = cu->get_builder ()->get_global_symbols ();
20599 break;
20600 case DW_TAG_common_block:
20601 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20602 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20603 add_symbol_to_list (sym, cu->list_in_scope);
20604 break;
20605 default:
20606 /* Not a tag we recognize. Hopefully we aren't processing
20607 trash data, but since we must specifically ignore things
20608 we don't recognize, there is nothing else we should do at
20609 this point. */
20610 complaint (_("unsupported tag: '%s'"),
20611 dwarf_tag_name (die->tag));
20612 break;
20613 }
20614
20615 if (suppress_add)
20616 {
20617 sym->hash_next = objfile->template_symbols;
20618 objfile->template_symbols = sym;
20619 list_to_add = NULL;
20620 }
20621
20622 if (list_to_add != NULL)
20623 add_symbol_to_list (sym, list_to_add);
20624
20625 /* For the benefit of old versions of GCC, check for anonymous
20626 namespaces based on the demangled name. */
20627 if (!cu->processing_has_namespace_info
20628 && cu->language == language_cplus)
20629 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20630 }
20631 return (sym);
20632 }
20633
20634 /* Given an attr with a DW_FORM_dataN value in host byte order,
20635 zero-extend it as appropriate for the symbol's type. The DWARF
20636 standard (v4) is not entirely clear about the meaning of using
20637 DW_FORM_dataN for a constant with a signed type, where the type is
20638 wider than the data. The conclusion of a discussion on the DWARF
20639 list was that this is unspecified. We choose to always zero-extend
20640 because that is the interpretation long in use by GCC. */
20641
20642 static gdb_byte *
20643 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20644 struct dwarf2_cu *cu, LONGEST *value, int bits)
20645 {
20646 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20647 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20648 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20649 LONGEST l = DW_UNSND (attr);
20650
20651 if (bits < sizeof (*value) * 8)
20652 {
20653 l &= ((LONGEST) 1 << bits) - 1;
20654 *value = l;
20655 }
20656 else if (bits == sizeof (*value) * 8)
20657 *value = l;
20658 else
20659 {
20660 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20661 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20662 return bytes;
20663 }
20664
20665 return NULL;
20666 }
20667
20668 /* Read a constant value from an attribute. Either set *VALUE, or if
20669 the value does not fit in *VALUE, set *BYTES - either already
20670 allocated on the objfile obstack, or newly allocated on OBSTACK,
20671 or, set *BATON, if we translated the constant to a location
20672 expression. */
20673
20674 static void
20675 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20676 const char *name, struct obstack *obstack,
20677 struct dwarf2_cu *cu,
20678 LONGEST *value, const gdb_byte **bytes,
20679 struct dwarf2_locexpr_baton **baton)
20680 {
20681 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20682 struct comp_unit_head *cu_header = &cu->header;
20683 struct dwarf_block *blk;
20684 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20685 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20686
20687 *value = 0;
20688 *bytes = NULL;
20689 *baton = NULL;
20690
20691 switch (attr->form)
20692 {
20693 case DW_FORM_addr:
20694 case DW_FORM_addrx:
20695 case DW_FORM_GNU_addr_index:
20696 {
20697 gdb_byte *data;
20698
20699 if (TYPE_LENGTH (type) != cu_header->addr_size)
20700 dwarf2_const_value_length_mismatch_complaint (name,
20701 cu_header->addr_size,
20702 TYPE_LENGTH (type));
20703 /* Symbols of this form are reasonably rare, so we just
20704 piggyback on the existing location code rather than writing
20705 a new implementation of symbol_computed_ops. */
20706 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20707 (*baton)->per_cu = cu->per_cu;
20708 gdb_assert ((*baton)->per_cu);
20709
20710 (*baton)->size = 2 + cu_header->addr_size;
20711 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20712 (*baton)->data = data;
20713
20714 data[0] = DW_OP_addr;
20715 store_unsigned_integer (&data[1], cu_header->addr_size,
20716 byte_order, DW_ADDR (attr));
20717 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20718 }
20719 break;
20720 case DW_FORM_string:
20721 case DW_FORM_strp:
20722 case DW_FORM_strx:
20723 case DW_FORM_GNU_str_index:
20724 case DW_FORM_GNU_strp_alt:
20725 /* DW_STRING is already allocated on the objfile obstack, point
20726 directly to it. */
20727 *bytes = (const gdb_byte *) DW_STRING (attr);
20728 break;
20729 case DW_FORM_block1:
20730 case DW_FORM_block2:
20731 case DW_FORM_block4:
20732 case DW_FORM_block:
20733 case DW_FORM_exprloc:
20734 case DW_FORM_data16:
20735 blk = DW_BLOCK (attr);
20736 if (TYPE_LENGTH (type) != blk->size)
20737 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20738 TYPE_LENGTH (type));
20739 *bytes = blk->data;
20740 break;
20741
20742 /* The DW_AT_const_value attributes are supposed to carry the
20743 symbol's value "represented as it would be on the target
20744 architecture." By the time we get here, it's already been
20745 converted to host endianness, so we just need to sign- or
20746 zero-extend it as appropriate. */
20747 case DW_FORM_data1:
20748 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
20749 break;
20750 case DW_FORM_data2:
20751 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
20752 break;
20753 case DW_FORM_data4:
20754 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
20755 break;
20756 case DW_FORM_data8:
20757 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
20758 break;
20759
20760 case DW_FORM_sdata:
20761 case DW_FORM_implicit_const:
20762 *value = DW_SND (attr);
20763 break;
20764
20765 case DW_FORM_udata:
20766 *value = DW_UNSND (attr);
20767 break;
20768
20769 default:
20770 complaint (_("unsupported const value attribute form: '%s'"),
20771 dwarf_form_name (attr->form));
20772 *value = 0;
20773 break;
20774 }
20775 }
20776
20777
20778 /* Copy constant value from an attribute to a symbol. */
20779
20780 static void
20781 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
20782 struct dwarf2_cu *cu)
20783 {
20784 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20785 LONGEST value;
20786 const gdb_byte *bytes;
20787 struct dwarf2_locexpr_baton *baton;
20788
20789 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
20790 sym->print_name (),
20791 &objfile->objfile_obstack, cu,
20792 &value, &bytes, &baton);
20793
20794 if (baton != NULL)
20795 {
20796 SYMBOL_LOCATION_BATON (sym) = baton;
20797 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
20798 }
20799 else if (bytes != NULL)
20800 {
20801 SYMBOL_VALUE_BYTES (sym) = bytes;
20802 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
20803 }
20804 else
20805 {
20806 SYMBOL_VALUE (sym) = value;
20807 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
20808 }
20809 }
20810
20811 /* Return the type of the die in question using its DW_AT_type attribute. */
20812
20813 static struct type *
20814 die_type (struct die_info *die, struct dwarf2_cu *cu)
20815 {
20816 struct attribute *type_attr;
20817
20818 type_attr = dwarf2_attr (die, DW_AT_type, cu);
20819 if (!type_attr)
20820 {
20821 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20822 /* A missing DW_AT_type represents a void type. */
20823 return objfile_type (objfile)->builtin_void;
20824 }
20825
20826 return lookup_die_type (die, type_attr, cu);
20827 }
20828
20829 /* True iff CU's producer generates GNAT Ada auxiliary information
20830 that allows to find parallel types through that information instead
20831 of having to do expensive parallel lookups by type name. */
20832
20833 static int
20834 need_gnat_info (struct dwarf2_cu *cu)
20835 {
20836 /* Assume that the Ada compiler was GNAT, which always produces
20837 the auxiliary information. */
20838 return (cu->language == language_ada);
20839 }
20840
20841 /* Return the auxiliary type of the die in question using its
20842 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20843 attribute is not present. */
20844
20845 static struct type *
20846 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20847 {
20848 struct attribute *type_attr;
20849
20850 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20851 if (!type_attr)
20852 return NULL;
20853
20854 return lookup_die_type (die, type_attr, cu);
20855 }
20856
20857 /* If DIE has a descriptive_type attribute, then set the TYPE's
20858 descriptive type accordingly. */
20859
20860 static void
20861 set_descriptive_type (struct type *type, struct die_info *die,
20862 struct dwarf2_cu *cu)
20863 {
20864 struct type *descriptive_type = die_descriptive_type (die, cu);
20865
20866 if (descriptive_type)
20867 {
20868 ALLOCATE_GNAT_AUX_TYPE (type);
20869 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20870 }
20871 }
20872
20873 /* Return the containing type of the die in question using its
20874 DW_AT_containing_type attribute. */
20875
20876 static struct type *
20877 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
20878 {
20879 struct attribute *type_attr;
20880 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20881
20882 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
20883 if (!type_attr)
20884 error (_("Dwarf Error: Problem turning containing type into gdb type "
20885 "[in module %s]"), objfile_name (objfile));
20886
20887 return lookup_die_type (die, type_attr, cu);
20888 }
20889
20890 /* Return an error marker type to use for the ill formed type in DIE/CU. */
20891
20892 static struct type *
20893 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20894 {
20895 struct dwarf2_per_objfile *dwarf2_per_objfile
20896 = cu->per_cu->dwarf2_per_objfile;
20897 struct objfile *objfile = dwarf2_per_objfile->objfile;
20898 char *saved;
20899
20900 std::string message
20901 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
20902 objfile_name (objfile),
20903 sect_offset_str (cu->header.sect_off),
20904 sect_offset_str (die->sect_off));
20905 saved = obstack_strdup (&objfile->objfile_obstack, message);
20906
20907 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
20908 }
20909
20910 /* Look up the type of DIE in CU using its type attribute ATTR.
20911 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20912 DW_AT_containing_type.
20913 If there is no type substitute an error marker. */
20914
20915 static struct type *
20916 lookup_die_type (struct die_info *die, const struct attribute *attr,
20917 struct dwarf2_cu *cu)
20918 {
20919 struct dwarf2_per_objfile *dwarf2_per_objfile
20920 = cu->per_cu->dwarf2_per_objfile;
20921 struct objfile *objfile = dwarf2_per_objfile->objfile;
20922 struct type *this_type;
20923
20924 gdb_assert (attr->name == DW_AT_type
20925 || attr->name == DW_AT_GNAT_descriptive_type
20926 || attr->name == DW_AT_containing_type);
20927
20928 /* First see if we have it cached. */
20929
20930 if (attr->form == DW_FORM_GNU_ref_alt)
20931 {
20932 struct dwarf2_per_cu_data *per_cu;
20933 sect_offset sect_off = attr->get_ref_die_offset ();
20934
20935 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
20936 dwarf2_per_objfile);
20937 this_type = get_die_type_at_offset (sect_off, per_cu);
20938 }
20939 else if (attr->form_is_ref ())
20940 {
20941 sect_offset sect_off = attr->get_ref_die_offset ();
20942
20943 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
20944 }
20945 else if (attr->form == DW_FORM_ref_sig8)
20946 {
20947 ULONGEST signature = DW_SIGNATURE (attr);
20948
20949 return get_signatured_type (die, signature, cu);
20950 }
20951 else
20952 {
20953 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
20954 " at %s [in module %s]"),
20955 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
20956 objfile_name (objfile));
20957 return build_error_marker_type (cu, die);
20958 }
20959
20960 /* If not cached we need to read it in. */
20961
20962 if (this_type == NULL)
20963 {
20964 struct die_info *type_die = NULL;
20965 struct dwarf2_cu *type_cu = cu;
20966
20967 if (attr->form_is_ref ())
20968 type_die = follow_die_ref (die, attr, &type_cu);
20969 if (type_die == NULL)
20970 return build_error_marker_type (cu, die);
20971 /* If we find the type now, it's probably because the type came
20972 from an inter-CU reference and the type's CU got expanded before
20973 ours. */
20974 this_type = read_type_die (type_die, type_cu);
20975 }
20976
20977 /* If we still don't have a type use an error marker. */
20978
20979 if (this_type == NULL)
20980 return build_error_marker_type (cu, die);
20981
20982 return this_type;
20983 }
20984
20985 /* Return the type in DIE, CU.
20986 Returns NULL for invalid types.
20987
20988 This first does a lookup in die_type_hash,
20989 and only reads the die in if necessary.
20990
20991 NOTE: This can be called when reading in partial or full symbols. */
20992
20993 static struct type *
20994 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
20995 {
20996 struct type *this_type;
20997
20998 this_type = get_die_type (die, cu);
20999 if (this_type)
21000 return this_type;
21001
21002 return read_type_die_1 (die, cu);
21003 }
21004
21005 /* Read the type in DIE, CU.
21006 Returns NULL for invalid types. */
21007
21008 static struct type *
21009 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21010 {
21011 struct type *this_type = NULL;
21012
21013 switch (die->tag)
21014 {
21015 case DW_TAG_class_type:
21016 case DW_TAG_interface_type:
21017 case DW_TAG_structure_type:
21018 case DW_TAG_union_type:
21019 this_type = read_structure_type (die, cu);
21020 break;
21021 case DW_TAG_enumeration_type:
21022 this_type = read_enumeration_type (die, cu);
21023 break;
21024 case DW_TAG_subprogram:
21025 case DW_TAG_subroutine_type:
21026 case DW_TAG_inlined_subroutine:
21027 this_type = read_subroutine_type (die, cu);
21028 break;
21029 case DW_TAG_array_type:
21030 this_type = read_array_type (die, cu);
21031 break;
21032 case DW_TAG_set_type:
21033 this_type = read_set_type (die, cu);
21034 break;
21035 case DW_TAG_pointer_type:
21036 this_type = read_tag_pointer_type (die, cu);
21037 break;
21038 case DW_TAG_ptr_to_member_type:
21039 this_type = read_tag_ptr_to_member_type (die, cu);
21040 break;
21041 case DW_TAG_reference_type:
21042 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21043 break;
21044 case DW_TAG_rvalue_reference_type:
21045 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21046 break;
21047 case DW_TAG_const_type:
21048 this_type = read_tag_const_type (die, cu);
21049 break;
21050 case DW_TAG_volatile_type:
21051 this_type = read_tag_volatile_type (die, cu);
21052 break;
21053 case DW_TAG_restrict_type:
21054 this_type = read_tag_restrict_type (die, cu);
21055 break;
21056 case DW_TAG_string_type:
21057 this_type = read_tag_string_type (die, cu);
21058 break;
21059 case DW_TAG_typedef:
21060 this_type = read_typedef (die, cu);
21061 break;
21062 case DW_TAG_subrange_type:
21063 this_type = read_subrange_type (die, cu);
21064 break;
21065 case DW_TAG_base_type:
21066 this_type = read_base_type (die, cu);
21067 break;
21068 case DW_TAG_unspecified_type:
21069 this_type = read_unspecified_type (die, cu);
21070 break;
21071 case DW_TAG_namespace:
21072 this_type = read_namespace_type (die, cu);
21073 break;
21074 case DW_TAG_module:
21075 this_type = read_module_type (die, cu);
21076 break;
21077 case DW_TAG_atomic_type:
21078 this_type = read_tag_atomic_type (die, cu);
21079 break;
21080 default:
21081 complaint (_("unexpected tag in read_type_die: '%s'"),
21082 dwarf_tag_name (die->tag));
21083 break;
21084 }
21085
21086 return this_type;
21087 }
21088
21089 /* See if we can figure out if the class lives in a namespace. We do
21090 this by looking for a member function; its demangled name will
21091 contain namespace info, if there is any.
21092 Return the computed name or NULL.
21093 Space for the result is allocated on the objfile's obstack.
21094 This is the full-die version of guess_partial_die_structure_name.
21095 In this case we know DIE has no useful parent. */
21096
21097 static const char *
21098 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21099 {
21100 struct die_info *spec_die;
21101 struct dwarf2_cu *spec_cu;
21102 struct die_info *child;
21103 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21104
21105 spec_cu = cu;
21106 spec_die = die_specification (die, &spec_cu);
21107 if (spec_die != NULL)
21108 {
21109 die = spec_die;
21110 cu = spec_cu;
21111 }
21112
21113 for (child = die->child;
21114 child != NULL;
21115 child = child->sibling)
21116 {
21117 if (child->tag == DW_TAG_subprogram)
21118 {
21119 const char *linkage_name = dw2_linkage_name (child, cu);
21120
21121 if (linkage_name != NULL)
21122 {
21123 gdb::unique_xmalloc_ptr<char> actual_name
21124 (language_class_name_from_physname (cu->language_defn,
21125 linkage_name));
21126 const char *name = NULL;
21127
21128 if (actual_name != NULL)
21129 {
21130 const char *die_name = dwarf2_name (die, cu);
21131
21132 if (die_name != NULL
21133 && strcmp (die_name, actual_name.get ()) != 0)
21134 {
21135 /* Strip off the class name from the full name.
21136 We want the prefix. */
21137 int die_name_len = strlen (die_name);
21138 int actual_name_len = strlen (actual_name.get ());
21139 const char *ptr = actual_name.get ();
21140
21141 /* Test for '::' as a sanity check. */
21142 if (actual_name_len > die_name_len + 2
21143 && ptr[actual_name_len - die_name_len - 1] == ':')
21144 name = obstack_strndup (
21145 &objfile->per_bfd->storage_obstack,
21146 ptr, actual_name_len - die_name_len - 2);
21147 }
21148 }
21149 return name;
21150 }
21151 }
21152 }
21153
21154 return NULL;
21155 }
21156
21157 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21158 prefix part in such case. See
21159 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21160
21161 static const char *
21162 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21163 {
21164 struct attribute *attr;
21165 const char *base;
21166
21167 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21168 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21169 return NULL;
21170
21171 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21172 return NULL;
21173
21174 attr = dw2_linkage_name_attr (die, cu);
21175 if (attr == NULL || DW_STRING (attr) == NULL)
21176 return NULL;
21177
21178 /* dwarf2_name had to be already called. */
21179 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21180
21181 /* Strip the base name, keep any leading namespaces/classes. */
21182 base = strrchr (DW_STRING (attr), ':');
21183 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21184 return "";
21185
21186 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21187 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21188 DW_STRING (attr),
21189 &base[-1] - DW_STRING (attr));
21190 }
21191
21192 /* Return the name of the namespace/class that DIE is defined within,
21193 or "" if we can't tell. The caller should not xfree the result.
21194
21195 For example, if we're within the method foo() in the following
21196 code:
21197
21198 namespace N {
21199 class C {
21200 void foo () {
21201 }
21202 };
21203 }
21204
21205 then determine_prefix on foo's die will return "N::C". */
21206
21207 static const char *
21208 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21209 {
21210 struct dwarf2_per_objfile *dwarf2_per_objfile
21211 = cu->per_cu->dwarf2_per_objfile;
21212 struct die_info *parent, *spec_die;
21213 struct dwarf2_cu *spec_cu;
21214 struct type *parent_type;
21215 const char *retval;
21216
21217 if (cu->language != language_cplus
21218 && cu->language != language_fortran && cu->language != language_d
21219 && cu->language != language_rust)
21220 return "";
21221
21222 retval = anonymous_struct_prefix (die, cu);
21223 if (retval)
21224 return retval;
21225
21226 /* We have to be careful in the presence of DW_AT_specification.
21227 For example, with GCC 3.4, given the code
21228
21229 namespace N {
21230 void foo() {
21231 // Definition of N::foo.
21232 }
21233 }
21234
21235 then we'll have a tree of DIEs like this:
21236
21237 1: DW_TAG_compile_unit
21238 2: DW_TAG_namespace // N
21239 3: DW_TAG_subprogram // declaration of N::foo
21240 4: DW_TAG_subprogram // definition of N::foo
21241 DW_AT_specification // refers to die #3
21242
21243 Thus, when processing die #4, we have to pretend that we're in
21244 the context of its DW_AT_specification, namely the contex of die
21245 #3. */
21246 spec_cu = cu;
21247 spec_die = die_specification (die, &spec_cu);
21248 if (spec_die == NULL)
21249 parent = die->parent;
21250 else
21251 {
21252 parent = spec_die->parent;
21253 cu = spec_cu;
21254 }
21255
21256 if (parent == NULL)
21257 return "";
21258 else if (parent->building_fullname)
21259 {
21260 const char *name;
21261 const char *parent_name;
21262
21263 /* It has been seen on RealView 2.2 built binaries,
21264 DW_TAG_template_type_param types actually _defined_ as
21265 children of the parent class:
21266
21267 enum E {};
21268 template class <class Enum> Class{};
21269 Class<enum E> class_e;
21270
21271 1: DW_TAG_class_type (Class)
21272 2: DW_TAG_enumeration_type (E)
21273 3: DW_TAG_enumerator (enum1:0)
21274 3: DW_TAG_enumerator (enum2:1)
21275 ...
21276 2: DW_TAG_template_type_param
21277 DW_AT_type DW_FORM_ref_udata (E)
21278
21279 Besides being broken debug info, it can put GDB into an
21280 infinite loop. Consider:
21281
21282 When we're building the full name for Class<E>, we'll start
21283 at Class, and go look over its template type parameters,
21284 finding E. We'll then try to build the full name of E, and
21285 reach here. We're now trying to build the full name of E,
21286 and look over the parent DIE for containing scope. In the
21287 broken case, if we followed the parent DIE of E, we'd again
21288 find Class, and once again go look at its template type
21289 arguments, etc., etc. Simply don't consider such parent die
21290 as source-level parent of this die (it can't be, the language
21291 doesn't allow it), and break the loop here. */
21292 name = dwarf2_name (die, cu);
21293 parent_name = dwarf2_name (parent, cu);
21294 complaint (_("template param type '%s' defined within parent '%s'"),
21295 name ? name : "<unknown>",
21296 parent_name ? parent_name : "<unknown>");
21297 return "";
21298 }
21299 else
21300 switch (parent->tag)
21301 {
21302 case DW_TAG_namespace:
21303 parent_type = read_type_die (parent, cu);
21304 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21305 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21306 Work around this problem here. */
21307 if (cu->language == language_cplus
21308 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21309 return "";
21310 /* We give a name to even anonymous namespaces. */
21311 return TYPE_NAME (parent_type);
21312 case DW_TAG_class_type:
21313 case DW_TAG_interface_type:
21314 case DW_TAG_structure_type:
21315 case DW_TAG_union_type:
21316 case DW_TAG_module:
21317 parent_type = read_type_die (parent, cu);
21318 if (TYPE_NAME (parent_type) != NULL)
21319 return TYPE_NAME (parent_type);
21320 else
21321 /* An anonymous structure is only allowed non-static data
21322 members; no typedefs, no member functions, et cetera.
21323 So it does not need a prefix. */
21324 return "";
21325 case DW_TAG_compile_unit:
21326 case DW_TAG_partial_unit:
21327 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21328 if (cu->language == language_cplus
21329 && !dwarf2_per_objfile->types.empty ()
21330 && die->child != NULL
21331 && (die->tag == DW_TAG_class_type
21332 || die->tag == DW_TAG_structure_type
21333 || die->tag == DW_TAG_union_type))
21334 {
21335 const char *name = guess_full_die_structure_name (die, cu);
21336 if (name != NULL)
21337 return name;
21338 }
21339 return "";
21340 case DW_TAG_subprogram:
21341 /* Nested subroutines in Fortran get a prefix with the name
21342 of the parent's subroutine. */
21343 if (cu->language == language_fortran)
21344 {
21345 if ((die->tag == DW_TAG_subprogram)
21346 && (dwarf2_name (parent, cu) != NULL))
21347 return dwarf2_name (parent, cu);
21348 }
21349 return determine_prefix (parent, cu);
21350 case DW_TAG_enumeration_type:
21351 parent_type = read_type_die (parent, cu);
21352 if (TYPE_DECLARED_CLASS (parent_type))
21353 {
21354 if (TYPE_NAME (parent_type) != NULL)
21355 return TYPE_NAME (parent_type);
21356 return "";
21357 }
21358 /* Fall through. */
21359 default:
21360 return determine_prefix (parent, cu);
21361 }
21362 }
21363
21364 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21365 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21366 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21367 an obconcat, otherwise allocate storage for the result. The CU argument is
21368 used to determine the language and hence, the appropriate separator. */
21369
21370 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21371
21372 static char *
21373 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21374 int physname, struct dwarf2_cu *cu)
21375 {
21376 const char *lead = "";
21377 const char *sep;
21378
21379 if (suffix == NULL || suffix[0] == '\0'
21380 || prefix == NULL || prefix[0] == '\0')
21381 sep = "";
21382 else if (cu->language == language_d)
21383 {
21384 /* For D, the 'main' function could be defined in any module, but it
21385 should never be prefixed. */
21386 if (strcmp (suffix, "D main") == 0)
21387 {
21388 prefix = "";
21389 sep = "";
21390 }
21391 else
21392 sep = ".";
21393 }
21394 else if (cu->language == language_fortran && physname)
21395 {
21396 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21397 DW_AT_MIPS_linkage_name is preferred and used instead. */
21398
21399 lead = "__";
21400 sep = "_MOD_";
21401 }
21402 else
21403 sep = "::";
21404
21405 if (prefix == NULL)
21406 prefix = "";
21407 if (suffix == NULL)
21408 suffix = "";
21409
21410 if (obs == NULL)
21411 {
21412 char *retval
21413 = ((char *)
21414 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21415
21416 strcpy (retval, lead);
21417 strcat (retval, prefix);
21418 strcat (retval, sep);
21419 strcat (retval, suffix);
21420 return retval;
21421 }
21422 else
21423 {
21424 /* We have an obstack. */
21425 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21426 }
21427 }
21428
21429 /* Get name of a die, return NULL if not found. */
21430
21431 static const char *
21432 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21433 struct objfile *objfile)
21434 {
21435 if (name && cu->language == language_cplus)
21436 {
21437 std::string canon_name = cp_canonicalize_string (name);
21438
21439 if (!canon_name.empty ())
21440 {
21441 if (canon_name != name)
21442 name = objfile->intern (canon_name);
21443 }
21444 }
21445
21446 return name;
21447 }
21448
21449 /* Get name of a die, return NULL if not found.
21450 Anonymous namespaces are converted to their magic string. */
21451
21452 static const char *
21453 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21454 {
21455 struct attribute *attr;
21456 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21457
21458 attr = dwarf2_attr (die, DW_AT_name, cu);
21459 if ((!attr || !DW_STRING (attr))
21460 && die->tag != DW_TAG_namespace
21461 && die->tag != DW_TAG_class_type
21462 && die->tag != DW_TAG_interface_type
21463 && die->tag != DW_TAG_structure_type
21464 && die->tag != DW_TAG_union_type)
21465 return NULL;
21466
21467 switch (die->tag)
21468 {
21469 case DW_TAG_compile_unit:
21470 case DW_TAG_partial_unit:
21471 /* Compilation units have a DW_AT_name that is a filename, not
21472 a source language identifier. */
21473 case DW_TAG_enumeration_type:
21474 case DW_TAG_enumerator:
21475 /* These tags always have simple identifiers already; no need
21476 to canonicalize them. */
21477 return DW_STRING (attr);
21478
21479 case DW_TAG_namespace:
21480 if (attr != NULL && DW_STRING (attr) != NULL)
21481 return DW_STRING (attr);
21482 return CP_ANONYMOUS_NAMESPACE_STR;
21483
21484 case DW_TAG_class_type:
21485 case DW_TAG_interface_type:
21486 case DW_TAG_structure_type:
21487 case DW_TAG_union_type:
21488 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21489 structures or unions. These were of the form "._%d" in GCC 4.1,
21490 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21491 and GCC 4.4. We work around this problem by ignoring these. */
21492 if (attr && DW_STRING (attr)
21493 && (startswith (DW_STRING (attr), "._")
21494 || startswith (DW_STRING (attr), "<anonymous")))
21495 return NULL;
21496
21497 /* GCC might emit a nameless typedef that has a linkage name. See
21498 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21499 if (!attr || DW_STRING (attr) == NULL)
21500 {
21501 attr = dw2_linkage_name_attr (die, cu);
21502 if (attr == NULL || DW_STRING (attr) == NULL)
21503 return NULL;
21504
21505 /* Avoid demangling DW_STRING (attr) the second time on a second
21506 call for the same DIE. */
21507 if (!DW_STRING_IS_CANONICAL (attr))
21508 {
21509 gdb::unique_xmalloc_ptr<char> demangled
21510 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21511 if (demangled == nullptr)
21512 return nullptr;
21513
21514 DW_STRING (attr) = objfile->intern (demangled.get ());
21515 DW_STRING_IS_CANONICAL (attr) = 1;
21516 }
21517
21518 /* Strip any leading namespaces/classes, keep only the base name.
21519 DW_AT_name for named DIEs does not contain the prefixes. */
21520 const char *base = strrchr (DW_STRING (attr), ':');
21521 if (base && base > DW_STRING (attr) && base[-1] == ':')
21522 return &base[1];
21523 else
21524 return DW_STRING (attr);
21525 }
21526 break;
21527
21528 default:
21529 break;
21530 }
21531
21532 if (!DW_STRING_IS_CANONICAL (attr))
21533 {
21534 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21535 objfile);
21536 DW_STRING_IS_CANONICAL (attr) = 1;
21537 }
21538 return DW_STRING (attr);
21539 }
21540
21541 /* Return the die that this die in an extension of, or NULL if there
21542 is none. *EXT_CU is the CU containing DIE on input, and the CU
21543 containing the return value on output. */
21544
21545 static struct die_info *
21546 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21547 {
21548 struct attribute *attr;
21549
21550 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21551 if (attr == NULL)
21552 return NULL;
21553
21554 return follow_die_ref (die, attr, ext_cu);
21555 }
21556
21557 static void
21558 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21559 {
21560 unsigned int i;
21561
21562 print_spaces (indent, f);
21563 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21564 dwarf_tag_name (die->tag), die->abbrev,
21565 sect_offset_str (die->sect_off));
21566
21567 if (die->parent != NULL)
21568 {
21569 print_spaces (indent, f);
21570 fprintf_unfiltered (f, " parent at offset: %s\n",
21571 sect_offset_str (die->parent->sect_off));
21572 }
21573
21574 print_spaces (indent, f);
21575 fprintf_unfiltered (f, " has children: %s\n",
21576 dwarf_bool_name (die->child != NULL));
21577
21578 print_spaces (indent, f);
21579 fprintf_unfiltered (f, " attributes:\n");
21580
21581 for (i = 0; i < die->num_attrs; ++i)
21582 {
21583 print_spaces (indent, f);
21584 fprintf_unfiltered (f, " %s (%s) ",
21585 dwarf_attr_name (die->attrs[i].name),
21586 dwarf_form_name (die->attrs[i].form));
21587
21588 switch (die->attrs[i].form)
21589 {
21590 case DW_FORM_addr:
21591 case DW_FORM_addrx:
21592 case DW_FORM_GNU_addr_index:
21593 fprintf_unfiltered (f, "address: ");
21594 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21595 break;
21596 case DW_FORM_block2:
21597 case DW_FORM_block4:
21598 case DW_FORM_block:
21599 case DW_FORM_block1:
21600 fprintf_unfiltered (f, "block: size %s",
21601 pulongest (DW_BLOCK (&die->attrs[i])->size));
21602 break;
21603 case DW_FORM_exprloc:
21604 fprintf_unfiltered (f, "expression: size %s",
21605 pulongest (DW_BLOCK (&die->attrs[i])->size));
21606 break;
21607 case DW_FORM_data16:
21608 fprintf_unfiltered (f, "constant of 16 bytes");
21609 break;
21610 case DW_FORM_ref_addr:
21611 fprintf_unfiltered (f, "ref address: ");
21612 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21613 break;
21614 case DW_FORM_GNU_ref_alt:
21615 fprintf_unfiltered (f, "alt ref address: ");
21616 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21617 break;
21618 case DW_FORM_ref1:
21619 case DW_FORM_ref2:
21620 case DW_FORM_ref4:
21621 case DW_FORM_ref8:
21622 case DW_FORM_ref_udata:
21623 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21624 (long) (DW_UNSND (&die->attrs[i])));
21625 break;
21626 case DW_FORM_data1:
21627 case DW_FORM_data2:
21628 case DW_FORM_data4:
21629 case DW_FORM_data8:
21630 case DW_FORM_udata:
21631 case DW_FORM_sdata:
21632 fprintf_unfiltered (f, "constant: %s",
21633 pulongest (DW_UNSND (&die->attrs[i])));
21634 break;
21635 case DW_FORM_sec_offset:
21636 fprintf_unfiltered (f, "section offset: %s",
21637 pulongest (DW_UNSND (&die->attrs[i])));
21638 break;
21639 case DW_FORM_ref_sig8:
21640 fprintf_unfiltered (f, "signature: %s",
21641 hex_string (DW_SIGNATURE (&die->attrs[i])));
21642 break;
21643 case DW_FORM_string:
21644 case DW_FORM_strp:
21645 case DW_FORM_line_strp:
21646 case DW_FORM_strx:
21647 case DW_FORM_GNU_str_index:
21648 case DW_FORM_GNU_strp_alt:
21649 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21650 DW_STRING (&die->attrs[i])
21651 ? DW_STRING (&die->attrs[i]) : "",
21652 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21653 break;
21654 case DW_FORM_flag:
21655 if (DW_UNSND (&die->attrs[i]))
21656 fprintf_unfiltered (f, "flag: TRUE");
21657 else
21658 fprintf_unfiltered (f, "flag: FALSE");
21659 break;
21660 case DW_FORM_flag_present:
21661 fprintf_unfiltered (f, "flag: TRUE");
21662 break;
21663 case DW_FORM_indirect:
21664 /* The reader will have reduced the indirect form to
21665 the "base form" so this form should not occur. */
21666 fprintf_unfiltered (f,
21667 "unexpected attribute form: DW_FORM_indirect");
21668 break;
21669 case DW_FORM_implicit_const:
21670 fprintf_unfiltered (f, "constant: %s",
21671 plongest (DW_SND (&die->attrs[i])));
21672 break;
21673 default:
21674 fprintf_unfiltered (f, "unsupported attribute form: %d.",
21675 die->attrs[i].form);
21676 break;
21677 }
21678 fprintf_unfiltered (f, "\n");
21679 }
21680 }
21681
21682 static void
21683 dump_die_for_error (struct die_info *die)
21684 {
21685 dump_die_shallow (gdb_stderr, 0, die);
21686 }
21687
21688 static void
21689 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21690 {
21691 int indent = level * 4;
21692
21693 gdb_assert (die != NULL);
21694
21695 if (level >= max_level)
21696 return;
21697
21698 dump_die_shallow (f, indent, die);
21699
21700 if (die->child != NULL)
21701 {
21702 print_spaces (indent, f);
21703 fprintf_unfiltered (f, " Children:");
21704 if (level + 1 < max_level)
21705 {
21706 fprintf_unfiltered (f, "\n");
21707 dump_die_1 (f, level + 1, max_level, die->child);
21708 }
21709 else
21710 {
21711 fprintf_unfiltered (f,
21712 " [not printed, max nesting level reached]\n");
21713 }
21714 }
21715
21716 if (die->sibling != NULL && level > 0)
21717 {
21718 dump_die_1 (f, level, max_level, die->sibling);
21719 }
21720 }
21721
21722 /* This is called from the pdie macro in gdbinit.in.
21723 It's not static so gcc will keep a copy callable from gdb. */
21724
21725 void
21726 dump_die (struct die_info *die, int max_level)
21727 {
21728 dump_die_1 (gdb_stdlog, 0, max_level, die);
21729 }
21730
21731 static void
21732 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
21733 {
21734 void **slot;
21735
21736 slot = htab_find_slot_with_hash (cu->die_hash, die,
21737 to_underlying (die->sect_off),
21738 INSERT);
21739
21740 *slot = die;
21741 }
21742
21743 /* Follow reference or signature attribute ATTR of SRC_DIE.
21744 On entry *REF_CU is the CU of SRC_DIE.
21745 On exit *REF_CU is the CU of the result. */
21746
21747 static struct die_info *
21748 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
21749 struct dwarf2_cu **ref_cu)
21750 {
21751 struct die_info *die;
21752
21753 if (attr->form_is_ref ())
21754 die = follow_die_ref (src_die, attr, ref_cu);
21755 else if (attr->form == DW_FORM_ref_sig8)
21756 die = follow_die_sig (src_die, attr, ref_cu);
21757 else
21758 {
21759 dump_die_for_error (src_die);
21760 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
21761 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
21762 }
21763
21764 return die;
21765 }
21766
21767 /* Follow reference OFFSET.
21768 On entry *REF_CU is the CU of the source die referencing OFFSET.
21769 On exit *REF_CU is the CU of the result.
21770 Returns NULL if OFFSET is invalid. */
21771
21772 static struct die_info *
21773 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
21774 struct dwarf2_cu **ref_cu)
21775 {
21776 struct die_info temp_die;
21777 struct dwarf2_cu *target_cu, *cu = *ref_cu;
21778 struct dwarf2_per_objfile *dwarf2_per_objfile
21779 = cu->per_cu->dwarf2_per_objfile;
21780
21781 gdb_assert (cu->per_cu != NULL);
21782
21783 target_cu = cu;
21784
21785 if (cu->per_cu->is_debug_types)
21786 {
21787 /* .debug_types CUs cannot reference anything outside their CU.
21788 If they need to, they have to reference a signatured type via
21789 DW_FORM_ref_sig8. */
21790 if (!cu->header.offset_in_cu_p (sect_off))
21791 return NULL;
21792 }
21793 else if (offset_in_dwz != cu->per_cu->is_dwz
21794 || !cu->header.offset_in_cu_p (sect_off))
21795 {
21796 struct dwarf2_per_cu_data *per_cu;
21797
21798 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
21799 dwarf2_per_objfile);
21800
21801 /* If necessary, add it to the queue and load its DIEs. */
21802 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
21803 load_full_comp_unit (per_cu, false, cu->language);
21804
21805 target_cu = per_cu->cu;
21806 }
21807 else if (cu->dies == NULL)
21808 {
21809 /* We're loading full DIEs during partial symbol reading. */
21810 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
21811 load_full_comp_unit (cu->per_cu, false, language_minimal);
21812 }
21813
21814 *ref_cu = target_cu;
21815 temp_die.sect_off = sect_off;
21816
21817 if (target_cu != cu)
21818 target_cu->ancestor = cu;
21819
21820 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
21821 &temp_die,
21822 to_underlying (sect_off));
21823 }
21824
21825 /* Follow reference attribute ATTR of SRC_DIE.
21826 On entry *REF_CU is the CU of SRC_DIE.
21827 On exit *REF_CU is the CU of the result. */
21828
21829 static struct die_info *
21830 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
21831 struct dwarf2_cu **ref_cu)
21832 {
21833 sect_offset sect_off = attr->get_ref_die_offset ();
21834 struct dwarf2_cu *cu = *ref_cu;
21835 struct die_info *die;
21836
21837 die = follow_die_offset (sect_off,
21838 (attr->form == DW_FORM_GNU_ref_alt
21839 || cu->per_cu->is_dwz),
21840 ref_cu);
21841 if (!die)
21842 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
21843 "at %s [in module %s]"),
21844 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
21845 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
21846
21847 return die;
21848 }
21849
21850 /* See read.h. */
21851
21852 struct dwarf2_locexpr_baton
21853 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
21854 dwarf2_per_cu_data *per_cu,
21855 CORE_ADDR (*get_frame_pc) (void *baton),
21856 void *baton, bool resolve_abstract_p)
21857 {
21858 struct dwarf2_cu *cu;
21859 struct die_info *die;
21860 struct attribute *attr;
21861 struct dwarf2_locexpr_baton retval;
21862 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
21863 struct objfile *objfile = dwarf2_per_objfile->objfile;
21864
21865 if (per_cu->cu == NULL)
21866 load_cu (per_cu, false);
21867 cu = per_cu->cu;
21868 if (cu == NULL)
21869 {
21870 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21871 Instead just throw an error, not much else we can do. */
21872 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21873 sect_offset_str (sect_off), objfile_name (objfile));
21874 }
21875
21876 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21877 if (!die)
21878 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21879 sect_offset_str (sect_off), objfile_name (objfile));
21880
21881 attr = dwarf2_attr (die, DW_AT_location, cu);
21882 if (!attr && resolve_abstract_p
21883 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
21884 != dwarf2_per_objfile->abstract_to_concrete.end ()))
21885 {
21886 CORE_ADDR pc = (*get_frame_pc) (baton);
21887 CORE_ADDR baseaddr = objfile->text_section_offset ();
21888 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21889
21890 for (const auto &cand_off
21891 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
21892 {
21893 struct dwarf2_cu *cand_cu = cu;
21894 struct die_info *cand
21895 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
21896 if (!cand
21897 || !cand->parent
21898 || cand->parent->tag != DW_TAG_subprogram)
21899 continue;
21900
21901 CORE_ADDR pc_low, pc_high;
21902 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
21903 if (pc_low == ((CORE_ADDR) -1))
21904 continue;
21905 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
21906 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
21907 if (!(pc_low <= pc && pc < pc_high))
21908 continue;
21909
21910 die = cand;
21911 attr = dwarf2_attr (die, DW_AT_location, cu);
21912 break;
21913 }
21914 }
21915
21916 if (!attr)
21917 {
21918 /* DWARF: "If there is no such attribute, then there is no effect.".
21919 DATA is ignored if SIZE is 0. */
21920
21921 retval.data = NULL;
21922 retval.size = 0;
21923 }
21924 else if (attr->form_is_section_offset ())
21925 {
21926 struct dwarf2_loclist_baton loclist_baton;
21927 CORE_ADDR pc = (*get_frame_pc) (baton);
21928 size_t size;
21929
21930 fill_in_loclist_baton (cu, &loclist_baton, attr);
21931
21932 retval.data = dwarf2_find_location_expression (&loclist_baton,
21933 &size, pc);
21934 retval.size = size;
21935 }
21936 else
21937 {
21938 if (!attr->form_is_block ())
21939 error (_("Dwarf Error: DIE at %s referenced in module %s "
21940 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
21941 sect_offset_str (sect_off), objfile_name (objfile));
21942
21943 retval.data = DW_BLOCK (attr)->data;
21944 retval.size = DW_BLOCK (attr)->size;
21945 }
21946 retval.per_cu = cu->per_cu;
21947
21948 age_cached_comp_units (dwarf2_per_objfile);
21949
21950 return retval;
21951 }
21952
21953 /* See read.h. */
21954
21955 struct dwarf2_locexpr_baton
21956 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
21957 dwarf2_per_cu_data *per_cu,
21958 CORE_ADDR (*get_frame_pc) (void *baton),
21959 void *baton)
21960 {
21961 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
21962
21963 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
21964 }
21965
21966 /* Write a constant of a given type as target-ordered bytes into
21967 OBSTACK. */
21968
21969 static const gdb_byte *
21970 write_constant_as_bytes (struct obstack *obstack,
21971 enum bfd_endian byte_order,
21972 struct type *type,
21973 ULONGEST value,
21974 LONGEST *len)
21975 {
21976 gdb_byte *result;
21977
21978 *len = TYPE_LENGTH (type);
21979 result = (gdb_byte *) obstack_alloc (obstack, *len);
21980 store_unsigned_integer (result, *len, byte_order, value);
21981
21982 return result;
21983 }
21984
21985 /* See read.h. */
21986
21987 const gdb_byte *
21988 dwarf2_fetch_constant_bytes (sect_offset sect_off,
21989 dwarf2_per_cu_data *per_cu,
21990 obstack *obstack,
21991 LONGEST *len)
21992 {
21993 struct dwarf2_cu *cu;
21994 struct die_info *die;
21995 struct attribute *attr;
21996 const gdb_byte *result = NULL;
21997 struct type *type;
21998 LONGEST value;
21999 enum bfd_endian byte_order;
22000 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22001
22002 if (per_cu->cu == NULL)
22003 load_cu (per_cu, false);
22004 cu = per_cu->cu;
22005 if (cu == NULL)
22006 {
22007 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22008 Instead just throw an error, not much else we can do. */
22009 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22010 sect_offset_str (sect_off), objfile_name (objfile));
22011 }
22012
22013 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22014 if (!die)
22015 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22016 sect_offset_str (sect_off), objfile_name (objfile));
22017
22018 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22019 if (attr == NULL)
22020 return NULL;
22021
22022 byte_order = (bfd_big_endian (objfile->obfd)
22023 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22024
22025 switch (attr->form)
22026 {
22027 case DW_FORM_addr:
22028 case DW_FORM_addrx:
22029 case DW_FORM_GNU_addr_index:
22030 {
22031 gdb_byte *tem;
22032
22033 *len = cu->header.addr_size;
22034 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22035 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22036 result = tem;
22037 }
22038 break;
22039 case DW_FORM_string:
22040 case DW_FORM_strp:
22041 case DW_FORM_strx:
22042 case DW_FORM_GNU_str_index:
22043 case DW_FORM_GNU_strp_alt:
22044 /* DW_STRING is already allocated on the objfile obstack, point
22045 directly to it. */
22046 result = (const gdb_byte *) DW_STRING (attr);
22047 *len = strlen (DW_STRING (attr));
22048 break;
22049 case DW_FORM_block1:
22050 case DW_FORM_block2:
22051 case DW_FORM_block4:
22052 case DW_FORM_block:
22053 case DW_FORM_exprloc:
22054 case DW_FORM_data16:
22055 result = DW_BLOCK (attr)->data;
22056 *len = DW_BLOCK (attr)->size;
22057 break;
22058
22059 /* The DW_AT_const_value attributes are supposed to carry the
22060 symbol's value "represented as it would be on the target
22061 architecture." By the time we get here, it's already been
22062 converted to host endianness, so we just need to sign- or
22063 zero-extend it as appropriate. */
22064 case DW_FORM_data1:
22065 type = die_type (die, cu);
22066 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22067 if (result == NULL)
22068 result = write_constant_as_bytes (obstack, byte_order,
22069 type, value, len);
22070 break;
22071 case DW_FORM_data2:
22072 type = die_type (die, cu);
22073 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22074 if (result == NULL)
22075 result = write_constant_as_bytes (obstack, byte_order,
22076 type, value, len);
22077 break;
22078 case DW_FORM_data4:
22079 type = die_type (die, cu);
22080 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22081 if (result == NULL)
22082 result = write_constant_as_bytes (obstack, byte_order,
22083 type, value, len);
22084 break;
22085 case DW_FORM_data8:
22086 type = die_type (die, cu);
22087 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22088 if (result == NULL)
22089 result = write_constant_as_bytes (obstack, byte_order,
22090 type, value, len);
22091 break;
22092
22093 case DW_FORM_sdata:
22094 case DW_FORM_implicit_const:
22095 type = die_type (die, cu);
22096 result = write_constant_as_bytes (obstack, byte_order,
22097 type, DW_SND (attr), len);
22098 break;
22099
22100 case DW_FORM_udata:
22101 type = die_type (die, cu);
22102 result = write_constant_as_bytes (obstack, byte_order,
22103 type, DW_UNSND (attr), len);
22104 break;
22105
22106 default:
22107 complaint (_("unsupported const value attribute form: '%s'"),
22108 dwarf_form_name (attr->form));
22109 break;
22110 }
22111
22112 return result;
22113 }
22114
22115 /* See read.h. */
22116
22117 struct type *
22118 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22119 dwarf2_per_cu_data *per_cu)
22120 {
22121 struct dwarf2_cu *cu;
22122 struct die_info *die;
22123
22124 if (per_cu->cu == NULL)
22125 load_cu (per_cu, false);
22126 cu = per_cu->cu;
22127 if (!cu)
22128 return NULL;
22129
22130 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22131 if (!die)
22132 return NULL;
22133
22134 return die_type (die, cu);
22135 }
22136
22137 /* See read.h. */
22138
22139 struct type *
22140 dwarf2_get_die_type (cu_offset die_offset,
22141 struct dwarf2_per_cu_data *per_cu)
22142 {
22143 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22144 return get_die_type_at_offset (die_offset_sect, per_cu);
22145 }
22146
22147 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22148 On entry *REF_CU is the CU of SRC_DIE.
22149 On exit *REF_CU is the CU of the result.
22150 Returns NULL if the referenced DIE isn't found. */
22151
22152 static struct die_info *
22153 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22154 struct dwarf2_cu **ref_cu)
22155 {
22156 struct die_info temp_die;
22157 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22158 struct die_info *die;
22159
22160 /* While it might be nice to assert sig_type->type == NULL here,
22161 we can get here for DW_AT_imported_declaration where we need
22162 the DIE not the type. */
22163
22164 /* If necessary, add it to the queue and load its DIEs. */
22165
22166 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22167 read_signatured_type (sig_type);
22168
22169 sig_cu = sig_type->per_cu.cu;
22170 gdb_assert (sig_cu != NULL);
22171 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22172 temp_die.sect_off = sig_type->type_offset_in_section;
22173 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22174 to_underlying (temp_die.sect_off));
22175 if (die)
22176 {
22177 struct dwarf2_per_objfile *dwarf2_per_objfile
22178 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22179
22180 /* For .gdb_index version 7 keep track of included TUs.
22181 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22182 if (dwarf2_per_objfile->index_table != NULL
22183 && dwarf2_per_objfile->index_table->version <= 7)
22184 {
22185 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22186 }
22187
22188 *ref_cu = sig_cu;
22189 if (sig_cu != cu)
22190 sig_cu->ancestor = cu;
22191
22192 return die;
22193 }
22194
22195 return NULL;
22196 }
22197
22198 /* Follow signatured type referenced by ATTR in SRC_DIE.
22199 On entry *REF_CU is the CU of SRC_DIE.
22200 On exit *REF_CU is the CU of the result.
22201 The result is the DIE of the type.
22202 If the referenced type cannot be found an error is thrown. */
22203
22204 static struct die_info *
22205 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22206 struct dwarf2_cu **ref_cu)
22207 {
22208 ULONGEST signature = DW_SIGNATURE (attr);
22209 struct signatured_type *sig_type;
22210 struct die_info *die;
22211
22212 gdb_assert (attr->form == DW_FORM_ref_sig8);
22213
22214 sig_type = lookup_signatured_type (*ref_cu, signature);
22215 /* sig_type will be NULL if the signatured type is missing from
22216 the debug info. */
22217 if (sig_type == NULL)
22218 {
22219 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22220 " from DIE at %s [in module %s]"),
22221 hex_string (signature), sect_offset_str (src_die->sect_off),
22222 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22223 }
22224
22225 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22226 if (die == NULL)
22227 {
22228 dump_die_for_error (src_die);
22229 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22230 " from DIE at %s [in module %s]"),
22231 hex_string (signature), sect_offset_str (src_die->sect_off),
22232 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22233 }
22234
22235 return die;
22236 }
22237
22238 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22239 reading in and processing the type unit if necessary. */
22240
22241 static struct type *
22242 get_signatured_type (struct die_info *die, ULONGEST signature,
22243 struct dwarf2_cu *cu)
22244 {
22245 struct dwarf2_per_objfile *dwarf2_per_objfile
22246 = cu->per_cu->dwarf2_per_objfile;
22247 struct signatured_type *sig_type;
22248 struct dwarf2_cu *type_cu;
22249 struct die_info *type_die;
22250 struct type *type;
22251
22252 sig_type = lookup_signatured_type (cu, signature);
22253 /* sig_type will be NULL if the signatured type is missing from
22254 the debug info. */
22255 if (sig_type == NULL)
22256 {
22257 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22258 " from DIE at %s [in module %s]"),
22259 hex_string (signature), sect_offset_str (die->sect_off),
22260 objfile_name (dwarf2_per_objfile->objfile));
22261 return build_error_marker_type (cu, die);
22262 }
22263
22264 /* If we already know the type we're done. */
22265 if (sig_type->type != NULL)
22266 return sig_type->type;
22267
22268 type_cu = cu;
22269 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22270 if (type_die != NULL)
22271 {
22272 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22273 is created. This is important, for example, because for c++ classes
22274 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22275 type = read_type_die (type_die, type_cu);
22276 if (type == NULL)
22277 {
22278 complaint (_("Dwarf Error: Cannot build signatured type %s"
22279 " referenced from DIE at %s [in module %s]"),
22280 hex_string (signature), sect_offset_str (die->sect_off),
22281 objfile_name (dwarf2_per_objfile->objfile));
22282 type = build_error_marker_type (cu, die);
22283 }
22284 }
22285 else
22286 {
22287 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22288 " from DIE at %s [in module %s]"),
22289 hex_string (signature), sect_offset_str (die->sect_off),
22290 objfile_name (dwarf2_per_objfile->objfile));
22291 type = build_error_marker_type (cu, die);
22292 }
22293 sig_type->type = type;
22294
22295 return type;
22296 }
22297
22298 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22299 reading in and processing the type unit if necessary. */
22300
22301 static struct type *
22302 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22303 struct dwarf2_cu *cu) /* ARI: editCase function */
22304 {
22305 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22306 if (attr->form_is_ref ())
22307 {
22308 struct dwarf2_cu *type_cu = cu;
22309 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22310
22311 return read_type_die (type_die, type_cu);
22312 }
22313 else if (attr->form == DW_FORM_ref_sig8)
22314 {
22315 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22316 }
22317 else
22318 {
22319 struct dwarf2_per_objfile *dwarf2_per_objfile
22320 = cu->per_cu->dwarf2_per_objfile;
22321
22322 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22323 " at %s [in module %s]"),
22324 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22325 objfile_name (dwarf2_per_objfile->objfile));
22326 return build_error_marker_type (cu, die);
22327 }
22328 }
22329
22330 /* Load the DIEs associated with type unit PER_CU into memory. */
22331
22332 static void
22333 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22334 {
22335 struct signatured_type *sig_type;
22336
22337 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22338 gdb_assert (! per_cu->type_unit_group_p ());
22339
22340 /* We have the per_cu, but we need the signatured_type.
22341 Fortunately this is an easy translation. */
22342 gdb_assert (per_cu->is_debug_types);
22343 sig_type = (struct signatured_type *) per_cu;
22344
22345 gdb_assert (per_cu->cu == NULL);
22346
22347 read_signatured_type (sig_type);
22348
22349 gdb_assert (per_cu->cu != NULL);
22350 }
22351
22352 /* Read in a signatured type and build its CU and DIEs.
22353 If the type is a stub for the real type in a DWO file,
22354 read in the real type from the DWO file as well. */
22355
22356 static void
22357 read_signatured_type (struct signatured_type *sig_type)
22358 {
22359 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22360
22361 gdb_assert (per_cu->is_debug_types);
22362 gdb_assert (per_cu->cu == NULL);
22363
22364 cutu_reader reader (per_cu, NULL, 0, false);
22365
22366 if (!reader.dummy_p)
22367 {
22368 struct dwarf2_cu *cu = reader.cu;
22369 const gdb_byte *info_ptr = reader.info_ptr;
22370
22371 gdb_assert (cu->die_hash == NULL);
22372 cu->die_hash =
22373 htab_create_alloc_ex (cu->header.length / 12,
22374 die_hash,
22375 die_eq,
22376 NULL,
22377 &cu->comp_unit_obstack,
22378 hashtab_obstack_allocate,
22379 dummy_obstack_deallocate);
22380
22381 if (reader.comp_unit_die->has_children)
22382 reader.comp_unit_die->child
22383 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22384 reader.comp_unit_die);
22385 cu->dies = reader.comp_unit_die;
22386 /* comp_unit_die is not stored in die_hash, no need. */
22387
22388 /* We try not to read any attributes in this function, because
22389 not all CUs needed for references have been loaded yet, and
22390 symbol table processing isn't initialized. But we have to
22391 set the CU language, or we won't be able to build types
22392 correctly. Similarly, if we do not read the producer, we can
22393 not apply producer-specific interpretation. */
22394 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22395
22396 reader.keep ();
22397 }
22398
22399 sig_type->per_cu.tu_read = 1;
22400 }
22401
22402 /* Decode simple location descriptions.
22403 Given a pointer to a dwarf block that defines a location, compute
22404 the location and return the value.
22405
22406 NOTE drow/2003-11-18: This function is called in two situations
22407 now: for the address of static or global variables (partial symbols
22408 only) and for offsets into structures which are expected to be
22409 (more or less) constant. The partial symbol case should go away,
22410 and only the constant case should remain. That will let this
22411 function complain more accurately. A few special modes are allowed
22412 without complaint for global variables (for instance, global
22413 register values and thread-local values).
22414
22415 A location description containing no operations indicates that the
22416 object is optimized out. The return value is 0 for that case.
22417 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22418 callers will only want a very basic result and this can become a
22419 complaint.
22420
22421 Note that stack[0] is unused except as a default error return. */
22422
22423 static CORE_ADDR
22424 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22425 {
22426 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22427 size_t i;
22428 size_t size = blk->size;
22429 const gdb_byte *data = blk->data;
22430 CORE_ADDR stack[64];
22431 int stacki;
22432 unsigned int bytes_read, unsnd;
22433 gdb_byte op;
22434
22435 i = 0;
22436 stacki = 0;
22437 stack[stacki] = 0;
22438 stack[++stacki] = 0;
22439
22440 while (i < size)
22441 {
22442 op = data[i++];
22443 switch (op)
22444 {
22445 case DW_OP_lit0:
22446 case DW_OP_lit1:
22447 case DW_OP_lit2:
22448 case DW_OP_lit3:
22449 case DW_OP_lit4:
22450 case DW_OP_lit5:
22451 case DW_OP_lit6:
22452 case DW_OP_lit7:
22453 case DW_OP_lit8:
22454 case DW_OP_lit9:
22455 case DW_OP_lit10:
22456 case DW_OP_lit11:
22457 case DW_OP_lit12:
22458 case DW_OP_lit13:
22459 case DW_OP_lit14:
22460 case DW_OP_lit15:
22461 case DW_OP_lit16:
22462 case DW_OP_lit17:
22463 case DW_OP_lit18:
22464 case DW_OP_lit19:
22465 case DW_OP_lit20:
22466 case DW_OP_lit21:
22467 case DW_OP_lit22:
22468 case DW_OP_lit23:
22469 case DW_OP_lit24:
22470 case DW_OP_lit25:
22471 case DW_OP_lit26:
22472 case DW_OP_lit27:
22473 case DW_OP_lit28:
22474 case DW_OP_lit29:
22475 case DW_OP_lit30:
22476 case DW_OP_lit31:
22477 stack[++stacki] = op - DW_OP_lit0;
22478 break;
22479
22480 case DW_OP_reg0:
22481 case DW_OP_reg1:
22482 case DW_OP_reg2:
22483 case DW_OP_reg3:
22484 case DW_OP_reg4:
22485 case DW_OP_reg5:
22486 case DW_OP_reg6:
22487 case DW_OP_reg7:
22488 case DW_OP_reg8:
22489 case DW_OP_reg9:
22490 case DW_OP_reg10:
22491 case DW_OP_reg11:
22492 case DW_OP_reg12:
22493 case DW_OP_reg13:
22494 case DW_OP_reg14:
22495 case DW_OP_reg15:
22496 case DW_OP_reg16:
22497 case DW_OP_reg17:
22498 case DW_OP_reg18:
22499 case DW_OP_reg19:
22500 case DW_OP_reg20:
22501 case DW_OP_reg21:
22502 case DW_OP_reg22:
22503 case DW_OP_reg23:
22504 case DW_OP_reg24:
22505 case DW_OP_reg25:
22506 case DW_OP_reg26:
22507 case DW_OP_reg27:
22508 case DW_OP_reg28:
22509 case DW_OP_reg29:
22510 case DW_OP_reg30:
22511 case DW_OP_reg31:
22512 stack[++stacki] = op - DW_OP_reg0;
22513 if (i < size)
22514 dwarf2_complex_location_expr_complaint ();
22515 break;
22516
22517 case DW_OP_regx:
22518 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22519 i += bytes_read;
22520 stack[++stacki] = unsnd;
22521 if (i < size)
22522 dwarf2_complex_location_expr_complaint ();
22523 break;
22524
22525 case DW_OP_addr:
22526 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22527 &bytes_read);
22528 i += bytes_read;
22529 break;
22530
22531 case DW_OP_const1u:
22532 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22533 i += 1;
22534 break;
22535
22536 case DW_OP_const1s:
22537 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22538 i += 1;
22539 break;
22540
22541 case DW_OP_const2u:
22542 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22543 i += 2;
22544 break;
22545
22546 case DW_OP_const2s:
22547 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22548 i += 2;
22549 break;
22550
22551 case DW_OP_const4u:
22552 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22553 i += 4;
22554 break;
22555
22556 case DW_OP_const4s:
22557 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22558 i += 4;
22559 break;
22560
22561 case DW_OP_const8u:
22562 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22563 i += 8;
22564 break;
22565
22566 case DW_OP_constu:
22567 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22568 &bytes_read);
22569 i += bytes_read;
22570 break;
22571
22572 case DW_OP_consts:
22573 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22574 i += bytes_read;
22575 break;
22576
22577 case DW_OP_dup:
22578 stack[stacki + 1] = stack[stacki];
22579 stacki++;
22580 break;
22581
22582 case DW_OP_plus:
22583 stack[stacki - 1] += stack[stacki];
22584 stacki--;
22585 break;
22586
22587 case DW_OP_plus_uconst:
22588 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22589 &bytes_read);
22590 i += bytes_read;
22591 break;
22592
22593 case DW_OP_minus:
22594 stack[stacki - 1] -= stack[stacki];
22595 stacki--;
22596 break;
22597
22598 case DW_OP_deref:
22599 /* If we're not the last op, then we definitely can't encode
22600 this using GDB's address_class enum. This is valid for partial
22601 global symbols, although the variable's address will be bogus
22602 in the psymtab. */
22603 if (i < size)
22604 dwarf2_complex_location_expr_complaint ();
22605 break;
22606
22607 case DW_OP_GNU_push_tls_address:
22608 case DW_OP_form_tls_address:
22609 /* The top of the stack has the offset from the beginning
22610 of the thread control block at which the variable is located. */
22611 /* Nothing should follow this operator, so the top of stack would
22612 be returned. */
22613 /* This is valid for partial global symbols, but the variable's
22614 address will be bogus in the psymtab. Make it always at least
22615 non-zero to not look as a variable garbage collected by linker
22616 which have DW_OP_addr 0. */
22617 if (i < size)
22618 dwarf2_complex_location_expr_complaint ();
22619 stack[stacki]++;
22620 break;
22621
22622 case DW_OP_GNU_uninit:
22623 break;
22624
22625 case DW_OP_addrx:
22626 case DW_OP_GNU_addr_index:
22627 case DW_OP_GNU_const_index:
22628 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22629 &bytes_read);
22630 i += bytes_read;
22631 break;
22632
22633 default:
22634 {
22635 const char *name = get_DW_OP_name (op);
22636
22637 if (name)
22638 complaint (_("unsupported stack op: '%s'"),
22639 name);
22640 else
22641 complaint (_("unsupported stack op: '%02x'"),
22642 op);
22643 }
22644
22645 return (stack[stacki]);
22646 }
22647
22648 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22649 outside of the allocated space. Also enforce minimum>0. */
22650 if (stacki >= ARRAY_SIZE (stack) - 1)
22651 {
22652 complaint (_("location description stack overflow"));
22653 return 0;
22654 }
22655
22656 if (stacki <= 0)
22657 {
22658 complaint (_("location description stack underflow"));
22659 return 0;
22660 }
22661 }
22662 return (stack[stacki]);
22663 }
22664
22665 /* memory allocation interface */
22666
22667 static struct dwarf_block *
22668 dwarf_alloc_block (struct dwarf2_cu *cu)
22669 {
22670 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
22671 }
22672
22673 static struct die_info *
22674 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
22675 {
22676 struct die_info *die;
22677 size_t size = sizeof (struct die_info);
22678
22679 if (num_attrs > 1)
22680 size += (num_attrs - 1) * sizeof (struct attribute);
22681
22682 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
22683 memset (die, 0, sizeof (struct die_info));
22684 return (die);
22685 }
22686
22687 \f
22688
22689 /* Macro support. */
22690
22691 /* An overload of dwarf_decode_macros that finds the correct section
22692 and ensures it is read in before calling the other overload. */
22693
22694 static void
22695 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22696 int section_is_gnu)
22697 {
22698 struct dwarf2_per_objfile *dwarf2_per_objfile
22699 = cu->per_cu->dwarf2_per_objfile;
22700 struct objfile *objfile = dwarf2_per_objfile->objfile;
22701 const struct line_header *lh = cu->line_header;
22702 unsigned int offset_size = cu->header.offset_size;
22703 struct dwarf2_section_info *section;
22704 const char *section_name;
22705
22706 if (cu->dwo_unit != nullptr)
22707 {
22708 if (section_is_gnu)
22709 {
22710 section = &cu->dwo_unit->dwo_file->sections.macro;
22711 section_name = ".debug_macro.dwo";
22712 }
22713 else
22714 {
22715 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22716 section_name = ".debug_macinfo.dwo";
22717 }
22718 }
22719 else
22720 {
22721 if (section_is_gnu)
22722 {
22723 section = &dwarf2_per_objfile->macro;
22724 section_name = ".debug_macro";
22725 }
22726 else
22727 {
22728 section = &dwarf2_per_objfile->macinfo;
22729 section_name = ".debug_macinfo";
22730 }
22731 }
22732
22733 section->read (objfile);
22734 if (section->buffer == nullptr)
22735 {
22736 complaint (_("missing %s section"), section_name);
22737 return;
22738 }
22739
22740 buildsym_compunit *builder = cu->get_builder ();
22741
22742 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
22743 offset_size, offset, section_is_gnu);
22744 }
22745
22746 /* Return the .debug_loc section to use for CU.
22747 For DWO files use .debug_loc.dwo. */
22748
22749 static struct dwarf2_section_info *
22750 cu_debug_loc_section (struct dwarf2_cu *cu)
22751 {
22752 struct dwarf2_per_objfile *dwarf2_per_objfile
22753 = cu->per_cu->dwarf2_per_objfile;
22754
22755 if (cu->dwo_unit)
22756 {
22757 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22758
22759 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22760 }
22761 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22762 : &dwarf2_per_objfile->loc);
22763 }
22764
22765 /* A helper function that fills in a dwarf2_loclist_baton. */
22766
22767 static void
22768 fill_in_loclist_baton (struct dwarf2_cu *cu,
22769 struct dwarf2_loclist_baton *baton,
22770 const struct attribute *attr)
22771 {
22772 struct dwarf2_per_objfile *dwarf2_per_objfile
22773 = cu->per_cu->dwarf2_per_objfile;
22774 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22775
22776 section->read (dwarf2_per_objfile->objfile);
22777
22778 baton->per_cu = cu->per_cu;
22779 gdb_assert (baton->per_cu);
22780 /* We don't know how long the location list is, but make sure we
22781 don't run off the edge of the section. */
22782 baton->size = section->size - DW_UNSND (attr);
22783 baton->data = section->buffer + DW_UNSND (attr);
22784 if (cu->base_address.has_value ())
22785 baton->base_address = *cu->base_address;
22786 else
22787 baton->base_address = 0;
22788 baton->from_dwo = cu->dwo_unit != NULL;
22789 }
22790
22791 static void
22792 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
22793 struct dwarf2_cu *cu, int is_block)
22794 {
22795 struct dwarf2_per_objfile *dwarf2_per_objfile
22796 = cu->per_cu->dwarf2_per_objfile;
22797 struct objfile *objfile = dwarf2_per_objfile->objfile;
22798 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22799
22800 if (attr->form_is_section_offset ()
22801 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
22802 the section. If so, fall through to the complaint in the
22803 other branch. */
22804 && DW_UNSND (attr) < section->get_size (objfile))
22805 {
22806 struct dwarf2_loclist_baton *baton;
22807
22808 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
22809
22810 fill_in_loclist_baton (cu, baton, attr);
22811
22812 if (!cu->base_address.has_value ())
22813 complaint (_("Location list used without "
22814 "specifying the CU base address."));
22815
22816 SYMBOL_ACLASS_INDEX (sym) = (is_block
22817 ? dwarf2_loclist_block_index
22818 : dwarf2_loclist_index);
22819 SYMBOL_LOCATION_BATON (sym) = baton;
22820 }
22821 else
22822 {
22823 struct dwarf2_locexpr_baton *baton;
22824
22825 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
22826 baton->per_cu = cu->per_cu;
22827 gdb_assert (baton->per_cu);
22828
22829 if (attr->form_is_block ())
22830 {
22831 /* Note that we're just copying the block's data pointer
22832 here, not the actual data. We're still pointing into the
22833 info_buffer for SYM's objfile; right now we never release
22834 that buffer, but when we do clean up properly this may
22835 need to change. */
22836 baton->size = DW_BLOCK (attr)->size;
22837 baton->data = DW_BLOCK (attr)->data;
22838 }
22839 else
22840 {
22841 dwarf2_invalid_attrib_class_complaint ("location description",
22842 sym->natural_name ());
22843 baton->size = 0;
22844 }
22845
22846 SYMBOL_ACLASS_INDEX (sym) = (is_block
22847 ? dwarf2_locexpr_block_index
22848 : dwarf2_locexpr_index);
22849 SYMBOL_LOCATION_BATON (sym) = baton;
22850 }
22851 }
22852
22853 /* See read.h. */
22854
22855 struct objfile *
22856 dwarf2_per_cu_data::objfile () const
22857 {
22858 struct objfile *objfile = dwarf2_per_objfile->objfile;
22859
22860 /* Return the master objfile, so that we can report and look up the
22861 correct file containing this variable. */
22862 if (objfile->separate_debug_objfile_backlink)
22863 objfile = objfile->separate_debug_objfile_backlink;
22864
22865 return objfile;
22866 }
22867
22868 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22869 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22870 CU_HEADERP first. */
22871
22872 static const struct comp_unit_head *
22873 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22874 const struct dwarf2_per_cu_data *per_cu)
22875 {
22876 const gdb_byte *info_ptr;
22877
22878 if (per_cu->cu)
22879 return &per_cu->cu->header;
22880
22881 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
22882
22883 memset (cu_headerp, 0, sizeof (*cu_headerp));
22884 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22885 rcuh_kind::COMPILE);
22886
22887 return cu_headerp;
22888 }
22889
22890 /* See read.h. */
22891
22892 int
22893 dwarf2_per_cu_data::addr_size () const
22894 {
22895 struct comp_unit_head cu_header_local;
22896 const struct comp_unit_head *cu_headerp;
22897
22898 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22899
22900 return cu_headerp->addr_size;
22901 }
22902
22903 /* See read.h. */
22904
22905 int
22906 dwarf2_per_cu_data::offset_size () const
22907 {
22908 struct comp_unit_head cu_header_local;
22909 const struct comp_unit_head *cu_headerp;
22910
22911 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22912
22913 return cu_headerp->offset_size;
22914 }
22915
22916 /* See read.h. */
22917
22918 int
22919 dwarf2_per_cu_data::ref_addr_size () const
22920 {
22921 struct comp_unit_head cu_header_local;
22922 const struct comp_unit_head *cu_headerp;
22923
22924 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22925
22926 if (cu_headerp->version == 2)
22927 return cu_headerp->addr_size;
22928 else
22929 return cu_headerp->offset_size;
22930 }
22931
22932 /* See read.h. */
22933
22934 CORE_ADDR
22935 dwarf2_per_cu_data::text_offset () const
22936 {
22937 struct objfile *objfile = dwarf2_per_objfile->objfile;
22938
22939 return objfile->text_section_offset ();
22940 }
22941
22942 /* See read.h. */
22943
22944 struct type *
22945 dwarf2_per_cu_data::addr_type () const
22946 {
22947 struct objfile *objfile = dwarf2_per_objfile->objfile;
22948 struct type *void_type = objfile_type (objfile)->builtin_void;
22949 struct type *addr_type = lookup_pointer_type (void_type);
22950 int addr_size = this->addr_size ();
22951
22952 if (TYPE_LENGTH (addr_type) == addr_size)
22953 return addr_type;
22954
22955 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
22956 return addr_type;
22957 }
22958
22959 /* A helper function for dwarf2_find_containing_comp_unit that returns
22960 the index of the result, and that searches a vector. It will
22961 return a result even if the offset in question does not actually
22962 occur in any CU. This is separate so that it can be unit
22963 tested. */
22964
22965 static int
22966 dwarf2_find_containing_comp_unit
22967 (sect_offset sect_off,
22968 unsigned int offset_in_dwz,
22969 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
22970 {
22971 int low, high;
22972
22973 low = 0;
22974 high = all_comp_units.size () - 1;
22975 while (high > low)
22976 {
22977 struct dwarf2_per_cu_data *mid_cu;
22978 int mid = low + (high - low) / 2;
22979
22980 mid_cu = all_comp_units[mid];
22981 if (mid_cu->is_dwz > offset_in_dwz
22982 || (mid_cu->is_dwz == offset_in_dwz
22983 && mid_cu->sect_off + mid_cu->length > sect_off))
22984 high = mid;
22985 else
22986 low = mid + 1;
22987 }
22988 gdb_assert (low == high);
22989 return low;
22990 }
22991
22992 /* Locate the .debug_info compilation unit from CU's objfile which contains
22993 the DIE at OFFSET. Raises an error on failure. */
22994
22995 static struct dwarf2_per_cu_data *
22996 dwarf2_find_containing_comp_unit (sect_offset sect_off,
22997 unsigned int offset_in_dwz,
22998 struct dwarf2_per_objfile *dwarf2_per_objfile)
22999 {
23000 int low
23001 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23002 dwarf2_per_objfile->all_comp_units);
23003 struct dwarf2_per_cu_data *this_cu
23004 = dwarf2_per_objfile->all_comp_units[low];
23005
23006 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23007 {
23008 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23009 error (_("Dwarf Error: could not find partial DIE containing "
23010 "offset %s [in module %s]"),
23011 sect_offset_str (sect_off),
23012 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23013
23014 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
23015 <= sect_off);
23016 return dwarf2_per_objfile->all_comp_units[low-1];
23017 }
23018 else
23019 {
23020 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
23021 && sect_off >= this_cu->sect_off + this_cu->length)
23022 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23023 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23024 return this_cu;
23025 }
23026 }
23027
23028 #if GDB_SELF_TEST
23029
23030 namespace selftests {
23031 namespace find_containing_comp_unit {
23032
23033 static void
23034 run_test ()
23035 {
23036 struct dwarf2_per_cu_data one {};
23037 struct dwarf2_per_cu_data two {};
23038 struct dwarf2_per_cu_data three {};
23039 struct dwarf2_per_cu_data four {};
23040
23041 one.length = 5;
23042 two.sect_off = sect_offset (one.length);
23043 two.length = 7;
23044
23045 three.length = 5;
23046 three.is_dwz = 1;
23047 four.sect_off = sect_offset (three.length);
23048 four.length = 7;
23049 four.is_dwz = 1;
23050
23051 std::vector<dwarf2_per_cu_data *> units;
23052 units.push_back (&one);
23053 units.push_back (&two);
23054 units.push_back (&three);
23055 units.push_back (&four);
23056
23057 int result;
23058
23059 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23060 SELF_CHECK (units[result] == &one);
23061 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23062 SELF_CHECK (units[result] == &one);
23063 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23064 SELF_CHECK (units[result] == &two);
23065
23066 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23067 SELF_CHECK (units[result] == &three);
23068 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23069 SELF_CHECK (units[result] == &three);
23070 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23071 SELF_CHECK (units[result] == &four);
23072 }
23073
23074 }
23075 }
23076
23077 #endif /* GDB_SELF_TEST */
23078
23079 /* Initialize dwarf2_cu CU, owned by PER_CU. */
23080
23081 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
23082 : per_cu (per_cu_),
23083 mark (false),
23084 has_loclist (false),
23085 checked_producer (false),
23086 producer_is_gxx_lt_4_6 (false),
23087 producer_is_gcc_lt_4_3 (false),
23088 producer_is_icc (false),
23089 producer_is_icc_lt_14 (false),
23090 producer_is_codewarrior (false),
23091 processing_has_namespace_info (false)
23092 {
23093 per_cu->cu = this;
23094 }
23095
23096 /* Destroy a dwarf2_cu. */
23097
23098 dwarf2_cu::~dwarf2_cu ()
23099 {
23100 per_cu->cu = NULL;
23101 }
23102
23103 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23104
23105 static void
23106 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23107 enum language pretend_language)
23108 {
23109 struct attribute *attr;
23110
23111 /* Set the language we're debugging. */
23112 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23113 if (attr != nullptr)
23114 set_cu_language (DW_UNSND (attr), cu);
23115 else
23116 {
23117 cu->language = pretend_language;
23118 cu->language_defn = language_def (cu->language);
23119 }
23120
23121 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23122 }
23123
23124 /* Increase the age counter on each cached compilation unit, and free
23125 any that are too old. */
23126
23127 static void
23128 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23129 {
23130 struct dwarf2_per_cu_data *per_cu, **last_chain;
23131
23132 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23133 per_cu = dwarf2_per_objfile->read_in_chain;
23134 while (per_cu != NULL)
23135 {
23136 per_cu->cu->last_used ++;
23137 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23138 dwarf2_mark (per_cu->cu);
23139 per_cu = per_cu->cu->read_in_chain;
23140 }
23141
23142 per_cu = dwarf2_per_objfile->read_in_chain;
23143 last_chain = &dwarf2_per_objfile->read_in_chain;
23144 while (per_cu != NULL)
23145 {
23146 struct dwarf2_per_cu_data *next_cu;
23147
23148 next_cu = per_cu->cu->read_in_chain;
23149
23150 if (!per_cu->cu->mark)
23151 {
23152 delete per_cu->cu;
23153 *last_chain = next_cu;
23154 }
23155 else
23156 last_chain = &per_cu->cu->read_in_chain;
23157
23158 per_cu = next_cu;
23159 }
23160 }
23161
23162 /* Remove a single compilation unit from the cache. */
23163
23164 static void
23165 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23166 {
23167 struct dwarf2_per_cu_data *per_cu, **last_chain;
23168 struct dwarf2_per_objfile *dwarf2_per_objfile
23169 = target_per_cu->dwarf2_per_objfile;
23170
23171 per_cu = dwarf2_per_objfile->read_in_chain;
23172 last_chain = &dwarf2_per_objfile->read_in_chain;
23173 while (per_cu != NULL)
23174 {
23175 struct dwarf2_per_cu_data *next_cu;
23176
23177 next_cu = per_cu->cu->read_in_chain;
23178
23179 if (per_cu == target_per_cu)
23180 {
23181 delete per_cu->cu;
23182 per_cu->cu = NULL;
23183 *last_chain = next_cu;
23184 break;
23185 }
23186 else
23187 last_chain = &per_cu->cu->read_in_chain;
23188
23189 per_cu = next_cu;
23190 }
23191 }
23192
23193 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23194 We store these in a hash table separate from the DIEs, and preserve them
23195 when the DIEs are flushed out of cache.
23196
23197 The CU "per_cu" pointer is needed because offset alone is not enough to
23198 uniquely identify the type. A file may have multiple .debug_types sections,
23199 or the type may come from a DWO file. Furthermore, while it's more logical
23200 to use per_cu->section+offset, with Fission the section with the data is in
23201 the DWO file but we don't know that section at the point we need it.
23202 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23203 because we can enter the lookup routine, get_die_type_at_offset, from
23204 outside this file, and thus won't necessarily have PER_CU->cu.
23205 Fortunately, PER_CU is stable for the life of the objfile. */
23206
23207 struct dwarf2_per_cu_offset_and_type
23208 {
23209 const struct dwarf2_per_cu_data *per_cu;
23210 sect_offset sect_off;
23211 struct type *type;
23212 };
23213
23214 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23215
23216 static hashval_t
23217 per_cu_offset_and_type_hash (const void *item)
23218 {
23219 const struct dwarf2_per_cu_offset_and_type *ofs
23220 = (const struct dwarf2_per_cu_offset_and_type *) item;
23221
23222 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23223 }
23224
23225 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23226
23227 static int
23228 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23229 {
23230 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23231 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23232 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23233 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23234
23235 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23236 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23237 }
23238
23239 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23240 table if necessary. For convenience, return TYPE.
23241
23242 The DIEs reading must have careful ordering to:
23243 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23244 reading current DIE.
23245 * Not trying to dereference contents of still incompletely read in types
23246 while reading in other DIEs.
23247 * Enable referencing still incompletely read in types just by a pointer to
23248 the type without accessing its fields.
23249
23250 Therefore caller should follow these rules:
23251 * Try to fetch any prerequisite types we may need to build this DIE type
23252 before building the type and calling set_die_type.
23253 * After building type call set_die_type for current DIE as soon as
23254 possible before fetching more types to complete the current type.
23255 * Make the type as complete as possible before fetching more types. */
23256
23257 static struct type *
23258 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23259 {
23260 struct dwarf2_per_objfile *dwarf2_per_objfile
23261 = cu->per_cu->dwarf2_per_objfile;
23262 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23263 struct objfile *objfile = dwarf2_per_objfile->objfile;
23264 struct attribute *attr;
23265 struct dynamic_prop prop;
23266
23267 /* For Ada types, make sure that the gnat-specific data is always
23268 initialized (if not already set). There are a few types where
23269 we should not be doing so, because the type-specific area is
23270 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23271 where the type-specific area is used to store the floatformat).
23272 But this is not a problem, because the gnat-specific information
23273 is actually not needed for these types. */
23274 if (need_gnat_info (cu)
23275 && TYPE_CODE (type) != TYPE_CODE_FUNC
23276 && TYPE_CODE (type) != TYPE_CODE_FLT
23277 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23278 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23279 && TYPE_CODE (type) != TYPE_CODE_METHOD
23280 && !HAVE_GNAT_AUX_INFO (type))
23281 INIT_GNAT_SPECIFIC (type);
23282
23283 /* Read DW_AT_allocated and set in type. */
23284 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23285 if (attr != NULL && attr->form_is_block ())
23286 {
23287 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23288 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23289 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
23290 }
23291 else if (attr != NULL)
23292 {
23293 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23294 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23295 sect_offset_str (die->sect_off));
23296 }
23297
23298 /* Read DW_AT_associated and set in type. */
23299 attr = dwarf2_attr (die, DW_AT_associated, cu);
23300 if (attr != NULL && attr->form_is_block ())
23301 {
23302 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23303 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23304 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
23305 }
23306 else if (attr != NULL)
23307 {
23308 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23309 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23310 sect_offset_str (die->sect_off));
23311 }
23312
23313 /* Read DW_AT_data_location and set in type. */
23314 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23315 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23316 cu->per_cu->addr_type ()))
23317 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
23318
23319 if (dwarf2_per_objfile->die_type_hash == NULL)
23320 dwarf2_per_objfile->die_type_hash
23321 = htab_up (htab_create_alloc (127,
23322 per_cu_offset_and_type_hash,
23323 per_cu_offset_and_type_eq,
23324 NULL, xcalloc, xfree));
23325
23326 ofs.per_cu = cu->per_cu;
23327 ofs.sect_off = die->sect_off;
23328 ofs.type = type;
23329 slot = (struct dwarf2_per_cu_offset_and_type **)
23330 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23331 if (*slot)
23332 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23333 sect_offset_str (die->sect_off));
23334 *slot = XOBNEW (&objfile->objfile_obstack,
23335 struct dwarf2_per_cu_offset_and_type);
23336 **slot = ofs;
23337 return type;
23338 }
23339
23340 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23341 or return NULL if the die does not have a saved type. */
23342
23343 static struct type *
23344 get_die_type_at_offset (sect_offset sect_off,
23345 struct dwarf2_per_cu_data *per_cu)
23346 {
23347 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23348 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23349
23350 if (dwarf2_per_objfile->die_type_hash == NULL)
23351 return NULL;
23352
23353 ofs.per_cu = per_cu;
23354 ofs.sect_off = sect_off;
23355 slot = ((struct dwarf2_per_cu_offset_and_type *)
23356 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23357 if (slot)
23358 return slot->type;
23359 else
23360 return NULL;
23361 }
23362
23363 /* Look up the type for DIE in CU in die_type_hash,
23364 or return NULL if DIE does not have a saved type. */
23365
23366 static struct type *
23367 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23368 {
23369 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23370 }
23371
23372 /* Add a dependence relationship from CU to REF_PER_CU. */
23373
23374 static void
23375 dwarf2_add_dependence (struct dwarf2_cu *cu,
23376 struct dwarf2_per_cu_data *ref_per_cu)
23377 {
23378 void **slot;
23379
23380 if (cu->dependencies == NULL)
23381 cu->dependencies
23382 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23383 NULL, &cu->comp_unit_obstack,
23384 hashtab_obstack_allocate,
23385 dummy_obstack_deallocate);
23386
23387 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23388 if (*slot == NULL)
23389 *slot = ref_per_cu;
23390 }
23391
23392 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23393 Set the mark field in every compilation unit in the
23394 cache that we must keep because we are keeping CU. */
23395
23396 static int
23397 dwarf2_mark_helper (void **slot, void *data)
23398 {
23399 struct dwarf2_per_cu_data *per_cu;
23400
23401 per_cu = (struct dwarf2_per_cu_data *) *slot;
23402
23403 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23404 reading of the chain. As such dependencies remain valid it is not much
23405 useful to track and undo them during QUIT cleanups. */
23406 if (per_cu->cu == NULL)
23407 return 1;
23408
23409 if (per_cu->cu->mark)
23410 return 1;
23411 per_cu->cu->mark = true;
23412
23413 if (per_cu->cu->dependencies != NULL)
23414 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23415
23416 return 1;
23417 }
23418
23419 /* Set the mark field in CU and in every other compilation unit in the
23420 cache that we must keep because we are keeping CU. */
23421
23422 static void
23423 dwarf2_mark (struct dwarf2_cu *cu)
23424 {
23425 if (cu->mark)
23426 return;
23427 cu->mark = true;
23428 if (cu->dependencies != NULL)
23429 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23430 }
23431
23432 static void
23433 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23434 {
23435 while (per_cu)
23436 {
23437 per_cu->cu->mark = false;
23438 per_cu = per_cu->cu->read_in_chain;
23439 }
23440 }
23441
23442 /* Trivial hash function for partial_die_info: the hash value of a DIE
23443 is its offset in .debug_info for this objfile. */
23444
23445 static hashval_t
23446 partial_die_hash (const void *item)
23447 {
23448 const struct partial_die_info *part_die
23449 = (const struct partial_die_info *) item;
23450
23451 return to_underlying (part_die->sect_off);
23452 }
23453
23454 /* Trivial comparison function for partial_die_info structures: two DIEs
23455 are equal if they have the same offset. */
23456
23457 static int
23458 partial_die_eq (const void *item_lhs, const void *item_rhs)
23459 {
23460 const struct partial_die_info *part_die_lhs
23461 = (const struct partial_die_info *) item_lhs;
23462 const struct partial_die_info *part_die_rhs
23463 = (const struct partial_die_info *) item_rhs;
23464
23465 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23466 }
23467
23468 struct cmd_list_element *set_dwarf_cmdlist;
23469 struct cmd_list_element *show_dwarf_cmdlist;
23470
23471 static void
23472 set_dwarf_cmd (const char *args, int from_tty)
23473 {
23474 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23475 gdb_stdout);
23476 }
23477
23478 static void
23479 show_dwarf_cmd (const char *args, int from_tty)
23480 {
23481 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23482 }
23483
23484 static void
23485 show_check_physname (struct ui_file *file, int from_tty,
23486 struct cmd_list_element *c, const char *value)
23487 {
23488 fprintf_filtered (file,
23489 _("Whether to check \"physname\" is %s.\n"),
23490 value);
23491 }
23492
23493 void _initialize_dwarf2_read ();
23494 void
23495 _initialize_dwarf2_read ()
23496 {
23497 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23498 Set DWARF specific variables.\n\
23499 Configure DWARF variables such as the cache size."),
23500 &set_dwarf_cmdlist, "maintenance set dwarf ",
23501 0/*allow-unknown*/, &maintenance_set_cmdlist);
23502
23503 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
23504 Show DWARF specific variables.\n\
23505 Show DWARF variables such as the cache size."),
23506 &show_dwarf_cmdlist, "maintenance show dwarf ",
23507 0/*allow-unknown*/, &maintenance_show_cmdlist);
23508
23509 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23510 &dwarf_max_cache_age, _("\
23511 Set the upper bound on the age of cached DWARF compilation units."), _("\
23512 Show the upper bound on the age of cached DWARF compilation units."), _("\
23513 A higher limit means that cached compilation units will be stored\n\
23514 in memory longer, and more total memory will be used. Zero disables\n\
23515 caching, which can slow down startup."),
23516 NULL,
23517 show_dwarf_max_cache_age,
23518 &set_dwarf_cmdlist,
23519 &show_dwarf_cmdlist);
23520
23521 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23522 Set debugging of the DWARF reader."), _("\
23523 Show debugging of the DWARF reader."), _("\
23524 When enabled (non-zero), debugging messages are printed during DWARF\n\
23525 reading and symtab expansion. A value of 1 (one) provides basic\n\
23526 information. A value greater than 1 provides more verbose information."),
23527 NULL,
23528 NULL,
23529 &setdebuglist, &showdebuglist);
23530
23531 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23532 Set debugging of the DWARF DIE reader."), _("\
23533 Show debugging of the DWARF DIE reader."), _("\
23534 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23535 The value is the maximum depth to print."),
23536 NULL,
23537 NULL,
23538 &setdebuglist, &showdebuglist);
23539
23540 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23541 Set debugging of the dwarf line reader."), _("\
23542 Show debugging of the dwarf line reader."), _("\
23543 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23544 A value of 1 (one) provides basic information.\n\
23545 A value greater than 1 provides more verbose information."),
23546 NULL,
23547 NULL,
23548 &setdebuglist, &showdebuglist);
23549
23550 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23551 Set cross-checking of \"physname\" code against demangler."), _("\
23552 Show cross-checking of \"physname\" code against demangler."), _("\
23553 When enabled, GDB's internal \"physname\" code is checked against\n\
23554 the demangler."),
23555 NULL, show_check_physname,
23556 &setdebuglist, &showdebuglist);
23557
23558 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23559 no_class, &use_deprecated_index_sections, _("\
23560 Set whether to use deprecated gdb_index sections."), _("\
23561 Show whether to use deprecated gdb_index sections."), _("\
23562 When enabled, deprecated .gdb_index sections are used anyway.\n\
23563 Normally they are ignored either because of a missing feature or\n\
23564 performance issue.\n\
23565 Warning: This option must be enabled before gdb reads the file."),
23566 NULL,
23567 NULL,
23568 &setlist, &showlist);
23569
23570 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23571 &dwarf2_locexpr_funcs);
23572 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23573 &dwarf2_loclist_funcs);
23574
23575 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23576 &dwarf2_block_frame_base_locexpr_funcs);
23577 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23578 &dwarf2_block_frame_base_loclist_funcs);
23579
23580 #if GDB_SELF_TEST
23581 selftests::register_test ("dw2_expand_symtabs_matching",
23582 selftests::dw2_expand_symtabs_matching::run_test);
23583 selftests::register_test ("dwarf2_find_containing_comp_unit",
23584 selftests::find_containing_comp_unit::run_test);
23585 #endif
23586 }
This page took 0.554674 seconds and 5 git commands to generate.