Use the linkage name if it exists
[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 bool * = nullptr);
1440
1441 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1442 struct dwarf2_cu *);
1443
1444 static struct die_info *read_die_and_siblings_1
1445 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1446 struct die_info *);
1447
1448 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1449 const gdb_byte *info_ptr,
1450 const gdb_byte **new_info_ptr,
1451 struct die_info *parent);
1452
1453 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1454 struct die_info **, const gdb_byte *,
1455 int);
1456
1457 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1458 struct die_info **, const gdb_byte *);
1459
1460 static void process_die (struct die_info *, struct dwarf2_cu *);
1461
1462 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1463 struct objfile *);
1464
1465 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1466
1467 static const char *dwarf2_full_name (const char *name,
1468 struct die_info *die,
1469 struct dwarf2_cu *cu);
1470
1471 static const char *dwarf2_physname (const char *name, struct die_info *die,
1472 struct dwarf2_cu *cu);
1473
1474 static struct die_info *dwarf2_extension (struct die_info *die,
1475 struct dwarf2_cu **);
1476
1477 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1478
1479 static void dump_die_for_error (struct die_info *);
1480
1481 static void dump_die_1 (struct ui_file *, int level, int max_level,
1482 struct die_info *);
1483
1484 /*static*/ void dump_die (struct die_info *, int max_level);
1485
1486 static void store_in_ref_table (struct die_info *,
1487 struct dwarf2_cu *);
1488
1489 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1490 const struct attribute *,
1491 struct dwarf2_cu **);
1492
1493 static struct die_info *follow_die_ref (struct die_info *,
1494 const struct attribute *,
1495 struct dwarf2_cu **);
1496
1497 static struct die_info *follow_die_sig (struct die_info *,
1498 const struct attribute *,
1499 struct dwarf2_cu **);
1500
1501 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1502 struct dwarf2_cu *);
1503
1504 static struct type *get_DW_AT_signature_type (struct die_info *,
1505 const struct attribute *,
1506 struct dwarf2_cu *);
1507
1508 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1509
1510 static void read_signatured_type (struct signatured_type *);
1511
1512 static int attr_to_dynamic_prop (const struct attribute *attr,
1513 struct die_info *die, struct dwarf2_cu *cu,
1514 struct dynamic_prop *prop, struct type *type);
1515
1516 /* memory allocation interface */
1517
1518 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1519
1520 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1521
1522 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1523
1524 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1525 struct dwarf2_loclist_baton *baton,
1526 const struct attribute *attr);
1527
1528 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1529 struct symbol *sym,
1530 struct dwarf2_cu *cu,
1531 int is_block);
1532
1533 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1534 const gdb_byte *info_ptr,
1535 struct abbrev_info *abbrev);
1536
1537 static hashval_t partial_die_hash (const void *item);
1538
1539 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1540
1541 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1542 (sect_offset sect_off, unsigned int offset_in_dwz,
1543 struct dwarf2_per_objfile *dwarf2_per_objfile);
1544
1545 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1546 struct die_info *comp_unit_die,
1547 enum language pretend_language);
1548
1549 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1550
1551 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1552
1553 static struct type *set_die_type (struct die_info *, struct type *,
1554 struct dwarf2_cu *);
1555
1556 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1557
1558 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1559
1560 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1561 enum language);
1562
1563 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1564 enum language);
1565
1566 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1567 enum language);
1568
1569 static void dwarf2_add_dependence (struct dwarf2_cu *,
1570 struct dwarf2_per_cu_data *);
1571
1572 static void dwarf2_mark (struct dwarf2_cu *);
1573
1574 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1575
1576 static struct type *get_die_type_at_offset (sect_offset,
1577 struct dwarf2_per_cu_data *);
1578
1579 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1580
1581 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1582 enum language pretend_language);
1583
1584 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1585
1586 /* Class, the destructor of which frees all allocated queue entries. This
1587 will only have work to do if an error was thrown while processing the
1588 dwarf. If no error was thrown then the queue entries should have all
1589 been processed, and freed, as we went along. */
1590
1591 class dwarf2_queue_guard
1592 {
1593 public:
1594 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1595 : m_per_objfile (per_objfile)
1596 {
1597 }
1598
1599 /* Free any entries remaining on the queue. There should only be
1600 entries left if we hit an error while processing the dwarf. */
1601 ~dwarf2_queue_guard ()
1602 {
1603 /* Ensure that no memory is allocated by the queue. */
1604 std::queue<dwarf2_queue_item> empty;
1605 std::swap (m_per_objfile->queue, empty);
1606 }
1607
1608 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1609
1610 private:
1611 dwarf2_per_objfile *m_per_objfile;
1612 };
1613
1614 dwarf2_queue_item::~dwarf2_queue_item ()
1615 {
1616 /* Anything still marked queued is likely to be in an
1617 inconsistent state, so discard it. */
1618 if (per_cu->queued)
1619 {
1620 if (per_cu->cu != NULL)
1621 free_one_cached_comp_unit (per_cu);
1622 per_cu->queued = 0;
1623 }
1624 }
1625
1626 /* The return type of find_file_and_directory. Note, the enclosed
1627 string pointers are only valid while this object is valid. */
1628
1629 struct file_and_directory
1630 {
1631 /* The filename. This is never NULL. */
1632 const char *name;
1633
1634 /* The compilation directory. NULL if not known. If we needed to
1635 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1636 points directly to the DW_AT_comp_dir string attribute owned by
1637 the obstack that owns the DIE. */
1638 const char *comp_dir;
1639
1640 /* If we needed to build a new string for comp_dir, this is what
1641 owns the storage. */
1642 std::string comp_dir_storage;
1643 };
1644
1645 static file_and_directory find_file_and_directory (struct die_info *die,
1646 struct dwarf2_cu *cu);
1647
1648 static htab_up allocate_signatured_type_table ();
1649
1650 static htab_up allocate_dwo_unit_table ();
1651
1652 static struct dwo_unit *lookup_dwo_unit_in_dwp
1653 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1654 struct dwp_file *dwp_file, const char *comp_dir,
1655 ULONGEST signature, int is_debug_types);
1656
1657 static struct dwp_file *get_dwp_file
1658 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1659
1660 static struct dwo_unit *lookup_dwo_comp_unit
1661 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1662
1663 static struct dwo_unit *lookup_dwo_type_unit
1664 (struct signatured_type *, const char *, const char *);
1665
1666 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1667
1668 /* A unique pointer to a dwo_file. */
1669
1670 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1671
1672 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1673
1674 static void check_producer (struct dwarf2_cu *cu);
1675
1676 static void free_line_header_voidp (void *arg);
1677 \f
1678 /* Various complaints about symbol reading that don't abort the process. */
1679
1680 static void
1681 dwarf2_debug_line_missing_file_complaint (void)
1682 {
1683 complaint (_(".debug_line section has line data without a file"));
1684 }
1685
1686 static void
1687 dwarf2_debug_line_missing_end_sequence_complaint (void)
1688 {
1689 complaint (_(".debug_line section has line "
1690 "program sequence without an end"));
1691 }
1692
1693 static void
1694 dwarf2_complex_location_expr_complaint (void)
1695 {
1696 complaint (_("location expression too complex"));
1697 }
1698
1699 static void
1700 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1701 int arg3)
1702 {
1703 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1704 arg1, arg2, arg3);
1705 }
1706
1707 static void
1708 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1709 {
1710 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1711 arg1, arg2);
1712 }
1713
1714 /* Hash function for line_header_hash. */
1715
1716 static hashval_t
1717 line_header_hash (const struct line_header *ofs)
1718 {
1719 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1720 }
1721
1722 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1723
1724 static hashval_t
1725 line_header_hash_voidp (const void *item)
1726 {
1727 const struct line_header *ofs = (const struct line_header *) item;
1728
1729 return line_header_hash (ofs);
1730 }
1731
1732 /* Equality function for line_header_hash. */
1733
1734 static int
1735 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1736 {
1737 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1738 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1739
1740 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1741 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1742 }
1743
1744 \f
1745
1746 /* See declaration. */
1747
1748 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
1749 const dwarf2_debug_sections *names,
1750 bool can_copy_)
1751 : objfile (objfile_),
1752 can_copy (can_copy_)
1753 {
1754 if (names == NULL)
1755 names = &dwarf2_elf_names;
1756
1757 bfd *obfd = objfile->obfd;
1758
1759 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1760 locate_sections (obfd, sec, *names);
1761 }
1762
1763 dwarf2_per_objfile::~dwarf2_per_objfile ()
1764 {
1765 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1766 free_cached_comp_units ();
1767
1768 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1769 per_cu->imported_symtabs_free ();
1770
1771 for (signatured_type *sig_type : all_type_units)
1772 sig_type->per_cu.imported_symtabs_free ();
1773
1774 /* Everything else should be on the objfile obstack. */
1775 }
1776
1777 /* See declaration. */
1778
1779 void
1780 dwarf2_per_objfile::free_cached_comp_units ()
1781 {
1782 dwarf2_per_cu_data *per_cu = read_in_chain;
1783 dwarf2_per_cu_data **last_chain = &read_in_chain;
1784 while (per_cu != NULL)
1785 {
1786 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1787
1788 delete per_cu->cu;
1789 *last_chain = next_cu;
1790 per_cu = next_cu;
1791 }
1792 }
1793
1794 /* A helper class that calls free_cached_comp_units on
1795 destruction. */
1796
1797 class free_cached_comp_units
1798 {
1799 public:
1800
1801 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1802 : m_per_objfile (per_objfile)
1803 {
1804 }
1805
1806 ~free_cached_comp_units ()
1807 {
1808 m_per_objfile->free_cached_comp_units ();
1809 }
1810
1811 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1812
1813 private:
1814
1815 dwarf2_per_objfile *m_per_objfile;
1816 };
1817
1818 /* Try to locate the sections we need for DWARF 2 debugging
1819 information and return true if we have enough to do something.
1820 NAMES points to the dwarf2 section names, or is NULL if the standard
1821 ELF names are used. CAN_COPY is true for formats where symbol
1822 interposition is possible and so symbol values must follow copy
1823 relocation rules. */
1824
1825 int
1826 dwarf2_has_info (struct objfile *objfile,
1827 const struct dwarf2_debug_sections *names,
1828 bool can_copy)
1829 {
1830 if (objfile->flags & OBJF_READNEVER)
1831 return 0;
1832
1833 struct dwarf2_per_objfile *dwarf2_per_objfile
1834 = get_dwarf2_per_objfile (objfile);
1835
1836 if (dwarf2_per_objfile == NULL)
1837 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
1838 names,
1839 can_copy);
1840
1841 return (!dwarf2_per_objfile->info.is_virtual
1842 && dwarf2_per_objfile->info.s.section != NULL
1843 && !dwarf2_per_objfile->abbrev.is_virtual
1844 && dwarf2_per_objfile->abbrev.s.section != NULL);
1845 }
1846
1847 /* When loading sections, we look either for uncompressed section or for
1848 compressed section names. */
1849
1850 static int
1851 section_is_p (const char *section_name,
1852 const struct dwarf2_section_names *names)
1853 {
1854 if (names->normal != NULL
1855 && strcmp (section_name, names->normal) == 0)
1856 return 1;
1857 if (names->compressed != NULL
1858 && strcmp (section_name, names->compressed) == 0)
1859 return 1;
1860 return 0;
1861 }
1862
1863 /* See declaration. */
1864
1865 void
1866 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1867 const dwarf2_debug_sections &names)
1868 {
1869 flagword aflag = bfd_section_flags (sectp);
1870
1871 if ((aflag & SEC_HAS_CONTENTS) == 0)
1872 {
1873 }
1874 else if (elf_section_data (sectp)->this_hdr.sh_size
1875 > bfd_get_file_size (abfd))
1876 {
1877 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1878 warning (_("Discarding section %s which has a section size (%s"
1879 ") larger than the file size [in module %s]"),
1880 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1881 bfd_get_filename (abfd));
1882 }
1883 else if (section_is_p (sectp->name, &names.info))
1884 {
1885 this->info.s.section = sectp;
1886 this->info.size = bfd_section_size (sectp);
1887 }
1888 else if (section_is_p (sectp->name, &names.abbrev))
1889 {
1890 this->abbrev.s.section = sectp;
1891 this->abbrev.size = bfd_section_size (sectp);
1892 }
1893 else if (section_is_p (sectp->name, &names.line))
1894 {
1895 this->line.s.section = sectp;
1896 this->line.size = bfd_section_size (sectp);
1897 }
1898 else if (section_is_p (sectp->name, &names.loc))
1899 {
1900 this->loc.s.section = sectp;
1901 this->loc.size = bfd_section_size (sectp);
1902 }
1903 else if (section_is_p (sectp->name, &names.loclists))
1904 {
1905 this->loclists.s.section = sectp;
1906 this->loclists.size = bfd_section_size (sectp);
1907 }
1908 else if (section_is_p (sectp->name, &names.macinfo))
1909 {
1910 this->macinfo.s.section = sectp;
1911 this->macinfo.size = bfd_section_size (sectp);
1912 }
1913 else if (section_is_p (sectp->name, &names.macro))
1914 {
1915 this->macro.s.section = sectp;
1916 this->macro.size = bfd_section_size (sectp);
1917 }
1918 else if (section_is_p (sectp->name, &names.str))
1919 {
1920 this->str.s.section = sectp;
1921 this->str.size = bfd_section_size (sectp);
1922 }
1923 else if (section_is_p (sectp->name, &names.str_offsets))
1924 {
1925 this->str_offsets.s.section = sectp;
1926 this->str_offsets.size = bfd_section_size (sectp);
1927 }
1928 else if (section_is_p (sectp->name, &names.line_str))
1929 {
1930 this->line_str.s.section = sectp;
1931 this->line_str.size = bfd_section_size (sectp);
1932 }
1933 else if (section_is_p (sectp->name, &names.addr))
1934 {
1935 this->addr.s.section = sectp;
1936 this->addr.size = bfd_section_size (sectp);
1937 }
1938 else if (section_is_p (sectp->name, &names.frame))
1939 {
1940 this->frame.s.section = sectp;
1941 this->frame.size = bfd_section_size (sectp);
1942 }
1943 else if (section_is_p (sectp->name, &names.eh_frame))
1944 {
1945 this->eh_frame.s.section = sectp;
1946 this->eh_frame.size = bfd_section_size (sectp);
1947 }
1948 else if (section_is_p (sectp->name, &names.ranges))
1949 {
1950 this->ranges.s.section = sectp;
1951 this->ranges.size = bfd_section_size (sectp);
1952 }
1953 else if (section_is_p (sectp->name, &names.rnglists))
1954 {
1955 this->rnglists.s.section = sectp;
1956 this->rnglists.size = bfd_section_size (sectp);
1957 }
1958 else if (section_is_p (sectp->name, &names.types))
1959 {
1960 struct dwarf2_section_info type_section;
1961
1962 memset (&type_section, 0, sizeof (type_section));
1963 type_section.s.section = sectp;
1964 type_section.size = bfd_section_size (sectp);
1965
1966 this->types.push_back (type_section);
1967 }
1968 else if (section_is_p (sectp->name, &names.gdb_index))
1969 {
1970 this->gdb_index.s.section = sectp;
1971 this->gdb_index.size = bfd_section_size (sectp);
1972 }
1973 else if (section_is_p (sectp->name, &names.debug_names))
1974 {
1975 this->debug_names.s.section = sectp;
1976 this->debug_names.size = bfd_section_size (sectp);
1977 }
1978 else if (section_is_p (sectp->name, &names.debug_aranges))
1979 {
1980 this->debug_aranges.s.section = sectp;
1981 this->debug_aranges.size = bfd_section_size (sectp);
1982 }
1983
1984 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1985 && bfd_section_vma (sectp) == 0)
1986 this->has_section_at_zero = true;
1987 }
1988
1989 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1990 SECTION_NAME. */
1991
1992 void
1993 dwarf2_get_section_info (struct objfile *objfile,
1994 enum dwarf2_section_enum sect,
1995 asection **sectp, const gdb_byte **bufp,
1996 bfd_size_type *sizep)
1997 {
1998 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
1999 struct dwarf2_section_info *info;
2000
2001 /* We may see an objfile without any DWARF, in which case we just
2002 return nothing. */
2003 if (data == NULL)
2004 {
2005 *sectp = NULL;
2006 *bufp = NULL;
2007 *sizep = 0;
2008 return;
2009 }
2010 switch (sect)
2011 {
2012 case DWARF2_DEBUG_FRAME:
2013 info = &data->frame;
2014 break;
2015 case DWARF2_EH_FRAME:
2016 info = &data->eh_frame;
2017 break;
2018 default:
2019 gdb_assert_not_reached ("unexpected section");
2020 }
2021
2022 info->read (objfile);
2023
2024 *sectp = info->get_bfd_section ();
2025 *bufp = info->buffer;
2026 *sizep = info->size;
2027 }
2028
2029 /* A helper function to find the sections for a .dwz file. */
2030
2031 static void
2032 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2033 {
2034 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2035
2036 /* Note that we only support the standard ELF names, because .dwz
2037 is ELF-only (at the time of writing). */
2038 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2039 {
2040 dwz_file->abbrev.s.section = sectp;
2041 dwz_file->abbrev.size = bfd_section_size (sectp);
2042 }
2043 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2044 {
2045 dwz_file->info.s.section = sectp;
2046 dwz_file->info.size = bfd_section_size (sectp);
2047 }
2048 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2049 {
2050 dwz_file->str.s.section = sectp;
2051 dwz_file->str.size = bfd_section_size (sectp);
2052 }
2053 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2054 {
2055 dwz_file->line.s.section = sectp;
2056 dwz_file->line.size = bfd_section_size (sectp);
2057 }
2058 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2059 {
2060 dwz_file->macro.s.section = sectp;
2061 dwz_file->macro.size = bfd_section_size (sectp);
2062 }
2063 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2064 {
2065 dwz_file->gdb_index.s.section = sectp;
2066 dwz_file->gdb_index.size = bfd_section_size (sectp);
2067 }
2068 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2069 {
2070 dwz_file->debug_names.s.section = sectp;
2071 dwz_file->debug_names.size = bfd_section_size (sectp);
2072 }
2073 }
2074
2075 /* See dwarf2read.h. */
2076
2077 struct dwz_file *
2078 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2079 {
2080 const char *filename;
2081 bfd_size_type buildid_len_arg;
2082 size_t buildid_len;
2083 bfd_byte *buildid;
2084
2085 if (dwarf2_per_objfile->dwz_file != NULL)
2086 return dwarf2_per_objfile->dwz_file.get ();
2087
2088 bfd_set_error (bfd_error_no_error);
2089 gdb::unique_xmalloc_ptr<char> data
2090 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2091 &buildid_len_arg, &buildid));
2092 if (data == NULL)
2093 {
2094 if (bfd_get_error () == bfd_error_no_error)
2095 return NULL;
2096 error (_("could not read '.gnu_debugaltlink' section: %s"),
2097 bfd_errmsg (bfd_get_error ()));
2098 }
2099
2100 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2101
2102 buildid_len = (size_t) buildid_len_arg;
2103
2104 filename = data.get ();
2105
2106 std::string abs_storage;
2107 if (!IS_ABSOLUTE_PATH (filename))
2108 {
2109 gdb::unique_xmalloc_ptr<char> abs
2110 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2111
2112 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2113 filename = abs_storage.c_str ();
2114 }
2115
2116 /* First try the file name given in the section. If that doesn't
2117 work, try to use the build-id instead. */
2118 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2119 if (dwz_bfd != NULL)
2120 {
2121 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2122 dwz_bfd.reset (nullptr);
2123 }
2124
2125 if (dwz_bfd == NULL)
2126 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2127
2128 if (dwz_bfd == nullptr)
2129 {
2130 gdb::unique_xmalloc_ptr<char> alt_filename;
2131 const char *origname = dwarf2_per_objfile->objfile->original_name;
2132
2133 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2134 buildid_len,
2135 origname,
2136 &alt_filename));
2137
2138 if (fd.get () >= 0)
2139 {
2140 /* File successfully retrieved from server. */
2141 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2142
2143 if (dwz_bfd == nullptr)
2144 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2145 alt_filename.get ());
2146 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2147 dwz_bfd.reset (nullptr);
2148 }
2149 }
2150
2151 if (dwz_bfd == NULL)
2152 error (_("could not find '.gnu_debugaltlink' file for %s"),
2153 objfile_name (dwarf2_per_objfile->objfile));
2154
2155 std::unique_ptr<struct dwz_file> result
2156 (new struct dwz_file (std::move (dwz_bfd)));
2157
2158 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2159 result.get ());
2160
2161 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2162 result->dwz_bfd.get ());
2163 dwarf2_per_objfile->dwz_file = std::move (result);
2164 return dwarf2_per_objfile->dwz_file.get ();
2165 }
2166 \f
2167 /* DWARF quick_symbols_functions support. */
2168
2169 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2170 unique line tables, so we maintain a separate table of all .debug_line
2171 derived entries to support the sharing.
2172 All the quick functions need is the list of file names. We discard the
2173 line_header when we're done and don't need to record it here. */
2174 struct quick_file_names
2175 {
2176 /* The data used to construct the hash key. */
2177 struct stmt_list_hash hash;
2178
2179 /* The number of entries in file_names, real_names. */
2180 unsigned int num_file_names;
2181
2182 /* The file names from the line table, after being run through
2183 file_full_name. */
2184 const char **file_names;
2185
2186 /* The file names from the line table after being run through
2187 gdb_realpath. These are computed lazily. */
2188 const char **real_names;
2189 };
2190
2191 /* When using the index (and thus not using psymtabs), each CU has an
2192 object of this type. This is used to hold information needed by
2193 the various "quick" methods. */
2194 struct dwarf2_per_cu_quick_data
2195 {
2196 /* The file table. This can be NULL if there was no file table
2197 or it's currently not read in.
2198 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2199 struct quick_file_names *file_names;
2200
2201 /* The corresponding symbol table. This is NULL if symbols for this
2202 CU have not yet been read. */
2203 struct compunit_symtab *compunit_symtab;
2204
2205 /* A temporary mark bit used when iterating over all CUs in
2206 expand_symtabs_matching. */
2207 unsigned int mark : 1;
2208
2209 /* True if we've tried to read the file table and found there isn't one.
2210 There will be no point in trying to read it again next time. */
2211 unsigned int no_file_data : 1;
2212 };
2213
2214 /* Utility hash function for a stmt_list_hash. */
2215
2216 static hashval_t
2217 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2218 {
2219 hashval_t v = 0;
2220
2221 if (stmt_list_hash->dwo_unit != NULL)
2222 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2223 v += to_underlying (stmt_list_hash->line_sect_off);
2224 return v;
2225 }
2226
2227 /* Utility equality function for a stmt_list_hash. */
2228
2229 static int
2230 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2231 const struct stmt_list_hash *rhs)
2232 {
2233 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2234 return 0;
2235 if (lhs->dwo_unit != NULL
2236 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2237 return 0;
2238
2239 return lhs->line_sect_off == rhs->line_sect_off;
2240 }
2241
2242 /* Hash function for a quick_file_names. */
2243
2244 static hashval_t
2245 hash_file_name_entry (const void *e)
2246 {
2247 const struct quick_file_names *file_data
2248 = (const struct quick_file_names *) e;
2249
2250 return hash_stmt_list_entry (&file_data->hash);
2251 }
2252
2253 /* Equality function for a quick_file_names. */
2254
2255 static int
2256 eq_file_name_entry (const void *a, const void *b)
2257 {
2258 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2259 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2260
2261 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2262 }
2263
2264 /* Delete function for a quick_file_names. */
2265
2266 static void
2267 delete_file_name_entry (void *e)
2268 {
2269 struct quick_file_names *file_data = (struct quick_file_names *) e;
2270 int i;
2271
2272 for (i = 0; i < file_data->num_file_names; ++i)
2273 {
2274 xfree ((void*) file_data->file_names[i]);
2275 if (file_data->real_names)
2276 xfree ((void*) file_data->real_names[i]);
2277 }
2278
2279 /* The space for the struct itself lives on objfile_obstack,
2280 so we don't free it here. */
2281 }
2282
2283 /* Create a quick_file_names hash table. */
2284
2285 static htab_up
2286 create_quick_file_names_table (unsigned int nr_initial_entries)
2287 {
2288 return htab_up (htab_create_alloc (nr_initial_entries,
2289 hash_file_name_entry, eq_file_name_entry,
2290 delete_file_name_entry, xcalloc, xfree));
2291 }
2292
2293 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2294 have to be created afterwards. You should call age_cached_comp_units after
2295 processing PER_CU->CU. dw2_setup must have been already called. */
2296
2297 static void
2298 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2299 {
2300 if (per_cu->is_debug_types)
2301 load_full_type_unit (per_cu);
2302 else
2303 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2304
2305 if (per_cu->cu == NULL)
2306 return; /* Dummy CU. */
2307
2308 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2309 }
2310
2311 /* Read in the symbols for PER_CU. */
2312
2313 static void
2314 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2315 {
2316 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2317
2318 /* Skip type_unit_groups, reading the type units they contain
2319 is handled elsewhere. */
2320 if (per_cu->type_unit_group_p ())
2321 return;
2322
2323 /* The destructor of dwarf2_queue_guard frees any entries left on
2324 the queue. After this point we're guaranteed to leave this function
2325 with the dwarf queue empty. */
2326 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2327
2328 if (dwarf2_per_objfile->using_index
2329 ? per_cu->v.quick->compunit_symtab == NULL
2330 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2331 {
2332 queue_comp_unit (per_cu, language_minimal);
2333 load_cu (per_cu, skip_partial);
2334
2335 /* If we just loaded a CU from a DWO, and we're working with an index
2336 that may badly handle TUs, load all the TUs in that DWO as well.
2337 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2338 if (!per_cu->is_debug_types
2339 && per_cu->cu != NULL
2340 && per_cu->cu->dwo_unit != NULL
2341 && dwarf2_per_objfile->index_table != NULL
2342 && dwarf2_per_objfile->index_table->version <= 7
2343 /* DWP files aren't supported yet. */
2344 && get_dwp_file (dwarf2_per_objfile) == NULL)
2345 queue_and_load_all_dwo_tus (per_cu);
2346 }
2347
2348 process_queue (dwarf2_per_objfile);
2349
2350 /* Age the cache, releasing compilation units that have not
2351 been used recently. */
2352 age_cached_comp_units (dwarf2_per_objfile);
2353 }
2354
2355 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2356 the objfile from which this CU came. Returns the resulting symbol
2357 table. */
2358
2359 static struct compunit_symtab *
2360 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2361 {
2362 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2363
2364 gdb_assert (dwarf2_per_objfile->using_index);
2365 if (!per_cu->v.quick->compunit_symtab)
2366 {
2367 free_cached_comp_units freer (dwarf2_per_objfile);
2368 scoped_restore decrementer = increment_reading_symtab ();
2369 dw2_do_instantiate_symtab (per_cu, skip_partial);
2370 process_cu_includes (dwarf2_per_objfile);
2371 }
2372
2373 return per_cu->v.quick->compunit_symtab;
2374 }
2375
2376 /* See declaration. */
2377
2378 dwarf2_per_cu_data *
2379 dwarf2_per_objfile::get_cutu (int index)
2380 {
2381 if (index >= this->all_comp_units.size ())
2382 {
2383 index -= this->all_comp_units.size ();
2384 gdb_assert (index < this->all_type_units.size ());
2385 return &this->all_type_units[index]->per_cu;
2386 }
2387
2388 return this->all_comp_units[index];
2389 }
2390
2391 /* See declaration. */
2392
2393 dwarf2_per_cu_data *
2394 dwarf2_per_objfile::get_cu (int index)
2395 {
2396 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2397
2398 return this->all_comp_units[index];
2399 }
2400
2401 /* See declaration. */
2402
2403 signatured_type *
2404 dwarf2_per_objfile::get_tu (int index)
2405 {
2406 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2407
2408 return this->all_type_units[index];
2409 }
2410
2411 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2412 objfile_obstack, and constructed with the specified field
2413 values. */
2414
2415 static dwarf2_per_cu_data *
2416 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2417 struct dwarf2_section_info *section,
2418 int is_dwz,
2419 sect_offset sect_off, ULONGEST length)
2420 {
2421 struct objfile *objfile = dwarf2_per_objfile->objfile;
2422 dwarf2_per_cu_data *the_cu
2423 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2424 struct dwarf2_per_cu_data);
2425 the_cu->sect_off = sect_off;
2426 the_cu->length = length;
2427 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2428 the_cu->section = section;
2429 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2430 struct dwarf2_per_cu_quick_data);
2431 the_cu->is_dwz = is_dwz;
2432 return the_cu;
2433 }
2434
2435 /* A helper for create_cus_from_index that handles a given list of
2436 CUs. */
2437
2438 static void
2439 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2440 const gdb_byte *cu_list, offset_type n_elements,
2441 struct dwarf2_section_info *section,
2442 int is_dwz)
2443 {
2444 for (offset_type i = 0; i < n_elements; i += 2)
2445 {
2446 gdb_static_assert (sizeof (ULONGEST) >= 8);
2447
2448 sect_offset sect_off
2449 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2450 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2451 cu_list += 2 * 8;
2452
2453 dwarf2_per_cu_data *per_cu
2454 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2455 sect_off, length);
2456 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2457 }
2458 }
2459
2460 /* Read the CU list from the mapped index, and use it to create all
2461 the CU objects for this objfile. */
2462
2463 static void
2464 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2465 const gdb_byte *cu_list, offset_type cu_list_elements,
2466 const gdb_byte *dwz_list, offset_type dwz_elements)
2467 {
2468 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2469 dwarf2_per_objfile->all_comp_units.reserve
2470 ((cu_list_elements + dwz_elements) / 2);
2471
2472 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2473 &dwarf2_per_objfile->info, 0);
2474
2475 if (dwz_elements == 0)
2476 return;
2477
2478 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2479 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2480 &dwz->info, 1);
2481 }
2482
2483 /* Create the signatured type hash table from the index. */
2484
2485 static void
2486 create_signatured_type_table_from_index
2487 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2488 struct dwarf2_section_info *section,
2489 const gdb_byte *bytes,
2490 offset_type elements)
2491 {
2492 struct objfile *objfile = dwarf2_per_objfile->objfile;
2493
2494 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2495 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2496
2497 htab_up sig_types_hash = allocate_signatured_type_table ();
2498
2499 for (offset_type i = 0; i < elements; i += 3)
2500 {
2501 struct signatured_type *sig_type;
2502 ULONGEST signature;
2503 void **slot;
2504 cu_offset type_offset_in_tu;
2505
2506 gdb_static_assert (sizeof (ULONGEST) >= 8);
2507 sect_offset sect_off
2508 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2509 type_offset_in_tu
2510 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2511 BFD_ENDIAN_LITTLE);
2512 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2513 bytes += 3 * 8;
2514
2515 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2516 struct signatured_type);
2517 sig_type->signature = signature;
2518 sig_type->type_offset_in_tu = type_offset_in_tu;
2519 sig_type->per_cu.is_debug_types = 1;
2520 sig_type->per_cu.section = section;
2521 sig_type->per_cu.sect_off = sect_off;
2522 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2523 sig_type->per_cu.v.quick
2524 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2525 struct dwarf2_per_cu_quick_data);
2526
2527 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2528 *slot = sig_type;
2529
2530 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2531 }
2532
2533 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2534 }
2535
2536 /* Create the signatured type hash table from .debug_names. */
2537
2538 static void
2539 create_signatured_type_table_from_debug_names
2540 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2541 const mapped_debug_names &map,
2542 struct dwarf2_section_info *section,
2543 struct dwarf2_section_info *abbrev_section)
2544 {
2545 struct objfile *objfile = dwarf2_per_objfile->objfile;
2546
2547 section->read (objfile);
2548 abbrev_section->read (objfile);
2549
2550 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2551 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2552
2553 htab_up sig_types_hash = allocate_signatured_type_table ();
2554
2555 for (uint32_t i = 0; i < map.tu_count; ++i)
2556 {
2557 struct signatured_type *sig_type;
2558 void **slot;
2559
2560 sect_offset sect_off
2561 = (sect_offset) (extract_unsigned_integer
2562 (map.tu_table_reordered + i * map.offset_size,
2563 map.offset_size,
2564 map.dwarf5_byte_order));
2565
2566 comp_unit_head cu_header;
2567 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2568 abbrev_section,
2569 section->buffer + to_underlying (sect_off),
2570 rcuh_kind::TYPE);
2571
2572 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2573 struct signatured_type);
2574 sig_type->signature = cu_header.signature;
2575 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2576 sig_type->per_cu.is_debug_types = 1;
2577 sig_type->per_cu.section = section;
2578 sig_type->per_cu.sect_off = sect_off;
2579 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2580 sig_type->per_cu.v.quick
2581 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2582 struct dwarf2_per_cu_quick_data);
2583
2584 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2585 *slot = sig_type;
2586
2587 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2588 }
2589
2590 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2591 }
2592
2593 /* Read the address map data from the mapped index, and use it to
2594 populate the objfile's psymtabs_addrmap. */
2595
2596 static void
2597 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2598 struct mapped_index *index)
2599 {
2600 struct objfile *objfile = dwarf2_per_objfile->objfile;
2601 struct gdbarch *gdbarch = objfile->arch ();
2602 const gdb_byte *iter, *end;
2603 struct addrmap *mutable_map;
2604 CORE_ADDR baseaddr;
2605
2606 auto_obstack temp_obstack;
2607
2608 mutable_map = addrmap_create_mutable (&temp_obstack);
2609
2610 iter = index->address_table.data ();
2611 end = iter + index->address_table.size ();
2612
2613 baseaddr = objfile->text_section_offset ();
2614
2615 while (iter < end)
2616 {
2617 ULONGEST hi, lo, cu_index;
2618 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2619 iter += 8;
2620 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2621 iter += 8;
2622 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2623 iter += 4;
2624
2625 if (lo > hi)
2626 {
2627 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2628 hex_string (lo), hex_string (hi));
2629 continue;
2630 }
2631
2632 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2633 {
2634 complaint (_(".gdb_index address table has invalid CU number %u"),
2635 (unsigned) cu_index);
2636 continue;
2637 }
2638
2639 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2640 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2641 addrmap_set_empty (mutable_map, lo, hi - 1,
2642 dwarf2_per_objfile->get_cu (cu_index));
2643 }
2644
2645 objfile->partial_symtabs->psymtabs_addrmap
2646 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2647 }
2648
2649 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2650 populate the objfile's psymtabs_addrmap. */
2651
2652 static void
2653 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2654 struct dwarf2_section_info *section)
2655 {
2656 struct objfile *objfile = dwarf2_per_objfile->objfile;
2657 bfd *abfd = objfile->obfd;
2658 struct gdbarch *gdbarch = objfile->arch ();
2659 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2660
2661 auto_obstack temp_obstack;
2662 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2663
2664 std::unordered_map<sect_offset,
2665 dwarf2_per_cu_data *,
2666 gdb::hash_enum<sect_offset>>
2667 debug_info_offset_to_per_cu;
2668 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2669 {
2670 const auto insertpair
2671 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2672 if (!insertpair.second)
2673 {
2674 warning (_("Section .debug_aranges in %s has duplicate "
2675 "debug_info_offset %s, ignoring .debug_aranges."),
2676 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2677 return;
2678 }
2679 }
2680
2681 section->read (objfile);
2682
2683 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2684
2685 const gdb_byte *addr = section->buffer;
2686
2687 while (addr < section->buffer + section->size)
2688 {
2689 const gdb_byte *const entry_addr = addr;
2690 unsigned int bytes_read;
2691
2692 const LONGEST entry_length = read_initial_length (abfd, addr,
2693 &bytes_read);
2694 addr += bytes_read;
2695
2696 const gdb_byte *const entry_end = addr + entry_length;
2697 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2698 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2699 if (addr + entry_length > section->buffer + section->size)
2700 {
2701 warning (_("Section .debug_aranges in %s entry at offset %s "
2702 "length %s exceeds section length %s, "
2703 "ignoring .debug_aranges."),
2704 objfile_name (objfile),
2705 plongest (entry_addr - section->buffer),
2706 plongest (bytes_read + entry_length),
2707 pulongest (section->size));
2708 return;
2709 }
2710
2711 /* The version number. */
2712 const uint16_t version = read_2_bytes (abfd, addr);
2713 addr += 2;
2714 if (version != 2)
2715 {
2716 warning (_("Section .debug_aranges in %s entry at offset %s "
2717 "has unsupported version %d, ignoring .debug_aranges."),
2718 objfile_name (objfile),
2719 plongest (entry_addr - section->buffer), version);
2720 return;
2721 }
2722
2723 const uint64_t debug_info_offset
2724 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2725 addr += offset_size;
2726 const auto per_cu_it
2727 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2728 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2729 {
2730 warning (_("Section .debug_aranges in %s entry at offset %s "
2731 "debug_info_offset %s does not exists, "
2732 "ignoring .debug_aranges."),
2733 objfile_name (objfile),
2734 plongest (entry_addr - section->buffer),
2735 pulongest (debug_info_offset));
2736 return;
2737 }
2738 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2739
2740 const uint8_t address_size = *addr++;
2741 if (address_size < 1 || address_size > 8)
2742 {
2743 warning (_("Section .debug_aranges in %s entry at offset %s "
2744 "address_size %u is invalid, ignoring .debug_aranges."),
2745 objfile_name (objfile),
2746 plongest (entry_addr - section->buffer), address_size);
2747 return;
2748 }
2749
2750 const uint8_t segment_selector_size = *addr++;
2751 if (segment_selector_size != 0)
2752 {
2753 warning (_("Section .debug_aranges in %s entry at offset %s "
2754 "segment_selector_size %u is not supported, "
2755 "ignoring .debug_aranges."),
2756 objfile_name (objfile),
2757 plongest (entry_addr - section->buffer),
2758 segment_selector_size);
2759 return;
2760 }
2761
2762 /* Must pad to an alignment boundary that is twice the address
2763 size. It is undocumented by the DWARF standard but GCC does
2764 use it. */
2765 for (size_t padding = ((-(addr - section->buffer))
2766 & (2 * address_size - 1));
2767 padding > 0; padding--)
2768 if (*addr++ != 0)
2769 {
2770 warning (_("Section .debug_aranges in %s entry at offset %s "
2771 "padding is not zero, ignoring .debug_aranges."),
2772 objfile_name (objfile),
2773 plongest (entry_addr - section->buffer));
2774 return;
2775 }
2776
2777 for (;;)
2778 {
2779 if (addr + 2 * address_size > entry_end)
2780 {
2781 warning (_("Section .debug_aranges in %s entry at offset %s "
2782 "address list is not properly terminated, "
2783 "ignoring .debug_aranges."),
2784 objfile_name (objfile),
2785 plongest (entry_addr - section->buffer));
2786 return;
2787 }
2788 ULONGEST start = extract_unsigned_integer (addr, address_size,
2789 dwarf5_byte_order);
2790 addr += address_size;
2791 ULONGEST length = extract_unsigned_integer (addr, address_size,
2792 dwarf5_byte_order);
2793 addr += address_size;
2794 if (start == 0 && length == 0)
2795 break;
2796 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2797 {
2798 /* Symbol was eliminated due to a COMDAT group. */
2799 continue;
2800 }
2801 ULONGEST end = start + length;
2802 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2803 - baseaddr);
2804 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2805 - baseaddr);
2806 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2807 }
2808 }
2809
2810 objfile->partial_symtabs->psymtabs_addrmap
2811 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2812 }
2813
2814 /* Find a slot in the mapped index INDEX for the object named NAME.
2815 If NAME is found, set *VEC_OUT to point to the CU vector in the
2816 constant pool and return true. If NAME cannot be found, return
2817 false. */
2818
2819 static bool
2820 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2821 offset_type **vec_out)
2822 {
2823 offset_type hash;
2824 offset_type slot, step;
2825 int (*cmp) (const char *, const char *);
2826
2827 gdb::unique_xmalloc_ptr<char> without_params;
2828 if (current_language->la_language == language_cplus
2829 || current_language->la_language == language_fortran
2830 || current_language->la_language == language_d)
2831 {
2832 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2833 not contain any. */
2834
2835 if (strchr (name, '(') != NULL)
2836 {
2837 without_params = cp_remove_params (name);
2838
2839 if (without_params != NULL)
2840 name = without_params.get ();
2841 }
2842 }
2843
2844 /* Index version 4 did not support case insensitive searches. But the
2845 indices for case insensitive languages are built in lowercase, therefore
2846 simulate our NAME being searched is also lowercased. */
2847 hash = mapped_index_string_hash ((index->version == 4
2848 && case_sensitivity == case_sensitive_off
2849 ? 5 : index->version),
2850 name);
2851
2852 slot = hash & (index->symbol_table.size () - 1);
2853 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2854 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2855
2856 for (;;)
2857 {
2858 const char *str;
2859
2860 const auto &bucket = index->symbol_table[slot];
2861 if (bucket.name == 0 && bucket.vec == 0)
2862 return false;
2863
2864 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2865 if (!cmp (name, str))
2866 {
2867 *vec_out = (offset_type *) (index->constant_pool
2868 + MAYBE_SWAP (bucket.vec));
2869 return true;
2870 }
2871
2872 slot = (slot + step) & (index->symbol_table.size () - 1);
2873 }
2874 }
2875
2876 /* A helper function that reads the .gdb_index from BUFFER and fills
2877 in MAP. FILENAME is the name of the file containing the data;
2878 it is used for error reporting. DEPRECATED_OK is true if it is
2879 ok to use deprecated sections.
2880
2881 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2882 out parameters that are filled in with information about the CU and
2883 TU lists in the section.
2884
2885 Returns true if all went well, false otherwise. */
2886
2887 static bool
2888 read_gdb_index_from_buffer (const char *filename,
2889 bool deprecated_ok,
2890 gdb::array_view<const gdb_byte> buffer,
2891 struct mapped_index *map,
2892 const gdb_byte **cu_list,
2893 offset_type *cu_list_elements,
2894 const gdb_byte **types_list,
2895 offset_type *types_list_elements)
2896 {
2897 const gdb_byte *addr = &buffer[0];
2898
2899 /* Version check. */
2900 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2901 /* Versions earlier than 3 emitted every copy of a psymbol. This
2902 causes the index to behave very poorly for certain requests. Version 3
2903 contained incomplete addrmap. So, it seems better to just ignore such
2904 indices. */
2905 if (version < 4)
2906 {
2907 static int warning_printed = 0;
2908 if (!warning_printed)
2909 {
2910 warning (_("Skipping obsolete .gdb_index section in %s."),
2911 filename);
2912 warning_printed = 1;
2913 }
2914 return 0;
2915 }
2916 /* Index version 4 uses a different hash function than index version
2917 5 and later.
2918
2919 Versions earlier than 6 did not emit psymbols for inlined
2920 functions. Using these files will cause GDB not to be able to
2921 set breakpoints on inlined functions by name, so we ignore these
2922 indices unless the user has done
2923 "set use-deprecated-index-sections on". */
2924 if (version < 6 && !deprecated_ok)
2925 {
2926 static int warning_printed = 0;
2927 if (!warning_printed)
2928 {
2929 warning (_("\
2930 Skipping deprecated .gdb_index section in %s.\n\
2931 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2932 to use the section anyway."),
2933 filename);
2934 warning_printed = 1;
2935 }
2936 return 0;
2937 }
2938 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2939 of the TU (for symbols coming from TUs),
2940 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2941 Plus gold-generated indices can have duplicate entries for global symbols,
2942 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2943 These are just performance bugs, and we can't distinguish gdb-generated
2944 indices from gold-generated ones, so issue no warning here. */
2945
2946 /* Indexes with higher version than the one supported by GDB may be no
2947 longer backward compatible. */
2948 if (version > 8)
2949 return 0;
2950
2951 map->version = version;
2952
2953 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2954
2955 int i = 0;
2956 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2957 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2958 / 8);
2959 ++i;
2960
2961 *types_list = addr + MAYBE_SWAP (metadata[i]);
2962 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2963 - MAYBE_SWAP (metadata[i]))
2964 / 8);
2965 ++i;
2966
2967 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2968 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2969 map->address_table
2970 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2971 ++i;
2972
2973 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2974 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2975 map->symbol_table
2976 = gdb::array_view<mapped_index::symbol_table_slot>
2977 ((mapped_index::symbol_table_slot *) symbol_table,
2978 (mapped_index::symbol_table_slot *) symbol_table_end);
2979
2980 ++i;
2981 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2982
2983 return 1;
2984 }
2985
2986 /* Callback types for dwarf2_read_gdb_index. */
2987
2988 typedef gdb::function_view
2989 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2990 get_gdb_index_contents_ftype;
2991 typedef gdb::function_view
2992 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2993 get_gdb_index_contents_dwz_ftype;
2994
2995 /* Read .gdb_index. If everything went ok, initialize the "quick"
2996 elements of all the CUs and return 1. Otherwise, return 0. */
2997
2998 static int
2999 dwarf2_read_gdb_index
3000 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3001 get_gdb_index_contents_ftype get_gdb_index_contents,
3002 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3003 {
3004 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3005 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3006 struct dwz_file *dwz;
3007 struct objfile *objfile = dwarf2_per_objfile->objfile;
3008
3009 gdb::array_view<const gdb_byte> main_index_contents
3010 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3011
3012 if (main_index_contents.empty ())
3013 return 0;
3014
3015 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3016 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3017 use_deprecated_index_sections,
3018 main_index_contents, map.get (), &cu_list,
3019 &cu_list_elements, &types_list,
3020 &types_list_elements))
3021 return 0;
3022
3023 /* Don't use the index if it's empty. */
3024 if (map->symbol_table.empty ())
3025 return 0;
3026
3027 /* If there is a .dwz file, read it so we can get its CU list as
3028 well. */
3029 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3030 if (dwz != NULL)
3031 {
3032 struct mapped_index dwz_map;
3033 const gdb_byte *dwz_types_ignore;
3034 offset_type dwz_types_elements_ignore;
3035
3036 gdb::array_view<const gdb_byte> dwz_index_content
3037 = get_gdb_index_contents_dwz (objfile, dwz);
3038
3039 if (dwz_index_content.empty ())
3040 return 0;
3041
3042 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3043 1, dwz_index_content, &dwz_map,
3044 &dwz_list, &dwz_list_elements,
3045 &dwz_types_ignore,
3046 &dwz_types_elements_ignore))
3047 {
3048 warning (_("could not read '.gdb_index' section from %s; skipping"),
3049 bfd_get_filename (dwz->dwz_bfd.get ()));
3050 return 0;
3051 }
3052 }
3053
3054 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3055 dwz_list, dwz_list_elements);
3056
3057 if (types_list_elements)
3058 {
3059 /* We can only handle a single .debug_types when we have an
3060 index. */
3061 if (dwarf2_per_objfile->types.size () != 1)
3062 return 0;
3063
3064 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3065
3066 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3067 types_list, types_list_elements);
3068 }
3069
3070 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3071
3072 dwarf2_per_objfile->index_table = std::move (map);
3073 dwarf2_per_objfile->using_index = 1;
3074 dwarf2_per_objfile->quick_file_names_table =
3075 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3076
3077 return 1;
3078 }
3079
3080 /* die_reader_func for dw2_get_file_names. */
3081
3082 static void
3083 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3084 const gdb_byte *info_ptr,
3085 struct die_info *comp_unit_die)
3086 {
3087 struct dwarf2_cu *cu = reader->cu;
3088 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3089 struct dwarf2_per_objfile *dwarf2_per_objfile
3090 = cu->per_cu->dwarf2_per_objfile;
3091 struct objfile *objfile = dwarf2_per_objfile->objfile;
3092 struct dwarf2_per_cu_data *lh_cu;
3093 struct attribute *attr;
3094 void **slot;
3095 struct quick_file_names *qfn;
3096
3097 gdb_assert (! this_cu->is_debug_types);
3098
3099 /* Our callers never want to match partial units -- instead they
3100 will match the enclosing full CU. */
3101 if (comp_unit_die->tag == DW_TAG_partial_unit)
3102 {
3103 this_cu->v.quick->no_file_data = 1;
3104 return;
3105 }
3106
3107 lh_cu = this_cu;
3108 slot = NULL;
3109
3110 line_header_up lh;
3111 sect_offset line_offset {};
3112
3113 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3114 if (attr != nullptr)
3115 {
3116 struct quick_file_names find_entry;
3117
3118 line_offset = (sect_offset) DW_UNSND (attr);
3119
3120 /* We may have already read in this line header (TU line header sharing).
3121 If we have we're done. */
3122 find_entry.hash.dwo_unit = cu->dwo_unit;
3123 find_entry.hash.line_sect_off = line_offset;
3124 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3125 &find_entry, INSERT);
3126 if (*slot != NULL)
3127 {
3128 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3129 return;
3130 }
3131
3132 lh = dwarf_decode_line_header (line_offset, cu);
3133 }
3134 if (lh == NULL)
3135 {
3136 lh_cu->v.quick->no_file_data = 1;
3137 return;
3138 }
3139
3140 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3141 qfn->hash.dwo_unit = cu->dwo_unit;
3142 qfn->hash.line_sect_off = line_offset;
3143 gdb_assert (slot != NULL);
3144 *slot = qfn;
3145
3146 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3147
3148 int offset = 0;
3149 if (strcmp (fnd.name, "<unknown>") != 0)
3150 ++offset;
3151
3152 qfn->num_file_names = offset + lh->file_names_size ();
3153 qfn->file_names =
3154 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3155 if (offset != 0)
3156 qfn->file_names[0] = xstrdup (fnd.name);
3157 for (int i = 0; i < lh->file_names_size (); ++i)
3158 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3159 fnd.comp_dir).release ();
3160 qfn->real_names = NULL;
3161
3162 lh_cu->v.quick->file_names = qfn;
3163 }
3164
3165 /* A helper for the "quick" functions which attempts to read the line
3166 table for THIS_CU. */
3167
3168 static struct quick_file_names *
3169 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3170 {
3171 /* This should never be called for TUs. */
3172 gdb_assert (! this_cu->is_debug_types);
3173 /* Nor type unit groups. */
3174 gdb_assert (! this_cu->type_unit_group_p ());
3175
3176 if (this_cu->v.quick->file_names != NULL)
3177 return this_cu->v.quick->file_names;
3178 /* If we know there is no line data, no point in looking again. */
3179 if (this_cu->v.quick->no_file_data)
3180 return NULL;
3181
3182 cutu_reader reader (this_cu);
3183 if (!reader.dummy_p)
3184 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3185
3186 if (this_cu->v.quick->no_file_data)
3187 return NULL;
3188 return this_cu->v.quick->file_names;
3189 }
3190
3191 /* A helper for the "quick" functions which computes and caches the
3192 real path for a given file name from the line table. */
3193
3194 static const char *
3195 dw2_get_real_path (struct objfile *objfile,
3196 struct quick_file_names *qfn, int index)
3197 {
3198 if (qfn->real_names == NULL)
3199 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3200 qfn->num_file_names, const char *);
3201
3202 if (qfn->real_names[index] == NULL)
3203 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3204
3205 return qfn->real_names[index];
3206 }
3207
3208 static struct symtab *
3209 dw2_find_last_source_symtab (struct objfile *objfile)
3210 {
3211 struct dwarf2_per_objfile *dwarf2_per_objfile
3212 = get_dwarf2_per_objfile (objfile);
3213 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3214 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3215
3216 if (cust == NULL)
3217 return NULL;
3218
3219 return compunit_primary_filetab (cust);
3220 }
3221
3222 /* Traversal function for dw2_forget_cached_source_info. */
3223
3224 static int
3225 dw2_free_cached_file_names (void **slot, void *info)
3226 {
3227 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3228
3229 if (file_data->real_names)
3230 {
3231 int i;
3232
3233 for (i = 0; i < file_data->num_file_names; ++i)
3234 {
3235 xfree ((void*) file_data->real_names[i]);
3236 file_data->real_names[i] = NULL;
3237 }
3238 }
3239
3240 return 1;
3241 }
3242
3243 static void
3244 dw2_forget_cached_source_info (struct objfile *objfile)
3245 {
3246 struct dwarf2_per_objfile *dwarf2_per_objfile
3247 = get_dwarf2_per_objfile (objfile);
3248
3249 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3250 dw2_free_cached_file_names, NULL);
3251 }
3252
3253 /* Helper function for dw2_map_symtabs_matching_filename that expands
3254 the symtabs and calls the iterator. */
3255
3256 static int
3257 dw2_map_expand_apply (struct objfile *objfile,
3258 struct dwarf2_per_cu_data *per_cu,
3259 const char *name, const char *real_path,
3260 gdb::function_view<bool (symtab *)> callback)
3261 {
3262 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3263
3264 /* Don't visit already-expanded CUs. */
3265 if (per_cu->v.quick->compunit_symtab)
3266 return 0;
3267
3268 /* This may expand more than one symtab, and we want to iterate over
3269 all of them. */
3270 dw2_instantiate_symtab (per_cu, false);
3271
3272 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3273 last_made, callback);
3274 }
3275
3276 /* Implementation of the map_symtabs_matching_filename method. */
3277
3278 static bool
3279 dw2_map_symtabs_matching_filename
3280 (struct objfile *objfile, const char *name, const char *real_path,
3281 gdb::function_view<bool (symtab *)> callback)
3282 {
3283 const char *name_basename = lbasename (name);
3284 struct dwarf2_per_objfile *dwarf2_per_objfile
3285 = get_dwarf2_per_objfile (objfile);
3286
3287 /* The rule is CUs specify all the files, including those used by
3288 any TU, so there's no need to scan TUs here. */
3289
3290 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3291 {
3292 /* We only need to look at symtabs not already expanded. */
3293 if (per_cu->v.quick->compunit_symtab)
3294 continue;
3295
3296 quick_file_names *file_data = dw2_get_file_names (per_cu);
3297 if (file_data == NULL)
3298 continue;
3299
3300 for (int j = 0; j < file_data->num_file_names; ++j)
3301 {
3302 const char *this_name = file_data->file_names[j];
3303 const char *this_real_name;
3304
3305 if (compare_filenames_for_search (this_name, name))
3306 {
3307 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3308 callback))
3309 return true;
3310 continue;
3311 }
3312
3313 /* Before we invoke realpath, which can get expensive when many
3314 files are involved, do a quick comparison of the basenames. */
3315 if (! basenames_may_differ
3316 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3317 continue;
3318
3319 this_real_name = dw2_get_real_path (objfile, file_data, j);
3320 if (compare_filenames_for_search (this_real_name, name))
3321 {
3322 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3323 callback))
3324 return true;
3325 continue;
3326 }
3327
3328 if (real_path != NULL)
3329 {
3330 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3331 gdb_assert (IS_ABSOLUTE_PATH (name));
3332 if (this_real_name != NULL
3333 && FILENAME_CMP (real_path, this_real_name) == 0)
3334 {
3335 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3336 callback))
3337 return true;
3338 continue;
3339 }
3340 }
3341 }
3342 }
3343
3344 return false;
3345 }
3346
3347 /* Struct used to manage iterating over all CUs looking for a symbol. */
3348
3349 struct dw2_symtab_iterator
3350 {
3351 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3352 struct dwarf2_per_objfile *dwarf2_per_objfile;
3353 /* If set, only look for symbols that match that block. Valid values are
3354 GLOBAL_BLOCK and STATIC_BLOCK. */
3355 gdb::optional<block_enum> block_index;
3356 /* The kind of symbol we're looking for. */
3357 domain_enum domain;
3358 /* The list of CUs from the index entry of the symbol,
3359 or NULL if not found. */
3360 offset_type *vec;
3361 /* The next element in VEC to look at. */
3362 int next;
3363 /* The number of elements in VEC, or zero if there is no match. */
3364 int length;
3365 /* Have we seen a global version of the symbol?
3366 If so we can ignore all further global instances.
3367 This is to work around gold/15646, inefficient gold-generated
3368 indices. */
3369 int global_seen;
3370 };
3371
3372 /* Initialize the index symtab iterator ITER. */
3373
3374 static void
3375 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3376 struct dwarf2_per_objfile *dwarf2_per_objfile,
3377 gdb::optional<block_enum> block_index,
3378 domain_enum domain,
3379 const char *name)
3380 {
3381 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3382 iter->block_index = block_index;
3383 iter->domain = domain;
3384 iter->next = 0;
3385 iter->global_seen = 0;
3386
3387 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3388
3389 /* index is NULL if OBJF_READNOW. */
3390 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3391 iter->length = MAYBE_SWAP (*iter->vec);
3392 else
3393 {
3394 iter->vec = NULL;
3395 iter->length = 0;
3396 }
3397 }
3398
3399 /* Return the next matching CU or NULL if there are no more. */
3400
3401 static struct dwarf2_per_cu_data *
3402 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3403 {
3404 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3405
3406 for ( ; iter->next < iter->length; ++iter->next)
3407 {
3408 offset_type cu_index_and_attrs =
3409 MAYBE_SWAP (iter->vec[iter->next + 1]);
3410 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3411 gdb_index_symbol_kind symbol_kind =
3412 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3413 /* Only check the symbol attributes if they're present.
3414 Indices prior to version 7 don't record them,
3415 and indices >= 7 may elide them for certain symbols
3416 (gold does this). */
3417 int attrs_valid =
3418 (dwarf2_per_objfile->index_table->version >= 7
3419 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3420
3421 /* Don't crash on bad data. */
3422 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3423 + dwarf2_per_objfile->all_type_units.size ()))
3424 {
3425 complaint (_(".gdb_index entry has bad CU index"
3426 " [in module %s]"),
3427 objfile_name (dwarf2_per_objfile->objfile));
3428 continue;
3429 }
3430
3431 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3432
3433 /* Skip if already read in. */
3434 if (per_cu->v.quick->compunit_symtab)
3435 continue;
3436
3437 /* Check static vs global. */
3438 if (attrs_valid)
3439 {
3440 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3441
3442 if (iter->block_index.has_value ())
3443 {
3444 bool want_static = *iter->block_index == STATIC_BLOCK;
3445
3446 if (is_static != want_static)
3447 continue;
3448 }
3449
3450 /* Work around gold/15646. */
3451 if (!is_static && iter->global_seen)
3452 continue;
3453 if (!is_static)
3454 iter->global_seen = 1;
3455 }
3456
3457 /* Only check the symbol's kind if it has one. */
3458 if (attrs_valid)
3459 {
3460 switch (iter->domain)
3461 {
3462 case VAR_DOMAIN:
3463 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3464 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3465 /* Some types are also in VAR_DOMAIN. */
3466 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3467 continue;
3468 break;
3469 case STRUCT_DOMAIN:
3470 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3471 continue;
3472 break;
3473 case LABEL_DOMAIN:
3474 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3475 continue;
3476 break;
3477 case MODULE_DOMAIN:
3478 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3479 continue;
3480 break;
3481 default:
3482 break;
3483 }
3484 }
3485
3486 ++iter->next;
3487 return per_cu;
3488 }
3489
3490 return NULL;
3491 }
3492
3493 static struct compunit_symtab *
3494 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3495 const char *name, domain_enum domain)
3496 {
3497 struct compunit_symtab *stab_best = NULL;
3498 struct dwarf2_per_objfile *dwarf2_per_objfile
3499 = get_dwarf2_per_objfile (objfile);
3500
3501 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3502
3503 struct dw2_symtab_iterator iter;
3504 struct dwarf2_per_cu_data *per_cu;
3505
3506 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3507
3508 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3509 {
3510 struct symbol *sym, *with_opaque = NULL;
3511 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3512 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3513 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3514
3515 sym = block_find_symbol (block, name, domain,
3516 block_find_non_opaque_type_preferred,
3517 &with_opaque);
3518
3519 /* Some caution must be observed with overloaded functions
3520 and methods, since the index will not contain any overload
3521 information (but NAME might contain it). */
3522
3523 if (sym != NULL
3524 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3525 return stab;
3526 if (with_opaque != NULL
3527 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3528 stab_best = stab;
3529
3530 /* Keep looking through other CUs. */
3531 }
3532
3533 return stab_best;
3534 }
3535
3536 static void
3537 dw2_print_stats (struct objfile *objfile)
3538 {
3539 struct dwarf2_per_objfile *dwarf2_per_objfile
3540 = get_dwarf2_per_objfile (objfile);
3541 int total = (dwarf2_per_objfile->all_comp_units.size ()
3542 + dwarf2_per_objfile->all_type_units.size ());
3543 int count = 0;
3544
3545 for (int i = 0; i < total; ++i)
3546 {
3547 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3548
3549 if (!per_cu->v.quick->compunit_symtab)
3550 ++count;
3551 }
3552 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3553 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3554 }
3555
3556 /* This dumps minimal information about the index.
3557 It is called via "mt print objfiles".
3558 One use is to verify .gdb_index has been loaded by the
3559 gdb.dwarf2/gdb-index.exp testcase. */
3560
3561 static void
3562 dw2_dump (struct objfile *objfile)
3563 {
3564 struct dwarf2_per_objfile *dwarf2_per_objfile
3565 = get_dwarf2_per_objfile (objfile);
3566
3567 gdb_assert (dwarf2_per_objfile->using_index);
3568 printf_filtered (".gdb_index:");
3569 if (dwarf2_per_objfile->index_table != NULL)
3570 {
3571 printf_filtered (" version %d\n",
3572 dwarf2_per_objfile->index_table->version);
3573 }
3574 else
3575 printf_filtered (" faked for \"readnow\"\n");
3576 printf_filtered ("\n");
3577 }
3578
3579 static void
3580 dw2_expand_symtabs_for_function (struct objfile *objfile,
3581 const char *func_name)
3582 {
3583 struct dwarf2_per_objfile *dwarf2_per_objfile
3584 = get_dwarf2_per_objfile (objfile);
3585
3586 struct dw2_symtab_iterator iter;
3587 struct dwarf2_per_cu_data *per_cu;
3588
3589 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3590
3591 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3592 dw2_instantiate_symtab (per_cu, false);
3593
3594 }
3595
3596 static void
3597 dw2_expand_all_symtabs (struct objfile *objfile)
3598 {
3599 struct dwarf2_per_objfile *dwarf2_per_objfile
3600 = get_dwarf2_per_objfile (objfile);
3601 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3602 + dwarf2_per_objfile->all_type_units.size ());
3603
3604 for (int i = 0; i < total_units; ++i)
3605 {
3606 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3607
3608 /* We don't want to directly expand a partial CU, because if we
3609 read it with the wrong language, then assertion failures can
3610 be triggered later on. See PR symtab/23010. So, tell
3611 dw2_instantiate_symtab to skip partial CUs -- any important
3612 partial CU will be read via DW_TAG_imported_unit anyway. */
3613 dw2_instantiate_symtab (per_cu, true);
3614 }
3615 }
3616
3617 static void
3618 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3619 const char *fullname)
3620 {
3621 struct dwarf2_per_objfile *dwarf2_per_objfile
3622 = get_dwarf2_per_objfile (objfile);
3623
3624 /* We don't need to consider type units here.
3625 This is only called for examining code, e.g. expand_line_sal.
3626 There can be an order of magnitude (or more) more type units
3627 than comp units, and we avoid them if we can. */
3628
3629 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3630 {
3631 /* We only need to look at symtabs not already expanded. */
3632 if (per_cu->v.quick->compunit_symtab)
3633 continue;
3634
3635 quick_file_names *file_data = dw2_get_file_names (per_cu);
3636 if (file_data == NULL)
3637 continue;
3638
3639 for (int j = 0; j < file_data->num_file_names; ++j)
3640 {
3641 const char *this_fullname = file_data->file_names[j];
3642
3643 if (filename_cmp (this_fullname, fullname) == 0)
3644 {
3645 dw2_instantiate_symtab (per_cu, false);
3646 break;
3647 }
3648 }
3649 }
3650 }
3651
3652 static void
3653 dw2_map_matching_symbols
3654 (struct objfile *objfile,
3655 const lookup_name_info &name, domain_enum domain,
3656 int global,
3657 gdb::function_view<symbol_found_callback_ftype> callback,
3658 symbol_compare_ftype *ordered_compare)
3659 {
3660 /* Used for Ada. */
3661 struct dwarf2_per_objfile *dwarf2_per_objfile
3662 = get_dwarf2_per_objfile (objfile);
3663
3664 if (dwarf2_per_objfile->index_table != nullptr)
3665 {
3666 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3667 here though if the current language is Ada for a non-Ada objfile
3668 using GNU index. As Ada does not look for non-Ada symbols this
3669 function should just return. */
3670 return;
3671 }
3672
3673 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3674 inline psym_map_matching_symbols here, assuming all partial symtabs have
3675 been read in. */
3676 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3677
3678 for (compunit_symtab *cust : objfile->compunits ())
3679 {
3680 const struct block *block;
3681
3682 if (cust == NULL)
3683 continue;
3684 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3685 if (!iterate_over_symbols_terminated (block, name,
3686 domain, callback))
3687 return;
3688 }
3689 }
3690
3691 /* Starting from a search name, return the string that finds the upper
3692 bound of all strings that start with SEARCH_NAME in a sorted name
3693 list. Returns the empty string to indicate that the upper bound is
3694 the end of the list. */
3695
3696 static std::string
3697 make_sort_after_prefix_name (const char *search_name)
3698 {
3699 /* When looking to complete "func", we find the upper bound of all
3700 symbols that start with "func" by looking for where we'd insert
3701 the closest string that would follow "func" in lexicographical
3702 order. Usually, that's "func"-with-last-character-incremented,
3703 i.e. "fund". Mind non-ASCII characters, though. Usually those
3704 will be UTF-8 multi-byte sequences, but we can't be certain.
3705 Especially mind the 0xff character, which is a valid character in
3706 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3707 rule out compilers allowing it in identifiers. Note that
3708 conveniently, strcmp/strcasecmp are specified to compare
3709 characters interpreted as unsigned char. So what we do is treat
3710 the whole string as a base 256 number composed of a sequence of
3711 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3712 to 0, and carries 1 to the following more-significant position.
3713 If the very first character in SEARCH_NAME ends up incremented
3714 and carries/overflows, then the upper bound is the end of the
3715 list. The string after the empty string is also the empty
3716 string.
3717
3718 Some examples of this operation:
3719
3720 SEARCH_NAME => "+1" RESULT
3721
3722 "abc" => "abd"
3723 "ab\xff" => "ac"
3724 "\xff" "a" "\xff" => "\xff" "b"
3725 "\xff" => ""
3726 "\xff\xff" => ""
3727 "" => ""
3728
3729 Then, with these symbols for example:
3730
3731 func
3732 func1
3733 fund
3734
3735 completing "func" looks for symbols between "func" and
3736 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3737 which finds "func" and "func1", but not "fund".
3738
3739 And with:
3740
3741 funcÿ (Latin1 'ÿ' [0xff])
3742 funcÿ1
3743 fund
3744
3745 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3746 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3747
3748 And with:
3749
3750 ÿÿ (Latin1 'ÿ' [0xff])
3751 ÿÿ1
3752
3753 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3754 the end of the list.
3755 */
3756 std::string after = search_name;
3757 while (!after.empty () && (unsigned char) after.back () == 0xff)
3758 after.pop_back ();
3759 if (!after.empty ())
3760 after.back () = (unsigned char) after.back () + 1;
3761 return after;
3762 }
3763
3764 /* See declaration. */
3765
3766 std::pair<std::vector<name_component>::const_iterator,
3767 std::vector<name_component>::const_iterator>
3768 mapped_index_base::find_name_components_bounds
3769 (const lookup_name_info &lookup_name_without_params, language lang) const
3770 {
3771 auto *name_cmp
3772 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3773
3774 const char *lang_name
3775 = lookup_name_without_params.language_lookup_name (lang);
3776
3777 /* Comparison function object for lower_bound that matches against a
3778 given symbol name. */
3779 auto lookup_compare_lower = [&] (const name_component &elem,
3780 const char *name)
3781 {
3782 const char *elem_qualified = this->symbol_name_at (elem.idx);
3783 const char *elem_name = elem_qualified + elem.name_offset;
3784 return name_cmp (elem_name, name) < 0;
3785 };
3786
3787 /* Comparison function object for upper_bound that matches against a
3788 given symbol name. */
3789 auto lookup_compare_upper = [&] (const char *name,
3790 const name_component &elem)
3791 {
3792 const char *elem_qualified = this->symbol_name_at (elem.idx);
3793 const char *elem_name = elem_qualified + elem.name_offset;
3794 return name_cmp (name, elem_name) < 0;
3795 };
3796
3797 auto begin = this->name_components.begin ();
3798 auto end = this->name_components.end ();
3799
3800 /* Find the lower bound. */
3801 auto lower = [&] ()
3802 {
3803 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3804 return begin;
3805 else
3806 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3807 } ();
3808
3809 /* Find the upper bound. */
3810 auto upper = [&] ()
3811 {
3812 if (lookup_name_without_params.completion_mode ())
3813 {
3814 /* In completion mode, we want UPPER to point past all
3815 symbols names that have the same prefix. I.e., with
3816 these symbols, and completing "func":
3817
3818 function << lower bound
3819 function1
3820 other_function << upper bound
3821
3822 We find the upper bound by looking for the insertion
3823 point of "func"-with-last-character-incremented,
3824 i.e. "fund". */
3825 std::string after = make_sort_after_prefix_name (lang_name);
3826 if (after.empty ())
3827 return end;
3828 return std::lower_bound (lower, end, after.c_str (),
3829 lookup_compare_lower);
3830 }
3831 else
3832 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3833 } ();
3834
3835 return {lower, upper};
3836 }
3837
3838 /* See declaration. */
3839
3840 void
3841 mapped_index_base::build_name_components ()
3842 {
3843 if (!this->name_components.empty ())
3844 return;
3845
3846 this->name_components_casing = case_sensitivity;
3847 auto *name_cmp
3848 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3849
3850 /* The code below only knows how to break apart components of C++
3851 symbol names (and other languages that use '::' as
3852 namespace/module separator) and Ada symbol names. */
3853 auto count = this->symbol_name_count ();
3854 for (offset_type idx = 0; idx < count; idx++)
3855 {
3856 if (this->symbol_name_slot_invalid (idx))
3857 continue;
3858
3859 const char *name = this->symbol_name_at (idx);
3860
3861 /* Add each name component to the name component table. */
3862 unsigned int previous_len = 0;
3863
3864 if (strstr (name, "::") != nullptr)
3865 {
3866 for (unsigned int current_len = cp_find_first_component (name);
3867 name[current_len] != '\0';
3868 current_len += cp_find_first_component (name + current_len))
3869 {
3870 gdb_assert (name[current_len] == ':');
3871 this->name_components.push_back ({previous_len, idx});
3872 /* Skip the '::'. */
3873 current_len += 2;
3874 previous_len = current_len;
3875 }
3876 }
3877 else
3878 {
3879 /* Handle the Ada encoded (aka mangled) form here. */
3880 for (const char *iter = strstr (name, "__");
3881 iter != nullptr;
3882 iter = strstr (iter, "__"))
3883 {
3884 this->name_components.push_back ({previous_len, idx});
3885 iter += 2;
3886 previous_len = iter - name;
3887 }
3888 }
3889
3890 this->name_components.push_back ({previous_len, idx});
3891 }
3892
3893 /* Sort name_components elements by name. */
3894 auto name_comp_compare = [&] (const name_component &left,
3895 const name_component &right)
3896 {
3897 const char *left_qualified = this->symbol_name_at (left.idx);
3898 const char *right_qualified = this->symbol_name_at (right.idx);
3899
3900 const char *left_name = left_qualified + left.name_offset;
3901 const char *right_name = right_qualified + right.name_offset;
3902
3903 return name_cmp (left_name, right_name) < 0;
3904 };
3905
3906 std::sort (this->name_components.begin (),
3907 this->name_components.end (),
3908 name_comp_compare);
3909 }
3910
3911 /* Helper for dw2_expand_symtabs_matching that works with a
3912 mapped_index_base instead of the containing objfile. This is split
3913 to a separate function in order to be able to unit test the
3914 name_components matching using a mock mapped_index_base. For each
3915 symbol name that matches, calls MATCH_CALLBACK, passing it the
3916 symbol's index in the mapped_index_base symbol table. */
3917
3918 static void
3919 dw2_expand_symtabs_matching_symbol
3920 (mapped_index_base &index,
3921 const lookup_name_info &lookup_name_in,
3922 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3923 enum search_domain kind,
3924 gdb::function_view<bool (offset_type)> match_callback)
3925 {
3926 lookup_name_info lookup_name_without_params
3927 = lookup_name_in.make_ignore_params ();
3928
3929 /* Build the symbol name component sorted vector, if we haven't
3930 yet. */
3931 index.build_name_components ();
3932
3933 /* The same symbol may appear more than once in the range though.
3934 E.g., if we're looking for symbols that complete "w", and we have
3935 a symbol named "w1::w2", we'll find the two name components for
3936 that same symbol in the range. To be sure we only call the
3937 callback once per symbol, we first collect the symbol name
3938 indexes that matched in a temporary vector and ignore
3939 duplicates. */
3940 std::vector<offset_type> matches;
3941
3942 struct name_and_matcher
3943 {
3944 symbol_name_matcher_ftype *matcher;
3945 const char *name;
3946
3947 bool operator== (const name_and_matcher &other) const
3948 {
3949 return matcher == other.matcher && strcmp (name, other.name) == 0;
3950 }
3951 };
3952
3953 /* A vector holding all the different symbol name matchers, for all
3954 languages. */
3955 std::vector<name_and_matcher> matchers;
3956
3957 for (int i = 0; i < nr_languages; i++)
3958 {
3959 enum language lang_e = (enum language) i;
3960
3961 const language_defn *lang = language_def (lang_e);
3962 symbol_name_matcher_ftype *name_matcher
3963 = get_symbol_name_matcher (lang, lookup_name_without_params);
3964
3965 name_and_matcher key {
3966 name_matcher,
3967 lookup_name_without_params.language_lookup_name (lang_e)
3968 };
3969
3970 /* Don't insert the same comparison routine more than once.
3971 Note that we do this linear walk. This is not a problem in
3972 practice because the number of supported languages is
3973 low. */
3974 if (std::find (matchers.begin (), matchers.end (), key)
3975 != matchers.end ())
3976 continue;
3977 matchers.push_back (std::move (key));
3978
3979 auto bounds
3980 = index.find_name_components_bounds (lookup_name_without_params,
3981 lang_e);
3982
3983 /* Now for each symbol name in range, check to see if we have a name
3984 match, and if so, call the MATCH_CALLBACK callback. */
3985
3986 for (; bounds.first != bounds.second; ++bounds.first)
3987 {
3988 const char *qualified = index.symbol_name_at (bounds.first->idx);
3989
3990 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3991 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3992 continue;
3993
3994 matches.push_back (bounds.first->idx);
3995 }
3996 }
3997
3998 std::sort (matches.begin (), matches.end ());
3999
4000 /* Finally call the callback, once per match. */
4001 ULONGEST prev = -1;
4002 for (offset_type idx : matches)
4003 {
4004 if (prev != idx)
4005 {
4006 if (!match_callback (idx))
4007 break;
4008 prev = idx;
4009 }
4010 }
4011
4012 /* Above we use a type wider than idx's for 'prev', since 0 and
4013 (offset_type)-1 are both possible values. */
4014 static_assert (sizeof (prev) > sizeof (offset_type), "");
4015 }
4016
4017 #if GDB_SELF_TEST
4018
4019 namespace selftests { namespace dw2_expand_symtabs_matching {
4020
4021 /* A mock .gdb_index/.debug_names-like name index table, enough to
4022 exercise dw2_expand_symtabs_matching_symbol, which works with the
4023 mapped_index_base interface. Builds an index from the symbol list
4024 passed as parameter to the constructor. */
4025 class mock_mapped_index : public mapped_index_base
4026 {
4027 public:
4028 mock_mapped_index (gdb::array_view<const char *> symbols)
4029 : m_symbol_table (symbols)
4030 {}
4031
4032 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4033
4034 /* Return the number of names in the symbol table. */
4035 size_t symbol_name_count () const override
4036 {
4037 return m_symbol_table.size ();
4038 }
4039
4040 /* Get the name of the symbol at IDX in the symbol table. */
4041 const char *symbol_name_at (offset_type idx) const override
4042 {
4043 return m_symbol_table[idx];
4044 }
4045
4046 private:
4047 gdb::array_view<const char *> m_symbol_table;
4048 };
4049
4050 /* Convenience function that converts a NULL pointer to a "<null>"
4051 string, to pass to print routines. */
4052
4053 static const char *
4054 string_or_null (const char *str)
4055 {
4056 return str != NULL ? str : "<null>";
4057 }
4058
4059 /* Check if a lookup_name_info built from
4060 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4061 index. EXPECTED_LIST is the list of expected matches, in expected
4062 matching order. If no match expected, then an empty list is
4063 specified. Returns true on success. On failure prints a warning
4064 indicating the file:line that failed, and returns false. */
4065
4066 static bool
4067 check_match (const char *file, int line,
4068 mock_mapped_index &mock_index,
4069 const char *name, symbol_name_match_type match_type,
4070 bool completion_mode,
4071 std::initializer_list<const char *> expected_list)
4072 {
4073 lookup_name_info lookup_name (name, match_type, completion_mode);
4074
4075 bool matched = true;
4076
4077 auto mismatch = [&] (const char *expected_str,
4078 const char *got)
4079 {
4080 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4081 "expected=\"%s\", got=\"%s\"\n"),
4082 file, line,
4083 (match_type == symbol_name_match_type::FULL
4084 ? "FULL" : "WILD"),
4085 name, string_or_null (expected_str), string_or_null (got));
4086 matched = false;
4087 };
4088
4089 auto expected_it = expected_list.begin ();
4090 auto expected_end = expected_list.end ();
4091
4092 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4093 NULL, ALL_DOMAIN,
4094 [&] (offset_type idx)
4095 {
4096 const char *matched_name = mock_index.symbol_name_at (idx);
4097 const char *expected_str
4098 = expected_it == expected_end ? NULL : *expected_it++;
4099
4100 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4101 mismatch (expected_str, matched_name);
4102 return true;
4103 });
4104
4105 const char *expected_str
4106 = expected_it == expected_end ? NULL : *expected_it++;
4107 if (expected_str != NULL)
4108 mismatch (expected_str, NULL);
4109
4110 return matched;
4111 }
4112
4113 /* The symbols added to the mock mapped_index for testing (in
4114 canonical form). */
4115 static const char *test_symbols[] = {
4116 "function",
4117 "std::bar",
4118 "std::zfunction",
4119 "std::zfunction2",
4120 "w1::w2",
4121 "ns::foo<char*>",
4122 "ns::foo<int>",
4123 "ns::foo<long>",
4124 "ns2::tmpl<int>::foo2",
4125 "(anonymous namespace)::A::B::C",
4126
4127 /* These are used to check that the increment-last-char in the
4128 matching algorithm for completion doesn't match "t1_fund" when
4129 completing "t1_func". */
4130 "t1_func",
4131 "t1_func1",
4132 "t1_fund",
4133 "t1_fund1",
4134
4135 /* A UTF-8 name with multi-byte sequences to make sure that
4136 cp-name-parser understands this as a single identifier ("função"
4137 is "function" in PT). */
4138 u8"u8função",
4139
4140 /* \377 (0xff) is Latin1 'ÿ'. */
4141 "yfunc\377",
4142
4143 /* \377 (0xff) is Latin1 'ÿ'. */
4144 "\377",
4145 "\377\377123",
4146
4147 /* A name with all sorts of complications. Starts with "z" to make
4148 it easier for the completion tests below. */
4149 #define Z_SYM_NAME \
4150 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4151 "::tuple<(anonymous namespace)::ui*, " \
4152 "std::default_delete<(anonymous namespace)::ui>, void>"
4153
4154 Z_SYM_NAME
4155 };
4156
4157 /* Returns true if the mapped_index_base::find_name_component_bounds
4158 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4159 in completion mode. */
4160
4161 static bool
4162 check_find_bounds_finds (mapped_index_base &index,
4163 const char *search_name,
4164 gdb::array_view<const char *> expected_syms)
4165 {
4166 lookup_name_info lookup_name (search_name,
4167 symbol_name_match_type::FULL, true);
4168
4169 auto bounds = index.find_name_components_bounds (lookup_name,
4170 language_cplus);
4171
4172 size_t distance = std::distance (bounds.first, bounds.second);
4173 if (distance != expected_syms.size ())
4174 return false;
4175
4176 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4177 {
4178 auto nc_elem = bounds.first + exp_elem;
4179 const char *qualified = index.symbol_name_at (nc_elem->idx);
4180 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4181 return false;
4182 }
4183
4184 return true;
4185 }
4186
4187 /* Test the lower-level mapped_index::find_name_component_bounds
4188 method. */
4189
4190 static void
4191 test_mapped_index_find_name_component_bounds ()
4192 {
4193 mock_mapped_index mock_index (test_symbols);
4194
4195 mock_index.build_name_components ();
4196
4197 /* Test the lower-level mapped_index::find_name_component_bounds
4198 method in completion mode. */
4199 {
4200 static const char *expected_syms[] = {
4201 "t1_func",
4202 "t1_func1",
4203 };
4204
4205 SELF_CHECK (check_find_bounds_finds (mock_index,
4206 "t1_func", expected_syms));
4207 }
4208
4209 /* Check that the increment-last-char in the name matching algorithm
4210 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4211 {
4212 static const char *expected_syms1[] = {
4213 "\377",
4214 "\377\377123",
4215 };
4216 SELF_CHECK (check_find_bounds_finds (mock_index,
4217 "\377", expected_syms1));
4218
4219 static const char *expected_syms2[] = {
4220 "\377\377123",
4221 };
4222 SELF_CHECK (check_find_bounds_finds (mock_index,
4223 "\377\377", expected_syms2));
4224 }
4225 }
4226
4227 /* Test dw2_expand_symtabs_matching_symbol. */
4228
4229 static void
4230 test_dw2_expand_symtabs_matching_symbol ()
4231 {
4232 mock_mapped_index mock_index (test_symbols);
4233
4234 /* We let all tests run until the end even if some fails, for debug
4235 convenience. */
4236 bool any_mismatch = false;
4237
4238 /* Create the expected symbols list (an initializer_list). Needed
4239 because lists have commas, and we need to pass them to CHECK,
4240 which is a macro. */
4241 #define EXPECT(...) { __VA_ARGS__ }
4242
4243 /* Wrapper for check_match that passes down the current
4244 __FILE__/__LINE__. */
4245 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4246 any_mismatch |= !check_match (__FILE__, __LINE__, \
4247 mock_index, \
4248 NAME, MATCH_TYPE, COMPLETION_MODE, \
4249 EXPECTED_LIST)
4250
4251 /* Identity checks. */
4252 for (const char *sym : test_symbols)
4253 {
4254 /* Should be able to match all existing symbols. */
4255 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4256 EXPECT (sym));
4257
4258 /* Should be able to match all existing symbols with
4259 parameters. */
4260 std::string with_params = std::string (sym) + "(int)";
4261 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4262 EXPECT (sym));
4263
4264 /* Should be able to match all existing symbols with
4265 parameters and qualifiers. */
4266 with_params = std::string (sym) + " ( int ) const";
4267 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4268 EXPECT (sym));
4269
4270 /* This should really find sym, but cp-name-parser.y doesn't
4271 know about lvalue/rvalue qualifiers yet. */
4272 with_params = std::string (sym) + " ( int ) &&";
4273 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4274 {});
4275 }
4276
4277 /* Check that the name matching algorithm for completion doesn't get
4278 confused with Latin1 'ÿ' / 0xff. */
4279 {
4280 static const char str[] = "\377";
4281 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4282 EXPECT ("\377", "\377\377123"));
4283 }
4284
4285 /* Check that the increment-last-char in the matching algorithm for
4286 completion doesn't match "t1_fund" when completing "t1_func". */
4287 {
4288 static const char str[] = "t1_func";
4289 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4290 EXPECT ("t1_func", "t1_func1"));
4291 }
4292
4293 /* Check that completion mode works at each prefix of the expected
4294 symbol name. */
4295 {
4296 static const char str[] = "function(int)";
4297 size_t len = strlen (str);
4298 std::string lookup;
4299
4300 for (size_t i = 1; i < len; i++)
4301 {
4302 lookup.assign (str, i);
4303 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4304 EXPECT ("function"));
4305 }
4306 }
4307
4308 /* While "w" is a prefix of both components, the match function
4309 should still only be called once. */
4310 {
4311 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4312 EXPECT ("w1::w2"));
4313 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4314 EXPECT ("w1::w2"));
4315 }
4316
4317 /* Same, with a "complicated" symbol. */
4318 {
4319 static const char str[] = Z_SYM_NAME;
4320 size_t len = strlen (str);
4321 std::string lookup;
4322
4323 for (size_t i = 1; i < len; i++)
4324 {
4325 lookup.assign (str, i);
4326 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4327 EXPECT (Z_SYM_NAME));
4328 }
4329 }
4330
4331 /* In FULL mode, an incomplete symbol doesn't match. */
4332 {
4333 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4334 {});
4335 }
4336
4337 /* A complete symbol with parameters matches any overload, since the
4338 index has no overload info. */
4339 {
4340 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4341 EXPECT ("std::zfunction", "std::zfunction2"));
4342 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4343 EXPECT ("std::zfunction", "std::zfunction2"));
4344 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4345 EXPECT ("std::zfunction", "std::zfunction2"));
4346 }
4347
4348 /* Check that whitespace is ignored appropriately. A symbol with a
4349 template argument list. */
4350 {
4351 static const char expected[] = "ns::foo<int>";
4352 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4353 EXPECT (expected));
4354 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4355 EXPECT (expected));
4356 }
4357
4358 /* Check that whitespace is ignored appropriately. A symbol with a
4359 template argument list that includes a pointer. */
4360 {
4361 static const char expected[] = "ns::foo<char*>";
4362 /* Try both completion and non-completion modes. */
4363 static const bool completion_mode[2] = {false, true};
4364 for (size_t i = 0; i < 2; i++)
4365 {
4366 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4367 completion_mode[i], EXPECT (expected));
4368 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4369 completion_mode[i], EXPECT (expected));
4370
4371 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4372 completion_mode[i], EXPECT (expected));
4373 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4374 completion_mode[i], EXPECT (expected));
4375 }
4376 }
4377
4378 {
4379 /* Check method qualifiers are ignored. */
4380 static const char expected[] = "ns::foo<char*>";
4381 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4382 symbol_name_match_type::FULL, true, EXPECT (expected));
4383 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4384 symbol_name_match_type::FULL, true, EXPECT (expected));
4385 CHECK_MATCH ("foo < char * > ( int ) const",
4386 symbol_name_match_type::WILD, true, EXPECT (expected));
4387 CHECK_MATCH ("foo < char * > ( int ) &&",
4388 symbol_name_match_type::WILD, true, EXPECT (expected));
4389 }
4390
4391 /* Test lookup names that don't match anything. */
4392 {
4393 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4394 {});
4395
4396 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4397 {});
4398 }
4399
4400 /* Some wild matching tests, exercising "(anonymous namespace)",
4401 which should not be confused with a parameter list. */
4402 {
4403 static const char *syms[] = {
4404 "A::B::C",
4405 "B::C",
4406 "C",
4407 "A :: B :: C ( int )",
4408 "B :: C ( int )",
4409 "C ( int )",
4410 };
4411
4412 for (const char *s : syms)
4413 {
4414 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4415 EXPECT ("(anonymous namespace)::A::B::C"));
4416 }
4417 }
4418
4419 {
4420 static const char expected[] = "ns2::tmpl<int>::foo2";
4421 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4422 EXPECT (expected));
4423 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4424 EXPECT (expected));
4425 }
4426
4427 SELF_CHECK (!any_mismatch);
4428
4429 #undef EXPECT
4430 #undef CHECK_MATCH
4431 }
4432
4433 static void
4434 run_test ()
4435 {
4436 test_mapped_index_find_name_component_bounds ();
4437 test_dw2_expand_symtabs_matching_symbol ();
4438 }
4439
4440 }} // namespace selftests::dw2_expand_symtabs_matching
4441
4442 #endif /* GDB_SELF_TEST */
4443
4444 /* If FILE_MATCHER is NULL or if PER_CU has
4445 dwarf2_per_cu_quick_data::MARK set (see
4446 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4447 EXPANSION_NOTIFY on it. */
4448
4449 static void
4450 dw2_expand_symtabs_matching_one
4451 (struct dwarf2_per_cu_data *per_cu,
4452 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4453 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4454 {
4455 if (file_matcher == NULL || per_cu->v.quick->mark)
4456 {
4457 bool symtab_was_null
4458 = (per_cu->v.quick->compunit_symtab == NULL);
4459
4460 dw2_instantiate_symtab (per_cu, false);
4461
4462 if (expansion_notify != NULL
4463 && symtab_was_null
4464 && per_cu->v.quick->compunit_symtab != NULL)
4465 expansion_notify (per_cu->v.quick->compunit_symtab);
4466 }
4467 }
4468
4469 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4470 matched, to expand corresponding CUs that were marked. IDX is the
4471 index of the symbol name that matched. */
4472
4473 static void
4474 dw2_expand_marked_cus
4475 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4476 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4477 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4478 search_domain kind)
4479 {
4480 offset_type *vec, vec_len, vec_idx;
4481 bool global_seen = false;
4482 mapped_index &index = *dwarf2_per_objfile->index_table;
4483
4484 vec = (offset_type *) (index.constant_pool
4485 + MAYBE_SWAP (index.symbol_table[idx].vec));
4486 vec_len = MAYBE_SWAP (vec[0]);
4487 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4488 {
4489 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4490 /* This value is only valid for index versions >= 7. */
4491 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4492 gdb_index_symbol_kind symbol_kind =
4493 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4494 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4495 /* Only check the symbol attributes if they're present.
4496 Indices prior to version 7 don't record them,
4497 and indices >= 7 may elide them for certain symbols
4498 (gold does this). */
4499 int attrs_valid =
4500 (index.version >= 7
4501 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4502
4503 /* Work around gold/15646. */
4504 if (attrs_valid)
4505 {
4506 if (!is_static && global_seen)
4507 continue;
4508 if (!is_static)
4509 global_seen = true;
4510 }
4511
4512 /* Only check the symbol's kind if it has one. */
4513 if (attrs_valid)
4514 {
4515 switch (kind)
4516 {
4517 case VARIABLES_DOMAIN:
4518 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4519 continue;
4520 break;
4521 case FUNCTIONS_DOMAIN:
4522 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4523 continue;
4524 break;
4525 case TYPES_DOMAIN:
4526 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4527 continue;
4528 break;
4529 case MODULES_DOMAIN:
4530 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4531 continue;
4532 break;
4533 default:
4534 break;
4535 }
4536 }
4537
4538 /* Don't crash on bad data. */
4539 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4540 + dwarf2_per_objfile->all_type_units.size ()))
4541 {
4542 complaint (_(".gdb_index entry has bad CU index"
4543 " [in module %s]"),
4544 objfile_name (dwarf2_per_objfile->objfile));
4545 continue;
4546 }
4547
4548 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4549 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4550 expansion_notify);
4551 }
4552 }
4553
4554 /* If FILE_MATCHER is non-NULL, set all the
4555 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4556 that match FILE_MATCHER. */
4557
4558 static void
4559 dw_expand_symtabs_matching_file_matcher
4560 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4561 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4562 {
4563 if (file_matcher == NULL)
4564 return;
4565
4566 objfile *const objfile = dwarf2_per_objfile->objfile;
4567
4568 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4569 htab_eq_pointer,
4570 NULL, xcalloc, xfree));
4571 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4572 htab_eq_pointer,
4573 NULL, xcalloc, xfree));
4574
4575 /* The rule is CUs specify all the files, including those used by
4576 any TU, so there's no need to scan TUs here. */
4577
4578 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4579 {
4580 QUIT;
4581
4582 per_cu->v.quick->mark = 0;
4583
4584 /* We only need to look at symtabs not already expanded. */
4585 if (per_cu->v.quick->compunit_symtab)
4586 continue;
4587
4588 quick_file_names *file_data = dw2_get_file_names (per_cu);
4589 if (file_data == NULL)
4590 continue;
4591
4592 if (htab_find (visited_not_found.get (), file_data) != NULL)
4593 continue;
4594 else if (htab_find (visited_found.get (), file_data) != NULL)
4595 {
4596 per_cu->v.quick->mark = 1;
4597 continue;
4598 }
4599
4600 for (int j = 0; j < file_data->num_file_names; ++j)
4601 {
4602 const char *this_real_name;
4603
4604 if (file_matcher (file_data->file_names[j], false))
4605 {
4606 per_cu->v.quick->mark = 1;
4607 break;
4608 }
4609
4610 /* Before we invoke realpath, which can get expensive when many
4611 files are involved, do a quick comparison of the basenames. */
4612 if (!basenames_may_differ
4613 && !file_matcher (lbasename (file_data->file_names[j]),
4614 true))
4615 continue;
4616
4617 this_real_name = dw2_get_real_path (objfile, file_data, j);
4618 if (file_matcher (this_real_name, false))
4619 {
4620 per_cu->v.quick->mark = 1;
4621 break;
4622 }
4623 }
4624
4625 void **slot = htab_find_slot (per_cu->v.quick->mark
4626 ? visited_found.get ()
4627 : visited_not_found.get (),
4628 file_data, INSERT);
4629 *slot = file_data;
4630 }
4631 }
4632
4633 static void
4634 dw2_expand_symtabs_matching
4635 (struct objfile *objfile,
4636 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4637 const lookup_name_info *lookup_name,
4638 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4639 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4640 enum search_domain kind)
4641 {
4642 struct dwarf2_per_objfile *dwarf2_per_objfile
4643 = get_dwarf2_per_objfile (objfile);
4644
4645 /* index_table is NULL if OBJF_READNOW. */
4646 if (!dwarf2_per_objfile->index_table)
4647 return;
4648
4649 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4650
4651 if (symbol_matcher == NULL && lookup_name == NULL)
4652 {
4653 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4654 {
4655 QUIT;
4656
4657 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4658 expansion_notify);
4659 }
4660 return;
4661 }
4662
4663 mapped_index &index = *dwarf2_per_objfile->index_table;
4664
4665 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4666 symbol_matcher,
4667 kind, [&] (offset_type idx)
4668 {
4669 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4670 expansion_notify, kind);
4671 return true;
4672 });
4673 }
4674
4675 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4676 symtab. */
4677
4678 static struct compunit_symtab *
4679 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4680 CORE_ADDR pc)
4681 {
4682 int i;
4683
4684 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4685 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4686 return cust;
4687
4688 if (cust->includes == NULL)
4689 return NULL;
4690
4691 for (i = 0; cust->includes[i]; ++i)
4692 {
4693 struct compunit_symtab *s = cust->includes[i];
4694
4695 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4696 if (s != NULL)
4697 return s;
4698 }
4699
4700 return NULL;
4701 }
4702
4703 static struct compunit_symtab *
4704 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4705 struct bound_minimal_symbol msymbol,
4706 CORE_ADDR pc,
4707 struct obj_section *section,
4708 int warn_if_readin)
4709 {
4710 struct dwarf2_per_cu_data *data;
4711 struct compunit_symtab *result;
4712
4713 if (!objfile->partial_symtabs->psymtabs_addrmap)
4714 return NULL;
4715
4716 CORE_ADDR baseaddr = objfile->text_section_offset ();
4717 data = (struct dwarf2_per_cu_data *) addrmap_find
4718 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4719 if (!data)
4720 return NULL;
4721
4722 if (warn_if_readin && data->v.quick->compunit_symtab)
4723 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4724 paddress (objfile->arch (), pc));
4725
4726 result
4727 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4728 false),
4729 pc);
4730 gdb_assert (result != NULL);
4731 return result;
4732 }
4733
4734 static void
4735 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4736 void *data, int need_fullname)
4737 {
4738 struct dwarf2_per_objfile *dwarf2_per_objfile
4739 = get_dwarf2_per_objfile (objfile);
4740
4741 if (!dwarf2_per_objfile->filenames_cache)
4742 {
4743 dwarf2_per_objfile->filenames_cache.emplace ();
4744
4745 htab_up visited (htab_create_alloc (10,
4746 htab_hash_pointer, htab_eq_pointer,
4747 NULL, xcalloc, xfree));
4748
4749 /* The rule is CUs specify all the files, including those used
4750 by any TU, so there's no need to scan TUs here. We can
4751 ignore file names coming from already-expanded CUs. */
4752
4753 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4754 {
4755 if (per_cu->v.quick->compunit_symtab)
4756 {
4757 void **slot = htab_find_slot (visited.get (),
4758 per_cu->v.quick->file_names,
4759 INSERT);
4760
4761 *slot = per_cu->v.quick->file_names;
4762 }
4763 }
4764
4765 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4766 {
4767 /* We only need to look at symtabs not already expanded. */
4768 if (per_cu->v.quick->compunit_symtab)
4769 continue;
4770
4771 quick_file_names *file_data = dw2_get_file_names (per_cu);
4772 if (file_data == NULL)
4773 continue;
4774
4775 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4776 if (*slot)
4777 {
4778 /* Already visited. */
4779 continue;
4780 }
4781 *slot = file_data;
4782
4783 for (int j = 0; j < file_data->num_file_names; ++j)
4784 {
4785 const char *filename = file_data->file_names[j];
4786 dwarf2_per_objfile->filenames_cache->seen (filename);
4787 }
4788 }
4789 }
4790
4791 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4792 {
4793 gdb::unique_xmalloc_ptr<char> this_real_name;
4794
4795 if (need_fullname)
4796 this_real_name = gdb_realpath (filename);
4797 (*fun) (filename, this_real_name.get (), data);
4798 });
4799 }
4800
4801 static int
4802 dw2_has_symbols (struct objfile *objfile)
4803 {
4804 return 1;
4805 }
4806
4807 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4808 {
4809 dw2_has_symbols,
4810 dw2_find_last_source_symtab,
4811 dw2_forget_cached_source_info,
4812 dw2_map_symtabs_matching_filename,
4813 dw2_lookup_symbol,
4814 NULL,
4815 dw2_print_stats,
4816 dw2_dump,
4817 dw2_expand_symtabs_for_function,
4818 dw2_expand_all_symtabs,
4819 dw2_expand_symtabs_with_fullname,
4820 dw2_map_matching_symbols,
4821 dw2_expand_symtabs_matching,
4822 dw2_find_pc_sect_compunit_symtab,
4823 NULL,
4824 dw2_map_symbol_filenames
4825 };
4826
4827 /* DWARF-5 debug_names reader. */
4828
4829 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4830 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4831
4832 /* A helper function that reads the .debug_names section in SECTION
4833 and fills in MAP. FILENAME is the name of the file containing the
4834 section; it is used for error reporting.
4835
4836 Returns true if all went well, false otherwise. */
4837
4838 static bool
4839 read_debug_names_from_section (struct objfile *objfile,
4840 const char *filename,
4841 struct dwarf2_section_info *section,
4842 mapped_debug_names &map)
4843 {
4844 if (section->empty ())
4845 return false;
4846
4847 /* Older elfutils strip versions could keep the section in the main
4848 executable while splitting it for the separate debug info file. */
4849 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4850 return false;
4851
4852 section->read (objfile);
4853
4854 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4855
4856 const gdb_byte *addr = section->buffer;
4857
4858 bfd *const abfd = section->get_bfd_owner ();
4859
4860 unsigned int bytes_read;
4861 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4862 addr += bytes_read;
4863
4864 map.dwarf5_is_dwarf64 = bytes_read != 4;
4865 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4866 if (bytes_read + length != section->size)
4867 {
4868 /* There may be multiple per-CU indices. */
4869 warning (_("Section .debug_names in %s length %s does not match "
4870 "section length %s, ignoring .debug_names."),
4871 filename, plongest (bytes_read + length),
4872 pulongest (section->size));
4873 return false;
4874 }
4875
4876 /* The version number. */
4877 uint16_t version = read_2_bytes (abfd, addr);
4878 addr += 2;
4879 if (version != 5)
4880 {
4881 warning (_("Section .debug_names in %s has unsupported version %d, "
4882 "ignoring .debug_names."),
4883 filename, version);
4884 return false;
4885 }
4886
4887 /* Padding. */
4888 uint16_t padding = read_2_bytes (abfd, addr);
4889 addr += 2;
4890 if (padding != 0)
4891 {
4892 warning (_("Section .debug_names in %s has unsupported padding %d, "
4893 "ignoring .debug_names."),
4894 filename, padding);
4895 return false;
4896 }
4897
4898 /* comp_unit_count - The number of CUs in the CU list. */
4899 map.cu_count = read_4_bytes (abfd, addr);
4900 addr += 4;
4901
4902 /* local_type_unit_count - The number of TUs in the local TU
4903 list. */
4904 map.tu_count = read_4_bytes (abfd, addr);
4905 addr += 4;
4906
4907 /* foreign_type_unit_count - The number of TUs in the foreign TU
4908 list. */
4909 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4910 addr += 4;
4911 if (foreign_tu_count != 0)
4912 {
4913 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4914 "ignoring .debug_names."),
4915 filename, static_cast<unsigned long> (foreign_tu_count));
4916 return false;
4917 }
4918
4919 /* bucket_count - The number of hash buckets in the hash lookup
4920 table. */
4921 map.bucket_count = read_4_bytes (abfd, addr);
4922 addr += 4;
4923
4924 /* name_count - The number of unique names in the index. */
4925 map.name_count = read_4_bytes (abfd, addr);
4926 addr += 4;
4927
4928 /* abbrev_table_size - The size in bytes of the abbreviations
4929 table. */
4930 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4931 addr += 4;
4932
4933 /* augmentation_string_size - The size in bytes of the augmentation
4934 string. This value is rounded up to a multiple of 4. */
4935 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4936 addr += 4;
4937 map.augmentation_is_gdb = ((augmentation_string_size
4938 == sizeof (dwarf5_augmentation))
4939 && memcmp (addr, dwarf5_augmentation,
4940 sizeof (dwarf5_augmentation)) == 0);
4941 augmentation_string_size += (-augmentation_string_size) & 3;
4942 addr += augmentation_string_size;
4943
4944 /* List of CUs */
4945 map.cu_table_reordered = addr;
4946 addr += map.cu_count * map.offset_size;
4947
4948 /* List of Local TUs */
4949 map.tu_table_reordered = addr;
4950 addr += map.tu_count * map.offset_size;
4951
4952 /* Hash Lookup Table */
4953 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4954 addr += map.bucket_count * 4;
4955 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4956 addr += map.name_count * 4;
4957
4958 /* Name Table */
4959 map.name_table_string_offs_reordered = addr;
4960 addr += map.name_count * map.offset_size;
4961 map.name_table_entry_offs_reordered = addr;
4962 addr += map.name_count * map.offset_size;
4963
4964 const gdb_byte *abbrev_table_start = addr;
4965 for (;;)
4966 {
4967 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4968 addr += bytes_read;
4969 if (index_num == 0)
4970 break;
4971
4972 const auto insertpair
4973 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4974 if (!insertpair.second)
4975 {
4976 warning (_("Section .debug_names in %s has duplicate index %s, "
4977 "ignoring .debug_names."),
4978 filename, pulongest (index_num));
4979 return false;
4980 }
4981 mapped_debug_names::index_val &indexval = insertpair.first->second;
4982 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4983 addr += bytes_read;
4984
4985 for (;;)
4986 {
4987 mapped_debug_names::index_val::attr attr;
4988 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4989 addr += bytes_read;
4990 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4991 addr += bytes_read;
4992 if (attr.form == DW_FORM_implicit_const)
4993 {
4994 attr.implicit_const = read_signed_leb128 (abfd, addr,
4995 &bytes_read);
4996 addr += bytes_read;
4997 }
4998 if (attr.dw_idx == 0 && attr.form == 0)
4999 break;
5000 indexval.attr_vec.push_back (std::move (attr));
5001 }
5002 }
5003 if (addr != abbrev_table_start + abbrev_table_size)
5004 {
5005 warning (_("Section .debug_names in %s has abbreviation_table "
5006 "of size %s vs. written as %u, ignoring .debug_names."),
5007 filename, plongest (addr - abbrev_table_start),
5008 abbrev_table_size);
5009 return false;
5010 }
5011 map.entry_pool = addr;
5012
5013 return true;
5014 }
5015
5016 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5017 list. */
5018
5019 static void
5020 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5021 const mapped_debug_names &map,
5022 dwarf2_section_info &section,
5023 bool is_dwz)
5024 {
5025 sect_offset sect_off_prev;
5026 for (uint32_t i = 0; i <= map.cu_count; ++i)
5027 {
5028 sect_offset sect_off_next;
5029 if (i < map.cu_count)
5030 {
5031 sect_off_next
5032 = (sect_offset) (extract_unsigned_integer
5033 (map.cu_table_reordered + i * map.offset_size,
5034 map.offset_size,
5035 map.dwarf5_byte_order));
5036 }
5037 else
5038 sect_off_next = (sect_offset) section.size;
5039 if (i >= 1)
5040 {
5041 const ULONGEST length = sect_off_next - sect_off_prev;
5042 dwarf2_per_cu_data *per_cu
5043 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5044 sect_off_prev, length);
5045 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5046 }
5047 sect_off_prev = sect_off_next;
5048 }
5049 }
5050
5051 /* Read the CU list from the mapped index, and use it to create all
5052 the CU objects for this dwarf2_per_objfile. */
5053
5054 static void
5055 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5056 const mapped_debug_names &map,
5057 const mapped_debug_names &dwz_map)
5058 {
5059 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5060 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5061
5062 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5063 dwarf2_per_objfile->info,
5064 false /* is_dwz */);
5065
5066 if (dwz_map.cu_count == 0)
5067 return;
5068
5069 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5070 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5071 true /* is_dwz */);
5072 }
5073
5074 /* Read .debug_names. If everything went ok, initialize the "quick"
5075 elements of all the CUs and return true. Otherwise, return false. */
5076
5077 static bool
5078 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5079 {
5080 std::unique_ptr<mapped_debug_names> map
5081 (new mapped_debug_names (dwarf2_per_objfile));
5082 mapped_debug_names dwz_map (dwarf2_per_objfile);
5083 struct objfile *objfile = dwarf2_per_objfile->objfile;
5084
5085 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5086 &dwarf2_per_objfile->debug_names,
5087 *map))
5088 return false;
5089
5090 /* Don't use the index if it's empty. */
5091 if (map->name_count == 0)
5092 return false;
5093
5094 /* If there is a .dwz file, read it so we can get its CU list as
5095 well. */
5096 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5097 if (dwz != NULL)
5098 {
5099 if (!read_debug_names_from_section (objfile,
5100 bfd_get_filename (dwz->dwz_bfd.get ()),
5101 &dwz->debug_names, dwz_map))
5102 {
5103 warning (_("could not read '.debug_names' section from %s; skipping"),
5104 bfd_get_filename (dwz->dwz_bfd.get ()));
5105 return false;
5106 }
5107 }
5108
5109 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5110
5111 if (map->tu_count != 0)
5112 {
5113 /* We can only handle a single .debug_types when we have an
5114 index. */
5115 if (dwarf2_per_objfile->types.size () != 1)
5116 return false;
5117
5118 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5119
5120 create_signatured_type_table_from_debug_names
5121 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5122 }
5123
5124 create_addrmap_from_aranges (dwarf2_per_objfile,
5125 &dwarf2_per_objfile->debug_aranges);
5126
5127 dwarf2_per_objfile->debug_names_table = std::move (map);
5128 dwarf2_per_objfile->using_index = 1;
5129 dwarf2_per_objfile->quick_file_names_table =
5130 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5131
5132 return true;
5133 }
5134
5135 /* Type used to manage iterating over all CUs looking for a symbol for
5136 .debug_names. */
5137
5138 class dw2_debug_names_iterator
5139 {
5140 public:
5141 dw2_debug_names_iterator (const mapped_debug_names &map,
5142 gdb::optional<block_enum> block_index,
5143 domain_enum domain,
5144 const char *name)
5145 : m_map (map), m_block_index (block_index), m_domain (domain),
5146 m_addr (find_vec_in_debug_names (map, name))
5147 {}
5148
5149 dw2_debug_names_iterator (const mapped_debug_names &map,
5150 search_domain search, uint32_t namei)
5151 : m_map (map),
5152 m_search (search),
5153 m_addr (find_vec_in_debug_names (map, namei))
5154 {}
5155
5156 dw2_debug_names_iterator (const mapped_debug_names &map,
5157 block_enum block_index, domain_enum domain,
5158 uint32_t namei)
5159 : m_map (map), m_block_index (block_index), m_domain (domain),
5160 m_addr (find_vec_in_debug_names (map, namei))
5161 {}
5162
5163 /* Return the next matching CU or NULL if there are no more. */
5164 dwarf2_per_cu_data *next ();
5165
5166 private:
5167 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5168 const char *name);
5169 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5170 uint32_t namei);
5171
5172 /* The internalized form of .debug_names. */
5173 const mapped_debug_names &m_map;
5174
5175 /* If set, only look for symbols that match that block. Valid values are
5176 GLOBAL_BLOCK and STATIC_BLOCK. */
5177 const gdb::optional<block_enum> m_block_index;
5178
5179 /* The kind of symbol we're looking for. */
5180 const domain_enum m_domain = UNDEF_DOMAIN;
5181 const search_domain m_search = ALL_DOMAIN;
5182
5183 /* The list of CUs from the index entry of the symbol, or NULL if
5184 not found. */
5185 const gdb_byte *m_addr;
5186 };
5187
5188 const char *
5189 mapped_debug_names::namei_to_name (uint32_t namei) const
5190 {
5191 const ULONGEST namei_string_offs
5192 = extract_unsigned_integer ((name_table_string_offs_reordered
5193 + namei * offset_size),
5194 offset_size,
5195 dwarf5_byte_order);
5196 return read_indirect_string_at_offset (dwarf2_per_objfile,
5197 namei_string_offs);
5198 }
5199
5200 /* Find a slot in .debug_names for the object named NAME. If NAME is
5201 found, return pointer to its pool data. If NAME cannot be found,
5202 return NULL. */
5203
5204 const gdb_byte *
5205 dw2_debug_names_iterator::find_vec_in_debug_names
5206 (const mapped_debug_names &map, const char *name)
5207 {
5208 int (*cmp) (const char *, const char *);
5209
5210 gdb::unique_xmalloc_ptr<char> without_params;
5211 if (current_language->la_language == language_cplus
5212 || current_language->la_language == language_fortran
5213 || current_language->la_language == language_d)
5214 {
5215 /* NAME is already canonical. Drop any qualifiers as
5216 .debug_names does not contain any. */
5217
5218 if (strchr (name, '(') != NULL)
5219 {
5220 without_params = cp_remove_params (name);
5221 if (without_params != NULL)
5222 name = without_params.get ();
5223 }
5224 }
5225
5226 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5227
5228 const uint32_t full_hash = dwarf5_djb_hash (name);
5229 uint32_t namei
5230 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5231 (map.bucket_table_reordered
5232 + (full_hash % map.bucket_count)), 4,
5233 map.dwarf5_byte_order);
5234 if (namei == 0)
5235 return NULL;
5236 --namei;
5237 if (namei >= map.name_count)
5238 {
5239 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5240 "[in module %s]"),
5241 namei, map.name_count,
5242 objfile_name (map.dwarf2_per_objfile->objfile));
5243 return NULL;
5244 }
5245
5246 for (;;)
5247 {
5248 const uint32_t namei_full_hash
5249 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5250 (map.hash_table_reordered + namei), 4,
5251 map.dwarf5_byte_order);
5252 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5253 return NULL;
5254
5255 if (full_hash == namei_full_hash)
5256 {
5257 const char *const namei_string = map.namei_to_name (namei);
5258
5259 #if 0 /* An expensive sanity check. */
5260 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5261 {
5262 complaint (_("Wrong .debug_names hash for string at index %u "
5263 "[in module %s]"),
5264 namei, objfile_name (dwarf2_per_objfile->objfile));
5265 return NULL;
5266 }
5267 #endif
5268
5269 if (cmp (namei_string, name) == 0)
5270 {
5271 const ULONGEST namei_entry_offs
5272 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5273 + namei * map.offset_size),
5274 map.offset_size, map.dwarf5_byte_order);
5275 return map.entry_pool + namei_entry_offs;
5276 }
5277 }
5278
5279 ++namei;
5280 if (namei >= map.name_count)
5281 return NULL;
5282 }
5283 }
5284
5285 const gdb_byte *
5286 dw2_debug_names_iterator::find_vec_in_debug_names
5287 (const mapped_debug_names &map, uint32_t namei)
5288 {
5289 if (namei >= map.name_count)
5290 {
5291 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5292 "[in module %s]"),
5293 namei, map.name_count,
5294 objfile_name (map.dwarf2_per_objfile->objfile));
5295 return NULL;
5296 }
5297
5298 const ULONGEST namei_entry_offs
5299 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5300 + namei * map.offset_size),
5301 map.offset_size, map.dwarf5_byte_order);
5302 return map.entry_pool + namei_entry_offs;
5303 }
5304
5305 /* See dw2_debug_names_iterator. */
5306
5307 dwarf2_per_cu_data *
5308 dw2_debug_names_iterator::next ()
5309 {
5310 if (m_addr == NULL)
5311 return NULL;
5312
5313 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5314 struct objfile *objfile = dwarf2_per_objfile->objfile;
5315 bfd *const abfd = objfile->obfd;
5316
5317 again:
5318
5319 unsigned int bytes_read;
5320 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5321 m_addr += bytes_read;
5322 if (abbrev == 0)
5323 return NULL;
5324
5325 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5326 if (indexval_it == m_map.abbrev_map.cend ())
5327 {
5328 complaint (_("Wrong .debug_names undefined abbrev code %s "
5329 "[in module %s]"),
5330 pulongest (abbrev), objfile_name (objfile));
5331 return NULL;
5332 }
5333 const mapped_debug_names::index_val &indexval = indexval_it->second;
5334 enum class symbol_linkage {
5335 unknown,
5336 static_,
5337 extern_,
5338 } symbol_linkage_ = symbol_linkage::unknown;
5339 dwarf2_per_cu_data *per_cu = NULL;
5340 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5341 {
5342 ULONGEST ull;
5343 switch (attr.form)
5344 {
5345 case DW_FORM_implicit_const:
5346 ull = attr.implicit_const;
5347 break;
5348 case DW_FORM_flag_present:
5349 ull = 1;
5350 break;
5351 case DW_FORM_udata:
5352 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5353 m_addr += bytes_read;
5354 break;
5355 default:
5356 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5357 dwarf_form_name (attr.form),
5358 objfile_name (objfile));
5359 return NULL;
5360 }
5361 switch (attr.dw_idx)
5362 {
5363 case DW_IDX_compile_unit:
5364 /* Don't crash on bad data. */
5365 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5366 {
5367 complaint (_(".debug_names entry has bad CU index %s"
5368 " [in module %s]"),
5369 pulongest (ull),
5370 objfile_name (dwarf2_per_objfile->objfile));
5371 continue;
5372 }
5373 per_cu = dwarf2_per_objfile->get_cutu (ull);
5374 break;
5375 case DW_IDX_type_unit:
5376 /* Don't crash on bad data. */
5377 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5378 {
5379 complaint (_(".debug_names entry has bad TU index %s"
5380 " [in module %s]"),
5381 pulongest (ull),
5382 objfile_name (dwarf2_per_objfile->objfile));
5383 continue;
5384 }
5385 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5386 break;
5387 case DW_IDX_GNU_internal:
5388 if (!m_map.augmentation_is_gdb)
5389 break;
5390 symbol_linkage_ = symbol_linkage::static_;
5391 break;
5392 case DW_IDX_GNU_external:
5393 if (!m_map.augmentation_is_gdb)
5394 break;
5395 symbol_linkage_ = symbol_linkage::extern_;
5396 break;
5397 }
5398 }
5399
5400 /* Skip if already read in. */
5401 if (per_cu->v.quick->compunit_symtab)
5402 goto again;
5403
5404 /* Check static vs global. */
5405 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5406 {
5407 const bool want_static = *m_block_index == STATIC_BLOCK;
5408 const bool symbol_is_static =
5409 symbol_linkage_ == symbol_linkage::static_;
5410 if (want_static != symbol_is_static)
5411 goto again;
5412 }
5413
5414 /* Match dw2_symtab_iter_next, symbol_kind
5415 and debug_names::psymbol_tag. */
5416 switch (m_domain)
5417 {
5418 case VAR_DOMAIN:
5419 switch (indexval.dwarf_tag)
5420 {
5421 case DW_TAG_variable:
5422 case DW_TAG_subprogram:
5423 /* Some types are also in VAR_DOMAIN. */
5424 case DW_TAG_typedef:
5425 case DW_TAG_structure_type:
5426 break;
5427 default:
5428 goto again;
5429 }
5430 break;
5431 case STRUCT_DOMAIN:
5432 switch (indexval.dwarf_tag)
5433 {
5434 case DW_TAG_typedef:
5435 case DW_TAG_structure_type:
5436 break;
5437 default:
5438 goto again;
5439 }
5440 break;
5441 case LABEL_DOMAIN:
5442 switch (indexval.dwarf_tag)
5443 {
5444 case 0:
5445 case DW_TAG_variable:
5446 break;
5447 default:
5448 goto again;
5449 }
5450 break;
5451 case MODULE_DOMAIN:
5452 switch (indexval.dwarf_tag)
5453 {
5454 case DW_TAG_module:
5455 break;
5456 default:
5457 goto again;
5458 }
5459 break;
5460 default:
5461 break;
5462 }
5463
5464 /* Match dw2_expand_symtabs_matching, symbol_kind and
5465 debug_names::psymbol_tag. */
5466 switch (m_search)
5467 {
5468 case VARIABLES_DOMAIN:
5469 switch (indexval.dwarf_tag)
5470 {
5471 case DW_TAG_variable:
5472 break;
5473 default:
5474 goto again;
5475 }
5476 break;
5477 case FUNCTIONS_DOMAIN:
5478 switch (indexval.dwarf_tag)
5479 {
5480 case DW_TAG_subprogram:
5481 break;
5482 default:
5483 goto again;
5484 }
5485 break;
5486 case TYPES_DOMAIN:
5487 switch (indexval.dwarf_tag)
5488 {
5489 case DW_TAG_typedef:
5490 case DW_TAG_structure_type:
5491 break;
5492 default:
5493 goto again;
5494 }
5495 break;
5496 case MODULES_DOMAIN:
5497 switch (indexval.dwarf_tag)
5498 {
5499 case DW_TAG_module:
5500 break;
5501 default:
5502 goto again;
5503 }
5504 default:
5505 break;
5506 }
5507
5508 return per_cu;
5509 }
5510
5511 static struct compunit_symtab *
5512 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5513 const char *name, domain_enum domain)
5514 {
5515 struct dwarf2_per_objfile *dwarf2_per_objfile
5516 = get_dwarf2_per_objfile (objfile);
5517
5518 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5519 if (!mapp)
5520 {
5521 /* index is NULL if OBJF_READNOW. */
5522 return NULL;
5523 }
5524 const auto &map = *mapp;
5525
5526 dw2_debug_names_iterator iter (map, block_index, domain, name);
5527
5528 struct compunit_symtab *stab_best = NULL;
5529 struct dwarf2_per_cu_data *per_cu;
5530 while ((per_cu = iter.next ()) != NULL)
5531 {
5532 struct symbol *sym, *with_opaque = NULL;
5533 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5534 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5535 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5536
5537 sym = block_find_symbol (block, name, domain,
5538 block_find_non_opaque_type_preferred,
5539 &with_opaque);
5540
5541 /* Some caution must be observed with overloaded functions and
5542 methods, since the index will not contain any overload
5543 information (but NAME might contain it). */
5544
5545 if (sym != NULL
5546 && strcmp_iw (sym->search_name (), name) == 0)
5547 return stab;
5548 if (with_opaque != NULL
5549 && strcmp_iw (with_opaque->search_name (), name) == 0)
5550 stab_best = stab;
5551
5552 /* Keep looking through other CUs. */
5553 }
5554
5555 return stab_best;
5556 }
5557
5558 /* This dumps minimal information about .debug_names. It is called
5559 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5560 uses this to verify that .debug_names has been loaded. */
5561
5562 static void
5563 dw2_debug_names_dump (struct objfile *objfile)
5564 {
5565 struct dwarf2_per_objfile *dwarf2_per_objfile
5566 = get_dwarf2_per_objfile (objfile);
5567
5568 gdb_assert (dwarf2_per_objfile->using_index);
5569 printf_filtered (".debug_names:");
5570 if (dwarf2_per_objfile->debug_names_table)
5571 printf_filtered (" exists\n");
5572 else
5573 printf_filtered (" faked for \"readnow\"\n");
5574 printf_filtered ("\n");
5575 }
5576
5577 static void
5578 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5579 const char *func_name)
5580 {
5581 struct dwarf2_per_objfile *dwarf2_per_objfile
5582 = get_dwarf2_per_objfile (objfile);
5583
5584 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5585 if (dwarf2_per_objfile->debug_names_table)
5586 {
5587 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5588
5589 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5590
5591 struct dwarf2_per_cu_data *per_cu;
5592 while ((per_cu = iter.next ()) != NULL)
5593 dw2_instantiate_symtab (per_cu, false);
5594 }
5595 }
5596
5597 static void
5598 dw2_debug_names_map_matching_symbols
5599 (struct objfile *objfile,
5600 const lookup_name_info &name, domain_enum domain,
5601 int global,
5602 gdb::function_view<symbol_found_callback_ftype> callback,
5603 symbol_compare_ftype *ordered_compare)
5604 {
5605 struct dwarf2_per_objfile *dwarf2_per_objfile
5606 = get_dwarf2_per_objfile (objfile);
5607
5608 /* debug_names_table is NULL if OBJF_READNOW. */
5609 if (!dwarf2_per_objfile->debug_names_table)
5610 return;
5611
5612 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5613 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5614
5615 const char *match_name = name.ada ().lookup_name ().c_str ();
5616 auto matcher = [&] (const char *symname)
5617 {
5618 if (ordered_compare == nullptr)
5619 return true;
5620 return ordered_compare (symname, match_name) == 0;
5621 };
5622
5623 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5624 [&] (offset_type namei)
5625 {
5626 /* The name was matched, now expand corresponding CUs that were
5627 marked. */
5628 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5629
5630 struct dwarf2_per_cu_data *per_cu;
5631 while ((per_cu = iter.next ()) != NULL)
5632 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5633 return true;
5634 });
5635
5636 /* It's a shame we couldn't do this inside the
5637 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5638 that have already been expanded. Instead, this loop matches what
5639 the psymtab code does. */
5640 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5641 {
5642 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5643 if (cust != nullptr)
5644 {
5645 const struct block *block
5646 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5647 if (!iterate_over_symbols_terminated (block, name,
5648 domain, callback))
5649 break;
5650 }
5651 }
5652 }
5653
5654 static void
5655 dw2_debug_names_expand_symtabs_matching
5656 (struct objfile *objfile,
5657 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5658 const lookup_name_info *lookup_name,
5659 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5660 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5661 enum search_domain kind)
5662 {
5663 struct dwarf2_per_objfile *dwarf2_per_objfile
5664 = get_dwarf2_per_objfile (objfile);
5665
5666 /* debug_names_table is NULL if OBJF_READNOW. */
5667 if (!dwarf2_per_objfile->debug_names_table)
5668 return;
5669
5670 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5671
5672 if (symbol_matcher == NULL && lookup_name == NULL)
5673 {
5674 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5675 {
5676 QUIT;
5677
5678 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5679 expansion_notify);
5680 }
5681 return;
5682 }
5683
5684 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5685
5686 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5687 symbol_matcher,
5688 kind, [&] (offset_type namei)
5689 {
5690 /* The name was matched, now expand corresponding CUs that were
5691 marked. */
5692 dw2_debug_names_iterator iter (map, kind, namei);
5693
5694 struct dwarf2_per_cu_data *per_cu;
5695 while ((per_cu = iter.next ()) != NULL)
5696 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5697 expansion_notify);
5698 return true;
5699 });
5700 }
5701
5702 const struct quick_symbol_functions dwarf2_debug_names_functions =
5703 {
5704 dw2_has_symbols,
5705 dw2_find_last_source_symtab,
5706 dw2_forget_cached_source_info,
5707 dw2_map_symtabs_matching_filename,
5708 dw2_debug_names_lookup_symbol,
5709 NULL,
5710 dw2_print_stats,
5711 dw2_debug_names_dump,
5712 dw2_debug_names_expand_symtabs_for_function,
5713 dw2_expand_all_symtabs,
5714 dw2_expand_symtabs_with_fullname,
5715 dw2_debug_names_map_matching_symbols,
5716 dw2_debug_names_expand_symtabs_matching,
5717 dw2_find_pc_sect_compunit_symtab,
5718 NULL,
5719 dw2_map_symbol_filenames
5720 };
5721
5722 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5723 to either a dwarf2_per_objfile or dwz_file object. */
5724
5725 template <typename T>
5726 static gdb::array_view<const gdb_byte>
5727 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5728 {
5729 dwarf2_section_info *section = &section_owner->gdb_index;
5730
5731 if (section->empty ())
5732 return {};
5733
5734 /* Older elfutils strip versions could keep the section in the main
5735 executable while splitting it for the separate debug info file. */
5736 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5737 return {};
5738
5739 section->read (obj);
5740
5741 /* dwarf2_section_info::size is a bfd_size_type, while
5742 gdb::array_view works with size_t. On 32-bit hosts, with
5743 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5744 is 32-bit. So we need an explicit narrowing conversion here.
5745 This is fine, because it's impossible to allocate or mmap an
5746 array/buffer larger than what size_t can represent. */
5747 return gdb::make_array_view (section->buffer, section->size);
5748 }
5749
5750 /* Lookup the index cache for the contents of the index associated to
5751 DWARF2_OBJ. */
5752
5753 static gdb::array_view<const gdb_byte>
5754 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5755 {
5756 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5757 if (build_id == nullptr)
5758 return {};
5759
5760 return global_index_cache.lookup_gdb_index (build_id,
5761 &dwarf2_obj->index_cache_res);
5762 }
5763
5764 /* Same as the above, but for DWZ. */
5765
5766 static gdb::array_view<const gdb_byte>
5767 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5768 {
5769 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5770 if (build_id == nullptr)
5771 return {};
5772
5773 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5774 }
5775
5776 /* See symfile.h. */
5777
5778 bool
5779 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5780 {
5781 struct dwarf2_per_objfile *dwarf2_per_objfile
5782 = get_dwarf2_per_objfile (objfile);
5783
5784 /* If we're about to read full symbols, don't bother with the
5785 indices. In this case we also don't care if some other debug
5786 format is making psymtabs, because they are all about to be
5787 expanded anyway. */
5788 if ((objfile->flags & OBJF_READNOW))
5789 {
5790 dwarf2_per_objfile->using_index = 1;
5791 create_all_comp_units (dwarf2_per_objfile);
5792 create_all_type_units (dwarf2_per_objfile);
5793 dwarf2_per_objfile->quick_file_names_table
5794 = create_quick_file_names_table
5795 (dwarf2_per_objfile->all_comp_units.size ());
5796
5797 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5798 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5799 {
5800 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5801
5802 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5803 struct dwarf2_per_cu_quick_data);
5804 }
5805
5806 /* Return 1 so that gdb sees the "quick" functions. However,
5807 these functions will be no-ops because we will have expanded
5808 all symtabs. */
5809 *index_kind = dw_index_kind::GDB_INDEX;
5810 return true;
5811 }
5812
5813 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5814 {
5815 *index_kind = dw_index_kind::DEBUG_NAMES;
5816 return true;
5817 }
5818
5819 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5820 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5821 get_gdb_index_contents_from_section<dwz_file>))
5822 {
5823 *index_kind = dw_index_kind::GDB_INDEX;
5824 return true;
5825 }
5826
5827 /* ... otherwise, try to find the index in the index cache. */
5828 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5829 get_gdb_index_contents_from_cache,
5830 get_gdb_index_contents_from_cache_dwz))
5831 {
5832 global_index_cache.hit ();
5833 *index_kind = dw_index_kind::GDB_INDEX;
5834 return true;
5835 }
5836
5837 global_index_cache.miss ();
5838 return false;
5839 }
5840
5841 \f
5842
5843 /* Build a partial symbol table. */
5844
5845 void
5846 dwarf2_build_psymtabs (struct objfile *objfile)
5847 {
5848 struct dwarf2_per_objfile *dwarf2_per_objfile
5849 = get_dwarf2_per_objfile (objfile);
5850
5851 init_psymbol_list (objfile, 1024);
5852
5853 try
5854 {
5855 /* This isn't really ideal: all the data we allocate on the
5856 objfile's obstack is still uselessly kept around. However,
5857 freeing it seems unsafe. */
5858 psymtab_discarder psymtabs (objfile);
5859 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5860 psymtabs.keep ();
5861
5862 /* (maybe) store an index in the cache. */
5863 global_index_cache.store (dwarf2_per_objfile);
5864 }
5865 catch (const gdb_exception_error &except)
5866 {
5867 exception_print (gdb_stderr, except);
5868 }
5869 }
5870
5871 /* Find the base address of the compilation unit for range lists and
5872 location lists. It will normally be specified by DW_AT_low_pc.
5873 In DWARF-3 draft 4, the base address could be overridden by
5874 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5875 compilation units with discontinuous ranges. */
5876
5877 static void
5878 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5879 {
5880 struct attribute *attr;
5881
5882 cu->base_address.reset ();
5883
5884 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5885 if (attr != nullptr)
5886 cu->base_address = attr->value_as_address ();
5887 else
5888 {
5889 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5890 if (attr != nullptr)
5891 cu->base_address = attr->value_as_address ();
5892 }
5893 }
5894
5895 /* Helper function that returns the proper abbrev section for
5896 THIS_CU. */
5897
5898 static struct dwarf2_section_info *
5899 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5900 {
5901 struct dwarf2_section_info *abbrev;
5902 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5903
5904 if (this_cu->is_dwz)
5905 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5906 else
5907 abbrev = &dwarf2_per_objfile->abbrev;
5908
5909 return abbrev;
5910 }
5911
5912 /* Fetch the abbreviation table offset from a comp or type unit header. */
5913
5914 static sect_offset
5915 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5916 struct dwarf2_section_info *section,
5917 sect_offset sect_off)
5918 {
5919 bfd *abfd = section->get_bfd_owner ();
5920 const gdb_byte *info_ptr;
5921 unsigned int initial_length_size, offset_size;
5922 uint16_t version;
5923
5924 section->read (dwarf2_per_objfile->objfile);
5925 info_ptr = section->buffer + to_underlying (sect_off);
5926 read_initial_length (abfd, info_ptr, &initial_length_size);
5927 offset_size = initial_length_size == 4 ? 4 : 8;
5928 info_ptr += initial_length_size;
5929
5930 version = read_2_bytes (abfd, info_ptr);
5931 info_ptr += 2;
5932 if (version >= 5)
5933 {
5934 /* Skip unit type and address size. */
5935 info_ptr += 2;
5936 }
5937
5938 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5939 }
5940
5941 /* A partial symtab that is used only for include files. */
5942 struct dwarf2_include_psymtab : public partial_symtab
5943 {
5944 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5945 : partial_symtab (filename, objfile)
5946 {
5947 }
5948
5949 void read_symtab (struct objfile *objfile) override
5950 {
5951 /* It's an include file, no symbols to read for it.
5952 Everything is in the includer symtab. */
5953
5954 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5955 expansion of the includer psymtab. We use the dependencies[0] field to
5956 model the includer. But if we go the regular route of calling
5957 expand_psymtab here, and having expand_psymtab call expand_dependencies
5958 to expand the includer, we'll only use expand_psymtab on the includer
5959 (making it a non-toplevel psymtab), while if we expand the includer via
5960 another path, we'll use read_symtab (making it a toplevel psymtab).
5961 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5962 psymtab, and trigger read_symtab on the includer here directly. */
5963 includer ()->read_symtab (objfile);
5964 }
5965
5966 void expand_psymtab (struct objfile *objfile) override
5967 {
5968 /* This is not called by read_symtab, and should not be called by any
5969 expand_dependencies. */
5970 gdb_assert (false);
5971 }
5972
5973 bool readin_p () const override
5974 {
5975 return includer ()->readin_p ();
5976 }
5977
5978 struct compunit_symtab *get_compunit_symtab () const override
5979 {
5980 return nullptr;
5981 }
5982
5983 private:
5984 partial_symtab *includer () const
5985 {
5986 /* An include psymtab has exactly one dependency: the psymtab that
5987 includes it. */
5988 gdb_assert (this->number_of_dependencies == 1);
5989 return this->dependencies[0];
5990 }
5991 };
5992
5993 /* Allocate a new partial symtab for file named NAME and mark this new
5994 partial symtab as being an include of PST. */
5995
5996 static void
5997 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5998 struct objfile *objfile)
5999 {
6000 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6001
6002 if (!IS_ABSOLUTE_PATH (subpst->filename))
6003 {
6004 /* It shares objfile->objfile_obstack. */
6005 subpst->dirname = pst->dirname;
6006 }
6007
6008 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6009 subpst->dependencies[0] = pst;
6010 subpst->number_of_dependencies = 1;
6011 }
6012
6013 /* Read the Line Number Program data and extract the list of files
6014 included by the source file represented by PST. Build an include
6015 partial symtab for each of these included files. */
6016
6017 static void
6018 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6019 struct die_info *die,
6020 dwarf2_psymtab *pst)
6021 {
6022 line_header_up lh;
6023 struct attribute *attr;
6024
6025 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6026 if (attr != nullptr)
6027 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6028 if (lh == NULL)
6029 return; /* No linetable, so no includes. */
6030
6031 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6032 that we pass in the raw text_low here; that is ok because we're
6033 only decoding the line table to make include partial symtabs, and
6034 so the addresses aren't really used. */
6035 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6036 pst->raw_text_low (), 1);
6037 }
6038
6039 static hashval_t
6040 hash_signatured_type (const void *item)
6041 {
6042 const struct signatured_type *sig_type
6043 = (const struct signatured_type *) item;
6044
6045 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6046 return sig_type->signature;
6047 }
6048
6049 static int
6050 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6051 {
6052 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6053 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6054
6055 return lhs->signature == rhs->signature;
6056 }
6057
6058 /* Allocate a hash table for signatured types. */
6059
6060 static htab_up
6061 allocate_signatured_type_table ()
6062 {
6063 return htab_up (htab_create_alloc (41,
6064 hash_signatured_type,
6065 eq_signatured_type,
6066 NULL, xcalloc, xfree));
6067 }
6068
6069 /* A helper function to add a signatured type CU to a table. */
6070
6071 static int
6072 add_signatured_type_cu_to_table (void **slot, void *datum)
6073 {
6074 struct signatured_type *sigt = (struct signatured_type *) *slot;
6075 std::vector<signatured_type *> *all_type_units
6076 = (std::vector<signatured_type *> *) datum;
6077
6078 all_type_units->push_back (sigt);
6079
6080 return 1;
6081 }
6082
6083 /* A helper for create_debug_types_hash_table. Read types from SECTION
6084 and fill them into TYPES_HTAB. It will process only type units,
6085 therefore DW_UT_type. */
6086
6087 static void
6088 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6089 struct dwo_file *dwo_file,
6090 dwarf2_section_info *section, htab_up &types_htab,
6091 rcuh_kind section_kind)
6092 {
6093 struct objfile *objfile = dwarf2_per_objfile->objfile;
6094 struct dwarf2_section_info *abbrev_section;
6095 bfd *abfd;
6096 const gdb_byte *info_ptr, *end_ptr;
6097
6098 abbrev_section = (dwo_file != NULL
6099 ? &dwo_file->sections.abbrev
6100 : &dwarf2_per_objfile->abbrev);
6101
6102 if (dwarf_read_debug)
6103 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6104 section->get_name (),
6105 abbrev_section->get_file_name ());
6106
6107 section->read (objfile);
6108 info_ptr = section->buffer;
6109
6110 if (info_ptr == NULL)
6111 return;
6112
6113 /* We can't set abfd until now because the section may be empty or
6114 not present, in which case the bfd is unknown. */
6115 abfd = section->get_bfd_owner ();
6116
6117 /* We don't use cutu_reader here because we don't need to read
6118 any dies: the signature is in the header. */
6119
6120 end_ptr = info_ptr + section->size;
6121 while (info_ptr < end_ptr)
6122 {
6123 struct signatured_type *sig_type;
6124 struct dwo_unit *dwo_tu;
6125 void **slot;
6126 const gdb_byte *ptr = info_ptr;
6127 struct comp_unit_head header;
6128 unsigned int length;
6129
6130 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6131
6132 /* Initialize it due to a false compiler warning. */
6133 header.signature = -1;
6134 header.type_cu_offset_in_tu = (cu_offset) -1;
6135
6136 /* We need to read the type's signature in order to build the hash
6137 table, but we don't need anything else just yet. */
6138
6139 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6140 abbrev_section, ptr, section_kind);
6141
6142 length = header.get_length ();
6143
6144 /* Skip dummy type units. */
6145 if (ptr >= info_ptr + length
6146 || peek_abbrev_code (abfd, ptr) == 0
6147 || header.unit_type != DW_UT_type)
6148 {
6149 info_ptr += length;
6150 continue;
6151 }
6152
6153 if (types_htab == NULL)
6154 {
6155 if (dwo_file)
6156 types_htab = allocate_dwo_unit_table ();
6157 else
6158 types_htab = allocate_signatured_type_table ();
6159 }
6160
6161 if (dwo_file)
6162 {
6163 sig_type = NULL;
6164 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6165 struct dwo_unit);
6166 dwo_tu->dwo_file = dwo_file;
6167 dwo_tu->signature = header.signature;
6168 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6169 dwo_tu->section = section;
6170 dwo_tu->sect_off = sect_off;
6171 dwo_tu->length = length;
6172 }
6173 else
6174 {
6175 /* N.B.: type_offset is not usable if this type uses a DWO file.
6176 The real type_offset is in the DWO file. */
6177 dwo_tu = NULL;
6178 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6179 struct signatured_type);
6180 sig_type->signature = header.signature;
6181 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6182 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6183 sig_type->per_cu.is_debug_types = 1;
6184 sig_type->per_cu.section = section;
6185 sig_type->per_cu.sect_off = sect_off;
6186 sig_type->per_cu.length = length;
6187 }
6188
6189 slot = htab_find_slot (types_htab.get (),
6190 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6191 INSERT);
6192 gdb_assert (slot != NULL);
6193 if (*slot != NULL)
6194 {
6195 sect_offset dup_sect_off;
6196
6197 if (dwo_file)
6198 {
6199 const struct dwo_unit *dup_tu
6200 = (const struct dwo_unit *) *slot;
6201
6202 dup_sect_off = dup_tu->sect_off;
6203 }
6204 else
6205 {
6206 const struct signatured_type *dup_tu
6207 = (const struct signatured_type *) *slot;
6208
6209 dup_sect_off = dup_tu->per_cu.sect_off;
6210 }
6211
6212 complaint (_("debug type entry at offset %s is duplicate to"
6213 " the entry at offset %s, signature %s"),
6214 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6215 hex_string (header.signature));
6216 }
6217 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6218
6219 if (dwarf_read_debug > 1)
6220 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6221 sect_offset_str (sect_off),
6222 hex_string (header.signature));
6223
6224 info_ptr += length;
6225 }
6226 }
6227
6228 /* Create the hash table of all entries in the .debug_types
6229 (or .debug_types.dwo) section(s).
6230 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6231 otherwise it is NULL.
6232
6233 The result is a pointer to the hash table or NULL if there are no types.
6234
6235 Note: This function processes DWO files only, not DWP files. */
6236
6237 static void
6238 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6239 struct dwo_file *dwo_file,
6240 gdb::array_view<dwarf2_section_info> type_sections,
6241 htab_up &types_htab)
6242 {
6243 for (dwarf2_section_info &section : type_sections)
6244 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6245 types_htab, rcuh_kind::TYPE);
6246 }
6247
6248 /* Create the hash table of all entries in the .debug_types section,
6249 and initialize all_type_units.
6250 The result is zero if there is an error (e.g. missing .debug_types section),
6251 otherwise non-zero. */
6252
6253 static int
6254 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6255 {
6256 htab_up types_htab;
6257
6258 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6259 &dwarf2_per_objfile->info, types_htab,
6260 rcuh_kind::COMPILE);
6261 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6262 dwarf2_per_objfile->types, types_htab);
6263 if (types_htab == NULL)
6264 {
6265 dwarf2_per_objfile->signatured_types = NULL;
6266 return 0;
6267 }
6268
6269 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6270
6271 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6272 dwarf2_per_objfile->all_type_units.reserve
6273 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6274
6275 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6276 add_signatured_type_cu_to_table,
6277 &dwarf2_per_objfile->all_type_units);
6278
6279 return 1;
6280 }
6281
6282 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6283 If SLOT is non-NULL, it is the entry to use in the hash table.
6284 Otherwise we find one. */
6285
6286 static struct signatured_type *
6287 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6288 void **slot)
6289 {
6290 struct objfile *objfile = dwarf2_per_objfile->objfile;
6291
6292 if (dwarf2_per_objfile->all_type_units.size ()
6293 == dwarf2_per_objfile->all_type_units.capacity ())
6294 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6295
6296 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6297 struct signatured_type);
6298
6299 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6300 sig_type->signature = sig;
6301 sig_type->per_cu.is_debug_types = 1;
6302 if (dwarf2_per_objfile->using_index)
6303 {
6304 sig_type->per_cu.v.quick =
6305 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6306 struct dwarf2_per_cu_quick_data);
6307 }
6308
6309 if (slot == NULL)
6310 {
6311 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6312 sig_type, INSERT);
6313 }
6314 gdb_assert (*slot == NULL);
6315 *slot = sig_type;
6316 /* The rest of sig_type must be filled in by the caller. */
6317 return sig_type;
6318 }
6319
6320 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6321 Fill in SIG_ENTRY with DWO_ENTRY. */
6322
6323 static void
6324 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6325 struct signatured_type *sig_entry,
6326 struct dwo_unit *dwo_entry)
6327 {
6328 /* Make sure we're not clobbering something we don't expect to. */
6329 gdb_assert (! sig_entry->per_cu.queued);
6330 gdb_assert (sig_entry->per_cu.cu == NULL);
6331 if (dwarf2_per_objfile->using_index)
6332 {
6333 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6334 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6335 }
6336 else
6337 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6338 gdb_assert (sig_entry->signature == dwo_entry->signature);
6339 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6340 gdb_assert (sig_entry->type_unit_group == NULL);
6341 gdb_assert (sig_entry->dwo_unit == NULL);
6342
6343 sig_entry->per_cu.section = dwo_entry->section;
6344 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6345 sig_entry->per_cu.length = dwo_entry->length;
6346 sig_entry->per_cu.reading_dwo_directly = 1;
6347 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6348 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6349 sig_entry->dwo_unit = dwo_entry;
6350 }
6351
6352 /* Subroutine of lookup_signatured_type.
6353 If we haven't read the TU yet, create the signatured_type data structure
6354 for a TU to be read in directly from a DWO file, bypassing the stub.
6355 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6356 using .gdb_index, then when reading a CU we want to stay in the DWO file
6357 containing that CU. Otherwise we could end up reading several other DWO
6358 files (due to comdat folding) to process the transitive closure of all the
6359 mentioned TUs, and that can be slow. The current DWO file will have every
6360 type signature that it needs.
6361 We only do this for .gdb_index because in the psymtab case we already have
6362 to read all the DWOs to build the type unit groups. */
6363
6364 static struct signatured_type *
6365 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6366 {
6367 struct dwarf2_per_objfile *dwarf2_per_objfile
6368 = cu->per_cu->dwarf2_per_objfile;
6369 struct dwo_file *dwo_file;
6370 struct dwo_unit find_dwo_entry, *dwo_entry;
6371 struct signatured_type find_sig_entry, *sig_entry;
6372 void **slot;
6373
6374 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6375
6376 /* If TU skeletons have been removed then we may not have read in any
6377 TUs yet. */
6378 if (dwarf2_per_objfile->signatured_types == NULL)
6379 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6380
6381 /* We only ever need to read in one copy of a signatured type.
6382 Use the global signatured_types array to do our own comdat-folding
6383 of types. If this is the first time we're reading this TU, and
6384 the TU has an entry in .gdb_index, replace the recorded data from
6385 .gdb_index with this TU. */
6386
6387 find_sig_entry.signature = sig;
6388 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6389 &find_sig_entry, INSERT);
6390 sig_entry = (struct signatured_type *) *slot;
6391
6392 /* We can get here with the TU already read, *or* in the process of being
6393 read. Don't reassign the global entry to point to this DWO if that's
6394 the case. Also note that if the TU is already being read, it may not
6395 have come from a DWO, the program may be a mix of Fission-compiled
6396 code and non-Fission-compiled code. */
6397
6398 /* Have we already tried to read this TU?
6399 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6400 needn't exist in the global table yet). */
6401 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6402 return sig_entry;
6403
6404 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6405 dwo_unit of the TU itself. */
6406 dwo_file = cu->dwo_unit->dwo_file;
6407
6408 /* Ok, this is the first time we're reading this TU. */
6409 if (dwo_file->tus == NULL)
6410 return NULL;
6411 find_dwo_entry.signature = sig;
6412 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6413 &find_dwo_entry);
6414 if (dwo_entry == NULL)
6415 return NULL;
6416
6417 /* If the global table doesn't have an entry for this TU, add one. */
6418 if (sig_entry == NULL)
6419 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6420
6421 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6422 sig_entry->per_cu.tu_read = 1;
6423 return sig_entry;
6424 }
6425
6426 /* Subroutine of lookup_signatured_type.
6427 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6428 then try the DWP file. If the TU stub (skeleton) has been removed then
6429 it won't be in .gdb_index. */
6430
6431 static struct signatured_type *
6432 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6433 {
6434 struct dwarf2_per_objfile *dwarf2_per_objfile
6435 = cu->per_cu->dwarf2_per_objfile;
6436 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6437 struct dwo_unit *dwo_entry;
6438 struct signatured_type find_sig_entry, *sig_entry;
6439 void **slot;
6440
6441 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6442 gdb_assert (dwp_file != NULL);
6443
6444 /* If TU skeletons have been removed then we may not have read in any
6445 TUs yet. */
6446 if (dwarf2_per_objfile->signatured_types == NULL)
6447 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6448
6449 find_sig_entry.signature = sig;
6450 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6451 &find_sig_entry, INSERT);
6452 sig_entry = (struct signatured_type *) *slot;
6453
6454 /* Have we already tried to read this TU?
6455 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6456 needn't exist in the global table yet). */
6457 if (sig_entry != NULL)
6458 return sig_entry;
6459
6460 if (dwp_file->tus == NULL)
6461 return NULL;
6462 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6463 sig, 1 /* is_debug_types */);
6464 if (dwo_entry == NULL)
6465 return NULL;
6466
6467 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6468 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6469
6470 return sig_entry;
6471 }
6472
6473 /* Lookup a signature based type for DW_FORM_ref_sig8.
6474 Returns NULL if signature SIG is not present in the table.
6475 It is up to the caller to complain about this. */
6476
6477 static struct signatured_type *
6478 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6479 {
6480 struct dwarf2_per_objfile *dwarf2_per_objfile
6481 = cu->per_cu->dwarf2_per_objfile;
6482
6483 if (cu->dwo_unit
6484 && dwarf2_per_objfile->using_index)
6485 {
6486 /* We're in a DWO/DWP file, and we're using .gdb_index.
6487 These cases require special processing. */
6488 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6489 return lookup_dwo_signatured_type (cu, sig);
6490 else
6491 return lookup_dwp_signatured_type (cu, sig);
6492 }
6493 else
6494 {
6495 struct signatured_type find_entry, *entry;
6496
6497 if (dwarf2_per_objfile->signatured_types == NULL)
6498 return NULL;
6499 find_entry.signature = sig;
6500 entry = ((struct signatured_type *)
6501 htab_find (dwarf2_per_objfile->signatured_types.get (),
6502 &find_entry));
6503 return entry;
6504 }
6505 }
6506
6507 /* Low level DIE reading support. */
6508
6509 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6510
6511 static void
6512 init_cu_die_reader (struct die_reader_specs *reader,
6513 struct dwarf2_cu *cu,
6514 struct dwarf2_section_info *section,
6515 struct dwo_file *dwo_file,
6516 struct abbrev_table *abbrev_table)
6517 {
6518 gdb_assert (section->readin && section->buffer != NULL);
6519 reader->abfd = section->get_bfd_owner ();
6520 reader->cu = cu;
6521 reader->dwo_file = dwo_file;
6522 reader->die_section = section;
6523 reader->buffer = section->buffer;
6524 reader->buffer_end = section->buffer + section->size;
6525 reader->abbrev_table = abbrev_table;
6526 }
6527
6528 /* Subroutine of cutu_reader to simplify it.
6529 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6530 There's just a lot of work to do, and cutu_reader is big enough
6531 already.
6532
6533 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6534 from it to the DIE in the DWO. If NULL we are skipping the stub.
6535 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6536 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6537 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6538 STUB_COMP_DIR may be non-NULL.
6539 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6540 are filled in with the info of the DIE from the DWO file.
6541 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6542 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6543 kept around for at least as long as *RESULT_READER.
6544
6545 The result is non-zero if a valid (non-dummy) DIE was found. */
6546
6547 static int
6548 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6549 struct dwo_unit *dwo_unit,
6550 struct die_info *stub_comp_unit_die,
6551 const char *stub_comp_dir,
6552 struct die_reader_specs *result_reader,
6553 const gdb_byte **result_info_ptr,
6554 struct die_info **result_comp_unit_die,
6555 abbrev_table_up *result_dwo_abbrev_table)
6556 {
6557 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6558 struct objfile *objfile = dwarf2_per_objfile->objfile;
6559 struct dwarf2_cu *cu = this_cu->cu;
6560 bfd *abfd;
6561 const gdb_byte *begin_info_ptr, *info_ptr;
6562 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6563 int i,num_extra_attrs;
6564 struct dwarf2_section_info *dwo_abbrev_section;
6565 struct die_info *comp_unit_die;
6566
6567 /* At most one of these may be provided. */
6568 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6569
6570 /* These attributes aren't processed until later:
6571 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6572 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6573 referenced later. However, these attributes are found in the stub
6574 which we won't have later. In order to not impose this complication
6575 on the rest of the code, we read them here and copy them to the
6576 DWO CU/TU die. */
6577
6578 stmt_list = NULL;
6579 low_pc = NULL;
6580 high_pc = NULL;
6581 ranges = NULL;
6582 comp_dir = NULL;
6583
6584 if (stub_comp_unit_die != NULL)
6585 {
6586 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6587 DWO file. */
6588 if (! this_cu->is_debug_types)
6589 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6590 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6591 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6592 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6593 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6594
6595 cu->addr_base = stub_comp_unit_die->addr_base ();
6596
6597 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6598 here (if needed). We need the value before we can process
6599 DW_AT_ranges. */
6600 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6601 }
6602 else if (stub_comp_dir != NULL)
6603 {
6604 /* Reconstruct the comp_dir attribute to simplify the code below. */
6605 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6606 comp_dir->name = DW_AT_comp_dir;
6607 comp_dir->form = DW_FORM_string;
6608 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6609 DW_STRING (comp_dir) = stub_comp_dir;
6610 }
6611
6612 /* Set up for reading the DWO CU/TU. */
6613 cu->dwo_unit = dwo_unit;
6614 dwarf2_section_info *section = dwo_unit->section;
6615 section->read (objfile);
6616 abfd = section->get_bfd_owner ();
6617 begin_info_ptr = info_ptr = (section->buffer
6618 + to_underlying (dwo_unit->sect_off));
6619 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6620
6621 if (this_cu->is_debug_types)
6622 {
6623 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6624
6625 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6626 &cu->header, section,
6627 dwo_abbrev_section,
6628 info_ptr, rcuh_kind::TYPE);
6629 /* This is not an assert because it can be caused by bad debug info. */
6630 if (sig_type->signature != cu->header.signature)
6631 {
6632 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6633 " TU at offset %s [in module %s]"),
6634 hex_string (sig_type->signature),
6635 hex_string (cu->header.signature),
6636 sect_offset_str (dwo_unit->sect_off),
6637 bfd_get_filename (abfd));
6638 }
6639 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6640 /* For DWOs coming from DWP files, we don't know the CU length
6641 nor the type's offset in the TU until now. */
6642 dwo_unit->length = cu->header.get_length ();
6643 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6644
6645 /* Establish the type offset that can be used to lookup the type.
6646 For DWO files, we don't know it until now. */
6647 sig_type->type_offset_in_section
6648 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6649 }
6650 else
6651 {
6652 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6653 &cu->header, section,
6654 dwo_abbrev_section,
6655 info_ptr, rcuh_kind::COMPILE);
6656 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6657 /* For DWOs coming from DWP files, we don't know the CU length
6658 until now. */
6659 dwo_unit->length = cu->header.get_length ();
6660 }
6661
6662 *result_dwo_abbrev_table
6663 = abbrev_table::read (objfile, dwo_abbrev_section,
6664 cu->header.abbrev_sect_off);
6665 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6666 result_dwo_abbrev_table->get ());
6667
6668 /* Read in the die, but leave space to copy over the attributes
6669 from the stub. This has the benefit of simplifying the rest of
6670 the code - all the work to maintain the illusion of a single
6671 DW_TAG_{compile,type}_unit DIE is done here. */
6672 num_extra_attrs = ((stmt_list != NULL)
6673 + (low_pc != NULL)
6674 + (high_pc != NULL)
6675 + (ranges != NULL)
6676 + (comp_dir != NULL));
6677 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6678 num_extra_attrs);
6679
6680 /* Copy over the attributes from the stub to the DIE we just read in. */
6681 comp_unit_die = *result_comp_unit_die;
6682 i = comp_unit_die->num_attrs;
6683 if (stmt_list != NULL)
6684 comp_unit_die->attrs[i++] = *stmt_list;
6685 if (low_pc != NULL)
6686 comp_unit_die->attrs[i++] = *low_pc;
6687 if (high_pc != NULL)
6688 comp_unit_die->attrs[i++] = *high_pc;
6689 if (ranges != NULL)
6690 comp_unit_die->attrs[i++] = *ranges;
6691 if (comp_dir != NULL)
6692 comp_unit_die->attrs[i++] = *comp_dir;
6693 comp_unit_die->num_attrs += num_extra_attrs;
6694
6695 if (dwarf_die_debug)
6696 {
6697 fprintf_unfiltered (gdb_stdlog,
6698 "Read die from %s@0x%x of %s:\n",
6699 section->get_name (),
6700 (unsigned) (begin_info_ptr - section->buffer),
6701 bfd_get_filename (abfd));
6702 dump_die (comp_unit_die, dwarf_die_debug);
6703 }
6704
6705 /* Skip dummy compilation units. */
6706 if (info_ptr >= begin_info_ptr + dwo_unit->length
6707 || peek_abbrev_code (abfd, info_ptr) == 0)
6708 return 0;
6709
6710 *result_info_ptr = info_ptr;
6711 return 1;
6712 }
6713
6714 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6715 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6716 signature is part of the header. */
6717 static gdb::optional<ULONGEST>
6718 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6719 {
6720 if (cu->header.version >= 5)
6721 return cu->header.signature;
6722 struct attribute *attr;
6723 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6724 if (attr == nullptr)
6725 return gdb::optional<ULONGEST> ();
6726 return DW_UNSND (attr);
6727 }
6728
6729 /* Subroutine of cutu_reader to simplify it.
6730 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6731 Returns NULL if the specified DWO unit cannot be found. */
6732
6733 static struct dwo_unit *
6734 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6735 struct die_info *comp_unit_die,
6736 const char *dwo_name)
6737 {
6738 struct dwarf2_cu *cu = this_cu->cu;
6739 struct dwo_unit *dwo_unit;
6740 const char *comp_dir;
6741
6742 gdb_assert (cu != NULL);
6743
6744 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6745 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6746 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6747
6748 if (this_cu->is_debug_types)
6749 {
6750 struct signatured_type *sig_type;
6751
6752 /* Since this_cu is the first member of struct signatured_type,
6753 we can go from a pointer to one to a pointer to the other. */
6754 sig_type = (struct signatured_type *) this_cu;
6755 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6756 }
6757 else
6758 {
6759 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6760 if (!signature.has_value ())
6761 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6762 " [in module %s]"),
6763 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6764 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6765 *signature);
6766 }
6767
6768 return dwo_unit;
6769 }
6770
6771 /* Subroutine of cutu_reader to simplify it.
6772 See it for a description of the parameters.
6773 Read a TU directly from a DWO file, bypassing the stub. */
6774
6775 void
6776 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6777 int use_existing_cu)
6778 {
6779 struct signatured_type *sig_type;
6780
6781 /* Verify we can do the following downcast, and that we have the
6782 data we need. */
6783 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6784 sig_type = (struct signatured_type *) this_cu;
6785 gdb_assert (sig_type->dwo_unit != NULL);
6786
6787 if (use_existing_cu && this_cu->cu != NULL)
6788 {
6789 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6790 /* There's no need to do the rereading_dwo_cu handling that
6791 cutu_reader does since we don't read the stub. */
6792 }
6793 else
6794 {
6795 /* If !use_existing_cu, this_cu->cu must be NULL. */
6796 gdb_assert (this_cu->cu == NULL);
6797 m_new_cu.reset (new dwarf2_cu (this_cu));
6798 }
6799
6800 /* A future optimization, if needed, would be to use an existing
6801 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6802 could share abbrev tables. */
6803
6804 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6805 NULL /* stub_comp_unit_die */,
6806 sig_type->dwo_unit->dwo_file->comp_dir,
6807 this, &info_ptr,
6808 &comp_unit_die,
6809 &m_dwo_abbrev_table) == 0)
6810 {
6811 /* Dummy die. */
6812 dummy_p = true;
6813 }
6814 }
6815
6816 /* Initialize a CU (or TU) and read its DIEs.
6817 If the CU defers to a DWO file, read the DWO file as well.
6818
6819 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6820 Otherwise the table specified in the comp unit header is read in and used.
6821 This is an optimization for when we already have the abbrev table.
6822
6823 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6824 Otherwise, a new CU is allocated with xmalloc. */
6825
6826 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6827 struct abbrev_table *abbrev_table,
6828 int use_existing_cu,
6829 bool skip_partial)
6830 : die_reader_specs {},
6831 m_this_cu (this_cu)
6832 {
6833 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6834 struct objfile *objfile = dwarf2_per_objfile->objfile;
6835 struct dwarf2_section_info *section = this_cu->section;
6836 bfd *abfd = section->get_bfd_owner ();
6837 struct dwarf2_cu *cu;
6838 const gdb_byte *begin_info_ptr;
6839 struct signatured_type *sig_type = NULL;
6840 struct dwarf2_section_info *abbrev_section;
6841 /* Non-zero if CU currently points to a DWO file and we need to
6842 reread it. When this happens we need to reread the skeleton die
6843 before we can reread the DWO file (this only applies to CUs, not TUs). */
6844 int rereading_dwo_cu = 0;
6845
6846 if (dwarf_die_debug)
6847 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6848 this_cu->is_debug_types ? "type" : "comp",
6849 sect_offset_str (this_cu->sect_off));
6850
6851 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6852 file (instead of going through the stub), short-circuit all of this. */
6853 if (this_cu->reading_dwo_directly)
6854 {
6855 /* Narrow down the scope of possibilities to have to understand. */
6856 gdb_assert (this_cu->is_debug_types);
6857 gdb_assert (abbrev_table == NULL);
6858 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6859 return;
6860 }
6861
6862 /* This is cheap if the section is already read in. */
6863 section->read (objfile);
6864
6865 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6866
6867 abbrev_section = get_abbrev_section_for_cu (this_cu);
6868
6869 if (use_existing_cu && this_cu->cu != NULL)
6870 {
6871 cu = this_cu->cu;
6872 /* If this CU is from a DWO file we need to start over, we need to
6873 refetch the attributes from the skeleton CU.
6874 This could be optimized by retrieving those attributes from when we
6875 were here the first time: the previous comp_unit_die was stored in
6876 comp_unit_obstack. But there's no data yet that we need this
6877 optimization. */
6878 if (cu->dwo_unit != NULL)
6879 rereading_dwo_cu = 1;
6880 }
6881 else
6882 {
6883 /* If !use_existing_cu, this_cu->cu must be NULL. */
6884 gdb_assert (this_cu->cu == NULL);
6885 m_new_cu.reset (new dwarf2_cu (this_cu));
6886 cu = m_new_cu.get ();
6887 }
6888
6889 /* Get the header. */
6890 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6891 {
6892 /* We already have the header, there's no need to read it in again. */
6893 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6894 }
6895 else
6896 {
6897 if (this_cu->is_debug_types)
6898 {
6899 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6900 &cu->header, section,
6901 abbrev_section, info_ptr,
6902 rcuh_kind::TYPE);
6903
6904 /* Since per_cu is the first member of struct signatured_type,
6905 we can go from a pointer to one to a pointer to the other. */
6906 sig_type = (struct signatured_type *) this_cu;
6907 gdb_assert (sig_type->signature == cu->header.signature);
6908 gdb_assert (sig_type->type_offset_in_tu
6909 == cu->header.type_cu_offset_in_tu);
6910 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6911
6912 /* LENGTH has not been set yet for type units if we're
6913 using .gdb_index. */
6914 this_cu->length = cu->header.get_length ();
6915
6916 /* Establish the type offset that can be used to lookup the type. */
6917 sig_type->type_offset_in_section =
6918 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6919
6920 this_cu->dwarf_version = cu->header.version;
6921 }
6922 else
6923 {
6924 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6925 &cu->header, section,
6926 abbrev_section,
6927 info_ptr,
6928 rcuh_kind::COMPILE);
6929
6930 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6931 gdb_assert (this_cu->length == cu->header.get_length ());
6932 this_cu->dwarf_version = cu->header.version;
6933 }
6934 }
6935
6936 /* Skip dummy compilation units. */
6937 if (info_ptr >= begin_info_ptr + this_cu->length
6938 || peek_abbrev_code (abfd, info_ptr) == 0)
6939 {
6940 dummy_p = true;
6941 return;
6942 }
6943
6944 /* If we don't have them yet, read the abbrevs for this compilation unit.
6945 And if we need to read them now, make sure they're freed when we're
6946 done. */
6947 if (abbrev_table != NULL)
6948 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6949 else
6950 {
6951 m_abbrev_table_holder
6952 = abbrev_table::read (objfile, abbrev_section,
6953 cu->header.abbrev_sect_off);
6954 abbrev_table = m_abbrev_table_holder.get ();
6955 }
6956
6957 /* Read the top level CU/TU die. */
6958 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6959 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6960
6961 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6962 {
6963 dummy_p = true;
6964 return;
6965 }
6966
6967 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6968 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6969 table from the DWO file and pass the ownership over to us. It will be
6970 referenced from READER, so we must make sure to free it after we're done
6971 with READER.
6972
6973 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6974 DWO CU, that this test will fail (the attribute will not be present). */
6975 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6976 if (dwo_name != nullptr)
6977 {
6978 struct dwo_unit *dwo_unit;
6979 struct die_info *dwo_comp_unit_die;
6980
6981 if (comp_unit_die->has_children)
6982 {
6983 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6984 " has children (offset %s) [in module %s]"),
6985 sect_offset_str (this_cu->sect_off),
6986 bfd_get_filename (abfd));
6987 }
6988 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6989 if (dwo_unit != NULL)
6990 {
6991 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6992 comp_unit_die, NULL,
6993 this, &info_ptr,
6994 &dwo_comp_unit_die,
6995 &m_dwo_abbrev_table) == 0)
6996 {
6997 /* Dummy die. */
6998 dummy_p = true;
6999 return;
7000 }
7001 comp_unit_die = dwo_comp_unit_die;
7002 }
7003 else
7004 {
7005 /* Yikes, we couldn't find the rest of the DIE, we only have
7006 the stub. A complaint has already been logged. There's
7007 not much more we can do except pass on the stub DIE to
7008 die_reader_func. We don't want to throw an error on bad
7009 debug info. */
7010 }
7011 }
7012 }
7013
7014 void
7015 cutu_reader::keep ()
7016 {
7017 /* Done, clean up. */
7018 gdb_assert (!dummy_p);
7019 if (m_new_cu != NULL)
7020 {
7021 struct dwarf2_per_objfile *dwarf2_per_objfile
7022 = m_this_cu->dwarf2_per_objfile;
7023 /* Link this CU into read_in_chain. */
7024 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7025 dwarf2_per_objfile->read_in_chain = m_this_cu;
7026 /* The chain owns it now. */
7027 m_new_cu.release ();
7028 }
7029 }
7030
7031 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7032 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7033 assumed to have already done the lookup to find the DWO file).
7034
7035 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7036 THIS_CU->is_debug_types, but nothing else.
7037
7038 We fill in THIS_CU->length.
7039
7040 THIS_CU->cu is always freed when done.
7041 This is done in order to not leave THIS_CU->cu in a state where we have
7042 to care whether it refers to the "main" CU or the DWO CU.
7043
7044 When parent_cu is passed, it is used to provide a default value for
7045 str_offsets_base and addr_base from the parent. */
7046
7047 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7048 struct dwarf2_cu *parent_cu,
7049 struct dwo_file *dwo_file)
7050 : die_reader_specs {},
7051 m_this_cu (this_cu)
7052 {
7053 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7054 struct objfile *objfile = dwarf2_per_objfile->objfile;
7055 struct dwarf2_section_info *section = this_cu->section;
7056 bfd *abfd = section->get_bfd_owner ();
7057 struct dwarf2_section_info *abbrev_section;
7058 const gdb_byte *begin_info_ptr, *info_ptr;
7059
7060 if (dwarf_die_debug)
7061 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7062 this_cu->is_debug_types ? "type" : "comp",
7063 sect_offset_str (this_cu->sect_off));
7064
7065 gdb_assert (this_cu->cu == NULL);
7066
7067 abbrev_section = (dwo_file != NULL
7068 ? &dwo_file->sections.abbrev
7069 : get_abbrev_section_for_cu (this_cu));
7070
7071 /* This is cheap if the section is already read in. */
7072 section->read (objfile);
7073
7074 m_new_cu.reset (new dwarf2_cu (this_cu));
7075
7076 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7077 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7078 &m_new_cu->header, section,
7079 abbrev_section, info_ptr,
7080 (this_cu->is_debug_types
7081 ? rcuh_kind::TYPE
7082 : rcuh_kind::COMPILE));
7083
7084 if (parent_cu != nullptr)
7085 {
7086 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7087 m_new_cu->addr_base = parent_cu->addr_base;
7088 }
7089 this_cu->length = m_new_cu->header.get_length ();
7090
7091 /* Skip dummy compilation units. */
7092 if (info_ptr >= begin_info_ptr + this_cu->length
7093 || peek_abbrev_code (abfd, info_ptr) == 0)
7094 {
7095 dummy_p = true;
7096 return;
7097 }
7098
7099 m_abbrev_table_holder
7100 = abbrev_table::read (objfile, abbrev_section,
7101 m_new_cu->header.abbrev_sect_off);
7102
7103 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7104 m_abbrev_table_holder.get ());
7105 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7106 }
7107
7108 \f
7109 /* Type Unit Groups.
7110
7111 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7112 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7113 so that all types coming from the same compilation (.o file) are grouped
7114 together. A future step could be to put the types in the same symtab as
7115 the CU the types ultimately came from. */
7116
7117 static hashval_t
7118 hash_type_unit_group (const void *item)
7119 {
7120 const struct type_unit_group *tu_group
7121 = (const struct type_unit_group *) item;
7122
7123 return hash_stmt_list_entry (&tu_group->hash);
7124 }
7125
7126 static int
7127 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7128 {
7129 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7130 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7131
7132 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7133 }
7134
7135 /* Allocate a hash table for type unit groups. */
7136
7137 static htab_up
7138 allocate_type_unit_groups_table ()
7139 {
7140 return htab_up (htab_create_alloc (3,
7141 hash_type_unit_group,
7142 eq_type_unit_group,
7143 NULL, xcalloc, xfree));
7144 }
7145
7146 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7147 partial symtabs. We combine several TUs per psymtab to not let the size
7148 of any one psymtab grow too big. */
7149 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7150 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7151
7152 /* Helper routine for get_type_unit_group.
7153 Create the type_unit_group object used to hold one or more TUs. */
7154
7155 static struct type_unit_group *
7156 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7157 {
7158 struct dwarf2_per_objfile *dwarf2_per_objfile
7159 = cu->per_cu->dwarf2_per_objfile;
7160 struct objfile *objfile = dwarf2_per_objfile->objfile;
7161 struct dwarf2_per_cu_data *per_cu;
7162 struct type_unit_group *tu_group;
7163
7164 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7165 struct type_unit_group);
7166 per_cu = &tu_group->per_cu;
7167 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7168
7169 if (dwarf2_per_objfile->using_index)
7170 {
7171 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7172 struct dwarf2_per_cu_quick_data);
7173 }
7174 else
7175 {
7176 unsigned int line_offset = to_underlying (line_offset_struct);
7177 dwarf2_psymtab *pst;
7178 std::string name;
7179
7180 /* Give the symtab a useful name for debug purposes. */
7181 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7182 name = string_printf ("<type_units_%d>",
7183 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7184 else
7185 name = string_printf ("<type_units_at_0x%x>", line_offset);
7186
7187 pst = create_partial_symtab (per_cu, name.c_str ());
7188 pst->anonymous = true;
7189 }
7190
7191 tu_group->hash.dwo_unit = cu->dwo_unit;
7192 tu_group->hash.line_sect_off = line_offset_struct;
7193
7194 return tu_group;
7195 }
7196
7197 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7198 STMT_LIST is a DW_AT_stmt_list attribute. */
7199
7200 static struct type_unit_group *
7201 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7202 {
7203 struct dwarf2_per_objfile *dwarf2_per_objfile
7204 = cu->per_cu->dwarf2_per_objfile;
7205 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7206 struct type_unit_group *tu_group;
7207 void **slot;
7208 unsigned int line_offset;
7209 struct type_unit_group type_unit_group_for_lookup;
7210
7211 if (dwarf2_per_objfile->type_unit_groups == NULL)
7212 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
7213
7214 /* Do we need to create a new group, or can we use an existing one? */
7215
7216 if (stmt_list)
7217 {
7218 line_offset = DW_UNSND (stmt_list);
7219 ++tu_stats->nr_symtab_sharers;
7220 }
7221 else
7222 {
7223 /* Ugh, no stmt_list. Rare, but we have to handle it.
7224 We can do various things here like create one group per TU or
7225 spread them over multiple groups to split up the expansion work.
7226 To avoid worst case scenarios (too many groups or too large groups)
7227 we, umm, group them in bunches. */
7228 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7229 | (tu_stats->nr_stmt_less_type_units
7230 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7231 ++tu_stats->nr_stmt_less_type_units;
7232 }
7233
7234 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7235 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7236 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7237 &type_unit_group_for_lookup, INSERT);
7238 if (*slot != NULL)
7239 {
7240 tu_group = (struct type_unit_group *) *slot;
7241 gdb_assert (tu_group != NULL);
7242 }
7243 else
7244 {
7245 sect_offset line_offset_struct = (sect_offset) line_offset;
7246 tu_group = create_type_unit_group (cu, line_offset_struct);
7247 *slot = tu_group;
7248 ++tu_stats->nr_symtabs;
7249 }
7250
7251 return tu_group;
7252 }
7253 \f
7254 /* Partial symbol tables. */
7255
7256 /* Create a psymtab named NAME and assign it to PER_CU.
7257
7258 The caller must fill in the following details:
7259 dirname, textlow, texthigh. */
7260
7261 static dwarf2_psymtab *
7262 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7263 {
7264 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7265 dwarf2_psymtab *pst;
7266
7267 pst = new dwarf2_psymtab (name, objfile, per_cu);
7268
7269 pst->psymtabs_addrmap_supported = true;
7270
7271 /* This is the glue that links PST into GDB's symbol API. */
7272 per_cu->v.psymtab = pst;
7273
7274 return pst;
7275 }
7276
7277 /* DIE reader function for process_psymtab_comp_unit. */
7278
7279 static void
7280 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7281 const gdb_byte *info_ptr,
7282 struct die_info *comp_unit_die,
7283 enum language pretend_language)
7284 {
7285 struct dwarf2_cu *cu = reader->cu;
7286 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7287 struct gdbarch *gdbarch = objfile->arch ();
7288 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7289 CORE_ADDR baseaddr;
7290 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7291 dwarf2_psymtab *pst;
7292 enum pc_bounds_kind cu_bounds_kind;
7293 const char *filename;
7294
7295 gdb_assert (! per_cu->is_debug_types);
7296
7297 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7298
7299 /* Allocate a new partial symbol table structure. */
7300 gdb::unique_xmalloc_ptr<char> debug_filename;
7301 static const char artificial[] = "<artificial>";
7302 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7303 if (filename == NULL)
7304 filename = "";
7305 else if (strcmp (filename, artificial) == 0)
7306 {
7307 debug_filename.reset (concat (artificial, "@",
7308 sect_offset_str (per_cu->sect_off),
7309 (char *) NULL));
7310 filename = debug_filename.get ();
7311 }
7312
7313 pst = create_partial_symtab (per_cu, filename);
7314
7315 /* This must be done before calling dwarf2_build_include_psymtabs. */
7316 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7317
7318 baseaddr = objfile->text_section_offset ();
7319
7320 dwarf2_find_base_address (comp_unit_die, cu);
7321
7322 /* Possibly set the default values of LOWPC and HIGHPC from
7323 `DW_AT_ranges'. */
7324 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7325 &best_highpc, cu, pst);
7326 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7327 {
7328 CORE_ADDR low
7329 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7330 - baseaddr);
7331 CORE_ADDR high
7332 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7333 - baseaddr - 1);
7334 /* Store the contiguous range if it is not empty; it can be
7335 empty for CUs with no code. */
7336 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7337 low, high, pst);
7338 }
7339
7340 /* Check if comp unit has_children.
7341 If so, read the rest of the partial symbols from this comp unit.
7342 If not, there's no more debug_info for this comp unit. */
7343 if (comp_unit_die->has_children)
7344 {
7345 struct partial_die_info *first_die;
7346 CORE_ADDR lowpc, highpc;
7347
7348 lowpc = ((CORE_ADDR) -1);
7349 highpc = ((CORE_ADDR) 0);
7350
7351 first_die = load_partial_dies (reader, info_ptr, 1);
7352
7353 scan_partial_symbols (first_die, &lowpc, &highpc,
7354 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7355
7356 /* If we didn't find a lowpc, set it to highpc to avoid
7357 complaints from `maint check'. */
7358 if (lowpc == ((CORE_ADDR) -1))
7359 lowpc = highpc;
7360
7361 /* If the compilation unit didn't have an explicit address range,
7362 then use the information extracted from its child dies. */
7363 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7364 {
7365 best_lowpc = lowpc;
7366 best_highpc = highpc;
7367 }
7368 }
7369 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7370 best_lowpc + baseaddr)
7371 - baseaddr);
7372 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7373 best_highpc + baseaddr)
7374 - baseaddr);
7375
7376 end_psymtab_common (objfile, pst);
7377
7378 if (!cu->per_cu->imported_symtabs_empty ())
7379 {
7380 int i;
7381 int len = cu->per_cu->imported_symtabs_size ();
7382
7383 /* Fill in 'dependencies' here; we fill in 'users' in a
7384 post-pass. */
7385 pst->number_of_dependencies = len;
7386 pst->dependencies
7387 = objfile->partial_symtabs->allocate_dependencies (len);
7388 for (i = 0; i < len; ++i)
7389 {
7390 pst->dependencies[i]
7391 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7392 }
7393
7394 cu->per_cu->imported_symtabs_free ();
7395 }
7396
7397 /* Get the list of files included in the current compilation unit,
7398 and build a psymtab for each of them. */
7399 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7400
7401 if (dwarf_read_debug)
7402 fprintf_unfiltered (gdb_stdlog,
7403 "Psymtab for %s unit @%s: %s - %s"
7404 ", %d global, %d static syms\n",
7405 per_cu->is_debug_types ? "type" : "comp",
7406 sect_offset_str (per_cu->sect_off),
7407 paddress (gdbarch, pst->text_low (objfile)),
7408 paddress (gdbarch, pst->text_high (objfile)),
7409 pst->n_global_syms, pst->n_static_syms);
7410 }
7411
7412 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7413 Process compilation unit THIS_CU for a psymtab. */
7414
7415 static void
7416 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7417 bool want_partial_unit,
7418 enum language pretend_language)
7419 {
7420 /* If this compilation unit was already read in, free the
7421 cached copy in order to read it in again. This is
7422 necessary because we skipped some symbols when we first
7423 read in the compilation unit (see load_partial_dies).
7424 This problem could be avoided, but the benefit is unclear. */
7425 if (this_cu->cu != NULL)
7426 free_one_cached_comp_unit (this_cu);
7427
7428 cutu_reader reader (this_cu, NULL, 0, false);
7429
7430 switch (reader.comp_unit_die->tag)
7431 {
7432 case DW_TAG_compile_unit:
7433 this_cu->unit_type = DW_UT_compile;
7434 break;
7435 case DW_TAG_partial_unit:
7436 this_cu->unit_type = DW_UT_partial;
7437 break;
7438 default:
7439 abort ();
7440 }
7441
7442 if (reader.dummy_p)
7443 {
7444 /* Nothing. */
7445 }
7446 else if (this_cu->is_debug_types)
7447 build_type_psymtabs_reader (&reader, reader.info_ptr,
7448 reader.comp_unit_die);
7449 else if (want_partial_unit
7450 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7451 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7452 reader.comp_unit_die,
7453 pretend_language);
7454
7455 this_cu->lang = this_cu->cu->language;
7456
7457 /* Age out any secondary CUs. */
7458 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7459 }
7460
7461 /* Reader function for build_type_psymtabs. */
7462
7463 static void
7464 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7465 const gdb_byte *info_ptr,
7466 struct die_info *type_unit_die)
7467 {
7468 struct dwarf2_per_objfile *dwarf2_per_objfile
7469 = reader->cu->per_cu->dwarf2_per_objfile;
7470 struct objfile *objfile = dwarf2_per_objfile->objfile;
7471 struct dwarf2_cu *cu = reader->cu;
7472 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7473 struct signatured_type *sig_type;
7474 struct type_unit_group *tu_group;
7475 struct attribute *attr;
7476 struct partial_die_info *first_die;
7477 CORE_ADDR lowpc, highpc;
7478 dwarf2_psymtab *pst;
7479
7480 gdb_assert (per_cu->is_debug_types);
7481 sig_type = (struct signatured_type *) per_cu;
7482
7483 if (! type_unit_die->has_children)
7484 return;
7485
7486 attr = type_unit_die->attr (DW_AT_stmt_list);
7487 tu_group = get_type_unit_group (cu, attr);
7488
7489 if (tu_group->tus == nullptr)
7490 tu_group->tus = new std::vector<signatured_type *>;
7491 tu_group->tus->push_back (sig_type);
7492
7493 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7494 pst = create_partial_symtab (per_cu, "");
7495 pst->anonymous = true;
7496
7497 first_die = load_partial_dies (reader, info_ptr, 1);
7498
7499 lowpc = (CORE_ADDR) -1;
7500 highpc = (CORE_ADDR) 0;
7501 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7502
7503 end_psymtab_common (objfile, pst);
7504 }
7505
7506 /* Struct used to sort TUs by their abbreviation table offset. */
7507
7508 struct tu_abbrev_offset
7509 {
7510 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7511 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7512 {}
7513
7514 signatured_type *sig_type;
7515 sect_offset abbrev_offset;
7516 };
7517
7518 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7519
7520 static bool
7521 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7522 const struct tu_abbrev_offset &b)
7523 {
7524 return a.abbrev_offset < b.abbrev_offset;
7525 }
7526
7527 /* Efficiently read all the type units.
7528 This does the bulk of the work for build_type_psymtabs.
7529
7530 The efficiency is because we sort TUs by the abbrev table they use and
7531 only read each abbrev table once. In one program there are 200K TUs
7532 sharing 8K abbrev tables.
7533
7534 The main purpose of this function is to support building the
7535 dwarf2_per_objfile->type_unit_groups table.
7536 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7537 can collapse the search space by grouping them by stmt_list.
7538 The savings can be significant, in the same program from above the 200K TUs
7539 share 8K stmt_list tables.
7540
7541 FUNC is expected to call get_type_unit_group, which will create the
7542 struct type_unit_group if necessary and add it to
7543 dwarf2_per_objfile->type_unit_groups. */
7544
7545 static void
7546 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7547 {
7548 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7549 abbrev_table_up abbrev_table;
7550 sect_offset abbrev_offset;
7551
7552 /* It's up to the caller to not call us multiple times. */
7553 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7554
7555 if (dwarf2_per_objfile->all_type_units.empty ())
7556 return;
7557
7558 /* TUs typically share abbrev tables, and there can be way more TUs than
7559 abbrev tables. Sort by abbrev table to reduce the number of times we
7560 read each abbrev table in.
7561 Alternatives are to punt or to maintain a cache of abbrev tables.
7562 This is simpler and efficient enough for now.
7563
7564 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7565 symtab to use). Typically TUs with the same abbrev offset have the same
7566 stmt_list value too so in practice this should work well.
7567
7568 The basic algorithm here is:
7569
7570 sort TUs by abbrev table
7571 for each TU with same abbrev table:
7572 read abbrev table if first user
7573 read TU top level DIE
7574 [IWBN if DWO skeletons had DW_AT_stmt_list]
7575 call FUNC */
7576
7577 if (dwarf_read_debug)
7578 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7579
7580 /* Sort in a separate table to maintain the order of all_type_units
7581 for .gdb_index: TU indices directly index all_type_units. */
7582 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7583 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7584
7585 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7586 sorted_by_abbrev.emplace_back
7587 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7588 sig_type->per_cu.section,
7589 sig_type->per_cu.sect_off));
7590
7591 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7592 sort_tu_by_abbrev_offset);
7593
7594 abbrev_offset = (sect_offset) ~(unsigned) 0;
7595
7596 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7597 {
7598 /* Switch to the next abbrev table if necessary. */
7599 if (abbrev_table == NULL
7600 || tu.abbrev_offset != abbrev_offset)
7601 {
7602 abbrev_offset = tu.abbrev_offset;
7603 abbrev_table =
7604 abbrev_table::read (dwarf2_per_objfile->objfile,
7605 &dwarf2_per_objfile->abbrev,
7606 abbrev_offset);
7607 ++tu_stats->nr_uniq_abbrev_tables;
7608 }
7609
7610 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7611 0, false);
7612 if (!reader.dummy_p)
7613 build_type_psymtabs_reader (&reader, reader.info_ptr,
7614 reader.comp_unit_die);
7615 }
7616 }
7617
7618 /* Print collected type unit statistics. */
7619
7620 static void
7621 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7622 {
7623 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7624
7625 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7626 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7627 dwarf2_per_objfile->all_type_units.size ());
7628 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7629 tu_stats->nr_uniq_abbrev_tables);
7630 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7631 tu_stats->nr_symtabs);
7632 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7633 tu_stats->nr_symtab_sharers);
7634 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7635 tu_stats->nr_stmt_less_type_units);
7636 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7637 tu_stats->nr_all_type_units_reallocs);
7638 }
7639
7640 /* Traversal function for build_type_psymtabs. */
7641
7642 static int
7643 build_type_psymtab_dependencies (void **slot, void *info)
7644 {
7645 struct dwarf2_per_objfile *dwarf2_per_objfile
7646 = (struct dwarf2_per_objfile *) info;
7647 struct objfile *objfile = dwarf2_per_objfile->objfile;
7648 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7649 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7650 dwarf2_psymtab *pst = per_cu->v.psymtab;
7651 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7652 int i;
7653
7654 gdb_assert (len > 0);
7655 gdb_assert (per_cu->type_unit_group_p ());
7656
7657 pst->number_of_dependencies = len;
7658 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7659 for (i = 0; i < len; ++i)
7660 {
7661 struct signatured_type *iter = tu_group->tus->at (i);
7662 gdb_assert (iter->per_cu.is_debug_types);
7663 pst->dependencies[i] = iter->per_cu.v.psymtab;
7664 iter->type_unit_group = tu_group;
7665 }
7666
7667 delete tu_group->tus;
7668 tu_group->tus = nullptr;
7669
7670 return 1;
7671 }
7672
7673 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7674 Build partial symbol tables for the .debug_types comp-units. */
7675
7676 static void
7677 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7678 {
7679 if (! create_all_type_units (dwarf2_per_objfile))
7680 return;
7681
7682 build_type_psymtabs_1 (dwarf2_per_objfile);
7683 }
7684
7685 /* Traversal function for process_skeletonless_type_unit.
7686 Read a TU in a DWO file and build partial symbols for it. */
7687
7688 static int
7689 process_skeletonless_type_unit (void **slot, void *info)
7690 {
7691 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7692 struct dwarf2_per_objfile *dwarf2_per_objfile
7693 = (struct dwarf2_per_objfile *) info;
7694 struct signatured_type find_entry, *entry;
7695
7696 /* If this TU doesn't exist in the global table, add it and read it in. */
7697
7698 if (dwarf2_per_objfile->signatured_types == NULL)
7699 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
7700
7701 find_entry.signature = dwo_unit->signature;
7702 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7703 &find_entry, INSERT);
7704 /* If we've already seen this type there's nothing to do. What's happening
7705 is we're doing our own version of comdat-folding here. */
7706 if (*slot != NULL)
7707 return 1;
7708
7709 /* This does the job that create_all_type_units would have done for
7710 this TU. */
7711 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7712 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7713 *slot = entry;
7714
7715 /* This does the job that build_type_psymtabs_1 would have done. */
7716 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7717 if (!reader.dummy_p)
7718 build_type_psymtabs_reader (&reader, reader.info_ptr,
7719 reader.comp_unit_die);
7720
7721 return 1;
7722 }
7723
7724 /* Traversal function for process_skeletonless_type_units. */
7725
7726 static int
7727 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7728 {
7729 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7730
7731 if (dwo_file->tus != NULL)
7732 htab_traverse_noresize (dwo_file->tus.get (),
7733 process_skeletonless_type_unit, info);
7734
7735 return 1;
7736 }
7737
7738 /* Scan all TUs of DWO files, verifying we've processed them.
7739 This is needed in case a TU was emitted without its skeleton.
7740 Note: This can't be done until we know what all the DWO files are. */
7741
7742 static void
7743 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7744 {
7745 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7746 if (get_dwp_file (dwarf2_per_objfile) == NULL
7747 && dwarf2_per_objfile->dwo_files != NULL)
7748 {
7749 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7750 process_dwo_file_for_skeletonless_type_units,
7751 dwarf2_per_objfile);
7752 }
7753 }
7754
7755 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7756
7757 static void
7758 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7759 {
7760 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7761 {
7762 dwarf2_psymtab *pst = per_cu->v.psymtab;
7763
7764 if (pst == NULL)
7765 continue;
7766
7767 for (int j = 0; j < pst->number_of_dependencies; ++j)
7768 {
7769 /* Set the 'user' field only if it is not already set. */
7770 if (pst->dependencies[j]->user == NULL)
7771 pst->dependencies[j]->user = pst;
7772 }
7773 }
7774 }
7775
7776 /* Build the partial symbol table by doing a quick pass through the
7777 .debug_info and .debug_abbrev sections. */
7778
7779 static void
7780 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7781 {
7782 struct objfile *objfile = dwarf2_per_objfile->objfile;
7783
7784 if (dwarf_read_debug)
7785 {
7786 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7787 objfile_name (objfile));
7788 }
7789
7790 scoped_restore restore_reading_psyms
7791 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7792 true);
7793
7794 dwarf2_per_objfile->info.read (objfile);
7795
7796 /* Any cached compilation units will be linked by the per-objfile
7797 read_in_chain. Make sure to free them when we're done. */
7798 free_cached_comp_units freer (dwarf2_per_objfile);
7799
7800 build_type_psymtabs (dwarf2_per_objfile);
7801
7802 create_all_comp_units (dwarf2_per_objfile);
7803
7804 /* Create a temporary address map on a temporary obstack. We later
7805 copy this to the final obstack. */
7806 auto_obstack temp_obstack;
7807
7808 scoped_restore save_psymtabs_addrmap
7809 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7810 addrmap_create_mutable (&temp_obstack));
7811
7812 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7813 {
7814 if (per_cu->v.psymtab != NULL)
7815 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7816 continue;
7817 process_psymtab_comp_unit (per_cu, false, language_minimal);
7818 }
7819
7820 /* This has to wait until we read the CUs, we need the list of DWOs. */
7821 process_skeletonless_type_units (dwarf2_per_objfile);
7822
7823 /* Now that all TUs have been processed we can fill in the dependencies. */
7824 if (dwarf2_per_objfile->type_unit_groups != NULL)
7825 {
7826 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7827 build_type_psymtab_dependencies, dwarf2_per_objfile);
7828 }
7829
7830 if (dwarf_read_debug)
7831 print_tu_stats (dwarf2_per_objfile);
7832
7833 set_partial_user (dwarf2_per_objfile);
7834
7835 objfile->partial_symtabs->psymtabs_addrmap
7836 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7837 objfile->partial_symtabs->obstack ());
7838 /* At this point we want to keep the address map. */
7839 save_psymtabs_addrmap.release ();
7840
7841 if (dwarf_read_debug)
7842 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7843 objfile_name (objfile));
7844 }
7845
7846 /* Load the partial DIEs for a secondary CU into memory.
7847 This is also used when rereading a primary CU with load_all_dies. */
7848
7849 static void
7850 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7851 {
7852 cutu_reader reader (this_cu, NULL, 1, false);
7853
7854 if (!reader.dummy_p)
7855 {
7856 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7857 language_minimal);
7858
7859 /* Check if comp unit has_children.
7860 If so, read the rest of the partial symbols from this comp unit.
7861 If not, there's no more debug_info for this comp unit. */
7862 if (reader.comp_unit_die->has_children)
7863 load_partial_dies (&reader, reader.info_ptr, 0);
7864
7865 reader.keep ();
7866 }
7867 }
7868
7869 static void
7870 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7871 struct dwarf2_section_info *section,
7872 struct dwarf2_section_info *abbrev_section,
7873 unsigned int is_dwz)
7874 {
7875 const gdb_byte *info_ptr;
7876 struct objfile *objfile = dwarf2_per_objfile->objfile;
7877
7878 if (dwarf_read_debug)
7879 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7880 section->get_name (),
7881 section->get_file_name ());
7882
7883 section->read (objfile);
7884
7885 info_ptr = section->buffer;
7886
7887 while (info_ptr < section->buffer + section->size)
7888 {
7889 struct dwarf2_per_cu_data *this_cu;
7890
7891 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7892
7893 comp_unit_head cu_header;
7894 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7895 abbrev_section, info_ptr,
7896 rcuh_kind::COMPILE);
7897
7898 /* Save the compilation unit for later lookup. */
7899 if (cu_header.unit_type != DW_UT_type)
7900 {
7901 this_cu = XOBNEW (&objfile->objfile_obstack,
7902 struct dwarf2_per_cu_data);
7903 memset (this_cu, 0, sizeof (*this_cu));
7904 }
7905 else
7906 {
7907 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7908 struct signatured_type);
7909 memset (sig_type, 0, sizeof (*sig_type));
7910 sig_type->signature = cu_header.signature;
7911 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7912 this_cu = &sig_type->per_cu;
7913 }
7914 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7915 this_cu->sect_off = sect_off;
7916 this_cu->length = cu_header.length + cu_header.initial_length_size;
7917 this_cu->is_dwz = is_dwz;
7918 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7919 this_cu->section = section;
7920
7921 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7922
7923 info_ptr = info_ptr + this_cu->length;
7924 }
7925 }
7926
7927 /* Create a list of all compilation units in OBJFILE.
7928 This is only done for -readnow and building partial symtabs. */
7929
7930 static void
7931 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7932 {
7933 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7934 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7935 &dwarf2_per_objfile->abbrev, 0);
7936
7937 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7938 if (dwz != NULL)
7939 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7940 1);
7941 }
7942
7943 /* Process all loaded DIEs for compilation unit CU, starting at
7944 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7945 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7946 DW_AT_ranges). See the comments of add_partial_subprogram on how
7947 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7948
7949 static void
7950 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7951 CORE_ADDR *highpc, int set_addrmap,
7952 struct dwarf2_cu *cu)
7953 {
7954 struct partial_die_info *pdi;
7955
7956 /* Now, march along the PDI's, descending into ones which have
7957 interesting children but skipping the children of the other ones,
7958 until we reach the end of the compilation unit. */
7959
7960 pdi = first_die;
7961
7962 while (pdi != NULL)
7963 {
7964 pdi->fixup (cu);
7965
7966 /* Anonymous namespaces or modules have no name but have interesting
7967 children, so we need to look at them. Ditto for anonymous
7968 enums. */
7969
7970 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7971 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7972 || pdi->tag == DW_TAG_imported_unit
7973 || pdi->tag == DW_TAG_inlined_subroutine)
7974 {
7975 switch (pdi->tag)
7976 {
7977 case DW_TAG_subprogram:
7978 case DW_TAG_inlined_subroutine:
7979 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7980 break;
7981 case DW_TAG_constant:
7982 case DW_TAG_variable:
7983 case DW_TAG_typedef:
7984 case DW_TAG_union_type:
7985 if (!pdi->is_declaration
7986 || (pdi->tag == DW_TAG_variable && pdi->is_external))
7987 {
7988 add_partial_symbol (pdi, cu);
7989 }
7990 break;
7991 case DW_TAG_class_type:
7992 case DW_TAG_interface_type:
7993 case DW_TAG_structure_type:
7994 if (!pdi->is_declaration)
7995 {
7996 add_partial_symbol (pdi, cu);
7997 }
7998 if ((cu->language == language_rust
7999 || cu->language == language_cplus) && pdi->has_children)
8000 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8001 set_addrmap, cu);
8002 break;
8003 case DW_TAG_enumeration_type:
8004 if (!pdi->is_declaration)
8005 add_partial_enumeration (pdi, cu);
8006 break;
8007 case DW_TAG_base_type:
8008 case DW_TAG_subrange_type:
8009 /* File scope base type definitions are added to the partial
8010 symbol table. */
8011 add_partial_symbol (pdi, cu);
8012 break;
8013 case DW_TAG_namespace:
8014 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8015 break;
8016 case DW_TAG_module:
8017 if (!pdi->is_declaration)
8018 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8019 break;
8020 case DW_TAG_imported_unit:
8021 {
8022 struct dwarf2_per_cu_data *per_cu;
8023
8024 /* For now we don't handle imported units in type units. */
8025 if (cu->per_cu->is_debug_types)
8026 {
8027 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8028 " supported in type units [in module %s]"),
8029 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8030 }
8031
8032 per_cu = dwarf2_find_containing_comp_unit
8033 (pdi->d.sect_off, pdi->is_dwz,
8034 cu->per_cu->dwarf2_per_objfile);
8035
8036 /* Go read the partial unit, if needed. */
8037 if (per_cu->v.psymtab == NULL)
8038 process_psymtab_comp_unit (per_cu, true, cu->language);
8039
8040 cu->per_cu->imported_symtabs_push (per_cu);
8041 }
8042 break;
8043 case DW_TAG_imported_declaration:
8044 add_partial_symbol (pdi, cu);
8045 break;
8046 default:
8047 break;
8048 }
8049 }
8050
8051 /* If the die has a sibling, skip to the sibling. */
8052
8053 pdi = pdi->die_sibling;
8054 }
8055 }
8056
8057 /* Functions used to compute the fully scoped name of a partial DIE.
8058
8059 Normally, this is simple. For C++, the parent DIE's fully scoped
8060 name is concatenated with "::" and the partial DIE's name.
8061 Enumerators are an exception; they use the scope of their parent
8062 enumeration type, i.e. the name of the enumeration type is not
8063 prepended to the enumerator.
8064
8065 There are two complexities. One is DW_AT_specification; in this
8066 case "parent" means the parent of the target of the specification,
8067 instead of the direct parent of the DIE. The other is compilers
8068 which do not emit DW_TAG_namespace; in this case we try to guess
8069 the fully qualified name of structure types from their members'
8070 linkage names. This must be done using the DIE's children rather
8071 than the children of any DW_AT_specification target. We only need
8072 to do this for structures at the top level, i.e. if the target of
8073 any DW_AT_specification (if any; otherwise the DIE itself) does not
8074 have a parent. */
8075
8076 /* Compute the scope prefix associated with PDI's parent, in
8077 compilation unit CU. The result will be allocated on CU's
8078 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8079 field. NULL is returned if no prefix is necessary. */
8080 static const char *
8081 partial_die_parent_scope (struct partial_die_info *pdi,
8082 struct dwarf2_cu *cu)
8083 {
8084 const char *grandparent_scope;
8085 struct partial_die_info *parent, *real_pdi;
8086
8087 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8088 then this means the parent of the specification DIE. */
8089
8090 real_pdi = pdi;
8091 while (real_pdi->has_specification)
8092 {
8093 auto res = find_partial_die (real_pdi->spec_offset,
8094 real_pdi->spec_is_dwz, cu);
8095 real_pdi = res.pdi;
8096 cu = res.cu;
8097 }
8098
8099 parent = real_pdi->die_parent;
8100 if (parent == NULL)
8101 return NULL;
8102
8103 if (parent->scope_set)
8104 return parent->scope;
8105
8106 parent->fixup (cu);
8107
8108 grandparent_scope = partial_die_parent_scope (parent, cu);
8109
8110 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8111 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8112 Work around this problem here. */
8113 if (cu->language == language_cplus
8114 && parent->tag == DW_TAG_namespace
8115 && strcmp (parent->name, "::") == 0
8116 && grandparent_scope == NULL)
8117 {
8118 parent->scope = NULL;
8119 parent->scope_set = 1;
8120 return NULL;
8121 }
8122
8123 /* Nested subroutines in Fortran get a prefix. */
8124 if (pdi->tag == DW_TAG_enumerator)
8125 /* Enumerators should not get the name of the enumeration as a prefix. */
8126 parent->scope = grandparent_scope;
8127 else if (parent->tag == DW_TAG_namespace
8128 || parent->tag == DW_TAG_module
8129 || parent->tag == DW_TAG_structure_type
8130 || parent->tag == DW_TAG_class_type
8131 || parent->tag == DW_TAG_interface_type
8132 || parent->tag == DW_TAG_union_type
8133 || parent->tag == DW_TAG_enumeration_type
8134 || (cu->language == language_fortran
8135 && parent->tag == DW_TAG_subprogram
8136 && pdi->tag == DW_TAG_subprogram))
8137 {
8138 if (grandparent_scope == NULL)
8139 parent->scope = parent->name;
8140 else
8141 parent->scope = typename_concat (&cu->comp_unit_obstack,
8142 grandparent_scope,
8143 parent->name, 0, cu);
8144 }
8145 else
8146 {
8147 /* FIXME drow/2004-04-01: What should we be doing with
8148 function-local names? For partial symbols, we should probably be
8149 ignoring them. */
8150 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8151 dwarf_tag_name (parent->tag),
8152 sect_offset_str (pdi->sect_off));
8153 parent->scope = grandparent_scope;
8154 }
8155
8156 parent->scope_set = 1;
8157 return parent->scope;
8158 }
8159
8160 /* Return the fully scoped name associated with PDI, from compilation unit
8161 CU. The result will be allocated with malloc. */
8162
8163 static gdb::unique_xmalloc_ptr<char>
8164 partial_die_full_name (struct partial_die_info *pdi,
8165 struct dwarf2_cu *cu)
8166 {
8167 const char *parent_scope;
8168
8169 /* If this is a template instantiation, we can not work out the
8170 template arguments from partial DIEs. So, unfortunately, we have
8171 to go through the full DIEs. At least any work we do building
8172 types here will be reused if full symbols are loaded later. */
8173 if (pdi->has_template_arguments)
8174 {
8175 pdi->fixup (cu);
8176
8177 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8178 {
8179 struct die_info *die;
8180 struct attribute attr;
8181 struct dwarf2_cu *ref_cu = cu;
8182
8183 /* DW_FORM_ref_addr is using section offset. */
8184 attr.name = (enum dwarf_attribute) 0;
8185 attr.form = DW_FORM_ref_addr;
8186 attr.u.unsnd = to_underlying (pdi->sect_off);
8187 die = follow_die_ref (NULL, &attr, &ref_cu);
8188
8189 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8190 }
8191 }
8192
8193 parent_scope = partial_die_parent_scope (pdi, cu);
8194 if (parent_scope == NULL)
8195 return NULL;
8196 else
8197 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8198 pdi->name, 0, cu));
8199 }
8200
8201 static void
8202 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8203 {
8204 struct dwarf2_per_objfile *dwarf2_per_objfile
8205 = cu->per_cu->dwarf2_per_objfile;
8206 struct objfile *objfile = dwarf2_per_objfile->objfile;
8207 struct gdbarch *gdbarch = objfile->arch ();
8208 CORE_ADDR addr = 0;
8209 const char *actual_name = NULL;
8210 CORE_ADDR baseaddr;
8211
8212 baseaddr = objfile->text_section_offset ();
8213
8214 gdb::unique_xmalloc_ptr<char> built_actual_name
8215 = partial_die_full_name (pdi, cu);
8216 if (built_actual_name != NULL)
8217 actual_name = built_actual_name.get ();
8218
8219 if (actual_name == NULL)
8220 actual_name = pdi->name;
8221
8222 partial_symbol psymbol;
8223 memset (&psymbol, 0, sizeof (psymbol));
8224 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8225 psymbol.ginfo.section = -1;
8226
8227 /* The code below indicates that the psymbol should be installed by
8228 setting this. */
8229 gdb::optional<psymbol_placement> where;
8230
8231 switch (pdi->tag)
8232 {
8233 case DW_TAG_inlined_subroutine:
8234 case DW_TAG_subprogram:
8235 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8236 - baseaddr);
8237 if (pdi->is_external
8238 || cu->language == language_ada
8239 || (cu->language == language_fortran
8240 && pdi->die_parent != NULL
8241 && pdi->die_parent->tag == DW_TAG_subprogram))
8242 {
8243 /* Normally, only "external" DIEs are part of the global scope.
8244 But in Ada and Fortran, we want to be able to access nested
8245 procedures globally. So all Ada and Fortran subprograms are
8246 stored in the global scope. */
8247 where = psymbol_placement::GLOBAL;
8248 }
8249 else
8250 where = psymbol_placement::STATIC;
8251
8252 psymbol.domain = VAR_DOMAIN;
8253 psymbol.aclass = LOC_BLOCK;
8254 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8255 psymbol.ginfo.value.address = addr;
8256
8257 if (pdi->main_subprogram && actual_name != NULL)
8258 set_objfile_main_name (objfile, actual_name, cu->language);
8259 break;
8260 case DW_TAG_constant:
8261 psymbol.domain = VAR_DOMAIN;
8262 psymbol.aclass = LOC_STATIC;
8263 where = (pdi->is_external
8264 ? psymbol_placement::GLOBAL
8265 : psymbol_placement::STATIC);
8266 break;
8267 case DW_TAG_variable:
8268 if (pdi->d.locdesc)
8269 addr = decode_locdesc (pdi->d.locdesc, cu);
8270
8271 if (pdi->d.locdesc
8272 && addr == 0
8273 && !dwarf2_per_objfile->has_section_at_zero)
8274 {
8275 /* A global or static variable may also have been stripped
8276 out by the linker if unused, in which case its address
8277 will be nullified; do not add such variables into partial
8278 symbol table then. */
8279 }
8280 else if (pdi->is_external)
8281 {
8282 /* Global Variable.
8283 Don't enter into the minimal symbol tables as there is
8284 a minimal symbol table entry from the ELF symbols already.
8285 Enter into partial symbol table if it has a location
8286 descriptor or a type.
8287 If the location descriptor is missing, new_symbol will create
8288 a LOC_UNRESOLVED symbol, the address of the variable will then
8289 be determined from the minimal symbol table whenever the variable
8290 is referenced.
8291 The address for the partial symbol table entry is not
8292 used by GDB, but it comes in handy for debugging partial symbol
8293 table building. */
8294
8295 if (pdi->d.locdesc || pdi->has_type)
8296 {
8297 psymbol.domain = VAR_DOMAIN;
8298 psymbol.aclass = LOC_STATIC;
8299 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8300 psymbol.ginfo.value.address = addr;
8301 where = psymbol_placement::GLOBAL;
8302 }
8303 }
8304 else
8305 {
8306 int has_loc = pdi->d.locdesc != NULL;
8307
8308 /* Static Variable. Skip symbols whose value we cannot know (those
8309 without location descriptors or constant values). */
8310 if (!has_loc && !pdi->has_const_value)
8311 return;
8312
8313 psymbol.domain = VAR_DOMAIN;
8314 psymbol.aclass = LOC_STATIC;
8315 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8316 if (has_loc)
8317 psymbol.ginfo.value.address = addr;
8318 where = psymbol_placement::STATIC;
8319 }
8320 break;
8321 case DW_TAG_typedef:
8322 case DW_TAG_base_type:
8323 case DW_TAG_subrange_type:
8324 psymbol.domain = VAR_DOMAIN;
8325 psymbol.aclass = LOC_TYPEDEF;
8326 where = psymbol_placement::STATIC;
8327 break;
8328 case DW_TAG_imported_declaration:
8329 case DW_TAG_namespace:
8330 psymbol.domain = VAR_DOMAIN;
8331 psymbol.aclass = LOC_TYPEDEF;
8332 where = psymbol_placement::GLOBAL;
8333 break;
8334 case DW_TAG_module:
8335 /* With Fortran 77 there might be a "BLOCK DATA" module
8336 available without any name. If so, we skip the module as it
8337 doesn't bring any value. */
8338 if (actual_name != nullptr)
8339 {
8340 psymbol.domain = MODULE_DOMAIN;
8341 psymbol.aclass = LOC_TYPEDEF;
8342 where = psymbol_placement::GLOBAL;
8343 }
8344 break;
8345 case DW_TAG_class_type:
8346 case DW_TAG_interface_type:
8347 case DW_TAG_structure_type:
8348 case DW_TAG_union_type:
8349 case DW_TAG_enumeration_type:
8350 /* Skip external references. The DWARF standard says in the section
8351 about "Structure, Union, and Class Type Entries": "An incomplete
8352 structure, union or class type is represented by a structure,
8353 union or class entry that does not have a byte size attribute
8354 and that has a DW_AT_declaration attribute." */
8355 if (!pdi->has_byte_size && pdi->is_declaration)
8356 return;
8357
8358 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8359 static vs. global. */
8360 psymbol.domain = STRUCT_DOMAIN;
8361 psymbol.aclass = LOC_TYPEDEF;
8362 where = (cu->language == language_cplus
8363 ? psymbol_placement::GLOBAL
8364 : psymbol_placement::STATIC);
8365 break;
8366 case DW_TAG_enumerator:
8367 psymbol.domain = VAR_DOMAIN;
8368 psymbol.aclass = LOC_CONST;
8369 where = (cu->language == language_cplus
8370 ? psymbol_placement::GLOBAL
8371 : psymbol_placement::STATIC);
8372 break;
8373 default:
8374 break;
8375 }
8376
8377 if (where.has_value ())
8378 {
8379 if (built_actual_name != nullptr)
8380 actual_name = objfile->intern (actual_name);
8381 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8382 psymbol.ginfo.set_linkage_name (actual_name);
8383 else
8384 {
8385 psymbol.ginfo.set_demangled_name (actual_name,
8386 &objfile->objfile_obstack);
8387 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8388 }
8389 add_psymbol_to_list (psymbol, *where, objfile);
8390 }
8391 }
8392
8393 /* Read a partial die corresponding to a namespace; also, add a symbol
8394 corresponding to that namespace to the symbol table. NAMESPACE is
8395 the name of the enclosing namespace. */
8396
8397 static void
8398 add_partial_namespace (struct partial_die_info *pdi,
8399 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8400 int set_addrmap, struct dwarf2_cu *cu)
8401 {
8402 /* Add a symbol for the namespace. */
8403
8404 add_partial_symbol (pdi, cu);
8405
8406 /* Now scan partial symbols in that namespace. */
8407
8408 if (pdi->has_children)
8409 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8410 }
8411
8412 /* Read a partial die corresponding to a Fortran module. */
8413
8414 static void
8415 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8416 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8417 {
8418 /* Add a symbol for the namespace. */
8419
8420 add_partial_symbol (pdi, cu);
8421
8422 /* Now scan partial symbols in that module. */
8423
8424 if (pdi->has_children)
8425 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8426 }
8427
8428 /* Read a partial die corresponding to a subprogram or an inlined
8429 subprogram and create a partial symbol for that subprogram.
8430 When the CU language allows it, this routine also defines a partial
8431 symbol for each nested subprogram that this subprogram contains.
8432 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8433 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8434
8435 PDI may also be a lexical block, in which case we simply search
8436 recursively for subprograms defined inside that lexical block.
8437 Again, this is only performed when the CU language allows this
8438 type of definitions. */
8439
8440 static void
8441 add_partial_subprogram (struct partial_die_info *pdi,
8442 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8443 int set_addrmap, struct dwarf2_cu *cu)
8444 {
8445 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8446 {
8447 if (pdi->has_pc_info)
8448 {
8449 if (pdi->lowpc < *lowpc)
8450 *lowpc = pdi->lowpc;
8451 if (pdi->highpc > *highpc)
8452 *highpc = pdi->highpc;
8453 if (set_addrmap)
8454 {
8455 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8456 struct gdbarch *gdbarch = objfile->arch ();
8457 CORE_ADDR baseaddr;
8458 CORE_ADDR this_highpc;
8459 CORE_ADDR this_lowpc;
8460
8461 baseaddr = objfile->text_section_offset ();
8462 this_lowpc
8463 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8464 pdi->lowpc + baseaddr)
8465 - baseaddr);
8466 this_highpc
8467 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8468 pdi->highpc + baseaddr)
8469 - baseaddr);
8470 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8471 this_lowpc, this_highpc - 1,
8472 cu->per_cu->v.psymtab);
8473 }
8474 }
8475
8476 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8477 {
8478 if (!pdi->is_declaration)
8479 /* Ignore subprogram DIEs that do not have a name, they are
8480 illegal. Do not emit a complaint at this point, we will
8481 do so when we convert this psymtab into a symtab. */
8482 if (pdi->name)
8483 add_partial_symbol (pdi, cu);
8484 }
8485 }
8486
8487 if (! pdi->has_children)
8488 return;
8489
8490 if (cu->language == language_ada || cu->language == language_fortran)
8491 {
8492 pdi = pdi->die_child;
8493 while (pdi != NULL)
8494 {
8495 pdi->fixup (cu);
8496 if (pdi->tag == DW_TAG_subprogram
8497 || pdi->tag == DW_TAG_inlined_subroutine
8498 || pdi->tag == DW_TAG_lexical_block)
8499 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8500 pdi = pdi->die_sibling;
8501 }
8502 }
8503 }
8504
8505 /* Read a partial die corresponding to an enumeration type. */
8506
8507 static void
8508 add_partial_enumeration (struct partial_die_info *enum_pdi,
8509 struct dwarf2_cu *cu)
8510 {
8511 struct partial_die_info *pdi;
8512
8513 if (enum_pdi->name != NULL)
8514 add_partial_symbol (enum_pdi, cu);
8515
8516 pdi = enum_pdi->die_child;
8517 while (pdi)
8518 {
8519 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8520 complaint (_("malformed enumerator DIE ignored"));
8521 else
8522 add_partial_symbol (pdi, cu);
8523 pdi = pdi->die_sibling;
8524 }
8525 }
8526
8527 /* Return the initial uleb128 in the die at INFO_PTR. */
8528
8529 static unsigned int
8530 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8531 {
8532 unsigned int bytes_read;
8533
8534 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8535 }
8536
8537 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8538 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8539
8540 Return the corresponding abbrev, or NULL if the number is zero (indicating
8541 an empty DIE). In either case *BYTES_READ will be set to the length of
8542 the initial number. */
8543
8544 static struct abbrev_info *
8545 peek_die_abbrev (const die_reader_specs &reader,
8546 const gdb_byte *info_ptr, unsigned int *bytes_read)
8547 {
8548 dwarf2_cu *cu = reader.cu;
8549 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8550 unsigned int abbrev_number
8551 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8552
8553 if (abbrev_number == 0)
8554 return NULL;
8555
8556 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8557 if (!abbrev)
8558 {
8559 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8560 " at offset %s [in module %s]"),
8561 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8562 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8563 }
8564
8565 return abbrev;
8566 }
8567
8568 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8569 Returns a pointer to the end of a series of DIEs, terminated by an empty
8570 DIE. Any children of the skipped DIEs will also be skipped. */
8571
8572 static const gdb_byte *
8573 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8574 {
8575 while (1)
8576 {
8577 unsigned int bytes_read;
8578 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8579
8580 if (abbrev == NULL)
8581 return info_ptr + bytes_read;
8582 else
8583 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8584 }
8585 }
8586
8587 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8588 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8589 abbrev corresponding to that skipped uleb128 should be passed in
8590 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8591 children. */
8592
8593 static const gdb_byte *
8594 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8595 struct abbrev_info *abbrev)
8596 {
8597 unsigned int bytes_read;
8598 struct attribute attr;
8599 bfd *abfd = reader->abfd;
8600 struct dwarf2_cu *cu = reader->cu;
8601 const gdb_byte *buffer = reader->buffer;
8602 const gdb_byte *buffer_end = reader->buffer_end;
8603 unsigned int form, i;
8604
8605 for (i = 0; i < abbrev->num_attrs; i++)
8606 {
8607 /* The only abbrev we care about is DW_AT_sibling. */
8608 if (abbrev->attrs[i].name == DW_AT_sibling)
8609 {
8610 bool ignored;
8611 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8612 &ignored);
8613 if (attr.form == DW_FORM_ref_addr)
8614 complaint (_("ignoring absolute DW_AT_sibling"));
8615 else
8616 {
8617 sect_offset off = attr.get_ref_die_offset ();
8618 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8619
8620 if (sibling_ptr < info_ptr)
8621 complaint (_("DW_AT_sibling points backwards"));
8622 else if (sibling_ptr > reader->buffer_end)
8623 reader->die_section->overflow_complaint ();
8624 else
8625 return sibling_ptr;
8626 }
8627 }
8628
8629 /* If it isn't DW_AT_sibling, skip this attribute. */
8630 form = abbrev->attrs[i].form;
8631 skip_attribute:
8632 switch (form)
8633 {
8634 case DW_FORM_ref_addr:
8635 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8636 and later it is offset sized. */
8637 if (cu->header.version == 2)
8638 info_ptr += cu->header.addr_size;
8639 else
8640 info_ptr += cu->header.offset_size;
8641 break;
8642 case DW_FORM_GNU_ref_alt:
8643 info_ptr += cu->header.offset_size;
8644 break;
8645 case DW_FORM_addr:
8646 info_ptr += cu->header.addr_size;
8647 break;
8648 case DW_FORM_data1:
8649 case DW_FORM_ref1:
8650 case DW_FORM_flag:
8651 case DW_FORM_strx1:
8652 info_ptr += 1;
8653 break;
8654 case DW_FORM_flag_present:
8655 case DW_FORM_implicit_const:
8656 break;
8657 case DW_FORM_data2:
8658 case DW_FORM_ref2:
8659 case DW_FORM_strx2:
8660 info_ptr += 2;
8661 break;
8662 case DW_FORM_strx3:
8663 info_ptr += 3;
8664 break;
8665 case DW_FORM_data4:
8666 case DW_FORM_ref4:
8667 case DW_FORM_strx4:
8668 info_ptr += 4;
8669 break;
8670 case DW_FORM_data8:
8671 case DW_FORM_ref8:
8672 case DW_FORM_ref_sig8:
8673 info_ptr += 8;
8674 break;
8675 case DW_FORM_data16:
8676 info_ptr += 16;
8677 break;
8678 case DW_FORM_string:
8679 read_direct_string (abfd, info_ptr, &bytes_read);
8680 info_ptr += bytes_read;
8681 break;
8682 case DW_FORM_sec_offset:
8683 case DW_FORM_strp:
8684 case DW_FORM_GNU_strp_alt:
8685 info_ptr += cu->header.offset_size;
8686 break;
8687 case DW_FORM_exprloc:
8688 case DW_FORM_block:
8689 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8690 info_ptr += bytes_read;
8691 break;
8692 case DW_FORM_block1:
8693 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8694 break;
8695 case DW_FORM_block2:
8696 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8697 break;
8698 case DW_FORM_block4:
8699 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8700 break;
8701 case DW_FORM_addrx:
8702 case DW_FORM_strx:
8703 case DW_FORM_sdata:
8704 case DW_FORM_udata:
8705 case DW_FORM_ref_udata:
8706 case DW_FORM_GNU_addr_index:
8707 case DW_FORM_GNU_str_index:
8708 case DW_FORM_rnglistx:
8709 case DW_FORM_loclistx:
8710 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8711 break;
8712 case DW_FORM_indirect:
8713 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8714 info_ptr += bytes_read;
8715 /* We need to continue parsing from here, so just go back to
8716 the top. */
8717 goto skip_attribute;
8718
8719 default:
8720 error (_("Dwarf Error: Cannot handle %s "
8721 "in DWARF reader [in module %s]"),
8722 dwarf_form_name (form),
8723 bfd_get_filename (abfd));
8724 }
8725 }
8726
8727 if (abbrev->has_children)
8728 return skip_children (reader, info_ptr);
8729 else
8730 return info_ptr;
8731 }
8732
8733 /* Locate ORIG_PDI's sibling.
8734 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8735
8736 static const gdb_byte *
8737 locate_pdi_sibling (const struct die_reader_specs *reader,
8738 struct partial_die_info *orig_pdi,
8739 const gdb_byte *info_ptr)
8740 {
8741 /* Do we know the sibling already? */
8742
8743 if (orig_pdi->sibling)
8744 return orig_pdi->sibling;
8745
8746 /* Are there any children to deal with? */
8747
8748 if (!orig_pdi->has_children)
8749 return info_ptr;
8750
8751 /* Skip the children the long way. */
8752
8753 return skip_children (reader, info_ptr);
8754 }
8755
8756 /* Expand this partial symbol table into a full symbol table. SELF is
8757 not NULL. */
8758
8759 void
8760 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8761 {
8762 struct dwarf2_per_objfile *dwarf2_per_objfile
8763 = get_dwarf2_per_objfile (objfile);
8764
8765 gdb_assert (!readin);
8766 /* If this psymtab is constructed from a debug-only objfile, the
8767 has_section_at_zero flag will not necessarily be correct. We
8768 can get the correct value for this flag by looking at the data
8769 associated with the (presumably stripped) associated objfile. */
8770 if (objfile->separate_debug_objfile_backlink)
8771 {
8772 struct dwarf2_per_objfile *dpo_backlink
8773 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8774
8775 dwarf2_per_objfile->has_section_at_zero
8776 = dpo_backlink->has_section_at_zero;
8777 }
8778
8779 expand_psymtab (objfile);
8780
8781 process_cu_includes (dwarf2_per_objfile);
8782 }
8783 \f
8784 /* Reading in full CUs. */
8785
8786 /* Add PER_CU to the queue. */
8787
8788 static void
8789 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8790 enum language pretend_language)
8791 {
8792 per_cu->queued = 1;
8793 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8794 }
8795
8796 /* If PER_CU is not yet queued, add it to the queue.
8797 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8798 dependency.
8799 The result is non-zero if PER_CU was queued, otherwise the result is zero
8800 meaning either PER_CU is already queued or it is already loaded.
8801
8802 N.B. There is an invariant here that if a CU is queued then it is loaded.
8803 The caller is required to load PER_CU if we return non-zero. */
8804
8805 static int
8806 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8807 struct dwarf2_per_cu_data *per_cu,
8808 enum language pretend_language)
8809 {
8810 /* We may arrive here during partial symbol reading, if we need full
8811 DIEs to process an unusual case (e.g. template arguments). Do
8812 not queue PER_CU, just tell our caller to load its DIEs. */
8813 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8814 {
8815 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8816 return 1;
8817 return 0;
8818 }
8819
8820 /* Mark the dependence relation so that we don't flush PER_CU
8821 too early. */
8822 if (dependent_cu != NULL)
8823 dwarf2_add_dependence (dependent_cu, per_cu);
8824
8825 /* If it's already on the queue, we have nothing to do. */
8826 if (per_cu->queued)
8827 return 0;
8828
8829 /* If the compilation unit is already loaded, just mark it as
8830 used. */
8831 if (per_cu->cu != NULL)
8832 {
8833 per_cu->cu->last_used = 0;
8834 return 0;
8835 }
8836
8837 /* Add it to the queue. */
8838 queue_comp_unit (per_cu, pretend_language);
8839
8840 return 1;
8841 }
8842
8843 /* Process the queue. */
8844
8845 static void
8846 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8847 {
8848 if (dwarf_read_debug)
8849 {
8850 fprintf_unfiltered (gdb_stdlog,
8851 "Expanding one or more symtabs of objfile %s ...\n",
8852 objfile_name (dwarf2_per_objfile->objfile));
8853 }
8854
8855 /* The queue starts out with one item, but following a DIE reference
8856 may load a new CU, adding it to the end of the queue. */
8857 while (!dwarf2_per_objfile->queue.empty ())
8858 {
8859 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8860
8861 if ((dwarf2_per_objfile->using_index
8862 ? !item.per_cu->v.quick->compunit_symtab
8863 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8864 /* Skip dummy CUs. */
8865 && item.per_cu->cu != NULL)
8866 {
8867 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8868 unsigned int debug_print_threshold;
8869 char buf[100];
8870
8871 if (per_cu->is_debug_types)
8872 {
8873 struct signatured_type *sig_type =
8874 (struct signatured_type *) per_cu;
8875
8876 sprintf (buf, "TU %s at offset %s",
8877 hex_string (sig_type->signature),
8878 sect_offset_str (per_cu->sect_off));
8879 /* There can be 100s of TUs.
8880 Only print them in verbose mode. */
8881 debug_print_threshold = 2;
8882 }
8883 else
8884 {
8885 sprintf (buf, "CU at offset %s",
8886 sect_offset_str (per_cu->sect_off));
8887 debug_print_threshold = 1;
8888 }
8889
8890 if (dwarf_read_debug >= debug_print_threshold)
8891 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8892
8893 if (per_cu->is_debug_types)
8894 process_full_type_unit (per_cu, item.pretend_language);
8895 else
8896 process_full_comp_unit (per_cu, item.pretend_language);
8897
8898 if (dwarf_read_debug >= debug_print_threshold)
8899 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8900 }
8901
8902 item.per_cu->queued = 0;
8903 dwarf2_per_objfile->queue.pop ();
8904 }
8905
8906 if (dwarf_read_debug)
8907 {
8908 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8909 objfile_name (dwarf2_per_objfile->objfile));
8910 }
8911 }
8912
8913 /* Read in full symbols for PST, and anything it depends on. */
8914
8915 void
8916 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8917 {
8918 gdb_assert (!readin);
8919
8920 expand_dependencies (objfile);
8921
8922 dw2_do_instantiate_symtab (per_cu_data, false);
8923 gdb_assert (get_compunit_symtab () != nullptr);
8924 }
8925
8926 /* Trivial hash function for die_info: the hash value of a DIE
8927 is its offset in .debug_info for this objfile. */
8928
8929 static hashval_t
8930 die_hash (const void *item)
8931 {
8932 const struct die_info *die = (const struct die_info *) item;
8933
8934 return to_underlying (die->sect_off);
8935 }
8936
8937 /* Trivial comparison function for die_info structures: two DIEs
8938 are equal if they have the same offset. */
8939
8940 static int
8941 die_eq (const void *item_lhs, const void *item_rhs)
8942 {
8943 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8944 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8945
8946 return die_lhs->sect_off == die_rhs->sect_off;
8947 }
8948
8949 /* Load the DIEs associated with PER_CU into memory. */
8950
8951 static void
8952 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8953 bool skip_partial,
8954 enum language pretend_language)
8955 {
8956 gdb_assert (! this_cu->is_debug_types);
8957
8958 cutu_reader reader (this_cu, NULL, 1, skip_partial);
8959 if (reader.dummy_p)
8960 return;
8961
8962 struct dwarf2_cu *cu = reader.cu;
8963 const gdb_byte *info_ptr = reader.info_ptr;
8964
8965 gdb_assert (cu->die_hash == NULL);
8966 cu->die_hash =
8967 htab_create_alloc_ex (cu->header.length / 12,
8968 die_hash,
8969 die_eq,
8970 NULL,
8971 &cu->comp_unit_obstack,
8972 hashtab_obstack_allocate,
8973 dummy_obstack_deallocate);
8974
8975 if (reader.comp_unit_die->has_children)
8976 reader.comp_unit_die->child
8977 = read_die_and_siblings (&reader, reader.info_ptr,
8978 &info_ptr, reader.comp_unit_die);
8979 cu->dies = reader.comp_unit_die;
8980 /* comp_unit_die is not stored in die_hash, no need. */
8981
8982 /* We try not to read any attributes in this function, because not
8983 all CUs needed for references have been loaded yet, and symbol
8984 table processing isn't initialized. But we have to set the CU language,
8985 or we won't be able to build types correctly.
8986 Similarly, if we do not read the producer, we can not apply
8987 producer-specific interpretation. */
8988 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8989
8990 reader.keep ();
8991 }
8992
8993 /* Add a DIE to the delayed physname list. */
8994
8995 static void
8996 add_to_method_list (struct type *type, int fnfield_index, int index,
8997 const char *name, struct die_info *die,
8998 struct dwarf2_cu *cu)
8999 {
9000 struct delayed_method_info mi;
9001 mi.type = type;
9002 mi.fnfield_index = fnfield_index;
9003 mi.index = index;
9004 mi.name = name;
9005 mi.die = die;
9006 cu->method_list.push_back (mi);
9007 }
9008
9009 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9010 "const" / "volatile". If so, decrements LEN by the length of the
9011 modifier and return true. Otherwise return false. */
9012
9013 template<size_t N>
9014 static bool
9015 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9016 {
9017 size_t mod_len = sizeof (mod) - 1;
9018 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9019 {
9020 len -= mod_len;
9021 return true;
9022 }
9023 return false;
9024 }
9025
9026 /* Compute the physnames of any methods on the CU's method list.
9027
9028 The computation of method physnames is delayed in order to avoid the
9029 (bad) condition that one of the method's formal parameters is of an as yet
9030 incomplete type. */
9031
9032 static void
9033 compute_delayed_physnames (struct dwarf2_cu *cu)
9034 {
9035 /* Only C++ delays computing physnames. */
9036 if (cu->method_list.empty ())
9037 return;
9038 gdb_assert (cu->language == language_cplus);
9039
9040 for (const delayed_method_info &mi : cu->method_list)
9041 {
9042 const char *physname;
9043 struct fn_fieldlist *fn_flp
9044 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9045 physname = dwarf2_physname (mi.name, mi.die, cu);
9046 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9047 = physname ? physname : "";
9048
9049 /* Since there's no tag to indicate whether a method is a
9050 const/volatile overload, extract that information out of the
9051 demangled name. */
9052 if (physname != NULL)
9053 {
9054 size_t len = strlen (physname);
9055
9056 while (1)
9057 {
9058 if (physname[len] == ')') /* shortcut */
9059 break;
9060 else if (check_modifier (physname, len, " const"))
9061 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9062 else if (check_modifier (physname, len, " volatile"))
9063 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9064 else
9065 break;
9066 }
9067 }
9068 }
9069
9070 /* The list is no longer needed. */
9071 cu->method_list.clear ();
9072 }
9073
9074 /* Go objects should be embedded in a DW_TAG_module DIE,
9075 and it's not clear if/how imported objects will appear.
9076 To keep Go support simple until that's worked out,
9077 go back through what we've read and create something usable.
9078 We could do this while processing each DIE, and feels kinda cleaner,
9079 but that way is more invasive.
9080 This is to, for example, allow the user to type "p var" or "b main"
9081 without having to specify the package name, and allow lookups
9082 of module.object to work in contexts that use the expression
9083 parser. */
9084
9085 static void
9086 fixup_go_packaging (struct dwarf2_cu *cu)
9087 {
9088 gdb::unique_xmalloc_ptr<char> package_name;
9089 struct pending *list;
9090 int i;
9091
9092 for (list = *cu->get_builder ()->get_global_symbols ();
9093 list != NULL;
9094 list = list->next)
9095 {
9096 for (i = 0; i < list->nsyms; ++i)
9097 {
9098 struct symbol *sym = list->symbol[i];
9099
9100 if (sym->language () == language_go
9101 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9102 {
9103 gdb::unique_xmalloc_ptr<char> this_package_name
9104 (go_symbol_package_name (sym));
9105
9106 if (this_package_name == NULL)
9107 continue;
9108 if (package_name == NULL)
9109 package_name = std::move (this_package_name);
9110 else
9111 {
9112 struct objfile *objfile
9113 = cu->per_cu->dwarf2_per_objfile->objfile;
9114 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9115 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9116 (symbol_symtab (sym) != NULL
9117 ? symtab_to_filename_for_display
9118 (symbol_symtab (sym))
9119 : objfile_name (objfile)),
9120 this_package_name.get (), package_name.get ());
9121 }
9122 }
9123 }
9124 }
9125
9126 if (package_name != NULL)
9127 {
9128 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9129 const char *saved_package_name = objfile->intern (package_name.get ());
9130 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9131 saved_package_name);
9132 struct symbol *sym;
9133
9134 sym = allocate_symbol (objfile);
9135 sym->set_language (language_go, &objfile->objfile_obstack);
9136 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9137 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9138 e.g., "main" finds the "main" module and not C's main(). */
9139 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9140 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9141 SYMBOL_TYPE (sym) = type;
9142
9143 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9144 }
9145 }
9146
9147 /* Allocate a fully-qualified name consisting of the two parts on the
9148 obstack. */
9149
9150 static const char *
9151 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9152 {
9153 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9154 }
9155
9156 /* A helper that allocates a variant part to attach to a Rust enum
9157 type. OBSTACK is where the results should be allocated. TYPE is
9158 the type we're processing. DISCRIMINANT_INDEX is the index of the
9159 discriminant. It must be the index of one of the fields of TYPE.
9160 DEFAULT_INDEX is the index of the default field; or -1 if there is
9161 no default. RANGES is indexed by "effective" field number (the
9162 field index, but omitting the discriminant and default fields) and
9163 must hold the discriminant values used by the variants. Note that
9164 RANGES must have a lifetime at least as long as OBSTACK -- either
9165 already allocated on it, or static. */
9166
9167 static void
9168 alloc_rust_variant (struct obstack *obstack, struct type *type,
9169 int discriminant_index, int default_index,
9170 gdb::array_view<discriminant_range> ranges)
9171 {
9172 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9173 must be handled by the caller. */
9174 gdb_assert (discriminant_index >= 0
9175 && discriminant_index < TYPE_NFIELDS (type));
9176 gdb_assert (default_index == -1
9177 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9178
9179 /* We have one variant for each non-discriminant field. */
9180 int n_variants = TYPE_NFIELDS (type) - 1;
9181
9182 variant *variants = new (obstack) variant[n_variants];
9183 int var_idx = 0;
9184 int range_idx = 0;
9185 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9186 {
9187 if (i == discriminant_index)
9188 continue;
9189
9190 variants[var_idx].first_field = i;
9191 variants[var_idx].last_field = i + 1;
9192
9193 /* The default field does not need a range, but other fields do.
9194 We skipped the discriminant above. */
9195 if (i != default_index)
9196 {
9197 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9198 ++range_idx;
9199 }
9200
9201 ++var_idx;
9202 }
9203
9204 gdb_assert (range_idx == ranges.size ());
9205 gdb_assert (var_idx == n_variants);
9206
9207 variant_part *part = new (obstack) variant_part;
9208 part->discriminant_index = discriminant_index;
9209 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9210 discriminant_index));
9211 part->variants = gdb::array_view<variant> (variants, n_variants);
9212
9213 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9214 gdb::array_view<variant_part> *prop_value
9215 = new (storage) gdb::array_view<variant_part> (part, 1);
9216
9217 struct dynamic_prop prop;
9218 prop.kind = PROP_VARIANT_PARTS;
9219 prop.data.variant_parts = prop_value;
9220
9221 add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop, type);
9222 }
9223
9224 /* Some versions of rustc emitted enums in an unusual way.
9225
9226 Ordinary enums were emitted as unions. The first element of each
9227 structure in the union was named "RUST$ENUM$DISR". This element
9228 held the discriminant.
9229
9230 These versions of Rust also implemented the "non-zero"
9231 optimization. When the enum had two values, and one is empty and
9232 the other holds a pointer that cannot be zero, the pointer is used
9233 as the discriminant, with a zero value meaning the empty variant.
9234 Here, the union's first member is of the form
9235 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9236 where the fieldnos are the indices of the fields that should be
9237 traversed in order to find the field (which may be several fields deep)
9238 and the variantname is the name of the variant of the case when the
9239 field is zero.
9240
9241 This function recognizes whether TYPE is of one of these forms,
9242 and, if so, smashes it to be a variant type. */
9243
9244 static void
9245 quirk_rust_enum (struct type *type, struct objfile *objfile)
9246 {
9247 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9248
9249 /* We don't need to deal with empty enums. */
9250 if (TYPE_NFIELDS (type) == 0)
9251 return;
9252
9253 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9254 if (TYPE_NFIELDS (type) == 1
9255 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9256 {
9257 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9258
9259 /* Decode the field name to find the offset of the
9260 discriminant. */
9261 ULONGEST bit_offset = 0;
9262 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9263 while (name[0] >= '0' && name[0] <= '9')
9264 {
9265 char *tail;
9266 unsigned long index = strtoul (name, &tail, 10);
9267 name = tail;
9268 if (*name != '$'
9269 || index >= TYPE_NFIELDS (field_type)
9270 || (TYPE_FIELD_LOC_KIND (field_type, index)
9271 != FIELD_LOC_KIND_BITPOS))
9272 {
9273 complaint (_("Could not parse Rust enum encoding string \"%s\""
9274 "[in module %s]"),
9275 TYPE_FIELD_NAME (type, 0),
9276 objfile_name (objfile));
9277 return;
9278 }
9279 ++name;
9280
9281 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9282 field_type = TYPE_FIELD_TYPE (field_type, index);
9283 }
9284
9285 /* Smash this type to be a structure type. We have to do this
9286 because the type has already been recorded. */
9287 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9288 TYPE_NFIELDS (type) = 3;
9289 /* Save the field we care about. */
9290 struct field saved_field = TYPE_FIELD (type, 0);
9291 TYPE_FIELDS (type)
9292 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9293
9294 /* Put the discriminant at index 0. */
9295 TYPE_FIELD_TYPE (type, 0) = field_type;
9296 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9297 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9298 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), bit_offset);
9299
9300 /* The order of fields doesn't really matter, so put the real
9301 field at index 1 and the data-less field at index 2. */
9302 TYPE_FIELD (type, 1) = saved_field;
9303 TYPE_FIELD_NAME (type, 1)
9304 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type, 1)));
9305 TYPE_NAME (TYPE_FIELD_TYPE (type, 1))
9306 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9307 TYPE_FIELD_NAME (type, 1));
9308
9309 const char *dataless_name
9310 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9311 name);
9312 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9313 dataless_name);
9314 TYPE_FIELD_TYPE (type, 2) = dataless_type;
9315 /* NAME points into the original discriminant name, which
9316 already has the correct lifetime. */
9317 TYPE_FIELD_NAME (type, 2) = name;
9318 SET_FIELD_BITPOS (TYPE_FIELD (type, 2), 0);
9319
9320 /* Indicate that this is a variant type. */
9321 static discriminant_range ranges[1] = { { 0, 0 } };
9322 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9323 }
9324 /* A union with a single anonymous field is probably an old-style
9325 univariant enum. */
9326 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9327 {
9328 /* Smash this type to be a structure type. We have to do this
9329 because the type has already been recorded. */
9330 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9331
9332 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9333 const char *variant_name
9334 = rust_last_path_segment (TYPE_NAME (field_type));
9335 TYPE_FIELD_NAME (type, 0) = variant_name;
9336 TYPE_NAME (field_type)
9337 = rust_fully_qualify (&objfile->objfile_obstack,
9338 TYPE_NAME (type), variant_name);
9339 }
9340 else
9341 {
9342 struct type *disr_type = nullptr;
9343 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9344 {
9345 disr_type = TYPE_FIELD_TYPE (type, i);
9346
9347 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9348 {
9349 /* All fields of a true enum will be structs. */
9350 return;
9351 }
9352 else if (TYPE_NFIELDS (disr_type) == 0)
9353 {
9354 /* Could be data-less variant, so keep going. */
9355 disr_type = nullptr;
9356 }
9357 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9358 "RUST$ENUM$DISR") != 0)
9359 {
9360 /* Not a Rust enum. */
9361 return;
9362 }
9363 else
9364 {
9365 /* Found one. */
9366 break;
9367 }
9368 }
9369
9370 /* If we got here without a discriminant, then it's probably
9371 just a union. */
9372 if (disr_type == nullptr)
9373 return;
9374
9375 /* Smash this type to be a structure type. We have to do this
9376 because the type has already been recorded. */
9377 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9378
9379 /* Make space for the discriminant field. */
9380 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9381 field *new_fields
9382 = (struct field *) TYPE_ZALLOC (type, (TYPE_NFIELDS (type)
9383 * sizeof (struct field)));
9384 memcpy (new_fields + 1, TYPE_FIELDS (type),
9385 TYPE_NFIELDS (type) * sizeof (struct field));
9386 TYPE_FIELDS (type) = new_fields;
9387 TYPE_NFIELDS (type) = TYPE_NFIELDS (type) + 1;
9388
9389 /* Install the discriminant at index 0 in the union. */
9390 TYPE_FIELD (type, 0) = *disr_field;
9391 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9392 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9393
9394 /* We need a way to find the correct discriminant given a
9395 variant name. For convenience we build a map here. */
9396 struct type *enum_type = FIELD_TYPE (*disr_field);
9397 std::unordered_map<std::string, ULONGEST> discriminant_map;
9398 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9399 {
9400 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9401 {
9402 const char *name
9403 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9404 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9405 }
9406 }
9407
9408 int n_fields = TYPE_NFIELDS (type);
9409 /* We don't need a range entry for the discriminant, but we do
9410 need one for every other field, as there is no default
9411 variant. */
9412 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9413 discriminant_range,
9414 n_fields - 1);
9415 /* Skip the discriminant here. */
9416 for (int i = 1; i < n_fields; ++i)
9417 {
9418 /* Find the final word in the name of this variant's type.
9419 That name can be used to look up the correct
9420 discriminant. */
9421 const char *variant_name
9422 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type, i)));
9423
9424 auto iter = discriminant_map.find (variant_name);
9425 if (iter != discriminant_map.end ())
9426 {
9427 ranges[i].low = iter->second;
9428 ranges[i].high = iter->second;
9429 }
9430
9431 /* Remove the discriminant field, if it exists. */
9432 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
9433 if (TYPE_NFIELDS (sub_type) > 0)
9434 {
9435 --TYPE_NFIELDS (sub_type);
9436 ++TYPE_FIELDS (sub_type);
9437 }
9438 TYPE_FIELD_NAME (type, i) = variant_name;
9439 TYPE_NAME (sub_type)
9440 = rust_fully_qualify (&objfile->objfile_obstack,
9441 TYPE_NAME (type), variant_name);
9442 }
9443
9444 /* Indicate that this is a variant type. */
9445 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9446 gdb::array_view<discriminant_range> (ranges,
9447 n_fields - 1));
9448 }
9449 }
9450
9451 /* Rewrite some Rust unions to be structures with variants parts. */
9452
9453 static void
9454 rust_union_quirks (struct dwarf2_cu *cu)
9455 {
9456 gdb_assert (cu->language == language_rust);
9457 for (type *type_ : cu->rust_unions)
9458 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9459 /* We don't need this any more. */
9460 cu->rust_unions.clear ();
9461 }
9462
9463 /* Return the symtab for PER_CU. This works properly regardless of
9464 whether we're using the index or psymtabs. */
9465
9466 static struct compunit_symtab *
9467 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9468 {
9469 return (per_cu->dwarf2_per_objfile->using_index
9470 ? per_cu->v.quick->compunit_symtab
9471 : per_cu->v.psymtab->compunit_symtab);
9472 }
9473
9474 /* A helper function for computing the list of all symbol tables
9475 included by PER_CU. */
9476
9477 static void
9478 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9479 htab_t all_children, htab_t all_type_symtabs,
9480 struct dwarf2_per_cu_data *per_cu,
9481 struct compunit_symtab *immediate_parent)
9482 {
9483 void **slot;
9484 struct compunit_symtab *cust;
9485
9486 slot = htab_find_slot (all_children, per_cu, INSERT);
9487 if (*slot != NULL)
9488 {
9489 /* This inclusion and its children have been processed. */
9490 return;
9491 }
9492
9493 *slot = per_cu;
9494 /* Only add a CU if it has a symbol table. */
9495 cust = get_compunit_symtab (per_cu);
9496 if (cust != NULL)
9497 {
9498 /* If this is a type unit only add its symbol table if we haven't
9499 seen it yet (type unit per_cu's can share symtabs). */
9500 if (per_cu->is_debug_types)
9501 {
9502 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9503 if (*slot == NULL)
9504 {
9505 *slot = cust;
9506 result->push_back (cust);
9507 if (cust->user == NULL)
9508 cust->user = immediate_parent;
9509 }
9510 }
9511 else
9512 {
9513 result->push_back (cust);
9514 if (cust->user == NULL)
9515 cust->user = immediate_parent;
9516 }
9517 }
9518
9519 if (!per_cu->imported_symtabs_empty ())
9520 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9521 {
9522 recursively_compute_inclusions (result, all_children,
9523 all_type_symtabs, ptr, cust);
9524 }
9525 }
9526
9527 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9528 PER_CU. */
9529
9530 static void
9531 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9532 {
9533 gdb_assert (! per_cu->is_debug_types);
9534
9535 if (!per_cu->imported_symtabs_empty ())
9536 {
9537 int len;
9538 std::vector<compunit_symtab *> result_symtabs;
9539 htab_t all_children, all_type_symtabs;
9540 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9541
9542 /* If we don't have a symtab, we can just skip this case. */
9543 if (cust == NULL)
9544 return;
9545
9546 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9547 NULL, xcalloc, xfree);
9548 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9549 NULL, xcalloc, xfree);
9550
9551 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9552 {
9553 recursively_compute_inclusions (&result_symtabs, all_children,
9554 all_type_symtabs, ptr, cust);
9555 }
9556
9557 /* Now we have a transitive closure of all the included symtabs. */
9558 len = result_symtabs.size ();
9559 cust->includes
9560 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9561 struct compunit_symtab *, len + 1);
9562 memcpy (cust->includes, result_symtabs.data (),
9563 len * sizeof (compunit_symtab *));
9564 cust->includes[len] = NULL;
9565
9566 htab_delete (all_children);
9567 htab_delete (all_type_symtabs);
9568 }
9569 }
9570
9571 /* Compute the 'includes' field for the symtabs of all the CUs we just
9572 read. */
9573
9574 static void
9575 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9576 {
9577 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9578 {
9579 if (! iter->is_debug_types)
9580 compute_compunit_symtab_includes (iter);
9581 }
9582
9583 dwarf2_per_objfile->just_read_cus.clear ();
9584 }
9585
9586 /* Generate full symbol information for PER_CU, whose DIEs have
9587 already been loaded into memory. */
9588
9589 static void
9590 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9591 enum language pretend_language)
9592 {
9593 struct dwarf2_cu *cu = per_cu->cu;
9594 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9595 struct objfile *objfile = dwarf2_per_objfile->objfile;
9596 struct gdbarch *gdbarch = objfile->arch ();
9597 CORE_ADDR lowpc, highpc;
9598 struct compunit_symtab *cust;
9599 CORE_ADDR baseaddr;
9600 struct block *static_block;
9601 CORE_ADDR addr;
9602
9603 baseaddr = objfile->text_section_offset ();
9604
9605 /* Clear the list here in case something was left over. */
9606 cu->method_list.clear ();
9607
9608 cu->language = pretend_language;
9609 cu->language_defn = language_def (cu->language);
9610
9611 /* Do line number decoding in read_file_scope () */
9612 process_die (cu->dies, cu);
9613
9614 /* For now fudge the Go package. */
9615 if (cu->language == language_go)
9616 fixup_go_packaging (cu);
9617
9618 /* Now that we have processed all the DIEs in the CU, all the types
9619 should be complete, and it should now be safe to compute all of the
9620 physnames. */
9621 compute_delayed_physnames (cu);
9622
9623 if (cu->language == language_rust)
9624 rust_union_quirks (cu);
9625
9626 /* Some compilers don't define a DW_AT_high_pc attribute for the
9627 compilation unit. If the DW_AT_high_pc is missing, synthesize
9628 it, by scanning the DIE's below the compilation unit. */
9629 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9630
9631 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9632 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9633
9634 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9635 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9636 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9637 addrmap to help ensure it has an accurate map of pc values belonging to
9638 this comp unit. */
9639 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9640
9641 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9642 SECT_OFF_TEXT (objfile),
9643 0);
9644
9645 if (cust != NULL)
9646 {
9647 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9648
9649 /* Set symtab language to language from DW_AT_language. If the
9650 compilation is from a C file generated by language preprocessors, do
9651 not set the language if it was already deduced by start_subfile. */
9652 if (!(cu->language == language_c
9653 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9654 COMPUNIT_FILETABS (cust)->language = cu->language;
9655
9656 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9657 produce DW_AT_location with location lists but it can be possibly
9658 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9659 there were bugs in prologue debug info, fixed later in GCC-4.5
9660 by "unwind info for epilogues" patch (which is not directly related).
9661
9662 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9663 needed, it would be wrong due to missing DW_AT_producer there.
9664
9665 Still one can confuse GDB by using non-standard GCC compilation
9666 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9667 */
9668 if (cu->has_loclist && gcc_4_minor >= 5)
9669 cust->locations_valid = 1;
9670
9671 if (gcc_4_minor >= 5)
9672 cust->epilogue_unwind_valid = 1;
9673
9674 cust->call_site_htab = cu->call_site_htab;
9675 }
9676
9677 if (dwarf2_per_objfile->using_index)
9678 per_cu->v.quick->compunit_symtab = cust;
9679 else
9680 {
9681 dwarf2_psymtab *pst = per_cu->v.psymtab;
9682 pst->compunit_symtab = cust;
9683 pst->readin = true;
9684 }
9685
9686 /* Push it for inclusion processing later. */
9687 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9688
9689 /* Not needed any more. */
9690 cu->reset_builder ();
9691 }
9692
9693 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9694 already been loaded into memory. */
9695
9696 static void
9697 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9698 enum language pretend_language)
9699 {
9700 struct dwarf2_cu *cu = per_cu->cu;
9701 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9702 struct objfile *objfile = dwarf2_per_objfile->objfile;
9703 struct compunit_symtab *cust;
9704 struct signatured_type *sig_type;
9705
9706 gdb_assert (per_cu->is_debug_types);
9707 sig_type = (struct signatured_type *) per_cu;
9708
9709 /* Clear the list here in case something was left over. */
9710 cu->method_list.clear ();
9711
9712 cu->language = pretend_language;
9713 cu->language_defn = language_def (cu->language);
9714
9715 /* The symbol tables are set up in read_type_unit_scope. */
9716 process_die (cu->dies, cu);
9717
9718 /* For now fudge the Go package. */
9719 if (cu->language == language_go)
9720 fixup_go_packaging (cu);
9721
9722 /* Now that we have processed all the DIEs in the CU, all the types
9723 should be complete, and it should now be safe to compute all of the
9724 physnames. */
9725 compute_delayed_physnames (cu);
9726
9727 if (cu->language == language_rust)
9728 rust_union_quirks (cu);
9729
9730 /* TUs share symbol tables.
9731 If this is the first TU to use this symtab, complete the construction
9732 of it with end_expandable_symtab. Otherwise, complete the addition of
9733 this TU's symbols to the existing symtab. */
9734 if (sig_type->type_unit_group->compunit_symtab == NULL)
9735 {
9736 buildsym_compunit *builder = cu->get_builder ();
9737 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9738 sig_type->type_unit_group->compunit_symtab = cust;
9739
9740 if (cust != NULL)
9741 {
9742 /* Set symtab language to language from DW_AT_language. If the
9743 compilation is from a C file generated by language preprocessors,
9744 do not set the language if it was already deduced by
9745 start_subfile. */
9746 if (!(cu->language == language_c
9747 && COMPUNIT_FILETABS (cust)->language != language_c))
9748 COMPUNIT_FILETABS (cust)->language = cu->language;
9749 }
9750 }
9751 else
9752 {
9753 cu->get_builder ()->augment_type_symtab ();
9754 cust = sig_type->type_unit_group->compunit_symtab;
9755 }
9756
9757 if (dwarf2_per_objfile->using_index)
9758 per_cu->v.quick->compunit_symtab = cust;
9759 else
9760 {
9761 dwarf2_psymtab *pst = per_cu->v.psymtab;
9762 pst->compunit_symtab = cust;
9763 pst->readin = true;
9764 }
9765
9766 /* Not needed any more. */
9767 cu->reset_builder ();
9768 }
9769
9770 /* Process an imported unit DIE. */
9771
9772 static void
9773 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9774 {
9775 struct attribute *attr;
9776
9777 /* For now we don't handle imported units in type units. */
9778 if (cu->per_cu->is_debug_types)
9779 {
9780 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9781 " supported in type units [in module %s]"),
9782 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9783 }
9784
9785 attr = dwarf2_attr (die, DW_AT_import, cu);
9786 if (attr != NULL)
9787 {
9788 sect_offset sect_off = attr->get_ref_die_offset ();
9789 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9790 dwarf2_per_cu_data *per_cu
9791 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9792 cu->per_cu->dwarf2_per_objfile);
9793
9794 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9795 into another compilation unit, at root level. Regard this as a hint,
9796 and ignore it. */
9797 if (die->parent && die->parent->parent == NULL
9798 && per_cu->unit_type == DW_UT_compile
9799 && per_cu->lang == language_cplus)
9800 return;
9801
9802 /* If necessary, add it to the queue and load its DIEs. */
9803 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9804 load_full_comp_unit (per_cu, false, cu->language);
9805
9806 cu->per_cu->imported_symtabs_push (per_cu);
9807 }
9808 }
9809
9810 /* RAII object that represents a process_die scope: i.e.,
9811 starts/finishes processing a DIE. */
9812 class process_die_scope
9813 {
9814 public:
9815 process_die_scope (die_info *die, dwarf2_cu *cu)
9816 : m_die (die), m_cu (cu)
9817 {
9818 /* We should only be processing DIEs not already in process. */
9819 gdb_assert (!m_die->in_process);
9820 m_die->in_process = true;
9821 }
9822
9823 ~process_die_scope ()
9824 {
9825 m_die->in_process = false;
9826
9827 /* If we're done processing the DIE for the CU that owns the line
9828 header, we don't need the line header anymore. */
9829 if (m_cu->line_header_die_owner == m_die)
9830 {
9831 delete m_cu->line_header;
9832 m_cu->line_header = NULL;
9833 m_cu->line_header_die_owner = NULL;
9834 }
9835 }
9836
9837 private:
9838 die_info *m_die;
9839 dwarf2_cu *m_cu;
9840 };
9841
9842 /* Process a die and its children. */
9843
9844 static void
9845 process_die (struct die_info *die, struct dwarf2_cu *cu)
9846 {
9847 process_die_scope scope (die, cu);
9848
9849 switch (die->tag)
9850 {
9851 case DW_TAG_padding:
9852 break;
9853 case DW_TAG_compile_unit:
9854 case DW_TAG_partial_unit:
9855 read_file_scope (die, cu);
9856 break;
9857 case DW_TAG_type_unit:
9858 read_type_unit_scope (die, cu);
9859 break;
9860 case DW_TAG_subprogram:
9861 /* Nested subprograms in Fortran get a prefix. */
9862 if (cu->language == language_fortran
9863 && die->parent != NULL
9864 && die->parent->tag == DW_TAG_subprogram)
9865 cu->processing_has_namespace_info = true;
9866 /* Fall through. */
9867 case DW_TAG_inlined_subroutine:
9868 read_func_scope (die, cu);
9869 break;
9870 case DW_TAG_lexical_block:
9871 case DW_TAG_try_block:
9872 case DW_TAG_catch_block:
9873 read_lexical_block_scope (die, cu);
9874 break;
9875 case DW_TAG_call_site:
9876 case DW_TAG_GNU_call_site:
9877 read_call_site_scope (die, cu);
9878 break;
9879 case DW_TAG_class_type:
9880 case DW_TAG_interface_type:
9881 case DW_TAG_structure_type:
9882 case DW_TAG_union_type:
9883 process_structure_scope (die, cu);
9884 break;
9885 case DW_TAG_enumeration_type:
9886 process_enumeration_scope (die, cu);
9887 break;
9888
9889 /* These dies have a type, but processing them does not create
9890 a symbol or recurse to process the children. Therefore we can
9891 read them on-demand through read_type_die. */
9892 case DW_TAG_subroutine_type:
9893 case DW_TAG_set_type:
9894 case DW_TAG_array_type:
9895 case DW_TAG_pointer_type:
9896 case DW_TAG_ptr_to_member_type:
9897 case DW_TAG_reference_type:
9898 case DW_TAG_rvalue_reference_type:
9899 case DW_TAG_string_type:
9900 break;
9901
9902 case DW_TAG_base_type:
9903 case DW_TAG_subrange_type:
9904 case DW_TAG_typedef:
9905 /* Add a typedef symbol for the type definition, if it has a
9906 DW_AT_name. */
9907 new_symbol (die, read_type_die (die, cu), cu);
9908 break;
9909 case DW_TAG_common_block:
9910 read_common_block (die, cu);
9911 break;
9912 case DW_TAG_common_inclusion:
9913 break;
9914 case DW_TAG_namespace:
9915 cu->processing_has_namespace_info = true;
9916 read_namespace (die, cu);
9917 break;
9918 case DW_TAG_module:
9919 cu->processing_has_namespace_info = true;
9920 read_module (die, cu);
9921 break;
9922 case DW_TAG_imported_declaration:
9923 cu->processing_has_namespace_info = true;
9924 if (read_namespace_alias (die, cu))
9925 break;
9926 /* The declaration is not a global namespace alias. */
9927 /* Fall through. */
9928 case DW_TAG_imported_module:
9929 cu->processing_has_namespace_info = true;
9930 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9931 || cu->language != language_fortran))
9932 complaint (_("Tag '%s' has unexpected children"),
9933 dwarf_tag_name (die->tag));
9934 read_import_statement (die, cu);
9935 break;
9936
9937 case DW_TAG_imported_unit:
9938 process_imported_unit_die (die, cu);
9939 break;
9940
9941 case DW_TAG_variable:
9942 read_variable (die, cu);
9943 break;
9944
9945 default:
9946 new_symbol (die, NULL, cu);
9947 break;
9948 }
9949 }
9950 \f
9951 /* DWARF name computation. */
9952
9953 /* A helper function for dwarf2_compute_name which determines whether DIE
9954 needs to have the name of the scope prepended to the name listed in the
9955 die. */
9956
9957 static int
9958 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9959 {
9960 struct attribute *attr;
9961
9962 switch (die->tag)
9963 {
9964 case DW_TAG_namespace:
9965 case DW_TAG_typedef:
9966 case DW_TAG_class_type:
9967 case DW_TAG_interface_type:
9968 case DW_TAG_structure_type:
9969 case DW_TAG_union_type:
9970 case DW_TAG_enumeration_type:
9971 case DW_TAG_enumerator:
9972 case DW_TAG_subprogram:
9973 case DW_TAG_inlined_subroutine:
9974 case DW_TAG_member:
9975 case DW_TAG_imported_declaration:
9976 return 1;
9977
9978 case DW_TAG_variable:
9979 case DW_TAG_constant:
9980 /* We only need to prefix "globally" visible variables. These include
9981 any variable marked with DW_AT_external or any variable that
9982 lives in a namespace. [Variables in anonymous namespaces
9983 require prefixing, but they are not DW_AT_external.] */
9984
9985 if (dwarf2_attr (die, DW_AT_specification, cu))
9986 {
9987 struct dwarf2_cu *spec_cu = cu;
9988
9989 return die_needs_namespace (die_specification (die, &spec_cu),
9990 spec_cu);
9991 }
9992
9993 attr = dwarf2_attr (die, DW_AT_external, cu);
9994 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9995 && die->parent->tag != DW_TAG_module)
9996 return 0;
9997 /* A variable in a lexical block of some kind does not need a
9998 namespace, even though in C++ such variables may be external
9999 and have a mangled name. */
10000 if (die->parent->tag == DW_TAG_lexical_block
10001 || die->parent->tag == DW_TAG_try_block
10002 || die->parent->tag == DW_TAG_catch_block
10003 || die->parent->tag == DW_TAG_subprogram)
10004 return 0;
10005 return 1;
10006
10007 default:
10008 return 0;
10009 }
10010 }
10011
10012 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10013 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10014 defined for the given DIE. */
10015
10016 static struct attribute *
10017 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10018 {
10019 struct attribute *attr;
10020
10021 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10022 if (attr == NULL)
10023 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10024
10025 return attr;
10026 }
10027
10028 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10029 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10030 defined for the given DIE. */
10031
10032 static const char *
10033 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10034 {
10035 const char *linkage_name;
10036
10037 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10038 if (linkage_name == NULL)
10039 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10040
10041 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10042 See https://github.com/rust-lang/rust/issues/32925. */
10043 if (cu->language == language_rust && linkage_name != NULL
10044 && strchr (linkage_name, '{') != NULL)
10045 linkage_name = NULL;
10046
10047 return linkage_name;
10048 }
10049
10050 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10051 compute the physname for the object, which include a method's:
10052 - formal parameters (C++),
10053 - receiver type (Go),
10054
10055 The term "physname" is a bit confusing.
10056 For C++, for example, it is the demangled name.
10057 For Go, for example, it's the mangled name.
10058
10059 For Ada, return the DIE's linkage name rather than the fully qualified
10060 name. PHYSNAME is ignored..
10061
10062 The result is allocated on the objfile_obstack and canonicalized. */
10063
10064 static const char *
10065 dwarf2_compute_name (const char *name,
10066 struct die_info *die, struct dwarf2_cu *cu,
10067 int physname)
10068 {
10069 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10070
10071 if (name == NULL)
10072 name = dwarf2_name (die, cu);
10073
10074 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10075 but otherwise compute it by typename_concat inside GDB.
10076 FIXME: Actually this is not really true, or at least not always true.
10077 It's all very confusing. compute_and_set_names doesn't try to demangle
10078 Fortran names because there is no mangling standard. So new_symbol
10079 will set the demangled name to the result of dwarf2_full_name, and it is
10080 the demangled name that GDB uses if it exists. */
10081 if (cu->language == language_ada
10082 || (cu->language == language_fortran && physname))
10083 {
10084 /* For Ada unit, we prefer the linkage name over the name, as
10085 the former contains the exported name, which the user expects
10086 to be able to reference. Ideally, we want the user to be able
10087 to reference this entity using either natural or linkage name,
10088 but we haven't started looking at this enhancement yet. */
10089 const char *linkage_name = dw2_linkage_name (die, cu);
10090
10091 if (linkage_name != NULL)
10092 return linkage_name;
10093 }
10094
10095 /* These are the only languages we know how to qualify names in. */
10096 if (name != NULL
10097 && (cu->language == language_cplus
10098 || cu->language == language_fortran || cu->language == language_d
10099 || cu->language == language_rust))
10100 {
10101 if (die_needs_namespace (die, cu))
10102 {
10103 const char *prefix;
10104 const char *canonical_name = NULL;
10105
10106 string_file buf;
10107
10108 prefix = determine_prefix (die, cu);
10109 if (*prefix != '\0')
10110 {
10111 gdb::unique_xmalloc_ptr<char> prefixed_name
10112 (typename_concat (NULL, prefix, name, physname, cu));
10113
10114 buf.puts (prefixed_name.get ());
10115 }
10116 else
10117 buf.puts (name);
10118
10119 /* Template parameters may be specified in the DIE's DW_AT_name, or
10120 as children with DW_TAG_template_type_param or
10121 DW_TAG_value_type_param. If the latter, add them to the name
10122 here. If the name already has template parameters, then
10123 skip this step; some versions of GCC emit both, and
10124 it is more efficient to use the pre-computed name.
10125
10126 Something to keep in mind about this process: it is very
10127 unlikely, or in some cases downright impossible, to produce
10128 something that will match the mangled name of a function.
10129 If the definition of the function has the same debug info,
10130 we should be able to match up with it anyway. But fallbacks
10131 using the minimal symbol, for instance to find a method
10132 implemented in a stripped copy of libstdc++, will not work.
10133 If we do not have debug info for the definition, we will have to
10134 match them up some other way.
10135
10136 When we do name matching there is a related problem with function
10137 templates; two instantiated function templates are allowed to
10138 differ only by their return types, which we do not add here. */
10139
10140 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10141 {
10142 struct attribute *attr;
10143 struct die_info *child;
10144 int first = 1;
10145
10146 die->building_fullname = 1;
10147
10148 for (child = die->child; child != NULL; child = child->sibling)
10149 {
10150 struct type *type;
10151 LONGEST value;
10152 const gdb_byte *bytes;
10153 struct dwarf2_locexpr_baton *baton;
10154 struct value *v;
10155
10156 if (child->tag != DW_TAG_template_type_param
10157 && child->tag != DW_TAG_template_value_param)
10158 continue;
10159
10160 if (first)
10161 {
10162 buf.puts ("<");
10163 first = 0;
10164 }
10165 else
10166 buf.puts (", ");
10167
10168 attr = dwarf2_attr (child, DW_AT_type, cu);
10169 if (attr == NULL)
10170 {
10171 complaint (_("template parameter missing DW_AT_type"));
10172 buf.puts ("UNKNOWN_TYPE");
10173 continue;
10174 }
10175 type = die_type (child, cu);
10176
10177 if (child->tag == DW_TAG_template_type_param)
10178 {
10179 c_print_type (type, "", &buf, -1, 0, cu->language,
10180 &type_print_raw_options);
10181 continue;
10182 }
10183
10184 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10185 if (attr == NULL)
10186 {
10187 complaint (_("template parameter missing "
10188 "DW_AT_const_value"));
10189 buf.puts ("UNKNOWN_VALUE");
10190 continue;
10191 }
10192
10193 dwarf2_const_value_attr (attr, type, name,
10194 &cu->comp_unit_obstack, cu,
10195 &value, &bytes, &baton);
10196
10197 if (TYPE_NOSIGN (type))
10198 /* GDB prints characters as NUMBER 'CHAR'. If that's
10199 changed, this can use value_print instead. */
10200 c_printchar (value, type, &buf);
10201 else
10202 {
10203 struct value_print_options opts;
10204
10205 if (baton != NULL)
10206 v = dwarf2_evaluate_loc_desc (type, NULL,
10207 baton->data,
10208 baton->size,
10209 baton->per_cu);
10210 else if (bytes != NULL)
10211 {
10212 v = allocate_value (type);
10213 memcpy (value_contents_writeable (v), bytes,
10214 TYPE_LENGTH (type));
10215 }
10216 else
10217 v = value_from_longest (type, value);
10218
10219 /* Specify decimal so that we do not depend on
10220 the radix. */
10221 get_formatted_print_options (&opts, 'd');
10222 opts.raw = 1;
10223 value_print (v, &buf, &opts);
10224 release_value (v);
10225 }
10226 }
10227
10228 die->building_fullname = 0;
10229
10230 if (!first)
10231 {
10232 /* Close the argument list, with a space if necessary
10233 (nested templates). */
10234 if (!buf.empty () && buf.string ().back () == '>')
10235 buf.puts (" >");
10236 else
10237 buf.puts (">");
10238 }
10239 }
10240
10241 /* For C++ methods, append formal parameter type
10242 information, if PHYSNAME. */
10243
10244 if (physname && die->tag == DW_TAG_subprogram
10245 && cu->language == language_cplus)
10246 {
10247 struct type *type = read_type_die (die, cu);
10248
10249 c_type_print_args (type, &buf, 1, cu->language,
10250 &type_print_raw_options);
10251
10252 if (cu->language == language_cplus)
10253 {
10254 /* Assume that an artificial first parameter is
10255 "this", but do not crash if it is not. RealView
10256 marks unnamed (and thus unused) parameters as
10257 artificial; there is no way to differentiate
10258 the two cases. */
10259 if (TYPE_NFIELDS (type) > 0
10260 && TYPE_FIELD_ARTIFICIAL (type, 0)
10261 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10262 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10263 0))))
10264 buf.puts (" const");
10265 }
10266 }
10267
10268 const std::string &intermediate_name = buf.string ();
10269
10270 if (cu->language == language_cplus)
10271 canonical_name
10272 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10273 objfile);
10274
10275 /* If we only computed INTERMEDIATE_NAME, or if
10276 INTERMEDIATE_NAME is already canonical, then we need to
10277 intern it. */
10278 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10279 name = objfile->intern (intermediate_name);
10280 else
10281 name = canonical_name;
10282 }
10283 }
10284
10285 return name;
10286 }
10287
10288 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10289 If scope qualifiers are appropriate they will be added. The result
10290 will be allocated on the storage_obstack, or NULL if the DIE does
10291 not have a name. NAME may either be from a previous call to
10292 dwarf2_name or NULL.
10293
10294 The output string will be canonicalized (if C++). */
10295
10296 static const char *
10297 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10298 {
10299 return dwarf2_compute_name (name, die, cu, 0);
10300 }
10301
10302 /* Construct a physname for the given DIE in CU. NAME may either be
10303 from a previous call to dwarf2_name or NULL. The result will be
10304 allocated on the objfile_objstack or NULL if the DIE does not have a
10305 name.
10306
10307 The output string will be canonicalized (if C++). */
10308
10309 static const char *
10310 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10311 {
10312 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10313 const char *retval, *mangled = NULL, *canon = NULL;
10314 int need_copy = 1;
10315
10316 /* In this case dwarf2_compute_name is just a shortcut not building anything
10317 on its own. */
10318 if (!die_needs_namespace (die, cu))
10319 return dwarf2_compute_name (name, die, cu, 1);
10320
10321 mangled = dw2_linkage_name (die, cu);
10322
10323 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10324 has computed. */
10325 gdb::unique_xmalloc_ptr<char> demangled;
10326 if (mangled != NULL)
10327 {
10328
10329 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10330 {
10331 /* Do nothing (do not demangle the symbol name). */
10332 }
10333 else if (cu->language == language_go)
10334 {
10335 /* This is a lie, but we already lie to the caller new_symbol.
10336 new_symbol assumes we return the mangled name.
10337 This just undoes that lie until things are cleaned up. */
10338 }
10339 else
10340 {
10341 /* Use DMGL_RET_DROP for C++ template functions to suppress
10342 their return type. It is easier for GDB users to search
10343 for such functions as `name(params)' than `long name(params)'.
10344 In such case the minimal symbol names do not match the full
10345 symbol names but for template functions there is never a need
10346 to look up their definition from their declaration so
10347 the only disadvantage remains the minimal symbol variant
10348 `long name(params)' does not have the proper inferior type. */
10349 demangled.reset (gdb_demangle (mangled,
10350 (DMGL_PARAMS | DMGL_ANSI
10351 | DMGL_RET_DROP)));
10352 }
10353 if (demangled)
10354 canon = demangled.get ();
10355 else
10356 {
10357 canon = mangled;
10358 need_copy = 0;
10359 }
10360 }
10361
10362 if (canon == NULL || check_physname)
10363 {
10364 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10365
10366 if (canon != NULL && strcmp (physname, canon) != 0)
10367 {
10368 /* It may not mean a bug in GDB. The compiler could also
10369 compute DW_AT_linkage_name incorrectly. But in such case
10370 GDB would need to be bug-to-bug compatible. */
10371
10372 complaint (_("Computed physname <%s> does not match demangled <%s> "
10373 "(from linkage <%s>) - DIE at %s [in module %s]"),
10374 physname, canon, mangled, sect_offset_str (die->sect_off),
10375 objfile_name (objfile));
10376
10377 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10378 is available here - over computed PHYSNAME. It is safer
10379 against both buggy GDB and buggy compilers. */
10380
10381 retval = canon;
10382 }
10383 else
10384 {
10385 retval = physname;
10386 need_copy = 0;
10387 }
10388 }
10389 else
10390 retval = canon;
10391
10392 if (need_copy)
10393 retval = objfile->intern (retval);
10394
10395 return retval;
10396 }
10397
10398 /* Inspect DIE in CU for a namespace alias. If one exists, record
10399 a new symbol for it.
10400
10401 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10402
10403 static int
10404 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10405 {
10406 struct attribute *attr;
10407
10408 /* If the die does not have a name, this is not a namespace
10409 alias. */
10410 attr = dwarf2_attr (die, DW_AT_name, cu);
10411 if (attr != NULL)
10412 {
10413 int num;
10414 struct die_info *d = die;
10415 struct dwarf2_cu *imported_cu = cu;
10416
10417 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10418 keep inspecting DIEs until we hit the underlying import. */
10419 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10420 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10421 {
10422 attr = dwarf2_attr (d, DW_AT_import, cu);
10423 if (attr == NULL)
10424 break;
10425
10426 d = follow_die_ref (d, attr, &imported_cu);
10427 if (d->tag != DW_TAG_imported_declaration)
10428 break;
10429 }
10430
10431 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10432 {
10433 complaint (_("DIE at %s has too many recursively imported "
10434 "declarations"), sect_offset_str (d->sect_off));
10435 return 0;
10436 }
10437
10438 if (attr != NULL)
10439 {
10440 struct type *type;
10441 sect_offset sect_off = attr->get_ref_die_offset ();
10442
10443 type = get_die_type_at_offset (sect_off, cu->per_cu);
10444 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10445 {
10446 /* This declaration is a global namespace alias. Add
10447 a symbol for it whose type is the aliased namespace. */
10448 new_symbol (die, type, cu);
10449 return 1;
10450 }
10451 }
10452 }
10453
10454 return 0;
10455 }
10456
10457 /* Return the using directives repository (global or local?) to use in the
10458 current context for CU.
10459
10460 For Ada, imported declarations can materialize renamings, which *may* be
10461 global. However it is impossible (for now?) in DWARF to distinguish
10462 "external" imported declarations and "static" ones. As all imported
10463 declarations seem to be static in all other languages, make them all CU-wide
10464 global only in Ada. */
10465
10466 static struct using_direct **
10467 using_directives (struct dwarf2_cu *cu)
10468 {
10469 if (cu->language == language_ada
10470 && cu->get_builder ()->outermost_context_p ())
10471 return cu->get_builder ()->get_global_using_directives ();
10472 else
10473 return cu->get_builder ()->get_local_using_directives ();
10474 }
10475
10476 /* Read the import statement specified by the given die and record it. */
10477
10478 static void
10479 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10480 {
10481 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10482 struct attribute *import_attr;
10483 struct die_info *imported_die, *child_die;
10484 struct dwarf2_cu *imported_cu;
10485 const char *imported_name;
10486 const char *imported_name_prefix;
10487 const char *canonical_name;
10488 const char *import_alias;
10489 const char *imported_declaration = NULL;
10490 const char *import_prefix;
10491 std::vector<const char *> excludes;
10492
10493 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10494 if (import_attr == NULL)
10495 {
10496 complaint (_("Tag '%s' has no DW_AT_import"),
10497 dwarf_tag_name (die->tag));
10498 return;
10499 }
10500
10501 imported_cu = cu;
10502 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10503 imported_name = dwarf2_name (imported_die, imported_cu);
10504 if (imported_name == NULL)
10505 {
10506 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10507
10508 The import in the following code:
10509 namespace A
10510 {
10511 typedef int B;
10512 }
10513
10514 int main ()
10515 {
10516 using A::B;
10517 B b;
10518 return b;
10519 }
10520
10521 ...
10522 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10523 <52> DW_AT_decl_file : 1
10524 <53> DW_AT_decl_line : 6
10525 <54> DW_AT_import : <0x75>
10526 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10527 <59> DW_AT_name : B
10528 <5b> DW_AT_decl_file : 1
10529 <5c> DW_AT_decl_line : 2
10530 <5d> DW_AT_type : <0x6e>
10531 ...
10532 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10533 <76> DW_AT_byte_size : 4
10534 <77> DW_AT_encoding : 5 (signed)
10535
10536 imports the wrong die ( 0x75 instead of 0x58 ).
10537 This case will be ignored until the gcc bug is fixed. */
10538 return;
10539 }
10540
10541 /* Figure out the local name after import. */
10542 import_alias = dwarf2_name (die, cu);
10543
10544 /* Figure out where the statement is being imported to. */
10545 import_prefix = determine_prefix (die, cu);
10546
10547 /* Figure out what the scope of the imported die is and prepend it
10548 to the name of the imported die. */
10549 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10550
10551 if (imported_die->tag != DW_TAG_namespace
10552 && imported_die->tag != DW_TAG_module)
10553 {
10554 imported_declaration = imported_name;
10555 canonical_name = imported_name_prefix;
10556 }
10557 else if (strlen (imported_name_prefix) > 0)
10558 canonical_name = obconcat (&objfile->objfile_obstack,
10559 imported_name_prefix,
10560 (cu->language == language_d ? "." : "::"),
10561 imported_name, (char *) NULL);
10562 else
10563 canonical_name = imported_name;
10564
10565 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10566 for (child_die = die->child; child_die && child_die->tag;
10567 child_die = child_die->sibling)
10568 {
10569 /* DWARF-4: A Fortran use statement with a “rename list” may be
10570 represented by an imported module entry with an import attribute
10571 referring to the module and owned entries corresponding to those
10572 entities that are renamed as part of being imported. */
10573
10574 if (child_die->tag != DW_TAG_imported_declaration)
10575 {
10576 complaint (_("child DW_TAG_imported_declaration expected "
10577 "- DIE at %s [in module %s]"),
10578 sect_offset_str (child_die->sect_off),
10579 objfile_name (objfile));
10580 continue;
10581 }
10582
10583 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10584 if (import_attr == NULL)
10585 {
10586 complaint (_("Tag '%s' has no DW_AT_import"),
10587 dwarf_tag_name (child_die->tag));
10588 continue;
10589 }
10590
10591 imported_cu = cu;
10592 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10593 &imported_cu);
10594 imported_name = dwarf2_name (imported_die, imported_cu);
10595 if (imported_name == NULL)
10596 {
10597 complaint (_("child DW_TAG_imported_declaration has unknown "
10598 "imported name - DIE at %s [in module %s]"),
10599 sect_offset_str (child_die->sect_off),
10600 objfile_name (objfile));
10601 continue;
10602 }
10603
10604 excludes.push_back (imported_name);
10605
10606 process_die (child_die, cu);
10607 }
10608
10609 add_using_directive (using_directives (cu),
10610 import_prefix,
10611 canonical_name,
10612 import_alias,
10613 imported_declaration,
10614 excludes,
10615 0,
10616 &objfile->objfile_obstack);
10617 }
10618
10619 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10620 types, but gives them a size of zero. Starting with version 14,
10621 ICC is compatible with GCC. */
10622
10623 static bool
10624 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10625 {
10626 if (!cu->checked_producer)
10627 check_producer (cu);
10628
10629 return cu->producer_is_icc_lt_14;
10630 }
10631
10632 /* ICC generates a DW_AT_type for C void functions. This was observed on
10633 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10634 which says that void functions should not have a DW_AT_type. */
10635
10636 static bool
10637 producer_is_icc (struct dwarf2_cu *cu)
10638 {
10639 if (!cu->checked_producer)
10640 check_producer (cu);
10641
10642 return cu->producer_is_icc;
10643 }
10644
10645 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10646 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10647 this, it was first present in GCC release 4.3.0. */
10648
10649 static bool
10650 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10651 {
10652 if (!cu->checked_producer)
10653 check_producer (cu);
10654
10655 return cu->producer_is_gcc_lt_4_3;
10656 }
10657
10658 static file_and_directory
10659 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10660 {
10661 file_and_directory res;
10662
10663 /* Find the filename. Do not use dwarf2_name here, since the filename
10664 is not a source language identifier. */
10665 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10666 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10667
10668 if (res.comp_dir == NULL
10669 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10670 && IS_ABSOLUTE_PATH (res.name))
10671 {
10672 res.comp_dir_storage = ldirname (res.name);
10673 if (!res.comp_dir_storage.empty ())
10674 res.comp_dir = res.comp_dir_storage.c_str ();
10675 }
10676 if (res.comp_dir != NULL)
10677 {
10678 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10679 directory, get rid of it. */
10680 const char *cp = strchr (res.comp_dir, ':');
10681
10682 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10683 res.comp_dir = cp + 1;
10684 }
10685
10686 if (res.name == NULL)
10687 res.name = "<unknown>";
10688
10689 return res;
10690 }
10691
10692 /* Handle DW_AT_stmt_list for a compilation unit.
10693 DIE is the DW_TAG_compile_unit die for CU.
10694 COMP_DIR is the compilation directory. LOWPC is passed to
10695 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10696
10697 static void
10698 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10699 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10700 {
10701 struct dwarf2_per_objfile *dwarf2_per_objfile
10702 = cu->per_cu->dwarf2_per_objfile;
10703 struct attribute *attr;
10704 struct line_header line_header_local;
10705 hashval_t line_header_local_hash;
10706 void **slot;
10707 int decode_mapping;
10708
10709 gdb_assert (! cu->per_cu->is_debug_types);
10710
10711 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10712 if (attr == NULL)
10713 return;
10714
10715 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10716
10717 /* The line header hash table is only created if needed (it exists to
10718 prevent redundant reading of the line table for partial_units).
10719 If we're given a partial_unit, we'll need it. If we're given a
10720 compile_unit, then use the line header hash table if it's already
10721 created, but don't create one just yet. */
10722
10723 if (dwarf2_per_objfile->line_header_hash == NULL
10724 && die->tag == DW_TAG_partial_unit)
10725 {
10726 dwarf2_per_objfile->line_header_hash
10727 .reset (htab_create_alloc (127, line_header_hash_voidp,
10728 line_header_eq_voidp,
10729 free_line_header_voidp,
10730 xcalloc, xfree));
10731 }
10732
10733 line_header_local.sect_off = line_offset;
10734 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10735 line_header_local_hash = line_header_hash (&line_header_local);
10736 if (dwarf2_per_objfile->line_header_hash != NULL)
10737 {
10738 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10739 &line_header_local,
10740 line_header_local_hash, NO_INSERT);
10741
10742 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10743 is not present in *SLOT (since if there is something in *SLOT then
10744 it will be for a partial_unit). */
10745 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10746 {
10747 gdb_assert (*slot != NULL);
10748 cu->line_header = (struct line_header *) *slot;
10749 return;
10750 }
10751 }
10752
10753 /* dwarf_decode_line_header does not yet provide sufficient information.
10754 We always have to call also dwarf_decode_lines for it. */
10755 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10756 if (lh == NULL)
10757 return;
10758
10759 cu->line_header = lh.release ();
10760 cu->line_header_die_owner = die;
10761
10762 if (dwarf2_per_objfile->line_header_hash == NULL)
10763 slot = NULL;
10764 else
10765 {
10766 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10767 &line_header_local,
10768 line_header_local_hash, INSERT);
10769 gdb_assert (slot != NULL);
10770 }
10771 if (slot != NULL && *slot == NULL)
10772 {
10773 /* This newly decoded line number information unit will be owned
10774 by line_header_hash hash table. */
10775 *slot = cu->line_header;
10776 cu->line_header_die_owner = NULL;
10777 }
10778 else
10779 {
10780 /* We cannot free any current entry in (*slot) as that struct line_header
10781 may be already used by multiple CUs. Create only temporary decoded
10782 line_header for this CU - it may happen at most once for each line
10783 number information unit. And if we're not using line_header_hash
10784 then this is what we want as well. */
10785 gdb_assert (die->tag != DW_TAG_partial_unit);
10786 }
10787 decode_mapping = (die->tag != DW_TAG_partial_unit);
10788 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10789 decode_mapping);
10790
10791 }
10792
10793 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10794
10795 static void
10796 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10797 {
10798 struct dwarf2_per_objfile *dwarf2_per_objfile
10799 = cu->per_cu->dwarf2_per_objfile;
10800 struct objfile *objfile = dwarf2_per_objfile->objfile;
10801 struct gdbarch *gdbarch = objfile->arch ();
10802 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10803 CORE_ADDR highpc = ((CORE_ADDR) 0);
10804 struct attribute *attr;
10805 struct die_info *child_die;
10806 CORE_ADDR baseaddr;
10807
10808 prepare_one_comp_unit (cu, die, cu->language);
10809 baseaddr = objfile->text_section_offset ();
10810
10811 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10812
10813 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10814 from finish_block. */
10815 if (lowpc == ((CORE_ADDR) -1))
10816 lowpc = highpc;
10817 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10818
10819 file_and_directory fnd = find_file_and_directory (die, cu);
10820
10821 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10822 standardised yet. As a workaround for the language detection we fall
10823 back to the DW_AT_producer string. */
10824 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10825 cu->language = language_opencl;
10826
10827 /* Similar hack for Go. */
10828 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10829 set_cu_language (DW_LANG_Go, cu);
10830
10831 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10832
10833 /* Decode line number information if present. We do this before
10834 processing child DIEs, so that the line header table is available
10835 for DW_AT_decl_file. */
10836 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10837
10838 /* Process all dies in compilation unit. */
10839 if (die->child != NULL)
10840 {
10841 child_die = die->child;
10842 while (child_die && child_die->tag)
10843 {
10844 process_die (child_die, cu);
10845 child_die = child_die->sibling;
10846 }
10847 }
10848
10849 /* Decode macro information, if present. Dwarf 2 macro information
10850 refers to information in the line number info statement program
10851 header, so we can only read it if we've read the header
10852 successfully. */
10853 attr = dwarf2_attr (die, DW_AT_macros, cu);
10854 if (attr == NULL)
10855 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10856 if (attr && cu->line_header)
10857 {
10858 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10859 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10860
10861 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10862 }
10863 else
10864 {
10865 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10866 if (attr && cu->line_header)
10867 {
10868 unsigned int macro_offset = DW_UNSND (attr);
10869
10870 dwarf_decode_macros (cu, macro_offset, 0);
10871 }
10872 }
10873 }
10874
10875 void
10876 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10877 {
10878 struct type_unit_group *tu_group;
10879 int first_time;
10880 struct attribute *attr;
10881 unsigned int i;
10882 struct signatured_type *sig_type;
10883
10884 gdb_assert (per_cu->is_debug_types);
10885 sig_type = (struct signatured_type *) per_cu;
10886
10887 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10888
10889 /* If we're using .gdb_index (includes -readnow) then
10890 per_cu->type_unit_group may not have been set up yet. */
10891 if (sig_type->type_unit_group == NULL)
10892 sig_type->type_unit_group = get_type_unit_group (this, attr);
10893 tu_group = sig_type->type_unit_group;
10894
10895 /* If we've already processed this stmt_list there's no real need to
10896 do it again, we could fake it and just recreate the part we need
10897 (file name,index -> symtab mapping). If data shows this optimization
10898 is useful we can do it then. */
10899 first_time = tu_group->compunit_symtab == NULL;
10900
10901 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10902 debug info. */
10903 line_header_up lh;
10904 if (attr != NULL)
10905 {
10906 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10907 lh = dwarf_decode_line_header (line_offset, this);
10908 }
10909 if (lh == NULL)
10910 {
10911 if (first_time)
10912 start_symtab ("", NULL, 0);
10913 else
10914 {
10915 gdb_assert (tu_group->symtabs == NULL);
10916 gdb_assert (m_builder == nullptr);
10917 struct compunit_symtab *cust = tu_group->compunit_symtab;
10918 m_builder.reset (new struct buildsym_compunit
10919 (COMPUNIT_OBJFILE (cust), "",
10920 COMPUNIT_DIRNAME (cust),
10921 compunit_language (cust),
10922 0, cust));
10923 }
10924 return;
10925 }
10926
10927 line_header = lh.release ();
10928 line_header_die_owner = die;
10929
10930 if (first_time)
10931 {
10932 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10933
10934 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10935 still initializing it, and our caller (a few levels up)
10936 process_full_type_unit still needs to know if this is the first
10937 time. */
10938
10939 tu_group->symtabs
10940 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10941 struct symtab *, line_header->file_names_size ());
10942
10943 auto &file_names = line_header->file_names ();
10944 for (i = 0; i < file_names.size (); ++i)
10945 {
10946 file_entry &fe = file_names[i];
10947 dwarf2_start_subfile (this, fe.name,
10948 fe.include_dir (line_header));
10949 buildsym_compunit *b = get_builder ();
10950 if (b->get_current_subfile ()->symtab == NULL)
10951 {
10952 /* NOTE: start_subfile will recognize when it's been
10953 passed a file it has already seen. So we can't
10954 assume there's a simple mapping from
10955 cu->line_header->file_names to subfiles, plus
10956 cu->line_header->file_names may contain dups. */
10957 b->get_current_subfile ()->symtab
10958 = allocate_symtab (cust, b->get_current_subfile ()->name);
10959 }
10960
10961 fe.symtab = b->get_current_subfile ()->symtab;
10962 tu_group->symtabs[i] = fe.symtab;
10963 }
10964 }
10965 else
10966 {
10967 gdb_assert (m_builder == nullptr);
10968 struct compunit_symtab *cust = tu_group->compunit_symtab;
10969 m_builder.reset (new struct buildsym_compunit
10970 (COMPUNIT_OBJFILE (cust), "",
10971 COMPUNIT_DIRNAME (cust),
10972 compunit_language (cust),
10973 0, cust));
10974
10975 auto &file_names = line_header->file_names ();
10976 for (i = 0; i < file_names.size (); ++i)
10977 {
10978 file_entry &fe = file_names[i];
10979 fe.symtab = tu_group->symtabs[i];
10980 }
10981 }
10982
10983 /* The main symtab is allocated last. Type units don't have DW_AT_name
10984 so they don't have a "real" (so to speak) symtab anyway.
10985 There is later code that will assign the main symtab to all symbols
10986 that don't have one. We need to handle the case of a symbol with a
10987 missing symtab (DW_AT_decl_file) anyway. */
10988 }
10989
10990 /* Process DW_TAG_type_unit.
10991 For TUs we want to skip the first top level sibling if it's not the
10992 actual type being defined by this TU. In this case the first top
10993 level sibling is there to provide context only. */
10994
10995 static void
10996 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10997 {
10998 struct die_info *child_die;
10999
11000 prepare_one_comp_unit (cu, die, language_minimal);
11001
11002 /* Initialize (or reinitialize) the machinery for building symtabs.
11003 We do this before processing child DIEs, so that the line header table
11004 is available for DW_AT_decl_file. */
11005 cu->setup_type_unit_groups (die);
11006
11007 if (die->child != NULL)
11008 {
11009 child_die = die->child;
11010 while (child_die && child_die->tag)
11011 {
11012 process_die (child_die, cu);
11013 child_die = child_die->sibling;
11014 }
11015 }
11016 }
11017 \f
11018 /* DWO/DWP files.
11019
11020 http://gcc.gnu.org/wiki/DebugFission
11021 http://gcc.gnu.org/wiki/DebugFissionDWP
11022
11023 To simplify handling of both DWO files ("object" files with the DWARF info)
11024 and DWP files (a file with the DWOs packaged up into one file), we treat
11025 DWP files as having a collection of virtual DWO files. */
11026
11027 static hashval_t
11028 hash_dwo_file (const void *item)
11029 {
11030 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11031 hashval_t hash;
11032
11033 hash = htab_hash_string (dwo_file->dwo_name);
11034 if (dwo_file->comp_dir != NULL)
11035 hash += htab_hash_string (dwo_file->comp_dir);
11036 return hash;
11037 }
11038
11039 static int
11040 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11041 {
11042 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11043 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11044
11045 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11046 return 0;
11047 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11048 return lhs->comp_dir == rhs->comp_dir;
11049 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11050 }
11051
11052 /* Allocate a hash table for DWO files. */
11053
11054 static htab_up
11055 allocate_dwo_file_hash_table ()
11056 {
11057 auto delete_dwo_file = [] (void *item)
11058 {
11059 struct dwo_file *dwo_file = (struct dwo_file *) item;
11060
11061 delete dwo_file;
11062 };
11063
11064 return htab_up (htab_create_alloc (41,
11065 hash_dwo_file,
11066 eq_dwo_file,
11067 delete_dwo_file,
11068 xcalloc, xfree));
11069 }
11070
11071 /* Lookup DWO file DWO_NAME. */
11072
11073 static void **
11074 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11075 const char *dwo_name,
11076 const char *comp_dir)
11077 {
11078 struct dwo_file find_entry;
11079 void **slot;
11080
11081 if (dwarf2_per_objfile->dwo_files == NULL)
11082 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
11083
11084 find_entry.dwo_name = dwo_name;
11085 find_entry.comp_dir = comp_dir;
11086 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11087 INSERT);
11088
11089 return slot;
11090 }
11091
11092 static hashval_t
11093 hash_dwo_unit (const void *item)
11094 {
11095 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11096
11097 /* This drops the top 32 bits of the id, but is ok for a hash. */
11098 return dwo_unit->signature;
11099 }
11100
11101 static int
11102 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11103 {
11104 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11105 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11106
11107 /* The signature is assumed to be unique within the DWO file.
11108 So while object file CU dwo_id's always have the value zero,
11109 that's OK, assuming each object file DWO file has only one CU,
11110 and that's the rule for now. */
11111 return lhs->signature == rhs->signature;
11112 }
11113
11114 /* Allocate a hash table for DWO CUs,TUs.
11115 There is one of these tables for each of CUs,TUs for each DWO file. */
11116
11117 static htab_up
11118 allocate_dwo_unit_table ()
11119 {
11120 /* Start out with a pretty small number.
11121 Generally DWO files contain only one CU and maybe some TUs. */
11122 return htab_up (htab_create_alloc (3,
11123 hash_dwo_unit,
11124 eq_dwo_unit,
11125 NULL, xcalloc, xfree));
11126 }
11127
11128 /* die_reader_func for create_dwo_cu. */
11129
11130 static void
11131 create_dwo_cu_reader (const struct die_reader_specs *reader,
11132 const gdb_byte *info_ptr,
11133 struct die_info *comp_unit_die,
11134 struct dwo_file *dwo_file,
11135 struct dwo_unit *dwo_unit)
11136 {
11137 struct dwarf2_cu *cu = reader->cu;
11138 sect_offset sect_off = cu->per_cu->sect_off;
11139 struct dwarf2_section_info *section = cu->per_cu->section;
11140
11141 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11142 if (!signature.has_value ())
11143 {
11144 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11145 " its dwo_id [in module %s]"),
11146 sect_offset_str (sect_off), dwo_file->dwo_name);
11147 return;
11148 }
11149
11150 dwo_unit->dwo_file = dwo_file;
11151 dwo_unit->signature = *signature;
11152 dwo_unit->section = section;
11153 dwo_unit->sect_off = sect_off;
11154 dwo_unit->length = cu->per_cu->length;
11155
11156 if (dwarf_read_debug)
11157 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11158 sect_offset_str (sect_off),
11159 hex_string (dwo_unit->signature));
11160 }
11161
11162 /* Create the dwo_units for the CUs in a DWO_FILE.
11163 Note: This function processes DWO files only, not DWP files. */
11164
11165 static void
11166 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11167 dwarf2_cu *cu, struct dwo_file &dwo_file,
11168 dwarf2_section_info &section, htab_up &cus_htab)
11169 {
11170 struct objfile *objfile = dwarf2_per_objfile->objfile;
11171 const gdb_byte *info_ptr, *end_ptr;
11172
11173 section.read (objfile);
11174 info_ptr = section.buffer;
11175
11176 if (info_ptr == NULL)
11177 return;
11178
11179 if (dwarf_read_debug)
11180 {
11181 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11182 section.get_name (),
11183 section.get_file_name ());
11184 }
11185
11186 end_ptr = info_ptr + section.size;
11187 while (info_ptr < end_ptr)
11188 {
11189 struct dwarf2_per_cu_data per_cu;
11190 struct dwo_unit read_unit {};
11191 struct dwo_unit *dwo_unit;
11192 void **slot;
11193 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11194
11195 memset (&per_cu, 0, sizeof (per_cu));
11196 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11197 per_cu.is_debug_types = 0;
11198 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11199 per_cu.section = &section;
11200
11201 cutu_reader reader (&per_cu, cu, &dwo_file);
11202 if (!reader.dummy_p)
11203 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11204 &dwo_file, &read_unit);
11205 info_ptr += per_cu.length;
11206
11207 // If the unit could not be parsed, skip it.
11208 if (read_unit.dwo_file == NULL)
11209 continue;
11210
11211 if (cus_htab == NULL)
11212 cus_htab = allocate_dwo_unit_table ();
11213
11214 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11215 *dwo_unit = read_unit;
11216 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11217 gdb_assert (slot != NULL);
11218 if (*slot != NULL)
11219 {
11220 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11221 sect_offset dup_sect_off = dup_cu->sect_off;
11222
11223 complaint (_("debug cu entry at offset %s is duplicate to"
11224 " the entry at offset %s, signature %s"),
11225 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11226 hex_string (dwo_unit->signature));
11227 }
11228 *slot = (void *)dwo_unit;
11229 }
11230 }
11231
11232 /* DWP file .debug_{cu,tu}_index section format:
11233 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11234
11235 DWP Version 1:
11236
11237 Both index sections have the same format, and serve to map a 64-bit
11238 signature to a set of section numbers. Each section begins with a header,
11239 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11240 indexes, and a pool of 32-bit section numbers. The index sections will be
11241 aligned at 8-byte boundaries in the file.
11242
11243 The index section header consists of:
11244
11245 V, 32 bit version number
11246 -, 32 bits unused
11247 N, 32 bit number of compilation units or type units in the index
11248 M, 32 bit number of slots in the hash table
11249
11250 Numbers are recorded using the byte order of the application binary.
11251
11252 The hash table begins at offset 16 in the section, and consists of an array
11253 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11254 order of the application binary). Unused slots in the hash table are 0.
11255 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11256
11257 The parallel table begins immediately after the hash table
11258 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11259 array of 32-bit indexes (using the byte order of the application binary),
11260 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11261 table contains a 32-bit index into the pool of section numbers. For unused
11262 hash table slots, the corresponding entry in the parallel table will be 0.
11263
11264 The pool of section numbers begins immediately following the hash table
11265 (at offset 16 + 12 * M from the beginning of the section). The pool of
11266 section numbers consists of an array of 32-bit words (using the byte order
11267 of the application binary). Each item in the array is indexed starting
11268 from 0. The hash table entry provides the index of the first section
11269 number in the set. Additional section numbers in the set follow, and the
11270 set is terminated by a 0 entry (section number 0 is not used in ELF).
11271
11272 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11273 section must be the first entry in the set, and the .debug_abbrev.dwo must
11274 be the second entry. Other members of the set may follow in any order.
11275
11276 ---
11277
11278 DWP Version 2:
11279
11280 DWP Version 2 combines all the .debug_info, etc. sections into one,
11281 and the entries in the index tables are now offsets into these sections.
11282 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11283 section.
11284
11285 Index Section Contents:
11286 Header
11287 Hash Table of Signatures dwp_hash_table.hash_table
11288 Parallel Table of Indices dwp_hash_table.unit_table
11289 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11290 Table of Section Sizes dwp_hash_table.v2.sizes
11291
11292 The index section header consists of:
11293
11294 V, 32 bit version number
11295 L, 32 bit number of columns in the table of section offsets
11296 N, 32 bit number of compilation units or type units in the index
11297 M, 32 bit number of slots in the hash table
11298
11299 Numbers are recorded using the byte order of the application binary.
11300
11301 The hash table has the same format as version 1.
11302 The parallel table of indices has the same format as version 1,
11303 except that the entries are origin-1 indices into the table of sections
11304 offsets and the table of section sizes.
11305
11306 The table of offsets begins immediately following the parallel table
11307 (at offset 16 + 12 * M from the beginning of the section). The table is
11308 a two-dimensional array of 32-bit words (using the byte order of the
11309 application binary), with L columns and N+1 rows, in row-major order.
11310 Each row in the array is indexed starting from 0. The first row provides
11311 a key to the remaining rows: each column in this row provides an identifier
11312 for a debug section, and the offsets in the same column of subsequent rows
11313 refer to that section. The section identifiers are:
11314
11315 DW_SECT_INFO 1 .debug_info.dwo
11316 DW_SECT_TYPES 2 .debug_types.dwo
11317 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11318 DW_SECT_LINE 4 .debug_line.dwo
11319 DW_SECT_LOC 5 .debug_loc.dwo
11320 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11321 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11322 DW_SECT_MACRO 8 .debug_macro.dwo
11323
11324 The offsets provided by the CU and TU index sections are the base offsets
11325 for the contributions made by each CU or TU to the corresponding section
11326 in the package file. Each CU and TU header contains an abbrev_offset
11327 field, used to find the abbreviations table for that CU or TU within the
11328 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11329 be interpreted as relative to the base offset given in the index section.
11330 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11331 should be interpreted as relative to the base offset for .debug_line.dwo,
11332 and offsets into other debug sections obtained from DWARF attributes should
11333 also be interpreted as relative to the corresponding base offset.
11334
11335 The table of sizes begins immediately following the table of offsets.
11336 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11337 with L columns and N rows, in row-major order. Each row in the array is
11338 indexed starting from 1 (row 0 is shared by the two tables).
11339
11340 ---
11341
11342 Hash table lookup is handled the same in version 1 and 2:
11343
11344 We assume that N and M will not exceed 2^32 - 1.
11345 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11346
11347 Given a 64-bit compilation unit signature or a type signature S, an entry
11348 in the hash table is located as follows:
11349
11350 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11351 the low-order k bits all set to 1.
11352
11353 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11354
11355 3) If the hash table entry at index H matches the signature, use that
11356 entry. If the hash table entry at index H is unused (all zeroes),
11357 terminate the search: the signature is not present in the table.
11358
11359 4) Let H = (H + H') modulo M. Repeat at Step 3.
11360
11361 Because M > N and H' and M are relatively prime, the search is guaranteed
11362 to stop at an unused slot or find the match. */
11363
11364 /* Create a hash table to map DWO IDs to their CU/TU entry in
11365 .debug_{info,types}.dwo in DWP_FILE.
11366 Returns NULL if there isn't one.
11367 Note: This function processes DWP files only, not DWO files. */
11368
11369 static struct dwp_hash_table *
11370 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11371 struct dwp_file *dwp_file, int is_debug_types)
11372 {
11373 struct objfile *objfile = dwarf2_per_objfile->objfile;
11374 bfd *dbfd = dwp_file->dbfd.get ();
11375 const gdb_byte *index_ptr, *index_end;
11376 struct dwarf2_section_info *index;
11377 uint32_t version, nr_columns, nr_units, nr_slots;
11378 struct dwp_hash_table *htab;
11379
11380 if (is_debug_types)
11381 index = &dwp_file->sections.tu_index;
11382 else
11383 index = &dwp_file->sections.cu_index;
11384
11385 if (index->empty ())
11386 return NULL;
11387 index->read (objfile);
11388
11389 index_ptr = index->buffer;
11390 index_end = index_ptr + index->size;
11391
11392 version = read_4_bytes (dbfd, index_ptr);
11393 index_ptr += 4;
11394 if (version == 2)
11395 nr_columns = read_4_bytes (dbfd, index_ptr);
11396 else
11397 nr_columns = 0;
11398 index_ptr += 4;
11399 nr_units = read_4_bytes (dbfd, index_ptr);
11400 index_ptr += 4;
11401 nr_slots = read_4_bytes (dbfd, index_ptr);
11402 index_ptr += 4;
11403
11404 if (version != 1 && version != 2)
11405 {
11406 error (_("Dwarf Error: unsupported DWP file version (%s)"
11407 " [in module %s]"),
11408 pulongest (version), dwp_file->name);
11409 }
11410 if (nr_slots != (nr_slots & -nr_slots))
11411 {
11412 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11413 " is not power of 2 [in module %s]"),
11414 pulongest (nr_slots), dwp_file->name);
11415 }
11416
11417 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11418 htab->version = version;
11419 htab->nr_columns = nr_columns;
11420 htab->nr_units = nr_units;
11421 htab->nr_slots = nr_slots;
11422 htab->hash_table = index_ptr;
11423 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11424
11425 /* Exit early if the table is empty. */
11426 if (nr_slots == 0 || nr_units == 0
11427 || (version == 2 && nr_columns == 0))
11428 {
11429 /* All must be zero. */
11430 if (nr_slots != 0 || nr_units != 0
11431 || (version == 2 && nr_columns != 0))
11432 {
11433 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11434 " all zero [in modules %s]"),
11435 dwp_file->name);
11436 }
11437 return htab;
11438 }
11439
11440 if (version == 1)
11441 {
11442 htab->section_pool.v1.indices =
11443 htab->unit_table + sizeof (uint32_t) * nr_slots;
11444 /* It's harder to decide whether the section is too small in v1.
11445 V1 is deprecated anyway so we punt. */
11446 }
11447 else
11448 {
11449 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11450 int *ids = htab->section_pool.v2.section_ids;
11451 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11452 /* Reverse map for error checking. */
11453 int ids_seen[DW_SECT_MAX + 1];
11454 int i;
11455
11456 if (nr_columns < 2)
11457 {
11458 error (_("Dwarf Error: bad DWP hash table, too few columns"
11459 " in section table [in module %s]"),
11460 dwp_file->name);
11461 }
11462 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11463 {
11464 error (_("Dwarf Error: bad DWP hash table, too many columns"
11465 " in section table [in module %s]"),
11466 dwp_file->name);
11467 }
11468 memset (ids, 255, sizeof_ids);
11469 memset (ids_seen, 255, sizeof (ids_seen));
11470 for (i = 0; i < nr_columns; ++i)
11471 {
11472 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11473
11474 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11475 {
11476 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11477 " in section table [in module %s]"),
11478 id, dwp_file->name);
11479 }
11480 if (ids_seen[id] != -1)
11481 {
11482 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11483 " id %d in section table [in module %s]"),
11484 id, dwp_file->name);
11485 }
11486 ids_seen[id] = i;
11487 ids[i] = id;
11488 }
11489 /* Must have exactly one info or types section. */
11490 if (((ids_seen[DW_SECT_INFO] != -1)
11491 + (ids_seen[DW_SECT_TYPES] != -1))
11492 != 1)
11493 {
11494 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11495 " DWO info/types section [in module %s]"),
11496 dwp_file->name);
11497 }
11498 /* Must have an abbrev section. */
11499 if (ids_seen[DW_SECT_ABBREV] == -1)
11500 {
11501 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11502 " section [in module %s]"),
11503 dwp_file->name);
11504 }
11505 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11506 htab->section_pool.v2.sizes =
11507 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11508 * nr_units * nr_columns);
11509 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11510 * nr_units * nr_columns))
11511 > index_end)
11512 {
11513 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11514 " [in module %s]"),
11515 dwp_file->name);
11516 }
11517 }
11518
11519 return htab;
11520 }
11521
11522 /* Update SECTIONS with the data from SECTP.
11523
11524 This function is like the other "locate" section routines that are
11525 passed to bfd_map_over_sections, but in this context the sections to
11526 read comes from the DWP V1 hash table, not the full ELF section table.
11527
11528 The result is non-zero for success, or zero if an error was found. */
11529
11530 static int
11531 locate_v1_virtual_dwo_sections (asection *sectp,
11532 struct virtual_v1_dwo_sections *sections)
11533 {
11534 const struct dwop_section_names *names = &dwop_section_names;
11535
11536 if (section_is_p (sectp->name, &names->abbrev_dwo))
11537 {
11538 /* There can be only one. */
11539 if (sections->abbrev.s.section != NULL)
11540 return 0;
11541 sections->abbrev.s.section = sectp;
11542 sections->abbrev.size = bfd_section_size (sectp);
11543 }
11544 else if (section_is_p (sectp->name, &names->info_dwo)
11545 || section_is_p (sectp->name, &names->types_dwo))
11546 {
11547 /* There can be only one. */
11548 if (sections->info_or_types.s.section != NULL)
11549 return 0;
11550 sections->info_or_types.s.section = sectp;
11551 sections->info_or_types.size = bfd_section_size (sectp);
11552 }
11553 else if (section_is_p (sectp->name, &names->line_dwo))
11554 {
11555 /* There can be only one. */
11556 if (sections->line.s.section != NULL)
11557 return 0;
11558 sections->line.s.section = sectp;
11559 sections->line.size = bfd_section_size (sectp);
11560 }
11561 else if (section_is_p (sectp->name, &names->loc_dwo))
11562 {
11563 /* There can be only one. */
11564 if (sections->loc.s.section != NULL)
11565 return 0;
11566 sections->loc.s.section = sectp;
11567 sections->loc.size = bfd_section_size (sectp);
11568 }
11569 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11570 {
11571 /* There can be only one. */
11572 if (sections->macinfo.s.section != NULL)
11573 return 0;
11574 sections->macinfo.s.section = sectp;
11575 sections->macinfo.size = bfd_section_size (sectp);
11576 }
11577 else if (section_is_p (sectp->name, &names->macro_dwo))
11578 {
11579 /* There can be only one. */
11580 if (sections->macro.s.section != NULL)
11581 return 0;
11582 sections->macro.s.section = sectp;
11583 sections->macro.size = bfd_section_size (sectp);
11584 }
11585 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11586 {
11587 /* There can be only one. */
11588 if (sections->str_offsets.s.section != NULL)
11589 return 0;
11590 sections->str_offsets.s.section = sectp;
11591 sections->str_offsets.size = bfd_section_size (sectp);
11592 }
11593 else
11594 {
11595 /* No other kind of section is valid. */
11596 return 0;
11597 }
11598
11599 return 1;
11600 }
11601
11602 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11603 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11604 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11605 This is for DWP version 1 files. */
11606
11607 static struct dwo_unit *
11608 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11609 struct dwp_file *dwp_file,
11610 uint32_t unit_index,
11611 const char *comp_dir,
11612 ULONGEST signature, int is_debug_types)
11613 {
11614 struct objfile *objfile = dwarf2_per_objfile->objfile;
11615 const struct dwp_hash_table *dwp_htab =
11616 is_debug_types ? dwp_file->tus : dwp_file->cus;
11617 bfd *dbfd = dwp_file->dbfd.get ();
11618 const char *kind = is_debug_types ? "TU" : "CU";
11619 struct dwo_file *dwo_file;
11620 struct dwo_unit *dwo_unit;
11621 struct virtual_v1_dwo_sections sections;
11622 void **dwo_file_slot;
11623 int i;
11624
11625 gdb_assert (dwp_file->version == 1);
11626
11627 if (dwarf_read_debug)
11628 {
11629 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11630 kind,
11631 pulongest (unit_index), hex_string (signature),
11632 dwp_file->name);
11633 }
11634
11635 /* Fetch the sections of this DWO unit.
11636 Put a limit on the number of sections we look for so that bad data
11637 doesn't cause us to loop forever. */
11638
11639 #define MAX_NR_V1_DWO_SECTIONS \
11640 (1 /* .debug_info or .debug_types */ \
11641 + 1 /* .debug_abbrev */ \
11642 + 1 /* .debug_line */ \
11643 + 1 /* .debug_loc */ \
11644 + 1 /* .debug_str_offsets */ \
11645 + 1 /* .debug_macro or .debug_macinfo */ \
11646 + 1 /* trailing zero */)
11647
11648 memset (&sections, 0, sizeof (sections));
11649
11650 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11651 {
11652 asection *sectp;
11653 uint32_t section_nr =
11654 read_4_bytes (dbfd,
11655 dwp_htab->section_pool.v1.indices
11656 + (unit_index + i) * sizeof (uint32_t));
11657
11658 if (section_nr == 0)
11659 break;
11660 if (section_nr >= dwp_file->num_sections)
11661 {
11662 error (_("Dwarf Error: bad DWP hash table, section number too large"
11663 " [in module %s]"),
11664 dwp_file->name);
11665 }
11666
11667 sectp = dwp_file->elf_sections[section_nr];
11668 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11669 {
11670 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11671 " [in module %s]"),
11672 dwp_file->name);
11673 }
11674 }
11675
11676 if (i < 2
11677 || sections.info_or_types.empty ()
11678 || sections.abbrev.empty ())
11679 {
11680 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11681 " [in module %s]"),
11682 dwp_file->name);
11683 }
11684 if (i == MAX_NR_V1_DWO_SECTIONS)
11685 {
11686 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11687 " [in module %s]"),
11688 dwp_file->name);
11689 }
11690
11691 /* It's easier for the rest of the code if we fake a struct dwo_file and
11692 have dwo_unit "live" in that. At least for now.
11693
11694 The DWP file can be made up of a random collection of CUs and TUs.
11695 However, for each CU + set of TUs that came from the same original DWO
11696 file, we can combine them back into a virtual DWO file to save space
11697 (fewer struct dwo_file objects to allocate). Remember that for really
11698 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11699
11700 std::string virtual_dwo_name =
11701 string_printf ("virtual-dwo/%d-%d-%d-%d",
11702 sections.abbrev.get_id (),
11703 sections.line.get_id (),
11704 sections.loc.get_id (),
11705 sections.str_offsets.get_id ());
11706 /* Can we use an existing virtual DWO file? */
11707 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11708 virtual_dwo_name.c_str (),
11709 comp_dir);
11710 /* Create one if necessary. */
11711 if (*dwo_file_slot == NULL)
11712 {
11713 if (dwarf_read_debug)
11714 {
11715 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11716 virtual_dwo_name.c_str ());
11717 }
11718 dwo_file = new struct dwo_file;
11719 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11720 dwo_file->comp_dir = comp_dir;
11721 dwo_file->sections.abbrev = sections.abbrev;
11722 dwo_file->sections.line = sections.line;
11723 dwo_file->sections.loc = sections.loc;
11724 dwo_file->sections.macinfo = sections.macinfo;
11725 dwo_file->sections.macro = sections.macro;
11726 dwo_file->sections.str_offsets = sections.str_offsets;
11727 /* The "str" section is global to the entire DWP file. */
11728 dwo_file->sections.str = dwp_file->sections.str;
11729 /* The info or types section is assigned below to dwo_unit,
11730 there's no need to record it in dwo_file.
11731 Also, we can't simply record type sections in dwo_file because
11732 we record a pointer into the vector in dwo_unit. As we collect more
11733 types we'll grow the vector and eventually have to reallocate space
11734 for it, invalidating all copies of pointers into the previous
11735 contents. */
11736 *dwo_file_slot = dwo_file;
11737 }
11738 else
11739 {
11740 if (dwarf_read_debug)
11741 {
11742 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11743 virtual_dwo_name.c_str ());
11744 }
11745 dwo_file = (struct dwo_file *) *dwo_file_slot;
11746 }
11747
11748 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11749 dwo_unit->dwo_file = dwo_file;
11750 dwo_unit->signature = signature;
11751 dwo_unit->section =
11752 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11753 *dwo_unit->section = sections.info_or_types;
11754 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11755
11756 return dwo_unit;
11757 }
11758
11759 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11760 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11761 piece within that section used by a TU/CU, return a virtual section
11762 of just that piece. */
11763
11764 static struct dwarf2_section_info
11765 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11766 struct dwarf2_section_info *section,
11767 bfd_size_type offset, bfd_size_type size)
11768 {
11769 struct dwarf2_section_info result;
11770 asection *sectp;
11771
11772 gdb_assert (section != NULL);
11773 gdb_assert (!section->is_virtual);
11774
11775 memset (&result, 0, sizeof (result));
11776 result.s.containing_section = section;
11777 result.is_virtual = true;
11778
11779 if (size == 0)
11780 return result;
11781
11782 sectp = section->get_bfd_section ();
11783
11784 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11785 bounds of the real section. This is a pretty-rare event, so just
11786 flag an error (easier) instead of a warning and trying to cope. */
11787 if (sectp == NULL
11788 || offset + size > bfd_section_size (sectp))
11789 {
11790 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11791 " in section %s [in module %s]"),
11792 sectp ? bfd_section_name (sectp) : "<unknown>",
11793 objfile_name (dwarf2_per_objfile->objfile));
11794 }
11795
11796 result.virtual_offset = offset;
11797 result.size = size;
11798 return result;
11799 }
11800
11801 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11802 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11803 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11804 This is for DWP version 2 files. */
11805
11806 static struct dwo_unit *
11807 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11808 struct dwp_file *dwp_file,
11809 uint32_t unit_index,
11810 const char *comp_dir,
11811 ULONGEST signature, int is_debug_types)
11812 {
11813 struct objfile *objfile = dwarf2_per_objfile->objfile;
11814 const struct dwp_hash_table *dwp_htab =
11815 is_debug_types ? dwp_file->tus : dwp_file->cus;
11816 bfd *dbfd = dwp_file->dbfd.get ();
11817 const char *kind = is_debug_types ? "TU" : "CU";
11818 struct dwo_file *dwo_file;
11819 struct dwo_unit *dwo_unit;
11820 struct virtual_v2_dwo_sections sections;
11821 void **dwo_file_slot;
11822 int i;
11823
11824 gdb_assert (dwp_file->version == 2);
11825
11826 if (dwarf_read_debug)
11827 {
11828 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11829 kind,
11830 pulongest (unit_index), hex_string (signature),
11831 dwp_file->name);
11832 }
11833
11834 /* Fetch the section offsets of this DWO unit. */
11835
11836 memset (&sections, 0, sizeof (sections));
11837
11838 for (i = 0; i < dwp_htab->nr_columns; ++i)
11839 {
11840 uint32_t offset = read_4_bytes (dbfd,
11841 dwp_htab->section_pool.v2.offsets
11842 + (((unit_index - 1) * dwp_htab->nr_columns
11843 + i)
11844 * sizeof (uint32_t)));
11845 uint32_t size = read_4_bytes (dbfd,
11846 dwp_htab->section_pool.v2.sizes
11847 + (((unit_index - 1) * dwp_htab->nr_columns
11848 + i)
11849 * sizeof (uint32_t)));
11850
11851 switch (dwp_htab->section_pool.v2.section_ids[i])
11852 {
11853 case DW_SECT_INFO:
11854 case DW_SECT_TYPES:
11855 sections.info_or_types_offset = offset;
11856 sections.info_or_types_size = size;
11857 break;
11858 case DW_SECT_ABBREV:
11859 sections.abbrev_offset = offset;
11860 sections.abbrev_size = size;
11861 break;
11862 case DW_SECT_LINE:
11863 sections.line_offset = offset;
11864 sections.line_size = size;
11865 break;
11866 case DW_SECT_LOC:
11867 sections.loc_offset = offset;
11868 sections.loc_size = size;
11869 break;
11870 case DW_SECT_STR_OFFSETS:
11871 sections.str_offsets_offset = offset;
11872 sections.str_offsets_size = size;
11873 break;
11874 case DW_SECT_MACINFO:
11875 sections.macinfo_offset = offset;
11876 sections.macinfo_size = size;
11877 break;
11878 case DW_SECT_MACRO:
11879 sections.macro_offset = offset;
11880 sections.macro_size = size;
11881 break;
11882 }
11883 }
11884
11885 /* It's easier for the rest of the code if we fake a struct dwo_file and
11886 have dwo_unit "live" in that. At least for now.
11887
11888 The DWP file can be made up of a random collection of CUs and TUs.
11889 However, for each CU + set of TUs that came from the same original DWO
11890 file, we can combine them back into a virtual DWO file to save space
11891 (fewer struct dwo_file objects to allocate). Remember that for really
11892 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11893
11894 std::string virtual_dwo_name =
11895 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11896 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11897 (long) (sections.line_size ? sections.line_offset : 0),
11898 (long) (sections.loc_size ? sections.loc_offset : 0),
11899 (long) (sections.str_offsets_size
11900 ? sections.str_offsets_offset : 0));
11901 /* Can we use an existing virtual DWO file? */
11902 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11903 virtual_dwo_name.c_str (),
11904 comp_dir);
11905 /* Create one if necessary. */
11906 if (*dwo_file_slot == NULL)
11907 {
11908 if (dwarf_read_debug)
11909 {
11910 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11911 virtual_dwo_name.c_str ());
11912 }
11913 dwo_file = new struct dwo_file;
11914 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11915 dwo_file->comp_dir = comp_dir;
11916 dwo_file->sections.abbrev =
11917 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11918 sections.abbrev_offset, sections.abbrev_size);
11919 dwo_file->sections.line =
11920 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11921 sections.line_offset, sections.line_size);
11922 dwo_file->sections.loc =
11923 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11924 sections.loc_offset, sections.loc_size);
11925 dwo_file->sections.macinfo =
11926 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11927 sections.macinfo_offset, sections.macinfo_size);
11928 dwo_file->sections.macro =
11929 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11930 sections.macro_offset, sections.macro_size);
11931 dwo_file->sections.str_offsets =
11932 create_dwp_v2_section (dwarf2_per_objfile,
11933 &dwp_file->sections.str_offsets,
11934 sections.str_offsets_offset,
11935 sections.str_offsets_size);
11936 /* The "str" section is global to the entire DWP file. */
11937 dwo_file->sections.str = dwp_file->sections.str;
11938 /* The info or types section is assigned below to dwo_unit,
11939 there's no need to record it in dwo_file.
11940 Also, we can't simply record type sections in dwo_file because
11941 we record a pointer into the vector in dwo_unit. As we collect more
11942 types we'll grow the vector and eventually have to reallocate space
11943 for it, invalidating all copies of pointers into the previous
11944 contents. */
11945 *dwo_file_slot = dwo_file;
11946 }
11947 else
11948 {
11949 if (dwarf_read_debug)
11950 {
11951 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11952 virtual_dwo_name.c_str ());
11953 }
11954 dwo_file = (struct dwo_file *) *dwo_file_slot;
11955 }
11956
11957 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11958 dwo_unit->dwo_file = dwo_file;
11959 dwo_unit->signature = signature;
11960 dwo_unit->section =
11961 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11962 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11963 is_debug_types
11964 ? &dwp_file->sections.types
11965 : &dwp_file->sections.info,
11966 sections.info_or_types_offset,
11967 sections.info_or_types_size);
11968 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11969
11970 return dwo_unit;
11971 }
11972
11973 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11974 Returns NULL if the signature isn't found. */
11975
11976 static struct dwo_unit *
11977 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11978 struct dwp_file *dwp_file, const char *comp_dir,
11979 ULONGEST signature, int is_debug_types)
11980 {
11981 const struct dwp_hash_table *dwp_htab =
11982 is_debug_types ? dwp_file->tus : dwp_file->cus;
11983 bfd *dbfd = dwp_file->dbfd.get ();
11984 uint32_t mask = dwp_htab->nr_slots - 1;
11985 uint32_t hash = signature & mask;
11986 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11987 unsigned int i;
11988 void **slot;
11989 struct dwo_unit find_dwo_cu;
11990
11991 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11992 find_dwo_cu.signature = signature;
11993 slot = htab_find_slot (is_debug_types
11994 ? dwp_file->loaded_tus.get ()
11995 : dwp_file->loaded_cus.get (),
11996 &find_dwo_cu, INSERT);
11997
11998 if (*slot != NULL)
11999 return (struct dwo_unit *) *slot;
12000
12001 /* Use a for loop so that we don't loop forever on bad debug info. */
12002 for (i = 0; i < dwp_htab->nr_slots; ++i)
12003 {
12004 ULONGEST signature_in_table;
12005
12006 signature_in_table =
12007 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12008 if (signature_in_table == signature)
12009 {
12010 uint32_t unit_index =
12011 read_4_bytes (dbfd,
12012 dwp_htab->unit_table + hash * sizeof (uint32_t));
12013
12014 if (dwp_file->version == 1)
12015 {
12016 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12017 dwp_file, unit_index,
12018 comp_dir, signature,
12019 is_debug_types);
12020 }
12021 else
12022 {
12023 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12024 dwp_file, unit_index,
12025 comp_dir, signature,
12026 is_debug_types);
12027 }
12028 return (struct dwo_unit *) *slot;
12029 }
12030 if (signature_in_table == 0)
12031 return NULL;
12032 hash = (hash + hash2) & mask;
12033 }
12034
12035 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12036 " [in module %s]"),
12037 dwp_file->name);
12038 }
12039
12040 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12041 Open the file specified by FILE_NAME and hand it off to BFD for
12042 preliminary analysis. Return a newly initialized bfd *, which
12043 includes a canonicalized copy of FILE_NAME.
12044 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12045 SEARCH_CWD is true if the current directory is to be searched.
12046 It will be searched before debug-file-directory.
12047 If successful, the file is added to the bfd include table of the
12048 objfile's bfd (see gdb_bfd_record_inclusion).
12049 If unable to find/open the file, return NULL.
12050 NOTE: This function is derived from symfile_bfd_open. */
12051
12052 static gdb_bfd_ref_ptr
12053 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12054 const char *file_name, int is_dwp, int search_cwd)
12055 {
12056 int desc;
12057 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12058 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12059 to debug_file_directory. */
12060 const char *search_path;
12061 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12062
12063 gdb::unique_xmalloc_ptr<char> search_path_holder;
12064 if (search_cwd)
12065 {
12066 if (*debug_file_directory != '\0')
12067 {
12068 search_path_holder.reset (concat (".", dirname_separator_string,
12069 debug_file_directory,
12070 (char *) NULL));
12071 search_path = search_path_holder.get ();
12072 }
12073 else
12074 search_path = ".";
12075 }
12076 else
12077 search_path = debug_file_directory;
12078
12079 openp_flags flags = OPF_RETURN_REALPATH;
12080 if (is_dwp)
12081 flags |= OPF_SEARCH_IN_PATH;
12082
12083 gdb::unique_xmalloc_ptr<char> absolute_name;
12084 desc = openp (search_path, flags, file_name,
12085 O_RDONLY | O_BINARY, &absolute_name);
12086 if (desc < 0)
12087 return NULL;
12088
12089 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12090 gnutarget, desc));
12091 if (sym_bfd == NULL)
12092 return NULL;
12093 bfd_set_cacheable (sym_bfd.get (), 1);
12094
12095 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12096 return NULL;
12097
12098 /* Success. Record the bfd as having been included by the objfile's bfd.
12099 This is important because things like demangled_names_hash lives in the
12100 objfile's per_bfd space and may have references to things like symbol
12101 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12102 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12103
12104 return sym_bfd;
12105 }
12106
12107 /* Try to open DWO file FILE_NAME.
12108 COMP_DIR is the DW_AT_comp_dir attribute.
12109 The result is the bfd handle of the file.
12110 If there is a problem finding or opening the file, return NULL.
12111 Upon success, the canonicalized path of the file is stored in the bfd,
12112 same as symfile_bfd_open. */
12113
12114 static gdb_bfd_ref_ptr
12115 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12116 const char *file_name, const char *comp_dir)
12117 {
12118 if (IS_ABSOLUTE_PATH (file_name))
12119 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12120 0 /*is_dwp*/, 0 /*search_cwd*/);
12121
12122 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12123
12124 if (comp_dir != NULL)
12125 {
12126 gdb::unique_xmalloc_ptr<char> path_to_try
12127 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12128
12129 /* NOTE: If comp_dir is a relative path, this will also try the
12130 search path, which seems useful. */
12131 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12132 path_to_try.get (),
12133 0 /*is_dwp*/,
12134 1 /*search_cwd*/));
12135 if (abfd != NULL)
12136 return abfd;
12137 }
12138
12139 /* That didn't work, try debug-file-directory, which, despite its name,
12140 is a list of paths. */
12141
12142 if (*debug_file_directory == '\0')
12143 return NULL;
12144
12145 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12146 0 /*is_dwp*/, 1 /*search_cwd*/);
12147 }
12148
12149 /* This function is mapped across the sections and remembers the offset and
12150 size of each of the DWO debugging sections we are interested in. */
12151
12152 static void
12153 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12154 {
12155 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12156 const struct dwop_section_names *names = &dwop_section_names;
12157
12158 if (section_is_p (sectp->name, &names->abbrev_dwo))
12159 {
12160 dwo_sections->abbrev.s.section = sectp;
12161 dwo_sections->abbrev.size = bfd_section_size (sectp);
12162 }
12163 else if (section_is_p (sectp->name, &names->info_dwo))
12164 {
12165 dwo_sections->info.s.section = sectp;
12166 dwo_sections->info.size = bfd_section_size (sectp);
12167 }
12168 else if (section_is_p (sectp->name, &names->line_dwo))
12169 {
12170 dwo_sections->line.s.section = sectp;
12171 dwo_sections->line.size = bfd_section_size (sectp);
12172 }
12173 else if (section_is_p (sectp->name, &names->loc_dwo))
12174 {
12175 dwo_sections->loc.s.section = sectp;
12176 dwo_sections->loc.size = bfd_section_size (sectp);
12177 }
12178 else if (section_is_p (sectp->name, &names->loclists_dwo))
12179 {
12180 dwo_sections->loclists.s.section = sectp;
12181 dwo_sections->loclists.size = bfd_section_size (sectp);
12182 }
12183 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12184 {
12185 dwo_sections->macinfo.s.section = sectp;
12186 dwo_sections->macinfo.size = bfd_section_size (sectp);
12187 }
12188 else if (section_is_p (sectp->name, &names->macro_dwo))
12189 {
12190 dwo_sections->macro.s.section = sectp;
12191 dwo_sections->macro.size = bfd_section_size (sectp);
12192 }
12193 else if (section_is_p (sectp->name, &names->str_dwo))
12194 {
12195 dwo_sections->str.s.section = sectp;
12196 dwo_sections->str.size = bfd_section_size (sectp);
12197 }
12198 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12199 {
12200 dwo_sections->str_offsets.s.section = sectp;
12201 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12202 }
12203 else if (section_is_p (sectp->name, &names->types_dwo))
12204 {
12205 struct dwarf2_section_info type_section;
12206
12207 memset (&type_section, 0, sizeof (type_section));
12208 type_section.s.section = sectp;
12209 type_section.size = bfd_section_size (sectp);
12210 dwo_sections->types.push_back (type_section);
12211 }
12212 }
12213
12214 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12215 by PER_CU. This is for the non-DWP case.
12216 The result is NULL if DWO_NAME can't be found. */
12217
12218 static struct dwo_file *
12219 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12220 const char *dwo_name, const char *comp_dir)
12221 {
12222 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12223
12224 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12225 if (dbfd == NULL)
12226 {
12227 if (dwarf_read_debug)
12228 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12229 return NULL;
12230 }
12231
12232 dwo_file_up dwo_file (new struct dwo_file);
12233 dwo_file->dwo_name = dwo_name;
12234 dwo_file->comp_dir = comp_dir;
12235 dwo_file->dbfd = std::move (dbfd);
12236
12237 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12238 &dwo_file->sections);
12239
12240 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12241 dwo_file->sections.info, dwo_file->cus);
12242
12243 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12244 dwo_file->sections.types, dwo_file->tus);
12245
12246 if (dwarf_read_debug)
12247 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12248
12249 return dwo_file.release ();
12250 }
12251
12252 /* This function is mapped across the sections and remembers the offset and
12253 size of each of the DWP debugging sections common to version 1 and 2 that
12254 we are interested in. */
12255
12256 static void
12257 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12258 void *dwp_file_ptr)
12259 {
12260 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12261 const struct dwop_section_names *names = &dwop_section_names;
12262 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12263
12264 /* Record the ELF section number for later lookup: this is what the
12265 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12266 gdb_assert (elf_section_nr < dwp_file->num_sections);
12267 dwp_file->elf_sections[elf_section_nr] = sectp;
12268
12269 /* Look for specific sections that we need. */
12270 if (section_is_p (sectp->name, &names->str_dwo))
12271 {
12272 dwp_file->sections.str.s.section = sectp;
12273 dwp_file->sections.str.size = bfd_section_size (sectp);
12274 }
12275 else if (section_is_p (sectp->name, &names->cu_index))
12276 {
12277 dwp_file->sections.cu_index.s.section = sectp;
12278 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12279 }
12280 else if (section_is_p (sectp->name, &names->tu_index))
12281 {
12282 dwp_file->sections.tu_index.s.section = sectp;
12283 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12284 }
12285 }
12286
12287 /* This function is mapped across the sections and remembers the offset and
12288 size of each of the DWP version 2 debugging sections that we are interested
12289 in. This is split into a separate function because we don't know if we
12290 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12291
12292 static void
12293 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12294 {
12295 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12296 const struct dwop_section_names *names = &dwop_section_names;
12297 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12298
12299 /* Record the ELF section number for later lookup: this is what the
12300 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12301 gdb_assert (elf_section_nr < dwp_file->num_sections);
12302 dwp_file->elf_sections[elf_section_nr] = sectp;
12303
12304 /* Look for specific sections that we need. */
12305 if (section_is_p (sectp->name, &names->abbrev_dwo))
12306 {
12307 dwp_file->sections.abbrev.s.section = sectp;
12308 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12309 }
12310 else if (section_is_p (sectp->name, &names->info_dwo))
12311 {
12312 dwp_file->sections.info.s.section = sectp;
12313 dwp_file->sections.info.size = bfd_section_size (sectp);
12314 }
12315 else if (section_is_p (sectp->name, &names->line_dwo))
12316 {
12317 dwp_file->sections.line.s.section = sectp;
12318 dwp_file->sections.line.size = bfd_section_size (sectp);
12319 }
12320 else if (section_is_p (sectp->name, &names->loc_dwo))
12321 {
12322 dwp_file->sections.loc.s.section = sectp;
12323 dwp_file->sections.loc.size = bfd_section_size (sectp);
12324 }
12325 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12326 {
12327 dwp_file->sections.macinfo.s.section = sectp;
12328 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12329 }
12330 else if (section_is_p (sectp->name, &names->macro_dwo))
12331 {
12332 dwp_file->sections.macro.s.section = sectp;
12333 dwp_file->sections.macro.size = bfd_section_size (sectp);
12334 }
12335 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12336 {
12337 dwp_file->sections.str_offsets.s.section = sectp;
12338 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12339 }
12340 else if (section_is_p (sectp->name, &names->types_dwo))
12341 {
12342 dwp_file->sections.types.s.section = sectp;
12343 dwp_file->sections.types.size = bfd_section_size (sectp);
12344 }
12345 }
12346
12347 /* Hash function for dwp_file loaded CUs/TUs. */
12348
12349 static hashval_t
12350 hash_dwp_loaded_cutus (const void *item)
12351 {
12352 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12353
12354 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12355 return dwo_unit->signature;
12356 }
12357
12358 /* Equality function for dwp_file loaded CUs/TUs. */
12359
12360 static int
12361 eq_dwp_loaded_cutus (const void *a, const void *b)
12362 {
12363 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12364 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12365
12366 return dua->signature == dub->signature;
12367 }
12368
12369 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12370
12371 static htab_up
12372 allocate_dwp_loaded_cutus_table ()
12373 {
12374 return htab_up (htab_create_alloc (3,
12375 hash_dwp_loaded_cutus,
12376 eq_dwp_loaded_cutus,
12377 NULL, xcalloc, xfree));
12378 }
12379
12380 /* Try to open DWP file FILE_NAME.
12381 The result is the bfd handle of the file.
12382 If there is a problem finding or opening the file, return NULL.
12383 Upon success, the canonicalized path of the file is stored in the bfd,
12384 same as symfile_bfd_open. */
12385
12386 static gdb_bfd_ref_ptr
12387 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12388 const char *file_name)
12389 {
12390 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12391 1 /*is_dwp*/,
12392 1 /*search_cwd*/));
12393 if (abfd != NULL)
12394 return abfd;
12395
12396 /* Work around upstream bug 15652.
12397 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12398 [Whether that's a "bug" is debatable, but it is getting in our way.]
12399 We have no real idea where the dwp file is, because gdb's realpath-ing
12400 of the executable's path may have discarded the needed info.
12401 [IWBN if the dwp file name was recorded in the executable, akin to
12402 .gnu_debuglink, but that doesn't exist yet.]
12403 Strip the directory from FILE_NAME and search again. */
12404 if (*debug_file_directory != '\0')
12405 {
12406 /* Don't implicitly search the current directory here.
12407 If the user wants to search "." to handle this case,
12408 it must be added to debug-file-directory. */
12409 return try_open_dwop_file (dwarf2_per_objfile,
12410 lbasename (file_name), 1 /*is_dwp*/,
12411 0 /*search_cwd*/);
12412 }
12413
12414 return NULL;
12415 }
12416
12417 /* Initialize the use of the DWP file for the current objfile.
12418 By convention the name of the DWP file is ${objfile}.dwp.
12419 The result is NULL if it can't be found. */
12420
12421 static std::unique_ptr<struct dwp_file>
12422 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12423 {
12424 struct objfile *objfile = dwarf2_per_objfile->objfile;
12425
12426 /* Try to find first .dwp for the binary file before any symbolic links
12427 resolving. */
12428
12429 /* If the objfile is a debug file, find the name of the real binary
12430 file and get the name of dwp file from there. */
12431 std::string dwp_name;
12432 if (objfile->separate_debug_objfile_backlink != NULL)
12433 {
12434 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12435 const char *backlink_basename = lbasename (backlink->original_name);
12436
12437 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12438 }
12439 else
12440 dwp_name = objfile->original_name;
12441
12442 dwp_name += ".dwp";
12443
12444 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12445 if (dbfd == NULL
12446 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12447 {
12448 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12449 dwp_name = objfile_name (objfile);
12450 dwp_name += ".dwp";
12451 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12452 }
12453
12454 if (dbfd == NULL)
12455 {
12456 if (dwarf_read_debug)
12457 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12458 return std::unique_ptr<dwp_file> ();
12459 }
12460
12461 const char *name = bfd_get_filename (dbfd.get ());
12462 std::unique_ptr<struct dwp_file> dwp_file
12463 (new struct dwp_file (name, std::move (dbfd)));
12464
12465 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12466 dwp_file->elf_sections =
12467 OBSTACK_CALLOC (&objfile->objfile_obstack,
12468 dwp_file->num_sections, asection *);
12469
12470 bfd_map_over_sections (dwp_file->dbfd.get (),
12471 dwarf2_locate_common_dwp_sections,
12472 dwp_file.get ());
12473
12474 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12475 0);
12476
12477 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12478 1);
12479
12480 /* The DWP file version is stored in the hash table. Oh well. */
12481 if (dwp_file->cus && dwp_file->tus
12482 && dwp_file->cus->version != dwp_file->tus->version)
12483 {
12484 /* Technically speaking, we should try to limp along, but this is
12485 pretty bizarre. We use pulongest here because that's the established
12486 portability solution (e.g, we cannot use %u for uint32_t). */
12487 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12488 " TU version %s [in DWP file %s]"),
12489 pulongest (dwp_file->cus->version),
12490 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12491 }
12492
12493 if (dwp_file->cus)
12494 dwp_file->version = dwp_file->cus->version;
12495 else if (dwp_file->tus)
12496 dwp_file->version = dwp_file->tus->version;
12497 else
12498 dwp_file->version = 2;
12499
12500 if (dwp_file->version == 2)
12501 bfd_map_over_sections (dwp_file->dbfd.get (),
12502 dwarf2_locate_v2_dwp_sections,
12503 dwp_file.get ());
12504
12505 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12506 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12507
12508 if (dwarf_read_debug)
12509 {
12510 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12511 fprintf_unfiltered (gdb_stdlog,
12512 " %s CUs, %s TUs\n",
12513 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12514 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12515 }
12516
12517 return dwp_file;
12518 }
12519
12520 /* Wrapper around open_and_init_dwp_file, only open it once. */
12521
12522 static struct dwp_file *
12523 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12524 {
12525 if (! dwarf2_per_objfile->dwp_checked)
12526 {
12527 dwarf2_per_objfile->dwp_file
12528 = open_and_init_dwp_file (dwarf2_per_objfile);
12529 dwarf2_per_objfile->dwp_checked = 1;
12530 }
12531 return dwarf2_per_objfile->dwp_file.get ();
12532 }
12533
12534 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12535 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12536 or in the DWP file for the objfile, referenced by THIS_UNIT.
12537 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12538 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12539
12540 This is called, for example, when wanting to read a variable with a
12541 complex location. Therefore we don't want to do file i/o for every call.
12542 Therefore we don't want to look for a DWO file on every call.
12543 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12544 then we check if we've already seen DWO_NAME, and only THEN do we check
12545 for a DWO file.
12546
12547 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12548 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12549
12550 static struct dwo_unit *
12551 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12552 const char *dwo_name, const char *comp_dir,
12553 ULONGEST signature, int is_debug_types)
12554 {
12555 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12556 struct objfile *objfile = dwarf2_per_objfile->objfile;
12557 const char *kind = is_debug_types ? "TU" : "CU";
12558 void **dwo_file_slot;
12559 struct dwo_file *dwo_file;
12560 struct dwp_file *dwp_file;
12561
12562 /* First see if there's a DWP file.
12563 If we have a DWP file but didn't find the DWO inside it, don't
12564 look for the original DWO file. It makes gdb behave differently
12565 depending on whether one is debugging in the build tree. */
12566
12567 dwp_file = get_dwp_file (dwarf2_per_objfile);
12568 if (dwp_file != NULL)
12569 {
12570 const struct dwp_hash_table *dwp_htab =
12571 is_debug_types ? dwp_file->tus : dwp_file->cus;
12572
12573 if (dwp_htab != NULL)
12574 {
12575 struct dwo_unit *dwo_cutu =
12576 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12577 signature, is_debug_types);
12578
12579 if (dwo_cutu != NULL)
12580 {
12581 if (dwarf_read_debug)
12582 {
12583 fprintf_unfiltered (gdb_stdlog,
12584 "Virtual DWO %s %s found: @%s\n",
12585 kind, hex_string (signature),
12586 host_address_to_string (dwo_cutu));
12587 }
12588 return dwo_cutu;
12589 }
12590 }
12591 }
12592 else
12593 {
12594 /* No DWP file, look for the DWO file. */
12595
12596 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12597 dwo_name, comp_dir);
12598 if (*dwo_file_slot == NULL)
12599 {
12600 /* Read in the file and build a table of the CUs/TUs it contains. */
12601 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12602 }
12603 /* NOTE: This will be NULL if unable to open the file. */
12604 dwo_file = (struct dwo_file *) *dwo_file_slot;
12605
12606 if (dwo_file != NULL)
12607 {
12608 struct dwo_unit *dwo_cutu = NULL;
12609
12610 if (is_debug_types && dwo_file->tus)
12611 {
12612 struct dwo_unit find_dwo_cutu;
12613
12614 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12615 find_dwo_cutu.signature = signature;
12616 dwo_cutu
12617 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12618 &find_dwo_cutu);
12619 }
12620 else if (!is_debug_types && dwo_file->cus)
12621 {
12622 struct dwo_unit find_dwo_cutu;
12623
12624 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12625 find_dwo_cutu.signature = signature;
12626 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12627 &find_dwo_cutu);
12628 }
12629
12630 if (dwo_cutu != NULL)
12631 {
12632 if (dwarf_read_debug)
12633 {
12634 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12635 kind, dwo_name, hex_string (signature),
12636 host_address_to_string (dwo_cutu));
12637 }
12638 return dwo_cutu;
12639 }
12640 }
12641 }
12642
12643 /* We didn't find it. This could mean a dwo_id mismatch, or
12644 someone deleted the DWO/DWP file, or the search path isn't set up
12645 correctly to find the file. */
12646
12647 if (dwarf_read_debug)
12648 {
12649 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12650 kind, dwo_name, hex_string (signature));
12651 }
12652
12653 /* This is a warning and not a complaint because it can be caused by
12654 pilot error (e.g., user accidentally deleting the DWO). */
12655 {
12656 /* Print the name of the DWP file if we looked there, helps the user
12657 better diagnose the problem. */
12658 std::string dwp_text;
12659
12660 if (dwp_file != NULL)
12661 dwp_text = string_printf (" [in DWP file %s]",
12662 lbasename (dwp_file->name));
12663
12664 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12665 " [in module %s]"),
12666 kind, dwo_name, hex_string (signature),
12667 dwp_text.c_str (),
12668 this_unit->is_debug_types ? "TU" : "CU",
12669 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12670 }
12671 return NULL;
12672 }
12673
12674 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12675 See lookup_dwo_cutu_unit for details. */
12676
12677 static struct dwo_unit *
12678 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12679 const char *dwo_name, const char *comp_dir,
12680 ULONGEST signature)
12681 {
12682 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12683 }
12684
12685 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12686 See lookup_dwo_cutu_unit for details. */
12687
12688 static struct dwo_unit *
12689 lookup_dwo_type_unit (struct signatured_type *this_tu,
12690 const char *dwo_name, const char *comp_dir)
12691 {
12692 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12693 }
12694
12695 /* Traversal function for queue_and_load_all_dwo_tus. */
12696
12697 static int
12698 queue_and_load_dwo_tu (void **slot, void *info)
12699 {
12700 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12701 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12702 ULONGEST signature = dwo_unit->signature;
12703 struct signatured_type *sig_type =
12704 lookup_dwo_signatured_type (per_cu->cu, signature);
12705
12706 if (sig_type != NULL)
12707 {
12708 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12709
12710 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12711 a real dependency of PER_CU on SIG_TYPE. That is detected later
12712 while processing PER_CU. */
12713 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12714 load_full_type_unit (sig_cu);
12715 per_cu->imported_symtabs_push (sig_cu);
12716 }
12717
12718 return 1;
12719 }
12720
12721 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12722 The DWO may have the only definition of the type, though it may not be
12723 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12724 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12725
12726 static void
12727 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12728 {
12729 struct dwo_unit *dwo_unit;
12730 struct dwo_file *dwo_file;
12731
12732 gdb_assert (!per_cu->is_debug_types);
12733 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12734 gdb_assert (per_cu->cu != NULL);
12735
12736 dwo_unit = per_cu->cu->dwo_unit;
12737 gdb_assert (dwo_unit != NULL);
12738
12739 dwo_file = dwo_unit->dwo_file;
12740 if (dwo_file->tus != NULL)
12741 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12742 per_cu);
12743 }
12744
12745 /* Read in various DIEs. */
12746
12747 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12748 Inherit only the children of the DW_AT_abstract_origin DIE not being
12749 already referenced by DW_AT_abstract_origin from the children of the
12750 current DIE. */
12751
12752 static void
12753 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12754 {
12755 struct die_info *child_die;
12756 sect_offset *offsetp;
12757 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12758 struct die_info *origin_die;
12759 /* Iterator of the ORIGIN_DIE children. */
12760 struct die_info *origin_child_die;
12761 struct attribute *attr;
12762 struct dwarf2_cu *origin_cu;
12763 struct pending **origin_previous_list_in_scope;
12764
12765 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12766 if (!attr)
12767 return;
12768
12769 /* Note that following die references may follow to a die in a
12770 different cu. */
12771
12772 origin_cu = cu;
12773 origin_die = follow_die_ref (die, attr, &origin_cu);
12774
12775 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12776 symbols in. */
12777 origin_previous_list_in_scope = origin_cu->list_in_scope;
12778 origin_cu->list_in_scope = cu->list_in_scope;
12779
12780 if (die->tag != origin_die->tag
12781 && !(die->tag == DW_TAG_inlined_subroutine
12782 && origin_die->tag == DW_TAG_subprogram))
12783 complaint (_("DIE %s and its abstract origin %s have different tags"),
12784 sect_offset_str (die->sect_off),
12785 sect_offset_str (origin_die->sect_off));
12786
12787 std::vector<sect_offset> offsets;
12788
12789 for (child_die = die->child;
12790 child_die && child_die->tag;
12791 child_die = child_die->sibling)
12792 {
12793 struct die_info *child_origin_die;
12794 struct dwarf2_cu *child_origin_cu;
12795
12796 /* We are trying to process concrete instance entries:
12797 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12798 it's not relevant to our analysis here. i.e. detecting DIEs that are
12799 present in the abstract instance but not referenced in the concrete
12800 one. */
12801 if (child_die->tag == DW_TAG_call_site
12802 || child_die->tag == DW_TAG_GNU_call_site)
12803 continue;
12804
12805 /* For each CHILD_DIE, find the corresponding child of
12806 ORIGIN_DIE. If there is more than one layer of
12807 DW_AT_abstract_origin, follow them all; there shouldn't be,
12808 but GCC versions at least through 4.4 generate this (GCC PR
12809 40573). */
12810 child_origin_die = child_die;
12811 child_origin_cu = cu;
12812 while (1)
12813 {
12814 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12815 child_origin_cu);
12816 if (attr == NULL)
12817 break;
12818 child_origin_die = follow_die_ref (child_origin_die, attr,
12819 &child_origin_cu);
12820 }
12821
12822 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12823 counterpart may exist. */
12824 if (child_origin_die != child_die)
12825 {
12826 if (child_die->tag != child_origin_die->tag
12827 && !(child_die->tag == DW_TAG_inlined_subroutine
12828 && child_origin_die->tag == DW_TAG_subprogram))
12829 complaint (_("Child DIE %s and its abstract origin %s have "
12830 "different tags"),
12831 sect_offset_str (child_die->sect_off),
12832 sect_offset_str (child_origin_die->sect_off));
12833 if (child_origin_die->parent != origin_die)
12834 complaint (_("Child DIE %s and its abstract origin %s have "
12835 "different parents"),
12836 sect_offset_str (child_die->sect_off),
12837 sect_offset_str (child_origin_die->sect_off));
12838 else
12839 offsets.push_back (child_origin_die->sect_off);
12840 }
12841 }
12842 std::sort (offsets.begin (), offsets.end ());
12843 sect_offset *offsets_end = offsets.data () + offsets.size ();
12844 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12845 if (offsetp[-1] == *offsetp)
12846 complaint (_("Multiple children of DIE %s refer "
12847 "to DIE %s as their abstract origin"),
12848 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12849
12850 offsetp = offsets.data ();
12851 origin_child_die = origin_die->child;
12852 while (origin_child_die && origin_child_die->tag)
12853 {
12854 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12855 while (offsetp < offsets_end
12856 && *offsetp < origin_child_die->sect_off)
12857 offsetp++;
12858 if (offsetp >= offsets_end
12859 || *offsetp > origin_child_die->sect_off)
12860 {
12861 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12862 Check whether we're already processing ORIGIN_CHILD_DIE.
12863 This can happen with mutually referenced abstract_origins.
12864 PR 16581. */
12865 if (!origin_child_die->in_process)
12866 process_die (origin_child_die, origin_cu);
12867 }
12868 origin_child_die = origin_child_die->sibling;
12869 }
12870 origin_cu->list_in_scope = origin_previous_list_in_scope;
12871
12872 if (cu != origin_cu)
12873 compute_delayed_physnames (origin_cu);
12874 }
12875
12876 static void
12877 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12878 {
12879 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12880 struct gdbarch *gdbarch = objfile->arch ();
12881 struct context_stack *newobj;
12882 CORE_ADDR lowpc;
12883 CORE_ADDR highpc;
12884 struct die_info *child_die;
12885 struct attribute *attr, *call_line, *call_file;
12886 const char *name;
12887 CORE_ADDR baseaddr;
12888 struct block *block;
12889 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12890 std::vector<struct symbol *> template_args;
12891 struct template_symbol *templ_func = NULL;
12892
12893 if (inlined_func)
12894 {
12895 /* If we do not have call site information, we can't show the
12896 caller of this inlined function. That's too confusing, so
12897 only use the scope for local variables. */
12898 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12899 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12900 if (call_line == NULL || call_file == NULL)
12901 {
12902 read_lexical_block_scope (die, cu);
12903 return;
12904 }
12905 }
12906
12907 baseaddr = objfile->text_section_offset ();
12908
12909 name = dwarf2_name (die, cu);
12910
12911 /* Ignore functions with missing or empty names. These are actually
12912 illegal according to the DWARF standard. */
12913 if (name == NULL)
12914 {
12915 complaint (_("missing name for subprogram DIE at %s"),
12916 sect_offset_str (die->sect_off));
12917 return;
12918 }
12919
12920 /* Ignore functions with missing or invalid low and high pc attributes. */
12921 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12922 <= PC_BOUNDS_INVALID)
12923 {
12924 attr = dwarf2_attr (die, DW_AT_external, cu);
12925 if (!attr || !DW_UNSND (attr))
12926 complaint (_("cannot get low and high bounds "
12927 "for subprogram DIE at %s"),
12928 sect_offset_str (die->sect_off));
12929 return;
12930 }
12931
12932 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12933 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12934
12935 /* If we have any template arguments, then we must allocate a
12936 different sort of symbol. */
12937 for (child_die = die->child; child_die; child_die = child_die->sibling)
12938 {
12939 if (child_die->tag == DW_TAG_template_type_param
12940 || child_die->tag == DW_TAG_template_value_param)
12941 {
12942 templ_func = allocate_template_symbol (objfile);
12943 templ_func->subclass = SYMBOL_TEMPLATE;
12944 break;
12945 }
12946 }
12947
12948 newobj = cu->get_builder ()->push_context (0, lowpc);
12949 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12950 (struct symbol *) templ_func);
12951
12952 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12953 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12954 cu->language);
12955
12956 /* If there is a location expression for DW_AT_frame_base, record
12957 it. */
12958 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12959 if (attr != nullptr)
12960 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12961
12962 /* If there is a location for the static link, record it. */
12963 newobj->static_link = NULL;
12964 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12965 if (attr != nullptr)
12966 {
12967 newobj->static_link
12968 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12969 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12970 cu->per_cu->addr_type ());
12971 }
12972
12973 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12974
12975 if (die->child != NULL)
12976 {
12977 child_die = die->child;
12978 while (child_die && child_die->tag)
12979 {
12980 if (child_die->tag == DW_TAG_template_type_param
12981 || child_die->tag == DW_TAG_template_value_param)
12982 {
12983 struct symbol *arg = new_symbol (child_die, NULL, cu);
12984
12985 if (arg != NULL)
12986 template_args.push_back (arg);
12987 }
12988 else
12989 process_die (child_die, cu);
12990 child_die = child_die->sibling;
12991 }
12992 }
12993
12994 inherit_abstract_dies (die, cu);
12995
12996 /* If we have a DW_AT_specification, we might need to import using
12997 directives from the context of the specification DIE. See the
12998 comment in determine_prefix. */
12999 if (cu->language == language_cplus
13000 && dwarf2_attr (die, DW_AT_specification, cu))
13001 {
13002 struct dwarf2_cu *spec_cu = cu;
13003 struct die_info *spec_die = die_specification (die, &spec_cu);
13004
13005 while (spec_die)
13006 {
13007 child_die = spec_die->child;
13008 while (child_die && child_die->tag)
13009 {
13010 if (child_die->tag == DW_TAG_imported_module)
13011 process_die (child_die, spec_cu);
13012 child_die = child_die->sibling;
13013 }
13014
13015 /* In some cases, GCC generates specification DIEs that
13016 themselves contain DW_AT_specification attributes. */
13017 spec_die = die_specification (spec_die, &spec_cu);
13018 }
13019 }
13020
13021 struct context_stack cstk = cu->get_builder ()->pop_context ();
13022 /* Make a block for the local symbols within. */
13023 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13024 cstk.static_link, lowpc, highpc);
13025
13026 /* For C++, set the block's scope. */
13027 if ((cu->language == language_cplus
13028 || cu->language == language_fortran
13029 || cu->language == language_d
13030 || cu->language == language_rust)
13031 && cu->processing_has_namespace_info)
13032 block_set_scope (block, determine_prefix (die, cu),
13033 &objfile->objfile_obstack);
13034
13035 /* If we have address ranges, record them. */
13036 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13037
13038 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13039
13040 /* Attach template arguments to function. */
13041 if (!template_args.empty ())
13042 {
13043 gdb_assert (templ_func != NULL);
13044
13045 templ_func->n_template_arguments = template_args.size ();
13046 templ_func->template_arguments
13047 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13048 templ_func->n_template_arguments);
13049 memcpy (templ_func->template_arguments,
13050 template_args.data (),
13051 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13052
13053 /* Make sure that the symtab is set on the new symbols. Even
13054 though they don't appear in this symtab directly, other parts
13055 of gdb assume that symbols do, and this is reasonably
13056 true. */
13057 for (symbol *sym : template_args)
13058 symbol_set_symtab (sym, symbol_symtab (templ_func));
13059 }
13060
13061 /* In C++, we can have functions nested inside functions (e.g., when
13062 a function declares a class that has methods). This means that
13063 when we finish processing a function scope, we may need to go
13064 back to building a containing block's symbol lists. */
13065 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13066 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13067
13068 /* If we've finished processing a top-level function, subsequent
13069 symbols go in the file symbol list. */
13070 if (cu->get_builder ()->outermost_context_p ())
13071 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13072 }
13073
13074 /* Process all the DIES contained within a lexical block scope. Start
13075 a new scope, process the dies, and then close the scope. */
13076
13077 static void
13078 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13079 {
13080 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13081 struct gdbarch *gdbarch = objfile->arch ();
13082 CORE_ADDR lowpc, highpc;
13083 struct die_info *child_die;
13084 CORE_ADDR baseaddr;
13085
13086 baseaddr = objfile->text_section_offset ();
13087
13088 /* Ignore blocks with missing or invalid low and high pc attributes. */
13089 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13090 as multiple lexical blocks? Handling children in a sane way would
13091 be nasty. Might be easier to properly extend generic blocks to
13092 describe ranges. */
13093 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13094 {
13095 case PC_BOUNDS_NOT_PRESENT:
13096 /* DW_TAG_lexical_block has no attributes, process its children as if
13097 there was no wrapping by that DW_TAG_lexical_block.
13098 GCC does no longer produces such DWARF since GCC r224161. */
13099 for (child_die = die->child;
13100 child_die != NULL && child_die->tag;
13101 child_die = child_die->sibling)
13102 process_die (child_die, cu);
13103 return;
13104 case PC_BOUNDS_INVALID:
13105 return;
13106 }
13107 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13108 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13109
13110 cu->get_builder ()->push_context (0, lowpc);
13111 if (die->child != NULL)
13112 {
13113 child_die = die->child;
13114 while (child_die && child_die->tag)
13115 {
13116 process_die (child_die, cu);
13117 child_die = child_die->sibling;
13118 }
13119 }
13120 inherit_abstract_dies (die, cu);
13121 struct context_stack cstk = cu->get_builder ()->pop_context ();
13122
13123 if (*cu->get_builder ()->get_local_symbols () != NULL
13124 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13125 {
13126 struct block *block
13127 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13128 cstk.start_addr, highpc);
13129
13130 /* Note that recording ranges after traversing children, as we
13131 do here, means that recording a parent's ranges entails
13132 walking across all its children's ranges as they appear in
13133 the address map, which is quadratic behavior.
13134
13135 It would be nicer to record the parent's ranges before
13136 traversing its children, simply overriding whatever you find
13137 there. But since we don't even decide whether to create a
13138 block until after we've traversed its children, that's hard
13139 to do. */
13140 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13141 }
13142 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13143 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13144 }
13145
13146 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13147
13148 static void
13149 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13150 {
13151 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13152 struct gdbarch *gdbarch = objfile->arch ();
13153 CORE_ADDR pc, baseaddr;
13154 struct attribute *attr;
13155 struct call_site *call_site, call_site_local;
13156 void **slot;
13157 int nparams;
13158 struct die_info *child_die;
13159
13160 baseaddr = objfile->text_section_offset ();
13161
13162 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13163 if (attr == NULL)
13164 {
13165 /* This was a pre-DWARF-5 GNU extension alias
13166 for DW_AT_call_return_pc. */
13167 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13168 }
13169 if (!attr)
13170 {
13171 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13172 "DIE %s [in module %s]"),
13173 sect_offset_str (die->sect_off), objfile_name (objfile));
13174 return;
13175 }
13176 pc = attr->value_as_address () + baseaddr;
13177 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13178
13179 if (cu->call_site_htab == NULL)
13180 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13181 NULL, &objfile->objfile_obstack,
13182 hashtab_obstack_allocate, NULL);
13183 call_site_local.pc = pc;
13184 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13185 if (*slot != NULL)
13186 {
13187 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13188 "DIE %s [in module %s]"),
13189 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13190 objfile_name (objfile));
13191 return;
13192 }
13193
13194 /* Count parameters at the caller. */
13195
13196 nparams = 0;
13197 for (child_die = die->child; child_die && child_die->tag;
13198 child_die = child_die->sibling)
13199 {
13200 if (child_die->tag != DW_TAG_call_site_parameter
13201 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13202 {
13203 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13204 "DW_TAG_call_site child DIE %s [in module %s]"),
13205 child_die->tag, sect_offset_str (child_die->sect_off),
13206 objfile_name (objfile));
13207 continue;
13208 }
13209
13210 nparams++;
13211 }
13212
13213 call_site
13214 = ((struct call_site *)
13215 obstack_alloc (&objfile->objfile_obstack,
13216 sizeof (*call_site)
13217 + (sizeof (*call_site->parameter) * (nparams - 1))));
13218 *slot = call_site;
13219 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13220 call_site->pc = pc;
13221
13222 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13223 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13224 {
13225 struct die_info *func_die;
13226
13227 /* Skip also over DW_TAG_inlined_subroutine. */
13228 for (func_die = die->parent;
13229 func_die && func_die->tag != DW_TAG_subprogram
13230 && func_die->tag != DW_TAG_subroutine_type;
13231 func_die = func_die->parent);
13232
13233 /* DW_AT_call_all_calls is a superset
13234 of DW_AT_call_all_tail_calls. */
13235 if (func_die
13236 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13237 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13238 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13239 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13240 {
13241 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13242 not complete. But keep CALL_SITE for look ups via call_site_htab,
13243 both the initial caller containing the real return address PC and
13244 the final callee containing the current PC of a chain of tail
13245 calls do not need to have the tail call list complete. But any
13246 function candidate for a virtual tail call frame searched via
13247 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13248 determined unambiguously. */
13249 }
13250 else
13251 {
13252 struct type *func_type = NULL;
13253
13254 if (func_die)
13255 func_type = get_die_type (func_die, cu);
13256 if (func_type != NULL)
13257 {
13258 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13259
13260 /* Enlist this call site to the function. */
13261 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13262 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13263 }
13264 else
13265 complaint (_("Cannot find function owning DW_TAG_call_site "
13266 "DIE %s [in module %s]"),
13267 sect_offset_str (die->sect_off), objfile_name (objfile));
13268 }
13269 }
13270
13271 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13272 if (attr == NULL)
13273 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13274 if (attr == NULL)
13275 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13276 if (attr == NULL)
13277 {
13278 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13279 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13280 }
13281 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13282 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13283 /* Keep NULL DWARF_BLOCK. */;
13284 else if (attr->form_is_block ())
13285 {
13286 struct dwarf2_locexpr_baton *dlbaton;
13287
13288 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13289 dlbaton->data = DW_BLOCK (attr)->data;
13290 dlbaton->size = DW_BLOCK (attr)->size;
13291 dlbaton->per_cu = cu->per_cu;
13292
13293 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13294 }
13295 else if (attr->form_is_ref ())
13296 {
13297 struct dwarf2_cu *target_cu = cu;
13298 struct die_info *target_die;
13299
13300 target_die = follow_die_ref (die, attr, &target_cu);
13301 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13302 if (die_is_declaration (target_die, target_cu))
13303 {
13304 const char *target_physname;
13305
13306 /* Prefer the mangled name; otherwise compute the demangled one. */
13307 target_physname = dw2_linkage_name (target_die, target_cu);
13308 if (target_physname == NULL)
13309 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13310 if (target_physname == NULL)
13311 complaint (_("DW_AT_call_target target DIE has invalid "
13312 "physname, for referencing DIE %s [in module %s]"),
13313 sect_offset_str (die->sect_off), objfile_name (objfile));
13314 else
13315 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13316 }
13317 else
13318 {
13319 CORE_ADDR lowpc;
13320
13321 /* DW_AT_entry_pc should be preferred. */
13322 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13323 <= PC_BOUNDS_INVALID)
13324 complaint (_("DW_AT_call_target target DIE has invalid "
13325 "low pc, for referencing DIE %s [in module %s]"),
13326 sect_offset_str (die->sect_off), objfile_name (objfile));
13327 else
13328 {
13329 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13330 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13331 }
13332 }
13333 }
13334 else
13335 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13336 "block nor reference, for DIE %s [in module %s]"),
13337 sect_offset_str (die->sect_off), objfile_name (objfile));
13338
13339 call_site->per_cu = cu->per_cu;
13340
13341 for (child_die = die->child;
13342 child_die && child_die->tag;
13343 child_die = child_die->sibling)
13344 {
13345 struct call_site_parameter *parameter;
13346 struct attribute *loc, *origin;
13347
13348 if (child_die->tag != DW_TAG_call_site_parameter
13349 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13350 {
13351 /* Already printed the complaint above. */
13352 continue;
13353 }
13354
13355 gdb_assert (call_site->parameter_count < nparams);
13356 parameter = &call_site->parameter[call_site->parameter_count];
13357
13358 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13359 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13360 register is contained in DW_AT_call_value. */
13361
13362 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13363 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13364 if (origin == NULL)
13365 {
13366 /* This was a pre-DWARF-5 GNU extension alias
13367 for DW_AT_call_parameter. */
13368 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13369 }
13370 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13371 {
13372 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13373
13374 sect_offset sect_off = origin->get_ref_die_offset ();
13375 if (!cu->header.offset_in_cu_p (sect_off))
13376 {
13377 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13378 binding can be done only inside one CU. Such referenced DIE
13379 therefore cannot be even moved to DW_TAG_partial_unit. */
13380 complaint (_("DW_AT_call_parameter offset is not in CU for "
13381 "DW_TAG_call_site child DIE %s [in module %s]"),
13382 sect_offset_str (child_die->sect_off),
13383 objfile_name (objfile));
13384 continue;
13385 }
13386 parameter->u.param_cu_off
13387 = (cu_offset) (sect_off - cu->header.sect_off);
13388 }
13389 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13390 {
13391 complaint (_("No DW_FORM_block* DW_AT_location for "
13392 "DW_TAG_call_site child DIE %s [in module %s]"),
13393 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13394 continue;
13395 }
13396 else
13397 {
13398 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13399 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13400 if (parameter->u.dwarf_reg != -1)
13401 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13402 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13403 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13404 &parameter->u.fb_offset))
13405 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13406 else
13407 {
13408 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13409 "for DW_FORM_block* DW_AT_location is supported for "
13410 "DW_TAG_call_site child DIE %s "
13411 "[in module %s]"),
13412 sect_offset_str (child_die->sect_off),
13413 objfile_name (objfile));
13414 continue;
13415 }
13416 }
13417
13418 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13419 if (attr == NULL)
13420 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13421 if (attr == NULL || !attr->form_is_block ())
13422 {
13423 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13424 "DW_TAG_call_site child DIE %s [in module %s]"),
13425 sect_offset_str (child_die->sect_off),
13426 objfile_name (objfile));
13427 continue;
13428 }
13429 parameter->value = DW_BLOCK (attr)->data;
13430 parameter->value_size = DW_BLOCK (attr)->size;
13431
13432 /* Parameters are not pre-cleared by memset above. */
13433 parameter->data_value = NULL;
13434 parameter->data_value_size = 0;
13435 call_site->parameter_count++;
13436
13437 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13438 if (attr == NULL)
13439 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13440 if (attr != nullptr)
13441 {
13442 if (!attr->form_is_block ())
13443 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13444 "DW_TAG_call_site child DIE %s [in module %s]"),
13445 sect_offset_str (child_die->sect_off),
13446 objfile_name (objfile));
13447 else
13448 {
13449 parameter->data_value = DW_BLOCK (attr)->data;
13450 parameter->data_value_size = DW_BLOCK (attr)->size;
13451 }
13452 }
13453 }
13454 }
13455
13456 /* Helper function for read_variable. If DIE represents a virtual
13457 table, then return the type of the concrete object that is
13458 associated with the virtual table. Otherwise, return NULL. */
13459
13460 static struct type *
13461 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13462 {
13463 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13464 if (attr == NULL)
13465 return NULL;
13466
13467 /* Find the type DIE. */
13468 struct die_info *type_die = NULL;
13469 struct dwarf2_cu *type_cu = cu;
13470
13471 if (attr->form_is_ref ())
13472 type_die = follow_die_ref (die, attr, &type_cu);
13473 if (type_die == NULL)
13474 return NULL;
13475
13476 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13477 return NULL;
13478 return die_containing_type (type_die, type_cu);
13479 }
13480
13481 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13482
13483 static void
13484 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13485 {
13486 struct rust_vtable_symbol *storage = NULL;
13487
13488 if (cu->language == language_rust)
13489 {
13490 struct type *containing_type = rust_containing_type (die, cu);
13491
13492 if (containing_type != NULL)
13493 {
13494 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13495
13496 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13497 initialize_objfile_symbol (storage);
13498 storage->concrete_type = containing_type;
13499 storage->subclass = SYMBOL_RUST_VTABLE;
13500 }
13501 }
13502
13503 struct symbol *res = new_symbol (die, NULL, cu, storage);
13504 struct attribute *abstract_origin
13505 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13506 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13507 if (res == NULL && loc && abstract_origin)
13508 {
13509 /* We have a variable without a name, but with a location and an abstract
13510 origin. This may be a concrete instance of an abstract variable
13511 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13512 later. */
13513 struct dwarf2_cu *origin_cu = cu;
13514 struct die_info *origin_die
13515 = follow_die_ref (die, abstract_origin, &origin_cu);
13516 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13517 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13518 }
13519 }
13520
13521 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13522 reading .debug_rnglists.
13523 Callback's type should be:
13524 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13525 Return true if the attributes are present and valid, otherwise,
13526 return false. */
13527
13528 template <typename Callback>
13529 static bool
13530 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13531 Callback &&callback)
13532 {
13533 struct dwarf2_per_objfile *dwarf2_per_objfile
13534 = cu->per_cu->dwarf2_per_objfile;
13535 struct objfile *objfile = dwarf2_per_objfile->objfile;
13536 bfd *obfd = objfile->obfd;
13537 /* Base address selection entry. */
13538 gdb::optional<CORE_ADDR> base;
13539 const gdb_byte *buffer;
13540 CORE_ADDR baseaddr;
13541 bool overflow = false;
13542
13543 base = cu->base_address;
13544
13545 dwarf2_per_objfile->rnglists.read (objfile);
13546 if (offset >= dwarf2_per_objfile->rnglists.size)
13547 {
13548 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13549 offset);
13550 return false;
13551 }
13552 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13553
13554 baseaddr = objfile->text_section_offset ();
13555
13556 while (1)
13557 {
13558 /* Initialize it due to a false compiler warning. */
13559 CORE_ADDR range_beginning = 0, range_end = 0;
13560 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13561 + dwarf2_per_objfile->rnglists.size);
13562 unsigned int bytes_read;
13563
13564 if (buffer == buf_end)
13565 {
13566 overflow = true;
13567 break;
13568 }
13569 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13570 switch (rlet)
13571 {
13572 case DW_RLE_end_of_list:
13573 break;
13574 case DW_RLE_base_address:
13575 if (buffer + cu->header.addr_size > buf_end)
13576 {
13577 overflow = true;
13578 break;
13579 }
13580 base = cu->header.read_address (obfd, buffer, &bytes_read);
13581 buffer += bytes_read;
13582 break;
13583 case DW_RLE_start_length:
13584 if (buffer + cu->header.addr_size > buf_end)
13585 {
13586 overflow = true;
13587 break;
13588 }
13589 range_beginning = cu->header.read_address (obfd, buffer,
13590 &bytes_read);
13591 buffer += bytes_read;
13592 range_end = (range_beginning
13593 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13594 buffer += bytes_read;
13595 if (buffer > buf_end)
13596 {
13597 overflow = true;
13598 break;
13599 }
13600 break;
13601 case DW_RLE_offset_pair:
13602 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13603 buffer += bytes_read;
13604 if (buffer > buf_end)
13605 {
13606 overflow = true;
13607 break;
13608 }
13609 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13610 buffer += bytes_read;
13611 if (buffer > buf_end)
13612 {
13613 overflow = true;
13614 break;
13615 }
13616 break;
13617 case DW_RLE_start_end:
13618 if (buffer + 2 * cu->header.addr_size > buf_end)
13619 {
13620 overflow = true;
13621 break;
13622 }
13623 range_beginning = cu->header.read_address (obfd, buffer,
13624 &bytes_read);
13625 buffer += bytes_read;
13626 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13627 buffer += bytes_read;
13628 break;
13629 default:
13630 complaint (_("Invalid .debug_rnglists data (no base address)"));
13631 return false;
13632 }
13633 if (rlet == DW_RLE_end_of_list || overflow)
13634 break;
13635 if (rlet == DW_RLE_base_address)
13636 continue;
13637
13638 if (!base.has_value ())
13639 {
13640 /* We have no valid base address for the ranges
13641 data. */
13642 complaint (_("Invalid .debug_rnglists data (no base address)"));
13643 return false;
13644 }
13645
13646 if (range_beginning > range_end)
13647 {
13648 /* Inverted range entries are invalid. */
13649 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13650 return false;
13651 }
13652
13653 /* Empty range entries have no effect. */
13654 if (range_beginning == range_end)
13655 continue;
13656
13657 range_beginning += *base;
13658 range_end += *base;
13659
13660 /* A not-uncommon case of bad debug info.
13661 Don't pollute the addrmap with bad data. */
13662 if (range_beginning + baseaddr == 0
13663 && !dwarf2_per_objfile->has_section_at_zero)
13664 {
13665 complaint (_(".debug_rnglists entry has start address of zero"
13666 " [in module %s]"), objfile_name (objfile));
13667 continue;
13668 }
13669
13670 callback (range_beginning, range_end);
13671 }
13672
13673 if (overflow)
13674 {
13675 complaint (_("Offset %d is not terminated "
13676 "for DW_AT_ranges attribute"),
13677 offset);
13678 return false;
13679 }
13680
13681 return true;
13682 }
13683
13684 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13685 Callback's type should be:
13686 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13687 Return 1 if the attributes are present and valid, otherwise, return 0. */
13688
13689 template <typename Callback>
13690 static int
13691 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13692 Callback &&callback)
13693 {
13694 struct dwarf2_per_objfile *dwarf2_per_objfile
13695 = cu->per_cu->dwarf2_per_objfile;
13696 struct objfile *objfile = dwarf2_per_objfile->objfile;
13697 struct comp_unit_head *cu_header = &cu->header;
13698 bfd *obfd = objfile->obfd;
13699 unsigned int addr_size = cu_header->addr_size;
13700 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13701 /* Base address selection entry. */
13702 gdb::optional<CORE_ADDR> base;
13703 unsigned int dummy;
13704 const gdb_byte *buffer;
13705 CORE_ADDR baseaddr;
13706
13707 if (cu_header->version >= 5)
13708 return dwarf2_rnglists_process (offset, cu, callback);
13709
13710 base = cu->base_address;
13711
13712 dwarf2_per_objfile->ranges.read (objfile);
13713 if (offset >= dwarf2_per_objfile->ranges.size)
13714 {
13715 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13716 offset);
13717 return 0;
13718 }
13719 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13720
13721 baseaddr = objfile->text_section_offset ();
13722
13723 while (1)
13724 {
13725 CORE_ADDR range_beginning, range_end;
13726
13727 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13728 buffer += addr_size;
13729 range_end = cu->header.read_address (obfd, buffer, &dummy);
13730 buffer += addr_size;
13731 offset += 2 * addr_size;
13732
13733 /* An end of list marker is a pair of zero addresses. */
13734 if (range_beginning == 0 && range_end == 0)
13735 /* Found the end of list entry. */
13736 break;
13737
13738 /* Each base address selection entry is a pair of 2 values.
13739 The first is the largest possible address, the second is
13740 the base address. Check for a base address here. */
13741 if ((range_beginning & mask) == mask)
13742 {
13743 /* If we found the largest possible address, then we already
13744 have the base address in range_end. */
13745 base = range_end;
13746 continue;
13747 }
13748
13749 if (!base.has_value ())
13750 {
13751 /* We have no valid base address for the ranges
13752 data. */
13753 complaint (_("Invalid .debug_ranges data (no base address)"));
13754 return 0;
13755 }
13756
13757 if (range_beginning > range_end)
13758 {
13759 /* Inverted range entries are invalid. */
13760 complaint (_("Invalid .debug_ranges data (inverted range)"));
13761 return 0;
13762 }
13763
13764 /* Empty range entries have no effect. */
13765 if (range_beginning == range_end)
13766 continue;
13767
13768 range_beginning += *base;
13769 range_end += *base;
13770
13771 /* A not-uncommon case of bad debug info.
13772 Don't pollute the addrmap with bad data. */
13773 if (range_beginning + baseaddr == 0
13774 && !dwarf2_per_objfile->has_section_at_zero)
13775 {
13776 complaint (_(".debug_ranges entry has start address of zero"
13777 " [in module %s]"), objfile_name (objfile));
13778 continue;
13779 }
13780
13781 callback (range_beginning, range_end);
13782 }
13783
13784 return 1;
13785 }
13786
13787 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13788 Return 1 if the attributes are present and valid, otherwise, return 0.
13789 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13790
13791 static int
13792 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13793 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13794 dwarf2_psymtab *ranges_pst)
13795 {
13796 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13797 struct gdbarch *gdbarch = objfile->arch ();
13798 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13799 int low_set = 0;
13800 CORE_ADDR low = 0;
13801 CORE_ADDR high = 0;
13802 int retval;
13803
13804 retval = dwarf2_ranges_process (offset, cu,
13805 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13806 {
13807 if (ranges_pst != NULL)
13808 {
13809 CORE_ADDR lowpc;
13810 CORE_ADDR highpc;
13811
13812 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13813 range_beginning + baseaddr)
13814 - baseaddr);
13815 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13816 range_end + baseaddr)
13817 - baseaddr);
13818 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13819 lowpc, highpc - 1, ranges_pst);
13820 }
13821
13822 /* FIXME: This is recording everything as a low-high
13823 segment of consecutive addresses. We should have a
13824 data structure for discontiguous block ranges
13825 instead. */
13826 if (! low_set)
13827 {
13828 low = range_beginning;
13829 high = range_end;
13830 low_set = 1;
13831 }
13832 else
13833 {
13834 if (range_beginning < low)
13835 low = range_beginning;
13836 if (range_end > high)
13837 high = range_end;
13838 }
13839 });
13840 if (!retval)
13841 return 0;
13842
13843 if (! low_set)
13844 /* If the first entry is an end-of-list marker, the range
13845 describes an empty scope, i.e. no instructions. */
13846 return 0;
13847
13848 if (low_return)
13849 *low_return = low;
13850 if (high_return)
13851 *high_return = high;
13852 return 1;
13853 }
13854
13855 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13856 definition for the return value. *LOWPC and *HIGHPC are set iff
13857 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13858
13859 static enum pc_bounds_kind
13860 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13861 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13862 dwarf2_psymtab *pst)
13863 {
13864 struct dwarf2_per_objfile *dwarf2_per_objfile
13865 = cu->per_cu->dwarf2_per_objfile;
13866 struct attribute *attr;
13867 struct attribute *attr_high;
13868 CORE_ADDR low = 0;
13869 CORE_ADDR high = 0;
13870 enum pc_bounds_kind ret;
13871
13872 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13873 if (attr_high)
13874 {
13875 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13876 if (attr != nullptr)
13877 {
13878 low = attr->value_as_address ();
13879 high = attr_high->value_as_address ();
13880 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13881 high += low;
13882 }
13883 else
13884 /* Found high w/o low attribute. */
13885 return PC_BOUNDS_INVALID;
13886
13887 /* Found consecutive range of addresses. */
13888 ret = PC_BOUNDS_HIGH_LOW;
13889 }
13890 else
13891 {
13892 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13893 if (attr != NULL)
13894 {
13895 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13896 We take advantage of the fact that DW_AT_ranges does not appear
13897 in DW_TAG_compile_unit of DWO files. */
13898 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13899 unsigned int ranges_offset = (DW_UNSND (attr)
13900 + (need_ranges_base
13901 ? cu->ranges_base
13902 : 0));
13903
13904 /* Value of the DW_AT_ranges attribute is the offset in the
13905 .debug_ranges section. */
13906 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13907 return PC_BOUNDS_INVALID;
13908 /* Found discontinuous range of addresses. */
13909 ret = PC_BOUNDS_RANGES;
13910 }
13911 else
13912 return PC_BOUNDS_NOT_PRESENT;
13913 }
13914
13915 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13916 if (high <= low)
13917 return PC_BOUNDS_INVALID;
13918
13919 /* When using the GNU linker, .gnu.linkonce. sections are used to
13920 eliminate duplicate copies of functions and vtables and such.
13921 The linker will arbitrarily choose one and discard the others.
13922 The AT_*_pc values for such functions refer to local labels in
13923 these sections. If the section from that file was discarded, the
13924 labels are not in the output, so the relocs get a value of 0.
13925 If this is a discarded function, mark the pc bounds as invalid,
13926 so that GDB will ignore it. */
13927 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13928 return PC_BOUNDS_INVALID;
13929
13930 *lowpc = low;
13931 if (highpc)
13932 *highpc = high;
13933 return ret;
13934 }
13935
13936 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13937 its low and high PC addresses. Do nothing if these addresses could not
13938 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13939 and HIGHPC to the high address if greater than HIGHPC. */
13940
13941 static void
13942 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13943 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13944 struct dwarf2_cu *cu)
13945 {
13946 CORE_ADDR low, high;
13947 struct die_info *child = die->child;
13948
13949 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13950 {
13951 *lowpc = std::min (*lowpc, low);
13952 *highpc = std::max (*highpc, high);
13953 }
13954
13955 /* If the language does not allow nested subprograms (either inside
13956 subprograms or lexical blocks), we're done. */
13957 if (cu->language != language_ada)
13958 return;
13959
13960 /* Check all the children of the given DIE. If it contains nested
13961 subprograms, then check their pc bounds. Likewise, we need to
13962 check lexical blocks as well, as they may also contain subprogram
13963 definitions. */
13964 while (child && child->tag)
13965 {
13966 if (child->tag == DW_TAG_subprogram
13967 || child->tag == DW_TAG_lexical_block)
13968 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13969 child = child->sibling;
13970 }
13971 }
13972
13973 /* Get the low and high pc's represented by the scope DIE, and store
13974 them in *LOWPC and *HIGHPC. If the correct values can't be
13975 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13976
13977 static void
13978 get_scope_pc_bounds (struct die_info *die,
13979 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13980 struct dwarf2_cu *cu)
13981 {
13982 CORE_ADDR best_low = (CORE_ADDR) -1;
13983 CORE_ADDR best_high = (CORE_ADDR) 0;
13984 CORE_ADDR current_low, current_high;
13985
13986 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13987 >= PC_BOUNDS_RANGES)
13988 {
13989 best_low = current_low;
13990 best_high = current_high;
13991 }
13992 else
13993 {
13994 struct die_info *child = die->child;
13995
13996 while (child && child->tag)
13997 {
13998 switch (child->tag) {
13999 case DW_TAG_subprogram:
14000 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14001 break;
14002 case DW_TAG_namespace:
14003 case DW_TAG_module:
14004 /* FIXME: carlton/2004-01-16: Should we do this for
14005 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14006 that current GCC's always emit the DIEs corresponding
14007 to definitions of methods of classes as children of a
14008 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14009 the DIEs giving the declarations, which could be
14010 anywhere). But I don't see any reason why the
14011 standards says that they have to be there. */
14012 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14013
14014 if (current_low != ((CORE_ADDR) -1))
14015 {
14016 best_low = std::min (best_low, current_low);
14017 best_high = std::max (best_high, current_high);
14018 }
14019 break;
14020 default:
14021 /* Ignore. */
14022 break;
14023 }
14024
14025 child = child->sibling;
14026 }
14027 }
14028
14029 *lowpc = best_low;
14030 *highpc = best_high;
14031 }
14032
14033 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14034 in DIE. */
14035
14036 static void
14037 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14038 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14039 {
14040 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14041 struct gdbarch *gdbarch = objfile->arch ();
14042 struct attribute *attr;
14043 struct attribute *attr_high;
14044
14045 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14046 if (attr_high)
14047 {
14048 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14049 if (attr != nullptr)
14050 {
14051 CORE_ADDR low = attr->value_as_address ();
14052 CORE_ADDR high = attr_high->value_as_address ();
14053
14054 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14055 high += low;
14056
14057 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14058 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14059 cu->get_builder ()->record_block_range (block, low, high - 1);
14060 }
14061 }
14062
14063 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14064 if (attr != nullptr)
14065 {
14066 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14067 We take advantage of the fact that DW_AT_ranges does not appear
14068 in DW_TAG_compile_unit of DWO files. */
14069 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14070
14071 /* The value of the DW_AT_ranges attribute is the offset of the
14072 address range list in the .debug_ranges section. */
14073 unsigned long offset = (DW_UNSND (attr)
14074 + (need_ranges_base ? cu->ranges_base : 0));
14075
14076 std::vector<blockrange> blockvec;
14077 dwarf2_ranges_process (offset, cu,
14078 [&] (CORE_ADDR start, CORE_ADDR end)
14079 {
14080 start += baseaddr;
14081 end += baseaddr;
14082 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14083 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14084 cu->get_builder ()->record_block_range (block, start, end - 1);
14085 blockvec.emplace_back (start, end);
14086 });
14087
14088 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14089 }
14090 }
14091
14092 /* Check whether the producer field indicates either of GCC < 4.6, or the
14093 Intel C/C++ compiler, and cache the result in CU. */
14094
14095 static void
14096 check_producer (struct dwarf2_cu *cu)
14097 {
14098 int major, minor;
14099
14100 if (cu->producer == NULL)
14101 {
14102 /* For unknown compilers expect their behavior is DWARF version
14103 compliant.
14104
14105 GCC started to support .debug_types sections by -gdwarf-4 since
14106 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14107 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14108 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14109 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14110 }
14111 else if (producer_is_gcc (cu->producer, &major, &minor))
14112 {
14113 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14114 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14115 }
14116 else if (producer_is_icc (cu->producer, &major, &minor))
14117 {
14118 cu->producer_is_icc = true;
14119 cu->producer_is_icc_lt_14 = major < 14;
14120 }
14121 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14122 cu->producer_is_codewarrior = true;
14123 else
14124 {
14125 /* For other non-GCC compilers, expect their behavior is DWARF version
14126 compliant. */
14127 }
14128
14129 cu->checked_producer = true;
14130 }
14131
14132 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14133 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14134 during 4.6.0 experimental. */
14135
14136 static bool
14137 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14138 {
14139 if (!cu->checked_producer)
14140 check_producer (cu);
14141
14142 return cu->producer_is_gxx_lt_4_6;
14143 }
14144
14145
14146 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14147 with incorrect is_stmt attributes. */
14148
14149 static bool
14150 producer_is_codewarrior (struct dwarf2_cu *cu)
14151 {
14152 if (!cu->checked_producer)
14153 check_producer (cu);
14154
14155 return cu->producer_is_codewarrior;
14156 }
14157
14158 /* Return the default accessibility type if it is not overridden by
14159 DW_AT_accessibility. */
14160
14161 static enum dwarf_access_attribute
14162 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14163 {
14164 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14165 {
14166 /* The default DWARF 2 accessibility for members is public, the default
14167 accessibility for inheritance is private. */
14168
14169 if (die->tag != DW_TAG_inheritance)
14170 return DW_ACCESS_public;
14171 else
14172 return DW_ACCESS_private;
14173 }
14174 else
14175 {
14176 /* DWARF 3+ defines the default accessibility a different way. The same
14177 rules apply now for DW_TAG_inheritance as for the members and it only
14178 depends on the container kind. */
14179
14180 if (die->parent->tag == DW_TAG_class_type)
14181 return DW_ACCESS_private;
14182 else
14183 return DW_ACCESS_public;
14184 }
14185 }
14186
14187 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14188 offset. If the attribute was not found return 0, otherwise return
14189 1. If it was found but could not properly be handled, set *OFFSET
14190 to 0. */
14191
14192 static int
14193 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14194 LONGEST *offset)
14195 {
14196 struct attribute *attr;
14197
14198 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14199 if (attr != NULL)
14200 {
14201 *offset = 0;
14202
14203 /* Note that we do not check for a section offset first here.
14204 This is because DW_AT_data_member_location is new in DWARF 4,
14205 so if we see it, we can assume that a constant form is really
14206 a constant and not a section offset. */
14207 if (attr->form_is_constant ())
14208 *offset = attr->constant_value (0);
14209 else if (attr->form_is_section_offset ())
14210 dwarf2_complex_location_expr_complaint ();
14211 else if (attr->form_is_block ())
14212 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14213 else
14214 dwarf2_complex_location_expr_complaint ();
14215
14216 return 1;
14217 }
14218
14219 return 0;
14220 }
14221
14222 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14223
14224 static void
14225 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14226 struct field *field)
14227 {
14228 struct attribute *attr;
14229
14230 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14231 if (attr != NULL)
14232 {
14233 if (attr->form_is_constant ())
14234 {
14235 LONGEST offset = attr->constant_value (0);
14236 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14237 }
14238 else if (attr->form_is_section_offset ())
14239 dwarf2_complex_location_expr_complaint ();
14240 else if (attr->form_is_block ())
14241 {
14242 bool handled;
14243 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14244 if (handled)
14245 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14246 else
14247 {
14248 struct objfile *objfile
14249 = cu->per_cu->dwarf2_per_objfile->objfile;
14250 struct dwarf2_locexpr_baton *dlbaton
14251 = XOBNEW (&objfile->objfile_obstack,
14252 struct dwarf2_locexpr_baton);
14253 dlbaton->data = DW_BLOCK (attr)->data;
14254 dlbaton->size = DW_BLOCK (attr)->size;
14255 /* When using this baton, we want to compute the address
14256 of the field, not the value. This is why
14257 is_reference is set to false here. */
14258 dlbaton->is_reference = false;
14259 dlbaton->per_cu = cu->per_cu;
14260
14261 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14262 }
14263 }
14264 else
14265 dwarf2_complex_location_expr_complaint ();
14266 }
14267 }
14268
14269 /* Add an aggregate field to the field list. */
14270
14271 static void
14272 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14273 struct dwarf2_cu *cu)
14274 {
14275 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14276 struct gdbarch *gdbarch = objfile->arch ();
14277 struct nextfield *new_field;
14278 struct attribute *attr;
14279 struct field *fp;
14280 const char *fieldname = "";
14281
14282 if (die->tag == DW_TAG_inheritance)
14283 {
14284 fip->baseclasses.emplace_back ();
14285 new_field = &fip->baseclasses.back ();
14286 }
14287 else
14288 {
14289 fip->fields.emplace_back ();
14290 new_field = &fip->fields.back ();
14291 }
14292
14293 new_field->offset = die->sect_off;
14294
14295 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14296 if (attr != nullptr)
14297 new_field->accessibility = DW_UNSND (attr);
14298 else
14299 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14300 if (new_field->accessibility != DW_ACCESS_public)
14301 fip->non_public_fields = 1;
14302
14303 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14304 if (attr != nullptr)
14305 new_field->virtuality = DW_UNSND (attr);
14306 else
14307 new_field->virtuality = DW_VIRTUALITY_none;
14308
14309 fp = &new_field->field;
14310
14311 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14312 {
14313 /* Data member other than a C++ static data member. */
14314
14315 /* Get type of field. */
14316 fp->type = die_type (die, cu);
14317
14318 SET_FIELD_BITPOS (*fp, 0);
14319
14320 /* Get bit size of field (zero if none). */
14321 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14322 if (attr != nullptr)
14323 {
14324 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14325 }
14326 else
14327 {
14328 FIELD_BITSIZE (*fp) = 0;
14329 }
14330
14331 /* Get bit offset of field. */
14332 handle_data_member_location (die, cu, fp);
14333 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14334 if (attr != nullptr)
14335 {
14336 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14337 {
14338 /* For big endian bits, the DW_AT_bit_offset gives the
14339 additional bit offset from the MSB of the containing
14340 anonymous object to the MSB of the field. We don't
14341 have to do anything special since we don't need to
14342 know the size of the anonymous object. */
14343 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14344 }
14345 else
14346 {
14347 /* For little endian bits, compute the bit offset to the
14348 MSB of the anonymous object, subtract off the number of
14349 bits from the MSB of the field to the MSB of the
14350 object, and then subtract off the number of bits of
14351 the field itself. The result is the bit offset of
14352 the LSB of the field. */
14353 int anonymous_size;
14354 int bit_offset = DW_UNSND (attr);
14355
14356 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14357 if (attr != nullptr)
14358 {
14359 /* The size of the anonymous object containing
14360 the bit field is explicit, so use the
14361 indicated size (in bytes). */
14362 anonymous_size = DW_UNSND (attr);
14363 }
14364 else
14365 {
14366 /* The size of the anonymous object containing
14367 the bit field must be inferred from the type
14368 attribute of the data member containing the
14369 bit field. */
14370 anonymous_size = TYPE_LENGTH (fp->type);
14371 }
14372 SET_FIELD_BITPOS (*fp,
14373 (FIELD_BITPOS (*fp)
14374 + anonymous_size * bits_per_byte
14375 - bit_offset - FIELD_BITSIZE (*fp)));
14376 }
14377 }
14378 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14379 if (attr != NULL)
14380 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14381 + attr->constant_value (0)));
14382
14383 /* Get name of field. */
14384 fieldname = dwarf2_name (die, cu);
14385 if (fieldname == NULL)
14386 fieldname = "";
14387
14388 /* The name is already allocated along with this objfile, so we don't
14389 need to duplicate it for the type. */
14390 fp->name = fieldname;
14391
14392 /* Change accessibility for artificial fields (e.g. virtual table
14393 pointer or virtual base class pointer) to private. */
14394 if (dwarf2_attr (die, DW_AT_artificial, cu))
14395 {
14396 FIELD_ARTIFICIAL (*fp) = 1;
14397 new_field->accessibility = DW_ACCESS_private;
14398 fip->non_public_fields = 1;
14399 }
14400 }
14401 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14402 {
14403 /* C++ static member. */
14404
14405 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14406 is a declaration, but all versions of G++ as of this writing
14407 (so through at least 3.2.1) incorrectly generate
14408 DW_TAG_variable tags. */
14409
14410 const char *physname;
14411
14412 /* Get name of field. */
14413 fieldname = dwarf2_name (die, cu);
14414 if (fieldname == NULL)
14415 return;
14416
14417 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14418 if (attr
14419 /* Only create a symbol if this is an external value.
14420 new_symbol checks this and puts the value in the global symbol
14421 table, which we want. If it is not external, new_symbol
14422 will try to put the value in cu->list_in_scope which is wrong. */
14423 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14424 {
14425 /* A static const member, not much different than an enum as far as
14426 we're concerned, except that we can support more types. */
14427 new_symbol (die, NULL, cu);
14428 }
14429
14430 /* Get physical name. */
14431 physname = dwarf2_physname (fieldname, die, cu);
14432
14433 /* The name is already allocated along with this objfile, so we don't
14434 need to duplicate it for the type. */
14435 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14436 FIELD_TYPE (*fp) = die_type (die, cu);
14437 FIELD_NAME (*fp) = fieldname;
14438 }
14439 else if (die->tag == DW_TAG_inheritance)
14440 {
14441 /* C++ base class field. */
14442 handle_data_member_location (die, cu, fp);
14443 FIELD_BITSIZE (*fp) = 0;
14444 FIELD_TYPE (*fp) = die_type (die, cu);
14445 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14446 }
14447 else
14448 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14449 }
14450
14451 /* Can the type given by DIE define another type? */
14452
14453 static bool
14454 type_can_define_types (const struct die_info *die)
14455 {
14456 switch (die->tag)
14457 {
14458 case DW_TAG_typedef:
14459 case DW_TAG_class_type:
14460 case DW_TAG_structure_type:
14461 case DW_TAG_union_type:
14462 case DW_TAG_enumeration_type:
14463 return true;
14464
14465 default:
14466 return false;
14467 }
14468 }
14469
14470 /* Add a type definition defined in the scope of the FIP's class. */
14471
14472 static void
14473 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14474 struct dwarf2_cu *cu)
14475 {
14476 struct decl_field fp;
14477 memset (&fp, 0, sizeof (fp));
14478
14479 gdb_assert (type_can_define_types (die));
14480
14481 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14482 fp.name = dwarf2_name (die, cu);
14483 fp.type = read_type_die (die, cu);
14484
14485 /* Save accessibility. */
14486 enum dwarf_access_attribute accessibility;
14487 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14488 if (attr != NULL)
14489 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14490 else
14491 accessibility = dwarf2_default_access_attribute (die, cu);
14492 switch (accessibility)
14493 {
14494 case DW_ACCESS_public:
14495 /* The assumed value if neither private nor protected. */
14496 break;
14497 case DW_ACCESS_private:
14498 fp.is_private = 1;
14499 break;
14500 case DW_ACCESS_protected:
14501 fp.is_protected = 1;
14502 break;
14503 default:
14504 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14505 }
14506
14507 if (die->tag == DW_TAG_typedef)
14508 fip->typedef_field_list.push_back (fp);
14509 else
14510 fip->nested_types_list.push_back (fp);
14511 }
14512
14513 /* A convenience typedef that's used when finding the discriminant
14514 field for a variant part. */
14515 typedef std::unordered_map<sect_offset, int> offset_map_type;
14516
14517 /* Compute the discriminant range for a given variant. OBSTACK is
14518 where the results will be stored. VARIANT is the variant to
14519 process. IS_UNSIGNED indicates whether the discriminant is signed
14520 or unsigned. */
14521
14522 static const gdb::array_view<discriminant_range>
14523 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14524 bool is_unsigned)
14525 {
14526 std::vector<discriminant_range> ranges;
14527
14528 if (variant.default_branch)
14529 return {};
14530
14531 if (variant.discr_list_data == nullptr)
14532 {
14533 discriminant_range r
14534 = {variant.discriminant_value, variant.discriminant_value};
14535 ranges.push_back (r);
14536 }
14537 else
14538 {
14539 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14540 variant.discr_list_data->size);
14541 while (!data.empty ())
14542 {
14543 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14544 {
14545 complaint (_("invalid discriminant marker: %d"), data[0]);
14546 break;
14547 }
14548 bool is_range = data[0] == DW_DSC_range;
14549 data = data.slice (1);
14550
14551 ULONGEST low, high;
14552 unsigned int bytes_read;
14553
14554 if (data.empty ())
14555 {
14556 complaint (_("DW_AT_discr_list missing low value"));
14557 break;
14558 }
14559 if (is_unsigned)
14560 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14561 else
14562 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14563 &bytes_read);
14564 data = data.slice (bytes_read);
14565
14566 if (is_range)
14567 {
14568 if (data.empty ())
14569 {
14570 complaint (_("DW_AT_discr_list missing high value"));
14571 break;
14572 }
14573 if (is_unsigned)
14574 high = read_unsigned_leb128 (nullptr, data.data (),
14575 &bytes_read);
14576 else
14577 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14578 &bytes_read);
14579 data = data.slice (bytes_read);
14580 }
14581 else
14582 high = low;
14583
14584 ranges.push_back ({ low, high });
14585 }
14586 }
14587
14588 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14589 ranges.size ());
14590 std::copy (ranges.begin (), ranges.end (), result);
14591 return gdb::array_view<discriminant_range> (result, ranges.size ());
14592 }
14593
14594 static const gdb::array_view<variant_part> create_variant_parts
14595 (struct obstack *obstack,
14596 const offset_map_type &offset_map,
14597 struct field_info *fi,
14598 const std::vector<variant_part_builder> &variant_parts);
14599
14600 /* Fill in a "struct variant" for a given variant field. RESULT is
14601 the variant to fill in. OBSTACK is where any needed allocations
14602 will be done. OFFSET_MAP holds the mapping from section offsets to
14603 fields for the type. FI describes the fields of the type we're
14604 processing. FIELD is the variant field we're converting. */
14605
14606 static void
14607 create_one_variant (variant &result, struct obstack *obstack,
14608 const offset_map_type &offset_map,
14609 struct field_info *fi, const variant_field &field)
14610 {
14611 result.discriminants = convert_variant_range (obstack, field, false);
14612 result.first_field = field.first_field + fi->baseclasses.size ();
14613 result.last_field = field.last_field + fi->baseclasses.size ();
14614 result.parts = create_variant_parts (obstack, offset_map, fi,
14615 field.variant_parts);
14616 }
14617
14618 /* Fill in a "struct variant_part" for a given variant part. RESULT
14619 is the variant part to fill in. OBSTACK is where any needed
14620 allocations will be done. OFFSET_MAP holds the mapping from
14621 section offsets to fields for the type. FI describes the fields of
14622 the type we're processing. BUILDER is the variant part to be
14623 converted. */
14624
14625 static void
14626 create_one_variant_part (variant_part &result,
14627 struct obstack *obstack,
14628 const offset_map_type &offset_map,
14629 struct field_info *fi,
14630 const variant_part_builder &builder)
14631 {
14632 auto iter = offset_map.find (builder.discriminant_offset);
14633 if (iter == offset_map.end ())
14634 {
14635 result.discriminant_index = -1;
14636 /* Doesn't matter. */
14637 result.is_unsigned = false;
14638 }
14639 else
14640 {
14641 result.discriminant_index = iter->second;
14642 result.is_unsigned
14643 = TYPE_UNSIGNED (FIELD_TYPE
14644 (fi->fields[result.discriminant_index].field));
14645 }
14646
14647 size_t n = builder.variants.size ();
14648 variant *output = new (obstack) variant[n];
14649 for (size_t i = 0; i < n; ++i)
14650 create_one_variant (output[i], obstack, offset_map, fi,
14651 builder.variants[i]);
14652
14653 result.variants = gdb::array_view<variant> (output, n);
14654 }
14655
14656 /* Create a vector of variant parts that can be attached to a type.
14657 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14658 holds the mapping from section offsets to fields for the type. FI
14659 describes the fields of the type we're processing. VARIANT_PARTS
14660 is the vector to convert. */
14661
14662 static const gdb::array_view<variant_part>
14663 create_variant_parts (struct obstack *obstack,
14664 const offset_map_type &offset_map,
14665 struct field_info *fi,
14666 const std::vector<variant_part_builder> &variant_parts)
14667 {
14668 if (variant_parts.empty ())
14669 return {};
14670
14671 size_t n = variant_parts.size ();
14672 variant_part *result = new (obstack) variant_part[n];
14673 for (size_t i = 0; i < n; ++i)
14674 create_one_variant_part (result[i], obstack, offset_map, fi,
14675 variant_parts[i]);
14676
14677 return gdb::array_view<variant_part> (result, n);
14678 }
14679
14680 /* Compute the variant part vector for FIP, attaching it to TYPE when
14681 done. */
14682
14683 static void
14684 add_variant_property (struct field_info *fip, struct type *type,
14685 struct dwarf2_cu *cu)
14686 {
14687 /* Map section offsets of fields to their field index. Note the
14688 field index here does not take the number of baseclasses into
14689 account. */
14690 offset_map_type offset_map;
14691 for (int i = 0; i < fip->fields.size (); ++i)
14692 offset_map[fip->fields[i].offset] = i;
14693
14694 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14695 gdb::array_view<variant_part> parts
14696 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14697 fip->variant_parts);
14698
14699 struct dynamic_prop prop;
14700 prop.kind = PROP_VARIANT_PARTS;
14701 prop.data.variant_parts
14702 = ((gdb::array_view<variant_part> *)
14703 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14704
14705 add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop, type);
14706 }
14707
14708 /* Create the vector of fields, and attach it to the type. */
14709
14710 static void
14711 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14712 struct dwarf2_cu *cu)
14713 {
14714 int nfields = fip->nfields ();
14715
14716 /* Record the field count, allocate space for the array of fields,
14717 and create blank accessibility bitfields if necessary. */
14718 TYPE_NFIELDS (type) = nfields;
14719 TYPE_FIELDS (type) = (struct field *)
14720 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14721
14722 if (fip->non_public_fields && cu->language != language_ada)
14723 {
14724 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14725
14726 TYPE_FIELD_PRIVATE_BITS (type) =
14727 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14728 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14729
14730 TYPE_FIELD_PROTECTED_BITS (type) =
14731 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14732 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14733
14734 TYPE_FIELD_IGNORE_BITS (type) =
14735 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14736 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14737 }
14738
14739 /* If the type has baseclasses, allocate and clear a bit vector for
14740 TYPE_FIELD_VIRTUAL_BITS. */
14741 if (!fip->baseclasses.empty () && cu->language != language_ada)
14742 {
14743 int num_bytes = B_BYTES (fip->baseclasses.size ());
14744 unsigned char *pointer;
14745
14746 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14747 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14748 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14749 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14750 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14751 }
14752
14753 if (!fip->variant_parts.empty ())
14754 add_variant_property (fip, type, cu);
14755
14756 /* Copy the saved-up fields into the field vector. */
14757 for (int i = 0; i < nfields; ++i)
14758 {
14759 struct nextfield &field
14760 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14761 : fip->fields[i - fip->baseclasses.size ()]);
14762
14763 TYPE_FIELD (type, i) = field.field;
14764 switch (field.accessibility)
14765 {
14766 case DW_ACCESS_private:
14767 if (cu->language != language_ada)
14768 SET_TYPE_FIELD_PRIVATE (type, i);
14769 break;
14770
14771 case DW_ACCESS_protected:
14772 if (cu->language != language_ada)
14773 SET_TYPE_FIELD_PROTECTED (type, i);
14774 break;
14775
14776 case DW_ACCESS_public:
14777 break;
14778
14779 default:
14780 /* Unknown accessibility. Complain and treat it as public. */
14781 {
14782 complaint (_("unsupported accessibility %d"),
14783 field.accessibility);
14784 }
14785 break;
14786 }
14787 if (i < fip->baseclasses.size ())
14788 {
14789 switch (field.virtuality)
14790 {
14791 case DW_VIRTUALITY_virtual:
14792 case DW_VIRTUALITY_pure_virtual:
14793 if (cu->language == language_ada)
14794 error (_("unexpected virtuality in component of Ada type"));
14795 SET_TYPE_FIELD_VIRTUAL (type, i);
14796 break;
14797 }
14798 }
14799 }
14800 }
14801
14802 /* Return true if this member function is a constructor, false
14803 otherwise. */
14804
14805 static int
14806 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14807 {
14808 const char *fieldname;
14809 const char *type_name;
14810 int len;
14811
14812 if (die->parent == NULL)
14813 return 0;
14814
14815 if (die->parent->tag != DW_TAG_structure_type
14816 && die->parent->tag != DW_TAG_union_type
14817 && die->parent->tag != DW_TAG_class_type)
14818 return 0;
14819
14820 fieldname = dwarf2_name (die, cu);
14821 type_name = dwarf2_name (die->parent, cu);
14822 if (fieldname == NULL || type_name == NULL)
14823 return 0;
14824
14825 len = strlen (fieldname);
14826 return (strncmp (fieldname, type_name, len) == 0
14827 && (type_name[len] == '\0' || type_name[len] == '<'));
14828 }
14829
14830 /* Check if the given VALUE is a recognized enum
14831 dwarf_defaulted_attribute constant according to DWARF5 spec,
14832 Table 7.24. */
14833
14834 static bool
14835 is_valid_DW_AT_defaulted (ULONGEST value)
14836 {
14837 switch (value)
14838 {
14839 case DW_DEFAULTED_no:
14840 case DW_DEFAULTED_in_class:
14841 case DW_DEFAULTED_out_of_class:
14842 return true;
14843 }
14844
14845 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14846 return false;
14847 }
14848
14849 /* Add a member function to the proper fieldlist. */
14850
14851 static void
14852 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14853 struct type *type, struct dwarf2_cu *cu)
14854 {
14855 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14856 struct attribute *attr;
14857 int i;
14858 struct fnfieldlist *flp = nullptr;
14859 struct fn_field *fnp;
14860 const char *fieldname;
14861 struct type *this_type;
14862 enum dwarf_access_attribute accessibility;
14863
14864 if (cu->language == language_ada)
14865 error (_("unexpected member function in Ada type"));
14866
14867 /* Get name of member function. */
14868 fieldname = dwarf2_name (die, cu);
14869 if (fieldname == NULL)
14870 return;
14871
14872 /* Look up member function name in fieldlist. */
14873 for (i = 0; i < fip->fnfieldlists.size (); i++)
14874 {
14875 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14876 {
14877 flp = &fip->fnfieldlists[i];
14878 break;
14879 }
14880 }
14881
14882 /* Create a new fnfieldlist if necessary. */
14883 if (flp == nullptr)
14884 {
14885 fip->fnfieldlists.emplace_back ();
14886 flp = &fip->fnfieldlists.back ();
14887 flp->name = fieldname;
14888 i = fip->fnfieldlists.size () - 1;
14889 }
14890
14891 /* Create a new member function field and add it to the vector of
14892 fnfieldlists. */
14893 flp->fnfields.emplace_back ();
14894 fnp = &flp->fnfields.back ();
14895
14896 /* Delay processing of the physname until later. */
14897 if (cu->language == language_cplus)
14898 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14899 die, cu);
14900 else
14901 {
14902 const char *physname = dwarf2_physname (fieldname, die, cu);
14903 fnp->physname = physname ? physname : "";
14904 }
14905
14906 fnp->type = alloc_type (objfile);
14907 this_type = read_type_die (die, cu);
14908 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14909 {
14910 int nparams = TYPE_NFIELDS (this_type);
14911
14912 /* TYPE is the domain of this method, and THIS_TYPE is the type
14913 of the method itself (TYPE_CODE_METHOD). */
14914 smash_to_method_type (fnp->type, type,
14915 TYPE_TARGET_TYPE (this_type),
14916 TYPE_FIELDS (this_type),
14917 TYPE_NFIELDS (this_type),
14918 TYPE_VARARGS (this_type));
14919
14920 /* Handle static member functions.
14921 Dwarf2 has no clean way to discern C++ static and non-static
14922 member functions. G++ helps GDB by marking the first
14923 parameter for non-static member functions (which is the this
14924 pointer) as artificial. We obtain this information from
14925 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14926 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14927 fnp->voffset = VOFFSET_STATIC;
14928 }
14929 else
14930 complaint (_("member function type missing for '%s'"),
14931 dwarf2_full_name (fieldname, die, cu));
14932
14933 /* Get fcontext from DW_AT_containing_type if present. */
14934 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14935 fnp->fcontext = die_containing_type (die, cu);
14936
14937 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14938 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14939
14940 /* Get accessibility. */
14941 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14942 if (attr != nullptr)
14943 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14944 else
14945 accessibility = dwarf2_default_access_attribute (die, cu);
14946 switch (accessibility)
14947 {
14948 case DW_ACCESS_private:
14949 fnp->is_private = 1;
14950 break;
14951 case DW_ACCESS_protected:
14952 fnp->is_protected = 1;
14953 break;
14954 }
14955
14956 /* Check for artificial methods. */
14957 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14958 if (attr && DW_UNSND (attr) != 0)
14959 fnp->is_artificial = 1;
14960
14961 /* Check for defaulted methods. */
14962 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14963 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14964 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14965
14966 /* Check for deleted methods. */
14967 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14968 if (attr != nullptr && DW_UNSND (attr) != 0)
14969 fnp->is_deleted = 1;
14970
14971 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14972
14973 /* Get index in virtual function table if it is a virtual member
14974 function. For older versions of GCC, this is an offset in the
14975 appropriate virtual table, as specified by DW_AT_containing_type.
14976 For everyone else, it is an expression to be evaluated relative
14977 to the object address. */
14978
14979 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14980 if (attr != nullptr)
14981 {
14982 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14983 {
14984 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14985 {
14986 /* Old-style GCC. */
14987 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14988 }
14989 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14990 || (DW_BLOCK (attr)->size > 1
14991 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14992 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14993 {
14994 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14995 if ((fnp->voffset % cu->header.addr_size) != 0)
14996 dwarf2_complex_location_expr_complaint ();
14997 else
14998 fnp->voffset /= cu->header.addr_size;
14999 fnp->voffset += 2;
15000 }
15001 else
15002 dwarf2_complex_location_expr_complaint ();
15003
15004 if (!fnp->fcontext)
15005 {
15006 /* If there is no `this' field and no DW_AT_containing_type,
15007 we cannot actually find a base class context for the
15008 vtable! */
15009 if (TYPE_NFIELDS (this_type) == 0
15010 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15011 {
15012 complaint (_("cannot determine context for virtual member "
15013 "function \"%s\" (offset %s)"),
15014 fieldname, sect_offset_str (die->sect_off));
15015 }
15016 else
15017 {
15018 fnp->fcontext
15019 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15020 }
15021 }
15022 }
15023 else if (attr->form_is_section_offset ())
15024 {
15025 dwarf2_complex_location_expr_complaint ();
15026 }
15027 else
15028 {
15029 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15030 fieldname);
15031 }
15032 }
15033 else
15034 {
15035 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15036 if (attr && DW_UNSND (attr))
15037 {
15038 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15039 complaint (_("Member function \"%s\" (offset %s) is virtual "
15040 "but the vtable offset is not specified"),
15041 fieldname, sect_offset_str (die->sect_off));
15042 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15043 TYPE_CPLUS_DYNAMIC (type) = 1;
15044 }
15045 }
15046 }
15047
15048 /* Create the vector of member function fields, and attach it to the type. */
15049
15050 static void
15051 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15052 struct dwarf2_cu *cu)
15053 {
15054 if (cu->language == language_ada)
15055 error (_("unexpected member functions in Ada type"));
15056
15057 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15058 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15059 TYPE_ALLOC (type,
15060 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15061
15062 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15063 {
15064 struct fnfieldlist &nf = fip->fnfieldlists[i];
15065 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15066
15067 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15068 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15069 fn_flp->fn_fields = (struct fn_field *)
15070 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15071
15072 for (int k = 0; k < nf.fnfields.size (); ++k)
15073 fn_flp->fn_fields[k] = nf.fnfields[k];
15074 }
15075
15076 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15077 }
15078
15079 /* Returns non-zero if NAME is the name of a vtable member in CU's
15080 language, zero otherwise. */
15081 static int
15082 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15083 {
15084 static const char vptr[] = "_vptr";
15085
15086 /* Look for the C++ form of the vtable. */
15087 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15088 return 1;
15089
15090 return 0;
15091 }
15092
15093 /* GCC outputs unnamed structures that are really pointers to member
15094 functions, with the ABI-specified layout. If TYPE describes
15095 such a structure, smash it into a member function type.
15096
15097 GCC shouldn't do this; it should just output pointer to member DIEs.
15098 This is GCC PR debug/28767. */
15099
15100 static void
15101 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15102 {
15103 struct type *pfn_type, *self_type, *new_type;
15104
15105 /* Check for a structure with no name and two children. */
15106 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15107 return;
15108
15109 /* Check for __pfn and __delta members. */
15110 if (TYPE_FIELD_NAME (type, 0) == NULL
15111 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15112 || TYPE_FIELD_NAME (type, 1) == NULL
15113 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15114 return;
15115
15116 /* Find the type of the method. */
15117 pfn_type = TYPE_FIELD_TYPE (type, 0);
15118 if (pfn_type == NULL
15119 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15120 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15121 return;
15122
15123 /* Look for the "this" argument. */
15124 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15125 if (TYPE_NFIELDS (pfn_type) == 0
15126 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15127 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15128 return;
15129
15130 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15131 new_type = alloc_type (objfile);
15132 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15133 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15134 TYPE_VARARGS (pfn_type));
15135 smash_to_methodptr_type (type, new_type);
15136 }
15137
15138 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15139 appropriate error checking and issuing complaints if there is a
15140 problem. */
15141
15142 static ULONGEST
15143 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15144 {
15145 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15146
15147 if (attr == nullptr)
15148 return 0;
15149
15150 if (!attr->form_is_constant ())
15151 {
15152 complaint (_("DW_AT_alignment must have constant form"
15153 " - DIE at %s [in module %s]"),
15154 sect_offset_str (die->sect_off),
15155 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15156 return 0;
15157 }
15158
15159 ULONGEST align;
15160 if (attr->form == DW_FORM_sdata)
15161 {
15162 LONGEST val = DW_SND (attr);
15163 if (val < 0)
15164 {
15165 complaint (_("DW_AT_alignment value must not be negative"
15166 " - DIE at %s [in module %s]"),
15167 sect_offset_str (die->sect_off),
15168 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15169 return 0;
15170 }
15171 align = val;
15172 }
15173 else
15174 align = DW_UNSND (attr);
15175
15176 if (align == 0)
15177 {
15178 complaint (_("DW_AT_alignment value must not be zero"
15179 " - DIE at %s [in module %s]"),
15180 sect_offset_str (die->sect_off),
15181 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15182 return 0;
15183 }
15184 if ((align & (align - 1)) != 0)
15185 {
15186 complaint (_("DW_AT_alignment value must be a power of 2"
15187 " - DIE at %s [in module %s]"),
15188 sect_offset_str (die->sect_off),
15189 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15190 return 0;
15191 }
15192
15193 return align;
15194 }
15195
15196 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15197 the alignment for TYPE. */
15198
15199 static void
15200 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15201 struct type *type)
15202 {
15203 if (!set_type_align (type, get_alignment (cu, die)))
15204 complaint (_("DW_AT_alignment value too large"
15205 " - DIE at %s [in module %s]"),
15206 sect_offset_str (die->sect_off),
15207 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15208 }
15209
15210 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15211 constant for a type, according to DWARF5 spec, Table 5.5. */
15212
15213 static bool
15214 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15215 {
15216 switch (value)
15217 {
15218 case DW_CC_normal:
15219 case DW_CC_pass_by_reference:
15220 case DW_CC_pass_by_value:
15221 return true;
15222
15223 default:
15224 complaint (_("unrecognized DW_AT_calling_convention value "
15225 "(%s) for a type"), pulongest (value));
15226 return false;
15227 }
15228 }
15229
15230 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15231 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15232 also according to GNU-specific values (see include/dwarf2.h). */
15233
15234 static bool
15235 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15236 {
15237 switch (value)
15238 {
15239 case DW_CC_normal:
15240 case DW_CC_program:
15241 case DW_CC_nocall:
15242 return true;
15243
15244 case DW_CC_GNU_renesas_sh:
15245 case DW_CC_GNU_borland_fastcall_i386:
15246 case DW_CC_GDB_IBM_OpenCL:
15247 return true;
15248
15249 default:
15250 complaint (_("unrecognized DW_AT_calling_convention value "
15251 "(%s) for a subroutine"), pulongest (value));
15252 return false;
15253 }
15254 }
15255
15256 /* Called when we find the DIE that starts a structure or union scope
15257 (definition) to create a type for the structure or union. Fill in
15258 the type's name and general properties; the members will not be
15259 processed until process_structure_scope. A symbol table entry for
15260 the type will also not be done until process_structure_scope (assuming
15261 the type has a name).
15262
15263 NOTE: we need to call these functions regardless of whether or not the
15264 DIE has a DW_AT_name attribute, since it might be an anonymous
15265 structure or union. This gets the type entered into our set of
15266 user defined types. */
15267
15268 static struct type *
15269 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15270 {
15271 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15272 struct type *type;
15273 struct attribute *attr;
15274 const char *name;
15275
15276 /* If the definition of this type lives in .debug_types, read that type.
15277 Don't follow DW_AT_specification though, that will take us back up
15278 the chain and we want to go down. */
15279 attr = die->attr (DW_AT_signature);
15280 if (attr != nullptr)
15281 {
15282 type = get_DW_AT_signature_type (die, attr, cu);
15283
15284 /* The type's CU may not be the same as CU.
15285 Ensure TYPE is recorded with CU in die_type_hash. */
15286 return set_die_type (die, type, cu);
15287 }
15288
15289 type = alloc_type (objfile);
15290 INIT_CPLUS_SPECIFIC (type);
15291
15292 name = dwarf2_name (die, cu);
15293 if (name != NULL)
15294 {
15295 if (cu->language == language_cplus
15296 || cu->language == language_d
15297 || cu->language == language_rust)
15298 {
15299 const char *full_name = dwarf2_full_name (name, die, cu);
15300
15301 /* dwarf2_full_name might have already finished building the DIE's
15302 type. If so, there is no need to continue. */
15303 if (get_die_type (die, cu) != NULL)
15304 return get_die_type (die, cu);
15305
15306 TYPE_NAME (type) = full_name;
15307 }
15308 else
15309 {
15310 /* The name is already allocated along with this objfile, so
15311 we don't need to duplicate it for the type. */
15312 TYPE_NAME (type) = name;
15313 }
15314 }
15315
15316 if (die->tag == DW_TAG_structure_type)
15317 {
15318 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15319 }
15320 else if (die->tag == DW_TAG_union_type)
15321 {
15322 TYPE_CODE (type) = TYPE_CODE_UNION;
15323 }
15324 else
15325 {
15326 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15327 }
15328
15329 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15330 TYPE_DECLARED_CLASS (type) = 1;
15331
15332 /* Store the calling convention in the type if it's available in
15333 the die. Otherwise the calling convention remains set to
15334 the default value DW_CC_normal. */
15335 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15336 if (attr != nullptr
15337 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15338 {
15339 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15340 TYPE_CPLUS_CALLING_CONVENTION (type)
15341 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15342 }
15343
15344 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15345 if (attr != nullptr)
15346 {
15347 if (attr->form_is_constant ())
15348 TYPE_LENGTH (type) = DW_UNSND (attr);
15349 else
15350 {
15351 struct dynamic_prop prop;
15352 if (attr_to_dynamic_prop (attr, die, cu, &prop,
15353 cu->per_cu->addr_type ()))
15354 add_dyn_prop (DYN_PROP_BYTE_SIZE, prop, type);
15355 TYPE_LENGTH (type) = 0;
15356 }
15357 }
15358 else
15359 {
15360 TYPE_LENGTH (type) = 0;
15361 }
15362
15363 maybe_set_alignment (cu, die, type);
15364
15365 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15366 {
15367 /* ICC<14 does not output the required DW_AT_declaration on
15368 incomplete types, but gives them a size of zero. */
15369 TYPE_STUB (type) = 1;
15370 }
15371 else
15372 TYPE_STUB_SUPPORTED (type) = 1;
15373
15374 if (die_is_declaration (die, cu))
15375 TYPE_STUB (type) = 1;
15376 else if (attr == NULL && die->child == NULL
15377 && producer_is_realview (cu->producer))
15378 /* RealView does not output the required DW_AT_declaration
15379 on incomplete types. */
15380 TYPE_STUB (type) = 1;
15381
15382 /* We need to add the type field to the die immediately so we don't
15383 infinitely recurse when dealing with pointers to the structure
15384 type within the structure itself. */
15385 set_die_type (die, type, cu);
15386
15387 /* set_die_type should be already done. */
15388 set_descriptive_type (type, die, cu);
15389
15390 return type;
15391 }
15392
15393 static void handle_struct_member_die
15394 (struct die_info *child_die,
15395 struct type *type,
15396 struct field_info *fi,
15397 std::vector<struct symbol *> *template_args,
15398 struct dwarf2_cu *cu);
15399
15400 /* A helper for handle_struct_member_die that handles
15401 DW_TAG_variant_part. */
15402
15403 static void
15404 handle_variant_part (struct die_info *die, struct type *type,
15405 struct field_info *fi,
15406 std::vector<struct symbol *> *template_args,
15407 struct dwarf2_cu *cu)
15408 {
15409 variant_part_builder *new_part;
15410 if (fi->current_variant_part == nullptr)
15411 {
15412 fi->variant_parts.emplace_back ();
15413 new_part = &fi->variant_parts.back ();
15414 }
15415 else if (!fi->current_variant_part->processing_variant)
15416 {
15417 complaint (_("nested DW_TAG_variant_part seen "
15418 "- DIE at %s [in module %s]"),
15419 sect_offset_str (die->sect_off),
15420 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15421 return;
15422 }
15423 else
15424 {
15425 variant_field &current = fi->current_variant_part->variants.back ();
15426 current.variant_parts.emplace_back ();
15427 new_part = &current.variant_parts.back ();
15428 }
15429
15430 /* When we recurse, we want callees to add to this new variant
15431 part. */
15432 scoped_restore save_current_variant_part
15433 = make_scoped_restore (&fi->current_variant_part, new_part);
15434
15435 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15436 if (discr == NULL)
15437 {
15438 /* It's a univariant form, an extension we support. */
15439 }
15440 else if (discr->form_is_ref ())
15441 {
15442 struct dwarf2_cu *target_cu = cu;
15443 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15444
15445 new_part->discriminant_offset = target_die->sect_off;
15446 }
15447 else
15448 {
15449 complaint (_("DW_AT_discr does not have DIE reference form"
15450 " - DIE at %s [in module %s]"),
15451 sect_offset_str (die->sect_off),
15452 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15453 }
15454
15455 for (die_info *child_die = die->child;
15456 child_die != NULL;
15457 child_die = child_die->sibling)
15458 handle_struct_member_die (child_die, type, fi, template_args, cu);
15459 }
15460
15461 /* A helper for handle_struct_member_die that handles
15462 DW_TAG_variant. */
15463
15464 static void
15465 handle_variant (struct die_info *die, struct type *type,
15466 struct field_info *fi,
15467 std::vector<struct symbol *> *template_args,
15468 struct dwarf2_cu *cu)
15469 {
15470 if (fi->current_variant_part == nullptr)
15471 {
15472 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15473 "- DIE at %s [in module %s]"),
15474 sect_offset_str (die->sect_off),
15475 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15476 return;
15477 }
15478 if (fi->current_variant_part->processing_variant)
15479 {
15480 complaint (_("nested DW_TAG_variant seen "
15481 "- DIE at %s [in module %s]"),
15482 sect_offset_str (die->sect_off),
15483 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15484 return;
15485 }
15486
15487 scoped_restore save_processing_variant
15488 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15489 true);
15490
15491 fi->current_variant_part->variants.emplace_back ();
15492 variant_field &variant = fi->current_variant_part->variants.back ();
15493 variant.first_field = fi->fields.size ();
15494
15495 /* In a variant we want to get the discriminant and also add a
15496 field for our sole member child. */
15497 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15498 if (discr == nullptr)
15499 {
15500 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15501 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15502 variant.default_branch = true;
15503 else
15504 variant.discr_list_data = DW_BLOCK (discr);
15505 }
15506 else
15507 variant.discriminant_value = DW_UNSND (discr);
15508
15509 for (die_info *variant_child = die->child;
15510 variant_child != NULL;
15511 variant_child = variant_child->sibling)
15512 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15513
15514 variant.last_field = fi->fields.size ();
15515 }
15516
15517 /* A helper for process_structure_scope that handles a single member
15518 DIE. */
15519
15520 static void
15521 handle_struct_member_die (struct die_info *child_die, struct type *type,
15522 struct field_info *fi,
15523 std::vector<struct symbol *> *template_args,
15524 struct dwarf2_cu *cu)
15525 {
15526 if (child_die->tag == DW_TAG_member
15527 || child_die->tag == DW_TAG_variable)
15528 {
15529 /* NOTE: carlton/2002-11-05: A C++ static data member
15530 should be a DW_TAG_member that is a declaration, but
15531 all versions of G++ as of this writing (so through at
15532 least 3.2.1) incorrectly generate DW_TAG_variable
15533 tags for them instead. */
15534 dwarf2_add_field (fi, child_die, cu);
15535 }
15536 else if (child_die->tag == DW_TAG_subprogram)
15537 {
15538 /* Rust doesn't have member functions in the C++ sense.
15539 However, it does emit ordinary functions as children
15540 of a struct DIE. */
15541 if (cu->language == language_rust)
15542 read_func_scope (child_die, cu);
15543 else
15544 {
15545 /* C++ member function. */
15546 dwarf2_add_member_fn (fi, child_die, type, cu);
15547 }
15548 }
15549 else if (child_die->tag == DW_TAG_inheritance)
15550 {
15551 /* C++ base class field. */
15552 dwarf2_add_field (fi, child_die, cu);
15553 }
15554 else if (type_can_define_types (child_die))
15555 dwarf2_add_type_defn (fi, child_die, cu);
15556 else if (child_die->tag == DW_TAG_template_type_param
15557 || child_die->tag == DW_TAG_template_value_param)
15558 {
15559 struct symbol *arg = new_symbol (child_die, NULL, cu);
15560
15561 if (arg != NULL)
15562 template_args->push_back (arg);
15563 }
15564 else if (child_die->tag == DW_TAG_variant_part)
15565 handle_variant_part (child_die, type, fi, template_args, cu);
15566 else if (child_die->tag == DW_TAG_variant)
15567 handle_variant (child_die, type, fi, template_args, cu);
15568 }
15569
15570 /* Finish creating a structure or union type, including filling in
15571 its members and creating a symbol for it. */
15572
15573 static void
15574 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15575 {
15576 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15577 struct die_info *child_die;
15578 struct type *type;
15579
15580 type = get_die_type (die, cu);
15581 if (type == NULL)
15582 type = read_structure_type (die, cu);
15583
15584 bool has_template_parameters = false;
15585 if (die->child != NULL && ! die_is_declaration (die, cu))
15586 {
15587 struct field_info fi;
15588 std::vector<struct symbol *> template_args;
15589
15590 child_die = die->child;
15591
15592 while (child_die && child_die->tag)
15593 {
15594 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15595 child_die = child_die->sibling;
15596 }
15597
15598 /* Attach template arguments to type. */
15599 if (!template_args.empty ())
15600 {
15601 has_template_parameters = true;
15602 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15603 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15604 TYPE_TEMPLATE_ARGUMENTS (type)
15605 = XOBNEWVEC (&objfile->objfile_obstack,
15606 struct symbol *,
15607 TYPE_N_TEMPLATE_ARGUMENTS (type));
15608 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15609 template_args.data (),
15610 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15611 * sizeof (struct symbol *)));
15612 }
15613
15614 /* Attach fields and member functions to the type. */
15615 if (fi.nfields () > 0)
15616 dwarf2_attach_fields_to_type (&fi, type, cu);
15617 if (!fi.fnfieldlists.empty ())
15618 {
15619 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15620
15621 /* Get the type which refers to the base class (possibly this
15622 class itself) which contains the vtable pointer for the current
15623 class from the DW_AT_containing_type attribute. This use of
15624 DW_AT_containing_type is a GNU extension. */
15625
15626 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15627 {
15628 struct type *t = die_containing_type (die, cu);
15629
15630 set_type_vptr_basetype (type, t);
15631 if (type == t)
15632 {
15633 int i;
15634
15635 /* Our own class provides vtbl ptr. */
15636 for (i = TYPE_NFIELDS (t) - 1;
15637 i >= TYPE_N_BASECLASSES (t);
15638 --i)
15639 {
15640 const char *fieldname = TYPE_FIELD_NAME (t, i);
15641
15642 if (is_vtable_name (fieldname, cu))
15643 {
15644 set_type_vptr_fieldno (type, i);
15645 break;
15646 }
15647 }
15648
15649 /* Complain if virtual function table field not found. */
15650 if (i < TYPE_N_BASECLASSES (t))
15651 complaint (_("virtual function table pointer "
15652 "not found when defining class '%s'"),
15653 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15654 }
15655 else
15656 {
15657 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15658 }
15659 }
15660 else if (cu->producer
15661 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15662 {
15663 /* The IBM XLC compiler does not provide direct indication
15664 of the containing type, but the vtable pointer is
15665 always named __vfp. */
15666
15667 int i;
15668
15669 for (i = TYPE_NFIELDS (type) - 1;
15670 i >= TYPE_N_BASECLASSES (type);
15671 --i)
15672 {
15673 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15674 {
15675 set_type_vptr_fieldno (type, i);
15676 set_type_vptr_basetype (type, type);
15677 break;
15678 }
15679 }
15680 }
15681 }
15682
15683 /* Copy fi.typedef_field_list linked list elements content into the
15684 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15685 if (!fi.typedef_field_list.empty ())
15686 {
15687 int count = fi.typedef_field_list.size ();
15688
15689 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15690 TYPE_TYPEDEF_FIELD_ARRAY (type)
15691 = ((struct decl_field *)
15692 TYPE_ALLOC (type,
15693 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15694 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15695
15696 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15697 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15698 }
15699
15700 /* Copy fi.nested_types_list linked list elements content into the
15701 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15702 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15703 {
15704 int count = fi.nested_types_list.size ();
15705
15706 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15707 TYPE_NESTED_TYPES_ARRAY (type)
15708 = ((struct decl_field *)
15709 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15710 TYPE_NESTED_TYPES_COUNT (type) = count;
15711
15712 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15713 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15714 }
15715 }
15716
15717 quirk_gcc_member_function_pointer (type, objfile);
15718 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15719 cu->rust_unions.push_back (type);
15720
15721 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15722 snapshots) has been known to create a die giving a declaration
15723 for a class that has, as a child, a die giving a definition for a
15724 nested class. So we have to process our children even if the
15725 current die is a declaration. Normally, of course, a declaration
15726 won't have any children at all. */
15727
15728 child_die = die->child;
15729
15730 while (child_die != NULL && child_die->tag)
15731 {
15732 if (child_die->tag == DW_TAG_member
15733 || child_die->tag == DW_TAG_variable
15734 || child_die->tag == DW_TAG_inheritance
15735 || child_die->tag == DW_TAG_template_value_param
15736 || child_die->tag == DW_TAG_template_type_param)
15737 {
15738 /* Do nothing. */
15739 }
15740 else
15741 process_die (child_die, cu);
15742
15743 child_die = child_die->sibling;
15744 }
15745
15746 /* Do not consider external references. According to the DWARF standard,
15747 these DIEs are identified by the fact that they have no byte_size
15748 attribute, and a declaration attribute. */
15749 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15750 || !die_is_declaration (die, cu))
15751 {
15752 struct symbol *sym = new_symbol (die, type, cu);
15753
15754 if (has_template_parameters)
15755 {
15756 struct symtab *symtab;
15757 if (sym != nullptr)
15758 symtab = symbol_symtab (sym);
15759 else if (cu->line_header != nullptr)
15760 {
15761 /* Any related symtab will do. */
15762 symtab
15763 = cu->line_header->file_names ()[0].symtab;
15764 }
15765 else
15766 {
15767 symtab = nullptr;
15768 complaint (_("could not find suitable "
15769 "symtab for template parameter"
15770 " - DIE at %s [in module %s]"),
15771 sect_offset_str (die->sect_off),
15772 objfile_name (objfile));
15773 }
15774
15775 if (symtab != nullptr)
15776 {
15777 /* Make sure that the symtab is set on the new symbols.
15778 Even though they don't appear in this symtab directly,
15779 other parts of gdb assume that symbols do, and this is
15780 reasonably true. */
15781 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15782 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15783 }
15784 }
15785 }
15786 }
15787
15788 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15789 update TYPE using some information only available in DIE's children. */
15790
15791 static void
15792 update_enumeration_type_from_children (struct die_info *die,
15793 struct type *type,
15794 struct dwarf2_cu *cu)
15795 {
15796 struct die_info *child_die;
15797 int unsigned_enum = 1;
15798 int flag_enum = 1;
15799
15800 auto_obstack obstack;
15801
15802 for (child_die = die->child;
15803 child_die != NULL && child_die->tag;
15804 child_die = child_die->sibling)
15805 {
15806 struct attribute *attr;
15807 LONGEST value;
15808 const gdb_byte *bytes;
15809 struct dwarf2_locexpr_baton *baton;
15810 const char *name;
15811
15812 if (child_die->tag != DW_TAG_enumerator)
15813 continue;
15814
15815 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15816 if (attr == NULL)
15817 continue;
15818
15819 name = dwarf2_name (child_die, cu);
15820 if (name == NULL)
15821 name = "<anonymous enumerator>";
15822
15823 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15824 &value, &bytes, &baton);
15825 if (value < 0)
15826 {
15827 unsigned_enum = 0;
15828 flag_enum = 0;
15829 }
15830 else
15831 {
15832 if (count_one_bits_ll (value) >= 2)
15833 flag_enum = 0;
15834 }
15835
15836 /* If we already know that the enum type is neither unsigned, nor
15837 a flag type, no need to look at the rest of the enumerates. */
15838 if (!unsigned_enum && !flag_enum)
15839 break;
15840 }
15841
15842 if (unsigned_enum)
15843 TYPE_UNSIGNED (type) = 1;
15844 if (flag_enum)
15845 TYPE_FLAG_ENUM (type) = 1;
15846 }
15847
15848 /* Given a DW_AT_enumeration_type die, set its type. We do not
15849 complete the type's fields yet, or create any symbols. */
15850
15851 static struct type *
15852 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15853 {
15854 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15855 struct type *type;
15856 struct attribute *attr;
15857 const char *name;
15858
15859 /* If the definition of this type lives in .debug_types, read that type.
15860 Don't follow DW_AT_specification though, that will take us back up
15861 the chain and we want to go down. */
15862 attr = die->attr (DW_AT_signature);
15863 if (attr != nullptr)
15864 {
15865 type = get_DW_AT_signature_type (die, attr, cu);
15866
15867 /* The type's CU may not be the same as CU.
15868 Ensure TYPE is recorded with CU in die_type_hash. */
15869 return set_die_type (die, type, cu);
15870 }
15871
15872 type = alloc_type (objfile);
15873
15874 TYPE_CODE (type) = TYPE_CODE_ENUM;
15875 name = dwarf2_full_name (NULL, die, cu);
15876 if (name != NULL)
15877 TYPE_NAME (type) = name;
15878
15879 attr = dwarf2_attr (die, DW_AT_type, cu);
15880 if (attr != NULL)
15881 {
15882 struct type *underlying_type = die_type (die, cu);
15883
15884 TYPE_TARGET_TYPE (type) = underlying_type;
15885 }
15886
15887 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15888 if (attr != nullptr)
15889 {
15890 TYPE_LENGTH (type) = DW_UNSND (attr);
15891 }
15892 else
15893 {
15894 TYPE_LENGTH (type) = 0;
15895 }
15896
15897 maybe_set_alignment (cu, die, type);
15898
15899 /* The enumeration DIE can be incomplete. In Ada, any type can be
15900 declared as private in the package spec, and then defined only
15901 inside the package body. Such types are known as Taft Amendment
15902 Types. When another package uses such a type, an incomplete DIE
15903 may be generated by the compiler. */
15904 if (die_is_declaration (die, cu))
15905 TYPE_STUB (type) = 1;
15906
15907 /* Finish the creation of this type by using the enum's children.
15908 We must call this even when the underlying type has been provided
15909 so that we can determine if we're looking at a "flag" enum. */
15910 update_enumeration_type_from_children (die, type, cu);
15911
15912 /* If this type has an underlying type that is not a stub, then we
15913 may use its attributes. We always use the "unsigned" attribute
15914 in this situation, because ordinarily we guess whether the type
15915 is unsigned -- but the guess can be wrong and the underlying type
15916 can tell us the reality. However, we defer to a local size
15917 attribute if one exists, because this lets the compiler override
15918 the underlying type if needed. */
15919 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15920 {
15921 struct type *underlying_type = TYPE_TARGET_TYPE (type);
15922 underlying_type = check_typedef (underlying_type);
15923 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
15924 if (TYPE_LENGTH (type) == 0)
15925 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
15926 if (TYPE_RAW_ALIGN (type) == 0
15927 && TYPE_RAW_ALIGN (underlying_type) != 0)
15928 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
15929 }
15930
15931 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15932
15933 return set_die_type (die, type, cu);
15934 }
15935
15936 /* Given a pointer to a die which begins an enumeration, process all
15937 the dies that define the members of the enumeration, and create the
15938 symbol for the enumeration type.
15939
15940 NOTE: We reverse the order of the element list. */
15941
15942 static void
15943 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15944 {
15945 struct type *this_type;
15946
15947 this_type = get_die_type (die, cu);
15948 if (this_type == NULL)
15949 this_type = read_enumeration_type (die, cu);
15950
15951 if (die->child != NULL)
15952 {
15953 struct die_info *child_die;
15954 struct symbol *sym;
15955 std::vector<struct field> fields;
15956 const char *name;
15957
15958 child_die = die->child;
15959 while (child_die && child_die->tag)
15960 {
15961 if (child_die->tag != DW_TAG_enumerator)
15962 {
15963 process_die (child_die, cu);
15964 }
15965 else
15966 {
15967 name = dwarf2_name (child_die, cu);
15968 if (name)
15969 {
15970 sym = new_symbol (child_die, this_type, cu);
15971
15972 fields.emplace_back ();
15973 struct field &field = fields.back ();
15974
15975 FIELD_NAME (field) = sym->linkage_name ();
15976 FIELD_TYPE (field) = NULL;
15977 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15978 FIELD_BITSIZE (field) = 0;
15979 }
15980 }
15981
15982 child_die = child_die->sibling;
15983 }
15984
15985 if (!fields.empty ())
15986 {
15987 TYPE_NFIELDS (this_type) = fields.size ();
15988 TYPE_FIELDS (this_type) = (struct field *)
15989 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15990 memcpy (TYPE_FIELDS (this_type), fields.data (),
15991 sizeof (struct field) * fields.size ());
15992 }
15993 }
15994
15995 /* If we are reading an enum from a .debug_types unit, and the enum
15996 is a declaration, and the enum is not the signatured type in the
15997 unit, then we do not want to add a symbol for it. Adding a
15998 symbol would in some cases obscure the true definition of the
15999 enum, giving users an incomplete type when the definition is
16000 actually available. Note that we do not want to do this for all
16001 enums which are just declarations, because C++0x allows forward
16002 enum declarations. */
16003 if (cu->per_cu->is_debug_types
16004 && die_is_declaration (die, cu))
16005 {
16006 struct signatured_type *sig_type;
16007
16008 sig_type = (struct signatured_type *) cu->per_cu;
16009 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16010 if (sig_type->type_offset_in_section != die->sect_off)
16011 return;
16012 }
16013
16014 new_symbol (die, this_type, cu);
16015 }
16016
16017 /* Extract all information from a DW_TAG_array_type DIE and put it in
16018 the DIE's type field. For now, this only handles one dimensional
16019 arrays. */
16020
16021 static struct type *
16022 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16023 {
16024 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16025 struct die_info *child_die;
16026 struct type *type;
16027 struct type *element_type, *range_type, *index_type;
16028 struct attribute *attr;
16029 const char *name;
16030 struct dynamic_prop *byte_stride_prop = NULL;
16031 unsigned int bit_stride = 0;
16032
16033 element_type = die_type (die, cu);
16034
16035 /* The die_type call above may have already set the type for this DIE. */
16036 type = get_die_type (die, cu);
16037 if (type)
16038 return type;
16039
16040 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16041 if (attr != NULL)
16042 {
16043 int stride_ok;
16044 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
16045
16046 byte_stride_prop
16047 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16048 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16049 prop_type);
16050 if (!stride_ok)
16051 {
16052 complaint (_("unable to read array DW_AT_byte_stride "
16053 " - DIE at %s [in module %s]"),
16054 sect_offset_str (die->sect_off),
16055 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16056 /* Ignore this attribute. We will likely not be able to print
16057 arrays of this type correctly, but there is little we can do
16058 to help if we cannot read the attribute's value. */
16059 byte_stride_prop = NULL;
16060 }
16061 }
16062
16063 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16064 if (attr != NULL)
16065 bit_stride = DW_UNSND (attr);
16066
16067 /* Irix 6.2 native cc creates array types without children for
16068 arrays with unspecified length. */
16069 if (die->child == NULL)
16070 {
16071 index_type = objfile_type (objfile)->builtin_int;
16072 range_type = create_static_range_type (NULL, index_type, 0, -1);
16073 type = create_array_type_with_stride (NULL, element_type, range_type,
16074 byte_stride_prop, bit_stride);
16075 return set_die_type (die, type, cu);
16076 }
16077
16078 std::vector<struct type *> range_types;
16079 child_die = die->child;
16080 while (child_die && child_die->tag)
16081 {
16082 if (child_die->tag == DW_TAG_subrange_type)
16083 {
16084 struct type *child_type = read_type_die (child_die, cu);
16085
16086 if (child_type != NULL)
16087 {
16088 /* The range type was succesfully read. Save it for the
16089 array type creation. */
16090 range_types.push_back (child_type);
16091 }
16092 }
16093 child_die = child_die->sibling;
16094 }
16095
16096 /* Dwarf2 dimensions are output from left to right, create the
16097 necessary array types in backwards order. */
16098
16099 type = element_type;
16100
16101 if (read_array_order (die, cu) == DW_ORD_col_major)
16102 {
16103 int i = 0;
16104
16105 while (i < range_types.size ())
16106 type = create_array_type_with_stride (NULL, type, range_types[i++],
16107 byte_stride_prop, bit_stride);
16108 }
16109 else
16110 {
16111 size_t ndim = range_types.size ();
16112 while (ndim-- > 0)
16113 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16114 byte_stride_prop, bit_stride);
16115 }
16116
16117 /* Understand Dwarf2 support for vector types (like they occur on
16118 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16119 array type. This is not part of the Dwarf2/3 standard yet, but a
16120 custom vendor extension. The main difference between a regular
16121 array and the vector variant is that vectors are passed by value
16122 to functions. */
16123 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16124 if (attr != nullptr)
16125 make_vector_type (type);
16126
16127 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16128 implementation may choose to implement triple vectors using this
16129 attribute. */
16130 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16131 if (attr != nullptr)
16132 {
16133 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16134 TYPE_LENGTH (type) = DW_UNSND (attr);
16135 else
16136 complaint (_("DW_AT_byte_size for array type smaller "
16137 "than the total size of elements"));
16138 }
16139
16140 name = dwarf2_name (die, cu);
16141 if (name)
16142 TYPE_NAME (type) = name;
16143
16144 maybe_set_alignment (cu, die, type);
16145
16146 /* Install the type in the die. */
16147 set_die_type (die, type, cu);
16148
16149 /* set_die_type should be already done. */
16150 set_descriptive_type (type, die, cu);
16151
16152 return type;
16153 }
16154
16155 static enum dwarf_array_dim_ordering
16156 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16157 {
16158 struct attribute *attr;
16159
16160 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16161
16162 if (attr != nullptr)
16163 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16164
16165 /* GNU F77 is a special case, as at 08/2004 array type info is the
16166 opposite order to the dwarf2 specification, but data is still
16167 laid out as per normal fortran.
16168
16169 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16170 version checking. */
16171
16172 if (cu->language == language_fortran
16173 && cu->producer && strstr (cu->producer, "GNU F77"))
16174 {
16175 return DW_ORD_row_major;
16176 }
16177
16178 switch (cu->language_defn->la_array_ordering)
16179 {
16180 case array_column_major:
16181 return DW_ORD_col_major;
16182 case array_row_major:
16183 default:
16184 return DW_ORD_row_major;
16185 };
16186 }
16187
16188 /* Extract all information from a DW_TAG_set_type DIE and put it in
16189 the DIE's type field. */
16190
16191 static struct type *
16192 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16193 {
16194 struct type *domain_type, *set_type;
16195 struct attribute *attr;
16196
16197 domain_type = die_type (die, cu);
16198
16199 /* The die_type call above may have already set the type for this DIE. */
16200 set_type = get_die_type (die, cu);
16201 if (set_type)
16202 return set_type;
16203
16204 set_type = create_set_type (NULL, domain_type);
16205
16206 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16207 if (attr != nullptr)
16208 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16209
16210 maybe_set_alignment (cu, die, set_type);
16211
16212 return set_die_type (die, set_type, cu);
16213 }
16214
16215 /* A helper for read_common_block that creates a locexpr baton.
16216 SYM is the symbol which we are marking as computed.
16217 COMMON_DIE is the DIE for the common block.
16218 COMMON_LOC is the location expression attribute for the common
16219 block itself.
16220 MEMBER_LOC is the location expression attribute for the particular
16221 member of the common block that we are processing.
16222 CU is the CU from which the above come. */
16223
16224 static void
16225 mark_common_block_symbol_computed (struct symbol *sym,
16226 struct die_info *common_die,
16227 struct attribute *common_loc,
16228 struct attribute *member_loc,
16229 struct dwarf2_cu *cu)
16230 {
16231 struct dwarf2_per_objfile *dwarf2_per_objfile
16232 = cu->per_cu->dwarf2_per_objfile;
16233 struct objfile *objfile = dwarf2_per_objfile->objfile;
16234 struct dwarf2_locexpr_baton *baton;
16235 gdb_byte *ptr;
16236 unsigned int cu_off;
16237 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16238 LONGEST offset = 0;
16239
16240 gdb_assert (common_loc && member_loc);
16241 gdb_assert (common_loc->form_is_block ());
16242 gdb_assert (member_loc->form_is_block ()
16243 || member_loc->form_is_constant ());
16244
16245 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16246 baton->per_cu = cu->per_cu;
16247 gdb_assert (baton->per_cu);
16248
16249 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16250
16251 if (member_loc->form_is_constant ())
16252 {
16253 offset = member_loc->constant_value (0);
16254 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16255 }
16256 else
16257 baton->size += DW_BLOCK (member_loc)->size;
16258
16259 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16260 baton->data = ptr;
16261
16262 *ptr++ = DW_OP_call4;
16263 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16264 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16265 ptr += 4;
16266
16267 if (member_loc->form_is_constant ())
16268 {
16269 *ptr++ = DW_OP_addr;
16270 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16271 ptr += cu->header.addr_size;
16272 }
16273 else
16274 {
16275 /* We have to copy the data here, because DW_OP_call4 will only
16276 use a DW_AT_location attribute. */
16277 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16278 ptr += DW_BLOCK (member_loc)->size;
16279 }
16280
16281 *ptr++ = DW_OP_plus;
16282 gdb_assert (ptr - baton->data == baton->size);
16283
16284 SYMBOL_LOCATION_BATON (sym) = baton;
16285 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16286 }
16287
16288 /* Create appropriate locally-scoped variables for all the
16289 DW_TAG_common_block entries. Also create a struct common_block
16290 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16291 is used to separate the common blocks name namespace from regular
16292 variable names. */
16293
16294 static void
16295 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16296 {
16297 struct attribute *attr;
16298
16299 attr = dwarf2_attr (die, DW_AT_location, cu);
16300 if (attr != nullptr)
16301 {
16302 /* Support the .debug_loc offsets. */
16303 if (attr->form_is_block ())
16304 {
16305 /* Ok. */
16306 }
16307 else if (attr->form_is_section_offset ())
16308 {
16309 dwarf2_complex_location_expr_complaint ();
16310 attr = NULL;
16311 }
16312 else
16313 {
16314 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16315 "common block member");
16316 attr = NULL;
16317 }
16318 }
16319
16320 if (die->child != NULL)
16321 {
16322 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16323 struct die_info *child_die;
16324 size_t n_entries = 0, size;
16325 struct common_block *common_block;
16326 struct symbol *sym;
16327
16328 for (child_die = die->child;
16329 child_die && child_die->tag;
16330 child_die = child_die->sibling)
16331 ++n_entries;
16332
16333 size = (sizeof (struct common_block)
16334 + (n_entries - 1) * sizeof (struct symbol *));
16335 common_block
16336 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16337 size);
16338 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16339 common_block->n_entries = 0;
16340
16341 for (child_die = die->child;
16342 child_die && child_die->tag;
16343 child_die = child_die->sibling)
16344 {
16345 /* Create the symbol in the DW_TAG_common_block block in the current
16346 symbol scope. */
16347 sym = new_symbol (child_die, NULL, cu);
16348 if (sym != NULL)
16349 {
16350 struct attribute *member_loc;
16351
16352 common_block->contents[common_block->n_entries++] = sym;
16353
16354 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16355 cu);
16356 if (member_loc)
16357 {
16358 /* GDB has handled this for a long time, but it is
16359 not specified by DWARF. It seems to have been
16360 emitted by gfortran at least as recently as:
16361 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16362 complaint (_("Variable in common block has "
16363 "DW_AT_data_member_location "
16364 "- DIE at %s [in module %s]"),
16365 sect_offset_str (child_die->sect_off),
16366 objfile_name (objfile));
16367
16368 if (member_loc->form_is_section_offset ())
16369 dwarf2_complex_location_expr_complaint ();
16370 else if (member_loc->form_is_constant ()
16371 || member_loc->form_is_block ())
16372 {
16373 if (attr != nullptr)
16374 mark_common_block_symbol_computed (sym, die, attr,
16375 member_loc, cu);
16376 }
16377 else
16378 dwarf2_complex_location_expr_complaint ();
16379 }
16380 }
16381 }
16382
16383 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16384 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16385 }
16386 }
16387
16388 /* Create a type for a C++ namespace. */
16389
16390 static struct type *
16391 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16392 {
16393 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16394 const char *previous_prefix, *name;
16395 int is_anonymous;
16396 struct type *type;
16397
16398 /* For extensions, reuse the type of the original namespace. */
16399 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16400 {
16401 struct die_info *ext_die;
16402 struct dwarf2_cu *ext_cu = cu;
16403
16404 ext_die = dwarf2_extension (die, &ext_cu);
16405 type = read_type_die (ext_die, ext_cu);
16406
16407 /* EXT_CU may not be the same as CU.
16408 Ensure TYPE is recorded with CU in die_type_hash. */
16409 return set_die_type (die, type, cu);
16410 }
16411
16412 name = namespace_name (die, &is_anonymous, cu);
16413
16414 /* Now build the name of the current namespace. */
16415
16416 previous_prefix = determine_prefix (die, cu);
16417 if (previous_prefix[0] != '\0')
16418 name = typename_concat (&objfile->objfile_obstack,
16419 previous_prefix, name, 0, cu);
16420
16421 /* Create the type. */
16422 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16423
16424 return set_die_type (die, type, cu);
16425 }
16426
16427 /* Read a namespace scope. */
16428
16429 static void
16430 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16431 {
16432 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16433 int is_anonymous;
16434
16435 /* Add a symbol associated to this if we haven't seen the namespace
16436 before. Also, add a using directive if it's an anonymous
16437 namespace. */
16438
16439 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16440 {
16441 struct type *type;
16442
16443 type = read_type_die (die, cu);
16444 new_symbol (die, type, cu);
16445
16446 namespace_name (die, &is_anonymous, cu);
16447 if (is_anonymous)
16448 {
16449 const char *previous_prefix = determine_prefix (die, cu);
16450
16451 std::vector<const char *> excludes;
16452 add_using_directive (using_directives (cu),
16453 previous_prefix, TYPE_NAME (type), NULL,
16454 NULL, excludes, 0, &objfile->objfile_obstack);
16455 }
16456 }
16457
16458 if (die->child != NULL)
16459 {
16460 struct die_info *child_die = die->child;
16461
16462 while (child_die && child_die->tag)
16463 {
16464 process_die (child_die, cu);
16465 child_die = child_die->sibling;
16466 }
16467 }
16468 }
16469
16470 /* Read a Fortran module as type. This DIE can be only a declaration used for
16471 imported module. Still we need that type as local Fortran "use ... only"
16472 declaration imports depend on the created type in determine_prefix. */
16473
16474 static struct type *
16475 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16476 {
16477 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16478 const char *module_name;
16479 struct type *type;
16480
16481 module_name = dwarf2_name (die, cu);
16482 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16483
16484 return set_die_type (die, type, cu);
16485 }
16486
16487 /* Read a Fortran module. */
16488
16489 static void
16490 read_module (struct die_info *die, struct dwarf2_cu *cu)
16491 {
16492 struct die_info *child_die = die->child;
16493 struct type *type;
16494
16495 type = read_type_die (die, cu);
16496 new_symbol (die, type, cu);
16497
16498 while (child_die && child_die->tag)
16499 {
16500 process_die (child_die, cu);
16501 child_die = child_die->sibling;
16502 }
16503 }
16504
16505 /* Return the name of the namespace represented by DIE. Set
16506 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16507 namespace. */
16508
16509 static const char *
16510 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16511 {
16512 struct die_info *current_die;
16513 const char *name = NULL;
16514
16515 /* Loop through the extensions until we find a name. */
16516
16517 for (current_die = die;
16518 current_die != NULL;
16519 current_die = dwarf2_extension (die, &cu))
16520 {
16521 /* We don't use dwarf2_name here so that we can detect the absence
16522 of a name -> anonymous namespace. */
16523 name = dwarf2_string_attr (die, DW_AT_name, cu);
16524
16525 if (name != NULL)
16526 break;
16527 }
16528
16529 /* Is it an anonymous namespace? */
16530
16531 *is_anonymous = (name == NULL);
16532 if (*is_anonymous)
16533 name = CP_ANONYMOUS_NAMESPACE_STR;
16534
16535 return name;
16536 }
16537
16538 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16539 the user defined type vector. */
16540
16541 static struct type *
16542 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16543 {
16544 struct gdbarch *gdbarch
16545 = cu->per_cu->dwarf2_per_objfile->objfile->arch ();
16546 struct comp_unit_head *cu_header = &cu->header;
16547 struct type *type;
16548 struct attribute *attr_byte_size;
16549 struct attribute *attr_address_class;
16550 int byte_size, addr_class;
16551 struct type *target_type;
16552
16553 target_type = die_type (die, cu);
16554
16555 /* The die_type call above may have already set the type for this DIE. */
16556 type = get_die_type (die, cu);
16557 if (type)
16558 return type;
16559
16560 type = lookup_pointer_type (target_type);
16561
16562 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16563 if (attr_byte_size)
16564 byte_size = DW_UNSND (attr_byte_size);
16565 else
16566 byte_size = cu_header->addr_size;
16567
16568 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16569 if (attr_address_class)
16570 addr_class = DW_UNSND (attr_address_class);
16571 else
16572 addr_class = DW_ADDR_none;
16573
16574 ULONGEST alignment = get_alignment (cu, die);
16575
16576 /* If the pointer size, alignment, or address class is different
16577 than the default, create a type variant marked as such and set
16578 the length accordingly. */
16579 if (TYPE_LENGTH (type) != byte_size
16580 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16581 && alignment != TYPE_RAW_ALIGN (type))
16582 || addr_class != DW_ADDR_none)
16583 {
16584 if (gdbarch_address_class_type_flags_p (gdbarch))
16585 {
16586 int type_flags;
16587
16588 type_flags = gdbarch_address_class_type_flags
16589 (gdbarch, byte_size, addr_class);
16590 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16591 == 0);
16592 type = make_type_with_address_space (type, type_flags);
16593 }
16594 else if (TYPE_LENGTH (type) != byte_size)
16595 {
16596 complaint (_("invalid pointer size %d"), byte_size);
16597 }
16598 else if (TYPE_RAW_ALIGN (type) != alignment)
16599 {
16600 complaint (_("Invalid DW_AT_alignment"
16601 " - DIE at %s [in module %s]"),
16602 sect_offset_str (die->sect_off),
16603 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16604 }
16605 else
16606 {
16607 /* Should we also complain about unhandled address classes? */
16608 }
16609 }
16610
16611 TYPE_LENGTH (type) = byte_size;
16612 set_type_align (type, alignment);
16613 return set_die_type (die, type, cu);
16614 }
16615
16616 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16617 the user defined type vector. */
16618
16619 static struct type *
16620 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16621 {
16622 struct type *type;
16623 struct type *to_type;
16624 struct type *domain;
16625
16626 to_type = die_type (die, cu);
16627 domain = die_containing_type (die, cu);
16628
16629 /* The calls above may have already set the type for this DIE. */
16630 type = get_die_type (die, cu);
16631 if (type)
16632 return type;
16633
16634 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16635 type = lookup_methodptr_type (to_type);
16636 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16637 {
16638 struct type *new_type
16639 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16640
16641 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16642 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16643 TYPE_VARARGS (to_type));
16644 type = lookup_methodptr_type (new_type);
16645 }
16646 else
16647 type = lookup_memberptr_type (to_type, domain);
16648
16649 return set_die_type (die, type, cu);
16650 }
16651
16652 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16653 the user defined type vector. */
16654
16655 static struct type *
16656 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16657 enum type_code refcode)
16658 {
16659 struct comp_unit_head *cu_header = &cu->header;
16660 struct type *type, *target_type;
16661 struct attribute *attr;
16662
16663 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16664
16665 target_type = die_type (die, cu);
16666
16667 /* The die_type call above may have already set the type for this DIE. */
16668 type = get_die_type (die, cu);
16669 if (type)
16670 return type;
16671
16672 type = lookup_reference_type (target_type, refcode);
16673 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16674 if (attr != nullptr)
16675 {
16676 TYPE_LENGTH (type) = DW_UNSND (attr);
16677 }
16678 else
16679 {
16680 TYPE_LENGTH (type) = cu_header->addr_size;
16681 }
16682 maybe_set_alignment (cu, die, type);
16683 return set_die_type (die, type, cu);
16684 }
16685
16686 /* Add the given cv-qualifiers to the element type of the array. GCC
16687 outputs DWARF type qualifiers that apply to an array, not the
16688 element type. But GDB relies on the array element type to carry
16689 the cv-qualifiers. This mimics section 6.7.3 of the C99
16690 specification. */
16691
16692 static struct type *
16693 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16694 struct type *base_type, int cnst, int voltl)
16695 {
16696 struct type *el_type, *inner_array;
16697
16698 base_type = copy_type (base_type);
16699 inner_array = base_type;
16700
16701 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16702 {
16703 TYPE_TARGET_TYPE (inner_array) =
16704 copy_type (TYPE_TARGET_TYPE (inner_array));
16705 inner_array = TYPE_TARGET_TYPE (inner_array);
16706 }
16707
16708 el_type = TYPE_TARGET_TYPE (inner_array);
16709 cnst |= TYPE_CONST (el_type);
16710 voltl |= TYPE_VOLATILE (el_type);
16711 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16712
16713 return set_die_type (die, base_type, cu);
16714 }
16715
16716 static struct type *
16717 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16718 {
16719 struct type *base_type, *cv_type;
16720
16721 base_type = die_type (die, cu);
16722
16723 /* The die_type call above may have already set the type for this DIE. */
16724 cv_type = get_die_type (die, cu);
16725 if (cv_type)
16726 return cv_type;
16727
16728 /* In case the const qualifier is applied to an array type, the element type
16729 is so qualified, not the array type (section 6.7.3 of C99). */
16730 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16731 return add_array_cv_type (die, cu, base_type, 1, 0);
16732
16733 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16734 return set_die_type (die, cv_type, cu);
16735 }
16736
16737 static struct type *
16738 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16739 {
16740 struct type *base_type, *cv_type;
16741
16742 base_type = die_type (die, cu);
16743
16744 /* The die_type call above may have already set the type for this DIE. */
16745 cv_type = get_die_type (die, cu);
16746 if (cv_type)
16747 return cv_type;
16748
16749 /* In case the volatile qualifier is applied to an array type, the
16750 element type is so qualified, not the array type (section 6.7.3
16751 of C99). */
16752 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16753 return add_array_cv_type (die, cu, base_type, 0, 1);
16754
16755 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16756 return set_die_type (die, cv_type, cu);
16757 }
16758
16759 /* Handle DW_TAG_restrict_type. */
16760
16761 static struct type *
16762 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16763 {
16764 struct type *base_type, *cv_type;
16765
16766 base_type = die_type (die, cu);
16767
16768 /* The die_type call above may have already set the type for this DIE. */
16769 cv_type = get_die_type (die, cu);
16770 if (cv_type)
16771 return cv_type;
16772
16773 cv_type = make_restrict_type (base_type);
16774 return set_die_type (die, cv_type, cu);
16775 }
16776
16777 /* Handle DW_TAG_atomic_type. */
16778
16779 static struct type *
16780 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16781 {
16782 struct type *base_type, *cv_type;
16783
16784 base_type = die_type (die, cu);
16785
16786 /* The die_type call above may have already set the type for this DIE. */
16787 cv_type = get_die_type (die, cu);
16788 if (cv_type)
16789 return cv_type;
16790
16791 cv_type = make_atomic_type (base_type);
16792 return set_die_type (die, cv_type, cu);
16793 }
16794
16795 /* Extract all information from a DW_TAG_string_type DIE and add to
16796 the user defined type vector. It isn't really a user defined type,
16797 but it behaves like one, with other DIE's using an AT_user_def_type
16798 attribute to reference it. */
16799
16800 static struct type *
16801 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16802 {
16803 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16804 struct gdbarch *gdbarch = objfile->arch ();
16805 struct type *type, *range_type, *index_type, *char_type;
16806 struct attribute *attr;
16807 struct dynamic_prop prop;
16808 bool length_is_constant = true;
16809 LONGEST length;
16810
16811 /* There are a couple of places where bit sizes might be made use of
16812 when parsing a DW_TAG_string_type, however, no producer that we know
16813 of make use of these. Handling bit sizes that are a multiple of the
16814 byte size is easy enough, but what about other bit sizes? Lets deal
16815 with that problem when we have to. Warn about these attributes being
16816 unsupported, then parse the type and ignore them like we always
16817 have. */
16818 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16819 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16820 {
16821 static bool warning_printed = false;
16822 if (!warning_printed)
16823 {
16824 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16825 "currently supported on DW_TAG_string_type."));
16826 warning_printed = true;
16827 }
16828 }
16829
16830 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16831 if (attr != nullptr && !attr->form_is_constant ())
16832 {
16833 /* The string length describes the location at which the length of
16834 the string can be found. The size of the length field can be
16835 specified with one of the attributes below. */
16836 struct type *prop_type;
16837 struct attribute *len
16838 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16839 if (len == nullptr)
16840 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16841 if (len != nullptr && len->form_is_constant ())
16842 {
16843 /* Pass 0 as the default as we know this attribute is constant
16844 and the default value will not be returned. */
16845 LONGEST sz = len->constant_value (0);
16846 prop_type = cu->per_cu->int_type (sz, true);
16847 }
16848 else
16849 {
16850 /* If the size is not specified then we assume it is the size of
16851 an address on this target. */
16852 prop_type = cu->per_cu->addr_sized_int_type (true);
16853 }
16854
16855 /* Convert the attribute into a dynamic property. */
16856 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16857 length = 1;
16858 else
16859 length_is_constant = false;
16860 }
16861 else if (attr != nullptr)
16862 {
16863 /* This DW_AT_string_length just contains the length with no
16864 indirection. There's no need to create a dynamic property in this
16865 case. Pass 0 for the default value as we know it will not be
16866 returned in this case. */
16867 length = attr->constant_value (0);
16868 }
16869 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16870 {
16871 /* We don't currently support non-constant byte sizes for strings. */
16872 length = attr->constant_value (1);
16873 }
16874 else
16875 {
16876 /* Use 1 as a fallback length if we have nothing else. */
16877 length = 1;
16878 }
16879
16880 index_type = objfile_type (objfile)->builtin_int;
16881 if (length_is_constant)
16882 range_type = create_static_range_type (NULL, index_type, 1, length);
16883 else
16884 {
16885 struct dynamic_prop low_bound;
16886
16887 low_bound.kind = PROP_CONST;
16888 low_bound.data.const_val = 1;
16889 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16890 }
16891 char_type = language_string_char_type (cu->language_defn, gdbarch);
16892 type = create_string_type (NULL, char_type, range_type);
16893
16894 return set_die_type (die, type, cu);
16895 }
16896
16897 /* Assuming that DIE corresponds to a function, returns nonzero
16898 if the function is prototyped. */
16899
16900 static int
16901 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16902 {
16903 struct attribute *attr;
16904
16905 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16906 if (attr && (DW_UNSND (attr) != 0))
16907 return 1;
16908
16909 /* The DWARF standard implies that the DW_AT_prototyped attribute
16910 is only meaningful for C, but the concept also extends to other
16911 languages that allow unprototyped functions (Eg: Objective C).
16912 For all other languages, assume that functions are always
16913 prototyped. */
16914 if (cu->language != language_c
16915 && cu->language != language_objc
16916 && cu->language != language_opencl)
16917 return 1;
16918
16919 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16920 prototyped and unprototyped functions; default to prototyped,
16921 since that is more common in modern code (and RealView warns
16922 about unprototyped functions). */
16923 if (producer_is_realview (cu->producer))
16924 return 1;
16925
16926 return 0;
16927 }
16928
16929 /* Handle DIES due to C code like:
16930
16931 struct foo
16932 {
16933 int (*funcp)(int a, long l);
16934 int b;
16935 };
16936
16937 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16938
16939 static struct type *
16940 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16941 {
16942 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16943 struct type *type; /* Type that this function returns. */
16944 struct type *ftype; /* Function that returns above type. */
16945 struct attribute *attr;
16946
16947 type = die_type (die, cu);
16948
16949 /* The die_type call above may have already set the type for this DIE. */
16950 ftype = get_die_type (die, cu);
16951 if (ftype)
16952 return ftype;
16953
16954 ftype = lookup_function_type (type);
16955
16956 if (prototyped_function_p (die, cu))
16957 TYPE_PROTOTYPED (ftype) = 1;
16958
16959 /* Store the calling convention in the type if it's available in
16960 the subroutine die. Otherwise set the calling convention to
16961 the default value DW_CC_normal. */
16962 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16963 if (attr != nullptr
16964 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16965 TYPE_CALLING_CONVENTION (ftype)
16966 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16967 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16968 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16969 else
16970 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16971
16972 /* Record whether the function returns normally to its caller or not
16973 if the DWARF producer set that information. */
16974 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16975 if (attr && (DW_UNSND (attr) != 0))
16976 TYPE_NO_RETURN (ftype) = 1;
16977
16978 /* We need to add the subroutine type to the die immediately so
16979 we don't infinitely recurse when dealing with parameters
16980 declared as the same subroutine type. */
16981 set_die_type (die, ftype, cu);
16982
16983 if (die->child != NULL)
16984 {
16985 struct type *void_type = objfile_type (objfile)->builtin_void;
16986 struct die_info *child_die;
16987 int nparams, iparams;
16988
16989 /* Count the number of parameters.
16990 FIXME: GDB currently ignores vararg functions, but knows about
16991 vararg member functions. */
16992 nparams = 0;
16993 child_die = die->child;
16994 while (child_die && child_die->tag)
16995 {
16996 if (child_die->tag == DW_TAG_formal_parameter)
16997 nparams++;
16998 else if (child_die->tag == DW_TAG_unspecified_parameters)
16999 TYPE_VARARGS (ftype) = 1;
17000 child_die = child_die->sibling;
17001 }
17002
17003 /* Allocate storage for parameters and fill them in. */
17004 TYPE_NFIELDS (ftype) = nparams;
17005 TYPE_FIELDS (ftype) = (struct field *)
17006 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17007
17008 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17009 even if we error out during the parameters reading below. */
17010 for (iparams = 0; iparams < nparams; iparams++)
17011 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17012
17013 iparams = 0;
17014 child_die = die->child;
17015 while (child_die && child_die->tag)
17016 {
17017 if (child_die->tag == DW_TAG_formal_parameter)
17018 {
17019 struct type *arg_type;
17020
17021 /* DWARF version 2 has no clean way to discern C++
17022 static and non-static member functions. G++ helps
17023 GDB by marking the first parameter for non-static
17024 member functions (which is the this pointer) as
17025 artificial. We pass this information to
17026 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17027
17028 DWARF version 3 added DW_AT_object_pointer, which GCC
17029 4.5 does not yet generate. */
17030 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17031 if (attr != nullptr)
17032 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17033 else
17034 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17035 arg_type = die_type (child_die, cu);
17036
17037 /* RealView does not mark THIS as const, which the testsuite
17038 expects. GCC marks THIS as const in method definitions,
17039 but not in the class specifications (GCC PR 43053). */
17040 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17041 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17042 {
17043 int is_this = 0;
17044 struct dwarf2_cu *arg_cu = cu;
17045 const char *name = dwarf2_name (child_die, cu);
17046
17047 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17048 if (attr != nullptr)
17049 {
17050 /* If the compiler emits this, use it. */
17051 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17052 is_this = 1;
17053 }
17054 else if (name && strcmp (name, "this") == 0)
17055 /* Function definitions will have the argument names. */
17056 is_this = 1;
17057 else if (name == NULL && iparams == 0)
17058 /* Declarations may not have the names, so like
17059 elsewhere in GDB, assume an artificial first
17060 argument is "this". */
17061 is_this = 1;
17062
17063 if (is_this)
17064 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17065 arg_type, 0);
17066 }
17067
17068 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17069 iparams++;
17070 }
17071 child_die = child_die->sibling;
17072 }
17073 }
17074
17075 return ftype;
17076 }
17077
17078 static struct type *
17079 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17080 {
17081 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17082 const char *name = NULL;
17083 struct type *this_type, *target_type;
17084
17085 name = dwarf2_full_name (NULL, die, cu);
17086 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17087 TYPE_TARGET_STUB (this_type) = 1;
17088 set_die_type (die, this_type, cu);
17089 target_type = die_type (die, cu);
17090 if (target_type != this_type)
17091 TYPE_TARGET_TYPE (this_type) = target_type;
17092 else
17093 {
17094 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17095 spec and cause infinite loops in GDB. */
17096 complaint (_("Self-referential DW_TAG_typedef "
17097 "- DIE at %s [in module %s]"),
17098 sect_offset_str (die->sect_off), objfile_name (objfile));
17099 TYPE_TARGET_TYPE (this_type) = NULL;
17100 }
17101 if (name == NULL)
17102 {
17103 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17104 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17105 Handle these by just returning the target type, rather than
17106 constructing an anonymous typedef type and trying to handle this
17107 elsewhere. */
17108 set_die_type (die, target_type, cu);
17109 return target_type;
17110 }
17111 return this_type;
17112 }
17113
17114 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17115 (which may be different from NAME) to the architecture back-end to allow
17116 it to guess the correct format if necessary. */
17117
17118 static struct type *
17119 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17120 const char *name_hint, enum bfd_endian byte_order)
17121 {
17122 struct gdbarch *gdbarch = objfile->arch ();
17123 const struct floatformat **format;
17124 struct type *type;
17125
17126 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17127 if (format)
17128 type = init_float_type (objfile, bits, name, format, byte_order);
17129 else
17130 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17131
17132 return type;
17133 }
17134
17135 /* Allocate an integer type of size BITS and name NAME. */
17136
17137 static struct type *
17138 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17139 int bits, int unsigned_p, const char *name)
17140 {
17141 struct type *type;
17142
17143 /* Versions of Intel's C Compiler generate an integer type called "void"
17144 instead of using DW_TAG_unspecified_type. This has been seen on
17145 at least versions 14, 17, and 18. */
17146 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17147 && strcmp (name, "void") == 0)
17148 type = objfile_type (objfile)->builtin_void;
17149 else
17150 type = init_integer_type (objfile, bits, unsigned_p, name);
17151
17152 return type;
17153 }
17154
17155 /* Initialise and return a floating point type of size BITS suitable for
17156 use as a component of a complex number. The NAME_HINT is passed through
17157 when initialising the floating point type and is the name of the complex
17158 type.
17159
17160 As DWARF doesn't currently provide an explicit name for the components
17161 of a complex number, but it can be helpful to have these components
17162 named, we try to select a suitable name based on the size of the
17163 component. */
17164 static struct type *
17165 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17166 struct objfile *objfile,
17167 int bits, const char *name_hint,
17168 enum bfd_endian byte_order)
17169 {
17170 gdbarch *gdbarch = objfile->arch ();
17171 struct type *tt = nullptr;
17172
17173 /* Try to find a suitable floating point builtin type of size BITS.
17174 We're going to use the name of this type as the name for the complex
17175 target type that we are about to create. */
17176 switch (cu->language)
17177 {
17178 case language_fortran:
17179 switch (bits)
17180 {
17181 case 32:
17182 tt = builtin_f_type (gdbarch)->builtin_real;
17183 break;
17184 case 64:
17185 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17186 break;
17187 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17188 case 128:
17189 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17190 break;
17191 }
17192 break;
17193 default:
17194 switch (bits)
17195 {
17196 case 32:
17197 tt = builtin_type (gdbarch)->builtin_float;
17198 break;
17199 case 64:
17200 tt = builtin_type (gdbarch)->builtin_double;
17201 break;
17202 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17203 case 128:
17204 tt = builtin_type (gdbarch)->builtin_long_double;
17205 break;
17206 }
17207 break;
17208 }
17209
17210 /* If the type we found doesn't match the size we were looking for, then
17211 pretend we didn't find a type at all, the complex target type we
17212 create will then be nameless. */
17213 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17214 tt = nullptr;
17215
17216 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17217 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17218 }
17219
17220 /* Find a representation of a given base type and install
17221 it in the TYPE field of the die. */
17222
17223 static struct type *
17224 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17225 {
17226 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17227 struct type *type;
17228 struct attribute *attr;
17229 int encoding = 0, bits = 0;
17230 const char *name;
17231 gdbarch *arch;
17232
17233 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17234 if (attr != nullptr)
17235 encoding = DW_UNSND (attr);
17236 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17237 if (attr != nullptr)
17238 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17239 name = dwarf2_name (die, cu);
17240 if (!name)
17241 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17242
17243 arch = objfile->arch ();
17244 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17245
17246 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17247 if (attr)
17248 {
17249 int endianity = DW_UNSND (attr);
17250
17251 switch (endianity)
17252 {
17253 case DW_END_big:
17254 byte_order = BFD_ENDIAN_BIG;
17255 break;
17256 case DW_END_little:
17257 byte_order = BFD_ENDIAN_LITTLE;
17258 break;
17259 default:
17260 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17261 break;
17262 }
17263 }
17264
17265 switch (encoding)
17266 {
17267 case DW_ATE_address:
17268 /* Turn DW_ATE_address into a void * pointer. */
17269 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17270 type = init_pointer_type (objfile, bits, name, type);
17271 break;
17272 case DW_ATE_boolean:
17273 type = init_boolean_type (objfile, bits, 1, name);
17274 break;
17275 case DW_ATE_complex_float:
17276 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17277 byte_order);
17278 if (TYPE_CODE (type) == TYPE_CODE_ERROR)
17279 {
17280 if (name == nullptr)
17281 {
17282 struct obstack *obstack
17283 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17284 name = obconcat (obstack, "_Complex ", TYPE_NAME (type),
17285 nullptr);
17286 }
17287 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17288 }
17289 else
17290 type = init_complex_type (name, type);
17291 break;
17292 case DW_ATE_decimal_float:
17293 type = init_decfloat_type (objfile, bits, name);
17294 break;
17295 case DW_ATE_float:
17296 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17297 break;
17298 case DW_ATE_signed:
17299 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17300 break;
17301 case DW_ATE_unsigned:
17302 if (cu->language == language_fortran
17303 && name
17304 && startswith (name, "character("))
17305 type = init_character_type (objfile, bits, 1, name);
17306 else
17307 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17308 break;
17309 case DW_ATE_signed_char:
17310 if (cu->language == language_ada || cu->language == language_m2
17311 || cu->language == language_pascal
17312 || cu->language == language_fortran)
17313 type = init_character_type (objfile, bits, 0, name);
17314 else
17315 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17316 break;
17317 case DW_ATE_unsigned_char:
17318 if (cu->language == language_ada || cu->language == language_m2
17319 || cu->language == language_pascal
17320 || cu->language == language_fortran
17321 || cu->language == language_rust)
17322 type = init_character_type (objfile, bits, 1, name);
17323 else
17324 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17325 break;
17326 case DW_ATE_UTF:
17327 {
17328 if (bits == 16)
17329 type = builtin_type (arch)->builtin_char16;
17330 else if (bits == 32)
17331 type = builtin_type (arch)->builtin_char32;
17332 else
17333 {
17334 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17335 bits);
17336 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17337 }
17338 return set_die_type (die, type, cu);
17339 }
17340 break;
17341
17342 default:
17343 complaint (_("unsupported DW_AT_encoding: '%s'"),
17344 dwarf_type_encoding_name (encoding));
17345 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17346 break;
17347 }
17348
17349 if (name && strcmp (name, "char") == 0)
17350 TYPE_NOSIGN (type) = 1;
17351
17352 maybe_set_alignment (cu, die, type);
17353
17354 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17355
17356 return set_die_type (die, type, cu);
17357 }
17358
17359 /* Parse dwarf attribute if it's a block, reference or constant and put the
17360 resulting value of the attribute into struct bound_prop.
17361 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17362
17363 static int
17364 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17365 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17366 struct type *default_type)
17367 {
17368 struct dwarf2_property_baton *baton;
17369 struct obstack *obstack
17370 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17371
17372 gdb_assert (default_type != NULL);
17373
17374 if (attr == NULL || prop == NULL)
17375 return 0;
17376
17377 if (attr->form_is_block ())
17378 {
17379 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17380 baton->property_type = default_type;
17381 baton->locexpr.per_cu = cu->per_cu;
17382 baton->locexpr.size = DW_BLOCK (attr)->size;
17383 baton->locexpr.data = DW_BLOCK (attr)->data;
17384 switch (attr->name)
17385 {
17386 case DW_AT_string_length:
17387 baton->locexpr.is_reference = true;
17388 break;
17389 default:
17390 baton->locexpr.is_reference = false;
17391 break;
17392 }
17393 prop->data.baton = baton;
17394 prop->kind = PROP_LOCEXPR;
17395 gdb_assert (prop->data.baton != NULL);
17396 }
17397 else if (attr->form_is_ref ())
17398 {
17399 struct dwarf2_cu *target_cu = cu;
17400 struct die_info *target_die;
17401 struct attribute *target_attr;
17402
17403 target_die = follow_die_ref (die, attr, &target_cu);
17404 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17405 if (target_attr == NULL)
17406 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17407 target_cu);
17408 if (target_attr == NULL)
17409 return 0;
17410
17411 switch (target_attr->name)
17412 {
17413 case DW_AT_location:
17414 if (target_attr->form_is_section_offset ())
17415 {
17416 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17417 baton->property_type = die_type (target_die, target_cu);
17418 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17419 prop->data.baton = baton;
17420 prop->kind = PROP_LOCLIST;
17421 gdb_assert (prop->data.baton != NULL);
17422 }
17423 else if (target_attr->form_is_block ())
17424 {
17425 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17426 baton->property_type = die_type (target_die, target_cu);
17427 baton->locexpr.per_cu = cu->per_cu;
17428 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17429 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17430 baton->locexpr.is_reference = true;
17431 prop->data.baton = baton;
17432 prop->kind = PROP_LOCEXPR;
17433 gdb_assert (prop->data.baton != NULL);
17434 }
17435 else
17436 {
17437 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17438 "dynamic property");
17439 return 0;
17440 }
17441 break;
17442 case DW_AT_data_member_location:
17443 {
17444 LONGEST offset;
17445
17446 if (!handle_data_member_location (target_die, target_cu,
17447 &offset))
17448 return 0;
17449
17450 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17451 baton->property_type = read_type_die (target_die->parent,
17452 target_cu);
17453 baton->offset_info.offset = offset;
17454 baton->offset_info.type = die_type (target_die, target_cu);
17455 prop->data.baton = baton;
17456 prop->kind = PROP_ADDR_OFFSET;
17457 break;
17458 }
17459 }
17460 }
17461 else if (attr->form_is_constant ())
17462 {
17463 prop->data.const_val = attr->constant_value (0);
17464 prop->kind = PROP_CONST;
17465 }
17466 else
17467 {
17468 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17469 dwarf2_name (die, cu));
17470 return 0;
17471 }
17472
17473 return 1;
17474 }
17475
17476 /* See read.h. */
17477
17478 struct type *
17479 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17480 {
17481 struct objfile *objfile = dwarf2_per_objfile->objfile;
17482 struct type *int_type;
17483
17484 /* Helper macro to examine the various builtin types. */
17485 #define TRY_TYPE(F) \
17486 int_type = (unsigned_p \
17487 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17488 : objfile_type (objfile)->builtin_ ## F); \
17489 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17490 return int_type
17491
17492 TRY_TYPE (char);
17493 TRY_TYPE (short);
17494 TRY_TYPE (int);
17495 TRY_TYPE (long);
17496 TRY_TYPE (long_long);
17497
17498 #undef TRY_TYPE
17499
17500 gdb_assert_not_reached ("unable to find suitable integer type");
17501 }
17502
17503 /* See read.h. */
17504
17505 struct type *
17506 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17507 {
17508 int addr_size = this->addr_size ();
17509 return int_type (addr_size, unsigned_p);
17510 }
17511
17512 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17513 present (which is valid) then compute the default type based on the
17514 compilation units address size. */
17515
17516 static struct type *
17517 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17518 {
17519 struct type *index_type = die_type (die, cu);
17520
17521 /* Dwarf-2 specifications explicitly allows to create subrange types
17522 without specifying a base type.
17523 In that case, the base type must be set to the type of
17524 the lower bound, upper bound or count, in that order, if any of these
17525 three attributes references an object that has a type.
17526 If no base type is found, the Dwarf-2 specifications say that
17527 a signed integer type of size equal to the size of an address should
17528 be used.
17529 For the following C code: `extern char gdb_int [];'
17530 GCC produces an empty range DIE.
17531 FIXME: muller/2010-05-28: Possible references to object for low bound,
17532 high bound or count are not yet handled by this code. */
17533 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17534 index_type = cu->per_cu->addr_sized_int_type (false);
17535
17536 return index_type;
17537 }
17538
17539 /* Read the given DW_AT_subrange DIE. */
17540
17541 static struct type *
17542 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17543 {
17544 struct type *base_type, *orig_base_type;
17545 struct type *range_type;
17546 struct attribute *attr;
17547 struct dynamic_prop low, high;
17548 int low_default_is_valid;
17549 int high_bound_is_count = 0;
17550 const char *name;
17551 ULONGEST negative_mask;
17552
17553 orig_base_type = read_subrange_index_type (die, cu);
17554
17555 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17556 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17557 creating the range type, but we use the result of check_typedef
17558 when examining properties of the type. */
17559 base_type = check_typedef (orig_base_type);
17560
17561 /* The die_type call above may have already set the type for this DIE. */
17562 range_type = get_die_type (die, cu);
17563 if (range_type)
17564 return range_type;
17565
17566 low.kind = PROP_CONST;
17567 high.kind = PROP_CONST;
17568 high.data.const_val = 0;
17569
17570 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17571 omitting DW_AT_lower_bound. */
17572 switch (cu->language)
17573 {
17574 case language_c:
17575 case language_cplus:
17576 low.data.const_val = 0;
17577 low_default_is_valid = 1;
17578 break;
17579 case language_fortran:
17580 low.data.const_val = 1;
17581 low_default_is_valid = 1;
17582 break;
17583 case language_d:
17584 case language_objc:
17585 case language_rust:
17586 low.data.const_val = 0;
17587 low_default_is_valid = (cu->header.version >= 4);
17588 break;
17589 case language_ada:
17590 case language_m2:
17591 case language_pascal:
17592 low.data.const_val = 1;
17593 low_default_is_valid = (cu->header.version >= 4);
17594 break;
17595 default:
17596 low.data.const_val = 0;
17597 low_default_is_valid = 0;
17598 break;
17599 }
17600
17601 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17602 if (attr != nullptr)
17603 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17604 else if (!low_default_is_valid)
17605 complaint (_("Missing DW_AT_lower_bound "
17606 "- DIE at %s [in module %s]"),
17607 sect_offset_str (die->sect_off),
17608 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17609
17610 struct attribute *attr_ub, *attr_count;
17611 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17612 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17613 {
17614 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17615 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17616 {
17617 /* If bounds are constant do the final calculation here. */
17618 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17619 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17620 else
17621 high_bound_is_count = 1;
17622 }
17623 else
17624 {
17625 if (attr_ub != NULL)
17626 complaint (_("Unresolved DW_AT_upper_bound "
17627 "- DIE at %s [in module %s]"),
17628 sect_offset_str (die->sect_off),
17629 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17630 if (attr_count != NULL)
17631 complaint (_("Unresolved DW_AT_count "
17632 "- DIE at %s [in module %s]"),
17633 sect_offset_str (die->sect_off),
17634 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17635 }
17636 }
17637
17638 LONGEST bias = 0;
17639 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17640 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17641 bias = bias_attr->constant_value (0);
17642
17643 /* Normally, the DWARF producers are expected to use a signed
17644 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17645 But this is unfortunately not always the case, as witnessed
17646 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17647 is used instead. To work around that ambiguity, we treat
17648 the bounds as signed, and thus sign-extend their values, when
17649 the base type is signed. */
17650 negative_mask =
17651 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17652 if (low.kind == PROP_CONST
17653 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17654 low.data.const_val |= negative_mask;
17655 if (high.kind == PROP_CONST
17656 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17657 high.data.const_val |= negative_mask;
17658
17659 /* Check for bit and byte strides. */
17660 struct dynamic_prop byte_stride_prop;
17661 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17662 if (attr_byte_stride != nullptr)
17663 {
17664 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17665 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17666 prop_type);
17667 }
17668
17669 struct dynamic_prop bit_stride_prop;
17670 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17671 if (attr_bit_stride != nullptr)
17672 {
17673 /* It only makes sense to have either a bit or byte stride. */
17674 if (attr_byte_stride != nullptr)
17675 {
17676 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17677 "- DIE at %s [in module %s]"),
17678 sect_offset_str (die->sect_off),
17679 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17680 attr_bit_stride = nullptr;
17681 }
17682 else
17683 {
17684 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17685 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17686 prop_type);
17687 }
17688 }
17689
17690 if (attr_byte_stride != nullptr
17691 || attr_bit_stride != nullptr)
17692 {
17693 bool byte_stride_p = (attr_byte_stride != nullptr);
17694 struct dynamic_prop *stride
17695 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17696
17697 range_type
17698 = create_range_type_with_stride (NULL, orig_base_type, &low,
17699 &high, bias, stride, byte_stride_p);
17700 }
17701 else
17702 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17703
17704 if (high_bound_is_count)
17705 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17706
17707 /* Ada expects an empty array on no boundary attributes. */
17708 if (attr == NULL && cu->language != language_ada)
17709 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17710
17711 name = dwarf2_name (die, cu);
17712 if (name)
17713 TYPE_NAME (range_type) = name;
17714
17715 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17716 if (attr != nullptr)
17717 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17718
17719 maybe_set_alignment (cu, die, range_type);
17720
17721 set_die_type (die, range_type, cu);
17722
17723 /* set_die_type should be already done. */
17724 set_descriptive_type (range_type, die, cu);
17725
17726 return range_type;
17727 }
17728
17729 static struct type *
17730 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17731 {
17732 struct type *type;
17733
17734 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17735 NULL);
17736 TYPE_NAME (type) = dwarf2_name (die, cu);
17737
17738 /* In Ada, an unspecified type is typically used when the description
17739 of the type is deferred to a different unit. When encountering
17740 such a type, we treat it as a stub, and try to resolve it later on,
17741 when needed. */
17742 if (cu->language == language_ada)
17743 TYPE_STUB (type) = 1;
17744
17745 return set_die_type (die, type, cu);
17746 }
17747
17748 /* Read a single die and all its descendents. Set the die's sibling
17749 field to NULL; set other fields in the die correctly, and set all
17750 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17751 location of the info_ptr after reading all of those dies. PARENT
17752 is the parent of the die in question. */
17753
17754 static struct die_info *
17755 read_die_and_children (const struct die_reader_specs *reader,
17756 const gdb_byte *info_ptr,
17757 const gdb_byte **new_info_ptr,
17758 struct die_info *parent)
17759 {
17760 struct die_info *die;
17761 const gdb_byte *cur_ptr;
17762
17763 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17764 if (die == NULL)
17765 {
17766 *new_info_ptr = cur_ptr;
17767 return NULL;
17768 }
17769 store_in_ref_table (die, reader->cu);
17770
17771 if (die->has_children)
17772 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17773 else
17774 {
17775 die->child = NULL;
17776 *new_info_ptr = cur_ptr;
17777 }
17778
17779 die->sibling = NULL;
17780 die->parent = parent;
17781 return die;
17782 }
17783
17784 /* Read a die, all of its descendents, and all of its siblings; set
17785 all of the fields of all of the dies correctly. Arguments are as
17786 in read_die_and_children. */
17787
17788 static struct die_info *
17789 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17790 const gdb_byte *info_ptr,
17791 const gdb_byte **new_info_ptr,
17792 struct die_info *parent)
17793 {
17794 struct die_info *first_die, *last_sibling;
17795 const gdb_byte *cur_ptr;
17796
17797 cur_ptr = info_ptr;
17798 first_die = last_sibling = NULL;
17799
17800 while (1)
17801 {
17802 struct die_info *die
17803 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17804
17805 if (die == NULL)
17806 {
17807 *new_info_ptr = cur_ptr;
17808 return first_die;
17809 }
17810
17811 if (!first_die)
17812 first_die = die;
17813 else
17814 last_sibling->sibling = die;
17815
17816 last_sibling = die;
17817 }
17818 }
17819
17820 /* Read a die, all of its descendents, and all of its siblings; set
17821 all of the fields of all of the dies correctly. Arguments are as
17822 in read_die_and_children.
17823 This the main entry point for reading a DIE and all its children. */
17824
17825 static struct die_info *
17826 read_die_and_siblings (const struct die_reader_specs *reader,
17827 const gdb_byte *info_ptr,
17828 const gdb_byte **new_info_ptr,
17829 struct die_info *parent)
17830 {
17831 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17832 new_info_ptr, parent);
17833
17834 if (dwarf_die_debug)
17835 {
17836 fprintf_unfiltered (gdb_stdlog,
17837 "Read die from %s@0x%x of %s:\n",
17838 reader->die_section->get_name (),
17839 (unsigned) (info_ptr - reader->die_section->buffer),
17840 bfd_get_filename (reader->abfd));
17841 dump_die (die, dwarf_die_debug);
17842 }
17843
17844 return die;
17845 }
17846
17847 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17848 attributes.
17849 The caller is responsible for filling in the extra attributes
17850 and updating (*DIEP)->num_attrs.
17851 Set DIEP to point to a newly allocated die with its information,
17852 except for its child, sibling, and parent fields. */
17853
17854 static const gdb_byte *
17855 read_full_die_1 (const struct die_reader_specs *reader,
17856 struct die_info **diep, const gdb_byte *info_ptr,
17857 int num_extra_attrs)
17858 {
17859 unsigned int abbrev_number, bytes_read, i;
17860 struct abbrev_info *abbrev;
17861 struct die_info *die;
17862 struct dwarf2_cu *cu = reader->cu;
17863 bfd *abfd = reader->abfd;
17864
17865 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17866 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17867 info_ptr += bytes_read;
17868 if (!abbrev_number)
17869 {
17870 *diep = NULL;
17871 return info_ptr;
17872 }
17873
17874 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17875 if (!abbrev)
17876 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17877 abbrev_number,
17878 bfd_get_filename (abfd));
17879
17880 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17881 die->sect_off = sect_off;
17882 die->tag = abbrev->tag;
17883 die->abbrev = abbrev_number;
17884 die->has_children = abbrev->has_children;
17885
17886 /* Make the result usable.
17887 The caller needs to update num_attrs after adding the extra
17888 attributes. */
17889 die->num_attrs = abbrev->num_attrs;
17890
17891 std::vector<int> indexes_that_need_reprocess;
17892 for (i = 0; i < abbrev->num_attrs; ++i)
17893 {
17894 bool need_reprocess;
17895 info_ptr =
17896 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17897 info_ptr, &need_reprocess);
17898 if (need_reprocess)
17899 indexes_that_need_reprocess.push_back (i);
17900 }
17901
17902 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
17903 if (attr != nullptr)
17904 cu->str_offsets_base = DW_UNSND (attr);
17905
17906 attr = die->attr (DW_AT_loclists_base);
17907 if (attr != nullptr)
17908 cu->loclist_base = DW_UNSND (attr);
17909
17910 auto maybe_addr_base = die->addr_base ();
17911 if (maybe_addr_base.has_value ())
17912 cu->addr_base = *maybe_addr_base;
17913 for (int index : indexes_that_need_reprocess)
17914 read_attribute_reprocess (reader, &die->attrs[index]);
17915 *diep = die;
17916 return info_ptr;
17917 }
17918
17919 /* Read a die and all its attributes.
17920 Set DIEP to point to a newly allocated die with its information,
17921 except for its child, sibling, and parent fields. */
17922
17923 static const gdb_byte *
17924 read_full_die (const struct die_reader_specs *reader,
17925 struct die_info **diep, const gdb_byte *info_ptr)
17926 {
17927 const gdb_byte *result;
17928
17929 result = read_full_die_1 (reader, diep, info_ptr, 0);
17930
17931 if (dwarf_die_debug)
17932 {
17933 fprintf_unfiltered (gdb_stdlog,
17934 "Read die from %s@0x%x of %s:\n",
17935 reader->die_section->get_name (),
17936 (unsigned) (info_ptr - reader->die_section->buffer),
17937 bfd_get_filename (reader->abfd));
17938 dump_die (*diep, dwarf_die_debug);
17939 }
17940
17941 return result;
17942 }
17943 \f
17944
17945 /* Returns nonzero if TAG represents a type that we might generate a partial
17946 symbol for. */
17947
17948 static int
17949 is_type_tag_for_partial (int tag)
17950 {
17951 switch (tag)
17952 {
17953 #if 0
17954 /* Some types that would be reasonable to generate partial symbols for,
17955 that we don't at present. */
17956 case DW_TAG_array_type:
17957 case DW_TAG_file_type:
17958 case DW_TAG_ptr_to_member_type:
17959 case DW_TAG_set_type:
17960 case DW_TAG_string_type:
17961 case DW_TAG_subroutine_type:
17962 #endif
17963 case DW_TAG_base_type:
17964 case DW_TAG_class_type:
17965 case DW_TAG_interface_type:
17966 case DW_TAG_enumeration_type:
17967 case DW_TAG_structure_type:
17968 case DW_TAG_subrange_type:
17969 case DW_TAG_typedef:
17970 case DW_TAG_union_type:
17971 return 1;
17972 default:
17973 return 0;
17974 }
17975 }
17976
17977 /* Load all DIEs that are interesting for partial symbols into memory. */
17978
17979 static struct partial_die_info *
17980 load_partial_dies (const struct die_reader_specs *reader,
17981 const gdb_byte *info_ptr, int building_psymtab)
17982 {
17983 struct dwarf2_cu *cu = reader->cu;
17984 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17985 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17986 unsigned int bytes_read;
17987 unsigned int load_all = 0;
17988 int nesting_level = 1;
17989
17990 parent_die = NULL;
17991 last_die = NULL;
17992
17993 gdb_assert (cu->per_cu != NULL);
17994 if (cu->per_cu->load_all_dies)
17995 load_all = 1;
17996
17997 cu->partial_dies
17998 = htab_create_alloc_ex (cu->header.length / 12,
17999 partial_die_hash,
18000 partial_die_eq,
18001 NULL,
18002 &cu->comp_unit_obstack,
18003 hashtab_obstack_allocate,
18004 dummy_obstack_deallocate);
18005
18006 while (1)
18007 {
18008 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18009
18010 /* A NULL abbrev means the end of a series of children. */
18011 if (abbrev == NULL)
18012 {
18013 if (--nesting_level == 0)
18014 return first_die;
18015
18016 info_ptr += bytes_read;
18017 last_die = parent_die;
18018 parent_die = parent_die->die_parent;
18019 continue;
18020 }
18021
18022 /* Check for template arguments. We never save these; if
18023 they're seen, we just mark the parent, and go on our way. */
18024 if (parent_die != NULL
18025 && cu->language == language_cplus
18026 && (abbrev->tag == DW_TAG_template_type_param
18027 || abbrev->tag == DW_TAG_template_value_param))
18028 {
18029 parent_die->has_template_arguments = 1;
18030
18031 if (!load_all)
18032 {
18033 /* We don't need a partial DIE for the template argument. */
18034 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18035 continue;
18036 }
18037 }
18038
18039 /* We only recurse into c++ subprograms looking for template arguments.
18040 Skip their other children. */
18041 if (!load_all
18042 && cu->language == language_cplus
18043 && parent_die != NULL
18044 && parent_die->tag == DW_TAG_subprogram)
18045 {
18046 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18047 continue;
18048 }
18049
18050 /* Check whether this DIE is interesting enough to save. Normally
18051 we would not be interested in members here, but there may be
18052 later variables referencing them via DW_AT_specification (for
18053 static members). */
18054 if (!load_all
18055 && !is_type_tag_for_partial (abbrev->tag)
18056 && abbrev->tag != DW_TAG_constant
18057 && abbrev->tag != DW_TAG_enumerator
18058 && abbrev->tag != DW_TAG_subprogram
18059 && abbrev->tag != DW_TAG_inlined_subroutine
18060 && abbrev->tag != DW_TAG_lexical_block
18061 && abbrev->tag != DW_TAG_variable
18062 && abbrev->tag != DW_TAG_namespace
18063 && abbrev->tag != DW_TAG_module
18064 && abbrev->tag != DW_TAG_member
18065 && abbrev->tag != DW_TAG_imported_unit
18066 && abbrev->tag != DW_TAG_imported_declaration)
18067 {
18068 /* Otherwise we skip to the next sibling, if any. */
18069 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18070 continue;
18071 }
18072
18073 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18074 abbrev);
18075
18076 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18077
18078 /* This two-pass algorithm for processing partial symbols has a
18079 high cost in cache pressure. Thus, handle some simple cases
18080 here which cover the majority of C partial symbols. DIEs
18081 which neither have specification tags in them, nor could have
18082 specification tags elsewhere pointing at them, can simply be
18083 processed and discarded.
18084
18085 This segment is also optional; scan_partial_symbols and
18086 add_partial_symbol will handle these DIEs if we chain
18087 them in normally. When compilers which do not emit large
18088 quantities of duplicate debug information are more common,
18089 this code can probably be removed. */
18090
18091 /* Any complete simple types at the top level (pretty much all
18092 of them, for a language without namespaces), can be processed
18093 directly. */
18094 if (parent_die == NULL
18095 && pdi.has_specification == 0
18096 && pdi.is_declaration == 0
18097 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18098 || pdi.tag == DW_TAG_base_type
18099 || pdi.tag == DW_TAG_subrange_type))
18100 {
18101 if (building_psymtab && pdi.name != NULL)
18102 add_psymbol_to_list (pdi.name, false,
18103 VAR_DOMAIN, LOC_TYPEDEF, -1,
18104 psymbol_placement::STATIC,
18105 0, cu->language, objfile);
18106 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18107 continue;
18108 }
18109
18110 /* The exception for DW_TAG_typedef with has_children above is
18111 a workaround of GCC PR debug/47510. In the case of this complaint
18112 type_name_or_error will error on such types later.
18113
18114 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18115 it could not find the child DIEs referenced later, this is checked
18116 above. In correct DWARF DW_TAG_typedef should have no children. */
18117
18118 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18119 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18120 "- DIE at %s [in module %s]"),
18121 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18122
18123 /* If we're at the second level, and we're an enumerator, and
18124 our parent has no specification (meaning possibly lives in a
18125 namespace elsewhere), then we can add the partial symbol now
18126 instead of queueing it. */
18127 if (pdi.tag == DW_TAG_enumerator
18128 && parent_die != NULL
18129 && parent_die->die_parent == NULL
18130 && parent_die->tag == DW_TAG_enumeration_type
18131 && parent_die->has_specification == 0)
18132 {
18133 if (pdi.name == NULL)
18134 complaint (_("malformed enumerator DIE ignored"));
18135 else if (building_psymtab)
18136 add_psymbol_to_list (pdi.name, false,
18137 VAR_DOMAIN, LOC_CONST, -1,
18138 cu->language == language_cplus
18139 ? psymbol_placement::GLOBAL
18140 : psymbol_placement::STATIC,
18141 0, cu->language, objfile);
18142
18143 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18144 continue;
18145 }
18146
18147 struct partial_die_info *part_die
18148 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18149
18150 /* We'll save this DIE so link it in. */
18151 part_die->die_parent = parent_die;
18152 part_die->die_sibling = NULL;
18153 part_die->die_child = NULL;
18154
18155 if (last_die && last_die == parent_die)
18156 last_die->die_child = part_die;
18157 else if (last_die)
18158 last_die->die_sibling = part_die;
18159
18160 last_die = part_die;
18161
18162 if (first_die == NULL)
18163 first_die = part_die;
18164
18165 /* Maybe add the DIE to the hash table. Not all DIEs that we
18166 find interesting need to be in the hash table, because we
18167 also have the parent/sibling/child chains; only those that we
18168 might refer to by offset later during partial symbol reading.
18169
18170 For now this means things that might have be the target of a
18171 DW_AT_specification, DW_AT_abstract_origin, or
18172 DW_AT_extension. DW_AT_extension will refer only to
18173 namespaces; DW_AT_abstract_origin refers to functions (and
18174 many things under the function DIE, but we do not recurse
18175 into function DIEs during partial symbol reading) and
18176 possibly variables as well; DW_AT_specification refers to
18177 declarations. Declarations ought to have the DW_AT_declaration
18178 flag. It happens that GCC forgets to put it in sometimes, but
18179 only for functions, not for types.
18180
18181 Adding more things than necessary to the hash table is harmless
18182 except for the performance cost. Adding too few will result in
18183 wasted time in find_partial_die, when we reread the compilation
18184 unit with load_all_dies set. */
18185
18186 if (load_all
18187 || abbrev->tag == DW_TAG_constant
18188 || abbrev->tag == DW_TAG_subprogram
18189 || abbrev->tag == DW_TAG_variable
18190 || abbrev->tag == DW_TAG_namespace
18191 || part_die->is_declaration)
18192 {
18193 void **slot;
18194
18195 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18196 to_underlying (part_die->sect_off),
18197 INSERT);
18198 *slot = part_die;
18199 }
18200
18201 /* For some DIEs we want to follow their children (if any). For C
18202 we have no reason to follow the children of structures; for other
18203 languages we have to, so that we can get at method physnames
18204 to infer fully qualified class names, for DW_AT_specification,
18205 and for C++ template arguments. For C++, we also look one level
18206 inside functions to find template arguments (if the name of the
18207 function does not already contain the template arguments).
18208
18209 For Ada and Fortran, we need to scan the children of subprograms
18210 and lexical blocks as well because these languages allow the
18211 definition of nested entities that could be interesting for the
18212 debugger, such as nested subprograms for instance. */
18213 if (last_die->has_children
18214 && (load_all
18215 || last_die->tag == DW_TAG_namespace
18216 || last_die->tag == DW_TAG_module
18217 || last_die->tag == DW_TAG_enumeration_type
18218 || (cu->language == language_cplus
18219 && last_die->tag == DW_TAG_subprogram
18220 && (last_die->name == NULL
18221 || strchr (last_die->name, '<') == NULL))
18222 || (cu->language != language_c
18223 && (last_die->tag == DW_TAG_class_type
18224 || last_die->tag == DW_TAG_interface_type
18225 || last_die->tag == DW_TAG_structure_type
18226 || last_die->tag == DW_TAG_union_type))
18227 || ((cu->language == language_ada
18228 || cu->language == language_fortran)
18229 && (last_die->tag == DW_TAG_subprogram
18230 || last_die->tag == DW_TAG_lexical_block))))
18231 {
18232 nesting_level++;
18233 parent_die = last_die;
18234 continue;
18235 }
18236
18237 /* Otherwise we skip to the next sibling, if any. */
18238 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18239
18240 /* Back to the top, do it again. */
18241 }
18242 }
18243
18244 partial_die_info::partial_die_info (sect_offset sect_off_,
18245 struct abbrev_info *abbrev)
18246 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18247 {
18248 }
18249
18250 /* Read a minimal amount of information into the minimal die structure.
18251 INFO_PTR should point just after the initial uleb128 of a DIE. */
18252
18253 const gdb_byte *
18254 partial_die_info::read (const struct die_reader_specs *reader,
18255 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18256 {
18257 struct dwarf2_cu *cu = reader->cu;
18258 struct dwarf2_per_objfile *dwarf2_per_objfile
18259 = cu->per_cu->dwarf2_per_objfile;
18260 unsigned int i;
18261 int has_low_pc_attr = 0;
18262 int has_high_pc_attr = 0;
18263 int high_pc_relative = 0;
18264
18265 for (i = 0; i < abbrev.num_attrs; ++i)
18266 {
18267 attribute attr;
18268 bool need_reprocess;
18269 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18270 info_ptr, &need_reprocess);
18271 /* String and address offsets that need to do the reprocessing have
18272 already been read at this point, so there is no need to wait until
18273 the loop terminates to do the reprocessing. */
18274 if (need_reprocess)
18275 read_attribute_reprocess (reader, &attr);
18276 /* Store the data if it is of an attribute we want to keep in a
18277 partial symbol table. */
18278 switch (attr.name)
18279 {
18280 case DW_AT_name:
18281 switch (tag)
18282 {
18283 case DW_TAG_compile_unit:
18284 case DW_TAG_partial_unit:
18285 case DW_TAG_type_unit:
18286 /* Compilation units have a DW_AT_name that is a filename, not
18287 a source language identifier. */
18288 case DW_TAG_enumeration_type:
18289 case DW_TAG_enumerator:
18290 /* These tags always have simple identifiers already; no need
18291 to canonicalize them. */
18292 name = DW_STRING (&attr);
18293 break;
18294 default:
18295 {
18296 struct objfile *objfile = dwarf2_per_objfile->objfile;
18297
18298 name
18299 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18300 }
18301 break;
18302 }
18303 break;
18304 case DW_AT_linkage_name:
18305 case DW_AT_MIPS_linkage_name:
18306 /* Note that both forms of linkage name might appear. We
18307 assume they will be the same, and we only store the last
18308 one we see. */
18309 linkage_name = attr.value_as_string ();
18310 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18311 See https://github.com/rust-lang/rust/issues/32925. */
18312 if (cu->language == language_rust && linkage_name != NULL
18313 && strchr (linkage_name, '{') != NULL)
18314 linkage_name = NULL;
18315 break;
18316 case DW_AT_low_pc:
18317 has_low_pc_attr = 1;
18318 lowpc = attr.value_as_address ();
18319 break;
18320 case DW_AT_high_pc:
18321 has_high_pc_attr = 1;
18322 highpc = attr.value_as_address ();
18323 if (cu->header.version >= 4 && attr.form_is_constant ())
18324 high_pc_relative = 1;
18325 break;
18326 case DW_AT_location:
18327 /* Support the .debug_loc offsets. */
18328 if (attr.form_is_block ())
18329 {
18330 d.locdesc = DW_BLOCK (&attr);
18331 }
18332 else if (attr.form_is_section_offset ())
18333 {
18334 dwarf2_complex_location_expr_complaint ();
18335 }
18336 else
18337 {
18338 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18339 "partial symbol information");
18340 }
18341 break;
18342 case DW_AT_external:
18343 is_external = DW_UNSND (&attr);
18344 break;
18345 case DW_AT_declaration:
18346 is_declaration = DW_UNSND (&attr);
18347 break;
18348 case DW_AT_type:
18349 has_type = 1;
18350 break;
18351 case DW_AT_abstract_origin:
18352 case DW_AT_specification:
18353 case DW_AT_extension:
18354 has_specification = 1;
18355 spec_offset = attr.get_ref_die_offset ();
18356 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18357 || cu->per_cu->is_dwz);
18358 break;
18359 case DW_AT_sibling:
18360 /* Ignore absolute siblings, they might point outside of
18361 the current compile unit. */
18362 if (attr.form == DW_FORM_ref_addr)
18363 complaint (_("ignoring absolute DW_AT_sibling"));
18364 else
18365 {
18366 const gdb_byte *buffer = reader->buffer;
18367 sect_offset off = attr.get_ref_die_offset ();
18368 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18369
18370 if (sibling_ptr < info_ptr)
18371 complaint (_("DW_AT_sibling points backwards"));
18372 else if (sibling_ptr > reader->buffer_end)
18373 reader->die_section->overflow_complaint ();
18374 else
18375 sibling = sibling_ptr;
18376 }
18377 break;
18378 case DW_AT_byte_size:
18379 has_byte_size = 1;
18380 break;
18381 case DW_AT_const_value:
18382 has_const_value = 1;
18383 break;
18384 case DW_AT_calling_convention:
18385 /* DWARF doesn't provide a way to identify a program's source-level
18386 entry point. DW_AT_calling_convention attributes are only meant
18387 to describe functions' calling conventions.
18388
18389 However, because it's a necessary piece of information in
18390 Fortran, and before DWARF 4 DW_CC_program was the only
18391 piece of debugging information whose definition refers to
18392 a 'main program' at all, several compilers marked Fortran
18393 main programs with DW_CC_program --- even when those
18394 functions use the standard calling conventions.
18395
18396 Although DWARF now specifies a way to provide this
18397 information, we support this practice for backward
18398 compatibility. */
18399 if (DW_UNSND (&attr) == DW_CC_program
18400 && cu->language == language_fortran)
18401 main_subprogram = 1;
18402 break;
18403 case DW_AT_inline:
18404 if (DW_UNSND (&attr) == DW_INL_inlined
18405 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18406 may_be_inlined = 1;
18407 break;
18408
18409 case DW_AT_import:
18410 if (tag == DW_TAG_imported_unit)
18411 {
18412 d.sect_off = attr.get_ref_die_offset ();
18413 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18414 || cu->per_cu->is_dwz);
18415 }
18416 break;
18417
18418 case DW_AT_main_subprogram:
18419 main_subprogram = DW_UNSND (&attr);
18420 break;
18421
18422 case DW_AT_ranges:
18423 {
18424 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18425 but that requires a full DIE, so instead we just
18426 reimplement it. */
18427 int need_ranges_base = tag != DW_TAG_compile_unit;
18428 unsigned int ranges_offset = (DW_UNSND (&attr)
18429 + (need_ranges_base
18430 ? cu->ranges_base
18431 : 0));
18432
18433 /* Value of the DW_AT_ranges attribute is the offset in the
18434 .debug_ranges section. */
18435 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18436 nullptr))
18437 has_pc_info = 1;
18438 }
18439 break;
18440
18441 default:
18442 break;
18443 }
18444 }
18445
18446 /* For Ada, if both the name and the linkage name appear, we prefer
18447 the latter. This lets "catch exception" work better, regardless
18448 of the order in which the name and linkage name were emitted.
18449 Really, though, this is just a workaround for the fact that gdb
18450 doesn't store both the name and the linkage name. */
18451 if (cu->language == language_ada && linkage_name != nullptr)
18452 name = linkage_name;
18453
18454 if (high_pc_relative)
18455 highpc += lowpc;
18456
18457 if (has_low_pc_attr && has_high_pc_attr)
18458 {
18459 /* When using the GNU linker, .gnu.linkonce. sections are used to
18460 eliminate duplicate copies of functions and vtables and such.
18461 The linker will arbitrarily choose one and discard the others.
18462 The AT_*_pc values for such functions refer to local labels in
18463 these sections. If the section from that file was discarded, the
18464 labels are not in the output, so the relocs get a value of 0.
18465 If this is a discarded function, mark the pc bounds as invalid,
18466 so that GDB will ignore it. */
18467 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18468 {
18469 struct objfile *objfile = dwarf2_per_objfile->objfile;
18470 struct gdbarch *gdbarch = objfile->arch ();
18471
18472 complaint (_("DW_AT_low_pc %s is zero "
18473 "for DIE at %s [in module %s]"),
18474 paddress (gdbarch, lowpc),
18475 sect_offset_str (sect_off),
18476 objfile_name (objfile));
18477 }
18478 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18479 else if (lowpc >= highpc)
18480 {
18481 struct objfile *objfile = dwarf2_per_objfile->objfile;
18482 struct gdbarch *gdbarch = objfile->arch ();
18483
18484 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18485 "for DIE at %s [in module %s]"),
18486 paddress (gdbarch, lowpc),
18487 paddress (gdbarch, highpc),
18488 sect_offset_str (sect_off),
18489 objfile_name (objfile));
18490 }
18491 else
18492 has_pc_info = 1;
18493 }
18494
18495 return info_ptr;
18496 }
18497
18498 /* Find a cached partial DIE at OFFSET in CU. */
18499
18500 struct partial_die_info *
18501 dwarf2_cu::find_partial_die (sect_offset sect_off)
18502 {
18503 struct partial_die_info *lookup_die = NULL;
18504 struct partial_die_info part_die (sect_off);
18505
18506 lookup_die = ((struct partial_die_info *)
18507 htab_find_with_hash (partial_dies, &part_die,
18508 to_underlying (sect_off)));
18509
18510 return lookup_die;
18511 }
18512
18513 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18514 except in the case of .debug_types DIEs which do not reference
18515 outside their CU (they do however referencing other types via
18516 DW_FORM_ref_sig8). */
18517
18518 static const struct cu_partial_die_info
18519 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18520 {
18521 struct dwarf2_per_objfile *dwarf2_per_objfile
18522 = cu->per_cu->dwarf2_per_objfile;
18523 struct objfile *objfile = dwarf2_per_objfile->objfile;
18524 struct dwarf2_per_cu_data *per_cu = NULL;
18525 struct partial_die_info *pd = NULL;
18526
18527 if (offset_in_dwz == cu->per_cu->is_dwz
18528 && cu->header.offset_in_cu_p (sect_off))
18529 {
18530 pd = cu->find_partial_die (sect_off);
18531 if (pd != NULL)
18532 return { cu, pd };
18533 /* We missed recording what we needed.
18534 Load all dies and try again. */
18535 per_cu = cu->per_cu;
18536 }
18537 else
18538 {
18539 /* TUs don't reference other CUs/TUs (except via type signatures). */
18540 if (cu->per_cu->is_debug_types)
18541 {
18542 error (_("Dwarf Error: Type Unit at offset %s contains"
18543 " external reference to offset %s [in module %s].\n"),
18544 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18545 bfd_get_filename (objfile->obfd));
18546 }
18547 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18548 dwarf2_per_objfile);
18549
18550 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18551 load_partial_comp_unit (per_cu);
18552
18553 per_cu->cu->last_used = 0;
18554 pd = per_cu->cu->find_partial_die (sect_off);
18555 }
18556
18557 /* If we didn't find it, and not all dies have been loaded,
18558 load them all and try again. */
18559
18560 if (pd == NULL && per_cu->load_all_dies == 0)
18561 {
18562 per_cu->load_all_dies = 1;
18563
18564 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18565 THIS_CU->cu may already be in use. So we can't just free it and
18566 replace its DIEs with the ones we read in. Instead, we leave those
18567 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18568 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18569 set. */
18570 load_partial_comp_unit (per_cu);
18571
18572 pd = per_cu->cu->find_partial_die (sect_off);
18573 }
18574
18575 if (pd == NULL)
18576 internal_error (__FILE__, __LINE__,
18577 _("could not find partial DIE %s "
18578 "in cache [from module %s]\n"),
18579 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18580 return { per_cu->cu, pd };
18581 }
18582
18583 /* See if we can figure out if the class lives in a namespace. We do
18584 this by looking for a member function; its demangled name will
18585 contain namespace info, if there is any. */
18586
18587 static void
18588 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18589 struct dwarf2_cu *cu)
18590 {
18591 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18592 what template types look like, because the demangler
18593 frequently doesn't give the same name as the debug info. We
18594 could fix this by only using the demangled name to get the
18595 prefix (but see comment in read_structure_type). */
18596
18597 struct partial_die_info *real_pdi;
18598 struct partial_die_info *child_pdi;
18599
18600 /* If this DIE (this DIE's specification, if any) has a parent, then
18601 we should not do this. We'll prepend the parent's fully qualified
18602 name when we create the partial symbol. */
18603
18604 real_pdi = struct_pdi;
18605 while (real_pdi->has_specification)
18606 {
18607 auto res = find_partial_die (real_pdi->spec_offset,
18608 real_pdi->spec_is_dwz, cu);
18609 real_pdi = res.pdi;
18610 cu = res.cu;
18611 }
18612
18613 if (real_pdi->die_parent != NULL)
18614 return;
18615
18616 for (child_pdi = struct_pdi->die_child;
18617 child_pdi != NULL;
18618 child_pdi = child_pdi->die_sibling)
18619 {
18620 if (child_pdi->tag == DW_TAG_subprogram
18621 && child_pdi->linkage_name != NULL)
18622 {
18623 gdb::unique_xmalloc_ptr<char> actual_class_name
18624 (language_class_name_from_physname (cu->language_defn,
18625 child_pdi->linkage_name));
18626 if (actual_class_name != NULL)
18627 {
18628 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18629 struct_pdi->name = objfile->intern (actual_class_name.get ());
18630 }
18631 break;
18632 }
18633 }
18634 }
18635
18636 /* Return true if a DIE with TAG may have the DW_AT_const_value
18637 attribute. */
18638
18639 static bool
18640 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18641 {
18642 switch (tag)
18643 {
18644 case DW_TAG_constant:
18645 case DW_TAG_enumerator:
18646 case DW_TAG_formal_parameter:
18647 case DW_TAG_template_value_param:
18648 case DW_TAG_variable:
18649 return true;
18650 }
18651
18652 return false;
18653 }
18654
18655 void
18656 partial_die_info::fixup (struct dwarf2_cu *cu)
18657 {
18658 /* Once we've fixed up a die, there's no point in doing so again.
18659 This also avoids a memory leak if we were to call
18660 guess_partial_die_structure_name multiple times. */
18661 if (fixup_called)
18662 return;
18663
18664 /* If we found a reference attribute and the DIE has no name, try
18665 to find a name in the referred to DIE. */
18666
18667 if (name == NULL && has_specification)
18668 {
18669 struct partial_die_info *spec_die;
18670
18671 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18672 spec_die = res.pdi;
18673 cu = res.cu;
18674
18675 spec_die->fixup (cu);
18676
18677 if (spec_die->name)
18678 {
18679 name = spec_die->name;
18680
18681 /* Copy DW_AT_external attribute if it is set. */
18682 if (spec_die->is_external)
18683 is_external = spec_die->is_external;
18684 }
18685 }
18686
18687 if (!has_const_value && has_specification
18688 && can_have_DW_AT_const_value_p (tag))
18689 {
18690 struct partial_die_info *spec_die;
18691
18692 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18693 spec_die = res.pdi;
18694 cu = res.cu;
18695
18696 spec_die->fixup (cu);
18697
18698 if (spec_die->has_const_value)
18699 {
18700 /* Copy DW_AT_const_value attribute if it is set. */
18701 has_const_value = spec_die->has_const_value;
18702 }
18703 }
18704
18705 /* Set default names for some unnamed DIEs. */
18706
18707 if (name == NULL && tag == DW_TAG_namespace)
18708 name = CP_ANONYMOUS_NAMESPACE_STR;
18709
18710 /* If there is no parent die to provide a namespace, and there are
18711 children, see if we can determine the namespace from their linkage
18712 name. */
18713 if (cu->language == language_cplus
18714 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18715 && die_parent == NULL
18716 && has_children
18717 && (tag == DW_TAG_class_type
18718 || tag == DW_TAG_structure_type
18719 || tag == DW_TAG_union_type))
18720 guess_partial_die_structure_name (this, cu);
18721
18722 /* GCC might emit a nameless struct or union that has a linkage
18723 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18724 if (name == NULL
18725 && (tag == DW_TAG_class_type
18726 || tag == DW_TAG_interface_type
18727 || tag == DW_TAG_structure_type
18728 || tag == DW_TAG_union_type)
18729 && linkage_name != NULL)
18730 {
18731 gdb::unique_xmalloc_ptr<char> demangled
18732 (gdb_demangle (linkage_name, DMGL_TYPES));
18733 if (demangled != nullptr)
18734 {
18735 const char *base;
18736
18737 /* Strip any leading namespaces/classes, keep only the base name.
18738 DW_AT_name for named DIEs does not contain the prefixes. */
18739 base = strrchr (demangled.get (), ':');
18740 if (base && base > demangled.get () && base[-1] == ':')
18741 base++;
18742 else
18743 base = demangled.get ();
18744
18745 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18746 name = objfile->intern (base);
18747 }
18748 }
18749
18750 fixup_called = 1;
18751 }
18752
18753 /* Read the .debug_loclists header contents from the given SECTION in the
18754 HEADER. */
18755 static void
18756 read_loclist_header (struct loclist_header *header,
18757 struct dwarf2_section_info *section)
18758 {
18759 unsigned int bytes_read;
18760 bfd *abfd = section->get_bfd_owner ();
18761 const gdb_byte *info_ptr = section->buffer;
18762 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18763 info_ptr += bytes_read;
18764 header->version = read_2_bytes (abfd, info_ptr);
18765 info_ptr += 2;
18766 header->addr_size = read_1_byte (abfd, info_ptr);
18767 info_ptr += 1;
18768 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18769 info_ptr += 1;
18770 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18771 }
18772
18773 /* Return the DW_AT_loclists_base value for the CU. */
18774 static ULONGEST
18775 lookup_loclist_base (struct dwarf2_cu *cu)
18776 {
18777 /* For the .dwo unit, the loclist_base points to the first offset following
18778 the header. The header consists of the following entities-
18779 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18780 bit format)
18781 2. version (2 bytes)
18782 3. address size (1 byte)
18783 4. segment selector size (1 byte)
18784 5. offset entry count (4 bytes)
18785 These sizes are derived as per the DWARFv5 standard. */
18786 if (cu->dwo_unit != nullptr)
18787 {
18788 if (cu->header.initial_length_size == 4)
18789 return LOCLIST_HEADER_SIZE32;
18790 return LOCLIST_HEADER_SIZE64;
18791 }
18792 return cu->loclist_base;
18793 }
18794
18795 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18796 array of offsets in the .debug_loclists section. */
18797 static CORE_ADDR
18798 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18799 {
18800 struct dwarf2_per_objfile *dwarf2_per_objfile
18801 = cu->per_cu->dwarf2_per_objfile;
18802 struct objfile *objfile = dwarf2_per_objfile->objfile;
18803 bfd *abfd = objfile->obfd;
18804 ULONGEST loclist_base = lookup_loclist_base (cu);
18805 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18806
18807 section->read (objfile);
18808 if (section->buffer == NULL)
18809 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18810 "section [in module %s]"), objfile_name (objfile));
18811 struct loclist_header header;
18812 read_loclist_header (&header, section);
18813 if (loclist_index >= header.offset_entry_count)
18814 complaint (_("DW_FORM_loclistx pointing outside of "
18815 ".debug_loclists offset array [in module %s]"),
18816 objfile_name (objfile));
18817 if (loclist_base + loclist_index * cu->header.offset_size
18818 >= section->size)
18819 complaint (_("DW_FORM_loclistx pointing outside of "
18820 ".debug_loclists section [in module %s]"),
18821 objfile_name (objfile));
18822 const gdb_byte *info_ptr
18823 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18824
18825 if (cu->header.offset_size == 4)
18826 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18827 else
18828 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18829 }
18830
18831 /* Process the attributes that had to be skipped in the first round. These
18832 attributes are the ones that need str_offsets_base or addr_base attributes.
18833 They could not have been processed in the first round, because at the time
18834 the values of str_offsets_base or addr_base may not have been known. */
18835 static void
18836 read_attribute_reprocess (const struct die_reader_specs *reader,
18837 struct attribute *attr)
18838 {
18839 struct dwarf2_cu *cu = reader->cu;
18840 switch (attr->form)
18841 {
18842 case DW_FORM_addrx:
18843 case DW_FORM_GNU_addr_index:
18844 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18845 break;
18846 case DW_FORM_loclistx:
18847 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18848 break;
18849 case DW_FORM_strx:
18850 case DW_FORM_strx1:
18851 case DW_FORM_strx2:
18852 case DW_FORM_strx3:
18853 case DW_FORM_strx4:
18854 case DW_FORM_GNU_str_index:
18855 {
18856 unsigned int str_index = DW_UNSND (attr);
18857 if (reader->dwo_file != NULL)
18858 {
18859 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18860 DW_STRING_IS_CANONICAL (attr) = 0;
18861 }
18862 else
18863 {
18864 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18865 DW_STRING_IS_CANONICAL (attr) = 0;
18866 }
18867 break;
18868 }
18869 default:
18870 gdb_assert_not_reached (_("Unexpected DWARF form."));
18871 }
18872 }
18873
18874 /* Read an attribute value described by an attribute form. */
18875
18876 static const gdb_byte *
18877 read_attribute_value (const struct die_reader_specs *reader,
18878 struct attribute *attr, unsigned form,
18879 LONGEST implicit_const, const gdb_byte *info_ptr,
18880 bool *need_reprocess)
18881 {
18882 struct dwarf2_cu *cu = reader->cu;
18883 struct dwarf2_per_objfile *dwarf2_per_objfile
18884 = cu->per_cu->dwarf2_per_objfile;
18885 struct objfile *objfile = dwarf2_per_objfile->objfile;
18886 bfd *abfd = reader->abfd;
18887 struct comp_unit_head *cu_header = &cu->header;
18888 unsigned int bytes_read;
18889 struct dwarf_block *blk;
18890 *need_reprocess = false;
18891
18892 attr->form = (enum dwarf_form) form;
18893 switch (form)
18894 {
18895 case DW_FORM_ref_addr:
18896 if (cu->header.version == 2)
18897 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18898 &bytes_read);
18899 else
18900 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18901 &bytes_read);
18902 info_ptr += bytes_read;
18903 break;
18904 case DW_FORM_GNU_ref_alt:
18905 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18906 info_ptr += bytes_read;
18907 break;
18908 case DW_FORM_addr:
18909 {
18910 struct gdbarch *gdbarch = objfile->arch ();
18911 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18912 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18913 info_ptr += bytes_read;
18914 }
18915 break;
18916 case DW_FORM_block2:
18917 blk = dwarf_alloc_block (cu);
18918 blk->size = read_2_bytes (abfd, info_ptr);
18919 info_ptr += 2;
18920 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18921 info_ptr += blk->size;
18922 DW_BLOCK (attr) = blk;
18923 break;
18924 case DW_FORM_block4:
18925 blk = dwarf_alloc_block (cu);
18926 blk->size = read_4_bytes (abfd, info_ptr);
18927 info_ptr += 4;
18928 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18929 info_ptr += blk->size;
18930 DW_BLOCK (attr) = blk;
18931 break;
18932 case DW_FORM_data2:
18933 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18934 info_ptr += 2;
18935 break;
18936 case DW_FORM_data4:
18937 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18938 info_ptr += 4;
18939 break;
18940 case DW_FORM_data8:
18941 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18942 info_ptr += 8;
18943 break;
18944 case DW_FORM_data16:
18945 blk = dwarf_alloc_block (cu);
18946 blk->size = 16;
18947 blk->data = read_n_bytes (abfd, info_ptr, 16);
18948 info_ptr += 16;
18949 DW_BLOCK (attr) = blk;
18950 break;
18951 case DW_FORM_sec_offset:
18952 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18953 info_ptr += bytes_read;
18954 break;
18955 case DW_FORM_loclistx:
18956 {
18957 *need_reprocess = true;
18958 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18959 info_ptr += bytes_read;
18960 }
18961 break;
18962 case DW_FORM_string:
18963 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18964 DW_STRING_IS_CANONICAL (attr) = 0;
18965 info_ptr += bytes_read;
18966 break;
18967 case DW_FORM_strp:
18968 if (!cu->per_cu->is_dwz)
18969 {
18970 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18971 abfd, info_ptr, cu_header,
18972 &bytes_read);
18973 DW_STRING_IS_CANONICAL (attr) = 0;
18974 info_ptr += bytes_read;
18975 break;
18976 }
18977 /* FALLTHROUGH */
18978 case DW_FORM_line_strp:
18979 if (!cu->per_cu->is_dwz)
18980 {
18981 DW_STRING (attr)
18982 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
18983 &bytes_read);
18984 DW_STRING_IS_CANONICAL (attr) = 0;
18985 info_ptr += bytes_read;
18986 break;
18987 }
18988 /* FALLTHROUGH */
18989 case DW_FORM_GNU_strp_alt:
18990 {
18991 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18992 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18993 &bytes_read);
18994
18995 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
18996 DW_STRING_IS_CANONICAL (attr) = 0;
18997 info_ptr += bytes_read;
18998 }
18999 break;
19000 case DW_FORM_exprloc:
19001 case DW_FORM_block:
19002 blk = dwarf_alloc_block (cu);
19003 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19004 info_ptr += bytes_read;
19005 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19006 info_ptr += blk->size;
19007 DW_BLOCK (attr) = blk;
19008 break;
19009 case DW_FORM_block1:
19010 blk = dwarf_alloc_block (cu);
19011 blk->size = read_1_byte (abfd, info_ptr);
19012 info_ptr += 1;
19013 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19014 info_ptr += blk->size;
19015 DW_BLOCK (attr) = blk;
19016 break;
19017 case DW_FORM_data1:
19018 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19019 info_ptr += 1;
19020 break;
19021 case DW_FORM_flag:
19022 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19023 info_ptr += 1;
19024 break;
19025 case DW_FORM_flag_present:
19026 DW_UNSND (attr) = 1;
19027 break;
19028 case DW_FORM_sdata:
19029 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19030 info_ptr += bytes_read;
19031 break;
19032 case DW_FORM_udata:
19033 case DW_FORM_rnglistx:
19034 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19035 info_ptr += bytes_read;
19036 break;
19037 case DW_FORM_ref1:
19038 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19039 + read_1_byte (abfd, info_ptr));
19040 info_ptr += 1;
19041 break;
19042 case DW_FORM_ref2:
19043 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19044 + read_2_bytes (abfd, info_ptr));
19045 info_ptr += 2;
19046 break;
19047 case DW_FORM_ref4:
19048 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19049 + read_4_bytes (abfd, info_ptr));
19050 info_ptr += 4;
19051 break;
19052 case DW_FORM_ref8:
19053 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19054 + read_8_bytes (abfd, info_ptr));
19055 info_ptr += 8;
19056 break;
19057 case DW_FORM_ref_sig8:
19058 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19059 info_ptr += 8;
19060 break;
19061 case DW_FORM_ref_udata:
19062 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19063 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19064 info_ptr += bytes_read;
19065 break;
19066 case DW_FORM_indirect:
19067 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19068 info_ptr += bytes_read;
19069 if (form == DW_FORM_implicit_const)
19070 {
19071 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19072 info_ptr += bytes_read;
19073 }
19074 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19075 info_ptr, need_reprocess);
19076 break;
19077 case DW_FORM_implicit_const:
19078 DW_SND (attr) = implicit_const;
19079 break;
19080 case DW_FORM_addrx:
19081 case DW_FORM_GNU_addr_index:
19082 *need_reprocess = true;
19083 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19084 info_ptr += bytes_read;
19085 break;
19086 case DW_FORM_strx:
19087 case DW_FORM_strx1:
19088 case DW_FORM_strx2:
19089 case DW_FORM_strx3:
19090 case DW_FORM_strx4:
19091 case DW_FORM_GNU_str_index:
19092 {
19093 ULONGEST str_index;
19094 if (form == DW_FORM_strx1)
19095 {
19096 str_index = read_1_byte (abfd, info_ptr);
19097 info_ptr += 1;
19098 }
19099 else if (form == DW_FORM_strx2)
19100 {
19101 str_index = read_2_bytes (abfd, info_ptr);
19102 info_ptr += 2;
19103 }
19104 else if (form == DW_FORM_strx3)
19105 {
19106 str_index = read_3_bytes (abfd, info_ptr);
19107 info_ptr += 3;
19108 }
19109 else if (form == DW_FORM_strx4)
19110 {
19111 str_index = read_4_bytes (abfd, info_ptr);
19112 info_ptr += 4;
19113 }
19114 else
19115 {
19116 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19117 info_ptr += bytes_read;
19118 }
19119 *need_reprocess = true;
19120 DW_UNSND (attr) = str_index;
19121 }
19122 break;
19123 default:
19124 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19125 dwarf_form_name (form),
19126 bfd_get_filename (abfd));
19127 }
19128
19129 /* Super hack. */
19130 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19131 attr->form = DW_FORM_GNU_ref_alt;
19132
19133 /* We have seen instances where the compiler tried to emit a byte
19134 size attribute of -1 which ended up being encoded as an unsigned
19135 0xffffffff. Although 0xffffffff is technically a valid size value,
19136 an object of this size seems pretty unlikely so we can relatively
19137 safely treat these cases as if the size attribute was invalid and
19138 treat them as zero by default. */
19139 if (attr->name == DW_AT_byte_size
19140 && form == DW_FORM_data4
19141 && DW_UNSND (attr) >= 0xffffffff)
19142 {
19143 complaint
19144 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19145 hex_string (DW_UNSND (attr)));
19146 DW_UNSND (attr) = 0;
19147 }
19148
19149 return info_ptr;
19150 }
19151
19152 /* Read an attribute described by an abbreviated attribute. */
19153
19154 static const gdb_byte *
19155 read_attribute (const struct die_reader_specs *reader,
19156 struct attribute *attr, struct attr_abbrev *abbrev,
19157 const gdb_byte *info_ptr, bool *need_reprocess)
19158 {
19159 attr->name = abbrev->name;
19160 return read_attribute_value (reader, attr, abbrev->form,
19161 abbrev->implicit_const, info_ptr,
19162 need_reprocess);
19163 }
19164
19165 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19166
19167 static const char *
19168 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19169 LONGEST str_offset)
19170 {
19171 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
19172 str_offset, "DW_FORM_strp");
19173 }
19174
19175 /* Return pointer to string at .debug_str offset as read from BUF.
19176 BUF is assumed to be in a compilation unit described by CU_HEADER.
19177 Return *BYTES_READ_PTR count of bytes read from BUF. */
19178
19179 static const char *
19180 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19181 const gdb_byte *buf,
19182 const struct comp_unit_head *cu_header,
19183 unsigned int *bytes_read_ptr)
19184 {
19185 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19186
19187 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
19188 }
19189
19190 /* See read.h. */
19191
19192 const char *
19193 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19194 const struct comp_unit_head *cu_header,
19195 unsigned int *bytes_read_ptr)
19196 {
19197 bfd *abfd = objfile->obfd;
19198 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19199
19200 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19201 }
19202
19203 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19204 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19205 ADDR_SIZE is the size of addresses from the CU header. */
19206
19207 static CORE_ADDR
19208 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19209 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19210 int addr_size)
19211 {
19212 struct objfile *objfile = dwarf2_per_objfile->objfile;
19213 bfd *abfd = objfile->obfd;
19214 const gdb_byte *info_ptr;
19215 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19216
19217 dwarf2_per_objfile->addr.read (objfile);
19218 if (dwarf2_per_objfile->addr.buffer == NULL)
19219 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19220 objfile_name (objfile));
19221 if (addr_base_or_zero + addr_index * addr_size
19222 >= dwarf2_per_objfile->addr.size)
19223 error (_("DW_FORM_addr_index pointing outside of "
19224 ".debug_addr section [in module %s]"),
19225 objfile_name (objfile));
19226 info_ptr = (dwarf2_per_objfile->addr.buffer
19227 + addr_base_or_zero + addr_index * addr_size);
19228 if (addr_size == 4)
19229 return bfd_get_32 (abfd, info_ptr);
19230 else
19231 return bfd_get_64 (abfd, info_ptr);
19232 }
19233
19234 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19235
19236 static CORE_ADDR
19237 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19238 {
19239 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19240 cu->addr_base, cu->header.addr_size);
19241 }
19242
19243 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19244
19245 static CORE_ADDR
19246 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19247 unsigned int *bytes_read)
19248 {
19249 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19250 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19251
19252 return read_addr_index (cu, addr_index);
19253 }
19254
19255 /* See read.h. */
19256
19257 CORE_ADDR
19258 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
19259 {
19260 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19261 struct dwarf2_cu *cu = per_cu->cu;
19262 gdb::optional<ULONGEST> addr_base;
19263 int addr_size;
19264
19265 /* We need addr_base and addr_size.
19266 If we don't have PER_CU->cu, we have to get it.
19267 Nasty, but the alternative is storing the needed info in PER_CU,
19268 which at this point doesn't seem justified: it's not clear how frequently
19269 it would get used and it would increase the size of every PER_CU.
19270 Entry points like dwarf2_per_cu_addr_size do a similar thing
19271 so we're not in uncharted territory here.
19272 Alas we need to be a bit more complicated as addr_base is contained
19273 in the DIE.
19274
19275 We don't need to read the entire CU(/TU).
19276 We just need the header and top level die.
19277
19278 IWBN to use the aging mechanism to let us lazily later discard the CU.
19279 For now we skip this optimization. */
19280
19281 if (cu != NULL)
19282 {
19283 addr_base = cu->addr_base;
19284 addr_size = cu->header.addr_size;
19285 }
19286 else
19287 {
19288 cutu_reader reader (per_cu, NULL, 0, false);
19289 addr_base = reader.cu->addr_base;
19290 addr_size = reader.cu->header.addr_size;
19291 }
19292
19293 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19294 addr_size);
19295 }
19296
19297 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19298 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19299 DWO file. */
19300
19301 static const char *
19302 read_str_index (struct dwarf2_cu *cu,
19303 struct dwarf2_section_info *str_section,
19304 struct dwarf2_section_info *str_offsets_section,
19305 ULONGEST str_offsets_base, ULONGEST str_index)
19306 {
19307 struct dwarf2_per_objfile *dwarf2_per_objfile
19308 = cu->per_cu->dwarf2_per_objfile;
19309 struct objfile *objfile = dwarf2_per_objfile->objfile;
19310 const char *objf_name = objfile_name (objfile);
19311 bfd *abfd = objfile->obfd;
19312 const gdb_byte *info_ptr;
19313 ULONGEST str_offset;
19314 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19315
19316 str_section->read (objfile);
19317 str_offsets_section->read (objfile);
19318 if (str_section->buffer == NULL)
19319 error (_("%s used without %s section"
19320 " in CU at offset %s [in module %s]"),
19321 form_name, str_section->get_name (),
19322 sect_offset_str (cu->header.sect_off), objf_name);
19323 if (str_offsets_section->buffer == NULL)
19324 error (_("%s used without %s section"
19325 " in CU at offset %s [in module %s]"),
19326 form_name, str_section->get_name (),
19327 sect_offset_str (cu->header.sect_off), objf_name);
19328 info_ptr = (str_offsets_section->buffer
19329 + str_offsets_base
19330 + str_index * cu->header.offset_size);
19331 if (cu->header.offset_size == 4)
19332 str_offset = bfd_get_32 (abfd, info_ptr);
19333 else
19334 str_offset = bfd_get_64 (abfd, info_ptr);
19335 if (str_offset >= str_section->size)
19336 error (_("Offset from %s pointing outside of"
19337 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19338 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19339 return (const char *) (str_section->buffer + str_offset);
19340 }
19341
19342 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19343
19344 static const char *
19345 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19346 {
19347 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19348 ? reader->cu->header.addr_size : 0;
19349 return read_str_index (reader->cu,
19350 &reader->dwo_file->sections.str,
19351 &reader->dwo_file->sections.str_offsets,
19352 str_offsets_base, str_index);
19353 }
19354
19355 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19356
19357 static const char *
19358 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19359 {
19360 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19361 const char *objf_name = objfile_name (objfile);
19362 static const char form_name[] = "DW_FORM_GNU_str_index";
19363 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19364
19365 if (!cu->str_offsets_base.has_value ())
19366 error (_("%s used in Fission stub without %s"
19367 " in CU at offset 0x%lx [in module %s]"),
19368 form_name, str_offsets_attr_name,
19369 (long) cu->header.offset_size, objf_name);
19370
19371 return read_str_index (cu,
19372 &cu->per_cu->dwarf2_per_objfile->str,
19373 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19374 *cu->str_offsets_base, str_index);
19375 }
19376
19377 /* Return the length of an LEB128 number in BUF. */
19378
19379 static int
19380 leb128_size (const gdb_byte *buf)
19381 {
19382 const gdb_byte *begin = buf;
19383 gdb_byte byte;
19384
19385 while (1)
19386 {
19387 byte = *buf++;
19388 if ((byte & 128) == 0)
19389 return buf - begin;
19390 }
19391 }
19392
19393 static void
19394 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19395 {
19396 switch (lang)
19397 {
19398 case DW_LANG_C89:
19399 case DW_LANG_C99:
19400 case DW_LANG_C11:
19401 case DW_LANG_C:
19402 case DW_LANG_UPC:
19403 cu->language = language_c;
19404 break;
19405 case DW_LANG_Java:
19406 case DW_LANG_C_plus_plus:
19407 case DW_LANG_C_plus_plus_11:
19408 case DW_LANG_C_plus_plus_14:
19409 cu->language = language_cplus;
19410 break;
19411 case DW_LANG_D:
19412 cu->language = language_d;
19413 break;
19414 case DW_LANG_Fortran77:
19415 case DW_LANG_Fortran90:
19416 case DW_LANG_Fortran95:
19417 case DW_LANG_Fortran03:
19418 case DW_LANG_Fortran08:
19419 cu->language = language_fortran;
19420 break;
19421 case DW_LANG_Go:
19422 cu->language = language_go;
19423 break;
19424 case DW_LANG_Mips_Assembler:
19425 cu->language = language_asm;
19426 break;
19427 case DW_LANG_Ada83:
19428 case DW_LANG_Ada95:
19429 cu->language = language_ada;
19430 break;
19431 case DW_LANG_Modula2:
19432 cu->language = language_m2;
19433 break;
19434 case DW_LANG_Pascal83:
19435 cu->language = language_pascal;
19436 break;
19437 case DW_LANG_ObjC:
19438 cu->language = language_objc;
19439 break;
19440 case DW_LANG_Rust:
19441 case DW_LANG_Rust_old:
19442 cu->language = language_rust;
19443 break;
19444 case DW_LANG_Cobol74:
19445 case DW_LANG_Cobol85:
19446 default:
19447 cu->language = language_minimal;
19448 break;
19449 }
19450 cu->language_defn = language_def (cu->language);
19451 }
19452
19453 /* Return the named attribute or NULL if not there. */
19454
19455 static struct attribute *
19456 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19457 {
19458 for (;;)
19459 {
19460 unsigned int i;
19461 struct attribute *spec = NULL;
19462
19463 for (i = 0; i < die->num_attrs; ++i)
19464 {
19465 if (die->attrs[i].name == name)
19466 return &die->attrs[i];
19467 if (die->attrs[i].name == DW_AT_specification
19468 || die->attrs[i].name == DW_AT_abstract_origin)
19469 spec = &die->attrs[i];
19470 }
19471
19472 if (!spec)
19473 break;
19474
19475 die = follow_die_ref (die, spec, &cu);
19476 }
19477
19478 return NULL;
19479 }
19480
19481 /* Return the string associated with a string-typed attribute, or NULL if it
19482 is either not found or is of an incorrect type. */
19483
19484 static const char *
19485 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19486 {
19487 struct attribute *attr;
19488 const char *str = NULL;
19489
19490 attr = dwarf2_attr (die, name, cu);
19491
19492 if (attr != NULL)
19493 {
19494 str = attr->value_as_string ();
19495 if (str == nullptr)
19496 complaint (_("string type expected for attribute %s for "
19497 "DIE at %s in module %s"),
19498 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19499 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19500 }
19501
19502 return str;
19503 }
19504
19505 /* Return the dwo name or NULL if not present. If present, it is in either
19506 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19507 static const char *
19508 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19509 {
19510 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19511 if (dwo_name == nullptr)
19512 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19513 return dwo_name;
19514 }
19515
19516 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19517 and holds a non-zero value. This function should only be used for
19518 DW_FORM_flag or DW_FORM_flag_present attributes. */
19519
19520 static int
19521 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19522 {
19523 struct attribute *attr = dwarf2_attr (die, name, cu);
19524
19525 return (attr && DW_UNSND (attr));
19526 }
19527
19528 static int
19529 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19530 {
19531 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19532 which value is non-zero. However, we have to be careful with
19533 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19534 (via dwarf2_flag_true_p) follows this attribute. So we may
19535 end up accidently finding a declaration attribute that belongs
19536 to a different DIE referenced by the specification attribute,
19537 even though the given DIE does not have a declaration attribute. */
19538 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19539 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19540 }
19541
19542 /* Return the die giving the specification for DIE, if there is
19543 one. *SPEC_CU is the CU containing DIE on input, and the CU
19544 containing the return value on output. If there is no
19545 specification, but there is an abstract origin, that is
19546 returned. */
19547
19548 static struct die_info *
19549 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19550 {
19551 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19552 *spec_cu);
19553
19554 if (spec_attr == NULL)
19555 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19556
19557 if (spec_attr == NULL)
19558 return NULL;
19559 else
19560 return follow_die_ref (die, spec_attr, spec_cu);
19561 }
19562
19563 /* Stub for free_line_header to match void * callback types. */
19564
19565 static void
19566 free_line_header_voidp (void *arg)
19567 {
19568 struct line_header *lh = (struct line_header *) arg;
19569
19570 delete lh;
19571 }
19572
19573 /* A convenience function to find the proper .debug_line section for a CU. */
19574
19575 static struct dwarf2_section_info *
19576 get_debug_line_section (struct dwarf2_cu *cu)
19577 {
19578 struct dwarf2_section_info *section;
19579 struct dwarf2_per_objfile *dwarf2_per_objfile
19580 = cu->per_cu->dwarf2_per_objfile;
19581
19582 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19583 DWO file. */
19584 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19585 section = &cu->dwo_unit->dwo_file->sections.line;
19586 else if (cu->per_cu->is_dwz)
19587 {
19588 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19589
19590 section = &dwz->line;
19591 }
19592 else
19593 section = &dwarf2_per_objfile->line;
19594
19595 return section;
19596 }
19597
19598 /* Read the statement program header starting at OFFSET in
19599 .debug_line, or .debug_line.dwo. Return a pointer
19600 to a struct line_header, allocated using xmalloc.
19601 Returns NULL if there is a problem reading the header, e.g., if it
19602 has a version we don't understand.
19603
19604 NOTE: the strings in the include directory and file name tables of
19605 the returned object point into the dwarf line section buffer,
19606 and must not be freed. */
19607
19608 static line_header_up
19609 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19610 {
19611 struct dwarf2_section_info *section;
19612 struct dwarf2_per_objfile *dwarf2_per_objfile
19613 = cu->per_cu->dwarf2_per_objfile;
19614
19615 section = get_debug_line_section (cu);
19616 section->read (dwarf2_per_objfile->objfile);
19617 if (section->buffer == NULL)
19618 {
19619 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19620 complaint (_("missing .debug_line.dwo section"));
19621 else
19622 complaint (_("missing .debug_line section"));
19623 return 0;
19624 }
19625
19626 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19627 dwarf2_per_objfile, section,
19628 &cu->header);
19629 }
19630
19631 /* Subroutine of dwarf_decode_lines to simplify it.
19632 Return the file name of the psymtab for the given file_entry.
19633 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19634 If space for the result is malloc'd, *NAME_HOLDER will be set.
19635 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19636
19637 static const char *
19638 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19639 const dwarf2_psymtab *pst,
19640 const char *comp_dir,
19641 gdb::unique_xmalloc_ptr<char> *name_holder)
19642 {
19643 const char *include_name = fe.name;
19644 const char *include_name_to_compare = include_name;
19645 const char *pst_filename;
19646 int file_is_pst;
19647
19648 const char *dir_name = fe.include_dir (lh);
19649
19650 gdb::unique_xmalloc_ptr<char> hold_compare;
19651 if (!IS_ABSOLUTE_PATH (include_name)
19652 && (dir_name != NULL || comp_dir != NULL))
19653 {
19654 /* Avoid creating a duplicate psymtab for PST.
19655 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19656 Before we do the comparison, however, we need to account
19657 for DIR_NAME and COMP_DIR.
19658 First prepend dir_name (if non-NULL). If we still don't
19659 have an absolute path prepend comp_dir (if non-NULL).
19660 However, the directory we record in the include-file's
19661 psymtab does not contain COMP_DIR (to match the
19662 corresponding symtab(s)).
19663
19664 Example:
19665
19666 bash$ cd /tmp
19667 bash$ gcc -g ./hello.c
19668 include_name = "hello.c"
19669 dir_name = "."
19670 DW_AT_comp_dir = comp_dir = "/tmp"
19671 DW_AT_name = "./hello.c"
19672
19673 */
19674
19675 if (dir_name != NULL)
19676 {
19677 name_holder->reset (concat (dir_name, SLASH_STRING,
19678 include_name, (char *) NULL));
19679 include_name = name_holder->get ();
19680 include_name_to_compare = include_name;
19681 }
19682 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19683 {
19684 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19685 include_name, (char *) NULL));
19686 include_name_to_compare = hold_compare.get ();
19687 }
19688 }
19689
19690 pst_filename = pst->filename;
19691 gdb::unique_xmalloc_ptr<char> copied_name;
19692 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19693 {
19694 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19695 pst_filename, (char *) NULL));
19696 pst_filename = copied_name.get ();
19697 }
19698
19699 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19700
19701 if (file_is_pst)
19702 return NULL;
19703 return include_name;
19704 }
19705
19706 /* State machine to track the state of the line number program. */
19707
19708 class lnp_state_machine
19709 {
19710 public:
19711 /* Initialize a machine state for the start of a line number
19712 program. */
19713 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19714 bool record_lines_p);
19715
19716 file_entry *current_file ()
19717 {
19718 /* lh->file_names is 0-based, but the file name numbers in the
19719 statement program are 1-based. */
19720 return m_line_header->file_name_at (m_file);
19721 }
19722
19723 /* Record the line in the state machine. END_SEQUENCE is true if
19724 we're processing the end of a sequence. */
19725 void record_line (bool end_sequence);
19726
19727 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19728 nop-out rest of the lines in this sequence. */
19729 void check_line_address (struct dwarf2_cu *cu,
19730 const gdb_byte *line_ptr,
19731 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19732
19733 void handle_set_discriminator (unsigned int discriminator)
19734 {
19735 m_discriminator = discriminator;
19736 m_line_has_non_zero_discriminator |= discriminator != 0;
19737 }
19738
19739 /* Handle DW_LNE_set_address. */
19740 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19741 {
19742 m_op_index = 0;
19743 address += baseaddr;
19744 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19745 }
19746
19747 /* Handle DW_LNS_advance_pc. */
19748 void handle_advance_pc (CORE_ADDR adjust);
19749
19750 /* Handle a special opcode. */
19751 void handle_special_opcode (unsigned char op_code);
19752
19753 /* Handle DW_LNS_advance_line. */
19754 void handle_advance_line (int line_delta)
19755 {
19756 advance_line (line_delta);
19757 }
19758
19759 /* Handle DW_LNS_set_file. */
19760 void handle_set_file (file_name_index file);
19761
19762 /* Handle DW_LNS_negate_stmt. */
19763 void handle_negate_stmt ()
19764 {
19765 m_is_stmt = !m_is_stmt;
19766 }
19767
19768 /* Handle DW_LNS_const_add_pc. */
19769 void handle_const_add_pc ();
19770
19771 /* Handle DW_LNS_fixed_advance_pc. */
19772 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19773 {
19774 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19775 m_op_index = 0;
19776 }
19777
19778 /* Handle DW_LNS_copy. */
19779 void handle_copy ()
19780 {
19781 record_line (false);
19782 m_discriminator = 0;
19783 }
19784
19785 /* Handle DW_LNE_end_sequence. */
19786 void handle_end_sequence ()
19787 {
19788 m_currently_recording_lines = true;
19789 }
19790
19791 private:
19792 /* Advance the line by LINE_DELTA. */
19793 void advance_line (int line_delta)
19794 {
19795 m_line += line_delta;
19796
19797 if (line_delta != 0)
19798 m_line_has_non_zero_discriminator = m_discriminator != 0;
19799 }
19800
19801 struct dwarf2_cu *m_cu;
19802
19803 gdbarch *m_gdbarch;
19804
19805 /* True if we're recording lines.
19806 Otherwise we're building partial symtabs and are just interested in
19807 finding include files mentioned by the line number program. */
19808 bool m_record_lines_p;
19809
19810 /* The line number header. */
19811 line_header *m_line_header;
19812
19813 /* These are part of the standard DWARF line number state machine,
19814 and initialized according to the DWARF spec. */
19815
19816 unsigned char m_op_index = 0;
19817 /* The line table index of the current file. */
19818 file_name_index m_file = 1;
19819 unsigned int m_line = 1;
19820
19821 /* These are initialized in the constructor. */
19822
19823 CORE_ADDR m_address;
19824 bool m_is_stmt;
19825 unsigned int m_discriminator;
19826
19827 /* Additional bits of state we need to track. */
19828
19829 /* The last file that we called dwarf2_start_subfile for.
19830 This is only used for TLLs. */
19831 unsigned int m_last_file = 0;
19832 /* The last file a line number was recorded for. */
19833 struct subfile *m_last_subfile = NULL;
19834
19835 /* When true, record the lines we decode. */
19836 bool m_currently_recording_lines = false;
19837
19838 /* The last line number that was recorded, used to coalesce
19839 consecutive entries for the same line. This can happen, for
19840 example, when discriminators are present. PR 17276. */
19841 unsigned int m_last_line = 0;
19842 bool m_line_has_non_zero_discriminator = false;
19843 };
19844
19845 void
19846 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19847 {
19848 CORE_ADDR addr_adj = (((m_op_index + adjust)
19849 / m_line_header->maximum_ops_per_instruction)
19850 * m_line_header->minimum_instruction_length);
19851 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19852 m_op_index = ((m_op_index + adjust)
19853 % m_line_header->maximum_ops_per_instruction);
19854 }
19855
19856 void
19857 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19858 {
19859 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19860 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19861 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19862 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19863 / m_line_header->maximum_ops_per_instruction)
19864 * m_line_header->minimum_instruction_length);
19865 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19866 m_op_index = ((m_op_index + adj_opcode_d)
19867 % m_line_header->maximum_ops_per_instruction);
19868
19869 int line_delta = m_line_header->line_base + adj_opcode_r;
19870 advance_line (line_delta);
19871 record_line (false);
19872 m_discriminator = 0;
19873 }
19874
19875 void
19876 lnp_state_machine::handle_set_file (file_name_index file)
19877 {
19878 m_file = file;
19879
19880 const file_entry *fe = current_file ();
19881 if (fe == NULL)
19882 dwarf2_debug_line_missing_file_complaint ();
19883 else if (m_record_lines_p)
19884 {
19885 const char *dir = fe->include_dir (m_line_header);
19886
19887 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19888 m_line_has_non_zero_discriminator = m_discriminator != 0;
19889 dwarf2_start_subfile (m_cu, fe->name, dir);
19890 }
19891 }
19892
19893 void
19894 lnp_state_machine::handle_const_add_pc ()
19895 {
19896 CORE_ADDR adjust
19897 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19898
19899 CORE_ADDR addr_adj
19900 = (((m_op_index + adjust)
19901 / m_line_header->maximum_ops_per_instruction)
19902 * m_line_header->minimum_instruction_length);
19903
19904 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19905 m_op_index = ((m_op_index + adjust)
19906 % m_line_header->maximum_ops_per_instruction);
19907 }
19908
19909 /* Return non-zero if we should add LINE to the line number table.
19910 LINE is the line to add, LAST_LINE is the last line that was added,
19911 LAST_SUBFILE is the subfile for LAST_LINE.
19912 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19913 had a non-zero discriminator.
19914
19915 We have to be careful in the presence of discriminators.
19916 E.g., for this line:
19917
19918 for (i = 0; i < 100000; i++);
19919
19920 clang can emit four line number entries for that one line,
19921 each with a different discriminator.
19922 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19923
19924 However, we want gdb to coalesce all four entries into one.
19925 Otherwise the user could stepi into the middle of the line and
19926 gdb would get confused about whether the pc really was in the
19927 middle of the line.
19928
19929 Things are further complicated by the fact that two consecutive
19930 line number entries for the same line is a heuristic used by gcc
19931 to denote the end of the prologue. So we can't just discard duplicate
19932 entries, we have to be selective about it. The heuristic we use is
19933 that we only collapse consecutive entries for the same line if at least
19934 one of those entries has a non-zero discriminator. PR 17276.
19935
19936 Note: Addresses in the line number state machine can never go backwards
19937 within one sequence, thus this coalescing is ok. */
19938
19939 static int
19940 dwarf_record_line_p (struct dwarf2_cu *cu,
19941 unsigned int line, unsigned int last_line,
19942 int line_has_non_zero_discriminator,
19943 struct subfile *last_subfile)
19944 {
19945 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19946 return 1;
19947 if (line != last_line)
19948 return 1;
19949 /* Same line for the same file that we've seen already.
19950 As a last check, for pr 17276, only record the line if the line
19951 has never had a non-zero discriminator. */
19952 if (!line_has_non_zero_discriminator)
19953 return 1;
19954 return 0;
19955 }
19956
19957 /* Use the CU's builder to record line number LINE beginning at
19958 address ADDRESS in the line table of subfile SUBFILE. */
19959
19960 static void
19961 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19962 unsigned int line, CORE_ADDR address, bool is_stmt,
19963 struct dwarf2_cu *cu)
19964 {
19965 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19966
19967 if (dwarf_line_debug)
19968 {
19969 fprintf_unfiltered (gdb_stdlog,
19970 "Recording line %u, file %s, address %s\n",
19971 line, lbasename (subfile->name),
19972 paddress (gdbarch, address));
19973 }
19974
19975 if (cu != nullptr)
19976 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
19977 }
19978
19979 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19980 Mark the end of a set of line number records.
19981 The arguments are the same as for dwarf_record_line_1.
19982 If SUBFILE is NULL the request is ignored. */
19983
19984 static void
19985 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19986 CORE_ADDR address, struct dwarf2_cu *cu)
19987 {
19988 if (subfile == NULL)
19989 return;
19990
19991 if (dwarf_line_debug)
19992 {
19993 fprintf_unfiltered (gdb_stdlog,
19994 "Finishing current line, file %s, address %s\n",
19995 lbasename (subfile->name),
19996 paddress (gdbarch, address));
19997 }
19998
19999 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20000 }
20001
20002 void
20003 lnp_state_machine::record_line (bool end_sequence)
20004 {
20005 if (dwarf_line_debug)
20006 {
20007 fprintf_unfiltered (gdb_stdlog,
20008 "Processing actual line %u: file %u,"
20009 " address %s, is_stmt %u, discrim %u%s\n",
20010 m_line, m_file,
20011 paddress (m_gdbarch, m_address),
20012 m_is_stmt, m_discriminator,
20013 (end_sequence ? "\t(end sequence)" : ""));
20014 }
20015
20016 file_entry *fe = current_file ();
20017
20018 if (fe == NULL)
20019 dwarf2_debug_line_missing_file_complaint ();
20020 /* For now we ignore lines not starting on an instruction boundary.
20021 But not when processing end_sequence for compatibility with the
20022 previous version of the code. */
20023 else if (m_op_index == 0 || end_sequence)
20024 {
20025 fe->included_p = 1;
20026 if (m_record_lines_p)
20027 {
20028 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20029 || end_sequence)
20030 {
20031 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20032 m_currently_recording_lines ? m_cu : nullptr);
20033 }
20034
20035 if (!end_sequence)
20036 {
20037 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20038
20039 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20040 m_line_has_non_zero_discriminator,
20041 m_last_subfile))
20042 {
20043 buildsym_compunit *builder = m_cu->get_builder ();
20044 dwarf_record_line_1 (m_gdbarch,
20045 builder->get_current_subfile (),
20046 m_line, m_address, is_stmt,
20047 m_currently_recording_lines ? m_cu : nullptr);
20048 }
20049 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20050 m_last_line = m_line;
20051 }
20052 }
20053 }
20054 }
20055
20056 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20057 line_header *lh, bool record_lines_p)
20058 {
20059 m_cu = cu;
20060 m_gdbarch = arch;
20061 m_record_lines_p = record_lines_p;
20062 m_line_header = lh;
20063
20064 m_currently_recording_lines = true;
20065
20066 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20067 was a line entry for it so that the backend has a chance to adjust it
20068 and also record it in case it needs it. This is currently used by MIPS
20069 code, cf. `mips_adjust_dwarf2_line'. */
20070 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20071 m_is_stmt = lh->default_is_stmt;
20072 m_discriminator = 0;
20073 }
20074
20075 void
20076 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20077 const gdb_byte *line_ptr,
20078 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20079 {
20080 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20081 the pc range of the CU. However, we restrict the test to only ADDRESS
20082 values of zero to preserve GDB's previous behaviour which is to handle
20083 the specific case of a function being GC'd by the linker. */
20084
20085 if (address == 0 && address < unrelocated_lowpc)
20086 {
20087 /* This line table is for a function which has been
20088 GCd by the linker. Ignore it. PR gdb/12528 */
20089
20090 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20091 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20092
20093 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20094 line_offset, objfile_name (objfile));
20095 m_currently_recording_lines = false;
20096 /* Note: m_currently_recording_lines is left as false until we see
20097 DW_LNE_end_sequence. */
20098 }
20099 }
20100
20101 /* Subroutine of dwarf_decode_lines to simplify it.
20102 Process the line number information in LH.
20103 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20104 program in order to set included_p for every referenced header. */
20105
20106 static void
20107 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20108 const int decode_for_pst_p, CORE_ADDR lowpc)
20109 {
20110 const gdb_byte *line_ptr, *extended_end;
20111 const gdb_byte *line_end;
20112 unsigned int bytes_read, extended_len;
20113 unsigned char op_code, extended_op;
20114 CORE_ADDR baseaddr;
20115 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20116 bfd *abfd = objfile->obfd;
20117 struct gdbarch *gdbarch = objfile->arch ();
20118 /* True if we're recording line info (as opposed to building partial
20119 symtabs and just interested in finding include files mentioned by
20120 the line number program). */
20121 bool record_lines_p = !decode_for_pst_p;
20122
20123 baseaddr = objfile->text_section_offset ();
20124
20125 line_ptr = lh->statement_program_start;
20126 line_end = lh->statement_program_end;
20127
20128 /* Read the statement sequences until there's nothing left. */
20129 while (line_ptr < line_end)
20130 {
20131 /* The DWARF line number program state machine. Reset the state
20132 machine at the start of each sequence. */
20133 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20134 bool end_sequence = false;
20135
20136 if (record_lines_p)
20137 {
20138 /* Start a subfile for the current file of the state
20139 machine. */
20140 const file_entry *fe = state_machine.current_file ();
20141
20142 if (fe != NULL)
20143 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20144 }
20145
20146 /* Decode the table. */
20147 while (line_ptr < line_end && !end_sequence)
20148 {
20149 op_code = read_1_byte (abfd, line_ptr);
20150 line_ptr += 1;
20151
20152 if (op_code >= lh->opcode_base)
20153 {
20154 /* Special opcode. */
20155 state_machine.handle_special_opcode (op_code);
20156 }
20157 else switch (op_code)
20158 {
20159 case DW_LNS_extended_op:
20160 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20161 &bytes_read);
20162 line_ptr += bytes_read;
20163 extended_end = line_ptr + extended_len;
20164 extended_op = read_1_byte (abfd, line_ptr);
20165 line_ptr += 1;
20166 switch (extended_op)
20167 {
20168 case DW_LNE_end_sequence:
20169 state_machine.handle_end_sequence ();
20170 end_sequence = true;
20171 break;
20172 case DW_LNE_set_address:
20173 {
20174 CORE_ADDR address
20175 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20176 line_ptr += bytes_read;
20177
20178 state_machine.check_line_address (cu, line_ptr,
20179 lowpc - baseaddr, address);
20180 state_machine.handle_set_address (baseaddr, address);
20181 }
20182 break;
20183 case DW_LNE_define_file:
20184 {
20185 const char *cur_file;
20186 unsigned int mod_time, length;
20187 dir_index dindex;
20188
20189 cur_file = read_direct_string (abfd, line_ptr,
20190 &bytes_read);
20191 line_ptr += bytes_read;
20192 dindex = (dir_index)
20193 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20194 line_ptr += bytes_read;
20195 mod_time =
20196 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20197 line_ptr += bytes_read;
20198 length =
20199 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20200 line_ptr += bytes_read;
20201 lh->add_file_name (cur_file, dindex, mod_time, length);
20202 }
20203 break;
20204 case DW_LNE_set_discriminator:
20205 {
20206 /* The discriminator is not interesting to the
20207 debugger; just ignore it. We still need to
20208 check its value though:
20209 if there are consecutive entries for the same
20210 (non-prologue) line we want to coalesce them.
20211 PR 17276. */
20212 unsigned int discr
20213 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20214 line_ptr += bytes_read;
20215
20216 state_machine.handle_set_discriminator (discr);
20217 }
20218 break;
20219 default:
20220 complaint (_("mangled .debug_line section"));
20221 return;
20222 }
20223 /* Make sure that we parsed the extended op correctly. If e.g.
20224 we expected a different address size than the producer used,
20225 we may have read the wrong number of bytes. */
20226 if (line_ptr != extended_end)
20227 {
20228 complaint (_("mangled .debug_line section"));
20229 return;
20230 }
20231 break;
20232 case DW_LNS_copy:
20233 state_machine.handle_copy ();
20234 break;
20235 case DW_LNS_advance_pc:
20236 {
20237 CORE_ADDR adjust
20238 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20239 line_ptr += bytes_read;
20240
20241 state_machine.handle_advance_pc (adjust);
20242 }
20243 break;
20244 case DW_LNS_advance_line:
20245 {
20246 int line_delta
20247 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20248 line_ptr += bytes_read;
20249
20250 state_machine.handle_advance_line (line_delta);
20251 }
20252 break;
20253 case DW_LNS_set_file:
20254 {
20255 file_name_index file
20256 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20257 &bytes_read);
20258 line_ptr += bytes_read;
20259
20260 state_machine.handle_set_file (file);
20261 }
20262 break;
20263 case DW_LNS_set_column:
20264 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20265 line_ptr += bytes_read;
20266 break;
20267 case DW_LNS_negate_stmt:
20268 state_machine.handle_negate_stmt ();
20269 break;
20270 case DW_LNS_set_basic_block:
20271 break;
20272 /* Add to the address register of the state machine the
20273 address increment value corresponding to special opcode
20274 255. I.e., this value is scaled by the minimum
20275 instruction length since special opcode 255 would have
20276 scaled the increment. */
20277 case DW_LNS_const_add_pc:
20278 state_machine.handle_const_add_pc ();
20279 break;
20280 case DW_LNS_fixed_advance_pc:
20281 {
20282 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20283 line_ptr += 2;
20284
20285 state_machine.handle_fixed_advance_pc (addr_adj);
20286 }
20287 break;
20288 default:
20289 {
20290 /* Unknown standard opcode, ignore it. */
20291 int i;
20292
20293 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20294 {
20295 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20296 line_ptr += bytes_read;
20297 }
20298 }
20299 }
20300 }
20301
20302 if (!end_sequence)
20303 dwarf2_debug_line_missing_end_sequence_complaint ();
20304
20305 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20306 in which case we still finish recording the last line). */
20307 state_machine.record_line (true);
20308 }
20309 }
20310
20311 /* Decode the Line Number Program (LNP) for the given line_header
20312 structure and CU. The actual information extracted and the type
20313 of structures created from the LNP depends on the value of PST.
20314
20315 1. If PST is NULL, then this procedure uses the data from the program
20316 to create all necessary symbol tables, and their linetables.
20317
20318 2. If PST is not NULL, this procedure reads the program to determine
20319 the list of files included by the unit represented by PST, and
20320 builds all the associated partial symbol tables.
20321
20322 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20323 It is used for relative paths in the line table.
20324 NOTE: When processing partial symtabs (pst != NULL),
20325 comp_dir == pst->dirname.
20326
20327 NOTE: It is important that psymtabs have the same file name (via strcmp)
20328 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20329 symtab we don't use it in the name of the psymtabs we create.
20330 E.g. expand_line_sal requires this when finding psymtabs to expand.
20331 A good testcase for this is mb-inline.exp.
20332
20333 LOWPC is the lowest address in CU (or 0 if not known).
20334
20335 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20336 for its PC<->lines mapping information. Otherwise only the filename
20337 table is read in. */
20338
20339 static void
20340 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20341 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20342 CORE_ADDR lowpc, int decode_mapping)
20343 {
20344 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20345 const int decode_for_pst_p = (pst != NULL);
20346
20347 if (decode_mapping)
20348 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20349
20350 if (decode_for_pst_p)
20351 {
20352 /* Now that we're done scanning the Line Header Program, we can
20353 create the psymtab of each included file. */
20354 for (auto &file_entry : lh->file_names ())
20355 if (file_entry.included_p == 1)
20356 {
20357 gdb::unique_xmalloc_ptr<char> name_holder;
20358 const char *include_name =
20359 psymtab_include_file_name (lh, file_entry, pst,
20360 comp_dir, &name_holder);
20361 if (include_name != NULL)
20362 dwarf2_create_include_psymtab (include_name, pst, objfile);
20363 }
20364 }
20365 else
20366 {
20367 /* Make sure a symtab is created for every file, even files
20368 which contain only variables (i.e. no code with associated
20369 line numbers). */
20370 buildsym_compunit *builder = cu->get_builder ();
20371 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20372
20373 for (auto &fe : lh->file_names ())
20374 {
20375 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20376 if (builder->get_current_subfile ()->symtab == NULL)
20377 {
20378 builder->get_current_subfile ()->symtab
20379 = allocate_symtab (cust,
20380 builder->get_current_subfile ()->name);
20381 }
20382 fe.symtab = builder->get_current_subfile ()->symtab;
20383 }
20384 }
20385 }
20386
20387 /* Start a subfile for DWARF. FILENAME is the name of the file and
20388 DIRNAME the name of the source directory which contains FILENAME
20389 or NULL if not known.
20390 This routine tries to keep line numbers from identical absolute and
20391 relative file names in a common subfile.
20392
20393 Using the `list' example from the GDB testsuite, which resides in
20394 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20395 of /srcdir/list0.c yields the following debugging information for list0.c:
20396
20397 DW_AT_name: /srcdir/list0.c
20398 DW_AT_comp_dir: /compdir
20399 files.files[0].name: list0.h
20400 files.files[0].dir: /srcdir
20401 files.files[1].name: list0.c
20402 files.files[1].dir: /srcdir
20403
20404 The line number information for list0.c has to end up in a single
20405 subfile, so that `break /srcdir/list0.c:1' works as expected.
20406 start_subfile will ensure that this happens provided that we pass the
20407 concatenation of files.files[1].dir and files.files[1].name as the
20408 subfile's name. */
20409
20410 static void
20411 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20412 const char *dirname)
20413 {
20414 gdb::unique_xmalloc_ptr<char> copy;
20415
20416 /* In order not to lose the line information directory,
20417 we concatenate it to the filename when it makes sense.
20418 Note that the Dwarf3 standard says (speaking of filenames in line
20419 information): ``The directory index is ignored for file names
20420 that represent full path names''. Thus ignoring dirname in the
20421 `else' branch below isn't an issue. */
20422
20423 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20424 {
20425 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20426 filename = copy.get ();
20427 }
20428
20429 cu->get_builder ()->start_subfile (filename);
20430 }
20431
20432 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20433 buildsym_compunit constructor. */
20434
20435 struct compunit_symtab *
20436 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20437 CORE_ADDR low_pc)
20438 {
20439 gdb_assert (m_builder == nullptr);
20440
20441 m_builder.reset (new struct buildsym_compunit
20442 (per_cu->dwarf2_per_objfile->objfile,
20443 name, comp_dir, language, low_pc));
20444
20445 list_in_scope = get_builder ()->get_file_symbols ();
20446
20447 get_builder ()->record_debugformat ("DWARF 2");
20448 get_builder ()->record_producer (producer);
20449
20450 processing_has_namespace_info = false;
20451
20452 return get_builder ()->get_compunit_symtab ();
20453 }
20454
20455 static void
20456 var_decode_location (struct attribute *attr, struct symbol *sym,
20457 struct dwarf2_cu *cu)
20458 {
20459 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20460 struct comp_unit_head *cu_header = &cu->header;
20461
20462 /* NOTE drow/2003-01-30: There used to be a comment and some special
20463 code here to turn a symbol with DW_AT_external and a
20464 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20465 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20466 with some versions of binutils) where shared libraries could have
20467 relocations against symbols in their debug information - the
20468 minimal symbol would have the right address, but the debug info
20469 would not. It's no longer necessary, because we will explicitly
20470 apply relocations when we read in the debug information now. */
20471
20472 /* A DW_AT_location attribute with no contents indicates that a
20473 variable has been optimized away. */
20474 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20475 {
20476 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20477 return;
20478 }
20479
20480 /* Handle one degenerate form of location expression specially, to
20481 preserve GDB's previous behavior when section offsets are
20482 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20483 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20484
20485 if (attr->form_is_block ()
20486 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20487 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20488 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20489 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20490 && (DW_BLOCK (attr)->size
20491 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20492 {
20493 unsigned int dummy;
20494
20495 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20496 SET_SYMBOL_VALUE_ADDRESS
20497 (sym, cu->header.read_address (objfile->obfd,
20498 DW_BLOCK (attr)->data + 1,
20499 &dummy));
20500 else
20501 SET_SYMBOL_VALUE_ADDRESS
20502 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20503 &dummy));
20504 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20505 fixup_symbol_section (sym, objfile);
20506 SET_SYMBOL_VALUE_ADDRESS
20507 (sym,
20508 SYMBOL_VALUE_ADDRESS (sym)
20509 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20510 return;
20511 }
20512
20513 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20514 expression evaluator, and use LOC_COMPUTED only when necessary
20515 (i.e. when the value of a register or memory location is
20516 referenced, or a thread-local block, etc.). Then again, it might
20517 not be worthwhile. I'm assuming that it isn't unless performance
20518 or memory numbers show me otherwise. */
20519
20520 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20521
20522 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20523 cu->has_loclist = true;
20524 }
20525
20526 /* Given a pointer to a DWARF information entry, figure out if we need
20527 to make a symbol table entry for it, and if so, create a new entry
20528 and return a pointer to it.
20529 If TYPE is NULL, determine symbol type from the die, otherwise
20530 used the passed type.
20531 If SPACE is not NULL, use it to hold the new symbol. If it is
20532 NULL, allocate a new symbol on the objfile's obstack. */
20533
20534 static struct symbol *
20535 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20536 struct symbol *space)
20537 {
20538 struct dwarf2_per_objfile *dwarf2_per_objfile
20539 = cu->per_cu->dwarf2_per_objfile;
20540 struct objfile *objfile = dwarf2_per_objfile->objfile;
20541 struct gdbarch *gdbarch = objfile->arch ();
20542 struct symbol *sym = NULL;
20543 const char *name;
20544 struct attribute *attr = NULL;
20545 struct attribute *attr2 = NULL;
20546 CORE_ADDR baseaddr;
20547 struct pending **list_to_add = NULL;
20548
20549 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20550
20551 baseaddr = objfile->text_section_offset ();
20552
20553 name = dwarf2_name (die, cu);
20554 if (name)
20555 {
20556 int suppress_add = 0;
20557
20558 if (space)
20559 sym = space;
20560 else
20561 sym = allocate_symbol (objfile);
20562 OBJSTAT (objfile, n_syms++);
20563
20564 /* Cache this symbol's name and the name's demangled form (if any). */
20565 sym->set_language (cu->language, &objfile->objfile_obstack);
20566 /* Fortran does not have mangling standard and the mangling does differ
20567 between gfortran, iFort etc. */
20568 const char *physname
20569 = (cu->language == language_fortran
20570 ? dwarf2_full_name (name, die, cu)
20571 : dwarf2_physname (name, die, cu));
20572 const char *linkagename = dw2_linkage_name (die, cu);
20573
20574 if (linkagename == nullptr || cu->language == language_ada)
20575 sym->set_linkage_name (physname);
20576 else
20577 {
20578 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20579 sym->set_linkage_name (linkagename);
20580 }
20581
20582 /* Default assumptions.
20583 Use the passed type or decode it from the die. */
20584 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20585 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20586 if (type != NULL)
20587 SYMBOL_TYPE (sym) = type;
20588 else
20589 SYMBOL_TYPE (sym) = die_type (die, cu);
20590 attr = dwarf2_attr (die,
20591 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20592 cu);
20593 if (attr != nullptr)
20594 {
20595 SYMBOL_LINE (sym) = DW_UNSND (attr);
20596 }
20597
20598 attr = dwarf2_attr (die,
20599 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20600 cu);
20601 if (attr != nullptr)
20602 {
20603 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20604 struct file_entry *fe;
20605
20606 if (cu->line_header != NULL)
20607 fe = cu->line_header->file_name_at (file_index);
20608 else
20609 fe = NULL;
20610
20611 if (fe == NULL)
20612 complaint (_("file index out of range"));
20613 else
20614 symbol_set_symtab (sym, fe->symtab);
20615 }
20616
20617 switch (die->tag)
20618 {
20619 case DW_TAG_label:
20620 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20621 if (attr != nullptr)
20622 {
20623 CORE_ADDR addr;
20624
20625 addr = attr->value_as_address ();
20626 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20627 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20628 }
20629 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20630 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20631 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20632 add_symbol_to_list (sym, cu->list_in_scope);
20633 break;
20634 case DW_TAG_subprogram:
20635 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20636 finish_block. */
20637 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20638 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20639 if ((attr2 && (DW_UNSND (attr2) != 0))
20640 || cu->language == language_ada
20641 || cu->language == language_fortran)
20642 {
20643 /* Subprograms marked external are stored as a global symbol.
20644 Ada and Fortran subprograms, whether marked external or
20645 not, are always stored as a global symbol, because we want
20646 to be able to access them globally. For instance, we want
20647 to be able to break on a nested subprogram without having
20648 to specify the context. */
20649 list_to_add = cu->get_builder ()->get_global_symbols ();
20650 }
20651 else
20652 {
20653 list_to_add = cu->list_in_scope;
20654 }
20655 break;
20656 case DW_TAG_inlined_subroutine:
20657 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20658 finish_block. */
20659 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20660 SYMBOL_INLINED (sym) = 1;
20661 list_to_add = cu->list_in_scope;
20662 break;
20663 case DW_TAG_template_value_param:
20664 suppress_add = 1;
20665 /* Fall through. */
20666 case DW_TAG_constant:
20667 case DW_TAG_variable:
20668 case DW_TAG_member:
20669 /* Compilation with minimal debug info may result in
20670 variables with missing type entries. Change the
20671 misleading `void' type to something sensible. */
20672 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20673 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20674
20675 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20676 /* In the case of DW_TAG_member, we should only be called for
20677 static const members. */
20678 if (die->tag == DW_TAG_member)
20679 {
20680 /* dwarf2_add_field uses die_is_declaration,
20681 so we do the same. */
20682 gdb_assert (die_is_declaration (die, cu));
20683 gdb_assert (attr);
20684 }
20685 if (attr != nullptr)
20686 {
20687 dwarf2_const_value (attr, sym, cu);
20688 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20689 if (!suppress_add)
20690 {
20691 if (attr2 && (DW_UNSND (attr2) != 0))
20692 list_to_add = cu->get_builder ()->get_global_symbols ();
20693 else
20694 list_to_add = cu->list_in_scope;
20695 }
20696 break;
20697 }
20698 attr = dwarf2_attr (die, DW_AT_location, cu);
20699 if (attr != nullptr)
20700 {
20701 var_decode_location (attr, sym, cu);
20702 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20703
20704 /* Fortran explicitly imports any global symbols to the local
20705 scope by DW_TAG_common_block. */
20706 if (cu->language == language_fortran && die->parent
20707 && die->parent->tag == DW_TAG_common_block)
20708 attr2 = NULL;
20709
20710 if (SYMBOL_CLASS (sym) == LOC_STATIC
20711 && SYMBOL_VALUE_ADDRESS (sym) == 0
20712 && !dwarf2_per_objfile->has_section_at_zero)
20713 {
20714 /* When a static variable is eliminated by the linker,
20715 the corresponding debug information is not stripped
20716 out, but the variable address is set to null;
20717 do not add such variables into symbol table. */
20718 }
20719 else if (attr2 && (DW_UNSND (attr2) != 0))
20720 {
20721 if (SYMBOL_CLASS (sym) == LOC_STATIC
20722 && (objfile->flags & OBJF_MAINLINE) == 0
20723 && dwarf2_per_objfile->can_copy)
20724 {
20725 /* A global static variable might be subject to
20726 copy relocation. We first check for a local
20727 minsym, though, because maybe the symbol was
20728 marked hidden, in which case this would not
20729 apply. */
20730 bound_minimal_symbol found
20731 = (lookup_minimal_symbol_linkage
20732 (sym->linkage_name (), objfile));
20733 if (found.minsym != nullptr)
20734 sym->maybe_copied = 1;
20735 }
20736
20737 /* A variable with DW_AT_external is never static,
20738 but it may be block-scoped. */
20739 list_to_add
20740 = ((cu->list_in_scope
20741 == cu->get_builder ()->get_file_symbols ())
20742 ? cu->get_builder ()->get_global_symbols ()
20743 : cu->list_in_scope);
20744 }
20745 else
20746 list_to_add = cu->list_in_scope;
20747 }
20748 else
20749 {
20750 /* We do not know the address of this symbol.
20751 If it is an external symbol and we have type information
20752 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20753 The address of the variable will then be determined from
20754 the minimal symbol table whenever the variable is
20755 referenced. */
20756 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20757
20758 /* Fortran explicitly imports any global symbols to the local
20759 scope by DW_TAG_common_block. */
20760 if (cu->language == language_fortran && die->parent
20761 && die->parent->tag == DW_TAG_common_block)
20762 {
20763 /* SYMBOL_CLASS doesn't matter here because
20764 read_common_block is going to reset it. */
20765 if (!suppress_add)
20766 list_to_add = cu->list_in_scope;
20767 }
20768 else if (attr2 && (DW_UNSND (attr2) != 0)
20769 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20770 {
20771 /* A variable with DW_AT_external is never static, but it
20772 may be block-scoped. */
20773 list_to_add
20774 = ((cu->list_in_scope
20775 == cu->get_builder ()->get_file_symbols ())
20776 ? cu->get_builder ()->get_global_symbols ()
20777 : cu->list_in_scope);
20778
20779 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20780 }
20781 else if (!die_is_declaration (die, cu))
20782 {
20783 /* Use the default LOC_OPTIMIZED_OUT class. */
20784 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20785 if (!suppress_add)
20786 list_to_add = cu->list_in_scope;
20787 }
20788 }
20789 break;
20790 case DW_TAG_formal_parameter:
20791 {
20792 /* If we are inside a function, mark this as an argument. If
20793 not, we might be looking at an argument to an inlined function
20794 when we do not have enough information to show inlined frames;
20795 pretend it's a local variable in that case so that the user can
20796 still see it. */
20797 struct context_stack *curr
20798 = cu->get_builder ()->get_current_context_stack ();
20799 if (curr != nullptr && curr->name != nullptr)
20800 SYMBOL_IS_ARGUMENT (sym) = 1;
20801 attr = dwarf2_attr (die, DW_AT_location, cu);
20802 if (attr != nullptr)
20803 {
20804 var_decode_location (attr, sym, cu);
20805 }
20806 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20807 if (attr != nullptr)
20808 {
20809 dwarf2_const_value (attr, sym, cu);
20810 }
20811
20812 list_to_add = cu->list_in_scope;
20813 }
20814 break;
20815 case DW_TAG_unspecified_parameters:
20816 /* From varargs functions; gdb doesn't seem to have any
20817 interest in this information, so just ignore it for now.
20818 (FIXME?) */
20819 break;
20820 case DW_TAG_template_type_param:
20821 suppress_add = 1;
20822 /* Fall through. */
20823 case DW_TAG_class_type:
20824 case DW_TAG_interface_type:
20825 case DW_TAG_structure_type:
20826 case DW_TAG_union_type:
20827 case DW_TAG_set_type:
20828 case DW_TAG_enumeration_type:
20829 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20830 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20831
20832 {
20833 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20834 really ever be static objects: otherwise, if you try
20835 to, say, break of a class's method and you're in a file
20836 which doesn't mention that class, it won't work unless
20837 the check for all static symbols in lookup_symbol_aux
20838 saves you. See the OtherFileClass tests in
20839 gdb.c++/namespace.exp. */
20840
20841 if (!suppress_add)
20842 {
20843 buildsym_compunit *builder = cu->get_builder ();
20844 list_to_add
20845 = (cu->list_in_scope == builder->get_file_symbols ()
20846 && cu->language == language_cplus
20847 ? builder->get_global_symbols ()
20848 : cu->list_in_scope);
20849
20850 /* The semantics of C++ state that "struct foo {
20851 ... }" also defines a typedef for "foo". */
20852 if (cu->language == language_cplus
20853 || cu->language == language_ada
20854 || cu->language == language_d
20855 || cu->language == language_rust)
20856 {
20857 /* The symbol's name is already allocated along
20858 with this objfile, so we don't need to
20859 duplicate it for the type. */
20860 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20861 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20862 }
20863 }
20864 }
20865 break;
20866 case DW_TAG_typedef:
20867 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20868 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20869 list_to_add = cu->list_in_scope;
20870 break;
20871 case DW_TAG_base_type:
20872 case DW_TAG_subrange_type:
20873 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20874 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20875 list_to_add = cu->list_in_scope;
20876 break;
20877 case DW_TAG_enumerator:
20878 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20879 if (attr != nullptr)
20880 {
20881 dwarf2_const_value (attr, sym, cu);
20882 }
20883 {
20884 /* NOTE: carlton/2003-11-10: See comment above in the
20885 DW_TAG_class_type, etc. block. */
20886
20887 list_to_add
20888 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20889 && cu->language == language_cplus
20890 ? cu->get_builder ()->get_global_symbols ()
20891 : cu->list_in_scope);
20892 }
20893 break;
20894 case DW_TAG_imported_declaration:
20895 case DW_TAG_namespace:
20896 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20897 list_to_add = cu->get_builder ()->get_global_symbols ();
20898 break;
20899 case DW_TAG_module:
20900 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20901 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20902 list_to_add = cu->get_builder ()->get_global_symbols ();
20903 break;
20904 case DW_TAG_common_block:
20905 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20906 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20907 add_symbol_to_list (sym, cu->list_in_scope);
20908 break;
20909 default:
20910 /* Not a tag we recognize. Hopefully we aren't processing
20911 trash data, but since we must specifically ignore things
20912 we don't recognize, there is nothing else we should do at
20913 this point. */
20914 complaint (_("unsupported tag: '%s'"),
20915 dwarf_tag_name (die->tag));
20916 break;
20917 }
20918
20919 if (suppress_add)
20920 {
20921 sym->hash_next = objfile->template_symbols;
20922 objfile->template_symbols = sym;
20923 list_to_add = NULL;
20924 }
20925
20926 if (list_to_add != NULL)
20927 add_symbol_to_list (sym, list_to_add);
20928
20929 /* For the benefit of old versions of GCC, check for anonymous
20930 namespaces based on the demangled name. */
20931 if (!cu->processing_has_namespace_info
20932 && cu->language == language_cplus)
20933 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20934 }
20935 return (sym);
20936 }
20937
20938 /* Given an attr with a DW_FORM_dataN value in host byte order,
20939 zero-extend it as appropriate for the symbol's type. The DWARF
20940 standard (v4) is not entirely clear about the meaning of using
20941 DW_FORM_dataN for a constant with a signed type, where the type is
20942 wider than the data. The conclusion of a discussion on the DWARF
20943 list was that this is unspecified. We choose to always zero-extend
20944 because that is the interpretation long in use by GCC. */
20945
20946 static gdb_byte *
20947 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20948 struct dwarf2_cu *cu, LONGEST *value, int bits)
20949 {
20950 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20951 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20952 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20953 LONGEST l = DW_UNSND (attr);
20954
20955 if (bits < sizeof (*value) * 8)
20956 {
20957 l &= ((LONGEST) 1 << bits) - 1;
20958 *value = l;
20959 }
20960 else if (bits == sizeof (*value) * 8)
20961 *value = l;
20962 else
20963 {
20964 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20965 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20966 return bytes;
20967 }
20968
20969 return NULL;
20970 }
20971
20972 /* Read a constant value from an attribute. Either set *VALUE, or if
20973 the value does not fit in *VALUE, set *BYTES - either already
20974 allocated on the objfile obstack, or newly allocated on OBSTACK,
20975 or, set *BATON, if we translated the constant to a location
20976 expression. */
20977
20978 static void
20979 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20980 const char *name, struct obstack *obstack,
20981 struct dwarf2_cu *cu,
20982 LONGEST *value, const gdb_byte **bytes,
20983 struct dwarf2_locexpr_baton **baton)
20984 {
20985 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20986 struct comp_unit_head *cu_header = &cu->header;
20987 struct dwarf_block *blk;
20988 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20989 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20990
20991 *value = 0;
20992 *bytes = NULL;
20993 *baton = NULL;
20994
20995 switch (attr->form)
20996 {
20997 case DW_FORM_addr:
20998 case DW_FORM_addrx:
20999 case DW_FORM_GNU_addr_index:
21000 {
21001 gdb_byte *data;
21002
21003 if (TYPE_LENGTH (type) != cu_header->addr_size)
21004 dwarf2_const_value_length_mismatch_complaint (name,
21005 cu_header->addr_size,
21006 TYPE_LENGTH (type));
21007 /* Symbols of this form are reasonably rare, so we just
21008 piggyback on the existing location code rather than writing
21009 a new implementation of symbol_computed_ops. */
21010 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21011 (*baton)->per_cu = cu->per_cu;
21012 gdb_assert ((*baton)->per_cu);
21013
21014 (*baton)->size = 2 + cu_header->addr_size;
21015 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21016 (*baton)->data = data;
21017
21018 data[0] = DW_OP_addr;
21019 store_unsigned_integer (&data[1], cu_header->addr_size,
21020 byte_order, DW_ADDR (attr));
21021 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21022 }
21023 break;
21024 case DW_FORM_string:
21025 case DW_FORM_strp:
21026 case DW_FORM_strx:
21027 case DW_FORM_GNU_str_index:
21028 case DW_FORM_GNU_strp_alt:
21029 /* DW_STRING is already allocated on the objfile obstack, point
21030 directly to it. */
21031 *bytes = (const gdb_byte *) DW_STRING (attr);
21032 break;
21033 case DW_FORM_block1:
21034 case DW_FORM_block2:
21035 case DW_FORM_block4:
21036 case DW_FORM_block:
21037 case DW_FORM_exprloc:
21038 case DW_FORM_data16:
21039 blk = DW_BLOCK (attr);
21040 if (TYPE_LENGTH (type) != blk->size)
21041 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21042 TYPE_LENGTH (type));
21043 *bytes = blk->data;
21044 break;
21045
21046 /* The DW_AT_const_value attributes are supposed to carry the
21047 symbol's value "represented as it would be on the target
21048 architecture." By the time we get here, it's already been
21049 converted to host endianness, so we just need to sign- or
21050 zero-extend it as appropriate. */
21051 case DW_FORM_data1:
21052 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21053 break;
21054 case DW_FORM_data2:
21055 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21056 break;
21057 case DW_FORM_data4:
21058 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21059 break;
21060 case DW_FORM_data8:
21061 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21062 break;
21063
21064 case DW_FORM_sdata:
21065 case DW_FORM_implicit_const:
21066 *value = DW_SND (attr);
21067 break;
21068
21069 case DW_FORM_udata:
21070 *value = DW_UNSND (attr);
21071 break;
21072
21073 default:
21074 complaint (_("unsupported const value attribute form: '%s'"),
21075 dwarf_form_name (attr->form));
21076 *value = 0;
21077 break;
21078 }
21079 }
21080
21081
21082 /* Copy constant value from an attribute to a symbol. */
21083
21084 static void
21085 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21086 struct dwarf2_cu *cu)
21087 {
21088 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21089 LONGEST value;
21090 const gdb_byte *bytes;
21091 struct dwarf2_locexpr_baton *baton;
21092
21093 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21094 sym->print_name (),
21095 &objfile->objfile_obstack, cu,
21096 &value, &bytes, &baton);
21097
21098 if (baton != NULL)
21099 {
21100 SYMBOL_LOCATION_BATON (sym) = baton;
21101 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21102 }
21103 else if (bytes != NULL)
21104 {
21105 SYMBOL_VALUE_BYTES (sym) = bytes;
21106 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21107 }
21108 else
21109 {
21110 SYMBOL_VALUE (sym) = value;
21111 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21112 }
21113 }
21114
21115 /* Return the type of the die in question using its DW_AT_type attribute. */
21116
21117 static struct type *
21118 die_type (struct die_info *die, struct dwarf2_cu *cu)
21119 {
21120 struct attribute *type_attr;
21121
21122 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21123 if (!type_attr)
21124 {
21125 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21126 /* A missing DW_AT_type represents a void type. */
21127 return objfile_type (objfile)->builtin_void;
21128 }
21129
21130 return lookup_die_type (die, type_attr, cu);
21131 }
21132
21133 /* True iff CU's producer generates GNAT Ada auxiliary information
21134 that allows to find parallel types through that information instead
21135 of having to do expensive parallel lookups by type name. */
21136
21137 static int
21138 need_gnat_info (struct dwarf2_cu *cu)
21139 {
21140 /* Assume that the Ada compiler was GNAT, which always produces
21141 the auxiliary information. */
21142 return (cu->language == language_ada);
21143 }
21144
21145 /* Return the auxiliary type of the die in question using its
21146 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21147 attribute is not present. */
21148
21149 static struct type *
21150 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21151 {
21152 struct attribute *type_attr;
21153
21154 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21155 if (!type_attr)
21156 return NULL;
21157
21158 return lookup_die_type (die, type_attr, cu);
21159 }
21160
21161 /* If DIE has a descriptive_type attribute, then set the TYPE's
21162 descriptive type accordingly. */
21163
21164 static void
21165 set_descriptive_type (struct type *type, struct die_info *die,
21166 struct dwarf2_cu *cu)
21167 {
21168 struct type *descriptive_type = die_descriptive_type (die, cu);
21169
21170 if (descriptive_type)
21171 {
21172 ALLOCATE_GNAT_AUX_TYPE (type);
21173 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21174 }
21175 }
21176
21177 /* Return the containing type of the die in question using its
21178 DW_AT_containing_type attribute. */
21179
21180 static struct type *
21181 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21182 {
21183 struct attribute *type_attr;
21184 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21185
21186 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21187 if (!type_attr)
21188 error (_("Dwarf Error: Problem turning containing type into gdb type "
21189 "[in module %s]"), objfile_name (objfile));
21190
21191 return lookup_die_type (die, type_attr, cu);
21192 }
21193
21194 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21195
21196 static struct type *
21197 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21198 {
21199 struct dwarf2_per_objfile *dwarf2_per_objfile
21200 = cu->per_cu->dwarf2_per_objfile;
21201 struct objfile *objfile = dwarf2_per_objfile->objfile;
21202 char *saved;
21203
21204 std::string message
21205 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21206 objfile_name (objfile),
21207 sect_offset_str (cu->header.sect_off),
21208 sect_offset_str (die->sect_off));
21209 saved = obstack_strdup (&objfile->objfile_obstack, message);
21210
21211 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21212 }
21213
21214 /* Look up the type of DIE in CU using its type attribute ATTR.
21215 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21216 DW_AT_containing_type.
21217 If there is no type substitute an error marker. */
21218
21219 static struct type *
21220 lookup_die_type (struct die_info *die, const struct attribute *attr,
21221 struct dwarf2_cu *cu)
21222 {
21223 struct dwarf2_per_objfile *dwarf2_per_objfile
21224 = cu->per_cu->dwarf2_per_objfile;
21225 struct objfile *objfile = dwarf2_per_objfile->objfile;
21226 struct type *this_type;
21227
21228 gdb_assert (attr->name == DW_AT_type
21229 || attr->name == DW_AT_GNAT_descriptive_type
21230 || attr->name == DW_AT_containing_type);
21231
21232 /* First see if we have it cached. */
21233
21234 if (attr->form == DW_FORM_GNU_ref_alt)
21235 {
21236 struct dwarf2_per_cu_data *per_cu;
21237 sect_offset sect_off = attr->get_ref_die_offset ();
21238
21239 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21240 dwarf2_per_objfile);
21241 this_type = get_die_type_at_offset (sect_off, per_cu);
21242 }
21243 else if (attr->form_is_ref ())
21244 {
21245 sect_offset sect_off = attr->get_ref_die_offset ();
21246
21247 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21248 }
21249 else if (attr->form == DW_FORM_ref_sig8)
21250 {
21251 ULONGEST signature = DW_SIGNATURE (attr);
21252
21253 return get_signatured_type (die, signature, cu);
21254 }
21255 else
21256 {
21257 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21258 " at %s [in module %s]"),
21259 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21260 objfile_name (objfile));
21261 return build_error_marker_type (cu, die);
21262 }
21263
21264 /* If not cached we need to read it in. */
21265
21266 if (this_type == NULL)
21267 {
21268 struct die_info *type_die = NULL;
21269 struct dwarf2_cu *type_cu = cu;
21270
21271 if (attr->form_is_ref ())
21272 type_die = follow_die_ref (die, attr, &type_cu);
21273 if (type_die == NULL)
21274 return build_error_marker_type (cu, die);
21275 /* If we find the type now, it's probably because the type came
21276 from an inter-CU reference and the type's CU got expanded before
21277 ours. */
21278 this_type = read_type_die (type_die, type_cu);
21279 }
21280
21281 /* If we still don't have a type use an error marker. */
21282
21283 if (this_type == NULL)
21284 return build_error_marker_type (cu, die);
21285
21286 return this_type;
21287 }
21288
21289 /* Return the type in DIE, CU.
21290 Returns NULL for invalid types.
21291
21292 This first does a lookup in die_type_hash,
21293 and only reads the die in if necessary.
21294
21295 NOTE: This can be called when reading in partial or full symbols. */
21296
21297 static struct type *
21298 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21299 {
21300 struct type *this_type;
21301
21302 this_type = get_die_type (die, cu);
21303 if (this_type)
21304 return this_type;
21305
21306 return read_type_die_1 (die, cu);
21307 }
21308
21309 /* Read the type in DIE, CU.
21310 Returns NULL for invalid types. */
21311
21312 static struct type *
21313 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21314 {
21315 struct type *this_type = NULL;
21316
21317 switch (die->tag)
21318 {
21319 case DW_TAG_class_type:
21320 case DW_TAG_interface_type:
21321 case DW_TAG_structure_type:
21322 case DW_TAG_union_type:
21323 this_type = read_structure_type (die, cu);
21324 break;
21325 case DW_TAG_enumeration_type:
21326 this_type = read_enumeration_type (die, cu);
21327 break;
21328 case DW_TAG_subprogram:
21329 case DW_TAG_subroutine_type:
21330 case DW_TAG_inlined_subroutine:
21331 this_type = read_subroutine_type (die, cu);
21332 break;
21333 case DW_TAG_array_type:
21334 this_type = read_array_type (die, cu);
21335 break;
21336 case DW_TAG_set_type:
21337 this_type = read_set_type (die, cu);
21338 break;
21339 case DW_TAG_pointer_type:
21340 this_type = read_tag_pointer_type (die, cu);
21341 break;
21342 case DW_TAG_ptr_to_member_type:
21343 this_type = read_tag_ptr_to_member_type (die, cu);
21344 break;
21345 case DW_TAG_reference_type:
21346 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21347 break;
21348 case DW_TAG_rvalue_reference_type:
21349 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21350 break;
21351 case DW_TAG_const_type:
21352 this_type = read_tag_const_type (die, cu);
21353 break;
21354 case DW_TAG_volatile_type:
21355 this_type = read_tag_volatile_type (die, cu);
21356 break;
21357 case DW_TAG_restrict_type:
21358 this_type = read_tag_restrict_type (die, cu);
21359 break;
21360 case DW_TAG_string_type:
21361 this_type = read_tag_string_type (die, cu);
21362 break;
21363 case DW_TAG_typedef:
21364 this_type = read_typedef (die, cu);
21365 break;
21366 case DW_TAG_subrange_type:
21367 this_type = read_subrange_type (die, cu);
21368 break;
21369 case DW_TAG_base_type:
21370 this_type = read_base_type (die, cu);
21371 break;
21372 case DW_TAG_unspecified_type:
21373 this_type = read_unspecified_type (die, cu);
21374 break;
21375 case DW_TAG_namespace:
21376 this_type = read_namespace_type (die, cu);
21377 break;
21378 case DW_TAG_module:
21379 this_type = read_module_type (die, cu);
21380 break;
21381 case DW_TAG_atomic_type:
21382 this_type = read_tag_atomic_type (die, cu);
21383 break;
21384 default:
21385 complaint (_("unexpected tag in read_type_die: '%s'"),
21386 dwarf_tag_name (die->tag));
21387 break;
21388 }
21389
21390 return this_type;
21391 }
21392
21393 /* See if we can figure out if the class lives in a namespace. We do
21394 this by looking for a member function; its demangled name will
21395 contain namespace info, if there is any.
21396 Return the computed name or NULL.
21397 Space for the result is allocated on the objfile's obstack.
21398 This is the full-die version of guess_partial_die_structure_name.
21399 In this case we know DIE has no useful parent. */
21400
21401 static const char *
21402 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21403 {
21404 struct die_info *spec_die;
21405 struct dwarf2_cu *spec_cu;
21406 struct die_info *child;
21407 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21408
21409 spec_cu = cu;
21410 spec_die = die_specification (die, &spec_cu);
21411 if (spec_die != NULL)
21412 {
21413 die = spec_die;
21414 cu = spec_cu;
21415 }
21416
21417 for (child = die->child;
21418 child != NULL;
21419 child = child->sibling)
21420 {
21421 if (child->tag == DW_TAG_subprogram)
21422 {
21423 const char *linkage_name = dw2_linkage_name (child, cu);
21424
21425 if (linkage_name != NULL)
21426 {
21427 gdb::unique_xmalloc_ptr<char> actual_name
21428 (language_class_name_from_physname (cu->language_defn,
21429 linkage_name));
21430 const char *name = NULL;
21431
21432 if (actual_name != NULL)
21433 {
21434 const char *die_name = dwarf2_name (die, cu);
21435
21436 if (die_name != NULL
21437 && strcmp (die_name, actual_name.get ()) != 0)
21438 {
21439 /* Strip off the class name from the full name.
21440 We want the prefix. */
21441 int die_name_len = strlen (die_name);
21442 int actual_name_len = strlen (actual_name.get ());
21443 const char *ptr = actual_name.get ();
21444
21445 /* Test for '::' as a sanity check. */
21446 if (actual_name_len > die_name_len + 2
21447 && ptr[actual_name_len - die_name_len - 1] == ':')
21448 name = obstack_strndup (
21449 &objfile->per_bfd->storage_obstack,
21450 ptr, actual_name_len - die_name_len - 2);
21451 }
21452 }
21453 return name;
21454 }
21455 }
21456 }
21457
21458 return NULL;
21459 }
21460
21461 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21462 prefix part in such case. See
21463 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21464
21465 static const char *
21466 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21467 {
21468 struct attribute *attr;
21469 const char *base;
21470
21471 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21472 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21473 return NULL;
21474
21475 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21476 return NULL;
21477
21478 attr = dw2_linkage_name_attr (die, cu);
21479 if (attr == NULL || DW_STRING (attr) == NULL)
21480 return NULL;
21481
21482 /* dwarf2_name had to be already called. */
21483 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21484
21485 /* Strip the base name, keep any leading namespaces/classes. */
21486 base = strrchr (DW_STRING (attr), ':');
21487 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21488 return "";
21489
21490 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21491 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21492 DW_STRING (attr),
21493 &base[-1] - DW_STRING (attr));
21494 }
21495
21496 /* Return the name of the namespace/class that DIE is defined within,
21497 or "" if we can't tell. The caller should not xfree the result.
21498
21499 For example, if we're within the method foo() in the following
21500 code:
21501
21502 namespace N {
21503 class C {
21504 void foo () {
21505 }
21506 };
21507 }
21508
21509 then determine_prefix on foo's die will return "N::C". */
21510
21511 static const char *
21512 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21513 {
21514 struct dwarf2_per_objfile *dwarf2_per_objfile
21515 = cu->per_cu->dwarf2_per_objfile;
21516 struct die_info *parent, *spec_die;
21517 struct dwarf2_cu *spec_cu;
21518 struct type *parent_type;
21519 const char *retval;
21520
21521 if (cu->language != language_cplus
21522 && cu->language != language_fortran && cu->language != language_d
21523 && cu->language != language_rust)
21524 return "";
21525
21526 retval = anonymous_struct_prefix (die, cu);
21527 if (retval)
21528 return retval;
21529
21530 /* We have to be careful in the presence of DW_AT_specification.
21531 For example, with GCC 3.4, given the code
21532
21533 namespace N {
21534 void foo() {
21535 // Definition of N::foo.
21536 }
21537 }
21538
21539 then we'll have a tree of DIEs like this:
21540
21541 1: DW_TAG_compile_unit
21542 2: DW_TAG_namespace // N
21543 3: DW_TAG_subprogram // declaration of N::foo
21544 4: DW_TAG_subprogram // definition of N::foo
21545 DW_AT_specification // refers to die #3
21546
21547 Thus, when processing die #4, we have to pretend that we're in
21548 the context of its DW_AT_specification, namely the contex of die
21549 #3. */
21550 spec_cu = cu;
21551 spec_die = die_specification (die, &spec_cu);
21552 if (spec_die == NULL)
21553 parent = die->parent;
21554 else
21555 {
21556 parent = spec_die->parent;
21557 cu = spec_cu;
21558 }
21559
21560 if (parent == NULL)
21561 return "";
21562 else if (parent->building_fullname)
21563 {
21564 const char *name;
21565 const char *parent_name;
21566
21567 /* It has been seen on RealView 2.2 built binaries,
21568 DW_TAG_template_type_param types actually _defined_ as
21569 children of the parent class:
21570
21571 enum E {};
21572 template class <class Enum> Class{};
21573 Class<enum E> class_e;
21574
21575 1: DW_TAG_class_type (Class)
21576 2: DW_TAG_enumeration_type (E)
21577 3: DW_TAG_enumerator (enum1:0)
21578 3: DW_TAG_enumerator (enum2:1)
21579 ...
21580 2: DW_TAG_template_type_param
21581 DW_AT_type DW_FORM_ref_udata (E)
21582
21583 Besides being broken debug info, it can put GDB into an
21584 infinite loop. Consider:
21585
21586 When we're building the full name for Class<E>, we'll start
21587 at Class, and go look over its template type parameters,
21588 finding E. We'll then try to build the full name of E, and
21589 reach here. We're now trying to build the full name of E,
21590 and look over the parent DIE for containing scope. In the
21591 broken case, if we followed the parent DIE of E, we'd again
21592 find Class, and once again go look at its template type
21593 arguments, etc., etc. Simply don't consider such parent die
21594 as source-level parent of this die (it can't be, the language
21595 doesn't allow it), and break the loop here. */
21596 name = dwarf2_name (die, cu);
21597 parent_name = dwarf2_name (parent, cu);
21598 complaint (_("template param type '%s' defined within parent '%s'"),
21599 name ? name : "<unknown>",
21600 parent_name ? parent_name : "<unknown>");
21601 return "";
21602 }
21603 else
21604 switch (parent->tag)
21605 {
21606 case DW_TAG_namespace:
21607 parent_type = read_type_die (parent, cu);
21608 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21609 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21610 Work around this problem here. */
21611 if (cu->language == language_cplus
21612 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21613 return "";
21614 /* We give a name to even anonymous namespaces. */
21615 return TYPE_NAME (parent_type);
21616 case DW_TAG_class_type:
21617 case DW_TAG_interface_type:
21618 case DW_TAG_structure_type:
21619 case DW_TAG_union_type:
21620 case DW_TAG_module:
21621 parent_type = read_type_die (parent, cu);
21622 if (TYPE_NAME (parent_type) != NULL)
21623 return TYPE_NAME (parent_type);
21624 else
21625 /* An anonymous structure is only allowed non-static data
21626 members; no typedefs, no member functions, et cetera.
21627 So it does not need a prefix. */
21628 return "";
21629 case DW_TAG_compile_unit:
21630 case DW_TAG_partial_unit:
21631 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21632 if (cu->language == language_cplus
21633 && !dwarf2_per_objfile->types.empty ()
21634 && die->child != NULL
21635 && (die->tag == DW_TAG_class_type
21636 || die->tag == DW_TAG_structure_type
21637 || die->tag == DW_TAG_union_type))
21638 {
21639 const char *name = guess_full_die_structure_name (die, cu);
21640 if (name != NULL)
21641 return name;
21642 }
21643 return "";
21644 case DW_TAG_subprogram:
21645 /* Nested subroutines in Fortran get a prefix with the name
21646 of the parent's subroutine. */
21647 if (cu->language == language_fortran)
21648 {
21649 if ((die->tag == DW_TAG_subprogram)
21650 && (dwarf2_name (parent, cu) != NULL))
21651 return dwarf2_name (parent, cu);
21652 }
21653 return determine_prefix (parent, cu);
21654 case DW_TAG_enumeration_type:
21655 parent_type = read_type_die (parent, cu);
21656 if (TYPE_DECLARED_CLASS (parent_type))
21657 {
21658 if (TYPE_NAME (parent_type) != NULL)
21659 return TYPE_NAME (parent_type);
21660 return "";
21661 }
21662 /* Fall through. */
21663 default:
21664 return determine_prefix (parent, cu);
21665 }
21666 }
21667
21668 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21669 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21670 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21671 an obconcat, otherwise allocate storage for the result. The CU argument is
21672 used to determine the language and hence, the appropriate separator. */
21673
21674 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21675
21676 static char *
21677 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21678 int physname, struct dwarf2_cu *cu)
21679 {
21680 const char *lead = "";
21681 const char *sep;
21682
21683 if (suffix == NULL || suffix[0] == '\0'
21684 || prefix == NULL || prefix[0] == '\0')
21685 sep = "";
21686 else if (cu->language == language_d)
21687 {
21688 /* For D, the 'main' function could be defined in any module, but it
21689 should never be prefixed. */
21690 if (strcmp (suffix, "D main") == 0)
21691 {
21692 prefix = "";
21693 sep = "";
21694 }
21695 else
21696 sep = ".";
21697 }
21698 else if (cu->language == language_fortran && physname)
21699 {
21700 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21701 DW_AT_MIPS_linkage_name is preferred and used instead. */
21702
21703 lead = "__";
21704 sep = "_MOD_";
21705 }
21706 else
21707 sep = "::";
21708
21709 if (prefix == NULL)
21710 prefix = "";
21711 if (suffix == NULL)
21712 suffix = "";
21713
21714 if (obs == NULL)
21715 {
21716 char *retval
21717 = ((char *)
21718 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21719
21720 strcpy (retval, lead);
21721 strcat (retval, prefix);
21722 strcat (retval, sep);
21723 strcat (retval, suffix);
21724 return retval;
21725 }
21726 else
21727 {
21728 /* We have an obstack. */
21729 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21730 }
21731 }
21732
21733 /* Get name of a die, return NULL if not found. */
21734
21735 static const char *
21736 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21737 struct objfile *objfile)
21738 {
21739 if (name && cu->language == language_cplus)
21740 {
21741 std::string canon_name = cp_canonicalize_string (name);
21742
21743 if (!canon_name.empty ())
21744 {
21745 if (canon_name != name)
21746 name = objfile->intern (canon_name);
21747 }
21748 }
21749
21750 return name;
21751 }
21752
21753 /* Get name of a die, return NULL if not found.
21754 Anonymous namespaces are converted to their magic string. */
21755
21756 static const char *
21757 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21758 {
21759 struct attribute *attr;
21760 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21761
21762 attr = dwarf2_attr (die, DW_AT_name, cu);
21763 if ((!attr || !DW_STRING (attr))
21764 && die->tag != DW_TAG_namespace
21765 && die->tag != DW_TAG_class_type
21766 && die->tag != DW_TAG_interface_type
21767 && die->tag != DW_TAG_structure_type
21768 && die->tag != DW_TAG_union_type)
21769 return NULL;
21770
21771 switch (die->tag)
21772 {
21773 case DW_TAG_compile_unit:
21774 case DW_TAG_partial_unit:
21775 /* Compilation units have a DW_AT_name that is a filename, not
21776 a source language identifier. */
21777 case DW_TAG_enumeration_type:
21778 case DW_TAG_enumerator:
21779 /* These tags always have simple identifiers already; no need
21780 to canonicalize them. */
21781 return DW_STRING (attr);
21782
21783 case DW_TAG_namespace:
21784 if (attr != NULL && DW_STRING (attr) != NULL)
21785 return DW_STRING (attr);
21786 return CP_ANONYMOUS_NAMESPACE_STR;
21787
21788 case DW_TAG_class_type:
21789 case DW_TAG_interface_type:
21790 case DW_TAG_structure_type:
21791 case DW_TAG_union_type:
21792 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21793 structures or unions. These were of the form "._%d" in GCC 4.1,
21794 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21795 and GCC 4.4. We work around this problem by ignoring these. */
21796 if (attr && DW_STRING (attr)
21797 && (startswith (DW_STRING (attr), "._")
21798 || startswith (DW_STRING (attr), "<anonymous")))
21799 return NULL;
21800
21801 /* GCC might emit a nameless typedef that has a linkage name. See
21802 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21803 if (!attr || DW_STRING (attr) == NULL)
21804 {
21805 attr = dw2_linkage_name_attr (die, cu);
21806 if (attr == NULL || DW_STRING (attr) == NULL)
21807 return NULL;
21808
21809 /* Avoid demangling DW_STRING (attr) the second time on a second
21810 call for the same DIE. */
21811 if (!DW_STRING_IS_CANONICAL (attr))
21812 {
21813 gdb::unique_xmalloc_ptr<char> demangled
21814 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21815 if (demangled == nullptr)
21816 return nullptr;
21817
21818 DW_STRING (attr) = objfile->intern (demangled.get ());
21819 DW_STRING_IS_CANONICAL (attr) = 1;
21820 }
21821
21822 /* Strip any leading namespaces/classes, keep only the base name.
21823 DW_AT_name for named DIEs does not contain the prefixes. */
21824 const char *base = strrchr (DW_STRING (attr), ':');
21825 if (base && base > DW_STRING (attr) && base[-1] == ':')
21826 return &base[1];
21827 else
21828 return DW_STRING (attr);
21829 }
21830 break;
21831
21832 default:
21833 break;
21834 }
21835
21836 if (!DW_STRING_IS_CANONICAL (attr))
21837 {
21838 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21839 objfile);
21840 DW_STRING_IS_CANONICAL (attr) = 1;
21841 }
21842 return DW_STRING (attr);
21843 }
21844
21845 /* Return the die that this die in an extension of, or NULL if there
21846 is none. *EXT_CU is the CU containing DIE on input, and the CU
21847 containing the return value on output. */
21848
21849 static struct die_info *
21850 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21851 {
21852 struct attribute *attr;
21853
21854 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21855 if (attr == NULL)
21856 return NULL;
21857
21858 return follow_die_ref (die, attr, ext_cu);
21859 }
21860
21861 static void
21862 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21863 {
21864 unsigned int i;
21865
21866 print_spaces (indent, f);
21867 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21868 dwarf_tag_name (die->tag), die->abbrev,
21869 sect_offset_str (die->sect_off));
21870
21871 if (die->parent != NULL)
21872 {
21873 print_spaces (indent, f);
21874 fprintf_unfiltered (f, " parent at offset: %s\n",
21875 sect_offset_str (die->parent->sect_off));
21876 }
21877
21878 print_spaces (indent, f);
21879 fprintf_unfiltered (f, " has children: %s\n",
21880 dwarf_bool_name (die->child != NULL));
21881
21882 print_spaces (indent, f);
21883 fprintf_unfiltered (f, " attributes:\n");
21884
21885 for (i = 0; i < die->num_attrs; ++i)
21886 {
21887 print_spaces (indent, f);
21888 fprintf_unfiltered (f, " %s (%s) ",
21889 dwarf_attr_name (die->attrs[i].name),
21890 dwarf_form_name (die->attrs[i].form));
21891
21892 switch (die->attrs[i].form)
21893 {
21894 case DW_FORM_addr:
21895 case DW_FORM_addrx:
21896 case DW_FORM_GNU_addr_index:
21897 fprintf_unfiltered (f, "address: ");
21898 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21899 break;
21900 case DW_FORM_block2:
21901 case DW_FORM_block4:
21902 case DW_FORM_block:
21903 case DW_FORM_block1:
21904 fprintf_unfiltered (f, "block: size %s",
21905 pulongest (DW_BLOCK (&die->attrs[i])->size));
21906 break;
21907 case DW_FORM_exprloc:
21908 fprintf_unfiltered (f, "expression: size %s",
21909 pulongest (DW_BLOCK (&die->attrs[i])->size));
21910 break;
21911 case DW_FORM_data16:
21912 fprintf_unfiltered (f, "constant of 16 bytes");
21913 break;
21914 case DW_FORM_ref_addr:
21915 fprintf_unfiltered (f, "ref address: ");
21916 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21917 break;
21918 case DW_FORM_GNU_ref_alt:
21919 fprintf_unfiltered (f, "alt ref address: ");
21920 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21921 break;
21922 case DW_FORM_ref1:
21923 case DW_FORM_ref2:
21924 case DW_FORM_ref4:
21925 case DW_FORM_ref8:
21926 case DW_FORM_ref_udata:
21927 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21928 (long) (DW_UNSND (&die->attrs[i])));
21929 break;
21930 case DW_FORM_data1:
21931 case DW_FORM_data2:
21932 case DW_FORM_data4:
21933 case DW_FORM_data8:
21934 case DW_FORM_udata:
21935 case DW_FORM_sdata:
21936 fprintf_unfiltered (f, "constant: %s",
21937 pulongest (DW_UNSND (&die->attrs[i])));
21938 break;
21939 case DW_FORM_sec_offset:
21940 fprintf_unfiltered (f, "section offset: %s",
21941 pulongest (DW_UNSND (&die->attrs[i])));
21942 break;
21943 case DW_FORM_ref_sig8:
21944 fprintf_unfiltered (f, "signature: %s",
21945 hex_string (DW_SIGNATURE (&die->attrs[i])));
21946 break;
21947 case DW_FORM_string:
21948 case DW_FORM_strp:
21949 case DW_FORM_line_strp:
21950 case DW_FORM_strx:
21951 case DW_FORM_GNU_str_index:
21952 case DW_FORM_GNU_strp_alt:
21953 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21954 DW_STRING (&die->attrs[i])
21955 ? DW_STRING (&die->attrs[i]) : "",
21956 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21957 break;
21958 case DW_FORM_flag:
21959 if (DW_UNSND (&die->attrs[i]))
21960 fprintf_unfiltered (f, "flag: TRUE");
21961 else
21962 fprintf_unfiltered (f, "flag: FALSE");
21963 break;
21964 case DW_FORM_flag_present:
21965 fprintf_unfiltered (f, "flag: TRUE");
21966 break;
21967 case DW_FORM_indirect:
21968 /* The reader will have reduced the indirect form to
21969 the "base form" so this form should not occur. */
21970 fprintf_unfiltered (f,
21971 "unexpected attribute form: DW_FORM_indirect");
21972 break;
21973 case DW_FORM_implicit_const:
21974 fprintf_unfiltered (f, "constant: %s",
21975 plongest (DW_SND (&die->attrs[i])));
21976 break;
21977 default:
21978 fprintf_unfiltered (f, "unsupported attribute form: %d.",
21979 die->attrs[i].form);
21980 break;
21981 }
21982 fprintf_unfiltered (f, "\n");
21983 }
21984 }
21985
21986 static void
21987 dump_die_for_error (struct die_info *die)
21988 {
21989 dump_die_shallow (gdb_stderr, 0, die);
21990 }
21991
21992 static void
21993 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21994 {
21995 int indent = level * 4;
21996
21997 gdb_assert (die != NULL);
21998
21999 if (level >= max_level)
22000 return;
22001
22002 dump_die_shallow (f, indent, die);
22003
22004 if (die->child != NULL)
22005 {
22006 print_spaces (indent, f);
22007 fprintf_unfiltered (f, " Children:");
22008 if (level + 1 < max_level)
22009 {
22010 fprintf_unfiltered (f, "\n");
22011 dump_die_1 (f, level + 1, max_level, die->child);
22012 }
22013 else
22014 {
22015 fprintf_unfiltered (f,
22016 " [not printed, max nesting level reached]\n");
22017 }
22018 }
22019
22020 if (die->sibling != NULL && level > 0)
22021 {
22022 dump_die_1 (f, level, max_level, die->sibling);
22023 }
22024 }
22025
22026 /* This is called from the pdie macro in gdbinit.in.
22027 It's not static so gcc will keep a copy callable from gdb. */
22028
22029 void
22030 dump_die (struct die_info *die, int max_level)
22031 {
22032 dump_die_1 (gdb_stdlog, 0, max_level, die);
22033 }
22034
22035 static void
22036 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22037 {
22038 void **slot;
22039
22040 slot = htab_find_slot_with_hash (cu->die_hash, die,
22041 to_underlying (die->sect_off),
22042 INSERT);
22043
22044 *slot = die;
22045 }
22046
22047 /* Follow reference or signature attribute ATTR of SRC_DIE.
22048 On entry *REF_CU is the CU of SRC_DIE.
22049 On exit *REF_CU is the CU of the result. */
22050
22051 static struct die_info *
22052 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22053 struct dwarf2_cu **ref_cu)
22054 {
22055 struct die_info *die;
22056
22057 if (attr->form_is_ref ())
22058 die = follow_die_ref (src_die, attr, ref_cu);
22059 else if (attr->form == DW_FORM_ref_sig8)
22060 die = follow_die_sig (src_die, attr, ref_cu);
22061 else
22062 {
22063 dump_die_for_error (src_die);
22064 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22065 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22066 }
22067
22068 return die;
22069 }
22070
22071 /* Follow reference OFFSET.
22072 On entry *REF_CU is the CU of the source die referencing OFFSET.
22073 On exit *REF_CU is the CU of the result.
22074 Returns NULL if OFFSET is invalid. */
22075
22076 static struct die_info *
22077 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22078 struct dwarf2_cu **ref_cu)
22079 {
22080 struct die_info temp_die;
22081 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22082 struct dwarf2_per_objfile *dwarf2_per_objfile
22083 = cu->per_cu->dwarf2_per_objfile;
22084
22085 gdb_assert (cu->per_cu != NULL);
22086
22087 target_cu = cu;
22088
22089 if (cu->per_cu->is_debug_types)
22090 {
22091 /* .debug_types CUs cannot reference anything outside their CU.
22092 If they need to, they have to reference a signatured type via
22093 DW_FORM_ref_sig8. */
22094 if (!cu->header.offset_in_cu_p (sect_off))
22095 return NULL;
22096 }
22097 else if (offset_in_dwz != cu->per_cu->is_dwz
22098 || !cu->header.offset_in_cu_p (sect_off))
22099 {
22100 struct dwarf2_per_cu_data *per_cu;
22101
22102 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22103 dwarf2_per_objfile);
22104
22105 /* If necessary, add it to the queue and load its DIEs. */
22106 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22107 load_full_comp_unit (per_cu, false, cu->language);
22108
22109 target_cu = per_cu->cu;
22110 }
22111 else if (cu->dies == NULL)
22112 {
22113 /* We're loading full DIEs during partial symbol reading. */
22114 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22115 load_full_comp_unit (cu->per_cu, false, language_minimal);
22116 }
22117
22118 *ref_cu = target_cu;
22119 temp_die.sect_off = sect_off;
22120
22121 if (target_cu != cu)
22122 target_cu->ancestor = cu;
22123
22124 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22125 &temp_die,
22126 to_underlying (sect_off));
22127 }
22128
22129 /* Follow reference attribute ATTR of SRC_DIE.
22130 On entry *REF_CU is the CU of SRC_DIE.
22131 On exit *REF_CU is the CU of the result. */
22132
22133 static struct die_info *
22134 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22135 struct dwarf2_cu **ref_cu)
22136 {
22137 sect_offset sect_off = attr->get_ref_die_offset ();
22138 struct dwarf2_cu *cu = *ref_cu;
22139 struct die_info *die;
22140
22141 die = follow_die_offset (sect_off,
22142 (attr->form == DW_FORM_GNU_ref_alt
22143 || cu->per_cu->is_dwz),
22144 ref_cu);
22145 if (!die)
22146 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22147 "at %s [in module %s]"),
22148 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22149 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22150
22151 return die;
22152 }
22153
22154 /* See read.h. */
22155
22156 struct dwarf2_locexpr_baton
22157 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22158 dwarf2_per_cu_data *per_cu,
22159 CORE_ADDR (*get_frame_pc) (void *baton),
22160 void *baton, bool resolve_abstract_p)
22161 {
22162 struct dwarf2_cu *cu;
22163 struct die_info *die;
22164 struct attribute *attr;
22165 struct dwarf2_locexpr_baton retval;
22166 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22167 struct objfile *objfile = dwarf2_per_objfile->objfile;
22168
22169 if (per_cu->cu == NULL)
22170 load_cu (per_cu, false);
22171 cu = per_cu->cu;
22172 if (cu == NULL)
22173 {
22174 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22175 Instead just throw an error, not much else we can do. */
22176 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22177 sect_offset_str (sect_off), objfile_name (objfile));
22178 }
22179
22180 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22181 if (!die)
22182 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22183 sect_offset_str (sect_off), objfile_name (objfile));
22184
22185 attr = dwarf2_attr (die, DW_AT_location, cu);
22186 if (!attr && resolve_abstract_p
22187 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
22188 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22189 {
22190 CORE_ADDR pc = (*get_frame_pc) (baton);
22191 CORE_ADDR baseaddr = objfile->text_section_offset ();
22192 struct gdbarch *gdbarch = objfile->arch ();
22193
22194 for (const auto &cand_off
22195 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
22196 {
22197 struct dwarf2_cu *cand_cu = cu;
22198 struct die_info *cand
22199 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22200 if (!cand
22201 || !cand->parent
22202 || cand->parent->tag != DW_TAG_subprogram)
22203 continue;
22204
22205 CORE_ADDR pc_low, pc_high;
22206 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22207 if (pc_low == ((CORE_ADDR) -1))
22208 continue;
22209 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22210 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22211 if (!(pc_low <= pc && pc < pc_high))
22212 continue;
22213
22214 die = cand;
22215 attr = dwarf2_attr (die, DW_AT_location, cu);
22216 break;
22217 }
22218 }
22219
22220 if (!attr)
22221 {
22222 /* DWARF: "If there is no such attribute, then there is no effect.".
22223 DATA is ignored if SIZE is 0. */
22224
22225 retval.data = NULL;
22226 retval.size = 0;
22227 }
22228 else if (attr->form_is_section_offset ())
22229 {
22230 struct dwarf2_loclist_baton loclist_baton;
22231 CORE_ADDR pc = (*get_frame_pc) (baton);
22232 size_t size;
22233
22234 fill_in_loclist_baton (cu, &loclist_baton, attr);
22235
22236 retval.data = dwarf2_find_location_expression (&loclist_baton,
22237 &size, pc);
22238 retval.size = size;
22239 }
22240 else
22241 {
22242 if (!attr->form_is_block ())
22243 error (_("Dwarf Error: DIE at %s referenced in module %s "
22244 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22245 sect_offset_str (sect_off), objfile_name (objfile));
22246
22247 retval.data = DW_BLOCK (attr)->data;
22248 retval.size = DW_BLOCK (attr)->size;
22249 }
22250 retval.per_cu = cu->per_cu;
22251
22252 age_cached_comp_units (dwarf2_per_objfile);
22253
22254 return retval;
22255 }
22256
22257 /* See read.h. */
22258
22259 struct dwarf2_locexpr_baton
22260 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22261 dwarf2_per_cu_data *per_cu,
22262 CORE_ADDR (*get_frame_pc) (void *baton),
22263 void *baton)
22264 {
22265 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22266
22267 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22268 }
22269
22270 /* Write a constant of a given type as target-ordered bytes into
22271 OBSTACK. */
22272
22273 static const gdb_byte *
22274 write_constant_as_bytes (struct obstack *obstack,
22275 enum bfd_endian byte_order,
22276 struct type *type,
22277 ULONGEST value,
22278 LONGEST *len)
22279 {
22280 gdb_byte *result;
22281
22282 *len = TYPE_LENGTH (type);
22283 result = (gdb_byte *) obstack_alloc (obstack, *len);
22284 store_unsigned_integer (result, *len, byte_order, value);
22285
22286 return result;
22287 }
22288
22289 /* See read.h. */
22290
22291 const gdb_byte *
22292 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22293 dwarf2_per_cu_data *per_cu,
22294 obstack *obstack,
22295 LONGEST *len)
22296 {
22297 struct dwarf2_cu *cu;
22298 struct die_info *die;
22299 struct attribute *attr;
22300 const gdb_byte *result = NULL;
22301 struct type *type;
22302 LONGEST value;
22303 enum bfd_endian byte_order;
22304 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22305
22306 if (per_cu->cu == NULL)
22307 load_cu (per_cu, false);
22308 cu = per_cu->cu;
22309 if (cu == NULL)
22310 {
22311 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22312 Instead just throw an error, not much else we can do. */
22313 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22314 sect_offset_str (sect_off), objfile_name (objfile));
22315 }
22316
22317 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22318 if (!die)
22319 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22320 sect_offset_str (sect_off), objfile_name (objfile));
22321
22322 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22323 if (attr == NULL)
22324 return NULL;
22325
22326 byte_order = (bfd_big_endian (objfile->obfd)
22327 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22328
22329 switch (attr->form)
22330 {
22331 case DW_FORM_addr:
22332 case DW_FORM_addrx:
22333 case DW_FORM_GNU_addr_index:
22334 {
22335 gdb_byte *tem;
22336
22337 *len = cu->header.addr_size;
22338 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22339 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22340 result = tem;
22341 }
22342 break;
22343 case DW_FORM_string:
22344 case DW_FORM_strp:
22345 case DW_FORM_strx:
22346 case DW_FORM_GNU_str_index:
22347 case DW_FORM_GNU_strp_alt:
22348 /* DW_STRING is already allocated on the objfile obstack, point
22349 directly to it. */
22350 result = (const gdb_byte *) DW_STRING (attr);
22351 *len = strlen (DW_STRING (attr));
22352 break;
22353 case DW_FORM_block1:
22354 case DW_FORM_block2:
22355 case DW_FORM_block4:
22356 case DW_FORM_block:
22357 case DW_FORM_exprloc:
22358 case DW_FORM_data16:
22359 result = DW_BLOCK (attr)->data;
22360 *len = DW_BLOCK (attr)->size;
22361 break;
22362
22363 /* The DW_AT_const_value attributes are supposed to carry the
22364 symbol's value "represented as it would be on the target
22365 architecture." By the time we get here, it's already been
22366 converted to host endianness, so we just need to sign- or
22367 zero-extend it as appropriate. */
22368 case DW_FORM_data1:
22369 type = die_type (die, cu);
22370 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22371 if (result == NULL)
22372 result = write_constant_as_bytes (obstack, byte_order,
22373 type, value, len);
22374 break;
22375 case DW_FORM_data2:
22376 type = die_type (die, cu);
22377 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22378 if (result == NULL)
22379 result = write_constant_as_bytes (obstack, byte_order,
22380 type, value, len);
22381 break;
22382 case DW_FORM_data4:
22383 type = die_type (die, cu);
22384 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22385 if (result == NULL)
22386 result = write_constant_as_bytes (obstack, byte_order,
22387 type, value, len);
22388 break;
22389 case DW_FORM_data8:
22390 type = die_type (die, cu);
22391 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22392 if (result == NULL)
22393 result = write_constant_as_bytes (obstack, byte_order,
22394 type, value, len);
22395 break;
22396
22397 case DW_FORM_sdata:
22398 case DW_FORM_implicit_const:
22399 type = die_type (die, cu);
22400 result = write_constant_as_bytes (obstack, byte_order,
22401 type, DW_SND (attr), len);
22402 break;
22403
22404 case DW_FORM_udata:
22405 type = die_type (die, cu);
22406 result = write_constant_as_bytes (obstack, byte_order,
22407 type, DW_UNSND (attr), len);
22408 break;
22409
22410 default:
22411 complaint (_("unsupported const value attribute form: '%s'"),
22412 dwarf_form_name (attr->form));
22413 break;
22414 }
22415
22416 return result;
22417 }
22418
22419 /* See read.h. */
22420
22421 struct type *
22422 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22423 dwarf2_per_cu_data *per_cu)
22424 {
22425 struct dwarf2_cu *cu;
22426 struct die_info *die;
22427
22428 if (per_cu->cu == NULL)
22429 load_cu (per_cu, false);
22430 cu = per_cu->cu;
22431 if (!cu)
22432 return NULL;
22433
22434 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22435 if (!die)
22436 return NULL;
22437
22438 return die_type (die, cu);
22439 }
22440
22441 /* See read.h. */
22442
22443 struct type *
22444 dwarf2_get_die_type (cu_offset die_offset,
22445 struct dwarf2_per_cu_data *per_cu)
22446 {
22447 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22448 return get_die_type_at_offset (die_offset_sect, per_cu);
22449 }
22450
22451 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22452 On entry *REF_CU is the CU of SRC_DIE.
22453 On exit *REF_CU is the CU of the result.
22454 Returns NULL if the referenced DIE isn't found. */
22455
22456 static struct die_info *
22457 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22458 struct dwarf2_cu **ref_cu)
22459 {
22460 struct die_info temp_die;
22461 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22462 struct die_info *die;
22463
22464 /* While it might be nice to assert sig_type->type == NULL here,
22465 we can get here for DW_AT_imported_declaration where we need
22466 the DIE not the type. */
22467
22468 /* If necessary, add it to the queue and load its DIEs. */
22469
22470 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22471 read_signatured_type (sig_type);
22472
22473 sig_cu = sig_type->per_cu.cu;
22474 gdb_assert (sig_cu != NULL);
22475 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22476 temp_die.sect_off = sig_type->type_offset_in_section;
22477 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22478 to_underlying (temp_die.sect_off));
22479 if (die)
22480 {
22481 struct dwarf2_per_objfile *dwarf2_per_objfile
22482 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22483
22484 /* For .gdb_index version 7 keep track of included TUs.
22485 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22486 if (dwarf2_per_objfile->index_table != NULL
22487 && dwarf2_per_objfile->index_table->version <= 7)
22488 {
22489 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22490 }
22491
22492 *ref_cu = sig_cu;
22493 if (sig_cu != cu)
22494 sig_cu->ancestor = cu;
22495
22496 return die;
22497 }
22498
22499 return NULL;
22500 }
22501
22502 /* Follow signatured type referenced by ATTR in SRC_DIE.
22503 On entry *REF_CU is the CU of SRC_DIE.
22504 On exit *REF_CU is the CU of the result.
22505 The result is the DIE of the type.
22506 If the referenced type cannot be found an error is thrown. */
22507
22508 static struct die_info *
22509 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22510 struct dwarf2_cu **ref_cu)
22511 {
22512 ULONGEST signature = DW_SIGNATURE (attr);
22513 struct signatured_type *sig_type;
22514 struct die_info *die;
22515
22516 gdb_assert (attr->form == DW_FORM_ref_sig8);
22517
22518 sig_type = lookup_signatured_type (*ref_cu, signature);
22519 /* sig_type will be NULL if the signatured type is missing from
22520 the debug info. */
22521 if (sig_type == NULL)
22522 {
22523 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22524 " from DIE at %s [in module %s]"),
22525 hex_string (signature), sect_offset_str (src_die->sect_off),
22526 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22527 }
22528
22529 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22530 if (die == NULL)
22531 {
22532 dump_die_for_error (src_die);
22533 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22534 " from DIE at %s [in module %s]"),
22535 hex_string (signature), sect_offset_str (src_die->sect_off),
22536 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22537 }
22538
22539 return die;
22540 }
22541
22542 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22543 reading in and processing the type unit if necessary. */
22544
22545 static struct type *
22546 get_signatured_type (struct die_info *die, ULONGEST signature,
22547 struct dwarf2_cu *cu)
22548 {
22549 struct dwarf2_per_objfile *dwarf2_per_objfile
22550 = cu->per_cu->dwarf2_per_objfile;
22551 struct signatured_type *sig_type;
22552 struct dwarf2_cu *type_cu;
22553 struct die_info *type_die;
22554 struct type *type;
22555
22556 sig_type = lookup_signatured_type (cu, signature);
22557 /* sig_type will be NULL if the signatured type is missing from
22558 the debug info. */
22559 if (sig_type == NULL)
22560 {
22561 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22562 " from DIE at %s [in module %s]"),
22563 hex_string (signature), sect_offset_str (die->sect_off),
22564 objfile_name (dwarf2_per_objfile->objfile));
22565 return build_error_marker_type (cu, die);
22566 }
22567
22568 /* If we already know the type we're done. */
22569 if (sig_type->type != NULL)
22570 return sig_type->type;
22571
22572 type_cu = cu;
22573 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22574 if (type_die != NULL)
22575 {
22576 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22577 is created. This is important, for example, because for c++ classes
22578 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22579 type = read_type_die (type_die, type_cu);
22580 if (type == NULL)
22581 {
22582 complaint (_("Dwarf Error: Cannot build signatured type %s"
22583 " referenced from DIE at %s [in module %s]"),
22584 hex_string (signature), sect_offset_str (die->sect_off),
22585 objfile_name (dwarf2_per_objfile->objfile));
22586 type = build_error_marker_type (cu, die);
22587 }
22588 }
22589 else
22590 {
22591 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22592 " from DIE at %s [in module %s]"),
22593 hex_string (signature), sect_offset_str (die->sect_off),
22594 objfile_name (dwarf2_per_objfile->objfile));
22595 type = build_error_marker_type (cu, die);
22596 }
22597 sig_type->type = type;
22598
22599 return type;
22600 }
22601
22602 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22603 reading in and processing the type unit if necessary. */
22604
22605 static struct type *
22606 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22607 struct dwarf2_cu *cu) /* ARI: editCase function */
22608 {
22609 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22610 if (attr->form_is_ref ())
22611 {
22612 struct dwarf2_cu *type_cu = cu;
22613 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22614
22615 return read_type_die (type_die, type_cu);
22616 }
22617 else if (attr->form == DW_FORM_ref_sig8)
22618 {
22619 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22620 }
22621 else
22622 {
22623 struct dwarf2_per_objfile *dwarf2_per_objfile
22624 = cu->per_cu->dwarf2_per_objfile;
22625
22626 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22627 " at %s [in module %s]"),
22628 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22629 objfile_name (dwarf2_per_objfile->objfile));
22630 return build_error_marker_type (cu, die);
22631 }
22632 }
22633
22634 /* Load the DIEs associated with type unit PER_CU into memory. */
22635
22636 static void
22637 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22638 {
22639 struct signatured_type *sig_type;
22640
22641 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22642 gdb_assert (! per_cu->type_unit_group_p ());
22643
22644 /* We have the per_cu, but we need the signatured_type.
22645 Fortunately this is an easy translation. */
22646 gdb_assert (per_cu->is_debug_types);
22647 sig_type = (struct signatured_type *) per_cu;
22648
22649 gdb_assert (per_cu->cu == NULL);
22650
22651 read_signatured_type (sig_type);
22652
22653 gdb_assert (per_cu->cu != NULL);
22654 }
22655
22656 /* Read in a signatured type and build its CU and DIEs.
22657 If the type is a stub for the real type in a DWO file,
22658 read in the real type from the DWO file as well. */
22659
22660 static void
22661 read_signatured_type (struct signatured_type *sig_type)
22662 {
22663 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22664
22665 gdb_assert (per_cu->is_debug_types);
22666 gdb_assert (per_cu->cu == NULL);
22667
22668 cutu_reader reader (per_cu, NULL, 0, false);
22669
22670 if (!reader.dummy_p)
22671 {
22672 struct dwarf2_cu *cu = reader.cu;
22673 const gdb_byte *info_ptr = reader.info_ptr;
22674
22675 gdb_assert (cu->die_hash == NULL);
22676 cu->die_hash =
22677 htab_create_alloc_ex (cu->header.length / 12,
22678 die_hash,
22679 die_eq,
22680 NULL,
22681 &cu->comp_unit_obstack,
22682 hashtab_obstack_allocate,
22683 dummy_obstack_deallocate);
22684
22685 if (reader.comp_unit_die->has_children)
22686 reader.comp_unit_die->child
22687 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22688 reader.comp_unit_die);
22689 cu->dies = reader.comp_unit_die;
22690 /* comp_unit_die is not stored in die_hash, no need. */
22691
22692 /* We try not to read any attributes in this function, because
22693 not all CUs needed for references have been loaded yet, and
22694 symbol table processing isn't initialized. But we have to
22695 set the CU language, or we won't be able to build types
22696 correctly. Similarly, if we do not read the producer, we can
22697 not apply producer-specific interpretation. */
22698 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22699
22700 reader.keep ();
22701 }
22702
22703 sig_type->per_cu.tu_read = 1;
22704 }
22705
22706 /* Decode simple location descriptions.
22707 Given a pointer to a dwarf block that defines a location, compute
22708 the location and return the value. If COMPUTED is non-null, it is
22709 set to true to indicate that decoding was successful, and false
22710 otherwise. If COMPUTED is null, then this function may emit a
22711 complaint. */
22712
22713 static CORE_ADDR
22714 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22715 {
22716 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22717 size_t i;
22718 size_t size = blk->size;
22719 const gdb_byte *data = blk->data;
22720 CORE_ADDR stack[64];
22721 int stacki;
22722 unsigned int bytes_read, unsnd;
22723 gdb_byte op;
22724
22725 if (computed != nullptr)
22726 *computed = false;
22727
22728 i = 0;
22729 stacki = 0;
22730 stack[stacki] = 0;
22731 stack[++stacki] = 0;
22732
22733 while (i < size)
22734 {
22735 op = data[i++];
22736 switch (op)
22737 {
22738 case DW_OP_lit0:
22739 case DW_OP_lit1:
22740 case DW_OP_lit2:
22741 case DW_OP_lit3:
22742 case DW_OP_lit4:
22743 case DW_OP_lit5:
22744 case DW_OP_lit6:
22745 case DW_OP_lit7:
22746 case DW_OP_lit8:
22747 case DW_OP_lit9:
22748 case DW_OP_lit10:
22749 case DW_OP_lit11:
22750 case DW_OP_lit12:
22751 case DW_OP_lit13:
22752 case DW_OP_lit14:
22753 case DW_OP_lit15:
22754 case DW_OP_lit16:
22755 case DW_OP_lit17:
22756 case DW_OP_lit18:
22757 case DW_OP_lit19:
22758 case DW_OP_lit20:
22759 case DW_OP_lit21:
22760 case DW_OP_lit22:
22761 case DW_OP_lit23:
22762 case DW_OP_lit24:
22763 case DW_OP_lit25:
22764 case DW_OP_lit26:
22765 case DW_OP_lit27:
22766 case DW_OP_lit28:
22767 case DW_OP_lit29:
22768 case DW_OP_lit30:
22769 case DW_OP_lit31:
22770 stack[++stacki] = op - DW_OP_lit0;
22771 break;
22772
22773 case DW_OP_reg0:
22774 case DW_OP_reg1:
22775 case DW_OP_reg2:
22776 case DW_OP_reg3:
22777 case DW_OP_reg4:
22778 case DW_OP_reg5:
22779 case DW_OP_reg6:
22780 case DW_OP_reg7:
22781 case DW_OP_reg8:
22782 case DW_OP_reg9:
22783 case DW_OP_reg10:
22784 case DW_OP_reg11:
22785 case DW_OP_reg12:
22786 case DW_OP_reg13:
22787 case DW_OP_reg14:
22788 case DW_OP_reg15:
22789 case DW_OP_reg16:
22790 case DW_OP_reg17:
22791 case DW_OP_reg18:
22792 case DW_OP_reg19:
22793 case DW_OP_reg20:
22794 case DW_OP_reg21:
22795 case DW_OP_reg22:
22796 case DW_OP_reg23:
22797 case DW_OP_reg24:
22798 case DW_OP_reg25:
22799 case DW_OP_reg26:
22800 case DW_OP_reg27:
22801 case DW_OP_reg28:
22802 case DW_OP_reg29:
22803 case DW_OP_reg30:
22804 case DW_OP_reg31:
22805 stack[++stacki] = op - DW_OP_reg0;
22806 if (i < size)
22807 {
22808 if (computed == nullptr)
22809 dwarf2_complex_location_expr_complaint ();
22810 else
22811 return 0;
22812 }
22813 break;
22814
22815 case DW_OP_regx:
22816 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22817 i += bytes_read;
22818 stack[++stacki] = unsnd;
22819 if (i < size)
22820 {
22821 if (computed == nullptr)
22822 dwarf2_complex_location_expr_complaint ();
22823 else
22824 return 0;
22825 }
22826 break;
22827
22828 case DW_OP_addr:
22829 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22830 &bytes_read);
22831 i += bytes_read;
22832 break;
22833
22834 case DW_OP_const1u:
22835 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22836 i += 1;
22837 break;
22838
22839 case DW_OP_const1s:
22840 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22841 i += 1;
22842 break;
22843
22844 case DW_OP_const2u:
22845 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22846 i += 2;
22847 break;
22848
22849 case DW_OP_const2s:
22850 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22851 i += 2;
22852 break;
22853
22854 case DW_OP_const4u:
22855 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22856 i += 4;
22857 break;
22858
22859 case DW_OP_const4s:
22860 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22861 i += 4;
22862 break;
22863
22864 case DW_OP_const8u:
22865 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22866 i += 8;
22867 break;
22868
22869 case DW_OP_constu:
22870 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22871 &bytes_read);
22872 i += bytes_read;
22873 break;
22874
22875 case DW_OP_consts:
22876 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22877 i += bytes_read;
22878 break;
22879
22880 case DW_OP_dup:
22881 stack[stacki + 1] = stack[stacki];
22882 stacki++;
22883 break;
22884
22885 case DW_OP_plus:
22886 stack[stacki - 1] += stack[stacki];
22887 stacki--;
22888 break;
22889
22890 case DW_OP_plus_uconst:
22891 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22892 &bytes_read);
22893 i += bytes_read;
22894 break;
22895
22896 case DW_OP_minus:
22897 stack[stacki - 1] -= stack[stacki];
22898 stacki--;
22899 break;
22900
22901 case DW_OP_deref:
22902 /* If we're not the last op, then we definitely can't encode
22903 this using GDB's address_class enum. This is valid for partial
22904 global symbols, although the variable's address will be bogus
22905 in the psymtab. */
22906 if (i < size)
22907 {
22908 if (computed == nullptr)
22909 dwarf2_complex_location_expr_complaint ();
22910 else
22911 return 0;
22912 }
22913 break;
22914
22915 case DW_OP_GNU_push_tls_address:
22916 case DW_OP_form_tls_address:
22917 /* The top of the stack has the offset from the beginning
22918 of the thread control block at which the variable is located. */
22919 /* Nothing should follow this operator, so the top of stack would
22920 be returned. */
22921 /* This is valid for partial global symbols, but the variable's
22922 address will be bogus in the psymtab. Make it always at least
22923 non-zero to not look as a variable garbage collected by linker
22924 which have DW_OP_addr 0. */
22925 if (i < size)
22926 {
22927 if (computed == nullptr)
22928 dwarf2_complex_location_expr_complaint ();
22929 else
22930 return 0;
22931 }
22932 stack[stacki]++;
22933 break;
22934
22935 case DW_OP_GNU_uninit:
22936 if (computed != nullptr)
22937 return 0;
22938 break;
22939
22940 case DW_OP_addrx:
22941 case DW_OP_GNU_addr_index:
22942 case DW_OP_GNU_const_index:
22943 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22944 &bytes_read);
22945 i += bytes_read;
22946 break;
22947
22948 default:
22949 if (computed == nullptr)
22950 {
22951 const char *name = get_DW_OP_name (op);
22952
22953 if (name)
22954 complaint (_("unsupported stack op: '%s'"),
22955 name);
22956 else
22957 complaint (_("unsupported stack op: '%02x'"),
22958 op);
22959 }
22960
22961 return (stack[stacki]);
22962 }
22963
22964 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22965 outside of the allocated space. Also enforce minimum>0. */
22966 if (stacki >= ARRAY_SIZE (stack) - 1)
22967 {
22968 if (computed == nullptr)
22969 complaint (_("location description stack overflow"));
22970 return 0;
22971 }
22972
22973 if (stacki <= 0)
22974 {
22975 if (computed == nullptr)
22976 complaint (_("location description stack underflow"));
22977 return 0;
22978 }
22979 }
22980
22981 if (computed != nullptr)
22982 *computed = true;
22983 return (stack[stacki]);
22984 }
22985
22986 /* memory allocation interface */
22987
22988 static struct dwarf_block *
22989 dwarf_alloc_block (struct dwarf2_cu *cu)
22990 {
22991 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
22992 }
22993
22994 static struct die_info *
22995 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
22996 {
22997 struct die_info *die;
22998 size_t size = sizeof (struct die_info);
22999
23000 if (num_attrs > 1)
23001 size += (num_attrs - 1) * sizeof (struct attribute);
23002
23003 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23004 memset (die, 0, sizeof (struct die_info));
23005 return (die);
23006 }
23007
23008 \f
23009
23010 /* Macro support. */
23011
23012 /* An overload of dwarf_decode_macros that finds the correct section
23013 and ensures it is read in before calling the other overload. */
23014
23015 static void
23016 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23017 int section_is_gnu)
23018 {
23019 struct dwarf2_per_objfile *dwarf2_per_objfile
23020 = cu->per_cu->dwarf2_per_objfile;
23021 struct objfile *objfile = dwarf2_per_objfile->objfile;
23022 const struct line_header *lh = cu->line_header;
23023 unsigned int offset_size = cu->header.offset_size;
23024 struct dwarf2_section_info *section;
23025 const char *section_name;
23026
23027 if (cu->dwo_unit != nullptr)
23028 {
23029 if (section_is_gnu)
23030 {
23031 section = &cu->dwo_unit->dwo_file->sections.macro;
23032 section_name = ".debug_macro.dwo";
23033 }
23034 else
23035 {
23036 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23037 section_name = ".debug_macinfo.dwo";
23038 }
23039 }
23040 else
23041 {
23042 if (section_is_gnu)
23043 {
23044 section = &dwarf2_per_objfile->macro;
23045 section_name = ".debug_macro";
23046 }
23047 else
23048 {
23049 section = &dwarf2_per_objfile->macinfo;
23050 section_name = ".debug_macinfo";
23051 }
23052 }
23053
23054 section->read (objfile);
23055 if (section->buffer == nullptr)
23056 {
23057 complaint (_("missing %s section"), section_name);
23058 return;
23059 }
23060
23061 buildsym_compunit *builder = cu->get_builder ();
23062
23063 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23064 offset_size, offset, section_is_gnu);
23065 }
23066
23067 /* Return the .debug_loc section to use for CU.
23068 For DWO files use .debug_loc.dwo. */
23069
23070 static struct dwarf2_section_info *
23071 cu_debug_loc_section (struct dwarf2_cu *cu)
23072 {
23073 struct dwarf2_per_objfile *dwarf2_per_objfile
23074 = cu->per_cu->dwarf2_per_objfile;
23075
23076 if (cu->dwo_unit)
23077 {
23078 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23079
23080 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23081 }
23082 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23083 : &dwarf2_per_objfile->loc);
23084 }
23085
23086 /* A helper function that fills in a dwarf2_loclist_baton. */
23087
23088 static void
23089 fill_in_loclist_baton (struct dwarf2_cu *cu,
23090 struct dwarf2_loclist_baton *baton,
23091 const struct attribute *attr)
23092 {
23093 struct dwarf2_per_objfile *dwarf2_per_objfile
23094 = cu->per_cu->dwarf2_per_objfile;
23095 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23096
23097 section->read (dwarf2_per_objfile->objfile);
23098
23099 baton->per_cu = cu->per_cu;
23100 gdb_assert (baton->per_cu);
23101 /* We don't know how long the location list is, but make sure we
23102 don't run off the edge of the section. */
23103 baton->size = section->size - DW_UNSND (attr);
23104 baton->data = section->buffer + DW_UNSND (attr);
23105 if (cu->base_address.has_value ())
23106 baton->base_address = *cu->base_address;
23107 else
23108 baton->base_address = 0;
23109 baton->from_dwo = cu->dwo_unit != NULL;
23110 }
23111
23112 static void
23113 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23114 struct dwarf2_cu *cu, int is_block)
23115 {
23116 struct dwarf2_per_objfile *dwarf2_per_objfile
23117 = cu->per_cu->dwarf2_per_objfile;
23118 struct objfile *objfile = dwarf2_per_objfile->objfile;
23119 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23120
23121 if (attr->form_is_section_offset ()
23122 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23123 the section. If so, fall through to the complaint in the
23124 other branch. */
23125 && DW_UNSND (attr) < section->get_size (objfile))
23126 {
23127 struct dwarf2_loclist_baton *baton;
23128
23129 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23130
23131 fill_in_loclist_baton (cu, baton, attr);
23132
23133 if (!cu->base_address.has_value ())
23134 complaint (_("Location list used without "
23135 "specifying the CU base address."));
23136
23137 SYMBOL_ACLASS_INDEX (sym) = (is_block
23138 ? dwarf2_loclist_block_index
23139 : dwarf2_loclist_index);
23140 SYMBOL_LOCATION_BATON (sym) = baton;
23141 }
23142 else
23143 {
23144 struct dwarf2_locexpr_baton *baton;
23145
23146 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23147 baton->per_cu = cu->per_cu;
23148 gdb_assert (baton->per_cu);
23149
23150 if (attr->form_is_block ())
23151 {
23152 /* Note that we're just copying the block's data pointer
23153 here, not the actual data. We're still pointing into the
23154 info_buffer for SYM's objfile; right now we never release
23155 that buffer, but when we do clean up properly this may
23156 need to change. */
23157 baton->size = DW_BLOCK (attr)->size;
23158 baton->data = DW_BLOCK (attr)->data;
23159 }
23160 else
23161 {
23162 dwarf2_invalid_attrib_class_complaint ("location description",
23163 sym->natural_name ());
23164 baton->size = 0;
23165 }
23166
23167 SYMBOL_ACLASS_INDEX (sym) = (is_block
23168 ? dwarf2_locexpr_block_index
23169 : dwarf2_locexpr_index);
23170 SYMBOL_LOCATION_BATON (sym) = baton;
23171 }
23172 }
23173
23174 /* See read.h. */
23175
23176 struct objfile *
23177 dwarf2_per_cu_data::objfile () const
23178 {
23179 struct objfile *objfile = dwarf2_per_objfile->objfile;
23180
23181 /* Return the master objfile, so that we can report and look up the
23182 correct file containing this variable. */
23183 if (objfile->separate_debug_objfile_backlink)
23184 objfile = objfile->separate_debug_objfile_backlink;
23185
23186 return objfile;
23187 }
23188
23189 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23190 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23191 CU_HEADERP first. */
23192
23193 static const struct comp_unit_head *
23194 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23195 const struct dwarf2_per_cu_data *per_cu)
23196 {
23197 const gdb_byte *info_ptr;
23198
23199 if (per_cu->cu)
23200 return &per_cu->cu->header;
23201
23202 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
23203
23204 memset (cu_headerp, 0, sizeof (*cu_headerp));
23205 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23206 rcuh_kind::COMPILE);
23207
23208 return cu_headerp;
23209 }
23210
23211 /* See read.h. */
23212
23213 int
23214 dwarf2_per_cu_data::addr_size () const
23215 {
23216 struct comp_unit_head cu_header_local;
23217 const struct comp_unit_head *cu_headerp;
23218
23219 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23220
23221 return cu_headerp->addr_size;
23222 }
23223
23224 /* See read.h. */
23225
23226 int
23227 dwarf2_per_cu_data::offset_size () const
23228 {
23229 struct comp_unit_head cu_header_local;
23230 const struct comp_unit_head *cu_headerp;
23231
23232 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23233
23234 return cu_headerp->offset_size;
23235 }
23236
23237 /* See read.h. */
23238
23239 int
23240 dwarf2_per_cu_data::ref_addr_size () const
23241 {
23242 struct comp_unit_head cu_header_local;
23243 const struct comp_unit_head *cu_headerp;
23244
23245 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23246
23247 if (cu_headerp->version == 2)
23248 return cu_headerp->addr_size;
23249 else
23250 return cu_headerp->offset_size;
23251 }
23252
23253 /* See read.h. */
23254
23255 CORE_ADDR
23256 dwarf2_per_cu_data::text_offset () const
23257 {
23258 struct objfile *objfile = dwarf2_per_objfile->objfile;
23259
23260 return objfile->text_section_offset ();
23261 }
23262
23263 /* See read.h. */
23264
23265 struct type *
23266 dwarf2_per_cu_data::addr_type () const
23267 {
23268 struct objfile *objfile = dwarf2_per_objfile->objfile;
23269 struct type *void_type = objfile_type (objfile)->builtin_void;
23270 struct type *addr_type = lookup_pointer_type (void_type);
23271 int addr_size = this->addr_size ();
23272
23273 if (TYPE_LENGTH (addr_type) == addr_size)
23274 return addr_type;
23275
23276 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23277 return addr_type;
23278 }
23279
23280 /* A helper function for dwarf2_find_containing_comp_unit that returns
23281 the index of the result, and that searches a vector. It will
23282 return a result even if the offset in question does not actually
23283 occur in any CU. This is separate so that it can be unit
23284 tested. */
23285
23286 static int
23287 dwarf2_find_containing_comp_unit
23288 (sect_offset sect_off,
23289 unsigned int offset_in_dwz,
23290 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23291 {
23292 int low, high;
23293
23294 low = 0;
23295 high = all_comp_units.size () - 1;
23296 while (high > low)
23297 {
23298 struct dwarf2_per_cu_data *mid_cu;
23299 int mid = low + (high - low) / 2;
23300
23301 mid_cu = all_comp_units[mid];
23302 if (mid_cu->is_dwz > offset_in_dwz
23303 || (mid_cu->is_dwz == offset_in_dwz
23304 && mid_cu->sect_off + mid_cu->length > sect_off))
23305 high = mid;
23306 else
23307 low = mid + 1;
23308 }
23309 gdb_assert (low == high);
23310 return low;
23311 }
23312
23313 /* Locate the .debug_info compilation unit from CU's objfile which contains
23314 the DIE at OFFSET. Raises an error on failure. */
23315
23316 static struct dwarf2_per_cu_data *
23317 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23318 unsigned int offset_in_dwz,
23319 struct dwarf2_per_objfile *dwarf2_per_objfile)
23320 {
23321 int low
23322 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23323 dwarf2_per_objfile->all_comp_units);
23324 struct dwarf2_per_cu_data *this_cu
23325 = dwarf2_per_objfile->all_comp_units[low];
23326
23327 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23328 {
23329 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23330 error (_("Dwarf Error: could not find partial DIE containing "
23331 "offset %s [in module %s]"),
23332 sect_offset_str (sect_off),
23333 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23334
23335 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
23336 <= sect_off);
23337 return dwarf2_per_objfile->all_comp_units[low-1];
23338 }
23339 else
23340 {
23341 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
23342 && sect_off >= this_cu->sect_off + this_cu->length)
23343 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23344 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23345 return this_cu;
23346 }
23347 }
23348
23349 #if GDB_SELF_TEST
23350
23351 namespace selftests {
23352 namespace find_containing_comp_unit {
23353
23354 static void
23355 run_test ()
23356 {
23357 struct dwarf2_per_cu_data one {};
23358 struct dwarf2_per_cu_data two {};
23359 struct dwarf2_per_cu_data three {};
23360 struct dwarf2_per_cu_data four {};
23361
23362 one.length = 5;
23363 two.sect_off = sect_offset (one.length);
23364 two.length = 7;
23365
23366 three.length = 5;
23367 three.is_dwz = 1;
23368 four.sect_off = sect_offset (three.length);
23369 four.length = 7;
23370 four.is_dwz = 1;
23371
23372 std::vector<dwarf2_per_cu_data *> units;
23373 units.push_back (&one);
23374 units.push_back (&two);
23375 units.push_back (&three);
23376 units.push_back (&four);
23377
23378 int result;
23379
23380 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23381 SELF_CHECK (units[result] == &one);
23382 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23383 SELF_CHECK (units[result] == &one);
23384 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23385 SELF_CHECK (units[result] == &two);
23386
23387 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23388 SELF_CHECK (units[result] == &three);
23389 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23390 SELF_CHECK (units[result] == &three);
23391 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23392 SELF_CHECK (units[result] == &four);
23393 }
23394
23395 }
23396 }
23397
23398 #endif /* GDB_SELF_TEST */
23399
23400 /* Initialize dwarf2_cu CU, owned by PER_CU. */
23401
23402 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
23403 : per_cu (per_cu_),
23404 mark (false),
23405 has_loclist (false),
23406 checked_producer (false),
23407 producer_is_gxx_lt_4_6 (false),
23408 producer_is_gcc_lt_4_3 (false),
23409 producer_is_icc (false),
23410 producer_is_icc_lt_14 (false),
23411 producer_is_codewarrior (false),
23412 processing_has_namespace_info (false)
23413 {
23414 per_cu->cu = this;
23415 }
23416
23417 /* Destroy a dwarf2_cu. */
23418
23419 dwarf2_cu::~dwarf2_cu ()
23420 {
23421 per_cu->cu = NULL;
23422 }
23423
23424 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23425
23426 static void
23427 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23428 enum language pretend_language)
23429 {
23430 struct attribute *attr;
23431
23432 /* Set the language we're debugging. */
23433 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23434 if (attr != nullptr)
23435 set_cu_language (DW_UNSND (attr), cu);
23436 else
23437 {
23438 cu->language = pretend_language;
23439 cu->language_defn = language_def (cu->language);
23440 }
23441
23442 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23443 }
23444
23445 /* Increase the age counter on each cached compilation unit, and free
23446 any that are too old. */
23447
23448 static void
23449 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23450 {
23451 struct dwarf2_per_cu_data *per_cu, **last_chain;
23452
23453 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23454 per_cu = dwarf2_per_objfile->read_in_chain;
23455 while (per_cu != NULL)
23456 {
23457 per_cu->cu->last_used ++;
23458 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23459 dwarf2_mark (per_cu->cu);
23460 per_cu = per_cu->cu->read_in_chain;
23461 }
23462
23463 per_cu = dwarf2_per_objfile->read_in_chain;
23464 last_chain = &dwarf2_per_objfile->read_in_chain;
23465 while (per_cu != NULL)
23466 {
23467 struct dwarf2_per_cu_data *next_cu;
23468
23469 next_cu = per_cu->cu->read_in_chain;
23470
23471 if (!per_cu->cu->mark)
23472 {
23473 delete per_cu->cu;
23474 *last_chain = next_cu;
23475 }
23476 else
23477 last_chain = &per_cu->cu->read_in_chain;
23478
23479 per_cu = next_cu;
23480 }
23481 }
23482
23483 /* Remove a single compilation unit from the cache. */
23484
23485 static void
23486 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23487 {
23488 struct dwarf2_per_cu_data *per_cu, **last_chain;
23489 struct dwarf2_per_objfile *dwarf2_per_objfile
23490 = target_per_cu->dwarf2_per_objfile;
23491
23492 per_cu = dwarf2_per_objfile->read_in_chain;
23493 last_chain = &dwarf2_per_objfile->read_in_chain;
23494 while (per_cu != NULL)
23495 {
23496 struct dwarf2_per_cu_data *next_cu;
23497
23498 next_cu = per_cu->cu->read_in_chain;
23499
23500 if (per_cu == target_per_cu)
23501 {
23502 delete per_cu->cu;
23503 per_cu->cu = NULL;
23504 *last_chain = next_cu;
23505 break;
23506 }
23507 else
23508 last_chain = &per_cu->cu->read_in_chain;
23509
23510 per_cu = next_cu;
23511 }
23512 }
23513
23514 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23515 We store these in a hash table separate from the DIEs, and preserve them
23516 when the DIEs are flushed out of cache.
23517
23518 The CU "per_cu" pointer is needed because offset alone is not enough to
23519 uniquely identify the type. A file may have multiple .debug_types sections,
23520 or the type may come from a DWO file. Furthermore, while it's more logical
23521 to use per_cu->section+offset, with Fission the section with the data is in
23522 the DWO file but we don't know that section at the point we need it.
23523 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23524 because we can enter the lookup routine, get_die_type_at_offset, from
23525 outside this file, and thus won't necessarily have PER_CU->cu.
23526 Fortunately, PER_CU is stable for the life of the objfile. */
23527
23528 struct dwarf2_per_cu_offset_and_type
23529 {
23530 const struct dwarf2_per_cu_data *per_cu;
23531 sect_offset sect_off;
23532 struct type *type;
23533 };
23534
23535 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23536
23537 static hashval_t
23538 per_cu_offset_and_type_hash (const void *item)
23539 {
23540 const struct dwarf2_per_cu_offset_and_type *ofs
23541 = (const struct dwarf2_per_cu_offset_and_type *) item;
23542
23543 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23544 }
23545
23546 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23547
23548 static int
23549 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23550 {
23551 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23552 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23553 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23554 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23555
23556 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23557 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23558 }
23559
23560 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23561 table if necessary. For convenience, return TYPE.
23562
23563 The DIEs reading must have careful ordering to:
23564 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23565 reading current DIE.
23566 * Not trying to dereference contents of still incompletely read in types
23567 while reading in other DIEs.
23568 * Enable referencing still incompletely read in types just by a pointer to
23569 the type without accessing its fields.
23570
23571 Therefore caller should follow these rules:
23572 * Try to fetch any prerequisite types we may need to build this DIE type
23573 before building the type and calling set_die_type.
23574 * After building type call set_die_type for current DIE as soon as
23575 possible before fetching more types to complete the current type.
23576 * Make the type as complete as possible before fetching more types. */
23577
23578 static struct type *
23579 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23580 {
23581 struct dwarf2_per_objfile *dwarf2_per_objfile
23582 = cu->per_cu->dwarf2_per_objfile;
23583 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23584 struct objfile *objfile = dwarf2_per_objfile->objfile;
23585 struct attribute *attr;
23586 struct dynamic_prop prop;
23587
23588 /* For Ada types, make sure that the gnat-specific data is always
23589 initialized (if not already set). There are a few types where
23590 we should not be doing so, because the type-specific area is
23591 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23592 where the type-specific area is used to store the floatformat).
23593 But this is not a problem, because the gnat-specific information
23594 is actually not needed for these types. */
23595 if (need_gnat_info (cu)
23596 && TYPE_CODE (type) != TYPE_CODE_FUNC
23597 && TYPE_CODE (type) != TYPE_CODE_FLT
23598 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23599 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23600 && TYPE_CODE (type) != TYPE_CODE_METHOD
23601 && !HAVE_GNAT_AUX_INFO (type))
23602 INIT_GNAT_SPECIFIC (type);
23603
23604 /* Read DW_AT_allocated and set in type. */
23605 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23606 if (attr != NULL && attr->form_is_block ())
23607 {
23608 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23609 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23610 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
23611 }
23612 else if (attr != NULL)
23613 {
23614 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23615 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23616 sect_offset_str (die->sect_off));
23617 }
23618
23619 /* Read DW_AT_associated and set in type. */
23620 attr = dwarf2_attr (die, DW_AT_associated, cu);
23621 if (attr != NULL && attr->form_is_block ())
23622 {
23623 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23624 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23625 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
23626 }
23627 else if (attr != NULL)
23628 {
23629 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23630 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23631 sect_offset_str (die->sect_off));
23632 }
23633
23634 /* Read DW_AT_data_location and set in type. */
23635 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23636 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23637 cu->per_cu->addr_type ()))
23638 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
23639
23640 if (dwarf2_per_objfile->die_type_hash == NULL)
23641 dwarf2_per_objfile->die_type_hash
23642 = htab_up (htab_create_alloc (127,
23643 per_cu_offset_and_type_hash,
23644 per_cu_offset_and_type_eq,
23645 NULL, xcalloc, xfree));
23646
23647 ofs.per_cu = cu->per_cu;
23648 ofs.sect_off = die->sect_off;
23649 ofs.type = type;
23650 slot = (struct dwarf2_per_cu_offset_and_type **)
23651 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23652 if (*slot)
23653 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23654 sect_offset_str (die->sect_off));
23655 *slot = XOBNEW (&objfile->objfile_obstack,
23656 struct dwarf2_per_cu_offset_and_type);
23657 **slot = ofs;
23658 return type;
23659 }
23660
23661 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23662 or return NULL if the die does not have a saved type. */
23663
23664 static struct type *
23665 get_die_type_at_offset (sect_offset sect_off,
23666 struct dwarf2_per_cu_data *per_cu)
23667 {
23668 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23669 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23670
23671 if (dwarf2_per_objfile->die_type_hash == NULL)
23672 return NULL;
23673
23674 ofs.per_cu = per_cu;
23675 ofs.sect_off = sect_off;
23676 slot = ((struct dwarf2_per_cu_offset_and_type *)
23677 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23678 if (slot)
23679 return slot->type;
23680 else
23681 return NULL;
23682 }
23683
23684 /* Look up the type for DIE in CU in die_type_hash,
23685 or return NULL if DIE does not have a saved type. */
23686
23687 static struct type *
23688 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23689 {
23690 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23691 }
23692
23693 /* Add a dependence relationship from CU to REF_PER_CU. */
23694
23695 static void
23696 dwarf2_add_dependence (struct dwarf2_cu *cu,
23697 struct dwarf2_per_cu_data *ref_per_cu)
23698 {
23699 void **slot;
23700
23701 if (cu->dependencies == NULL)
23702 cu->dependencies
23703 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23704 NULL, &cu->comp_unit_obstack,
23705 hashtab_obstack_allocate,
23706 dummy_obstack_deallocate);
23707
23708 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23709 if (*slot == NULL)
23710 *slot = ref_per_cu;
23711 }
23712
23713 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23714 Set the mark field in every compilation unit in the
23715 cache that we must keep because we are keeping CU. */
23716
23717 static int
23718 dwarf2_mark_helper (void **slot, void *data)
23719 {
23720 struct dwarf2_per_cu_data *per_cu;
23721
23722 per_cu = (struct dwarf2_per_cu_data *) *slot;
23723
23724 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23725 reading of the chain. As such dependencies remain valid it is not much
23726 useful to track and undo them during QUIT cleanups. */
23727 if (per_cu->cu == NULL)
23728 return 1;
23729
23730 if (per_cu->cu->mark)
23731 return 1;
23732 per_cu->cu->mark = true;
23733
23734 if (per_cu->cu->dependencies != NULL)
23735 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23736
23737 return 1;
23738 }
23739
23740 /* Set the mark field in CU and in every other compilation unit in the
23741 cache that we must keep because we are keeping CU. */
23742
23743 static void
23744 dwarf2_mark (struct dwarf2_cu *cu)
23745 {
23746 if (cu->mark)
23747 return;
23748 cu->mark = true;
23749 if (cu->dependencies != NULL)
23750 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23751 }
23752
23753 static void
23754 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23755 {
23756 while (per_cu)
23757 {
23758 per_cu->cu->mark = false;
23759 per_cu = per_cu->cu->read_in_chain;
23760 }
23761 }
23762
23763 /* Trivial hash function for partial_die_info: the hash value of a DIE
23764 is its offset in .debug_info for this objfile. */
23765
23766 static hashval_t
23767 partial_die_hash (const void *item)
23768 {
23769 const struct partial_die_info *part_die
23770 = (const struct partial_die_info *) item;
23771
23772 return to_underlying (part_die->sect_off);
23773 }
23774
23775 /* Trivial comparison function for partial_die_info structures: two DIEs
23776 are equal if they have the same offset. */
23777
23778 static int
23779 partial_die_eq (const void *item_lhs, const void *item_rhs)
23780 {
23781 const struct partial_die_info *part_die_lhs
23782 = (const struct partial_die_info *) item_lhs;
23783 const struct partial_die_info *part_die_rhs
23784 = (const struct partial_die_info *) item_rhs;
23785
23786 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23787 }
23788
23789 struct cmd_list_element *set_dwarf_cmdlist;
23790 struct cmd_list_element *show_dwarf_cmdlist;
23791
23792 static void
23793 show_check_physname (struct ui_file *file, int from_tty,
23794 struct cmd_list_element *c, const char *value)
23795 {
23796 fprintf_filtered (file,
23797 _("Whether to check \"physname\" is %s.\n"),
23798 value);
23799 }
23800
23801 void _initialize_dwarf2_read ();
23802 void
23803 _initialize_dwarf2_read ()
23804 {
23805 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
23806 Set DWARF specific variables.\n\
23807 Configure DWARF variables such as the cache size."),
23808 &set_dwarf_cmdlist, "maintenance set dwarf ",
23809 0/*allow-unknown*/, &maintenance_set_cmdlist);
23810
23811 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
23812 Show DWARF specific variables.\n\
23813 Show DWARF variables such as the cache size."),
23814 &show_dwarf_cmdlist, "maintenance show dwarf ",
23815 0/*allow-unknown*/, &maintenance_show_cmdlist);
23816
23817 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23818 &dwarf_max_cache_age, _("\
23819 Set the upper bound on the age of cached DWARF compilation units."), _("\
23820 Show the upper bound on the age of cached DWARF compilation units."), _("\
23821 A higher limit means that cached compilation units will be stored\n\
23822 in memory longer, and more total memory will be used. Zero disables\n\
23823 caching, which can slow down startup."),
23824 NULL,
23825 show_dwarf_max_cache_age,
23826 &set_dwarf_cmdlist,
23827 &show_dwarf_cmdlist);
23828
23829 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23830 Set debugging of the DWARF reader."), _("\
23831 Show debugging of the DWARF reader."), _("\
23832 When enabled (non-zero), debugging messages are printed during DWARF\n\
23833 reading and symtab expansion. A value of 1 (one) provides basic\n\
23834 information. A value greater than 1 provides more verbose information."),
23835 NULL,
23836 NULL,
23837 &setdebuglist, &showdebuglist);
23838
23839 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23840 Set debugging of the DWARF DIE reader."), _("\
23841 Show debugging of the DWARF DIE reader."), _("\
23842 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23843 The value is the maximum depth to print."),
23844 NULL,
23845 NULL,
23846 &setdebuglist, &showdebuglist);
23847
23848 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23849 Set debugging of the dwarf line reader."), _("\
23850 Show debugging of the dwarf line reader."), _("\
23851 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23852 A value of 1 (one) provides basic information.\n\
23853 A value greater than 1 provides more verbose information."),
23854 NULL,
23855 NULL,
23856 &setdebuglist, &showdebuglist);
23857
23858 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23859 Set cross-checking of \"physname\" code against demangler."), _("\
23860 Show cross-checking of \"physname\" code against demangler."), _("\
23861 When enabled, GDB's internal \"physname\" code is checked against\n\
23862 the demangler."),
23863 NULL, show_check_physname,
23864 &setdebuglist, &showdebuglist);
23865
23866 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23867 no_class, &use_deprecated_index_sections, _("\
23868 Set whether to use deprecated gdb_index sections."), _("\
23869 Show whether to use deprecated gdb_index sections."), _("\
23870 When enabled, deprecated .gdb_index sections are used anyway.\n\
23871 Normally they are ignored either because of a missing feature or\n\
23872 performance issue.\n\
23873 Warning: This option must be enabled before gdb reads the file."),
23874 NULL,
23875 NULL,
23876 &setlist, &showlist);
23877
23878 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23879 &dwarf2_locexpr_funcs);
23880 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23881 &dwarf2_loclist_funcs);
23882
23883 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23884 &dwarf2_block_frame_base_locexpr_funcs);
23885 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23886 &dwarf2_block_frame_base_loclist_funcs);
23887
23888 #if GDB_SELF_TEST
23889 selftests::register_test ("dw2_expand_symtabs_matching",
23890 selftests::dw2_expand_symtabs_matching::run_test);
23891 selftests::register_test ("dwarf2_find_containing_comp_unit",
23892 selftests::find_containing_comp_unit::run_test);
23893 #endif
23894 }