d71bf9173838b245b25a856dc3e78422af685a6b
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "dwarf2/stringify.h"
44 #include "bfd.h"
45 #include "elf-bfd.h"
46 #include "symtab.h"
47 #include "gdbtypes.h"
48 #include "objfiles.h"
49 #include "dwarf2.h"
50 #include "buildsym.h"
51 #include "demangle.h"
52 #include "gdb-demangle.h"
53 #include "filenames.h" /* for DOSish file names */
54 #include "language.h"
55 #include "complaints.h"
56 #include "dwarf2/expr.h"
57 #include "dwarf2/loc.h"
58 #include "cp-support.h"
59 #include "hashtab.h"
60 #include "command.h"
61 #include "gdbcmd.h"
62 #include "block.h"
63 #include "addrmap.h"
64 #include "typeprint.h"
65 #include "psympriv.h"
66 #include "c-lang.h"
67 #include "go-lang.h"
68 #include "valprint.h"
69 #include "gdbcore.h" /* for gnutarget */
70 #include "gdb/gdb-index.h"
71 #include "gdb_bfd.h"
72 #include "f-lang.h"
73 #include "source.h"
74 #include "build-id.h"
75 #include "namespace.h"
76 #include "gdbsupport/function-view.h"
77 #include "gdbsupport/gdb_optional.h"
78 #include "gdbsupport/underlying.h"
79 #include "gdbsupport/hash_enum.h"
80 #include "filename-seen-cache.h"
81 #include "producer.h"
82 #include <fcntl.h>
83 #include <algorithm>
84 #include <unordered_map>
85 #include "gdbsupport/selftest.h"
86 #include "rust-lang.h"
87 #include "gdbsupport/pathstuff.h"
88 #include "count-one-bits.h"
89 #include "debuginfod-support.h"
90
91 /* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94 static unsigned int dwarf_read_debug = 0;
95
96 /* When non-zero, dump DIEs after they are read in. */
97 static unsigned int dwarf_die_debug = 0;
98
99 /* When non-zero, dump line number entries as they are read in. */
100 unsigned int dwarf_line_debug = 0;
101
102 /* When true, cross-check physname against demangler. */
103 static bool check_physname = false;
104
105 /* When true, do not reject deprecated .gdb_index sections. */
106 static bool use_deprecated_index_sections = false;
107
108 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
109
110 /* The "aclass" indices for various kinds of computed DWARF symbols. */
111
112 static int dwarf2_locexpr_index;
113 static int dwarf2_loclist_index;
114 static int dwarf2_locexpr_block_index;
115 static int dwarf2_loclist_block_index;
116
117 /* Size of .debug_loclists section header for 32-bit DWARF format. */
118 #define LOCLIST_HEADER_SIZE32 12
119
120 /* Size of .debug_loclists section header for 64-bit DWARF format. */
121 #define LOCLIST_HEADER_SIZE64 20
122
123 /* An index into a (C++) symbol name component in a symbol name as
124 recorded in the mapped_index's symbol table. For each C++ symbol
125 in the symbol table, we record one entry for the start of each
126 component in the symbol in a table of name components, and then
127 sort the table, in order to be able to binary search symbol names,
128 ignoring leading namespaces, both completion and regular look up.
129 For example, for symbol "A::B::C", we'll have an entry that points
130 to "A::B::C", another that points to "B::C", and another for "C".
131 Note that function symbols in GDB index have no parameter
132 information, just the function/method names. You can convert a
133 name_component to a "const char *" using the
134 'mapped_index::symbol_name_at(offset_type)' method. */
135
136 struct name_component
137 {
138 /* Offset in the symbol name where the component starts. Stored as
139 a (32-bit) offset instead of a pointer to save memory and improve
140 locality on 64-bit architectures. */
141 offset_type name_offset;
142
143 /* The symbol's index in the symbol and constant pool tables of a
144 mapped_index. */
145 offset_type idx;
146 };
147
148 /* Base class containing bits shared by both .gdb_index and
149 .debug_name indexes. */
150
151 struct mapped_index_base
152 {
153 mapped_index_base () = default;
154 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
155
156 /* The name_component table (a sorted vector). See name_component's
157 description above. */
158 std::vector<name_component> name_components;
159
160 /* How NAME_COMPONENTS is sorted. */
161 enum case_sensitivity name_components_casing;
162
163 /* Return the number of names in the symbol table. */
164 virtual size_t symbol_name_count () const = 0;
165
166 /* Get the name of the symbol at IDX in the symbol table. */
167 virtual const char *symbol_name_at (offset_type idx) const = 0;
168
169 /* Return whether the name at IDX in the symbol table should be
170 ignored. */
171 virtual bool symbol_name_slot_invalid (offset_type idx) const
172 {
173 return false;
174 }
175
176 /* Build the symbol name component sorted vector, if we haven't
177 yet. */
178 void build_name_components ();
179
180 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
181 possible matches for LN_NO_PARAMS in the name component
182 vector. */
183 std::pair<std::vector<name_component>::const_iterator,
184 std::vector<name_component>::const_iterator>
185 find_name_components_bounds (const lookup_name_info &ln_no_params,
186 enum language lang) const;
187
188 /* Prevent deleting/destroying via a base class pointer. */
189 protected:
190 ~mapped_index_base() = default;
191 };
192
193 /* A description of the mapped index. The file format is described in
194 a comment by the code that writes the index. */
195 struct mapped_index final : public mapped_index_base
196 {
197 /* A slot/bucket in the symbol table hash. */
198 struct symbol_table_slot
199 {
200 const offset_type name;
201 const offset_type vec;
202 };
203
204 /* Index data format version. */
205 int version = 0;
206
207 /* The address table data. */
208 gdb::array_view<const gdb_byte> address_table;
209
210 /* The symbol table, implemented as a hash table. */
211 gdb::array_view<symbol_table_slot> symbol_table;
212
213 /* A pointer to the constant pool. */
214 const char *constant_pool = nullptr;
215
216 bool symbol_name_slot_invalid (offset_type idx) const override
217 {
218 const auto &bucket = this->symbol_table[idx];
219 return bucket.name == 0 && bucket.vec == 0;
220 }
221
222 /* Convenience method to get at the name of the symbol at IDX in the
223 symbol table. */
224 const char *symbol_name_at (offset_type idx) const override
225 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
226
227 size_t symbol_name_count () const override
228 { return this->symbol_table.size (); }
229 };
230
231 /* A description of the mapped .debug_names.
232 Uninitialized map has CU_COUNT 0. */
233 struct mapped_debug_names final : public mapped_index_base
234 {
235 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
236 : dwarf2_per_objfile (dwarf2_per_objfile_)
237 {}
238
239 struct dwarf2_per_objfile *dwarf2_per_objfile;
240 bfd_endian dwarf5_byte_order;
241 bool dwarf5_is_dwarf64;
242 bool augmentation_is_gdb;
243 uint8_t offset_size;
244 uint32_t cu_count = 0;
245 uint32_t tu_count, bucket_count, name_count;
246 const gdb_byte *cu_table_reordered, *tu_table_reordered;
247 const uint32_t *bucket_table_reordered, *hash_table_reordered;
248 const gdb_byte *name_table_string_offs_reordered;
249 const gdb_byte *name_table_entry_offs_reordered;
250 const gdb_byte *entry_pool;
251
252 struct index_val
253 {
254 ULONGEST dwarf_tag;
255 struct attr
256 {
257 /* Attribute name DW_IDX_*. */
258 ULONGEST dw_idx;
259
260 /* Attribute form DW_FORM_*. */
261 ULONGEST form;
262
263 /* Value if FORM is DW_FORM_implicit_const. */
264 LONGEST implicit_const;
265 };
266 std::vector<attr> attr_vec;
267 };
268
269 std::unordered_map<ULONGEST, index_val> abbrev_map;
270
271 const char *namei_to_name (uint32_t namei) const;
272
273 /* Implementation of the mapped_index_base virtual interface, for
274 the name_components cache. */
275
276 const char *symbol_name_at (offset_type idx) const override
277 { return namei_to_name (idx); }
278
279 size_t symbol_name_count () const override
280 { return this->name_count; }
281 };
282
283 /* See dwarf2read.h. */
284
285 dwarf2_per_objfile *
286 get_dwarf2_per_objfile (struct objfile *objfile)
287 {
288 return dwarf2_objfile_data_key.get (objfile);
289 }
290
291 /* Default names of the debugging sections. */
292
293 /* Note that if the debugging section has been compressed, it might
294 have a name like .zdebug_info. */
295
296 static const struct dwarf2_debug_sections dwarf2_elf_names =
297 {
298 { ".debug_info", ".zdebug_info" },
299 { ".debug_abbrev", ".zdebug_abbrev" },
300 { ".debug_line", ".zdebug_line" },
301 { ".debug_loc", ".zdebug_loc" },
302 { ".debug_loclists", ".zdebug_loclists" },
303 { ".debug_macinfo", ".zdebug_macinfo" },
304 { ".debug_macro", ".zdebug_macro" },
305 { ".debug_str", ".zdebug_str" },
306 { ".debug_str_offsets", ".zdebug_str_offsets" },
307 { ".debug_line_str", ".zdebug_line_str" },
308 { ".debug_ranges", ".zdebug_ranges" },
309 { ".debug_rnglists", ".zdebug_rnglists" },
310 { ".debug_types", ".zdebug_types" },
311 { ".debug_addr", ".zdebug_addr" },
312 { ".debug_frame", ".zdebug_frame" },
313 { ".eh_frame", NULL },
314 { ".gdb_index", ".zgdb_index" },
315 { ".debug_names", ".zdebug_names" },
316 { ".debug_aranges", ".zdebug_aranges" },
317 23
318 };
319
320 /* List of DWO/DWP sections. */
321
322 static const struct dwop_section_names
323 {
324 struct dwarf2_section_names abbrev_dwo;
325 struct dwarf2_section_names info_dwo;
326 struct dwarf2_section_names line_dwo;
327 struct dwarf2_section_names loc_dwo;
328 struct dwarf2_section_names loclists_dwo;
329 struct dwarf2_section_names macinfo_dwo;
330 struct dwarf2_section_names macro_dwo;
331 struct dwarf2_section_names str_dwo;
332 struct dwarf2_section_names str_offsets_dwo;
333 struct dwarf2_section_names types_dwo;
334 struct dwarf2_section_names cu_index;
335 struct dwarf2_section_names tu_index;
336 }
337 dwop_section_names =
338 {
339 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
340 { ".debug_info.dwo", ".zdebug_info.dwo" },
341 { ".debug_line.dwo", ".zdebug_line.dwo" },
342 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
343 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
344 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
345 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
346 { ".debug_str.dwo", ".zdebug_str.dwo" },
347 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
348 { ".debug_types.dwo", ".zdebug_types.dwo" },
349 { ".debug_cu_index", ".zdebug_cu_index" },
350 { ".debug_tu_index", ".zdebug_tu_index" },
351 };
352
353 /* local data types */
354
355 /* The location list section (.debug_loclists) begins with a header,
356 which contains the following information. */
357 struct loclist_header
358 {
359 /* A 4-byte or 12-byte length containing the length of the
360 set of entries for this compilation unit, not including the
361 length field itself. */
362 unsigned int length;
363
364 /* A 2-byte version identifier. */
365 short version;
366
367 /* A 1-byte unsigned integer containing the size in bytes of an address on
368 the target system. */
369 unsigned char addr_size;
370
371 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
372 on the target system. */
373 unsigned char segment_collector_size;
374
375 /* A 4-byte count of the number of offsets that follow the header. */
376 unsigned int offset_entry_count;
377 };
378
379 /* Type used for delaying computation of method physnames.
380 See comments for compute_delayed_physnames. */
381 struct delayed_method_info
382 {
383 /* The type to which the method is attached, i.e., its parent class. */
384 struct type *type;
385
386 /* The index of the method in the type's function fieldlists. */
387 int fnfield_index;
388
389 /* The index of the method in the fieldlist. */
390 int index;
391
392 /* The name of the DIE. */
393 const char *name;
394
395 /* The DIE associated with this method. */
396 struct die_info *die;
397 };
398
399 /* Internal state when decoding a particular compilation unit. */
400 struct dwarf2_cu
401 {
402 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
403 ~dwarf2_cu ();
404
405 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
406
407 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
408 Create the set of symtabs used by this TU, or if this TU is sharing
409 symtabs with another TU and the symtabs have already been created
410 then restore those symtabs in the line header.
411 We don't need the pc/line-number mapping for type units. */
412 void setup_type_unit_groups (struct die_info *die);
413
414 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
415 buildsym_compunit constructor. */
416 struct compunit_symtab *start_symtab (const char *name,
417 const char *comp_dir,
418 CORE_ADDR low_pc);
419
420 /* Reset the builder. */
421 void reset_builder () { m_builder.reset (); }
422
423 /* The header of the compilation unit. */
424 struct comp_unit_head header {};
425
426 /* Base address of this compilation unit. */
427 gdb::optional<CORE_ADDR> base_address;
428
429 /* The language we are debugging. */
430 enum language language = language_unknown;
431 const struct language_defn *language_defn = nullptr;
432
433 const char *producer = nullptr;
434
435 private:
436 /* The symtab builder for this CU. This is only non-NULL when full
437 symbols are being read. */
438 std::unique_ptr<buildsym_compunit> m_builder;
439
440 public:
441 /* The generic symbol table building routines have separate lists for
442 file scope symbols and all all other scopes (local scopes). So
443 we need to select the right one to pass to add_symbol_to_list().
444 We do it by keeping a pointer to the correct list in list_in_scope.
445
446 FIXME: The original dwarf code just treated the file scope as the
447 first local scope, and all other local scopes as nested local
448 scopes, and worked fine. Check to see if we really need to
449 distinguish these in buildsym.c. */
450 struct pending **list_in_scope = nullptr;
451
452 /* Hash table holding all the loaded partial DIEs
453 with partial_die->offset.SECT_OFF as hash. */
454 htab_t partial_dies = nullptr;
455
456 /* Storage for things with the same lifetime as this read-in compilation
457 unit, including partial DIEs. */
458 auto_obstack comp_unit_obstack;
459
460 /* When multiple dwarf2_cu structures are living in memory, this field
461 chains them all together, so that they can be released efficiently.
462 We will probably also want a generation counter so that most-recently-used
463 compilation units are cached... */
464 struct dwarf2_per_cu_data *read_in_chain = nullptr;
465
466 /* Backlink to our per_cu entry. */
467 struct dwarf2_per_cu_data *per_cu;
468
469 /* How many compilation units ago was this CU last referenced? */
470 int last_used = 0;
471
472 /* A hash table of DIE cu_offset for following references with
473 die_info->offset.sect_off as hash. */
474 htab_t die_hash = nullptr;
475
476 /* Full DIEs if read in. */
477 struct die_info *dies = nullptr;
478
479 /* A set of pointers to dwarf2_per_cu_data objects for compilation
480 units referenced by this one. Only set during full symbol processing;
481 partial symbol tables do not have dependencies. */
482 htab_t dependencies = nullptr;
483
484 /* Header data from the line table, during full symbol processing. */
485 struct line_header *line_header = nullptr;
486 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
487 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
488 this is the DW_TAG_compile_unit die for this CU. We'll hold on
489 to the line header as long as this DIE is being processed. See
490 process_die_scope. */
491 die_info *line_header_die_owner = nullptr;
492
493 /* A list of methods which need to have physnames computed
494 after all type information has been read. */
495 std::vector<delayed_method_info> method_list;
496
497 /* To be copied to symtab->call_site_htab. */
498 htab_t call_site_htab = nullptr;
499
500 /* Non-NULL if this CU came from a DWO file.
501 There is an invariant here that is important to remember:
502 Except for attributes copied from the top level DIE in the "main"
503 (or "stub") file in preparation for reading the DWO file
504 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
505 Either there isn't a DWO file (in which case this is NULL and the point
506 is moot), or there is and either we're not going to read it (in which
507 case this is NULL) or there is and we are reading it (in which case this
508 is non-NULL). */
509 struct dwo_unit *dwo_unit = nullptr;
510
511 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
512 Note this value comes from the Fission stub CU/TU's DIE. */
513 gdb::optional<ULONGEST> addr_base;
514
515 /* The DW_AT_rnglists_base attribute if present.
516 Note this value comes from the Fission stub CU/TU's DIE.
517 Also note that the value is zero in the non-DWO case so this value can
518 be used without needing to know whether DWO files are in use or not.
519 N.B. This does not apply to DW_AT_ranges appearing in
520 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
521 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
522 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
523 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
524 ULONGEST ranges_base = 0;
525
526 /* The DW_AT_loclists_base attribute if present. */
527 ULONGEST loclist_base = 0;
528
529 /* When reading debug info generated by older versions of rustc, we
530 have to rewrite some union types to be struct types with a
531 variant part. This rewriting must be done after the CU is fully
532 read in, because otherwise at the point of rewriting some struct
533 type might not have been fully processed. So, we keep a list of
534 all such types here and process them after expansion. */
535 std::vector<struct type *> rust_unions;
536
537 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
538 files, the value is implicitly zero. For DWARF 5 version DWO files, the
539 value is often implicit and is the size of the header of
540 .debug_str_offsets section (8 or 4, depending on the address size). */
541 gdb::optional<ULONGEST> str_offsets_base;
542
543 /* Mark used when releasing cached dies. */
544 bool mark : 1;
545
546 /* This CU references .debug_loc. See the symtab->locations_valid field.
547 This test is imperfect as there may exist optimized debug code not using
548 any location list and still facing inlining issues if handled as
549 unoptimized code. For a future better test see GCC PR other/32998. */
550 bool has_loclist : 1;
551
552 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
553 if all the producer_is_* fields are valid. This information is cached
554 because profiling CU expansion showed excessive time spent in
555 producer_is_gxx_lt_4_6. */
556 bool checked_producer : 1;
557 bool producer_is_gxx_lt_4_6 : 1;
558 bool producer_is_gcc_lt_4_3 : 1;
559 bool producer_is_icc : 1;
560 bool producer_is_icc_lt_14 : 1;
561 bool producer_is_codewarrior : 1;
562
563 /* When true, the file that we're processing is known to have
564 debugging info for C++ namespaces. GCC 3.3.x did not produce
565 this information, but later versions do. */
566
567 bool processing_has_namespace_info : 1;
568
569 struct partial_die_info *find_partial_die (sect_offset sect_off);
570
571 /* If this CU was inherited by another CU (via specification,
572 abstract_origin, etc), this is the ancestor CU. */
573 dwarf2_cu *ancestor;
574
575 /* Get the buildsym_compunit for this CU. */
576 buildsym_compunit *get_builder ()
577 {
578 /* If this CU has a builder associated with it, use that. */
579 if (m_builder != nullptr)
580 return m_builder.get ();
581
582 /* Otherwise, search ancestors for a valid builder. */
583 if (ancestor != nullptr)
584 return ancestor->get_builder ();
585
586 return nullptr;
587 }
588 };
589
590 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
591 This includes type_unit_group and quick_file_names. */
592
593 struct stmt_list_hash
594 {
595 /* The DWO unit this table is from or NULL if there is none. */
596 struct dwo_unit *dwo_unit;
597
598 /* Offset in .debug_line or .debug_line.dwo. */
599 sect_offset line_sect_off;
600 };
601
602 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
603 an object of this type. */
604
605 struct type_unit_group
606 {
607 /* dwarf2read.c's main "handle" on a TU symtab.
608 To simplify things we create an artificial CU that "includes" all the
609 type units using this stmt_list so that the rest of the code still has
610 a "per_cu" handle on the symtab. */
611 struct dwarf2_per_cu_data per_cu;
612
613 /* The TUs that share this DW_AT_stmt_list entry.
614 This is added to while parsing type units to build partial symtabs,
615 and is deleted afterwards and not used again. */
616 std::vector<signatured_type *> *tus;
617
618 /* The compunit symtab.
619 Type units in a group needn't all be defined in the same source file,
620 so we create an essentially anonymous symtab as the compunit symtab. */
621 struct compunit_symtab *compunit_symtab;
622
623 /* The data used to construct the hash key. */
624 struct stmt_list_hash hash;
625
626 /* The symbol tables for this TU (obtained from the files listed in
627 DW_AT_stmt_list).
628 WARNING: The order of entries here must match the order of entries
629 in the line header. After the first TU using this type_unit_group, the
630 line header for the subsequent TUs is recreated from this. This is done
631 because we need to use the same symtabs for each TU using the same
632 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
633 there's no guarantee the line header doesn't have duplicate entries. */
634 struct symtab **symtabs;
635 };
636
637 /* These sections are what may appear in a (real or virtual) DWO file. */
638
639 struct dwo_sections
640 {
641 struct dwarf2_section_info abbrev;
642 struct dwarf2_section_info line;
643 struct dwarf2_section_info loc;
644 struct dwarf2_section_info loclists;
645 struct dwarf2_section_info macinfo;
646 struct dwarf2_section_info macro;
647 struct dwarf2_section_info str;
648 struct dwarf2_section_info str_offsets;
649 /* In the case of a virtual DWO file, these two are unused. */
650 struct dwarf2_section_info info;
651 std::vector<dwarf2_section_info> types;
652 };
653
654 /* CUs/TUs in DWP/DWO files. */
655
656 struct dwo_unit
657 {
658 /* Backlink to the containing struct dwo_file. */
659 struct dwo_file *dwo_file;
660
661 /* The "id" that distinguishes this CU/TU.
662 .debug_info calls this "dwo_id", .debug_types calls this "signature".
663 Since signatures came first, we stick with it for consistency. */
664 ULONGEST signature;
665
666 /* The section this CU/TU lives in, in the DWO file. */
667 struct dwarf2_section_info *section;
668
669 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
670 sect_offset sect_off;
671 unsigned int length;
672
673 /* For types, offset in the type's DIE of the type defined by this TU. */
674 cu_offset type_offset_in_tu;
675 };
676
677 /* include/dwarf2.h defines the DWP section codes.
678 It defines a max value but it doesn't define a min value, which we
679 use for error checking, so provide one. */
680
681 enum dwp_v2_section_ids
682 {
683 DW_SECT_MIN = 1
684 };
685
686 /* Data for one DWO file.
687
688 This includes virtual DWO files (a virtual DWO file is a DWO file as it
689 appears in a DWP file). DWP files don't really have DWO files per se -
690 comdat folding of types "loses" the DWO file they came from, and from
691 a high level view DWP files appear to contain a mass of random types.
692 However, to maintain consistency with the non-DWP case we pretend DWP
693 files contain virtual DWO files, and we assign each TU with one virtual
694 DWO file (generally based on the line and abbrev section offsets -
695 a heuristic that seems to work in practice). */
696
697 struct dwo_file
698 {
699 dwo_file () = default;
700 DISABLE_COPY_AND_ASSIGN (dwo_file);
701
702 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
703 For virtual DWO files the name is constructed from the section offsets
704 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
705 from related CU+TUs. */
706 const char *dwo_name = nullptr;
707
708 /* The DW_AT_comp_dir attribute. */
709 const char *comp_dir = nullptr;
710
711 /* The bfd, when the file is open. Otherwise this is NULL.
712 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
713 gdb_bfd_ref_ptr dbfd;
714
715 /* The sections that make up this DWO file.
716 Remember that for virtual DWO files in DWP V2, these are virtual
717 sections (for lack of a better name). */
718 struct dwo_sections sections {};
719
720 /* The CUs in the file.
721 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
722 an extension to handle LLVM's Link Time Optimization output (where
723 multiple source files may be compiled into a single object/dwo pair). */
724 htab_up cus;
725
726 /* Table of TUs in the file.
727 Each element is a struct dwo_unit. */
728 htab_up tus;
729 };
730
731 /* These sections are what may appear in a DWP file. */
732
733 struct dwp_sections
734 {
735 /* These are used by both DWP version 1 and 2. */
736 struct dwarf2_section_info str;
737 struct dwarf2_section_info cu_index;
738 struct dwarf2_section_info tu_index;
739
740 /* These are only used by DWP version 2 files.
741 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
742 sections are referenced by section number, and are not recorded here.
743 In DWP version 2 there is at most one copy of all these sections, each
744 section being (effectively) comprised of the concatenation of all of the
745 individual sections that exist in the version 1 format.
746 To keep the code simple we treat each of these concatenated pieces as a
747 section itself (a virtual section?). */
748 struct dwarf2_section_info abbrev;
749 struct dwarf2_section_info info;
750 struct dwarf2_section_info line;
751 struct dwarf2_section_info loc;
752 struct dwarf2_section_info macinfo;
753 struct dwarf2_section_info macro;
754 struct dwarf2_section_info str_offsets;
755 struct dwarf2_section_info types;
756 };
757
758 /* These sections are what may appear in a virtual DWO file in DWP version 1.
759 A virtual DWO file is a DWO file as it appears in a DWP file. */
760
761 struct virtual_v1_dwo_sections
762 {
763 struct dwarf2_section_info abbrev;
764 struct dwarf2_section_info line;
765 struct dwarf2_section_info loc;
766 struct dwarf2_section_info macinfo;
767 struct dwarf2_section_info macro;
768 struct dwarf2_section_info str_offsets;
769 /* Each DWP hash table entry records one CU or one TU.
770 That is recorded here, and copied to dwo_unit.section. */
771 struct dwarf2_section_info info_or_types;
772 };
773
774 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
775 In version 2, the sections of the DWO files are concatenated together
776 and stored in one section of that name. Thus each ELF section contains
777 several "virtual" sections. */
778
779 struct virtual_v2_dwo_sections
780 {
781 bfd_size_type abbrev_offset;
782 bfd_size_type abbrev_size;
783
784 bfd_size_type line_offset;
785 bfd_size_type line_size;
786
787 bfd_size_type loc_offset;
788 bfd_size_type loc_size;
789
790 bfd_size_type macinfo_offset;
791 bfd_size_type macinfo_size;
792
793 bfd_size_type macro_offset;
794 bfd_size_type macro_size;
795
796 bfd_size_type str_offsets_offset;
797 bfd_size_type str_offsets_size;
798
799 /* Each DWP hash table entry records one CU or one TU.
800 That is recorded here, and copied to dwo_unit.section. */
801 bfd_size_type info_or_types_offset;
802 bfd_size_type info_or_types_size;
803 };
804
805 /* Contents of DWP hash tables. */
806
807 struct dwp_hash_table
808 {
809 uint32_t version, nr_columns;
810 uint32_t nr_units, nr_slots;
811 const gdb_byte *hash_table, *unit_table;
812 union
813 {
814 struct
815 {
816 const gdb_byte *indices;
817 } v1;
818 struct
819 {
820 /* This is indexed by column number and gives the id of the section
821 in that column. */
822 #define MAX_NR_V2_DWO_SECTIONS \
823 (1 /* .debug_info or .debug_types */ \
824 + 1 /* .debug_abbrev */ \
825 + 1 /* .debug_line */ \
826 + 1 /* .debug_loc */ \
827 + 1 /* .debug_str_offsets */ \
828 + 1 /* .debug_macro or .debug_macinfo */)
829 int section_ids[MAX_NR_V2_DWO_SECTIONS];
830 const gdb_byte *offsets;
831 const gdb_byte *sizes;
832 } v2;
833 } section_pool;
834 };
835
836 /* Data for one DWP file. */
837
838 struct dwp_file
839 {
840 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
841 : name (name_),
842 dbfd (std::move (abfd))
843 {
844 }
845
846 /* Name of the file. */
847 const char *name;
848
849 /* File format version. */
850 int version = 0;
851
852 /* The bfd. */
853 gdb_bfd_ref_ptr dbfd;
854
855 /* Section info for this file. */
856 struct dwp_sections sections {};
857
858 /* Table of CUs in the file. */
859 const struct dwp_hash_table *cus = nullptr;
860
861 /* Table of TUs in the file. */
862 const struct dwp_hash_table *tus = nullptr;
863
864 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
865 htab_up loaded_cus;
866 htab_up loaded_tus;
867
868 /* Table to map ELF section numbers to their sections.
869 This is only needed for the DWP V1 file format. */
870 unsigned int num_sections = 0;
871 asection **elf_sections = nullptr;
872 };
873
874 /* Struct used to pass misc. parameters to read_die_and_children, et
875 al. which are used for both .debug_info and .debug_types dies.
876 All parameters here are unchanging for the life of the call. This
877 struct exists to abstract away the constant parameters of die reading. */
878
879 struct die_reader_specs
880 {
881 /* The bfd of die_section. */
882 bfd* abfd;
883
884 /* The CU of the DIE we are parsing. */
885 struct dwarf2_cu *cu;
886
887 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
888 struct dwo_file *dwo_file;
889
890 /* The section the die comes from.
891 This is either .debug_info or .debug_types, or the .dwo variants. */
892 struct dwarf2_section_info *die_section;
893
894 /* die_section->buffer. */
895 const gdb_byte *buffer;
896
897 /* The end of the buffer. */
898 const gdb_byte *buffer_end;
899
900 /* The abbreviation table to use when reading the DIEs. */
901 struct abbrev_table *abbrev_table;
902 };
903
904 /* A subclass of die_reader_specs that holds storage and has complex
905 constructor and destructor behavior. */
906
907 class cutu_reader : public die_reader_specs
908 {
909 public:
910
911 cutu_reader (struct dwarf2_per_cu_data *this_cu,
912 struct abbrev_table *abbrev_table,
913 int use_existing_cu,
914 bool skip_partial);
915
916 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
917 struct dwarf2_cu *parent_cu = nullptr,
918 struct dwo_file *dwo_file = nullptr);
919
920 DISABLE_COPY_AND_ASSIGN (cutu_reader);
921
922 const gdb_byte *info_ptr = nullptr;
923 struct die_info *comp_unit_die = nullptr;
924 bool dummy_p = false;
925
926 /* Release the new CU, putting it on the chain. This cannot be done
927 for dummy CUs. */
928 void keep ();
929
930 private:
931 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
932 int use_existing_cu);
933
934 struct dwarf2_per_cu_data *m_this_cu;
935 std::unique_ptr<dwarf2_cu> m_new_cu;
936
937 /* The ordinary abbreviation table. */
938 abbrev_table_up m_abbrev_table_holder;
939
940 /* The DWO abbreviation table. */
941 abbrev_table_up m_dwo_abbrev_table;
942 };
943
944 /* When we construct a partial symbol table entry we only
945 need this much information. */
946 struct partial_die_info : public allocate_on_obstack
947 {
948 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
949
950 /* Disable assign but still keep copy ctor, which is needed
951 load_partial_dies. */
952 partial_die_info& operator=(const partial_die_info& rhs) = delete;
953
954 /* Adjust the partial die before generating a symbol for it. This
955 function may set the is_external flag or change the DIE's
956 name. */
957 void fixup (struct dwarf2_cu *cu);
958
959 /* Read a minimal amount of information into the minimal die
960 structure. */
961 const gdb_byte *read (const struct die_reader_specs *reader,
962 const struct abbrev_info &abbrev,
963 const gdb_byte *info_ptr);
964
965 /* Offset of this DIE. */
966 const sect_offset sect_off;
967
968 /* DWARF-2 tag for this DIE. */
969 const ENUM_BITFIELD(dwarf_tag) tag : 16;
970
971 /* Assorted flags describing the data found in this DIE. */
972 const unsigned int has_children : 1;
973
974 unsigned int is_external : 1;
975 unsigned int is_declaration : 1;
976 unsigned int has_type : 1;
977 unsigned int has_specification : 1;
978 unsigned int has_pc_info : 1;
979 unsigned int may_be_inlined : 1;
980
981 /* This DIE has been marked DW_AT_main_subprogram. */
982 unsigned int main_subprogram : 1;
983
984 /* Flag set if the SCOPE field of this structure has been
985 computed. */
986 unsigned int scope_set : 1;
987
988 /* Flag set if the DIE has a byte_size attribute. */
989 unsigned int has_byte_size : 1;
990
991 /* Flag set if the DIE has a DW_AT_const_value attribute. */
992 unsigned int has_const_value : 1;
993
994 /* Flag set if any of the DIE's children are template arguments. */
995 unsigned int has_template_arguments : 1;
996
997 /* Flag set if fixup has been called on this die. */
998 unsigned int fixup_called : 1;
999
1000 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1001 unsigned int is_dwz : 1;
1002
1003 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1004 unsigned int spec_is_dwz : 1;
1005
1006 /* The name of this DIE. Normally the value of DW_AT_name, but
1007 sometimes a default name for unnamed DIEs. */
1008 const char *name = nullptr;
1009
1010 /* The linkage name, if present. */
1011 const char *linkage_name = nullptr;
1012
1013 /* The scope to prepend to our children. This is generally
1014 allocated on the comp_unit_obstack, so will disappear
1015 when this compilation unit leaves the cache. */
1016 const char *scope = nullptr;
1017
1018 /* Some data associated with the partial DIE. The tag determines
1019 which field is live. */
1020 union
1021 {
1022 /* The location description associated with this DIE, if any. */
1023 struct dwarf_block *locdesc;
1024 /* The offset of an import, for DW_TAG_imported_unit. */
1025 sect_offset sect_off;
1026 } d {};
1027
1028 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1029 CORE_ADDR lowpc = 0;
1030 CORE_ADDR highpc = 0;
1031
1032 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1033 DW_AT_sibling, if any. */
1034 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1035 could return DW_AT_sibling values to its caller load_partial_dies. */
1036 const gdb_byte *sibling = nullptr;
1037
1038 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1039 DW_AT_specification (or DW_AT_abstract_origin or
1040 DW_AT_extension). */
1041 sect_offset spec_offset {};
1042
1043 /* Pointers to this DIE's parent, first child, and next sibling,
1044 if any. */
1045 struct partial_die_info *die_parent = nullptr;
1046 struct partial_die_info *die_child = nullptr;
1047 struct partial_die_info *die_sibling = nullptr;
1048
1049 friend struct partial_die_info *
1050 dwarf2_cu::find_partial_die (sect_offset sect_off);
1051
1052 private:
1053 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1054 partial_die_info (sect_offset sect_off)
1055 : partial_die_info (sect_off, DW_TAG_padding, 0)
1056 {
1057 }
1058
1059 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1060 int has_children_)
1061 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1062 {
1063 is_external = 0;
1064 is_declaration = 0;
1065 has_type = 0;
1066 has_specification = 0;
1067 has_pc_info = 0;
1068 may_be_inlined = 0;
1069 main_subprogram = 0;
1070 scope_set = 0;
1071 has_byte_size = 0;
1072 has_const_value = 0;
1073 has_template_arguments = 0;
1074 fixup_called = 0;
1075 is_dwz = 0;
1076 spec_is_dwz = 0;
1077 }
1078 };
1079
1080 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1081 but this would require a corresponding change in unpack_field_as_long
1082 and friends. */
1083 static int bits_per_byte = 8;
1084
1085 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 psymbol.ginfo.set_linkage_name (actual_name);
8382 add_psymbol_to_list (psymbol, *where, objfile);
8383 }
8384 }
8385
8386 /* Read a partial die corresponding to a namespace; also, add a symbol
8387 corresponding to that namespace to the symbol table. NAMESPACE is
8388 the name of the enclosing namespace. */
8389
8390 static void
8391 add_partial_namespace (struct partial_die_info *pdi,
8392 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8393 int set_addrmap, struct dwarf2_cu *cu)
8394 {
8395 /* Add a symbol for the namespace. */
8396
8397 add_partial_symbol (pdi, cu);
8398
8399 /* Now scan partial symbols in that namespace. */
8400
8401 if (pdi->has_children)
8402 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8403 }
8404
8405 /* Read a partial die corresponding to a Fortran module. */
8406
8407 static void
8408 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8409 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8410 {
8411 /* Add a symbol for the namespace. */
8412
8413 add_partial_symbol (pdi, cu);
8414
8415 /* Now scan partial symbols in that module. */
8416
8417 if (pdi->has_children)
8418 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8419 }
8420
8421 /* Read a partial die corresponding to a subprogram or an inlined
8422 subprogram and create a partial symbol for that subprogram.
8423 When the CU language allows it, this routine also defines a partial
8424 symbol for each nested subprogram that this subprogram contains.
8425 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8426 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8427
8428 PDI may also be a lexical block, in which case we simply search
8429 recursively for subprograms defined inside that lexical block.
8430 Again, this is only performed when the CU language allows this
8431 type of definitions. */
8432
8433 static void
8434 add_partial_subprogram (struct partial_die_info *pdi,
8435 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8436 int set_addrmap, struct dwarf2_cu *cu)
8437 {
8438 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8439 {
8440 if (pdi->has_pc_info)
8441 {
8442 if (pdi->lowpc < *lowpc)
8443 *lowpc = pdi->lowpc;
8444 if (pdi->highpc > *highpc)
8445 *highpc = pdi->highpc;
8446 if (set_addrmap)
8447 {
8448 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8449 struct gdbarch *gdbarch = objfile->arch ();
8450 CORE_ADDR baseaddr;
8451 CORE_ADDR this_highpc;
8452 CORE_ADDR this_lowpc;
8453
8454 baseaddr = objfile->text_section_offset ();
8455 this_lowpc
8456 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8457 pdi->lowpc + baseaddr)
8458 - baseaddr);
8459 this_highpc
8460 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8461 pdi->highpc + baseaddr)
8462 - baseaddr);
8463 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8464 this_lowpc, this_highpc - 1,
8465 cu->per_cu->v.psymtab);
8466 }
8467 }
8468
8469 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8470 {
8471 if (!pdi->is_declaration)
8472 /* Ignore subprogram DIEs that do not have a name, they are
8473 illegal. Do not emit a complaint at this point, we will
8474 do so when we convert this psymtab into a symtab. */
8475 if (pdi->name)
8476 add_partial_symbol (pdi, cu);
8477 }
8478 }
8479
8480 if (! pdi->has_children)
8481 return;
8482
8483 if (cu->language == language_ada || cu->language == language_fortran)
8484 {
8485 pdi = pdi->die_child;
8486 while (pdi != NULL)
8487 {
8488 pdi->fixup (cu);
8489 if (pdi->tag == DW_TAG_subprogram
8490 || pdi->tag == DW_TAG_inlined_subroutine
8491 || pdi->tag == DW_TAG_lexical_block)
8492 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8493 pdi = pdi->die_sibling;
8494 }
8495 }
8496 }
8497
8498 /* Read a partial die corresponding to an enumeration type. */
8499
8500 static void
8501 add_partial_enumeration (struct partial_die_info *enum_pdi,
8502 struct dwarf2_cu *cu)
8503 {
8504 struct partial_die_info *pdi;
8505
8506 if (enum_pdi->name != NULL)
8507 add_partial_symbol (enum_pdi, cu);
8508
8509 pdi = enum_pdi->die_child;
8510 while (pdi)
8511 {
8512 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8513 complaint (_("malformed enumerator DIE ignored"));
8514 else
8515 add_partial_symbol (pdi, cu);
8516 pdi = pdi->die_sibling;
8517 }
8518 }
8519
8520 /* Return the initial uleb128 in the die at INFO_PTR. */
8521
8522 static unsigned int
8523 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8524 {
8525 unsigned int bytes_read;
8526
8527 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8528 }
8529
8530 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8531 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8532
8533 Return the corresponding abbrev, or NULL if the number is zero (indicating
8534 an empty DIE). In either case *BYTES_READ will be set to the length of
8535 the initial number. */
8536
8537 static struct abbrev_info *
8538 peek_die_abbrev (const die_reader_specs &reader,
8539 const gdb_byte *info_ptr, unsigned int *bytes_read)
8540 {
8541 dwarf2_cu *cu = reader.cu;
8542 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8543 unsigned int abbrev_number
8544 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8545
8546 if (abbrev_number == 0)
8547 return NULL;
8548
8549 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8550 if (!abbrev)
8551 {
8552 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8553 " at offset %s [in module %s]"),
8554 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8555 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8556 }
8557
8558 return abbrev;
8559 }
8560
8561 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8562 Returns a pointer to the end of a series of DIEs, terminated by an empty
8563 DIE. Any children of the skipped DIEs will also be skipped. */
8564
8565 static const gdb_byte *
8566 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8567 {
8568 while (1)
8569 {
8570 unsigned int bytes_read;
8571 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8572
8573 if (abbrev == NULL)
8574 return info_ptr + bytes_read;
8575 else
8576 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8577 }
8578 }
8579
8580 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8581 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8582 abbrev corresponding to that skipped uleb128 should be passed in
8583 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8584 children. */
8585
8586 static const gdb_byte *
8587 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8588 struct abbrev_info *abbrev)
8589 {
8590 unsigned int bytes_read;
8591 struct attribute attr;
8592 bfd *abfd = reader->abfd;
8593 struct dwarf2_cu *cu = reader->cu;
8594 const gdb_byte *buffer = reader->buffer;
8595 const gdb_byte *buffer_end = reader->buffer_end;
8596 unsigned int form, i;
8597
8598 for (i = 0; i < abbrev->num_attrs; i++)
8599 {
8600 /* The only abbrev we care about is DW_AT_sibling. */
8601 if (abbrev->attrs[i].name == DW_AT_sibling)
8602 {
8603 bool ignored;
8604 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8605 &ignored);
8606 if (attr.form == DW_FORM_ref_addr)
8607 complaint (_("ignoring absolute DW_AT_sibling"));
8608 else
8609 {
8610 sect_offset off = attr.get_ref_die_offset ();
8611 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8612
8613 if (sibling_ptr < info_ptr)
8614 complaint (_("DW_AT_sibling points backwards"));
8615 else if (sibling_ptr > reader->buffer_end)
8616 reader->die_section->overflow_complaint ();
8617 else
8618 return sibling_ptr;
8619 }
8620 }
8621
8622 /* If it isn't DW_AT_sibling, skip this attribute. */
8623 form = abbrev->attrs[i].form;
8624 skip_attribute:
8625 switch (form)
8626 {
8627 case DW_FORM_ref_addr:
8628 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8629 and later it is offset sized. */
8630 if (cu->header.version == 2)
8631 info_ptr += cu->header.addr_size;
8632 else
8633 info_ptr += cu->header.offset_size;
8634 break;
8635 case DW_FORM_GNU_ref_alt:
8636 info_ptr += cu->header.offset_size;
8637 break;
8638 case DW_FORM_addr:
8639 info_ptr += cu->header.addr_size;
8640 break;
8641 case DW_FORM_data1:
8642 case DW_FORM_ref1:
8643 case DW_FORM_flag:
8644 case DW_FORM_strx1:
8645 info_ptr += 1;
8646 break;
8647 case DW_FORM_flag_present:
8648 case DW_FORM_implicit_const:
8649 break;
8650 case DW_FORM_data2:
8651 case DW_FORM_ref2:
8652 case DW_FORM_strx2:
8653 info_ptr += 2;
8654 break;
8655 case DW_FORM_strx3:
8656 info_ptr += 3;
8657 break;
8658 case DW_FORM_data4:
8659 case DW_FORM_ref4:
8660 case DW_FORM_strx4:
8661 info_ptr += 4;
8662 break;
8663 case DW_FORM_data8:
8664 case DW_FORM_ref8:
8665 case DW_FORM_ref_sig8:
8666 info_ptr += 8;
8667 break;
8668 case DW_FORM_data16:
8669 info_ptr += 16;
8670 break;
8671 case DW_FORM_string:
8672 read_direct_string (abfd, info_ptr, &bytes_read);
8673 info_ptr += bytes_read;
8674 break;
8675 case DW_FORM_sec_offset:
8676 case DW_FORM_strp:
8677 case DW_FORM_GNU_strp_alt:
8678 info_ptr += cu->header.offset_size;
8679 break;
8680 case DW_FORM_exprloc:
8681 case DW_FORM_block:
8682 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8683 info_ptr += bytes_read;
8684 break;
8685 case DW_FORM_block1:
8686 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8687 break;
8688 case DW_FORM_block2:
8689 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8690 break;
8691 case DW_FORM_block4:
8692 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8693 break;
8694 case DW_FORM_addrx:
8695 case DW_FORM_strx:
8696 case DW_FORM_sdata:
8697 case DW_FORM_udata:
8698 case DW_FORM_ref_udata:
8699 case DW_FORM_GNU_addr_index:
8700 case DW_FORM_GNU_str_index:
8701 case DW_FORM_rnglistx:
8702 case DW_FORM_loclistx:
8703 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8704 break;
8705 case DW_FORM_indirect:
8706 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8707 info_ptr += bytes_read;
8708 /* We need to continue parsing from here, so just go back to
8709 the top. */
8710 goto skip_attribute;
8711
8712 default:
8713 error (_("Dwarf Error: Cannot handle %s "
8714 "in DWARF reader [in module %s]"),
8715 dwarf_form_name (form),
8716 bfd_get_filename (abfd));
8717 }
8718 }
8719
8720 if (abbrev->has_children)
8721 return skip_children (reader, info_ptr);
8722 else
8723 return info_ptr;
8724 }
8725
8726 /* Locate ORIG_PDI's sibling.
8727 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8728
8729 static const gdb_byte *
8730 locate_pdi_sibling (const struct die_reader_specs *reader,
8731 struct partial_die_info *orig_pdi,
8732 const gdb_byte *info_ptr)
8733 {
8734 /* Do we know the sibling already? */
8735
8736 if (orig_pdi->sibling)
8737 return orig_pdi->sibling;
8738
8739 /* Are there any children to deal with? */
8740
8741 if (!orig_pdi->has_children)
8742 return info_ptr;
8743
8744 /* Skip the children the long way. */
8745
8746 return skip_children (reader, info_ptr);
8747 }
8748
8749 /* Expand this partial symbol table into a full symbol table. SELF is
8750 not NULL. */
8751
8752 void
8753 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8754 {
8755 struct dwarf2_per_objfile *dwarf2_per_objfile
8756 = get_dwarf2_per_objfile (objfile);
8757
8758 gdb_assert (!readin);
8759 /* If this psymtab is constructed from a debug-only objfile, the
8760 has_section_at_zero flag will not necessarily be correct. We
8761 can get the correct value for this flag by looking at the data
8762 associated with the (presumably stripped) associated objfile. */
8763 if (objfile->separate_debug_objfile_backlink)
8764 {
8765 struct dwarf2_per_objfile *dpo_backlink
8766 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8767
8768 dwarf2_per_objfile->has_section_at_zero
8769 = dpo_backlink->has_section_at_zero;
8770 }
8771
8772 expand_psymtab (objfile);
8773
8774 process_cu_includes (dwarf2_per_objfile);
8775 }
8776 \f
8777 /* Reading in full CUs. */
8778
8779 /* Add PER_CU to the queue. */
8780
8781 static void
8782 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8783 enum language pretend_language)
8784 {
8785 per_cu->queued = 1;
8786 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8787 }
8788
8789 /* If PER_CU is not yet queued, add it to the queue.
8790 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8791 dependency.
8792 The result is non-zero if PER_CU was queued, otherwise the result is zero
8793 meaning either PER_CU is already queued or it is already loaded.
8794
8795 N.B. There is an invariant here that if a CU is queued then it is loaded.
8796 The caller is required to load PER_CU if we return non-zero. */
8797
8798 static int
8799 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8800 struct dwarf2_per_cu_data *per_cu,
8801 enum language pretend_language)
8802 {
8803 /* We may arrive here during partial symbol reading, if we need full
8804 DIEs to process an unusual case (e.g. template arguments). Do
8805 not queue PER_CU, just tell our caller to load its DIEs. */
8806 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8807 {
8808 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8809 return 1;
8810 return 0;
8811 }
8812
8813 /* Mark the dependence relation so that we don't flush PER_CU
8814 too early. */
8815 if (dependent_cu != NULL)
8816 dwarf2_add_dependence (dependent_cu, per_cu);
8817
8818 /* If it's already on the queue, we have nothing to do. */
8819 if (per_cu->queued)
8820 return 0;
8821
8822 /* If the compilation unit is already loaded, just mark it as
8823 used. */
8824 if (per_cu->cu != NULL)
8825 {
8826 per_cu->cu->last_used = 0;
8827 return 0;
8828 }
8829
8830 /* Add it to the queue. */
8831 queue_comp_unit (per_cu, pretend_language);
8832
8833 return 1;
8834 }
8835
8836 /* Process the queue. */
8837
8838 static void
8839 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8840 {
8841 if (dwarf_read_debug)
8842 {
8843 fprintf_unfiltered (gdb_stdlog,
8844 "Expanding one or more symtabs of objfile %s ...\n",
8845 objfile_name (dwarf2_per_objfile->objfile));
8846 }
8847
8848 /* The queue starts out with one item, but following a DIE reference
8849 may load a new CU, adding it to the end of the queue. */
8850 while (!dwarf2_per_objfile->queue.empty ())
8851 {
8852 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8853
8854 if ((dwarf2_per_objfile->using_index
8855 ? !item.per_cu->v.quick->compunit_symtab
8856 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8857 /* Skip dummy CUs. */
8858 && item.per_cu->cu != NULL)
8859 {
8860 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8861 unsigned int debug_print_threshold;
8862 char buf[100];
8863
8864 if (per_cu->is_debug_types)
8865 {
8866 struct signatured_type *sig_type =
8867 (struct signatured_type *) per_cu;
8868
8869 sprintf (buf, "TU %s at offset %s",
8870 hex_string (sig_type->signature),
8871 sect_offset_str (per_cu->sect_off));
8872 /* There can be 100s of TUs.
8873 Only print them in verbose mode. */
8874 debug_print_threshold = 2;
8875 }
8876 else
8877 {
8878 sprintf (buf, "CU at offset %s",
8879 sect_offset_str (per_cu->sect_off));
8880 debug_print_threshold = 1;
8881 }
8882
8883 if (dwarf_read_debug >= debug_print_threshold)
8884 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8885
8886 if (per_cu->is_debug_types)
8887 process_full_type_unit (per_cu, item.pretend_language);
8888 else
8889 process_full_comp_unit (per_cu, item.pretend_language);
8890
8891 if (dwarf_read_debug >= debug_print_threshold)
8892 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8893 }
8894
8895 item.per_cu->queued = 0;
8896 dwarf2_per_objfile->queue.pop ();
8897 }
8898
8899 if (dwarf_read_debug)
8900 {
8901 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8902 objfile_name (dwarf2_per_objfile->objfile));
8903 }
8904 }
8905
8906 /* Read in full symbols for PST, and anything it depends on. */
8907
8908 void
8909 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8910 {
8911 gdb_assert (!readin);
8912
8913 expand_dependencies (objfile);
8914
8915 dw2_do_instantiate_symtab (per_cu_data, false);
8916 gdb_assert (get_compunit_symtab () != nullptr);
8917 }
8918
8919 /* Trivial hash function for die_info: the hash value of a DIE
8920 is its offset in .debug_info for this objfile. */
8921
8922 static hashval_t
8923 die_hash (const void *item)
8924 {
8925 const struct die_info *die = (const struct die_info *) item;
8926
8927 return to_underlying (die->sect_off);
8928 }
8929
8930 /* Trivial comparison function for die_info structures: two DIEs
8931 are equal if they have the same offset. */
8932
8933 static int
8934 die_eq (const void *item_lhs, const void *item_rhs)
8935 {
8936 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8937 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8938
8939 return die_lhs->sect_off == die_rhs->sect_off;
8940 }
8941
8942 /* Load the DIEs associated with PER_CU into memory. */
8943
8944 static void
8945 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8946 bool skip_partial,
8947 enum language pretend_language)
8948 {
8949 gdb_assert (! this_cu->is_debug_types);
8950
8951 cutu_reader reader (this_cu, NULL, 1, skip_partial);
8952 if (reader.dummy_p)
8953 return;
8954
8955 struct dwarf2_cu *cu = reader.cu;
8956 const gdb_byte *info_ptr = reader.info_ptr;
8957
8958 gdb_assert (cu->die_hash == NULL);
8959 cu->die_hash =
8960 htab_create_alloc_ex (cu->header.length / 12,
8961 die_hash,
8962 die_eq,
8963 NULL,
8964 &cu->comp_unit_obstack,
8965 hashtab_obstack_allocate,
8966 dummy_obstack_deallocate);
8967
8968 if (reader.comp_unit_die->has_children)
8969 reader.comp_unit_die->child
8970 = read_die_and_siblings (&reader, reader.info_ptr,
8971 &info_ptr, reader.comp_unit_die);
8972 cu->dies = reader.comp_unit_die;
8973 /* comp_unit_die is not stored in die_hash, no need. */
8974
8975 /* We try not to read any attributes in this function, because not
8976 all CUs needed for references have been loaded yet, and symbol
8977 table processing isn't initialized. But we have to set the CU language,
8978 or we won't be able to build types correctly.
8979 Similarly, if we do not read the producer, we can not apply
8980 producer-specific interpretation. */
8981 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8982
8983 reader.keep ();
8984 }
8985
8986 /* Add a DIE to the delayed physname list. */
8987
8988 static void
8989 add_to_method_list (struct type *type, int fnfield_index, int index,
8990 const char *name, struct die_info *die,
8991 struct dwarf2_cu *cu)
8992 {
8993 struct delayed_method_info mi;
8994 mi.type = type;
8995 mi.fnfield_index = fnfield_index;
8996 mi.index = index;
8997 mi.name = name;
8998 mi.die = die;
8999 cu->method_list.push_back (mi);
9000 }
9001
9002 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9003 "const" / "volatile". If so, decrements LEN by the length of the
9004 modifier and return true. Otherwise return false. */
9005
9006 template<size_t N>
9007 static bool
9008 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9009 {
9010 size_t mod_len = sizeof (mod) - 1;
9011 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9012 {
9013 len -= mod_len;
9014 return true;
9015 }
9016 return false;
9017 }
9018
9019 /* Compute the physnames of any methods on the CU's method list.
9020
9021 The computation of method physnames is delayed in order to avoid the
9022 (bad) condition that one of the method's formal parameters is of an as yet
9023 incomplete type. */
9024
9025 static void
9026 compute_delayed_physnames (struct dwarf2_cu *cu)
9027 {
9028 /* Only C++ delays computing physnames. */
9029 if (cu->method_list.empty ())
9030 return;
9031 gdb_assert (cu->language == language_cplus);
9032
9033 for (const delayed_method_info &mi : cu->method_list)
9034 {
9035 const char *physname;
9036 struct fn_fieldlist *fn_flp
9037 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9038 physname = dwarf2_physname (mi.name, mi.die, cu);
9039 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9040 = physname ? physname : "";
9041
9042 /* Since there's no tag to indicate whether a method is a
9043 const/volatile overload, extract that information out of the
9044 demangled name. */
9045 if (physname != NULL)
9046 {
9047 size_t len = strlen (physname);
9048
9049 while (1)
9050 {
9051 if (physname[len] == ')') /* shortcut */
9052 break;
9053 else if (check_modifier (physname, len, " const"))
9054 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9055 else if (check_modifier (physname, len, " volatile"))
9056 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9057 else
9058 break;
9059 }
9060 }
9061 }
9062
9063 /* The list is no longer needed. */
9064 cu->method_list.clear ();
9065 }
9066
9067 /* Go objects should be embedded in a DW_TAG_module DIE,
9068 and it's not clear if/how imported objects will appear.
9069 To keep Go support simple until that's worked out,
9070 go back through what we've read and create something usable.
9071 We could do this while processing each DIE, and feels kinda cleaner,
9072 but that way is more invasive.
9073 This is to, for example, allow the user to type "p var" or "b main"
9074 without having to specify the package name, and allow lookups
9075 of module.object to work in contexts that use the expression
9076 parser. */
9077
9078 static void
9079 fixup_go_packaging (struct dwarf2_cu *cu)
9080 {
9081 gdb::unique_xmalloc_ptr<char> package_name;
9082 struct pending *list;
9083 int i;
9084
9085 for (list = *cu->get_builder ()->get_global_symbols ();
9086 list != NULL;
9087 list = list->next)
9088 {
9089 for (i = 0; i < list->nsyms; ++i)
9090 {
9091 struct symbol *sym = list->symbol[i];
9092
9093 if (sym->language () == language_go
9094 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9095 {
9096 gdb::unique_xmalloc_ptr<char> this_package_name
9097 (go_symbol_package_name (sym));
9098
9099 if (this_package_name == NULL)
9100 continue;
9101 if (package_name == NULL)
9102 package_name = std::move (this_package_name);
9103 else
9104 {
9105 struct objfile *objfile
9106 = cu->per_cu->dwarf2_per_objfile->objfile;
9107 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9108 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9109 (symbol_symtab (sym) != NULL
9110 ? symtab_to_filename_for_display
9111 (symbol_symtab (sym))
9112 : objfile_name (objfile)),
9113 this_package_name.get (), package_name.get ());
9114 }
9115 }
9116 }
9117 }
9118
9119 if (package_name != NULL)
9120 {
9121 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9122 const char *saved_package_name = objfile->intern (package_name.get ());
9123 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9124 saved_package_name);
9125 struct symbol *sym;
9126
9127 sym = allocate_symbol (objfile);
9128 sym->set_language (language_go, &objfile->objfile_obstack);
9129 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9130 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9131 e.g., "main" finds the "main" module and not C's main(). */
9132 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9133 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9134 SYMBOL_TYPE (sym) = type;
9135
9136 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9137 }
9138 }
9139
9140 /* Allocate a fully-qualified name consisting of the two parts on the
9141 obstack. */
9142
9143 static const char *
9144 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9145 {
9146 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9147 }
9148
9149 /* A helper that allocates a variant part to attach to a Rust enum
9150 type. OBSTACK is where the results should be allocated. TYPE is
9151 the type we're processing. DISCRIMINANT_INDEX is the index of the
9152 discriminant. It must be the index of one of the fields of TYPE.
9153 DEFAULT_INDEX is the index of the default field; or -1 if there is
9154 no default. RANGES is indexed by "effective" field number (the
9155 field index, but omitting the discriminant and default fields) and
9156 must hold the discriminant values used by the variants. Note that
9157 RANGES must have a lifetime at least as long as OBSTACK -- either
9158 already allocated on it, or static. */
9159
9160 static void
9161 alloc_rust_variant (struct obstack *obstack, struct type *type,
9162 int discriminant_index, int default_index,
9163 gdb::array_view<discriminant_range> ranges)
9164 {
9165 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9166 must be handled by the caller. */
9167 gdb_assert (discriminant_index >= 0
9168 && discriminant_index < TYPE_NFIELDS (type));
9169 gdb_assert (default_index == -1
9170 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9171
9172 /* We have one variant for each non-discriminant field. */
9173 int n_variants = TYPE_NFIELDS (type) - 1;
9174
9175 variant *variants = new (obstack) variant[n_variants];
9176 int var_idx = 0;
9177 int range_idx = 0;
9178 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9179 {
9180 if (i == discriminant_index)
9181 continue;
9182
9183 variants[var_idx].first_field = i;
9184 variants[var_idx].last_field = i + 1;
9185
9186 /* The default field does not need a range, but other fields do.
9187 We skipped the discriminant above. */
9188 if (i != default_index)
9189 {
9190 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9191 ++range_idx;
9192 }
9193
9194 ++var_idx;
9195 }
9196
9197 gdb_assert (range_idx == ranges.size ());
9198 gdb_assert (var_idx == n_variants);
9199
9200 variant_part *part = new (obstack) variant_part;
9201 part->discriminant_index = discriminant_index;
9202 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9203 discriminant_index));
9204 part->variants = gdb::array_view<variant> (variants, n_variants);
9205
9206 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9207 gdb::array_view<variant_part> *prop_value
9208 = new (storage) gdb::array_view<variant_part> (part, 1);
9209
9210 struct dynamic_prop prop;
9211 prop.kind = PROP_VARIANT_PARTS;
9212 prop.data.variant_parts = prop_value;
9213
9214 add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop, type);
9215 }
9216
9217 /* Some versions of rustc emitted enums in an unusual way.
9218
9219 Ordinary enums were emitted as unions. The first element of each
9220 structure in the union was named "RUST$ENUM$DISR". This element
9221 held the discriminant.
9222
9223 These versions of Rust also implemented the "non-zero"
9224 optimization. When the enum had two values, and one is empty and
9225 the other holds a pointer that cannot be zero, the pointer is used
9226 as the discriminant, with a zero value meaning the empty variant.
9227 Here, the union's first member is of the form
9228 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9229 where the fieldnos are the indices of the fields that should be
9230 traversed in order to find the field (which may be several fields deep)
9231 and the variantname is the name of the variant of the case when the
9232 field is zero.
9233
9234 This function recognizes whether TYPE is of one of these forms,
9235 and, if so, smashes it to be a variant type. */
9236
9237 static void
9238 quirk_rust_enum (struct type *type, struct objfile *objfile)
9239 {
9240 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9241
9242 /* We don't need to deal with empty enums. */
9243 if (TYPE_NFIELDS (type) == 0)
9244 return;
9245
9246 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9247 if (TYPE_NFIELDS (type) == 1
9248 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9249 {
9250 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9251
9252 /* Decode the field name to find the offset of the
9253 discriminant. */
9254 ULONGEST bit_offset = 0;
9255 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9256 while (name[0] >= '0' && name[0] <= '9')
9257 {
9258 char *tail;
9259 unsigned long index = strtoul (name, &tail, 10);
9260 name = tail;
9261 if (*name != '$'
9262 || index >= TYPE_NFIELDS (field_type)
9263 || (TYPE_FIELD_LOC_KIND (field_type, index)
9264 != FIELD_LOC_KIND_BITPOS))
9265 {
9266 complaint (_("Could not parse Rust enum encoding string \"%s\""
9267 "[in module %s]"),
9268 TYPE_FIELD_NAME (type, 0),
9269 objfile_name (objfile));
9270 return;
9271 }
9272 ++name;
9273
9274 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9275 field_type = TYPE_FIELD_TYPE (field_type, index);
9276 }
9277
9278 /* Smash this type to be a structure type. We have to do this
9279 because the type has already been recorded. */
9280 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9281 TYPE_NFIELDS (type) = 3;
9282 /* Save the field we care about. */
9283 struct field saved_field = TYPE_FIELD (type, 0);
9284 TYPE_FIELDS (type)
9285 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9286
9287 /* Put the discriminant at index 0. */
9288 TYPE_FIELD_TYPE (type, 0) = field_type;
9289 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9290 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9291 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), bit_offset);
9292
9293 /* The order of fields doesn't really matter, so put the real
9294 field at index 1 and the data-less field at index 2. */
9295 TYPE_FIELD (type, 1) = saved_field;
9296 TYPE_FIELD_NAME (type, 1)
9297 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type, 1)));
9298 TYPE_NAME (TYPE_FIELD_TYPE (type, 1))
9299 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9300 TYPE_FIELD_NAME (type, 1));
9301
9302 const char *dataless_name
9303 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9304 name);
9305 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9306 dataless_name);
9307 TYPE_FIELD_TYPE (type, 2) = dataless_type;
9308 /* NAME points into the original discriminant name, which
9309 already has the correct lifetime. */
9310 TYPE_FIELD_NAME (type, 2) = name;
9311 SET_FIELD_BITPOS (TYPE_FIELD (type, 2), 0);
9312
9313 /* Indicate that this is a variant type. */
9314 static discriminant_range ranges[1] = { { 0, 0 } };
9315 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9316 }
9317 /* A union with a single anonymous field is probably an old-style
9318 univariant enum. */
9319 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9320 {
9321 /* Smash this type to be a structure type. We have to do this
9322 because the type has already been recorded. */
9323 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9324
9325 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9326 const char *variant_name
9327 = rust_last_path_segment (TYPE_NAME (field_type));
9328 TYPE_FIELD_NAME (type, 0) = variant_name;
9329 TYPE_NAME (field_type)
9330 = rust_fully_qualify (&objfile->objfile_obstack,
9331 TYPE_NAME (type), variant_name);
9332 }
9333 else
9334 {
9335 struct type *disr_type = nullptr;
9336 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9337 {
9338 disr_type = TYPE_FIELD_TYPE (type, i);
9339
9340 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9341 {
9342 /* All fields of a true enum will be structs. */
9343 return;
9344 }
9345 else if (TYPE_NFIELDS (disr_type) == 0)
9346 {
9347 /* Could be data-less variant, so keep going. */
9348 disr_type = nullptr;
9349 }
9350 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9351 "RUST$ENUM$DISR") != 0)
9352 {
9353 /* Not a Rust enum. */
9354 return;
9355 }
9356 else
9357 {
9358 /* Found one. */
9359 break;
9360 }
9361 }
9362
9363 /* If we got here without a discriminant, then it's probably
9364 just a union. */
9365 if (disr_type == nullptr)
9366 return;
9367
9368 /* Smash this type to be a structure type. We have to do this
9369 because the type has already been recorded. */
9370 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9371
9372 /* Make space for the discriminant field. */
9373 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9374 field *new_fields
9375 = (struct field *) TYPE_ZALLOC (type, (TYPE_NFIELDS (type)
9376 * sizeof (struct field)));
9377 memcpy (new_fields + 1, TYPE_FIELDS (type),
9378 TYPE_NFIELDS (type) * sizeof (struct field));
9379 TYPE_FIELDS (type) = new_fields;
9380 TYPE_NFIELDS (type) = TYPE_NFIELDS (type) + 1;
9381
9382 /* Install the discriminant at index 0 in the union. */
9383 TYPE_FIELD (type, 0) = *disr_field;
9384 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9385 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9386
9387 /* We need a way to find the correct discriminant given a
9388 variant name. For convenience we build a map here. */
9389 struct type *enum_type = FIELD_TYPE (*disr_field);
9390 std::unordered_map<std::string, ULONGEST> discriminant_map;
9391 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9392 {
9393 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9394 {
9395 const char *name
9396 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9397 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9398 }
9399 }
9400
9401 int n_fields = TYPE_NFIELDS (type);
9402 /* We don't need a range entry for the discriminant, but we do
9403 need one for every other field, as there is no default
9404 variant. */
9405 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9406 discriminant_range,
9407 n_fields - 1);
9408 /* Skip the discriminant here. */
9409 for (int i = 1; i < n_fields; ++i)
9410 {
9411 /* Find the final word in the name of this variant's type.
9412 That name can be used to look up the correct
9413 discriminant. */
9414 const char *variant_name
9415 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type, i)));
9416
9417 auto iter = discriminant_map.find (variant_name);
9418 if (iter != discriminant_map.end ())
9419 {
9420 ranges[i].low = iter->second;
9421 ranges[i].high = iter->second;
9422 }
9423
9424 /* Remove the discriminant field, if it exists. */
9425 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
9426 if (TYPE_NFIELDS (sub_type) > 0)
9427 {
9428 --TYPE_NFIELDS (sub_type);
9429 ++TYPE_FIELDS (sub_type);
9430 }
9431 TYPE_FIELD_NAME (type, i) = variant_name;
9432 TYPE_NAME (sub_type)
9433 = rust_fully_qualify (&objfile->objfile_obstack,
9434 TYPE_NAME (type), variant_name);
9435 }
9436
9437 /* Indicate that this is a variant type. */
9438 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9439 gdb::array_view<discriminant_range> (ranges,
9440 n_fields - 1));
9441 }
9442 }
9443
9444 /* Rewrite some Rust unions to be structures with variants parts. */
9445
9446 static void
9447 rust_union_quirks (struct dwarf2_cu *cu)
9448 {
9449 gdb_assert (cu->language == language_rust);
9450 for (type *type_ : cu->rust_unions)
9451 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9452 /* We don't need this any more. */
9453 cu->rust_unions.clear ();
9454 }
9455
9456 /* Return the symtab for PER_CU. This works properly regardless of
9457 whether we're using the index or psymtabs. */
9458
9459 static struct compunit_symtab *
9460 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9461 {
9462 return (per_cu->dwarf2_per_objfile->using_index
9463 ? per_cu->v.quick->compunit_symtab
9464 : per_cu->v.psymtab->compunit_symtab);
9465 }
9466
9467 /* A helper function for computing the list of all symbol tables
9468 included by PER_CU. */
9469
9470 static void
9471 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9472 htab_t all_children, htab_t all_type_symtabs,
9473 struct dwarf2_per_cu_data *per_cu,
9474 struct compunit_symtab *immediate_parent)
9475 {
9476 void **slot;
9477 struct compunit_symtab *cust;
9478
9479 slot = htab_find_slot (all_children, per_cu, INSERT);
9480 if (*slot != NULL)
9481 {
9482 /* This inclusion and its children have been processed. */
9483 return;
9484 }
9485
9486 *slot = per_cu;
9487 /* Only add a CU if it has a symbol table. */
9488 cust = get_compunit_symtab (per_cu);
9489 if (cust != NULL)
9490 {
9491 /* If this is a type unit only add its symbol table if we haven't
9492 seen it yet (type unit per_cu's can share symtabs). */
9493 if (per_cu->is_debug_types)
9494 {
9495 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9496 if (*slot == NULL)
9497 {
9498 *slot = cust;
9499 result->push_back (cust);
9500 if (cust->user == NULL)
9501 cust->user = immediate_parent;
9502 }
9503 }
9504 else
9505 {
9506 result->push_back (cust);
9507 if (cust->user == NULL)
9508 cust->user = immediate_parent;
9509 }
9510 }
9511
9512 if (!per_cu->imported_symtabs_empty ())
9513 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9514 {
9515 recursively_compute_inclusions (result, all_children,
9516 all_type_symtabs, ptr, cust);
9517 }
9518 }
9519
9520 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9521 PER_CU. */
9522
9523 static void
9524 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9525 {
9526 gdb_assert (! per_cu->is_debug_types);
9527
9528 if (!per_cu->imported_symtabs_empty ())
9529 {
9530 int len;
9531 std::vector<compunit_symtab *> result_symtabs;
9532 htab_t all_children, all_type_symtabs;
9533 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9534
9535 /* If we don't have a symtab, we can just skip this case. */
9536 if (cust == NULL)
9537 return;
9538
9539 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9540 NULL, xcalloc, xfree);
9541 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9542 NULL, xcalloc, xfree);
9543
9544 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9545 {
9546 recursively_compute_inclusions (&result_symtabs, all_children,
9547 all_type_symtabs, ptr, cust);
9548 }
9549
9550 /* Now we have a transitive closure of all the included symtabs. */
9551 len = result_symtabs.size ();
9552 cust->includes
9553 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9554 struct compunit_symtab *, len + 1);
9555 memcpy (cust->includes, result_symtabs.data (),
9556 len * sizeof (compunit_symtab *));
9557 cust->includes[len] = NULL;
9558
9559 htab_delete (all_children);
9560 htab_delete (all_type_symtabs);
9561 }
9562 }
9563
9564 /* Compute the 'includes' field for the symtabs of all the CUs we just
9565 read. */
9566
9567 static void
9568 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9569 {
9570 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9571 {
9572 if (! iter->is_debug_types)
9573 compute_compunit_symtab_includes (iter);
9574 }
9575
9576 dwarf2_per_objfile->just_read_cus.clear ();
9577 }
9578
9579 /* Generate full symbol information for PER_CU, whose DIEs have
9580 already been loaded into memory. */
9581
9582 static void
9583 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9584 enum language pretend_language)
9585 {
9586 struct dwarf2_cu *cu = per_cu->cu;
9587 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9588 struct objfile *objfile = dwarf2_per_objfile->objfile;
9589 struct gdbarch *gdbarch = objfile->arch ();
9590 CORE_ADDR lowpc, highpc;
9591 struct compunit_symtab *cust;
9592 CORE_ADDR baseaddr;
9593 struct block *static_block;
9594 CORE_ADDR addr;
9595
9596 baseaddr = objfile->text_section_offset ();
9597
9598 /* Clear the list here in case something was left over. */
9599 cu->method_list.clear ();
9600
9601 cu->language = pretend_language;
9602 cu->language_defn = language_def (cu->language);
9603
9604 /* Do line number decoding in read_file_scope () */
9605 process_die (cu->dies, cu);
9606
9607 /* For now fudge the Go package. */
9608 if (cu->language == language_go)
9609 fixup_go_packaging (cu);
9610
9611 /* Now that we have processed all the DIEs in the CU, all the types
9612 should be complete, and it should now be safe to compute all of the
9613 physnames. */
9614 compute_delayed_physnames (cu);
9615
9616 if (cu->language == language_rust)
9617 rust_union_quirks (cu);
9618
9619 /* Some compilers don't define a DW_AT_high_pc attribute for the
9620 compilation unit. If the DW_AT_high_pc is missing, synthesize
9621 it, by scanning the DIE's below the compilation unit. */
9622 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9623
9624 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9625 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9626
9627 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9628 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9629 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9630 addrmap to help ensure it has an accurate map of pc values belonging to
9631 this comp unit. */
9632 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9633
9634 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9635 SECT_OFF_TEXT (objfile),
9636 0);
9637
9638 if (cust != NULL)
9639 {
9640 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9641
9642 /* Set symtab language to language from DW_AT_language. If the
9643 compilation is from a C file generated by language preprocessors, do
9644 not set the language if it was already deduced by start_subfile. */
9645 if (!(cu->language == language_c
9646 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9647 COMPUNIT_FILETABS (cust)->language = cu->language;
9648
9649 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9650 produce DW_AT_location with location lists but it can be possibly
9651 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9652 there were bugs in prologue debug info, fixed later in GCC-4.5
9653 by "unwind info for epilogues" patch (which is not directly related).
9654
9655 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9656 needed, it would be wrong due to missing DW_AT_producer there.
9657
9658 Still one can confuse GDB by using non-standard GCC compilation
9659 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9660 */
9661 if (cu->has_loclist && gcc_4_minor >= 5)
9662 cust->locations_valid = 1;
9663
9664 if (gcc_4_minor >= 5)
9665 cust->epilogue_unwind_valid = 1;
9666
9667 cust->call_site_htab = cu->call_site_htab;
9668 }
9669
9670 if (dwarf2_per_objfile->using_index)
9671 per_cu->v.quick->compunit_symtab = cust;
9672 else
9673 {
9674 dwarf2_psymtab *pst = per_cu->v.psymtab;
9675 pst->compunit_symtab = cust;
9676 pst->readin = true;
9677 }
9678
9679 /* Push it for inclusion processing later. */
9680 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9681
9682 /* Not needed any more. */
9683 cu->reset_builder ();
9684 }
9685
9686 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9687 already been loaded into memory. */
9688
9689 static void
9690 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9691 enum language pretend_language)
9692 {
9693 struct dwarf2_cu *cu = per_cu->cu;
9694 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9695 struct objfile *objfile = dwarf2_per_objfile->objfile;
9696 struct compunit_symtab *cust;
9697 struct signatured_type *sig_type;
9698
9699 gdb_assert (per_cu->is_debug_types);
9700 sig_type = (struct signatured_type *) per_cu;
9701
9702 /* Clear the list here in case something was left over. */
9703 cu->method_list.clear ();
9704
9705 cu->language = pretend_language;
9706 cu->language_defn = language_def (cu->language);
9707
9708 /* The symbol tables are set up in read_type_unit_scope. */
9709 process_die (cu->dies, cu);
9710
9711 /* For now fudge the Go package. */
9712 if (cu->language == language_go)
9713 fixup_go_packaging (cu);
9714
9715 /* Now that we have processed all the DIEs in the CU, all the types
9716 should be complete, and it should now be safe to compute all of the
9717 physnames. */
9718 compute_delayed_physnames (cu);
9719
9720 if (cu->language == language_rust)
9721 rust_union_quirks (cu);
9722
9723 /* TUs share symbol tables.
9724 If this is the first TU to use this symtab, complete the construction
9725 of it with end_expandable_symtab. Otherwise, complete the addition of
9726 this TU's symbols to the existing symtab. */
9727 if (sig_type->type_unit_group->compunit_symtab == NULL)
9728 {
9729 buildsym_compunit *builder = cu->get_builder ();
9730 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9731 sig_type->type_unit_group->compunit_symtab = cust;
9732
9733 if (cust != NULL)
9734 {
9735 /* Set symtab language to language from DW_AT_language. If the
9736 compilation is from a C file generated by language preprocessors,
9737 do not set the language if it was already deduced by
9738 start_subfile. */
9739 if (!(cu->language == language_c
9740 && COMPUNIT_FILETABS (cust)->language != language_c))
9741 COMPUNIT_FILETABS (cust)->language = cu->language;
9742 }
9743 }
9744 else
9745 {
9746 cu->get_builder ()->augment_type_symtab ();
9747 cust = sig_type->type_unit_group->compunit_symtab;
9748 }
9749
9750 if (dwarf2_per_objfile->using_index)
9751 per_cu->v.quick->compunit_symtab = cust;
9752 else
9753 {
9754 dwarf2_psymtab *pst = per_cu->v.psymtab;
9755 pst->compunit_symtab = cust;
9756 pst->readin = true;
9757 }
9758
9759 /* Not needed any more. */
9760 cu->reset_builder ();
9761 }
9762
9763 /* Process an imported unit DIE. */
9764
9765 static void
9766 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9767 {
9768 struct attribute *attr;
9769
9770 /* For now we don't handle imported units in type units. */
9771 if (cu->per_cu->is_debug_types)
9772 {
9773 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9774 " supported in type units [in module %s]"),
9775 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9776 }
9777
9778 attr = dwarf2_attr (die, DW_AT_import, cu);
9779 if (attr != NULL)
9780 {
9781 sect_offset sect_off = attr->get_ref_die_offset ();
9782 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9783 dwarf2_per_cu_data *per_cu
9784 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9785 cu->per_cu->dwarf2_per_objfile);
9786
9787 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9788 into another compilation unit, at root level. Regard this as a hint,
9789 and ignore it. */
9790 if (die->parent && die->parent->parent == NULL
9791 && per_cu->unit_type == DW_UT_compile
9792 && per_cu->lang == language_cplus)
9793 return;
9794
9795 /* If necessary, add it to the queue and load its DIEs. */
9796 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9797 load_full_comp_unit (per_cu, false, cu->language);
9798
9799 cu->per_cu->imported_symtabs_push (per_cu);
9800 }
9801 }
9802
9803 /* RAII object that represents a process_die scope: i.e.,
9804 starts/finishes processing a DIE. */
9805 class process_die_scope
9806 {
9807 public:
9808 process_die_scope (die_info *die, dwarf2_cu *cu)
9809 : m_die (die), m_cu (cu)
9810 {
9811 /* We should only be processing DIEs not already in process. */
9812 gdb_assert (!m_die->in_process);
9813 m_die->in_process = true;
9814 }
9815
9816 ~process_die_scope ()
9817 {
9818 m_die->in_process = false;
9819
9820 /* If we're done processing the DIE for the CU that owns the line
9821 header, we don't need the line header anymore. */
9822 if (m_cu->line_header_die_owner == m_die)
9823 {
9824 delete m_cu->line_header;
9825 m_cu->line_header = NULL;
9826 m_cu->line_header_die_owner = NULL;
9827 }
9828 }
9829
9830 private:
9831 die_info *m_die;
9832 dwarf2_cu *m_cu;
9833 };
9834
9835 /* Process a die and its children. */
9836
9837 static void
9838 process_die (struct die_info *die, struct dwarf2_cu *cu)
9839 {
9840 process_die_scope scope (die, cu);
9841
9842 switch (die->tag)
9843 {
9844 case DW_TAG_padding:
9845 break;
9846 case DW_TAG_compile_unit:
9847 case DW_TAG_partial_unit:
9848 read_file_scope (die, cu);
9849 break;
9850 case DW_TAG_type_unit:
9851 read_type_unit_scope (die, cu);
9852 break;
9853 case DW_TAG_subprogram:
9854 /* Nested subprograms in Fortran get a prefix. */
9855 if (cu->language == language_fortran
9856 && die->parent != NULL
9857 && die->parent->tag == DW_TAG_subprogram)
9858 cu->processing_has_namespace_info = true;
9859 /* Fall through. */
9860 case DW_TAG_inlined_subroutine:
9861 read_func_scope (die, cu);
9862 break;
9863 case DW_TAG_lexical_block:
9864 case DW_TAG_try_block:
9865 case DW_TAG_catch_block:
9866 read_lexical_block_scope (die, cu);
9867 break;
9868 case DW_TAG_call_site:
9869 case DW_TAG_GNU_call_site:
9870 read_call_site_scope (die, cu);
9871 break;
9872 case DW_TAG_class_type:
9873 case DW_TAG_interface_type:
9874 case DW_TAG_structure_type:
9875 case DW_TAG_union_type:
9876 process_structure_scope (die, cu);
9877 break;
9878 case DW_TAG_enumeration_type:
9879 process_enumeration_scope (die, cu);
9880 break;
9881
9882 /* These dies have a type, but processing them does not create
9883 a symbol or recurse to process the children. Therefore we can
9884 read them on-demand through read_type_die. */
9885 case DW_TAG_subroutine_type:
9886 case DW_TAG_set_type:
9887 case DW_TAG_array_type:
9888 case DW_TAG_pointer_type:
9889 case DW_TAG_ptr_to_member_type:
9890 case DW_TAG_reference_type:
9891 case DW_TAG_rvalue_reference_type:
9892 case DW_TAG_string_type:
9893 break;
9894
9895 case DW_TAG_base_type:
9896 case DW_TAG_subrange_type:
9897 case DW_TAG_typedef:
9898 /* Add a typedef symbol for the type definition, if it has a
9899 DW_AT_name. */
9900 new_symbol (die, read_type_die (die, cu), cu);
9901 break;
9902 case DW_TAG_common_block:
9903 read_common_block (die, cu);
9904 break;
9905 case DW_TAG_common_inclusion:
9906 break;
9907 case DW_TAG_namespace:
9908 cu->processing_has_namespace_info = true;
9909 read_namespace (die, cu);
9910 break;
9911 case DW_TAG_module:
9912 cu->processing_has_namespace_info = true;
9913 read_module (die, cu);
9914 break;
9915 case DW_TAG_imported_declaration:
9916 cu->processing_has_namespace_info = true;
9917 if (read_namespace_alias (die, cu))
9918 break;
9919 /* The declaration is not a global namespace alias. */
9920 /* Fall through. */
9921 case DW_TAG_imported_module:
9922 cu->processing_has_namespace_info = true;
9923 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9924 || cu->language != language_fortran))
9925 complaint (_("Tag '%s' has unexpected children"),
9926 dwarf_tag_name (die->tag));
9927 read_import_statement (die, cu);
9928 break;
9929
9930 case DW_TAG_imported_unit:
9931 process_imported_unit_die (die, cu);
9932 break;
9933
9934 case DW_TAG_variable:
9935 read_variable (die, cu);
9936 break;
9937
9938 default:
9939 new_symbol (die, NULL, cu);
9940 break;
9941 }
9942 }
9943 \f
9944 /* DWARF name computation. */
9945
9946 /* A helper function for dwarf2_compute_name which determines whether DIE
9947 needs to have the name of the scope prepended to the name listed in the
9948 die. */
9949
9950 static int
9951 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9952 {
9953 struct attribute *attr;
9954
9955 switch (die->tag)
9956 {
9957 case DW_TAG_namespace:
9958 case DW_TAG_typedef:
9959 case DW_TAG_class_type:
9960 case DW_TAG_interface_type:
9961 case DW_TAG_structure_type:
9962 case DW_TAG_union_type:
9963 case DW_TAG_enumeration_type:
9964 case DW_TAG_enumerator:
9965 case DW_TAG_subprogram:
9966 case DW_TAG_inlined_subroutine:
9967 case DW_TAG_member:
9968 case DW_TAG_imported_declaration:
9969 return 1;
9970
9971 case DW_TAG_variable:
9972 case DW_TAG_constant:
9973 /* We only need to prefix "globally" visible variables. These include
9974 any variable marked with DW_AT_external or any variable that
9975 lives in a namespace. [Variables in anonymous namespaces
9976 require prefixing, but they are not DW_AT_external.] */
9977
9978 if (dwarf2_attr (die, DW_AT_specification, cu))
9979 {
9980 struct dwarf2_cu *spec_cu = cu;
9981
9982 return die_needs_namespace (die_specification (die, &spec_cu),
9983 spec_cu);
9984 }
9985
9986 attr = dwarf2_attr (die, DW_AT_external, cu);
9987 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9988 && die->parent->tag != DW_TAG_module)
9989 return 0;
9990 /* A variable in a lexical block of some kind does not need a
9991 namespace, even though in C++ such variables may be external
9992 and have a mangled name. */
9993 if (die->parent->tag == DW_TAG_lexical_block
9994 || die->parent->tag == DW_TAG_try_block
9995 || die->parent->tag == DW_TAG_catch_block
9996 || die->parent->tag == DW_TAG_subprogram)
9997 return 0;
9998 return 1;
9999
10000 default:
10001 return 0;
10002 }
10003 }
10004
10005 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10006 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10007 defined for the given DIE. */
10008
10009 static struct attribute *
10010 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10011 {
10012 struct attribute *attr;
10013
10014 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10015 if (attr == NULL)
10016 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10017
10018 return attr;
10019 }
10020
10021 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10022 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10023 defined for the given DIE. */
10024
10025 static const char *
10026 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10027 {
10028 const char *linkage_name;
10029
10030 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10031 if (linkage_name == NULL)
10032 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10033
10034 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10035 See https://github.com/rust-lang/rust/issues/32925. */
10036 if (cu->language == language_rust && linkage_name != NULL
10037 && strchr (linkage_name, '{') != NULL)
10038 linkage_name = NULL;
10039
10040 return linkage_name;
10041 }
10042
10043 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10044 compute the physname for the object, which include a method's:
10045 - formal parameters (C++),
10046 - receiver type (Go),
10047
10048 The term "physname" is a bit confusing.
10049 For C++, for example, it is the demangled name.
10050 For Go, for example, it's the mangled name.
10051
10052 For Ada, return the DIE's linkage name rather than the fully qualified
10053 name. PHYSNAME is ignored..
10054
10055 The result is allocated on the objfile_obstack and canonicalized. */
10056
10057 static const char *
10058 dwarf2_compute_name (const char *name,
10059 struct die_info *die, struct dwarf2_cu *cu,
10060 int physname)
10061 {
10062 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10063
10064 if (name == NULL)
10065 name = dwarf2_name (die, cu);
10066
10067 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10068 but otherwise compute it by typename_concat inside GDB.
10069 FIXME: Actually this is not really true, or at least not always true.
10070 It's all very confusing. compute_and_set_names doesn't try to demangle
10071 Fortran names because there is no mangling standard. So new_symbol
10072 will set the demangled name to the result of dwarf2_full_name, and it is
10073 the demangled name that GDB uses if it exists. */
10074 if (cu->language == language_ada
10075 || (cu->language == language_fortran && physname))
10076 {
10077 /* For Ada unit, we prefer the linkage name over the name, as
10078 the former contains the exported name, which the user expects
10079 to be able to reference. Ideally, we want the user to be able
10080 to reference this entity using either natural or linkage name,
10081 but we haven't started looking at this enhancement yet. */
10082 const char *linkage_name = dw2_linkage_name (die, cu);
10083
10084 if (linkage_name != NULL)
10085 return linkage_name;
10086 }
10087
10088 /* These are the only languages we know how to qualify names in. */
10089 if (name != NULL
10090 && (cu->language == language_cplus
10091 || cu->language == language_fortran || cu->language == language_d
10092 || cu->language == language_rust))
10093 {
10094 if (die_needs_namespace (die, cu))
10095 {
10096 const char *prefix;
10097 const char *canonical_name = NULL;
10098
10099 string_file buf;
10100
10101 prefix = determine_prefix (die, cu);
10102 if (*prefix != '\0')
10103 {
10104 gdb::unique_xmalloc_ptr<char> prefixed_name
10105 (typename_concat (NULL, prefix, name, physname, cu));
10106
10107 buf.puts (prefixed_name.get ());
10108 }
10109 else
10110 buf.puts (name);
10111
10112 /* Template parameters may be specified in the DIE's DW_AT_name, or
10113 as children with DW_TAG_template_type_param or
10114 DW_TAG_value_type_param. If the latter, add them to the name
10115 here. If the name already has template parameters, then
10116 skip this step; some versions of GCC emit both, and
10117 it is more efficient to use the pre-computed name.
10118
10119 Something to keep in mind about this process: it is very
10120 unlikely, or in some cases downright impossible, to produce
10121 something that will match the mangled name of a function.
10122 If the definition of the function has the same debug info,
10123 we should be able to match up with it anyway. But fallbacks
10124 using the minimal symbol, for instance to find a method
10125 implemented in a stripped copy of libstdc++, will not work.
10126 If we do not have debug info for the definition, we will have to
10127 match them up some other way.
10128
10129 When we do name matching there is a related problem with function
10130 templates; two instantiated function templates are allowed to
10131 differ only by their return types, which we do not add here. */
10132
10133 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10134 {
10135 struct attribute *attr;
10136 struct die_info *child;
10137 int first = 1;
10138
10139 die->building_fullname = 1;
10140
10141 for (child = die->child; child != NULL; child = child->sibling)
10142 {
10143 struct type *type;
10144 LONGEST value;
10145 const gdb_byte *bytes;
10146 struct dwarf2_locexpr_baton *baton;
10147 struct value *v;
10148
10149 if (child->tag != DW_TAG_template_type_param
10150 && child->tag != DW_TAG_template_value_param)
10151 continue;
10152
10153 if (first)
10154 {
10155 buf.puts ("<");
10156 first = 0;
10157 }
10158 else
10159 buf.puts (", ");
10160
10161 attr = dwarf2_attr (child, DW_AT_type, cu);
10162 if (attr == NULL)
10163 {
10164 complaint (_("template parameter missing DW_AT_type"));
10165 buf.puts ("UNKNOWN_TYPE");
10166 continue;
10167 }
10168 type = die_type (child, cu);
10169
10170 if (child->tag == DW_TAG_template_type_param)
10171 {
10172 c_print_type (type, "", &buf, -1, 0, cu->language,
10173 &type_print_raw_options);
10174 continue;
10175 }
10176
10177 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10178 if (attr == NULL)
10179 {
10180 complaint (_("template parameter missing "
10181 "DW_AT_const_value"));
10182 buf.puts ("UNKNOWN_VALUE");
10183 continue;
10184 }
10185
10186 dwarf2_const_value_attr (attr, type, name,
10187 &cu->comp_unit_obstack, cu,
10188 &value, &bytes, &baton);
10189
10190 if (TYPE_NOSIGN (type))
10191 /* GDB prints characters as NUMBER 'CHAR'. If that's
10192 changed, this can use value_print instead. */
10193 c_printchar (value, type, &buf);
10194 else
10195 {
10196 struct value_print_options opts;
10197
10198 if (baton != NULL)
10199 v = dwarf2_evaluate_loc_desc (type, NULL,
10200 baton->data,
10201 baton->size,
10202 baton->per_cu);
10203 else if (bytes != NULL)
10204 {
10205 v = allocate_value (type);
10206 memcpy (value_contents_writeable (v), bytes,
10207 TYPE_LENGTH (type));
10208 }
10209 else
10210 v = value_from_longest (type, value);
10211
10212 /* Specify decimal so that we do not depend on
10213 the radix. */
10214 get_formatted_print_options (&opts, 'd');
10215 opts.raw = 1;
10216 value_print (v, &buf, &opts);
10217 release_value (v);
10218 }
10219 }
10220
10221 die->building_fullname = 0;
10222
10223 if (!first)
10224 {
10225 /* Close the argument list, with a space if necessary
10226 (nested templates). */
10227 if (!buf.empty () && buf.string ().back () == '>')
10228 buf.puts (" >");
10229 else
10230 buf.puts (">");
10231 }
10232 }
10233
10234 /* For C++ methods, append formal parameter type
10235 information, if PHYSNAME. */
10236
10237 if (physname && die->tag == DW_TAG_subprogram
10238 && cu->language == language_cplus)
10239 {
10240 struct type *type = read_type_die (die, cu);
10241
10242 c_type_print_args (type, &buf, 1, cu->language,
10243 &type_print_raw_options);
10244
10245 if (cu->language == language_cplus)
10246 {
10247 /* Assume that an artificial first parameter is
10248 "this", but do not crash if it is not. RealView
10249 marks unnamed (and thus unused) parameters as
10250 artificial; there is no way to differentiate
10251 the two cases. */
10252 if (TYPE_NFIELDS (type) > 0
10253 && TYPE_FIELD_ARTIFICIAL (type, 0)
10254 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10255 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10256 0))))
10257 buf.puts (" const");
10258 }
10259 }
10260
10261 const std::string &intermediate_name = buf.string ();
10262
10263 if (cu->language == language_cplus)
10264 canonical_name
10265 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10266 objfile);
10267
10268 /* If we only computed INTERMEDIATE_NAME, or if
10269 INTERMEDIATE_NAME is already canonical, then we need to
10270 intern it. */
10271 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10272 name = objfile->intern (intermediate_name);
10273 else
10274 name = canonical_name;
10275 }
10276 }
10277
10278 return name;
10279 }
10280
10281 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10282 If scope qualifiers are appropriate they will be added. The result
10283 will be allocated on the storage_obstack, or NULL if the DIE does
10284 not have a name. NAME may either be from a previous call to
10285 dwarf2_name or NULL.
10286
10287 The output string will be canonicalized (if C++). */
10288
10289 static const char *
10290 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10291 {
10292 return dwarf2_compute_name (name, die, cu, 0);
10293 }
10294
10295 /* Construct a physname for the given DIE in CU. NAME may either be
10296 from a previous call to dwarf2_name or NULL. The result will be
10297 allocated on the objfile_objstack or NULL if the DIE does not have a
10298 name.
10299
10300 The output string will be canonicalized (if C++). */
10301
10302 static const char *
10303 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10304 {
10305 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10306 const char *retval, *mangled = NULL, *canon = NULL;
10307 int need_copy = 1;
10308
10309 /* In this case dwarf2_compute_name is just a shortcut not building anything
10310 on its own. */
10311 if (!die_needs_namespace (die, cu))
10312 return dwarf2_compute_name (name, die, cu, 1);
10313
10314 mangled = dw2_linkage_name (die, cu);
10315
10316 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10317 has computed. */
10318 gdb::unique_xmalloc_ptr<char> demangled;
10319 if (mangled != NULL)
10320 {
10321
10322 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10323 {
10324 /* Do nothing (do not demangle the symbol name). */
10325 }
10326 else if (cu->language == language_go)
10327 {
10328 /* This is a lie, but we already lie to the caller new_symbol.
10329 new_symbol assumes we return the mangled name.
10330 This just undoes that lie until things are cleaned up. */
10331 }
10332 else
10333 {
10334 /* Use DMGL_RET_DROP for C++ template functions to suppress
10335 their return type. It is easier for GDB users to search
10336 for such functions as `name(params)' than `long name(params)'.
10337 In such case the minimal symbol names do not match the full
10338 symbol names but for template functions there is never a need
10339 to look up their definition from their declaration so
10340 the only disadvantage remains the minimal symbol variant
10341 `long name(params)' does not have the proper inferior type. */
10342 demangled.reset (gdb_demangle (mangled,
10343 (DMGL_PARAMS | DMGL_ANSI
10344 | DMGL_RET_DROP)));
10345 }
10346 if (demangled)
10347 canon = demangled.get ();
10348 else
10349 {
10350 canon = mangled;
10351 need_copy = 0;
10352 }
10353 }
10354
10355 if (canon == NULL || check_physname)
10356 {
10357 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10358
10359 if (canon != NULL && strcmp (physname, canon) != 0)
10360 {
10361 /* It may not mean a bug in GDB. The compiler could also
10362 compute DW_AT_linkage_name incorrectly. But in such case
10363 GDB would need to be bug-to-bug compatible. */
10364
10365 complaint (_("Computed physname <%s> does not match demangled <%s> "
10366 "(from linkage <%s>) - DIE at %s [in module %s]"),
10367 physname, canon, mangled, sect_offset_str (die->sect_off),
10368 objfile_name (objfile));
10369
10370 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10371 is available here - over computed PHYSNAME. It is safer
10372 against both buggy GDB and buggy compilers. */
10373
10374 retval = canon;
10375 }
10376 else
10377 {
10378 retval = physname;
10379 need_copy = 0;
10380 }
10381 }
10382 else
10383 retval = canon;
10384
10385 if (need_copy)
10386 retval = objfile->intern (retval);
10387
10388 return retval;
10389 }
10390
10391 /* Inspect DIE in CU for a namespace alias. If one exists, record
10392 a new symbol for it.
10393
10394 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10395
10396 static int
10397 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10398 {
10399 struct attribute *attr;
10400
10401 /* If the die does not have a name, this is not a namespace
10402 alias. */
10403 attr = dwarf2_attr (die, DW_AT_name, cu);
10404 if (attr != NULL)
10405 {
10406 int num;
10407 struct die_info *d = die;
10408 struct dwarf2_cu *imported_cu = cu;
10409
10410 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10411 keep inspecting DIEs until we hit the underlying import. */
10412 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10413 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10414 {
10415 attr = dwarf2_attr (d, DW_AT_import, cu);
10416 if (attr == NULL)
10417 break;
10418
10419 d = follow_die_ref (d, attr, &imported_cu);
10420 if (d->tag != DW_TAG_imported_declaration)
10421 break;
10422 }
10423
10424 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10425 {
10426 complaint (_("DIE at %s has too many recursively imported "
10427 "declarations"), sect_offset_str (d->sect_off));
10428 return 0;
10429 }
10430
10431 if (attr != NULL)
10432 {
10433 struct type *type;
10434 sect_offset sect_off = attr->get_ref_die_offset ();
10435
10436 type = get_die_type_at_offset (sect_off, cu->per_cu);
10437 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10438 {
10439 /* This declaration is a global namespace alias. Add
10440 a symbol for it whose type is the aliased namespace. */
10441 new_symbol (die, type, cu);
10442 return 1;
10443 }
10444 }
10445 }
10446
10447 return 0;
10448 }
10449
10450 /* Return the using directives repository (global or local?) to use in the
10451 current context for CU.
10452
10453 For Ada, imported declarations can materialize renamings, which *may* be
10454 global. However it is impossible (for now?) in DWARF to distinguish
10455 "external" imported declarations and "static" ones. As all imported
10456 declarations seem to be static in all other languages, make them all CU-wide
10457 global only in Ada. */
10458
10459 static struct using_direct **
10460 using_directives (struct dwarf2_cu *cu)
10461 {
10462 if (cu->language == language_ada
10463 && cu->get_builder ()->outermost_context_p ())
10464 return cu->get_builder ()->get_global_using_directives ();
10465 else
10466 return cu->get_builder ()->get_local_using_directives ();
10467 }
10468
10469 /* Read the import statement specified by the given die and record it. */
10470
10471 static void
10472 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10473 {
10474 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10475 struct attribute *import_attr;
10476 struct die_info *imported_die, *child_die;
10477 struct dwarf2_cu *imported_cu;
10478 const char *imported_name;
10479 const char *imported_name_prefix;
10480 const char *canonical_name;
10481 const char *import_alias;
10482 const char *imported_declaration = NULL;
10483 const char *import_prefix;
10484 std::vector<const char *> excludes;
10485
10486 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10487 if (import_attr == NULL)
10488 {
10489 complaint (_("Tag '%s' has no DW_AT_import"),
10490 dwarf_tag_name (die->tag));
10491 return;
10492 }
10493
10494 imported_cu = cu;
10495 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10496 imported_name = dwarf2_name (imported_die, imported_cu);
10497 if (imported_name == NULL)
10498 {
10499 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10500
10501 The import in the following code:
10502 namespace A
10503 {
10504 typedef int B;
10505 }
10506
10507 int main ()
10508 {
10509 using A::B;
10510 B b;
10511 return b;
10512 }
10513
10514 ...
10515 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10516 <52> DW_AT_decl_file : 1
10517 <53> DW_AT_decl_line : 6
10518 <54> DW_AT_import : <0x75>
10519 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10520 <59> DW_AT_name : B
10521 <5b> DW_AT_decl_file : 1
10522 <5c> DW_AT_decl_line : 2
10523 <5d> DW_AT_type : <0x6e>
10524 ...
10525 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10526 <76> DW_AT_byte_size : 4
10527 <77> DW_AT_encoding : 5 (signed)
10528
10529 imports the wrong die ( 0x75 instead of 0x58 ).
10530 This case will be ignored until the gcc bug is fixed. */
10531 return;
10532 }
10533
10534 /* Figure out the local name after import. */
10535 import_alias = dwarf2_name (die, cu);
10536
10537 /* Figure out where the statement is being imported to. */
10538 import_prefix = determine_prefix (die, cu);
10539
10540 /* Figure out what the scope of the imported die is and prepend it
10541 to the name of the imported die. */
10542 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10543
10544 if (imported_die->tag != DW_TAG_namespace
10545 && imported_die->tag != DW_TAG_module)
10546 {
10547 imported_declaration = imported_name;
10548 canonical_name = imported_name_prefix;
10549 }
10550 else if (strlen (imported_name_prefix) > 0)
10551 canonical_name = obconcat (&objfile->objfile_obstack,
10552 imported_name_prefix,
10553 (cu->language == language_d ? "." : "::"),
10554 imported_name, (char *) NULL);
10555 else
10556 canonical_name = imported_name;
10557
10558 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10559 for (child_die = die->child; child_die && child_die->tag;
10560 child_die = child_die->sibling)
10561 {
10562 /* DWARF-4: A Fortran use statement with a “rename list” may be
10563 represented by an imported module entry with an import attribute
10564 referring to the module and owned entries corresponding to those
10565 entities that are renamed as part of being imported. */
10566
10567 if (child_die->tag != DW_TAG_imported_declaration)
10568 {
10569 complaint (_("child DW_TAG_imported_declaration expected "
10570 "- DIE at %s [in module %s]"),
10571 sect_offset_str (child_die->sect_off),
10572 objfile_name (objfile));
10573 continue;
10574 }
10575
10576 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10577 if (import_attr == NULL)
10578 {
10579 complaint (_("Tag '%s' has no DW_AT_import"),
10580 dwarf_tag_name (child_die->tag));
10581 continue;
10582 }
10583
10584 imported_cu = cu;
10585 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10586 &imported_cu);
10587 imported_name = dwarf2_name (imported_die, imported_cu);
10588 if (imported_name == NULL)
10589 {
10590 complaint (_("child DW_TAG_imported_declaration has unknown "
10591 "imported name - DIE at %s [in module %s]"),
10592 sect_offset_str (child_die->sect_off),
10593 objfile_name (objfile));
10594 continue;
10595 }
10596
10597 excludes.push_back (imported_name);
10598
10599 process_die (child_die, cu);
10600 }
10601
10602 add_using_directive (using_directives (cu),
10603 import_prefix,
10604 canonical_name,
10605 import_alias,
10606 imported_declaration,
10607 excludes,
10608 0,
10609 &objfile->objfile_obstack);
10610 }
10611
10612 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10613 types, but gives them a size of zero. Starting with version 14,
10614 ICC is compatible with GCC. */
10615
10616 static bool
10617 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10618 {
10619 if (!cu->checked_producer)
10620 check_producer (cu);
10621
10622 return cu->producer_is_icc_lt_14;
10623 }
10624
10625 /* ICC generates a DW_AT_type for C void functions. This was observed on
10626 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10627 which says that void functions should not have a DW_AT_type. */
10628
10629 static bool
10630 producer_is_icc (struct dwarf2_cu *cu)
10631 {
10632 if (!cu->checked_producer)
10633 check_producer (cu);
10634
10635 return cu->producer_is_icc;
10636 }
10637
10638 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10639 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10640 this, it was first present in GCC release 4.3.0. */
10641
10642 static bool
10643 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10644 {
10645 if (!cu->checked_producer)
10646 check_producer (cu);
10647
10648 return cu->producer_is_gcc_lt_4_3;
10649 }
10650
10651 static file_and_directory
10652 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10653 {
10654 file_and_directory res;
10655
10656 /* Find the filename. Do not use dwarf2_name here, since the filename
10657 is not a source language identifier. */
10658 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10659 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10660
10661 if (res.comp_dir == NULL
10662 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10663 && IS_ABSOLUTE_PATH (res.name))
10664 {
10665 res.comp_dir_storage = ldirname (res.name);
10666 if (!res.comp_dir_storage.empty ())
10667 res.comp_dir = res.comp_dir_storage.c_str ();
10668 }
10669 if (res.comp_dir != NULL)
10670 {
10671 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10672 directory, get rid of it. */
10673 const char *cp = strchr (res.comp_dir, ':');
10674
10675 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10676 res.comp_dir = cp + 1;
10677 }
10678
10679 if (res.name == NULL)
10680 res.name = "<unknown>";
10681
10682 return res;
10683 }
10684
10685 /* Handle DW_AT_stmt_list for a compilation unit.
10686 DIE is the DW_TAG_compile_unit die for CU.
10687 COMP_DIR is the compilation directory. LOWPC is passed to
10688 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10689
10690 static void
10691 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10692 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10693 {
10694 struct dwarf2_per_objfile *dwarf2_per_objfile
10695 = cu->per_cu->dwarf2_per_objfile;
10696 struct attribute *attr;
10697 struct line_header line_header_local;
10698 hashval_t line_header_local_hash;
10699 void **slot;
10700 int decode_mapping;
10701
10702 gdb_assert (! cu->per_cu->is_debug_types);
10703
10704 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10705 if (attr == NULL)
10706 return;
10707
10708 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10709
10710 /* The line header hash table is only created if needed (it exists to
10711 prevent redundant reading of the line table for partial_units).
10712 If we're given a partial_unit, we'll need it. If we're given a
10713 compile_unit, then use the line header hash table if it's already
10714 created, but don't create one just yet. */
10715
10716 if (dwarf2_per_objfile->line_header_hash == NULL
10717 && die->tag == DW_TAG_partial_unit)
10718 {
10719 dwarf2_per_objfile->line_header_hash
10720 .reset (htab_create_alloc (127, line_header_hash_voidp,
10721 line_header_eq_voidp,
10722 free_line_header_voidp,
10723 xcalloc, xfree));
10724 }
10725
10726 line_header_local.sect_off = line_offset;
10727 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10728 line_header_local_hash = line_header_hash (&line_header_local);
10729 if (dwarf2_per_objfile->line_header_hash != NULL)
10730 {
10731 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10732 &line_header_local,
10733 line_header_local_hash, NO_INSERT);
10734
10735 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10736 is not present in *SLOT (since if there is something in *SLOT then
10737 it will be for a partial_unit). */
10738 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10739 {
10740 gdb_assert (*slot != NULL);
10741 cu->line_header = (struct line_header *) *slot;
10742 return;
10743 }
10744 }
10745
10746 /* dwarf_decode_line_header does not yet provide sufficient information.
10747 We always have to call also dwarf_decode_lines for it. */
10748 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10749 if (lh == NULL)
10750 return;
10751
10752 cu->line_header = lh.release ();
10753 cu->line_header_die_owner = die;
10754
10755 if (dwarf2_per_objfile->line_header_hash == NULL)
10756 slot = NULL;
10757 else
10758 {
10759 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10760 &line_header_local,
10761 line_header_local_hash, INSERT);
10762 gdb_assert (slot != NULL);
10763 }
10764 if (slot != NULL && *slot == NULL)
10765 {
10766 /* This newly decoded line number information unit will be owned
10767 by line_header_hash hash table. */
10768 *slot = cu->line_header;
10769 cu->line_header_die_owner = NULL;
10770 }
10771 else
10772 {
10773 /* We cannot free any current entry in (*slot) as that struct line_header
10774 may be already used by multiple CUs. Create only temporary decoded
10775 line_header for this CU - it may happen at most once for each line
10776 number information unit. And if we're not using line_header_hash
10777 then this is what we want as well. */
10778 gdb_assert (die->tag != DW_TAG_partial_unit);
10779 }
10780 decode_mapping = (die->tag != DW_TAG_partial_unit);
10781 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10782 decode_mapping);
10783
10784 }
10785
10786 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10787
10788 static void
10789 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10790 {
10791 struct dwarf2_per_objfile *dwarf2_per_objfile
10792 = cu->per_cu->dwarf2_per_objfile;
10793 struct objfile *objfile = dwarf2_per_objfile->objfile;
10794 struct gdbarch *gdbarch = objfile->arch ();
10795 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10796 CORE_ADDR highpc = ((CORE_ADDR) 0);
10797 struct attribute *attr;
10798 struct die_info *child_die;
10799 CORE_ADDR baseaddr;
10800
10801 prepare_one_comp_unit (cu, die, cu->language);
10802 baseaddr = objfile->text_section_offset ();
10803
10804 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10805
10806 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10807 from finish_block. */
10808 if (lowpc == ((CORE_ADDR) -1))
10809 lowpc = highpc;
10810 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10811
10812 file_and_directory fnd = find_file_and_directory (die, cu);
10813
10814 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10815 standardised yet. As a workaround for the language detection we fall
10816 back to the DW_AT_producer string. */
10817 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10818 cu->language = language_opencl;
10819
10820 /* Similar hack for Go. */
10821 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10822 set_cu_language (DW_LANG_Go, cu);
10823
10824 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10825
10826 /* Decode line number information if present. We do this before
10827 processing child DIEs, so that the line header table is available
10828 for DW_AT_decl_file. */
10829 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10830
10831 /* Process all dies in compilation unit. */
10832 if (die->child != NULL)
10833 {
10834 child_die = die->child;
10835 while (child_die && child_die->tag)
10836 {
10837 process_die (child_die, cu);
10838 child_die = child_die->sibling;
10839 }
10840 }
10841
10842 /* Decode macro information, if present. Dwarf 2 macro information
10843 refers to information in the line number info statement program
10844 header, so we can only read it if we've read the header
10845 successfully. */
10846 attr = dwarf2_attr (die, DW_AT_macros, cu);
10847 if (attr == NULL)
10848 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10849 if (attr && cu->line_header)
10850 {
10851 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10852 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10853
10854 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10855 }
10856 else
10857 {
10858 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10859 if (attr && cu->line_header)
10860 {
10861 unsigned int macro_offset = DW_UNSND (attr);
10862
10863 dwarf_decode_macros (cu, macro_offset, 0);
10864 }
10865 }
10866 }
10867
10868 void
10869 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10870 {
10871 struct type_unit_group *tu_group;
10872 int first_time;
10873 struct attribute *attr;
10874 unsigned int i;
10875 struct signatured_type *sig_type;
10876
10877 gdb_assert (per_cu->is_debug_types);
10878 sig_type = (struct signatured_type *) per_cu;
10879
10880 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10881
10882 /* If we're using .gdb_index (includes -readnow) then
10883 per_cu->type_unit_group may not have been set up yet. */
10884 if (sig_type->type_unit_group == NULL)
10885 sig_type->type_unit_group = get_type_unit_group (this, attr);
10886 tu_group = sig_type->type_unit_group;
10887
10888 /* If we've already processed this stmt_list there's no real need to
10889 do it again, we could fake it and just recreate the part we need
10890 (file name,index -> symtab mapping). If data shows this optimization
10891 is useful we can do it then. */
10892 first_time = tu_group->compunit_symtab == NULL;
10893
10894 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10895 debug info. */
10896 line_header_up lh;
10897 if (attr != NULL)
10898 {
10899 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10900 lh = dwarf_decode_line_header (line_offset, this);
10901 }
10902 if (lh == NULL)
10903 {
10904 if (first_time)
10905 start_symtab ("", NULL, 0);
10906 else
10907 {
10908 gdb_assert (tu_group->symtabs == NULL);
10909 gdb_assert (m_builder == nullptr);
10910 struct compunit_symtab *cust = tu_group->compunit_symtab;
10911 m_builder.reset (new struct buildsym_compunit
10912 (COMPUNIT_OBJFILE (cust), "",
10913 COMPUNIT_DIRNAME (cust),
10914 compunit_language (cust),
10915 0, cust));
10916 }
10917 return;
10918 }
10919
10920 line_header = lh.release ();
10921 line_header_die_owner = die;
10922
10923 if (first_time)
10924 {
10925 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10926
10927 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10928 still initializing it, and our caller (a few levels up)
10929 process_full_type_unit still needs to know if this is the first
10930 time. */
10931
10932 tu_group->symtabs
10933 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10934 struct symtab *, line_header->file_names_size ());
10935
10936 auto &file_names = line_header->file_names ();
10937 for (i = 0; i < file_names.size (); ++i)
10938 {
10939 file_entry &fe = file_names[i];
10940 dwarf2_start_subfile (this, fe.name,
10941 fe.include_dir (line_header));
10942 buildsym_compunit *b = get_builder ();
10943 if (b->get_current_subfile ()->symtab == NULL)
10944 {
10945 /* NOTE: start_subfile will recognize when it's been
10946 passed a file it has already seen. So we can't
10947 assume there's a simple mapping from
10948 cu->line_header->file_names to subfiles, plus
10949 cu->line_header->file_names may contain dups. */
10950 b->get_current_subfile ()->symtab
10951 = allocate_symtab (cust, b->get_current_subfile ()->name);
10952 }
10953
10954 fe.symtab = b->get_current_subfile ()->symtab;
10955 tu_group->symtabs[i] = fe.symtab;
10956 }
10957 }
10958 else
10959 {
10960 gdb_assert (m_builder == nullptr);
10961 struct compunit_symtab *cust = tu_group->compunit_symtab;
10962 m_builder.reset (new struct buildsym_compunit
10963 (COMPUNIT_OBJFILE (cust), "",
10964 COMPUNIT_DIRNAME (cust),
10965 compunit_language (cust),
10966 0, cust));
10967
10968 auto &file_names = line_header->file_names ();
10969 for (i = 0; i < file_names.size (); ++i)
10970 {
10971 file_entry &fe = file_names[i];
10972 fe.symtab = tu_group->symtabs[i];
10973 }
10974 }
10975
10976 /* The main symtab is allocated last. Type units don't have DW_AT_name
10977 so they don't have a "real" (so to speak) symtab anyway.
10978 There is later code that will assign the main symtab to all symbols
10979 that don't have one. We need to handle the case of a symbol with a
10980 missing symtab (DW_AT_decl_file) anyway. */
10981 }
10982
10983 /* Process DW_TAG_type_unit.
10984 For TUs we want to skip the first top level sibling if it's not the
10985 actual type being defined by this TU. In this case the first top
10986 level sibling is there to provide context only. */
10987
10988 static void
10989 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10990 {
10991 struct die_info *child_die;
10992
10993 prepare_one_comp_unit (cu, die, language_minimal);
10994
10995 /* Initialize (or reinitialize) the machinery for building symtabs.
10996 We do this before processing child DIEs, so that the line header table
10997 is available for DW_AT_decl_file. */
10998 cu->setup_type_unit_groups (die);
10999
11000 if (die->child != NULL)
11001 {
11002 child_die = die->child;
11003 while (child_die && child_die->tag)
11004 {
11005 process_die (child_die, cu);
11006 child_die = child_die->sibling;
11007 }
11008 }
11009 }
11010 \f
11011 /* DWO/DWP files.
11012
11013 http://gcc.gnu.org/wiki/DebugFission
11014 http://gcc.gnu.org/wiki/DebugFissionDWP
11015
11016 To simplify handling of both DWO files ("object" files with the DWARF info)
11017 and DWP files (a file with the DWOs packaged up into one file), we treat
11018 DWP files as having a collection of virtual DWO files. */
11019
11020 static hashval_t
11021 hash_dwo_file (const void *item)
11022 {
11023 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11024 hashval_t hash;
11025
11026 hash = htab_hash_string (dwo_file->dwo_name);
11027 if (dwo_file->comp_dir != NULL)
11028 hash += htab_hash_string (dwo_file->comp_dir);
11029 return hash;
11030 }
11031
11032 static int
11033 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11034 {
11035 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11036 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11037
11038 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11039 return 0;
11040 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11041 return lhs->comp_dir == rhs->comp_dir;
11042 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11043 }
11044
11045 /* Allocate a hash table for DWO files. */
11046
11047 static htab_up
11048 allocate_dwo_file_hash_table ()
11049 {
11050 auto delete_dwo_file = [] (void *item)
11051 {
11052 struct dwo_file *dwo_file = (struct dwo_file *) item;
11053
11054 delete dwo_file;
11055 };
11056
11057 return htab_up (htab_create_alloc (41,
11058 hash_dwo_file,
11059 eq_dwo_file,
11060 delete_dwo_file,
11061 xcalloc, xfree));
11062 }
11063
11064 /* Lookup DWO file DWO_NAME. */
11065
11066 static void **
11067 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11068 const char *dwo_name,
11069 const char *comp_dir)
11070 {
11071 struct dwo_file find_entry;
11072 void **slot;
11073
11074 if (dwarf2_per_objfile->dwo_files == NULL)
11075 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
11076
11077 find_entry.dwo_name = dwo_name;
11078 find_entry.comp_dir = comp_dir;
11079 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11080 INSERT);
11081
11082 return slot;
11083 }
11084
11085 static hashval_t
11086 hash_dwo_unit (const void *item)
11087 {
11088 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11089
11090 /* This drops the top 32 bits of the id, but is ok for a hash. */
11091 return dwo_unit->signature;
11092 }
11093
11094 static int
11095 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11096 {
11097 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11098 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11099
11100 /* The signature is assumed to be unique within the DWO file.
11101 So while object file CU dwo_id's always have the value zero,
11102 that's OK, assuming each object file DWO file has only one CU,
11103 and that's the rule for now. */
11104 return lhs->signature == rhs->signature;
11105 }
11106
11107 /* Allocate a hash table for DWO CUs,TUs.
11108 There is one of these tables for each of CUs,TUs for each DWO file. */
11109
11110 static htab_up
11111 allocate_dwo_unit_table ()
11112 {
11113 /* Start out with a pretty small number.
11114 Generally DWO files contain only one CU and maybe some TUs. */
11115 return htab_up (htab_create_alloc (3,
11116 hash_dwo_unit,
11117 eq_dwo_unit,
11118 NULL, xcalloc, xfree));
11119 }
11120
11121 /* die_reader_func for create_dwo_cu. */
11122
11123 static void
11124 create_dwo_cu_reader (const struct die_reader_specs *reader,
11125 const gdb_byte *info_ptr,
11126 struct die_info *comp_unit_die,
11127 struct dwo_file *dwo_file,
11128 struct dwo_unit *dwo_unit)
11129 {
11130 struct dwarf2_cu *cu = reader->cu;
11131 sect_offset sect_off = cu->per_cu->sect_off;
11132 struct dwarf2_section_info *section = cu->per_cu->section;
11133
11134 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11135 if (!signature.has_value ())
11136 {
11137 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11138 " its dwo_id [in module %s]"),
11139 sect_offset_str (sect_off), dwo_file->dwo_name);
11140 return;
11141 }
11142
11143 dwo_unit->dwo_file = dwo_file;
11144 dwo_unit->signature = *signature;
11145 dwo_unit->section = section;
11146 dwo_unit->sect_off = sect_off;
11147 dwo_unit->length = cu->per_cu->length;
11148
11149 if (dwarf_read_debug)
11150 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11151 sect_offset_str (sect_off),
11152 hex_string (dwo_unit->signature));
11153 }
11154
11155 /* Create the dwo_units for the CUs in a DWO_FILE.
11156 Note: This function processes DWO files only, not DWP files. */
11157
11158 static void
11159 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11160 dwarf2_cu *cu, struct dwo_file &dwo_file,
11161 dwarf2_section_info &section, htab_up &cus_htab)
11162 {
11163 struct objfile *objfile = dwarf2_per_objfile->objfile;
11164 const gdb_byte *info_ptr, *end_ptr;
11165
11166 section.read (objfile);
11167 info_ptr = section.buffer;
11168
11169 if (info_ptr == NULL)
11170 return;
11171
11172 if (dwarf_read_debug)
11173 {
11174 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11175 section.get_name (),
11176 section.get_file_name ());
11177 }
11178
11179 end_ptr = info_ptr + section.size;
11180 while (info_ptr < end_ptr)
11181 {
11182 struct dwarf2_per_cu_data per_cu;
11183 struct dwo_unit read_unit {};
11184 struct dwo_unit *dwo_unit;
11185 void **slot;
11186 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11187
11188 memset (&per_cu, 0, sizeof (per_cu));
11189 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11190 per_cu.is_debug_types = 0;
11191 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11192 per_cu.section = &section;
11193
11194 cutu_reader reader (&per_cu, cu, &dwo_file);
11195 if (!reader.dummy_p)
11196 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11197 &dwo_file, &read_unit);
11198 info_ptr += per_cu.length;
11199
11200 // If the unit could not be parsed, skip it.
11201 if (read_unit.dwo_file == NULL)
11202 continue;
11203
11204 if (cus_htab == NULL)
11205 cus_htab = allocate_dwo_unit_table ();
11206
11207 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11208 *dwo_unit = read_unit;
11209 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11210 gdb_assert (slot != NULL);
11211 if (*slot != NULL)
11212 {
11213 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11214 sect_offset dup_sect_off = dup_cu->sect_off;
11215
11216 complaint (_("debug cu entry at offset %s is duplicate to"
11217 " the entry at offset %s, signature %s"),
11218 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11219 hex_string (dwo_unit->signature));
11220 }
11221 *slot = (void *)dwo_unit;
11222 }
11223 }
11224
11225 /* DWP file .debug_{cu,tu}_index section format:
11226 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11227
11228 DWP Version 1:
11229
11230 Both index sections have the same format, and serve to map a 64-bit
11231 signature to a set of section numbers. Each section begins with a header,
11232 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11233 indexes, and a pool of 32-bit section numbers. The index sections will be
11234 aligned at 8-byte boundaries in the file.
11235
11236 The index section header consists of:
11237
11238 V, 32 bit version number
11239 -, 32 bits unused
11240 N, 32 bit number of compilation units or type units in the index
11241 M, 32 bit number of slots in the hash table
11242
11243 Numbers are recorded using the byte order of the application binary.
11244
11245 The hash table begins at offset 16 in the section, and consists of an array
11246 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11247 order of the application binary). Unused slots in the hash table are 0.
11248 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11249
11250 The parallel table begins immediately after the hash table
11251 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11252 array of 32-bit indexes (using the byte order of the application binary),
11253 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11254 table contains a 32-bit index into the pool of section numbers. For unused
11255 hash table slots, the corresponding entry in the parallel table will be 0.
11256
11257 The pool of section numbers begins immediately following the hash table
11258 (at offset 16 + 12 * M from the beginning of the section). The pool of
11259 section numbers consists of an array of 32-bit words (using the byte order
11260 of the application binary). Each item in the array is indexed starting
11261 from 0. The hash table entry provides the index of the first section
11262 number in the set. Additional section numbers in the set follow, and the
11263 set is terminated by a 0 entry (section number 0 is not used in ELF).
11264
11265 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11266 section must be the first entry in the set, and the .debug_abbrev.dwo must
11267 be the second entry. Other members of the set may follow in any order.
11268
11269 ---
11270
11271 DWP Version 2:
11272
11273 DWP Version 2 combines all the .debug_info, etc. sections into one,
11274 and the entries in the index tables are now offsets into these sections.
11275 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11276 section.
11277
11278 Index Section Contents:
11279 Header
11280 Hash Table of Signatures dwp_hash_table.hash_table
11281 Parallel Table of Indices dwp_hash_table.unit_table
11282 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11283 Table of Section Sizes dwp_hash_table.v2.sizes
11284
11285 The index section header consists of:
11286
11287 V, 32 bit version number
11288 L, 32 bit number of columns in the table of section offsets
11289 N, 32 bit number of compilation units or type units in the index
11290 M, 32 bit number of slots in the hash table
11291
11292 Numbers are recorded using the byte order of the application binary.
11293
11294 The hash table has the same format as version 1.
11295 The parallel table of indices has the same format as version 1,
11296 except that the entries are origin-1 indices into the table of sections
11297 offsets and the table of section sizes.
11298
11299 The table of offsets begins immediately following the parallel table
11300 (at offset 16 + 12 * M from the beginning of the section). The table is
11301 a two-dimensional array of 32-bit words (using the byte order of the
11302 application binary), with L columns and N+1 rows, in row-major order.
11303 Each row in the array is indexed starting from 0. The first row provides
11304 a key to the remaining rows: each column in this row provides an identifier
11305 for a debug section, and the offsets in the same column of subsequent rows
11306 refer to that section. The section identifiers are:
11307
11308 DW_SECT_INFO 1 .debug_info.dwo
11309 DW_SECT_TYPES 2 .debug_types.dwo
11310 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11311 DW_SECT_LINE 4 .debug_line.dwo
11312 DW_SECT_LOC 5 .debug_loc.dwo
11313 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11314 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11315 DW_SECT_MACRO 8 .debug_macro.dwo
11316
11317 The offsets provided by the CU and TU index sections are the base offsets
11318 for the contributions made by each CU or TU to the corresponding section
11319 in the package file. Each CU and TU header contains an abbrev_offset
11320 field, used to find the abbreviations table for that CU or TU within the
11321 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11322 be interpreted as relative to the base offset given in the index section.
11323 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11324 should be interpreted as relative to the base offset for .debug_line.dwo,
11325 and offsets into other debug sections obtained from DWARF attributes should
11326 also be interpreted as relative to the corresponding base offset.
11327
11328 The table of sizes begins immediately following the table of offsets.
11329 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11330 with L columns and N rows, in row-major order. Each row in the array is
11331 indexed starting from 1 (row 0 is shared by the two tables).
11332
11333 ---
11334
11335 Hash table lookup is handled the same in version 1 and 2:
11336
11337 We assume that N and M will not exceed 2^32 - 1.
11338 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11339
11340 Given a 64-bit compilation unit signature or a type signature S, an entry
11341 in the hash table is located as follows:
11342
11343 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11344 the low-order k bits all set to 1.
11345
11346 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11347
11348 3) If the hash table entry at index H matches the signature, use that
11349 entry. If the hash table entry at index H is unused (all zeroes),
11350 terminate the search: the signature is not present in the table.
11351
11352 4) Let H = (H + H') modulo M. Repeat at Step 3.
11353
11354 Because M > N and H' and M are relatively prime, the search is guaranteed
11355 to stop at an unused slot or find the match. */
11356
11357 /* Create a hash table to map DWO IDs to their CU/TU entry in
11358 .debug_{info,types}.dwo in DWP_FILE.
11359 Returns NULL if there isn't one.
11360 Note: This function processes DWP files only, not DWO files. */
11361
11362 static struct dwp_hash_table *
11363 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11364 struct dwp_file *dwp_file, int is_debug_types)
11365 {
11366 struct objfile *objfile = dwarf2_per_objfile->objfile;
11367 bfd *dbfd = dwp_file->dbfd.get ();
11368 const gdb_byte *index_ptr, *index_end;
11369 struct dwarf2_section_info *index;
11370 uint32_t version, nr_columns, nr_units, nr_slots;
11371 struct dwp_hash_table *htab;
11372
11373 if (is_debug_types)
11374 index = &dwp_file->sections.tu_index;
11375 else
11376 index = &dwp_file->sections.cu_index;
11377
11378 if (index->empty ())
11379 return NULL;
11380 index->read (objfile);
11381
11382 index_ptr = index->buffer;
11383 index_end = index_ptr + index->size;
11384
11385 version = read_4_bytes (dbfd, index_ptr);
11386 index_ptr += 4;
11387 if (version == 2)
11388 nr_columns = read_4_bytes (dbfd, index_ptr);
11389 else
11390 nr_columns = 0;
11391 index_ptr += 4;
11392 nr_units = read_4_bytes (dbfd, index_ptr);
11393 index_ptr += 4;
11394 nr_slots = read_4_bytes (dbfd, index_ptr);
11395 index_ptr += 4;
11396
11397 if (version != 1 && version != 2)
11398 {
11399 error (_("Dwarf Error: unsupported DWP file version (%s)"
11400 " [in module %s]"),
11401 pulongest (version), dwp_file->name);
11402 }
11403 if (nr_slots != (nr_slots & -nr_slots))
11404 {
11405 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11406 " is not power of 2 [in module %s]"),
11407 pulongest (nr_slots), dwp_file->name);
11408 }
11409
11410 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11411 htab->version = version;
11412 htab->nr_columns = nr_columns;
11413 htab->nr_units = nr_units;
11414 htab->nr_slots = nr_slots;
11415 htab->hash_table = index_ptr;
11416 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11417
11418 /* Exit early if the table is empty. */
11419 if (nr_slots == 0 || nr_units == 0
11420 || (version == 2 && nr_columns == 0))
11421 {
11422 /* All must be zero. */
11423 if (nr_slots != 0 || nr_units != 0
11424 || (version == 2 && nr_columns != 0))
11425 {
11426 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11427 " all zero [in modules %s]"),
11428 dwp_file->name);
11429 }
11430 return htab;
11431 }
11432
11433 if (version == 1)
11434 {
11435 htab->section_pool.v1.indices =
11436 htab->unit_table + sizeof (uint32_t) * nr_slots;
11437 /* It's harder to decide whether the section is too small in v1.
11438 V1 is deprecated anyway so we punt. */
11439 }
11440 else
11441 {
11442 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11443 int *ids = htab->section_pool.v2.section_ids;
11444 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11445 /* Reverse map for error checking. */
11446 int ids_seen[DW_SECT_MAX + 1];
11447 int i;
11448
11449 if (nr_columns < 2)
11450 {
11451 error (_("Dwarf Error: bad DWP hash table, too few columns"
11452 " in section table [in module %s]"),
11453 dwp_file->name);
11454 }
11455 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11456 {
11457 error (_("Dwarf Error: bad DWP hash table, too many columns"
11458 " in section table [in module %s]"),
11459 dwp_file->name);
11460 }
11461 memset (ids, 255, sizeof_ids);
11462 memset (ids_seen, 255, sizeof (ids_seen));
11463 for (i = 0; i < nr_columns; ++i)
11464 {
11465 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11466
11467 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11468 {
11469 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11470 " in section table [in module %s]"),
11471 id, dwp_file->name);
11472 }
11473 if (ids_seen[id] != -1)
11474 {
11475 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11476 " id %d in section table [in module %s]"),
11477 id, dwp_file->name);
11478 }
11479 ids_seen[id] = i;
11480 ids[i] = id;
11481 }
11482 /* Must have exactly one info or types section. */
11483 if (((ids_seen[DW_SECT_INFO] != -1)
11484 + (ids_seen[DW_SECT_TYPES] != -1))
11485 != 1)
11486 {
11487 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11488 " DWO info/types section [in module %s]"),
11489 dwp_file->name);
11490 }
11491 /* Must have an abbrev section. */
11492 if (ids_seen[DW_SECT_ABBREV] == -1)
11493 {
11494 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11495 " section [in module %s]"),
11496 dwp_file->name);
11497 }
11498 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11499 htab->section_pool.v2.sizes =
11500 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11501 * nr_units * nr_columns);
11502 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11503 * nr_units * nr_columns))
11504 > index_end)
11505 {
11506 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11507 " [in module %s]"),
11508 dwp_file->name);
11509 }
11510 }
11511
11512 return htab;
11513 }
11514
11515 /* Update SECTIONS with the data from SECTP.
11516
11517 This function is like the other "locate" section routines that are
11518 passed to bfd_map_over_sections, but in this context the sections to
11519 read comes from the DWP V1 hash table, not the full ELF section table.
11520
11521 The result is non-zero for success, or zero if an error was found. */
11522
11523 static int
11524 locate_v1_virtual_dwo_sections (asection *sectp,
11525 struct virtual_v1_dwo_sections *sections)
11526 {
11527 const struct dwop_section_names *names = &dwop_section_names;
11528
11529 if (section_is_p (sectp->name, &names->abbrev_dwo))
11530 {
11531 /* There can be only one. */
11532 if (sections->abbrev.s.section != NULL)
11533 return 0;
11534 sections->abbrev.s.section = sectp;
11535 sections->abbrev.size = bfd_section_size (sectp);
11536 }
11537 else if (section_is_p (sectp->name, &names->info_dwo)
11538 || section_is_p (sectp->name, &names->types_dwo))
11539 {
11540 /* There can be only one. */
11541 if (sections->info_or_types.s.section != NULL)
11542 return 0;
11543 sections->info_or_types.s.section = sectp;
11544 sections->info_or_types.size = bfd_section_size (sectp);
11545 }
11546 else if (section_is_p (sectp->name, &names->line_dwo))
11547 {
11548 /* There can be only one. */
11549 if (sections->line.s.section != NULL)
11550 return 0;
11551 sections->line.s.section = sectp;
11552 sections->line.size = bfd_section_size (sectp);
11553 }
11554 else if (section_is_p (sectp->name, &names->loc_dwo))
11555 {
11556 /* There can be only one. */
11557 if (sections->loc.s.section != NULL)
11558 return 0;
11559 sections->loc.s.section = sectp;
11560 sections->loc.size = bfd_section_size (sectp);
11561 }
11562 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11563 {
11564 /* There can be only one. */
11565 if (sections->macinfo.s.section != NULL)
11566 return 0;
11567 sections->macinfo.s.section = sectp;
11568 sections->macinfo.size = bfd_section_size (sectp);
11569 }
11570 else if (section_is_p (sectp->name, &names->macro_dwo))
11571 {
11572 /* There can be only one. */
11573 if (sections->macro.s.section != NULL)
11574 return 0;
11575 sections->macro.s.section = sectp;
11576 sections->macro.size = bfd_section_size (sectp);
11577 }
11578 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11579 {
11580 /* There can be only one. */
11581 if (sections->str_offsets.s.section != NULL)
11582 return 0;
11583 sections->str_offsets.s.section = sectp;
11584 sections->str_offsets.size = bfd_section_size (sectp);
11585 }
11586 else
11587 {
11588 /* No other kind of section is valid. */
11589 return 0;
11590 }
11591
11592 return 1;
11593 }
11594
11595 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11596 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11597 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11598 This is for DWP version 1 files. */
11599
11600 static struct dwo_unit *
11601 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11602 struct dwp_file *dwp_file,
11603 uint32_t unit_index,
11604 const char *comp_dir,
11605 ULONGEST signature, int is_debug_types)
11606 {
11607 struct objfile *objfile = dwarf2_per_objfile->objfile;
11608 const struct dwp_hash_table *dwp_htab =
11609 is_debug_types ? dwp_file->tus : dwp_file->cus;
11610 bfd *dbfd = dwp_file->dbfd.get ();
11611 const char *kind = is_debug_types ? "TU" : "CU";
11612 struct dwo_file *dwo_file;
11613 struct dwo_unit *dwo_unit;
11614 struct virtual_v1_dwo_sections sections;
11615 void **dwo_file_slot;
11616 int i;
11617
11618 gdb_assert (dwp_file->version == 1);
11619
11620 if (dwarf_read_debug)
11621 {
11622 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11623 kind,
11624 pulongest (unit_index), hex_string (signature),
11625 dwp_file->name);
11626 }
11627
11628 /* Fetch the sections of this DWO unit.
11629 Put a limit on the number of sections we look for so that bad data
11630 doesn't cause us to loop forever. */
11631
11632 #define MAX_NR_V1_DWO_SECTIONS \
11633 (1 /* .debug_info or .debug_types */ \
11634 + 1 /* .debug_abbrev */ \
11635 + 1 /* .debug_line */ \
11636 + 1 /* .debug_loc */ \
11637 + 1 /* .debug_str_offsets */ \
11638 + 1 /* .debug_macro or .debug_macinfo */ \
11639 + 1 /* trailing zero */)
11640
11641 memset (&sections, 0, sizeof (sections));
11642
11643 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11644 {
11645 asection *sectp;
11646 uint32_t section_nr =
11647 read_4_bytes (dbfd,
11648 dwp_htab->section_pool.v1.indices
11649 + (unit_index + i) * sizeof (uint32_t));
11650
11651 if (section_nr == 0)
11652 break;
11653 if (section_nr >= dwp_file->num_sections)
11654 {
11655 error (_("Dwarf Error: bad DWP hash table, section number too large"
11656 " [in module %s]"),
11657 dwp_file->name);
11658 }
11659
11660 sectp = dwp_file->elf_sections[section_nr];
11661 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11662 {
11663 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11664 " [in module %s]"),
11665 dwp_file->name);
11666 }
11667 }
11668
11669 if (i < 2
11670 || sections.info_or_types.empty ()
11671 || sections.abbrev.empty ())
11672 {
11673 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11674 " [in module %s]"),
11675 dwp_file->name);
11676 }
11677 if (i == MAX_NR_V1_DWO_SECTIONS)
11678 {
11679 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11680 " [in module %s]"),
11681 dwp_file->name);
11682 }
11683
11684 /* It's easier for the rest of the code if we fake a struct dwo_file and
11685 have dwo_unit "live" in that. At least for now.
11686
11687 The DWP file can be made up of a random collection of CUs and TUs.
11688 However, for each CU + set of TUs that came from the same original DWO
11689 file, we can combine them back into a virtual DWO file to save space
11690 (fewer struct dwo_file objects to allocate). Remember that for really
11691 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11692
11693 std::string virtual_dwo_name =
11694 string_printf ("virtual-dwo/%d-%d-%d-%d",
11695 sections.abbrev.get_id (),
11696 sections.line.get_id (),
11697 sections.loc.get_id (),
11698 sections.str_offsets.get_id ());
11699 /* Can we use an existing virtual DWO file? */
11700 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11701 virtual_dwo_name.c_str (),
11702 comp_dir);
11703 /* Create one if necessary. */
11704 if (*dwo_file_slot == NULL)
11705 {
11706 if (dwarf_read_debug)
11707 {
11708 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11709 virtual_dwo_name.c_str ());
11710 }
11711 dwo_file = new struct dwo_file;
11712 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11713 dwo_file->comp_dir = comp_dir;
11714 dwo_file->sections.abbrev = sections.abbrev;
11715 dwo_file->sections.line = sections.line;
11716 dwo_file->sections.loc = sections.loc;
11717 dwo_file->sections.macinfo = sections.macinfo;
11718 dwo_file->sections.macro = sections.macro;
11719 dwo_file->sections.str_offsets = sections.str_offsets;
11720 /* The "str" section is global to the entire DWP file. */
11721 dwo_file->sections.str = dwp_file->sections.str;
11722 /* The info or types section is assigned below to dwo_unit,
11723 there's no need to record it in dwo_file.
11724 Also, we can't simply record type sections in dwo_file because
11725 we record a pointer into the vector in dwo_unit. As we collect more
11726 types we'll grow the vector and eventually have to reallocate space
11727 for it, invalidating all copies of pointers into the previous
11728 contents. */
11729 *dwo_file_slot = dwo_file;
11730 }
11731 else
11732 {
11733 if (dwarf_read_debug)
11734 {
11735 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11736 virtual_dwo_name.c_str ());
11737 }
11738 dwo_file = (struct dwo_file *) *dwo_file_slot;
11739 }
11740
11741 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11742 dwo_unit->dwo_file = dwo_file;
11743 dwo_unit->signature = signature;
11744 dwo_unit->section =
11745 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11746 *dwo_unit->section = sections.info_or_types;
11747 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11748
11749 return dwo_unit;
11750 }
11751
11752 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11753 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11754 piece within that section used by a TU/CU, return a virtual section
11755 of just that piece. */
11756
11757 static struct dwarf2_section_info
11758 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11759 struct dwarf2_section_info *section,
11760 bfd_size_type offset, bfd_size_type size)
11761 {
11762 struct dwarf2_section_info result;
11763 asection *sectp;
11764
11765 gdb_assert (section != NULL);
11766 gdb_assert (!section->is_virtual);
11767
11768 memset (&result, 0, sizeof (result));
11769 result.s.containing_section = section;
11770 result.is_virtual = true;
11771
11772 if (size == 0)
11773 return result;
11774
11775 sectp = section->get_bfd_section ();
11776
11777 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11778 bounds of the real section. This is a pretty-rare event, so just
11779 flag an error (easier) instead of a warning and trying to cope. */
11780 if (sectp == NULL
11781 || offset + size > bfd_section_size (sectp))
11782 {
11783 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11784 " in section %s [in module %s]"),
11785 sectp ? bfd_section_name (sectp) : "<unknown>",
11786 objfile_name (dwarf2_per_objfile->objfile));
11787 }
11788
11789 result.virtual_offset = offset;
11790 result.size = size;
11791 return result;
11792 }
11793
11794 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11795 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11796 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11797 This is for DWP version 2 files. */
11798
11799 static struct dwo_unit *
11800 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11801 struct dwp_file *dwp_file,
11802 uint32_t unit_index,
11803 const char *comp_dir,
11804 ULONGEST signature, int is_debug_types)
11805 {
11806 struct objfile *objfile = dwarf2_per_objfile->objfile;
11807 const struct dwp_hash_table *dwp_htab =
11808 is_debug_types ? dwp_file->tus : dwp_file->cus;
11809 bfd *dbfd = dwp_file->dbfd.get ();
11810 const char *kind = is_debug_types ? "TU" : "CU";
11811 struct dwo_file *dwo_file;
11812 struct dwo_unit *dwo_unit;
11813 struct virtual_v2_dwo_sections sections;
11814 void **dwo_file_slot;
11815 int i;
11816
11817 gdb_assert (dwp_file->version == 2);
11818
11819 if (dwarf_read_debug)
11820 {
11821 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11822 kind,
11823 pulongest (unit_index), hex_string (signature),
11824 dwp_file->name);
11825 }
11826
11827 /* Fetch the section offsets of this DWO unit. */
11828
11829 memset (&sections, 0, sizeof (sections));
11830
11831 for (i = 0; i < dwp_htab->nr_columns; ++i)
11832 {
11833 uint32_t offset = read_4_bytes (dbfd,
11834 dwp_htab->section_pool.v2.offsets
11835 + (((unit_index - 1) * dwp_htab->nr_columns
11836 + i)
11837 * sizeof (uint32_t)));
11838 uint32_t size = read_4_bytes (dbfd,
11839 dwp_htab->section_pool.v2.sizes
11840 + (((unit_index - 1) * dwp_htab->nr_columns
11841 + i)
11842 * sizeof (uint32_t)));
11843
11844 switch (dwp_htab->section_pool.v2.section_ids[i])
11845 {
11846 case DW_SECT_INFO:
11847 case DW_SECT_TYPES:
11848 sections.info_or_types_offset = offset;
11849 sections.info_or_types_size = size;
11850 break;
11851 case DW_SECT_ABBREV:
11852 sections.abbrev_offset = offset;
11853 sections.abbrev_size = size;
11854 break;
11855 case DW_SECT_LINE:
11856 sections.line_offset = offset;
11857 sections.line_size = size;
11858 break;
11859 case DW_SECT_LOC:
11860 sections.loc_offset = offset;
11861 sections.loc_size = size;
11862 break;
11863 case DW_SECT_STR_OFFSETS:
11864 sections.str_offsets_offset = offset;
11865 sections.str_offsets_size = size;
11866 break;
11867 case DW_SECT_MACINFO:
11868 sections.macinfo_offset = offset;
11869 sections.macinfo_size = size;
11870 break;
11871 case DW_SECT_MACRO:
11872 sections.macro_offset = offset;
11873 sections.macro_size = size;
11874 break;
11875 }
11876 }
11877
11878 /* It's easier for the rest of the code if we fake a struct dwo_file and
11879 have dwo_unit "live" in that. At least for now.
11880
11881 The DWP file can be made up of a random collection of CUs and TUs.
11882 However, for each CU + set of TUs that came from the same original DWO
11883 file, we can combine them back into a virtual DWO file to save space
11884 (fewer struct dwo_file objects to allocate). Remember that for really
11885 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11886
11887 std::string virtual_dwo_name =
11888 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11889 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11890 (long) (sections.line_size ? sections.line_offset : 0),
11891 (long) (sections.loc_size ? sections.loc_offset : 0),
11892 (long) (sections.str_offsets_size
11893 ? sections.str_offsets_offset : 0));
11894 /* Can we use an existing virtual DWO file? */
11895 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11896 virtual_dwo_name.c_str (),
11897 comp_dir);
11898 /* Create one if necessary. */
11899 if (*dwo_file_slot == NULL)
11900 {
11901 if (dwarf_read_debug)
11902 {
11903 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11904 virtual_dwo_name.c_str ());
11905 }
11906 dwo_file = new struct dwo_file;
11907 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11908 dwo_file->comp_dir = comp_dir;
11909 dwo_file->sections.abbrev =
11910 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11911 sections.abbrev_offset, sections.abbrev_size);
11912 dwo_file->sections.line =
11913 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11914 sections.line_offset, sections.line_size);
11915 dwo_file->sections.loc =
11916 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11917 sections.loc_offset, sections.loc_size);
11918 dwo_file->sections.macinfo =
11919 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11920 sections.macinfo_offset, sections.macinfo_size);
11921 dwo_file->sections.macro =
11922 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11923 sections.macro_offset, sections.macro_size);
11924 dwo_file->sections.str_offsets =
11925 create_dwp_v2_section (dwarf2_per_objfile,
11926 &dwp_file->sections.str_offsets,
11927 sections.str_offsets_offset,
11928 sections.str_offsets_size);
11929 /* The "str" section is global to the entire DWP file. */
11930 dwo_file->sections.str = dwp_file->sections.str;
11931 /* The info or types section is assigned below to dwo_unit,
11932 there's no need to record it in dwo_file.
11933 Also, we can't simply record type sections in dwo_file because
11934 we record a pointer into the vector in dwo_unit. As we collect more
11935 types we'll grow the vector and eventually have to reallocate space
11936 for it, invalidating all copies of pointers into the previous
11937 contents. */
11938 *dwo_file_slot = dwo_file;
11939 }
11940 else
11941 {
11942 if (dwarf_read_debug)
11943 {
11944 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11945 virtual_dwo_name.c_str ());
11946 }
11947 dwo_file = (struct dwo_file *) *dwo_file_slot;
11948 }
11949
11950 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11951 dwo_unit->dwo_file = dwo_file;
11952 dwo_unit->signature = signature;
11953 dwo_unit->section =
11954 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11955 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11956 is_debug_types
11957 ? &dwp_file->sections.types
11958 : &dwp_file->sections.info,
11959 sections.info_or_types_offset,
11960 sections.info_or_types_size);
11961 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11962
11963 return dwo_unit;
11964 }
11965
11966 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11967 Returns NULL if the signature isn't found. */
11968
11969 static struct dwo_unit *
11970 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11971 struct dwp_file *dwp_file, const char *comp_dir,
11972 ULONGEST signature, int is_debug_types)
11973 {
11974 const struct dwp_hash_table *dwp_htab =
11975 is_debug_types ? dwp_file->tus : dwp_file->cus;
11976 bfd *dbfd = dwp_file->dbfd.get ();
11977 uint32_t mask = dwp_htab->nr_slots - 1;
11978 uint32_t hash = signature & mask;
11979 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11980 unsigned int i;
11981 void **slot;
11982 struct dwo_unit find_dwo_cu;
11983
11984 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11985 find_dwo_cu.signature = signature;
11986 slot = htab_find_slot (is_debug_types
11987 ? dwp_file->loaded_tus.get ()
11988 : dwp_file->loaded_cus.get (),
11989 &find_dwo_cu, INSERT);
11990
11991 if (*slot != NULL)
11992 return (struct dwo_unit *) *slot;
11993
11994 /* Use a for loop so that we don't loop forever on bad debug info. */
11995 for (i = 0; i < dwp_htab->nr_slots; ++i)
11996 {
11997 ULONGEST signature_in_table;
11998
11999 signature_in_table =
12000 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12001 if (signature_in_table == signature)
12002 {
12003 uint32_t unit_index =
12004 read_4_bytes (dbfd,
12005 dwp_htab->unit_table + hash * sizeof (uint32_t));
12006
12007 if (dwp_file->version == 1)
12008 {
12009 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12010 dwp_file, unit_index,
12011 comp_dir, signature,
12012 is_debug_types);
12013 }
12014 else
12015 {
12016 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12017 dwp_file, unit_index,
12018 comp_dir, signature,
12019 is_debug_types);
12020 }
12021 return (struct dwo_unit *) *slot;
12022 }
12023 if (signature_in_table == 0)
12024 return NULL;
12025 hash = (hash + hash2) & mask;
12026 }
12027
12028 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12029 " [in module %s]"),
12030 dwp_file->name);
12031 }
12032
12033 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12034 Open the file specified by FILE_NAME and hand it off to BFD for
12035 preliminary analysis. Return a newly initialized bfd *, which
12036 includes a canonicalized copy of FILE_NAME.
12037 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12038 SEARCH_CWD is true if the current directory is to be searched.
12039 It will be searched before debug-file-directory.
12040 If successful, the file is added to the bfd include table of the
12041 objfile's bfd (see gdb_bfd_record_inclusion).
12042 If unable to find/open the file, return NULL.
12043 NOTE: This function is derived from symfile_bfd_open. */
12044
12045 static gdb_bfd_ref_ptr
12046 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12047 const char *file_name, int is_dwp, int search_cwd)
12048 {
12049 int desc;
12050 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12051 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12052 to debug_file_directory. */
12053 const char *search_path;
12054 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12055
12056 gdb::unique_xmalloc_ptr<char> search_path_holder;
12057 if (search_cwd)
12058 {
12059 if (*debug_file_directory != '\0')
12060 {
12061 search_path_holder.reset (concat (".", dirname_separator_string,
12062 debug_file_directory,
12063 (char *) NULL));
12064 search_path = search_path_holder.get ();
12065 }
12066 else
12067 search_path = ".";
12068 }
12069 else
12070 search_path = debug_file_directory;
12071
12072 openp_flags flags = OPF_RETURN_REALPATH;
12073 if (is_dwp)
12074 flags |= OPF_SEARCH_IN_PATH;
12075
12076 gdb::unique_xmalloc_ptr<char> absolute_name;
12077 desc = openp (search_path, flags, file_name,
12078 O_RDONLY | O_BINARY, &absolute_name);
12079 if (desc < 0)
12080 return NULL;
12081
12082 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12083 gnutarget, desc));
12084 if (sym_bfd == NULL)
12085 return NULL;
12086 bfd_set_cacheable (sym_bfd.get (), 1);
12087
12088 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12089 return NULL;
12090
12091 /* Success. Record the bfd as having been included by the objfile's bfd.
12092 This is important because things like demangled_names_hash lives in the
12093 objfile's per_bfd space and may have references to things like symbol
12094 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12095 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12096
12097 return sym_bfd;
12098 }
12099
12100 /* Try to open DWO file FILE_NAME.
12101 COMP_DIR is the DW_AT_comp_dir attribute.
12102 The result is the bfd handle of the file.
12103 If there is a problem finding or opening the file, return NULL.
12104 Upon success, the canonicalized path of the file is stored in the bfd,
12105 same as symfile_bfd_open. */
12106
12107 static gdb_bfd_ref_ptr
12108 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12109 const char *file_name, const char *comp_dir)
12110 {
12111 if (IS_ABSOLUTE_PATH (file_name))
12112 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12113 0 /*is_dwp*/, 0 /*search_cwd*/);
12114
12115 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12116
12117 if (comp_dir != NULL)
12118 {
12119 gdb::unique_xmalloc_ptr<char> path_to_try
12120 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12121
12122 /* NOTE: If comp_dir is a relative path, this will also try the
12123 search path, which seems useful. */
12124 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12125 path_to_try.get (),
12126 0 /*is_dwp*/,
12127 1 /*search_cwd*/));
12128 if (abfd != NULL)
12129 return abfd;
12130 }
12131
12132 /* That didn't work, try debug-file-directory, which, despite its name,
12133 is a list of paths. */
12134
12135 if (*debug_file_directory == '\0')
12136 return NULL;
12137
12138 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12139 0 /*is_dwp*/, 1 /*search_cwd*/);
12140 }
12141
12142 /* This function is mapped across the sections and remembers the offset and
12143 size of each of the DWO debugging sections we are interested in. */
12144
12145 static void
12146 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12147 {
12148 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12149 const struct dwop_section_names *names = &dwop_section_names;
12150
12151 if (section_is_p (sectp->name, &names->abbrev_dwo))
12152 {
12153 dwo_sections->abbrev.s.section = sectp;
12154 dwo_sections->abbrev.size = bfd_section_size (sectp);
12155 }
12156 else if (section_is_p (sectp->name, &names->info_dwo))
12157 {
12158 dwo_sections->info.s.section = sectp;
12159 dwo_sections->info.size = bfd_section_size (sectp);
12160 }
12161 else if (section_is_p (sectp->name, &names->line_dwo))
12162 {
12163 dwo_sections->line.s.section = sectp;
12164 dwo_sections->line.size = bfd_section_size (sectp);
12165 }
12166 else if (section_is_p (sectp->name, &names->loc_dwo))
12167 {
12168 dwo_sections->loc.s.section = sectp;
12169 dwo_sections->loc.size = bfd_section_size (sectp);
12170 }
12171 else if (section_is_p (sectp->name, &names->loclists_dwo))
12172 {
12173 dwo_sections->loclists.s.section = sectp;
12174 dwo_sections->loclists.size = bfd_section_size (sectp);
12175 }
12176 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12177 {
12178 dwo_sections->macinfo.s.section = sectp;
12179 dwo_sections->macinfo.size = bfd_section_size (sectp);
12180 }
12181 else if (section_is_p (sectp->name, &names->macro_dwo))
12182 {
12183 dwo_sections->macro.s.section = sectp;
12184 dwo_sections->macro.size = bfd_section_size (sectp);
12185 }
12186 else if (section_is_p (sectp->name, &names->str_dwo))
12187 {
12188 dwo_sections->str.s.section = sectp;
12189 dwo_sections->str.size = bfd_section_size (sectp);
12190 }
12191 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12192 {
12193 dwo_sections->str_offsets.s.section = sectp;
12194 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12195 }
12196 else if (section_is_p (sectp->name, &names->types_dwo))
12197 {
12198 struct dwarf2_section_info type_section;
12199
12200 memset (&type_section, 0, sizeof (type_section));
12201 type_section.s.section = sectp;
12202 type_section.size = bfd_section_size (sectp);
12203 dwo_sections->types.push_back (type_section);
12204 }
12205 }
12206
12207 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12208 by PER_CU. This is for the non-DWP case.
12209 The result is NULL if DWO_NAME can't be found. */
12210
12211 static struct dwo_file *
12212 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12213 const char *dwo_name, const char *comp_dir)
12214 {
12215 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12216
12217 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12218 if (dbfd == NULL)
12219 {
12220 if (dwarf_read_debug)
12221 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12222 return NULL;
12223 }
12224
12225 dwo_file_up dwo_file (new struct dwo_file);
12226 dwo_file->dwo_name = dwo_name;
12227 dwo_file->comp_dir = comp_dir;
12228 dwo_file->dbfd = std::move (dbfd);
12229
12230 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12231 &dwo_file->sections);
12232
12233 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12234 dwo_file->sections.info, dwo_file->cus);
12235
12236 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12237 dwo_file->sections.types, dwo_file->tus);
12238
12239 if (dwarf_read_debug)
12240 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12241
12242 return dwo_file.release ();
12243 }
12244
12245 /* This function is mapped across the sections and remembers the offset and
12246 size of each of the DWP debugging sections common to version 1 and 2 that
12247 we are interested in. */
12248
12249 static void
12250 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12251 void *dwp_file_ptr)
12252 {
12253 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12254 const struct dwop_section_names *names = &dwop_section_names;
12255 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12256
12257 /* Record the ELF section number for later lookup: this is what the
12258 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12259 gdb_assert (elf_section_nr < dwp_file->num_sections);
12260 dwp_file->elf_sections[elf_section_nr] = sectp;
12261
12262 /* Look for specific sections that we need. */
12263 if (section_is_p (sectp->name, &names->str_dwo))
12264 {
12265 dwp_file->sections.str.s.section = sectp;
12266 dwp_file->sections.str.size = bfd_section_size (sectp);
12267 }
12268 else if (section_is_p (sectp->name, &names->cu_index))
12269 {
12270 dwp_file->sections.cu_index.s.section = sectp;
12271 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12272 }
12273 else if (section_is_p (sectp->name, &names->tu_index))
12274 {
12275 dwp_file->sections.tu_index.s.section = sectp;
12276 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12277 }
12278 }
12279
12280 /* This function is mapped across the sections and remembers the offset and
12281 size of each of the DWP version 2 debugging sections that we are interested
12282 in. This is split into a separate function because we don't know if we
12283 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12284
12285 static void
12286 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12287 {
12288 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12289 const struct dwop_section_names *names = &dwop_section_names;
12290 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12291
12292 /* Record the ELF section number for later lookup: this is what the
12293 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12294 gdb_assert (elf_section_nr < dwp_file->num_sections);
12295 dwp_file->elf_sections[elf_section_nr] = sectp;
12296
12297 /* Look for specific sections that we need. */
12298 if (section_is_p (sectp->name, &names->abbrev_dwo))
12299 {
12300 dwp_file->sections.abbrev.s.section = sectp;
12301 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12302 }
12303 else if (section_is_p (sectp->name, &names->info_dwo))
12304 {
12305 dwp_file->sections.info.s.section = sectp;
12306 dwp_file->sections.info.size = bfd_section_size (sectp);
12307 }
12308 else if (section_is_p (sectp->name, &names->line_dwo))
12309 {
12310 dwp_file->sections.line.s.section = sectp;
12311 dwp_file->sections.line.size = bfd_section_size (sectp);
12312 }
12313 else if (section_is_p (sectp->name, &names->loc_dwo))
12314 {
12315 dwp_file->sections.loc.s.section = sectp;
12316 dwp_file->sections.loc.size = bfd_section_size (sectp);
12317 }
12318 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12319 {
12320 dwp_file->sections.macinfo.s.section = sectp;
12321 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12322 }
12323 else if (section_is_p (sectp->name, &names->macro_dwo))
12324 {
12325 dwp_file->sections.macro.s.section = sectp;
12326 dwp_file->sections.macro.size = bfd_section_size (sectp);
12327 }
12328 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12329 {
12330 dwp_file->sections.str_offsets.s.section = sectp;
12331 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12332 }
12333 else if (section_is_p (sectp->name, &names->types_dwo))
12334 {
12335 dwp_file->sections.types.s.section = sectp;
12336 dwp_file->sections.types.size = bfd_section_size (sectp);
12337 }
12338 }
12339
12340 /* Hash function for dwp_file loaded CUs/TUs. */
12341
12342 static hashval_t
12343 hash_dwp_loaded_cutus (const void *item)
12344 {
12345 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12346
12347 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12348 return dwo_unit->signature;
12349 }
12350
12351 /* Equality function for dwp_file loaded CUs/TUs. */
12352
12353 static int
12354 eq_dwp_loaded_cutus (const void *a, const void *b)
12355 {
12356 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12357 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12358
12359 return dua->signature == dub->signature;
12360 }
12361
12362 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12363
12364 static htab_up
12365 allocate_dwp_loaded_cutus_table ()
12366 {
12367 return htab_up (htab_create_alloc (3,
12368 hash_dwp_loaded_cutus,
12369 eq_dwp_loaded_cutus,
12370 NULL, xcalloc, xfree));
12371 }
12372
12373 /* Try to open DWP file FILE_NAME.
12374 The result is the bfd handle of the file.
12375 If there is a problem finding or opening the file, return NULL.
12376 Upon success, the canonicalized path of the file is stored in the bfd,
12377 same as symfile_bfd_open. */
12378
12379 static gdb_bfd_ref_ptr
12380 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12381 const char *file_name)
12382 {
12383 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12384 1 /*is_dwp*/,
12385 1 /*search_cwd*/));
12386 if (abfd != NULL)
12387 return abfd;
12388
12389 /* Work around upstream bug 15652.
12390 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12391 [Whether that's a "bug" is debatable, but it is getting in our way.]
12392 We have no real idea where the dwp file is, because gdb's realpath-ing
12393 of the executable's path may have discarded the needed info.
12394 [IWBN if the dwp file name was recorded in the executable, akin to
12395 .gnu_debuglink, but that doesn't exist yet.]
12396 Strip the directory from FILE_NAME and search again. */
12397 if (*debug_file_directory != '\0')
12398 {
12399 /* Don't implicitly search the current directory here.
12400 If the user wants to search "." to handle this case,
12401 it must be added to debug-file-directory. */
12402 return try_open_dwop_file (dwarf2_per_objfile,
12403 lbasename (file_name), 1 /*is_dwp*/,
12404 0 /*search_cwd*/);
12405 }
12406
12407 return NULL;
12408 }
12409
12410 /* Initialize the use of the DWP file for the current objfile.
12411 By convention the name of the DWP file is ${objfile}.dwp.
12412 The result is NULL if it can't be found. */
12413
12414 static std::unique_ptr<struct dwp_file>
12415 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12416 {
12417 struct objfile *objfile = dwarf2_per_objfile->objfile;
12418
12419 /* Try to find first .dwp for the binary file before any symbolic links
12420 resolving. */
12421
12422 /* If the objfile is a debug file, find the name of the real binary
12423 file and get the name of dwp file from there. */
12424 std::string dwp_name;
12425 if (objfile->separate_debug_objfile_backlink != NULL)
12426 {
12427 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12428 const char *backlink_basename = lbasename (backlink->original_name);
12429
12430 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12431 }
12432 else
12433 dwp_name = objfile->original_name;
12434
12435 dwp_name += ".dwp";
12436
12437 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12438 if (dbfd == NULL
12439 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12440 {
12441 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12442 dwp_name = objfile_name (objfile);
12443 dwp_name += ".dwp";
12444 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12445 }
12446
12447 if (dbfd == NULL)
12448 {
12449 if (dwarf_read_debug)
12450 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12451 return std::unique_ptr<dwp_file> ();
12452 }
12453
12454 const char *name = bfd_get_filename (dbfd.get ());
12455 std::unique_ptr<struct dwp_file> dwp_file
12456 (new struct dwp_file (name, std::move (dbfd)));
12457
12458 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12459 dwp_file->elf_sections =
12460 OBSTACK_CALLOC (&objfile->objfile_obstack,
12461 dwp_file->num_sections, asection *);
12462
12463 bfd_map_over_sections (dwp_file->dbfd.get (),
12464 dwarf2_locate_common_dwp_sections,
12465 dwp_file.get ());
12466
12467 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12468 0);
12469
12470 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12471 1);
12472
12473 /* The DWP file version is stored in the hash table. Oh well. */
12474 if (dwp_file->cus && dwp_file->tus
12475 && dwp_file->cus->version != dwp_file->tus->version)
12476 {
12477 /* Technically speaking, we should try to limp along, but this is
12478 pretty bizarre. We use pulongest here because that's the established
12479 portability solution (e.g, we cannot use %u for uint32_t). */
12480 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12481 " TU version %s [in DWP file %s]"),
12482 pulongest (dwp_file->cus->version),
12483 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12484 }
12485
12486 if (dwp_file->cus)
12487 dwp_file->version = dwp_file->cus->version;
12488 else if (dwp_file->tus)
12489 dwp_file->version = dwp_file->tus->version;
12490 else
12491 dwp_file->version = 2;
12492
12493 if (dwp_file->version == 2)
12494 bfd_map_over_sections (dwp_file->dbfd.get (),
12495 dwarf2_locate_v2_dwp_sections,
12496 dwp_file.get ());
12497
12498 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12499 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12500
12501 if (dwarf_read_debug)
12502 {
12503 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12504 fprintf_unfiltered (gdb_stdlog,
12505 " %s CUs, %s TUs\n",
12506 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12507 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12508 }
12509
12510 return dwp_file;
12511 }
12512
12513 /* Wrapper around open_and_init_dwp_file, only open it once. */
12514
12515 static struct dwp_file *
12516 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12517 {
12518 if (! dwarf2_per_objfile->dwp_checked)
12519 {
12520 dwarf2_per_objfile->dwp_file
12521 = open_and_init_dwp_file (dwarf2_per_objfile);
12522 dwarf2_per_objfile->dwp_checked = 1;
12523 }
12524 return dwarf2_per_objfile->dwp_file.get ();
12525 }
12526
12527 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12528 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12529 or in the DWP file for the objfile, referenced by THIS_UNIT.
12530 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12531 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12532
12533 This is called, for example, when wanting to read a variable with a
12534 complex location. Therefore we don't want to do file i/o for every call.
12535 Therefore we don't want to look for a DWO file on every call.
12536 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12537 then we check if we've already seen DWO_NAME, and only THEN do we check
12538 for a DWO file.
12539
12540 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12541 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12542
12543 static struct dwo_unit *
12544 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12545 const char *dwo_name, const char *comp_dir,
12546 ULONGEST signature, int is_debug_types)
12547 {
12548 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12549 struct objfile *objfile = dwarf2_per_objfile->objfile;
12550 const char *kind = is_debug_types ? "TU" : "CU";
12551 void **dwo_file_slot;
12552 struct dwo_file *dwo_file;
12553 struct dwp_file *dwp_file;
12554
12555 /* First see if there's a DWP file.
12556 If we have a DWP file but didn't find the DWO inside it, don't
12557 look for the original DWO file. It makes gdb behave differently
12558 depending on whether one is debugging in the build tree. */
12559
12560 dwp_file = get_dwp_file (dwarf2_per_objfile);
12561 if (dwp_file != NULL)
12562 {
12563 const struct dwp_hash_table *dwp_htab =
12564 is_debug_types ? dwp_file->tus : dwp_file->cus;
12565
12566 if (dwp_htab != NULL)
12567 {
12568 struct dwo_unit *dwo_cutu =
12569 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12570 signature, is_debug_types);
12571
12572 if (dwo_cutu != NULL)
12573 {
12574 if (dwarf_read_debug)
12575 {
12576 fprintf_unfiltered (gdb_stdlog,
12577 "Virtual DWO %s %s found: @%s\n",
12578 kind, hex_string (signature),
12579 host_address_to_string (dwo_cutu));
12580 }
12581 return dwo_cutu;
12582 }
12583 }
12584 }
12585 else
12586 {
12587 /* No DWP file, look for the DWO file. */
12588
12589 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12590 dwo_name, comp_dir);
12591 if (*dwo_file_slot == NULL)
12592 {
12593 /* Read in the file and build a table of the CUs/TUs it contains. */
12594 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12595 }
12596 /* NOTE: This will be NULL if unable to open the file. */
12597 dwo_file = (struct dwo_file *) *dwo_file_slot;
12598
12599 if (dwo_file != NULL)
12600 {
12601 struct dwo_unit *dwo_cutu = NULL;
12602
12603 if (is_debug_types && dwo_file->tus)
12604 {
12605 struct dwo_unit find_dwo_cutu;
12606
12607 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12608 find_dwo_cutu.signature = signature;
12609 dwo_cutu
12610 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12611 &find_dwo_cutu);
12612 }
12613 else if (!is_debug_types && dwo_file->cus)
12614 {
12615 struct dwo_unit find_dwo_cutu;
12616
12617 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12618 find_dwo_cutu.signature = signature;
12619 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12620 &find_dwo_cutu);
12621 }
12622
12623 if (dwo_cutu != NULL)
12624 {
12625 if (dwarf_read_debug)
12626 {
12627 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12628 kind, dwo_name, hex_string (signature),
12629 host_address_to_string (dwo_cutu));
12630 }
12631 return dwo_cutu;
12632 }
12633 }
12634 }
12635
12636 /* We didn't find it. This could mean a dwo_id mismatch, or
12637 someone deleted the DWO/DWP file, or the search path isn't set up
12638 correctly to find the file. */
12639
12640 if (dwarf_read_debug)
12641 {
12642 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12643 kind, dwo_name, hex_string (signature));
12644 }
12645
12646 /* This is a warning and not a complaint because it can be caused by
12647 pilot error (e.g., user accidentally deleting the DWO). */
12648 {
12649 /* Print the name of the DWP file if we looked there, helps the user
12650 better diagnose the problem. */
12651 std::string dwp_text;
12652
12653 if (dwp_file != NULL)
12654 dwp_text = string_printf (" [in DWP file %s]",
12655 lbasename (dwp_file->name));
12656
12657 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12658 " [in module %s]"),
12659 kind, dwo_name, hex_string (signature),
12660 dwp_text.c_str (),
12661 this_unit->is_debug_types ? "TU" : "CU",
12662 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12663 }
12664 return NULL;
12665 }
12666
12667 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12668 See lookup_dwo_cutu_unit for details. */
12669
12670 static struct dwo_unit *
12671 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12672 const char *dwo_name, const char *comp_dir,
12673 ULONGEST signature)
12674 {
12675 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12676 }
12677
12678 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12679 See lookup_dwo_cutu_unit for details. */
12680
12681 static struct dwo_unit *
12682 lookup_dwo_type_unit (struct signatured_type *this_tu,
12683 const char *dwo_name, const char *comp_dir)
12684 {
12685 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12686 }
12687
12688 /* Traversal function for queue_and_load_all_dwo_tus. */
12689
12690 static int
12691 queue_and_load_dwo_tu (void **slot, void *info)
12692 {
12693 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12694 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12695 ULONGEST signature = dwo_unit->signature;
12696 struct signatured_type *sig_type =
12697 lookup_dwo_signatured_type (per_cu->cu, signature);
12698
12699 if (sig_type != NULL)
12700 {
12701 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12702
12703 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12704 a real dependency of PER_CU on SIG_TYPE. That is detected later
12705 while processing PER_CU. */
12706 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12707 load_full_type_unit (sig_cu);
12708 per_cu->imported_symtabs_push (sig_cu);
12709 }
12710
12711 return 1;
12712 }
12713
12714 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12715 The DWO may have the only definition of the type, though it may not be
12716 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12717 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12718
12719 static void
12720 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12721 {
12722 struct dwo_unit *dwo_unit;
12723 struct dwo_file *dwo_file;
12724
12725 gdb_assert (!per_cu->is_debug_types);
12726 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12727 gdb_assert (per_cu->cu != NULL);
12728
12729 dwo_unit = per_cu->cu->dwo_unit;
12730 gdb_assert (dwo_unit != NULL);
12731
12732 dwo_file = dwo_unit->dwo_file;
12733 if (dwo_file->tus != NULL)
12734 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12735 per_cu);
12736 }
12737
12738 /* Read in various DIEs. */
12739
12740 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12741 Inherit only the children of the DW_AT_abstract_origin DIE not being
12742 already referenced by DW_AT_abstract_origin from the children of the
12743 current DIE. */
12744
12745 static void
12746 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12747 {
12748 struct die_info *child_die;
12749 sect_offset *offsetp;
12750 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12751 struct die_info *origin_die;
12752 /* Iterator of the ORIGIN_DIE children. */
12753 struct die_info *origin_child_die;
12754 struct attribute *attr;
12755 struct dwarf2_cu *origin_cu;
12756 struct pending **origin_previous_list_in_scope;
12757
12758 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12759 if (!attr)
12760 return;
12761
12762 /* Note that following die references may follow to a die in a
12763 different cu. */
12764
12765 origin_cu = cu;
12766 origin_die = follow_die_ref (die, attr, &origin_cu);
12767
12768 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12769 symbols in. */
12770 origin_previous_list_in_scope = origin_cu->list_in_scope;
12771 origin_cu->list_in_scope = cu->list_in_scope;
12772
12773 if (die->tag != origin_die->tag
12774 && !(die->tag == DW_TAG_inlined_subroutine
12775 && origin_die->tag == DW_TAG_subprogram))
12776 complaint (_("DIE %s and its abstract origin %s have different tags"),
12777 sect_offset_str (die->sect_off),
12778 sect_offset_str (origin_die->sect_off));
12779
12780 std::vector<sect_offset> offsets;
12781
12782 for (child_die = die->child;
12783 child_die && child_die->tag;
12784 child_die = child_die->sibling)
12785 {
12786 struct die_info *child_origin_die;
12787 struct dwarf2_cu *child_origin_cu;
12788
12789 /* We are trying to process concrete instance entries:
12790 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12791 it's not relevant to our analysis here. i.e. detecting DIEs that are
12792 present in the abstract instance but not referenced in the concrete
12793 one. */
12794 if (child_die->tag == DW_TAG_call_site
12795 || child_die->tag == DW_TAG_GNU_call_site)
12796 continue;
12797
12798 /* For each CHILD_DIE, find the corresponding child of
12799 ORIGIN_DIE. If there is more than one layer of
12800 DW_AT_abstract_origin, follow them all; there shouldn't be,
12801 but GCC versions at least through 4.4 generate this (GCC PR
12802 40573). */
12803 child_origin_die = child_die;
12804 child_origin_cu = cu;
12805 while (1)
12806 {
12807 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12808 child_origin_cu);
12809 if (attr == NULL)
12810 break;
12811 child_origin_die = follow_die_ref (child_origin_die, attr,
12812 &child_origin_cu);
12813 }
12814
12815 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12816 counterpart may exist. */
12817 if (child_origin_die != child_die)
12818 {
12819 if (child_die->tag != child_origin_die->tag
12820 && !(child_die->tag == DW_TAG_inlined_subroutine
12821 && child_origin_die->tag == DW_TAG_subprogram))
12822 complaint (_("Child DIE %s and its abstract origin %s have "
12823 "different tags"),
12824 sect_offset_str (child_die->sect_off),
12825 sect_offset_str (child_origin_die->sect_off));
12826 if (child_origin_die->parent != origin_die)
12827 complaint (_("Child DIE %s and its abstract origin %s have "
12828 "different parents"),
12829 sect_offset_str (child_die->sect_off),
12830 sect_offset_str (child_origin_die->sect_off));
12831 else
12832 offsets.push_back (child_origin_die->sect_off);
12833 }
12834 }
12835 std::sort (offsets.begin (), offsets.end ());
12836 sect_offset *offsets_end = offsets.data () + offsets.size ();
12837 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12838 if (offsetp[-1] == *offsetp)
12839 complaint (_("Multiple children of DIE %s refer "
12840 "to DIE %s as their abstract origin"),
12841 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12842
12843 offsetp = offsets.data ();
12844 origin_child_die = origin_die->child;
12845 while (origin_child_die && origin_child_die->tag)
12846 {
12847 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12848 while (offsetp < offsets_end
12849 && *offsetp < origin_child_die->sect_off)
12850 offsetp++;
12851 if (offsetp >= offsets_end
12852 || *offsetp > origin_child_die->sect_off)
12853 {
12854 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12855 Check whether we're already processing ORIGIN_CHILD_DIE.
12856 This can happen with mutually referenced abstract_origins.
12857 PR 16581. */
12858 if (!origin_child_die->in_process)
12859 process_die (origin_child_die, origin_cu);
12860 }
12861 origin_child_die = origin_child_die->sibling;
12862 }
12863 origin_cu->list_in_scope = origin_previous_list_in_scope;
12864
12865 if (cu != origin_cu)
12866 compute_delayed_physnames (origin_cu);
12867 }
12868
12869 static void
12870 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12871 {
12872 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12873 struct gdbarch *gdbarch = objfile->arch ();
12874 struct context_stack *newobj;
12875 CORE_ADDR lowpc;
12876 CORE_ADDR highpc;
12877 struct die_info *child_die;
12878 struct attribute *attr, *call_line, *call_file;
12879 const char *name;
12880 CORE_ADDR baseaddr;
12881 struct block *block;
12882 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12883 std::vector<struct symbol *> template_args;
12884 struct template_symbol *templ_func = NULL;
12885
12886 if (inlined_func)
12887 {
12888 /* If we do not have call site information, we can't show the
12889 caller of this inlined function. That's too confusing, so
12890 only use the scope for local variables. */
12891 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12892 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12893 if (call_line == NULL || call_file == NULL)
12894 {
12895 read_lexical_block_scope (die, cu);
12896 return;
12897 }
12898 }
12899
12900 baseaddr = objfile->text_section_offset ();
12901
12902 name = dwarf2_name (die, cu);
12903
12904 /* Ignore functions with missing or empty names. These are actually
12905 illegal according to the DWARF standard. */
12906 if (name == NULL)
12907 {
12908 complaint (_("missing name for subprogram DIE at %s"),
12909 sect_offset_str (die->sect_off));
12910 return;
12911 }
12912
12913 /* Ignore functions with missing or invalid low and high pc attributes. */
12914 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12915 <= PC_BOUNDS_INVALID)
12916 {
12917 attr = dwarf2_attr (die, DW_AT_external, cu);
12918 if (!attr || !DW_UNSND (attr))
12919 complaint (_("cannot get low and high bounds "
12920 "for subprogram DIE at %s"),
12921 sect_offset_str (die->sect_off));
12922 return;
12923 }
12924
12925 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12926 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12927
12928 /* If we have any template arguments, then we must allocate a
12929 different sort of symbol. */
12930 for (child_die = die->child; child_die; child_die = child_die->sibling)
12931 {
12932 if (child_die->tag == DW_TAG_template_type_param
12933 || child_die->tag == DW_TAG_template_value_param)
12934 {
12935 templ_func = allocate_template_symbol (objfile);
12936 templ_func->subclass = SYMBOL_TEMPLATE;
12937 break;
12938 }
12939 }
12940
12941 newobj = cu->get_builder ()->push_context (0, lowpc);
12942 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12943 (struct symbol *) templ_func);
12944
12945 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12946 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12947 cu->language);
12948
12949 /* If there is a location expression for DW_AT_frame_base, record
12950 it. */
12951 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12952 if (attr != nullptr)
12953 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12954
12955 /* If there is a location for the static link, record it. */
12956 newobj->static_link = NULL;
12957 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12958 if (attr != nullptr)
12959 {
12960 newobj->static_link
12961 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12962 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12963 cu->per_cu->addr_type ());
12964 }
12965
12966 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12967
12968 if (die->child != NULL)
12969 {
12970 child_die = die->child;
12971 while (child_die && child_die->tag)
12972 {
12973 if (child_die->tag == DW_TAG_template_type_param
12974 || child_die->tag == DW_TAG_template_value_param)
12975 {
12976 struct symbol *arg = new_symbol (child_die, NULL, cu);
12977
12978 if (arg != NULL)
12979 template_args.push_back (arg);
12980 }
12981 else
12982 process_die (child_die, cu);
12983 child_die = child_die->sibling;
12984 }
12985 }
12986
12987 inherit_abstract_dies (die, cu);
12988
12989 /* If we have a DW_AT_specification, we might need to import using
12990 directives from the context of the specification DIE. See the
12991 comment in determine_prefix. */
12992 if (cu->language == language_cplus
12993 && dwarf2_attr (die, DW_AT_specification, cu))
12994 {
12995 struct dwarf2_cu *spec_cu = cu;
12996 struct die_info *spec_die = die_specification (die, &spec_cu);
12997
12998 while (spec_die)
12999 {
13000 child_die = spec_die->child;
13001 while (child_die && child_die->tag)
13002 {
13003 if (child_die->tag == DW_TAG_imported_module)
13004 process_die (child_die, spec_cu);
13005 child_die = child_die->sibling;
13006 }
13007
13008 /* In some cases, GCC generates specification DIEs that
13009 themselves contain DW_AT_specification attributes. */
13010 spec_die = die_specification (spec_die, &spec_cu);
13011 }
13012 }
13013
13014 struct context_stack cstk = cu->get_builder ()->pop_context ();
13015 /* Make a block for the local symbols within. */
13016 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13017 cstk.static_link, lowpc, highpc);
13018
13019 /* For C++, set the block's scope. */
13020 if ((cu->language == language_cplus
13021 || cu->language == language_fortran
13022 || cu->language == language_d
13023 || cu->language == language_rust)
13024 && cu->processing_has_namespace_info)
13025 block_set_scope (block, determine_prefix (die, cu),
13026 &objfile->objfile_obstack);
13027
13028 /* If we have address ranges, record them. */
13029 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13030
13031 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13032
13033 /* Attach template arguments to function. */
13034 if (!template_args.empty ())
13035 {
13036 gdb_assert (templ_func != NULL);
13037
13038 templ_func->n_template_arguments = template_args.size ();
13039 templ_func->template_arguments
13040 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13041 templ_func->n_template_arguments);
13042 memcpy (templ_func->template_arguments,
13043 template_args.data (),
13044 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13045
13046 /* Make sure that the symtab is set on the new symbols. Even
13047 though they don't appear in this symtab directly, other parts
13048 of gdb assume that symbols do, and this is reasonably
13049 true. */
13050 for (symbol *sym : template_args)
13051 symbol_set_symtab (sym, symbol_symtab (templ_func));
13052 }
13053
13054 /* In C++, we can have functions nested inside functions (e.g., when
13055 a function declares a class that has methods). This means that
13056 when we finish processing a function scope, we may need to go
13057 back to building a containing block's symbol lists. */
13058 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13059 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13060
13061 /* If we've finished processing a top-level function, subsequent
13062 symbols go in the file symbol list. */
13063 if (cu->get_builder ()->outermost_context_p ())
13064 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13065 }
13066
13067 /* Process all the DIES contained within a lexical block scope. Start
13068 a new scope, process the dies, and then close the scope. */
13069
13070 static void
13071 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13072 {
13073 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13074 struct gdbarch *gdbarch = objfile->arch ();
13075 CORE_ADDR lowpc, highpc;
13076 struct die_info *child_die;
13077 CORE_ADDR baseaddr;
13078
13079 baseaddr = objfile->text_section_offset ();
13080
13081 /* Ignore blocks with missing or invalid low and high pc attributes. */
13082 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13083 as multiple lexical blocks? Handling children in a sane way would
13084 be nasty. Might be easier to properly extend generic blocks to
13085 describe ranges. */
13086 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13087 {
13088 case PC_BOUNDS_NOT_PRESENT:
13089 /* DW_TAG_lexical_block has no attributes, process its children as if
13090 there was no wrapping by that DW_TAG_lexical_block.
13091 GCC does no longer produces such DWARF since GCC r224161. */
13092 for (child_die = die->child;
13093 child_die != NULL && child_die->tag;
13094 child_die = child_die->sibling)
13095 process_die (child_die, cu);
13096 return;
13097 case PC_BOUNDS_INVALID:
13098 return;
13099 }
13100 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13101 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13102
13103 cu->get_builder ()->push_context (0, lowpc);
13104 if (die->child != NULL)
13105 {
13106 child_die = die->child;
13107 while (child_die && child_die->tag)
13108 {
13109 process_die (child_die, cu);
13110 child_die = child_die->sibling;
13111 }
13112 }
13113 inherit_abstract_dies (die, cu);
13114 struct context_stack cstk = cu->get_builder ()->pop_context ();
13115
13116 if (*cu->get_builder ()->get_local_symbols () != NULL
13117 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13118 {
13119 struct block *block
13120 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13121 cstk.start_addr, highpc);
13122
13123 /* Note that recording ranges after traversing children, as we
13124 do here, means that recording a parent's ranges entails
13125 walking across all its children's ranges as they appear in
13126 the address map, which is quadratic behavior.
13127
13128 It would be nicer to record the parent's ranges before
13129 traversing its children, simply overriding whatever you find
13130 there. But since we don't even decide whether to create a
13131 block until after we've traversed its children, that's hard
13132 to do. */
13133 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13134 }
13135 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13136 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13137 }
13138
13139 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13140
13141 static void
13142 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13143 {
13144 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13145 struct gdbarch *gdbarch = objfile->arch ();
13146 CORE_ADDR pc, baseaddr;
13147 struct attribute *attr;
13148 struct call_site *call_site, call_site_local;
13149 void **slot;
13150 int nparams;
13151 struct die_info *child_die;
13152
13153 baseaddr = objfile->text_section_offset ();
13154
13155 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13156 if (attr == NULL)
13157 {
13158 /* This was a pre-DWARF-5 GNU extension alias
13159 for DW_AT_call_return_pc. */
13160 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13161 }
13162 if (!attr)
13163 {
13164 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13165 "DIE %s [in module %s]"),
13166 sect_offset_str (die->sect_off), objfile_name (objfile));
13167 return;
13168 }
13169 pc = attr->value_as_address () + baseaddr;
13170 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13171
13172 if (cu->call_site_htab == NULL)
13173 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13174 NULL, &objfile->objfile_obstack,
13175 hashtab_obstack_allocate, NULL);
13176 call_site_local.pc = pc;
13177 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13178 if (*slot != NULL)
13179 {
13180 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13181 "DIE %s [in module %s]"),
13182 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13183 objfile_name (objfile));
13184 return;
13185 }
13186
13187 /* Count parameters at the caller. */
13188
13189 nparams = 0;
13190 for (child_die = die->child; child_die && child_die->tag;
13191 child_die = child_die->sibling)
13192 {
13193 if (child_die->tag != DW_TAG_call_site_parameter
13194 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13195 {
13196 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13197 "DW_TAG_call_site child DIE %s [in module %s]"),
13198 child_die->tag, sect_offset_str (child_die->sect_off),
13199 objfile_name (objfile));
13200 continue;
13201 }
13202
13203 nparams++;
13204 }
13205
13206 call_site
13207 = ((struct call_site *)
13208 obstack_alloc (&objfile->objfile_obstack,
13209 sizeof (*call_site)
13210 + (sizeof (*call_site->parameter) * (nparams - 1))));
13211 *slot = call_site;
13212 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13213 call_site->pc = pc;
13214
13215 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13216 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13217 {
13218 struct die_info *func_die;
13219
13220 /* Skip also over DW_TAG_inlined_subroutine. */
13221 for (func_die = die->parent;
13222 func_die && func_die->tag != DW_TAG_subprogram
13223 && func_die->tag != DW_TAG_subroutine_type;
13224 func_die = func_die->parent);
13225
13226 /* DW_AT_call_all_calls is a superset
13227 of DW_AT_call_all_tail_calls. */
13228 if (func_die
13229 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13230 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13231 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13232 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13233 {
13234 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13235 not complete. But keep CALL_SITE for look ups via call_site_htab,
13236 both the initial caller containing the real return address PC and
13237 the final callee containing the current PC of a chain of tail
13238 calls do not need to have the tail call list complete. But any
13239 function candidate for a virtual tail call frame searched via
13240 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13241 determined unambiguously. */
13242 }
13243 else
13244 {
13245 struct type *func_type = NULL;
13246
13247 if (func_die)
13248 func_type = get_die_type (func_die, cu);
13249 if (func_type != NULL)
13250 {
13251 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13252
13253 /* Enlist this call site to the function. */
13254 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13255 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13256 }
13257 else
13258 complaint (_("Cannot find function owning DW_TAG_call_site "
13259 "DIE %s [in module %s]"),
13260 sect_offset_str (die->sect_off), objfile_name (objfile));
13261 }
13262 }
13263
13264 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13265 if (attr == NULL)
13266 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13267 if (attr == NULL)
13268 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13269 if (attr == NULL)
13270 {
13271 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13272 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13273 }
13274 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13275 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13276 /* Keep NULL DWARF_BLOCK. */;
13277 else if (attr->form_is_block ())
13278 {
13279 struct dwarf2_locexpr_baton *dlbaton;
13280
13281 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13282 dlbaton->data = DW_BLOCK (attr)->data;
13283 dlbaton->size = DW_BLOCK (attr)->size;
13284 dlbaton->per_cu = cu->per_cu;
13285
13286 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13287 }
13288 else if (attr->form_is_ref ())
13289 {
13290 struct dwarf2_cu *target_cu = cu;
13291 struct die_info *target_die;
13292
13293 target_die = follow_die_ref (die, attr, &target_cu);
13294 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13295 if (die_is_declaration (target_die, target_cu))
13296 {
13297 const char *target_physname;
13298
13299 /* Prefer the mangled name; otherwise compute the demangled one. */
13300 target_physname = dw2_linkage_name (target_die, target_cu);
13301 if (target_physname == NULL)
13302 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13303 if (target_physname == NULL)
13304 complaint (_("DW_AT_call_target target DIE has invalid "
13305 "physname, for referencing DIE %s [in module %s]"),
13306 sect_offset_str (die->sect_off), objfile_name (objfile));
13307 else
13308 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13309 }
13310 else
13311 {
13312 CORE_ADDR lowpc;
13313
13314 /* DW_AT_entry_pc should be preferred. */
13315 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13316 <= PC_BOUNDS_INVALID)
13317 complaint (_("DW_AT_call_target target DIE has invalid "
13318 "low pc, for referencing DIE %s [in module %s]"),
13319 sect_offset_str (die->sect_off), objfile_name (objfile));
13320 else
13321 {
13322 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13323 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13324 }
13325 }
13326 }
13327 else
13328 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13329 "block nor reference, for DIE %s [in module %s]"),
13330 sect_offset_str (die->sect_off), objfile_name (objfile));
13331
13332 call_site->per_cu = cu->per_cu;
13333
13334 for (child_die = die->child;
13335 child_die && child_die->tag;
13336 child_die = child_die->sibling)
13337 {
13338 struct call_site_parameter *parameter;
13339 struct attribute *loc, *origin;
13340
13341 if (child_die->tag != DW_TAG_call_site_parameter
13342 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13343 {
13344 /* Already printed the complaint above. */
13345 continue;
13346 }
13347
13348 gdb_assert (call_site->parameter_count < nparams);
13349 parameter = &call_site->parameter[call_site->parameter_count];
13350
13351 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13352 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13353 register is contained in DW_AT_call_value. */
13354
13355 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13356 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13357 if (origin == NULL)
13358 {
13359 /* This was a pre-DWARF-5 GNU extension alias
13360 for DW_AT_call_parameter. */
13361 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13362 }
13363 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13364 {
13365 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13366
13367 sect_offset sect_off = origin->get_ref_die_offset ();
13368 if (!cu->header.offset_in_cu_p (sect_off))
13369 {
13370 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13371 binding can be done only inside one CU. Such referenced DIE
13372 therefore cannot be even moved to DW_TAG_partial_unit. */
13373 complaint (_("DW_AT_call_parameter offset is not in CU for "
13374 "DW_TAG_call_site child DIE %s [in module %s]"),
13375 sect_offset_str (child_die->sect_off),
13376 objfile_name (objfile));
13377 continue;
13378 }
13379 parameter->u.param_cu_off
13380 = (cu_offset) (sect_off - cu->header.sect_off);
13381 }
13382 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13383 {
13384 complaint (_("No DW_FORM_block* DW_AT_location for "
13385 "DW_TAG_call_site child DIE %s [in module %s]"),
13386 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13387 continue;
13388 }
13389 else
13390 {
13391 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13392 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13393 if (parameter->u.dwarf_reg != -1)
13394 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13395 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13396 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13397 &parameter->u.fb_offset))
13398 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13399 else
13400 {
13401 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13402 "for DW_FORM_block* DW_AT_location is supported for "
13403 "DW_TAG_call_site child DIE %s "
13404 "[in module %s]"),
13405 sect_offset_str (child_die->sect_off),
13406 objfile_name (objfile));
13407 continue;
13408 }
13409 }
13410
13411 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13412 if (attr == NULL)
13413 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13414 if (attr == NULL || !attr->form_is_block ())
13415 {
13416 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13417 "DW_TAG_call_site child DIE %s [in module %s]"),
13418 sect_offset_str (child_die->sect_off),
13419 objfile_name (objfile));
13420 continue;
13421 }
13422 parameter->value = DW_BLOCK (attr)->data;
13423 parameter->value_size = DW_BLOCK (attr)->size;
13424
13425 /* Parameters are not pre-cleared by memset above. */
13426 parameter->data_value = NULL;
13427 parameter->data_value_size = 0;
13428 call_site->parameter_count++;
13429
13430 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13431 if (attr == NULL)
13432 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13433 if (attr != nullptr)
13434 {
13435 if (!attr->form_is_block ())
13436 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13437 "DW_TAG_call_site child DIE %s [in module %s]"),
13438 sect_offset_str (child_die->sect_off),
13439 objfile_name (objfile));
13440 else
13441 {
13442 parameter->data_value = DW_BLOCK (attr)->data;
13443 parameter->data_value_size = DW_BLOCK (attr)->size;
13444 }
13445 }
13446 }
13447 }
13448
13449 /* Helper function for read_variable. If DIE represents a virtual
13450 table, then return the type of the concrete object that is
13451 associated with the virtual table. Otherwise, return NULL. */
13452
13453 static struct type *
13454 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13455 {
13456 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13457 if (attr == NULL)
13458 return NULL;
13459
13460 /* Find the type DIE. */
13461 struct die_info *type_die = NULL;
13462 struct dwarf2_cu *type_cu = cu;
13463
13464 if (attr->form_is_ref ())
13465 type_die = follow_die_ref (die, attr, &type_cu);
13466 if (type_die == NULL)
13467 return NULL;
13468
13469 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13470 return NULL;
13471 return die_containing_type (type_die, type_cu);
13472 }
13473
13474 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13475
13476 static void
13477 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13478 {
13479 struct rust_vtable_symbol *storage = NULL;
13480
13481 if (cu->language == language_rust)
13482 {
13483 struct type *containing_type = rust_containing_type (die, cu);
13484
13485 if (containing_type != NULL)
13486 {
13487 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13488
13489 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13490 initialize_objfile_symbol (storage);
13491 storage->concrete_type = containing_type;
13492 storage->subclass = SYMBOL_RUST_VTABLE;
13493 }
13494 }
13495
13496 struct symbol *res = new_symbol (die, NULL, cu, storage);
13497 struct attribute *abstract_origin
13498 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13499 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13500 if (res == NULL && loc && abstract_origin)
13501 {
13502 /* We have a variable without a name, but with a location and an abstract
13503 origin. This may be a concrete instance of an abstract variable
13504 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13505 later. */
13506 struct dwarf2_cu *origin_cu = cu;
13507 struct die_info *origin_die
13508 = follow_die_ref (die, abstract_origin, &origin_cu);
13509 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13510 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13511 }
13512 }
13513
13514 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13515 reading .debug_rnglists.
13516 Callback's type should be:
13517 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13518 Return true if the attributes are present and valid, otherwise,
13519 return false. */
13520
13521 template <typename Callback>
13522 static bool
13523 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13524 Callback &&callback)
13525 {
13526 struct dwarf2_per_objfile *dwarf2_per_objfile
13527 = cu->per_cu->dwarf2_per_objfile;
13528 struct objfile *objfile = dwarf2_per_objfile->objfile;
13529 bfd *obfd = objfile->obfd;
13530 /* Base address selection entry. */
13531 gdb::optional<CORE_ADDR> base;
13532 const gdb_byte *buffer;
13533 CORE_ADDR baseaddr;
13534 bool overflow = false;
13535
13536 base = cu->base_address;
13537
13538 dwarf2_per_objfile->rnglists.read (objfile);
13539 if (offset >= dwarf2_per_objfile->rnglists.size)
13540 {
13541 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13542 offset);
13543 return false;
13544 }
13545 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13546
13547 baseaddr = objfile->text_section_offset ();
13548
13549 while (1)
13550 {
13551 /* Initialize it due to a false compiler warning. */
13552 CORE_ADDR range_beginning = 0, range_end = 0;
13553 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13554 + dwarf2_per_objfile->rnglists.size);
13555 unsigned int bytes_read;
13556
13557 if (buffer == buf_end)
13558 {
13559 overflow = true;
13560 break;
13561 }
13562 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13563 switch (rlet)
13564 {
13565 case DW_RLE_end_of_list:
13566 break;
13567 case DW_RLE_base_address:
13568 if (buffer + cu->header.addr_size > buf_end)
13569 {
13570 overflow = true;
13571 break;
13572 }
13573 base = cu->header.read_address (obfd, buffer, &bytes_read);
13574 buffer += bytes_read;
13575 break;
13576 case DW_RLE_start_length:
13577 if (buffer + cu->header.addr_size > buf_end)
13578 {
13579 overflow = true;
13580 break;
13581 }
13582 range_beginning = cu->header.read_address (obfd, buffer,
13583 &bytes_read);
13584 buffer += bytes_read;
13585 range_end = (range_beginning
13586 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13587 buffer += bytes_read;
13588 if (buffer > buf_end)
13589 {
13590 overflow = true;
13591 break;
13592 }
13593 break;
13594 case DW_RLE_offset_pair:
13595 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13596 buffer += bytes_read;
13597 if (buffer > buf_end)
13598 {
13599 overflow = true;
13600 break;
13601 }
13602 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13603 buffer += bytes_read;
13604 if (buffer > buf_end)
13605 {
13606 overflow = true;
13607 break;
13608 }
13609 break;
13610 case DW_RLE_start_end:
13611 if (buffer + 2 * cu->header.addr_size > buf_end)
13612 {
13613 overflow = true;
13614 break;
13615 }
13616 range_beginning = cu->header.read_address (obfd, buffer,
13617 &bytes_read);
13618 buffer += bytes_read;
13619 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13620 buffer += bytes_read;
13621 break;
13622 default:
13623 complaint (_("Invalid .debug_rnglists data (no base address)"));
13624 return false;
13625 }
13626 if (rlet == DW_RLE_end_of_list || overflow)
13627 break;
13628 if (rlet == DW_RLE_base_address)
13629 continue;
13630
13631 if (!base.has_value ())
13632 {
13633 /* We have no valid base address for the ranges
13634 data. */
13635 complaint (_("Invalid .debug_rnglists data (no base address)"));
13636 return false;
13637 }
13638
13639 if (range_beginning > range_end)
13640 {
13641 /* Inverted range entries are invalid. */
13642 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13643 return false;
13644 }
13645
13646 /* Empty range entries have no effect. */
13647 if (range_beginning == range_end)
13648 continue;
13649
13650 range_beginning += *base;
13651 range_end += *base;
13652
13653 /* A not-uncommon case of bad debug info.
13654 Don't pollute the addrmap with bad data. */
13655 if (range_beginning + baseaddr == 0
13656 && !dwarf2_per_objfile->has_section_at_zero)
13657 {
13658 complaint (_(".debug_rnglists entry has start address of zero"
13659 " [in module %s]"), objfile_name (objfile));
13660 continue;
13661 }
13662
13663 callback (range_beginning, range_end);
13664 }
13665
13666 if (overflow)
13667 {
13668 complaint (_("Offset %d is not terminated "
13669 "for DW_AT_ranges attribute"),
13670 offset);
13671 return false;
13672 }
13673
13674 return true;
13675 }
13676
13677 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13678 Callback's type should be:
13679 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13680 Return 1 if the attributes are present and valid, otherwise, return 0. */
13681
13682 template <typename Callback>
13683 static int
13684 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13685 Callback &&callback)
13686 {
13687 struct dwarf2_per_objfile *dwarf2_per_objfile
13688 = cu->per_cu->dwarf2_per_objfile;
13689 struct objfile *objfile = dwarf2_per_objfile->objfile;
13690 struct comp_unit_head *cu_header = &cu->header;
13691 bfd *obfd = objfile->obfd;
13692 unsigned int addr_size = cu_header->addr_size;
13693 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13694 /* Base address selection entry. */
13695 gdb::optional<CORE_ADDR> base;
13696 unsigned int dummy;
13697 const gdb_byte *buffer;
13698 CORE_ADDR baseaddr;
13699
13700 if (cu_header->version >= 5)
13701 return dwarf2_rnglists_process (offset, cu, callback);
13702
13703 base = cu->base_address;
13704
13705 dwarf2_per_objfile->ranges.read (objfile);
13706 if (offset >= dwarf2_per_objfile->ranges.size)
13707 {
13708 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13709 offset);
13710 return 0;
13711 }
13712 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13713
13714 baseaddr = objfile->text_section_offset ();
13715
13716 while (1)
13717 {
13718 CORE_ADDR range_beginning, range_end;
13719
13720 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13721 buffer += addr_size;
13722 range_end = cu->header.read_address (obfd, buffer, &dummy);
13723 buffer += addr_size;
13724 offset += 2 * addr_size;
13725
13726 /* An end of list marker is a pair of zero addresses. */
13727 if (range_beginning == 0 && range_end == 0)
13728 /* Found the end of list entry. */
13729 break;
13730
13731 /* Each base address selection entry is a pair of 2 values.
13732 The first is the largest possible address, the second is
13733 the base address. Check for a base address here. */
13734 if ((range_beginning & mask) == mask)
13735 {
13736 /* If we found the largest possible address, then we already
13737 have the base address in range_end. */
13738 base = range_end;
13739 continue;
13740 }
13741
13742 if (!base.has_value ())
13743 {
13744 /* We have no valid base address for the ranges
13745 data. */
13746 complaint (_("Invalid .debug_ranges data (no base address)"));
13747 return 0;
13748 }
13749
13750 if (range_beginning > range_end)
13751 {
13752 /* Inverted range entries are invalid. */
13753 complaint (_("Invalid .debug_ranges data (inverted range)"));
13754 return 0;
13755 }
13756
13757 /* Empty range entries have no effect. */
13758 if (range_beginning == range_end)
13759 continue;
13760
13761 range_beginning += *base;
13762 range_end += *base;
13763
13764 /* A not-uncommon case of bad debug info.
13765 Don't pollute the addrmap with bad data. */
13766 if (range_beginning + baseaddr == 0
13767 && !dwarf2_per_objfile->has_section_at_zero)
13768 {
13769 complaint (_(".debug_ranges entry has start address of zero"
13770 " [in module %s]"), objfile_name (objfile));
13771 continue;
13772 }
13773
13774 callback (range_beginning, range_end);
13775 }
13776
13777 return 1;
13778 }
13779
13780 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13781 Return 1 if the attributes are present and valid, otherwise, return 0.
13782 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13783
13784 static int
13785 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13786 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13787 dwarf2_psymtab *ranges_pst)
13788 {
13789 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13790 struct gdbarch *gdbarch = objfile->arch ();
13791 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13792 int low_set = 0;
13793 CORE_ADDR low = 0;
13794 CORE_ADDR high = 0;
13795 int retval;
13796
13797 retval = dwarf2_ranges_process (offset, cu,
13798 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13799 {
13800 if (ranges_pst != NULL)
13801 {
13802 CORE_ADDR lowpc;
13803 CORE_ADDR highpc;
13804
13805 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13806 range_beginning + baseaddr)
13807 - baseaddr);
13808 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13809 range_end + baseaddr)
13810 - baseaddr);
13811 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13812 lowpc, highpc - 1, ranges_pst);
13813 }
13814
13815 /* FIXME: This is recording everything as a low-high
13816 segment of consecutive addresses. We should have a
13817 data structure for discontiguous block ranges
13818 instead. */
13819 if (! low_set)
13820 {
13821 low = range_beginning;
13822 high = range_end;
13823 low_set = 1;
13824 }
13825 else
13826 {
13827 if (range_beginning < low)
13828 low = range_beginning;
13829 if (range_end > high)
13830 high = range_end;
13831 }
13832 });
13833 if (!retval)
13834 return 0;
13835
13836 if (! low_set)
13837 /* If the first entry is an end-of-list marker, the range
13838 describes an empty scope, i.e. no instructions. */
13839 return 0;
13840
13841 if (low_return)
13842 *low_return = low;
13843 if (high_return)
13844 *high_return = high;
13845 return 1;
13846 }
13847
13848 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13849 definition for the return value. *LOWPC and *HIGHPC are set iff
13850 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13851
13852 static enum pc_bounds_kind
13853 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13854 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13855 dwarf2_psymtab *pst)
13856 {
13857 struct dwarf2_per_objfile *dwarf2_per_objfile
13858 = cu->per_cu->dwarf2_per_objfile;
13859 struct attribute *attr;
13860 struct attribute *attr_high;
13861 CORE_ADDR low = 0;
13862 CORE_ADDR high = 0;
13863 enum pc_bounds_kind ret;
13864
13865 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13866 if (attr_high)
13867 {
13868 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13869 if (attr != nullptr)
13870 {
13871 low = attr->value_as_address ();
13872 high = attr_high->value_as_address ();
13873 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13874 high += low;
13875 }
13876 else
13877 /* Found high w/o low attribute. */
13878 return PC_BOUNDS_INVALID;
13879
13880 /* Found consecutive range of addresses. */
13881 ret = PC_BOUNDS_HIGH_LOW;
13882 }
13883 else
13884 {
13885 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13886 if (attr != NULL)
13887 {
13888 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13889 We take advantage of the fact that DW_AT_ranges does not appear
13890 in DW_TAG_compile_unit of DWO files. */
13891 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13892 unsigned int ranges_offset = (DW_UNSND (attr)
13893 + (need_ranges_base
13894 ? cu->ranges_base
13895 : 0));
13896
13897 /* Value of the DW_AT_ranges attribute is the offset in the
13898 .debug_ranges section. */
13899 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13900 return PC_BOUNDS_INVALID;
13901 /* Found discontinuous range of addresses. */
13902 ret = PC_BOUNDS_RANGES;
13903 }
13904 else
13905 return PC_BOUNDS_NOT_PRESENT;
13906 }
13907
13908 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13909 if (high <= low)
13910 return PC_BOUNDS_INVALID;
13911
13912 /* When using the GNU linker, .gnu.linkonce. sections are used to
13913 eliminate duplicate copies of functions and vtables and such.
13914 The linker will arbitrarily choose one and discard the others.
13915 The AT_*_pc values for such functions refer to local labels in
13916 these sections. If the section from that file was discarded, the
13917 labels are not in the output, so the relocs get a value of 0.
13918 If this is a discarded function, mark the pc bounds as invalid,
13919 so that GDB will ignore it. */
13920 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13921 return PC_BOUNDS_INVALID;
13922
13923 *lowpc = low;
13924 if (highpc)
13925 *highpc = high;
13926 return ret;
13927 }
13928
13929 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13930 its low and high PC addresses. Do nothing if these addresses could not
13931 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13932 and HIGHPC to the high address if greater than HIGHPC. */
13933
13934 static void
13935 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13936 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13937 struct dwarf2_cu *cu)
13938 {
13939 CORE_ADDR low, high;
13940 struct die_info *child = die->child;
13941
13942 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13943 {
13944 *lowpc = std::min (*lowpc, low);
13945 *highpc = std::max (*highpc, high);
13946 }
13947
13948 /* If the language does not allow nested subprograms (either inside
13949 subprograms or lexical blocks), we're done. */
13950 if (cu->language != language_ada)
13951 return;
13952
13953 /* Check all the children of the given DIE. If it contains nested
13954 subprograms, then check their pc bounds. Likewise, we need to
13955 check lexical blocks as well, as they may also contain subprogram
13956 definitions. */
13957 while (child && child->tag)
13958 {
13959 if (child->tag == DW_TAG_subprogram
13960 || child->tag == DW_TAG_lexical_block)
13961 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13962 child = child->sibling;
13963 }
13964 }
13965
13966 /* Get the low and high pc's represented by the scope DIE, and store
13967 them in *LOWPC and *HIGHPC. If the correct values can't be
13968 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13969
13970 static void
13971 get_scope_pc_bounds (struct die_info *die,
13972 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13973 struct dwarf2_cu *cu)
13974 {
13975 CORE_ADDR best_low = (CORE_ADDR) -1;
13976 CORE_ADDR best_high = (CORE_ADDR) 0;
13977 CORE_ADDR current_low, current_high;
13978
13979 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13980 >= PC_BOUNDS_RANGES)
13981 {
13982 best_low = current_low;
13983 best_high = current_high;
13984 }
13985 else
13986 {
13987 struct die_info *child = die->child;
13988
13989 while (child && child->tag)
13990 {
13991 switch (child->tag) {
13992 case DW_TAG_subprogram:
13993 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13994 break;
13995 case DW_TAG_namespace:
13996 case DW_TAG_module:
13997 /* FIXME: carlton/2004-01-16: Should we do this for
13998 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13999 that current GCC's always emit the DIEs corresponding
14000 to definitions of methods of classes as children of a
14001 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14002 the DIEs giving the declarations, which could be
14003 anywhere). But I don't see any reason why the
14004 standards says that they have to be there. */
14005 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14006
14007 if (current_low != ((CORE_ADDR) -1))
14008 {
14009 best_low = std::min (best_low, current_low);
14010 best_high = std::max (best_high, current_high);
14011 }
14012 break;
14013 default:
14014 /* Ignore. */
14015 break;
14016 }
14017
14018 child = child->sibling;
14019 }
14020 }
14021
14022 *lowpc = best_low;
14023 *highpc = best_high;
14024 }
14025
14026 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14027 in DIE. */
14028
14029 static void
14030 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14031 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14032 {
14033 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14034 struct gdbarch *gdbarch = objfile->arch ();
14035 struct attribute *attr;
14036 struct attribute *attr_high;
14037
14038 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14039 if (attr_high)
14040 {
14041 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14042 if (attr != nullptr)
14043 {
14044 CORE_ADDR low = attr->value_as_address ();
14045 CORE_ADDR high = attr_high->value_as_address ();
14046
14047 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14048 high += low;
14049
14050 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14051 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14052 cu->get_builder ()->record_block_range (block, low, high - 1);
14053 }
14054 }
14055
14056 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14057 if (attr != nullptr)
14058 {
14059 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14060 We take advantage of the fact that DW_AT_ranges does not appear
14061 in DW_TAG_compile_unit of DWO files. */
14062 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14063
14064 /* The value of the DW_AT_ranges attribute is the offset of the
14065 address range list in the .debug_ranges section. */
14066 unsigned long offset = (DW_UNSND (attr)
14067 + (need_ranges_base ? cu->ranges_base : 0));
14068
14069 std::vector<blockrange> blockvec;
14070 dwarf2_ranges_process (offset, cu,
14071 [&] (CORE_ADDR start, CORE_ADDR end)
14072 {
14073 start += baseaddr;
14074 end += baseaddr;
14075 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14076 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14077 cu->get_builder ()->record_block_range (block, start, end - 1);
14078 blockvec.emplace_back (start, end);
14079 });
14080
14081 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14082 }
14083 }
14084
14085 /* Check whether the producer field indicates either of GCC < 4.6, or the
14086 Intel C/C++ compiler, and cache the result in CU. */
14087
14088 static void
14089 check_producer (struct dwarf2_cu *cu)
14090 {
14091 int major, minor;
14092
14093 if (cu->producer == NULL)
14094 {
14095 /* For unknown compilers expect their behavior is DWARF version
14096 compliant.
14097
14098 GCC started to support .debug_types sections by -gdwarf-4 since
14099 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14100 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14101 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14102 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14103 }
14104 else if (producer_is_gcc (cu->producer, &major, &minor))
14105 {
14106 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14107 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14108 }
14109 else if (producer_is_icc (cu->producer, &major, &minor))
14110 {
14111 cu->producer_is_icc = true;
14112 cu->producer_is_icc_lt_14 = major < 14;
14113 }
14114 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14115 cu->producer_is_codewarrior = true;
14116 else
14117 {
14118 /* For other non-GCC compilers, expect their behavior is DWARF version
14119 compliant. */
14120 }
14121
14122 cu->checked_producer = true;
14123 }
14124
14125 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14126 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14127 during 4.6.0 experimental. */
14128
14129 static bool
14130 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14131 {
14132 if (!cu->checked_producer)
14133 check_producer (cu);
14134
14135 return cu->producer_is_gxx_lt_4_6;
14136 }
14137
14138
14139 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14140 with incorrect is_stmt attributes. */
14141
14142 static bool
14143 producer_is_codewarrior (struct dwarf2_cu *cu)
14144 {
14145 if (!cu->checked_producer)
14146 check_producer (cu);
14147
14148 return cu->producer_is_codewarrior;
14149 }
14150
14151 /* Return the default accessibility type if it is not overridden by
14152 DW_AT_accessibility. */
14153
14154 static enum dwarf_access_attribute
14155 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14156 {
14157 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14158 {
14159 /* The default DWARF 2 accessibility for members is public, the default
14160 accessibility for inheritance is private. */
14161
14162 if (die->tag != DW_TAG_inheritance)
14163 return DW_ACCESS_public;
14164 else
14165 return DW_ACCESS_private;
14166 }
14167 else
14168 {
14169 /* DWARF 3+ defines the default accessibility a different way. The same
14170 rules apply now for DW_TAG_inheritance as for the members and it only
14171 depends on the container kind. */
14172
14173 if (die->parent->tag == DW_TAG_class_type)
14174 return DW_ACCESS_private;
14175 else
14176 return DW_ACCESS_public;
14177 }
14178 }
14179
14180 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14181 offset. If the attribute was not found return 0, otherwise return
14182 1. If it was found but could not properly be handled, set *OFFSET
14183 to 0. */
14184
14185 static int
14186 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14187 LONGEST *offset)
14188 {
14189 struct attribute *attr;
14190
14191 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14192 if (attr != NULL)
14193 {
14194 *offset = 0;
14195
14196 /* Note that we do not check for a section offset first here.
14197 This is because DW_AT_data_member_location is new in DWARF 4,
14198 so if we see it, we can assume that a constant form is really
14199 a constant and not a section offset. */
14200 if (attr->form_is_constant ())
14201 *offset = attr->constant_value (0);
14202 else if (attr->form_is_section_offset ())
14203 dwarf2_complex_location_expr_complaint ();
14204 else if (attr->form_is_block ())
14205 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14206 else
14207 dwarf2_complex_location_expr_complaint ();
14208
14209 return 1;
14210 }
14211
14212 return 0;
14213 }
14214
14215 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14216
14217 static void
14218 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14219 struct field *field)
14220 {
14221 struct attribute *attr;
14222
14223 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14224 if (attr != NULL)
14225 {
14226 if (attr->form_is_constant ())
14227 {
14228 LONGEST offset = attr->constant_value (0);
14229 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14230 }
14231 else if (attr->form_is_section_offset ())
14232 dwarf2_complex_location_expr_complaint ();
14233 else if (attr->form_is_block ())
14234 {
14235 bool handled;
14236 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14237 if (handled)
14238 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14239 else
14240 {
14241 struct objfile *objfile
14242 = cu->per_cu->dwarf2_per_objfile->objfile;
14243 struct dwarf2_locexpr_baton *dlbaton
14244 = XOBNEW (&objfile->objfile_obstack,
14245 struct dwarf2_locexpr_baton);
14246 dlbaton->data = DW_BLOCK (attr)->data;
14247 dlbaton->size = DW_BLOCK (attr)->size;
14248 /* When using this baton, we want to compute the address
14249 of the field, not the value. This is why
14250 is_reference is set to false here. */
14251 dlbaton->is_reference = false;
14252 dlbaton->per_cu = cu->per_cu;
14253
14254 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14255 }
14256 }
14257 else
14258 dwarf2_complex_location_expr_complaint ();
14259 }
14260 }
14261
14262 /* Add an aggregate field to the field list. */
14263
14264 static void
14265 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14266 struct dwarf2_cu *cu)
14267 {
14268 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14269 struct gdbarch *gdbarch = objfile->arch ();
14270 struct nextfield *new_field;
14271 struct attribute *attr;
14272 struct field *fp;
14273 const char *fieldname = "";
14274
14275 if (die->tag == DW_TAG_inheritance)
14276 {
14277 fip->baseclasses.emplace_back ();
14278 new_field = &fip->baseclasses.back ();
14279 }
14280 else
14281 {
14282 fip->fields.emplace_back ();
14283 new_field = &fip->fields.back ();
14284 }
14285
14286 new_field->offset = die->sect_off;
14287
14288 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14289 if (attr != nullptr)
14290 new_field->accessibility = DW_UNSND (attr);
14291 else
14292 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14293 if (new_field->accessibility != DW_ACCESS_public)
14294 fip->non_public_fields = 1;
14295
14296 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14297 if (attr != nullptr)
14298 new_field->virtuality = DW_UNSND (attr);
14299 else
14300 new_field->virtuality = DW_VIRTUALITY_none;
14301
14302 fp = &new_field->field;
14303
14304 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14305 {
14306 /* Data member other than a C++ static data member. */
14307
14308 /* Get type of field. */
14309 fp->type = die_type (die, cu);
14310
14311 SET_FIELD_BITPOS (*fp, 0);
14312
14313 /* Get bit size of field (zero if none). */
14314 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14315 if (attr != nullptr)
14316 {
14317 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14318 }
14319 else
14320 {
14321 FIELD_BITSIZE (*fp) = 0;
14322 }
14323
14324 /* Get bit offset of field. */
14325 handle_data_member_location (die, cu, fp);
14326 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14327 if (attr != nullptr)
14328 {
14329 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14330 {
14331 /* For big endian bits, the DW_AT_bit_offset gives the
14332 additional bit offset from the MSB of the containing
14333 anonymous object to the MSB of the field. We don't
14334 have to do anything special since we don't need to
14335 know the size of the anonymous object. */
14336 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14337 }
14338 else
14339 {
14340 /* For little endian bits, compute the bit offset to the
14341 MSB of the anonymous object, subtract off the number of
14342 bits from the MSB of the field to the MSB of the
14343 object, and then subtract off the number of bits of
14344 the field itself. The result is the bit offset of
14345 the LSB of the field. */
14346 int anonymous_size;
14347 int bit_offset = DW_UNSND (attr);
14348
14349 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14350 if (attr != nullptr)
14351 {
14352 /* The size of the anonymous object containing
14353 the bit field is explicit, so use the
14354 indicated size (in bytes). */
14355 anonymous_size = DW_UNSND (attr);
14356 }
14357 else
14358 {
14359 /* The size of the anonymous object containing
14360 the bit field must be inferred from the type
14361 attribute of the data member containing the
14362 bit field. */
14363 anonymous_size = TYPE_LENGTH (fp->type);
14364 }
14365 SET_FIELD_BITPOS (*fp,
14366 (FIELD_BITPOS (*fp)
14367 + anonymous_size * bits_per_byte
14368 - bit_offset - FIELD_BITSIZE (*fp)));
14369 }
14370 }
14371 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14372 if (attr != NULL)
14373 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14374 + attr->constant_value (0)));
14375
14376 /* Get name of field. */
14377 fieldname = dwarf2_name (die, cu);
14378 if (fieldname == NULL)
14379 fieldname = "";
14380
14381 /* The name is already allocated along with this objfile, so we don't
14382 need to duplicate it for the type. */
14383 fp->name = fieldname;
14384
14385 /* Change accessibility for artificial fields (e.g. virtual table
14386 pointer or virtual base class pointer) to private. */
14387 if (dwarf2_attr (die, DW_AT_artificial, cu))
14388 {
14389 FIELD_ARTIFICIAL (*fp) = 1;
14390 new_field->accessibility = DW_ACCESS_private;
14391 fip->non_public_fields = 1;
14392 }
14393 }
14394 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14395 {
14396 /* C++ static member. */
14397
14398 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14399 is a declaration, but all versions of G++ as of this writing
14400 (so through at least 3.2.1) incorrectly generate
14401 DW_TAG_variable tags. */
14402
14403 const char *physname;
14404
14405 /* Get name of field. */
14406 fieldname = dwarf2_name (die, cu);
14407 if (fieldname == NULL)
14408 return;
14409
14410 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14411 if (attr
14412 /* Only create a symbol if this is an external value.
14413 new_symbol checks this and puts the value in the global symbol
14414 table, which we want. If it is not external, new_symbol
14415 will try to put the value in cu->list_in_scope which is wrong. */
14416 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14417 {
14418 /* A static const member, not much different than an enum as far as
14419 we're concerned, except that we can support more types. */
14420 new_symbol (die, NULL, cu);
14421 }
14422
14423 /* Get physical name. */
14424 physname = dwarf2_physname (fieldname, die, cu);
14425
14426 /* The name is already allocated along with this objfile, so we don't
14427 need to duplicate it for the type. */
14428 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14429 FIELD_TYPE (*fp) = die_type (die, cu);
14430 FIELD_NAME (*fp) = fieldname;
14431 }
14432 else if (die->tag == DW_TAG_inheritance)
14433 {
14434 /* C++ base class field. */
14435 handle_data_member_location (die, cu, fp);
14436 FIELD_BITSIZE (*fp) = 0;
14437 FIELD_TYPE (*fp) = die_type (die, cu);
14438 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14439 }
14440 else
14441 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14442 }
14443
14444 /* Can the type given by DIE define another type? */
14445
14446 static bool
14447 type_can_define_types (const struct die_info *die)
14448 {
14449 switch (die->tag)
14450 {
14451 case DW_TAG_typedef:
14452 case DW_TAG_class_type:
14453 case DW_TAG_structure_type:
14454 case DW_TAG_union_type:
14455 case DW_TAG_enumeration_type:
14456 return true;
14457
14458 default:
14459 return false;
14460 }
14461 }
14462
14463 /* Add a type definition defined in the scope of the FIP's class. */
14464
14465 static void
14466 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14467 struct dwarf2_cu *cu)
14468 {
14469 struct decl_field fp;
14470 memset (&fp, 0, sizeof (fp));
14471
14472 gdb_assert (type_can_define_types (die));
14473
14474 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14475 fp.name = dwarf2_name (die, cu);
14476 fp.type = read_type_die (die, cu);
14477
14478 /* Save accessibility. */
14479 enum dwarf_access_attribute accessibility;
14480 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14481 if (attr != NULL)
14482 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14483 else
14484 accessibility = dwarf2_default_access_attribute (die, cu);
14485 switch (accessibility)
14486 {
14487 case DW_ACCESS_public:
14488 /* The assumed value if neither private nor protected. */
14489 break;
14490 case DW_ACCESS_private:
14491 fp.is_private = 1;
14492 break;
14493 case DW_ACCESS_protected:
14494 fp.is_protected = 1;
14495 break;
14496 default:
14497 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14498 }
14499
14500 if (die->tag == DW_TAG_typedef)
14501 fip->typedef_field_list.push_back (fp);
14502 else
14503 fip->nested_types_list.push_back (fp);
14504 }
14505
14506 /* A convenience typedef that's used when finding the discriminant
14507 field for a variant part. */
14508 typedef std::unordered_map<sect_offset, int> offset_map_type;
14509
14510 /* Compute the discriminant range for a given variant. OBSTACK is
14511 where the results will be stored. VARIANT is the variant to
14512 process. IS_UNSIGNED indicates whether the discriminant is signed
14513 or unsigned. */
14514
14515 static const gdb::array_view<discriminant_range>
14516 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14517 bool is_unsigned)
14518 {
14519 std::vector<discriminant_range> ranges;
14520
14521 if (variant.default_branch)
14522 return {};
14523
14524 if (variant.discr_list_data == nullptr)
14525 {
14526 discriminant_range r
14527 = {variant.discriminant_value, variant.discriminant_value};
14528 ranges.push_back (r);
14529 }
14530 else
14531 {
14532 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14533 variant.discr_list_data->size);
14534 while (!data.empty ())
14535 {
14536 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14537 {
14538 complaint (_("invalid discriminant marker: %d"), data[0]);
14539 break;
14540 }
14541 bool is_range = data[0] == DW_DSC_range;
14542 data = data.slice (1);
14543
14544 ULONGEST low, high;
14545 unsigned int bytes_read;
14546
14547 if (data.empty ())
14548 {
14549 complaint (_("DW_AT_discr_list missing low value"));
14550 break;
14551 }
14552 if (is_unsigned)
14553 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14554 else
14555 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14556 &bytes_read);
14557 data = data.slice (bytes_read);
14558
14559 if (is_range)
14560 {
14561 if (data.empty ())
14562 {
14563 complaint (_("DW_AT_discr_list missing high value"));
14564 break;
14565 }
14566 if (is_unsigned)
14567 high = read_unsigned_leb128 (nullptr, data.data (),
14568 &bytes_read);
14569 else
14570 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14571 &bytes_read);
14572 data = data.slice (bytes_read);
14573 }
14574 else
14575 high = low;
14576
14577 ranges.push_back ({ low, high });
14578 }
14579 }
14580
14581 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14582 ranges.size ());
14583 std::copy (ranges.begin (), ranges.end (), result);
14584 return gdb::array_view<discriminant_range> (result, ranges.size ());
14585 }
14586
14587 static const gdb::array_view<variant_part> create_variant_parts
14588 (struct obstack *obstack,
14589 const offset_map_type &offset_map,
14590 struct field_info *fi,
14591 const std::vector<variant_part_builder> &variant_parts);
14592
14593 /* Fill in a "struct variant" for a given variant field. RESULT is
14594 the variant to fill in. OBSTACK is where any needed allocations
14595 will be done. OFFSET_MAP holds the mapping from section offsets to
14596 fields for the type. FI describes the fields of the type we're
14597 processing. FIELD is the variant field we're converting. */
14598
14599 static void
14600 create_one_variant (variant &result, struct obstack *obstack,
14601 const offset_map_type &offset_map,
14602 struct field_info *fi, const variant_field &field)
14603 {
14604 result.discriminants = convert_variant_range (obstack, field, false);
14605 result.first_field = field.first_field + fi->baseclasses.size ();
14606 result.last_field = field.last_field + fi->baseclasses.size ();
14607 result.parts = create_variant_parts (obstack, offset_map, fi,
14608 field.variant_parts);
14609 }
14610
14611 /* Fill in a "struct variant_part" for a given variant part. RESULT
14612 is the variant part to fill in. OBSTACK is where any needed
14613 allocations will be done. OFFSET_MAP holds the mapping from
14614 section offsets to fields for the type. FI describes the fields of
14615 the type we're processing. BUILDER is the variant part to be
14616 converted. */
14617
14618 static void
14619 create_one_variant_part (variant_part &result,
14620 struct obstack *obstack,
14621 const offset_map_type &offset_map,
14622 struct field_info *fi,
14623 const variant_part_builder &builder)
14624 {
14625 auto iter = offset_map.find (builder.discriminant_offset);
14626 if (iter == offset_map.end ())
14627 {
14628 result.discriminant_index = -1;
14629 /* Doesn't matter. */
14630 result.is_unsigned = false;
14631 }
14632 else
14633 {
14634 result.discriminant_index = iter->second;
14635 result.is_unsigned
14636 = TYPE_UNSIGNED (FIELD_TYPE
14637 (fi->fields[result.discriminant_index].field));
14638 }
14639
14640 size_t n = builder.variants.size ();
14641 variant *output = new (obstack) variant[n];
14642 for (size_t i = 0; i < n; ++i)
14643 create_one_variant (output[i], obstack, offset_map, fi,
14644 builder.variants[i]);
14645
14646 result.variants = gdb::array_view<variant> (output, n);
14647 }
14648
14649 /* Create a vector of variant parts that can be attached to a type.
14650 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14651 holds the mapping from section offsets to fields for the type. FI
14652 describes the fields of the type we're processing. VARIANT_PARTS
14653 is the vector to convert. */
14654
14655 static const gdb::array_view<variant_part>
14656 create_variant_parts (struct obstack *obstack,
14657 const offset_map_type &offset_map,
14658 struct field_info *fi,
14659 const std::vector<variant_part_builder> &variant_parts)
14660 {
14661 if (variant_parts.empty ())
14662 return {};
14663
14664 size_t n = variant_parts.size ();
14665 variant_part *result = new (obstack) variant_part[n];
14666 for (size_t i = 0; i < n; ++i)
14667 create_one_variant_part (result[i], obstack, offset_map, fi,
14668 variant_parts[i]);
14669
14670 return gdb::array_view<variant_part> (result, n);
14671 }
14672
14673 /* Compute the variant part vector for FIP, attaching it to TYPE when
14674 done. */
14675
14676 static void
14677 add_variant_property (struct field_info *fip, struct type *type,
14678 struct dwarf2_cu *cu)
14679 {
14680 /* Map section offsets of fields to their field index. Note the
14681 field index here does not take the number of baseclasses into
14682 account. */
14683 offset_map_type offset_map;
14684 for (int i = 0; i < fip->fields.size (); ++i)
14685 offset_map[fip->fields[i].offset] = i;
14686
14687 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14688 gdb::array_view<variant_part> parts
14689 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14690 fip->variant_parts);
14691
14692 struct dynamic_prop prop;
14693 prop.kind = PROP_VARIANT_PARTS;
14694 prop.data.variant_parts
14695 = ((gdb::array_view<variant_part> *)
14696 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14697
14698 add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop, type);
14699 }
14700
14701 /* Create the vector of fields, and attach it to the type. */
14702
14703 static void
14704 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14705 struct dwarf2_cu *cu)
14706 {
14707 int nfields = fip->nfields ();
14708
14709 /* Record the field count, allocate space for the array of fields,
14710 and create blank accessibility bitfields if necessary. */
14711 TYPE_NFIELDS (type) = nfields;
14712 TYPE_FIELDS (type) = (struct field *)
14713 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14714
14715 if (fip->non_public_fields && cu->language != language_ada)
14716 {
14717 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14718
14719 TYPE_FIELD_PRIVATE_BITS (type) =
14720 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14721 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14722
14723 TYPE_FIELD_PROTECTED_BITS (type) =
14724 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14725 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14726
14727 TYPE_FIELD_IGNORE_BITS (type) =
14728 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14729 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14730 }
14731
14732 /* If the type has baseclasses, allocate and clear a bit vector for
14733 TYPE_FIELD_VIRTUAL_BITS. */
14734 if (!fip->baseclasses.empty () && cu->language != language_ada)
14735 {
14736 int num_bytes = B_BYTES (fip->baseclasses.size ());
14737 unsigned char *pointer;
14738
14739 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14740 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14741 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14742 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14743 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14744 }
14745
14746 if (!fip->variant_parts.empty ())
14747 add_variant_property (fip, type, cu);
14748
14749 /* Copy the saved-up fields into the field vector. */
14750 for (int i = 0; i < nfields; ++i)
14751 {
14752 struct nextfield &field
14753 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14754 : fip->fields[i - fip->baseclasses.size ()]);
14755
14756 TYPE_FIELD (type, i) = field.field;
14757 switch (field.accessibility)
14758 {
14759 case DW_ACCESS_private:
14760 if (cu->language != language_ada)
14761 SET_TYPE_FIELD_PRIVATE (type, i);
14762 break;
14763
14764 case DW_ACCESS_protected:
14765 if (cu->language != language_ada)
14766 SET_TYPE_FIELD_PROTECTED (type, i);
14767 break;
14768
14769 case DW_ACCESS_public:
14770 break;
14771
14772 default:
14773 /* Unknown accessibility. Complain and treat it as public. */
14774 {
14775 complaint (_("unsupported accessibility %d"),
14776 field.accessibility);
14777 }
14778 break;
14779 }
14780 if (i < fip->baseclasses.size ())
14781 {
14782 switch (field.virtuality)
14783 {
14784 case DW_VIRTUALITY_virtual:
14785 case DW_VIRTUALITY_pure_virtual:
14786 if (cu->language == language_ada)
14787 error (_("unexpected virtuality in component of Ada type"));
14788 SET_TYPE_FIELD_VIRTUAL (type, i);
14789 break;
14790 }
14791 }
14792 }
14793 }
14794
14795 /* Return true if this member function is a constructor, false
14796 otherwise. */
14797
14798 static int
14799 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14800 {
14801 const char *fieldname;
14802 const char *type_name;
14803 int len;
14804
14805 if (die->parent == NULL)
14806 return 0;
14807
14808 if (die->parent->tag != DW_TAG_structure_type
14809 && die->parent->tag != DW_TAG_union_type
14810 && die->parent->tag != DW_TAG_class_type)
14811 return 0;
14812
14813 fieldname = dwarf2_name (die, cu);
14814 type_name = dwarf2_name (die->parent, cu);
14815 if (fieldname == NULL || type_name == NULL)
14816 return 0;
14817
14818 len = strlen (fieldname);
14819 return (strncmp (fieldname, type_name, len) == 0
14820 && (type_name[len] == '\0' || type_name[len] == '<'));
14821 }
14822
14823 /* Check if the given VALUE is a recognized enum
14824 dwarf_defaulted_attribute constant according to DWARF5 spec,
14825 Table 7.24. */
14826
14827 static bool
14828 is_valid_DW_AT_defaulted (ULONGEST value)
14829 {
14830 switch (value)
14831 {
14832 case DW_DEFAULTED_no:
14833 case DW_DEFAULTED_in_class:
14834 case DW_DEFAULTED_out_of_class:
14835 return true;
14836 }
14837
14838 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14839 return false;
14840 }
14841
14842 /* Add a member function to the proper fieldlist. */
14843
14844 static void
14845 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14846 struct type *type, struct dwarf2_cu *cu)
14847 {
14848 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14849 struct attribute *attr;
14850 int i;
14851 struct fnfieldlist *flp = nullptr;
14852 struct fn_field *fnp;
14853 const char *fieldname;
14854 struct type *this_type;
14855 enum dwarf_access_attribute accessibility;
14856
14857 if (cu->language == language_ada)
14858 error (_("unexpected member function in Ada type"));
14859
14860 /* Get name of member function. */
14861 fieldname = dwarf2_name (die, cu);
14862 if (fieldname == NULL)
14863 return;
14864
14865 /* Look up member function name in fieldlist. */
14866 for (i = 0; i < fip->fnfieldlists.size (); i++)
14867 {
14868 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14869 {
14870 flp = &fip->fnfieldlists[i];
14871 break;
14872 }
14873 }
14874
14875 /* Create a new fnfieldlist if necessary. */
14876 if (flp == nullptr)
14877 {
14878 fip->fnfieldlists.emplace_back ();
14879 flp = &fip->fnfieldlists.back ();
14880 flp->name = fieldname;
14881 i = fip->fnfieldlists.size () - 1;
14882 }
14883
14884 /* Create a new member function field and add it to the vector of
14885 fnfieldlists. */
14886 flp->fnfields.emplace_back ();
14887 fnp = &flp->fnfields.back ();
14888
14889 /* Delay processing of the physname until later. */
14890 if (cu->language == language_cplus)
14891 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14892 die, cu);
14893 else
14894 {
14895 const char *physname = dwarf2_physname (fieldname, die, cu);
14896 fnp->physname = physname ? physname : "";
14897 }
14898
14899 fnp->type = alloc_type (objfile);
14900 this_type = read_type_die (die, cu);
14901 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14902 {
14903 int nparams = TYPE_NFIELDS (this_type);
14904
14905 /* TYPE is the domain of this method, and THIS_TYPE is the type
14906 of the method itself (TYPE_CODE_METHOD). */
14907 smash_to_method_type (fnp->type, type,
14908 TYPE_TARGET_TYPE (this_type),
14909 TYPE_FIELDS (this_type),
14910 TYPE_NFIELDS (this_type),
14911 TYPE_VARARGS (this_type));
14912
14913 /* Handle static member functions.
14914 Dwarf2 has no clean way to discern C++ static and non-static
14915 member functions. G++ helps GDB by marking the first
14916 parameter for non-static member functions (which is the this
14917 pointer) as artificial. We obtain this information from
14918 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14919 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14920 fnp->voffset = VOFFSET_STATIC;
14921 }
14922 else
14923 complaint (_("member function type missing for '%s'"),
14924 dwarf2_full_name (fieldname, die, cu));
14925
14926 /* Get fcontext from DW_AT_containing_type if present. */
14927 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14928 fnp->fcontext = die_containing_type (die, cu);
14929
14930 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14931 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14932
14933 /* Get accessibility. */
14934 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14935 if (attr != nullptr)
14936 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14937 else
14938 accessibility = dwarf2_default_access_attribute (die, cu);
14939 switch (accessibility)
14940 {
14941 case DW_ACCESS_private:
14942 fnp->is_private = 1;
14943 break;
14944 case DW_ACCESS_protected:
14945 fnp->is_protected = 1;
14946 break;
14947 }
14948
14949 /* Check for artificial methods. */
14950 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14951 if (attr && DW_UNSND (attr) != 0)
14952 fnp->is_artificial = 1;
14953
14954 /* Check for defaulted methods. */
14955 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14956 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14957 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14958
14959 /* Check for deleted methods. */
14960 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14961 if (attr != nullptr && DW_UNSND (attr) != 0)
14962 fnp->is_deleted = 1;
14963
14964 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14965
14966 /* Get index in virtual function table if it is a virtual member
14967 function. For older versions of GCC, this is an offset in the
14968 appropriate virtual table, as specified by DW_AT_containing_type.
14969 For everyone else, it is an expression to be evaluated relative
14970 to the object address. */
14971
14972 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14973 if (attr != nullptr)
14974 {
14975 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14976 {
14977 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14978 {
14979 /* Old-style GCC. */
14980 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14981 }
14982 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14983 || (DW_BLOCK (attr)->size > 1
14984 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14985 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14986 {
14987 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14988 if ((fnp->voffset % cu->header.addr_size) != 0)
14989 dwarf2_complex_location_expr_complaint ();
14990 else
14991 fnp->voffset /= cu->header.addr_size;
14992 fnp->voffset += 2;
14993 }
14994 else
14995 dwarf2_complex_location_expr_complaint ();
14996
14997 if (!fnp->fcontext)
14998 {
14999 /* If there is no `this' field and no DW_AT_containing_type,
15000 we cannot actually find a base class context for the
15001 vtable! */
15002 if (TYPE_NFIELDS (this_type) == 0
15003 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15004 {
15005 complaint (_("cannot determine context for virtual member "
15006 "function \"%s\" (offset %s)"),
15007 fieldname, sect_offset_str (die->sect_off));
15008 }
15009 else
15010 {
15011 fnp->fcontext
15012 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15013 }
15014 }
15015 }
15016 else if (attr->form_is_section_offset ())
15017 {
15018 dwarf2_complex_location_expr_complaint ();
15019 }
15020 else
15021 {
15022 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15023 fieldname);
15024 }
15025 }
15026 else
15027 {
15028 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15029 if (attr && DW_UNSND (attr))
15030 {
15031 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15032 complaint (_("Member function \"%s\" (offset %s) is virtual "
15033 "but the vtable offset is not specified"),
15034 fieldname, sect_offset_str (die->sect_off));
15035 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15036 TYPE_CPLUS_DYNAMIC (type) = 1;
15037 }
15038 }
15039 }
15040
15041 /* Create the vector of member function fields, and attach it to the type. */
15042
15043 static void
15044 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15045 struct dwarf2_cu *cu)
15046 {
15047 if (cu->language == language_ada)
15048 error (_("unexpected member functions in Ada type"));
15049
15050 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15051 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15052 TYPE_ALLOC (type,
15053 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15054
15055 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15056 {
15057 struct fnfieldlist &nf = fip->fnfieldlists[i];
15058 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15059
15060 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15061 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15062 fn_flp->fn_fields = (struct fn_field *)
15063 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15064
15065 for (int k = 0; k < nf.fnfields.size (); ++k)
15066 fn_flp->fn_fields[k] = nf.fnfields[k];
15067 }
15068
15069 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15070 }
15071
15072 /* Returns non-zero if NAME is the name of a vtable member in CU's
15073 language, zero otherwise. */
15074 static int
15075 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15076 {
15077 static const char vptr[] = "_vptr";
15078
15079 /* Look for the C++ form of the vtable. */
15080 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15081 return 1;
15082
15083 return 0;
15084 }
15085
15086 /* GCC outputs unnamed structures that are really pointers to member
15087 functions, with the ABI-specified layout. If TYPE describes
15088 such a structure, smash it into a member function type.
15089
15090 GCC shouldn't do this; it should just output pointer to member DIEs.
15091 This is GCC PR debug/28767. */
15092
15093 static void
15094 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15095 {
15096 struct type *pfn_type, *self_type, *new_type;
15097
15098 /* Check for a structure with no name and two children. */
15099 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15100 return;
15101
15102 /* Check for __pfn and __delta members. */
15103 if (TYPE_FIELD_NAME (type, 0) == NULL
15104 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15105 || TYPE_FIELD_NAME (type, 1) == NULL
15106 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15107 return;
15108
15109 /* Find the type of the method. */
15110 pfn_type = TYPE_FIELD_TYPE (type, 0);
15111 if (pfn_type == NULL
15112 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15113 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15114 return;
15115
15116 /* Look for the "this" argument. */
15117 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15118 if (TYPE_NFIELDS (pfn_type) == 0
15119 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15120 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15121 return;
15122
15123 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15124 new_type = alloc_type (objfile);
15125 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15126 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15127 TYPE_VARARGS (pfn_type));
15128 smash_to_methodptr_type (type, new_type);
15129 }
15130
15131 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15132 appropriate error checking and issuing complaints if there is a
15133 problem. */
15134
15135 static ULONGEST
15136 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15137 {
15138 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15139
15140 if (attr == nullptr)
15141 return 0;
15142
15143 if (!attr->form_is_constant ())
15144 {
15145 complaint (_("DW_AT_alignment must have constant form"
15146 " - DIE at %s [in module %s]"),
15147 sect_offset_str (die->sect_off),
15148 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15149 return 0;
15150 }
15151
15152 ULONGEST align;
15153 if (attr->form == DW_FORM_sdata)
15154 {
15155 LONGEST val = DW_SND (attr);
15156 if (val < 0)
15157 {
15158 complaint (_("DW_AT_alignment value must not be negative"
15159 " - DIE at %s [in module %s]"),
15160 sect_offset_str (die->sect_off),
15161 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15162 return 0;
15163 }
15164 align = val;
15165 }
15166 else
15167 align = DW_UNSND (attr);
15168
15169 if (align == 0)
15170 {
15171 complaint (_("DW_AT_alignment value must not be zero"
15172 " - DIE at %s [in module %s]"),
15173 sect_offset_str (die->sect_off),
15174 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15175 return 0;
15176 }
15177 if ((align & (align - 1)) != 0)
15178 {
15179 complaint (_("DW_AT_alignment value must be a power of 2"
15180 " - DIE at %s [in module %s]"),
15181 sect_offset_str (die->sect_off),
15182 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15183 return 0;
15184 }
15185
15186 return align;
15187 }
15188
15189 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15190 the alignment for TYPE. */
15191
15192 static void
15193 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15194 struct type *type)
15195 {
15196 if (!set_type_align (type, get_alignment (cu, die)))
15197 complaint (_("DW_AT_alignment value too large"
15198 " - DIE at %s [in module %s]"),
15199 sect_offset_str (die->sect_off),
15200 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15201 }
15202
15203 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15204 constant for a type, according to DWARF5 spec, Table 5.5. */
15205
15206 static bool
15207 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15208 {
15209 switch (value)
15210 {
15211 case DW_CC_normal:
15212 case DW_CC_pass_by_reference:
15213 case DW_CC_pass_by_value:
15214 return true;
15215
15216 default:
15217 complaint (_("unrecognized DW_AT_calling_convention value "
15218 "(%s) for a type"), pulongest (value));
15219 return false;
15220 }
15221 }
15222
15223 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15224 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15225 also according to GNU-specific values (see include/dwarf2.h). */
15226
15227 static bool
15228 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15229 {
15230 switch (value)
15231 {
15232 case DW_CC_normal:
15233 case DW_CC_program:
15234 case DW_CC_nocall:
15235 return true;
15236
15237 case DW_CC_GNU_renesas_sh:
15238 case DW_CC_GNU_borland_fastcall_i386:
15239 case DW_CC_GDB_IBM_OpenCL:
15240 return true;
15241
15242 default:
15243 complaint (_("unrecognized DW_AT_calling_convention value "
15244 "(%s) for a subroutine"), pulongest (value));
15245 return false;
15246 }
15247 }
15248
15249 /* Called when we find the DIE that starts a structure or union scope
15250 (definition) to create a type for the structure or union. Fill in
15251 the type's name and general properties; the members will not be
15252 processed until process_structure_scope. A symbol table entry for
15253 the type will also not be done until process_structure_scope (assuming
15254 the type has a name).
15255
15256 NOTE: we need to call these functions regardless of whether or not the
15257 DIE has a DW_AT_name attribute, since it might be an anonymous
15258 structure or union. This gets the type entered into our set of
15259 user defined types. */
15260
15261 static struct type *
15262 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15263 {
15264 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15265 struct type *type;
15266 struct attribute *attr;
15267 const char *name;
15268
15269 /* If the definition of this type lives in .debug_types, read that type.
15270 Don't follow DW_AT_specification though, that will take us back up
15271 the chain and we want to go down. */
15272 attr = die->attr (DW_AT_signature);
15273 if (attr != nullptr)
15274 {
15275 type = get_DW_AT_signature_type (die, attr, cu);
15276
15277 /* The type's CU may not be the same as CU.
15278 Ensure TYPE is recorded with CU in die_type_hash. */
15279 return set_die_type (die, type, cu);
15280 }
15281
15282 type = alloc_type (objfile);
15283 INIT_CPLUS_SPECIFIC (type);
15284
15285 name = dwarf2_name (die, cu);
15286 if (name != NULL)
15287 {
15288 if (cu->language == language_cplus
15289 || cu->language == language_d
15290 || cu->language == language_rust)
15291 {
15292 const char *full_name = dwarf2_full_name (name, die, cu);
15293
15294 /* dwarf2_full_name might have already finished building the DIE's
15295 type. If so, there is no need to continue. */
15296 if (get_die_type (die, cu) != NULL)
15297 return get_die_type (die, cu);
15298
15299 TYPE_NAME (type) = full_name;
15300 }
15301 else
15302 {
15303 /* The name is already allocated along with this objfile, so
15304 we don't need to duplicate it for the type. */
15305 TYPE_NAME (type) = name;
15306 }
15307 }
15308
15309 if (die->tag == DW_TAG_structure_type)
15310 {
15311 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15312 }
15313 else if (die->tag == DW_TAG_union_type)
15314 {
15315 TYPE_CODE (type) = TYPE_CODE_UNION;
15316 }
15317 else
15318 {
15319 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15320 }
15321
15322 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15323 TYPE_DECLARED_CLASS (type) = 1;
15324
15325 /* Store the calling convention in the type if it's available in
15326 the die. Otherwise the calling convention remains set to
15327 the default value DW_CC_normal. */
15328 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15329 if (attr != nullptr
15330 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15331 {
15332 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15333 TYPE_CPLUS_CALLING_CONVENTION (type)
15334 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15335 }
15336
15337 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15338 if (attr != nullptr)
15339 {
15340 if (attr->form_is_constant ())
15341 TYPE_LENGTH (type) = DW_UNSND (attr);
15342 else
15343 {
15344 struct dynamic_prop prop;
15345 if (attr_to_dynamic_prop (attr, die, cu, &prop,
15346 cu->per_cu->addr_type ()))
15347 add_dyn_prop (DYN_PROP_BYTE_SIZE, prop, type);
15348 TYPE_LENGTH (type) = 0;
15349 }
15350 }
15351 else
15352 {
15353 TYPE_LENGTH (type) = 0;
15354 }
15355
15356 maybe_set_alignment (cu, die, type);
15357
15358 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15359 {
15360 /* ICC<14 does not output the required DW_AT_declaration on
15361 incomplete types, but gives them a size of zero. */
15362 TYPE_STUB (type) = 1;
15363 }
15364 else
15365 TYPE_STUB_SUPPORTED (type) = 1;
15366
15367 if (die_is_declaration (die, cu))
15368 TYPE_STUB (type) = 1;
15369 else if (attr == NULL && die->child == NULL
15370 && producer_is_realview (cu->producer))
15371 /* RealView does not output the required DW_AT_declaration
15372 on incomplete types. */
15373 TYPE_STUB (type) = 1;
15374
15375 /* We need to add the type field to the die immediately so we don't
15376 infinitely recurse when dealing with pointers to the structure
15377 type within the structure itself. */
15378 set_die_type (die, type, cu);
15379
15380 /* set_die_type should be already done. */
15381 set_descriptive_type (type, die, cu);
15382
15383 return type;
15384 }
15385
15386 static void handle_struct_member_die
15387 (struct die_info *child_die,
15388 struct type *type,
15389 struct field_info *fi,
15390 std::vector<struct symbol *> *template_args,
15391 struct dwarf2_cu *cu);
15392
15393 /* A helper for handle_struct_member_die that handles
15394 DW_TAG_variant_part. */
15395
15396 static void
15397 handle_variant_part (struct die_info *die, struct type *type,
15398 struct field_info *fi,
15399 std::vector<struct symbol *> *template_args,
15400 struct dwarf2_cu *cu)
15401 {
15402 variant_part_builder *new_part;
15403 if (fi->current_variant_part == nullptr)
15404 {
15405 fi->variant_parts.emplace_back ();
15406 new_part = &fi->variant_parts.back ();
15407 }
15408 else if (!fi->current_variant_part->processing_variant)
15409 {
15410 complaint (_("nested DW_TAG_variant_part seen "
15411 "- DIE at %s [in module %s]"),
15412 sect_offset_str (die->sect_off),
15413 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15414 return;
15415 }
15416 else
15417 {
15418 variant_field &current = fi->current_variant_part->variants.back ();
15419 current.variant_parts.emplace_back ();
15420 new_part = &current.variant_parts.back ();
15421 }
15422
15423 /* When we recurse, we want callees to add to this new variant
15424 part. */
15425 scoped_restore save_current_variant_part
15426 = make_scoped_restore (&fi->current_variant_part, new_part);
15427
15428 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15429 if (discr == NULL)
15430 {
15431 /* It's a univariant form, an extension we support. */
15432 }
15433 else if (discr->form_is_ref ())
15434 {
15435 struct dwarf2_cu *target_cu = cu;
15436 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15437
15438 new_part->discriminant_offset = target_die->sect_off;
15439 }
15440 else
15441 {
15442 complaint (_("DW_AT_discr does not have DIE reference form"
15443 " - DIE at %s [in module %s]"),
15444 sect_offset_str (die->sect_off),
15445 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15446 }
15447
15448 for (die_info *child_die = die->child;
15449 child_die != NULL;
15450 child_die = child_die->sibling)
15451 handle_struct_member_die (child_die, type, fi, template_args, cu);
15452 }
15453
15454 /* A helper for handle_struct_member_die that handles
15455 DW_TAG_variant. */
15456
15457 static void
15458 handle_variant (struct die_info *die, struct type *type,
15459 struct field_info *fi,
15460 std::vector<struct symbol *> *template_args,
15461 struct dwarf2_cu *cu)
15462 {
15463 if (fi->current_variant_part == nullptr)
15464 {
15465 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15466 "- DIE at %s [in module %s]"),
15467 sect_offset_str (die->sect_off),
15468 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15469 return;
15470 }
15471 if (fi->current_variant_part->processing_variant)
15472 {
15473 complaint (_("nested DW_TAG_variant seen "
15474 "- DIE at %s [in module %s]"),
15475 sect_offset_str (die->sect_off),
15476 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15477 return;
15478 }
15479
15480 scoped_restore save_processing_variant
15481 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15482 true);
15483
15484 fi->current_variant_part->variants.emplace_back ();
15485 variant_field &variant = fi->current_variant_part->variants.back ();
15486 variant.first_field = fi->fields.size ();
15487
15488 /* In a variant we want to get the discriminant and also add a
15489 field for our sole member child. */
15490 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15491 if (discr == nullptr)
15492 {
15493 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15494 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15495 variant.default_branch = true;
15496 else
15497 variant.discr_list_data = DW_BLOCK (discr);
15498 }
15499 else
15500 variant.discriminant_value = DW_UNSND (discr);
15501
15502 for (die_info *variant_child = die->child;
15503 variant_child != NULL;
15504 variant_child = variant_child->sibling)
15505 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15506
15507 variant.last_field = fi->fields.size ();
15508 }
15509
15510 /* A helper for process_structure_scope that handles a single member
15511 DIE. */
15512
15513 static void
15514 handle_struct_member_die (struct die_info *child_die, struct type *type,
15515 struct field_info *fi,
15516 std::vector<struct symbol *> *template_args,
15517 struct dwarf2_cu *cu)
15518 {
15519 if (child_die->tag == DW_TAG_member
15520 || child_die->tag == DW_TAG_variable)
15521 {
15522 /* NOTE: carlton/2002-11-05: A C++ static data member
15523 should be a DW_TAG_member that is a declaration, but
15524 all versions of G++ as of this writing (so through at
15525 least 3.2.1) incorrectly generate DW_TAG_variable
15526 tags for them instead. */
15527 dwarf2_add_field (fi, child_die, cu);
15528 }
15529 else if (child_die->tag == DW_TAG_subprogram)
15530 {
15531 /* Rust doesn't have member functions in the C++ sense.
15532 However, it does emit ordinary functions as children
15533 of a struct DIE. */
15534 if (cu->language == language_rust)
15535 read_func_scope (child_die, cu);
15536 else
15537 {
15538 /* C++ member function. */
15539 dwarf2_add_member_fn (fi, child_die, type, cu);
15540 }
15541 }
15542 else if (child_die->tag == DW_TAG_inheritance)
15543 {
15544 /* C++ base class field. */
15545 dwarf2_add_field (fi, child_die, cu);
15546 }
15547 else if (type_can_define_types (child_die))
15548 dwarf2_add_type_defn (fi, child_die, cu);
15549 else if (child_die->tag == DW_TAG_template_type_param
15550 || child_die->tag == DW_TAG_template_value_param)
15551 {
15552 struct symbol *arg = new_symbol (child_die, NULL, cu);
15553
15554 if (arg != NULL)
15555 template_args->push_back (arg);
15556 }
15557 else if (child_die->tag == DW_TAG_variant_part)
15558 handle_variant_part (child_die, type, fi, template_args, cu);
15559 else if (child_die->tag == DW_TAG_variant)
15560 handle_variant (child_die, type, fi, template_args, cu);
15561 }
15562
15563 /* Finish creating a structure or union type, including filling in
15564 its members and creating a symbol for it. */
15565
15566 static void
15567 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15568 {
15569 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15570 struct die_info *child_die;
15571 struct type *type;
15572
15573 type = get_die_type (die, cu);
15574 if (type == NULL)
15575 type = read_structure_type (die, cu);
15576
15577 bool has_template_parameters = false;
15578 if (die->child != NULL && ! die_is_declaration (die, cu))
15579 {
15580 struct field_info fi;
15581 std::vector<struct symbol *> template_args;
15582
15583 child_die = die->child;
15584
15585 while (child_die && child_die->tag)
15586 {
15587 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15588 child_die = child_die->sibling;
15589 }
15590
15591 /* Attach template arguments to type. */
15592 if (!template_args.empty ())
15593 {
15594 has_template_parameters = true;
15595 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15596 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15597 TYPE_TEMPLATE_ARGUMENTS (type)
15598 = XOBNEWVEC (&objfile->objfile_obstack,
15599 struct symbol *,
15600 TYPE_N_TEMPLATE_ARGUMENTS (type));
15601 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15602 template_args.data (),
15603 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15604 * sizeof (struct symbol *)));
15605 }
15606
15607 /* Attach fields and member functions to the type. */
15608 if (fi.nfields () > 0)
15609 dwarf2_attach_fields_to_type (&fi, type, cu);
15610 if (!fi.fnfieldlists.empty ())
15611 {
15612 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15613
15614 /* Get the type which refers to the base class (possibly this
15615 class itself) which contains the vtable pointer for the current
15616 class from the DW_AT_containing_type attribute. This use of
15617 DW_AT_containing_type is a GNU extension. */
15618
15619 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15620 {
15621 struct type *t = die_containing_type (die, cu);
15622
15623 set_type_vptr_basetype (type, t);
15624 if (type == t)
15625 {
15626 int i;
15627
15628 /* Our own class provides vtbl ptr. */
15629 for (i = TYPE_NFIELDS (t) - 1;
15630 i >= TYPE_N_BASECLASSES (t);
15631 --i)
15632 {
15633 const char *fieldname = TYPE_FIELD_NAME (t, i);
15634
15635 if (is_vtable_name (fieldname, cu))
15636 {
15637 set_type_vptr_fieldno (type, i);
15638 break;
15639 }
15640 }
15641
15642 /* Complain if virtual function table field not found. */
15643 if (i < TYPE_N_BASECLASSES (t))
15644 complaint (_("virtual function table pointer "
15645 "not found when defining class '%s'"),
15646 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15647 }
15648 else
15649 {
15650 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15651 }
15652 }
15653 else if (cu->producer
15654 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15655 {
15656 /* The IBM XLC compiler does not provide direct indication
15657 of the containing type, but the vtable pointer is
15658 always named __vfp. */
15659
15660 int i;
15661
15662 for (i = TYPE_NFIELDS (type) - 1;
15663 i >= TYPE_N_BASECLASSES (type);
15664 --i)
15665 {
15666 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15667 {
15668 set_type_vptr_fieldno (type, i);
15669 set_type_vptr_basetype (type, type);
15670 break;
15671 }
15672 }
15673 }
15674 }
15675
15676 /* Copy fi.typedef_field_list linked list elements content into the
15677 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15678 if (!fi.typedef_field_list.empty ())
15679 {
15680 int count = fi.typedef_field_list.size ();
15681
15682 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15683 TYPE_TYPEDEF_FIELD_ARRAY (type)
15684 = ((struct decl_field *)
15685 TYPE_ALLOC (type,
15686 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15687 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15688
15689 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15690 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15691 }
15692
15693 /* Copy fi.nested_types_list linked list elements content into the
15694 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15695 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15696 {
15697 int count = fi.nested_types_list.size ();
15698
15699 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15700 TYPE_NESTED_TYPES_ARRAY (type)
15701 = ((struct decl_field *)
15702 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15703 TYPE_NESTED_TYPES_COUNT (type) = count;
15704
15705 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15706 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15707 }
15708 }
15709
15710 quirk_gcc_member_function_pointer (type, objfile);
15711 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15712 cu->rust_unions.push_back (type);
15713
15714 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15715 snapshots) has been known to create a die giving a declaration
15716 for a class that has, as a child, a die giving a definition for a
15717 nested class. So we have to process our children even if the
15718 current die is a declaration. Normally, of course, a declaration
15719 won't have any children at all. */
15720
15721 child_die = die->child;
15722
15723 while (child_die != NULL && child_die->tag)
15724 {
15725 if (child_die->tag == DW_TAG_member
15726 || child_die->tag == DW_TAG_variable
15727 || child_die->tag == DW_TAG_inheritance
15728 || child_die->tag == DW_TAG_template_value_param
15729 || child_die->tag == DW_TAG_template_type_param)
15730 {
15731 /* Do nothing. */
15732 }
15733 else
15734 process_die (child_die, cu);
15735
15736 child_die = child_die->sibling;
15737 }
15738
15739 /* Do not consider external references. According to the DWARF standard,
15740 these DIEs are identified by the fact that they have no byte_size
15741 attribute, and a declaration attribute. */
15742 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15743 || !die_is_declaration (die, cu))
15744 {
15745 struct symbol *sym = new_symbol (die, type, cu);
15746
15747 if (has_template_parameters)
15748 {
15749 struct symtab *symtab;
15750 if (sym != nullptr)
15751 symtab = symbol_symtab (sym);
15752 else if (cu->line_header != nullptr)
15753 {
15754 /* Any related symtab will do. */
15755 symtab
15756 = cu->line_header->file_names ()[0].symtab;
15757 }
15758 else
15759 {
15760 symtab = nullptr;
15761 complaint (_("could not find suitable "
15762 "symtab for template parameter"
15763 " - DIE at %s [in module %s]"),
15764 sect_offset_str (die->sect_off),
15765 objfile_name (objfile));
15766 }
15767
15768 if (symtab != nullptr)
15769 {
15770 /* Make sure that the symtab is set on the new symbols.
15771 Even though they don't appear in this symtab directly,
15772 other parts of gdb assume that symbols do, and this is
15773 reasonably true. */
15774 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15775 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15776 }
15777 }
15778 }
15779 }
15780
15781 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15782 update TYPE using some information only available in DIE's children. */
15783
15784 static void
15785 update_enumeration_type_from_children (struct die_info *die,
15786 struct type *type,
15787 struct dwarf2_cu *cu)
15788 {
15789 struct die_info *child_die;
15790 int unsigned_enum = 1;
15791 int flag_enum = 1;
15792
15793 auto_obstack obstack;
15794
15795 for (child_die = die->child;
15796 child_die != NULL && child_die->tag;
15797 child_die = child_die->sibling)
15798 {
15799 struct attribute *attr;
15800 LONGEST value;
15801 const gdb_byte *bytes;
15802 struct dwarf2_locexpr_baton *baton;
15803 const char *name;
15804
15805 if (child_die->tag != DW_TAG_enumerator)
15806 continue;
15807
15808 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15809 if (attr == NULL)
15810 continue;
15811
15812 name = dwarf2_name (child_die, cu);
15813 if (name == NULL)
15814 name = "<anonymous enumerator>";
15815
15816 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15817 &value, &bytes, &baton);
15818 if (value < 0)
15819 {
15820 unsigned_enum = 0;
15821 flag_enum = 0;
15822 }
15823 else
15824 {
15825 if (count_one_bits_ll (value) >= 2)
15826 flag_enum = 0;
15827 }
15828
15829 /* If we already know that the enum type is neither unsigned, nor
15830 a flag type, no need to look at the rest of the enumerates. */
15831 if (!unsigned_enum && !flag_enum)
15832 break;
15833 }
15834
15835 if (unsigned_enum)
15836 TYPE_UNSIGNED (type) = 1;
15837 if (flag_enum)
15838 TYPE_FLAG_ENUM (type) = 1;
15839 }
15840
15841 /* Given a DW_AT_enumeration_type die, set its type. We do not
15842 complete the type's fields yet, or create any symbols. */
15843
15844 static struct type *
15845 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15846 {
15847 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15848 struct type *type;
15849 struct attribute *attr;
15850 const char *name;
15851
15852 /* If the definition of this type lives in .debug_types, read that type.
15853 Don't follow DW_AT_specification though, that will take us back up
15854 the chain and we want to go down. */
15855 attr = die->attr (DW_AT_signature);
15856 if (attr != nullptr)
15857 {
15858 type = get_DW_AT_signature_type (die, attr, cu);
15859
15860 /* The type's CU may not be the same as CU.
15861 Ensure TYPE is recorded with CU in die_type_hash. */
15862 return set_die_type (die, type, cu);
15863 }
15864
15865 type = alloc_type (objfile);
15866
15867 TYPE_CODE (type) = TYPE_CODE_ENUM;
15868 name = dwarf2_full_name (NULL, die, cu);
15869 if (name != NULL)
15870 TYPE_NAME (type) = name;
15871
15872 attr = dwarf2_attr (die, DW_AT_type, cu);
15873 if (attr != NULL)
15874 {
15875 struct type *underlying_type = die_type (die, cu);
15876
15877 TYPE_TARGET_TYPE (type) = underlying_type;
15878 }
15879
15880 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15881 if (attr != nullptr)
15882 {
15883 TYPE_LENGTH (type) = DW_UNSND (attr);
15884 }
15885 else
15886 {
15887 TYPE_LENGTH (type) = 0;
15888 }
15889
15890 maybe_set_alignment (cu, die, type);
15891
15892 /* The enumeration DIE can be incomplete. In Ada, any type can be
15893 declared as private in the package spec, and then defined only
15894 inside the package body. Such types are known as Taft Amendment
15895 Types. When another package uses such a type, an incomplete DIE
15896 may be generated by the compiler. */
15897 if (die_is_declaration (die, cu))
15898 TYPE_STUB (type) = 1;
15899
15900 /* Finish the creation of this type by using the enum's children.
15901 We must call this even when the underlying type has been provided
15902 so that we can determine if we're looking at a "flag" enum. */
15903 update_enumeration_type_from_children (die, type, cu);
15904
15905 /* If this type has an underlying type that is not a stub, then we
15906 may use its attributes. We always use the "unsigned" attribute
15907 in this situation, because ordinarily we guess whether the type
15908 is unsigned -- but the guess can be wrong and the underlying type
15909 can tell us the reality. However, we defer to a local size
15910 attribute if one exists, because this lets the compiler override
15911 the underlying type if needed. */
15912 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15913 {
15914 struct type *underlying_type = TYPE_TARGET_TYPE (type);
15915 underlying_type = check_typedef (underlying_type);
15916 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
15917 if (TYPE_LENGTH (type) == 0)
15918 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
15919 if (TYPE_RAW_ALIGN (type) == 0
15920 && TYPE_RAW_ALIGN (underlying_type) != 0)
15921 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
15922 }
15923
15924 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15925
15926 return set_die_type (die, type, cu);
15927 }
15928
15929 /* Given a pointer to a die which begins an enumeration, process all
15930 the dies that define the members of the enumeration, and create the
15931 symbol for the enumeration type.
15932
15933 NOTE: We reverse the order of the element list. */
15934
15935 static void
15936 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15937 {
15938 struct type *this_type;
15939
15940 this_type = get_die_type (die, cu);
15941 if (this_type == NULL)
15942 this_type = read_enumeration_type (die, cu);
15943
15944 if (die->child != NULL)
15945 {
15946 struct die_info *child_die;
15947 struct symbol *sym;
15948 std::vector<struct field> fields;
15949 const char *name;
15950
15951 child_die = die->child;
15952 while (child_die && child_die->tag)
15953 {
15954 if (child_die->tag != DW_TAG_enumerator)
15955 {
15956 process_die (child_die, cu);
15957 }
15958 else
15959 {
15960 name = dwarf2_name (child_die, cu);
15961 if (name)
15962 {
15963 sym = new_symbol (child_die, this_type, cu);
15964
15965 fields.emplace_back ();
15966 struct field &field = fields.back ();
15967
15968 FIELD_NAME (field) = sym->linkage_name ();
15969 FIELD_TYPE (field) = NULL;
15970 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15971 FIELD_BITSIZE (field) = 0;
15972 }
15973 }
15974
15975 child_die = child_die->sibling;
15976 }
15977
15978 if (!fields.empty ())
15979 {
15980 TYPE_NFIELDS (this_type) = fields.size ();
15981 TYPE_FIELDS (this_type) = (struct field *)
15982 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15983 memcpy (TYPE_FIELDS (this_type), fields.data (),
15984 sizeof (struct field) * fields.size ());
15985 }
15986 }
15987
15988 /* If we are reading an enum from a .debug_types unit, and the enum
15989 is a declaration, and the enum is not the signatured type in the
15990 unit, then we do not want to add a symbol for it. Adding a
15991 symbol would in some cases obscure the true definition of the
15992 enum, giving users an incomplete type when the definition is
15993 actually available. Note that we do not want to do this for all
15994 enums which are just declarations, because C++0x allows forward
15995 enum declarations. */
15996 if (cu->per_cu->is_debug_types
15997 && die_is_declaration (die, cu))
15998 {
15999 struct signatured_type *sig_type;
16000
16001 sig_type = (struct signatured_type *) cu->per_cu;
16002 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16003 if (sig_type->type_offset_in_section != die->sect_off)
16004 return;
16005 }
16006
16007 new_symbol (die, this_type, cu);
16008 }
16009
16010 /* Extract all information from a DW_TAG_array_type DIE and put it in
16011 the DIE's type field. For now, this only handles one dimensional
16012 arrays. */
16013
16014 static struct type *
16015 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16016 {
16017 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16018 struct die_info *child_die;
16019 struct type *type;
16020 struct type *element_type, *range_type, *index_type;
16021 struct attribute *attr;
16022 const char *name;
16023 struct dynamic_prop *byte_stride_prop = NULL;
16024 unsigned int bit_stride = 0;
16025
16026 element_type = die_type (die, cu);
16027
16028 /* The die_type call above may have already set the type for this DIE. */
16029 type = get_die_type (die, cu);
16030 if (type)
16031 return type;
16032
16033 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16034 if (attr != NULL)
16035 {
16036 int stride_ok;
16037 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
16038
16039 byte_stride_prop
16040 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16041 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16042 prop_type);
16043 if (!stride_ok)
16044 {
16045 complaint (_("unable to read array DW_AT_byte_stride "
16046 " - DIE at %s [in module %s]"),
16047 sect_offset_str (die->sect_off),
16048 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16049 /* Ignore this attribute. We will likely not be able to print
16050 arrays of this type correctly, but there is little we can do
16051 to help if we cannot read the attribute's value. */
16052 byte_stride_prop = NULL;
16053 }
16054 }
16055
16056 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16057 if (attr != NULL)
16058 bit_stride = DW_UNSND (attr);
16059
16060 /* Irix 6.2 native cc creates array types without children for
16061 arrays with unspecified length. */
16062 if (die->child == NULL)
16063 {
16064 index_type = objfile_type (objfile)->builtin_int;
16065 range_type = create_static_range_type (NULL, index_type, 0, -1);
16066 type = create_array_type_with_stride (NULL, element_type, range_type,
16067 byte_stride_prop, bit_stride);
16068 return set_die_type (die, type, cu);
16069 }
16070
16071 std::vector<struct type *> range_types;
16072 child_die = die->child;
16073 while (child_die && child_die->tag)
16074 {
16075 if (child_die->tag == DW_TAG_subrange_type)
16076 {
16077 struct type *child_type = read_type_die (child_die, cu);
16078
16079 if (child_type != NULL)
16080 {
16081 /* The range type was succesfully read. Save it for the
16082 array type creation. */
16083 range_types.push_back (child_type);
16084 }
16085 }
16086 child_die = child_die->sibling;
16087 }
16088
16089 /* Dwarf2 dimensions are output from left to right, create the
16090 necessary array types in backwards order. */
16091
16092 type = element_type;
16093
16094 if (read_array_order (die, cu) == DW_ORD_col_major)
16095 {
16096 int i = 0;
16097
16098 while (i < range_types.size ())
16099 type = create_array_type_with_stride (NULL, type, range_types[i++],
16100 byte_stride_prop, bit_stride);
16101 }
16102 else
16103 {
16104 size_t ndim = range_types.size ();
16105 while (ndim-- > 0)
16106 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16107 byte_stride_prop, bit_stride);
16108 }
16109
16110 /* Understand Dwarf2 support for vector types (like they occur on
16111 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16112 array type. This is not part of the Dwarf2/3 standard yet, but a
16113 custom vendor extension. The main difference between a regular
16114 array and the vector variant is that vectors are passed by value
16115 to functions. */
16116 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16117 if (attr != nullptr)
16118 make_vector_type (type);
16119
16120 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16121 implementation may choose to implement triple vectors using this
16122 attribute. */
16123 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16124 if (attr != nullptr)
16125 {
16126 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16127 TYPE_LENGTH (type) = DW_UNSND (attr);
16128 else
16129 complaint (_("DW_AT_byte_size for array type smaller "
16130 "than the total size of elements"));
16131 }
16132
16133 name = dwarf2_name (die, cu);
16134 if (name)
16135 TYPE_NAME (type) = name;
16136
16137 maybe_set_alignment (cu, die, type);
16138
16139 /* Install the type in the die. */
16140 set_die_type (die, type, cu);
16141
16142 /* set_die_type should be already done. */
16143 set_descriptive_type (type, die, cu);
16144
16145 return type;
16146 }
16147
16148 static enum dwarf_array_dim_ordering
16149 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16150 {
16151 struct attribute *attr;
16152
16153 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16154
16155 if (attr != nullptr)
16156 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16157
16158 /* GNU F77 is a special case, as at 08/2004 array type info is the
16159 opposite order to the dwarf2 specification, but data is still
16160 laid out as per normal fortran.
16161
16162 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16163 version checking. */
16164
16165 if (cu->language == language_fortran
16166 && cu->producer && strstr (cu->producer, "GNU F77"))
16167 {
16168 return DW_ORD_row_major;
16169 }
16170
16171 switch (cu->language_defn->la_array_ordering)
16172 {
16173 case array_column_major:
16174 return DW_ORD_col_major;
16175 case array_row_major:
16176 default:
16177 return DW_ORD_row_major;
16178 };
16179 }
16180
16181 /* Extract all information from a DW_TAG_set_type DIE and put it in
16182 the DIE's type field. */
16183
16184 static struct type *
16185 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16186 {
16187 struct type *domain_type, *set_type;
16188 struct attribute *attr;
16189
16190 domain_type = die_type (die, cu);
16191
16192 /* The die_type call above may have already set the type for this DIE. */
16193 set_type = get_die_type (die, cu);
16194 if (set_type)
16195 return set_type;
16196
16197 set_type = create_set_type (NULL, domain_type);
16198
16199 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16200 if (attr != nullptr)
16201 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16202
16203 maybe_set_alignment (cu, die, set_type);
16204
16205 return set_die_type (die, set_type, cu);
16206 }
16207
16208 /* A helper for read_common_block that creates a locexpr baton.
16209 SYM is the symbol which we are marking as computed.
16210 COMMON_DIE is the DIE for the common block.
16211 COMMON_LOC is the location expression attribute for the common
16212 block itself.
16213 MEMBER_LOC is the location expression attribute for the particular
16214 member of the common block that we are processing.
16215 CU is the CU from which the above come. */
16216
16217 static void
16218 mark_common_block_symbol_computed (struct symbol *sym,
16219 struct die_info *common_die,
16220 struct attribute *common_loc,
16221 struct attribute *member_loc,
16222 struct dwarf2_cu *cu)
16223 {
16224 struct dwarf2_per_objfile *dwarf2_per_objfile
16225 = cu->per_cu->dwarf2_per_objfile;
16226 struct objfile *objfile = dwarf2_per_objfile->objfile;
16227 struct dwarf2_locexpr_baton *baton;
16228 gdb_byte *ptr;
16229 unsigned int cu_off;
16230 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16231 LONGEST offset = 0;
16232
16233 gdb_assert (common_loc && member_loc);
16234 gdb_assert (common_loc->form_is_block ());
16235 gdb_assert (member_loc->form_is_block ()
16236 || member_loc->form_is_constant ());
16237
16238 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16239 baton->per_cu = cu->per_cu;
16240 gdb_assert (baton->per_cu);
16241
16242 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16243
16244 if (member_loc->form_is_constant ())
16245 {
16246 offset = member_loc->constant_value (0);
16247 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16248 }
16249 else
16250 baton->size += DW_BLOCK (member_loc)->size;
16251
16252 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16253 baton->data = ptr;
16254
16255 *ptr++ = DW_OP_call4;
16256 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16257 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16258 ptr += 4;
16259
16260 if (member_loc->form_is_constant ())
16261 {
16262 *ptr++ = DW_OP_addr;
16263 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16264 ptr += cu->header.addr_size;
16265 }
16266 else
16267 {
16268 /* We have to copy the data here, because DW_OP_call4 will only
16269 use a DW_AT_location attribute. */
16270 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16271 ptr += DW_BLOCK (member_loc)->size;
16272 }
16273
16274 *ptr++ = DW_OP_plus;
16275 gdb_assert (ptr - baton->data == baton->size);
16276
16277 SYMBOL_LOCATION_BATON (sym) = baton;
16278 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16279 }
16280
16281 /* Create appropriate locally-scoped variables for all the
16282 DW_TAG_common_block entries. Also create a struct common_block
16283 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16284 is used to separate the common blocks name namespace from regular
16285 variable names. */
16286
16287 static void
16288 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16289 {
16290 struct attribute *attr;
16291
16292 attr = dwarf2_attr (die, DW_AT_location, cu);
16293 if (attr != nullptr)
16294 {
16295 /* Support the .debug_loc offsets. */
16296 if (attr->form_is_block ())
16297 {
16298 /* Ok. */
16299 }
16300 else if (attr->form_is_section_offset ())
16301 {
16302 dwarf2_complex_location_expr_complaint ();
16303 attr = NULL;
16304 }
16305 else
16306 {
16307 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16308 "common block member");
16309 attr = NULL;
16310 }
16311 }
16312
16313 if (die->child != NULL)
16314 {
16315 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16316 struct die_info *child_die;
16317 size_t n_entries = 0, size;
16318 struct common_block *common_block;
16319 struct symbol *sym;
16320
16321 for (child_die = die->child;
16322 child_die && child_die->tag;
16323 child_die = child_die->sibling)
16324 ++n_entries;
16325
16326 size = (sizeof (struct common_block)
16327 + (n_entries - 1) * sizeof (struct symbol *));
16328 common_block
16329 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16330 size);
16331 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16332 common_block->n_entries = 0;
16333
16334 for (child_die = die->child;
16335 child_die && child_die->tag;
16336 child_die = child_die->sibling)
16337 {
16338 /* Create the symbol in the DW_TAG_common_block block in the current
16339 symbol scope. */
16340 sym = new_symbol (child_die, NULL, cu);
16341 if (sym != NULL)
16342 {
16343 struct attribute *member_loc;
16344
16345 common_block->contents[common_block->n_entries++] = sym;
16346
16347 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16348 cu);
16349 if (member_loc)
16350 {
16351 /* GDB has handled this for a long time, but it is
16352 not specified by DWARF. It seems to have been
16353 emitted by gfortran at least as recently as:
16354 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16355 complaint (_("Variable in common block has "
16356 "DW_AT_data_member_location "
16357 "- DIE at %s [in module %s]"),
16358 sect_offset_str (child_die->sect_off),
16359 objfile_name (objfile));
16360
16361 if (member_loc->form_is_section_offset ())
16362 dwarf2_complex_location_expr_complaint ();
16363 else if (member_loc->form_is_constant ()
16364 || member_loc->form_is_block ())
16365 {
16366 if (attr != nullptr)
16367 mark_common_block_symbol_computed (sym, die, attr,
16368 member_loc, cu);
16369 }
16370 else
16371 dwarf2_complex_location_expr_complaint ();
16372 }
16373 }
16374 }
16375
16376 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16377 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16378 }
16379 }
16380
16381 /* Create a type for a C++ namespace. */
16382
16383 static struct type *
16384 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16385 {
16386 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16387 const char *previous_prefix, *name;
16388 int is_anonymous;
16389 struct type *type;
16390
16391 /* For extensions, reuse the type of the original namespace. */
16392 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16393 {
16394 struct die_info *ext_die;
16395 struct dwarf2_cu *ext_cu = cu;
16396
16397 ext_die = dwarf2_extension (die, &ext_cu);
16398 type = read_type_die (ext_die, ext_cu);
16399
16400 /* EXT_CU may not be the same as CU.
16401 Ensure TYPE is recorded with CU in die_type_hash. */
16402 return set_die_type (die, type, cu);
16403 }
16404
16405 name = namespace_name (die, &is_anonymous, cu);
16406
16407 /* Now build the name of the current namespace. */
16408
16409 previous_prefix = determine_prefix (die, cu);
16410 if (previous_prefix[0] != '\0')
16411 name = typename_concat (&objfile->objfile_obstack,
16412 previous_prefix, name, 0, cu);
16413
16414 /* Create the type. */
16415 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16416
16417 return set_die_type (die, type, cu);
16418 }
16419
16420 /* Read a namespace scope. */
16421
16422 static void
16423 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16424 {
16425 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16426 int is_anonymous;
16427
16428 /* Add a symbol associated to this if we haven't seen the namespace
16429 before. Also, add a using directive if it's an anonymous
16430 namespace. */
16431
16432 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16433 {
16434 struct type *type;
16435
16436 type = read_type_die (die, cu);
16437 new_symbol (die, type, cu);
16438
16439 namespace_name (die, &is_anonymous, cu);
16440 if (is_anonymous)
16441 {
16442 const char *previous_prefix = determine_prefix (die, cu);
16443
16444 std::vector<const char *> excludes;
16445 add_using_directive (using_directives (cu),
16446 previous_prefix, TYPE_NAME (type), NULL,
16447 NULL, excludes, 0, &objfile->objfile_obstack);
16448 }
16449 }
16450
16451 if (die->child != NULL)
16452 {
16453 struct die_info *child_die = die->child;
16454
16455 while (child_die && child_die->tag)
16456 {
16457 process_die (child_die, cu);
16458 child_die = child_die->sibling;
16459 }
16460 }
16461 }
16462
16463 /* Read a Fortran module as type. This DIE can be only a declaration used for
16464 imported module. Still we need that type as local Fortran "use ... only"
16465 declaration imports depend on the created type in determine_prefix. */
16466
16467 static struct type *
16468 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16469 {
16470 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16471 const char *module_name;
16472 struct type *type;
16473
16474 module_name = dwarf2_name (die, cu);
16475 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16476
16477 return set_die_type (die, type, cu);
16478 }
16479
16480 /* Read a Fortran module. */
16481
16482 static void
16483 read_module (struct die_info *die, struct dwarf2_cu *cu)
16484 {
16485 struct die_info *child_die = die->child;
16486 struct type *type;
16487
16488 type = read_type_die (die, cu);
16489 new_symbol (die, type, cu);
16490
16491 while (child_die && child_die->tag)
16492 {
16493 process_die (child_die, cu);
16494 child_die = child_die->sibling;
16495 }
16496 }
16497
16498 /* Return the name of the namespace represented by DIE. Set
16499 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16500 namespace. */
16501
16502 static const char *
16503 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16504 {
16505 struct die_info *current_die;
16506 const char *name = NULL;
16507
16508 /* Loop through the extensions until we find a name. */
16509
16510 for (current_die = die;
16511 current_die != NULL;
16512 current_die = dwarf2_extension (die, &cu))
16513 {
16514 /* We don't use dwarf2_name here so that we can detect the absence
16515 of a name -> anonymous namespace. */
16516 name = dwarf2_string_attr (die, DW_AT_name, cu);
16517
16518 if (name != NULL)
16519 break;
16520 }
16521
16522 /* Is it an anonymous namespace? */
16523
16524 *is_anonymous = (name == NULL);
16525 if (*is_anonymous)
16526 name = CP_ANONYMOUS_NAMESPACE_STR;
16527
16528 return name;
16529 }
16530
16531 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16532 the user defined type vector. */
16533
16534 static struct type *
16535 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16536 {
16537 struct gdbarch *gdbarch
16538 = cu->per_cu->dwarf2_per_objfile->objfile->arch ();
16539 struct comp_unit_head *cu_header = &cu->header;
16540 struct type *type;
16541 struct attribute *attr_byte_size;
16542 struct attribute *attr_address_class;
16543 int byte_size, addr_class;
16544 struct type *target_type;
16545
16546 target_type = die_type (die, cu);
16547
16548 /* The die_type call above may have already set the type for this DIE. */
16549 type = get_die_type (die, cu);
16550 if (type)
16551 return type;
16552
16553 type = lookup_pointer_type (target_type);
16554
16555 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16556 if (attr_byte_size)
16557 byte_size = DW_UNSND (attr_byte_size);
16558 else
16559 byte_size = cu_header->addr_size;
16560
16561 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16562 if (attr_address_class)
16563 addr_class = DW_UNSND (attr_address_class);
16564 else
16565 addr_class = DW_ADDR_none;
16566
16567 ULONGEST alignment = get_alignment (cu, die);
16568
16569 /* If the pointer size, alignment, or address class is different
16570 than the default, create a type variant marked as such and set
16571 the length accordingly. */
16572 if (TYPE_LENGTH (type) != byte_size
16573 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16574 && alignment != TYPE_RAW_ALIGN (type))
16575 || addr_class != DW_ADDR_none)
16576 {
16577 if (gdbarch_address_class_type_flags_p (gdbarch))
16578 {
16579 int type_flags;
16580
16581 type_flags = gdbarch_address_class_type_flags
16582 (gdbarch, byte_size, addr_class);
16583 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16584 == 0);
16585 type = make_type_with_address_space (type, type_flags);
16586 }
16587 else if (TYPE_LENGTH (type) != byte_size)
16588 {
16589 complaint (_("invalid pointer size %d"), byte_size);
16590 }
16591 else if (TYPE_RAW_ALIGN (type) != alignment)
16592 {
16593 complaint (_("Invalid DW_AT_alignment"
16594 " - DIE at %s [in module %s]"),
16595 sect_offset_str (die->sect_off),
16596 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16597 }
16598 else
16599 {
16600 /* Should we also complain about unhandled address classes? */
16601 }
16602 }
16603
16604 TYPE_LENGTH (type) = byte_size;
16605 set_type_align (type, alignment);
16606 return set_die_type (die, type, cu);
16607 }
16608
16609 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16610 the user defined type vector. */
16611
16612 static struct type *
16613 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16614 {
16615 struct type *type;
16616 struct type *to_type;
16617 struct type *domain;
16618
16619 to_type = die_type (die, cu);
16620 domain = die_containing_type (die, cu);
16621
16622 /* The calls above may have already set the type for this DIE. */
16623 type = get_die_type (die, cu);
16624 if (type)
16625 return type;
16626
16627 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16628 type = lookup_methodptr_type (to_type);
16629 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16630 {
16631 struct type *new_type
16632 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16633
16634 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16635 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16636 TYPE_VARARGS (to_type));
16637 type = lookup_methodptr_type (new_type);
16638 }
16639 else
16640 type = lookup_memberptr_type (to_type, domain);
16641
16642 return set_die_type (die, type, cu);
16643 }
16644
16645 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16646 the user defined type vector. */
16647
16648 static struct type *
16649 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16650 enum type_code refcode)
16651 {
16652 struct comp_unit_head *cu_header = &cu->header;
16653 struct type *type, *target_type;
16654 struct attribute *attr;
16655
16656 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16657
16658 target_type = die_type (die, cu);
16659
16660 /* The die_type call above may have already set the type for this DIE. */
16661 type = get_die_type (die, cu);
16662 if (type)
16663 return type;
16664
16665 type = lookup_reference_type (target_type, refcode);
16666 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16667 if (attr != nullptr)
16668 {
16669 TYPE_LENGTH (type) = DW_UNSND (attr);
16670 }
16671 else
16672 {
16673 TYPE_LENGTH (type) = cu_header->addr_size;
16674 }
16675 maybe_set_alignment (cu, die, type);
16676 return set_die_type (die, type, cu);
16677 }
16678
16679 /* Add the given cv-qualifiers to the element type of the array. GCC
16680 outputs DWARF type qualifiers that apply to an array, not the
16681 element type. But GDB relies on the array element type to carry
16682 the cv-qualifiers. This mimics section 6.7.3 of the C99
16683 specification. */
16684
16685 static struct type *
16686 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16687 struct type *base_type, int cnst, int voltl)
16688 {
16689 struct type *el_type, *inner_array;
16690
16691 base_type = copy_type (base_type);
16692 inner_array = base_type;
16693
16694 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16695 {
16696 TYPE_TARGET_TYPE (inner_array) =
16697 copy_type (TYPE_TARGET_TYPE (inner_array));
16698 inner_array = TYPE_TARGET_TYPE (inner_array);
16699 }
16700
16701 el_type = TYPE_TARGET_TYPE (inner_array);
16702 cnst |= TYPE_CONST (el_type);
16703 voltl |= TYPE_VOLATILE (el_type);
16704 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16705
16706 return set_die_type (die, base_type, cu);
16707 }
16708
16709 static struct type *
16710 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16711 {
16712 struct type *base_type, *cv_type;
16713
16714 base_type = die_type (die, cu);
16715
16716 /* The die_type call above may have already set the type for this DIE. */
16717 cv_type = get_die_type (die, cu);
16718 if (cv_type)
16719 return cv_type;
16720
16721 /* In case the const qualifier is applied to an array type, the element type
16722 is so qualified, not the array type (section 6.7.3 of C99). */
16723 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16724 return add_array_cv_type (die, cu, base_type, 1, 0);
16725
16726 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16727 return set_die_type (die, cv_type, cu);
16728 }
16729
16730 static struct type *
16731 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16732 {
16733 struct type *base_type, *cv_type;
16734
16735 base_type = die_type (die, cu);
16736
16737 /* The die_type call above may have already set the type for this DIE. */
16738 cv_type = get_die_type (die, cu);
16739 if (cv_type)
16740 return cv_type;
16741
16742 /* In case the volatile qualifier is applied to an array type, the
16743 element type is so qualified, not the array type (section 6.7.3
16744 of C99). */
16745 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16746 return add_array_cv_type (die, cu, base_type, 0, 1);
16747
16748 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16749 return set_die_type (die, cv_type, cu);
16750 }
16751
16752 /* Handle DW_TAG_restrict_type. */
16753
16754 static struct type *
16755 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16756 {
16757 struct type *base_type, *cv_type;
16758
16759 base_type = die_type (die, cu);
16760
16761 /* The die_type call above may have already set the type for this DIE. */
16762 cv_type = get_die_type (die, cu);
16763 if (cv_type)
16764 return cv_type;
16765
16766 cv_type = make_restrict_type (base_type);
16767 return set_die_type (die, cv_type, cu);
16768 }
16769
16770 /* Handle DW_TAG_atomic_type. */
16771
16772 static struct type *
16773 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16774 {
16775 struct type *base_type, *cv_type;
16776
16777 base_type = die_type (die, cu);
16778
16779 /* The die_type call above may have already set the type for this DIE. */
16780 cv_type = get_die_type (die, cu);
16781 if (cv_type)
16782 return cv_type;
16783
16784 cv_type = make_atomic_type (base_type);
16785 return set_die_type (die, cv_type, cu);
16786 }
16787
16788 /* Extract all information from a DW_TAG_string_type DIE and add to
16789 the user defined type vector. It isn't really a user defined type,
16790 but it behaves like one, with other DIE's using an AT_user_def_type
16791 attribute to reference it. */
16792
16793 static struct type *
16794 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16795 {
16796 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16797 struct gdbarch *gdbarch = objfile->arch ();
16798 struct type *type, *range_type, *index_type, *char_type;
16799 struct attribute *attr;
16800 struct dynamic_prop prop;
16801 bool length_is_constant = true;
16802 LONGEST length;
16803
16804 /* There are a couple of places where bit sizes might be made use of
16805 when parsing a DW_TAG_string_type, however, no producer that we know
16806 of make use of these. Handling bit sizes that are a multiple of the
16807 byte size is easy enough, but what about other bit sizes? Lets deal
16808 with that problem when we have to. Warn about these attributes being
16809 unsupported, then parse the type and ignore them like we always
16810 have. */
16811 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16812 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16813 {
16814 static bool warning_printed = false;
16815 if (!warning_printed)
16816 {
16817 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16818 "currently supported on DW_TAG_string_type."));
16819 warning_printed = true;
16820 }
16821 }
16822
16823 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16824 if (attr != nullptr && !attr->form_is_constant ())
16825 {
16826 /* The string length describes the location at which the length of
16827 the string can be found. The size of the length field can be
16828 specified with one of the attributes below. */
16829 struct type *prop_type;
16830 struct attribute *len
16831 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16832 if (len == nullptr)
16833 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16834 if (len != nullptr && len->form_is_constant ())
16835 {
16836 /* Pass 0 as the default as we know this attribute is constant
16837 and the default value will not be returned. */
16838 LONGEST sz = len->constant_value (0);
16839 prop_type = cu->per_cu->int_type (sz, true);
16840 }
16841 else
16842 {
16843 /* If the size is not specified then we assume it is the size of
16844 an address on this target. */
16845 prop_type = cu->per_cu->addr_sized_int_type (true);
16846 }
16847
16848 /* Convert the attribute into a dynamic property. */
16849 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16850 length = 1;
16851 else
16852 length_is_constant = false;
16853 }
16854 else if (attr != nullptr)
16855 {
16856 /* This DW_AT_string_length just contains the length with no
16857 indirection. There's no need to create a dynamic property in this
16858 case. Pass 0 for the default value as we know it will not be
16859 returned in this case. */
16860 length = attr->constant_value (0);
16861 }
16862 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16863 {
16864 /* We don't currently support non-constant byte sizes for strings. */
16865 length = attr->constant_value (1);
16866 }
16867 else
16868 {
16869 /* Use 1 as a fallback length if we have nothing else. */
16870 length = 1;
16871 }
16872
16873 index_type = objfile_type (objfile)->builtin_int;
16874 if (length_is_constant)
16875 range_type = create_static_range_type (NULL, index_type, 1, length);
16876 else
16877 {
16878 struct dynamic_prop low_bound;
16879
16880 low_bound.kind = PROP_CONST;
16881 low_bound.data.const_val = 1;
16882 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16883 }
16884 char_type = language_string_char_type (cu->language_defn, gdbarch);
16885 type = create_string_type (NULL, char_type, range_type);
16886
16887 return set_die_type (die, type, cu);
16888 }
16889
16890 /* Assuming that DIE corresponds to a function, returns nonzero
16891 if the function is prototyped. */
16892
16893 static int
16894 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16895 {
16896 struct attribute *attr;
16897
16898 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16899 if (attr && (DW_UNSND (attr) != 0))
16900 return 1;
16901
16902 /* The DWARF standard implies that the DW_AT_prototyped attribute
16903 is only meaningful for C, but the concept also extends to other
16904 languages that allow unprototyped functions (Eg: Objective C).
16905 For all other languages, assume that functions are always
16906 prototyped. */
16907 if (cu->language != language_c
16908 && cu->language != language_objc
16909 && cu->language != language_opencl)
16910 return 1;
16911
16912 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16913 prototyped and unprototyped functions; default to prototyped,
16914 since that is more common in modern code (and RealView warns
16915 about unprototyped functions). */
16916 if (producer_is_realview (cu->producer))
16917 return 1;
16918
16919 return 0;
16920 }
16921
16922 /* Handle DIES due to C code like:
16923
16924 struct foo
16925 {
16926 int (*funcp)(int a, long l);
16927 int b;
16928 };
16929
16930 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16931
16932 static struct type *
16933 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16934 {
16935 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16936 struct type *type; /* Type that this function returns. */
16937 struct type *ftype; /* Function that returns above type. */
16938 struct attribute *attr;
16939
16940 type = die_type (die, cu);
16941
16942 /* The die_type call above may have already set the type for this DIE. */
16943 ftype = get_die_type (die, cu);
16944 if (ftype)
16945 return ftype;
16946
16947 ftype = lookup_function_type (type);
16948
16949 if (prototyped_function_p (die, cu))
16950 TYPE_PROTOTYPED (ftype) = 1;
16951
16952 /* Store the calling convention in the type if it's available in
16953 the subroutine die. Otherwise set the calling convention to
16954 the default value DW_CC_normal. */
16955 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16956 if (attr != nullptr
16957 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16958 TYPE_CALLING_CONVENTION (ftype)
16959 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16960 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16961 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16962 else
16963 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16964
16965 /* Record whether the function returns normally to its caller or not
16966 if the DWARF producer set that information. */
16967 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16968 if (attr && (DW_UNSND (attr) != 0))
16969 TYPE_NO_RETURN (ftype) = 1;
16970
16971 /* We need to add the subroutine type to the die immediately so
16972 we don't infinitely recurse when dealing with parameters
16973 declared as the same subroutine type. */
16974 set_die_type (die, ftype, cu);
16975
16976 if (die->child != NULL)
16977 {
16978 struct type *void_type = objfile_type (objfile)->builtin_void;
16979 struct die_info *child_die;
16980 int nparams, iparams;
16981
16982 /* Count the number of parameters.
16983 FIXME: GDB currently ignores vararg functions, but knows about
16984 vararg member functions. */
16985 nparams = 0;
16986 child_die = die->child;
16987 while (child_die && child_die->tag)
16988 {
16989 if (child_die->tag == DW_TAG_formal_parameter)
16990 nparams++;
16991 else if (child_die->tag == DW_TAG_unspecified_parameters)
16992 TYPE_VARARGS (ftype) = 1;
16993 child_die = child_die->sibling;
16994 }
16995
16996 /* Allocate storage for parameters and fill them in. */
16997 TYPE_NFIELDS (ftype) = nparams;
16998 TYPE_FIELDS (ftype) = (struct field *)
16999 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17000
17001 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17002 even if we error out during the parameters reading below. */
17003 for (iparams = 0; iparams < nparams; iparams++)
17004 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17005
17006 iparams = 0;
17007 child_die = die->child;
17008 while (child_die && child_die->tag)
17009 {
17010 if (child_die->tag == DW_TAG_formal_parameter)
17011 {
17012 struct type *arg_type;
17013
17014 /* DWARF version 2 has no clean way to discern C++
17015 static and non-static member functions. G++ helps
17016 GDB by marking the first parameter for non-static
17017 member functions (which is the this pointer) as
17018 artificial. We pass this information to
17019 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17020
17021 DWARF version 3 added DW_AT_object_pointer, which GCC
17022 4.5 does not yet generate. */
17023 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17024 if (attr != nullptr)
17025 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17026 else
17027 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17028 arg_type = die_type (child_die, cu);
17029
17030 /* RealView does not mark THIS as const, which the testsuite
17031 expects. GCC marks THIS as const in method definitions,
17032 but not in the class specifications (GCC PR 43053). */
17033 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17034 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17035 {
17036 int is_this = 0;
17037 struct dwarf2_cu *arg_cu = cu;
17038 const char *name = dwarf2_name (child_die, cu);
17039
17040 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17041 if (attr != nullptr)
17042 {
17043 /* If the compiler emits this, use it. */
17044 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17045 is_this = 1;
17046 }
17047 else if (name && strcmp (name, "this") == 0)
17048 /* Function definitions will have the argument names. */
17049 is_this = 1;
17050 else if (name == NULL && iparams == 0)
17051 /* Declarations may not have the names, so like
17052 elsewhere in GDB, assume an artificial first
17053 argument is "this". */
17054 is_this = 1;
17055
17056 if (is_this)
17057 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17058 arg_type, 0);
17059 }
17060
17061 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17062 iparams++;
17063 }
17064 child_die = child_die->sibling;
17065 }
17066 }
17067
17068 return ftype;
17069 }
17070
17071 static struct type *
17072 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17073 {
17074 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17075 const char *name = NULL;
17076 struct type *this_type, *target_type;
17077
17078 name = dwarf2_full_name (NULL, die, cu);
17079 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17080 TYPE_TARGET_STUB (this_type) = 1;
17081 set_die_type (die, this_type, cu);
17082 target_type = die_type (die, cu);
17083 if (target_type != this_type)
17084 TYPE_TARGET_TYPE (this_type) = target_type;
17085 else
17086 {
17087 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17088 spec and cause infinite loops in GDB. */
17089 complaint (_("Self-referential DW_TAG_typedef "
17090 "- DIE at %s [in module %s]"),
17091 sect_offset_str (die->sect_off), objfile_name (objfile));
17092 TYPE_TARGET_TYPE (this_type) = NULL;
17093 }
17094 if (name == NULL)
17095 {
17096 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17097 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17098 Handle these by just returning the target type, rather than
17099 constructing an anonymous typedef type and trying to handle this
17100 elsewhere. */
17101 set_die_type (die, target_type, cu);
17102 return target_type;
17103 }
17104 return this_type;
17105 }
17106
17107 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17108 (which may be different from NAME) to the architecture back-end to allow
17109 it to guess the correct format if necessary. */
17110
17111 static struct type *
17112 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17113 const char *name_hint, enum bfd_endian byte_order)
17114 {
17115 struct gdbarch *gdbarch = objfile->arch ();
17116 const struct floatformat **format;
17117 struct type *type;
17118
17119 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17120 if (format)
17121 type = init_float_type (objfile, bits, name, format, byte_order);
17122 else
17123 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17124
17125 return type;
17126 }
17127
17128 /* Allocate an integer type of size BITS and name NAME. */
17129
17130 static struct type *
17131 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17132 int bits, int unsigned_p, const char *name)
17133 {
17134 struct type *type;
17135
17136 /* Versions of Intel's C Compiler generate an integer type called "void"
17137 instead of using DW_TAG_unspecified_type. This has been seen on
17138 at least versions 14, 17, and 18. */
17139 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17140 && strcmp (name, "void") == 0)
17141 type = objfile_type (objfile)->builtin_void;
17142 else
17143 type = init_integer_type (objfile, bits, unsigned_p, name);
17144
17145 return type;
17146 }
17147
17148 /* Initialise and return a floating point type of size BITS suitable for
17149 use as a component of a complex number. The NAME_HINT is passed through
17150 when initialising the floating point type and is the name of the complex
17151 type.
17152
17153 As DWARF doesn't currently provide an explicit name for the components
17154 of a complex number, but it can be helpful to have these components
17155 named, we try to select a suitable name based on the size of the
17156 component. */
17157 static struct type *
17158 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17159 struct objfile *objfile,
17160 int bits, const char *name_hint,
17161 enum bfd_endian byte_order)
17162 {
17163 gdbarch *gdbarch = objfile->arch ();
17164 struct type *tt = nullptr;
17165
17166 /* Try to find a suitable floating point builtin type of size BITS.
17167 We're going to use the name of this type as the name for the complex
17168 target type that we are about to create. */
17169 switch (cu->language)
17170 {
17171 case language_fortran:
17172 switch (bits)
17173 {
17174 case 32:
17175 tt = builtin_f_type (gdbarch)->builtin_real;
17176 break;
17177 case 64:
17178 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17179 break;
17180 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17181 case 128:
17182 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17183 break;
17184 }
17185 break;
17186 default:
17187 switch (bits)
17188 {
17189 case 32:
17190 tt = builtin_type (gdbarch)->builtin_float;
17191 break;
17192 case 64:
17193 tt = builtin_type (gdbarch)->builtin_double;
17194 break;
17195 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17196 case 128:
17197 tt = builtin_type (gdbarch)->builtin_long_double;
17198 break;
17199 }
17200 break;
17201 }
17202
17203 /* If the type we found doesn't match the size we were looking for, then
17204 pretend we didn't find a type at all, the complex target type we
17205 create will then be nameless. */
17206 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17207 tt = nullptr;
17208
17209 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17210 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17211 }
17212
17213 /* Find a representation of a given base type and install
17214 it in the TYPE field of the die. */
17215
17216 static struct type *
17217 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17218 {
17219 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17220 struct type *type;
17221 struct attribute *attr;
17222 int encoding = 0, bits = 0;
17223 const char *name;
17224 gdbarch *arch;
17225
17226 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17227 if (attr != nullptr)
17228 encoding = DW_UNSND (attr);
17229 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17230 if (attr != nullptr)
17231 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17232 name = dwarf2_name (die, cu);
17233 if (!name)
17234 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17235
17236 arch = objfile->arch ();
17237 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17238
17239 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17240 if (attr)
17241 {
17242 int endianity = DW_UNSND (attr);
17243
17244 switch (endianity)
17245 {
17246 case DW_END_big:
17247 byte_order = BFD_ENDIAN_BIG;
17248 break;
17249 case DW_END_little:
17250 byte_order = BFD_ENDIAN_LITTLE;
17251 break;
17252 default:
17253 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17254 break;
17255 }
17256 }
17257
17258 switch (encoding)
17259 {
17260 case DW_ATE_address:
17261 /* Turn DW_ATE_address into a void * pointer. */
17262 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17263 type = init_pointer_type (objfile, bits, name, type);
17264 break;
17265 case DW_ATE_boolean:
17266 type = init_boolean_type (objfile, bits, 1, name);
17267 break;
17268 case DW_ATE_complex_float:
17269 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17270 byte_order);
17271 if (TYPE_CODE (type) == TYPE_CODE_ERROR)
17272 {
17273 if (name == nullptr)
17274 {
17275 struct obstack *obstack
17276 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17277 name = obconcat (obstack, "_Complex ", TYPE_NAME (type),
17278 nullptr);
17279 }
17280 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17281 }
17282 else
17283 type = init_complex_type (name, type);
17284 break;
17285 case DW_ATE_decimal_float:
17286 type = init_decfloat_type (objfile, bits, name);
17287 break;
17288 case DW_ATE_float:
17289 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17290 break;
17291 case DW_ATE_signed:
17292 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17293 break;
17294 case DW_ATE_unsigned:
17295 if (cu->language == language_fortran
17296 && name
17297 && startswith (name, "character("))
17298 type = init_character_type (objfile, bits, 1, name);
17299 else
17300 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17301 break;
17302 case DW_ATE_signed_char:
17303 if (cu->language == language_ada || cu->language == language_m2
17304 || cu->language == language_pascal
17305 || cu->language == language_fortran)
17306 type = init_character_type (objfile, bits, 0, name);
17307 else
17308 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17309 break;
17310 case DW_ATE_unsigned_char:
17311 if (cu->language == language_ada || cu->language == language_m2
17312 || cu->language == language_pascal
17313 || cu->language == language_fortran
17314 || cu->language == language_rust)
17315 type = init_character_type (objfile, bits, 1, name);
17316 else
17317 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17318 break;
17319 case DW_ATE_UTF:
17320 {
17321 if (bits == 16)
17322 type = builtin_type (arch)->builtin_char16;
17323 else if (bits == 32)
17324 type = builtin_type (arch)->builtin_char32;
17325 else
17326 {
17327 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17328 bits);
17329 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17330 }
17331 return set_die_type (die, type, cu);
17332 }
17333 break;
17334
17335 default:
17336 complaint (_("unsupported DW_AT_encoding: '%s'"),
17337 dwarf_type_encoding_name (encoding));
17338 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17339 break;
17340 }
17341
17342 if (name && strcmp (name, "char") == 0)
17343 TYPE_NOSIGN (type) = 1;
17344
17345 maybe_set_alignment (cu, die, type);
17346
17347 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17348
17349 return set_die_type (die, type, cu);
17350 }
17351
17352 /* Parse dwarf attribute if it's a block, reference or constant and put the
17353 resulting value of the attribute into struct bound_prop.
17354 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17355
17356 static int
17357 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17358 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17359 struct type *default_type)
17360 {
17361 struct dwarf2_property_baton *baton;
17362 struct obstack *obstack
17363 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17364
17365 gdb_assert (default_type != NULL);
17366
17367 if (attr == NULL || prop == NULL)
17368 return 0;
17369
17370 if (attr->form_is_block ())
17371 {
17372 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17373 baton->property_type = default_type;
17374 baton->locexpr.per_cu = cu->per_cu;
17375 baton->locexpr.size = DW_BLOCK (attr)->size;
17376 baton->locexpr.data = DW_BLOCK (attr)->data;
17377 switch (attr->name)
17378 {
17379 case DW_AT_string_length:
17380 baton->locexpr.is_reference = true;
17381 break;
17382 default:
17383 baton->locexpr.is_reference = false;
17384 break;
17385 }
17386 prop->data.baton = baton;
17387 prop->kind = PROP_LOCEXPR;
17388 gdb_assert (prop->data.baton != NULL);
17389 }
17390 else if (attr->form_is_ref ())
17391 {
17392 struct dwarf2_cu *target_cu = cu;
17393 struct die_info *target_die;
17394 struct attribute *target_attr;
17395
17396 target_die = follow_die_ref (die, attr, &target_cu);
17397 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17398 if (target_attr == NULL)
17399 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17400 target_cu);
17401 if (target_attr == NULL)
17402 return 0;
17403
17404 switch (target_attr->name)
17405 {
17406 case DW_AT_location:
17407 if (target_attr->form_is_section_offset ())
17408 {
17409 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17410 baton->property_type = die_type (target_die, target_cu);
17411 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17412 prop->data.baton = baton;
17413 prop->kind = PROP_LOCLIST;
17414 gdb_assert (prop->data.baton != NULL);
17415 }
17416 else if (target_attr->form_is_block ())
17417 {
17418 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17419 baton->property_type = die_type (target_die, target_cu);
17420 baton->locexpr.per_cu = cu->per_cu;
17421 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17422 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17423 baton->locexpr.is_reference = true;
17424 prop->data.baton = baton;
17425 prop->kind = PROP_LOCEXPR;
17426 gdb_assert (prop->data.baton != NULL);
17427 }
17428 else
17429 {
17430 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17431 "dynamic property");
17432 return 0;
17433 }
17434 break;
17435 case DW_AT_data_member_location:
17436 {
17437 LONGEST offset;
17438
17439 if (!handle_data_member_location (target_die, target_cu,
17440 &offset))
17441 return 0;
17442
17443 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17444 baton->property_type = read_type_die (target_die->parent,
17445 target_cu);
17446 baton->offset_info.offset = offset;
17447 baton->offset_info.type = die_type (target_die, target_cu);
17448 prop->data.baton = baton;
17449 prop->kind = PROP_ADDR_OFFSET;
17450 break;
17451 }
17452 }
17453 }
17454 else if (attr->form_is_constant ())
17455 {
17456 prop->data.const_val = attr->constant_value (0);
17457 prop->kind = PROP_CONST;
17458 }
17459 else
17460 {
17461 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17462 dwarf2_name (die, cu));
17463 return 0;
17464 }
17465
17466 return 1;
17467 }
17468
17469 /* See read.h. */
17470
17471 struct type *
17472 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17473 {
17474 struct objfile *objfile = dwarf2_per_objfile->objfile;
17475 struct type *int_type;
17476
17477 /* Helper macro to examine the various builtin types. */
17478 #define TRY_TYPE(F) \
17479 int_type = (unsigned_p \
17480 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17481 : objfile_type (objfile)->builtin_ ## F); \
17482 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17483 return int_type
17484
17485 TRY_TYPE (char);
17486 TRY_TYPE (short);
17487 TRY_TYPE (int);
17488 TRY_TYPE (long);
17489 TRY_TYPE (long_long);
17490
17491 #undef TRY_TYPE
17492
17493 gdb_assert_not_reached ("unable to find suitable integer type");
17494 }
17495
17496 /* See read.h. */
17497
17498 struct type *
17499 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17500 {
17501 int addr_size = this->addr_size ();
17502 return int_type (addr_size, unsigned_p);
17503 }
17504
17505 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17506 present (which is valid) then compute the default type based on the
17507 compilation units address size. */
17508
17509 static struct type *
17510 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17511 {
17512 struct type *index_type = die_type (die, cu);
17513
17514 /* Dwarf-2 specifications explicitly allows to create subrange types
17515 without specifying a base type.
17516 In that case, the base type must be set to the type of
17517 the lower bound, upper bound or count, in that order, if any of these
17518 three attributes references an object that has a type.
17519 If no base type is found, the Dwarf-2 specifications say that
17520 a signed integer type of size equal to the size of an address should
17521 be used.
17522 For the following C code: `extern char gdb_int [];'
17523 GCC produces an empty range DIE.
17524 FIXME: muller/2010-05-28: Possible references to object for low bound,
17525 high bound or count are not yet handled by this code. */
17526 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17527 index_type = cu->per_cu->addr_sized_int_type (false);
17528
17529 return index_type;
17530 }
17531
17532 /* Read the given DW_AT_subrange DIE. */
17533
17534 static struct type *
17535 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17536 {
17537 struct type *base_type, *orig_base_type;
17538 struct type *range_type;
17539 struct attribute *attr;
17540 struct dynamic_prop low, high;
17541 int low_default_is_valid;
17542 int high_bound_is_count = 0;
17543 const char *name;
17544 ULONGEST negative_mask;
17545
17546 orig_base_type = read_subrange_index_type (die, cu);
17547
17548 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17549 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17550 creating the range type, but we use the result of check_typedef
17551 when examining properties of the type. */
17552 base_type = check_typedef (orig_base_type);
17553
17554 /* The die_type call above may have already set the type for this DIE. */
17555 range_type = get_die_type (die, cu);
17556 if (range_type)
17557 return range_type;
17558
17559 low.kind = PROP_CONST;
17560 high.kind = PROP_CONST;
17561 high.data.const_val = 0;
17562
17563 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17564 omitting DW_AT_lower_bound. */
17565 switch (cu->language)
17566 {
17567 case language_c:
17568 case language_cplus:
17569 low.data.const_val = 0;
17570 low_default_is_valid = 1;
17571 break;
17572 case language_fortran:
17573 low.data.const_val = 1;
17574 low_default_is_valid = 1;
17575 break;
17576 case language_d:
17577 case language_objc:
17578 case language_rust:
17579 low.data.const_val = 0;
17580 low_default_is_valid = (cu->header.version >= 4);
17581 break;
17582 case language_ada:
17583 case language_m2:
17584 case language_pascal:
17585 low.data.const_val = 1;
17586 low_default_is_valid = (cu->header.version >= 4);
17587 break;
17588 default:
17589 low.data.const_val = 0;
17590 low_default_is_valid = 0;
17591 break;
17592 }
17593
17594 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17595 if (attr != nullptr)
17596 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17597 else if (!low_default_is_valid)
17598 complaint (_("Missing DW_AT_lower_bound "
17599 "- DIE at %s [in module %s]"),
17600 sect_offset_str (die->sect_off),
17601 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17602
17603 struct attribute *attr_ub, *attr_count;
17604 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17605 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17606 {
17607 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17608 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17609 {
17610 /* If bounds are constant do the final calculation here. */
17611 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17612 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17613 else
17614 high_bound_is_count = 1;
17615 }
17616 else
17617 {
17618 if (attr_ub != NULL)
17619 complaint (_("Unresolved DW_AT_upper_bound "
17620 "- DIE at %s [in module %s]"),
17621 sect_offset_str (die->sect_off),
17622 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17623 if (attr_count != NULL)
17624 complaint (_("Unresolved DW_AT_count "
17625 "- DIE at %s [in module %s]"),
17626 sect_offset_str (die->sect_off),
17627 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17628 }
17629 }
17630
17631 LONGEST bias = 0;
17632 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17633 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17634 bias = bias_attr->constant_value (0);
17635
17636 /* Normally, the DWARF producers are expected to use a signed
17637 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17638 But this is unfortunately not always the case, as witnessed
17639 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17640 is used instead. To work around that ambiguity, we treat
17641 the bounds as signed, and thus sign-extend their values, when
17642 the base type is signed. */
17643 negative_mask =
17644 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17645 if (low.kind == PROP_CONST
17646 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17647 low.data.const_val |= negative_mask;
17648 if (high.kind == PROP_CONST
17649 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17650 high.data.const_val |= negative_mask;
17651
17652 /* Check for bit and byte strides. */
17653 struct dynamic_prop byte_stride_prop;
17654 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17655 if (attr_byte_stride != nullptr)
17656 {
17657 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17658 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17659 prop_type);
17660 }
17661
17662 struct dynamic_prop bit_stride_prop;
17663 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17664 if (attr_bit_stride != nullptr)
17665 {
17666 /* It only makes sense to have either a bit or byte stride. */
17667 if (attr_byte_stride != nullptr)
17668 {
17669 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17670 "- DIE at %s [in module %s]"),
17671 sect_offset_str (die->sect_off),
17672 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17673 attr_bit_stride = nullptr;
17674 }
17675 else
17676 {
17677 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17678 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17679 prop_type);
17680 }
17681 }
17682
17683 if (attr_byte_stride != nullptr
17684 || attr_bit_stride != nullptr)
17685 {
17686 bool byte_stride_p = (attr_byte_stride != nullptr);
17687 struct dynamic_prop *stride
17688 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17689
17690 range_type
17691 = create_range_type_with_stride (NULL, orig_base_type, &low,
17692 &high, bias, stride, byte_stride_p);
17693 }
17694 else
17695 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17696
17697 if (high_bound_is_count)
17698 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17699
17700 /* Ada expects an empty array on no boundary attributes. */
17701 if (attr == NULL && cu->language != language_ada)
17702 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17703
17704 name = dwarf2_name (die, cu);
17705 if (name)
17706 TYPE_NAME (range_type) = name;
17707
17708 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17709 if (attr != nullptr)
17710 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17711
17712 maybe_set_alignment (cu, die, range_type);
17713
17714 set_die_type (die, range_type, cu);
17715
17716 /* set_die_type should be already done. */
17717 set_descriptive_type (range_type, die, cu);
17718
17719 return range_type;
17720 }
17721
17722 static struct type *
17723 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17724 {
17725 struct type *type;
17726
17727 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17728 NULL);
17729 TYPE_NAME (type) = dwarf2_name (die, cu);
17730
17731 /* In Ada, an unspecified type is typically used when the description
17732 of the type is deferred to a different unit. When encountering
17733 such a type, we treat it as a stub, and try to resolve it later on,
17734 when needed. */
17735 if (cu->language == language_ada)
17736 TYPE_STUB (type) = 1;
17737
17738 return set_die_type (die, type, cu);
17739 }
17740
17741 /* Read a single die and all its descendents. Set the die's sibling
17742 field to NULL; set other fields in the die correctly, and set all
17743 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17744 location of the info_ptr after reading all of those dies. PARENT
17745 is the parent of the die in question. */
17746
17747 static struct die_info *
17748 read_die_and_children (const struct die_reader_specs *reader,
17749 const gdb_byte *info_ptr,
17750 const gdb_byte **new_info_ptr,
17751 struct die_info *parent)
17752 {
17753 struct die_info *die;
17754 const gdb_byte *cur_ptr;
17755
17756 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17757 if (die == NULL)
17758 {
17759 *new_info_ptr = cur_ptr;
17760 return NULL;
17761 }
17762 store_in_ref_table (die, reader->cu);
17763
17764 if (die->has_children)
17765 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17766 else
17767 {
17768 die->child = NULL;
17769 *new_info_ptr = cur_ptr;
17770 }
17771
17772 die->sibling = NULL;
17773 die->parent = parent;
17774 return die;
17775 }
17776
17777 /* Read a die, all of its descendents, and all of its siblings; set
17778 all of the fields of all of the dies correctly. Arguments are as
17779 in read_die_and_children. */
17780
17781 static struct die_info *
17782 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17783 const gdb_byte *info_ptr,
17784 const gdb_byte **new_info_ptr,
17785 struct die_info *parent)
17786 {
17787 struct die_info *first_die, *last_sibling;
17788 const gdb_byte *cur_ptr;
17789
17790 cur_ptr = info_ptr;
17791 first_die = last_sibling = NULL;
17792
17793 while (1)
17794 {
17795 struct die_info *die
17796 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17797
17798 if (die == NULL)
17799 {
17800 *new_info_ptr = cur_ptr;
17801 return first_die;
17802 }
17803
17804 if (!first_die)
17805 first_die = die;
17806 else
17807 last_sibling->sibling = die;
17808
17809 last_sibling = die;
17810 }
17811 }
17812
17813 /* Read a die, all of its descendents, and all of its siblings; set
17814 all of the fields of all of the dies correctly. Arguments are as
17815 in read_die_and_children.
17816 This the main entry point for reading a DIE and all its children. */
17817
17818 static struct die_info *
17819 read_die_and_siblings (const struct die_reader_specs *reader,
17820 const gdb_byte *info_ptr,
17821 const gdb_byte **new_info_ptr,
17822 struct die_info *parent)
17823 {
17824 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17825 new_info_ptr, parent);
17826
17827 if (dwarf_die_debug)
17828 {
17829 fprintf_unfiltered (gdb_stdlog,
17830 "Read die from %s@0x%x of %s:\n",
17831 reader->die_section->get_name (),
17832 (unsigned) (info_ptr - reader->die_section->buffer),
17833 bfd_get_filename (reader->abfd));
17834 dump_die (die, dwarf_die_debug);
17835 }
17836
17837 return die;
17838 }
17839
17840 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17841 attributes.
17842 The caller is responsible for filling in the extra attributes
17843 and updating (*DIEP)->num_attrs.
17844 Set DIEP to point to a newly allocated die with its information,
17845 except for its child, sibling, and parent fields. */
17846
17847 static const gdb_byte *
17848 read_full_die_1 (const struct die_reader_specs *reader,
17849 struct die_info **diep, const gdb_byte *info_ptr,
17850 int num_extra_attrs)
17851 {
17852 unsigned int abbrev_number, bytes_read, i;
17853 struct abbrev_info *abbrev;
17854 struct die_info *die;
17855 struct dwarf2_cu *cu = reader->cu;
17856 bfd *abfd = reader->abfd;
17857
17858 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17859 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17860 info_ptr += bytes_read;
17861 if (!abbrev_number)
17862 {
17863 *diep = NULL;
17864 return info_ptr;
17865 }
17866
17867 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17868 if (!abbrev)
17869 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17870 abbrev_number,
17871 bfd_get_filename (abfd));
17872
17873 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17874 die->sect_off = sect_off;
17875 die->tag = abbrev->tag;
17876 die->abbrev = abbrev_number;
17877 die->has_children = abbrev->has_children;
17878
17879 /* Make the result usable.
17880 The caller needs to update num_attrs after adding the extra
17881 attributes. */
17882 die->num_attrs = abbrev->num_attrs;
17883
17884 std::vector<int> indexes_that_need_reprocess;
17885 for (i = 0; i < abbrev->num_attrs; ++i)
17886 {
17887 bool need_reprocess;
17888 info_ptr =
17889 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17890 info_ptr, &need_reprocess);
17891 if (need_reprocess)
17892 indexes_that_need_reprocess.push_back (i);
17893 }
17894
17895 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
17896 if (attr != nullptr)
17897 cu->str_offsets_base = DW_UNSND (attr);
17898
17899 attr = die->attr (DW_AT_loclists_base);
17900 if (attr != nullptr)
17901 cu->loclist_base = DW_UNSND (attr);
17902
17903 auto maybe_addr_base = die->addr_base ();
17904 if (maybe_addr_base.has_value ())
17905 cu->addr_base = *maybe_addr_base;
17906 for (int index : indexes_that_need_reprocess)
17907 read_attribute_reprocess (reader, &die->attrs[index]);
17908 *diep = die;
17909 return info_ptr;
17910 }
17911
17912 /* Read a die and all its attributes.
17913 Set DIEP to point to a newly allocated die with its information,
17914 except for its child, sibling, and parent fields. */
17915
17916 static const gdb_byte *
17917 read_full_die (const struct die_reader_specs *reader,
17918 struct die_info **diep, const gdb_byte *info_ptr)
17919 {
17920 const gdb_byte *result;
17921
17922 result = read_full_die_1 (reader, diep, info_ptr, 0);
17923
17924 if (dwarf_die_debug)
17925 {
17926 fprintf_unfiltered (gdb_stdlog,
17927 "Read die from %s@0x%x of %s:\n",
17928 reader->die_section->get_name (),
17929 (unsigned) (info_ptr - reader->die_section->buffer),
17930 bfd_get_filename (reader->abfd));
17931 dump_die (*diep, dwarf_die_debug);
17932 }
17933
17934 return result;
17935 }
17936 \f
17937
17938 /* Returns nonzero if TAG represents a type that we might generate a partial
17939 symbol for. */
17940
17941 static int
17942 is_type_tag_for_partial (int tag)
17943 {
17944 switch (tag)
17945 {
17946 #if 0
17947 /* Some types that would be reasonable to generate partial symbols for,
17948 that we don't at present. */
17949 case DW_TAG_array_type:
17950 case DW_TAG_file_type:
17951 case DW_TAG_ptr_to_member_type:
17952 case DW_TAG_set_type:
17953 case DW_TAG_string_type:
17954 case DW_TAG_subroutine_type:
17955 #endif
17956 case DW_TAG_base_type:
17957 case DW_TAG_class_type:
17958 case DW_TAG_interface_type:
17959 case DW_TAG_enumeration_type:
17960 case DW_TAG_structure_type:
17961 case DW_TAG_subrange_type:
17962 case DW_TAG_typedef:
17963 case DW_TAG_union_type:
17964 return 1;
17965 default:
17966 return 0;
17967 }
17968 }
17969
17970 /* Load all DIEs that are interesting for partial symbols into memory. */
17971
17972 static struct partial_die_info *
17973 load_partial_dies (const struct die_reader_specs *reader,
17974 const gdb_byte *info_ptr, int building_psymtab)
17975 {
17976 struct dwarf2_cu *cu = reader->cu;
17977 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17978 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17979 unsigned int bytes_read;
17980 unsigned int load_all = 0;
17981 int nesting_level = 1;
17982
17983 parent_die = NULL;
17984 last_die = NULL;
17985
17986 gdb_assert (cu->per_cu != NULL);
17987 if (cu->per_cu->load_all_dies)
17988 load_all = 1;
17989
17990 cu->partial_dies
17991 = htab_create_alloc_ex (cu->header.length / 12,
17992 partial_die_hash,
17993 partial_die_eq,
17994 NULL,
17995 &cu->comp_unit_obstack,
17996 hashtab_obstack_allocate,
17997 dummy_obstack_deallocate);
17998
17999 while (1)
18000 {
18001 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18002
18003 /* A NULL abbrev means the end of a series of children. */
18004 if (abbrev == NULL)
18005 {
18006 if (--nesting_level == 0)
18007 return first_die;
18008
18009 info_ptr += bytes_read;
18010 last_die = parent_die;
18011 parent_die = parent_die->die_parent;
18012 continue;
18013 }
18014
18015 /* Check for template arguments. We never save these; if
18016 they're seen, we just mark the parent, and go on our way. */
18017 if (parent_die != NULL
18018 && cu->language == language_cplus
18019 && (abbrev->tag == DW_TAG_template_type_param
18020 || abbrev->tag == DW_TAG_template_value_param))
18021 {
18022 parent_die->has_template_arguments = 1;
18023
18024 if (!load_all)
18025 {
18026 /* We don't need a partial DIE for the template argument. */
18027 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18028 continue;
18029 }
18030 }
18031
18032 /* We only recurse into c++ subprograms looking for template arguments.
18033 Skip their other children. */
18034 if (!load_all
18035 && cu->language == language_cplus
18036 && parent_die != NULL
18037 && parent_die->tag == DW_TAG_subprogram)
18038 {
18039 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18040 continue;
18041 }
18042
18043 /* Check whether this DIE is interesting enough to save. Normally
18044 we would not be interested in members here, but there may be
18045 later variables referencing them via DW_AT_specification (for
18046 static members). */
18047 if (!load_all
18048 && !is_type_tag_for_partial (abbrev->tag)
18049 && abbrev->tag != DW_TAG_constant
18050 && abbrev->tag != DW_TAG_enumerator
18051 && abbrev->tag != DW_TAG_subprogram
18052 && abbrev->tag != DW_TAG_inlined_subroutine
18053 && abbrev->tag != DW_TAG_lexical_block
18054 && abbrev->tag != DW_TAG_variable
18055 && abbrev->tag != DW_TAG_namespace
18056 && abbrev->tag != DW_TAG_module
18057 && abbrev->tag != DW_TAG_member
18058 && abbrev->tag != DW_TAG_imported_unit
18059 && abbrev->tag != DW_TAG_imported_declaration)
18060 {
18061 /* Otherwise we skip to the next sibling, if any. */
18062 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18063 continue;
18064 }
18065
18066 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18067 abbrev);
18068
18069 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18070
18071 /* This two-pass algorithm for processing partial symbols has a
18072 high cost in cache pressure. Thus, handle some simple cases
18073 here which cover the majority of C partial symbols. DIEs
18074 which neither have specification tags in them, nor could have
18075 specification tags elsewhere pointing at them, can simply be
18076 processed and discarded.
18077
18078 This segment is also optional; scan_partial_symbols and
18079 add_partial_symbol will handle these DIEs if we chain
18080 them in normally. When compilers which do not emit large
18081 quantities of duplicate debug information are more common,
18082 this code can probably be removed. */
18083
18084 /* Any complete simple types at the top level (pretty much all
18085 of them, for a language without namespaces), can be processed
18086 directly. */
18087 if (parent_die == NULL
18088 && pdi.has_specification == 0
18089 && pdi.is_declaration == 0
18090 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18091 || pdi.tag == DW_TAG_base_type
18092 || pdi.tag == DW_TAG_subrange_type))
18093 {
18094 if (building_psymtab && pdi.name != NULL)
18095 add_psymbol_to_list (pdi.name, false,
18096 VAR_DOMAIN, LOC_TYPEDEF, -1,
18097 psymbol_placement::STATIC,
18098 0, cu->language, objfile);
18099 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18100 continue;
18101 }
18102
18103 /* The exception for DW_TAG_typedef with has_children above is
18104 a workaround of GCC PR debug/47510. In the case of this complaint
18105 type_name_or_error will error on such types later.
18106
18107 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18108 it could not find the child DIEs referenced later, this is checked
18109 above. In correct DWARF DW_TAG_typedef should have no children. */
18110
18111 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18112 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18113 "- DIE at %s [in module %s]"),
18114 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18115
18116 /* If we're at the second level, and we're an enumerator, and
18117 our parent has no specification (meaning possibly lives in a
18118 namespace elsewhere), then we can add the partial symbol now
18119 instead of queueing it. */
18120 if (pdi.tag == DW_TAG_enumerator
18121 && parent_die != NULL
18122 && parent_die->die_parent == NULL
18123 && parent_die->tag == DW_TAG_enumeration_type
18124 && parent_die->has_specification == 0)
18125 {
18126 if (pdi.name == NULL)
18127 complaint (_("malformed enumerator DIE ignored"));
18128 else if (building_psymtab)
18129 add_psymbol_to_list (pdi.name, false,
18130 VAR_DOMAIN, LOC_CONST, -1,
18131 cu->language == language_cplus
18132 ? psymbol_placement::GLOBAL
18133 : psymbol_placement::STATIC,
18134 0, cu->language, objfile);
18135
18136 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18137 continue;
18138 }
18139
18140 struct partial_die_info *part_die
18141 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18142
18143 /* We'll save this DIE so link it in. */
18144 part_die->die_parent = parent_die;
18145 part_die->die_sibling = NULL;
18146 part_die->die_child = NULL;
18147
18148 if (last_die && last_die == parent_die)
18149 last_die->die_child = part_die;
18150 else if (last_die)
18151 last_die->die_sibling = part_die;
18152
18153 last_die = part_die;
18154
18155 if (first_die == NULL)
18156 first_die = part_die;
18157
18158 /* Maybe add the DIE to the hash table. Not all DIEs that we
18159 find interesting need to be in the hash table, because we
18160 also have the parent/sibling/child chains; only those that we
18161 might refer to by offset later during partial symbol reading.
18162
18163 For now this means things that might have be the target of a
18164 DW_AT_specification, DW_AT_abstract_origin, or
18165 DW_AT_extension. DW_AT_extension will refer only to
18166 namespaces; DW_AT_abstract_origin refers to functions (and
18167 many things under the function DIE, but we do not recurse
18168 into function DIEs during partial symbol reading) and
18169 possibly variables as well; DW_AT_specification refers to
18170 declarations. Declarations ought to have the DW_AT_declaration
18171 flag. It happens that GCC forgets to put it in sometimes, but
18172 only for functions, not for types.
18173
18174 Adding more things than necessary to the hash table is harmless
18175 except for the performance cost. Adding too few will result in
18176 wasted time in find_partial_die, when we reread the compilation
18177 unit with load_all_dies set. */
18178
18179 if (load_all
18180 || abbrev->tag == DW_TAG_constant
18181 || abbrev->tag == DW_TAG_subprogram
18182 || abbrev->tag == DW_TAG_variable
18183 || abbrev->tag == DW_TAG_namespace
18184 || part_die->is_declaration)
18185 {
18186 void **slot;
18187
18188 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18189 to_underlying (part_die->sect_off),
18190 INSERT);
18191 *slot = part_die;
18192 }
18193
18194 /* For some DIEs we want to follow their children (if any). For C
18195 we have no reason to follow the children of structures; for other
18196 languages we have to, so that we can get at method physnames
18197 to infer fully qualified class names, for DW_AT_specification,
18198 and for C++ template arguments. For C++, we also look one level
18199 inside functions to find template arguments (if the name of the
18200 function does not already contain the template arguments).
18201
18202 For Ada and Fortran, we need to scan the children of subprograms
18203 and lexical blocks as well because these languages allow the
18204 definition of nested entities that could be interesting for the
18205 debugger, such as nested subprograms for instance. */
18206 if (last_die->has_children
18207 && (load_all
18208 || last_die->tag == DW_TAG_namespace
18209 || last_die->tag == DW_TAG_module
18210 || last_die->tag == DW_TAG_enumeration_type
18211 || (cu->language == language_cplus
18212 && last_die->tag == DW_TAG_subprogram
18213 && (last_die->name == NULL
18214 || strchr (last_die->name, '<') == NULL))
18215 || (cu->language != language_c
18216 && (last_die->tag == DW_TAG_class_type
18217 || last_die->tag == DW_TAG_interface_type
18218 || last_die->tag == DW_TAG_structure_type
18219 || last_die->tag == DW_TAG_union_type))
18220 || ((cu->language == language_ada
18221 || cu->language == language_fortran)
18222 && (last_die->tag == DW_TAG_subprogram
18223 || last_die->tag == DW_TAG_lexical_block))))
18224 {
18225 nesting_level++;
18226 parent_die = last_die;
18227 continue;
18228 }
18229
18230 /* Otherwise we skip to the next sibling, if any. */
18231 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18232
18233 /* Back to the top, do it again. */
18234 }
18235 }
18236
18237 partial_die_info::partial_die_info (sect_offset sect_off_,
18238 struct abbrev_info *abbrev)
18239 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18240 {
18241 }
18242
18243 /* Read a minimal amount of information into the minimal die structure.
18244 INFO_PTR should point just after the initial uleb128 of a DIE. */
18245
18246 const gdb_byte *
18247 partial_die_info::read (const struct die_reader_specs *reader,
18248 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18249 {
18250 struct dwarf2_cu *cu = reader->cu;
18251 struct dwarf2_per_objfile *dwarf2_per_objfile
18252 = cu->per_cu->dwarf2_per_objfile;
18253 unsigned int i;
18254 int has_low_pc_attr = 0;
18255 int has_high_pc_attr = 0;
18256 int high_pc_relative = 0;
18257
18258 for (i = 0; i < abbrev.num_attrs; ++i)
18259 {
18260 attribute attr;
18261 bool need_reprocess;
18262 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18263 info_ptr, &need_reprocess);
18264 /* String and address offsets that need to do the reprocessing have
18265 already been read at this point, so there is no need to wait until
18266 the loop terminates to do the reprocessing. */
18267 if (need_reprocess)
18268 read_attribute_reprocess (reader, &attr);
18269 /* Store the data if it is of an attribute we want to keep in a
18270 partial symbol table. */
18271 switch (attr.name)
18272 {
18273 case DW_AT_name:
18274 switch (tag)
18275 {
18276 case DW_TAG_compile_unit:
18277 case DW_TAG_partial_unit:
18278 case DW_TAG_type_unit:
18279 /* Compilation units have a DW_AT_name that is a filename, not
18280 a source language identifier. */
18281 case DW_TAG_enumeration_type:
18282 case DW_TAG_enumerator:
18283 /* These tags always have simple identifiers already; no need
18284 to canonicalize them. */
18285 name = DW_STRING (&attr);
18286 break;
18287 default:
18288 {
18289 struct objfile *objfile = dwarf2_per_objfile->objfile;
18290
18291 name
18292 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18293 }
18294 break;
18295 }
18296 break;
18297 case DW_AT_linkage_name:
18298 case DW_AT_MIPS_linkage_name:
18299 /* Note that both forms of linkage name might appear. We
18300 assume they will be the same, and we only store the last
18301 one we see. */
18302 linkage_name = attr.value_as_string ();
18303 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18304 See https://github.com/rust-lang/rust/issues/32925. */
18305 if (cu->language == language_rust && linkage_name != NULL
18306 && strchr (linkage_name, '{') != NULL)
18307 linkage_name = NULL;
18308 break;
18309 case DW_AT_low_pc:
18310 has_low_pc_attr = 1;
18311 lowpc = attr.value_as_address ();
18312 break;
18313 case DW_AT_high_pc:
18314 has_high_pc_attr = 1;
18315 highpc = attr.value_as_address ();
18316 if (cu->header.version >= 4 && attr.form_is_constant ())
18317 high_pc_relative = 1;
18318 break;
18319 case DW_AT_location:
18320 /* Support the .debug_loc offsets. */
18321 if (attr.form_is_block ())
18322 {
18323 d.locdesc = DW_BLOCK (&attr);
18324 }
18325 else if (attr.form_is_section_offset ())
18326 {
18327 dwarf2_complex_location_expr_complaint ();
18328 }
18329 else
18330 {
18331 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18332 "partial symbol information");
18333 }
18334 break;
18335 case DW_AT_external:
18336 is_external = DW_UNSND (&attr);
18337 break;
18338 case DW_AT_declaration:
18339 is_declaration = DW_UNSND (&attr);
18340 break;
18341 case DW_AT_type:
18342 has_type = 1;
18343 break;
18344 case DW_AT_abstract_origin:
18345 case DW_AT_specification:
18346 case DW_AT_extension:
18347 has_specification = 1;
18348 spec_offset = attr.get_ref_die_offset ();
18349 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18350 || cu->per_cu->is_dwz);
18351 break;
18352 case DW_AT_sibling:
18353 /* Ignore absolute siblings, they might point outside of
18354 the current compile unit. */
18355 if (attr.form == DW_FORM_ref_addr)
18356 complaint (_("ignoring absolute DW_AT_sibling"));
18357 else
18358 {
18359 const gdb_byte *buffer = reader->buffer;
18360 sect_offset off = attr.get_ref_die_offset ();
18361 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18362
18363 if (sibling_ptr < info_ptr)
18364 complaint (_("DW_AT_sibling points backwards"));
18365 else if (sibling_ptr > reader->buffer_end)
18366 reader->die_section->overflow_complaint ();
18367 else
18368 sibling = sibling_ptr;
18369 }
18370 break;
18371 case DW_AT_byte_size:
18372 has_byte_size = 1;
18373 break;
18374 case DW_AT_const_value:
18375 has_const_value = 1;
18376 break;
18377 case DW_AT_calling_convention:
18378 /* DWARF doesn't provide a way to identify a program's source-level
18379 entry point. DW_AT_calling_convention attributes are only meant
18380 to describe functions' calling conventions.
18381
18382 However, because it's a necessary piece of information in
18383 Fortran, and before DWARF 4 DW_CC_program was the only
18384 piece of debugging information whose definition refers to
18385 a 'main program' at all, several compilers marked Fortran
18386 main programs with DW_CC_program --- even when those
18387 functions use the standard calling conventions.
18388
18389 Although DWARF now specifies a way to provide this
18390 information, we support this practice for backward
18391 compatibility. */
18392 if (DW_UNSND (&attr) == DW_CC_program
18393 && cu->language == language_fortran)
18394 main_subprogram = 1;
18395 break;
18396 case DW_AT_inline:
18397 if (DW_UNSND (&attr) == DW_INL_inlined
18398 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18399 may_be_inlined = 1;
18400 break;
18401
18402 case DW_AT_import:
18403 if (tag == DW_TAG_imported_unit)
18404 {
18405 d.sect_off = attr.get_ref_die_offset ();
18406 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18407 || cu->per_cu->is_dwz);
18408 }
18409 break;
18410
18411 case DW_AT_main_subprogram:
18412 main_subprogram = DW_UNSND (&attr);
18413 break;
18414
18415 case DW_AT_ranges:
18416 {
18417 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18418 but that requires a full DIE, so instead we just
18419 reimplement it. */
18420 int need_ranges_base = tag != DW_TAG_compile_unit;
18421 unsigned int ranges_offset = (DW_UNSND (&attr)
18422 + (need_ranges_base
18423 ? cu->ranges_base
18424 : 0));
18425
18426 /* Value of the DW_AT_ranges attribute is the offset in the
18427 .debug_ranges section. */
18428 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18429 nullptr))
18430 has_pc_info = 1;
18431 }
18432 break;
18433
18434 default:
18435 break;
18436 }
18437 }
18438
18439 /* For Ada, if both the name and the linkage name appear, we prefer
18440 the latter. This lets "catch exception" work better, regardless
18441 of the order in which the name and linkage name were emitted.
18442 Really, though, this is just a workaround for the fact that gdb
18443 doesn't store both the name and the linkage name. */
18444 if (cu->language == language_ada && linkage_name != nullptr)
18445 name = linkage_name;
18446
18447 if (high_pc_relative)
18448 highpc += lowpc;
18449
18450 if (has_low_pc_attr && has_high_pc_attr)
18451 {
18452 /* When using the GNU linker, .gnu.linkonce. sections are used to
18453 eliminate duplicate copies of functions and vtables and such.
18454 The linker will arbitrarily choose one and discard the others.
18455 The AT_*_pc values for such functions refer to local labels in
18456 these sections. If the section from that file was discarded, the
18457 labels are not in the output, so the relocs get a value of 0.
18458 If this is a discarded function, mark the pc bounds as invalid,
18459 so that GDB will ignore it. */
18460 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18461 {
18462 struct objfile *objfile = dwarf2_per_objfile->objfile;
18463 struct gdbarch *gdbarch = objfile->arch ();
18464
18465 complaint (_("DW_AT_low_pc %s is zero "
18466 "for DIE at %s [in module %s]"),
18467 paddress (gdbarch, lowpc),
18468 sect_offset_str (sect_off),
18469 objfile_name (objfile));
18470 }
18471 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18472 else if (lowpc >= highpc)
18473 {
18474 struct objfile *objfile = dwarf2_per_objfile->objfile;
18475 struct gdbarch *gdbarch = objfile->arch ();
18476
18477 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18478 "for DIE at %s [in module %s]"),
18479 paddress (gdbarch, lowpc),
18480 paddress (gdbarch, highpc),
18481 sect_offset_str (sect_off),
18482 objfile_name (objfile));
18483 }
18484 else
18485 has_pc_info = 1;
18486 }
18487
18488 return info_ptr;
18489 }
18490
18491 /* Find a cached partial DIE at OFFSET in CU. */
18492
18493 struct partial_die_info *
18494 dwarf2_cu::find_partial_die (sect_offset sect_off)
18495 {
18496 struct partial_die_info *lookup_die = NULL;
18497 struct partial_die_info part_die (sect_off);
18498
18499 lookup_die = ((struct partial_die_info *)
18500 htab_find_with_hash (partial_dies, &part_die,
18501 to_underlying (sect_off)));
18502
18503 return lookup_die;
18504 }
18505
18506 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18507 except in the case of .debug_types DIEs which do not reference
18508 outside their CU (they do however referencing other types via
18509 DW_FORM_ref_sig8). */
18510
18511 static const struct cu_partial_die_info
18512 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18513 {
18514 struct dwarf2_per_objfile *dwarf2_per_objfile
18515 = cu->per_cu->dwarf2_per_objfile;
18516 struct objfile *objfile = dwarf2_per_objfile->objfile;
18517 struct dwarf2_per_cu_data *per_cu = NULL;
18518 struct partial_die_info *pd = NULL;
18519
18520 if (offset_in_dwz == cu->per_cu->is_dwz
18521 && cu->header.offset_in_cu_p (sect_off))
18522 {
18523 pd = cu->find_partial_die (sect_off);
18524 if (pd != NULL)
18525 return { cu, pd };
18526 /* We missed recording what we needed.
18527 Load all dies and try again. */
18528 per_cu = cu->per_cu;
18529 }
18530 else
18531 {
18532 /* TUs don't reference other CUs/TUs (except via type signatures). */
18533 if (cu->per_cu->is_debug_types)
18534 {
18535 error (_("Dwarf Error: Type Unit at offset %s contains"
18536 " external reference to offset %s [in module %s].\n"),
18537 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18538 bfd_get_filename (objfile->obfd));
18539 }
18540 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18541 dwarf2_per_objfile);
18542
18543 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18544 load_partial_comp_unit (per_cu);
18545
18546 per_cu->cu->last_used = 0;
18547 pd = per_cu->cu->find_partial_die (sect_off);
18548 }
18549
18550 /* If we didn't find it, and not all dies have been loaded,
18551 load them all and try again. */
18552
18553 if (pd == NULL && per_cu->load_all_dies == 0)
18554 {
18555 per_cu->load_all_dies = 1;
18556
18557 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18558 THIS_CU->cu may already be in use. So we can't just free it and
18559 replace its DIEs with the ones we read in. Instead, we leave those
18560 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18561 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18562 set. */
18563 load_partial_comp_unit (per_cu);
18564
18565 pd = per_cu->cu->find_partial_die (sect_off);
18566 }
18567
18568 if (pd == NULL)
18569 internal_error (__FILE__, __LINE__,
18570 _("could not find partial DIE %s "
18571 "in cache [from module %s]\n"),
18572 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18573 return { per_cu->cu, pd };
18574 }
18575
18576 /* See if we can figure out if the class lives in a namespace. We do
18577 this by looking for a member function; its demangled name will
18578 contain namespace info, if there is any. */
18579
18580 static void
18581 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18582 struct dwarf2_cu *cu)
18583 {
18584 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18585 what template types look like, because the demangler
18586 frequently doesn't give the same name as the debug info. We
18587 could fix this by only using the demangled name to get the
18588 prefix (but see comment in read_structure_type). */
18589
18590 struct partial_die_info *real_pdi;
18591 struct partial_die_info *child_pdi;
18592
18593 /* If this DIE (this DIE's specification, if any) has a parent, then
18594 we should not do this. We'll prepend the parent's fully qualified
18595 name when we create the partial symbol. */
18596
18597 real_pdi = struct_pdi;
18598 while (real_pdi->has_specification)
18599 {
18600 auto res = find_partial_die (real_pdi->spec_offset,
18601 real_pdi->spec_is_dwz, cu);
18602 real_pdi = res.pdi;
18603 cu = res.cu;
18604 }
18605
18606 if (real_pdi->die_parent != NULL)
18607 return;
18608
18609 for (child_pdi = struct_pdi->die_child;
18610 child_pdi != NULL;
18611 child_pdi = child_pdi->die_sibling)
18612 {
18613 if (child_pdi->tag == DW_TAG_subprogram
18614 && child_pdi->linkage_name != NULL)
18615 {
18616 gdb::unique_xmalloc_ptr<char> actual_class_name
18617 (language_class_name_from_physname (cu->language_defn,
18618 child_pdi->linkage_name));
18619 if (actual_class_name != NULL)
18620 {
18621 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18622 struct_pdi->name = objfile->intern (actual_class_name.get ());
18623 }
18624 break;
18625 }
18626 }
18627 }
18628
18629 /* Return true if a DIE with TAG may have the DW_AT_const_value
18630 attribute. */
18631
18632 static bool
18633 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18634 {
18635 switch (tag)
18636 {
18637 case DW_TAG_constant:
18638 case DW_TAG_enumerator:
18639 case DW_TAG_formal_parameter:
18640 case DW_TAG_template_value_param:
18641 case DW_TAG_variable:
18642 return true;
18643 }
18644
18645 return false;
18646 }
18647
18648 void
18649 partial_die_info::fixup (struct dwarf2_cu *cu)
18650 {
18651 /* Once we've fixed up a die, there's no point in doing so again.
18652 This also avoids a memory leak if we were to call
18653 guess_partial_die_structure_name multiple times. */
18654 if (fixup_called)
18655 return;
18656
18657 /* If we found a reference attribute and the DIE has no name, try
18658 to find a name in the referred to DIE. */
18659
18660 if (name == NULL && has_specification)
18661 {
18662 struct partial_die_info *spec_die;
18663
18664 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18665 spec_die = res.pdi;
18666 cu = res.cu;
18667
18668 spec_die->fixup (cu);
18669
18670 if (spec_die->name)
18671 {
18672 name = spec_die->name;
18673
18674 /* Copy DW_AT_external attribute if it is set. */
18675 if (spec_die->is_external)
18676 is_external = spec_die->is_external;
18677 }
18678 }
18679
18680 if (!has_const_value && has_specification
18681 && can_have_DW_AT_const_value_p (tag))
18682 {
18683 struct partial_die_info *spec_die;
18684
18685 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18686 spec_die = res.pdi;
18687 cu = res.cu;
18688
18689 spec_die->fixup (cu);
18690
18691 if (spec_die->has_const_value)
18692 {
18693 /* Copy DW_AT_const_value attribute if it is set. */
18694 has_const_value = spec_die->has_const_value;
18695 }
18696 }
18697
18698 /* Set default names for some unnamed DIEs. */
18699
18700 if (name == NULL && tag == DW_TAG_namespace)
18701 name = CP_ANONYMOUS_NAMESPACE_STR;
18702
18703 /* If there is no parent die to provide a namespace, and there are
18704 children, see if we can determine the namespace from their linkage
18705 name. */
18706 if (cu->language == language_cplus
18707 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18708 && die_parent == NULL
18709 && has_children
18710 && (tag == DW_TAG_class_type
18711 || tag == DW_TAG_structure_type
18712 || tag == DW_TAG_union_type))
18713 guess_partial_die_structure_name (this, cu);
18714
18715 /* GCC might emit a nameless struct or union that has a linkage
18716 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18717 if (name == NULL
18718 && (tag == DW_TAG_class_type
18719 || tag == DW_TAG_interface_type
18720 || tag == DW_TAG_structure_type
18721 || tag == DW_TAG_union_type)
18722 && linkage_name != NULL)
18723 {
18724 gdb::unique_xmalloc_ptr<char> demangled
18725 (gdb_demangle (linkage_name, DMGL_TYPES));
18726 if (demangled != nullptr)
18727 {
18728 const char *base;
18729
18730 /* Strip any leading namespaces/classes, keep only the base name.
18731 DW_AT_name for named DIEs does not contain the prefixes. */
18732 base = strrchr (demangled.get (), ':');
18733 if (base && base > demangled.get () && base[-1] == ':')
18734 base++;
18735 else
18736 base = demangled.get ();
18737
18738 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18739 name = objfile->intern (base);
18740 }
18741 }
18742
18743 fixup_called = 1;
18744 }
18745
18746 /* Read the .debug_loclists header contents from the given SECTION in the
18747 HEADER. */
18748 static void
18749 read_loclist_header (struct loclist_header *header,
18750 struct dwarf2_section_info *section)
18751 {
18752 unsigned int bytes_read;
18753 bfd *abfd = section->get_bfd_owner ();
18754 const gdb_byte *info_ptr = section->buffer;
18755 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18756 info_ptr += bytes_read;
18757 header->version = read_2_bytes (abfd, info_ptr);
18758 info_ptr += 2;
18759 header->addr_size = read_1_byte (abfd, info_ptr);
18760 info_ptr += 1;
18761 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18762 info_ptr += 1;
18763 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18764 }
18765
18766 /* Return the DW_AT_loclists_base value for the CU. */
18767 static ULONGEST
18768 lookup_loclist_base (struct dwarf2_cu *cu)
18769 {
18770 /* For the .dwo unit, the loclist_base points to the first offset following
18771 the header. The header consists of the following entities-
18772 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18773 bit format)
18774 2. version (2 bytes)
18775 3. address size (1 byte)
18776 4. segment selector size (1 byte)
18777 5. offset entry count (4 bytes)
18778 These sizes are derived as per the DWARFv5 standard. */
18779 if (cu->dwo_unit != nullptr)
18780 {
18781 if (cu->header.initial_length_size == 4)
18782 return LOCLIST_HEADER_SIZE32;
18783 return LOCLIST_HEADER_SIZE64;
18784 }
18785 return cu->loclist_base;
18786 }
18787
18788 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18789 array of offsets in the .debug_loclists section. */
18790 static CORE_ADDR
18791 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18792 {
18793 struct dwarf2_per_objfile *dwarf2_per_objfile
18794 = cu->per_cu->dwarf2_per_objfile;
18795 struct objfile *objfile = dwarf2_per_objfile->objfile;
18796 bfd *abfd = objfile->obfd;
18797 ULONGEST loclist_base = lookup_loclist_base (cu);
18798 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18799
18800 section->read (objfile);
18801 if (section->buffer == NULL)
18802 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18803 "section [in module %s]"), objfile_name (objfile));
18804 struct loclist_header header;
18805 read_loclist_header (&header, section);
18806 if (loclist_index >= header.offset_entry_count)
18807 complaint (_("DW_FORM_loclistx pointing outside of "
18808 ".debug_loclists offset array [in module %s]"),
18809 objfile_name (objfile));
18810 if (loclist_base + loclist_index * cu->header.offset_size
18811 >= section->size)
18812 complaint (_("DW_FORM_loclistx pointing outside of "
18813 ".debug_loclists section [in module %s]"),
18814 objfile_name (objfile));
18815 const gdb_byte *info_ptr
18816 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18817
18818 if (cu->header.offset_size == 4)
18819 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18820 else
18821 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18822 }
18823
18824 /* Process the attributes that had to be skipped in the first round. These
18825 attributes are the ones that need str_offsets_base or addr_base attributes.
18826 They could not have been processed in the first round, because at the time
18827 the values of str_offsets_base or addr_base may not have been known. */
18828 static void
18829 read_attribute_reprocess (const struct die_reader_specs *reader,
18830 struct attribute *attr)
18831 {
18832 struct dwarf2_cu *cu = reader->cu;
18833 switch (attr->form)
18834 {
18835 case DW_FORM_addrx:
18836 case DW_FORM_GNU_addr_index:
18837 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18838 break;
18839 case DW_FORM_loclistx:
18840 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18841 break;
18842 case DW_FORM_strx:
18843 case DW_FORM_strx1:
18844 case DW_FORM_strx2:
18845 case DW_FORM_strx3:
18846 case DW_FORM_strx4:
18847 case DW_FORM_GNU_str_index:
18848 {
18849 unsigned int str_index = DW_UNSND (attr);
18850 if (reader->dwo_file != NULL)
18851 {
18852 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18853 DW_STRING_IS_CANONICAL (attr) = 0;
18854 }
18855 else
18856 {
18857 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18858 DW_STRING_IS_CANONICAL (attr) = 0;
18859 }
18860 break;
18861 }
18862 default:
18863 gdb_assert_not_reached (_("Unexpected DWARF form."));
18864 }
18865 }
18866
18867 /* Read an attribute value described by an attribute form. */
18868
18869 static const gdb_byte *
18870 read_attribute_value (const struct die_reader_specs *reader,
18871 struct attribute *attr, unsigned form,
18872 LONGEST implicit_const, const gdb_byte *info_ptr,
18873 bool *need_reprocess)
18874 {
18875 struct dwarf2_cu *cu = reader->cu;
18876 struct dwarf2_per_objfile *dwarf2_per_objfile
18877 = cu->per_cu->dwarf2_per_objfile;
18878 struct objfile *objfile = dwarf2_per_objfile->objfile;
18879 bfd *abfd = reader->abfd;
18880 struct comp_unit_head *cu_header = &cu->header;
18881 unsigned int bytes_read;
18882 struct dwarf_block *blk;
18883 *need_reprocess = false;
18884
18885 attr->form = (enum dwarf_form) form;
18886 switch (form)
18887 {
18888 case DW_FORM_ref_addr:
18889 if (cu->header.version == 2)
18890 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18891 &bytes_read);
18892 else
18893 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18894 &bytes_read);
18895 info_ptr += bytes_read;
18896 break;
18897 case DW_FORM_GNU_ref_alt:
18898 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18899 info_ptr += bytes_read;
18900 break;
18901 case DW_FORM_addr:
18902 {
18903 struct gdbarch *gdbarch = objfile->arch ();
18904 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18905 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18906 info_ptr += bytes_read;
18907 }
18908 break;
18909 case DW_FORM_block2:
18910 blk = dwarf_alloc_block (cu);
18911 blk->size = read_2_bytes (abfd, info_ptr);
18912 info_ptr += 2;
18913 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18914 info_ptr += blk->size;
18915 DW_BLOCK (attr) = blk;
18916 break;
18917 case DW_FORM_block4:
18918 blk = dwarf_alloc_block (cu);
18919 blk->size = read_4_bytes (abfd, info_ptr);
18920 info_ptr += 4;
18921 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18922 info_ptr += blk->size;
18923 DW_BLOCK (attr) = blk;
18924 break;
18925 case DW_FORM_data2:
18926 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18927 info_ptr += 2;
18928 break;
18929 case DW_FORM_data4:
18930 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18931 info_ptr += 4;
18932 break;
18933 case DW_FORM_data8:
18934 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18935 info_ptr += 8;
18936 break;
18937 case DW_FORM_data16:
18938 blk = dwarf_alloc_block (cu);
18939 blk->size = 16;
18940 blk->data = read_n_bytes (abfd, info_ptr, 16);
18941 info_ptr += 16;
18942 DW_BLOCK (attr) = blk;
18943 break;
18944 case DW_FORM_sec_offset:
18945 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18946 info_ptr += bytes_read;
18947 break;
18948 case DW_FORM_loclistx:
18949 {
18950 *need_reprocess = true;
18951 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18952 info_ptr += bytes_read;
18953 }
18954 break;
18955 case DW_FORM_string:
18956 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18957 DW_STRING_IS_CANONICAL (attr) = 0;
18958 info_ptr += bytes_read;
18959 break;
18960 case DW_FORM_strp:
18961 if (!cu->per_cu->is_dwz)
18962 {
18963 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18964 abfd, info_ptr, cu_header,
18965 &bytes_read);
18966 DW_STRING_IS_CANONICAL (attr) = 0;
18967 info_ptr += bytes_read;
18968 break;
18969 }
18970 /* FALLTHROUGH */
18971 case DW_FORM_line_strp:
18972 if (!cu->per_cu->is_dwz)
18973 {
18974 DW_STRING (attr)
18975 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
18976 &bytes_read);
18977 DW_STRING_IS_CANONICAL (attr) = 0;
18978 info_ptr += bytes_read;
18979 break;
18980 }
18981 /* FALLTHROUGH */
18982 case DW_FORM_GNU_strp_alt:
18983 {
18984 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18985 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18986 &bytes_read);
18987
18988 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
18989 DW_STRING_IS_CANONICAL (attr) = 0;
18990 info_ptr += bytes_read;
18991 }
18992 break;
18993 case DW_FORM_exprloc:
18994 case DW_FORM_block:
18995 blk = dwarf_alloc_block (cu);
18996 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18997 info_ptr += bytes_read;
18998 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18999 info_ptr += blk->size;
19000 DW_BLOCK (attr) = blk;
19001 break;
19002 case DW_FORM_block1:
19003 blk = dwarf_alloc_block (cu);
19004 blk->size = read_1_byte (abfd, info_ptr);
19005 info_ptr += 1;
19006 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19007 info_ptr += blk->size;
19008 DW_BLOCK (attr) = blk;
19009 break;
19010 case DW_FORM_data1:
19011 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19012 info_ptr += 1;
19013 break;
19014 case DW_FORM_flag:
19015 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19016 info_ptr += 1;
19017 break;
19018 case DW_FORM_flag_present:
19019 DW_UNSND (attr) = 1;
19020 break;
19021 case DW_FORM_sdata:
19022 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19023 info_ptr += bytes_read;
19024 break;
19025 case DW_FORM_udata:
19026 case DW_FORM_rnglistx:
19027 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19028 info_ptr += bytes_read;
19029 break;
19030 case DW_FORM_ref1:
19031 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19032 + read_1_byte (abfd, info_ptr));
19033 info_ptr += 1;
19034 break;
19035 case DW_FORM_ref2:
19036 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19037 + read_2_bytes (abfd, info_ptr));
19038 info_ptr += 2;
19039 break;
19040 case DW_FORM_ref4:
19041 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19042 + read_4_bytes (abfd, info_ptr));
19043 info_ptr += 4;
19044 break;
19045 case DW_FORM_ref8:
19046 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19047 + read_8_bytes (abfd, info_ptr));
19048 info_ptr += 8;
19049 break;
19050 case DW_FORM_ref_sig8:
19051 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19052 info_ptr += 8;
19053 break;
19054 case DW_FORM_ref_udata:
19055 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19056 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19057 info_ptr += bytes_read;
19058 break;
19059 case DW_FORM_indirect:
19060 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19061 info_ptr += bytes_read;
19062 if (form == DW_FORM_implicit_const)
19063 {
19064 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19065 info_ptr += bytes_read;
19066 }
19067 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19068 info_ptr, need_reprocess);
19069 break;
19070 case DW_FORM_implicit_const:
19071 DW_SND (attr) = implicit_const;
19072 break;
19073 case DW_FORM_addrx:
19074 case DW_FORM_GNU_addr_index:
19075 *need_reprocess = true;
19076 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19077 info_ptr += bytes_read;
19078 break;
19079 case DW_FORM_strx:
19080 case DW_FORM_strx1:
19081 case DW_FORM_strx2:
19082 case DW_FORM_strx3:
19083 case DW_FORM_strx4:
19084 case DW_FORM_GNU_str_index:
19085 {
19086 ULONGEST str_index;
19087 if (form == DW_FORM_strx1)
19088 {
19089 str_index = read_1_byte (abfd, info_ptr);
19090 info_ptr += 1;
19091 }
19092 else if (form == DW_FORM_strx2)
19093 {
19094 str_index = read_2_bytes (abfd, info_ptr);
19095 info_ptr += 2;
19096 }
19097 else if (form == DW_FORM_strx3)
19098 {
19099 str_index = read_3_bytes (abfd, info_ptr);
19100 info_ptr += 3;
19101 }
19102 else if (form == DW_FORM_strx4)
19103 {
19104 str_index = read_4_bytes (abfd, info_ptr);
19105 info_ptr += 4;
19106 }
19107 else
19108 {
19109 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19110 info_ptr += bytes_read;
19111 }
19112 *need_reprocess = true;
19113 DW_UNSND (attr) = str_index;
19114 }
19115 break;
19116 default:
19117 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19118 dwarf_form_name (form),
19119 bfd_get_filename (abfd));
19120 }
19121
19122 /* Super hack. */
19123 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19124 attr->form = DW_FORM_GNU_ref_alt;
19125
19126 /* We have seen instances where the compiler tried to emit a byte
19127 size attribute of -1 which ended up being encoded as an unsigned
19128 0xffffffff. Although 0xffffffff is technically a valid size value,
19129 an object of this size seems pretty unlikely so we can relatively
19130 safely treat these cases as if the size attribute was invalid and
19131 treat them as zero by default. */
19132 if (attr->name == DW_AT_byte_size
19133 && form == DW_FORM_data4
19134 && DW_UNSND (attr) >= 0xffffffff)
19135 {
19136 complaint
19137 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19138 hex_string (DW_UNSND (attr)));
19139 DW_UNSND (attr) = 0;
19140 }
19141
19142 return info_ptr;
19143 }
19144
19145 /* Read an attribute described by an abbreviated attribute. */
19146
19147 static const gdb_byte *
19148 read_attribute (const struct die_reader_specs *reader,
19149 struct attribute *attr, struct attr_abbrev *abbrev,
19150 const gdb_byte *info_ptr, bool *need_reprocess)
19151 {
19152 attr->name = abbrev->name;
19153 return read_attribute_value (reader, attr, abbrev->form,
19154 abbrev->implicit_const, info_ptr,
19155 need_reprocess);
19156 }
19157
19158 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19159
19160 static const char *
19161 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19162 LONGEST str_offset)
19163 {
19164 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
19165 str_offset, "DW_FORM_strp");
19166 }
19167
19168 /* Return pointer to string at .debug_str offset as read from BUF.
19169 BUF is assumed to be in a compilation unit described by CU_HEADER.
19170 Return *BYTES_READ_PTR count of bytes read from BUF. */
19171
19172 static const char *
19173 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19174 const gdb_byte *buf,
19175 const struct comp_unit_head *cu_header,
19176 unsigned int *bytes_read_ptr)
19177 {
19178 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19179
19180 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
19181 }
19182
19183 /* See read.h. */
19184
19185 const char *
19186 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19187 const struct comp_unit_head *cu_header,
19188 unsigned int *bytes_read_ptr)
19189 {
19190 bfd *abfd = objfile->obfd;
19191 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19192
19193 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19194 }
19195
19196 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19197 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19198 ADDR_SIZE is the size of addresses from the CU header. */
19199
19200 static CORE_ADDR
19201 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19202 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19203 int addr_size)
19204 {
19205 struct objfile *objfile = dwarf2_per_objfile->objfile;
19206 bfd *abfd = objfile->obfd;
19207 const gdb_byte *info_ptr;
19208 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19209
19210 dwarf2_per_objfile->addr.read (objfile);
19211 if (dwarf2_per_objfile->addr.buffer == NULL)
19212 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19213 objfile_name (objfile));
19214 if (addr_base_or_zero + addr_index * addr_size
19215 >= dwarf2_per_objfile->addr.size)
19216 error (_("DW_FORM_addr_index pointing outside of "
19217 ".debug_addr section [in module %s]"),
19218 objfile_name (objfile));
19219 info_ptr = (dwarf2_per_objfile->addr.buffer
19220 + addr_base_or_zero + addr_index * addr_size);
19221 if (addr_size == 4)
19222 return bfd_get_32 (abfd, info_ptr);
19223 else
19224 return bfd_get_64 (abfd, info_ptr);
19225 }
19226
19227 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19228
19229 static CORE_ADDR
19230 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19231 {
19232 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19233 cu->addr_base, cu->header.addr_size);
19234 }
19235
19236 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19237
19238 static CORE_ADDR
19239 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19240 unsigned int *bytes_read)
19241 {
19242 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19243 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19244
19245 return read_addr_index (cu, addr_index);
19246 }
19247
19248 /* See read.h. */
19249
19250 CORE_ADDR
19251 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
19252 {
19253 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19254 struct dwarf2_cu *cu = per_cu->cu;
19255 gdb::optional<ULONGEST> addr_base;
19256 int addr_size;
19257
19258 /* We need addr_base and addr_size.
19259 If we don't have PER_CU->cu, we have to get it.
19260 Nasty, but the alternative is storing the needed info in PER_CU,
19261 which at this point doesn't seem justified: it's not clear how frequently
19262 it would get used and it would increase the size of every PER_CU.
19263 Entry points like dwarf2_per_cu_addr_size do a similar thing
19264 so we're not in uncharted territory here.
19265 Alas we need to be a bit more complicated as addr_base is contained
19266 in the DIE.
19267
19268 We don't need to read the entire CU(/TU).
19269 We just need the header and top level die.
19270
19271 IWBN to use the aging mechanism to let us lazily later discard the CU.
19272 For now we skip this optimization. */
19273
19274 if (cu != NULL)
19275 {
19276 addr_base = cu->addr_base;
19277 addr_size = cu->header.addr_size;
19278 }
19279 else
19280 {
19281 cutu_reader reader (per_cu, NULL, 0, false);
19282 addr_base = reader.cu->addr_base;
19283 addr_size = reader.cu->header.addr_size;
19284 }
19285
19286 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19287 addr_size);
19288 }
19289
19290 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19291 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19292 DWO file. */
19293
19294 static const char *
19295 read_str_index (struct dwarf2_cu *cu,
19296 struct dwarf2_section_info *str_section,
19297 struct dwarf2_section_info *str_offsets_section,
19298 ULONGEST str_offsets_base, ULONGEST str_index)
19299 {
19300 struct dwarf2_per_objfile *dwarf2_per_objfile
19301 = cu->per_cu->dwarf2_per_objfile;
19302 struct objfile *objfile = dwarf2_per_objfile->objfile;
19303 const char *objf_name = objfile_name (objfile);
19304 bfd *abfd = objfile->obfd;
19305 const gdb_byte *info_ptr;
19306 ULONGEST str_offset;
19307 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19308
19309 str_section->read (objfile);
19310 str_offsets_section->read (objfile);
19311 if (str_section->buffer == NULL)
19312 error (_("%s used without %s section"
19313 " in CU at offset %s [in module %s]"),
19314 form_name, str_section->get_name (),
19315 sect_offset_str (cu->header.sect_off), objf_name);
19316 if (str_offsets_section->buffer == NULL)
19317 error (_("%s used without %s section"
19318 " in CU at offset %s [in module %s]"),
19319 form_name, str_section->get_name (),
19320 sect_offset_str (cu->header.sect_off), objf_name);
19321 info_ptr = (str_offsets_section->buffer
19322 + str_offsets_base
19323 + str_index * cu->header.offset_size);
19324 if (cu->header.offset_size == 4)
19325 str_offset = bfd_get_32 (abfd, info_ptr);
19326 else
19327 str_offset = bfd_get_64 (abfd, info_ptr);
19328 if (str_offset >= str_section->size)
19329 error (_("Offset from %s pointing outside of"
19330 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19331 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19332 return (const char *) (str_section->buffer + str_offset);
19333 }
19334
19335 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19336
19337 static const char *
19338 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19339 {
19340 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19341 ? reader->cu->header.addr_size : 0;
19342 return read_str_index (reader->cu,
19343 &reader->dwo_file->sections.str,
19344 &reader->dwo_file->sections.str_offsets,
19345 str_offsets_base, str_index);
19346 }
19347
19348 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19349
19350 static const char *
19351 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19352 {
19353 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19354 const char *objf_name = objfile_name (objfile);
19355 static const char form_name[] = "DW_FORM_GNU_str_index";
19356 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19357
19358 if (!cu->str_offsets_base.has_value ())
19359 error (_("%s used in Fission stub without %s"
19360 " in CU at offset 0x%lx [in module %s]"),
19361 form_name, str_offsets_attr_name,
19362 (long) cu->header.offset_size, objf_name);
19363
19364 return read_str_index (cu,
19365 &cu->per_cu->dwarf2_per_objfile->str,
19366 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19367 *cu->str_offsets_base, str_index);
19368 }
19369
19370 /* Return the length of an LEB128 number in BUF. */
19371
19372 static int
19373 leb128_size (const gdb_byte *buf)
19374 {
19375 const gdb_byte *begin = buf;
19376 gdb_byte byte;
19377
19378 while (1)
19379 {
19380 byte = *buf++;
19381 if ((byte & 128) == 0)
19382 return buf - begin;
19383 }
19384 }
19385
19386 static void
19387 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19388 {
19389 switch (lang)
19390 {
19391 case DW_LANG_C89:
19392 case DW_LANG_C99:
19393 case DW_LANG_C11:
19394 case DW_LANG_C:
19395 case DW_LANG_UPC:
19396 cu->language = language_c;
19397 break;
19398 case DW_LANG_Java:
19399 case DW_LANG_C_plus_plus:
19400 case DW_LANG_C_plus_plus_11:
19401 case DW_LANG_C_plus_plus_14:
19402 cu->language = language_cplus;
19403 break;
19404 case DW_LANG_D:
19405 cu->language = language_d;
19406 break;
19407 case DW_LANG_Fortran77:
19408 case DW_LANG_Fortran90:
19409 case DW_LANG_Fortran95:
19410 case DW_LANG_Fortran03:
19411 case DW_LANG_Fortran08:
19412 cu->language = language_fortran;
19413 break;
19414 case DW_LANG_Go:
19415 cu->language = language_go;
19416 break;
19417 case DW_LANG_Mips_Assembler:
19418 cu->language = language_asm;
19419 break;
19420 case DW_LANG_Ada83:
19421 case DW_LANG_Ada95:
19422 cu->language = language_ada;
19423 break;
19424 case DW_LANG_Modula2:
19425 cu->language = language_m2;
19426 break;
19427 case DW_LANG_Pascal83:
19428 cu->language = language_pascal;
19429 break;
19430 case DW_LANG_ObjC:
19431 cu->language = language_objc;
19432 break;
19433 case DW_LANG_Rust:
19434 case DW_LANG_Rust_old:
19435 cu->language = language_rust;
19436 break;
19437 case DW_LANG_Cobol74:
19438 case DW_LANG_Cobol85:
19439 default:
19440 cu->language = language_minimal;
19441 break;
19442 }
19443 cu->language_defn = language_def (cu->language);
19444 }
19445
19446 /* Return the named attribute or NULL if not there. */
19447
19448 static struct attribute *
19449 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19450 {
19451 for (;;)
19452 {
19453 unsigned int i;
19454 struct attribute *spec = NULL;
19455
19456 for (i = 0; i < die->num_attrs; ++i)
19457 {
19458 if (die->attrs[i].name == name)
19459 return &die->attrs[i];
19460 if (die->attrs[i].name == DW_AT_specification
19461 || die->attrs[i].name == DW_AT_abstract_origin)
19462 spec = &die->attrs[i];
19463 }
19464
19465 if (!spec)
19466 break;
19467
19468 die = follow_die_ref (die, spec, &cu);
19469 }
19470
19471 return NULL;
19472 }
19473
19474 /* Return the string associated with a string-typed attribute, or NULL if it
19475 is either not found or is of an incorrect type. */
19476
19477 static const char *
19478 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19479 {
19480 struct attribute *attr;
19481 const char *str = NULL;
19482
19483 attr = dwarf2_attr (die, name, cu);
19484
19485 if (attr != NULL)
19486 {
19487 str = attr->value_as_string ();
19488 if (str == nullptr)
19489 complaint (_("string type expected for attribute %s for "
19490 "DIE at %s in module %s"),
19491 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19492 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19493 }
19494
19495 return str;
19496 }
19497
19498 /* Return the dwo name or NULL if not present. If present, it is in either
19499 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19500 static const char *
19501 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19502 {
19503 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19504 if (dwo_name == nullptr)
19505 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19506 return dwo_name;
19507 }
19508
19509 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19510 and holds a non-zero value. This function should only be used for
19511 DW_FORM_flag or DW_FORM_flag_present attributes. */
19512
19513 static int
19514 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19515 {
19516 struct attribute *attr = dwarf2_attr (die, name, cu);
19517
19518 return (attr && DW_UNSND (attr));
19519 }
19520
19521 static int
19522 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19523 {
19524 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19525 which value is non-zero. However, we have to be careful with
19526 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19527 (via dwarf2_flag_true_p) follows this attribute. So we may
19528 end up accidently finding a declaration attribute that belongs
19529 to a different DIE referenced by the specification attribute,
19530 even though the given DIE does not have a declaration attribute. */
19531 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19532 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19533 }
19534
19535 /* Return the die giving the specification for DIE, if there is
19536 one. *SPEC_CU is the CU containing DIE on input, and the CU
19537 containing the return value on output. If there is no
19538 specification, but there is an abstract origin, that is
19539 returned. */
19540
19541 static struct die_info *
19542 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19543 {
19544 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19545 *spec_cu);
19546
19547 if (spec_attr == NULL)
19548 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19549
19550 if (spec_attr == NULL)
19551 return NULL;
19552 else
19553 return follow_die_ref (die, spec_attr, spec_cu);
19554 }
19555
19556 /* Stub for free_line_header to match void * callback types. */
19557
19558 static void
19559 free_line_header_voidp (void *arg)
19560 {
19561 struct line_header *lh = (struct line_header *) arg;
19562
19563 delete lh;
19564 }
19565
19566 /* A convenience function to find the proper .debug_line section for a CU. */
19567
19568 static struct dwarf2_section_info *
19569 get_debug_line_section (struct dwarf2_cu *cu)
19570 {
19571 struct dwarf2_section_info *section;
19572 struct dwarf2_per_objfile *dwarf2_per_objfile
19573 = cu->per_cu->dwarf2_per_objfile;
19574
19575 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19576 DWO file. */
19577 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19578 section = &cu->dwo_unit->dwo_file->sections.line;
19579 else if (cu->per_cu->is_dwz)
19580 {
19581 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19582
19583 section = &dwz->line;
19584 }
19585 else
19586 section = &dwarf2_per_objfile->line;
19587
19588 return section;
19589 }
19590
19591 /* Read the statement program header starting at OFFSET in
19592 .debug_line, or .debug_line.dwo. Return a pointer
19593 to a struct line_header, allocated using xmalloc.
19594 Returns NULL if there is a problem reading the header, e.g., if it
19595 has a version we don't understand.
19596
19597 NOTE: the strings in the include directory and file name tables of
19598 the returned object point into the dwarf line section buffer,
19599 and must not be freed. */
19600
19601 static line_header_up
19602 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19603 {
19604 struct dwarf2_section_info *section;
19605 struct dwarf2_per_objfile *dwarf2_per_objfile
19606 = cu->per_cu->dwarf2_per_objfile;
19607
19608 section = get_debug_line_section (cu);
19609 section->read (dwarf2_per_objfile->objfile);
19610 if (section->buffer == NULL)
19611 {
19612 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19613 complaint (_("missing .debug_line.dwo section"));
19614 else
19615 complaint (_("missing .debug_line section"));
19616 return 0;
19617 }
19618
19619 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19620 dwarf2_per_objfile, section,
19621 &cu->header);
19622 }
19623
19624 /* Subroutine of dwarf_decode_lines to simplify it.
19625 Return the file name of the psymtab for the given file_entry.
19626 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19627 If space for the result is malloc'd, *NAME_HOLDER will be set.
19628 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19629
19630 static const char *
19631 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19632 const dwarf2_psymtab *pst,
19633 const char *comp_dir,
19634 gdb::unique_xmalloc_ptr<char> *name_holder)
19635 {
19636 const char *include_name = fe.name;
19637 const char *include_name_to_compare = include_name;
19638 const char *pst_filename;
19639 int file_is_pst;
19640
19641 const char *dir_name = fe.include_dir (lh);
19642
19643 gdb::unique_xmalloc_ptr<char> hold_compare;
19644 if (!IS_ABSOLUTE_PATH (include_name)
19645 && (dir_name != NULL || comp_dir != NULL))
19646 {
19647 /* Avoid creating a duplicate psymtab for PST.
19648 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19649 Before we do the comparison, however, we need to account
19650 for DIR_NAME and COMP_DIR.
19651 First prepend dir_name (if non-NULL). If we still don't
19652 have an absolute path prepend comp_dir (if non-NULL).
19653 However, the directory we record in the include-file's
19654 psymtab does not contain COMP_DIR (to match the
19655 corresponding symtab(s)).
19656
19657 Example:
19658
19659 bash$ cd /tmp
19660 bash$ gcc -g ./hello.c
19661 include_name = "hello.c"
19662 dir_name = "."
19663 DW_AT_comp_dir = comp_dir = "/tmp"
19664 DW_AT_name = "./hello.c"
19665
19666 */
19667
19668 if (dir_name != NULL)
19669 {
19670 name_holder->reset (concat (dir_name, SLASH_STRING,
19671 include_name, (char *) NULL));
19672 include_name = name_holder->get ();
19673 include_name_to_compare = include_name;
19674 }
19675 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19676 {
19677 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19678 include_name, (char *) NULL));
19679 include_name_to_compare = hold_compare.get ();
19680 }
19681 }
19682
19683 pst_filename = pst->filename;
19684 gdb::unique_xmalloc_ptr<char> copied_name;
19685 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19686 {
19687 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19688 pst_filename, (char *) NULL));
19689 pst_filename = copied_name.get ();
19690 }
19691
19692 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19693
19694 if (file_is_pst)
19695 return NULL;
19696 return include_name;
19697 }
19698
19699 /* State machine to track the state of the line number program. */
19700
19701 class lnp_state_machine
19702 {
19703 public:
19704 /* Initialize a machine state for the start of a line number
19705 program. */
19706 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19707 bool record_lines_p);
19708
19709 file_entry *current_file ()
19710 {
19711 /* lh->file_names is 0-based, but the file name numbers in the
19712 statement program are 1-based. */
19713 return m_line_header->file_name_at (m_file);
19714 }
19715
19716 /* Record the line in the state machine. END_SEQUENCE is true if
19717 we're processing the end of a sequence. */
19718 void record_line (bool end_sequence);
19719
19720 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19721 nop-out rest of the lines in this sequence. */
19722 void check_line_address (struct dwarf2_cu *cu,
19723 const gdb_byte *line_ptr,
19724 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19725
19726 void handle_set_discriminator (unsigned int discriminator)
19727 {
19728 m_discriminator = discriminator;
19729 m_line_has_non_zero_discriminator |= discriminator != 0;
19730 }
19731
19732 /* Handle DW_LNE_set_address. */
19733 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19734 {
19735 m_op_index = 0;
19736 address += baseaddr;
19737 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19738 }
19739
19740 /* Handle DW_LNS_advance_pc. */
19741 void handle_advance_pc (CORE_ADDR adjust);
19742
19743 /* Handle a special opcode. */
19744 void handle_special_opcode (unsigned char op_code);
19745
19746 /* Handle DW_LNS_advance_line. */
19747 void handle_advance_line (int line_delta)
19748 {
19749 advance_line (line_delta);
19750 }
19751
19752 /* Handle DW_LNS_set_file. */
19753 void handle_set_file (file_name_index file);
19754
19755 /* Handle DW_LNS_negate_stmt. */
19756 void handle_negate_stmt ()
19757 {
19758 m_is_stmt = !m_is_stmt;
19759 }
19760
19761 /* Handle DW_LNS_const_add_pc. */
19762 void handle_const_add_pc ();
19763
19764 /* Handle DW_LNS_fixed_advance_pc. */
19765 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19766 {
19767 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19768 m_op_index = 0;
19769 }
19770
19771 /* Handle DW_LNS_copy. */
19772 void handle_copy ()
19773 {
19774 record_line (false);
19775 m_discriminator = 0;
19776 }
19777
19778 /* Handle DW_LNE_end_sequence. */
19779 void handle_end_sequence ()
19780 {
19781 m_currently_recording_lines = true;
19782 }
19783
19784 private:
19785 /* Advance the line by LINE_DELTA. */
19786 void advance_line (int line_delta)
19787 {
19788 m_line += line_delta;
19789
19790 if (line_delta != 0)
19791 m_line_has_non_zero_discriminator = m_discriminator != 0;
19792 }
19793
19794 struct dwarf2_cu *m_cu;
19795
19796 gdbarch *m_gdbarch;
19797
19798 /* True if we're recording lines.
19799 Otherwise we're building partial symtabs and are just interested in
19800 finding include files mentioned by the line number program. */
19801 bool m_record_lines_p;
19802
19803 /* The line number header. */
19804 line_header *m_line_header;
19805
19806 /* These are part of the standard DWARF line number state machine,
19807 and initialized according to the DWARF spec. */
19808
19809 unsigned char m_op_index = 0;
19810 /* The line table index of the current file. */
19811 file_name_index m_file = 1;
19812 unsigned int m_line = 1;
19813
19814 /* These are initialized in the constructor. */
19815
19816 CORE_ADDR m_address;
19817 bool m_is_stmt;
19818 unsigned int m_discriminator;
19819
19820 /* Additional bits of state we need to track. */
19821
19822 /* The last file that we called dwarf2_start_subfile for.
19823 This is only used for TLLs. */
19824 unsigned int m_last_file = 0;
19825 /* The last file a line number was recorded for. */
19826 struct subfile *m_last_subfile = NULL;
19827
19828 /* When true, record the lines we decode. */
19829 bool m_currently_recording_lines = false;
19830
19831 /* The last line number that was recorded, used to coalesce
19832 consecutive entries for the same line. This can happen, for
19833 example, when discriminators are present. PR 17276. */
19834 unsigned int m_last_line = 0;
19835 bool m_line_has_non_zero_discriminator = false;
19836 };
19837
19838 void
19839 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19840 {
19841 CORE_ADDR addr_adj = (((m_op_index + adjust)
19842 / m_line_header->maximum_ops_per_instruction)
19843 * m_line_header->minimum_instruction_length);
19844 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19845 m_op_index = ((m_op_index + adjust)
19846 % m_line_header->maximum_ops_per_instruction);
19847 }
19848
19849 void
19850 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19851 {
19852 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19853 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19854 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19855 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19856 / m_line_header->maximum_ops_per_instruction)
19857 * m_line_header->minimum_instruction_length);
19858 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19859 m_op_index = ((m_op_index + adj_opcode_d)
19860 % m_line_header->maximum_ops_per_instruction);
19861
19862 int line_delta = m_line_header->line_base + adj_opcode_r;
19863 advance_line (line_delta);
19864 record_line (false);
19865 m_discriminator = 0;
19866 }
19867
19868 void
19869 lnp_state_machine::handle_set_file (file_name_index file)
19870 {
19871 m_file = file;
19872
19873 const file_entry *fe = current_file ();
19874 if (fe == NULL)
19875 dwarf2_debug_line_missing_file_complaint ();
19876 else if (m_record_lines_p)
19877 {
19878 const char *dir = fe->include_dir (m_line_header);
19879
19880 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19881 m_line_has_non_zero_discriminator = m_discriminator != 0;
19882 dwarf2_start_subfile (m_cu, fe->name, dir);
19883 }
19884 }
19885
19886 void
19887 lnp_state_machine::handle_const_add_pc ()
19888 {
19889 CORE_ADDR adjust
19890 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19891
19892 CORE_ADDR addr_adj
19893 = (((m_op_index + adjust)
19894 / m_line_header->maximum_ops_per_instruction)
19895 * m_line_header->minimum_instruction_length);
19896
19897 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19898 m_op_index = ((m_op_index + adjust)
19899 % m_line_header->maximum_ops_per_instruction);
19900 }
19901
19902 /* Return non-zero if we should add LINE to the line number table.
19903 LINE is the line to add, LAST_LINE is the last line that was added,
19904 LAST_SUBFILE is the subfile for LAST_LINE.
19905 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19906 had a non-zero discriminator.
19907
19908 We have to be careful in the presence of discriminators.
19909 E.g., for this line:
19910
19911 for (i = 0; i < 100000; i++);
19912
19913 clang can emit four line number entries for that one line,
19914 each with a different discriminator.
19915 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19916
19917 However, we want gdb to coalesce all four entries into one.
19918 Otherwise the user could stepi into the middle of the line and
19919 gdb would get confused about whether the pc really was in the
19920 middle of the line.
19921
19922 Things are further complicated by the fact that two consecutive
19923 line number entries for the same line is a heuristic used by gcc
19924 to denote the end of the prologue. So we can't just discard duplicate
19925 entries, we have to be selective about it. The heuristic we use is
19926 that we only collapse consecutive entries for the same line if at least
19927 one of those entries has a non-zero discriminator. PR 17276.
19928
19929 Note: Addresses in the line number state machine can never go backwards
19930 within one sequence, thus this coalescing is ok. */
19931
19932 static int
19933 dwarf_record_line_p (struct dwarf2_cu *cu,
19934 unsigned int line, unsigned int last_line,
19935 int line_has_non_zero_discriminator,
19936 struct subfile *last_subfile)
19937 {
19938 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19939 return 1;
19940 if (line != last_line)
19941 return 1;
19942 /* Same line for the same file that we've seen already.
19943 As a last check, for pr 17276, only record the line if the line
19944 has never had a non-zero discriminator. */
19945 if (!line_has_non_zero_discriminator)
19946 return 1;
19947 return 0;
19948 }
19949
19950 /* Use the CU's builder to record line number LINE beginning at
19951 address ADDRESS in the line table of subfile SUBFILE. */
19952
19953 static void
19954 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19955 unsigned int line, CORE_ADDR address, bool is_stmt,
19956 struct dwarf2_cu *cu)
19957 {
19958 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19959
19960 if (dwarf_line_debug)
19961 {
19962 fprintf_unfiltered (gdb_stdlog,
19963 "Recording line %u, file %s, address %s\n",
19964 line, lbasename (subfile->name),
19965 paddress (gdbarch, address));
19966 }
19967
19968 if (cu != nullptr)
19969 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
19970 }
19971
19972 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19973 Mark the end of a set of line number records.
19974 The arguments are the same as for dwarf_record_line_1.
19975 If SUBFILE is NULL the request is ignored. */
19976
19977 static void
19978 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19979 CORE_ADDR address, struct dwarf2_cu *cu)
19980 {
19981 if (subfile == NULL)
19982 return;
19983
19984 if (dwarf_line_debug)
19985 {
19986 fprintf_unfiltered (gdb_stdlog,
19987 "Finishing current line, file %s, address %s\n",
19988 lbasename (subfile->name),
19989 paddress (gdbarch, address));
19990 }
19991
19992 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
19993 }
19994
19995 void
19996 lnp_state_machine::record_line (bool end_sequence)
19997 {
19998 if (dwarf_line_debug)
19999 {
20000 fprintf_unfiltered (gdb_stdlog,
20001 "Processing actual line %u: file %u,"
20002 " address %s, is_stmt %u, discrim %u%s\n",
20003 m_line, m_file,
20004 paddress (m_gdbarch, m_address),
20005 m_is_stmt, m_discriminator,
20006 (end_sequence ? "\t(end sequence)" : ""));
20007 }
20008
20009 file_entry *fe = current_file ();
20010
20011 if (fe == NULL)
20012 dwarf2_debug_line_missing_file_complaint ();
20013 /* For now we ignore lines not starting on an instruction boundary.
20014 But not when processing end_sequence for compatibility with the
20015 previous version of the code. */
20016 else if (m_op_index == 0 || end_sequence)
20017 {
20018 fe->included_p = 1;
20019 if (m_record_lines_p)
20020 {
20021 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20022 || end_sequence)
20023 {
20024 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20025 m_currently_recording_lines ? m_cu : nullptr);
20026 }
20027
20028 if (!end_sequence)
20029 {
20030 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20031
20032 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20033 m_line_has_non_zero_discriminator,
20034 m_last_subfile))
20035 {
20036 buildsym_compunit *builder = m_cu->get_builder ();
20037 dwarf_record_line_1 (m_gdbarch,
20038 builder->get_current_subfile (),
20039 m_line, m_address, is_stmt,
20040 m_currently_recording_lines ? m_cu : nullptr);
20041 }
20042 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20043 m_last_line = m_line;
20044 }
20045 }
20046 }
20047 }
20048
20049 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20050 line_header *lh, bool record_lines_p)
20051 {
20052 m_cu = cu;
20053 m_gdbarch = arch;
20054 m_record_lines_p = record_lines_p;
20055 m_line_header = lh;
20056
20057 m_currently_recording_lines = true;
20058
20059 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20060 was a line entry for it so that the backend has a chance to adjust it
20061 and also record it in case it needs it. This is currently used by MIPS
20062 code, cf. `mips_adjust_dwarf2_line'. */
20063 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20064 m_is_stmt = lh->default_is_stmt;
20065 m_discriminator = 0;
20066 }
20067
20068 void
20069 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20070 const gdb_byte *line_ptr,
20071 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20072 {
20073 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20074 the pc range of the CU. However, we restrict the test to only ADDRESS
20075 values of zero to preserve GDB's previous behaviour which is to handle
20076 the specific case of a function being GC'd by the linker. */
20077
20078 if (address == 0 && address < unrelocated_lowpc)
20079 {
20080 /* This line table is for a function which has been
20081 GCd by the linker. Ignore it. PR gdb/12528 */
20082
20083 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20084 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20085
20086 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20087 line_offset, objfile_name (objfile));
20088 m_currently_recording_lines = false;
20089 /* Note: m_currently_recording_lines is left as false until we see
20090 DW_LNE_end_sequence. */
20091 }
20092 }
20093
20094 /* Subroutine of dwarf_decode_lines to simplify it.
20095 Process the line number information in LH.
20096 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20097 program in order to set included_p for every referenced header. */
20098
20099 static void
20100 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20101 const int decode_for_pst_p, CORE_ADDR lowpc)
20102 {
20103 const gdb_byte *line_ptr, *extended_end;
20104 const gdb_byte *line_end;
20105 unsigned int bytes_read, extended_len;
20106 unsigned char op_code, extended_op;
20107 CORE_ADDR baseaddr;
20108 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20109 bfd *abfd = objfile->obfd;
20110 struct gdbarch *gdbarch = objfile->arch ();
20111 /* True if we're recording line info (as opposed to building partial
20112 symtabs and just interested in finding include files mentioned by
20113 the line number program). */
20114 bool record_lines_p = !decode_for_pst_p;
20115
20116 baseaddr = objfile->text_section_offset ();
20117
20118 line_ptr = lh->statement_program_start;
20119 line_end = lh->statement_program_end;
20120
20121 /* Read the statement sequences until there's nothing left. */
20122 while (line_ptr < line_end)
20123 {
20124 /* The DWARF line number program state machine. Reset the state
20125 machine at the start of each sequence. */
20126 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20127 bool end_sequence = false;
20128
20129 if (record_lines_p)
20130 {
20131 /* Start a subfile for the current file of the state
20132 machine. */
20133 const file_entry *fe = state_machine.current_file ();
20134
20135 if (fe != NULL)
20136 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20137 }
20138
20139 /* Decode the table. */
20140 while (line_ptr < line_end && !end_sequence)
20141 {
20142 op_code = read_1_byte (abfd, line_ptr);
20143 line_ptr += 1;
20144
20145 if (op_code >= lh->opcode_base)
20146 {
20147 /* Special opcode. */
20148 state_machine.handle_special_opcode (op_code);
20149 }
20150 else switch (op_code)
20151 {
20152 case DW_LNS_extended_op:
20153 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20154 &bytes_read);
20155 line_ptr += bytes_read;
20156 extended_end = line_ptr + extended_len;
20157 extended_op = read_1_byte (abfd, line_ptr);
20158 line_ptr += 1;
20159 switch (extended_op)
20160 {
20161 case DW_LNE_end_sequence:
20162 state_machine.handle_end_sequence ();
20163 end_sequence = true;
20164 break;
20165 case DW_LNE_set_address:
20166 {
20167 CORE_ADDR address
20168 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20169 line_ptr += bytes_read;
20170
20171 state_machine.check_line_address (cu, line_ptr,
20172 lowpc - baseaddr, address);
20173 state_machine.handle_set_address (baseaddr, address);
20174 }
20175 break;
20176 case DW_LNE_define_file:
20177 {
20178 const char *cur_file;
20179 unsigned int mod_time, length;
20180 dir_index dindex;
20181
20182 cur_file = read_direct_string (abfd, line_ptr,
20183 &bytes_read);
20184 line_ptr += bytes_read;
20185 dindex = (dir_index)
20186 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20187 line_ptr += bytes_read;
20188 mod_time =
20189 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20190 line_ptr += bytes_read;
20191 length =
20192 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20193 line_ptr += bytes_read;
20194 lh->add_file_name (cur_file, dindex, mod_time, length);
20195 }
20196 break;
20197 case DW_LNE_set_discriminator:
20198 {
20199 /* The discriminator is not interesting to the
20200 debugger; just ignore it. We still need to
20201 check its value though:
20202 if there are consecutive entries for the same
20203 (non-prologue) line we want to coalesce them.
20204 PR 17276. */
20205 unsigned int discr
20206 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20207 line_ptr += bytes_read;
20208
20209 state_machine.handle_set_discriminator (discr);
20210 }
20211 break;
20212 default:
20213 complaint (_("mangled .debug_line section"));
20214 return;
20215 }
20216 /* Make sure that we parsed the extended op correctly. If e.g.
20217 we expected a different address size than the producer used,
20218 we may have read the wrong number of bytes. */
20219 if (line_ptr != extended_end)
20220 {
20221 complaint (_("mangled .debug_line section"));
20222 return;
20223 }
20224 break;
20225 case DW_LNS_copy:
20226 state_machine.handle_copy ();
20227 break;
20228 case DW_LNS_advance_pc:
20229 {
20230 CORE_ADDR adjust
20231 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20232 line_ptr += bytes_read;
20233
20234 state_machine.handle_advance_pc (adjust);
20235 }
20236 break;
20237 case DW_LNS_advance_line:
20238 {
20239 int line_delta
20240 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20241 line_ptr += bytes_read;
20242
20243 state_machine.handle_advance_line (line_delta);
20244 }
20245 break;
20246 case DW_LNS_set_file:
20247 {
20248 file_name_index file
20249 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20250 &bytes_read);
20251 line_ptr += bytes_read;
20252
20253 state_machine.handle_set_file (file);
20254 }
20255 break;
20256 case DW_LNS_set_column:
20257 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20258 line_ptr += bytes_read;
20259 break;
20260 case DW_LNS_negate_stmt:
20261 state_machine.handle_negate_stmt ();
20262 break;
20263 case DW_LNS_set_basic_block:
20264 break;
20265 /* Add to the address register of the state machine the
20266 address increment value corresponding to special opcode
20267 255. I.e., this value is scaled by the minimum
20268 instruction length since special opcode 255 would have
20269 scaled the increment. */
20270 case DW_LNS_const_add_pc:
20271 state_machine.handle_const_add_pc ();
20272 break;
20273 case DW_LNS_fixed_advance_pc:
20274 {
20275 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20276 line_ptr += 2;
20277
20278 state_machine.handle_fixed_advance_pc (addr_adj);
20279 }
20280 break;
20281 default:
20282 {
20283 /* Unknown standard opcode, ignore it. */
20284 int i;
20285
20286 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20287 {
20288 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20289 line_ptr += bytes_read;
20290 }
20291 }
20292 }
20293 }
20294
20295 if (!end_sequence)
20296 dwarf2_debug_line_missing_end_sequence_complaint ();
20297
20298 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20299 in which case we still finish recording the last line). */
20300 state_machine.record_line (true);
20301 }
20302 }
20303
20304 /* Decode the Line Number Program (LNP) for the given line_header
20305 structure and CU. The actual information extracted and the type
20306 of structures created from the LNP depends on the value of PST.
20307
20308 1. If PST is NULL, then this procedure uses the data from the program
20309 to create all necessary symbol tables, and their linetables.
20310
20311 2. If PST is not NULL, this procedure reads the program to determine
20312 the list of files included by the unit represented by PST, and
20313 builds all the associated partial symbol tables.
20314
20315 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20316 It is used for relative paths in the line table.
20317 NOTE: When processing partial symtabs (pst != NULL),
20318 comp_dir == pst->dirname.
20319
20320 NOTE: It is important that psymtabs have the same file name (via strcmp)
20321 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20322 symtab we don't use it in the name of the psymtabs we create.
20323 E.g. expand_line_sal requires this when finding psymtabs to expand.
20324 A good testcase for this is mb-inline.exp.
20325
20326 LOWPC is the lowest address in CU (or 0 if not known).
20327
20328 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20329 for its PC<->lines mapping information. Otherwise only the filename
20330 table is read in. */
20331
20332 static void
20333 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20334 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20335 CORE_ADDR lowpc, int decode_mapping)
20336 {
20337 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20338 const int decode_for_pst_p = (pst != NULL);
20339
20340 if (decode_mapping)
20341 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20342
20343 if (decode_for_pst_p)
20344 {
20345 /* Now that we're done scanning the Line Header Program, we can
20346 create the psymtab of each included file. */
20347 for (auto &file_entry : lh->file_names ())
20348 if (file_entry.included_p == 1)
20349 {
20350 gdb::unique_xmalloc_ptr<char> name_holder;
20351 const char *include_name =
20352 psymtab_include_file_name (lh, file_entry, pst,
20353 comp_dir, &name_holder);
20354 if (include_name != NULL)
20355 dwarf2_create_include_psymtab (include_name, pst, objfile);
20356 }
20357 }
20358 else
20359 {
20360 /* Make sure a symtab is created for every file, even files
20361 which contain only variables (i.e. no code with associated
20362 line numbers). */
20363 buildsym_compunit *builder = cu->get_builder ();
20364 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20365
20366 for (auto &fe : lh->file_names ())
20367 {
20368 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20369 if (builder->get_current_subfile ()->symtab == NULL)
20370 {
20371 builder->get_current_subfile ()->symtab
20372 = allocate_symtab (cust,
20373 builder->get_current_subfile ()->name);
20374 }
20375 fe.symtab = builder->get_current_subfile ()->symtab;
20376 }
20377 }
20378 }
20379
20380 /* Start a subfile for DWARF. FILENAME is the name of the file and
20381 DIRNAME the name of the source directory which contains FILENAME
20382 or NULL if not known.
20383 This routine tries to keep line numbers from identical absolute and
20384 relative file names in a common subfile.
20385
20386 Using the `list' example from the GDB testsuite, which resides in
20387 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20388 of /srcdir/list0.c yields the following debugging information for list0.c:
20389
20390 DW_AT_name: /srcdir/list0.c
20391 DW_AT_comp_dir: /compdir
20392 files.files[0].name: list0.h
20393 files.files[0].dir: /srcdir
20394 files.files[1].name: list0.c
20395 files.files[1].dir: /srcdir
20396
20397 The line number information for list0.c has to end up in a single
20398 subfile, so that `break /srcdir/list0.c:1' works as expected.
20399 start_subfile will ensure that this happens provided that we pass the
20400 concatenation of files.files[1].dir and files.files[1].name as the
20401 subfile's name. */
20402
20403 static void
20404 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20405 const char *dirname)
20406 {
20407 gdb::unique_xmalloc_ptr<char> copy;
20408
20409 /* In order not to lose the line information directory,
20410 we concatenate it to the filename when it makes sense.
20411 Note that the Dwarf3 standard says (speaking of filenames in line
20412 information): ``The directory index is ignored for file names
20413 that represent full path names''. Thus ignoring dirname in the
20414 `else' branch below isn't an issue. */
20415
20416 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20417 {
20418 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20419 filename = copy.get ();
20420 }
20421
20422 cu->get_builder ()->start_subfile (filename);
20423 }
20424
20425 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20426 buildsym_compunit constructor. */
20427
20428 struct compunit_symtab *
20429 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20430 CORE_ADDR low_pc)
20431 {
20432 gdb_assert (m_builder == nullptr);
20433
20434 m_builder.reset (new struct buildsym_compunit
20435 (per_cu->dwarf2_per_objfile->objfile,
20436 name, comp_dir, language, low_pc));
20437
20438 list_in_scope = get_builder ()->get_file_symbols ();
20439
20440 get_builder ()->record_debugformat ("DWARF 2");
20441 get_builder ()->record_producer (producer);
20442
20443 processing_has_namespace_info = false;
20444
20445 return get_builder ()->get_compunit_symtab ();
20446 }
20447
20448 static void
20449 var_decode_location (struct attribute *attr, struct symbol *sym,
20450 struct dwarf2_cu *cu)
20451 {
20452 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20453 struct comp_unit_head *cu_header = &cu->header;
20454
20455 /* NOTE drow/2003-01-30: There used to be a comment and some special
20456 code here to turn a symbol with DW_AT_external and a
20457 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20458 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20459 with some versions of binutils) where shared libraries could have
20460 relocations against symbols in their debug information - the
20461 minimal symbol would have the right address, but the debug info
20462 would not. It's no longer necessary, because we will explicitly
20463 apply relocations when we read in the debug information now. */
20464
20465 /* A DW_AT_location attribute with no contents indicates that a
20466 variable has been optimized away. */
20467 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20468 {
20469 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20470 return;
20471 }
20472
20473 /* Handle one degenerate form of location expression specially, to
20474 preserve GDB's previous behavior when section offsets are
20475 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20476 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20477
20478 if (attr->form_is_block ()
20479 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20480 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20481 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20482 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20483 && (DW_BLOCK (attr)->size
20484 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20485 {
20486 unsigned int dummy;
20487
20488 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20489 SET_SYMBOL_VALUE_ADDRESS
20490 (sym, cu->header.read_address (objfile->obfd,
20491 DW_BLOCK (attr)->data + 1,
20492 &dummy));
20493 else
20494 SET_SYMBOL_VALUE_ADDRESS
20495 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20496 &dummy));
20497 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20498 fixup_symbol_section (sym, objfile);
20499 SET_SYMBOL_VALUE_ADDRESS
20500 (sym,
20501 SYMBOL_VALUE_ADDRESS (sym)
20502 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20503 return;
20504 }
20505
20506 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20507 expression evaluator, and use LOC_COMPUTED only when necessary
20508 (i.e. when the value of a register or memory location is
20509 referenced, or a thread-local block, etc.). Then again, it might
20510 not be worthwhile. I'm assuming that it isn't unless performance
20511 or memory numbers show me otherwise. */
20512
20513 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20514
20515 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20516 cu->has_loclist = true;
20517 }
20518
20519 /* Given a pointer to a DWARF information entry, figure out if we need
20520 to make a symbol table entry for it, and if so, create a new entry
20521 and return a pointer to it.
20522 If TYPE is NULL, determine symbol type from the die, otherwise
20523 used the passed type.
20524 If SPACE is not NULL, use it to hold the new symbol. If it is
20525 NULL, allocate a new symbol on the objfile's obstack. */
20526
20527 static struct symbol *
20528 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20529 struct symbol *space)
20530 {
20531 struct dwarf2_per_objfile *dwarf2_per_objfile
20532 = cu->per_cu->dwarf2_per_objfile;
20533 struct objfile *objfile = dwarf2_per_objfile->objfile;
20534 struct gdbarch *gdbarch = objfile->arch ();
20535 struct symbol *sym = NULL;
20536 const char *name;
20537 struct attribute *attr = NULL;
20538 struct attribute *attr2 = NULL;
20539 CORE_ADDR baseaddr;
20540 struct pending **list_to_add = NULL;
20541
20542 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20543
20544 baseaddr = objfile->text_section_offset ();
20545
20546 name = dwarf2_name (die, cu);
20547 if (name)
20548 {
20549 const char *linkagename;
20550 int suppress_add = 0;
20551
20552 if (space)
20553 sym = space;
20554 else
20555 sym = allocate_symbol (objfile);
20556 OBJSTAT (objfile, n_syms++);
20557
20558 /* Cache this symbol's name and the name's demangled form (if any). */
20559 sym->set_language (cu->language, &objfile->objfile_obstack);
20560 linkagename = dwarf2_physname (name, die, cu);
20561 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20562
20563 /* Fortran does not have mangling standard and the mangling does differ
20564 between gfortran, iFort etc. */
20565 if (cu->language == language_fortran
20566 && symbol_get_demangled_name (sym) == NULL)
20567 sym->set_demangled_name (dwarf2_full_name (name, die, cu), NULL);
20568
20569 /* Default assumptions.
20570 Use the passed type or decode it from the die. */
20571 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20572 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20573 if (type != NULL)
20574 SYMBOL_TYPE (sym) = type;
20575 else
20576 SYMBOL_TYPE (sym) = die_type (die, cu);
20577 attr = dwarf2_attr (die,
20578 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20579 cu);
20580 if (attr != nullptr)
20581 {
20582 SYMBOL_LINE (sym) = DW_UNSND (attr);
20583 }
20584
20585 attr = dwarf2_attr (die,
20586 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20587 cu);
20588 if (attr != nullptr)
20589 {
20590 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20591 struct file_entry *fe;
20592
20593 if (cu->line_header != NULL)
20594 fe = cu->line_header->file_name_at (file_index);
20595 else
20596 fe = NULL;
20597
20598 if (fe == NULL)
20599 complaint (_("file index out of range"));
20600 else
20601 symbol_set_symtab (sym, fe->symtab);
20602 }
20603
20604 switch (die->tag)
20605 {
20606 case DW_TAG_label:
20607 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20608 if (attr != nullptr)
20609 {
20610 CORE_ADDR addr;
20611
20612 addr = attr->value_as_address ();
20613 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20614 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20615 }
20616 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20617 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20618 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20619 add_symbol_to_list (sym, cu->list_in_scope);
20620 break;
20621 case DW_TAG_subprogram:
20622 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20623 finish_block. */
20624 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20625 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20626 if ((attr2 && (DW_UNSND (attr2) != 0))
20627 || cu->language == language_ada
20628 || cu->language == language_fortran)
20629 {
20630 /* Subprograms marked external are stored as a global symbol.
20631 Ada and Fortran subprograms, whether marked external or
20632 not, are always stored as a global symbol, because we want
20633 to be able to access them globally. For instance, we want
20634 to be able to break on a nested subprogram without having
20635 to specify the context. */
20636 list_to_add = cu->get_builder ()->get_global_symbols ();
20637 }
20638 else
20639 {
20640 list_to_add = cu->list_in_scope;
20641 }
20642 break;
20643 case DW_TAG_inlined_subroutine:
20644 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20645 finish_block. */
20646 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20647 SYMBOL_INLINED (sym) = 1;
20648 list_to_add = cu->list_in_scope;
20649 break;
20650 case DW_TAG_template_value_param:
20651 suppress_add = 1;
20652 /* Fall through. */
20653 case DW_TAG_constant:
20654 case DW_TAG_variable:
20655 case DW_TAG_member:
20656 /* Compilation with minimal debug info may result in
20657 variables with missing type entries. Change the
20658 misleading `void' type to something sensible. */
20659 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20660 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20661
20662 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20663 /* In the case of DW_TAG_member, we should only be called for
20664 static const members. */
20665 if (die->tag == DW_TAG_member)
20666 {
20667 /* dwarf2_add_field uses die_is_declaration,
20668 so we do the same. */
20669 gdb_assert (die_is_declaration (die, cu));
20670 gdb_assert (attr);
20671 }
20672 if (attr != nullptr)
20673 {
20674 dwarf2_const_value (attr, sym, cu);
20675 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20676 if (!suppress_add)
20677 {
20678 if (attr2 && (DW_UNSND (attr2) != 0))
20679 list_to_add = cu->get_builder ()->get_global_symbols ();
20680 else
20681 list_to_add = cu->list_in_scope;
20682 }
20683 break;
20684 }
20685 attr = dwarf2_attr (die, DW_AT_location, cu);
20686 if (attr != nullptr)
20687 {
20688 var_decode_location (attr, sym, cu);
20689 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20690
20691 /* Fortran explicitly imports any global symbols to the local
20692 scope by DW_TAG_common_block. */
20693 if (cu->language == language_fortran && die->parent
20694 && die->parent->tag == DW_TAG_common_block)
20695 attr2 = NULL;
20696
20697 if (SYMBOL_CLASS (sym) == LOC_STATIC
20698 && SYMBOL_VALUE_ADDRESS (sym) == 0
20699 && !dwarf2_per_objfile->has_section_at_zero)
20700 {
20701 /* When a static variable is eliminated by the linker,
20702 the corresponding debug information is not stripped
20703 out, but the variable address is set to null;
20704 do not add such variables into symbol table. */
20705 }
20706 else if (attr2 && (DW_UNSND (attr2) != 0))
20707 {
20708 if (SYMBOL_CLASS (sym) == LOC_STATIC
20709 && (objfile->flags & OBJF_MAINLINE) == 0
20710 && dwarf2_per_objfile->can_copy)
20711 {
20712 /* A global static variable might be subject to
20713 copy relocation. We first check for a local
20714 minsym, though, because maybe the symbol was
20715 marked hidden, in which case this would not
20716 apply. */
20717 bound_minimal_symbol found
20718 = (lookup_minimal_symbol_linkage
20719 (sym->linkage_name (), objfile));
20720 if (found.minsym != nullptr)
20721 sym->maybe_copied = 1;
20722 }
20723
20724 /* A variable with DW_AT_external is never static,
20725 but it may be block-scoped. */
20726 list_to_add
20727 = ((cu->list_in_scope
20728 == cu->get_builder ()->get_file_symbols ())
20729 ? cu->get_builder ()->get_global_symbols ()
20730 : cu->list_in_scope);
20731 }
20732 else
20733 list_to_add = cu->list_in_scope;
20734 }
20735 else
20736 {
20737 /* We do not know the address of this symbol.
20738 If it is an external symbol and we have type information
20739 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20740 The address of the variable will then be determined from
20741 the minimal symbol table whenever the variable is
20742 referenced. */
20743 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20744
20745 /* Fortran explicitly imports any global symbols to the local
20746 scope by DW_TAG_common_block. */
20747 if (cu->language == language_fortran && die->parent
20748 && die->parent->tag == DW_TAG_common_block)
20749 {
20750 /* SYMBOL_CLASS doesn't matter here because
20751 read_common_block is going to reset it. */
20752 if (!suppress_add)
20753 list_to_add = cu->list_in_scope;
20754 }
20755 else if (attr2 && (DW_UNSND (attr2) != 0)
20756 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20757 {
20758 /* A variable with DW_AT_external is never static, but it
20759 may be block-scoped. */
20760 list_to_add
20761 = ((cu->list_in_scope
20762 == cu->get_builder ()->get_file_symbols ())
20763 ? cu->get_builder ()->get_global_symbols ()
20764 : cu->list_in_scope);
20765
20766 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20767 }
20768 else if (!die_is_declaration (die, cu))
20769 {
20770 /* Use the default LOC_OPTIMIZED_OUT class. */
20771 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20772 if (!suppress_add)
20773 list_to_add = cu->list_in_scope;
20774 }
20775 }
20776 break;
20777 case DW_TAG_formal_parameter:
20778 {
20779 /* If we are inside a function, mark this as an argument. If
20780 not, we might be looking at an argument to an inlined function
20781 when we do not have enough information to show inlined frames;
20782 pretend it's a local variable in that case so that the user can
20783 still see it. */
20784 struct context_stack *curr
20785 = cu->get_builder ()->get_current_context_stack ();
20786 if (curr != nullptr && curr->name != nullptr)
20787 SYMBOL_IS_ARGUMENT (sym) = 1;
20788 attr = dwarf2_attr (die, DW_AT_location, cu);
20789 if (attr != nullptr)
20790 {
20791 var_decode_location (attr, sym, cu);
20792 }
20793 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20794 if (attr != nullptr)
20795 {
20796 dwarf2_const_value (attr, sym, cu);
20797 }
20798
20799 list_to_add = cu->list_in_scope;
20800 }
20801 break;
20802 case DW_TAG_unspecified_parameters:
20803 /* From varargs functions; gdb doesn't seem to have any
20804 interest in this information, so just ignore it for now.
20805 (FIXME?) */
20806 break;
20807 case DW_TAG_template_type_param:
20808 suppress_add = 1;
20809 /* Fall through. */
20810 case DW_TAG_class_type:
20811 case DW_TAG_interface_type:
20812 case DW_TAG_structure_type:
20813 case DW_TAG_union_type:
20814 case DW_TAG_set_type:
20815 case DW_TAG_enumeration_type:
20816 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20817 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20818
20819 {
20820 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20821 really ever be static objects: otherwise, if you try
20822 to, say, break of a class's method and you're in a file
20823 which doesn't mention that class, it won't work unless
20824 the check for all static symbols in lookup_symbol_aux
20825 saves you. See the OtherFileClass tests in
20826 gdb.c++/namespace.exp. */
20827
20828 if (!suppress_add)
20829 {
20830 buildsym_compunit *builder = cu->get_builder ();
20831 list_to_add
20832 = (cu->list_in_scope == builder->get_file_symbols ()
20833 && cu->language == language_cplus
20834 ? builder->get_global_symbols ()
20835 : cu->list_in_scope);
20836
20837 /* The semantics of C++ state that "struct foo {
20838 ... }" also defines a typedef for "foo". */
20839 if (cu->language == language_cplus
20840 || cu->language == language_ada
20841 || cu->language == language_d
20842 || cu->language == language_rust)
20843 {
20844 /* The symbol's name is already allocated along
20845 with this objfile, so we don't need to
20846 duplicate it for the type. */
20847 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20848 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20849 }
20850 }
20851 }
20852 break;
20853 case DW_TAG_typedef:
20854 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20855 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20856 list_to_add = cu->list_in_scope;
20857 break;
20858 case DW_TAG_base_type:
20859 case DW_TAG_subrange_type:
20860 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20861 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20862 list_to_add = cu->list_in_scope;
20863 break;
20864 case DW_TAG_enumerator:
20865 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20866 if (attr != nullptr)
20867 {
20868 dwarf2_const_value (attr, sym, cu);
20869 }
20870 {
20871 /* NOTE: carlton/2003-11-10: See comment above in the
20872 DW_TAG_class_type, etc. block. */
20873
20874 list_to_add
20875 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20876 && cu->language == language_cplus
20877 ? cu->get_builder ()->get_global_symbols ()
20878 : cu->list_in_scope);
20879 }
20880 break;
20881 case DW_TAG_imported_declaration:
20882 case DW_TAG_namespace:
20883 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20884 list_to_add = cu->get_builder ()->get_global_symbols ();
20885 break;
20886 case DW_TAG_module:
20887 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20888 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20889 list_to_add = cu->get_builder ()->get_global_symbols ();
20890 break;
20891 case DW_TAG_common_block:
20892 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20893 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20894 add_symbol_to_list (sym, cu->list_in_scope);
20895 break;
20896 default:
20897 /* Not a tag we recognize. Hopefully we aren't processing
20898 trash data, but since we must specifically ignore things
20899 we don't recognize, there is nothing else we should do at
20900 this point. */
20901 complaint (_("unsupported tag: '%s'"),
20902 dwarf_tag_name (die->tag));
20903 break;
20904 }
20905
20906 if (suppress_add)
20907 {
20908 sym->hash_next = objfile->template_symbols;
20909 objfile->template_symbols = sym;
20910 list_to_add = NULL;
20911 }
20912
20913 if (list_to_add != NULL)
20914 add_symbol_to_list (sym, list_to_add);
20915
20916 /* For the benefit of old versions of GCC, check for anonymous
20917 namespaces based on the demangled name. */
20918 if (!cu->processing_has_namespace_info
20919 && cu->language == language_cplus)
20920 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20921 }
20922 return (sym);
20923 }
20924
20925 /* Given an attr with a DW_FORM_dataN value in host byte order,
20926 zero-extend it as appropriate for the symbol's type. The DWARF
20927 standard (v4) is not entirely clear about the meaning of using
20928 DW_FORM_dataN for a constant with a signed type, where the type is
20929 wider than the data. The conclusion of a discussion on the DWARF
20930 list was that this is unspecified. We choose to always zero-extend
20931 because that is the interpretation long in use by GCC. */
20932
20933 static gdb_byte *
20934 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20935 struct dwarf2_cu *cu, LONGEST *value, int bits)
20936 {
20937 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20938 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20939 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20940 LONGEST l = DW_UNSND (attr);
20941
20942 if (bits < sizeof (*value) * 8)
20943 {
20944 l &= ((LONGEST) 1 << bits) - 1;
20945 *value = l;
20946 }
20947 else if (bits == sizeof (*value) * 8)
20948 *value = l;
20949 else
20950 {
20951 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20952 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20953 return bytes;
20954 }
20955
20956 return NULL;
20957 }
20958
20959 /* Read a constant value from an attribute. Either set *VALUE, or if
20960 the value does not fit in *VALUE, set *BYTES - either already
20961 allocated on the objfile obstack, or newly allocated on OBSTACK,
20962 or, set *BATON, if we translated the constant to a location
20963 expression. */
20964
20965 static void
20966 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20967 const char *name, struct obstack *obstack,
20968 struct dwarf2_cu *cu,
20969 LONGEST *value, const gdb_byte **bytes,
20970 struct dwarf2_locexpr_baton **baton)
20971 {
20972 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20973 struct comp_unit_head *cu_header = &cu->header;
20974 struct dwarf_block *blk;
20975 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20976 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20977
20978 *value = 0;
20979 *bytes = NULL;
20980 *baton = NULL;
20981
20982 switch (attr->form)
20983 {
20984 case DW_FORM_addr:
20985 case DW_FORM_addrx:
20986 case DW_FORM_GNU_addr_index:
20987 {
20988 gdb_byte *data;
20989
20990 if (TYPE_LENGTH (type) != cu_header->addr_size)
20991 dwarf2_const_value_length_mismatch_complaint (name,
20992 cu_header->addr_size,
20993 TYPE_LENGTH (type));
20994 /* Symbols of this form are reasonably rare, so we just
20995 piggyback on the existing location code rather than writing
20996 a new implementation of symbol_computed_ops. */
20997 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20998 (*baton)->per_cu = cu->per_cu;
20999 gdb_assert ((*baton)->per_cu);
21000
21001 (*baton)->size = 2 + cu_header->addr_size;
21002 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21003 (*baton)->data = data;
21004
21005 data[0] = DW_OP_addr;
21006 store_unsigned_integer (&data[1], cu_header->addr_size,
21007 byte_order, DW_ADDR (attr));
21008 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21009 }
21010 break;
21011 case DW_FORM_string:
21012 case DW_FORM_strp:
21013 case DW_FORM_strx:
21014 case DW_FORM_GNU_str_index:
21015 case DW_FORM_GNU_strp_alt:
21016 /* DW_STRING is already allocated on the objfile obstack, point
21017 directly to it. */
21018 *bytes = (const gdb_byte *) DW_STRING (attr);
21019 break;
21020 case DW_FORM_block1:
21021 case DW_FORM_block2:
21022 case DW_FORM_block4:
21023 case DW_FORM_block:
21024 case DW_FORM_exprloc:
21025 case DW_FORM_data16:
21026 blk = DW_BLOCK (attr);
21027 if (TYPE_LENGTH (type) != blk->size)
21028 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21029 TYPE_LENGTH (type));
21030 *bytes = blk->data;
21031 break;
21032
21033 /* The DW_AT_const_value attributes are supposed to carry the
21034 symbol's value "represented as it would be on the target
21035 architecture." By the time we get here, it's already been
21036 converted to host endianness, so we just need to sign- or
21037 zero-extend it as appropriate. */
21038 case DW_FORM_data1:
21039 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21040 break;
21041 case DW_FORM_data2:
21042 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21043 break;
21044 case DW_FORM_data4:
21045 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21046 break;
21047 case DW_FORM_data8:
21048 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21049 break;
21050
21051 case DW_FORM_sdata:
21052 case DW_FORM_implicit_const:
21053 *value = DW_SND (attr);
21054 break;
21055
21056 case DW_FORM_udata:
21057 *value = DW_UNSND (attr);
21058 break;
21059
21060 default:
21061 complaint (_("unsupported const value attribute form: '%s'"),
21062 dwarf_form_name (attr->form));
21063 *value = 0;
21064 break;
21065 }
21066 }
21067
21068
21069 /* Copy constant value from an attribute to a symbol. */
21070
21071 static void
21072 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21073 struct dwarf2_cu *cu)
21074 {
21075 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21076 LONGEST value;
21077 const gdb_byte *bytes;
21078 struct dwarf2_locexpr_baton *baton;
21079
21080 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21081 sym->print_name (),
21082 &objfile->objfile_obstack, cu,
21083 &value, &bytes, &baton);
21084
21085 if (baton != NULL)
21086 {
21087 SYMBOL_LOCATION_BATON (sym) = baton;
21088 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21089 }
21090 else if (bytes != NULL)
21091 {
21092 SYMBOL_VALUE_BYTES (sym) = bytes;
21093 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21094 }
21095 else
21096 {
21097 SYMBOL_VALUE (sym) = value;
21098 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21099 }
21100 }
21101
21102 /* Return the type of the die in question using its DW_AT_type attribute. */
21103
21104 static struct type *
21105 die_type (struct die_info *die, struct dwarf2_cu *cu)
21106 {
21107 struct attribute *type_attr;
21108
21109 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21110 if (!type_attr)
21111 {
21112 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21113 /* A missing DW_AT_type represents a void type. */
21114 return objfile_type (objfile)->builtin_void;
21115 }
21116
21117 return lookup_die_type (die, type_attr, cu);
21118 }
21119
21120 /* True iff CU's producer generates GNAT Ada auxiliary information
21121 that allows to find parallel types through that information instead
21122 of having to do expensive parallel lookups by type name. */
21123
21124 static int
21125 need_gnat_info (struct dwarf2_cu *cu)
21126 {
21127 /* Assume that the Ada compiler was GNAT, which always produces
21128 the auxiliary information. */
21129 return (cu->language == language_ada);
21130 }
21131
21132 /* Return the auxiliary type of the die in question using its
21133 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21134 attribute is not present. */
21135
21136 static struct type *
21137 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21138 {
21139 struct attribute *type_attr;
21140
21141 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21142 if (!type_attr)
21143 return NULL;
21144
21145 return lookup_die_type (die, type_attr, cu);
21146 }
21147
21148 /* If DIE has a descriptive_type attribute, then set the TYPE's
21149 descriptive type accordingly. */
21150
21151 static void
21152 set_descriptive_type (struct type *type, struct die_info *die,
21153 struct dwarf2_cu *cu)
21154 {
21155 struct type *descriptive_type = die_descriptive_type (die, cu);
21156
21157 if (descriptive_type)
21158 {
21159 ALLOCATE_GNAT_AUX_TYPE (type);
21160 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21161 }
21162 }
21163
21164 /* Return the containing type of the die in question using its
21165 DW_AT_containing_type attribute. */
21166
21167 static struct type *
21168 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21169 {
21170 struct attribute *type_attr;
21171 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21172
21173 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21174 if (!type_attr)
21175 error (_("Dwarf Error: Problem turning containing type into gdb type "
21176 "[in module %s]"), objfile_name (objfile));
21177
21178 return lookup_die_type (die, type_attr, cu);
21179 }
21180
21181 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21182
21183 static struct type *
21184 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21185 {
21186 struct dwarf2_per_objfile *dwarf2_per_objfile
21187 = cu->per_cu->dwarf2_per_objfile;
21188 struct objfile *objfile = dwarf2_per_objfile->objfile;
21189 char *saved;
21190
21191 std::string message
21192 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21193 objfile_name (objfile),
21194 sect_offset_str (cu->header.sect_off),
21195 sect_offset_str (die->sect_off));
21196 saved = obstack_strdup (&objfile->objfile_obstack, message);
21197
21198 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21199 }
21200
21201 /* Look up the type of DIE in CU using its type attribute ATTR.
21202 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21203 DW_AT_containing_type.
21204 If there is no type substitute an error marker. */
21205
21206 static struct type *
21207 lookup_die_type (struct die_info *die, const struct attribute *attr,
21208 struct dwarf2_cu *cu)
21209 {
21210 struct dwarf2_per_objfile *dwarf2_per_objfile
21211 = cu->per_cu->dwarf2_per_objfile;
21212 struct objfile *objfile = dwarf2_per_objfile->objfile;
21213 struct type *this_type;
21214
21215 gdb_assert (attr->name == DW_AT_type
21216 || attr->name == DW_AT_GNAT_descriptive_type
21217 || attr->name == DW_AT_containing_type);
21218
21219 /* First see if we have it cached. */
21220
21221 if (attr->form == DW_FORM_GNU_ref_alt)
21222 {
21223 struct dwarf2_per_cu_data *per_cu;
21224 sect_offset sect_off = attr->get_ref_die_offset ();
21225
21226 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21227 dwarf2_per_objfile);
21228 this_type = get_die_type_at_offset (sect_off, per_cu);
21229 }
21230 else if (attr->form_is_ref ())
21231 {
21232 sect_offset sect_off = attr->get_ref_die_offset ();
21233
21234 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21235 }
21236 else if (attr->form == DW_FORM_ref_sig8)
21237 {
21238 ULONGEST signature = DW_SIGNATURE (attr);
21239
21240 return get_signatured_type (die, signature, cu);
21241 }
21242 else
21243 {
21244 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21245 " at %s [in module %s]"),
21246 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21247 objfile_name (objfile));
21248 return build_error_marker_type (cu, die);
21249 }
21250
21251 /* If not cached we need to read it in. */
21252
21253 if (this_type == NULL)
21254 {
21255 struct die_info *type_die = NULL;
21256 struct dwarf2_cu *type_cu = cu;
21257
21258 if (attr->form_is_ref ())
21259 type_die = follow_die_ref (die, attr, &type_cu);
21260 if (type_die == NULL)
21261 return build_error_marker_type (cu, die);
21262 /* If we find the type now, it's probably because the type came
21263 from an inter-CU reference and the type's CU got expanded before
21264 ours. */
21265 this_type = read_type_die (type_die, type_cu);
21266 }
21267
21268 /* If we still don't have a type use an error marker. */
21269
21270 if (this_type == NULL)
21271 return build_error_marker_type (cu, die);
21272
21273 return this_type;
21274 }
21275
21276 /* Return the type in DIE, CU.
21277 Returns NULL for invalid types.
21278
21279 This first does a lookup in die_type_hash,
21280 and only reads the die in if necessary.
21281
21282 NOTE: This can be called when reading in partial or full symbols. */
21283
21284 static struct type *
21285 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21286 {
21287 struct type *this_type;
21288
21289 this_type = get_die_type (die, cu);
21290 if (this_type)
21291 return this_type;
21292
21293 return read_type_die_1 (die, cu);
21294 }
21295
21296 /* Read the type in DIE, CU.
21297 Returns NULL for invalid types. */
21298
21299 static struct type *
21300 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21301 {
21302 struct type *this_type = NULL;
21303
21304 switch (die->tag)
21305 {
21306 case DW_TAG_class_type:
21307 case DW_TAG_interface_type:
21308 case DW_TAG_structure_type:
21309 case DW_TAG_union_type:
21310 this_type = read_structure_type (die, cu);
21311 break;
21312 case DW_TAG_enumeration_type:
21313 this_type = read_enumeration_type (die, cu);
21314 break;
21315 case DW_TAG_subprogram:
21316 case DW_TAG_subroutine_type:
21317 case DW_TAG_inlined_subroutine:
21318 this_type = read_subroutine_type (die, cu);
21319 break;
21320 case DW_TAG_array_type:
21321 this_type = read_array_type (die, cu);
21322 break;
21323 case DW_TAG_set_type:
21324 this_type = read_set_type (die, cu);
21325 break;
21326 case DW_TAG_pointer_type:
21327 this_type = read_tag_pointer_type (die, cu);
21328 break;
21329 case DW_TAG_ptr_to_member_type:
21330 this_type = read_tag_ptr_to_member_type (die, cu);
21331 break;
21332 case DW_TAG_reference_type:
21333 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21334 break;
21335 case DW_TAG_rvalue_reference_type:
21336 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21337 break;
21338 case DW_TAG_const_type:
21339 this_type = read_tag_const_type (die, cu);
21340 break;
21341 case DW_TAG_volatile_type:
21342 this_type = read_tag_volatile_type (die, cu);
21343 break;
21344 case DW_TAG_restrict_type:
21345 this_type = read_tag_restrict_type (die, cu);
21346 break;
21347 case DW_TAG_string_type:
21348 this_type = read_tag_string_type (die, cu);
21349 break;
21350 case DW_TAG_typedef:
21351 this_type = read_typedef (die, cu);
21352 break;
21353 case DW_TAG_subrange_type:
21354 this_type = read_subrange_type (die, cu);
21355 break;
21356 case DW_TAG_base_type:
21357 this_type = read_base_type (die, cu);
21358 break;
21359 case DW_TAG_unspecified_type:
21360 this_type = read_unspecified_type (die, cu);
21361 break;
21362 case DW_TAG_namespace:
21363 this_type = read_namespace_type (die, cu);
21364 break;
21365 case DW_TAG_module:
21366 this_type = read_module_type (die, cu);
21367 break;
21368 case DW_TAG_atomic_type:
21369 this_type = read_tag_atomic_type (die, cu);
21370 break;
21371 default:
21372 complaint (_("unexpected tag in read_type_die: '%s'"),
21373 dwarf_tag_name (die->tag));
21374 break;
21375 }
21376
21377 return this_type;
21378 }
21379
21380 /* See if we can figure out if the class lives in a namespace. We do
21381 this by looking for a member function; its demangled name will
21382 contain namespace info, if there is any.
21383 Return the computed name or NULL.
21384 Space for the result is allocated on the objfile's obstack.
21385 This is the full-die version of guess_partial_die_structure_name.
21386 In this case we know DIE has no useful parent. */
21387
21388 static const char *
21389 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21390 {
21391 struct die_info *spec_die;
21392 struct dwarf2_cu *spec_cu;
21393 struct die_info *child;
21394 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21395
21396 spec_cu = cu;
21397 spec_die = die_specification (die, &spec_cu);
21398 if (spec_die != NULL)
21399 {
21400 die = spec_die;
21401 cu = spec_cu;
21402 }
21403
21404 for (child = die->child;
21405 child != NULL;
21406 child = child->sibling)
21407 {
21408 if (child->tag == DW_TAG_subprogram)
21409 {
21410 const char *linkage_name = dw2_linkage_name (child, cu);
21411
21412 if (linkage_name != NULL)
21413 {
21414 gdb::unique_xmalloc_ptr<char> actual_name
21415 (language_class_name_from_physname (cu->language_defn,
21416 linkage_name));
21417 const char *name = NULL;
21418
21419 if (actual_name != NULL)
21420 {
21421 const char *die_name = dwarf2_name (die, cu);
21422
21423 if (die_name != NULL
21424 && strcmp (die_name, actual_name.get ()) != 0)
21425 {
21426 /* Strip off the class name from the full name.
21427 We want the prefix. */
21428 int die_name_len = strlen (die_name);
21429 int actual_name_len = strlen (actual_name.get ());
21430 const char *ptr = actual_name.get ();
21431
21432 /* Test for '::' as a sanity check. */
21433 if (actual_name_len > die_name_len + 2
21434 && ptr[actual_name_len - die_name_len - 1] == ':')
21435 name = obstack_strndup (
21436 &objfile->per_bfd->storage_obstack,
21437 ptr, actual_name_len - die_name_len - 2);
21438 }
21439 }
21440 return name;
21441 }
21442 }
21443 }
21444
21445 return NULL;
21446 }
21447
21448 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21449 prefix part in such case. See
21450 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21451
21452 static const char *
21453 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21454 {
21455 struct attribute *attr;
21456 const char *base;
21457
21458 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21459 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21460 return NULL;
21461
21462 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21463 return NULL;
21464
21465 attr = dw2_linkage_name_attr (die, cu);
21466 if (attr == NULL || DW_STRING (attr) == NULL)
21467 return NULL;
21468
21469 /* dwarf2_name had to be already called. */
21470 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21471
21472 /* Strip the base name, keep any leading namespaces/classes. */
21473 base = strrchr (DW_STRING (attr), ':');
21474 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21475 return "";
21476
21477 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21478 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21479 DW_STRING (attr),
21480 &base[-1] - DW_STRING (attr));
21481 }
21482
21483 /* Return the name of the namespace/class that DIE is defined within,
21484 or "" if we can't tell. The caller should not xfree the result.
21485
21486 For example, if we're within the method foo() in the following
21487 code:
21488
21489 namespace N {
21490 class C {
21491 void foo () {
21492 }
21493 };
21494 }
21495
21496 then determine_prefix on foo's die will return "N::C". */
21497
21498 static const char *
21499 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21500 {
21501 struct dwarf2_per_objfile *dwarf2_per_objfile
21502 = cu->per_cu->dwarf2_per_objfile;
21503 struct die_info *parent, *spec_die;
21504 struct dwarf2_cu *spec_cu;
21505 struct type *parent_type;
21506 const char *retval;
21507
21508 if (cu->language != language_cplus
21509 && cu->language != language_fortran && cu->language != language_d
21510 && cu->language != language_rust)
21511 return "";
21512
21513 retval = anonymous_struct_prefix (die, cu);
21514 if (retval)
21515 return retval;
21516
21517 /* We have to be careful in the presence of DW_AT_specification.
21518 For example, with GCC 3.4, given the code
21519
21520 namespace N {
21521 void foo() {
21522 // Definition of N::foo.
21523 }
21524 }
21525
21526 then we'll have a tree of DIEs like this:
21527
21528 1: DW_TAG_compile_unit
21529 2: DW_TAG_namespace // N
21530 3: DW_TAG_subprogram // declaration of N::foo
21531 4: DW_TAG_subprogram // definition of N::foo
21532 DW_AT_specification // refers to die #3
21533
21534 Thus, when processing die #4, we have to pretend that we're in
21535 the context of its DW_AT_specification, namely the contex of die
21536 #3. */
21537 spec_cu = cu;
21538 spec_die = die_specification (die, &spec_cu);
21539 if (spec_die == NULL)
21540 parent = die->parent;
21541 else
21542 {
21543 parent = spec_die->parent;
21544 cu = spec_cu;
21545 }
21546
21547 if (parent == NULL)
21548 return "";
21549 else if (parent->building_fullname)
21550 {
21551 const char *name;
21552 const char *parent_name;
21553
21554 /* It has been seen on RealView 2.2 built binaries,
21555 DW_TAG_template_type_param types actually _defined_ as
21556 children of the parent class:
21557
21558 enum E {};
21559 template class <class Enum> Class{};
21560 Class<enum E> class_e;
21561
21562 1: DW_TAG_class_type (Class)
21563 2: DW_TAG_enumeration_type (E)
21564 3: DW_TAG_enumerator (enum1:0)
21565 3: DW_TAG_enumerator (enum2:1)
21566 ...
21567 2: DW_TAG_template_type_param
21568 DW_AT_type DW_FORM_ref_udata (E)
21569
21570 Besides being broken debug info, it can put GDB into an
21571 infinite loop. Consider:
21572
21573 When we're building the full name for Class<E>, we'll start
21574 at Class, and go look over its template type parameters,
21575 finding E. We'll then try to build the full name of E, and
21576 reach here. We're now trying to build the full name of E,
21577 and look over the parent DIE for containing scope. In the
21578 broken case, if we followed the parent DIE of E, we'd again
21579 find Class, and once again go look at its template type
21580 arguments, etc., etc. Simply don't consider such parent die
21581 as source-level parent of this die (it can't be, the language
21582 doesn't allow it), and break the loop here. */
21583 name = dwarf2_name (die, cu);
21584 parent_name = dwarf2_name (parent, cu);
21585 complaint (_("template param type '%s' defined within parent '%s'"),
21586 name ? name : "<unknown>",
21587 parent_name ? parent_name : "<unknown>");
21588 return "";
21589 }
21590 else
21591 switch (parent->tag)
21592 {
21593 case DW_TAG_namespace:
21594 parent_type = read_type_die (parent, cu);
21595 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21596 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21597 Work around this problem here. */
21598 if (cu->language == language_cplus
21599 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21600 return "";
21601 /* We give a name to even anonymous namespaces. */
21602 return TYPE_NAME (parent_type);
21603 case DW_TAG_class_type:
21604 case DW_TAG_interface_type:
21605 case DW_TAG_structure_type:
21606 case DW_TAG_union_type:
21607 case DW_TAG_module:
21608 parent_type = read_type_die (parent, cu);
21609 if (TYPE_NAME (parent_type) != NULL)
21610 return TYPE_NAME (parent_type);
21611 else
21612 /* An anonymous structure is only allowed non-static data
21613 members; no typedefs, no member functions, et cetera.
21614 So it does not need a prefix. */
21615 return "";
21616 case DW_TAG_compile_unit:
21617 case DW_TAG_partial_unit:
21618 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21619 if (cu->language == language_cplus
21620 && !dwarf2_per_objfile->types.empty ()
21621 && die->child != NULL
21622 && (die->tag == DW_TAG_class_type
21623 || die->tag == DW_TAG_structure_type
21624 || die->tag == DW_TAG_union_type))
21625 {
21626 const char *name = guess_full_die_structure_name (die, cu);
21627 if (name != NULL)
21628 return name;
21629 }
21630 return "";
21631 case DW_TAG_subprogram:
21632 /* Nested subroutines in Fortran get a prefix with the name
21633 of the parent's subroutine. */
21634 if (cu->language == language_fortran)
21635 {
21636 if ((die->tag == DW_TAG_subprogram)
21637 && (dwarf2_name (parent, cu) != NULL))
21638 return dwarf2_name (parent, cu);
21639 }
21640 return determine_prefix (parent, cu);
21641 case DW_TAG_enumeration_type:
21642 parent_type = read_type_die (parent, cu);
21643 if (TYPE_DECLARED_CLASS (parent_type))
21644 {
21645 if (TYPE_NAME (parent_type) != NULL)
21646 return TYPE_NAME (parent_type);
21647 return "";
21648 }
21649 /* Fall through. */
21650 default:
21651 return determine_prefix (parent, cu);
21652 }
21653 }
21654
21655 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21656 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21657 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21658 an obconcat, otherwise allocate storage for the result. The CU argument is
21659 used to determine the language and hence, the appropriate separator. */
21660
21661 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21662
21663 static char *
21664 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21665 int physname, struct dwarf2_cu *cu)
21666 {
21667 const char *lead = "";
21668 const char *sep;
21669
21670 if (suffix == NULL || suffix[0] == '\0'
21671 || prefix == NULL || prefix[0] == '\0')
21672 sep = "";
21673 else if (cu->language == language_d)
21674 {
21675 /* For D, the 'main' function could be defined in any module, but it
21676 should never be prefixed. */
21677 if (strcmp (suffix, "D main") == 0)
21678 {
21679 prefix = "";
21680 sep = "";
21681 }
21682 else
21683 sep = ".";
21684 }
21685 else if (cu->language == language_fortran && physname)
21686 {
21687 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21688 DW_AT_MIPS_linkage_name is preferred and used instead. */
21689
21690 lead = "__";
21691 sep = "_MOD_";
21692 }
21693 else
21694 sep = "::";
21695
21696 if (prefix == NULL)
21697 prefix = "";
21698 if (suffix == NULL)
21699 suffix = "";
21700
21701 if (obs == NULL)
21702 {
21703 char *retval
21704 = ((char *)
21705 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21706
21707 strcpy (retval, lead);
21708 strcat (retval, prefix);
21709 strcat (retval, sep);
21710 strcat (retval, suffix);
21711 return retval;
21712 }
21713 else
21714 {
21715 /* We have an obstack. */
21716 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21717 }
21718 }
21719
21720 /* Get name of a die, return NULL if not found. */
21721
21722 static const char *
21723 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21724 struct objfile *objfile)
21725 {
21726 if (name && cu->language == language_cplus)
21727 {
21728 std::string canon_name = cp_canonicalize_string (name);
21729
21730 if (!canon_name.empty ())
21731 {
21732 if (canon_name != name)
21733 name = objfile->intern (canon_name);
21734 }
21735 }
21736
21737 return name;
21738 }
21739
21740 /* Get name of a die, return NULL if not found.
21741 Anonymous namespaces are converted to their magic string. */
21742
21743 static const char *
21744 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21745 {
21746 struct attribute *attr;
21747 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21748
21749 attr = dwarf2_attr (die, DW_AT_name, cu);
21750 if ((!attr || !DW_STRING (attr))
21751 && die->tag != DW_TAG_namespace
21752 && die->tag != DW_TAG_class_type
21753 && die->tag != DW_TAG_interface_type
21754 && die->tag != DW_TAG_structure_type
21755 && die->tag != DW_TAG_union_type)
21756 return NULL;
21757
21758 switch (die->tag)
21759 {
21760 case DW_TAG_compile_unit:
21761 case DW_TAG_partial_unit:
21762 /* Compilation units have a DW_AT_name that is a filename, not
21763 a source language identifier. */
21764 case DW_TAG_enumeration_type:
21765 case DW_TAG_enumerator:
21766 /* These tags always have simple identifiers already; no need
21767 to canonicalize them. */
21768 return DW_STRING (attr);
21769
21770 case DW_TAG_namespace:
21771 if (attr != NULL && DW_STRING (attr) != NULL)
21772 return DW_STRING (attr);
21773 return CP_ANONYMOUS_NAMESPACE_STR;
21774
21775 case DW_TAG_class_type:
21776 case DW_TAG_interface_type:
21777 case DW_TAG_structure_type:
21778 case DW_TAG_union_type:
21779 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21780 structures or unions. These were of the form "._%d" in GCC 4.1,
21781 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21782 and GCC 4.4. We work around this problem by ignoring these. */
21783 if (attr && DW_STRING (attr)
21784 && (startswith (DW_STRING (attr), "._")
21785 || startswith (DW_STRING (attr), "<anonymous")))
21786 return NULL;
21787
21788 /* GCC might emit a nameless typedef that has a linkage name. See
21789 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21790 if (!attr || DW_STRING (attr) == NULL)
21791 {
21792 attr = dw2_linkage_name_attr (die, cu);
21793 if (attr == NULL || DW_STRING (attr) == NULL)
21794 return NULL;
21795
21796 /* Avoid demangling DW_STRING (attr) the second time on a second
21797 call for the same DIE. */
21798 if (!DW_STRING_IS_CANONICAL (attr))
21799 {
21800 gdb::unique_xmalloc_ptr<char> demangled
21801 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21802 if (demangled == nullptr)
21803 return nullptr;
21804
21805 DW_STRING (attr) = objfile->intern (demangled.get ());
21806 DW_STRING_IS_CANONICAL (attr) = 1;
21807 }
21808
21809 /* Strip any leading namespaces/classes, keep only the base name.
21810 DW_AT_name for named DIEs does not contain the prefixes. */
21811 const char *base = strrchr (DW_STRING (attr), ':');
21812 if (base && base > DW_STRING (attr) && base[-1] == ':')
21813 return &base[1];
21814 else
21815 return DW_STRING (attr);
21816 }
21817 break;
21818
21819 default:
21820 break;
21821 }
21822
21823 if (!DW_STRING_IS_CANONICAL (attr))
21824 {
21825 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21826 objfile);
21827 DW_STRING_IS_CANONICAL (attr) = 1;
21828 }
21829 return DW_STRING (attr);
21830 }
21831
21832 /* Return the die that this die in an extension of, or NULL if there
21833 is none. *EXT_CU is the CU containing DIE on input, and the CU
21834 containing the return value on output. */
21835
21836 static struct die_info *
21837 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21838 {
21839 struct attribute *attr;
21840
21841 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21842 if (attr == NULL)
21843 return NULL;
21844
21845 return follow_die_ref (die, attr, ext_cu);
21846 }
21847
21848 static void
21849 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21850 {
21851 unsigned int i;
21852
21853 print_spaces (indent, f);
21854 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21855 dwarf_tag_name (die->tag), die->abbrev,
21856 sect_offset_str (die->sect_off));
21857
21858 if (die->parent != NULL)
21859 {
21860 print_spaces (indent, f);
21861 fprintf_unfiltered (f, " parent at offset: %s\n",
21862 sect_offset_str (die->parent->sect_off));
21863 }
21864
21865 print_spaces (indent, f);
21866 fprintf_unfiltered (f, " has children: %s\n",
21867 dwarf_bool_name (die->child != NULL));
21868
21869 print_spaces (indent, f);
21870 fprintf_unfiltered (f, " attributes:\n");
21871
21872 for (i = 0; i < die->num_attrs; ++i)
21873 {
21874 print_spaces (indent, f);
21875 fprintf_unfiltered (f, " %s (%s) ",
21876 dwarf_attr_name (die->attrs[i].name),
21877 dwarf_form_name (die->attrs[i].form));
21878
21879 switch (die->attrs[i].form)
21880 {
21881 case DW_FORM_addr:
21882 case DW_FORM_addrx:
21883 case DW_FORM_GNU_addr_index:
21884 fprintf_unfiltered (f, "address: ");
21885 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21886 break;
21887 case DW_FORM_block2:
21888 case DW_FORM_block4:
21889 case DW_FORM_block:
21890 case DW_FORM_block1:
21891 fprintf_unfiltered (f, "block: size %s",
21892 pulongest (DW_BLOCK (&die->attrs[i])->size));
21893 break;
21894 case DW_FORM_exprloc:
21895 fprintf_unfiltered (f, "expression: size %s",
21896 pulongest (DW_BLOCK (&die->attrs[i])->size));
21897 break;
21898 case DW_FORM_data16:
21899 fprintf_unfiltered (f, "constant of 16 bytes");
21900 break;
21901 case DW_FORM_ref_addr:
21902 fprintf_unfiltered (f, "ref address: ");
21903 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21904 break;
21905 case DW_FORM_GNU_ref_alt:
21906 fprintf_unfiltered (f, "alt ref address: ");
21907 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21908 break;
21909 case DW_FORM_ref1:
21910 case DW_FORM_ref2:
21911 case DW_FORM_ref4:
21912 case DW_FORM_ref8:
21913 case DW_FORM_ref_udata:
21914 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21915 (long) (DW_UNSND (&die->attrs[i])));
21916 break;
21917 case DW_FORM_data1:
21918 case DW_FORM_data2:
21919 case DW_FORM_data4:
21920 case DW_FORM_data8:
21921 case DW_FORM_udata:
21922 case DW_FORM_sdata:
21923 fprintf_unfiltered (f, "constant: %s",
21924 pulongest (DW_UNSND (&die->attrs[i])));
21925 break;
21926 case DW_FORM_sec_offset:
21927 fprintf_unfiltered (f, "section offset: %s",
21928 pulongest (DW_UNSND (&die->attrs[i])));
21929 break;
21930 case DW_FORM_ref_sig8:
21931 fprintf_unfiltered (f, "signature: %s",
21932 hex_string (DW_SIGNATURE (&die->attrs[i])));
21933 break;
21934 case DW_FORM_string:
21935 case DW_FORM_strp:
21936 case DW_FORM_line_strp:
21937 case DW_FORM_strx:
21938 case DW_FORM_GNU_str_index:
21939 case DW_FORM_GNU_strp_alt:
21940 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21941 DW_STRING (&die->attrs[i])
21942 ? DW_STRING (&die->attrs[i]) : "",
21943 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21944 break;
21945 case DW_FORM_flag:
21946 if (DW_UNSND (&die->attrs[i]))
21947 fprintf_unfiltered (f, "flag: TRUE");
21948 else
21949 fprintf_unfiltered (f, "flag: FALSE");
21950 break;
21951 case DW_FORM_flag_present:
21952 fprintf_unfiltered (f, "flag: TRUE");
21953 break;
21954 case DW_FORM_indirect:
21955 /* The reader will have reduced the indirect form to
21956 the "base form" so this form should not occur. */
21957 fprintf_unfiltered (f,
21958 "unexpected attribute form: DW_FORM_indirect");
21959 break;
21960 case DW_FORM_implicit_const:
21961 fprintf_unfiltered (f, "constant: %s",
21962 plongest (DW_SND (&die->attrs[i])));
21963 break;
21964 default:
21965 fprintf_unfiltered (f, "unsupported attribute form: %d.",
21966 die->attrs[i].form);
21967 break;
21968 }
21969 fprintf_unfiltered (f, "\n");
21970 }
21971 }
21972
21973 static void
21974 dump_die_for_error (struct die_info *die)
21975 {
21976 dump_die_shallow (gdb_stderr, 0, die);
21977 }
21978
21979 static void
21980 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21981 {
21982 int indent = level * 4;
21983
21984 gdb_assert (die != NULL);
21985
21986 if (level >= max_level)
21987 return;
21988
21989 dump_die_shallow (f, indent, die);
21990
21991 if (die->child != NULL)
21992 {
21993 print_spaces (indent, f);
21994 fprintf_unfiltered (f, " Children:");
21995 if (level + 1 < max_level)
21996 {
21997 fprintf_unfiltered (f, "\n");
21998 dump_die_1 (f, level + 1, max_level, die->child);
21999 }
22000 else
22001 {
22002 fprintf_unfiltered (f,
22003 " [not printed, max nesting level reached]\n");
22004 }
22005 }
22006
22007 if (die->sibling != NULL && level > 0)
22008 {
22009 dump_die_1 (f, level, max_level, die->sibling);
22010 }
22011 }
22012
22013 /* This is called from the pdie macro in gdbinit.in.
22014 It's not static so gcc will keep a copy callable from gdb. */
22015
22016 void
22017 dump_die (struct die_info *die, int max_level)
22018 {
22019 dump_die_1 (gdb_stdlog, 0, max_level, die);
22020 }
22021
22022 static void
22023 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22024 {
22025 void **slot;
22026
22027 slot = htab_find_slot_with_hash (cu->die_hash, die,
22028 to_underlying (die->sect_off),
22029 INSERT);
22030
22031 *slot = die;
22032 }
22033
22034 /* Follow reference or signature attribute ATTR of SRC_DIE.
22035 On entry *REF_CU is the CU of SRC_DIE.
22036 On exit *REF_CU is the CU of the result. */
22037
22038 static struct die_info *
22039 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22040 struct dwarf2_cu **ref_cu)
22041 {
22042 struct die_info *die;
22043
22044 if (attr->form_is_ref ())
22045 die = follow_die_ref (src_die, attr, ref_cu);
22046 else if (attr->form == DW_FORM_ref_sig8)
22047 die = follow_die_sig (src_die, attr, ref_cu);
22048 else
22049 {
22050 dump_die_for_error (src_die);
22051 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22052 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22053 }
22054
22055 return die;
22056 }
22057
22058 /* Follow reference OFFSET.
22059 On entry *REF_CU is the CU of the source die referencing OFFSET.
22060 On exit *REF_CU is the CU of the result.
22061 Returns NULL if OFFSET is invalid. */
22062
22063 static struct die_info *
22064 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22065 struct dwarf2_cu **ref_cu)
22066 {
22067 struct die_info temp_die;
22068 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22069 struct dwarf2_per_objfile *dwarf2_per_objfile
22070 = cu->per_cu->dwarf2_per_objfile;
22071
22072 gdb_assert (cu->per_cu != NULL);
22073
22074 target_cu = cu;
22075
22076 if (cu->per_cu->is_debug_types)
22077 {
22078 /* .debug_types CUs cannot reference anything outside their CU.
22079 If they need to, they have to reference a signatured type via
22080 DW_FORM_ref_sig8. */
22081 if (!cu->header.offset_in_cu_p (sect_off))
22082 return NULL;
22083 }
22084 else if (offset_in_dwz != cu->per_cu->is_dwz
22085 || !cu->header.offset_in_cu_p (sect_off))
22086 {
22087 struct dwarf2_per_cu_data *per_cu;
22088
22089 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22090 dwarf2_per_objfile);
22091
22092 /* If necessary, add it to the queue and load its DIEs. */
22093 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22094 load_full_comp_unit (per_cu, false, cu->language);
22095
22096 target_cu = per_cu->cu;
22097 }
22098 else if (cu->dies == NULL)
22099 {
22100 /* We're loading full DIEs during partial symbol reading. */
22101 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22102 load_full_comp_unit (cu->per_cu, false, language_minimal);
22103 }
22104
22105 *ref_cu = target_cu;
22106 temp_die.sect_off = sect_off;
22107
22108 if (target_cu != cu)
22109 target_cu->ancestor = cu;
22110
22111 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22112 &temp_die,
22113 to_underlying (sect_off));
22114 }
22115
22116 /* Follow reference attribute ATTR of SRC_DIE.
22117 On entry *REF_CU is the CU of SRC_DIE.
22118 On exit *REF_CU is the CU of the result. */
22119
22120 static struct die_info *
22121 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22122 struct dwarf2_cu **ref_cu)
22123 {
22124 sect_offset sect_off = attr->get_ref_die_offset ();
22125 struct dwarf2_cu *cu = *ref_cu;
22126 struct die_info *die;
22127
22128 die = follow_die_offset (sect_off,
22129 (attr->form == DW_FORM_GNU_ref_alt
22130 || cu->per_cu->is_dwz),
22131 ref_cu);
22132 if (!die)
22133 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22134 "at %s [in module %s]"),
22135 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22136 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22137
22138 return die;
22139 }
22140
22141 /* See read.h. */
22142
22143 struct dwarf2_locexpr_baton
22144 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22145 dwarf2_per_cu_data *per_cu,
22146 CORE_ADDR (*get_frame_pc) (void *baton),
22147 void *baton, bool resolve_abstract_p)
22148 {
22149 struct dwarf2_cu *cu;
22150 struct die_info *die;
22151 struct attribute *attr;
22152 struct dwarf2_locexpr_baton retval;
22153 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22154 struct objfile *objfile = dwarf2_per_objfile->objfile;
22155
22156 if (per_cu->cu == NULL)
22157 load_cu (per_cu, false);
22158 cu = per_cu->cu;
22159 if (cu == NULL)
22160 {
22161 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22162 Instead just throw an error, not much else we can do. */
22163 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22164 sect_offset_str (sect_off), objfile_name (objfile));
22165 }
22166
22167 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22168 if (!die)
22169 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22170 sect_offset_str (sect_off), objfile_name (objfile));
22171
22172 attr = dwarf2_attr (die, DW_AT_location, cu);
22173 if (!attr && resolve_abstract_p
22174 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
22175 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22176 {
22177 CORE_ADDR pc = (*get_frame_pc) (baton);
22178 CORE_ADDR baseaddr = objfile->text_section_offset ();
22179 struct gdbarch *gdbarch = objfile->arch ();
22180
22181 for (const auto &cand_off
22182 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
22183 {
22184 struct dwarf2_cu *cand_cu = cu;
22185 struct die_info *cand
22186 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22187 if (!cand
22188 || !cand->parent
22189 || cand->parent->tag != DW_TAG_subprogram)
22190 continue;
22191
22192 CORE_ADDR pc_low, pc_high;
22193 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22194 if (pc_low == ((CORE_ADDR) -1))
22195 continue;
22196 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22197 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22198 if (!(pc_low <= pc && pc < pc_high))
22199 continue;
22200
22201 die = cand;
22202 attr = dwarf2_attr (die, DW_AT_location, cu);
22203 break;
22204 }
22205 }
22206
22207 if (!attr)
22208 {
22209 /* DWARF: "If there is no such attribute, then there is no effect.".
22210 DATA is ignored if SIZE is 0. */
22211
22212 retval.data = NULL;
22213 retval.size = 0;
22214 }
22215 else if (attr->form_is_section_offset ())
22216 {
22217 struct dwarf2_loclist_baton loclist_baton;
22218 CORE_ADDR pc = (*get_frame_pc) (baton);
22219 size_t size;
22220
22221 fill_in_loclist_baton (cu, &loclist_baton, attr);
22222
22223 retval.data = dwarf2_find_location_expression (&loclist_baton,
22224 &size, pc);
22225 retval.size = size;
22226 }
22227 else
22228 {
22229 if (!attr->form_is_block ())
22230 error (_("Dwarf Error: DIE at %s referenced in module %s "
22231 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22232 sect_offset_str (sect_off), objfile_name (objfile));
22233
22234 retval.data = DW_BLOCK (attr)->data;
22235 retval.size = DW_BLOCK (attr)->size;
22236 }
22237 retval.per_cu = cu->per_cu;
22238
22239 age_cached_comp_units (dwarf2_per_objfile);
22240
22241 return retval;
22242 }
22243
22244 /* See read.h. */
22245
22246 struct dwarf2_locexpr_baton
22247 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22248 dwarf2_per_cu_data *per_cu,
22249 CORE_ADDR (*get_frame_pc) (void *baton),
22250 void *baton)
22251 {
22252 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22253
22254 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22255 }
22256
22257 /* Write a constant of a given type as target-ordered bytes into
22258 OBSTACK. */
22259
22260 static const gdb_byte *
22261 write_constant_as_bytes (struct obstack *obstack,
22262 enum bfd_endian byte_order,
22263 struct type *type,
22264 ULONGEST value,
22265 LONGEST *len)
22266 {
22267 gdb_byte *result;
22268
22269 *len = TYPE_LENGTH (type);
22270 result = (gdb_byte *) obstack_alloc (obstack, *len);
22271 store_unsigned_integer (result, *len, byte_order, value);
22272
22273 return result;
22274 }
22275
22276 /* See read.h. */
22277
22278 const gdb_byte *
22279 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22280 dwarf2_per_cu_data *per_cu,
22281 obstack *obstack,
22282 LONGEST *len)
22283 {
22284 struct dwarf2_cu *cu;
22285 struct die_info *die;
22286 struct attribute *attr;
22287 const gdb_byte *result = NULL;
22288 struct type *type;
22289 LONGEST value;
22290 enum bfd_endian byte_order;
22291 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22292
22293 if (per_cu->cu == NULL)
22294 load_cu (per_cu, false);
22295 cu = per_cu->cu;
22296 if (cu == NULL)
22297 {
22298 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22299 Instead just throw an error, not much else we can do. */
22300 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22301 sect_offset_str (sect_off), objfile_name (objfile));
22302 }
22303
22304 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22305 if (!die)
22306 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22307 sect_offset_str (sect_off), objfile_name (objfile));
22308
22309 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22310 if (attr == NULL)
22311 return NULL;
22312
22313 byte_order = (bfd_big_endian (objfile->obfd)
22314 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22315
22316 switch (attr->form)
22317 {
22318 case DW_FORM_addr:
22319 case DW_FORM_addrx:
22320 case DW_FORM_GNU_addr_index:
22321 {
22322 gdb_byte *tem;
22323
22324 *len = cu->header.addr_size;
22325 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22326 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22327 result = tem;
22328 }
22329 break;
22330 case DW_FORM_string:
22331 case DW_FORM_strp:
22332 case DW_FORM_strx:
22333 case DW_FORM_GNU_str_index:
22334 case DW_FORM_GNU_strp_alt:
22335 /* DW_STRING is already allocated on the objfile obstack, point
22336 directly to it. */
22337 result = (const gdb_byte *) DW_STRING (attr);
22338 *len = strlen (DW_STRING (attr));
22339 break;
22340 case DW_FORM_block1:
22341 case DW_FORM_block2:
22342 case DW_FORM_block4:
22343 case DW_FORM_block:
22344 case DW_FORM_exprloc:
22345 case DW_FORM_data16:
22346 result = DW_BLOCK (attr)->data;
22347 *len = DW_BLOCK (attr)->size;
22348 break;
22349
22350 /* The DW_AT_const_value attributes are supposed to carry the
22351 symbol's value "represented as it would be on the target
22352 architecture." By the time we get here, it's already been
22353 converted to host endianness, so we just need to sign- or
22354 zero-extend it as appropriate. */
22355 case DW_FORM_data1:
22356 type = die_type (die, cu);
22357 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22358 if (result == NULL)
22359 result = write_constant_as_bytes (obstack, byte_order,
22360 type, value, len);
22361 break;
22362 case DW_FORM_data2:
22363 type = die_type (die, cu);
22364 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22365 if (result == NULL)
22366 result = write_constant_as_bytes (obstack, byte_order,
22367 type, value, len);
22368 break;
22369 case DW_FORM_data4:
22370 type = die_type (die, cu);
22371 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22372 if (result == NULL)
22373 result = write_constant_as_bytes (obstack, byte_order,
22374 type, value, len);
22375 break;
22376 case DW_FORM_data8:
22377 type = die_type (die, cu);
22378 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22379 if (result == NULL)
22380 result = write_constant_as_bytes (obstack, byte_order,
22381 type, value, len);
22382 break;
22383
22384 case DW_FORM_sdata:
22385 case DW_FORM_implicit_const:
22386 type = die_type (die, cu);
22387 result = write_constant_as_bytes (obstack, byte_order,
22388 type, DW_SND (attr), len);
22389 break;
22390
22391 case DW_FORM_udata:
22392 type = die_type (die, cu);
22393 result = write_constant_as_bytes (obstack, byte_order,
22394 type, DW_UNSND (attr), len);
22395 break;
22396
22397 default:
22398 complaint (_("unsupported const value attribute form: '%s'"),
22399 dwarf_form_name (attr->form));
22400 break;
22401 }
22402
22403 return result;
22404 }
22405
22406 /* See read.h. */
22407
22408 struct type *
22409 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22410 dwarf2_per_cu_data *per_cu)
22411 {
22412 struct dwarf2_cu *cu;
22413 struct die_info *die;
22414
22415 if (per_cu->cu == NULL)
22416 load_cu (per_cu, false);
22417 cu = per_cu->cu;
22418 if (!cu)
22419 return NULL;
22420
22421 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22422 if (!die)
22423 return NULL;
22424
22425 return die_type (die, cu);
22426 }
22427
22428 /* See read.h. */
22429
22430 struct type *
22431 dwarf2_get_die_type (cu_offset die_offset,
22432 struct dwarf2_per_cu_data *per_cu)
22433 {
22434 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22435 return get_die_type_at_offset (die_offset_sect, per_cu);
22436 }
22437
22438 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22439 On entry *REF_CU is the CU of SRC_DIE.
22440 On exit *REF_CU is the CU of the result.
22441 Returns NULL if the referenced DIE isn't found. */
22442
22443 static struct die_info *
22444 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22445 struct dwarf2_cu **ref_cu)
22446 {
22447 struct die_info temp_die;
22448 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22449 struct die_info *die;
22450
22451 /* While it might be nice to assert sig_type->type == NULL here,
22452 we can get here for DW_AT_imported_declaration where we need
22453 the DIE not the type. */
22454
22455 /* If necessary, add it to the queue and load its DIEs. */
22456
22457 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22458 read_signatured_type (sig_type);
22459
22460 sig_cu = sig_type->per_cu.cu;
22461 gdb_assert (sig_cu != NULL);
22462 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22463 temp_die.sect_off = sig_type->type_offset_in_section;
22464 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22465 to_underlying (temp_die.sect_off));
22466 if (die)
22467 {
22468 struct dwarf2_per_objfile *dwarf2_per_objfile
22469 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22470
22471 /* For .gdb_index version 7 keep track of included TUs.
22472 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22473 if (dwarf2_per_objfile->index_table != NULL
22474 && dwarf2_per_objfile->index_table->version <= 7)
22475 {
22476 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22477 }
22478
22479 *ref_cu = sig_cu;
22480 if (sig_cu != cu)
22481 sig_cu->ancestor = cu;
22482
22483 return die;
22484 }
22485
22486 return NULL;
22487 }
22488
22489 /* Follow signatured type referenced by ATTR in SRC_DIE.
22490 On entry *REF_CU is the CU of SRC_DIE.
22491 On exit *REF_CU is the CU of the result.
22492 The result is the DIE of the type.
22493 If the referenced type cannot be found an error is thrown. */
22494
22495 static struct die_info *
22496 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22497 struct dwarf2_cu **ref_cu)
22498 {
22499 ULONGEST signature = DW_SIGNATURE (attr);
22500 struct signatured_type *sig_type;
22501 struct die_info *die;
22502
22503 gdb_assert (attr->form == DW_FORM_ref_sig8);
22504
22505 sig_type = lookup_signatured_type (*ref_cu, signature);
22506 /* sig_type will be NULL if the signatured type is missing from
22507 the debug info. */
22508 if (sig_type == NULL)
22509 {
22510 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22511 " from DIE at %s [in module %s]"),
22512 hex_string (signature), sect_offset_str (src_die->sect_off),
22513 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22514 }
22515
22516 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22517 if (die == NULL)
22518 {
22519 dump_die_for_error (src_die);
22520 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22521 " from DIE at %s [in module %s]"),
22522 hex_string (signature), sect_offset_str (src_die->sect_off),
22523 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22524 }
22525
22526 return die;
22527 }
22528
22529 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22530 reading in and processing the type unit if necessary. */
22531
22532 static struct type *
22533 get_signatured_type (struct die_info *die, ULONGEST signature,
22534 struct dwarf2_cu *cu)
22535 {
22536 struct dwarf2_per_objfile *dwarf2_per_objfile
22537 = cu->per_cu->dwarf2_per_objfile;
22538 struct signatured_type *sig_type;
22539 struct dwarf2_cu *type_cu;
22540 struct die_info *type_die;
22541 struct type *type;
22542
22543 sig_type = lookup_signatured_type (cu, signature);
22544 /* sig_type will be NULL if the signatured type is missing from
22545 the debug info. */
22546 if (sig_type == NULL)
22547 {
22548 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22549 " from DIE at %s [in module %s]"),
22550 hex_string (signature), sect_offset_str (die->sect_off),
22551 objfile_name (dwarf2_per_objfile->objfile));
22552 return build_error_marker_type (cu, die);
22553 }
22554
22555 /* If we already know the type we're done. */
22556 if (sig_type->type != NULL)
22557 return sig_type->type;
22558
22559 type_cu = cu;
22560 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22561 if (type_die != NULL)
22562 {
22563 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22564 is created. This is important, for example, because for c++ classes
22565 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22566 type = read_type_die (type_die, type_cu);
22567 if (type == NULL)
22568 {
22569 complaint (_("Dwarf Error: Cannot build signatured type %s"
22570 " referenced from DIE at %s [in module %s]"),
22571 hex_string (signature), sect_offset_str (die->sect_off),
22572 objfile_name (dwarf2_per_objfile->objfile));
22573 type = build_error_marker_type (cu, die);
22574 }
22575 }
22576 else
22577 {
22578 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22579 " from DIE at %s [in module %s]"),
22580 hex_string (signature), sect_offset_str (die->sect_off),
22581 objfile_name (dwarf2_per_objfile->objfile));
22582 type = build_error_marker_type (cu, die);
22583 }
22584 sig_type->type = type;
22585
22586 return type;
22587 }
22588
22589 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22590 reading in and processing the type unit if necessary. */
22591
22592 static struct type *
22593 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22594 struct dwarf2_cu *cu) /* ARI: editCase function */
22595 {
22596 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22597 if (attr->form_is_ref ())
22598 {
22599 struct dwarf2_cu *type_cu = cu;
22600 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22601
22602 return read_type_die (type_die, type_cu);
22603 }
22604 else if (attr->form == DW_FORM_ref_sig8)
22605 {
22606 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22607 }
22608 else
22609 {
22610 struct dwarf2_per_objfile *dwarf2_per_objfile
22611 = cu->per_cu->dwarf2_per_objfile;
22612
22613 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22614 " at %s [in module %s]"),
22615 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22616 objfile_name (dwarf2_per_objfile->objfile));
22617 return build_error_marker_type (cu, die);
22618 }
22619 }
22620
22621 /* Load the DIEs associated with type unit PER_CU into memory. */
22622
22623 static void
22624 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22625 {
22626 struct signatured_type *sig_type;
22627
22628 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22629 gdb_assert (! per_cu->type_unit_group_p ());
22630
22631 /* We have the per_cu, but we need the signatured_type.
22632 Fortunately this is an easy translation. */
22633 gdb_assert (per_cu->is_debug_types);
22634 sig_type = (struct signatured_type *) per_cu;
22635
22636 gdb_assert (per_cu->cu == NULL);
22637
22638 read_signatured_type (sig_type);
22639
22640 gdb_assert (per_cu->cu != NULL);
22641 }
22642
22643 /* Read in a signatured type and build its CU and DIEs.
22644 If the type is a stub for the real type in a DWO file,
22645 read in the real type from the DWO file as well. */
22646
22647 static void
22648 read_signatured_type (struct signatured_type *sig_type)
22649 {
22650 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22651
22652 gdb_assert (per_cu->is_debug_types);
22653 gdb_assert (per_cu->cu == NULL);
22654
22655 cutu_reader reader (per_cu, NULL, 0, false);
22656
22657 if (!reader.dummy_p)
22658 {
22659 struct dwarf2_cu *cu = reader.cu;
22660 const gdb_byte *info_ptr = reader.info_ptr;
22661
22662 gdb_assert (cu->die_hash == NULL);
22663 cu->die_hash =
22664 htab_create_alloc_ex (cu->header.length / 12,
22665 die_hash,
22666 die_eq,
22667 NULL,
22668 &cu->comp_unit_obstack,
22669 hashtab_obstack_allocate,
22670 dummy_obstack_deallocate);
22671
22672 if (reader.comp_unit_die->has_children)
22673 reader.comp_unit_die->child
22674 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22675 reader.comp_unit_die);
22676 cu->dies = reader.comp_unit_die;
22677 /* comp_unit_die is not stored in die_hash, no need. */
22678
22679 /* We try not to read any attributes in this function, because
22680 not all CUs needed for references have been loaded yet, and
22681 symbol table processing isn't initialized. But we have to
22682 set the CU language, or we won't be able to build types
22683 correctly. Similarly, if we do not read the producer, we can
22684 not apply producer-specific interpretation. */
22685 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22686
22687 reader.keep ();
22688 }
22689
22690 sig_type->per_cu.tu_read = 1;
22691 }
22692
22693 /* Decode simple location descriptions.
22694 Given a pointer to a dwarf block that defines a location, compute
22695 the location and return the value. If COMPUTED is non-null, it is
22696 set to true to indicate that decoding was successful, and false
22697 otherwise. If COMPUTED is null, then this function may emit a
22698 complaint. */
22699
22700 static CORE_ADDR
22701 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22702 {
22703 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22704 size_t i;
22705 size_t size = blk->size;
22706 const gdb_byte *data = blk->data;
22707 CORE_ADDR stack[64];
22708 int stacki;
22709 unsigned int bytes_read, unsnd;
22710 gdb_byte op;
22711
22712 if (computed != nullptr)
22713 *computed = false;
22714
22715 i = 0;
22716 stacki = 0;
22717 stack[stacki] = 0;
22718 stack[++stacki] = 0;
22719
22720 while (i < size)
22721 {
22722 op = data[i++];
22723 switch (op)
22724 {
22725 case DW_OP_lit0:
22726 case DW_OP_lit1:
22727 case DW_OP_lit2:
22728 case DW_OP_lit3:
22729 case DW_OP_lit4:
22730 case DW_OP_lit5:
22731 case DW_OP_lit6:
22732 case DW_OP_lit7:
22733 case DW_OP_lit8:
22734 case DW_OP_lit9:
22735 case DW_OP_lit10:
22736 case DW_OP_lit11:
22737 case DW_OP_lit12:
22738 case DW_OP_lit13:
22739 case DW_OP_lit14:
22740 case DW_OP_lit15:
22741 case DW_OP_lit16:
22742 case DW_OP_lit17:
22743 case DW_OP_lit18:
22744 case DW_OP_lit19:
22745 case DW_OP_lit20:
22746 case DW_OP_lit21:
22747 case DW_OP_lit22:
22748 case DW_OP_lit23:
22749 case DW_OP_lit24:
22750 case DW_OP_lit25:
22751 case DW_OP_lit26:
22752 case DW_OP_lit27:
22753 case DW_OP_lit28:
22754 case DW_OP_lit29:
22755 case DW_OP_lit30:
22756 case DW_OP_lit31:
22757 stack[++stacki] = op - DW_OP_lit0;
22758 break;
22759
22760 case DW_OP_reg0:
22761 case DW_OP_reg1:
22762 case DW_OP_reg2:
22763 case DW_OP_reg3:
22764 case DW_OP_reg4:
22765 case DW_OP_reg5:
22766 case DW_OP_reg6:
22767 case DW_OP_reg7:
22768 case DW_OP_reg8:
22769 case DW_OP_reg9:
22770 case DW_OP_reg10:
22771 case DW_OP_reg11:
22772 case DW_OP_reg12:
22773 case DW_OP_reg13:
22774 case DW_OP_reg14:
22775 case DW_OP_reg15:
22776 case DW_OP_reg16:
22777 case DW_OP_reg17:
22778 case DW_OP_reg18:
22779 case DW_OP_reg19:
22780 case DW_OP_reg20:
22781 case DW_OP_reg21:
22782 case DW_OP_reg22:
22783 case DW_OP_reg23:
22784 case DW_OP_reg24:
22785 case DW_OP_reg25:
22786 case DW_OP_reg26:
22787 case DW_OP_reg27:
22788 case DW_OP_reg28:
22789 case DW_OP_reg29:
22790 case DW_OP_reg30:
22791 case DW_OP_reg31:
22792 stack[++stacki] = op - DW_OP_reg0;
22793 if (i < size)
22794 {
22795 if (computed == nullptr)
22796 dwarf2_complex_location_expr_complaint ();
22797 else
22798 return 0;
22799 }
22800 break;
22801
22802 case DW_OP_regx:
22803 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22804 i += bytes_read;
22805 stack[++stacki] = unsnd;
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_addr:
22816 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22817 &bytes_read);
22818 i += bytes_read;
22819 break;
22820
22821 case DW_OP_const1u:
22822 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22823 i += 1;
22824 break;
22825
22826 case DW_OP_const1s:
22827 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22828 i += 1;
22829 break;
22830
22831 case DW_OP_const2u:
22832 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22833 i += 2;
22834 break;
22835
22836 case DW_OP_const2s:
22837 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22838 i += 2;
22839 break;
22840
22841 case DW_OP_const4u:
22842 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22843 i += 4;
22844 break;
22845
22846 case DW_OP_const4s:
22847 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22848 i += 4;
22849 break;
22850
22851 case DW_OP_const8u:
22852 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22853 i += 8;
22854 break;
22855
22856 case DW_OP_constu:
22857 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22858 &bytes_read);
22859 i += bytes_read;
22860 break;
22861
22862 case DW_OP_consts:
22863 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22864 i += bytes_read;
22865 break;
22866
22867 case DW_OP_dup:
22868 stack[stacki + 1] = stack[stacki];
22869 stacki++;
22870 break;
22871
22872 case DW_OP_plus:
22873 stack[stacki - 1] += stack[stacki];
22874 stacki--;
22875 break;
22876
22877 case DW_OP_plus_uconst:
22878 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22879 &bytes_read);
22880 i += bytes_read;
22881 break;
22882
22883 case DW_OP_minus:
22884 stack[stacki - 1] -= stack[stacki];
22885 stacki--;
22886 break;
22887
22888 case DW_OP_deref:
22889 /* If we're not the last op, then we definitely can't encode
22890 this using GDB's address_class enum. This is valid for partial
22891 global symbols, although the variable's address will be bogus
22892 in the psymtab. */
22893 if (i < size)
22894 {
22895 if (computed == nullptr)
22896 dwarf2_complex_location_expr_complaint ();
22897 else
22898 return 0;
22899 }
22900 break;
22901
22902 case DW_OP_GNU_push_tls_address:
22903 case DW_OP_form_tls_address:
22904 /* The top of the stack has the offset from the beginning
22905 of the thread control block at which the variable is located. */
22906 /* Nothing should follow this operator, so the top of stack would
22907 be returned. */
22908 /* This is valid for partial global symbols, but the variable's
22909 address will be bogus in the psymtab. Make it always at least
22910 non-zero to not look as a variable garbage collected by linker
22911 which have DW_OP_addr 0. */
22912 if (i < size)
22913 {
22914 if (computed == nullptr)
22915 dwarf2_complex_location_expr_complaint ();
22916 else
22917 return 0;
22918 }
22919 stack[stacki]++;
22920 break;
22921
22922 case DW_OP_GNU_uninit:
22923 if (computed != nullptr)
22924 return 0;
22925 break;
22926
22927 case DW_OP_addrx:
22928 case DW_OP_GNU_addr_index:
22929 case DW_OP_GNU_const_index:
22930 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22931 &bytes_read);
22932 i += bytes_read;
22933 break;
22934
22935 default:
22936 if (computed == nullptr)
22937 {
22938 const char *name = get_DW_OP_name (op);
22939
22940 if (name)
22941 complaint (_("unsupported stack op: '%s'"),
22942 name);
22943 else
22944 complaint (_("unsupported stack op: '%02x'"),
22945 op);
22946 }
22947
22948 return (stack[stacki]);
22949 }
22950
22951 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22952 outside of the allocated space. Also enforce minimum>0. */
22953 if (stacki >= ARRAY_SIZE (stack) - 1)
22954 {
22955 if (computed == nullptr)
22956 complaint (_("location description stack overflow"));
22957 return 0;
22958 }
22959
22960 if (stacki <= 0)
22961 {
22962 if (computed == nullptr)
22963 complaint (_("location description stack underflow"));
22964 return 0;
22965 }
22966 }
22967
22968 if (computed != nullptr)
22969 *computed = true;
22970 return (stack[stacki]);
22971 }
22972
22973 /* memory allocation interface */
22974
22975 static struct dwarf_block *
22976 dwarf_alloc_block (struct dwarf2_cu *cu)
22977 {
22978 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
22979 }
22980
22981 static struct die_info *
22982 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
22983 {
22984 struct die_info *die;
22985 size_t size = sizeof (struct die_info);
22986
22987 if (num_attrs > 1)
22988 size += (num_attrs - 1) * sizeof (struct attribute);
22989
22990 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
22991 memset (die, 0, sizeof (struct die_info));
22992 return (die);
22993 }
22994
22995 \f
22996
22997 /* Macro support. */
22998
22999 /* An overload of dwarf_decode_macros that finds the correct section
23000 and ensures it is read in before calling the other overload. */
23001
23002 static void
23003 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23004 int section_is_gnu)
23005 {
23006 struct dwarf2_per_objfile *dwarf2_per_objfile
23007 = cu->per_cu->dwarf2_per_objfile;
23008 struct objfile *objfile = dwarf2_per_objfile->objfile;
23009 const struct line_header *lh = cu->line_header;
23010 unsigned int offset_size = cu->header.offset_size;
23011 struct dwarf2_section_info *section;
23012 const char *section_name;
23013
23014 if (cu->dwo_unit != nullptr)
23015 {
23016 if (section_is_gnu)
23017 {
23018 section = &cu->dwo_unit->dwo_file->sections.macro;
23019 section_name = ".debug_macro.dwo";
23020 }
23021 else
23022 {
23023 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23024 section_name = ".debug_macinfo.dwo";
23025 }
23026 }
23027 else
23028 {
23029 if (section_is_gnu)
23030 {
23031 section = &dwarf2_per_objfile->macro;
23032 section_name = ".debug_macro";
23033 }
23034 else
23035 {
23036 section = &dwarf2_per_objfile->macinfo;
23037 section_name = ".debug_macinfo";
23038 }
23039 }
23040
23041 section->read (objfile);
23042 if (section->buffer == nullptr)
23043 {
23044 complaint (_("missing %s section"), section_name);
23045 return;
23046 }
23047
23048 buildsym_compunit *builder = cu->get_builder ();
23049
23050 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23051 offset_size, offset, section_is_gnu);
23052 }
23053
23054 /* Return the .debug_loc section to use for CU.
23055 For DWO files use .debug_loc.dwo. */
23056
23057 static struct dwarf2_section_info *
23058 cu_debug_loc_section (struct dwarf2_cu *cu)
23059 {
23060 struct dwarf2_per_objfile *dwarf2_per_objfile
23061 = cu->per_cu->dwarf2_per_objfile;
23062
23063 if (cu->dwo_unit)
23064 {
23065 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23066
23067 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23068 }
23069 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23070 : &dwarf2_per_objfile->loc);
23071 }
23072
23073 /* A helper function that fills in a dwarf2_loclist_baton. */
23074
23075 static void
23076 fill_in_loclist_baton (struct dwarf2_cu *cu,
23077 struct dwarf2_loclist_baton *baton,
23078 const struct attribute *attr)
23079 {
23080 struct dwarf2_per_objfile *dwarf2_per_objfile
23081 = cu->per_cu->dwarf2_per_objfile;
23082 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23083
23084 section->read (dwarf2_per_objfile->objfile);
23085
23086 baton->per_cu = cu->per_cu;
23087 gdb_assert (baton->per_cu);
23088 /* We don't know how long the location list is, but make sure we
23089 don't run off the edge of the section. */
23090 baton->size = section->size - DW_UNSND (attr);
23091 baton->data = section->buffer + DW_UNSND (attr);
23092 if (cu->base_address.has_value ())
23093 baton->base_address = *cu->base_address;
23094 else
23095 baton->base_address = 0;
23096 baton->from_dwo = cu->dwo_unit != NULL;
23097 }
23098
23099 static void
23100 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23101 struct dwarf2_cu *cu, int is_block)
23102 {
23103 struct dwarf2_per_objfile *dwarf2_per_objfile
23104 = cu->per_cu->dwarf2_per_objfile;
23105 struct objfile *objfile = dwarf2_per_objfile->objfile;
23106 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23107
23108 if (attr->form_is_section_offset ()
23109 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23110 the section. If so, fall through to the complaint in the
23111 other branch. */
23112 && DW_UNSND (attr) < section->get_size (objfile))
23113 {
23114 struct dwarf2_loclist_baton *baton;
23115
23116 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23117
23118 fill_in_loclist_baton (cu, baton, attr);
23119
23120 if (!cu->base_address.has_value ())
23121 complaint (_("Location list used without "
23122 "specifying the CU base address."));
23123
23124 SYMBOL_ACLASS_INDEX (sym) = (is_block
23125 ? dwarf2_loclist_block_index
23126 : dwarf2_loclist_index);
23127 SYMBOL_LOCATION_BATON (sym) = baton;
23128 }
23129 else
23130 {
23131 struct dwarf2_locexpr_baton *baton;
23132
23133 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23134 baton->per_cu = cu->per_cu;
23135 gdb_assert (baton->per_cu);
23136
23137 if (attr->form_is_block ())
23138 {
23139 /* Note that we're just copying the block's data pointer
23140 here, not the actual data. We're still pointing into the
23141 info_buffer for SYM's objfile; right now we never release
23142 that buffer, but when we do clean up properly this may
23143 need to change. */
23144 baton->size = DW_BLOCK (attr)->size;
23145 baton->data = DW_BLOCK (attr)->data;
23146 }
23147 else
23148 {
23149 dwarf2_invalid_attrib_class_complaint ("location description",
23150 sym->natural_name ());
23151 baton->size = 0;
23152 }
23153
23154 SYMBOL_ACLASS_INDEX (sym) = (is_block
23155 ? dwarf2_locexpr_block_index
23156 : dwarf2_locexpr_index);
23157 SYMBOL_LOCATION_BATON (sym) = baton;
23158 }
23159 }
23160
23161 /* See read.h. */
23162
23163 struct objfile *
23164 dwarf2_per_cu_data::objfile () const
23165 {
23166 struct objfile *objfile = dwarf2_per_objfile->objfile;
23167
23168 /* Return the master objfile, so that we can report and look up the
23169 correct file containing this variable. */
23170 if (objfile->separate_debug_objfile_backlink)
23171 objfile = objfile->separate_debug_objfile_backlink;
23172
23173 return objfile;
23174 }
23175
23176 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23177 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23178 CU_HEADERP first. */
23179
23180 static const struct comp_unit_head *
23181 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23182 const struct dwarf2_per_cu_data *per_cu)
23183 {
23184 const gdb_byte *info_ptr;
23185
23186 if (per_cu->cu)
23187 return &per_cu->cu->header;
23188
23189 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
23190
23191 memset (cu_headerp, 0, sizeof (*cu_headerp));
23192 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23193 rcuh_kind::COMPILE);
23194
23195 return cu_headerp;
23196 }
23197
23198 /* See read.h. */
23199
23200 int
23201 dwarf2_per_cu_data::addr_size () const
23202 {
23203 struct comp_unit_head cu_header_local;
23204 const struct comp_unit_head *cu_headerp;
23205
23206 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23207
23208 return cu_headerp->addr_size;
23209 }
23210
23211 /* See read.h. */
23212
23213 int
23214 dwarf2_per_cu_data::offset_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->offset_size;
23222 }
23223
23224 /* See read.h. */
23225
23226 int
23227 dwarf2_per_cu_data::ref_addr_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 if (cu_headerp->version == 2)
23235 return cu_headerp->addr_size;
23236 else
23237 return cu_headerp->offset_size;
23238 }
23239
23240 /* See read.h. */
23241
23242 CORE_ADDR
23243 dwarf2_per_cu_data::text_offset () const
23244 {
23245 struct objfile *objfile = dwarf2_per_objfile->objfile;
23246
23247 return objfile->text_section_offset ();
23248 }
23249
23250 /* See read.h. */
23251
23252 struct type *
23253 dwarf2_per_cu_data::addr_type () const
23254 {
23255 struct objfile *objfile = dwarf2_per_objfile->objfile;
23256 struct type *void_type = objfile_type (objfile)->builtin_void;
23257 struct type *addr_type = lookup_pointer_type (void_type);
23258 int addr_size = this->addr_size ();
23259
23260 if (TYPE_LENGTH (addr_type) == addr_size)
23261 return addr_type;
23262
23263 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23264 return addr_type;
23265 }
23266
23267 /* A helper function for dwarf2_find_containing_comp_unit that returns
23268 the index of the result, and that searches a vector. It will
23269 return a result even if the offset in question does not actually
23270 occur in any CU. This is separate so that it can be unit
23271 tested. */
23272
23273 static int
23274 dwarf2_find_containing_comp_unit
23275 (sect_offset sect_off,
23276 unsigned int offset_in_dwz,
23277 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23278 {
23279 int low, high;
23280
23281 low = 0;
23282 high = all_comp_units.size () - 1;
23283 while (high > low)
23284 {
23285 struct dwarf2_per_cu_data *mid_cu;
23286 int mid = low + (high - low) / 2;
23287
23288 mid_cu = all_comp_units[mid];
23289 if (mid_cu->is_dwz > offset_in_dwz
23290 || (mid_cu->is_dwz == offset_in_dwz
23291 && mid_cu->sect_off + mid_cu->length > sect_off))
23292 high = mid;
23293 else
23294 low = mid + 1;
23295 }
23296 gdb_assert (low == high);
23297 return low;
23298 }
23299
23300 /* Locate the .debug_info compilation unit from CU's objfile which contains
23301 the DIE at OFFSET. Raises an error on failure. */
23302
23303 static struct dwarf2_per_cu_data *
23304 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23305 unsigned int offset_in_dwz,
23306 struct dwarf2_per_objfile *dwarf2_per_objfile)
23307 {
23308 int low
23309 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23310 dwarf2_per_objfile->all_comp_units);
23311 struct dwarf2_per_cu_data *this_cu
23312 = dwarf2_per_objfile->all_comp_units[low];
23313
23314 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23315 {
23316 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23317 error (_("Dwarf Error: could not find partial DIE containing "
23318 "offset %s [in module %s]"),
23319 sect_offset_str (sect_off),
23320 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23321
23322 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
23323 <= sect_off);
23324 return dwarf2_per_objfile->all_comp_units[low-1];
23325 }
23326 else
23327 {
23328 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
23329 && sect_off >= this_cu->sect_off + this_cu->length)
23330 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23331 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23332 return this_cu;
23333 }
23334 }
23335
23336 #if GDB_SELF_TEST
23337
23338 namespace selftests {
23339 namespace find_containing_comp_unit {
23340
23341 static void
23342 run_test ()
23343 {
23344 struct dwarf2_per_cu_data one {};
23345 struct dwarf2_per_cu_data two {};
23346 struct dwarf2_per_cu_data three {};
23347 struct dwarf2_per_cu_data four {};
23348
23349 one.length = 5;
23350 two.sect_off = sect_offset (one.length);
23351 two.length = 7;
23352
23353 three.length = 5;
23354 three.is_dwz = 1;
23355 four.sect_off = sect_offset (three.length);
23356 four.length = 7;
23357 four.is_dwz = 1;
23358
23359 std::vector<dwarf2_per_cu_data *> units;
23360 units.push_back (&one);
23361 units.push_back (&two);
23362 units.push_back (&three);
23363 units.push_back (&four);
23364
23365 int result;
23366
23367 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23368 SELF_CHECK (units[result] == &one);
23369 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23370 SELF_CHECK (units[result] == &one);
23371 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23372 SELF_CHECK (units[result] == &two);
23373
23374 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23375 SELF_CHECK (units[result] == &three);
23376 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23377 SELF_CHECK (units[result] == &three);
23378 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23379 SELF_CHECK (units[result] == &four);
23380 }
23381
23382 }
23383 }
23384
23385 #endif /* GDB_SELF_TEST */
23386
23387 /* Initialize dwarf2_cu CU, owned by PER_CU. */
23388
23389 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
23390 : per_cu (per_cu_),
23391 mark (false),
23392 has_loclist (false),
23393 checked_producer (false),
23394 producer_is_gxx_lt_4_6 (false),
23395 producer_is_gcc_lt_4_3 (false),
23396 producer_is_icc (false),
23397 producer_is_icc_lt_14 (false),
23398 producer_is_codewarrior (false),
23399 processing_has_namespace_info (false)
23400 {
23401 per_cu->cu = this;
23402 }
23403
23404 /* Destroy a dwarf2_cu. */
23405
23406 dwarf2_cu::~dwarf2_cu ()
23407 {
23408 per_cu->cu = NULL;
23409 }
23410
23411 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23412
23413 static void
23414 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23415 enum language pretend_language)
23416 {
23417 struct attribute *attr;
23418
23419 /* Set the language we're debugging. */
23420 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23421 if (attr != nullptr)
23422 set_cu_language (DW_UNSND (attr), cu);
23423 else
23424 {
23425 cu->language = pretend_language;
23426 cu->language_defn = language_def (cu->language);
23427 }
23428
23429 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23430 }
23431
23432 /* Increase the age counter on each cached compilation unit, and free
23433 any that are too old. */
23434
23435 static void
23436 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23437 {
23438 struct dwarf2_per_cu_data *per_cu, **last_chain;
23439
23440 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23441 per_cu = dwarf2_per_objfile->read_in_chain;
23442 while (per_cu != NULL)
23443 {
23444 per_cu->cu->last_used ++;
23445 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23446 dwarf2_mark (per_cu->cu);
23447 per_cu = per_cu->cu->read_in_chain;
23448 }
23449
23450 per_cu = dwarf2_per_objfile->read_in_chain;
23451 last_chain = &dwarf2_per_objfile->read_in_chain;
23452 while (per_cu != NULL)
23453 {
23454 struct dwarf2_per_cu_data *next_cu;
23455
23456 next_cu = per_cu->cu->read_in_chain;
23457
23458 if (!per_cu->cu->mark)
23459 {
23460 delete per_cu->cu;
23461 *last_chain = next_cu;
23462 }
23463 else
23464 last_chain = &per_cu->cu->read_in_chain;
23465
23466 per_cu = next_cu;
23467 }
23468 }
23469
23470 /* Remove a single compilation unit from the cache. */
23471
23472 static void
23473 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23474 {
23475 struct dwarf2_per_cu_data *per_cu, **last_chain;
23476 struct dwarf2_per_objfile *dwarf2_per_objfile
23477 = target_per_cu->dwarf2_per_objfile;
23478
23479 per_cu = dwarf2_per_objfile->read_in_chain;
23480 last_chain = &dwarf2_per_objfile->read_in_chain;
23481 while (per_cu != NULL)
23482 {
23483 struct dwarf2_per_cu_data *next_cu;
23484
23485 next_cu = per_cu->cu->read_in_chain;
23486
23487 if (per_cu == target_per_cu)
23488 {
23489 delete per_cu->cu;
23490 per_cu->cu = NULL;
23491 *last_chain = next_cu;
23492 break;
23493 }
23494 else
23495 last_chain = &per_cu->cu->read_in_chain;
23496
23497 per_cu = next_cu;
23498 }
23499 }
23500
23501 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23502 We store these in a hash table separate from the DIEs, and preserve them
23503 when the DIEs are flushed out of cache.
23504
23505 The CU "per_cu" pointer is needed because offset alone is not enough to
23506 uniquely identify the type. A file may have multiple .debug_types sections,
23507 or the type may come from a DWO file. Furthermore, while it's more logical
23508 to use per_cu->section+offset, with Fission the section with the data is in
23509 the DWO file but we don't know that section at the point we need it.
23510 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23511 because we can enter the lookup routine, get_die_type_at_offset, from
23512 outside this file, and thus won't necessarily have PER_CU->cu.
23513 Fortunately, PER_CU is stable for the life of the objfile. */
23514
23515 struct dwarf2_per_cu_offset_and_type
23516 {
23517 const struct dwarf2_per_cu_data *per_cu;
23518 sect_offset sect_off;
23519 struct type *type;
23520 };
23521
23522 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23523
23524 static hashval_t
23525 per_cu_offset_and_type_hash (const void *item)
23526 {
23527 const struct dwarf2_per_cu_offset_and_type *ofs
23528 = (const struct dwarf2_per_cu_offset_and_type *) item;
23529
23530 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23531 }
23532
23533 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23534
23535 static int
23536 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23537 {
23538 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23539 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23540 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23541 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23542
23543 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23544 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23545 }
23546
23547 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23548 table if necessary. For convenience, return TYPE.
23549
23550 The DIEs reading must have careful ordering to:
23551 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23552 reading current DIE.
23553 * Not trying to dereference contents of still incompletely read in types
23554 while reading in other DIEs.
23555 * Enable referencing still incompletely read in types just by a pointer to
23556 the type without accessing its fields.
23557
23558 Therefore caller should follow these rules:
23559 * Try to fetch any prerequisite types we may need to build this DIE type
23560 before building the type and calling set_die_type.
23561 * After building type call set_die_type for current DIE as soon as
23562 possible before fetching more types to complete the current type.
23563 * Make the type as complete as possible before fetching more types. */
23564
23565 static struct type *
23566 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23567 {
23568 struct dwarf2_per_objfile *dwarf2_per_objfile
23569 = cu->per_cu->dwarf2_per_objfile;
23570 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23571 struct objfile *objfile = dwarf2_per_objfile->objfile;
23572 struct attribute *attr;
23573 struct dynamic_prop prop;
23574
23575 /* For Ada types, make sure that the gnat-specific data is always
23576 initialized (if not already set). There are a few types where
23577 we should not be doing so, because the type-specific area is
23578 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23579 where the type-specific area is used to store the floatformat).
23580 But this is not a problem, because the gnat-specific information
23581 is actually not needed for these types. */
23582 if (need_gnat_info (cu)
23583 && TYPE_CODE (type) != TYPE_CODE_FUNC
23584 && TYPE_CODE (type) != TYPE_CODE_FLT
23585 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23586 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23587 && TYPE_CODE (type) != TYPE_CODE_METHOD
23588 && !HAVE_GNAT_AUX_INFO (type))
23589 INIT_GNAT_SPECIFIC (type);
23590
23591 /* Read DW_AT_allocated and set in type. */
23592 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23593 if (attr != NULL && attr->form_is_block ())
23594 {
23595 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23596 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23597 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
23598 }
23599 else if (attr != NULL)
23600 {
23601 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23602 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23603 sect_offset_str (die->sect_off));
23604 }
23605
23606 /* Read DW_AT_associated and set in type. */
23607 attr = dwarf2_attr (die, DW_AT_associated, cu);
23608 if (attr != NULL && attr->form_is_block ())
23609 {
23610 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23611 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23612 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
23613 }
23614 else if (attr != NULL)
23615 {
23616 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23617 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23618 sect_offset_str (die->sect_off));
23619 }
23620
23621 /* Read DW_AT_data_location and set in type. */
23622 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23623 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23624 cu->per_cu->addr_type ()))
23625 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
23626
23627 if (dwarf2_per_objfile->die_type_hash == NULL)
23628 dwarf2_per_objfile->die_type_hash
23629 = htab_up (htab_create_alloc (127,
23630 per_cu_offset_and_type_hash,
23631 per_cu_offset_and_type_eq,
23632 NULL, xcalloc, xfree));
23633
23634 ofs.per_cu = cu->per_cu;
23635 ofs.sect_off = die->sect_off;
23636 ofs.type = type;
23637 slot = (struct dwarf2_per_cu_offset_and_type **)
23638 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23639 if (*slot)
23640 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23641 sect_offset_str (die->sect_off));
23642 *slot = XOBNEW (&objfile->objfile_obstack,
23643 struct dwarf2_per_cu_offset_and_type);
23644 **slot = ofs;
23645 return type;
23646 }
23647
23648 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23649 or return NULL if the die does not have a saved type. */
23650
23651 static struct type *
23652 get_die_type_at_offset (sect_offset sect_off,
23653 struct dwarf2_per_cu_data *per_cu)
23654 {
23655 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23656 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23657
23658 if (dwarf2_per_objfile->die_type_hash == NULL)
23659 return NULL;
23660
23661 ofs.per_cu = per_cu;
23662 ofs.sect_off = sect_off;
23663 slot = ((struct dwarf2_per_cu_offset_and_type *)
23664 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23665 if (slot)
23666 return slot->type;
23667 else
23668 return NULL;
23669 }
23670
23671 /* Look up the type for DIE in CU in die_type_hash,
23672 or return NULL if DIE does not have a saved type. */
23673
23674 static struct type *
23675 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23676 {
23677 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23678 }
23679
23680 /* Add a dependence relationship from CU to REF_PER_CU. */
23681
23682 static void
23683 dwarf2_add_dependence (struct dwarf2_cu *cu,
23684 struct dwarf2_per_cu_data *ref_per_cu)
23685 {
23686 void **slot;
23687
23688 if (cu->dependencies == NULL)
23689 cu->dependencies
23690 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23691 NULL, &cu->comp_unit_obstack,
23692 hashtab_obstack_allocate,
23693 dummy_obstack_deallocate);
23694
23695 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23696 if (*slot == NULL)
23697 *slot = ref_per_cu;
23698 }
23699
23700 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23701 Set the mark field in every compilation unit in the
23702 cache that we must keep because we are keeping CU. */
23703
23704 static int
23705 dwarf2_mark_helper (void **slot, void *data)
23706 {
23707 struct dwarf2_per_cu_data *per_cu;
23708
23709 per_cu = (struct dwarf2_per_cu_data *) *slot;
23710
23711 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23712 reading of the chain. As such dependencies remain valid it is not much
23713 useful to track and undo them during QUIT cleanups. */
23714 if (per_cu->cu == NULL)
23715 return 1;
23716
23717 if (per_cu->cu->mark)
23718 return 1;
23719 per_cu->cu->mark = true;
23720
23721 if (per_cu->cu->dependencies != NULL)
23722 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23723
23724 return 1;
23725 }
23726
23727 /* Set the mark field in CU and in every other compilation unit in the
23728 cache that we must keep because we are keeping CU. */
23729
23730 static void
23731 dwarf2_mark (struct dwarf2_cu *cu)
23732 {
23733 if (cu->mark)
23734 return;
23735 cu->mark = true;
23736 if (cu->dependencies != NULL)
23737 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23738 }
23739
23740 static void
23741 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23742 {
23743 while (per_cu)
23744 {
23745 per_cu->cu->mark = false;
23746 per_cu = per_cu->cu->read_in_chain;
23747 }
23748 }
23749
23750 /* Trivial hash function for partial_die_info: the hash value of a DIE
23751 is its offset in .debug_info for this objfile. */
23752
23753 static hashval_t
23754 partial_die_hash (const void *item)
23755 {
23756 const struct partial_die_info *part_die
23757 = (const struct partial_die_info *) item;
23758
23759 return to_underlying (part_die->sect_off);
23760 }
23761
23762 /* Trivial comparison function for partial_die_info structures: two DIEs
23763 are equal if they have the same offset. */
23764
23765 static int
23766 partial_die_eq (const void *item_lhs, const void *item_rhs)
23767 {
23768 const struct partial_die_info *part_die_lhs
23769 = (const struct partial_die_info *) item_lhs;
23770 const struct partial_die_info *part_die_rhs
23771 = (const struct partial_die_info *) item_rhs;
23772
23773 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23774 }
23775
23776 struct cmd_list_element *set_dwarf_cmdlist;
23777 struct cmd_list_element *show_dwarf_cmdlist;
23778
23779 static void
23780 show_check_physname (struct ui_file *file, int from_tty,
23781 struct cmd_list_element *c, const char *value)
23782 {
23783 fprintf_filtered (file,
23784 _("Whether to check \"physname\" is %s.\n"),
23785 value);
23786 }
23787
23788 void _initialize_dwarf2_read ();
23789 void
23790 _initialize_dwarf2_read ()
23791 {
23792 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
23793 Set DWARF specific variables.\n\
23794 Configure DWARF variables such as the cache size."),
23795 &set_dwarf_cmdlist, "maintenance set dwarf ",
23796 0/*allow-unknown*/, &maintenance_set_cmdlist);
23797
23798 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
23799 Show DWARF specific variables.\n\
23800 Show DWARF variables such as the cache size."),
23801 &show_dwarf_cmdlist, "maintenance show dwarf ",
23802 0/*allow-unknown*/, &maintenance_show_cmdlist);
23803
23804 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23805 &dwarf_max_cache_age, _("\
23806 Set the upper bound on the age of cached DWARF compilation units."), _("\
23807 Show the upper bound on the age of cached DWARF compilation units."), _("\
23808 A higher limit means that cached compilation units will be stored\n\
23809 in memory longer, and more total memory will be used. Zero disables\n\
23810 caching, which can slow down startup."),
23811 NULL,
23812 show_dwarf_max_cache_age,
23813 &set_dwarf_cmdlist,
23814 &show_dwarf_cmdlist);
23815
23816 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23817 Set debugging of the DWARF reader."), _("\
23818 Show debugging of the DWARF reader."), _("\
23819 When enabled (non-zero), debugging messages are printed during DWARF\n\
23820 reading and symtab expansion. A value of 1 (one) provides basic\n\
23821 information. A value greater than 1 provides more verbose information."),
23822 NULL,
23823 NULL,
23824 &setdebuglist, &showdebuglist);
23825
23826 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23827 Set debugging of the DWARF DIE reader."), _("\
23828 Show debugging of the DWARF DIE reader."), _("\
23829 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23830 The value is the maximum depth to print."),
23831 NULL,
23832 NULL,
23833 &setdebuglist, &showdebuglist);
23834
23835 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23836 Set debugging of the dwarf line reader."), _("\
23837 Show debugging of the dwarf line reader."), _("\
23838 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23839 A value of 1 (one) provides basic information.\n\
23840 A value greater than 1 provides more verbose information."),
23841 NULL,
23842 NULL,
23843 &setdebuglist, &showdebuglist);
23844
23845 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23846 Set cross-checking of \"physname\" code against demangler."), _("\
23847 Show cross-checking of \"physname\" code against demangler."), _("\
23848 When enabled, GDB's internal \"physname\" code is checked against\n\
23849 the demangler."),
23850 NULL, show_check_physname,
23851 &setdebuglist, &showdebuglist);
23852
23853 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23854 no_class, &use_deprecated_index_sections, _("\
23855 Set whether to use deprecated gdb_index sections."), _("\
23856 Show whether to use deprecated gdb_index sections."), _("\
23857 When enabled, deprecated .gdb_index sections are used anyway.\n\
23858 Normally they are ignored either because of a missing feature or\n\
23859 performance issue.\n\
23860 Warning: This option must be enabled before gdb reads the file."),
23861 NULL,
23862 NULL,
23863 &setlist, &showlist);
23864
23865 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23866 &dwarf2_locexpr_funcs);
23867 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23868 &dwarf2_loclist_funcs);
23869
23870 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23871 &dwarf2_block_frame_base_locexpr_funcs);
23872 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23873 &dwarf2_block_frame_base_loclist_funcs);
23874
23875 #if GDB_SELF_TEST
23876 selftests::register_test ("dw2_expand_symtabs_matching",
23877 selftests::dw2_expand_symtabs_matching::run_test);
23878 selftests::register_test ("dwarf2_find_containing_comp_unit",
23879 selftests::find_containing_comp_unit::run_test);
23880 #endif
23881 }
This page took 0.489504 seconds and 3 git commands to generate.