Support for DW_AT_loclists_base and DW_FORM_loclistx.
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "dwarf2/stringify.h"
44 #include "bfd.h"
45 #include "elf-bfd.h"
46 #include "symtab.h"
47 #include "gdbtypes.h"
48 #include "objfiles.h"
49 #include "dwarf2.h"
50 #include "buildsym.h"
51 #include "demangle.h"
52 #include "gdb-demangle.h"
53 #include "filenames.h" /* for DOSish file names */
54 #include "language.h"
55 #include "complaints.h"
56 #include "dwarf2/expr.h"
57 #include "dwarf2/loc.h"
58 #include "cp-support.h"
59 #include "hashtab.h"
60 #include "command.h"
61 #include "gdbcmd.h"
62 #include "block.h"
63 #include "addrmap.h"
64 #include "typeprint.h"
65 #include "psympriv.h"
66 #include "c-lang.h"
67 #include "go-lang.h"
68 #include "valprint.h"
69 #include "gdbcore.h" /* for gnutarget */
70 #include "gdb/gdb-index.h"
71 #include "gdb_bfd.h"
72 #include "f-lang.h"
73 #include "source.h"
74 #include "build-id.h"
75 #include "namespace.h"
76 #include "gdbsupport/function-view.h"
77 #include "gdbsupport/gdb_optional.h"
78 #include "gdbsupport/underlying.h"
79 #include "gdbsupport/hash_enum.h"
80 #include "filename-seen-cache.h"
81 #include "producer.h"
82 #include <fcntl.h>
83 #include <algorithm>
84 #include <unordered_map>
85 #include "gdbsupport/selftest.h"
86 #include "rust-lang.h"
87 #include "gdbsupport/pathstuff.h"
88 #include "count-one-bits.h"
89 #include "debuginfod-support.h"
90
91 /* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94 static unsigned int dwarf_read_debug = 0;
95
96 /* When non-zero, dump DIEs after they are read in. */
97 static unsigned int dwarf_die_debug = 0;
98
99 /* When non-zero, dump line number entries as they are read in. */
100 unsigned int dwarf_line_debug = 0;
101
102 /* When true, cross-check physname against demangler. */
103 static bool check_physname = false;
104
105 /* When true, do not reject deprecated .gdb_index sections. */
106 static bool use_deprecated_index_sections = false;
107
108 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
109
110 /* The "aclass" indices for various kinds of computed DWARF symbols. */
111
112 static int dwarf2_locexpr_index;
113 static int dwarf2_loclist_index;
114 static int dwarf2_locexpr_block_index;
115 static int dwarf2_loclist_block_index;
116
117 /* Size of .debug_loclists section header for 32-bit DWARF format. */
118 #define LOCLIST_HEADER_SIZE32 12
119
120 /* Size of .debug_loclists section header for 64-bit DWARF format. */
121 #define LOCLIST_HEADER_SIZE64 20
122
123 /* An index into a (C++) symbol name component in a symbol name as
124 recorded in the mapped_index's symbol table. For each C++ symbol
125 in the symbol table, we record one entry for the start of each
126 component in the symbol in a table of name components, and then
127 sort the table, in order to be able to binary search symbol names,
128 ignoring leading namespaces, both completion and regular look up.
129 For example, for symbol "A::B::C", we'll have an entry that points
130 to "A::B::C", another that points to "B::C", and another for "C".
131 Note that function symbols in GDB index have no parameter
132 information, just the function/method names. You can convert a
133 name_component to a "const char *" using the
134 'mapped_index::symbol_name_at(offset_type)' method. */
135
136 struct name_component
137 {
138 /* Offset in the symbol name where the component starts. Stored as
139 a (32-bit) offset instead of a pointer to save memory and improve
140 locality on 64-bit architectures. */
141 offset_type name_offset;
142
143 /* The symbol's index in the symbol and constant pool tables of a
144 mapped_index. */
145 offset_type idx;
146 };
147
148 /* Base class containing bits shared by both .gdb_index and
149 .debug_name indexes. */
150
151 struct mapped_index_base
152 {
153 mapped_index_base () = default;
154 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
155
156 /* The name_component table (a sorted vector). See name_component's
157 description above. */
158 std::vector<name_component> name_components;
159
160 /* How NAME_COMPONENTS is sorted. */
161 enum case_sensitivity name_components_casing;
162
163 /* Return the number of names in the symbol table. */
164 virtual size_t symbol_name_count () const = 0;
165
166 /* Get the name of the symbol at IDX in the symbol table. */
167 virtual const char *symbol_name_at (offset_type idx) const = 0;
168
169 /* Return whether the name at IDX in the symbol table should be
170 ignored. */
171 virtual bool symbol_name_slot_invalid (offset_type idx) const
172 {
173 return false;
174 }
175
176 /* Build the symbol name component sorted vector, if we haven't
177 yet. */
178 void build_name_components ();
179
180 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
181 possible matches for LN_NO_PARAMS in the name component
182 vector. */
183 std::pair<std::vector<name_component>::const_iterator,
184 std::vector<name_component>::const_iterator>
185 find_name_components_bounds (const lookup_name_info &ln_no_params,
186 enum language lang) const;
187
188 /* Prevent deleting/destroying via a base class pointer. */
189 protected:
190 ~mapped_index_base() = default;
191 };
192
193 /* A description of the mapped index. The file format is described in
194 a comment by the code that writes the index. */
195 struct mapped_index final : public mapped_index_base
196 {
197 /* A slot/bucket in the symbol table hash. */
198 struct symbol_table_slot
199 {
200 const offset_type name;
201 const offset_type vec;
202 };
203
204 /* Index data format version. */
205 int version = 0;
206
207 /* The address table data. */
208 gdb::array_view<const gdb_byte> address_table;
209
210 /* The symbol table, implemented as a hash table. */
211 gdb::array_view<symbol_table_slot> symbol_table;
212
213 /* A pointer to the constant pool. */
214 const char *constant_pool = nullptr;
215
216 bool symbol_name_slot_invalid (offset_type idx) const override
217 {
218 const auto &bucket = this->symbol_table[idx];
219 return bucket.name == 0 && bucket.vec == 0;
220 }
221
222 /* Convenience method to get at the name of the symbol at IDX in the
223 symbol table. */
224 const char *symbol_name_at (offset_type idx) const override
225 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
226
227 size_t symbol_name_count () const override
228 { return this->symbol_table.size (); }
229 };
230
231 /* A description of the mapped .debug_names.
232 Uninitialized map has CU_COUNT 0. */
233 struct mapped_debug_names final : public mapped_index_base
234 {
235 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
236 : dwarf2_per_objfile (dwarf2_per_objfile_)
237 {}
238
239 struct dwarf2_per_objfile *dwarf2_per_objfile;
240 bfd_endian dwarf5_byte_order;
241 bool dwarf5_is_dwarf64;
242 bool augmentation_is_gdb;
243 uint8_t offset_size;
244 uint32_t cu_count = 0;
245 uint32_t tu_count, bucket_count, name_count;
246 const gdb_byte *cu_table_reordered, *tu_table_reordered;
247 const uint32_t *bucket_table_reordered, *hash_table_reordered;
248 const gdb_byte *name_table_string_offs_reordered;
249 const gdb_byte *name_table_entry_offs_reordered;
250 const gdb_byte *entry_pool;
251
252 struct index_val
253 {
254 ULONGEST dwarf_tag;
255 struct attr
256 {
257 /* Attribute name DW_IDX_*. */
258 ULONGEST dw_idx;
259
260 /* Attribute form DW_FORM_*. */
261 ULONGEST form;
262
263 /* Value if FORM is DW_FORM_implicit_const. */
264 LONGEST implicit_const;
265 };
266 std::vector<attr> attr_vec;
267 };
268
269 std::unordered_map<ULONGEST, index_val> abbrev_map;
270
271 const char *namei_to_name (uint32_t namei) const;
272
273 /* Implementation of the mapped_index_base virtual interface, for
274 the name_components cache. */
275
276 const char *symbol_name_at (offset_type idx) const override
277 { return namei_to_name (idx); }
278
279 size_t symbol_name_count () const override
280 { return this->name_count; }
281 };
282
283 /* See dwarf2read.h. */
284
285 dwarf2_per_objfile *
286 get_dwarf2_per_objfile (struct objfile *objfile)
287 {
288 return dwarf2_objfile_data_key.get (objfile);
289 }
290
291 /* Default names of the debugging sections. */
292
293 /* Note that if the debugging section has been compressed, it might
294 have a name like .zdebug_info. */
295
296 static const struct dwarf2_debug_sections dwarf2_elf_names =
297 {
298 { ".debug_info", ".zdebug_info" },
299 { ".debug_abbrev", ".zdebug_abbrev" },
300 { ".debug_line", ".zdebug_line" },
301 { ".debug_loc", ".zdebug_loc" },
302 { ".debug_loclists", ".zdebug_loclists" },
303 { ".debug_macinfo", ".zdebug_macinfo" },
304 { ".debug_macro", ".zdebug_macro" },
305 { ".debug_str", ".zdebug_str" },
306 { ".debug_str_offsets", ".zdebug_str_offsets" },
307 { ".debug_line_str", ".zdebug_line_str" },
308 { ".debug_ranges", ".zdebug_ranges" },
309 { ".debug_rnglists", ".zdebug_rnglists" },
310 { ".debug_types", ".zdebug_types" },
311 { ".debug_addr", ".zdebug_addr" },
312 { ".debug_frame", ".zdebug_frame" },
313 { ".eh_frame", NULL },
314 { ".gdb_index", ".zgdb_index" },
315 { ".debug_names", ".zdebug_names" },
316 { ".debug_aranges", ".zdebug_aranges" },
317 23
318 };
319
320 /* List of DWO/DWP sections. */
321
322 static const struct dwop_section_names
323 {
324 struct dwarf2_section_names abbrev_dwo;
325 struct dwarf2_section_names info_dwo;
326 struct dwarf2_section_names line_dwo;
327 struct dwarf2_section_names loc_dwo;
328 struct dwarf2_section_names loclists_dwo;
329 struct dwarf2_section_names macinfo_dwo;
330 struct dwarf2_section_names macro_dwo;
331 struct dwarf2_section_names str_dwo;
332 struct dwarf2_section_names str_offsets_dwo;
333 struct dwarf2_section_names types_dwo;
334 struct dwarf2_section_names cu_index;
335 struct dwarf2_section_names tu_index;
336 }
337 dwop_section_names =
338 {
339 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
340 { ".debug_info.dwo", ".zdebug_info.dwo" },
341 { ".debug_line.dwo", ".zdebug_line.dwo" },
342 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
343 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
344 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
345 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
346 { ".debug_str.dwo", ".zdebug_str.dwo" },
347 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
348 { ".debug_types.dwo", ".zdebug_types.dwo" },
349 { ".debug_cu_index", ".zdebug_cu_index" },
350 { ".debug_tu_index", ".zdebug_tu_index" },
351 };
352
353 /* local data types */
354
355 /* The location list section (.debug_loclists) begins with a header,
356 which contains the following information. */
357 struct loclist_header
358 {
359 /* A 4-byte or 12-byte length containing the length of the
360 set of entries for this compilation unit, not including the
361 length field itself. */
362 unsigned int length;
363
364 /* A 2-byte version identifier. */
365 short version;
366
367 /* A 1-byte unsigned integer containing the size in bytes of an address on
368 the target system. */
369 unsigned char addr_size;
370
371 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
372 on the target system. */
373 unsigned char segment_collector_size;
374
375 /* A 4-byte count of the number of offsets that follow the header. */
376 unsigned int offset_entry_count;
377 };
378
379 /* Type used for delaying computation of method physnames.
380 See comments for compute_delayed_physnames. */
381 struct delayed_method_info
382 {
383 /* The type to which the method is attached, i.e., its parent class. */
384 struct type *type;
385
386 /* The index of the method in the type's function fieldlists. */
387 int fnfield_index;
388
389 /* The index of the method in the fieldlist. */
390 int index;
391
392 /* The name of the DIE. */
393 const char *name;
394
395 /* The DIE associated with this method. */
396 struct die_info *die;
397 };
398
399 /* Internal state when decoding a particular compilation unit. */
400 struct dwarf2_cu
401 {
402 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
403 ~dwarf2_cu ();
404
405 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
406
407 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
408 Create the set of symtabs used by this TU, or if this TU is sharing
409 symtabs with another TU and the symtabs have already been created
410 then restore those symtabs in the line header.
411 We don't need the pc/line-number mapping for type units. */
412 void setup_type_unit_groups (struct die_info *die);
413
414 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
415 buildsym_compunit constructor. */
416 struct compunit_symtab *start_symtab (const char *name,
417 const char *comp_dir,
418 CORE_ADDR low_pc);
419
420 /* Reset the builder. */
421 void reset_builder () { m_builder.reset (); }
422
423 /* The header of the compilation unit. */
424 struct comp_unit_head header {};
425
426 /* Base address of this compilation unit. */
427 gdb::optional<CORE_ADDR> base_address;
428
429 /* The language we are debugging. */
430 enum language language = language_unknown;
431 const struct language_defn *language_defn = nullptr;
432
433 const char *producer = nullptr;
434
435 private:
436 /* The symtab builder for this CU. This is only non-NULL when full
437 symbols are being read. */
438 std::unique_ptr<buildsym_compunit> m_builder;
439
440 public:
441 /* The generic symbol table building routines have separate lists for
442 file scope symbols and all all other scopes (local scopes). So
443 we need to select the right one to pass to add_symbol_to_list().
444 We do it by keeping a pointer to the correct list in list_in_scope.
445
446 FIXME: The original dwarf code just treated the file scope as the
447 first local scope, and all other local scopes as nested local
448 scopes, and worked fine. Check to see if we really need to
449 distinguish these in buildsym.c. */
450 struct pending **list_in_scope = nullptr;
451
452 /* Hash table holding all the loaded partial DIEs
453 with partial_die->offset.SECT_OFF as hash. */
454 htab_t partial_dies = nullptr;
455
456 /* Storage for things with the same lifetime as this read-in compilation
457 unit, including partial DIEs. */
458 auto_obstack comp_unit_obstack;
459
460 /* When multiple dwarf2_cu structures are living in memory, this field
461 chains them all together, so that they can be released efficiently.
462 We will probably also want a generation counter so that most-recently-used
463 compilation units are cached... */
464 struct dwarf2_per_cu_data *read_in_chain = nullptr;
465
466 /* Backlink to our per_cu entry. */
467 struct dwarf2_per_cu_data *per_cu;
468
469 /* How many compilation units ago was this CU last referenced? */
470 int last_used = 0;
471
472 /* A hash table of DIE cu_offset for following references with
473 die_info->offset.sect_off as hash. */
474 htab_t die_hash = nullptr;
475
476 /* Full DIEs if read in. */
477 struct die_info *dies = nullptr;
478
479 /* A set of pointers to dwarf2_per_cu_data objects for compilation
480 units referenced by this one. Only set during full symbol processing;
481 partial symbol tables do not have dependencies. */
482 htab_t dependencies = nullptr;
483
484 /* Header data from the line table, during full symbol processing. */
485 struct line_header *line_header = nullptr;
486 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
487 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
488 this is the DW_TAG_compile_unit die for this CU. We'll hold on
489 to the line header as long as this DIE is being processed. See
490 process_die_scope. */
491 die_info *line_header_die_owner = nullptr;
492
493 /* A list of methods which need to have physnames computed
494 after all type information has been read. */
495 std::vector<delayed_method_info> method_list;
496
497 /* To be copied to symtab->call_site_htab. */
498 htab_t call_site_htab = nullptr;
499
500 /* Non-NULL if this CU came from a DWO file.
501 There is an invariant here that is important to remember:
502 Except for attributes copied from the top level DIE in the "main"
503 (or "stub") file in preparation for reading the DWO file
504 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
505 Either there isn't a DWO file (in which case this is NULL and the point
506 is moot), or there is and either we're not going to read it (in which
507 case this is NULL) or there is and we are reading it (in which case this
508 is non-NULL). */
509 struct dwo_unit *dwo_unit = nullptr;
510
511 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
512 Note this value comes from the Fission stub CU/TU's DIE. */
513 gdb::optional<ULONGEST> addr_base;
514
515 /* The DW_AT_rnglists_base attribute if present.
516 Note this value comes from the Fission stub CU/TU's DIE.
517 Also note that the value is zero in the non-DWO case so this value can
518 be used without needing to know whether DWO files are in use or not.
519 N.B. This does not apply to DW_AT_ranges appearing in
520 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
521 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
522 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
523 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
524 ULONGEST ranges_base = 0;
525
526 /* The DW_AT_loclists_base attribute if present. */
527 ULONGEST loclist_base = 0;
528
529 /* When reading debug info generated by older versions of rustc, we
530 have to rewrite some union types to be struct types with a
531 variant part. This rewriting must be done after the CU is fully
532 read in, because otherwise at the point of rewriting some struct
533 type might not have been fully processed. So, we keep a list of
534 all such types here and process them after expansion. */
535 std::vector<struct type *> rust_unions;
536
537 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
538 files, the value is implicitly zero. For DWARF 5 version DWO files, the
539 value is often implicit and is the size of the header of
540 .debug_str_offsets section (8 or 4, depending on the address size). */
541 gdb::optional<ULONGEST> str_offsets_base;
542
543 /* Mark used when releasing cached dies. */
544 bool mark : 1;
545
546 /* This CU references .debug_loc. See the symtab->locations_valid field.
547 This test is imperfect as there may exist optimized debug code not using
548 any location list and still facing inlining issues if handled as
549 unoptimized code. For a future better test see GCC PR other/32998. */
550 bool has_loclist : 1;
551
552 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
553 if all the producer_is_* fields are valid. This information is cached
554 because profiling CU expansion showed excessive time spent in
555 producer_is_gxx_lt_4_6. */
556 bool checked_producer : 1;
557 bool producer_is_gxx_lt_4_6 : 1;
558 bool producer_is_gcc_lt_4_3 : 1;
559 bool producer_is_icc : 1;
560 bool producer_is_icc_lt_14 : 1;
561 bool producer_is_codewarrior : 1;
562
563 /* When true, the file that we're processing is known to have
564 debugging info for C++ namespaces. GCC 3.3.x did not produce
565 this information, but later versions do. */
566
567 bool processing_has_namespace_info : 1;
568
569 struct partial_die_info *find_partial_die (sect_offset sect_off);
570
571 /* If this CU was inherited by another CU (via specification,
572 abstract_origin, etc), this is the ancestor CU. */
573 dwarf2_cu *ancestor;
574
575 /* Get the buildsym_compunit for this CU. */
576 buildsym_compunit *get_builder ()
577 {
578 /* If this CU has a builder associated with it, use that. */
579 if (m_builder != nullptr)
580 return m_builder.get ();
581
582 /* Otherwise, search ancestors for a valid builder. */
583 if (ancestor != nullptr)
584 return ancestor->get_builder ();
585
586 return nullptr;
587 }
588 };
589
590 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
591 This includes type_unit_group and quick_file_names. */
592
593 struct stmt_list_hash
594 {
595 /* The DWO unit this table is from or NULL if there is none. */
596 struct dwo_unit *dwo_unit;
597
598 /* Offset in .debug_line or .debug_line.dwo. */
599 sect_offset line_sect_off;
600 };
601
602 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
603 an object of this type. */
604
605 struct type_unit_group
606 {
607 /* dwarf2read.c's main "handle" on a TU symtab.
608 To simplify things we create an artificial CU that "includes" all the
609 type units using this stmt_list so that the rest of the code still has
610 a "per_cu" handle on the symtab. */
611 struct dwarf2_per_cu_data per_cu;
612
613 /* The TUs that share this DW_AT_stmt_list entry.
614 This is added to while parsing type units to build partial symtabs,
615 and is deleted afterwards and not used again. */
616 std::vector<signatured_type *> *tus;
617
618 /* The compunit symtab.
619 Type units in a group needn't all be defined in the same source file,
620 so we create an essentially anonymous symtab as the compunit symtab. */
621 struct compunit_symtab *compunit_symtab;
622
623 /* The data used to construct the hash key. */
624 struct stmt_list_hash hash;
625
626 /* The symbol tables for this TU (obtained from the files listed in
627 DW_AT_stmt_list).
628 WARNING: The order of entries here must match the order of entries
629 in the line header. After the first TU using this type_unit_group, the
630 line header for the subsequent TUs is recreated from this. This is done
631 because we need to use the same symtabs for each TU using the same
632 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
633 there's no guarantee the line header doesn't have duplicate entries. */
634 struct symtab **symtabs;
635 };
636
637 /* These sections are what may appear in a (real or virtual) DWO file. */
638
639 struct dwo_sections
640 {
641 struct dwarf2_section_info abbrev;
642 struct dwarf2_section_info line;
643 struct dwarf2_section_info loc;
644 struct dwarf2_section_info loclists;
645 struct dwarf2_section_info macinfo;
646 struct dwarf2_section_info macro;
647 struct dwarf2_section_info str;
648 struct dwarf2_section_info str_offsets;
649 /* In the case of a virtual DWO file, these two are unused. */
650 struct dwarf2_section_info info;
651 std::vector<dwarf2_section_info> types;
652 };
653
654 /* CUs/TUs in DWP/DWO files. */
655
656 struct dwo_unit
657 {
658 /* Backlink to the containing struct dwo_file. */
659 struct dwo_file *dwo_file;
660
661 /* The "id" that distinguishes this CU/TU.
662 .debug_info calls this "dwo_id", .debug_types calls this "signature".
663 Since signatures came first, we stick with it for consistency. */
664 ULONGEST signature;
665
666 /* The section this CU/TU lives in, in the DWO file. */
667 struct dwarf2_section_info *section;
668
669 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
670 sect_offset sect_off;
671 unsigned int length;
672
673 /* For types, offset in the type's DIE of the type defined by this TU. */
674 cu_offset type_offset_in_tu;
675 };
676
677 /* include/dwarf2.h defines the DWP section codes.
678 It defines a max value but it doesn't define a min value, which we
679 use for error checking, so provide one. */
680
681 enum dwp_v2_section_ids
682 {
683 DW_SECT_MIN = 1
684 };
685
686 /* Data for one DWO file.
687
688 This includes virtual DWO files (a virtual DWO file is a DWO file as it
689 appears in a DWP file). DWP files don't really have DWO files per se -
690 comdat folding of types "loses" the DWO file they came from, and from
691 a high level view DWP files appear to contain a mass of random types.
692 However, to maintain consistency with the non-DWP case we pretend DWP
693 files contain virtual DWO files, and we assign each TU with one virtual
694 DWO file (generally based on the line and abbrev section offsets -
695 a heuristic that seems to work in practice). */
696
697 struct dwo_file
698 {
699 dwo_file () = default;
700 DISABLE_COPY_AND_ASSIGN (dwo_file);
701
702 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
703 For virtual DWO files the name is constructed from the section offsets
704 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
705 from related CU+TUs. */
706 const char *dwo_name = nullptr;
707
708 /* The DW_AT_comp_dir attribute. */
709 const char *comp_dir = nullptr;
710
711 /* The bfd, when the file is open. Otherwise this is NULL.
712 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
713 gdb_bfd_ref_ptr dbfd;
714
715 /* The sections that make up this DWO file.
716 Remember that for virtual DWO files in DWP V2, these are virtual
717 sections (for lack of a better name). */
718 struct dwo_sections sections {};
719
720 /* The CUs in the file.
721 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
722 an extension to handle LLVM's Link Time Optimization output (where
723 multiple source files may be compiled into a single object/dwo pair). */
724 htab_up cus;
725
726 /* Table of TUs in the file.
727 Each element is a struct dwo_unit. */
728 htab_up tus;
729 };
730
731 /* These sections are what may appear in a DWP file. */
732
733 struct dwp_sections
734 {
735 /* These are used by both DWP version 1 and 2. */
736 struct dwarf2_section_info str;
737 struct dwarf2_section_info cu_index;
738 struct dwarf2_section_info tu_index;
739
740 /* These are only used by DWP version 2 files.
741 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
742 sections are referenced by section number, and are not recorded here.
743 In DWP version 2 there is at most one copy of all these sections, each
744 section being (effectively) comprised of the concatenation of all of the
745 individual sections that exist in the version 1 format.
746 To keep the code simple we treat each of these concatenated pieces as a
747 section itself (a virtual section?). */
748 struct dwarf2_section_info abbrev;
749 struct dwarf2_section_info info;
750 struct dwarf2_section_info line;
751 struct dwarf2_section_info loc;
752 struct dwarf2_section_info macinfo;
753 struct dwarf2_section_info macro;
754 struct dwarf2_section_info str_offsets;
755 struct dwarf2_section_info types;
756 };
757
758 /* These sections are what may appear in a virtual DWO file in DWP version 1.
759 A virtual DWO file is a DWO file as it appears in a DWP file. */
760
761 struct virtual_v1_dwo_sections
762 {
763 struct dwarf2_section_info abbrev;
764 struct dwarf2_section_info line;
765 struct dwarf2_section_info loc;
766 struct dwarf2_section_info macinfo;
767 struct dwarf2_section_info macro;
768 struct dwarf2_section_info str_offsets;
769 /* Each DWP hash table entry records one CU or one TU.
770 That is recorded here, and copied to dwo_unit.section. */
771 struct dwarf2_section_info info_or_types;
772 };
773
774 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
775 In version 2, the sections of the DWO files are concatenated together
776 and stored in one section of that name. Thus each ELF section contains
777 several "virtual" sections. */
778
779 struct virtual_v2_dwo_sections
780 {
781 bfd_size_type abbrev_offset;
782 bfd_size_type abbrev_size;
783
784 bfd_size_type line_offset;
785 bfd_size_type line_size;
786
787 bfd_size_type loc_offset;
788 bfd_size_type loc_size;
789
790 bfd_size_type macinfo_offset;
791 bfd_size_type macinfo_size;
792
793 bfd_size_type macro_offset;
794 bfd_size_type macro_size;
795
796 bfd_size_type str_offsets_offset;
797 bfd_size_type str_offsets_size;
798
799 /* Each DWP hash table entry records one CU or one TU.
800 That is recorded here, and copied to dwo_unit.section. */
801 bfd_size_type info_or_types_offset;
802 bfd_size_type info_or_types_size;
803 };
804
805 /* Contents of DWP hash tables. */
806
807 struct dwp_hash_table
808 {
809 uint32_t version, nr_columns;
810 uint32_t nr_units, nr_slots;
811 const gdb_byte *hash_table, *unit_table;
812 union
813 {
814 struct
815 {
816 const gdb_byte *indices;
817 } v1;
818 struct
819 {
820 /* This is indexed by column number and gives the id of the section
821 in that column. */
822 #define MAX_NR_V2_DWO_SECTIONS \
823 (1 /* .debug_info or .debug_types */ \
824 + 1 /* .debug_abbrev */ \
825 + 1 /* .debug_line */ \
826 + 1 /* .debug_loc */ \
827 + 1 /* .debug_str_offsets */ \
828 + 1 /* .debug_macro or .debug_macinfo */)
829 int section_ids[MAX_NR_V2_DWO_SECTIONS];
830 const gdb_byte *offsets;
831 const gdb_byte *sizes;
832 } v2;
833 } section_pool;
834 };
835
836 /* Data for one DWP file. */
837
838 struct dwp_file
839 {
840 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
841 : name (name_),
842 dbfd (std::move (abfd))
843 {
844 }
845
846 /* Name of the file. */
847 const char *name;
848
849 /* File format version. */
850 int version = 0;
851
852 /* The bfd. */
853 gdb_bfd_ref_ptr dbfd;
854
855 /* Section info for this file. */
856 struct dwp_sections sections {};
857
858 /* Table of CUs in the file. */
859 const struct dwp_hash_table *cus = nullptr;
860
861 /* Table of TUs in the file. */
862 const struct dwp_hash_table *tus = nullptr;
863
864 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
865 htab_up loaded_cus;
866 htab_up loaded_tus;
867
868 /* Table to map ELF section numbers to their sections.
869 This is only needed for the DWP V1 file format. */
870 unsigned int num_sections = 0;
871 asection **elf_sections = nullptr;
872 };
873
874 /* Struct used to pass misc. parameters to read_die_and_children, et
875 al. which are used for both .debug_info and .debug_types dies.
876 All parameters here are unchanging for the life of the call. This
877 struct exists to abstract away the constant parameters of die reading. */
878
879 struct die_reader_specs
880 {
881 /* The bfd of die_section. */
882 bfd* abfd;
883
884 /* The CU of the DIE we are parsing. */
885 struct dwarf2_cu *cu;
886
887 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
888 struct dwo_file *dwo_file;
889
890 /* The section the die comes from.
891 This is either .debug_info or .debug_types, or the .dwo variants. */
892 struct dwarf2_section_info *die_section;
893
894 /* die_section->buffer. */
895 const gdb_byte *buffer;
896
897 /* The end of the buffer. */
898 const gdb_byte *buffer_end;
899
900 /* The abbreviation table to use when reading the DIEs. */
901 struct abbrev_table *abbrev_table;
902 };
903
904 /* A subclass of die_reader_specs that holds storage and has complex
905 constructor and destructor behavior. */
906
907 class cutu_reader : public die_reader_specs
908 {
909 public:
910
911 cutu_reader (struct dwarf2_per_cu_data *this_cu,
912 struct abbrev_table *abbrev_table,
913 int use_existing_cu,
914 bool skip_partial);
915
916 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
917 struct dwarf2_cu *parent_cu = nullptr,
918 struct dwo_file *dwo_file = nullptr);
919
920 DISABLE_COPY_AND_ASSIGN (cutu_reader);
921
922 const gdb_byte *info_ptr = nullptr;
923 struct die_info *comp_unit_die = nullptr;
924 bool dummy_p = false;
925
926 /* Release the new CU, putting it on the chain. This cannot be done
927 for dummy CUs. */
928 void keep ();
929
930 private:
931 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
932 int use_existing_cu);
933
934 struct dwarf2_per_cu_data *m_this_cu;
935 std::unique_ptr<dwarf2_cu> m_new_cu;
936
937 /* The ordinary abbreviation table. */
938 abbrev_table_up m_abbrev_table_holder;
939
940 /* The DWO abbreviation table. */
941 abbrev_table_up m_dwo_abbrev_table;
942 };
943
944 /* When we construct a partial symbol table entry we only
945 need this much information. */
946 struct partial_die_info : public allocate_on_obstack
947 {
948 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
949
950 /* Disable assign but still keep copy ctor, which is needed
951 load_partial_dies. */
952 partial_die_info& operator=(const partial_die_info& rhs) = delete;
953
954 /* Adjust the partial die before generating a symbol for it. This
955 function may set the is_external flag or change the DIE's
956 name. */
957 void fixup (struct dwarf2_cu *cu);
958
959 /* Read a minimal amount of information into the minimal die
960 structure. */
961 const gdb_byte *read (const struct die_reader_specs *reader,
962 const struct abbrev_info &abbrev,
963 const gdb_byte *info_ptr);
964
965 /* Offset of this DIE. */
966 const sect_offset sect_off;
967
968 /* DWARF-2 tag for this DIE. */
969 const ENUM_BITFIELD(dwarf_tag) tag : 16;
970
971 /* Assorted flags describing the data found in this DIE. */
972 const unsigned int has_children : 1;
973
974 unsigned int is_external : 1;
975 unsigned int is_declaration : 1;
976 unsigned int has_type : 1;
977 unsigned int has_specification : 1;
978 unsigned int has_pc_info : 1;
979 unsigned int may_be_inlined : 1;
980
981 /* This DIE has been marked DW_AT_main_subprogram. */
982 unsigned int main_subprogram : 1;
983
984 /* Flag set if the SCOPE field of this structure has been
985 computed. */
986 unsigned int scope_set : 1;
987
988 /* Flag set if the DIE has a byte_size attribute. */
989 unsigned int has_byte_size : 1;
990
991 /* Flag set if the DIE has a DW_AT_const_value attribute. */
992 unsigned int has_const_value : 1;
993
994 /* Flag set if any of the DIE's children are template arguments. */
995 unsigned int has_template_arguments : 1;
996
997 /* Flag set if fixup has been called on this die. */
998 unsigned int fixup_called : 1;
999
1000 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1001 unsigned int is_dwz : 1;
1002
1003 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1004 unsigned int spec_is_dwz : 1;
1005
1006 /* The name of this DIE. Normally the value of DW_AT_name, but
1007 sometimes a default name for unnamed DIEs. */
1008 const char *name = nullptr;
1009
1010 /* The linkage name, if present. */
1011 const char *linkage_name = nullptr;
1012
1013 /* The scope to prepend to our children. This is generally
1014 allocated on the comp_unit_obstack, so will disappear
1015 when this compilation unit leaves the cache. */
1016 const char *scope = nullptr;
1017
1018 /* Some data associated with the partial DIE. The tag determines
1019 which field is live. */
1020 union
1021 {
1022 /* The location description associated with this DIE, if any. */
1023 struct dwarf_block *locdesc;
1024 /* The offset of an import, for DW_TAG_imported_unit. */
1025 sect_offset sect_off;
1026 } d {};
1027
1028 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1029 CORE_ADDR lowpc = 0;
1030 CORE_ADDR highpc = 0;
1031
1032 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1033 DW_AT_sibling, if any. */
1034 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1035 could return DW_AT_sibling values to its caller load_partial_dies. */
1036 const gdb_byte *sibling = nullptr;
1037
1038 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1039 DW_AT_specification (or DW_AT_abstract_origin or
1040 DW_AT_extension). */
1041 sect_offset spec_offset {};
1042
1043 /* Pointers to this DIE's parent, first child, and next sibling,
1044 if any. */
1045 struct partial_die_info *die_parent = nullptr;
1046 struct partial_die_info *die_child = nullptr;
1047 struct partial_die_info *die_sibling = nullptr;
1048
1049 friend struct partial_die_info *
1050 dwarf2_cu::find_partial_die (sect_offset sect_off);
1051
1052 private:
1053 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1054 partial_die_info (sect_offset sect_off)
1055 : partial_die_info (sect_off, DW_TAG_padding, 0)
1056 {
1057 }
1058
1059 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1060 int has_children_)
1061 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1062 {
1063 is_external = 0;
1064 is_declaration = 0;
1065 has_type = 0;
1066 has_specification = 0;
1067 has_pc_info = 0;
1068 may_be_inlined = 0;
1069 main_subprogram = 0;
1070 scope_set = 0;
1071 has_byte_size = 0;
1072 has_const_value = 0;
1073 has_template_arguments = 0;
1074 fixup_called = 0;
1075 is_dwz = 0;
1076 spec_is_dwz = 0;
1077 }
1078 };
1079
1080 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1081 but this would require a corresponding change in unpack_field_as_long
1082 and friends. */
1083 static int bits_per_byte = 8;
1084
1085 /* When reading a variant or variant part, we track a bit more
1086 information about the field, and store it in an object of this
1087 type. */
1088
1089 struct variant_field
1090 {
1091 /* If we see a DW_TAG_variant, then this will be the discriminant
1092 value. */
1093 ULONGEST discriminant_value;
1094 /* If we see a DW_TAG_variant, then this will be set if this is the
1095 default branch. */
1096 bool default_branch;
1097 /* While reading a DW_TAG_variant_part, this will be set if this
1098 field is the discriminant. */
1099 bool is_discriminant;
1100 };
1101
1102 struct nextfield
1103 {
1104 int accessibility = 0;
1105 int virtuality = 0;
1106 /* Extra information to describe a variant or variant part. */
1107 struct variant_field variant {};
1108 struct field field {};
1109 };
1110
1111 struct fnfieldlist
1112 {
1113 const char *name = nullptr;
1114 std::vector<struct fn_field> fnfields;
1115 };
1116
1117 /* The routines that read and process dies for a C struct or C++ class
1118 pass lists of data member fields and lists of member function fields
1119 in an instance of a field_info structure, as defined below. */
1120 struct field_info
1121 {
1122 /* List of data member and baseclasses fields. */
1123 std::vector<struct nextfield> fields;
1124 std::vector<struct nextfield> baseclasses;
1125
1126 /* Set if the accessibility of one of the fields is not public. */
1127 int non_public_fields = 0;
1128
1129 /* Member function fieldlist array, contains name of possibly overloaded
1130 member function, number of overloaded member functions and a pointer
1131 to the head of the member function field chain. */
1132 std::vector<struct fnfieldlist> fnfieldlists;
1133
1134 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1135 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1136 std::vector<struct decl_field> typedef_field_list;
1137
1138 /* Nested types defined by this class and the number of elements in this
1139 list. */
1140 std::vector<struct decl_field> nested_types_list;
1141
1142 /* Return the total number of fields (including baseclasses). */
1143 int nfields () const
1144 {
1145 return fields.size () + baseclasses.size ();
1146 }
1147 };
1148
1149 /* Loaded secondary compilation units are kept in memory until they
1150 have not been referenced for the processing of this many
1151 compilation units. Set this to zero to disable caching. Cache
1152 sizes of up to at least twenty will improve startup time for
1153 typical inter-CU-reference binaries, at an obvious memory cost. */
1154 static int dwarf_max_cache_age = 5;
1155 static void
1156 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1157 struct cmd_list_element *c, const char *value)
1158 {
1159 fprintf_filtered (file, _("The upper bound on the age of cached "
1160 "DWARF compilation units is %s.\n"),
1161 value);
1162 }
1163 \f
1164 /* local function prototypes */
1165
1166 static void dwarf2_find_base_address (struct die_info *die,
1167 struct dwarf2_cu *cu);
1168
1169 static dwarf2_psymtab *create_partial_symtab
1170 (struct dwarf2_per_cu_data *per_cu, const char *name);
1171
1172 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1173 const gdb_byte *info_ptr,
1174 struct die_info *type_unit_die);
1175
1176 static void dwarf2_build_psymtabs_hard
1177 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1178
1179 static void scan_partial_symbols (struct partial_die_info *,
1180 CORE_ADDR *, CORE_ADDR *,
1181 int, struct dwarf2_cu *);
1182
1183 static void add_partial_symbol (struct partial_die_info *,
1184 struct dwarf2_cu *);
1185
1186 static void add_partial_namespace (struct partial_die_info *pdi,
1187 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1188 int set_addrmap, struct dwarf2_cu *cu);
1189
1190 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1191 CORE_ADDR *highpc, int set_addrmap,
1192 struct dwarf2_cu *cu);
1193
1194 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1195 struct dwarf2_cu *cu);
1196
1197 static void add_partial_subprogram (struct partial_die_info *pdi,
1198 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1199 int need_pc, struct dwarf2_cu *cu);
1200
1201 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1202
1203 static struct partial_die_info *load_partial_dies
1204 (const struct die_reader_specs *, const gdb_byte *, int);
1205
1206 /* A pair of partial_die_info and compilation unit. */
1207 struct cu_partial_die_info
1208 {
1209 /* The compilation unit of the partial_die_info. */
1210 struct dwarf2_cu *cu;
1211 /* A partial_die_info. */
1212 struct partial_die_info *pdi;
1213
1214 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1215 : cu (cu),
1216 pdi (pdi)
1217 { /* Nothing. */ }
1218
1219 private:
1220 cu_partial_die_info () = delete;
1221 };
1222
1223 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1224 struct dwarf2_cu *);
1225
1226 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1227 struct attribute *, struct attr_abbrev *,
1228 const gdb_byte *, bool *need_reprocess);
1229
1230 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1231 struct attribute *attr);
1232
1233 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1234
1235 static sect_offset read_abbrev_offset
1236 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1237 struct dwarf2_section_info *, sect_offset);
1238
1239 static const char *read_indirect_string
1240 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1241 const struct comp_unit_head *, unsigned int *);
1242
1243 static const char *read_indirect_string_at_offset
1244 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1245
1246 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1247 const gdb_byte *,
1248 unsigned int *);
1249
1250 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1251 ULONGEST str_index);
1252
1253 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1254 ULONGEST str_index);
1255
1256 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1257
1258 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1259 struct dwarf2_cu *);
1260
1261 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1262 struct dwarf2_cu *cu);
1263
1264 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1265
1266 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1267 struct dwarf2_cu *cu);
1268
1269 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1270
1271 static struct die_info *die_specification (struct die_info *die,
1272 struct dwarf2_cu **);
1273
1274 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1275 struct dwarf2_cu *cu);
1276
1277 static void dwarf_decode_lines (struct line_header *, const char *,
1278 struct dwarf2_cu *, dwarf2_psymtab *,
1279 CORE_ADDR, int decode_mapping);
1280
1281 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1282 const char *);
1283
1284 static struct symbol *new_symbol (struct die_info *, struct type *,
1285 struct dwarf2_cu *, struct symbol * = NULL);
1286
1287 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1288 struct dwarf2_cu *);
1289
1290 static void dwarf2_const_value_attr (const struct attribute *attr,
1291 struct type *type,
1292 const char *name,
1293 struct obstack *obstack,
1294 struct dwarf2_cu *cu, LONGEST *value,
1295 const gdb_byte **bytes,
1296 struct dwarf2_locexpr_baton **baton);
1297
1298 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1299
1300 static int need_gnat_info (struct dwarf2_cu *);
1301
1302 static struct type *die_descriptive_type (struct die_info *,
1303 struct dwarf2_cu *);
1304
1305 static void set_descriptive_type (struct type *, struct die_info *,
1306 struct dwarf2_cu *);
1307
1308 static struct type *die_containing_type (struct die_info *,
1309 struct dwarf2_cu *);
1310
1311 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1312 struct dwarf2_cu *);
1313
1314 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1315
1316 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1317
1318 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1319
1320 static char *typename_concat (struct obstack *obs, const char *prefix,
1321 const char *suffix, int physname,
1322 struct dwarf2_cu *cu);
1323
1324 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1325
1326 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1327
1328 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1329
1330 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1331
1332 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1333
1334 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1335
1336 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1337 struct dwarf2_cu *, dwarf2_psymtab *);
1338
1339 /* Return the .debug_loclists section to use for cu. */
1340 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1341
1342 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1343 values. Keep the items ordered with increasing constraints compliance. */
1344 enum pc_bounds_kind
1345 {
1346 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1347 PC_BOUNDS_NOT_PRESENT,
1348
1349 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1350 were present but they do not form a valid range of PC addresses. */
1351 PC_BOUNDS_INVALID,
1352
1353 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1354 PC_BOUNDS_RANGES,
1355
1356 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1357 PC_BOUNDS_HIGH_LOW,
1358 };
1359
1360 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1361 CORE_ADDR *, CORE_ADDR *,
1362 struct dwarf2_cu *,
1363 dwarf2_psymtab *);
1364
1365 static void get_scope_pc_bounds (struct die_info *,
1366 CORE_ADDR *, CORE_ADDR *,
1367 struct dwarf2_cu *);
1368
1369 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1370 CORE_ADDR, struct dwarf2_cu *);
1371
1372 static void dwarf2_add_field (struct field_info *, struct die_info *,
1373 struct dwarf2_cu *);
1374
1375 static void dwarf2_attach_fields_to_type (struct field_info *,
1376 struct type *, struct dwarf2_cu *);
1377
1378 static void dwarf2_add_member_fn (struct field_info *,
1379 struct die_info *, struct type *,
1380 struct dwarf2_cu *);
1381
1382 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1383 struct type *,
1384 struct dwarf2_cu *);
1385
1386 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1387
1388 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1389
1390 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1391
1392 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1393
1394 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1395
1396 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1397
1398 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1399
1400 static struct type *read_module_type (struct die_info *die,
1401 struct dwarf2_cu *cu);
1402
1403 static const char *namespace_name (struct die_info *die,
1404 int *is_anonymous, struct dwarf2_cu *);
1405
1406 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1407
1408 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1409
1410 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1411 struct dwarf2_cu *);
1412
1413 static struct die_info *read_die_and_siblings_1
1414 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1415 struct die_info *);
1416
1417 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1418 const gdb_byte *info_ptr,
1419 const gdb_byte **new_info_ptr,
1420 struct die_info *parent);
1421
1422 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1423 struct die_info **, const gdb_byte *,
1424 int);
1425
1426 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1427 struct die_info **, const gdb_byte *);
1428
1429 static void process_die (struct die_info *, struct dwarf2_cu *);
1430
1431 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1432 struct objfile *);
1433
1434 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1435
1436 static const char *dwarf2_full_name (const char *name,
1437 struct die_info *die,
1438 struct dwarf2_cu *cu);
1439
1440 static const char *dwarf2_physname (const char *name, struct die_info *die,
1441 struct dwarf2_cu *cu);
1442
1443 static struct die_info *dwarf2_extension (struct die_info *die,
1444 struct dwarf2_cu **);
1445
1446 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1447
1448 static void dump_die_for_error (struct die_info *);
1449
1450 static void dump_die_1 (struct ui_file *, int level, int max_level,
1451 struct die_info *);
1452
1453 /*static*/ void dump_die (struct die_info *, int max_level);
1454
1455 static void store_in_ref_table (struct die_info *,
1456 struct dwarf2_cu *);
1457
1458 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1459 const struct attribute *,
1460 struct dwarf2_cu **);
1461
1462 static struct die_info *follow_die_ref (struct die_info *,
1463 const struct attribute *,
1464 struct dwarf2_cu **);
1465
1466 static struct die_info *follow_die_sig (struct die_info *,
1467 const struct attribute *,
1468 struct dwarf2_cu **);
1469
1470 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1471 struct dwarf2_cu *);
1472
1473 static struct type *get_DW_AT_signature_type (struct die_info *,
1474 const struct attribute *,
1475 struct dwarf2_cu *);
1476
1477 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1478
1479 static void read_signatured_type (struct signatured_type *);
1480
1481 static int attr_to_dynamic_prop (const struct attribute *attr,
1482 struct die_info *die, struct dwarf2_cu *cu,
1483 struct dynamic_prop *prop, struct type *type);
1484
1485 /* memory allocation interface */
1486
1487 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1488
1489 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1490
1491 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1492
1493 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1494 struct dwarf2_loclist_baton *baton,
1495 const struct attribute *attr);
1496
1497 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1498 struct symbol *sym,
1499 struct dwarf2_cu *cu,
1500 int is_block);
1501
1502 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1503 const gdb_byte *info_ptr,
1504 struct abbrev_info *abbrev);
1505
1506 static hashval_t partial_die_hash (const void *item);
1507
1508 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1509
1510 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1511 (sect_offset sect_off, unsigned int offset_in_dwz,
1512 struct dwarf2_per_objfile *dwarf2_per_objfile);
1513
1514 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1515 struct die_info *comp_unit_die,
1516 enum language pretend_language);
1517
1518 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1519
1520 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1521
1522 static struct type *set_die_type (struct die_info *, struct type *,
1523 struct dwarf2_cu *);
1524
1525 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1526
1527 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1528
1529 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1530 enum language);
1531
1532 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1533 enum language);
1534
1535 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1536 enum language);
1537
1538 static void dwarf2_add_dependence (struct dwarf2_cu *,
1539 struct dwarf2_per_cu_data *);
1540
1541 static void dwarf2_mark (struct dwarf2_cu *);
1542
1543 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1544
1545 static struct type *get_die_type_at_offset (sect_offset,
1546 struct dwarf2_per_cu_data *);
1547
1548 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1549
1550 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1551 enum language pretend_language);
1552
1553 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1554
1555 /* Class, the destructor of which frees all allocated queue entries. This
1556 will only have work to do if an error was thrown while processing the
1557 dwarf. If no error was thrown then the queue entries should have all
1558 been processed, and freed, as we went along. */
1559
1560 class dwarf2_queue_guard
1561 {
1562 public:
1563 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1564 : m_per_objfile (per_objfile)
1565 {
1566 }
1567
1568 /* Free any entries remaining on the queue. There should only be
1569 entries left if we hit an error while processing the dwarf. */
1570 ~dwarf2_queue_guard ()
1571 {
1572 /* Ensure that no memory is allocated by the queue. */
1573 std::queue<dwarf2_queue_item> empty;
1574 std::swap (m_per_objfile->queue, empty);
1575 }
1576
1577 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1578
1579 private:
1580 dwarf2_per_objfile *m_per_objfile;
1581 };
1582
1583 dwarf2_queue_item::~dwarf2_queue_item ()
1584 {
1585 /* Anything still marked queued is likely to be in an
1586 inconsistent state, so discard it. */
1587 if (per_cu->queued)
1588 {
1589 if (per_cu->cu != NULL)
1590 free_one_cached_comp_unit (per_cu);
1591 per_cu->queued = 0;
1592 }
1593 }
1594
1595 /* The return type of find_file_and_directory. Note, the enclosed
1596 string pointers are only valid while this object is valid. */
1597
1598 struct file_and_directory
1599 {
1600 /* The filename. This is never NULL. */
1601 const char *name;
1602
1603 /* The compilation directory. NULL if not known. If we needed to
1604 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1605 points directly to the DW_AT_comp_dir string attribute owned by
1606 the obstack that owns the DIE. */
1607 const char *comp_dir;
1608
1609 /* If we needed to build a new string for comp_dir, this is what
1610 owns the storage. */
1611 std::string comp_dir_storage;
1612 };
1613
1614 static file_and_directory find_file_and_directory (struct die_info *die,
1615 struct dwarf2_cu *cu);
1616
1617 static htab_up allocate_signatured_type_table ();
1618
1619 static htab_up allocate_dwo_unit_table ();
1620
1621 static struct dwo_unit *lookup_dwo_unit_in_dwp
1622 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1623 struct dwp_file *dwp_file, const char *comp_dir,
1624 ULONGEST signature, int is_debug_types);
1625
1626 static struct dwp_file *get_dwp_file
1627 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1628
1629 static struct dwo_unit *lookup_dwo_comp_unit
1630 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1631
1632 static struct dwo_unit *lookup_dwo_type_unit
1633 (struct signatured_type *, const char *, const char *);
1634
1635 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1636
1637 /* A unique pointer to a dwo_file. */
1638
1639 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1640
1641 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1642
1643 static void check_producer (struct dwarf2_cu *cu);
1644
1645 static void free_line_header_voidp (void *arg);
1646 \f
1647 /* Various complaints about symbol reading that don't abort the process. */
1648
1649 static void
1650 dwarf2_debug_line_missing_file_complaint (void)
1651 {
1652 complaint (_(".debug_line section has line data without a file"));
1653 }
1654
1655 static void
1656 dwarf2_debug_line_missing_end_sequence_complaint (void)
1657 {
1658 complaint (_(".debug_line section has line "
1659 "program sequence without an end"));
1660 }
1661
1662 static void
1663 dwarf2_complex_location_expr_complaint (void)
1664 {
1665 complaint (_("location expression too complex"));
1666 }
1667
1668 static void
1669 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1670 int arg3)
1671 {
1672 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1673 arg1, arg2, arg3);
1674 }
1675
1676 static void
1677 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1678 {
1679 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1680 arg1, arg2);
1681 }
1682
1683 /* Hash function for line_header_hash. */
1684
1685 static hashval_t
1686 line_header_hash (const struct line_header *ofs)
1687 {
1688 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1689 }
1690
1691 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1692
1693 static hashval_t
1694 line_header_hash_voidp (const void *item)
1695 {
1696 const struct line_header *ofs = (const struct line_header *) item;
1697
1698 return line_header_hash (ofs);
1699 }
1700
1701 /* Equality function for line_header_hash. */
1702
1703 static int
1704 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1705 {
1706 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1707 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1708
1709 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1710 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1711 }
1712
1713 \f
1714
1715 /* See declaration. */
1716
1717 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
1718 const dwarf2_debug_sections *names,
1719 bool can_copy_)
1720 : objfile (objfile_),
1721 can_copy (can_copy_)
1722 {
1723 if (names == NULL)
1724 names = &dwarf2_elf_names;
1725
1726 bfd *obfd = objfile->obfd;
1727
1728 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1729 locate_sections (obfd, sec, *names);
1730 }
1731
1732 dwarf2_per_objfile::~dwarf2_per_objfile ()
1733 {
1734 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1735 free_cached_comp_units ();
1736
1737 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1738 per_cu->imported_symtabs_free ();
1739
1740 for (signatured_type *sig_type : all_type_units)
1741 sig_type->per_cu.imported_symtabs_free ();
1742
1743 /* Everything else should be on the objfile obstack. */
1744 }
1745
1746 /* See declaration. */
1747
1748 void
1749 dwarf2_per_objfile::free_cached_comp_units ()
1750 {
1751 dwarf2_per_cu_data *per_cu = read_in_chain;
1752 dwarf2_per_cu_data **last_chain = &read_in_chain;
1753 while (per_cu != NULL)
1754 {
1755 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1756
1757 delete per_cu->cu;
1758 *last_chain = next_cu;
1759 per_cu = next_cu;
1760 }
1761 }
1762
1763 /* A helper class that calls free_cached_comp_units on
1764 destruction. */
1765
1766 class free_cached_comp_units
1767 {
1768 public:
1769
1770 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1771 : m_per_objfile (per_objfile)
1772 {
1773 }
1774
1775 ~free_cached_comp_units ()
1776 {
1777 m_per_objfile->free_cached_comp_units ();
1778 }
1779
1780 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1781
1782 private:
1783
1784 dwarf2_per_objfile *m_per_objfile;
1785 };
1786
1787 /* Try to locate the sections we need for DWARF 2 debugging
1788 information and return true if we have enough to do something.
1789 NAMES points to the dwarf2 section names, or is NULL if the standard
1790 ELF names are used. CAN_COPY is true for formats where symbol
1791 interposition is possible and so symbol values must follow copy
1792 relocation rules. */
1793
1794 int
1795 dwarf2_has_info (struct objfile *objfile,
1796 const struct dwarf2_debug_sections *names,
1797 bool can_copy)
1798 {
1799 if (objfile->flags & OBJF_READNEVER)
1800 return 0;
1801
1802 struct dwarf2_per_objfile *dwarf2_per_objfile
1803 = get_dwarf2_per_objfile (objfile);
1804
1805 if (dwarf2_per_objfile == NULL)
1806 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
1807 names,
1808 can_copy);
1809
1810 return (!dwarf2_per_objfile->info.is_virtual
1811 && dwarf2_per_objfile->info.s.section != NULL
1812 && !dwarf2_per_objfile->abbrev.is_virtual
1813 && dwarf2_per_objfile->abbrev.s.section != NULL);
1814 }
1815
1816 /* When loading sections, we look either for uncompressed section or for
1817 compressed section names. */
1818
1819 static int
1820 section_is_p (const char *section_name,
1821 const struct dwarf2_section_names *names)
1822 {
1823 if (names->normal != NULL
1824 && strcmp (section_name, names->normal) == 0)
1825 return 1;
1826 if (names->compressed != NULL
1827 && strcmp (section_name, names->compressed) == 0)
1828 return 1;
1829 return 0;
1830 }
1831
1832 /* See declaration. */
1833
1834 void
1835 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1836 const dwarf2_debug_sections &names)
1837 {
1838 flagword aflag = bfd_section_flags (sectp);
1839
1840 if ((aflag & SEC_HAS_CONTENTS) == 0)
1841 {
1842 }
1843 else if (elf_section_data (sectp)->this_hdr.sh_size
1844 > bfd_get_file_size (abfd))
1845 {
1846 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1847 warning (_("Discarding section %s which has a section size (%s"
1848 ") larger than the file size [in module %s]"),
1849 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1850 bfd_get_filename (abfd));
1851 }
1852 else if (section_is_p (sectp->name, &names.info))
1853 {
1854 this->info.s.section = sectp;
1855 this->info.size = bfd_section_size (sectp);
1856 }
1857 else if (section_is_p (sectp->name, &names.abbrev))
1858 {
1859 this->abbrev.s.section = sectp;
1860 this->abbrev.size = bfd_section_size (sectp);
1861 }
1862 else if (section_is_p (sectp->name, &names.line))
1863 {
1864 this->line.s.section = sectp;
1865 this->line.size = bfd_section_size (sectp);
1866 }
1867 else if (section_is_p (sectp->name, &names.loc))
1868 {
1869 this->loc.s.section = sectp;
1870 this->loc.size = bfd_section_size (sectp);
1871 }
1872 else if (section_is_p (sectp->name, &names.loclists))
1873 {
1874 this->loclists.s.section = sectp;
1875 this->loclists.size = bfd_section_size (sectp);
1876 }
1877 else if (section_is_p (sectp->name, &names.macinfo))
1878 {
1879 this->macinfo.s.section = sectp;
1880 this->macinfo.size = bfd_section_size (sectp);
1881 }
1882 else if (section_is_p (sectp->name, &names.macro))
1883 {
1884 this->macro.s.section = sectp;
1885 this->macro.size = bfd_section_size (sectp);
1886 }
1887 else if (section_is_p (sectp->name, &names.str))
1888 {
1889 this->str.s.section = sectp;
1890 this->str.size = bfd_section_size (sectp);
1891 }
1892 else if (section_is_p (sectp->name, &names.str_offsets))
1893 {
1894 this->str_offsets.s.section = sectp;
1895 this->str_offsets.size = bfd_section_size (sectp);
1896 }
1897 else if (section_is_p (sectp->name, &names.line_str))
1898 {
1899 this->line_str.s.section = sectp;
1900 this->line_str.size = bfd_section_size (sectp);
1901 }
1902 else if (section_is_p (sectp->name, &names.addr))
1903 {
1904 this->addr.s.section = sectp;
1905 this->addr.size = bfd_section_size (sectp);
1906 }
1907 else if (section_is_p (sectp->name, &names.frame))
1908 {
1909 this->frame.s.section = sectp;
1910 this->frame.size = bfd_section_size (sectp);
1911 }
1912 else if (section_is_p (sectp->name, &names.eh_frame))
1913 {
1914 this->eh_frame.s.section = sectp;
1915 this->eh_frame.size = bfd_section_size (sectp);
1916 }
1917 else if (section_is_p (sectp->name, &names.ranges))
1918 {
1919 this->ranges.s.section = sectp;
1920 this->ranges.size = bfd_section_size (sectp);
1921 }
1922 else if (section_is_p (sectp->name, &names.rnglists))
1923 {
1924 this->rnglists.s.section = sectp;
1925 this->rnglists.size = bfd_section_size (sectp);
1926 }
1927 else if (section_is_p (sectp->name, &names.types))
1928 {
1929 struct dwarf2_section_info type_section;
1930
1931 memset (&type_section, 0, sizeof (type_section));
1932 type_section.s.section = sectp;
1933 type_section.size = bfd_section_size (sectp);
1934
1935 this->types.push_back (type_section);
1936 }
1937 else if (section_is_p (sectp->name, &names.gdb_index))
1938 {
1939 this->gdb_index.s.section = sectp;
1940 this->gdb_index.size = bfd_section_size (sectp);
1941 }
1942 else if (section_is_p (sectp->name, &names.debug_names))
1943 {
1944 this->debug_names.s.section = sectp;
1945 this->debug_names.size = bfd_section_size (sectp);
1946 }
1947 else if (section_is_p (sectp->name, &names.debug_aranges))
1948 {
1949 this->debug_aranges.s.section = sectp;
1950 this->debug_aranges.size = bfd_section_size (sectp);
1951 }
1952
1953 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1954 && bfd_section_vma (sectp) == 0)
1955 this->has_section_at_zero = true;
1956 }
1957
1958 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1959 SECTION_NAME. */
1960
1961 void
1962 dwarf2_get_section_info (struct objfile *objfile,
1963 enum dwarf2_section_enum sect,
1964 asection **sectp, const gdb_byte **bufp,
1965 bfd_size_type *sizep)
1966 {
1967 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
1968 struct dwarf2_section_info *info;
1969
1970 /* We may see an objfile without any DWARF, in which case we just
1971 return nothing. */
1972 if (data == NULL)
1973 {
1974 *sectp = NULL;
1975 *bufp = NULL;
1976 *sizep = 0;
1977 return;
1978 }
1979 switch (sect)
1980 {
1981 case DWARF2_DEBUG_FRAME:
1982 info = &data->frame;
1983 break;
1984 case DWARF2_EH_FRAME:
1985 info = &data->eh_frame;
1986 break;
1987 default:
1988 gdb_assert_not_reached ("unexpected section");
1989 }
1990
1991 info->read (objfile);
1992
1993 *sectp = info->get_bfd_section ();
1994 *bufp = info->buffer;
1995 *sizep = info->size;
1996 }
1997
1998 /* A helper function to find the sections for a .dwz file. */
1999
2000 static void
2001 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2002 {
2003 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2004
2005 /* Note that we only support the standard ELF names, because .dwz
2006 is ELF-only (at the time of writing). */
2007 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2008 {
2009 dwz_file->abbrev.s.section = sectp;
2010 dwz_file->abbrev.size = bfd_section_size (sectp);
2011 }
2012 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2013 {
2014 dwz_file->info.s.section = sectp;
2015 dwz_file->info.size = bfd_section_size (sectp);
2016 }
2017 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2018 {
2019 dwz_file->str.s.section = sectp;
2020 dwz_file->str.size = bfd_section_size (sectp);
2021 }
2022 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2023 {
2024 dwz_file->line.s.section = sectp;
2025 dwz_file->line.size = bfd_section_size (sectp);
2026 }
2027 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2028 {
2029 dwz_file->macro.s.section = sectp;
2030 dwz_file->macro.size = bfd_section_size (sectp);
2031 }
2032 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2033 {
2034 dwz_file->gdb_index.s.section = sectp;
2035 dwz_file->gdb_index.size = bfd_section_size (sectp);
2036 }
2037 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2038 {
2039 dwz_file->debug_names.s.section = sectp;
2040 dwz_file->debug_names.size = bfd_section_size (sectp);
2041 }
2042 }
2043
2044 /* See dwarf2read.h. */
2045
2046 struct dwz_file *
2047 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2048 {
2049 const char *filename;
2050 bfd_size_type buildid_len_arg;
2051 size_t buildid_len;
2052 bfd_byte *buildid;
2053
2054 if (dwarf2_per_objfile->dwz_file != NULL)
2055 return dwarf2_per_objfile->dwz_file.get ();
2056
2057 bfd_set_error (bfd_error_no_error);
2058 gdb::unique_xmalloc_ptr<char> data
2059 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2060 &buildid_len_arg, &buildid));
2061 if (data == NULL)
2062 {
2063 if (bfd_get_error () == bfd_error_no_error)
2064 return NULL;
2065 error (_("could not read '.gnu_debugaltlink' section: %s"),
2066 bfd_errmsg (bfd_get_error ()));
2067 }
2068
2069 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2070
2071 buildid_len = (size_t) buildid_len_arg;
2072
2073 filename = data.get ();
2074
2075 std::string abs_storage;
2076 if (!IS_ABSOLUTE_PATH (filename))
2077 {
2078 gdb::unique_xmalloc_ptr<char> abs
2079 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2080
2081 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2082 filename = abs_storage.c_str ();
2083 }
2084
2085 /* First try the file name given in the section. If that doesn't
2086 work, try to use the build-id instead. */
2087 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2088 if (dwz_bfd != NULL)
2089 {
2090 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2091 dwz_bfd.reset (nullptr);
2092 }
2093
2094 if (dwz_bfd == NULL)
2095 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2096
2097 if (dwz_bfd == nullptr)
2098 {
2099 gdb::unique_xmalloc_ptr<char> alt_filename;
2100 const char *origname = dwarf2_per_objfile->objfile->original_name;
2101
2102 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2103 buildid_len,
2104 origname,
2105 &alt_filename));
2106
2107 if (fd.get () >= 0)
2108 {
2109 /* File successfully retrieved from server. */
2110 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2111
2112 if (dwz_bfd == nullptr)
2113 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2114 alt_filename.get ());
2115 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2116 dwz_bfd.reset (nullptr);
2117 }
2118 }
2119
2120 if (dwz_bfd == NULL)
2121 error (_("could not find '.gnu_debugaltlink' file for %s"),
2122 objfile_name (dwarf2_per_objfile->objfile));
2123
2124 std::unique_ptr<struct dwz_file> result
2125 (new struct dwz_file (std::move (dwz_bfd)));
2126
2127 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2128 result.get ());
2129
2130 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2131 result->dwz_bfd.get ());
2132 dwarf2_per_objfile->dwz_file = std::move (result);
2133 return dwarf2_per_objfile->dwz_file.get ();
2134 }
2135 \f
2136 /* DWARF quick_symbols_functions support. */
2137
2138 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2139 unique line tables, so we maintain a separate table of all .debug_line
2140 derived entries to support the sharing.
2141 All the quick functions need is the list of file names. We discard the
2142 line_header when we're done and don't need to record it here. */
2143 struct quick_file_names
2144 {
2145 /* The data used to construct the hash key. */
2146 struct stmt_list_hash hash;
2147
2148 /* The number of entries in file_names, real_names. */
2149 unsigned int num_file_names;
2150
2151 /* The file names from the line table, after being run through
2152 file_full_name. */
2153 const char **file_names;
2154
2155 /* The file names from the line table after being run through
2156 gdb_realpath. These are computed lazily. */
2157 const char **real_names;
2158 };
2159
2160 /* When using the index (and thus not using psymtabs), each CU has an
2161 object of this type. This is used to hold information needed by
2162 the various "quick" methods. */
2163 struct dwarf2_per_cu_quick_data
2164 {
2165 /* The file table. This can be NULL if there was no file table
2166 or it's currently not read in.
2167 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2168 struct quick_file_names *file_names;
2169
2170 /* The corresponding symbol table. This is NULL if symbols for this
2171 CU have not yet been read. */
2172 struct compunit_symtab *compunit_symtab;
2173
2174 /* A temporary mark bit used when iterating over all CUs in
2175 expand_symtabs_matching. */
2176 unsigned int mark : 1;
2177
2178 /* True if we've tried to read the file table and found there isn't one.
2179 There will be no point in trying to read it again next time. */
2180 unsigned int no_file_data : 1;
2181 };
2182
2183 /* Utility hash function for a stmt_list_hash. */
2184
2185 static hashval_t
2186 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2187 {
2188 hashval_t v = 0;
2189
2190 if (stmt_list_hash->dwo_unit != NULL)
2191 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2192 v += to_underlying (stmt_list_hash->line_sect_off);
2193 return v;
2194 }
2195
2196 /* Utility equality function for a stmt_list_hash. */
2197
2198 static int
2199 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2200 const struct stmt_list_hash *rhs)
2201 {
2202 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2203 return 0;
2204 if (lhs->dwo_unit != NULL
2205 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2206 return 0;
2207
2208 return lhs->line_sect_off == rhs->line_sect_off;
2209 }
2210
2211 /* Hash function for a quick_file_names. */
2212
2213 static hashval_t
2214 hash_file_name_entry (const void *e)
2215 {
2216 const struct quick_file_names *file_data
2217 = (const struct quick_file_names *) e;
2218
2219 return hash_stmt_list_entry (&file_data->hash);
2220 }
2221
2222 /* Equality function for a quick_file_names. */
2223
2224 static int
2225 eq_file_name_entry (const void *a, const void *b)
2226 {
2227 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2228 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2229
2230 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2231 }
2232
2233 /* Delete function for a quick_file_names. */
2234
2235 static void
2236 delete_file_name_entry (void *e)
2237 {
2238 struct quick_file_names *file_data = (struct quick_file_names *) e;
2239 int i;
2240
2241 for (i = 0; i < file_data->num_file_names; ++i)
2242 {
2243 xfree ((void*) file_data->file_names[i]);
2244 if (file_data->real_names)
2245 xfree ((void*) file_data->real_names[i]);
2246 }
2247
2248 /* The space for the struct itself lives on objfile_obstack,
2249 so we don't free it here. */
2250 }
2251
2252 /* Create a quick_file_names hash table. */
2253
2254 static htab_up
2255 create_quick_file_names_table (unsigned int nr_initial_entries)
2256 {
2257 return htab_up (htab_create_alloc (nr_initial_entries,
2258 hash_file_name_entry, eq_file_name_entry,
2259 delete_file_name_entry, xcalloc, xfree));
2260 }
2261
2262 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2263 have to be created afterwards. You should call age_cached_comp_units after
2264 processing PER_CU->CU. dw2_setup must have been already called. */
2265
2266 static void
2267 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2268 {
2269 if (per_cu->is_debug_types)
2270 load_full_type_unit (per_cu);
2271 else
2272 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2273
2274 if (per_cu->cu == NULL)
2275 return; /* Dummy CU. */
2276
2277 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2278 }
2279
2280 /* Read in the symbols for PER_CU. */
2281
2282 static void
2283 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2284 {
2285 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2286
2287 /* Skip type_unit_groups, reading the type units they contain
2288 is handled elsewhere. */
2289 if (per_cu->type_unit_group_p ())
2290 return;
2291
2292 /* The destructor of dwarf2_queue_guard frees any entries left on
2293 the queue. After this point we're guaranteed to leave this function
2294 with the dwarf queue empty. */
2295 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2296
2297 if (dwarf2_per_objfile->using_index
2298 ? per_cu->v.quick->compunit_symtab == NULL
2299 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2300 {
2301 queue_comp_unit (per_cu, language_minimal);
2302 load_cu (per_cu, skip_partial);
2303
2304 /* If we just loaded a CU from a DWO, and we're working with an index
2305 that may badly handle TUs, load all the TUs in that DWO as well.
2306 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2307 if (!per_cu->is_debug_types
2308 && per_cu->cu != NULL
2309 && per_cu->cu->dwo_unit != NULL
2310 && dwarf2_per_objfile->index_table != NULL
2311 && dwarf2_per_objfile->index_table->version <= 7
2312 /* DWP files aren't supported yet. */
2313 && get_dwp_file (dwarf2_per_objfile) == NULL)
2314 queue_and_load_all_dwo_tus (per_cu);
2315 }
2316
2317 process_queue (dwarf2_per_objfile);
2318
2319 /* Age the cache, releasing compilation units that have not
2320 been used recently. */
2321 age_cached_comp_units (dwarf2_per_objfile);
2322 }
2323
2324 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2325 the objfile from which this CU came. Returns the resulting symbol
2326 table. */
2327
2328 static struct compunit_symtab *
2329 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2330 {
2331 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2332
2333 gdb_assert (dwarf2_per_objfile->using_index);
2334 if (!per_cu->v.quick->compunit_symtab)
2335 {
2336 free_cached_comp_units freer (dwarf2_per_objfile);
2337 scoped_restore decrementer = increment_reading_symtab ();
2338 dw2_do_instantiate_symtab (per_cu, skip_partial);
2339 process_cu_includes (dwarf2_per_objfile);
2340 }
2341
2342 return per_cu->v.quick->compunit_symtab;
2343 }
2344
2345 /* See declaration. */
2346
2347 dwarf2_per_cu_data *
2348 dwarf2_per_objfile::get_cutu (int index)
2349 {
2350 if (index >= this->all_comp_units.size ())
2351 {
2352 index -= this->all_comp_units.size ();
2353 gdb_assert (index < this->all_type_units.size ());
2354 return &this->all_type_units[index]->per_cu;
2355 }
2356
2357 return this->all_comp_units[index];
2358 }
2359
2360 /* See declaration. */
2361
2362 dwarf2_per_cu_data *
2363 dwarf2_per_objfile::get_cu (int index)
2364 {
2365 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2366
2367 return this->all_comp_units[index];
2368 }
2369
2370 /* See declaration. */
2371
2372 signatured_type *
2373 dwarf2_per_objfile::get_tu (int index)
2374 {
2375 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2376
2377 return this->all_type_units[index];
2378 }
2379
2380 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2381 objfile_obstack, and constructed with the specified field
2382 values. */
2383
2384 static dwarf2_per_cu_data *
2385 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2386 struct dwarf2_section_info *section,
2387 int is_dwz,
2388 sect_offset sect_off, ULONGEST length)
2389 {
2390 struct objfile *objfile = dwarf2_per_objfile->objfile;
2391 dwarf2_per_cu_data *the_cu
2392 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2393 struct dwarf2_per_cu_data);
2394 the_cu->sect_off = sect_off;
2395 the_cu->length = length;
2396 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2397 the_cu->section = section;
2398 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2399 struct dwarf2_per_cu_quick_data);
2400 the_cu->is_dwz = is_dwz;
2401 return the_cu;
2402 }
2403
2404 /* A helper for create_cus_from_index that handles a given list of
2405 CUs. */
2406
2407 static void
2408 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2409 const gdb_byte *cu_list, offset_type n_elements,
2410 struct dwarf2_section_info *section,
2411 int is_dwz)
2412 {
2413 for (offset_type i = 0; i < n_elements; i += 2)
2414 {
2415 gdb_static_assert (sizeof (ULONGEST) >= 8);
2416
2417 sect_offset sect_off
2418 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2419 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2420 cu_list += 2 * 8;
2421
2422 dwarf2_per_cu_data *per_cu
2423 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2424 sect_off, length);
2425 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2426 }
2427 }
2428
2429 /* Read the CU list from the mapped index, and use it to create all
2430 the CU objects for this objfile. */
2431
2432 static void
2433 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2434 const gdb_byte *cu_list, offset_type cu_list_elements,
2435 const gdb_byte *dwz_list, offset_type dwz_elements)
2436 {
2437 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2438 dwarf2_per_objfile->all_comp_units.reserve
2439 ((cu_list_elements + dwz_elements) / 2);
2440
2441 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2442 &dwarf2_per_objfile->info, 0);
2443
2444 if (dwz_elements == 0)
2445 return;
2446
2447 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2448 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2449 &dwz->info, 1);
2450 }
2451
2452 /* Create the signatured type hash table from the index. */
2453
2454 static void
2455 create_signatured_type_table_from_index
2456 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2457 struct dwarf2_section_info *section,
2458 const gdb_byte *bytes,
2459 offset_type elements)
2460 {
2461 struct objfile *objfile = dwarf2_per_objfile->objfile;
2462
2463 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2464 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2465
2466 htab_up sig_types_hash = allocate_signatured_type_table ();
2467
2468 for (offset_type i = 0; i < elements; i += 3)
2469 {
2470 struct signatured_type *sig_type;
2471 ULONGEST signature;
2472 void **slot;
2473 cu_offset type_offset_in_tu;
2474
2475 gdb_static_assert (sizeof (ULONGEST) >= 8);
2476 sect_offset sect_off
2477 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2478 type_offset_in_tu
2479 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2480 BFD_ENDIAN_LITTLE);
2481 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2482 bytes += 3 * 8;
2483
2484 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2485 struct signatured_type);
2486 sig_type->signature = signature;
2487 sig_type->type_offset_in_tu = type_offset_in_tu;
2488 sig_type->per_cu.is_debug_types = 1;
2489 sig_type->per_cu.section = section;
2490 sig_type->per_cu.sect_off = sect_off;
2491 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2492 sig_type->per_cu.v.quick
2493 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2494 struct dwarf2_per_cu_quick_data);
2495
2496 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2497 *slot = sig_type;
2498
2499 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2500 }
2501
2502 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2503 }
2504
2505 /* Create the signatured type hash table from .debug_names. */
2506
2507 static void
2508 create_signatured_type_table_from_debug_names
2509 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2510 const mapped_debug_names &map,
2511 struct dwarf2_section_info *section,
2512 struct dwarf2_section_info *abbrev_section)
2513 {
2514 struct objfile *objfile = dwarf2_per_objfile->objfile;
2515
2516 section->read (objfile);
2517 abbrev_section->read (objfile);
2518
2519 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2520 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2521
2522 htab_up sig_types_hash = allocate_signatured_type_table ();
2523
2524 for (uint32_t i = 0; i < map.tu_count; ++i)
2525 {
2526 struct signatured_type *sig_type;
2527 void **slot;
2528
2529 sect_offset sect_off
2530 = (sect_offset) (extract_unsigned_integer
2531 (map.tu_table_reordered + i * map.offset_size,
2532 map.offset_size,
2533 map.dwarf5_byte_order));
2534
2535 comp_unit_head cu_header;
2536 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2537 abbrev_section,
2538 section->buffer + to_underlying (sect_off),
2539 rcuh_kind::TYPE);
2540
2541 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2542 struct signatured_type);
2543 sig_type->signature = cu_header.signature;
2544 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2545 sig_type->per_cu.is_debug_types = 1;
2546 sig_type->per_cu.section = section;
2547 sig_type->per_cu.sect_off = sect_off;
2548 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2549 sig_type->per_cu.v.quick
2550 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2551 struct dwarf2_per_cu_quick_data);
2552
2553 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2554 *slot = sig_type;
2555
2556 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2557 }
2558
2559 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2560 }
2561
2562 /* Read the address map data from the mapped index, and use it to
2563 populate the objfile's psymtabs_addrmap. */
2564
2565 static void
2566 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2567 struct mapped_index *index)
2568 {
2569 struct objfile *objfile = dwarf2_per_objfile->objfile;
2570 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2571 const gdb_byte *iter, *end;
2572 struct addrmap *mutable_map;
2573 CORE_ADDR baseaddr;
2574
2575 auto_obstack temp_obstack;
2576
2577 mutable_map = addrmap_create_mutable (&temp_obstack);
2578
2579 iter = index->address_table.data ();
2580 end = iter + index->address_table.size ();
2581
2582 baseaddr = objfile->text_section_offset ();
2583
2584 while (iter < end)
2585 {
2586 ULONGEST hi, lo, cu_index;
2587 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2588 iter += 8;
2589 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2590 iter += 8;
2591 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2592 iter += 4;
2593
2594 if (lo > hi)
2595 {
2596 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2597 hex_string (lo), hex_string (hi));
2598 continue;
2599 }
2600
2601 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2602 {
2603 complaint (_(".gdb_index address table has invalid CU number %u"),
2604 (unsigned) cu_index);
2605 continue;
2606 }
2607
2608 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2609 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2610 addrmap_set_empty (mutable_map, lo, hi - 1,
2611 dwarf2_per_objfile->get_cu (cu_index));
2612 }
2613
2614 objfile->partial_symtabs->psymtabs_addrmap
2615 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2616 }
2617
2618 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2619 populate the objfile's psymtabs_addrmap. */
2620
2621 static void
2622 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2623 struct dwarf2_section_info *section)
2624 {
2625 struct objfile *objfile = dwarf2_per_objfile->objfile;
2626 bfd *abfd = objfile->obfd;
2627 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2628 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2629
2630 auto_obstack temp_obstack;
2631 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2632
2633 std::unordered_map<sect_offset,
2634 dwarf2_per_cu_data *,
2635 gdb::hash_enum<sect_offset>>
2636 debug_info_offset_to_per_cu;
2637 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2638 {
2639 const auto insertpair
2640 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2641 if (!insertpair.second)
2642 {
2643 warning (_("Section .debug_aranges in %s has duplicate "
2644 "debug_info_offset %s, ignoring .debug_aranges."),
2645 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2646 return;
2647 }
2648 }
2649
2650 section->read (objfile);
2651
2652 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2653
2654 const gdb_byte *addr = section->buffer;
2655
2656 while (addr < section->buffer + section->size)
2657 {
2658 const gdb_byte *const entry_addr = addr;
2659 unsigned int bytes_read;
2660
2661 const LONGEST entry_length = read_initial_length (abfd, addr,
2662 &bytes_read);
2663 addr += bytes_read;
2664
2665 const gdb_byte *const entry_end = addr + entry_length;
2666 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2667 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2668 if (addr + entry_length > section->buffer + section->size)
2669 {
2670 warning (_("Section .debug_aranges in %s entry at offset %s "
2671 "length %s exceeds section length %s, "
2672 "ignoring .debug_aranges."),
2673 objfile_name (objfile),
2674 plongest (entry_addr - section->buffer),
2675 plongest (bytes_read + entry_length),
2676 pulongest (section->size));
2677 return;
2678 }
2679
2680 /* The version number. */
2681 const uint16_t version = read_2_bytes (abfd, addr);
2682 addr += 2;
2683 if (version != 2)
2684 {
2685 warning (_("Section .debug_aranges in %s entry at offset %s "
2686 "has unsupported version %d, ignoring .debug_aranges."),
2687 objfile_name (objfile),
2688 plongest (entry_addr - section->buffer), version);
2689 return;
2690 }
2691
2692 const uint64_t debug_info_offset
2693 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2694 addr += offset_size;
2695 const auto per_cu_it
2696 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2697 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2698 {
2699 warning (_("Section .debug_aranges in %s entry at offset %s "
2700 "debug_info_offset %s does not exists, "
2701 "ignoring .debug_aranges."),
2702 objfile_name (objfile),
2703 plongest (entry_addr - section->buffer),
2704 pulongest (debug_info_offset));
2705 return;
2706 }
2707 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2708
2709 const uint8_t address_size = *addr++;
2710 if (address_size < 1 || address_size > 8)
2711 {
2712 warning (_("Section .debug_aranges in %s entry at offset %s "
2713 "address_size %u is invalid, ignoring .debug_aranges."),
2714 objfile_name (objfile),
2715 plongest (entry_addr - section->buffer), address_size);
2716 return;
2717 }
2718
2719 const uint8_t segment_selector_size = *addr++;
2720 if (segment_selector_size != 0)
2721 {
2722 warning (_("Section .debug_aranges in %s entry at offset %s "
2723 "segment_selector_size %u is not supported, "
2724 "ignoring .debug_aranges."),
2725 objfile_name (objfile),
2726 plongest (entry_addr - section->buffer),
2727 segment_selector_size);
2728 return;
2729 }
2730
2731 /* Must pad to an alignment boundary that is twice the address
2732 size. It is undocumented by the DWARF standard but GCC does
2733 use it. */
2734 for (size_t padding = ((-(addr - section->buffer))
2735 & (2 * address_size - 1));
2736 padding > 0; padding--)
2737 if (*addr++ != 0)
2738 {
2739 warning (_("Section .debug_aranges in %s entry at offset %s "
2740 "padding is not zero, ignoring .debug_aranges."),
2741 objfile_name (objfile),
2742 plongest (entry_addr - section->buffer));
2743 return;
2744 }
2745
2746 for (;;)
2747 {
2748 if (addr + 2 * address_size > entry_end)
2749 {
2750 warning (_("Section .debug_aranges in %s entry at offset %s "
2751 "address list is not properly terminated, "
2752 "ignoring .debug_aranges."),
2753 objfile_name (objfile),
2754 plongest (entry_addr - section->buffer));
2755 return;
2756 }
2757 ULONGEST start = extract_unsigned_integer (addr, address_size,
2758 dwarf5_byte_order);
2759 addr += address_size;
2760 ULONGEST length = extract_unsigned_integer (addr, address_size,
2761 dwarf5_byte_order);
2762 addr += address_size;
2763 if (start == 0 && length == 0)
2764 break;
2765 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2766 {
2767 /* Symbol was eliminated due to a COMDAT group. */
2768 continue;
2769 }
2770 ULONGEST end = start + length;
2771 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2772 - baseaddr);
2773 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2774 - baseaddr);
2775 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2776 }
2777 }
2778
2779 objfile->partial_symtabs->psymtabs_addrmap
2780 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2781 }
2782
2783 /* Find a slot in the mapped index INDEX for the object named NAME.
2784 If NAME is found, set *VEC_OUT to point to the CU vector in the
2785 constant pool and return true. If NAME cannot be found, return
2786 false. */
2787
2788 static bool
2789 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2790 offset_type **vec_out)
2791 {
2792 offset_type hash;
2793 offset_type slot, step;
2794 int (*cmp) (const char *, const char *);
2795
2796 gdb::unique_xmalloc_ptr<char> without_params;
2797 if (current_language->la_language == language_cplus
2798 || current_language->la_language == language_fortran
2799 || current_language->la_language == language_d)
2800 {
2801 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2802 not contain any. */
2803
2804 if (strchr (name, '(') != NULL)
2805 {
2806 without_params = cp_remove_params (name);
2807
2808 if (without_params != NULL)
2809 name = without_params.get ();
2810 }
2811 }
2812
2813 /* Index version 4 did not support case insensitive searches. But the
2814 indices for case insensitive languages are built in lowercase, therefore
2815 simulate our NAME being searched is also lowercased. */
2816 hash = mapped_index_string_hash ((index->version == 4
2817 && case_sensitivity == case_sensitive_off
2818 ? 5 : index->version),
2819 name);
2820
2821 slot = hash & (index->symbol_table.size () - 1);
2822 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2823 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2824
2825 for (;;)
2826 {
2827 const char *str;
2828
2829 const auto &bucket = index->symbol_table[slot];
2830 if (bucket.name == 0 && bucket.vec == 0)
2831 return false;
2832
2833 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2834 if (!cmp (name, str))
2835 {
2836 *vec_out = (offset_type *) (index->constant_pool
2837 + MAYBE_SWAP (bucket.vec));
2838 return true;
2839 }
2840
2841 slot = (slot + step) & (index->symbol_table.size () - 1);
2842 }
2843 }
2844
2845 /* A helper function that reads the .gdb_index from BUFFER and fills
2846 in MAP. FILENAME is the name of the file containing the data;
2847 it is used for error reporting. DEPRECATED_OK is true if it is
2848 ok to use deprecated sections.
2849
2850 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2851 out parameters that are filled in with information about the CU and
2852 TU lists in the section.
2853
2854 Returns true if all went well, false otherwise. */
2855
2856 static bool
2857 read_gdb_index_from_buffer (struct objfile *objfile,
2858 const char *filename,
2859 bool deprecated_ok,
2860 gdb::array_view<const gdb_byte> buffer,
2861 struct mapped_index *map,
2862 const gdb_byte **cu_list,
2863 offset_type *cu_list_elements,
2864 const gdb_byte **types_list,
2865 offset_type *types_list_elements)
2866 {
2867 const gdb_byte *addr = &buffer[0];
2868
2869 /* Version check. */
2870 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2871 /* Versions earlier than 3 emitted every copy of a psymbol. This
2872 causes the index to behave very poorly for certain requests. Version 3
2873 contained incomplete addrmap. So, it seems better to just ignore such
2874 indices. */
2875 if (version < 4)
2876 {
2877 static int warning_printed = 0;
2878 if (!warning_printed)
2879 {
2880 warning (_("Skipping obsolete .gdb_index section in %s."),
2881 filename);
2882 warning_printed = 1;
2883 }
2884 return 0;
2885 }
2886 /* Index version 4 uses a different hash function than index version
2887 5 and later.
2888
2889 Versions earlier than 6 did not emit psymbols for inlined
2890 functions. Using these files will cause GDB not to be able to
2891 set breakpoints on inlined functions by name, so we ignore these
2892 indices unless the user has done
2893 "set use-deprecated-index-sections on". */
2894 if (version < 6 && !deprecated_ok)
2895 {
2896 static int warning_printed = 0;
2897 if (!warning_printed)
2898 {
2899 warning (_("\
2900 Skipping deprecated .gdb_index section in %s.\n\
2901 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2902 to use the section anyway."),
2903 filename);
2904 warning_printed = 1;
2905 }
2906 return 0;
2907 }
2908 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2909 of the TU (for symbols coming from TUs),
2910 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2911 Plus gold-generated indices can have duplicate entries for global symbols,
2912 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2913 These are just performance bugs, and we can't distinguish gdb-generated
2914 indices from gold-generated ones, so issue no warning here. */
2915
2916 /* Indexes with higher version than the one supported by GDB may be no
2917 longer backward compatible. */
2918 if (version > 8)
2919 return 0;
2920
2921 map->version = version;
2922
2923 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2924
2925 int i = 0;
2926 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2927 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2928 / 8);
2929 ++i;
2930
2931 *types_list = addr + MAYBE_SWAP (metadata[i]);
2932 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2933 - MAYBE_SWAP (metadata[i]))
2934 / 8);
2935 ++i;
2936
2937 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2938 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2939 map->address_table
2940 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2941 ++i;
2942
2943 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2944 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2945 map->symbol_table
2946 = gdb::array_view<mapped_index::symbol_table_slot>
2947 ((mapped_index::symbol_table_slot *) symbol_table,
2948 (mapped_index::symbol_table_slot *) symbol_table_end);
2949
2950 ++i;
2951 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2952
2953 return 1;
2954 }
2955
2956 /* Callback types for dwarf2_read_gdb_index. */
2957
2958 typedef gdb::function_view
2959 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2960 get_gdb_index_contents_ftype;
2961 typedef gdb::function_view
2962 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2963 get_gdb_index_contents_dwz_ftype;
2964
2965 /* Read .gdb_index. If everything went ok, initialize the "quick"
2966 elements of all the CUs and return 1. Otherwise, return 0. */
2967
2968 static int
2969 dwarf2_read_gdb_index
2970 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2971 get_gdb_index_contents_ftype get_gdb_index_contents,
2972 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2973 {
2974 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2975 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2976 struct dwz_file *dwz;
2977 struct objfile *objfile = dwarf2_per_objfile->objfile;
2978
2979 gdb::array_view<const gdb_byte> main_index_contents
2980 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
2981
2982 if (main_index_contents.empty ())
2983 return 0;
2984
2985 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2986 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
2987 use_deprecated_index_sections,
2988 main_index_contents, map.get (), &cu_list,
2989 &cu_list_elements, &types_list,
2990 &types_list_elements))
2991 return 0;
2992
2993 /* Don't use the index if it's empty. */
2994 if (map->symbol_table.empty ())
2995 return 0;
2996
2997 /* If there is a .dwz file, read it so we can get its CU list as
2998 well. */
2999 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3000 if (dwz != NULL)
3001 {
3002 struct mapped_index dwz_map;
3003 const gdb_byte *dwz_types_ignore;
3004 offset_type dwz_types_elements_ignore;
3005
3006 gdb::array_view<const gdb_byte> dwz_index_content
3007 = get_gdb_index_contents_dwz (objfile, dwz);
3008
3009 if (dwz_index_content.empty ())
3010 return 0;
3011
3012 if (!read_gdb_index_from_buffer (objfile,
3013 bfd_get_filename (dwz->dwz_bfd.get ()),
3014 1, dwz_index_content, &dwz_map,
3015 &dwz_list, &dwz_list_elements,
3016 &dwz_types_ignore,
3017 &dwz_types_elements_ignore))
3018 {
3019 warning (_("could not read '.gdb_index' section from %s; skipping"),
3020 bfd_get_filename (dwz->dwz_bfd.get ()));
3021 return 0;
3022 }
3023 }
3024
3025 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3026 dwz_list, dwz_list_elements);
3027
3028 if (types_list_elements)
3029 {
3030 /* We can only handle a single .debug_types when we have an
3031 index. */
3032 if (dwarf2_per_objfile->types.size () != 1)
3033 return 0;
3034
3035 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3036
3037 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3038 types_list, types_list_elements);
3039 }
3040
3041 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3042
3043 dwarf2_per_objfile->index_table = std::move (map);
3044 dwarf2_per_objfile->using_index = 1;
3045 dwarf2_per_objfile->quick_file_names_table =
3046 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3047
3048 return 1;
3049 }
3050
3051 /* die_reader_func for dw2_get_file_names. */
3052
3053 static void
3054 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3055 const gdb_byte *info_ptr,
3056 struct die_info *comp_unit_die)
3057 {
3058 struct dwarf2_cu *cu = reader->cu;
3059 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3060 struct dwarf2_per_objfile *dwarf2_per_objfile
3061 = cu->per_cu->dwarf2_per_objfile;
3062 struct objfile *objfile = dwarf2_per_objfile->objfile;
3063 struct dwarf2_per_cu_data *lh_cu;
3064 struct attribute *attr;
3065 void **slot;
3066 struct quick_file_names *qfn;
3067
3068 gdb_assert (! this_cu->is_debug_types);
3069
3070 /* Our callers never want to match partial units -- instead they
3071 will match the enclosing full CU. */
3072 if (comp_unit_die->tag == DW_TAG_partial_unit)
3073 {
3074 this_cu->v.quick->no_file_data = 1;
3075 return;
3076 }
3077
3078 lh_cu = this_cu;
3079 slot = NULL;
3080
3081 line_header_up lh;
3082 sect_offset line_offset {};
3083
3084 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3085 if (attr != nullptr)
3086 {
3087 struct quick_file_names find_entry;
3088
3089 line_offset = (sect_offset) DW_UNSND (attr);
3090
3091 /* We may have already read in this line header (TU line header sharing).
3092 If we have we're done. */
3093 find_entry.hash.dwo_unit = cu->dwo_unit;
3094 find_entry.hash.line_sect_off = line_offset;
3095 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3096 &find_entry, INSERT);
3097 if (*slot != NULL)
3098 {
3099 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3100 return;
3101 }
3102
3103 lh = dwarf_decode_line_header (line_offset, cu);
3104 }
3105 if (lh == NULL)
3106 {
3107 lh_cu->v.quick->no_file_data = 1;
3108 return;
3109 }
3110
3111 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3112 qfn->hash.dwo_unit = cu->dwo_unit;
3113 qfn->hash.line_sect_off = line_offset;
3114 gdb_assert (slot != NULL);
3115 *slot = qfn;
3116
3117 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3118
3119 int offset = 0;
3120 if (strcmp (fnd.name, "<unknown>") != 0)
3121 ++offset;
3122
3123 qfn->num_file_names = offset + lh->file_names_size ();
3124 qfn->file_names =
3125 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3126 if (offset != 0)
3127 qfn->file_names[0] = xstrdup (fnd.name);
3128 for (int i = 0; i < lh->file_names_size (); ++i)
3129 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3130 fnd.comp_dir).release ();
3131 qfn->real_names = NULL;
3132
3133 lh_cu->v.quick->file_names = qfn;
3134 }
3135
3136 /* A helper for the "quick" functions which attempts to read the line
3137 table for THIS_CU. */
3138
3139 static struct quick_file_names *
3140 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3141 {
3142 /* This should never be called for TUs. */
3143 gdb_assert (! this_cu->is_debug_types);
3144 /* Nor type unit groups. */
3145 gdb_assert (! this_cu->type_unit_group_p ());
3146
3147 if (this_cu->v.quick->file_names != NULL)
3148 return this_cu->v.quick->file_names;
3149 /* If we know there is no line data, no point in looking again. */
3150 if (this_cu->v.quick->no_file_data)
3151 return NULL;
3152
3153 cutu_reader reader (this_cu);
3154 if (!reader.dummy_p)
3155 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3156
3157 if (this_cu->v.quick->no_file_data)
3158 return NULL;
3159 return this_cu->v.quick->file_names;
3160 }
3161
3162 /* A helper for the "quick" functions which computes and caches the
3163 real path for a given file name from the line table. */
3164
3165 static const char *
3166 dw2_get_real_path (struct objfile *objfile,
3167 struct quick_file_names *qfn, int index)
3168 {
3169 if (qfn->real_names == NULL)
3170 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3171 qfn->num_file_names, const char *);
3172
3173 if (qfn->real_names[index] == NULL)
3174 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3175
3176 return qfn->real_names[index];
3177 }
3178
3179 static struct symtab *
3180 dw2_find_last_source_symtab (struct objfile *objfile)
3181 {
3182 struct dwarf2_per_objfile *dwarf2_per_objfile
3183 = get_dwarf2_per_objfile (objfile);
3184 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3185 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3186
3187 if (cust == NULL)
3188 return NULL;
3189
3190 return compunit_primary_filetab (cust);
3191 }
3192
3193 /* Traversal function for dw2_forget_cached_source_info. */
3194
3195 static int
3196 dw2_free_cached_file_names (void **slot, void *info)
3197 {
3198 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3199
3200 if (file_data->real_names)
3201 {
3202 int i;
3203
3204 for (i = 0; i < file_data->num_file_names; ++i)
3205 {
3206 xfree ((void*) file_data->real_names[i]);
3207 file_data->real_names[i] = NULL;
3208 }
3209 }
3210
3211 return 1;
3212 }
3213
3214 static void
3215 dw2_forget_cached_source_info (struct objfile *objfile)
3216 {
3217 struct dwarf2_per_objfile *dwarf2_per_objfile
3218 = get_dwarf2_per_objfile (objfile);
3219
3220 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3221 dw2_free_cached_file_names, NULL);
3222 }
3223
3224 /* Helper function for dw2_map_symtabs_matching_filename that expands
3225 the symtabs and calls the iterator. */
3226
3227 static int
3228 dw2_map_expand_apply (struct objfile *objfile,
3229 struct dwarf2_per_cu_data *per_cu,
3230 const char *name, const char *real_path,
3231 gdb::function_view<bool (symtab *)> callback)
3232 {
3233 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3234
3235 /* Don't visit already-expanded CUs. */
3236 if (per_cu->v.quick->compunit_symtab)
3237 return 0;
3238
3239 /* This may expand more than one symtab, and we want to iterate over
3240 all of them. */
3241 dw2_instantiate_symtab (per_cu, false);
3242
3243 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3244 last_made, callback);
3245 }
3246
3247 /* Implementation of the map_symtabs_matching_filename method. */
3248
3249 static bool
3250 dw2_map_symtabs_matching_filename
3251 (struct objfile *objfile, const char *name, const char *real_path,
3252 gdb::function_view<bool (symtab *)> callback)
3253 {
3254 const char *name_basename = lbasename (name);
3255 struct dwarf2_per_objfile *dwarf2_per_objfile
3256 = get_dwarf2_per_objfile (objfile);
3257
3258 /* The rule is CUs specify all the files, including those used by
3259 any TU, so there's no need to scan TUs here. */
3260
3261 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3262 {
3263 /* We only need to look at symtabs not already expanded. */
3264 if (per_cu->v.quick->compunit_symtab)
3265 continue;
3266
3267 quick_file_names *file_data = dw2_get_file_names (per_cu);
3268 if (file_data == NULL)
3269 continue;
3270
3271 for (int j = 0; j < file_data->num_file_names; ++j)
3272 {
3273 const char *this_name = file_data->file_names[j];
3274 const char *this_real_name;
3275
3276 if (compare_filenames_for_search (this_name, name))
3277 {
3278 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3279 callback))
3280 return true;
3281 continue;
3282 }
3283
3284 /* Before we invoke realpath, which can get expensive when many
3285 files are involved, do a quick comparison of the basenames. */
3286 if (! basenames_may_differ
3287 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3288 continue;
3289
3290 this_real_name = dw2_get_real_path (objfile, file_data, j);
3291 if (compare_filenames_for_search (this_real_name, name))
3292 {
3293 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3294 callback))
3295 return true;
3296 continue;
3297 }
3298
3299 if (real_path != NULL)
3300 {
3301 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3302 gdb_assert (IS_ABSOLUTE_PATH (name));
3303 if (this_real_name != NULL
3304 && FILENAME_CMP (real_path, this_real_name) == 0)
3305 {
3306 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3307 callback))
3308 return true;
3309 continue;
3310 }
3311 }
3312 }
3313 }
3314
3315 return false;
3316 }
3317
3318 /* Struct used to manage iterating over all CUs looking for a symbol. */
3319
3320 struct dw2_symtab_iterator
3321 {
3322 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3323 struct dwarf2_per_objfile *dwarf2_per_objfile;
3324 /* If set, only look for symbols that match that block. Valid values are
3325 GLOBAL_BLOCK and STATIC_BLOCK. */
3326 gdb::optional<block_enum> block_index;
3327 /* The kind of symbol we're looking for. */
3328 domain_enum domain;
3329 /* The list of CUs from the index entry of the symbol,
3330 or NULL if not found. */
3331 offset_type *vec;
3332 /* The next element in VEC to look at. */
3333 int next;
3334 /* The number of elements in VEC, or zero if there is no match. */
3335 int length;
3336 /* Have we seen a global version of the symbol?
3337 If so we can ignore all further global instances.
3338 This is to work around gold/15646, inefficient gold-generated
3339 indices. */
3340 int global_seen;
3341 };
3342
3343 /* Initialize the index symtab iterator ITER. */
3344
3345 static void
3346 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3347 struct dwarf2_per_objfile *dwarf2_per_objfile,
3348 gdb::optional<block_enum> block_index,
3349 domain_enum domain,
3350 const char *name)
3351 {
3352 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3353 iter->block_index = block_index;
3354 iter->domain = domain;
3355 iter->next = 0;
3356 iter->global_seen = 0;
3357
3358 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3359
3360 /* index is NULL if OBJF_READNOW. */
3361 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3362 iter->length = MAYBE_SWAP (*iter->vec);
3363 else
3364 {
3365 iter->vec = NULL;
3366 iter->length = 0;
3367 }
3368 }
3369
3370 /* Return the next matching CU or NULL if there are no more. */
3371
3372 static struct dwarf2_per_cu_data *
3373 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3374 {
3375 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3376
3377 for ( ; iter->next < iter->length; ++iter->next)
3378 {
3379 offset_type cu_index_and_attrs =
3380 MAYBE_SWAP (iter->vec[iter->next + 1]);
3381 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3382 gdb_index_symbol_kind symbol_kind =
3383 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3384 /* Only check the symbol attributes if they're present.
3385 Indices prior to version 7 don't record them,
3386 and indices >= 7 may elide them for certain symbols
3387 (gold does this). */
3388 int attrs_valid =
3389 (dwarf2_per_objfile->index_table->version >= 7
3390 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3391
3392 /* Don't crash on bad data. */
3393 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3394 + dwarf2_per_objfile->all_type_units.size ()))
3395 {
3396 complaint (_(".gdb_index entry has bad CU index"
3397 " [in module %s]"),
3398 objfile_name (dwarf2_per_objfile->objfile));
3399 continue;
3400 }
3401
3402 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3403
3404 /* Skip if already read in. */
3405 if (per_cu->v.quick->compunit_symtab)
3406 continue;
3407
3408 /* Check static vs global. */
3409 if (attrs_valid)
3410 {
3411 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3412
3413 if (iter->block_index.has_value ())
3414 {
3415 bool want_static = *iter->block_index == STATIC_BLOCK;
3416
3417 if (is_static != want_static)
3418 continue;
3419 }
3420
3421 /* Work around gold/15646. */
3422 if (!is_static && iter->global_seen)
3423 continue;
3424 if (!is_static)
3425 iter->global_seen = 1;
3426 }
3427
3428 /* Only check the symbol's kind if it has one. */
3429 if (attrs_valid)
3430 {
3431 switch (iter->domain)
3432 {
3433 case VAR_DOMAIN:
3434 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3435 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3436 /* Some types are also in VAR_DOMAIN. */
3437 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3438 continue;
3439 break;
3440 case STRUCT_DOMAIN:
3441 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3442 continue;
3443 break;
3444 case LABEL_DOMAIN:
3445 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3446 continue;
3447 break;
3448 case MODULE_DOMAIN:
3449 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3450 continue;
3451 break;
3452 default:
3453 break;
3454 }
3455 }
3456
3457 ++iter->next;
3458 return per_cu;
3459 }
3460
3461 return NULL;
3462 }
3463
3464 static struct compunit_symtab *
3465 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3466 const char *name, domain_enum domain)
3467 {
3468 struct compunit_symtab *stab_best = NULL;
3469 struct dwarf2_per_objfile *dwarf2_per_objfile
3470 = get_dwarf2_per_objfile (objfile);
3471
3472 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3473
3474 struct dw2_symtab_iterator iter;
3475 struct dwarf2_per_cu_data *per_cu;
3476
3477 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3478
3479 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3480 {
3481 struct symbol *sym, *with_opaque = NULL;
3482 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3483 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3484 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3485
3486 sym = block_find_symbol (block, name, domain,
3487 block_find_non_opaque_type_preferred,
3488 &with_opaque);
3489
3490 /* Some caution must be observed with overloaded functions
3491 and methods, since the index will not contain any overload
3492 information (but NAME might contain it). */
3493
3494 if (sym != NULL
3495 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3496 return stab;
3497 if (with_opaque != NULL
3498 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3499 stab_best = stab;
3500
3501 /* Keep looking through other CUs. */
3502 }
3503
3504 return stab_best;
3505 }
3506
3507 static void
3508 dw2_print_stats (struct objfile *objfile)
3509 {
3510 struct dwarf2_per_objfile *dwarf2_per_objfile
3511 = get_dwarf2_per_objfile (objfile);
3512 int total = (dwarf2_per_objfile->all_comp_units.size ()
3513 + dwarf2_per_objfile->all_type_units.size ());
3514 int count = 0;
3515
3516 for (int i = 0; i < total; ++i)
3517 {
3518 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3519
3520 if (!per_cu->v.quick->compunit_symtab)
3521 ++count;
3522 }
3523 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3524 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3525 }
3526
3527 /* This dumps minimal information about the index.
3528 It is called via "mt print objfiles".
3529 One use is to verify .gdb_index has been loaded by the
3530 gdb.dwarf2/gdb-index.exp testcase. */
3531
3532 static void
3533 dw2_dump (struct objfile *objfile)
3534 {
3535 struct dwarf2_per_objfile *dwarf2_per_objfile
3536 = get_dwarf2_per_objfile (objfile);
3537
3538 gdb_assert (dwarf2_per_objfile->using_index);
3539 printf_filtered (".gdb_index:");
3540 if (dwarf2_per_objfile->index_table != NULL)
3541 {
3542 printf_filtered (" version %d\n",
3543 dwarf2_per_objfile->index_table->version);
3544 }
3545 else
3546 printf_filtered (" faked for \"readnow\"\n");
3547 printf_filtered ("\n");
3548 }
3549
3550 static void
3551 dw2_expand_symtabs_for_function (struct objfile *objfile,
3552 const char *func_name)
3553 {
3554 struct dwarf2_per_objfile *dwarf2_per_objfile
3555 = get_dwarf2_per_objfile (objfile);
3556
3557 struct dw2_symtab_iterator iter;
3558 struct dwarf2_per_cu_data *per_cu;
3559
3560 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3561
3562 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3563 dw2_instantiate_symtab (per_cu, false);
3564
3565 }
3566
3567 static void
3568 dw2_expand_all_symtabs (struct objfile *objfile)
3569 {
3570 struct dwarf2_per_objfile *dwarf2_per_objfile
3571 = get_dwarf2_per_objfile (objfile);
3572 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3573 + dwarf2_per_objfile->all_type_units.size ());
3574
3575 for (int i = 0; i < total_units; ++i)
3576 {
3577 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3578
3579 /* We don't want to directly expand a partial CU, because if we
3580 read it with the wrong language, then assertion failures can
3581 be triggered later on. See PR symtab/23010. So, tell
3582 dw2_instantiate_symtab to skip partial CUs -- any important
3583 partial CU will be read via DW_TAG_imported_unit anyway. */
3584 dw2_instantiate_symtab (per_cu, true);
3585 }
3586 }
3587
3588 static void
3589 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3590 const char *fullname)
3591 {
3592 struct dwarf2_per_objfile *dwarf2_per_objfile
3593 = get_dwarf2_per_objfile (objfile);
3594
3595 /* We don't need to consider type units here.
3596 This is only called for examining code, e.g. expand_line_sal.
3597 There can be an order of magnitude (or more) more type units
3598 than comp units, and we avoid them if we can. */
3599
3600 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3601 {
3602 /* We only need to look at symtabs not already expanded. */
3603 if (per_cu->v.quick->compunit_symtab)
3604 continue;
3605
3606 quick_file_names *file_data = dw2_get_file_names (per_cu);
3607 if (file_data == NULL)
3608 continue;
3609
3610 for (int j = 0; j < file_data->num_file_names; ++j)
3611 {
3612 const char *this_fullname = file_data->file_names[j];
3613
3614 if (filename_cmp (this_fullname, fullname) == 0)
3615 {
3616 dw2_instantiate_symtab (per_cu, false);
3617 break;
3618 }
3619 }
3620 }
3621 }
3622
3623 static void
3624 dw2_map_matching_symbols
3625 (struct objfile *objfile,
3626 const lookup_name_info &name, domain_enum domain,
3627 int global,
3628 gdb::function_view<symbol_found_callback_ftype> callback,
3629 symbol_compare_ftype *ordered_compare)
3630 {
3631 /* Used for Ada. */
3632 struct dwarf2_per_objfile *dwarf2_per_objfile
3633 = get_dwarf2_per_objfile (objfile);
3634
3635 if (dwarf2_per_objfile->index_table != nullptr)
3636 {
3637 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3638 here though if the current language is Ada for a non-Ada objfile
3639 using GNU index. As Ada does not look for non-Ada symbols this
3640 function should just return. */
3641 return;
3642 }
3643
3644 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3645 inline psym_map_matching_symbols here, assuming all partial symtabs have
3646 been read in. */
3647 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3648
3649 for (compunit_symtab *cust : objfile->compunits ())
3650 {
3651 const struct block *block;
3652
3653 if (cust == NULL)
3654 continue;
3655 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3656 if (!iterate_over_symbols_terminated (block, name,
3657 domain, callback))
3658 return;
3659 }
3660 }
3661
3662 /* Starting from a search name, return the string that finds the upper
3663 bound of all strings that start with SEARCH_NAME in a sorted name
3664 list. Returns the empty string to indicate that the upper bound is
3665 the end of the list. */
3666
3667 static std::string
3668 make_sort_after_prefix_name (const char *search_name)
3669 {
3670 /* When looking to complete "func", we find the upper bound of all
3671 symbols that start with "func" by looking for where we'd insert
3672 the closest string that would follow "func" in lexicographical
3673 order. Usually, that's "func"-with-last-character-incremented,
3674 i.e. "fund". Mind non-ASCII characters, though. Usually those
3675 will be UTF-8 multi-byte sequences, but we can't be certain.
3676 Especially mind the 0xff character, which is a valid character in
3677 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3678 rule out compilers allowing it in identifiers. Note that
3679 conveniently, strcmp/strcasecmp are specified to compare
3680 characters interpreted as unsigned char. So what we do is treat
3681 the whole string as a base 256 number composed of a sequence of
3682 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3683 to 0, and carries 1 to the following more-significant position.
3684 If the very first character in SEARCH_NAME ends up incremented
3685 and carries/overflows, then the upper bound is the end of the
3686 list. The string after the empty string is also the empty
3687 string.
3688
3689 Some examples of this operation:
3690
3691 SEARCH_NAME => "+1" RESULT
3692
3693 "abc" => "abd"
3694 "ab\xff" => "ac"
3695 "\xff" "a" "\xff" => "\xff" "b"
3696 "\xff" => ""
3697 "\xff\xff" => ""
3698 "" => ""
3699
3700 Then, with these symbols for example:
3701
3702 func
3703 func1
3704 fund
3705
3706 completing "func" looks for symbols between "func" and
3707 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3708 which finds "func" and "func1", but not "fund".
3709
3710 And with:
3711
3712 funcÿ (Latin1 'ÿ' [0xff])
3713 funcÿ1
3714 fund
3715
3716 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3717 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3718
3719 And with:
3720
3721 ÿÿ (Latin1 'ÿ' [0xff])
3722 ÿÿ1
3723
3724 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3725 the end of the list.
3726 */
3727 std::string after = search_name;
3728 while (!after.empty () && (unsigned char) after.back () == 0xff)
3729 after.pop_back ();
3730 if (!after.empty ())
3731 after.back () = (unsigned char) after.back () + 1;
3732 return after;
3733 }
3734
3735 /* See declaration. */
3736
3737 std::pair<std::vector<name_component>::const_iterator,
3738 std::vector<name_component>::const_iterator>
3739 mapped_index_base::find_name_components_bounds
3740 (const lookup_name_info &lookup_name_without_params, language lang) const
3741 {
3742 auto *name_cmp
3743 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3744
3745 const char *lang_name
3746 = lookup_name_without_params.language_lookup_name (lang);
3747
3748 /* Comparison function object for lower_bound that matches against a
3749 given symbol name. */
3750 auto lookup_compare_lower = [&] (const name_component &elem,
3751 const char *name)
3752 {
3753 const char *elem_qualified = this->symbol_name_at (elem.idx);
3754 const char *elem_name = elem_qualified + elem.name_offset;
3755 return name_cmp (elem_name, name) < 0;
3756 };
3757
3758 /* Comparison function object for upper_bound that matches against a
3759 given symbol name. */
3760 auto lookup_compare_upper = [&] (const char *name,
3761 const name_component &elem)
3762 {
3763 const char *elem_qualified = this->symbol_name_at (elem.idx);
3764 const char *elem_name = elem_qualified + elem.name_offset;
3765 return name_cmp (name, elem_name) < 0;
3766 };
3767
3768 auto begin = this->name_components.begin ();
3769 auto end = this->name_components.end ();
3770
3771 /* Find the lower bound. */
3772 auto lower = [&] ()
3773 {
3774 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3775 return begin;
3776 else
3777 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3778 } ();
3779
3780 /* Find the upper bound. */
3781 auto upper = [&] ()
3782 {
3783 if (lookup_name_without_params.completion_mode ())
3784 {
3785 /* In completion mode, we want UPPER to point past all
3786 symbols names that have the same prefix. I.e., with
3787 these symbols, and completing "func":
3788
3789 function << lower bound
3790 function1
3791 other_function << upper bound
3792
3793 We find the upper bound by looking for the insertion
3794 point of "func"-with-last-character-incremented,
3795 i.e. "fund". */
3796 std::string after = make_sort_after_prefix_name (lang_name);
3797 if (after.empty ())
3798 return end;
3799 return std::lower_bound (lower, end, after.c_str (),
3800 lookup_compare_lower);
3801 }
3802 else
3803 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3804 } ();
3805
3806 return {lower, upper};
3807 }
3808
3809 /* See declaration. */
3810
3811 void
3812 mapped_index_base::build_name_components ()
3813 {
3814 if (!this->name_components.empty ())
3815 return;
3816
3817 this->name_components_casing = case_sensitivity;
3818 auto *name_cmp
3819 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3820
3821 /* The code below only knows how to break apart components of C++
3822 symbol names (and other languages that use '::' as
3823 namespace/module separator) and Ada symbol names. */
3824 auto count = this->symbol_name_count ();
3825 for (offset_type idx = 0; idx < count; idx++)
3826 {
3827 if (this->symbol_name_slot_invalid (idx))
3828 continue;
3829
3830 const char *name = this->symbol_name_at (idx);
3831
3832 /* Add each name component to the name component table. */
3833 unsigned int previous_len = 0;
3834
3835 if (strstr (name, "::") != nullptr)
3836 {
3837 for (unsigned int current_len = cp_find_first_component (name);
3838 name[current_len] != '\0';
3839 current_len += cp_find_first_component (name + current_len))
3840 {
3841 gdb_assert (name[current_len] == ':');
3842 this->name_components.push_back ({previous_len, idx});
3843 /* Skip the '::'. */
3844 current_len += 2;
3845 previous_len = current_len;
3846 }
3847 }
3848 else
3849 {
3850 /* Handle the Ada encoded (aka mangled) form here. */
3851 for (const char *iter = strstr (name, "__");
3852 iter != nullptr;
3853 iter = strstr (iter, "__"))
3854 {
3855 this->name_components.push_back ({previous_len, idx});
3856 iter += 2;
3857 previous_len = iter - name;
3858 }
3859 }
3860
3861 this->name_components.push_back ({previous_len, idx});
3862 }
3863
3864 /* Sort name_components elements by name. */
3865 auto name_comp_compare = [&] (const name_component &left,
3866 const name_component &right)
3867 {
3868 const char *left_qualified = this->symbol_name_at (left.idx);
3869 const char *right_qualified = this->symbol_name_at (right.idx);
3870
3871 const char *left_name = left_qualified + left.name_offset;
3872 const char *right_name = right_qualified + right.name_offset;
3873
3874 return name_cmp (left_name, right_name) < 0;
3875 };
3876
3877 std::sort (this->name_components.begin (),
3878 this->name_components.end (),
3879 name_comp_compare);
3880 }
3881
3882 /* Helper for dw2_expand_symtabs_matching that works with a
3883 mapped_index_base instead of the containing objfile. This is split
3884 to a separate function in order to be able to unit test the
3885 name_components matching using a mock mapped_index_base. For each
3886 symbol name that matches, calls MATCH_CALLBACK, passing it the
3887 symbol's index in the mapped_index_base symbol table. */
3888
3889 static void
3890 dw2_expand_symtabs_matching_symbol
3891 (mapped_index_base &index,
3892 const lookup_name_info &lookup_name_in,
3893 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3894 enum search_domain kind,
3895 gdb::function_view<bool (offset_type)> match_callback)
3896 {
3897 lookup_name_info lookup_name_without_params
3898 = lookup_name_in.make_ignore_params ();
3899
3900 /* Build the symbol name component sorted vector, if we haven't
3901 yet. */
3902 index.build_name_components ();
3903
3904 /* The same symbol may appear more than once in the range though.
3905 E.g., if we're looking for symbols that complete "w", and we have
3906 a symbol named "w1::w2", we'll find the two name components for
3907 that same symbol in the range. To be sure we only call the
3908 callback once per symbol, we first collect the symbol name
3909 indexes that matched in a temporary vector and ignore
3910 duplicates. */
3911 std::vector<offset_type> matches;
3912
3913 struct name_and_matcher
3914 {
3915 symbol_name_matcher_ftype *matcher;
3916 const std::string &name;
3917
3918 bool operator== (const name_and_matcher &other) const
3919 {
3920 return matcher == other.matcher && name == other.name;
3921 }
3922 };
3923
3924 /* A vector holding all the different symbol name matchers, for all
3925 languages. */
3926 std::vector<name_and_matcher> matchers;
3927
3928 for (int i = 0; i < nr_languages; i++)
3929 {
3930 enum language lang_e = (enum language) i;
3931
3932 const language_defn *lang = language_def (lang_e);
3933 symbol_name_matcher_ftype *name_matcher
3934 = get_symbol_name_matcher (lang, lookup_name_without_params);
3935
3936 name_and_matcher key {
3937 name_matcher,
3938 lookup_name_without_params.language_lookup_name (lang_e)
3939 };
3940
3941 /* Don't insert the same comparison routine more than once.
3942 Note that we do this linear walk. This is not a problem in
3943 practice because the number of supported languages is
3944 low. */
3945 if (std::find (matchers.begin (), matchers.end (), key)
3946 != matchers.end ())
3947 continue;
3948 matchers.push_back (std::move (key));
3949
3950 auto bounds
3951 = index.find_name_components_bounds (lookup_name_without_params,
3952 lang_e);
3953
3954 /* Now for each symbol name in range, check to see if we have a name
3955 match, and if so, call the MATCH_CALLBACK callback. */
3956
3957 for (; bounds.first != bounds.second; ++bounds.first)
3958 {
3959 const char *qualified = index.symbol_name_at (bounds.first->idx);
3960
3961 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3962 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3963 continue;
3964
3965 matches.push_back (bounds.first->idx);
3966 }
3967 }
3968
3969 std::sort (matches.begin (), matches.end ());
3970
3971 /* Finally call the callback, once per match. */
3972 ULONGEST prev = -1;
3973 for (offset_type idx : matches)
3974 {
3975 if (prev != idx)
3976 {
3977 if (!match_callback (idx))
3978 break;
3979 prev = idx;
3980 }
3981 }
3982
3983 /* Above we use a type wider than idx's for 'prev', since 0 and
3984 (offset_type)-1 are both possible values. */
3985 static_assert (sizeof (prev) > sizeof (offset_type), "");
3986 }
3987
3988 #if GDB_SELF_TEST
3989
3990 namespace selftests { namespace dw2_expand_symtabs_matching {
3991
3992 /* A mock .gdb_index/.debug_names-like name index table, enough to
3993 exercise dw2_expand_symtabs_matching_symbol, which works with the
3994 mapped_index_base interface. Builds an index from the symbol list
3995 passed as parameter to the constructor. */
3996 class mock_mapped_index : public mapped_index_base
3997 {
3998 public:
3999 mock_mapped_index (gdb::array_view<const char *> symbols)
4000 : m_symbol_table (symbols)
4001 {}
4002
4003 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4004
4005 /* Return the number of names in the symbol table. */
4006 size_t symbol_name_count () const override
4007 {
4008 return m_symbol_table.size ();
4009 }
4010
4011 /* Get the name of the symbol at IDX in the symbol table. */
4012 const char *symbol_name_at (offset_type idx) const override
4013 {
4014 return m_symbol_table[idx];
4015 }
4016
4017 private:
4018 gdb::array_view<const char *> m_symbol_table;
4019 };
4020
4021 /* Convenience function that converts a NULL pointer to a "<null>"
4022 string, to pass to print routines. */
4023
4024 static const char *
4025 string_or_null (const char *str)
4026 {
4027 return str != NULL ? str : "<null>";
4028 }
4029
4030 /* Check if a lookup_name_info built from
4031 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4032 index. EXPECTED_LIST is the list of expected matches, in expected
4033 matching order. If no match expected, then an empty list is
4034 specified. Returns true on success. On failure prints a warning
4035 indicating the file:line that failed, and returns false. */
4036
4037 static bool
4038 check_match (const char *file, int line,
4039 mock_mapped_index &mock_index,
4040 const char *name, symbol_name_match_type match_type,
4041 bool completion_mode,
4042 std::initializer_list<const char *> expected_list)
4043 {
4044 lookup_name_info lookup_name (name, match_type, completion_mode);
4045
4046 bool matched = true;
4047
4048 auto mismatch = [&] (const char *expected_str,
4049 const char *got)
4050 {
4051 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4052 "expected=\"%s\", got=\"%s\"\n"),
4053 file, line,
4054 (match_type == symbol_name_match_type::FULL
4055 ? "FULL" : "WILD"),
4056 name, string_or_null (expected_str), string_or_null (got));
4057 matched = false;
4058 };
4059
4060 auto expected_it = expected_list.begin ();
4061 auto expected_end = expected_list.end ();
4062
4063 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4064 NULL, ALL_DOMAIN,
4065 [&] (offset_type idx)
4066 {
4067 const char *matched_name = mock_index.symbol_name_at (idx);
4068 const char *expected_str
4069 = expected_it == expected_end ? NULL : *expected_it++;
4070
4071 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4072 mismatch (expected_str, matched_name);
4073 return true;
4074 });
4075
4076 const char *expected_str
4077 = expected_it == expected_end ? NULL : *expected_it++;
4078 if (expected_str != NULL)
4079 mismatch (expected_str, NULL);
4080
4081 return matched;
4082 }
4083
4084 /* The symbols added to the mock mapped_index for testing (in
4085 canonical form). */
4086 static const char *test_symbols[] = {
4087 "function",
4088 "std::bar",
4089 "std::zfunction",
4090 "std::zfunction2",
4091 "w1::w2",
4092 "ns::foo<char*>",
4093 "ns::foo<int>",
4094 "ns::foo<long>",
4095 "ns2::tmpl<int>::foo2",
4096 "(anonymous namespace)::A::B::C",
4097
4098 /* These are used to check that the increment-last-char in the
4099 matching algorithm for completion doesn't match "t1_fund" when
4100 completing "t1_func". */
4101 "t1_func",
4102 "t1_func1",
4103 "t1_fund",
4104 "t1_fund1",
4105
4106 /* A UTF-8 name with multi-byte sequences to make sure that
4107 cp-name-parser understands this as a single identifier ("função"
4108 is "function" in PT). */
4109 u8"u8função",
4110
4111 /* \377 (0xff) is Latin1 'ÿ'. */
4112 "yfunc\377",
4113
4114 /* \377 (0xff) is Latin1 'ÿ'. */
4115 "\377",
4116 "\377\377123",
4117
4118 /* A name with all sorts of complications. Starts with "z" to make
4119 it easier for the completion tests below. */
4120 #define Z_SYM_NAME \
4121 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4122 "::tuple<(anonymous namespace)::ui*, " \
4123 "std::default_delete<(anonymous namespace)::ui>, void>"
4124
4125 Z_SYM_NAME
4126 };
4127
4128 /* Returns true if the mapped_index_base::find_name_component_bounds
4129 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4130 in completion mode. */
4131
4132 static bool
4133 check_find_bounds_finds (mapped_index_base &index,
4134 const char *search_name,
4135 gdb::array_view<const char *> expected_syms)
4136 {
4137 lookup_name_info lookup_name (search_name,
4138 symbol_name_match_type::FULL, true);
4139
4140 auto bounds = index.find_name_components_bounds (lookup_name,
4141 language_cplus);
4142
4143 size_t distance = std::distance (bounds.first, bounds.second);
4144 if (distance != expected_syms.size ())
4145 return false;
4146
4147 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4148 {
4149 auto nc_elem = bounds.first + exp_elem;
4150 const char *qualified = index.symbol_name_at (nc_elem->idx);
4151 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4152 return false;
4153 }
4154
4155 return true;
4156 }
4157
4158 /* Test the lower-level mapped_index::find_name_component_bounds
4159 method. */
4160
4161 static void
4162 test_mapped_index_find_name_component_bounds ()
4163 {
4164 mock_mapped_index mock_index (test_symbols);
4165
4166 mock_index.build_name_components ();
4167
4168 /* Test the lower-level mapped_index::find_name_component_bounds
4169 method in completion mode. */
4170 {
4171 static const char *expected_syms[] = {
4172 "t1_func",
4173 "t1_func1",
4174 };
4175
4176 SELF_CHECK (check_find_bounds_finds (mock_index,
4177 "t1_func", expected_syms));
4178 }
4179
4180 /* Check that the increment-last-char in the name matching algorithm
4181 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4182 {
4183 static const char *expected_syms1[] = {
4184 "\377",
4185 "\377\377123",
4186 };
4187 SELF_CHECK (check_find_bounds_finds (mock_index,
4188 "\377", expected_syms1));
4189
4190 static const char *expected_syms2[] = {
4191 "\377\377123",
4192 };
4193 SELF_CHECK (check_find_bounds_finds (mock_index,
4194 "\377\377", expected_syms2));
4195 }
4196 }
4197
4198 /* Test dw2_expand_symtabs_matching_symbol. */
4199
4200 static void
4201 test_dw2_expand_symtabs_matching_symbol ()
4202 {
4203 mock_mapped_index mock_index (test_symbols);
4204
4205 /* We let all tests run until the end even if some fails, for debug
4206 convenience. */
4207 bool any_mismatch = false;
4208
4209 /* Create the expected symbols list (an initializer_list). Needed
4210 because lists have commas, and we need to pass them to CHECK,
4211 which is a macro. */
4212 #define EXPECT(...) { __VA_ARGS__ }
4213
4214 /* Wrapper for check_match that passes down the current
4215 __FILE__/__LINE__. */
4216 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4217 any_mismatch |= !check_match (__FILE__, __LINE__, \
4218 mock_index, \
4219 NAME, MATCH_TYPE, COMPLETION_MODE, \
4220 EXPECTED_LIST)
4221
4222 /* Identity checks. */
4223 for (const char *sym : test_symbols)
4224 {
4225 /* Should be able to match all existing symbols. */
4226 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4227 EXPECT (sym));
4228
4229 /* Should be able to match all existing symbols with
4230 parameters. */
4231 std::string with_params = std::string (sym) + "(int)";
4232 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4233 EXPECT (sym));
4234
4235 /* Should be able to match all existing symbols with
4236 parameters and qualifiers. */
4237 with_params = std::string (sym) + " ( int ) const";
4238 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4239 EXPECT (sym));
4240
4241 /* This should really find sym, but cp-name-parser.y doesn't
4242 know about lvalue/rvalue qualifiers yet. */
4243 with_params = std::string (sym) + " ( int ) &&";
4244 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4245 {});
4246 }
4247
4248 /* Check that the name matching algorithm for completion doesn't get
4249 confused with Latin1 'ÿ' / 0xff. */
4250 {
4251 static const char str[] = "\377";
4252 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4253 EXPECT ("\377", "\377\377123"));
4254 }
4255
4256 /* Check that the increment-last-char in the matching algorithm for
4257 completion doesn't match "t1_fund" when completing "t1_func". */
4258 {
4259 static const char str[] = "t1_func";
4260 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4261 EXPECT ("t1_func", "t1_func1"));
4262 }
4263
4264 /* Check that completion mode works at each prefix of the expected
4265 symbol name. */
4266 {
4267 static const char str[] = "function(int)";
4268 size_t len = strlen (str);
4269 std::string lookup;
4270
4271 for (size_t i = 1; i < len; i++)
4272 {
4273 lookup.assign (str, i);
4274 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4275 EXPECT ("function"));
4276 }
4277 }
4278
4279 /* While "w" is a prefix of both components, the match function
4280 should still only be called once. */
4281 {
4282 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4283 EXPECT ("w1::w2"));
4284 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4285 EXPECT ("w1::w2"));
4286 }
4287
4288 /* Same, with a "complicated" symbol. */
4289 {
4290 static const char str[] = Z_SYM_NAME;
4291 size_t len = strlen (str);
4292 std::string lookup;
4293
4294 for (size_t i = 1; i < len; i++)
4295 {
4296 lookup.assign (str, i);
4297 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4298 EXPECT (Z_SYM_NAME));
4299 }
4300 }
4301
4302 /* In FULL mode, an incomplete symbol doesn't match. */
4303 {
4304 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4305 {});
4306 }
4307
4308 /* A complete symbol with parameters matches any overload, since the
4309 index has no overload info. */
4310 {
4311 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4312 EXPECT ("std::zfunction", "std::zfunction2"));
4313 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4314 EXPECT ("std::zfunction", "std::zfunction2"));
4315 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4316 EXPECT ("std::zfunction", "std::zfunction2"));
4317 }
4318
4319 /* Check that whitespace is ignored appropriately. A symbol with a
4320 template argument list. */
4321 {
4322 static const char expected[] = "ns::foo<int>";
4323 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4324 EXPECT (expected));
4325 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4326 EXPECT (expected));
4327 }
4328
4329 /* Check that whitespace is ignored appropriately. A symbol with a
4330 template argument list that includes a pointer. */
4331 {
4332 static const char expected[] = "ns::foo<char*>";
4333 /* Try both completion and non-completion modes. */
4334 static const bool completion_mode[2] = {false, true};
4335 for (size_t i = 0; i < 2; i++)
4336 {
4337 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4338 completion_mode[i], EXPECT (expected));
4339 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4340 completion_mode[i], EXPECT (expected));
4341
4342 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4343 completion_mode[i], EXPECT (expected));
4344 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4345 completion_mode[i], EXPECT (expected));
4346 }
4347 }
4348
4349 {
4350 /* Check method qualifiers are ignored. */
4351 static const char expected[] = "ns::foo<char*>";
4352 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4353 symbol_name_match_type::FULL, true, EXPECT (expected));
4354 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4355 symbol_name_match_type::FULL, true, EXPECT (expected));
4356 CHECK_MATCH ("foo < char * > ( int ) const",
4357 symbol_name_match_type::WILD, true, EXPECT (expected));
4358 CHECK_MATCH ("foo < char * > ( int ) &&",
4359 symbol_name_match_type::WILD, true, EXPECT (expected));
4360 }
4361
4362 /* Test lookup names that don't match anything. */
4363 {
4364 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4365 {});
4366
4367 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4368 {});
4369 }
4370
4371 /* Some wild matching tests, exercising "(anonymous namespace)",
4372 which should not be confused with a parameter list. */
4373 {
4374 static const char *syms[] = {
4375 "A::B::C",
4376 "B::C",
4377 "C",
4378 "A :: B :: C ( int )",
4379 "B :: C ( int )",
4380 "C ( int )",
4381 };
4382
4383 for (const char *s : syms)
4384 {
4385 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4386 EXPECT ("(anonymous namespace)::A::B::C"));
4387 }
4388 }
4389
4390 {
4391 static const char expected[] = "ns2::tmpl<int>::foo2";
4392 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4393 EXPECT (expected));
4394 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4395 EXPECT (expected));
4396 }
4397
4398 SELF_CHECK (!any_mismatch);
4399
4400 #undef EXPECT
4401 #undef CHECK_MATCH
4402 }
4403
4404 static void
4405 run_test ()
4406 {
4407 test_mapped_index_find_name_component_bounds ();
4408 test_dw2_expand_symtabs_matching_symbol ();
4409 }
4410
4411 }} // namespace selftests::dw2_expand_symtabs_matching
4412
4413 #endif /* GDB_SELF_TEST */
4414
4415 /* If FILE_MATCHER is NULL or if PER_CU has
4416 dwarf2_per_cu_quick_data::MARK set (see
4417 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4418 EXPANSION_NOTIFY on it. */
4419
4420 static void
4421 dw2_expand_symtabs_matching_one
4422 (struct dwarf2_per_cu_data *per_cu,
4423 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4424 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4425 {
4426 if (file_matcher == NULL || per_cu->v.quick->mark)
4427 {
4428 bool symtab_was_null
4429 = (per_cu->v.quick->compunit_symtab == NULL);
4430
4431 dw2_instantiate_symtab (per_cu, false);
4432
4433 if (expansion_notify != NULL
4434 && symtab_was_null
4435 && per_cu->v.quick->compunit_symtab != NULL)
4436 expansion_notify (per_cu->v.quick->compunit_symtab);
4437 }
4438 }
4439
4440 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4441 matched, to expand corresponding CUs that were marked. IDX is the
4442 index of the symbol name that matched. */
4443
4444 static void
4445 dw2_expand_marked_cus
4446 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4447 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4448 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4449 search_domain kind)
4450 {
4451 offset_type *vec, vec_len, vec_idx;
4452 bool global_seen = false;
4453 mapped_index &index = *dwarf2_per_objfile->index_table;
4454
4455 vec = (offset_type *) (index.constant_pool
4456 + MAYBE_SWAP (index.symbol_table[idx].vec));
4457 vec_len = MAYBE_SWAP (vec[0]);
4458 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4459 {
4460 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4461 /* This value is only valid for index versions >= 7. */
4462 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4463 gdb_index_symbol_kind symbol_kind =
4464 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4465 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4466 /* Only check the symbol attributes if they're present.
4467 Indices prior to version 7 don't record them,
4468 and indices >= 7 may elide them for certain symbols
4469 (gold does this). */
4470 int attrs_valid =
4471 (index.version >= 7
4472 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4473
4474 /* Work around gold/15646. */
4475 if (attrs_valid)
4476 {
4477 if (!is_static && global_seen)
4478 continue;
4479 if (!is_static)
4480 global_seen = true;
4481 }
4482
4483 /* Only check the symbol's kind if it has one. */
4484 if (attrs_valid)
4485 {
4486 switch (kind)
4487 {
4488 case VARIABLES_DOMAIN:
4489 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4490 continue;
4491 break;
4492 case FUNCTIONS_DOMAIN:
4493 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4494 continue;
4495 break;
4496 case TYPES_DOMAIN:
4497 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4498 continue;
4499 break;
4500 case MODULES_DOMAIN:
4501 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4502 continue;
4503 break;
4504 default:
4505 break;
4506 }
4507 }
4508
4509 /* Don't crash on bad data. */
4510 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4511 + dwarf2_per_objfile->all_type_units.size ()))
4512 {
4513 complaint (_(".gdb_index entry has bad CU index"
4514 " [in module %s]"),
4515 objfile_name (dwarf2_per_objfile->objfile));
4516 continue;
4517 }
4518
4519 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4520 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4521 expansion_notify);
4522 }
4523 }
4524
4525 /* If FILE_MATCHER is non-NULL, set all the
4526 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4527 that match FILE_MATCHER. */
4528
4529 static void
4530 dw_expand_symtabs_matching_file_matcher
4531 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4532 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4533 {
4534 if (file_matcher == NULL)
4535 return;
4536
4537 objfile *const objfile = dwarf2_per_objfile->objfile;
4538
4539 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4540 htab_eq_pointer,
4541 NULL, xcalloc, xfree));
4542 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4543 htab_eq_pointer,
4544 NULL, xcalloc, xfree));
4545
4546 /* The rule is CUs specify all the files, including those used by
4547 any TU, so there's no need to scan TUs here. */
4548
4549 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4550 {
4551 QUIT;
4552
4553 per_cu->v.quick->mark = 0;
4554
4555 /* We only need to look at symtabs not already expanded. */
4556 if (per_cu->v.quick->compunit_symtab)
4557 continue;
4558
4559 quick_file_names *file_data = dw2_get_file_names (per_cu);
4560 if (file_data == NULL)
4561 continue;
4562
4563 if (htab_find (visited_not_found.get (), file_data) != NULL)
4564 continue;
4565 else if (htab_find (visited_found.get (), file_data) != NULL)
4566 {
4567 per_cu->v.quick->mark = 1;
4568 continue;
4569 }
4570
4571 for (int j = 0; j < file_data->num_file_names; ++j)
4572 {
4573 const char *this_real_name;
4574
4575 if (file_matcher (file_data->file_names[j], false))
4576 {
4577 per_cu->v.quick->mark = 1;
4578 break;
4579 }
4580
4581 /* Before we invoke realpath, which can get expensive when many
4582 files are involved, do a quick comparison of the basenames. */
4583 if (!basenames_may_differ
4584 && !file_matcher (lbasename (file_data->file_names[j]),
4585 true))
4586 continue;
4587
4588 this_real_name = dw2_get_real_path (objfile, file_data, j);
4589 if (file_matcher (this_real_name, false))
4590 {
4591 per_cu->v.quick->mark = 1;
4592 break;
4593 }
4594 }
4595
4596 void **slot = htab_find_slot (per_cu->v.quick->mark
4597 ? visited_found.get ()
4598 : visited_not_found.get (),
4599 file_data, INSERT);
4600 *slot = file_data;
4601 }
4602 }
4603
4604 static void
4605 dw2_expand_symtabs_matching
4606 (struct objfile *objfile,
4607 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4608 const lookup_name_info &lookup_name,
4609 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4610 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4611 enum search_domain kind)
4612 {
4613 struct dwarf2_per_objfile *dwarf2_per_objfile
4614 = get_dwarf2_per_objfile (objfile);
4615
4616 /* index_table is NULL if OBJF_READNOW. */
4617 if (!dwarf2_per_objfile->index_table)
4618 return;
4619
4620 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4621
4622 mapped_index &index = *dwarf2_per_objfile->index_table;
4623
4624 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4625 symbol_matcher,
4626 kind, [&] (offset_type idx)
4627 {
4628 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4629 expansion_notify, kind);
4630 return true;
4631 });
4632 }
4633
4634 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4635 symtab. */
4636
4637 static struct compunit_symtab *
4638 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4639 CORE_ADDR pc)
4640 {
4641 int i;
4642
4643 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4644 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4645 return cust;
4646
4647 if (cust->includes == NULL)
4648 return NULL;
4649
4650 for (i = 0; cust->includes[i]; ++i)
4651 {
4652 struct compunit_symtab *s = cust->includes[i];
4653
4654 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4655 if (s != NULL)
4656 return s;
4657 }
4658
4659 return NULL;
4660 }
4661
4662 static struct compunit_symtab *
4663 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4664 struct bound_minimal_symbol msymbol,
4665 CORE_ADDR pc,
4666 struct obj_section *section,
4667 int warn_if_readin)
4668 {
4669 struct dwarf2_per_cu_data *data;
4670 struct compunit_symtab *result;
4671
4672 if (!objfile->partial_symtabs->psymtabs_addrmap)
4673 return NULL;
4674
4675 CORE_ADDR baseaddr = objfile->text_section_offset ();
4676 data = (struct dwarf2_per_cu_data *) addrmap_find
4677 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4678 if (!data)
4679 return NULL;
4680
4681 if (warn_if_readin && data->v.quick->compunit_symtab)
4682 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4683 paddress (get_objfile_arch (objfile), pc));
4684
4685 result
4686 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4687 false),
4688 pc);
4689 gdb_assert (result != NULL);
4690 return result;
4691 }
4692
4693 static void
4694 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4695 void *data, int need_fullname)
4696 {
4697 struct dwarf2_per_objfile *dwarf2_per_objfile
4698 = get_dwarf2_per_objfile (objfile);
4699
4700 if (!dwarf2_per_objfile->filenames_cache)
4701 {
4702 dwarf2_per_objfile->filenames_cache.emplace ();
4703
4704 htab_up visited (htab_create_alloc (10,
4705 htab_hash_pointer, htab_eq_pointer,
4706 NULL, xcalloc, xfree));
4707
4708 /* The rule is CUs specify all the files, including those used
4709 by any TU, so there's no need to scan TUs here. We can
4710 ignore file names coming from already-expanded CUs. */
4711
4712 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4713 {
4714 if (per_cu->v.quick->compunit_symtab)
4715 {
4716 void **slot = htab_find_slot (visited.get (),
4717 per_cu->v.quick->file_names,
4718 INSERT);
4719
4720 *slot = per_cu->v.quick->file_names;
4721 }
4722 }
4723
4724 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4725 {
4726 /* We only need to look at symtabs not already expanded. */
4727 if (per_cu->v.quick->compunit_symtab)
4728 continue;
4729
4730 quick_file_names *file_data = dw2_get_file_names (per_cu);
4731 if (file_data == NULL)
4732 continue;
4733
4734 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4735 if (*slot)
4736 {
4737 /* Already visited. */
4738 continue;
4739 }
4740 *slot = file_data;
4741
4742 for (int j = 0; j < file_data->num_file_names; ++j)
4743 {
4744 const char *filename = file_data->file_names[j];
4745 dwarf2_per_objfile->filenames_cache->seen (filename);
4746 }
4747 }
4748 }
4749
4750 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4751 {
4752 gdb::unique_xmalloc_ptr<char> this_real_name;
4753
4754 if (need_fullname)
4755 this_real_name = gdb_realpath (filename);
4756 (*fun) (filename, this_real_name.get (), data);
4757 });
4758 }
4759
4760 static int
4761 dw2_has_symbols (struct objfile *objfile)
4762 {
4763 return 1;
4764 }
4765
4766 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4767 {
4768 dw2_has_symbols,
4769 dw2_find_last_source_symtab,
4770 dw2_forget_cached_source_info,
4771 dw2_map_symtabs_matching_filename,
4772 dw2_lookup_symbol,
4773 NULL,
4774 dw2_print_stats,
4775 dw2_dump,
4776 dw2_expand_symtabs_for_function,
4777 dw2_expand_all_symtabs,
4778 dw2_expand_symtabs_with_fullname,
4779 dw2_map_matching_symbols,
4780 dw2_expand_symtabs_matching,
4781 dw2_find_pc_sect_compunit_symtab,
4782 NULL,
4783 dw2_map_symbol_filenames
4784 };
4785
4786 /* DWARF-5 debug_names reader. */
4787
4788 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4789 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4790
4791 /* A helper function that reads the .debug_names section in SECTION
4792 and fills in MAP. FILENAME is the name of the file containing the
4793 section; it is used for error reporting.
4794
4795 Returns true if all went well, false otherwise. */
4796
4797 static bool
4798 read_debug_names_from_section (struct objfile *objfile,
4799 const char *filename,
4800 struct dwarf2_section_info *section,
4801 mapped_debug_names &map)
4802 {
4803 if (section->empty ())
4804 return false;
4805
4806 /* Older elfutils strip versions could keep the section in the main
4807 executable while splitting it for the separate debug info file. */
4808 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4809 return false;
4810
4811 section->read (objfile);
4812
4813 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4814
4815 const gdb_byte *addr = section->buffer;
4816
4817 bfd *const abfd = section->get_bfd_owner ();
4818
4819 unsigned int bytes_read;
4820 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4821 addr += bytes_read;
4822
4823 map.dwarf5_is_dwarf64 = bytes_read != 4;
4824 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4825 if (bytes_read + length != section->size)
4826 {
4827 /* There may be multiple per-CU indices. */
4828 warning (_("Section .debug_names in %s length %s does not match "
4829 "section length %s, ignoring .debug_names."),
4830 filename, plongest (bytes_read + length),
4831 pulongest (section->size));
4832 return false;
4833 }
4834
4835 /* The version number. */
4836 uint16_t version = read_2_bytes (abfd, addr);
4837 addr += 2;
4838 if (version != 5)
4839 {
4840 warning (_("Section .debug_names in %s has unsupported version %d, "
4841 "ignoring .debug_names."),
4842 filename, version);
4843 return false;
4844 }
4845
4846 /* Padding. */
4847 uint16_t padding = read_2_bytes (abfd, addr);
4848 addr += 2;
4849 if (padding != 0)
4850 {
4851 warning (_("Section .debug_names in %s has unsupported padding %d, "
4852 "ignoring .debug_names."),
4853 filename, padding);
4854 return false;
4855 }
4856
4857 /* comp_unit_count - The number of CUs in the CU list. */
4858 map.cu_count = read_4_bytes (abfd, addr);
4859 addr += 4;
4860
4861 /* local_type_unit_count - The number of TUs in the local TU
4862 list. */
4863 map.tu_count = read_4_bytes (abfd, addr);
4864 addr += 4;
4865
4866 /* foreign_type_unit_count - The number of TUs in the foreign TU
4867 list. */
4868 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4869 addr += 4;
4870 if (foreign_tu_count != 0)
4871 {
4872 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4873 "ignoring .debug_names."),
4874 filename, static_cast<unsigned long> (foreign_tu_count));
4875 return false;
4876 }
4877
4878 /* bucket_count - The number of hash buckets in the hash lookup
4879 table. */
4880 map.bucket_count = read_4_bytes (abfd, addr);
4881 addr += 4;
4882
4883 /* name_count - The number of unique names in the index. */
4884 map.name_count = read_4_bytes (abfd, addr);
4885 addr += 4;
4886
4887 /* abbrev_table_size - The size in bytes of the abbreviations
4888 table. */
4889 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4890 addr += 4;
4891
4892 /* augmentation_string_size - The size in bytes of the augmentation
4893 string. This value is rounded up to a multiple of 4. */
4894 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4895 addr += 4;
4896 map.augmentation_is_gdb = ((augmentation_string_size
4897 == sizeof (dwarf5_augmentation))
4898 && memcmp (addr, dwarf5_augmentation,
4899 sizeof (dwarf5_augmentation)) == 0);
4900 augmentation_string_size += (-augmentation_string_size) & 3;
4901 addr += augmentation_string_size;
4902
4903 /* List of CUs */
4904 map.cu_table_reordered = addr;
4905 addr += map.cu_count * map.offset_size;
4906
4907 /* List of Local TUs */
4908 map.tu_table_reordered = addr;
4909 addr += map.tu_count * map.offset_size;
4910
4911 /* Hash Lookup Table */
4912 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4913 addr += map.bucket_count * 4;
4914 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4915 addr += map.name_count * 4;
4916
4917 /* Name Table */
4918 map.name_table_string_offs_reordered = addr;
4919 addr += map.name_count * map.offset_size;
4920 map.name_table_entry_offs_reordered = addr;
4921 addr += map.name_count * map.offset_size;
4922
4923 const gdb_byte *abbrev_table_start = addr;
4924 for (;;)
4925 {
4926 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4927 addr += bytes_read;
4928 if (index_num == 0)
4929 break;
4930
4931 const auto insertpair
4932 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4933 if (!insertpair.second)
4934 {
4935 warning (_("Section .debug_names in %s has duplicate index %s, "
4936 "ignoring .debug_names."),
4937 filename, pulongest (index_num));
4938 return false;
4939 }
4940 mapped_debug_names::index_val &indexval = insertpair.first->second;
4941 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4942 addr += bytes_read;
4943
4944 for (;;)
4945 {
4946 mapped_debug_names::index_val::attr attr;
4947 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4948 addr += bytes_read;
4949 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4950 addr += bytes_read;
4951 if (attr.form == DW_FORM_implicit_const)
4952 {
4953 attr.implicit_const = read_signed_leb128 (abfd, addr,
4954 &bytes_read);
4955 addr += bytes_read;
4956 }
4957 if (attr.dw_idx == 0 && attr.form == 0)
4958 break;
4959 indexval.attr_vec.push_back (std::move (attr));
4960 }
4961 }
4962 if (addr != abbrev_table_start + abbrev_table_size)
4963 {
4964 warning (_("Section .debug_names in %s has abbreviation_table "
4965 "of size %s vs. written as %u, ignoring .debug_names."),
4966 filename, plongest (addr - abbrev_table_start),
4967 abbrev_table_size);
4968 return false;
4969 }
4970 map.entry_pool = addr;
4971
4972 return true;
4973 }
4974
4975 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4976 list. */
4977
4978 static void
4979 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4980 const mapped_debug_names &map,
4981 dwarf2_section_info &section,
4982 bool is_dwz)
4983 {
4984 sect_offset sect_off_prev;
4985 for (uint32_t i = 0; i <= map.cu_count; ++i)
4986 {
4987 sect_offset sect_off_next;
4988 if (i < map.cu_count)
4989 {
4990 sect_off_next
4991 = (sect_offset) (extract_unsigned_integer
4992 (map.cu_table_reordered + i * map.offset_size,
4993 map.offset_size,
4994 map.dwarf5_byte_order));
4995 }
4996 else
4997 sect_off_next = (sect_offset) section.size;
4998 if (i >= 1)
4999 {
5000 const ULONGEST length = sect_off_next - sect_off_prev;
5001 dwarf2_per_cu_data *per_cu
5002 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5003 sect_off_prev, length);
5004 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5005 }
5006 sect_off_prev = sect_off_next;
5007 }
5008 }
5009
5010 /* Read the CU list from the mapped index, and use it to create all
5011 the CU objects for this dwarf2_per_objfile. */
5012
5013 static void
5014 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5015 const mapped_debug_names &map,
5016 const mapped_debug_names &dwz_map)
5017 {
5018 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5019 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5020
5021 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5022 dwarf2_per_objfile->info,
5023 false /* is_dwz */);
5024
5025 if (dwz_map.cu_count == 0)
5026 return;
5027
5028 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5029 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5030 true /* is_dwz */);
5031 }
5032
5033 /* Read .debug_names. If everything went ok, initialize the "quick"
5034 elements of all the CUs and return true. Otherwise, return false. */
5035
5036 static bool
5037 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5038 {
5039 std::unique_ptr<mapped_debug_names> map
5040 (new mapped_debug_names (dwarf2_per_objfile));
5041 mapped_debug_names dwz_map (dwarf2_per_objfile);
5042 struct objfile *objfile = dwarf2_per_objfile->objfile;
5043
5044 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5045 &dwarf2_per_objfile->debug_names,
5046 *map))
5047 return false;
5048
5049 /* Don't use the index if it's empty. */
5050 if (map->name_count == 0)
5051 return false;
5052
5053 /* If there is a .dwz file, read it so we can get its CU list as
5054 well. */
5055 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5056 if (dwz != NULL)
5057 {
5058 if (!read_debug_names_from_section (objfile,
5059 bfd_get_filename (dwz->dwz_bfd.get ()),
5060 &dwz->debug_names, dwz_map))
5061 {
5062 warning (_("could not read '.debug_names' section from %s; skipping"),
5063 bfd_get_filename (dwz->dwz_bfd.get ()));
5064 return false;
5065 }
5066 }
5067
5068 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5069
5070 if (map->tu_count != 0)
5071 {
5072 /* We can only handle a single .debug_types when we have an
5073 index. */
5074 if (dwarf2_per_objfile->types.size () != 1)
5075 return false;
5076
5077 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5078
5079 create_signatured_type_table_from_debug_names
5080 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5081 }
5082
5083 create_addrmap_from_aranges (dwarf2_per_objfile,
5084 &dwarf2_per_objfile->debug_aranges);
5085
5086 dwarf2_per_objfile->debug_names_table = std::move (map);
5087 dwarf2_per_objfile->using_index = 1;
5088 dwarf2_per_objfile->quick_file_names_table =
5089 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5090
5091 return true;
5092 }
5093
5094 /* Type used to manage iterating over all CUs looking for a symbol for
5095 .debug_names. */
5096
5097 class dw2_debug_names_iterator
5098 {
5099 public:
5100 dw2_debug_names_iterator (const mapped_debug_names &map,
5101 gdb::optional<block_enum> block_index,
5102 domain_enum domain,
5103 const char *name)
5104 : m_map (map), m_block_index (block_index), m_domain (domain),
5105 m_addr (find_vec_in_debug_names (map, name))
5106 {}
5107
5108 dw2_debug_names_iterator (const mapped_debug_names &map,
5109 search_domain search, uint32_t namei)
5110 : m_map (map),
5111 m_search (search),
5112 m_addr (find_vec_in_debug_names (map, namei))
5113 {}
5114
5115 dw2_debug_names_iterator (const mapped_debug_names &map,
5116 block_enum block_index, domain_enum domain,
5117 uint32_t namei)
5118 : m_map (map), m_block_index (block_index), m_domain (domain),
5119 m_addr (find_vec_in_debug_names (map, namei))
5120 {}
5121
5122 /* Return the next matching CU or NULL if there are no more. */
5123 dwarf2_per_cu_data *next ();
5124
5125 private:
5126 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5127 const char *name);
5128 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5129 uint32_t namei);
5130
5131 /* The internalized form of .debug_names. */
5132 const mapped_debug_names &m_map;
5133
5134 /* If set, only look for symbols that match that block. Valid values are
5135 GLOBAL_BLOCK and STATIC_BLOCK. */
5136 const gdb::optional<block_enum> m_block_index;
5137
5138 /* The kind of symbol we're looking for. */
5139 const domain_enum m_domain = UNDEF_DOMAIN;
5140 const search_domain m_search = ALL_DOMAIN;
5141
5142 /* The list of CUs from the index entry of the symbol, or NULL if
5143 not found. */
5144 const gdb_byte *m_addr;
5145 };
5146
5147 const char *
5148 mapped_debug_names::namei_to_name (uint32_t namei) const
5149 {
5150 const ULONGEST namei_string_offs
5151 = extract_unsigned_integer ((name_table_string_offs_reordered
5152 + namei * offset_size),
5153 offset_size,
5154 dwarf5_byte_order);
5155 return read_indirect_string_at_offset (dwarf2_per_objfile,
5156 namei_string_offs);
5157 }
5158
5159 /* Find a slot in .debug_names for the object named NAME. If NAME is
5160 found, return pointer to its pool data. If NAME cannot be found,
5161 return NULL. */
5162
5163 const gdb_byte *
5164 dw2_debug_names_iterator::find_vec_in_debug_names
5165 (const mapped_debug_names &map, const char *name)
5166 {
5167 int (*cmp) (const char *, const char *);
5168
5169 gdb::unique_xmalloc_ptr<char> without_params;
5170 if (current_language->la_language == language_cplus
5171 || current_language->la_language == language_fortran
5172 || current_language->la_language == language_d)
5173 {
5174 /* NAME is already canonical. Drop any qualifiers as
5175 .debug_names does not contain any. */
5176
5177 if (strchr (name, '(') != NULL)
5178 {
5179 without_params = cp_remove_params (name);
5180 if (without_params != NULL)
5181 name = without_params.get ();
5182 }
5183 }
5184
5185 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5186
5187 const uint32_t full_hash = dwarf5_djb_hash (name);
5188 uint32_t namei
5189 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5190 (map.bucket_table_reordered
5191 + (full_hash % map.bucket_count)), 4,
5192 map.dwarf5_byte_order);
5193 if (namei == 0)
5194 return NULL;
5195 --namei;
5196 if (namei >= map.name_count)
5197 {
5198 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5199 "[in module %s]"),
5200 namei, map.name_count,
5201 objfile_name (map.dwarf2_per_objfile->objfile));
5202 return NULL;
5203 }
5204
5205 for (;;)
5206 {
5207 const uint32_t namei_full_hash
5208 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5209 (map.hash_table_reordered + namei), 4,
5210 map.dwarf5_byte_order);
5211 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5212 return NULL;
5213
5214 if (full_hash == namei_full_hash)
5215 {
5216 const char *const namei_string = map.namei_to_name (namei);
5217
5218 #if 0 /* An expensive sanity check. */
5219 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5220 {
5221 complaint (_("Wrong .debug_names hash for string at index %u "
5222 "[in module %s]"),
5223 namei, objfile_name (dwarf2_per_objfile->objfile));
5224 return NULL;
5225 }
5226 #endif
5227
5228 if (cmp (namei_string, name) == 0)
5229 {
5230 const ULONGEST namei_entry_offs
5231 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5232 + namei * map.offset_size),
5233 map.offset_size, map.dwarf5_byte_order);
5234 return map.entry_pool + namei_entry_offs;
5235 }
5236 }
5237
5238 ++namei;
5239 if (namei >= map.name_count)
5240 return NULL;
5241 }
5242 }
5243
5244 const gdb_byte *
5245 dw2_debug_names_iterator::find_vec_in_debug_names
5246 (const mapped_debug_names &map, uint32_t namei)
5247 {
5248 if (namei >= map.name_count)
5249 {
5250 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5251 "[in module %s]"),
5252 namei, map.name_count,
5253 objfile_name (map.dwarf2_per_objfile->objfile));
5254 return NULL;
5255 }
5256
5257 const ULONGEST namei_entry_offs
5258 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5259 + namei * map.offset_size),
5260 map.offset_size, map.dwarf5_byte_order);
5261 return map.entry_pool + namei_entry_offs;
5262 }
5263
5264 /* See dw2_debug_names_iterator. */
5265
5266 dwarf2_per_cu_data *
5267 dw2_debug_names_iterator::next ()
5268 {
5269 if (m_addr == NULL)
5270 return NULL;
5271
5272 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5273 struct objfile *objfile = dwarf2_per_objfile->objfile;
5274 bfd *const abfd = objfile->obfd;
5275
5276 again:
5277
5278 unsigned int bytes_read;
5279 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5280 m_addr += bytes_read;
5281 if (abbrev == 0)
5282 return NULL;
5283
5284 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5285 if (indexval_it == m_map.abbrev_map.cend ())
5286 {
5287 complaint (_("Wrong .debug_names undefined abbrev code %s "
5288 "[in module %s]"),
5289 pulongest (abbrev), objfile_name (objfile));
5290 return NULL;
5291 }
5292 const mapped_debug_names::index_val &indexval = indexval_it->second;
5293 enum class symbol_linkage {
5294 unknown,
5295 static_,
5296 extern_,
5297 } symbol_linkage_ = symbol_linkage::unknown;
5298 dwarf2_per_cu_data *per_cu = NULL;
5299 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5300 {
5301 ULONGEST ull;
5302 switch (attr.form)
5303 {
5304 case DW_FORM_implicit_const:
5305 ull = attr.implicit_const;
5306 break;
5307 case DW_FORM_flag_present:
5308 ull = 1;
5309 break;
5310 case DW_FORM_udata:
5311 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5312 m_addr += bytes_read;
5313 break;
5314 default:
5315 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5316 dwarf_form_name (attr.form),
5317 objfile_name (objfile));
5318 return NULL;
5319 }
5320 switch (attr.dw_idx)
5321 {
5322 case DW_IDX_compile_unit:
5323 /* Don't crash on bad data. */
5324 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5325 {
5326 complaint (_(".debug_names entry has bad CU index %s"
5327 " [in module %s]"),
5328 pulongest (ull),
5329 objfile_name (dwarf2_per_objfile->objfile));
5330 continue;
5331 }
5332 per_cu = dwarf2_per_objfile->get_cutu (ull);
5333 break;
5334 case DW_IDX_type_unit:
5335 /* Don't crash on bad data. */
5336 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5337 {
5338 complaint (_(".debug_names entry has bad TU index %s"
5339 " [in module %s]"),
5340 pulongest (ull),
5341 objfile_name (dwarf2_per_objfile->objfile));
5342 continue;
5343 }
5344 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5345 break;
5346 case DW_IDX_GNU_internal:
5347 if (!m_map.augmentation_is_gdb)
5348 break;
5349 symbol_linkage_ = symbol_linkage::static_;
5350 break;
5351 case DW_IDX_GNU_external:
5352 if (!m_map.augmentation_is_gdb)
5353 break;
5354 symbol_linkage_ = symbol_linkage::extern_;
5355 break;
5356 }
5357 }
5358
5359 /* Skip if already read in. */
5360 if (per_cu->v.quick->compunit_symtab)
5361 goto again;
5362
5363 /* Check static vs global. */
5364 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5365 {
5366 const bool want_static = *m_block_index == STATIC_BLOCK;
5367 const bool symbol_is_static =
5368 symbol_linkage_ == symbol_linkage::static_;
5369 if (want_static != symbol_is_static)
5370 goto again;
5371 }
5372
5373 /* Match dw2_symtab_iter_next, symbol_kind
5374 and debug_names::psymbol_tag. */
5375 switch (m_domain)
5376 {
5377 case VAR_DOMAIN:
5378 switch (indexval.dwarf_tag)
5379 {
5380 case DW_TAG_variable:
5381 case DW_TAG_subprogram:
5382 /* Some types are also in VAR_DOMAIN. */
5383 case DW_TAG_typedef:
5384 case DW_TAG_structure_type:
5385 break;
5386 default:
5387 goto again;
5388 }
5389 break;
5390 case STRUCT_DOMAIN:
5391 switch (indexval.dwarf_tag)
5392 {
5393 case DW_TAG_typedef:
5394 case DW_TAG_structure_type:
5395 break;
5396 default:
5397 goto again;
5398 }
5399 break;
5400 case LABEL_DOMAIN:
5401 switch (indexval.dwarf_tag)
5402 {
5403 case 0:
5404 case DW_TAG_variable:
5405 break;
5406 default:
5407 goto again;
5408 }
5409 break;
5410 case MODULE_DOMAIN:
5411 switch (indexval.dwarf_tag)
5412 {
5413 case DW_TAG_module:
5414 break;
5415 default:
5416 goto again;
5417 }
5418 break;
5419 default:
5420 break;
5421 }
5422
5423 /* Match dw2_expand_symtabs_matching, symbol_kind and
5424 debug_names::psymbol_tag. */
5425 switch (m_search)
5426 {
5427 case VARIABLES_DOMAIN:
5428 switch (indexval.dwarf_tag)
5429 {
5430 case DW_TAG_variable:
5431 break;
5432 default:
5433 goto again;
5434 }
5435 break;
5436 case FUNCTIONS_DOMAIN:
5437 switch (indexval.dwarf_tag)
5438 {
5439 case DW_TAG_subprogram:
5440 break;
5441 default:
5442 goto again;
5443 }
5444 break;
5445 case TYPES_DOMAIN:
5446 switch (indexval.dwarf_tag)
5447 {
5448 case DW_TAG_typedef:
5449 case DW_TAG_structure_type:
5450 break;
5451 default:
5452 goto again;
5453 }
5454 break;
5455 case MODULES_DOMAIN:
5456 switch (indexval.dwarf_tag)
5457 {
5458 case DW_TAG_module:
5459 break;
5460 default:
5461 goto again;
5462 }
5463 default:
5464 break;
5465 }
5466
5467 return per_cu;
5468 }
5469
5470 static struct compunit_symtab *
5471 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5472 const char *name, domain_enum domain)
5473 {
5474 struct dwarf2_per_objfile *dwarf2_per_objfile
5475 = get_dwarf2_per_objfile (objfile);
5476
5477 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5478 if (!mapp)
5479 {
5480 /* index is NULL if OBJF_READNOW. */
5481 return NULL;
5482 }
5483 const auto &map = *mapp;
5484
5485 dw2_debug_names_iterator iter (map, block_index, domain, name);
5486
5487 struct compunit_symtab *stab_best = NULL;
5488 struct dwarf2_per_cu_data *per_cu;
5489 while ((per_cu = iter.next ()) != NULL)
5490 {
5491 struct symbol *sym, *with_opaque = NULL;
5492 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5493 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5494 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5495
5496 sym = block_find_symbol (block, name, domain,
5497 block_find_non_opaque_type_preferred,
5498 &with_opaque);
5499
5500 /* Some caution must be observed with overloaded functions and
5501 methods, since the index will not contain any overload
5502 information (but NAME might contain it). */
5503
5504 if (sym != NULL
5505 && strcmp_iw (sym->search_name (), name) == 0)
5506 return stab;
5507 if (with_opaque != NULL
5508 && strcmp_iw (with_opaque->search_name (), name) == 0)
5509 stab_best = stab;
5510
5511 /* Keep looking through other CUs. */
5512 }
5513
5514 return stab_best;
5515 }
5516
5517 /* This dumps minimal information about .debug_names. It is called
5518 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5519 uses this to verify that .debug_names has been loaded. */
5520
5521 static void
5522 dw2_debug_names_dump (struct objfile *objfile)
5523 {
5524 struct dwarf2_per_objfile *dwarf2_per_objfile
5525 = get_dwarf2_per_objfile (objfile);
5526
5527 gdb_assert (dwarf2_per_objfile->using_index);
5528 printf_filtered (".debug_names:");
5529 if (dwarf2_per_objfile->debug_names_table)
5530 printf_filtered (" exists\n");
5531 else
5532 printf_filtered (" faked for \"readnow\"\n");
5533 printf_filtered ("\n");
5534 }
5535
5536 static void
5537 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5538 const char *func_name)
5539 {
5540 struct dwarf2_per_objfile *dwarf2_per_objfile
5541 = get_dwarf2_per_objfile (objfile);
5542
5543 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5544 if (dwarf2_per_objfile->debug_names_table)
5545 {
5546 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5547
5548 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5549
5550 struct dwarf2_per_cu_data *per_cu;
5551 while ((per_cu = iter.next ()) != NULL)
5552 dw2_instantiate_symtab (per_cu, false);
5553 }
5554 }
5555
5556 static void
5557 dw2_debug_names_map_matching_symbols
5558 (struct objfile *objfile,
5559 const lookup_name_info &name, domain_enum domain,
5560 int global,
5561 gdb::function_view<symbol_found_callback_ftype> callback,
5562 symbol_compare_ftype *ordered_compare)
5563 {
5564 struct dwarf2_per_objfile *dwarf2_per_objfile
5565 = get_dwarf2_per_objfile (objfile);
5566
5567 /* debug_names_table is NULL if OBJF_READNOW. */
5568 if (!dwarf2_per_objfile->debug_names_table)
5569 return;
5570
5571 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5572 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5573
5574 const char *match_name = name.ada ().lookup_name ().c_str ();
5575 auto matcher = [&] (const char *symname)
5576 {
5577 if (ordered_compare == nullptr)
5578 return true;
5579 return ordered_compare (symname, match_name) == 0;
5580 };
5581
5582 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5583 [&] (offset_type namei)
5584 {
5585 /* The name was matched, now expand corresponding CUs that were
5586 marked. */
5587 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5588
5589 struct dwarf2_per_cu_data *per_cu;
5590 while ((per_cu = iter.next ()) != NULL)
5591 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5592 return true;
5593 });
5594
5595 /* It's a shame we couldn't do this inside the
5596 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5597 that have already been expanded. Instead, this loop matches what
5598 the psymtab code does. */
5599 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5600 {
5601 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5602 if (cust != nullptr)
5603 {
5604 const struct block *block
5605 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5606 if (!iterate_over_symbols_terminated (block, name,
5607 domain, callback))
5608 break;
5609 }
5610 }
5611 }
5612
5613 static void
5614 dw2_debug_names_expand_symtabs_matching
5615 (struct objfile *objfile,
5616 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5617 const lookup_name_info &lookup_name,
5618 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5619 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5620 enum search_domain kind)
5621 {
5622 struct dwarf2_per_objfile *dwarf2_per_objfile
5623 = get_dwarf2_per_objfile (objfile);
5624
5625 /* debug_names_table is NULL if OBJF_READNOW. */
5626 if (!dwarf2_per_objfile->debug_names_table)
5627 return;
5628
5629 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5630
5631 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5632
5633 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5634 symbol_matcher,
5635 kind, [&] (offset_type namei)
5636 {
5637 /* The name was matched, now expand corresponding CUs that were
5638 marked. */
5639 dw2_debug_names_iterator iter (map, kind, namei);
5640
5641 struct dwarf2_per_cu_data *per_cu;
5642 while ((per_cu = iter.next ()) != NULL)
5643 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5644 expansion_notify);
5645 return true;
5646 });
5647 }
5648
5649 const struct quick_symbol_functions dwarf2_debug_names_functions =
5650 {
5651 dw2_has_symbols,
5652 dw2_find_last_source_symtab,
5653 dw2_forget_cached_source_info,
5654 dw2_map_symtabs_matching_filename,
5655 dw2_debug_names_lookup_symbol,
5656 NULL,
5657 dw2_print_stats,
5658 dw2_debug_names_dump,
5659 dw2_debug_names_expand_symtabs_for_function,
5660 dw2_expand_all_symtabs,
5661 dw2_expand_symtabs_with_fullname,
5662 dw2_debug_names_map_matching_symbols,
5663 dw2_debug_names_expand_symtabs_matching,
5664 dw2_find_pc_sect_compunit_symtab,
5665 NULL,
5666 dw2_map_symbol_filenames
5667 };
5668
5669 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5670 to either a dwarf2_per_objfile or dwz_file object. */
5671
5672 template <typename T>
5673 static gdb::array_view<const gdb_byte>
5674 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5675 {
5676 dwarf2_section_info *section = &section_owner->gdb_index;
5677
5678 if (section->empty ())
5679 return {};
5680
5681 /* Older elfutils strip versions could keep the section in the main
5682 executable while splitting it for the separate debug info file. */
5683 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5684 return {};
5685
5686 section->read (obj);
5687
5688 /* dwarf2_section_info::size is a bfd_size_type, while
5689 gdb::array_view works with size_t. On 32-bit hosts, with
5690 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5691 is 32-bit. So we need an explicit narrowing conversion here.
5692 This is fine, because it's impossible to allocate or mmap an
5693 array/buffer larger than what size_t can represent. */
5694 return gdb::make_array_view (section->buffer, section->size);
5695 }
5696
5697 /* Lookup the index cache for the contents of the index associated to
5698 DWARF2_OBJ. */
5699
5700 static gdb::array_view<const gdb_byte>
5701 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5702 {
5703 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5704 if (build_id == nullptr)
5705 return {};
5706
5707 return global_index_cache.lookup_gdb_index (build_id,
5708 &dwarf2_obj->index_cache_res);
5709 }
5710
5711 /* Same as the above, but for DWZ. */
5712
5713 static gdb::array_view<const gdb_byte>
5714 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5715 {
5716 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5717 if (build_id == nullptr)
5718 return {};
5719
5720 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5721 }
5722
5723 /* See symfile.h. */
5724
5725 bool
5726 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5727 {
5728 struct dwarf2_per_objfile *dwarf2_per_objfile
5729 = get_dwarf2_per_objfile (objfile);
5730
5731 /* If we're about to read full symbols, don't bother with the
5732 indices. In this case we also don't care if some other debug
5733 format is making psymtabs, because they are all about to be
5734 expanded anyway. */
5735 if ((objfile->flags & OBJF_READNOW))
5736 {
5737 dwarf2_per_objfile->using_index = 1;
5738 create_all_comp_units (dwarf2_per_objfile);
5739 create_all_type_units (dwarf2_per_objfile);
5740 dwarf2_per_objfile->quick_file_names_table
5741 = create_quick_file_names_table
5742 (dwarf2_per_objfile->all_comp_units.size ());
5743
5744 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5745 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5746 {
5747 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5748
5749 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5750 struct dwarf2_per_cu_quick_data);
5751 }
5752
5753 /* Return 1 so that gdb sees the "quick" functions. However,
5754 these functions will be no-ops because we will have expanded
5755 all symtabs. */
5756 *index_kind = dw_index_kind::GDB_INDEX;
5757 return true;
5758 }
5759
5760 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5761 {
5762 *index_kind = dw_index_kind::DEBUG_NAMES;
5763 return true;
5764 }
5765
5766 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5767 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5768 get_gdb_index_contents_from_section<dwz_file>))
5769 {
5770 *index_kind = dw_index_kind::GDB_INDEX;
5771 return true;
5772 }
5773
5774 /* ... otherwise, try to find the index in the index cache. */
5775 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5776 get_gdb_index_contents_from_cache,
5777 get_gdb_index_contents_from_cache_dwz))
5778 {
5779 global_index_cache.hit ();
5780 *index_kind = dw_index_kind::GDB_INDEX;
5781 return true;
5782 }
5783
5784 global_index_cache.miss ();
5785 return false;
5786 }
5787
5788 \f
5789
5790 /* Build a partial symbol table. */
5791
5792 void
5793 dwarf2_build_psymtabs (struct objfile *objfile)
5794 {
5795 struct dwarf2_per_objfile *dwarf2_per_objfile
5796 = get_dwarf2_per_objfile (objfile);
5797
5798 init_psymbol_list (objfile, 1024);
5799
5800 try
5801 {
5802 /* This isn't really ideal: all the data we allocate on the
5803 objfile's obstack is still uselessly kept around. However,
5804 freeing it seems unsafe. */
5805 psymtab_discarder psymtabs (objfile);
5806 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5807 psymtabs.keep ();
5808
5809 /* (maybe) store an index in the cache. */
5810 global_index_cache.store (dwarf2_per_objfile);
5811 }
5812 catch (const gdb_exception_error &except)
5813 {
5814 exception_print (gdb_stderr, except);
5815 }
5816 }
5817
5818 /* Find the base address of the compilation unit for range lists and
5819 location lists. It will normally be specified by DW_AT_low_pc.
5820 In DWARF-3 draft 4, the base address could be overridden by
5821 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5822 compilation units with discontinuous ranges. */
5823
5824 static void
5825 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5826 {
5827 struct attribute *attr;
5828
5829 cu->base_address.reset ();
5830
5831 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5832 if (attr != nullptr)
5833 cu->base_address = attr->value_as_address ();
5834 else
5835 {
5836 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5837 if (attr != nullptr)
5838 cu->base_address = attr->value_as_address ();
5839 }
5840 }
5841
5842 /* Helper function that returns the proper abbrev section for
5843 THIS_CU. */
5844
5845 static struct dwarf2_section_info *
5846 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5847 {
5848 struct dwarf2_section_info *abbrev;
5849 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5850
5851 if (this_cu->is_dwz)
5852 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5853 else
5854 abbrev = &dwarf2_per_objfile->abbrev;
5855
5856 return abbrev;
5857 }
5858
5859 /* Fetch the abbreviation table offset from a comp or type unit header. */
5860
5861 static sect_offset
5862 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5863 struct dwarf2_section_info *section,
5864 sect_offset sect_off)
5865 {
5866 bfd *abfd = section->get_bfd_owner ();
5867 const gdb_byte *info_ptr;
5868 unsigned int initial_length_size, offset_size;
5869 uint16_t version;
5870
5871 section->read (dwarf2_per_objfile->objfile);
5872 info_ptr = section->buffer + to_underlying (sect_off);
5873 read_initial_length (abfd, info_ptr, &initial_length_size);
5874 offset_size = initial_length_size == 4 ? 4 : 8;
5875 info_ptr += initial_length_size;
5876
5877 version = read_2_bytes (abfd, info_ptr);
5878 info_ptr += 2;
5879 if (version >= 5)
5880 {
5881 /* Skip unit type and address size. */
5882 info_ptr += 2;
5883 }
5884
5885 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5886 }
5887
5888 /* A partial symtab that is used only for include files. */
5889 struct dwarf2_include_psymtab : public partial_symtab
5890 {
5891 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5892 : partial_symtab (filename, objfile)
5893 {
5894 }
5895
5896 void read_symtab (struct objfile *objfile) override
5897 {
5898 expand_psymtab (objfile);
5899 }
5900
5901 void expand_psymtab (struct objfile *objfile) override
5902 {
5903 if (m_readin)
5904 return;
5905 /* It's an include file, no symbols to read for it.
5906 Everything is in the parent symtab. */
5907 expand_dependencies (objfile);
5908 m_readin = true;
5909 }
5910
5911 bool readin_p () const override
5912 {
5913 return m_readin;
5914 }
5915
5916 struct compunit_symtab *get_compunit_symtab () const override
5917 {
5918 return nullptr;
5919 }
5920
5921 private:
5922
5923 bool m_readin = false;
5924 };
5925
5926 /* Allocate a new partial symtab for file named NAME and mark this new
5927 partial symtab as being an include of PST. */
5928
5929 static void
5930 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5931 struct objfile *objfile)
5932 {
5933 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
5934
5935 if (!IS_ABSOLUTE_PATH (subpst->filename))
5936 {
5937 /* It shares objfile->objfile_obstack. */
5938 subpst->dirname = pst->dirname;
5939 }
5940
5941 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
5942 subpst->dependencies[0] = pst;
5943 subpst->number_of_dependencies = 1;
5944 }
5945
5946 /* Read the Line Number Program data and extract the list of files
5947 included by the source file represented by PST. Build an include
5948 partial symtab for each of these included files. */
5949
5950 static void
5951 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5952 struct die_info *die,
5953 dwarf2_psymtab *pst)
5954 {
5955 line_header_up lh;
5956 struct attribute *attr;
5957
5958 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5959 if (attr != nullptr)
5960 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5961 if (lh == NULL)
5962 return; /* No linetable, so no includes. */
5963
5964 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5965 that we pass in the raw text_low here; that is ok because we're
5966 only decoding the line table to make include partial symtabs, and
5967 so the addresses aren't really used. */
5968 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5969 pst->raw_text_low (), 1);
5970 }
5971
5972 static hashval_t
5973 hash_signatured_type (const void *item)
5974 {
5975 const struct signatured_type *sig_type
5976 = (const struct signatured_type *) item;
5977
5978 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5979 return sig_type->signature;
5980 }
5981
5982 static int
5983 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5984 {
5985 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5986 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5987
5988 return lhs->signature == rhs->signature;
5989 }
5990
5991 /* Allocate a hash table for signatured types. */
5992
5993 static htab_up
5994 allocate_signatured_type_table ()
5995 {
5996 return htab_up (htab_create_alloc (41,
5997 hash_signatured_type,
5998 eq_signatured_type,
5999 NULL, xcalloc, xfree));
6000 }
6001
6002 /* A helper function to add a signatured type CU to a table. */
6003
6004 static int
6005 add_signatured_type_cu_to_table (void **slot, void *datum)
6006 {
6007 struct signatured_type *sigt = (struct signatured_type *) *slot;
6008 std::vector<signatured_type *> *all_type_units
6009 = (std::vector<signatured_type *> *) datum;
6010
6011 all_type_units->push_back (sigt);
6012
6013 return 1;
6014 }
6015
6016 /* A helper for create_debug_types_hash_table. Read types from SECTION
6017 and fill them into TYPES_HTAB. It will process only type units,
6018 therefore DW_UT_type. */
6019
6020 static void
6021 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6022 struct dwo_file *dwo_file,
6023 dwarf2_section_info *section, htab_up &types_htab,
6024 rcuh_kind section_kind)
6025 {
6026 struct objfile *objfile = dwarf2_per_objfile->objfile;
6027 struct dwarf2_section_info *abbrev_section;
6028 bfd *abfd;
6029 const gdb_byte *info_ptr, *end_ptr;
6030
6031 abbrev_section = (dwo_file != NULL
6032 ? &dwo_file->sections.abbrev
6033 : &dwarf2_per_objfile->abbrev);
6034
6035 if (dwarf_read_debug)
6036 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6037 section->get_name (),
6038 abbrev_section->get_file_name ());
6039
6040 section->read (objfile);
6041 info_ptr = section->buffer;
6042
6043 if (info_ptr == NULL)
6044 return;
6045
6046 /* We can't set abfd until now because the section may be empty or
6047 not present, in which case the bfd is unknown. */
6048 abfd = section->get_bfd_owner ();
6049
6050 /* We don't use cutu_reader here because we don't need to read
6051 any dies: the signature is in the header. */
6052
6053 end_ptr = info_ptr + section->size;
6054 while (info_ptr < end_ptr)
6055 {
6056 struct signatured_type *sig_type;
6057 struct dwo_unit *dwo_tu;
6058 void **slot;
6059 const gdb_byte *ptr = info_ptr;
6060 struct comp_unit_head header;
6061 unsigned int length;
6062
6063 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6064
6065 /* Initialize it due to a false compiler warning. */
6066 header.signature = -1;
6067 header.type_cu_offset_in_tu = (cu_offset) -1;
6068
6069 /* We need to read the type's signature in order to build the hash
6070 table, but we don't need anything else just yet. */
6071
6072 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6073 abbrev_section, ptr, section_kind);
6074
6075 length = header.get_length ();
6076
6077 /* Skip dummy type units. */
6078 if (ptr >= info_ptr + length
6079 || peek_abbrev_code (abfd, ptr) == 0
6080 || header.unit_type != DW_UT_type)
6081 {
6082 info_ptr += length;
6083 continue;
6084 }
6085
6086 if (types_htab == NULL)
6087 {
6088 if (dwo_file)
6089 types_htab = allocate_dwo_unit_table ();
6090 else
6091 types_htab = allocate_signatured_type_table ();
6092 }
6093
6094 if (dwo_file)
6095 {
6096 sig_type = NULL;
6097 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6098 struct dwo_unit);
6099 dwo_tu->dwo_file = dwo_file;
6100 dwo_tu->signature = header.signature;
6101 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6102 dwo_tu->section = section;
6103 dwo_tu->sect_off = sect_off;
6104 dwo_tu->length = length;
6105 }
6106 else
6107 {
6108 /* N.B.: type_offset is not usable if this type uses a DWO file.
6109 The real type_offset is in the DWO file. */
6110 dwo_tu = NULL;
6111 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6112 struct signatured_type);
6113 sig_type->signature = header.signature;
6114 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6115 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6116 sig_type->per_cu.is_debug_types = 1;
6117 sig_type->per_cu.section = section;
6118 sig_type->per_cu.sect_off = sect_off;
6119 sig_type->per_cu.length = length;
6120 }
6121
6122 slot = htab_find_slot (types_htab.get (),
6123 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6124 INSERT);
6125 gdb_assert (slot != NULL);
6126 if (*slot != NULL)
6127 {
6128 sect_offset dup_sect_off;
6129
6130 if (dwo_file)
6131 {
6132 const struct dwo_unit *dup_tu
6133 = (const struct dwo_unit *) *slot;
6134
6135 dup_sect_off = dup_tu->sect_off;
6136 }
6137 else
6138 {
6139 const struct signatured_type *dup_tu
6140 = (const struct signatured_type *) *slot;
6141
6142 dup_sect_off = dup_tu->per_cu.sect_off;
6143 }
6144
6145 complaint (_("debug type entry at offset %s is duplicate to"
6146 " the entry at offset %s, signature %s"),
6147 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6148 hex_string (header.signature));
6149 }
6150 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6151
6152 if (dwarf_read_debug > 1)
6153 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6154 sect_offset_str (sect_off),
6155 hex_string (header.signature));
6156
6157 info_ptr += length;
6158 }
6159 }
6160
6161 /* Create the hash table of all entries in the .debug_types
6162 (or .debug_types.dwo) section(s).
6163 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6164 otherwise it is NULL.
6165
6166 The result is a pointer to the hash table or NULL if there are no types.
6167
6168 Note: This function processes DWO files only, not DWP files. */
6169
6170 static void
6171 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6172 struct dwo_file *dwo_file,
6173 gdb::array_view<dwarf2_section_info> type_sections,
6174 htab_up &types_htab)
6175 {
6176 for (dwarf2_section_info &section : type_sections)
6177 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6178 types_htab, rcuh_kind::TYPE);
6179 }
6180
6181 /* Create the hash table of all entries in the .debug_types section,
6182 and initialize all_type_units.
6183 The result is zero if there is an error (e.g. missing .debug_types section),
6184 otherwise non-zero. */
6185
6186 static int
6187 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6188 {
6189 htab_up types_htab;
6190
6191 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6192 &dwarf2_per_objfile->info, types_htab,
6193 rcuh_kind::COMPILE);
6194 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6195 dwarf2_per_objfile->types, types_htab);
6196 if (types_htab == NULL)
6197 {
6198 dwarf2_per_objfile->signatured_types = NULL;
6199 return 0;
6200 }
6201
6202 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6203
6204 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6205 dwarf2_per_objfile->all_type_units.reserve
6206 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6207
6208 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6209 add_signatured_type_cu_to_table,
6210 &dwarf2_per_objfile->all_type_units);
6211
6212 return 1;
6213 }
6214
6215 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6216 If SLOT is non-NULL, it is the entry to use in the hash table.
6217 Otherwise we find one. */
6218
6219 static struct signatured_type *
6220 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6221 void **slot)
6222 {
6223 struct objfile *objfile = dwarf2_per_objfile->objfile;
6224
6225 if (dwarf2_per_objfile->all_type_units.size ()
6226 == dwarf2_per_objfile->all_type_units.capacity ())
6227 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6228
6229 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6230 struct signatured_type);
6231
6232 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6233 sig_type->signature = sig;
6234 sig_type->per_cu.is_debug_types = 1;
6235 if (dwarf2_per_objfile->using_index)
6236 {
6237 sig_type->per_cu.v.quick =
6238 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6239 struct dwarf2_per_cu_quick_data);
6240 }
6241
6242 if (slot == NULL)
6243 {
6244 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6245 sig_type, INSERT);
6246 }
6247 gdb_assert (*slot == NULL);
6248 *slot = sig_type;
6249 /* The rest of sig_type must be filled in by the caller. */
6250 return sig_type;
6251 }
6252
6253 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6254 Fill in SIG_ENTRY with DWO_ENTRY. */
6255
6256 static void
6257 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6258 struct signatured_type *sig_entry,
6259 struct dwo_unit *dwo_entry)
6260 {
6261 /* Make sure we're not clobbering something we don't expect to. */
6262 gdb_assert (! sig_entry->per_cu.queued);
6263 gdb_assert (sig_entry->per_cu.cu == NULL);
6264 if (dwarf2_per_objfile->using_index)
6265 {
6266 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6267 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6268 }
6269 else
6270 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6271 gdb_assert (sig_entry->signature == dwo_entry->signature);
6272 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6273 gdb_assert (sig_entry->type_unit_group == NULL);
6274 gdb_assert (sig_entry->dwo_unit == NULL);
6275
6276 sig_entry->per_cu.section = dwo_entry->section;
6277 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6278 sig_entry->per_cu.length = dwo_entry->length;
6279 sig_entry->per_cu.reading_dwo_directly = 1;
6280 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6281 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6282 sig_entry->dwo_unit = dwo_entry;
6283 }
6284
6285 /* Subroutine of lookup_signatured_type.
6286 If we haven't read the TU yet, create the signatured_type data structure
6287 for a TU to be read in directly from a DWO file, bypassing the stub.
6288 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6289 using .gdb_index, then when reading a CU we want to stay in the DWO file
6290 containing that CU. Otherwise we could end up reading several other DWO
6291 files (due to comdat folding) to process the transitive closure of all the
6292 mentioned TUs, and that can be slow. The current DWO file will have every
6293 type signature that it needs.
6294 We only do this for .gdb_index because in the psymtab case we already have
6295 to read all the DWOs to build the type unit groups. */
6296
6297 static struct signatured_type *
6298 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6299 {
6300 struct dwarf2_per_objfile *dwarf2_per_objfile
6301 = cu->per_cu->dwarf2_per_objfile;
6302 struct dwo_file *dwo_file;
6303 struct dwo_unit find_dwo_entry, *dwo_entry;
6304 struct signatured_type find_sig_entry, *sig_entry;
6305 void **slot;
6306
6307 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6308
6309 /* If TU skeletons have been removed then we may not have read in any
6310 TUs yet. */
6311 if (dwarf2_per_objfile->signatured_types == NULL)
6312 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6313
6314 /* We only ever need to read in one copy of a signatured type.
6315 Use the global signatured_types array to do our own comdat-folding
6316 of types. If this is the first time we're reading this TU, and
6317 the TU has an entry in .gdb_index, replace the recorded data from
6318 .gdb_index with this TU. */
6319
6320 find_sig_entry.signature = sig;
6321 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6322 &find_sig_entry, INSERT);
6323 sig_entry = (struct signatured_type *) *slot;
6324
6325 /* We can get here with the TU already read, *or* in the process of being
6326 read. Don't reassign the global entry to point to this DWO if that's
6327 the case. Also note that if the TU is already being read, it may not
6328 have come from a DWO, the program may be a mix of Fission-compiled
6329 code and non-Fission-compiled code. */
6330
6331 /* Have we already tried to read this TU?
6332 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6333 needn't exist in the global table yet). */
6334 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6335 return sig_entry;
6336
6337 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6338 dwo_unit of the TU itself. */
6339 dwo_file = cu->dwo_unit->dwo_file;
6340
6341 /* Ok, this is the first time we're reading this TU. */
6342 if (dwo_file->tus == NULL)
6343 return NULL;
6344 find_dwo_entry.signature = sig;
6345 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6346 &find_dwo_entry);
6347 if (dwo_entry == NULL)
6348 return NULL;
6349
6350 /* If the global table doesn't have an entry for this TU, add one. */
6351 if (sig_entry == NULL)
6352 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6353
6354 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6355 sig_entry->per_cu.tu_read = 1;
6356 return sig_entry;
6357 }
6358
6359 /* Subroutine of lookup_signatured_type.
6360 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6361 then try the DWP file. If the TU stub (skeleton) has been removed then
6362 it won't be in .gdb_index. */
6363
6364 static struct signatured_type *
6365 lookup_dwp_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 dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6370 struct dwo_unit *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 gdb_assert (dwp_file != NULL);
6376
6377 /* If TU skeletons have been removed then we may not have read in any
6378 TUs yet. */
6379 if (dwarf2_per_objfile->signatured_types == NULL)
6380 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6381
6382 find_sig_entry.signature = sig;
6383 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6384 &find_sig_entry, INSERT);
6385 sig_entry = (struct signatured_type *) *slot;
6386
6387 /* Have we already tried to read this TU?
6388 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6389 needn't exist in the global table yet). */
6390 if (sig_entry != NULL)
6391 return sig_entry;
6392
6393 if (dwp_file->tus == NULL)
6394 return NULL;
6395 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6396 sig, 1 /* is_debug_types */);
6397 if (dwo_entry == NULL)
6398 return NULL;
6399
6400 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6401 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6402
6403 return sig_entry;
6404 }
6405
6406 /* Lookup a signature based type for DW_FORM_ref_sig8.
6407 Returns NULL if signature SIG is not present in the table.
6408 It is up to the caller to complain about this. */
6409
6410 static struct signatured_type *
6411 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6412 {
6413 struct dwarf2_per_objfile *dwarf2_per_objfile
6414 = cu->per_cu->dwarf2_per_objfile;
6415
6416 if (cu->dwo_unit
6417 && dwarf2_per_objfile->using_index)
6418 {
6419 /* We're in a DWO/DWP file, and we're using .gdb_index.
6420 These cases require special processing. */
6421 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6422 return lookup_dwo_signatured_type (cu, sig);
6423 else
6424 return lookup_dwp_signatured_type (cu, sig);
6425 }
6426 else
6427 {
6428 struct signatured_type find_entry, *entry;
6429
6430 if (dwarf2_per_objfile->signatured_types == NULL)
6431 return NULL;
6432 find_entry.signature = sig;
6433 entry = ((struct signatured_type *)
6434 htab_find (dwarf2_per_objfile->signatured_types.get (),
6435 &find_entry));
6436 return entry;
6437 }
6438 }
6439
6440 /* Low level DIE reading support. */
6441
6442 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6443
6444 static void
6445 init_cu_die_reader (struct die_reader_specs *reader,
6446 struct dwarf2_cu *cu,
6447 struct dwarf2_section_info *section,
6448 struct dwo_file *dwo_file,
6449 struct abbrev_table *abbrev_table)
6450 {
6451 gdb_assert (section->readin && section->buffer != NULL);
6452 reader->abfd = section->get_bfd_owner ();
6453 reader->cu = cu;
6454 reader->dwo_file = dwo_file;
6455 reader->die_section = section;
6456 reader->buffer = section->buffer;
6457 reader->buffer_end = section->buffer + section->size;
6458 reader->abbrev_table = abbrev_table;
6459 }
6460
6461 /* Subroutine of cutu_reader to simplify it.
6462 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6463 There's just a lot of work to do, and cutu_reader is big enough
6464 already.
6465
6466 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6467 from it to the DIE in the DWO. If NULL we are skipping the stub.
6468 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6469 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6470 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6471 STUB_COMP_DIR may be non-NULL.
6472 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6473 are filled in with the info of the DIE from the DWO file.
6474 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6475 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6476 kept around for at least as long as *RESULT_READER.
6477
6478 The result is non-zero if a valid (non-dummy) DIE was found. */
6479
6480 static int
6481 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6482 struct dwo_unit *dwo_unit,
6483 struct die_info *stub_comp_unit_die,
6484 const char *stub_comp_dir,
6485 struct die_reader_specs *result_reader,
6486 const gdb_byte **result_info_ptr,
6487 struct die_info **result_comp_unit_die,
6488 abbrev_table_up *result_dwo_abbrev_table)
6489 {
6490 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6491 struct objfile *objfile = dwarf2_per_objfile->objfile;
6492 struct dwarf2_cu *cu = this_cu->cu;
6493 bfd *abfd;
6494 const gdb_byte *begin_info_ptr, *info_ptr;
6495 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6496 int i,num_extra_attrs;
6497 struct dwarf2_section_info *dwo_abbrev_section;
6498 struct die_info *comp_unit_die;
6499
6500 /* At most one of these may be provided. */
6501 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6502
6503 /* These attributes aren't processed until later:
6504 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6505 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6506 referenced later. However, these attributes are found in the stub
6507 which we won't have later. In order to not impose this complication
6508 on the rest of the code, we read them here and copy them to the
6509 DWO CU/TU die. */
6510
6511 stmt_list = NULL;
6512 low_pc = NULL;
6513 high_pc = NULL;
6514 ranges = NULL;
6515 comp_dir = NULL;
6516
6517 if (stub_comp_unit_die != NULL)
6518 {
6519 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6520 DWO file. */
6521 if (! this_cu->is_debug_types)
6522 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6523 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6524 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6525 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6526 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6527
6528 cu->addr_base = stub_comp_unit_die->addr_base ();
6529
6530 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6531 here (if needed). We need the value before we can process
6532 DW_AT_ranges. */
6533 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6534 }
6535 else if (stub_comp_dir != NULL)
6536 {
6537 /* Reconstruct the comp_dir attribute to simplify the code below. */
6538 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6539 comp_dir->name = DW_AT_comp_dir;
6540 comp_dir->form = DW_FORM_string;
6541 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6542 DW_STRING (comp_dir) = stub_comp_dir;
6543 }
6544
6545 /* Set up for reading the DWO CU/TU. */
6546 cu->dwo_unit = dwo_unit;
6547 dwarf2_section_info *section = dwo_unit->section;
6548 section->read (objfile);
6549 abfd = section->get_bfd_owner ();
6550 begin_info_ptr = info_ptr = (section->buffer
6551 + to_underlying (dwo_unit->sect_off));
6552 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6553
6554 if (this_cu->is_debug_types)
6555 {
6556 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6557
6558 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6559 &cu->header, section,
6560 dwo_abbrev_section,
6561 info_ptr, rcuh_kind::TYPE);
6562 /* This is not an assert because it can be caused by bad debug info. */
6563 if (sig_type->signature != cu->header.signature)
6564 {
6565 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6566 " TU at offset %s [in module %s]"),
6567 hex_string (sig_type->signature),
6568 hex_string (cu->header.signature),
6569 sect_offset_str (dwo_unit->sect_off),
6570 bfd_get_filename (abfd));
6571 }
6572 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6573 /* For DWOs coming from DWP files, we don't know the CU length
6574 nor the type's offset in the TU until now. */
6575 dwo_unit->length = cu->header.get_length ();
6576 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6577
6578 /* Establish the type offset that can be used to lookup the type.
6579 For DWO files, we don't know it until now. */
6580 sig_type->type_offset_in_section
6581 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6582 }
6583 else
6584 {
6585 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6586 &cu->header, section,
6587 dwo_abbrev_section,
6588 info_ptr, rcuh_kind::COMPILE);
6589 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6590 /* For DWOs coming from DWP files, we don't know the CU length
6591 until now. */
6592 dwo_unit->length = cu->header.get_length ();
6593 }
6594
6595 *result_dwo_abbrev_table
6596 = abbrev_table::read (objfile, dwo_abbrev_section,
6597 cu->header.abbrev_sect_off);
6598 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6599 result_dwo_abbrev_table->get ());
6600
6601 /* Read in the die, but leave space to copy over the attributes
6602 from the stub. This has the benefit of simplifying the rest of
6603 the code - all the work to maintain the illusion of a single
6604 DW_TAG_{compile,type}_unit DIE is done here. */
6605 num_extra_attrs = ((stmt_list != NULL)
6606 + (low_pc != NULL)
6607 + (high_pc != NULL)
6608 + (ranges != NULL)
6609 + (comp_dir != NULL));
6610 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6611 num_extra_attrs);
6612
6613 /* Copy over the attributes from the stub to the DIE we just read in. */
6614 comp_unit_die = *result_comp_unit_die;
6615 i = comp_unit_die->num_attrs;
6616 if (stmt_list != NULL)
6617 comp_unit_die->attrs[i++] = *stmt_list;
6618 if (low_pc != NULL)
6619 comp_unit_die->attrs[i++] = *low_pc;
6620 if (high_pc != NULL)
6621 comp_unit_die->attrs[i++] = *high_pc;
6622 if (ranges != NULL)
6623 comp_unit_die->attrs[i++] = *ranges;
6624 if (comp_dir != NULL)
6625 comp_unit_die->attrs[i++] = *comp_dir;
6626 comp_unit_die->num_attrs += num_extra_attrs;
6627
6628 if (dwarf_die_debug)
6629 {
6630 fprintf_unfiltered (gdb_stdlog,
6631 "Read die from %s@0x%x of %s:\n",
6632 section->get_name (),
6633 (unsigned) (begin_info_ptr - section->buffer),
6634 bfd_get_filename (abfd));
6635 dump_die (comp_unit_die, dwarf_die_debug);
6636 }
6637
6638 /* Skip dummy compilation units. */
6639 if (info_ptr >= begin_info_ptr + dwo_unit->length
6640 || peek_abbrev_code (abfd, info_ptr) == 0)
6641 return 0;
6642
6643 *result_info_ptr = info_ptr;
6644 return 1;
6645 }
6646
6647 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6648 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6649 signature is part of the header. */
6650 static gdb::optional<ULONGEST>
6651 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6652 {
6653 if (cu->header.version >= 5)
6654 return cu->header.signature;
6655 struct attribute *attr;
6656 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6657 if (attr == nullptr)
6658 return gdb::optional<ULONGEST> ();
6659 return DW_UNSND (attr);
6660 }
6661
6662 /* Subroutine of cutu_reader to simplify it.
6663 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6664 Returns NULL if the specified DWO unit cannot be found. */
6665
6666 static struct dwo_unit *
6667 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6668 struct die_info *comp_unit_die,
6669 const char *dwo_name)
6670 {
6671 struct dwarf2_cu *cu = this_cu->cu;
6672 struct dwo_unit *dwo_unit;
6673 const char *comp_dir;
6674
6675 gdb_assert (cu != NULL);
6676
6677 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6678 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6679 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6680
6681 if (this_cu->is_debug_types)
6682 {
6683 struct signatured_type *sig_type;
6684
6685 /* Since this_cu is the first member of struct signatured_type,
6686 we can go from a pointer to one to a pointer to the other. */
6687 sig_type = (struct signatured_type *) this_cu;
6688 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6689 }
6690 else
6691 {
6692 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6693 if (!signature.has_value ())
6694 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6695 " [in module %s]"),
6696 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6697 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6698 *signature);
6699 }
6700
6701 return dwo_unit;
6702 }
6703
6704 /* Subroutine of cutu_reader to simplify it.
6705 See it for a description of the parameters.
6706 Read a TU directly from a DWO file, bypassing the stub. */
6707
6708 void
6709 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6710 int use_existing_cu)
6711 {
6712 struct signatured_type *sig_type;
6713
6714 /* Verify we can do the following downcast, and that we have the
6715 data we need. */
6716 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6717 sig_type = (struct signatured_type *) this_cu;
6718 gdb_assert (sig_type->dwo_unit != NULL);
6719
6720 if (use_existing_cu && this_cu->cu != NULL)
6721 {
6722 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6723 /* There's no need to do the rereading_dwo_cu handling that
6724 cutu_reader does since we don't read the stub. */
6725 }
6726 else
6727 {
6728 /* If !use_existing_cu, this_cu->cu must be NULL. */
6729 gdb_assert (this_cu->cu == NULL);
6730 m_new_cu.reset (new dwarf2_cu (this_cu));
6731 }
6732
6733 /* A future optimization, if needed, would be to use an existing
6734 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6735 could share abbrev tables. */
6736
6737 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6738 NULL /* stub_comp_unit_die */,
6739 sig_type->dwo_unit->dwo_file->comp_dir,
6740 this, &info_ptr,
6741 &comp_unit_die,
6742 &m_dwo_abbrev_table) == 0)
6743 {
6744 /* Dummy die. */
6745 dummy_p = true;
6746 }
6747 }
6748
6749 /* Initialize a CU (or TU) and read its DIEs.
6750 If the CU defers to a DWO file, read the DWO file as well.
6751
6752 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6753 Otherwise the table specified in the comp unit header is read in and used.
6754 This is an optimization for when we already have the abbrev table.
6755
6756 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6757 Otherwise, a new CU is allocated with xmalloc. */
6758
6759 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6760 struct abbrev_table *abbrev_table,
6761 int use_existing_cu,
6762 bool skip_partial)
6763 : die_reader_specs {},
6764 m_this_cu (this_cu)
6765 {
6766 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6767 struct objfile *objfile = dwarf2_per_objfile->objfile;
6768 struct dwarf2_section_info *section = this_cu->section;
6769 bfd *abfd = section->get_bfd_owner ();
6770 struct dwarf2_cu *cu;
6771 const gdb_byte *begin_info_ptr;
6772 struct signatured_type *sig_type = NULL;
6773 struct dwarf2_section_info *abbrev_section;
6774 /* Non-zero if CU currently points to a DWO file and we need to
6775 reread it. When this happens we need to reread the skeleton die
6776 before we can reread the DWO file (this only applies to CUs, not TUs). */
6777 int rereading_dwo_cu = 0;
6778
6779 if (dwarf_die_debug)
6780 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6781 this_cu->is_debug_types ? "type" : "comp",
6782 sect_offset_str (this_cu->sect_off));
6783
6784 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6785 file (instead of going through the stub), short-circuit all of this. */
6786 if (this_cu->reading_dwo_directly)
6787 {
6788 /* Narrow down the scope of possibilities to have to understand. */
6789 gdb_assert (this_cu->is_debug_types);
6790 gdb_assert (abbrev_table == NULL);
6791 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6792 return;
6793 }
6794
6795 /* This is cheap if the section is already read in. */
6796 section->read (objfile);
6797
6798 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6799
6800 abbrev_section = get_abbrev_section_for_cu (this_cu);
6801
6802 if (use_existing_cu && this_cu->cu != NULL)
6803 {
6804 cu = this_cu->cu;
6805 /* If this CU is from a DWO file we need to start over, we need to
6806 refetch the attributes from the skeleton CU.
6807 This could be optimized by retrieving those attributes from when we
6808 were here the first time: the previous comp_unit_die was stored in
6809 comp_unit_obstack. But there's no data yet that we need this
6810 optimization. */
6811 if (cu->dwo_unit != NULL)
6812 rereading_dwo_cu = 1;
6813 }
6814 else
6815 {
6816 /* If !use_existing_cu, this_cu->cu must be NULL. */
6817 gdb_assert (this_cu->cu == NULL);
6818 m_new_cu.reset (new dwarf2_cu (this_cu));
6819 cu = m_new_cu.get ();
6820 }
6821
6822 /* Get the header. */
6823 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6824 {
6825 /* We already have the header, there's no need to read it in again. */
6826 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6827 }
6828 else
6829 {
6830 if (this_cu->is_debug_types)
6831 {
6832 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6833 &cu->header, section,
6834 abbrev_section, info_ptr,
6835 rcuh_kind::TYPE);
6836
6837 /* Since per_cu is the first member of struct signatured_type,
6838 we can go from a pointer to one to a pointer to the other. */
6839 sig_type = (struct signatured_type *) this_cu;
6840 gdb_assert (sig_type->signature == cu->header.signature);
6841 gdb_assert (sig_type->type_offset_in_tu
6842 == cu->header.type_cu_offset_in_tu);
6843 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6844
6845 /* LENGTH has not been set yet for type units if we're
6846 using .gdb_index. */
6847 this_cu->length = cu->header.get_length ();
6848
6849 /* Establish the type offset that can be used to lookup the type. */
6850 sig_type->type_offset_in_section =
6851 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6852
6853 this_cu->dwarf_version = cu->header.version;
6854 }
6855 else
6856 {
6857 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6858 &cu->header, section,
6859 abbrev_section,
6860 info_ptr,
6861 rcuh_kind::COMPILE);
6862
6863 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6864 gdb_assert (this_cu->length == cu->header.get_length ());
6865 this_cu->dwarf_version = cu->header.version;
6866 }
6867 }
6868
6869 /* Skip dummy compilation units. */
6870 if (info_ptr >= begin_info_ptr + this_cu->length
6871 || peek_abbrev_code (abfd, info_ptr) == 0)
6872 {
6873 dummy_p = true;
6874 return;
6875 }
6876
6877 /* If we don't have them yet, read the abbrevs for this compilation unit.
6878 And if we need to read them now, make sure they're freed when we're
6879 done. */
6880 if (abbrev_table != NULL)
6881 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6882 else
6883 {
6884 m_abbrev_table_holder
6885 = abbrev_table::read (objfile, abbrev_section,
6886 cu->header.abbrev_sect_off);
6887 abbrev_table = m_abbrev_table_holder.get ();
6888 }
6889
6890 /* Read the top level CU/TU die. */
6891 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6892 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6893
6894 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6895 {
6896 dummy_p = true;
6897 return;
6898 }
6899
6900 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6901 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6902 table from the DWO file and pass the ownership over to us. It will be
6903 referenced from READER, so we must make sure to free it after we're done
6904 with READER.
6905
6906 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6907 DWO CU, that this test will fail (the attribute will not be present). */
6908 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6909 if (dwo_name != nullptr)
6910 {
6911 struct dwo_unit *dwo_unit;
6912 struct die_info *dwo_comp_unit_die;
6913
6914 if (comp_unit_die->has_children)
6915 {
6916 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6917 " has children (offset %s) [in module %s]"),
6918 sect_offset_str (this_cu->sect_off),
6919 bfd_get_filename (abfd));
6920 }
6921 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6922 if (dwo_unit != NULL)
6923 {
6924 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6925 comp_unit_die, NULL,
6926 this, &info_ptr,
6927 &dwo_comp_unit_die,
6928 &m_dwo_abbrev_table) == 0)
6929 {
6930 /* Dummy die. */
6931 dummy_p = true;
6932 return;
6933 }
6934 comp_unit_die = dwo_comp_unit_die;
6935 }
6936 else
6937 {
6938 /* Yikes, we couldn't find the rest of the DIE, we only have
6939 the stub. A complaint has already been logged. There's
6940 not much more we can do except pass on the stub DIE to
6941 die_reader_func. We don't want to throw an error on bad
6942 debug info. */
6943 }
6944 }
6945 }
6946
6947 void
6948 cutu_reader::keep ()
6949 {
6950 /* Done, clean up. */
6951 gdb_assert (!dummy_p);
6952 if (m_new_cu != NULL)
6953 {
6954 struct dwarf2_per_objfile *dwarf2_per_objfile
6955 = m_this_cu->dwarf2_per_objfile;
6956 /* Link this CU into read_in_chain. */
6957 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6958 dwarf2_per_objfile->read_in_chain = m_this_cu;
6959 /* The chain owns it now. */
6960 m_new_cu.release ();
6961 }
6962 }
6963
6964 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6965 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6966 assumed to have already done the lookup to find the DWO file).
6967
6968 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6969 THIS_CU->is_debug_types, but nothing else.
6970
6971 We fill in THIS_CU->length.
6972
6973 THIS_CU->cu is always freed when done.
6974 This is done in order to not leave THIS_CU->cu in a state where we have
6975 to care whether it refers to the "main" CU or the DWO CU.
6976
6977 When parent_cu is passed, it is used to provide a default value for
6978 str_offsets_base and addr_base from the parent. */
6979
6980 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6981 struct dwarf2_cu *parent_cu,
6982 struct dwo_file *dwo_file)
6983 : die_reader_specs {},
6984 m_this_cu (this_cu)
6985 {
6986 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6987 struct objfile *objfile = dwarf2_per_objfile->objfile;
6988 struct dwarf2_section_info *section = this_cu->section;
6989 bfd *abfd = section->get_bfd_owner ();
6990 struct dwarf2_section_info *abbrev_section;
6991 const gdb_byte *begin_info_ptr, *info_ptr;
6992
6993 if (dwarf_die_debug)
6994 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6995 this_cu->is_debug_types ? "type" : "comp",
6996 sect_offset_str (this_cu->sect_off));
6997
6998 gdb_assert (this_cu->cu == NULL);
6999
7000 abbrev_section = (dwo_file != NULL
7001 ? &dwo_file->sections.abbrev
7002 : get_abbrev_section_for_cu (this_cu));
7003
7004 /* This is cheap if the section is already read in. */
7005 section->read (objfile);
7006
7007 m_new_cu.reset (new dwarf2_cu (this_cu));
7008
7009 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7010 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7011 &m_new_cu->header, section,
7012 abbrev_section, info_ptr,
7013 (this_cu->is_debug_types
7014 ? rcuh_kind::TYPE
7015 : rcuh_kind::COMPILE));
7016
7017 if (parent_cu != nullptr)
7018 {
7019 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7020 m_new_cu->addr_base = parent_cu->addr_base;
7021 }
7022 this_cu->length = m_new_cu->header.get_length ();
7023
7024 /* Skip dummy compilation units. */
7025 if (info_ptr >= begin_info_ptr + this_cu->length
7026 || peek_abbrev_code (abfd, info_ptr) == 0)
7027 {
7028 dummy_p = true;
7029 return;
7030 }
7031
7032 m_abbrev_table_holder
7033 = abbrev_table::read (objfile, abbrev_section,
7034 m_new_cu->header.abbrev_sect_off);
7035
7036 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7037 m_abbrev_table_holder.get ());
7038 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7039 }
7040
7041 \f
7042 /* Type Unit Groups.
7043
7044 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7045 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7046 so that all types coming from the same compilation (.o file) are grouped
7047 together. A future step could be to put the types in the same symtab as
7048 the CU the types ultimately came from. */
7049
7050 static hashval_t
7051 hash_type_unit_group (const void *item)
7052 {
7053 const struct type_unit_group *tu_group
7054 = (const struct type_unit_group *) item;
7055
7056 return hash_stmt_list_entry (&tu_group->hash);
7057 }
7058
7059 static int
7060 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7061 {
7062 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7063 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7064
7065 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7066 }
7067
7068 /* Allocate a hash table for type unit groups. */
7069
7070 static htab_up
7071 allocate_type_unit_groups_table ()
7072 {
7073 return htab_up (htab_create_alloc (3,
7074 hash_type_unit_group,
7075 eq_type_unit_group,
7076 NULL, xcalloc, xfree));
7077 }
7078
7079 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7080 partial symtabs. We combine several TUs per psymtab to not let the size
7081 of any one psymtab grow too big. */
7082 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7083 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7084
7085 /* Helper routine for get_type_unit_group.
7086 Create the type_unit_group object used to hold one or more TUs. */
7087
7088 static struct type_unit_group *
7089 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7090 {
7091 struct dwarf2_per_objfile *dwarf2_per_objfile
7092 = cu->per_cu->dwarf2_per_objfile;
7093 struct objfile *objfile = dwarf2_per_objfile->objfile;
7094 struct dwarf2_per_cu_data *per_cu;
7095 struct type_unit_group *tu_group;
7096
7097 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7098 struct type_unit_group);
7099 per_cu = &tu_group->per_cu;
7100 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7101
7102 if (dwarf2_per_objfile->using_index)
7103 {
7104 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7105 struct dwarf2_per_cu_quick_data);
7106 }
7107 else
7108 {
7109 unsigned int line_offset = to_underlying (line_offset_struct);
7110 dwarf2_psymtab *pst;
7111 std::string name;
7112
7113 /* Give the symtab a useful name for debug purposes. */
7114 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7115 name = string_printf ("<type_units_%d>",
7116 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7117 else
7118 name = string_printf ("<type_units_at_0x%x>", line_offset);
7119
7120 pst = create_partial_symtab (per_cu, name.c_str ());
7121 pst->anonymous = true;
7122 }
7123
7124 tu_group->hash.dwo_unit = cu->dwo_unit;
7125 tu_group->hash.line_sect_off = line_offset_struct;
7126
7127 return tu_group;
7128 }
7129
7130 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7131 STMT_LIST is a DW_AT_stmt_list attribute. */
7132
7133 static struct type_unit_group *
7134 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7135 {
7136 struct dwarf2_per_objfile *dwarf2_per_objfile
7137 = cu->per_cu->dwarf2_per_objfile;
7138 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7139 struct type_unit_group *tu_group;
7140 void **slot;
7141 unsigned int line_offset;
7142 struct type_unit_group type_unit_group_for_lookup;
7143
7144 if (dwarf2_per_objfile->type_unit_groups == NULL)
7145 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
7146
7147 /* Do we need to create a new group, or can we use an existing one? */
7148
7149 if (stmt_list)
7150 {
7151 line_offset = DW_UNSND (stmt_list);
7152 ++tu_stats->nr_symtab_sharers;
7153 }
7154 else
7155 {
7156 /* Ugh, no stmt_list. Rare, but we have to handle it.
7157 We can do various things here like create one group per TU or
7158 spread them over multiple groups to split up the expansion work.
7159 To avoid worst case scenarios (too many groups or too large groups)
7160 we, umm, group them in bunches. */
7161 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7162 | (tu_stats->nr_stmt_less_type_units
7163 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7164 ++tu_stats->nr_stmt_less_type_units;
7165 }
7166
7167 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7168 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7169 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7170 &type_unit_group_for_lookup, INSERT);
7171 if (*slot != NULL)
7172 {
7173 tu_group = (struct type_unit_group *) *slot;
7174 gdb_assert (tu_group != NULL);
7175 }
7176 else
7177 {
7178 sect_offset line_offset_struct = (sect_offset) line_offset;
7179 tu_group = create_type_unit_group (cu, line_offset_struct);
7180 *slot = tu_group;
7181 ++tu_stats->nr_symtabs;
7182 }
7183
7184 return tu_group;
7185 }
7186 \f
7187 /* Partial symbol tables. */
7188
7189 /* Create a psymtab named NAME and assign it to PER_CU.
7190
7191 The caller must fill in the following details:
7192 dirname, textlow, texthigh. */
7193
7194 static dwarf2_psymtab *
7195 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7196 {
7197 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7198 dwarf2_psymtab *pst;
7199
7200 pst = new dwarf2_psymtab (name, objfile, per_cu);
7201
7202 pst->psymtabs_addrmap_supported = true;
7203
7204 /* This is the glue that links PST into GDB's symbol API. */
7205 per_cu->v.psymtab = pst;
7206
7207 return pst;
7208 }
7209
7210 /* DIE reader function for process_psymtab_comp_unit. */
7211
7212 static void
7213 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7214 const gdb_byte *info_ptr,
7215 struct die_info *comp_unit_die,
7216 enum language pretend_language)
7217 {
7218 struct dwarf2_cu *cu = reader->cu;
7219 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7220 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7221 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7222 CORE_ADDR baseaddr;
7223 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7224 dwarf2_psymtab *pst;
7225 enum pc_bounds_kind cu_bounds_kind;
7226 const char *filename;
7227
7228 gdb_assert (! per_cu->is_debug_types);
7229
7230 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7231
7232 /* Allocate a new partial symbol table structure. */
7233 gdb::unique_xmalloc_ptr<char> debug_filename;
7234 static const char artificial[] = "<artificial>";
7235 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7236 if (filename == NULL)
7237 filename = "";
7238 else if (strcmp (filename, artificial) == 0)
7239 {
7240 debug_filename.reset (concat (artificial, "@",
7241 sect_offset_str (per_cu->sect_off),
7242 (char *) NULL));
7243 filename = debug_filename.get ();
7244 }
7245
7246 pst = create_partial_symtab (per_cu, filename);
7247
7248 /* This must be done before calling dwarf2_build_include_psymtabs. */
7249 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7250
7251 baseaddr = objfile->text_section_offset ();
7252
7253 dwarf2_find_base_address (comp_unit_die, cu);
7254
7255 /* Possibly set the default values of LOWPC and HIGHPC from
7256 `DW_AT_ranges'. */
7257 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7258 &best_highpc, cu, pst);
7259 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7260 {
7261 CORE_ADDR low
7262 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7263 - baseaddr);
7264 CORE_ADDR high
7265 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7266 - baseaddr - 1);
7267 /* Store the contiguous range if it is not empty; it can be
7268 empty for CUs with no code. */
7269 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7270 low, high, pst);
7271 }
7272
7273 /* Check if comp unit has_children.
7274 If so, read the rest of the partial symbols from this comp unit.
7275 If not, there's no more debug_info for this comp unit. */
7276 if (comp_unit_die->has_children)
7277 {
7278 struct partial_die_info *first_die;
7279 CORE_ADDR lowpc, highpc;
7280
7281 lowpc = ((CORE_ADDR) -1);
7282 highpc = ((CORE_ADDR) 0);
7283
7284 first_die = load_partial_dies (reader, info_ptr, 1);
7285
7286 scan_partial_symbols (first_die, &lowpc, &highpc,
7287 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7288
7289 /* If we didn't find a lowpc, set it to highpc to avoid
7290 complaints from `maint check'. */
7291 if (lowpc == ((CORE_ADDR) -1))
7292 lowpc = highpc;
7293
7294 /* If the compilation unit didn't have an explicit address range,
7295 then use the information extracted from its child dies. */
7296 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7297 {
7298 best_lowpc = lowpc;
7299 best_highpc = highpc;
7300 }
7301 }
7302 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7303 best_lowpc + baseaddr)
7304 - baseaddr);
7305 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7306 best_highpc + baseaddr)
7307 - baseaddr);
7308
7309 end_psymtab_common (objfile, pst);
7310
7311 if (!cu->per_cu->imported_symtabs_empty ())
7312 {
7313 int i;
7314 int len = cu->per_cu->imported_symtabs_size ();
7315
7316 /* Fill in 'dependencies' here; we fill in 'users' in a
7317 post-pass. */
7318 pst->number_of_dependencies = len;
7319 pst->dependencies
7320 = objfile->partial_symtabs->allocate_dependencies (len);
7321 for (i = 0; i < len; ++i)
7322 {
7323 pst->dependencies[i]
7324 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7325 }
7326
7327 cu->per_cu->imported_symtabs_free ();
7328 }
7329
7330 /* Get the list of files included in the current compilation unit,
7331 and build a psymtab for each of them. */
7332 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7333
7334 if (dwarf_read_debug)
7335 fprintf_unfiltered (gdb_stdlog,
7336 "Psymtab for %s unit @%s: %s - %s"
7337 ", %d global, %d static syms\n",
7338 per_cu->is_debug_types ? "type" : "comp",
7339 sect_offset_str (per_cu->sect_off),
7340 paddress (gdbarch, pst->text_low (objfile)),
7341 paddress (gdbarch, pst->text_high (objfile)),
7342 pst->n_global_syms, pst->n_static_syms);
7343 }
7344
7345 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7346 Process compilation unit THIS_CU for a psymtab. */
7347
7348 static void
7349 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7350 bool want_partial_unit,
7351 enum language pretend_language)
7352 {
7353 /* If this compilation unit was already read in, free the
7354 cached copy in order to read it in again. This is
7355 necessary because we skipped some symbols when we first
7356 read in the compilation unit (see load_partial_dies).
7357 This problem could be avoided, but the benefit is unclear. */
7358 if (this_cu->cu != NULL)
7359 free_one_cached_comp_unit (this_cu);
7360
7361 cutu_reader reader (this_cu, NULL, 0, false);
7362
7363 switch (reader.comp_unit_die->tag)
7364 {
7365 case DW_TAG_compile_unit:
7366 this_cu->unit_type = DW_UT_compile;
7367 break;
7368 case DW_TAG_partial_unit:
7369 this_cu->unit_type = DW_UT_partial;
7370 break;
7371 default:
7372 abort ();
7373 }
7374
7375 if (reader.dummy_p)
7376 {
7377 /* Nothing. */
7378 }
7379 else if (this_cu->is_debug_types)
7380 build_type_psymtabs_reader (&reader, reader.info_ptr,
7381 reader.comp_unit_die);
7382 else if (want_partial_unit
7383 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7384 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7385 reader.comp_unit_die,
7386 pretend_language);
7387
7388 this_cu->lang = this_cu->cu->language;
7389
7390 /* Age out any secondary CUs. */
7391 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7392 }
7393
7394 /* Reader function for build_type_psymtabs. */
7395
7396 static void
7397 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7398 const gdb_byte *info_ptr,
7399 struct die_info *type_unit_die)
7400 {
7401 struct dwarf2_per_objfile *dwarf2_per_objfile
7402 = reader->cu->per_cu->dwarf2_per_objfile;
7403 struct objfile *objfile = dwarf2_per_objfile->objfile;
7404 struct dwarf2_cu *cu = reader->cu;
7405 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7406 struct signatured_type *sig_type;
7407 struct type_unit_group *tu_group;
7408 struct attribute *attr;
7409 struct partial_die_info *first_die;
7410 CORE_ADDR lowpc, highpc;
7411 dwarf2_psymtab *pst;
7412
7413 gdb_assert (per_cu->is_debug_types);
7414 sig_type = (struct signatured_type *) per_cu;
7415
7416 if (! type_unit_die->has_children)
7417 return;
7418
7419 attr = type_unit_die->attr (DW_AT_stmt_list);
7420 tu_group = get_type_unit_group (cu, attr);
7421
7422 if (tu_group->tus == nullptr)
7423 tu_group->tus = new std::vector<signatured_type *>;
7424 tu_group->tus->push_back (sig_type);
7425
7426 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7427 pst = create_partial_symtab (per_cu, "");
7428 pst->anonymous = true;
7429
7430 first_die = load_partial_dies (reader, info_ptr, 1);
7431
7432 lowpc = (CORE_ADDR) -1;
7433 highpc = (CORE_ADDR) 0;
7434 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7435
7436 end_psymtab_common (objfile, pst);
7437 }
7438
7439 /* Struct used to sort TUs by their abbreviation table offset. */
7440
7441 struct tu_abbrev_offset
7442 {
7443 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7444 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7445 {}
7446
7447 signatured_type *sig_type;
7448 sect_offset abbrev_offset;
7449 };
7450
7451 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7452
7453 static bool
7454 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7455 const struct tu_abbrev_offset &b)
7456 {
7457 return a.abbrev_offset < b.abbrev_offset;
7458 }
7459
7460 /* Efficiently read all the type units.
7461 This does the bulk of the work for build_type_psymtabs.
7462
7463 The efficiency is because we sort TUs by the abbrev table they use and
7464 only read each abbrev table once. In one program there are 200K TUs
7465 sharing 8K abbrev tables.
7466
7467 The main purpose of this function is to support building the
7468 dwarf2_per_objfile->type_unit_groups table.
7469 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7470 can collapse the search space by grouping them by stmt_list.
7471 The savings can be significant, in the same program from above the 200K TUs
7472 share 8K stmt_list tables.
7473
7474 FUNC is expected to call get_type_unit_group, which will create the
7475 struct type_unit_group if necessary and add it to
7476 dwarf2_per_objfile->type_unit_groups. */
7477
7478 static void
7479 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7480 {
7481 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7482 abbrev_table_up abbrev_table;
7483 sect_offset abbrev_offset;
7484
7485 /* It's up to the caller to not call us multiple times. */
7486 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7487
7488 if (dwarf2_per_objfile->all_type_units.empty ())
7489 return;
7490
7491 /* TUs typically share abbrev tables, and there can be way more TUs than
7492 abbrev tables. Sort by abbrev table to reduce the number of times we
7493 read each abbrev table in.
7494 Alternatives are to punt or to maintain a cache of abbrev tables.
7495 This is simpler and efficient enough for now.
7496
7497 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7498 symtab to use). Typically TUs with the same abbrev offset have the same
7499 stmt_list value too so in practice this should work well.
7500
7501 The basic algorithm here is:
7502
7503 sort TUs by abbrev table
7504 for each TU with same abbrev table:
7505 read abbrev table if first user
7506 read TU top level DIE
7507 [IWBN if DWO skeletons had DW_AT_stmt_list]
7508 call FUNC */
7509
7510 if (dwarf_read_debug)
7511 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7512
7513 /* Sort in a separate table to maintain the order of all_type_units
7514 for .gdb_index: TU indices directly index all_type_units. */
7515 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7516 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7517
7518 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7519 sorted_by_abbrev.emplace_back
7520 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7521 sig_type->per_cu.section,
7522 sig_type->per_cu.sect_off));
7523
7524 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7525 sort_tu_by_abbrev_offset);
7526
7527 abbrev_offset = (sect_offset) ~(unsigned) 0;
7528
7529 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7530 {
7531 /* Switch to the next abbrev table if necessary. */
7532 if (abbrev_table == NULL
7533 || tu.abbrev_offset != abbrev_offset)
7534 {
7535 abbrev_offset = tu.abbrev_offset;
7536 abbrev_table =
7537 abbrev_table::read (dwarf2_per_objfile->objfile,
7538 &dwarf2_per_objfile->abbrev,
7539 abbrev_offset);
7540 ++tu_stats->nr_uniq_abbrev_tables;
7541 }
7542
7543 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7544 0, false);
7545 if (!reader.dummy_p)
7546 build_type_psymtabs_reader (&reader, reader.info_ptr,
7547 reader.comp_unit_die);
7548 }
7549 }
7550
7551 /* Print collected type unit statistics. */
7552
7553 static void
7554 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7555 {
7556 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7557
7558 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7559 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7560 dwarf2_per_objfile->all_type_units.size ());
7561 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7562 tu_stats->nr_uniq_abbrev_tables);
7563 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7564 tu_stats->nr_symtabs);
7565 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7566 tu_stats->nr_symtab_sharers);
7567 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7568 tu_stats->nr_stmt_less_type_units);
7569 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7570 tu_stats->nr_all_type_units_reallocs);
7571 }
7572
7573 /* Traversal function for build_type_psymtabs. */
7574
7575 static int
7576 build_type_psymtab_dependencies (void **slot, void *info)
7577 {
7578 struct dwarf2_per_objfile *dwarf2_per_objfile
7579 = (struct dwarf2_per_objfile *) info;
7580 struct objfile *objfile = dwarf2_per_objfile->objfile;
7581 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7582 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7583 dwarf2_psymtab *pst = per_cu->v.psymtab;
7584 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7585 int i;
7586
7587 gdb_assert (len > 0);
7588 gdb_assert (per_cu->type_unit_group_p ());
7589
7590 pst->number_of_dependencies = len;
7591 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7592 for (i = 0; i < len; ++i)
7593 {
7594 struct signatured_type *iter = tu_group->tus->at (i);
7595 gdb_assert (iter->per_cu.is_debug_types);
7596 pst->dependencies[i] = iter->per_cu.v.psymtab;
7597 iter->type_unit_group = tu_group;
7598 }
7599
7600 delete tu_group->tus;
7601 tu_group->tus = nullptr;
7602
7603 return 1;
7604 }
7605
7606 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7607 Build partial symbol tables for the .debug_types comp-units. */
7608
7609 static void
7610 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7611 {
7612 if (! create_all_type_units (dwarf2_per_objfile))
7613 return;
7614
7615 build_type_psymtabs_1 (dwarf2_per_objfile);
7616 }
7617
7618 /* Traversal function for process_skeletonless_type_unit.
7619 Read a TU in a DWO file and build partial symbols for it. */
7620
7621 static int
7622 process_skeletonless_type_unit (void **slot, void *info)
7623 {
7624 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7625 struct dwarf2_per_objfile *dwarf2_per_objfile
7626 = (struct dwarf2_per_objfile *) info;
7627 struct signatured_type find_entry, *entry;
7628
7629 /* If this TU doesn't exist in the global table, add it and read it in. */
7630
7631 if (dwarf2_per_objfile->signatured_types == NULL)
7632 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
7633
7634 find_entry.signature = dwo_unit->signature;
7635 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7636 &find_entry, INSERT);
7637 /* If we've already seen this type there's nothing to do. What's happening
7638 is we're doing our own version of comdat-folding here. */
7639 if (*slot != NULL)
7640 return 1;
7641
7642 /* This does the job that create_all_type_units would have done for
7643 this TU. */
7644 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7645 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7646 *slot = entry;
7647
7648 /* This does the job that build_type_psymtabs_1 would have done. */
7649 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7650 if (!reader.dummy_p)
7651 build_type_psymtabs_reader (&reader, reader.info_ptr,
7652 reader.comp_unit_die);
7653
7654 return 1;
7655 }
7656
7657 /* Traversal function for process_skeletonless_type_units. */
7658
7659 static int
7660 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7661 {
7662 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7663
7664 if (dwo_file->tus != NULL)
7665 htab_traverse_noresize (dwo_file->tus.get (),
7666 process_skeletonless_type_unit, info);
7667
7668 return 1;
7669 }
7670
7671 /* Scan all TUs of DWO files, verifying we've processed them.
7672 This is needed in case a TU was emitted without its skeleton.
7673 Note: This can't be done until we know what all the DWO files are. */
7674
7675 static void
7676 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7677 {
7678 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7679 if (get_dwp_file (dwarf2_per_objfile) == NULL
7680 && dwarf2_per_objfile->dwo_files != NULL)
7681 {
7682 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7683 process_dwo_file_for_skeletonless_type_units,
7684 dwarf2_per_objfile);
7685 }
7686 }
7687
7688 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7689
7690 static void
7691 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7692 {
7693 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7694 {
7695 dwarf2_psymtab *pst = per_cu->v.psymtab;
7696
7697 if (pst == NULL)
7698 continue;
7699
7700 for (int j = 0; j < pst->number_of_dependencies; ++j)
7701 {
7702 /* Set the 'user' field only if it is not already set. */
7703 if (pst->dependencies[j]->user == NULL)
7704 pst->dependencies[j]->user = pst;
7705 }
7706 }
7707 }
7708
7709 /* Build the partial symbol table by doing a quick pass through the
7710 .debug_info and .debug_abbrev sections. */
7711
7712 static void
7713 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7714 {
7715 struct objfile *objfile = dwarf2_per_objfile->objfile;
7716
7717 if (dwarf_read_debug)
7718 {
7719 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7720 objfile_name (objfile));
7721 }
7722
7723 scoped_restore restore_reading_psyms
7724 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7725 true);
7726
7727 dwarf2_per_objfile->info.read (objfile);
7728
7729 /* Any cached compilation units will be linked by the per-objfile
7730 read_in_chain. Make sure to free them when we're done. */
7731 free_cached_comp_units freer (dwarf2_per_objfile);
7732
7733 build_type_psymtabs (dwarf2_per_objfile);
7734
7735 create_all_comp_units (dwarf2_per_objfile);
7736
7737 /* Create a temporary address map on a temporary obstack. We later
7738 copy this to the final obstack. */
7739 auto_obstack temp_obstack;
7740
7741 scoped_restore save_psymtabs_addrmap
7742 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7743 addrmap_create_mutable (&temp_obstack));
7744
7745 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7746 process_psymtab_comp_unit (per_cu, false, language_minimal);
7747
7748 /* This has to wait until we read the CUs, we need the list of DWOs. */
7749 process_skeletonless_type_units (dwarf2_per_objfile);
7750
7751 /* Now that all TUs have been processed we can fill in the dependencies. */
7752 if (dwarf2_per_objfile->type_unit_groups != NULL)
7753 {
7754 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7755 build_type_psymtab_dependencies, dwarf2_per_objfile);
7756 }
7757
7758 if (dwarf_read_debug)
7759 print_tu_stats (dwarf2_per_objfile);
7760
7761 set_partial_user (dwarf2_per_objfile);
7762
7763 objfile->partial_symtabs->psymtabs_addrmap
7764 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7765 objfile->partial_symtabs->obstack ());
7766 /* At this point we want to keep the address map. */
7767 save_psymtabs_addrmap.release ();
7768
7769 if (dwarf_read_debug)
7770 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7771 objfile_name (objfile));
7772 }
7773
7774 /* Load the partial DIEs for a secondary CU into memory.
7775 This is also used when rereading a primary CU with load_all_dies. */
7776
7777 static void
7778 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7779 {
7780 cutu_reader reader (this_cu, NULL, 1, false);
7781
7782 if (!reader.dummy_p)
7783 {
7784 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7785 language_minimal);
7786
7787 /* Check if comp unit has_children.
7788 If so, read the rest of the partial symbols from this comp unit.
7789 If not, there's no more debug_info for this comp unit. */
7790 if (reader.comp_unit_die->has_children)
7791 load_partial_dies (&reader, reader.info_ptr, 0);
7792
7793 reader.keep ();
7794 }
7795 }
7796
7797 static void
7798 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7799 struct dwarf2_section_info *section,
7800 struct dwarf2_section_info *abbrev_section,
7801 unsigned int is_dwz)
7802 {
7803 const gdb_byte *info_ptr;
7804 struct objfile *objfile = dwarf2_per_objfile->objfile;
7805
7806 if (dwarf_read_debug)
7807 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7808 section->get_name (),
7809 section->get_file_name ());
7810
7811 section->read (objfile);
7812
7813 info_ptr = section->buffer;
7814
7815 while (info_ptr < section->buffer + section->size)
7816 {
7817 struct dwarf2_per_cu_data *this_cu;
7818
7819 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7820
7821 comp_unit_head cu_header;
7822 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7823 abbrev_section, info_ptr,
7824 rcuh_kind::COMPILE);
7825
7826 /* Save the compilation unit for later lookup. */
7827 if (cu_header.unit_type != DW_UT_type)
7828 {
7829 this_cu = XOBNEW (&objfile->objfile_obstack,
7830 struct dwarf2_per_cu_data);
7831 memset (this_cu, 0, sizeof (*this_cu));
7832 }
7833 else
7834 {
7835 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7836 struct signatured_type);
7837 memset (sig_type, 0, sizeof (*sig_type));
7838 sig_type->signature = cu_header.signature;
7839 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7840 this_cu = &sig_type->per_cu;
7841 }
7842 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7843 this_cu->sect_off = sect_off;
7844 this_cu->length = cu_header.length + cu_header.initial_length_size;
7845 this_cu->is_dwz = is_dwz;
7846 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7847 this_cu->section = section;
7848
7849 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7850
7851 info_ptr = info_ptr + this_cu->length;
7852 }
7853 }
7854
7855 /* Create a list of all compilation units in OBJFILE.
7856 This is only done for -readnow and building partial symtabs. */
7857
7858 static void
7859 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7860 {
7861 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7862 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7863 &dwarf2_per_objfile->abbrev, 0);
7864
7865 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7866 if (dwz != NULL)
7867 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7868 1);
7869 }
7870
7871 /* Process all loaded DIEs for compilation unit CU, starting at
7872 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7873 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7874 DW_AT_ranges). See the comments of add_partial_subprogram on how
7875 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7876
7877 static void
7878 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7879 CORE_ADDR *highpc, int set_addrmap,
7880 struct dwarf2_cu *cu)
7881 {
7882 struct partial_die_info *pdi;
7883
7884 /* Now, march along the PDI's, descending into ones which have
7885 interesting children but skipping the children of the other ones,
7886 until we reach the end of the compilation unit. */
7887
7888 pdi = first_die;
7889
7890 while (pdi != NULL)
7891 {
7892 pdi->fixup (cu);
7893
7894 /* Anonymous namespaces or modules have no name but have interesting
7895 children, so we need to look at them. Ditto for anonymous
7896 enums. */
7897
7898 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7899 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7900 || pdi->tag == DW_TAG_imported_unit
7901 || pdi->tag == DW_TAG_inlined_subroutine)
7902 {
7903 switch (pdi->tag)
7904 {
7905 case DW_TAG_subprogram:
7906 case DW_TAG_inlined_subroutine:
7907 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7908 break;
7909 case DW_TAG_constant:
7910 case DW_TAG_variable:
7911 case DW_TAG_typedef:
7912 case DW_TAG_union_type:
7913 if (!pdi->is_declaration)
7914 {
7915 add_partial_symbol (pdi, cu);
7916 }
7917 break;
7918 case DW_TAG_class_type:
7919 case DW_TAG_interface_type:
7920 case DW_TAG_structure_type:
7921 if (!pdi->is_declaration)
7922 {
7923 add_partial_symbol (pdi, cu);
7924 }
7925 if ((cu->language == language_rust
7926 || cu->language == language_cplus) && pdi->has_children)
7927 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7928 set_addrmap, cu);
7929 break;
7930 case DW_TAG_enumeration_type:
7931 if (!pdi->is_declaration)
7932 add_partial_enumeration (pdi, cu);
7933 break;
7934 case DW_TAG_base_type:
7935 case DW_TAG_subrange_type:
7936 /* File scope base type definitions are added to the partial
7937 symbol table. */
7938 add_partial_symbol (pdi, cu);
7939 break;
7940 case DW_TAG_namespace:
7941 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7942 break;
7943 case DW_TAG_module:
7944 if (!pdi->is_declaration)
7945 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7946 break;
7947 case DW_TAG_imported_unit:
7948 {
7949 struct dwarf2_per_cu_data *per_cu;
7950
7951 /* For now we don't handle imported units in type units. */
7952 if (cu->per_cu->is_debug_types)
7953 {
7954 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7955 " supported in type units [in module %s]"),
7956 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7957 }
7958
7959 per_cu = dwarf2_find_containing_comp_unit
7960 (pdi->d.sect_off, pdi->is_dwz,
7961 cu->per_cu->dwarf2_per_objfile);
7962
7963 /* Go read the partial unit, if needed. */
7964 if (per_cu->v.psymtab == NULL)
7965 process_psymtab_comp_unit (per_cu, true, cu->language);
7966
7967 cu->per_cu->imported_symtabs_push (per_cu);
7968 }
7969 break;
7970 case DW_TAG_imported_declaration:
7971 add_partial_symbol (pdi, cu);
7972 break;
7973 default:
7974 break;
7975 }
7976 }
7977
7978 /* If the die has a sibling, skip to the sibling. */
7979
7980 pdi = pdi->die_sibling;
7981 }
7982 }
7983
7984 /* Functions used to compute the fully scoped name of a partial DIE.
7985
7986 Normally, this is simple. For C++, the parent DIE's fully scoped
7987 name is concatenated with "::" and the partial DIE's name.
7988 Enumerators are an exception; they use the scope of their parent
7989 enumeration type, i.e. the name of the enumeration type is not
7990 prepended to the enumerator.
7991
7992 There are two complexities. One is DW_AT_specification; in this
7993 case "parent" means the parent of the target of the specification,
7994 instead of the direct parent of the DIE. The other is compilers
7995 which do not emit DW_TAG_namespace; in this case we try to guess
7996 the fully qualified name of structure types from their members'
7997 linkage names. This must be done using the DIE's children rather
7998 than the children of any DW_AT_specification target. We only need
7999 to do this for structures at the top level, i.e. if the target of
8000 any DW_AT_specification (if any; otherwise the DIE itself) does not
8001 have a parent. */
8002
8003 /* Compute the scope prefix associated with PDI's parent, in
8004 compilation unit CU. The result will be allocated on CU's
8005 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8006 field. NULL is returned if no prefix is necessary. */
8007 static const char *
8008 partial_die_parent_scope (struct partial_die_info *pdi,
8009 struct dwarf2_cu *cu)
8010 {
8011 const char *grandparent_scope;
8012 struct partial_die_info *parent, *real_pdi;
8013
8014 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8015 then this means the parent of the specification DIE. */
8016
8017 real_pdi = pdi;
8018 while (real_pdi->has_specification)
8019 {
8020 auto res = find_partial_die (real_pdi->spec_offset,
8021 real_pdi->spec_is_dwz, cu);
8022 real_pdi = res.pdi;
8023 cu = res.cu;
8024 }
8025
8026 parent = real_pdi->die_parent;
8027 if (parent == NULL)
8028 return NULL;
8029
8030 if (parent->scope_set)
8031 return parent->scope;
8032
8033 parent->fixup (cu);
8034
8035 grandparent_scope = partial_die_parent_scope (parent, cu);
8036
8037 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8038 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8039 Work around this problem here. */
8040 if (cu->language == language_cplus
8041 && parent->tag == DW_TAG_namespace
8042 && strcmp (parent->name, "::") == 0
8043 && grandparent_scope == NULL)
8044 {
8045 parent->scope = NULL;
8046 parent->scope_set = 1;
8047 return NULL;
8048 }
8049
8050 /* Nested subroutines in Fortran get a prefix. */
8051 if (pdi->tag == DW_TAG_enumerator)
8052 /* Enumerators should not get the name of the enumeration as a prefix. */
8053 parent->scope = grandparent_scope;
8054 else if (parent->tag == DW_TAG_namespace
8055 || parent->tag == DW_TAG_module
8056 || parent->tag == DW_TAG_structure_type
8057 || parent->tag == DW_TAG_class_type
8058 || parent->tag == DW_TAG_interface_type
8059 || parent->tag == DW_TAG_union_type
8060 || parent->tag == DW_TAG_enumeration_type
8061 || (cu->language == language_fortran
8062 && parent->tag == DW_TAG_subprogram
8063 && pdi->tag == DW_TAG_subprogram))
8064 {
8065 if (grandparent_scope == NULL)
8066 parent->scope = parent->name;
8067 else
8068 parent->scope = typename_concat (&cu->comp_unit_obstack,
8069 grandparent_scope,
8070 parent->name, 0, cu);
8071 }
8072 else
8073 {
8074 /* FIXME drow/2004-04-01: What should we be doing with
8075 function-local names? For partial symbols, we should probably be
8076 ignoring them. */
8077 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8078 dwarf_tag_name (parent->tag),
8079 sect_offset_str (pdi->sect_off));
8080 parent->scope = grandparent_scope;
8081 }
8082
8083 parent->scope_set = 1;
8084 return parent->scope;
8085 }
8086
8087 /* Return the fully scoped name associated with PDI, from compilation unit
8088 CU. The result will be allocated with malloc. */
8089
8090 static gdb::unique_xmalloc_ptr<char>
8091 partial_die_full_name (struct partial_die_info *pdi,
8092 struct dwarf2_cu *cu)
8093 {
8094 const char *parent_scope;
8095
8096 /* If this is a template instantiation, we can not work out the
8097 template arguments from partial DIEs. So, unfortunately, we have
8098 to go through the full DIEs. At least any work we do building
8099 types here will be reused if full symbols are loaded later. */
8100 if (pdi->has_template_arguments)
8101 {
8102 pdi->fixup (cu);
8103
8104 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8105 {
8106 struct die_info *die;
8107 struct attribute attr;
8108 struct dwarf2_cu *ref_cu = cu;
8109
8110 /* DW_FORM_ref_addr is using section offset. */
8111 attr.name = (enum dwarf_attribute) 0;
8112 attr.form = DW_FORM_ref_addr;
8113 attr.u.unsnd = to_underlying (pdi->sect_off);
8114 die = follow_die_ref (NULL, &attr, &ref_cu);
8115
8116 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8117 }
8118 }
8119
8120 parent_scope = partial_die_parent_scope (pdi, cu);
8121 if (parent_scope == NULL)
8122 return NULL;
8123 else
8124 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8125 pdi->name, 0, cu));
8126 }
8127
8128 static void
8129 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8130 {
8131 struct dwarf2_per_objfile *dwarf2_per_objfile
8132 = cu->per_cu->dwarf2_per_objfile;
8133 struct objfile *objfile = dwarf2_per_objfile->objfile;
8134 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8135 CORE_ADDR addr = 0;
8136 const char *actual_name = NULL;
8137 CORE_ADDR baseaddr;
8138
8139 baseaddr = objfile->text_section_offset ();
8140
8141 gdb::unique_xmalloc_ptr<char> built_actual_name
8142 = partial_die_full_name (pdi, cu);
8143 if (built_actual_name != NULL)
8144 actual_name = built_actual_name.get ();
8145
8146 if (actual_name == NULL)
8147 actual_name = pdi->name;
8148
8149 switch (pdi->tag)
8150 {
8151 case DW_TAG_inlined_subroutine:
8152 case DW_TAG_subprogram:
8153 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8154 - baseaddr);
8155 if (pdi->is_external
8156 || cu->language == language_ada
8157 || (cu->language == language_fortran
8158 && pdi->die_parent != NULL
8159 && pdi->die_parent->tag == DW_TAG_subprogram))
8160 {
8161 /* Normally, only "external" DIEs are part of the global scope.
8162 But in Ada and Fortran, we want to be able to access nested
8163 procedures globally. So all Ada and Fortran subprograms are
8164 stored in the global scope. */
8165 add_psymbol_to_list (actual_name,
8166 built_actual_name != NULL,
8167 VAR_DOMAIN, LOC_BLOCK,
8168 SECT_OFF_TEXT (objfile),
8169 psymbol_placement::GLOBAL,
8170 addr,
8171 cu->language, objfile);
8172 }
8173 else
8174 {
8175 add_psymbol_to_list (actual_name,
8176 built_actual_name != NULL,
8177 VAR_DOMAIN, LOC_BLOCK,
8178 SECT_OFF_TEXT (objfile),
8179 psymbol_placement::STATIC,
8180 addr, cu->language, objfile);
8181 }
8182
8183 if (pdi->main_subprogram && actual_name != NULL)
8184 set_objfile_main_name (objfile, actual_name, cu->language);
8185 break;
8186 case DW_TAG_constant:
8187 add_psymbol_to_list (actual_name,
8188 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8189 -1, (pdi->is_external
8190 ? psymbol_placement::GLOBAL
8191 : psymbol_placement::STATIC),
8192 0, cu->language, objfile);
8193 break;
8194 case DW_TAG_variable:
8195 if (pdi->d.locdesc)
8196 addr = decode_locdesc (pdi->d.locdesc, cu);
8197
8198 if (pdi->d.locdesc
8199 && addr == 0
8200 && !dwarf2_per_objfile->has_section_at_zero)
8201 {
8202 /* A global or static variable may also have been stripped
8203 out by the linker if unused, in which case its address
8204 will be nullified; do not add such variables into partial
8205 symbol table then. */
8206 }
8207 else if (pdi->is_external)
8208 {
8209 /* Global Variable.
8210 Don't enter into the minimal symbol tables as there is
8211 a minimal symbol table entry from the ELF symbols already.
8212 Enter into partial symbol table if it has a location
8213 descriptor or a type.
8214 If the location descriptor is missing, new_symbol will create
8215 a LOC_UNRESOLVED symbol, the address of the variable will then
8216 be determined from the minimal symbol table whenever the variable
8217 is referenced.
8218 The address for the partial symbol table entry is not
8219 used by GDB, but it comes in handy for debugging partial symbol
8220 table building. */
8221
8222 if (pdi->d.locdesc || pdi->has_type)
8223 add_psymbol_to_list (actual_name,
8224 built_actual_name != NULL,
8225 VAR_DOMAIN, LOC_STATIC,
8226 SECT_OFF_TEXT (objfile),
8227 psymbol_placement::GLOBAL,
8228 addr, cu->language, objfile);
8229 }
8230 else
8231 {
8232 int has_loc = pdi->d.locdesc != NULL;
8233
8234 /* Static Variable. Skip symbols whose value we cannot know (those
8235 without location descriptors or constant values). */
8236 if (!has_loc && !pdi->has_const_value)
8237 return;
8238
8239 add_psymbol_to_list (actual_name,
8240 built_actual_name != NULL,
8241 VAR_DOMAIN, LOC_STATIC,
8242 SECT_OFF_TEXT (objfile),
8243 psymbol_placement::STATIC,
8244 has_loc ? addr : 0,
8245 cu->language, objfile);
8246 }
8247 break;
8248 case DW_TAG_typedef:
8249 case DW_TAG_base_type:
8250 case DW_TAG_subrange_type:
8251 add_psymbol_to_list (actual_name,
8252 built_actual_name != NULL,
8253 VAR_DOMAIN, LOC_TYPEDEF, -1,
8254 psymbol_placement::STATIC,
8255 0, cu->language, objfile);
8256 break;
8257 case DW_TAG_imported_declaration:
8258 case DW_TAG_namespace:
8259 add_psymbol_to_list (actual_name,
8260 built_actual_name != NULL,
8261 VAR_DOMAIN, LOC_TYPEDEF, -1,
8262 psymbol_placement::GLOBAL,
8263 0, cu->language, objfile);
8264 break;
8265 case DW_TAG_module:
8266 /* With Fortran 77 there might be a "BLOCK DATA" module
8267 available without any name. If so, we skip the module as it
8268 doesn't bring any value. */
8269 if (actual_name != nullptr)
8270 add_psymbol_to_list (actual_name,
8271 built_actual_name != NULL,
8272 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8273 psymbol_placement::GLOBAL,
8274 0, cu->language, objfile);
8275 break;
8276 case DW_TAG_class_type:
8277 case DW_TAG_interface_type:
8278 case DW_TAG_structure_type:
8279 case DW_TAG_union_type:
8280 case DW_TAG_enumeration_type:
8281 /* Skip external references. The DWARF standard says in the section
8282 about "Structure, Union, and Class Type Entries": "An incomplete
8283 structure, union or class type is represented by a structure,
8284 union or class entry that does not have a byte size attribute
8285 and that has a DW_AT_declaration attribute." */
8286 if (!pdi->has_byte_size && pdi->is_declaration)
8287 return;
8288
8289 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8290 static vs. global. */
8291 add_psymbol_to_list (actual_name,
8292 built_actual_name != NULL,
8293 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8294 cu->language == language_cplus
8295 ? psymbol_placement::GLOBAL
8296 : psymbol_placement::STATIC,
8297 0, cu->language, objfile);
8298
8299 break;
8300 case DW_TAG_enumerator:
8301 add_psymbol_to_list (actual_name,
8302 built_actual_name != NULL,
8303 VAR_DOMAIN, LOC_CONST, -1,
8304 cu->language == language_cplus
8305 ? psymbol_placement::GLOBAL
8306 : psymbol_placement::STATIC,
8307 0, cu->language, objfile);
8308 break;
8309 default:
8310 break;
8311 }
8312 }
8313
8314 /* Read a partial die corresponding to a namespace; also, add a symbol
8315 corresponding to that namespace to the symbol table. NAMESPACE is
8316 the name of the enclosing namespace. */
8317
8318 static void
8319 add_partial_namespace (struct partial_die_info *pdi,
8320 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8321 int set_addrmap, struct dwarf2_cu *cu)
8322 {
8323 /* Add a symbol for the namespace. */
8324
8325 add_partial_symbol (pdi, cu);
8326
8327 /* Now scan partial symbols in that namespace. */
8328
8329 if (pdi->has_children)
8330 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8331 }
8332
8333 /* Read a partial die corresponding to a Fortran module. */
8334
8335 static void
8336 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8337 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8338 {
8339 /* Add a symbol for the namespace. */
8340
8341 add_partial_symbol (pdi, cu);
8342
8343 /* Now scan partial symbols in that module. */
8344
8345 if (pdi->has_children)
8346 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8347 }
8348
8349 /* Read a partial die corresponding to a subprogram or an inlined
8350 subprogram and create a partial symbol for that subprogram.
8351 When the CU language allows it, this routine also defines a partial
8352 symbol for each nested subprogram that this subprogram contains.
8353 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8354 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8355
8356 PDI may also be a lexical block, in which case we simply search
8357 recursively for subprograms defined inside that lexical block.
8358 Again, this is only performed when the CU language allows this
8359 type of definitions. */
8360
8361 static void
8362 add_partial_subprogram (struct partial_die_info *pdi,
8363 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8364 int set_addrmap, struct dwarf2_cu *cu)
8365 {
8366 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8367 {
8368 if (pdi->has_pc_info)
8369 {
8370 if (pdi->lowpc < *lowpc)
8371 *lowpc = pdi->lowpc;
8372 if (pdi->highpc > *highpc)
8373 *highpc = pdi->highpc;
8374 if (set_addrmap)
8375 {
8376 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8377 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8378 CORE_ADDR baseaddr;
8379 CORE_ADDR this_highpc;
8380 CORE_ADDR this_lowpc;
8381
8382 baseaddr = objfile->text_section_offset ();
8383 this_lowpc
8384 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8385 pdi->lowpc + baseaddr)
8386 - baseaddr);
8387 this_highpc
8388 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8389 pdi->highpc + baseaddr)
8390 - baseaddr);
8391 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8392 this_lowpc, this_highpc - 1,
8393 cu->per_cu->v.psymtab);
8394 }
8395 }
8396
8397 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8398 {
8399 if (!pdi->is_declaration)
8400 /* Ignore subprogram DIEs that do not have a name, they are
8401 illegal. Do not emit a complaint at this point, we will
8402 do so when we convert this psymtab into a symtab. */
8403 if (pdi->name)
8404 add_partial_symbol (pdi, cu);
8405 }
8406 }
8407
8408 if (! pdi->has_children)
8409 return;
8410
8411 if (cu->language == language_ada || cu->language == language_fortran)
8412 {
8413 pdi = pdi->die_child;
8414 while (pdi != NULL)
8415 {
8416 pdi->fixup (cu);
8417 if (pdi->tag == DW_TAG_subprogram
8418 || pdi->tag == DW_TAG_inlined_subroutine
8419 || pdi->tag == DW_TAG_lexical_block)
8420 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8421 pdi = pdi->die_sibling;
8422 }
8423 }
8424 }
8425
8426 /* Read a partial die corresponding to an enumeration type. */
8427
8428 static void
8429 add_partial_enumeration (struct partial_die_info *enum_pdi,
8430 struct dwarf2_cu *cu)
8431 {
8432 struct partial_die_info *pdi;
8433
8434 if (enum_pdi->name != NULL)
8435 add_partial_symbol (enum_pdi, cu);
8436
8437 pdi = enum_pdi->die_child;
8438 while (pdi)
8439 {
8440 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8441 complaint (_("malformed enumerator DIE ignored"));
8442 else
8443 add_partial_symbol (pdi, cu);
8444 pdi = pdi->die_sibling;
8445 }
8446 }
8447
8448 /* Return the initial uleb128 in the die at INFO_PTR. */
8449
8450 static unsigned int
8451 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8452 {
8453 unsigned int bytes_read;
8454
8455 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8456 }
8457
8458 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8459 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8460
8461 Return the corresponding abbrev, or NULL if the number is zero (indicating
8462 an empty DIE). In either case *BYTES_READ will be set to the length of
8463 the initial number. */
8464
8465 static struct abbrev_info *
8466 peek_die_abbrev (const die_reader_specs &reader,
8467 const gdb_byte *info_ptr, unsigned int *bytes_read)
8468 {
8469 dwarf2_cu *cu = reader.cu;
8470 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8471 unsigned int abbrev_number
8472 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8473
8474 if (abbrev_number == 0)
8475 return NULL;
8476
8477 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8478 if (!abbrev)
8479 {
8480 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8481 " at offset %s [in module %s]"),
8482 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8483 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8484 }
8485
8486 return abbrev;
8487 }
8488
8489 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8490 Returns a pointer to the end of a series of DIEs, terminated by an empty
8491 DIE. Any children of the skipped DIEs will also be skipped. */
8492
8493 static const gdb_byte *
8494 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8495 {
8496 while (1)
8497 {
8498 unsigned int bytes_read;
8499 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8500
8501 if (abbrev == NULL)
8502 return info_ptr + bytes_read;
8503 else
8504 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8505 }
8506 }
8507
8508 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8509 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8510 abbrev corresponding to that skipped uleb128 should be passed in
8511 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8512 children. */
8513
8514 static const gdb_byte *
8515 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8516 struct abbrev_info *abbrev)
8517 {
8518 unsigned int bytes_read;
8519 struct attribute attr;
8520 bfd *abfd = reader->abfd;
8521 struct dwarf2_cu *cu = reader->cu;
8522 const gdb_byte *buffer = reader->buffer;
8523 const gdb_byte *buffer_end = reader->buffer_end;
8524 unsigned int form, i;
8525
8526 for (i = 0; i < abbrev->num_attrs; i++)
8527 {
8528 /* The only abbrev we care about is DW_AT_sibling. */
8529 if (abbrev->attrs[i].name == DW_AT_sibling)
8530 {
8531 bool ignored;
8532 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8533 &ignored);
8534 if (attr.form == DW_FORM_ref_addr)
8535 complaint (_("ignoring absolute DW_AT_sibling"));
8536 else
8537 {
8538 sect_offset off = attr.get_ref_die_offset ();
8539 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8540
8541 if (sibling_ptr < info_ptr)
8542 complaint (_("DW_AT_sibling points backwards"));
8543 else if (sibling_ptr > reader->buffer_end)
8544 reader->die_section->overflow_complaint ();
8545 else
8546 return sibling_ptr;
8547 }
8548 }
8549
8550 /* If it isn't DW_AT_sibling, skip this attribute. */
8551 form = abbrev->attrs[i].form;
8552 skip_attribute:
8553 switch (form)
8554 {
8555 case DW_FORM_ref_addr:
8556 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8557 and later it is offset sized. */
8558 if (cu->header.version == 2)
8559 info_ptr += cu->header.addr_size;
8560 else
8561 info_ptr += cu->header.offset_size;
8562 break;
8563 case DW_FORM_GNU_ref_alt:
8564 info_ptr += cu->header.offset_size;
8565 break;
8566 case DW_FORM_addr:
8567 info_ptr += cu->header.addr_size;
8568 break;
8569 case DW_FORM_data1:
8570 case DW_FORM_ref1:
8571 case DW_FORM_flag:
8572 case DW_FORM_strx1:
8573 info_ptr += 1;
8574 break;
8575 case DW_FORM_flag_present:
8576 case DW_FORM_implicit_const:
8577 break;
8578 case DW_FORM_data2:
8579 case DW_FORM_ref2:
8580 case DW_FORM_strx2:
8581 info_ptr += 2;
8582 break;
8583 case DW_FORM_strx3:
8584 info_ptr += 3;
8585 break;
8586 case DW_FORM_data4:
8587 case DW_FORM_ref4:
8588 case DW_FORM_strx4:
8589 info_ptr += 4;
8590 break;
8591 case DW_FORM_data8:
8592 case DW_FORM_ref8:
8593 case DW_FORM_ref_sig8:
8594 info_ptr += 8;
8595 break;
8596 case DW_FORM_data16:
8597 info_ptr += 16;
8598 break;
8599 case DW_FORM_string:
8600 read_direct_string (abfd, info_ptr, &bytes_read);
8601 info_ptr += bytes_read;
8602 break;
8603 case DW_FORM_sec_offset:
8604 case DW_FORM_strp:
8605 case DW_FORM_GNU_strp_alt:
8606 info_ptr += cu->header.offset_size;
8607 break;
8608 case DW_FORM_exprloc:
8609 case DW_FORM_block:
8610 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8611 info_ptr += bytes_read;
8612 break;
8613 case DW_FORM_block1:
8614 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8615 break;
8616 case DW_FORM_block2:
8617 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8618 break;
8619 case DW_FORM_block4:
8620 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8621 break;
8622 case DW_FORM_addrx:
8623 case DW_FORM_strx:
8624 case DW_FORM_sdata:
8625 case DW_FORM_udata:
8626 case DW_FORM_ref_udata:
8627 case DW_FORM_GNU_addr_index:
8628 case DW_FORM_GNU_str_index:
8629 case DW_FORM_rnglistx:
8630 case DW_FORM_loclistx:
8631 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8632 break;
8633 case DW_FORM_indirect:
8634 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8635 info_ptr += bytes_read;
8636 /* We need to continue parsing from here, so just go back to
8637 the top. */
8638 goto skip_attribute;
8639
8640 default:
8641 error (_("Dwarf Error: Cannot handle %s "
8642 "in DWARF reader [in module %s]"),
8643 dwarf_form_name (form),
8644 bfd_get_filename (abfd));
8645 }
8646 }
8647
8648 if (abbrev->has_children)
8649 return skip_children (reader, info_ptr);
8650 else
8651 return info_ptr;
8652 }
8653
8654 /* Locate ORIG_PDI's sibling.
8655 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8656
8657 static const gdb_byte *
8658 locate_pdi_sibling (const struct die_reader_specs *reader,
8659 struct partial_die_info *orig_pdi,
8660 const gdb_byte *info_ptr)
8661 {
8662 /* Do we know the sibling already? */
8663
8664 if (orig_pdi->sibling)
8665 return orig_pdi->sibling;
8666
8667 /* Are there any children to deal with? */
8668
8669 if (!orig_pdi->has_children)
8670 return info_ptr;
8671
8672 /* Skip the children the long way. */
8673
8674 return skip_children (reader, info_ptr);
8675 }
8676
8677 /* Expand this partial symbol table into a full symbol table. SELF is
8678 not NULL. */
8679
8680 void
8681 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8682 {
8683 struct dwarf2_per_objfile *dwarf2_per_objfile
8684 = get_dwarf2_per_objfile (objfile);
8685
8686 gdb_assert (!readin);
8687 /* If this psymtab is constructed from a debug-only objfile, the
8688 has_section_at_zero flag will not necessarily be correct. We
8689 can get the correct value for this flag by looking at the data
8690 associated with the (presumably stripped) associated objfile. */
8691 if (objfile->separate_debug_objfile_backlink)
8692 {
8693 struct dwarf2_per_objfile *dpo_backlink
8694 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8695
8696 dwarf2_per_objfile->has_section_at_zero
8697 = dpo_backlink->has_section_at_zero;
8698 }
8699
8700 expand_psymtab (objfile);
8701
8702 process_cu_includes (dwarf2_per_objfile);
8703 }
8704 \f
8705 /* Reading in full CUs. */
8706
8707 /* Add PER_CU to the queue. */
8708
8709 static void
8710 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8711 enum language pretend_language)
8712 {
8713 per_cu->queued = 1;
8714 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8715 }
8716
8717 /* If PER_CU is not yet queued, add it to the queue.
8718 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8719 dependency.
8720 The result is non-zero if PER_CU was queued, otherwise the result is zero
8721 meaning either PER_CU is already queued or it is already loaded.
8722
8723 N.B. There is an invariant here that if a CU is queued then it is loaded.
8724 The caller is required to load PER_CU if we return non-zero. */
8725
8726 static int
8727 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8728 struct dwarf2_per_cu_data *per_cu,
8729 enum language pretend_language)
8730 {
8731 /* We may arrive here during partial symbol reading, if we need full
8732 DIEs to process an unusual case (e.g. template arguments). Do
8733 not queue PER_CU, just tell our caller to load its DIEs. */
8734 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8735 {
8736 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8737 return 1;
8738 return 0;
8739 }
8740
8741 /* Mark the dependence relation so that we don't flush PER_CU
8742 too early. */
8743 if (dependent_cu != NULL)
8744 dwarf2_add_dependence (dependent_cu, per_cu);
8745
8746 /* If it's already on the queue, we have nothing to do. */
8747 if (per_cu->queued)
8748 return 0;
8749
8750 /* If the compilation unit is already loaded, just mark it as
8751 used. */
8752 if (per_cu->cu != NULL)
8753 {
8754 per_cu->cu->last_used = 0;
8755 return 0;
8756 }
8757
8758 /* Add it to the queue. */
8759 queue_comp_unit (per_cu, pretend_language);
8760
8761 return 1;
8762 }
8763
8764 /* Process the queue. */
8765
8766 static void
8767 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8768 {
8769 if (dwarf_read_debug)
8770 {
8771 fprintf_unfiltered (gdb_stdlog,
8772 "Expanding one or more symtabs of objfile %s ...\n",
8773 objfile_name (dwarf2_per_objfile->objfile));
8774 }
8775
8776 /* The queue starts out with one item, but following a DIE reference
8777 may load a new CU, adding it to the end of the queue. */
8778 while (!dwarf2_per_objfile->queue.empty ())
8779 {
8780 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8781
8782 if ((dwarf2_per_objfile->using_index
8783 ? !item.per_cu->v.quick->compunit_symtab
8784 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8785 /* Skip dummy CUs. */
8786 && item.per_cu->cu != NULL)
8787 {
8788 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8789 unsigned int debug_print_threshold;
8790 char buf[100];
8791
8792 if (per_cu->is_debug_types)
8793 {
8794 struct signatured_type *sig_type =
8795 (struct signatured_type *) per_cu;
8796
8797 sprintf (buf, "TU %s at offset %s",
8798 hex_string (sig_type->signature),
8799 sect_offset_str (per_cu->sect_off));
8800 /* There can be 100s of TUs.
8801 Only print them in verbose mode. */
8802 debug_print_threshold = 2;
8803 }
8804 else
8805 {
8806 sprintf (buf, "CU at offset %s",
8807 sect_offset_str (per_cu->sect_off));
8808 debug_print_threshold = 1;
8809 }
8810
8811 if (dwarf_read_debug >= debug_print_threshold)
8812 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8813
8814 if (per_cu->is_debug_types)
8815 process_full_type_unit (per_cu, item.pretend_language);
8816 else
8817 process_full_comp_unit (per_cu, item.pretend_language);
8818
8819 if (dwarf_read_debug >= debug_print_threshold)
8820 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8821 }
8822
8823 item.per_cu->queued = 0;
8824 dwarf2_per_objfile->queue.pop ();
8825 }
8826
8827 if (dwarf_read_debug)
8828 {
8829 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8830 objfile_name (dwarf2_per_objfile->objfile));
8831 }
8832 }
8833
8834 /* Read in full symbols for PST, and anything it depends on. */
8835
8836 void
8837 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8838 {
8839 if (readin)
8840 return;
8841
8842 expand_dependencies (objfile);
8843
8844 dw2_do_instantiate_symtab (per_cu_data, false);
8845 gdb_assert (get_compunit_symtab () != nullptr);
8846 }
8847
8848 /* Trivial hash function for die_info: the hash value of a DIE
8849 is its offset in .debug_info for this objfile. */
8850
8851 static hashval_t
8852 die_hash (const void *item)
8853 {
8854 const struct die_info *die = (const struct die_info *) item;
8855
8856 return to_underlying (die->sect_off);
8857 }
8858
8859 /* Trivial comparison function for die_info structures: two DIEs
8860 are equal if they have the same offset. */
8861
8862 static int
8863 die_eq (const void *item_lhs, const void *item_rhs)
8864 {
8865 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8866 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8867
8868 return die_lhs->sect_off == die_rhs->sect_off;
8869 }
8870
8871 /* Load the DIEs associated with PER_CU into memory. */
8872
8873 static void
8874 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8875 bool skip_partial,
8876 enum language pretend_language)
8877 {
8878 gdb_assert (! this_cu->is_debug_types);
8879
8880 cutu_reader reader (this_cu, NULL, 1, skip_partial);
8881 if (reader.dummy_p)
8882 return;
8883
8884 struct dwarf2_cu *cu = reader.cu;
8885 const gdb_byte *info_ptr = reader.info_ptr;
8886
8887 gdb_assert (cu->die_hash == NULL);
8888 cu->die_hash =
8889 htab_create_alloc_ex (cu->header.length / 12,
8890 die_hash,
8891 die_eq,
8892 NULL,
8893 &cu->comp_unit_obstack,
8894 hashtab_obstack_allocate,
8895 dummy_obstack_deallocate);
8896
8897 if (reader.comp_unit_die->has_children)
8898 reader.comp_unit_die->child
8899 = read_die_and_siblings (&reader, reader.info_ptr,
8900 &info_ptr, reader.comp_unit_die);
8901 cu->dies = reader.comp_unit_die;
8902 /* comp_unit_die is not stored in die_hash, no need. */
8903
8904 /* We try not to read any attributes in this function, because not
8905 all CUs needed for references have been loaded yet, and symbol
8906 table processing isn't initialized. But we have to set the CU language,
8907 or we won't be able to build types correctly.
8908 Similarly, if we do not read the producer, we can not apply
8909 producer-specific interpretation. */
8910 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8911
8912 reader.keep ();
8913 }
8914
8915 /* Add a DIE to the delayed physname list. */
8916
8917 static void
8918 add_to_method_list (struct type *type, int fnfield_index, int index,
8919 const char *name, struct die_info *die,
8920 struct dwarf2_cu *cu)
8921 {
8922 struct delayed_method_info mi;
8923 mi.type = type;
8924 mi.fnfield_index = fnfield_index;
8925 mi.index = index;
8926 mi.name = name;
8927 mi.die = die;
8928 cu->method_list.push_back (mi);
8929 }
8930
8931 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8932 "const" / "volatile". If so, decrements LEN by the length of the
8933 modifier and return true. Otherwise return false. */
8934
8935 template<size_t N>
8936 static bool
8937 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8938 {
8939 size_t mod_len = sizeof (mod) - 1;
8940 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8941 {
8942 len -= mod_len;
8943 return true;
8944 }
8945 return false;
8946 }
8947
8948 /* Compute the physnames of any methods on the CU's method list.
8949
8950 The computation of method physnames is delayed in order to avoid the
8951 (bad) condition that one of the method's formal parameters is of an as yet
8952 incomplete type. */
8953
8954 static void
8955 compute_delayed_physnames (struct dwarf2_cu *cu)
8956 {
8957 /* Only C++ delays computing physnames. */
8958 if (cu->method_list.empty ())
8959 return;
8960 gdb_assert (cu->language == language_cplus);
8961
8962 for (const delayed_method_info &mi : cu->method_list)
8963 {
8964 const char *physname;
8965 struct fn_fieldlist *fn_flp
8966 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8967 physname = dwarf2_physname (mi.name, mi.die, cu);
8968 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8969 = physname ? physname : "";
8970
8971 /* Since there's no tag to indicate whether a method is a
8972 const/volatile overload, extract that information out of the
8973 demangled name. */
8974 if (physname != NULL)
8975 {
8976 size_t len = strlen (physname);
8977
8978 while (1)
8979 {
8980 if (physname[len] == ')') /* shortcut */
8981 break;
8982 else if (check_modifier (physname, len, " const"))
8983 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8984 else if (check_modifier (physname, len, " volatile"))
8985 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8986 else
8987 break;
8988 }
8989 }
8990 }
8991
8992 /* The list is no longer needed. */
8993 cu->method_list.clear ();
8994 }
8995
8996 /* Go objects should be embedded in a DW_TAG_module DIE,
8997 and it's not clear if/how imported objects will appear.
8998 To keep Go support simple until that's worked out,
8999 go back through what we've read and create something usable.
9000 We could do this while processing each DIE, and feels kinda cleaner,
9001 but that way is more invasive.
9002 This is to, for example, allow the user to type "p var" or "b main"
9003 without having to specify the package name, and allow lookups
9004 of module.object to work in contexts that use the expression
9005 parser. */
9006
9007 static void
9008 fixup_go_packaging (struct dwarf2_cu *cu)
9009 {
9010 gdb::unique_xmalloc_ptr<char> package_name;
9011 struct pending *list;
9012 int i;
9013
9014 for (list = *cu->get_builder ()->get_global_symbols ();
9015 list != NULL;
9016 list = list->next)
9017 {
9018 for (i = 0; i < list->nsyms; ++i)
9019 {
9020 struct symbol *sym = list->symbol[i];
9021
9022 if (sym->language () == language_go
9023 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9024 {
9025 gdb::unique_xmalloc_ptr<char> this_package_name
9026 (go_symbol_package_name (sym));
9027
9028 if (this_package_name == NULL)
9029 continue;
9030 if (package_name == NULL)
9031 package_name = std::move (this_package_name);
9032 else
9033 {
9034 struct objfile *objfile
9035 = cu->per_cu->dwarf2_per_objfile->objfile;
9036 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9037 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9038 (symbol_symtab (sym) != NULL
9039 ? symtab_to_filename_for_display
9040 (symbol_symtab (sym))
9041 : objfile_name (objfile)),
9042 this_package_name.get (), package_name.get ());
9043 }
9044 }
9045 }
9046 }
9047
9048 if (package_name != NULL)
9049 {
9050 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9051 const char *saved_package_name = objfile->intern (package_name.get ());
9052 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9053 saved_package_name);
9054 struct symbol *sym;
9055
9056 sym = allocate_symbol (objfile);
9057 sym->set_language (language_go, &objfile->objfile_obstack);
9058 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9059 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9060 e.g., "main" finds the "main" module and not C's main(). */
9061 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9062 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9063 SYMBOL_TYPE (sym) = type;
9064
9065 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9066 }
9067 }
9068
9069 /* Allocate a fully-qualified name consisting of the two parts on the
9070 obstack. */
9071
9072 static const char *
9073 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9074 {
9075 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9076 }
9077
9078 /* A helper that allocates a struct discriminant_info to attach to a
9079 union type. */
9080
9081 static struct discriminant_info *
9082 alloc_discriminant_info (struct type *type, int discriminant_index,
9083 int default_index)
9084 {
9085 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9086 gdb_assert (discriminant_index == -1
9087 || (discriminant_index >= 0
9088 && discriminant_index < TYPE_NFIELDS (type)));
9089 gdb_assert (default_index == -1
9090 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9091
9092 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9093
9094 struct discriminant_info *disc
9095 = ((struct discriminant_info *)
9096 TYPE_ZALLOC (type,
9097 offsetof (struct discriminant_info, discriminants)
9098 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9099 disc->default_index = default_index;
9100 disc->discriminant_index = discriminant_index;
9101
9102 struct dynamic_prop prop;
9103 prop.kind = PROP_UNDEFINED;
9104 prop.data.baton = disc;
9105
9106 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9107
9108 return disc;
9109 }
9110
9111 /* Some versions of rustc emitted enums in an unusual way.
9112
9113 Ordinary enums were emitted as unions. The first element of each
9114 structure in the union was named "RUST$ENUM$DISR". This element
9115 held the discriminant.
9116
9117 These versions of Rust also implemented the "non-zero"
9118 optimization. When the enum had two values, and one is empty and
9119 the other holds a pointer that cannot be zero, the pointer is used
9120 as the discriminant, with a zero value meaning the empty variant.
9121 Here, the union's first member is of the form
9122 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9123 where the fieldnos are the indices of the fields that should be
9124 traversed in order to find the field (which may be several fields deep)
9125 and the variantname is the name of the variant of the case when the
9126 field is zero.
9127
9128 This function recognizes whether TYPE is of one of these forms,
9129 and, if so, smashes it to be a variant type. */
9130
9131 static void
9132 quirk_rust_enum (struct type *type, struct objfile *objfile)
9133 {
9134 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9135
9136 /* We don't need to deal with empty enums. */
9137 if (TYPE_NFIELDS (type) == 0)
9138 return;
9139
9140 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9141 if (TYPE_NFIELDS (type) == 1
9142 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9143 {
9144 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9145
9146 /* Decode the field name to find the offset of the
9147 discriminant. */
9148 ULONGEST bit_offset = 0;
9149 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9150 while (name[0] >= '0' && name[0] <= '9')
9151 {
9152 char *tail;
9153 unsigned long index = strtoul (name, &tail, 10);
9154 name = tail;
9155 if (*name != '$'
9156 || index >= TYPE_NFIELDS (field_type)
9157 || (TYPE_FIELD_LOC_KIND (field_type, index)
9158 != FIELD_LOC_KIND_BITPOS))
9159 {
9160 complaint (_("Could not parse Rust enum encoding string \"%s\""
9161 "[in module %s]"),
9162 TYPE_FIELD_NAME (type, 0),
9163 objfile_name (objfile));
9164 return;
9165 }
9166 ++name;
9167
9168 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9169 field_type = TYPE_FIELD_TYPE (field_type, index);
9170 }
9171
9172 /* Make a union to hold the variants. */
9173 struct type *union_type = alloc_type (objfile);
9174 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9175 TYPE_NFIELDS (union_type) = 3;
9176 TYPE_FIELDS (union_type)
9177 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9178 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9179 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9180
9181 /* Put the discriminant must at index 0. */
9182 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9183 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9184 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9185 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9186
9187 /* The order of fields doesn't really matter, so put the real
9188 field at index 1 and the data-less field at index 2. */
9189 struct discriminant_info *disc
9190 = alloc_discriminant_info (union_type, 0, 1);
9191 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9192 TYPE_FIELD_NAME (union_type, 1)
9193 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9194 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9195 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9196 TYPE_FIELD_NAME (union_type, 1));
9197
9198 const char *dataless_name
9199 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9200 name);
9201 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9202 dataless_name);
9203 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9204 /* NAME points into the original discriminant name, which
9205 already has the correct lifetime. */
9206 TYPE_FIELD_NAME (union_type, 2) = name;
9207 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9208 disc->discriminants[2] = 0;
9209
9210 /* Smash this type to be a structure type. We have to do this
9211 because the type has already been recorded. */
9212 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9213 TYPE_NFIELDS (type) = 1;
9214 TYPE_FIELDS (type)
9215 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9216
9217 /* Install the variant part. */
9218 TYPE_FIELD_TYPE (type, 0) = union_type;
9219 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9220 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9221 }
9222 /* A union with a single anonymous field is probably an old-style
9223 univariant enum. */
9224 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9225 {
9226 /* Smash this type to be a structure type. We have to do this
9227 because the type has already been recorded. */
9228 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9229
9230 /* Make a union to hold the variants. */
9231 struct type *union_type = alloc_type (objfile);
9232 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9233 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9234 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9235 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9236 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9237
9238 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9239 const char *variant_name
9240 = rust_last_path_segment (TYPE_NAME (field_type));
9241 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9242 TYPE_NAME (field_type)
9243 = rust_fully_qualify (&objfile->objfile_obstack,
9244 TYPE_NAME (type), variant_name);
9245
9246 /* Install the union in the outer struct type. */
9247 TYPE_NFIELDS (type) = 1;
9248 TYPE_FIELDS (type)
9249 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9250 TYPE_FIELD_TYPE (type, 0) = union_type;
9251 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9252 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9253
9254 alloc_discriminant_info (union_type, -1, 0);
9255 }
9256 else
9257 {
9258 struct type *disr_type = nullptr;
9259 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9260 {
9261 disr_type = TYPE_FIELD_TYPE (type, i);
9262
9263 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9264 {
9265 /* All fields of a true enum will be structs. */
9266 return;
9267 }
9268 else if (TYPE_NFIELDS (disr_type) == 0)
9269 {
9270 /* Could be data-less variant, so keep going. */
9271 disr_type = nullptr;
9272 }
9273 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9274 "RUST$ENUM$DISR") != 0)
9275 {
9276 /* Not a Rust enum. */
9277 return;
9278 }
9279 else
9280 {
9281 /* Found one. */
9282 break;
9283 }
9284 }
9285
9286 /* If we got here without a discriminant, then it's probably
9287 just a union. */
9288 if (disr_type == nullptr)
9289 return;
9290
9291 /* Smash this type to be a structure type. We have to do this
9292 because the type has already been recorded. */
9293 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9294
9295 /* Make a union to hold the variants. */
9296 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9297 struct type *union_type = alloc_type (objfile);
9298 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9299 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9300 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9301 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9302 TYPE_FIELDS (union_type)
9303 = (struct field *) TYPE_ZALLOC (union_type,
9304 (TYPE_NFIELDS (union_type)
9305 * sizeof (struct field)));
9306
9307 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9308 TYPE_NFIELDS (type) * sizeof (struct field));
9309
9310 /* Install the discriminant at index 0 in the union. */
9311 TYPE_FIELD (union_type, 0) = *disr_field;
9312 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9313 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9314
9315 /* Install the union in the outer struct type. */
9316 TYPE_FIELD_TYPE (type, 0) = union_type;
9317 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9318 TYPE_NFIELDS (type) = 1;
9319
9320 /* Set the size and offset of the union type. */
9321 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9322
9323 /* We need a way to find the correct discriminant given a
9324 variant name. For convenience we build a map here. */
9325 struct type *enum_type = FIELD_TYPE (*disr_field);
9326 std::unordered_map<std::string, ULONGEST> discriminant_map;
9327 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9328 {
9329 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9330 {
9331 const char *name
9332 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9333 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9334 }
9335 }
9336
9337 int n_fields = TYPE_NFIELDS (union_type);
9338 struct discriminant_info *disc
9339 = alloc_discriminant_info (union_type, 0, -1);
9340 /* Skip the discriminant here. */
9341 for (int i = 1; i < n_fields; ++i)
9342 {
9343 /* Find the final word in the name of this variant's type.
9344 That name can be used to look up the correct
9345 discriminant. */
9346 const char *variant_name
9347 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9348 i)));
9349
9350 auto iter = discriminant_map.find (variant_name);
9351 if (iter != discriminant_map.end ())
9352 disc->discriminants[i] = iter->second;
9353
9354 /* Remove the discriminant field, if it exists. */
9355 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9356 if (TYPE_NFIELDS (sub_type) > 0)
9357 {
9358 --TYPE_NFIELDS (sub_type);
9359 ++TYPE_FIELDS (sub_type);
9360 }
9361 TYPE_FIELD_NAME (union_type, i) = variant_name;
9362 TYPE_NAME (sub_type)
9363 = rust_fully_qualify (&objfile->objfile_obstack,
9364 TYPE_NAME (type), variant_name);
9365 }
9366 }
9367 }
9368
9369 /* Rewrite some Rust unions to be structures with variants parts. */
9370
9371 static void
9372 rust_union_quirks (struct dwarf2_cu *cu)
9373 {
9374 gdb_assert (cu->language == language_rust);
9375 for (type *type_ : cu->rust_unions)
9376 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9377 /* We don't need this any more. */
9378 cu->rust_unions.clear ();
9379 }
9380
9381 /* Return the symtab for PER_CU. This works properly regardless of
9382 whether we're using the index or psymtabs. */
9383
9384 static struct compunit_symtab *
9385 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9386 {
9387 return (per_cu->dwarf2_per_objfile->using_index
9388 ? per_cu->v.quick->compunit_symtab
9389 : per_cu->v.psymtab->compunit_symtab);
9390 }
9391
9392 /* A helper function for computing the list of all symbol tables
9393 included by PER_CU. */
9394
9395 static void
9396 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9397 htab_t all_children, htab_t all_type_symtabs,
9398 struct dwarf2_per_cu_data *per_cu,
9399 struct compunit_symtab *immediate_parent)
9400 {
9401 void **slot;
9402 struct compunit_symtab *cust;
9403
9404 slot = htab_find_slot (all_children, per_cu, INSERT);
9405 if (*slot != NULL)
9406 {
9407 /* This inclusion and its children have been processed. */
9408 return;
9409 }
9410
9411 *slot = per_cu;
9412 /* Only add a CU if it has a symbol table. */
9413 cust = get_compunit_symtab (per_cu);
9414 if (cust != NULL)
9415 {
9416 /* If this is a type unit only add its symbol table if we haven't
9417 seen it yet (type unit per_cu's can share symtabs). */
9418 if (per_cu->is_debug_types)
9419 {
9420 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9421 if (*slot == NULL)
9422 {
9423 *slot = cust;
9424 result->push_back (cust);
9425 if (cust->user == NULL)
9426 cust->user = immediate_parent;
9427 }
9428 }
9429 else
9430 {
9431 result->push_back (cust);
9432 if (cust->user == NULL)
9433 cust->user = immediate_parent;
9434 }
9435 }
9436
9437 if (!per_cu->imported_symtabs_empty ())
9438 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9439 {
9440 recursively_compute_inclusions (result, all_children,
9441 all_type_symtabs, ptr, cust);
9442 }
9443 }
9444
9445 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9446 PER_CU. */
9447
9448 static void
9449 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9450 {
9451 gdb_assert (! per_cu->is_debug_types);
9452
9453 if (!per_cu->imported_symtabs_empty ())
9454 {
9455 int len;
9456 std::vector<compunit_symtab *> result_symtabs;
9457 htab_t all_children, all_type_symtabs;
9458 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9459
9460 /* If we don't have a symtab, we can just skip this case. */
9461 if (cust == NULL)
9462 return;
9463
9464 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9465 NULL, xcalloc, xfree);
9466 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9467 NULL, xcalloc, xfree);
9468
9469 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9470 {
9471 recursively_compute_inclusions (&result_symtabs, all_children,
9472 all_type_symtabs, ptr, cust);
9473 }
9474
9475 /* Now we have a transitive closure of all the included symtabs. */
9476 len = result_symtabs.size ();
9477 cust->includes
9478 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9479 struct compunit_symtab *, len + 1);
9480 memcpy (cust->includes, result_symtabs.data (),
9481 len * sizeof (compunit_symtab *));
9482 cust->includes[len] = NULL;
9483
9484 htab_delete (all_children);
9485 htab_delete (all_type_symtabs);
9486 }
9487 }
9488
9489 /* Compute the 'includes' field for the symtabs of all the CUs we just
9490 read. */
9491
9492 static void
9493 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9494 {
9495 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9496 {
9497 if (! iter->is_debug_types)
9498 compute_compunit_symtab_includes (iter);
9499 }
9500
9501 dwarf2_per_objfile->just_read_cus.clear ();
9502 }
9503
9504 /* Generate full symbol information for PER_CU, whose DIEs have
9505 already been loaded into memory. */
9506
9507 static void
9508 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9509 enum language pretend_language)
9510 {
9511 struct dwarf2_cu *cu = per_cu->cu;
9512 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9513 struct objfile *objfile = dwarf2_per_objfile->objfile;
9514 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9515 CORE_ADDR lowpc, highpc;
9516 struct compunit_symtab *cust;
9517 CORE_ADDR baseaddr;
9518 struct block *static_block;
9519 CORE_ADDR addr;
9520
9521 baseaddr = objfile->text_section_offset ();
9522
9523 /* Clear the list here in case something was left over. */
9524 cu->method_list.clear ();
9525
9526 cu->language = pretend_language;
9527 cu->language_defn = language_def (cu->language);
9528
9529 /* Do line number decoding in read_file_scope () */
9530 process_die (cu->dies, cu);
9531
9532 /* For now fudge the Go package. */
9533 if (cu->language == language_go)
9534 fixup_go_packaging (cu);
9535
9536 /* Now that we have processed all the DIEs in the CU, all the types
9537 should be complete, and it should now be safe to compute all of the
9538 physnames. */
9539 compute_delayed_physnames (cu);
9540
9541 if (cu->language == language_rust)
9542 rust_union_quirks (cu);
9543
9544 /* Some compilers don't define a DW_AT_high_pc attribute for the
9545 compilation unit. If the DW_AT_high_pc is missing, synthesize
9546 it, by scanning the DIE's below the compilation unit. */
9547 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9548
9549 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9550 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9551
9552 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9553 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9554 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9555 addrmap to help ensure it has an accurate map of pc values belonging to
9556 this comp unit. */
9557 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9558
9559 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9560 SECT_OFF_TEXT (objfile),
9561 0);
9562
9563 if (cust != NULL)
9564 {
9565 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9566
9567 /* Set symtab language to language from DW_AT_language. If the
9568 compilation is from a C file generated by language preprocessors, do
9569 not set the language if it was already deduced by start_subfile. */
9570 if (!(cu->language == language_c
9571 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9572 COMPUNIT_FILETABS (cust)->language = cu->language;
9573
9574 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9575 produce DW_AT_location with location lists but it can be possibly
9576 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9577 there were bugs in prologue debug info, fixed later in GCC-4.5
9578 by "unwind info for epilogues" patch (which is not directly related).
9579
9580 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9581 needed, it would be wrong due to missing DW_AT_producer there.
9582
9583 Still one can confuse GDB by using non-standard GCC compilation
9584 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9585 */
9586 if (cu->has_loclist && gcc_4_minor >= 5)
9587 cust->locations_valid = 1;
9588
9589 if (gcc_4_minor >= 5)
9590 cust->epilogue_unwind_valid = 1;
9591
9592 cust->call_site_htab = cu->call_site_htab;
9593 }
9594
9595 if (dwarf2_per_objfile->using_index)
9596 per_cu->v.quick->compunit_symtab = cust;
9597 else
9598 {
9599 dwarf2_psymtab *pst = per_cu->v.psymtab;
9600 pst->compunit_symtab = cust;
9601 pst->readin = true;
9602 }
9603
9604 /* Push it for inclusion processing later. */
9605 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9606
9607 /* Not needed any more. */
9608 cu->reset_builder ();
9609 }
9610
9611 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9612 already been loaded into memory. */
9613
9614 static void
9615 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9616 enum language pretend_language)
9617 {
9618 struct dwarf2_cu *cu = per_cu->cu;
9619 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9620 struct objfile *objfile = dwarf2_per_objfile->objfile;
9621 struct compunit_symtab *cust;
9622 struct signatured_type *sig_type;
9623
9624 gdb_assert (per_cu->is_debug_types);
9625 sig_type = (struct signatured_type *) per_cu;
9626
9627 /* Clear the list here in case something was left over. */
9628 cu->method_list.clear ();
9629
9630 cu->language = pretend_language;
9631 cu->language_defn = language_def (cu->language);
9632
9633 /* The symbol tables are set up in read_type_unit_scope. */
9634 process_die (cu->dies, cu);
9635
9636 /* For now fudge the Go package. */
9637 if (cu->language == language_go)
9638 fixup_go_packaging (cu);
9639
9640 /* Now that we have processed all the DIEs in the CU, all the types
9641 should be complete, and it should now be safe to compute all of the
9642 physnames. */
9643 compute_delayed_physnames (cu);
9644
9645 if (cu->language == language_rust)
9646 rust_union_quirks (cu);
9647
9648 /* TUs share symbol tables.
9649 If this is the first TU to use this symtab, complete the construction
9650 of it with end_expandable_symtab. Otherwise, complete the addition of
9651 this TU's symbols to the existing symtab. */
9652 if (sig_type->type_unit_group->compunit_symtab == NULL)
9653 {
9654 buildsym_compunit *builder = cu->get_builder ();
9655 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9656 sig_type->type_unit_group->compunit_symtab = cust;
9657
9658 if (cust != NULL)
9659 {
9660 /* Set symtab language to language from DW_AT_language. If the
9661 compilation is from a C file generated by language preprocessors,
9662 do not set the language if it was already deduced by
9663 start_subfile. */
9664 if (!(cu->language == language_c
9665 && COMPUNIT_FILETABS (cust)->language != language_c))
9666 COMPUNIT_FILETABS (cust)->language = cu->language;
9667 }
9668 }
9669 else
9670 {
9671 cu->get_builder ()->augment_type_symtab ();
9672 cust = sig_type->type_unit_group->compunit_symtab;
9673 }
9674
9675 if (dwarf2_per_objfile->using_index)
9676 per_cu->v.quick->compunit_symtab = cust;
9677 else
9678 {
9679 dwarf2_psymtab *pst = per_cu->v.psymtab;
9680 pst->compunit_symtab = cust;
9681 pst->readin = true;
9682 }
9683
9684 /* Not needed any more. */
9685 cu->reset_builder ();
9686 }
9687
9688 /* Process an imported unit DIE. */
9689
9690 static void
9691 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9692 {
9693 struct attribute *attr;
9694
9695 /* For now we don't handle imported units in type units. */
9696 if (cu->per_cu->is_debug_types)
9697 {
9698 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9699 " supported in type units [in module %s]"),
9700 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9701 }
9702
9703 attr = dwarf2_attr (die, DW_AT_import, cu);
9704 if (attr != NULL)
9705 {
9706 sect_offset sect_off = attr->get_ref_die_offset ();
9707 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9708 dwarf2_per_cu_data *per_cu
9709 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9710 cu->per_cu->dwarf2_per_objfile);
9711
9712 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9713 into another compilation unit, at root level. Regard this as a hint,
9714 and ignore it. */
9715 if (die->parent && die->parent->parent == NULL
9716 && per_cu->unit_type == DW_UT_compile
9717 && per_cu->lang == language_cplus)
9718 return;
9719
9720 /* If necessary, add it to the queue and load its DIEs. */
9721 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9722 load_full_comp_unit (per_cu, false, cu->language);
9723
9724 cu->per_cu->imported_symtabs_push (per_cu);
9725 }
9726 }
9727
9728 /* RAII object that represents a process_die scope: i.e.,
9729 starts/finishes processing a DIE. */
9730 class process_die_scope
9731 {
9732 public:
9733 process_die_scope (die_info *die, dwarf2_cu *cu)
9734 : m_die (die), m_cu (cu)
9735 {
9736 /* We should only be processing DIEs not already in process. */
9737 gdb_assert (!m_die->in_process);
9738 m_die->in_process = true;
9739 }
9740
9741 ~process_die_scope ()
9742 {
9743 m_die->in_process = false;
9744
9745 /* If we're done processing the DIE for the CU that owns the line
9746 header, we don't need the line header anymore. */
9747 if (m_cu->line_header_die_owner == m_die)
9748 {
9749 delete m_cu->line_header;
9750 m_cu->line_header = NULL;
9751 m_cu->line_header_die_owner = NULL;
9752 }
9753 }
9754
9755 private:
9756 die_info *m_die;
9757 dwarf2_cu *m_cu;
9758 };
9759
9760 /* Process a die and its children. */
9761
9762 static void
9763 process_die (struct die_info *die, struct dwarf2_cu *cu)
9764 {
9765 process_die_scope scope (die, cu);
9766
9767 switch (die->tag)
9768 {
9769 case DW_TAG_padding:
9770 break;
9771 case DW_TAG_compile_unit:
9772 case DW_TAG_partial_unit:
9773 read_file_scope (die, cu);
9774 break;
9775 case DW_TAG_type_unit:
9776 read_type_unit_scope (die, cu);
9777 break;
9778 case DW_TAG_subprogram:
9779 /* Nested subprograms in Fortran get a prefix. */
9780 if (cu->language == language_fortran
9781 && die->parent != NULL
9782 && die->parent->tag == DW_TAG_subprogram)
9783 cu->processing_has_namespace_info = true;
9784 /* Fall through. */
9785 case DW_TAG_inlined_subroutine:
9786 read_func_scope (die, cu);
9787 break;
9788 case DW_TAG_lexical_block:
9789 case DW_TAG_try_block:
9790 case DW_TAG_catch_block:
9791 read_lexical_block_scope (die, cu);
9792 break;
9793 case DW_TAG_call_site:
9794 case DW_TAG_GNU_call_site:
9795 read_call_site_scope (die, cu);
9796 break;
9797 case DW_TAG_class_type:
9798 case DW_TAG_interface_type:
9799 case DW_TAG_structure_type:
9800 case DW_TAG_union_type:
9801 process_structure_scope (die, cu);
9802 break;
9803 case DW_TAG_enumeration_type:
9804 process_enumeration_scope (die, cu);
9805 break;
9806
9807 /* These dies have a type, but processing them does not create
9808 a symbol or recurse to process the children. Therefore we can
9809 read them on-demand through read_type_die. */
9810 case DW_TAG_subroutine_type:
9811 case DW_TAG_set_type:
9812 case DW_TAG_array_type:
9813 case DW_TAG_pointer_type:
9814 case DW_TAG_ptr_to_member_type:
9815 case DW_TAG_reference_type:
9816 case DW_TAG_rvalue_reference_type:
9817 case DW_TAG_string_type:
9818 break;
9819
9820 case DW_TAG_base_type:
9821 case DW_TAG_subrange_type:
9822 case DW_TAG_typedef:
9823 /* Add a typedef symbol for the type definition, if it has a
9824 DW_AT_name. */
9825 new_symbol (die, read_type_die (die, cu), cu);
9826 break;
9827 case DW_TAG_common_block:
9828 read_common_block (die, cu);
9829 break;
9830 case DW_TAG_common_inclusion:
9831 break;
9832 case DW_TAG_namespace:
9833 cu->processing_has_namespace_info = true;
9834 read_namespace (die, cu);
9835 break;
9836 case DW_TAG_module:
9837 cu->processing_has_namespace_info = true;
9838 read_module (die, cu);
9839 break;
9840 case DW_TAG_imported_declaration:
9841 cu->processing_has_namespace_info = true;
9842 if (read_namespace_alias (die, cu))
9843 break;
9844 /* The declaration is not a global namespace alias. */
9845 /* Fall through. */
9846 case DW_TAG_imported_module:
9847 cu->processing_has_namespace_info = true;
9848 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9849 || cu->language != language_fortran))
9850 complaint (_("Tag '%s' has unexpected children"),
9851 dwarf_tag_name (die->tag));
9852 read_import_statement (die, cu);
9853 break;
9854
9855 case DW_TAG_imported_unit:
9856 process_imported_unit_die (die, cu);
9857 break;
9858
9859 case DW_TAG_variable:
9860 read_variable (die, cu);
9861 break;
9862
9863 default:
9864 new_symbol (die, NULL, cu);
9865 break;
9866 }
9867 }
9868 \f
9869 /* DWARF name computation. */
9870
9871 /* A helper function for dwarf2_compute_name which determines whether DIE
9872 needs to have the name of the scope prepended to the name listed in the
9873 die. */
9874
9875 static int
9876 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9877 {
9878 struct attribute *attr;
9879
9880 switch (die->tag)
9881 {
9882 case DW_TAG_namespace:
9883 case DW_TAG_typedef:
9884 case DW_TAG_class_type:
9885 case DW_TAG_interface_type:
9886 case DW_TAG_structure_type:
9887 case DW_TAG_union_type:
9888 case DW_TAG_enumeration_type:
9889 case DW_TAG_enumerator:
9890 case DW_TAG_subprogram:
9891 case DW_TAG_inlined_subroutine:
9892 case DW_TAG_member:
9893 case DW_TAG_imported_declaration:
9894 return 1;
9895
9896 case DW_TAG_variable:
9897 case DW_TAG_constant:
9898 /* We only need to prefix "globally" visible variables. These include
9899 any variable marked with DW_AT_external or any variable that
9900 lives in a namespace. [Variables in anonymous namespaces
9901 require prefixing, but they are not DW_AT_external.] */
9902
9903 if (dwarf2_attr (die, DW_AT_specification, cu))
9904 {
9905 struct dwarf2_cu *spec_cu = cu;
9906
9907 return die_needs_namespace (die_specification (die, &spec_cu),
9908 spec_cu);
9909 }
9910
9911 attr = dwarf2_attr (die, DW_AT_external, cu);
9912 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9913 && die->parent->tag != DW_TAG_module)
9914 return 0;
9915 /* A variable in a lexical block of some kind does not need a
9916 namespace, even though in C++ such variables may be external
9917 and have a mangled name. */
9918 if (die->parent->tag == DW_TAG_lexical_block
9919 || die->parent->tag == DW_TAG_try_block
9920 || die->parent->tag == DW_TAG_catch_block
9921 || die->parent->tag == DW_TAG_subprogram)
9922 return 0;
9923 return 1;
9924
9925 default:
9926 return 0;
9927 }
9928 }
9929
9930 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9931 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9932 defined for the given DIE. */
9933
9934 static struct attribute *
9935 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9936 {
9937 struct attribute *attr;
9938
9939 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9940 if (attr == NULL)
9941 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9942
9943 return attr;
9944 }
9945
9946 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9947 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9948 defined for the given DIE. */
9949
9950 static const char *
9951 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9952 {
9953 const char *linkage_name;
9954
9955 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9956 if (linkage_name == NULL)
9957 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9958
9959 return linkage_name;
9960 }
9961
9962 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9963 compute the physname for the object, which include a method's:
9964 - formal parameters (C++),
9965 - receiver type (Go),
9966
9967 The term "physname" is a bit confusing.
9968 For C++, for example, it is the demangled name.
9969 For Go, for example, it's the mangled name.
9970
9971 For Ada, return the DIE's linkage name rather than the fully qualified
9972 name. PHYSNAME is ignored..
9973
9974 The result is allocated on the objfile_obstack and canonicalized. */
9975
9976 static const char *
9977 dwarf2_compute_name (const char *name,
9978 struct die_info *die, struct dwarf2_cu *cu,
9979 int physname)
9980 {
9981 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9982
9983 if (name == NULL)
9984 name = dwarf2_name (die, cu);
9985
9986 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9987 but otherwise compute it by typename_concat inside GDB.
9988 FIXME: Actually this is not really true, or at least not always true.
9989 It's all very confusing. compute_and_set_names doesn't try to demangle
9990 Fortran names because there is no mangling standard. So new_symbol
9991 will set the demangled name to the result of dwarf2_full_name, and it is
9992 the demangled name that GDB uses if it exists. */
9993 if (cu->language == language_ada
9994 || (cu->language == language_fortran && physname))
9995 {
9996 /* For Ada unit, we prefer the linkage name over the name, as
9997 the former contains the exported name, which the user expects
9998 to be able to reference. Ideally, we want the user to be able
9999 to reference this entity using either natural or linkage name,
10000 but we haven't started looking at this enhancement yet. */
10001 const char *linkage_name = dw2_linkage_name (die, cu);
10002
10003 if (linkage_name != NULL)
10004 return linkage_name;
10005 }
10006
10007 /* These are the only languages we know how to qualify names in. */
10008 if (name != NULL
10009 && (cu->language == language_cplus
10010 || cu->language == language_fortran || cu->language == language_d
10011 || cu->language == language_rust))
10012 {
10013 if (die_needs_namespace (die, cu))
10014 {
10015 const char *prefix;
10016 const char *canonical_name = NULL;
10017
10018 string_file buf;
10019
10020 prefix = determine_prefix (die, cu);
10021 if (*prefix != '\0')
10022 {
10023 gdb::unique_xmalloc_ptr<char> prefixed_name
10024 (typename_concat (NULL, prefix, name, physname, cu));
10025
10026 buf.puts (prefixed_name.get ());
10027 }
10028 else
10029 buf.puts (name);
10030
10031 /* Template parameters may be specified in the DIE's DW_AT_name, or
10032 as children with DW_TAG_template_type_param or
10033 DW_TAG_value_type_param. If the latter, add them to the name
10034 here. If the name already has template parameters, then
10035 skip this step; some versions of GCC emit both, and
10036 it is more efficient to use the pre-computed name.
10037
10038 Something to keep in mind about this process: it is very
10039 unlikely, or in some cases downright impossible, to produce
10040 something that will match the mangled name of a function.
10041 If the definition of the function has the same debug info,
10042 we should be able to match up with it anyway. But fallbacks
10043 using the minimal symbol, for instance to find a method
10044 implemented in a stripped copy of libstdc++, will not work.
10045 If we do not have debug info for the definition, we will have to
10046 match them up some other way.
10047
10048 When we do name matching there is a related problem with function
10049 templates; two instantiated function templates are allowed to
10050 differ only by their return types, which we do not add here. */
10051
10052 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10053 {
10054 struct attribute *attr;
10055 struct die_info *child;
10056 int first = 1;
10057
10058 die->building_fullname = 1;
10059
10060 for (child = die->child; child != NULL; child = child->sibling)
10061 {
10062 struct type *type;
10063 LONGEST value;
10064 const gdb_byte *bytes;
10065 struct dwarf2_locexpr_baton *baton;
10066 struct value *v;
10067
10068 if (child->tag != DW_TAG_template_type_param
10069 && child->tag != DW_TAG_template_value_param)
10070 continue;
10071
10072 if (first)
10073 {
10074 buf.puts ("<");
10075 first = 0;
10076 }
10077 else
10078 buf.puts (", ");
10079
10080 attr = dwarf2_attr (child, DW_AT_type, cu);
10081 if (attr == NULL)
10082 {
10083 complaint (_("template parameter missing DW_AT_type"));
10084 buf.puts ("UNKNOWN_TYPE");
10085 continue;
10086 }
10087 type = die_type (child, cu);
10088
10089 if (child->tag == DW_TAG_template_type_param)
10090 {
10091 c_print_type (type, "", &buf, -1, 0, cu->language,
10092 &type_print_raw_options);
10093 continue;
10094 }
10095
10096 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10097 if (attr == NULL)
10098 {
10099 complaint (_("template parameter missing "
10100 "DW_AT_const_value"));
10101 buf.puts ("UNKNOWN_VALUE");
10102 continue;
10103 }
10104
10105 dwarf2_const_value_attr (attr, type, name,
10106 &cu->comp_unit_obstack, cu,
10107 &value, &bytes, &baton);
10108
10109 if (TYPE_NOSIGN (type))
10110 /* GDB prints characters as NUMBER 'CHAR'. If that's
10111 changed, this can use value_print instead. */
10112 c_printchar (value, type, &buf);
10113 else
10114 {
10115 struct value_print_options opts;
10116
10117 if (baton != NULL)
10118 v = dwarf2_evaluate_loc_desc (type, NULL,
10119 baton->data,
10120 baton->size,
10121 baton->per_cu);
10122 else if (bytes != NULL)
10123 {
10124 v = allocate_value (type);
10125 memcpy (value_contents_writeable (v), bytes,
10126 TYPE_LENGTH (type));
10127 }
10128 else
10129 v = value_from_longest (type, value);
10130
10131 /* Specify decimal so that we do not depend on
10132 the radix. */
10133 get_formatted_print_options (&opts, 'd');
10134 opts.raw = 1;
10135 value_print (v, &buf, &opts);
10136 release_value (v);
10137 }
10138 }
10139
10140 die->building_fullname = 0;
10141
10142 if (!first)
10143 {
10144 /* Close the argument list, with a space if necessary
10145 (nested templates). */
10146 if (!buf.empty () && buf.string ().back () == '>')
10147 buf.puts (" >");
10148 else
10149 buf.puts (">");
10150 }
10151 }
10152
10153 /* For C++ methods, append formal parameter type
10154 information, if PHYSNAME. */
10155
10156 if (physname && die->tag == DW_TAG_subprogram
10157 && cu->language == language_cplus)
10158 {
10159 struct type *type = read_type_die (die, cu);
10160
10161 c_type_print_args (type, &buf, 1, cu->language,
10162 &type_print_raw_options);
10163
10164 if (cu->language == language_cplus)
10165 {
10166 /* Assume that an artificial first parameter is
10167 "this", but do not crash if it is not. RealView
10168 marks unnamed (and thus unused) parameters as
10169 artificial; there is no way to differentiate
10170 the two cases. */
10171 if (TYPE_NFIELDS (type) > 0
10172 && TYPE_FIELD_ARTIFICIAL (type, 0)
10173 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10174 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10175 0))))
10176 buf.puts (" const");
10177 }
10178 }
10179
10180 const std::string &intermediate_name = buf.string ();
10181
10182 if (cu->language == language_cplus)
10183 canonical_name
10184 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10185 objfile);
10186
10187 /* If we only computed INTERMEDIATE_NAME, or if
10188 INTERMEDIATE_NAME is already canonical, then we need to
10189 intern it. */
10190 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10191 name = objfile->intern (intermediate_name);
10192 else
10193 name = canonical_name;
10194 }
10195 }
10196
10197 return name;
10198 }
10199
10200 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10201 If scope qualifiers are appropriate they will be added. The result
10202 will be allocated on the storage_obstack, or NULL if the DIE does
10203 not have a name. NAME may either be from a previous call to
10204 dwarf2_name or NULL.
10205
10206 The output string will be canonicalized (if C++). */
10207
10208 static const char *
10209 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10210 {
10211 return dwarf2_compute_name (name, die, cu, 0);
10212 }
10213
10214 /* Construct a physname for the given DIE in CU. NAME may either be
10215 from a previous call to dwarf2_name or NULL. The result will be
10216 allocated on the objfile_objstack or NULL if the DIE does not have a
10217 name.
10218
10219 The output string will be canonicalized (if C++). */
10220
10221 static const char *
10222 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10223 {
10224 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10225 const char *retval, *mangled = NULL, *canon = NULL;
10226 int need_copy = 1;
10227
10228 /* In this case dwarf2_compute_name is just a shortcut not building anything
10229 on its own. */
10230 if (!die_needs_namespace (die, cu))
10231 return dwarf2_compute_name (name, die, cu, 1);
10232
10233 mangled = dw2_linkage_name (die, cu);
10234
10235 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10236 See https://github.com/rust-lang/rust/issues/32925. */
10237 if (cu->language == language_rust && mangled != NULL
10238 && strchr (mangled, '{') != NULL)
10239 mangled = NULL;
10240
10241 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10242 has computed. */
10243 gdb::unique_xmalloc_ptr<char> demangled;
10244 if (mangled != NULL)
10245 {
10246
10247 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10248 {
10249 /* Do nothing (do not demangle the symbol name). */
10250 }
10251 else if (cu->language == language_go)
10252 {
10253 /* This is a lie, but we already lie to the caller new_symbol.
10254 new_symbol assumes we return the mangled name.
10255 This just undoes that lie until things are cleaned up. */
10256 }
10257 else
10258 {
10259 /* Use DMGL_RET_DROP for C++ template functions to suppress
10260 their return type. It is easier for GDB users to search
10261 for such functions as `name(params)' than `long name(params)'.
10262 In such case the minimal symbol names do not match the full
10263 symbol names but for template functions there is never a need
10264 to look up their definition from their declaration so
10265 the only disadvantage remains the minimal symbol variant
10266 `long name(params)' does not have the proper inferior type. */
10267 demangled.reset (gdb_demangle (mangled,
10268 (DMGL_PARAMS | DMGL_ANSI
10269 | DMGL_RET_DROP)));
10270 }
10271 if (demangled)
10272 canon = demangled.get ();
10273 else
10274 {
10275 canon = mangled;
10276 need_copy = 0;
10277 }
10278 }
10279
10280 if (canon == NULL || check_physname)
10281 {
10282 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10283
10284 if (canon != NULL && strcmp (physname, canon) != 0)
10285 {
10286 /* It may not mean a bug in GDB. The compiler could also
10287 compute DW_AT_linkage_name incorrectly. But in such case
10288 GDB would need to be bug-to-bug compatible. */
10289
10290 complaint (_("Computed physname <%s> does not match demangled <%s> "
10291 "(from linkage <%s>) - DIE at %s [in module %s]"),
10292 physname, canon, mangled, sect_offset_str (die->sect_off),
10293 objfile_name (objfile));
10294
10295 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10296 is available here - over computed PHYSNAME. It is safer
10297 against both buggy GDB and buggy compilers. */
10298
10299 retval = canon;
10300 }
10301 else
10302 {
10303 retval = physname;
10304 need_copy = 0;
10305 }
10306 }
10307 else
10308 retval = canon;
10309
10310 if (need_copy)
10311 retval = objfile->intern (retval);
10312
10313 return retval;
10314 }
10315
10316 /* Inspect DIE in CU for a namespace alias. If one exists, record
10317 a new symbol for it.
10318
10319 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10320
10321 static int
10322 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10323 {
10324 struct attribute *attr;
10325
10326 /* If the die does not have a name, this is not a namespace
10327 alias. */
10328 attr = dwarf2_attr (die, DW_AT_name, cu);
10329 if (attr != NULL)
10330 {
10331 int num;
10332 struct die_info *d = die;
10333 struct dwarf2_cu *imported_cu = cu;
10334
10335 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10336 keep inspecting DIEs until we hit the underlying import. */
10337 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10338 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10339 {
10340 attr = dwarf2_attr (d, DW_AT_import, cu);
10341 if (attr == NULL)
10342 break;
10343
10344 d = follow_die_ref (d, attr, &imported_cu);
10345 if (d->tag != DW_TAG_imported_declaration)
10346 break;
10347 }
10348
10349 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10350 {
10351 complaint (_("DIE at %s has too many recursively imported "
10352 "declarations"), sect_offset_str (d->sect_off));
10353 return 0;
10354 }
10355
10356 if (attr != NULL)
10357 {
10358 struct type *type;
10359 sect_offset sect_off = attr->get_ref_die_offset ();
10360
10361 type = get_die_type_at_offset (sect_off, cu->per_cu);
10362 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10363 {
10364 /* This declaration is a global namespace alias. Add
10365 a symbol for it whose type is the aliased namespace. */
10366 new_symbol (die, type, cu);
10367 return 1;
10368 }
10369 }
10370 }
10371
10372 return 0;
10373 }
10374
10375 /* Return the using directives repository (global or local?) to use in the
10376 current context for CU.
10377
10378 For Ada, imported declarations can materialize renamings, which *may* be
10379 global. However it is impossible (for now?) in DWARF to distinguish
10380 "external" imported declarations and "static" ones. As all imported
10381 declarations seem to be static in all other languages, make them all CU-wide
10382 global only in Ada. */
10383
10384 static struct using_direct **
10385 using_directives (struct dwarf2_cu *cu)
10386 {
10387 if (cu->language == language_ada
10388 && cu->get_builder ()->outermost_context_p ())
10389 return cu->get_builder ()->get_global_using_directives ();
10390 else
10391 return cu->get_builder ()->get_local_using_directives ();
10392 }
10393
10394 /* Read the import statement specified by the given die and record it. */
10395
10396 static void
10397 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10398 {
10399 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10400 struct attribute *import_attr;
10401 struct die_info *imported_die, *child_die;
10402 struct dwarf2_cu *imported_cu;
10403 const char *imported_name;
10404 const char *imported_name_prefix;
10405 const char *canonical_name;
10406 const char *import_alias;
10407 const char *imported_declaration = NULL;
10408 const char *import_prefix;
10409 std::vector<const char *> excludes;
10410
10411 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10412 if (import_attr == NULL)
10413 {
10414 complaint (_("Tag '%s' has no DW_AT_import"),
10415 dwarf_tag_name (die->tag));
10416 return;
10417 }
10418
10419 imported_cu = cu;
10420 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10421 imported_name = dwarf2_name (imported_die, imported_cu);
10422 if (imported_name == NULL)
10423 {
10424 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10425
10426 The import in the following code:
10427 namespace A
10428 {
10429 typedef int B;
10430 }
10431
10432 int main ()
10433 {
10434 using A::B;
10435 B b;
10436 return b;
10437 }
10438
10439 ...
10440 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10441 <52> DW_AT_decl_file : 1
10442 <53> DW_AT_decl_line : 6
10443 <54> DW_AT_import : <0x75>
10444 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10445 <59> DW_AT_name : B
10446 <5b> DW_AT_decl_file : 1
10447 <5c> DW_AT_decl_line : 2
10448 <5d> DW_AT_type : <0x6e>
10449 ...
10450 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10451 <76> DW_AT_byte_size : 4
10452 <77> DW_AT_encoding : 5 (signed)
10453
10454 imports the wrong die ( 0x75 instead of 0x58 ).
10455 This case will be ignored until the gcc bug is fixed. */
10456 return;
10457 }
10458
10459 /* Figure out the local name after import. */
10460 import_alias = dwarf2_name (die, cu);
10461
10462 /* Figure out where the statement is being imported to. */
10463 import_prefix = determine_prefix (die, cu);
10464
10465 /* Figure out what the scope of the imported die is and prepend it
10466 to the name of the imported die. */
10467 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10468
10469 if (imported_die->tag != DW_TAG_namespace
10470 && imported_die->tag != DW_TAG_module)
10471 {
10472 imported_declaration = imported_name;
10473 canonical_name = imported_name_prefix;
10474 }
10475 else if (strlen (imported_name_prefix) > 0)
10476 canonical_name = obconcat (&objfile->objfile_obstack,
10477 imported_name_prefix,
10478 (cu->language == language_d ? "." : "::"),
10479 imported_name, (char *) NULL);
10480 else
10481 canonical_name = imported_name;
10482
10483 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10484 for (child_die = die->child; child_die && child_die->tag;
10485 child_die = child_die->sibling)
10486 {
10487 /* DWARF-4: A Fortran use statement with a “rename list” may be
10488 represented by an imported module entry with an import attribute
10489 referring to the module and owned entries corresponding to those
10490 entities that are renamed as part of being imported. */
10491
10492 if (child_die->tag != DW_TAG_imported_declaration)
10493 {
10494 complaint (_("child DW_TAG_imported_declaration expected "
10495 "- DIE at %s [in module %s]"),
10496 sect_offset_str (child_die->sect_off),
10497 objfile_name (objfile));
10498 continue;
10499 }
10500
10501 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10502 if (import_attr == NULL)
10503 {
10504 complaint (_("Tag '%s' has no DW_AT_import"),
10505 dwarf_tag_name (child_die->tag));
10506 continue;
10507 }
10508
10509 imported_cu = cu;
10510 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10511 &imported_cu);
10512 imported_name = dwarf2_name (imported_die, imported_cu);
10513 if (imported_name == NULL)
10514 {
10515 complaint (_("child DW_TAG_imported_declaration has unknown "
10516 "imported name - DIE at %s [in module %s]"),
10517 sect_offset_str (child_die->sect_off),
10518 objfile_name (objfile));
10519 continue;
10520 }
10521
10522 excludes.push_back (imported_name);
10523
10524 process_die (child_die, cu);
10525 }
10526
10527 add_using_directive (using_directives (cu),
10528 import_prefix,
10529 canonical_name,
10530 import_alias,
10531 imported_declaration,
10532 excludes,
10533 0,
10534 &objfile->objfile_obstack);
10535 }
10536
10537 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10538 types, but gives them a size of zero. Starting with version 14,
10539 ICC is compatible with GCC. */
10540
10541 static bool
10542 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10543 {
10544 if (!cu->checked_producer)
10545 check_producer (cu);
10546
10547 return cu->producer_is_icc_lt_14;
10548 }
10549
10550 /* ICC generates a DW_AT_type for C void functions. This was observed on
10551 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10552 which says that void functions should not have a DW_AT_type. */
10553
10554 static bool
10555 producer_is_icc (struct dwarf2_cu *cu)
10556 {
10557 if (!cu->checked_producer)
10558 check_producer (cu);
10559
10560 return cu->producer_is_icc;
10561 }
10562
10563 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10564 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10565 this, it was first present in GCC release 4.3.0. */
10566
10567 static bool
10568 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10569 {
10570 if (!cu->checked_producer)
10571 check_producer (cu);
10572
10573 return cu->producer_is_gcc_lt_4_3;
10574 }
10575
10576 static file_and_directory
10577 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10578 {
10579 file_and_directory res;
10580
10581 /* Find the filename. Do not use dwarf2_name here, since the filename
10582 is not a source language identifier. */
10583 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10584 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10585
10586 if (res.comp_dir == NULL
10587 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10588 && IS_ABSOLUTE_PATH (res.name))
10589 {
10590 res.comp_dir_storage = ldirname (res.name);
10591 if (!res.comp_dir_storage.empty ())
10592 res.comp_dir = res.comp_dir_storage.c_str ();
10593 }
10594 if (res.comp_dir != NULL)
10595 {
10596 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10597 directory, get rid of it. */
10598 const char *cp = strchr (res.comp_dir, ':');
10599
10600 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10601 res.comp_dir = cp + 1;
10602 }
10603
10604 if (res.name == NULL)
10605 res.name = "<unknown>";
10606
10607 return res;
10608 }
10609
10610 /* Handle DW_AT_stmt_list for a compilation unit.
10611 DIE is the DW_TAG_compile_unit die for CU.
10612 COMP_DIR is the compilation directory. LOWPC is passed to
10613 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10614
10615 static void
10616 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10617 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10618 {
10619 struct dwarf2_per_objfile *dwarf2_per_objfile
10620 = cu->per_cu->dwarf2_per_objfile;
10621 struct attribute *attr;
10622 struct line_header line_header_local;
10623 hashval_t line_header_local_hash;
10624 void **slot;
10625 int decode_mapping;
10626
10627 gdb_assert (! cu->per_cu->is_debug_types);
10628
10629 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10630 if (attr == NULL)
10631 return;
10632
10633 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10634
10635 /* The line header hash table is only created if needed (it exists to
10636 prevent redundant reading of the line table for partial_units).
10637 If we're given a partial_unit, we'll need it. If we're given a
10638 compile_unit, then use the line header hash table if it's already
10639 created, but don't create one just yet. */
10640
10641 if (dwarf2_per_objfile->line_header_hash == NULL
10642 && die->tag == DW_TAG_partial_unit)
10643 {
10644 dwarf2_per_objfile->line_header_hash
10645 .reset (htab_create_alloc (127, line_header_hash_voidp,
10646 line_header_eq_voidp,
10647 free_line_header_voidp,
10648 xcalloc, xfree));
10649 }
10650
10651 line_header_local.sect_off = line_offset;
10652 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10653 line_header_local_hash = line_header_hash (&line_header_local);
10654 if (dwarf2_per_objfile->line_header_hash != NULL)
10655 {
10656 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10657 &line_header_local,
10658 line_header_local_hash, NO_INSERT);
10659
10660 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10661 is not present in *SLOT (since if there is something in *SLOT then
10662 it will be for a partial_unit). */
10663 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10664 {
10665 gdb_assert (*slot != NULL);
10666 cu->line_header = (struct line_header *) *slot;
10667 return;
10668 }
10669 }
10670
10671 /* dwarf_decode_line_header does not yet provide sufficient information.
10672 We always have to call also dwarf_decode_lines for it. */
10673 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10674 if (lh == NULL)
10675 return;
10676
10677 cu->line_header = lh.release ();
10678 cu->line_header_die_owner = die;
10679
10680 if (dwarf2_per_objfile->line_header_hash == NULL)
10681 slot = NULL;
10682 else
10683 {
10684 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10685 &line_header_local,
10686 line_header_local_hash, INSERT);
10687 gdb_assert (slot != NULL);
10688 }
10689 if (slot != NULL && *slot == NULL)
10690 {
10691 /* This newly decoded line number information unit will be owned
10692 by line_header_hash hash table. */
10693 *slot = cu->line_header;
10694 cu->line_header_die_owner = NULL;
10695 }
10696 else
10697 {
10698 /* We cannot free any current entry in (*slot) as that struct line_header
10699 may be already used by multiple CUs. Create only temporary decoded
10700 line_header for this CU - it may happen at most once for each line
10701 number information unit. And if we're not using line_header_hash
10702 then this is what we want as well. */
10703 gdb_assert (die->tag != DW_TAG_partial_unit);
10704 }
10705 decode_mapping = (die->tag != DW_TAG_partial_unit);
10706 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10707 decode_mapping);
10708
10709 }
10710
10711 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10712
10713 static void
10714 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10715 {
10716 struct dwarf2_per_objfile *dwarf2_per_objfile
10717 = cu->per_cu->dwarf2_per_objfile;
10718 struct objfile *objfile = dwarf2_per_objfile->objfile;
10719 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10720 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10721 CORE_ADDR highpc = ((CORE_ADDR) 0);
10722 struct attribute *attr;
10723 struct die_info *child_die;
10724 CORE_ADDR baseaddr;
10725
10726 prepare_one_comp_unit (cu, die, cu->language);
10727 baseaddr = objfile->text_section_offset ();
10728
10729 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10730
10731 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10732 from finish_block. */
10733 if (lowpc == ((CORE_ADDR) -1))
10734 lowpc = highpc;
10735 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10736
10737 file_and_directory fnd = find_file_and_directory (die, cu);
10738
10739 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10740 standardised yet. As a workaround for the language detection we fall
10741 back to the DW_AT_producer string. */
10742 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10743 cu->language = language_opencl;
10744
10745 /* Similar hack for Go. */
10746 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10747 set_cu_language (DW_LANG_Go, cu);
10748
10749 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10750
10751 /* Decode line number information if present. We do this before
10752 processing child DIEs, so that the line header table is available
10753 for DW_AT_decl_file. */
10754 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10755
10756 /* Process all dies in compilation unit. */
10757 if (die->child != NULL)
10758 {
10759 child_die = die->child;
10760 while (child_die && child_die->tag)
10761 {
10762 process_die (child_die, cu);
10763 child_die = child_die->sibling;
10764 }
10765 }
10766
10767 /* Decode macro information, if present. Dwarf 2 macro information
10768 refers to information in the line number info statement program
10769 header, so we can only read it if we've read the header
10770 successfully. */
10771 attr = dwarf2_attr (die, DW_AT_macros, cu);
10772 if (attr == NULL)
10773 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10774 if (attr && cu->line_header)
10775 {
10776 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10777 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10778
10779 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10780 }
10781 else
10782 {
10783 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10784 if (attr && cu->line_header)
10785 {
10786 unsigned int macro_offset = DW_UNSND (attr);
10787
10788 dwarf_decode_macros (cu, macro_offset, 0);
10789 }
10790 }
10791 }
10792
10793 void
10794 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10795 {
10796 struct type_unit_group *tu_group;
10797 int first_time;
10798 struct attribute *attr;
10799 unsigned int i;
10800 struct signatured_type *sig_type;
10801
10802 gdb_assert (per_cu->is_debug_types);
10803 sig_type = (struct signatured_type *) per_cu;
10804
10805 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10806
10807 /* If we're using .gdb_index (includes -readnow) then
10808 per_cu->type_unit_group may not have been set up yet. */
10809 if (sig_type->type_unit_group == NULL)
10810 sig_type->type_unit_group = get_type_unit_group (this, attr);
10811 tu_group = sig_type->type_unit_group;
10812
10813 /* If we've already processed this stmt_list there's no real need to
10814 do it again, we could fake it and just recreate the part we need
10815 (file name,index -> symtab mapping). If data shows this optimization
10816 is useful we can do it then. */
10817 first_time = tu_group->compunit_symtab == NULL;
10818
10819 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10820 debug info. */
10821 line_header_up lh;
10822 if (attr != NULL)
10823 {
10824 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10825 lh = dwarf_decode_line_header (line_offset, this);
10826 }
10827 if (lh == NULL)
10828 {
10829 if (first_time)
10830 start_symtab ("", NULL, 0);
10831 else
10832 {
10833 gdb_assert (tu_group->symtabs == NULL);
10834 gdb_assert (m_builder == nullptr);
10835 struct compunit_symtab *cust = tu_group->compunit_symtab;
10836 m_builder.reset (new struct buildsym_compunit
10837 (COMPUNIT_OBJFILE (cust), "",
10838 COMPUNIT_DIRNAME (cust),
10839 compunit_language (cust),
10840 0, cust));
10841 }
10842 return;
10843 }
10844
10845 line_header = lh.release ();
10846 line_header_die_owner = die;
10847
10848 if (first_time)
10849 {
10850 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10851
10852 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10853 still initializing it, and our caller (a few levels up)
10854 process_full_type_unit still needs to know if this is the first
10855 time. */
10856
10857 tu_group->symtabs
10858 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10859 struct symtab *, line_header->file_names_size ());
10860
10861 auto &file_names = line_header->file_names ();
10862 for (i = 0; i < file_names.size (); ++i)
10863 {
10864 file_entry &fe = file_names[i];
10865 dwarf2_start_subfile (this, fe.name,
10866 fe.include_dir (line_header));
10867 buildsym_compunit *b = get_builder ();
10868 if (b->get_current_subfile ()->symtab == NULL)
10869 {
10870 /* NOTE: start_subfile will recognize when it's been
10871 passed a file it has already seen. So we can't
10872 assume there's a simple mapping from
10873 cu->line_header->file_names to subfiles, plus
10874 cu->line_header->file_names may contain dups. */
10875 b->get_current_subfile ()->symtab
10876 = allocate_symtab (cust, b->get_current_subfile ()->name);
10877 }
10878
10879 fe.symtab = b->get_current_subfile ()->symtab;
10880 tu_group->symtabs[i] = fe.symtab;
10881 }
10882 }
10883 else
10884 {
10885 gdb_assert (m_builder == nullptr);
10886 struct compunit_symtab *cust = tu_group->compunit_symtab;
10887 m_builder.reset (new struct buildsym_compunit
10888 (COMPUNIT_OBJFILE (cust), "",
10889 COMPUNIT_DIRNAME (cust),
10890 compunit_language (cust),
10891 0, cust));
10892
10893 auto &file_names = line_header->file_names ();
10894 for (i = 0; i < file_names.size (); ++i)
10895 {
10896 file_entry &fe = file_names[i];
10897 fe.symtab = tu_group->symtabs[i];
10898 }
10899 }
10900
10901 /* The main symtab is allocated last. Type units don't have DW_AT_name
10902 so they don't have a "real" (so to speak) symtab anyway.
10903 There is later code that will assign the main symtab to all symbols
10904 that don't have one. We need to handle the case of a symbol with a
10905 missing symtab (DW_AT_decl_file) anyway. */
10906 }
10907
10908 /* Process DW_TAG_type_unit.
10909 For TUs we want to skip the first top level sibling if it's not the
10910 actual type being defined by this TU. In this case the first top
10911 level sibling is there to provide context only. */
10912
10913 static void
10914 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10915 {
10916 struct die_info *child_die;
10917
10918 prepare_one_comp_unit (cu, die, language_minimal);
10919
10920 /* Initialize (or reinitialize) the machinery for building symtabs.
10921 We do this before processing child DIEs, so that the line header table
10922 is available for DW_AT_decl_file. */
10923 cu->setup_type_unit_groups (die);
10924
10925 if (die->child != NULL)
10926 {
10927 child_die = die->child;
10928 while (child_die && child_die->tag)
10929 {
10930 process_die (child_die, cu);
10931 child_die = child_die->sibling;
10932 }
10933 }
10934 }
10935 \f
10936 /* DWO/DWP files.
10937
10938 http://gcc.gnu.org/wiki/DebugFission
10939 http://gcc.gnu.org/wiki/DebugFissionDWP
10940
10941 To simplify handling of both DWO files ("object" files with the DWARF info)
10942 and DWP files (a file with the DWOs packaged up into one file), we treat
10943 DWP files as having a collection of virtual DWO files. */
10944
10945 static hashval_t
10946 hash_dwo_file (const void *item)
10947 {
10948 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10949 hashval_t hash;
10950
10951 hash = htab_hash_string (dwo_file->dwo_name);
10952 if (dwo_file->comp_dir != NULL)
10953 hash += htab_hash_string (dwo_file->comp_dir);
10954 return hash;
10955 }
10956
10957 static int
10958 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10959 {
10960 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10961 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10962
10963 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10964 return 0;
10965 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10966 return lhs->comp_dir == rhs->comp_dir;
10967 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10968 }
10969
10970 /* Allocate a hash table for DWO files. */
10971
10972 static htab_up
10973 allocate_dwo_file_hash_table ()
10974 {
10975 auto delete_dwo_file = [] (void *item)
10976 {
10977 struct dwo_file *dwo_file = (struct dwo_file *) item;
10978
10979 delete dwo_file;
10980 };
10981
10982 return htab_up (htab_create_alloc (41,
10983 hash_dwo_file,
10984 eq_dwo_file,
10985 delete_dwo_file,
10986 xcalloc, xfree));
10987 }
10988
10989 /* Lookup DWO file DWO_NAME. */
10990
10991 static void **
10992 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
10993 const char *dwo_name,
10994 const char *comp_dir)
10995 {
10996 struct dwo_file find_entry;
10997 void **slot;
10998
10999 if (dwarf2_per_objfile->dwo_files == NULL)
11000 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
11001
11002 find_entry.dwo_name = dwo_name;
11003 find_entry.comp_dir = comp_dir;
11004 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11005 INSERT);
11006
11007 return slot;
11008 }
11009
11010 static hashval_t
11011 hash_dwo_unit (const void *item)
11012 {
11013 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11014
11015 /* This drops the top 32 bits of the id, but is ok for a hash. */
11016 return dwo_unit->signature;
11017 }
11018
11019 static int
11020 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11021 {
11022 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11023 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11024
11025 /* The signature is assumed to be unique within the DWO file.
11026 So while object file CU dwo_id's always have the value zero,
11027 that's OK, assuming each object file DWO file has only one CU,
11028 and that's the rule for now. */
11029 return lhs->signature == rhs->signature;
11030 }
11031
11032 /* Allocate a hash table for DWO CUs,TUs.
11033 There is one of these tables for each of CUs,TUs for each DWO file. */
11034
11035 static htab_up
11036 allocate_dwo_unit_table ()
11037 {
11038 /* Start out with a pretty small number.
11039 Generally DWO files contain only one CU and maybe some TUs. */
11040 return htab_up (htab_create_alloc (3,
11041 hash_dwo_unit,
11042 eq_dwo_unit,
11043 NULL, xcalloc, xfree));
11044 }
11045
11046 /* die_reader_func for create_dwo_cu. */
11047
11048 static void
11049 create_dwo_cu_reader (const struct die_reader_specs *reader,
11050 const gdb_byte *info_ptr,
11051 struct die_info *comp_unit_die,
11052 struct dwo_file *dwo_file,
11053 struct dwo_unit *dwo_unit)
11054 {
11055 struct dwarf2_cu *cu = reader->cu;
11056 sect_offset sect_off = cu->per_cu->sect_off;
11057 struct dwarf2_section_info *section = cu->per_cu->section;
11058
11059 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11060 if (!signature.has_value ())
11061 {
11062 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11063 " its dwo_id [in module %s]"),
11064 sect_offset_str (sect_off), dwo_file->dwo_name);
11065 return;
11066 }
11067
11068 dwo_unit->dwo_file = dwo_file;
11069 dwo_unit->signature = *signature;
11070 dwo_unit->section = section;
11071 dwo_unit->sect_off = sect_off;
11072 dwo_unit->length = cu->per_cu->length;
11073
11074 if (dwarf_read_debug)
11075 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11076 sect_offset_str (sect_off),
11077 hex_string (dwo_unit->signature));
11078 }
11079
11080 /* Create the dwo_units for the CUs in a DWO_FILE.
11081 Note: This function processes DWO files only, not DWP files. */
11082
11083 static void
11084 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11085 dwarf2_cu *cu, struct dwo_file &dwo_file,
11086 dwarf2_section_info &section, htab_up &cus_htab)
11087 {
11088 struct objfile *objfile = dwarf2_per_objfile->objfile;
11089 const gdb_byte *info_ptr, *end_ptr;
11090
11091 section.read (objfile);
11092 info_ptr = section.buffer;
11093
11094 if (info_ptr == NULL)
11095 return;
11096
11097 if (dwarf_read_debug)
11098 {
11099 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11100 section.get_name (),
11101 section.get_file_name ());
11102 }
11103
11104 end_ptr = info_ptr + section.size;
11105 while (info_ptr < end_ptr)
11106 {
11107 struct dwarf2_per_cu_data per_cu;
11108 struct dwo_unit read_unit {};
11109 struct dwo_unit *dwo_unit;
11110 void **slot;
11111 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11112
11113 memset (&per_cu, 0, sizeof (per_cu));
11114 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11115 per_cu.is_debug_types = 0;
11116 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11117 per_cu.section = &section;
11118
11119 cutu_reader reader (&per_cu, cu, &dwo_file);
11120 if (!reader.dummy_p)
11121 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11122 &dwo_file, &read_unit);
11123 info_ptr += per_cu.length;
11124
11125 // If the unit could not be parsed, skip it.
11126 if (read_unit.dwo_file == NULL)
11127 continue;
11128
11129 if (cus_htab == NULL)
11130 cus_htab = allocate_dwo_unit_table ();
11131
11132 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11133 *dwo_unit = read_unit;
11134 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11135 gdb_assert (slot != NULL);
11136 if (*slot != NULL)
11137 {
11138 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11139 sect_offset dup_sect_off = dup_cu->sect_off;
11140
11141 complaint (_("debug cu entry at offset %s is duplicate to"
11142 " the entry at offset %s, signature %s"),
11143 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11144 hex_string (dwo_unit->signature));
11145 }
11146 *slot = (void *)dwo_unit;
11147 }
11148 }
11149
11150 /* DWP file .debug_{cu,tu}_index section format:
11151 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11152
11153 DWP Version 1:
11154
11155 Both index sections have the same format, and serve to map a 64-bit
11156 signature to a set of section numbers. Each section begins with a header,
11157 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11158 indexes, and a pool of 32-bit section numbers. The index sections will be
11159 aligned at 8-byte boundaries in the file.
11160
11161 The index section header consists of:
11162
11163 V, 32 bit version number
11164 -, 32 bits unused
11165 N, 32 bit number of compilation units or type units in the index
11166 M, 32 bit number of slots in the hash table
11167
11168 Numbers are recorded using the byte order of the application binary.
11169
11170 The hash table begins at offset 16 in the section, and consists of an array
11171 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11172 order of the application binary). Unused slots in the hash table are 0.
11173 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11174
11175 The parallel table begins immediately after the hash table
11176 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11177 array of 32-bit indexes (using the byte order of the application binary),
11178 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11179 table contains a 32-bit index into the pool of section numbers. For unused
11180 hash table slots, the corresponding entry in the parallel table will be 0.
11181
11182 The pool of section numbers begins immediately following the hash table
11183 (at offset 16 + 12 * M from the beginning of the section). The pool of
11184 section numbers consists of an array of 32-bit words (using the byte order
11185 of the application binary). Each item in the array is indexed starting
11186 from 0. The hash table entry provides the index of the first section
11187 number in the set. Additional section numbers in the set follow, and the
11188 set is terminated by a 0 entry (section number 0 is not used in ELF).
11189
11190 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11191 section must be the first entry in the set, and the .debug_abbrev.dwo must
11192 be the second entry. Other members of the set may follow in any order.
11193
11194 ---
11195
11196 DWP Version 2:
11197
11198 DWP Version 2 combines all the .debug_info, etc. sections into one,
11199 and the entries in the index tables are now offsets into these sections.
11200 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11201 section.
11202
11203 Index Section Contents:
11204 Header
11205 Hash Table of Signatures dwp_hash_table.hash_table
11206 Parallel Table of Indices dwp_hash_table.unit_table
11207 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11208 Table of Section Sizes dwp_hash_table.v2.sizes
11209
11210 The index section header consists of:
11211
11212 V, 32 bit version number
11213 L, 32 bit number of columns in the table of section offsets
11214 N, 32 bit number of compilation units or type units in the index
11215 M, 32 bit number of slots in the hash table
11216
11217 Numbers are recorded using the byte order of the application binary.
11218
11219 The hash table has the same format as version 1.
11220 The parallel table of indices has the same format as version 1,
11221 except that the entries are origin-1 indices into the table of sections
11222 offsets and the table of section sizes.
11223
11224 The table of offsets begins immediately following the parallel table
11225 (at offset 16 + 12 * M from the beginning of the section). The table is
11226 a two-dimensional array of 32-bit words (using the byte order of the
11227 application binary), with L columns and N+1 rows, in row-major order.
11228 Each row in the array is indexed starting from 0. The first row provides
11229 a key to the remaining rows: each column in this row provides an identifier
11230 for a debug section, and the offsets in the same column of subsequent rows
11231 refer to that section. The section identifiers are:
11232
11233 DW_SECT_INFO 1 .debug_info.dwo
11234 DW_SECT_TYPES 2 .debug_types.dwo
11235 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11236 DW_SECT_LINE 4 .debug_line.dwo
11237 DW_SECT_LOC 5 .debug_loc.dwo
11238 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11239 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11240 DW_SECT_MACRO 8 .debug_macro.dwo
11241
11242 The offsets provided by the CU and TU index sections are the base offsets
11243 for the contributions made by each CU or TU to the corresponding section
11244 in the package file. Each CU and TU header contains an abbrev_offset
11245 field, used to find the abbreviations table for that CU or TU within the
11246 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11247 be interpreted as relative to the base offset given in the index section.
11248 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11249 should be interpreted as relative to the base offset for .debug_line.dwo,
11250 and offsets into other debug sections obtained from DWARF attributes should
11251 also be interpreted as relative to the corresponding base offset.
11252
11253 The table of sizes begins immediately following the table of offsets.
11254 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11255 with L columns and N rows, in row-major order. Each row in the array is
11256 indexed starting from 1 (row 0 is shared by the two tables).
11257
11258 ---
11259
11260 Hash table lookup is handled the same in version 1 and 2:
11261
11262 We assume that N and M will not exceed 2^32 - 1.
11263 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11264
11265 Given a 64-bit compilation unit signature or a type signature S, an entry
11266 in the hash table is located as follows:
11267
11268 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11269 the low-order k bits all set to 1.
11270
11271 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11272
11273 3) If the hash table entry at index H matches the signature, use that
11274 entry. If the hash table entry at index H is unused (all zeroes),
11275 terminate the search: the signature is not present in the table.
11276
11277 4) Let H = (H + H') modulo M. Repeat at Step 3.
11278
11279 Because M > N and H' and M are relatively prime, the search is guaranteed
11280 to stop at an unused slot or find the match. */
11281
11282 /* Create a hash table to map DWO IDs to their CU/TU entry in
11283 .debug_{info,types}.dwo in DWP_FILE.
11284 Returns NULL if there isn't one.
11285 Note: This function processes DWP files only, not DWO files. */
11286
11287 static struct dwp_hash_table *
11288 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11289 struct dwp_file *dwp_file, int is_debug_types)
11290 {
11291 struct objfile *objfile = dwarf2_per_objfile->objfile;
11292 bfd *dbfd = dwp_file->dbfd.get ();
11293 const gdb_byte *index_ptr, *index_end;
11294 struct dwarf2_section_info *index;
11295 uint32_t version, nr_columns, nr_units, nr_slots;
11296 struct dwp_hash_table *htab;
11297
11298 if (is_debug_types)
11299 index = &dwp_file->sections.tu_index;
11300 else
11301 index = &dwp_file->sections.cu_index;
11302
11303 if (index->empty ())
11304 return NULL;
11305 index->read (objfile);
11306
11307 index_ptr = index->buffer;
11308 index_end = index_ptr + index->size;
11309
11310 version = read_4_bytes (dbfd, index_ptr);
11311 index_ptr += 4;
11312 if (version == 2)
11313 nr_columns = read_4_bytes (dbfd, index_ptr);
11314 else
11315 nr_columns = 0;
11316 index_ptr += 4;
11317 nr_units = read_4_bytes (dbfd, index_ptr);
11318 index_ptr += 4;
11319 nr_slots = read_4_bytes (dbfd, index_ptr);
11320 index_ptr += 4;
11321
11322 if (version != 1 && version != 2)
11323 {
11324 error (_("Dwarf Error: unsupported DWP file version (%s)"
11325 " [in module %s]"),
11326 pulongest (version), dwp_file->name);
11327 }
11328 if (nr_slots != (nr_slots & -nr_slots))
11329 {
11330 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11331 " is not power of 2 [in module %s]"),
11332 pulongest (nr_slots), dwp_file->name);
11333 }
11334
11335 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11336 htab->version = version;
11337 htab->nr_columns = nr_columns;
11338 htab->nr_units = nr_units;
11339 htab->nr_slots = nr_slots;
11340 htab->hash_table = index_ptr;
11341 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11342
11343 /* Exit early if the table is empty. */
11344 if (nr_slots == 0 || nr_units == 0
11345 || (version == 2 && nr_columns == 0))
11346 {
11347 /* All must be zero. */
11348 if (nr_slots != 0 || nr_units != 0
11349 || (version == 2 && nr_columns != 0))
11350 {
11351 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11352 " all zero [in modules %s]"),
11353 dwp_file->name);
11354 }
11355 return htab;
11356 }
11357
11358 if (version == 1)
11359 {
11360 htab->section_pool.v1.indices =
11361 htab->unit_table + sizeof (uint32_t) * nr_slots;
11362 /* It's harder to decide whether the section is too small in v1.
11363 V1 is deprecated anyway so we punt. */
11364 }
11365 else
11366 {
11367 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11368 int *ids = htab->section_pool.v2.section_ids;
11369 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11370 /* Reverse map for error checking. */
11371 int ids_seen[DW_SECT_MAX + 1];
11372 int i;
11373
11374 if (nr_columns < 2)
11375 {
11376 error (_("Dwarf Error: bad DWP hash table, too few columns"
11377 " in section table [in module %s]"),
11378 dwp_file->name);
11379 }
11380 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11381 {
11382 error (_("Dwarf Error: bad DWP hash table, too many columns"
11383 " in section table [in module %s]"),
11384 dwp_file->name);
11385 }
11386 memset (ids, 255, sizeof_ids);
11387 memset (ids_seen, 255, sizeof (ids_seen));
11388 for (i = 0; i < nr_columns; ++i)
11389 {
11390 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11391
11392 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11393 {
11394 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11395 " in section table [in module %s]"),
11396 id, dwp_file->name);
11397 }
11398 if (ids_seen[id] != -1)
11399 {
11400 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11401 " id %d in section table [in module %s]"),
11402 id, dwp_file->name);
11403 }
11404 ids_seen[id] = i;
11405 ids[i] = id;
11406 }
11407 /* Must have exactly one info or types section. */
11408 if (((ids_seen[DW_SECT_INFO] != -1)
11409 + (ids_seen[DW_SECT_TYPES] != -1))
11410 != 1)
11411 {
11412 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11413 " DWO info/types section [in module %s]"),
11414 dwp_file->name);
11415 }
11416 /* Must have an abbrev section. */
11417 if (ids_seen[DW_SECT_ABBREV] == -1)
11418 {
11419 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11420 " section [in module %s]"),
11421 dwp_file->name);
11422 }
11423 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11424 htab->section_pool.v2.sizes =
11425 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11426 * nr_units * nr_columns);
11427 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11428 * nr_units * nr_columns))
11429 > index_end)
11430 {
11431 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11432 " [in module %s]"),
11433 dwp_file->name);
11434 }
11435 }
11436
11437 return htab;
11438 }
11439
11440 /* Update SECTIONS with the data from SECTP.
11441
11442 This function is like the other "locate" section routines that are
11443 passed to bfd_map_over_sections, but in this context the sections to
11444 read comes from the DWP V1 hash table, not the full ELF section table.
11445
11446 The result is non-zero for success, or zero if an error was found. */
11447
11448 static int
11449 locate_v1_virtual_dwo_sections (asection *sectp,
11450 struct virtual_v1_dwo_sections *sections)
11451 {
11452 const struct dwop_section_names *names = &dwop_section_names;
11453
11454 if (section_is_p (sectp->name, &names->abbrev_dwo))
11455 {
11456 /* There can be only one. */
11457 if (sections->abbrev.s.section != NULL)
11458 return 0;
11459 sections->abbrev.s.section = sectp;
11460 sections->abbrev.size = bfd_section_size (sectp);
11461 }
11462 else if (section_is_p (sectp->name, &names->info_dwo)
11463 || section_is_p (sectp->name, &names->types_dwo))
11464 {
11465 /* There can be only one. */
11466 if (sections->info_or_types.s.section != NULL)
11467 return 0;
11468 sections->info_or_types.s.section = sectp;
11469 sections->info_or_types.size = bfd_section_size (sectp);
11470 }
11471 else if (section_is_p (sectp->name, &names->line_dwo))
11472 {
11473 /* There can be only one. */
11474 if (sections->line.s.section != NULL)
11475 return 0;
11476 sections->line.s.section = sectp;
11477 sections->line.size = bfd_section_size (sectp);
11478 }
11479 else if (section_is_p (sectp->name, &names->loc_dwo))
11480 {
11481 /* There can be only one. */
11482 if (sections->loc.s.section != NULL)
11483 return 0;
11484 sections->loc.s.section = sectp;
11485 sections->loc.size = bfd_section_size (sectp);
11486 }
11487 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11488 {
11489 /* There can be only one. */
11490 if (sections->macinfo.s.section != NULL)
11491 return 0;
11492 sections->macinfo.s.section = sectp;
11493 sections->macinfo.size = bfd_section_size (sectp);
11494 }
11495 else if (section_is_p (sectp->name, &names->macro_dwo))
11496 {
11497 /* There can be only one. */
11498 if (sections->macro.s.section != NULL)
11499 return 0;
11500 sections->macro.s.section = sectp;
11501 sections->macro.size = bfd_section_size (sectp);
11502 }
11503 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11504 {
11505 /* There can be only one. */
11506 if (sections->str_offsets.s.section != NULL)
11507 return 0;
11508 sections->str_offsets.s.section = sectp;
11509 sections->str_offsets.size = bfd_section_size (sectp);
11510 }
11511 else
11512 {
11513 /* No other kind of section is valid. */
11514 return 0;
11515 }
11516
11517 return 1;
11518 }
11519
11520 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11521 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11522 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11523 This is for DWP version 1 files. */
11524
11525 static struct dwo_unit *
11526 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11527 struct dwp_file *dwp_file,
11528 uint32_t unit_index,
11529 const char *comp_dir,
11530 ULONGEST signature, int is_debug_types)
11531 {
11532 struct objfile *objfile = dwarf2_per_objfile->objfile;
11533 const struct dwp_hash_table *dwp_htab =
11534 is_debug_types ? dwp_file->tus : dwp_file->cus;
11535 bfd *dbfd = dwp_file->dbfd.get ();
11536 const char *kind = is_debug_types ? "TU" : "CU";
11537 struct dwo_file *dwo_file;
11538 struct dwo_unit *dwo_unit;
11539 struct virtual_v1_dwo_sections sections;
11540 void **dwo_file_slot;
11541 int i;
11542
11543 gdb_assert (dwp_file->version == 1);
11544
11545 if (dwarf_read_debug)
11546 {
11547 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11548 kind,
11549 pulongest (unit_index), hex_string (signature),
11550 dwp_file->name);
11551 }
11552
11553 /* Fetch the sections of this DWO unit.
11554 Put a limit on the number of sections we look for so that bad data
11555 doesn't cause us to loop forever. */
11556
11557 #define MAX_NR_V1_DWO_SECTIONS \
11558 (1 /* .debug_info or .debug_types */ \
11559 + 1 /* .debug_abbrev */ \
11560 + 1 /* .debug_line */ \
11561 + 1 /* .debug_loc */ \
11562 + 1 /* .debug_str_offsets */ \
11563 + 1 /* .debug_macro or .debug_macinfo */ \
11564 + 1 /* trailing zero */)
11565
11566 memset (&sections, 0, sizeof (sections));
11567
11568 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11569 {
11570 asection *sectp;
11571 uint32_t section_nr =
11572 read_4_bytes (dbfd,
11573 dwp_htab->section_pool.v1.indices
11574 + (unit_index + i) * sizeof (uint32_t));
11575
11576 if (section_nr == 0)
11577 break;
11578 if (section_nr >= dwp_file->num_sections)
11579 {
11580 error (_("Dwarf Error: bad DWP hash table, section number too large"
11581 " [in module %s]"),
11582 dwp_file->name);
11583 }
11584
11585 sectp = dwp_file->elf_sections[section_nr];
11586 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11587 {
11588 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11589 " [in module %s]"),
11590 dwp_file->name);
11591 }
11592 }
11593
11594 if (i < 2
11595 || sections.info_or_types.empty ()
11596 || sections.abbrev.empty ())
11597 {
11598 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11599 " [in module %s]"),
11600 dwp_file->name);
11601 }
11602 if (i == MAX_NR_V1_DWO_SECTIONS)
11603 {
11604 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11605 " [in module %s]"),
11606 dwp_file->name);
11607 }
11608
11609 /* It's easier for the rest of the code if we fake a struct dwo_file and
11610 have dwo_unit "live" in that. At least for now.
11611
11612 The DWP file can be made up of a random collection of CUs and TUs.
11613 However, for each CU + set of TUs that came from the same original DWO
11614 file, we can combine them back into a virtual DWO file to save space
11615 (fewer struct dwo_file objects to allocate). Remember that for really
11616 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11617
11618 std::string virtual_dwo_name =
11619 string_printf ("virtual-dwo/%d-%d-%d-%d",
11620 sections.abbrev.get_id (),
11621 sections.line.get_id (),
11622 sections.loc.get_id (),
11623 sections.str_offsets.get_id ());
11624 /* Can we use an existing virtual DWO file? */
11625 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11626 virtual_dwo_name.c_str (),
11627 comp_dir);
11628 /* Create one if necessary. */
11629 if (*dwo_file_slot == NULL)
11630 {
11631 if (dwarf_read_debug)
11632 {
11633 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11634 virtual_dwo_name.c_str ());
11635 }
11636 dwo_file = new struct dwo_file;
11637 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11638 dwo_file->comp_dir = comp_dir;
11639 dwo_file->sections.abbrev = sections.abbrev;
11640 dwo_file->sections.line = sections.line;
11641 dwo_file->sections.loc = sections.loc;
11642 dwo_file->sections.macinfo = sections.macinfo;
11643 dwo_file->sections.macro = sections.macro;
11644 dwo_file->sections.str_offsets = sections.str_offsets;
11645 /* The "str" section is global to the entire DWP file. */
11646 dwo_file->sections.str = dwp_file->sections.str;
11647 /* The info or types section is assigned below to dwo_unit,
11648 there's no need to record it in dwo_file.
11649 Also, we can't simply record type sections in dwo_file because
11650 we record a pointer into the vector in dwo_unit. As we collect more
11651 types we'll grow the vector and eventually have to reallocate space
11652 for it, invalidating all copies of pointers into the previous
11653 contents. */
11654 *dwo_file_slot = dwo_file;
11655 }
11656 else
11657 {
11658 if (dwarf_read_debug)
11659 {
11660 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11661 virtual_dwo_name.c_str ());
11662 }
11663 dwo_file = (struct dwo_file *) *dwo_file_slot;
11664 }
11665
11666 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11667 dwo_unit->dwo_file = dwo_file;
11668 dwo_unit->signature = signature;
11669 dwo_unit->section =
11670 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11671 *dwo_unit->section = sections.info_or_types;
11672 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11673
11674 return dwo_unit;
11675 }
11676
11677 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11678 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11679 piece within that section used by a TU/CU, return a virtual section
11680 of just that piece. */
11681
11682 static struct dwarf2_section_info
11683 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11684 struct dwarf2_section_info *section,
11685 bfd_size_type offset, bfd_size_type size)
11686 {
11687 struct dwarf2_section_info result;
11688 asection *sectp;
11689
11690 gdb_assert (section != NULL);
11691 gdb_assert (!section->is_virtual);
11692
11693 memset (&result, 0, sizeof (result));
11694 result.s.containing_section = section;
11695 result.is_virtual = true;
11696
11697 if (size == 0)
11698 return result;
11699
11700 sectp = section->get_bfd_section ();
11701
11702 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11703 bounds of the real section. This is a pretty-rare event, so just
11704 flag an error (easier) instead of a warning and trying to cope. */
11705 if (sectp == NULL
11706 || offset + size > bfd_section_size (sectp))
11707 {
11708 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11709 " in section %s [in module %s]"),
11710 sectp ? bfd_section_name (sectp) : "<unknown>",
11711 objfile_name (dwarf2_per_objfile->objfile));
11712 }
11713
11714 result.virtual_offset = offset;
11715 result.size = size;
11716 return result;
11717 }
11718
11719 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11720 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11721 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11722 This is for DWP version 2 files. */
11723
11724 static struct dwo_unit *
11725 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11726 struct dwp_file *dwp_file,
11727 uint32_t unit_index,
11728 const char *comp_dir,
11729 ULONGEST signature, int is_debug_types)
11730 {
11731 struct objfile *objfile = dwarf2_per_objfile->objfile;
11732 const struct dwp_hash_table *dwp_htab =
11733 is_debug_types ? dwp_file->tus : dwp_file->cus;
11734 bfd *dbfd = dwp_file->dbfd.get ();
11735 const char *kind = is_debug_types ? "TU" : "CU";
11736 struct dwo_file *dwo_file;
11737 struct dwo_unit *dwo_unit;
11738 struct virtual_v2_dwo_sections sections;
11739 void **dwo_file_slot;
11740 int i;
11741
11742 gdb_assert (dwp_file->version == 2);
11743
11744 if (dwarf_read_debug)
11745 {
11746 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11747 kind,
11748 pulongest (unit_index), hex_string (signature),
11749 dwp_file->name);
11750 }
11751
11752 /* Fetch the section offsets of this DWO unit. */
11753
11754 memset (&sections, 0, sizeof (sections));
11755
11756 for (i = 0; i < dwp_htab->nr_columns; ++i)
11757 {
11758 uint32_t offset = read_4_bytes (dbfd,
11759 dwp_htab->section_pool.v2.offsets
11760 + (((unit_index - 1) * dwp_htab->nr_columns
11761 + i)
11762 * sizeof (uint32_t)));
11763 uint32_t size = read_4_bytes (dbfd,
11764 dwp_htab->section_pool.v2.sizes
11765 + (((unit_index - 1) * dwp_htab->nr_columns
11766 + i)
11767 * sizeof (uint32_t)));
11768
11769 switch (dwp_htab->section_pool.v2.section_ids[i])
11770 {
11771 case DW_SECT_INFO:
11772 case DW_SECT_TYPES:
11773 sections.info_or_types_offset = offset;
11774 sections.info_or_types_size = size;
11775 break;
11776 case DW_SECT_ABBREV:
11777 sections.abbrev_offset = offset;
11778 sections.abbrev_size = size;
11779 break;
11780 case DW_SECT_LINE:
11781 sections.line_offset = offset;
11782 sections.line_size = size;
11783 break;
11784 case DW_SECT_LOC:
11785 sections.loc_offset = offset;
11786 sections.loc_size = size;
11787 break;
11788 case DW_SECT_STR_OFFSETS:
11789 sections.str_offsets_offset = offset;
11790 sections.str_offsets_size = size;
11791 break;
11792 case DW_SECT_MACINFO:
11793 sections.macinfo_offset = offset;
11794 sections.macinfo_size = size;
11795 break;
11796 case DW_SECT_MACRO:
11797 sections.macro_offset = offset;
11798 sections.macro_size = size;
11799 break;
11800 }
11801 }
11802
11803 /* It's easier for the rest of the code if we fake a struct dwo_file and
11804 have dwo_unit "live" in that. At least for now.
11805
11806 The DWP file can be made up of a random collection of CUs and TUs.
11807 However, for each CU + set of TUs that came from the same original DWO
11808 file, we can combine them back into a virtual DWO file to save space
11809 (fewer struct dwo_file objects to allocate). Remember that for really
11810 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11811
11812 std::string virtual_dwo_name =
11813 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11814 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11815 (long) (sections.line_size ? sections.line_offset : 0),
11816 (long) (sections.loc_size ? sections.loc_offset : 0),
11817 (long) (sections.str_offsets_size
11818 ? sections.str_offsets_offset : 0));
11819 /* Can we use an existing virtual DWO file? */
11820 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11821 virtual_dwo_name.c_str (),
11822 comp_dir);
11823 /* Create one if necessary. */
11824 if (*dwo_file_slot == NULL)
11825 {
11826 if (dwarf_read_debug)
11827 {
11828 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11829 virtual_dwo_name.c_str ());
11830 }
11831 dwo_file = new struct dwo_file;
11832 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11833 dwo_file->comp_dir = comp_dir;
11834 dwo_file->sections.abbrev =
11835 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11836 sections.abbrev_offset, sections.abbrev_size);
11837 dwo_file->sections.line =
11838 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11839 sections.line_offset, sections.line_size);
11840 dwo_file->sections.loc =
11841 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11842 sections.loc_offset, sections.loc_size);
11843 dwo_file->sections.macinfo =
11844 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11845 sections.macinfo_offset, sections.macinfo_size);
11846 dwo_file->sections.macro =
11847 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11848 sections.macro_offset, sections.macro_size);
11849 dwo_file->sections.str_offsets =
11850 create_dwp_v2_section (dwarf2_per_objfile,
11851 &dwp_file->sections.str_offsets,
11852 sections.str_offsets_offset,
11853 sections.str_offsets_size);
11854 /* The "str" section is global to the entire DWP file. */
11855 dwo_file->sections.str = dwp_file->sections.str;
11856 /* The info or types section is assigned below to dwo_unit,
11857 there's no need to record it in dwo_file.
11858 Also, we can't simply record type sections in dwo_file because
11859 we record a pointer into the vector in dwo_unit. As we collect more
11860 types we'll grow the vector and eventually have to reallocate space
11861 for it, invalidating all copies of pointers into the previous
11862 contents. */
11863 *dwo_file_slot = dwo_file;
11864 }
11865 else
11866 {
11867 if (dwarf_read_debug)
11868 {
11869 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11870 virtual_dwo_name.c_str ());
11871 }
11872 dwo_file = (struct dwo_file *) *dwo_file_slot;
11873 }
11874
11875 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11876 dwo_unit->dwo_file = dwo_file;
11877 dwo_unit->signature = signature;
11878 dwo_unit->section =
11879 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11880 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11881 is_debug_types
11882 ? &dwp_file->sections.types
11883 : &dwp_file->sections.info,
11884 sections.info_or_types_offset,
11885 sections.info_or_types_size);
11886 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11887
11888 return dwo_unit;
11889 }
11890
11891 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11892 Returns NULL if the signature isn't found. */
11893
11894 static struct dwo_unit *
11895 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11896 struct dwp_file *dwp_file, const char *comp_dir,
11897 ULONGEST signature, int is_debug_types)
11898 {
11899 const struct dwp_hash_table *dwp_htab =
11900 is_debug_types ? dwp_file->tus : dwp_file->cus;
11901 bfd *dbfd = dwp_file->dbfd.get ();
11902 uint32_t mask = dwp_htab->nr_slots - 1;
11903 uint32_t hash = signature & mask;
11904 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11905 unsigned int i;
11906 void **slot;
11907 struct dwo_unit find_dwo_cu;
11908
11909 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11910 find_dwo_cu.signature = signature;
11911 slot = htab_find_slot (is_debug_types
11912 ? dwp_file->loaded_tus.get ()
11913 : dwp_file->loaded_cus.get (),
11914 &find_dwo_cu, INSERT);
11915
11916 if (*slot != NULL)
11917 return (struct dwo_unit *) *slot;
11918
11919 /* Use a for loop so that we don't loop forever on bad debug info. */
11920 for (i = 0; i < dwp_htab->nr_slots; ++i)
11921 {
11922 ULONGEST signature_in_table;
11923
11924 signature_in_table =
11925 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11926 if (signature_in_table == signature)
11927 {
11928 uint32_t unit_index =
11929 read_4_bytes (dbfd,
11930 dwp_htab->unit_table + hash * sizeof (uint32_t));
11931
11932 if (dwp_file->version == 1)
11933 {
11934 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11935 dwp_file, unit_index,
11936 comp_dir, signature,
11937 is_debug_types);
11938 }
11939 else
11940 {
11941 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11942 dwp_file, unit_index,
11943 comp_dir, signature,
11944 is_debug_types);
11945 }
11946 return (struct dwo_unit *) *slot;
11947 }
11948 if (signature_in_table == 0)
11949 return NULL;
11950 hash = (hash + hash2) & mask;
11951 }
11952
11953 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11954 " [in module %s]"),
11955 dwp_file->name);
11956 }
11957
11958 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11959 Open the file specified by FILE_NAME and hand it off to BFD for
11960 preliminary analysis. Return a newly initialized bfd *, which
11961 includes a canonicalized copy of FILE_NAME.
11962 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11963 SEARCH_CWD is true if the current directory is to be searched.
11964 It will be searched before debug-file-directory.
11965 If successful, the file is added to the bfd include table of the
11966 objfile's bfd (see gdb_bfd_record_inclusion).
11967 If unable to find/open the file, return NULL.
11968 NOTE: This function is derived from symfile_bfd_open. */
11969
11970 static gdb_bfd_ref_ptr
11971 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11972 const char *file_name, int is_dwp, int search_cwd)
11973 {
11974 int desc;
11975 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11976 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11977 to debug_file_directory. */
11978 const char *search_path;
11979 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11980
11981 gdb::unique_xmalloc_ptr<char> search_path_holder;
11982 if (search_cwd)
11983 {
11984 if (*debug_file_directory != '\0')
11985 {
11986 search_path_holder.reset (concat (".", dirname_separator_string,
11987 debug_file_directory,
11988 (char *) NULL));
11989 search_path = search_path_holder.get ();
11990 }
11991 else
11992 search_path = ".";
11993 }
11994 else
11995 search_path = debug_file_directory;
11996
11997 openp_flags flags = OPF_RETURN_REALPATH;
11998 if (is_dwp)
11999 flags |= OPF_SEARCH_IN_PATH;
12000
12001 gdb::unique_xmalloc_ptr<char> absolute_name;
12002 desc = openp (search_path, flags, file_name,
12003 O_RDONLY | O_BINARY, &absolute_name);
12004 if (desc < 0)
12005 return NULL;
12006
12007 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12008 gnutarget, desc));
12009 if (sym_bfd == NULL)
12010 return NULL;
12011 bfd_set_cacheable (sym_bfd.get (), 1);
12012
12013 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12014 return NULL;
12015
12016 /* Success. Record the bfd as having been included by the objfile's bfd.
12017 This is important because things like demangled_names_hash lives in the
12018 objfile's per_bfd space and may have references to things like symbol
12019 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12020 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12021
12022 return sym_bfd;
12023 }
12024
12025 /* Try to open DWO file FILE_NAME.
12026 COMP_DIR is the DW_AT_comp_dir attribute.
12027 The result is the bfd handle of the file.
12028 If there is a problem finding or opening the file, return NULL.
12029 Upon success, the canonicalized path of the file is stored in the bfd,
12030 same as symfile_bfd_open. */
12031
12032 static gdb_bfd_ref_ptr
12033 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12034 const char *file_name, const char *comp_dir)
12035 {
12036 if (IS_ABSOLUTE_PATH (file_name))
12037 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12038 0 /*is_dwp*/, 0 /*search_cwd*/);
12039
12040 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12041
12042 if (comp_dir != NULL)
12043 {
12044 gdb::unique_xmalloc_ptr<char> path_to_try
12045 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12046
12047 /* NOTE: If comp_dir is a relative path, this will also try the
12048 search path, which seems useful. */
12049 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12050 path_to_try.get (),
12051 0 /*is_dwp*/,
12052 1 /*search_cwd*/));
12053 if (abfd != NULL)
12054 return abfd;
12055 }
12056
12057 /* That didn't work, try debug-file-directory, which, despite its name,
12058 is a list of paths. */
12059
12060 if (*debug_file_directory == '\0')
12061 return NULL;
12062
12063 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12064 0 /*is_dwp*/, 1 /*search_cwd*/);
12065 }
12066
12067 /* This function is mapped across the sections and remembers the offset and
12068 size of each of the DWO debugging sections we are interested in. */
12069
12070 static void
12071 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12072 {
12073 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12074 const struct dwop_section_names *names = &dwop_section_names;
12075
12076 if (section_is_p (sectp->name, &names->abbrev_dwo))
12077 {
12078 dwo_sections->abbrev.s.section = sectp;
12079 dwo_sections->abbrev.size = bfd_section_size (sectp);
12080 }
12081 else if (section_is_p (sectp->name, &names->info_dwo))
12082 {
12083 dwo_sections->info.s.section = sectp;
12084 dwo_sections->info.size = bfd_section_size (sectp);
12085 }
12086 else if (section_is_p (sectp->name, &names->line_dwo))
12087 {
12088 dwo_sections->line.s.section = sectp;
12089 dwo_sections->line.size = bfd_section_size (sectp);
12090 }
12091 else if (section_is_p (sectp->name, &names->loc_dwo))
12092 {
12093 dwo_sections->loc.s.section = sectp;
12094 dwo_sections->loc.size = bfd_section_size (sectp);
12095 }
12096 else if (section_is_p (sectp->name, &names->loclists_dwo))
12097 {
12098 dwo_sections->loclists.s.section = sectp;
12099 dwo_sections->loclists.size = bfd_section_size (sectp);
12100 }
12101 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12102 {
12103 dwo_sections->macinfo.s.section = sectp;
12104 dwo_sections->macinfo.size = bfd_section_size (sectp);
12105 }
12106 else if (section_is_p (sectp->name, &names->macro_dwo))
12107 {
12108 dwo_sections->macro.s.section = sectp;
12109 dwo_sections->macro.size = bfd_section_size (sectp);
12110 }
12111 else if (section_is_p (sectp->name, &names->str_dwo))
12112 {
12113 dwo_sections->str.s.section = sectp;
12114 dwo_sections->str.size = bfd_section_size (sectp);
12115 }
12116 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12117 {
12118 dwo_sections->str_offsets.s.section = sectp;
12119 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12120 }
12121 else if (section_is_p (sectp->name, &names->types_dwo))
12122 {
12123 struct dwarf2_section_info type_section;
12124
12125 memset (&type_section, 0, sizeof (type_section));
12126 type_section.s.section = sectp;
12127 type_section.size = bfd_section_size (sectp);
12128 dwo_sections->types.push_back (type_section);
12129 }
12130 }
12131
12132 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12133 by PER_CU. This is for the non-DWP case.
12134 The result is NULL if DWO_NAME can't be found. */
12135
12136 static struct dwo_file *
12137 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12138 const char *dwo_name, const char *comp_dir)
12139 {
12140 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12141
12142 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12143 if (dbfd == NULL)
12144 {
12145 if (dwarf_read_debug)
12146 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12147 return NULL;
12148 }
12149
12150 dwo_file_up dwo_file (new struct dwo_file);
12151 dwo_file->dwo_name = dwo_name;
12152 dwo_file->comp_dir = comp_dir;
12153 dwo_file->dbfd = std::move (dbfd);
12154
12155 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12156 &dwo_file->sections);
12157
12158 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12159 dwo_file->sections.info, dwo_file->cus);
12160
12161 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12162 dwo_file->sections.types, dwo_file->tus);
12163
12164 if (dwarf_read_debug)
12165 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12166
12167 return dwo_file.release ();
12168 }
12169
12170 /* This function is mapped across the sections and remembers the offset and
12171 size of each of the DWP debugging sections common to version 1 and 2 that
12172 we are interested in. */
12173
12174 static void
12175 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12176 void *dwp_file_ptr)
12177 {
12178 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12179 const struct dwop_section_names *names = &dwop_section_names;
12180 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12181
12182 /* Record the ELF section number for later lookup: this is what the
12183 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12184 gdb_assert (elf_section_nr < dwp_file->num_sections);
12185 dwp_file->elf_sections[elf_section_nr] = sectp;
12186
12187 /* Look for specific sections that we need. */
12188 if (section_is_p (sectp->name, &names->str_dwo))
12189 {
12190 dwp_file->sections.str.s.section = sectp;
12191 dwp_file->sections.str.size = bfd_section_size (sectp);
12192 }
12193 else if (section_is_p (sectp->name, &names->cu_index))
12194 {
12195 dwp_file->sections.cu_index.s.section = sectp;
12196 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12197 }
12198 else if (section_is_p (sectp->name, &names->tu_index))
12199 {
12200 dwp_file->sections.tu_index.s.section = sectp;
12201 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12202 }
12203 }
12204
12205 /* This function is mapped across the sections and remembers the offset and
12206 size of each of the DWP version 2 debugging sections that we are interested
12207 in. This is split into a separate function because we don't know if we
12208 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12209
12210 static void
12211 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12212 {
12213 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12214 const struct dwop_section_names *names = &dwop_section_names;
12215 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12216
12217 /* Record the ELF section number for later lookup: this is what the
12218 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12219 gdb_assert (elf_section_nr < dwp_file->num_sections);
12220 dwp_file->elf_sections[elf_section_nr] = sectp;
12221
12222 /* Look for specific sections that we need. */
12223 if (section_is_p (sectp->name, &names->abbrev_dwo))
12224 {
12225 dwp_file->sections.abbrev.s.section = sectp;
12226 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12227 }
12228 else if (section_is_p (sectp->name, &names->info_dwo))
12229 {
12230 dwp_file->sections.info.s.section = sectp;
12231 dwp_file->sections.info.size = bfd_section_size (sectp);
12232 }
12233 else if (section_is_p (sectp->name, &names->line_dwo))
12234 {
12235 dwp_file->sections.line.s.section = sectp;
12236 dwp_file->sections.line.size = bfd_section_size (sectp);
12237 }
12238 else if (section_is_p (sectp->name, &names->loc_dwo))
12239 {
12240 dwp_file->sections.loc.s.section = sectp;
12241 dwp_file->sections.loc.size = bfd_section_size (sectp);
12242 }
12243 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12244 {
12245 dwp_file->sections.macinfo.s.section = sectp;
12246 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12247 }
12248 else if (section_is_p (sectp->name, &names->macro_dwo))
12249 {
12250 dwp_file->sections.macro.s.section = sectp;
12251 dwp_file->sections.macro.size = bfd_section_size (sectp);
12252 }
12253 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12254 {
12255 dwp_file->sections.str_offsets.s.section = sectp;
12256 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12257 }
12258 else if (section_is_p (sectp->name, &names->types_dwo))
12259 {
12260 dwp_file->sections.types.s.section = sectp;
12261 dwp_file->sections.types.size = bfd_section_size (sectp);
12262 }
12263 }
12264
12265 /* Hash function for dwp_file loaded CUs/TUs. */
12266
12267 static hashval_t
12268 hash_dwp_loaded_cutus (const void *item)
12269 {
12270 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12271
12272 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12273 return dwo_unit->signature;
12274 }
12275
12276 /* Equality function for dwp_file loaded CUs/TUs. */
12277
12278 static int
12279 eq_dwp_loaded_cutus (const void *a, const void *b)
12280 {
12281 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12282 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12283
12284 return dua->signature == dub->signature;
12285 }
12286
12287 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12288
12289 static htab_up
12290 allocate_dwp_loaded_cutus_table ()
12291 {
12292 return htab_up (htab_create_alloc (3,
12293 hash_dwp_loaded_cutus,
12294 eq_dwp_loaded_cutus,
12295 NULL, xcalloc, xfree));
12296 }
12297
12298 /* Try to open DWP file FILE_NAME.
12299 The result is the bfd handle of the file.
12300 If there is a problem finding or opening the file, return NULL.
12301 Upon success, the canonicalized path of the file is stored in the bfd,
12302 same as symfile_bfd_open. */
12303
12304 static gdb_bfd_ref_ptr
12305 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12306 const char *file_name)
12307 {
12308 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12309 1 /*is_dwp*/,
12310 1 /*search_cwd*/));
12311 if (abfd != NULL)
12312 return abfd;
12313
12314 /* Work around upstream bug 15652.
12315 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12316 [Whether that's a "bug" is debatable, but it is getting in our way.]
12317 We have no real idea where the dwp file is, because gdb's realpath-ing
12318 of the executable's path may have discarded the needed info.
12319 [IWBN if the dwp file name was recorded in the executable, akin to
12320 .gnu_debuglink, but that doesn't exist yet.]
12321 Strip the directory from FILE_NAME and search again. */
12322 if (*debug_file_directory != '\0')
12323 {
12324 /* Don't implicitly search the current directory here.
12325 If the user wants to search "." to handle this case,
12326 it must be added to debug-file-directory. */
12327 return try_open_dwop_file (dwarf2_per_objfile,
12328 lbasename (file_name), 1 /*is_dwp*/,
12329 0 /*search_cwd*/);
12330 }
12331
12332 return NULL;
12333 }
12334
12335 /* Initialize the use of the DWP file for the current objfile.
12336 By convention the name of the DWP file is ${objfile}.dwp.
12337 The result is NULL if it can't be found. */
12338
12339 static std::unique_ptr<struct dwp_file>
12340 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12341 {
12342 struct objfile *objfile = dwarf2_per_objfile->objfile;
12343
12344 /* Try to find first .dwp for the binary file before any symbolic links
12345 resolving. */
12346
12347 /* If the objfile is a debug file, find the name of the real binary
12348 file and get the name of dwp file from there. */
12349 std::string dwp_name;
12350 if (objfile->separate_debug_objfile_backlink != NULL)
12351 {
12352 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12353 const char *backlink_basename = lbasename (backlink->original_name);
12354
12355 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12356 }
12357 else
12358 dwp_name = objfile->original_name;
12359
12360 dwp_name += ".dwp";
12361
12362 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12363 if (dbfd == NULL
12364 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12365 {
12366 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12367 dwp_name = objfile_name (objfile);
12368 dwp_name += ".dwp";
12369 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12370 }
12371
12372 if (dbfd == NULL)
12373 {
12374 if (dwarf_read_debug)
12375 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12376 return std::unique_ptr<dwp_file> ();
12377 }
12378
12379 const char *name = bfd_get_filename (dbfd.get ());
12380 std::unique_ptr<struct dwp_file> dwp_file
12381 (new struct dwp_file (name, std::move (dbfd)));
12382
12383 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12384 dwp_file->elf_sections =
12385 OBSTACK_CALLOC (&objfile->objfile_obstack,
12386 dwp_file->num_sections, asection *);
12387
12388 bfd_map_over_sections (dwp_file->dbfd.get (),
12389 dwarf2_locate_common_dwp_sections,
12390 dwp_file.get ());
12391
12392 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12393 0);
12394
12395 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12396 1);
12397
12398 /* The DWP file version is stored in the hash table. Oh well. */
12399 if (dwp_file->cus && dwp_file->tus
12400 && dwp_file->cus->version != dwp_file->tus->version)
12401 {
12402 /* Technically speaking, we should try to limp along, but this is
12403 pretty bizarre. We use pulongest here because that's the established
12404 portability solution (e.g, we cannot use %u for uint32_t). */
12405 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12406 " TU version %s [in DWP file %s]"),
12407 pulongest (dwp_file->cus->version),
12408 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12409 }
12410
12411 if (dwp_file->cus)
12412 dwp_file->version = dwp_file->cus->version;
12413 else if (dwp_file->tus)
12414 dwp_file->version = dwp_file->tus->version;
12415 else
12416 dwp_file->version = 2;
12417
12418 if (dwp_file->version == 2)
12419 bfd_map_over_sections (dwp_file->dbfd.get (),
12420 dwarf2_locate_v2_dwp_sections,
12421 dwp_file.get ());
12422
12423 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12424 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12425
12426 if (dwarf_read_debug)
12427 {
12428 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12429 fprintf_unfiltered (gdb_stdlog,
12430 " %s CUs, %s TUs\n",
12431 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12432 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12433 }
12434
12435 return dwp_file;
12436 }
12437
12438 /* Wrapper around open_and_init_dwp_file, only open it once. */
12439
12440 static struct dwp_file *
12441 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12442 {
12443 if (! dwarf2_per_objfile->dwp_checked)
12444 {
12445 dwarf2_per_objfile->dwp_file
12446 = open_and_init_dwp_file (dwarf2_per_objfile);
12447 dwarf2_per_objfile->dwp_checked = 1;
12448 }
12449 return dwarf2_per_objfile->dwp_file.get ();
12450 }
12451
12452 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12453 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12454 or in the DWP file for the objfile, referenced by THIS_UNIT.
12455 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12456 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12457
12458 This is called, for example, when wanting to read a variable with a
12459 complex location. Therefore we don't want to do file i/o for every call.
12460 Therefore we don't want to look for a DWO file on every call.
12461 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12462 then we check if we've already seen DWO_NAME, and only THEN do we check
12463 for a DWO file.
12464
12465 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12466 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12467
12468 static struct dwo_unit *
12469 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12470 const char *dwo_name, const char *comp_dir,
12471 ULONGEST signature, int is_debug_types)
12472 {
12473 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12474 struct objfile *objfile = dwarf2_per_objfile->objfile;
12475 const char *kind = is_debug_types ? "TU" : "CU";
12476 void **dwo_file_slot;
12477 struct dwo_file *dwo_file;
12478 struct dwp_file *dwp_file;
12479
12480 /* First see if there's a DWP file.
12481 If we have a DWP file but didn't find the DWO inside it, don't
12482 look for the original DWO file. It makes gdb behave differently
12483 depending on whether one is debugging in the build tree. */
12484
12485 dwp_file = get_dwp_file (dwarf2_per_objfile);
12486 if (dwp_file != NULL)
12487 {
12488 const struct dwp_hash_table *dwp_htab =
12489 is_debug_types ? dwp_file->tus : dwp_file->cus;
12490
12491 if (dwp_htab != NULL)
12492 {
12493 struct dwo_unit *dwo_cutu =
12494 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12495 signature, is_debug_types);
12496
12497 if (dwo_cutu != NULL)
12498 {
12499 if (dwarf_read_debug)
12500 {
12501 fprintf_unfiltered (gdb_stdlog,
12502 "Virtual DWO %s %s found: @%s\n",
12503 kind, hex_string (signature),
12504 host_address_to_string (dwo_cutu));
12505 }
12506 return dwo_cutu;
12507 }
12508 }
12509 }
12510 else
12511 {
12512 /* No DWP file, look for the DWO file. */
12513
12514 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12515 dwo_name, comp_dir);
12516 if (*dwo_file_slot == NULL)
12517 {
12518 /* Read in the file and build a table of the CUs/TUs it contains. */
12519 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12520 }
12521 /* NOTE: This will be NULL if unable to open the file. */
12522 dwo_file = (struct dwo_file *) *dwo_file_slot;
12523
12524 if (dwo_file != NULL)
12525 {
12526 struct dwo_unit *dwo_cutu = NULL;
12527
12528 if (is_debug_types && dwo_file->tus)
12529 {
12530 struct dwo_unit find_dwo_cutu;
12531
12532 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12533 find_dwo_cutu.signature = signature;
12534 dwo_cutu
12535 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12536 &find_dwo_cutu);
12537 }
12538 else if (!is_debug_types && dwo_file->cus)
12539 {
12540 struct dwo_unit find_dwo_cutu;
12541
12542 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12543 find_dwo_cutu.signature = signature;
12544 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12545 &find_dwo_cutu);
12546 }
12547
12548 if (dwo_cutu != NULL)
12549 {
12550 if (dwarf_read_debug)
12551 {
12552 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12553 kind, dwo_name, hex_string (signature),
12554 host_address_to_string (dwo_cutu));
12555 }
12556 return dwo_cutu;
12557 }
12558 }
12559 }
12560
12561 /* We didn't find it. This could mean a dwo_id mismatch, or
12562 someone deleted the DWO/DWP file, or the search path isn't set up
12563 correctly to find the file. */
12564
12565 if (dwarf_read_debug)
12566 {
12567 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12568 kind, dwo_name, hex_string (signature));
12569 }
12570
12571 /* This is a warning and not a complaint because it can be caused by
12572 pilot error (e.g., user accidentally deleting the DWO). */
12573 {
12574 /* Print the name of the DWP file if we looked there, helps the user
12575 better diagnose the problem. */
12576 std::string dwp_text;
12577
12578 if (dwp_file != NULL)
12579 dwp_text = string_printf (" [in DWP file %s]",
12580 lbasename (dwp_file->name));
12581
12582 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12583 " [in module %s]"),
12584 kind, dwo_name, hex_string (signature),
12585 dwp_text.c_str (),
12586 this_unit->is_debug_types ? "TU" : "CU",
12587 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12588 }
12589 return NULL;
12590 }
12591
12592 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12593 See lookup_dwo_cutu_unit for details. */
12594
12595 static struct dwo_unit *
12596 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12597 const char *dwo_name, const char *comp_dir,
12598 ULONGEST signature)
12599 {
12600 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12601 }
12602
12603 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12604 See lookup_dwo_cutu_unit for details. */
12605
12606 static struct dwo_unit *
12607 lookup_dwo_type_unit (struct signatured_type *this_tu,
12608 const char *dwo_name, const char *comp_dir)
12609 {
12610 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12611 }
12612
12613 /* Traversal function for queue_and_load_all_dwo_tus. */
12614
12615 static int
12616 queue_and_load_dwo_tu (void **slot, void *info)
12617 {
12618 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12619 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12620 ULONGEST signature = dwo_unit->signature;
12621 struct signatured_type *sig_type =
12622 lookup_dwo_signatured_type (per_cu->cu, signature);
12623
12624 if (sig_type != NULL)
12625 {
12626 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12627
12628 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12629 a real dependency of PER_CU on SIG_TYPE. That is detected later
12630 while processing PER_CU. */
12631 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12632 load_full_type_unit (sig_cu);
12633 per_cu->imported_symtabs_push (sig_cu);
12634 }
12635
12636 return 1;
12637 }
12638
12639 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12640 The DWO may have the only definition of the type, though it may not be
12641 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12642 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12643
12644 static void
12645 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12646 {
12647 struct dwo_unit *dwo_unit;
12648 struct dwo_file *dwo_file;
12649
12650 gdb_assert (!per_cu->is_debug_types);
12651 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12652 gdb_assert (per_cu->cu != NULL);
12653
12654 dwo_unit = per_cu->cu->dwo_unit;
12655 gdb_assert (dwo_unit != NULL);
12656
12657 dwo_file = dwo_unit->dwo_file;
12658 if (dwo_file->tus != NULL)
12659 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12660 per_cu);
12661 }
12662
12663 /* Read in various DIEs. */
12664
12665 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12666 Inherit only the children of the DW_AT_abstract_origin DIE not being
12667 already referenced by DW_AT_abstract_origin from the children of the
12668 current DIE. */
12669
12670 static void
12671 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12672 {
12673 struct die_info *child_die;
12674 sect_offset *offsetp;
12675 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12676 struct die_info *origin_die;
12677 /* Iterator of the ORIGIN_DIE children. */
12678 struct die_info *origin_child_die;
12679 struct attribute *attr;
12680 struct dwarf2_cu *origin_cu;
12681 struct pending **origin_previous_list_in_scope;
12682
12683 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12684 if (!attr)
12685 return;
12686
12687 /* Note that following die references may follow to a die in a
12688 different cu. */
12689
12690 origin_cu = cu;
12691 origin_die = follow_die_ref (die, attr, &origin_cu);
12692
12693 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12694 symbols in. */
12695 origin_previous_list_in_scope = origin_cu->list_in_scope;
12696 origin_cu->list_in_scope = cu->list_in_scope;
12697
12698 if (die->tag != origin_die->tag
12699 && !(die->tag == DW_TAG_inlined_subroutine
12700 && origin_die->tag == DW_TAG_subprogram))
12701 complaint (_("DIE %s and its abstract origin %s have different tags"),
12702 sect_offset_str (die->sect_off),
12703 sect_offset_str (origin_die->sect_off));
12704
12705 std::vector<sect_offset> offsets;
12706
12707 for (child_die = die->child;
12708 child_die && child_die->tag;
12709 child_die = child_die->sibling)
12710 {
12711 struct die_info *child_origin_die;
12712 struct dwarf2_cu *child_origin_cu;
12713
12714 /* We are trying to process concrete instance entries:
12715 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12716 it's not relevant to our analysis here. i.e. detecting DIEs that are
12717 present in the abstract instance but not referenced in the concrete
12718 one. */
12719 if (child_die->tag == DW_TAG_call_site
12720 || child_die->tag == DW_TAG_GNU_call_site)
12721 continue;
12722
12723 /* For each CHILD_DIE, find the corresponding child of
12724 ORIGIN_DIE. If there is more than one layer of
12725 DW_AT_abstract_origin, follow them all; there shouldn't be,
12726 but GCC versions at least through 4.4 generate this (GCC PR
12727 40573). */
12728 child_origin_die = child_die;
12729 child_origin_cu = cu;
12730 while (1)
12731 {
12732 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12733 child_origin_cu);
12734 if (attr == NULL)
12735 break;
12736 child_origin_die = follow_die_ref (child_origin_die, attr,
12737 &child_origin_cu);
12738 }
12739
12740 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12741 counterpart may exist. */
12742 if (child_origin_die != child_die)
12743 {
12744 if (child_die->tag != child_origin_die->tag
12745 && !(child_die->tag == DW_TAG_inlined_subroutine
12746 && child_origin_die->tag == DW_TAG_subprogram))
12747 complaint (_("Child DIE %s and its abstract origin %s have "
12748 "different tags"),
12749 sect_offset_str (child_die->sect_off),
12750 sect_offset_str (child_origin_die->sect_off));
12751 if (child_origin_die->parent != origin_die)
12752 complaint (_("Child DIE %s and its abstract origin %s have "
12753 "different parents"),
12754 sect_offset_str (child_die->sect_off),
12755 sect_offset_str (child_origin_die->sect_off));
12756 else
12757 offsets.push_back (child_origin_die->sect_off);
12758 }
12759 }
12760 std::sort (offsets.begin (), offsets.end ());
12761 sect_offset *offsets_end = offsets.data () + offsets.size ();
12762 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12763 if (offsetp[-1] == *offsetp)
12764 complaint (_("Multiple children of DIE %s refer "
12765 "to DIE %s as their abstract origin"),
12766 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12767
12768 offsetp = offsets.data ();
12769 origin_child_die = origin_die->child;
12770 while (origin_child_die && origin_child_die->tag)
12771 {
12772 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12773 while (offsetp < offsets_end
12774 && *offsetp < origin_child_die->sect_off)
12775 offsetp++;
12776 if (offsetp >= offsets_end
12777 || *offsetp > origin_child_die->sect_off)
12778 {
12779 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12780 Check whether we're already processing ORIGIN_CHILD_DIE.
12781 This can happen with mutually referenced abstract_origins.
12782 PR 16581. */
12783 if (!origin_child_die->in_process)
12784 process_die (origin_child_die, origin_cu);
12785 }
12786 origin_child_die = origin_child_die->sibling;
12787 }
12788 origin_cu->list_in_scope = origin_previous_list_in_scope;
12789
12790 if (cu != origin_cu)
12791 compute_delayed_physnames (origin_cu);
12792 }
12793
12794 static void
12795 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12796 {
12797 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12798 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12799 struct context_stack *newobj;
12800 CORE_ADDR lowpc;
12801 CORE_ADDR highpc;
12802 struct die_info *child_die;
12803 struct attribute *attr, *call_line, *call_file;
12804 const char *name;
12805 CORE_ADDR baseaddr;
12806 struct block *block;
12807 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12808 std::vector<struct symbol *> template_args;
12809 struct template_symbol *templ_func = NULL;
12810
12811 if (inlined_func)
12812 {
12813 /* If we do not have call site information, we can't show the
12814 caller of this inlined function. That's too confusing, so
12815 only use the scope for local variables. */
12816 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12817 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12818 if (call_line == NULL || call_file == NULL)
12819 {
12820 read_lexical_block_scope (die, cu);
12821 return;
12822 }
12823 }
12824
12825 baseaddr = objfile->text_section_offset ();
12826
12827 name = dwarf2_name (die, cu);
12828
12829 /* Ignore functions with missing or empty names. These are actually
12830 illegal according to the DWARF standard. */
12831 if (name == NULL)
12832 {
12833 complaint (_("missing name for subprogram DIE at %s"),
12834 sect_offset_str (die->sect_off));
12835 return;
12836 }
12837
12838 /* Ignore functions with missing or invalid low and high pc attributes. */
12839 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12840 <= PC_BOUNDS_INVALID)
12841 {
12842 attr = dwarf2_attr (die, DW_AT_external, cu);
12843 if (!attr || !DW_UNSND (attr))
12844 complaint (_("cannot get low and high bounds "
12845 "for subprogram DIE at %s"),
12846 sect_offset_str (die->sect_off));
12847 return;
12848 }
12849
12850 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12851 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12852
12853 /* If we have any template arguments, then we must allocate a
12854 different sort of symbol. */
12855 for (child_die = die->child; child_die; child_die = child_die->sibling)
12856 {
12857 if (child_die->tag == DW_TAG_template_type_param
12858 || child_die->tag == DW_TAG_template_value_param)
12859 {
12860 templ_func = allocate_template_symbol (objfile);
12861 templ_func->subclass = SYMBOL_TEMPLATE;
12862 break;
12863 }
12864 }
12865
12866 newobj = cu->get_builder ()->push_context (0, lowpc);
12867 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12868 (struct symbol *) templ_func);
12869
12870 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12871 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12872 cu->language);
12873
12874 /* If there is a location expression for DW_AT_frame_base, record
12875 it. */
12876 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12877 if (attr != nullptr)
12878 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12879
12880 /* If there is a location for the static link, record it. */
12881 newobj->static_link = NULL;
12882 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12883 if (attr != nullptr)
12884 {
12885 newobj->static_link
12886 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12887 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12888 cu->per_cu->addr_type ());
12889 }
12890
12891 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12892
12893 if (die->child != NULL)
12894 {
12895 child_die = die->child;
12896 while (child_die && child_die->tag)
12897 {
12898 if (child_die->tag == DW_TAG_template_type_param
12899 || child_die->tag == DW_TAG_template_value_param)
12900 {
12901 struct symbol *arg = new_symbol (child_die, NULL, cu);
12902
12903 if (arg != NULL)
12904 template_args.push_back (arg);
12905 }
12906 else
12907 process_die (child_die, cu);
12908 child_die = child_die->sibling;
12909 }
12910 }
12911
12912 inherit_abstract_dies (die, cu);
12913
12914 /* If we have a DW_AT_specification, we might need to import using
12915 directives from the context of the specification DIE. See the
12916 comment in determine_prefix. */
12917 if (cu->language == language_cplus
12918 && dwarf2_attr (die, DW_AT_specification, cu))
12919 {
12920 struct dwarf2_cu *spec_cu = cu;
12921 struct die_info *spec_die = die_specification (die, &spec_cu);
12922
12923 while (spec_die)
12924 {
12925 child_die = spec_die->child;
12926 while (child_die && child_die->tag)
12927 {
12928 if (child_die->tag == DW_TAG_imported_module)
12929 process_die (child_die, spec_cu);
12930 child_die = child_die->sibling;
12931 }
12932
12933 /* In some cases, GCC generates specification DIEs that
12934 themselves contain DW_AT_specification attributes. */
12935 spec_die = die_specification (spec_die, &spec_cu);
12936 }
12937 }
12938
12939 struct context_stack cstk = cu->get_builder ()->pop_context ();
12940 /* Make a block for the local symbols within. */
12941 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12942 cstk.static_link, lowpc, highpc);
12943
12944 /* For C++, set the block's scope. */
12945 if ((cu->language == language_cplus
12946 || cu->language == language_fortran
12947 || cu->language == language_d
12948 || cu->language == language_rust)
12949 && cu->processing_has_namespace_info)
12950 block_set_scope (block, determine_prefix (die, cu),
12951 &objfile->objfile_obstack);
12952
12953 /* If we have address ranges, record them. */
12954 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12955
12956 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12957
12958 /* Attach template arguments to function. */
12959 if (!template_args.empty ())
12960 {
12961 gdb_assert (templ_func != NULL);
12962
12963 templ_func->n_template_arguments = template_args.size ();
12964 templ_func->template_arguments
12965 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12966 templ_func->n_template_arguments);
12967 memcpy (templ_func->template_arguments,
12968 template_args.data (),
12969 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12970
12971 /* Make sure that the symtab is set on the new symbols. Even
12972 though they don't appear in this symtab directly, other parts
12973 of gdb assume that symbols do, and this is reasonably
12974 true. */
12975 for (symbol *sym : template_args)
12976 symbol_set_symtab (sym, symbol_symtab (templ_func));
12977 }
12978
12979 /* In C++, we can have functions nested inside functions (e.g., when
12980 a function declares a class that has methods). This means that
12981 when we finish processing a function scope, we may need to go
12982 back to building a containing block's symbol lists. */
12983 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12984 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12985
12986 /* If we've finished processing a top-level function, subsequent
12987 symbols go in the file symbol list. */
12988 if (cu->get_builder ()->outermost_context_p ())
12989 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
12990 }
12991
12992 /* Process all the DIES contained within a lexical block scope. Start
12993 a new scope, process the dies, and then close the scope. */
12994
12995 static void
12996 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
12997 {
12998 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12999 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13000 CORE_ADDR lowpc, highpc;
13001 struct die_info *child_die;
13002 CORE_ADDR baseaddr;
13003
13004 baseaddr = objfile->text_section_offset ();
13005
13006 /* Ignore blocks with missing or invalid low and high pc attributes. */
13007 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13008 as multiple lexical blocks? Handling children in a sane way would
13009 be nasty. Might be easier to properly extend generic blocks to
13010 describe ranges. */
13011 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13012 {
13013 case PC_BOUNDS_NOT_PRESENT:
13014 /* DW_TAG_lexical_block has no attributes, process its children as if
13015 there was no wrapping by that DW_TAG_lexical_block.
13016 GCC does no longer produces such DWARF since GCC r224161. */
13017 for (child_die = die->child;
13018 child_die != NULL && child_die->tag;
13019 child_die = child_die->sibling)
13020 process_die (child_die, cu);
13021 return;
13022 case PC_BOUNDS_INVALID:
13023 return;
13024 }
13025 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13026 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13027
13028 cu->get_builder ()->push_context (0, lowpc);
13029 if (die->child != NULL)
13030 {
13031 child_die = die->child;
13032 while (child_die && child_die->tag)
13033 {
13034 process_die (child_die, cu);
13035 child_die = child_die->sibling;
13036 }
13037 }
13038 inherit_abstract_dies (die, cu);
13039 struct context_stack cstk = cu->get_builder ()->pop_context ();
13040
13041 if (*cu->get_builder ()->get_local_symbols () != NULL
13042 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13043 {
13044 struct block *block
13045 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13046 cstk.start_addr, highpc);
13047
13048 /* Note that recording ranges after traversing children, as we
13049 do here, means that recording a parent's ranges entails
13050 walking across all its children's ranges as they appear in
13051 the address map, which is quadratic behavior.
13052
13053 It would be nicer to record the parent's ranges before
13054 traversing its children, simply overriding whatever you find
13055 there. But since we don't even decide whether to create a
13056 block until after we've traversed its children, that's hard
13057 to do. */
13058 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13059 }
13060 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13061 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13062 }
13063
13064 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13065
13066 static void
13067 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13068 {
13069 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13070 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13071 CORE_ADDR pc, baseaddr;
13072 struct attribute *attr;
13073 struct call_site *call_site, call_site_local;
13074 void **slot;
13075 int nparams;
13076 struct die_info *child_die;
13077
13078 baseaddr = objfile->text_section_offset ();
13079
13080 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13081 if (attr == NULL)
13082 {
13083 /* This was a pre-DWARF-5 GNU extension alias
13084 for DW_AT_call_return_pc. */
13085 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13086 }
13087 if (!attr)
13088 {
13089 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13090 "DIE %s [in module %s]"),
13091 sect_offset_str (die->sect_off), objfile_name (objfile));
13092 return;
13093 }
13094 pc = attr->value_as_address () + baseaddr;
13095 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13096
13097 if (cu->call_site_htab == NULL)
13098 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13099 NULL, &objfile->objfile_obstack,
13100 hashtab_obstack_allocate, NULL);
13101 call_site_local.pc = pc;
13102 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13103 if (*slot != NULL)
13104 {
13105 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13106 "DIE %s [in module %s]"),
13107 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13108 objfile_name (objfile));
13109 return;
13110 }
13111
13112 /* Count parameters at the caller. */
13113
13114 nparams = 0;
13115 for (child_die = die->child; child_die && child_die->tag;
13116 child_die = child_die->sibling)
13117 {
13118 if (child_die->tag != DW_TAG_call_site_parameter
13119 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13120 {
13121 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13122 "DW_TAG_call_site child DIE %s [in module %s]"),
13123 child_die->tag, sect_offset_str (child_die->sect_off),
13124 objfile_name (objfile));
13125 continue;
13126 }
13127
13128 nparams++;
13129 }
13130
13131 call_site
13132 = ((struct call_site *)
13133 obstack_alloc (&objfile->objfile_obstack,
13134 sizeof (*call_site)
13135 + (sizeof (*call_site->parameter) * (nparams - 1))));
13136 *slot = call_site;
13137 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13138 call_site->pc = pc;
13139
13140 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13141 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13142 {
13143 struct die_info *func_die;
13144
13145 /* Skip also over DW_TAG_inlined_subroutine. */
13146 for (func_die = die->parent;
13147 func_die && func_die->tag != DW_TAG_subprogram
13148 && func_die->tag != DW_TAG_subroutine_type;
13149 func_die = func_die->parent);
13150
13151 /* DW_AT_call_all_calls is a superset
13152 of DW_AT_call_all_tail_calls. */
13153 if (func_die
13154 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13155 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13156 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13157 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13158 {
13159 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13160 not complete. But keep CALL_SITE for look ups via call_site_htab,
13161 both the initial caller containing the real return address PC and
13162 the final callee containing the current PC of a chain of tail
13163 calls do not need to have the tail call list complete. But any
13164 function candidate for a virtual tail call frame searched via
13165 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13166 determined unambiguously. */
13167 }
13168 else
13169 {
13170 struct type *func_type = NULL;
13171
13172 if (func_die)
13173 func_type = get_die_type (func_die, cu);
13174 if (func_type != NULL)
13175 {
13176 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13177
13178 /* Enlist this call site to the function. */
13179 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13180 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13181 }
13182 else
13183 complaint (_("Cannot find function owning DW_TAG_call_site "
13184 "DIE %s [in module %s]"),
13185 sect_offset_str (die->sect_off), objfile_name (objfile));
13186 }
13187 }
13188
13189 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13190 if (attr == NULL)
13191 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13192 if (attr == NULL)
13193 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13194 if (attr == NULL)
13195 {
13196 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13197 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13198 }
13199 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13200 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13201 /* Keep NULL DWARF_BLOCK. */;
13202 else if (attr->form_is_block ())
13203 {
13204 struct dwarf2_locexpr_baton *dlbaton;
13205
13206 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13207 dlbaton->data = DW_BLOCK (attr)->data;
13208 dlbaton->size = DW_BLOCK (attr)->size;
13209 dlbaton->per_cu = cu->per_cu;
13210
13211 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13212 }
13213 else if (attr->form_is_ref ())
13214 {
13215 struct dwarf2_cu *target_cu = cu;
13216 struct die_info *target_die;
13217
13218 target_die = follow_die_ref (die, attr, &target_cu);
13219 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13220 if (die_is_declaration (target_die, target_cu))
13221 {
13222 const char *target_physname;
13223
13224 /* Prefer the mangled name; otherwise compute the demangled one. */
13225 target_physname = dw2_linkage_name (target_die, target_cu);
13226 if (target_physname == NULL)
13227 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13228 if (target_physname == NULL)
13229 complaint (_("DW_AT_call_target target DIE has invalid "
13230 "physname, for referencing DIE %s [in module %s]"),
13231 sect_offset_str (die->sect_off), objfile_name (objfile));
13232 else
13233 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13234 }
13235 else
13236 {
13237 CORE_ADDR lowpc;
13238
13239 /* DW_AT_entry_pc should be preferred. */
13240 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13241 <= PC_BOUNDS_INVALID)
13242 complaint (_("DW_AT_call_target target DIE has invalid "
13243 "low pc, for referencing DIE %s [in module %s]"),
13244 sect_offset_str (die->sect_off), objfile_name (objfile));
13245 else
13246 {
13247 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13248 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13249 }
13250 }
13251 }
13252 else
13253 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13254 "block nor reference, for DIE %s [in module %s]"),
13255 sect_offset_str (die->sect_off), objfile_name (objfile));
13256
13257 call_site->per_cu = cu->per_cu;
13258
13259 for (child_die = die->child;
13260 child_die && child_die->tag;
13261 child_die = child_die->sibling)
13262 {
13263 struct call_site_parameter *parameter;
13264 struct attribute *loc, *origin;
13265
13266 if (child_die->tag != DW_TAG_call_site_parameter
13267 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13268 {
13269 /* Already printed the complaint above. */
13270 continue;
13271 }
13272
13273 gdb_assert (call_site->parameter_count < nparams);
13274 parameter = &call_site->parameter[call_site->parameter_count];
13275
13276 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13277 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13278 register is contained in DW_AT_call_value. */
13279
13280 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13281 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13282 if (origin == NULL)
13283 {
13284 /* This was a pre-DWARF-5 GNU extension alias
13285 for DW_AT_call_parameter. */
13286 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13287 }
13288 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13289 {
13290 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13291
13292 sect_offset sect_off = origin->get_ref_die_offset ();
13293 if (!cu->header.offset_in_cu_p (sect_off))
13294 {
13295 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13296 binding can be done only inside one CU. Such referenced DIE
13297 therefore cannot be even moved to DW_TAG_partial_unit. */
13298 complaint (_("DW_AT_call_parameter offset is not in CU for "
13299 "DW_TAG_call_site child DIE %s [in module %s]"),
13300 sect_offset_str (child_die->sect_off),
13301 objfile_name (objfile));
13302 continue;
13303 }
13304 parameter->u.param_cu_off
13305 = (cu_offset) (sect_off - cu->header.sect_off);
13306 }
13307 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13308 {
13309 complaint (_("No DW_FORM_block* DW_AT_location for "
13310 "DW_TAG_call_site child DIE %s [in module %s]"),
13311 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13312 continue;
13313 }
13314 else
13315 {
13316 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13317 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13318 if (parameter->u.dwarf_reg != -1)
13319 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13320 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13321 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13322 &parameter->u.fb_offset))
13323 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13324 else
13325 {
13326 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13327 "for DW_FORM_block* DW_AT_location is supported for "
13328 "DW_TAG_call_site child DIE %s "
13329 "[in module %s]"),
13330 sect_offset_str (child_die->sect_off),
13331 objfile_name (objfile));
13332 continue;
13333 }
13334 }
13335
13336 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13337 if (attr == NULL)
13338 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13339 if (attr == NULL || !attr->form_is_block ())
13340 {
13341 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13342 "DW_TAG_call_site child DIE %s [in module %s]"),
13343 sect_offset_str (child_die->sect_off),
13344 objfile_name (objfile));
13345 continue;
13346 }
13347 parameter->value = DW_BLOCK (attr)->data;
13348 parameter->value_size = DW_BLOCK (attr)->size;
13349
13350 /* Parameters are not pre-cleared by memset above. */
13351 parameter->data_value = NULL;
13352 parameter->data_value_size = 0;
13353 call_site->parameter_count++;
13354
13355 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13356 if (attr == NULL)
13357 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13358 if (attr != nullptr)
13359 {
13360 if (!attr->form_is_block ())
13361 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13362 "DW_TAG_call_site child DIE %s [in module %s]"),
13363 sect_offset_str (child_die->sect_off),
13364 objfile_name (objfile));
13365 else
13366 {
13367 parameter->data_value = DW_BLOCK (attr)->data;
13368 parameter->data_value_size = DW_BLOCK (attr)->size;
13369 }
13370 }
13371 }
13372 }
13373
13374 /* Helper function for read_variable. If DIE represents a virtual
13375 table, then return the type of the concrete object that is
13376 associated with the virtual table. Otherwise, return NULL. */
13377
13378 static struct type *
13379 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13380 {
13381 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13382 if (attr == NULL)
13383 return NULL;
13384
13385 /* Find the type DIE. */
13386 struct die_info *type_die = NULL;
13387 struct dwarf2_cu *type_cu = cu;
13388
13389 if (attr->form_is_ref ())
13390 type_die = follow_die_ref (die, attr, &type_cu);
13391 if (type_die == NULL)
13392 return NULL;
13393
13394 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13395 return NULL;
13396 return die_containing_type (type_die, type_cu);
13397 }
13398
13399 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13400
13401 static void
13402 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13403 {
13404 struct rust_vtable_symbol *storage = NULL;
13405
13406 if (cu->language == language_rust)
13407 {
13408 struct type *containing_type = rust_containing_type (die, cu);
13409
13410 if (containing_type != NULL)
13411 {
13412 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13413
13414 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13415 initialize_objfile_symbol (storage);
13416 storage->concrete_type = containing_type;
13417 storage->subclass = SYMBOL_RUST_VTABLE;
13418 }
13419 }
13420
13421 struct symbol *res = new_symbol (die, NULL, cu, storage);
13422 struct attribute *abstract_origin
13423 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13424 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13425 if (res == NULL && loc && abstract_origin)
13426 {
13427 /* We have a variable without a name, but with a location and an abstract
13428 origin. This may be a concrete instance of an abstract variable
13429 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13430 later. */
13431 struct dwarf2_cu *origin_cu = cu;
13432 struct die_info *origin_die
13433 = follow_die_ref (die, abstract_origin, &origin_cu);
13434 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13435 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13436 }
13437 }
13438
13439 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13440 reading .debug_rnglists.
13441 Callback's type should be:
13442 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13443 Return true if the attributes are present and valid, otherwise,
13444 return false. */
13445
13446 template <typename Callback>
13447 static bool
13448 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13449 Callback &&callback)
13450 {
13451 struct dwarf2_per_objfile *dwarf2_per_objfile
13452 = cu->per_cu->dwarf2_per_objfile;
13453 struct objfile *objfile = dwarf2_per_objfile->objfile;
13454 bfd *obfd = objfile->obfd;
13455 /* Base address selection entry. */
13456 gdb::optional<CORE_ADDR> base;
13457 const gdb_byte *buffer;
13458 CORE_ADDR baseaddr;
13459 bool overflow = false;
13460
13461 base = cu->base_address;
13462
13463 dwarf2_per_objfile->rnglists.read (objfile);
13464 if (offset >= dwarf2_per_objfile->rnglists.size)
13465 {
13466 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13467 offset);
13468 return false;
13469 }
13470 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13471
13472 baseaddr = objfile->text_section_offset ();
13473
13474 while (1)
13475 {
13476 /* Initialize it due to a false compiler warning. */
13477 CORE_ADDR range_beginning = 0, range_end = 0;
13478 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13479 + dwarf2_per_objfile->rnglists.size);
13480 unsigned int bytes_read;
13481
13482 if (buffer == buf_end)
13483 {
13484 overflow = true;
13485 break;
13486 }
13487 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13488 switch (rlet)
13489 {
13490 case DW_RLE_end_of_list:
13491 break;
13492 case DW_RLE_base_address:
13493 if (buffer + cu->header.addr_size > buf_end)
13494 {
13495 overflow = true;
13496 break;
13497 }
13498 base = cu->header.read_address (obfd, buffer, &bytes_read);
13499 buffer += bytes_read;
13500 break;
13501 case DW_RLE_start_length:
13502 if (buffer + cu->header.addr_size > buf_end)
13503 {
13504 overflow = true;
13505 break;
13506 }
13507 range_beginning = cu->header.read_address (obfd, buffer,
13508 &bytes_read);
13509 buffer += bytes_read;
13510 range_end = (range_beginning
13511 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13512 buffer += bytes_read;
13513 if (buffer > buf_end)
13514 {
13515 overflow = true;
13516 break;
13517 }
13518 break;
13519 case DW_RLE_offset_pair:
13520 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13521 buffer += bytes_read;
13522 if (buffer > buf_end)
13523 {
13524 overflow = true;
13525 break;
13526 }
13527 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13528 buffer += bytes_read;
13529 if (buffer > buf_end)
13530 {
13531 overflow = true;
13532 break;
13533 }
13534 break;
13535 case DW_RLE_start_end:
13536 if (buffer + 2 * cu->header.addr_size > buf_end)
13537 {
13538 overflow = true;
13539 break;
13540 }
13541 range_beginning = cu->header.read_address (obfd, buffer,
13542 &bytes_read);
13543 buffer += bytes_read;
13544 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13545 buffer += bytes_read;
13546 break;
13547 default:
13548 complaint (_("Invalid .debug_rnglists data (no base address)"));
13549 return false;
13550 }
13551 if (rlet == DW_RLE_end_of_list || overflow)
13552 break;
13553 if (rlet == DW_RLE_base_address)
13554 continue;
13555
13556 if (!base.has_value ())
13557 {
13558 /* We have no valid base address for the ranges
13559 data. */
13560 complaint (_("Invalid .debug_rnglists data (no base address)"));
13561 return false;
13562 }
13563
13564 if (range_beginning > range_end)
13565 {
13566 /* Inverted range entries are invalid. */
13567 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13568 return false;
13569 }
13570
13571 /* Empty range entries have no effect. */
13572 if (range_beginning == range_end)
13573 continue;
13574
13575 range_beginning += *base;
13576 range_end += *base;
13577
13578 /* A not-uncommon case of bad debug info.
13579 Don't pollute the addrmap with bad data. */
13580 if (range_beginning + baseaddr == 0
13581 && !dwarf2_per_objfile->has_section_at_zero)
13582 {
13583 complaint (_(".debug_rnglists entry has start address of zero"
13584 " [in module %s]"), objfile_name (objfile));
13585 continue;
13586 }
13587
13588 callback (range_beginning, range_end);
13589 }
13590
13591 if (overflow)
13592 {
13593 complaint (_("Offset %d is not terminated "
13594 "for DW_AT_ranges attribute"),
13595 offset);
13596 return false;
13597 }
13598
13599 return true;
13600 }
13601
13602 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13603 Callback's type should be:
13604 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13605 Return 1 if the attributes are present and valid, otherwise, return 0. */
13606
13607 template <typename Callback>
13608 static int
13609 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13610 Callback &&callback)
13611 {
13612 struct dwarf2_per_objfile *dwarf2_per_objfile
13613 = cu->per_cu->dwarf2_per_objfile;
13614 struct objfile *objfile = dwarf2_per_objfile->objfile;
13615 struct comp_unit_head *cu_header = &cu->header;
13616 bfd *obfd = objfile->obfd;
13617 unsigned int addr_size = cu_header->addr_size;
13618 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13619 /* Base address selection entry. */
13620 gdb::optional<CORE_ADDR> base;
13621 unsigned int dummy;
13622 const gdb_byte *buffer;
13623 CORE_ADDR baseaddr;
13624
13625 if (cu_header->version >= 5)
13626 return dwarf2_rnglists_process (offset, cu, callback);
13627
13628 base = cu->base_address;
13629
13630 dwarf2_per_objfile->ranges.read (objfile);
13631 if (offset >= dwarf2_per_objfile->ranges.size)
13632 {
13633 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13634 offset);
13635 return 0;
13636 }
13637 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13638
13639 baseaddr = objfile->text_section_offset ();
13640
13641 while (1)
13642 {
13643 CORE_ADDR range_beginning, range_end;
13644
13645 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13646 buffer += addr_size;
13647 range_end = cu->header.read_address (obfd, buffer, &dummy);
13648 buffer += addr_size;
13649 offset += 2 * addr_size;
13650
13651 /* An end of list marker is a pair of zero addresses. */
13652 if (range_beginning == 0 && range_end == 0)
13653 /* Found the end of list entry. */
13654 break;
13655
13656 /* Each base address selection entry is a pair of 2 values.
13657 The first is the largest possible address, the second is
13658 the base address. Check for a base address here. */
13659 if ((range_beginning & mask) == mask)
13660 {
13661 /* If we found the largest possible address, then we already
13662 have the base address in range_end. */
13663 base = range_end;
13664 continue;
13665 }
13666
13667 if (!base.has_value ())
13668 {
13669 /* We have no valid base address for the ranges
13670 data. */
13671 complaint (_("Invalid .debug_ranges data (no base address)"));
13672 return 0;
13673 }
13674
13675 if (range_beginning > range_end)
13676 {
13677 /* Inverted range entries are invalid. */
13678 complaint (_("Invalid .debug_ranges data (inverted range)"));
13679 return 0;
13680 }
13681
13682 /* Empty range entries have no effect. */
13683 if (range_beginning == range_end)
13684 continue;
13685
13686 range_beginning += *base;
13687 range_end += *base;
13688
13689 /* A not-uncommon case of bad debug info.
13690 Don't pollute the addrmap with bad data. */
13691 if (range_beginning + baseaddr == 0
13692 && !dwarf2_per_objfile->has_section_at_zero)
13693 {
13694 complaint (_(".debug_ranges entry has start address of zero"
13695 " [in module %s]"), objfile_name (objfile));
13696 continue;
13697 }
13698
13699 callback (range_beginning, range_end);
13700 }
13701
13702 return 1;
13703 }
13704
13705 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13706 Return 1 if the attributes are present and valid, otherwise, return 0.
13707 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13708
13709 static int
13710 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13711 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13712 dwarf2_psymtab *ranges_pst)
13713 {
13714 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13715 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13716 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13717 int low_set = 0;
13718 CORE_ADDR low = 0;
13719 CORE_ADDR high = 0;
13720 int retval;
13721
13722 retval = dwarf2_ranges_process (offset, cu,
13723 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13724 {
13725 if (ranges_pst != NULL)
13726 {
13727 CORE_ADDR lowpc;
13728 CORE_ADDR highpc;
13729
13730 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13731 range_beginning + baseaddr)
13732 - baseaddr);
13733 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13734 range_end + baseaddr)
13735 - baseaddr);
13736 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13737 lowpc, highpc - 1, ranges_pst);
13738 }
13739
13740 /* FIXME: This is recording everything as a low-high
13741 segment of consecutive addresses. We should have a
13742 data structure for discontiguous block ranges
13743 instead. */
13744 if (! low_set)
13745 {
13746 low = range_beginning;
13747 high = range_end;
13748 low_set = 1;
13749 }
13750 else
13751 {
13752 if (range_beginning < low)
13753 low = range_beginning;
13754 if (range_end > high)
13755 high = range_end;
13756 }
13757 });
13758 if (!retval)
13759 return 0;
13760
13761 if (! low_set)
13762 /* If the first entry is an end-of-list marker, the range
13763 describes an empty scope, i.e. no instructions. */
13764 return 0;
13765
13766 if (low_return)
13767 *low_return = low;
13768 if (high_return)
13769 *high_return = high;
13770 return 1;
13771 }
13772
13773 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13774 definition for the return value. *LOWPC and *HIGHPC are set iff
13775 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13776
13777 static enum pc_bounds_kind
13778 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13779 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13780 dwarf2_psymtab *pst)
13781 {
13782 struct dwarf2_per_objfile *dwarf2_per_objfile
13783 = cu->per_cu->dwarf2_per_objfile;
13784 struct attribute *attr;
13785 struct attribute *attr_high;
13786 CORE_ADDR low = 0;
13787 CORE_ADDR high = 0;
13788 enum pc_bounds_kind ret;
13789
13790 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13791 if (attr_high)
13792 {
13793 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13794 if (attr != nullptr)
13795 {
13796 low = attr->value_as_address ();
13797 high = attr_high->value_as_address ();
13798 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13799 high += low;
13800 }
13801 else
13802 /* Found high w/o low attribute. */
13803 return PC_BOUNDS_INVALID;
13804
13805 /* Found consecutive range of addresses. */
13806 ret = PC_BOUNDS_HIGH_LOW;
13807 }
13808 else
13809 {
13810 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13811 if (attr != NULL)
13812 {
13813 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13814 We take advantage of the fact that DW_AT_ranges does not appear
13815 in DW_TAG_compile_unit of DWO files. */
13816 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13817 unsigned int ranges_offset = (DW_UNSND (attr)
13818 + (need_ranges_base
13819 ? cu->ranges_base
13820 : 0));
13821
13822 /* Value of the DW_AT_ranges attribute is the offset in the
13823 .debug_ranges section. */
13824 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13825 return PC_BOUNDS_INVALID;
13826 /* Found discontinuous range of addresses. */
13827 ret = PC_BOUNDS_RANGES;
13828 }
13829 else
13830 return PC_BOUNDS_NOT_PRESENT;
13831 }
13832
13833 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13834 if (high <= low)
13835 return PC_BOUNDS_INVALID;
13836
13837 /* When using the GNU linker, .gnu.linkonce. sections are used to
13838 eliminate duplicate copies of functions and vtables and such.
13839 The linker will arbitrarily choose one and discard the others.
13840 The AT_*_pc values for such functions refer to local labels in
13841 these sections. If the section from that file was discarded, the
13842 labels are not in the output, so the relocs get a value of 0.
13843 If this is a discarded function, mark the pc bounds as invalid,
13844 so that GDB will ignore it. */
13845 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13846 return PC_BOUNDS_INVALID;
13847
13848 *lowpc = low;
13849 if (highpc)
13850 *highpc = high;
13851 return ret;
13852 }
13853
13854 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13855 its low and high PC addresses. Do nothing if these addresses could not
13856 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13857 and HIGHPC to the high address if greater than HIGHPC. */
13858
13859 static void
13860 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13861 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13862 struct dwarf2_cu *cu)
13863 {
13864 CORE_ADDR low, high;
13865 struct die_info *child = die->child;
13866
13867 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13868 {
13869 *lowpc = std::min (*lowpc, low);
13870 *highpc = std::max (*highpc, high);
13871 }
13872
13873 /* If the language does not allow nested subprograms (either inside
13874 subprograms or lexical blocks), we're done. */
13875 if (cu->language != language_ada)
13876 return;
13877
13878 /* Check all the children of the given DIE. If it contains nested
13879 subprograms, then check their pc bounds. Likewise, we need to
13880 check lexical blocks as well, as they may also contain subprogram
13881 definitions. */
13882 while (child && child->tag)
13883 {
13884 if (child->tag == DW_TAG_subprogram
13885 || child->tag == DW_TAG_lexical_block)
13886 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13887 child = child->sibling;
13888 }
13889 }
13890
13891 /* Get the low and high pc's represented by the scope DIE, and store
13892 them in *LOWPC and *HIGHPC. If the correct values can't be
13893 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13894
13895 static void
13896 get_scope_pc_bounds (struct die_info *die,
13897 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13898 struct dwarf2_cu *cu)
13899 {
13900 CORE_ADDR best_low = (CORE_ADDR) -1;
13901 CORE_ADDR best_high = (CORE_ADDR) 0;
13902 CORE_ADDR current_low, current_high;
13903
13904 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13905 >= PC_BOUNDS_RANGES)
13906 {
13907 best_low = current_low;
13908 best_high = current_high;
13909 }
13910 else
13911 {
13912 struct die_info *child = die->child;
13913
13914 while (child && child->tag)
13915 {
13916 switch (child->tag) {
13917 case DW_TAG_subprogram:
13918 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13919 break;
13920 case DW_TAG_namespace:
13921 case DW_TAG_module:
13922 /* FIXME: carlton/2004-01-16: Should we do this for
13923 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13924 that current GCC's always emit the DIEs corresponding
13925 to definitions of methods of classes as children of a
13926 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13927 the DIEs giving the declarations, which could be
13928 anywhere). But I don't see any reason why the
13929 standards says that they have to be there. */
13930 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13931
13932 if (current_low != ((CORE_ADDR) -1))
13933 {
13934 best_low = std::min (best_low, current_low);
13935 best_high = std::max (best_high, current_high);
13936 }
13937 break;
13938 default:
13939 /* Ignore. */
13940 break;
13941 }
13942
13943 child = child->sibling;
13944 }
13945 }
13946
13947 *lowpc = best_low;
13948 *highpc = best_high;
13949 }
13950
13951 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13952 in DIE. */
13953
13954 static void
13955 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13956 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13957 {
13958 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13959 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13960 struct attribute *attr;
13961 struct attribute *attr_high;
13962
13963 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13964 if (attr_high)
13965 {
13966 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13967 if (attr != nullptr)
13968 {
13969 CORE_ADDR low = attr->value_as_address ();
13970 CORE_ADDR high = attr_high->value_as_address ();
13971
13972 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13973 high += low;
13974
13975 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13976 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13977 cu->get_builder ()->record_block_range (block, low, high - 1);
13978 }
13979 }
13980
13981 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13982 if (attr != nullptr)
13983 {
13984 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13985 We take advantage of the fact that DW_AT_ranges does not appear
13986 in DW_TAG_compile_unit of DWO files. */
13987 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13988
13989 /* The value of the DW_AT_ranges attribute is the offset of the
13990 address range list in the .debug_ranges section. */
13991 unsigned long offset = (DW_UNSND (attr)
13992 + (need_ranges_base ? cu->ranges_base : 0));
13993
13994 std::vector<blockrange> blockvec;
13995 dwarf2_ranges_process (offset, cu,
13996 [&] (CORE_ADDR start, CORE_ADDR end)
13997 {
13998 start += baseaddr;
13999 end += baseaddr;
14000 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14001 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14002 cu->get_builder ()->record_block_range (block, start, end - 1);
14003 blockvec.emplace_back (start, end);
14004 });
14005
14006 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14007 }
14008 }
14009
14010 /* Check whether the producer field indicates either of GCC < 4.6, or the
14011 Intel C/C++ compiler, and cache the result in CU. */
14012
14013 static void
14014 check_producer (struct dwarf2_cu *cu)
14015 {
14016 int major, minor;
14017
14018 if (cu->producer == NULL)
14019 {
14020 /* For unknown compilers expect their behavior is DWARF version
14021 compliant.
14022
14023 GCC started to support .debug_types sections by -gdwarf-4 since
14024 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14025 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14026 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14027 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14028 }
14029 else if (producer_is_gcc (cu->producer, &major, &minor))
14030 {
14031 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14032 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14033 }
14034 else if (producer_is_icc (cu->producer, &major, &minor))
14035 {
14036 cu->producer_is_icc = true;
14037 cu->producer_is_icc_lt_14 = major < 14;
14038 }
14039 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14040 cu->producer_is_codewarrior = true;
14041 else
14042 {
14043 /* For other non-GCC compilers, expect their behavior is DWARF version
14044 compliant. */
14045 }
14046
14047 cu->checked_producer = true;
14048 }
14049
14050 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14051 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14052 during 4.6.0 experimental. */
14053
14054 static bool
14055 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14056 {
14057 if (!cu->checked_producer)
14058 check_producer (cu);
14059
14060 return cu->producer_is_gxx_lt_4_6;
14061 }
14062
14063
14064 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14065 with incorrect is_stmt attributes. */
14066
14067 static bool
14068 producer_is_codewarrior (struct dwarf2_cu *cu)
14069 {
14070 if (!cu->checked_producer)
14071 check_producer (cu);
14072
14073 return cu->producer_is_codewarrior;
14074 }
14075
14076 /* Return the default accessibility type if it is not overridden by
14077 DW_AT_accessibility. */
14078
14079 static enum dwarf_access_attribute
14080 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14081 {
14082 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14083 {
14084 /* The default DWARF 2 accessibility for members is public, the default
14085 accessibility for inheritance is private. */
14086
14087 if (die->tag != DW_TAG_inheritance)
14088 return DW_ACCESS_public;
14089 else
14090 return DW_ACCESS_private;
14091 }
14092 else
14093 {
14094 /* DWARF 3+ defines the default accessibility a different way. The same
14095 rules apply now for DW_TAG_inheritance as for the members and it only
14096 depends on the container kind. */
14097
14098 if (die->parent->tag == DW_TAG_class_type)
14099 return DW_ACCESS_private;
14100 else
14101 return DW_ACCESS_public;
14102 }
14103 }
14104
14105 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14106 offset. If the attribute was not found return 0, otherwise return
14107 1. If it was found but could not properly be handled, set *OFFSET
14108 to 0. */
14109
14110 static int
14111 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14112 LONGEST *offset)
14113 {
14114 struct attribute *attr;
14115
14116 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14117 if (attr != NULL)
14118 {
14119 *offset = 0;
14120
14121 /* Note that we do not check for a section offset first here.
14122 This is because DW_AT_data_member_location is new in DWARF 4,
14123 so if we see it, we can assume that a constant form is really
14124 a constant and not a section offset. */
14125 if (attr->form_is_constant ())
14126 *offset = attr->constant_value (0);
14127 else if (attr->form_is_section_offset ())
14128 dwarf2_complex_location_expr_complaint ();
14129 else if (attr->form_is_block ())
14130 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14131 else
14132 dwarf2_complex_location_expr_complaint ();
14133
14134 return 1;
14135 }
14136
14137 return 0;
14138 }
14139
14140 /* Add an aggregate field to the field list. */
14141
14142 static void
14143 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14144 struct dwarf2_cu *cu)
14145 {
14146 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14147 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14148 struct nextfield *new_field;
14149 struct attribute *attr;
14150 struct field *fp;
14151 const char *fieldname = "";
14152
14153 if (die->tag == DW_TAG_inheritance)
14154 {
14155 fip->baseclasses.emplace_back ();
14156 new_field = &fip->baseclasses.back ();
14157 }
14158 else
14159 {
14160 fip->fields.emplace_back ();
14161 new_field = &fip->fields.back ();
14162 }
14163
14164 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14165 if (attr != nullptr)
14166 new_field->accessibility = DW_UNSND (attr);
14167 else
14168 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14169 if (new_field->accessibility != DW_ACCESS_public)
14170 fip->non_public_fields = 1;
14171
14172 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14173 if (attr != nullptr)
14174 new_field->virtuality = DW_UNSND (attr);
14175 else
14176 new_field->virtuality = DW_VIRTUALITY_none;
14177
14178 fp = &new_field->field;
14179
14180 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14181 {
14182 LONGEST offset;
14183
14184 /* Data member other than a C++ static data member. */
14185
14186 /* Get type of field. */
14187 fp->type = die_type (die, cu);
14188
14189 SET_FIELD_BITPOS (*fp, 0);
14190
14191 /* Get bit size of field (zero if none). */
14192 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14193 if (attr != nullptr)
14194 {
14195 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14196 }
14197 else
14198 {
14199 FIELD_BITSIZE (*fp) = 0;
14200 }
14201
14202 /* Get bit offset of field. */
14203 if (handle_data_member_location (die, cu, &offset))
14204 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14205 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14206 if (attr != nullptr)
14207 {
14208 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14209 {
14210 /* For big endian bits, the DW_AT_bit_offset gives the
14211 additional bit offset from the MSB of the containing
14212 anonymous object to the MSB of the field. We don't
14213 have to do anything special since we don't need to
14214 know the size of the anonymous object. */
14215 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14216 }
14217 else
14218 {
14219 /* For little endian bits, compute the bit offset to the
14220 MSB of the anonymous object, subtract off the number of
14221 bits from the MSB of the field to the MSB of the
14222 object, and then subtract off the number of bits of
14223 the field itself. The result is the bit offset of
14224 the LSB of the field. */
14225 int anonymous_size;
14226 int bit_offset = DW_UNSND (attr);
14227
14228 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14229 if (attr != nullptr)
14230 {
14231 /* The size of the anonymous object containing
14232 the bit field is explicit, so use the
14233 indicated size (in bytes). */
14234 anonymous_size = DW_UNSND (attr);
14235 }
14236 else
14237 {
14238 /* The size of the anonymous object containing
14239 the bit field must be inferred from the type
14240 attribute of the data member containing the
14241 bit field. */
14242 anonymous_size = TYPE_LENGTH (fp->type);
14243 }
14244 SET_FIELD_BITPOS (*fp,
14245 (FIELD_BITPOS (*fp)
14246 + anonymous_size * bits_per_byte
14247 - bit_offset - FIELD_BITSIZE (*fp)));
14248 }
14249 }
14250 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14251 if (attr != NULL)
14252 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14253 + attr->constant_value (0)));
14254
14255 /* Get name of field. */
14256 fieldname = dwarf2_name (die, cu);
14257 if (fieldname == NULL)
14258 fieldname = "";
14259
14260 /* The name is already allocated along with this objfile, so we don't
14261 need to duplicate it for the type. */
14262 fp->name = fieldname;
14263
14264 /* Change accessibility for artificial fields (e.g. virtual table
14265 pointer or virtual base class pointer) to private. */
14266 if (dwarf2_attr (die, DW_AT_artificial, cu))
14267 {
14268 FIELD_ARTIFICIAL (*fp) = 1;
14269 new_field->accessibility = DW_ACCESS_private;
14270 fip->non_public_fields = 1;
14271 }
14272 }
14273 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14274 {
14275 /* C++ static member. */
14276
14277 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14278 is a declaration, but all versions of G++ as of this writing
14279 (so through at least 3.2.1) incorrectly generate
14280 DW_TAG_variable tags. */
14281
14282 const char *physname;
14283
14284 /* Get name of field. */
14285 fieldname = dwarf2_name (die, cu);
14286 if (fieldname == NULL)
14287 return;
14288
14289 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14290 if (attr
14291 /* Only create a symbol if this is an external value.
14292 new_symbol checks this and puts the value in the global symbol
14293 table, which we want. If it is not external, new_symbol
14294 will try to put the value in cu->list_in_scope which is wrong. */
14295 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14296 {
14297 /* A static const member, not much different than an enum as far as
14298 we're concerned, except that we can support more types. */
14299 new_symbol (die, NULL, cu);
14300 }
14301
14302 /* Get physical name. */
14303 physname = dwarf2_physname (fieldname, die, cu);
14304
14305 /* The name is already allocated along with this objfile, so we don't
14306 need to duplicate it for the type. */
14307 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14308 FIELD_TYPE (*fp) = die_type (die, cu);
14309 FIELD_NAME (*fp) = fieldname;
14310 }
14311 else if (die->tag == DW_TAG_inheritance)
14312 {
14313 LONGEST offset;
14314
14315 /* C++ base class field. */
14316 if (handle_data_member_location (die, cu, &offset))
14317 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14318 FIELD_BITSIZE (*fp) = 0;
14319 FIELD_TYPE (*fp) = die_type (die, cu);
14320 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14321 }
14322 else if (die->tag == DW_TAG_variant_part)
14323 {
14324 /* process_structure_scope will treat this DIE as a union. */
14325 process_structure_scope (die, cu);
14326
14327 /* The variant part is relative to the start of the enclosing
14328 structure. */
14329 SET_FIELD_BITPOS (*fp, 0);
14330 fp->type = get_die_type (die, cu);
14331 fp->artificial = 1;
14332 fp->name = "<<variant>>";
14333
14334 /* Normally a DW_TAG_variant_part won't have a size, but our
14335 representation requires one, so set it to the maximum of the
14336 child sizes, being sure to account for the offset at which
14337 each child is seen. */
14338 if (TYPE_LENGTH (fp->type) == 0)
14339 {
14340 unsigned max = 0;
14341 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14342 {
14343 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14344 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14345 if (len > max)
14346 max = len;
14347 }
14348 TYPE_LENGTH (fp->type) = max;
14349 }
14350 }
14351 else
14352 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14353 }
14354
14355 /* Can the type given by DIE define another type? */
14356
14357 static bool
14358 type_can_define_types (const struct die_info *die)
14359 {
14360 switch (die->tag)
14361 {
14362 case DW_TAG_typedef:
14363 case DW_TAG_class_type:
14364 case DW_TAG_structure_type:
14365 case DW_TAG_union_type:
14366 case DW_TAG_enumeration_type:
14367 return true;
14368
14369 default:
14370 return false;
14371 }
14372 }
14373
14374 /* Add a type definition defined in the scope of the FIP's class. */
14375
14376 static void
14377 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14378 struct dwarf2_cu *cu)
14379 {
14380 struct decl_field fp;
14381 memset (&fp, 0, sizeof (fp));
14382
14383 gdb_assert (type_can_define_types (die));
14384
14385 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14386 fp.name = dwarf2_name (die, cu);
14387 fp.type = read_type_die (die, cu);
14388
14389 /* Save accessibility. */
14390 enum dwarf_access_attribute accessibility;
14391 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14392 if (attr != NULL)
14393 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14394 else
14395 accessibility = dwarf2_default_access_attribute (die, cu);
14396 switch (accessibility)
14397 {
14398 case DW_ACCESS_public:
14399 /* The assumed value if neither private nor protected. */
14400 break;
14401 case DW_ACCESS_private:
14402 fp.is_private = 1;
14403 break;
14404 case DW_ACCESS_protected:
14405 fp.is_protected = 1;
14406 break;
14407 default:
14408 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14409 }
14410
14411 if (die->tag == DW_TAG_typedef)
14412 fip->typedef_field_list.push_back (fp);
14413 else
14414 fip->nested_types_list.push_back (fp);
14415 }
14416
14417 /* Create the vector of fields, and attach it to the type. */
14418
14419 static void
14420 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14421 struct dwarf2_cu *cu)
14422 {
14423 int nfields = fip->nfields ();
14424
14425 /* Record the field count, allocate space for the array of fields,
14426 and create blank accessibility bitfields if necessary. */
14427 TYPE_NFIELDS (type) = nfields;
14428 TYPE_FIELDS (type) = (struct field *)
14429 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14430
14431 if (fip->non_public_fields && cu->language != language_ada)
14432 {
14433 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14434
14435 TYPE_FIELD_PRIVATE_BITS (type) =
14436 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14437 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14438
14439 TYPE_FIELD_PROTECTED_BITS (type) =
14440 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14441 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14442
14443 TYPE_FIELD_IGNORE_BITS (type) =
14444 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14445 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14446 }
14447
14448 /* If the type has baseclasses, allocate and clear a bit vector for
14449 TYPE_FIELD_VIRTUAL_BITS. */
14450 if (!fip->baseclasses.empty () && cu->language != language_ada)
14451 {
14452 int num_bytes = B_BYTES (fip->baseclasses.size ());
14453 unsigned char *pointer;
14454
14455 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14456 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14457 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14458 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14459 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14460 }
14461
14462 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14463 {
14464 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14465
14466 for (int index = 0; index < nfields; ++index)
14467 {
14468 struct nextfield &field = fip->fields[index];
14469
14470 if (field.variant.is_discriminant)
14471 di->discriminant_index = index;
14472 else if (field.variant.default_branch)
14473 di->default_index = index;
14474 else
14475 di->discriminants[index] = field.variant.discriminant_value;
14476 }
14477 }
14478
14479 /* Copy the saved-up fields into the field vector. */
14480 for (int i = 0; i < nfields; ++i)
14481 {
14482 struct nextfield &field
14483 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14484 : fip->fields[i - fip->baseclasses.size ()]);
14485
14486 TYPE_FIELD (type, i) = field.field;
14487 switch (field.accessibility)
14488 {
14489 case DW_ACCESS_private:
14490 if (cu->language != language_ada)
14491 SET_TYPE_FIELD_PRIVATE (type, i);
14492 break;
14493
14494 case DW_ACCESS_protected:
14495 if (cu->language != language_ada)
14496 SET_TYPE_FIELD_PROTECTED (type, i);
14497 break;
14498
14499 case DW_ACCESS_public:
14500 break;
14501
14502 default:
14503 /* Unknown accessibility. Complain and treat it as public. */
14504 {
14505 complaint (_("unsupported accessibility %d"),
14506 field.accessibility);
14507 }
14508 break;
14509 }
14510 if (i < fip->baseclasses.size ())
14511 {
14512 switch (field.virtuality)
14513 {
14514 case DW_VIRTUALITY_virtual:
14515 case DW_VIRTUALITY_pure_virtual:
14516 if (cu->language == language_ada)
14517 error (_("unexpected virtuality in component of Ada type"));
14518 SET_TYPE_FIELD_VIRTUAL (type, i);
14519 break;
14520 }
14521 }
14522 }
14523 }
14524
14525 /* Return true if this member function is a constructor, false
14526 otherwise. */
14527
14528 static int
14529 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14530 {
14531 const char *fieldname;
14532 const char *type_name;
14533 int len;
14534
14535 if (die->parent == NULL)
14536 return 0;
14537
14538 if (die->parent->tag != DW_TAG_structure_type
14539 && die->parent->tag != DW_TAG_union_type
14540 && die->parent->tag != DW_TAG_class_type)
14541 return 0;
14542
14543 fieldname = dwarf2_name (die, cu);
14544 type_name = dwarf2_name (die->parent, cu);
14545 if (fieldname == NULL || type_name == NULL)
14546 return 0;
14547
14548 len = strlen (fieldname);
14549 return (strncmp (fieldname, type_name, len) == 0
14550 && (type_name[len] == '\0' || type_name[len] == '<'));
14551 }
14552
14553 /* Check if the given VALUE is a recognized enum
14554 dwarf_defaulted_attribute constant according to DWARF5 spec,
14555 Table 7.24. */
14556
14557 static bool
14558 is_valid_DW_AT_defaulted (ULONGEST value)
14559 {
14560 switch (value)
14561 {
14562 case DW_DEFAULTED_no:
14563 case DW_DEFAULTED_in_class:
14564 case DW_DEFAULTED_out_of_class:
14565 return true;
14566 }
14567
14568 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14569 return false;
14570 }
14571
14572 /* Add a member function to the proper fieldlist. */
14573
14574 static void
14575 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14576 struct type *type, struct dwarf2_cu *cu)
14577 {
14578 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14579 struct attribute *attr;
14580 int i;
14581 struct fnfieldlist *flp = nullptr;
14582 struct fn_field *fnp;
14583 const char *fieldname;
14584 struct type *this_type;
14585 enum dwarf_access_attribute accessibility;
14586
14587 if (cu->language == language_ada)
14588 error (_("unexpected member function in Ada type"));
14589
14590 /* Get name of member function. */
14591 fieldname = dwarf2_name (die, cu);
14592 if (fieldname == NULL)
14593 return;
14594
14595 /* Look up member function name in fieldlist. */
14596 for (i = 0; i < fip->fnfieldlists.size (); i++)
14597 {
14598 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14599 {
14600 flp = &fip->fnfieldlists[i];
14601 break;
14602 }
14603 }
14604
14605 /* Create a new fnfieldlist if necessary. */
14606 if (flp == nullptr)
14607 {
14608 fip->fnfieldlists.emplace_back ();
14609 flp = &fip->fnfieldlists.back ();
14610 flp->name = fieldname;
14611 i = fip->fnfieldlists.size () - 1;
14612 }
14613
14614 /* Create a new member function field and add it to the vector of
14615 fnfieldlists. */
14616 flp->fnfields.emplace_back ();
14617 fnp = &flp->fnfields.back ();
14618
14619 /* Delay processing of the physname until later. */
14620 if (cu->language == language_cplus)
14621 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14622 die, cu);
14623 else
14624 {
14625 const char *physname = dwarf2_physname (fieldname, die, cu);
14626 fnp->physname = physname ? physname : "";
14627 }
14628
14629 fnp->type = alloc_type (objfile);
14630 this_type = read_type_die (die, cu);
14631 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14632 {
14633 int nparams = TYPE_NFIELDS (this_type);
14634
14635 /* TYPE is the domain of this method, and THIS_TYPE is the type
14636 of the method itself (TYPE_CODE_METHOD). */
14637 smash_to_method_type (fnp->type, type,
14638 TYPE_TARGET_TYPE (this_type),
14639 TYPE_FIELDS (this_type),
14640 TYPE_NFIELDS (this_type),
14641 TYPE_VARARGS (this_type));
14642
14643 /* Handle static member functions.
14644 Dwarf2 has no clean way to discern C++ static and non-static
14645 member functions. G++ helps GDB by marking the first
14646 parameter for non-static member functions (which is the this
14647 pointer) as artificial. We obtain this information from
14648 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14649 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14650 fnp->voffset = VOFFSET_STATIC;
14651 }
14652 else
14653 complaint (_("member function type missing for '%s'"),
14654 dwarf2_full_name (fieldname, die, cu));
14655
14656 /* Get fcontext from DW_AT_containing_type if present. */
14657 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14658 fnp->fcontext = die_containing_type (die, cu);
14659
14660 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14661 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14662
14663 /* Get accessibility. */
14664 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14665 if (attr != nullptr)
14666 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14667 else
14668 accessibility = dwarf2_default_access_attribute (die, cu);
14669 switch (accessibility)
14670 {
14671 case DW_ACCESS_private:
14672 fnp->is_private = 1;
14673 break;
14674 case DW_ACCESS_protected:
14675 fnp->is_protected = 1;
14676 break;
14677 }
14678
14679 /* Check for artificial methods. */
14680 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14681 if (attr && DW_UNSND (attr) != 0)
14682 fnp->is_artificial = 1;
14683
14684 /* Check for defaulted methods. */
14685 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14686 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14687 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14688
14689 /* Check for deleted methods. */
14690 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14691 if (attr != nullptr && DW_UNSND (attr) != 0)
14692 fnp->is_deleted = 1;
14693
14694 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14695
14696 /* Get index in virtual function table if it is a virtual member
14697 function. For older versions of GCC, this is an offset in the
14698 appropriate virtual table, as specified by DW_AT_containing_type.
14699 For everyone else, it is an expression to be evaluated relative
14700 to the object address. */
14701
14702 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14703 if (attr != nullptr)
14704 {
14705 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14706 {
14707 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14708 {
14709 /* Old-style GCC. */
14710 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14711 }
14712 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14713 || (DW_BLOCK (attr)->size > 1
14714 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14715 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14716 {
14717 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14718 if ((fnp->voffset % cu->header.addr_size) != 0)
14719 dwarf2_complex_location_expr_complaint ();
14720 else
14721 fnp->voffset /= cu->header.addr_size;
14722 fnp->voffset += 2;
14723 }
14724 else
14725 dwarf2_complex_location_expr_complaint ();
14726
14727 if (!fnp->fcontext)
14728 {
14729 /* If there is no `this' field and no DW_AT_containing_type,
14730 we cannot actually find a base class context for the
14731 vtable! */
14732 if (TYPE_NFIELDS (this_type) == 0
14733 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14734 {
14735 complaint (_("cannot determine context for virtual member "
14736 "function \"%s\" (offset %s)"),
14737 fieldname, sect_offset_str (die->sect_off));
14738 }
14739 else
14740 {
14741 fnp->fcontext
14742 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14743 }
14744 }
14745 }
14746 else if (attr->form_is_section_offset ())
14747 {
14748 dwarf2_complex_location_expr_complaint ();
14749 }
14750 else
14751 {
14752 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14753 fieldname);
14754 }
14755 }
14756 else
14757 {
14758 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14759 if (attr && DW_UNSND (attr))
14760 {
14761 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14762 complaint (_("Member function \"%s\" (offset %s) is virtual "
14763 "but the vtable offset is not specified"),
14764 fieldname, sect_offset_str (die->sect_off));
14765 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14766 TYPE_CPLUS_DYNAMIC (type) = 1;
14767 }
14768 }
14769 }
14770
14771 /* Create the vector of member function fields, and attach it to the type. */
14772
14773 static void
14774 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14775 struct dwarf2_cu *cu)
14776 {
14777 if (cu->language == language_ada)
14778 error (_("unexpected member functions in Ada type"));
14779
14780 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14781 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14782 TYPE_ALLOC (type,
14783 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14784
14785 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14786 {
14787 struct fnfieldlist &nf = fip->fnfieldlists[i];
14788 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14789
14790 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14791 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14792 fn_flp->fn_fields = (struct fn_field *)
14793 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14794
14795 for (int k = 0; k < nf.fnfields.size (); ++k)
14796 fn_flp->fn_fields[k] = nf.fnfields[k];
14797 }
14798
14799 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14800 }
14801
14802 /* Returns non-zero if NAME is the name of a vtable member in CU's
14803 language, zero otherwise. */
14804 static int
14805 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14806 {
14807 static const char vptr[] = "_vptr";
14808
14809 /* Look for the C++ form of the vtable. */
14810 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14811 return 1;
14812
14813 return 0;
14814 }
14815
14816 /* GCC outputs unnamed structures that are really pointers to member
14817 functions, with the ABI-specified layout. If TYPE describes
14818 such a structure, smash it into a member function type.
14819
14820 GCC shouldn't do this; it should just output pointer to member DIEs.
14821 This is GCC PR debug/28767. */
14822
14823 static void
14824 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14825 {
14826 struct type *pfn_type, *self_type, *new_type;
14827
14828 /* Check for a structure with no name and two children. */
14829 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14830 return;
14831
14832 /* Check for __pfn and __delta members. */
14833 if (TYPE_FIELD_NAME (type, 0) == NULL
14834 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14835 || TYPE_FIELD_NAME (type, 1) == NULL
14836 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14837 return;
14838
14839 /* Find the type of the method. */
14840 pfn_type = TYPE_FIELD_TYPE (type, 0);
14841 if (pfn_type == NULL
14842 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14843 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14844 return;
14845
14846 /* Look for the "this" argument. */
14847 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14848 if (TYPE_NFIELDS (pfn_type) == 0
14849 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14850 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14851 return;
14852
14853 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14854 new_type = alloc_type (objfile);
14855 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14856 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14857 TYPE_VARARGS (pfn_type));
14858 smash_to_methodptr_type (type, new_type);
14859 }
14860
14861 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14862 appropriate error checking and issuing complaints if there is a
14863 problem. */
14864
14865 static ULONGEST
14866 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14867 {
14868 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14869
14870 if (attr == nullptr)
14871 return 0;
14872
14873 if (!attr->form_is_constant ())
14874 {
14875 complaint (_("DW_AT_alignment must have constant form"
14876 " - DIE at %s [in module %s]"),
14877 sect_offset_str (die->sect_off),
14878 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14879 return 0;
14880 }
14881
14882 ULONGEST align;
14883 if (attr->form == DW_FORM_sdata)
14884 {
14885 LONGEST val = DW_SND (attr);
14886 if (val < 0)
14887 {
14888 complaint (_("DW_AT_alignment value must not be negative"
14889 " - DIE at %s [in module %s]"),
14890 sect_offset_str (die->sect_off),
14891 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14892 return 0;
14893 }
14894 align = val;
14895 }
14896 else
14897 align = DW_UNSND (attr);
14898
14899 if (align == 0)
14900 {
14901 complaint (_("DW_AT_alignment value must not be zero"
14902 " - DIE at %s [in module %s]"),
14903 sect_offset_str (die->sect_off),
14904 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14905 return 0;
14906 }
14907 if ((align & (align - 1)) != 0)
14908 {
14909 complaint (_("DW_AT_alignment value must be a power of 2"
14910 " - DIE at %s [in module %s]"),
14911 sect_offset_str (die->sect_off),
14912 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14913 return 0;
14914 }
14915
14916 return align;
14917 }
14918
14919 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14920 the alignment for TYPE. */
14921
14922 static void
14923 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14924 struct type *type)
14925 {
14926 if (!set_type_align (type, get_alignment (cu, die)))
14927 complaint (_("DW_AT_alignment value too large"
14928 " - DIE at %s [in module %s]"),
14929 sect_offset_str (die->sect_off),
14930 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14931 }
14932
14933 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14934 constant for a type, according to DWARF5 spec, Table 5.5. */
14935
14936 static bool
14937 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14938 {
14939 switch (value)
14940 {
14941 case DW_CC_normal:
14942 case DW_CC_pass_by_reference:
14943 case DW_CC_pass_by_value:
14944 return true;
14945
14946 default:
14947 complaint (_("unrecognized DW_AT_calling_convention value "
14948 "(%s) for a type"), pulongest (value));
14949 return false;
14950 }
14951 }
14952
14953 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14954 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14955 also according to GNU-specific values (see include/dwarf2.h). */
14956
14957 static bool
14958 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14959 {
14960 switch (value)
14961 {
14962 case DW_CC_normal:
14963 case DW_CC_program:
14964 case DW_CC_nocall:
14965 return true;
14966
14967 case DW_CC_GNU_renesas_sh:
14968 case DW_CC_GNU_borland_fastcall_i386:
14969 case DW_CC_GDB_IBM_OpenCL:
14970 return true;
14971
14972 default:
14973 complaint (_("unrecognized DW_AT_calling_convention value "
14974 "(%s) for a subroutine"), pulongest (value));
14975 return false;
14976 }
14977 }
14978
14979 /* Called when we find the DIE that starts a structure or union scope
14980 (definition) to create a type for the structure or union. Fill in
14981 the type's name and general properties; the members will not be
14982 processed until process_structure_scope. A symbol table entry for
14983 the type will also not be done until process_structure_scope (assuming
14984 the type has a name).
14985
14986 NOTE: we need to call these functions regardless of whether or not the
14987 DIE has a DW_AT_name attribute, since it might be an anonymous
14988 structure or union. This gets the type entered into our set of
14989 user defined types. */
14990
14991 static struct type *
14992 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
14993 {
14994 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14995 struct type *type;
14996 struct attribute *attr;
14997 const char *name;
14998
14999 /* If the definition of this type lives in .debug_types, read that type.
15000 Don't follow DW_AT_specification though, that will take us back up
15001 the chain and we want to go down. */
15002 attr = die->attr (DW_AT_signature);
15003 if (attr != nullptr)
15004 {
15005 type = get_DW_AT_signature_type (die, attr, cu);
15006
15007 /* The type's CU may not be the same as CU.
15008 Ensure TYPE is recorded with CU in die_type_hash. */
15009 return set_die_type (die, type, cu);
15010 }
15011
15012 type = alloc_type (objfile);
15013 INIT_CPLUS_SPECIFIC (type);
15014
15015 name = dwarf2_name (die, cu);
15016 if (name != NULL)
15017 {
15018 if (cu->language == language_cplus
15019 || cu->language == language_d
15020 || cu->language == language_rust)
15021 {
15022 const char *full_name = dwarf2_full_name (name, die, cu);
15023
15024 /* dwarf2_full_name might have already finished building the DIE's
15025 type. If so, there is no need to continue. */
15026 if (get_die_type (die, cu) != NULL)
15027 return get_die_type (die, cu);
15028
15029 TYPE_NAME (type) = full_name;
15030 }
15031 else
15032 {
15033 /* The name is already allocated along with this objfile, so
15034 we don't need to duplicate it for the type. */
15035 TYPE_NAME (type) = name;
15036 }
15037 }
15038
15039 if (die->tag == DW_TAG_structure_type)
15040 {
15041 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15042 }
15043 else if (die->tag == DW_TAG_union_type)
15044 {
15045 TYPE_CODE (type) = TYPE_CODE_UNION;
15046 }
15047 else if (die->tag == DW_TAG_variant_part)
15048 {
15049 TYPE_CODE (type) = TYPE_CODE_UNION;
15050 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15051 }
15052 else
15053 {
15054 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15055 }
15056
15057 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15058 TYPE_DECLARED_CLASS (type) = 1;
15059
15060 /* Store the calling convention in the type if it's available in
15061 the die. Otherwise the calling convention remains set to
15062 the default value DW_CC_normal. */
15063 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15064 if (attr != nullptr
15065 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15066 {
15067 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15068 TYPE_CPLUS_CALLING_CONVENTION (type)
15069 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15070 }
15071
15072 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15073 if (attr != nullptr)
15074 {
15075 if (attr->form_is_constant ())
15076 TYPE_LENGTH (type) = DW_UNSND (attr);
15077 else
15078 {
15079 /* For the moment, dynamic type sizes are not supported
15080 by GDB's struct type. The actual size is determined
15081 on-demand when resolving the type of a given object,
15082 so set the type's length to zero for now. Otherwise,
15083 we record an expression as the length, and that expression
15084 could lead to a very large value, which could eventually
15085 lead to us trying to allocate that much memory when creating
15086 a value of that type. */
15087 TYPE_LENGTH (type) = 0;
15088 }
15089 }
15090 else
15091 {
15092 TYPE_LENGTH (type) = 0;
15093 }
15094
15095 maybe_set_alignment (cu, die, type);
15096
15097 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15098 {
15099 /* ICC<14 does not output the required DW_AT_declaration on
15100 incomplete types, but gives them a size of zero. */
15101 TYPE_STUB (type) = 1;
15102 }
15103 else
15104 TYPE_STUB_SUPPORTED (type) = 1;
15105
15106 if (die_is_declaration (die, cu))
15107 TYPE_STUB (type) = 1;
15108 else if (attr == NULL && die->child == NULL
15109 && producer_is_realview (cu->producer))
15110 /* RealView does not output the required DW_AT_declaration
15111 on incomplete types. */
15112 TYPE_STUB (type) = 1;
15113
15114 /* We need to add the type field to the die immediately so we don't
15115 infinitely recurse when dealing with pointers to the structure
15116 type within the structure itself. */
15117 set_die_type (die, type, cu);
15118
15119 /* set_die_type should be already done. */
15120 set_descriptive_type (type, die, cu);
15121
15122 return type;
15123 }
15124
15125 /* A helper for process_structure_scope that handles a single member
15126 DIE. */
15127
15128 static void
15129 handle_struct_member_die (struct die_info *child_die, struct type *type,
15130 struct field_info *fi,
15131 std::vector<struct symbol *> *template_args,
15132 struct dwarf2_cu *cu)
15133 {
15134 if (child_die->tag == DW_TAG_member
15135 || child_die->tag == DW_TAG_variable
15136 || child_die->tag == DW_TAG_variant_part)
15137 {
15138 /* NOTE: carlton/2002-11-05: A C++ static data member
15139 should be a DW_TAG_member that is a declaration, but
15140 all versions of G++ as of this writing (so through at
15141 least 3.2.1) incorrectly generate DW_TAG_variable
15142 tags for them instead. */
15143 dwarf2_add_field (fi, child_die, cu);
15144 }
15145 else if (child_die->tag == DW_TAG_subprogram)
15146 {
15147 /* Rust doesn't have member functions in the C++ sense.
15148 However, it does emit ordinary functions as children
15149 of a struct DIE. */
15150 if (cu->language == language_rust)
15151 read_func_scope (child_die, cu);
15152 else
15153 {
15154 /* C++ member function. */
15155 dwarf2_add_member_fn (fi, child_die, type, cu);
15156 }
15157 }
15158 else if (child_die->tag == DW_TAG_inheritance)
15159 {
15160 /* C++ base class field. */
15161 dwarf2_add_field (fi, child_die, cu);
15162 }
15163 else if (type_can_define_types (child_die))
15164 dwarf2_add_type_defn (fi, child_die, cu);
15165 else if (child_die->tag == DW_TAG_template_type_param
15166 || child_die->tag == DW_TAG_template_value_param)
15167 {
15168 struct symbol *arg = new_symbol (child_die, NULL, cu);
15169
15170 if (arg != NULL)
15171 template_args->push_back (arg);
15172 }
15173 else if (child_die->tag == DW_TAG_variant)
15174 {
15175 /* In a variant we want to get the discriminant and also add a
15176 field for our sole member child. */
15177 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15178
15179 for (die_info *variant_child = child_die->child;
15180 variant_child != NULL;
15181 variant_child = variant_child->sibling)
15182 {
15183 if (variant_child->tag == DW_TAG_member)
15184 {
15185 handle_struct_member_die (variant_child, type, fi,
15186 template_args, cu);
15187 /* Only handle the one. */
15188 break;
15189 }
15190 }
15191
15192 /* We don't handle this but we might as well report it if we see
15193 it. */
15194 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15195 complaint (_("DW_AT_discr_list is not supported yet"
15196 " - DIE at %s [in module %s]"),
15197 sect_offset_str (child_die->sect_off),
15198 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15199
15200 /* The first field was just added, so we can stash the
15201 discriminant there. */
15202 gdb_assert (!fi->fields.empty ());
15203 if (discr == NULL)
15204 fi->fields.back ().variant.default_branch = true;
15205 else
15206 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15207 }
15208 }
15209
15210 /* Finish creating a structure or union type, including filling in
15211 its members and creating a symbol for it. */
15212
15213 static void
15214 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15215 {
15216 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15217 struct die_info *child_die;
15218 struct type *type;
15219
15220 type = get_die_type (die, cu);
15221 if (type == NULL)
15222 type = read_structure_type (die, cu);
15223
15224 /* When reading a DW_TAG_variant_part, we need to notice when we
15225 read the discriminant member, so we can record it later in the
15226 discriminant_info. */
15227 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15228 sect_offset discr_offset {};
15229 bool has_template_parameters = false;
15230
15231 if (is_variant_part)
15232 {
15233 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15234 if (discr == NULL)
15235 {
15236 /* Maybe it's a univariant form, an extension we support.
15237 In this case arrange not to check the offset. */
15238 is_variant_part = false;
15239 }
15240 else if (discr->form_is_ref ())
15241 {
15242 struct dwarf2_cu *target_cu = cu;
15243 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15244
15245 discr_offset = target_die->sect_off;
15246 }
15247 else
15248 {
15249 complaint (_("DW_AT_discr does not have DIE reference form"
15250 " - DIE at %s [in module %s]"),
15251 sect_offset_str (die->sect_off),
15252 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15253 is_variant_part = false;
15254 }
15255 }
15256
15257 if (die->child != NULL && ! die_is_declaration (die, cu))
15258 {
15259 struct field_info fi;
15260 std::vector<struct symbol *> template_args;
15261
15262 child_die = die->child;
15263
15264 while (child_die && child_die->tag)
15265 {
15266 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15267
15268 if (is_variant_part && discr_offset == child_die->sect_off)
15269 fi.fields.back ().variant.is_discriminant = true;
15270
15271 child_die = child_die->sibling;
15272 }
15273
15274 /* Attach template arguments to type. */
15275 if (!template_args.empty ())
15276 {
15277 has_template_parameters = true;
15278 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15279 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15280 TYPE_TEMPLATE_ARGUMENTS (type)
15281 = XOBNEWVEC (&objfile->objfile_obstack,
15282 struct symbol *,
15283 TYPE_N_TEMPLATE_ARGUMENTS (type));
15284 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15285 template_args.data (),
15286 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15287 * sizeof (struct symbol *)));
15288 }
15289
15290 /* Attach fields and member functions to the type. */
15291 if (fi.nfields () > 0)
15292 dwarf2_attach_fields_to_type (&fi, type, cu);
15293 if (!fi.fnfieldlists.empty ())
15294 {
15295 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15296
15297 /* Get the type which refers to the base class (possibly this
15298 class itself) which contains the vtable pointer for the current
15299 class from the DW_AT_containing_type attribute. This use of
15300 DW_AT_containing_type is a GNU extension. */
15301
15302 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15303 {
15304 struct type *t = die_containing_type (die, cu);
15305
15306 set_type_vptr_basetype (type, t);
15307 if (type == t)
15308 {
15309 int i;
15310
15311 /* Our own class provides vtbl ptr. */
15312 for (i = TYPE_NFIELDS (t) - 1;
15313 i >= TYPE_N_BASECLASSES (t);
15314 --i)
15315 {
15316 const char *fieldname = TYPE_FIELD_NAME (t, i);
15317
15318 if (is_vtable_name (fieldname, cu))
15319 {
15320 set_type_vptr_fieldno (type, i);
15321 break;
15322 }
15323 }
15324
15325 /* Complain if virtual function table field not found. */
15326 if (i < TYPE_N_BASECLASSES (t))
15327 complaint (_("virtual function table pointer "
15328 "not found when defining class '%s'"),
15329 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15330 }
15331 else
15332 {
15333 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15334 }
15335 }
15336 else if (cu->producer
15337 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15338 {
15339 /* The IBM XLC compiler does not provide direct indication
15340 of the containing type, but the vtable pointer is
15341 always named __vfp. */
15342
15343 int i;
15344
15345 for (i = TYPE_NFIELDS (type) - 1;
15346 i >= TYPE_N_BASECLASSES (type);
15347 --i)
15348 {
15349 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15350 {
15351 set_type_vptr_fieldno (type, i);
15352 set_type_vptr_basetype (type, type);
15353 break;
15354 }
15355 }
15356 }
15357 }
15358
15359 /* Copy fi.typedef_field_list linked list elements content into the
15360 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15361 if (!fi.typedef_field_list.empty ())
15362 {
15363 int count = fi.typedef_field_list.size ();
15364
15365 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15366 TYPE_TYPEDEF_FIELD_ARRAY (type)
15367 = ((struct decl_field *)
15368 TYPE_ALLOC (type,
15369 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15370 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15371
15372 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15373 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15374 }
15375
15376 /* Copy fi.nested_types_list linked list elements content into the
15377 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15378 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15379 {
15380 int count = fi.nested_types_list.size ();
15381
15382 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15383 TYPE_NESTED_TYPES_ARRAY (type)
15384 = ((struct decl_field *)
15385 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15386 TYPE_NESTED_TYPES_COUNT (type) = count;
15387
15388 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15389 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15390 }
15391 }
15392
15393 quirk_gcc_member_function_pointer (type, objfile);
15394 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15395 cu->rust_unions.push_back (type);
15396
15397 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15398 snapshots) has been known to create a die giving a declaration
15399 for a class that has, as a child, a die giving a definition for a
15400 nested class. So we have to process our children even if the
15401 current die is a declaration. Normally, of course, a declaration
15402 won't have any children at all. */
15403
15404 child_die = die->child;
15405
15406 while (child_die != NULL && child_die->tag)
15407 {
15408 if (child_die->tag == DW_TAG_member
15409 || child_die->tag == DW_TAG_variable
15410 || child_die->tag == DW_TAG_inheritance
15411 || child_die->tag == DW_TAG_template_value_param
15412 || child_die->tag == DW_TAG_template_type_param)
15413 {
15414 /* Do nothing. */
15415 }
15416 else
15417 process_die (child_die, cu);
15418
15419 child_die = child_die->sibling;
15420 }
15421
15422 /* Do not consider external references. According to the DWARF standard,
15423 these DIEs are identified by the fact that they have no byte_size
15424 attribute, and a declaration attribute. */
15425 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15426 || !die_is_declaration (die, cu))
15427 {
15428 struct symbol *sym = new_symbol (die, type, cu);
15429
15430 if (has_template_parameters)
15431 {
15432 struct symtab *symtab;
15433 if (sym != nullptr)
15434 symtab = symbol_symtab (sym);
15435 else if (cu->line_header != nullptr)
15436 {
15437 /* Any related symtab will do. */
15438 symtab
15439 = cu->line_header->file_names ()[0].symtab;
15440 }
15441 else
15442 {
15443 symtab = nullptr;
15444 complaint (_("could not find suitable "
15445 "symtab for template parameter"
15446 " - DIE at %s [in module %s]"),
15447 sect_offset_str (die->sect_off),
15448 objfile_name (objfile));
15449 }
15450
15451 if (symtab != nullptr)
15452 {
15453 /* Make sure that the symtab is set on the new symbols.
15454 Even though they don't appear in this symtab directly,
15455 other parts of gdb assume that symbols do, and this is
15456 reasonably true. */
15457 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15458 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15459 }
15460 }
15461 }
15462 }
15463
15464 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15465 update TYPE using some information only available in DIE's children. */
15466
15467 static void
15468 update_enumeration_type_from_children (struct die_info *die,
15469 struct type *type,
15470 struct dwarf2_cu *cu)
15471 {
15472 struct die_info *child_die;
15473 int unsigned_enum = 1;
15474 int flag_enum = 1;
15475
15476 auto_obstack obstack;
15477
15478 for (child_die = die->child;
15479 child_die != NULL && child_die->tag;
15480 child_die = child_die->sibling)
15481 {
15482 struct attribute *attr;
15483 LONGEST value;
15484 const gdb_byte *bytes;
15485 struct dwarf2_locexpr_baton *baton;
15486 const char *name;
15487
15488 if (child_die->tag != DW_TAG_enumerator)
15489 continue;
15490
15491 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15492 if (attr == NULL)
15493 continue;
15494
15495 name = dwarf2_name (child_die, cu);
15496 if (name == NULL)
15497 name = "<anonymous enumerator>";
15498
15499 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15500 &value, &bytes, &baton);
15501 if (value < 0)
15502 {
15503 unsigned_enum = 0;
15504 flag_enum = 0;
15505 }
15506 else
15507 {
15508 if (count_one_bits_ll (value) >= 2)
15509 flag_enum = 0;
15510 }
15511
15512 /* If we already know that the enum type is neither unsigned, nor
15513 a flag type, no need to look at the rest of the enumerates. */
15514 if (!unsigned_enum && !flag_enum)
15515 break;
15516 }
15517
15518 if (unsigned_enum)
15519 TYPE_UNSIGNED (type) = 1;
15520 if (flag_enum)
15521 TYPE_FLAG_ENUM (type) = 1;
15522 }
15523
15524 /* Given a DW_AT_enumeration_type die, set its type. We do not
15525 complete the type's fields yet, or create any symbols. */
15526
15527 static struct type *
15528 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15529 {
15530 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15531 struct type *type;
15532 struct attribute *attr;
15533 const char *name;
15534
15535 /* If the definition of this type lives in .debug_types, read that type.
15536 Don't follow DW_AT_specification though, that will take us back up
15537 the chain and we want to go down. */
15538 attr = die->attr (DW_AT_signature);
15539 if (attr != nullptr)
15540 {
15541 type = get_DW_AT_signature_type (die, attr, cu);
15542
15543 /* The type's CU may not be the same as CU.
15544 Ensure TYPE is recorded with CU in die_type_hash. */
15545 return set_die_type (die, type, cu);
15546 }
15547
15548 type = alloc_type (objfile);
15549
15550 TYPE_CODE (type) = TYPE_CODE_ENUM;
15551 name = dwarf2_full_name (NULL, die, cu);
15552 if (name != NULL)
15553 TYPE_NAME (type) = name;
15554
15555 attr = dwarf2_attr (die, DW_AT_type, cu);
15556 if (attr != NULL)
15557 {
15558 struct type *underlying_type = die_type (die, cu);
15559
15560 TYPE_TARGET_TYPE (type) = underlying_type;
15561 }
15562
15563 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15564 if (attr != nullptr)
15565 {
15566 TYPE_LENGTH (type) = DW_UNSND (attr);
15567 }
15568 else
15569 {
15570 TYPE_LENGTH (type) = 0;
15571 }
15572
15573 maybe_set_alignment (cu, die, type);
15574
15575 /* The enumeration DIE can be incomplete. In Ada, any type can be
15576 declared as private in the package spec, and then defined only
15577 inside the package body. Such types are known as Taft Amendment
15578 Types. When another package uses such a type, an incomplete DIE
15579 may be generated by the compiler. */
15580 if (die_is_declaration (die, cu))
15581 TYPE_STUB (type) = 1;
15582
15583 /* Finish the creation of this type by using the enum's children.
15584 We must call this even when the underlying type has been provided
15585 so that we can determine if we're looking at a "flag" enum. */
15586 update_enumeration_type_from_children (die, type, cu);
15587
15588 /* If this type has an underlying type that is not a stub, then we
15589 may use its attributes. We always use the "unsigned" attribute
15590 in this situation, because ordinarily we guess whether the type
15591 is unsigned -- but the guess can be wrong and the underlying type
15592 can tell us the reality. However, we defer to a local size
15593 attribute if one exists, because this lets the compiler override
15594 the underlying type if needed. */
15595 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15596 {
15597 struct type *underlying_type = TYPE_TARGET_TYPE (type);
15598 underlying_type = check_typedef (underlying_type);
15599 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
15600 if (TYPE_LENGTH (type) == 0)
15601 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
15602 if (TYPE_RAW_ALIGN (type) == 0
15603 && TYPE_RAW_ALIGN (underlying_type) != 0)
15604 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
15605 }
15606
15607 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15608
15609 return set_die_type (die, type, cu);
15610 }
15611
15612 /* Given a pointer to a die which begins an enumeration, process all
15613 the dies that define the members of the enumeration, and create the
15614 symbol for the enumeration type.
15615
15616 NOTE: We reverse the order of the element list. */
15617
15618 static void
15619 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15620 {
15621 struct type *this_type;
15622
15623 this_type = get_die_type (die, cu);
15624 if (this_type == NULL)
15625 this_type = read_enumeration_type (die, cu);
15626
15627 if (die->child != NULL)
15628 {
15629 struct die_info *child_die;
15630 struct symbol *sym;
15631 std::vector<struct field> fields;
15632 const char *name;
15633
15634 child_die = die->child;
15635 while (child_die && child_die->tag)
15636 {
15637 if (child_die->tag != DW_TAG_enumerator)
15638 {
15639 process_die (child_die, cu);
15640 }
15641 else
15642 {
15643 name = dwarf2_name (child_die, cu);
15644 if (name)
15645 {
15646 sym = new_symbol (child_die, this_type, cu);
15647
15648 fields.emplace_back ();
15649 struct field &field = fields.back ();
15650
15651 FIELD_NAME (field) = sym->linkage_name ();
15652 FIELD_TYPE (field) = NULL;
15653 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15654 FIELD_BITSIZE (field) = 0;
15655 }
15656 }
15657
15658 child_die = child_die->sibling;
15659 }
15660
15661 if (!fields.empty ())
15662 {
15663 TYPE_NFIELDS (this_type) = fields.size ();
15664 TYPE_FIELDS (this_type) = (struct field *)
15665 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15666 memcpy (TYPE_FIELDS (this_type), fields.data (),
15667 sizeof (struct field) * fields.size ());
15668 }
15669 }
15670
15671 /* If we are reading an enum from a .debug_types unit, and the enum
15672 is a declaration, and the enum is not the signatured type in the
15673 unit, then we do not want to add a symbol for it. Adding a
15674 symbol would in some cases obscure the true definition of the
15675 enum, giving users an incomplete type when the definition is
15676 actually available. Note that we do not want to do this for all
15677 enums which are just declarations, because C++0x allows forward
15678 enum declarations. */
15679 if (cu->per_cu->is_debug_types
15680 && die_is_declaration (die, cu))
15681 {
15682 struct signatured_type *sig_type;
15683
15684 sig_type = (struct signatured_type *) cu->per_cu;
15685 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15686 if (sig_type->type_offset_in_section != die->sect_off)
15687 return;
15688 }
15689
15690 new_symbol (die, this_type, cu);
15691 }
15692
15693 /* Extract all information from a DW_TAG_array_type DIE and put it in
15694 the DIE's type field. For now, this only handles one dimensional
15695 arrays. */
15696
15697 static struct type *
15698 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15699 {
15700 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15701 struct die_info *child_die;
15702 struct type *type;
15703 struct type *element_type, *range_type, *index_type;
15704 struct attribute *attr;
15705 const char *name;
15706 struct dynamic_prop *byte_stride_prop = NULL;
15707 unsigned int bit_stride = 0;
15708
15709 element_type = die_type (die, cu);
15710
15711 /* The die_type call above may have already set the type for this DIE. */
15712 type = get_die_type (die, cu);
15713 if (type)
15714 return type;
15715
15716 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15717 if (attr != NULL)
15718 {
15719 int stride_ok;
15720 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
15721
15722 byte_stride_prop
15723 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15724 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15725 prop_type);
15726 if (!stride_ok)
15727 {
15728 complaint (_("unable to read array DW_AT_byte_stride "
15729 " - DIE at %s [in module %s]"),
15730 sect_offset_str (die->sect_off),
15731 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15732 /* Ignore this attribute. We will likely not be able to print
15733 arrays of this type correctly, but there is little we can do
15734 to help if we cannot read the attribute's value. */
15735 byte_stride_prop = NULL;
15736 }
15737 }
15738
15739 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15740 if (attr != NULL)
15741 bit_stride = DW_UNSND (attr);
15742
15743 /* Irix 6.2 native cc creates array types without children for
15744 arrays with unspecified length. */
15745 if (die->child == NULL)
15746 {
15747 index_type = objfile_type (objfile)->builtin_int;
15748 range_type = create_static_range_type (NULL, index_type, 0, -1);
15749 type = create_array_type_with_stride (NULL, element_type, range_type,
15750 byte_stride_prop, bit_stride);
15751 return set_die_type (die, type, cu);
15752 }
15753
15754 std::vector<struct type *> range_types;
15755 child_die = die->child;
15756 while (child_die && child_die->tag)
15757 {
15758 if (child_die->tag == DW_TAG_subrange_type)
15759 {
15760 struct type *child_type = read_type_die (child_die, cu);
15761
15762 if (child_type != NULL)
15763 {
15764 /* The range type was succesfully read. Save it for the
15765 array type creation. */
15766 range_types.push_back (child_type);
15767 }
15768 }
15769 child_die = child_die->sibling;
15770 }
15771
15772 /* Dwarf2 dimensions are output from left to right, create the
15773 necessary array types in backwards order. */
15774
15775 type = element_type;
15776
15777 if (read_array_order (die, cu) == DW_ORD_col_major)
15778 {
15779 int i = 0;
15780
15781 while (i < range_types.size ())
15782 type = create_array_type_with_stride (NULL, type, range_types[i++],
15783 byte_stride_prop, bit_stride);
15784 }
15785 else
15786 {
15787 size_t ndim = range_types.size ();
15788 while (ndim-- > 0)
15789 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15790 byte_stride_prop, bit_stride);
15791 }
15792
15793 /* Understand Dwarf2 support for vector types (like they occur on
15794 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15795 array type. This is not part of the Dwarf2/3 standard yet, but a
15796 custom vendor extension. The main difference between a regular
15797 array and the vector variant is that vectors are passed by value
15798 to functions. */
15799 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15800 if (attr != nullptr)
15801 make_vector_type (type);
15802
15803 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15804 implementation may choose to implement triple vectors using this
15805 attribute. */
15806 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15807 if (attr != nullptr)
15808 {
15809 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15810 TYPE_LENGTH (type) = DW_UNSND (attr);
15811 else
15812 complaint (_("DW_AT_byte_size for array type smaller "
15813 "than the total size of elements"));
15814 }
15815
15816 name = dwarf2_name (die, cu);
15817 if (name)
15818 TYPE_NAME (type) = name;
15819
15820 maybe_set_alignment (cu, die, type);
15821
15822 /* Install the type in the die. */
15823 set_die_type (die, type, cu);
15824
15825 /* set_die_type should be already done. */
15826 set_descriptive_type (type, die, cu);
15827
15828 return type;
15829 }
15830
15831 static enum dwarf_array_dim_ordering
15832 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15833 {
15834 struct attribute *attr;
15835
15836 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15837
15838 if (attr != nullptr)
15839 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15840
15841 /* GNU F77 is a special case, as at 08/2004 array type info is the
15842 opposite order to the dwarf2 specification, but data is still
15843 laid out as per normal fortran.
15844
15845 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15846 version checking. */
15847
15848 if (cu->language == language_fortran
15849 && cu->producer && strstr (cu->producer, "GNU F77"))
15850 {
15851 return DW_ORD_row_major;
15852 }
15853
15854 switch (cu->language_defn->la_array_ordering)
15855 {
15856 case array_column_major:
15857 return DW_ORD_col_major;
15858 case array_row_major:
15859 default:
15860 return DW_ORD_row_major;
15861 };
15862 }
15863
15864 /* Extract all information from a DW_TAG_set_type DIE and put it in
15865 the DIE's type field. */
15866
15867 static struct type *
15868 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15869 {
15870 struct type *domain_type, *set_type;
15871 struct attribute *attr;
15872
15873 domain_type = die_type (die, cu);
15874
15875 /* The die_type call above may have already set the type for this DIE. */
15876 set_type = get_die_type (die, cu);
15877 if (set_type)
15878 return set_type;
15879
15880 set_type = create_set_type (NULL, domain_type);
15881
15882 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15883 if (attr != nullptr)
15884 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15885
15886 maybe_set_alignment (cu, die, set_type);
15887
15888 return set_die_type (die, set_type, cu);
15889 }
15890
15891 /* A helper for read_common_block that creates a locexpr baton.
15892 SYM is the symbol which we are marking as computed.
15893 COMMON_DIE is the DIE for the common block.
15894 COMMON_LOC is the location expression attribute for the common
15895 block itself.
15896 MEMBER_LOC is the location expression attribute for the particular
15897 member of the common block that we are processing.
15898 CU is the CU from which the above come. */
15899
15900 static void
15901 mark_common_block_symbol_computed (struct symbol *sym,
15902 struct die_info *common_die,
15903 struct attribute *common_loc,
15904 struct attribute *member_loc,
15905 struct dwarf2_cu *cu)
15906 {
15907 struct dwarf2_per_objfile *dwarf2_per_objfile
15908 = cu->per_cu->dwarf2_per_objfile;
15909 struct objfile *objfile = dwarf2_per_objfile->objfile;
15910 struct dwarf2_locexpr_baton *baton;
15911 gdb_byte *ptr;
15912 unsigned int cu_off;
15913 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15914 LONGEST offset = 0;
15915
15916 gdb_assert (common_loc && member_loc);
15917 gdb_assert (common_loc->form_is_block ());
15918 gdb_assert (member_loc->form_is_block ()
15919 || member_loc->form_is_constant ());
15920
15921 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15922 baton->per_cu = cu->per_cu;
15923 gdb_assert (baton->per_cu);
15924
15925 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15926
15927 if (member_loc->form_is_constant ())
15928 {
15929 offset = member_loc->constant_value (0);
15930 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15931 }
15932 else
15933 baton->size += DW_BLOCK (member_loc)->size;
15934
15935 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15936 baton->data = ptr;
15937
15938 *ptr++ = DW_OP_call4;
15939 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15940 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15941 ptr += 4;
15942
15943 if (member_loc->form_is_constant ())
15944 {
15945 *ptr++ = DW_OP_addr;
15946 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15947 ptr += cu->header.addr_size;
15948 }
15949 else
15950 {
15951 /* We have to copy the data here, because DW_OP_call4 will only
15952 use a DW_AT_location attribute. */
15953 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15954 ptr += DW_BLOCK (member_loc)->size;
15955 }
15956
15957 *ptr++ = DW_OP_plus;
15958 gdb_assert (ptr - baton->data == baton->size);
15959
15960 SYMBOL_LOCATION_BATON (sym) = baton;
15961 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
15962 }
15963
15964 /* Create appropriate locally-scoped variables for all the
15965 DW_TAG_common_block entries. Also create a struct common_block
15966 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15967 is used to separate the common blocks name namespace from regular
15968 variable names. */
15969
15970 static void
15971 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15972 {
15973 struct attribute *attr;
15974
15975 attr = dwarf2_attr (die, DW_AT_location, cu);
15976 if (attr != nullptr)
15977 {
15978 /* Support the .debug_loc offsets. */
15979 if (attr->form_is_block ())
15980 {
15981 /* Ok. */
15982 }
15983 else if (attr->form_is_section_offset ())
15984 {
15985 dwarf2_complex_location_expr_complaint ();
15986 attr = NULL;
15987 }
15988 else
15989 {
15990 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15991 "common block member");
15992 attr = NULL;
15993 }
15994 }
15995
15996 if (die->child != NULL)
15997 {
15998 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15999 struct die_info *child_die;
16000 size_t n_entries = 0, size;
16001 struct common_block *common_block;
16002 struct symbol *sym;
16003
16004 for (child_die = die->child;
16005 child_die && child_die->tag;
16006 child_die = child_die->sibling)
16007 ++n_entries;
16008
16009 size = (sizeof (struct common_block)
16010 + (n_entries - 1) * sizeof (struct symbol *));
16011 common_block
16012 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16013 size);
16014 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16015 common_block->n_entries = 0;
16016
16017 for (child_die = die->child;
16018 child_die && child_die->tag;
16019 child_die = child_die->sibling)
16020 {
16021 /* Create the symbol in the DW_TAG_common_block block in the current
16022 symbol scope. */
16023 sym = new_symbol (child_die, NULL, cu);
16024 if (sym != NULL)
16025 {
16026 struct attribute *member_loc;
16027
16028 common_block->contents[common_block->n_entries++] = sym;
16029
16030 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16031 cu);
16032 if (member_loc)
16033 {
16034 /* GDB has handled this for a long time, but it is
16035 not specified by DWARF. It seems to have been
16036 emitted by gfortran at least as recently as:
16037 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16038 complaint (_("Variable in common block has "
16039 "DW_AT_data_member_location "
16040 "- DIE at %s [in module %s]"),
16041 sect_offset_str (child_die->sect_off),
16042 objfile_name (objfile));
16043
16044 if (member_loc->form_is_section_offset ())
16045 dwarf2_complex_location_expr_complaint ();
16046 else if (member_loc->form_is_constant ()
16047 || member_loc->form_is_block ())
16048 {
16049 if (attr != nullptr)
16050 mark_common_block_symbol_computed (sym, die, attr,
16051 member_loc, cu);
16052 }
16053 else
16054 dwarf2_complex_location_expr_complaint ();
16055 }
16056 }
16057 }
16058
16059 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16060 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16061 }
16062 }
16063
16064 /* Create a type for a C++ namespace. */
16065
16066 static struct type *
16067 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16068 {
16069 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16070 const char *previous_prefix, *name;
16071 int is_anonymous;
16072 struct type *type;
16073
16074 /* For extensions, reuse the type of the original namespace. */
16075 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16076 {
16077 struct die_info *ext_die;
16078 struct dwarf2_cu *ext_cu = cu;
16079
16080 ext_die = dwarf2_extension (die, &ext_cu);
16081 type = read_type_die (ext_die, ext_cu);
16082
16083 /* EXT_CU may not be the same as CU.
16084 Ensure TYPE is recorded with CU in die_type_hash. */
16085 return set_die_type (die, type, cu);
16086 }
16087
16088 name = namespace_name (die, &is_anonymous, cu);
16089
16090 /* Now build the name of the current namespace. */
16091
16092 previous_prefix = determine_prefix (die, cu);
16093 if (previous_prefix[0] != '\0')
16094 name = typename_concat (&objfile->objfile_obstack,
16095 previous_prefix, name, 0, cu);
16096
16097 /* Create the type. */
16098 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16099
16100 return set_die_type (die, type, cu);
16101 }
16102
16103 /* Read a namespace scope. */
16104
16105 static void
16106 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16107 {
16108 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16109 int is_anonymous;
16110
16111 /* Add a symbol associated to this if we haven't seen the namespace
16112 before. Also, add a using directive if it's an anonymous
16113 namespace. */
16114
16115 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16116 {
16117 struct type *type;
16118
16119 type = read_type_die (die, cu);
16120 new_symbol (die, type, cu);
16121
16122 namespace_name (die, &is_anonymous, cu);
16123 if (is_anonymous)
16124 {
16125 const char *previous_prefix = determine_prefix (die, cu);
16126
16127 std::vector<const char *> excludes;
16128 add_using_directive (using_directives (cu),
16129 previous_prefix, TYPE_NAME (type), NULL,
16130 NULL, excludes, 0, &objfile->objfile_obstack);
16131 }
16132 }
16133
16134 if (die->child != NULL)
16135 {
16136 struct die_info *child_die = die->child;
16137
16138 while (child_die && child_die->tag)
16139 {
16140 process_die (child_die, cu);
16141 child_die = child_die->sibling;
16142 }
16143 }
16144 }
16145
16146 /* Read a Fortran module as type. This DIE can be only a declaration used for
16147 imported module. Still we need that type as local Fortran "use ... only"
16148 declaration imports depend on the created type in determine_prefix. */
16149
16150 static struct type *
16151 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16152 {
16153 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16154 const char *module_name;
16155 struct type *type;
16156
16157 module_name = dwarf2_name (die, cu);
16158 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16159
16160 return set_die_type (die, type, cu);
16161 }
16162
16163 /* Read a Fortran module. */
16164
16165 static void
16166 read_module (struct die_info *die, struct dwarf2_cu *cu)
16167 {
16168 struct die_info *child_die = die->child;
16169 struct type *type;
16170
16171 type = read_type_die (die, cu);
16172 new_symbol (die, type, cu);
16173
16174 while (child_die && child_die->tag)
16175 {
16176 process_die (child_die, cu);
16177 child_die = child_die->sibling;
16178 }
16179 }
16180
16181 /* Return the name of the namespace represented by DIE. Set
16182 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16183 namespace. */
16184
16185 static const char *
16186 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16187 {
16188 struct die_info *current_die;
16189 const char *name = NULL;
16190
16191 /* Loop through the extensions until we find a name. */
16192
16193 for (current_die = die;
16194 current_die != NULL;
16195 current_die = dwarf2_extension (die, &cu))
16196 {
16197 /* We don't use dwarf2_name here so that we can detect the absence
16198 of a name -> anonymous namespace. */
16199 name = dwarf2_string_attr (die, DW_AT_name, cu);
16200
16201 if (name != NULL)
16202 break;
16203 }
16204
16205 /* Is it an anonymous namespace? */
16206
16207 *is_anonymous = (name == NULL);
16208 if (*is_anonymous)
16209 name = CP_ANONYMOUS_NAMESPACE_STR;
16210
16211 return name;
16212 }
16213
16214 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16215 the user defined type vector. */
16216
16217 static struct type *
16218 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16219 {
16220 struct gdbarch *gdbarch
16221 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16222 struct comp_unit_head *cu_header = &cu->header;
16223 struct type *type;
16224 struct attribute *attr_byte_size;
16225 struct attribute *attr_address_class;
16226 int byte_size, addr_class;
16227 struct type *target_type;
16228
16229 target_type = die_type (die, cu);
16230
16231 /* The die_type call above may have already set the type for this DIE. */
16232 type = get_die_type (die, cu);
16233 if (type)
16234 return type;
16235
16236 type = lookup_pointer_type (target_type);
16237
16238 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16239 if (attr_byte_size)
16240 byte_size = DW_UNSND (attr_byte_size);
16241 else
16242 byte_size = cu_header->addr_size;
16243
16244 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16245 if (attr_address_class)
16246 addr_class = DW_UNSND (attr_address_class);
16247 else
16248 addr_class = DW_ADDR_none;
16249
16250 ULONGEST alignment = get_alignment (cu, die);
16251
16252 /* If the pointer size, alignment, or address class is different
16253 than the default, create a type variant marked as such and set
16254 the length accordingly. */
16255 if (TYPE_LENGTH (type) != byte_size
16256 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16257 && alignment != TYPE_RAW_ALIGN (type))
16258 || addr_class != DW_ADDR_none)
16259 {
16260 if (gdbarch_address_class_type_flags_p (gdbarch))
16261 {
16262 int type_flags;
16263
16264 type_flags = gdbarch_address_class_type_flags
16265 (gdbarch, byte_size, addr_class);
16266 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16267 == 0);
16268 type = make_type_with_address_space (type, type_flags);
16269 }
16270 else if (TYPE_LENGTH (type) != byte_size)
16271 {
16272 complaint (_("invalid pointer size %d"), byte_size);
16273 }
16274 else if (TYPE_RAW_ALIGN (type) != alignment)
16275 {
16276 complaint (_("Invalid DW_AT_alignment"
16277 " - DIE at %s [in module %s]"),
16278 sect_offset_str (die->sect_off),
16279 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16280 }
16281 else
16282 {
16283 /* Should we also complain about unhandled address classes? */
16284 }
16285 }
16286
16287 TYPE_LENGTH (type) = byte_size;
16288 set_type_align (type, alignment);
16289 return set_die_type (die, type, cu);
16290 }
16291
16292 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16293 the user defined type vector. */
16294
16295 static struct type *
16296 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16297 {
16298 struct type *type;
16299 struct type *to_type;
16300 struct type *domain;
16301
16302 to_type = die_type (die, cu);
16303 domain = die_containing_type (die, cu);
16304
16305 /* The calls above may have already set the type for this DIE. */
16306 type = get_die_type (die, cu);
16307 if (type)
16308 return type;
16309
16310 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16311 type = lookup_methodptr_type (to_type);
16312 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16313 {
16314 struct type *new_type
16315 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16316
16317 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16318 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16319 TYPE_VARARGS (to_type));
16320 type = lookup_methodptr_type (new_type);
16321 }
16322 else
16323 type = lookup_memberptr_type (to_type, domain);
16324
16325 return set_die_type (die, type, cu);
16326 }
16327
16328 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16329 the user defined type vector. */
16330
16331 static struct type *
16332 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16333 enum type_code refcode)
16334 {
16335 struct comp_unit_head *cu_header = &cu->header;
16336 struct type *type, *target_type;
16337 struct attribute *attr;
16338
16339 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16340
16341 target_type = die_type (die, cu);
16342
16343 /* The die_type call above may have already set the type for this DIE. */
16344 type = get_die_type (die, cu);
16345 if (type)
16346 return type;
16347
16348 type = lookup_reference_type (target_type, refcode);
16349 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16350 if (attr != nullptr)
16351 {
16352 TYPE_LENGTH (type) = DW_UNSND (attr);
16353 }
16354 else
16355 {
16356 TYPE_LENGTH (type) = cu_header->addr_size;
16357 }
16358 maybe_set_alignment (cu, die, type);
16359 return set_die_type (die, type, cu);
16360 }
16361
16362 /* Add the given cv-qualifiers to the element type of the array. GCC
16363 outputs DWARF type qualifiers that apply to an array, not the
16364 element type. But GDB relies on the array element type to carry
16365 the cv-qualifiers. This mimics section 6.7.3 of the C99
16366 specification. */
16367
16368 static struct type *
16369 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16370 struct type *base_type, int cnst, int voltl)
16371 {
16372 struct type *el_type, *inner_array;
16373
16374 base_type = copy_type (base_type);
16375 inner_array = base_type;
16376
16377 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16378 {
16379 TYPE_TARGET_TYPE (inner_array) =
16380 copy_type (TYPE_TARGET_TYPE (inner_array));
16381 inner_array = TYPE_TARGET_TYPE (inner_array);
16382 }
16383
16384 el_type = TYPE_TARGET_TYPE (inner_array);
16385 cnst |= TYPE_CONST (el_type);
16386 voltl |= TYPE_VOLATILE (el_type);
16387 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16388
16389 return set_die_type (die, base_type, cu);
16390 }
16391
16392 static struct type *
16393 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16394 {
16395 struct type *base_type, *cv_type;
16396
16397 base_type = die_type (die, cu);
16398
16399 /* The die_type call above may have already set the type for this DIE. */
16400 cv_type = get_die_type (die, cu);
16401 if (cv_type)
16402 return cv_type;
16403
16404 /* In case the const qualifier is applied to an array type, the element type
16405 is so qualified, not the array type (section 6.7.3 of C99). */
16406 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16407 return add_array_cv_type (die, cu, base_type, 1, 0);
16408
16409 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16410 return set_die_type (die, cv_type, cu);
16411 }
16412
16413 static struct type *
16414 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16415 {
16416 struct type *base_type, *cv_type;
16417
16418 base_type = die_type (die, cu);
16419
16420 /* The die_type call above may have already set the type for this DIE. */
16421 cv_type = get_die_type (die, cu);
16422 if (cv_type)
16423 return cv_type;
16424
16425 /* In case the volatile qualifier is applied to an array type, the
16426 element type is so qualified, not the array type (section 6.7.3
16427 of C99). */
16428 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16429 return add_array_cv_type (die, cu, base_type, 0, 1);
16430
16431 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16432 return set_die_type (die, cv_type, cu);
16433 }
16434
16435 /* Handle DW_TAG_restrict_type. */
16436
16437 static struct type *
16438 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16439 {
16440 struct type *base_type, *cv_type;
16441
16442 base_type = die_type (die, cu);
16443
16444 /* The die_type call above may have already set the type for this DIE. */
16445 cv_type = get_die_type (die, cu);
16446 if (cv_type)
16447 return cv_type;
16448
16449 cv_type = make_restrict_type (base_type);
16450 return set_die_type (die, cv_type, cu);
16451 }
16452
16453 /* Handle DW_TAG_atomic_type. */
16454
16455 static struct type *
16456 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16457 {
16458 struct type *base_type, *cv_type;
16459
16460 base_type = die_type (die, cu);
16461
16462 /* The die_type call above may have already set the type for this DIE. */
16463 cv_type = get_die_type (die, cu);
16464 if (cv_type)
16465 return cv_type;
16466
16467 cv_type = make_atomic_type (base_type);
16468 return set_die_type (die, cv_type, cu);
16469 }
16470
16471 /* Extract all information from a DW_TAG_string_type DIE and add to
16472 the user defined type vector. It isn't really a user defined type,
16473 but it behaves like one, with other DIE's using an AT_user_def_type
16474 attribute to reference it. */
16475
16476 static struct type *
16477 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16478 {
16479 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16480 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16481 struct type *type, *range_type, *index_type, *char_type;
16482 struct attribute *attr;
16483 struct dynamic_prop prop;
16484 bool length_is_constant = true;
16485 LONGEST length;
16486
16487 /* There are a couple of places where bit sizes might be made use of
16488 when parsing a DW_TAG_string_type, however, no producer that we know
16489 of make use of these. Handling bit sizes that are a multiple of the
16490 byte size is easy enough, but what about other bit sizes? Lets deal
16491 with that problem when we have to. Warn about these attributes being
16492 unsupported, then parse the type and ignore them like we always
16493 have. */
16494 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16495 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16496 {
16497 static bool warning_printed = false;
16498 if (!warning_printed)
16499 {
16500 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16501 "currently supported on DW_TAG_string_type."));
16502 warning_printed = true;
16503 }
16504 }
16505
16506 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16507 if (attr != nullptr && !attr->form_is_constant ())
16508 {
16509 /* The string length describes the location at which the length of
16510 the string can be found. The size of the length field can be
16511 specified with one of the attributes below. */
16512 struct type *prop_type;
16513 struct attribute *len
16514 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16515 if (len == nullptr)
16516 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16517 if (len != nullptr && len->form_is_constant ())
16518 {
16519 /* Pass 0 as the default as we know this attribute is constant
16520 and the default value will not be returned. */
16521 LONGEST sz = len->constant_value (0);
16522 prop_type = cu->per_cu->int_type (sz, true);
16523 }
16524 else
16525 {
16526 /* If the size is not specified then we assume it is the size of
16527 an address on this target. */
16528 prop_type = cu->per_cu->addr_sized_int_type (true);
16529 }
16530
16531 /* Convert the attribute into a dynamic property. */
16532 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16533 length = 1;
16534 else
16535 length_is_constant = false;
16536 }
16537 else if (attr != nullptr)
16538 {
16539 /* This DW_AT_string_length just contains the length with no
16540 indirection. There's no need to create a dynamic property in this
16541 case. Pass 0 for the default value as we know it will not be
16542 returned in this case. */
16543 length = attr->constant_value (0);
16544 }
16545 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16546 {
16547 /* We don't currently support non-constant byte sizes for strings. */
16548 length = attr->constant_value (1);
16549 }
16550 else
16551 {
16552 /* Use 1 as a fallback length if we have nothing else. */
16553 length = 1;
16554 }
16555
16556 index_type = objfile_type (objfile)->builtin_int;
16557 if (length_is_constant)
16558 range_type = create_static_range_type (NULL, index_type, 1, length);
16559 else
16560 {
16561 struct dynamic_prop low_bound;
16562
16563 low_bound.kind = PROP_CONST;
16564 low_bound.data.const_val = 1;
16565 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16566 }
16567 char_type = language_string_char_type (cu->language_defn, gdbarch);
16568 type = create_string_type (NULL, char_type, range_type);
16569
16570 return set_die_type (die, type, cu);
16571 }
16572
16573 /* Assuming that DIE corresponds to a function, returns nonzero
16574 if the function is prototyped. */
16575
16576 static int
16577 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16578 {
16579 struct attribute *attr;
16580
16581 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16582 if (attr && (DW_UNSND (attr) != 0))
16583 return 1;
16584
16585 /* The DWARF standard implies that the DW_AT_prototyped attribute
16586 is only meaningful for C, but the concept also extends to other
16587 languages that allow unprototyped functions (Eg: Objective C).
16588 For all other languages, assume that functions are always
16589 prototyped. */
16590 if (cu->language != language_c
16591 && cu->language != language_objc
16592 && cu->language != language_opencl)
16593 return 1;
16594
16595 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16596 prototyped and unprototyped functions; default to prototyped,
16597 since that is more common in modern code (and RealView warns
16598 about unprototyped functions). */
16599 if (producer_is_realview (cu->producer))
16600 return 1;
16601
16602 return 0;
16603 }
16604
16605 /* Handle DIES due to C code like:
16606
16607 struct foo
16608 {
16609 int (*funcp)(int a, long l);
16610 int b;
16611 };
16612
16613 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16614
16615 static struct type *
16616 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16617 {
16618 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16619 struct type *type; /* Type that this function returns. */
16620 struct type *ftype; /* Function that returns above type. */
16621 struct attribute *attr;
16622
16623 type = die_type (die, cu);
16624
16625 /* The die_type call above may have already set the type for this DIE. */
16626 ftype = get_die_type (die, cu);
16627 if (ftype)
16628 return ftype;
16629
16630 ftype = lookup_function_type (type);
16631
16632 if (prototyped_function_p (die, cu))
16633 TYPE_PROTOTYPED (ftype) = 1;
16634
16635 /* Store the calling convention in the type if it's available in
16636 the subroutine die. Otherwise set the calling convention to
16637 the default value DW_CC_normal. */
16638 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16639 if (attr != nullptr
16640 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16641 TYPE_CALLING_CONVENTION (ftype)
16642 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16643 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16644 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16645 else
16646 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16647
16648 /* Record whether the function returns normally to its caller or not
16649 if the DWARF producer set that information. */
16650 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16651 if (attr && (DW_UNSND (attr) != 0))
16652 TYPE_NO_RETURN (ftype) = 1;
16653
16654 /* We need to add the subroutine type to the die immediately so
16655 we don't infinitely recurse when dealing with parameters
16656 declared as the same subroutine type. */
16657 set_die_type (die, ftype, cu);
16658
16659 if (die->child != NULL)
16660 {
16661 struct type *void_type = objfile_type (objfile)->builtin_void;
16662 struct die_info *child_die;
16663 int nparams, iparams;
16664
16665 /* Count the number of parameters.
16666 FIXME: GDB currently ignores vararg functions, but knows about
16667 vararg member functions. */
16668 nparams = 0;
16669 child_die = die->child;
16670 while (child_die && child_die->tag)
16671 {
16672 if (child_die->tag == DW_TAG_formal_parameter)
16673 nparams++;
16674 else if (child_die->tag == DW_TAG_unspecified_parameters)
16675 TYPE_VARARGS (ftype) = 1;
16676 child_die = child_die->sibling;
16677 }
16678
16679 /* Allocate storage for parameters and fill them in. */
16680 TYPE_NFIELDS (ftype) = nparams;
16681 TYPE_FIELDS (ftype) = (struct field *)
16682 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16683
16684 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16685 even if we error out during the parameters reading below. */
16686 for (iparams = 0; iparams < nparams; iparams++)
16687 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16688
16689 iparams = 0;
16690 child_die = die->child;
16691 while (child_die && child_die->tag)
16692 {
16693 if (child_die->tag == DW_TAG_formal_parameter)
16694 {
16695 struct type *arg_type;
16696
16697 /* DWARF version 2 has no clean way to discern C++
16698 static and non-static member functions. G++ helps
16699 GDB by marking the first parameter for non-static
16700 member functions (which is the this pointer) as
16701 artificial. We pass this information to
16702 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16703
16704 DWARF version 3 added DW_AT_object_pointer, which GCC
16705 4.5 does not yet generate. */
16706 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16707 if (attr != nullptr)
16708 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16709 else
16710 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16711 arg_type = die_type (child_die, cu);
16712
16713 /* RealView does not mark THIS as const, which the testsuite
16714 expects. GCC marks THIS as const in method definitions,
16715 but not in the class specifications (GCC PR 43053). */
16716 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16717 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16718 {
16719 int is_this = 0;
16720 struct dwarf2_cu *arg_cu = cu;
16721 const char *name = dwarf2_name (child_die, cu);
16722
16723 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16724 if (attr != nullptr)
16725 {
16726 /* If the compiler emits this, use it. */
16727 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16728 is_this = 1;
16729 }
16730 else if (name && strcmp (name, "this") == 0)
16731 /* Function definitions will have the argument names. */
16732 is_this = 1;
16733 else if (name == NULL && iparams == 0)
16734 /* Declarations may not have the names, so like
16735 elsewhere in GDB, assume an artificial first
16736 argument is "this". */
16737 is_this = 1;
16738
16739 if (is_this)
16740 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16741 arg_type, 0);
16742 }
16743
16744 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16745 iparams++;
16746 }
16747 child_die = child_die->sibling;
16748 }
16749 }
16750
16751 return ftype;
16752 }
16753
16754 static struct type *
16755 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16756 {
16757 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16758 const char *name = NULL;
16759 struct type *this_type, *target_type;
16760
16761 name = dwarf2_full_name (NULL, die, cu);
16762 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16763 TYPE_TARGET_STUB (this_type) = 1;
16764 set_die_type (die, this_type, cu);
16765 target_type = die_type (die, cu);
16766 if (target_type != this_type)
16767 TYPE_TARGET_TYPE (this_type) = target_type;
16768 else
16769 {
16770 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16771 spec and cause infinite loops in GDB. */
16772 complaint (_("Self-referential DW_TAG_typedef "
16773 "- DIE at %s [in module %s]"),
16774 sect_offset_str (die->sect_off), objfile_name (objfile));
16775 TYPE_TARGET_TYPE (this_type) = NULL;
16776 }
16777 if (name == NULL)
16778 {
16779 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16780 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16781 Handle these by just returning the target type, rather than
16782 constructing an anonymous typedef type and trying to handle this
16783 elsewhere. */
16784 set_die_type (die, target_type, cu);
16785 return target_type;
16786 }
16787 return this_type;
16788 }
16789
16790 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16791 (which may be different from NAME) to the architecture back-end to allow
16792 it to guess the correct format if necessary. */
16793
16794 static struct type *
16795 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16796 const char *name_hint, enum bfd_endian byte_order)
16797 {
16798 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16799 const struct floatformat **format;
16800 struct type *type;
16801
16802 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16803 if (format)
16804 type = init_float_type (objfile, bits, name, format, byte_order);
16805 else
16806 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16807
16808 return type;
16809 }
16810
16811 /* Allocate an integer type of size BITS and name NAME. */
16812
16813 static struct type *
16814 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16815 int bits, int unsigned_p, const char *name)
16816 {
16817 struct type *type;
16818
16819 /* Versions of Intel's C Compiler generate an integer type called "void"
16820 instead of using DW_TAG_unspecified_type. This has been seen on
16821 at least versions 14, 17, and 18. */
16822 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16823 && strcmp (name, "void") == 0)
16824 type = objfile_type (objfile)->builtin_void;
16825 else
16826 type = init_integer_type (objfile, bits, unsigned_p, name);
16827
16828 return type;
16829 }
16830
16831 /* Initialise and return a floating point type of size BITS suitable for
16832 use as a component of a complex number. The NAME_HINT is passed through
16833 when initialising the floating point type and is the name of the complex
16834 type.
16835
16836 As DWARF doesn't currently provide an explicit name for the components
16837 of a complex number, but it can be helpful to have these components
16838 named, we try to select a suitable name based on the size of the
16839 component. */
16840 static struct type *
16841 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16842 struct objfile *objfile,
16843 int bits, const char *name_hint,
16844 enum bfd_endian byte_order)
16845 {
16846 gdbarch *gdbarch = get_objfile_arch (objfile);
16847 struct type *tt = nullptr;
16848
16849 /* Try to find a suitable floating point builtin type of size BITS.
16850 We're going to use the name of this type as the name for the complex
16851 target type that we are about to create. */
16852 switch (cu->language)
16853 {
16854 case language_fortran:
16855 switch (bits)
16856 {
16857 case 32:
16858 tt = builtin_f_type (gdbarch)->builtin_real;
16859 break;
16860 case 64:
16861 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16862 break;
16863 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16864 case 128:
16865 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16866 break;
16867 }
16868 break;
16869 default:
16870 switch (bits)
16871 {
16872 case 32:
16873 tt = builtin_type (gdbarch)->builtin_float;
16874 break;
16875 case 64:
16876 tt = builtin_type (gdbarch)->builtin_double;
16877 break;
16878 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16879 case 128:
16880 tt = builtin_type (gdbarch)->builtin_long_double;
16881 break;
16882 }
16883 break;
16884 }
16885
16886 /* If the type we found doesn't match the size we were looking for, then
16887 pretend we didn't find a type at all, the complex target type we
16888 create will then be nameless. */
16889 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
16890 tt = nullptr;
16891
16892 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
16893 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
16894 }
16895
16896 /* Find a representation of a given base type and install
16897 it in the TYPE field of the die. */
16898
16899 static struct type *
16900 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16901 {
16902 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16903 struct type *type;
16904 struct attribute *attr;
16905 int encoding = 0, bits = 0;
16906 const char *name;
16907 gdbarch *arch;
16908
16909 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16910 if (attr != nullptr)
16911 encoding = DW_UNSND (attr);
16912 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16913 if (attr != nullptr)
16914 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16915 name = dwarf2_name (die, cu);
16916 if (!name)
16917 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
16918
16919 arch = get_objfile_arch (objfile);
16920 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16921
16922 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16923 if (attr)
16924 {
16925 int endianity = DW_UNSND (attr);
16926
16927 switch (endianity)
16928 {
16929 case DW_END_big:
16930 byte_order = BFD_ENDIAN_BIG;
16931 break;
16932 case DW_END_little:
16933 byte_order = BFD_ENDIAN_LITTLE;
16934 break;
16935 default:
16936 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16937 break;
16938 }
16939 }
16940
16941 switch (encoding)
16942 {
16943 case DW_ATE_address:
16944 /* Turn DW_ATE_address into a void * pointer. */
16945 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16946 type = init_pointer_type (objfile, bits, name, type);
16947 break;
16948 case DW_ATE_boolean:
16949 type = init_boolean_type (objfile, bits, 1, name);
16950 break;
16951 case DW_ATE_complex_float:
16952 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16953 byte_order);
16954 if (TYPE_CODE (type) == TYPE_CODE_ERROR)
16955 {
16956 if (name == nullptr)
16957 {
16958 struct obstack *obstack
16959 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
16960 name = obconcat (obstack, "_Complex ", TYPE_NAME (type),
16961 nullptr);
16962 }
16963 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16964 }
16965 else
16966 type = init_complex_type (name, type);
16967 break;
16968 case DW_ATE_decimal_float:
16969 type = init_decfloat_type (objfile, bits, name);
16970 break;
16971 case DW_ATE_float:
16972 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
16973 break;
16974 case DW_ATE_signed:
16975 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16976 break;
16977 case DW_ATE_unsigned:
16978 if (cu->language == language_fortran
16979 && name
16980 && startswith (name, "character("))
16981 type = init_character_type (objfile, bits, 1, name);
16982 else
16983 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16984 break;
16985 case DW_ATE_signed_char:
16986 if (cu->language == language_ada || cu->language == language_m2
16987 || cu->language == language_pascal
16988 || cu->language == language_fortran)
16989 type = init_character_type (objfile, bits, 0, name);
16990 else
16991 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16992 break;
16993 case DW_ATE_unsigned_char:
16994 if (cu->language == language_ada || cu->language == language_m2
16995 || cu->language == language_pascal
16996 || cu->language == language_fortran
16997 || cu->language == language_rust)
16998 type = init_character_type (objfile, bits, 1, name);
16999 else
17000 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17001 break;
17002 case DW_ATE_UTF:
17003 {
17004 if (bits == 16)
17005 type = builtin_type (arch)->builtin_char16;
17006 else if (bits == 32)
17007 type = builtin_type (arch)->builtin_char32;
17008 else
17009 {
17010 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17011 bits);
17012 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17013 }
17014 return set_die_type (die, type, cu);
17015 }
17016 break;
17017
17018 default:
17019 complaint (_("unsupported DW_AT_encoding: '%s'"),
17020 dwarf_type_encoding_name (encoding));
17021 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17022 break;
17023 }
17024
17025 if (name && strcmp (name, "char") == 0)
17026 TYPE_NOSIGN (type) = 1;
17027
17028 maybe_set_alignment (cu, die, type);
17029
17030 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17031
17032 return set_die_type (die, type, cu);
17033 }
17034
17035 /* Parse dwarf attribute if it's a block, reference or constant and put the
17036 resulting value of the attribute into struct bound_prop.
17037 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17038
17039 static int
17040 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17041 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17042 struct type *default_type)
17043 {
17044 struct dwarf2_property_baton *baton;
17045 struct obstack *obstack
17046 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17047
17048 gdb_assert (default_type != NULL);
17049
17050 if (attr == NULL || prop == NULL)
17051 return 0;
17052
17053 if (attr->form_is_block ())
17054 {
17055 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17056 baton->property_type = default_type;
17057 baton->locexpr.per_cu = cu->per_cu;
17058 baton->locexpr.size = DW_BLOCK (attr)->size;
17059 baton->locexpr.data = DW_BLOCK (attr)->data;
17060 switch (attr->name)
17061 {
17062 case DW_AT_string_length:
17063 baton->locexpr.is_reference = true;
17064 break;
17065 default:
17066 baton->locexpr.is_reference = false;
17067 break;
17068 }
17069 prop->data.baton = baton;
17070 prop->kind = PROP_LOCEXPR;
17071 gdb_assert (prop->data.baton != NULL);
17072 }
17073 else if (attr->form_is_ref ())
17074 {
17075 struct dwarf2_cu *target_cu = cu;
17076 struct die_info *target_die;
17077 struct attribute *target_attr;
17078
17079 target_die = follow_die_ref (die, attr, &target_cu);
17080 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17081 if (target_attr == NULL)
17082 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17083 target_cu);
17084 if (target_attr == NULL)
17085 return 0;
17086
17087 switch (target_attr->name)
17088 {
17089 case DW_AT_location:
17090 if (target_attr->form_is_section_offset ())
17091 {
17092 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17093 baton->property_type = die_type (target_die, target_cu);
17094 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17095 prop->data.baton = baton;
17096 prop->kind = PROP_LOCLIST;
17097 gdb_assert (prop->data.baton != NULL);
17098 }
17099 else if (target_attr->form_is_block ())
17100 {
17101 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17102 baton->property_type = die_type (target_die, target_cu);
17103 baton->locexpr.per_cu = cu->per_cu;
17104 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17105 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17106 baton->locexpr.is_reference = true;
17107 prop->data.baton = baton;
17108 prop->kind = PROP_LOCEXPR;
17109 gdb_assert (prop->data.baton != NULL);
17110 }
17111 else
17112 {
17113 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17114 "dynamic property");
17115 return 0;
17116 }
17117 break;
17118 case DW_AT_data_member_location:
17119 {
17120 LONGEST offset;
17121
17122 if (!handle_data_member_location (target_die, target_cu,
17123 &offset))
17124 return 0;
17125
17126 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17127 baton->property_type = read_type_die (target_die->parent,
17128 target_cu);
17129 baton->offset_info.offset = offset;
17130 baton->offset_info.type = die_type (target_die, target_cu);
17131 prop->data.baton = baton;
17132 prop->kind = PROP_ADDR_OFFSET;
17133 break;
17134 }
17135 }
17136 }
17137 else if (attr->form_is_constant ())
17138 {
17139 prop->data.const_val = attr->constant_value (0);
17140 prop->kind = PROP_CONST;
17141 }
17142 else
17143 {
17144 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17145 dwarf2_name (die, cu));
17146 return 0;
17147 }
17148
17149 return 1;
17150 }
17151
17152 /* See read.h. */
17153
17154 struct type *
17155 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17156 {
17157 struct objfile *objfile = dwarf2_per_objfile->objfile;
17158 struct type *int_type;
17159
17160 /* Helper macro to examine the various builtin types. */
17161 #define TRY_TYPE(F) \
17162 int_type = (unsigned_p \
17163 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17164 : objfile_type (objfile)->builtin_ ## F); \
17165 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17166 return int_type
17167
17168 TRY_TYPE (char);
17169 TRY_TYPE (short);
17170 TRY_TYPE (int);
17171 TRY_TYPE (long);
17172 TRY_TYPE (long_long);
17173
17174 #undef TRY_TYPE
17175
17176 gdb_assert_not_reached ("unable to find suitable integer type");
17177 }
17178
17179 /* See read.h. */
17180
17181 struct type *
17182 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17183 {
17184 int addr_size = this->addr_size ();
17185 return int_type (addr_size, unsigned_p);
17186 }
17187
17188 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17189 present (which is valid) then compute the default type based on the
17190 compilation units address size. */
17191
17192 static struct type *
17193 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17194 {
17195 struct type *index_type = die_type (die, cu);
17196
17197 /* Dwarf-2 specifications explicitly allows to create subrange types
17198 without specifying a base type.
17199 In that case, the base type must be set to the type of
17200 the lower bound, upper bound or count, in that order, if any of these
17201 three attributes references an object that has a type.
17202 If no base type is found, the Dwarf-2 specifications say that
17203 a signed integer type of size equal to the size of an address should
17204 be used.
17205 For the following C code: `extern char gdb_int [];'
17206 GCC produces an empty range DIE.
17207 FIXME: muller/2010-05-28: Possible references to object for low bound,
17208 high bound or count are not yet handled by this code. */
17209 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17210 index_type = cu->per_cu->addr_sized_int_type (false);
17211
17212 return index_type;
17213 }
17214
17215 /* Read the given DW_AT_subrange DIE. */
17216
17217 static struct type *
17218 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17219 {
17220 struct type *base_type, *orig_base_type;
17221 struct type *range_type;
17222 struct attribute *attr;
17223 struct dynamic_prop low, high;
17224 int low_default_is_valid;
17225 int high_bound_is_count = 0;
17226 const char *name;
17227 ULONGEST negative_mask;
17228
17229 orig_base_type = read_subrange_index_type (die, cu);
17230
17231 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17232 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17233 creating the range type, but we use the result of check_typedef
17234 when examining properties of the type. */
17235 base_type = check_typedef (orig_base_type);
17236
17237 /* The die_type call above may have already set the type for this DIE. */
17238 range_type = get_die_type (die, cu);
17239 if (range_type)
17240 return range_type;
17241
17242 low.kind = PROP_CONST;
17243 high.kind = PROP_CONST;
17244 high.data.const_val = 0;
17245
17246 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17247 omitting DW_AT_lower_bound. */
17248 switch (cu->language)
17249 {
17250 case language_c:
17251 case language_cplus:
17252 low.data.const_val = 0;
17253 low_default_is_valid = 1;
17254 break;
17255 case language_fortran:
17256 low.data.const_val = 1;
17257 low_default_is_valid = 1;
17258 break;
17259 case language_d:
17260 case language_objc:
17261 case language_rust:
17262 low.data.const_val = 0;
17263 low_default_is_valid = (cu->header.version >= 4);
17264 break;
17265 case language_ada:
17266 case language_m2:
17267 case language_pascal:
17268 low.data.const_val = 1;
17269 low_default_is_valid = (cu->header.version >= 4);
17270 break;
17271 default:
17272 low.data.const_val = 0;
17273 low_default_is_valid = 0;
17274 break;
17275 }
17276
17277 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17278 if (attr != nullptr)
17279 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17280 else if (!low_default_is_valid)
17281 complaint (_("Missing DW_AT_lower_bound "
17282 "- DIE at %s [in module %s]"),
17283 sect_offset_str (die->sect_off),
17284 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17285
17286 struct attribute *attr_ub, *attr_count;
17287 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17288 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17289 {
17290 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17291 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17292 {
17293 /* If bounds are constant do the final calculation here. */
17294 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17295 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17296 else
17297 high_bound_is_count = 1;
17298 }
17299 else
17300 {
17301 if (attr_ub != NULL)
17302 complaint (_("Unresolved DW_AT_upper_bound "
17303 "- DIE at %s [in module %s]"),
17304 sect_offset_str (die->sect_off),
17305 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17306 if (attr_count != NULL)
17307 complaint (_("Unresolved DW_AT_count "
17308 "- DIE at %s [in module %s]"),
17309 sect_offset_str (die->sect_off),
17310 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17311 }
17312 }
17313
17314 LONGEST bias = 0;
17315 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17316 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17317 bias = bias_attr->constant_value (0);
17318
17319 /* Normally, the DWARF producers are expected to use a signed
17320 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17321 But this is unfortunately not always the case, as witnessed
17322 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17323 is used instead. To work around that ambiguity, we treat
17324 the bounds as signed, and thus sign-extend their values, when
17325 the base type is signed. */
17326 negative_mask =
17327 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17328 if (low.kind == PROP_CONST
17329 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17330 low.data.const_val |= negative_mask;
17331 if (high.kind == PROP_CONST
17332 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17333 high.data.const_val |= negative_mask;
17334
17335 /* Check for bit and byte strides. */
17336 struct dynamic_prop byte_stride_prop;
17337 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17338 if (attr_byte_stride != nullptr)
17339 {
17340 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17341 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17342 prop_type);
17343 }
17344
17345 struct dynamic_prop bit_stride_prop;
17346 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17347 if (attr_bit_stride != nullptr)
17348 {
17349 /* It only makes sense to have either a bit or byte stride. */
17350 if (attr_byte_stride != nullptr)
17351 {
17352 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17353 "- DIE at %s [in module %s]"),
17354 sect_offset_str (die->sect_off),
17355 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17356 attr_bit_stride = nullptr;
17357 }
17358 else
17359 {
17360 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17361 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17362 prop_type);
17363 }
17364 }
17365
17366 if (attr_byte_stride != nullptr
17367 || attr_bit_stride != nullptr)
17368 {
17369 bool byte_stride_p = (attr_byte_stride != nullptr);
17370 struct dynamic_prop *stride
17371 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17372
17373 range_type
17374 = create_range_type_with_stride (NULL, orig_base_type, &low,
17375 &high, bias, stride, byte_stride_p);
17376 }
17377 else
17378 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17379
17380 if (high_bound_is_count)
17381 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17382
17383 /* Ada expects an empty array on no boundary attributes. */
17384 if (attr == NULL && cu->language != language_ada)
17385 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17386
17387 name = dwarf2_name (die, cu);
17388 if (name)
17389 TYPE_NAME (range_type) = name;
17390
17391 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17392 if (attr != nullptr)
17393 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17394
17395 maybe_set_alignment (cu, die, range_type);
17396
17397 set_die_type (die, range_type, cu);
17398
17399 /* set_die_type should be already done. */
17400 set_descriptive_type (range_type, die, cu);
17401
17402 return range_type;
17403 }
17404
17405 static struct type *
17406 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17407 {
17408 struct type *type;
17409
17410 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17411 NULL);
17412 TYPE_NAME (type) = dwarf2_name (die, cu);
17413
17414 /* In Ada, an unspecified type is typically used when the description
17415 of the type is deferred to a different unit. When encountering
17416 such a type, we treat it as a stub, and try to resolve it later on,
17417 when needed. */
17418 if (cu->language == language_ada)
17419 TYPE_STUB (type) = 1;
17420
17421 return set_die_type (die, type, cu);
17422 }
17423
17424 /* Read a single die and all its descendents. Set the die's sibling
17425 field to NULL; set other fields in the die correctly, and set all
17426 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17427 location of the info_ptr after reading all of those dies. PARENT
17428 is the parent of the die in question. */
17429
17430 static struct die_info *
17431 read_die_and_children (const struct die_reader_specs *reader,
17432 const gdb_byte *info_ptr,
17433 const gdb_byte **new_info_ptr,
17434 struct die_info *parent)
17435 {
17436 struct die_info *die;
17437 const gdb_byte *cur_ptr;
17438
17439 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17440 if (die == NULL)
17441 {
17442 *new_info_ptr = cur_ptr;
17443 return NULL;
17444 }
17445 store_in_ref_table (die, reader->cu);
17446
17447 if (die->has_children)
17448 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17449 else
17450 {
17451 die->child = NULL;
17452 *new_info_ptr = cur_ptr;
17453 }
17454
17455 die->sibling = NULL;
17456 die->parent = parent;
17457 return die;
17458 }
17459
17460 /* Read a die, all of its descendents, and all of its siblings; set
17461 all of the fields of all of the dies correctly. Arguments are as
17462 in read_die_and_children. */
17463
17464 static struct die_info *
17465 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17466 const gdb_byte *info_ptr,
17467 const gdb_byte **new_info_ptr,
17468 struct die_info *parent)
17469 {
17470 struct die_info *first_die, *last_sibling;
17471 const gdb_byte *cur_ptr;
17472
17473 cur_ptr = info_ptr;
17474 first_die = last_sibling = NULL;
17475
17476 while (1)
17477 {
17478 struct die_info *die
17479 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17480
17481 if (die == NULL)
17482 {
17483 *new_info_ptr = cur_ptr;
17484 return first_die;
17485 }
17486
17487 if (!first_die)
17488 first_die = die;
17489 else
17490 last_sibling->sibling = die;
17491
17492 last_sibling = die;
17493 }
17494 }
17495
17496 /* Read a die, all of its descendents, and all of its siblings; set
17497 all of the fields of all of the dies correctly. Arguments are as
17498 in read_die_and_children.
17499 This the main entry point for reading a DIE and all its children. */
17500
17501 static struct die_info *
17502 read_die_and_siblings (const struct die_reader_specs *reader,
17503 const gdb_byte *info_ptr,
17504 const gdb_byte **new_info_ptr,
17505 struct die_info *parent)
17506 {
17507 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17508 new_info_ptr, parent);
17509
17510 if (dwarf_die_debug)
17511 {
17512 fprintf_unfiltered (gdb_stdlog,
17513 "Read die from %s@0x%x of %s:\n",
17514 reader->die_section->get_name (),
17515 (unsigned) (info_ptr - reader->die_section->buffer),
17516 bfd_get_filename (reader->abfd));
17517 dump_die (die, dwarf_die_debug);
17518 }
17519
17520 return die;
17521 }
17522
17523 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17524 attributes.
17525 The caller is responsible for filling in the extra attributes
17526 and updating (*DIEP)->num_attrs.
17527 Set DIEP to point to a newly allocated die with its information,
17528 except for its child, sibling, and parent fields. */
17529
17530 static const gdb_byte *
17531 read_full_die_1 (const struct die_reader_specs *reader,
17532 struct die_info **diep, const gdb_byte *info_ptr,
17533 int num_extra_attrs)
17534 {
17535 unsigned int abbrev_number, bytes_read, i;
17536 struct abbrev_info *abbrev;
17537 struct die_info *die;
17538 struct dwarf2_cu *cu = reader->cu;
17539 bfd *abfd = reader->abfd;
17540
17541 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17542 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17543 info_ptr += bytes_read;
17544 if (!abbrev_number)
17545 {
17546 *diep = NULL;
17547 return info_ptr;
17548 }
17549
17550 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17551 if (!abbrev)
17552 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17553 abbrev_number,
17554 bfd_get_filename (abfd));
17555
17556 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17557 die->sect_off = sect_off;
17558 die->tag = abbrev->tag;
17559 die->abbrev = abbrev_number;
17560 die->has_children = abbrev->has_children;
17561
17562 /* Make the result usable.
17563 The caller needs to update num_attrs after adding the extra
17564 attributes. */
17565 die->num_attrs = abbrev->num_attrs;
17566
17567 std::vector<int> indexes_that_need_reprocess;
17568 for (i = 0; i < abbrev->num_attrs; ++i)
17569 {
17570 bool need_reprocess;
17571 info_ptr =
17572 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17573 info_ptr, &need_reprocess);
17574 if (need_reprocess)
17575 indexes_that_need_reprocess.push_back (i);
17576 }
17577
17578 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
17579 if (attr != nullptr)
17580 cu->str_offsets_base = DW_UNSND (attr);
17581
17582 attr = die->attr (DW_AT_loclists_base);
17583 if (attr != nullptr)
17584 cu->loclist_base = DW_UNSND (attr);
17585
17586 auto maybe_addr_base = die->addr_base ();
17587 if (maybe_addr_base.has_value ())
17588 cu->addr_base = *maybe_addr_base;
17589 for (int index : indexes_that_need_reprocess)
17590 read_attribute_reprocess (reader, &die->attrs[index]);
17591 *diep = die;
17592 return info_ptr;
17593 }
17594
17595 /* Read a die and all its attributes.
17596 Set DIEP to point to a newly allocated die with its information,
17597 except for its child, sibling, and parent fields. */
17598
17599 static const gdb_byte *
17600 read_full_die (const struct die_reader_specs *reader,
17601 struct die_info **diep, const gdb_byte *info_ptr)
17602 {
17603 const gdb_byte *result;
17604
17605 result = read_full_die_1 (reader, diep, info_ptr, 0);
17606
17607 if (dwarf_die_debug)
17608 {
17609 fprintf_unfiltered (gdb_stdlog,
17610 "Read die from %s@0x%x of %s:\n",
17611 reader->die_section->get_name (),
17612 (unsigned) (info_ptr - reader->die_section->buffer),
17613 bfd_get_filename (reader->abfd));
17614 dump_die (*diep, dwarf_die_debug);
17615 }
17616
17617 return result;
17618 }
17619 \f
17620
17621 /* Returns nonzero if TAG represents a type that we might generate a partial
17622 symbol for. */
17623
17624 static int
17625 is_type_tag_for_partial (int tag)
17626 {
17627 switch (tag)
17628 {
17629 #if 0
17630 /* Some types that would be reasonable to generate partial symbols for,
17631 that we don't at present. */
17632 case DW_TAG_array_type:
17633 case DW_TAG_file_type:
17634 case DW_TAG_ptr_to_member_type:
17635 case DW_TAG_set_type:
17636 case DW_TAG_string_type:
17637 case DW_TAG_subroutine_type:
17638 #endif
17639 case DW_TAG_base_type:
17640 case DW_TAG_class_type:
17641 case DW_TAG_interface_type:
17642 case DW_TAG_enumeration_type:
17643 case DW_TAG_structure_type:
17644 case DW_TAG_subrange_type:
17645 case DW_TAG_typedef:
17646 case DW_TAG_union_type:
17647 return 1;
17648 default:
17649 return 0;
17650 }
17651 }
17652
17653 /* Load all DIEs that are interesting for partial symbols into memory. */
17654
17655 static struct partial_die_info *
17656 load_partial_dies (const struct die_reader_specs *reader,
17657 const gdb_byte *info_ptr, int building_psymtab)
17658 {
17659 struct dwarf2_cu *cu = reader->cu;
17660 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17661 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17662 unsigned int bytes_read;
17663 unsigned int load_all = 0;
17664 int nesting_level = 1;
17665
17666 parent_die = NULL;
17667 last_die = NULL;
17668
17669 gdb_assert (cu->per_cu != NULL);
17670 if (cu->per_cu->load_all_dies)
17671 load_all = 1;
17672
17673 cu->partial_dies
17674 = htab_create_alloc_ex (cu->header.length / 12,
17675 partial_die_hash,
17676 partial_die_eq,
17677 NULL,
17678 &cu->comp_unit_obstack,
17679 hashtab_obstack_allocate,
17680 dummy_obstack_deallocate);
17681
17682 while (1)
17683 {
17684 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17685
17686 /* A NULL abbrev means the end of a series of children. */
17687 if (abbrev == NULL)
17688 {
17689 if (--nesting_level == 0)
17690 return first_die;
17691
17692 info_ptr += bytes_read;
17693 last_die = parent_die;
17694 parent_die = parent_die->die_parent;
17695 continue;
17696 }
17697
17698 /* Check for template arguments. We never save these; if
17699 they're seen, we just mark the parent, and go on our way. */
17700 if (parent_die != NULL
17701 && cu->language == language_cplus
17702 && (abbrev->tag == DW_TAG_template_type_param
17703 || abbrev->tag == DW_TAG_template_value_param))
17704 {
17705 parent_die->has_template_arguments = 1;
17706
17707 if (!load_all)
17708 {
17709 /* We don't need a partial DIE for the template argument. */
17710 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17711 continue;
17712 }
17713 }
17714
17715 /* We only recurse into c++ subprograms looking for template arguments.
17716 Skip their other children. */
17717 if (!load_all
17718 && cu->language == language_cplus
17719 && parent_die != NULL
17720 && parent_die->tag == DW_TAG_subprogram)
17721 {
17722 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17723 continue;
17724 }
17725
17726 /* Check whether this DIE is interesting enough to save. Normally
17727 we would not be interested in members here, but there may be
17728 later variables referencing them via DW_AT_specification (for
17729 static members). */
17730 if (!load_all
17731 && !is_type_tag_for_partial (abbrev->tag)
17732 && abbrev->tag != DW_TAG_constant
17733 && abbrev->tag != DW_TAG_enumerator
17734 && abbrev->tag != DW_TAG_subprogram
17735 && abbrev->tag != DW_TAG_inlined_subroutine
17736 && abbrev->tag != DW_TAG_lexical_block
17737 && abbrev->tag != DW_TAG_variable
17738 && abbrev->tag != DW_TAG_namespace
17739 && abbrev->tag != DW_TAG_module
17740 && abbrev->tag != DW_TAG_member
17741 && abbrev->tag != DW_TAG_imported_unit
17742 && abbrev->tag != DW_TAG_imported_declaration)
17743 {
17744 /* Otherwise we skip to the next sibling, if any. */
17745 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17746 continue;
17747 }
17748
17749 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17750 abbrev);
17751
17752 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
17753
17754 /* This two-pass algorithm for processing partial symbols has a
17755 high cost in cache pressure. Thus, handle some simple cases
17756 here which cover the majority of C partial symbols. DIEs
17757 which neither have specification tags in them, nor could have
17758 specification tags elsewhere pointing at them, can simply be
17759 processed and discarded.
17760
17761 This segment is also optional; scan_partial_symbols and
17762 add_partial_symbol will handle these DIEs if we chain
17763 them in normally. When compilers which do not emit large
17764 quantities of duplicate debug information are more common,
17765 this code can probably be removed. */
17766
17767 /* Any complete simple types at the top level (pretty much all
17768 of them, for a language without namespaces), can be processed
17769 directly. */
17770 if (parent_die == NULL
17771 && pdi.has_specification == 0
17772 && pdi.is_declaration == 0
17773 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17774 || pdi.tag == DW_TAG_base_type
17775 || pdi.tag == DW_TAG_subrange_type))
17776 {
17777 if (building_psymtab && pdi.name != NULL)
17778 add_psymbol_to_list (pdi.name, false,
17779 VAR_DOMAIN, LOC_TYPEDEF, -1,
17780 psymbol_placement::STATIC,
17781 0, cu->language, objfile);
17782 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17783 continue;
17784 }
17785
17786 /* The exception for DW_TAG_typedef with has_children above is
17787 a workaround of GCC PR debug/47510. In the case of this complaint
17788 type_name_or_error will error on such types later.
17789
17790 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17791 it could not find the child DIEs referenced later, this is checked
17792 above. In correct DWARF DW_TAG_typedef should have no children. */
17793
17794 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
17795 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17796 "- DIE at %s [in module %s]"),
17797 sect_offset_str (pdi.sect_off), objfile_name (objfile));
17798
17799 /* If we're at the second level, and we're an enumerator, and
17800 our parent has no specification (meaning possibly lives in a
17801 namespace elsewhere), then we can add the partial symbol now
17802 instead of queueing it. */
17803 if (pdi.tag == DW_TAG_enumerator
17804 && parent_die != NULL
17805 && parent_die->die_parent == NULL
17806 && parent_die->tag == DW_TAG_enumeration_type
17807 && parent_die->has_specification == 0)
17808 {
17809 if (pdi.name == NULL)
17810 complaint (_("malformed enumerator DIE ignored"));
17811 else if (building_psymtab)
17812 add_psymbol_to_list (pdi.name, false,
17813 VAR_DOMAIN, LOC_CONST, -1,
17814 cu->language == language_cplus
17815 ? psymbol_placement::GLOBAL
17816 : psymbol_placement::STATIC,
17817 0, cu->language, objfile);
17818
17819 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17820 continue;
17821 }
17822
17823 struct partial_die_info *part_die
17824 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
17825
17826 /* We'll save this DIE so link it in. */
17827 part_die->die_parent = parent_die;
17828 part_die->die_sibling = NULL;
17829 part_die->die_child = NULL;
17830
17831 if (last_die && last_die == parent_die)
17832 last_die->die_child = part_die;
17833 else if (last_die)
17834 last_die->die_sibling = part_die;
17835
17836 last_die = part_die;
17837
17838 if (first_die == NULL)
17839 first_die = part_die;
17840
17841 /* Maybe add the DIE to the hash table. Not all DIEs that we
17842 find interesting need to be in the hash table, because we
17843 also have the parent/sibling/child chains; only those that we
17844 might refer to by offset later during partial symbol reading.
17845
17846 For now this means things that might have be the target of a
17847 DW_AT_specification, DW_AT_abstract_origin, or
17848 DW_AT_extension. DW_AT_extension will refer only to
17849 namespaces; DW_AT_abstract_origin refers to functions (and
17850 many things under the function DIE, but we do not recurse
17851 into function DIEs during partial symbol reading) and
17852 possibly variables as well; DW_AT_specification refers to
17853 declarations. Declarations ought to have the DW_AT_declaration
17854 flag. It happens that GCC forgets to put it in sometimes, but
17855 only for functions, not for types.
17856
17857 Adding more things than necessary to the hash table is harmless
17858 except for the performance cost. Adding too few will result in
17859 wasted time in find_partial_die, when we reread the compilation
17860 unit with load_all_dies set. */
17861
17862 if (load_all
17863 || abbrev->tag == DW_TAG_constant
17864 || abbrev->tag == DW_TAG_subprogram
17865 || abbrev->tag == DW_TAG_variable
17866 || abbrev->tag == DW_TAG_namespace
17867 || part_die->is_declaration)
17868 {
17869 void **slot;
17870
17871 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17872 to_underlying (part_die->sect_off),
17873 INSERT);
17874 *slot = part_die;
17875 }
17876
17877 /* For some DIEs we want to follow their children (if any). For C
17878 we have no reason to follow the children of structures; for other
17879 languages we have to, so that we can get at method physnames
17880 to infer fully qualified class names, for DW_AT_specification,
17881 and for C++ template arguments. For C++, we also look one level
17882 inside functions to find template arguments (if the name of the
17883 function does not already contain the template arguments).
17884
17885 For Ada and Fortran, we need to scan the children of subprograms
17886 and lexical blocks as well because these languages allow the
17887 definition of nested entities that could be interesting for the
17888 debugger, such as nested subprograms for instance. */
17889 if (last_die->has_children
17890 && (load_all
17891 || last_die->tag == DW_TAG_namespace
17892 || last_die->tag == DW_TAG_module
17893 || last_die->tag == DW_TAG_enumeration_type
17894 || (cu->language == language_cplus
17895 && last_die->tag == DW_TAG_subprogram
17896 && (last_die->name == NULL
17897 || strchr (last_die->name, '<') == NULL))
17898 || (cu->language != language_c
17899 && (last_die->tag == DW_TAG_class_type
17900 || last_die->tag == DW_TAG_interface_type
17901 || last_die->tag == DW_TAG_structure_type
17902 || last_die->tag == DW_TAG_union_type))
17903 || ((cu->language == language_ada
17904 || cu->language == language_fortran)
17905 && (last_die->tag == DW_TAG_subprogram
17906 || last_die->tag == DW_TAG_lexical_block))))
17907 {
17908 nesting_level++;
17909 parent_die = last_die;
17910 continue;
17911 }
17912
17913 /* Otherwise we skip to the next sibling, if any. */
17914 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17915
17916 /* Back to the top, do it again. */
17917 }
17918 }
17919
17920 partial_die_info::partial_die_info (sect_offset sect_off_,
17921 struct abbrev_info *abbrev)
17922 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17923 {
17924 }
17925
17926 /* Read a minimal amount of information into the minimal die structure.
17927 INFO_PTR should point just after the initial uleb128 of a DIE. */
17928
17929 const gdb_byte *
17930 partial_die_info::read (const struct die_reader_specs *reader,
17931 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
17932 {
17933 struct dwarf2_cu *cu = reader->cu;
17934 struct dwarf2_per_objfile *dwarf2_per_objfile
17935 = cu->per_cu->dwarf2_per_objfile;
17936 unsigned int i;
17937 int has_low_pc_attr = 0;
17938 int has_high_pc_attr = 0;
17939 int high_pc_relative = 0;
17940
17941 for (i = 0; i < abbrev.num_attrs; ++i)
17942 {
17943 attribute attr;
17944 bool need_reprocess;
17945 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
17946 info_ptr, &need_reprocess);
17947 /* String and address offsets that need to do the reprocessing have
17948 already been read at this point, so there is no need to wait until
17949 the loop terminates to do the reprocessing. */
17950 if (need_reprocess)
17951 read_attribute_reprocess (reader, &attr);
17952 /* Store the data if it is of an attribute we want to keep in a
17953 partial symbol table. */
17954 switch (attr.name)
17955 {
17956 case DW_AT_name:
17957 switch (tag)
17958 {
17959 case DW_TAG_compile_unit:
17960 case DW_TAG_partial_unit:
17961 case DW_TAG_type_unit:
17962 /* Compilation units have a DW_AT_name that is a filename, not
17963 a source language identifier. */
17964 case DW_TAG_enumeration_type:
17965 case DW_TAG_enumerator:
17966 /* These tags always have simple identifiers already; no need
17967 to canonicalize them. */
17968 name = DW_STRING (&attr);
17969 break;
17970 default:
17971 {
17972 struct objfile *objfile = dwarf2_per_objfile->objfile;
17973
17974 name
17975 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
17976 }
17977 break;
17978 }
17979 break;
17980 case DW_AT_linkage_name:
17981 case DW_AT_MIPS_linkage_name:
17982 /* Note that both forms of linkage name might appear. We
17983 assume they will be the same, and we only store the last
17984 one we see. */
17985 linkage_name = DW_STRING (&attr);
17986 break;
17987 case DW_AT_low_pc:
17988 has_low_pc_attr = 1;
17989 lowpc = attr.value_as_address ();
17990 break;
17991 case DW_AT_high_pc:
17992 has_high_pc_attr = 1;
17993 highpc = attr.value_as_address ();
17994 if (cu->header.version >= 4 && attr.form_is_constant ())
17995 high_pc_relative = 1;
17996 break;
17997 case DW_AT_location:
17998 /* Support the .debug_loc offsets. */
17999 if (attr.form_is_block ())
18000 {
18001 d.locdesc = DW_BLOCK (&attr);
18002 }
18003 else if (attr.form_is_section_offset ())
18004 {
18005 dwarf2_complex_location_expr_complaint ();
18006 }
18007 else
18008 {
18009 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18010 "partial symbol information");
18011 }
18012 break;
18013 case DW_AT_external:
18014 is_external = DW_UNSND (&attr);
18015 break;
18016 case DW_AT_declaration:
18017 is_declaration = DW_UNSND (&attr);
18018 break;
18019 case DW_AT_type:
18020 has_type = 1;
18021 break;
18022 case DW_AT_abstract_origin:
18023 case DW_AT_specification:
18024 case DW_AT_extension:
18025 has_specification = 1;
18026 spec_offset = attr.get_ref_die_offset ();
18027 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18028 || cu->per_cu->is_dwz);
18029 break;
18030 case DW_AT_sibling:
18031 /* Ignore absolute siblings, they might point outside of
18032 the current compile unit. */
18033 if (attr.form == DW_FORM_ref_addr)
18034 complaint (_("ignoring absolute DW_AT_sibling"));
18035 else
18036 {
18037 const gdb_byte *buffer = reader->buffer;
18038 sect_offset off = attr.get_ref_die_offset ();
18039 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18040
18041 if (sibling_ptr < info_ptr)
18042 complaint (_("DW_AT_sibling points backwards"));
18043 else if (sibling_ptr > reader->buffer_end)
18044 reader->die_section->overflow_complaint ();
18045 else
18046 sibling = sibling_ptr;
18047 }
18048 break;
18049 case DW_AT_byte_size:
18050 has_byte_size = 1;
18051 break;
18052 case DW_AT_const_value:
18053 has_const_value = 1;
18054 break;
18055 case DW_AT_calling_convention:
18056 /* DWARF doesn't provide a way to identify a program's source-level
18057 entry point. DW_AT_calling_convention attributes are only meant
18058 to describe functions' calling conventions.
18059
18060 However, because it's a necessary piece of information in
18061 Fortran, and before DWARF 4 DW_CC_program was the only
18062 piece of debugging information whose definition refers to
18063 a 'main program' at all, several compilers marked Fortran
18064 main programs with DW_CC_program --- even when those
18065 functions use the standard calling conventions.
18066
18067 Although DWARF now specifies a way to provide this
18068 information, we support this practice for backward
18069 compatibility. */
18070 if (DW_UNSND (&attr) == DW_CC_program
18071 && cu->language == language_fortran)
18072 main_subprogram = 1;
18073 break;
18074 case DW_AT_inline:
18075 if (DW_UNSND (&attr) == DW_INL_inlined
18076 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18077 may_be_inlined = 1;
18078 break;
18079
18080 case DW_AT_import:
18081 if (tag == DW_TAG_imported_unit)
18082 {
18083 d.sect_off = attr.get_ref_die_offset ();
18084 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18085 || cu->per_cu->is_dwz);
18086 }
18087 break;
18088
18089 case DW_AT_main_subprogram:
18090 main_subprogram = DW_UNSND (&attr);
18091 break;
18092
18093 case DW_AT_ranges:
18094 {
18095 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18096 but that requires a full DIE, so instead we just
18097 reimplement it. */
18098 int need_ranges_base = tag != DW_TAG_compile_unit;
18099 unsigned int ranges_offset = (DW_UNSND (&attr)
18100 + (need_ranges_base
18101 ? cu->ranges_base
18102 : 0));
18103
18104 /* Value of the DW_AT_ranges attribute is the offset in the
18105 .debug_ranges section. */
18106 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18107 nullptr))
18108 has_pc_info = 1;
18109 }
18110 break;
18111
18112 default:
18113 break;
18114 }
18115 }
18116
18117 /* For Ada, if both the name and the linkage name appear, we prefer
18118 the latter. This lets "catch exception" work better, regardless
18119 of the order in which the name and linkage name were emitted.
18120 Really, though, this is just a workaround for the fact that gdb
18121 doesn't store both the name and the linkage name. */
18122 if (cu->language == language_ada && linkage_name != nullptr)
18123 name = linkage_name;
18124
18125 if (high_pc_relative)
18126 highpc += lowpc;
18127
18128 if (has_low_pc_attr && has_high_pc_attr)
18129 {
18130 /* When using the GNU linker, .gnu.linkonce. sections are used to
18131 eliminate duplicate copies of functions and vtables and such.
18132 The linker will arbitrarily choose one and discard the others.
18133 The AT_*_pc values for such functions refer to local labels in
18134 these sections. If the section from that file was discarded, the
18135 labels are not in the output, so the relocs get a value of 0.
18136 If this is a discarded function, mark the pc bounds as invalid,
18137 so that GDB will ignore it. */
18138 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18139 {
18140 struct objfile *objfile = dwarf2_per_objfile->objfile;
18141 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18142
18143 complaint (_("DW_AT_low_pc %s is zero "
18144 "for DIE at %s [in module %s]"),
18145 paddress (gdbarch, lowpc),
18146 sect_offset_str (sect_off),
18147 objfile_name (objfile));
18148 }
18149 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18150 else if (lowpc >= highpc)
18151 {
18152 struct objfile *objfile = dwarf2_per_objfile->objfile;
18153 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18154
18155 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18156 "for DIE at %s [in module %s]"),
18157 paddress (gdbarch, lowpc),
18158 paddress (gdbarch, highpc),
18159 sect_offset_str (sect_off),
18160 objfile_name (objfile));
18161 }
18162 else
18163 has_pc_info = 1;
18164 }
18165
18166 return info_ptr;
18167 }
18168
18169 /* Find a cached partial DIE at OFFSET in CU. */
18170
18171 struct partial_die_info *
18172 dwarf2_cu::find_partial_die (sect_offset sect_off)
18173 {
18174 struct partial_die_info *lookup_die = NULL;
18175 struct partial_die_info part_die (sect_off);
18176
18177 lookup_die = ((struct partial_die_info *)
18178 htab_find_with_hash (partial_dies, &part_die,
18179 to_underlying (sect_off)));
18180
18181 return lookup_die;
18182 }
18183
18184 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18185 except in the case of .debug_types DIEs which do not reference
18186 outside their CU (they do however referencing other types via
18187 DW_FORM_ref_sig8). */
18188
18189 static const struct cu_partial_die_info
18190 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18191 {
18192 struct dwarf2_per_objfile *dwarf2_per_objfile
18193 = cu->per_cu->dwarf2_per_objfile;
18194 struct objfile *objfile = dwarf2_per_objfile->objfile;
18195 struct dwarf2_per_cu_data *per_cu = NULL;
18196 struct partial_die_info *pd = NULL;
18197
18198 if (offset_in_dwz == cu->per_cu->is_dwz
18199 && cu->header.offset_in_cu_p (sect_off))
18200 {
18201 pd = cu->find_partial_die (sect_off);
18202 if (pd != NULL)
18203 return { cu, pd };
18204 /* We missed recording what we needed.
18205 Load all dies and try again. */
18206 per_cu = cu->per_cu;
18207 }
18208 else
18209 {
18210 /* TUs don't reference other CUs/TUs (except via type signatures). */
18211 if (cu->per_cu->is_debug_types)
18212 {
18213 error (_("Dwarf Error: Type Unit at offset %s contains"
18214 " external reference to offset %s [in module %s].\n"),
18215 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18216 bfd_get_filename (objfile->obfd));
18217 }
18218 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18219 dwarf2_per_objfile);
18220
18221 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18222 load_partial_comp_unit (per_cu);
18223
18224 per_cu->cu->last_used = 0;
18225 pd = per_cu->cu->find_partial_die (sect_off);
18226 }
18227
18228 /* If we didn't find it, and not all dies have been loaded,
18229 load them all and try again. */
18230
18231 if (pd == NULL && per_cu->load_all_dies == 0)
18232 {
18233 per_cu->load_all_dies = 1;
18234
18235 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18236 THIS_CU->cu may already be in use. So we can't just free it and
18237 replace its DIEs with the ones we read in. Instead, we leave those
18238 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18239 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18240 set. */
18241 load_partial_comp_unit (per_cu);
18242
18243 pd = per_cu->cu->find_partial_die (sect_off);
18244 }
18245
18246 if (pd == NULL)
18247 internal_error (__FILE__, __LINE__,
18248 _("could not find partial DIE %s "
18249 "in cache [from module %s]\n"),
18250 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18251 return { per_cu->cu, pd };
18252 }
18253
18254 /* See if we can figure out if the class lives in a namespace. We do
18255 this by looking for a member function; its demangled name will
18256 contain namespace info, if there is any. */
18257
18258 static void
18259 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18260 struct dwarf2_cu *cu)
18261 {
18262 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18263 what template types look like, because the demangler
18264 frequently doesn't give the same name as the debug info. We
18265 could fix this by only using the demangled name to get the
18266 prefix (but see comment in read_structure_type). */
18267
18268 struct partial_die_info *real_pdi;
18269 struct partial_die_info *child_pdi;
18270
18271 /* If this DIE (this DIE's specification, if any) has a parent, then
18272 we should not do this. We'll prepend the parent's fully qualified
18273 name when we create the partial symbol. */
18274
18275 real_pdi = struct_pdi;
18276 while (real_pdi->has_specification)
18277 {
18278 auto res = find_partial_die (real_pdi->spec_offset,
18279 real_pdi->spec_is_dwz, cu);
18280 real_pdi = res.pdi;
18281 cu = res.cu;
18282 }
18283
18284 if (real_pdi->die_parent != NULL)
18285 return;
18286
18287 for (child_pdi = struct_pdi->die_child;
18288 child_pdi != NULL;
18289 child_pdi = child_pdi->die_sibling)
18290 {
18291 if (child_pdi->tag == DW_TAG_subprogram
18292 && child_pdi->linkage_name != NULL)
18293 {
18294 gdb::unique_xmalloc_ptr<char> actual_class_name
18295 (language_class_name_from_physname (cu->language_defn,
18296 child_pdi->linkage_name));
18297 if (actual_class_name != NULL)
18298 {
18299 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18300 struct_pdi->name = objfile->intern (actual_class_name.get ());
18301 }
18302 break;
18303 }
18304 }
18305 }
18306
18307 /* Return true if a DIE with TAG may have the DW_AT_const_value
18308 attribute. */
18309
18310 static bool
18311 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18312 {
18313 switch (tag)
18314 {
18315 case DW_TAG_constant:
18316 case DW_TAG_enumerator:
18317 case DW_TAG_formal_parameter:
18318 case DW_TAG_template_value_param:
18319 case DW_TAG_variable:
18320 return true;
18321 }
18322
18323 return false;
18324 }
18325
18326 void
18327 partial_die_info::fixup (struct dwarf2_cu *cu)
18328 {
18329 /* Once we've fixed up a die, there's no point in doing so again.
18330 This also avoids a memory leak if we were to call
18331 guess_partial_die_structure_name multiple times. */
18332 if (fixup_called)
18333 return;
18334
18335 /* If we found a reference attribute and the DIE has no name, try
18336 to find a name in the referred to DIE. */
18337
18338 if (name == NULL && has_specification)
18339 {
18340 struct partial_die_info *spec_die;
18341
18342 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18343 spec_die = res.pdi;
18344 cu = res.cu;
18345
18346 spec_die->fixup (cu);
18347
18348 if (spec_die->name)
18349 {
18350 name = spec_die->name;
18351
18352 /* Copy DW_AT_external attribute if it is set. */
18353 if (spec_die->is_external)
18354 is_external = spec_die->is_external;
18355 }
18356 }
18357
18358 if (!has_const_value && has_specification
18359 && can_have_DW_AT_const_value_p (tag))
18360 {
18361 struct partial_die_info *spec_die;
18362
18363 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18364 spec_die = res.pdi;
18365 cu = res.cu;
18366
18367 spec_die->fixup (cu);
18368
18369 if (spec_die->has_const_value)
18370 {
18371 /* Copy DW_AT_const_value attribute if it is set. */
18372 has_const_value = spec_die->has_const_value;
18373 }
18374 }
18375
18376 /* Set default names for some unnamed DIEs. */
18377
18378 if (name == NULL && tag == DW_TAG_namespace)
18379 name = CP_ANONYMOUS_NAMESPACE_STR;
18380
18381 /* If there is no parent die to provide a namespace, and there are
18382 children, see if we can determine the namespace from their linkage
18383 name. */
18384 if (cu->language == language_cplus
18385 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18386 && die_parent == NULL
18387 && has_children
18388 && (tag == DW_TAG_class_type
18389 || tag == DW_TAG_structure_type
18390 || tag == DW_TAG_union_type))
18391 guess_partial_die_structure_name (this, cu);
18392
18393 /* GCC might emit a nameless struct or union that has a linkage
18394 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18395 if (name == NULL
18396 && (tag == DW_TAG_class_type
18397 || tag == DW_TAG_interface_type
18398 || tag == DW_TAG_structure_type
18399 || tag == DW_TAG_union_type)
18400 && linkage_name != NULL)
18401 {
18402 gdb::unique_xmalloc_ptr<char> demangled
18403 (gdb_demangle (linkage_name, DMGL_TYPES));
18404 if (demangled != nullptr)
18405 {
18406 const char *base;
18407
18408 /* Strip any leading namespaces/classes, keep only the base name.
18409 DW_AT_name for named DIEs does not contain the prefixes. */
18410 base = strrchr (demangled.get (), ':');
18411 if (base && base > demangled.get () && base[-1] == ':')
18412 base++;
18413 else
18414 base = demangled.get ();
18415
18416 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18417 name = objfile->intern (base);
18418 }
18419 }
18420
18421 fixup_called = 1;
18422 }
18423
18424 /* Read the .debug_loclists header contents from the given SECTION in the
18425 HEADER. */
18426 static void
18427 read_loclist_header (struct loclist_header *header,
18428 struct dwarf2_section_info *section)
18429 {
18430 unsigned int bytes_read;
18431 bfd *abfd = section->get_bfd_owner ();
18432 const gdb_byte *info_ptr = section->buffer;
18433 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18434 info_ptr += bytes_read;
18435 header->version = read_2_bytes (abfd, info_ptr);
18436 info_ptr += 2;
18437 header->addr_size = read_1_byte (abfd, info_ptr);
18438 info_ptr += 1;
18439 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18440 info_ptr += 1;
18441 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18442 }
18443
18444 /* Return the DW_AT_loclists_base value for the CU. */
18445 static ULONGEST
18446 lookup_loclist_base (struct dwarf2_cu *cu)
18447 {
18448 /* For the .dwo unit, the loclist_base points to the first offset following
18449 the header. The header consists of the following entities-
18450 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18451 bit format)
18452 2. version (2 bytes)
18453 3. address size (1 byte)
18454 4. segment selector size (1 byte)
18455 5. offset entry count (4 bytes)
18456 These sizes are derived as per the DWARFv5 standard. */
18457 if (cu->dwo_unit != nullptr)
18458 {
18459 if (cu->header.initial_length_size == 4)
18460 return LOCLIST_HEADER_SIZE32;
18461 return LOCLIST_HEADER_SIZE64;
18462 }
18463 return cu->loclist_base;
18464 }
18465
18466 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18467 array of offsets in the .debug_loclists section. */
18468 static CORE_ADDR
18469 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18470 {
18471 struct dwarf2_per_objfile *dwarf2_per_objfile
18472 = cu->per_cu->dwarf2_per_objfile;
18473 struct objfile *objfile = dwarf2_per_objfile->objfile;
18474 bfd *abfd = objfile->obfd;
18475 ULONGEST loclist_base = lookup_loclist_base (cu);
18476 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18477
18478 section->read (objfile);
18479 if (section->buffer == NULL)
18480 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18481 "section [in module %s]"), objfile_name (objfile));
18482 struct loclist_header header;
18483 read_loclist_header (&header, section);
18484 if (loclist_index >= header.offset_entry_count)
18485 complaint (_("DW_FORM_loclistx pointing outside of "
18486 ".debug_loclists offset array [in module %s]"),
18487 objfile_name (objfile));
18488 if (loclist_base + loclist_index * cu->header.offset_size
18489 >= section->size)
18490 complaint (_("DW_FORM_loclistx pointing outside of "
18491 ".debug_loclists section [in module %s]"),
18492 objfile_name (objfile));
18493 const gdb_byte *info_ptr
18494 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18495
18496 if (cu->header.offset_size == 4)
18497 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18498 else
18499 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18500 }
18501
18502 /* Process the attributes that had to be skipped in the first round. These
18503 attributes are the ones that need str_offsets_base or addr_base attributes.
18504 They could not have been processed in the first round, because at the time
18505 the values of str_offsets_base or addr_base may not have been known. */
18506 static void
18507 read_attribute_reprocess (const struct die_reader_specs *reader,
18508 struct attribute *attr)
18509 {
18510 struct dwarf2_cu *cu = reader->cu;
18511 switch (attr->form)
18512 {
18513 case DW_FORM_addrx:
18514 case DW_FORM_GNU_addr_index:
18515 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18516 break;
18517 case DW_FORM_loclistx:
18518 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18519 break;
18520 case DW_FORM_strx:
18521 case DW_FORM_strx1:
18522 case DW_FORM_strx2:
18523 case DW_FORM_strx3:
18524 case DW_FORM_strx4:
18525 case DW_FORM_GNU_str_index:
18526 {
18527 unsigned int str_index = DW_UNSND (attr);
18528 if (reader->dwo_file != NULL)
18529 {
18530 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18531 DW_STRING_IS_CANONICAL (attr) = 0;
18532 }
18533 else
18534 {
18535 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18536 DW_STRING_IS_CANONICAL (attr) = 0;
18537 }
18538 break;
18539 }
18540 default:
18541 gdb_assert_not_reached (_("Unexpected DWARF form."));
18542 }
18543 }
18544
18545 /* Read an attribute value described by an attribute form. */
18546
18547 static const gdb_byte *
18548 read_attribute_value (const struct die_reader_specs *reader,
18549 struct attribute *attr, unsigned form,
18550 LONGEST implicit_const, const gdb_byte *info_ptr,
18551 bool *need_reprocess)
18552 {
18553 struct dwarf2_cu *cu = reader->cu;
18554 struct dwarf2_per_objfile *dwarf2_per_objfile
18555 = cu->per_cu->dwarf2_per_objfile;
18556 struct objfile *objfile = dwarf2_per_objfile->objfile;
18557 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18558 bfd *abfd = reader->abfd;
18559 struct comp_unit_head *cu_header = &cu->header;
18560 unsigned int bytes_read;
18561 struct dwarf_block *blk;
18562 *need_reprocess = false;
18563
18564 attr->form = (enum dwarf_form) form;
18565 switch (form)
18566 {
18567 case DW_FORM_ref_addr:
18568 if (cu->header.version == 2)
18569 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18570 &bytes_read);
18571 else
18572 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18573 &bytes_read);
18574 info_ptr += bytes_read;
18575 break;
18576 case DW_FORM_GNU_ref_alt:
18577 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18578 info_ptr += bytes_read;
18579 break;
18580 case DW_FORM_addr:
18581 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18582 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18583 info_ptr += bytes_read;
18584 break;
18585 case DW_FORM_block2:
18586 blk = dwarf_alloc_block (cu);
18587 blk->size = read_2_bytes (abfd, info_ptr);
18588 info_ptr += 2;
18589 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18590 info_ptr += blk->size;
18591 DW_BLOCK (attr) = blk;
18592 break;
18593 case DW_FORM_block4:
18594 blk = dwarf_alloc_block (cu);
18595 blk->size = read_4_bytes (abfd, info_ptr);
18596 info_ptr += 4;
18597 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18598 info_ptr += blk->size;
18599 DW_BLOCK (attr) = blk;
18600 break;
18601 case DW_FORM_data2:
18602 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18603 info_ptr += 2;
18604 break;
18605 case DW_FORM_data4:
18606 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18607 info_ptr += 4;
18608 break;
18609 case DW_FORM_data8:
18610 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18611 info_ptr += 8;
18612 break;
18613 case DW_FORM_data16:
18614 blk = dwarf_alloc_block (cu);
18615 blk->size = 16;
18616 blk->data = read_n_bytes (abfd, info_ptr, 16);
18617 info_ptr += 16;
18618 DW_BLOCK (attr) = blk;
18619 break;
18620 case DW_FORM_sec_offset:
18621 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18622 info_ptr += bytes_read;
18623 break;
18624 case DW_FORM_loclistx:
18625 {
18626 *need_reprocess = true;
18627 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18628 info_ptr += bytes_read;
18629 }
18630 break;
18631 case DW_FORM_string:
18632 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18633 DW_STRING_IS_CANONICAL (attr) = 0;
18634 info_ptr += bytes_read;
18635 break;
18636 case DW_FORM_strp:
18637 if (!cu->per_cu->is_dwz)
18638 {
18639 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18640 abfd, info_ptr, cu_header,
18641 &bytes_read);
18642 DW_STRING_IS_CANONICAL (attr) = 0;
18643 info_ptr += bytes_read;
18644 break;
18645 }
18646 /* FALLTHROUGH */
18647 case DW_FORM_line_strp:
18648 if (!cu->per_cu->is_dwz)
18649 {
18650 DW_STRING (attr)
18651 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
18652 &bytes_read);
18653 DW_STRING_IS_CANONICAL (attr) = 0;
18654 info_ptr += bytes_read;
18655 break;
18656 }
18657 /* FALLTHROUGH */
18658 case DW_FORM_GNU_strp_alt:
18659 {
18660 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18661 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18662 &bytes_read);
18663
18664 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
18665 DW_STRING_IS_CANONICAL (attr) = 0;
18666 info_ptr += bytes_read;
18667 }
18668 break;
18669 case DW_FORM_exprloc:
18670 case DW_FORM_block:
18671 blk = dwarf_alloc_block (cu);
18672 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18673 info_ptr += bytes_read;
18674 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18675 info_ptr += blk->size;
18676 DW_BLOCK (attr) = blk;
18677 break;
18678 case DW_FORM_block1:
18679 blk = dwarf_alloc_block (cu);
18680 blk->size = read_1_byte (abfd, info_ptr);
18681 info_ptr += 1;
18682 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18683 info_ptr += blk->size;
18684 DW_BLOCK (attr) = blk;
18685 break;
18686 case DW_FORM_data1:
18687 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18688 info_ptr += 1;
18689 break;
18690 case DW_FORM_flag:
18691 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18692 info_ptr += 1;
18693 break;
18694 case DW_FORM_flag_present:
18695 DW_UNSND (attr) = 1;
18696 break;
18697 case DW_FORM_sdata:
18698 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18699 info_ptr += bytes_read;
18700 break;
18701 case DW_FORM_udata:
18702 case DW_FORM_rnglistx:
18703 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18704 info_ptr += bytes_read;
18705 break;
18706 case DW_FORM_ref1:
18707 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18708 + read_1_byte (abfd, info_ptr));
18709 info_ptr += 1;
18710 break;
18711 case DW_FORM_ref2:
18712 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18713 + read_2_bytes (abfd, info_ptr));
18714 info_ptr += 2;
18715 break;
18716 case DW_FORM_ref4:
18717 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18718 + read_4_bytes (abfd, info_ptr));
18719 info_ptr += 4;
18720 break;
18721 case DW_FORM_ref8:
18722 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18723 + read_8_bytes (abfd, info_ptr));
18724 info_ptr += 8;
18725 break;
18726 case DW_FORM_ref_sig8:
18727 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18728 info_ptr += 8;
18729 break;
18730 case DW_FORM_ref_udata:
18731 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18732 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18733 info_ptr += bytes_read;
18734 break;
18735 case DW_FORM_indirect:
18736 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18737 info_ptr += bytes_read;
18738 if (form == DW_FORM_implicit_const)
18739 {
18740 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18741 info_ptr += bytes_read;
18742 }
18743 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18744 info_ptr, need_reprocess);
18745 break;
18746 case DW_FORM_implicit_const:
18747 DW_SND (attr) = implicit_const;
18748 break;
18749 case DW_FORM_addrx:
18750 case DW_FORM_GNU_addr_index:
18751 *need_reprocess = true;
18752 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18753 info_ptr += bytes_read;
18754 break;
18755 case DW_FORM_strx:
18756 case DW_FORM_strx1:
18757 case DW_FORM_strx2:
18758 case DW_FORM_strx3:
18759 case DW_FORM_strx4:
18760 case DW_FORM_GNU_str_index:
18761 {
18762 ULONGEST str_index;
18763 if (form == DW_FORM_strx1)
18764 {
18765 str_index = read_1_byte (abfd, info_ptr);
18766 info_ptr += 1;
18767 }
18768 else if (form == DW_FORM_strx2)
18769 {
18770 str_index = read_2_bytes (abfd, info_ptr);
18771 info_ptr += 2;
18772 }
18773 else if (form == DW_FORM_strx3)
18774 {
18775 str_index = read_3_bytes (abfd, info_ptr);
18776 info_ptr += 3;
18777 }
18778 else if (form == DW_FORM_strx4)
18779 {
18780 str_index = read_4_bytes (abfd, info_ptr);
18781 info_ptr += 4;
18782 }
18783 else
18784 {
18785 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18786 info_ptr += bytes_read;
18787 }
18788 *need_reprocess = true;
18789 DW_UNSND (attr) = str_index;
18790 }
18791 break;
18792 default:
18793 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
18794 dwarf_form_name (form),
18795 bfd_get_filename (abfd));
18796 }
18797
18798 /* Super hack. */
18799 if (cu->per_cu->is_dwz && attr->form_is_ref ())
18800 attr->form = DW_FORM_GNU_ref_alt;
18801
18802 /* We have seen instances where the compiler tried to emit a byte
18803 size attribute of -1 which ended up being encoded as an unsigned
18804 0xffffffff. Although 0xffffffff is technically a valid size value,
18805 an object of this size seems pretty unlikely so we can relatively
18806 safely treat these cases as if the size attribute was invalid and
18807 treat them as zero by default. */
18808 if (attr->name == DW_AT_byte_size
18809 && form == DW_FORM_data4
18810 && DW_UNSND (attr) >= 0xffffffff)
18811 {
18812 complaint
18813 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
18814 hex_string (DW_UNSND (attr)));
18815 DW_UNSND (attr) = 0;
18816 }
18817
18818 return info_ptr;
18819 }
18820
18821 /* Read an attribute described by an abbreviated attribute. */
18822
18823 static const gdb_byte *
18824 read_attribute (const struct die_reader_specs *reader,
18825 struct attribute *attr, struct attr_abbrev *abbrev,
18826 const gdb_byte *info_ptr, bool *need_reprocess)
18827 {
18828 attr->name = abbrev->name;
18829 return read_attribute_value (reader, attr, abbrev->form,
18830 abbrev->implicit_const, info_ptr,
18831 need_reprocess);
18832 }
18833
18834 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18835
18836 static const char *
18837 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18838 LONGEST str_offset)
18839 {
18840 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
18841 str_offset, "DW_FORM_strp");
18842 }
18843
18844 /* Return pointer to string at .debug_str offset as read from BUF.
18845 BUF is assumed to be in a compilation unit described by CU_HEADER.
18846 Return *BYTES_READ_PTR count of bytes read from BUF. */
18847
18848 static const char *
18849 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18850 const gdb_byte *buf,
18851 const struct comp_unit_head *cu_header,
18852 unsigned int *bytes_read_ptr)
18853 {
18854 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18855
18856 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
18857 }
18858
18859 /* See read.h. */
18860
18861 const char *
18862 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
18863 const struct comp_unit_head *cu_header,
18864 unsigned int *bytes_read_ptr)
18865 {
18866 bfd *abfd = objfile->obfd;
18867 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18868
18869 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
18870 }
18871
18872 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18873 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
18874 ADDR_SIZE is the size of addresses from the CU header. */
18875
18876 static CORE_ADDR
18877 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18878 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18879 int addr_size)
18880 {
18881 struct objfile *objfile = dwarf2_per_objfile->objfile;
18882 bfd *abfd = objfile->obfd;
18883 const gdb_byte *info_ptr;
18884 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
18885
18886 dwarf2_per_objfile->addr.read (objfile);
18887 if (dwarf2_per_objfile->addr.buffer == NULL)
18888 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18889 objfile_name (objfile));
18890 if (addr_base_or_zero + addr_index * addr_size
18891 >= dwarf2_per_objfile->addr.size)
18892 error (_("DW_FORM_addr_index pointing outside of "
18893 ".debug_addr section [in module %s]"),
18894 objfile_name (objfile));
18895 info_ptr = (dwarf2_per_objfile->addr.buffer
18896 + addr_base_or_zero + addr_index * addr_size);
18897 if (addr_size == 4)
18898 return bfd_get_32 (abfd, info_ptr);
18899 else
18900 return bfd_get_64 (abfd, info_ptr);
18901 }
18902
18903 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18904
18905 static CORE_ADDR
18906 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18907 {
18908 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18909 cu->addr_base, cu->header.addr_size);
18910 }
18911
18912 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18913
18914 static CORE_ADDR
18915 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18916 unsigned int *bytes_read)
18917 {
18918 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
18919 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18920
18921 return read_addr_index (cu, addr_index);
18922 }
18923
18924 /* See read.h. */
18925
18926 CORE_ADDR
18927 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
18928 {
18929 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
18930 struct dwarf2_cu *cu = per_cu->cu;
18931 gdb::optional<ULONGEST> addr_base;
18932 int addr_size;
18933
18934 /* We need addr_base and addr_size.
18935 If we don't have PER_CU->cu, we have to get it.
18936 Nasty, but the alternative is storing the needed info in PER_CU,
18937 which at this point doesn't seem justified: it's not clear how frequently
18938 it would get used and it would increase the size of every PER_CU.
18939 Entry points like dwarf2_per_cu_addr_size do a similar thing
18940 so we're not in uncharted territory here.
18941 Alas we need to be a bit more complicated as addr_base is contained
18942 in the DIE.
18943
18944 We don't need to read the entire CU(/TU).
18945 We just need the header and top level die.
18946
18947 IWBN to use the aging mechanism to let us lazily later discard the CU.
18948 For now we skip this optimization. */
18949
18950 if (cu != NULL)
18951 {
18952 addr_base = cu->addr_base;
18953 addr_size = cu->header.addr_size;
18954 }
18955 else
18956 {
18957 cutu_reader reader (per_cu, NULL, 0, false);
18958 addr_base = reader.cu->addr_base;
18959 addr_size = reader.cu->header.addr_size;
18960 }
18961
18962 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18963 addr_size);
18964 }
18965
18966 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18967 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18968 DWO file. */
18969
18970 static const char *
18971 read_str_index (struct dwarf2_cu *cu,
18972 struct dwarf2_section_info *str_section,
18973 struct dwarf2_section_info *str_offsets_section,
18974 ULONGEST str_offsets_base, ULONGEST str_index)
18975 {
18976 struct dwarf2_per_objfile *dwarf2_per_objfile
18977 = cu->per_cu->dwarf2_per_objfile;
18978 struct objfile *objfile = dwarf2_per_objfile->objfile;
18979 const char *objf_name = objfile_name (objfile);
18980 bfd *abfd = objfile->obfd;
18981 const gdb_byte *info_ptr;
18982 ULONGEST str_offset;
18983 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
18984
18985 str_section->read (objfile);
18986 str_offsets_section->read (objfile);
18987 if (str_section->buffer == NULL)
18988 error (_("%s used without %s section"
18989 " in CU at offset %s [in module %s]"),
18990 form_name, str_section->get_name (),
18991 sect_offset_str (cu->header.sect_off), objf_name);
18992 if (str_offsets_section->buffer == NULL)
18993 error (_("%s used without %s section"
18994 " in CU at offset %s [in module %s]"),
18995 form_name, str_section->get_name (),
18996 sect_offset_str (cu->header.sect_off), objf_name);
18997 info_ptr = (str_offsets_section->buffer
18998 + str_offsets_base
18999 + str_index * cu->header.offset_size);
19000 if (cu->header.offset_size == 4)
19001 str_offset = bfd_get_32 (abfd, info_ptr);
19002 else
19003 str_offset = bfd_get_64 (abfd, info_ptr);
19004 if (str_offset >= str_section->size)
19005 error (_("Offset from %s pointing outside of"
19006 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19007 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19008 return (const char *) (str_section->buffer + str_offset);
19009 }
19010
19011 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19012
19013 static const char *
19014 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19015 {
19016 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19017 ? reader->cu->header.addr_size : 0;
19018 return read_str_index (reader->cu,
19019 &reader->dwo_file->sections.str,
19020 &reader->dwo_file->sections.str_offsets,
19021 str_offsets_base, str_index);
19022 }
19023
19024 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19025
19026 static const char *
19027 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19028 {
19029 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19030 const char *objf_name = objfile_name (objfile);
19031 static const char form_name[] = "DW_FORM_GNU_str_index";
19032 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19033
19034 if (!cu->str_offsets_base.has_value ())
19035 error (_("%s used in Fission stub without %s"
19036 " in CU at offset 0x%lx [in module %s]"),
19037 form_name, str_offsets_attr_name,
19038 (long) cu->header.offset_size, objf_name);
19039
19040 return read_str_index (cu,
19041 &cu->per_cu->dwarf2_per_objfile->str,
19042 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19043 *cu->str_offsets_base, str_index);
19044 }
19045
19046 /* Return the length of an LEB128 number in BUF. */
19047
19048 static int
19049 leb128_size (const gdb_byte *buf)
19050 {
19051 const gdb_byte *begin = buf;
19052 gdb_byte byte;
19053
19054 while (1)
19055 {
19056 byte = *buf++;
19057 if ((byte & 128) == 0)
19058 return buf - begin;
19059 }
19060 }
19061
19062 static void
19063 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19064 {
19065 switch (lang)
19066 {
19067 case DW_LANG_C89:
19068 case DW_LANG_C99:
19069 case DW_LANG_C11:
19070 case DW_LANG_C:
19071 case DW_LANG_UPC:
19072 cu->language = language_c;
19073 break;
19074 case DW_LANG_Java:
19075 case DW_LANG_C_plus_plus:
19076 case DW_LANG_C_plus_plus_11:
19077 case DW_LANG_C_plus_plus_14:
19078 cu->language = language_cplus;
19079 break;
19080 case DW_LANG_D:
19081 cu->language = language_d;
19082 break;
19083 case DW_LANG_Fortran77:
19084 case DW_LANG_Fortran90:
19085 case DW_LANG_Fortran95:
19086 case DW_LANG_Fortran03:
19087 case DW_LANG_Fortran08:
19088 cu->language = language_fortran;
19089 break;
19090 case DW_LANG_Go:
19091 cu->language = language_go;
19092 break;
19093 case DW_LANG_Mips_Assembler:
19094 cu->language = language_asm;
19095 break;
19096 case DW_LANG_Ada83:
19097 case DW_LANG_Ada95:
19098 cu->language = language_ada;
19099 break;
19100 case DW_LANG_Modula2:
19101 cu->language = language_m2;
19102 break;
19103 case DW_LANG_Pascal83:
19104 cu->language = language_pascal;
19105 break;
19106 case DW_LANG_ObjC:
19107 cu->language = language_objc;
19108 break;
19109 case DW_LANG_Rust:
19110 case DW_LANG_Rust_old:
19111 cu->language = language_rust;
19112 break;
19113 case DW_LANG_Cobol74:
19114 case DW_LANG_Cobol85:
19115 default:
19116 cu->language = language_minimal;
19117 break;
19118 }
19119 cu->language_defn = language_def (cu->language);
19120 }
19121
19122 /* Return the named attribute or NULL if not there. */
19123
19124 static struct attribute *
19125 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19126 {
19127 for (;;)
19128 {
19129 unsigned int i;
19130 struct attribute *spec = NULL;
19131
19132 for (i = 0; i < die->num_attrs; ++i)
19133 {
19134 if (die->attrs[i].name == name)
19135 return &die->attrs[i];
19136 if (die->attrs[i].name == DW_AT_specification
19137 || die->attrs[i].name == DW_AT_abstract_origin)
19138 spec = &die->attrs[i];
19139 }
19140
19141 if (!spec)
19142 break;
19143
19144 die = follow_die_ref (die, spec, &cu);
19145 }
19146
19147 return NULL;
19148 }
19149
19150 /* Return the string associated with a string-typed attribute, or NULL if it
19151 is either not found or is of an incorrect type. */
19152
19153 static const char *
19154 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19155 {
19156 struct attribute *attr;
19157 const char *str = NULL;
19158
19159 attr = dwarf2_attr (die, name, cu);
19160
19161 if (attr != NULL)
19162 {
19163 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19164 || attr->form == DW_FORM_string
19165 || attr->form == DW_FORM_strx
19166 || attr->form == DW_FORM_strx1
19167 || attr->form == DW_FORM_strx2
19168 || attr->form == DW_FORM_strx3
19169 || attr->form == DW_FORM_strx4
19170 || attr->form == DW_FORM_GNU_str_index
19171 || attr->form == DW_FORM_GNU_strp_alt)
19172 str = DW_STRING (attr);
19173 else
19174 complaint (_("string type expected for attribute %s for "
19175 "DIE at %s in module %s"),
19176 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19177 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19178 }
19179
19180 return str;
19181 }
19182
19183 /* Return the dwo name or NULL if not present. If present, it is in either
19184 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19185 static const char *
19186 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19187 {
19188 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19189 if (dwo_name == nullptr)
19190 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19191 return dwo_name;
19192 }
19193
19194 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19195 and holds a non-zero value. This function should only be used for
19196 DW_FORM_flag or DW_FORM_flag_present attributes. */
19197
19198 static int
19199 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19200 {
19201 struct attribute *attr = dwarf2_attr (die, name, cu);
19202
19203 return (attr && DW_UNSND (attr));
19204 }
19205
19206 static int
19207 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19208 {
19209 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19210 which value is non-zero. However, we have to be careful with
19211 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19212 (via dwarf2_flag_true_p) follows this attribute. So we may
19213 end up accidently finding a declaration attribute that belongs
19214 to a different DIE referenced by the specification attribute,
19215 even though the given DIE does not have a declaration attribute. */
19216 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19217 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19218 }
19219
19220 /* Return the die giving the specification for DIE, if there is
19221 one. *SPEC_CU is the CU containing DIE on input, and the CU
19222 containing the return value on output. If there is no
19223 specification, but there is an abstract origin, that is
19224 returned. */
19225
19226 static struct die_info *
19227 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19228 {
19229 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19230 *spec_cu);
19231
19232 if (spec_attr == NULL)
19233 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19234
19235 if (spec_attr == NULL)
19236 return NULL;
19237 else
19238 return follow_die_ref (die, spec_attr, spec_cu);
19239 }
19240
19241 /* Stub for free_line_header to match void * callback types. */
19242
19243 static void
19244 free_line_header_voidp (void *arg)
19245 {
19246 struct line_header *lh = (struct line_header *) arg;
19247
19248 delete lh;
19249 }
19250
19251 /* A convenience function to find the proper .debug_line section for a CU. */
19252
19253 static struct dwarf2_section_info *
19254 get_debug_line_section (struct dwarf2_cu *cu)
19255 {
19256 struct dwarf2_section_info *section;
19257 struct dwarf2_per_objfile *dwarf2_per_objfile
19258 = cu->per_cu->dwarf2_per_objfile;
19259
19260 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19261 DWO file. */
19262 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19263 section = &cu->dwo_unit->dwo_file->sections.line;
19264 else if (cu->per_cu->is_dwz)
19265 {
19266 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19267
19268 section = &dwz->line;
19269 }
19270 else
19271 section = &dwarf2_per_objfile->line;
19272
19273 return section;
19274 }
19275
19276 /* Read the statement program header starting at OFFSET in
19277 .debug_line, or .debug_line.dwo. Return a pointer
19278 to a struct line_header, allocated using xmalloc.
19279 Returns NULL if there is a problem reading the header, e.g., if it
19280 has a version we don't understand.
19281
19282 NOTE: the strings in the include directory and file name tables of
19283 the returned object point into the dwarf line section buffer,
19284 and must not be freed. */
19285
19286 static line_header_up
19287 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19288 {
19289 struct dwarf2_section_info *section;
19290 struct dwarf2_per_objfile *dwarf2_per_objfile
19291 = cu->per_cu->dwarf2_per_objfile;
19292
19293 section = get_debug_line_section (cu);
19294 section->read (dwarf2_per_objfile->objfile);
19295 if (section->buffer == NULL)
19296 {
19297 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19298 complaint (_("missing .debug_line.dwo section"));
19299 else
19300 complaint (_("missing .debug_line section"));
19301 return 0;
19302 }
19303
19304 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19305 dwarf2_per_objfile, section,
19306 &cu->header);
19307 }
19308
19309 /* Subroutine of dwarf_decode_lines to simplify it.
19310 Return the file name of the psymtab for the given file_entry.
19311 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19312 If space for the result is malloc'd, *NAME_HOLDER will be set.
19313 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19314
19315 static const char *
19316 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19317 const dwarf2_psymtab *pst,
19318 const char *comp_dir,
19319 gdb::unique_xmalloc_ptr<char> *name_holder)
19320 {
19321 const char *include_name = fe.name;
19322 const char *include_name_to_compare = include_name;
19323 const char *pst_filename;
19324 int file_is_pst;
19325
19326 const char *dir_name = fe.include_dir (lh);
19327
19328 gdb::unique_xmalloc_ptr<char> hold_compare;
19329 if (!IS_ABSOLUTE_PATH (include_name)
19330 && (dir_name != NULL || comp_dir != NULL))
19331 {
19332 /* Avoid creating a duplicate psymtab for PST.
19333 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19334 Before we do the comparison, however, we need to account
19335 for DIR_NAME and COMP_DIR.
19336 First prepend dir_name (if non-NULL). If we still don't
19337 have an absolute path prepend comp_dir (if non-NULL).
19338 However, the directory we record in the include-file's
19339 psymtab does not contain COMP_DIR (to match the
19340 corresponding symtab(s)).
19341
19342 Example:
19343
19344 bash$ cd /tmp
19345 bash$ gcc -g ./hello.c
19346 include_name = "hello.c"
19347 dir_name = "."
19348 DW_AT_comp_dir = comp_dir = "/tmp"
19349 DW_AT_name = "./hello.c"
19350
19351 */
19352
19353 if (dir_name != NULL)
19354 {
19355 name_holder->reset (concat (dir_name, SLASH_STRING,
19356 include_name, (char *) NULL));
19357 include_name = name_holder->get ();
19358 include_name_to_compare = include_name;
19359 }
19360 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19361 {
19362 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19363 include_name, (char *) NULL));
19364 include_name_to_compare = hold_compare.get ();
19365 }
19366 }
19367
19368 pst_filename = pst->filename;
19369 gdb::unique_xmalloc_ptr<char> copied_name;
19370 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19371 {
19372 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19373 pst_filename, (char *) NULL));
19374 pst_filename = copied_name.get ();
19375 }
19376
19377 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19378
19379 if (file_is_pst)
19380 return NULL;
19381 return include_name;
19382 }
19383
19384 /* State machine to track the state of the line number program. */
19385
19386 class lnp_state_machine
19387 {
19388 public:
19389 /* Initialize a machine state for the start of a line number
19390 program. */
19391 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19392 bool record_lines_p);
19393
19394 file_entry *current_file ()
19395 {
19396 /* lh->file_names is 0-based, but the file name numbers in the
19397 statement program are 1-based. */
19398 return m_line_header->file_name_at (m_file);
19399 }
19400
19401 /* Record the line in the state machine. END_SEQUENCE is true if
19402 we're processing the end of a sequence. */
19403 void record_line (bool end_sequence);
19404
19405 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19406 nop-out rest of the lines in this sequence. */
19407 void check_line_address (struct dwarf2_cu *cu,
19408 const gdb_byte *line_ptr,
19409 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19410
19411 void handle_set_discriminator (unsigned int discriminator)
19412 {
19413 m_discriminator = discriminator;
19414 m_line_has_non_zero_discriminator |= discriminator != 0;
19415 }
19416
19417 /* Handle DW_LNE_set_address. */
19418 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19419 {
19420 m_op_index = 0;
19421 address += baseaddr;
19422 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19423 }
19424
19425 /* Handle DW_LNS_advance_pc. */
19426 void handle_advance_pc (CORE_ADDR adjust);
19427
19428 /* Handle a special opcode. */
19429 void handle_special_opcode (unsigned char op_code);
19430
19431 /* Handle DW_LNS_advance_line. */
19432 void handle_advance_line (int line_delta)
19433 {
19434 advance_line (line_delta);
19435 }
19436
19437 /* Handle DW_LNS_set_file. */
19438 void handle_set_file (file_name_index file);
19439
19440 /* Handle DW_LNS_negate_stmt. */
19441 void handle_negate_stmt ()
19442 {
19443 m_is_stmt = !m_is_stmt;
19444 }
19445
19446 /* Handle DW_LNS_const_add_pc. */
19447 void handle_const_add_pc ();
19448
19449 /* Handle DW_LNS_fixed_advance_pc. */
19450 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19451 {
19452 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19453 m_op_index = 0;
19454 }
19455
19456 /* Handle DW_LNS_copy. */
19457 void handle_copy ()
19458 {
19459 record_line (false);
19460 m_discriminator = 0;
19461 }
19462
19463 /* Handle DW_LNE_end_sequence. */
19464 void handle_end_sequence ()
19465 {
19466 m_currently_recording_lines = true;
19467 }
19468
19469 private:
19470 /* Advance the line by LINE_DELTA. */
19471 void advance_line (int line_delta)
19472 {
19473 m_line += line_delta;
19474
19475 if (line_delta != 0)
19476 m_line_has_non_zero_discriminator = m_discriminator != 0;
19477 }
19478
19479 struct dwarf2_cu *m_cu;
19480
19481 gdbarch *m_gdbarch;
19482
19483 /* True if we're recording lines.
19484 Otherwise we're building partial symtabs and are just interested in
19485 finding include files mentioned by the line number program. */
19486 bool m_record_lines_p;
19487
19488 /* The line number header. */
19489 line_header *m_line_header;
19490
19491 /* These are part of the standard DWARF line number state machine,
19492 and initialized according to the DWARF spec. */
19493
19494 unsigned char m_op_index = 0;
19495 /* The line table index of the current file. */
19496 file_name_index m_file = 1;
19497 unsigned int m_line = 1;
19498
19499 /* These are initialized in the constructor. */
19500
19501 CORE_ADDR m_address;
19502 bool m_is_stmt;
19503 unsigned int m_discriminator;
19504
19505 /* Additional bits of state we need to track. */
19506
19507 /* The last file that we called dwarf2_start_subfile for.
19508 This is only used for TLLs. */
19509 unsigned int m_last_file = 0;
19510 /* The last file a line number was recorded for. */
19511 struct subfile *m_last_subfile = NULL;
19512
19513 /* When true, record the lines we decode. */
19514 bool m_currently_recording_lines = false;
19515
19516 /* The last line number that was recorded, used to coalesce
19517 consecutive entries for the same line. This can happen, for
19518 example, when discriminators are present. PR 17276. */
19519 unsigned int m_last_line = 0;
19520 bool m_line_has_non_zero_discriminator = false;
19521 };
19522
19523 void
19524 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19525 {
19526 CORE_ADDR addr_adj = (((m_op_index + adjust)
19527 / m_line_header->maximum_ops_per_instruction)
19528 * m_line_header->minimum_instruction_length);
19529 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19530 m_op_index = ((m_op_index + adjust)
19531 % m_line_header->maximum_ops_per_instruction);
19532 }
19533
19534 void
19535 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19536 {
19537 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19538 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19539 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19540 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19541 / m_line_header->maximum_ops_per_instruction)
19542 * m_line_header->minimum_instruction_length);
19543 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19544 m_op_index = ((m_op_index + adj_opcode_d)
19545 % m_line_header->maximum_ops_per_instruction);
19546
19547 int line_delta = m_line_header->line_base + adj_opcode_r;
19548 advance_line (line_delta);
19549 record_line (false);
19550 m_discriminator = 0;
19551 }
19552
19553 void
19554 lnp_state_machine::handle_set_file (file_name_index file)
19555 {
19556 m_file = file;
19557
19558 const file_entry *fe = current_file ();
19559 if (fe == NULL)
19560 dwarf2_debug_line_missing_file_complaint ();
19561 else if (m_record_lines_p)
19562 {
19563 const char *dir = fe->include_dir (m_line_header);
19564
19565 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19566 m_line_has_non_zero_discriminator = m_discriminator != 0;
19567 dwarf2_start_subfile (m_cu, fe->name, dir);
19568 }
19569 }
19570
19571 void
19572 lnp_state_machine::handle_const_add_pc ()
19573 {
19574 CORE_ADDR adjust
19575 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19576
19577 CORE_ADDR addr_adj
19578 = (((m_op_index + adjust)
19579 / m_line_header->maximum_ops_per_instruction)
19580 * m_line_header->minimum_instruction_length);
19581
19582 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19583 m_op_index = ((m_op_index + adjust)
19584 % m_line_header->maximum_ops_per_instruction);
19585 }
19586
19587 /* Return non-zero if we should add LINE to the line number table.
19588 LINE is the line to add, LAST_LINE is the last line that was added,
19589 LAST_SUBFILE is the subfile for LAST_LINE.
19590 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19591 had a non-zero discriminator.
19592
19593 We have to be careful in the presence of discriminators.
19594 E.g., for this line:
19595
19596 for (i = 0; i < 100000; i++);
19597
19598 clang can emit four line number entries for that one line,
19599 each with a different discriminator.
19600 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19601
19602 However, we want gdb to coalesce all four entries into one.
19603 Otherwise the user could stepi into the middle of the line and
19604 gdb would get confused about whether the pc really was in the
19605 middle of the line.
19606
19607 Things are further complicated by the fact that two consecutive
19608 line number entries for the same line is a heuristic used by gcc
19609 to denote the end of the prologue. So we can't just discard duplicate
19610 entries, we have to be selective about it. The heuristic we use is
19611 that we only collapse consecutive entries for the same line if at least
19612 one of those entries has a non-zero discriminator. PR 17276.
19613
19614 Note: Addresses in the line number state machine can never go backwards
19615 within one sequence, thus this coalescing is ok. */
19616
19617 static int
19618 dwarf_record_line_p (struct dwarf2_cu *cu,
19619 unsigned int line, unsigned int last_line,
19620 int line_has_non_zero_discriminator,
19621 struct subfile *last_subfile)
19622 {
19623 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19624 return 1;
19625 if (line != last_line)
19626 return 1;
19627 /* Same line for the same file that we've seen already.
19628 As a last check, for pr 17276, only record the line if the line
19629 has never had a non-zero discriminator. */
19630 if (!line_has_non_zero_discriminator)
19631 return 1;
19632 return 0;
19633 }
19634
19635 /* Use the CU's builder to record line number LINE beginning at
19636 address ADDRESS in the line table of subfile SUBFILE. */
19637
19638 static void
19639 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19640 unsigned int line, CORE_ADDR address, bool is_stmt,
19641 struct dwarf2_cu *cu)
19642 {
19643 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19644
19645 if (dwarf_line_debug)
19646 {
19647 fprintf_unfiltered (gdb_stdlog,
19648 "Recording line %u, file %s, address %s\n",
19649 line, lbasename (subfile->name),
19650 paddress (gdbarch, address));
19651 }
19652
19653 if (cu != nullptr)
19654 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
19655 }
19656
19657 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19658 Mark the end of a set of line number records.
19659 The arguments are the same as for dwarf_record_line_1.
19660 If SUBFILE is NULL the request is ignored. */
19661
19662 static void
19663 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19664 CORE_ADDR address, struct dwarf2_cu *cu)
19665 {
19666 if (subfile == NULL)
19667 return;
19668
19669 if (dwarf_line_debug)
19670 {
19671 fprintf_unfiltered (gdb_stdlog,
19672 "Finishing current line, file %s, address %s\n",
19673 lbasename (subfile->name),
19674 paddress (gdbarch, address));
19675 }
19676
19677 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
19678 }
19679
19680 void
19681 lnp_state_machine::record_line (bool end_sequence)
19682 {
19683 if (dwarf_line_debug)
19684 {
19685 fprintf_unfiltered (gdb_stdlog,
19686 "Processing actual line %u: file %u,"
19687 " address %s, is_stmt %u, discrim %u%s\n",
19688 m_line, m_file,
19689 paddress (m_gdbarch, m_address),
19690 m_is_stmt, m_discriminator,
19691 (end_sequence ? "\t(end sequence)" : ""));
19692 }
19693
19694 file_entry *fe = current_file ();
19695
19696 if (fe == NULL)
19697 dwarf2_debug_line_missing_file_complaint ();
19698 /* For now we ignore lines not starting on an instruction boundary.
19699 But not when processing end_sequence for compatibility with the
19700 previous version of the code. */
19701 else if (m_op_index == 0 || end_sequence)
19702 {
19703 fe->included_p = 1;
19704 if (m_record_lines_p)
19705 {
19706 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
19707 || end_sequence)
19708 {
19709 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19710 m_currently_recording_lines ? m_cu : nullptr);
19711 }
19712
19713 if (!end_sequence)
19714 {
19715 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
19716
19717 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
19718 m_line_has_non_zero_discriminator,
19719 m_last_subfile))
19720 {
19721 buildsym_compunit *builder = m_cu->get_builder ();
19722 dwarf_record_line_1 (m_gdbarch,
19723 builder->get_current_subfile (),
19724 m_line, m_address, is_stmt,
19725 m_currently_recording_lines ? m_cu : nullptr);
19726 }
19727 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19728 m_last_line = m_line;
19729 }
19730 }
19731 }
19732 }
19733
19734 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
19735 line_header *lh, bool record_lines_p)
19736 {
19737 m_cu = cu;
19738 m_gdbarch = arch;
19739 m_record_lines_p = record_lines_p;
19740 m_line_header = lh;
19741
19742 m_currently_recording_lines = true;
19743
19744 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19745 was a line entry for it so that the backend has a chance to adjust it
19746 and also record it in case it needs it. This is currently used by MIPS
19747 code, cf. `mips_adjust_dwarf2_line'. */
19748 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19749 m_is_stmt = lh->default_is_stmt;
19750 m_discriminator = 0;
19751 }
19752
19753 void
19754 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19755 const gdb_byte *line_ptr,
19756 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
19757 {
19758 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
19759 the pc range of the CU. However, we restrict the test to only ADDRESS
19760 values of zero to preserve GDB's previous behaviour which is to handle
19761 the specific case of a function being GC'd by the linker. */
19762
19763 if (address == 0 && address < unrelocated_lowpc)
19764 {
19765 /* This line table is for a function which has been
19766 GCd by the linker. Ignore it. PR gdb/12528 */
19767
19768 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19769 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19770
19771 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
19772 line_offset, objfile_name (objfile));
19773 m_currently_recording_lines = false;
19774 /* Note: m_currently_recording_lines is left as false until we see
19775 DW_LNE_end_sequence. */
19776 }
19777 }
19778
19779 /* Subroutine of dwarf_decode_lines to simplify it.
19780 Process the line number information in LH.
19781 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19782 program in order to set included_p for every referenced header. */
19783
19784 static void
19785 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19786 const int decode_for_pst_p, CORE_ADDR lowpc)
19787 {
19788 const gdb_byte *line_ptr, *extended_end;
19789 const gdb_byte *line_end;
19790 unsigned int bytes_read, extended_len;
19791 unsigned char op_code, extended_op;
19792 CORE_ADDR baseaddr;
19793 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19794 bfd *abfd = objfile->obfd;
19795 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19796 /* True if we're recording line info (as opposed to building partial
19797 symtabs and just interested in finding include files mentioned by
19798 the line number program). */
19799 bool record_lines_p = !decode_for_pst_p;
19800
19801 baseaddr = objfile->text_section_offset ();
19802
19803 line_ptr = lh->statement_program_start;
19804 line_end = lh->statement_program_end;
19805
19806 /* Read the statement sequences until there's nothing left. */
19807 while (line_ptr < line_end)
19808 {
19809 /* The DWARF line number program state machine. Reset the state
19810 machine at the start of each sequence. */
19811 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
19812 bool end_sequence = false;
19813
19814 if (record_lines_p)
19815 {
19816 /* Start a subfile for the current file of the state
19817 machine. */
19818 const file_entry *fe = state_machine.current_file ();
19819
19820 if (fe != NULL)
19821 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
19822 }
19823
19824 /* Decode the table. */
19825 while (line_ptr < line_end && !end_sequence)
19826 {
19827 op_code = read_1_byte (abfd, line_ptr);
19828 line_ptr += 1;
19829
19830 if (op_code >= lh->opcode_base)
19831 {
19832 /* Special opcode. */
19833 state_machine.handle_special_opcode (op_code);
19834 }
19835 else switch (op_code)
19836 {
19837 case DW_LNS_extended_op:
19838 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19839 &bytes_read);
19840 line_ptr += bytes_read;
19841 extended_end = line_ptr + extended_len;
19842 extended_op = read_1_byte (abfd, line_ptr);
19843 line_ptr += 1;
19844 switch (extended_op)
19845 {
19846 case DW_LNE_end_sequence:
19847 state_machine.handle_end_sequence ();
19848 end_sequence = true;
19849 break;
19850 case DW_LNE_set_address:
19851 {
19852 CORE_ADDR address
19853 = cu->header.read_address (abfd, line_ptr, &bytes_read);
19854 line_ptr += bytes_read;
19855
19856 state_machine.check_line_address (cu, line_ptr,
19857 lowpc - baseaddr, address);
19858 state_machine.handle_set_address (baseaddr, address);
19859 }
19860 break;
19861 case DW_LNE_define_file:
19862 {
19863 const char *cur_file;
19864 unsigned int mod_time, length;
19865 dir_index dindex;
19866
19867 cur_file = read_direct_string (abfd, line_ptr,
19868 &bytes_read);
19869 line_ptr += bytes_read;
19870 dindex = (dir_index)
19871 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19872 line_ptr += bytes_read;
19873 mod_time =
19874 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19875 line_ptr += bytes_read;
19876 length =
19877 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19878 line_ptr += bytes_read;
19879 lh->add_file_name (cur_file, dindex, mod_time, length);
19880 }
19881 break;
19882 case DW_LNE_set_discriminator:
19883 {
19884 /* The discriminator is not interesting to the
19885 debugger; just ignore it. We still need to
19886 check its value though:
19887 if there are consecutive entries for the same
19888 (non-prologue) line we want to coalesce them.
19889 PR 17276. */
19890 unsigned int discr
19891 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19892 line_ptr += bytes_read;
19893
19894 state_machine.handle_set_discriminator (discr);
19895 }
19896 break;
19897 default:
19898 complaint (_("mangled .debug_line section"));
19899 return;
19900 }
19901 /* Make sure that we parsed the extended op correctly. If e.g.
19902 we expected a different address size than the producer used,
19903 we may have read the wrong number of bytes. */
19904 if (line_ptr != extended_end)
19905 {
19906 complaint (_("mangled .debug_line section"));
19907 return;
19908 }
19909 break;
19910 case DW_LNS_copy:
19911 state_machine.handle_copy ();
19912 break;
19913 case DW_LNS_advance_pc:
19914 {
19915 CORE_ADDR adjust
19916 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19917 line_ptr += bytes_read;
19918
19919 state_machine.handle_advance_pc (adjust);
19920 }
19921 break;
19922 case DW_LNS_advance_line:
19923 {
19924 int line_delta
19925 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
19926 line_ptr += bytes_read;
19927
19928 state_machine.handle_advance_line (line_delta);
19929 }
19930 break;
19931 case DW_LNS_set_file:
19932 {
19933 file_name_index file
19934 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19935 &bytes_read);
19936 line_ptr += bytes_read;
19937
19938 state_machine.handle_set_file (file);
19939 }
19940 break;
19941 case DW_LNS_set_column:
19942 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19943 line_ptr += bytes_read;
19944 break;
19945 case DW_LNS_negate_stmt:
19946 state_machine.handle_negate_stmt ();
19947 break;
19948 case DW_LNS_set_basic_block:
19949 break;
19950 /* Add to the address register of the state machine the
19951 address increment value corresponding to special opcode
19952 255. I.e., this value is scaled by the minimum
19953 instruction length since special opcode 255 would have
19954 scaled the increment. */
19955 case DW_LNS_const_add_pc:
19956 state_machine.handle_const_add_pc ();
19957 break;
19958 case DW_LNS_fixed_advance_pc:
19959 {
19960 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
19961 line_ptr += 2;
19962
19963 state_machine.handle_fixed_advance_pc (addr_adj);
19964 }
19965 break;
19966 default:
19967 {
19968 /* Unknown standard opcode, ignore it. */
19969 int i;
19970
19971 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
19972 {
19973 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19974 line_ptr += bytes_read;
19975 }
19976 }
19977 }
19978 }
19979
19980 if (!end_sequence)
19981 dwarf2_debug_line_missing_end_sequence_complaint ();
19982
19983 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
19984 in which case we still finish recording the last line). */
19985 state_machine.record_line (true);
19986 }
19987 }
19988
19989 /* Decode the Line Number Program (LNP) for the given line_header
19990 structure and CU. The actual information extracted and the type
19991 of structures created from the LNP depends on the value of PST.
19992
19993 1. If PST is NULL, then this procedure uses the data from the program
19994 to create all necessary symbol tables, and their linetables.
19995
19996 2. If PST is not NULL, this procedure reads the program to determine
19997 the list of files included by the unit represented by PST, and
19998 builds all the associated partial symbol tables.
19999
20000 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20001 It is used for relative paths in the line table.
20002 NOTE: When processing partial symtabs (pst != NULL),
20003 comp_dir == pst->dirname.
20004
20005 NOTE: It is important that psymtabs have the same file name (via strcmp)
20006 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20007 symtab we don't use it in the name of the psymtabs we create.
20008 E.g. expand_line_sal requires this when finding psymtabs to expand.
20009 A good testcase for this is mb-inline.exp.
20010
20011 LOWPC is the lowest address in CU (or 0 if not known).
20012
20013 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20014 for its PC<->lines mapping information. Otherwise only the filename
20015 table is read in. */
20016
20017 static void
20018 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20019 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20020 CORE_ADDR lowpc, int decode_mapping)
20021 {
20022 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20023 const int decode_for_pst_p = (pst != NULL);
20024
20025 if (decode_mapping)
20026 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20027
20028 if (decode_for_pst_p)
20029 {
20030 /* Now that we're done scanning the Line Header Program, we can
20031 create the psymtab of each included file. */
20032 for (auto &file_entry : lh->file_names ())
20033 if (file_entry.included_p == 1)
20034 {
20035 gdb::unique_xmalloc_ptr<char> name_holder;
20036 const char *include_name =
20037 psymtab_include_file_name (lh, file_entry, pst,
20038 comp_dir, &name_holder);
20039 if (include_name != NULL)
20040 dwarf2_create_include_psymtab (include_name, pst, objfile);
20041 }
20042 }
20043 else
20044 {
20045 /* Make sure a symtab is created for every file, even files
20046 which contain only variables (i.e. no code with associated
20047 line numbers). */
20048 buildsym_compunit *builder = cu->get_builder ();
20049 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20050
20051 for (auto &fe : lh->file_names ())
20052 {
20053 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20054 if (builder->get_current_subfile ()->symtab == NULL)
20055 {
20056 builder->get_current_subfile ()->symtab
20057 = allocate_symtab (cust,
20058 builder->get_current_subfile ()->name);
20059 }
20060 fe.symtab = builder->get_current_subfile ()->symtab;
20061 }
20062 }
20063 }
20064
20065 /* Start a subfile for DWARF. FILENAME is the name of the file and
20066 DIRNAME the name of the source directory which contains FILENAME
20067 or NULL if not known.
20068 This routine tries to keep line numbers from identical absolute and
20069 relative file names in a common subfile.
20070
20071 Using the `list' example from the GDB testsuite, which resides in
20072 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20073 of /srcdir/list0.c yields the following debugging information for list0.c:
20074
20075 DW_AT_name: /srcdir/list0.c
20076 DW_AT_comp_dir: /compdir
20077 files.files[0].name: list0.h
20078 files.files[0].dir: /srcdir
20079 files.files[1].name: list0.c
20080 files.files[1].dir: /srcdir
20081
20082 The line number information for list0.c has to end up in a single
20083 subfile, so that `break /srcdir/list0.c:1' works as expected.
20084 start_subfile will ensure that this happens provided that we pass the
20085 concatenation of files.files[1].dir and files.files[1].name as the
20086 subfile's name. */
20087
20088 static void
20089 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20090 const char *dirname)
20091 {
20092 gdb::unique_xmalloc_ptr<char> copy;
20093
20094 /* In order not to lose the line information directory,
20095 we concatenate it to the filename when it makes sense.
20096 Note that the Dwarf3 standard says (speaking of filenames in line
20097 information): ``The directory index is ignored for file names
20098 that represent full path names''. Thus ignoring dirname in the
20099 `else' branch below isn't an issue. */
20100
20101 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20102 {
20103 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20104 filename = copy.get ();
20105 }
20106
20107 cu->get_builder ()->start_subfile (filename);
20108 }
20109
20110 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20111 buildsym_compunit constructor. */
20112
20113 struct compunit_symtab *
20114 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20115 CORE_ADDR low_pc)
20116 {
20117 gdb_assert (m_builder == nullptr);
20118
20119 m_builder.reset (new struct buildsym_compunit
20120 (per_cu->dwarf2_per_objfile->objfile,
20121 name, comp_dir, language, low_pc));
20122
20123 list_in_scope = get_builder ()->get_file_symbols ();
20124
20125 get_builder ()->record_debugformat ("DWARF 2");
20126 get_builder ()->record_producer (producer);
20127
20128 processing_has_namespace_info = false;
20129
20130 return get_builder ()->get_compunit_symtab ();
20131 }
20132
20133 static void
20134 var_decode_location (struct attribute *attr, struct symbol *sym,
20135 struct dwarf2_cu *cu)
20136 {
20137 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20138 struct comp_unit_head *cu_header = &cu->header;
20139
20140 /* NOTE drow/2003-01-30: There used to be a comment and some special
20141 code here to turn a symbol with DW_AT_external and a
20142 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20143 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20144 with some versions of binutils) where shared libraries could have
20145 relocations against symbols in their debug information - the
20146 minimal symbol would have the right address, but the debug info
20147 would not. It's no longer necessary, because we will explicitly
20148 apply relocations when we read in the debug information now. */
20149
20150 /* A DW_AT_location attribute with no contents indicates that a
20151 variable has been optimized away. */
20152 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20153 {
20154 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20155 return;
20156 }
20157
20158 /* Handle one degenerate form of location expression specially, to
20159 preserve GDB's previous behavior when section offsets are
20160 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20161 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20162
20163 if (attr->form_is_block ()
20164 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20165 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20166 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20167 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20168 && (DW_BLOCK (attr)->size
20169 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20170 {
20171 unsigned int dummy;
20172
20173 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20174 SET_SYMBOL_VALUE_ADDRESS
20175 (sym, cu->header.read_address (objfile->obfd,
20176 DW_BLOCK (attr)->data + 1,
20177 &dummy));
20178 else
20179 SET_SYMBOL_VALUE_ADDRESS
20180 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20181 &dummy));
20182 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20183 fixup_symbol_section (sym, objfile);
20184 SET_SYMBOL_VALUE_ADDRESS
20185 (sym,
20186 SYMBOL_VALUE_ADDRESS (sym)
20187 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20188 return;
20189 }
20190
20191 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20192 expression evaluator, and use LOC_COMPUTED only when necessary
20193 (i.e. when the value of a register or memory location is
20194 referenced, or a thread-local block, etc.). Then again, it might
20195 not be worthwhile. I'm assuming that it isn't unless performance
20196 or memory numbers show me otherwise. */
20197
20198 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20199
20200 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20201 cu->has_loclist = true;
20202 }
20203
20204 /* Given a pointer to a DWARF information entry, figure out if we need
20205 to make a symbol table entry for it, and if so, create a new entry
20206 and return a pointer to it.
20207 If TYPE is NULL, determine symbol type from the die, otherwise
20208 used the passed type.
20209 If SPACE is not NULL, use it to hold the new symbol. If it is
20210 NULL, allocate a new symbol on the objfile's obstack. */
20211
20212 static struct symbol *
20213 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20214 struct symbol *space)
20215 {
20216 struct dwarf2_per_objfile *dwarf2_per_objfile
20217 = cu->per_cu->dwarf2_per_objfile;
20218 struct objfile *objfile = dwarf2_per_objfile->objfile;
20219 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20220 struct symbol *sym = NULL;
20221 const char *name;
20222 struct attribute *attr = NULL;
20223 struct attribute *attr2 = NULL;
20224 CORE_ADDR baseaddr;
20225 struct pending **list_to_add = NULL;
20226
20227 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20228
20229 baseaddr = objfile->text_section_offset ();
20230
20231 name = dwarf2_name (die, cu);
20232 if (name)
20233 {
20234 const char *linkagename;
20235 int suppress_add = 0;
20236
20237 if (space)
20238 sym = space;
20239 else
20240 sym = allocate_symbol (objfile);
20241 OBJSTAT (objfile, n_syms++);
20242
20243 /* Cache this symbol's name and the name's demangled form (if any). */
20244 sym->set_language (cu->language, &objfile->objfile_obstack);
20245 linkagename = dwarf2_physname (name, die, cu);
20246 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20247
20248 /* Fortran does not have mangling standard and the mangling does differ
20249 between gfortran, iFort etc. */
20250 if (cu->language == language_fortran
20251 && symbol_get_demangled_name (sym) == NULL)
20252 symbol_set_demangled_name (sym,
20253 dwarf2_full_name (name, die, cu),
20254 NULL);
20255
20256 /* Default assumptions.
20257 Use the passed type or decode it from the die. */
20258 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20259 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20260 if (type != NULL)
20261 SYMBOL_TYPE (sym) = type;
20262 else
20263 SYMBOL_TYPE (sym) = die_type (die, cu);
20264 attr = dwarf2_attr (die,
20265 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20266 cu);
20267 if (attr != nullptr)
20268 {
20269 SYMBOL_LINE (sym) = DW_UNSND (attr);
20270 }
20271
20272 attr = dwarf2_attr (die,
20273 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20274 cu);
20275 if (attr != nullptr)
20276 {
20277 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20278 struct file_entry *fe;
20279
20280 if (cu->line_header != NULL)
20281 fe = cu->line_header->file_name_at (file_index);
20282 else
20283 fe = NULL;
20284
20285 if (fe == NULL)
20286 complaint (_("file index out of range"));
20287 else
20288 symbol_set_symtab (sym, fe->symtab);
20289 }
20290
20291 switch (die->tag)
20292 {
20293 case DW_TAG_label:
20294 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20295 if (attr != nullptr)
20296 {
20297 CORE_ADDR addr;
20298
20299 addr = attr->value_as_address ();
20300 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20301 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20302 }
20303 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20304 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20305 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20306 add_symbol_to_list (sym, cu->list_in_scope);
20307 break;
20308 case DW_TAG_subprogram:
20309 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20310 finish_block. */
20311 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20312 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20313 if ((attr2 && (DW_UNSND (attr2) != 0))
20314 || cu->language == language_ada
20315 || cu->language == language_fortran)
20316 {
20317 /* Subprograms marked external are stored as a global symbol.
20318 Ada and Fortran subprograms, whether marked external or
20319 not, are always stored as a global symbol, because we want
20320 to be able to access them globally. For instance, we want
20321 to be able to break on a nested subprogram without having
20322 to specify the context. */
20323 list_to_add = cu->get_builder ()->get_global_symbols ();
20324 }
20325 else
20326 {
20327 list_to_add = cu->list_in_scope;
20328 }
20329 break;
20330 case DW_TAG_inlined_subroutine:
20331 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20332 finish_block. */
20333 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20334 SYMBOL_INLINED (sym) = 1;
20335 list_to_add = cu->list_in_scope;
20336 break;
20337 case DW_TAG_template_value_param:
20338 suppress_add = 1;
20339 /* Fall through. */
20340 case DW_TAG_constant:
20341 case DW_TAG_variable:
20342 case DW_TAG_member:
20343 /* Compilation with minimal debug info may result in
20344 variables with missing type entries. Change the
20345 misleading `void' type to something sensible. */
20346 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20347 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20348
20349 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20350 /* In the case of DW_TAG_member, we should only be called for
20351 static const members. */
20352 if (die->tag == DW_TAG_member)
20353 {
20354 /* dwarf2_add_field uses die_is_declaration,
20355 so we do the same. */
20356 gdb_assert (die_is_declaration (die, cu));
20357 gdb_assert (attr);
20358 }
20359 if (attr != nullptr)
20360 {
20361 dwarf2_const_value (attr, sym, cu);
20362 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20363 if (!suppress_add)
20364 {
20365 if (attr2 && (DW_UNSND (attr2) != 0))
20366 list_to_add = cu->get_builder ()->get_global_symbols ();
20367 else
20368 list_to_add = cu->list_in_scope;
20369 }
20370 break;
20371 }
20372 attr = dwarf2_attr (die, DW_AT_location, cu);
20373 if (attr != nullptr)
20374 {
20375 var_decode_location (attr, sym, cu);
20376 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20377
20378 /* Fortran explicitly imports any global symbols to the local
20379 scope by DW_TAG_common_block. */
20380 if (cu->language == language_fortran && die->parent
20381 && die->parent->tag == DW_TAG_common_block)
20382 attr2 = NULL;
20383
20384 if (SYMBOL_CLASS (sym) == LOC_STATIC
20385 && SYMBOL_VALUE_ADDRESS (sym) == 0
20386 && !dwarf2_per_objfile->has_section_at_zero)
20387 {
20388 /* When a static variable is eliminated by the linker,
20389 the corresponding debug information is not stripped
20390 out, but the variable address is set to null;
20391 do not add such variables into symbol table. */
20392 }
20393 else if (attr2 && (DW_UNSND (attr2) != 0))
20394 {
20395 if (SYMBOL_CLASS (sym) == LOC_STATIC
20396 && (objfile->flags & OBJF_MAINLINE) == 0
20397 && dwarf2_per_objfile->can_copy)
20398 {
20399 /* A global static variable might be subject to
20400 copy relocation. We first check for a local
20401 minsym, though, because maybe the symbol was
20402 marked hidden, in which case this would not
20403 apply. */
20404 bound_minimal_symbol found
20405 = (lookup_minimal_symbol_linkage
20406 (sym->linkage_name (), objfile));
20407 if (found.minsym != nullptr)
20408 sym->maybe_copied = 1;
20409 }
20410
20411 /* A variable with DW_AT_external is never static,
20412 but it may be block-scoped. */
20413 list_to_add
20414 = ((cu->list_in_scope
20415 == cu->get_builder ()->get_file_symbols ())
20416 ? cu->get_builder ()->get_global_symbols ()
20417 : cu->list_in_scope);
20418 }
20419 else
20420 list_to_add = cu->list_in_scope;
20421 }
20422 else
20423 {
20424 /* We do not know the address of this symbol.
20425 If it is an external symbol and we have type information
20426 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20427 The address of the variable will then be determined from
20428 the minimal symbol table whenever the variable is
20429 referenced. */
20430 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20431
20432 /* Fortran explicitly imports any global symbols to the local
20433 scope by DW_TAG_common_block. */
20434 if (cu->language == language_fortran && die->parent
20435 && die->parent->tag == DW_TAG_common_block)
20436 {
20437 /* SYMBOL_CLASS doesn't matter here because
20438 read_common_block is going to reset it. */
20439 if (!suppress_add)
20440 list_to_add = cu->list_in_scope;
20441 }
20442 else if (attr2 && (DW_UNSND (attr2) != 0)
20443 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20444 {
20445 /* A variable with DW_AT_external is never static, but it
20446 may be block-scoped. */
20447 list_to_add
20448 = ((cu->list_in_scope
20449 == cu->get_builder ()->get_file_symbols ())
20450 ? cu->get_builder ()->get_global_symbols ()
20451 : cu->list_in_scope);
20452
20453 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20454 }
20455 else if (!die_is_declaration (die, cu))
20456 {
20457 /* Use the default LOC_OPTIMIZED_OUT class. */
20458 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20459 if (!suppress_add)
20460 list_to_add = cu->list_in_scope;
20461 }
20462 }
20463 break;
20464 case DW_TAG_formal_parameter:
20465 {
20466 /* If we are inside a function, mark this as an argument. If
20467 not, we might be looking at an argument to an inlined function
20468 when we do not have enough information to show inlined frames;
20469 pretend it's a local variable in that case so that the user can
20470 still see it. */
20471 struct context_stack *curr
20472 = cu->get_builder ()->get_current_context_stack ();
20473 if (curr != nullptr && curr->name != nullptr)
20474 SYMBOL_IS_ARGUMENT (sym) = 1;
20475 attr = dwarf2_attr (die, DW_AT_location, cu);
20476 if (attr != nullptr)
20477 {
20478 var_decode_location (attr, sym, cu);
20479 }
20480 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20481 if (attr != nullptr)
20482 {
20483 dwarf2_const_value (attr, sym, cu);
20484 }
20485
20486 list_to_add = cu->list_in_scope;
20487 }
20488 break;
20489 case DW_TAG_unspecified_parameters:
20490 /* From varargs functions; gdb doesn't seem to have any
20491 interest in this information, so just ignore it for now.
20492 (FIXME?) */
20493 break;
20494 case DW_TAG_template_type_param:
20495 suppress_add = 1;
20496 /* Fall through. */
20497 case DW_TAG_class_type:
20498 case DW_TAG_interface_type:
20499 case DW_TAG_structure_type:
20500 case DW_TAG_union_type:
20501 case DW_TAG_set_type:
20502 case DW_TAG_enumeration_type:
20503 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20504 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20505
20506 {
20507 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20508 really ever be static objects: otherwise, if you try
20509 to, say, break of a class's method and you're in a file
20510 which doesn't mention that class, it won't work unless
20511 the check for all static symbols in lookup_symbol_aux
20512 saves you. See the OtherFileClass tests in
20513 gdb.c++/namespace.exp. */
20514
20515 if (!suppress_add)
20516 {
20517 buildsym_compunit *builder = cu->get_builder ();
20518 list_to_add
20519 = (cu->list_in_scope == builder->get_file_symbols ()
20520 && cu->language == language_cplus
20521 ? builder->get_global_symbols ()
20522 : cu->list_in_scope);
20523
20524 /* The semantics of C++ state that "struct foo {
20525 ... }" also defines a typedef for "foo". */
20526 if (cu->language == language_cplus
20527 || cu->language == language_ada
20528 || cu->language == language_d
20529 || cu->language == language_rust)
20530 {
20531 /* The symbol's name is already allocated along
20532 with this objfile, so we don't need to
20533 duplicate it for the type. */
20534 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20535 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20536 }
20537 }
20538 }
20539 break;
20540 case DW_TAG_typedef:
20541 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20542 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20543 list_to_add = cu->list_in_scope;
20544 break;
20545 case DW_TAG_base_type:
20546 case DW_TAG_subrange_type:
20547 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20548 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20549 list_to_add = cu->list_in_scope;
20550 break;
20551 case DW_TAG_enumerator:
20552 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20553 if (attr != nullptr)
20554 {
20555 dwarf2_const_value (attr, sym, cu);
20556 }
20557 {
20558 /* NOTE: carlton/2003-11-10: See comment above in the
20559 DW_TAG_class_type, etc. block. */
20560
20561 list_to_add
20562 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20563 && cu->language == language_cplus
20564 ? cu->get_builder ()->get_global_symbols ()
20565 : cu->list_in_scope);
20566 }
20567 break;
20568 case DW_TAG_imported_declaration:
20569 case DW_TAG_namespace:
20570 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20571 list_to_add = cu->get_builder ()->get_global_symbols ();
20572 break;
20573 case DW_TAG_module:
20574 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20575 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20576 list_to_add = cu->get_builder ()->get_global_symbols ();
20577 break;
20578 case DW_TAG_common_block:
20579 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20580 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20581 add_symbol_to_list (sym, cu->list_in_scope);
20582 break;
20583 default:
20584 /* Not a tag we recognize. Hopefully we aren't processing
20585 trash data, but since we must specifically ignore things
20586 we don't recognize, there is nothing else we should do at
20587 this point. */
20588 complaint (_("unsupported tag: '%s'"),
20589 dwarf_tag_name (die->tag));
20590 break;
20591 }
20592
20593 if (suppress_add)
20594 {
20595 sym->hash_next = objfile->template_symbols;
20596 objfile->template_symbols = sym;
20597 list_to_add = NULL;
20598 }
20599
20600 if (list_to_add != NULL)
20601 add_symbol_to_list (sym, list_to_add);
20602
20603 /* For the benefit of old versions of GCC, check for anonymous
20604 namespaces based on the demangled name. */
20605 if (!cu->processing_has_namespace_info
20606 && cu->language == language_cplus)
20607 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20608 }
20609 return (sym);
20610 }
20611
20612 /* Given an attr with a DW_FORM_dataN value in host byte order,
20613 zero-extend it as appropriate for the symbol's type. The DWARF
20614 standard (v4) is not entirely clear about the meaning of using
20615 DW_FORM_dataN for a constant with a signed type, where the type is
20616 wider than the data. The conclusion of a discussion on the DWARF
20617 list was that this is unspecified. We choose to always zero-extend
20618 because that is the interpretation long in use by GCC. */
20619
20620 static gdb_byte *
20621 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20622 struct dwarf2_cu *cu, LONGEST *value, int bits)
20623 {
20624 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20625 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20626 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20627 LONGEST l = DW_UNSND (attr);
20628
20629 if (bits < sizeof (*value) * 8)
20630 {
20631 l &= ((LONGEST) 1 << bits) - 1;
20632 *value = l;
20633 }
20634 else if (bits == sizeof (*value) * 8)
20635 *value = l;
20636 else
20637 {
20638 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20639 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20640 return bytes;
20641 }
20642
20643 return NULL;
20644 }
20645
20646 /* Read a constant value from an attribute. Either set *VALUE, or if
20647 the value does not fit in *VALUE, set *BYTES - either already
20648 allocated on the objfile obstack, or newly allocated on OBSTACK,
20649 or, set *BATON, if we translated the constant to a location
20650 expression. */
20651
20652 static void
20653 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20654 const char *name, struct obstack *obstack,
20655 struct dwarf2_cu *cu,
20656 LONGEST *value, const gdb_byte **bytes,
20657 struct dwarf2_locexpr_baton **baton)
20658 {
20659 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20660 struct comp_unit_head *cu_header = &cu->header;
20661 struct dwarf_block *blk;
20662 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20663 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20664
20665 *value = 0;
20666 *bytes = NULL;
20667 *baton = NULL;
20668
20669 switch (attr->form)
20670 {
20671 case DW_FORM_addr:
20672 case DW_FORM_addrx:
20673 case DW_FORM_GNU_addr_index:
20674 {
20675 gdb_byte *data;
20676
20677 if (TYPE_LENGTH (type) != cu_header->addr_size)
20678 dwarf2_const_value_length_mismatch_complaint (name,
20679 cu_header->addr_size,
20680 TYPE_LENGTH (type));
20681 /* Symbols of this form are reasonably rare, so we just
20682 piggyback on the existing location code rather than writing
20683 a new implementation of symbol_computed_ops. */
20684 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20685 (*baton)->per_cu = cu->per_cu;
20686 gdb_assert ((*baton)->per_cu);
20687
20688 (*baton)->size = 2 + cu_header->addr_size;
20689 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20690 (*baton)->data = data;
20691
20692 data[0] = DW_OP_addr;
20693 store_unsigned_integer (&data[1], cu_header->addr_size,
20694 byte_order, DW_ADDR (attr));
20695 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20696 }
20697 break;
20698 case DW_FORM_string:
20699 case DW_FORM_strp:
20700 case DW_FORM_strx:
20701 case DW_FORM_GNU_str_index:
20702 case DW_FORM_GNU_strp_alt:
20703 /* DW_STRING is already allocated on the objfile obstack, point
20704 directly to it. */
20705 *bytes = (const gdb_byte *) DW_STRING (attr);
20706 break;
20707 case DW_FORM_block1:
20708 case DW_FORM_block2:
20709 case DW_FORM_block4:
20710 case DW_FORM_block:
20711 case DW_FORM_exprloc:
20712 case DW_FORM_data16:
20713 blk = DW_BLOCK (attr);
20714 if (TYPE_LENGTH (type) != blk->size)
20715 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20716 TYPE_LENGTH (type));
20717 *bytes = blk->data;
20718 break;
20719
20720 /* The DW_AT_const_value attributes are supposed to carry the
20721 symbol's value "represented as it would be on the target
20722 architecture." By the time we get here, it's already been
20723 converted to host endianness, so we just need to sign- or
20724 zero-extend it as appropriate. */
20725 case DW_FORM_data1:
20726 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
20727 break;
20728 case DW_FORM_data2:
20729 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
20730 break;
20731 case DW_FORM_data4:
20732 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
20733 break;
20734 case DW_FORM_data8:
20735 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
20736 break;
20737
20738 case DW_FORM_sdata:
20739 case DW_FORM_implicit_const:
20740 *value = DW_SND (attr);
20741 break;
20742
20743 case DW_FORM_udata:
20744 *value = DW_UNSND (attr);
20745 break;
20746
20747 default:
20748 complaint (_("unsupported const value attribute form: '%s'"),
20749 dwarf_form_name (attr->form));
20750 *value = 0;
20751 break;
20752 }
20753 }
20754
20755
20756 /* Copy constant value from an attribute to a symbol. */
20757
20758 static void
20759 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
20760 struct dwarf2_cu *cu)
20761 {
20762 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20763 LONGEST value;
20764 const gdb_byte *bytes;
20765 struct dwarf2_locexpr_baton *baton;
20766
20767 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
20768 sym->print_name (),
20769 &objfile->objfile_obstack, cu,
20770 &value, &bytes, &baton);
20771
20772 if (baton != NULL)
20773 {
20774 SYMBOL_LOCATION_BATON (sym) = baton;
20775 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
20776 }
20777 else if (bytes != NULL)
20778 {
20779 SYMBOL_VALUE_BYTES (sym) = bytes;
20780 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
20781 }
20782 else
20783 {
20784 SYMBOL_VALUE (sym) = value;
20785 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
20786 }
20787 }
20788
20789 /* Return the type of the die in question using its DW_AT_type attribute. */
20790
20791 static struct type *
20792 die_type (struct die_info *die, struct dwarf2_cu *cu)
20793 {
20794 struct attribute *type_attr;
20795
20796 type_attr = dwarf2_attr (die, DW_AT_type, cu);
20797 if (!type_attr)
20798 {
20799 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20800 /* A missing DW_AT_type represents a void type. */
20801 return objfile_type (objfile)->builtin_void;
20802 }
20803
20804 return lookup_die_type (die, type_attr, cu);
20805 }
20806
20807 /* True iff CU's producer generates GNAT Ada auxiliary information
20808 that allows to find parallel types through that information instead
20809 of having to do expensive parallel lookups by type name. */
20810
20811 static int
20812 need_gnat_info (struct dwarf2_cu *cu)
20813 {
20814 /* Assume that the Ada compiler was GNAT, which always produces
20815 the auxiliary information. */
20816 return (cu->language == language_ada);
20817 }
20818
20819 /* Return the auxiliary type of the die in question using its
20820 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20821 attribute is not present. */
20822
20823 static struct type *
20824 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20825 {
20826 struct attribute *type_attr;
20827
20828 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20829 if (!type_attr)
20830 return NULL;
20831
20832 return lookup_die_type (die, type_attr, cu);
20833 }
20834
20835 /* If DIE has a descriptive_type attribute, then set the TYPE's
20836 descriptive type accordingly. */
20837
20838 static void
20839 set_descriptive_type (struct type *type, struct die_info *die,
20840 struct dwarf2_cu *cu)
20841 {
20842 struct type *descriptive_type = die_descriptive_type (die, cu);
20843
20844 if (descriptive_type)
20845 {
20846 ALLOCATE_GNAT_AUX_TYPE (type);
20847 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20848 }
20849 }
20850
20851 /* Return the containing type of the die in question using its
20852 DW_AT_containing_type attribute. */
20853
20854 static struct type *
20855 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
20856 {
20857 struct attribute *type_attr;
20858 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20859
20860 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
20861 if (!type_attr)
20862 error (_("Dwarf Error: Problem turning containing type into gdb type "
20863 "[in module %s]"), objfile_name (objfile));
20864
20865 return lookup_die_type (die, type_attr, cu);
20866 }
20867
20868 /* Return an error marker type to use for the ill formed type in DIE/CU. */
20869
20870 static struct type *
20871 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20872 {
20873 struct dwarf2_per_objfile *dwarf2_per_objfile
20874 = cu->per_cu->dwarf2_per_objfile;
20875 struct objfile *objfile = dwarf2_per_objfile->objfile;
20876 char *saved;
20877
20878 std::string message
20879 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
20880 objfile_name (objfile),
20881 sect_offset_str (cu->header.sect_off),
20882 sect_offset_str (die->sect_off));
20883 saved = obstack_strdup (&objfile->objfile_obstack, message);
20884
20885 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
20886 }
20887
20888 /* Look up the type of DIE in CU using its type attribute ATTR.
20889 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20890 DW_AT_containing_type.
20891 If there is no type substitute an error marker. */
20892
20893 static struct type *
20894 lookup_die_type (struct die_info *die, const struct attribute *attr,
20895 struct dwarf2_cu *cu)
20896 {
20897 struct dwarf2_per_objfile *dwarf2_per_objfile
20898 = cu->per_cu->dwarf2_per_objfile;
20899 struct objfile *objfile = dwarf2_per_objfile->objfile;
20900 struct type *this_type;
20901
20902 gdb_assert (attr->name == DW_AT_type
20903 || attr->name == DW_AT_GNAT_descriptive_type
20904 || attr->name == DW_AT_containing_type);
20905
20906 /* First see if we have it cached. */
20907
20908 if (attr->form == DW_FORM_GNU_ref_alt)
20909 {
20910 struct dwarf2_per_cu_data *per_cu;
20911 sect_offset sect_off = attr->get_ref_die_offset ();
20912
20913 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
20914 dwarf2_per_objfile);
20915 this_type = get_die_type_at_offset (sect_off, per_cu);
20916 }
20917 else if (attr->form_is_ref ())
20918 {
20919 sect_offset sect_off = attr->get_ref_die_offset ();
20920
20921 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
20922 }
20923 else if (attr->form == DW_FORM_ref_sig8)
20924 {
20925 ULONGEST signature = DW_SIGNATURE (attr);
20926
20927 return get_signatured_type (die, signature, cu);
20928 }
20929 else
20930 {
20931 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
20932 " at %s [in module %s]"),
20933 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
20934 objfile_name (objfile));
20935 return build_error_marker_type (cu, die);
20936 }
20937
20938 /* If not cached we need to read it in. */
20939
20940 if (this_type == NULL)
20941 {
20942 struct die_info *type_die = NULL;
20943 struct dwarf2_cu *type_cu = cu;
20944
20945 if (attr->form_is_ref ())
20946 type_die = follow_die_ref (die, attr, &type_cu);
20947 if (type_die == NULL)
20948 return build_error_marker_type (cu, die);
20949 /* If we find the type now, it's probably because the type came
20950 from an inter-CU reference and the type's CU got expanded before
20951 ours. */
20952 this_type = read_type_die (type_die, type_cu);
20953 }
20954
20955 /* If we still don't have a type use an error marker. */
20956
20957 if (this_type == NULL)
20958 return build_error_marker_type (cu, die);
20959
20960 return this_type;
20961 }
20962
20963 /* Return the type in DIE, CU.
20964 Returns NULL for invalid types.
20965
20966 This first does a lookup in die_type_hash,
20967 and only reads the die in if necessary.
20968
20969 NOTE: This can be called when reading in partial or full symbols. */
20970
20971 static struct type *
20972 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
20973 {
20974 struct type *this_type;
20975
20976 this_type = get_die_type (die, cu);
20977 if (this_type)
20978 return this_type;
20979
20980 return read_type_die_1 (die, cu);
20981 }
20982
20983 /* Read the type in DIE, CU.
20984 Returns NULL for invalid types. */
20985
20986 static struct type *
20987 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
20988 {
20989 struct type *this_type = NULL;
20990
20991 switch (die->tag)
20992 {
20993 case DW_TAG_class_type:
20994 case DW_TAG_interface_type:
20995 case DW_TAG_structure_type:
20996 case DW_TAG_union_type:
20997 this_type = read_structure_type (die, cu);
20998 break;
20999 case DW_TAG_enumeration_type:
21000 this_type = read_enumeration_type (die, cu);
21001 break;
21002 case DW_TAG_subprogram:
21003 case DW_TAG_subroutine_type:
21004 case DW_TAG_inlined_subroutine:
21005 this_type = read_subroutine_type (die, cu);
21006 break;
21007 case DW_TAG_array_type:
21008 this_type = read_array_type (die, cu);
21009 break;
21010 case DW_TAG_set_type:
21011 this_type = read_set_type (die, cu);
21012 break;
21013 case DW_TAG_pointer_type:
21014 this_type = read_tag_pointer_type (die, cu);
21015 break;
21016 case DW_TAG_ptr_to_member_type:
21017 this_type = read_tag_ptr_to_member_type (die, cu);
21018 break;
21019 case DW_TAG_reference_type:
21020 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21021 break;
21022 case DW_TAG_rvalue_reference_type:
21023 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21024 break;
21025 case DW_TAG_const_type:
21026 this_type = read_tag_const_type (die, cu);
21027 break;
21028 case DW_TAG_volatile_type:
21029 this_type = read_tag_volatile_type (die, cu);
21030 break;
21031 case DW_TAG_restrict_type:
21032 this_type = read_tag_restrict_type (die, cu);
21033 break;
21034 case DW_TAG_string_type:
21035 this_type = read_tag_string_type (die, cu);
21036 break;
21037 case DW_TAG_typedef:
21038 this_type = read_typedef (die, cu);
21039 break;
21040 case DW_TAG_subrange_type:
21041 this_type = read_subrange_type (die, cu);
21042 break;
21043 case DW_TAG_base_type:
21044 this_type = read_base_type (die, cu);
21045 break;
21046 case DW_TAG_unspecified_type:
21047 this_type = read_unspecified_type (die, cu);
21048 break;
21049 case DW_TAG_namespace:
21050 this_type = read_namespace_type (die, cu);
21051 break;
21052 case DW_TAG_module:
21053 this_type = read_module_type (die, cu);
21054 break;
21055 case DW_TAG_atomic_type:
21056 this_type = read_tag_atomic_type (die, cu);
21057 break;
21058 default:
21059 complaint (_("unexpected tag in read_type_die: '%s'"),
21060 dwarf_tag_name (die->tag));
21061 break;
21062 }
21063
21064 return this_type;
21065 }
21066
21067 /* See if we can figure out if the class lives in a namespace. We do
21068 this by looking for a member function; its demangled name will
21069 contain namespace info, if there is any.
21070 Return the computed name or NULL.
21071 Space for the result is allocated on the objfile's obstack.
21072 This is the full-die version of guess_partial_die_structure_name.
21073 In this case we know DIE has no useful parent. */
21074
21075 static const char *
21076 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21077 {
21078 struct die_info *spec_die;
21079 struct dwarf2_cu *spec_cu;
21080 struct die_info *child;
21081 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21082
21083 spec_cu = cu;
21084 spec_die = die_specification (die, &spec_cu);
21085 if (spec_die != NULL)
21086 {
21087 die = spec_die;
21088 cu = spec_cu;
21089 }
21090
21091 for (child = die->child;
21092 child != NULL;
21093 child = child->sibling)
21094 {
21095 if (child->tag == DW_TAG_subprogram)
21096 {
21097 const char *linkage_name = dw2_linkage_name (child, cu);
21098
21099 if (linkage_name != NULL)
21100 {
21101 gdb::unique_xmalloc_ptr<char> actual_name
21102 (language_class_name_from_physname (cu->language_defn,
21103 linkage_name));
21104 const char *name = NULL;
21105
21106 if (actual_name != NULL)
21107 {
21108 const char *die_name = dwarf2_name (die, cu);
21109
21110 if (die_name != NULL
21111 && strcmp (die_name, actual_name.get ()) != 0)
21112 {
21113 /* Strip off the class name from the full name.
21114 We want the prefix. */
21115 int die_name_len = strlen (die_name);
21116 int actual_name_len = strlen (actual_name.get ());
21117 const char *ptr = actual_name.get ();
21118
21119 /* Test for '::' as a sanity check. */
21120 if (actual_name_len > die_name_len + 2
21121 && ptr[actual_name_len - die_name_len - 1] == ':')
21122 name = obstack_strndup (
21123 &objfile->per_bfd->storage_obstack,
21124 ptr, actual_name_len - die_name_len - 2);
21125 }
21126 }
21127 return name;
21128 }
21129 }
21130 }
21131
21132 return NULL;
21133 }
21134
21135 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21136 prefix part in such case. See
21137 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21138
21139 static const char *
21140 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21141 {
21142 struct attribute *attr;
21143 const char *base;
21144
21145 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21146 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21147 return NULL;
21148
21149 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21150 return NULL;
21151
21152 attr = dw2_linkage_name_attr (die, cu);
21153 if (attr == NULL || DW_STRING (attr) == NULL)
21154 return NULL;
21155
21156 /* dwarf2_name had to be already called. */
21157 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21158
21159 /* Strip the base name, keep any leading namespaces/classes. */
21160 base = strrchr (DW_STRING (attr), ':');
21161 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21162 return "";
21163
21164 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21165 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21166 DW_STRING (attr),
21167 &base[-1] - DW_STRING (attr));
21168 }
21169
21170 /* Return the name of the namespace/class that DIE is defined within,
21171 or "" if we can't tell. The caller should not xfree the result.
21172
21173 For example, if we're within the method foo() in the following
21174 code:
21175
21176 namespace N {
21177 class C {
21178 void foo () {
21179 }
21180 };
21181 }
21182
21183 then determine_prefix on foo's die will return "N::C". */
21184
21185 static const char *
21186 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21187 {
21188 struct dwarf2_per_objfile *dwarf2_per_objfile
21189 = cu->per_cu->dwarf2_per_objfile;
21190 struct die_info *parent, *spec_die;
21191 struct dwarf2_cu *spec_cu;
21192 struct type *parent_type;
21193 const char *retval;
21194
21195 if (cu->language != language_cplus
21196 && cu->language != language_fortran && cu->language != language_d
21197 && cu->language != language_rust)
21198 return "";
21199
21200 retval = anonymous_struct_prefix (die, cu);
21201 if (retval)
21202 return retval;
21203
21204 /* We have to be careful in the presence of DW_AT_specification.
21205 For example, with GCC 3.4, given the code
21206
21207 namespace N {
21208 void foo() {
21209 // Definition of N::foo.
21210 }
21211 }
21212
21213 then we'll have a tree of DIEs like this:
21214
21215 1: DW_TAG_compile_unit
21216 2: DW_TAG_namespace // N
21217 3: DW_TAG_subprogram // declaration of N::foo
21218 4: DW_TAG_subprogram // definition of N::foo
21219 DW_AT_specification // refers to die #3
21220
21221 Thus, when processing die #4, we have to pretend that we're in
21222 the context of its DW_AT_specification, namely the contex of die
21223 #3. */
21224 spec_cu = cu;
21225 spec_die = die_specification (die, &spec_cu);
21226 if (spec_die == NULL)
21227 parent = die->parent;
21228 else
21229 {
21230 parent = spec_die->parent;
21231 cu = spec_cu;
21232 }
21233
21234 if (parent == NULL)
21235 return "";
21236 else if (parent->building_fullname)
21237 {
21238 const char *name;
21239 const char *parent_name;
21240
21241 /* It has been seen on RealView 2.2 built binaries,
21242 DW_TAG_template_type_param types actually _defined_ as
21243 children of the parent class:
21244
21245 enum E {};
21246 template class <class Enum> Class{};
21247 Class<enum E> class_e;
21248
21249 1: DW_TAG_class_type (Class)
21250 2: DW_TAG_enumeration_type (E)
21251 3: DW_TAG_enumerator (enum1:0)
21252 3: DW_TAG_enumerator (enum2:1)
21253 ...
21254 2: DW_TAG_template_type_param
21255 DW_AT_type DW_FORM_ref_udata (E)
21256
21257 Besides being broken debug info, it can put GDB into an
21258 infinite loop. Consider:
21259
21260 When we're building the full name for Class<E>, we'll start
21261 at Class, and go look over its template type parameters,
21262 finding E. We'll then try to build the full name of E, and
21263 reach here. We're now trying to build the full name of E,
21264 and look over the parent DIE for containing scope. In the
21265 broken case, if we followed the parent DIE of E, we'd again
21266 find Class, and once again go look at its template type
21267 arguments, etc., etc. Simply don't consider such parent die
21268 as source-level parent of this die (it can't be, the language
21269 doesn't allow it), and break the loop here. */
21270 name = dwarf2_name (die, cu);
21271 parent_name = dwarf2_name (parent, cu);
21272 complaint (_("template param type '%s' defined within parent '%s'"),
21273 name ? name : "<unknown>",
21274 parent_name ? parent_name : "<unknown>");
21275 return "";
21276 }
21277 else
21278 switch (parent->tag)
21279 {
21280 case DW_TAG_namespace:
21281 parent_type = read_type_die (parent, cu);
21282 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21283 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21284 Work around this problem here. */
21285 if (cu->language == language_cplus
21286 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21287 return "";
21288 /* We give a name to even anonymous namespaces. */
21289 return TYPE_NAME (parent_type);
21290 case DW_TAG_class_type:
21291 case DW_TAG_interface_type:
21292 case DW_TAG_structure_type:
21293 case DW_TAG_union_type:
21294 case DW_TAG_module:
21295 parent_type = read_type_die (parent, cu);
21296 if (TYPE_NAME (parent_type) != NULL)
21297 return TYPE_NAME (parent_type);
21298 else
21299 /* An anonymous structure is only allowed non-static data
21300 members; no typedefs, no member functions, et cetera.
21301 So it does not need a prefix. */
21302 return "";
21303 case DW_TAG_compile_unit:
21304 case DW_TAG_partial_unit:
21305 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21306 if (cu->language == language_cplus
21307 && !dwarf2_per_objfile->types.empty ()
21308 && die->child != NULL
21309 && (die->tag == DW_TAG_class_type
21310 || die->tag == DW_TAG_structure_type
21311 || die->tag == DW_TAG_union_type))
21312 {
21313 const char *name = guess_full_die_structure_name (die, cu);
21314 if (name != NULL)
21315 return name;
21316 }
21317 return "";
21318 case DW_TAG_subprogram:
21319 /* Nested subroutines in Fortran get a prefix with the name
21320 of the parent's subroutine. */
21321 if (cu->language == language_fortran)
21322 {
21323 if ((die->tag == DW_TAG_subprogram)
21324 && (dwarf2_name (parent, cu) != NULL))
21325 return dwarf2_name (parent, cu);
21326 }
21327 return determine_prefix (parent, cu);
21328 case DW_TAG_enumeration_type:
21329 parent_type = read_type_die (parent, cu);
21330 if (TYPE_DECLARED_CLASS (parent_type))
21331 {
21332 if (TYPE_NAME (parent_type) != NULL)
21333 return TYPE_NAME (parent_type);
21334 return "";
21335 }
21336 /* Fall through. */
21337 default:
21338 return determine_prefix (parent, cu);
21339 }
21340 }
21341
21342 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21343 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21344 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21345 an obconcat, otherwise allocate storage for the result. The CU argument is
21346 used to determine the language and hence, the appropriate separator. */
21347
21348 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21349
21350 static char *
21351 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21352 int physname, struct dwarf2_cu *cu)
21353 {
21354 const char *lead = "";
21355 const char *sep;
21356
21357 if (suffix == NULL || suffix[0] == '\0'
21358 || prefix == NULL || prefix[0] == '\0')
21359 sep = "";
21360 else if (cu->language == language_d)
21361 {
21362 /* For D, the 'main' function could be defined in any module, but it
21363 should never be prefixed. */
21364 if (strcmp (suffix, "D main") == 0)
21365 {
21366 prefix = "";
21367 sep = "";
21368 }
21369 else
21370 sep = ".";
21371 }
21372 else if (cu->language == language_fortran && physname)
21373 {
21374 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21375 DW_AT_MIPS_linkage_name is preferred and used instead. */
21376
21377 lead = "__";
21378 sep = "_MOD_";
21379 }
21380 else
21381 sep = "::";
21382
21383 if (prefix == NULL)
21384 prefix = "";
21385 if (suffix == NULL)
21386 suffix = "";
21387
21388 if (obs == NULL)
21389 {
21390 char *retval
21391 = ((char *)
21392 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21393
21394 strcpy (retval, lead);
21395 strcat (retval, prefix);
21396 strcat (retval, sep);
21397 strcat (retval, suffix);
21398 return retval;
21399 }
21400 else
21401 {
21402 /* We have an obstack. */
21403 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21404 }
21405 }
21406
21407 /* Get name of a die, return NULL if not found. */
21408
21409 static const char *
21410 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21411 struct objfile *objfile)
21412 {
21413 if (name && cu->language == language_cplus)
21414 {
21415 std::string canon_name = cp_canonicalize_string (name);
21416
21417 if (!canon_name.empty ())
21418 {
21419 if (canon_name != name)
21420 name = objfile->intern (canon_name);
21421 }
21422 }
21423
21424 return name;
21425 }
21426
21427 /* Get name of a die, return NULL if not found.
21428 Anonymous namespaces are converted to their magic string. */
21429
21430 static const char *
21431 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21432 {
21433 struct attribute *attr;
21434 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21435
21436 attr = dwarf2_attr (die, DW_AT_name, cu);
21437 if ((!attr || !DW_STRING (attr))
21438 && die->tag != DW_TAG_namespace
21439 && die->tag != DW_TAG_class_type
21440 && die->tag != DW_TAG_interface_type
21441 && die->tag != DW_TAG_structure_type
21442 && die->tag != DW_TAG_union_type)
21443 return NULL;
21444
21445 switch (die->tag)
21446 {
21447 case DW_TAG_compile_unit:
21448 case DW_TAG_partial_unit:
21449 /* Compilation units have a DW_AT_name that is a filename, not
21450 a source language identifier. */
21451 case DW_TAG_enumeration_type:
21452 case DW_TAG_enumerator:
21453 /* These tags always have simple identifiers already; no need
21454 to canonicalize them. */
21455 return DW_STRING (attr);
21456
21457 case DW_TAG_namespace:
21458 if (attr != NULL && DW_STRING (attr) != NULL)
21459 return DW_STRING (attr);
21460 return CP_ANONYMOUS_NAMESPACE_STR;
21461
21462 case DW_TAG_class_type:
21463 case DW_TAG_interface_type:
21464 case DW_TAG_structure_type:
21465 case DW_TAG_union_type:
21466 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21467 structures or unions. These were of the form "._%d" in GCC 4.1,
21468 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21469 and GCC 4.4. We work around this problem by ignoring these. */
21470 if (attr && DW_STRING (attr)
21471 && (startswith (DW_STRING (attr), "._")
21472 || startswith (DW_STRING (attr), "<anonymous")))
21473 return NULL;
21474
21475 /* GCC might emit a nameless typedef that has a linkage name. See
21476 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21477 if (!attr || DW_STRING (attr) == NULL)
21478 {
21479 attr = dw2_linkage_name_attr (die, cu);
21480 if (attr == NULL || DW_STRING (attr) == NULL)
21481 return NULL;
21482
21483 /* Avoid demangling DW_STRING (attr) the second time on a second
21484 call for the same DIE. */
21485 if (!DW_STRING_IS_CANONICAL (attr))
21486 {
21487 gdb::unique_xmalloc_ptr<char> demangled
21488 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21489 if (demangled == nullptr)
21490 return nullptr;
21491
21492 DW_STRING (attr) = objfile->intern (demangled.get ());
21493 DW_STRING_IS_CANONICAL (attr) = 1;
21494 }
21495
21496 /* Strip any leading namespaces/classes, keep only the base name.
21497 DW_AT_name for named DIEs does not contain the prefixes. */
21498 const char *base = strrchr (DW_STRING (attr), ':');
21499 if (base && base > DW_STRING (attr) && base[-1] == ':')
21500 return &base[1];
21501 else
21502 return DW_STRING (attr);
21503 }
21504 break;
21505
21506 default:
21507 break;
21508 }
21509
21510 if (!DW_STRING_IS_CANONICAL (attr))
21511 {
21512 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21513 objfile);
21514 DW_STRING_IS_CANONICAL (attr) = 1;
21515 }
21516 return DW_STRING (attr);
21517 }
21518
21519 /* Return the die that this die in an extension of, or NULL if there
21520 is none. *EXT_CU is the CU containing DIE on input, and the CU
21521 containing the return value on output. */
21522
21523 static struct die_info *
21524 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21525 {
21526 struct attribute *attr;
21527
21528 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21529 if (attr == NULL)
21530 return NULL;
21531
21532 return follow_die_ref (die, attr, ext_cu);
21533 }
21534
21535 static void
21536 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21537 {
21538 unsigned int i;
21539
21540 print_spaces (indent, f);
21541 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21542 dwarf_tag_name (die->tag), die->abbrev,
21543 sect_offset_str (die->sect_off));
21544
21545 if (die->parent != NULL)
21546 {
21547 print_spaces (indent, f);
21548 fprintf_unfiltered (f, " parent at offset: %s\n",
21549 sect_offset_str (die->parent->sect_off));
21550 }
21551
21552 print_spaces (indent, f);
21553 fprintf_unfiltered (f, " has children: %s\n",
21554 dwarf_bool_name (die->child != NULL));
21555
21556 print_spaces (indent, f);
21557 fprintf_unfiltered (f, " attributes:\n");
21558
21559 for (i = 0; i < die->num_attrs; ++i)
21560 {
21561 print_spaces (indent, f);
21562 fprintf_unfiltered (f, " %s (%s) ",
21563 dwarf_attr_name (die->attrs[i].name),
21564 dwarf_form_name (die->attrs[i].form));
21565
21566 switch (die->attrs[i].form)
21567 {
21568 case DW_FORM_addr:
21569 case DW_FORM_addrx:
21570 case DW_FORM_GNU_addr_index:
21571 fprintf_unfiltered (f, "address: ");
21572 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21573 break;
21574 case DW_FORM_block2:
21575 case DW_FORM_block4:
21576 case DW_FORM_block:
21577 case DW_FORM_block1:
21578 fprintf_unfiltered (f, "block: size %s",
21579 pulongest (DW_BLOCK (&die->attrs[i])->size));
21580 break;
21581 case DW_FORM_exprloc:
21582 fprintf_unfiltered (f, "expression: size %s",
21583 pulongest (DW_BLOCK (&die->attrs[i])->size));
21584 break;
21585 case DW_FORM_data16:
21586 fprintf_unfiltered (f, "constant of 16 bytes");
21587 break;
21588 case DW_FORM_ref_addr:
21589 fprintf_unfiltered (f, "ref address: ");
21590 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21591 break;
21592 case DW_FORM_GNU_ref_alt:
21593 fprintf_unfiltered (f, "alt ref address: ");
21594 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21595 break;
21596 case DW_FORM_ref1:
21597 case DW_FORM_ref2:
21598 case DW_FORM_ref4:
21599 case DW_FORM_ref8:
21600 case DW_FORM_ref_udata:
21601 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21602 (long) (DW_UNSND (&die->attrs[i])));
21603 break;
21604 case DW_FORM_data1:
21605 case DW_FORM_data2:
21606 case DW_FORM_data4:
21607 case DW_FORM_data8:
21608 case DW_FORM_udata:
21609 case DW_FORM_sdata:
21610 fprintf_unfiltered (f, "constant: %s",
21611 pulongest (DW_UNSND (&die->attrs[i])));
21612 break;
21613 case DW_FORM_sec_offset:
21614 fprintf_unfiltered (f, "section offset: %s",
21615 pulongest (DW_UNSND (&die->attrs[i])));
21616 break;
21617 case DW_FORM_ref_sig8:
21618 fprintf_unfiltered (f, "signature: %s",
21619 hex_string (DW_SIGNATURE (&die->attrs[i])));
21620 break;
21621 case DW_FORM_string:
21622 case DW_FORM_strp:
21623 case DW_FORM_line_strp:
21624 case DW_FORM_strx:
21625 case DW_FORM_GNU_str_index:
21626 case DW_FORM_GNU_strp_alt:
21627 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21628 DW_STRING (&die->attrs[i])
21629 ? DW_STRING (&die->attrs[i]) : "",
21630 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21631 break;
21632 case DW_FORM_flag:
21633 if (DW_UNSND (&die->attrs[i]))
21634 fprintf_unfiltered (f, "flag: TRUE");
21635 else
21636 fprintf_unfiltered (f, "flag: FALSE");
21637 break;
21638 case DW_FORM_flag_present:
21639 fprintf_unfiltered (f, "flag: TRUE");
21640 break;
21641 case DW_FORM_indirect:
21642 /* The reader will have reduced the indirect form to
21643 the "base form" so this form should not occur. */
21644 fprintf_unfiltered (f,
21645 "unexpected attribute form: DW_FORM_indirect");
21646 break;
21647 case DW_FORM_implicit_const:
21648 fprintf_unfiltered (f, "constant: %s",
21649 plongest (DW_SND (&die->attrs[i])));
21650 break;
21651 default:
21652 fprintf_unfiltered (f, "unsupported attribute form: %d.",
21653 die->attrs[i].form);
21654 break;
21655 }
21656 fprintf_unfiltered (f, "\n");
21657 }
21658 }
21659
21660 static void
21661 dump_die_for_error (struct die_info *die)
21662 {
21663 dump_die_shallow (gdb_stderr, 0, die);
21664 }
21665
21666 static void
21667 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21668 {
21669 int indent = level * 4;
21670
21671 gdb_assert (die != NULL);
21672
21673 if (level >= max_level)
21674 return;
21675
21676 dump_die_shallow (f, indent, die);
21677
21678 if (die->child != NULL)
21679 {
21680 print_spaces (indent, f);
21681 fprintf_unfiltered (f, " Children:");
21682 if (level + 1 < max_level)
21683 {
21684 fprintf_unfiltered (f, "\n");
21685 dump_die_1 (f, level + 1, max_level, die->child);
21686 }
21687 else
21688 {
21689 fprintf_unfiltered (f,
21690 " [not printed, max nesting level reached]\n");
21691 }
21692 }
21693
21694 if (die->sibling != NULL && level > 0)
21695 {
21696 dump_die_1 (f, level, max_level, die->sibling);
21697 }
21698 }
21699
21700 /* This is called from the pdie macro in gdbinit.in.
21701 It's not static so gcc will keep a copy callable from gdb. */
21702
21703 void
21704 dump_die (struct die_info *die, int max_level)
21705 {
21706 dump_die_1 (gdb_stdlog, 0, max_level, die);
21707 }
21708
21709 static void
21710 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
21711 {
21712 void **slot;
21713
21714 slot = htab_find_slot_with_hash (cu->die_hash, die,
21715 to_underlying (die->sect_off),
21716 INSERT);
21717
21718 *slot = die;
21719 }
21720
21721 /* Follow reference or signature attribute ATTR of SRC_DIE.
21722 On entry *REF_CU is the CU of SRC_DIE.
21723 On exit *REF_CU is the CU of the result. */
21724
21725 static struct die_info *
21726 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
21727 struct dwarf2_cu **ref_cu)
21728 {
21729 struct die_info *die;
21730
21731 if (attr->form_is_ref ())
21732 die = follow_die_ref (src_die, attr, ref_cu);
21733 else if (attr->form == DW_FORM_ref_sig8)
21734 die = follow_die_sig (src_die, attr, ref_cu);
21735 else
21736 {
21737 dump_die_for_error (src_die);
21738 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
21739 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
21740 }
21741
21742 return die;
21743 }
21744
21745 /* Follow reference OFFSET.
21746 On entry *REF_CU is the CU of the source die referencing OFFSET.
21747 On exit *REF_CU is the CU of the result.
21748 Returns NULL if OFFSET is invalid. */
21749
21750 static struct die_info *
21751 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
21752 struct dwarf2_cu **ref_cu)
21753 {
21754 struct die_info temp_die;
21755 struct dwarf2_cu *target_cu, *cu = *ref_cu;
21756 struct dwarf2_per_objfile *dwarf2_per_objfile
21757 = cu->per_cu->dwarf2_per_objfile;
21758
21759 gdb_assert (cu->per_cu != NULL);
21760
21761 target_cu = cu;
21762
21763 if (cu->per_cu->is_debug_types)
21764 {
21765 /* .debug_types CUs cannot reference anything outside their CU.
21766 If they need to, they have to reference a signatured type via
21767 DW_FORM_ref_sig8. */
21768 if (!cu->header.offset_in_cu_p (sect_off))
21769 return NULL;
21770 }
21771 else if (offset_in_dwz != cu->per_cu->is_dwz
21772 || !cu->header.offset_in_cu_p (sect_off))
21773 {
21774 struct dwarf2_per_cu_data *per_cu;
21775
21776 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
21777 dwarf2_per_objfile);
21778
21779 /* If necessary, add it to the queue and load its DIEs. */
21780 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
21781 load_full_comp_unit (per_cu, false, cu->language);
21782
21783 target_cu = per_cu->cu;
21784 }
21785 else if (cu->dies == NULL)
21786 {
21787 /* We're loading full DIEs during partial symbol reading. */
21788 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
21789 load_full_comp_unit (cu->per_cu, false, language_minimal);
21790 }
21791
21792 *ref_cu = target_cu;
21793 temp_die.sect_off = sect_off;
21794
21795 if (target_cu != cu)
21796 target_cu->ancestor = cu;
21797
21798 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
21799 &temp_die,
21800 to_underlying (sect_off));
21801 }
21802
21803 /* Follow reference attribute ATTR of SRC_DIE.
21804 On entry *REF_CU is the CU of SRC_DIE.
21805 On exit *REF_CU is the CU of the result. */
21806
21807 static struct die_info *
21808 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
21809 struct dwarf2_cu **ref_cu)
21810 {
21811 sect_offset sect_off = attr->get_ref_die_offset ();
21812 struct dwarf2_cu *cu = *ref_cu;
21813 struct die_info *die;
21814
21815 die = follow_die_offset (sect_off,
21816 (attr->form == DW_FORM_GNU_ref_alt
21817 || cu->per_cu->is_dwz),
21818 ref_cu);
21819 if (!die)
21820 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
21821 "at %s [in module %s]"),
21822 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
21823 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
21824
21825 return die;
21826 }
21827
21828 /* See read.h. */
21829
21830 struct dwarf2_locexpr_baton
21831 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
21832 dwarf2_per_cu_data *per_cu,
21833 CORE_ADDR (*get_frame_pc) (void *baton),
21834 void *baton, bool resolve_abstract_p)
21835 {
21836 struct dwarf2_cu *cu;
21837 struct die_info *die;
21838 struct attribute *attr;
21839 struct dwarf2_locexpr_baton retval;
21840 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
21841 struct objfile *objfile = dwarf2_per_objfile->objfile;
21842
21843 if (per_cu->cu == NULL)
21844 load_cu (per_cu, false);
21845 cu = per_cu->cu;
21846 if (cu == NULL)
21847 {
21848 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21849 Instead just throw an error, not much else we can do. */
21850 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21851 sect_offset_str (sect_off), objfile_name (objfile));
21852 }
21853
21854 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21855 if (!die)
21856 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21857 sect_offset_str (sect_off), objfile_name (objfile));
21858
21859 attr = dwarf2_attr (die, DW_AT_location, cu);
21860 if (!attr && resolve_abstract_p
21861 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
21862 != dwarf2_per_objfile->abstract_to_concrete.end ()))
21863 {
21864 CORE_ADDR pc = (*get_frame_pc) (baton);
21865 CORE_ADDR baseaddr = objfile->text_section_offset ();
21866 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21867
21868 for (const auto &cand_off
21869 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
21870 {
21871 struct dwarf2_cu *cand_cu = cu;
21872 struct die_info *cand
21873 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
21874 if (!cand
21875 || !cand->parent
21876 || cand->parent->tag != DW_TAG_subprogram)
21877 continue;
21878
21879 CORE_ADDR pc_low, pc_high;
21880 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
21881 if (pc_low == ((CORE_ADDR) -1))
21882 continue;
21883 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
21884 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
21885 if (!(pc_low <= pc && pc < pc_high))
21886 continue;
21887
21888 die = cand;
21889 attr = dwarf2_attr (die, DW_AT_location, cu);
21890 break;
21891 }
21892 }
21893
21894 if (!attr)
21895 {
21896 /* DWARF: "If there is no such attribute, then there is no effect.".
21897 DATA is ignored if SIZE is 0. */
21898
21899 retval.data = NULL;
21900 retval.size = 0;
21901 }
21902 else if (attr->form_is_section_offset ())
21903 {
21904 struct dwarf2_loclist_baton loclist_baton;
21905 CORE_ADDR pc = (*get_frame_pc) (baton);
21906 size_t size;
21907
21908 fill_in_loclist_baton (cu, &loclist_baton, attr);
21909
21910 retval.data = dwarf2_find_location_expression (&loclist_baton,
21911 &size, pc);
21912 retval.size = size;
21913 }
21914 else
21915 {
21916 if (!attr->form_is_block ())
21917 error (_("Dwarf Error: DIE at %s referenced in module %s "
21918 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
21919 sect_offset_str (sect_off), objfile_name (objfile));
21920
21921 retval.data = DW_BLOCK (attr)->data;
21922 retval.size = DW_BLOCK (attr)->size;
21923 }
21924 retval.per_cu = cu->per_cu;
21925
21926 age_cached_comp_units (dwarf2_per_objfile);
21927
21928 return retval;
21929 }
21930
21931 /* See read.h. */
21932
21933 struct dwarf2_locexpr_baton
21934 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
21935 dwarf2_per_cu_data *per_cu,
21936 CORE_ADDR (*get_frame_pc) (void *baton),
21937 void *baton)
21938 {
21939 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
21940
21941 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
21942 }
21943
21944 /* Write a constant of a given type as target-ordered bytes into
21945 OBSTACK. */
21946
21947 static const gdb_byte *
21948 write_constant_as_bytes (struct obstack *obstack,
21949 enum bfd_endian byte_order,
21950 struct type *type,
21951 ULONGEST value,
21952 LONGEST *len)
21953 {
21954 gdb_byte *result;
21955
21956 *len = TYPE_LENGTH (type);
21957 result = (gdb_byte *) obstack_alloc (obstack, *len);
21958 store_unsigned_integer (result, *len, byte_order, value);
21959
21960 return result;
21961 }
21962
21963 /* See read.h. */
21964
21965 const gdb_byte *
21966 dwarf2_fetch_constant_bytes (sect_offset sect_off,
21967 dwarf2_per_cu_data *per_cu,
21968 obstack *obstack,
21969 LONGEST *len)
21970 {
21971 struct dwarf2_cu *cu;
21972 struct die_info *die;
21973 struct attribute *attr;
21974 const gdb_byte *result = NULL;
21975 struct type *type;
21976 LONGEST value;
21977 enum bfd_endian byte_order;
21978 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
21979
21980 if (per_cu->cu == NULL)
21981 load_cu (per_cu, false);
21982 cu = per_cu->cu;
21983 if (cu == NULL)
21984 {
21985 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21986 Instead just throw an error, not much else we can do. */
21987 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21988 sect_offset_str (sect_off), objfile_name (objfile));
21989 }
21990
21991 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21992 if (!die)
21993 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21994 sect_offset_str (sect_off), objfile_name (objfile));
21995
21996 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21997 if (attr == NULL)
21998 return NULL;
21999
22000 byte_order = (bfd_big_endian (objfile->obfd)
22001 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22002
22003 switch (attr->form)
22004 {
22005 case DW_FORM_addr:
22006 case DW_FORM_addrx:
22007 case DW_FORM_GNU_addr_index:
22008 {
22009 gdb_byte *tem;
22010
22011 *len = cu->header.addr_size;
22012 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22013 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22014 result = tem;
22015 }
22016 break;
22017 case DW_FORM_string:
22018 case DW_FORM_strp:
22019 case DW_FORM_strx:
22020 case DW_FORM_GNU_str_index:
22021 case DW_FORM_GNU_strp_alt:
22022 /* DW_STRING is already allocated on the objfile obstack, point
22023 directly to it. */
22024 result = (const gdb_byte *) DW_STRING (attr);
22025 *len = strlen (DW_STRING (attr));
22026 break;
22027 case DW_FORM_block1:
22028 case DW_FORM_block2:
22029 case DW_FORM_block4:
22030 case DW_FORM_block:
22031 case DW_FORM_exprloc:
22032 case DW_FORM_data16:
22033 result = DW_BLOCK (attr)->data;
22034 *len = DW_BLOCK (attr)->size;
22035 break;
22036
22037 /* The DW_AT_const_value attributes are supposed to carry the
22038 symbol's value "represented as it would be on the target
22039 architecture." By the time we get here, it's already been
22040 converted to host endianness, so we just need to sign- or
22041 zero-extend it as appropriate. */
22042 case DW_FORM_data1:
22043 type = die_type (die, cu);
22044 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22045 if (result == NULL)
22046 result = write_constant_as_bytes (obstack, byte_order,
22047 type, value, len);
22048 break;
22049 case DW_FORM_data2:
22050 type = die_type (die, cu);
22051 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22052 if (result == NULL)
22053 result = write_constant_as_bytes (obstack, byte_order,
22054 type, value, len);
22055 break;
22056 case DW_FORM_data4:
22057 type = die_type (die, cu);
22058 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22059 if (result == NULL)
22060 result = write_constant_as_bytes (obstack, byte_order,
22061 type, value, len);
22062 break;
22063 case DW_FORM_data8:
22064 type = die_type (die, cu);
22065 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22066 if (result == NULL)
22067 result = write_constant_as_bytes (obstack, byte_order,
22068 type, value, len);
22069 break;
22070
22071 case DW_FORM_sdata:
22072 case DW_FORM_implicit_const:
22073 type = die_type (die, cu);
22074 result = write_constant_as_bytes (obstack, byte_order,
22075 type, DW_SND (attr), len);
22076 break;
22077
22078 case DW_FORM_udata:
22079 type = die_type (die, cu);
22080 result = write_constant_as_bytes (obstack, byte_order,
22081 type, DW_UNSND (attr), len);
22082 break;
22083
22084 default:
22085 complaint (_("unsupported const value attribute form: '%s'"),
22086 dwarf_form_name (attr->form));
22087 break;
22088 }
22089
22090 return result;
22091 }
22092
22093 /* See read.h. */
22094
22095 struct type *
22096 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22097 dwarf2_per_cu_data *per_cu)
22098 {
22099 struct dwarf2_cu *cu;
22100 struct die_info *die;
22101
22102 if (per_cu->cu == NULL)
22103 load_cu (per_cu, false);
22104 cu = per_cu->cu;
22105 if (!cu)
22106 return NULL;
22107
22108 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22109 if (!die)
22110 return NULL;
22111
22112 return die_type (die, cu);
22113 }
22114
22115 /* See read.h. */
22116
22117 struct type *
22118 dwarf2_get_die_type (cu_offset die_offset,
22119 struct dwarf2_per_cu_data *per_cu)
22120 {
22121 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22122 return get_die_type_at_offset (die_offset_sect, per_cu);
22123 }
22124
22125 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22126 On entry *REF_CU is the CU of SRC_DIE.
22127 On exit *REF_CU is the CU of the result.
22128 Returns NULL if the referenced DIE isn't found. */
22129
22130 static struct die_info *
22131 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22132 struct dwarf2_cu **ref_cu)
22133 {
22134 struct die_info temp_die;
22135 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22136 struct die_info *die;
22137
22138 /* While it might be nice to assert sig_type->type == NULL here,
22139 we can get here for DW_AT_imported_declaration where we need
22140 the DIE not the type. */
22141
22142 /* If necessary, add it to the queue and load its DIEs. */
22143
22144 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22145 read_signatured_type (sig_type);
22146
22147 sig_cu = sig_type->per_cu.cu;
22148 gdb_assert (sig_cu != NULL);
22149 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22150 temp_die.sect_off = sig_type->type_offset_in_section;
22151 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22152 to_underlying (temp_die.sect_off));
22153 if (die)
22154 {
22155 struct dwarf2_per_objfile *dwarf2_per_objfile
22156 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22157
22158 /* For .gdb_index version 7 keep track of included TUs.
22159 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22160 if (dwarf2_per_objfile->index_table != NULL
22161 && dwarf2_per_objfile->index_table->version <= 7)
22162 {
22163 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22164 }
22165
22166 *ref_cu = sig_cu;
22167 if (sig_cu != cu)
22168 sig_cu->ancestor = cu;
22169
22170 return die;
22171 }
22172
22173 return NULL;
22174 }
22175
22176 /* Follow signatured type referenced by ATTR in SRC_DIE.
22177 On entry *REF_CU is the CU of SRC_DIE.
22178 On exit *REF_CU is the CU of the result.
22179 The result is the DIE of the type.
22180 If the referenced type cannot be found an error is thrown. */
22181
22182 static struct die_info *
22183 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22184 struct dwarf2_cu **ref_cu)
22185 {
22186 ULONGEST signature = DW_SIGNATURE (attr);
22187 struct signatured_type *sig_type;
22188 struct die_info *die;
22189
22190 gdb_assert (attr->form == DW_FORM_ref_sig8);
22191
22192 sig_type = lookup_signatured_type (*ref_cu, signature);
22193 /* sig_type will be NULL if the signatured type is missing from
22194 the debug info. */
22195 if (sig_type == NULL)
22196 {
22197 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22198 " from DIE at %s [in module %s]"),
22199 hex_string (signature), sect_offset_str (src_die->sect_off),
22200 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22201 }
22202
22203 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22204 if (die == NULL)
22205 {
22206 dump_die_for_error (src_die);
22207 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22208 " from DIE at %s [in module %s]"),
22209 hex_string (signature), sect_offset_str (src_die->sect_off),
22210 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22211 }
22212
22213 return die;
22214 }
22215
22216 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22217 reading in and processing the type unit if necessary. */
22218
22219 static struct type *
22220 get_signatured_type (struct die_info *die, ULONGEST signature,
22221 struct dwarf2_cu *cu)
22222 {
22223 struct dwarf2_per_objfile *dwarf2_per_objfile
22224 = cu->per_cu->dwarf2_per_objfile;
22225 struct signatured_type *sig_type;
22226 struct dwarf2_cu *type_cu;
22227 struct die_info *type_die;
22228 struct type *type;
22229
22230 sig_type = lookup_signatured_type (cu, signature);
22231 /* sig_type will be NULL if the signatured type is missing from
22232 the debug info. */
22233 if (sig_type == NULL)
22234 {
22235 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22236 " from DIE at %s [in module %s]"),
22237 hex_string (signature), sect_offset_str (die->sect_off),
22238 objfile_name (dwarf2_per_objfile->objfile));
22239 return build_error_marker_type (cu, die);
22240 }
22241
22242 /* If we already know the type we're done. */
22243 if (sig_type->type != NULL)
22244 return sig_type->type;
22245
22246 type_cu = cu;
22247 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22248 if (type_die != NULL)
22249 {
22250 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22251 is created. This is important, for example, because for c++ classes
22252 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22253 type = read_type_die (type_die, type_cu);
22254 if (type == NULL)
22255 {
22256 complaint (_("Dwarf Error: Cannot build signatured type %s"
22257 " referenced from DIE at %s [in module %s]"),
22258 hex_string (signature), sect_offset_str (die->sect_off),
22259 objfile_name (dwarf2_per_objfile->objfile));
22260 type = build_error_marker_type (cu, die);
22261 }
22262 }
22263 else
22264 {
22265 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22266 " from DIE at %s [in module %s]"),
22267 hex_string (signature), sect_offset_str (die->sect_off),
22268 objfile_name (dwarf2_per_objfile->objfile));
22269 type = build_error_marker_type (cu, die);
22270 }
22271 sig_type->type = type;
22272
22273 return type;
22274 }
22275
22276 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22277 reading in and processing the type unit if necessary. */
22278
22279 static struct type *
22280 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22281 struct dwarf2_cu *cu) /* ARI: editCase function */
22282 {
22283 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22284 if (attr->form_is_ref ())
22285 {
22286 struct dwarf2_cu *type_cu = cu;
22287 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22288
22289 return read_type_die (type_die, type_cu);
22290 }
22291 else if (attr->form == DW_FORM_ref_sig8)
22292 {
22293 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22294 }
22295 else
22296 {
22297 struct dwarf2_per_objfile *dwarf2_per_objfile
22298 = cu->per_cu->dwarf2_per_objfile;
22299
22300 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22301 " at %s [in module %s]"),
22302 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22303 objfile_name (dwarf2_per_objfile->objfile));
22304 return build_error_marker_type (cu, die);
22305 }
22306 }
22307
22308 /* Load the DIEs associated with type unit PER_CU into memory. */
22309
22310 static void
22311 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22312 {
22313 struct signatured_type *sig_type;
22314
22315 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22316 gdb_assert (! per_cu->type_unit_group_p ());
22317
22318 /* We have the per_cu, but we need the signatured_type.
22319 Fortunately this is an easy translation. */
22320 gdb_assert (per_cu->is_debug_types);
22321 sig_type = (struct signatured_type *) per_cu;
22322
22323 gdb_assert (per_cu->cu == NULL);
22324
22325 read_signatured_type (sig_type);
22326
22327 gdb_assert (per_cu->cu != NULL);
22328 }
22329
22330 /* Read in a signatured type and build its CU and DIEs.
22331 If the type is a stub for the real type in a DWO file,
22332 read in the real type from the DWO file as well. */
22333
22334 static void
22335 read_signatured_type (struct signatured_type *sig_type)
22336 {
22337 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22338
22339 gdb_assert (per_cu->is_debug_types);
22340 gdb_assert (per_cu->cu == NULL);
22341
22342 cutu_reader reader (per_cu, NULL, 0, false);
22343
22344 if (!reader.dummy_p)
22345 {
22346 struct dwarf2_cu *cu = reader.cu;
22347 const gdb_byte *info_ptr = reader.info_ptr;
22348
22349 gdb_assert (cu->die_hash == NULL);
22350 cu->die_hash =
22351 htab_create_alloc_ex (cu->header.length / 12,
22352 die_hash,
22353 die_eq,
22354 NULL,
22355 &cu->comp_unit_obstack,
22356 hashtab_obstack_allocate,
22357 dummy_obstack_deallocate);
22358
22359 if (reader.comp_unit_die->has_children)
22360 reader.comp_unit_die->child
22361 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22362 reader.comp_unit_die);
22363 cu->dies = reader.comp_unit_die;
22364 /* comp_unit_die is not stored in die_hash, no need. */
22365
22366 /* We try not to read any attributes in this function, because
22367 not all CUs needed for references have been loaded yet, and
22368 symbol table processing isn't initialized. But we have to
22369 set the CU language, or we won't be able to build types
22370 correctly. Similarly, if we do not read the producer, we can
22371 not apply producer-specific interpretation. */
22372 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22373
22374 reader.keep ();
22375 }
22376
22377 sig_type->per_cu.tu_read = 1;
22378 }
22379
22380 /* Decode simple location descriptions.
22381 Given a pointer to a dwarf block that defines a location, compute
22382 the location and return the value.
22383
22384 NOTE drow/2003-11-18: This function is called in two situations
22385 now: for the address of static or global variables (partial symbols
22386 only) and for offsets into structures which are expected to be
22387 (more or less) constant. The partial symbol case should go away,
22388 and only the constant case should remain. That will let this
22389 function complain more accurately. A few special modes are allowed
22390 without complaint for global variables (for instance, global
22391 register values and thread-local values).
22392
22393 A location description containing no operations indicates that the
22394 object is optimized out. The return value is 0 for that case.
22395 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22396 callers will only want a very basic result and this can become a
22397 complaint.
22398
22399 Note that stack[0] is unused except as a default error return. */
22400
22401 static CORE_ADDR
22402 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22403 {
22404 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22405 size_t i;
22406 size_t size = blk->size;
22407 const gdb_byte *data = blk->data;
22408 CORE_ADDR stack[64];
22409 int stacki;
22410 unsigned int bytes_read, unsnd;
22411 gdb_byte op;
22412
22413 i = 0;
22414 stacki = 0;
22415 stack[stacki] = 0;
22416 stack[++stacki] = 0;
22417
22418 while (i < size)
22419 {
22420 op = data[i++];
22421 switch (op)
22422 {
22423 case DW_OP_lit0:
22424 case DW_OP_lit1:
22425 case DW_OP_lit2:
22426 case DW_OP_lit3:
22427 case DW_OP_lit4:
22428 case DW_OP_lit5:
22429 case DW_OP_lit6:
22430 case DW_OP_lit7:
22431 case DW_OP_lit8:
22432 case DW_OP_lit9:
22433 case DW_OP_lit10:
22434 case DW_OP_lit11:
22435 case DW_OP_lit12:
22436 case DW_OP_lit13:
22437 case DW_OP_lit14:
22438 case DW_OP_lit15:
22439 case DW_OP_lit16:
22440 case DW_OP_lit17:
22441 case DW_OP_lit18:
22442 case DW_OP_lit19:
22443 case DW_OP_lit20:
22444 case DW_OP_lit21:
22445 case DW_OP_lit22:
22446 case DW_OP_lit23:
22447 case DW_OP_lit24:
22448 case DW_OP_lit25:
22449 case DW_OP_lit26:
22450 case DW_OP_lit27:
22451 case DW_OP_lit28:
22452 case DW_OP_lit29:
22453 case DW_OP_lit30:
22454 case DW_OP_lit31:
22455 stack[++stacki] = op - DW_OP_lit0;
22456 break;
22457
22458 case DW_OP_reg0:
22459 case DW_OP_reg1:
22460 case DW_OP_reg2:
22461 case DW_OP_reg3:
22462 case DW_OP_reg4:
22463 case DW_OP_reg5:
22464 case DW_OP_reg6:
22465 case DW_OP_reg7:
22466 case DW_OP_reg8:
22467 case DW_OP_reg9:
22468 case DW_OP_reg10:
22469 case DW_OP_reg11:
22470 case DW_OP_reg12:
22471 case DW_OP_reg13:
22472 case DW_OP_reg14:
22473 case DW_OP_reg15:
22474 case DW_OP_reg16:
22475 case DW_OP_reg17:
22476 case DW_OP_reg18:
22477 case DW_OP_reg19:
22478 case DW_OP_reg20:
22479 case DW_OP_reg21:
22480 case DW_OP_reg22:
22481 case DW_OP_reg23:
22482 case DW_OP_reg24:
22483 case DW_OP_reg25:
22484 case DW_OP_reg26:
22485 case DW_OP_reg27:
22486 case DW_OP_reg28:
22487 case DW_OP_reg29:
22488 case DW_OP_reg30:
22489 case DW_OP_reg31:
22490 stack[++stacki] = op - DW_OP_reg0;
22491 if (i < size)
22492 dwarf2_complex_location_expr_complaint ();
22493 break;
22494
22495 case DW_OP_regx:
22496 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22497 i += bytes_read;
22498 stack[++stacki] = unsnd;
22499 if (i < size)
22500 dwarf2_complex_location_expr_complaint ();
22501 break;
22502
22503 case DW_OP_addr:
22504 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22505 &bytes_read);
22506 i += bytes_read;
22507 break;
22508
22509 case DW_OP_const1u:
22510 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22511 i += 1;
22512 break;
22513
22514 case DW_OP_const1s:
22515 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22516 i += 1;
22517 break;
22518
22519 case DW_OP_const2u:
22520 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22521 i += 2;
22522 break;
22523
22524 case DW_OP_const2s:
22525 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22526 i += 2;
22527 break;
22528
22529 case DW_OP_const4u:
22530 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22531 i += 4;
22532 break;
22533
22534 case DW_OP_const4s:
22535 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22536 i += 4;
22537 break;
22538
22539 case DW_OP_const8u:
22540 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22541 i += 8;
22542 break;
22543
22544 case DW_OP_constu:
22545 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22546 &bytes_read);
22547 i += bytes_read;
22548 break;
22549
22550 case DW_OP_consts:
22551 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22552 i += bytes_read;
22553 break;
22554
22555 case DW_OP_dup:
22556 stack[stacki + 1] = stack[stacki];
22557 stacki++;
22558 break;
22559
22560 case DW_OP_plus:
22561 stack[stacki - 1] += stack[stacki];
22562 stacki--;
22563 break;
22564
22565 case DW_OP_plus_uconst:
22566 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22567 &bytes_read);
22568 i += bytes_read;
22569 break;
22570
22571 case DW_OP_minus:
22572 stack[stacki - 1] -= stack[stacki];
22573 stacki--;
22574 break;
22575
22576 case DW_OP_deref:
22577 /* If we're not the last op, then we definitely can't encode
22578 this using GDB's address_class enum. This is valid for partial
22579 global symbols, although the variable's address will be bogus
22580 in the psymtab. */
22581 if (i < size)
22582 dwarf2_complex_location_expr_complaint ();
22583 break;
22584
22585 case DW_OP_GNU_push_tls_address:
22586 case DW_OP_form_tls_address:
22587 /* The top of the stack has the offset from the beginning
22588 of the thread control block at which the variable is located. */
22589 /* Nothing should follow this operator, so the top of stack would
22590 be returned. */
22591 /* This is valid for partial global symbols, but the variable's
22592 address will be bogus in the psymtab. Make it always at least
22593 non-zero to not look as a variable garbage collected by linker
22594 which have DW_OP_addr 0. */
22595 if (i < size)
22596 dwarf2_complex_location_expr_complaint ();
22597 stack[stacki]++;
22598 break;
22599
22600 case DW_OP_GNU_uninit:
22601 break;
22602
22603 case DW_OP_addrx:
22604 case DW_OP_GNU_addr_index:
22605 case DW_OP_GNU_const_index:
22606 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22607 &bytes_read);
22608 i += bytes_read;
22609 break;
22610
22611 default:
22612 {
22613 const char *name = get_DW_OP_name (op);
22614
22615 if (name)
22616 complaint (_("unsupported stack op: '%s'"),
22617 name);
22618 else
22619 complaint (_("unsupported stack op: '%02x'"),
22620 op);
22621 }
22622
22623 return (stack[stacki]);
22624 }
22625
22626 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22627 outside of the allocated space. Also enforce minimum>0. */
22628 if (stacki >= ARRAY_SIZE (stack) - 1)
22629 {
22630 complaint (_("location description stack overflow"));
22631 return 0;
22632 }
22633
22634 if (stacki <= 0)
22635 {
22636 complaint (_("location description stack underflow"));
22637 return 0;
22638 }
22639 }
22640 return (stack[stacki]);
22641 }
22642
22643 /* memory allocation interface */
22644
22645 static struct dwarf_block *
22646 dwarf_alloc_block (struct dwarf2_cu *cu)
22647 {
22648 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
22649 }
22650
22651 static struct die_info *
22652 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
22653 {
22654 struct die_info *die;
22655 size_t size = sizeof (struct die_info);
22656
22657 if (num_attrs > 1)
22658 size += (num_attrs - 1) * sizeof (struct attribute);
22659
22660 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
22661 memset (die, 0, sizeof (struct die_info));
22662 return (die);
22663 }
22664
22665 \f
22666
22667 /* Macro support. */
22668
22669 /* An overload of dwarf_decode_macros that finds the correct section
22670 and ensures it is read in before calling the other overload. */
22671
22672 static void
22673 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22674 int section_is_gnu)
22675 {
22676 struct dwarf2_per_objfile *dwarf2_per_objfile
22677 = cu->per_cu->dwarf2_per_objfile;
22678 struct objfile *objfile = dwarf2_per_objfile->objfile;
22679 const struct line_header *lh = cu->line_header;
22680 unsigned int offset_size = cu->header.offset_size;
22681 struct dwarf2_section_info *section;
22682 const char *section_name;
22683
22684 if (cu->dwo_unit != nullptr)
22685 {
22686 if (section_is_gnu)
22687 {
22688 section = &cu->dwo_unit->dwo_file->sections.macro;
22689 section_name = ".debug_macro.dwo";
22690 }
22691 else
22692 {
22693 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22694 section_name = ".debug_macinfo.dwo";
22695 }
22696 }
22697 else
22698 {
22699 if (section_is_gnu)
22700 {
22701 section = &dwarf2_per_objfile->macro;
22702 section_name = ".debug_macro";
22703 }
22704 else
22705 {
22706 section = &dwarf2_per_objfile->macinfo;
22707 section_name = ".debug_macinfo";
22708 }
22709 }
22710
22711 section->read (objfile);
22712 if (section->buffer == nullptr)
22713 {
22714 complaint (_("missing %s section"), section_name);
22715 return;
22716 }
22717
22718 buildsym_compunit *builder = cu->get_builder ();
22719
22720 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
22721 offset_size, offset, section_is_gnu);
22722 }
22723
22724 /* Return the .debug_loc section to use for CU.
22725 For DWO files use .debug_loc.dwo. */
22726
22727 static struct dwarf2_section_info *
22728 cu_debug_loc_section (struct dwarf2_cu *cu)
22729 {
22730 struct dwarf2_per_objfile *dwarf2_per_objfile
22731 = cu->per_cu->dwarf2_per_objfile;
22732
22733 if (cu->dwo_unit)
22734 {
22735 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22736
22737 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22738 }
22739 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22740 : &dwarf2_per_objfile->loc);
22741 }
22742
22743 /* A helper function that fills in a dwarf2_loclist_baton. */
22744
22745 static void
22746 fill_in_loclist_baton (struct dwarf2_cu *cu,
22747 struct dwarf2_loclist_baton *baton,
22748 const struct attribute *attr)
22749 {
22750 struct dwarf2_per_objfile *dwarf2_per_objfile
22751 = cu->per_cu->dwarf2_per_objfile;
22752 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22753
22754 section->read (dwarf2_per_objfile->objfile);
22755
22756 baton->per_cu = cu->per_cu;
22757 gdb_assert (baton->per_cu);
22758 /* We don't know how long the location list is, but make sure we
22759 don't run off the edge of the section. */
22760 baton->size = section->size - DW_UNSND (attr);
22761 baton->data = section->buffer + DW_UNSND (attr);
22762 if (cu->base_address.has_value ())
22763 baton->base_address = *cu->base_address;
22764 else
22765 baton->base_address = 0;
22766 baton->from_dwo = cu->dwo_unit != NULL;
22767 }
22768
22769 static void
22770 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
22771 struct dwarf2_cu *cu, int is_block)
22772 {
22773 struct dwarf2_per_objfile *dwarf2_per_objfile
22774 = cu->per_cu->dwarf2_per_objfile;
22775 struct objfile *objfile = dwarf2_per_objfile->objfile;
22776 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22777
22778 if (attr->form_is_section_offset ()
22779 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
22780 the section. If so, fall through to the complaint in the
22781 other branch. */
22782 && DW_UNSND (attr) < section->get_size (objfile))
22783 {
22784 struct dwarf2_loclist_baton *baton;
22785
22786 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
22787
22788 fill_in_loclist_baton (cu, baton, attr);
22789
22790 if (!cu->base_address.has_value ())
22791 complaint (_("Location list used without "
22792 "specifying the CU base address."));
22793
22794 SYMBOL_ACLASS_INDEX (sym) = (is_block
22795 ? dwarf2_loclist_block_index
22796 : dwarf2_loclist_index);
22797 SYMBOL_LOCATION_BATON (sym) = baton;
22798 }
22799 else
22800 {
22801 struct dwarf2_locexpr_baton *baton;
22802
22803 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
22804 baton->per_cu = cu->per_cu;
22805 gdb_assert (baton->per_cu);
22806
22807 if (attr->form_is_block ())
22808 {
22809 /* Note that we're just copying the block's data pointer
22810 here, not the actual data. We're still pointing into the
22811 info_buffer for SYM's objfile; right now we never release
22812 that buffer, but when we do clean up properly this may
22813 need to change. */
22814 baton->size = DW_BLOCK (attr)->size;
22815 baton->data = DW_BLOCK (attr)->data;
22816 }
22817 else
22818 {
22819 dwarf2_invalid_attrib_class_complaint ("location description",
22820 sym->natural_name ());
22821 baton->size = 0;
22822 }
22823
22824 SYMBOL_ACLASS_INDEX (sym) = (is_block
22825 ? dwarf2_locexpr_block_index
22826 : dwarf2_locexpr_index);
22827 SYMBOL_LOCATION_BATON (sym) = baton;
22828 }
22829 }
22830
22831 /* See read.h. */
22832
22833 struct objfile *
22834 dwarf2_per_cu_data::objfile () const
22835 {
22836 struct objfile *objfile = dwarf2_per_objfile->objfile;
22837
22838 /* Return the master objfile, so that we can report and look up the
22839 correct file containing this variable. */
22840 if (objfile->separate_debug_objfile_backlink)
22841 objfile = objfile->separate_debug_objfile_backlink;
22842
22843 return objfile;
22844 }
22845
22846 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22847 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22848 CU_HEADERP first. */
22849
22850 static const struct comp_unit_head *
22851 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22852 const struct dwarf2_per_cu_data *per_cu)
22853 {
22854 const gdb_byte *info_ptr;
22855
22856 if (per_cu->cu)
22857 return &per_cu->cu->header;
22858
22859 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
22860
22861 memset (cu_headerp, 0, sizeof (*cu_headerp));
22862 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22863 rcuh_kind::COMPILE);
22864
22865 return cu_headerp;
22866 }
22867
22868 /* See read.h. */
22869
22870 int
22871 dwarf2_per_cu_data::addr_size () const
22872 {
22873 struct comp_unit_head cu_header_local;
22874 const struct comp_unit_head *cu_headerp;
22875
22876 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22877
22878 return cu_headerp->addr_size;
22879 }
22880
22881 /* See read.h. */
22882
22883 int
22884 dwarf2_per_cu_data::offset_size () const
22885 {
22886 struct comp_unit_head cu_header_local;
22887 const struct comp_unit_head *cu_headerp;
22888
22889 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22890
22891 return cu_headerp->offset_size;
22892 }
22893
22894 /* See read.h. */
22895
22896 int
22897 dwarf2_per_cu_data::ref_addr_size () const
22898 {
22899 struct comp_unit_head cu_header_local;
22900 const struct comp_unit_head *cu_headerp;
22901
22902 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22903
22904 if (cu_headerp->version == 2)
22905 return cu_headerp->addr_size;
22906 else
22907 return cu_headerp->offset_size;
22908 }
22909
22910 /* See read.h. */
22911
22912 CORE_ADDR
22913 dwarf2_per_cu_data::text_offset () const
22914 {
22915 struct objfile *objfile = dwarf2_per_objfile->objfile;
22916
22917 return objfile->text_section_offset ();
22918 }
22919
22920 /* See read.h. */
22921
22922 struct type *
22923 dwarf2_per_cu_data::addr_type () const
22924 {
22925 struct objfile *objfile = dwarf2_per_objfile->objfile;
22926 struct type *void_type = objfile_type (objfile)->builtin_void;
22927 struct type *addr_type = lookup_pointer_type (void_type);
22928 int addr_size = this->addr_size ();
22929
22930 if (TYPE_LENGTH (addr_type) == addr_size)
22931 return addr_type;
22932
22933 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
22934 return addr_type;
22935 }
22936
22937 /* A helper function for dwarf2_find_containing_comp_unit that returns
22938 the index of the result, and that searches a vector. It will
22939 return a result even if the offset in question does not actually
22940 occur in any CU. This is separate so that it can be unit
22941 tested. */
22942
22943 static int
22944 dwarf2_find_containing_comp_unit
22945 (sect_offset sect_off,
22946 unsigned int offset_in_dwz,
22947 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
22948 {
22949 int low, high;
22950
22951 low = 0;
22952 high = all_comp_units.size () - 1;
22953 while (high > low)
22954 {
22955 struct dwarf2_per_cu_data *mid_cu;
22956 int mid = low + (high - low) / 2;
22957
22958 mid_cu = all_comp_units[mid];
22959 if (mid_cu->is_dwz > offset_in_dwz
22960 || (mid_cu->is_dwz == offset_in_dwz
22961 && mid_cu->sect_off + mid_cu->length > sect_off))
22962 high = mid;
22963 else
22964 low = mid + 1;
22965 }
22966 gdb_assert (low == high);
22967 return low;
22968 }
22969
22970 /* Locate the .debug_info compilation unit from CU's objfile which contains
22971 the DIE at OFFSET. Raises an error on failure. */
22972
22973 static struct dwarf2_per_cu_data *
22974 dwarf2_find_containing_comp_unit (sect_offset sect_off,
22975 unsigned int offset_in_dwz,
22976 struct dwarf2_per_objfile *dwarf2_per_objfile)
22977 {
22978 int low
22979 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22980 dwarf2_per_objfile->all_comp_units);
22981 struct dwarf2_per_cu_data *this_cu
22982 = dwarf2_per_objfile->all_comp_units[low];
22983
22984 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
22985 {
22986 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
22987 error (_("Dwarf Error: could not find partial DIE containing "
22988 "offset %s [in module %s]"),
22989 sect_offset_str (sect_off),
22990 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
22991
22992 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22993 <= sect_off);
22994 return dwarf2_per_objfile->all_comp_units[low-1];
22995 }
22996 else
22997 {
22998 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
22999 && sect_off >= this_cu->sect_off + this_cu->length)
23000 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23001 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23002 return this_cu;
23003 }
23004 }
23005
23006 #if GDB_SELF_TEST
23007
23008 namespace selftests {
23009 namespace find_containing_comp_unit {
23010
23011 static void
23012 run_test ()
23013 {
23014 struct dwarf2_per_cu_data one {};
23015 struct dwarf2_per_cu_data two {};
23016 struct dwarf2_per_cu_data three {};
23017 struct dwarf2_per_cu_data four {};
23018
23019 one.length = 5;
23020 two.sect_off = sect_offset (one.length);
23021 two.length = 7;
23022
23023 three.length = 5;
23024 three.is_dwz = 1;
23025 four.sect_off = sect_offset (three.length);
23026 four.length = 7;
23027 four.is_dwz = 1;
23028
23029 std::vector<dwarf2_per_cu_data *> units;
23030 units.push_back (&one);
23031 units.push_back (&two);
23032 units.push_back (&three);
23033 units.push_back (&four);
23034
23035 int result;
23036
23037 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23038 SELF_CHECK (units[result] == &one);
23039 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23040 SELF_CHECK (units[result] == &one);
23041 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23042 SELF_CHECK (units[result] == &two);
23043
23044 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23045 SELF_CHECK (units[result] == &three);
23046 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23047 SELF_CHECK (units[result] == &three);
23048 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23049 SELF_CHECK (units[result] == &four);
23050 }
23051
23052 }
23053 }
23054
23055 #endif /* GDB_SELF_TEST */
23056
23057 /* Initialize dwarf2_cu CU, owned by PER_CU. */
23058
23059 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
23060 : per_cu (per_cu_),
23061 mark (false),
23062 has_loclist (false),
23063 checked_producer (false),
23064 producer_is_gxx_lt_4_6 (false),
23065 producer_is_gcc_lt_4_3 (false),
23066 producer_is_icc (false),
23067 producer_is_icc_lt_14 (false),
23068 producer_is_codewarrior (false),
23069 processing_has_namespace_info (false)
23070 {
23071 per_cu->cu = this;
23072 }
23073
23074 /* Destroy a dwarf2_cu. */
23075
23076 dwarf2_cu::~dwarf2_cu ()
23077 {
23078 per_cu->cu = NULL;
23079 }
23080
23081 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23082
23083 static void
23084 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23085 enum language pretend_language)
23086 {
23087 struct attribute *attr;
23088
23089 /* Set the language we're debugging. */
23090 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23091 if (attr != nullptr)
23092 set_cu_language (DW_UNSND (attr), cu);
23093 else
23094 {
23095 cu->language = pretend_language;
23096 cu->language_defn = language_def (cu->language);
23097 }
23098
23099 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23100 }
23101
23102 /* Increase the age counter on each cached compilation unit, and free
23103 any that are too old. */
23104
23105 static void
23106 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23107 {
23108 struct dwarf2_per_cu_data *per_cu, **last_chain;
23109
23110 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23111 per_cu = dwarf2_per_objfile->read_in_chain;
23112 while (per_cu != NULL)
23113 {
23114 per_cu->cu->last_used ++;
23115 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23116 dwarf2_mark (per_cu->cu);
23117 per_cu = per_cu->cu->read_in_chain;
23118 }
23119
23120 per_cu = dwarf2_per_objfile->read_in_chain;
23121 last_chain = &dwarf2_per_objfile->read_in_chain;
23122 while (per_cu != NULL)
23123 {
23124 struct dwarf2_per_cu_data *next_cu;
23125
23126 next_cu = per_cu->cu->read_in_chain;
23127
23128 if (!per_cu->cu->mark)
23129 {
23130 delete per_cu->cu;
23131 *last_chain = next_cu;
23132 }
23133 else
23134 last_chain = &per_cu->cu->read_in_chain;
23135
23136 per_cu = next_cu;
23137 }
23138 }
23139
23140 /* Remove a single compilation unit from the cache. */
23141
23142 static void
23143 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23144 {
23145 struct dwarf2_per_cu_data *per_cu, **last_chain;
23146 struct dwarf2_per_objfile *dwarf2_per_objfile
23147 = target_per_cu->dwarf2_per_objfile;
23148
23149 per_cu = dwarf2_per_objfile->read_in_chain;
23150 last_chain = &dwarf2_per_objfile->read_in_chain;
23151 while (per_cu != NULL)
23152 {
23153 struct dwarf2_per_cu_data *next_cu;
23154
23155 next_cu = per_cu->cu->read_in_chain;
23156
23157 if (per_cu == target_per_cu)
23158 {
23159 delete per_cu->cu;
23160 per_cu->cu = NULL;
23161 *last_chain = next_cu;
23162 break;
23163 }
23164 else
23165 last_chain = &per_cu->cu->read_in_chain;
23166
23167 per_cu = next_cu;
23168 }
23169 }
23170
23171 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23172 We store these in a hash table separate from the DIEs, and preserve them
23173 when the DIEs are flushed out of cache.
23174
23175 The CU "per_cu" pointer is needed because offset alone is not enough to
23176 uniquely identify the type. A file may have multiple .debug_types sections,
23177 or the type may come from a DWO file. Furthermore, while it's more logical
23178 to use per_cu->section+offset, with Fission the section with the data is in
23179 the DWO file but we don't know that section at the point we need it.
23180 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23181 because we can enter the lookup routine, get_die_type_at_offset, from
23182 outside this file, and thus won't necessarily have PER_CU->cu.
23183 Fortunately, PER_CU is stable for the life of the objfile. */
23184
23185 struct dwarf2_per_cu_offset_and_type
23186 {
23187 const struct dwarf2_per_cu_data *per_cu;
23188 sect_offset sect_off;
23189 struct type *type;
23190 };
23191
23192 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23193
23194 static hashval_t
23195 per_cu_offset_and_type_hash (const void *item)
23196 {
23197 const struct dwarf2_per_cu_offset_and_type *ofs
23198 = (const struct dwarf2_per_cu_offset_and_type *) item;
23199
23200 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23201 }
23202
23203 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23204
23205 static int
23206 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23207 {
23208 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23209 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23210 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23211 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23212
23213 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23214 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23215 }
23216
23217 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23218 table if necessary. For convenience, return TYPE.
23219
23220 The DIEs reading must have careful ordering to:
23221 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23222 reading current DIE.
23223 * Not trying to dereference contents of still incompletely read in types
23224 while reading in other DIEs.
23225 * Enable referencing still incompletely read in types just by a pointer to
23226 the type without accessing its fields.
23227
23228 Therefore caller should follow these rules:
23229 * Try to fetch any prerequisite types we may need to build this DIE type
23230 before building the type and calling set_die_type.
23231 * After building type call set_die_type for current DIE as soon as
23232 possible before fetching more types to complete the current type.
23233 * Make the type as complete as possible before fetching more types. */
23234
23235 static struct type *
23236 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23237 {
23238 struct dwarf2_per_objfile *dwarf2_per_objfile
23239 = cu->per_cu->dwarf2_per_objfile;
23240 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23241 struct objfile *objfile = dwarf2_per_objfile->objfile;
23242 struct attribute *attr;
23243 struct dynamic_prop prop;
23244
23245 /* For Ada types, make sure that the gnat-specific data is always
23246 initialized (if not already set). There are a few types where
23247 we should not be doing so, because the type-specific area is
23248 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23249 where the type-specific area is used to store the floatformat).
23250 But this is not a problem, because the gnat-specific information
23251 is actually not needed for these types. */
23252 if (need_gnat_info (cu)
23253 && TYPE_CODE (type) != TYPE_CODE_FUNC
23254 && TYPE_CODE (type) != TYPE_CODE_FLT
23255 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23256 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23257 && TYPE_CODE (type) != TYPE_CODE_METHOD
23258 && !HAVE_GNAT_AUX_INFO (type))
23259 INIT_GNAT_SPECIFIC (type);
23260
23261 /* Read DW_AT_allocated and set in type. */
23262 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23263 if (attr != NULL && attr->form_is_block ())
23264 {
23265 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23266 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23267 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
23268 }
23269 else if (attr != NULL)
23270 {
23271 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23272 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23273 sect_offset_str (die->sect_off));
23274 }
23275
23276 /* Read DW_AT_associated and set in type. */
23277 attr = dwarf2_attr (die, DW_AT_associated, cu);
23278 if (attr != NULL && attr->form_is_block ())
23279 {
23280 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23281 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23282 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
23283 }
23284 else if (attr != NULL)
23285 {
23286 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23287 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23288 sect_offset_str (die->sect_off));
23289 }
23290
23291 /* Read DW_AT_data_location and set in type. */
23292 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23293 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23294 cu->per_cu->addr_type ()))
23295 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
23296
23297 if (dwarf2_per_objfile->die_type_hash == NULL)
23298 dwarf2_per_objfile->die_type_hash
23299 = htab_up (htab_create_alloc (127,
23300 per_cu_offset_and_type_hash,
23301 per_cu_offset_and_type_eq,
23302 NULL, xcalloc, xfree));
23303
23304 ofs.per_cu = cu->per_cu;
23305 ofs.sect_off = die->sect_off;
23306 ofs.type = type;
23307 slot = (struct dwarf2_per_cu_offset_and_type **)
23308 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23309 if (*slot)
23310 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23311 sect_offset_str (die->sect_off));
23312 *slot = XOBNEW (&objfile->objfile_obstack,
23313 struct dwarf2_per_cu_offset_and_type);
23314 **slot = ofs;
23315 return type;
23316 }
23317
23318 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23319 or return NULL if the die does not have a saved type. */
23320
23321 static struct type *
23322 get_die_type_at_offset (sect_offset sect_off,
23323 struct dwarf2_per_cu_data *per_cu)
23324 {
23325 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23326 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23327
23328 if (dwarf2_per_objfile->die_type_hash == NULL)
23329 return NULL;
23330
23331 ofs.per_cu = per_cu;
23332 ofs.sect_off = sect_off;
23333 slot = ((struct dwarf2_per_cu_offset_and_type *)
23334 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23335 if (slot)
23336 return slot->type;
23337 else
23338 return NULL;
23339 }
23340
23341 /* Look up the type for DIE in CU in die_type_hash,
23342 or return NULL if DIE does not have a saved type. */
23343
23344 static struct type *
23345 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23346 {
23347 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23348 }
23349
23350 /* Add a dependence relationship from CU to REF_PER_CU. */
23351
23352 static void
23353 dwarf2_add_dependence (struct dwarf2_cu *cu,
23354 struct dwarf2_per_cu_data *ref_per_cu)
23355 {
23356 void **slot;
23357
23358 if (cu->dependencies == NULL)
23359 cu->dependencies
23360 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23361 NULL, &cu->comp_unit_obstack,
23362 hashtab_obstack_allocate,
23363 dummy_obstack_deallocate);
23364
23365 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23366 if (*slot == NULL)
23367 *slot = ref_per_cu;
23368 }
23369
23370 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23371 Set the mark field in every compilation unit in the
23372 cache that we must keep because we are keeping CU. */
23373
23374 static int
23375 dwarf2_mark_helper (void **slot, void *data)
23376 {
23377 struct dwarf2_per_cu_data *per_cu;
23378
23379 per_cu = (struct dwarf2_per_cu_data *) *slot;
23380
23381 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23382 reading of the chain. As such dependencies remain valid it is not much
23383 useful to track and undo them during QUIT cleanups. */
23384 if (per_cu->cu == NULL)
23385 return 1;
23386
23387 if (per_cu->cu->mark)
23388 return 1;
23389 per_cu->cu->mark = true;
23390
23391 if (per_cu->cu->dependencies != NULL)
23392 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23393
23394 return 1;
23395 }
23396
23397 /* Set the mark field in CU and in every other compilation unit in the
23398 cache that we must keep because we are keeping CU. */
23399
23400 static void
23401 dwarf2_mark (struct dwarf2_cu *cu)
23402 {
23403 if (cu->mark)
23404 return;
23405 cu->mark = true;
23406 if (cu->dependencies != NULL)
23407 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23408 }
23409
23410 static void
23411 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23412 {
23413 while (per_cu)
23414 {
23415 per_cu->cu->mark = false;
23416 per_cu = per_cu->cu->read_in_chain;
23417 }
23418 }
23419
23420 /* Trivial hash function for partial_die_info: the hash value of a DIE
23421 is its offset in .debug_info for this objfile. */
23422
23423 static hashval_t
23424 partial_die_hash (const void *item)
23425 {
23426 const struct partial_die_info *part_die
23427 = (const struct partial_die_info *) item;
23428
23429 return to_underlying (part_die->sect_off);
23430 }
23431
23432 /* Trivial comparison function for partial_die_info structures: two DIEs
23433 are equal if they have the same offset. */
23434
23435 static int
23436 partial_die_eq (const void *item_lhs, const void *item_rhs)
23437 {
23438 const struct partial_die_info *part_die_lhs
23439 = (const struct partial_die_info *) item_lhs;
23440 const struct partial_die_info *part_die_rhs
23441 = (const struct partial_die_info *) item_rhs;
23442
23443 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23444 }
23445
23446 struct cmd_list_element *set_dwarf_cmdlist;
23447 struct cmd_list_element *show_dwarf_cmdlist;
23448
23449 static void
23450 set_dwarf_cmd (const char *args, int from_tty)
23451 {
23452 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23453 gdb_stdout);
23454 }
23455
23456 static void
23457 show_dwarf_cmd (const char *args, int from_tty)
23458 {
23459 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23460 }
23461
23462 static void
23463 show_check_physname (struct ui_file *file, int from_tty,
23464 struct cmd_list_element *c, const char *value)
23465 {
23466 fprintf_filtered (file,
23467 _("Whether to check \"physname\" is %s.\n"),
23468 value);
23469 }
23470
23471 void _initialize_dwarf2_read ();
23472 void
23473 _initialize_dwarf2_read ()
23474 {
23475 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23476 Set DWARF specific variables.\n\
23477 Configure DWARF variables such as the cache size."),
23478 &set_dwarf_cmdlist, "maintenance set dwarf ",
23479 0/*allow-unknown*/, &maintenance_set_cmdlist);
23480
23481 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
23482 Show DWARF specific variables.\n\
23483 Show DWARF variables such as the cache size."),
23484 &show_dwarf_cmdlist, "maintenance show dwarf ",
23485 0/*allow-unknown*/, &maintenance_show_cmdlist);
23486
23487 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23488 &dwarf_max_cache_age, _("\
23489 Set the upper bound on the age of cached DWARF compilation units."), _("\
23490 Show the upper bound on the age of cached DWARF compilation units."), _("\
23491 A higher limit means that cached compilation units will be stored\n\
23492 in memory longer, and more total memory will be used. Zero disables\n\
23493 caching, which can slow down startup."),
23494 NULL,
23495 show_dwarf_max_cache_age,
23496 &set_dwarf_cmdlist,
23497 &show_dwarf_cmdlist);
23498
23499 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23500 Set debugging of the DWARF reader."), _("\
23501 Show debugging of the DWARF reader."), _("\
23502 When enabled (non-zero), debugging messages are printed during DWARF\n\
23503 reading and symtab expansion. A value of 1 (one) provides basic\n\
23504 information. A value greater than 1 provides more verbose information."),
23505 NULL,
23506 NULL,
23507 &setdebuglist, &showdebuglist);
23508
23509 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23510 Set debugging of the DWARF DIE reader."), _("\
23511 Show debugging of the DWARF DIE reader."), _("\
23512 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23513 The value is the maximum depth to print."),
23514 NULL,
23515 NULL,
23516 &setdebuglist, &showdebuglist);
23517
23518 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23519 Set debugging of the dwarf line reader."), _("\
23520 Show debugging of the dwarf line reader."), _("\
23521 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23522 A value of 1 (one) provides basic information.\n\
23523 A value greater than 1 provides more verbose information."),
23524 NULL,
23525 NULL,
23526 &setdebuglist, &showdebuglist);
23527
23528 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23529 Set cross-checking of \"physname\" code against demangler."), _("\
23530 Show cross-checking of \"physname\" code against demangler."), _("\
23531 When enabled, GDB's internal \"physname\" code is checked against\n\
23532 the demangler."),
23533 NULL, show_check_physname,
23534 &setdebuglist, &showdebuglist);
23535
23536 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23537 no_class, &use_deprecated_index_sections, _("\
23538 Set whether to use deprecated gdb_index sections."), _("\
23539 Show whether to use deprecated gdb_index sections."), _("\
23540 When enabled, deprecated .gdb_index sections are used anyway.\n\
23541 Normally they are ignored either because of a missing feature or\n\
23542 performance issue.\n\
23543 Warning: This option must be enabled before gdb reads the file."),
23544 NULL,
23545 NULL,
23546 &setlist, &showlist);
23547
23548 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23549 &dwarf2_locexpr_funcs);
23550 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23551 &dwarf2_loclist_funcs);
23552
23553 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23554 &dwarf2_block_frame_base_locexpr_funcs);
23555 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23556 &dwarf2_block_frame_base_loclist_funcs);
23557
23558 #if GDB_SELF_TEST
23559 selftests::register_test ("dw2_expand_symtabs_matching",
23560 selftests::dw2_expand_symtabs_matching::run_test);
23561 selftests::register_test ("dwarf2_find_containing_comp_unit",
23562 selftests::find_containing_comp_unit::run_test);
23563 #endif
23564 }
This page took 0.481182 seconds and 5 git commands to generate.