]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
Rewrite the existing variant part code
[thirdparty/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 struct variant_part_builder;
1086
1087 /* When reading a variant, we track a bit more information about the
1088 field, and store it in an object of this type. */
1089
1090 struct variant_field
1091 {
1092 int first_field = -1;
1093 int last_field = -1;
1094
1095 /* A variant can contain other variant parts. */
1096 std::vector<variant_part_builder> variant_parts;
1097
1098 /* If we see a DW_TAG_variant, then this will be set if this is the
1099 default branch. */
1100 bool default_branch = false;
1101 /* If we see a DW_AT_discr_value, then this will be the discriminant
1102 value. */
1103 ULONGEST discriminant_value = 0;
1104 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1105 data. */
1106 struct dwarf_block *discr_list_data = nullptr;
1107 };
1108
1109 /* This represents a DW_TAG_variant_part. */
1110
1111 struct variant_part_builder
1112 {
1113 /* The offset of the discriminant field. */
1114 sect_offset discriminant_offset {};
1115
1116 /* Variants that are direct children of this variant part. */
1117 std::vector<variant_field> variants;
1118
1119 /* True if we're currently reading a variant. */
1120 bool processing_variant = false;
1121 };
1122
1123 struct nextfield
1124 {
1125 int accessibility = 0;
1126 int virtuality = 0;
1127 /* Variant parts need to find the discriminant, which is a DIE
1128 reference. We track the section offset of each field to make
1129 this link. */
1130 sect_offset offset;
1131 struct field field {};
1132 };
1133
1134 struct fnfieldlist
1135 {
1136 const char *name = nullptr;
1137 std::vector<struct fn_field> fnfields;
1138 };
1139
1140 /* The routines that read and process dies for a C struct or C++ class
1141 pass lists of data member fields and lists of member function fields
1142 in an instance of a field_info structure, as defined below. */
1143 struct field_info
1144 {
1145 /* List of data member and baseclasses fields. */
1146 std::vector<struct nextfield> fields;
1147 std::vector<struct nextfield> baseclasses;
1148
1149 /* Set if the accessibility of one of the fields is not public. */
1150 int non_public_fields = 0;
1151
1152 /* Member function fieldlist array, contains name of possibly overloaded
1153 member function, number of overloaded member functions and a pointer
1154 to the head of the member function field chain. */
1155 std::vector<struct fnfieldlist> fnfieldlists;
1156
1157 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1158 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1159 std::vector<struct decl_field> typedef_field_list;
1160
1161 /* Nested types defined by this class and the number of elements in this
1162 list. */
1163 std::vector<struct decl_field> nested_types_list;
1164
1165 /* If non-null, this is the variant part we are currently
1166 reading. */
1167 variant_part_builder *current_variant_part = nullptr;
1168 /* This holds all the top-level variant parts attached to the type
1169 we're reading. */
1170 std::vector<variant_part_builder> variant_parts;
1171
1172 /* Return the total number of fields (including baseclasses). */
1173 int nfields () const
1174 {
1175 return fields.size () + baseclasses.size ();
1176 }
1177 };
1178
1179 /* Loaded secondary compilation units are kept in memory until they
1180 have not been referenced for the processing of this many
1181 compilation units. Set this to zero to disable caching. Cache
1182 sizes of up to at least twenty will improve startup time for
1183 typical inter-CU-reference binaries, at an obvious memory cost. */
1184 static int dwarf_max_cache_age = 5;
1185 static void
1186 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1187 struct cmd_list_element *c, const char *value)
1188 {
1189 fprintf_filtered (file, _("The upper bound on the age of cached "
1190 "DWARF compilation units is %s.\n"),
1191 value);
1192 }
1193 \f
1194 /* local function prototypes */
1195
1196 static void dwarf2_find_base_address (struct die_info *die,
1197 struct dwarf2_cu *cu);
1198
1199 static dwarf2_psymtab *create_partial_symtab
1200 (struct dwarf2_per_cu_data *per_cu, const char *name);
1201
1202 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1203 const gdb_byte *info_ptr,
1204 struct die_info *type_unit_die);
1205
1206 static void dwarf2_build_psymtabs_hard
1207 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1208
1209 static void scan_partial_symbols (struct partial_die_info *,
1210 CORE_ADDR *, CORE_ADDR *,
1211 int, struct dwarf2_cu *);
1212
1213 static void add_partial_symbol (struct partial_die_info *,
1214 struct dwarf2_cu *);
1215
1216 static void add_partial_namespace (struct partial_die_info *pdi,
1217 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1218 int set_addrmap, struct dwarf2_cu *cu);
1219
1220 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1221 CORE_ADDR *highpc, int set_addrmap,
1222 struct dwarf2_cu *cu);
1223
1224 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1225 struct dwarf2_cu *cu);
1226
1227 static void add_partial_subprogram (struct partial_die_info *pdi,
1228 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1229 int need_pc, struct dwarf2_cu *cu);
1230
1231 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1232
1233 static struct partial_die_info *load_partial_dies
1234 (const struct die_reader_specs *, const gdb_byte *, int);
1235
1236 /* A pair of partial_die_info and compilation unit. */
1237 struct cu_partial_die_info
1238 {
1239 /* The compilation unit of the partial_die_info. */
1240 struct dwarf2_cu *cu;
1241 /* A partial_die_info. */
1242 struct partial_die_info *pdi;
1243
1244 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1245 : cu (cu),
1246 pdi (pdi)
1247 { /* Nothing. */ }
1248
1249 private:
1250 cu_partial_die_info () = delete;
1251 };
1252
1253 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1254 struct dwarf2_cu *);
1255
1256 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1257 struct attribute *, struct attr_abbrev *,
1258 const gdb_byte *, bool *need_reprocess);
1259
1260 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1261 struct attribute *attr);
1262
1263 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1264
1265 static sect_offset read_abbrev_offset
1266 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1267 struct dwarf2_section_info *, sect_offset);
1268
1269 static const char *read_indirect_string
1270 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1271 const struct comp_unit_head *, unsigned int *);
1272
1273 static const char *read_indirect_string_at_offset
1274 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1275
1276 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1277 const gdb_byte *,
1278 unsigned int *);
1279
1280 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1281 ULONGEST str_index);
1282
1283 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1284 ULONGEST str_index);
1285
1286 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1287
1288 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1289 struct dwarf2_cu *);
1290
1291 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1292 struct dwarf2_cu *cu);
1293
1294 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1295
1296 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1297 struct dwarf2_cu *cu);
1298
1299 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1300
1301 static struct die_info *die_specification (struct die_info *die,
1302 struct dwarf2_cu **);
1303
1304 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1305 struct dwarf2_cu *cu);
1306
1307 static void dwarf_decode_lines (struct line_header *, const char *,
1308 struct dwarf2_cu *, dwarf2_psymtab *,
1309 CORE_ADDR, int decode_mapping);
1310
1311 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1312 const char *);
1313
1314 static struct symbol *new_symbol (struct die_info *, struct type *,
1315 struct dwarf2_cu *, struct symbol * = NULL);
1316
1317 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1318 struct dwarf2_cu *);
1319
1320 static void dwarf2_const_value_attr (const struct attribute *attr,
1321 struct type *type,
1322 const char *name,
1323 struct obstack *obstack,
1324 struct dwarf2_cu *cu, LONGEST *value,
1325 const gdb_byte **bytes,
1326 struct dwarf2_locexpr_baton **baton);
1327
1328 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1329
1330 static int need_gnat_info (struct dwarf2_cu *);
1331
1332 static struct type *die_descriptive_type (struct die_info *,
1333 struct dwarf2_cu *);
1334
1335 static void set_descriptive_type (struct type *, struct die_info *,
1336 struct dwarf2_cu *);
1337
1338 static struct type *die_containing_type (struct die_info *,
1339 struct dwarf2_cu *);
1340
1341 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1342 struct dwarf2_cu *);
1343
1344 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1345
1346 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1347
1348 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1349
1350 static char *typename_concat (struct obstack *obs, const char *prefix,
1351 const char *suffix, int physname,
1352 struct dwarf2_cu *cu);
1353
1354 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1355
1356 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1357
1358 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1359
1360 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1361
1362 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1363
1364 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1365
1366 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1367 struct dwarf2_cu *, dwarf2_psymtab *);
1368
1369 /* Return the .debug_loclists section to use for cu. */
1370 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1371
1372 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1373 values. Keep the items ordered with increasing constraints compliance. */
1374 enum pc_bounds_kind
1375 {
1376 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1377 PC_BOUNDS_NOT_PRESENT,
1378
1379 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1380 were present but they do not form a valid range of PC addresses. */
1381 PC_BOUNDS_INVALID,
1382
1383 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1384 PC_BOUNDS_RANGES,
1385
1386 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1387 PC_BOUNDS_HIGH_LOW,
1388 };
1389
1390 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1391 CORE_ADDR *, CORE_ADDR *,
1392 struct dwarf2_cu *,
1393 dwarf2_psymtab *);
1394
1395 static void get_scope_pc_bounds (struct die_info *,
1396 CORE_ADDR *, CORE_ADDR *,
1397 struct dwarf2_cu *);
1398
1399 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1400 CORE_ADDR, struct dwarf2_cu *);
1401
1402 static void dwarf2_add_field (struct field_info *, struct die_info *,
1403 struct dwarf2_cu *);
1404
1405 static void dwarf2_attach_fields_to_type (struct field_info *,
1406 struct type *, struct dwarf2_cu *);
1407
1408 static void dwarf2_add_member_fn (struct field_info *,
1409 struct die_info *, struct type *,
1410 struct dwarf2_cu *);
1411
1412 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1413 struct type *,
1414 struct dwarf2_cu *);
1415
1416 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1417
1418 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1419
1420 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1421
1422 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1423
1424 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1425
1426 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1427
1428 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1429
1430 static struct type *read_module_type (struct die_info *die,
1431 struct dwarf2_cu *cu);
1432
1433 static const char *namespace_name (struct die_info *die,
1434 int *is_anonymous, struct dwarf2_cu *);
1435
1436 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1437
1438 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1439
1440 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1441 struct dwarf2_cu *);
1442
1443 static struct die_info *read_die_and_siblings_1
1444 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1445 struct die_info *);
1446
1447 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1448 const gdb_byte *info_ptr,
1449 const gdb_byte **new_info_ptr,
1450 struct die_info *parent);
1451
1452 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1453 struct die_info **, const gdb_byte *,
1454 int);
1455
1456 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1457 struct die_info **, const gdb_byte *);
1458
1459 static void process_die (struct die_info *, struct dwarf2_cu *);
1460
1461 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1462 struct objfile *);
1463
1464 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1465
1466 static const char *dwarf2_full_name (const char *name,
1467 struct die_info *die,
1468 struct dwarf2_cu *cu);
1469
1470 static const char *dwarf2_physname (const char *name, struct die_info *die,
1471 struct dwarf2_cu *cu);
1472
1473 static struct die_info *dwarf2_extension (struct die_info *die,
1474 struct dwarf2_cu **);
1475
1476 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1477
1478 static void dump_die_for_error (struct die_info *);
1479
1480 static void dump_die_1 (struct ui_file *, int level, int max_level,
1481 struct die_info *);
1482
1483 /*static*/ void dump_die (struct die_info *, int max_level);
1484
1485 static void store_in_ref_table (struct die_info *,
1486 struct dwarf2_cu *);
1487
1488 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1489 const struct attribute *,
1490 struct dwarf2_cu **);
1491
1492 static struct die_info *follow_die_ref (struct die_info *,
1493 const struct attribute *,
1494 struct dwarf2_cu **);
1495
1496 static struct die_info *follow_die_sig (struct die_info *,
1497 const struct attribute *,
1498 struct dwarf2_cu **);
1499
1500 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1501 struct dwarf2_cu *);
1502
1503 static struct type *get_DW_AT_signature_type (struct die_info *,
1504 const struct attribute *,
1505 struct dwarf2_cu *);
1506
1507 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1508
1509 static void read_signatured_type (struct signatured_type *);
1510
1511 static int attr_to_dynamic_prop (const struct attribute *attr,
1512 struct die_info *die, struct dwarf2_cu *cu,
1513 struct dynamic_prop *prop, struct type *type);
1514
1515 /* memory allocation interface */
1516
1517 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1518
1519 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1520
1521 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1522
1523 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1524 struct dwarf2_loclist_baton *baton,
1525 const struct attribute *attr);
1526
1527 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1528 struct symbol *sym,
1529 struct dwarf2_cu *cu,
1530 int is_block);
1531
1532 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1533 const gdb_byte *info_ptr,
1534 struct abbrev_info *abbrev);
1535
1536 static hashval_t partial_die_hash (const void *item);
1537
1538 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1539
1540 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1541 (sect_offset sect_off, unsigned int offset_in_dwz,
1542 struct dwarf2_per_objfile *dwarf2_per_objfile);
1543
1544 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1545 struct die_info *comp_unit_die,
1546 enum language pretend_language);
1547
1548 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1549
1550 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1551
1552 static struct type *set_die_type (struct die_info *, struct type *,
1553 struct dwarf2_cu *);
1554
1555 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1556
1557 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1558
1559 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1560 enum language);
1561
1562 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1563 enum language);
1564
1565 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1566 enum language);
1567
1568 static void dwarf2_add_dependence (struct dwarf2_cu *,
1569 struct dwarf2_per_cu_data *);
1570
1571 static void dwarf2_mark (struct dwarf2_cu *);
1572
1573 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1574
1575 static struct type *get_die_type_at_offset (sect_offset,
1576 struct dwarf2_per_cu_data *);
1577
1578 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1579
1580 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1581 enum language pretend_language);
1582
1583 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1584
1585 /* Class, the destructor of which frees all allocated queue entries. This
1586 will only have work to do if an error was thrown while processing the
1587 dwarf. If no error was thrown then the queue entries should have all
1588 been processed, and freed, as we went along. */
1589
1590 class dwarf2_queue_guard
1591 {
1592 public:
1593 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1594 : m_per_objfile (per_objfile)
1595 {
1596 }
1597
1598 /* Free any entries remaining on the queue. There should only be
1599 entries left if we hit an error while processing the dwarf. */
1600 ~dwarf2_queue_guard ()
1601 {
1602 /* Ensure that no memory is allocated by the queue. */
1603 std::queue<dwarf2_queue_item> empty;
1604 std::swap (m_per_objfile->queue, empty);
1605 }
1606
1607 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1608
1609 private:
1610 dwarf2_per_objfile *m_per_objfile;
1611 };
1612
1613 dwarf2_queue_item::~dwarf2_queue_item ()
1614 {
1615 /* Anything still marked queued is likely to be in an
1616 inconsistent state, so discard it. */
1617 if (per_cu->queued)
1618 {
1619 if (per_cu->cu != NULL)
1620 free_one_cached_comp_unit (per_cu);
1621 per_cu->queued = 0;
1622 }
1623 }
1624
1625 /* The return type of find_file_and_directory. Note, the enclosed
1626 string pointers are only valid while this object is valid. */
1627
1628 struct file_and_directory
1629 {
1630 /* The filename. This is never NULL. */
1631 const char *name;
1632
1633 /* The compilation directory. NULL if not known. If we needed to
1634 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1635 points directly to the DW_AT_comp_dir string attribute owned by
1636 the obstack that owns the DIE. */
1637 const char *comp_dir;
1638
1639 /* If we needed to build a new string for comp_dir, this is what
1640 owns the storage. */
1641 std::string comp_dir_storage;
1642 };
1643
1644 static file_and_directory find_file_and_directory (struct die_info *die,
1645 struct dwarf2_cu *cu);
1646
1647 static htab_up allocate_signatured_type_table ();
1648
1649 static htab_up allocate_dwo_unit_table ();
1650
1651 static struct dwo_unit *lookup_dwo_unit_in_dwp
1652 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1653 struct dwp_file *dwp_file, const char *comp_dir,
1654 ULONGEST signature, int is_debug_types);
1655
1656 static struct dwp_file *get_dwp_file
1657 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1658
1659 static struct dwo_unit *lookup_dwo_comp_unit
1660 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1661
1662 static struct dwo_unit *lookup_dwo_type_unit
1663 (struct signatured_type *, const char *, const char *);
1664
1665 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1666
1667 /* A unique pointer to a dwo_file. */
1668
1669 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1670
1671 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1672
1673 static void check_producer (struct dwarf2_cu *cu);
1674
1675 static void free_line_header_voidp (void *arg);
1676 \f
1677 /* Various complaints about symbol reading that don't abort the process. */
1678
1679 static void
1680 dwarf2_debug_line_missing_file_complaint (void)
1681 {
1682 complaint (_(".debug_line section has line data without a file"));
1683 }
1684
1685 static void
1686 dwarf2_debug_line_missing_end_sequence_complaint (void)
1687 {
1688 complaint (_(".debug_line section has line "
1689 "program sequence without an end"));
1690 }
1691
1692 static void
1693 dwarf2_complex_location_expr_complaint (void)
1694 {
1695 complaint (_("location expression too complex"));
1696 }
1697
1698 static void
1699 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1700 int arg3)
1701 {
1702 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1703 arg1, arg2, arg3);
1704 }
1705
1706 static void
1707 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1708 {
1709 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1710 arg1, arg2);
1711 }
1712
1713 /* Hash function for line_header_hash. */
1714
1715 static hashval_t
1716 line_header_hash (const struct line_header *ofs)
1717 {
1718 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1719 }
1720
1721 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1722
1723 static hashval_t
1724 line_header_hash_voidp (const void *item)
1725 {
1726 const struct line_header *ofs = (const struct line_header *) item;
1727
1728 return line_header_hash (ofs);
1729 }
1730
1731 /* Equality function for line_header_hash. */
1732
1733 static int
1734 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1735 {
1736 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1737 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1738
1739 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1740 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1741 }
1742
1743 \f
1744
1745 /* See declaration. */
1746
1747 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
1748 const dwarf2_debug_sections *names,
1749 bool can_copy_)
1750 : objfile (objfile_),
1751 can_copy (can_copy_)
1752 {
1753 if (names == NULL)
1754 names = &dwarf2_elf_names;
1755
1756 bfd *obfd = objfile->obfd;
1757
1758 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1759 locate_sections (obfd, sec, *names);
1760 }
1761
1762 dwarf2_per_objfile::~dwarf2_per_objfile ()
1763 {
1764 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1765 free_cached_comp_units ();
1766
1767 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1768 per_cu->imported_symtabs_free ();
1769
1770 for (signatured_type *sig_type : all_type_units)
1771 sig_type->per_cu.imported_symtabs_free ();
1772
1773 /* Everything else should be on the objfile obstack. */
1774 }
1775
1776 /* See declaration. */
1777
1778 void
1779 dwarf2_per_objfile::free_cached_comp_units ()
1780 {
1781 dwarf2_per_cu_data *per_cu = read_in_chain;
1782 dwarf2_per_cu_data **last_chain = &read_in_chain;
1783 while (per_cu != NULL)
1784 {
1785 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1786
1787 delete per_cu->cu;
1788 *last_chain = next_cu;
1789 per_cu = next_cu;
1790 }
1791 }
1792
1793 /* A helper class that calls free_cached_comp_units on
1794 destruction. */
1795
1796 class free_cached_comp_units
1797 {
1798 public:
1799
1800 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1801 : m_per_objfile (per_objfile)
1802 {
1803 }
1804
1805 ~free_cached_comp_units ()
1806 {
1807 m_per_objfile->free_cached_comp_units ();
1808 }
1809
1810 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1811
1812 private:
1813
1814 dwarf2_per_objfile *m_per_objfile;
1815 };
1816
1817 /* Try to locate the sections we need for DWARF 2 debugging
1818 information and return true if we have enough to do something.
1819 NAMES points to the dwarf2 section names, or is NULL if the standard
1820 ELF names are used. CAN_COPY is true for formats where symbol
1821 interposition is possible and so symbol values must follow copy
1822 relocation rules. */
1823
1824 int
1825 dwarf2_has_info (struct objfile *objfile,
1826 const struct dwarf2_debug_sections *names,
1827 bool can_copy)
1828 {
1829 if (objfile->flags & OBJF_READNEVER)
1830 return 0;
1831
1832 struct dwarf2_per_objfile *dwarf2_per_objfile
1833 = get_dwarf2_per_objfile (objfile);
1834
1835 if (dwarf2_per_objfile == NULL)
1836 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
1837 names,
1838 can_copy);
1839
1840 return (!dwarf2_per_objfile->info.is_virtual
1841 && dwarf2_per_objfile->info.s.section != NULL
1842 && !dwarf2_per_objfile->abbrev.is_virtual
1843 && dwarf2_per_objfile->abbrev.s.section != NULL);
1844 }
1845
1846 /* When loading sections, we look either for uncompressed section or for
1847 compressed section names. */
1848
1849 static int
1850 section_is_p (const char *section_name,
1851 const struct dwarf2_section_names *names)
1852 {
1853 if (names->normal != NULL
1854 && strcmp (section_name, names->normal) == 0)
1855 return 1;
1856 if (names->compressed != NULL
1857 && strcmp (section_name, names->compressed) == 0)
1858 return 1;
1859 return 0;
1860 }
1861
1862 /* See declaration. */
1863
1864 void
1865 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1866 const dwarf2_debug_sections &names)
1867 {
1868 flagword aflag = bfd_section_flags (sectp);
1869
1870 if ((aflag & SEC_HAS_CONTENTS) == 0)
1871 {
1872 }
1873 else if (elf_section_data (sectp)->this_hdr.sh_size
1874 > bfd_get_file_size (abfd))
1875 {
1876 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1877 warning (_("Discarding section %s which has a section size (%s"
1878 ") larger than the file size [in module %s]"),
1879 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1880 bfd_get_filename (abfd));
1881 }
1882 else if (section_is_p (sectp->name, &names.info))
1883 {
1884 this->info.s.section = sectp;
1885 this->info.size = bfd_section_size (sectp);
1886 }
1887 else if (section_is_p (sectp->name, &names.abbrev))
1888 {
1889 this->abbrev.s.section = sectp;
1890 this->abbrev.size = bfd_section_size (sectp);
1891 }
1892 else if (section_is_p (sectp->name, &names.line))
1893 {
1894 this->line.s.section = sectp;
1895 this->line.size = bfd_section_size (sectp);
1896 }
1897 else if (section_is_p (sectp->name, &names.loc))
1898 {
1899 this->loc.s.section = sectp;
1900 this->loc.size = bfd_section_size (sectp);
1901 }
1902 else if (section_is_p (sectp->name, &names.loclists))
1903 {
1904 this->loclists.s.section = sectp;
1905 this->loclists.size = bfd_section_size (sectp);
1906 }
1907 else if (section_is_p (sectp->name, &names.macinfo))
1908 {
1909 this->macinfo.s.section = sectp;
1910 this->macinfo.size = bfd_section_size (sectp);
1911 }
1912 else if (section_is_p (sectp->name, &names.macro))
1913 {
1914 this->macro.s.section = sectp;
1915 this->macro.size = bfd_section_size (sectp);
1916 }
1917 else if (section_is_p (sectp->name, &names.str))
1918 {
1919 this->str.s.section = sectp;
1920 this->str.size = bfd_section_size (sectp);
1921 }
1922 else if (section_is_p (sectp->name, &names.str_offsets))
1923 {
1924 this->str_offsets.s.section = sectp;
1925 this->str_offsets.size = bfd_section_size (sectp);
1926 }
1927 else if (section_is_p (sectp->name, &names.line_str))
1928 {
1929 this->line_str.s.section = sectp;
1930 this->line_str.size = bfd_section_size (sectp);
1931 }
1932 else if (section_is_p (sectp->name, &names.addr))
1933 {
1934 this->addr.s.section = sectp;
1935 this->addr.size = bfd_section_size (sectp);
1936 }
1937 else if (section_is_p (sectp->name, &names.frame))
1938 {
1939 this->frame.s.section = sectp;
1940 this->frame.size = bfd_section_size (sectp);
1941 }
1942 else if (section_is_p (sectp->name, &names.eh_frame))
1943 {
1944 this->eh_frame.s.section = sectp;
1945 this->eh_frame.size = bfd_section_size (sectp);
1946 }
1947 else if (section_is_p (sectp->name, &names.ranges))
1948 {
1949 this->ranges.s.section = sectp;
1950 this->ranges.size = bfd_section_size (sectp);
1951 }
1952 else if (section_is_p (sectp->name, &names.rnglists))
1953 {
1954 this->rnglists.s.section = sectp;
1955 this->rnglists.size = bfd_section_size (sectp);
1956 }
1957 else if (section_is_p (sectp->name, &names.types))
1958 {
1959 struct dwarf2_section_info type_section;
1960
1961 memset (&type_section, 0, sizeof (type_section));
1962 type_section.s.section = sectp;
1963 type_section.size = bfd_section_size (sectp);
1964
1965 this->types.push_back (type_section);
1966 }
1967 else if (section_is_p (sectp->name, &names.gdb_index))
1968 {
1969 this->gdb_index.s.section = sectp;
1970 this->gdb_index.size = bfd_section_size (sectp);
1971 }
1972 else if (section_is_p (sectp->name, &names.debug_names))
1973 {
1974 this->debug_names.s.section = sectp;
1975 this->debug_names.size = bfd_section_size (sectp);
1976 }
1977 else if (section_is_p (sectp->name, &names.debug_aranges))
1978 {
1979 this->debug_aranges.s.section = sectp;
1980 this->debug_aranges.size = bfd_section_size (sectp);
1981 }
1982
1983 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1984 && bfd_section_vma (sectp) == 0)
1985 this->has_section_at_zero = true;
1986 }
1987
1988 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1989 SECTION_NAME. */
1990
1991 void
1992 dwarf2_get_section_info (struct objfile *objfile,
1993 enum dwarf2_section_enum sect,
1994 asection **sectp, const gdb_byte **bufp,
1995 bfd_size_type *sizep)
1996 {
1997 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
1998 struct dwarf2_section_info *info;
1999
2000 /* We may see an objfile without any DWARF, in which case we just
2001 return nothing. */
2002 if (data == NULL)
2003 {
2004 *sectp = NULL;
2005 *bufp = NULL;
2006 *sizep = 0;
2007 return;
2008 }
2009 switch (sect)
2010 {
2011 case DWARF2_DEBUG_FRAME:
2012 info = &data->frame;
2013 break;
2014 case DWARF2_EH_FRAME:
2015 info = &data->eh_frame;
2016 break;
2017 default:
2018 gdb_assert_not_reached ("unexpected section");
2019 }
2020
2021 info->read (objfile);
2022
2023 *sectp = info->get_bfd_section ();
2024 *bufp = info->buffer;
2025 *sizep = info->size;
2026 }
2027
2028 /* A helper function to find the sections for a .dwz file. */
2029
2030 static void
2031 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2032 {
2033 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2034
2035 /* Note that we only support the standard ELF names, because .dwz
2036 is ELF-only (at the time of writing). */
2037 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2038 {
2039 dwz_file->abbrev.s.section = sectp;
2040 dwz_file->abbrev.size = bfd_section_size (sectp);
2041 }
2042 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2043 {
2044 dwz_file->info.s.section = sectp;
2045 dwz_file->info.size = bfd_section_size (sectp);
2046 }
2047 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2048 {
2049 dwz_file->str.s.section = sectp;
2050 dwz_file->str.size = bfd_section_size (sectp);
2051 }
2052 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2053 {
2054 dwz_file->line.s.section = sectp;
2055 dwz_file->line.size = bfd_section_size (sectp);
2056 }
2057 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2058 {
2059 dwz_file->macro.s.section = sectp;
2060 dwz_file->macro.size = bfd_section_size (sectp);
2061 }
2062 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2063 {
2064 dwz_file->gdb_index.s.section = sectp;
2065 dwz_file->gdb_index.size = bfd_section_size (sectp);
2066 }
2067 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2068 {
2069 dwz_file->debug_names.s.section = sectp;
2070 dwz_file->debug_names.size = bfd_section_size (sectp);
2071 }
2072 }
2073
2074 /* See dwarf2read.h. */
2075
2076 struct dwz_file *
2077 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2078 {
2079 const char *filename;
2080 bfd_size_type buildid_len_arg;
2081 size_t buildid_len;
2082 bfd_byte *buildid;
2083
2084 if (dwarf2_per_objfile->dwz_file != NULL)
2085 return dwarf2_per_objfile->dwz_file.get ();
2086
2087 bfd_set_error (bfd_error_no_error);
2088 gdb::unique_xmalloc_ptr<char> data
2089 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2090 &buildid_len_arg, &buildid));
2091 if (data == NULL)
2092 {
2093 if (bfd_get_error () == bfd_error_no_error)
2094 return NULL;
2095 error (_("could not read '.gnu_debugaltlink' section: %s"),
2096 bfd_errmsg (bfd_get_error ()));
2097 }
2098
2099 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2100
2101 buildid_len = (size_t) buildid_len_arg;
2102
2103 filename = data.get ();
2104
2105 std::string abs_storage;
2106 if (!IS_ABSOLUTE_PATH (filename))
2107 {
2108 gdb::unique_xmalloc_ptr<char> abs
2109 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2110
2111 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2112 filename = abs_storage.c_str ();
2113 }
2114
2115 /* First try the file name given in the section. If that doesn't
2116 work, try to use the build-id instead. */
2117 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2118 if (dwz_bfd != NULL)
2119 {
2120 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2121 dwz_bfd.reset (nullptr);
2122 }
2123
2124 if (dwz_bfd == NULL)
2125 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2126
2127 if (dwz_bfd == nullptr)
2128 {
2129 gdb::unique_xmalloc_ptr<char> alt_filename;
2130 const char *origname = dwarf2_per_objfile->objfile->original_name;
2131
2132 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2133 buildid_len,
2134 origname,
2135 &alt_filename));
2136
2137 if (fd.get () >= 0)
2138 {
2139 /* File successfully retrieved from server. */
2140 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2141
2142 if (dwz_bfd == nullptr)
2143 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2144 alt_filename.get ());
2145 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2146 dwz_bfd.reset (nullptr);
2147 }
2148 }
2149
2150 if (dwz_bfd == NULL)
2151 error (_("could not find '.gnu_debugaltlink' file for %s"),
2152 objfile_name (dwarf2_per_objfile->objfile));
2153
2154 std::unique_ptr<struct dwz_file> result
2155 (new struct dwz_file (std::move (dwz_bfd)));
2156
2157 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2158 result.get ());
2159
2160 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2161 result->dwz_bfd.get ());
2162 dwarf2_per_objfile->dwz_file = std::move (result);
2163 return dwarf2_per_objfile->dwz_file.get ();
2164 }
2165 \f
2166 /* DWARF quick_symbols_functions support. */
2167
2168 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2169 unique line tables, so we maintain a separate table of all .debug_line
2170 derived entries to support the sharing.
2171 All the quick functions need is the list of file names. We discard the
2172 line_header when we're done and don't need to record it here. */
2173 struct quick_file_names
2174 {
2175 /* The data used to construct the hash key. */
2176 struct stmt_list_hash hash;
2177
2178 /* The number of entries in file_names, real_names. */
2179 unsigned int num_file_names;
2180
2181 /* The file names from the line table, after being run through
2182 file_full_name. */
2183 const char **file_names;
2184
2185 /* The file names from the line table after being run through
2186 gdb_realpath. These are computed lazily. */
2187 const char **real_names;
2188 };
2189
2190 /* When using the index (and thus not using psymtabs), each CU has an
2191 object of this type. This is used to hold information needed by
2192 the various "quick" methods. */
2193 struct dwarf2_per_cu_quick_data
2194 {
2195 /* The file table. This can be NULL if there was no file table
2196 or it's currently not read in.
2197 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2198 struct quick_file_names *file_names;
2199
2200 /* The corresponding symbol table. This is NULL if symbols for this
2201 CU have not yet been read. */
2202 struct compunit_symtab *compunit_symtab;
2203
2204 /* A temporary mark bit used when iterating over all CUs in
2205 expand_symtabs_matching. */
2206 unsigned int mark : 1;
2207
2208 /* True if we've tried to read the file table and found there isn't one.
2209 There will be no point in trying to read it again next time. */
2210 unsigned int no_file_data : 1;
2211 };
2212
2213 /* Utility hash function for a stmt_list_hash. */
2214
2215 static hashval_t
2216 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2217 {
2218 hashval_t v = 0;
2219
2220 if (stmt_list_hash->dwo_unit != NULL)
2221 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2222 v += to_underlying (stmt_list_hash->line_sect_off);
2223 return v;
2224 }
2225
2226 /* Utility equality function for a stmt_list_hash. */
2227
2228 static int
2229 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2230 const struct stmt_list_hash *rhs)
2231 {
2232 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2233 return 0;
2234 if (lhs->dwo_unit != NULL
2235 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2236 return 0;
2237
2238 return lhs->line_sect_off == rhs->line_sect_off;
2239 }
2240
2241 /* Hash function for a quick_file_names. */
2242
2243 static hashval_t
2244 hash_file_name_entry (const void *e)
2245 {
2246 const struct quick_file_names *file_data
2247 = (const struct quick_file_names *) e;
2248
2249 return hash_stmt_list_entry (&file_data->hash);
2250 }
2251
2252 /* Equality function for a quick_file_names. */
2253
2254 static int
2255 eq_file_name_entry (const void *a, const void *b)
2256 {
2257 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2258 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2259
2260 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2261 }
2262
2263 /* Delete function for a quick_file_names. */
2264
2265 static void
2266 delete_file_name_entry (void *e)
2267 {
2268 struct quick_file_names *file_data = (struct quick_file_names *) e;
2269 int i;
2270
2271 for (i = 0; i < file_data->num_file_names; ++i)
2272 {
2273 xfree ((void*) file_data->file_names[i]);
2274 if (file_data->real_names)
2275 xfree ((void*) file_data->real_names[i]);
2276 }
2277
2278 /* The space for the struct itself lives on objfile_obstack,
2279 so we don't free it here. */
2280 }
2281
2282 /* Create a quick_file_names hash table. */
2283
2284 static htab_up
2285 create_quick_file_names_table (unsigned int nr_initial_entries)
2286 {
2287 return htab_up (htab_create_alloc (nr_initial_entries,
2288 hash_file_name_entry, eq_file_name_entry,
2289 delete_file_name_entry, xcalloc, xfree));
2290 }
2291
2292 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2293 have to be created afterwards. You should call age_cached_comp_units after
2294 processing PER_CU->CU. dw2_setup must have been already called. */
2295
2296 static void
2297 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2298 {
2299 if (per_cu->is_debug_types)
2300 load_full_type_unit (per_cu);
2301 else
2302 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2303
2304 if (per_cu->cu == NULL)
2305 return; /* Dummy CU. */
2306
2307 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2308 }
2309
2310 /* Read in the symbols for PER_CU. */
2311
2312 static void
2313 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2314 {
2315 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2316
2317 /* Skip type_unit_groups, reading the type units they contain
2318 is handled elsewhere. */
2319 if (per_cu->type_unit_group_p ())
2320 return;
2321
2322 /* The destructor of dwarf2_queue_guard frees any entries left on
2323 the queue. After this point we're guaranteed to leave this function
2324 with the dwarf queue empty. */
2325 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2326
2327 if (dwarf2_per_objfile->using_index
2328 ? per_cu->v.quick->compunit_symtab == NULL
2329 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2330 {
2331 queue_comp_unit (per_cu, language_minimal);
2332 load_cu (per_cu, skip_partial);
2333
2334 /* If we just loaded a CU from a DWO, and we're working with an index
2335 that may badly handle TUs, load all the TUs in that DWO as well.
2336 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2337 if (!per_cu->is_debug_types
2338 && per_cu->cu != NULL
2339 && per_cu->cu->dwo_unit != NULL
2340 && dwarf2_per_objfile->index_table != NULL
2341 && dwarf2_per_objfile->index_table->version <= 7
2342 /* DWP files aren't supported yet. */
2343 && get_dwp_file (dwarf2_per_objfile) == NULL)
2344 queue_and_load_all_dwo_tus (per_cu);
2345 }
2346
2347 process_queue (dwarf2_per_objfile);
2348
2349 /* Age the cache, releasing compilation units that have not
2350 been used recently. */
2351 age_cached_comp_units (dwarf2_per_objfile);
2352 }
2353
2354 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2355 the objfile from which this CU came. Returns the resulting symbol
2356 table. */
2357
2358 static struct compunit_symtab *
2359 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2360 {
2361 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2362
2363 gdb_assert (dwarf2_per_objfile->using_index);
2364 if (!per_cu->v.quick->compunit_symtab)
2365 {
2366 free_cached_comp_units freer (dwarf2_per_objfile);
2367 scoped_restore decrementer = increment_reading_symtab ();
2368 dw2_do_instantiate_symtab (per_cu, skip_partial);
2369 process_cu_includes (dwarf2_per_objfile);
2370 }
2371
2372 return per_cu->v.quick->compunit_symtab;
2373 }
2374
2375 /* See declaration. */
2376
2377 dwarf2_per_cu_data *
2378 dwarf2_per_objfile::get_cutu (int index)
2379 {
2380 if (index >= this->all_comp_units.size ())
2381 {
2382 index -= this->all_comp_units.size ();
2383 gdb_assert (index < this->all_type_units.size ());
2384 return &this->all_type_units[index]->per_cu;
2385 }
2386
2387 return this->all_comp_units[index];
2388 }
2389
2390 /* See declaration. */
2391
2392 dwarf2_per_cu_data *
2393 dwarf2_per_objfile::get_cu (int index)
2394 {
2395 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2396
2397 return this->all_comp_units[index];
2398 }
2399
2400 /* See declaration. */
2401
2402 signatured_type *
2403 dwarf2_per_objfile::get_tu (int index)
2404 {
2405 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2406
2407 return this->all_type_units[index];
2408 }
2409
2410 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2411 objfile_obstack, and constructed with the specified field
2412 values. */
2413
2414 static dwarf2_per_cu_data *
2415 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2416 struct dwarf2_section_info *section,
2417 int is_dwz,
2418 sect_offset sect_off, ULONGEST length)
2419 {
2420 struct objfile *objfile = dwarf2_per_objfile->objfile;
2421 dwarf2_per_cu_data *the_cu
2422 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2423 struct dwarf2_per_cu_data);
2424 the_cu->sect_off = sect_off;
2425 the_cu->length = length;
2426 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2427 the_cu->section = section;
2428 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2429 struct dwarf2_per_cu_quick_data);
2430 the_cu->is_dwz = is_dwz;
2431 return the_cu;
2432 }
2433
2434 /* A helper for create_cus_from_index that handles a given list of
2435 CUs. */
2436
2437 static void
2438 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2439 const gdb_byte *cu_list, offset_type n_elements,
2440 struct dwarf2_section_info *section,
2441 int is_dwz)
2442 {
2443 for (offset_type i = 0; i < n_elements; i += 2)
2444 {
2445 gdb_static_assert (sizeof (ULONGEST) >= 8);
2446
2447 sect_offset sect_off
2448 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2449 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2450 cu_list += 2 * 8;
2451
2452 dwarf2_per_cu_data *per_cu
2453 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2454 sect_off, length);
2455 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2456 }
2457 }
2458
2459 /* Read the CU list from the mapped index, and use it to create all
2460 the CU objects for this objfile. */
2461
2462 static void
2463 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2464 const gdb_byte *cu_list, offset_type cu_list_elements,
2465 const gdb_byte *dwz_list, offset_type dwz_elements)
2466 {
2467 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2468 dwarf2_per_objfile->all_comp_units.reserve
2469 ((cu_list_elements + dwz_elements) / 2);
2470
2471 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2472 &dwarf2_per_objfile->info, 0);
2473
2474 if (dwz_elements == 0)
2475 return;
2476
2477 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2478 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2479 &dwz->info, 1);
2480 }
2481
2482 /* Create the signatured type hash table from the index. */
2483
2484 static void
2485 create_signatured_type_table_from_index
2486 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2487 struct dwarf2_section_info *section,
2488 const gdb_byte *bytes,
2489 offset_type elements)
2490 {
2491 struct objfile *objfile = dwarf2_per_objfile->objfile;
2492
2493 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2494 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2495
2496 htab_up sig_types_hash = allocate_signatured_type_table ();
2497
2498 for (offset_type i = 0; i < elements; i += 3)
2499 {
2500 struct signatured_type *sig_type;
2501 ULONGEST signature;
2502 void **slot;
2503 cu_offset type_offset_in_tu;
2504
2505 gdb_static_assert (sizeof (ULONGEST) >= 8);
2506 sect_offset sect_off
2507 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2508 type_offset_in_tu
2509 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2510 BFD_ENDIAN_LITTLE);
2511 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2512 bytes += 3 * 8;
2513
2514 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2515 struct signatured_type);
2516 sig_type->signature = signature;
2517 sig_type->type_offset_in_tu = type_offset_in_tu;
2518 sig_type->per_cu.is_debug_types = 1;
2519 sig_type->per_cu.section = section;
2520 sig_type->per_cu.sect_off = sect_off;
2521 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2522 sig_type->per_cu.v.quick
2523 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2524 struct dwarf2_per_cu_quick_data);
2525
2526 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2527 *slot = sig_type;
2528
2529 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2530 }
2531
2532 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2533 }
2534
2535 /* Create the signatured type hash table from .debug_names. */
2536
2537 static void
2538 create_signatured_type_table_from_debug_names
2539 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2540 const mapped_debug_names &map,
2541 struct dwarf2_section_info *section,
2542 struct dwarf2_section_info *abbrev_section)
2543 {
2544 struct objfile *objfile = dwarf2_per_objfile->objfile;
2545
2546 section->read (objfile);
2547 abbrev_section->read (objfile);
2548
2549 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2550 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2551
2552 htab_up sig_types_hash = allocate_signatured_type_table ();
2553
2554 for (uint32_t i = 0; i < map.tu_count; ++i)
2555 {
2556 struct signatured_type *sig_type;
2557 void **slot;
2558
2559 sect_offset sect_off
2560 = (sect_offset) (extract_unsigned_integer
2561 (map.tu_table_reordered + i * map.offset_size,
2562 map.offset_size,
2563 map.dwarf5_byte_order));
2564
2565 comp_unit_head cu_header;
2566 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2567 abbrev_section,
2568 section->buffer + to_underlying (sect_off),
2569 rcuh_kind::TYPE);
2570
2571 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2572 struct signatured_type);
2573 sig_type->signature = cu_header.signature;
2574 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2575 sig_type->per_cu.is_debug_types = 1;
2576 sig_type->per_cu.section = section;
2577 sig_type->per_cu.sect_off = sect_off;
2578 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2579 sig_type->per_cu.v.quick
2580 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2581 struct dwarf2_per_cu_quick_data);
2582
2583 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2584 *slot = sig_type;
2585
2586 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2587 }
2588
2589 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2590 }
2591
2592 /* Read the address map data from the mapped index, and use it to
2593 populate the objfile's psymtabs_addrmap. */
2594
2595 static void
2596 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2597 struct mapped_index *index)
2598 {
2599 struct objfile *objfile = dwarf2_per_objfile->objfile;
2600 struct gdbarch *gdbarch = objfile->arch ();
2601 const gdb_byte *iter, *end;
2602 struct addrmap *mutable_map;
2603 CORE_ADDR baseaddr;
2604
2605 auto_obstack temp_obstack;
2606
2607 mutable_map = addrmap_create_mutable (&temp_obstack);
2608
2609 iter = index->address_table.data ();
2610 end = iter + index->address_table.size ();
2611
2612 baseaddr = objfile->text_section_offset ();
2613
2614 while (iter < end)
2615 {
2616 ULONGEST hi, lo, cu_index;
2617 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2618 iter += 8;
2619 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2620 iter += 8;
2621 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2622 iter += 4;
2623
2624 if (lo > hi)
2625 {
2626 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2627 hex_string (lo), hex_string (hi));
2628 continue;
2629 }
2630
2631 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2632 {
2633 complaint (_(".gdb_index address table has invalid CU number %u"),
2634 (unsigned) cu_index);
2635 continue;
2636 }
2637
2638 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2639 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2640 addrmap_set_empty (mutable_map, lo, hi - 1,
2641 dwarf2_per_objfile->get_cu (cu_index));
2642 }
2643
2644 objfile->partial_symtabs->psymtabs_addrmap
2645 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2646 }
2647
2648 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2649 populate the objfile's psymtabs_addrmap. */
2650
2651 static void
2652 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2653 struct dwarf2_section_info *section)
2654 {
2655 struct objfile *objfile = dwarf2_per_objfile->objfile;
2656 bfd *abfd = objfile->obfd;
2657 struct gdbarch *gdbarch = objfile->arch ();
2658 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2659
2660 auto_obstack temp_obstack;
2661 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2662
2663 std::unordered_map<sect_offset,
2664 dwarf2_per_cu_data *,
2665 gdb::hash_enum<sect_offset>>
2666 debug_info_offset_to_per_cu;
2667 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2668 {
2669 const auto insertpair
2670 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2671 if (!insertpair.second)
2672 {
2673 warning (_("Section .debug_aranges in %s has duplicate "
2674 "debug_info_offset %s, ignoring .debug_aranges."),
2675 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2676 return;
2677 }
2678 }
2679
2680 section->read (objfile);
2681
2682 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2683
2684 const gdb_byte *addr = section->buffer;
2685
2686 while (addr < section->buffer + section->size)
2687 {
2688 const gdb_byte *const entry_addr = addr;
2689 unsigned int bytes_read;
2690
2691 const LONGEST entry_length = read_initial_length (abfd, addr,
2692 &bytes_read);
2693 addr += bytes_read;
2694
2695 const gdb_byte *const entry_end = addr + entry_length;
2696 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2697 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2698 if (addr + entry_length > section->buffer + section->size)
2699 {
2700 warning (_("Section .debug_aranges in %s entry at offset %s "
2701 "length %s exceeds section length %s, "
2702 "ignoring .debug_aranges."),
2703 objfile_name (objfile),
2704 plongest (entry_addr - section->buffer),
2705 plongest (bytes_read + entry_length),
2706 pulongest (section->size));
2707 return;
2708 }
2709
2710 /* The version number. */
2711 const uint16_t version = read_2_bytes (abfd, addr);
2712 addr += 2;
2713 if (version != 2)
2714 {
2715 warning (_("Section .debug_aranges in %s entry at offset %s "
2716 "has unsupported version %d, ignoring .debug_aranges."),
2717 objfile_name (objfile),
2718 plongest (entry_addr - section->buffer), version);
2719 return;
2720 }
2721
2722 const uint64_t debug_info_offset
2723 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2724 addr += offset_size;
2725 const auto per_cu_it
2726 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2727 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2728 {
2729 warning (_("Section .debug_aranges in %s entry at offset %s "
2730 "debug_info_offset %s does not exists, "
2731 "ignoring .debug_aranges."),
2732 objfile_name (objfile),
2733 plongest (entry_addr - section->buffer),
2734 pulongest (debug_info_offset));
2735 return;
2736 }
2737 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2738
2739 const uint8_t address_size = *addr++;
2740 if (address_size < 1 || address_size > 8)
2741 {
2742 warning (_("Section .debug_aranges in %s entry at offset %s "
2743 "address_size %u is invalid, ignoring .debug_aranges."),
2744 objfile_name (objfile),
2745 plongest (entry_addr - section->buffer), address_size);
2746 return;
2747 }
2748
2749 const uint8_t segment_selector_size = *addr++;
2750 if (segment_selector_size != 0)
2751 {
2752 warning (_("Section .debug_aranges in %s entry at offset %s "
2753 "segment_selector_size %u is not supported, "
2754 "ignoring .debug_aranges."),
2755 objfile_name (objfile),
2756 plongest (entry_addr - section->buffer),
2757 segment_selector_size);
2758 return;
2759 }
2760
2761 /* Must pad to an alignment boundary that is twice the address
2762 size. It is undocumented by the DWARF standard but GCC does
2763 use it. */
2764 for (size_t padding = ((-(addr - section->buffer))
2765 & (2 * address_size - 1));
2766 padding > 0; padding--)
2767 if (*addr++ != 0)
2768 {
2769 warning (_("Section .debug_aranges in %s entry at offset %s "
2770 "padding is not zero, ignoring .debug_aranges."),
2771 objfile_name (objfile),
2772 plongest (entry_addr - section->buffer));
2773 return;
2774 }
2775
2776 for (;;)
2777 {
2778 if (addr + 2 * address_size > entry_end)
2779 {
2780 warning (_("Section .debug_aranges in %s entry at offset %s "
2781 "address list is not properly terminated, "
2782 "ignoring .debug_aranges."),
2783 objfile_name (objfile),
2784 plongest (entry_addr - section->buffer));
2785 return;
2786 }
2787 ULONGEST start = extract_unsigned_integer (addr, address_size,
2788 dwarf5_byte_order);
2789 addr += address_size;
2790 ULONGEST length = extract_unsigned_integer (addr, address_size,
2791 dwarf5_byte_order);
2792 addr += address_size;
2793 if (start == 0 && length == 0)
2794 break;
2795 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2796 {
2797 /* Symbol was eliminated due to a COMDAT group. */
2798 continue;
2799 }
2800 ULONGEST end = start + length;
2801 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2802 - baseaddr);
2803 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2804 - baseaddr);
2805 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2806 }
2807 }
2808
2809 objfile->partial_symtabs->psymtabs_addrmap
2810 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2811 }
2812
2813 /* Find a slot in the mapped index INDEX for the object named NAME.
2814 If NAME is found, set *VEC_OUT to point to the CU vector in the
2815 constant pool and return true. If NAME cannot be found, return
2816 false. */
2817
2818 static bool
2819 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2820 offset_type **vec_out)
2821 {
2822 offset_type hash;
2823 offset_type slot, step;
2824 int (*cmp) (const char *, const char *);
2825
2826 gdb::unique_xmalloc_ptr<char> without_params;
2827 if (current_language->la_language == language_cplus
2828 || current_language->la_language == language_fortran
2829 || current_language->la_language == language_d)
2830 {
2831 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2832 not contain any. */
2833
2834 if (strchr (name, '(') != NULL)
2835 {
2836 without_params = cp_remove_params (name);
2837
2838 if (without_params != NULL)
2839 name = without_params.get ();
2840 }
2841 }
2842
2843 /* Index version 4 did not support case insensitive searches. But the
2844 indices for case insensitive languages are built in lowercase, therefore
2845 simulate our NAME being searched is also lowercased. */
2846 hash = mapped_index_string_hash ((index->version == 4
2847 && case_sensitivity == case_sensitive_off
2848 ? 5 : index->version),
2849 name);
2850
2851 slot = hash & (index->symbol_table.size () - 1);
2852 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2853 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2854
2855 for (;;)
2856 {
2857 const char *str;
2858
2859 const auto &bucket = index->symbol_table[slot];
2860 if (bucket.name == 0 && bucket.vec == 0)
2861 return false;
2862
2863 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2864 if (!cmp (name, str))
2865 {
2866 *vec_out = (offset_type *) (index->constant_pool
2867 + MAYBE_SWAP (bucket.vec));
2868 return true;
2869 }
2870
2871 slot = (slot + step) & (index->symbol_table.size () - 1);
2872 }
2873 }
2874
2875 /* A helper function that reads the .gdb_index from BUFFER and fills
2876 in MAP. FILENAME is the name of the file containing the data;
2877 it is used for error reporting. DEPRECATED_OK is true if it is
2878 ok to use deprecated sections.
2879
2880 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2881 out parameters that are filled in with information about the CU and
2882 TU lists in the section.
2883
2884 Returns true if all went well, false otherwise. */
2885
2886 static bool
2887 read_gdb_index_from_buffer (const char *filename,
2888 bool deprecated_ok,
2889 gdb::array_view<const gdb_byte> buffer,
2890 struct mapped_index *map,
2891 const gdb_byte **cu_list,
2892 offset_type *cu_list_elements,
2893 const gdb_byte **types_list,
2894 offset_type *types_list_elements)
2895 {
2896 const gdb_byte *addr = &buffer[0];
2897
2898 /* Version check. */
2899 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2900 /* Versions earlier than 3 emitted every copy of a psymbol. This
2901 causes the index to behave very poorly for certain requests. Version 3
2902 contained incomplete addrmap. So, it seems better to just ignore such
2903 indices. */
2904 if (version < 4)
2905 {
2906 static int warning_printed = 0;
2907 if (!warning_printed)
2908 {
2909 warning (_("Skipping obsolete .gdb_index section in %s."),
2910 filename);
2911 warning_printed = 1;
2912 }
2913 return 0;
2914 }
2915 /* Index version 4 uses a different hash function than index version
2916 5 and later.
2917
2918 Versions earlier than 6 did not emit psymbols for inlined
2919 functions. Using these files will cause GDB not to be able to
2920 set breakpoints on inlined functions by name, so we ignore these
2921 indices unless the user has done
2922 "set use-deprecated-index-sections on". */
2923 if (version < 6 && !deprecated_ok)
2924 {
2925 static int warning_printed = 0;
2926 if (!warning_printed)
2927 {
2928 warning (_("\
2929 Skipping deprecated .gdb_index section in %s.\n\
2930 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2931 to use the section anyway."),
2932 filename);
2933 warning_printed = 1;
2934 }
2935 return 0;
2936 }
2937 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2938 of the TU (for symbols coming from TUs),
2939 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2940 Plus gold-generated indices can have duplicate entries for global symbols,
2941 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2942 These are just performance bugs, and we can't distinguish gdb-generated
2943 indices from gold-generated ones, so issue no warning here. */
2944
2945 /* Indexes with higher version than the one supported by GDB may be no
2946 longer backward compatible. */
2947 if (version > 8)
2948 return 0;
2949
2950 map->version = version;
2951
2952 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2953
2954 int i = 0;
2955 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2956 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2957 / 8);
2958 ++i;
2959
2960 *types_list = addr + MAYBE_SWAP (metadata[i]);
2961 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2962 - MAYBE_SWAP (metadata[i]))
2963 / 8);
2964 ++i;
2965
2966 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2967 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2968 map->address_table
2969 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2970 ++i;
2971
2972 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2973 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2974 map->symbol_table
2975 = gdb::array_view<mapped_index::symbol_table_slot>
2976 ((mapped_index::symbol_table_slot *) symbol_table,
2977 (mapped_index::symbol_table_slot *) symbol_table_end);
2978
2979 ++i;
2980 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2981
2982 return 1;
2983 }
2984
2985 /* Callback types for dwarf2_read_gdb_index. */
2986
2987 typedef gdb::function_view
2988 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2989 get_gdb_index_contents_ftype;
2990 typedef gdb::function_view
2991 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2992 get_gdb_index_contents_dwz_ftype;
2993
2994 /* Read .gdb_index. If everything went ok, initialize the "quick"
2995 elements of all the CUs and return 1. Otherwise, return 0. */
2996
2997 static int
2998 dwarf2_read_gdb_index
2999 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3000 get_gdb_index_contents_ftype get_gdb_index_contents,
3001 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3002 {
3003 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3004 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3005 struct dwz_file *dwz;
3006 struct objfile *objfile = dwarf2_per_objfile->objfile;
3007
3008 gdb::array_view<const gdb_byte> main_index_contents
3009 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3010
3011 if (main_index_contents.empty ())
3012 return 0;
3013
3014 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3015 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3016 use_deprecated_index_sections,
3017 main_index_contents, map.get (), &cu_list,
3018 &cu_list_elements, &types_list,
3019 &types_list_elements))
3020 return 0;
3021
3022 /* Don't use the index if it's empty. */
3023 if (map->symbol_table.empty ())
3024 return 0;
3025
3026 /* If there is a .dwz file, read it so we can get its CU list as
3027 well. */
3028 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3029 if (dwz != NULL)
3030 {
3031 struct mapped_index dwz_map;
3032 const gdb_byte *dwz_types_ignore;
3033 offset_type dwz_types_elements_ignore;
3034
3035 gdb::array_view<const gdb_byte> dwz_index_content
3036 = get_gdb_index_contents_dwz (objfile, dwz);
3037
3038 if (dwz_index_content.empty ())
3039 return 0;
3040
3041 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3042 1, dwz_index_content, &dwz_map,
3043 &dwz_list, &dwz_list_elements,
3044 &dwz_types_ignore,
3045 &dwz_types_elements_ignore))
3046 {
3047 warning (_("could not read '.gdb_index' section from %s; skipping"),
3048 bfd_get_filename (dwz->dwz_bfd.get ()));
3049 return 0;
3050 }
3051 }
3052
3053 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3054 dwz_list, dwz_list_elements);
3055
3056 if (types_list_elements)
3057 {
3058 /* We can only handle a single .debug_types when we have an
3059 index. */
3060 if (dwarf2_per_objfile->types.size () != 1)
3061 return 0;
3062
3063 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3064
3065 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3066 types_list, types_list_elements);
3067 }
3068
3069 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3070
3071 dwarf2_per_objfile->index_table = std::move (map);
3072 dwarf2_per_objfile->using_index = 1;
3073 dwarf2_per_objfile->quick_file_names_table =
3074 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3075
3076 return 1;
3077 }
3078
3079 /* die_reader_func for dw2_get_file_names. */
3080
3081 static void
3082 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3083 const gdb_byte *info_ptr,
3084 struct die_info *comp_unit_die)
3085 {
3086 struct dwarf2_cu *cu = reader->cu;
3087 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3088 struct dwarf2_per_objfile *dwarf2_per_objfile
3089 = cu->per_cu->dwarf2_per_objfile;
3090 struct objfile *objfile = dwarf2_per_objfile->objfile;
3091 struct dwarf2_per_cu_data *lh_cu;
3092 struct attribute *attr;
3093 void **slot;
3094 struct quick_file_names *qfn;
3095
3096 gdb_assert (! this_cu->is_debug_types);
3097
3098 /* Our callers never want to match partial units -- instead they
3099 will match the enclosing full CU. */
3100 if (comp_unit_die->tag == DW_TAG_partial_unit)
3101 {
3102 this_cu->v.quick->no_file_data = 1;
3103 return;
3104 }
3105
3106 lh_cu = this_cu;
3107 slot = NULL;
3108
3109 line_header_up lh;
3110 sect_offset line_offset {};
3111
3112 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3113 if (attr != nullptr)
3114 {
3115 struct quick_file_names find_entry;
3116
3117 line_offset = (sect_offset) DW_UNSND (attr);
3118
3119 /* We may have already read in this line header (TU line header sharing).
3120 If we have we're done. */
3121 find_entry.hash.dwo_unit = cu->dwo_unit;
3122 find_entry.hash.line_sect_off = line_offset;
3123 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3124 &find_entry, INSERT);
3125 if (*slot != NULL)
3126 {
3127 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3128 return;
3129 }
3130
3131 lh = dwarf_decode_line_header (line_offset, cu);
3132 }
3133 if (lh == NULL)
3134 {
3135 lh_cu->v.quick->no_file_data = 1;
3136 return;
3137 }
3138
3139 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3140 qfn->hash.dwo_unit = cu->dwo_unit;
3141 qfn->hash.line_sect_off = line_offset;
3142 gdb_assert (slot != NULL);
3143 *slot = qfn;
3144
3145 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3146
3147 int offset = 0;
3148 if (strcmp (fnd.name, "<unknown>") != 0)
3149 ++offset;
3150
3151 qfn->num_file_names = offset + lh->file_names_size ();
3152 qfn->file_names =
3153 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3154 if (offset != 0)
3155 qfn->file_names[0] = xstrdup (fnd.name);
3156 for (int i = 0; i < lh->file_names_size (); ++i)
3157 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3158 fnd.comp_dir).release ();
3159 qfn->real_names = NULL;
3160
3161 lh_cu->v.quick->file_names = qfn;
3162 }
3163
3164 /* A helper for the "quick" functions which attempts to read the line
3165 table for THIS_CU. */
3166
3167 static struct quick_file_names *
3168 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3169 {
3170 /* This should never be called for TUs. */
3171 gdb_assert (! this_cu->is_debug_types);
3172 /* Nor type unit groups. */
3173 gdb_assert (! this_cu->type_unit_group_p ());
3174
3175 if (this_cu->v.quick->file_names != NULL)
3176 return this_cu->v.quick->file_names;
3177 /* If we know there is no line data, no point in looking again. */
3178 if (this_cu->v.quick->no_file_data)
3179 return NULL;
3180
3181 cutu_reader reader (this_cu);
3182 if (!reader.dummy_p)
3183 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3184
3185 if (this_cu->v.quick->no_file_data)
3186 return NULL;
3187 return this_cu->v.quick->file_names;
3188 }
3189
3190 /* A helper for the "quick" functions which computes and caches the
3191 real path for a given file name from the line table. */
3192
3193 static const char *
3194 dw2_get_real_path (struct objfile *objfile,
3195 struct quick_file_names *qfn, int index)
3196 {
3197 if (qfn->real_names == NULL)
3198 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3199 qfn->num_file_names, const char *);
3200
3201 if (qfn->real_names[index] == NULL)
3202 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3203
3204 return qfn->real_names[index];
3205 }
3206
3207 static struct symtab *
3208 dw2_find_last_source_symtab (struct objfile *objfile)
3209 {
3210 struct dwarf2_per_objfile *dwarf2_per_objfile
3211 = get_dwarf2_per_objfile (objfile);
3212 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3213 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3214
3215 if (cust == NULL)
3216 return NULL;
3217
3218 return compunit_primary_filetab (cust);
3219 }
3220
3221 /* Traversal function for dw2_forget_cached_source_info. */
3222
3223 static int
3224 dw2_free_cached_file_names (void **slot, void *info)
3225 {
3226 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3227
3228 if (file_data->real_names)
3229 {
3230 int i;
3231
3232 for (i = 0; i < file_data->num_file_names; ++i)
3233 {
3234 xfree ((void*) file_data->real_names[i]);
3235 file_data->real_names[i] = NULL;
3236 }
3237 }
3238
3239 return 1;
3240 }
3241
3242 static void
3243 dw2_forget_cached_source_info (struct objfile *objfile)
3244 {
3245 struct dwarf2_per_objfile *dwarf2_per_objfile
3246 = get_dwarf2_per_objfile (objfile);
3247
3248 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3249 dw2_free_cached_file_names, NULL);
3250 }
3251
3252 /* Helper function for dw2_map_symtabs_matching_filename that expands
3253 the symtabs and calls the iterator. */
3254
3255 static int
3256 dw2_map_expand_apply (struct objfile *objfile,
3257 struct dwarf2_per_cu_data *per_cu,
3258 const char *name, const char *real_path,
3259 gdb::function_view<bool (symtab *)> callback)
3260 {
3261 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3262
3263 /* Don't visit already-expanded CUs. */
3264 if (per_cu->v.quick->compunit_symtab)
3265 return 0;
3266
3267 /* This may expand more than one symtab, and we want to iterate over
3268 all of them. */
3269 dw2_instantiate_symtab (per_cu, false);
3270
3271 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3272 last_made, callback);
3273 }
3274
3275 /* Implementation of the map_symtabs_matching_filename method. */
3276
3277 static bool
3278 dw2_map_symtabs_matching_filename
3279 (struct objfile *objfile, const char *name, const char *real_path,
3280 gdb::function_view<bool (symtab *)> callback)
3281 {
3282 const char *name_basename = lbasename (name);
3283 struct dwarf2_per_objfile *dwarf2_per_objfile
3284 = get_dwarf2_per_objfile (objfile);
3285
3286 /* The rule is CUs specify all the files, including those used by
3287 any TU, so there's no need to scan TUs here. */
3288
3289 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3290 {
3291 /* We only need to look at symtabs not already expanded. */
3292 if (per_cu->v.quick->compunit_symtab)
3293 continue;
3294
3295 quick_file_names *file_data = dw2_get_file_names (per_cu);
3296 if (file_data == NULL)
3297 continue;
3298
3299 for (int j = 0; j < file_data->num_file_names; ++j)
3300 {
3301 const char *this_name = file_data->file_names[j];
3302 const char *this_real_name;
3303
3304 if (compare_filenames_for_search (this_name, name))
3305 {
3306 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3307 callback))
3308 return true;
3309 continue;
3310 }
3311
3312 /* Before we invoke realpath, which can get expensive when many
3313 files are involved, do a quick comparison of the basenames. */
3314 if (! basenames_may_differ
3315 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3316 continue;
3317
3318 this_real_name = dw2_get_real_path (objfile, file_data, j);
3319 if (compare_filenames_for_search (this_real_name, name))
3320 {
3321 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3322 callback))
3323 return true;
3324 continue;
3325 }
3326
3327 if (real_path != NULL)
3328 {
3329 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3330 gdb_assert (IS_ABSOLUTE_PATH (name));
3331 if (this_real_name != NULL
3332 && FILENAME_CMP (real_path, this_real_name) == 0)
3333 {
3334 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3335 callback))
3336 return true;
3337 continue;
3338 }
3339 }
3340 }
3341 }
3342
3343 return false;
3344 }
3345
3346 /* Struct used to manage iterating over all CUs looking for a symbol. */
3347
3348 struct dw2_symtab_iterator
3349 {
3350 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3351 struct dwarf2_per_objfile *dwarf2_per_objfile;
3352 /* If set, only look for symbols that match that block. Valid values are
3353 GLOBAL_BLOCK and STATIC_BLOCK. */
3354 gdb::optional<block_enum> block_index;
3355 /* The kind of symbol we're looking for. */
3356 domain_enum domain;
3357 /* The list of CUs from the index entry of the symbol,
3358 or NULL if not found. */
3359 offset_type *vec;
3360 /* The next element in VEC to look at. */
3361 int next;
3362 /* The number of elements in VEC, or zero if there is no match. */
3363 int length;
3364 /* Have we seen a global version of the symbol?
3365 If so we can ignore all further global instances.
3366 This is to work around gold/15646, inefficient gold-generated
3367 indices. */
3368 int global_seen;
3369 };
3370
3371 /* Initialize the index symtab iterator ITER. */
3372
3373 static void
3374 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3375 struct dwarf2_per_objfile *dwarf2_per_objfile,
3376 gdb::optional<block_enum> block_index,
3377 domain_enum domain,
3378 const char *name)
3379 {
3380 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3381 iter->block_index = block_index;
3382 iter->domain = domain;
3383 iter->next = 0;
3384 iter->global_seen = 0;
3385
3386 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3387
3388 /* index is NULL if OBJF_READNOW. */
3389 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3390 iter->length = MAYBE_SWAP (*iter->vec);
3391 else
3392 {
3393 iter->vec = NULL;
3394 iter->length = 0;
3395 }
3396 }
3397
3398 /* Return the next matching CU or NULL if there are no more. */
3399
3400 static struct dwarf2_per_cu_data *
3401 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3402 {
3403 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3404
3405 for ( ; iter->next < iter->length; ++iter->next)
3406 {
3407 offset_type cu_index_and_attrs =
3408 MAYBE_SWAP (iter->vec[iter->next + 1]);
3409 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3410 gdb_index_symbol_kind symbol_kind =
3411 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3412 /* Only check the symbol attributes if they're present.
3413 Indices prior to version 7 don't record them,
3414 and indices >= 7 may elide them for certain symbols
3415 (gold does this). */
3416 int attrs_valid =
3417 (dwarf2_per_objfile->index_table->version >= 7
3418 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3419
3420 /* Don't crash on bad data. */
3421 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3422 + dwarf2_per_objfile->all_type_units.size ()))
3423 {
3424 complaint (_(".gdb_index entry has bad CU index"
3425 " [in module %s]"),
3426 objfile_name (dwarf2_per_objfile->objfile));
3427 continue;
3428 }
3429
3430 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3431
3432 /* Skip if already read in. */
3433 if (per_cu->v.quick->compunit_symtab)
3434 continue;
3435
3436 /* Check static vs global. */
3437 if (attrs_valid)
3438 {
3439 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3440
3441 if (iter->block_index.has_value ())
3442 {
3443 bool want_static = *iter->block_index == STATIC_BLOCK;
3444
3445 if (is_static != want_static)
3446 continue;
3447 }
3448
3449 /* Work around gold/15646. */
3450 if (!is_static && iter->global_seen)
3451 continue;
3452 if (!is_static)
3453 iter->global_seen = 1;
3454 }
3455
3456 /* Only check the symbol's kind if it has one. */
3457 if (attrs_valid)
3458 {
3459 switch (iter->domain)
3460 {
3461 case VAR_DOMAIN:
3462 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3463 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3464 /* Some types are also in VAR_DOMAIN. */
3465 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3466 continue;
3467 break;
3468 case STRUCT_DOMAIN:
3469 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3470 continue;
3471 break;
3472 case LABEL_DOMAIN:
3473 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3474 continue;
3475 break;
3476 case MODULE_DOMAIN:
3477 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3478 continue;
3479 break;
3480 default:
3481 break;
3482 }
3483 }
3484
3485 ++iter->next;
3486 return per_cu;
3487 }
3488
3489 return NULL;
3490 }
3491
3492 static struct compunit_symtab *
3493 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3494 const char *name, domain_enum domain)
3495 {
3496 struct compunit_symtab *stab_best = NULL;
3497 struct dwarf2_per_objfile *dwarf2_per_objfile
3498 = get_dwarf2_per_objfile (objfile);
3499
3500 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3501
3502 struct dw2_symtab_iterator iter;
3503 struct dwarf2_per_cu_data *per_cu;
3504
3505 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3506
3507 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3508 {
3509 struct symbol *sym, *with_opaque = NULL;
3510 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3511 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3512 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3513
3514 sym = block_find_symbol (block, name, domain,
3515 block_find_non_opaque_type_preferred,
3516 &with_opaque);
3517
3518 /* Some caution must be observed with overloaded functions
3519 and methods, since the index will not contain any overload
3520 information (but NAME might contain it). */
3521
3522 if (sym != NULL
3523 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3524 return stab;
3525 if (with_opaque != NULL
3526 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3527 stab_best = stab;
3528
3529 /* Keep looking through other CUs. */
3530 }
3531
3532 return stab_best;
3533 }
3534
3535 static void
3536 dw2_print_stats (struct objfile *objfile)
3537 {
3538 struct dwarf2_per_objfile *dwarf2_per_objfile
3539 = get_dwarf2_per_objfile (objfile);
3540 int total = (dwarf2_per_objfile->all_comp_units.size ()
3541 + dwarf2_per_objfile->all_type_units.size ());
3542 int count = 0;
3543
3544 for (int i = 0; i < total; ++i)
3545 {
3546 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3547
3548 if (!per_cu->v.quick->compunit_symtab)
3549 ++count;
3550 }
3551 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3552 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3553 }
3554
3555 /* This dumps minimal information about the index.
3556 It is called via "mt print objfiles".
3557 One use is to verify .gdb_index has been loaded by the
3558 gdb.dwarf2/gdb-index.exp testcase. */
3559
3560 static void
3561 dw2_dump (struct objfile *objfile)
3562 {
3563 struct dwarf2_per_objfile *dwarf2_per_objfile
3564 = get_dwarf2_per_objfile (objfile);
3565
3566 gdb_assert (dwarf2_per_objfile->using_index);
3567 printf_filtered (".gdb_index:");
3568 if (dwarf2_per_objfile->index_table != NULL)
3569 {
3570 printf_filtered (" version %d\n",
3571 dwarf2_per_objfile->index_table->version);
3572 }
3573 else
3574 printf_filtered (" faked for \"readnow\"\n");
3575 printf_filtered ("\n");
3576 }
3577
3578 static void
3579 dw2_expand_symtabs_for_function (struct objfile *objfile,
3580 const char *func_name)
3581 {
3582 struct dwarf2_per_objfile *dwarf2_per_objfile
3583 = get_dwarf2_per_objfile (objfile);
3584
3585 struct dw2_symtab_iterator iter;
3586 struct dwarf2_per_cu_data *per_cu;
3587
3588 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3589
3590 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3591 dw2_instantiate_symtab (per_cu, false);
3592
3593 }
3594
3595 static void
3596 dw2_expand_all_symtabs (struct objfile *objfile)
3597 {
3598 struct dwarf2_per_objfile *dwarf2_per_objfile
3599 = get_dwarf2_per_objfile (objfile);
3600 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3601 + dwarf2_per_objfile->all_type_units.size ());
3602
3603 for (int i = 0; i < total_units; ++i)
3604 {
3605 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3606
3607 /* We don't want to directly expand a partial CU, because if we
3608 read it with the wrong language, then assertion failures can
3609 be triggered later on. See PR symtab/23010. So, tell
3610 dw2_instantiate_symtab to skip partial CUs -- any important
3611 partial CU will be read via DW_TAG_imported_unit anyway. */
3612 dw2_instantiate_symtab (per_cu, true);
3613 }
3614 }
3615
3616 static void
3617 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3618 const char *fullname)
3619 {
3620 struct dwarf2_per_objfile *dwarf2_per_objfile
3621 = get_dwarf2_per_objfile (objfile);
3622
3623 /* We don't need to consider type units here.
3624 This is only called for examining code, e.g. expand_line_sal.
3625 There can be an order of magnitude (or more) more type units
3626 than comp units, and we avoid them if we can. */
3627
3628 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3629 {
3630 /* We only need to look at symtabs not already expanded. */
3631 if (per_cu->v.quick->compunit_symtab)
3632 continue;
3633
3634 quick_file_names *file_data = dw2_get_file_names (per_cu);
3635 if (file_data == NULL)
3636 continue;
3637
3638 for (int j = 0; j < file_data->num_file_names; ++j)
3639 {
3640 const char *this_fullname = file_data->file_names[j];
3641
3642 if (filename_cmp (this_fullname, fullname) == 0)
3643 {
3644 dw2_instantiate_symtab (per_cu, false);
3645 break;
3646 }
3647 }
3648 }
3649 }
3650
3651 static void
3652 dw2_map_matching_symbols
3653 (struct objfile *objfile,
3654 const lookup_name_info &name, domain_enum domain,
3655 int global,
3656 gdb::function_view<symbol_found_callback_ftype> callback,
3657 symbol_compare_ftype *ordered_compare)
3658 {
3659 /* Used for Ada. */
3660 struct dwarf2_per_objfile *dwarf2_per_objfile
3661 = get_dwarf2_per_objfile (objfile);
3662
3663 if (dwarf2_per_objfile->index_table != nullptr)
3664 {
3665 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3666 here though if the current language is Ada for a non-Ada objfile
3667 using GNU index. As Ada does not look for non-Ada symbols this
3668 function should just return. */
3669 return;
3670 }
3671
3672 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3673 inline psym_map_matching_symbols here, assuming all partial symtabs have
3674 been read in. */
3675 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3676
3677 for (compunit_symtab *cust : objfile->compunits ())
3678 {
3679 const struct block *block;
3680
3681 if (cust == NULL)
3682 continue;
3683 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3684 if (!iterate_over_symbols_terminated (block, name,
3685 domain, callback))
3686 return;
3687 }
3688 }
3689
3690 /* Starting from a search name, return the string that finds the upper
3691 bound of all strings that start with SEARCH_NAME in a sorted name
3692 list. Returns the empty string to indicate that the upper bound is
3693 the end of the list. */
3694
3695 static std::string
3696 make_sort_after_prefix_name (const char *search_name)
3697 {
3698 /* When looking to complete "func", we find the upper bound of all
3699 symbols that start with "func" by looking for where we'd insert
3700 the closest string that would follow "func" in lexicographical
3701 order. Usually, that's "func"-with-last-character-incremented,
3702 i.e. "fund". Mind non-ASCII characters, though. Usually those
3703 will be UTF-8 multi-byte sequences, but we can't be certain.
3704 Especially mind the 0xff character, which is a valid character in
3705 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3706 rule out compilers allowing it in identifiers. Note that
3707 conveniently, strcmp/strcasecmp are specified to compare
3708 characters interpreted as unsigned char. So what we do is treat
3709 the whole string as a base 256 number composed of a sequence of
3710 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3711 to 0, and carries 1 to the following more-significant position.
3712 If the very first character in SEARCH_NAME ends up incremented
3713 and carries/overflows, then the upper bound is the end of the
3714 list. The string after the empty string is also the empty
3715 string.
3716
3717 Some examples of this operation:
3718
3719 SEARCH_NAME => "+1" RESULT
3720
3721 "abc" => "abd"
3722 "ab\xff" => "ac"
3723 "\xff" "a" "\xff" => "\xff" "b"
3724 "\xff" => ""
3725 "\xff\xff" => ""
3726 "" => ""
3727
3728 Then, with these symbols for example:
3729
3730 func
3731 func1
3732 fund
3733
3734 completing "func" looks for symbols between "func" and
3735 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3736 which finds "func" and "func1", but not "fund".
3737
3738 And with:
3739
3740 funcÿ (Latin1 'ÿ' [0xff])
3741 funcÿ1
3742 fund
3743
3744 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3745 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3746
3747 And with:
3748
3749 ÿÿ (Latin1 'ÿ' [0xff])
3750 ÿÿ1
3751
3752 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3753 the end of the list.
3754 */
3755 std::string after = search_name;
3756 while (!after.empty () && (unsigned char) after.back () == 0xff)
3757 after.pop_back ();
3758 if (!after.empty ())
3759 after.back () = (unsigned char) after.back () + 1;
3760 return after;
3761 }
3762
3763 /* See declaration. */
3764
3765 std::pair<std::vector<name_component>::const_iterator,
3766 std::vector<name_component>::const_iterator>
3767 mapped_index_base::find_name_components_bounds
3768 (const lookup_name_info &lookup_name_without_params, language lang) const
3769 {
3770 auto *name_cmp
3771 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3772
3773 const char *lang_name
3774 = lookup_name_without_params.language_lookup_name (lang);
3775
3776 /* Comparison function object for lower_bound that matches against a
3777 given symbol name. */
3778 auto lookup_compare_lower = [&] (const name_component &elem,
3779 const char *name)
3780 {
3781 const char *elem_qualified = this->symbol_name_at (elem.idx);
3782 const char *elem_name = elem_qualified + elem.name_offset;
3783 return name_cmp (elem_name, name) < 0;
3784 };
3785
3786 /* Comparison function object for upper_bound that matches against a
3787 given symbol name. */
3788 auto lookup_compare_upper = [&] (const char *name,
3789 const name_component &elem)
3790 {
3791 const char *elem_qualified = this->symbol_name_at (elem.idx);
3792 const char *elem_name = elem_qualified + elem.name_offset;
3793 return name_cmp (name, elem_name) < 0;
3794 };
3795
3796 auto begin = this->name_components.begin ();
3797 auto end = this->name_components.end ();
3798
3799 /* Find the lower bound. */
3800 auto lower = [&] ()
3801 {
3802 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3803 return begin;
3804 else
3805 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3806 } ();
3807
3808 /* Find the upper bound. */
3809 auto upper = [&] ()
3810 {
3811 if (lookup_name_without_params.completion_mode ())
3812 {
3813 /* In completion mode, we want UPPER to point past all
3814 symbols names that have the same prefix. I.e., with
3815 these symbols, and completing "func":
3816
3817 function << lower bound
3818 function1
3819 other_function << upper bound
3820
3821 We find the upper bound by looking for the insertion
3822 point of "func"-with-last-character-incremented,
3823 i.e. "fund". */
3824 std::string after = make_sort_after_prefix_name (lang_name);
3825 if (after.empty ())
3826 return end;
3827 return std::lower_bound (lower, end, after.c_str (),
3828 lookup_compare_lower);
3829 }
3830 else
3831 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3832 } ();
3833
3834 return {lower, upper};
3835 }
3836
3837 /* See declaration. */
3838
3839 void
3840 mapped_index_base::build_name_components ()
3841 {
3842 if (!this->name_components.empty ())
3843 return;
3844
3845 this->name_components_casing = case_sensitivity;
3846 auto *name_cmp
3847 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3848
3849 /* The code below only knows how to break apart components of C++
3850 symbol names (and other languages that use '::' as
3851 namespace/module separator) and Ada symbol names. */
3852 auto count = this->symbol_name_count ();
3853 for (offset_type idx = 0; idx < count; idx++)
3854 {
3855 if (this->symbol_name_slot_invalid (idx))
3856 continue;
3857
3858 const char *name = this->symbol_name_at (idx);
3859
3860 /* Add each name component to the name component table. */
3861 unsigned int previous_len = 0;
3862
3863 if (strstr (name, "::") != nullptr)
3864 {
3865 for (unsigned int current_len = cp_find_first_component (name);
3866 name[current_len] != '\0';
3867 current_len += cp_find_first_component (name + current_len))
3868 {
3869 gdb_assert (name[current_len] == ':');
3870 this->name_components.push_back ({previous_len, idx});
3871 /* Skip the '::'. */
3872 current_len += 2;
3873 previous_len = current_len;
3874 }
3875 }
3876 else
3877 {
3878 /* Handle the Ada encoded (aka mangled) form here. */
3879 for (const char *iter = strstr (name, "__");
3880 iter != nullptr;
3881 iter = strstr (iter, "__"))
3882 {
3883 this->name_components.push_back ({previous_len, idx});
3884 iter += 2;
3885 previous_len = iter - name;
3886 }
3887 }
3888
3889 this->name_components.push_back ({previous_len, idx});
3890 }
3891
3892 /* Sort name_components elements by name. */
3893 auto name_comp_compare = [&] (const name_component &left,
3894 const name_component &right)
3895 {
3896 const char *left_qualified = this->symbol_name_at (left.idx);
3897 const char *right_qualified = this->symbol_name_at (right.idx);
3898
3899 const char *left_name = left_qualified + left.name_offset;
3900 const char *right_name = right_qualified + right.name_offset;
3901
3902 return name_cmp (left_name, right_name) < 0;
3903 };
3904
3905 std::sort (this->name_components.begin (),
3906 this->name_components.end (),
3907 name_comp_compare);
3908 }
3909
3910 /* Helper for dw2_expand_symtabs_matching that works with a
3911 mapped_index_base instead of the containing objfile. This is split
3912 to a separate function in order to be able to unit test the
3913 name_components matching using a mock mapped_index_base. For each
3914 symbol name that matches, calls MATCH_CALLBACK, passing it the
3915 symbol's index in the mapped_index_base symbol table. */
3916
3917 static void
3918 dw2_expand_symtabs_matching_symbol
3919 (mapped_index_base &index,
3920 const lookup_name_info &lookup_name_in,
3921 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3922 enum search_domain kind,
3923 gdb::function_view<bool (offset_type)> match_callback)
3924 {
3925 lookup_name_info lookup_name_without_params
3926 = lookup_name_in.make_ignore_params ();
3927
3928 /* Build the symbol name component sorted vector, if we haven't
3929 yet. */
3930 index.build_name_components ();
3931
3932 /* The same symbol may appear more than once in the range though.
3933 E.g., if we're looking for symbols that complete "w", and we have
3934 a symbol named "w1::w2", we'll find the two name components for
3935 that same symbol in the range. To be sure we only call the
3936 callback once per symbol, we first collect the symbol name
3937 indexes that matched in a temporary vector and ignore
3938 duplicates. */
3939 std::vector<offset_type> matches;
3940
3941 struct name_and_matcher
3942 {
3943 symbol_name_matcher_ftype *matcher;
3944 const char *name;
3945
3946 bool operator== (const name_and_matcher &other) const
3947 {
3948 return matcher == other.matcher && strcmp (name, other.name) == 0;
3949 }
3950 };
3951
3952 /* A vector holding all the different symbol name matchers, for all
3953 languages. */
3954 std::vector<name_and_matcher> matchers;
3955
3956 for (int i = 0; i < nr_languages; i++)
3957 {
3958 enum language lang_e = (enum language) i;
3959
3960 const language_defn *lang = language_def (lang_e);
3961 symbol_name_matcher_ftype *name_matcher
3962 = get_symbol_name_matcher (lang, lookup_name_without_params);
3963
3964 name_and_matcher key {
3965 name_matcher,
3966 lookup_name_without_params.language_lookup_name (lang_e)
3967 };
3968
3969 /* Don't insert the same comparison routine more than once.
3970 Note that we do this linear walk. This is not a problem in
3971 practice because the number of supported languages is
3972 low. */
3973 if (std::find (matchers.begin (), matchers.end (), key)
3974 != matchers.end ())
3975 continue;
3976 matchers.push_back (std::move (key));
3977
3978 auto bounds
3979 = index.find_name_components_bounds (lookup_name_without_params,
3980 lang_e);
3981
3982 /* Now for each symbol name in range, check to see if we have a name
3983 match, and if so, call the MATCH_CALLBACK callback. */
3984
3985 for (; bounds.first != bounds.second; ++bounds.first)
3986 {
3987 const char *qualified = index.symbol_name_at (bounds.first->idx);
3988
3989 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3990 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3991 continue;
3992
3993 matches.push_back (bounds.first->idx);
3994 }
3995 }
3996
3997 std::sort (matches.begin (), matches.end ());
3998
3999 /* Finally call the callback, once per match. */
4000 ULONGEST prev = -1;
4001 for (offset_type idx : matches)
4002 {
4003 if (prev != idx)
4004 {
4005 if (!match_callback (idx))
4006 break;
4007 prev = idx;
4008 }
4009 }
4010
4011 /* Above we use a type wider than idx's for 'prev', since 0 and
4012 (offset_type)-1 are both possible values. */
4013 static_assert (sizeof (prev) > sizeof (offset_type), "");
4014 }
4015
4016 #if GDB_SELF_TEST
4017
4018 namespace selftests { namespace dw2_expand_symtabs_matching {
4019
4020 /* A mock .gdb_index/.debug_names-like name index table, enough to
4021 exercise dw2_expand_symtabs_matching_symbol, which works with the
4022 mapped_index_base interface. Builds an index from the symbol list
4023 passed as parameter to the constructor. */
4024 class mock_mapped_index : public mapped_index_base
4025 {
4026 public:
4027 mock_mapped_index (gdb::array_view<const char *> symbols)
4028 : m_symbol_table (symbols)
4029 {}
4030
4031 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4032
4033 /* Return the number of names in the symbol table. */
4034 size_t symbol_name_count () const override
4035 {
4036 return m_symbol_table.size ();
4037 }
4038
4039 /* Get the name of the symbol at IDX in the symbol table. */
4040 const char *symbol_name_at (offset_type idx) const override
4041 {
4042 return m_symbol_table[idx];
4043 }
4044
4045 private:
4046 gdb::array_view<const char *> m_symbol_table;
4047 };
4048
4049 /* Convenience function that converts a NULL pointer to a "<null>"
4050 string, to pass to print routines. */
4051
4052 static const char *
4053 string_or_null (const char *str)
4054 {
4055 return str != NULL ? str : "<null>";
4056 }
4057
4058 /* Check if a lookup_name_info built from
4059 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4060 index. EXPECTED_LIST is the list of expected matches, in expected
4061 matching order. If no match expected, then an empty list is
4062 specified. Returns true on success. On failure prints a warning
4063 indicating the file:line that failed, and returns false. */
4064
4065 static bool
4066 check_match (const char *file, int line,
4067 mock_mapped_index &mock_index,
4068 const char *name, symbol_name_match_type match_type,
4069 bool completion_mode,
4070 std::initializer_list<const char *> expected_list)
4071 {
4072 lookup_name_info lookup_name (name, match_type, completion_mode);
4073
4074 bool matched = true;
4075
4076 auto mismatch = [&] (const char *expected_str,
4077 const char *got)
4078 {
4079 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4080 "expected=\"%s\", got=\"%s\"\n"),
4081 file, line,
4082 (match_type == symbol_name_match_type::FULL
4083 ? "FULL" : "WILD"),
4084 name, string_or_null (expected_str), string_or_null (got));
4085 matched = false;
4086 };
4087
4088 auto expected_it = expected_list.begin ();
4089 auto expected_end = expected_list.end ();
4090
4091 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4092 NULL, ALL_DOMAIN,
4093 [&] (offset_type idx)
4094 {
4095 const char *matched_name = mock_index.symbol_name_at (idx);
4096 const char *expected_str
4097 = expected_it == expected_end ? NULL : *expected_it++;
4098
4099 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4100 mismatch (expected_str, matched_name);
4101 return true;
4102 });
4103
4104 const char *expected_str
4105 = expected_it == expected_end ? NULL : *expected_it++;
4106 if (expected_str != NULL)
4107 mismatch (expected_str, NULL);
4108
4109 return matched;
4110 }
4111
4112 /* The symbols added to the mock mapped_index for testing (in
4113 canonical form). */
4114 static const char *test_symbols[] = {
4115 "function",
4116 "std::bar",
4117 "std::zfunction",
4118 "std::zfunction2",
4119 "w1::w2",
4120 "ns::foo<char*>",
4121 "ns::foo<int>",
4122 "ns::foo<long>",
4123 "ns2::tmpl<int>::foo2",
4124 "(anonymous namespace)::A::B::C",
4125
4126 /* These are used to check that the increment-last-char in the
4127 matching algorithm for completion doesn't match "t1_fund" when
4128 completing "t1_func". */
4129 "t1_func",
4130 "t1_func1",
4131 "t1_fund",
4132 "t1_fund1",
4133
4134 /* A UTF-8 name with multi-byte sequences to make sure that
4135 cp-name-parser understands this as a single identifier ("função"
4136 is "function" in PT). */
4137 u8"u8função",
4138
4139 /* \377 (0xff) is Latin1 'ÿ'. */
4140 "yfunc\377",
4141
4142 /* \377 (0xff) is Latin1 'ÿ'. */
4143 "\377",
4144 "\377\377123",
4145
4146 /* A name with all sorts of complications. Starts with "z" to make
4147 it easier for the completion tests below. */
4148 #define Z_SYM_NAME \
4149 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4150 "::tuple<(anonymous namespace)::ui*, " \
4151 "std::default_delete<(anonymous namespace)::ui>, void>"
4152
4153 Z_SYM_NAME
4154 };
4155
4156 /* Returns true if the mapped_index_base::find_name_component_bounds
4157 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4158 in completion mode. */
4159
4160 static bool
4161 check_find_bounds_finds (mapped_index_base &index,
4162 const char *search_name,
4163 gdb::array_view<const char *> expected_syms)
4164 {
4165 lookup_name_info lookup_name (search_name,
4166 symbol_name_match_type::FULL, true);
4167
4168 auto bounds = index.find_name_components_bounds (lookup_name,
4169 language_cplus);
4170
4171 size_t distance = std::distance (bounds.first, bounds.second);
4172 if (distance != expected_syms.size ())
4173 return false;
4174
4175 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4176 {
4177 auto nc_elem = bounds.first + exp_elem;
4178 const char *qualified = index.symbol_name_at (nc_elem->idx);
4179 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4180 return false;
4181 }
4182
4183 return true;
4184 }
4185
4186 /* Test the lower-level mapped_index::find_name_component_bounds
4187 method. */
4188
4189 static void
4190 test_mapped_index_find_name_component_bounds ()
4191 {
4192 mock_mapped_index mock_index (test_symbols);
4193
4194 mock_index.build_name_components ();
4195
4196 /* Test the lower-level mapped_index::find_name_component_bounds
4197 method in completion mode. */
4198 {
4199 static const char *expected_syms[] = {
4200 "t1_func",
4201 "t1_func1",
4202 };
4203
4204 SELF_CHECK (check_find_bounds_finds (mock_index,
4205 "t1_func", expected_syms));
4206 }
4207
4208 /* Check that the increment-last-char in the name matching algorithm
4209 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4210 {
4211 static const char *expected_syms1[] = {
4212 "\377",
4213 "\377\377123",
4214 };
4215 SELF_CHECK (check_find_bounds_finds (mock_index,
4216 "\377", expected_syms1));
4217
4218 static const char *expected_syms2[] = {
4219 "\377\377123",
4220 };
4221 SELF_CHECK (check_find_bounds_finds (mock_index,
4222 "\377\377", expected_syms2));
4223 }
4224 }
4225
4226 /* Test dw2_expand_symtabs_matching_symbol. */
4227
4228 static void
4229 test_dw2_expand_symtabs_matching_symbol ()
4230 {
4231 mock_mapped_index mock_index (test_symbols);
4232
4233 /* We let all tests run until the end even if some fails, for debug
4234 convenience. */
4235 bool any_mismatch = false;
4236
4237 /* Create the expected symbols list (an initializer_list). Needed
4238 because lists have commas, and we need to pass them to CHECK,
4239 which is a macro. */
4240 #define EXPECT(...) { __VA_ARGS__ }
4241
4242 /* Wrapper for check_match that passes down the current
4243 __FILE__/__LINE__. */
4244 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4245 any_mismatch |= !check_match (__FILE__, __LINE__, \
4246 mock_index, \
4247 NAME, MATCH_TYPE, COMPLETION_MODE, \
4248 EXPECTED_LIST)
4249
4250 /* Identity checks. */
4251 for (const char *sym : test_symbols)
4252 {
4253 /* Should be able to match all existing symbols. */
4254 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4255 EXPECT (sym));
4256
4257 /* Should be able to match all existing symbols with
4258 parameters. */
4259 std::string with_params = std::string (sym) + "(int)";
4260 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4261 EXPECT (sym));
4262
4263 /* Should be able to match all existing symbols with
4264 parameters and qualifiers. */
4265 with_params = std::string (sym) + " ( int ) const";
4266 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4267 EXPECT (sym));
4268
4269 /* This should really find sym, but cp-name-parser.y doesn't
4270 know about lvalue/rvalue qualifiers yet. */
4271 with_params = std::string (sym) + " ( int ) &&";
4272 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4273 {});
4274 }
4275
4276 /* Check that the name matching algorithm for completion doesn't get
4277 confused with Latin1 'ÿ' / 0xff. */
4278 {
4279 static const char str[] = "\377";
4280 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4281 EXPECT ("\377", "\377\377123"));
4282 }
4283
4284 /* Check that the increment-last-char in the matching algorithm for
4285 completion doesn't match "t1_fund" when completing "t1_func". */
4286 {
4287 static const char str[] = "t1_func";
4288 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4289 EXPECT ("t1_func", "t1_func1"));
4290 }
4291
4292 /* Check that completion mode works at each prefix of the expected
4293 symbol name. */
4294 {
4295 static const char str[] = "function(int)";
4296 size_t len = strlen (str);
4297 std::string lookup;
4298
4299 for (size_t i = 1; i < len; i++)
4300 {
4301 lookup.assign (str, i);
4302 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4303 EXPECT ("function"));
4304 }
4305 }
4306
4307 /* While "w" is a prefix of both components, the match function
4308 should still only be called once. */
4309 {
4310 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4311 EXPECT ("w1::w2"));
4312 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4313 EXPECT ("w1::w2"));
4314 }
4315
4316 /* Same, with a "complicated" symbol. */
4317 {
4318 static const char str[] = Z_SYM_NAME;
4319 size_t len = strlen (str);
4320 std::string lookup;
4321
4322 for (size_t i = 1; i < len; i++)
4323 {
4324 lookup.assign (str, i);
4325 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4326 EXPECT (Z_SYM_NAME));
4327 }
4328 }
4329
4330 /* In FULL mode, an incomplete symbol doesn't match. */
4331 {
4332 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4333 {});
4334 }
4335
4336 /* A complete symbol with parameters matches any overload, since the
4337 index has no overload info. */
4338 {
4339 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4340 EXPECT ("std::zfunction", "std::zfunction2"));
4341 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4342 EXPECT ("std::zfunction", "std::zfunction2"));
4343 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4344 EXPECT ("std::zfunction", "std::zfunction2"));
4345 }
4346
4347 /* Check that whitespace is ignored appropriately. A symbol with a
4348 template argument list. */
4349 {
4350 static const char expected[] = "ns::foo<int>";
4351 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4352 EXPECT (expected));
4353 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4354 EXPECT (expected));
4355 }
4356
4357 /* Check that whitespace is ignored appropriately. A symbol with a
4358 template argument list that includes a pointer. */
4359 {
4360 static const char expected[] = "ns::foo<char*>";
4361 /* Try both completion and non-completion modes. */
4362 static const bool completion_mode[2] = {false, true};
4363 for (size_t i = 0; i < 2; i++)
4364 {
4365 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4366 completion_mode[i], EXPECT (expected));
4367 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4368 completion_mode[i], EXPECT (expected));
4369
4370 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4371 completion_mode[i], EXPECT (expected));
4372 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4373 completion_mode[i], EXPECT (expected));
4374 }
4375 }
4376
4377 {
4378 /* Check method qualifiers are ignored. */
4379 static const char expected[] = "ns::foo<char*>";
4380 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4381 symbol_name_match_type::FULL, true, EXPECT (expected));
4382 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4383 symbol_name_match_type::FULL, true, EXPECT (expected));
4384 CHECK_MATCH ("foo < char * > ( int ) const",
4385 symbol_name_match_type::WILD, true, EXPECT (expected));
4386 CHECK_MATCH ("foo < char * > ( int ) &&",
4387 symbol_name_match_type::WILD, true, EXPECT (expected));
4388 }
4389
4390 /* Test lookup names that don't match anything. */
4391 {
4392 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4393 {});
4394
4395 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4396 {});
4397 }
4398
4399 /* Some wild matching tests, exercising "(anonymous namespace)",
4400 which should not be confused with a parameter list. */
4401 {
4402 static const char *syms[] = {
4403 "A::B::C",
4404 "B::C",
4405 "C",
4406 "A :: B :: C ( int )",
4407 "B :: C ( int )",
4408 "C ( int )",
4409 };
4410
4411 for (const char *s : syms)
4412 {
4413 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4414 EXPECT ("(anonymous namespace)::A::B::C"));
4415 }
4416 }
4417
4418 {
4419 static const char expected[] = "ns2::tmpl<int>::foo2";
4420 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4421 EXPECT (expected));
4422 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4423 EXPECT (expected));
4424 }
4425
4426 SELF_CHECK (!any_mismatch);
4427
4428 #undef EXPECT
4429 #undef CHECK_MATCH
4430 }
4431
4432 static void
4433 run_test ()
4434 {
4435 test_mapped_index_find_name_component_bounds ();
4436 test_dw2_expand_symtabs_matching_symbol ();
4437 }
4438
4439 }} // namespace selftests::dw2_expand_symtabs_matching
4440
4441 #endif /* GDB_SELF_TEST */
4442
4443 /* If FILE_MATCHER is NULL or if PER_CU has
4444 dwarf2_per_cu_quick_data::MARK set (see
4445 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4446 EXPANSION_NOTIFY on it. */
4447
4448 static void
4449 dw2_expand_symtabs_matching_one
4450 (struct dwarf2_per_cu_data *per_cu,
4451 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4452 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4453 {
4454 if (file_matcher == NULL || per_cu->v.quick->mark)
4455 {
4456 bool symtab_was_null
4457 = (per_cu->v.quick->compunit_symtab == NULL);
4458
4459 dw2_instantiate_symtab (per_cu, false);
4460
4461 if (expansion_notify != NULL
4462 && symtab_was_null
4463 && per_cu->v.quick->compunit_symtab != NULL)
4464 expansion_notify (per_cu->v.quick->compunit_symtab);
4465 }
4466 }
4467
4468 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4469 matched, to expand corresponding CUs that were marked. IDX is the
4470 index of the symbol name that matched. */
4471
4472 static void
4473 dw2_expand_marked_cus
4474 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4475 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4476 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4477 search_domain kind)
4478 {
4479 offset_type *vec, vec_len, vec_idx;
4480 bool global_seen = false;
4481 mapped_index &index = *dwarf2_per_objfile->index_table;
4482
4483 vec = (offset_type *) (index.constant_pool
4484 + MAYBE_SWAP (index.symbol_table[idx].vec));
4485 vec_len = MAYBE_SWAP (vec[0]);
4486 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4487 {
4488 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4489 /* This value is only valid for index versions >= 7. */
4490 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4491 gdb_index_symbol_kind symbol_kind =
4492 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4493 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4494 /* Only check the symbol attributes if they're present.
4495 Indices prior to version 7 don't record them,
4496 and indices >= 7 may elide them for certain symbols
4497 (gold does this). */
4498 int attrs_valid =
4499 (index.version >= 7
4500 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4501
4502 /* Work around gold/15646. */
4503 if (attrs_valid)
4504 {
4505 if (!is_static && global_seen)
4506 continue;
4507 if (!is_static)
4508 global_seen = true;
4509 }
4510
4511 /* Only check the symbol's kind if it has one. */
4512 if (attrs_valid)
4513 {
4514 switch (kind)
4515 {
4516 case VARIABLES_DOMAIN:
4517 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4518 continue;
4519 break;
4520 case FUNCTIONS_DOMAIN:
4521 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4522 continue;
4523 break;
4524 case TYPES_DOMAIN:
4525 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4526 continue;
4527 break;
4528 case MODULES_DOMAIN:
4529 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4530 continue;
4531 break;
4532 default:
4533 break;
4534 }
4535 }
4536
4537 /* Don't crash on bad data. */
4538 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4539 + dwarf2_per_objfile->all_type_units.size ()))
4540 {
4541 complaint (_(".gdb_index entry has bad CU index"
4542 " [in module %s]"),
4543 objfile_name (dwarf2_per_objfile->objfile));
4544 continue;
4545 }
4546
4547 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4548 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4549 expansion_notify);
4550 }
4551 }
4552
4553 /* If FILE_MATCHER is non-NULL, set all the
4554 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4555 that match FILE_MATCHER. */
4556
4557 static void
4558 dw_expand_symtabs_matching_file_matcher
4559 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4560 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4561 {
4562 if (file_matcher == NULL)
4563 return;
4564
4565 objfile *const objfile = dwarf2_per_objfile->objfile;
4566
4567 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4568 htab_eq_pointer,
4569 NULL, xcalloc, xfree));
4570 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4571 htab_eq_pointer,
4572 NULL, xcalloc, xfree));
4573
4574 /* The rule is CUs specify all the files, including those used by
4575 any TU, so there's no need to scan TUs here. */
4576
4577 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4578 {
4579 QUIT;
4580
4581 per_cu->v.quick->mark = 0;
4582
4583 /* We only need to look at symtabs not already expanded. */
4584 if (per_cu->v.quick->compunit_symtab)
4585 continue;
4586
4587 quick_file_names *file_data = dw2_get_file_names (per_cu);
4588 if (file_data == NULL)
4589 continue;
4590
4591 if (htab_find (visited_not_found.get (), file_data) != NULL)
4592 continue;
4593 else if (htab_find (visited_found.get (), file_data) != NULL)
4594 {
4595 per_cu->v.quick->mark = 1;
4596 continue;
4597 }
4598
4599 for (int j = 0; j < file_data->num_file_names; ++j)
4600 {
4601 const char *this_real_name;
4602
4603 if (file_matcher (file_data->file_names[j], false))
4604 {
4605 per_cu->v.quick->mark = 1;
4606 break;
4607 }
4608
4609 /* Before we invoke realpath, which can get expensive when many
4610 files are involved, do a quick comparison of the basenames. */
4611 if (!basenames_may_differ
4612 && !file_matcher (lbasename (file_data->file_names[j]),
4613 true))
4614 continue;
4615
4616 this_real_name = dw2_get_real_path (objfile, file_data, j);
4617 if (file_matcher (this_real_name, false))
4618 {
4619 per_cu->v.quick->mark = 1;
4620 break;
4621 }
4622 }
4623
4624 void **slot = htab_find_slot (per_cu->v.quick->mark
4625 ? visited_found.get ()
4626 : visited_not_found.get (),
4627 file_data, INSERT);
4628 *slot = file_data;
4629 }
4630 }
4631
4632 static void
4633 dw2_expand_symtabs_matching
4634 (struct objfile *objfile,
4635 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4636 const lookup_name_info *lookup_name,
4637 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4638 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4639 enum search_domain kind)
4640 {
4641 struct dwarf2_per_objfile *dwarf2_per_objfile
4642 = get_dwarf2_per_objfile (objfile);
4643
4644 /* index_table is NULL if OBJF_READNOW. */
4645 if (!dwarf2_per_objfile->index_table)
4646 return;
4647
4648 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4649
4650 if (symbol_matcher == NULL && lookup_name == NULL)
4651 {
4652 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4653 {
4654 QUIT;
4655
4656 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4657 expansion_notify);
4658 }
4659 return;
4660 }
4661
4662 mapped_index &index = *dwarf2_per_objfile->index_table;
4663
4664 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4665 symbol_matcher,
4666 kind, [&] (offset_type idx)
4667 {
4668 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4669 expansion_notify, kind);
4670 return true;
4671 });
4672 }
4673
4674 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4675 symtab. */
4676
4677 static struct compunit_symtab *
4678 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4679 CORE_ADDR pc)
4680 {
4681 int i;
4682
4683 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4684 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4685 return cust;
4686
4687 if (cust->includes == NULL)
4688 return NULL;
4689
4690 for (i = 0; cust->includes[i]; ++i)
4691 {
4692 struct compunit_symtab *s = cust->includes[i];
4693
4694 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4695 if (s != NULL)
4696 return s;
4697 }
4698
4699 return NULL;
4700 }
4701
4702 static struct compunit_symtab *
4703 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4704 struct bound_minimal_symbol msymbol,
4705 CORE_ADDR pc,
4706 struct obj_section *section,
4707 int warn_if_readin)
4708 {
4709 struct dwarf2_per_cu_data *data;
4710 struct compunit_symtab *result;
4711
4712 if (!objfile->partial_symtabs->psymtabs_addrmap)
4713 return NULL;
4714
4715 CORE_ADDR baseaddr = objfile->text_section_offset ();
4716 data = (struct dwarf2_per_cu_data *) addrmap_find
4717 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4718 if (!data)
4719 return NULL;
4720
4721 if (warn_if_readin && data->v.quick->compunit_symtab)
4722 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4723 paddress (objfile->arch (), pc));
4724
4725 result
4726 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4727 false),
4728 pc);
4729 gdb_assert (result != NULL);
4730 return result;
4731 }
4732
4733 static void
4734 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4735 void *data, int need_fullname)
4736 {
4737 struct dwarf2_per_objfile *dwarf2_per_objfile
4738 = get_dwarf2_per_objfile (objfile);
4739
4740 if (!dwarf2_per_objfile->filenames_cache)
4741 {
4742 dwarf2_per_objfile->filenames_cache.emplace ();
4743
4744 htab_up visited (htab_create_alloc (10,
4745 htab_hash_pointer, htab_eq_pointer,
4746 NULL, xcalloc, xfree));
4747
4748 /* The rule is CUs specify all the files, including those used
4749 by any TU, so there's no need to scan TUs here. We can
4750 ignore file names coming from already-expanded CUs. */
4751
4752 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4753 {
4754 if (per_cu->v.quick->compunit_symtab)
4755 {
4756 void **slot = htab_find_slot (visited.get (),
4757 per_cu->v.quick->file_names,
4758 INSERT);
4759
4760 *slot = per_cu->v.quick->file_names;
4761 }
4762 }
4763
4764 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4765 {
4766 /* We only need to look at symtabs not already expanded. */
4767 if (per_cu->v.quick->compunit_symtab)
4768 continue;
4769
4770 quick_file_names *file_data = dw2_get_file_names (per_cu);
4771 if (file_data == NULL)
4772 continue;
4773
4774 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4775 if (*slot)
4776 {
4777 /* Already visited. */
4778 continue;
4779 }
4780 *slot = file_data;
4781
4782 for (int j = 0; j < file_data->num_file_names; ++j)
4783 {
4784 const char *filename = file_data->file_names[j];
4785 dwarf2_per_objfile->filenames_cache->seen (filename);
4786 }
4787 }
4788 }
4789
4790 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4791 {
4792 gdb::unique_xmalloc_ptr<char> this_real_name;
4793
4794 if (need_fullname)
4795 this_real_name = gdb_realpath (filename);
4796 (*fun) (filename, this_real_name.get (), data);
4797 });
4798 }
4799
4800 static int
4801 dw2_has_symbols (struct objfile *objfile)
4802 {
4803 return 1;
4804 }
4805
4806 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4807 {
4808 dw2_has_symbols,
4809 dw2_find_last_source_symtab,
4810 dw2_forget_cached_source_info,
4811 dw2_map_symtabs_matching_filename,
4812 dw2_lookup_symbol,
4813 NULL,
4814 dw2_print_stats,
4815 dw2_dump,
4816 dw2_expand_symtabs_for_function,
4817 dw2_expand_all_symtabs,
4818 dw2_expand_symtabs_with_fullname,
4819 dw2_map_matching_symbols,
4820 dw2_expand_symtabs_matching,
4821 dw2_find_pc_sect_compunit_symtab,
4822 NULL,
4823 dw2_map_symbol_filenames
4824 };
4825
4826 /* DWARF-5 debug_names reader. */
4827
4828 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4829 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4830
4831 /* A helper function that reads the .debug_names section in SECTION
4832 and fills in MAP. FILENAME is the name of the file containing the
4833 section; it is used for error reporting.
4834
4835 Returns true if all went well, false otherwise. */
4836
4837 static bool
4838 read_debug_names_from_section (struct objfile *objfile,
4839 const char *filename,
4840 struct dwarf2_section_info *section,
4841 mapped_debug_names &map)
4842 {
4843 if (section->empty ())
4844 return false;
4845
4846 /* Older elfutils strip versions could keep the section in the main
4847 executable while splitting it for the separate debug info file. */
4848 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4849 return false;
4850
4851 section->read (objfile);
4852
4853 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4854
4855 const gdb_byte *addr = section->buffer;
4856
4857 bfd *const abfd = section->get_bfd_owner ();
4858
4859 unsigned int bytes_read;
4860 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4861 addr += bytes_read;
4862
4863 map.dwarf5_is_dwarf64 = bytes_read != 4;
4864 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4865 if (bytes_read + length != section->size)
4866 {
4867 /* There may be multiple per-CU indices. */
4868 warning (_("Section .debug_names in %s length %s does not match "
4869 "section length %s, ignoring .debug_names."),
4870 filename, plongest (bytes_read + length),
4871 pulongest (section->size));
4872 return false;
4873 }
4874
4875 /* The version number. */
4876 uint16_t version = read_2_bytes (abfd, addr);
4877 addr += 2;
4878 if (version != 5)
4879 {
4880 warning (_("Section .debug_names in %s has unsupported version %d, "
4881 "ignoring .debug_names."),
4882 filename, version);
4883 return false;
4884 }
4885
4886 /* Padding. */
4887 uint16_t padding = read_2_bytes (abfd, addr);
4888 addr += 2;
4889 if (padding != 0)
4890 {
4891 warning (_("Section .debug_names in %s has unsupported padding %d, "
4892 "ignoring .debug_names."),
4893 filename, padding);
4894 return false;
4895 }
4896
4897 /* comp_unit_count - The number of CUs in the CU list. */
4898 map.cu_count = read_4_bytes (abfd, addr);
4899 addr += 4;
4900
4901 /* local_type_unit_count - The number of TUs in the local TU
4902 list. */
4903 map.tu_count = read_4_bytes (abfd, addr);
4904 addr += 4;
4905
4906 /* foreign_type_unit_count - The number of TUs in the foreign TU
4907 list. */
4908 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4909 addr += 4;
4910 if (foreign_tu_count != 0)
4911 {
4912 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4913 "ignoring .debug_names."),
4914 filename, static_cast<unsigned long> (foreign_tu_count));
4915 return false;
4916 }
4917
4918 /* bucket_count - The number of hash buckets in the hash lookup
4919 table. */
4920 map.bucket_count = read_4_bytes (abfd, addr);
4921 addr += 4;
4922
4923 /* name_count - The number of unique names in the index. */
4924 map.name_count = read_4_bytes (abfd, addr);
4925 addr += 4;
4926
4927 /* abbrev_table_size - The size in bytes of the abbreviations
4928 table. */
4929 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4930 addr += 4;
4931
4932 /* augmentation_string_size - The size in bytes of the augmentation
4933 string. This value is rounded up to a multiple of 4. */
4934 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4935 addr += 4;
4936 map.augmentation_is_gdb = ((augmentation_string_size
4937 == sizeof (dwarf5_augmentation))
4938 && memcmp (addr, dwarf5_augmentation,
4939 sizeof (dwarf5_augmentation)) == 0);
4940 augmentation_string_size += (-augmentation_string_size) & 3;
4941 addr += augmentation_string_size;
4942
4943 /* List of CUs */
4944 map.cu_table_reordered = addr;
4945 addr += map.cu_count * map.offset_size;
4946
4947 /* List of Local TUs */
4948 map.tu_table_reordered = addr;
4949 addr += map.tu_count * map.offset_size;
4950
4951 /* Hash Lookup Table */
4952 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4953 addr += map.bucket_count * 4;
4954 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4955 addr += map.name_count * 4;
4956
4957 /* Name Table */
4958 map.name_table_string_offs_reordered = addr;
4959 addr += map.name_count * map.offset_size;
4960 map.name_table_entry_offs_reordered = addr;
4961 addr += map.name_count * map.offset_size;
4962
4963 const gdb_byte *abbrev_table_start = addr;
4964 for (;;)
4965 {
4966 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4967 addr += bytes_read;
4968 if (index_num == 0)
4969 break;
4970
4971 const auto insertpair
4972 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4973 if (!insertpair.second)
4974 {
4975 warning (_("Section .debug_names in %s has duplicate index %s, "
4976 "ignoring .debug_names."),
4977 filename, pulongest (index_num));
4978 return false;
4979 }
4980 mapped_debug_names::index_val &indexval = insertpair.first->second;
4981 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4982 addr += bytes_read;
4983
4984 for (;;)
4985 {
4986 mapped_debug_names::index_val::attr attr;
4987 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4988 addr += bytes_read;
4989 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4990 addr += bytes_read;
4991 if (attr.form == DW_FORM_implicit_const)
4992 {
4993 attr.implicit_const = read_signed_leb128 (abfd, addr,
4994 &bytes_read);
4995 addr += bytes_read;
4996 }
4997 if (attr.dw_idx == 0 && attr.form == 0)
4998 break;
4999 indexval.attr_vec.push_back (std::move (attr));
5000 }
5001 }
5002 if (addr != abbrev_table_start + abbrev_table_size)
5003 {
5004 warning (_("Section .debug_names in %s has abbreviation_table "
5005 "of size %s vs. written as %u, ignoring .debug_names."),
5006 filename, plongest (addr - abbrev_table_start),
5007 abbrev_table_size);
5008 return false;
5009 }
5010 map.entry_pool = addr;
5011
5012 return true;
5013 }
5014
5015 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5016 list. */
5017
5018 static void
5019 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5020 const mapped_debug_names &map,
5021 dwarf2_section_info &section,
5022 bool is_dwz)
5023 {
5024 sect_offset sect_off_prev;
5025 for (uint32_t i = 0; i <= map.cu_count; ++i)
5026 {
5027 sect_offset sect_off_next;
5028 if (i < map.cu_count)
5029 {
5030 sect_off_next
5031 = (sect_offset) (extract_unsigned_integer
5032 (map.cu_table_reordered + i * map.offset_size,
5033 map.offset_size,
5034 map.dwarf5_byte_order));
5035 }
5036 else
5037 sect_off_next = (sect_offset) section.size;
5038 if (i >= 1)
5039 {
5040 const ULONGEST length = sect_off_next - sect_off_prev;
5041 dwarf2_per_cu_data *per_cu
5042 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5043 sect_off_prev, length);
5044 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5045 }
5046 sect_off_prev = sect_off_next;
5047 }
5048 }
5049
5050 /* Read the CU list from the mapped index, and use it to create all
5051 the CU objects for this dwarf2_per_objfile. */
5052
5053 static void
5054 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5055 const mapped_debug_names &map,
5056 const mapped_debug_names &dwz_map)
5057 {
5058 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5059 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5060
5061 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5062 dwarf2_per_objfile->info,
5063 false /* is_dwz */);
5064
5065 if (dwz_map.cu_count == 0)
5066 return;
5067
5068 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5069 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5070 true /* is_dwz */);
5071 }
5072
5073 /* Read .debug_names. If everything went ok, initialize the "quick"
5074 elements of all the CUs and return true. Otherwise, return false. */
5075
5076 static bool
5077 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5078 {
5079 std::unique_ptr<mapped_debug_names> map
5080 (new mapped_debug_names (dwarf2_per_objfile));
5081 mapped_debug_names dwz_map (dwarf2_per_objfile);
5082 struct objfile *objfile = dwarf2_per_objfile->objfile;
5083
5084 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5085 &dwarf2_per_objfile->debug_names,
5086 *map))
5087 return false;
5088
5089 /* Don't use the index if it's empty. */
5090 if (map->name_count == 0)
5091 return false;
5092
5093 /* If there is a .dwz file, read it so we can get its CU list as
5094 well. */
5095 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5096 if (dwz != NULL)
5097 {
5098 if (!read_debug_names_from_section (objfile,
5099 bfd_get_filename (dwz->dwz_bfd.get ()),
5100 &dwz->debug_names, dwz_map))
5101 {
5102 warning (_("could not read '.debug_names' section from %s; skipping"),
5103 bfd_get_filename (dwz->dwz_bfd.get ()));
5104 return false;
5105 }
5106 }
5107
5108 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5109
5110 if (map->tu_count != 0)
5111 {
5112 /* We can only handle a single .debug_types when we have an
5113 index. */
5114 if (dwarf2_per_objfile->types.size () != 1)
5115 return false;
5116
5117 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5118
5119 create_signatured_type_table_from_debug_names
5120 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5121 }
5122
5123 create_addrmap_from_aranges (dwarf2_per_objfile,
5124 &dwarf2_per_objfile->debug_aranges);
5125
5126 dwarf2_per_objfile->debug_names_table = std::move (map);
5127 dwarf2_per_objfile->using_index = 1;
5128 dwarf2_per_objfile->quick_file_names_table =
5129 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5130
5131 return true;
5132 }
5133
5134 /* Type used to manage iterating over all CUs looking for a symbol for
5135 .debug_names. */
5136
5137 class dw2_debug_names_iterator
5138 {
5139 public:
5140 dw2_debug_names_iterator (const mapped_debug_names &map,
5141 gdb::optional<block_enum> block_index,
5142 domain_enum domain,
5143 const char *name)
5144 : m_map (map), m_block_index (block_index), m_domain (domain),
5145 m_addr (find_vec_in_debug_names (map, name))
5146 {}
5147
5148 dw2_debug_names_iterator (const mapped_debug_names &map,
5149 search_domain search, uint32_t namei)
5150 : m_map (map),
5151 m_search (search),
5152 m_addr (find_vec_in_debug_names (map, namei))
5153 {}
5154
5155 dw2_debug_names_iterator (const mapped_debug_names &map,
5156 block_enum block_index, domain_enum domain,
5157 uint32_t namei)
5158 : m_map (map), m_block_index (block_index), m_domain (domain),
5159 m_addr (find_vec_in_debug_names (map, namei))
5160 {}
5161
5162 /* Return the next matching CU or NULL if there are no more. */
5163 dwarf2_per_cu_data *next ();
5164
5165 private:
5166 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5167 const char *name);
5168 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5169 uint32_t namei);
5170
5171 /* The internalized form of .debug_names. */
5172 const mapped_debug_names &m_map;
5173
5174 /* If set, only look for symbols that match that block. Valid values are
5175 GLOBAL_BLOCK and STATIC_BLOCK. */
5176 const gdb::optional<block_enum> m_block_index;
5177
5178 /* The kind of symbol we're looking for. */
5179 const domain_enum m_domain = UNDEF_DOMAIN;
5180 const search_domain m_search = ALL_DOMAIN;
5181
5182 /* The list of CUs from the index entry of the symbol, or NULL if
5183 not found. */
5184 const gdb_byte *m_addr;
5185 };
5186
5187 const char *
5188 mapped_debug_names::namei_to_name (uint32_t namei) const
5189 {
5190 const ULONGEST namei_string_offs
5191 = extract_unsigned_integer ((name_table_string_offs_reordered
5192 + namei * offset_size),
5193 offset_size,
5194 dwarf5_byte_order);
5195 return read_indirect_string_at_offset (dwarf2_per_objfile,
5196 namei_string_offs);
5197 }
5198
5199 /* Find a slot in .debug_names for the object named NAME. If NAME is
5200 found, return pointer to its pool data. If NAME cannot be found,
5201 return NULL. */
5202
5203 const gdb_byte *
5204 dw2_debug_names_iterator::find_vec_in_debug_names
5205 (const mapped_debug_names &map, const char *name)
5206 {
5207 int (*cmp) (const char *, const char *);
5208
5209 gdb::unique_xmalloc_ptr<char> without_params;
5210 if (current_language->la_language == language_cplus
5211 || current_language->la_language == language_fortran
5212 || current_language->la_language == language_d)
5213 {
5214 /* NAME is already canonical. Drop any qualifiers as
5215 .debug_names does not contain any. */
5216
5217 if (strchr (name, '(') != NULL)
5218 {
5219 without_params = cp_remove_params (name);
5220 if (without_params != NULL)
5221 name = without_params.get ();
5222 }
5223 }
5224
5225 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5226
5227 const uint32_t full_hash = dwarf5_djb_hash (name);
5228 uint32_t namei
5229 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5230 (map.bucket_table_reordered
5231 + (full_hash % map.bucket_count)), 4,
5232 map.dwarf5_byte_order);
5233 if (namei == 0)
5234 return NULL;
5235 --namei;
5236 if (namei >= map.name_count)
5237 {
5238 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5239 "[in module %s]"),
5240 namei, map.name_count,
5241 objfile_name (map.dwarf2_per_objfile->objfile));
5242 return NULL;
5243 }
5244
5245 for (;;)
5246 {
5247 const uint32_t namei_full_hash
5248 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5249 (map.hash_table_reordered + namei), 4,
5250 map.dwarf5_byte_order);
5251 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5252 return NULL;
5253
5254 if (full_hash == namei_full_hash)
5255 {
5256 const char *const namei_string = map.namei_to_name (namei);
5257
5258 #if 0 /* An expensive sanity check. */
5259 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5260 {
5261 complaint (_("Wrong .debug_names hash for string at index %u "
5262 "[in module %s]"),
5263 namei, objfile_name (dwarf2_per_objfile->objfile));
5264 return NULL;
5265 }
5266 #endif
5267
5268 if (cmp (namei_string, name) == 0)
5269 {
5270 const ULONGEST namei_entry_offs
5271 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5272 + namei * map.offset_size),
5273 map.offset_size, map.dwarf5_byte_order);
5274 return map.entry_pool + namei_entry_offs;
5275 }
5276 }
5277
5278 ++namei;
5279 if (namei >= map.name_count)
5280 return NULL;
5281 }
5282 }
5283
5284 const gdb_byte *
5285 dw2_debug_names_iterator::find_vec_in_debug_names
5286 (const mapped_debug_names &map, uint32_t namei)
5287 {
5288 if (namei >= map.name_count)
5289 {
5290 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5291 "[in module %s]"),
5292 namei, map.name_count,
5293 objfile_name (map.dwarf2_per_objfile->objfile));
5294 return NULL;
5295 }
5296
5297 const ULONGEST namei_entry_offs
5298 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5299 + namei * map.offset_size),
5300 map.offset_size, map.dwarf5_byte_order);
5301 return map.entry_pool + namei_entry_offs;
5302 }
5303
5304 /* See dw2_debug_names_iterator. */
5305
5306 dwarf2_per_cu_data *
5307 dw2_debug_names_iterator::next ()
5308 {
5309 if (m_addr == NULL)
5310 return NULL;
5311
5312 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5313 struct objfile *objfile = dwarf2_per_objfile->objfile;
5314 bfd *const abfd = objfile->obfd;
5315
5316 again:
5317
5318 unsigned int bytes_read;
5319 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5320 m_addr += bytes_read;
5321 if (abbrev == 0)
5322 return NULL;
5323
5324 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5325 if (indexval_it == m_map.abbrev_map.cend ())
5326 {
5327 complaint (_("Wrong .debug_names undefined abbrev code %s "
5328 "[in module %s]"),
5329 pulongest (abbrev), objfile_name (objfile));
5330 return NULL;
5331 }
5332 const mapped_debug_names::index_val &indexval = indexval_it->second;
5333 enum class symbol_linkage {
5334 unknown,
5335 static_,
5336 extern_,
5337 } symbol_linkage_ = symbol_linkage::unknown;
5338 dwarf2_per_cu_data *per_cu = NULL;
5339 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5340 {
5341 ULONGEST ull;
5342 switch (attr.form)
5343 {
5344 case DW_FORM_implicit_const:
5345 ull = attr.implicit_const;
5346 break;
5347 case DW_FORM_flag_present:
5348 ull = 1;
5349 break;
5350 case DW_FORM_udata:
5351 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5352 m_addr += bytes_read;
5353 break;
5354 default:
5355 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5356 dwarf_form_name (attr.form),
5357 objfile_name (objfile));
5358 return NULL;
5359 }
5360 switch (attr.dw_idx)
5361 {
5362 case DW_IDX_compile_unit:
5363 /* Don't crash on bad data. */
5364 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5365 {
5366 complaint (_(".debug_names entry has bad CU index %s"
5367 " [in module %s]"),
5368 pulongest (ull),
5369 objfile_name (dwarf2_per_objfile->objfile));
5370 continue;
5371 }
5372 per_cu = dwarf2_per_objfile->get_cutu (ull);
5373 break;
5374 case DW_IDX_type_unit:
5375 /* Don't crash on bad data. */
5376 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5377 {
5378 complaint (_(".debug_names entry has bad TU index %s"
5379 " [in module %s]"),
5380 pulongest (ull),
5381 objfile_name (dwarf2_per_objfile->objfile));
5382 continue;
5383 }
5384 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5385 break;
5386 case DW_IDX_GNU_internal:
5387 if (!m_map.augmentation_is_gdb)
5388 break;
5389 symbol_linkage_ = symbol_linkage::static_;
5390 break;
5391 case DW_IDX_GNU_external:
5392 if (!m_map.augmentation_is_gdb)
5393 break;
5394 symbol_linkage_ = symbol_linkage::extern_;
5395 break;
5396 }
5397 }
5398
5399 /* Skip if already read in. */
5400 if (per_cu->v.quick->compunit_symtab)
5401 goto again;
5402
5403 /* Check static vs global. */
5404 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5405 {
5406 const bool want_static = *m_block_index == STATIC_BLOCK;
5407 const bool symbol_is_static =
5408 symbol_linkage_ == symbol_linkage::static_;
5409 if (want_static != symbol_is_static)
5410 goto again;
5411 }
5412
5413 /* Match dw2_symtab_iter_next, symbol_kind
5414 and debug_names::psymbol_tag. */
5415 switch (m_domain)
5416 {
5417 case VAR_DOMAIN:
5418 switch (indexval.dwarf_tag)
5419 {
5420 case DW_TAG_variable:
5421 case DW_TAG_subprogram:
5422 /* Some types are also in VAR_DOMAIN. */
5423 case DW_TAG_typedef:
5424 case DW_TAG_structure_type:
5425 break;
5426 default:
5427 goto again;
5428 }
5429 break;
5430 case STRUCT_DOMAIN:
5431 switch (indexval.dwarf_tag)
5432 {
5433 case DW_TAG_typedef:
5434 case DW_TAG_structure_type:
5435 break;
5436 default:
5437 goto again;
5438 }
5439 break;
5440 case LABEL_DOMAIN:
5441 switch (indexval.dwarf_tag)
5442 {
5443 case 0:
5444 case DW_TAG_variable:
5445 break;
5446 default:
5447 goto again;
5448 }
5449 break;
5450 case MODULE_DOMAIN:
5451 switch (indexval.dwarf_tag)
5452 {
5453 case DW_TAG_module:
5454 break;
5455 default:
5456 goto again;
5457 }
5458 break;
5459 default:
5460 break;
5461 }
5462
5463 /* Match dw2_expand_symtabs_matching, symbol_kind and
5464 debug_names::psymbol_tag. */
5465 switch (m_search)
5466 {
5467 case VARIABLES_DOMAIN:
5468 switch (indexval.dwarf_tag)
5469 {
5470 case DW_TAG_variable:
5471 break;
5472 default:
5473 goto again;
5474 }
5475 break;
5476 case FUNCTIONS_DOMAIN:
5477 switch (indexval.dwarf_tag)
5478 {
5479 case DW_TAG_subprogram:
5480 break;
5481 default:
5482 goto again;
5483 }
5484 break;
5485 case TYPES_DOMAIN:
5486 switch (indexval.dwarf_tag)
5487 {
5488 case DW_TAG_typedef:
5489 case DW_TAG_structure_type:
5490 break;
5491 default:
5492 goto again;
5493 }
5494 break;
5495 case MODULES_DOMAIN:
5496 switch (indexval.dwarf_tag)
5497 {
5498 case DW_TAG_module:
5499 break;
5500 default:
5501 goto again;
5502 }
5503 default:
5504 break;
5505 }
5506
5507 return per_cu;
5508 }
5509
5510 static struct compunit_symtab *
5511 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5512 const char *name, domain_enum domain)
5513 {
5514 struct dwarf2_per_objfile *dwarf2_per_objfile
5515 = get_dwarf2_per_objfile (objfile);
5516
5517 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5518 if (!mapp)
5519 {
5520 /* index is NULL if OBJF_READNOW. */
5521 return NULL;
5522 }
5523 const auto &map = *mapp;
5524
5525 dw2_debug_names_iterator iter (map, block_index, domain, name);
5526
5527 struct compunit_symtab *stab_best = NULL;
5528 struct dwarf2_per_cu_data *per_cu;
5529 while ((per_cu = iter.next ()) != NULL)
5530 {
5531 struct symbol *sym, *with_opaque = NULL;
5532 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5533 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5534 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5535
5536 sym = block_find_symbol (block, name, domain,
5537 block_find_non_opaque_type_preferred,
5538 &with_opaque);
5539
5540 /* Some caution must be observed with overloaded functions and
5541 methods, since the index will not contain any overload
5542 information (but NAME might contain it). */
5543
5544 if (sym != NULL
5545 && strcmp_iw (sym->search_name (), name) == 0)
5546 return stab;
5547 if (with_opaque != NULL
5548 && strcmp_iw (with_opaque->search_name (), name) == 0)
5549 stab_best = stab;
5550
5551 /* Keep looking through other CUs. */
5552 }
5553
5554 return stab_best;
5555 }
5556
5557 /* This dumps minimal information about .debug_names. It is called
5558 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5559 uses this to verify that .debug_names has been loaded. */
5560
5561 static void
5562 dw2_debug_names_dump (struct objfile *objfile)
5563 {
5564 struct dwarf2_per_objfile *dwarf2_per_objfile
5565 = get_dwarf2_per_objfile (objfile);
5566
5567 gdb_assert (dwarf2_per_objfile->using_index);
5568 printf_filtered (".debug_names:");
5569 if (dwarf2_per_objfile->debug_names_table)
5570 printf_filtered (" exists\n");
5571 else
5572 printf_filtered (" faked for \"readnow\"\n");
5573 printf_filtered ("\n");
5574 }
5575
5576 static void
5577 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5578 const char *func_name)
5579 {
5580 struct dwarf2_per_objfile *dwarf2_per_objfile
5581 = get_dwarf2_per_objfile (objfile);
5582
5583 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5584 if (dwarf2_per_objfile->debug_names_table)
5585 {
5586 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5587
5588 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5589
5590 struct dwarf2_per_cu_data *per_cu;
5591 while ((per_cu = iter.next ()) != NULL)
5592 dw2_instantiate_symtab (per_cu, false);
5593 }
5594 }
5595
5596 static void
5597 dw2_debug_names_map_matching_symbols
5598 (struct objfile *objfile,
5599 const lookup_name_info &name, domain_enum domain,
5600 int global,
5601 gdb::function_view<symbol_found_callback_ftype> callback,
5602 symbol_compare_ftype *ordered_compare)
5603 {
5604 struct dwarf2_per_objfile *dwarf2_per_objfile
5605 = get_dwarf2_per_objfile (objfile);
5606
5607 /* debug_names_table is NULL if OBJF_READNOW. */
5608 if (!dwarf2_per_objfile->debug_names_table)
5609 return;
5610
5611 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5612 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5613
5614 const char *match_name = name.ada ().lookup_name ().c_str ();
5615 auto matcher = [&] (const char *symname)
5616 {
5617 if (ordered_compare == nullptr)
5618 return true;
5619 return ordered_compare (symname, match_name) == 0;
5620 };
5621
5622 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5623 [&] (offset_type namei)
5624 {
5625 /* The name was matched, now expand corresponding CUs that were
5626 marked. */
5627 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5628
5629 struct dwarf2_per_cu_data *per_cu;
5630 while ((per_cu = iter.next ()) != NULL)
5631 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5632 return true;
5633 });
5634
5635 /* It's a shame we couldn't do this inside the
5636 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5637 that have already been expanded. Instead, this loop matches what
5638 the psymtab code does. */
5639 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5640 {
5641 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5642 if (cust != nullptr)
5643 {
5644 const struct block *block
5645 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5646 if (!iterate_over_symbols_terminated (block, name,
5647 domain, callback))
5648 break;
5649 }
5650 }
5651 }
5652
5653 static void
5654 dw2_debug_names_expand_symtabs_matching
5655 (struct objfile *objfile,
5656 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5657 const lookup_name_info *lookup_name,
5658 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5659 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5660 enum search_domain kind)
5661 {
5662 struct dwarf2_per_objfile *dwarf2_per_objfile
5663 = get_dwarf2_per_objfile (objfile);
5664
5665 /* debug_names_table is NULL if OBJF_READNOW. */
5666 if (!dwarf2_per_objfile->debug_names_table)
5667 return;
5668
5669 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5670
5671 if (symbol_matcher == NULL && lookup_name == NULL)
5672 {
5673 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5674 {
5675 QUIT;
5676
5677 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5678 expansion_notify);
5679 }
5680 return;
5681 }
5682
5683 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5684
5685 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5686 symbol_matcher,
5687 kind, [&] (offset_type namei)
5688 {
5689 /* The name was matched, now expand corresponding CUs that were
5690 marked. */
5691 dw2_debug_names_iterator iter (map, kind, namei);
5692
5693 struct dwarf2_per_cu_data *per_cu;
5694 while ((per_cu = iter.next ()) != NULL)
5695 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5696 expansion_notify);
5697 return true;
5698 });
5699 }
5700
5701 const struct quick_symbol_functions dwarf2_debug_names_functions =
5702 {
5703 dw2_has_symbols,
5704 dw2_find_last_source_symtab,
5705 dw2_forget_cached_source_info,
5706 dw2_map_symtabs_matching_filename,
5707 dw2_debug_names_lookup_symbol,
5708 NULL,
5709 dw2_print_stats,
5710 dw2_debug_names_dump,
5711 dw2_debug_names_expand_symtabs_for_function,
5712 dw2_expand_all_symtabs,
5713 dw2_expand_symtabs_with_fullname,
5714 dw2_debug_names_map_matching_symbols,
5715 dw2_debug_names_expand_symtabs_matching,
5716 dw2_find_pc_sect_compunit_symtab,
5717 NULL,
5718 dw2_map_symbol_filenames
5719 };
5720
5721 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5722 to either a dwarf2_per_objfile or dwz_file object. */
5723
5724 template <typename T>
5725 static gdb::array_view<const gdb_byte>
5726 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5727 {
5728 dwarf2_section_info *section = &section_owner->gdb_index;
5729
5730 if (section->empty ())
5731 return {};
5732
5733 /* Older elfutils strip versions could keep the section in the main
5734 executable while splitting it for the separate debug info file. */
5735 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5736 return {};
5737
5738 section->read (obj);
5739
5740 /* dwarf2_section_info::size is a bfd_size_type, while
5741 gdb::array_view works with size_t. On 32-bit hosts, with
5742 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5743 is 32-bit. So we need an explicit narrowing conversion here.
5744 This is fine, because it's impossible to allocate or mmap an
5745 array/buffer larger than what size_t can represent. */
5746 return gdb::make_array_view (section->buffer, section->size);
5747 }
5748
5749 /* Lookup the index cache for the contents of the index associated to
5750 DWARF2_OBJ. */
5751
5752 static gdb::array_view<const gdb_byte>
5753 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5754 {
5755 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5756 if (build_id == nullptr)
5757 return {};
5758
5759 return global_index_cache.lookup_gdb_index (build_id,
5760 &dwarf2_obj->index_cache_res);
5761 }
5762
5763 /* Same as the above, but for DWZ. */
5764
5765 static gdb::array_view<const gdb_byte>
5766 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5767 {
5768 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5769 if (build_id == nullptr)
5770 return {};
5771
5772 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5773 }
5774
5775 /* See symfile.h. */
5776
5777 bool
5778 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5779 {
5780 struct dwarf2_per_objfile *dwarf2_per_objfile
5781 = get_dwarf2_per_objfile (objfile);
5782
5783 /* If we're about to read full symbols, don't bother with the
5784 indices. In this case we also don't care if some other debug
5785 format is making psymtabs, because they are all about to be
5786 expanded anyway. */
5787 if ((objfile->flags & OBJF_READNOW))
5788 {
5789 dwarf2_per_objfile->using_index = 1;
5790 create_all_comp_units (dwarf2_per_objfile);
5791 create_all_type_units (dwarf2_per_objfile);
5792 dwarf2_per_objfile->quick_file_names_table
5793 = create_quick_file_names_table
5794 (dwarf2_per_objfile->all_comp_units.size ());
5795
5796 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5797 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5798 {
5799 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5800
5801 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5802 struct dwarf2_per_cu_quick_data);
5803 }
5804
5805 /* Return 1 so that gdb sees the "quick" functions. However,
5806 these functions will be no-ops because we will have expanded
5807 all symtabs. */
5808 *index_kind = dw_index_kind::GDB_INDEX;
5809 return true;
5810 }
5811
5812 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5813 {
5814 *index_kind = dw_index_kind::DEBUG_NAMES;
5815 return true;
5816 }
5817
5818 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5819 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5820 get_gdb_index_contents_from_section<dwz_file>))
5821 {
5822 *index_kind = dw_index_kind::GDB_INDEX;
5823 return true;
5824 }
5825
5826 /* ... otherwise, try to find the index in the index cache. */
5827 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5828 get_gdb_index_contents_from_cache,
5829 get_gdb_index_contents_from_cache_dwz))
5830 {
5831 global_index_cache.hit ();
5832 *index_kind = dw_index_kind::GDB_INDEX;
5833 return true;
5834 }
5835
5836 global_index_cache.miss ();
5837 return false;
5838 }
5839
5840 \f
5841
5842 /* Build a partial symbol table. */
5843
5844 void
5845 dwarf2_build_psymtabs (struct objfile *objfile)
5846 {
5847 struct dwarf2_per_objfile *dwarf2_per_objfile
5848 = get_dwarf2_per_objfile (objfile);
5849
5850 init_psymbol_list (objfile, 1024);
5851
5852 try
5853 {
5854 /* This isn't really ideal: all the data we allocate on the
5855 objfile's obstack is still uselessly kept around. However,
5856 freeing it seems unsafe. */
5857 psymtab_discarder psymtabs (objfile);
5858 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5859 psymtabs.keep ();
5860
5861 /* (maybe) store an index in the cache. */
5862 global_index_cache.store (dwarf2_per_objfile);
5863 }
5864 catch (const gdb_exception_error &except)
5865 {
5866 exception_print (gdb_stderr, except);
5867 }
5868 }
5869
5870 /* Find the base address of the compilation unit for range lists and
5871 location lists. It will normally be specified by DW_AT_low_pc.
5872 In DWARF-3 draft 4, the base address could be overridden by
5873 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5874 compilation units with discontinuous ranges. */
5875
5876 static void
5877 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5878 {
5879 struct attribute *attr;
5880
5881 cu->base_address.reset ();
5882
5883 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5884 if (attr != nullptr)
5885 cu->base_address = attr->value_as_address ();
5886 else
5887 {
5888 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5889 if (attr != nullptr)
5890 cu->base_address = attr->value_as_address ();
5891 }
5892 }
5893
5894 /* Helper function that returns the proper abbrev section for
5895 THIS_CU. */
5896
5897 static struct dwarf2_section_info *
5898 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5899 {
5900 struct dwarf2_section_info *abbrev;
5901 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5902
5903 if (this_cu->is_dwz)
5904 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5905 else
5906 abbrev = &dwarf2_per_objfile->abbrev;
5907
5908 return abbrev;
5909 }
5910
5911 /* Fetch the abbreviation table offset from a comp or type unit header. */
5912
5913 static sect_offset
5914 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5915 struct dwarf2_section_info *section,
5916 sect_offset sect_off)
5917 {
5918 bfd *abfd = section->get_bfd_owner ();
5919 const gdb_byte *info_ptr;
5920 unsigned int initial_length_size, offset_size;
5921 uint16_t version;
5922
5923 section->read (dwarf2_per_objfile->objfile);
5924 info_ptr = section->buffer + to_underlying (sect_off);
5925 read_initial_length (abfd, info_ptr, &initial_length_size);
5926 offset_size = initial_length_size == 4 ? 4 : 8;
5927 info_ptr += initial_length_size;
5928
5929 version = read_2_bytes (abfd, info_ptr);
5930 info_ptr += 2;
5931 if (version >= 5)
5932 {
5933 /* Skip unit type and address size. */
5934 info_ptr += 2;
5935 }
5936
5937 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5938 }
5939
5940 /* A partial symtab that is used only for include files. */
5941 struct dwarf2_include_psymtab : public partial_symtab
5942 {
5943 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5944 : partial_symtab (filename, objfile)
5945 {
5946 }
5947
5948 void read_symtab (struct objfile *objfile) override
5949 {
5950 /* It's an include file, no symbols to read for it.
5951 Everything is in the includer symtab. */
5952
5953 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5954 expansion of the includer psymtab. We use the dependencies[0] field to
5955 model the includer. But if we go the regular route of calling
5956 expand_psymtab here, and having expand_psymtab call expand_dependencies
5957 to expand the includer, we'll only use expand_psymtab on the includer
5958 (making it a non-toplevel psymtab), while if we expand the includer via
5959 another path, we'll use read_symtab (making it a toplevel psymtab).
5960 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5961 psymtab, and trigger read_symtab on the includer here directly. */
5962 includer ()->read_symtab (objfile);
5963 }
5964
5965 void expand_psymtab (struct objfile *objfile) override
5966 {
5967 /* This is not called by read_symtab, and should not be called by any
5968 expand_dependencies. */
5969 gdb_assert (false);
5970 }
5971
5972 bool readin_p () const override
5973 {
5974 return includer ()->readin_p ();
5975 }
5976
5977 struct compunit_symtab *get_compunit_symtab () const override
5978 {
5979 return nullptr;
5980 }
5981
5982 private:
5983 partial_symtab *includer () const
5984 {
5985 /* An include psymtab has exactly one dependency: the psymtab that
5986 includes it. */
5987 gdb_assert (this->number_of_dependencies == 1);
5988 return this->dependencies[0];
5989 }
5990 };
5991
5992 /* Allocate a new partial symtab for file named NAME and mark this new
5993 partial symtab as being an include of PST. */
5994
5995 static void
5996 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5997 struct objfile *objfile)
5998 {
5999 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6000
6001 if (!IS_ABSOLUTE_PATH (subpst->filename))
6002 {
6003 /* It shares objfile->objfile_obstack. */
6004 subpst->dirname = pst->dirname;
6005 }
6006
6007 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6008 subpst->dependencies[0] = pst;
6009 subpst->number_of_dependencies = 1;
6010 }
6011
6012 /* Read the Line Number Program data and extract the list of files
6013 included by the source file represented by PST. Build an include
6014 partial symtab for each of these included files. */
6015
6016 static void
6017 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6018 struct die_info *die,
6019 dwarf2_psymtab *pst)
6020 {
6021 line_header_up lh;
6022 struct attribute *attr;
6023
6024 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6025 if (attr != nullptr)
6026 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6027 if (lh == NULL)
6028 return; /* No linetable, so no includes. */
6029
6030 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6031 that we pass in the raw text_low here; that is ok because we're
6032 only decoding the line table to make include partial symtabs, and
6033 so the addresses aren't really used. */
6034 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6035 pst->raw_text_low (), 1);
6036 }
6037
6038 static hashval_t
6039 hash_signatured_type (const void *item)
6040 {
6041 const struct signatured_type *sig_type
6042 = (const struct signatured_type *) item;
6043
6044 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6045 return sig_type->signature;
6046 }
6047
6048 static int
6049 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6050 {
6051 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6052 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6053
6054 return lhs->signature == rhs->signature;
6055 }
6056
6057 /* Allocate a hash table for signatured types. */
6058
6059 static htab_up
6060 allocate_signatured_type_table ()
6061 {
6062 return htab_up (htab_create_alloc (41,
6063 hash_signatured_type,
6064 eq_signatured_type,
6065 NULL, xcalloc, xfree));
6066 }
6067
6068 /* A helper function to add a signatured type CU to a table. */
6069
6070 static int
6071 add_signatured_type_cu_to_table (void **slot, void *datum)
6072 {
6073 struct signatured_type *sigt = (struct signatured_type *) *slot;
6074 std::vector<signatured_type *> *all_type_units
6075 = (std::vector<signatured_type *> *) datum;
6076
6077 all_type_units->push_back (sigt);
6078
6079 return 1;
6080 }
6081
6082 /* A helper for create_debug_types_hash_table. Read types from SECTION
6083 and fill them into TYPES_HTAB. It will process only type units,
6084 therefore DW_UT_type. */
6085
6086 static void
6087 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6088 struct dwo_file *dwo_file,
6089 dwarf2_section_info *section, htab_up &types_htab,
6090 rcuh_kind section_kind)
6091 {
6092 struct objfile *objfile = dwarf2_per_objfile->objfile;
6093 struct dwarf2_section_info *abbrev_section;
6094 bfd *abfd;
6095 const gdb_byte *info_ptr, *end_ptr;
6096
6097 abbrev_section = (dwo_file != NULL
6098 ? &dwo_file->sections.abbrev
6099 : &dwarf2_per_objfile->abbrev);
6100
6101 if (dwarf_read_debug)
6102 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6103 section->get_name (),
6104 abbrev_section->get_file_name ());
6105
6106 section->read (objfile);
6107 info_ptr = section->buffer;
6108
6109 if (info_ptr == NULL)
6110 return;
6111
6112 /* We can't set abfd until now because the section may be empty or
6113 not present, in which case the bfd is unknown. */
6114 abfd = section->get_bfd_owner ();
6115
6116 /* We don't use cutu_reader here because we don't need to read
6117 any dies: the signature is in the header. */
6118
6119 end_ptr = info_ptr + section->size;
6120 while (info_ptr < end_ptr)
6121 {
6122 struct signatured_type *sig_type;
6123 struct dwo_unit *dwo_tu;
6124 void **slot;
6125 const gdb_byte *ptr = info_ptr;
6126 struct comp_unit_head header;
6127 unsigned int length;
6128
6129 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6130
6131 /* Initialize it due to a false compiler warning. */
6132 header.signature = -1;
6133 header.type_cu_offset_in_tu = (cu_offset) -1;
6134
6135 /* We need to read the type's signature in order to build the hash
6136 table, but we don't need anything else just yet. */
6137
6138 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6139 abbrev_section, ptr, section_kind);
6140
6141 length = header.get_length ();
6142
6143 /* Skip dummy type units. */
6144 if (ptr >= info_ptr + length
6145 || peek_abbrev_code (abfd, ptr) == 0
6146 || header.unit_type != DW_UT_type)
6147 {
6148 info_ptr += length;
6149 continue;
6150 }
6151
6152 if (types_htab == NULL)
6153 {
6154 if (dwo_file)
6155 types_htab = allocate_dwo_unit_table ();
6156 else
6157 types_htab = allocate_signatured_type_table ();
6158 }
6159
6160 if (dwo_file)
6161 {
6162 sig_type = NULL;
6163 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6164 struct dwo_unit);
6165 dwo_tu->dwo_file = dwo_file;
6166 dwo_tu->signature = header.signature;
6167 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6168 dwo_tu->section = section;
6169 dwo_tu->sect_off = sect_off;
6170 dwo_tu->length = length;
6171 }
6172 else
6173 {
6174 /* N.B.: type_offset is not usable if this type uses a DWO file.
6175 The real type_offset is in the DWO file. */
6176 dwo_tu = NULL;
6177 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6178 struct signatured_type);
6179 sig_type->signature = header.signature;
6180 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6181 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6182 sig_type->per_cu.is_debug_types = 1;
6183 sig_type->per_cu.section = section;
6184 sig_type->per_cu.sect_off = sect_off;
6185 sig_type->per_cu.length = length;
6186 }
6187
6188 slot = htab_find_slot (types_htab.get (),
6189 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6190 INSERT);
6191 gdb_assert (slot != NULL);
6192 if (*slot != NULL)
6193 {
6194 sect_offset dup_sect_off;
6195
6196 if (dwo_file)
6197 {
6198 const struct dwo_unit *dup_tu
6199 = (const struct dwo_unit *) *slot;
6200
6201 dup_sect_off = dup_tu->sect_off;
6202 }
6203 else
6204 {
6205 const struct signatured_type *dup_tu
6206 = (const struct signatured_type *) *slot;
6207
6208 dup_sect_off = dup_tu->per_cu.sect_off;
6209 }
6210
6211 complaint (_("debug type entry at offset %s is duplicate to"
6212 " the entry at offset %s, signature %s"),
6213 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6214 hex_string (header.signature));
6215 }
6216 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6217
6218 if (dwarf_read_debug > 1)
6219 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6220 sect_offset_str (sect_off),
6221 hex_string (header.signature));
6222
6223 info_ptr += length;
6224 }
6225 }
6226
6227 /* Create the hash table of all entries in the .debug_types
6228 (or .debug_types.dwo) section(s).
6229 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6230 otherwise it is NULL.
6231
6232 The result is a pointer to the hash table or NULL if there are no types.
6233
6234 Note: This function processes DWO files only, not DWP files. */
6235
6236 static void
6237 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6238 struct dwo_file *dwo_file,
6239 gdb::array_view<dwarf2_section_info> type_sections,
6240 htab_up &types_htab)
6241 {
6242 for (dwarf2_section_info &section : type_sections)
6243 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6244 types_htab, rcuh_kind::TYPE);
6245 }
6246
6247 /* Create the hash table of all entries in the .debug_types section,
6248 and initialize all_type_units.
6249 The result is zero if there is an error (e.g. missing .debug_types section),
6250 otherwise non-zero. */
6251
6252 static int
6253 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6254 {
6255 htab_up types_htab;
6256
6257 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6258 &dwarf2_per_objfile->info, types_htab,
6259 rcuh_kind::COMPILE);
6260 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6261 dwarf2_per_objfile->types, types_htab);
6262 if (types_htab == NULL)
6263 {
6264 dwarf2_per_objfile->signatured_types = NULL;
6265 return 0;
6266 }
6267
6268 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6269
6270 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6271 dwarf2_per_objfile->all_type_units.reserve
6272 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6273
6274 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6275 add_signatured_type_cu_to_table,
6276 &dwarf2_per_objfile->all_type_units);
6277
6278 return 1;
6279 }
6280
6281 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6282 If SLOT is non-NULL, it is the entry to use in the hash table.
6283 Otherwise we find one. */
6284
6285 static struct signatured_type *
6286 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6287 void **slot)
6288 {
6289 struct objfile *objfile = dwarf2_per_objfile->objfile;
6290
6291 if (dwarf2_per_objfile->all_type_units.size ()
6292 == dwarf2_per_objfile->all_type_units.capacity ())
6293 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6294
6295 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6296 struct signatured_type);
6297
6298 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6299 sig_type->signature = sig;
6300 sig_type->per_cu.is_debug_types = 1;
6301 if (dwarf2_per_objfile->using_index)
6302 {
6303 sig_type->per_cu.v.quick =
6304 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6305 struct dwarf2_per_cu_quick_data);
6306 }
6307
6308 if (slot == NULL)
6309 {
6310 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6311 sig_type, INSERT);
6312 }
6313 gdb_assert (*slot == NULL);
6314 *slot = sig_type;
6315 /* The rest of sig_type must be filled in by the caller. */
6316 return sig_type;
6317 }
6318
6319 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6320 Fill in SIG_ENTRY with DWO_ENTRY. */
6321
6322 static void
6323 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6324 struct signatured_type *sig_entry,
6325 struct dwo_unit *dwo_entry)
6326 {
6327 /* Make sure we're not clobbering something we don't expect to. */
6328 gdb_assert (! sig_entry->per_cu.queued);
6329 gdb_assert (sig_entry->per_cu.cu == NULL);
6330 if (dwarf2_per_objfile->using_index)
6331 {
6332 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6333 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6334 }
6335 else
6336 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6337 gdb_assert (sig_entry->signature == dwo_entry->signature);
6338 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6339 gdb_assert (sig_entry->type_unit_group == NULL);
6340 gdb_assert (sig_entry->dwo_unit == NULL);
6341
6342 sig_entry->per_cu.section = dwo_entry->section;
6343 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6344 sig_entry->per_cu.length = dwo_entry->length;
6345 sig_entry->per_cu.reading_dwo_directly = 1;
6346 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6347 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6348 sig_entry->dwo_unit = dwo_entry;
6349 }
6350
6351 /* Subroutine of lookup_signatured_type.
6352 If we haven't read the TU yet, create the signatured_type data structure
6353 for a TU to be read in directly from a DWO file, bypassing the stub.
6354 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6355 using .gdb_index, then when reading a CU we want to stay in the DWO file
6356 containing that CU. Otherwise we could end up reading several other DWO
6357 files (due to comdat folding) to process the transitive closure of all the
6358 mentioned TUs, and that can be slow. The current DWO file will have every
6359 type signature that it needs.
6360 We only do this for .gdb_index because in the psymtab case we already have
6361 to read all the DWOs to build the type unit groups. */
6362
6363 static struct signatured_type *
6364 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6365 {
6366 struct dwarf2_per_objfile *dwarf2_per_objfile
6367 = cu->per_cu->dwarf2_per_objfile;
6368 struct dwo_file *dwo_file;
6369 struct dwo_unit find_dwo_entry, *dwo_entry;
6370 struct signatured_type find_sig_entry, *sig_entry;
6371 void **slot;
6372
6373 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6374
6375 /* If TU skeletons have been removed then we may not have read in any
6376 TUs yet. */
6377 if (dwarf2_per_objfile->signatured_types == NULL)
6378 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6379
6380 /* We only ever need to read in one copy of a signatured type.
6381 Use the global signatured_types array to do our own comdat-folding
6382 of types. If this is the first time we're reading this TU, and
6383 the TU has an entry in .gdb_index, replace the recorded data from
6384 .gdb_index with this TU. */
6385
6386 find_sig_entry.signature = sig;
6387 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6388 &find_sig_entry, INSERT);
6389 sig_entry = (struct signatured_type *) *slot;
6390
6391 /* We can get here with the TU already read, *or* in the process of being
6392 read. Don't reassign the global entry to point to this DWO if that's
6393 the case. Also note that if the TU is already being read, it may not
6394 have come from a DWO, the program may be a mix of Fission-compiled
6395 code and non-Fission-compiled code. */
6396
6397 /* Have we already tried to read this TU?
6398 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6399 needn't exist in the global table yet). */
6400 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6401 return sig_entry;
6402
6403 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6404 dwo_unit of the TU itself. */
6405 dwo_file = cu->dwo_unit->dwo_file;
6406
6407 /* Ok, this is the first time we're reading this TU. */
6408 if (dwo_file->tus == NULL)
6409 return NULL;
6410 find_dwo_entry.signature = sig;
6411 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6412 &find_dwo_entry);
6413 if (dwo_entry == NULL)
6414 return NULL;
6415
6416 /* If the global table doesn't have an entry for this TU, add one. */
6417 if (sig_entry == NULL)
6418 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6419
6420 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6421 sig_entry->per_cu.tu_read = 1;
6422 return sig_entry;
6423 }
6424
6425 /* Subroutine of lookup_signatured_type.
6426 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6427 then try the DWP file. If the TU stub (skeleton) has been removed then
6428 it won't be in .gdb_index. */
6429
6430 static struct signatured_type *
6431 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6432 {
6433 struct dwarf2_per_objfile *dwarf2_per_objfile
6434 = cu->per_cu->dwarf2_per_objfile;
6435 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6436 struct dwo_unit *dwo_entry;
6437 struct signatured_type find_sig_entry, *sig_entry;
6438 void **slot;
6439
6440 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6441 gdb_assert (dwp_file != NULL);
6442
6443 /* If TU skeletons have been removed then we may not have read in any
6444 TUs yet. */
6445 if (dwarf2_per_objfile->signatured_types == NULL)
6446 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6447
6448 find_sig_entry.signature = sig;
6449 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6450 &find_sig_entry, INSERT);
6451 sig_entry = (struct signatured_type *) *slot;
6452
6453 /* Have we already tried to read this TU?
6454 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6455 needn't exist in the global table yet). */
6456 if (sig_entry != NULL)
6457 return sig_entry;
6458
6459 if (dwp_file->tus == NULL)
6460 return NULL;
6461 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6462 sig, 1 /* is_debug_types */);
6463 if (dwo_entry == NULL)
6464 return NULL;
6465
6466 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6467 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6468
6469 return sig_entry;
6470 }
6471
6472 /* Lookup a signature based type for DW_FORM_ref_sig8.
6473 Returns NULL if signature SIG is not present in the table.
6474 It is up to the caller to complain about this. */
6475
6476 static struct signatured_type *
6477 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6478 {
6479 struct dwarf2_per_objfile *dwarf2_per_objfile
6480 = cu->per_cu->dwarf2_per_objfile;
6481
6482 if (cu->dwo_unit
6483 && dwarf2_per_objfile->using_index)
6484 {
6485 /* We're in a DWO/DWP file, and we're using .gdb_index.
6486 These cases require special processing. */
6487 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6488 return lookup_dwo_signatured_type (cu, sig);
6489 else
6490 return lookup_dwp_signatured_type (cu, sig);
6491 }
6492 else
6493 {
6494 struct signatured_type find_entry, *entry;
6495
6496 if (dwarf2_per_objfile->signatured_types == NULL)
6497 return NULL;
6498 find_entry.signature = sig;
6499 entry = ((struct signatured_type *)
6500 htab_find (dwarf2_per_objfile->signatured_types.get (),
6501 &find_entry));
6502 return entry;
6503 }
6504 }
6505
6506 /* Low level DIE reading support. */
6507
6508 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6509
6510 static void
6511 init_cu_die_reader (struct die_reader_specs *reader,
6512 struct dwarf2_cu *cu,
6513 struct dwarf2_section_info *section,
6514 struct dwo_file *dwo_file,
6515 struct abbrev_table *abbrev_table)
6516 {
6517 gdb_assert (section->readin && section->buffer != NULL);
6518 reader->abfd = section->get_bfd_owner ();
6519 reader->cu = cu;
6520 reader->dwo_file = dwo_file;
6521 reader->die_section = section;
6522 reader->buffer = section->buffer;
6523 reader->buffer_end = section->buffer + section->size;
6524 reader->abbrev_table = abbrev_table;
6525 }
6526
6527 /* Subroutine of cutu_reader to simplify it.
6528 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6529 There's just a lot of work to do, and cutu_reader is big enough
6530 already.
6531
6532 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6533 from it to the DIE in the DWO. If NULL we are skipping the stub.
6534 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6535 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6536 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6537 STUB_COMP_DIR may be non-NULL.
6538 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6539 are filled in with the info of the DIE from the DWO file.
6540 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6541 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6542 kept around for at least as long as *RESULT_READER.
6543
6544 The result is non-zero if a valid (non-dummy) DIE was found. */
6545
6546 static int
6547 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6548 struct dwo_unit *dwo_unit,
6549 struct die_info *stub_comp_unit_die,
6550 const char *stub_comp_dir,
6551 struct die_reader_specs *result_reader,
6552 const gdb_byte **result_info_ptr,
6553 struct die_info **result_comp_unit_die,
6554 abbrev_table_up *result_dwo_abbrev_table)
6555 {
6556 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6557 struct objfile *objfile = dwarf2_per_objfile->objfile;
6558 struct dwarf2_cu *cu = this_cu->cu;
6559 bfd *abfd;
6560 const gdb_byte *begin_info_ptr, *info_ptr;
6561 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6562 int i,num_extra_attrs;
6563 struct dwarf2_section_info *dwo_abbrev_section;
6564 struct die_info *comp_unit_die;
6565
6566 /* At most one of these may be provided. */
6567 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6568
6569 /* These attributes aren't processed until later:
6570 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6571 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6572 referenced later. However, these attributes are found in the stub
6573 which we won't have later. In order to not impose this complication
6574 on the rest of the code, we read them here and copy them to the
6575 DWO CU/TU die. */
6576
6577 stmt_list = NULL;
6578 low_pc = NULL;
6579 high_pc = NULL;
6580 ranges = NULL;
6581 comp_dir = NULL;
6582
6583 if (stub_comp_unit_die != NULL)
6584 {
6585 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6586 DWO file. */
6587 if (! this_cu->is_debug_types)
6588 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6589 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6590 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6591 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6592 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6593
6594 cu->addr_base = stub_comp_unit_die->addr_base ();
6595
6596 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6597 here (if needed). We need the value before we can process
6598 DW_AT_ranges. */
6599 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6600 }
6601 else if (stub_comp_dir != NULL)
6602 {
6603 /* Reconstruct the comp_dir attribute to simplify the code below. */
6604 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6605 comp_dir->name = DW_AT_comp_dir;
6606 comp_dir->form = DW_FORM_string;
6607 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6608 DW_STRING (comp_dir) = stub_comp_dir;
6609 }
6610
6611 /* Set up for reading the DWO CU/TU. */
6612 cu->dwo_unit = dwo_unit;
6613 dwarf2_section_info *section = dwo_unit->section;
6614 section->read (objfile);
6615 abfd = section->get_bfd_owner ();
6616 begin_info_ptr = info_ptr = (section->buffer
6617 + to_underlying (dwo_unit->sect_off));
6618 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6619
6620 if (this_cu->is_debug_types)
6621 {
6622 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6623
6624 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6625 &cu->header, section,
6626 dwo_abbrev_section,
6627 info_ptr, rcuh_kind::TYPE);
6628 /* This is not an assert because it can be caused by bad debug info. */
6629 if (sig_type->signature != cu->header.signature)
6630 {
6631 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6632 " TU at offset %s [in module %s]"),
6633 hex_string (sig_type->signature),
6634 hex_string (cu->header.signature),
6635 sect_offset_str (dwo_unit->sect_off),
6636 bfd_get_filename (abfd));
6637 }
6638 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6639 /* For DWOs coming from DWP files, we don't know the CU length
6640 nor the type's offset in the TU until now. */
6641 dwo_unit->length = cu->header.get_length ();
6642 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6643
6644 /* Establish the type offset that can be used to lookup the type.
6645 For DWO files, we don't know it until now. */
6646 sig_type->type_offset_in_section
6647 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6648 }
6649 else
6650 {
6651 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6652 &cu->header, section,
6653 dwo_abbrev_section,
6654 info_ptr, rcuh_kind::COMPILE);
6655 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6656 /* For DWOs coming from DWP files, we don't know the CU length
6657 until now. */
6658 dwo_unit->length = cu->header.get_length ();
6659 }
6660
6661 *result_dwo_abbrev_table
6662 = abbrev_table::read (objfile, dwo_abbrev_section,
6663 cu->header.abbrev_sect_off);
6664 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6665 result_dwo_abbrev_table->get ());
6666
6667 /* Read in the die, but leave space to copy over the attributes
6668 from the stub. This has the benefit of simplifying the rest of
6669 the code - all the work to maintain the illusion of a single
6670 DW_TAG_{compile,type}_unit DIE is done here. */
6671 num_extra_attrs = ((stmt_list != NULL)
6672 + (low_pc != NULL)
6673 + (high_pc != NULL)
6674 + (ranges != NULL)
6675 + (comp_dir != NULL));
6676 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6677 num_extra_attrs);
6678
6679 /* Copy over the attributes from the stub to the DIE we just read in. */
6680 comp_unit_die = *result_comp_unit_die;
6681 i = comp_unit_die->num_attrs;
6682 if (stmt_list != NULL)
6683 comp_unit_die->attrs[i++] = *stmt_list;
6684 if (low_pc != NULL)
6685 comp_unit_die->attrs[i++] = *low_pc;
6686 if (high_pc != NULL)
6687 comp_unit_die->attrs[i++] = *high_pc;
6688 if (ranges != NULL)
6689 comp_unit_die->attrs[i++] = *ranges;
6690 if (comp_dir != NULL)
6691 comp_unit_die->attrs[i++] = *comp_dir;
6692 comp_unit_die->num_attrs += num_extra_attrs;
6693
6694 if (dwarf_die_debug)
6695 {
6696 fprintf_unfiltered (gdb_stdlog,
6697 "Read die from %s@0x%x of %s:\n",
6698 section->get_name (),
6699 (unsigned) (begin_info_ptr - section->buffer),
6700 bfd_get_filename (abfd));
6701 dump_die (comp_unit_die, dwarf_die_debug);
6702 }
6703
6704 /* Skip dummy compilation units. */
6705 if (info_ptr >= begin_info_ptr + dwo_unit->length
6706 || peek_abbrev_code (abfd, info_ptr) == 0)
6707 return 0;
6708
6709 *result_info_ptr = info_ptr;
6710 return 1;
6711 }
6712
6713 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6714 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6715 signature is part of the header. */
6716 static gdb::optional<ULONGEST>
6717 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6718 {
6719 if (cu->header.version >= 5)
6720 return cu->header.signature;
6721 struct attribute *attr;
6722 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6723 if (attr == nullptr)
6724 return gdb::optional<ULONGEST> ();
6725 return DW_UNSND (attr);
6726 }
6727
6728 /* Subroutine of cutu_reader to simplify it.
6729 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6730 Returns NULL if the specified DWO unit cannot be found. */
6731
6732 static struct dwo_unit *
6733 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6734 struct die_info *comp_unit_die,
6735 const char *dwo_name)
6736 {
6737 struct dwarf2_cu *cu = this_cu->cu;
6738 struct dwo_unit *dwo_unit;
6739 const char *comp_dir;
6740
6741 gdb_assert (cu != NULL);
6742
6743 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6744 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6745 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6746
6747 if (this_cu->is_debug_types)
6748 {
6749 struct signatured_type *sig_type;
6750
6751 /* Since this_cu is the first member of struct signatured_type,
6752 we can go from a pointer to one to a pointer to the other. */
6753 sig_type = (struct signatured_type *) this_cu;
6754 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6755 }
6756 else
6757 {
6758 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6759 if (!signature.has_value ())
6760 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6761 " [in module %s]"),
6762 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6763 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6764 *signature);
6765 }
6766
6767 return dwo_unit;
6768 }
6769
6770 /* Subroutine of cutu_reader to simplify it.
6771 See it for a description of the parameters.
6772 Read a TU directly from a DWO file, bypassing the stub. */
6773
6774 void
6775 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6776 int use_existing_cu)
6777 {
6778 struct signatured_type *sig_type;
6779
6780 /* Verify we can do the following downcast, and that we have the
6781 data we need. */
6782 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6783 sig_type = (struct signatured_type *) this_cu;
6784 gdb_assert (sig_type->dwo_unit != NULL);
6785
6786 if (use_existing_cu && this_cu->cu != NULL)
6787 {
6788 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6789 /* There's no need to do the rereading_dwo_cu handling that
6790 cutu_reader does since we don't read the stub. */
6791 }
6792 else
6793 {
6794 /* If !use_existing_cu, this_cu->cu must be NULL. */
6795 gdb_assert (this_cu->cu == NULL);
6796 m_new_cu.reset (new dwarf2_cu (this_cu));
6797 }
6798
6799 /* A future optimization, if needed, would be to use an existing
6800 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6801 could share abbrev tables. */
6802
6803 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6804 NULL /* stub_comp_unit_die */,
6805 sig_type->dwo_unit->dwo_file->comp_dir,
6806 this, &info_ptr,
6807 &comp_unit_die,
6808 &m_dwo_abbrev_table) == 0)
6809 {
6810 /* Dummy die. */
6811 dummy_p = true;
6812 }
6813 }
6814
6815 /* Initialize a CU (or TU) and read its DIEs.
6816 If the CU defers to a DWO file, read the DWO file as well.
6817
6818 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6819 Otherwise the table specified in the comp unit header is read in and used.
6820 This is an optimization for when we already have the abbrev table.
6821
6822 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6823 Otherwise, a new CU is allocated with xmalloc. */
6824
6825 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6826 struct abbrev_table *abbrev_table,
6827 int use_existing_cu,
6828 bool skip_partial)
6829 : die_reader_specs {},
6830 m_this_cu (this_cu)
6831 {
6832 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6833 struct objfile *objfile = dwarf2_per_objfile->objfile;
6834 struct dwarf2_section_info *section = this_cu->section;
6835 bfd *abfd = section->get_bfd_owner ();
6836 struct dwarf2_cu *cu;
6837 const gdb_byte *begin_info_ptr;
6838 struct signatured_type *sig_type = NULL;
6839 struct dwarf2_section_info *abbrev_section;
6840 /* Non-zero if CU currently points to a DWO file and we need to
6841 reread it. When this happens we need to reread the skeleton die
6842 before we can reread the DWO file (this only applies to CUs, not TUs). */
6843 int rereading_dwo_cu = 0;
6844
6845 if (dwarf_die_debug)
6846 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6847 this_cu->is_debug_types ? "type" : "comp",
6848 sect_offset_str (this_cu->sect_off));
6849
6850 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6851 file (instead of going through the stub), short-circuit all of this. */
6852 if (this_cu->reading_dwo_directly)
6853 {
6854 /* Narrow down the scope of possibilities to have to understand. */
6855 gdb_assert (this_cu->is_debug_types);
6856 gdb_assert (abbrev_table == NULL);
6857 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6858 return;
6859 }
6860
6861 /* This is cheap if the section is already read in. */
6862 section->read (objfile);
6863
6864 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6865
6866 abbrev_section = get_abbrev_section_for_cu (this_cu);
6867
6868 if (use_existing_cu && this_cu->cu != NULL)
6869 {
6870 cu = this_cu->cu;
6871 /* If this CU is from a DWO file we need to start over, we need to
6872 refetch the attributes from the skeleton CU.
6873 This could be optimized by retrieving those attributes from when we
6874 were here the first time: the previous comp_unit_die was stored in
6875 comp_unit_obstack. But there's no data yet that we need this
6876 optimization. */
6877 if (cu->dwo_unit != NULL)
6878 rereading_dwo_cu = 1;
6879 }
6880 else
6881 {
6882 /* If !use_existing_cu, this_cu->cu must be NULL. */
6883 gdb_assert (this_cu->cu == NULL);
6884 m_new_cu.reset (new dwarf2_cu (this_cu));
6885 cu = m_new_cu.get ();
6886 }
6887
6888 /* Get the header. */
6889 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6890 {
6891 /* We already have the header, there's no need to read it in again. */
6892 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6893 }
6894 else
6895 {
6896 if (this_cu->is_debug_types)
6897 {
6898 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6899 &cu->header, section,
6900 abbrev_section, info_ptr,
6901 rcuh_kind::TYPE);
6902
6903 /* Since per_cu is the first member of struct signatured_type,
6904 we can go from a pointer to one to a pointer to the other. */
6905 sig_type = (struct signatured_type *) this_cu;
6906 gdb_assert (sig_type->signature == cu->header.signature);
6907 gdb_assert (sig_type->type_offset_in_tu
6908 == cu->header.type_cu_offset_in_tu);
6909 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6910
6911 /* LENGTH has not been set yet for type units if we're
6912 using .gdb_index. */
6913 this_cu->length = cu->header.get_length ();
6914
6915 /* Establish the type offset that can be used to lookup the type. */
6916 sig_type->type_offset_in_section =
6917 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6918
6919 this_cu->dwarf_version = cu->header.version;
6920 }
6921 else
6922 {
6923 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6924 &cu->header, section,
6925 abbrev_section,
6926 info_ptr,
6927 rcuh_kind::COMPILE);
6928
6929 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6930 gdb_assert (this_cu->length == cu->header.get_length ());
6931 this_cu->dwarf_version = cu->header.version;
6932 }
6933 }
6934
6935 /* Skip dummy compilation units. */
6936 if (info_ptr >= begin_info_ptr + this_cu->length
6937 || peek_abbrev_code (abfd, info_ptr) == 0)
6938 {
6939 dummy_p = true;
6940 return;
6941 }
6942
6943 /* If we don't have them yet, read the abbrevs for this compilation unit.
6944 And if we need to read them now, make sure they're freed when we're
6945 done. */
6946 if (abbrev_table != NULL)
6947 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6948 else
6949 {
6950 m_abbrev_table_holder
6951 = abbrev_table::read (objfile, abbrev_section,
6952 cu->header.abbrev_sect_off);
6953 abbrev_table = m_abbrev_table_holder.get ();
6954 }
6955
6956 /* Read the top level CU/TU die. */
6957 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6958 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6959
6960 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6961 {
6962 dummy_p = true;
6963 return;
6964 }
6965
6966 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6967 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6968 table from the DWO file and pass the ownership over to us. It will be
6969 referenced from READER, so we must make sure to free it after we're done
6970 with READER.
6971
6972 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6973 DWO CU, that this test will fail (the attribute will not be present). */
6974 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6975 if (dwo_name != nullptr)
6976 {
6977 struct dwo_unit *dwo_unit;
6978 struct die_info *dwo_comp_unit_die;
6979
6980 if (comp_unit_die->has_children)
6981 {
6982 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6983 " has children (offset %s) [in module %s]"),
6984 sect_offset_str (this_cu->sect_off),
6985 bfd_get_filename (abfd));
6986 }
6987 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6988 if (dwo_unit != NULL)
6989 {
6990 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6991 comp_unit_die, NULL,
6992 this, &info_ptr,
6993 &dwo_comp_unit_die,
6994 &m_dwo_abbrev_table) == 0)
6995 {
6996 /* Dummy die. */
6997 dummy_p = true;
6998 return;
6999 }
7000 comp_unit_die = dwo_comp_unit_die;
7001 }
7002 else
7003 {
7004 /* Yikes, we couldn't find the rest of the DIE, we only have
7005 the stub. A complaint has already been logged. There's
7006 not much more we can do except pass on the stub DIE to
7007 die_reader_func. We don't want to throw an error on bad
7008 debug info. */
7009 }
7010 }
7011 }
7012
7013 void
7014 cutu_reader::keep ()
7015 {
7016 /* Done, clean up. */
7017 gdb_assert (!dummy_p);
7018 if (m_new_cu != NULL)
7019 {
7020 struct dwarf2_per_objfile *dwarf2_per_objfile
7021 = m_this_cu->dwarf2_per_objfile;
7022 /* Link this CU into read_in_chain. */
7023 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7024 dwarf2_per_objfile->read_in_chain = m_this_cu;
7025 /* The chain owns it now. */
7026 m_new_cu.release ();
7027 }
7028 }
7029
7030 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7031 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7032 assumed to have already done the lookup to find the DWO file).
7033
7034 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7035 THIS_CU->is_debug_types, but nothing else.
7036
7037 We fill in THIS_CU->length.
7038
7039 THIS_CU->cu is always freed when done.
7040 This is done in order to not leave THIS_CU->cu in a state where we have
7041 to care whether it refers to the "main" CU or the DWO CU.
7042
7043 When parent_cu is passed, it is used to provide a default value for
7044 str_offsets_base and addr_base from the parent. */
7045
7046 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7047 struct dwarf2_cu *parent_cu,
7048 struct dwo_file *dwo_file)
7049 : die_reader_specs {},
7050 m_this_cu (this_cu)
7051 {
7052 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7053 struct objfile *objfile = dwarf2_per_objfile->objfile;
7054 struct dwarf2_section_info *section = this_cu->section;
7055 bfd *abfd = section->get_bfd_owner ();
7056 struct dwarf2_section_info *abbrev_section;
7057 const gdb_byte *begin_info_ptr, *info_ptr;
7058
7059 if (dwarf_die_debug)
7060 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7061 this_cu->is_debug_types ? "type" : "comp",
7062 sect_offset_str (this_cu->sect_off));
7063
7064 gdb_assert (this_cu->cu == NULL);
7065
7066 abbrev_section = (dwo_file != NULL
7067 ? &dwo_file->sections.abbrev
7068 : get_abbrev_section_for_cu (this_cu));
7069
7070 /* This is cheap if the section is already read in. */
7071 section->read (objfile);
7072
7073 m_new_cu.reset (new dwarf2_cu (this_cu));
7074
7075 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7076 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7077 &m_new_cu->header, section,
7078 abbrev_section, info_ptr,
7079 (this_cu->is_debug_types
7080 ? rcuh_kind::TYPE
7081 : rcuh_kind::COMPILE));
7082
7083 if (parent_cu != nullptr)
7084 {
7085 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7086 m_new_cu->addr_base = parent_cu->addr_base;
7087 }
7088 this_cu->length = m_new_cu->header.get_length ();
7089
7090 /* Skip dummy compilation units. */
7091 if (info_ptr >= begin_info_ptr + this_cu->length
7092 || peek_abbrev_code (abfd, info_ptr) == 0)
7093 {
7094 dummy_p = true;
7095 return;
7096 }
7097
7098 m_abbrev_table_holder
7099 = abbrev_table::read (objfile, abbrev_section,
7100 m_new_cu->header.abbrev_sect_off);
7101
7102 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7103 m_abbrev_table_holder.get ());
7104 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7105 }
7106
7107 \f
7108 /* Type Unit Groups.
7109
7110 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7111 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7112 so that all types coming from the same compilation (.o file) are grouped
7113 together. A future step could be to put the types in the same symtab as
7114 the CU the types ultimately came from. */
7115
7116 static hashval_t
7117 hash_type_unit_group (const void *item)
7118 {
7119 const struct type_unit_group *tu_group
7120 = (const struct type_unit_group *) item;
7121
7122 return hash_stmt_list_entry (&tu_group->hash);
7123 }
7124
7125 static int
7126 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7127 {
7128 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7129 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7130
7131 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7132 }
7133
7134 /* Allocate a hash table for type unit groups. */
7135
7136 static htab_up
7137 allocate_type_unit_groups_table ()
7138 {
7139 return htab_up (htab_create_alloc (3,
7140 hash_type_unit_group,
7141 eq_type_unit_group,
7142 NULL, xcalloc, xfree));
7143 }
7144
7145 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7146 partial symtabs. We combine several TUs per psymtab to not let the size
7147 of any one psymtab grow too big. */
7148 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7149 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7150
7151 /* Helper routine for get_type_unit_group.
7152 Create the type_unit_group object used to hold one or more TUs. */
7153
7154 static struct type_unit_group *
7155 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7156 {
7157 struct dwarf2_per_objfile *dwarf2_per_objfile
7158 = cu->per_cu->dwarf2_per_objfile;
7159 struct objfile *objfile = dwarf2_per_objfile->objfile;
7160 struct dwarf2_per_cu_data *per_cu;
7161 struct type_unit_group *tu_group;
7162
7163 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7164 struct type_unit_group);
7165 per_cu = &tu_group->per_cu;
7166 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7167
7168 if (dwarf2_per_objfile->using_index)
7169 {
7170 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7171 struct dwarf2_per_cu_quick_data);
7172 }
7173 else
7174 {
7175 unsigned int line_offset = to_underlying (line_offset_struct);
7176 dwarf2_psymtab *pst;
7177 std::string name;
7178
7179 /* Give the symtab a useful name for debug purposes. */
7180 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7181 name = string_printf ("<type_units_%d>",
7182 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7183 else
7184 name = string_printf ("<type_units_at_0x%x>", line_offset);
7185
7186 pst = create_partial_symtab (per_cu, name.c_str ());
7187 pst->anonymous = true;
7188 }
7189
7190 tu_group->hash.dwo_unit = cu->dwo_unit;
7191 tu_group->hash.line_sect_off = line_offset_struct;
7192
7193 return tu_group;
7194 }
7195
7196 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7197 STMT_LIST is a DW_AT_stmt_list attribute. */
7198
7199 static struct type_unit_group *
7200 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7201 {
7202 struct dwarf2_per_objfile *dwarf2_per_objfile
7203 = cu->per_cu->dwarf2_per_objfile;
7204 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7205 struct type_unit_group *tu_group;
7206 void **slot;
7207 unsigned int line_offset;
7208 struct type_unit_group type_unit_group_for_lookup;
7209
7210 if (dwarf2_per_objfile->type_unit_groups == NULL)
7211 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
7212
7213 /* Do we need to create a new group, or can we use an existing one? */
7214
7215 if (stmt_list)
7216 {
7217 line_offset = DW_UNSND (stmt_list);
7218 ++tu_stats->nr_symtab_sharers;
7219 }
7220 else
7221 {
7222 /* Ugh, no stmt_list. Rare, but we have to handle it.
7223 We can do various things here like create one group per TU or
7224 spread them over multiple groups to split up the expansion work.
7225 To avoid worst case scenarios (too many groups or too large groups)
7226 we, umm, group them in bunches. */
7227 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7228 | (tu_stats->nr_stmt_less_type_units
7229 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7230 ++tu_stats->nr_stmt_less_type_units;
7231 }
7232
7233 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7234 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7235 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7236 &type_unit_group_for_lookup, INSERT);
7237 if (*slot != NULL)
7238 {
7239 tu_group = (struct type_unit_group *) *slot;
7240 gdb_assert (tu_group != NULL);
7241 }
7242 else
7243 {
7244 sect_offset line_offset_struct = (sect_offset) line_offset;
7245 tu_group = create_type_unit_group (cu, line_offset_struct);
7246 *slot = tu_group;
7247 ++tu_stats->nr_symtabs;
7248 }
7249
7250 return tu_group;
7251 }
7252 \f
7253 /* Partial symbol tables. */
7254
7255 /* Create a psymtab named NAME and assign it to PER_CU.
7256
7257 The caller must fill in the following details:
7258 dirname, textlow, texthigh. */
7259
7260 static dwarf2_psymtab *
7261 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7262 {
7263 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7264 dwarf2_psymtab *pst;
7265
7266 pst = new dwarf2_psymtab (name, objfile, per_cu);
7267
7268 pst->psymtabs_addrmap_supported = true;
7269
7270 /* This is the glue that links PST into GDB's symbol API. */
7271 per_cu->v.psymtab = pst;
7272
7273 return pst;
7274 }
7275
7276 /* DIE reader function for process_psymtab_comp_unit. */
7277
7278 static void
7279 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7280 const gdb_byte *info_ptr,
7281 struct die_info *comp_unit_die,
7282 enum language pretend_language)
7283 {
7284 struct dwarf2_cu *cu = reader->cu;
7285 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7286 struct gdbarch *gdbarch = objfile->arch ();
7287 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7288 CORE_ADDR baseaddr;
7289 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7290 dwarf2_psymtab *pst;
7291 enum pc_bounds_kind cu_bounds_kind;
7292 const char *filename;
7293
7294 gdb_assert (! per_cu->is_debug_types);
7295
7296 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7297
7298 /* Allocate a new partial symbol table structure. */
7299 gdb::unique_xmalloc_ptr<char> debug_filename;
7300 static const char artificial[] = "<artificial>";
7301 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7302 if (filename == NULL)
7303 filename = "";
7304 else if (strcmp (filename, artificial) == 0)
7305 {
7306 debug_filename.reset (concat (artificial, "@",
7307 sect_offset_str (per_cu->sect_off),
7308 (char *) NULL));
7309 filename = debug_filename.get ();
7310 }
7311
7312 pst = create_partial_symtab (per_cu, filename);
7313
7314 /* This must be done before calling dwarf2_build_include_psymtabs. */
7315 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7316
7317 baseaddr = objfile->text_section_offset ();
7318
7319 dwarf2_find_base_address (comp_unit_die, cu);
7320
7321 /* Possibly set the default values of LOWPC and HIGHPC from
7322 `DW_AT_ranges'. */
7323 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7324 &best_highpc, cu, pst);
7325 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7326 {
7327 CORE_ADDR low
7328 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7329 - baseaddr);
7330 CORE_ADDR high
7331 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7332 - baseaddr - 1);
7333 /* Store the contiguous range if it is not empty; it can be
7334 empty for CUs with no code. */
7335 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7336 low, high, pst);
7337 }
7338
7339 /* Check if comp unit has_children.
7340 If so, read the rest of the partial symbols from this comp unit.
7341 If not, there's no more debug_info for this comp unit. */
7342 if (comp_unit_die->has_children)
7343 {
7344 struct partial_die_info *first_die;
7345 CORE_ADDR lowpc, highpc;
7346
7347 lowpc = ((CORE_ADDR) -1);
7348 highpc = ((CORE_ADDR) 0);
7349
7350 first_die = load_partial_dies (reader, info_ptr, 1);
7351
7352 scan_partial_symbols (first_die, &lowpc, &highpc,
7353 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7354
7355 /* If we didn't find a lowpc, set it to highpc to avoid
7356 complaints from `maint check'. */
7357 if (lowpc == ((CORE_ADDR) -1))
7358 lowpc = highpc;
7359
7360 /* If the compilation unit didn't have an explicit address range,
7361 then use the information extracted from its child dies. */
7362 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7363 {
7364 best_lowpc = lowpc;
7365 best_highpc = highpc;
7366 }
7367 }
7368 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7369 best_lowpc + baseaddr)
7370 - baseaddr);
7371 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7372 best_highpc + baseaddr)
7373 - baseaddr);
7374
7375 end_psymtab_common (objfile, pst);
7376
7377 if (!cu->per_cu->imported_symtabs_empty ())
7378 {
7379 int i;
7380 int len = cu->per_cu->imported_symtabs_size ();
7381
7382 /* Fill in 'dependencies' here; we fill in 'users' in a
7383 post-pass. */
7384 pst->number_of_dependencies = len;
7385 pst->dependencies
7386 = objfile->partial_symtabs->allocate_dependencies (len);
7387 for (i = 0; i < len; ++i)
7388 {
7389 pst->dependencies[i]
7390 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7391 }
7392
7393 cu->per_cu->imported_symtabs_free ();
7394 }
7395
7396 /* Get the list of files included in the current compilation unit,
7397 and build a psymtab for each of them. */
7398 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7399
7400 if (dwarf_read_debug)
7401 fprintf_unfiltered (gdb_stdlog,
7402 "Psymtab for %s unit @%s: %s - %s"
7403 ", %d global, %d static syms\n",
7404 per_cu->is_debug_types ? "type" : "comp",
7405 sect_offset_str (per_cu->sect_off),
7406 paddress (gdbarch, pst->text_low (objfile)),
7407 paddress (gdbarch, pst->text_high (objfile)),
7408 pst->n_global_syms, pst->n_static_syms);
7409 }
7410
7411 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7412 Process compilation unit THIS_CU for a psymtab. */
7413
7414 static void
7415 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7416 bool want_partial_unit,
7417 enum language pretend_language)
7418 {
7419 /* If this compilation unit was already read in, free the
7420 cached copy in order to read it in again. This is
7421 necessary because we skipped some symbols when we first
7422 read in the compilation unit (see load_partial_dies).
7423 This problem could be avoided, but the benefit is unclear. */
7424 if (this_cu->cu != NULL)
7425 free_one_cached_comp_unit (this_cu);
7426
7427 cutu_reader reader (this_cu, NULL, 0, false);
7428
7429 switch (reader.comp_unit_die->tag)
7430 {
7431 case DW_TAG_compile_unit:
7432 this_cu->unit_type = DW_UT_compile;
7433 break;
7434 case DW_TAG_partial_unit:
7435 this_cu->unit_type = DW_UT_partial;
7436 break;
7437 default:
7438 abort ();
7439 }
7440
7441 if (reader.dummy_p)
7442 {
7443 /* Nothing. */
7444 }
7445 else if (this_cu->is_debug_types)
7446 build_type_psymtabs_reader (&reader, reader.info_ptr,
7447 reader.comp_unit_die);
7448 else if (want_partial_unit
7449 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7450 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7451 reader.comp_unit_die,
7452 pretend_language);
7453
7454 this_cu->lang = this_cu->cu->language;
7455
7456 /* Age out any secondary CUs. */
7457 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7458 }
7459
7460 /* Reader function for build_type_psymtabs. */
7461
7462 static void
7463 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7464 const gdb_byte *info_ptr,
7465 struct die_info *type_unit_die)
7466 {
7467 struct dwarf2_per_objfile *dwarf2_per_objfile
7468 = reader->cu->per_cu->dwarf2_per_objfile;
7469 struct objfile *objfile = dwarf2_per_objfile->objfile;
7470 struct dwarf2_cu *cu = reader->cu;
7471 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7472 struct signatured_type *sig_type;
7473 struct type_unit_group *tu_group;
7474 struct attribute *attr;
7475 struct partial_die_info *first_die;
7476 CORE_ADDR lowpc, highpc;
7477 dwarf2_psymtab *pst;
7478
7479 gdb_assert (per_cu->is_debug_types);
7480 sig_type = (struct signatured_type *) per_cu;
7481
7482 if (! type_unit_die->has_children)
7483 return;
7484
7485 attr = type_unit_die->attr (DW_AT_stmt_list);
7486 tu_group = get_type_unit_group (cu, attr);
7487
7488 if (tu_group->tus == nullptr)
7489 tu_group->tus = new std::vector<signatured_type *>;
7490 tu_group->tus->push_back (sig_type);
7491
7492 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7493 pst = create_partial_symtab (per_cu, "");
7494 pst->anonymous = true;
7495
7496 first_die = load_partial_dies (reader, info_ptr, 1);
7497
7498 lowpc = (CORE_ADDR) -1;
7499 highpc = (CORE_ADDR) 0;
7500 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7501
7502 end_psymtab_common (objfile, pst);
7503 }
7504
7505 /* Struct used to sort TUs by their abbreviation table offset. */
7506
7507 struct tu_abbrev_offset
7508 {
7509 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7510 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7511 {}
7512
7513 signatured_type *sig_type;
7514 sect_offset abbrev_offset;
7515 };
7516
7517 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7518
7519 static bool
7520 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7521 const struct tu_abbrev_offset &b)
7522 {
7523 return a.abbrev_offset < b.abbrev_offset;
7524 }
7525
7526 /* Efficiently read all the type units.
7527 This does the bulk of the work for build_type_psymtabs.
7528
7529 The efficiency is because we sort TUs by the abbrev table they use and
7530 only read each abbrev table once. In one program there are 200K TUs
7531 sharing 8K abbrev tables.
7532
7533 The main purpose of this function is to support building the
7534 dwarf2_per_objfile->type_unit_groups table.
7535 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7536 can collapse the search space by grouping them by stmt_list.
7537 The savings can be significant, in the same program from above the 200K TUs
7538 share 8K stmt_list tables.
7539
7540 FUNC is expected to call get_type_unit_group, which will create the
7541 struct type_unit_group if necessary and add it to
7542 dwarf2_per_objfile->type_unit_groups. */
7543
7544 static void
7545 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7546 {
7547 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7548 abbrev_table_up abbrev_table;
7549 sect_offset abbrev_offset;
7550
7551 /* It's up to the caller to not call us multiple times. */
7552 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7553
7554 if (dwarf2_per_objfile->all_type_units.empty ())
7555 return;
7556
7557 /* TUs typically share abbrev tables, and there can be way more TUs than
7558 abbrev tables. Sort by abbrev table to reduce the number of times we
7559 read each abbrev table in.
7560 Alternatives are to punt or to maintain a cache of abbrev tables.
7561 This is simpler and efficient enough for now.
7562
7563 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7564 symtab to use). Typically TUs with the same abbrev offset have the same
7565 stmt_list value too so in practice this should work well.
7566
7567 The basic algorithm here is:
7568
7569 sort TUs by abbrev table
7570 for each TU with same abbrev table:
7571 read abbrev table if first user
7572 read TU top level DIE
7573 [IWBN if DWO skeletons had DW_AT_stmt_list]
7574 call FUNC */
7575
7576 if (dwarf_read_debug)
7577 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7578
7579 /* Sort in a separate table to maintain the order of all_type_units
7580 for .gdb_index: TU indices directly index all_type_units. */
7581 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7582 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7583
7584 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7585 sorted_by_abbrev.emplace_back
7586 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7587 sig_type->per_cu.section,
7588 sig_type->per_cu.sect_off));
7589
7590 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7591 sort_tu_by_abbrev_offset);
7592
7593 abbrev_offset = (sect_offset) ~(unsigned) 0;
7594
7595 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7596 {
7597 /* Switch to the next abbrev table if necessary. */
7598 if (abbrev_table == NULL
7599 || tu.abbrev_offset != abbrev_offset)
7600 {
7601 abbrev_offset = tu.abbrev_offset;
7602 abbrev_table =
7603 abbrev_table::read (dwarf2_per_objfile->objfile,
7604 &dwarf2_per_objfile->abbrev,
7605 abbrev_offset);
7606 ++tu_stats->nr_uniq_abbrev_tables;
7607 }
7608
7609 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7610 0, false);
7611 if (!reader.dummy_p)
7612 build_type_psymtabs_reader (&reader, reader.info_ptr,
7613 reader.comp_unit_die);
7614 }
7615 }
7616
7617 /* Print collected type unit statistics. */
7618
7619 static void
7620 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7621 {
7622 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7623
7624 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7625 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7626 dwarf2_per_objfile->all_type_units.size ());
7627 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7628 tu_stats->nr_uniq_abbrev_tables);
7629 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7630 tu_stats->nr_symtabs);
7631 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7632 tu_stats->nr_symtab_sharers);
7633 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7634 tu_stats->nr_stmt_less_type_units);
7635 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7636 tu_stats->nr_all_type_units_reallocs);
7637 }
7638
7639 /* Traversal function for build_type_psymtabs. */
7640
7641 static int
7642 build_type_psymtab_dependencies (void **slot, void *info)
7643 {
7644 struct dwarf2_per_objfile *dwarf2_per_objfile
7645 = (struct dwarf2_per_objfile *) info;
7646 struct objfile *objfile = dwarf2_per_objfile->objfile;
7647 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7648 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7649 dwarf2_psymtab *pst = per_cu->v.psymtab;
7650 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7651 int i;
7652
7653 gdb_assert (len > 0);
7654 gdb_assert (per_cu->type_unit_group_p ());
7655
7656 pst->number_of_dependencies = len;
7657 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7658 for (i = 0; i < len; ++i)
7659 {
7660 struct signatured_type *iter = tu_group->tus->at (i);
7661 gdb_assert (iter->per_cu.is_debug_types);
7662 pst->dependencies[i] = iter->per_cu.v.psymtab;
7663 iter->type_unit_group = tu_group;
7664 }
7665
7666 delete tu_group->tus;
7667 tu_group->tus = nullptr;
7668
7669 return 1;
7670 }
7671
7672 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7673 Build partial symbol tables for the .debug_types comp-units. */
7674
7675 static void
7676 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7677 {
7678 if (! create_all_type_units (dwarf2_per_objfile))
7679 return;
7680
7681 build_type_psymtabs_1 (dwarf2_per_objfile);
7682 }
7683
7684 /* Traversal function for process_skeletonless_type_unit.
7685 Read a TU in a DWO file and build partial symbols for it. */
7686
7687 static int
7688 process_skeletonless_type_unit (void **slot, void *info)
7689 {
7690 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7691 struct dwarf2_per_objfile *dwarf2_per_objfile
7692 = (struct dwarf2_per_objfile *) info;
7693 struct signatured_type find_entry, *entry;
7694
7695 /* If this TU doesn't exist in the global table, add it and read it in. */
7696
7697 if (dwarf2_per_objfile->signatured_types == NULL)
7698 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
7699
7700 find_entry.signature = dwo_unit->signature;
7701 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7702 &find_entry, INSERT);
7703 /* If we've already seen this type there's nothing to do. What's happening
7704 is we're doing our own version of comdat-folding here. */
7705 if (*slot != NULL)
7706 return 1;
7707
7708 /* This does the job that create_all_type_units would have done for
7709 this TU. */
7710 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7711 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7712 *slot = entry;
7713
7714 /* This does the job that build_type_psymtabs_1 would have done. */
7715 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7716 if (!reader.dummy_p)
7717 build_type_psymtabs_reader (&reader, reader.info_ptr,
7718 reader.comp_unit_die);
7719
7720 return 1;
7721 }
7722
7723 /* Traversal function for process_skeletonless_type_units. */
7724
7725 static int
7726 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7727 {
7728 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7729
7730 if (dwo_file->tus != NULL)
7731 htab_traverse_noresize (dwo_file->tus.get (),
7732 process_skeletonless_type_unit, info);
7733
7734 return 1;
7735 }
7736
7737 /* Scan all TUs of DWO files, verifying we've processed them.
7738 This is needed in case a TU was emitted without its skeleton.
7739 Note: This can't be done until we know what all the DWO files are. */
7740
7741 static void
7742 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7743 {
7744 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7745 if (get_dwp_file (dwarf2_per_objfile) == NULL
7746 && dwarf2_per_objfile->dwo_files != NULL)
7747 {
7748 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7749 process_dwo_file_for_skeletonless_type_units,
7750 dwarf2_per_objfile);
7751 }
7752 }
7753
7754 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7755
7756 static void
7757 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7758 {
7759 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7760 {
7761 dwarf2_psymtab *pst = per_cu->v.psymtab;
7762
7763 if (pst == NULL)
7764 continue;
7765
7766 for (int j = 0; j < pst->number_of_dependencies; ++j)
7767 {
7768 /* Set the 'user' field only if it is not already set. */
7769 if (pst->dependencies[j]->user == NULL)
7770 pst->dependencies[j]->user = pst;
7771 }
7772 }
7773 }
7774
7775 /* Build the partial symbol table by doing a quick pass through the
7776 .debug_info and .debug_abbrev sections. */
7777
7778 static void
7779 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7780 {
7781 struct objfile *objfile = dwarf2_per_objfile->objfile;
7782
7783 if (dwarf_read_debug)
7784 {
7785 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7786 objfile_name (objfile));
7787 }
7788
7789 scoped_restore restore_reading_psyms
7790 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7791 true);
7792
7793 dwarf2_per_objfile->info.read (objfile);
7794
7795 /* Any cached compilation units will be linked by the per-objfile
7796 read_in_chain. Make sure to free them when we're done. */
7797 free_cached_comp_units freer (dwarf2_per_objfile);
7798
7799 build_type_psymtabs (dwarf2_per_objfile);
7800
7801 create_all_comp_units (dwarf2_per_objfile);
7802
7803 /* Create a temporary address map on a temporary obstack. We later
7804 copy this to the final obstack. */
7805 auto_obstack temp_obstack;
7806
7807 scoped_restore save_psymtabs_addrmap
7808 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7809 addrmap_create_mutable (&temp_obstack));
7810
7811 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7812 {
7813 if (per_cu->v.psymtab != NULL)
7814 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7815 continue;
7816 process_psymtab_comp_unit (per_cu, false, language_minimal);
7817 }
7818
7819 /* This has to wait until we read the CUs, we need the list of DWOs. */
7820 process_skeletonless_type_units (dwarf2_per_objfile);
7821
7822 /* Now that all TUs have been processed we can fill in the dependencies. */
7823 if (dwarf2_per_objfile->type_unit_groups != NULL)
7824 {
7825 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7826 build_type_psymtab_dependencies, dwarf2_per_objfile);
7827 }
7828
7829 if (dwarf_read_debug)
7830 print_tu_stats (dwarf2_per_objfile);
7831
7832 set_partial_user (dwarf2_per_objfile);
7833
7834 objfile->partial_symtabs->psymtabs_addrmap
7835 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7836 objfile->partial_symtabs->obstack ());
7837 /* At this point we want to keep the address map. */
7838 save_psymtabs_addrmap.release ();
7839
7840 if (dwarf_read_debug)
7841 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7842 objfile_name (objfile));
7843 }
7844
7845 /* Load the partial DIEs for a secondary CU into memory.
7846 This is also used when rereading a primary CU with load_all_dies. */
7847
7848 static void
7849 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7850 {
7851 cutu_reader reader (this_cu, NULL, 1, false);
7852
7853 if (!reader.dummy_p)
7854 {
7855 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7856 language_minimal);
7857
7858 /* Check if comp unit has_children.
7859 If so, read the rest of the partial symbols from this comp unit.
7860 If not, there's no more debug_info for this comp unit. */
7861 if (reader.comp_unit_die->has_children)
7862 load_partial_dies (&reader, reader.info_ptr, 0);
7863
7864 reader.keep ();
7865 }
7866 }
7867
7868 static void
7869 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7870 struct dwarf2_section_info *section,
7871 struct dwarf2_section_info *abbrev_section,
7872 unsigned int is_dwz)
7873 {
7874 const gdb_byte *info_ptr;
7875 struct objfile *objfile = dwarf2_per_objfile->objfile;
7876
7877 if (dwarf_read_debug)
7878 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7879 section->get_name (),
7880 section->get_file_name ());
7881
7882 section->read (objfile);
7883
7884 info_ptr = section->buffer;
7885
7886 while (info_ptr < section->buffer + section->size)
7887 {
7888 struct dwarf2_per_cu_data *this_cu;
7889
7890 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7891
7892 comp_unit_head cu_header;
7893 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7894 abbrev_section, info_ptr,
7895 rcuh_kind::COMPILE);
7896
7897 /* Save the compilation unit for later lookup. */
7898 if (cu_header.unit_type != DW_UT_type)
7899 {
7900 this_cu = XOBNEW (&objfile->objfile_obstack,
7901 struct dwarf2_per_cu_data);
7902 memset (this_cu, 0, sizeof (*this_cu));
7903 }
7904 else
7905 {
7906 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7907 struct signatured_type);
7908 memset (sig_type, 0, sizeof (*sig_type));
7909 sig_type->signature = cu_header.signature;
7910 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7911 this_cu = &sig_type->per_cu;
7912 }
7913 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7914 this_cu->sect_off = sect_off;
7915 this_cu->length = cu_header.length + cu_header.initial_length_size;
7916 this_cu->is_dwz = is_dwz;
7917 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7918 this_cu->section = section;
7919
7920 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7921
7922 info_ptr = info_ptr + this_cu->length;
7923 }
7924 }
7925
7926 /* Create a list of all compilation units in OBJFILE.
7927 This is only done for -readnow and building partial symtabs. */
7928
7929 static void
7930 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7931 {
7932 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7933 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7934 &dwarf2_per_objfile->abbrev, 0);
7935
7936 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7937 if (dwz != NULL)
7938 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7939 1);
7940 }
7941
7942 /* Process all loaded DIEs for compilation unit CU, starting at
7943 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7944 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7945 DW_AT_ranges). See the comments of add_partial_subprogram on how
7946 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7947
7948 static void
7949 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7950 CORE_ADDR *highpc, int set_addrmap,
7951 struct dwarf2_cu *cu)
7952 {
7953 struct partial_die_info *pdi;
7954
7955 /* Now, march along the PDI's, descending into ones which have
7956 interesting children but skipping the children of the other ones,
7957 until we reach the end of the compilation unit. */
7958
7959 pdi = first_die;
7960
7961 while (pdi != NULL)
7962 {
7963 pdi->fixup (cu);
7964
7965 /* Anonymous namespaces or modules have no name but have interesting
7966 children, so we need to look at them. Ditto for anonymous
7967 enums. */
7968
7969 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7970 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7971 || pdi->tag == DW_TAG_imported_unit
7972 || pdi->tag == DW_TAG_inlined_subroutine)
7973 {
7974 switch (pdi->tag)
7975 {
7976 case DW_TAG_subprogram:
7977 case DW_TAG_inlined_subroutine:
7978 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7979 break;
7980 case DW_TAG_constant:
7981 case DW_TAG_variable:
7982 case DW_TAG_typedef:
7983 case DW_TAG_union_type:
7984 if (!pdi->is_declaration
7985 || (pdi->tag == DW_TAG_variable && pdi->is_external))
7986 {
7987 add_partial_symbol (pdi, cu);
7988 }
7989 break;
7990 case DW_TAG_class_type:
7991 case DW_TAG_interface_type:
7992 case DW_TAG_structure_type:
7993 if (!pdi->is_declaration)
7994 {
7995 add_partial_symbol (pdi, cu);
7996 }
7997 if ((cu->language == language_rust
7998 || cu->language == language_cplus) && pdi->has_children)
7999 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8000 set_addrmap, cu);
8001 break;
8002 case DW_TAG_enumeration_type:
8003 if (!pdi->is_declaration)
8004 add_partial_enumeration (pdi, cu);
8005 break;
8006 case DW_TAG_base_type:
8007 case DW_TAG_subrange_type:
8008 /* File scope base type definitions are added to the partial
8009 symbol table. */
8010 add_partial_symbol (pdi, cu);
8011 break;
8012 case DW_TAG_namespace:
8013 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8014 break;
8015 case DW_TAG_module:
8016 if (!pdi->is_declaration)
8017 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8018 break;
8019 case DW_TAG_imported_unit:
8020 {
8021 struct dwarf2_per_cu_data *per_cu;
8022
8023 /* For now we don't handle imported units in type units. */
8024 if (cu->per_cu->is_debug_types)
8025 {
8026 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8027 " supported in type units [in module %s]"),
8028 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8029 }
8030
8031 per_cu = dwarf2_find_containing_comp_unit
8032 (pdi->d.sect_off, pdi->is_dwz,
8033 cu->per_cu->dwarf2_per_objfile);
8034
8035 /* Go read the partial unit, if needed. */
8036 if (per_cu->v.psymtab == NULL)
8037 process_psymtab_comp_unit (per_cu, true, cu->language);
8038
8039 cu->per_cu->imported_symtabs_push (per_cu);
8040 }
8041 break;
8042 case DW_TAG_imported_declaration:
8043 add_partial_symbol (pdi, cu);
8044 break;
8045 default:
8046 break;
8047 }
8048 }
8049
8050 /* If the die has a sibling, skip to the sibling. */
8051
8052 pdi = pdi->die_sibling;
8053 }
8054 }
8055
8056 /* Functions used to compute the fully scoped name of a partial DIE.
8057
8058 Normally, this is simple. For C++, the parent DIE's fully scoped
8059 name is concatenated with "::" and the partial DIE's name.
8060 Enumerators are an exception; they use the scope of their parent
8061 enumeration type, i.e. the name of the enumeration type is not
8062 prepended to the enumerator.
8063
8064 There are two complexities. One is DW_AT_specification; in this
8065 case "parent" means the parent of the target of the specification,
8066 instead of the direct parent of the DIE. The other is compilers
8067 which do not emit DW_TAG_namespace; in this case we try to guess
8068 the fully qualified name of structure types from their members'
8069 linkage names. This must be done using the DIE's children rather
8070 than the children of any DW_AT_specification target. We only need
8071 to do this for structures at the top level, i.e. if the target of
8072 any DW_AT_specification (if any; otherwise the DIE itself) does not
8073 have a parent. */
8074
8075 /* Compute the scope prefix associated with PDI's parent, in
8076 compilation unit CU. The result will be allocated on CU's
8077 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8078 field. NULL is returned if no prefix is necessary. */
8079 static const char *
8080 partial_die_parent_scope (struct partial_die_info *pdi,
8081 struct dwarf2_cu *cu)
8082 {
8083 const char *grandparent_scope;
8084 struct partial_die_info *parent, *real_pdi;
8085
8086 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8087 then this means the parent of the specification DIE. */
8088
8089 real_pdi = pdi;
8090 while (real_pdi->has_specification)
8091 {
8092 auto res = find_partial_die (real_pdi->spec_offset,
8093 real_pdi->spec_is_dwz, cu);
8094 real_pdi = res.pdi;
8095 cu = res.cu;
8096 }
8097
8098 parent = real_pdi->die_parent;
8099 if (parent == NULL)
8100 return NULL;
8101
8102 if (parent->scope_set)
8103 return parent->scope;
8104
8105 parent->fixup (cu);
8106
8107 grandparent_scope = partial_die_parent_scope (parent, cu);
8108
8109 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8110 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8111 Work around this problem here. */
8112 if (cu->language == language_cplus
8113 && parent->tag == DW_TAG_namespace
8114 && strcmp (parent->name, "::") == 0
8115 && grandparent_scope == NULL)
8116 {
8117 parent->scope = NULL;
8118 parent->scope_set = 1;
8119 return NULL;
8120 }
8121
8122 /* Nested subroutines in Fortran get a prefix. */
8123 if (pdi->tag == DW_TAG_enumerator)
8124 /* Enumerators should not get the name of the enumeration as a prefix. */
8125 parent->scope = grandparent_scope;
8126 else if (parent->tag == DW_TAG_namespace
8127 || parent->tag == DW_TAG_module
8128 || parent->tag == DW_TAG_structure_type
8129 || parent->tag == DW_TAG_class_type
8130 || parent->tag == DW_TAG_interface_type
8131 || parent->tag == DW_TAG_union_type
8132 || parent->tag == DW_TAG_enumeration_type
8133 || (cu->language == language_fortran
8134 && parent->tag == DW_TAG_subprogram
8135 && pdi->tag == DW_TAG_subprogram))
8136 {
8137 if (grandparent_scope == NULL)
8138 parent->scope = parent->name;
8139 else
8140 parent->scope = typename_concat (&cu->comp_unit_obstack,
8141 grandparent_scope,
8142 parent->name, 0, cu);
8143 }
8144 else
8145 {
8146 /* FIXME drow/2004-04-01: What should we be doing with
8147 function-local names? For partial symbols, we should probably be
8148 ignoring them. */
8149 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8150 dwarf_tag_name (parent->tag),
8151 sect_offset_str (pdi->sect_off));
8152 parent->scope = grandparent_scope;
8153 }
8154
8155 parent->scope_set = 1;
8156 return parent->scope;
8157 }
8158
8159 /* Return the fully scoped name associated with PDI, from compilation unit
8160 CU. The result will be allocated with malloc. */
8161
8162 static gdb::unique_xmalloc_ptr<char>
8163 partial_die_full_name (struct partial_die_info *pdi,
8164 struct dwarf2_cu *cu)
8165 {
8166 const char *parent_scope;
8167
8168 /* If this is a template instantiation, we can not work out the
8169 template arguments from partial DIEs. So, unfortunately, we have
8170 to go through the full DIEs. At least any work we do building
8171 types here will be reused if full symbols are loaded later. */
8172 if (pdi->has_template_arguments)
8173 {
8174 pdi->fixup (cu);
8175
8176 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8177 {
8178 struct die_info *die;
8179 struct attribute attr;
8180 struct dwarf2_cu *ref_cu = cu;
8181
8182 /* DW_FORM_ref_addr is using section offset. */
8183 attr.name = (enum dwarf_attribute) 0;
8184 attr.form = DW_FORM_ref_addr;
8185 attr.u.unsnd = to_underlying (pdi->sect_off);
8186 die = follow_die_ref (NULL, &attr, &ref_cu);
8187
8188 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8189 }
8190 }
8191
8192 parent_scope = partial_die_parent_scope (pdi, cu);
8193 if (parent_scope == NULL)
8194 return NULL;
8195 else
8196 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8197 pdi->name, 0, cu));
8198 }
8199
8200 static void
8201 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8202 {
8203 struct dwarf2_per_objfile *dwarf2_per_objfile
8204 = cu->per_cu->dwarf2_per_objfile;
8205 struct objfile *objfile = dwarf2_per_objfile->objfile;
8206 struct gdbarch *gdbarch = objfile->arch ();
8207 CORE_ADDR addr = 0;
8208 const char *actual_name = NULL;
8209 CORE_ADDR baseaddr;
8210
8211 baseaddr = objfile->text_section_offset ();
8212
8213 gdb::unique_xmalloc_ptr<char> built_actual_name
8214 = partial_die_full_name (pdi, cu);
8215 if (built_actual_name != NULL)
8216 actual_name = built_actual_name.get ();
8217
8218 if (actual_name == NULL)
8219 actual_name = pdi->name;
8220
8221 switch (pdi->tag)
8222 {
8223 case DW_TAG_inlined_subroutine:
8224 case DW_TAG_subprogram:
8225 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8226 - baseaddr);
8227 if (pdi->is_external
8228 || cu->language == language_ada
8229 || (cu->language == language_fortran
8230 && pdi->die_parent != NULL
8231 && pdi->die_parent->tag == DW_TAG_subprogram))
8232 {
8233 /* Normally, only "external" DIEs are part of the global scope.
8234 But in Ada and Fortran, we want to be able to access nested
8235 procedures globally. So all Ada and Fortran subprograms are
8236 stored in the global scope. */
8237 add_psymbol_to_list (actual_name,
8238 built_actual_name != NULL,
8239 VAR_DOMAIN, LOC_BLOCK,
8240 SECT_OFF_TEXT (objfile),
8241 psymbol_placement::GLOBAL,
8242 addr,
8243 cu->language, objfile);
8244 }
8245 else
8246 {
8247 add_psymbol_to_list (actual_name,
8248 built_actual_name != NULL,
8249 VAR_DOMAIN, LOC_BLOCK,
8250 SECT_OFF_TEXT (objfile),
8251 psymbol_placement::STATIC,
8252 addr, cu->language, objfile);
8253 }
8254
8255 if (pdi->main_subprogram && actual_name != NULL)
8256 set_objfile_main_name (objfile, actual_name, cu->language);
8257 break;
8258 case DW_TAG_constant:
8259 add_psymbol_to_list (actual_name,
8260 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8261 -1, (pdi->is_external
8262 ? psymbol_placement::GLOBAL
8263 : psymbol_placement::STATIC),
8264 0, cu->language, objfile);
8265 break;
8266 case DW_TAG_variable:
8267 if (pdi->d.locdesc)
8268 addr = decode_locdesc (pdi->d.locdesc, cu);
8269
8270 if (pdi->d.locdesc
8271 && addr == 0
8272 && !dwarf2_per_objfile->has_section_at_zero)
8273 {
8274 /* A global or static variable may also have been stripped
8275 out by the linker if unused, in which case its address
8276 will be nullified; do not add such variables into partial
8277 symbol table then. */
8278 }
8279 else if (pdi->is_external)
8280 {
8281 /* Global Variable.
8282 Don't enter into the minimal symbol tables as there is
8283 a minimal symbol table entry from the ELF symbols already.
8284 Enter into partial symbol table if it has a location
8285 descriptor or a type.
8286 If the location descriptor is missing, new_symbol will create
8287 a LOC_UNRESOLVED symbol, the address of the variable will then
8288 be determined from the minimal symbol table whenever the variable
8289 is referenced.
8290 The address for the partial symbol table entry is not
8291 used by GDB, but it comes in handy for debugging partial symbol
8292 table building. */
8293
8294 if (pdi->d.locdesc || pdi->has_type)
8295 add_psymbol_to_list (actual_name,
8296 built_actual_name != NULL,
8297 VAR_DOMAIN, LOC_STATIC,
8298 SECT_OFF_TEXT (objfile),
8299 psymbol_placement::GLOBAL,
8300 addr, cu->language, objfile);
8301 }
8302 else
8303 {
8304 int has_loc = pdi->d.locdesc != NULL;
8305
8306 /* Static Variable. Skip symbols whose value we cannot know (those
8307 without location descriptors or constant values). */
8308 if (!has_loc && !pdi->has_const_value)
8309 return;
8310
8311 add_psymbol_to_list (actual_name,
8312 built_actual_name != NULL,
8313 VAR_DOMAIN, LOC_STATIC,
8314 SECT_OFF_TEXT (objfile),
8315 psymbol_placement::STATIC,
8316 has_loc ? addr : 0,
8317 cu->language, objfile);
8318 }
8319 break;
8320 case DW_TAG_typedef:
8321 case DW_TAG_base_type:
8322 case DW_TAG_subrange_type:
8323 add_psymbol_to_list (actual_name,
8324 built_actual_name != NULL,
8325 VAR_DOMAIN, LOC_TYPEDEF, -1,
8326 psymbol_placement::STATIC,
8327 0, cu->language, objfile);
8328 break;
8329 case DW_TAG_imported_declaration:
8330 case DW_TAG_namespace:
8331 add_psymbol_to_list (actual_name,
8332 built_actual_name != NULL,
8333 VAR_DOMAIN, LOC_TYPEDEF, -1,
8334 psymbol_placement::GLOBAL,
8335 0, cu->language, objfile);
8336 break;
8337 case DW_TAG_module:
8338 /* With Fortran 77 there might be a "BLOCK DATA" module
8339 available without any name. If so, we skip the module as it
8340 doesn't bring any value. */
8341 if (actual_name != nullptr)
8342 add_psymbol_to_list (actual_name,
8343 built_actual_name != NULL,
8344 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8345 psymbol_placement::GLOBAL,
8346 0, cu->language, objfile);
8347 break;
8348 case DW_TAG_class_type:
8349 case DW_TAG_interface_type:
8350 case DW_TAG_structure_type:
8351 case DW_TAG_union_type:
8352 case DW_TAG_enumeration_type:
8353 /* Skip external references. The DWARF standard says in the section
8354 about "Structure, Union, and Class Type Entries": "An incomplete
8355 structure, union or class type is represented by a structure,
8356 union or class entry that does not have a byte size attribute
8357 and that has a DW_AT_declaration attribute." */
8358 if (!pdi->has_byte_size && pdi->is_declaration)
8359 return;
8360
8361 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8362 static vs. global. */
8363 add_psymbol_to_list (actual_name,
8364 built_actual_name != NULL,
8365 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8366 cu->language == language_cplus
8367 ? psymbol_placement::GLOBAL
8368 : psymbol_placement::STATIC,
8369 0, cu->language, objfile);
8370
8371 break;
8372 case DW_TAG_enumerator:
8373 add_psymbol_to_list (actual_name,
8374 built_actual_name != NULL,
8375 VAR_DOMAIN, LOC_CONST, -1,
8376 cu->language == language_cplus
8377 ? psymbol_placement::GLOBAL
8378 : psymbol_placement::STATIC,
8379 0, cu->language, objfile);
8380 break;
8381 default:
8382 break;
8383 }
8384 }
8385
8386 /* Read a partial die corresponding to a namespace; also, add a symbol
8387 corresponding to that namespace to the symbol table. NAMESPACE is
8388 the name of the enclosing namespace. */
8389
8390 static void
8391 add_partial_namespace (struct partial_die_info *pdi,
8392 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8393 int set_addrmap, struct dwarf2_cu *cu)
8394 {
8395 /* Add a symbol for the namespace. */
8396
8397 add_partial_symbol (pdi, cu);
8398
8399 /* Now scan partial symbols in that namespace. */
8400
8401 if (pdi->has_children)
8402 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8403 }
8404
8405 /* Read a partial die corresponding to a Fortran module. */
8406
8407 static void
8408 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8409 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8410 {
8411 /* Add a symbol for the namespace. */
8412
8413 add_partial_symbol (pdi, cu);
8414
8415 /* Now scan partial symbols in that module. */
8416
8417 if (pdi->has_children)
8418 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8419 }
8420
8421 /* Read a partial die corresponding to a subprogram or an inlined
8422 subprogram and create a partial symbol for that subprogram.
8423 When the CU language allows it, this routine also defines a partial
8424 symbol for each nested subprogram that this subprogram contains.
8425 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8426 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8427
8428 PDI may also be a lexical block, in which case we simply search
8429 recursively for subprograms defined inside that lexical block.
8430 Again, this is only performed when the CU language allows this
8431 type of definitions. */
8432
8433 static void
8434 add_partial_subprogram (struct partial_die_info *pdi,
8435 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8436 int set_addrmap, struct dwarf2_cu *cu)
8437 {
8438 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8439 {
8440 if (pdi->has_pc_info)
8441 {
8442 if (pdi->lowpc < *lowpc)
8443 *lowpc = pdi->lowpc;
8444 if (pdi->highpc > *highpc)
8445 *highpc = pdi->highpc;
8446 if (set_addrmap)
8447 {
8448 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8449 struct gdbarch *gdbarch = objfile->arch ();
8450 CORE_ADDR baseaddr;
8451 CORE_ADDR this_highpc;
8452 CORE_ADDR this_lowpc;
8453
8454 baseaddr = objfile->text_section_offset ();
8455 this_lowpc
8456 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8457 pdi->lowpc + baseaddr)
8458 - baseaddr);
8459 this_highpc
8460 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8461 pdi->highpc + baseaddr)
8462 - baseaddr);
8463 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8464 this_lowpc, this_highpc - 1,
8465 cu->per_cu->v.psymtab);
8466 }
8467 }
8468
8469 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8470 {
8471 if (!pdi->is_declaration)
8472 /* Ignore subprogram DIEs that do not have a name, they are
8473 illegal. Do not emit a complaint at this point, we will
8474 do so when we convert this psymtab into a symtab. */
8475 if (pdi->name)
8476 add_partial_symbol (pdi, cu);
8477 }
8478 }
8479
8480 if (! pdi->has_children)
8481 return;
8482
8483 if (cu->language == language_ada || cu->language == language_fortran)
8484 {
8485 pdi = pdi->die_child;
8486 while (pdi != NULL)
8487 {
8488 pdi->fixup (cu);
8489 if (pdi->tag == DW_TAG_subprogram
8490 || pdi->tag == DW_TAG_inlined_subroutine
8491 || pdi->tag == DW_TAG_lexical_block)
8492 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8493 pdi = pdi->die_sibling;
8494 }
8495 }
8496 }
8497
8498 /* Read a partial die corresponding to an enumeration type. */
8499
8500 static void
8501 add_partial_enumeration (struct partial_die_info *enum_pdi,
8502 struct dwarf2_cu *cu)
8503 {
8504 struct partial_die_info *pdi;
8505
8506 if (enum_pdi->name != NULL)
8507 add_partial_symbol (enum_pdi, cu);
8508
8509 pdi = enum_pdi->die_child;
8510 while (pdi)
8511 {
8512 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8513 complaint (_("malformed enumerator DIE ignored"));
8514 else
8515 add_partial_symbol (pdi, cu);
8516 pdi = pdi->die_sibling;
8517 }
8518 }
8519
8520 /* Return the initial uleb128 in the die at INFO_PTR. */
8521
8522 static unsigned int
8523 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8524 {
8525 unsigned int bytes_read;
8526
8527 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8528 }
8529
8530 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8531 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8532
8533 Return the corresponding abbrev, or NULL if the number is zero (indicating
8534 an empty DIE). In either case *BYTES_READ will be set to the length of
8535 the initial number. */
8536
8537 static struct abbrev_info *
8538 peek_die_abbrev (const die_reader_specs &reader,
8539 const gdb_byte *info_ptr, unsigned int *bytes_read)
8540 {
8541 dwarf2_cu *cu = reader.cu;
8542 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8543 unsigned int abbrev_number
8544 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8545
8546 if (abbrev_number == 0)
8547 return NULL;
8548
8549 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8550 if (!abbrev)
8551 {
8552 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8553 " at offset %s [in module %s]"),
8554 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8555 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8556 }
8557
8558 return abbrev;
8559 }
8560
8561 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8562 Returns a pointer to the end of a series of DIEs, terminated by an empty
8563 DIE. Any children of the skipped DIEs will also be skipped. */
8564
8565 static const gdb_byte *
8566 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8567 {
8568 while (1)
8569 {
8570 unsigned int bytes_read;
8571 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8572
8573 if (abbrev == NULL)
8574 return info_ptr + bytes_read;
8575 else
8576 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8577 }
8578 }
8579
8580 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8581 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8582 abbrev corresponding to that skipped uleb128 should be passed in
8583 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8584 children. */
8585
8586 static const gdb_byte *
8587 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8588 struct abbrev_info *abbrev)
8589 {
8590 unsigned int bytes_read;
8591 struct attribute attr;
8592 bfd *abfd = reader->abfd;
8593 struct dwarf2_cu *cu = reader->cu;
8594 const gdb_byte *buffer = reader->buffer;
8595 const gdb_byte *buffer_end = reader->buffer_end;
8596 unsigned int form, i;
8597
8598 for (i = 0; i < abbrev->num_attrs; i++)
8599 {
8600 /* The only abbrev we care about is DW_AT_sibling. */
8601 if (abbrev->attrs[i].name == DW_AT_sibling)
8602 {
8603 bool ignored;
8604 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8605 &ignored);
8606 if (attr.form == DW_FORM_ref_addr)
8607 complaint (_("ignoring absolute DW_AT_sibling"));
8608 else
8609 {
8610 sect_offset off = attr.get_ref_die_offset ();
8611 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8612
8613 if (sibling_ptr < info_ptr)
8614 complaint (_("DW_AT_sibling points backwards"));
8615 else if (sibling_ptr > reader->buffer_end)
8616 reader->die_section->overflow_complaint ();
8617 else
8618 return sibling_ptr;
8619 }
8620 }
8621
8622 /* If it isn't DW_AT_sibling, skip this attribute. */
8623 form = abbrev->attrs[i].form;
8624 skip_attribute:
8625 switch (form)
8626 {
8627 case DW_FORM_ref_addr:
8628 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8629 and later it is offset sized. */
8630 if (cu->header.version == 2)
8631 info_ptr += cu->header.addr_size;
8632 else
8633 info_ptr += cu->header.offset_size;
8634 break;
8635 case DW_FORM_GNU_ref_alt:
8636 info_ptr += cu->header.offset_size;
8637 break;
8638 case DW_FORM_addr:
8639 info_ptr += cu->header.addr_size;
8640 break;
8641 case DW_FORM_data1:
8642 case DW_FORM_ref1:
8643 case DW_FORM_flag:
8644 case DW_FORM_strx1:
8645 info_ptr += 1;
8646 break;
8647 case DW_FORM_flag_present:
8648 case DW_FORM_implicit_const:
8649 break;
8650 case DW_FORM_data2:
8651 case DW_FORM_ref2:
8652 case DW_FORM_strx2:
8653 info_ptr += 2;
8654 break;
8655 case DW_FORM_strx3:
8656 info_ptr += 3;
8657 break;
8658 case DW_FORM_data4:
8659 case DW_FORM_ref4:
8660 case DW_FORM_strx4:
8661 info_ptr += 4;
8662 break;
8663 case DW_FORM_data8:
8664 case DW_FORM_ref8:
8665 case DW_FORM_ref_sig8:
8666 info_ptr += 8;
8667 break;
8668 case DW_FORM_data16:
8669 info_ptr += 16;
8670 break;
8671 case DW_FORM_string:
8672 read_direct_string (abfd, info_ptr, &bytes_read);
8673 info_ptr += bytes_read;
8674 break;
8675 case DW_FORM_sec_offset:
8676 case DW_FORM_strp:
8677 case DW_FORM_GNU_strp_alt:
8678 info_ptr += cu->header.offset_size;
8679 break;
8680 case DW_FORM_exprloc:
8681 case DW_FORM_block:
8682 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8683 info_ptr += bytes_read;
8684 break;
8685 case DW_FORM_block1:
8686 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8687 break;
8688 case DW_FORM_block2:
8689 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8690 break;
8691 case DW_FORM_block4:
8692 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8693 break;
8694 case DW_FORM_addrx:
8695 case DW_FORM_strx:
8696 case DW_FORM_sdata:
8697 case DW_FORM_udata:
8698 case DW_FORM_ref_udata:
8699 case DW_FORM_GNU_addr_index:
8700 case DW_FORM_GNU_str_index:
8701 case DW_FORM_rnglistx:
8702 case DW_FORM_loclistx:
8703 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8704 break;
8705 case DW_FORM_indirect:
8706 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8707 info_ptr += bytes_read;
8708 /* We need to continue parsing from here, so just go back to
8709 the top. */
8710 goto skip_attribute;
8711
8712 default:
8713 error (_("Dwarf Error: Cannot handle %s "
8714 "in DWARF reader [in module %s]"),
8715 dwarf_form_name (form),
8716 bfd_get_filename (abfd));
8717 }
8718 }
8719
8720 if (abbrev->has_children)
8721 return skip_children (reader, info_ptr);
8722 else
8723 return info_ptr;
8724 }
8725
8726 /* Locate ORIG_PDI's sibling.
8727 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8728
8729 static const gdb_byte *
8730 locate_pdi_sibling (const struct die_reader_specs *reader,
8731 struct partial_die_info *orig_pdi,
8732 const gdb_byte *info_ptr)
8733 {
8734 /* Do we know the sibling already? */
8735
8736 if (orig_pdi->sibling)
8737 return orig_pdi->sibling;
8738
8739 /* Are there any children to deal with? */
8740
8741 if (!orig_pdi->has_children)
8742 return info_ptr;
8743
8744 /* Skip the children the long way. */
8745
8746 return skip_children (reader, info_ptr);
8747 }
8748
8749 /* Expand this partial symbol table into a full symbol table. SELF is
8750 not NULL. */
8751
8752 void
8753 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8754 {
8755 struct dwarf2_per_objfile *dwarf2_per_objfile
8756 = get_dwarf2_per_objfile (objfile);
8757
8758 gdb_assert (!readin);
8759 /* If this psymtab is constructed from a debug-only objfile, the
8760 has_section_at_zero flag will not necessarily be correct. We
8761 can get the correct value for this flag by looking at the data
8762 associated with the (presumably stripped) associated objfile. */
8763 if (objfile->separate_debug_objfile_backlink)
8764 {
8765 struct dwarf2_per_objfile *dpo_backlink
8766 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8767
8768 dwarf2_per_objfile->has_section_at_zero
8769 = dpo_backlink->has_section_at_zero;
8770 }
8771
8772 expand_psymtab (objfile);
8773
8774 process_cu_includes (dwarf2_per_objfile);
8775 }
8776 \f
8777 /* Reading in full CUs. */
8778
8779 /* Add PER_CU to the queue. */
8780
8781 static void
8782 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8783 enum language pretend_language)
8784 {
8785 per_cu->queued = 1;
8786 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8787 }
8788
8789 /* If PER_CU is not yet queued, add it to the queue.
8790 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8791 dependency.
8792 The result is non-zero if PER_CU was queued, otherwise the result is zero
8793 meaning either PER_CU is already queued or it is already loaded.
8794
8795 N.B. There is an invariant here that if a CU is queued then it is loaded.
8796 The caller is required to load PER_CU if we return non-zero. */
8797
8798 static int
8799 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8800 struct dwarf2_per_cu_data *per_cu,
8801 enum language pretend_language)
8802 {
8803 /* We may arrive here during partial symbol reading, if we need full
8804 DIEs to process an unusual case (e.g. template arguments). Do
8805 not queue PER_CU, just tell our caller to load its DIEs. */
8806 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8807 {
8808 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8809 return 1;
8810 return 0;
8811 }
8812
8813 /* Mark the dependence relation so that we don't flush PER_CU
8814 too early. */
8815 if (dependent_cu != NULL)
8816 dwarf2_add_dependence (dependent_cu, per_cu);
8817
8818 /* If it's already on the queue, we have nothing to do. */
8819 if (per_cu->queued)
8820 return 0;
8821
8822 /* If the compilation unit is already loaded, just mark it as
8823 used. */
8824 if (per_cu->cu != NULL)
8825 {
8826 per_cu->cu->last_used = 0;
8827 return 0;
8828 }
8829
8830 /* Add it to the queue. */
8831 queue_comp_unit (per_cu, pretend_language);
8832
8833 return 1;
8834 }
8835
8836 /* Process the queue. */
8837
8838 static void
8839 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8840 {
8841 if (dwarf_read_debug)
8842 {
8843 fprintf_unfiltered (gdb_stdlog,
8844 "Expanding one or more symtabs of objfile %s ...\n",
8845 objfile_name (dwarf2_per_objfile->objfile));
8846 }
8847
8848 /* The queue starts out with one item, but following a DIE reference
8849 may load a new CU, adding it to the end of the queue. */
8850 while (!dwarf2_per_objfile->queue.empty ())
8851 {
8852 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8853
8854 if ((dwarf2_per_objfile->using_index
8855 ? !item.per_cu->v.quick->compunit_symtab
8856 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8857 /* Skip dummy CUs. */
8858 && item.per_cu->cu != NULL)
8859 {
8860 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8861 unsigned int debug_print_threshold;
8862 char buf[100];
8863
8864 if (per_cu->is_debug_types)
8865 {
8866 struct signatured_type *sig_type =
8867 (struct signatured_type *) per_cu;
8868
8869 sprintf (buf, "TU %s at offset %s",
8870 hex_string (sig_type->signature),
8871 sect_offset_str (per_cu->sect_off));
8872 /* There can be 100s of TUs.
8873 Only print them in verbose mode. */
8874 debug_print_threshold = 2;
8875 }
8876 else
8877 {
8878 sprintf (buf, "CU at offset %s",
8879 sect_offset_str (per_cu->sect_off));
8880 debug_print_threshold = 1;
8881 }
8882
8883 if (dwarf_read_debug >= debug_print_threshold)
8884 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8885
8886 if (per_cu->is_debug_types)
8887 process_full_type_unit (per_cu, item.pretend_language);
8888 else
8889 process_full_comp_unit (per_cu, item.pretend_language);
8890
8891 if (dwarf_read_debug >= debug_print_threshold)
8892 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8893 }
8894
8895 item.per_cu->queued = 0;
8896 dwarf2_per_objfile->queue.pop ();
8897 }
8898
8899 if (dwarf_read_debug)
8900 {
8901 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8902 objfile_name (dwarf2_per_objfile->objfile));
8903 }
8904 }
8905
8906 /* Read in full symbols for PST, and anything it depends on. */
8907
8908 void
8909 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8910 {
8911 gdb_assert (!readin);
8912
8913 expand_dependencies (objfile);
8914
8915 dw2_do_instantiate_symtab (per_cu_data, false);
8916 gdb_assert (get_compunit_symtab () != nullptr);
8917 }
8918
8919 /* Trivial hash function for die_info: the hash value of a DIE
8920 is its offset in .debug_info for this objfile. */
8921
8922 static hashval_t
8923 die_hash (const void *item)
8924 {
8925 const struct die_info *die = (const struct die_info *) item;
8926
8927 return to_underlying (die->sect_off);
8928 }
8929
8930 /* Trivial comparison function for die_info structures: two DIEs
8931 are equal if they have the same offset. */
8932
8933 static int
8934 die_eq (const void *item_lhs, const void *item_rhs)
8935 {
8936 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8937 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8938
8939 return die_lhs->sect_off == die_rhs->sect_off;
8940 }
8941
8942 /* Load the DIEs associated with PER_CU into memory. */
8943
8944 static void
8945 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8946 bool skip_partial,
8947 enum language pretend_language)
8948 {
8949 gdb_assert (! this_cu->is_debug_types);
8950
8951 cutu_reader reader (this_cu, NULL, 1, skip_partial);
8952 if (reader.dummy_p)
8953 return;
8954
8955 struct dwarf2_cu *cu = reader.cu;
8956 const gdb_byte *info_ptr = reader.info_ptr;
8957
8958 gdb_assert (cu->die_hash == NULL);
8959 cu->die_hash =
8960 htab_create_alloc_ex (cu->header.length / 12,
8961 die_hash,
8962 die_eq,
8963 NULL,
8964 &cu->comp_unit_obstack,
8965 hashtab_obstack_allocate,
8966 dummy_obstack_deallocate);
8967
8968 if (reader.comp_unit_die->has_children)
8969 reader.comp_unit_die->child
8970 = read_die_and_siblings (&reader, reader.info_ptr,
8971 &info_ptr, reader.comp_unit_die);
8972 cu->dies = reader.comp_unit_die;
8973 /* comp_unit_die is not stored in die_hash, no need. */
8974
8975 /* We try not to read any attributes in this function, because not
8976 all CUs needed for references have been loaded yet, and symbol
8977 table processing isn't initialized. But we have to set the CU language,
8978 or we won't be able to build types correctly.
8979 Similarly, if we do not read the producer, we can not apply
8980 producer-specific interpretation. */
8981 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8982
8983 reader.keep ();
8984 }
8985
8986 /* Add a DIE to the delayed physname list. */
8987
8988 static void
8989 add_to_method_list (struct type *type, int fnfield_index, int index,
8990 const char *name, struct die_info *die,
8991 struct dwarf2_cu *cu)
8992 {
8993 struct delayed_method_info mi;
8994 mi.type = type;
8995 mi.fnfield_index = fnfield_index;
8996 mi.index = index;
8997 mi.name = name;
8998 mi.die = die;
8999 cu->method_list.push_back (mi);
9000 }
9001
9002 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9003 "const" / "volatile". If so, decrements LEN by the length of the
9004 modifier and return true. Otherwise return false. */
9005
9006 template<size_t N>
9007 static bool
9008 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9009 {
9010 size_t mod_len = sizeof (mod) - 1;
9011 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9012 {
9013 len -= mod_len;
9014 return true;
9015 }
9016 return false;
9017 }
9018
9019 /* Compute the physnames of any methods on the CU's method list.
9020
9021 The computation of method physnames is delayed in order to avoid the
9022 (bad) condition that one of the method's formal parameters is of an as yet
9023 incomplete type. */
9024
9025 static void
9026 compute_delayed_physnames (struct dwarf2_cu *cu)
9027 {
9028 /* Only C++ delays computing physnames. */
9029 if (cu->method_list.empty ())
9030 return;
9031 gdb_assert (cu->language == language_cplus);
9032
9033 for (const delayed_method_info &mi : cu->method_list)
9034 {
9035 const char *physname;
9036 struct fn_fieldlist *fn_flp
9037 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9038 physname = dwarf2_physname (mi.name, mi.die, cu);
9039 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9040 = physname ? physname : "";
9041
9042 /* Since there's no tag to indicate whether a method is a
9043 const/volatile overload, extract that information out of the
9044 demangled name. */
9045 if (physname != NULL)
9046 {
9047 size_t len = strlen (physname);
9048
9049 while (1)
9050 {
9051 if (physname[len] == ')') /* shortcut */
9052 break;
9053 else if (check_modifier (physname, len, " const"))
9054 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9055 else if (check_modifier (physname, len, " volatile"))
9056 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9057 else
9058 break;
9059 }
9060 }
9061 }
9062
9063 /* The list is no longer needed. */
9064 cu->method_list.clear ();
9065 }
9066
9067 /* Go objects should be embedded in a DW_TAG_module DIE,
9068 and it's not clear if/how imported objects will appear.
9069 To keep Go support simple until that's worked out,
9070 go back through what we've read and create something usable.
9071 We could do this while processing each DIE, and feels kinda cleaner,
9072 but that way is more invasive.
9073 This is to, for example, allow the user to type "p var" or "b main"
9074 without having to specify the package name, and allow lookups
9075 of module.object to work in contexts that use the expression
9076 parser. */
9077
9078 static void
9079 fixup_go_packaging (struct dwarf2_cu *cu)
9080 {
9081 gdb::unique_xmalloc_ptr<char> package_name;
9082 struct pending *list;
9083 int i;
9084
9085 for (list = *cu->get_builder ()->get_global_symbols ();
9086 list != NULL;
9087 list = list->next)
9088 {
9089 for (i = 0; i < list->nsyms; ++i)
9090 {
9091 struct symbol *sym = list->symbol[i];
9092
9093 if (sym->language () == language_go
9094 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9095 {
9096 gdb::unique_xmalloc_ptr<char> this_package_name
9097 (go_symbol_package_name (sym));
9098
9099 if (this_package_name == NULL)
9100 continue;
9101 if (package_name == NULL)
9102 package_name = std::move (this_package_name);
9103 else
9104 {
9105 struct objfile *objfile
9106 = cu->per_cu->dwarf2_per_objfile->objfile;
9107 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9108 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9109 (symbol_symtab (sym) != NULL
9110 ? symtab_to_filename_for_display
9111 (symbol_symtab (sym))
9112 : objfile_name (objfile)),
9113 this_package_name.get (), package_name.get ());
9114 }
9115 }
9116 }
9117 }
9118
9119 if (package_name != NULL)
9120 {
9121 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9122 const char *saved_package_name = objfile->intern (package_name.get ());
9123 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9124 saved_package_name);
9125 struct symbol *sym;
9126
9127 sym = allocate_symbol (objfile);
9128 sym->set_language (language_go, &objfile->objfile_obstack);
9129 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9130 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9131 e.g., "main" finds the "main" module and not C's main(). */
9132 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9133 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9134 SYMBOL_TYPE (sym) = type;
9135
9136 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9137 }
9138 }
9139
9140 /* Allocate a fully-qualified name consisting of the two parts on the
9141 obstack. */
9142
9143 static const char *
9144 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9145 {
9146 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9147 }
9148
9149 /* A helper that allocates a variant part to attach to a Rust enum
9150 type. OBSTACK is where the results should be allocated. TYPE is
9151 the type we're processing. DISCRIMINANT_INDEX is the index of the
9152 discriminant. It must be the index of one of the fields of TYPE.
9153 DEFAULT_INDEX is the index of the default field; or -1 if there is
9154 no default. RANGES is indexed by "effective" field number (the
9155 field index, but omitting the discriminant and default fields) and
9156 must hold the discriminant values used by the variants. Note that
9157 RANGES must have a lifetime at least as long as OBSTACK -- either
9158 already allocated on it, or static. */
9159
9160 static void
9161 alloc_rust_variant (struct obstack *obstack, struct type *type,
9162 int discriminant_index, int default_index,
9163 gdb::array_view<discriminant_range> ranges)
9164 {
9165 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9166 must be handled by the caller. */
9167 gdb_assert (discriminant_index >= 0
9168 && discriminant_index < TYPE_NFIELDS (type));
9169 gdb_assert (default_index == -1
9170 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9171
9172 /* We have one variant for each non-discriminant field. */
9173 int n_variants = TYPE_NFIELDS (type) - 1;
9174
9175 variant *variants = new (obstack) variant[n_variants];
9176 int var_idx = 0;
9177 int range_idx = 0;
9178 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9179 {
9180 if (i == discriminant_index)
9181 continue;
9182
9183 variants[var_idx].first_field = i;
9184 variants[var_idx].last_field = i + 1;
9185
9186 /* The default field does not need a range, but other fields do.
9187 We skipped the discriminant above. */
9188 if (i != default_index)
9189 {
9190 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9191 ++range_idx;
9192 }
9193
9194 ++var_idx;
9195 }
9196
9197 gdb_assert (range_idx == ranges.size ());
9198 gdb_assert (var_idx == n_variants);
9199
9200 variant_part *part = new (obstack) variant_part;
9201 part->discriminant_index = discriminant_index;
9202 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9203 discriminant_index));
9204 part->variants = gdb::array_view<variant> (variants, n_variants);
9205
9206 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9207 gdb::array_view<variant_part> *prop_value
9208 = new (storage) gdb::array_view<variant_part> (part, 1);
9209
9210 struct dynamic_prop prop;
9211 prop.kind = PROP_VARIANT_PARTS;
9212 prop.data.variant_parts = prop_value;
9213
9214 add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop, type);
9215 }
9216
9217 /* Some versions of rustc emitted enums in an unusual way.
9218
9219 Ordinary enums were emitted as unions. The first element of each
9220 structure in the union was named "RUST$ENUM$DISR". This element
9221 held the discriminant.
9222
9223 These versions of Rust also implemented the "non-zero"
9224 optimization. When the enum had two values, and one is empty and
9225 the other holds a pointer that cannot be zero, the pointer is used
9226 as the discriminant, with a zero value meaning the empty variant.
9227 Here, the union's first member is of the form
9228 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9229 where the fieldnos are the indices of the fields that should be
9230 traversed in order to find the field (which may be several fields deep)
9231 and the variantname is the name of the variant of the case when the
9232 field is zero.
9233
9234 This function recognizes whether TYPE is of one of these forms,
9235 and, if so, smashes it to be a variant type. */
9236
9237 static void
9238 quirk_rust_enum (struct type *type, struct objfile *objfile)
9239 {
9240 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9241
9242 /* We don't need to deal with empty enums. */
9243 if (TYPE_NFIELDS (type) == 0)
9244 return;
9245
9246 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9247 if (TYPE_NFIELDS (type) == 1
9248 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9249 {
9250 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9251
9252 /* Decode the field name to find the offset of the
9253 discriminant. */
9254 ULONGEST bit_offset = 0;
9255 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9256 while (name[0] >= '0' && name[0] <= '9')
9257 {
9258 char *tail;
9259 unsigned long index = strtoul (name, &tail, 10);
9260 name = tail;
9261 if (*name != '$'
9262 || index >= TYPE_NFIELDS (field_type)
9263 || (TYPE_FIELD_LOC_KIND (field_type, index)
9264 != FIELD_LOC_KIND_BITPOS))
9265 {
9266 complaint (_("Could not parse Rust enum encoding string \"%s\""
9267 "[in module %s]"),
9268 TYPE_FIELD_NAME (type, 0),
9269 objfile_name (objfile));
9270 return;
9271 }
9272 ++name;
9273
9274 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9275 field_type = TYPE_FIELD_TYPE (field_type, index);
9276 }
9277
9278 /* Smash this type to be a structure type. We have to do this
9279 because the type has already been recorded. */
9280 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9281 TYPE_NFIELDS (type) = 3;
9282 /* Save the field we care about. */
9283 struct field saved_field = TYPE_FIELD (type, 0);
9284 TYPE_FIELDS (type)
9285 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9286
9287 /* Put the discriminant at index 0. */
9288 TYPE_FIELD_TYPE (type, 0) = field_type;
9289 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9290 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9291 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), bit_offset);
9292
9293 /* The order of fields doesn't really matter, so put the real
9294 field at index 1 and the data-less field at index 2. */
9295 TYPE_FIELD (type, 1) = saved_field;
9296 TYPE_FIELD_NAME (type, 1)
9297 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type, 1)));
9298 TYPE_NAME (TYPE_FIELD_TYPE (type, 1))
9299 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9300 TYPE_FIELD_NAME (type, 1));
9301
9302 const char *dataless_name
9303 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9304 name);
9305 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9306 dataless_name);
9307 TYPE_FIELD_TYPE (type, 2) = dataless_type;
9308 /* NAME points into the original discriminant name, which
9309 already has the correct lifetime. */
9310 TYPE_FIELD_NAME (type, 2) = name;
9311 SET_FIELD_BITPOS (TYPE_FIELD (type, 2), 0);
9312
9313 /* Indicate that this is a variant type. */
9314 static discriminant_range ranges[1] = { { 0, 0 } };
9315 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9316 }
9317 /* A union with a single anonymous field is probably an old-style
9318 univariant enum. */
9319 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9320 {
9321 /* Smash this type to be a structure type. We have to do this
9322 because the type has already been recorded. */
9323 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9324
9325 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9326 const char *variant_name
9327 = rust_last_path_segment (TYPE_NAME (field_type));
9328 TYPE_FIELD_NAME (type, 0) = variant_name;
9329 TYPE_NAME (field_type)
9330 = rust_fully_qualify (&objfile->objfile_obstack,
9331 TYPE_NAME (type), variant_name);
9332 }
9333 else
9334 {
9335 struct type *disr_type = nullptr;
9336 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9337 {
9338 disr_type = TYPE_FIELD_TYPE (type, i);
9339
9340 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9341 {
9342 /* All fields of a true enum will be structs. */
9343 return;
9344 }
9345 else if (TYPE_NFIELDS (disr_type) == 0)
9346 {
9347 /* Could be data-less variant, so keep going. */
9348 disr_type = nullptr;
9349 }
9350 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9351 "RUST$ENUM$DISR") != 0)
9352 {
9353 /* Not a Rust enum. */
9354 return;
9355 }
9356 else
9357 {
9358 /* Found one. */
9359 break;
9360 }
9361 }
9362
9363 /* If we got here without a discriminant, then it's probably
9364 just a union. */
9365 if (disr_type == nullptr)
9366 return;
9367
9368 /* Smash this type to be a structure type. We have to do this
9369 because the type has already been recorded. */
9370 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9371
9372 /* Make space for the discriminant field. */
9373 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9374 field *new_fields
9375 = (struct field *) TYPE_ZALLOC (type, (TYPE_NFIELDS (type)
9376 * sizeof (struct field)));
9377 memcpy (new_fields + 1, TYPE_FIELDS (type),
9378 TYPE_NFIELDS (type) * sizeof (struct field));
9379 TYPE_FIELDS (type) = new_fields;
9380 TYPE_NFIELDS (type) = TYPE_NFIELDS (type) + 1;
9381
9382 /* Install the discriminant at index 0 in the union. */
9383 TYPE_FIELD (type, 0) = *disr_field;
9384 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9385 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9386
9387 /* We need a way to find the correct discriminant given a
9388 variant name. For convenience we build a map here. */
9389 struct type *enum_type = FIELD_TYPE (*disr_field);
9390 std::unordered_map<std::string, ULONGEST> discriminant_map;
9391 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9392 {
9393 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9394 {
9395 const char *name
9396 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9397 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9398 }
9399 }
9400
9401 int n_fields = TYPE_NFIELDS (type);
9402 /* We don't need a range entry for the discriminant, but we do
9403 need one for every other field, as there is no default
9404 variant. */
9405 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9406 discriminant_range,
9407 n_fields - 1);
9408 /* Skip the discriminant here. */
9409 for (int i = 1; i < n_fields; ++i)
9410 {
9411 /* Find the final word in the name of this variant's type.
9412 That name can be used to look up the correct
9413 discriminant. */
9414 const char *variant_name
9415 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type, i)));
9416
9417 auto iter = discriminant_map.find (variant_name);
9418 if (iter != discriminant_map.end ())
9419 {
9420 ranges[i].low = iter->second;
9421 ranges[i].high = iter->second;
9422 }
9423
9424 /* Remove the discriminant field, if it exists. */
9425 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
9426 if (TYPE_NFIELDS (sub_type) > 0)
9427 {
9428 --TYPE_NFIELDS (sub_type);
9429 ++TYPE_FIELDS (sub_type);
9430 }
9431 TYPE_FIELD_NAME (type, i) = variant_name;
9432 TYPE_NAME (sub_type)
9433 = rust_fully_qualify (&objfile->objfile_obstack,
9434 TYPE_NAME (type), variant_name);
9435 }
9436
9437 /* Indicate that this is a variant type. */
9438 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9439 gdb::array_view<discriminant_range> (ranges,
9440 n_fields - 1));
9441 }
9442 }
9443
9444 /* Rewrite some Rust unions to be structures with variants parts. */
9445
9446 static void
9447 rust_union_quirks (struct dwarf2_cu *cu)
9448 {
9449 gdb_assert (cu->language == language_rust);
9450 for (type *type_ : cu->rust_unions)
9451 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9452 /* We don't need this any more. */
9453 cu->rust_unions.clear ();
9454 }
9455
9456 /* Return the symtab for PER_CU. This works properly regardless of
9457 whether we're using the index or psymtabs. */
9458
9459 static struct compunit_symtab *
9460 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9461 {
9462 return (per_cu->dwarf2_per_objfile->using_index
9463 ? per_cu->v.quick->compunit_symtab
9464 : per_cu->v.psymtab->compunit_symtab);
9465 }
9466
9467 /* A helper function for computing the list of all symbol tables
9468 included by PER_CU. */
9469
9470 static void
9471 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9472 htab_t all_children, htab_t all_type_symtabs,
9473 struct dwarf2_per_cu_data *per_cu,
9474 struct compunit_symtab *immediate_parent)
9475 {
9476 void **slot;
9477 struct compunit_symtab *cust;
9478
9479 slot = htab_find_slot (all_children, per_cu, INSERT);
9480 if (*slot != NULL)
9481 {
9482 /* This inclusion and its children have been processed. */
9483 return;
9484 }
9485
9486 *slot = per_cu;
9487 /* Only add a CU if it has a symbol table. */
9488 cust = get_compunit_symtab (per_cu);
9489 if (cust != NULL)
9490 {
9491 /* If this is a type unit only add its symbol table if we haven't
9492 seen it yet (type unit per_cu's can share symtabs). */
9493 if (per_cu->is_debug_types)
9494 {
9495 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9496 if (*slot == NULL)
9497 {
9498 *slot = cust;
9499 result->push_back (cust);
9500 if (cust->user == NULL)
9501 cust->user = immediate_parent;
9502 }
9503 }
9504 else
9505 {
9506 result->push_back (cust);
9507 if (cust->user == NULL)
9508 cust->user = immediate_parent;
9509 }
9510 }
9511
9512 if (!per_cu->imported_symtabs_empty ())
9513 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9514 {
9515 recursively_compute_inclusions (result, all_children,
9516 all_type_symtabs, ptr, cust);
9517 }
9518 }
9519
9520 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9521 PER_CU. */
9522
9523 static void
9524 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9525 {
9526 gdb_assert (! per_cu->is_debug_types);
9527
9528 if (!per_cu->imported_symtabs_empty ())
9529 {
9530 int len;
9531 std::vector<compunit_symtab *> result_symtabs;
9532 htab_t all_children, all_type_symtabs;
9533 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9534
9535 /* If we don't have a symtab, we can just skip this case. */
9536 if (cust == NULL)
9537 return;
9538
9539 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9540 NULL, xcalloc, xfree);
9541 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9542 NULL, xcalloc, xfree);
9543
9544 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9545 {
9546 recursively_compute_inclusions (&result_symtabs, all_children,
9547 all_type_symtabs, ptr, cust);
9548 }
9549
9550 /* Now we have a transitive closure of all the included symtabs. */
9551 len = result_symtabs.size ();
9552 cust->includes
9553 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9554 struct compunit_symtab *, len + 1);
9555 memcpy (cust->includes, result_symtabs.data (),
9556 len * sizeof (compunit_symtab *));
9557 cust->includes[len] = NULL;
9558
9559 htab_delete (all_children);
9560 htab_delete (all_type_symtabs);
9561 }
9562 }
9563
9564 /* Compute the 'includes' field for the symtabs of all the CUs we just
9565 read. */
9566
9567 static void
9568 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9569 {
9570 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9571 {
9572 if (! iter->is_debug_types)
9573 compute_compunit_symtab_includes (iter);
9574 }
9575
9576 dwarf2_per_objfile->just_read_cus.clear ();
9577 }
9578
9579 /* Generate full symbol information for PER_CU, whose DIEs have
9580 already been loaded into memory. */
9581
9582 static void
9583 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9584 enum language pretend_language)
9585 {
9586 struct dwarf2_cu *cu = per_cu->cu;
9587 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9588 struct objfile *objfile = dwarf2_per_objfile->objfile;
9589 struct gdbarch *gdbarch = objfile->arch ();
9590 CORE_ADDR lowpc, highpc;
9591 struct compunit_symtab *cust;
9592 CORE_ADDR baseaddr;
9593 struct block *static_block;
9594 CORE_ADDR addr;
9595
9596 baseaddr = objfile->text_section_offset ();
9597
9598 /* Clear the list here in case something was left over. */
9599 cu->method_list.clear ();
9600
9601 cu->language = pretend_language;
9602 cu->language_defn = language_def (cu->language);
9603
9604 /* Do line number decoding in read_file_scope () */
9605 process_die (cu->dies, cu);
9606
9607 /* For now fudge the Go package. */
9608 if (cu->language == language_go)
9609 fixup_go_packaging (cu);
9610
9611 /* Now that we have processed all the DIEs in the CU, all the types
9612 should be complete, and it should now be safe to compute all of the
9613 physnames. */
9614 compute_delayed_physnames (cu);
9615
9616 if (cu->language == language_rust)
9617 rust_union_quirks (cu);
9618
9619 /* Some compilers don't define a DW_AT_high_pc attribute for the
9620 compilation unit. If the DW_AT_high_pc is missing, synthesize
9621 it, by scanning the DIE's below the compilation unit. */
9622 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9623
9624 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9625 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9626
9627 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9628 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9629 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9630 addrmap to help ensure it has an accurate map of pc values belonging to
9631 this comp unit. */
9632 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9633
9634 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9635 SECT_OFF_TEXT (objfile),
9636 0);
9637
9638 if (cust != NULL)
9639 {
9640 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9641
9642 /* Set symtab language to language from DW_AT_language. If the
9643 compilation is from a C file generated by language preprocessors, do
9644 not set the language if it was already deduced by start_subfile. */
9645 if (!(cu->language == language_c
9646 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9647 COMPUNIT_FILETABS (cust)->language = cu->language;
9648
9649 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9650 produce DW_AT_location with location lists but it can be possibly
9651 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9652 there were bugs in prologue debug info, fixed later in GCC-4.5
9653 by "unwind info for epilogues" patch (which is not directly related).
9654
9655 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9656 needed, it would be wrong due to missing DW_AT_producer there.
9657
9658 Still one can confuse GDB by using non-standard GCC compilation
9659 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9660 */
9661 if (cu->has_loclist && gcc_4_minor >= 5)
9662 cust->locations_valid = 1;
9663
9664 if (gcc_4_minor >= 5)
9665 cust->epilogue_unwind_valid = 1;
9666
9667 cust->call_site_htab = cu->call_site_htab;
9668 }
9669
9670 if (dwarf2_per_objfile->using_index)
9671 per_cu->v.quick->compunit_symtab = cust;
9672 else
9673 {
9674 dwarf2_psymtab *pst = per_cu->v.psymtab;
9675 pst->compunit_symtab = cust;
9676 pst->readin = true;
9677 }
9678
9679 /* Push it for inclusion processing later. */
9680 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9681
9682 /* Not needed any more. */
9683 cu->reset_builder ();
9684 }
9685
9686 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9687 already been loaded into memory. */
9688
9689 static void
9690 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9691 enum language pretend_language)
9692 {
9693 struct dwarf2_cu *cu = per_cu->cu;
9694 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9695 struct objfile *objfile = dwarf2_per_objfile->objfile;
9696 struct compunit_symtab *cust;
9697 struct signatured_type *sig_type;
9698
9699 gdb_assert (per_cu->is_debug_types);
9700 sig_type = (struct signatured_type *) per_cu;
9701
9702 /* Clear the list here in case something was left over. */
9703 cu->method_list.clear ();
9704
9705 cu->language = pretend_language;
9706 cu->language_defn = language_def (cu->language);
9707
9708 /* The symbol tables are set up in read_type_unit_scope. */
9709 process_die (cu->dies, cu);
9710
9711 /* For now fudge the Go package. */
9712 if (cu->language == language_go)
9713 fixup_go_packaging (cu);
9714
9715 /* Now that we have processed all the DIEs in the CU, all the types
9716 should be complete, and it should now be safe to compute all of the
9717 physnames. */
9718 compute_delayed_physnames (cu);
9719
9720 if (cu->language == language_rust)
9721 rust_union_quirks (cu);
9722
9723 /* TUs share symbol tables.
9724 If this is the first TU to use this symtab, complete the construction
9725 of it with end_expandable_symtab. Otherwise, complete the addition of
9726 this TU's symbols to the existing symtab. */
9727 if (sig_type->type_unit_group->compunit_symtab == NULL)
9728 {
9729 buildsym_compunit *builder = cu->get_builder ();
9730 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9731 sig_type->type_unit_group->compunit_symtab = cust;
9732
9733 if (cust != NULL)
9734 {
9735 /* Set symtab language to language from DW_AT_language. If the
9736 compilation is from a C file generated by language preprocessors,
9737 do not set the language if it was already deduced by
9738 start_subfile. */
9739 if (!(cu->language == language_c
9740 && COMPUNIT_FILETABS (cust)->language != language_c))
9741 COMPUNIT_FILETABS (cust)->language = cu->language;
9742 }
9743 }
9744 else
9745 {
9746 cu->get_builder ()->augment_type_symtab ();
9747 cust = sig_type->type_unit_group->compunit_symtab;
9748 }
9749
9750 if (dwarf2_per_objfile->using_index)
9751 per_cu->v.quick->compunit_symtab = cust;
9752 else
9753 {
9754 dwarf2_psymtab *pst = per_cu->v.psymtab;
9755 pst->compunit_symtab = cust;
9756 pst->readin = true;
9757 }
9758
9759 /* Not needed any more. */
9760 cu->reset_builder ();
9761 }
9762
9763 /* Process an imported unit DIE. */
9764
9765 static void
9766 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9767 {
9768 struct attribute *attr;
9769
9770 /* For now we don't handle imported units in type units. */
9771 if (cu->per_cu->is_debug_types)
9772 {
9773 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9774 " supported in type units [in module %s]"),
9775 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9776 }
9777
9778 attr = dwarf2_attr (die, DW_AT_import, cu);
9779 if (attr != NULL)
9780 {
9781 sect_offset sect_off = attr->get_ref_die_offset ();
9782 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9783 dwarf2_per_cu_data *per_cu
9784 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9785 cu->per_cu->dwarf2_per_objfile);
9786
9787 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9788 into another compilation unit, at root level. Regard this as a hint,
9789 and ignore it. */
9790 if (die->parent && die->parent->parent == NULL
9791 && per_cu->unit_type == DW_UT_compile
9792 && per_cu->lang == language_cplus)
9793 return;
9794
9795 /* If necessary, add it to the queue and load its DIEs. */
9796 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9797 load_full_comp_unit (per_cu, false, cu->language);
9798
9799 cu->per_cu->imported_symtabs_push (per_cu);
9800 }
9801 }
9802
9803 /* RAII object that represents a process_die scope: i.e.,
9804 starts/finishes processing a DIE. */
9805 class process_die_scope
9806 {
9807 public:
9808 process_die_scope (die_info *die, dwarf2_cu *cu)
9809 : m_die (die), m_cu (cu)
9810 {
9811 /* We should only be processing DIEs not already in process. */
9812 gdb_assert (!m_die->in_process);
9813 m_die->in_process = true;
9814 }
9815
9816 ~process_die_scope ()
9817 {
9818 m_die->in_process = false;
9819
9820 /* If we're done processing the DIE for the CU that owns the line
9821 header, we don't need the line header anymore. */
9822 if (m_cu->line_header_die_owner == m_die)
9823 {
9824 delete m_cu->line_header;
9825 m_cu->line_header = NULL;
9826 m_cu->line_header_die_owner = NULL;
9827 }
9828 }
9829
9830 private:
9831 die_info *m_die;
9832 dwarf2_cu *m_cu;
9833 };
9834
9835 /* Process a die and its children. */
9836
9837 static void
9838 process_die (struct die_info *die, struct dwarf2_cu *cu)
9839 {
9840 process_die_scope scope (die, cu);
9841
9842 switch (die->tag)
9843 {
9844 case DW_TAG_padding:
9845 break;
9846 case DW_TAG_compile_unit:
9847 case DW_TAG_partial_unit:
9848 read_file_scope (die, cu);
9849 break;
9850 case DW_TAG_type_unit:
9851 read_type_unit_scope (die, cu);
9852 break;
9853 case DW_TAG_subprogram:
9854 /* Nested subprograms in Fortran get a prefix. */
9855 if (cu->language == language_fortran
9856 && die->parent != NULL
9857 && die->parent->tag == DW_TAG_subprogram)
9858 cu->processing_has_namespace_info = true;
9859 /* Fall through. */
9860 case DW_TAG_inlined_subroutine:
9861 read_func_scope (die, cu);
9862 break;
9863 case DW_TAG_lexical_block:
9864 case DW_TAG_try_block:
9865 case DW_TAG_catch_block:
9866 read_lexical_block_scope (die, cu);
9867 break;
9868 case DW_TAG_call_site:
9869 case DW_TAG_GNU_call_site:
9870 read_call_site_scope (die, cu);
9871 break;
9872 case DW_TAG_class_type:
9873 case DW_TAG_interface_type:
9874 case DW_TAG_structure_type:
9875 case DW_TAG_union_type:
9876 process_structure_scope (die, cu);
9877 break;
9878 case DW_TAG_enumeration_type:
9879 process_enumeration_scope (die, cu);
9880 break;
9881
9882 /* These dies have a type, but processing them does not create
9883 a symbol or recurse to process the children. Therefore we can
9884 read them on-demand through read_type_die. */
9885 case DW_TAG_subroutine_type:
9886 case DW_TAG_set_type:
9887 case DW_TAG_array_type:
9888 case DW_TAG_pointer_type:
9889 case DW_TAG_ptr_to_member_type:
9890 case DW_TAG_reference_type:
9891 case DW_TAG_rvalue_reference_type:
9892 case DW_TAG_string_type:
9893 break;
9894
9895 case DW_TAG_base_type:
9896 case DW_TAG_subrange_type:
9897 case DW_TAG_typedef:
9898 /* Add a typedef symbol for the type definition, if it has a
9899 DW_AT_name. */
9900 new_symbol (die, read_type_die (die, cu), cu);
9901 break;
9902 case DW_TAG_common_block:
9903 read_common_block (die, cu);
9904 break;
9905 case DW_TAG_common_inclusion:
9906 break;
9907 case DW_TAG_namespace:
9908 cu->processing_has_namespace_info = true;
9909 read_namespace (die, cu);
9910 break;
9911 case DW_TAG_module:
9912 cu->processing_has_namespace_info = true;
9913 read_module (die, cu);
9914 break;
9915 case DW_TAG_imported_declaration:
9916 cu->processing_has_namespace_info = true;
9917 if (read_namespace_alias (die, cu))
9918 break;
9919 /* The declaration is not a global namespace alias. */
9920 /* Fall through. */
9921 case DW_TAG_imported_module:
9922 cu->processing_has_namespace_info = true;
9923 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9924 || cu->language != language_fortran))
9925 complaint (_("Tag '%s' has unexpected children"),
9926 dwarf_tag_name (die->tag));
9927 read_import_statement (die, cu);
9928 break;
9929
9930 case DW_TAG_imported_unit:
9931 process_imported_unit_die (die, cu);
9932 break;
9933
9934 case DW_TAG_variable:
9935 read_variable (die, cu);
9936 break;
9937
9938 default:
9939 new_symbol (die, NULL, cu);
9940 break;
9941 }
9942 }
9943 \f
9944 /* DWARF name computation. */
9945
9946 /* A helper function for dwarf2_compute_name which determines whether DIE
9947 needs to have the name of the scope prepended to the name listed in the
9948 die. */
9949
9950 static int
9951 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9952 {
9953 struct attribute *attr;
9954
9955 switch (die->tag)
9956 {
9957 case DW_TAG_namespace:
9958 case DW_TAG_typedef:
9959 case DW_TAG_class_type:
9960 case DW_TAG_interface_type:
9961 case DW_TAG_structure_type:
9962 case DW_TAG_union_type:
9963 case DW_TAG_enumeration_type:
9964 case DW_TAG_enumerator:
9965 case DW_TAG_subprogram:
9966 case DW_TAG_inlined_subroutine:
9967 case DW_TAG_member:
9968 case DW_TAG_imported_declaration:
9969 return 1;
9970
9971 case DW_TAG_variable:
9972 case DW_TAG_constant:
9973 /* We only need to prefix "globally" visible variables. These include
9974 any variable marked with DW_AT_external or any variable that
9975 lives in a namespace. [Variables in anonymous namespaces
9976 require prefixing, but they are not DW_AT_external.] */
9977
9978 if (dwarf2_attr (die, DW_AT_specification, cu))
9979 {
9980 struct dwarf2_cu *spec_cu = cu;
9981
9982 return die_needs_namespace (die_specification (die, &spec_cu),
9983 spec_cu);
9984 }
9985
9986 attr = dwarf2_attr (die, DW_AT_external, cu);
9987 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9988 && die->parent->tag != DW_TAG_module)
9989 return 0;
9990 /* A variable in a lexical block of some kind does not need a
9991 namespace, even though in C++ such variables may be external
9992 and have a mangled name. */
9993 if (die->parent->tag == DW_TAG_lexical_block
9994 || die->parent->tag == DW_TAG_try_block
9995 || die->parent->tag == DW_TAG_catch_block
9996 || die->parent->tag == DW_TAG_subprogram)
9997 return 0;
9998 return 1;
9999
10000 default:
10001 return 0;
10002 }
10003 }
10004
10005 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10006 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10007 defined for the given DIE. */
10008
10009 static struct attribute *
10010 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10011 {
10012 struct attribute *attr;
10013
10014 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10015 if (attr == NULL)
10016 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10017
10018 return attr;
10019 }
10020
10021 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10022 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10023 defined for the given DIE. */
10024
10025 static const char *
10026 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10027 {
10028 const char *linkage_name;
10029
10030 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10031 if (linkage_name == NULL)
10032 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10033
10034 return linkage_name;
10035 }
10036
10037 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10038 compute the physname for the object, which include a method's:
10039 - formal parameters (C++),
10040 - receiver type (Go),
10041
10042 The term "physname" is a bit confusing.
10043 For C++, for example, it is the demangled name.
10044 For Go, for example, it's the mangled name.
10045
10046 For Ada, return the DIE's linkage name rather than the fully qualified
10047 name. PHYSNAME is ignored..
10048
10049 The result is allocated on the objfile_obstack and canonicalized. */
10050
10051 static const char *
10052 dwarf2_compute_name (const char *name,
10053 struct die_info *die, struct dwarf2_cu *cu,
10054 int physname)
10055 {
10056 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10057
10058 if (name == NULL)
10059 name = dwarf2_name (die, cu);
10060
10061 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10062 but otherwise compute it by typename_concat inside GDB.
10063 FIXME: Actually this is not really true, or at least not always true.
10064 It's all very confusing. compute_and_set_names doesn't try to demangle
10065 Fortran names because there is no mangling standard. So new_symbol
10066 will set the demangled name to the result of dwarf2_full_name, and it is
10067 the demangled name that GDB uses if it exists. */
10068 if (cu->language == language_ada
10069 || (cu->language == language_fortran && physname))
10070 {
10071 /* For Ada unit, we prefer the linkage name over the name, as
10072 the former contains the exported name, which the user expects
10073 to be able to reference. Ideally, we want the user to be able
10074 to reference this entity using either natural or linkage name,
10075 but we haven't started looking at this enhancement yet. */
10076 const char *linkage_name = dw2_linkage_name (die, cu);
10077
10078 if (linkage_name != NULL)
10079 return linkage_name;
10080 }
10081
10082 /* These are the only languages we know how to qualify names in. */
10083 if (name != NULL
10084 && (cu->language == language_cplus
10085 || cu->language == language_fortran || cu->language == language_d
10086 || cu->language == language_rust))
10087 {
10088 if (die_needs_namespace (die, cu))
10089 {
10090 const char *prefix;
10091 const char *canonical_name = NULL;
10092
10093 string_file buf;
10094
10095 prefix = determine_prefix (die, cu);
10096 if (*prefix != '\0')
10097 {
10098 gdb::unique_xmalloc_ptr<char> prefixed_name
10099 (typename_concat (NULL, prefix, name, physname, cu));
10100
10101 buf.puts (prefixed_name.get ());
10102 }
10103 else
10104 buf.puts (name);
10105
10106 /* Template parameters may be specified in the DIE's DW_AT_name, or
10107 as children with DW_TAG_template_type_param or
10108 DW_TAG_value_type_param. If the latter, add them to the name
10109 here. If the name already has template parameters, then
10110 skip this step; some versions of GCC emit both, and
10111 it is more efficient to use the pre-computed name.
10112
10113 Something to keep in mind about this process: it is very
10114 unlikely, or in some cases downright impossible, to produce
10115 something that will match the mangled name of a function.
10116 If the definition of the function has the same debug info,
10117 we should be able to match up with it anyway. But fallbacks
10118 using the minimal symbol, for instance to find a method
10119 implemented in a stripped copy of libstdc++, will not work.
10120 If we do not have debug info for the definition, we will have to
10121 match them up some other way.
10122
10123 When we do name matching there is a related problem with function
10124 templates; two instantiated function templates are allowed to
10125 differ only by their return types, which we do not add here. */
10126
10127 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10128 {
10129 struct attribute *attr;
10130 struct die_info *child;
10131 int first = 1;
10132
10133 die->building_fullname = 1;
10134
10135 for (child = die->child; child != NULL; child = child->sibling)
10136 {
10137 struct type *type;
10138 LONGEST value;
10139 const gdb_byte *bytes;
10140 struct dwarf2_locexpr_baton *baton;
10141 struct value *v;
10142
10143 if (child->tag != DW_TAG_template_type_param
10144 && child->tag != DW_TAG_template_value_param)
10145 continue;
10146
10147 if (first)
10148 {
10149 buf.puts ("<");
10150 first = 0;
10151 }
10152 else
10153 buf.puts (", ");
10154
10155 attr = dwarf2_attr (child, DW_AT_type, cu);
10156 if (attr == NULL)
10157 {
10158 complaint (_("template parameter missing DW_AT_type"));
10159 buf.puts ("UNKNOWN_TYPE");
10160 continue;
10161 }
10162 type = die_type (child, cu);
10163
10164 if (child->tag == DW_TAG_template_type_param)
10165 {
10166 c_print_type (type, "", &buf, -1, 0, cu->language,
10167 &type_print_raw_options);
10168 continue;
10169 }
10170
10171 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10172 if (attr == NULL)
10173 {
10174 complaint (_("template parameter missing "
10175 "DW_AT_const_value"));
10176 buf.puts ("UNKNOWN_VALUE");
10177 continue;
10178 }
10179
10180 dwarf2_const_value_attr (attr, type, name,
10181 &cu->comp_unit_obstack, cu,
10182 &value, &bytes, &baton);
10183
10184 if (TYPE_NOSIGN (type))
10185 /* GDB prints characters as NUMBER 'CHAR'. If that's
10186 changed, this can use value_print instead. */
10187 c_printchar (value, type, &buf);
10188 else
10189 {
10190 struct value_print_options opts;
10191
10192 if (baton != NULL)
10193 v = dwarf2_evaluate_loc_desc (type, NULL,
10194 baton->data,
10195 baton->size,
10196 baton->per_cu);
10197 else if (bytes != NULL)
10198 {
10199 v = allocate_value (type);
10200 memcpy (value_contents_writeable (v), bytes,
10201 TYPE_LENGTH (type));
10202 }
10203 else
10204 v = value_from_longest (type, value);
10205
10206 /* Specify decimal so that we do not depend on
10207 the radix. */
10208 get_formatted_print_options (&opts, 'd');
10209 opts.raw = 1;
10210 value_print (v, &buf, &opts);
10211 release_value (v);
10212 }
10213 }
10214
10215 die->building_fullname = 0;
10216
10217 if (!first)
10218 {
10219 /* Close the argument list, with a space if necessary
10220 (nested templates). */
10221 if (!buf.empty () && buf.string ().back () == '>')
10222 buf.puts (" >");
10223 else
10224 buf.puts (">");
10225 }
10226 }
10227
10228 /* For C++ methods, append formal parameter type
10229 information, if PHYSNAME. */
10230
10231 if (physname && die->tag == DW_TAG_subprogram
10232 && cu->language == language_cplus)
10233 {
10234 struct type *type = read_type_die (die, cu);
10235
10236 c_type_print_args (type, &buf, 1, cu->language,
10237 &type_print_raw_options);
10238
10239 if (cu->language == language_cplus)
10240 {
10241 /* Assume that an artificial first parameter is
10242 "this", but do not crash if it is not. RealView
10243 marks unnamed (and thus unused) parameters as
10244 artificial; there is no way to differentiate
10245 the two cases. */
10246 if (TYPE_NFIELDS (type) > 0
10247 && TYPE_FIELD_ARTIFICIAL (type, 0)
10248 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10249 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10250 0))))
10251 buf.puts (" const");
10252 }
10253 }
10254
10255 const std::string &intermediate_name = buf.string ();
10256
10257 if (cu->language == language_cplus)
10258 canonical_name
10259 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10260 objfile);
10261
10262 /* If we only computed INTERMEDIATE_NAME, or if
10263 INTERMEDIATE_NAME is already canonical, then we need to
10264 intern it. */
10265 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10266 name = objfile->intern (intermediate_name);
10267 else
10268 name = canonical_name;
10269 }
10270 }
10271
10272 return name;
10273 }
10274
10275 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10276 If scope qualifiers are appropriate they will be added. The result
10277 will be allocated on the storage_obstack, or NULL if the DIE does
10278 not have a name. NAME may either be from a previous call to
10279 dwarf2_name or NULL.
10280
10281 The output string will be canonicalized (if C++). */
10282
10283 static const char *
10284 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10285 {
10286 return dwarf2_compute_name (name, die, cu, 0);
10287 }
10288
10289 /* Construct a physname for the given DIE in CU. NAME may either be
10290 from a previous call to dwarf2_name or NULL. The result will be
10291 allocated on the objfile_objstack or NULL if the DIE does not have a
10292 name.
10293
10294 The output string will be canonicalized (if C++). */
10295
10296 static const char *
10297 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10298 {
10299 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10300 const char *retval, *mangled = NULL, *canon = NULL;
10301 int need_copy = 1;
10302
10303 /* In this case dwarf2_compute_name is just a shortcut not building anything
10304 on its own. */
10305 if (!die_needs_namespace (die, cu))
10306 return dwarf2_compute_name (name, die, cu, 1);
10307
10308 mangled = dw2_linkage_name (die, cu);
10309
10310 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10311 See https://github.com/rust-lang/rust/issues/32925. */
10312 if (cu->language == language_rust && mangled != NULL
10313 && strchr (mangled, '{') != NULL)
10314 mangled = NULL;
10315
10316 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10317 has computed. */
10318 gdb::unique_xmalloc_ptr<char> demangled;
10319 if (mangled != NULL)
10320 {
10321
10322 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10323 {
10324 /* Do nothing (do not demangle the symbol name). */
10325 }
10326 else if (cu->language == language_go)
10327 {
10328 /* This is a lie, but we already lie to the caller new_symbol.
10329 new_symbol assumes we return the mangled name.
10330 This just undoes that lie until things are cleaned up. */
10331 }
10332 else
10333 {
10334 /* Use DMGL_RET_DROP for C++ template functions to suppress
10335 their return type. It is easier for GDB users to search
10336 for such functions as `name(params)' than `long name(params)'.
10337 In such case the minimal symbol names do not match the full
10338 symbol names but for template functions there is never a need
10339 to look up their definition from their declaration so
10340 the only disadvantage remains the minimal symbol variant
10341 `long name(params)' does not have the proper inferior type. */
10342 demangled.reset (gdb_demangle (mangled,
10343 (DMGL_PARAMS | DMGL_ANSI
10344 | DMGL_RET_DROP)));
10345 }
10346 if (demangled)
10347 canon = demangled.get ();
10348 else
10349 {
10350 canon = mangled;
10351 need_copy = 0;
10352 }
10353 }
10354
10355 if (canon == NULL || check_physname)
10356 {
10357 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10358
10359 if (canon != NULL && strcmp (physname, canon) != 0)
10360 {
10361 /* It may not mean a bug in GDB. The compiler could also
10362 compute DW_AT_linkage_name incorrectly. But in such case
10363 GDB would need to be bug-to-bug compatible. */
10364
10365 complaint (_("Computed physname <%s> does not match demangled <%s> "
10366 "(from linkage <%s>) - DIE at %s [in module %s]"),
10367 physname, canon, mangled, sect_offset_str (die->sect_off),
10368 objfile_name (objfile));
10369
10370 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10371 is available here - over computed PHYSNAME. It is safer
10372 against both buggy GDB and buggy compilers. */
10373
10374 retval = canon;
10375 }
10376 else
10377 {
10378 retval = physname;
10379 need_copy = 0;
10380 }
10381 }
10382 else
10383 retval = canon;
10384
10385 if (need_copy)
10386 retval = objfile->intern (retval);
10387
10388 return retval;
10389 }
10390
10391 /* Inspect DIE in CU for a namespace alias. If one exists, record
10392 a new symbol for it.
10393
10394 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10395
10396 static int
10397 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10398 {
10399 struct attribute *attr;
10400
10401 /* If the die does not have a name, this is not a namespace
10402 alias. */
10403 attr = dwarf2_attr (die, DW_AT_name, cu);
10404 if (attr != NULL)
10405 {
10406 int num;
10407 struct die_info *d = die;
10408 struct dwarf2_cu *imported_cu = cu;
10409
10410 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10411 keep inspecting DIEs until we hit the underlying import. */
10412 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10413 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10414 {
10415 attr = dwarf2_attr (d, DW_AT_import, cu);
10416 if (attr == NULL)
10417 break;
10418
10419 d = follow_die_ref (d, attr, &imported_cu);
10420 if (d->tag != DW_TAG_imported_declaration)
10421 break;
10422 }
10423
10424 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10425 {
10426 complaint (_("DIE at %s has too many recursively imported "
10427 "declarations"), sect_offset_str (d->sect_off));
10428 return 0;
10429 }
10430
10431 if (attr != NULL)
10432 {
10433 struct type *type;
10434 sect_offset sect_off = attr->get_ref_die_offset ();
10435
10436 type = get_die_type_at_offset (sect_off, cu->per_cu);
10437 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10438 {
10439 /* This declaration is a global namespace alias. Add
10440 a symbol for it whose type is the aliased namespace. */
10441 new_symbol (die, type, cu);
10442 return 1;
10443 }
10444 }
10445 }
10446
10447 return 0;
10448 }
10449
10450 /* Return the using directives repository (global or local?) to use in the
10451 current context for CU.
10452
10453 For Ada, imported declarations can materialize renamings, which *may* be
10454 global. However it is impossible (for now?) in DWARF to distinguish
10455 "external" imported declarations and "static" ones. As all imported
10456 declarations seem to be static in all other languages, make them all CU-wide
10457 global only in Ada. */
10458
10459 static struct using_direct **
10460 using_directives (struct dwarf2_cu *cu)
10461 {
10462 if (cu->language == language_ada
10463 && cu->get_builder ()->outermost_context_p ())
10464 return cu->get_builder ()->get_global_using_directives ();
10465 else
10466 return cu->get_builder ()->get_local_using_directives ();
10467 }
10468
10469 /* Read the import statement specified by the given die and record it. */
10470
10471 static void
10472 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10473 {
10474 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10475 struct attribute *import_attr;
10476 struct die_info *imported_die, *child_die;
10477 struct dwarf2_cu *imported_cu;
10478 const char *imported_name;
10479 const char *imported_name_prefix;
10480 const char *canonical_name;
10481 const char *import_alias;
10482 const char *imported_declaration = NULL;
10483 const char *import_prefix;
10484 std::vector<const char *> excludes;
10485
10486 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10487 if (import_attr == NULL)
10488 {
10489 complaint (_("Tag '%s' has no DW_AT_import"),
10490 dwarf_tag_name (die->tag));
10491 return;
10492 }
10493
10494 imported_cu = cu;
10495 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10496 imported_name = dwarf2_name (imported_die, imported_cu);
10497 if (imported_name == NULL)
10498 {
10499 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10500
10501 The import in the following code:
10502 namespace A
10503 {
10504 typedef int B;
10505 }
10506
10507 int main ()
10508 {
10509 using A::B;
10510 B b;
10511 return b;
10512 }
10513
10514 ...
10515 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10516 <52> DW_AT_decl_file : 1
10517 <53> DW_AT_decl_line : 6
10518 <54> DW_AT_import : <0x75>
10519 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10520 <59> DW_AT_name : B
10521 <5b> DW_AT_decl_file : 1
10522 <5c> DW_AT_decl_line : 2
10523 <5d> DW_AT_type : <0x6e>
10524 ...
10525 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10526 <76> DW_AT_byte_size : 4
10527 <77> DW_AT_encoding : 5 (signed)
10528
10529 imports the wrong die ( 0x75 instead of 0x58 ).
10530 This case will be ignored until the gcc bug is fixed. */
10531 return;
10532 }
10533
10534 /* Figure out the local name after import. */
10535 import_alias = dwarf2_name (die, cu);
10536
10537 /* Figure out where the statement is being imported to. */
10538 import_prefix = determine_prefix (die, cu);
10539
10540 /* Figure out what the scope of the imported die is and prepend it
10541 to the name of the imported die. */
10542 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10543
10544 if (imported_die->tag != DW_TAG_namespace
10545 && imported_die->tag != DW_TAG_module)
10546 {
10547 imported_declaration = imported_name;
10548 canonical_name = imported_name_prefix;
10549 }
10550 else if (strlen (imported_name_prefix) > 0)
10551 canonical_name = obconcat (&objfile->objfile_obstack,
10552 imported_name_prefix,
10553 (cu->language == language_d ? "." : "::"),
10554 imported_name, (char *) NULL);
10555 else
10556 canonical_name = imported_name;
10557
10558 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10559 for (child_die = die->child; child_die && child_die->tag;
10560 child_die = child_die->sibling)
10561 {
10562 /* DWARF-4: A Fortran use statement with a “rename list” may be
10563 represented by an imported module entry with an import attribute
10564 referring to the module and owned entries corresponding to those
10565 entities that are renamed as part of being imported. */
10566
10567 if (child_die->tag != DW_TAG_imported_declaration)
10568 {
10569 complaint (_("child DW_TAG_imported_declaration expected "
10570 "- DIE at %s [in module %s]"),
10571 sect_offset_str (child_die->sect_off),
10572 objfile_name (objfile));
10573 continue;
10574 }
10575
10576 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10577 if (import_attr == NULL)
10578 {
10579 complaint (_("Tag '%s' has no DW_AT_import"),
10580 dwarf_tag_name (child_die->tag));
10581 continue;
10582 }
10583
10584 imported_cu = cu;
10585 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10586 &imported_cu);
10587 imported_name = dwarf2_name (imported_die, imported_cu);
10588 if (imported_name == NULL)
10589 {
10590 complaint (_("child DW_TAG_imported_declaration has unknown "
10591 "imported name - DIE at %s [in module %s]"),
10592 sect_offset_str (child_die->sect_off),
10593 objfile_name (objfile));
10594 continue;
10595 }
10596
10597 excludes.push_back (imported_name);
10598
10599 process_die (child_die, cu);
10600 }
10601
10602 add_using_directive (using_directives (cu),
10603 import_prefix,
10604 canonical_name,
10605 import_alias,
10606 imported_declaration,
10607 excludes,
10608 0,
10609 &objfile->objfile_obstack);
10610 }
10611
10612 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10613 types, but gives them a size of zero. Starting with version 14,
10614 ICC is compatible with GCC. */
10615
10616 static bool
10617 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10618 {
10619 if (!cu->checked_producer)
10620 check_producer (cu);
10621
10622 return cu->producer_is_icc_lt_14;
10623 }
10624
10625 /* ICC generates a DW_AT_type for C void functions. This was observed on
10626 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10627 which says that void functions should not have a DW_AT_type. */
10628
10629 static bool
10630 producer_is_icc (struct dwarf2_cu *cu)
10631 {
10632 if (!cu->checked_producer)
10633 check_producer (cu);
10634
10635 return cu->producer_is_icc;
10636 }
10637
10638 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10639 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10640 this, it was first present in GCC release 4.3.0. */
10641
10642 static bool
10643 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10644 {
10645 if (!cu->checked_producer)
10646 check_producer (cu);
10647
10648 return cu->producer_is_gcc_lt_4_3;
10649 }
10650
10651 static file_and_directory
10652 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10653 {
10654 file_and_directory res;
10655
10656 /* Find the filename. Do not use dwarf2_name here, since the filename
10657 is not a source language identifier. */
10658 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10659 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10660
10661 if (res.comp_dir == NULL
10662 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10663 && IS_ABSOLUTE_PATH (res.name))
10664 {
10665 res.comp_dir_storage = ldirname (res.name);
10666 if (!res.comp_dir_storage.empty ())
10667 res.comp_dir = res.comp_dir_storage.c_str ();
10668 }
10669 if (res.comp_dir != NULL)
10670 {
10671 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10672 directory, get rid of it. */
10673 const char *cp = strchr (res.comp_dir, ':');
10674
10675 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10676 res.comp_dir = cp + 1;
10677 }
10678
10679 if (res.name == NULL)
10680 res.name = "<unknown>";
10681
10682 return res;
10683 }
10684
10685 /* Handle DW_AT_stmt_list for a compilation unit.
10686 DIE is the DW_TAG_compile_unit die for CU.
10687 COMP_DIR is the compilation directory. LOWPC is passed to
10688 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10689
10690 static void
10691 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10692 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10693 {
10694 struct dwarf2_per_objfile *dwarf2_per_objfile
10695 = cu->per_cu->dwarf2_per_objfile;
10696 struct attribute *attr;
10697 struct line_header line_header_local;
10698 hashval_t line_header_local_hash;
10699 void **slot;
10700 int decode_mapping;
10701
10702 gdb_assert (! cu->per_cu->is_debug_types);
10703
10704 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10705 if (attr == NULL)
10706 return;
10707
10708 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10709
10710 /* The line header hash table is only created if needed (it exists to
10711 prevent redundant reading of the line table for partial_units).
10712 If we're given a partial_unit, we'll need it. If we're given a
10713 compile_unit, then use the line header hash table if it's already
10714 created, but don't create one just yet. */
10715
10716 if (dwarf2_per_objfile->line_header_hash == NULL
10717 && die->tag == DW_TAG_partial_unit)
10718 {
10719 dwarf2_per_objfile->line_header_hash
10720 .reset (htab_create_alloc (127, line_header_hash_voidp,
10721 line_header_eq_voidp,
10722 free_line_header_voidp,
10723 xcalloc, xfree));
10724 }
10725
10726 line_header_local.sect_off = line_offset;
10727 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10728 line_header_local_hash = line_header_hash (&line_header_local);
10729 if (dwarf2_per_objfile->line_header_hash != NULL)
10730 {
10731 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10732 &line_header_local,
10733 line_header_local_hash, NO_INSERT);
10734
10735 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10736 is not present in *SLOT (since if there is something in *SLOT then
10737 it will be for a partial_unit). */
10738 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10739 {
10740 gdb_assert (*slot != NULL);
10741 cu->line_header = (struct line_header *) *slot;
10742 return;
10743 }
10744 }
10745
10746 /* dwarf_decode_line_header does not yet provide sufficient information.
10747 We always have to call also dwarf_decode_lines for it. */
10748 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10749 if (lh == NULL)
10750 return;
10751
10752 cu->line_header = lh.release ();
10753 cu->line_header_die_owner = die;
10754
10755 if (dwarf2_per_objfile->line_header_hash == NULL)
10756 slot = NULL;
10757 else
10758 {
10759 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10760 &line_header_local,
10761 line_header_local_hash, INSERT);
10762 gdb_assert (slot != NULL);
10763 }
10764 if (slot != NULL && *slot == NULL)
10765 {
10766 /* This newly decoded line number information unit will be owned
10767 by line_header_hash hash table. */
10768 *slot = cu->line_header;
10769 cu->line_header_die_owner = NULL;
10770 }
10771 else
10772 {
10773 /* We cannot free any current entry in (*slot) as that struct line_header
10774 may be already used by multiple CUs. Create only temporary decoded
10775 line_header for this CU - it may happen at most once for each line
10776 number information unit. And if we're not using line_header_hash
10777 then this is what we want as well. */
10778 gdb_assert (die->tag != DW_TAG_partial_unit);
10779 }
10780 decode_mapping = (die->tag != DW_TAG_partial_unit);
10781 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10782 decode_mapping);
10783
10784 }
10785
10786 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10787
10788 static void
10789 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10790 {
10791 struct dwarf2_per_objfile *dwarf2_per_objfile
10792 = cu->per_cu->dwarf2_per_objfile;
10793 struct objfile *objfile = dwarf2_per_objfile->objfile;
10794 struct gdbarch *gdbarch = objfile->arch ();
10795 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10796 CORE_ADDR highpc = ((CORE_ADDR) 0);
10797 struct attribute *attr;
10798 struct die_info *child_die;
10799 CORE_ADDR baseaddr;
10800
10801 prepare_one_comp_unit (cu, die, cu->language);
10802 baseaddr = objfile->text_section_offset ();
10803
10804 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10805
10806 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10807 from finish_block. */
10808 if (lowpc == ((CORE_ADDR) -1))
10809 lowpc = highpc;
10810 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10811
10812 file_and_directory fnd = find_file_and_directory (die, cu);
10813
10814 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10815 standardised yet. As a workaround for the language detection we fall
10816 back to the DW_AT_producer string. */
10817 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10818 cu->language = language_opencl;
10819
10820 /* Similar hack for Go. */
10821 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10822 set_cu_language (DW_LANG_Go, cu);
10823
10824 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10825
10826 /* Decode line number information if present. We do this before
10827 processing child DIEs, so that the line header table is available
10828 for DW_AT_decl_file. */
10829 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10830
10831 /* Process all dies in compilation unit. */
10832 if (die->child != NULL)
10833 {
10834 child_die = die->child;
10835 while (child_die && child_die->tag)
10836 {
10837 process_die (child_die, cu);
10838 child_die = child_die->sibling;
10839 }
10840 }
10841
10842 /* Decode macro information, if present. Dwarf 2 macro information
10843 refers to information in the line number info statement program
10844 header, so we can only read it if we've read the header
10845 successfully. */
10846 attr = dwarf2_attr (die, DW_AT_macros, cu);
10847 if (attr == NULL)
10848 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10849 if (attr && cu->line_header)
10850 {
10851 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10852 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10853
10854 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10855 }
10856 else
10857 {
10858 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10859 if (attr && cu->line_header)
10860 {
10861 unsigned int macro_offset = DW_UNSND (attr);
10862
10863 dwarf_decode_macros (cu, macro_offset, 0);
10864 }
10865 }
10866 }
10867
10868 void
10869 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10870 {
10871 struct type_unit_group *tu_group;
10872 int first_time;
10873 struct attribute *attr;
10874 unsigned int i;
10875 struct signatured_type *sig_type;
10876
10877 gdb_assert (per_cu->is_debug_types);
10878 sig_type = (struct signatured_type *) per_cu;
10879
10880 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10881
10882 /* If we're using .gdb_index (includes -readnow) then
10883 per_cu->type_unit_group may not have been set up yet. */
10884 if (sig_type->type_unit_group == NULL)
10885 sig_type->type_unit_group = get_type_unit_group (this, attr);
10886 tu_group = sig_type->type_unit_group;
10887
10888 /* If we've already processed this stmt_list there's no real need to
10889 do it again, we could fake it and just recreate the part we need
10890 (file name,index -> symtab mapping). If data shows this optimization
10891 is useful we can do it then. */
10892 first_time = tu_group->compunit_symtab == NULL;
10893
10894 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10895 debug info. */
10896 line_header_up lh;
10897 if (attr != NULL)
10898 {
10899 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10900 lh = dwarf_decode_line_header (line_offset, this);
10901 }
10902 if (lh == NULL)
10903 {
10904 if (first_time)
10905 start_symtab ("", NULL, 0);
10906 else
10907 {
10908 gdb_assert (tu_group->symtabs == NULL);
10909 gdb_assert (m_builder == nullptr);
10910 struct compunit_symtab *cust = tu_group->compunit_symtab;
10911 m_builder.reset (new struct buildsym_compunit
10912 (COMPUNIT_OBJFILE (cust), "",
10913 COMPUNIT_DIRNAME (cust),
10914 compunit_language (cust),
10915 0, cust));
10916 }
10917 return;
10918 }
10919
10920 line_header = lh.release ();
10921 line_header_die_owner = die;
10922
10923 if (first_time)
10924 {
10925 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10926
10927 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10928 still initializing it, and our caller (a few levels up)
10929 process_full_type_unit still needs to know if this is the first
10930 time. */
10931
10932 tu_group->symtabs
10933 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10934 struct symtab *, line_header->file_names_size ());
10935
10936 auto &file_names = line_header->file_names ();
10937 for (i = 0; i < file_names.size (); ++i)
10938 {
10939 file_entry &fe = file_names[i];
10940 dwarf2_start_subfile (this, fe.name,
10941 fe.include_dir (line_header));
10942 buildsym_compunit *b = get_builder ();
10943 if (b->get_current_subfile ()->symtab == NULL)
10944 {
10945 /* NOTE: start_subfile will recognize when it's been
10946 passed a file it has already seen. So we can't
10947 assume there's a simple mapping from
10948 cu->line_header->file_names to subfiles, plus
10949 cu->line_header->file_names may contain dups. */
10950 b->get_current_subfile ()->symtab
10951 = allocate_symtab (cust, b->get_current_subfile ()->name);
10952 }
10953
10954 fe.symtab = b->get_current_subfile ()->symtab;
10955 tu_group->symtabs[i] = fe.symtab;
10956 }
10957 }
10958 else
10959 {
10960 gdb_assert (m_builder == nullptr);
10961 struct compunit_symtab *cust = tu_group->compunit_symtab;
10962 m_builder.reset (new struct buildsym_compunit
10963 (COMPUNIT_OBJFILE (cust), "",
10964 COMPUNIT_DIRNAME (cust),
10965 compunit_language (cust),
10966 0, cust));
10967
10968 auto &file_names = line_header->file_names ();
10969 for (i = 0; i < file_names.size (); ++i)
10970 {
10971 file_entry &fe = file_names[i];
10972 fe.symtab = tu_group->symtabs[i];
10973 }
10974 }
10975
10976 /* The main symtab is allocated last. Type units don't have DW_AT_name
10977 so they don't have a "real" (so to speak) symtab anyway.
10978 There is later code that will assign the main symtab to all symbols
10979 that don't have one. We need to handle the case of a symbol with a
10980 missing symtab (DW_AT_decl_file) anyway. */
10981 }
10982
10983 /* Process DW_TAG_type_unit.
10984 For TUs we want to skip the first top level sibling if it's not the
10985 actual type being defined by this TU. In this case the first top
10986 level sibling is there to provide context only. */
10987
10988 static void
10989 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10990 {
10991 struct die_info *child_die;
10992
10993 prepare_one_comp_unit (cu, die, language_minimal);
10994
10995 /* Initialize (or reinitialize) the machinery for building symtabs.
10996 We do this before processing child DIEs, so that the line header table
10997 is available for DW_AT_decl_file. */
10998 cu->setup_type_unit_groups (die);
10999
11000 if (die->child != NULL)
11001 {
11002 child_die = die->child;
11003 while (child_die && child_die->tag)
11004 {
11005 process_die (child_die, cu);
11006 child_die = child_die->sibling;
11007 }
11008 }
11009 }
11010 \f
11011 /* DWO/DWP files.
11012
11013 http://gcc.gnu.org/wiki/DebugFission
11014 http://gcc.gnu.org/wiki/DebugFissionDWP
11015
11016 To simplify handling of both DWO files ("object" files with the DWARF info)
11017 and DWP files (a file with the DWOs packaged up into one file), we treat
11018 DWP files as having a collection of virtual DWO files. */
11019
11020 static hashval_t
11021 hash_dwo_file (const void *item)
11022 {
11023 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11024 hashval_t hash;
11025
11026 hash = htab_hash_string (dwo_file->dwo_name);
11027 if (dwo_file->comp_dir != NULL)
11028 hash += htab_hash_string (dwo_file->comp_dir);
11029 return hash;
11030 }
11031
11032 static int
11033 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11034 {
11035 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11036 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11037
11038 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11039 return 0;
11040 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11041 return lhs->comp_dir == rhs->comp_dir;
11042 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11043 }
11044
11045 /* Allocate a hash table for DWO files. */
11046
11047 static htab_up
11048 allocate_dwo_file_hash_table ()
11049 {
11050 auto delete_dwo_file = [] (void *item)
11051 {
11052 struct dwo_file *dwo_file = (struct dwo_file *) item;
11053
11054 delete dwo_file;
11055 };
11056
11057 return htab_up (htab_create_alloc (41,
11058 hash_dwo_file,
11059 eq_dwo_file,
11060 delete_dwo_file,
11061 xcalloc, xfree));
11062 }
11063
11064 /* Lookup DWO file DWO_NAME. */
11065
11066 static void **
11067 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11068 const char *dwo_name,
11069 const char *comp_dir)
11070 {
11071 struct dwo_file find_entry;
11072 void **slot;
11073
11074 if (dwarf2_per_objfile->dwo_files == NULL)
11075 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
11076
11077 find_entry.dwo_name = dwo_name;
11078 find_entry.comp_dir = comp_dir;
11079 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11080 INSERT);
11081
11082 return slot;
11083 }
11084
11085 static hashval_t
11086 hash_dwo_unit (const void *item)
11087 {
11088 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11089
11090 /* This drops the top 32 bits of the id, but is ok for a hash. */
11091 return dwo_unit->signature;
11092 }
11093
11094 static int
11095 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11096 {
11097 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11098 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11099
11100 /* The signature is assumed to be unique within the DWO file.
11101 So while object file CU dwo_id's always have the value zero,
11102 that's OK, assuming each object file DWO file has only one CU,
11103 and that's the rule for now. */
11104 return lhs->signature == rhs->signature;
11105 }
11106
11107 /* Allocate a hash table for DWO CUs,TUs.
11108 There is one of these tables for each of CUs,TUs for each DWO file. */
11109
11110 static htab_up
11111 allocate_dwo_unit_table ()
11112 {
11113 /* Start out with a pretty small number.
11114 Generally DWO files contain only one CU and maybe some TUs. */
11115 return htab_up (htab_create_alloc (3,
11116 hash_dwo_unit,
11117 eq_dwo_unit,
11118 NULL, xcalloc, xfree));
11119 }
11120
11121 /* die_reader_func for create_dwo_cu. */
11122
11123 static void
11124 create_dwo_cu_reader (const struct die_reader_specs *reader,
11125 const gdb_byte *info_ptr,
11126 struct die_info *comp_unit_die,
11127 struct dwo_file *dwo_file,
11128 struct dwo_unit *dwo_unit)
11129 {
11130 struct dwarf2_cu *cu = reader->cu;
11131 sect_offset sect_off = cu->per_cu->sect_off;
11132 struct dwarf2_section_info *section = cu->per_cu->section;
11133
11134 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11135 if (!signature.has_value ())
11136 {
11137 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11138 " its dwo_id [in module %s]"),
11139 sect_offset_str (sect_off), dwo_file->dwo_name);
11140 return;
11141 }
11142
11143 dwo_unit->dwo_file = dwo_file;
11144 dwo_unit->signature = *signature;
11145 dwo_unit->section = section;
11146 dwo_unit->sect_off = sect_off;
11147 dwo_unit->length = cu->per_cu->length;
11148
11149 if (dwarf_read_debug)
11150 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11151 sect_offset_str (sect_off),
11152 hex_string (dwo_unit->signature));
11153 }
11154
11155 /* Create the dwo_units for the CUs in a DWO_FILE.
11156 Note: This function processes DWO files only, not DWP files. */
11157
11158 static void
11159 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11160 dwarf2_cu *cu, struct dwo_file &dwo_file,
11161 dwarf2_section_info &section, htab_up &cus_htab)
11162 {
11163 struct objfile *objfile = dwarf2_per_objfile->objfile;
11164 const gdb_byte *info_ptr, *end_ptr;
11165
11166 section.read (objfile);
11167 info_ptr = section.buffer;
11168
11169 if (info_ptr == NULL)
11170 return;
11171
11172 if (dwarf_read_debug)
11173 {
11174 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11175 section.get_name (),
11176 section.get_file_name ());
11177 }
11178
11179 end_ptr = info_ptr + section.size;
11180 while (info_ptr < end_ptr)
11181 {
11182 struct dwarf2_per_cu_data per_cu;
11183 struct dwo_unit read_unit {};
11184 struct dwo_unit *dwo_unit;
11185 void **slot;
11186 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11187
11188 memset (&per_cu, 0, sizeof (per_cu));
11189 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11190 per_cu.is_debug_types = 0;
11191 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11192 per_cu.section = &section;
11193
11194 cutu_reader reader (&per_cu, cu, &dwo_file);
11195 if (!reader.dummy_p)
11196 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11197 &dwo_file, &read_unit);
11198 info_ptr += per_cu.length;
11199
11200 // If the unit could not be parsed, skip it.
11201 if (read_unit.dwo_file == NULL)
11202 continue;
11203
11204 if (cus_htab == NULL)
11205 cus_htab = allocate_dwo_unit_table ();
11206
11207 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11208 *dwo_unit = read_unit;
11209 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11210 gdb_assert (slot != NULL);
11211 if (*slot != NULL)
11212 {
11213 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11214 sect_offset dup_sect_off = dup_cu->sect_off;
11215
11216 complaint (_("debug cu entry at offset %s is duplicate to"
11217 " the entry at offset %s, signature %s"),
11218 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11219 hex_string (dwo_unit->signature));
11220 }
11221 *slot = (void *)dwo_unit;
11222 }
11223 }
11224
11225 /* DWP file .debug_{cu,tu}_index section format:
11226 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11227
11228 DWP Version 1:
11229
11230 Both index sections have the same format, and serve to map a 64-bit
11231 signature to a set of section numbers. Each section begins with a header,
11232 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11233 indexes, and a pool of 32-bit section numbers. The index sections will be
11234 aligned at 8-byte boundaries in the file.
11235
11236 The index section header consists of:
11237
11238 V, 32 bit version number
11239 -, 32 bits unused
11240 N, 32 bit number of compilation units or type units in the index
11241 M, 32 bit number of slots in the hash table
11242
11243 Numbers are recorded using the byte order of the application binary.
11244
11245 The hash table begins at offset 16 in the section, and consists of an array
11246 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11247 order of the application binary). Unused slots in the hash table are 0.
11248 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11249
11250 The parallel table begins immediately after the hash table
11251 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11252 array of 32-bit indexes (using the byte order of the application binary),
11253 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11254 table contains a 32-bit index into the pool of section numbers. For unused
11255 hash table slots, the corresponding entry in the parallel table will be 0.
11256
11257 The pool of section numbers begins immediately following the hash table
11258 (at offset 16 + 12 * M from the beginning of the section). The pool of
11259 section numbers consists of an array of 32-bit words (using the byte order
11260 of the application binary). Each item in the array is indexed starting
11261 from 0. The hash table entry provides the index of the first section
11262 number in the set. Additional section numbers in the set follow, and the
11263 set is terminated by a 0 entry (section number 0 is not used in ELF).
11264
11265 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11266 section must be the first entry in the set, and the .debug_abbrev.dwo must
11267 be the second entry. Other members of the set may follow in any order.
11268
11269 ---
11270
11271 DWP Version 2:
11272
11273 DWP Version 2 combines all the .debug_info, etc. sections into one,
11274 and the entries in the index tables are now offsets into these sections.
11275 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11276 section.
11277
11278 Index Section Contents:
11279 Header
11280 Hash Table of Signatures dwp_hash_table.hash_table
11281 Parallel Table of Indices dwp_hash_table.unit_table
11282 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11283 Table of Section Sizes dwp_hash_table.v2.sizes
11284
11285 The index section header consists of:
11286
11287 V, 32 bit version number
11288 L, 32 bit number of columns in the table of section offsets
11289 N, 32 bit number of compilation units or type units in the index
11290 M, 32 bit number of slots in the hash table
11291
11292 Numbers are recorded using the byte order of the application binary.
11293
11294 The hash table has the same format as version 1.
11295 The parallel table of indices has the same format as version 1,
11296 except that the entries are origin-1 indices into the table of sections
11297 offsets and the table of section sizes.
11298
11299 The table of offsets begins immediately following the parallel table
11300 (at offset 16 + 12 * M from the beginning of the section). The table is
11301 a two-dimensional array of 32-bit words (using the byte order of the
11302 application binary), with L columns and N+1 rows, in row-major order.
11303 Each row in the array is indexed starting from 0. The first row provides
11304 a key to the remaining rows: each column in this row provides an identifier
11305 for a debug section, and the offsets in the same column of subsequent rows
11306 refer to that section. The section identifiers are:
11307
11308 DW_SECT_INFO 1 .debug_info.dwo
11309 DW_SECT_TYPES 2 .debug_types.dwo
11310 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11311 DW_SECT_LINE 4 .debug_line.dwo
11312 DW_SECT_LOC 5 .debug_loc.dwo
11313 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11314 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11315 DW_SECT_MACRO 8 .debug_macro.dwo
11316
11317 The offsets provided by the CU and TU index sections are the base offsets
11318 for the contributions made by each CU or TU to the corresponding section
11319 in the package file. Each CU and TU header contains an abbrev_offset
11320 field, used to find the abbreviations table for that CU or TU within the
11321 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11322 be interpreted as relative to the base offset given in the index section.
11323 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11324 should be interpreted as relative to the base offset for .debug_line.dwo,
11325 and offsets into other debug sections obtained from DWARF attributes should
11326 also be interpreted as relative to the corresponding base offset.
11327
11328 The table of sizes begins immediately following the table of offsets.
11329 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11330 with L columns and N rows, in row-major order. Each row in the array is
11331 indexed starting from 1 (row 0 is shared by the two tables).
11332
11333 ---
11334
11335 Hash table lookup is handled the same in version 1 and 2:
11336
11337 We assume that N and M will not exceed 2^32 - 1.
11338 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11339
11340 Given a 64-bit compilation unit signature or a type signature S, an entry
11341 in the hash table is located as follows:
11342
11343 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11344 the low-order k bits all set to 1.
11345
11346 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11347
11348 3) If the hash table entry at index H matches the signature, use that
11349 entry. If the hash table entry at index H is unused (all zeroes),
11350 terminate the search: the signature is not present in the table.
11351
11352 4) Let H = (H + H') modulo M. Repeat at Step 3.
11353
11354 Because M > N and H' and M are relatively prime, the search is guaranteed
11355 to stop at an unused slot or find the match. */
11356
11357 /* Create a hash table to map DWO IDs to their CU/TU entry in
11358 .debug_{info,types}.dwo in DWP_FILE.
11359 Returns NULL if there isn't one.
11360 Note: This function processes DWP files only, not DWO files. */
11361
11362 static struct dwp_hash_table *
11363 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11364 struct dwp_file *dwp_file, int is_debug_types)
11365 {
11366 struct objfile *objfile = dwarf2_per_objfile->objfile;
11367 bfd *dbfd = dwp_file->dbfd.get ();
11368 const gdb_byte *index_ptr, *index_end;
11369 struct dwarf2_section_info *index;
11370 uint32_t version, nr_columns, nr_units, nr_slots;
11371 struct dwp_hash_table *htab;
11372
11373 if (is_debug_types)
11374 index = &dwp_file->sections.tu_index;
11375 else
11376 index = &dwp_file->sections.cu_index;
11377
11378 if (index->empty ())
11379 return NULL;
11380 index->read (objfile);
11381
11382 index_ptr = index->buffer;
11383 index_end = index_ptr + index->size;
11384
11385 version = read_4_bytes (dbfd, index_ptr);
11386 index_ptr += 4;
11387 if (version == 2)
11388 nr_columns = read_4_bytes (dbfd, index_ptr);
11389 else
11390 nr_columns = 0;
11391 index_ptr += 4;
11392 nr_units = read_4_bytes (dbfd, index_ptr);
11393 index_ptr += 4;
11394 nr_slots = read_4_bytes (dbfd, index_ptr);
11395 index_ptr += 4;
11396
11397 if (version != 1 && version != 2)
11398 {
11399 error (_("Dwarf Error: unsupported DWP file version (%s)"
11400 " [in module %s]"),
11401 pulongest (version), dwp_file->name);
11402 }
11403 if (nr_slots != (nr_slots & -nr_slots))
11404 {
11405 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11406 " is not power of 2 [in module %s]"),
11407 pulongest (nr_slots), dwp_file->name);
11408 }
11409
11410 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11411 htab->version = version;
11412 htab->nr_columns = nr_columns;
11413 htab->nr_units = nr_units;
11414 htab->nr_slots = nr_slots;
11415 htab->hash_table = index_ptr;
11416 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11417
11418 /* Exit early if the table is empty. */
11419 if (nr_slots == 0 || nr_units == 0
11420 || (version == 2 && nr_columns == 0))
11421 {
11422 /* All must be zero. */
11423 if (nr_slots != 0 || nr_units != 0
11424 || (version == 2 && nr_columns != 0))
11425 {
11426 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11427 " all zero [in modules %s]"),
11428 dwp_file->name);
11429 }
11430 return htab;
11431 }
11432
11433 if (version == 1)
11434 {
11435 htab->section_pool.v1.indices =
11436 htab->unit_table + sizeof (uint32_t) * nr_slots;
11437 /* It's harder to decide whether the section is too small in v1.
11438 V1 is deprecated anyway so we punt. */
11439 }
11440 else
11441 {
11442 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11443 int *ids = htab->section_pool.v2.section_ids;
11444 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11445 /* Reverse map for error checking. */
11446 int ids_seen[DW_SECT_MAX + 1];
11447 int i;
11448
11449 if (nr_columns < 2)
11450 {
11451 error (_("Dwarf Error: bad DWP hash table, too few columns"
11452 " in section table [in module %s]"),
11453 dwp_file->name);
11454 }
11455 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11456 {
11457 error (_("Dwarf Error: bad DWP hash table, too many columns"
11458 " in section table [in module %s]"),
11459 dwp_file->name);
11460 }
11461 memset (ids, 255, sizeof_ids);
11462 memset (ids_seen, 255, sizeof (ids_seen));
11463 for (i = 0; i < nr_columns; ++i)
11464 {
11465 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11466
11467 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11468 {
11469 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11470 " in section table [in module %s]"),
11471 id, dwp_file->name);
11472 }
11473 if (ids_seen[id] != -1)
11474 {
11475 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11476 " id %d in section table [in module %s]"),
11477 id, dwp_file->name);
11478 }
11479 ids_seen[id] = i;
11480 ids[i] = id;
11481 }
11482 /* Must have exactly one info or types section. */
11483 if (((ids_seen[DW_SECT_INFO] != -1)
11484 + (ids_seen[DW_SECT_TYPES] != -1))
11485 != 1)
11486 {
11487 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11488 " DWO info/types section [in module %s]"),
11489 dwp_file->name);
11490 }
11491 /* Must have an abbrev section. */
11492 if (ids_seen[DW_SECT_ABBREV] == -1)
11493 {
11494 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11495 " section [in module %s]"),
11496 dwp_file->name);
11497 }
11498 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11499 htab->section_pool.v2.sizes =
11500 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11501 * nr_units * nr_columns);
11502 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11503 * nr_units * nr_columns))
11504 > index_end)
11505 {
11506 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11507 " [in module %s]"),
11508 dwp_file->name);
11509 }
11510 }
11511
11512 return htab;
11513 }
11514
11515 /* Update SECTIONS with the data from SECTP.
11516
11517 This function is like the other "locate" section routines that are
11518 passed to bfd_map_over_sections, but in this context the sections to
11519 read comes from the DWP V1 hash table, not the full ELF section table.
11520
11521 The result is non-zero for success, or zero if an error was found. */
11522
11523 static int
11524 locate_v1_virtual_dwo_sections (asection *sectp,
11525 struct virtual_v1_dwo_sections *sections)
11526 {
11527 const struct dwop_section_names *names = &dwop_section_names;
11528
11529 if (section_is_p (sectp->name, &names->abbrev_dwo))
11530 {
11531 /* There can be only one. */
11532 if (sections->abbrev.s.section != NULL)
11533 return 0;
11534 sections->abbrev.s.section = sectp;
11535 sections->abbrev.size = bfd_section_size (sectp);
11536 }
11537 else if (section_is_p (sectp->name, &names->info_dwo)
11538 || section_is_p (sectp->name, &names->types_dwo))
11539 {
11540 /* There can be only one. */
11541 if (sections->info_or_types.s.section != NULL)
11542 return 0;
11543 sections->info_or_types.s.section = sectp;
11544 sections->info_or_types.size = bfd_section_size (sectp);
11545 }
11546 else if (section_is_p (sectp->name, &names->line_dwo))
11547 {
11548 /* There can be only one. */
11549 if (sections->line.s.section != NULL)
11550 return 0;
11551 sections->line.s.section = sectp;
11552 sections->line.size = bfd_section_size (sectp);
11553 }
11554 else if (section_is_p (sectp->name, &names->loc_dwo))
11555 {
11556 /* There can be only one. */
11557 if (sections->loc.s.section != NULL)
11558 return 0;
11559 sections->loc.s.section = sectp;
11560 sections->loc.size = bfd_section_size (sectp);
11561 }
11562 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11563 {
11564 /* There can be only one. */
11565 if (sections->macinfo.s.section != NULL)
11566 return 0;
11567 sections->macinfo.s.section = sectp;
11568 sections->macinfo.size = bfd_section_size (sectp);
11569 }
11570 else if (section_is_p (sectp->name, &names->macro_dwo))
11571 {
11572 /* There can be only one. */
11573 if (sections->macro.s.section != NULL)
11574 return 0;
11575 sections->macro.s.section = sectp;
11576 sections->macro.size = bfd_section_size (sectp);
11577 }
11578 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11579 {
11580 /* There can be only one. */
11581 if (sections->str_offsets.s.section != NULL)
11582 return 0;
11583 sections->str_offsets.s.section = sectp;
11584 sections->str_offsets.size = bfd_section_size (sectp);
11585 }
11586 else
11587 {
11588 /* No other kind of section is valid. */
11589 return 0;
11590 }
11591
11592 return 1;
11593 }
11594
11595 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11596 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11597 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11598 This is for DWP version 1 files. */
11599
11600 static struct dwo_unit *
11601 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11602 struct dwp_file *dwp_file,
11603 uint32_t unit_index,
11604 const char *comp_dir,
11605 ULONGEST signature, int is_debug_types)
11606 {
11607 struct objfile *objfile = dwarf2_per_objfile->objfile;
11608 const struct dwp_hash_table *dwp_htab =
11609 is_debug_types ? dwp_file->tus : dwp_file->cus;
11610 bfd *dbfd = dwp_file->dbfd.get ();
11611 const char *kind = is_debug_types ? "TU" : "CU";
11612 struct dwo_file *dwo_file;
11613 struct dwo_unit *dwo_unit;
11614 struct virtual_v1_dwo_sections sections;
11615 void **dwo_file_slot;
11616 int i;
11617
11618 gdb_assert (dwp_file->version == 1);
11619
11620 if (dwarf_read_debug)
11621 {
11622 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11623 kind,
11624 pulongest (unit_index), hex_string (signature),
11625 dwp_file->name);
11626 }
11627
11628 /* Fetch the sections of this DWO unit.
11629 Put a limit on the number of sections we look for so that bad data
11630 doesn't cause us to loop forever. */
11631
11632 #define MAX_NR_V1_DWO_SECTIONS \
11633 (1 /* .debug_info or .debug_types */ \
11634 + 1 /* .debug_abbrev */ \
11635 + 1 /* .debug_line */ \
11636 + 1 /* .debug_loc */ \
11637 + 1 /* .debug_str_offsets */ \
11638 + 1 /* .debug_macro or .debug_macinfo */ \
11639 + 1 /* trailing zero */)
11640
11641 memset (&sections, 0, sizeof (sections));
11642
11643 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11644 {
11645 asection *sectp;
11646 uint32_t section_nr =
11647 read_4_bytes (dbfd,
11648 dwp_htab->section_pool.v1.indices
11649 + (unit_index + i) * sizeof (uint32_t));
11650
11651 if (section_nr == 0)
11652 break;
11653 if (section_nr >= dwp_file->num_sections)
11654 {
11655 error (_("Dwarf Error: bad DWP hash table, section number too large"
11656 " [in module %s]"),
11657 dwp_file->name);
11658 }
11659
11660 sectp = dwp_file->elf_sections[section_nr];
11661 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11662 {
11663 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11664 " [in module %s]"),
11665 dwp_file->name);
11666 }
11667 }
11668
11669 if (i < 2
11670 || sections.info_or_types.empty ()
11671 || sections.abbrev.empty ())
11672 {
11673 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11674 " [in module %s]"),
11675 dwp_file->name);
11676 }
11677 if (i == MAX_NR_V1_DWO_SECTIONS)
11678 {
11679 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11680 " [in module %s]"),
11681 dwp_file->name);
11682 }
11683
11684 /* It's easier for the rest of the code if we fake a struct dwo_file and
11685 have dwo_unit "live" in that. At least for now.
11686
11687 The DWP file can be made up of a random collection of CUs and TUs.
11688 However, for each CU + set of TUs that came from the same original DWO
11689 file, we can combine them back into a virtual DWO file to save space
11690 (fewer struct dwo_file objects to allocate). Remember that for really
11691 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11692
11693 std::string virtual_dwo_name =
11694 string_printf ("virtual-dwo/%d-%d-%d-%d",
11695 sections.abbrev.get_id (),
11696 sections.line.get_id (),
11697 sections.loc.get_id (),
11698 sections.str_offsets.get_id ());
11699 /* Can we use an existing virtual DWO file? */
11700 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11701 virtual_dwo_name.c_str (),
11702 comp_dir);
11703 /* Create one if necessary. */
11704 if (*dwo_file_slot == NULL)
11705 {
11706 if (dwarf_read_debug)
11707 {
11708 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11709 virtual_dwo_name.c_str ());
11710 }
11711 dwo_file = new struct dwo_file;
11712 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11713 dwo_file->comp_dir = comp_dir;
11714 dwo_file->sections.abbrev = sections.abbrev;
11715 dwo_file->sections.line = sections.line;
11716 dwo_file->sections.loc = sections.loc;
11717 dwo_file->sections.macinfo = sections.macinfo;
11718 dwo_file->sections.macro = sections.macro;
11719 dwo_file->sections.str_offsets = sections.str_offsets;
11720 /* The "str" section is global to the entire DWP file. */
11721 dwo_file->sections.str = dwp_file->sections.str;
11722 /* The info or types section is assigned below to dwo_unit,
11723 there's no need to record it in dwo_file.
11724 Also, we can't simply record type sections in dwo_file because
11725 we record a pointer into the vector in dwo_unit. As we collect more
11726 types we'll grow the vector and eventually have to reallocate space
11727 for it, invalidating all copies of pointers into the previous
11728 contents. */
11729 *dwo_file_slot = dwo_file;
11730 }
11731 else
11732 {
11733 if (dwarf_read_debug)
11734 {
11735 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11736 virtual_dwo_name.c_str ());
11737 }
11738 dwo_file = (struct dwo_file *) *dwo_file_slot;
11739 }
11740
11741 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11742 dwo_unit->dwo_file = dwo_file;
11743 dwo_unit->signature = signature;
11744 dwo_unit->section =
11745 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11746 *dwo_unit->section = sections.info_or_types;
11747 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11748
11749 return dwo_unit;
11750 }
11751
11752 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11753 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11754 piece within that section used by a TU/CU, return a virtual section
11755 of just that piece. */
11756
11757 static struct dwarf2_section_info
11758 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11759 struct dwarf2_section_info *section,
11760 bfd_size_type offset, bfd_size_type size)
11761 {
11762 struct dwarf2_section_info result;
11763 asection *sectp;
11764
11765 gdb_assert (section != NULL);
11766 gdb_assert (!section->is_virtual);
11767
11768 memset (&result, 0, sizeof (result));
11769 result.s.containing_section = section;
11770 result.is_virtual = true;
11771
11772 if (size == 0)
11773 return result;
11774
11775 sectp = section->get_bfd_section ();
11776
11777 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11778 bounds of the real section. This is a pretty-rare event, so just
11779 flag an error (easier) instead of a warning and trying to cope. */
11780 if (sectp == NULL
11781 || offset + size > bfd_section_size (sectp))
11782 {
11783 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11784 " in section %s [in module %s]"),
11785 sectp ? bfd_section_name (sectp) : "<unknown>",
11786 objfile_name (dwarf2_per_objfile->objfile));
11787 }
11788
11789 result.virtual_offset = offset;
11790 result.size = size;
11791 return result;
11792 }
11793
11794 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11795 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11796 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11797 This is for DWP version 2 files. */
11798
11799 static struct dwo_unit *
11800 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11801 struct dwp_file *dwp_file,
11802 uint32_t unit_index,
11803 const char *comp_dir,
11804 ULONGEST signature, int is_debug_types)
11805 {
11806 struct objfile *objfile = dwarf2_per_objfile->objfile;
11807 const struct dwp_hash_table *dwp_htab =
11808 is_debug_types ? dwp_file->tus : dwp_file->cus;
11809 bfd *dbfd = dwp_file->dbfd.get ();
11810 const char *kind = is_debug_types ? "TU" : "CU";
11811 struct dwo_file *dwo_file;
11812 struct dwo_unit *dwo_unit;
11813 struct virtual_v2_dwo_sections sections;
11814 void **dwo_file_slot;
11815 int i;
11816
11817 gdb_assert (dwp_file->version == 2);
11818
11819 if (dwarf_read_debug)
11820 {
11821 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11822 kind,
11823 pulongest (unit_index), hex_string (signature),
11824 dwp_file->name);
11825 }
11826
11827 /* Fetch the section offsets of this DWO unit. */
11828
11829 memset (&sections, 0, sizeof (sections));
11830
11831 for (i = 0; i < dwp_htab->nr_columns; ++i)
11832 {
11833 uint32_t offset = read_4_bytes (dbfd,
11834 dwp_htab->section_pool.v2.offsets
11835 + (((unit_index - 1) * dwp_htab->nr_columns
11836 + i)
11837 * sizeof (uint32_t)));
11838 uint32_t size = read_4_bytes (dbfd,
11839 dwp_htab->section_pool.v2.sizes
11840 + (((unit_index - 1) * dwp_htab->nr_columns
11841 + i)
11842 * sizeof (uint32_t)));
11843
11844 switch (dwp_htab->section_pool.v2.section_ids[i])
11845 {
11846 case DW_SECT_INFO:
11847 case DW_SECT_TYPES:
11848 sections.info_or_types_offset = offset;
11849 sections.info_or_types_size = size;
11850 break;
11851 case DW_SECT_ABBREV:
11852 sections.abbrev_offset = offset;
11853 sections.abbrev_size = size;
11854 break;
11855 case DW_SECT_LINE:
11856 sections.line_offset = offset;
11857 sections.line_size = size;
11858 break;
11859 case DW_SECT_LOC:
11860 sections.loc_offset = offset;
11861 sections.loc_size = size;
11862 break;
11863 case DW_SECT_STR_OFFSETS:
11864 sections.str_offsets_offset = offset;
11865 sections.str_offsets_size = size;
11866 break;
11867 case DW_SECT_MACINFO:
11868 sections.macinfo_offset = offset;
11869 sections.macinfo_size = size;
11870 break;
11871 case DW_SECT_MACRO:
11872 sections.macro_offset = offset;
11873 sections.macro_size = size;
11874 break;
11875 }
11876 }
11877
11878 /* It's easier for the rest of the code if we fake a struct dwo_file and
11879 have dwo_unit "live" in that. At least for now.
11880
11881 The DWP file can be made up of a random collection of CUs and TUs.
11882 However, for each CU + set of TUs that came from the same original DWO
11883 file, we can combine them back into a virtual DWO file to save space
11884 (fewer struct dwo_file objects to allocate). Remember that for really
11885 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11886
11887 std::string virtual_dwo_name =
11888 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11889 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11890 (long) (sections.line_size ? sections.line_offset : 0),
11891 (long) (sections.loc_size ? sections.loc_offset : 0),
11892 (long) (sections.str_offsets_size
11893 ? sections.str_offsets_offset : 0));
11894 /* Can we use an existing virtual DWO file? */
11895 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11896 virtual_dwo_name.c_str (),
11897 comp_dir);
11898 /* Create one if necessary. */
11899 if (*dwo_file_slot == NULL)
11900 {
11901 if (dwarf_read_debug)
11902 {
11903 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11904 virtual_dwo_name.c_str ());
11905 }
11906 dwo_file = new struct dwo_file;
11907 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11908 dwo_file->comp_dir = comp_dir;
11909 dwo_file->sections.abbrev =
11910 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11911 sections.abbrev_offset, sections.abbrev_size);
11912 dwo_file->sections.line =
11913 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11914 sections.line_offset, sections.line_size);
11915 dwo_file->sections.loc =
11916 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11917 sections.loc_offset, sections.loc_size);
11918 dwo_file->sections.macinfo =
11919 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11920 sections.macinfo_offset, sections.macinfo_size);
11921 dwo_file->sections.macro =
11922 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11923 sections.macro_offset, sections.macro_size);
11924 dwo_file->sections.str_offsets =
11925 create_dwp_v2_section (dwarf2_per_objfile,
11926 &dwp_file->sections.str_offsets,
11927 sections.str_offsets_offset,
11928 sections.str_offsets_size);
11929 /* The "str" section is global to the entire DWP file. */
11930 dwo_file->sections.str = dwp_file->sections.str;
11931 /* The info or types section is assigned below to dwo_unit,
11932 there's no need to record it in dwo_file.
11933 Also, we can't simply record type sections in dwo_file because
11934 we record a pointer into the vector in dwo_unit. As we collect more
11935 types we'll grow the vector and eventually have to reallocate space
11936 for it, invalidating all copies of pointers into the previous
11937 contents. */
11938 *dwo_file_slot = dwo_file;
11939 }
11940 else
11941 {
11942 if (dwarf_read_debug)
11943 {
11944 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11945 virtual_dwo_name.c_str ());
11946 }
11947 dwo_file = (struct dwo_file *) *dwo_file_slot;
11948 }
11949
11950 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11951 dwo_unit->dwo_file = dwo_file;
11952 dwo_unit->signature = signature;
11953 dwo_unit->section =
11954 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11955 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11956 is_debug_types
11957 ? &dwp_file->sections.types
11958 : &dwp_file->sections.info,
11959 sections.info_or_types_offset,
11960 sections.info_or_types_size);
11961 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11962
11963 return dwo_unit;
11964 }
11965
11966 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11967 Returns NULL if the signature isn't found. */
11968
11969 static struct dwo_unit *
11970 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11971 struct dwp_file *dwp_file, const char *comp_dir,
11972 ULONGEST signature, int is_debug_types)
11973 {
11974 const struct dwp_hash_table *dwp_htab =
11975 is_debug_types ? dwp_file->tus : dwp_file->cus;
11976 bfd *dbfd = dwp_file->dbfd.get ();
11977 uint32_t mask = dwp_htab->nr_slots - 1;
11978 uint32_t hash = signature & mask;
11979 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11980 unsigned int i;
11981 void **slot;
11982 struct dwo_unit find_dwo_cu;
11983
11984 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11985 find_dwo_cu.signature = signature;
11986 slot = htab_find_slot (is_debug_types
11987 ? dwp_file->loaded_tus.get ()
11988 : dwp_file->loaded_cus.get (),
11989 &find_dwo_cu, INSERT);
11990
11991 if (*slot != NULL)
11992 return (struct dwo_unit *) *slot;
11993
11994 /* Use a for loop so that we don't loop forever on bad debug info. */
11995 for (i = 0; i < dwp_htab->nr_slots; ++i)
11996 {
11997 ULONGEST signature_in_table;
11998
11999 signature_in_table =
12000 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12001 if (signature_in_table == signature)
12002 {
12003 uint32_t unit_index =
12004 read_4_bytes (dbfd,
12005 dwp_htab->unit_table + hash * sizeof (uint32_t));
12006
12007 if (dwp_file->version == 1)
12008 {
12009 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12010 dwp_file, unit_index,
12011 comp_dir, signature,
12012 is_debug_types);
12013 }
12014 else
12015 {
12016 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12017 dwp_file, unit_index,
12018 comp_dir, signature,
12019 is_debug_types);
12020 }
12021 return (struct dwo_unit *) *slot;
12022 }
12023 if (signature_in_table == 0)
12024 return NULL;
12025 hash = (hash + hash2) & mask;
12026 }
12027
12028 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12029 " [in module %s]"),
12030 dwp_file->name);
12031 }
12032
12033 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12034 Open the file specified by FILE_NAME and hand it off to BFD for
12035 preliminary analysis. Return a newly initialized bfd *, which
12036 includes a canonicalized copy of FILE_NAME.
12037 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12038 SEARCH_CWD is true if the current directory is to be searched.
12039 It will be searched before debug-file-directory.
12040 If successful, the file is added to the bfd include table of the
12041 objfile's bfd (see gdb_bfd_record_inclusion).
12042 If unable to find/open the file, return NULL.
12043 NOTE: This function is derived from symfile_bfd_open. */
12044
12045 static gdb_bfd_ref_ptr
12046 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12047 const char *file_name, int is_dwp, int search_cwd)
12048 {
12049 int desc;
12050 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12051 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12052 to debug_file_directory. */
12053 const char *search_path;
12054 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12055
12056 gdb::unique_xmalloc_ptr<char> search_path_holder;
12057 if (search_cwd)
12058 {
12059 if (*debug_file_directory != '\0')
12060 {
12061 search_path_holder.reset (concat (".", dirname_separator_string,
12062 debug_file_directory,
12063 (char *) NULL));
12064 search_path = search_path_holder.get ();
12065 }
12066 else
12067 search_path = ".";
12068 }
12069 else
12070 search_path = debug_file_directory;
12071
12072 openp_flags flags = OPF_RETURN_REALPATH;
12073 if (is_dwp)
12074 flags |= OPF_SEARCH_IN_PATH;
12075
12076 gdb::unique_xmalloc_ptr<char> absolute_name;
12077 desc = openp (search_path, flags, file_name,
12078 O_RDONLY | O_BINARY, &absolute_name);
12079 if (desc < 0)
12080 return NULL;
12081
12082 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12083 gnutarget, desc));
12084 if (sym_bfd == NULL)
12085 return NULL;
12086 bfd_set_cacheable (sym_bfd.get (), 1);
12087
12088 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12089 return NULL;
12090
12091 /* Success. Record the bfd as having been included by the objfile's bfd.
12092 This is important because things like demangled_names_hash lives in the
12093 objfile's per_bfd space and may have references to things like symbol
12094 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12095 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12096
12097 return sym_bfd;
12098 }
12099
12100 /* Try to open DWO file FILE_NAME.
12101 COMP_DIR is the DW_AT_comp_dir attribute.
12102 The result is the bfd handle of the file.
12103 If there is a problem finding or opening the file, return NULL.
12104 Upon success, the canonicalized path of the file is stored in the bfd,
12105 same as symfile_bfd_open. */
12106
12107 static gdb_bfd_ref_ptr
12108 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12109 const char *file_name, const char *comp_dir)
12110 {
12111 if (IS_ABSOLUTE_PATH (file_name))
12112 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12113 0 /*is_dwp*/, 0 /*search_cwd*/);
12114
12115 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12116
12117 if (comp_dir != NULL)
12118 {
12119 gdb::unique_xmalloc_ptr<char> path_to_try
12120 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12121
12122 /* NOTE: If comp_dir is a relative path, this will also try the
12123 search path, which seems useful. */
12124 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12125 path_to_try.get (),
12126 0 /*is_dwp*/,
12127 1 /*search_cwd*/));
12128 if (abfd != NULL)
12129 return abfd;
12130 }
12131
12132 /* That didn't work, try debug-file-directory, which, despite its name,
12133 is a list of paths. */
12134
12135 if (*debug_file_directory == '\0')
12136 return NULL;
12137
12138 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12139 0 /*is_dwp*/, 1 /*search_cwd*/);
12140 }
12141
12142 /* This function is mapped across the sections and remembers the offset and
12143 size of each of the DWO debugging sections we are interested in. */
12144
12145 static void
12146 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12147 {
12148 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12149 const struct dwop_section_names *names = &dwop_section_names;
12150
12151 if (section_is_p (sectp->name, &names->abbrev_dwo))
12152 {
12153 dwo_sections->abbrev.s.section = sectp;
12154 dwo_sections->abbrev.size = bfd_section_size (sectp);
12155 }
12156 else if (section_is_p (sectp->name, &names->info_dwo))
12157 {
12158 dwo_sections->info.s.section = sectp;
12159 dwo_sections->info.size = bfd_section_size (sectp);
12160 }
12161 else if (section_is_p (sectp->name, &names->line_dwo))
12162 {
12163 dwo_sections->line.s.section = sectp;
12164 dwo_sections->line.size = bfd_section_size (sectp);
12165 }
12166 else if (section_is_p (sectp->name, &names->loc_dwo))
12167 {
12168 dwo_sections->loc.s.section = sectp;
12169 dwo_sections->loc.size = bfd_section_size (sectp);
12170 }
12171 else if (section_is_p (sectp->name, &names->loclists_dwo))
12172 {
12173 dwo_sections->loclists.s.section = sectp;
12174 dwo_sections->loclists.size = bfd_section_size (sectp);
12175 }
12176 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12177 {
12178 dwo_sections->macinfo.s.section = sectp;
12179 dwo_sections->macinfo.size = bfd_section_size (sectp);
12180 }
12181 else if (section_is_p (sectp->name, &names->macro_dwo))
12182 {
12183 dwo_sections->macro.s.section = sectp;
12184 dwo_sections->macro.size = bfd_section_size (sectp);
12185 }
12186 else if (section_is_p (sectp->name, &names->str_dwo))
12187 {
12188 dwo_sections->str.s.section = sectp;
12189 dwo_sections->str.size = bfd_section_size (sectp);
12190 }
12191 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12192 {
12193 dwo_sections->str_offsets.s.section = sectp;
12194 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12195 }
12196 else if (section_is_p (sectp->name, &names->types_dwo))
12197 {
12198 struct dwarf2_section_info type_section;
12199
12200 memset (&type_section, 0, sizeof (type_section));
12201 type_section.s.section = sectp;
12202 type_section.size = bfd_section_size (sectp);
12203 dwo_sections->types.push_back (type_section);
12204 }
12205 }
12206
12207 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12208 by PER_CU. This is for the non-DWP case.
12209 The result is NULL if DWO_NAME can't be found. */
12210
12211 static struct dwo_file *
12212 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12213 const char *dwo_name, const char *comp_dir)
12214 {
12215 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12216
12217 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12218 if (dbfd == NULL)
12219 {
12220 if (dwarf_read_debug)
12221 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12222 return NULL;
12223 }
12224
12225 dwo_file_up dwo_file (new struct dwo_file);
12226 dwo_file->dwo_name = dwo_name;
12227 dwo_file->comp_dir = comp_dir;
12228 dwo_file->dbfd = std::move (dbfd);
12229
12230 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12231 &dwo_file->sections);
12232
12233 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12234 dwo_file->sections.info, dwo_file->cus);
12235
12236 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12237 dwo_file->sections.types, dwo_file->tus);
12238
12239 if (dwarf_read_debug)
12240 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12241
12242 return dwo_file.release ();
12243 }
12244
12245 /* This function is mapped across the sections and remembers the offset and
12246 size of each of the DWP debugging sections common to version 1 and 2 that
12247 we are interested in. */
12248
12249 static void
12250 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12251 void *dwp_file_ptr)
12252 {
12253 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12254 const struct dwop_section_names *names = &dwop_section_names;
12255 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12256
12257 /* Record the ELF section number for later lookup: this is what the
12258 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12259 gdb_assert (elf_section_nr < dwp_file->num_sections);
12260 dwp_file->elf_sections[elf_section_nr] = sectp;
12261
12262 /* Look for specific sections that we need. */
12263 if (section_is_p (sectp->name, &names->str_dwo))
12264 {
12265 dwp_file->sections.str.s.section = sectp;
12266 dwp_file->sections.str.size = bfd_section_size (sectp);
12267 }
12268 else if (section_is_p (sectp->name, &names->cu_index))
12269 {
12270 dwp_file->sections.cu_index.s.section = sectp;
12271 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12272 }
12273 else if (section_is_p (sectp->name, &names->tu_index))
12274 {
12275 dwp_file->sections.tu_index.s.section = sectp;
12276 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12277 }
12278 }
12279
12280 /* This function is mapped across the sections and remembers the offset and
12281 size of each of the DWP version 2 debugging sections that we are interested
12282 in. This is split into a separate function because we don't know if we
12283 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12284
12285 static void
12286 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12287 {
12288 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12289 const struct dwop_section_names *names = &dwop_section_names;
12290 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12291
12292 /* Record the ELF section number for later lookup: this is what the
12293 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12294 gdb_assert (elf_section_nr < dwp_file->num_sections);
12295 dwp_file->elf_sections[elf_section_nr] = sectp;
12296
12297 /* Look for specific sections that we need. */
12298 if (section_is_p (sectp->name, &names->abbrev_dwo))
12299 {
12300 dwp_file->sections.abbrev.s.section = sectp;
12301 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12302 }
12303 else if (section_is_p (sectp->name, &names->info_dwo))
12304 {
12305 dwp_file->sections.info.s.section = sectp;
12306 dwp_file->sections.info.size = bfd_section_size (sectp);
12307 }
12308 else if (section_is_p (sectp->name, &names->line_dwo))
12309 {
12310 dwp_file->sections.line.s.section = sectp;
12311 dwp_file->sections.line.size = bfd_section_size (sectp);
12312 }
12313 else if (section_is_p (sectp->name, &names->loc_dwo))
12314 {
12315 dwp_file->sections.loc.s.section = sectp;
12316 dwp_file->sections.loc.size = bfd_section_size (sectp);
12317 }
12318 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12319 {
12320 dwp_file->sections.macinfo.s.section = sectp;
12321 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12322 }
12323 else if (section_is_p (sectp->name, &names->macro_dwo))
12324 {
12325 dwp_file->sections.macro.s.section = sectp;
12326 dwp_file->sections.macro.size = bfd_section_size (sectp);
12327 }
12328 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12329 {
12330 dwp_file->sections.str_offsets.s.section = sectp;
12331 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12332 }
12333 else if (section_is_p (sectp->name, &names->types_dwo))
12334 {
12335 dwp_file->sections.types.s.section = sectp;
12336 dwp_file->sections.types.size = bfd_section_size (sectp);
12337 }
12338 }
12339
12340 /* Hash function for dwp_file loaded CUs/TUs. */
12341
12342 static hashval_t
12343 hash_dwp_loaded_cutus (const void *item)
12344 {
12345 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12346
12347 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12348 return dwo_unit->signature;
12349 }
12350
12351 /* Equality function for dwp_file loaded CUs/TUs. */
12352
12353 static int
12354 eq_dwp_loaded_cutus (const void *a, const void *b)
12355 {
12356 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12357 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12358
12359 return dua->signature == dub->signature;
12360 }
12361
12362 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12363
12364 static htab_up
12365 allocate_dwp_loaded_cutus_table ()
12366 {
12367 return htab_up (htab_create_alloc (3,
12368 hash_dwp_loaded_cutus,
12369 eq_dwp_loaded_cutus,
12370 NULL, xcalloc, xfree));
12371 }
12372
12373 /* Try to open DWP file FILE_NAME.
12374 The result is the bfd handle of the file.
12375 If there is a problem finding or opening the file, return NULL.
12376 Upon success, the canonicalized path of the file is stored in the bfd,
12377 same as symfile_bfd_open. */
12378
12379 static gdb_bfd_ref_ptr
12380 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12381 const char *file_name)
12382 {
12383 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12384 1 /*is_dwp*/,
12385 1 /*search_cwd*/));
12386 if (abfd != NULL)
12387 return abfd;
12388
12389 /* Work around upstream bug 15652.
12390 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12391 [Whether that's a "bug" is debatable, but it is getting in our way.]
12392 We have no real idea where the dwp file is, because gdb's realpath-ing
12393 of the executable's path may have discarded the needed info.
12394 [IWBN if the dwp file name was recorded in the executable, akin to
12395 .gnu_debuglink, but that doesn't exist yet.]
12396 Strip the directory from FILE_NAME and search again. */
12397 if (*debug_file_directory != '\0')
12398 {
12399 /* Don't implicitly search the current directory here.
12400 If the user wants to search "." to handle this case,
12401 it must be added to debug-file-directory. */
12402 return try_open_dwop_file (dwarf2_per_objfile,
12403 lbasename (file_name), 1 /*is_dwp*/,
12404 0 /*search_cwd*/);
12405 }
12406
12407 return NULL;
12408 }
12409
12410 /* Initialize the use of the DWP file for the current objfile.
12411 By convention the name of the DWP file is ${objfile}.dwp.
12412 The result is NULL if it can't be found. */
12413
12414 static std::unique_ptr<struct dwp_file>
12415 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12416 {
12417 struct objfile *objfile = dwarf2_per_objfile->objfile;
12418
12419 /* Try to find first .dwp for the binary file before any symbolic links
12420 resolving. */
12421
12422 /* If the objfile is a debug file, find the name of the real binary
12423 file and get the name of dwp file from there. */
12424 std::string dwp_name;
12425 if (objfile->separate_debug_objfile_backlink != NULL)
12426 {
12427 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12428 const char *backlink_basename = lbasename (backlink->original_name);
12429
12430 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12431 }
12432 else
12433 dwp_name = objfile->original_name;
12434
12435 dwp_name += ".dwp";
12436
12437 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12438 if (dbfd == NULL
12439 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12440 {
12441 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12442 dwp_name = objfile_name (objfile);
12443 dwp_name += ".dwp";
12444 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12445 }
12446
12447 if (dbfd == NULL)
12448 {
12449 if (dwarf_read_debug)
12450 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12451 return std::unique_ptr<dwp_file> ();
12452 }
12453
12454 const char *name = bfd_get_filename (dbfd.get ());
12455 std::unique_ptr<struct dwp_file> dwp_file
12456 (new struct dwp_file (name, std::move (dbfd)));
12457
12458 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12459 dwp_file->elf_sections =
12460 OBSTACK_CALLOC (&objfile->objfile_obstack,
12461 dwp_file->num_sections, asection *);
12462
12463 bfd_map_over_sections (dwp_file->dbfd.get (),
12464 dwarf2_locate_common_dwp_sections,
12465 dwp_file.get ());
12466
12467 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12468 0);
12469
12470 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12471 1);
12472
12473 /* The DWP file version is stored in the hash table. Oh well. */
12474 if (dwp_file->cus && dwp_file->tus
12475 && dwp_file->cus->version != dwp_file->tus->version)
12476 {
12477 /* Technically speaking, we should try to limp along, but this is
12478 pretty bizarre. We use pulongest here because that's the established
12479 portability solution (e.g, we cannot use %u for uint32_t). */
12480 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12481 " TU version %s [in DWP file %s]"),
12482 pulongest (dwp_file->cus->version),
12483 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12484 }
12485
12486 if (dwp_file->cus)
12487 dwp_file->version = dwp_file->cus->version;
12488 else if (dwp_file->tus)
12489 dwp_file->version = dwp_file->tus->version;
12490 else
12491 dwp_file->version = 2;
12492
12493 if (dwp_file->version == 2)
12494 bfd_map_over_sections (dwp_file->dbfd.get (),
12495 dwarf2_locate_v2_dwp_sections,
12496 dwp_file.get ());
12497
12498 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12499 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12500
12501 if (dwarf_read_debug)
12502 {
12503 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12504 fprintf_unfiltered (gdb_stdlog,
12505 " %s CUs, %s TUs\n",
12506 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12507 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12508 }
12509
12510 return dwp_file;
12511 }
12512
12513 /* Wrapper around open_and_init_dwp_file, only open it once. */
12514
12515 static struct dwp_file *
12516 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12517 {
12518 if (! dwarf2_per_objfile->dwp_checked)
12519 {
12520 dwarf2_per_objfile->dwp_file
12521 = open_and_init_dwp_file (dwarf2_per_objfile);
12522 dwarf2_per_objfile->dwp_checked = 1;
12523 }
12524 return dwarf2_per_objfile->dwp_file.get ();
12525 }
12526
12527 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12528 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12529 or in the DWP file for the objfile, referenced by THIS_UNIT.
12530 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12531 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12532
12533 This is called, for example, when wanting to read a variable with a
12534 complex location. Therefore we don't want to do file i/o for every call.
12535 Therefore we don't want to look for a DWO file on every call.
12536 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12537 then we check if we've already seen DWO_NAME, and only THEN do we check
12538 for a DWO file.
12539
12540 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12541 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12542
12543 static struct dwo_unit *
12544 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12545 const char *dwo_name, const char *comp_dir,
12546 ULONGEST signature, int is_debug_types)
12547 {
12548 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12549 struct objfile *objfile = dwarf2_per_objfile->objfile;
12550 const char *kind = is_debug_types ? "TU" : "CU";
12551 void **dwo_file_slot;
12552 struct dwo_file *dwo_file;
12553 struct dwp_file *dwp_file;
12554
12555 /* First see if there's a DWP file.
12556 If we have a DWP file but didn't find the DWO inside it, don't
12557 look for the original DWO file. It makes gdb behave differently
12558 depending on whether one is debugging in the build tree. */
12559
12560 dwp_file = get_dwp_file (dwarf2_per_objfile);
12561 if (dwp_file != NULL)
12562 {
12563 const struct dwp_hash_table *dwp_htab =
12564 is_debug_types ? dwp_file->tus : dwp_file->cus;
12565
12566 if (dwp_htab != NULL)
12567 {
12568 struct dwo_unit *dwo_cutu =
12569 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12570 signature, is_debug_types);
12571
12572 if (dwo_cutu != NULL)
12573 {
12574 if (dwarf_read_debug)
12575 {
12576 fprintf_unfiltered (gdb_stdlog,
12577 "Virtual DWO %s %s found: @%s\n",
12578 kind, hex_string (signature),
12579 host_address_to_string (dwo_cutu));
12580 }
12581 return dwo_cutu;
12582 }
12583 }
12584 }
12585 else
12586 {
12587 /* No DWP file, look for the DWO file. */
12588
12589 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12590 dwo_name, comp_dir);
12591 if (*dwo_file_slot == NULL)
12592 {
12593 /* Read in the file and build a table of the CUs/TUs it contains. */
12594 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12595 }
12596 /* NOTE: This will be NULL if unable to open the file. */
12597 dwo_file = (struct dwo_file *) *dwo_file_slot;
12598
12599 if (dwo_file != NULL)
12600 {
12601 struct dwo_unit *dwo_cutu = NULL;
12602
12603 if (is_debug_types && dwo_file->tus)
12604 {
12605 struct dwo_unit find_dwo_cutu;
12606
12607 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12608 find_dwo_cutu.signature = signature;
12609 dwo_cutu
12610 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12611 &find_dwo_cutu);
12612 }
12613 else if (!is_debug_types && dwo_file->cus)
12614 {
12615 struct dwo_unit find_dwo_cutu;
12616
12617 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12618 find_dwo_cutu.signature = signature;
12619 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12620 &find_dwo_cutu);
12621 }
12622
12623 if (dwo_cutu != NULL)
12624 {
12625 if (dwarf_read_debug)
12626 {
12627 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12628 kind, dwo_name, hex_string (signature),
12629 host_address_to_string (dwo_cutu));
12630 }
12631 return dwo_cutu;
12632 }
12633 }
12634 }
12635
12636 /* We didn't find it. This could mean a dwo_id mismatch, or
12637 someone deleted the DWO/DWP file, or the search path isn't set up
12638 correctly to find the file. */
12639
12640 if (dwarf_read_debug)
12641 {
12642 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12643 kind, dwo_name, hex_string (signature));
12644 }
12645
12646 /* This is a warning and not a complaint because it can be caused by
12647 pilot error (e.g., user accidentally deleting the DWO). */
12648 {
12649 /* Print the name of the DWP file if we looked there, helps the user
12650 better diagnose the problem. */
12651 std::string dwp_text;
12652
12653 if (dwp_file != NULL)
12654 dwp_text = string_printf (" [in DWP file %s]",
12655 lbasename (dwp_file->name));
12656
12657 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12658 " [in module %s]"),
12659 kind, dwo_name, hex_string (signature),
12660 dwp_text.c_str (),
12661 this_unit->is_debug_types ? "TU" : "CU",
12662 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12663 }
12664 return NULL;
12665 }
12666
12667 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12668 See lookup_dwo_cutu_unit for details. */
12669
12670 static struct dwo_unit *
12671 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12672 const char *dwo_name, const char *comp_dir,
12673 ULONGEST signature)
12674 {
12675 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12676 }
12677
12678 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12679 See lookup_dwo_cutu_unit for details. */
12680
12681 static struct dwo_unit *
12682 lookup_dwo_type_unit (struct signatured_type *this_tu,
12683 const char *dwo_name, const char *comp_dir)
12684 {
12685 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12686 }
12687
12688 /* Traversal function for queue_and_load_all_dwo_tus. */
12689
12690 static int
12691 queue_and_load_dwo_tu (void **slot, void *info)
12692 {
12693 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12694 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12695 ULONGEST signature = dwo_unit->signature;
12696 struct signatured_type *sig_type =
12697 lookup_dwo_signatured_type (per_cu->cu, signature);
12698
12699 if (sig_type != NULL)
12700 {
12701 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12702
12703 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12704 a real dependency of PER_CU on SIG_TYPE. That is detected later
12705 while processing PER_CU. */
12706 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12707 load_full_type_unit (sig_cu);
12708 per_cu->imported_symtabs_push (sig_cu);
12709 }
12710
12711 return 1;
12712 }
12713
12714 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12715 The DWO may have the only definition of the type, though it may not be
12716 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12717 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12718
12719 static void
12720 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12721 {
12722 struct dwo_unit *dwo_unit;
12723 struct dwo_file *dwo_file;
12724
12725 gdb_assert (!per_cu->is_debug_types);
12726 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12727 gdb_assert (per_cu->cu != NULL);
12728
12729 dwo_unit = per_cu->cu->dwo_unit;
12730 gdb_assert (dwo_unit != NULL);
12731
12732 dwo_file = dwo_unit->dwo_file;
12733 if (dwo_file->tus != NULL)
12734 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12735 per_cu);
12736 }
12737
12738 /* Read in various DIEs. */
12739
12740 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12741 Inherit only the children of the DW_AT_abstract_origin DIE not being
12742 already referenced by DW_AT_abstract_origin from the children of the
12743 current DIE. */
12744
12745 static void
12746 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12747 {
12748 struct die_info *child_die;
12749 sect_offset *offsetp;
12750 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12751 struct die_info *origin_die;
12752 /* Iterator of the ORIGIN_DIE children. */
12753 struct die_info *origin_child_die;
12754 struct attribute *attr;
12755 struct dwarf2_cu *origin_cu;
12756 struct pending **origin_previous_list_in_scope;
12757
12758 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12759 if (!attr)
12760 return;
12761
12762 /* Note that following die references may follow to a die in a
12763 different cu. */
12764
12765 origin_cu = cu;
12766 origin_die = follow_die_ref (die, attr, &origin_cu);
12767
12768 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12769 symbols in. */
12770 origin_previous_list_in_scope = origin_cu->list_in_scope;
12771 origin_cu->list_in_scope = cu->list_in_scope;
12772
12773 if (die->tag != origin_die->tag
12774 && !(die->tag == DW_TAG_inlined_subroutine
12775 && origin_die->tag == DW_TAG_subprogram))
12776 complaint (_("DIE %s and its abstract origin %s have different tags"),
12777 sect_offset_str (die->sect_off),
12778 sect_offset_str (origin_die->sect_off));
12779
12780 std::vector<sect_offset> offsets;
12781
12782 for (child_die = die->child;
12783 child_die && child_die->tag;
12784 child_die = child_die->sibling)
12785 {
12786 struct die_info *child_origin_die;
12787 struct dwarf2_cu *child_origin_cu;
12788
12789 /* We are trying to process concrete instance entries:
12790 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12791 it's not relevant to our analysis here. i.e. detecting DIEs that are
12792 present in the abstract instance but not referenced in the concrete
12793 one. */
12794 if (child_die->tag == DW_TAG_call_site
12795 || child_die->tag == DW_TAG_GNU_call_site)
12796 continue;
12797
12798 /* For each CHILD_DIE, find the corresponding child of
12799 ORIGIN_DIE. If there is more than one layer of
12800 DW_AT_abstract_origin, follow them all; there shouldn't be,
12801 but GCC versions at least through 4.4 generate this (GCC PR
12802 40573). */
12803 child_origin_die = child_die;
12804 child_origin_cu = cu;
12805 while (1)
12806 {
12807 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12808 child_origin_cu);
12809 if (attr == NULL)
12810 break;
12811 child_origin_die = follow_die_ref (child_origin_die, attr,
12812 &child_origin_cu);
12813 }
12814
12815 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12816 counterpart may exist. */
12817 if (child_origin_die != child_die)
12818 {
12819 if (child_die->tag != child_origin_die->tag
12820 && !(child_die->tag == DW_TAG_inlined_subroutine
12821 && child_origin_die->tag == DW_TAG_subprogram))
12822 complaint (_("Child DIE %s and its abstract origin %s have "
12823 "different tags"),
12824 sect_offset_str (child_die->sect_off),
12825 sect_offset_str (child_origin_die->sect_off));
12826 if (child_origin_die->parent != origin_die)
12827 complaint (_("Child DIE %s and its abstract origin %s have "
12828 "different parents"),
12829 sect_offset_str (child_die->sect_off),
12830 sect_offset_str (child_origin_die->sect_off));
12831 else
12832 offsets.push_back (child_origin_die->sect_off);
12833 }
12834 }
12835 std::sort (offsets.begin (), offsets.end ());
12836 sect_offset *offsets_end = offsets.data () + offsets.size ();
12837 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12838 if (offsetp[-1] == *offsetp)
12839 complaint (_("Multiple children of DIE %s refer "
12840 "to DIE %s as their abstract origin"),
12841 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12842
12843 offsetp = offsets.data ();
12844 origin_child_die = origin_die->child;
12845 while (origin_child_die && origin_child_die->tag)
12846 {
12847 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12848 while (offsetp < offsets_end
12849 && *offsetp < origin_child_die->sect_off)
12850 offsetp++;
12851 if (offsetp >= offsets_end
12852 || *offsetp > origin_child_die->sect_off)
12853 {
12854 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12855 Check whether we're already processing ORIGIN_CHILD_DIE.
12856 This can happen with mutually referenced abstract_origins.
12857 PR 16581. */
12858 if (!origin_child_die->in_process)
12859 process_die (origin_child_die, origin_cu);
12860 }
12861 origin_child_die = origin_child_die->sibling;
12862 }
12863 origin_cu->list_in_scope = origin_previous_list_in_scope;
12864
12865 if (cu != origin_cu)
12866 compute_delayed_physnames (origin_cu);
12867 }
12868
12869 static void
12870 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12871 {
12872 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12873 struct gdbarch *gdbarch = objfile->arch ();
12874 struct context_stack *newobj;
12875 CORE_ADDR lowpc;
12876 CORE_ADDR highpc;
12877 struct die_info *child_die;
12878 struct attribute *attr, *call_line, *call_file;
12879 const char *name;
12880 CORE_ADDR baseaddr;
12881 struct block *block;
12882 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12883 std::vector<struct symbol *> template_args;
12884 struct template_symbol *templ_func = NULL;
12885
12886 if (inlined_func)
12887 {
12888 /* If we do not have call site information, we can't show the
12889 caller of this inlined function. That's too confusing, so
12890 only use the scope for local variables. */
12891 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12892 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12893 if (call_line == NULL || call_file == NULL)
12894 {
12895 read_lexical_block_scope (die, cu);
12896 return;
12897 }
12898 }
12899
12900 baseaddr = objfile->text_section_offset ();
12901
12902 name = dwarf2_name (die, cu);
12903
12904 /* Ignore functions with missing or empty names. These are actually
12905 illegal according to the DWARF standard. */
12906 if (name == NULL)
12907 {
12908 complaint (_("missing name for subprogram DIE at %s"),
12909 sect_offset_str (die->sect_off));
12910 return;
12911 }
12912
12913 /* Ignore functions with missing or invalid low and high pc attributes. */
12914 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12915 <= PC_BOUNDS_INVALID)
12916 {
12917 attr = dwarf2_attr (die, DW_AT_external, cu);
12918 if (!attr || !DW_UNSND (attr))
12919 complaint (_("cannot get low and high bounds "
12920 "for subprogram DIE at %s"),
12921 sect_offset_str (die->sect_off));
12922 return;
12923 }
12924
12925 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12926 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12927
12928 /* If we have any template arguments, then we must allocate a
12929 different sort of symbol. */
12930 for (child_die = die->child; child_die; child_die = child_die->sibling)
12931 {
12932 if (child_die->tag == DW_TAG_template_type_param
12933 || child_die->tag == DW_TAG_template_value_param)
12934 {
12935 templ_func = allocate_template_symbol (objfile);
12936 templ_func->subclass = SYMBOL_TEMPLATE;
12937 break;
12938 }
12939 }
12940
12941 newobj = cu->get_builder ()->push_context (0, lowpc);
12942 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12943 (struct symbol *) templ_func);
12944
12945 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12946 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12947 cu->language);
12948
12949 /* If there is a location expression for DW_AT_frame_base, record
12950 it. */
12951 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12952 if (attr != nullptr)
12953 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12954
12955 /* If there is a location for the static link, record it. */
12956 newobj->static_link = NULL;
12957 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12958 if (attr != nullptr)
12959 {
12960 newobj->static_link
12961 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12962 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12963 cu->per_cu->addr_type ());
12964 }
12965
12966 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12967
12968 if (die->child != NULL)
12969 {
12970 child_die = die->child;
12971 while (child_die && child_die->tag)
12972 {
12973 if (child_die->tag == DW_TAG_template_type_param
12974 || child_die->tag == DW_TAG_template_value_param)
12975 {
12976 struct symbol *arg = new_symbol (child_die, NULL, cu);
12977
12978 if (arg != NULL)
12979 template_args.push_back (arg);
12980 }
12981 else
12982 process_die (child_die, cu);
12983 child_die = child_die->sibling;
12984 }
12985 }
12986
12987 inherit_abstract_dies (die, cu);
12988
12989 /* If we have a DW_AT_specification, we might need to import using
12990 directives from the context of the specification DIE. See the
12991 comment in determine_prefix. */
12992 if (cu->language == language_cplus
12993 && dwarf2_attr (die, DW_AT_specification, cu))
12994 {
12995 struct dwarf2_cu *spec_cu = cu;
12996 struct die_info *spec_die = die_specification (die, &spec_cu);
12997
12998 while (spec_die)
12999 {
13000 child_die = spec_die->child;
13001 while (child_die && child_die->tag)
13002 {
13003 if (child_die->tag == DW_TAG_imported_module)
13004 process_die (child_die, spec_cu);
13005 child_die = child_die->sibling;
13006 }
13007
13008 /* In some cases, GCC generates specification DIEs that
13009 themselves contain DW_AT_specification attributes. */
13010 spec_die = die_specification (spec_die, &spec_cu);
13011 }
13012 }
13013
13014 struct context_stack cstk = cu->get_builder ()->pop_context ();
13015 /* Make a block for the local symbols within. */
13016 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13017 cstk.static_link, lowpc, highpc);
13018
13019 /* For C++, set the block's scope. */
13020 if ((cu->language == language_cplus
13021 || cu->language == language_fortran
13022 || cu->language == language_d
13023 || cu->language == language_rust)
13024 && cu->processing_has_namespace_info)
13025 block_set_scope (block, determine_prefix (die, cu),
13026 &objfile->objfile_obstack);
13027
13028 /* If we have address ranges, record them. */
13029 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13030
13031 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13032
13033 /* Attach template arguments to function. */
13034 if (!template_args.empty ())
13035 {
13036 gdb_assert (templ_func != NULL);
13037
13038 templ_func->n_template_arguments = template_args.size ();
13039 templ_func->template_arguments
13040 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13041 templ_func->n_template_arguments);
13042 memcpy (templ_func->template_arguments,
13043 template_args.data (),
13044 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13045
13046 /* Make sure that the symtab is set on the new symbols. Even
13047 though they don't appear in this symtab directly, other parts
13048 of gdb assume that symbols do, and this is reasonably
13049 true. */
13050 for (symbol *sym : template_args)
13051 symbol_set_symtab (sym, symbol_symtab (templ_func));
13052 }
13053
13054 /* In C++, we can have functions nested inside functions (e.g., when
13055 a function declares a class that has methods). This means that
13056 when we finish processing a function scope, we may need to go
13057 back to building a containing block's symbol lists. */
13058 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13059 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13060
13061 /* If we've finished processing a top-level function, subsequent
13062 symbols go in the file symbol list. */
13063 if (cu->get_builder ()->outermost_context_p ())
13064 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13065 }
13066
13067 /* Process all the DIES contained within a lexical block scope. Start
13068 a new scope, process the dies, and then close the scope. */
13069
13070 static void
13071 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13072 {
13073 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13074 struct gdbarch *gdbarch = objfile->arch ();
13075 CORE_ADDR lowpc, highpc;
13076 struct die_info *child_die;
13077 CORE_ADDR baseaddr;
13078
13079 baseaddr = objfile->text_section_offset ();
13080
13081 /* Ignore blocks with missing or invalid low and high pc attributes. */
13082 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13083 as multiple lexical blocks? Handling children in a sane way would
13084 be nasty. Might be easier to properly extend generic blocks to
13085 describe ranges. */
13086 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13087 {
13088 case PC_BOUNDS_NOT_PRESENT:
13089 /* DW_TAG_lexical_block has no attributes, process its children as if
13090 there was no wrapping by that DW_TAG_lexical_block.
13091 GCC does no longer produces such DWARF since GCC r224161. */
13092 for (child_die = die->child;
13093 child_die != NULL && child_die->tag;
13094 child_die = child_die->sibling)
13095 process_die (child_die, cu);
13096 return;
13097 case PC_BOUNDS_INVALID:
13098 return;
13099 }
13100 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13101 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13102
13103 cu->get_builder ()->push_context (0, lowpc);
13104 if (die->child != NULL)
13105 {
13106 child_die = die->child;
13107 while (child_die && child_die->tag)
13108 {
13109 process_die (child_die, cu);
13110 child_die = child_die->sibling;
13111 }
13112 }
13113 inherit_abstract_dies (die, cu);
13114 struct context_stack cstk = cu->get_builder ()->pop_context ();
13115
13116 if (*cu->get_builder ()->get_local_symbols () != NULL
13117 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13118 {
13119 struct block *block
13120 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13121 cstk.start_addr, highpc);
13122
13123 /* Note that recording ranges after traversing children, as we
13124 do here, means that recording a parent's ranges entails
13125 walking across all its children's ranges as they appear in
13126 the address map, which is quadratic behavior.
13127
13128 It would be nicer to record the parent's ranges before
13129 traversing its children, simply overriding whatever you find
13130 there. But since we don't even decide whether to create a
13131 block until after we've traversed its children, that's hard
13132 to do. */
13133 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13134 }
13135 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13136 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13137 }
13138
13139 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13140
13141 static void
13142 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13143 {
13144 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13145 struct gdbarch *gdbarch = objfile->arch ();
13146 CORE_ADDR pc, baseaddr;
13147 struct attribute *attr;
13148 struct call_site *call_site, call_site_local;
13149 void **slot;
13150 int nparams;
13151 struct die_info *child_die;
13152
13153 baseaddr = objfile->text_section_offset ();
13154
13155 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13156 if (attr == NULL)
13157 {
13158 /* This was a pre-DWARF-5 GNU extension alias
13159 for DW_AT_call_return_pc. */
13160 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13161 }
13162 if (!attr)
13163 {
13164 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13165 "DIE %s [in module %s]"),
13166 sect_offset_str (die->sect_off), objfile_name (objfile));
13167 return;
13168 }
13169 pc = attr->value_as_address () + baseaddr;
13170 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13171
13172 if (cu->call_site_htab == NULL)
13173 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13174 NULL, &objfile->objfile_obstack,
13175 hashtab_obstack_allocate, NULL);
13176 call_site_local.pc = pc;
13177 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13178 if (*slot != NULL)
13179 {
13180 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13181 "DIE %s [in module %s]"),
13182 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13183 objfile_name (objfile));
13184 return;
13185 }
13186
13187 /* Count parameters at the caller. */
13188
13189 nparams = 0;
13190 for (child_die = die->child; child_die && child_die->tag;
13191 child_die = child_die->sibling)
13192 {
13193 if (child_die->tag != DW_TAG_call_site_parameter
13194 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13195 {
13196 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13197 "DW_TAG_call_site child DIE %s [in module %s]"),
13198 child_die->tag, sect_offset_str (child_die->sect_off),
13199 objfile_name (objfile));
13200 continue;
13201 }
13202
13203 nparams++;
13204 }
13205
13206 call_site
13207 = ((struct call_site *)
13208 obstack_alloc (&objfile->objfile_obstack,
13209 sizeof (*call_site)
13210 + (sizeof (*call_site->parameter) * (nparams - 1))));
13211 *slot = call_site;
13212 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13213 call_site->pc = pc;
13214
13215 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13216 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13217 {
13218 struct die_info *func_die;
13219
13220 /* Skip also over DW_TAG_inlined_subroutine. */
13221 for (func_die = die->parent;
13222 func_die && func_die->tag != DW_TAG_subprogram
13223 && func_die->tag != DW_TAG_subroutine_type;
13224 func_die = func_die->parent);
13225
13226 /* DW_AT_call_all_calls is a superset
13227 of DW_AT_call_all_tail_calls. */
13228 if (func_die
13229 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13230 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13231 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13232 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13233 {
13234 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13235 not complete. But keep CALL_SITE for look ups via call_site_htab,
13236 both the initial caller containing the real return address PC and
13237 the final callee containing the current PC of a chain of tail
13238 calls do not need to have the tail call list complete. But any
13239 function candidate for a virtual tail call frame searched via
13240 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13241 determined unambiguously. */
13242 }
13243 else
13244 {
13245 struct type *func_type = NULL;
13246
13247 if (func_die)
13248 func_type = get_die_type (func_die, cu);
13249 if (func_type != NULL)
13250 {
13251 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13252
13253 /* Enlist this call site to the function. */
13254 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13255 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13256 }
13257 else
13258 complaint (_("Cannot find function owning DW_TAG_call_site "
13259 "DIE %s [in module %s]"),
13260 sect_offset_str (die->sect_off), objfile_name (objfile));
13261 }
13262 }
13263
13264 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13265 if (attr == NULL)
13266 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13267 if (attr == NULL)
13268 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13269 if (attr == NULL)
13270 {
13271 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13272 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13273 }
13274 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13275 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13276 /* Keep NULL DWARF_BLOCK. */;
13277 else if (attr->form_is_block ())
13278 {
13279 struct dwarf2_locexpr_baton *dlbaton;
13280
13281 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13282 dlbaton->data = DW_BLOCK (attr)->data;
13283 dlbaton->size = DW_BLOCK (attr)->size;
13284 dlbaton->per_cu = cu->per_cu;
13285
13286 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13287 }
13288 else if (attr->form_is_ref ())
13289 {
13290 struct dwarf2_cu *target_cu = cu;
13291 struct die_info *target_die;
13292
13293 target_die = follow_die_ref (die, attr, &target_cu);
13294 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13295 if (die_is_declaration (target_die, target_cu))
13296 {
13297 const char *target_physname;
13298
13299 /* Prefer the mangled name; otherwise compute the demangled one. */
13300 target_physname = dw2_linkage_name (target_die, target_cu);
13301 if (target_physname == NULL)
13302 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13303 if (target_physname == NULL)
13304 complaint (_("DW_AT_call_target target DIE has invalid "
13305 "physname, for referencing DIE %s [in module %s]"),
13306 sect_offset_str (die->sect_off), objfile_name (objfile));
13307 else
13308 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13309 }
13310 else
13311 {
13312 CORE_ADDR lowpc;
13313
13314 /* DW_AT_entry_pc should be preferred. */
13315 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13316 <= PC_BOUNDS_INVALID)
13317 complaint (_("DW_AT_call_target target DIE has invalid "
13318 "low pc, for referencing DIE %s [in module %s]"),
13319 sect_offset_str (die->sect_off), objfile_name (objfile));
13320 else
13321 {
13322 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13323 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13324 }
13325 }
13326 }
13327 else
13328 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13329 "block nor reference, for DIE %s [in module %s]"),
13330 sect_offset_str (die->sect_off), objfile_name (objfile));
13331
13332 call_site->per_cu = cu->per_cu;
13333
13334 for (child_die = die->child;
13335 child_die && child_die->tag;
13336 child_die = child_die->sibling)
13337 {
13338 struct call_site_parameter *parameter;
13339 struct attribute *loc, *origin;
13340
13341 if (child_die->tag != DW_TAG_call_site_parameter
13342 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13343 {
13344 /* Already printed the complaint above. */
13345 continue;
13346 }
13347
13348 gdb_assert (call_site->parameter_count < nparams);
13349 parameter = &call_site->parameter[call_site->parameter_count];
13350
13351 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13352 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13353 register is contained in DW_AT_call_value. */
13354
13355 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13356 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13357 if (origin == NULL)
13358 {
13359 /* This was a pre-DWARF-5 GNU extension alias
13360 for DW_AT_call_parameter. */
13361 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13362 }
13363 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13364 {
13365 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13366
13367 sect_offset sect_off = origin->get_ref_die_offset ();
13368 if (!cu->header.offset_in_cu_p (sect_off))
13369 {
13370 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13371 binding can be done only inside one CU. Such referenced DIE
13372 therefore cannot be even moved to DW_TAG_partial_unit. */
13373 complaint (_("DW_AT_call_parameter offset is not in CU for "
13374 "DW_TAG_call_site child DIE %s [in module %s]"),
13375 sect_offset_str (child_die->sect_off),
13376 objfile_name (objfile));
13377 continue;
13378 }
13379 parameter->u.param_cu_off
13380 = (cu_offset) (sect_off - cu->header.sect_off);
13381 }
13382 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13383 {
13384 complaint (_("No DW_FORM_block* DW_AT_location for "
13385 "DW_TAG_call_site child DIE %s [in module %s]"),
13386 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13387 continue;
13388 }
13389 else
13390 {
13391 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13392 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13393 if (parameter->u.dwarf_reg != -1)
13394 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13395 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13396 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13397 &parameter->u.fb_offset))
13398 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13399 else
13400 {
13401 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13402 "for DW_FORM_block* DW_AT_location is supported for "
13403 "DW_TAG_call_site child DIE %s "
13404 "[in module %s]"),
13405 sect_offset_str (child_die->sect_off),
13406 objfile_name (objfile));
13407 continue;
13408 }
13409 }
13410
13411 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13412 if (attr == NULL)
13413 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13414 if (attr == NULL || !attr->form_is_block ())
13415 {
13416 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13417 "DW_TAG_call_site child DIE %s [in module %s]"),
13418 sect_offset_str (child_die->sect_off),
13419 objfile_name (objfile));
13420 continue;
13421 }
13422 parameter->value = DW_BLOCK (attr)->data;
13423 parameter->value_size = DW_BLOCK (attr)->size;
13424
13425 /* Parameters are not pre-cleared by memset above. */
13426 parameter->data_value = NULL;
13427 parameter->data_value_size = 0;
13428 call_site->parameter_count++;
13429
13430 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13431 if (attr == NULL)
13432 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13433 if (attr != nullptr)
13434 {
13435 if (!attr->form_is_block ())
13436 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13437 "DW_TAG_call_site child DIE %s [in module %s]"),
13438 sect_offset_str (child_die->sect_off),
13439 objfile_name (objfile));
13440 else
13441 {
13442 parameter->data_value = DW_BLOCK (attr)->data;
13443 parameter->data_value_size = DW_BLOCK (attr)->size;
13444 }
13445 }
13446 }
13447 }
13448
13449 /* Helper function for read_variable. If DIE represents a virtual
13450 table, then return the type of the concrete object that is
13451 associated with the virtual table. Otherwise, return NULL. */
13452
13453 static struct type *
13454 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13455 {
13456 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13457 if (attr == NULL)
13458 return NULL;
13459
13460 /* Find the type DIE. */
13461 struct die_info *type_die = NULL;
13462 struct dwarf2_cu *type_cu = cu;
13463
13464 if (attr->form_is_ref ())
13465 type_die = follow_die_ref (die, attr, &type_cu);
13466 if (type_die == NULL)
13467 return NULL;
13468
13469 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13470 return NULL;
13471 return die_containing_type (type_die, type_cu);
13472 }
13473
13474 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13475
13476 static void
13477 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13478 {
13479 struct rust_vtable_symbol *storage = NULL;
13480
13481 if (cu->language == language_rust)
13482 {
13483 struct type *containing_type = rust_containing_type (die, cu);
13484
13485 if (containing_type != NULL)
13486 {
13487 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13488
13489 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13490 initialize_objfile_symbol (storage);
13491 storage->concrete_type = containing_type;
13492 storage->subclass = SYMBOL_RUST_VTABLE;
13493 }
13494 }
13495
13496 struct symbol *res = new_symbol (die, NULL, cu, storage);
13497 struct attribute *abstract_origin
13498 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13499 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13500 if (res == NULL && loc && abstract_origin)
13501 {
13502 /* We have a variable without a name, but with a location and an abstract
13503 origin. This may be a concrete instance of an abstract variable
13504 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13505 later. */
13506 struct dwarf2_cu *origin_cu = cu;
13507 struct die_info *origin_die
13508 = follow_die_ref (die, abstract_origin, &origin_cu);
13509 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13510 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13511 }
13512 }
13513
13514 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13515 reading .debug_rnglists.
13516 Callback's type should be:
13517 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13518 Return true if the attributes are present and valid, otherwise,
13519 return false. */
13520
13521 template <typename Callback>
13522 static bool
13523 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13524 Callback &&callback)
13525 {
13526 struct dwarf2_per_objfile *dwarf2_per_objfile
13527 = cu->per_cu->dwarf2_per_objfile;
13528 struct objfile *objfile = dwarf2_per_objfile->objfile;
13529 bfd *obfd = objfile->obfd;
13530 /* Base address selection entry. */
13531 gdb::optional<CORE_ADDR> base;
13532 const gdb_byte *buffer;
13533 CORE_ADDR baseaddr;
13534 bool overflow = false;
13535
13536 base = cu->base_address;
13537
13538 dwarf2_per_objfile->rnglists.read (objfile);
13539 if (offset >= dwarf2_per_objfile->rnglists.size)
13540 {
13541 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13542 offset);
13543 return false;
13544 }
13545 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13546
13547 baseaddr = objfile->text_section_offset ();
13548
13549 while (1)
13550 {
13551 /* Initialize it due to a false compiler warning. */
13552 CORE_ADDR range_beginning = 0, range_end = 0;
13553 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13554 + dwarf2_per_objfile->rnglists.size);
13555 unsigned int bytes_read;
13556
13557 if (buffer == buf_end)
13558 {
13559 overflow = true;
13560 break;
13561 }
13562 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13563 switch (rlet)
13564 {
13565 case DW_RLE_end_of_list:
13566 break;
13567 case DW_RLE_base_address:
13568 if (buffer + cu->header.addr_size > buf_end)
13569 {
13570 overflow = true;
13571 break;
13572 }
13573 base = cu->header.read_address (obfd, buffer, &bytes_read);
13574 buffer += bytes_read;
13575 break;
13576 case DW_RLE_start_length:
13577 if (buffer + cu->header.addr_size > buf_end)
13578 {
13579 overflow = true;
13580 break;
13581 }
13582 range_beginning = cu->header.read_address (obfd, buffer,
13583 &bytes_read);
13584 buffer += bytes_read;
13585 range_end = (range_beginning
13586 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13587 buffer += bytes_read;
13588 if (buffer > buf_end)
13589 {
13590 overflow = true;
13591 break;
13592 }
13593 break;
13594 case DW_RLE_offset_pair:
13595 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13596 buffer += bytes_read;
13597 if (buffer > buf_end)
13598 {
13599 overflow = true;
13600 break;
13601 }
13602 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13603 buffer += bytes_read;
13604 if (buffer > buf_end)
13605 {
13606 overflow = true;
13607 break;
13608 }
13609 break;
13610 case DW_RLE_start_end:
13611 if (buffer + 2 * cu->header.addr_size > buf_end)
13612 {
13613 overflow = true;
13614 break;
13615 }
13616 range_beginning = cu->header.read_address (obfd, buffer,
13617 &bytes_read);
13618 buffer += bytes_read;
13619 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13620 buffer += bytes_read;
13621 break;
13622 default:
13623 complaint (_("Invalid .debug_rnglists data (no base address)"));
13624 return false;
13625 }
13626 if (rlet == DW_RLE_end_of_list || overflow)
13627 break;
13628 if (rlet == DW_RLE_base_address)
13629 continue;
13630
13631 if (!base.has_value ())
13632 {
13633 /* We have no valid base address for the ranges
13634 data. */
13635 complaint (_("Invalid .debug_rnglists data (no base address)"));
13636 return false;
13637 }
13638
13639 if (range_beginning > range_end)
13640 {
13641 /* Inverted range entries are invalid. */
13642 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13643 return false;
13644 }
13645
13646 /* Empty range entries have no effect. */
13647 if (range_beginning == range_end)
13648 continue;
13649
13650 range_beginning += *base;
13651 range_end += *base;
13652
13653 /* A not-uncommon case of bad debug info.
13654 Don't pollute the addrmap with bad data. */
13655 if (range_beginning + baseaddr == 0
13656 && !dwarf2_per_objfile->has_section_at_zero)
13657 {
13658 complaint (_(".debug_rnglists entry has start address of zero"
13659 " [in module %s]"), objfile_name (objfile));
13660 continue;
13661 }
13662
13663 callback (range_beginning, range_end);
13664 }
13665
13666 if (overflow)
13667 {
13668 complaint (_("Offset %d is not terminated "
13669 "for DW_AT_ranges attribute"),
13670 offset);
13671 return false;
13672 }
13673
13674 return true;
13675 }
13676
13677 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13678 Callback's type should be:
13679 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13680 Return 1 if the attributes are present and valid, otherwise, return 0. */
13681
13682 template <typename Callback>
13683 static int
13684 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13685 Callback &&callback)
13686 {
13687 struct dwarf2_per_objfile *dwarf2_per_objfile
13688 = cu->per_cu->dwarf2_per_objfile;
13689 struct objfile *objfile = dwarf2_per_objfile->objfile;
13690 struct comp_unit_head *cu_header = &cu->header;
13691 bfd *obfd = objfile->obfd;
13692 unsigned int addr_size = cu_header->addr_size;
13693 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13694 /* Base address selection entry. */
13695 gdb::optional<CORE_ADDR> base;
13696 unsigned int dummy;
13697 const gdb_byte *buffer;
13698 CORE_ADDR baseaddr;
13699
13700 if (cu_header->version >= 5)
13701 return dwarf2_rnglists_process (offset, cu, callback);
13702
13703 base = cu->base_address;
13704
13705 dwarf2_per_objfile->ranges.read (objfile);
13706 if (offset >= dwarf2_per_objfile->ranges.size)
13707 {
13708 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13709 offset);
13710 return 0;
13711 }
13712 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13713
13714 baseaddr = objfile->text_section_offset ();
13715
13716 while (1)
13717 {
13718 CORE_ADDR range_beginning, range_end;
13719
13720 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13721 buffer += addr_size;
13722 range_end = cu->header.read_address (obfd, buffer, &dummy);
13723 buffer += addr_size;
13724 offset += 2 * addr_size;
13725
13726 /* An end of list marker is a pair of zero addresses. */
13727 if (range_beginning == 0 && range_end == 0)
13728 /* Found the end of list entry. */
13729 break;
13730
13731 /* Each base address selection entry is a pair of 2 values.
13732 The first is the largest possible address, the second is
13733 the base address. Check for a base address here. */
13734 if ((range_beginning & mask) == mask)
13735 {
13736 /* If we found the largest possible address, then we already
13737 have the base address in range_end. */
13738 base = range_end;
13739 continue;
13740 }
13741
13742 if (!base.has_value ())
13743 {
13744 /* We have no valid base address for the ranges
13745 data. */
13746 complaint (_("Invalid .debug_ranges data (no base address)"));
13747 return 0;
13748 }
13749
13750 if (range_beginning > range_end)
13751 {
13752 /* Inverted range entries are invalid. */
13753 complaint (_("Invalid .debug_ranges data (inverted range)"));
13754 return 0;
13755 }
13756
13757 /* Empty range entries have no effect. */
13758 if (range_beginning == range_end)
13759 continue;
13760
13761 range_beginning += *base;
13762 range_end += *base;
13763
13764 /* A not-uncommon case of bad debug info.
13765 Don't pollute the addrmap with bad data. */
13766 if (range_beginning + baseaddr == 0
13767 && !dwarf2_per_objfile->has_section_at_zero)
13768 {
13769 complaint (_(".debug_ranges entry has start address of zero"
13770 " [in module %s]"), objfile_name (objfile));
13771 continue;
13772 }
13773
13774 callback (range_beginning, range_end);
13775 }
13776
13777 return 1;
13778 }
13779
13780 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13781 Return 1 if the attributes are present and valid, otherwise, return 0.
13782 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13783
13784 static int
13785 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13786 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13787 dwarf2_psymtab *ranges_pst)
13788 {
13789 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13790 struct gdbarch *gdbarch = objfile->arch ();
13791 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13792 int low_set = 0;
13793 CORE_ADDR low = 0;
13794 CORE_ADDR high = 0;
13795 int retval;
13796
13797 retval = dwarf2_ranges_process (offset, cu,
13798 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13799 {
13800 if (ranges_pst != NULL)
13801 {
13802 CORE_ADDR lowpc;
13803 CORE_ADDR highpc;
13804
13805 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13806 range_beginning + baseaddr)
13807 - baseaddr);
13808 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13809 range_end + baseaddr)
13810 - baseaddr);
13811 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13812 lowpc, highpc - 1, ranges_pst);
13813 }
13814
13815 /* FIXME: This is recording everything as a low-high
13816 segment of consecutive addresses. We should have a
13817 data structure for discontiguous block ranges
13818 instead. */
13819 if (! low_set)
13820 {
13821 low = range_beginning;
13822 high = range_end;
13823 low_set = 1;
13824 }
13825 else
13826 {
13827 if (range_beginning < low)
13828 low = range_beginning;
13829 if (range_end > high)
13830 high = range_end;
13831 }
13832 });
13833 if (!retval)
13834 return 0;
13835
13836 if (! low_set)
13837 /* If the first entry is an end-of-list marker, the range
13838 describes an empty scope, i.e. no instructions. */
13839 return 0;
13840
13841 if (low_return)
13842 *low_return = low;
13843 if (high_return)
13844 *high_return = high;
13845 return 1;
13846 }
13847
13848 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13849 definition for the return value. *LOWPC and *HIGHPC are set iff
13850 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13851
13852 static enum pc_bounds_kind
13853 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13854 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13855 dwarf2_psymtab *pst)
13856 {
13857 struct dwarf2_per_objfile *dwarf2_per_objfile
13858 = cu->per_cu->dwarf2_per_objfile;
13859 struct attribute *attr;
13860 struct attribute *attr_high;
13861 CORE_ADDR low = 0;
13862 CORE_ADDR high = 0;
13863 enum pc_bounds_kind ret;
13864
13865 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13866 if (attr_high)
13867 {
13868 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13869 if (attr != nullptr)
13870 {
13871 low = attr->value_as_address ();
13872 high = attr_high->value_as_address ();
13873 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13874 high += low;
13875 }
13876 else
13877 /* Found high w/o low attribute. */
13878 return PC_BOUNDS_INVALID;
13879
13880 /* Found consecutive range of addresses. */
13881 ret = PC_BOUNDS_HIGH_LOW;
13882 }
13883 else
13884 {
13885 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13886 if (attr != NULL)
13887 {
13888 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13889 We take advantage of the fact that DW_AT_ranges does not appear
13890 in DW_TAG_compile_unit of DWO files. */
13891 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13892 unsigned int ranges_offset = (DW_UNSND (attr)
13893 + (need_ranges_base
13894 ? cu->ranges_base
13895 : 0));
13896
13897 /* Value of the DW_AT_ranges attribute is the offset in the
13898 .debug_ranges section. */
13899 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13900 return PC_BOUNDS_INVALID;
13901 /* Found discontinuous range of addresses. */
13902 ret = PC_BOUNDS_RANGES;
13903 }
13904 else
13905 return PC_BOUNDS_NOT_PRESENT;
13906 }
13907
13908 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13909 if (high <= low)
13910 return PC_BOUNDS_INVALID;
13911
13912 /* When using the GNU linker, .gnu.linkonce. sections are used to
13913 eliminate duplicate copies of functions and vtables and such.
13914 The linker will arbitrarily choose one and discard the others.
13915 The AT_*_pc values for such functions refer to local labels in
13916 these sections. If the section from that file was discarded, the
13917 labels are not in the output, so the relocs get a value of 0.
13918 If this is a discarded function, mark the pc bounds as invalid,
13919 so that GDB will ignore it. */
13920 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13921 return PC_BOUNDS_INVALID;
13922
13923 *lowpc = low;
13924 if (highpc)
13925 *highpc = high;
13926 return ret;
13927 }
13928
13929 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13930 its low and high PC addresses. Do nothing if these addresses could not
13931 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13932 and HIGHPC to the high address if greater than HIGHPC. */
13933
13934 static void
13935 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13936 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13937 struct dwarf2_cu *cu)
13938 {
13939 CORE_ADDR low, high;
13940 struct die_info *child = die->child;
13941
13942 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13943 {
13944 *lowpc = std::min (*lowpc, low);
13945 *highpc = std::max (*highpc, high);
13946 }
13947
13948 /* If the language does not allow nested subprograms (either inside
13949 subprograms or lexical blocks), we're done. */
13950 if (cu->language != language_ada)
13951 return;
13952
13953 /* Check all the children of the given DIE. If it contains nested
13954 subprograms, then check their pc bounds. Likewise, we need to
13955 check lexical blocks as well, as they may also contain subprogram
13956 definitions. */
13957 while (child && child->tag)
13958 {
13959 if (child->tag == DW_TAG_subprogram
13960 || child->tag == DW_TAG_lexical_block)
13961 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13962 child = child->sibling;
13963 }
13964 }
13965
13966 /* Get the low and high pc's represented by the scope DIE, and store
13967 them in *LOWPC and *HIGHPC. If the correct values can't be
13968 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13969
13970 static void
13971 get_scope_pc_bounds (struct die_info *die,
13972 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13973 struct dwarf2_cu *cu)
13974 {
13975 CORE_ADDR best_low = (CORE_ADDR) -1;
13976 CORE_ADDR best_high = (CORE_ADDR) 0;
13977 CORE_ADDR current_low, current_high;
13978
13979 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13980 >= PC_BOUNDS_RANGES)
13981 {
13982 best_low = current_low;
13983 best_high = current_high;
13984 }
13985 else
13986 {
13987 struct die_info *child = die->child;
13988
13989 while (child && child->tag)
13990 {
13991 switch (child->tag) {
13992 case DW_TAG_subprogram:
13993 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13994 break;
13995 case DW_TAG_namespace:
13996 case DW_TAG_module:
13997 /* FIXME: carlton/2004-01-16: Should we do this for
13998 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13999 that current GCC's always emit the DIEs corresponding
14000 to definitions of methods of classes as children of a
14001 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14002 the DIEs giving the declarations, which could be
14003 anywhere). But I don't see any reason why the
14004 standards says that they have to be there. */
14005 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14006
14007 if (current_low != ((CORE_ADDR) -1))
14008 {
14009 best_low = std::min (best_low, current_low);
14010 best_high = std::max (best_high, current_high);
14011 }
14012 break;
14013 default:
14014 /* Ignore. */
14015 break;
14016 }
14017
14018 child = child->sibling;
14019 }
14020 }
14021
14022 *lowpc = best_low;
14023 *highpc = best_high;
14024 }
14025
14026 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14027 in DIE. */
14028
14029 static void
14030 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14031 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14032 {
14033 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14034 struct gdbarch *gdbarch = objfile->arch ();
14035 struct attribute *attr;
14036 struct attribute *attr_high;
14037
14038 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14039 if (attr_high)
14040 {
14041 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14042 if (attr != nullptr)
14043 {
14044 CORE_ADDR low = attr->value_as_address ();
14045 CORE_ADDR high = attr_high->value_as_address ();
14046
14047 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14048 high += low;
14049
14050 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14051 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14052 cu->get_builder ()->record_block_range (block, low, high - 1);
14053 }
14054 }
14055
14056 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14057 if (attr != nullptr)
14058 {
14059 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14060 We take advantage of the fact that DW_AT_ranges does not appear
14061 in DW_TAG_compile_unit of DWO files. */
14062 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14063
14064 /* The value of the DW_AT_ranges attribute is the offset of the
14065 address range list in the .debug_ranges section. */
14066 unsigned long offset = (DW_UNSND (attr)
14067 + (need_ranges_base ? cu->ranges_base : 0));
14068
14069 std::vector<blockrange> blockvec;
14070 dwarf2_ranges_process (offset, cu,
14071 [&] (CORE_ADDR start, CORE_ADDR end)
14072 {
14073 start += baseaddr;
14074 end += baseaddr;
14075 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14076 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14077 cu->get_builder ()->record_block_range (block, start, end - 1);
14078 blockvec.emplace_back (start, end);
14079 });
14080
14081 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14082 }
14083 }
14084
14085 /* Check whether the producer field indicates either of GCC < 4.6, or the
14086 Intel C/C++ compiler, and cache the result in CU. */
14087
14088 static void
14089 check_producer (struct dwarf2_cu *cu)
14090 {
14091 int major, minor;
14092
14093 if (cu->producer == NULL)
14094 {
14095 /* For unknown compilers expect their behavior is DWARF version
14096 compliant.
14097
14098 GCC started to support .debug_types sections by -gdwarf-4 since
14099 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14100 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14101 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14102 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14103 }
14104 else if (producer_is_gcc (cu->producer, &major, &minor))
14105 {
14106 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14107 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14108 }
14109 else if (producer_is_icc (cu->producer, &major, &minor))
14110 {
14111 cu->producer_is_icc = true;
14112 cu->producer_is_icc_lt_14 = major < 14;
14113 }
14114 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14115 cu->producer_is_codewarrior = true;
14116 else
14117 {
14118 /* For other non-GCC compilers, expect their behavior is DWARF version
14119 compliant. */
14120 }
14121
14122 cu->checked_producer = true;
14123 }
14124
14125 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14126 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14127 during 4.6.0 experimental. */
14128
14129 static bool
14130 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14131 {
14132 if (!cu->checked_producer)
14133 check_producer (cu);
14134
14135 return cu->producer_is_gxx_lt_4_6;
14136 }
14137
14138
14139 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14140 with incorrect is_stmt attributes. */
14141
14142 static bool
14143 producer_is_codewarrior (struct dwarf2_cu *cu)
14144 {
14145 if (!cu->checked_producer)
14146 check_producer (cu);
14147
14148 return cu->producer_is_codewarrior;
14149 }
14150
14151 /* Return the default accessibility type if it is not overridden by
14152 DW_AT_accessibility. */
14153
14154 static enum dwarf_access_attribute
14155 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14156 {
14157 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14158 {
14159 /* The default DWARF 2 accessibility for members is public, the default
14160 accessibility for inheritance is private. */
14161
14162 if (die->tag != DW_TAG_inheritance)
14163 return DW_ACCESS_public;
14164 else
14165 return DW_ACCESS_private;
14166 }
14167 else
14168 {
14169 /* DWARF 3+ defines the default accessibility a different way. The same
14170 rules apply now for DW_TAG_inheritance as for the members and it only
14171 depends on the container kind. */
14172
14173 if (die->parent->tag == DW_TAG_class_type)
14174 return DW_ACCESS_private;
14175 else
14176 return DW_ACCESS_public;
14177 }
14178 }
14179
14180 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14181 offset. If the attribute was not found return 0, otherwise return
14182 1. If it was found but could not properly be handled, set *OFFSET
14183 to 0. */
14184
14185 static int
14186 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14187 LONGEST *offset)
14188 {
14189 struct attribute *attr;
14190
14191 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14192 if (attr != NULL)
14193 {
14194 *offset = 0;
14195
14196 /* Note that we do not check for a section offset first here.
14197 This is because DW_AT_data_member_location is new in DWARF 4,
14198 so if we see it, we can assume that a constant form is really
14199 a constant and not a section offset. */
14200 if (attr->form_is_constant ())
14201 *offset = attr->constant_value (0);
14202 else if (attr->form_is_section_offset ())
14203 dwarf2_complex_location_expr_complaint ();
14204 else if (attr->form_is_block ())
14205 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14206 else
14207 dwarf2_complex_location_expr_complaint ();
14208
14209 return 1;
14210 }
14211
14212 return 0;
14213 }
14214
14215 /* Add an aggregate field to the field list. */
14216
14217 static void
14218 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14219 struct dwarf2_cu *cu)
14220 {
14221 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14222 struct gdbarch *gdbarch = objfile->arch ();
14223 struct nextfield *new_field;
14224 struct attribute *attr;
14225 struct field *fp;
14226 const char *fieldname = "";
14227
14228 if (die->tag == DW_TAG_inheritance)
14229 {
14230 fip->baseclasses.emplace_back ();
14231 new_field = &fip->baseclasses.back ();
14232 }
14233 else
14234 {
14235 fip->fields.emplace_back ();
14236 new_field = &fip->fields.back ();
14237 }
14238
14239 new_field->offset = die->sect_off;
14240
14241 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14242 if (attr != nullptr)
14243 new_field->accessibility = DW_UNSND (attr);
14244 else
14245 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14246 if (new_field->accessibility != DW_ACCESS_public)
14247 fip->non_public_fields = 1;
14248
14249 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14250 if (attr != nullptr)
14251 new_field->virtuality = DW_UNSND (attr);
14252 else
14253 new_field->virtuality = DW_VIRTUALITY_none;
14254
14255 fp = &new_field->field;
14256
14257 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14258 {
14259 LONGEST offset;
14260
14261 /* Data member other than a C++ static data member. */
14262
14263 /* Get type of field. */
14264 fp->type = die_type (die, cu);
14265
14266 SET_FIELD_BITPOS (*fp, 0);
14267
14268 /* Get bit size of field (zero if none). */
14269 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14270 if (attr != nullptr)
14271 {
14272 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14273 }
14274 else
14275 {
14276 FIELD_BITSIZE (*fp) = 0;
14277 }
14278
14279 /* Get bit offset of field. */
14280 if (handle_data_member_location (die, cu, &offset))
14281 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14282 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14283 if (attr != nullptr)
14284 {
14285 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14286 {
14287 /* For big endian bits, the DW_AT_bit_offset gives the
14288 additional bit offset from the MSB of the containing
14289 anonymous object to the MSB of the field. We don't
14290 have to do anything special since we don't need to
14291 know the size of the anonymous object. */
14292 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14293 }
14294 else
14295 {
14296 /* For little endian bits, compute the bit offset to the
14297 MSB of the anonymous object, subtract off the number of
14298 bits from the MSB of the field to the MSB of the
14299 object, and then subtract off the number of bits of
14300 the field itself. The result is the bit offset of
14301 the LSB of the field. */
14302 int anonymous_size;
14303 int bit_offset = DW_UNSND (attr);
14304
14305 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14306 if (attr != nullptr)
14307 {
14308 /* The size of the anonymous object containing
14309 the bit field is explicit, so use the
14310 indicated size (in bytes). */
14311 anonymous_size = DW_UNSND (attr);
14312 }
14313 else
14314 {
14315 /* The size of the anonymous object containing
14316 the bit field must be inferred from the type
14317 attribute of the data member containing the
14318 bit field. */
14319 anonymous_size = TYPE_LENGTH (fp->type);
14320 }
14321 SET_FIELD_BITPOS (*fp,
14322 (FIELD_BITPOS (*fp)
14323 + anonymous_size * bits_per_byte
14324 - bit_offset - FIELD_BITSIZE (*fp)));
14325 }
14326 }
14327 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14328 if (attr != NULL)
14329 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14330 + attr->constant_value (0)));
14331
14332 /* Get name of field. */
14333 fieldname = dwarf2_name (die, cu);
14334 if (fieldname == NULL)
14335 fieldname = "";
14336
14337 /* The name is already allocated along with this objfile, so we don't
14338 need to duplicate it for the type. */
14339 fp->name = fieldname;
14340
14341 /* Change accessibility for artificial fields (e.g. virtual table
14342 pointer or virtual base class pointer) to private. */
14343 if (dwarf2_attr (die, DW_AT_artificial, cu))
14344 {
14345 FIELD_ARTIFICIAL (*fp) = 1;
14346 new_field->accessibility = DW_ACCESS_private;
14347 fip->non_public_fields = 1;
14348 }
14349 }
14350 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14351 {
14352 /* C++ static member. */
14353
14354 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14355 is a declaration, but all versions of G++ as of this writing
14356 (so through at least 3.2.1) incorrectly generate
14357 DW_TAG_variable tags. */
14358
14359 const char *physname;
14360
14361 /* Get name of field. */
14362 fieldname = dwarf2_name (die, cu);
14363 if (fieldname == NULL)
14364 return;
14365
14366 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14367 if (attr
14368 /* Only create a symbol if this is an external value.
14369 new_symbol checks this and puts the value in the global symbol
14370 table, which we want. If it is not external, new_symbol
14371 will try to put the value in cu->list_in_scope which is wrong. */
14372 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14373 {
14374 /* A static const member, not much different than an enum as far as
14375 we're concerned, except that we can support more types. */
14376 new_symbol (die, NULL, cu);
14377 }
14378
14379 /* Get physical name. */
14380 physname = dwarf2_physname (fieldname, die, cu);
14381
14382 /* The name is already allocated along with this objfile, so we don't
14383 need to duplicate it for the type. */
14384 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14385 FIELD_TYPE (*fp) = die_type (die, cu);
14386 FIELD_NAME (*fp) = fieldname;
14387 }
14388 else if (die->tag == DW_TAG_inheritance)
14389 {
14390 LONGEST offset;
14391
14392 /* C++ base class field. */
14393 if (handle_data_member_location (die, cu, &offset))
14394 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14395 FIELD_BITSIZE (*fp) = 0;
14396 FIELD_TYPE (*fp) = die_type (die, cu);
14397 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14398 }
14399 else
14400 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14401 }
14402
14403 /* Can the type given by DIE define another type? */
14404
14405 static bool
14406 type_can_define_types (const struct die_info *die)
14407 {
14408 switch (die->tag)
14409 {
14410 case DW_TAG_typedef:
14411 case DW_TAG_class_type:
14412 case DW_TAG_structure_type:
14413 case DW_TAG_union_type:
14414 case DW_TAG_enumeration_type:
14415 return true;
14416
14417 default:
14418 return false;
14419 }
14420 }
14421
14422 /* Add a type definition defined in the scope of the FIP's class. */
14423
14424 static void
14425 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14426 struct dwarf2_cu *cu)
14427 {
14428 struct decl_field fp;
14429 memset (&fp, 0, sizeof (fp));
14430
14431 gdb_assert (type_can_define_types (die));
14432
14433 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14434 fp.name = dwarf2_name (die, cu);
14435 fp.type = read_type_die (die, cu);
14436
14437 /* Save accessibility. */
14438 enum dwarf_access_attribute accessibility;
14439 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14440 if (attr != NULL)
14441 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14442 else
14443 accessibility = dwarf2_default_access_attribute (die, cu);
14444 switch (accessibility)
14445 {
14446 case DW_ACCESS_public:
14447 /* The assumed value if neither private nor protected. */
14448 break;
14449 case DW_ACCESS_private:
14450 fp.is_private = 1;
14451 break;
14452 case DW_ACCESS_protected:
14453 fp.is_protected = 1;
14454 break;
14455 default:
14456 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14457 }
14458
14459 if (die->tag == DW_TAG_typedef)
14460 fip->typedef_field_list.push_back (fp);
14461 else
14462 fip->nested_types_list.push_back (fp);
14463 }
14464
14465 /* A convenience typedef that's used when finding the discriminant
14466 field for a variant part. */
14467 typedef std::unordered_map<sect_offset, int> offset_map_type;
14468
14469 /* Compute the discriminant range for a given variant. OBSTACK is
14470 where the results will be stored. VARIANT is the variant to
14471 process. IS_UNSIGNED indicates whether the discriminant is signed
14472 or unsigned. */
14473
14474 static const gdb::array_view<discriminant_range>
14475 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14476 bool is_unsigned)
14477 {
14478 std::vector<discriminant_range> ranges;
14479
14480 if (variant.default_branch)
14481 return {};
14482
14483 if (variant.discr_list_data == nullptr)
14484 {
14485 discriminant_range r
14486 = {variant.discriminant_value, variant.discriminant_value};
14487 ranges.push_back (r);
14488 }
14489 else
14490 {
14491 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14492 variant.discr_list_data->size);
14493 while (!data.empty ())
14494 {
14495 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14496 {
14497 complaint (_("invalid discriminant marker: %d"), data[0]);
14498 break;
14499 }
14500 bool is_range = data[0] == DW_DSC_range;
14501 data = data.slice (1);
14502
14503 ULONGEST low, high;
14504 unsigned int bytes_read;
14505
14506 if (data.empty ())
14507 {
14508 complaint (_("DW_AT_discr_list missing low value"));
14509 break;
14510 }
14511 if (is_unsigned)
14512 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14513 else
14514 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14515 &bytes_read);
14516 data = data.slice (bytes_read);
14517
14518 if (is_range)
14519 {
14520 if (data.empty ())
14521 {
14522 complaint (_("DW_AT_discr_list missing high value"));
14523 break;
14524 }
14525 if (is_unsigned)
14526 high = read_unsigned_leb128 (nullptr, data.data (),
14527 &bytes_read);
14528 else
14529 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14530 &bytes_read);
14531 data = data.slice (bytes_read);
14532 }
14533 else
14534 high = low;
14535
14536 ranges.push_back ({ low, high });
14537 }
14538 }
14539
14540 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14541 ranges.size ());
14542 std::copy (ranges.begin (), ranges.end (), result);
14543 return gdb::array_view<discriminant_range> (result, ranges.size ());
14544 }
14545
14546 static const gdb::array_view<variant_part> create_variant_parts
14547 (struct obstack *obstack,
14548 const offset_map_type &offset_map,
14549 struct field_info *fi,
14550 const std::vector<variant_part_builder> &variant_parts);
14551
14552 /* Fill in a "struct variant" for a given variant field. RESULT is
14553 the variant to fill in. OBSTACK is where any needed allocations
14554 will be done. OFFSET_MAP holds the mapping from section offsets to
14555 fields for the type. FI describes the fields of the type we're
14556 processing. FIELD is the variant field we're converting. */
14557
14558 static void
14559 create_one_variant (variant &result, struct obstack *obstack,
14560 const offset_map_type &offset_map,
14561 struct field_info *fi, const variant_field &field)
14562 {
14563 result.discriminants = convert_variant_range (obstack, field, false);
14564 result.first_field = field.first_field + fi->baseclasses.size ();
14565 result.last_field = field.last_field + fi->baseclasses.size ();
14566 result.parts = create_variant_parts (obstack, offset_map, fi,
14567 field.variant_parts);
14568 }
14569
14570 /* Fill in a "struct variant_part" for a given variant part. RESULT
14571 is the variant part to fill in. OBSTACK is where any needed
14572 allocations will be done. OFFSET_MAP holds the mapping from
14573 section offsets to fields for the type. FI describes the fields of
14574 the type we're processing. BUILDER is the variant part to be
14575 converted. */
14576
14577 static void
14578 create_one_variant_part (variant_part &result,
14579 struct obstack *obstack,
14580 const offset_map_type &offset_map,
14581 struct field_info *fi,
14582 const variant_part_builder &builder)
14583 {
14584 auto iter = offset_map.find (builder.discriminant_offset);
14585 if (iter == offset_map.end ())
14586 {
14587 result.discriminant_index = -1;
14588 /* Doesn't matter. */
14589 result.is_unsigned = false;
14590 }
14591 else
14592 {
14593 result.discriminant_index = iter->second;
14594 result.is_unsigned
14595 = TYPE_UNSIGNED (FIELD_TYPE
14596 (fi->fields[result.discriminant_index].field));
14597 }
14598
14599 size_t n = builder.variants.size ();
14600 variant *output = new (obstack) variant[n];
14601 for (size_t i = 0; i < n; ++i)
14602 create_one_variant (output[i], obstack, offset_map, fi,
14603 builder.variants[i]);
14604
14605 result.variants = gdb::array_view<variant> (output, n);
14606 }
14607
14608 /* Create a vector of variant parts that can be attached to a type.
14609 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14610 holds the mapping from section offsets to fields for the type. FI
14611 describes the fields of the type we're processing. VARIANT_PARTS
14612 is the vector to convert. */
14613
14614 static const gdb::array_view<variant_part>
14615 create_variant_parts (struct obstack *obstack,
14616 const offset_map_type &offset_map,
14617 struct field_info *fi,
14618 const std::vector<variant_part_builder> &variant_parts)
14619 {
14620 if (variant_parts.empty ())
14621 return {};
14622
14623 size_t n = variant_parts.size ();
14624 variant_part *result = new (obstack) variant_part[n];
14625 for (size_t i = 0; i < n; ++i)
14626 create_one_variant_part (result[i], obstack, offset_map, fi,
14627 variant_parts[i]);
14628
14629 return gdb::array_view<variant_part> (result, n);
14630 }
14631
14632 /* Compute the variant part vector for FIP, attaching it to TYPE when
14633 done. */
14634
14635 static void
14636 add_variant_property (struct field_info *fip, struct type *type,
14637 struct dwarf2_cu *cu)
14638 {
14639 /* Map section offsets of fields to their field index. Note the
14640 field index here does not take the number of baseclasses into
14641 account. */
14642 offset_map_type offset_map;
14643 for (int i = 0; i < fip->fields.size (); ++i)
14644 offset_map[fip->fields[i].offset] = i;
14645
14646 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14647 gdb::array_view<variant_part> parts
14648 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14649 fip->variant_parts);
14650
14651 struct dynamic_prop prop;
14652 prop.kind = PROP_VARIANT_PARTS;
14653 prop.data.variant_parts
14654 = ((gdb::array_view<variant_part> *)
14655 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14656
14657 add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop, type);
14658 }
14659
14660 /* Create the vector of fields, and attach it to the type. */
14661
14662 static void
14663 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14664 struct dwarf2_cu *cu)
14665 {
14666 int nfields = fip->nfields ();
14667
14668 /* Record the field count, allocate space for the array of fields,
14669 and create blank accessibility bitfields if necessary. */
14670 TYPE_NFIELDS (type) = nfields;
14671 TYPE_FIELDS (type) = (struct field *)
14672 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14673
14674 if (fip->non_public_fields && cu->language != language_ada)
14675 {
14676 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14677
14678 TYPE_FIELD_PRIVATE_BITS (type) =
14679 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14680 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14681
14682 TYPE_FIELD_PROTECTED_BITS (type) =
14683 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14684 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14685
14686 TYPE_FIELD_IGNORE_BITS (type) =
14687 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14688 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14689 }
14690
14691 /* If the type has baseclasses, allocate and clear a bit vector for
14692 TYPE_FIELD_VIRTUAL_BITS. */
14693 if (!fip->baseclasses.empty () && cu->language != language_ada)
14694 {
14695 int num_bytes = B_BYTES (fip->baseclasses.size ());
14696 unsigned char *pointer;
14697
14698 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14699 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14700 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14701 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14702 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14703 }
14704
14705 if (!fip->variant_parts.empty ())
14706 add_variant_property (fip, type, cu);
14707
14708 /* Copy the saved-up fields into the field vector. */
14709 for (int i = 0; i < nfields; ++i)
14710 {
14711 struct nextfield &field
14712 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14713 : fip->fields[i - fip->baseclasses.size ()]);
14714
14715 TYPE_FIELD (type, i) = field.field;
14716 switch (field.accessibility)
14717 {
14718 case DW_ACCESS_private:
14719 if (cu->language != language_ada)
14720 SET_TYPE_FIELD_PRIVATE (type, i);
14721 break;
14722
14723 case DW_ACCESS_protected:
14724 if (cu->language != language_ada)
14725 SET_TYPE_FIELD_PROTECTED (type, i);
14726 break;
14727
14728 case DW_ACCESS_public:
14729 break;
14730
14731 default:
14732 /* Unknown accessibility. Complain and treat it as public. */
14733 {
14734 complaint (_("unsupported accessibility %d"),
14735 field.accessibility);
14736 }
14737 break;
14738 }
14739 if (i < fip->baseclasses.size ())
14740 {
14741 switch (field.virtuality)
14742 {
14743 case DW_VIRTUALITY_virtual:
14744 case DW_VIRTUALITY_pure_virtual:
14745 if (cu->language == language_ada)
14746 error (_("unexpected virtuality in component of Ada type"));
14747 SET_TYPE_FIELD_VIRTUAL (type, i);
14748 break;
14749 }
14750 }
14751 }
14752 }
14753
14754 /* Return true if this member function is a constructor, false
14755 otherwise. */
14756
14757 static int
14758 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14759 {
14760 const char *fieldname;
14761 const char *type_name;
14762 int len;
14763
14764 if (die->parent == NULL)
14765 return 0;
14766
14767 if (die->parent->tag != DW_TAG_structure_type
14768 && die->parent->tag != DW_TAG_union_type
14769 && die->parent->tag != DW_TAG_class_type)
14770 return 0;
14771
14772 fieldname = dwarf2_name (die, cu);
14773 type_name = dwarf2_name (die->parent, cu);
14774 if (fieldname == NULL || type_name == NULL)
14775 return 0;
14776
14777 len = strlen (fieldname);
14778 return (strncmp (fieldname, type_name, len) == 0
14779 && (type_name[len] == '\0' || type_name[len] == '<'));
14780 }
14781
14782 /* Check if the given VALUE is a recognized enum
14783 dwarf_defaulted_attribute constant according to DWARF5 spec,
14784 Table 7.24. */
14785
14786 static bool
14787 is_valid_DW_AT_defaulted (ULONGEST value)
14788 {
14789 switch (value)
14790 {
14791 case DW_DEFAULTED_no:
14792 case DW_DEFAULTED_in_class:
14793 case DW_DEFAULTED_out_of_class:
14794 return true;
14795 }
14796
14797 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14798 return false;
14799 }
14800
14801 /* Add a member function to the proper fieldlist. */
14802
14803 static void
14804 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14805 struct type *type, struct dwarf2_cu *cu)
14806 {
14807 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14808 struct attribute *attr;
14809 int i;
14810 struct fnfieldlist *flp = nullptr;
14811 struct fn_field *fnp;
14812 const char *fieldname;
14813 struct type *this_type;
14814 enum dwarf_access_attribute accessibility;
14815
14816 if (cu->language == language_ada)
14817 error (_("unexpected member function in Ada type"));
14818
14819 /* Get name of member function. */
14820 fieldname = dwarf2_name (die, cu);
14821 if (fieldname == NULL)
14822 return;
14823
14824 /* Look up member function name in fieldlist. */
14825 for (i = 0; i < fip->fnfieldlists.size (); i++)
14826 {
14827 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14828 {
14829 flp = &fip->fnfieldlists[i];
14830 break;
14831 }
14832 }
14833
14834 /* Create a new fnfieldlist if necessary. */
14835 if (flp == nullptr)
14836 {
14837 fip->fnfieldlists.emplace_back ();
14838 flp = &fip->fnfieldlists.back ();
14839 flp->name = fieldname;
14840 i = fip->fnfieldlists.size () - 1;
14841 }
14842
14843 /* Create a new member function field and add it to the vector of
14844 fnfieldlists. */
14845 flp->fnfields.emplace_back ();
14846 fnp = &flp->fnfields.back ();
14847
14848 /* Delay processing of the physname until later. */
14849 if (cu->language == language_cplus)
14850 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14851 die, cu);
14852 else
14853 {
14854 const char *physname = dwarf2_physname (fieldname, die, cu);
14855 fnp->physname = physname ? physname : "";
14856 }
14857
14858 fnp->type = alloc_type (objfile);
14859 this_type = read_type_die (die, cu);
14860 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14861 {
14862 int nparams = TYPE_NFIELDS (this_type);
14863
14864 /* TYPE is the domain of this method, and THIS_TYPE is the type
14865 of the method itself (TYPE_CODE_METHOD). */
14866 smash_to_method_type (fnp->type, type,
14867 TYPE_TARGET_TYPE (this_type),
14868 TYPE_FIELDS (this_type),
14869 TYPE_NFIELDS (this_type),
14870 TYPE_VARARGS (this_type));
14871
14872 /* Handle static member functions.
14873 Dwarf2 has no clean way to discern C++ static and non-static
14874 member functions. G++ helps GDB by marking the first
14875 parameter for non-static member functions (which is the this
14876 pointer) as artificial. We obtain this information from
14877 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14878 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14879 fnp->voffset = VOFFSET_STATIC;
14880 }
14881 else
14882 complaint (_("member function type missing for '%s'"),
14883 dwarf2_full_name (fieldname, die, cu));
14884
14885 /* Get fcontext from DW_AT_containing_type if present. */
14886 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14887 fnp->fcontext = die_containing_type (die, cu);
14888
14889 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14890 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14891
14892 /* Get accessibility. */
14893 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14894 if (attr != nullptr)
14895 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14896 else
14897 accessibility = dwarf2_default_access_attribute (die, cu);
14898 switch (accessibility)
14899 {
14900 case DW_ACCESS_private:
14901 fnp->is_private = 1;
14902 break;
14903 case DW_ACCESS_protected:
14904 fnp->is_protected = 1;
14905 break;
14906 }
14907
14908 /* Check for artificial methods. */
14909 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14910 if (attr && DW_UNSND (attr) != 0)
14911 fnp->is_artificial = 1;
14912
14913 /* Check for defaulted methods. */
14914 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14915 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14916 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14917
14918 /* Check for deleted methods. */
14919 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14920 if (attr != nullptr && DW_UNSND (attr) != 0)
14921 fnp->is_deleted = 1;
14922
14923 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14924
14925 /* Get index in virtual function table if it is a virtual member
14926 function. For older versions of GCC, this is an offset in the
14927 appropriate virtual table, as specified by DW_AT_containing_type.
14928 For everyone else, it is an expression to be evaluated relative
14929 to the object address. */
14930
14931 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14932 if (attr != nullptr)
14933 {
14934 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14935 {
14936 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14937 {
14938 /* Old-style GCC. */
14939 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14940 }
14941 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14942 || (DW_BLOCK (attr)->size > 1
14943 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14944 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14945 {
14946 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14947 if ((fnp->voffset % cu->header.addr_size) != 0)
14948 dwarf2_complex_location_expr_complaint ();
14949 else
14950 fnp->voffset /= cu->header.addr_size;
14951 fnp->voffset += 2;
14952 }
14953 else
14954 dwarf2_complex_location_expr_complaint ();
14955
14956 if (!fnp->fcontext)
14957 {
14958 /* If there is no `this' field and no DW_AT_containing_type,
14959 we cannot actually find a base class context for the
14960 vtable! */
14961 if (TYPE_NFIELDS (this_type) == 0
14962 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14963 {
14964 complaint (_("cannot determine context for virtual member "
14965 "function \"%s\" (offset %s)"),
14966 fieldname, sect_offset_str (die->sect_off));
14967 }
14968 else
14969 {
14970 fnp->fcontext
14971 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14972 }
14973 }
14974 }
14975 else if (attr->form_is_section_offset ())
14976 {
14977 dwarf2_complex_location_expr_complaint ();
14978 }
14979 else
14980 {
14981 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14982 fieldname);
14983 }
14984 }
14985 else
14986 {
14987 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14988 if (attr && DW_UNSND (attr))
14989 {
14990 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14991 complaint (_("Member function \"%s\" (offset %s) is virtual "
14992 "but the vtable offset is not specified"),
14993 fieldname, sect_offset_str (die->sect_off));
14994 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14995 TYPE_CPLUS_DYNAMIC (type) = 1;
14996 }
14997 }
14998 }
14999
15000 /* Create the vector of member function fields, and attach it to the type. */
15001
15002 static void
15003 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15004 struct dwarf2_cu *cu)
15005 {
15006 if (cu->language == language_ada)
15007 error (_("unexpected member functions in Ada type"));
15008
15009 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15010 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15011 TYPE_ALLOC (type,
15012 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15013
15014 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15015 {
15016 struct fnfieldlist &nf = fip->fnfieldlists[i];
15017 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15018
15019 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15020 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15021 fn_flp->fn_fields = (struct fn_field *)
15022 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15023
15024 for (int k = 0; k < nf.fnfields.size (); ++k)
15025 fn_flp->fn_fields[k] = nf.fnfields[k];
15026 }
15027
15028 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15029 }
15030
15031 /* Returns non-zero if NAME is the name of a vtable member in CU's
15032 language, zero otherwise. */
15033 static int
15034 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15035 {
15036 static const char vptr[] = "_vptr";
15037
15038 /* Look for the C++ form of the vtable. */
15039 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15040 return 1;
15041
15042 return 0;
15043 }
15044
15045 /* GCC outputs unnamed structures that are really pointers to member
15046 functions, with the ABI-specified layout. If TYPE describes
15047 such a structure, smash it into a member function type.
15048
15049 GCC shouldn't do this; it should just output pointer to member DIEs.
15050 This is GCC PR debug/28767. */
15051
15052 static void
15053 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15054 {
15055 struct type *pfn_type, *self_type, *new_type;
15056
15057 /* Check for a structure with no name and two children. */
15058 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15059 return;
15060
15061 /* Check for __pfn and __delta members. */
15062 if (TYPE_FIELD_NAME (type, 0) == NULL
15063 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15064 || TYPE_FIELD_NAME (type, 1) == NULL
15065 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15066 return;
15067
15068 /* Find the type of the method. */
15069 pfn_type = TYPE_FIELD_TYPE (type, 0);
15070 if (pfn_type == NULL
15071 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15072 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15073 return;
15074
15075 /* Look for the "this" argument. */
15076 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15077 if (TYPE_NFIELDS (pfn_type) == 0
15078 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15079 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15080 return;
15081
15082 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15083 new_type = alloc_type (objfile);
15084 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15085 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15086 TYPE_VARARGS (pfn_type));
15087 smash_to_methodptr_type (type, new_type);
15088 }
15089
15090 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15091 appropriate error checking and issuing complaints if there is a
15092 problem. */
15093
15094 static ULONGEST
15095 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15096 {
15097 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15098
15099 if (attr == nullptr)
15100 return 0;
15101
15102 if (!attr->form_is_constant ())
15103 {
15104 complaint (_("DW_AT_alignment must have constant form"
15105 " - DIE at %s [in module %s]"),
15106 sect_offset_str (die->sect_off),
15107 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15108 return 0;
15109 }
15110
15111 ULONGEST align;
15112 if (attr->form == DW_FORM_sdata)
15113 {
15114 LONGEST val = DW_SND (attr);
15115 if (val < 0)
15116 {
15117 complaint (_("DW_AT_alignment value must not be negative"
15118 " - DIE at %s [in module %s]"),
15119 sect_offset_str (die->sect_off),
15120 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15121 return 0;
15122 }
15123 align = val;
15124 }
15125 else
15126 align = DW_UNSND (attr);
15127
15128 if (align == 0)
15129 {
15130 complaint (_("DW_AT_alignment value must not be zero"
15131 " - DIE at %s [in module %s]"),
15132 sect_offset_str (die->sect_off),
15133 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15134 return 0;
15135 }
15136 if ((align & (align - 1)) != 0)
15137 {
15138 complaint (_("DW_AT_alignment value must be a power of 2"
15139 " - DIE at %s [in module %s]"),
15140 sect_offset_str (die->sect_off),
15141 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15142 return 0;
15143 }
15144
15145 return align;
15146 }
15147
15148 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15149 the alignment for TYPE. */
15150
15151 static void
15152 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15153 struct type *type)
15154 {
15155 if (!set_type_align (type, get_alignment (cu, die)))
15156 complaint (_("DW_AT_alignment value too large"
15157 " - DIE at %s [in module %s]"),
15158 sect_offset_str (die->sect_off),
15159 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15160 }
15161
15162 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15163 constant for a type, according to DWARF5 spec, Table 5.5. */
15164
15165 static bool
15166 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15167 {
15168 switch (value)
15169 {
15170 case DW_CC_normal:
15171 case DW_CC_pass_by_reference:
15172 case DW_CC_pass_by_value:
15173 return true;
15174
15175 default:
15176 complaint (_("unrecognized DW_AT_calling_convention value "
15177 "(%s) for a type"), pulongest (value));
15178 return false;
15179 }
15180 }
15181
15182 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15183 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15184 also according to GNU-specific values (see include/dwarf2.h). */
15185
15186 static bool
15187 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15188 {
15189 switch (value)
15190 {
15191 case DW_CC_normal:
15192 case DW_CC_program:
15193 case DW_CC_nocall:
15194 return true;
15195
15196 case DW_CC_GNU_renesas_sh:
15197 case DW_CC_GNU_borland_fastcall_i386:
15198 case DW_CC_GDB_IBM_OpenCL:
15199 return true;
15200
15201 default:
15202 complaint (_("unrecognized DW_AT_calling_convention value "
15203 "(%s) for a subroutine"), pulongest (value));
15204 return false;
15205 }
15206 }
15207
15208 /* Called when we find the DIE that starts a structure or union scope
15209 (definition) to create a type for the structure or union. Fill in
15210 the type's name and general properties; the members will not be
15211 processed until process_structure_scope. A symbol table entry for
15212 the type will also not be done until process_structure_scope (assuming
15213 the type has a name).
15214
15215 NOTE: we need to call these functions regardless of whether or not the
15216 DIE has a DW_AT_name attribute, since it might be an anonymous
15217 structure or union. This gets the type entered into our set of
15218 user defined types. */
15219
15220 static struct type *
15221 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15222 {
15223 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15224 struct type *type;
15225 struct attribute *attr;
15226 const char *name;
15227
15228 /* If the definition of this type lives in .debug_types, read that type.
15229 Don't follow DW_AT_specification though, that will take us back up
15230 the chain and we want to go down. */
15231 attr = die->attr (DW_AT_signature);
15232 if (attr != nullptr)
15233 {
15234 type = get_DW_AT_signature_type (die, attr, cu);
15235
15236 /* The type's CU may not be the same as CU.
15237 Ensure TYPE is recorded with CU in die_type_hash. */
15238 return set_die_type (die, type, cu);
15239 }
15240
15241 type = alloc_type (objfile);
15242 INIT_CPLUS_SPECIFIC (type);
15243
15244 name = dwarf2_name (die, cu);
15245 if (name != NULL)
15246 {
15247 if (cu->language == language_cplus
15248 || cu->language == language_d
15249 || cu->language == language_rust)
15250 {
15251 const char *full_name = dwarf2_full_name (name, die, cu);
15252
15253 /* dwarf2_full_name might have already finished building the DIE's
15254 type. If so, there is no need to continue. */
15255 if (get_die_type (die, cu) != NULL)
15256 return get_die_type (die, cu);
15257
15258 TYPE_NAME (type) = full_name;
15259 }
15260 else
15261 {
15262 /* The name is already allocated along with this objfile, so
15263 we don't need to duplicate it for the type. */
15264 TYPE_NAME (type) = name;
15265 }
15266 }
15267
15268 if (die->tag == DW_TAG_structure_type)
15269 {
15270 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15271 }
15272 else if (die->tag == DW_TAG_union_type)
15273 {
15274 TYPE_CODE (type) = TYPE_CODE_UNION;
15275 }
15276 else
15277 {
15278 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15279 }
15280
15281 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15282 TYPE_DECLARED_CLASS (type) = 1;
15283
15284 /* Store the calling convention in the type if it's available in
15285 the die. Otherwise the calling convention remains set to
15286 the default value DW_CC_normal. */
15287 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15288 if (attr != nullptr
15289 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15290 {
15291 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15292 TYPE_CPLUS_CALLING_CONVENTION (type)
15293 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15294 }
15295
15296 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15297 if (attr != nullptr)
15298 {
15299 if (attr->form_is_constant ())
15300 TYPE_LENGTH (type) = DW_UNSND (attr);
15301 else
15302 {
15303 /* For the moment, dynamic type sizes are not supported
15304 by GDB's struct type. The actual size is determined
15305 on-demand when resolving the type of a given object,
15306 so set the type's length to zero for now. Otherwise,
15307 we record an expression as the length, and that expression
15308 could lead to a very large value, which could eventually
15309 lead to us trying to allocate that much memory when creating
15310 a value of that type. */
15311 TYPE_LENGTH (type) = 0;
15312 }
15313 }
15314 else
15315 {
15316 TYPE_LENGTH (type) = 0;
15317 }
15318
15319 maybe_set_alignment (cu, die, type);
15320
15321 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15322 {
15323 /* ICC<14 does not output the required DW_AT_declaration on
15324 incomplete types, but gives them a size of zero. */
15325 TYPE_STUB (type) = 1;
15326 }
15327 else
15328 TYPE_STUB_SUPPORTED (type) = 1;
15329
15330 if (die_is_declaration (die, cu))
15331 TYPE_STUB (type) = 1;
15332 else if (attr == NULL && die->child == NULL
15333 && producer_is_realview (cu->producer))
15334 /* RealView does not output the required DW_AT_declaration
15335 on incomplete types. */
15336 TYPE_STUB (type) = 1;
15337
15338 /* We need to add the type field to the die immediately so we don't
15339 infinitely recurse when dealing with pointers to the structure
15340 type within the structure itself. */
15341 set_die_type (die, type, cu);
15342
15343 /* set_die_type should be already done. */
15344 set_descriptive_type (type, die, cu);
15345
15346 return type;
15347 }
15348
15349 static void handle_struct_member_die
15350 (struct die_info *child_die,
15351 struct type *type,
15352 struct field_info *fi,
15353 std::vector<struct symbol *> *template_args,
15354 struct dwarf2_cu *cu);
15355
15356 /* A helper for handle_struct_member_die that handles
15357 DW_TAG_variant_part. */
15358
15359 static void
15360 handle_variant_part (struct die_info *die, struct type *type,
15361 struct field_info *fi,
15362 std::vector<struct symbol *> *template_args,
15363 struct dwarf2_cu *cu)
15364 {
15365 variant_part_builder *new_part;
15366 if (fi->current_variant_part == nullptr)
15367 {
15368 fi->variant_parts.emplace_back ();
15369 new_part = &fi->variant_parts.back ();
15370 }
15371 else if (!fi->current_variant_part->processing_variant)
15372 {
15373 complaint (_("nested DW_TAG_variant_part seen "
15374 "- DIE at %s [in module %s]"),
15375 sect_offset_str (die->sect_off),
15376 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15377 return;
15378 }
15379 else
15380 {
15381 variant_field &current = fi->current_variant_part->variants.back ();
15382 current.variant_parts.emplace_back ();
15383 new_part = &current.variant_parts.back ();
15384 }
15385
15386 /* When we recurse, we want callees to add to this new variant
15387 part. */
15388 scoped_restore save_current_variant_part
15389 = make_scoped_restore (&fi->current_variant_part, new_part);
15390
15391 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15392 if (discr == NULL)
15393 {
15394 /* It's a univariant form, an extension we support. */
15395 }
15396 else if (discr->form_is_ref ())
15397 {
15398 struct dwarf2_cu *target_cu = cu;
15399 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15400
15401 new_part->discriminant_offset = target_die->sect_off;
15402 }
15403 else
15404 {
15405 complaint (_("DW_AT_discr does not have DIE reference form"
15406 " - DIE at %s [in module %s]"),
15407 sect_offset_str (die->sect_off),
15408 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15409 }
15410
15411 for (die_info *child_die = die->child;
15412 child_die != NULL;
15413 child_die = child_die->sibling)
15414 handle_struct_member_die (child_die, type, fi, template_args, cu);
15415 }
15416
15417 /* A helper for handle_struct_member_die that handles
15418 DW_TAG_variant. */
15419
15420 static void
15421 handle_variant (struct die_info *die, struct type *type,
15422 struct field_info *fi,
15423 std::vector<struct symbol *> *template_args,
15424 struct dwarf2_cu *cu)
15425 {
15426 if (fi->current_variant_part == nullptr)
15427 {
15428 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15429 "- DIE at %s [in module %s]"),
15430 sect_offset_str (die->sect_off),
15431 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15432 return;
15433 }
15434 if (fi->current_variant_part->processing_variant)
15435 {
15436 complaint (_("nested DW_TAG_variant seen "
15437 "- DIE at %s [in module %s]"),
15438 sect_offset_str (die->sect_off),
15439 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15440 return;
15441 }
15442
15443 scoped_restore save_processing_variant
15444 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15445 true);
15446
15447 fi->current_variant_part->variants.emplace_back ();
15448 variant_field &variant = fi->current_variant_part->variants.back ();
15449 variant.first_field = fi->fields.size ();
15450
15451 /* In a variant we want to get the discriminant and also add a
15452 field for our sole member child. */
15453 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15454 if (discr == nullptr)
15455 {
15456 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15457 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15458 variant.default_branch = true;
15459 else
15460 variant.discr_list_data = DW_BLOCK (discr);
15461 }
15462 else
15463 variant.discriminant_value = DW_UNSND (discr);
15464
15465 for (die_info *variant_child = die->child;
15466 variant_child != NULL;
15467 variant_child = variant_child->sibling)
15468 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15469
15470 variant.last_field = fi->fields.size ();
15471 }
15472
15473 /* A helper for process_structure_scope that handles a single member
15474 DIE. */
15475
15476 static void
15477 handle_struct_member_die (struct die_info *child_die, struct type *type,
15478 struct field_info *fi,
15479 std::vector<struct symbol *> *template_args,
15480 struct dwarf2_cu *cu)
15481 {
15482 if (child_die->tag == DW_TAG_member
15483 || child_die->tag == DW_TAG_variable)
15484 {
15485 /* NOTE: carlton/2002-11-05: A C++ static data member
15486 should be a DW_TAG_member that is a declaration, but
15487 all versions of G++ as of this writing (so through at
15488 least 3.2.1) incorrectly generate DW_TAG_variable
15489 tags for them instead. */
15490 dwarf2_add_field (fi, child_die, cu);
15491 }
15492 else if (child_die->tag == DW_TAG_subprogram)
15493 {
15494 /* Rust doesn't have member functions in the C++ sense.
15495 However, it does emit ordinary functions as children
15496 of a struct DIE. */
15497 if (cu->language == language_rust)
15498 read_func_scope (child_die, cu);
15499 else
15500 {
15501 /* C++ member function. */
15502 dwarf2_add_member_fn (fi, child_die, type, cu);
15503 }
15504 }
15505 else if (child_die->tag == DW_TAG_inheritance)
15506 {
15507 /* C++ base class field. */
15508 dwarf2_add_field (fi, child_die, cu);
15509 }
15510 else if (type_can_define_types (child_die))
15511 dwarf2_add_type_defn (fi, child_die, cu);
15512 else if (child_die->tag == DW_TAG_template_type_param
15513 || child_die->tag == DW_TAG_template_value_param)
15514 {
15515 struct symbol *arg = new_symbol (child_die, NULL, cu);
15516
15517 if (arg != NULL)
15518 template_args->push_back (arg);
15519 }
15520 else if (child_die->tag == DW_TAG_variant_part)
15521 handle_variant_part (child_die, type, fi, template_args, cu);
15522 else if (child_die->tag == DW_TAG_variant)
15523 handle_variant (child_die, type, fi, template_args, cu);
15524 }
15525
15526 /* Finish creating a structure or union type, including filling in
15527 its members and creating a symbol for it. */
15528
15529 static void
15530 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15531 {
15532 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15533 struct die_info *child_die;
15534 struct type *type;
15535
15536 type = get_die_type (die, cu);
15537 if (type == NULL)
15538 type = read_structure_type (die, cu);
15539
15540 bool has_template_parameters = false;
15541 if (die->child != NULL && ! die_is_declaration (die, cu))
15542 {
15543 struct field_info fi;
15544 std::vector<struct symbol *> template_args;
15545
15546 child_die = die->child;
15547
15548 while (child_die && child_die->tag)
15549 {
15550 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15551 child_die = child_die->sibling;
15552 }
15553
15554 /* Attach template arguments to type. */
15555 if (!template_args.empty ())
15556 {
15557 has_template_parameters = true;
15558 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15559 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15560 TYPE_TEMPLATE_ARGUMENTS (type)
15561 = XOBNEWVEC (&objfile->objfile_obstack,
15562 struct symbol *,
15563 TYPE_N_TEMPLATE_ARGUMENTS (type));
15564 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15565 template_args.data (),
15566 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15567 * sizeof (struct symbol *)));
15568 }
15569
15570 /* Attach fields and member functions to the type. */
15571 if (fi.nfields () > 0)
15572 dwarf2_attach_fields_to_type (&fi, type, cu);
15573 if (!fi.fnfieldlists.empty ())
15574 {
15575 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15576
15577 /* Get the type which refers to the base class (possibly this
15578 class itself) which contains the vtable pointer for the current
15579 class from the DW_AT_containing_type attribute. This use of
15580 DW_AT_containing_type is a GNU extension. */
15581
15582 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15583 {
15584 struct type *t = die_containing_type (die, cu);
15585
15586 set_type_vptr_basetype (type, t);
15587 if (type == t)
15588 {
15589 int i;
15590
15591 /* Our own class provides vtbl ptr. */
15592 for (i = TYPE_NFIELDS (t) - 1;
15593 i >= TYPE_N_BASECLASSES (t);
15594 --i)
15595 {
15596 const char *fieldname = TYPE_FIELD_NAME (t, i);
15597
15598 if (is_vtable_name (fieldname, cu))
15599 {
15600 set_type_vptr_fieldno (type, i);
15601 break;
15602 }
15603 }
15604
15605 /* Complain if virtual function table field not found. */
15606 if (i < TYPE_N_BASECLASSES (t))
15607 complaint (_("virtual function table pointer "
15608 "not found when defining class '%s'"),
15609 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15610 }
15611 else
15612 {
15613 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15614 }
15615 }
15616 else if (cu->producer
15617 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15618 {
15619 /* The IBM XLC compiler does not provide direct indication
15620 of the containing type, but the vtable pointer is
15621 always named __vfp. */
15622
15623 int i;
15624
15625 for (i = TYPE_NFIELDS (type) - 1;
15626 i >= TYPE_N_BASECLASSES (type);
15627 --i)
15628 {
15629 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15630 {
15631 set_type_vptr_fieldno (type, i);
15632 set_type_vptr_basetype (type, type);
15633 break;
15634 }
15635 }
15636 }
15637 }
15638
15639 /* Copy fi.typedef_field_list linked list elements content into the
15640 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15641 if (!fi.typedef_field_list.empty ())
15642 {
15643 int count = fi.typedef_field_list.size ();
15644
15645 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15646 TYPE_TYPEDEF_FIELD_ARRAY (type)
15647 = ((struct decl_field *)
15648 TYPE_ALLOC (type,
15649 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15650 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15651
15652 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15653 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15654 }
15655
15656 /* Copy fi.nested_types_list linked list elements content into the
15657 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15658 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15659 {
15660 int count = fi.nested_types_list.size ();
15661
15662 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15663 TYPE_NESTED_TYPES_ARRAY (type)
15664 = ((struct decl_field *)
15665 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15666 TYPE_NESTED_TYPES_COUNT (type) = count;
15667
15668 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15669 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15670 }
15671 }
15672
15673 quirk_gcc_member_function_pointer (type, objfile);
15674 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15675 cu->rust_unions.push_back (type);
15676
15677 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15678 snapshots) has been known to create a die giving a declaration
15679 for a class that has, as a child, a die giving a definition for a
15680 nested class. So we have to process our children even if the
15681 current die is a declaration. Normally, of course, a declaration
15682 won't have any children at all. */
15683
15684 child_die = die->child;
15685
15686 while (child_die != NULL && child_die->tag)
15687 {
15688 if (child_die->tag == DW_TAG_member
15689 || child_die->tag == DW_TAG_variable
15690 || child_die->tag == DW_TAG_inheritance
15691 || child_die->tag == DW_TAG_template_value_param
15692 || child_die->tag == DW_TAG_template_type_param)
15693 {
15694 /* Do nothing. */
15695 }
15696 else
15697 process_die (child_die, cu);
15698
15699 child_die = child_die->sibling;
15700 }
15701
15702 /* Do not consider external references. According to the DWARF standard,
15703 these DIEs are identified by the fact that they have no byte_size
15704 attribute, and a declaration attribute. */
15705 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15706 || !die_is_declaration (die, cu))
15707 {
15708 struct symbol *sym = new_symbol (die, type, cu);
15709
15710 if (has_template_parameters)
15711 {
15712 struct symtab *symtab;
15713 if (sym != nullptr)
15714 symtab = symbol_symtab (sym);
15715 else if (cu->line_header != nullptr)
15716 {
15717 /* Any related symtab will do. */
15718 symtab
15719 = cu->line_header->file_names ()[0].symtab;
15720 }
15721 else
15722 {
15723 symtab = nullptr;
15724 complaint (_("could not find suitable "
15725 "symtab for template parameter"
15726 " - DIE at %s [in module %s]"),
15727 sect_offset_str (die->sect_off),
15728 objfile_name (objfile));
15729 }
15730
15731 if (symtab != nullptr)
15732 {
15733 /* Make sure that the symtab is set on the new symbols.
15734 Even though they don't appear in this symtab directly,
15735 other parts of gdb assume that symbols do, and this is
15736 reasonably true. */
15737 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15738 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15739 }
15740 }
15741 }
15742 }
15743
15744 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15745 update TYPE using some information only available in DIE's children. */
15746
15747 static void
15748 update_enumeration_type_from_children (struct die_info *die,
15749 struct type *type,
15750 struct dwarf2_cu *cu)
15751 {
15752 struct die_info *child_die;
15753 int unsigned_enum = 1;
15754 int flag_enum = 1;
15755
15756 auto_obstack obstack;
15757
15758 for (child_die = die->child;
15759 child_die != NULL && child_die->tag;
15760 child_die = child_die->sibling)
15761 {
15762 struct attribute *attr;
15763 LONGEST value;
15764 const gdb_byte *bytes;
15765 struct dwarf2_locexpr_baton *baton;
15766 const char *name;
15767
15768 if (child_die->tag != DW_TAG_enumerator)
15769 continue;
15770
15771 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15772 if (attr == NULL)
15773 continue;
15774
15775 name = dwarf2_name (child_die, cu);
15776 if (name == NULL)
15777 name = "<anonymous enumerator>";
15778
15779 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15780 &value, &bytes, &baton);
15781 if (value < 0)
15782 {
15783 unsigned_enum = 0;
15784 flag_enum = 0;
15785 }
15786 else
15787 {
15788 if (count_one_bits_ll (value) >= 2)
15789 flag_enum = 0;
15790 }
15791
15792 /* If we already know that the enum type is neither unsigned, nor
15793 a flag type, no need to look at the rest of the enumerates. */
15794 if (!unsigned_enum && !flag_enum)
15795 break;
15796 }
15797
15798 if (unsigned_enum)
15799 TYPE_UNSIGNED (type) = 1;
15800 if (flag_enum)
15801 TYPE_FLAG_ENUM (type) = 1;
15802 }
15803
15804 /* Given a DW_AT_enumeration_type die, set its type. We do not
15805 complete the type's fields yet, or create any symbols. */
15806
15807 static struct type *
15808 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15809 {
15810 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15811 struct type *type;
15812 struct attribute *attr;
15813 const char *name;
15814
15815 /* If the definition of this type lives in .debug_types, read that type.
15816 Don't follow DW_AT_specification though, that will take us back up
15817 the chain and we want to go down. */
15818 attr = die->attr (DW_AT_signature);
15819 if (attr != nullptr)
15820 {
15821 type = get_DW_AT_signature_type (die, attr, cu);
15822
15823 /* The type's CU may not be the same as CU.
15824 Ensure TYPE is recorded with CU in die_type_hash. */
15825 return set_die_type (die, type, cu);
15826 }
15827
15828 type = alloc_type (objfile);
15829
15830 TYPE_CODE (type) = TYPE_CODE_ENUM;
15831 name = dwarf2_full_name (NULL, die, cu);
15832 if (name != NULL)
15833 TYPE_NAME (type) = name;
15834
15835 attr = dwarf2_attr (die, DW_AT_type, cu);
15836 if (attr != NULL)
15837 {
15838 struct type *underlying_type = die_type (die, cu);
15839
15840 TYPE_TARGET_TYPE (type) = underlying_type;
15841 }
15842
15843 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15844 if (attr != nullptr)
15845 {
15846 TYPE_LENGTH (type) = DW_UNSND (attr);
15847 }
15848 else
15849 {
15850 TYPE_LENGTH (type) = 0;
15851 }
15852
15853 maybe_set_alignment (cu, die, type);
15854
15855 /* The enumeration DIE can be incomplete. In Ada, any type can be
15856 declared as private in the package spec, and then defined only
15857 inside the package body. Such types are known as Taft Amendment
15858 Types. When another package uses such a type, an incomplete DIE
15859 may be generated by the compiler. */
15860 if (die_is_declaration (die, cu))
15861 TYPE_STUB (type) = 1;
15862
15863 /* Finish the creation of this type by using the enum's children.
15864 We must call this even when the underlying type has been provided
15865 so that we can determine if we're looking at a "flag" enum. */
15866 update_enumeration_type_from_children (die, type, cu);
15867
15868 /* If this type has an underlying type that is not a stub, then we
15869 may use its attributes. We always use the "unsigned" attribute
15870 in this situation, because ordinarily we guess whether the type
15871 is unsigned -- but the guess can be wrong and the underlying type
15872 can tell us the reality. However, we defer to a local size
15873 attribute if one exists, because this lets the compiler override
15874 the underlying type if needed. */
15875 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15876 {
15877 struct type *underlying_type = TYPE_TARGET_TYPE (type);
15878 underlying_type = check_typedef (underlying_type);
15879 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
15880 if (TYPE_LENGTH (type) == 0)
15881 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
15882 if (TYPE_RAW_ALIGN (type) == 0
15883 && TYPE_RAW_ALIGN (underlying_type) != 0)
15884 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
15885 }
15886
15887 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15888
15889 return set_die_type (die, type, cu);
15890 }
15891
15892 /* Given a pointer to a die which begins an enumeration, process all
15893 the dies that define the members of the enumeration, and create the
15894 symbol for the enumeration type.
15895
15896 NOTE: We reverse the order of the element list. */
15897
15898 static void
15899 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15900 {
15901 struct type *this_type;
15902
15903 this_type = get_die_type (die, cu);
15904 if (this_type == NULL)
15905 this_type = read_enumeration_type (die, cu);
15906
15907 if (die->child != NULL)
15908 {
15909 struct die_info *child_die;
15910 struct symbol *sym;
15911 std::vector<struct field> fields;
15912 const char *name;
15913
15914 child_die = die->child;
15915 while (child_die && child_die->tag)
15916 {
15917 if (child_die->tag != DW_TAG_enumerator)
15918 {
15919 process_die (child_die, cu);
15920 }
15921 else
15922 {
15923 name = dwarf2_name (child_die, cu);
15924 if (name)
15925 {
15926 sym = new_symbol (child_die, this_type, cu);
15927
15928 fields.emplace_back ();
15929 struct field &field = fields.back ();
15930
15931 FIELD_NAME (field) = sym->linkage_name ();
15932 FIELD_TYPE (field) = NULL;
15933 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15934 FIELD_BITSIZE (field) = 0;
15935 }
15936 }
15937
15938 child_die = child_die->sibling;
15939 }
15940
15941 if (!fields.empty ())
15942 {
15943 TYPE_NFIELDS (this_type) = fields.size ();
15944 TYPE_FIELDS (this_type) = (struct field *)
15945 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15946 memcpy (TYPE_FIELDS (this_type), fields.data (),
15947 sizeof (struct field) * fields.size ());
15948 }
15949 }
15950
15951 /* If we are reading an enum from a .debug_types unit, and the enum
15952 is a declaration, and the enum is not the signatured type in the
15953 unit, then we do not want to add a symbol for it. Adding a
15954 symbol would in some cases obscure the true definition of the
15955 enum, giving users an incomplete type when the definition is
15956 actually available. Note that we do not want to do this for all
15957 enums which are just declarations, because C++0x allows forward
15958 enum declarations. */
15959 if (cu->per_cu->is_debug_types
15960 && die_is_declaration (die, cu))
15961 {
15962 struct signatured_type *sig_type;
15963
15964 sig_type = (struct signatured_type *) cu->per_cu;
15965 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15966 if (sig_type->type_offset_in_section != die->sect_off)
15967 return;
15968 }
15969
15970 new_symbol (die, this_type, cu);
15971 }
15972
15973 /* Extract all information from a DW_TAG_array_type DIE and put it in
15974 the DIE's type field. For now, this only handles one dimensional
15975 arrays. */
15976
15977 static struct type *
15978 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15979 {
15980 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15981 struct die_info *child_die;
15982 struct type *type;
15983 struct type *element_type, *range_type, *index_type;
15984 struct attribute *attr;
15985 const char *name;
15986 struct dynamic_prop *byte_stride_prop = NULL;
15987 unsigned int bit_stride = 0;
15988
15989 element_type = die_type (die, cu);
15990
15991 /* The die_type call above may have already set the type for this DIE. */
15992 type = get_die_type (die, cu);
15993 if (type)
15994 return type;
15995
15996 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15997 if (attr != NULL)
15998 {
15999 int stride_ok;
16000 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
16001
16002 byte_stride_prop
16003 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16004 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16005 prop_type);
16006 if (!stride_ok)
16007 {
16008 complaint (_("unable to read array DW_AT_byte_stride "
16009 " - DIE at %s [in module %s]"),
16010 sect_offset_str (die->sect_off),
16011 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16012 /* Ignore this attribute. We will likely not be able to print
16013 arrays of this type correctly, but there is little we can do
16014 to help if we cannot read the attribute's value. */
16015 byte_stride_prop = NULL;
16016 }
16017 }
16018
16019 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16020 if (attr != NULL)
16021 bit_stride = DW_UNSND (attr);
16022
16023 /* Irix 6.2 native cc creates array types without children for
16024 arrays with unspecified length. */
16025 if (die->child == NULL)
16026 {
16027 index_type = objfile_type (objfile)->builtin_int;
16028 range_type = create_static_range_type (NULL, index_type, 0, -1);
16029 type = create_array_type_with_stride (NULL, element_type, range_type,
16030 byte_stride_prop, bit_stride);
16031 return set_die_type (die, type, cu);
16032 }
16033
16034 std::vector<struct type *> range_types;
16035 child_die = die->child;
16036 while (child_die && child_die->tag)
16037 {
16038 if (child_die->tag == DW_TAG_subrange_type)
16039 {
16040 struct type *child_type = read_type_die (child_die, cu);
16041
16042 if (child_type != NULL)
16043 {
16044 /* The range type was succesfully read. Save it for the
16045 array type creation. */
16046 range_types.push_back (child_type);
16047 }
16048 }
16049 child_die = child_die->sibling;
16050 }
16051
16052 /* Dwarf2 dimensions are output from left to right, create the
16053 necessary array types in backwards order. */
16054
16055 type = element_type;
16056
16057 if (read_array_order (die, cu) == DW_ORD_col_major)
16058 {
16059 int i = 0;
16060
16061 while (i < range_types.size ())
16062 type = create_array_type_with_stride (NULL, type, range_types[i++],
16063 byte_stride_prop, bit_stride);
16064 }
16065 else
16066 {
16067 size_t ndim = range_types.size ();
16068 while (ndim-- > 0)
16069 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16070 byte_stride_prop, bit_stride);
16071 }
16072
16073 /* Understand Dwarf2 support for vector types (like they occur on
16074 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16075 array type. This is not part of the Dwarf2/3 standard yet, but a
16076 custom vendor extension. The main difference between a regular
16077 array and the vector variant is that vectors are passed by value
16078 to functions. */
16079 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16080 if (attr != nullptr)
16081 make_vector_type (type);
16082
16083 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16084 implementation may choose to implement triple vectors using this
16085 attribute. */
16086 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16087 if (attr != nullptr)
16088 {
16089 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16090 TYPE_LENGTH (type) = DW_UNSND (attr);
16091 else
16092 complaint (_("DW_AT_byte_size for array type smaller "
16093 "than the total size of elements"));
16094 }
16095
16096 name = dwarf2_name (die, cu);
16097 if (name)
16098 TYPE_NAME (type) = name;
16099
16100 maybe_set_alignment (cu, die, type);
16101
16102 /* Install the type in the die. */
16103 set_die_type (die, type, cu);
16104
16105 /* set_die_type should be already done. */
16106 set_descriptive_type (type, die, cu);
16107
16108 return type;
16109 }
16110
16111 static enum dwarf_array_dim_ordering
16112 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16113 {
16114 struct attribute *attr;
16115
16116 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16117
16118 if (attr != nullptr)
16119 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16120
16121 /* GNU F77 is a special case, as at 08/2004 array type info is the
16122 opposite order to the dwarf2 specification, but data is still
16123 laid out as per normal fortran.
16124
16125 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16126 version checking. */
16127
16128 if (cu->language == language_fortran
16129 && cu->producer && strstr (cu->producer, "GNU F77"))
16130 {
16131 return DW_ORD_row_major;
16132 }
16133
16134 switch (cu->language_defn->la_array_ordering)
16135 {
16136 case array_column_major:
16137 return DW_ORD_col_major;
16138 case array_row_major:
16139 default:
16140 return DW_ORD_row_major;
16141 };
16142 }
16143
16144 /* Extract all information from a DW_TAG_set_type DIE and put it in
16145 the DIE's type field. */
16146
16147 static struct type *
16148 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16149 {
16150 struct type *domain_type, *set_type;
16151 struct attribute *attr;
16152
16153 domain_type = die_type (die, cu);
16154
16155 /* The die_type call above may have already set the type for this DIE. */
16156 set_type = get_die_type (die, cu);
16157 if (set_type)
16158 return set_type;
16159
16160 set_type = create_set_type (NULL, domain_type);
16161
16162 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16163 if (attr != nullptr)
16164 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16165
16166 maybe_set_alignment (cu, die, set_type);
16167
16168 return set_die_type (die, set_type, cu);
16169 }
16170
16171 /* A helper for read_common_block that creates a locexpr baton.
16172 SYM is the symbol which we are marking as computed.
16173 COMMON_DIE is the DIE for the common block.
16174 COMMON_LOC is the location expression attribute for the common
16175 block itself.
16176 MEMBER_LOC is the location expression attribute for the particular
16177 member of the common block that we are processing.
16178 CU is the CU from which the above come. */
16179
16180 static void
16181 mark_common_block_symbol_computed (struct symbol *sym,
16182 struct die_info *common_die,
16183 struct attribute *common_loc,
16184 struct attribute *member_loc,
16185 struct dwarf2_cu *cu)
16186 {
16187 struct dwarf2_per_objfile *dwarf2_per_objfile
16188 = cu->per_cu->dwarf2_per_objfile;
16189 struct objfile *objfile = dwarf2_per_objfile->objfile;
16190 struct dwarf2_locexpr_baton *baton;
16191 gdb_byte *ptr;
16192 unsigned int cu_off;
16193 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16194 LONGEST offset = 0;
16195
16196 gdb_assert (common_loc && member_loc);
16197 gdb_assert (common_loc->form_is_block ());
16198 gdb_assert (member_loc->form_is_block ()
16199 || member_loc->form_is_constant ());
16200
16201 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16202 baton->per_cu = cu->per_cu;
16203 gdb_assert (baton->per_cu);
16204
16205 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16206
16207 if (member_loc->form_is_constant ())
16208 {
16209 offset = member_loc->constant_value (0);
16210 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16211 }
16212 else
16213 baton->size += DW_BLOCK (member_loc)->size;
16214
16215 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16216 baton->data = ptr;
16217
16218 *ptr++ = DW_OP_call4;
16219 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16220 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16221 ptr += 4;
16222
16223 if (member_loc->form_is_constant ())
16224 {
16225 *ptr++ = DW_OP_addr;
16226 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16227 ptr += cu->header.addr_size;
16228 }
16229 else
16230 {
16231 /* We have to copy the data here, because DW_OP_call4 will only
16232 use a DW_AT_location attribute. */
16233 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16234 ptr += DW_BLOCK (member_loc)->size;
16235 }
16236
16237 *ptr++ = DW_OP_plus;
16238 gdb_assert (ptr - baton->data == baton->size);
16239
16240 SYMBOL_LOCATION_BATON (sym) = baton;
16241 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16242 }
16243
16244 /* Create appropriate locally-scoped variables for all the
16245 DW_TAG_common_block entries. Also create a struct common_block
16246 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16247 is used to separate the common blocks name namespace from regular
16248 variable names. */
16249
16250 static void
16251 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16252 {
16253 struct attribute *attr;
16254
16255 attr = dwarf2_attr (die, DW_AT_location, cu);
16256 if (attr != nullptr)
16257 {
16258 /* Support the .debug_loc offsets. */
16259 if (attr->form_is_block ())
16260 {
16261 /* Ok. */
16262 }
16263 else if (attr->form_is_section_offset ())
16264 {
16265 dwarf2_complex_location_expr_complaint ();
16266 attr = NULL;
16267 }
16268 else
16269 {
16270 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16271 "common block member");
16272 attr = NULL;
16273 }
16274 }
16275
16276 if (die->child != NULL)
16277 {
16278 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16279 struct die_info *child_die;
16280 size_t n_entries = 0, size;
16281 struct common_block *common_block;
16282 struct symbol *sym;
16283
16284 for (child_die = die->child;
16285 child_die && child_die->tag;
16286 child_die = child_die->sibling)
16287 ++n_entries;
16288
16289 size = (sizeof (struct common_block)
16290 + (n_entries - 1) * sizeof (struct symbol *));
16291 common_block
16292 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16293 size);
16294 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16295 common_block->n_entries = 0;
16296
16297 for (child_die = die->child;
16298 child_die && child_die->tag;
16299 child_die = child_die->sibling)
16300 {
16301 /* Create the symbol in the DW_TAG_common_block block in the current
16302 symbol scope. */
16303 sym = new_symbol (child_die, NULL, cu);
16304 if (sym != NULL)
16305 {
16306 struct attribute *member_loc;
16307
16308 common_block->contents[common_block->n_entries++] = sym;
16309
16310 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16311 cu);
16312 if (member_loc)
16313 {
16314 /* GDB has handled this for a long time, but it is
16315 not specified by DWARF. It seems to have been
16316 emitted by gfortran at least as recently as:
16317 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16318 complaint (_("Variable in common block has "
16319 "DW_AT_data_member_location "
16320 "- DIE at %s [in module %s]"),
16321 sect_offset_str (child_die->sect_off),
16322 objfile_name (objfile));
16323
16324 if (member_loc->form_is_section_offset ())
16325 dwarf2_complex_location_expr_complaint ();
16326 else if (member_loc->form_is_constant ()
16327 || member_loc->form_is_block ())
16328 {
16329 if (attr != nullptr)
16330 mark_common_block_symbol_computed (sym, die, attr,
16331 member_loc, cu);
16332 }
16333 else
16334 dwarf2_complex_location_expr_complaint ();
16335 }
16336 }
16337 }
16338
16339 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16340 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16341 }
16342 }
16343
16344 /* Create a type for a C++ namespace. */
16345
16346 static struct type *
16347 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16348 {
16349 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16350 const char *previous_prefix, *name;
16351 int is_anonymous;
16352 struct type *type;
16353
16354 /* For extensions, reuse the type of the original namespace. */
16355 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16356 {
16357 struct die_info *ext_die;
16358 struct dwarf2_cu *ext_cu = cu;
16359
16360 ext_die = dwarf2_extension (die, &ext_cu);
16361 type = read_type_die (ext_die, ext_cu);
16362
16363 /* EXT_CU may not be the same as CU.
16364 Ensure TYPE is recorded with CU in die_type_hash. */
16365 return set_die_type (die, type, cu);
16366 }
16367
16368 name = namespace_name (die, &is_anonymous, cu);
16369
16370 /* Now build the name of the current namespace. */
16371
16372 previous_prefix = determine_prefix (die, cu);
16373 if (previous_prefix[0] != '\0')
16374 name = typename_concat (&objfile->objfile_obstack,
16375 previous_prefix, name, 0, cu);
16376
16377 /* Create the type. */
16378 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16379
16380 return set_die_type (die, type, cu);
16381 }
16382
16383 /* Read a namespace scope. */
16384
16385 static void
16386 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16387 {
16388 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16389 int is_anonymous;
16390
16391 /* Add a symbol associated to this if we haven't seen the namespace
16392 before. Also, add a using directive if it's an anonymous
16393 namespace. */
16394
16395 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16396 {
16397 struct type *type;
16398
16399 type = read_type_die (die, cu);
16400 new_symbol (die, type, cu);
16401
16402 namespace_name (die, &is_anonymous, cu);
16403 if (is_anonymous)
16404 {
16405 const char *previous_prefix = determine_prefix (die, cu);
16406
16407 std::vector<const char *> excludes;
16408 add_using_directive (using_directives (cu),
16409 previous_prefix, TYPE_NAME (type), NULL,
16410 NULL, excludes, 0, &objfile->objfile_obstack);
16411 }
16412 }
16413
16414 if (die->child != NULL)
16415 {
16416 struct die_info *child_die = die->child;
16417
16418 while (child_die && child_die->tag)
16419 {
16420 process_die (child_die, cu);
16421 child_die = child_die->sibling;
16422 }
16423 }
16424 }
16425
16426 /* Read a Fortran module as type. This DIE can be only a declaration used for
16427 imported module. Still we need that type as local Fortran "use ... only"
16428 declaration imports depend on the created type in determine_prefix. */
16429
16430 static struct type *
16431 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16432 {
16433 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16434 const char *module_name;
16435 struct type *type;
16436
16437 module_name = dwarf2_name (die, cu);
16438 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16439
16440 return set_die_type (die, type, cu);
16441 }
16442
16443 /* Read a Fortran module. */
16444
16445 static void
16446 read_module (struct die_info *die, struct dwarf2_cu *cu)
16447 {
16448 struct die_info *child_die = die->child;
16449 struct type *type;
16450
16451 type = read_type_die (die, cu);
16452 new_symbol (die, type, cu);
16453
16454 while (child_die && child_die->tag)
16455 {
16456 process_die (child_die, cu);
16457 child_die = child_die->sibling;
16458 }
16459 }
16460
16461 /* Return the name of the namespace represented by DIE. Set
16462 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16463 namespace. */
16464
16465 static const char *
16466 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16467 {
16468 struct die_info *current_die;
16469 const char *name = NULL;
16470
16471 /* Loop through the extensions until we find a name. */
16472
16473 for (current_die = die;
16474 current_die != NULL;
16475 current_die = dwarf2_extension (die, &cu))
16476 {
16477 /* We don't use dwarf2_name here so that we can detect the absence
16478 of a name -> anonymous namespace. */
16479 name = dwarf2_string_attr (die, DW_AT_name, cu);
16480
16481 if (name != NULL)
16482 break;
16483 }
16484
16485 /* Is it an anonymous namespace? */
16486
16487 *is_anonymous = (name == NULL);
16488 if (*is_anonymous)
16489 name = CP_ANONYMOUS_NAMESPACE_STR;
16490
16491 return name;
16492 }
16493
16494 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16495 the user defined type vector. */
16496
16497 static struct type *
16498 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16499 {
16500 struct gdbarch *gdbarch
16501 = cu->per_cu->dwarf2_per_objfile->objfile->arch ();
16502 struct comp_unit_head *cu_header = &cu->header;
16503 struct type *type;
16504 struct attribute *attr_byte_size;
16505 struct attribute *attr_address_class;
16506 int byte_size, addr_class;
16507 struct type *target_type;
16508
16509 target_type = die_type (die, cu);
16510
16511 /* The die_type call above may have already set the type for this DIE. */
16512 type = get_die_type (die, cu);
16513 if (type)
16514 return type;
16515
16516 type = lookup_pointer_type (target_type);
16517
16518 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16519 if (attr_byte_size)
16520 byte_size = DW_UNSND (attr_byte_size);
16521 else
16522 byte_size = cu_header->addr_size;
16523
16524 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16525 if (attr_address_class)
16526 addr_class = DW_UNSND (attr_address_class);
16527 else
16528 addr_class = DW_ADDR_none;
16529
16530 ULONGEST alignment = get_alignment (cu, die);
16531
16532 /* If the pointer size, alignment, or address class is different
16533 than the default, create a type variant marked as such and set
16534 the length accordingly. */
16535 if (TYPE_LENGTH (type) != byte_size
16536 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16537 && alignment != TYPE_RAW_ALIGN (type))
16538 || addr_class != DW_ADDR_none)
16539 {
16540 if (gdbarch_address_class_type_flags_p (gdbarch))
16541 {
16542 int type_flags;
16543
16544 type_flags = gdbarch_address_class_type_flags
16545 (gdbarch, byte_size, addr_class);
16546 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16547 == 0);
16548 type = make_type_with_address_space (type, type_flags);
16549 }
16550 else if (TYPE_LENGTH (type) != byte_size)
16551 {
16552 complaint (_("invalid pointer size %d"), byte_size);
16553 }
16554 else if (TYPE_RAW_ALIGN (type) != alignment)
16555 {
16556 complaint (_("Invalid DW_AT_alignment"
16557 " - DIE at %s [in module %s]"),
16558 sect_offset_str (die->sect_off),
16559 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16560 }
16561 else
16562 {
16563 /* Should we also complain about unhandled address classes? */
16564 }
16565 }
16566
16567 TYPE_LENGTH (type) = byte_size;
16568 set_type_align (type, alignment);
16569 return set_die_type (die, type, cu);
16570 }
16571
16572 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16573 the user defined type vector. */
16574
16575 static struct type *
16576 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16577 {
16578 struct type *type;
16579 struct type *to_type;
16580 struct type *domain;
16581
16582 to_type = die_type (die, cu);
16583 domain = die_containing_type (die, cu);
16584
16585 /* The calls above may have already set the type for this DIE. */
16586 type = get_die_type (die, cu);
16587 if (type)
16588 return type;
16589
16590 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16591 type = lookup_methodptr_type (to_type);
16592 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16593 {
16594 struct type *new_type
16595 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16596
16597 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16598 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16599 TYPE_VARARGS (to_type));
16600 type = lookup_methodptr_type (new_type);
16601 }
16602 else
16603 type = lookup_memberptr_type (to_type, domain);
16604
16605 return set_die_type (die, type, cu);
16606 }
16607
16608 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16609 the user defined type vector. */
16610
16611 static struct type *
16612 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16613 enum type_code refcode)
16614 {
16615 struct comp_unit_head *cu_header = &cu->header;
16616 struct type *type, *target_type;
16617 struct attribute *attr;
16618
16619 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16620
16621 target_type = die_type (die, cu);
16622
16623 /* The die_type call above may have already set the type for this DIE. */
16624 type = get_die_type (die, cu);
16625 if (type)
16626 return type;
16627
16628 type = lookup_reference_type (target_type, refcode);
16629 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16630 if (attr != nullptr)
16631 {
16632 TYPE_LENGTH (type) = DW_UNSND (attr);
16633 }
16634 else
16635 {
16636 TYPE_LENGTH (type) = cu_header->addr_size;
16637 }
16638 maybe_set_alignment (cu, die, type);
16639 return set_die_type (die, type, cu);
16640 }
16641
16642 /* Add the given cv-qualifiers to the element type of the array. GCC
16643 outputs DWARF type qualifiers that apply to an array, not the
16644 element type. But GDB relies on the array element type to carry
16645 the cv-qualifiers. This mimics section 6.7.3 of the C99
16646 specification. */
16647
16648 static struct type *
16649 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16650 struct type *base_type, int cnst, int voltl)
16651 {
16652 struct type *el_type, *inner_array;
16653
16654 base_type = copy_type (base_type);
16655 inner_array = base_type;
16656
16657 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16658 {
16659 TYPE_TARGET_TYPE (inner_array) =
16660 copy_type (TYPE_TARGET_TYPE (inner_array));
16661 inner_array = TYPE_TARGET_TYPE (inner_array);
16662 }
16663
16664 el_type = TYPE_TARGET_TYPE (inner_array);
16665 cnst |= TYPE_CONST (el_type);
16666 voltl |= TYPE_VOLATILE (el_type);
16667 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16668
16669 return set_die_type (die, base_type, cu);
16670 }
16671
16672 static struct type *
16673 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16674 {
16675 struct type *base_type, *cv_type;
16676
16677 base_type = die_type (die, cu);
16678
16679 /* The die_type call above may have already set the type for this DIE. */
16680 cv_type = get_die_type (die, cu);
16681 if (cv_type)
16682 return cv_type;
16683
16684 /* In case the const qualifier is applied to an array type, the element type
16685 is so qualified, not the array type (section 6.7.3 of C99). */
16686 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16687 return add_array_cv_type (die, cu, base_type, 1, 0);
16688
16689 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16690 return set_die_type (die, cv_type, cu);
16691 }
16692
16693 static struct type *
16694 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16695 {
16696 struct type *base_type, *cv_type;
16697
16698 base_type = die_type (die, cu);
16699
16700 /* The die_type call above may have already set the type for this DIE. */
16701 cv_type = get_die_type (die, cu);
16702 if (cv_type)
16703 return cv_type;
16704
16705 /* In case the volatile qualifier is applied to an array type, the
16706 element type is so qualified, not the array type (section 6.7.3
16707 of C99). */
16708 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16709 return add_array_cv_type (die, cu, base_type, 0, 1);
16710
16711 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16712 return set_die_type (die, cv_type, cu);
16713 }
16714
16715 /* Handle DW_TAG_restrict_type. */
16716
16717 static struct type *
16718 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16719 {
16720 struct type *base_type, *cv_type;
16721
16722 base_type = die_type (die, cu);
16723
16724 /* The die_type call above may have already set the type for this DIE. */
16725 cv_type = get_die_type (die, cu);
16726 if (cv_type)
16727 return cv_type;
16728
16729 cv_type = make_restrict_type (base_type);
16730 return set_die_type (die, cv_type, cu);
16731 }
16732
16733 /* Handle DW_TAG_atomic_type. */
16734
16735 static struct type *
16736 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16737 {
16738 struct type *base_type, *cv_type;
16739
16740 base_type = die_type (die, cu);
16741
16742 /* The die_type call above may have already set the type for this DIE. */
16743 cv_type = get_die_type (die, cu);
16744 if (cv_type)
16745 return cv_type;
16746
16747 cv_type = make_atomic_type (base_type);
16748 return set_die_type (die, cv_type, cu);
16749 }
16750
16751 /* Extract all information from a DW_TAG_string_type DIE and add to
16752 the user defined type vector. It isn't really a user defined type,
16753 but it behaves like one, with other DIE's using an AT_user_def_type
16754 attribute to reference it. */
16755
16756 static struct type *
16757 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16758 {
16759 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16760 struct gdbarch *gdbarch = objfile->arch ();
16761 struct type *type, *range_type, *index_type, *char_type;
16762 struct attribute *attr;
16763 struct dynamic_prop prop;
16764 bool length_is_constant = true;
16765 LONGEST length;
16766
16767 /* There are a couple of places where bit sizes might be made use of
16768 when parsing a DW_TAG_string_type, however, no producer that we know
16769 of make use of these. Handling bit sizes that are a multiple of the
16770 byte size is easy enough, but what about other bit sizes? Lets deal
16771 with that problem when we have to. Warn about these attributes being
16772 unsupported, then parse the type and ignore them like we always
16773 have. */
16774 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16775 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16776 {
16777 static bool warning_printed = false;
16778 if (!warning_printed)
16779 {
16780 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16781 "currently supported on DW_TAG_string_type."));
16782 warning_printed = true;
16783 }
16784 }
16785
16786 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16787 if (attr != nullptr && !attr->form_is_constant ())
16788 {
16789 /* The string length describes the location at which the length of
16790 the string can be found. The size of the length field can be
16791 specified with one of the attributes below. */
16792 struct type *prop_type;
16793 struct attribute *len
16794 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16795 if (len == nullptr)
16796 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16797 if (len != nullptr && len->form_is_constant ())
16798 {
16799 /* Pass 0 as the default as we know this attribute is constant
16800 and the default value will not be returned. */
16801 LONGEST sz = len->constant_value (0);
16802 prop_type = cu->per_cu->int_type (sz, true);
16803 }
16804 else
16805 {
16806 /* If the size is not specified then we assume it is the size of
16807 an address on this target. */
16808 prop_type = cu->per_cu->addr_sized_int_type (true);
16809 }
16810
16811 /* Convert the attribute into a dynamic property. */
16812 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16813 length = 1;
16814 else
16815 length_is_constant = false;
16816 }
16817 else if (attr != nullptr)
16818 {
16819 /* This DW_AT_string_length just contains the length with no
16820 indirection. There's no need to create a dynamic property in this
16821 case. Pass 0 for the default value as we know it will not be
16822 returned in this case. */
16823 length = attr->constant_value (0);
16824 }
16825 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16826 {
16827 /* We don't currently support non-constant byte sizes for strings. */
16828 length = attr->constant_value (1);
16829 }
16830 else
16831 {
16832 /* Use 1 as a fallback length if we have nothing else. */
16833 length = 1;
16834 }
16835
16836 index_type = objfile_type (objfile)->builtin_int;
16837 if (length_is_constant)
16838 range_type = create_static_range_type (NULL, index_type, 1, length);
16839 else
16840 {
16841 struct dynamic_prop low_bound;
16842
16843 low_bound.kind = PROP_CONST;
16844 low_bound.data.const_val = 1;
16845 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16846 }
16847 char_type = language_string_char_type (cu->language_defn, gdbarch);
16848 type = create_string_type (NULL, char_type, range_type);
16849
16850 return set_die_type (die, type, cu);
16851 }
16852
16853 /* Assuming that DIE corresponds to a function, returns nonzero
16854 if the function is prototyped. */
16855
16856 static int
16857 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16858 {
16859 struct attribute *attr;
16860
16861 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16862 if (attr && (DW_UNSND (attr) != 0))
16863 return 1;
16864
16865 /* The DWARF standard implies that the DW_AT_prototyped attribute
16866 is only meaningful for C, but the concept also extends to other
16867 languages that allow unprototyped functions (Eg: Objective C).
16868 For all other languages, assume that functions are always
16869 prototyped. */
16870 if (cu->language != language_c
16871 && cu->language != language_objc
16872 && cu->language != language_opencl)
16873 return 1;
16874
16875 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16876 prototyped and unprototyped functions; default to prototyped,
16877 since that is more common in modern code (and RealView warns
16878 about unprototyped functions). */
16879 if (producer_is_realview (cu->producer))
16880 return 1;
16881
16882 return 0;
16883 }
16884
16885 /* Handle DIES due to C code like:
16886
16887 struct foo
16888 {
16889 int (*funcp)(int a, long l);
16890 int b;
16891 };
16892
16893 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16894
16895 static struct type *
16896 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16897 {
16898 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16899 struct type *type; /* Type that this function returns. */
16900 struct type *ftype; /* Function that returns above type. */
16901 struct attribute *attr;
16902
16903 type = die_type (die, cu);
16904
16905 /* The die_type call above may have already set the type for this DIE. */
16906 ftype = get_die_type (die, cu);
16907 if (ftype)
16908 return ftype;
16909
16910 ftype = lookup_function_type (type);
16911
16912 if (prototyped_function_p (die, cu))
16913 TYPE_PROTOTYPED (ftype) = 1;
16914
16915 /* Store the calling convention in the type if it's available in
16916 the subroutine die. Otherwise set the calling convention to
16917 the default value DW_CC_normal. */
16918 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16919 if (attr != nullptr
16920 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16921 TYPE_CALLING_CONVENTION (ftype)
16922 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16923 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16924 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16925 else
16926 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16927
16928 /* Record whether the function returns normally to its caller or not
16929 if the DWARF producer set that information. */
16930 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16931 if (attr && (DW_UNSND (attr) != 0))
16932 TYPE_NO_RETURN (ftype) = 1;
16933
16934 /* We need to add the subroutine type to the die immediately so
16935 we don't infinitely recurse when dealing with parameters
16936 declared as the same subroutine type. */
16937 set_die_type (die, ftype, cu);
16938
16939 if (die->child != NULL)
16940 {
16941 struct type *void_type = objfile_type (objfile)->builtin_void;
16942 struct die_info *child_die;
16943 int nparams, iparams;
16944
16945 /* Count the number of parameters.
16946 FIXME: GDB currently ignores vararg functions, but knows about
16947 vararg member functions. */
16948 nparams = 0;
16949 child_die = die->child;
16950 while (child_die && child_die->tag)
16951 {
16952 if (child_die->tag == DW_TAG_formal_parameter)
16953 nparams++;
16954 else if (child_die->tag == DW_TAG_unspecified_parameters)
16955 TYPE_VARARGS (ftype) = 1;
16956 child_die = child_die->sibling;
16957 }
16958
16959 /* Allocate storage for parameters and fill them in. */
16960 TYPE_NFIELDS (ftype) = nparams;
16961 TYPE_FIELDS (ftype) = (struct field *)
16962 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16963
16964 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16965 even if we error out during the parameters reading below. */
16966 for (iparams = 0; iparams < nparams; iparams++)
16967 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16968
16969 iparams = 0;
16970 child_die = die->child;
16971 while (child_die && child_die->tag)
16972 {
16973 if (child_die->tag == DW_TAG_formal_parameter)
16974 {
16975 struct type *arg_type;
16976
16977 /* DWARF version 2 has no clean way to discern C++
16978 static and non-static member functions. G++ helps
16979 GDB by marking the first parameter for non-static
16980 member functions (which is the this pointer) as
16981 artificial. We pass this information to
16982 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16983
16984 DWARF version 3 added DW_AT_object_pointer, which GCC
16985 4.5 does not yet generate. */
16986 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16987 if (attr != nullptr)
16988 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16989 else
16990 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16991 arg_type = die_type (child_die, cu);
16992
16993 /* RealView does not mark THIS as const, which the testsuite
16994 expects. GCC marks THIS as const in method definitions,
16995 but not in the class specifications (GCC PR 43053). */
16996 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16997 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16998 {
16999 int is_this = 0;
17000 struct dwarf2_cu *arg_cu = cu;
17001 const char *name = dwarf2_name (child_die, cu);
17002
17003 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17004 if (attr != nullptr)
17005 {
17006 /* If the compiler emits this, use it. */
17007 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17008 is_this = 1;
17009 }
17010 else if (name && strcmp (name, "this") == 0)
17011 /* Function definitions will have the argument names. */
17012 is_this = 1;
17013 else if (name == NULL && iparams == 0)
17014 /* Declarations may not have the names, so like
17015 elsewhere in GDB, assume an artificial first
17016 argument is "this". */
17017 is_this = 1;
17018
17019 if (is_this)
17020 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17021 arg_type, 0);
17022 }
17023
17024 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17025 iparams++;
17026 }
17027 child_die = child_die->sibling;
17028 }
17029 }
17030
17031 return ftype;
17032 }
17033
17034 static struct type *
17035 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17036 {
17037 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17038 const char *name = NULL;
17039 struct type *this_type, *target_type;
17040
17041 name = dwarf2_full_name (NULL, die, cu);
17042 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17043 TYPE_TARGET_STUB (this_type) = 1;
17044 set_die_type (die, this_type, cu);
17045 target_type = die_type (die, cu);
17046 if (target_type != this_type)
17047 TYPE_TARGET_TYPE (this_type) = target_type;
17048 else
17049 {
17050 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17051 spec and cause infinite loops in GDB. */
17052 complaint (_("Self-referential DW_TAG_typedef "
17053 "- DIE at %s [in module %s]"),
17054 sect_offset_str (die->sect_off), objfile_name (objfile));
17055 TYPE_TARGET_TYPE (this_type) = NULL;
17056 }
17057 if (name == NULL)
17058 {
17059 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17060 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17061 Handle these by just returning the target type, rather than
17062 constructing an anonymous typedef type and trying to handle this
17063 elsewhere. */
17064 set_die_type (die, target_type, cu);
17065 return target_type;
17066 }
17067 return this_type;
17068 }
17069
17070 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17071 (which may be different from NAME) to the architecture back-end to allow
17072 it to guess the correct format if necessary. */
17073
17074 static struct type *
17075 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17076 const char *name_hint, enum bfd_endian byte_order)
17077 {
17078 struct gdbarch *gdbarch = objfile->arch ();
17079 const struct floatformat **format;
17080 struct type *type;
17081
17082 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17083 if (format)
17084 type = init_float_type (objfile, bits, name, format, byte_order);
17085 else
17086 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17087
17088 return type;
17089 }
17090
17091 /* Allocate an integer type of size BITS and name NAME. */
17092
17093 static struct type *
17094 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17095 int bits, int unsigned_p, const char *name)
17096 {
17097 struct type *type;
17098
17099 /* Versions of Intel's C Compiler generate an integer type called "void"
17100 instead of using DW_TAG_unspecified_type. This has been seen on
17101 at least versions 14, 17, and 18. */
17102 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17103 && strcmp (name, "void") == 0)
17104 type = objfile_type (objfile)->builtin_void;
17105 else
17106 type = init_integer_type (objfile, bits, unsigned_p, name);
17107
17108 return type;
17109 }
17110
17111 /* Initialise and return a floating point type of size BITS suitable for
17112 use as a component of a complex number. The NAME_HINT is passed through
17113 when initialising the floating point type and is the name of the complex
17114 type.
17115
17116 As DWARF doesn't currently provide an explicit name for the components
17117 of a complex number, but it can be helpful to have these components
17118 named, we try to select a suitable name based on the size of the
17119 component. */
17120 static struct type *
17121 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17122 struct objfile *objfile,
17123 int bits, const char *name_hint,
17124 enum bfd_endian byte_order)
17125 {
17126 gdbarch *gdbarch = objfile->arch ();
17127 struct type *tt = nullptr;
17128
17129 /* Try to find a suitable floating point builtin type of size BITS.
17130 We're going to use the name of this type as the name for the complex
17131 target type that we are about to create. */
17132 switch (cu->language)
17133 {
17134 case language_fortran:
17135 switch (bits)
17136 {
17137 case 32:
17138 tt = builtin_f_type (gdbarch)->builtin_real;
17139 break;
17140 case 64:
17141 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17142 break;
17143 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17144 case 128:
17145 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17146 break;
17147 }
17148 break;
17149 default:
17150 switch (bits)
17151 {
17152 case 32:
17153 tt = builtin_type (gdbarch)->builtin_float;
17154 break;
17155 case 64:
17156 tt = builtin_type (gdbarch)->builtin_double;
17157 break;
17158 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17159 case 128:
17160 tt = builtin_type (gdbarch)->builtin_long_double;
17161 break;
17162 }
17163 break;
17164 }
17165
17166 /* If the type we found doesn't match the size we were looking for, then
17167 pretend we didn't find a type at all, the complex target type we
17168 create will then be nameless. */
17169 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17170 tt = nullptr;
17171
17172 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17173 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17174 }
17175
17176 /* Find a representation of a given base type and install
17177 it in the TYPE field of the die. */
17178
17179 static struct type *
17180 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17181 {
17182 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17183 struct type *type;
17184 struct attribute *attr;
17185 int encoding = 0, bits = 0;
17186 const char *name;
17187 gdbarch *arch;
17188
17189 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17190 if (attr != nullptr)
17191 encoding = DW_UNSND (attr);
17192 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17193 if (attr != nullptr)
17194 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17195 name = dwarf2_name (die, cu);
17196 if (!name)
17197 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17198
17199 arch = objfile->arch ();
17200 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17201
17202 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17203 if (attr)
17204 {
17205 int endianity = DW_UNSND (attr);
17206
17207 switch (endianity)
17208 {
17209 case DW_END_big:
17210 byte_order = BFD_ENDIAN_BIG;
17211 break;
17212 case DW_END_little:
17213 byte_order = BFD_ENDIAN_LITTLE;
17214 break;
17215 default:
17216 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17217 break;
17218 }
17219 }
17220
17221 switch (encoding)
17222 {
17223 case DW_ATE_address:
17224 /* Turn DW_ATE_address into a void * pointer. */
17225 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17226 type = init_pointer_type (objfile, bits, name, type);
17227 break;
17228 case DW_ATE_boolean:
17229 type = init_boolean_type (objfile, bits, 1, name);
17230 break;
17231 case DW_ATE_complex_float:
17232 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17233 byte_order);
17234 if (TYPE_CODE (type) == TYPE_CODE_ERROR)
17235 {
17236 if (name == nullptr)
17237 {
17238 struct obstack *obstack
17239 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17240 name = obconcat (obstack, "_Complex ", TYPE_NAME (type),
17241 nullptr);
17242 }
17243 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17244 }
17245 else
17246 type = init_complex_type (name, type);
17247 break;
17248 case DW_ATE_decimal_float:
17249 type = init_decfloat_type (objfile, bits, name);
17250 break;
17251 case DW_ATE_float:
17252 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17253 break;
17254 case DW_ATE_signed:
17255 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17256 break;
17257 case DW_ATE_unsigned:
17258 if (cu->language == language_fortran
17259 && name
17260 && startswith (name, "character("))
17261 type = init_character_type (objfile, bits, 1, name);
17262 else
17263 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17264 break;
17265 case DW_ATE_signed_char:
17266 if (cu->language == language_ada || cu->language == language_m2
17267 || cu->language == language_pascal
17268 || cu->language == language_fortran)
17269 type = init_character_type (objfile, bits, 0, name);
17270 else
17271 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17272 break;
17273 case DW_ATE_unsigned_char:
17274 if (cu->language == language_ada || cu->language == language_m2
17275 || cu->language == language_pascal
17276 || cu->language == language_fortran
17277 || cu->language == language_rust)
17278 type = init_character_type (objfile, bits, 1, name);
17279 else
17280 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17281 break;
17282 case DW_ATE_UTF:
17283 {
17284 if (bits == 16)
17285 type = builtin_type (arch)->builtin_char16;
17286 else if (bits == 32)
17287 type = builtin_type (arch)->builtin_char32;
17288 else
17289 {
17290 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17291 bits);
17292 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17293 }
17294 return set_die_type (die, type, cu);
17295 }
17296 break;
17297
17298 default:
17299 complaint (_("unsupported DW_AT_encoding: '%s'"),
17300 dwarf_type_encoding_name (encoding));
17301 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17302 break;
17303 }
17304
17305 if (name && strcmp (name, "char") == 0)
17306 TYPE_NOSIGN (type) = 1;
17307
17308 maybe_set_alignment (cu, die, type);
17309
17310 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17311
17312 return set_die_type (die, type, cu);
17313 }
17314
17315 /* Parse dwarf attribute if it's a block, reference or constant and put the
17316 resulting value of the attribute into struct bound_prop.
17317 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17318
17319 static int
17320 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17321 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17322 struct type *default_type)
17323 {
17324 struct dwarf2_property_baton *baton;
17325 struct obstack *obstack
17326 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17327
17328 gdb_assert (default_type != NULL);
17329
17330 if (attr == NULL || prop == NULL)
17331 return 0;
17332
17333 if (attr->form_is_block ())
17334 {
17335 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17336 baton->property_type = default_type;
17337 baton->locexpr.per_cu = cu->per_cu;
17338 baton->locexpr.size = DW_BLOCK (attr)->size;
17339 baton->locexpr.data = DW_BLOCK (attr)->data;
17340 switch (attr->name)
17341 {
17342 case DW_AT_string_length:
17343 baton->locexpr.is_reference = true;
17344 break;
17345 default:
17346 baton->locexpr.is_reference = false;
17347 break;
17348 }
17349 prop->data.baton = baton;
17350 prop->kind = PROP_LOCEXPR;
17351 gdb_assert (prop->data.baton != NULL);
17352 }
17353 else if (attr->form_is_ref ())
17354 {
17355 struct dwarf2_cu *target_cu = cu;
17356 struct die_info *target_die;
17357 struct attribute *target_attr;
17358
17359 target_die = follow_die_ref (die, attr, &target_cu);
17360 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17361 if (target_attr == NULL)
17362 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17363 target_cu);
17364 if (target_attr == NULL)
17365 return 0;
17366
17367 switch (target_attr->name)
17368 {
17369 case DW_AT_location:
17370 if (target_attr->form_is_section_offset ())
17371 {
17372 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17373 baton->property_type = die_type (target_die, target_cu);
17374 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17375 prop->data.baton = baton;
17376 prop->kind = PROP_LOCLIST;
17377 gdb_assert (prop->data.baton != NULL);
17378 }
17379 else if (target_attr->form_is_block ())
17380 {
17381 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17382 baton->property_type = die_type (target_die, target_cu);
17383 baton->locexpr.per_cu = cu->per_cu;
17384 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17385 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17386 baton->locexpr.is_reference = true;
17387 prop->data.baton = baton;
17388 prop->kind = PROP_LOCEXPR;
17389 gdb_assert (prop->data.baton != NULL);
17390 }
17391 else
17392 {
17393 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17394 "dynamic property");
17395 return 0;
17396 }
17397 break;
17398 case DW_AT_data_member_location:
17399 {
17400 LONGEST offset;
17401
17402 if (!handle_data_member_location (target_die, target_cu,
17403 &offset))
17404 return 0;
17405
17406 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17407 baton->property_type = read_type_die (target_die->parent,
17408 target_cu);
17409 baton->offset_info.offset = offset;
17410 baton->offset_info.type = die_type (target_die, target_cu);
17411 prop->data.baton = baton;
17412 prop->kind = PROP_ADDR_OFFSET;
17413 break;
17414 }
17415 }
17416 }
17417 else if (attr->form_is_constant ())
17418 {
17419 prop->data.const_val = attr->constant_value (0);
17420 prop->kind = PROP_CONST;
17421 }
17422 else
17423 {
17424 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17425 dwarf2_name (die, cu));
17426 return 0;
17427 }
17428
17429 return 1;
17430 }
17431
17432 /* See read.h. */
17433
17434 struct type *
17435 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17436 {
17437 struct objfile *objfile = dwarf2_per_objfile->objfile;
17438 struct type *int_type;
17439
17440 /* Helper macro to examine the various builtin types. */
17441 #define TRY_TYPE(F) \
17442 int_type = (unsigned_p \
17443 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17444 : objfile_type (objfile)->builtin_ ## F); \
17445 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17446 return int_type
17447
17448 TRY_TYPE (char);
17449 TRY_TYPE (short);
17450 TRY_TYPE (int);
17451 TRY_TYPE (long);
17452 TRY_TYPE (long_long);
17453
17454 #undef TRY_TYPE
17455
17456 gdb_assert_not_reached ("unable to find suitable integer type");
17457 }
17458
17459 /* See read.h. */
17460
17461 struct type *
17462 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17463 {
17464 int addr_size = this->addr_size ();
17465 return int_type (addr_size, unsigned_p);
17466 }
17467
17468 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17469 present (which is valid) then compute the default type based on the
17470 compilation units address size. */
17471
17472 static struct type *
17473 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17474 {
17475 struct type *index_type = die_type (die, cu);
17476
17477 /* Dwarf-2 specifications explicitly allows to create subrange types
17478 without specifying a base type.
17479 In that case, the base type must be set to the type of
17480 the lower bound, upper bound or count, in that order, if any of these
17481 three attributes references an object that has a type.
17482 If no base type is found, the Dwarf-2 specifications say that
17483 a signed integer type of size equal to the size of an address should
17484 be used.
17485 For the following C code: `extern char gdb_int [];'
17486 GCC produces an empty range DIE.
17487 FIXME: muller/2010-05-28: Possible references to object for low bound,
17488 high bound or count are not yet handled by this code. */
17489 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17490 index_type = cu->per_cu->addr_sized_int_type (false);
17491
17492 return index_type;
17493 }
17494
17495 /* Read the given DW_AT_subrange DIE. */
17496
17497 static struct type *
17498 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17499 {
17500 struct type *base_type, *orig_base_type;
17501 struct type *range_type;
17502 struct attribute *attr;
17503 struct dynamic_prop low, high;
17504 int low_default_is_valid;
17505 int high_bound_is_count = 0;
17506 const char *name;
17507 ULONGEST negative_mask;
17508
17509 orig_base_type = read_subrange_index_type (die, cu);
17510
17511 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17512 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17513 creating the range type, but we use the result of check_typedef
17514 when examining properties of the type. */
17515 base_type = check_typedef (orig_base_type);
17516
17517 /* The die_type call above may have already set the type for this DIE. */
17518 range_type = get_die_type (die, cu);
17519 if (range_type)
17520 return range_type;
17521
17522 low.kind = PROP_CONST;
17523 high.kind = PROP_CONST;
17524 high.data.const_val = 0;
17525
17526 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17527 omitting DW_AT_lower_bound. */
17528 switch (cu->language)
17529 {
17530 case language_c:
17531 case language_cplus:
17532 low.data.const_val = 0;
17533 low_default_is_valid = 1;
17534 break;
17535 case language_fortran:
17536 low.data.const_val = 1;
17537 low_default_is_valid = 1;
17538 break;
17539 case language_d:
17540 case language_objc:
17541 case language_rust:
17542 low.data.const_val = 0;
17543 low_default_is_valid = (cu->header.version >= 4);
17544 break;
17545 case language_ada:
17546 case language_m2:
17547 case language_pascal:
17548 low.data.const_val = 1;
17549 low_default_is_valid = (cu->header.version >= 4);
17550 break;
17551 default:
17552 low.data.const_val = 0;
17553 low_default_is_valid = 0;
17554 break;
17555 }
17556
17557 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17558 if (attr != nullptr)
17559 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17560 else if (!low_default_is_valid)
17561 complaint (_("Missing DW_AT_lower_bound "
17562 "- DIE at %s [in module %s]"),
17563 sect_offset_str (die->sect_off),
17564 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17565
17566 struct attribute *attr_ub, *attr_count;
17567 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17568 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17569 {
17570 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17571 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17572 {
17573 /* If bounds are constant do the final calculation here. */
17574 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17575 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17576 else
17577 high_bound_is_count = 1;
17578 }
17579 else
17580 {
17581 if (attr_ub != NULL)
17582 complaint (_("Unresolved DW_AT_upper_bound "
17583 "- DIE at %s [in module %s]"),
17584 sect_offset_str (die->sect_off),
17585 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17586 if (attr_count != NULL)
17587 complaint (_("Unresolved DW_AT_count "
17588 "- DIE at %s [in module %s]"),
17589 sect_offset_str (die->sect_off),
17590 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17591 }
17592 }
17593
17594 LONGEST bias = 0;
17595 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17596 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17597 bias = bias_attr->constant_value (0);
17598
17599 /* Normally, the DWARF producers are expected to use a signed
17600 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17601 But this is unfortunately not always the case, as witnessed
17602 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17603 is used instead. To work around that ambiguity, we treat
17604 the bounds as signed, and thus sign-extend their values, when
17605 the base type is signed. */
17606 negative_mask =
17607 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17608 if (low.kind == PROP_CONST
17609 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17610 low.data.const_val |= negative_mask;
17611 if (high.kind == PROP_CONST
17612 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17613 high.data.const_val |= negative_mask;
17614
17615 /* Check for bit and byte strides. */
17616 struct dynamic_prop byte_stride_prop;
17617 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17618 if (attr_byte_stride != nullptr)
17619 {
17620 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17621 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17622 prop_type);
17623 }
17624
17625 struct dynamic_prop bit_stride_prop;
17626 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17627 if (attr_bit_stride != nullptr)
17628 {
17629 /* It only makes sense to have either a bit or byte stride. */
17630 if (attr_byte_stride != nullptr)
17631 {
17632 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17633 "- DIE at %s [in module %s]"),
17634 sect_offset_str (die->sect_off),
17635 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17636 attr_bit_stride = nullptr;
17637 }
17638 else
17639 {
17640 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17641 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17642 prop_type);
17643 }
17644 }
17645
17646 if (attr_byte_stride != nullptr
17647 || attr_bit_stride != nullptr)
17648 {
17649 bool byte_stride_p = (attr_byte_stride != nullptr);
17650 struct dynamic_prop *stride
17651 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17652
17653 range_type
17654 = create_range_type_with_stride (NULL, orig_base_type, &low,
17655 &high, bias, stride, byte_stride_p);
17656 }
17657 else
17658 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17659
17660 if (high_bound_is_count)
17661 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17662
17663 /* Ada expects an empty array on no boundary attributes. */
17664 if (attr == NULL && cu->language != language_ada)
17665 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17666
17667 name = dwarf2_name (die, cu);
17668 if (name)
17669 TYPE_NAME (range_type) = name;
17670
17671 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17672 if (attr != nullptr)
17673 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17674
17675 maybe_set_alignment (cu, die, range_type);
17676
17677 set_die_type (die, range_type, cu);
17678
17679 /* set_die_type should be already done. */
17680 set_descriptive_type (range_type, die, cu);
17681
17682 return range_type;
17683 }
17684
17685 static struct type *
17686 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17687 {
17688 struct type *type;
17689
17690 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17691 NULL);
17692 TYPE_NAME (type) = dwarf2_name (die, cu);
17693
17694 /* In Ada, an unspecified type is typically used when the description
17695 of the type is deferred to a different unit. When encountering
17696 such a type, we treat it as a stub, and try to resolve it later on,
17697 when needed. */
17698 if (cu->language == language_ada)
17699 TYPE_STUB (type) = 1;
17700
17701 return set_die_type (die, type, cu);
17702 }
17703
17704 /* Read a single die and all its descendents. Set the die's sibling
17705 field to NULL; set other fields in the die correctly, and set all
17706 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17707 location of the info_ptr after reading all of those dies. PARENT
17708 is the parent of the die in question. */
17709
17710 static struct die_info *
17711 read_die_and_children (const struct die_reader_specs *reader,
17712 const gdb_byte *info_ptr,
17713 const gdb_byte **new_info_ptr,
17714 struct die_info *parent)
17715 {
17716 struct die_info *die;
17717 const gdb_byte *cur_ptr;
17718
17719 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17720 if (die == NULL)
17721 {
17722 *new_info_ptr = cur_ptr;
17723 return NULL;
17724 }
17725 store_in_ref_table (die, reader->cu);
17726
17727 if (die->has_children)
17728 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17729 else
17730 {
17731 die->child = NULL;
17732 *new_info_ptr = cur_ptr;
17733 }
17734
17735 die->sibling = NULL;
17736 die->parent = parent;
17737 return die;
17738 }
17739
17740 /* Read a die, all of its descendents, and all of its siblings; set
17741 all of the fields of all of the dies correctly. Arguments are as
17742 in read_die_and_children. */
17743
17744 static struct die_info *
17745 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17746 const gdb_byte *info_ptr,
17747 const gdb_byte **new_info_ptr,
17748 struct die_info *parent)
17749 {
17750 struct die_info *first_die, *last_sibling;
17751 const gdb_byte *cur_ptr;
17752
17753 cur_ptr = info_ptr;
17754 first_die = last_sibling = NULL;
17755
17756 while (1)
17757 {
17758 struct die_info *die
17759 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17760
17761 if (die == NULL)
17762 {
17763 *new_info_ptr = cur_ptr;
17764 return first_die;
17765 }
17766
17767 if (!first_die)
17768 first_die = die;
17769 else
17770 last_sibling->sibling = die;
17771
17772 last_sibling = die;
17773 }
17774 }
17775
17776 /* Read a die, all of its descendents, and all of its siblings; set
17777 all of the fields of all of the dies correctly. Arguments are as
17778 in read_die_and_children.
17779 This the main entry point for reading a DIE and all its children. */
17780
17781 static struct die_info *
17782 read_die_and_siblings (const struct die_reader_specs *reader,
17783 const gdb_byte *info_ptr,
17784 const gdb_byte **new_info_ptr,
17785 struct die_info *parent)
17786 {
17787 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17788 new_info_ptr, parent);
17789
17790 if (dwarf_die_debug)
17791 {
17792 fprintf_unfiltered (gdb_stdlog,
17793 "Read die from %s@0x%x of %s:\n",
17794 reader->die_section->get_name (),
17795 (unsigned) (info_ptr - reader->die_section->buffer),
17796 bfd_get_filename (reader->abfd));
17797 dump_die (die, dwarf_die_debug);
17798 }
17799
17800 return die;
17801 }
17802
17803 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17804 attributes.
17805 The caller is responsible for filling in the extra attributes
17806 and updating (*DIEP)->num_attrs.
17807 Set DIEP to point to a newly allocated die with its information,
17808 except for its child, sibling, and parent fields. */
17809
17810 static const gdb_byte *
17811 read_full_die_1 (const struct die_reader_specs *reader,
17812 struct die_info **diep, const gdb_byte *info_ptr,
17813 int num_extra_attrs)
17814 {
17815 unsigned int abbrev_number, bytes_read, i;
17816 struct abbrev_info *abbrev;
17817 struct die_info *die;
17818 struct dwarf2_cu *cu = reader->cu;
17819 bfd *abfd = reader->abfd;
17820
17821 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17822 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17823 info_ptr += bytes_read;
17824 if (!abbrev_number)
17825 {
17826 *diep = NULL;
17827 return info_ptr;
17828 }
17829
17830 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17831 if (!abbrev)
17832 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17833 abbrev_number,
17834 bfd_get_filename (abfd));
17835
17836 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17837 die->sect_off = sect_off;
17838 die->tag = abbrev->tag;
17839 die->abbrev = abbrev_number;
17840 die->has_children = abbrev->has_children;
17841
17842 /* Make the result usable.
17843 The caller needs to update num_attrs after adding the extra
17844 attributes. */
17845 die->num_attrs = abbrev->num_attrs;
17846
17847 std::vector<int> indexes_that_need_reprocess;
17848 for (i = 0; i < abbrev->num_attrs; ++i)
17849 {
17850 bool need_reprocess;
17851 info_ptr =
17852 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17853 info_ptr, &need_reprocess);
17854 if (need_reprocess)
17855 indexes_that_need_reprocess.push_back (i);
17856 }
17857
17858 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
17859 if (attr != nullptr)
17860 cu->str_offsets_base = DW_UNSND (attr);
17861
17862 attr = die->attr (DW_AT_loclists_base);
17863 if (attr != nullptr)
17864 cu->loclist_base = DW_UNSND (attr);
17865
17866 auto maybe_addr_base = die->addr_base ();
17867 if (maybe_addr_base.has_value ())
17868 cu->addr_base = *maybe_addr_base;
17869 for (int index : indexes_that_need_reprocess)
17870 read_attribute_reprocess (reader, &die->attrs[index]);
17871 *diep = die;
17872 return info_ptr;
17873 }
17874
17875 /* Read a die and all its attributes.
17876 Set DIEP to point to a newly allocated die with its information,
17877 except for its child, sibling, and parent fields. */
17878
17879 static const gdb_byte *
17880 read_full_die (const struct die_reader_specs *reader,
17881 struct die_info **diep, const gdb_byte *info_ptr)
17882 {
17883 const gdb_byte *result;
17884
17885 result = read_full_die_1 (reader, diep, info_ptr, 0);
17886
17887 if (dwarf_die_debug)
17888 {
17889 fprintf_unfiltered (gdb_stdlog,
17890 "Read die from %s@0x%x of %s:\n",
17891 reader->die_section->get_name (),
17892 (unsigned) (info_ptr - reader->die_section->buffer),
17893 bfd_get_filename (reader->abfd));
17894 dump_die (*diep, dwarf_die_debug);
17895 }
17896
17897 return result;
17898 }
17899 \f
17900
17901 /* Returns nonzero if TAG represents a type that we might generate a partial
17902 symbol for. */
17903
17904 static int
17905 is_type_tag_for_partial (int tag)
17906 {
17907 switch (tag)
17908 {
17909 #if 0
17910 /* Some types that would be reasonable to generate partial symbols for,
17911 that we don't at present. */
17912 case DW_TAG_array_type:
17913 case DW_TAG_file_type:
17914 case DW_TAG_ptr_to_member_type:
17915 case DW_TAG_set_type:
17916 case DW_TAG_string_type:
17917 case DW_TAG_subroutine_type:
17918 #endif
17919 case DW_TAG_base_type:
17920 case DW_TAG_class_type:
17921 case DW_TAG_interface_type:
17922 case DW_TAG_enumeration_type:
17923 case DW_TAG_structure_type:
17924 case DW_TAG_subrange_type:
17925 case DW_TAG_typedef:
17926 case DW_TAG_union_type:
17927 return 1;
17928 default:
17929 return 0;
17930 }
17931 }
17932
17933 /* Load all DIEs that are interesting for partial symbols into memory. */
17934
17935 static struct partial_die_info *
17936 load_partial_dies (const struct die_reader_specs *reader,
17937 const gdb_byte *info_ptr, int building_psymtab)
17938 {
17939 struct dwarf2_cu *cu = reader->cu;
17940 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17941 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17942 unsigned int bytes_read;
17943 unsigned int load_all = 0;
17944 int nesting_level = 1;
17945
17946 parent_die = NULL;
17947 last_die = NULL;
17948
17949 gdb_assert (cu->per_cu != NULL);
17950 if (cu->per_cu->load_all_dies)
17951 load_all = 1;
17952
17953 cu->partial_dies
17954 = htab_create_alloc_ex (cu->header.length / 12,
17955 partial_die_hash,
17956 partial_die_eq,
17957 NULL,
17958 &cu->comp_unit_obstack,
17959 hashtab_obstack_allocate,
17960 dummy_obstack_deallocate);
17961
17962 while (1)
17963 {
17964 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17965
17966 /* A NULL abbrev means the end of a series of children. */
17967 if (abbrev == NULL)
17968 {
17969 if (--nesting_level == 0)
17970 return first_die;
17971
17972 info_ptr += bytes_read;
17973 last_die = parent_die;
17974 parent_die = parent_die->die_parent;
17975 continue;
17976 }
17977
17978 /* Check for template arguments. We never save these; if
17979 they're seen, we just mark the parent, and go on our way. */
17980 if (parent_die != NULL
17981 && cu->language == language_cplus
17982 && (abbrev->tag == DW_TAG_template_type_param
17983 || abbrev->tag == DW_TAG_template_value_param))
17984 {
17985 parent_die->has_template_arguments = 1;
17986
17987 if (!load_all)
17988 {
17989 /* We don't need a partial DIE for the template argument. */
17990 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17991 continue;
17992 }
17993 }
17994
17995 /* We only recurse into c++ subprograms looking for template arguments.
17996 Skip their other children. */
17997 if (!load_all
17998 && cu->language == language_cplus
17999 && parent_die != NULL
18000 && parent_die->tag == DW_TAG_subprogram)
18001 {
18002 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18003 continue;
18004 }
18005
18006 /* Check whether this DIE is interesting enough to save. Normally
18007 we would not be interested in members here, but there may be
18008 later variables referencing them via DW_AT_specification (for
18009 static members). */
18010 if (!load_all
18011 && !is_type_tag_for_partial (abbrev->tag)
18012 && abbrev->tag != DW_TAG_constant
18013 && abbrev->tag != DW_TAG_enumerator
18014 && abbrev->tag != DW_TAG_subprogram
18015 && abbrev->tag != DW_TAG_inlined_subroutine
18016 && abbrev->tag != DW_TAG_lexical_block
18017 && abbrev->tag != DW_TAG_variable
18018 && abbrev->tag != DW_TAG_namespace
18019 && abbrev->tag != DW_TAG_module
18020 && abbrev->tag != DW_TAG_member
18021 && abbrev->tag != DW_TAG_imported_unit
18022 && abbrev->tag != DW_TAG_imported_declaration)
18023 {
18024 /* Otherwise we skip to the next sibling, if any. */
18025 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18026 continue;
18027 }
18028
18029 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18030 abbrev);
18031
18032 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18033
18034 /* This two-pass algorithm for processing partial symbols has a
18035 high cost in cache pressure. Thus, handle some simple cases
18036 here which cover the majority of C partial symbols. DIEs
18037 which neither have specification tags in them, nor could have
18038 specification tags elsewhere pointing at them, can simply be
18039 processed and discarded.
18040
18041 This segment is also optional; scan_partial_symbols and
18042 add_partial_symbol will handle these DIEs if we chain
18043 them in normally. When compilers which do not emit large
18044 quantities of duplicate debug information are more common,
18045 this code can probably be removed. */
18046
18047 /* Any complete simple types at the top level (pretty much all
18048 of them, for a language without namespaces), can be processed
18049 directly. */
18050 if (parent_die == NULL
18051 && pdi.has_specification == 0
18052 && pdi.is_declaration == 0
18053 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18054 || pdi.tag == DW_TAG_base_type
18055 || pdi.tag == DW_TAG_subrange_type))
18056 {
18057 if (building_psymtab && pdi.name != NULL)
18058 add_psymbol_to_list (pdi.name, false,
18059 VAR_DOMAIN, LOC_TYPEDEF, -1,
18060 psymbol_placement::STATIC,
18061 0, cu->language, objfile);
18062 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18063 continue;
18064 }
18065
18066 /* The exception for DW_TAG_typedef with has_children above is
18067 a workaround of GCC PR debug/47510. In the case of this complaint
18068 type_name_or_error will error on such types later.
18069
18070 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18071 it could not find the child DIEs referenced later, this is checked
18072 above. In correct DWARF DW_TAG_typedef should have no children. */
18073
18074 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18075 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18076 "- DIE at %s [in module %s]"),
18077 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18078
18079 /* If we're at the second level, and we're an enumerator, and
18080 our parent has no specification (meaning possibly lives in a
18081 namespace elsewhere), then we can add the partial symbol now
18082 instead of queueing it. */
18083 if (pdi.tag == DW_TAG_enumerator
18084 && parent_die != NULL
18085 && parent_die->die_parent == NULL
18086 && parent_die->tag == DW_TAG_enumeration_type
18087 && parent_die->has_specification == 0)
18088 {
18089 if (pdi.name == NULL)
18090 complaint (_("malformed enumerator DIE ignored"));
18091 else if (building_psymtab)
18092 add_psymbol_to_list (pdi.name, false,
18093 VAR_DOMAIN, LOC_CONST, -1,
18094 cu->language == language_cplus
18095 ? psymbol_placement::GLOBAL
18096 : psymbol_placement::STATIC,
18097 0, cu->language, objfile);
18098
18099 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18100 continue;
18101 }
18102
18103 struct partial_die_info *part_die
18104 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18105
18106 /* We'll save this DIE so link it in. */
18107 part_die->die_parent = parent_die;
18108 part_die->die_sibling = NULL;
18109 part_die->die_child = NULL;
18110
18111 if (last_die && last_die == parent_die)
18112 last_die->die_child = part_die;
18113 else if (last_die)
18114 last_die->die_sibling = part_die;
18115
18116 last_die = part_die;
18117
18118 if (first_die == NULL)
18119 first_die = part_die;
18120
18121 /* Maybe add the DIE to the hash table. Not all DIEs that we
18122 find interesting need to be in the hash table, because we
18123 also have the parent/sibling/child chains; only those that we
18124 might refer to by offset later during partial symbol reading.
18125
18126 For now this means things that might have be the target of a
18127 DW_AT_specification, DW_AT_abstract_origin, or
18128 DW_AT_extension. DW_AT_extension will refer only to
18129 namespaces; DW_AT_abstract_origin refers to functions (and
18130 many things under the function DIE, but we do not recurse
18131 into function DIEs during partial symbol reading) and
18132 possibly variables as well; DW_AT_specification refers to
18133 declarations. Declarations ought to have the DW_AT_declaration
18134 flag. It happens that GCC forgets to put it in sometimes, but
18135 only for functions, not for types.
18136
18137 Adding more things than necessary to the hash table is harmless
18138 except for the performance cost. Adding too few will result in
18139 wasted time in find_partial_die, when we reread the compilation
18140 unit with load_all_dies set. */
18141
18142 if (load_all
18143 || abbrev->tag == DW_TAG_constant
18144 || abbrev->tag == DW_TAG_subprogram
18145 || abbrev->tag == DW_TAG_variable
18146 || abbrev->tag == DW_TAG_namespace
18147 || part_die->is_declaration)
18148 {
18149 void **slot;
18150
18151 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18152 to_underlying (part_die->sect_off),
18153 INSERT);
18154 *slot = part_die;
18155 }
18156
18157 /* For some DIEs we want to follow their children (if any). For C
18158 we have no reason to follow the children of structures; for other
18159 languages we have to, so that we can get at method physnames
18160 to infer fully qualified class names, for DW_AT_specification,
18161 and for C++ template arguments. For C++, we also look one level
18162 inside functions to find template arguments (if the name of the
18163 function does not already contain the template arguments).
18164
18165 For Ada and Fortran, we need to scan the children of subprograms
18166 and lexical blocks as well because these languages allow the
18167 definition of nested entities that could be interesting for the
18168 debugger, such as nested subprograms for instance. */
18169 if (last_die->has_children
18170 && (load_all
18171 || last_die->tag == DW_TAG_namespace
18172 || last_die->tag == DW_TAG_module
18173 || last_die->tag == DW_TAG_enumeration_type
18174 || (cu->language == language_cplus
18175 && last_die->tag == DW_TAG_subprogram
18176 && (last_die->name == NULL
18177 || strchr (last_die->name, '<') == NULL))
18178 || (cu->language != language_c
18179 && (last_die->tag == DW_TAG_class_type
18180 || last_die->tag == DW_TAG_interface_type
18181 || last_die->tag == DW_TAG_structure_type
18182 || last_die->tag == DW_TAG_union_type))
18183 || ((cu->language == language_ada
18184 || cu->language == language_fortran)
18185 && (last_die->tag == DW_TAG_subprogram
18186 || last_die->tag == DW_TAG_lexical_block))))
18187 {
18188 nesting_level++;
18189 parent_die = last_die;
18190 continue;
18191 }
18192
18193 /* Otherwise we skip to the next sibling, if any. */
18194 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18195
18196 /* Back to the top, do it again. */
18197 }
18198 }
18199
18200 partial_die_info::partial_die_info (sect_offset sect_off_,
18201 struct abbrev_info *abbrev)
18202 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18203 {
18204 }
18205
18206 /* Read a minimal amount of information into the minimal die structure.
18207 INFO_PTR should point just after the initial uleb128 of a DIE. */
18208
18209 const gdb_byte *
18210 partial_die_info::read (const struct die_reader_specs *reader,
18211 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18212 {
18213 struct dwarf2_cu *cu = reader->cu;
18214 struct dwarf2_per_objfile *dwarf2_per_objfile
18215 = cu->per_cu->dwarf2_per_objfile;
18216 unsigned int i;
18217 int has_low_pc_attr = 0;
18218 int has_high_pc_attr = 0;
18219 int high_pc_relative = 0;
18220
18221 for (i = 0; i < abbrev.num_attrs; ++i)
18222 {
18223 attribute attr;
18224 bool need_reprocess;
18225 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18226 info_ptr, &need_reprocess);
18227 /* String and address offsets that need to do the reprocessing have
18228 already been read at this point, so there is no need to wait until
18229 the loop terminates to do the reprocessing. */
18230 if (need_reprocess)
18231 read_attribute_reprocess (reader, &attr);
18232 /* Store the data if it is of an attribute we want to keep in a
18233 partial symbol table. */
18234 switch (attr.name)
18235 {
18236 case DW_AT_name:
18237 switch (tag)
18238 {
18239 case DW_TAG_compile_unit:
18240 case DW_TAG_partial_unit:
18241 case DW_TAG_type_unit:
18242 /* Compilation units have a DW_AT_name that is a filename, not
18243 a source language identifier. */
18244 case DW_TAG_enumeration_type:
18245 case DW_TAG_enumerator:
18246 /* These tags always have simple identifiers already; no need
18247 to canonicalize them. */
18248 name = DW_STRING (&attr);
18249 break;
18250 default:
18251 {
18252 struct objfile *objfile = dwarf2_per_objfile->objfile;
18253
18254 name
18255 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18256 }
18257 break;
18258 }
18259 break;
18260 case DW_AT_linkage_name:
18261 case DW_AT_MIPS_linkage_name:
18262 /* Note that both forms of linkage name might appear. We
18263 assume they will be the same, and we only store the last
18264 one we see. */
18265 linkage_name = DW_STRING (&attr);
18266 break;
18267 case DW_AT_low_pc:
18268 has_low_pc_attr = 1;
18269 lowpc = attr.value_as_address ();
18270 break;
18271 case DW_AT_high_pc:
18272 has_high_pc_attr = 1;
18273 highpc = attr.value_as_address ();
18274 if (cu->header.version >= 4 && attr.form_is_constant ())
18275 high_pc_relative = 1;
18276 break;
18277 case DW_AT_location:
18278 /* Support the .debug_loc offsets. */
18279 if (attr.form_is_block ())
18280 {
18281 d.locdesc = DW_BLOCK (&attr);
18282 }
18283 else if (attr.form_is_section_offset ())
18284 {
18285 dwarf2_complex_location_expr_complaint ();
18286 }
18287 else
18288 {
18289 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18290 "partial symbol information");
18291 }
18292 break;
18293 case DW_AT_external:
18294 is_external = DW_UNSND (&attr);
18295 break;
18296 case DW_AT_declaration:
18297 is_declaration = DW_UNSND (&attr);
18298 break;
18299 case DW_AT_type:
18300 has_type = 1;
18301 break;
18302 case DW_AT_abstract_origin:
18303 case DW_AT_specification:
18304 case DW_AT_extension:
18305 has_specification = 1;
18306 spec_offset = attr.get_ref_die_offset ();
18307 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18308 || cu->per_cu->is_dwz);
18309 break;
18310 case DW_AT_sibling:
18311 /* Ignore absolute siblings, they might point outside of
18312 the current compile unit. */
18313 if (attr.form == DW_FORM_ref_addr)
18314 complaint (_("ignoring absolute DW_AT_sibling"));
18315 else
18316 {
18317 const gdb_byte *buffer = reader->buffer;
18318 sect_offset off = attr.get_ref_die_offset ();
18319 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18320
18321 if (sibling_ptr < info_ptr)
18322 complaint (_("DW_AT_sibling points backwards"));
18323 else if (sibling_ptr > reader->buffer_end)
18324 reader->die_section->overflow_complaint ();
18325 else
18326 sibling = sibling_ptr;
18327 }
18328 break;
18329 case DW_AT_byte_size:
18330 has_byte_size = 1;
18331 break;
18332 case DW_AT_const_value:
18333 has_const_value = 1;
18334 break;
18335 case DW_AT_calling_convention:
18336 /* DWARF doesn't provide a way to identify a program's source-level
18337 entry point. DW_AT_calling_convention attributes are only meant
18338 to describe functions' calling conventions.
18339
18340 However, because it's a necessary piece of information in
18341 Fortran, and before DWARF 4 DW_CC_program was the only
18342 piece of debugging information whose definition refers to
18343 a 'main program' at all, several compilers marked Fortran
18344 main programs with DW_CC_program --- even when those
18345 functions use the standard calling conventions.
18346
18347 Although DWARF now specifies a way to provide this
18348 information, we support this practice for backward
18349 compatibility. */
18350 if (DW_UNSND (&attr) == DW_CC_program
18351 && cu->language == language_fortran)
18352 main_subprogram = 1;
18353 break;
18354 case DW_AT_inline:
18355 if (DW_UNSND (&attr) == DW_INL_inlined
18356 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18357 may_be_inlined = 1;
18358 break;
18359
18360 case DW_AT_import:
18361 if (tag == DW_TAG_imported_unit)
18362 {
18363 d.sect_off = attr.get_ref_die_offset ();
18364 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18365 || cu->per_cu->is_dwz);
18366 }
18367 break;
18368
18369 case DW_AT_main_subprogram:
18370 main_subprogram = DW_UNSND (&attr);
18371 break;
18372
18373 case DW_AT_ranges:
18374 {
18375 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18376 but that requires a full DIE, so instead we just
18377 reimplement it. */
18378 int need_ranges_base = tag != DW_TAG_compile_unit;
18379 unsigned int ranges_offset = (DW_UNSND (&attr)
18380 + (need_ranges_base
18381 ? cu->ranges_base
18382 : 0));
18383
18384 /* Value of the DW_AT_ranges attribute is the offset in the
18385 .debug_ranges section. */
18386 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18387 nullptr))
18388 has_pc_info = 1;
18389 }
18390 break;
18391
18392 default:
18393 break;
18394 }
18395 }
18396
18397 /* For Ada, if both the name and the linkage name appear, we prefer
18398 the latter. This lets "catch exception" work better, regardless
18399 of the order in which the name and linkage name were emitted.
18400 Really, though, this is just a workaround for the fact that gdb
18401 doesn't store both the name and the linkage name. */
18402 if (cu->language == language_ada && linkage_name != nullptr)
18403 name = linkage_name;
18404
18405 if (high_pc_relative)
18406 highpc += lowpc;
18407
18408 if (has_low_pc_attr && has_high_pc_attr)
18409 {
18410 /* When using the GNU linker, .gnu.linkonce. sections are used to
18411 eliminate duplicate copies of functions and vtables and such.
18412 The linker will arbitrarily choose one and discard the others.
18413 The AT_*_pc values for such functions refer to local labels in
18414 these sections. If the section from that file was discarded, the
18415 labels are not in the output, so the relocs get a value of 0.
18416 If this is a discarded function, mark the pc bounds as invalid,
18417 so that GDB will ignore it. */
18418 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18419 {
18420 struct objfile *objfile = dwarf2_per_objfile->objfile;
18421 struct gdbarch *gdbarch = objfile->arch ();
18422
18423 complaint (_("DW_AT_low_pc %s is zero "
18424 "for DIE at %s [in module %s]"),
18425 paddress (gdbarch, lowpc),
18426 sect_offset_str (sect_off),
18427 objfile_name (objfile));
18428 }
18429 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18430 else if (lowpc >= highpc)
18431 {
18432 struct objfile *objfile = dwarf2_per_objfile->objfile;
18433 struct gdbarch *gdbarch = objfile->arch ();
18434
18435 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18436 "for DIE at %s [in module %s]"),
18437 paddress (gdbarch, lowpc),
18438 paddress (gdbarch, highpc),
18439 sect_offset_str (sect_off),
18440 objfile_name (objfile));
18441 }
18442 else
18443 has_pc_info = 1;
18444 }
18445
18446 return info_ptr;
18447 }
18448
18449 /* Find a cached partial DIE at OFFSET in CU. */
18450
18451 struct partial_die_info *
18452 dwarf2_cu::find_partial_die (sect_offset sect_off)
18453 {
18454 struct partial_die_info *lookup_die = NULL;
18455 struct partial_die_info part_die (sect_off);
18456
18457 lookup_die = ((struct partial_die_info *)
18458 htab_find_with_hash (partial_dies, &part_die,
18459 to_underlying (sect_off)));
18460
18461 return lookup_die;
18462 }
18463
18464 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18465 except in the case of .debug_types DIEs which do not reference
18466 outside their CU (they do however referencing other types via
18467 DW_FORM_ref_sig8). */
18468
18469 static const struct cu_partial_die_info
18470 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18471 {
18472 struct dwarf2_per_objfile *dwarf2_per_objfile
18473 = cu->per_cu->dwarf2_per_objfile;
18474 struct objfile *objfile = dwarf2_per_objfile->objfile;
18475 struct dwarf2_per_cu_data *per_cu = NULL;
18476 struct partial_die_info *pd = NULL;
18477
18478 if (offset_in_dwz == cu->per_cu->is_dwz
18479 && cu->header.offset_in_cu_p (sect_off))
18480 {
18481 pd = cu->find_partial_die (sect_off);
18482 if (pd != NULL)
18483 return { cu, pd };
18484 /* We missed recording what we needed.
18485 Load all dies and try again. */
18486 per_cu = cu->per_cu;
18487 }
18488 else
18489 {
18490 /* TUs don't reference other CUs/TUs (except via type signatures). */
18491 if (cu->per_cu->is_debug_types)
18492 {
18493 error (_("Dwarf Error: Type Unit at offset %s contains"
18494 " external reference to offset %s [in module %s].\n"),
18495 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18496 bfd_get_filename (objfile->obfd));
18497 }
18498 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18499 dwarf2_per_objfile);
18500
18501 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18502 load_partial_comp_unit (per_cu);
18503
18504 per_cu->cu->last_used = 0;
18505 pd = per_cu->cu->find_partial_die (sect_off);
18506 }
18507
18508 /* If we didn't find it, and not all dies have been loaded,
18509 load them all and try again. */
18510
18511 if (pd == NULL && per_cu->load_all_dies == 0)
18512 {
18513 per_cu->load_all_dies = 1;
18514
18515 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18516 THIS_CU->cu may already be in use. So we can't just free it and
18517 replace its DIEs with the ones we read in. Instead, we leave those
18518 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18519 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18520 set. */
18521 load_partial_comp_unit (per_cu);
18522
18523 pd = per_cu->cu->find_partial_die (sect_off);
18524 }
18525
18526 if (pd == NULL)
18527 internal_error (__FILE__, __LINE__,
18528 _("could not find partial DIE %s "
18529 "in cache [from module %s]\n"),
18530 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18531 return { per_cu->cu, pd };
18532 }
18533
18534 /* See if we can figure out if the class lives in a namespace. We do
18535 this by looking for a member function; its demangled name will
18536 contain namespace info, if there is any. */
18537
18538 static void
18539 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18540 struct dwarf2_cu *cu)
18541 {
18542 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18543 what template types look like, because the demangler
18544 frequently doesn't give the same name as the debug info. We
18545 could fix this by only using the demangled name to get the
18546 prefix (but see comment in read_structure_type). */
18547
18548 struct partial_die_info *real_pdi;
18549 struct partial_die_info *child_pdi;
18550
18551 /* If this DIE (this DIE's specification, if any) has a parent, then
18552 we should not do this. We'll prepend the parent's fully qualified
18553 name when we create the partial symbol. */
18554
18555 real_pdi = struct_pdi;
18556 while (real_pdi->has_specification)
18557 {
18558 auto res = find_partial_die (real_pdi->spec_offset,
18559 real_pdi->spec_is_dwz, cu);
18560 real_pdi = res.pdi;
18561 cu = res.cu;
18562 }
18563
18564 if (real_pdi->die_parent != NULL)
18565 return;
18566
18567 for (child_pdi = struct_pdi->die_child;
18568 child_pdi != NULL;
18569 child_pdi = child_pdi->die_sibling)
18570 {
18571 if (child_pdi->tag == DW_TAG_subprogram
18572 && child_pdi->linkage_name != NULL)
18573 {
18574 gdb::unique_xmalloc_ptr<char> actual_class_name
18575 (language_class_name_from_physname (cu->language_defn,
18576 child_pdi->linkage_name));
18577 if (actual_class_name != NULL)
18578 {
18579 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18580 struct_pdi->name = objfile->intern (actual_class_name.get ());
18581 }
18582 break;
18583 }
18584 }
18585 }
18586
18587 /* Return true if a DIE with TAG may have the DW_AT_const_value
18588 attribute. */
18589
18590 static bool
18591 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18592 {
18593 switch (tag)
18594 {
18595 case DW_TAG_constant:
18596 case DW_TAG_enumerator:
18597 case DW_TAG_formal_parameter:
18598 case DW_TAG_template_value_param:
18599 case DW_TAG_variable:
18600 return true;
18601 }
18602
18603 return false;
18604 }
18605
18606 void
18607 partial_die_info::fixup (struct dwarf2_cu *cu)
18608 {
18609 /* Once we've fixed up a die, there's no point in doing so again.
18610 This also avoids a memory leak if we were to call
18611 guess_partial_die_structure_name multiple times. */
18612 if (fixup_called)
18613 return;
18614
18615 /* If we found a reference attribute and the DIE has no name, try
18616 to find a name in the referred to DIE. */
18617
18618 if (name == NULL && has_specification)
18619 {
18620 struct partial_die_info *spec_die;
18621
18622 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18623 spec_die = res.pdi;
18624 cu = res.cu;
18625
18626 spec_die->fixup (cu);
18627
18628 if (spec_die->name)
18629 {
18630 name = spec_die->name;
18631
18632 /* Copy DW_AT_external attribute if it is set. */
18633 if (spec_die->is_external)
18634 is_external = spec_die->is_external;
18635 }
18636 }
18637
18638 if (!has_const_value && has_specification
18639 && can_have_DW_AT_const_value_p (tag))
18640 {
18641 struct partial_die_info *spec_die;
18642
18643 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18644 spec_die = res.pdi;
18645 cu = res.cu;
18646
18647 spec_die->fixup (cu);
18648
18649 if (spec_die->has_const_value)
18650 {
18651 /* Copy DW_AT_const_value attribute if it is set. */
18652 has_const_value = spec_die->has_const_value;
18653 }
18654 }
18655
18656 /* Set default names for some unnamed DIEs. */
18657
18658 if (name == NULL && tag == DW_TAG_namespace)
18659 name = CP_ANONYMOUS_NAMESPACE_STR;
18660
18661 /* If there is no parent die to provide a namespace, and there are
18662 children, see if we can determine the namespace from their linkage
18663 name. */
18664 if (cu->language == language_cplus
18665 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18666 && die_parent == NULL
18667 && has_children
18668 && (tag == DW_TAG_class_type
18669 || tag == DW_TAG_structure_type
18670 || tag == DW_TAG_union_type))
18671 guess_partial_die_structure_name (this, cu);
18672
18673 /* GCC might emit a nameless struct or union that has a linkage
18674 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18675 if (name == NULL
18676 && (tag == DW_TAG_class_type
18677 || tag == DW_TAG_interface_type
18678 || tag == DW_TAG_structure_type
18679 || tag == DW_TAG_union_type)
18680 && linkage_name != NULL)
18681 {
18682 gdb::unique_xmalloc_ptr<char> demangled
18683 (gdb_demangle (linkage_name, DMGL_TYPES));
18684 if (demangled != nullptr)
18685 {
18686 const char *base;
18687
18688 /* Strip any leading namespaces/classes, keep only the base name.
18689 DW_AT_name for named DIEs does not contain the prefixes. */
18690 base = strrchr (demangled.get (), ':');
18691 if (base && base > demangled.get () && base[-1] == ':')
18692 base++;
18693 else
18694 base = demangled.get ();
18695
18696 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18697 name = objfile->intern (base);
18698 }
18699 }
18700
18701 fixup_called = 1;
18702 }
18703
18704 /* Read the .debug_loclists header contents from the given SECTION in the
18705 HEADER. */
18706 static void
18707 read_loclist_header (struct loclist_header *header,
18708 struct dwarf2_section_info *section)
18709 {
18710 unsigned int bytes_read;
18711 bfd *abfd = section->get_bfd_owner ();
18712 const gdb_byte *info_ptr = section->buffer;
18713 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18714 info_ptr += bytes_read;
18715 header->version = read_2_bytes (abfd, info_ptr);
18716 info_ptr += 2;
18717 header->addr_size = read_1_byte (abfd, info_ptr);
18718 info_ptr += 1;
18719 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18720 info_ptr += 1;
18721 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18722 }
18723
18724 /* Return the DW_AT_loclists_base value for the CU. */
18725 static ULONGEST
18726 lookup_loclist_base (struct dwarf2_cu *cu)
18727 {
18728 /* For the .dwo unit, the loclist_base points to the first offset following
18729 the header. The header consists of the following entities-
18730 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18731 bit format)
18732 2. version (2 bytes)
18733 3. address size (1 byte)
18734 4. segment selector size (1 byte)
18735 5. offset entry count (4 bytes)
18736 These sizes are derived as per the DWARFv5 standard. */
18737 if (cu->dwo_unit != nullptr)
18738 {
18739 if (cu->header.initial_length_size == 4)
18740 return LOCLIST_HEADER_SIZE32;
18741 return LOCLIST_HEADER_SIZE64;
18742 }
18743 return cu->loclist_base;
18744 }
18745
18746 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18747 array of offsets in the .debug_loclists section. */
18748 static CORE_ADDR
18749 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18750 {
18751 struct dwarf2_per_objfile *dwarf2_per_objfile
18752 = cu->per_cu->dwarf2_per_objfile;
18753 struct objfile *objfile = dwarf2_per_objfile->objfile;
18754 bfd *abfd = objfile->obfd;
18755 ULONGEST loclist_base = lookup_loclist_base (cu);
18756 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18757
18758 section->read (objfile);
18759 if (section->buffer == NULL)
18760 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18761 "section [in module %s]"), objfile_name (objfile));
18762 struct loclist_header header;
18763 read_loclist_header (&header, section);
18764 if (loclist_index >= header.offset_entry_count)
18765 complaint (_("DW_FORM_loclistx pointing outside of "
18766 ".debug_loclists offset array [in module %s]"),
18767 objfile_name (objfile));
18768 if (loclist_base + loclist_index * cu->header.offset_size
18769 >= section->size)
18770 complaint (_("DW_FORM_loclistx pointing outside of "
18771 ".debug_loclists section [in module %s]"),
18772 objfile_name (objfile));
18773 const gdb_byte *info_ptr
18774 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18775
18776 if (cu->header.offset_size == 4)
18777 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18778 else
18779 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18780 }
18781
18782 /* Process the attributes that had to be skipped in the first round. These
18783 attributes are the ones that need str_offsets_base or addr_base attributes.
18784 They could not have been processed in the first round, because at the time
18785 the values of str_offsets_base or addr_base may not have been known. */
18786 static void
18787 read_attribute_reprocess (const struct die_reader_specs *reader,
18788 struct attribute *attr)
18789 {
18790 struct dwarf2_cu *cu = reader->cu;
18791 switch (attr->form)
18792 {
18793 case DW_FORM_addrx:
18794 case DW_FORM_GNU_addr_index:
18795 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18796 break;
18797 case DW_FORM_loclistx:
18798 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18799 break;
18800 case DW_FORM_strx:
18801 case DW_FORM_strx1:
18802 case DW_FORM_strx2:
18803 case DW_FORM_strx3:
18804 case DW_FORM_strx4:
18805 case DW_FORM_GNU_str_index:
18806 {
18807 unsigned int str_index = DW_UNSND (attr);
18808 if (reader->dwo_file != NULL)
18809 {
18810 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18811 DW_STRING_IS_CANONICAL (attr) = 0;
18812 }
18813 else
18814 {
18815 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18816 DW_STRING_IS_CANONICAL (attr) = 0;
18817 }
18818 break;
18819 }
18820 default:
18821 gdb_assert_not_reached (_("Unexpected DWARF form."));
18822 }
18823 }
18824
18825 /* Read an attribute value described by an attribute form. */
18826
18827 static const gdb_byte *
18828 read_attribute_value (const struct die_reader_specs *reader,
18829 struct attribute *attr, unsigned form,
18830 LONGEST implicit_const, const gdb_byte *info_ptr,
18831 bool *need_reprocess)
18832 {
18833 struct dwarf2_cu *cu = reader->cu;
18834 struct dwarf2_per_objfile *dwarf2_per_objfile
18835 = cu->per_cu->dwarf2_per_objfile;
18836 struct objfile *objfile = dwarf2_per_objfile->objfile;
18837 bfd *abfd = reader->abfd;
18838 struct comp_unit_head *cu_header = &cu->header;
18839 unsigned int bytes_read;
18840 struct dwarf_block *blk;
18841 *need_reprocess = false;
18842
18843 attr->form = (enum dwarf_form) form;
18844 switch (form)
18845 {
18846 case DW_FORM_ref_addr:
18847 if (cu->header.version == 2)
18848 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18849 &bytes_read);
18850 else
18851 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18852 &bytes_read);
18853 info_ptr += bytes_read;
18854 break;
18855 case DW_FORM_GNU_ref_alt:
18856 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18857 info_ptr += bytes_read;
18858 break;
18859 case DW_FORM_addr:
18860 {
18861 struct gdbarch *gdbarch = objfile->arch ();
18862 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18863 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18864 info_ptr += bytes_read;
18865 }
18866 break;
18867 case DW_FORM_block2:
18868 blk = dwarf_alloc_block (cu);
18869 blk->size = read_2_bytes (abfd, info_ptr);
18870 info_ptr += 2;
18871 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18872 info_ptr += blk->size;
18873 DW_BLOCK (attr) = blk;
18874 break;
18875 case DW_FORM_block4:
18876 blk = dwarf_alloc_block (cu);
18877 blk->size = read_4_bytes (abfd, info_ptr);
18878 info_ptr += 4;
18879 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18880 info_ptr += blk->size;
18881 DW_BLOCK (attr) = blk;
18882 break;
18883 case DW_FORM_data2:
18884 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18885 info_ptr += 2;
18886 break;
18887 case DW_FORM_data4:
18888 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18889 info_ptr += 4;
18890 break;
18891 case DW_FORM_data8:
18892 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18893 info_ptr += 8;
18894 break;
18895 case DW_FORM_data16:
18896 blk = dwarf_alloc_block (cu);
18897 blk->size = 16;
18898 blk->data = read_n_bytes (abfd, info_ptr, 16);
18899 info_ptr += 16;
18900 DW_BLOCK (attr) = blk;
18901 break;
18902 case DW_FORM_sec_offset:
18903 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18904 info_ptr += bytes_read;
18905 break;
18906 case DW_FORM_loclistx:
18907 {
18908 *need_reprocess = true;
18909 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18910 info_ptr += bytes_read;
18911 }
18912 break;
18913 case DW_FORM_string:
18914 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18915 DW_STRING_IS_CANONICAL (attr) = 0;
18916 info_ptr += bytes_read;
18917 break;
18918 case DW_FORM_strp:
18919 if (!cu->per_cu->is_dwz)
18920 {
18921 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18922 abfd, info_ptr, cu_header,
18923 &bytes_read);
18924 DW_STRING_IS_CANONICAL (attr) = 0;
18925 info_ptr += bytes_read;
18926 break;
18927 }
18928 /* FALLTHROUGH */
18929 case DW_FORM_line_strp:
18930 if (!cu->per_cu->is_dwz)
18931 {
18932 DW_STRING (attr)
18933 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
18934 &bytes_read);
18935 DW_STRING_IS_CANONICAL (attr) = 0;
18936 info_ptr += bytes_read;
18937 break;
18938 }
18939 /* FALLTHROUGH */
18940 case DW_FORM_GNU_strp_alt:
18941 {
18942 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18943 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18944 &bytes_read);
18945
18946 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
18947 DW_STRING_IS_CANONICAL (attr) = 0;
18948 info_ptr += bytes_read;
18949 }
18950 break;
18951 case DW_FORM_exprloc:
18952 case DW_FORM_block:
18953 blk = dwarf_alloc_block (cu);
18954 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18955 info_ptr += bytes_read;
18956 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18957 info_ptr += blk->size;
18958 DW_BLOCK (attr) = blk;
18959 break;
18960 case DW_FORM_block1:
18961 blk = dwarf_alloc_block (cu);
18962 blk->size = read_1_byte (abfd, info_ptr);
18963 info_ptr += 1;
18964 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18965 info_ptr += blk->size;
18966 DW_BLOCK (attr) = blk;
18967 break;
18968 case DW_FORM_data1:
18969 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18970 info_ptr += 1;
18971 break;
18972 case DW_FORM_flag:
18973 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18974 info_ptr += 1;
18975 break;
18976 case DW_FORM_flag_present:
18977 DW_UNSND (attr) = 1;
18978 break;
18979 case DW_FORM_sdata:
18980 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18981 info_ptr += bytes_read;
18982 break;
18983 case DW_FORM_udata:
18984 case DW_FORM_rnglistx:
18985 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18986 info_ptr += bytes_read;
18987 break;
18988 case DW_FORM_ref1:
18989 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18990 + read_1_byte (abfd, info_ptr));
18991 info_ptr += 1;
18992 break;
18993 case DW_FORM_ref2:
18994 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18995 + read_2_bytes (abfd, info_ptr));
18996 info_ptr += 2;
18997 break;
18998 case DW_FORM_ref4:
18999 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19000 + read_4_bytes (abfd, info_ptr));
19001 info_ptr += 4;
19002 break;
19003 case DW_FORM_ref8:
19004 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19005 + read_8_bytes (abfd, info_ptr));
19006 info_ptr += 8;
19007 break;
19008 case DW_FORM_ref_sig8:
19009 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19010 info_ptr += 8;
19011 break;
19012 case DW_FORM_ref_udata:
19013 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19014 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19015 info_ptr += bytes_read;
19016 break;
19017 case DW_FORM_indirect:
19018 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19019 info_ptr += bytes_read;
19020 if (form == DW_FORM_implicit_const)
19021 {
19022 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19023 info_ptr += bytes_read;
19024 }
19025 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19026 info_ptr, need_reprocess);
19027 break;
19028 case DW_FORM_implicit_const:
19029 DW_SND (attr) = implicit_const;
19030 break;
19031 case DW_FORM_addrx:
19032 case DW_FORM_GNU_addr_index:
19033 *need_reprocess = true;
19034 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19035 info_ptr += bytes_read;
19036 break;
19037 case DW_FORM_strx:
19038 case DW_FORM_strx1:
19039 case DW_FORM_strx2:
19040 case DW_FORM_strx3:
19041 case DW_FORM_strx4:
19042 case DW_FORM_GNU_str_index:
19043 {
19044 ULONGEST str_index;
19045 if (form == DW_FORM_strx1)
19046 {
19047 str_index = read_1_byte (abfd, info_ptr);
19048 info_ptr += 1;
19049 }
19050 else if (form == DW_FORM_strx2)
19051 {
19052 str_index = read_2_bytes (abfd, info_ptr);
19053 info_ptr += 2;
19054 }
19055 else if (form == DW_FORM_strx3)
19056 {
19057 str_index = read_3_bytes (abfd, info_ptr);
19058 info_ptr += 3;
19059 }
19060 else if (form == DW_FORM_strx4)
19061 {
19062 str_index = read_4_bytes (abfd, info_ptr);
19063 info_ptr += 4;
19064 }
19065 else
19066 {
19067 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19068 info_ptr += bytes_read;
19069 }
19070 *need_reprocess = true;
19071 DW_UNSND (attr) = str_index;
19072 }
19073 break;
19074 default:
19075 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19076 dwarf_form_name (form),
19077 bfd_get_filename (abfd));
19078 }
19079
19080 /* Super hack. */
19081 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19082 attr->form = DW_FORM_GNU_ref_alt;
19083
19084 /* We have seen instances where the compiler tried to emit a byte
19085 size attribute of -1 which ended up being encoded as an unsigned
19086 0xffffffff. Although 0xffffffff is technically a valid size value,
19087 an object of this size seems pretty unlikely so we can relatively
19088 safely treat these cases as if the size attribute was invalid and
19089 treat them as zero by default. */
19090 if (attr->name == DW_AT_byte_size
19091 && form == DW_FORM_data4
19092 && DW_UNSND (attr) >= 0xffffffff)
19093 {
19094 complaint
19095 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19096 hex_string (DW_UNSND (attr)));
19097 DW_UNSND (attr) = 0;
19098 }
19099
19100 return info_ptr;
19101 }
19102
19103 /* Read an attribute described by an abbreviated attribute. */
19104
19105 static const gdb_byte *
19106 read_attribute (const struct die_reader_specs *reader,
19107 struct attribute *attr, struct attr_abbrev *abbrev,
19108 const gdb_byte *info_ptr, bool *need_reprocess)
19109 {
19110 attr->name = abbrev->name;
19111 return read_attribute_value (reader, attr, abbrev->form,
19112 abbrev->implicit_const, info_ptr,
19113 need_reprocess);
19114 }
19115
19116 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19117
19118 static const char *
19119 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19120 LONGEST str_offset)
19121 {
19122 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
19123 str_offset, "DW_FORM_strp");
19124 }
19125
19126 /* Return pointer to string at .debug_str offset as read from BUF.
19127 BUF is assumed to be in a compilation unit described by CU_HEADER.
19128 Return *BYTES_READ_PTR count of bytes read from BUF. */
19129
19130 static const char *
19131 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19132 const gdb_byte *buf,
19133 const struct comp_unit_head *cu_header,
19134 unsigned int *bytes_read_ptr)
19135 {
19136 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19137
19138 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
19139 }
19140
19141 /* See read.h. */
19142
19143 const char *
19144 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19145 const struct comp_unit_head *cu_header,
19146 unsigned int *bytes_read_ptr)
19147 {
19148 bfd *abfd = objfile->obfd;
19149 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19150
19151 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19152 }
19153
19154 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19155 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19156 ADDR_SIZE is the size of addresses from the CU header. */
19157
19158 static CORE_ADDR
19159 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19160 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19161 int addr_size)
19162 {
19163 struct objfile *objfile = dwarf2_per_objfile->objfile;
19164 bfd *abfd = objfile->obfd;
19165 const gdb_byte *info_ptr;
19166 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19167
19168 dwarf2_per_objfile->addr.read (objfile);
19169 if (dwarf2_per_objfile->addr.buffer == NULL)
19170 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19171 objfile_name (objfile));
19172 if (addr_base_or_zero + addr_index * addr_size
19173 >= dwarf2_per_objfile->addr.size)
19174 error (_("DW_FORM_addr_index pointing outside of "
19175 ".debug_addr section [in module %s]"),
19176 objfile_name (objfile));
19177 info_ptr = (dwarf2_per_objfile->addr.buffer
19178 + addr_base_or_zero + addr_index * addr_size);
19179 if (addr_size == 4)
19180 return bfd_get_32 (abfd, info_ptr);
19181 else
19182 return bfd_get_64 (abfd, info_ptr);
19183 }
19184
19185 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19186
19187 static CORE_ADDR
19188 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19189 {
19190 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19191 cu->addr_base, cu->header.addr_size);
19192 }
19193
19194 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19195
19196 static CORE_ADDR
19197 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19198 unsigned int *bytes_read)
19199 {
19200 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19201 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19202
19203 return read_addr_index (cu, addr_index);
19204 }
19205
19206 /* See read.h. */
19207
19208 CORE_ADDR
19209 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
19210 {
19211 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19212 struct dwarf2_cu *cu = per_cu->cu;
19213 gdb::optional<ULONGEST> addr_base;
19214 int addr_size;
19215
19216 /* We need addr_base and addr_size.
19217 If we don't have PER_CU->cu, we have to get it.
19218 Nasty, but the alternative is storing the needed info in PER_CU,
19219 which at this point doesn't seem justified: it's not clear how frequently
19220 it would get used and it would increase the size of every PER_CU.
19221 Entry points like dwarf2_per_cu_addr_size do a similar thing
19222 so we're not in uncharted territory here.
19223 Alas we need to be a bit more complicated as addr_base is contained
19224 in the DIE.
19225
19226 We don't need to read the entire CU(/TU).
19227 We just need the header and top level die.
19228
19229 IWBN to use the aging mechanism to let us lazily later discard the CU.
19230 For now we skip this optimization. */
19231
19232 if (cu != NULL)
19233 {
19234 addr_base = cu->addr_base;
19235 addr_size = cu->header.addr_size;
19236 }
19237 else
19238 {
19239 cutu_reader reader (per_cu, NULL, 0, false);
19240 addr_base = reader.cu->addr_base;
19241 addr_size = reader.cu->header.addr_size;
19242 }
19243
19244 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19245 addr_size);
19246 }
19247
19248 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19249 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19250 DWO file. */
19251
19252 static const char *
19253 read_str_index (struct dwarf2_cu *cu,
19254 struct dwarf2_section_info *str_section,
19255 struct dwarf2_section_info *str_offsets_section,
19256 ULONGEST str_offsets_base, ULONGEST str_index)
19257 {
19258 struct dwarf2_per_objfile *dwarf2_per_objfile
19259 = cu->per_cu->dwarf2_per_objfile;
19260 struct objfile *objfile = dwarf2_per_objfile->objfile;
19261 const char *objf_name = objfile_name (objfile);
19262 bfd *abfd = objfile->obfd;
19263 const gdb_byte *info_ptr;
19264 ULONGEST str_offset;
19265 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19266
19267 str_section->read (objfile);
19268 str_offsets_section->read (objfile);
19269 if (str_section->buffer == NULL)
19270 error (_("%s used without %s section"
19271 " in CU at offset %s [in module %s]"),
19272 form_name, str_section->get_name (),
19273 sect_offset_str (cu->header.sect_off), objf_name);
19274 if (str_offsets_section->buffer == NULL)
19275 error (_("%s used without %s section"
19276 " in CU at offset %s [in module %s]"),
19277 form_name, str_section->get_name (),
19278 sect_offset_str (cu->header.sect_off), objf_name);
19279 info_ptr = (str_offsets_section->buffer
19280 + str_offsets_base
19281 + str_index * cu->header.offset_size);
19282 if (cu->header.offset_size == 4)
19283 str_offset = bfd_get_32 (abfd, info_ptr);
19284 else
19285 str_offset = bfd_get_64 (abfd, info_ptr);
19286 if (str_offset >= str_section->size)
19287 error (_("Offset from %s pointing outside of"
19288 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19289 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19290 return (const char *) (str_section->buffer + str_offset);
19291 }
19292
19293 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19294
19295 static const char *
19296 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19297 {
19298 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19299 ? reader->cu->header.addr_size : 0;
19300 return read_str_index (reader->cu,
19301 &reader->dwo_file->sections.str,
19302 &reader->dwo_file->sections.str_offsets,
19303 str_offsets_base, str_index);
19304 }
19305
19306 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19307
19308 static const char *
19309 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19310 {
19311 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19312 const char *objf_name = objfile_name (objfile);
19313 static const char form_name[] = "DW_FORM_GNU_str_index";
19314 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19315
19316 if (!cu->str_offsets_base.has_value ())
19317 error (_("%s used in Fission stub without %s"
19318 " in CU at offset 0x%lx [in module %s]"),
19319 form_name, str_offsets_attr_name,
19320 (long) cu->header.offset_size, objf_name);
19321
19322 return read_str_index (cu,
19323 &cu->per_cu->dwarf2_per_objfile->str,
19324 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19325 *cu->str_offsets_base, str_index);
19326 }
19327
19328 /* Return the length of an LEB128 number in BUF. */
19329
19330 static int
19331 leb128_size (const gdb_byte *buf)
19332 {
19333 const gdb_byte *begin = buf;
19334 gdb_byte byte;
19335
19336 while (1)
19337 {
19338 byte = *buf++;
19339 if ((byte & 128) == 0)
19340 return buf - begin;
19341 }
19342 }
19343
19344 static void
19345 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19346 {
19347 switch (lang)
19348 {
19349 case DW_LANG_C89:
19350 case DW_LANG_C99:
19351 case DW_LANG_C11:
19352 case DW_LANG_C:
19353 case DW_LANG_UPC:
19354 cu->language = language_c;
19355 break;
19356 case DW_LANG_Java:
19357 case DW_LANG_C_plus_plus:
19358 case DW_LANG_C_plus_plus_11:
19359 case DW_LANG_C_plus_plus_14:
19360 cu->language = language_cplus;
19361 break;
19362 case DW_LANG_D:
19363 cu->language = language_d;
19364 break;
19365 case DW_LANG_Fortran77:
19366 case DW_LANG_Fortran90:
19367 case DW_LANG_Fortran95:
19368 case DW_LANG_Fortran03:
19369 case DW_LANG_Fortran08:
19370 cu->language = language_fortran;
19371 break;
19372 case DW_LANG_Go:
19373 cu->language = language_go;
19374 break;
19375 case DW_LANG_Mips_Assembler:
19376 cu->language = language_asm;
19377 break;
19378 case DW_LANG_Ada83:
19379 case DW_LANG_Ada95:
19380 cu->language = language_ada;
19381 break;
19382 case DW_LANG_Modula2:
19383 cu->language = language_m2;
19384 break;
19385 case DW_LANG_Pascal83:
19386 cu->language = language_pascal;
19387 break;
19388 case DW_LANG_ObjC:
19389 cu->language = language_objc;
19390 break;
19391 case DW_LANG_Rust:
19392 case DW_LANG_Rust_old:
19393 cu->language = language_rust;
19394 break;
19395 case DW_LANG_Cobol74:
19396 case DW_LANG_Cobol85:
19397 default:
19398 cu->language = language_minimal;
19399 break;
19400 }
19401 cu->language_defn = language_def (cu->language);
19402 }
19403
19404 /* Return the named attribute or NULL if not there. */
19405
19406 static struct attribute *
19407 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19408 {
19409 for (;;)
19410 {
19411 unsigned int i;
19412 struct attribute *spec = NULL;
19413
19414 for (i = 0; i < die->num_attrs; ++i)
19415 {
19416 if (die->attrs[i].name == name)
19417 return &die->attrs[i];
19418 if (die->attrs[i].name == DW_AT_specification
19419 || die->attrs[i].name == DW_AT_abstract_origin)
19420 spec = &die->attrs[i];
19421 }
19422
19423 if (!spec)
19424 break;
19425
19426 die = follow_die_ref (die, spec, &cu);
19427 }
19428
19429 return NULL;
19430 }
19431
19432 /* Return the string associated with a string-typed attribute, or NULL if it
19433 is either not found or is of an incorrect type. */
19434
19435 static const char *
19436 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19437 {
19438 struct attribute *attr;
19439 const char *str = NULL;
19440
19441 attr = dwarf2_attr (die, name, cu);
19442
19443 if (attr != NULL)
19444 {
19445 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19446 || attr->form == DW_FORM_string
19447 || attr->form == DW_FORM_strx
19448 || attr->form == DW_FORM_strx1
19449 || attr->form == DW_FORM_strx2
19450 || attr->form == DW_FORM_strx3
19451 || attr->form == DW_FORM_strx4
19452 || attr->form == DW_FORM_GNU_str_index
19453 || attr->form == DW_FORM_GNU_strp_alt)
19454 str = DW_STRING (attr);
19455 else
19456 complaint (_("string type expected for attribute %s for "
19457 "DIE at %s in module %s"),
19458 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19459 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19460 }
19461
19462 return str;
19463 }
19464
19465 /* Return the dwo name or NULL if not present. If present, it is in either
19466 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19467 static const char *
19468 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19469 {
19470 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19471 if (dwo_name == nullptr)
19472 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19473 return dwo_name;
19474 }
19475
19476 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19477 and holds a non-zero value. This function should only be used for
19478 DW_FORM_flag or DW_FORM_flag_present attributes. */
19479
19480 static int
19481 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19482 {
19483 struct attribute *attr = dwarf2_attr (die, name, cu);
19484
19485 return (attr && DW_UNSND (attr));
19486 }
19487
19488 static int
19489 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19490 {
19491 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19492 which value is non-zero. However, we have to be careful with
19493 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19494 (via dwarf2_flag_true_p) follows this attribute. So we may
19495 end up accidently finding a declaration attribute that belongs
19496 to a different DIE referenced by the specification attribute,
19497 even though the given DIE does not have a declaration attribute. */
19498 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19499 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19500 }
19501
19502 /* Return the die giving the specification for DIE, if there is
19503 one. *SPEC_CU is the CU containing DIE on input, and the CU
19504 containing the return value on output. If there is no
19505 specification, but there is an abstract origin, that is
19506 returned. */
19507
19508 static struct die_info *
19509 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19510 {
19511 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19512 *spec_cu);
19513
19514 if (spec_attr == NULL)
19515 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19516
19517 if (spec_attr == NULL)
19518 return NULL;
19519 else
19520 return follow_die_ref (die, spec_attr, spec_cu);
19521 }
19522
19523 /* Stub for free_line_header to match void * callback types. */
19524
19525 static void
19526 free_line_header_voidp (void *arg)
19527 {
19528 struct line_header *lh = (struct line_header *) arg;
19529
19530 delete lh;
19531 }
19532
19533 /* A convenience function to find the proper .debug_line section for a CU. */
19534
19535 static struct dwarf2_section_info *
19536 get_debug_line_section (struct dwarf2_cu *cu)
19537 {
19538 struct dwarf2_section_info *section;
19539 struct dwarf2_per_objfile *dwarf2_per_objfile
19540 = cu->per_cu->dwarf2_per_objfile;
19541
19542 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19543 DWO file. */
19544 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19545 section = &cu->dwo_unit->dwo_file->sections.line;
19546 else if (cu->per_cu->is_dwz)
19547 {
19548 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19549
19550 section = &dwz->line;
19551 }
19552 else
19553 section = &dwarf2_per_objfile->line;
19554
19555 return section;
19556 }
19557
19558 /* Read the statement program header starting at OFFSET in
19559 .debug_line, or .debug_line.dwo. Return a pointer
19560 to a struct line_header, allocated using xmalloc.
19561 Returns NULL if there is a problem reading the header, e.g., if it
19562 has a version we don't understand.
19563
19564 NOTE: the strings in the include directory and file name tables of
19565 the returned object point into the dwarf line section buffer,
19566 and must not be freed. */
19567
19568 static line_header_up
19569 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19570 {
19571 struct dwarf2_section_info *section;
19572 struct dwarf2_per_objfile *dwarf2_per_objfile
19573 = cu->per_cu->dwarf2_per_objfile;
19574
19575 section = get_debug_line_section (cu);
19576 section->read (dwarf2_per_objfile->objfile);
19577 if (section->buffer == NULL)
19578 {
19579 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19580 complaint (_("missing .debug_line.dwo section"));
19581 else
19582 complaint (_("missing .debug_line section"));
19583 return 0;
19584 }
19585
19586 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19587 dwarf2_per_objfile, section,
19588 &cu->header);
19589 }
19590
19591 /* Subroutine of dwarf_decode_lines to simplify it.
19592 Return the file name of the psymtab for the given file_entry.
19593 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19594 If space for the result is malloc'd, *NAME_HOLDER will be set.
19595 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19596
19597 static const char *
19598 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19599 const dwarf2_psymtab *pst,
19600 const char *comp_dir,
19601 gdb::unique_xmalloc_ptr<char> *name_holder)
19602 {
19603 const char *include_name = fe.name;
19604 const char *include_name_to_compare = include_name;
19605 const char *pst_filename;
19606 int file_is_pst;
19607
19608 const char *dir_name = fe.include_dir (lh);
19609
19610 gdb::unique_xmalloc_ptr<char> hold_compare;
19611 if (!IS_ABSOLUTE_PATH (include_name)
19612 && (dir_name != NULL || comp_dir != NULL))
19613 {
19614 /* Avoid creating a duplicate psymtab for PST.
19615 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19616 Before we do the comparison, however, we need to account
19617 for DIR_NAME and COMP_DIR.
19618 First prepend dir_name (if non-NULL). If we still don't
19619 have an absolute path prepend comp_dir (if non-NULL).
19620 However, the directory we record in the include-file's
19621 psymtab does not contain COMP_DIR (to match the
19622 corresponding symtab(s)).
19623
19624 Example:
19625
19626 bash$ cd /tmp
19627 bash$ gcc -g ./hello.c
19628 include_name = "hello.c"
19629 dir_name = "."
19630 DW_AT_comp_dir = comp_dir = "/tmp"
19631 DW_AT_name = "./hello.c"
19632
19633 */
19634
19635 if (dir_name != NULL)
19636 {
19637 name_holder->reset (concat (dir_name, SLASH_STRING,
19638 include_name, (char *) NULL));
19639 include_name = name_holder->get ();
19640 include_name_to_compare = include_name;
19641 }
19642 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19643 {
19644 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19645 include_name, (char *) NULL));
19646 include_name_to_compare = hold_compare.get ();
19647 }
19648 }
19649
19650 pst_filename = pst->filename;
19651 gdb::unique_xmalloc_ptr<char> copied_name;
19652 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19653 {
19654 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19655 pst_filename, (char *) NULL));
19656 pst_filename = copied_name.get ();
19657 }
19658
19659 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19660
19661 if (file_is_pst)
19662 return NULL;
19663 return include_name;
19664 }
19665
19666 /* State machine to track the state of the line number program. */
19667
19668 class lnp_state_machine
19669 {
19670 public:
19671 /* Initialize a machine state for the start of a line number
19672 program. */
19673 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19674 bool record_lines_p);
19675
19676 file_entry *current_file ()
19677 {
19678 /* lh->file_names is 0-based, but the file name numbers in the
19679 statement program are 1-based. */
19680 return m_line_header->file_name_at (m_file);
19681 }
19682
19683 /* Record the line in the state machine. END_SEQUENCE is true if
19684 we're processing the end of a sequence. */
19685 void record_line (bool end_sequence);
19686
19687 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19688 nop-out rest of the lines in this sequence. */
19689 void check_line_address (struct dwarf2_cu *cu,
19690 const gdb_byte *line_ptr,
19691 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19692
19693 void handle_set_discriminator (unsigned int discriminator)
19694 {
19695 m_discriminator = discriminator;
19696 m_line_has_non_zero_discriminator |= discriminator != 0;
19697 }
19698
19699 /* Handle DW_LNE_set_address. */
19700 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19701 {
19702 m_op_index = 0;
19703 address += baseaddr;
19704 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19705 }
19706
19707 /* Handle DW_LNS_advance_pc. */
19708 void handle_advance_pc (CORE_ADDR adjust);
19709
19710 /* Handle a special opcode. */
19711 void handle_special_opcode (unsigned char op_code);
19712
19713 /* Handle DW_LNS_advance_line. */
19714 void handle_advance_line (int line_delta)
19715 {
19716 advance_line (line_delta);
19717 }
19718
19719 /* Handle DW_LNS_set_file. */
19720 void handle_set_file (file_name_index file);
19721
19722 /* Handle DW_LNS_negate_stmt. */
19723 void handle_negate_stmt ()
19724 {
19725 m_is_stmt = !m_is_stmt;
19726 }
19727
19728 /* Handle DW_LNS_const_add_pc. */
19729 void handle_const_add_pc ();
19730
19731 /* Handle DW_LNS_fixed_advance_pc. */
19732 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19733 {
19734 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19735 m_op_index = 0;
19736 }
19737
19738 /* Handle DW_LNS_copy. */
19739 void handle_copy ()
19740 {
19741 record_line (false);
19742 m_discriminator = 0;
19743 }
19744
19745 /* Handle DW_LNE_end_sequence. */
19746 void handle_end_sequence ()
19747 {
19748 m_currently_recording_lines = true;
19749 }
19750
19751 private:
19752 /* Advance the line by LINE_DELTA. */
19753 void advance_line (int line_delta)
19754 {
19755 m_line += line_delta;
19756
19757 if (line_delta != 0)
19758 m_line_has_non_zero_discriminator = m_discriminator != 0;
19759 }
19760
19761 struct dwarf2_cu *m_cu;
19762
19763 gdbarch *m_gdbarch;
19764
19765 /* True if we're recording lines.
19766 Otherwise we're building partial symtabs and are just interested in
19767 finding include files mentioned by the line number program. */
19768 bool m_record_lines_p;
19769
19770 /* The line number header. */
19771 line_header *m_line_header;
19772
19773 /* These are part of the standard DWARF line number state machine,
19774 and initialized according to the DWARF spec. */
19775
19776 unsigned char m_op_index = 0;
19777 /* The line table index of the current file. */
19778 file_name_index m_file = 1;
19779 unsigned int m_line = 1;
19780
19781 /* These are initialized in the constructor. */
19782
19783 CORE_ADDR m_address;
19784 bool m_is_stmt;
19785 unsigned int m_discriminator;
19786
19787 /* Additional bits of state we need to track. */
19788
19789 /* The last file that we called dwarf2_start_subfile for.
19790 This is only used for TLLs. */
19791 unsigned int m_last_file = 0;
19792 /* The last file a line number was recorded for. */
19793 struct subfile *m_last_subfile = NULL;
19794
19795 /* When true, record the lines we decode. */
19796 bool m_currently_recording_lines = false;
19797
19798 /* The last line number that was recorded, used to coalesce
19799 consecutive entries for the same line. This can happen, for
19800 example, when discriminators are present. PR 17276. */
19801 unsigned int m_last_line = 0;
19802 bool m_line_has_non_zero_discriminator = false;
19803 };
19804
19805 void
19806 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19807 {
19808 CORE_ADDR addr_adj = (((m_op_index + adjust)
19809 / m_line_header->maximum_ops_per_instruction)
19810 * m_line_header->minimum_instruction_length);
19811 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19812 m_op_index = ((m_op_index + adjust)
19813 % m_line_header->maximum_ops_per_instruction);
19814 }
19815
19816 void
19817 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19818 {
19819 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19820 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19821 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19822 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19823 / m_line_header->maximum_ops_per_instruction)
19824 * m_line_header->minimum_instruction_length);
19825 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19826 m_op_index = ((m_op_index + adj_opcode_d)
19827 % m_line_header->maximum_ops_per_instruction);
19828
19829 int line_delta = m_line_header->line_base + adj_opcode_r;
19830 advance_line (line_delta);
19831 record_line (false);
19832 m_discriminator = 0;
19833 }
19834
19835 void
19836 lnp_state_machine::handle_set_file (file_name_index file)
19837 {
19838 m_file = file;
19839
19840 const file_entry *fe = current_file ();
19841 if (fe == NULL)
19842 dwarf2_debug_line_missing_file_complaint ();
19843 else if (m_record_lines_p)
19844 {
19845 const char *dir = fe->include_dir (m_line_header);
19846
19847 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19848 m_line_has_non_zero_discriminator = m_discriminator != 0;
19849 dwarf2_start_subfile (m_cu, fe->name, dir);
19850 }
19851 }
19852
19853 void
19854 lnp_state_machine::handle_const_add_pc ()
19855 {
19856 CORE_ADDR adjust
19857 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19858
19859 CORE_ADDR addr_adj
19860 = (((m_op_index + adjust)
19861 / m_line_header->maximum_ops_per_instruction)
19862 * m_line_header->minimum_instruction_length);
19863
19864 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19865 m_op_index = ((m_op_index + adjust)
19866 % m_line_header->maximum_ops_per_instruction);
19867 }
19868
19869 /* Return non-zero if we should add LINE to the line number table.
19870 LINE is the line to add, LAST_LINE is the last line that was added,
19871 LAST_SUBFILE is the subfile for LAST_LINE.
19872 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19873 had a non-zero discriminator.
19874
19875 We have to be careful in the presence of discriminators.
19876 E.g., for this line:
19877
19878 for (i = 0; i < 100000; i++);
19879
19880 clang can emit four line number entries for that one line,
19881 each with a different discriminator.
19882 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19883
19884 However, we want gdb to coalesce all four entries into one.
19885 Otherwise the user could stepi into the middle of the line and
19886 gdb would get confused about whether the pc really was in the
19887 middle of the line.
19888
19889 Things are further complicated by the fact that two consecutive
19890 line number entries for the same line is a heuristic used by gcc
19891 to denote the end of the prologue. So we can't just discard duplicate
19892 entries, we have to be selective about it. The heuristic we use is
19893 that we only collapse consecutive entries for the same line if at least
19894 one of those entries has a non-zero discriminator. PR 17276.
19895
19896 Note: Addresses in the line number state machine can never go backwards
19897 within one sequence, thus this coalescing is ok. */
19898
19899 static int
19900 dwarf_record_line_p (struct dwarf2_cu *cu,
19901 unsigned int line, unsigned int last_line,
19902 int line_has_non_zero_discriminator,
19903 struct subfile *last_subfile)
19904 {
19905 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19906 return 1;
19907 if (line != last_line)
19908 return 1;
19909 /* Same line for the same file that we've seen already.
19910 As a last check, for pr 17276, only record the line if the line
19911 has never had a non-zero discriminator. */
19912 if (!line_has_non_zero_discriminator)
19913 return 1;
19914 return 0;
19915 }
19916
19917 /* Use the CU's builder to record line number LINE beginning at
19918 address ADDRESS in the line table of subfile SUBFILE. */
19919
19920 static void
19921 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19922 unsigned int line, CORE_ADDR address, bool is_stmt,
19923 struct dwarf2_cu *cu)
19924 {
19925 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19926
19927 if (dwarf_line_debug)
19928 {
19929 fprintf_unfiltered (gdb_stdlog,
19930 "Recording line %u, file %s, address %s\n",
19931 line, lbasename (subfile->name),
19932 paddress (gdbarch, address));
19933 }
19934
19935 if (cu != nullptr)
19936 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
19937 }
19938
19939 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19940 Mark the end of a set of line number records.
19941 The arguments are the same as for dwarf_record_line_1.
19942 If SUBFILE is NULL the request is ignored. */
19943
19944 static void
19945 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19946 CORE_ADDR address, struct dwarf2_cu *cu)
19947 {
19948 if (subfile == NULL)
19949 return;
19950
19951 if (dwarf_line_debug)
19952 {
19953 fprintf_unfiltered (gdb_stdlog,
19954 "Finishing current line, file %s, address %s\n",
19955 lbasename (subfile->name),
19956 paddress (gdbarch, address));
19957 }
19958
19959 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
19960 }
19961
19962 void
19963 lnp_state_machine::record_line (bool end_sequence)
19964 {
19965 if (dwarf_line_debug)
19966 {
19967 fprintf_unfiltered (gdb_stdlog,
19968 "Processing actual line %u: file %u,"
19969 " address %s, is_stmt %u, discrim %u%s\n",
19970 m_line, m_file,
19971 paddress (m_gdbarch, m_address),
19972 m_is_stmt, m_discriminator,
19973 (end_sequence ? "\t(end sequence)" : ""));
19974 }
19975
19976 file_entry *fe = current_file ();
19977
19978 if (fe == NULL)
19979 dwarf2_debug_line_missing_file_complaint ();
19980 /* For now we ignore lines not starting on an instruction boundary.
19981 But not when processing end_sequence for compatibility with the
19982 previous version of the code. */
19983 else if (m_op_index == 0 || end_sequence)
19984 {
19985 fe->included_p = 1;
19986 if (m_record_lines_p)
19987 {
19988 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
19989 || end_sequence)
19990 {
19991 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19992 m_currently_recording_lines ? m_cu : nullptr);
19993 }
19994
19995 if (!end_sequence)
19996 {
19997 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
19998
19999 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20000 m_line_has_non_zero_discriminator,
20001 m_last_subfile))
20002 {
20003 buildsym_compunit *builder = m_cu->get_builder ();
20004 dwarf_record_line_1 (m_gdbarch,
20005 builder->get_current_subfile (),
20006 m_line, m_address, is_stmt,
20007 m_currently_recording_lines ? m_cu : nullptr);
20008 }
20009 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20010 m_last_line = m_line;
20011 }
20012 }
20013 }
20014 }
20015
20016 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20017 line_header *lh, bool record_lines_p)
20018 {
20019 m_cu = cu;
20020 m_gdbarch = arch;
20021 m_record_lines_p = record_lines_p;
20022 m_line_header = lh;
20023
20024 m_currently_recording_lines = true;
20025
20026 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20027 was a line entry for it so that the backend has a chance to adjust it
20028 and also record it in case it needs it. This is currently used by MIPS
20029 code, cf. `mips_adjust_dwarf2_line'. */
20030 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20031 m_is_stmt = lh->default_is_stmt;
20032 m_discriminator = 0;
20033 }
20034
20035 void
20036 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20037 const gdb_byte *line_ptr,
20038 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20039 {
20040 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20041 the pc range of the CU. However, we restrict the test to only ADDRESS
20042 values of zero to preserve GDB's previous behaviour which is to handle
20043 the specific case of a function being GC'd by the linker. */
20044
20045 if (address == 0 && address < unrelocated_lowpc)
20046 {
20047 /* This line table is for a function which has been
20048 GCd by the linker. Ignore it. PR gdb/12528 */
20049
20050 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20051 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20052
20053 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20054 line_offset, objfile_name (objfile));
20055 m_currently_recording_lines = false;
20056 /* Note: m_currently_recording_lines is left as false until we see
20057 DW_LNE_end_sequence. */
20058 }
20059 }
20060
20061 /* Subroutine of dwarf_decode_lines to simplify it.
20062 Process the line number information in LH.
20063 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20064 program in order to set included_p for every referenced header. */
20065
20066 static void
20067 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20068 const int decode_for_pst_p, CORE_ADDR lowpc)
20069 {
20070 const gdb_byte *line_ptr, *extended_end;
20071 const gdb_byte *line_end;
20072 unsigned int bytes_read, extended_len;
20073 unsigned char op_code, extended_op;
20074 CORE_ADDR baseaddr;
20075 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20076 bfd *abfd = objfile->obfd;
20077 struct gdbarch *gdbarch = objfile->arch ();
20078 /* True if we're recording line info (as opposed to building partial
20079 symtabs and just interested in finding include files mentioned by
20080 the line number program). */
20081 bool record_lines_p = !decode_for_pst_p;
20082
20083 baseaddr = objfile->text_section_offset ();
20084
20085 line_ptr = lh->statement_program_start;
20086 line_end = lh->statement_program_end;
20087
20088 /* Read the statement sequences until there's nothing left. */
20089 while (line_ptr < line_end)
20090 {
20091 /* The DWARF line number program state machine. Reset the state
20092 machine at the start of each sequence. */
20093 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20094 bool end_sequence = false;
20095
20096 if (record_lines_p)
20097 {
20098 /* Start a subfile for the current file of the state
20099 machine. */
20100 const file_entry *fe = state_machine.current_file ();
20101
20102 if (fe != NULL)
20103 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20104 }
20105
20106 /* Decode the table. */
20107 while (line_ptr < line_end && !end_sequence)
20108 {
20109 op_code = read_1_byte (abfd, line_ptr);
20110 line_ptr += 1;
20111
20112 if (op_code >= lh->opcode_base)
20113 {
20114 /* Special opcode. */
20115 state_machine.handle_special_opcode (op_code);
20116 }
20117 else switch (op_code)
20118 {
20119 case DW_LNS_extended_op:
20120 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20121 &bytes_read);
20122 line_ptr += bytes_read;
20123 extended_end = line_ptr + extended_len;
20124 extended_op = read_1_byte (abfd, line_ptr);
20125 line_ptr += 1;
20126 switch (extended_op)
20127 {
20128 case DW_LNE_end_sequence:
20129 state_machine.handle_end_sequence ();
20130 end_sequence = true;
20131 break;
20132 case DW_LNE_set_address:
20133 {
20134 CORE_ADDR address
20135 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20136 line_ptr += bytes_read;
20137
20138 state_machine.check_line_address (cu, line_ptr,
20139 lowpc - baseaddr, address);
20140 state_machine.handle_set_address (baseaddr, address);
20141 }
20142 break;
20143 case DW_LNE_define_file:
20144 {
20145 const char *cur_file;
20146 unsigned int mod_time, length;
20147 dir_index dindex;
20148
20149 cur_file = read_direct_string (abfd, line_ptr,
20150 &bytes_read);
20151 line_ptr += bytes_read;
20152 dindex = (dir_index)
20153 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20154 line_ptr += bytes_read;
20155 mod_time =
20156 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20157 line_ptr += bytes_read;
20158 length =
20159 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20160 line_ptr += bytes_read;
20161 lh->add_file_name (cur_file, dindex, mod_time, length);
20162 }
20163 break;
20164 case DW_LNE_set_discriminator:
20165 {
20166 /* The discriminator is not interesting to the
20167 debugger; just ignore it. We still need to
20168 check its value though:
20169 if there are consecutive entries for the same
20170 (non-prologue) line we want to coalesce them.
20171 PR 17276. */
20172 unsigned int discr
20173 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20174 line_ptr += bytes_read;
20175
20176 state_machine.handle_set_discriminator (discr);
20177 }
20178 break;
20179 default:
20180 complaint (_("mangled .debug_line section"));
20181 return;
20182 }
20183 /* Make sure that we parsed the extended op correctly. If e.g.
20184 we expected a different address size than the producer used,
20185 we may have read the wrong number of bytes. */
20186 if (line_ptr != extended_end)
20187 {
20188 complaint (_("mangled .debug_line section"));
20189 return;
20190 }
20191 break;
20192 case DW_LNS_copy:
20193 state_machine.handle_copy ();
20194 break;
20195 case DW_LNS_advance_pc:
20196 {
20197 CORE_ADDR adjust
20198 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20199 line_ptr += bytes_read;
20200
20201 state_machine.handle_advance_pc (adjust);
20202 }
20203 break;
20204 case DW_LNS_advance_line:
20205 {
20206 int line_delta
20207 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20208 line_ptr += bytes_read;
20209
20210 state_machine.handle_advance_line (line_delta);
20211 }
20212 break;
20213 case DW_LNS_set_file:
20214 {
20215 file_name_index file
20216 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20217 &bytes_read);
20218 line_ptr += bytes_read;
20219
20220 state_machine.handle_set_file (file);
20221 }
20222 break;
20223 case DW_LNS_set_column:
20224 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20225 line_ptr += bytes_read;
20226 break;
20227 case DW_LNS_negate_stmt:
20228 state_machine.handle_negate_stmt ();
20229 break;
20230 case DW_LNS_set_basic_block:
20231 break;
20232 /* Add to the address register of the state machine the
20233 address increment value corresponding to special opcode
20234 255. I.e., this value is scaled by the minimum
20235 instruction length since special opcode 255 would have
20236 scaled the increment. */
20237 case DW_LNS_const_add_pc:
20238 state_machine.handle_const_add_pc ();
20239 break;
20240 case DW_LNS_fixed_advance_pc:
20241 {
20242 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20243 line_ptr += 2;
20244
20245 state_machine.handle_fixed_advance_pc (addr_adj);
20246 }
20247 break;
20248 default:
20249 {
20250 /* Unknown standard opcode, ignore it. */
20251 int i;
20252
20253 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20254 {
20255 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20256 line_ptr += bytes_read;
20257 }
20258 }
20259 }
20260 }
20261
20262 if (!end_sequence)
20263 dwarf2_debug_line_missing_end_sequence_complaint ();
20264
20265 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20266 in which case we still finish recording the last line). */
20267 state_machine.record_line (true);
20268 }
20269 }
20270
20271 /* Decode the Line Number Program (LNP) for the given line_header
20272 structure and CU. The actual information extracted and the type
20273 of structures created from the LNP depends on the value of PST.
20274
20275 1. If PST is NULL, then this procedure uses the data from the program
20276 to create all necessary symbol tables, and their linetables.
20277
20278 2. If PST is not NULL, this procedure reads the program to determine
20279 the list of files included by the unit represented by PST, and
20280 builds all the associated partial symbol tables.
20281
20282 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20283 It is used for relative paths in the line table.
20284 NOTE: When processing partial symtabs (pst != NULL),
20285 comp_dir == pst->dirname.
20286
20287 NOTE: It is important that psymtabs have the same file name (via strcmp)
20288 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20289 symtab we don't use it in the name of the psymtabs we create.
20290 E.g. expand_line_sal requires this when finding psymtabs to expand.
20291 A good testcase for this is mb-inline.exp.
20292
20293 LOWPC is the lowest address in CU (or 0 if not known).
20294
20295 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20296 for its PC<->lines mapping information. Otherwise only the filename
20297 table is read in. */
20298
20299 static void
20300 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20301 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20302 CORE_ADDR lowpc, int decode_mapping)
20303 {
20304 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20305 const int decode_for_pst_p = (pst != NULL);
20306
20307 if (decode_mapping)
20308 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20309
20310 if (decode_for_pst_p)
20311 {
20312 /* Now that we're done scanning the Line Header Program, we can
20313 create the psymtab of each included file. */
20314 for (auto &file_entry : lh->file_names ())
20315 if (file_entry.included_p == 1)
20316 {
20317 gdb::unique_xmalloc_ptr<char> name_holder;
20318 const char *include_name =
20319 psymtab_include_file_name (lh, file_entry, pst,
20320 comp_dir, &name_holder);
20321 if (include_name != NULL)
20322 dwarf2_create_include_psymtab (include_name, pst, objfile);
20323 }
20324 }
20325 else
20326 {
20327 /* Make sure a symtab is created for every file, even files
20328 which contain only variables (i.e. no code with associated
20329 line numbers). */
20330 buildsym_compunit *builder = cu->get_builder ();
20331 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20332
20333 for (auto &fe : lh->file_names ())
20334 {
20335 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20336 if (builder->get_current_subfile ()->symtab == NULL)
20337 {
20338 builder->get_current_subfile ()->symtab
20339 = allocate_symtab (cust,
20340 builder->get_current_subfile ()->name);
20341 }
20342 fe.symtab = builder->get_current_subfile ()->symtab;
20343 }
20344 }
20345 }
20346
20347 /* Start a subfile for DWARF. FILENAME is the name of the file and
20348 DIRNAME the name of the source directory which contains FILENAME
20349 or NULL if not known.
20350 This routine tries to keep line numbers from identical absolute and
20351 relative file names in a common subfile.
20352
20353 Using the `list' example from the GDB testsuite, which resides in
20354 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20355 of /srcdir/list0.c yields the following debugging information for list0.c:
20356
20357 DW_AT_name: /srcdir/list0.c
20358 DW_AT_comp_dir: /compdir
20359 files.files[0].name: list0.h
20360 files.files[0].dir: /srcdir
20361 files.files[1].name: list0.c
20362 files.files[1].dir: /srcdir
20363
20364 The line number information for list0.c has to end up in a single
20365 subfile, so that `break /srcdir/list0.c:1' works as expected.
20366 start_subfile will ensure that this happens provided that we pass the
20367 concatenation of files.files[1].dir and files.files[1].name as the
20368 subfile's name. */
20369
20370 static void
20371 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20372 const char *dirname)
20373 {
20374 gdb::unique_xmalloc_ptr<char> copy;
20375
20376 /* In order not to lose the line information directory,
20377 we concatenate it to the filename when it makes sense.
20378 Note that the Dwarf3 standard says (speaking of filenames in line
20379 information): ``The directory index is ignored for file names
20380 that represent full path names''. Thus ignoring dirname in the
20381 `else' branch below isn't an issue. */
20382
20383 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20384 {
20385 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20386 filename = copy.get ();
20387 }
20388
20389 cu->get_builder ()->start_subfile (filename);
20390 }
20391
20392 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20393 buildsym_compunit constructor. */
20394
20395 struct compunit_symtab *
20396 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20397 CORE_ADDR low_pc)
20398 {
20399 gdb_assert (m_builder == nullptr);
20400
20401 m_builder.reset (new struct buildsym_compunit
20402 (per_cu->dwarf2_per_objfile->objfile,
20403 name, comp_dir, language, low_pc));
20404
20405 list_in_scope = get_builder ()->get_file_symbols ();
20406
20407 get_builder ()->record_debugformat ("DWARF 2");
20408 get_builder ()->record_producer (producer);
20409
20410 processing_has_namespace_info = false;
20411
20412 return get_builder ()->get_compunit_symtab ();
20413 }
20414
20415 static void
20416 var_decode_location (struct attribute *attr, struct symbol *sym,
20417 struct dwarf2_cu *cu)
20418 {
20419 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20420 struct comp_unit_head *cu_header = &cu->header;
20421
20422 /* NOTE drow/2003-01-30: There used to be a comment and some special
20423 code here to turn a symbol with DW_AT_external and a
20424 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20425 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20426 with some versions of binutils) where shared libraries could have
20427 relocations against symbols in their debug information - the
20428 minimal symbol would have the right address, but the debug info
20429 would not. It's no longer necessary, because we will explicitly
20430 apply relocations when we read in the debug information now. */
20431
20432 /* A DW_AT_location attribute with no contents indicates that a
20433 variable has been optimized away. */
20434 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20435 {
20436 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20437 return;
20438 }
20439
20440 /* Handle one degenerate form of location expression specially, to
20441 preserve GDB's previous behavior when section offsets are
20442 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20443 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20444
20445 if (attr->form_is_block ()
20446 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20447 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20448 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20449 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20450 && (DW_BLOCK (attr)->size
20451 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20452 {
20453 unsigned int dummy;
20454
20455 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20456 SET_SYMBOL_VALUE_ADDRESS
20457 (sym, cu->header.read_address (objfile->obfd,
20458 DW_BLOCK (attr)->data + 1,
20459 &dummy));
20460 else
20461 SET_SYMBOL_VALUE_ADDRESS
20462 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20463 &dummy));
20464 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20465 fixup_symbol_section (sym, objfile);
20466 SET_SYMBOL_VALUE_ADDRESS
20467 (sym,
20468 SYMBOL_VALUE_ADDRESS (sym)
20469 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20470 return;
20471 }
20472
20473 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20474 expression evaluator, and use LOC_COMPUTED only when necessary
20475 (i.e. when the value of a register or memory location is
20476 referenced, or a thread-local block, etc.). Then again, it might
20477 not be worthwhile. I'm assuming that it isn't unless performance
20478 or memory numbers show me otherwise. */
20479
20480 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20481
20482 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20483 cu->has_loclist = true;
20484 }
20485
20486 /* Given a pointer to a DWARF information entry, figure out if we need
20487 to make a symbol table entry for it, and if so, create a new entry
20488 and return a pointer to it.
20489 If TYPE is NULL, determine symbol type from the die, otherwise
20490 used the passed type.
20491 If SPACE is not NULL, use it to hold the new symbol. If it is
20492 NULL, allocate a new symbol on the objfile's obstack. */
20493
20494 static struct symbol *
20495 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20496 struct symbol *space)
20497 {
20498 struct dwarf2_per_objfile *dwarf2_per_objfile
20499 = cu->per_cu->dwarf2_per_objfile;
20500 struct objfile *objfile = dwarf2_per_objfile->objfile;
20501 struct gdbarch *gdbarch = objfile->arch ();
20502 struct symbol *sym = NULL;
20503 const char *name;
20504 struct attribute *attr = NULL;
20505 struct attribute *attr2 = NULL;
20506 CORE_ADDR baseaddr;
20507 struct pending **list_to_add = NULL;
20508
20509 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20510
20511 baseaddr = objfile->text_section_offset ();
20512
20513 name = dwarf2_name (die, cu);
20514 if (name)
20515 {
20516 const char *linkagename;
20517 int suppress_add = 0;
20518
20519 if (space)
20520 sym = space;
20521 else
20522 sym = allocate_symbol (objfile);
20523 OBJSTAT (objfile, n_syms++);
20524
20525 /* Cache this symbol's name and the name's demangled form (if any). */
20526 sym->set_language (cu->language, &objfile->objfile_obstack);
20527 linkagename = dwarf2_physname (name, die, cu);
20528 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20529
20530 /* Fortran does not have mangling standard and the mangling does differ
20531 between gfortran, iFort etc. */
20532 if (cu->language == language_fortran
20533 && symbol_get_demangled_name (sym) == NULL)
20534 symbol_set_demangled_name (sym,
20535 dwarf2_full_name (name, die, cu),
20536 NULL);
20537
20538 /* Default assumptions.
20539 Use the passed type or decode it from the die. */
20540 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20541 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20542 if (type != NULL)
20543 SYMBOL_TYPE (sym) = type;
20544 else
20545 SYMBOL_TYPE (sym) = die_type (die, cu);
20546 attr = dwarf2_attr (die,
20547 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20548 cu);
20549 if (attr != nullptr)
20550 {
20551 SYMBOL_LINE (sym) = DW_UNSND (attr);
20552 }
20553
20554 attr = dwarf2_attr (die,
20555 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20556 cu);
20557 if (attr != nullptr)
20558 {
20559 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20560 struct file_entry *fe;
20561
20562 if (cu->line_header != NULL)
20563 fe = cu->line_header->file_name_at (file_index);
20564 else
20565 fe = NULL;
20566
20567 if (fe == NULL)
20568 complaint (_("file index out of range"));
20569 else
20570 symbol_set_symtab (sym, fe->symtab);
20571 }
20572
20573 switch (die->tag)
20574 {
20575 case DW_TAG_label:
20576 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20577 if (attr != nullptr)
20578 {
20579 CORE_ADDR addr;
20580
20581 addr = attr->value_as_address ();
20582 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20583 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20584 }
20585 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20586 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20587 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20588 add_symbol_to_list (sym, cu->list_in_scope);
20589 break;
20590 case DW_TAG_subprogram:
20591 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20592 finish_block. */
20593 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20594 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20595 if ((attr2 && (DW_UNSND (attr2) != 0))
20596 || cu->language == language_ada
20597 || cu->language == language_fortran)
20598 {
20599 /* Subprograms marked external are stored as a global symbol.
20600 Ada and Fortran subprograms, whether marked external or
20601 not, are always stored as a global symbol, because we want
20602 to be able to access them globally. For instance, we want
20603 to be able to break on a nested subprogram without having
20604 to specify the context. */
20605 list_to_add = cu->get_builder ()->get_global_symbols ();
20606 }
20607 else
20608 {
20609 list_to_add = cu->list_in_scope;
20610 }
20611 break;
20612 case DW_TAG_inlined_subroutine:
20613 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20614 finish_block. */
20615 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20616 SYMBOL_INLINED (sym) = 1;
20617 list_to_add = cu->list_in_scope;
20618 break;
20619 case DW_TAG_template_value_param:
20620 suppress_add = 1;
20621 /* Fall through. */
20622 case DW_TAG_constant:
20623 case DW_TAG_variable:
20624 case DW_TAG_member:
20625 /* Compilation with minimal debug info may result in
20626 variables with missing type entries. Change the
20627 misleading `void' type to something sensible. */
20628 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20629 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20630
20631 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20632 /* In the case of DW_TAG_member, we should only be called for
20633 static const members. */
20634 if (die->tag == DW_TAG_member)
20635 {
20636 /* dwarf2_add_field uses die_is_declaration,
20637 so we do the same. */
20638 gdb_assert (die_is_declaration (die, cu));
20639 gdb_assert (attr);
20640 }
20641 if (attr != nullptr)
20642 {
20643 dwarf2_const_value (attr, sym, cu);
20644 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20645 if (!suppress_add)
20646 {
20647 if (attr2 && (DW_UNSND (attr2) != 0))
20648 list_to_add = cu->get_builder ()->get_global_symbols ();
20649 else
20650 list_to_add = cu->list_in_scope;
20651 }
20652 break;
20653 }
20654 attr = dwarf2_attr (die, DW_AT_location, cu);
20655 if (attr != nullptr)
20656 {
20657 var_decode_location (attr, sym, cu);
20658 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20659
20660 /* Fortran explicitly imports any global symbols to the local
20661 scope by DW_TAG_common_block. */
20662 if (cu->language == language_fortran && die->parent
20663 && die->parent->tag == DW_TAG_common_block)
20664 attr2 = NULL;
20665
20666 if (SYMBOL_CLASS (sym) == LOC_STATIC
20667 && SYMBOL_VALUE_ADDRESS (sym) == 0
20668 && !dwarf2_per_objfile->has_section_at_zero)
20669 {
20670 /* When a static variable is eliminated by the linker,
20671 the corresponding debug information is not stripped
20672 out, but the variable address is set to null;
20673 do not add such variables into symbol table. */
20674 }
20675 else if (attr2 && (DW_UNSND (attr2) != 0))
20676 {
20677 if (SYMBOL_CLASS (sym) == LOC_STATIC
20678 && (objfile->flags & OBJF_MAINLINE) == 0
20679 && dwarf2_per_objfile->can_copy)
20680 {
20681 /* A global static variable might be subject to
20682 copy relocation. We first check for a local
20683 minsym, though, because maybe the symbol was
20684 marked hidden, in which case this would not
20685 apply. */
20686 bound_minimal_symbol found
20687 = (lookup_minimal_symbol_linkage
20688 (sym->linkage_name (), objfile));
20689 if (found.minsym != nullptr)
20690 sym->maybe_copied = 1;
20691 }
20692
20693 /* A variable with DW_AT_external is never static,
20694 but it may be block-scoped. */
20695 list_to_add
20696 = ((cu->list_in_scope
20697 == cu->get_builder ()->get_file_symbols ())
20698 ? cu->get_builder ()->get_global_symbols ()
20699 : cu->list_in_scope);
20700 }
20701 else
20702 list_to_add = cu->list_in_scope;
20703 }
20704 else
20705 {
20706 /* We do not know the address of this symbol.
20707 If it is an external symbol and we have type information
20708 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20709 The address of the variable will then be determined from
20710 the minimal symbol table whenever the variable is
20711 referenced. */
20712 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20713
20714 /* Fortran explicitly imports any global symbols to the local
20715 scope by DW_TAG_common_block. */
20716 if (cu->language == language_fortran && die->parent
20717 && die->parent->tag == DW_TAG_common_block)
20718 {
20719 /* SYMBOL_CLASS doesn't matter here because
20720 read_common_block is going to reset it. */
20721 if (!suppress_add)
20722 list_to_add = cu->list_in_scope;
20723 }
20724 else if (attr2 && (DW_UNSND (attr2) != 0)
20725 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20726 {
20727 /* A variable with DW_AT_external is never static, but it
20728 may be block-scoped. */
20729 list_to_add
20730 = ((cu->list_in_scope
20731 == cu->get_builder ()->get_file_symbols ())
20732 ? cu->get_builder ()->get_global_symbols ()
20733 : cu->list_in_scope);
20734
20735 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20736 }
20737 else if (!die_is_declaration (die, cu))
20738 {
20739 /* Use the default LOC_OPTIMIZED_OUT class. */
20740 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20741 if (!suppress_add)
20742 list_to_add = cu->list_in_scope;
20743 }
20744 }
20745 break;
20746 case DW_TAG_formal_parameter:
20747 {
20748 /* If we are inside a function, mark this as an argument. If
20749 not, we might be looking at an argument to an inlined function
20750 when we do not have enough information to show inlined frames;
20751 pretend it's a local variable in that case so that the user can
20752 still see it. */
20753 struct context_stack *curr
20754 = cu->get_builder ()->get_current_context_stack ();
20755 if (curr != nullptr && curr->name != nullptr)
20756 SYMBOL_IS_ARGUMENT (sym) = 1;
20757 attr = dwarf2_attr (die, DW_AT_location, cu);
20758 if (attr != nullptr)
20759 {
20760 var_decode_location (attr, sym, cu);
20761 }
20762 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20763 if (attr != nullptr)
20764 {
20765 dwarf2_const_value (attr, sym, cu);
20766 }
20767
20768 list_to_add = cu->list_in_scope;
20769 }
20770 break;
20771 case DW_TAG_unspecified_parameters:
20772 /* From varargs functions; gdb doesn't seem to have any
20773 interest in this information, so just ignore it for now.
20774 (FIXME?) */
20775 break;
20776 case DW_TAG_template_type_param:
20777 suppress_add = 1;
20778 /* Fall through. */
20779 case DW_TAG_class_type:
20780 case DW_TAG_interface_type:
20781 case DW_TAG_structure_type:
20782 case DW_TAG_union_type:
20783 case DW_TAG_set_type:
20784 case DW_TAG_enumeration_type:
20785 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20786 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20787
20788 {
20789 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20790 really ever be static objects: otherwise, if you try
20791 to, say, break of a class's method and you're in a file
20792 which doesn't mention that class, it won't work unless
20793 the check for all static symbols in lookup_symbol_aux
20794 saves you. See the OtherFileClass tests in
20795 gdb.c++/namespace.exp. */
20796
20797 if (!suppress_add)
20798 {
20799 buildsym_compunit *builder = cu->get_builder ();
20800 list_to_add
20801 = (cu->list_in_scope == builder->get_file_symbols ()
20802 && cu->language == language_cplus
20803 ? builder->get_global_symbols ()
20804 : cu->list_in_scope);
20805
20806 /* The semantics of C++ state that "struct foo {
20807 ... }" also defines a typedef for "foo". */
20808 if (cu->language == language_cplus
20809 || cu->language == language_ada
20810 || cu->language == language_d
20811 || cu->language == language_rust)
20812 {
20813 /* The symbol's name is already allocated along
20814 with this objfile, so we don't need to
20815 duplicate it for the type. */
20816 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20817 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20818 }
20819 }
20820 }
20821 break;
20822 case DW_TAG_typedef:
20823 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20824 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20825 list_to_add = cu->list_in_scope;
20826 break;
20827 case DW_TAG_base_type:
20828 case DW_TAG_subrange_type:
20829 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20830 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20831 list_to_add = cu->list_in_scope;
20832 break;
20833 case DW_TAG_enumerator:
20834 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20835 if (attr != nullptr)
20836 {
20837 dwarf2_const_value (attr, sym, cu);
20838 }
20839 {
20840 /* NOTE: carlton/2003-11-10: See comment above in the
20841 DW_TAG_class_type, etc. block. */
20842
20843 list_to_add
20844 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20845 && cu->language == language_cplus
20846 ? cu->get_builder ()->get_global_symbols ()
20847 : cu->list_in_scope);
20848 }
20849 break;
20850 case DW_TAG_imported_declaration:
20851 case DW_TAG_namespace:
20852 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20853 list_to_add = cu->get_builder ()->get_global_symbols ();
20854 break;
20855 case DW_TAG_module:
20856 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20857 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20858 list_to_add = cu->get_builder ()->get_global_symbols ();
20859 break;
20860 case DW_TAG_common_block:
20861 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20862 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20863 add_symbol_to_list (sym, cu->list_in_scope);
20864 break;
20865 default:
20866 /* Not a tag we recognize. Hopefully we aren't processing
20867 trash data, but since we must specifically ignore things
20868 we don't recognize, there is nothing else we should do at
20869 this point. */
20870 complaint (_("unsupported tag: '%s'"),
20871 dwarf_tag_name (die->tag));
20872 break;
20873 }
20874
20875 if (suppress_add)
20876 {
20877 sym->hash_next = objfile->template_symbols;
20878 objfile->template_symbols = sym;
20879 list_to_add = NULL;
20880 }
20881
20882 if (list_to_add != NULL)
20883 add_symbol_to_list (sym, list_to_add);
20884
20885 /* For the benefit of old versions of GCC, check for anonymous
20886 namespaces based on the demangled name. */
20887 if (!cu->processing_has_namespace_info
20888 && cu->language == language_cplus)
20889 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20890 }
20891 return (sym);
20892 }
20893
20894 /* Given an attr with a DW_FORM_dataN value in host byte order,
20895 zero-extend it as appropriate for the symbol's type. The DWARF
20896 standard (v4) is not entirely clear about the meaning of using
20897 DW_FORM_dataN for a constant with a signed type, where the type is
20898 wider than the data. The conclusion of a discussion on the DWARF
20899 list was that this is unspecified. We choose to always zero-extend
20900 because that is the interpretation long in use by GCC. */
20901
20902 static gdb_byte *
20903 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20904 struct dwarf2_cu *cu, LONGEST *value, int bits)
20905 {
20906 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20907 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20908 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20909 LONGEST l = DW_UNSND (attr);
20910
20911 if (bits < sizeof (*value) * 8)
20912 {
20913 l &= ((LONGEST) 1 << bits) - 1;
20914 *value = l;
20915 }
20916 else if (bits == sizeof (*value) * 8)
20917 *value = l;
20918 else
20919 {
20920 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20921 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20922 return bytes;
20923 }
20924
20925 return NULL;
20926 }
20927
20928 /* Read a constant value from an attribute. Either set *VALUE, or if
20929 the value does not fit in *VALUE, set *BYTES - either already
20930 allocated on the objfile obstack, or newly allocated on OBSTACK,
20931 or, set *BATON, if we translated the constant to a location
20932 expression. */
20933
20934 static void
20935 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20936 const char *name, struct obstack *obstack,
20937 struct dwarf2_cu *cu,
20938 LONGEST *value, const gdb_byte **bytes,
20939 struct dwarf2_locexpr_baton **baton)
20940 {
20941 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20942 struct comp_unit_head *cu_header = &cu->header;
20943 struct dwarf_block *blk;
20944 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20945 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20946
20947 *value = 0;
20948 *bytes = NULL;
20949 *baton = NULL;
20950
20951 switch (attr->form)
20952 {
20953 case DW_FORM_addr:
20954 case DW_FORM_addrx:
20955 case DW_FORM_GNU_addr_index:
20956 {
20957 gdb_byte *data;
20958
20959 if (TYPE_LENGTH (type) != cu_header->addr_size)
20960 dwarf2_const_value_length_mismatch_complaint (name,
20961 cu_header->addr_size,
20962 TYPE_LENGTH (type));
20963 /* Symbols of this form are reasonably rare, so we just
20964 piggyback on the existing location code rather than writing
20965 a new implementation of symbol_computed_ops. */
20966 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20967 (*baton)->per_cu = cu->per_cu;
20968 gdb_assert ((*baton)->per_cu);
20969
20970 (*baton)->size = 2 + cu_header->addr_size;
20971 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20972 (*baton)->data = data;
20973
20974 data[0] = DW_OP_addr;
20975 store_unsigned_integer (&data[1], cu_header->addr_size,
20976 byte_order, DW_ADDR (attr));
20977 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20978 }
20979 break;
20980 case DW_FORM_string:
20981 case DW_FORM_strp:
20982 case DW_FORM_strx:
20983 case DW_FORM_GNU_str_index:
20984 case DW_FORM_GNU_strp_alt:
20985 /* DW_STRING is already allocated on the objfile obstack, point
20986 directly to it. */
20987 *bytes = (const gdb_byte *) DW_STRING (attr);
20988 break;
20989 case DW_FORM_block1:
20990 case DW_FORM_block2:
20991 case DW_FORM_block4:
20992 case DW_FORM_block:
20993 case DW_FORM_exprloc:
20994 case DW_FORM_data16:
20995 blk = DW_BLOCK (attr);
20996 if (TYPE_LENGTH (type) != blk->size)
20997 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20998 TYPE_LENGTH (type));
20999 *bytes = blk->data;
21000 break;
21001
21002 /* The DW_AT_const_value attributes are supposed to carry the
21003 symbol's value "represented as it would be on the target
21004 architecture." By the time we get here, it's already been
21005 converted to host endianness, so we just need to sign- or
21006 zero-extend it as appropriate. */
21007 case DW_FORM_data1:
21008 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21009 break;
21010 case DW_FORM_data2:
21011 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21012 break;
21013 case DW_FORM_data4:
21014 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21015 break;
21016 case DW_FORM_data8:
21017 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21018 break;
21019
21020 case DW_FORM_sdata:
21021 case DW_FORM_implicit_const:
21022 *value = DW_SND (attr);
21023 break;
21024
21025 case DW_FORM_udata:
21026 *value = DW_UNSND (attr);
21027 break;
21028
21029 default:
21030 complaint (_("unsupported const value attribute form: '%s'"),
21031 dwarf_form_name (attr->form));
21032 *value = 0;
21033 break;
21034 }
21035 }
21036
21037
21038 /* Copy constant value from an attribute to a symbol. */
21039
21040 static void
21041 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21042 struct dwarf2_cu *cu)
21043 {
21044 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21045 LONGEST value;
21046 const gdb_byte *bytes;
21047 struct dwarf2_locexpr_baton *baton;
21048
21049 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21050 sym->print_name (),
21051 &objfile->objfile_obstack, cu,
21052 &value, &bytes, &baton);
21053
21054 if (baton != NULL)
21055 {
21056 SYMBOL_LOCATION_BATON (sym) = baton;
21057 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21058 }
21059 else if (bytes != NULL)
21060 {
21061 SYMBOL_VALUE_BYTES (sym) = bytes;
21062 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21063 }
21064 else
21065 {
21066 SYMBOL_VALUE (sym) = value;
21067 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21068 }
21069 }
21070
21071 /* Return the type of the die in question using its DW_AT_type attribute. */
21072
21073 static struct type *
21074 die_type (struct die_info *die, struct dwarf2_cu *cu)
21075 {
21076 struct attribute *type_attr;
21077
21078 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21079 if (!type_attr)
21080 {
21081 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21082 /* A missing DW_AT_type represents a void type. */
21083 return objfile_type (objfile)->builtin_void;
21084 }
21085
21086 return lookup_die_type (die, type_attr, cu);
21087 }
21088
21089 /* True iff CU's producer generates GNAT Ada auxiliary information
21090 that allows to find parallel types through that information instead
21091 of having to do expensive parallel lookups by type name. */
21092
21093 static int
21094 need_gnat_info (struct dwarf2_cu *cu)
21095 {
21096 /* Assume that the Ada compiler was GNAT, which always produces
21097 the auxiliary information. */
21098 return (cu->language == language_ada);
21099 }
21100
21101 /* Return the auxiliary type of the die in question using its
21102 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21103 attribute is not present. */
21104
21105 static struct type *
21106 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21107 {
21108 struct attribute *type_attr;
21109
21110 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21111 if (!type_attr)
21112 return NULL;
21113
21114 return lookup_die_type (die, type_attr, cu);
21115 }
21116
21117 /* If DIE has a descriptive_type attribute, then set the TYPE's
21118 descriptive type accordingly. */
21119
21120 static void
21121 set_descriptive_type (struct type *type, struct die_info *die,
21122 struct dwarf2_cu *cu)
21123 {
21124 struct type *descriptive_type = die_descriptive_type (die, cu);
21125
21126 if (descriptive_type)
21127 {
21128 ALLOCATE_GNAT_AUX_TYPE (type);
21129 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21130 }
21131 }
21132
21133 /* Return the containing type of the die in question using its
21134 DW_AT_containing_type attribute. */
21135
21136 static struct type *
21137 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21138 {
21139 struct attribute *type_attr;
21140 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21141
21142 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21143 if (!type_attr)
21144 error (_("Dwarf Error: Problem turning containing type into gdb type "
21145 "[in module %s]"), objfile_name (objfile));
21146
21147 return lookup_die_type (die, type_attr, cu);
21148 }
21149
21150 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21151
21152 static struct type *
21153 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21154 {
21155 struct dwarf2_per_objfile *dwarf2_per_objfile
21156 = cu->per_cu->dwarf2_per_objfile;
21157 struct objfile *objfile = dwarf2_per_objfile->objfile;
21158 char *saved;
21159
21160 std::string message
21161 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21162 objfile_name (objfile),
21163 sect_offset_str (cu->header.sect_off),
21164 sect_offset_str (die->sect_off));
21165 saved = obstack_strdup (&objfile->objfile_obstack, message);
21166
21167 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21168 }
21169
21170 /* Look up the type of DIE in CU using its type attribute ATTR.
21171 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21172 DW_AT_containing_type.
21173 If there is no type substitute an error marker. */
21174
21175 static struct type *
21176 lookup_die_type (struct die_info *die, const struct attribute *attr,
21177 struct dwarf2_cu *cu)
21178 {
21179 struct dwarf2_per_objfile *dwarf2_per_objfile
21180 = cu->per_cu->dwarf2_per_objfile;
21181 struct objfile *objfile = dwarf2_per_objfile->objfile;
21182 struct type *this_type;
21183
21184 gdb_assert (attr->name == DW_AT_type
21185 || attr->name == DW_AT_GNAT_descriptive_type
21186 || attr->name == DW_AT_containing_type);
21187
21188 /* First see if we have it cached. */
21189
21190 if (attr->form == DW_FORM_GNU_ref_alt)
21191 {
21192 struct dwarf2_per_cu_data *per_cu;
21193 sect_offset sect_off = attr->get_ref_die_offset ();
21194
21195 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21196 dwarf2_per_objfile);
21197 this_type = get_die_type_at_offset (sect_off, per_cu);
21198 }
21199 else if (attr->form_is_ref ())
21200 {
21201 sect_offset sect_off = attr->get_ref_die_offset ();
21202
21203 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21204 }
21205 else if (attr->form == DW_FORM_ref_sig8)
21206 {
21207 ULONGEST signature = DW_SIGNATURE (attr);
21208
21209 return get_signatured_type (die, signature, cu);
21210 }
21211 else
21212 {
21213 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21214 " at %s [in module %s]"),
21215 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21216 objfile_name (objfile));
21217 return build_error_marker_type (cu, die);
21218 }
21219
21220 /* If not cached we need to read it in. */
21221
21222 if (this_type == NULL)
21223 {
21224 struct die_info *type_die = NULL;
21225 struct dwarf2_cu *type_cu = cu;
21226
21227 if (attr->form_is_ref ())
21228 type_die = follow_die_ref (die, attr, &type_cu);
21229 if (type_die == NULL)
21230 return build_error_marker_type (cu, die);
21231 /* If we find the type now, it's probably because the type came
21232 from an inter-CU reference and the type's CU got expanded before
21233 ours. */
21234 this_type = read_type_die (type_die, type_cu);
21235 }
21236
21237 /* If we still don't have a type use an error marker. */
21238
21239 if (this_type == NULL)
21240 return build_error_marker_type (cu, die);
21241
21242 return this_type;
21243 }
21244
21245 /* Return the type in DIE, CU.
21246 Returns NULL for invalid types.
21247
21248 This first does a lookup in die_type_hash,
21249 and only reads the die in if necessary.
21250
21251 NOTE: This can be called when reading in partial or full symbols. */
21252
21253 static struct type *
21254 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21255 {
21256 struct type *this_type;
21257
21258 this_type = get_die_type (die, cu);
21259 if (this_type)
21260 return this_type;
21261
21262 return read_type_die_1 (die, cu);
21263 }
21264
21265 /* Read the type in DIE, CU.
21266 Returns NULL for invalid types. */
21267
21268 static struct type *
21269 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21270 {
21271 struct type *this_type = NULL;
21272
21273 switch (die->tag)
21274 {
21275 case DW_TAG_class_type:
21276 case DW_TAG_interface_type:
21277 case DW_TAG_structure_type:
21278 case DW_TAG_union_type:
21279 this_type = read_structure_type (die, cu);
21280 break;
21281 case DW_TAG_enumeration_type:
21282 this_type = read_enumeration_type (die, cu);
21283 break;
21284 case DW_TAG_subprogram:
21285 case DW_TAG_subroutine_type:
21286 case DW_TAG_inlined_subroutine:
21287 this_type = read_subroutine_type (die, cu);
21288 break;
21289 case DW_TAG_array_type:
21290 this_type = read_array_type (die, cu);
21291 break;
21292 case DW_TAG_set_type:
21293 this_type = read_set_type (die, cu);
21294 break;
21295 case DW_TAG_pointer_type:
21296 this_type = read_tag_pointer_type (die, cu);
21297 break;
21298 case DW_TAG_ptr_to_member_type:
21299 this_type = read_tag_ptr_to_member_type (die, cu);
21300 break;
21301 case DW_TAG_reference_type:
21302 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21303 break;
21304 case DW_TAG_rvalue_reference_type:
21305 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21306 break;
21307 case DW_TAG_const_type:
21308 this_type = read_tag_const_type (die, cu);
21309 break;
21310 case DW_TAG_volatile_type:
21311 this_type = read_tag_volatile_type (die, cu);
21312 break;
21313 case DW_TAG_restrict_type:
21314 this_type = read_tag_restrict_type (die, cu);
21315 break;
21316 case DW_TAG_string_type:
21317 this_type = read_tag_string_type (die, cu);
21318 break;
21319 case DW_TAG_typedef:
21320 this_type = read_typedef (die, cu);
21321 break;
21322 case DW_TAG_subrange_type:
21323 this_type = read_subrange_type (die, cu);
21324 break;
21325 case DW_TAG_base_type:
21326 this_type = read_base_type (die, cu);
21327 break;
21328 case DW_TAG_unspecified_type:
21329 this_type = read_unspecified_type (die, cu);
21330 break;
21331 case DW_TAG_namespace:
21332 this_type = read_namespace_type (die, cu);
21333 break;
21334 case DW_TAG_module:
21335 this_type = read_module_type (die, cu);
21336 break;
21337 case DW_TAG_atomic_type:
21338 this_type = read_tag_atomic_type (die, cu);
21339 break;
21340 default:
21341 complaint (_("unexpected tag in read_type_die: '%s'"),
21342 dwarf_tag_name (die->tag));
21343 break;
21344 }
21345
21346 return this_type;
21347 }
21348
21349 /* See if we can figure out if the class lives in a namespace. We do
21350 this by looking for a member function; its demangled name will
21351 contain namespace info, if there is any.
21352 Return the computed name or NULL.
21353 Space for the result is allocated on the objfile's obstack.
21354 This is the full-die version of guess_partial_die_structure_name.
21355 In this case we know DIE has no useful parent. */
21356
21357 static const char *
21358 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21359 {
21360 struct die_info *spec_die;
21361 struct dwarf2_cu *spec_cu;
21362 struct die_info *child;
21363 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21364
21365 spec_cu = cu;
21366 spec_die = die_specification (die, &spec_cu);
21367 if (spec_die != NULL)
21368 {
21369 die = spec_die;
21370 cu = spec_cu;
21371 }
21372
21373 for (child = die->child;
21374 child != NULL;
21375 child = child->sibling)
21376 {
21377 if (child->tag == DW_TAG_subprogram)
21378 {
21379 const char *linkage_name = dw2_linkage_name (child, cu);
21380
21381 if (linkage_name != NULL)
21382 {
21383 gdb::unique_xmalloc_ptr<char> actual_name
21384 (language_class_name_from_physname (cu->language_defn,
21385 linkage_name));
21386 const char *name = NULL;
21387
21388 if (actual_name != NULL)
21389 {
21390 const char *die_name = dwarf2_name (die, cu);
21391
21392 if (die_name != NULL
21393 && strcmp (die_name, actual_name.get ()) != 0)
21394 {
21395 /* Strip off the class name from the full name.
21396 We want the prefix. */
21397 int die_name_len = strlen (die_name);
21398 int actual_name_len = strlen (actual_name.get ());
21399 const char *ptr = actual_name.get ();
21400
21401 /* Test for '::' as a sanity check. */
21402 if (actual_name_len > die_name_len + 2
21403 && ptr[actual_name_len - die_name_len - 1] == ':')
21404 name = obstack_strndup (
21405 &objfile->per_bfd->storage_obstack,
21406 ptr, actual_name_len - die_name_len - 2);
21407 }
21408 }
21409 return name;
21410 }
21411 }
21412 }
21413
21414 return NULL;
21415 }
21416
21417 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21418 prefix part in such case. See
21419 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21420
21421 static const char *
21422 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21423 {
21424 struct attribute *attr;
21425 const char *base;
21426
21427 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21428 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21429 return NULL;
21430
21431 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21432 return NULL;
21433
21434 attr = dw2_linkage_name_attr (die, cu);
21435 if (attr == NULL || DW_STRING (attr) == NULL)
21436 return NULL;
21437
21438 /* dwarf2_name had to be already called. */
21439 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21440
21441 /* Strip the base name, keep any leading namespaces/classes. */
21442 base = strrchr (DW_STRING (attr), ':');
21443 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21444 return "";
21445
21446 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21447 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21448 DW_STRING (attr),
21449 &base[-1] - DW_STRING (attr));
21450 }
21451
21452 /* Return the name of the namespace/class that DIE is defined within,
21453 or "" if we can't tell. The caller should not xfree the result.
21454
21455 For example, if we're within the method foo() in the following
21456 code:
21457
21458 namespace N {
21459 class C {
21460 void foo () {
21461 }
21462 };
21463 }
21464
21465 then determine_prefix on foo's die will return "N::C". */
21466
21467 static const char *
21468 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21469 {
21470 struct dwarf2_per_objfile *dwarf2_per_objfile
21471 = cu->per_cu->dwarf2_per_objfile;
21472 struct die_info *parent, *spec_die;
21473 struct dwarf2_cu *spec_cu;
21474 struct type *parent_type;
21475 const char *retval;
21476
21477 if (cu->language != language_cplus
21478 && cu->language != language_fortran && cu->language != language_d
21479 && cu->language != language_rust)
21480 return "";
21481
21482 retval = anonymous_struct_prefix (die, cu);
21483 if (retval)
21484 return retval;
21485
21486 /* We have to be careful in the presence of DW_AT_specification.
21487 For example, with GCC 3.4, given the code
21488
21489 namespace N {
21490 void foo() {
21491 // Definition of N::foo.
21492 }
21493 }
21494
21495 then we'll have a tree of DIEs like this:
21496
21497 1: DW_TAG_compile_unit
21498 2: DW_TAG_namespace // N
21499 3: DW_TAG_subprogram // declaration of N::foo
21500 4: DW_TAG_subprogram // definition of N::foo
21501 DW_AT_specification // refers to die #3
21502
21503 Thus, when processing die #4, we have to pretend that we're in
21504 the context of its DW_AT_specification, namely the contex of die
21505 #3. */
21506 spec_cu = cu;
21507 spec_die = die_specification (die, &spec_cu);
21508 if (spec_die == NULL)
21509 parent = die->parent;
21510 else
21511 {
21512 parent = spec_die->parent;
21513 cu = spec_cu;
21514 }
21515
21516 if (parent == NULL)
21517 return "";
21518 else if (parent->building_fullname)
21519 {
21520 const char *name;
21521 const char *parent_name;
21522
21523 /* It has been seen on RealView 2.2 built binaries,
21524 DW_TAG_template_type_param types actually _defined_ as
21525 children of the parent class:
21526
21527 enum E {};
21528 template class <class Enum> Class{};
21529 Class<enum E> class_e;
21530
21531 1: DW_TAG_class_type (Class)
21532 2: DW_TAG_enumeration_type (E)
21533 3: DW_TAG_enumerator (enum1:0)
21534 3: DW_TAG_enumerator (enum2:1)
21535 ...
21536 2: DW_TAG_template_type_param
21537 DW_AT_type DW_FORM_ref_udata (E)
21538
21539 Besides being broken debug info, it can put GDB into an
21540 infinite loop. Consider:
21541
21542 When we're building the full name for Class<E>, we'll start
21543 at Class, and go look over its template type parameters,
21544 finding E. We'll then try to build the full name of E, and
21545 reach here. We're now trying to build the full name of E,
21546 and look over the parent DIE for containing scope. In the
21547 broken case, if we followed the parent DIE of E, we'd again
21548 find Class, and once again go look at its template type
21549 arguments, etc., etc. Simply don't consider such parent die
21550 as source-level parent of this die (it can't be, the language
21551 doesn't allow it), and break the loop here. */
21552 name = dwarf2_name (die, cu);
21553 parent_name = dwarf2_name (parent, cu);
21554 complaint (_("template param type '%s' defined within parent '%s'"),
21555 name ? name : "<unknown>",
21556 parent_name ? parent_name : "<unknown>");
21557 return "";
21558 }
21559 else
21560 switch (parent->tag)
21561 {
21562 case DW_TAG_namespace:
21563 parent_type = read_type_die (parent, cu);
21564 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21565 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21566 Work around this problem here. */
21567 if (cu->language == language_cplus
21568 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21569 return "";
21570 /* We give a name to even anonymous namespaces. */
21571 return TYPE_NAME (parent_type);
21572 case DW_TAG_class_type:
21573 case DW_TAG_interface_type:
21574 case DW_TAG_structure_type:
21575 case DW_TAG_union_type:
21576 case DW_TAG_module:
21577 parent_type = read_type_die (parent, cu);
21578 if (TYPE_NAME (parent_type) != NULL)
21579 return TYPE_NAME (parent_type);
21580 else
21581 /* An anonymous structure is only allowed non-static data
21582 members; no typedefs, no member functions, et cetera.
21583 So it does not need a prefix. */
21584 return "";
21585 case DW_TAG_compile_unit:
21586 case DW_TAG_partial_unit:
21587 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21588 if (cu->language == language_cplus
21589 && !dwarf2_per_objfile->types.empty ()
21590 && die->child != NULL
21591 && (die->tag == DW_TAG_class_type
21592 || die->tag == DW_TAG_structure_type
21593 || die->tag == DW_TAG_union_type))
21594 {
21595 const char *name = guess_full_die_structure_name (die, cu);
21596 if (name != NULL)
21597 return name;
21598 }
21599 return "";
21600 case DW_TAG_subprogram:
21601 /* Nested subroutines in Fortran get a prefix with the name
21602 of the parent's subroutine. */
21603 if (cu->language == language_fortran)
21604 {
21605 if ((die->tag == DW_TAG_subprogram)
21606 && (dwarf2_name (parent, cu) != NULL))
21607 return dwarf2_name (parent, cu);
21608 }
21609 return determine_prefix (parent, cu);
21610 case DW_TAG_enumeration_type:
21611 parent_type = read_type_die (parent, cu);
21612 if (TYPE_DECLARED_CLASS (parent_type))
21613 {
21614 if (TYPE_NAME (parent_type) != NULL)
21615 return TYPE_NAME (parent_type);
21616 return "";
21617 }
21618 /* Fall through. */
21619 default:
21620 return determine_prefix (parent, cu);
21621 }
21622 }
21623
21624 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21625 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21626 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21627 an obconcat, otherwise allocate storage for the result. The CU argument is
21628 used to determine the language and hence, the appropriate separator. */
21629
21630 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21631
21632 static char *
21633 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21634 int physname, struct dwarf2_cu *cu)
21635 {
21636 const char *lead = "";
21637 const char *sep;
21638
21639 if (suffix == NULL || suffix[0] == '\0'
21640 || prefix == NULL || prefix[0] == '\0')
21641 sep = "";
21642 else if (cu->language == language_d)
21643 {
21644 /* For D, the 'main' function could be defined in any module, but it
21645 should never be prefixed. */
21646 if (strcmp (suffix, "D main") == 0)
21647 {
21648 prefix = "";
21649 sep = "";
21650 }
21651 else
21652 sep = ".";
21653 }
21654 else if (cu->language == language_fortran && physname)
21655 {
21656 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21657 DW_AT_MIPS_linkage_name is preferred and used instead. */
21658
21659 lead = "__";
21660 sep = "_MOD_";
21661 }
21662 else
21663 sep = "::";
21664
21665 if (prefix == NULL)
21666 prefix = "";
21667 if (suffix == NULL)
21668 suffix = "";
21669
21670 if (obs == NULL)
21671 {
21672 char *retval
21673 = ((char *)
21674 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21675
21676 strcpy (retval, lead);
21677 strcat (retval, prefix);
21678 strcat (retval, sep);
21679 strcat (retval, suffix);
21680 return retval;
21681 }
21682 else
21683 {
21684 /* We have an obstack. */
21685 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21686 }
21687 }
21688
21689 /* Get name of a die, return NULL if not found. */
21690
21691 static const char *
21692 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21693 struct objfile *objfile)
21694 {
21695 if (name && cu->language == language_cplus)
21696 {
21697 std::string canon_name = cp_canonicalize_string (name);
21698
21699 if (!canon_name.empty ())
21700 {
21701 if (canon_name != name)
21702 name = objfile->intern (canon_name);
21703 }
21704 }
21705
21706 return name;
21707 }
21708
21709 /* Get name of a die, return NULL if not found.
21710 Anonymous namespaces are converted to their magic string. */
21711
21712 static const char *
21713 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21714 {
21715 struct attribute *attr;
21716 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21717
21718 attr = dwarf2_attr (die, DW_AT_name, cu);
21719 if ((!attr || !DW_STRING (attr))
21720 && die->tag != DW_TAG_namespace
21721 && die->tag != DW_TAG_class_type
21722 && die->tag != DW_TAG_interface_type
21723 && die->tag != DW_TAG_structure_type
21724 && die->tag != DW_TAG_union_type)
21725 return NULL;
21726
21727 switch (die->tag)
21728 {
21729 case DW_TAG_compile_unit:
21730 case DW_TAG_partial_unit:
21731 /* Compilation units have a DW_AT_name that is a filename, not
21732 a source language identifier. */
21733 case DW_TAG_enumeration_type:
21734 case DW_TAG_enumerator:
21735 /* These tags always have simple identifiers already; no need
21736 to canonicalize them. */
21737 return DW_STRING (attr);
21738
21739 case DW_TAG_namespace:
21740 if (attr != NULL && DW_STRING (attr) != NULL)
21741 return DW_STRING (attr);
21742 return CP_ANONYMOUS_NAMESPACE_STR;
21743
21744 case DW_TAG_class_type:
21745 case DW_TAG_interface_type:
21746 case DW_TAG_structure_type:
21747 case DW_TAG_union_type:
21748 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21749 structures or unions. These were of the form "._%d" in GCC 4.1,
21750 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21751 and GCC 4.4. We work around this problem by ignoring these. */
21752 if (attr && DW_STRING (attr)
21753 && (startswith (DW_STRING (attr), "._")
21754 || startswith (DW_STRING (attr), "<anonymous")))
21755 return NULL;
21756
21757 /* GCC might emit a nameless typedef that has a linkage name. See
21758 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21759 if (!attr || DW_STRING (attr) == NULL)
21760 {
21761 attr = dw2_linkage_name_attr (die, cu);
21762 if (attr == NULL || DW_STRING (attr) == NULL)
21763 return NULL;
21764
21765 /* Avoid demangling DW_STRING (attr) the second time on a second
21766 call for the same DIE. */
21767 if (!DW_STRING_IS_CANONICAL (attr))
21768 {
21769 gdb::unique_xmalloc_ptr<char> demangled
21770 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21771 if (demangled == nullptr)
21772 return nullptr;
21773
21774 DW_STRING (attr) = objfile->intern (demangled.get ());
21775 DW_STRING_IS_CANONICAL (attr) = 1;
21776 }
21777
21778 /* Strip any leading namespaces/classes, keep only the base name.
21779 DW_AT_name for named DIEs does not contain the prefixes. */
21780 const char *base = strrchr (DW_STRING (attr), ':');
21781 if (base && base > DW_STRING (attr) && base[-1] == ':')
21782 return &base[1];
21783 else
21784 return DW_STRING (attr);
21785 }
21786 break;
21787
21788 default:
21789 break;
21790 }
21791
21792 if (!DW_STRING_IS_CANONICAL (attr))
21793 {
21794 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21795 objfile);
21796 DW_STRING_IS_CANONICAL (attr) = 1;
21797 }
21798 return DW_STRING (attr);
21799 }
21800
21801 /* Return the die that this die in an extension of, or NULL if there
21802 is none. *EXT_CU is the CU containing DIE on input, and the CU
21803 containing the return value on output. */
21804
21805 static struct die_info *
21806 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21807 {
21808 struct attribute *attr;
21809
21810 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21811 if (attr == NULL)
21812 return NULL;
21813
21814 return follow_die_ref (die, attr, ext_cu);
21815 }
21816
21817 static void
21818 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21819 {
21820 unsigned int i;
21821
21822 print_spaces (indent, f);
21823 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21824 dwarf_tag_name (die->tag), die->abbrev,
21825 sect_offset_str (die->sect_off));
21826
21827 if (die->parent != NULL)
21828 {
21829 print_spaces (indent, f);
21830 fprintf_unfiltered (f, " parent at offset: %s\n",
21831 sect_offset_str (die->parent->sect_off));
21832 }
21833
21834 print_spaces (indent, f);
21835 fprintf_unfiltered (f, " has children: %s\n",
21836 dwarf_bool_name (die->child != NULL));
21837
21838 print_spaces (indent, f);
21839 fprintf_unfiltered (f, " attributes:\n");
21840
21841 for (i = 0; i < die->num_attrs; ++i)
21842 {
21843 print_spaces (indent, f);
21844 fprintf_unfiltered (f, " %s (%s) ",
21845 dwarf_attr_name (die->attrs[i].name),
21846 dwarf_form_name (die->attrs[i].form));
21847
21848 switch (die->attrs[i].form)
21849 {
21850 case DW_FORM_addr:
21851 case DW_FORM_addrx:
21852 case DW_FORM_GNU_addr_index:
21853 fprintf_unfiltered (f, "address: ");
21854 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21855 break;
21856 case DW_FORM_block2:
21857 case DW_FORM_block4:
21858 case DW_FORM_block:
21859 case DW_FORM_block1:
21860 fprintf_unfiltered (f, "block: size %s",
21861 pulongest (DW_BLOCK (&die->attrs[i])->size));
21862 break;
21863 case DW_FORM_exprloc:
21864 fprintf_unfiltered (f, "expression: size %s",
21865 pulongest (DW_BLOCK (&die->attrs[i])->size));
21866 break;
21867 case DW_FORM_data16:
21868 fprintf_unfiltered (f, "constant of 16 bytes");
21869 break;
21870 case DW_FORM_ref_addr:
21871 fprintf_unfiltered (f, "ref address: ");
21872 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21873 break;
21874 case DW_FORM_GNU_ref_alt:
21875 fprintf_unfiltered (f, "alt ref address: ");
21876 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21877 break;
21878 case DW_FORM_ref1:
21879 case DW_FORM_ref2:
21880 case DW_FORM_ref4:
21881 case DW_FORM_ref8:
21882 case DW_FORM_ref_udata:
21883 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21884 (long) (DW_UNSND (&die->attrs[i])));
21885 break;
21886 case DW_FORM_data1:
21887 case DW_FORM_data2:
21888 case DW_FORM_data4:
21889 case DW_FORM_data8:
21890 case DW_FORM_udata:
21891 case DW_FORM_sdata:
21892 fprintf_unfiltered (f, "constant: %s",
21893 pulongest (DW_UNSND (&die->attrs[i])));
21894 break;
21895 case DW_FORM_sec_offset:
21896 fprintf_unfiltered (f, "section offset: %s",
21897 pulongest (DW_UNSND (&die->attrs[i])));
21898 break;
21899 case DW_FORM_ref_sig8:
21900 fprintf_unfiltered (f, "signature: %s",
21901 hex_string (DW_SIGNATURE (&die->attrs[i])));
21902 break;
21903 case DW_FORM_string:
21904 case DW_FORM_strp:
21905 case DW_FORM_line_strp:
21906 case DW_FORM_strx:
21907 case DW_FORM_GNU_str_index:
21908 case DW_FORM_GNU_strp_alt:
21909 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21910 DW_STRING (&die->attrs[i])
21911 ? DW_STRING (&die->attrs[i]) : "",
21912 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21913 break;
21914 case DW_FORM_flag:
21915 if (DW_UNSND (&die->attrs[i]))
21916 fprintf_unfiltered (f, "flag: TRUE");
21917 else
21918 fprintf_unfiltered (f, "flag: FALSE");
21919 break;
21920 case DW_FORM_flag_present:
21921 fprintf_unfiltered (f, "flag: TRUE");
21922 break;
21923 case DW_FORM_indirect:
21924 /* The reader will have reduced the indirect form to
21925 the "base form" so this form should not occur. */
21926 fprintf_unfiltered (f,
21927 "unexpected attribute form: DW_FORM_indirect");
21928 break;
21929 case DW_FORM_implicit_const:
21930 fprintf_unfiltered (f, "constant: %s",
21931 plongest (DW_SND (&die->attrs[i])));
21932 break;
21933 default:
21934 fprintf_unfiltered (f, "unsupported attribute form: %d.",
21935 die->attrs[i].form);
21936 break;
21937 }
21938 fprintf_unfiltered (f, "\n");
21939 }
21940 }
21941
21942 static void
21943 dump_die_for_error (struct die_info *die)
21944 {
21945 dump_die_shallow (gdb_stderr, 0, die);
21946 }
21947
21948 static void
21949 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21950 {
21951 int indent = level * 4;
21952
21953 gdb_assert (die != NULL);
21954
21955 if (level >= max_level)
21956 return;
21957
21958 dump_die_shallow (f, indent, die);
21959
21960 if (die->child != NULL)
21961 {
21962 print_spaces (indent, f);
21963 fprintf_unfiltered (f, " Children:");
21964 if (level + 1 < max_level)
21965 {
21966 fprintf_unfiltered (f, "\n");
21967 dump_die_1 (f, level + 1, max_level, die->child);
21968 }
21969 else
21970 {
21971 fprintf_unfiltered (f,
21972 " [not printed, max nesting level reached]\n");
21973 }
21974 }
21975
21976 if (die->sibling != NULL && level > 0)
21977 {
21978 dump_die_1 (f, level, max_level, die->sibling);
21979 }
21980 }
21981
21982 /* This is called from the pdie macro in gdbinit.in.
21983 It's not static so gcc will keep a copy callable from gdb. */
21984
21985 void
21986 dump_die (struct die_info *die, int max_level)
21987 {
21988 dump_die_1 (gdb_stdlog, 0, max_level, die);
21989 }
21990
21991 static void
21992 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
21993 {
21994 void **slot;
21995
21996 slot = htab_find_slot_with_hash (cu->die_hash, die,
21997 to_underlying (die->sect_off),
21998 INSERT);
21999
22000 *slot = die;
22001 }
22002
22003 /* Follow reference or signature attribute ATTR of SRC_DIE.
22004 On entry *REF_CU is the CU of SRC_DIE.
22005 On exit *REF_CU is the CU of the result. */
22006
22007 static struct die_info *
22008 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22009 struct dwarf2_cu **ref_cu)
22010 {
22011 struct die_info *die;
22012
22013 if (attr->form_is_ref ())
22014 die = follow_die_ref (src_die, attr, ref_cu);
22015 else if (attr->form == DW_FORM_ref_sig8)
22016 die = follow_die_sig (src_die, attr, ref_cu);
22017 else
22018 {
22019 dump_die_for_error (src_die);
22020 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22021 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22022 }
22023
22024 return die;
22025 }
22026
22027 /* Follow reference OFFSET.
22028 On entry *REF_CU is the CU of the source die referencing OFFSET.
22029 On exit *REF_CU is the CU of the result.
22030 Returns NULL if OFFSET is invalid. */
22031
22032 static struct die_info *
22033 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22034 struct dwarf2_cu **ref_cu)
22035 {
22036 struct die_info temp_die;
22037 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22038 struct dwarf2_per_objfile *dwarf2_per_objfile
22039 = cu->per_cu->dwarf2_per_objfile;
22040
22041 gdb_assert (cu->per_cu != NULL);
22042
22043 target_cu = cu;
22044
22045 if (cu->per_cu->is_debug_types)
22046 {
22047 /* .debug_types CUs cannot reference anything outside their CU.
22048 If they need to, they have to reference a signatured type via
22049 DW_FORM_ref_sig8. */
22050 if (!cu->header.offset_in_cu_p (sect_off))
22051 return NULL;
22052 }
22053 else if (offset_in_dwz != cu->per_cu->is_dwz
22054 || !cu->header.offset_in_cu_p (sect_off))
22055 {
22056 struct dwarf2_per_cu_data *per_cu;
22057
22058 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22059 dwarf2_per_objfile);
22060
22061 /* If necessary, add it to the queue and load its DIEs. */
22062 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22063 load_full_comp_unit (per_cu, false, cu->language);
22064
22065 target_cu = per_cu->cu;
22066 }
22067 else if (cu->dies == NULL)
22068 {
22069 /* We're loading full DIEs during partial symbol reading. */
22070 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22071 load_full_comp_unit (cu->per_cu, false, language_minimal);
22072 }
22073
22074 *ref_cu = target_cu;
22075 temp_die.sect_off = sect_off;
22076
22077 if (target_cu != cu)
22078 target_cu->ancestor = cu;
22079
22080 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22081 &temp_die,
22082 to_underlying (sect_off));
22083 }
22084
22085 /* Follow reference attribute ATTR of SRC_DIE.
22086 On entry *REF_CU is the CU of SRC_DIE.
22087 On exit *REF_CU is the CU of the result. */
22088
22089 static struct die_info *
22090 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22091 struct dwarf2_cu **ref_cu)
22092 {
22093 sect_offset sect_off = attr->get_ref_die_offset ();
22094 struct dwarf2_cu *cu = *ref_cu;
22095 struct die_info *die;
22096
22097 die = follow_die_offset (sect_off,
22098 (attr->form == DW_FORM_GNU_ref_alt
22099 || cu->per_cu->is_dwz),
22100 ref_cu);
22101 if (!die)
22102 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22103 "at %s [in module %s]"),
22104 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22105 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22106
22107 return die;
22108 }
22109
22110 /* See read.h. */
22111
22112 struct dwarf2_locexpr_baton
22113 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22114 dwarf2_per_cu_data *per_cu,
22115 CORE_ADDR (*get_frame_pc) (void *baton),
22116 void *baton, bool resolve_abstract_p)
22117 {
22118 struct dwarf2_cu *cu;
22119 struct die_info *die;
22120 struct attribute *attr;
22121 struct dwarf2_locexpr_baton retval;
22122 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22123 struct objfile *objfile = dwarf2_per_objfile->objfile;
22124
22125 if (per_cu->cu == NULL)
22126 load_cu (per_cu, false);
22127 cu = per_cu->cu;
22128 if (cu == NULL)
22129 {
22130 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22131 Instead just throw an error, not much else we can do. */
22132 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22133 sect_offset_str (sect_off), objfile_name (objfile));
22134 }
22135
22136 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22137 if (!die)
22138 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22139 sect_offset_str (sect_off), objfile_name (objfile));
22140
22141 attr = dwarf2_attr (die, DW_AT_location, cu);
22142 if (!attr && resolve_abstract_p
22143 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
22144 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22145 {
22146 CORE_ADDR pc = (*get_frame_pc) (baton);
22147 CORE_ADDR baseaddr = objfile->text_section_offset ();
22148 struct gdbarch *gdbarch = objfile->arch ();
22149
22150 for (const auto &cand_off
22151 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
22152 {
22153 struct dwarf2_cu *cand_cu = cu;
22154 struct die_info *cand
22155 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22156 if (!cand
22157 || !cand->parent
22158 || cand->parent->tag != DW_TAG_subprogram)
22159 continue;
22160
22161 CORE_ADDR pc_low, pc_high;
22162 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22163 if (pc_low == ((CORE_ADDR) -1))
22164 continue;
22165 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22166 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22167 if (!(pc_low <= pc && pc < pc_high))
22168 continue;
22169
22170 die = cand;
22171 attr = dwarf2_attr (die, DW_AT_location, cu);
22172 break;
22173 }
22174 }
22175
22176 if (!attr)
22177 {
22178 /* DWARF: "If there is no such attribute, then there is no effect.".
22179 DATA is ignored if SIZE is 0. */
22180
22181 retval.data = NULL;
22182 retval.size = 0;
22183 }
22184 else if (attr->form_is_section_offset ())
22185 {
22186 struct dwarf2_loclist_baton loclist_baton;
22187 CORE_ADDR pc = (*get_frame_pc) (baton);
22188 size_t size;
22189
22190 fill_in_loclist_baton (cu, &loclist_baton, attr);
22191
22192 retval.data = dwarf2_find_location_expression (&loclist_baton,
22193 &size, pc);
22194 retval.size = size;
22195 }
22196 else
22197 {
22198 if (!attr->form_is_block ())
22199 error (_("Dwarf Error: DIE at %s referenced in module %s "
22200 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22201 sect_offset_str (sect_off), objfile_name (objfile));
22202
22203 retval.data = DW_BLOCK (attr)->data;
22204 retval.size = DW_BLOCK (attr)->size;
22205 }
22206 retval.per_cu = cu->per_cu;
22207
22208 age_cached_comp_units (dwarf2_per_objfile);
22209
22210 return retval;
22211 }
22212
22213 /* See read.h. */
22214
22215 struct dwarf2_locexpr_baton
22216 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22217 dwarf2_per_cu_data *per_cu,
22218 CORE_ADDR (*get_frame_pc) (void *baton),
22219 void *baton)
22220 {
22221 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22222
22223 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22224 }
22225
22226 /* Write a constant of a given type as target-ordered bytes into
22227 OBSTACK. */
22228
22229 static const gdb_byte *
22230 write_constant_as_bytes (struct obstack *obstack,
22231 enum bfd_endian byte_order,
22232 struct type *type,
22233 ULONGEST value,
22234 LONGEST *len)
22235 {
22236 gdb_byte *result;
22237
22238 *len = TYPE_LENGTH (type);
22239 result = (gdb_byte *) obstack_alloc (obstack, *len);
22240 store_unsigned_integer (result, *len, byte_order, value);
22241
22242 return result;
22243 }
22244
22245 /* See read.h. */
22246
22247 const gdb_byte *
22248 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22249 dwarf2_per_cu_data *per_cu,
22250 obstack *obstack,
22251 LONGEST *len)
22252 {
22253 struct dwarf2_cu *cu;
22254 struct die_info *die;
22255 struct attribute *attr;
22256 const gdb_byte *result = NULL;
22257 struct type *type;
22258 LONGEST value;
22259 enum bfd_endian byte_order;
22260 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22261
22262 if (per_cu->cu == NULL)
22263 load_cu (per_cu, false);
22264 cu = per_cu->cu;
22265 if (cu == NULL)
22266 {
22267 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22268 Instead just throw an error, not much else we can do. */
22269 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22270 sect_offset_str (sect_off), objfile_name (objfile));
22271 }
22272
22273 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22274 if (!die)
22275 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22276 sect_offset_str (sect_off), objfile_name (objfile));
22277
22278 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22279 if (attr == NULL)
22280 return NULL;
22281
22282 byte_order = (bfd_big_endian (objfile->obfd)
22283 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22284
22285 switch (attr->form)
22286 {
22287 case DW_FORM_addr:
22288 case DW_FORM_addrx:
22289 case DW_FORM_GNU_addr_index:
22290 {
22291 gdb_byte *tem;
22292
22293 *len = cu->header.addr_size;
22294 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22295 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22296 result = tem;
22297 }
22298 break;
22299 case DW_FORM_string:
22300 case DW_FORM_strp:
22301 case DW_FORM_strx:
22302 case DW_FORM_GNU_str_index:
22303 case DW_FORM_GNU_strp_alt:
22304 /* DW_STRING is already allocated on the objfile obstack, point
22305 directly to it. */
22306 result = (const gdb_byte *) DW_STRING (attr);
22307 *len = strlen (DW_STRING (attr));
22308 break;
22309 case DW_FORM_block1:
22310 case DW_FORM_block2:
22311 case DW_FORM_block4:
22312 case DW_FORM_block:
22313 case DW_FORM_exprloc:
22314 case DW_FORM_data16:
22315 result = DW_BLOCK (attr)->data;
22316 *len = DW_BLOCK (attr)->size;
22317 break;
22318
22319 /* The DW_AT_const_value attributes are supposed to carry the
22320 symbol's value "represented as it would be on the target
22321 architecture." By the time we get here, it's already been
22322 converted to host endianness, so we just need to sign- or
22323 zero-extend it as appropriate. */
22324 case DW_FORM_data1:
22325 type = die_type (die, cu);
22326 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22327 if (result == NULL)
22328 result = write_constant_as_bytes (obstack, byte_order,
22329 type, value, len);
22330 break;
22331 case DW_FORM_data2:
22332 type = die_type (die, cu);
22333 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22334 if (result == NULL)
22335 result = write_constant_as_bytes (obstack, byte_order,
22336 type, value, len);
22337 break;
22338 case DW_FORM_data4:
22339 type = die_type (die, cu);
22340 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22341 if (result == NULL)
22342 result = write_constant_as_bytes (obstack, byte_order,
22343 type, value, len);
22344 break;
22345 case DW_FORM_data8:
22346 type = die_type (die, cu);
22347 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22348 if (result == NULL)
22349 result = write_constant_as_bytes (obstack, byte_order,
22350 type, value, len);
22351 break;
22352
22353 case DW_FORM_sdata:
22354 case DW_FORM_implicit_const:
22355 type = die_type (die, cu);
22356 result = write_constant_as_bytes (obstack, byte_order,
22357 type, DW_SND (attr), len);
22358 break;
22359
22360 case DW_FORM_udata:
22361 type = die_type (die, cu);
22362 result = write_constant_as_bytes (obstack, byte_order,
22363 type, DW_UNSND (attr), len);
22364 break;
22365
22366 default:
22367 complaint (_("unsupported const value attribute form: '%s'"),
22368 dwarf_form_name (attr->form));
22369 break;
22370 }
22371
22372 return result;
22373 }
22374
22375 /* See read.h. */
22376
22377 struct type *
22378 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22379 dwarf2_per_cu_data *per_cu)
22380 {
22381 struct dwarf2_cu *cu;
22382 struct die_info *die;
22383
22384 if (per_cu->cu == NULL)
22385 load_cu (per_cu, false);
22386 cu = per_cu->cu;
22387 if (!cu)
22388 return NULL;
22389
22390 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22391 if (!die)
22392 return NULL;
22393
22394 return die_type (die, cu);
22395 }
22396
22397 /* See read.h. */
22398
22399 struct type *
22400 dwarf2_get_die_type (cu_offset die_offset,
22401 struct dwarf2_per_cu_data *per_cu)
22402 {
22403 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22404 return get_die_type_at_offset (die_offset_sect, per_cu);
22405 }
22406
22407 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22408 On entry *REF_CU is the CU of SRC_DIE.
22409 On exit *REF_CU is the CU of the result.
22410 Returns NULL if the referenced DIE isn't found. */
22411
22412 static struct die_info *
22413 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22414 struct dwarf2_cu **ref_cu)
22415 {
22416 struct die_info temp_die;
22417 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22418 struct die_info *die;
22419
22420 /* While it might be nice to assert sig_type->type == NULL here,
22421 we can get here for DW_AT_imported_declaration where we need
22422 the DIE not the type. */
22423
22424 /* If necessary, add it to the queue and load its DIEs. */
22425
22426 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22427 read_signatured_type (sig_type);
22428
22429 sig_cu = sig_type->per_cu.cu;
22430 gdb_assert (sig_cu != NULL);
22431 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22432 temp_die.sect_off = sig_type->type_offset_in_section;
22433 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22434 to_underlying (temp_die.sect_off));
22435 if (die)
22436 {
22437 struct dwarf2_per_objfile *dwarf2_per_objfile
22438 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22439
22440 /* For .gdb_index version 7 keep track of included TUs.
22441 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22442 if (dwarf2_per_objfile->index_table != NULL
22443 && dwarf2_per_objfile->index_table->version <= 7)
22444 {
22445 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22446 }
22447
22448 *ref_cu = sig_cu;
22449 if (sig_cu != cu)
22450 sig_cu->ancestor = cu;
22451
22452 return die;
22453 }
22454
22455 return NULL;
22456 }
22457
22458 /* Follow signatured type referenced by ATTR in SRC_DIE.
22459 On entry *REF_CU is the CU of SRC_DIE.
22460 On exit *REF_CU is the CU of the result.
22461 The result is the DIE of the type.
22462 If the referenced type cannot be found an error is thrown. */
22463
22464 static struct die_info *
22465 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22466 struct dwarf2_cu **ref_cu)
22467 {
22468 ULONGEST signature = DW_SIGNATURE (attr);
22469 struct signatured_type *sig_type;
22470 struct die_info *die;
22471
22472 gdb_assert (attr->form == DW_FORM_ref_sig8);
22473
22474 sig_type = lookup_signatured_type (*ref_cu, signature);
22475 /* sig_type will be NULL if the signatured type is missing from
22476 the debug info. */
22477 if (sig_type == NULL)
22478 {
22479 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22480 " from DIE at %s [in module %s]"),
22481 hex_string (signature), sect_offset_str (src_die->sect_off),
22482 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22483 }
22484
22485 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22486 if (die == NULL)
22487 {
22488 dump_die_for_error (src_die);
22489 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22490 " from DIE at %s [in module %s]"),
22491 hex_string (signature), sect_offset_str (src_die->sect_off),
22492 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22493 }
22494
22495 return die;
22496 }
22497
22498 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22499 reading in and processing the type unit if necessary. */
22500
22501 static struct type *
22502 get_signatured_type (struct die_info *die, ULONGEST signature,
22503 struct dwarf2_cu *cu)
22504 {
22505 struct dwarf2_per_objfile *dwarf2_per_objfile
22506 = cu->per_cu->dwarf2_per_objfile;
22507 struct signatured_type *sig_type;
22508 struct dwarf2_cu *type_cu;
22509 struct die_info *type_die;
22510 struct type *type;
22511
22512 sig_type = lookup_signatured_type (cu, signature);
22513 /* sig_type will be NULL if the signatured type is missing from
22514 the debug info. */
22515 if (sig_type == NULL)
22516 {
22517 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22518 " from DIE at %s [in module %s]"),
22519 hex_string (signature), sect_offset_str (die->sect_off),
22520 objfile_name (dwarf2_per_objfile->objfile));
22521 return build_error_marker_type (cu, die);
22522 }
22523
22524 /* If we already know the type we're done. */
22525 if (sig_type->type != NULL)
22526 return sig_type->type;
22527
22528 type_cu = cu;
22529 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22530 if (type_die != NULL)
22531 {
22532 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22533 is created. This is important, for example, because for c++ classes
22534 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22535 type = read_type_die (type_die, type_cu);
22536 if (type == NULL)
22537 {
22538 complaint (_("Dwarf Error: Cannot build signatured type %s"
22539 " referenced from DIE at %s [in module %s]"),
22540 hex_string (signature), sect_offset_str (die->sect_off),
22541 objfile_name (dwarf2_per_objfile->objfile));
22542 type = build_error_marker_type (cu, die);
22543 }
22544 }
22545 else
22546 {
22547 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22548 " from DIE at %s [in module %s]"),
22549 hex_string (signature), sect_offset_str (die->sect_off),
22550 objfile_name (dwarf2_per_objfile->objfile));
22551 type = build_error_marker_type (cu, die);
22552 }
22553 sig_type->type = type;
22554
22555 return type;
22556 }
22557
22558 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22559 reading in and processing the type unit if necessary. */
22560
22561 static struct type *
22562 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22563 struct dwarf2_cu *cu) /* ARI: editCase function */
22564 {
22565 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22566 if (attr->form_is_ref ())
22567 {
22568 struct dwarf2_cu *type_cu = cu;
22569 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22570
22571 return read_type_die (type_die, type_cu);
22572 }
22573 else if (attr->form == DW_FORM_ref_sig8)
22574 {
22575 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22576 }
22577 else
22578 {
22579 struct dwarf2_per_objfile *dwarf2_per_objfile
22580 = cu->per_cu->dwarf2_per_objfile;
22581
22582 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22583 " at %s [in module %s]"),
22584 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22585 objfile_name (dwarf2_per_objfile->objfile));
22586 return build_error_marker_type (cu, die);
22587 }
22588 }
22589
22590 /* Load the DIEs associated with type unit PER_CU into memory. */
22591
22592 static void
22593 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22594 {
22595 struct signatured_type *sig_type;
22596
22597 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22598 gdb_assert (! per_cu->type_unit_group_p ());
22599
22600 /* We have the per_cu, but we need the signatured_type.
22601 Fortunately this is an easy translation. */
22602 gdb_assert (per_cu->is_debug_types);
22603 sig_type = (struct signatured_type *) per_cu;
22604
22605 gdb_assert (per_cu->cu == NULL);
22606
22607 read_signatured_type (sig_type);
22608
22609 gdb_assert (per_cu->cu != NULL);
22610 }
22611
22612 /* Read in a signatured type and build its CU and DIEs.
22613 If the type is a stub for the real type in a DWO file,
22614 read in the real type from the DWO file as well. */
22615
22616 static void
22617 read_signatured_type (struct signatured_type *sig_type)
22618 {
22619 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22620
22621 gdb_assert (per_cu->is_debug_types);
22622 gdb_assert (per_cu->cu == NULL);
22623
22624 cutu_reader reader (per_cu, NULL, 0, false);
22625
22626 if (!reader.dummy_p)
22627 {
22628 struct dwarf2_cu *cu = reader.cu;
22629 const gdb_byte *info_ptr = reader.info_ptr;
22630
22631 gdb_assert (cu->die_hash == NULL);
22632 cu->die_hash =
22633 htab_create_alloc_ex (cu->header.length / 12,
22634 die_hash,
22635 die_eq,
22636 NULL,
22637 &cu->comp_unit_obstack,
22638 hashtab_obstack_allocate,
22639 dummy_obstack_deallocate);
22640
22641 if (reader.comp_unit_die->has_children)
22642 reader.comp_unit_die->child
22643 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22644 reader.comp_unit_die);
22645 cu->dies = reader.comp_unit_die;
22646 /* comp_unit_die is not stored in die_hash, no need. */
22647
22648 /* We try not to read any attributes in this function, because
22649 not all CUs needed for references have been loaded yet, and
22650 symbol table processing isn't initialized. But we have to
22651 set the CU language, or we won't be able to build types
22652 correctly. Similarly, if we do not read the producer, we can
22653 not apply producer-specific interpretation. */
22654 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22655
22656 reader.keep ();
22657 }
22658
22659 sig_type->per_cu.tu_read = 1;
22660 }
22661
22662 /* Decode simple location descriptions.
22663 Given a pointer to a dwarf block that defines a location, compute
22664 the location and return the value.
22665
22666 NOTE drow/2003-11-18: This function is called in two situations
22667 now: for the address of static or global variables (partial symbols
22668 only) and for offsets into structures which are expected to be
22669 (more or less) constant. The partial symbol case should go away,
22670 and only the constant case should remain. That will let this
22671 function complain more accurately. A few special modes are allowed
22672 without complaint for global variables (for instance, global
22673 register values and thread-local values).
22674
22675 A location description containing no operations indicates that the
22676 object is optimized out. The return value is 0 for that case.
22677 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22678 callers will only want a very basic result and this can become a
22679 complaint.
22680
22681 Note that stack[0] is unused except as a default error return. */
22682
22683 static CORE_ADDR
22684 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22685 {
22686 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22687 size_t i;
22688 size_t size = blk->size;
22689 const gdb_byte *data = blk->data;
22690 CORE_ADDR stack[64];
22691 int stacki;
22692 unsigned int bytes_read, unsnd;
22693 gdb_byte op;
22694
22695 i = 0;
22696 stacki = 0;
22697 stack[stacki] = 0;
22698 stack[++stacki] = 0;
22699
22700 while (i < size)
22701 {
22702 op = data[i++];
22703 switch (op)
22704 {
22705 case DW_OP_lit0:
22706 case DW_OP_lit1:
22707 case DW_OP_lit2:
22708 case DW_OP_lit3:
22709 case DW_OP_lit4:
22710 case DW_OP_lit5:
22711 case DW_OP_lit6:
22712 case DW_OP_lit7:
22713 case DW_OP_lit8:
22714 case DW_OP_lit9:
22715 case DW_OP_lit10:
22716 case DW_OP_lit11:
22717 case DW_OP_lit12:
22718 case DW_OP_lit13:
22719 case DW_OP_lit14:
22720 case DW_OP_lit15:
22721 case DW_OP_lit16:
22722 case DW_OP_lit17:
22723 case DW_OP_lit18:
22724 case DW_OP_lit19:
22725 case DW_OP_lit20:
22726 case DW_OP_lit21:
22727 case DW_OP_lit22:
22728 case DW_OP_lit23:
22729 case DW_OP_lit24:
22730 case DW_OP_lit25:
22731 case DW_OP_lit26:
22732 case DW_OP_lit27:
22733 case DW_OP_lit28:
22734 case DW_OP_lit29:
22735 case DW_OP_lit30:
22736 case DW_OP_lit31:
22737 stack[++stacki] = op - DW_OP_lit0;
22738 break;
22739
22740 case DW_OP_reg0:
22741 case DW_OP_reg1:
22742 case DW_OP_reg2:
22743 case DW_OP_reg3:
22744 case DW_OP_reg4:
22745 case DW_OP_reg5:
22746 case DW_OP_reg6:
22747 case DW_OP_reg7:
22748 case DW_OP_reg8:
22749 case DW_OP_reg9:
22750 case DW_OP_reg10:
22751 case DW_OP_reg11:
22752 case DW_OP_reg12:
22753 case DW_OP_reg13:
22754 case DW_OP_reg14:
22755 case DW_OP_reg15:
22756 case DW_OP_reg16:
22757 case DW_OP_reg17:
22758 case DW_OP_reg18:
22759 case DW_OP_reg19:
22760 case DW_OP_reg20:
22761 case DW_OP_reg21:
22762 case DW_OP_reg22:
22763 case DW_OP_reg23:
22764 case DW_OP_reg24:
22765 case DW_OP_reg25:
22766 case DW_OP_reg26:
22767 case DW_OP_reg27:
22768 case DW_OP_reg28:
22769 case DW_OP_reg29:
22770 case DW_OP_reg30:
22771 case DW_OP_reg31:
22772 stack[++stacki] = op - DW_OP_reg0;
22773 if (i < size)
22774 dwarf2_complex_location_expr_complaint ();
22775 break;
22776
22777 case DW_OP_regx:
22778 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22779 i += bytes_read;
22780 stack[++stacki] = unsnd;
22781 if (i < size)
22782 dwarf2_complex_location_expr_complaint ();
22783 break;
22784
22785 case DW_OP_addr:
22786 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22787 &bytes_read);
22788 i += bytes_read;
22789 break;
22790
22791 case DW_OP_const1u:
22792 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22793 i += 1;
22794 break;
22795
22796 case DW_OP_const1s:
22797 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22798 i += 1;
22799 break;
22800
22801 case DW_OP_const2u:
22802 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22803 i += 2;
22804 break;
22805
22806 case DW_OP_const2s:
22807 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22808 i += 2;
22809 break;
22810
22811 case DW_OP_const4u:
22812 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22813 i += 4;
22814 break;
22815
22816 case DW_OP_const4s:
22817 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22818 i += 4;
22819 break;
22820
22821 case DW_OP_const8u:
22822 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22823 i += 8;
22824 break;
22825
22826 case DW_OP_constu:
22827 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22828 &bytes_read);
22829 i += bytes_read;
22830 break;
22831
22832 case DW_OP_consts:
22833 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22834 i += bytes_read;
22835 break;
22836
22837 case DW_OP_dup:
22838 stack[stacki + 1] = stack[stacki];
22839 stacki++;
22840 break;
22841
22842 case DW_OP_plus:
22843 stack[stacki - 1] += stack[stacki];
22844 stacki--;
22845 break;
22846
22847 case DW_OP_plus_uconst:
22848 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22849 &bytes_read);
22850 i += bytes_read;
22851 break;
22852
22853 case DW_OP_minus:
22854 stack[stacki - 1] -= stack[stacki];
22855 stacki--;
22856 break;
22857
22858 case DW_OP_deref:
22859 /* If we're not the last op, then we definitely can't encode
22860 this using GDB's address_class enum. This is valid for partial
22861 global symbols, although the variable's address will be bogus
22862 in the psymtab. */
22863 if (i < size)
22864 dwarf2_complex_location_expr_complaint ();
22865 break;
22866
22867 case DW_OP_GNU_push_tls_address:
22868 case DW_OP_form_tls_address:
22869 /* The top of the stack has the offset from the beginning
22870 of the thread control block at which the variable is located. */
22871 /* Nothing should follow this operator, so the top of stack would
22872 be returned. */
22873 /* This is valid for partial global symbols, but the variable's
22874 address will be bogus in the psymtab. Make it always at least
22875 non-zero to not look as a variable garbage collected by linker
22876 which have DW_OP_addr 0. */
22877 if (i < size)
22878 dwarf2_complex_location_expr_complaint ();
22879 stack[stacki]++;
22880 break;
22881
22882 case DW_OP_GNU_uninit:
22883 break;
22884
22885 case DW_OP_addrx:
22886 case DW_OP_GNU_addr_index:
22887 case DW_OP_GNU_const_index:
22888 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22889 &bytes_read);
22890 i += bytes_read;
22891 break;
22892
22893 default:
22894 {
22895 const char *name = get_DW_OP_name (op);
22896
22897 if (name)
22898 complaint (_("unsupported stack op: '%s'"),
22899 name);
22900 else
22901 complaint (_("unsupported stack op: '%02x'"),
22902 op);
22903 }
22904
22905 return (stack[stacki]);
22906 }
22907
22908 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22909 outside of the allocated space. Also enforce minimum>0. */
22910 if (stacki >= ARRAY_SIZE (stack) - 1)
22911 {
22912 complaint (_("location description stack overflow"));
22913 return 0;
22914 }
22915
22916 if (stacki <= 0)
22917 {
22918 complaint (_("location description stack underflow"));
22919 return 0;
22920 }
22921 }
22922 return (stack[stacki]);
22923 }
22924
22925 /* memory allocation interface */
22926
22927 static struct dwarf_block *
22928 dwarf_alloc_block (struct dwarf2_cu *cu)
22929 {
22930 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
22931 }
22932
22933 static struct die_info *
22934 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
22935 {
22936 struct die_info *die;
22937 size_t size = sizeof (struct die_info);
22938
22939 if (num_attrs > 1)
22940 size += (num_attrs - 1) * sizeof (struct attribute);
22941
22942 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
22943 memset (die, 0, sizeof (struct die_info));
22944 return (die);
22945 }
22946
22947 \f
22948
22949 /* Macro support. */
22950
22951 /* An overload of dwarf_decode_macros that finds the correct section
22952 and ensures it is read in before calling the other overload. */
22953
22954 static void
22955 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22956 int section_is_gnu)
22957 {
22958 struct dwarf2_per_objfile *dwarf2_per_objfile
22959 = cu->per_cu->dwarf2_per_objfile;
22960 struct objfile *objfile = dwarf2_per_objfile->objfile;
22961 const struct line_header *lh = cu->line_header;
22962 unsigned int offset_size = cu->header.offset_size;
22963 struct dwarf2_section_info *section;
22964 const char *section_name;
22965
22966 if (cu->dwo_unit != nullptr)
22967 {
22968 if (section_is_gnu)
22969 {
22970 section = &cu->dwo_unit->dwo_file->sections.macro;
22971 section_name = ".debug_macro.dwo";
22972 }
22973 else
22974 {
22975 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22976 section_name = ".debug_macinfo.dwo";
22977 }
22978 }
22979 else
22980 {
22981 if (section_is_gnu)
22982 {
22983 section = &dwarf2_per_objfile->macro;
22984 section_name = ".debug_macro";
22985 }
22986 else
22987 {
22988 section = &dwarf2_per_objfile->macinfo;
22989 section_name = ".debug_macinfo";
22990 }
22991 }
22992
22993 section->read (objfile);
22994 if (section->buffer == nullptr)
22995 {
22996 complaint (_("missing %s section"), section_name);
22997 return;
22998 }
22999
23000 buildsym_compunit *builder = cu->get_builder ();
23001
23002 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23003 offset_size, offset, section_is_gnu);
23004 }
23005
23006 /* Return the .debug_loc section to use for CU.
23007 For DWO files use .debug_loc.dwo. */
23008
23009 static struct dwarf2_section_info *
23010 cu_debug_loc_section (struct dwarf2_cu *cu)
23011 {
23012 struct dwarf2_per_objfile *dwarf2_per_objfile
23013 = cu->per_cu->dwarf2_per_objfile;
23014
23015 if (cu->dwo_unit)
23016 {
23017 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23018
23019 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23020 }
23021 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23022 : &dwarf2_per_objfile->loc);
23023 }
23024
23025 /* A helper function that fills in a dwarf2_loclist_baton. */
23026
23027 static void
23028 fill_in_loclist_baton (struct dwarf2_cu *cu,
23029 struct dwarf2_loclist_baton *baton,
23030 const struct attribute *attr)
23031 {
23032 struct dwarf2_per_objfile *dwarf2_per_objfile
23033 = cu->per_cu->dwarf2_per_objfile;
23034 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23035
23036 section->read (dwarf2_per_objfile->objfile);
23037
23038 baton->per_cu = cu->per_cu;
23039 gdb_assert (baton->per_cu);
23040 /* We don't know how long the location list is, but make sure we
23041 don't run off the edge of the section. */
23042 baton->size = section->size - DW_UNSND (attr);
23043 baton->data = section->buffer + DW_UNSND (attr);
23044 if (cu->base_address.has_value ())
23045 baton->base_address = *cu->base_address;
23046 else
23047 baton->base_address = 0;
23048 baton->from_dwo = cu->dwo_unit != NULL;
23049 }
23050
23051 static void
23052 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23053 struct dwarf2_cu *cu, int is_block)
23054 {
23055 struct dwarf2_per_objfile *dwarf2_per_objfile
23056 = cu->per_cu->dwarf2_per_objfile;
23057 struct objfile *objfile = dwarf2_per_objfile->objfile;
23058 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23059
23060 if (attr->form_is_section_offset ()
23061 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23062 the section. If so, fall through to the complaint in the
23063 other branch. */
23064 && DW_UNSND (attr) < section->get_size (objfile))
23065 {
23066 struct dwarf2_loclist_baton *baton;
23067
23068 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23069
23070 fill_in_loclist_baton (cu, baton, attr);
23071
23072 if (!cu->base_address.has_value ())
23073 complaint (_("Location list used without "
23074 "specifying the CU base address."));
23075
23076 SYMBOL_ACLASS_INDEX (sym) = (is_block
23077 ? dwarf2_loclist_block_index
23078 : dwarf2_loclist_index);
23079 SYMBOL_LOCATION_BATON (sym) = baton;
23080 }
23081 else
23082 {
23083 struct dwarf2_locexpr_baton *baton;
23084
23085 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23086 baton->per_cu = cu->per_cu;
23087 gdb_assert (baton->per_cu);
23088
23089 if (attr->form_is_block ())
23090 {
23091 /* Note that we're just copying the block's data pointer
23092 here, not the actual data. We're still pointing into the
23093 info_buffer for SYM's objfile; right now we never release
23094 that buffer, but when we do clean up properly this may
23095 need to change. */
23096 baton->size = DW_BLOCK (attr)->size;
23097 baton->data = DW_BLOCK (attr)->data;
23098 }
23099 else
23100 {
23101 dwarf2_invalid_attrib_class_complaint ("location description",
23102 sym->natural_name ());
23103 baton->size = 0;
23104 }
23105
23106 SYMBOL_ACLASS_INDEX (sym) = (is_block
23107 ? dwarf2_locexpr_block_index
23108 : dwarf2_locexpr_index);
23109 SYMBOL_LOCATION_BATON (sym) = baton;
23110 }
23111 }
23112
23113 /* See read.h. */
23114
23115 struct objfile *
23116 dwarf2_per_cu_data::objfile () const
23117 {
23118 struct objfile *objfile = dwarf2_per_objfile->objfile;
23119
23120 /* Return the master objfile, so that we can report and look up the
23121 correct file containing this variable. */
23122 if (objfile->separate_debug_objfile_backlink)
23123 objfile = objfile->separate_debug_objfile_backlink;
23124
23125 return objfile;
23126 }
23127
23128 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23129 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23130 CU_HEADERP first. */
23131
23132 static const struct comp_unit_head *
23133 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23134 const struct dwarf2_per_cu_data *per_cu)
23135 {
23136 const gdb_byte *info_ptr;
23137
23138 if (per_cu->cu)
23139 return &per_cu->cu->header;
23140
23141 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
23142
23143 memset (cu_headerp, 0, sizeof (*cu_headerp));
23144 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23145 rcuh_kind::COMPILE);
23146
23147 return cu_headerp;
23148 }
23149
23150 /* See read.h. */
23151
23152 int
23153 dwarf2_per_cu_data::addr_size () const
23154 {
23155 struct comp_unit_head cu_header_local;
23156 const struct comp_unit_head *cu_headerp;
23157
23158 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23159
23160 return cu_headerp->addr_size;
23161 }
23162
23163 /* See read.h. */
23164
23165 int
23166 dwarf2_per_cu_data::offset_size () const
23167 {
23168 struct comp_unit_head cu_header_local;
23169 const struct comp_unit_head *cu_headerp;
23170
23171 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23172
23173 return cu_headerp->offset_size;
23174 }
23175
23176 /* See read.h. */
23177
23178 int
23179 dwarf2_per_cu_data::ref_addr_size () const
23180 {
23181 struct comp_unit_head cu_header_local;
23182 const struct comp_unit_head *cu_headerp;
23183
23184 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23185
23186 if (cu_headerp->version == 2)
23187 return cu_headerp->addr_size;
23188 else
23189 return cu_headerp->offset_size;
23190 }
23191
23192 /* See read.h. */
23193
23194 CORE_ADDR
23195 dwarf2_per_cu_data::text_offset () const
23196 {
23197 struct objfile *objfile = dwarf2_per_objfile->objfile;
23198
23199 return objfile->text_section_offset ();
23200 }
23201
23202 /* See read.h. */
23203
23204 struct type *
23205 dwarf2_per_cu_data::addr_type () const
23206 {
23207 struct objfile *objfile = dwarf2_per_objfile->objfile;
23208 struct type *void_type = objfile_type (objfile)->builtin_void;
23209 struct type *addr_type = lookup_pointer_type (void_type);
23210 int addr_size = this->addr_size ();
23211
23212 if (TYPE_LENGTH (addr_type) == addr_size)
23213 return addr_type;
23214
23215 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23216 return addr_type;
23217 }
23218
23219 /* A helper function for dwarf2_find_containing_comp_unit that returns
23220 the index of the result, and that searches a vector. It will
23221 return a result even if the offset in question does not actually
23222 occur in any CU. This is separate so that it can be unit
23223 tested. */
23224
23225 static int
23226 dwarf2_find_containing_comp_unit
23227 (sect_offset sect_off,
23228 unsigned int offset_in_dwz,
23229 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23230 {
23231 int low, high;
23232
23233 low = 0;
23234 high = all_comp_units.size () - 1;
23235 while (high > low)
23236 {
23237 struct dwarf2_per_cu_data *mid_cu;
23238 int mid = low + (high - low) / 2;
23239
23240 mid_cu = all_comp_units[mid];
23241 if (mid_cu->is_dwz > offset_in_dwz
23242 || (mid_cu->is_dwz == offset_in_dwz
23243 && mid_cu->sect_off + mid_cu->length > sect_off))
23244 high = mid;
23245 else
23246 low = mid + 1;
23247 }
23248 gdb_assert (low == high);
23249 return low;
23250 }
23251
23252 /* Locate the .debug_info compilation unit from CU's objfile which contains
23253 the DIE at OFFSET. Raises an error on failure. */
23254
23255 static struct dwarf2_per_cu_data *
23256 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23257 unsigned int offset_in_dwz,
23258 struct dwarf2_per_objfile *dwarf2_per_objfile)
23259 {
23260 int low
23261 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23262 dwarf2_per_objfile->all_comp_units);
23263 struct dwarf2_per_cu_data *this_cu
23264 = dwarf2_per_objfile->all_comp_units[low];
23265
23266 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23267 {
23268 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23269 error (_("Dwarf Error: could not find partial DIE containing "
23270 "offset %s [in module %s]"),
23271 sect_offset_str (sect_off),
23272 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23273
23274 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
23275 <= sect_off);
23276 return dwarf2_per_objfile->all_comp_units[low-1];
23277 }
23278 else
23279 {
23280 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
23281 && sect_off >= this_cu->sect_off + this_cu->length)
23282 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23283 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23284 return this_cu;
23285 }
23286 }
23287
23288 #if GDB_SELF_TEST
23289
23290 namespace selftests {
23291 namespace find_containing_comp_unit {
23292
23293 static void
23294 run_test ()
23295 {
23296 struct dwarf2_per_cu_data one {};
23297 struct dwarf2_per_cu_data two {};
23298 struct dwarf2_per_cu_data three {};
23299 struct dwarf2_per_cu_data four {};
23300
23301 one.length = 5;
23302 two.sect_off = sect_offset (one.length);
23303 two.length = 7;
23304
23305 three.length = 5;
23306 three.is_dwz = 1;
23307 four.sect_off = sect_offset (three.length);
23308 four.length = 7;
23309 four.is_dwz = 1;
23310
23311 std::vector<dwarf2_per_cu_data *> units;
23312 units.push_back (&one);
23313 units.push_back (&two);
23314 units.push_back (&three);
23315 units.push_back (&four);
23316
23317 int result;
23318
23319 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23320 SELF_CHECK (units[result] == &one);
23321 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23322 SELF_CHECK (units[result] == &one);
23323 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23324 SELF_CHECK (units[result] == &two);
23325
23326 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23327 SELF_CHECK (units[result] == &three);
23328 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23329 SELF_CHECK (units[result] == &three);
23330 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23331 SELF_CHECK (units[result] == &four);
23332 }
23333
23334 }
23335 }
23336
23337 #endif /* GDB_SELF_TEST */
23338
23339 /* Initialize dwarf2_cu CU, owned by PER_CU. */
23340
23341 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
23342 : per_cu (per_cu_),
23343 mark (false),
23344 has_loclist (false),
23345 checked_producer (false),
23346 producer_is_gxx_lt_4_6 (false),
23347 producer_is_gcc_lt_4_3 (false),
23348 producer_is_icc (false),
23349 producer_is_icc_lt_14 (false),
23350 producer_is_codewarrior (false),
23351 processing_has_namespace_info (false)
23352 {
23353 per_cu->cu = this;
23354 }
23355
23356 /* Destroy a dwarf2_cu. */
23357
23358 dwarf2_cu::~dwarf2_cu ()
23359 {
23360 per_cu->cu = NULL;
23361 }
23362
23363 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23364
23365 static void
23366 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23367 enum language pretend_language)
23368 {
23369 struct attribute *attr;
23370
23371 /* Set the language we're debugging. */
23372 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23373 if (attr != nullptr)
23374 set_cu_language (DW_UNSND (attr), cu);
23375 else
23376 {
23377 cu->language = pretend_language;
23378 cu->language_defn = language_def (cu->language);
23379 }
23380
23381 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23382 }
23383
23384 /* Increase the age counter on each cached compilation unit, and free
23385 any that are too old. */
23386
23387 static void
23388 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23389 {
23390 struct dwarf2_per_cu_data *per_cu, **last_chain;
23391
23392 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23393 per_cu = dwarf2_per_objfile->read_in_chain;
23394 while (per_cu != NULL)
23395 {
23396 per_cu->cu->last_used ++;
23397 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23398 dwarf2_mark (per_cu->cu);
23399 per_cu = per_cu->cu->read_in_chain;
23400 }
23401
23402 per_cu = dwarf2_per_objfile->read_in_chain;
23403 last_chain = &dwarf2_per_objfile->read_in_chain;
23404 while (per_cu != NULL)
23405 {
23406 struct dwarf2_per_cu_data *next_cu;
23407
23408 next_cu = per_cu->cu->read_in_chain;
23409
23410 if (!per_cu->cu->mark)
23411 {
23412 delete per_cu->cu;
23413 *last_chain = next_cu;
23414 }
23415 else
23416 last_chain = &per_cu->cu->read_in_chain;
23417
23418 per_cu = next_cu;
23419 }
23420 }
23421
23422 /* Remove a single compilation unit from the cache. */
23423
23424 static void
23425 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23426 {
23427 struct dwarf2_per_cu_data *per_cu, **last_chain;
23428 struct dwarf2_per_objfile *dwarf2_per_objfile
23429 = target_per_cu->dwarf2_per_objfile;
23430
23431 per_cu = dwarf2_per_objfile->read_in_chain;
23432 last_chain = &dwarf2_per_objfile->read_in_chain;
23433 while (per_cu != NULL)
23434 {
23435 struct dwarf2_per_cu_data *next_cu;
23436
23437 next_cu = per_cu->cu->read_in_chain;
23438
23439 if (per_cu == target_per_cu)
23440 {
23441 delete per_cu->cu;
23442 per_cu->cu = NULL;
23443 *last_chain = next_cu;
23444 break;
23445 }
23446 else
23447 last_chain = &per_cu->cu->read_in_chain;
23448
23449 per_cu = next_cu;
23450 }
23451 }
23452
23453 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23454 We store these in a hash table separate from the DIEs, and preserve them
23455 when the DIEs are flushed out of cache.
23456
23457 The CU "per_cu" pointer is needed because offset alone is not enough to
23458 uniquely identify the type. A file may have multiple .debug_types sections,
23459 or the type may come from a DWO file. Furthermore, while it's more logical
23460 to use per_cu->section+offset, with Fission the section with the data is in
23461 the DWO file but we don't know that section at the point we need it.
23462 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23463 because we can enter the lookup routine, get_die_type_at_offset, from
23464 outside this file, and thus won't necessarily have PER_CU->cu.
23465 Fortunately, PER_CU is stable for the life of the objfile. */
23466
23467 struct dwarf2_per_cu_offset_and_type
23468 {
23469 const struct dwarf2_per_cu_data *per_cu;
23470 sect_offset sect_off;
23471 struct type *type;
23472 };
23473
23474 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23475
23476 static hashval_t
23477 per_cu_offset_and_type_hash (const void *item)
23478 {
23479 const struct dwarf2_per_cu_offset_and_type *ofs
23480 = (const struct dwarf2_per_cu_offset_and_type *) item;
23481
23482 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23483 }
23484
23485 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23486
23487 static int
23488 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23489 {
23490 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23491 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23492 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23493 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23494
23495 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23496 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23497 }
23498
23499 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23500 table if necessary. For convenience, return TYPE.
23501
23502 The DIEs reading must have careful ordering to:
23503 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23504 reading current DIE.
23505 * Not trying to dereference contents of still incompletely read in types
23506 while reading in other DIEs.
23507 * Enable referencing still incompletely read in types just by a pointer to
23508 the type without accessing its fields.
23509
23510 Therefore caller should follow these rules:
23511 * Try to fetch any prerequisite types we may need to build this DIE type
23512 before building the type and calling set_die_type.
23513 * After building type call set_die_type for current DIE as soon as
23514 possible before fetching more types to complete the current type.
23515 * Make the type as complete as possible before fetching more types. */
23516
23517 static struct type *
23518 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23519 {
23520 struct dwarf2_per_objfile *dwarf2_per_objfile
23521 = cu->per_cu->dwarf2_per_objfile;
23522 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23523 struct objfile *objfile = dwarf2_per_objfile->objfile;
23524 struct attribute *attr;
23525 struct dynamic_prop prop;
23526
23527 /* For Ada types, make sure that the gnat-specific data is always
23528 initialized (if not already set). There are a few types where
23529 we should not be doing so, because the type-specific area is
23530 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23531 where the type-specific area is used to store the floatformat).
23532 But this is not a problem, because the gnat-specific information
23533 is actually not needed for these types. */
23534 if (need_gnat_info (cu)
23535 && TYPE_CODE (type) != TYPE_CODE_FUNC
23536 && TYPE_CODE (type) != TYPE_CODE_FLT
23537 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23538 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23539 && TYPE_CODE (type) != TYPE_CODE_METHOD
23540 && !HAVE_GNAT_AUX_INFO (type))
23541 INIT_GNAT_SPECIFIC (type);
23542
23543 /* Read DW_AT_allocated and set in type. */
23544 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23545 if (attr != NULL && attr->form_is_block ())
23546 {
23547 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23548 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23549 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
23550 }
23551 else if (attr != NULL)
23552 {
23553 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23554 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23555 sect_offset_str (die->sect_off));
23556 }
23557
23558 /* Read DW_AT_associated and set in type. */
23559 attr = dwarf2_attr (die, DW_AT_associated, cu);
23560 if (attr != NULL && attr->form_is_block ())
23561 {
23562 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23563 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23564 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
23565 }
23566 else if (attr != NULL)
23567 {
23568 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23569 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23570 sect_offset_str (die->sect_off));
23571 }
23572
23573 /* Read DW_AT_data_location and set in type. */
23574 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23575 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23576 cu->per_cu->addr_type ()))
23577 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
23578
23579 if (dwarf2_per_objfile->die_type_hash == NULL)
23580 dwarf2_per_objfile->die_type_hash
23581 = htab_up (htab_create_alloc (127,
23582 per_cu_offset_and_type_hash,
23583 per_cu_offset_and_type_eq,
23584 NULL, xcalloc, xfree));
23585
23586 ofs.per_cu = cu->per_cu;
23587 ofs.sect_off = die->sect_off;
23588 ofs.type = type;
23589 slot = (struct dwarf2_per_cu_offset_and_type **)
23590 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23591 if (*slot)
23592 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23593 sect_offset_str (die->sect_off));
23594 *slot = XOBNEW (&objfile->objfile_obstack,
23595 struct dwarf2_per_cu_offset_and_type);
23596 **slot = ofs;
23597 return type;
23598 }
23599
23600 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23601 or return NULL if the die does not have a saved type. */
23602
23603 static struct type *
23604 get_die_type_at_offset (sect_offset sect_off,
23605 struct dwarf2_per_cu_data *per_cu)
23606 {
23607 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23608 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23609
23610 if (dwarf2_per_objfile->die_type_hash == NULL)
23611 return NULL;
23612
23613 ofs.per_cu = per_cu;
23614 ofs.sect_off = sect_off;
23615 slot = ((struct dwarf2_per_cu_offset_and_type *)
23616 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23617 if (slot)
23618 return slot->type;
23619 else
23620 return NULL;
23621 }
23622
23623 /* Look up the type for DIE in CU in die_type_hash,
23624 or return NULL if DIE does not have a saved type. */
23625
23626 static struct type *
23627 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23628 {
23629 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23630 }
23631
23632 /* Add a dependence relationship from CU to REF_PER_CU. */
23633
23634 static void
23635 dwarf2_add_dependence (struct dwarf2_cu *cu,
23636 struct dwarf2_per_cu_data *ref_per_cu)
23637 {
23638 void **slot;
23639
23640 if (cu->dependencies == NULL)
23641 cu->dependencies
23642 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23643 NULL, &cu->comp_unit_obstack,
23644 hashtab_obstack_allocate,
23645 dummy_obstack_deallocate);
23646
23647 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23648 if (*slot == NULL)
23649 *slot = ref_per_cu;
23650 }
23651
23652 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23653 Set the mark field in every compilation unit in the
23654 cache that we must keep because we are keeping CU. */
23655
23656 static int
23657 dwarf2_mark_helper (void **slot, void *data)
23658 {
23659 struct dwarf2_per_cu_data *per_cu;
23660
23661 per_cu = (struct dwarf2_per_cu_data *) *slot;
23662
23663 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23664 reading of the chain. As such dependencies remain valid it is not much
23665 useful to track and undo them during QUIT cleanups. */
23666 if (per_cu->cu == NULL)
23667 return 1;
23668
23669 if (per_cu->cu->mark)
23670 return 1;
23671 per_cu->cu->mark = true;
23672
23673 if (per_cu->cu->dependencies != NULL)
23674 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23675
23676 return 1;
23677 }
23678
23679 /* Set the mark field in CU and in every other compilation unit in the
23680 cache that we must keep because we are keeping CU. */
23681
23682 static void
23683 dwarf2_mark (struct dwarf2_cu *cu)
23684 {
23685 if (cu->mark)
23686 return;
23687 cu->mark = true;
23688 if (cu->dependencies != NULL)
23689 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23690 }
23691
23692 static void
23693 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23694 {
23695 while (per_cu)
23696 {
23697 per_cu->cu->mark = false;
23698 per_cu = per_cu->cu->read_in_chain;
23699 }
23700 }
23701
23702 /* Trivial hash function for partial_die_info: the hash value of a DIE
23703 is its offset in .debug_info for this objfile. */
23704
23705 static hashval_t
23706 partial_die_hash (const void *item)
23707 {
23708 const struct partial_die_info *part_die
23709 = (const struct partial_die_info *) item;
23710
23711 return to_underlying (part_die->sect_off);
23712 }
23713
23714 /* Trivial comparison function for partial_die_info structures: two DIEs
23715 are equal if they have the same offset. */
23716
23717 static int
23718 partial_die_eq (const void *item_lhs, const void *item_rhs)
23719 {
23720 const struct partial_die_info *part_die_lhs
23721 = (const struct partial_die_info *) item_lhs;
23722 const struct partial_die_info *part_die_rhs
23723 = (const struct partial_die_info *) item_rhs;
23724
23725 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23726 }
23727
23728 struct cmd_list_element *set_dwarf_cmdlist;
23729 struct cmd_list_element *show_dwarf_cmdlist;
23730
23731 static void
23732 show_check_physname (struct ui_file *file, int from_tty,
23733 struct cmd_list_element *c, const char *value)
23734 {
23735 fprintf_filtered (file,
23736 _("Whether to check \"physname\" is %s.\n"),
23737 value);
23738 }
23739
23740 void _initialize_dwarf2_read ();
23741 void
23742 _initialize_dwarf2_read ()
23743 {
23744 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
23745 Set DWARF specific variables.\n\
23746 Configure DWARF variables such as the cache size."),
23747 &set_dwarf_cmdlist, "maintenance set dwarf ",
23748 0/*allow-unknown*/, &maintenance_set_cmdlist);
23749
23750 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
23751 Show DWARF specific variables.\n\
23752 Show DWARF variables such as the cache size."),
23753 &show_dwarf_cmdlist, "maintenance show dwarf ",
23754 0/*allow-unknown*/, &maintenance_show_cmdlist);
23755
23756 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23757 &dwarf_max_cache_age, _("\
23758 Set the upper bound on the age of cached DWARF compilation units."), _("\
23759 Show the upper bound on the age of cached DWARF compilation units."), _("\
23760 A higher limit means that cached compilation units will be stored\n\
23761 in memory longer, and more total memory will be used. Zero disables\n\
23762 caching, which can slow down startup."),
23763 NULL,
23764 show_dwarf_max_cache_age,
23765 &set_dwarf_cmdlist,
23766 &show_dwarf_cmdlist);
23767
23768 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23769 Set debugging of the DWARF reader."), _("\
23770 Show debugging of the DWARF reader."), _("\
23771 When enabled (non-zero), debugging messages are printed during DWARF\n\
23772 reading and symtab expansion. A value of 1 (one) provides basic\n\
23773 information. A value greater than 1 provides more verbose information."),
23774 NULL,
23775 NULL,
23776 &setdebuglist, &showdebuglist);
23777
23778 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23779 Set debugging of the DWARF DIE reader."), _("\
23780 Show debugging of the DWARF DIE reader."), _("\
23781 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23782 The value is the maximum depth to print."),
23783 NULL,
23784 NULL,
23785 &setdebuglist, &showdebuglist);
23786
23787 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23788 Set debugging of the dwarf line reader."), _("\
23789 Show debugging of the dwarf line reader."), _("\
23790 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23791 A value of 1 (one) provides basic information.\n\
23792 A value greater than 1 provides more verbose information."),
23793 NULL,
23794 NULL,
23795 &setdebuglist, &showdebuglist);
23796
23797 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23798 Set cross-checking of \"physname\" code against demangler."), _("\
23799 Show cross-checking of \"physname\" code against demangler."), _("\
23800 When enabled, GDB's internal \"physname\" code is checked against\n\
23801 the demangler."),
23802 NULL, show_check_physname,
23803 &setdebuglist, &showdebuglist);
23804
23805 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23806 no_class, &use_deprecated_index_sections, _("\
23807 Set whether to use deprecated gdb_index sections."), _("\
23808 Show whether to use deprecated gdb_index sections."), _("\
23809 When enabled, deprecated .gdb_index sections are used anyway.\n\
23810 Normally they are ignored either because of a missing feature or\n\
23811 performance issue.\n\
23812 Warning: This option must be enabled before gdb reads the file."),
23813 NULL,
23814 NULL,
23815 &setlist, &showlist);
23816
23817 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23818 &dwarf2_locexpr_funcs);
23819 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23820 &dwarf2_loclist_funcs);
23821
23822 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23823 &dwarf2_block_frame_base_locexpr_funcs);
23824 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23825 &dwarf2_block_frame_base_loclist_funcs);
23826
23827 #if GDB_SELF_TEST
23828 selftests::register_test ("dw2_expand_symtabs_matching",
23829 selftests::dw2_expand_symtabs_matching::run_test);
23830 selftests::register_test ("dwarf2_find_containing_comp_unit",
23831 selftests::find_containing_comp_unit::run_test);
23832 #endif
23833 }