]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
Replace most calls to help_list and cmd_show_list
[thirdparty/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "dwarf2/stringify.h"
44 #include "bfd.h"
45 #include "elf-bfd.h"
46 #include "symtab.h"
47 #include "gdbtypes.h"
48 #include "objfiles.h"
49 #include "dwarf2.h"
50 #include "buildsym.h"
51 #include "demangle.h"
52 #include "gdb-demangle.h"
53 #include "filenames.h" /* for DOSish file names */
54 #include "language.h"
55 #include "complaints.h"
56 #include "dwarf2/expr.h"
57 #include "dwarf2/loc.h"
58 #include "cp-support.h"
59 #include "hashtab.h"
60 #include "command.h"
61 #include "gdbcmd.h"
62 #include "block.h"
63 #include "addrmap.h"
64 #include "typeprint.h"
65 #include "psympriv.h"
66 #include "c-lang.h"
67 #include "go-lang.h"
68 #include "valprint.h"
69 #include "gdbcore.h" /* for gnutarget */
70 #include "gdb/gdb-index.h"
71 #include "gdb_bfd.h"
72 #include "f-lang.h"
73 #include "source.h"
74 #include "build-id.h"
75 #include "namespace.h"
76 #include "gdbsupport/function-view.h"
77 #include "gdbsupport/gdb_optional.h"
78 #include "gdbsupport/underlying.h"
79 #include "gdbsupport/hash_enum.h"
80 #include "filename-seen-cache.h"
81 #include "producer.h"
82 #include <fcntl.h>
83 #include <algorithm>
84 #include <unordered_map>
85 #include "gdbsupport/selftest.h"
86 #include "rust-lang.h"
87 #include "gdbsupport/pathstuff.h"
88 #include "count-one-bits.h"
89 #include "debuginfod-support.h"
90
91 /* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94 static unsigned int dwarf_read_debug = 0;
95
96 /* When non-zero, dump DIEs after they are read in. */
97 static unsigned int dwarf_die_debug = 0;
98
99 /* When non-zero, dump line number entries as they are read in. */
100 unsigned int dwarf_line_debug = 0;
101
102 /* When true, cross-check physname against demangler. */
103 static bool check_physname = false;
104
105 /* When true, do not reject deprecated .gdb_index sections. */
106 static bool use_deprecated_index_sections = false;
107
108 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
109
110 /* The "aclass" indices for various kinds of computed DWARF symbols. */
111
112 static int dwarf2_locexpr_index;
113 static int dwarf2_loclist_index;
114 static int dwarf2_locexpr_block_index;
115 static int dwarf2_loclist_block_index;
116
117 /* Size of .debug_loclists section header for 32-bit DWARF format. */
118 #define LOCLIST_HEADER_SIZE32 12
119
120 /* Size of .debug_loclists section header for 64-bit DWARF format. */
121 #define LOCLIST_HEADER_SIZE64 20
122
123 /* An index into a (C++) symbol name component in a symbol name as
124 recorded in the mapped_index's symbol table. For each C++ symbol
125 in the symbol table, we record one entry for the start of each
126 component in the symbol in a table of name components, and then
127 sort the table, in order to be able to binary search symbol names,
128 ignoring leading namespaces, both completion and regular look up.
129 For example, for symbol "A::B::C", we'll have an entry that points
130 to "A::B::C", another that points to "B::C", and another for "C".
131 Note that function symbols in GDB index have no parameter
132 information, just the function/method names. You can convert a
133 name_component to a "const char *" using the
134 'mapped_index::symbol_name_at(offset_type)' method. */
135
136 struct name_component
137 {
138 /* Offset in the symbol name where the component starts. Stored as
139 a (32-bit) offset instead of a pointer to save memory and improve
140 locality on 64-bit architectures. */
141 offset_type name_offset;
142
143 /* The symbol's index in the symbol and constant pool tables of a
144 mapped_index. */
145 offset_type idx;
146 };
147
148 /* Base class containing bits shared by both .gdb_index and
149 .debug_name indexes. */
150
151 struct mapped_index_base
152 {
153 mapped_index_base () = default;
154 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
155
156 /* The name_component table (a sorted vector). See name_component's
157 description above. */
158 std::vector<name_component> name_components;
159
160 /* How NAME_COMPONENTS is sorted. */
161 enum case_sensitivity name_components_casing;
162
163 /* Return the number of names in the symbol table. */
164 virtual size_t symbol_name_count () const = 0;
165
166 /* Get the name of the symbol at IDX in the symbol table. */
167 virtual const char *symbol_name_at (offset_type idx) const = 0;
168
169 /* Return whether the name at IDX in the symbol table should be
170 ignored. */
171 virtual bool symbol_name_slot_invalid (offset_type idx) const
172 {
173 return false;
174 }
175
176 /* Build the symbol name component sorted vector, if we haven't
177 yet. */
178 void build_name_components ();
179
180 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
181 possible matches for LN_NO_PARAMS in the name component
182 vector. */
183 std::pair<std::vector<name_component>::const_iterator,
184 std::vector<name_component>::const_iterator>
185 find_name_components_bounds (const lookup_name_info &ln_no_params,
186 enum language lang) const;
187
188 /* Prevent deleting/destroying via a base class pointer. */
189 protected:
190 ~mapped_index_base() = default;
191 };
192
193 /* A description of the mapped index. The file format is described in
194 a comment by the code that writes the index. */
195 struct mapped_index final : public mapped_index_base
196 {
197 /* A slot/bucket in the symbol table hash. */
198 struct symbol_table_slot
199 {
200 const offset_type name;
201 const offset_type vec;
202 };
203
204 /* Index data format version. */
205 int version = 0;
206
207 /* The address table data. */
208 gdb::array_view<const gdb_byte> address_table;
209
210 /* The symbol table, implemented as a hash table. */
211 gdb::array_view<symbol_table_slot> symbol_table;
212
213 /* A pointer to the constant pool. */
214 const char *constant_pool = nullptr;
215
216 bool symbol_name_slot_invalid (offset_type idx) const override
217 {
218 const auto &bucket = this->symbol_table[idx];
219 return bucket.name == 0 && bucket.vec == 0;
220 }
221
222 /* Convenience method to get at the name of the symbol at IDX in the
223 symbol table. */
224 const char *symbol_name_at (offset_type idx) const override
225 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
226
227 size_t symbol_name_count () const override
228 { return this->symbol_table.size (); }
229 };
230
231 /* A description of the mapped .debug_names.
232 Uninitialized map has CU_COUNT 0. */
233 struct mapped_debug_names final : public mapped_index_base
234 {
235 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
236 : dwarf2_per_objfile (dwarf2_per_objfile_)
237 {}
238
239 struct dwarf2_per_objfile *dwarf2_per_objfile;
240 bfd_endian dwarf5_byte_order;
241 bool dwarf5_is_dwarf64;
242 bool augmentation_is_gdb;
243 uint8_t offset_size;
244 uint32_t cu_count = 0;
245 uint32_t tu_count, bucket_count, name_count;
246 const gdb_byte *cu_table_reordered, *tu_table_reordered;
247 const uint32_t *bucket_table_reordered, *hash_table_reordered;
248 const gdb_byte *name_table_string_offs_reordered;
249 const gdb_byte *name_table_entry_offs_reordered;
250 const gdb_byte *entry_pool;
251
252 struct index_val
253 {
254 ULONGEST dwarf_tag;
255 struct attr
256 {
257 /* Attribute name DW_IDX_*. */
258 ULONGEST dw_idx;
259
260 /* Attribute form DW_FORM_*. */
261 ULONGEST form;
262
263 /* Value if FORM is DW_FORM_implicit_const. */
264 LONGEST implicit_const;
265 };
266 std::vector<attr> attr_vec;
267 };
268
269 std::unordered_map<ULONGEST, index_val> abbrev_map;
270
271 const char *namei_to_name (uint32_t namei) const;
272
273 /* Implementation of the mapped_index_base virtual interface, for
274 the name_components cache. */
275
276 const char *symbol_name_at (offset_type idx) const override
277 { return namei_to_name (idx); }
278
279 size_t symbol_name_count () const override
280 { return this->name_count; }
281 };
282
283 /* See dwarf2read.h. */
284
285 dwarf2_per_objfile *
286 get_dwarf2_per_objfile (struct objfile *objfile)
287 {
288 return dwarf2_objfile_data_key.get (objfile);
289 }
290
291 /* Default names of the debugging sections. */
292
293 /* Note that if the debugging section has been compressed, it might
294 have a name like .zdebug_info. */
295
296 static const struct dwarf2_debug_sections dwarf2_elf_names =
297 {
298 { ".debug_info", ".zdebug_info" },
299 { ".debug_abbrev", ".zdebug_abbrev" },
300 { ".debug_line", ".zdebug_line" },
301 { ".debug_loc", ".zdebug_loc" },
302 { ".debug_loclists", ".zdebug_loclists" },
303 { ".debug_macinfo", ".zdebug_macinfo" },
304 { ".debug_macro", ".zdebug_macro" },
305 { ".debug_str", ".zdebug_str" },
306 { ".debug_str_offsets", ".zdebug_str_offsets" },
307 { ".debug_line_str", ".zdebug_line_str" },
308 { ".debug_ranges", ".zdebug_ranges" },
309 { ".debug_rnglists", ".zdebug_rnglists" },
310 { ".debug_types", ".zdebug_types" },
311 { ".debug_addr", ".zdebug_addr" },
312 { ".debug_frame", ".zdebug_frame" },
313 { ".eh_frame", NULL },
314 { ".gdb_index", ".zgdb_index" },
315 { ".debug_names", ".zdebug_names" },
316 { ".debug_aranges", ".zdebug_aranges" },
317 23
318 };
319
320 /* List of DWO/DWP sections. */
321
322 static const struct dwop_section_names
323 {
324 struct dwarf2_section_names abbrev_dwo;
325 struct dwarf2_section_names info_dwo;
326 struct dwarf2_section_names line_dwo;
327 struct dwarf2_section_names loc_dwo;
328 struct dwarf2_section_names loclists_dwo;
329 struct dwarf2_section_names macinfo_dwo;
330 struct dwarf2_section_names macro_dwo;
331 struct dwarf2_section_names str_dwo;
332 struct dwarf2_section_names str_offsets_dwo;
333 struct dwarf2_section_names types_dwo;
334 struct dwarf2_section_names cu_index;
335 struct dwarf2_section_names tu_index;
336 }
337 dwop_section_names =
338 {
339 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
340 { ".debug_info.dwo", ".zdebug_info.dwo" },
341 { ".debug_line.dwo", ".zdebug_line.dwo" },
342 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
343 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
344 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
345 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
346 { ".debug_str.dwo", ".zdebug_str.dwo" },
347 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
348 { ".debug_types.dwo", ".zdebug_types.dwo" },
349 { ".debug_cu_index", ".zdebug_cu_index" },
350 { ".debug_tu_index", ".zdebug_tu_index" },
351 };
352
353 /* local data types */
354
355 /* The location list section (.debug_loclists) begins with a header,
356 which contains the following information. */
357 struct loclist_header
358 {
359 /* A 4-byte or 12-byte length containing the length of the
360 set of entries for this compilation unit, not including the
361 length field itself. */
362 unsigned int length;
363
364 /* A 2-byte version identifier. */
365 short version;
366
367 /* A 1-byte unsigned integer containing the size in bytes of an address on
368 the target system. */
369 unsigned char addr_size;
370
371 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
372 on the target system. */
373 unsigned char segment_collector_size;
374
375 /* A 4-byte count of the number of offsets that follow the header. */
376 unsigned int offset_entry_count;
377 };
378
379 /* Type used for delaying computation of method physnames.
380 See comments for compute_delayed_physnames. */
381 struct delayed_method_info
382 {
383 /* The type to which the method is attached, i.e., its parent class. */
384 struct type *type;
385
386 /* The index of the method in the type's function fieldlists. */
387 int fnfield_index;
388
389 /* The index of the method in the fieldlist. */
390 int index;
391
392 /* The name of the DIE. */
393 const char *name;
394
395 /* The DIE associated with this method. */
396 struct die_info *die;
397 };
398
399 /* Internal state when decoding a particular compilation unit. */
400 struct dwarf2_cu
401 {
402 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
403 ~dwarf2_cu ();
404
405 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
406
407 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
408 Create the set of symtabs used by this TU, or if this TU is sharing
409 symtabs with another TU and the symtabs have already been created
410 then restore those symtabs in the line header.
411 We don't need the pc/line-number mapping for type units. */
412 void setup_type_unit_groups (struct die_info *die);
413
414 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
415 buildsym_compunit constructor. */
416 struct compunit_symtab *start_symtab (const char *name,
417 const char *comp_dir,
418 CORE_ADDR low_pc);
419
420 /* Reset the builder. */
421 void reset_builder () { m_builder.reset (); }
422
423 /* The header of the compilation unit. */
424 struct comp_unit_head header {};
425
426 /* Base address of this compilation unit. */
427 gdb::optional<CORE_ADDR> base_address;
428
429 /* The language we are debugging. */
430 enum language language = language_unknown;
431 const struct language_defn *language_defn = nullptr;
432
433 const char *producer = nullptr;
434
435 private:
436 /* The symtab builder for this CU. This is only non-NULL when full
437 symbols are being read. */
438 std::unique_ptr<buildsym_compunit> m_builder;
439
440 public:
441 /* The generic symbol table building routines have separate lists for
442 file scope symbols and all all other scopes (local scopes). So
443 we need to select the right one to pass to add_symbol_to_list().
444 We do it by keeping a pointer to the correct list in list_in_scope.
445
446 FIXME: The original dwarf code just treated the file scope as the
447 first local scope, and all other local scopes as nested local
448 scopes, and worked fine. Check to see if we really need to
449 distinguish these in buildsym.c. */
450 struct pending **list_in_scope = nullptr;
451
452 /* Hash table holding all the loaded partial DIEs
453 with partial_die->offset.SECT_OFF as hash. */
454 htab_t partial_dies = nullptr;
455
456 /* Storage for things with the same lifetime as this read-in compilation
457 unit, including partial DIEs. */
458 auto_obstack comp_unit_obstack;
459
460 /* When multiple dwarf2_cu structures are living in memory, this field
461 chains them all together, so that they can be released efficiently.
462 We will probably also want a generation counter so that most-recently-used
463 compilation units are cached... */
464 struct dwarf2_per_cu_data *read_in_chain = nullptr;
465
466 /* Backlink to our per_cu entry. */
467 struct dwarf2_per_cu_data *per_cu;
468
469 /* How many compilation units ago was this CU last referenced? */
470 int last_used = 0;
471
472 /* A hash table of DIE cu_offset for following references with
473 die_info->offset.sect_off as hash. */
474 htab_t die_hash = nullptr;
475
476 /* Full DIEs if read in. */
477 struct die_info *dies = nullptr;
478
479 /* A set of pointers to dwarf2_per_cu_data objects for compilation
480 units referenced by this one. Only set during full symbol processing;
481 partial symbol tables do not have dependencies. */
482 htab_t dependencies = nullptr;
483
484 /* Header data from the line table, during full symbol processing. */
485 struct line_header *line_header = nullptr;
486 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
487 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
488 this is the DW_TAG_compile_unit die for this CU. We'll hold on
489 to the line header as long as this DIE is being processed. See
490 process_die_scope. */
491 die_info *line_header_die_owner = nullptr;
492
493 /* A list of methods which need to have physnames computed
494 after all type information has been read. */
495 std::vector<delayed_method_info> method_list;
496
497 /* To be copied to symtab->call_site_htab. */
498 htab_t call_site_htab = nullptr;
499
500 /* Non-NULL if this CU came from a DWO file.
501 There is an invariant here that is important to remember:
502 Except for attributes copied from the top level DIE in the "main"
503 (or "stub") file in preparation for reading the DWO file
504 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
505 Either there isn't a DWO file (in which case this is NULL and the point
506 is moot), or there is and either we're not going to read it (in which
507 case this is NULL) or there is and we are reading it (in which case this
508 is non-NULL). */
509 struct dwo_unit *dwo_unit = nullptr;
510
511 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
512 Note this value comes from the Fission stub CU/TU's DIE. */
513 gdb::optional<ULONGEST> addr_base;
514
515 /* The DW_AT_rnglists_base attribute if present.
516 Note this value comes from the Fission stub CU/TU's DIE.
517 Also note that the value is zero in the non-DWO case so this value can
518 be used without needing to know whether DWO files are in use or not.
519 N.B. This does not apply to DW_AT_ranges appearing in
520 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
521 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
522 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
523 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
524 ULONGEST ranges_base = 0;
525
526 /* The DW_AT_loclists_base attribute if present. */
527 ULONGEST loclist_base = 0;
528
529 /* When reading debug info generated by older versions of rustc, we
530 have to rewrite some union types to be struct types with a
531 variant part. This rewriting must be done after the CU is fully
532 read in, because otherwise at the point of rewriting some struct
533 type might not have been fully processed. So, we keep a list of
534 all such types here and process them after expansion. */
535 std::vector<struct type *> rust_unions;
536
537 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
538 files, the value is implicitly zero. For DWARF 5 version DWO files, the
539 value is often implicit and is the size of the header of
540 .debug_str_offsets section (8 or 4, depending on the address size). */
541 gdb::optional<ULONGEST> str_offsets_base;
542
543 /* Mark used when releasing cached dies. */
544 bool mark : 1;
545
546 /* This CU references .debug_loc. See the symtab->locations_valid field.
547 This test is imperfect as there may exist optimized debug code not using
548 any location list and still facing inlining issues if handled as
549 unoptimized code. For a future better test see GCC PR other/32998. */
550 bool has_loclist : 1;
551
552 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
553 if all the producer_is_* fields are valid. This information is cached
554 because profiling CU expansion showed excessive time spent in
555 producer_is_gxx_lt_4_6. */
556 bool checked_producer : 1;
557 bool producer_is_gxx_lt_4_6 : 1;
558 bool producer_is_gcc_lt_4_3 : 1;
559 bool producer_is_icc : 1;
560 bool producer_is_icc_lt_14 : 1;
561 bool producer_is_codewarrior : 1;
562
563 /* When true, the file that we're processing is known to have
564 debugging info for C++ namespaces. GCC 3.3.x did not produce
565 this information, but later versions do. */
566
567 bool processing_has_namespace_info : 1;
568
569 struct partial_die_info *find_partial_die (sect_offset sect_off);
570
571 /* If this CU was inherited by another CU (via specification,
572 abstract_origin, etc), this is the ancestor CU. */
573 dwarf2_cu *ancestor;
574
575 /* Get the buildsym_compunit for this CU. */
576 buildsym_compunit *get_builder ()
577 {
578 /* If this CU has a builder associated with it, use that. */
579 if (m_builder != nullptr)
580 return m_builder.get ();
581
582 /* Otherwise, search ancestors for a valid builder. */
583 if (ancestor != nullptr)
584 return ancestor->get_builder ();
585
586 return nullptr;
587 }
588 };
589
590 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
591 This includes type_unit_group and quick_file_names. */
592
593 struct stmt_list_hash
594 {
595 /* The DWO unit this table is from or NULL if there is none. */
596 struct dwo_unit *dwo_unit;
597
598 /* Offset in .debug_line or .debug_line.dwo. */
599 sect_offset line_sect_off;
600 };
601
602 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
603 an object of this type. */
604
605 struct type_unit_group
606 {
607 /* dwarf2read.c's main "handle" on a TU symtab.
608 To simplify things we create an artificial CU that "includes" all the
609 type units using this stmt_list so that the rest of the code still has
610 a "per_cu" handle on the symtab. */
611 struct dwarf2_per_cu_data per_cu;
612
613 /* The TUs that share this DW_AT_stmt_list entry.
614 This is added to while parsing type units to build partial symtabs,
615 and is deleted afterwards and not used again. */
616 std::vector<signatured_type *> *tus;
617
618 /* The compunit symtab.
619 Type units in a group needn't all be defined in the same source file,
620 so we create an essentially anonymous symtab as the compunit symtab. */
621 struct compunit_symtab *compunit_symtab;
622
623 /* The data used to construct the hash key. */
624 struct stmt_list_hash hash;
625
626 /* The symbol tables for this TU (obtained from the files listed in
627 DW_AT_stmt_list).
628 WARNING: The order of entries here must match the order of entries
629 in the line header. After the first TU using this type_unit_group, the
630 line header for the subsequent TUs is recreated from this. This is done
631 because we need to use the same symtabs for each TU using the same
632 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
633 there's no guarantee the line header doesn't have duplicate entries. */
634 struct symtab **symtabs;
635 };
636
637 /* These sections are what may appear in a (real or virtual) DWO file. */
638
639 struct dwo_sections
640 {
641 struct dwarf2_section_info abbrev;
642 struct dwarf2_section_info line;
643 struct dwarf2_section_info loc;
644 struct dwarf2_section_info loclists;
645 struct dwarf2_section_info macinfo;
646 struct dwarf2_section_info macro;
647 struct dwarf2_section_info str;
648 struct dwarf2_section_info str_offsets;
649 /* In the case of a virtual DWO file, these two are unused. */
650 struct dwarf2_section_info info;
651 std::vector<dwarf2_section_info> types;
652 };
653
654 /* CUs/TUs in DWP/DWO files. */
655
656 struct dwo_unit
657 {
658 /* Backlink to the containing struct dwo_file. */
659 struct dwo_file *dwo_file;
660
661 /* The "id" that distinguishes this CU/TU.
662 .debug_info calls this "dwo_id", .debug_types calls this "signature".
663 Since signatures came first, we stick with it for consistency. */
664 ULONGEST signature;
665
666 /* The section this CU/TU lives in, in the DWO file. */
667 struct dwarf2_section_info *section;
668
669 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
670 sect_offset sect_off;
671 unsigned int length;
672
673 /* For types, offset in the type's DIE of the type defined by this TU. */
674 cu_offset type_offset_in_tu;
675 };
676
677 /* include/dwarf2.h defines the DWP section codes.
678 It defines a max value but it doesn't define a min value, which we
679 use for error checking, so provide one. */
680
681 enum dwp_v2_section_ids
682 {
683 DW_SECT_MIN = 1
684 };
685
686 /* Data for one DWO file.
687
688 This includes virtual DWO files (a virtual DWO file is a DWO file as it
689 appears in a DWP file). DWP files don't really have DWO files per se -
690 comdat folding of types "loses" the DWO file they came from, and from
691 a high level view DWP files appear to contain a mass of random types.
692 However, to maintain consistency with the non-DWP case we pretend DWP
693 files contain virtual DWO files, and we assign each TU with one virtual
694 DWO file (generally based on the line and abbrev section offsets -
695 a heuristic that seems to work in practice). */
696
697 struct dwo_file
698 {
699 dwo_file () = default;
700 DISABLE_COPY_AND_ASSIGN (dwo_file);
701
702 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
703 For virtual DWO files the name is constructed from the section offsets
704 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
705 from related CU+TUs. */
706 const char *dwo_name = nullptr;
707
708 /* The DW_AT_comp_dir attribute. */
709 const char *comp_dir = nullptr;
710
711 /* The bfd, when the file is open. Otherwise this is NULL.
712 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
713 gdb_bfd_ref_ptr dbfd;
714
715 /* The sections that make up this DWO file.
716 Remember that for virtual DWO files in DWP V2, these are virtual
717 sections (for lack of a better name). */
718 struct dwo_sections sections {};
719
720 /* The CUs in the file.
721 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
722 an extension to handle LLVM's Link Time Optimization output (where
723 multiple source files may be compiled into a single object/dwo pair). */
724 htab_up cus;
725
726 /* Table of TUs in the file.
727 Each element is a struct dwo_unit. */
728 htab_up tus;
729 };
730
731 /* These sections are what may appear in a DWP file. */
732
733 struct dwp_sections
734 {
735 /* These are used by both DWP version 1 and 2. */
736 struct dwarf2_section_info str;
737 struct dwarf2_section_info cu_index;
738 struct dwarf2_section_info tu_index;
739
740 /* These are only used by DWP version 2 files.
741 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
742 sections are referenced by section number, and are not recorded here.
743 In DWP version 2 there is at most one copy of all these sections, each
744 section being (effectively) comprised of the concatenation of all of the
745 individual sections that exist in the version 1 format.
746 To keep the code simple we treat each of these concatenated pieces as a
747 section itself (a virtual section?). */
748 struct dwarf2_section_info abbrev;
749 struct dwarf2_section_info info;
750 struct dwarf2_section_info line;
751 struct dwarf2_section_info loc;
752 struct dwarf2_section_info macinfo;
753 struct dwarf2_section_info macro;
754 struct dwarf2_section_info str_offsets;
755 struct dwarf2_section_info types;
756 };
757
758 /* These sections are what may appear in a virtual DWO file in DWP version 1.
759 A virtual DWO file is a DWO file as it appears in a DWP file. */
760
761 struct virtual_v1_dwo_sections
762 {
763 struct dwarf2_section_info abbrev;
764 struct dwarf2_section_info line;
765 struct dwarf2_section_info loc;
766 struct dwarf2_section_info macinfo;
767 struct dwarf2_section_info macro;
768 struct dwarf2_section_info str_offsets;
769 /* Each DWP hash table entry records one CU or one TU.
770 That is recorded here, and copied to dwo_unit.section. */
771 struct dwarf2_section_info info_or_types;
772 };
773
774 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
775 In version 2, the sections of the DWO files are concatenated together
776 and stored in one section of that name. Thus each ELF section contains
777 several "virtual" sections. */
778
779 struct virtual_v2_dwo_sections
780 {
781 bfd_size_type abbrev_offset;
782 bfd_size_type abbrev_size;
783
784 bfd_size_type line_offset;
785 bfd_size_type line_size;
786
787 bfd_size_type loc_offset;
788 bfd_size_type loc_size;
789
790 bfd_size_type macinfo_offset;
791 bfd_size_type macinfo_size;
792
793 bfd_size_type macro_offset;
794 bfd_size_type macro_size;
795
796 bfd_size_type str_offsets_offset;
797 bfd_size_type str_offsets_size;
798
799 /* Each DWP hash table entry records one CU or one TU.
800 That is recorded here, and copied to dwo_unit.section. */
801 bfd_size_type info_or_types_offset;
802 bfd_size_type info_or_types_size;
803 };
804
805 /* Contents of DWP hash tables. */
806
807 struct dwp_hash_table
808 {
809 uint32_t version, nr_columns;
810 uint32_t nr_units, nr_slots;
811 const gdb_byte *hash_table, *unit_table;
812 union
813 {
814 struct
815 {
816 const gdb_byte *indices;
817 } v1;
818 struct
819 {
820 /* This is indexed by column number and gives the id of the section
821 in that column. */
822 #define MAX_NR_V2_DWO_SECTIONS \
823 (1 /* .debug_info or .debug_types */ \
824 + 1 /* .debug_abbrev */ \
825 + 1 /* .debug_line */ \
826 + 1 /* .debug_loc */ \
827 + 1 /* .debug_str_offsets */ \
828 + 1 /* .debug_macro or .debug_macinfo */)
829 int section_ids[MAX_NR_V2_DWO_SECTIONS];
830 const gdb_byte *offsets;
831 const gdb_byte *sizes;
832 } v2;
833 } section_pool;
834 };
835
836 /* Data for one DWP file. */
837
838 struct dwp_file
839 {
840 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
841 : name (name_),
842 dbfd (std::move (abfd))
843 {
844 }
845
846 /* Name of the file. */
847 const char *name;
848
849 /* File format version. */
850 int version = 0;
851
852 /* The bfd. */
853 gdb_bfd_ref_ptr dbfd;
854
855 /* Section info for this file. */
856 struct dwp_sections sections {};
857
858 /* Table of CUs in the file. */
859 const struct dwp_hash_table *cus = nullptr;
860
861 /* Table of TUs in the file. */
862 const struct dwp_hash_table *tus = nullptr;
863
864 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
865 htab_up loaded_cus;
866 htab_up loaded_tus;
867
868 /* Table to map ELF section numbers to their sections.
869 This is only needed for the DWP V1 file format. */
870 unsigned int num_sections = 0;
871 asection **elf_sections = nullptr;
872 };
873
874 /* Struct used to pass misc. parameters to read_die_and_children, et
875 al. which are used for both .debug_info and .debug_types dies.
876 All parameters here are unchanging for the life of the call. This
877 struct exists to abstract away the constant parameters of die reading. */
878
879 struct die_reader_specs
880 {
881 /* The bfd of die_section. */
882 bfd* abfd;
883
884 /* The CU of the DIE we are parsing. */
885 struct dwarf2_cu *cu;
886
887 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
888 struct dwo_file *dwo_file;
889
890 /* The section the die comes from.
891 This is either .debug_info or .debug_types, or the .dwo variants. */
892 struct dwarf2_section_info *die_section;
893
894 /* die_section->buffer. */
895 const gdb_byte *buffer;
896
897 /* The end of the buffer. */
898 const gdb_byte *buffer_end;
899
900 /* The abbreviation table to use when reading the DIEs. */
901 struct abbrev_table *abbrev_table;
902 };
903
904 /* A subclass of die_reader_specs that holds storage and has complex
905 constructor and destructor behavior. */
906
907 class cutu_reader : public die_reader_specs
908 {
909 public:
910
911 cutu_reader (struct dwarf2_per_cu_data *this_cu,
912 struct abbrev_table *abbrev_table,
913 int use_existing_cu,
914 bool skip_partial);
915
916 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
917 struct dwarf2_cu *parent_cu = nullptr,
918 struct dwo_file *dwo_file = nullptr);
919
920 DISABLE_COPY_AND_ASSIGN (cutu_reader);
921
922 const gdb_byte *info_ptr = nullptr;
923 struct die_info *comp_unit_die = nullptr;
924 bool dummy_p = false;
925
926 /* Release the new CU, putting it on the chain. This cannot be done
927 for dummy CUs. */
928 void keep ();
929
930 private:
931 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
932 int use_existing_cu);
933
934 struct dwarf2_per_cu_data *m_this_cu;
935 std::unique_ptr<dwarf2_cu> m_new_cu;
936
937 /* The ordinary abbreviation table. */
938 abbrev_table_up m_abbrev_table_holder;
939
940 /* The DWO abbreviation table. */
941 abbrev_table_up m_dwo_abbrev_table;
942 };
943
944 /* When we construct a partial symbol table entry we only
945 need this much information. */
946 struct partial_die_info : public allocate_on_obstack
947 {
948 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
949
950 /* Disable assign but still keep copy ctor, which is needed
951 load_partial_dies. */
952 partial_die_info& operator=(const partial_die_info& rhs) = delete;
953
954 /* Adjust the partial die before generating a symbol for it. This
955 function may set the is_external flag or change the DIE's
956 name. */
957 void fixup (struct dwarf2_cu *cu);
958
959 /* Read a minimal amount of information into the minimal die
960 structure. */
961 const gdb_byte *read (const struct die_reader_specs *reader,
962 const struct abbrev_info &abbrev,
963 const gdb_byte *info_ptr);
964
965 /* Offset of this DIE. */
966 const sect_offset sect_off;
967
968 /* DWARF-2 tag for this DIE. */
969 const ENUM_BITFIELD(dwarf_tag) tag : 16;
970
971 /* Assorted flags describing the data found in this DIE. */
972 const unsigned int has_children : 1;
973
974 unsigned int is_external : 1;
975 unsigned int is_declaration : 1;
976 unsigned int has_type : 1;
977 unsigned int has_specification : 1;
978 unsigned int has_pc_info : 1;
979 unsigned int may_be_inlined : 1;
980
981 /* This DIE has been marked DW_AT_main_subprogram. */
982 unsigned int main_subprogram : 1;
983
984 /* Flag set if the SCOPE field of this structure has been
985 computed. */
986 unsigned int scope_set : 1;
987
988 /* Flag set if the DIE has a byte_size attribute. */
989 unsigned int has_byte_size : 1;
990
991 /* Flag set if the DIE has a DW_AT_const_value attribute. */
992 unsigned int has_const_value : 1;
993
994 /* Flag set if any of the DIE's children are template arguments. */
995 unsigned int has_template_arguments : 1;
996
997 /* Flag set if fixup has been called on this die. */
998 unsigned int fixup_called : 1;
999
1000 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1001 unsigned int is_dwz : 1;
1002
1003 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1004 unsigned int spec_is_dwz : 1;
1005
1006 /* The name of this DIE. Normally the value of DW_AT_name, but
1007 sometimes a default name for unnamed DIEs. */
1008 const char *name = nullptr;
1009
1010 /* The linkage name, if present. */
1011 const char *linkage_name = nullptr;
1012
1013 /* The scope to prepend to our children. This is generally
1014 allocated on the comp_unit_obstack, so will disappear
1015 when this compilation unit leaves the cache. */
1016 const char *scope = nullptr;
1017
1018 /* Some data associated with the partial DIE. The tag determines
1019 which field is live. */
1020 union
1021 {
1022 /* The location description associated with this DIE, if any. */
1023 struct dwarf_block *locdesc;
1024 /* The offset of an import, for DW_TAG_imported_unit. */
1025 sect_offset sect_off;
1026 } d {};
1027
1028 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1029 CORE_ADDR lowpc = 0;
1030 CORE_ADDR highpc = 0;
1031
1032 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1033 DW_AT_sibling, if any. */
1034 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1035 could return DW_AT_sibling values to its caller load_partial_dies. */
1036 const gdb_byte *sibling = nullptr;
1037
1038 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1039 DW_AT_specification (or DW_AT_abstract_origin or
1040 DW_AT_extension). */
1041 sect_offset spec_offset {};
1042
1043 /* Pointers to this DIE's parent, first child, and next sibling,
1044 if any. */
1045 struct partial_die_info *die_parent = nullptr;
1046 struct partial_die_info *die_child = nullptr;
1047 struct partial_die_info *die_sibling = nullptr;
1048
1049 friend struct partial_die_info *
1050 dwarf2_cu::find_partial_die (sect_offset sect_off);
1051
1052 private:
1053 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1054 partial_die_info (sect_offset sect_off)
1055 : partial_die_info (sect_off, DW_TAG_padding, 0)
1056 {
1057 }
1058
1059 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1060 int has_children_)
1061 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1062 {
1063 is_external = 0;
1064 is_declaration = 0;
1065 has_type = 0;
1066 has_specification = 0;
1067 has_pc_info = 0;
1068 may_be_inlined = 0;
1069 main_subprogram = 0;
1070 scope_set = 0;
1071 has_byte_size = 0;
1072 has_const_value = 0;
1073 has_template_arguments = 0;
1074 fixup_called = 0;
1075 is_dwz = 0;
1076 spec_is_dwz = 0;
1077 }
1078 };
1079
1080 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1081 but this would require a corresponding change in unpack_field_as_long
1082 and friends. */
1083 static int bits_per_byte = 8;
1084
1085 /* When reading a variant or variant part, we track a bit more
1086 information about the field, and store it in an object of this
1087 type. */
1088
1089 struct variant_field
1090 {
1091 /* If we see a DW_TAG_variant, then this will be the discriminant
1092 value. */
1093 ULONGEST discriminant_value;
1094 /* If we see a DW_TAG_variant, then this will be set if this is the
1095 default branch. */
1096 bool default_branch;
1097 /* While reading a DW_TAG_variant_part, this will be set if this
1098 field is the discriminant. */
1099 bool is_discriminant;
1100 };
1101
1102 struct nextfield
1103 {
1104 int accessibility = 0;
1105 int virtuality = 0;
1106 /* Extra information to describe a variant or variant part. */
1107 struct variant_field variant {};
1108 struct field field {};
1109 };
1110
1111 struct fnfieldlist
1112 {
1113 const char *name = nullptr;
1114 std::vector<struct fn_field> fnfields;
1115 };
1116
1117 /* The routines that read and process dies for a C struct or C++ class
1118 pass lists of data member fields and lists of member function fields
1119 in an instance of a field_info structure, as defined below. */
1120 struct field_info
1121 {
1122 /* List of data member and baseclasses fields. */
1123 std::vector<struct nextfield> fields;
1124 std::vector<struct nextfield> baseclasses;
1125
1126 /* Set if the accessibility of one of the fields is not public. */
1127 int non_public_fields = 0;
1128
1129 /* Member function fieldlist array, contains name of possibly overloaded
1130 member function, number of overloaded member functions and a pointer
1131 to the head of the member function field chain. */
1132 std::vector<struct fnfieldlist> fnfieldlists;
1133
1134 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1135 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1136 std::vector<struct decl_field> typedef_field_list;
1137
1138 /* Nested types defined by this class and the number of elements in this
1139 list. */
1140 std::vector<struct decl_field> nested_types_list;
1141
1142 /* Return the total number of fields (including baseclasses). */
1143 int nfields () const
1144 {
1145 return fields.size () + baseclasses.size ();
1146 }
1147 };
1148
1149 /* Loaded secondary compilation units are kept in memory until they
1150 have not been referenced for the processing of this many
1151 compilation units. Set this to zero to disable caching. Cache
1152 sizes of up to at least twenty will improve startup time for
1153 typical inter-CU-reference binaries, at an obvious memory cost. */
1154 static int dwarf_max_cache_age = 5;
1155 static void
1156 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1157 struct cmd_list_element *c, const char *value)
1158 {
1159 fprintf_filtered (file, _("The upper bound on the age of cached "
1160 "DWARF compilation units is %s.\n"),
1161 value);
1162 }
1163 \f
1164 /* local function prototypes */
1165
1166 static void dwarf2_find_base_address (struct die_info *die,
1167 struct dwarf2_cu *cu);
1168
1169 static dwarf2_psymtab *create_partial_symtab
1170 (struct dwarf2_per_cu_data *per_cu, const char *name);
1171
1172 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1173 const gdb_byte *info_ptr,
1174 struct die_info *type_unit_die);
1175
1176 static void dwarf2_build_psymtabs_hard
1177 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1178
1179 static void scan_partial_symbols (struct partial_die_info *,
1180 CORE_ADDR *, CORE_ADDR *,
1181 int, struct dwarf2_cu *);
1182
1183 static void add_partial_symbol (struct partial_die_info *,
1184 struct dwarf2_cu *);
1185
1186 static void add_partial_namespace (struct partial_die_info *pdi,
1187 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1188 int set_addrmap, struct dwarf2_cu *cu);
1189
1190 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1191 CORE_ADDR *highpc, int set_addrmap,
1192 struct dwarf2_cu *cu);
1193
1194 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1195 struct dwarf2_cu *cu);
1196
1197 static void add_partial_subprogram (struct partial_die_info *pdi,
1198 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1199 int need_pc, struct dwarf2_cu *cu);
1200
1201 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1202
1203 static struct partial_die_info *load_partial_dies
1204 (const struct die_reader_specs *, const gdb_byte *, int);
1205
1206 /* A pair of partial_die_info and compilation unit. */
1207 struct cu_partial_die_info
1208 {
1209 /* The compilation unit of the partial_die_info. */
1210 struct dwarf2_cu *cu;
1211 /* A partial_die_info. */
1212 struct partial_die_info *pdi;
1213
1214 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1215 : cu (cu),
1216 pdi (pdi)
1217 { /* Nothing. */ }
1218
1219 private:
1220 cu_partial_die_info () = delete;
1221 };
1222
1223 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1224 struct dwarf2_cu *);
1225
1226 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1227 struct attribute *, struct attr_abbrev *,
1228 const gdb_byte *, bool *need_reprocess);
1229
1230 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1231 struct attribute *attr);
1232
1233 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1234
1235 static sect_offset read_abbrev_offset
1236 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1237 struct dwarf2_section_info *, sect_offset);
1238
1239 static const char *read_indirect_string
1240 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1241 const struct comp_unit_head *, unsigned int *);
1242
1243 static const char *read_indirect_string_at_offset
1244 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1245
1246 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1247 const gdb_byte *,
1248 unsigned int *);
1249
1250 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1251 ULONGEST str_index);
1252
1253 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1254 ULONGEST str_index);
1255
1256 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1257
1258 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1259 struct dwarf2_cu *);
1260
1261 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1262 struct dwarf2_cu *cu);
1263
1264 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1265
1266 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1267 struct dwarf2_cu *cu);
1268
1269 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1270
1271 static struct die_info *die_specification (struct die_info *die,
1272 struct dwarf2_cu **);
1273
1274 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1275 struct dwarf2_cu *cu);
1276
1277 static void dwarf_decode_lines (struct line_header *, const char *,
1278 struct dwarf2_cu *, dwarf2_psymtab *,
1279 CORE_ADDR, int decode_mapping);
1280
1281 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1282 const char *);
1283
1284 static struct symbol *new_symbol (struct die_info *, struct type *,
1285 struct dwarf2_cu *, struct symbol * = NULL);
1286
1287 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1288 struct dwarf2_cu *);
1289
1290 static void dwarf2_const_value_attr (const struct attribute *attr,
1291 struct type *type,
1292 const char *name,
1293 struct obstack *obstack,
1294 struct dwarf2_cu *cu, LONGEST *value,
1295 const gdb_byte **bytes,
1296 struct dwarf2_locexpr_baton **baton);
1297
1298 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1299
1300 static int need_gnat_info (struct dwarf2_cu *);
1301
1302 static struct type *die_descriptive_type (struct die_info *,
1303 struct dwarf2_cu *);
1304
1305 static void set_descriptive_type (struct type *, struct die_info *,
1306 struct dwarf2_cu *);
1307
1308 static struct type *die_containing_type (struct die_info *,
1309 struct dwarf2_cu *);
1310
1311 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1312 struct dwarf2_cu *);
1313
1314 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1315
1316 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1317
1318 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1319
1320 static char *typename_concat (struct obstack *obs, const char *prefix,
1321 const char *suffix, int physname,
1322 struct dwarf2_cu *cu);
1323
1324 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1325
1326 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1327
1328 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1329
1330 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1331
1332 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1333
1334 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1335
1336 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1337 struct dwarf2_cu *, dwarf2_psymtab *);
1338
1339 /* Return the .debug_loclists section to use for cu. */
1340 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1341
1342 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1343 values. Keep the items ordered with increasing constraints compliance. */
1344 enum pc_bounds_kind
1345 {
1346 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1347 PC_BOUNDS_NOT_PRESENT,
1348
1349 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1350 were present but they do not form a valid range of PC addresses. */
1351 PC_BOUNDS_INVALID,
1352
1353 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1354 PC_BOUNDS_RANGES,
1355
1356 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1357 PC_BOUNDS_HIGH_LOW,
1358 };
1359
1360 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1361 CORE_ADDR *, CORE_ADDR *,
1362 struct dwarf2_cu *,
1363 dwarf2_psymtab *);
1364
1365 static void get_scope_pc_bounds (struct die_info *,
1366 CORE_ADDR *, CORE_ADDR *,
1367 struct dwarf2_cu *);
1368
1369 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1370 CORE_ADDR, struct dwarf2_cu *);
1371
1372 static void dwarf2_add_field (struct field_info *, struct die_info *,
1373 struct dwarf2_cu *);
1374
1375 static void dwarf2_attach_fields_to_type (struct field_info *,
1376 struct type *, struct dwarf2_cu *);
1377
1378 static void dwarf2_add_member_fn (struct field_info *,
1379 struct die_info *, struct type *,
1380 struct dwarf2_cu *);
1381
1382 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1383 struct type *,
1384 struct dwarf2_cu *);
1385
1386 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1387
1388 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1389
1390 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1391
1392 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1393
1394 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1395
1396 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1397
1398 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1399
1400 static struct type *read_module_type (struct die_info *die,
1401 struct dwarf2_cu *cu);
1402
1403 static const char *namespace_name (struct die_info *die,
1404 int *is_anonymous, struct dwarf2_cu *);
1405
1406 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1407
1408 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1409
1410 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1411 struct dwarf2_cu *);
1412
1413 static struct die_info *read_die_and_siblings_1
1414 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1415 struct die_info *);
1416
1417 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1418 const gdb_byte *info_ptr,
1419 const gdb_byte **new_info_ptr,
1420 struct die_info *parent);
1421
1422 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1423 struct die_info **, const gdb_byte *,
1424 int);
1425
1426 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1427 struct die_info **, const gdb_byte *);
1428
1429 static void process_die (struct die_info *, struct dwarf2_cu *);
1430
1431 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1432 struct objfile *);
1433
1434 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1435
1436 static const char *dwarf2_full_name (const char *name,
1437 struct die_info *die,
1438 struct dwarf2_cu *cu);
1439
1440 static const char *dwarf2_physname (const char *name, struct die_info *die,
1441 struct dwarf2_cu *cu);
1442
1443 static struct die_info *dwarf2_extension (struct die_info *die,
1444 struct dwarf2_cu **);
1445
1446 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1447
1448 static void dump_die_for_error (struct die_info *);
1449
1450 static void dump_die_1 (struct ui_file *, int level, int max_level,
1451 struct die_info *);
1452
1453 /*static*/ void dump_die (struct die_info *, int max_level);
1454
1455 static void store_in_ref_table (struct die_info *,
1456 struct dwarf2_cu *);
1457
1458 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1459 const struct attribute *,
1460 struct dwarf2_cu **);
1461
1462 static struct die_info *follow_die_ref (struct die_info *,
1463 const struct attribute *,
1464 struct dwarf2_cu **);
1465
1466 static struct die_info *follow_die_sig (struct die_info *,
1467 const struct attribute *,
1468 struct dwarf2_cu **);
1469
1470 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1471 struct dwarf2_cu *);
1472
1473 static struct type *get_DW_AT_signature_type (struct die_info *,
1474 const struct attribute *,
1475 struct dwarf2_cu *);
1476
1477 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1478
1479 static void read_signatured_type (struct signatured_type *);
1480
1481 static int attr_to_dynamic_prop (const struct attribute *attr,
1482 struct die_info *die, struct dwarf2_cu *cu,
1483 struct dynamic_prop *prop, struct type *type);
1484
1485 /* memory allocation interface */
1486
1487 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1488
1489 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1490
1491 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1492
1493 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1494 struct dwarf2_loclist_baton *baton,
1495 const struct attribute *attr);
1496
1497 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1498 struct symbol *sym,
1499 struct dwarf2_cu *cu,
1500 int is_block);
1501
1502 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1503 const gdb_byte *info_ptr,
1504 struct abbrev_info *abbrev);
1505
1506 static hashval_t partial_die_hash (const void *item);
1507
1508 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1509
1510 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1511 (sect_offset sect_off, unsigned int offset_in_dwz,
1512 struct dwarf2_per_objfile *dwarf2_per_objfile);
1513
1514 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1515 struct die_info *comp_unit_die,
1516 enum language pretend_language);
1517
1518 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1519
1520 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1521
1522 static struct type *set_die_type (struct die_info *, struct type *,
1523 struct dwarf2_cu *);
1524
1525 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1526
1527 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1528
1529 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1530 enum language);
1531
1532 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1533 enum language);
1534
1535 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1536 enum language);
1537
1538 static void dwarf2_add_dependence (struct dwarf2_cu *,
1539 struct dwarf2_per_cu_data *);
1540
1541 static void dwarf2_mark (struct dwarf2_cu *);
1542
1543 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1544
1545 static struct type *get_die_type_at_offset (sect_offset,
1546 struct dwarf2_per_cu_data *);
1547
1548 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1549
1550 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1551 enum language pretend_language);
1552
1553 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1554
1555 /* Class, the destructor of which frees all allocated queue entries. This
1556 will only have work to do if an error was thrown while processing the
1557 dwarf. If no error was thrown then the queue entries should have all
1558 been processed, and freed, as we went along. */
1559
1560 class dwarf2_queue_guard
1561 {
1562 public:
1563 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1564 : m_per_objfile (per_objfile)
1565 {
1566 }
1567
1568 /* Free any entries remaining on the queue. There should only be
1569 entries left if we hit an error while processing the dwarf. */
1570 ~dwarf2_queue_guard ()
1571 {
1572 /* Ensure that no memory is allocated by the queue. */
1573 std::queue<dwarf2_queue_item> empty;
1574 std::swap (m_per_objfile->queue, empty);
1575 }
1576
1577 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1578
1579 private:
1580 dwarf2_per_objfile *m_per_objfile;
1581 };
1582
1583 dwarf2_queue_item::~dwarf2_queue_item ()
1584 {
1585 /* Anything still marked queued is likely to be in an
1586 inconsistent state, so discard it. */
1587 if (per_cu->queued)
1588 {
1589 if (per_cu->cu != NULL)
1590 free_one_cached_comp_unit (per_cu);
1591 per_cu->queued = 0;
1592 }
1593 }
1594
1595 /* The return type of find_file_and_directory. Note, the enclosed
1596 string pointers are only valid while this object is valid. */
1597
1598 struct file_and_directory
1599 {
1600 /* The filename. This is never NULL. */
1601 const char *name;
1602
1603 /* The compilation directory. NULL if not known. If we needed to
1604 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1605 points directly to the DW_AT_comp_dir string attribute owned by
1606 the obstack that owns the DIE. */
1607 const char *comp_dir;
1608
1609 /* If we needed to build a new string for comp_dir, this is what
1610 owns the storage. */
1611 std::string comp_dir_storage;
1612 };
1613
1614 static file_and_directory find_file_and_directory (struct die_info *die,
1615 struct dwarf2_cu *cu);
1616
1617 static htab_up allocate_signatured_type_table ();
1618
1619 static htab_up allocate_dwo_unit_table ();
1620
1621 static struct dwo_unit *lookup_dwo_unit_in_dwp
1622 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1623 struct dwp_file *dwp_file, const char *comp_dir,
1624 ULONGEST signature, int is_debug_types);
1625
1626 static struct dwp_file *get_dwp_file
1627 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1628
1629 static struct dwo_unit *lookup_dwo_comp_unit
1630 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1631
1632 static struct dwo_unit *lookup_dwo_type_unit
1633 (struct signatured_type *, const char *, const char *);
1634
1635 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1636
1637 /* A unique pointer to a dwo_file. */
1638
1639 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1640
1641 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1642
1643 static void check_producer (struct dwarf2_cu *cu);
1644
1645 static void free_line_header_voidp (void *arg);
1646 \f
1647 /* Various complaints about symbol reading that don't abort the process. */
1648
1649 static void
1650 dwarf2_debug_line_missing_file_complaint (void)
1651 {
1652 complaint (_(".debug_line section has line data without a file"));
1653 }
1654
1655 static void
1656 dwarf2_debug_line_missing_end_sequence_complaint (void)
1657 {
1658 complaint (_(".debug_line section has line "
1659 "program sequence without an end"));
1660 }
1661
1662 static void
1663 dwarf2_complex_location_expr_complaint (void)
1664 {
1665 complaint (_("location expression too complex"));
1666 }
1667
1668 static void
1669 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1670 int arg3)
1671 {
1672 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1673 arg1, arg2, arg3);
1674 }
1675
1676 static void
1677 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1678 {
1679 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1680 arg1, arg2);
1681 }
1682
1683 /* Hash function for line_header_hash. */
1684
1685 static hashval_t
1686 line_header_hash (const struct line_header *ofs)
1687 {
1688 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1689 }
1690
1691 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1692
1693 static hashval_t
1694 line_header_hash_voidp (const void *item)
1695 {
1696 const struct line_header *ofs = (const struct line_header *) item;
1697
1698 return line_header_hash (ofs);
1699 }
1700
1701 /* Equality function for line_header_hash. */
1702
1703 static int
1704 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1705 {
1706 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1707 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1708
1709 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1710 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1711 }
1712
1713 \f
1714
1715 /* See declaration. */
1716
1717 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
1718 const dwarf2_debug_sections *names,
1719 bool can_copy_)
1720 : objfile (objfile_),
1721 can_copy (can_copy_)
1722 {
1723 if (names == NULL)
1724 names = &dwarf2_elf_names;
1725
1726 bfd *obfd = objfile->obfd;
1727
1728 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1729 locate_sections (obfd, sec, *names);
1730 }
1731
1732 dwarf2_per_objfile::~dwarf2_per_objfile ()
1733 {
1734 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1735 free_cached_comp_units ();
1736
1737 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1738 per_cu->imported_symtabs_free ();
1739
1740 for (signatured_type *sig_type : all_type_units)
1741 sig_type->per_cu.imported_symtabs_free ();
1742
1743 /* Everything else should be on the objfile obstack. */
1744 }
1745
1746 /* See declaration. */
1747
1748 void
1749 dwarf2_per_objfile::free_cached_comp_units ()
1750 {
1751 dwarf2_per_cu_data *per_cu = read_in_chain;
1752 dwarf2_per_cu_data **last_chain = &read_in_chain;
1753 while (per_cu != NULL)
1754 {
1755 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1756
1757 delete per_cu->cu;
1758 *last_chain = next_cu;
1759 per_cu = next_cu;
1760 }
1761 }
1762
1763 /* A helper class that calls free_cached_comp_units on
1764 destruction. */
1765
1766 class free_cached_comp_units
1767 {
1768 public:
1769
1770 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1771 : m_per_objfile (per_objfile)
1772 {
1773 }
1774
1775 ~free_cached_comp_units ()
1776 {
1777 m_per_objfile->free_cached_comp_units ();
1778 }
1779
1780 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1781
1782 private:
1783
1784 dwarf2_per_objfile *m_per_objfile;
1785 };
1786
1787 /* Try to locate the sections we need for DWARF 2 debugging
1788 information and return true if we have enough to do something.
1789 NAMES points to the dwarf2 section names, or is NULL if the standard
1790 ELF names are used. CAN_COPY is true for formats where symbol
1791 interposition is possible and so symbol values must follow copy
1792 relocation rules. */
1793
1794 int
1795 dwarf2_has_info (struct objfile *objfile,
1796 const struct dwarf2_debug_sections *names,
1797 bool can_copy)
1798 {
1799 if (objfile->flags & OBJF_READNEVER)
1800 return 0;
1801
1802 struct dwarf2_per_objfile *dwarf2_per_objfile
1803 = get_dwarf2_per_objfile (objfile);
1804
1805 if (dwarf2_per_objfile == NULL)
1806 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
1807 names,
1808 can_copy);
1809
1810 return (!dwarf2_per_objfile->info.is_virtual
1811 && dwarf2_per_objfile->info.s.section != NULL
1812 && !dwarf2_per_objfile->abbrev.is_virtual
1813 && dwarf2_per_objfile->abbrev.s.section != NULL);
1814 }
1815
1816 /* When loading sections, we look either for uncompressed section or for
1817 compressed section names. */
1818
1819 static int
1820 section_is_p (const char *section_name,
1821 const struct dwarf2_section_names *names)
1822 {
1823 if (names->normal != NULL
1824 && strcmp (section_name, names->normal) == 0)
1825 return 1;
1826 if (names->compressed != NULL
1827 && strcmp (section_name, names->compressed) == 0)
1828 return 1;
1829 return 0;
1830 }
1831
1832 /* See declaration. */
1833
1834 void
1835 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1836 const dwarf2_debug_sections &names)
1837 {
1838 flagword aflag = bfd_section_flags (sectp);
1839
1840 if ((aflag & SEC_HAS_CONTENTS) == 0)
1841 {
1842 }
1843 else if (elf_section_data (sectp)->this_hdr.sh_size
1844 > bfd_get_file_size (abfd))
1845 {
1846 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1847 warning (_("Discarding section %s which has a section size (%s"
1848 ") larger than the file size [in module %s]"),
1849 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1850 bfd_get_filename (abfd));
1851 }
1852 else if (section_is_p (sectp->name, &names.info))
1853 {
1854 this->info.s.section = sectp;
1855 this->info.size = bfd_section_size (sectp);
1856 }
1857 else if (section_is_p (sectp->name, &names.abbrev))
1858 {
1859 this->abbrev.s.section = sectp;
1860 this->abbrev.size = bfd_section_size (sectp);
1861 }
1862 else if (section_is_p (sectp->name, &names.line))
1863 {
1864 this->line.s.section = sectp;
1865 this->line.size = bfd_section_size (sectp);
1866 }
1867 else if (section_is_p (sectp->name, &names.loc))
1868 {
1869 this->loc.s.section = sectp;
1870 this->loc.size = bfd_section_size (sectp);
1871 }
1872 else if (section_is_p (sectp->name, &names.loclists))
1873 {
1874 this->loclists.s.section = sectp;
1875 this->loclists.size = bfd_section_size (sectp);
1876 }
1877 else if (section_is_p (sectp->name, &names.macinfo))
1878 {
1879 this->macinfo.s.section = sectp;
1880 this->macinfo.size = bfd_section_size (sectp);
1881 }
1882 else if (section_is_p (sectp->name, &names.macro))
1883 {
1884 this->macro.s.section = sectp;
1885 this->macro.size = bfd_section_size (sectp);
1886 }
1887 else if (section_is_p (sectp->name, &names.str))
1888 {
1889 this->str.s.section = sectp;
1890 this->str.size = bfd_section_size (sectp);
1891 }
1892 else if (section_is_p (sectp->name, &names.str_offsets))
1893 {
1894 this->str_offsets.s.section = sectp;
1895 this->str_offsets.size = bfd_section_size (sectp);
1896 }
1897 else if (section_is_p (sectp->name, &names.line_str))
1898 {
1899 this->line_str.s.section = sectp;
1900 this->line_str.size = bfd_section_size (sectp);
1901 }
1902 else if (section_is_p (sectp->name, &names.addr))
1903 {
1904 this->addr.s.section = sectp;
1905 this->addr.size = bfd_section_size (sectp);
1906 }
1907 else if (section_is_p (sectp->name, &names.frame))
1908 {
1909 this->frame.s.section = sectp;
1910 this->frame.size = bfd_section_size (sectp);
1911 }
1912 else if (section_is_p (sectp->name, &names.eh_frame))
1913 {
1914 this->eh_frame.s.section = sectp;
1915 this->eh_frame.size = bfd_section_size (sectp);
1916 }
1917 else if (section_is_p (sectp->name, &names.ranges))
1918 {
1919 this->ranges.s.section = sectp;
1920 this->ranges.size = bfd_section_size (sectp);
1921 }
1922 else if (section_is_p (sectp->name, &names.rnglists))
1923 {
1924 this->rnglists.s.section = sectp;
1925 this->rnglists.size = bfd_section_size (sectp);
1926 }
1927 else if (section_is_p (sectp->name, &names.types))
1928 {
1929 struct dwarf2_section_info type_section;
1930
1931 memset (&type_section, 0, sizeof (type_section));
1932 type_section.s.section = sectp;
1933 type_section.size = bfd_section_size (sectp);
1934
1935 this->types.push_back (type_section);
1936 }
1937 else if (section_is_p (sectp->name, &names.gdb_index))
1938 {
1939 this->gdb_index.s.section = sectp;
1940 this->gdb_index.size = bfd_section_size (sectp);
1941 }
1942 else if (section_is_p (sectp->name, &names.debug_names))
1943 {
1944 this->debug_names.s.section = sectp;
1945 this->debug_names.size = bfd_section_size (sectp);
1946 }
1947 else if (section_is_p (sectp->name, &names.debug_aranges))
1948 {
1949 this->debug_aranges.s.section = sectp;
1950 this->debug_aranges.size = bfd_section_size (sectp);
1951 }
1952
1953 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1954 && bfd_section_vma (sectp) == 0)
1955 this->has_section_at_zero = true;
1956 }
1957
1958 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1959 SECTION_NAME. */
1960
1961 void
1962 dwarf2_get_section_info (struct objfile *objfile,
1963 enum dwarf2_section_enum sect,
1964 asection **sectp, const gdb_byte **bufp,
1965 bfd_size_type *sizep)
1966 {
1967 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
1968 struct dwarf2_section_info *info;
1969
1970 /* We may see an objfile without any DWARF, in which case we just
1971 return nothing. */
1972 if (data == NULL)
1973 {
1974 *sectp = NULL;
1975 *bufp = NULL;
1976 *sizep = 0;
1977 return;
1978 }
1979 switch (sect)
1980 {
1981 case DWARF2_DEBUG_FRAME:
1982 info = &data->frame;
1983 break;
1984 case DWARF2_EH_FRAME:
1985 info = &data->eh_frame;
1986 break;
1987 default:
1988 gdb_assert_not_reached ("unexpected section");
1989 }
1990
1991 info->read (objfile);
1992
1993 *sectp = info->get_bfd_section ();
1994 *bufp = info->buffer;
1995 *sizep = info->size;
1996 }
1997
1998 /* A helper function to find the sections for a .dwz file. */
1999
2000 static void
2001 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2002 {
2003 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2004
2005 /* Note that we only support the standard ELF names, because .dwz
2006 is ELF-only (at the time of writing). */
2007 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2008 {
2009 dwz_file->abbrev.s.section = sectp;
2010 dwz_file->abbrev.size = bfd_section_size (sectp);
2011 }
2012 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2013 {
2014 dwz_file->info.s.section = sectp;
2015 dwz_file->info.size = bfd_section_size (sectp);
2016 }
2017 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2018 {
2019 dwz_file->str.s.section = sectp;
2020 dwz_file->str.size = bfd_section_size (sectp);
2021 }
2022 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2023 {
2024 dwz_file->line.s.section = sectp;
2025 dwz_file->line.size = bfd_section_size (sectp);
2026 }
2027 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2028 {
2029 dwz_file->macro.s.section = sectp;
2030 dwz_file->macro.size = bfd_section_size (sectp);
2031 }
2032 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2033 {
2034 dwz_file->gdb_index.s.section = sectp;
2035 dwz_file->gdb_index.size = bfd_section_size (sectp);
2036 }
2037 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2038 {
2039 dwz_file->debug_names.s.section = sectp;
2040 dwz_file->debug_names.size = bfd_section_size (sectp);
2041 }
2042 }
2043
2044 /* See dwarf2read.h. */
2045
2046 struct dwz_file *
2047 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2048 {
2049 const char *filename;
2050 bfd_size_type buildid_len_arg;
2051 size_t buildid_len;
2052 bfd_byte *buildid;
2053
2054 if (dwarf2_per_objfile->dwz_file != NULL)
2055 return dwarf2_per_objfile->dwz_file.get ();
2056
2057 bfd_set_error (bfd_error_no_error);
2058 gdb::unique_xmalloc_ptr<char> data
2059 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2060 &buildid_len_arg, &buildid));
2061 if (data == NULL)
2062 {
2063 if (bfd_get_error () == bfd_error_no_error)
2064 return NULL;
2065 error (_("could not read '.gnu_debugaltlink' section: %s"),
2066 bfd_errmsg (bfd_get_error ()));
2067 }
2068
2069 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2070
2071 buildid_len = (size_t) buildid_len_arg;
2072
2073 filename = data.get ();
2074
2075 std::string abs_storage;
2076 if (!IS_ABSOLUTE_PATH (filename))
2077 {
2078 gdb::unique_xmalloc_ptr<char> abs
2079 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2080
2081 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2082 filename = abs_storage.c_str ();
2083 }
2084
2085 /* First try the file name given in the section. If that doesn't
2086 work, try to use the build-id instead. */
2087 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2088 if (dwz_bfd != NULL)
2089 {
2090 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2091 dwz_bfd.reset (nullptr);
2092 }
2093
2094 if (dwz_bfd == NULL)
2095 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2096
2097 if (dwz_bfd == nullptr)
2098 {
2099 gdb::unique_xmalloc_ptr<char> alt_filename;
2100 const char *origname = dwarf2_per_objfile->objfile->original_name;
2101
2102 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2103 buildid_len,
2104 origname,
2105 &alt_filename));
2106
2107 if (fd.get () >= 0)
2108 {
2109 /* File successfully retrieved from server. */
2110 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2111
2112 if (dwz_bfd == nullptr)
2113 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2114 alt_filename.get ());
2115 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2116 dwz_bfd.reset (nullptr);
2117 }
2118 }
2119
2120 if (dwz_bfd == NULL)
2121 error (_("could not find '.gnu_debugaltlink' file for %s"),
2122 objfile_name (dwarf2_per_objfile->objfile));
2123
2124 std::unique_ptr<struct dwz_file> result
2125 (new struct dwz_file (std::move (dwz_bfd)));
2126
2127 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2128 result.get ());
2129
2130 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2131 result->dwz_bfd.get ());
2132 dwarf2_per_objfile->dwz_file = std::move (result);
2133 return dwarf2_per_objfile->dwz_file.get ();
2134 }
2135 \f
2136 /* DWARF quick_symbols_functions support. */
2137
2138 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2139 unique line tables, so we maintain a separate table of all .debug_line
2140 derived entries to support the sharing.
2141 All the quick functions need is the list of file names. We discard the
2142 line_header when we're done and don't need to record it here. */
2143 struct quick_file_names
2144 {
2145 /* The data used to construct the hash key. */
2146 struct stmt_list_hash hash;
2147
2148 /* The number of entries in file_names, real_names. */
2149 unsigned int num_file_names;
2150
2151 /* The file names from the line table, after being run through
2152 file_full_name. */
2153 const char **file_names;
2154
2155 /* The file names from the line table after being run through
2156 gdb_realpath. These are computed lazily. */
2157 const char **real_names;
2158 };
2159
2160 /* When using the index (and thus not using psymtabs), each CU has an
2161 object of this type. This is used to hold information needed by
2162 the various "quick" methods. */
2163 struct dwarf2_per_cu_quick_data
2164 {
2165 /* The file table. This can be NULL if there was no file table
2166 or it's currently not read in.
2167 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2168 struct quick_file_names *file_names;
2169
2170 /* The corresponding symbol table. This is NULL if symbols for this
2171 CU have not yet been read. */
2172 struct compunit_symtab *compunit_symtab;
2173
2174 /* A temporary mark bit used when iterating over all CUs in
2175 expand_symtabs_matching. */
2176 unsigned int mark : 1;
2177
2178 /* True if we've tried to read the file table and found there isn't one.
2179 There will be no point in trying to read it again next time. */
2180 unsigned int no_file_data : 1;
2181 };
2182
2183 /* Utility hash function for a stmt_list_hash. */
2184
2185 static hashval_t
2186 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2187 {
2188 hashval_t v = 0;
2189
2190 if (stmt_list_hash->dwo_unit != NULL)
2191 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2192 v += to_underlying (stmt_list_hash->line_sect_off);
2193 return v;
2194 }
2195
2196 /* Utility equality function for a stmt_list_hash. */
2197
2198 static int
2199 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2200 const struct stmt_list_hash *rhs)
2201 {
2202 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2203 return 0;
2204 if (lhs->dwo_unit != NULL
2205 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2206 return 0;
2207
2208 return lhs->line_sect_off == rhs->line_sect_off;
2209 }
2210
2211 /* Hash function for a quick_file_names. */
2212
2213 static hashval_t
2214 hash_file_name_entry (const void *e)
2215 {
2216 const struct quick_file_names *file_data
2217 = (const struct quick_file_names *) e;
2218
2219 return hash_stmt_list_entry (&file_data->hash);
2220 }
2221
2222 /* Equality function for a quick_file_names. */
2223
2224 static int
2225 eq_file_name_entry (const void *a, const void *b)
2226 {
2227 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2228 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2229
2230 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2231 }
2232
2233 /* Delete function for a quick_file_names. */
2234
2235 static void
2236 delete_file_name_entry (void *e)
2237 {
2238 struct quick_file_names *file_data = (struct quick_file_names *) e;
2239 int i;
2240
2241 for (i = 0; i < file_data->num_file_names; ++i)
2242 {
2243 xfree ((void*) file_data->file_names[i]);
2244 if (file_data->real_names)
2245 xfree ((void*) file_data->real_names[i]);
2246 }
2247
2248 /* The space for the struct itself lives on objfile_obstack,
2249 so we don't free it here. */
2250 }
2251
2252 /* Create a quick_file_names hash table. */
2253
2254 static htab_up
2255 create_quick_file_names_table (unsigned int nr_initial_entries)
2256 {
2257 return htab_up (htab_create_alloc (nr_initial_entries,
2258 hash_file_name_entry, eq_file_name_entry,
2259 delete_file_name_entry, xcalloc, xfree));
2260 }
2261
2262 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2263 have to be created afterwards. You should call age_cached_comp_units after
2264 processing PER_CU->CU. dw2_setup must have been already called. */
2265
2266 static void
2267 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2268 {
2269 if (per_cu->is_debug_types)
2270 load_full_type_unit (per_cu);
2271 else
2272 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2273
2274 if (per_cu->cu == NULL)
2275 return; /* Dummy CU. */
2276
2277 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2278 }
2279
2280 /* Read in the symbols for PER_CU. */
2281
2282 static void
2283 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2284 {
2285 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2286
2287 /* Skip type_unit_groups, reading the type units they contain
2288 is handled elsewhere. */
2289 if (per_cu->type_unit_group_p ())
2290 return;
2291
2292 /* The destructor of dwarf2_queue_guard frees any entries left on
2293 the queue. After this point we're guaranteed to leave this function
2294 with the dwarf queue empty. */
2295 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2296
2297 if (dwarf2_per_objfile->using_index
2298 ? per_cu->v.quick->compunit_symtab == NULL
2299 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2300 {
2301 queue_comp_unit (per_cu, language_minimal);
2302 load_cu (per_cu, skip_partial);
2303
2304 /* If we just loaded a CU from a DWO, and we're working with an index
2305 that may badly handle TUs, load all the TUs in that DWO as well.
2306 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2307 if (!per_cu->is_debug_types
2308 && per_cu->cu != NULL
2309 && per_cu->cu->dwo_unit != NULL
2310 && dwarf2_per_objfile->index_table != NULL
2311 && dwarf2_per_objfile->index_table->version <= 7
2312 /* DWP files aren't supported yet. */
2313 && get_dwp_file (dwarf2_per_objfile) == NULL)
2314 queue_and_load_all_dwo_tus (per_cu);
2315 }
2316
2317 process_queue (dwarf2_per_objfile);
2318
2319 /* Age the cache, releasing compilation units that have not
2320 been used recently. */
2321 age_cached_comp_units (dwarf2_per_objfile);
2322 }
2323
2324 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2325 the objfile from which this CU came. Returns the resulting symbol
2326 table. */
2327
2328 static struct compunit_symtab *
2329 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2330 {
2331 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2332
2333 gdb_assert (dwarf2_per_objfile->using_index);
2334 if (!per_cu->v.quick->compunit_symtab)
2335 {
2336 free_cached_comp_units freer (dwarf2_per_objfile);
2337 scoped_restore decrementer = increment_reading_symtab ();
2338 dw2_do_instantiate_symtab (per_cu, skip_partial);
2339 process_cu_includes (dwarf2_per_objfile);
2340 }
2341
2342 return per_cu->v.quick->compunit_symtab;
2343 }
2344
2345 /* See declaration. */
2346
2347 dwarf2_per_cu_data *
2348 dwarf2_per_objfile::get_cutu (int index)
2349 {
2350 if (index >= this->all_comp_units.size ())
2351 {
2352 index -= this->all_comp_units.size ();
2353 gdb_assert (index < this->all_type_units.size ());
2354 return &this->all_type_units[index]->per_cu;
2355 }
2356
2357 return this->all_comp_units[index];
2358 }
2359
2360 /* See declaration. */
2361
2362 dwarf2_per_cu_data *
2363 dwarf2_per_objfile::get_cu (int index)
2364 {
2365 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2366
2367 return this->all_comp_units[index];
2368 }
2369
2370 /* See declaration. */
2371
2372 signatured_type *
2373 dwarf2_per_objfile::get_tu (int index)
2374 {
2375 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2376
2377 return this->all_type_units[index];
2378 }
2379
2380 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2381 objfile_obstack, and constructed with the specified field
2382 values. */
2383
2384 static dwarf2_per_cu_data *
2385 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2386 struct dwarf2_section_info *section,
2387 int is_dwz,
2388 sect_offset sect_off, ULONGEST length)
2389 {
2390 struct objfile *objfile = dwarf2_per_objfile->objfile;
2391 dwarf2_per_cu_data *the_cu
2392 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2393 struct dwarf2_per_cu_data);
2394 the_cu->sect_off = sect_off;
2395 the_cu->length = length;
2396 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2397 the_cu->section = section;
2398 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2399 struct dwarf2_per_cu_quick_data);
2400 the_cu->is_dwz = is_dwz;
2401 return the_cu;
2402 }
2403
2404 /* A helper for create_cus_from_index that handles a given list of
2405 CUs. */
2406
2407 static void
2408 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2409 const gdb_byte *cu_list, offset_type n_elements,
2410 struct dwarf2_section_info *section,
2411 int is_dwz)
2412 {
2413 for (offset_type i = 0; i < n_elements; i += 2)
2414 {
2415 gdb_static_assert (sizeof (ULONGEST) >= 8);
2416
2417 sect_offset sect_off
2418 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2419 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2420 cu_list += 2 * 8;
2421
2422 dwarf2_per_cu_data *per_cu
2423 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2424 sect_off, length);
2425 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2426 }
2427 }
2428
2429 /* Read the CU list from the mapped index, and use it to create all
2430 the CU objects for this objfile. */
2431
2432 static void
2433 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2434 const gdb_byte *cu_list, offset_type cu_list_elements,
2435 const gdb_byte *dwz_list, offset_type dwz_elements)
2436 {
2437 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2438 dwarf2_per_objfile->all_comp_units.reserve
2439 ((cu_list_elements + dwz_elements) / 2);
2440
2441 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2442 &dwarf2_per_objfile->info, 0);
2443
2444 if (dwz_elements == 0)
2445 return;
2446
2447 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2448 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2449 &dwz->info, 1);
2450 }
2451
2452 /* Create the signatured type hash table from the index. */
2453
2454 static void
2455 create_signatured_type_table_from_index
2456 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2457 struct dwarf2_section_info *section,
2458 const gdb_byte *bytes,
2459 offset_type elements)
2460 {
2461 struct objfile *objfile = dwarf2_per_objfile->objfile;
2462
2463 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2464 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2465
2466 htab_up sig_types_hash = allocate_signatured_type_table ();
2467
2468 for (offset_type i = 0; i < elements; i += 3)
2469 {
2470 struct signatured_type *sig_type;
2471 ULONGEST signature;
2472 void **slot;
2473 cu_offset type_offset_in_tu;
2474
2475 gdb_static_assert (sizeof (ULONGEST) >= 8);
2476 sect_offset sect_off
2477 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2478 type_offset_in_tu
2479 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2480 BFD_ENDIAN_LITTLE);
2481 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2482 bytes += 3 * 8;
2483
2484 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2485 struct signatured_type);
2486 sig_type->signature = signature;
2487 sig_type->type_offset_in_tu = type_offset_in_tu;
2488 sig_type->per_cu.is_debug_types = 1;
2489 sig_type->per_cu.section = section;
2490 sig_type->per_cu.sect_off = sect_off;
2491 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2492 sig_type->per_cu.v.quick
2493 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2494 struct dwarf2_per_cu_quick_data);
2495
2496 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2497 *slot = sig_type;
2498
2499 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2500 }
2501
2502 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2503 }
2504
2505 /* Create the signatured type hash table from .debug_names. */
2506
2507 static void
2508 create_signatured_type_table_from_debug_names
2509 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2510 const mapped_debug_names &map,
2511 struct dwarf2_section_info *section,
2512 struct dwarf2_section_info *abbrev_section)
2513 {
2514 struct objfile *objfile = dwarf2_per_objfile->objfile;
2515
2516 section->read (objfile);
2517 abbrev_section->read (objfile);
2518
2519 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2520 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2521
2522 htab_up sig_types_hash = allocate_signatured_type_table ();
2523
2524 for (uint32_t i = 0; i < map.tu_count; ++i)
2525 {
2526 struct signatured_type *sig_type;
2527 void **slot;
2528
2529 sect_offset sect_off
2530 = (sect_offset) (extract_unsigned_integer
2531 (map.tu_table_reordered + i * map.offset_size,
2532 map.offset_size,
2533 map.dwarf5_byte_order));
2534
2535 comp_unit_head cu_header;
2536 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2537 abbrev_section,
2538 section->buffer + to_underlying (sect_off),
2539 rcuh_kind::TYPE);
2540
2541 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2542 struct signatured_type);
2543 sig_type->signature = cu_header.signature;
2544 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2545 sig_type->per_cu.is_debug_types = 1;
2546 sig_type->per_cu.section = section;
2547 sig_type->per_cu.sect_off = sect_off;
2548 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2549 sig_type->per_cu.v.quick
2550 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2551 struct dwarf2_per_cu_quick_data);
2552
2553 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2554 *slot = sig_type;
2555
2556 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2557 }
2558
2559 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2560 }
2561
2562 /* Read the address map data from the mapped index, and use it to
2563 populate the objfile's psymtabs_addrmap. */
2564
2565 static void
2566 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2567 struct mapped_index *index)
2568 {
2569 struct objfile *objfile = dwarf2_per_objfile->objfile;
2570 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2571 const gdb_byte *iter, *end;
2572 struct addrmap *mutable_map;
2573 CORE_ADDR baseaddr;
2574
2575 auto_obstack temp_obstack;
2576
2577 mutable_map = addrmap_create_mutable (&temp_obstack);
2578
2579 iter = index->address_table.data ();
2580 end = iter + index->address_table.size ();
2581
2582 baseaddr = objfile->text_section_offset ();
2583
2584 while (iter < end)
2585 {
2586 ULONGEST hi, lo, cu_index;
2587 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2588 iter += 8;
2589 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2590 iter += 8;
2591 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2592 iter += 4;
2593
2594 if (lo > hi)
2595 {
2596 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2597 hex_string (lo), hex_string (hi));
2598 continue;
2599 }
2600
2601 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2602 {
2603 complaint (_(".gdb_index address table has invalid CU number %u"),
2604 (unsigned) cu_index);
2605 continue;
2606 }
2607
2608 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2609 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2610 addrmap_set_empty (mutable_map, lo, hi - 1,
2611 dwarf2_per_objfile->get_cu (cu_index));
2612 }
2613
2614 objfile->partial_symtabs->psymtabs_addrmap
2615 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2616 }
2617
2618 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2619 populate the objfile's psymtabs_addrmap. */
2620
2621 static void
2622 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2623 struct dwarf2_section_info *section)
2624 {
2625 struct objfile *objfile = dwarf2_per_objfile->objfile;
2626 bfd *abfd = objfile->obfd;
2627 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2628 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2629
2630 auto_obstack temp_obstack;
2631 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2632
2633 std::unordered_map<sect_offset,
2634 dwarf2_per_cu_data *,
2635 gdb::hash_enum<sect_offset>>
2636 debug_info_offset_to_per_cu;
2637 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2638 {
2639 const auto insertpair
2640 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2641 if (!insertpair.second)
2642 {
2643 warning (_("Section .debug_aranges in %s has duplicate "
2644 "debug_info_offset %s, ignoring .debug_aranges."),
2645 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2646 return;
2647 }
2648 }
2649
2650 section->read (objfile);
2651
2652 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2653
2654 const gdb_byte *addr = section->buffer;
2655
2656 while (addr < section->buffer + section->size)
2657 {
2658 const gdb_byte *const entry_addr = addr;
2659 unsigned int bytes_read;
2660
2661 const LONGEST entry_length = read_initial_length (abfd, addr,
2662 &bytes_read);
2663 addr += bytes_read;
2664
2665 const gdb_byte *const entry_end = addr + entry_length;
2666 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2667 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2668 if (addr + entry_length > section->buffer + section->size)
2669 {
2670 warning (_("Section .debug_aranges in %s entry at offset %s "
2671 "length %s exceeds section length %s, "
2672 "ignoring .debug_aranges."),
2673 objfile_name (objfile),
2674 plongest (entry_addr - section->buffer),
2675 plongest (bytes_read + entry_length),
2676 pulongest (section->size));
2677 return;
2678 }
2679
2680 /* The version number. */
2681 const uint16_t version = read_2_bytes (abfd, addr);
2682 addr += 2;
2683 if (version != 2)
2684 {
2685 warning (_("Section .debug_aranges in %s entry at offset %s "
2686 "has unsupported version %d, ignoring .debug_aranges."),
2687 objfile_name (objfile),
2688 plongest (entry_addr - section->buffer), version);
2689 return;
2690 }
2691
2692 const uint64_t debug_info_offset
2693 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2694 addr += offset_size;
2695 const auto per_cu_it
2696 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2697 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2698 {
2699 warning (_("Section .debug_aranges in %s entry at offset %s "
2700 "debug_info_offset %s does not exists, "
2701 "ignoring .debug_aranges."),
2702 objfile_name (objfile),
2703 plongest (entry_addr - section->buffer),
2704 pulongest (debug_info_offset));
2705 return;
2706 }
2707 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2708
2709 const uint8_t address_size = *addr++;
2710 if (address_size < 1 || address_size > 8)
2711 {
2712 warning (_("Section .debug_aranges in %s entry at offset %s "
2713 "address_size %u is invalid, ignoring .debug_aranges."),
2714 objfile_name (objfile),
2715 plongest (entry_addr - section->buffer), address_size);
2716 return;
2717 }
2718
2719 const uint8_t segment_selector_size = *addr++;
2720 if (segment_selector_size != 0)
2721 {
2722 warning (_("Section .debug_aranges in %s entry at offset %s "
2723 "segment_selector_size %u is not supported, "
2724 "ignoring .debug_aranges."),
2725 objfile_name (objfile),
2726 plongest (entry_addr - section->buffer),
2727 segment_selector_size);
2728 return;
2729 }
2730
2731 /* Must pad to an alignment boundary that is twice the address
2732 size. It is undocumented by the DWARF standard but GCC does
2733 use it. */
2734 for (size_t padding = ((-(addr - section->buffer))
2735 & (2 * address_size - 1));
2736 padding > 0; padding--)
2737 if (*addr++ != 0)
2738 {
2739 warning (_("Section .debug_aranges in %s entry at offset %s "
2740 "padding is not zero, ignoring .debug_aranges."),
2741 objfile_name (objfile),
2742 plongest (entry_addr - section->buffer));
2743 return;
2744 }
2745
2746 for (;;)
2747 {
2748 if (addr + 2 * address_size > entry_end)
2749 {
2750 warning (_("Section .debug_aranges in %s entry at offset %s "
2751 "address list is not properly terminated, "
2752 "ignoring .debug_aranges."),
2753 objfile_name (objfile),
2754 plongest (entry_addr - section->buffer));
2755 return;
2756 }
2757 ULONGEST start = extract_unsigned_integer (addr, address_size,
2758 dwarf5_byte_order);
2759 addr += address_size;
2760 ULONGEST length = extract_unsigned_integer (addr, address_size,
2761 dwarf5_byte_order);
2762 addr += address_size;
2763 if (start == 0 && length == 0)
2764 break;
2765 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2766 {
2767 /* Symbol was eliminated due to a COMDAT group. */
2768 continue;
2769 }
2770 ULONGEST end = start + length;
2771 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2772 - baseaddr);
2773 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2774 - baseaddr);
2775 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2776 }
2777 }
2778
2779 objfile->partial_symtabs->psymtabs_addrmap
2780 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2781 }
2782
2783 /* Find a slot in the mapped index INDEX for the object named NAME.
2784 If NAME is found, set *VEC_OUT to point to the CU vector in the
2785 constant pool and return true. If NAME cannot be found, return
2786 false. */
2787
2788 static bool
2789 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2790 offset_type **vec_out)
2791 {
2792 offset_type hash;
2793 offset_type slot, step;
2794 int (*cmp) (const char *, const char *);
2795
2796 gdb::unique_xmalloc_ptr<char> without_params;
2797 if (current_language->la_language == language_cplus
2798 || current_language->la_language == language_fortran
2799 || current_language->la_language == language_d)
2800 {
2801 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2802 not contain any. */
2803
2804 if (strchr (name, '(') != NULL)
2805 {
2806 without_params = cp_remove_params (name);
2807
2808 if (without_params != NULL)
2809 name = without_params.get ();
2810 }
2811 }
2812
2813 /* Index version 4 did not support case insensitive searches. But the
2814 indices for case insensitive languages are built in lowercase, therefore
2815 simulate our NAME being searched is also lowercased. */
2816 hash = mapped_index_string_hash ((index->version == 4
2817 && case_sensitivity == case_sensitive_off
2818 ? 5 : index->version),
2819 name);
2820
2821 slot = hash & (index->symbol_table.size () - 1);
2822 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2823 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2824
2825 for (;;)
2826 {
2827 const char *str;
2828
2829 const auto &bucket = index->symbol_table[slot];
2830 if (bucket.name == 0 && bucket.vec == 0)
2831 return false;
2832
2833 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2834 if (!cmp (name, str))
2835 {
2836 *vec_out = (offset_type *) (index->constant_pool
2837 + MAYBE_SWAP (bucket.vec));
2838 return true;
2839 }
2840
2841 slot = (slot + step) & (index->symbol_table.size () - 1);
2842 }
2843 }
2844
2845 /* A helper function that reads the .gdb_index from BUFFER and fills
2846 in MAP. FILENAME is the name of the file containing the data;
2847 it is used for error reporting. DEPRECATED_OK is true if it is
2848 ok to use deprecated sections.
2849
2850 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2851 out parameters that are filled in with information about the CU and
2852 TU lists in the section.
2853
2854 Returns true if all went well, false otherwise. */
2855
2856 static bool
2857 read_gdb_index_from_buffer (const char *filename,
2858 bool deprecated_ok,
2859 gdb::array_view<const gdb_byte> buffer,
2860 struct mapped_index *map,
2861 const gdb_byte **cu_list,
2862 offset_type *cu_list_elements,
2863 const gdb_byte **types_list,
2864 offset_type *types_list_elements)
2865 {
2866 const gdb_byte *addr = &buffer[0];
2867
2868 /* Version check. */
2869 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2870 /* Versions earlier than 3 emitted every copy of a psymbol. This
2871 causes the index to behave very poorly for certain requests. Version 3
2872 contained incomplete addrmap. So, it seems better to just ignore such
2873 indices. */
2874 if (version < 4)
2875 {
2876 static int warning_printed = 0;
2877 if (!warning_printed)
2878 {
2879 warning (_("Skipping obsolete .gdb_index section in %s."),
2880 filename);
2881 warning_printed = 1;
2882 }
2883 return 0;
2884 }
2885 /* Index version 4 uses a different hash function than index version
2886 5 and later.
2887
2888 Versions earlier than 6 did not emit psymbols for inlined
2889 functions. Using these files will cause GDB not to be able to
2890 set breakpoints on inlined functions by name, so we ignore these
2891 indices unless the user has done
2892 "set use-deprecated-index-sections on". */
2893 if (version < 6 && !deprecated_ok)
2894 {
2895 static int warning_printed = 0;
2896 if (!warning_printed)
2897 {
2898 warning (_("\
2899 Skipping deprecated .gdb_index section in %s.\n\
2900 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2901 to use the section anyway."),
2902 filename);
2903 warning_printed = 1;
2904 }
2905 return 0;
2906 }
2907 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2908 of the TU (for symbols coming from TUs),
2909 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2910 Plus gold-generated indices can have duplicate entries for global symbols,
2911 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2912 These are just performance bugs, and we can't distinguish gdb-generated
2913 indices from gold-generated ones, so issue no warning here. */
2914
2915 /* Indexes with higher version than the one supported by GDB may be no
2916 longer backward compatible. */
2917 if (version > 8)
2918 return 0;
2919
2920 map->version = version;
2921
2922 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2923
2924 int i = 0;
2925 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2926 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2927 / 8);
2928 ++i;
2929
2930 *types_list = addr + MAYBE_SWAP (metadata[i]);
2931 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2932 - MAYBE_SWAP (metadata[i]))
2933 / 8);
2934 ++i;
2935
2936 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2937 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2938 map->address_table
2939 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2940 ++i;
2941
2942 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2943 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2944 map->symbol_table
2945 = gdb::array_view<mapped_index::symbol_table_slot>
2946 ((mapped_index::symbol_table_slot *) symbol_table,
2947 (mapped_index::symbol_table_slot *) symbol_table_end);
2948
2949 ++i;
2950 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2951
2952 return 1;
2953 }
2954
2955 /* Callback types for dwarf2_read_gdb_index. */
2956
2957 typedef gdb::function_view
2958 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2959 get_gdb_index_contents_ftype;
2960 typedef gdb::function_view
2961 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2962 get_gdb_index_contents_dwz_ftype;
2963
2964 /* Read .gdb_index. If everything went ok, initialize the "quick"
2965 elements of all the CUs and return 1. Otherwise, return 0. */
2966
2967 static int
2968 dwarf2_read_gdb_index
2969 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2970 get_gdb_index_contents_ftype get_gdb_index_contents,
2971 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2972 {
2973 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2974 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2975 struct dwz_file *dwz;
2976 struct objfile *objfile = dwarf2_per_objfile->objfile;
2977
2978 gdb::array_view<const gdb_byte> main_index_contents
2979 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
2980
2981 if (main_index_contents.empty ())
2982 return 0;
2983
2984 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2985 if (!read_gdb_index_from_buffer (objfile_name (objfile),
2986 use_deprecated_index_sections,
2987 main_index_contents, map.get (), &cu_list,
2988 &cu_list_elements, &types_list,
2989 &types_list_elements))
2990 return 0;
2991
2992 /* Don't use the index if it's empty. */
2993 if (map->symbol_table.empty ())
2994 return 0;
2995
2996 /* If there is a .dwz file, read it so we can get its CU list as
2997 well. */
2998 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2999 if (dwz != NULL)
3000 {
3001 struct mapped_index dwz_map;
3002 const gdb_byte *dwz_types_ignore;
3003 offset_type dwz_types_elements_ignore;
3004
3005 gdb::array_view<const gdb_byte> dwz_index_content
3006 = get_gdb_index_contents_dwz (objfile, dwz);
3007
3008 if (dwz_index_content.empty ())
3009 return 0;
3010
3011 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3012 1, dwz_index_content, &dwz_map,
3013 &dwz_list, &dwz_list_elements,
3014 &dwz_types_ignore,
3015 &dwz_types_elements_ignore))
3016 {
3017 warning (_("could not read '.gdb_index' section from %s; skipping"),
3018 bfd_get_filename (dwz->dwz_bfd.get ()));
3019 return 0;
3020 }
3021 }
3022
3023 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3024 dwz_list, dwz_list_elements);
3025
3026 if (types_list_elements)
3027 {
3028 /* We can only handle a single .debug_types when we have an
3029 index. */
3030 if (dwarf2_per_objfile->types.size () != 1)
3031 return 0;
3032
3033 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3034
3035 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3036 types_list, types_list_elements);
3037 }
3038
3039 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3040
3041 dwarf2_per_objfile->index_table = std::move (map);
3042 dwarf2_per_objfile->using_index = 1;
3043 dwarf2_per_objfile->quick_file_names_table =
3044 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3045
3046 return 1;
3047 }
3048
3049 /* die_reader_func for dw2_get_file_names. */
3050
3051 static void
3052 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3053 const gdb_byte *info_ptr,
3054 struct die_info *comp_unit_die)
3055 {
3056 struct dwarf2_cu *cu = reader->cu;
3057 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3058 struct dwarf2_per_objfile *dwarf2_per_objfile
3059 = cu->per_cu->dwarf2_per_objfile;
3060 struct objfile *objfile = dwarf2_per_objfile->objfile;
3061 struct dwarf2_per_cu_data *lh_cu;
3062 struct attribute *attr;
3063 void **slot;
3064 struct quick_file_names *qfn;
3065
3066 gdb_assert (! this_cu->is_debug_types);
3067
3068 /* Our callers never want to match partial units -- instead they
3069 will match the enclosing full CU. */
3070 if (comp_unit_die->tag == DW_TAG_partial_unit)
3071 {
3072 this_cu->v.quick->no_file_data = 1;
3073 return;
3074 }
3075
3076 lh_cu = this_cu;
3077 slot = NULL;
3078
3079 line_header_up lh;
3080 sect_offset line_offset {};
3081
3082 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3083 if (attr != nullptr)
3084 {
3085 struct quick_file_names find_entry;
3086
3087 line_offset = (sect_offset) DW_UNSND (attr);
3088
3089 /* We may have already read in this line header (TU line header sharing).
3090 If we have we're done. */
3091 find_entry.hash.dwo_unit = cu->dwo_unit;
3092 find_entry.hash.line_sect_off = line_offset;
3093 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3094 &find_entry, INSERT);
3095 if (*slot != NULL)
3096 {
3097 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3098 return;
3099 }
3100
3101 lh = dwarf_decode_line_header (line_offset, cu);
3102 }
3103 if (lh == NULL)
3104 {
3105 lh_cu->v.quick->no_file_data = 1;
3106 return;
3107 }
3108
3109 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3110 qfn->hash.dwo_unit = cu->dwo_unit;
3111 qfn->hash.line_sect_off = line_offset;
3112 gdb_assert (slot != NULL);
3113 *slot = qfn;
3114
3115 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3116
3117 int offset = 0;
3118 if (strcmp (fnd.name, "<unknown>") != 0)
3119 ++offset;
3120
3121 qfn->num_file_names = offset + lh->file_names_size ();
3122 qfn->file_names =
3123 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3124 if (offset != 0)
3125 qfn->file_names[0] = xstrdup (fnd.name);
3126 for (int i = 0; i < lh->file_names_size (); ++i)
3127 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3128 fnd.comp_dir).release ();
3129 qfn->real_names = NULL;
3130
3131 lh_cu->v.quick->file_names = qfn;
3132 }
3133
3134 /* A helper for the "quick" functions which attempts to read the line
3135 table for THIS_CU. */
3136
3137 static struct quick_file_names *
3138 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3139 {
3140 /* This should never be called for TUs. */
3141 gdb_assert (! this_cu->is_debug_types);
3142 /* Nor type unit groups. */
3143 gdb_assert (! this_cu->type_unit_group_p ());
3144
3145 if (this_cu->v.quick->file_names != NULL)
3146 return this_cu->v.quick->file_names;
3147 /* If we know there is no line data, no point in looking again. */
3148 if (this_cu->v.quick->no_file_data)
3149 return NULL;
3150
3151 cutu_reader reader (this_cu);
3152 if (!reader.dummy_p)
3153 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3154
3155 if (this_cu->v.quick->no_file_data)
3156 return NULL;
3157 return this_cu->v.quick->file_names;
3158 }
3159
3160 /* A helper for the "quick" functions which computes and caches the
3161 real path for a given file name from the line table. */
3162
3163 static const char *
3164 dw2_get_real_path (struct objfile *objfile,
3165 struct quick_file_names *qfn, int index)
3166 {
3167 if (qfn->real_names == NULL)
3168 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3169 qfn->num_file_names, const char *);
3170
3171 if (qfn->real_names[index] == NULL)
3172 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3173
3174 return qfn->real_names[index];
3175 }
3176
3177 static struct symtab *
3178 dw2_find_last_source_symtab (struct objfile *objfile)
3179 {
3180 struct dwarf2_per_objfile *dwarf2_per_objfile
3181 = get_dwarf2_per_objfile (objfile);
3182 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3183 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3184
3185 if (cust == NULL)
3186 return NULL;
3187
3188 return compunit_primary_filetab (cust);
3189 }
3190
3191 /* Traversal function for dw2_forget_cached_source_info. */
3192
3193 static int
3194 dw2_free_cached_file_names (void **slot, void *info)
3195 {
3196 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3197
3198 if (file_data->real_names)
3199 {
3200 int i;
3201
3202 for (i = 0; i < file_data->num_file_names; ++i)
3203 {
3204 xfree ((void*) file_data->real_names[i]);
3205 file_data->real_names[i] = NULL;
3206 }
3207 }
3208
3209 return 1;
3210 }
3211
3212 static void
3213 dw2_forget_cached_source_info (struct objfile *objfile)
3214 {
3215 struct dwarf2_per_objfile *dwarf2_per_objfile
3216 = get_dwarf2_per_objfile (objfile);
3217
3218 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3219 dw2_free_cached_file_names, NULL);
3220 }
3221
3222 /* Helper function for dw2_map_symtabs_matching_filename that expands
3223 the symtabs and calls the iterator. */
3224
3225 static int
3226 dw2_map_expand_apply (struct objfile *objfile,
3227 struct dwarf2_per_cu_data *per_cu,
3228 const char *name, const char *real_path,
3229 gdb::function_view<bool (symtab *)> callback)
3230 {
3231 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3232
3233 /* Don't visit already-expanded CUs. */
3234 if (per_cu->v.quick->compunit_symtab)
3235 return 0;
3236
3237 /* This may expand more than one symtab, and we want to iterate over
3238 all of them. */
3239 dw2_instantiate_symtab (per_cu, false);
3240
3241 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3242 last_made, callback);
3243 }
3244
3245 /* Implementation of the map_symtabs_matching_filename method. */
3246
3247 static bool
3248 dw2_map_symtabs_matching_filename
3249 (struct objfile *objfile, const char *name, const char *real_path,
3250 gdb::function_view<bool (symtab *)> callback)
3251 {
3252 const char *name_basename = lbasename (name);
3253 struct dwarf2_per_objfile *dwarf2_per_objfile
3254 = get_dwarf2_per_objfile (objfile);
3255
3256 /* The rule is CUs specify all the files, including those used by
3257 any TU, so there's no need to scan TUs here. */
3258
3259 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3260 {
3261 /* We only need to look at symtabs not already expanded. */
3262 if (per_cu->v.quick->compunit_symtab)
3263 continue;
3264
3265 quick_file_names *file_data = dw2_get_file_names (per_cu);
3266 if (file_data == NULL)
3267 continue;
3268
3269 for (int j = 0; j < file_data->num_file_names; ++j)
3270 {
3271 const char *this_name = file_data->file_names[j];
3272 const char *this_real_name;
3273
3274 if (compare_filenames_for_search (this_name, name))
3275 {
3276 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3277 callback))
3278 return true;
3279 continue;
3280 }
3281
3282 /* Before we invoke realpath, which can get expensive when many
3283 files are involved, do a quick comparison of the basenames. */
3284 if (! basenames_may_differ
3285 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3286 continue;
3287
3288 this_real_name = dw2_get_real_path (objfile, file_data, j);
3289 if (compare_filenames_for_search (this_real_name, name))
3290 {
3291 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3292 callback))
3293 return true;
3294 continue;
3295 }
3296
3297 if (real_path != NULL)
3298 {
3299 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3300 gdb_assert (IS_ABSOLUTE_PATH (name));
3301 if (this_real_name != NULL
3302 && FILENAME_CMP (real_path, this_real_name) == 0)
3303 {
3304 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3305 callback))
3306 return true;
3307 continue;
3308 }
3309 }
3310 }
3311 }
3312
3313 return false;
3314 }
3315
3316 /* Struct used to manage iterating over all CUs looking for a symbol. */
3317
3318 struct dw2_symtab_iterator
3319 {
3320 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3321 struct dwarf2_per_objfile *dwarf2_per_objfile;
3322 /* If set, only look for symbols that match that block. Valid values are
3323 GLOBAL_BLOCK and STATIC_BLOCK. */
3324 gdb::optional<block_enum> block_index;
3325 /* The kind of symbol we're looking for. */
3326 domain_enum domain;
3327 /* The list of CUs from the index entry of the symbol,
3328 or NULL if not found. */
3329 offset_type *vec;
3330 /* The next element in VEC to look at. */
3331 int next;
3332 /* The number of elements in VEC, or zero if there is no match. */
3333 int length;
3334 /* Have we seen a global version of the symbol?
3335 If so we can ignore all further global instances.
3336 This is to work around gold/15646, inefficient gold-generated
3337 indices. */
3338 int global_seen;
3339 };
3340
3341 /* Initialize the index symtab iterator ITER. */
3342
3343 static void
3344 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3345 struct dwarf2_per_objfile *dwarf2_per_objfile,
3346 gdb::optional<block_enum> block_index,
3347 domain_enum domain,
3348 const char *name)
3349 {
3350 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3351 iter->block_index = block_index;
3352 iter->domain = domain;
3353 iter->next = 0;
3354 iter->global_seen = 0;
3355
3356 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3357
3358 /* index is NULL if OBJF_READNOW. */
3359 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3360 iter->length = MAYBE_SWAP (*iter->vec);
3361 else
3362 {
3363 iter->vec = NULL;
3364 iter->length = 0;
3365 }
3366 }
3367
3368 /* Return the next matching CU or NULL if there are no more. */
3369
3370 static struct dwarf2_per_cu_data *
3371 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3372 {
3373 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3374
3375 for ( ; iter->next < iter->length; ++iter->next)
3376 {
3377 offset_type cu_index_and_attrs =
3378 MAYBE_SWAP (iter->vec[iter->next + 1]);
3379 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3380 gdb_index_symbol_kind symbol_kind =
3381 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3382 /* Only check the symbol attributes if they're present.
3383 Indices prior to version 7 don't record them,
3384 and indices >= 7 may elide them for certain symbols
3385 (gold does this). */
3386 int attrs_valid =
3387 (dwarf2_per_objfile->index_table->version >= 7
3388 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3389
3390 /* Don't crash on bad data. */
3391 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3392 + dwarf2_per_objfile->all_type_units.size ()))
3393 {
3394 complaint (_(".gdb_index entry has bad CU index"
3395 " [in module %s]"),
3396 objfile_name (dwarf2_per_objfile->objfile));
3397 continue;
3398 }
3399
3400 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3401
3402 /* Skip if already read in. */
3403 if (per_cu->v.quick->compunit_symtab)
3404 continue;
3405
3406 /* Check static vs global. */
3407 if (attrs_valid)
3408 {
3409 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3410
3411 if (iter->block_index.has_value ())
3412 {
3413 bool want_static = *iter->block_index == STATIC_BLOCK;
3414
3415 if (is_static != want_static)
3416 continue;
3417 }
3418
3419 /* Work around gold/15646. */
3420 if (!is_static && iter->global_seen)
3421 continue;
3422 if (!is_static)
3423 iter->global_seen = 1;
3424 }
3425
3426 /* Only check the symbol's kind if it has one. */
3427 if (attrs_valid)
3428 {
3429 switch (iter->domain)
3430 {
3431 case VAR_DOMAIN:
3432 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3433 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3434 /* Some types are also in VAR_DOMAIN. */
3435 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3436 continue;
3437 break;
3438 case STRUCT_DOMAIN:
3439 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3440 continue;
3441 break;
3442 case LABEL_DOMAIN:
3443 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3444 continue;
3445 break;
3446 case MODULE_DOMAIN:
3447 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3448 continue;
3449 break;
3450 default:
3451 break;
3452 }
3453 }
3454
3455 ++iter->next;
3456 return per_cu;
3457 }
3458
3459 return NULL;
3460 }
3461
3462 static struct compunit_symtab *
3463 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3464 const char *name, domain_enum domain)
3465 {
3466 struct compunit_symtab *stab_best = NULL;
3467 struct dwarf2_per_objfile *dwarf2_per_objfile
3468 = get_dwarf2_per_objfile (objfile);
3469
3470 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3471
3472 struct dw2_symtab_iterator iter;
3473 struct dwarf2_per_cu_data *per_cu;
3474
3475 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3476
3477 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3478 {
3479 struct symbol *sym, *with_opaque = NULL;
3480 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3481 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3482 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3483
3484 sym = block_find_symbol (block, name, domain,
3485 block_find_non_opaque_type_preferred,
3486 &with_opaque);
3487
3488 /* Some caution must be observed with overloaded functions
3489 and methods, since the index will not contain any overload
3490 information (but NAME might contain it). */
3491
3492 if (sym != NULL
3493 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3494 return stab;
3495 if (with_opaque != NULL
3496 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3497 stab_best = stab;
3498
3499 /* Keep looking through other CUs. */
3500 }
3501
3502 return stab_best;
3503 }
3504
3505 static void
3506 dw2_print_stats (struct objfile *objfile)
3507 {
3508 struct dwarf2_per_objfile *dwarf2_per_objfile
3509 = get_dwarf2_per_objfile (objfile);
3510 int total = (dwarf2_per_objfile->all_comp_units.size ()
3511 + dwarf2_per_objfile->all_type_units.size ());
3512 int count = 0;
3513
3514 for (int i = 0; i < total; ++i)
3515 {
3516 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3517
3518 if (!per_cu->v.quick->compunit_symtab)
3519 ++count;
3520 }
3521 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3522 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3523 }
3524
3525 /* This dumps minimal information about the index.
3526 It is called via "mt print objfiles".
3527 One use is to verify .gdb_index has been loaded by the
3528 gdb.dwarf2/gdb-index.exp testcase. */
3529
3530 static void
3531 dw2_dump (struct objfile *objfile)
3532 {
3533 struct dwarf2_per_objfile *dwarf2_per_objfile
3534 = get_dwarf2_per_objfile (objfile);
3535
3536 gdb_assert (dwarf2_per_objfile->using_index);
3537 printf_filtered (".gdb_index:");
3538 if (dwarf2_per_objfile->index_table != NULL)
3539 {
3540 printf_filtered (" version %d\n",
3541 dwarf2_per_objfile->index_table->version);
3542 }
3543 else
3544 printf_filtered (" faked for \"readnow\"\n");
3545 printf_filtered ("\n");
3546 }
3547
3548 static void
3549 dw2_expand_symtabs_for_function (struct objfile *objfile,
3550 const char *func_name)
3551 {
3552 struct dwarf2_per_objfile *dwarf2_per_objfile
3553 = get_dwarf2_per_objfile (objfile);
3554
3555 struct dw2_symtab_iterator iter;
3556 struct dwarf2_per_cu_data *per_cu;
3557
3558 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3559
3560 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3561 dw2_instantiate_symtab (per_cu, false);
3562
3563 }
3564
3565 static void
3566 dw2_expand_all_symtabs (struct objfile *objfile)
3567 {
3568 struct dwarf2_per_objfile *dwarf2_per_objfile
3569 = get_dwarf2_per_objfile (objfile);
3570 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3571 + dwarf2_per_objfile->all_type_units.size ());
3572
3573 for (int i = 0; i < total_units; ++i)
3574 {
3575 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3576
3577 /* We don't want to directly expand a partial CU, because if we
3578 read it with the wrong language, then assertion failures can
3579 be triggered later on. See PR symtab/23010. So, tell
3580 dw2_instantiate_symtab to skip partial CUs -- any important
3581 partial CU will be read via DW_TAG_imported_unit anyway. */
3582 dw2_instantiate_symtab (per_cu, true);
3583 }
3584 }
3585
3586 static void
3587 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3588 const char *fullname)
3589 {
3590 struct dwarf2_per_objfile *dwarf2_per_objfile
3591 = get_dwarf2_per_objfile (objfile);
3592
3593 /* We don't need to consider type units here.
3594 This is only called for examining code, e.g. expand_line_sal.
3595 There can be an order of magnitude (or more) more type units
3596 than comp units, and we avoid them if we can. */
3597
3598 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3599 {
3600 /* We only need to look at symtabs not already expanded. */
3601 if (per_cu->v.quick->compunit_symtab)
3602 continue;
3603
3604 quick_file_names *file_data = dw2_get_file_names (per_cu);
3605 if (file_data == NULL)
3606 continue;
3607
3608 for (int j = 0; j < file_data->num_file_names; ++j)
3609 {
3610 const char *this_fullname = file_data->file_names[j];
3611
3612 if (filename_cmp (this_fullname, fullname) == 0)
3613 {
3614 dw2_instantiate_symtab (per_cu, false);
3615 break;
3616 }
3617 }
3618 }
3619 }
3620
3621 static void
3622 dw2_map_matching_symbols
3623 (struct objfile *objfile,
3624 const lookup_name_info &name, domain_enum domain,
3625 int global,
3626 gdb::function_view<symbol_found_callback_ftype> callback,
3627 symbol_compare_ftype *ordered_compare)
3628 {
3629 /* Used for Ada. */
3630 struct dwarf2_per_objfile *dwarf2_per_objfile
3631 = get_dwarf2_per_objfile (objfile);
3632
3633 if (dwarf2_per_objfile->index_table != nullptr)
3634 {
3635 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3636 here though if the current language is Ada for a non-Ada objfile
3637 using GNU index. As Ada does not look for non-Ada symbols this
3638 function should just return. */
3639 return;
3640 }
3641
3642 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3643 inline psym_map_matching_symbols here, assuming all partial symtabs have
3644 been read in. */
3645 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3646
3647 for (compunit_symtab *cust : objfile->compunits ())
3648 {
3649 const struct block *block;
3650
3651 if (cust == NULL)
3652 continue;
3653 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3654 if (!iterate_over_symbols_terminated (block, name,
3655 domain, callback))
3656 return;
3657 }
3658 }
3659
3660 /* Starting from a search name, return the string that finds the upper
3661 bound of all strings that start with SEARCH_NAME in a sorted name
3662 list. Returns the empty string to indicate that the upper bound is
3663 the end of the list. */
3664
3665 static std::string
3666 make_sort_after_prefix_name (const char *search_name)
3667 {
3668 /* When looking to complete "func", we find the upper bound of all
3669 symbols that start with "func" by looking for where we'd insert
3670 the closest string that would follow "func" in lexicographical
3671 order. Usually, that's "func"-with-last-character-incremented,
3672 i.e. "fund". Mind non-ASCII characters, though. Usually those
3673 will be UTF-8 multi-byte sequences, but we can't be certain.
3674 Especially mind the 0xff character, which is a valid character in
3675 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3676 rule out compilers allowing it in identifiers. Note that
3677 conveniently, strcmp/strcasecmp are specified to compare
3678 characters interpreted as unsigned char. So what we do is treat
3679 the whole string as a base 256 number composed of a sequence of
3680 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3681 to 0, and carries 1 to the following more-significant position.
3682 If the very first character in SEARCH_NAME ends up incremented
3683 and carries/overflows, then the upper bound is the end of the
3684 list. The string after the empty string is also the empty
3685 string.
3686
3687 Some examples of this operation:
3688
3689 SEARCH_NAME => "+1" RESULT
3690
3691 "abc" => "abd"
3692 "ab\xff" => "ac"
3693 "\xff" "a" "\xff" => "\xff" "b"
3694 "\xff" => ""
3695 "\xff\xff" => ""
3696 "" => ""
3697
3698 Then, with these symbols for example:
3699
3700 func
3701 func1
3702 fund
3703
3704 completing "func" looks for symbols between "func" and
3705 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3706 which finds "func" and "func1", but not "fund".
3707
3708 And with:
3709
3710 funcÿ (Latin1 'ÿ' [0xff])
3711 funcÿ1
3712 fund
3713
3714 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3715 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3716
3717 And with:
3718
3719 ÿÿ (Latin1 'ÿ' [0xff])
3720 ÿÿ1
3721
3722 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3723 the end of the list.
3724 */
3725 std::string after = search_name;
3726 while (!after.empty () && (unsigned char) after.back () == 0xff)
3727 after.pop_back ();
3728 if (!after.empty ())
3729 after.back () = (unsigned char) after.back () + 1;
3730 return after;
3731 }
3732
3733 /* See declaration. */
3734
3735 std::pair<std::vector<name_component>::const_iterator,
3736 std::vector<name_component>::const_iterator>
3737 mapped_index_base::find_name_components_bounds
3738 (const lookup_name_info &lookup_name_without_params, language lang) const
3739 {
3740 auto *name_cmp
3741 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3742
3743 const char *lang_name
3744 = lookup_name_without_params.language_lookup_name (lang);
3745
3746 /* Comparison function object for lower_bound that matches against a
3747 given symbol name. */
3748 auto lookup_compare_lower = [&] (const name_component &elem,
3749 const char *name)
3750 {
3751 const char *elem_qualified = this->symbol_name_at (elem.idx);
3752 const char *elem_name = elem_qualified + elem.name_offset;
3753 return name_cmp (elem_name, name) < 0;
3754 };
3755
3756 /* Comparison function object for upper_bound that matches against a
3757 given symbol name. */
3758 auto lookup_compare_upper = [&] (const char *name,
3759 const name_component &elem)
3760 {
3761 const char *elem_qualified = this->symbol_name_at (elem.idx);
3762 const char *elem_name = elem_qualified + elem.name_offset;
3763 return name_cmp (name, elem_name) < 0;
3764 };
3765
3766 auto begin = this->name_components.begin ();
3767 auto end = this->name_components.end ();
3768
3769 /* Find the lower bound. */
3770 auto lower = [&] ()
3771 {
3772 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3773 return begin;
3774 else
3775 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3776 } ();
3777
3778 /* Find the upper bound. */
3779 auto upper = [&] ()
3780 {
3781 if (lookup_name_without_params.completion_mode ())
3782 {
3783 /* In completion mode, we want UPPER to point past all
3784 symbols names that have the same prefix. I.e., with
3785 these symbols, and completing "func":
3786
3787 function << lower bound
3788 function1
3789 other_function << upper bound
3790
3791 We find the upper bound by looking for the insertion
3792 point of "func"-with-last-character-incremented,
3793 i.e. "fund". */
3794 std::string after = make_sort_after_prefix_name (lang_name);
3795 if (after.empty ())
3796 return end;
3797 return std::lower_bound (lower, end, after.c_str (),
3798 lookup_compare_lower);
3799 }
3800 else
3801 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3802 } ();
3803
3804 return {lower, upper};
3805 }
3806
3807 /* See declaration. */
3808
3809 void
3810 mapped_index_base::build_name_components ()
3811 {
3812 if (!this->name_components.empty ())
3813 return;
3814
3815 this->name_components_casing = case_sensitivity;
3816 auto *name_cmp
3817 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3818
3819 /* The code below only knows how to break apart components of C++
3820 symbol names (and other languages that use '::' as
3821 namespace/module separator) and Ada symbol names. */
3822 auto count = this->symbol_name_count ();
3823 for (offset_type idx = 0; idx < count; idx++)
3824 {
3825 if (this->symbol_name_slot_invalid (idx))
3826 continue;
3827
3828 const char *name = this->symbol_name_at (idx);
3829
3830 /* Add each name component to the name component table. */
3831 unsigned int previous_len = 0;
3832
3833 if (strstr (name, "::") != nullptr)
3834 {
3835 for (unsigned int current_len = cp_find_first_component (name);
3836 name[current_len] != '\0';
3837 current_len += cp_find_first_component (name + current_len))
3838 {
3839 gdb_assert (name[current_len] == ':');
3840 this->name_components.push_back ({previous_len, idx});
3841 /* Skip the '::'. */
3842 current_len += 2;
3843 previous_len = current_len;
3844 }
3845 }
3846 else
3847 {
3848 /* Handle the Ada encoded (aka mangled) form here. */
3849 for (const char *iter = strstr (name, "__");
3850 iter != nullptr;
3851 iter = strstr (iter, "__"))
3852 {
3853 this->name_components.push_back ({previous_len, idx});
3854 iter += 2;
3855 previous_len = iter - name;
3856 }
3857 }
3858
3859 this->name_components.push_back ({previous_len, idx});
3860 }
3861
3862 /* Sort name_components elements by name. */
3863 auto name_comp_compare = [&] (const name_component &left,
3864 const name_component &right)
3865 {
3866 const char *left_qualified = this->symbol_name_at (left.idx);
3867 const char *right_qualified = this->symbol_name_at (right.idx);
3868
3869 const char *left_name = left_qualified + left.name_offset;
3870 const char *right_name = right_qualified + right.name_offset;
3871
3872 return name_cmp (left_name, right_name) < 0;
3873 };
3874
3875 std::sort (this->name_components.begin (),
3876 this->name_components.end (),
3877 name_comp_compare);
3878 }
3879
3880 /* Helper for dw2_expand_symtabs_matching that works with a
3881 mapped_index_base instead of the containing objfile. This is split
3882 to a separate function in order to be able to unit test the
3883 name_components matching using a mock mapped_index_base. For each
3884 symbol name that matches, calls MATCH_CALLBACK, passing it the
3885 symbol's index in the mapped_index_base symbol table. */
3886
3887 static void
3888 dw2_expand_symtabs_matching_symbol
3889 (mapped_index_base &index,
3890 const lookup_name_info &lookup_name_in,
3891 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3892 enum search_domain kind,
3893 gdb::function_view<bool (offset_type)> match_callback)
3894 {
3895 lookup_name_info lookup_name_without_params
3896 = lookup_name_in.make_ignore_params ();
3897
3898 /* Build the symbol name component sorted vector, if we haven't
3899 yet. */
3900 index.build_name_components ();
3901
3902 /* The same symbol may appear more than once in the range though.
3903 E.g., if we're looking for symbols that complete "w", and we have
3904 a symbol named "w1::w2", we'll find the two name components for
3905 that same symbol in the range. To be sure we only call the
3906 callback once per symbol, we first collect the symbol name
3907 indexes that matched in a temporary vector and ignore
3908 duplicates. */
3909 std::vector<offset_type> matches;
3910
3911 struct name_and_matcher
3912 {
3913 symbol_name_matcher_ftype *matcher;
3914 const std::string &name;
3915
3916 bool operator== (const name_and_matcher &other) const
3917 {
3918 return matcher == other.matcher && name == other.name;
3919 }
3920 };
3921
3922 /* A vector holding all the different symbol name matchers, for all
3923 languages. */
3924 std::vector<name_and_matcher> matchers;
3925
3926 for (int i = 0; i < nr_languages; i++)
3927 {
3928 enum language lang_e = (enum language) i;
3929
3930 const language_defn *lang = language_def (lang_e);
3931 symbol_name_matcher_ftype *name_matcher
3932 = get_symbol_name_matcher (lang, lookup_name_without_params);
3933
3934 name_and_matcher key {
3935 name_matcher,
3936 lookup_name_without_params.language_lookup_name (lang_e)
3937 };
3938
3939 /* Don't insert the same comparison routine more than once.
3940 Note that we do this linear walk. This is not a problem in
3941 practice because the number of supported languages is
3942 low. */
3943 if (std::find (matchers.begin (), matchers.end (), key)
3944 != matchers.end ())
3945 continue;
3946 matchers.push_back (std::move (key));
3947
3948 auto bounds
3949 = index.find_name_components_bounds (lookup_name_without_params,
3950 lang_e);
3951
3952 /* Now for each symbol name in range, check to see if we have a name
3953 match, and if so, call the MATCH_CALLBACK callback. */
3954
3955 for (; bounds.first != bounds.second; ++bounds.first)
3956 {
3957 const char *qualified = index.symbol_name_at (bounds.first->idx);
3958
3959 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3960 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3961 continue;
3962
3963 matches.push_back (bounds.first->idx);
3964 }
3965 }
3966
3967 std::sort (matches.begin (), matches.end ());
3968
3969 /* Finally call the callback, once per match. */
3970 ULONGEST prev = -1;
3971 for (offset_type idx : matches)
3972 {
3973 if (prev != idx)
3974 {
3975 if (!match_callback (idx))
3976 break;
3977 prev = idx;
3978 }
3979 }
3980
3981 /* Above we use a type wider than idx's for 'prev', since 0 and
3982 (offset_type)-1 are both possible values. */
3983 static_assert (sizeof (prev) > sizeof (offset_type), "");
3984 }
3985
3986 #if GDB_SELF_TEST
3987
3988 namespace selftests { namespace dw2_expand_symtabs_matching {
3989
3990 /* A mock .gdb_index/.debug_names-like name index table, enough to
3991 exercise dw2_expand_symtabs_matching_symbol, which works with the
3992 mapped_index_base interface. Builds an index from the symbol list
3993 passed as parameter to the constructor. */
3994 class mock_mapped_index : public mapped_index_base
3995 {
3996 public:
3997 mock_mapped_index (gdb::array_view<const char *> symbols)
3998 : m_symbol_table (symbols)
3999 {}
4000
4001 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4002
4003 /* Return the number of names in the symbol table. */
4004 size_t symbol_name_count () const override
4005 {
4006 return m_symbol_table.size ();
4007 }
4008
4009 /* Get the name of the symbol at IDX in the symbol table. */
4010 const char *symbol_name_at (offset_type idx) const override
4011 {
4012 return m_symbol_table[idx];
4013 }
4014
4015 private:
4016 gdb::array_view<const char *> m_symbol_table;
4017 };
4018
4019 /* Convenience function that converts a NULL pointer to a "<null>"
4020 string, to pass to print routines. */
4021
4022 static const char *
4023 string_or_null (const char *str)
4024 {
4025 return str != NULL ? str : "<null>";
4026 }
4027
4028 /* Check if a lookup_name_info built from
4029 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4030 index. EXPECTED_LIST is the list of expected matches, in expected
4031 matching order. If no match expected, then an empty list is
4032 specified. Returns true on success. On failure prints a warning
4033 indicating the file:line that failed, and returns false. */
4034
4035 static bool
4036 check_match (const char *file, int line,
4037 mock_mapped_index &mock_index,
4038 const char *name, symbol_name_match_type match_type,
4039 bool completion_mode,
4040 std::initializer_list<const char *> expected_list)
4041 {
4042 lookup_name_info lookup_name (name, match_type, completion_mode);
4043
4044 bool matched = true;
4045
4046 auto mismatch = [&] (const char *expected_str,
4047 const char *got)
4048 {
4049 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4050 "expected=\"%s\", got=\"%s\"\n"),
4051 file, line,
4052 (match_type == symbol_name_match_type::FULL
4053 ? "FULL" : "WILD"),
4054 name, string_or_null (expected_str), string_or_null (got));
4055 matched = false;
4056 };
4057
4058 auto expected_it = expected_list.begin ();
4059 auto expected_end = expected_list.end ();
4060
4061 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4062 NULL, ALL_DOMAIN,
4063 [&] (offset_type idx)
4064 {
4065 const char *matched_name = mock_index.symbol_name_at (idx);
4066 const char *expected_str
4067 = expected_it == expected_end ? NULL : *expected_it++;
4068
4069 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4070 mismatch (expected_str, matched_name);
4071 return true;
4072 });
4073
4074 const char *expected_str
4075 = expected_it == expected_end ? NULL : *expected_it++;
4076 if (expected_str != NULL)
4077 mismatch (expected_str, NULL);
4078
4079 return matched;
4080 }
4081
4082 /* The symbols added to the mock mapped_index for testing (in
4083 canonical form). */
4084 static const char *test_symbols[] = {
4085 "function",
4086 "std::bar",
4087 "std::zfunction",
4088 "std::zfunction2",
4089 "w1::w2",
4090 "ns::foo<char*>",
4091 "ns::foo<int>",
4092 "ns::foo<long>",
4093 "ns2::tmpl<int>::foo2",
4094 "(anonymous namespace)::A::B::C",
4095
4096 /* These are used to check that the increment-last-char in the
4097 matching algorithm for completion doesn't match "t1_fund" when
4098 completing "t1_func". */
4099 "t1_func",
4100 "t1_func1",
4101 "t1_fund",
4102 "t1_fund1",
4103
4104 /* A UTF-8 name with multi-byte sequences to make sure that
4105 cp-name-parser understands this as a single identifier ("função"
4106 is "function" in PT). */
4107 u8"u8função",
4108
4109 /* \377 (0xff) is Latin1 'ÿ'. */
4110 "yfunc\377",
4111
4112 /* \377 (0xff) is Latin1 'ÿ'. */
4113 "\377",
4114 "\377\377123",
4115
4116 /* A name with all sorts of complications. Starts with "z" to make
4117 it easier for the completion tests below. */
4118 #define Z_SYM_NAME \
4119 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4120 "::tuple<(anonymous namespace)::ui*, " \
4121 "std::default_delete<(anonymous namespace)::ui>, void>"
4122
4123 Z_SYM_NAME
4124 };
4125
4126 /* Returns true if the mapped_index_base::find_name_component_bounds
4127 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4128 in completion mode. */
4129
4130 static bool
4131 check_find_bounds_finds (mapped_index_base &index,
4132 const char *search_name,
4133 gdb::array_view<const char *> expected_syms)
4134 {
4135 lookup_name_info lookup_name (search_name,
4136 symbol_name_match_type::FULL, true);
4137
4138 auto bounds = index.find_name_components_bounds (lookup_name,
4139 language_cplus);
4140
4141 size_t distance = std::distance (bounds.first, bounds.second);
4142 if (distance != expected_syms.size ())
4143 return false;
4144
4145 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4146 {
4147 auto nc_elem = bounds.first + exp_elem;
4148 const char *qualified = index.symbol_name_at (nc_elem->idx);
4149 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4150 return false;
4151 }
4152
4153 return true;
4154 }
4155
4156 /* Test the lower-level mapped_index::find_name_component_bounds
4157 method. */
4158
4159 static void
4160 test_mapped_index_find_name_component_bounds ()
4161 {
4162 mock_mapped_index mock_index (test_symbols);
4163
4164 mock_index.build_name_components ();
4165
4166 /* Test the lower-level mapped_index::find_name_component_bounds
4167 method in completion mode. */
4168 {
4169 static const char *expected_syms[] = {
4170 "t1_func",
4171 "t1_func1",
4172 };
4173
4174 SELF_CHECK (check_find_bounds_finds (mock_index,
4175 "t1_func", expected_syms));
4176 }
4177
4178 /* Check that the increment-last-char in the name matching algorithm
4179 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4180 {
4181 static const char *expected_syms1[] = {
4182 "\377",
4183 "\377\377123",
4184 };
4185 SELF_CHECK (check_find_bounds_finds (mock_index,
4186 "\377", expected_syms1));
4187
4188 static const char *expected_syms2[] = {
4189 "\377\377123",
4190 };
4191 SELF_CHECK (check_find_bounds_finds (mock_index,
4192 "\377\377", expected_syms2));
4193 }
4194 }
4195
4196 /* Test dw2_expand_symtabs_matching_symbol. */
4197
4198 static void
4199 test_dw2_expand_symtabs_matching_symbol ()
4200 {
4201 mock_mapped_index mock_index (test_symbols);
4202
4203 /* We let all tests run until the end even if some fails, for debug
4204 convenience. */
4205 bool any_mismatch = false;
4206
4207 /* Create the expected symbols list (an initializer_list). Needed
4208 because lists have commas, and we need to pass them to CHECK,
4209 which is a macro. */
4210 #define EXPECT(...) { __VA_ARGS__ }
4211
4212 /* Wrapper for check_match that passes down the current
4213 __FILE__/__LINE__. */
4214 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4215 any_mismatch |= !check_match (__FILE__, __LINE__, \
4216 mock_index, \
4217 NAME, MATCH_TYPE, COMPLETION_MODE, \
4218 EXPECTED_LIST)
4219
4220 /* Identity checks. */
4221 for (const char *sym : test_symbols)
4222 {
4223 /* Should be able to match all existing symbols. */
4224 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4225 EXPECT (sym));
4226
4227 /* Should be able to match all existing symbols with
4228 parameters. */
4229 std::string with_params = std::string (sym) + "(int)";
4230 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4231 EXPECT (sym));
4232
4233 /* Should be able to match all existing symbols with
4234 parameters and qualifiers. */
4235 with_params = std::string (sym) + " ( int ) const";
4236 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4237 EXPECT (sym));
4238
4239 /* This should really find sym, but cp-name-parser.y doesn't
4240 know about lvalue/rvalue qualifiers yet. */
4241 with_params = std::string (sym) + " ( int ) &&";
4242 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4243 {});
4244 }
4245
4246 /* Check that the name matching algorithm for completion doesn't get
4247 confused with Latin1 'ÿ' / 0xff. */
4248 {
4249 static const char str[] = "\377";
4250 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4251 EXPECT ("\377", "\377\377123"));
4252 }
4253
4254 /* Check that the increment-last-char in the matching algorithm for
4255 completion doesn't match "t1_fund" when completing "t1_func". */
4256 {
4257 static const char str[] = "t1_func";
4258 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4259 EXPECT ("t1_func", "t1_func1"));
4260 }
4261
4262 /* Check that completion mode works at each prefix of the expected
4263 symbol name. */
4264 {
4265 static const char str[] = "function(int)";
4266 size_t len = strlen (str);
4267 std::string lookup;
4268
4269 for (size_t i = 1; i < len; i++)
4270 {
4271 lookup.assign (str, i);
4272 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4273 EXPECT ("function"));
4274 }
4275 }
4276
4277 /* While "w" is a prefix of both components, the match function
4278 should still only be called once. */
4279 {
4280 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4281 EXPECT ("w1::w2"));
4282 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4283 EXPECT ("w1::w2"));
4284 }
4285
4286 /* Same, with a "complicated" symbol. */
4287 {
4288 static const char str[] = Z_SYM_NAME;
4289 size_t len = strlen (str);
4290 std::string lookup;
4291
4292 for (size_t i = 1; i < len; i++)
4293 {
4294 lookup.assign (str, i);
4295 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4296 EXPECT (Z_SYM_NAME));
4297 }
4298 }
4299
4300 /* In FULL mode, an incomplete symbol doesn't match. */
4301 {
4302 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4303 {});
4304 }
4305
4306 /* A complete symbol with parameters matches any overload, since the
4307 index has no overload info. */
4308 {
4309 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4310 EXPECT ("std::zfunction", "std::zfunction2"));
4311 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4312 EXPECT ("std::zfunction", "std::zfunction2"));
4313 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4314 EXPECT ("std::zfunction", "std::zfunction2"));
4315 }
4316
4317 /* Check that whitespace is ignored appropriately. A symbol with a
4318 template argument list. */
4319 {
4320 static const char expected[] = "ns::foo<int>";
4321 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4322 EXPECT (expected));
4323 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4324 EXPECT (expected));
4325 }
4326
4327 /* Check that whitespace is ignored appropriately. A symbol with a
4328 template argument list that includes a pointer. */
4329 {
4330 static const char expected[] = "ns::foo<char*>";
4331 /* Try both completion and non-completion modes. */
4332 static const bool completion_mode[2] = {false, true};
4333 for (size_t i = 0; i < 2; i++)
4334 {
4335 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4336 completion_mode[i], EXPECT (expected));
4337 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4338 completion_mode[i], EXPECT (expected));
4339
4340 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4341 completion_mode[i], EXPECT (expected));
4342 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4343 completion_mode[i], EXPECT (expected));
4344 }
4345 }
4346
4347 {
4348 /* Check method qualifiers are ignored. */
4349 static const char expected[] = "ns::foo<char*>";
4350 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4351 symbol_name_match_type::FULL, true, EXPECT (expected));
4352 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4353 symbol_name_match_type::FULL, true, EXPECT (expected));
4354 CHECK_MATCH ("foo < char * > ( int ) const",
4355 symbol_name_match_type::WILD, true, EXPECT (expected));
4356 CHECK_MATCH ("foo < char * > ( int ) &&",
4357 symbol_name_match_type::WILD, true, EXPECT (expected));
4358 }
4359
4360 /* Test lookup names that don't match anything. */
4361 {
4362 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4363 {});
4364
4365 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4366 {});
4367 }
4368
4369 /* Some wild matching tests, exercising "(anonymous namespace)",
4370 which should not be confused with a parameter list. */
4371 {
4372 static const char *syms[] = {
4373 "A::B::C",
4374 "B::C",
4375 "C",
4376 "A :: B :: C ( int )",
4377 "B :: C ( int )",
4378 "C ( int )",
4379 };
4380
4381 for (const char *s : syms)
4382 {
4383 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4384 EXPECT ("(anonymous namespace)::A::B::C"));
4385 }
4386 }
4387
4388 {
4389 static const char expected[] = "ns2::tmpl<int>::foo2";
4390 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4391 EXPECT (expected));
4392 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4393 EXPECT (expected));
4394 }
4395
4396 SELF_CHECK (!any_mismatch);
4397
4398 #undef EXPECT
4399 #undef CHECK_MATCH
4400 }
4401
4402 static void
4403 run_test ()
4404 {
4405 test_mapped_index_find_name_component_bounds ();
4406 test_dw2_expand_symtabs_matching_symbol ();
4407 }
4408
4409 }} // namespace selftests::dw2_expand_symtabs_matching
4410
4411 #endif /* GDB_SELF_TEST */
4412
4413 /* If FILE_MATCHER is NULL or if PER_CU has
4414 dwarf2_per_cu_quick_data::MARK set (see
4415 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4416 EXPANSION_NOTIFY on it. */
4417
4418 static void
4419 dw2_expand_symtabs_matching_one
4420 (struct dwarf2_per_cu_data *per_cu,
4421 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4422 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4423 {
4424 if (file_matcher == NULL || per_cu->v.quick->mark)
4425 {
4426 bool symtab_was_null
4427 = (per_cu->v.quick->compunit_symtab == NULL);
4428
4429 dw2_instantiate_symtab (per_cu, false);
4430
4431 if (expansion_notify != NULL
4432 && symtab_was_null
4433 && per_cu->v.quick->compunit_symtab != NULL)
4434 expansion_notify (per_cu->v.quick->compunit_symtab);
4435 }
4436 }
4437
4438 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4439 matched, to expand corresponding CUs that were marked. IDX is the
4440 index of the symbol name that matched. */
4441
4442 static void
4443 dw2_expand_marked_cus
4444 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4445 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4446 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4447 search_domain kind)
4448 {
4449 offset_type *vec, vec_len, vec_idx;
4450 bool global_seen = false;
4451 mapped_index &index = *dwarf2_per_objfile->index_table;
4452
4453 vec = (offset_type *) (index.constant_pool
4454 + MAYBE_SWAP (index.symbol_table[idx].vec));
4455 vec_len = MAYBE_SWAP (vec[0]);
4456 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4457 {
4458 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4459 /* This value is only valid for index versions >= 7. */
4460 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4461 gdb_index_symbol_kind symbol_kind =
4462 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4463 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4464 /* Only check the symbol attributes if they're present.
4465 Indices prior to version 7 don't record them,
4466 and indices >= 7 may elide them for certain symbols
4467 (gold does this). */
4468 int attrs_valid =
4469 (index.version >= 7
4470 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4471
4472 /* Work around gold/15646. */
4473 if (attrs_valid)
4474 {
4475 if (!is_static && global_seen)
4476 continue;
4477 if (!is_static)
4478 global_seen = true;
4479 }
4480
4481 /* Only check the symbol's kind if it has one. */
4482 if (attrs_valid)
4483 {
4484 switch (kind)
4485 {
4486 case VARIABLES_DOMAIN:
4487 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4488 continue;
4489 break;
4490 case FUNCTIONS_DOMAIN:
4491 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4492 continue;
4493 break;
4494 case TYPES_DOMAIN:
4495 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4496 continue;
4497 break;
4498 case MODULES_DOMAIN:
4499 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4500 continue;
4501 break;
4502 default:
4503 break;
4504 }
4505 }
4506
4507 /* Don't crash on bad data. */
4508 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4509 + dwarf2_per_objfile->all_type_units.size ()))
4510 {
4511 complaint (_(".gdb_index entry has bad CU index"
4512 " [in module %s]"),
4513 objfile_name (dwarf2_per_objfile->objfile));
4514 continue;
4515 }
4516
4517 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4518 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4519 expansion_notify);
4520 }
4521 }
4522
4523 /* If FILE_MATCHER is non-NULL, set all the
4524 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4525 that match FILE_MATCHER. */
4526
4527 static void
4528 dw_expand_symtabs_matching_file_matcher
4529 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4530 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4531 {
4532 if (file_matcher == NULL)
4533 return;
4534
4535 objfile *const objfile = dwarf2_per_objfile->objfile;
4536
4537 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4538 htab_eq_pointer,
4539 NULL, xcalloc, xfree));
4540 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4541 htab_eq_pointer,
4542 NULL, xcalloc, xfree));
4543
4544 /* The rule is CUs specify all the files, including those used by
4545 any TU, so there's no need to scan TUs here. */
4546
4547 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4548 {
4549 QUIT;
4550
4551 per_cu->v.quick->mark = 0;
4552
4553 /* We only need to look at symtabs not already expanded. */
4554 if (per_cu->v.quick->compunit_symtab)
4555 continue;
4556
4557 quick_file_names *file_data = dw2_get_file_names (per_cu);
4558 if (file_data == NULL)
4559 continue;
4560
4561 if (htab_find (visited_not_found.get (), file_data) != NULL)
4562 continue;
4563 else if (htab_find (visited_found.get (), file_data) != NULL)
4564 {
4565 per_cu->v.quick->mark = 1;
4566 continue;
4567 }
4568
4569 for (int j = 0; j < file_data->num_file_names; ++j)
4570 {
4571 const char *this_real_name;
4572
4573 if (file_matcher (file_data->file_names[j], false))
4574 {
4575 per_cu->v.quick->mark = 1;
4576 break;
4577 }
4578
4579 /* Before we invoke realpath, which can get expensive when many
4580 files are involved, do a quick comparison of the basenames. */
4581 if (!basenames_may_differ
4582 && !file_matcher (lbasename (file_data->file_names[j]),
4583 true))
4584 continue;
4585
4586 this_real_name = dw2_get_real_path (objfile, file_data, j);
4587 if (file_matcher (this_real_name, false))
4588 {
4589 per_cu->v.quick->mark = 1;
4590 break;
4591 }
4592 }
4593
4594 void **slot = htab_find_slot (per_cu->v.quick->mark
4595 ? visited_found.get ()
4596 : visited_not_found.get (),
4597 file_data, INSERT);
4598 *slot = file_data;
4599 }
4600 }
4601
4602 static void
4603 dw2_expand_symtabs_matching
4604 (struct objfile *objfile,
4605 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4606 const lookup_name_info *lookup_name,
4607 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4608 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4609 enum search_domain kind)
4610 {
4611 struct dwarf2_per_objfile *dwarf2_per_objfile
4612 = get_dwarf2_per_objfile (objfile);
4613
4614 /* index_table is NULL if OBJF_READNOW. */
4615 if (!dwarf2_per_objfile->index_table)
4616 return;
4617
4618 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4619
4620 if (symbol_matcher == NULL && lookup_name == NULL)
4621 {
4622 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4623 {
4624 QUIT;
4625
4626 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4627 expansion_notify);
4628 }
4629 return;
4630 }
4631
4632 mapped_index &index = *dwarf2_per_objfile->index_table;
4633
4634 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4635 symbol_matcher,
4636 kind, [&] (offset_type idx)
4637 {
4638 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4639 expansion_notify, kind);
4640 return true;
4641 });
4642 }
4643
4644 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4645 symtab. */
4646
4647 static struct compunit_symtab *
4648 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4649 CORE_ADDR pc)
4650 {
4651 int i;
4652
4653 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4654 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4655 return cust;
4656
4657 if (cust->includes == NULL)
4658 return NULL;
4659
4660 for (i = 0; cust->includes[i]; ++i)
4661 {
4662 struct compunit_symtab *s = cust->includes[i];
4663
4664 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4665 if (s != NULL)
4666 return s;
4667 }
4668
4669 return NULL;
4670 }
4671
4672 static struct compunit_symtab *
4673 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4674 struct bound_minimal_symbol msymbol,
4675 CORE_ADDR pc,
4676 struct obj_section *section,
4677 int warn_if_readin)
4678 {
4679 struct dwarf2_per_cu_data *data;
4680 struct compunit_symtab *result;
4681
4682 if (!objfile->partial_symtabs->psymtabs_addrmap)
4683 return NULL;
4684
4685 CORE_ADDR baseaddr = objfile->text_section_offset ();
4686 data = (struct dwarf2_per_cu_data *) addrmap_find
4687 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4688 if (!data)
4689 return NULL;
4690
4691 if (warn_if_readin && data->v.quick->compunit_symtab)
4692 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4693 paddress (get_objfile_arch (objfile), pc));
4694
4695 result
4696 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4697 false),
4698 pc);
4699 gdb_assert (result != NULL);
4700 return result;
4701 }
4702
4703 static void
4704 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4705 void *data, int need_fullname)
4706 {
4707 struct dwarf2_per_objfile *dwarf2_per_objfile
4708 = get_dwarf2_per_objfile (objfile);
4709
4710 if (!dwarf2_per_objfile->filenames_cache)
4711 {
4712 dwarf2_per_objfile->filenames_cache.emplace ();
4713
4714 htab_up visited (htab_create_alloc (10,
4715 htab_hash_pointer, htab_eq_pointer,
4716 NULL, xcalloc, xfree));
4717
4718 /* The rule is CUs specify all the files, including those used
4719 by any TU, so there's no need to scan TUs here. We can
4720 ignore file names coming from already-expanded CUs. */
4721
4722 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4723 {
4724 if (per_cu->v.quick->compunit_symtab)
4725 {
4726 void **slot = htab_find_slot (visited.get (),
4727 per_cu->v.quick->file_names,
4728 INSERT);
4729
4730 *slot = per_cu->v.quick->file_names;
4731 }
4732 }
4733
4734 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4735 {
4736 /* We only need to look at symtabs not already expanded. */
4737 if (per_cu->v.quick->compunit_symtab)
4738 continue;
4739
4740 quick_file_names *file_data = dw2_get_file_names (per_cu);
4741 if (file_data == NULL)
4742 continue;
4743
4744 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4745 if (*slot)
4746 {
4747 /* Already visited. */
4748 continue;
4749 }
4750 *slot = file_data;
4751
4752 for (int j = 0; j < file_data->num_file_names; ++j)
4753 {
4754 const char *filename = file_data->file_names[j];
4755 dwarf2_per_objfile->filenames_cache->seen (filename);
4756 }
4757 }
4758 }
4759
4760 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4761 {
4762 gdb::unique_xmalloc_ptr<char> this_real_name;
4763
4764 if (need_fullname)
4765 this_real_name = gdb_realpath (filename);
4766 (*fun) (filename, this_real_name.get (), data);
4767 });
4768 }
4769
4770 static int
4771 dw2_has_symbols (struct objfile *objfile)
4772 {
4773 return 1;
4774 }
4775
4776 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4777 {
4778 dw2_has_symbols,
4779 dw2_find_last_source_symtab,
4780 dw2_forget_cached_source_info,
4781 dw2_map_symtabs_matching_filename,
4782 dw2_lookup_symbol,
4783 NULL,
4784 dw2_print_stats,
4785 dw2_dump,
4786 dw2_expand_symtabs_for_function,
4787 dw2_expand_all_symtabs,
4788 dw2_expand_symtabs_with_fullname,
4789 dw2_map_matching_symbols,
4790 dw2_expand_symtabs_matching,
4791 dw2_find_pc_sect_compunit_symtab,
4792 NULL,
4793 dw2_map_symbol_filenames
4794 };
4795
4796 /* DWARF-5 debug_names reader. */
4797
4798 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4799 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4800
4801 /* A helper function that reads the .debug_names section in SECTION
4802 and fills in MAP. FILENAME is the name of the file containing the
4803 section; it is used for error reporting.
4804
4805 Returns true if all went well, false otherwise. */
4806
4807 static bool
4808 read_debug_names_from_section (struct objfile *objfile,
4809 const char *filename,
4810 struct dwarf2_section_info *section,
4811 mapped_debug_names &map)
4812 {
4813 if (section->empty ())
4814 return false;
4815
4816 /* Older elfutils strip versions could keep the section in the main
4817 executable while splitting it for the separate debug info file. */
4818 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4819 return false;
4820
4821 section->read (objfile);
4822
4823 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4824
4825 const gdb_byte *addr = section->buffer;
4826
4827 bfd *const abfd = section->get_bfd_owner ();
4828
4829 unsigned int bytes_read;
4830 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4831 addr += bytes_read;
4832
4833 map.dwarf5_is_dwarf64 = bytes_read != 4;
4834 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4835 if (bytes_read + length != section->size)
4836 {
4837 /* There may be multiple per-CU indices. */
4838 warning (_("Section .debug_names in %s length %s does not match "
4839 "section length %s, ignoring .debug_names."),
4840 filename, plongest (bytes_read + length),
4841 pulongest (section->size));
4842 return false;
4843 }
4844
4845 /* The version number. */
4846 uint16_t version = read_2_bytes (abfd, addr);
4847 addr += 2;
4848 if (version != 5)
4849 {
4850 warning (_("Section .debug_names in %s has unsupported version %d, "
4851 "ignoring .debug_names."),
4852 filename, version);
4853 return false;
4854 }
4855
4856 /* Padding. */
4857 uint16_t padding = read_2_bytes (abfd, addr);
4858 addr += 2;
4859 if (padding != 0)
4860 {
4861 warning (_("Section .debug_names in %s has unsupported padding %d, "
4862 "ignoring .debug_names."),
4863 filename, padding);
4864 return false;
4865 }
4866
4867 /* comp_unit_count - The number of CUs in the CU list. */
4868 map.cu_count = read_4_bytes (abfd, addr);
4869 addr += 4;
4870
4871 /* local_type_unit_count - The number of TUs in the local TU
4872 list. */
4873 map.tu_count = read_4_bytes (abfd, addr);
4874 addr += 4;
4875
4876 /* foreign_type_unit_count - The number of TUs in the foreign TU
4877 list. */
4878 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4879 addr += 4;
4880 if (foreign_tu_count != 0)
4881 {
4882 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4883 "ignoring .debug_names."),
4884 filename, static_cast<unsigned long> (foreign_tu_count));
4885 return false;
4886 }
4887
4888 /* bucket_count - The number of hash buckets in the hash lookup
4889 table. */
4890 map.bucket_count = read_4_bytes (abfd, addr);
4891 addr += 4;
4892
4893 /* name_count - The number of unique names in the index. */
4894 map.name_count = read_4_bytes (abfd, addr);
4895 addr += 4;
4896
4897 /* abbrev_table_size - The size in bytes of the abbreviations
4898 table. */
4899 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4900 addr += 4;
4901
4902 /* augmentation_string_size - The size in bytes of the augmentation
4903 string. This value is rounded up to a multiple of 4. */
4904 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4905 addr += 4;
4906 map.augmentation_is_gdb = ((augmentation_string_size
4907 == sizeof (dwarf5_augmentation))
4908 && memcmp (addr, dwarf5_augmentation,
4909 sizeof (dwarf5_augmentation)) == 0);
4910 augmentation_string_size += (-augmentation_string_size) & 3;
4911 addr += augmentation_string_size;
4912
4913 /* List of CUs */
4914 map.cu_table_reordered = addr;
4915 addr += map.cu_count * map.offset_size;
4916
4917 /* List of Local TUs */
4918 map.tu_table_reordered = addr;
4919 addr += map.tu_count * map.offset_size;
4920
4921 /* Hash Lookup Table */
4922 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4923 addr += map.bucket_count * 4;
4924 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4925 addr += map.name_count * 4;
4926
4927 /* Name Table */
4928 map.name_table_string_offs_reordered = addr;
4929 addr += map.name_count * map.offset_size;
4930 map.name_table_entry_offs_reordered = addr;
4931 addr += map.name_count * map.offset_size;
4932
4933 const gdb_byte *abbrev_table_start = addr;
4934 for (;;)
4935 {
4936 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4937 addr += bytes_read;
4938 if (index_num == 0)
4939 break;
4940
4941 const auto insertpair
4942 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4943 if (!insertpair.second)
4944 {
4945 warning (_("Section .debug_names in %s has duplicate index %s, "
4946 "ignoring .debug_names."),
4947 filename, pulongest (index_num));
4948 return false;
4949 }
4950 mapped_debug_names::index_val &indexval = insertpair.first->second;
4951 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4952 addr += bytes_read;
4953
4954 for (;;)
4955 {
4956 mapped_debug_names::index_val::attr attr;
4957 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4958 addr += bytes_read;
4959 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4960 addr += bytes_read;
4961 if (attr.form == DW_FORM_implicit_const)
4962 {
4963 attr.implicit_const = read_signed_leb128 (abfd, addr,
4964 &bytes_read);
4965 addr += bytes_read;
4966 }
4967 if (attr.dw_idx == 0 && attr.form == 0)
4968 break;
4969 indexval.attr_vec.push_back (std::move (attr));
4970 }
4971 }
4972 if (addr != abbrev_table_start + abbrev_table_size)
4973 {
4974 warning (_("Section .debug_names in %s has abbreviation_table "
4975 "of size %s vs. written as %u, ignoring .debug_names."),
4976 filename, plongest (addr - abbrev_table_start),
4977 abbrev_table_size);
4978 return false;
4979 }
4980 map.entry_pool = addr;
4981
4982 return true;
4983 }
4984
4985 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4986 list. */
4987
4988 static void
4989 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4990 const mapped_debug_names &map,
4991 dwarf2_section_info &section,
4992 bool is_dwz)
4993 {
4994 sect_offset sect_off_prev;
4995 for (uint32_t i = 0; i <= map.cu_count; ++i)
4996 {
4997 sect_offset sect_off_next;
4998 if (i < map.cu_count)
4999 {
5000 sect_off_next
5001 = (sect_offset) (extract_unsigned_integer
5002 (map.cu_table_reordered + i * map.offset_size,
5003 map.offset_size,
5004 map.dwarf5_byte_order));
5005 }
5006 else
5007 sect_off_next = (sect_offset) section.size;
5008 if (i >= 1)
5009 {
5010 const ULONGEST length = sect_off_next - sect_off_prev;
5011 dwarf2_per_cu_data *per_cu
5012 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5013 sect_off_prev, length);
5014 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5015 }
5016 sect_off_prev = sect_off_next;
5017 }
5018 }
5019
5020 /* Read the CU list from the mapped index, and use it to create all
5021 the CU objects for this dwarf2_per_objfile. */
5022
5023 static void
5024 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5025 const mapped_debug_names &map,
5026 const mapped_debug_names &dwz_map)
5027 {
5028 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5029 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5030
5031 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5032 dwarf2_per_objfile->info,
5033 false /* is_dwz */);
5034
5035 if (dwz_map.cu_count == 0)
5036 return;
5037
5038 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5039 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5040 true /* is_dwz */);
5041 }
5042
5043 /* Read .debug_names. If everything went ok, initialize the "quick"
5044 elements of all the CUs and return true. Otherwise, return false. */
5045
5046 static bool
5047 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5048 {
5049 std::unique_ptr<mapped_debug_names> map
5050 (new mapped_debug_names (dwarf2_per_objfile));
5051 mapped_debug_names dwz_map (dwarf2_per_objfile);
5052 struct objfile *objfile = dwarf2_per_objfile->objfile;
5053
5054 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5055 &dwarf2_per_objfile->debug_names,
5056 *map))
5057 return false;
5058
5059 /* Don't use the index if it's empty. */
5060 if (map->name_count == 0)
5061 return false;
5062
5063 /* If there is a .dwz file, read it so we can get its CU list as
5064 well. */
5065 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5066 if (dwz != NULL)
5067 {
5068 if (!read_debug_names_from_section (objfile,
5069 bfd_get_filename (dwz->dwz_bfd.get ()),
5070 &dwz->debug_names, dwz_map))
5071 {
5072 warning (_("could not read '.debug_names' section from %s; skipping"),
5073 bfd_get_filename (dwz->dwz_bfd.get ()));
5074 return false;
5075 }
5076 }
5077
5078 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5079
5080 if (map->tu_count != 0)
5081 {
5082 /* We can only handle a single .debug_types when we have an
5083 index. */
5084 if (dwarf2_per_objfile->types.size () != 1)
5085 return false;
5086
5087 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5088
5089 create_signatured_type_table_from_debug_names
5090 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5091 }
5092
5093 create_addrmap_from_aranges (dwarf2_per_objfile,
5094 &dwarf2_per_objfile->debug_aranges);
5095
5096 dwarf2_per_objfile->debug_names_table = std::move (map);
5097 dwarf2_per_objfile->using_index = 1;
5098 dwarf2_per_objfile->quick_file_names_table =
5099 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5100
5101 return true;
5102 }
5103
5104 /* Type used to manage iterating over all CUs looking for a symbol for
5105 .debug_names. */
5106
5107 class dw2_debug_names_iterator
5108 {
5109 public:
5110 dw2_debug_names_iterator (const mapped_debug_names &map,
5111 gdb::optional<block_enum> block_index,
5112 domain_enum domain,
5113 const char *name)
5114 : m_map (map), m_block_index (block_index), m_domain (domain),
5115 m_addr (find_vec_in_debug_names (map, name))
5116 {}
5117
5118 dw2_debug_names_iterator (const mapped_debug_names &map,
5119 search_domain search, uint32_t namei)
5120 : m_map (map),
5121 m_search (search),
5122 m_addr (find_vec_in_debug_names (map, namei))
5123 {}
5124
5125 dw2_debug_names_iterator (const mapped_debug_names &map,
5126 block_enum block_index, domain_enum domain,
5127 uint32_t namei)
5128 : m_map (map), m_block_index (block_index), m_domain (domain),
5129 m_addr (find_vec_in_debug_names (map, namei))
5130 {}
5131
5132 /* Return the next matching CU or NULL if there are no more. */
5133 dwarf2_per_cu_data *next ();
5134
5135 private:
5136 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5137 const char *name);
5138 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5139 uint32_t namei);
5140
5141 /* The internalized form of .debug_names. */
5142 const mapped_debug_names &m_map;
5143
5144 /* If set, only look for symbols that match that block. Valid values are
5145 GLOBAL_BLOCK and STATIC_BLOCK. */
5146 const gdb::optional<block_enum> m_block_index;
5147
5148 /* The kind of symbol we're looking for. */
5149 const domain_enum m_domain = UNDEF_DOMAIN;
5150 const search_domain m_search = ALL_DOMAIN;
5151
5152 /* The list of CUs from the index entry of the symbol, or NULL if
5153 not found. */
5154 const gdb_byte *m_addr;
5155 };
5156
5157 const char *
5158 mapped_debug_names::namei_to_name (uint32_t namei) const
5159 {
5160 const ULONGEST namei_string_offs
5161 = extract_unsigned_integer ((name_table_string_offs_reordered
5162 + namei * offset_size),
5163 offset_size,
5164 dwarf5_byte_order);
5165 return read_indirect_string_at_offset (dwarf2_per_objfile,
5166 namei_string_offs);
5167 }
5168
5169 /* Find a slot in .debug_names for the object named NAME. If NAME is
5170 found, return pointer to its pool data. If NAME cannot be found,
5171 return NULL. */
5172
5173 const gdb_byte *
5174 dw2_debug_names_iterator::find_vec_in_debug_names
5175 (const mapped_debug_names &map, const char *name)
5176 {
5177 int (*cmp) (const char *, const char *);
5178
5179 gdb::unique_xmalloc_ptr<char> without_params;
5180 if (current_language->la_language == language_cplus
5181 || current_language->la_language == language_fortran
5182 || current_language->la_language == language_d)
5183 {
5184 /* NAME is already canonical. Drop any qualifiers as
5185 .debug_names does not contain any. */
5186
5187 if (strchr (name, '(') != NULL)
5188 {
5189 without_params = cp_remove_params (name);
5190 if (without_params != NULL)
5191 name = without_params.get ();
5192 }
5193 }
5194
5195 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5196
5197 const uint32_t full_hash = dwarf5_djb_hash (name);
5198 uint32_t namei
5199 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5200 (map.bucket_table_reordered
5201 + (full_hash % map.bucket_count)), 4,
5202 map.dwarf5_byte_order);
5203 if (namei == 0)
5204 return NULL;
5205 --namei;
5206 if (namei >= map.name_count)
5207 {
5208 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5209 "[in module %s]"),
5210 namei, map.name_count,
5211 objfile_name (map.dwarf2_per_objfile->objfile));
5212 return NULL;
5213 }
5214
5215 for (;;)
5216 {
5217 const uint32_t namei_full_hash
5218 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5219 (map.hash_table_reordered + namei), 4,
5220 map.dwarf5_byte_order);
5221 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5222 return NULL;
5223
5224 if (full_hash == namei_full_hash)
5225 {
5226 const char *const namei_string = map.namei_to_name (namei);
5227
5228 #if 0 /* An expensive sanity check. */
5229 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5230 {
5231 complaint (_("Wrong .debug_names hash for string at index %u "
5232 "[in module %s]"),
5233 namei, objfile_name (dwarf2_per_objfile->objfile));
5234 return NULL;
5235 }
5236 #endif
5237
5238 if (cmp (namei_string, name) == 0)
5239 {
5240 const ULONGEST namei_entry_offs
5241 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5242 + namei * map.offset_size),
5243 map.offset_size, map.dwarf5_byte_order);
5244 return map.entry_pool + namei_entry_offs;
5245 }
5246 }
5247
5248 ++namei;
5249 if (namei >= map.name_count)
5250 return NULL;
5251 }
5252 }
5253
5254 const gdb_byte *
5255 dw2_debug_names_iterator::find_vec_in_debug_names
5256 (const mapped_debug_names &map, uint32_t namei)
5257 {
5258 if (namei >= map.name_count)
5259 {
5260 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5261 "[in module %s]"),
5262 namei, map.name_count,
5263 objfile_name (map.dwarf2_per_objfile->objfile));
5264 return NULL;
5265 }
5266
5267 const ULONGEST namei_entry_offs
5268 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5269 + namei * map.offset_size),
5270 map.offset_size, map.dwarf5_byte_order);
5271 return map.entry_pool + namei_entry_offs;
5272 }
5273
5274 /* See dw2_debug_names_iterator. */
5275
5276 dwarf2_per_cu_data *
5277 dw2_debug_names_iterator::next ()
5278 {
5279 if (m_addr == NULL)
5280 return NULL;
5281
5282 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5283 struct objfile *objfile = dwarf2_per_objfile->objfile;
5284 bfd *const abfd = objfile->obfd;
5285
5286 again:
5287
5288 unsigned int bytes_read;
5289 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5290 m_addr += bytes_read;
5291 if (abbrev == 0)
5292 return NULL;
5293
5294 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5295 if (indexval_it == m_map.abbrev_map.cend ())
5296 {
5297 complaint (_("Wrong .debug_names undefined abbrev code %s "
5298 "[in module %s]"),
5299 pulongest (abbrev), objfile_name (objfile));
5300 return NULL;
5301 }
5302 const mapped_debug_names::index_val &indexval = indexval_it->second;
5303 enum class symbol_linkage {
5304 unknown,
5305 static_,
5306 extern_,
5307 } symbol_linkage_ = symbol_linkage::unknown;
5308 dwarf2_per_cu_data *per_cu = NULL;
5309 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5310 {
5311 ULONGEST ull;
5312 switch (attr.form)
5313 {
5314 case DW_FORM_implicit_const:
5315 ull = attr.implicit_const;
5316 break;
5317 case DW_FORM_flag_present:
5318 ull = 1;
5319 break;
5320 case DW_FORM_udata:
5321 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5322 m_addr += bytes_read;
5323 break;
5324 default:
5325 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5326 dwarf_form_name (attr.form),
5327 objfile_name (objfile));
5328 return NULL;
5329 }
5330 switch (attr.dw_idx)
5331 {
5332 case DW_IDX_compile_unit:
5333 /* Don't crash on bad data. */
5334 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5335 {
5336 complaint (_(".debug_names entry has bad CU index %s"
5337 " [in module %s]"),
5338 pulongest (ull),
5339 objfile_name (dwarf2_per_objfile->objfile));
5340 continue;
5341 }
5342 per_cu = dwarf2_per_objfile->get_cutu (ull);
5343 break;
5344 case DW_IDX_type_unit:
5345 /* Don't crash on bad data. */
5346 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5347 {
5348 complaint (_(".debug_names entry has bad TU index %s"
5349 " [in module %s]"),
5350 pulongest (ull),
5351 objfile_name (dwarf2_per_objfile->objfile));
5352 continue;
5353 }
5354 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5355 break;
5356 case DW_IDX_GNU_internal:
5357 if (!m_map.augmentation_is_gdb)
5358 break;
5359 symbol_linkage_ = symbol_linkage::static_;
5360 break;
5361 case DW_IDX_GNU_external:
5362 if (!m_map.augmentation_is_gdb)
5363 break;
5364 symbol_linkage_ = symbol_linkage::extern_;
5365 break;
5366 }
5367 }
5368
5369 /* Skip if already read in. */
5370 if (per_cu->v.quick->compunit_symtab)
5371 goto again;
5372
5373 /* Check static vs global. */
5374 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5375 {
5376 const bool want_static = *m_block_index == STATIC_BLOCK;
5377 const bool symbol_is_static =
5378 symbol_linkage_ == symbol_linkage::static_;
5379 if (want_static != symbol_is_static)
5380 goto again;
5381 }
5382
5383 /* Match dw2_symtab_iter_next, symbol_kind
5384 and debug_names::psymbol_tag. */
5385 switch (m_domain)
5386 {
5387 case VAR_DOMAIN:
5388 switch (indexval.dwarf_tag)
5389 {
5390 case DW_TAG_variable:
5391 case DW_TAG_subprogram:
5392 /* Some types are also in VAR_DOMAIN. */
5393 case DW_TAG_typedef:
5394 case DW_TAG_structure_type:
5395 break;
5396 default:
5397 goto again;
5398 }
5399 break;
5400 case STRUCT_DOMAIN:
5401 switch (indexval.dwarf_tag)
5402 {
5403 case DW_TAG_typedef:
5404 case DW_TAG_structure_type:
5405 break;
5406 default:
5407 goto again;
5408 }
5409 break;
5410 case LABEL_DOMAIN:
5411 switch (indexval.dwarf_tag)
5412 {
5413 case 0:
5414 case DW_TAG_variable:
5415 break;
5416 default:
5417 goto again;
5418 }
5419 break;
5420 case MODULE_DOMAIN:
5421 switch (indexval.dwarf_tag)
5422 {
5423 case DW_TAG_module:
5424 break;
5425 default:
5426 goto again;
5427 }
5428 break;
5429 default:
5430 break;
5431 }
5432
5433 /* Match dw2_expand_symtabs_matching, symbol_kind and
5434 debug_names::psymbol_tag. */
5435 switch (m_search)
5436 {
5437 case VARIABLES_DOMAIN:
5438 switch (indexval.dwarf_tag)
5439 {
5440 case DW_TAG_variable:
5441 break;
5442 default:
5443 goto again;
5444 }
5445 break;
5446 case FUNCTIONS_DOMAIN:
5447 switch (indexval.dwarf_tag)
5448 {
5449 case DW_TAG_subprogram:
5450 break;
5451 default:
5452 goto again;
5453 }
5454 break;
5455 case TYPES_DOMAIN:
5456 switch (indexval.dwarf_tag)
5457 {
5458 case DW_TAG_typedef:
5459 case DW_TAG_structure_type:
5460 break;
5461 default:
5462 goto again;
5463 }
5464 break;
5465 case MODULES_DOMAIN:
5466 switch (indexval.dwarf_tag)
5467 {
5468 case DW_TAG_module:
5469 break;
5470 default:
5471 goto again;
5472 }
5473 default:
5474 break;
5475 }
5476
5477 return per_cu;
5478 }
5479
5480 static struct compunit_symtab *
5481 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5482 const char *name, domain_enum domain)
5483 {
5484 struct dwarf2_per_objfile *dwarf2_per_objfile
5485 = get_dwarf2_per_objfile (objfile);
5486
5487 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5488 if (!mapp)
5489 {
5490 /* index is NULL if OBJF_READNOW. */
5491 return NULL;
5492 }
5493 const auto &map = *mapp;
5494
5495 dw2_debug_names_iterator iter (map, block_index, domain, name);
5496
5497 struct compunit_symtab *stab_best = NULL;
5498 struct dwarf2_per_cu_data *per_cu;
5499 while ((per_cu = iter.next ()) != NULL)
5500 {
5501 struct symbol *sym, *with_opaque = NULL;
5502 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5503 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5504 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5505
5506 sym = block_find_symbol (block, name, domain,
5507 block_find_non_opaque_type_preferred,
5508 &with_opaque);
5509
5510 /* Some caution must be observed with overloaded functions and
5511 methods, since the index will not contain any overload
5512 information (but NAME might contain it). */
5513
5514 if (sym != NULL
5515 && strcmp_iw (sym->search_name (), name) == 0)
5516 return stab;
5517 if (with_opaque != NULL
5518 && strcmp_iw (with_opaque->search_name (), name) == 0)
5519 stab_best = stab;
5520
5521 /* Keep looking through other CUs. */
5522 }
5523
5524 return stab_best;
5525 }
5526
5527 /* This dumps minimal information about .debug_names. It is called
5528 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5529 uses this to verify that .debug_names has been loaded. */
5530
5531 static void
5532 dw2_debug_names_dump (struct objfile *objfile)
5533 {
5534 struct dwarf2_per_objfile *dwarf2_per_objfile
5535 = get_dwarf2_per_objfile (objfile);
5536
5537 gdb_assert (dwarf2_per_objfile->using_index);
5538 printf_filtered (".debug_names:");
5539 if (dwarf2_per_objfile->debug_names_table)
5540 printf_filtered (" exists\n");
5541 else
5542 printf_filtered (" faked for \"readnow\"\n");
5543 printf_filtered ("\n");
5544 }
5545
5546 static void
5547 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5548 const char *func_name)
5549 {
5550 struct dwarf2_per_objfile *dwarf2_per_objfile
5551 = get_dwarf2_per_objfile (objfile);
5552
5553 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5554 if (dwarf2_per_objfile->debug_names_table)
5555 {
5556 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5557
5558 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5559
5560 struct dwarf2_per_cu_data *per_cu;
5561 while ((per_cu = iter.next ()) != NULL)
5562 dw2_instantiate_symtab (per_cu, false);
5563 }
5564 }
5565
5566 static void
5567 dw2_debug_names_map_matching_symbols
5568 (struct objfile *objfile,
5569 const lookup_name_info &name, domain_enum domain,
5570 int global,
5571 gdb::function_view<symbol_found_callback_ftype> callback,
5572 symbol_compare_ftype *ordered_compare)
5573 {
5574 struct dwarf2_per_objfile *dwarf2_per_objfile
5575 = get_dwarf2_per_objfile (objfile);
5576
5577 /* debug_names_table is NULL if OBJF_READNOW. */
5578 if (!dwarf2_per_objfile->debug_names_table)
5579 return;
5580
5581 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5582 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5583
5584 const char *match_name = name.ada ().lookup_name ().c_str ();
5585 auto matcher = [&] (const char *symname)
5586 {
5587 if (ordered_compare == nullptr)
5588 return true;
5589 return ordered_compare (symname, match_name) == 0;
5590 };
5591
5592 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5593 [&] (offset_type namei)
5594 {
5595 /* The name was matched, now expand corresponding CUs that were
5596 marked. */
5597 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5598
5599 struct dwarf2_per_cu_data *per_cu;
5600 while ((per_cu = iter.next ()) != NULL)
5601 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5602 return true;
5603 });
5604
5605 /* It's a shame we couldn't do this inside the
5606 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5607 that have already been expanded. Instead, this loop matches what
5608 the psymtab code does. */
5609 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5610 {
5611 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5612 if (cust != nullptr)
5613 {
5614 const struct block *block
5615 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5616 if (!iterate_over_symbols_terminated (block, name,
5617 domain, callback))
5618 break;
5619 }
5620 }
5621 }
5622
5623 static void
5624 dw2_debug_names_expand_symtabs_matching
5625 (struct objfile *objfile,
5626 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5627 const lookup_name_info *lookup_name,
5628 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5629 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5630 enum search_domain kind)
5631 {
5632 struct dwarf2_per_objfile *dwarf2_per_objfile
5633 = get_dwarf2_per_objfile (objfile);
5634
5635 /* debug_names_table is NULL if OBJF_READNOW. */
5636 if (!dwarf2_per_objfile->debug_names_table)
5637 return;
5638
5639 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5640
5641 if (symbol_matcher == NULL && lookup_name == NULL)
5642 {
5643 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5644 {
5645 QUIT;
5646
5647 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5648 expansion_notify);
5649 }
5650 return;
5651 }
5652
5653 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5654
5655 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5656 symbol_matcher,
5657 kind, [&] (offset_type namei)
5658 {
5659 /* The name was matched, now expand corresponding CUs that were
5660 marked. */
5661 dw2_debug_names_iterator iter (map, kind, namei);
5662
5663 struct dwarf2_per_cu_data *per_cu;
5664 while ((per_cu = iter.next ()) != NULL)
5665 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5666 expansion_notify);
5667 return true;
5668 });
5669 }
5670
5671 const struct quick_symbol_functions dwarf2_debug_names_functions =
5672 {
5673 dw2_has_symbols,
5674 dw2_find_last_source_symtab,
5675 dw2_forget_cached_source_info,
5676 dw2_map_symtabs_matching_filename,
5677 dw2_debug_names_lookup_symbol,
5678 NULL,
5679 dw2_print_stats,
5680 dw2_debug_names_dump,
5681 dw2_debug_names_expand_symtabs_for_function,
5682 dw2_expand_all_symtabs,
5683 dw2_expand_symtabs_with_fullname,
5684 dw2_debug_names_map_matching_symbols,
5685 dw2_debug_names_expand_symtabs_matching,
5686 dw2_find_pc_sect_compunit_symtab,
5687 NULL,
5688 dw2_map_symbol_filenames
5689 };
5690
5691 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5692 to either a dwarf2_per_objfile or dwz_file object. */
5693
5694 template <typename T>
5695 static gdb::array_view<const gdb_byte>
5696 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5697 {
5698 dwarf2_section_info *section = &section_owner->gdb_index;
5699
5700 if (section->empty ())
5701 return {};
5702
5703 /* Older elfutils strip versions could keep the section in the main
5704 executable while splitting it for the separate debug info file. */
5705 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5706 return {};
5707
5708 section->read (obj);
5709
5710 /* dwarf2_section_info::size is a bfd_size_type, while
5711 gdb::array_view works with size_t. On 32-bit hosts, with
5712 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5713 is 32-bit. So we need an explicit narrowing conversion here.
5714 This is fine, because it's impossible to allocate or mmap an
5715 array/buffer larger than what size_t can represent. */
5716 return gdb::make_array_view (section->buffer, section->size);
5717 }
5718
5719 /* Lookup the index cache for the contents of the index associated to
5720 DWARF2_OBJ. */
5721
5722 static gdb::array_view<const gdb_byte>
5723 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5724 {
5725 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5726 if (build_id == nullptr)
5727 return {};
5728
5729 return global_index_cache.lookup_gdb_index (build_id,
5730 &dwarf2_obj->index_cache_res);
5731 }
5732
5733 /* Same as the above, but for DWZ. */
5734
5735 static gdb::array_view<const gdb_byte>
5736 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5737 {
5738 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5739 if (build_id == nullptr)
5740 return {};
5741
5742 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5743 }
5744
5745 /* See symfile.h. */
5746
5747 bool
5748 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5749 {
5750 struct dwarf2_per_objfile *dwarf2_per_objfile
5751 = get_dwarf2_per_objfile (objfile);
5752
5753 /* If we're about to read full symbols, don't bother with the
5754 indices. In this case we also don't care if some other debug
5755 format is making psymtabs, because they are all about to be
5756 expanded anyway. */
5757 if ((objfile->flags & OBJF_READNOW))
5758 {
5759 dwarf2_per_objfile->using_index = 1;
5760 create_all_comp_units (dwarf2_per_objfile);
5761 create_all_type_units (dwarf2_per_objfile);
5762 dwarf2_per_objfile->quick_file_names_table
5763 = create_quick_file_names_table
5764 (dwarf2_per_objfile->all_comp_units.size ());
5765
5766 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5767 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5768 {
5769 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5770
5771 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5772 struct dwarf2_per_cu_quick_data);
5773 }
5774
5775 /* Return 1 so that gdb sees the "quick" functions. However,
5776 these functions will be no-ops because we will have expanded
5777 all symtabs. */
5778 *index_kind = dw_index_kind::GDB_INDEX;
5779 return true;
5780 }
5781
5782 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5783 {
5784 *index_kind = dw_index_kind::DEBUG_NAMES;
5785 return true;
5786 }
5787
5788 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5789 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5790 get_gdb_index_contents_from_section<dwz_file>))
5791 {
5792 *index_kind = dw_index_kind::GDB_INDEX;
5793 return true;
5794 }
5795
5796 /* ... otherwise, try to find the index in the index cache. */
5797 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5798 get_gdb_index_contents_from_cache,
5799 get_gdb_index_contents_from_cache_dwz))
5800 {
5801 global_index_cache.hit ();
5802 *index_kind = dw_index_kind::GDB_INDEX;
5803 return true;
5804 }
5805
5806 global_index_cache.miss ();
5807 return false;
5808 }
5809
5810 \f
5811
5812 /* Build a partial symbol table. */
5813
5814 void
5815 dwarf2_build_psymtabs (struct objfile *objfile)
5816 {
5817 struct dwarf2_per_objfile *dwarf2_per_objfile
5818 = get_dwarf2_per_objfile (objfile);
5819
5820 init_psymbol_list (objfile, 1024);
5821
5822 try
5823 {
5824 /* This isn't really ideal: all the data we allocate on the
5825 objfile's obstack is still uselessly kept around. However,
5826 freeing it seems unsafe. */
5827 psymtab_discarder psymtabs (objfile);
5828 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5829 psymtabs.keep ();
5830
5831 /* (maybe) store an index in the cache. */
5832 global_index_cache.store (dwarf2_per_objfile);
5833 }
5834 catch (const gdb_exception_error &except)
5835 {
5836 exception_print (gdb_stderr, except);
5837 }
5838 }
5839
5840 /* Find the base address of the compilation unit for range lists and
5841 location lists. It will normally be specified by DW_AT_low_pc.
5842 In DWARF-3 draft 4, the base address could be overridden by
5843 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5844 compilation units with discontinuous ranges. */
5845
5846 static void
5847 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5848 {
5849 struct attribute *attr;
5850
5851 cu->base_address.reset ();
5852
5853 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5854 if (attr != nullptr)
5855 cu->base_address = attr->value_as_address ();
5856 else
5857 {
5858 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5859 if (attr != nullptr)
5860 cu->base_address = attr->value_as_address ();
5861 }
5862 }
5863
5864 /* Helper function that returns the proper abbrev section for
5865 THIS_CU. */
5866
5867 static struct dwarf2_section_info *
5868 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5869 {
5870 struct dwarf2_section_info *abbrev;
5871 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5872
5873 if (this_cu->is_dwz)
5874 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5875 else
5876 abbrev = &dwarf2_per_objfile->abbrev;
5877
5878 return abbrev;
5879 }
5880
5881 /* Fetch the abbreviation table offset from a comp or type unit header. */
5882
5883 static sect_offset
5884 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5885 struct dwarf2_section_info *section,
5886 sect_offset sect_off)
5887 {
5888 bfd *abfd = section->get_bfd_owner ();
5889 const gdb_byte *info_ptr;
5890 unsigned int initial_length_size, offset_size;
5891 uint16_t version;
5892
5893 section->read (dwarf2_per_objfile->objfile);
5894 info_ptr = section->buffer + to_underlying (sect_off);
5895 read_initial_length (abfd, info_ptr, &initial_length_size);
5896 offset_size = initial_length_size == 4 ? 4 : 8;
5897 info_ptr += initial_length_size;
5898
5899 version = read_2_bytes (abfd, info_ptr);
5900 info_ptr += 2;
5901 if (version >= 5)
5902 {
5903 /* Skip unit type and address size. */
5904 info_ptr += 2;
5905 }
5906
5907 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5908 }
5909
5910 /* A partial symtab that is used only for include files. */
5911 struct dwarf2_include_psymtab : public partial_symtab
5912 {
5913 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5914 : partial_symtab (filename, objfile)
5915 {
5916 }
5917
5918 void read_symtab (struct objfile *objfile) override
5919 {
5920 /* It's an include file, no symbols to read for it.
5921 Everything is in the includer symtab. */
5922
5923 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5924 expansion of the includer psymtab. We use the dependencies[0] field to
5925 model the includer. But if we go the regular route of calling
5926 expand_psymtab here, and having expand_psymtab call expand_dependencies
5927 to expand the includer, we'll only use expand_psymtab on the includer
5928 (making it a non-toplevel psymtab), while if we expand the includer via
5929 another path, we'll use read_symtab (making it a toplevel psymtab).
5930 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5931 psymtab, and trigger read_symtab on the includer here directly. */
5932 includer ()->read_symtab (objfile);
5933 }
5934
5935 void expand_psymtab (struct objfile *objfile) override
5936 {
5937 /* This is not called by read_symtab, and should not be called by any
5938 expand_dependencies. */
5939 gdb_assert (false);
5940 }
5941
5942 bool readin_p () const override
5943 {
5944 return includer ()->readin_p ();
5945 }
5946
5947 struct compunit_symtab *get_compunit_symtab () const override
5948 {
5949 return nullptr;
5950 }
5951
5952 private:
5953 partial_symtab *includer () const
5954 {
5955 /* An include psymtab has exactly one dependency: the psymtab that
5956 includes it. */
5957 gdb_assert (this->number_of_dependencies == 1);
5958 return this->dependencies[0];
5959 }
5960 };
5961
5962 /* Allocate a new partial symtab for file named NAME and mark this new
5963 partial symtab as being an include of PST. */
5964
5965 static void
5966 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5967 struct objfile *objfile)
5968 {
5969 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
5970
5971 if (!IS_ABSOLUTE_PATH (subpst->filename))
5972 {
5973 /* It shares objfile->objfile_obstack. */
5974 subpst->dirname = pst->dirname;
5975 }
5976
5977 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
5978 subpst->dependencies[0] = pst;
5979 subpst->number_of_dependencies = 1;
5980 }
5981
5982 /* Read the Line Number Program data and extract the list of files
5983 included by the source file represented by PST. Build an include
5984 partial symtab for each of these included files. */
5985
5986 static void
5987 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5988 struct die_info *die,
5989 dwarf2_psymtab *pst)
5990 {
5991 line_header_up lh;
5992 struct attribute *attr;
5993
5994 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5995 if (attr != nullptr)
5996 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5997 if (lh == NULL)
5998 return; /* No linetable, so no includes. */
5999
6000 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6001 that we pass in the raw text_low here; that is ok because we're
6002 only decoding the line table to make include partial symtabs, and
6003 so the addresses aren't really used. */
6004 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6005 pst->raw_text_low (), 1);
6006 }
6007
6008 static hashval_t
6009 hash_signatured_type (const void *item)
6010 {
6011 const struct signatured_type *sig_type
6012 = (const struct signatured_type *) item;
6013
6014 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6015 return sig_type->signature;
6016 }
6017
6018 static int
6019 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6020 {
6021 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6022 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6023
6024 return lhs->signature == rhs->signature;
6025 }
6026
6027 /* Allocate a hash table for signatured types. */
6028
6029 static htab_up
6030 allocate_signatured_type_table ()
6031 {
6032 return htab_up (htab_create_alloc (41,
6033 hash_signatured_type,
6034 eq_signatured_type,
6035 NULL, xcalloc, xfree));
6036 }
6037
6038 /* A helper function to add a signatured type CU to a table. */
6039
6040 static int
6041 add_signatured_type_cu_to_table (void **slot, void *datum)
6042 {
6043 struct signatured_type *sigt = (struct signatured_type *) *slot;
6044 std::vector<signatured_type *> *all_type_units
6045 = (std::vector<signatured_type *> *) datum;
6046
6047 all_type_units->push_back (sigt);
6048
6049 return 1;
6050 }
6051
6052 /* A helper for create_debug_types_hash_table. Read types from SECTION
6053 and fill them into TYPES_HTAB. It will process only type units,
6054 therefore DW_UT_type. */
6055
6056 static void
6057 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6058 struct dwo_file *dwo_file,
6059 dwarf2_section_info *section, htab_up &types_htab,
6060 rcuh_kind section_kind)
6061 {
6062 struct objfile *objfile = dwarf2_per_objfile->objfile;
6063 struct dwarf2_section_info *abbrev_section;
6064 bfd *abfd;
6065 const gdb_byte *info_ptr, *end_ptr;
6066
6067 abbrev_section = (dwo_file != NULL
6068 ? &dwo_file->sections.abbrev
6069 : &dwarf2_per_objfile->abbrev);
6070
6071 if (dwarf_read_debug)
6072 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6073 section->get_name (),
6074 abbrev_section->get_file_name ());
6075
6076 section->read (objfile);
6077 info_ptr = section->buffer;
6078
6079 if (info_ptr == NULL)
6080 return;
6081
6082 /* We can't set abfd until now because the section may be empty or
6083 not present, in which case the bfd is unknown. */
6084 abfd = section->get_bfd_owner ();
6085
6086 /* We don't use cutu_reader here because we don't need to read
6087 any dies: the signature is in the header. */
6088
6089 end_ptr = info_ptr + section->size;
6090 while (info_ptr < end_ptr)
6091 {
6092 struct signatured_type *sig_type;
6093 struct dwo_unit *dwo_tu;
6094 void **slot;
6095 const gdb_byte *ptr = info_ptr;
6096 struct comp_unit_head header;
6097 unsigned int length;
6098
6099 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6100
6101 /* Initialize it due to a false compiler warning. */
6102 header.signature = -1;
6103 header.type_cu_offset_in_tu = (cu_offset) -1;
6104
6105 /* We need to read the type's signature in order to build the hash
6106 table, but we don't need anything else just yet. */
6107
6108 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6109 abbrev_section, ptr, section_kind);
6110
6111 length = header.get_length ();
6112
6113 /* Skip dummy type units. */
6114 if (ptr >= info_ptr + length
6115 || peek_abbrev_code (abfd, ptr) == 0
6116 || header.unit_type != DW_UT_type)
6117 {
6118 info_ptr += length;
6119 continue;
6120 }
6121
6122 if (types_htab == NULL)
6123 {
6124 if (dwo_file)
6125 types_htab = allocate_dwo_unit_table ();
6126 else
6127 types_htab = allocate_signatured_type_table ();
6128 }
6129
6130 if (dwo_file)
6131 {
6132 sig_type = NULL;
6133 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6134 struct dwo_unit);
6135 dwo_tu->dwo_file = dwo_file;
6136 dwo_tu->signature = header.signature;
6137 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6138 dwo_tu->section = section;
6139 dwo_tu->sect_off = sect_off;
6140 dwo_tu->length = length;
6141 }
6142 else
6143 {
6144 /* N.B.: type_offset is not usable if this type uses a DWO file.
6145 The real type_offset is in the DWO file. */
6146 dwo_tu = NULL;
6147 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6148 struct signatured_type);
6149 sig_type->signature = header.signature;
6150 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6151 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6152 sig_type->per_cu.is_debug_types = 1;
6153 sig_type->per_cu.section = section;
6154 sig_type->per_cu.sect_off = sect_off;
6155 sig_type->per_cu.length = length;
6156 }
6157
6158 slot = htab_find_slot (types_htab.get (),
6159 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6160 INSERT);
6161 gdb_assert (slot != NULL);
6162 if (*slot != NULL)
6163 {
6164 sect_offset dup_sect_off;
6165
6166 if (dwo_file)
6167 {
6168 const struct dwo_unit *dup_tu
6169 = (const struct dwo_unit *) *slot;
6170
6171 dup_sect_off = dup_tu->sect_off;
6172 }
6173 else
6174 {
6175 const struct signatured_type *dup_tu
6176 = (const struct signatured_type *) *slot;
6177
6178 dup_sect_off = dup_tu->per_cu.sect_off;
6179 }
6180
6181 complaint (_("debug type entry at offset %s is duplicate to"
6182 " the entry at offset %s, signature %s"),
6183 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6184 hex_string (header.signature));
6185 }
6186 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6187
6188 if (dwarf_read_debug > 1)
6189 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6190 sect_offset_str (sect_off),
6191 hex_string (header.signature));
6192
6193 info_ptr += length;
6194 }
6195 }
6196
6197 /* Create the hash table of all entries in the .debug_types
6198 (or .debug_types.dwo) section(s).
6199 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6200 otherwise it is NULL.
6201
6202 The result is a pointer to the hash table or NULL if there are no types.
6203
6204 Note: This function processes DWO files only, not DWP files. */
6205
6206 static void
6207 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6208 struct dwo_file *dwo_file,
6209 gdb::array_view<dwarf2_section_info> type_sections,
6210 htab_up &types_htab)
6211 {
6212 for (dwarf2_section_info &section : type_sections)
6213 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6214 types_htab, rcuh_kind::TYPE);
6215 }
6216
6217 /* Create the hash table of all entries in the .debug_types section,
6218 and initialize all_type_units.
6219 The result is zero if there is an error (e.g. missing .debug_types section),
6220 otherwise non-zero. */
6221
6222 static int
6223 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6224 {
6225 htab_up types_htab;
6226
6227 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6228 &dwarf2_per_objfile->info, types_htab,
6229 rcuh_kind::COMPILE);
6230 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6231 dwarf2_per_objfile->types, types_htab);
6232 if (types_htab == NULL)
6233 {
6234 dwarf2_per_objfile->signatured_types = NULL;
6235 return 0;
6236 }
6237
6238 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6239
6240 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6241 dwarf2_per_objfile->all_type_units.reserve
6242 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6243
6244 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6245 add_signatured_type_cu_to_table,
6246 &dwarf2_per_objfile->all_type_units);
6247
6248 return 1;
6249 }
6250
6251 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6252 If SLOT is non-NULL, it is the entry to use in the hash table.
6253 Otherwise we find one. */
6254
6255 static struct signatured_type *
6256 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6257 void **slot)
6258 {
6259 struct objfile *objfile = dwarf2_per_objfile->objfile;
6260
6261 if (dwarf2_per_objfile->all_type_units.size ()
6262 == dwarf2_per_objfile->all_type_units.capacity ())
6263 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6264
6265 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6266 struct signatured_type);
6267
6268 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6269 sig_type->signature = sig;
6270 sig_type->per_cu.is_debug_types = 1;
6271 if (dwarf2_per_objfile->using_index)
6272 {
6273 sig_type->per_cu.v.quick =
6274 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6275 struct dwarf2_per_cu_quick_data);
6276 }
6277
6278 if (slot == NULL)
6279 {
6280 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6281 sig_type, INSERT);
6282 }
6283 gdb_assert (*slot == NULL);
6284 *slot = sig_type;
6285 /* The rest of sig_type must be filled in by the caller. */
6286 return sig_type;
6287 }
6288
6289 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6290 Fill in SIG_ENTRY with DWO_ENTRY. */
6291
6292 static void
6293 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6294 struct signatured_type *sig_entry,
6295 struct dwo_unit *dwo_entry)
6296 {
6297 /* Make sure we're not clobbering something we don't expect to. */
6298 gdb_assert (! sig_entry->per_cu.queued);
6299 gdb_assert (sig_entry->per_cu.cu == NULL);
6300 if (dwarf2_per_objfile->using_index)
6301 {
6302 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6303 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6304 }
6305 else
6306 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6307 gdb_assert (sig_entry->signature == dwo_entry->signature);
6308 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6309 gdb_assert (sig_entry->type_unit_group == NULL);
6310 gdb_assert (sig_entry->dwo_unit == NULL);
6311
6312 sig_entry->per_cu.section = dwo_entry->section;
6313 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6314 sig_entry->per_cu.length = dwo_entry->length;
6315 sig_entry->per_cu.reading_dwo_directly = 1;
6316 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6317 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6318 sig_entry->dwo_unit = dwo_entry;
6319 }
6320
6321 /* Subroutine of lookup_signatured_type.
6322 If we haven't read the TU yet, create the signatured_type data structure
6323 for a TU to be read in directly from a DWO file, bypassing the stub.
6324 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6325 using .gdb_index, then when reading a CU we want to stay in the DWO file
6326 containing that CU. Otherwise we could end up reading several other DWO
6327 files (due to comdat folding) to process the transitive closure of all the
6328 mentioned TUs, and that can be slow. The current DWO file will have every
6329 type signature that it needs.
6330 We only do this for .gdb_index because in the psymtab case we already have
6331 to read all the DWOs to build the type unit groups. */
6332
6333 static struct signatured_type *
6334 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6335 {
6336 struct dwarf2_per_objfile *dwarf2_per_objfile
6337 = cu->per_cu->dwarf2_per_objfile;
6338 struct dwo_file *dwo_file;
6339 struct dwo_unit find_dwo_entry, *dwo_entry;
6340 struct signatured_type find_sig_entry, *sig_entry;
6341 void **slot;
6342
6343 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6344
6345 /* If TU skeletons have been removed then we may not have read in any
6346 TUs yet. */
6347 if (dwarf2_per_objfile->signatured_types == NULL)
6348 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6349
6350 /* We only ever need to read in one copy of a signatured type.
6351 Use the global signatured_types array to do our own comdat-folding
6352 of types. If this is the first time we're reading this TU, and
6353 the TU has an entry in .gdb_index, replace the recorded data from
6354 .gdb_index with this TU. */
6355
6356 find_sig_entry.signature = sig;
6357 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6358 &find_sig_entry, INSERT);
6359 sig_entry = (struct signatured_type *) *slot;
6360
6361 /* We can get here with the TU already read, *or* in the process of being
6362 read. Don't reassign the global entry to point to this DWO if that's
6363 the case. Also note that if the TU is already being read, it may not
6364 have come from a DWO, the program may be a mix of Fission-compiled
6365 code and non-Fission-compiled code. */
6366
6367 /* Have we already tried to read this TU?
6368 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6369 needn't exist in the global table yet). */
6370 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6371 return sig_entry;
6372
6373 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6374 dwo_unit of the TU itself. */
6375 dwo_file = cu->dwo_unit->dwo_file;
6376
6377 /* Ok, this is the first time we're reading this TU. */
6378 if (dwo_file->tus == NULL)
6379 return NULL;
6380 find_dwo_entry.signature = sig;
6381 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6382 &find_dwo_entry);
6383 if (dwo_entry == NULL)
6384 return NULL;
6385
6386 /* If the global table doesn't have an entry for this TU, add one. */
6387 if (sig_entry == NULL)
6388 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6389
6390 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6391 sig_entry->per_cu.tu_read = 1;
6392 return sig_entry;
6393 }
6394
6395 /* Subroutine of lookup_signatured_type.
6396 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6397 then try the DWP file. If the TU stub (skeleton) has been removed then
6398 it won't be in .gdb_index. */
6399
6400 static struct signatured_type *
6401 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6402 {
6403 struct dwarf2_per_objfile *dwarf2_per_objfile
6404 = cu->per_cu->dwarf2_per_objfile;
6405 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6406 struct dwo_unit *dwo_entry;
6407 struct signatured_type find_sig_entry, *sig_entry;
6408 void **slot;
6409
6410 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6411 gdb_assert (dwp_file != NULL);
6412
6413 /* If TU skeletons have been removed then we may not have read in any
6414 TUs yet. */
6415 if (dwarf2_per_objfile->signatured_types == NULL)
6416 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6417
6418 find_sig_entry.signature = sig;
6419 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6420 &find_sig_entry, INSERT);
6421 sig_entry = (struct signatured_type *) *slot;
6422
6423 /* Have we already tried to read this TU?
6424 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6425 needn't exist in the global table yet). */
6426 if (sig_entry != NULL)
6427 return sig_entry;
6428
6429 if (dwp_file->tus == NULL)
6430 return NULL;
6431 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6432 sig, 1 /* is_debug_types */);
6433 if (dwo_entry == NULL)
6434 return NULL;
6435
6436 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6437 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6438
6439 return sig_entry;
6440 }
6441
6442 /* Lookup a signature based type for DW_FORM_ref_sig8.
6443 Returns NULL if signature SIG is not present in the table.
6444 It is up to the caller to complain about this. */
6445
6446 static struct signatured_type *
6447 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6448 {
6449 struct dwarf2_per_objfile *dwarf2_per_objfile
6450 = cu->per_cu->dwarf2_per_objfile;
6451
6452 if (cu->dwo_unit
6453 && dwarf2_per_objfile->using_index)
6454 {
6455 /* We're in a DWO/DWP file, and we're using .gdb_index.
6456 These cases require special processing. */
6457 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6458 return lookup_dwo_signatured_type (cu, sig);
6459 else
6460 return lookup_dwp_signatured_type (cu, sig);
6461 }
6462 else
6463 {
6464 struct signatured_type find_entry, *entry;
6465
6466 if (dwarf2_per_objfile->signatured_types == NULL)
6467 return NULL;
6468 find_entry.signature = sig;
6469 entry = ((struct signatured_type *)
6470 htab_find (dwarf2_per_objfile->signatured_types.get (),
6471 &find_entry));
6472 return entry;
6473 }
6474 }
6475
6476 /* Low level DIE reading support. */
6477
6478 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6479
6480 static void
6481 init_cu_die_reader (struct die_reader_specs *reader,
6482 struct dwarf2_cu *cu,
6483 struct dwarf2_section_info *section,
6484 struct dwo_file *dwo_file,
6485 struct abbrev_table *abbrev_table)
6486 {
6487 gdb_assert (section->readin && section->buffer != NULL);
6488 reader->abfd = section->get_bfd_owner ();
6489 reader->cu = cu;
6490 reader->dwo_file = dwo_file;
6491 reader->die_section = section;
6492 reader->buffer = section->buffer;
6493 reader->buffer_end = section->buffer + section->size;
6494 reader->abbrev_table = abbrev_table;
6495 }
6496
6497 /* Subroutine of cutu_reader to simplify it.
6498 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6499 There's just a lot of work to do, and cutu_reader is big enough
6500 already.
6501
6502 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6503 from it to the DIE in the DWO. If NULL we are skipping the stub.
6504 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6505 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6506 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6507 STUB_COMP_DIR may be non-NULL.
6508 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6509 are filled in with the info of the DIE from the DWO file.
6510 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6511 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6512 kept around for at least as long as *RESULT_READER.
6513
6514 The result is non-zero if a valid (non-dummy) DIE was found. */
6515
6516 static int
6517 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6518 struct dwo_unit *dwo_unit,
6519 struct die_info *stub_comp_unit_die,
6520 const char *stub_comp_dir,
6521 struct die_reader_specs *result_reader,
6522 const gdb_byte **result_info_ptr,
6523 struct die_info **result_comp_unit_die,
6524 abbrev_table_up *result_dwo_abbrev_table)
6525 {
6526 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6527 struct objfile *objfile = dwarf2_per_objfile->objfile;
6528 struct dwarf2_cu *cu = this_cu->cu;
6529 bfd *abfd;
6530 const gdb_byte *begin_info_ptr, *info_ptr;
6531 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6532 int i,num_extra_attrs;
6533 struct dwarf2_section_info *dwo_abbrev_section;
6534 struct die_info *comp_unit_die;
6535
6536 /* At most one of these may be provided. */
6537 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6538
6539 /* These attributes aren't processed until later:
6540 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6541 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6542 referenced later. However, these attributes are found in the stub
6543 which we won't have later. In order to not impose this complication
6544 on the rest of the code, we read them here and copy them to the
6545 DWO CU/TU die. */
6546
6547 stmt_list = NULL;
6548 low_pc = NULL;
6549 high_pc = NULL;
6550 ranges = NULL;
6551 comp_dir = NULL;
6552
6553 if (stub_comp_unit_die != NULL)
6554 {
6555 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6556 DWO file. */
6557 if (! this_cu->is_debug_types)
6558 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6559 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6560 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6561 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6562 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6563
6564 cu->addr_base = stub_comp_unit_die->addr_base ();
6565
6566 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6567 here (if needed). We need the value before we can process
6568 DW_AT_ranges. */
6569 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6570 }
6571 else if (stub_comp_dir != NULL)
6572 {
6573 /* Reconstruct the comp_dir attribute to simplify the code below. */
6574 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6575 comp_dir->name = DW_AT_comp_dir;
6576 comp_dir->form = DW_FORM_string;
6577 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6578 DW_STRING (comp_dir) = stub_comp_dir;
6579 }
6580
6581 /* Set up for reading the DWO CU/TU. */
6582 cu->dwo_unit = dwo_unit;
6583 dwarf2_section_info *section = dwo_unit->section;
6584 section->read (objfile);
6585 abfd = section->get_bfd_owner ();
6586 begin_info_ptr = info_ptr = (section->buffer
6587 + to_underlying (dwo_unit->sect_off));
6588 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6589
6590 if (this_cu->is_debug_types)
6591 {
6592 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6593
6594 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6595 &cu->header, section,
6596 dwo_abbrev_section,
6597 info_ptr, rcuh_kind::TYPE);
6598 /* This is not an assert because it can be caused by bad debug info. */
6599 if (sig_type->signature != cu->header.signature)
6600 {
6601 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6602 " TU at offset %s [in module %s]"),
6603 hex_string (sig_type->signature),
6604 hex_string (cu->header.signature),
6605 sect_offset_str (dwo_unit->sect_off),
6606 bfd_get_filename (abfd));
6607 }
6608 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6609 /* For DWOs coming from DWP files, we don't know the CU length
6610 nor the type's offset in the TU until now. */
6611 dwo_unit->length = cu->header.get_length ();
6612 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6613
6614 /* Establish the type offset that can be used to lookup the type.
6615 For DWO files, we don't know it until now. */
6616 sig_type->type_offset_in_section
6617 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6618 }
6619 else
6620 {
6621 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6622 &cu->header, section,
6623 dwo_abbrev_section,
6624 info_ptr, rcuh_kind::COMPILE);
6625 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6626 /* For DWOs coming from DWP files, we don't know the CU length
6627 until now. */
6628 dwo_unit->length = cu->header.get_length ();
6629 }
6630
6631 *result_dwo_abbrev_table
6632 = abbrev_table::read (objfile, dwo_abbrev_section,
6633 cu->header.abbrev_sect_off);
6634 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6635 result_dwo_abbrev_table->get ());
6636
6637 /* Read in the die, but leave space to copy over the attributes
6638 from the stub. This has the benefit of simplifying the rest of
6639 the code - all the work to maintain the illusion of a single
6640 DW_TAG_{compile,type}_unit DIE is done here. */
6641 num_extra_attrs = ((stmt_list != NULL)
6642 + (low_pc != NULL)
6643 + (high_pc != NULL)
6644 + (ranges != NULL)
6645 + (comp_dir != NULL));
6646 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6647 num_extra_attrs);
6648
6649 /* Copy over the attributes from the stub to the DIE we just read in. */
6650 comp_unit_die = *result_comp_unit_die;
6651 i = comp_unit_die->num_attrs;
6652 if (stmt_list != NULL)
6653 comp_unit_die->attrs[i++] = *stmt_list;
6654 if (low_pc != NULL)
6655 comp_unit_die->attrs[i++] = *low_pc;
6656 if (high_pc != NULL)
6657 comp_unit_die->attrs[i++] = *high_pc;
6658 if (ranges != NULL)
6659 comp_unit_die->attrs[i++] = *ranges;
6660 if (comp_dir != NULL)
6661 comp_unit_die->attrs[i++] = *comp_dir;
6662 comp_unit_die->num_attrs += num_extra_attrs;
6663
6664 if (dwarf_die_debug)
6665 {
6666 fprintf_unfiltered (gdb_stdlog,
6667 "Read die from %s@0x%x of %s:\n",
6668 section->get_name (),
6669 (unsigned) (begin_info_ptr - section->buffer),
6670 bfd_get_filename (abfd));
6671 dump_die (comp_unit_die, dwarf_die_debug);
6672 }
6673
6674 /* Skip dummy compilation units. */
6675 if (info_ptr >= begin_info_ptr + dwo_unit->length
6676 || peek_abbrev_code (abfd, info_ptr) == 0)
6677 return 0;
6678
6679 *result_info_ptr = info_ptr;
6680 return 1;
6681 }
6682
6683 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6684 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6685 signature is part of the header. */
6686 static gdb::optional<ULONGEST>
6687 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6688 {
6689 if (cu->header.version >= 5)
6690 return cu->header.signature;
6691 struct attribute *attr;
6692 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6693 if (attr == nullptr)
6694 return gdb::optional<ULONGEST> ();
6695 return DW_UNSND (attr);
6696 }
6697
6698 /* Subroutine of cutu_reader to simplify it.
6699 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6700 Returns NULL if the specified DWO unit cannot be found. */
6701
6702 static struct dwo_unit *
6703 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6704 struct die_info *comp_unit_die,
6705 const char *dwo_name)
6706 {
6707 struct dwarf2_cu *cu = this_cu->cu;
6708 struct dwo_unit *dwo_unit;
6709 const char *comp_dir;
6710
6711 gdb_assert (cu != NULL);
6712
6713 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6714 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6715 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6716
6717 if (this_cu->is_debug_types)
6718 {
6719 struct signatured_type *sig_type;
6720
6721 /* Since this_cu is the first member of struct signatured_type,
6722 we can go from a pointer to one to a pointer to the other. */
6723 sig_type = (struct signatured_type *) this_cu;
6724 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6725 }
6726 else
6727 {
6728 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6729 if (!signature.has_value ())
6730 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6731 " [in module %s]"),
6732 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6733 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6734 *signature);
6735 }
6736
6737 return dwo_unit;
6738 }
6739
6740 /* Subroutine of cutu_reader to simplify it.
6741 See it for a description of the parameters.
6742 Read a TU directly from a DWO file, bypassing the stub. */
6743
6744 void
6745 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6746 int use_existing_cu)
6747 {
6748 struct signatured_type *sig_type;
6749
6750 /* Verify we can do the following downcast, and that we have the
6751 data we need. */
6752 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6753 sig_type = (struct signatured_type *) this_cu;
6754 gdb_assert (sig_type->dwo_unit != NULL);
6755
6756 if (use_existing_cu && this_cu->cu != NULL)
6757 {
6758 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6759 /* There's no need to do the rereading_dwo_cu handling that
6760 cutu_reader does since we don't read the stub. */
6761 }
6762 else
6763 {
6764 /* If !use_existing_cu, this_cu->cu must be NULL. */
6765 gdb_assert (this_cu->cu == NULL);
6766 m_new_cu.reset (new dwarf2_cu (this_cu));
6767 }
6768
6769 /* A future optimization, if needed, would be to use an existing
6770 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6771 could share abbrev tables. */
6772
6773 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6774 NULL /* stub_comp_unit_die */,
6775 sig_type->dwo_unit->dwo_file->comp_dir,
6776 this, &info_ptr,
6777 &comp_unit_die,
6778 &m_dwo_abbrev_table) == 0)
6779 {
6780 /* Dummy die. */
6781 dummy_p = true;
6782 }
6783 }
6784
6785 /* Initialize a CU (or TU) and read its DIEs.
6786 If the CU defers to a DWO file, read the DWO file as well.
6787
6788 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6789 Otherwise the table specified in the comp unit header is read in and used.
6790 This is an optimization for when we already have the abbrev table.
6791
6792 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6793 Otherwise, a new CU is allocated with xmalloc. */
6794
6795 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6796 struct abbrev_table *abbrev_table,
6797 int use_existing_cu,
6798 bool skip_partial)
6799 : die_reader_specs {},
6800 m_this_cu (this_cu)
6801 {
6802 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6803 struct objfile *objfile = dwarf2_per_objfile->objfile;
6804 struct dwarf2_section_info *section = this_cu->section;
6805 bfd *abfd = section->get_bfd_owner ();
6806 struct dwarf2_cu *cu;
6807 const gdb_byte *begin_info_ptr;
6808 struct signatured_type *sig_type = NULL;
6809 struct dwarf2_section_info *abbrev_section;
6810 /* Non-zero if CU currently points to a DWO file and we need to
6811 reread it. When this happens we need to reread the skeleton die
6812 before we can reread the DWO file (this only applies to CUs, not TUs). */
6813 int rereading_dwo_cu = 0;
6814
6815 if (dwarf_die_debug)
6816 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6817 this_cu->is_debug_types ? "type" : "comp",
6818 sect_offset_str (this_cu->sect_off));
6819
6820 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6821 file (instead of going through the stub), short-circuit all of this. */
6822 if (this_cu->reading_dwo_directly)
6823 {
6824 /* Narrow down the scope of possibilities to have to understand. */
6825 gdb_assert (this_cu->is_debug_types);
6826 gdb_assert (abbrev_table == NULL);
6827 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6828 return;
6829 }
6830
6831 /* This is cheap if the section is already read in. */
6832 section->read (objfile);
6833
6834 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6835
6836 abbrev_section = get_abbrev_section_for_cu (this_cu);
6837
6838 if (use_existing_cu && this_cu->cu != NULL)
6839 {
6840 cu = this_cu->cu;
6841 /* If this CU is from a DWO file we need to start over, we need to
6842 refetch the attributes from the skeleton CU.
6843 This could be optimized by retrieving those attributes from when we
6844 were here the first time: the previous comp_unit_die was stored in
6845 comp_unit_obstack. But there's no data yet that we need this
6846 optimization. */
6847 if (cu->dwo_unit != NULL)
6848 rereading_dwo_cu = 1;
6849 }
6850 else
6851 {
6852 /* If !use_existing_cu, this_cu->cu must be NULL. */
6853 gdb_assert (this_cu->cu == NULL);
6854 m_new_cu.reset (new dwarf2_cu (this_cu));
6855 cu = m_new_cu.get ();
6856 }
6857
6858 /* Get the header. */
6859 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6860 {
6861 /* We already have the header, there's no need to read it in again. */
6862 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6863 }
6864 else
6865 {
6866 if (this_cu->is_debug_types)
6867 {
6868 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6869 &cu->header, section,
6870 abbrev_section, info_ptr,
6871 rcuh_kind::TYPE);
6872
6873 /* Since per_cu is the first member of struct signatured_type,
6874 we can go from a pointer to one to a pointer to the other. */
6875 sig_type = (struct signatured_type *) this_cu;
6876 gdb_assert (sig_type->signature == cu->header.signature);
6877 gdb_assert (sig_type->type_offset_in_tu
6878 == cu->header.type_cu_offset_in_tu);
6879 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6880
6881 /* LENGTH has not been set yet for type units if we're
6882 using .gdb_index. */
6883 this_cu->length = cu->header.get_length ();
6884
6885 /* Establish the type offset that can be used to lookup the type. */
6886 sig_type->type_offset_in_section =
6887 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6888
6889 this_cu->dwarf_version = cu->header.version;
6890 }
6891 else
6892 {
6893 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6894 &cu->header, section,
6895 abbrev_section,
6896 info_ptr,
6897 rcuh_kind::COMPILE);
6898
6899 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6900 gdb_assert (this_cu->length == cu->header.get_length ());
6901 this_cu->dwarf_version = cu->header.version;
6902 }
6903 }
6904
6905 /* Skip dummy compilation units. */
6906 if (info_ptr >= begin_info_ptr + this_cu->length
6907 || peek_abbrev_code (abfd, info_ptr) == 0)
6908 {
6909 dummy_p = true;
6910 return;
6911 }
6912
6913 /* If we don't have them yet, read the abbrevs for this compilation unit.
6914 And if we need to read them now, make sure they're freed when we're
6915 done. */
6916 if (abbrev_table != NULL)
6917 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6918 else
6919 {
6920 m_abbrev_table_holder
6921 = abbrev_table::read (objfile, abbrev_section,
6922 cu->header.abbrev_sect_off);
6923 abbrev_table = m_abbrev_table_holder.get ();
6924 }
6925
6926 /* Read the top level CU/TU die. */
6927 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6928 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6929
6930 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6931 {
6932 dummy_p = true;
6933 return;
6934 }
6935
6936 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6937 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6938 table from the DWO file and pass the ownership over to us. It will be
6939 referenced from READER, so we must make sure to free it after we're done
6940 with READER.
6941
6942 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6943 DWO CU, that this test will fail (the attribute will not be present). */
6944 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6945 if (dwo_name != nullptr)
6946 {
6947 struct dwo_unit *dwo_unit;
6948 struct die_info *dwo_comp_unit_die;
6949
6950 if (comp_unit_die->has_children)
6951 {
6952 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6953 " has children (offset %s) [in module %s]"),
6954 sect_offset_str (this_cu->sect_off),
6955 bfd_get_filename (abfd));
6956 }
6957 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6958 if (dwo_unit != NULL)
6959 {
6960 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6961 comp_unit_die, NULL,
6962 this, &info_ptr,
6963 &dwo_comp_unit_die,
6964 &m_dwo_abbrev_table) == 0)
6965 {
6966 /* Dummy die. */
6967 dummy_p = true;
6968 return;
6969 }
6970 comp_unit_die = dwo_comp_unit_die;
6971 }
6972 else
6973 {
6974 /* Yikes, we couldn't find the rest of the DIE, we only have
6975 the stub. A complaint has already been logged. There's
6976 not much more we can do except pass on the stub DIE to
6977 die_reader_func. We don't want to throw an error on bad
6978 debug info. */
6979 }
6980 }
6981 }
6982
6983 void
6984 cutu_reader::keep ()
6985 {
6986 /* Done, clean up. */
6987 gdb_assert (!dummy_p);
6988 if (m_new_cu != NULL)
6989 {
6990 struct dwarf2_per_objfile *dwarf2_per_objfile
6991 = m_this_cu->dwarf2_per_objfile;
6992 /* Link this CU into read_in_chain. */
6993 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6994 dwarf2_per_objfile->read_in_chain = m_this_cu;
6995 /* The chain owns it now. */
6996 m_new_cu.release ();
6997 }
6998 }
6999
7000 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7001 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7002 assumed to have already done the lookup to find the DWO file).
7003
7004 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7005 THIS_CU->is_debug_types, but nothing else.
7006
7007 We fill in THIS_CU->length.
7008
7009 THIS_CU->cu is always freed when done.
7010 This is done in order to not leave THIS_CU->cu in a state where we have
7011 to care whether it refers to the "main" CU or the DWO CU.
7012
7013 When parent_cu is passed, it is used to provide a default value for
7014 str_offsets_base and addr_base from the parent. */
7015
7016 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7017 struct dwarf2_cu *parent_cu,
7018 struct dwo_file *dwo_file)
7019 : die_reader_specs {},
7020 m_this_cu (this_cu)
7021 {
7022 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7023 struct objfile *objfile = dwarf2_per_objfile->objfile;
7024 struct dwarf2_section_info *section = this_cu->section;
7025 bfd *abfd = section->get_bfd_owner ();
7026 struct dwarf2_section_info *abbrev_section;
7027 const gdb_byte *begin_info_ptr, *info_ptr;
7028
7029 if (dwarf_die_debug)
7030 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7031 this_cu->is_debug_types ? "type" : "comp",
7032 sect_offset_str (this_cu->sect_off));
7033
7034 gdb_assert (this_cu->cu == NULL);
7035
7036 abbrev_section = (dwo_file != NULL
7037 ? &dwo_file->sections.abbrev
7038 : get_abbrev_section_for_cu (this_cu));
7039
7040 /* This is cheap if the section is already read in. */
7041 section->read (objfile);
7042
7043 m_new_cu.reset (new dwarf2_cu (this_cu));
7044
7045 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7046 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7047 &m_new_cu->header, section,
7048 abbrev_section, info_ptr,
7049 (this_cu->is_debug_types
7050 ? rcuh_kind::TYPE
7051 : rcuh_kind::COMPILE));
7052
7053 if (parent_cu != nullptr)
7054 {
7055 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7056 m_new_cu->addr_base = parent_cu->addr_base;
7057 }
7058 this_cu->length = m_new_cu->header.get_length ();
7059
7060 /* Skip dummy compilation units. */
7061 if (info_ptr >= begin_info_ptr + this_cu->length
7062 || peek_abbrev_code (abfd, info_ptr) == 0)
7063 {
7064 dummy_p = true;
7065 return;
7066 }
7067
7068 m_abbrev_table_holder
7069 = abbrev_table::read (objfile, abbrev_section,
7070 m_new_cu->header.abbrev_sect_off);
7071
7072 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7073 m_abbrev_table_holder.get ());
7074 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7075 }
7076
7077 \f
7078 /* Type Unit Groups.
7079
7080 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7081 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7082 so that all types coming from the same compilation (.o file) are grouped
7083 together. A future step could be to put the types in the same symtab as
7084 the CU the types ultimately came from. */
7085
7086 static hashval_t
7087 hash_type_unit_group (const void *item)
7088 {
7089 const struct type_unit_group *tu_group
7090 = (const struct type_unit_group *) item;
7091
7092 return hash_stmt_list_entry (&tu_group->hash);
7093 }
7094
7095 static int
7096 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7097 {
7098 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7099 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7100
7101 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7102 }
7103
7104 /* Allocate a hash table for type unit groups. */
7105
7106 static htab_up
7107 allocate_type_unit_groups_table ()
7108 {
7109 return htab_up (htab_create_alloc (3,
7110 hash_type_unit_group,
7111 eq_type_unit_group,
7112 NULL, xcalloc, xfree));
7113 }
7114
7115 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7116 partial symtabs. We combine several TUs per psymtab to not let the size
7117 of any one psymtab grow too big. */
7118 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7119 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7120
7121 /* Helper routine for get_type_unit_group.
7122 Create the type_unit_group object used to hold one or more TUs. */
7123
7124 static struct type_unit_group *
7125 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7126 {
7127 struct dwarf2_per_objfile *dwarf2_per_objfile
7128 = cu->per_cu->dwarf2_per_objfile;
7129 struct objfile *objfile = dwarf2_per_objfile->objfile;
7130 struct dwarf2_per_cu_data *per_cu;
7131 struct type_unit_group *tu_group;
7132
7133 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7134 struct type_unit_group);
7135 per_cu = &tu_group->per_cu;
7136 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7137
7138 if (dwarf2_per_objfile->using_index)
7139 {
7140 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7141 struct dwarf2_per_cu_quick_data);
7142 }
7143 else
7144 {
7145 unsigned int line_offset = to_underlying (line_offset_struct);
7146 dwarf2_psymtab *pst;
7147 std::string name;
7148
7149 /* Give the symtab a useful name for debug purposes. */
7150 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7151 name = string_printf ("<type_units_%d>",
7152 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7153 else
7154 name = string_printf ("<type_units_at_0x%x>", line_offset);
7155
7156 pst = create_partial_symtab (per_cu, name.c_str ());
7157 pst->anonymous = true;
7158 }
7159
7160 tu_group->hash.dwo_unit = cu->dwo_unit;
7161 tu_group->hash.line_sect_off = line_offset_struct;
7162
7163 return tu_group;
7164 }
7165
7166 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7167 STMT_LIST is a DW_AT_stmt_list attribute. */
7168
7169 static struct type_unit_group *
7170 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7171 {
7172 struct dwarf2_per_objfile *dwarf2_per_objfile
7173 = cu->per_cu->dwarf2_per_objfile;
7174 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7175 struct type_unit_group *tu_group;
7176 void **slot;
7177 unsigned int line_offset;
7178 struct type_unit_group type_unit_group_for_lookup;
7179
7180 if (dwarf2_per_objfile->type_unit_groups == NULL)
7181 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
7182
7183 /* Do we need to create a new group, or can we use an existing one? */
7184
7185 if (stmt_list)
7186 {
7187 line_offset = DW_UNSND (stmt_list);
7188 ++tu_stats->nr_symtab_sharers;
7189 }
7190 else
7191 {
7192 /* Ugh, no stmt_list. Rare, but we have to handle it.
7193 We can do various things here like create one group per TU or
7194 spread them over multiple groups to split up the expansion work.
7195 To avoid worst case scenarios (too many groups or too large groups)
7196 we, umm, group them in bunches. */
7197 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7198 | (tu_stats->nr_stmt_less_type_units
7199 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7200 ++tu_stats->nr_stmt_less_type_units;
7201 }
7202
7203 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7204 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7205 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7206 &type_unit_group_for_lookup, INSERT);
7207 if (*slot != NULL)
7208 {
7209 tu_group = (struct type_unit_group *) *slot;
7210 gdb_assert (tu_group != NULL);
7211 }
7212 else
7213 {
7214 sect_offset line_offset_struct = (sect_offset) line_offset;
7215 tu_group = create_type_unit_group (cu, line_offset_struct);
7216 *slot = tu_group;
7217 ++tu_stats->nr_symtabs;
7218 }
7219
7220 return tu_group;
7221 }
7222 \f
7223 /* Partial symbol tables. */
7224
7225 /* Create a psymtab named NAME and assign it to PER_CU.
7226
7227 The caller must fill in the following details:
7228 dirname, textlow, texthigh. */
7229
7230 static dwarf2_psymtab *
7231 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7232 {
7233 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7234 dwarf2_psymtab *pst;
7235
7236 pst = new dwarf2_psymtab (name, objfile, per_cu);
7237
7238 pst->psymtabs_addrmap_supported = true;
7239
7240 /* This is the glue that links PST into GDB's symbol API. */
7241 per_cu->v.psymtab = pst;
7242
7243 return pst;
7244 }
7245
7246 /* DIE reader function for process_psymtab_comp_unit. */
7247
7248 static void
7249 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7250 const gdb_byte *info_ptr,
7251 struct die_info *comp_unit_die,
7252 enum language pretend_language)
7253 {
7254 struct dwarf2_cu *cu = reader->cu;
7255 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7256 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7257 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7258 CORE_ADDR baseaddr;
7259 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7260 dwarf2_psymtab *pst;
7261 enum pc_bounds_kind cu_bounds_kind;
7262 const char *filename;
7263
7264 gdb_assert (! per_cu->is_debug_types);
7265
7266 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7267
7268 /* Allocate a new partial symbol table structure. */
7269 gdb::unique_xmalloc_ptr<char> debug_filename;
7270 static const char artificial[] = "<artificial>";
7271 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7272 if (filename == NULL)
7273 filename = "";
7274 else if (strcmp (filename, artificial) == 0)
7275 {
7276 debug_filename.reset (concat (artificial, "@",
7277 sect_offset_str (per_cu->sect_off),
7278 (char *) NULL));
7279 filename = debug_filename.get ();
7280 }
7281
7282 pst = create_partial_symtab (per_cu, filename);
7283
7284 /* This must be done before calling dwarf2_build_include_psymtabs. */
7285 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7286
7287 baseaddr = objfile->text_section_offset ();
7288
7289 dwarf2_find_base_address (comp_unit_die, cu);
7290
7291 /* Possibly set the default values of LOWPC and HIGHPC from
7292 `DW_AT_ranges'. */
7293 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7294 &best_highpc, cu, pst);
7295 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7296 {
7297 CORE_ADDR low
7298 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7299 - baseaddr);
7300 CORE_ADDR high
7301 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7302 - baseaddr - 1);
7303 /* Store the contiguous range if it is not empty; it can be
7304 empty for CUs with no code. */
7305 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7306 low, high, pst);
7307 }
7308
7309 /* Check if comp unit has_children.
7310 If so, read the rest of the partial symbols from this comp unit.
7311 If not, there's no more debug_info for this comp unit. */
7312 if (comp_unit_die->has_children)
7313 {
7314 struct partial_die_info *first_die;
7315 CORE_ADDR lowpc, highpc;
7316
7317 lowpc = ((CORE_ADDR) -1);
7318 highpc = ((CORE_ADDR) 0);
7319
7320 first_die = load_partial_dies (reader, info_ptr, 1);
7321
7322 scan_partial_symbols (first_die, &lowpc, &highpc,
7323 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7324
7325 /* If we didn't find a lowpc, set it to highpc to avoid
7326 complaints from `maint check'. */
7327 if (lowpc == ((CORE_ADDR) -1))
7328 lowpc = highpc;
7329
7330 /* If the compilation unit didn't have an explicit address range,
7331 then use the information extracted from its child dies. */
7332 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7333 {
7334 best_lowpc = lowpc;
7335 best_highpc = highpc;
7336 }
7337 }
7338 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7339 best_lowpc + baseaddr)
7340 - baseaddr);
7341 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7342 best_highpc + baseaddr)
7343 - baseaddr);
7344
7345 end_psymtab_common (objfile, pst);
7346
7347 if (!cu->per_cu->imported_symtabs_empty ())
7348 {
7349 int i;
7350 int len = cu->per_cu->imported_symtabs_size ();
7351
7352 /* Fill in 'dependencies' here; we fill in 'users' in a
7353 post-pass. */
7354 pst->number_of_dependencies = len;
7355 pst->dependencies
7356 = objfile->partial_symtabs->allocate_dependencies (len);
7357 for (i = 0; i < len; ++i)
7358 {
7359 pst->dependencies[i]
7360 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7361 }
7362
7363 cu->per_cu->imported_symtabs_free ();
7364 }
7365
7366 /* Get the list of files included in the current compilation unit,
7367 and build a psymtab for each of them. */
7368 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7369
7370 if (dwarf_read_debug)
7371 fprintf_unfiltered (gdb_stdlog,
7372 "Psymtab for %s unit @%s: %s - %s"
7373 ", %d global, %d static syms\n",
7374 per_cu->is_debug_types ? "type" : "comp",
7375 sect_offset_str (per_cu->sect_off),
7376 paddress (gdbarch, pst->text_low (objfile)),
7377 paddress (gdbarch, pst->text_high (objfile)),
7378 pst->n_global_syms, pst->n_static_syms);
7379 }
7380
7381 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7382 Process compilation unit THIS_CU for a psymtab. */
7383
7384 static void
7385 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7386 bool want_partial_unit,
7387 enum language pretend_language)
7388 {
7389 /* If this compilation unit was already read in, free the
7390 cached copy in order to read it in again. This is
7391 necessary because we skipped some symbols when we first
7392 read in the compilation unit (see load_partial_dies).
7393 This problem could be avoided, but the benefit is unclear. */
7394 if (this_cu->cu != NULL)
7395 free_one_cached_comp_unit (this_cu);
7396
7397 cutu_reader reader (this_cu, NULL, 0, false);
7398
7399 switch (reader.comp_unit_die->tag)
7400 {
7401 case DW_TAG_compile_unit:
7402 this_cu->unit_type = DW_UT_compile;
7403 break;
7404 case DW_TAG_partial_unit:
7405 this_cu->unit_type = DW_UT_partial;
7406 break;
7407 default:
7408 abort ();
7409 }
7410
7411 if (reader.dummy_p)
7412 {
7413 /* Nothing. */
7414 }
7415 else if (this_cu->is_debug_types)
7416 build_type_psymtabs_reader (&reader, reader.info_ptr,
7417 reader.comp_unit_die);
7418 else if (want_partial_unit
7419 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7420 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7421 reader.comp_unit_die,
7422 pretend_language);
7423
7424 this_cu->lang = this_cu->cu->language;
7425
7426 /* Age out any secondary CUs. */
7427 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7428 }
7429
7430 /* Reader function for build_type_psymtabs. */
7431
7432 static void
7433 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7434 const gdb_byte *info_ptr,
7435 struct die_info *type_unit_die)
7436 {
7437 struct dwarf2_per_objfile *dwarf2_per_objfile
7438 = reader->cu->per_cu->dwarf2_per_objfile;
7439 struct objfile *objfile = dwarf2_per_objfile->objfile;
7440 struct dwarf2_cu *cu = reader->cu;
7441 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7442 struct signatured_type *sig_type;
7443 struct type_unit_group *tu_group;
7444 struct attribute *attr;
7445 struct partial_die_info *first_die;
7446 CORE_ADDR lowpc, highpc;
7447 dwarf2_psymtab *pst;
7448
7449 gdb_assert (per_cu->is_debug_types);
7450 sig_type = (struct signatured_type *) per_cu;
7451
7452 if (! type_unit_die->has_children)
7453 return;
7454
7455 attr = type_unit_die->attr (DW_AT_stmt_list);
7456 tu_group = get_type_unit_group (cu, attr);
7457
7458 if (tu_group->tus == nullptr)
7459 tu_group->tus = new std::vector<signatured_type *>;
7460 tu_group->tus->push_back (sig_type);
7461
7462 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7463 pst = create_partial_symtab (per_cu, "");
7464 pst->anonymous = true;
7465
7466 first_die = load_partial_dies (reader, info_ptr, 1);
7467
7468 lowpc = (CORE_ADDR) -1;
7469 highpc = (CORE_ADDR) 0;
7470 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7471
7472 end_psymtab_common (objfile, pst);
7473 }
7474
7475 /* Struct used to sort TUs by their abbreviation table offset. */
7476
7477 struct tu_abbrev_offset
7478 {
7479 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7480 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7481 {}
7482
7483 signatured_type *sig_type;
7484 sect_offset abbrev_offset;
7485 };
7486
7487 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7488
7489 static bool
7490 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7491 const struct tu_abbrev_offset &b)
7492 {
7493 return a.abbrev_offset < b.abbrev_offset;
7494 }
7495
7496 /* Efficiently read all the type units.
7497 This does the bulk of the work for build_type_psymtabs.
7498
7499 The efficiency is because we sort TUs by the abbrev table they use and
7500 only read each abbrev table once. In one program there are 200K TUs
7501 sharing 8K abbrev tables.
7502
7503 The main purpose of this function is to support building the
7504 dwarf2_per_objfile->type_unit_groups table.
7505 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7506 can collapse the search space by grouping them by stmt_list.
7507 The savings can be significant, in the same program from above the 200K TUs
7508 share 8K stmt_list tables.
7509
7510 FUNC is expected to call get_type_unit_group, which will create the
7511 struct type_unit_group if necessary and add it to
7512 dwarf2_per_objfile->type_unit_groups. */
7513
7514 static void
7515 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7516 {
7517 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7518 abbrev_table_up abbrev_table;
7519 sect_offset abbrev_offset;
7520
7521 /* It's up to the caller to not call us multiple times. */
7522 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7523
7524 if (dwarf2_per_objfile->all_type_units.empty ())
7525 return;
7526
7527 /* TUs typically share abbrev tables, and there can be way more TUs than
7528 abbrev tables. Sort by abbrev table to reduce the number of times we
7529 read each abbrev table in.
7530 Alternatives are to punt or to maintain a cache of abbrev tables.
7531 This is simpler and efficient enough for now.
7532
7533 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7534 symtab to use). Typically TUs with the same abbrev offset have the same
7535 stmt_list value too so in practice this should work well.
7536
7537 The basic algorithm here is:
7538
7539 sort TUs by abbrev table
7540 for each TU with same abbrev table:
7541 read abbrev table if first user
7542 read TU top level DIE
7543 [IWBN if DWO skeletons had DW_AT_stmt_list]
7544 call FUNC */
7545
7546 if (dwarf_read_debug)
7547 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7548
7549 /* Sort in a separate table to maintain the order of all_type_units
7550 for .gdb_index: TU indices directly index all_type_units. */
7551 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7552 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7553
7554 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7555 sorted_by_abbrev.emplace_back
7556 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7557 sig_type->per_cu.section,
7558 sig_type->per_cu.sect_off));
7559
7560 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7561 sort_tu_by_abbrev_offset);
7562
7563 abbrev_offset = (sect_offset) ~(unsigned) 0;
7564
7565 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7566 {
7567 /* Switch to the next abbrev table if necessary. */
7568 if (abbrev_table == NULL
7569 || tu.abbrev_offset != abbrev_offset)
7570 {
7571 abbrev_offset = tu.abbrev_offset;
7572 abbrev_table =
7573 abbrev_table::read (dwarf2_per_objfile->objfile,
7574 &dwarf2_per_objfile->abbrev,
7575 abbrev_offset);
7576 ++tu_stats->nr_uniq_abbrev_tables;
7577 }
7578
7579 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7580 0, false);
7581 if (!reader.dummy_p)
7582 build_type_psymtabs_reader (&reader, reader.info_ptr,
7583 reader.comp_unit_die);
7584 }
7585 }
7586
7587 /* Print collected type unit statistics. */
7588
7589 static void
7590 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7591 {
7592 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7593
7594 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7595 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7596 dwarf2_per_objfile->all_type_units.size ());
7597 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7598 tu_stats->nr_uniq_abbrev_tables);
7599 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7600 tu_stats->nr_symtabs);
7601 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7602 tu_stats->nr_symtab_sharers);
7603 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7604 tu_stats->nr_stmt_less_type_units);
7605 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7606 tu_stats->nr_all_type_units_reallocs);
7607 }
7608
7609 /* Traversal function for build_type_psymtabs. */
7610
7611 static int
7612 build_type_psymtab_dependencies (void **slot, void *info)
7613 {
7614 struct dwarf2_per_objfile *dwarf2_per_objfile
7615 = (struct dwarf2_per_objfile *) info;
7616 struct objfile *objfile = dwarf2_per_objfile->objfile;
7617 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7618 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7619 dwarf2_psymtab *pst = per_cu->v.psymtab;
7620 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7621 int i;
7622
7623 gdb_assert (len > 0);
7624 gdb_assert (per_cu->type_unit_group_p ());
7625
7626 pst->number_of_dependencies = len;
7627 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7628 for (i = 0; i < len; ++i)
7629 {
7630 struct signatured_type *iter = tu_group->tus->at (i);
7631 gdb_assert (iter->per_cu.is_debug_types);
7632 pst->dependencies[i] = iter->per_cu.v.psymtab;
7633 iter->type_unit_group = tu_group;
7634 }
7635
7636 delete tu_group->tus;
7637 tu_group->tus = nullptr;
7638
7639 return 1;
7640 }
7641
7642 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7643 Build partial symbol tables for the .debug_types comp-units. */
7644
7645 static void
7646 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7647 {
7648 if (! create_all_type_units (dwarf2_per_objfile))
7649 return;
7650
7651 build_type_psymtabs_1 (dwarf2_per_objfile);
7652 }
7653
7654 /* Traversal function for process_skeletonless_type_unit.
7655 Read a TU in a DWO file and build partial symbols for it. */
7656
7657 static int
7658 process_skeletonless_type_unit (void **slot, void *info)
7659 {
7660 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7661 struct dwarf2_per_objfile *dwarf2_per_objfile
7662 = (struct dwarf2_per_objfile *) info;
7663 struct signatured_type find_entry, *entry;
7664
7665 /* If this TU doesn't exist in the global table, add it and read it in. */
7666
7667 if (dwarf2_per_objfile->signatured_types == NULL)
7668 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
7669
7670 find_entry.signature = dwo_unit->signature;
7671 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7672 &find_entry, INSERT);
7673 /* If we've already seen this type there's nothing to do. What's happening
7674 is we're doing our own version of comdat-folding here. */
7675 if (*slot != NULL)
7676 return 1;
7677
7678 /* This does the job that create_all_type_units would have done for
7679 this TU. */
7680 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7681 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7682 *slot = entry;
7683
7684 /* This does the job that build_type_psymtabs_1 would have done. */
7685 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7686 if (!reader.dummy_p)
7687 build_type_psymtabs_reader (&reader, reader.info_ptr,
7688 reader.comp_unit_die);
7689
7690 return 1;
7691 }
7692
7693 /* Traversal function for process_skeletonless_type_units. */
7694
7695 static int
7696 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7697 {
7698 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7699
7700 if (dwo_file->tus != NULL)
7701 htab_traverse_noresize (dwo_file->tus.get (),
7702 process_skeletonless_type_unit, info);
7703
7704 return 1;
7705 }
7706
7707 /* Scan all TUs of DWO files, verifying we've processed them.
7708 This is needed in case a TU was emitted without its skeleton.
7709 Note: This can't be done until we know what all the DWO files are. */
7710
7711 static void
7712 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7713 {
7714 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7715 if (get_dwp_file (dwarf2_per_objfile) == NULL
7716 && dwarf2_per_objfile->dwo_files != NULL)
7717 {
7718 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7719 process_dwo_file_for_skeletonless_type_units,
7720 dwarf2_per_objfile);
7721 }
7722 }
7723
7724 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7725
7726 static void
7727 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7728 {
7729 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7730 {
7731 dwarf2_psymtab *pst = per_cu->v.psymtab;
7732
7733 if (pst == NULL)
7734 continue;
7735
7736 for (int j = 0; j < pst->number_of_dependencies; ++j)
7737 {
7738 /* Set the 'user' field only if it is not already set. */
7739 if (pst->dependencies[j]->user == NULL)
7740 pst->dependencies[j]->user = pst;
7741 }
7742 }
7743 }
7744
7745 /* Build the partial symbol table by doing a quick pass through the
7746 .debug_info and .debug_abbrev sections. */
7747
7748 static void
7749 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7750 {
7751 struct objfile *objfile = dwarf2_per_objfile->objfile;
7752
7753 if (dwarf_read_debug)
7754 {
7755 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7756 objfile_name (objfile));
7757 }
7758
7759 scoped_restore restore_reading_psyms
7760 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7761 true);
7762
7763 dwarf2_per_objfile->info.read (objfile);
7764
7765 /* Any cached compilation units will be linked by the per-objfile
7766 read_in_chain. Make sure to free them when we're done. */
7767 free_cached_comp_units freer (dwarf2_per_objfile);
7768
7769 build_type_psymtabs (dwarf2_per_objfile);
7770
7771 create_all_comp_units (dwarf2_per_objfile);
7772
7773 /* Create a temporary address map on a temporary obstack. We later
7774 copy this to the final obstack. */
7775 auto_obstack temp_obstack;
7776
7777 scoped_restore save_psymtabs_addrmap
7778 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7779 addrmap_create_mutable (&temp_obstack));
7780
7781 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7782 process_psymtab_comp_unit (per_cu, false, language_minimal);
7783
7784 /* This has to wait until we read the CUs, we need the list of DWOs. */
7785 process_skeletonless_type_units (dwarf2_per_objfile);
7786
7787 /* Now that all TUs have been processed we can fill in the dependencies. */
7788 if (dwarf2_per_objfile->type_unit_groups != NULL)
7789 {
7790 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7791 build_type_psymtab_dependencies, dwarf2_per_objfile);
7792 }
7793
7794 if (dwarf_read_debug)
7795 print_tu_stats (dwarf2_per_objfile);
7796
7797 set_partial_user (dwarf2_per_objfile);
7798
7799 objfile->partial_symtabs->psymtabs_addrmap
7800 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7801 objfile->partial_symtabs->obstack ());
7802 /* At this point we want to keep the address map. */
7803 save_psymtabs_addrmap.release ();
7804
7805 if (dwarf_read_debug)
7806 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7807 objfile_name (objfile));
7808 }
7809
7810 /* Load the partial DIEs for a secondary CU into memory.
7811 This is also used when rereading a primary CU with load_all_dies. */
7812
7813 static void
7814 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7815 {
7816 cutu_reader reader (this_cu, NULL, 1, false);
7817
7818 if (!reader.dummy_p)
7819 {
7820 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7821 language_minimal);
7822
7823 /* Check if comp unit has_children.
7824 If so, read the rest of the partial symbols from this comp unit.
7825 If not, there's no more debug_info for this comp unit. */
7826 if (reader.comp_unit_die->has_children)
7827 load_partial_dies (&reader, reader.info_ptr, 0);
7828
7829 reader.keep ();
7830 }
7831 }
7832
7833 static void
7834 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7835 struct dwarf2_section_info *section,
7836 struct dwarf2_section_info *abbrev_section,
7837 unsigned int is_dwz)
7838 {
7839 const gdb_byte *info_ptr;
7840 struct objfile *objfile = dwarf2_per_objfile->objfile;
7841
7842 if (dwarf_read_debug)
7843 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7844 section->get_name (),
7845 section->get_file_name ());
7846
7847 section->read (objfile);
7848
7849 info_ptr = section->buffer;
7850
7851 while (info_ptr < section->buffer + section->size)
7852 {
7853 struct dwarf2_per_cu_data *this_cu;
7854
7855 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7856
7857 comp_unit_head cu_header;
7858 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7859 abbrev_section, info_ptr,
7860 rcuh_kind::COMPILE);
7861
7862 /* Save the compilation unit for later lookup. */
7863 if (cu_header.unit_type != DW_UT_type)
7864 {
7865 this_cu = XOBNEW (&objfile->objfile_obstack,
7866 struct dwarf2_per_cu_data);
7867 memset (this_cu, 0, sizeof (*this_cu));
7868 }
7869 else
7870 {
7871 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7872 struct signatured_type);
7873 memset (sig_type, 0, sizeof (*sig_type));
7874 sig_type->signature = cu_header.signature;
7875 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7876 this_cu = &sig_type->per_cu;
7877 }
7878 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7879 this_cu->sect_off = sect_off;
7880 this_cu->length = cu_header.length + cu_header.initial_length_size;
7881 this_cu->is_dwz = is_dwz;
7882 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7883 this_cu->section = section;
7884
7885 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7886
7887 info_ptr = info_ptr + this_cu->length;
7888 }
7889 }
7890
7891 /* Create a list of all compilation units in OBJFILE.
7892 This is only done for -readnow and building partial symtabs. */
7893
7894 static void
7895 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7896 {
7897 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7898 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7899 &dwarf2_per_objfile->abbrev, 0);
7900
7901 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7902 if (dwz != NULL)
7903 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7904 1);
7905 }
7906
7907 /* Process all loaded DIEs for compilation unit CU, starting at
7908 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7909 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7910 DW_AT_ranges). See the comments of add_partial_subprogram on how
7911 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7912
7913 static void
7914 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7915 CORE_ADDR *highpc, int set_addrmap,
7916 struct dwarf2_cu *cu)
7917 {
7918 struct partial_die_info *pdi;
7919
7920 /* Now, march along the PDI's, descending into ones which have
7921 interesting children but skipping the children of the other ones,
7922 until we reach the end of the compilation unit. */
7923
7924 pdi = first_die;
7925
7926 while (pdi != NULL)
7927 {
7928 pdi->fixup (cu);
7929
7930 /* Anonymous namespaces or modules have no name but have interesting
7931 children, so we need to look at them. Ditto for anonymous
7932 enums. */
7933
7934 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7935 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7936 || pdi->tag == DW_TAG_imported_unit
7937 || pdi->tag == DW_TAG_inlined_subroutine)
7938 {
7939 switch (pdi->tag)
7940 {
7941 case DW_TAG_subprogram:
7942 case DW_TAG_inlined_subroutine:
7943 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7944 break;
7945 case DW_TAG_constant:
7946 case DW_TAG_variable:
7947 case DW_TAG_typedef:
7948 case DW_TAG_union_type:
7949 if (!pdi->is_declaration)
7950 {
7951 add_partial_symbol (pdi, cu);
7952 }
7953 break;
7954 case DW_TAG_class_type:
7955 case DW_TAG_interface_type:
7956 case DW_TAG_structure_type:
7957 if (!pdi->is_declaration)
7958 {
7959 add_partial_symbol (pdi, cu);
7960 }
7961 if ((cu->language == language_rust
7962 || cu->language == language_cplus) && pdi->has_children)
7963 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7964 set_addrmap, cu);
7965 break;
7966 case DW_TAG_enumeration_type:
7967 if (!pdi->is_declaration)
7968 add_partial_enumeration (pdi, cu);
7969 break;
7970 case DW_TAG_base_type:
7971 case DW_TAG_subrange_type:
7972 /* File scope base type definitions are added to the partial
7973 symbol table. */
7974 add_partial_symbol (pdi, cu);
7975 break;
7976 case DW_TAG_namespace:
7977 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7978 break;
7979 case DW_TAG_module:
7980 if (!pdi->is_declaration)
7981 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7982 break;
7983 case DW_TAG_imported_unit:
7984 {
7985 struct dwarf2_per_cu_data *per_cu;
7986
7987 /* For now we don't handle imported units in type units. */
7988 if (cu->per_cu->is_debug_types)
7989 {
7990 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7991 " supported in type units [in module %s]"),
7992 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7993 }
7994
7995 per_cu = dwarf2_find_containing_comp_unit
7996 (pdi->d.sect_off, pdi->is_dwz,
7997 cu->per_cu->dwarf2_per_objfile);
7998
7999 /* Go read the partial unit, if needed. */
8000 if (per_cu->v.psymtab == NULL)
8001 process_psymtab_comp_unit (per_cu, true, cu->language);
8002
8003 cu->per_cu->imported_symtabs_push (per_cu);
8004 }
8005 break;
8006 case DW_TAG_imported_declaration:
8007 add_partial_symbol (pdi, cu);
8008 break;
8009 default:
8010 break;
8011 }
8012 }
8013
8014 /* If the die has a sibling, skip to the sibling. */
8015
8016 pdi = pdi->die_sibling;
8017 }
8018 }
8019
8020 /* Functions used to compute the fully scoped name of a partial DIE.
8021
8022 Normally, this is simple. For C++, the parent DIE's fully scoped
8023 name is concatenated with "::" and the partial DIE's name.
8024 Enumerators are an exception; they use the scope of their parent
8025 enumeration type, i.e. the name of the enumeration type is not
8026 prepended to the enumerator.
8027
8028 There are two complexities. One is DW_AT_specification; in this
8029 case "parent" means the parent of the target of the specification,
8030 instead of the direct parent of the DIE. The other is compilers
8031 which do not emit DW_TAG_namespace; in this case we try to guess
8032 the fully qualified name of structure types from their members'
8033 linkage names. This must be done using the DIE's children rather
8034 than the children of any DW_AT_specification target. We only need
8035 to do this for structures at the top level, i.e. if the target of
8036 any DW_AT_specification (if any; otherwise the DIE itself) does not
8037 have a parent. */
8038
8039 /* Compute the scope prefix associated with PDI's parent, in
8040 compilation unit CU. The result will be allocated on CU's
8041 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8042 field. NULL is returned if no prefix is necessary. */
8043 static const char *
8044 partial_die_parent_scope (struct partial_die_info *pdi,
8045 struct dwarf2_cu *cu)
8046 {
8047 const char *grandparent_scope;
8048 struct partial_die_info *parent, *real_pdi;
8049
8050 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8051 then this means the parent of the specification DIE. */
8052
8053 real_pdi = pdi;
8054 while (real_pdi->has_specification)
8055 {
8056 auto res = find_partial_die (real_pdi->spec_offset,
8057 real_pdi->spec_is_dwz, cu);
8058 real_pdi = res.pdi;
8059 cu = res.cu;
8060 }
8061
8062 parent = real_pdi->die_parent;
8063 if (parent == NULL)
8064 return NULL;
8065
8066 if (parent->scope_set)
8067 return parent->scope;
8068
8069 parent->fixup (cu);
8070
8071 grandparent_scope = partial_die_parent_scope (parent, cu);
8072
8073 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8074 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8075 Work around this problem here. */
8076 if (cu->language == language_cplus
8077 && parent->tag == DW_TAG_namespace
8078 && strcmp (parent->name, "::") == 0
8079 && grandparent_scope == NULL)
8080 {
8081 parent->scope = NULL;
8082 parent->scope_set = 1;
8083 return NULL;
8084 }
8085
8086 /* Nested subroutines in Fortran get a prefix. */
8087 if (pdi->tag == DW_TAG_enumerator)
8088 /* Enumerators should not get the name of the enumeration as a prefix. */
8089 parent->scope = grandparent_scope;
8090 else if (parent->tag == DW_TAG_namespace
8091 || parent->tag == DW_TAG_module
8092 || parent->tag == DW_TAG_structure_type
8093 || parent->tag == DW_TAG_class_type
8094 || parent->tag == DW_TAG_interface_type
8095 || parent->tag == DW_TAG_union_type
8096 || parent->tag == DW_TAG_enumeration_type
8097 || (cu->language == language_fortran
8098 && parent->tag == DW_TAG_subprogram
8099 && pdi->tag == DW_TAG_subprogram))
8100 {
8101 if (grandparent_scope == NULL)
8102 parent->scope = parent->name;
8103 else
8104 parent->scope = typename_concat (&cu->comp_unit_obstack,
8105 grandparent_scope,
8106 parent->name, 0, cu);
8107 }
8108 else
8109 {
8110 /* FIXME drow/2004-04-01: What should we be doing with
8111 function-local names? For partial symbols, we should probably be
8112 ignoring them. */
8113 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8114 dwarf_tag_name (parent->tag),
8115 sect_offset_str (pdi->sect_off));
8116 parent->scope = grandparent_scope;
8117 }
8118
8119 parent->scope_set = 1;
8120 return parent->scope;
8121 }
8122
8123 /* Return the fully scoped name associated with PDI, from compilation unit
8124 CU. The result will be allocated with malloc. */
8125
8126 static gdb::unique_xmalloc_ptr<char>
8127 partial_die_full_name (struct partial_die_info *pdi,
8128 struct dwarf2_cu *cu)
8129 {
8130 const char *parent_scope;
8131
8132 /* If this is a template instantiation, we can not work out the
8133 template arguments from partial DIEs. So, unfortunately, we have
8134 to go through the full DIEs. At least any work we do building
8135 types here will be reused if full symbols are loaded later. */
8136 if (pdi->has_template_arguments)
8137 {
8138 pdi->fixup (cu);
8139
8140 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8141 {
8142 struct die_info *die;
8143 struct attribute attr;
8144 struct dwarf2_cu *ref_cu = cu;
8145
8146 /* DW_FORM_ref_addr is using section offset. */
8147 attr.name = (enum dwarf_attribute) 0;
8148 attr.form = DW_FORM_ref_addr;
8149 attr.u.unsnd = to_underlying (pdi->sect_off);
8150 die = follow_die_ref (NULL, &attr, &ref_cu);
8151
8152 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8153 }
8154 }
8155
8156 parent_scope = partial_die_parent_scope (pdi, cu);
8157 if (parent_scope == NULL)
8158 return NULL;
8159 else
8160 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8161 pdi->name, 0, cu));
8162 }
8163
8164 static void
8165 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8166 {
8167 struct dwarf2_per_objfile *dwarf2_per_objfile
8168 = cu->per_cu->dwarf2_per_objfile;
8169 struct objfile *objfile = dwarf2_per_objfile->objfile;
8170 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8171 CORE_ADDR addr = 0;
8172 const char *actual_name = NULL;
8173 CORE_ADDR baseaddr;
8174
8175 baseaddr = objfile->text_section_offset ();
8176
8177 gdb::unique_xmalloc_ptr<char> built_actual_name
8178 = partial_die_full_name (pdi, cu);
8179 if (built_actual_name != NULL)
8180 actual_name = built_actual_name.get ();
8181
8182 if (actual_name == NULL)
8183 actual_name = pdi->name;
8184
8185 switch (pdi->tag)
8186 {
8187 case DW_TAG_inlined_subroutine:
8188 case DW_TAG_subprogram:
8189 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8190 - baseaddr);
8191 if (pdi->is_external
8192 || cu->language == language_ada
8193 || (cu->language == language_fortran
8194 && pdi->die_parent != NULL
8195 && pdi->die_parent->tag == DW_TAG_subprogram))
8196 {
8197 /* Normally, only "external" DIEs are part of the global scope.
8198 But in Ada and Fortran, we want to be able to access nested
8199 procedures globally. So all Ada and Fortran subprograms are
8200 stored in the global scope. */
8201 add_psymbol_to_list (actual_name,
8202 built_actual_name != NULL,
8203 VAR_DOMAIN, LOC_BLOCK,
8204 SECT_OFF_TEXT (objfile),
8205 psymbol_placement::GLOBAL,
8206 addr,
8207 cu->language, objfile);
8208 }
8209 else
8210 {
8211 add_psymbol_to_list (actual_name,
8212 built_actual_name != NULL,
8213 VAR_DOMAIN, LOC_BLOCK,
8214 SECT_OFF_TEXT (objfile),
8215 psymbol_placement::STATIC,
8216 addr, cu->language, objfile);
8217 }
8218
8219 if (pdi->main_subprogram && actual_name != NULL)
8220 set_objfile_main_name (objfile, actual_name, cu->language);
8221 break;
8222 case DW_TAG_constant:
8223 add_psymbol_to_list (actual_name,
8224 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8225 -1, (pdi->is_external
8226 ? psymbol_placement::GLOBAL
8227 : psymbol_placement::STATIC),
8228 0, cu->language, objfile);
8229 break;
8230 case DW_TAG_variable:
8231 if (pdi->d.locdesc)
8232 addr = decode_locdesc (pdi->d.locdesc, cu);
8233
8234 if (pdi->d.locdesc
8235 && addr == 0
8236 && !dwarf2_per_objfile->has_section_at_zero)
8237 {
8238 /* A global or static variable may also have been stripped
8239 out by the linker if unused, in which case its address
8240 will be nullified; do not add such variables into partial
8241 symbol table then. */
8242 }
8243 else if (pdi->is_external)
8244 {
8245 /* Global Variable.
8246 Don't enter into the minimal symbol tables as there is
8247 a minimal symbol table entry from the ELF symbols already.
8248 Enter into partial symbol table if it has a location
8249 descriptor or a type.
8250 If the location descriptor is missing, new_symbol will create
8251 a LOC_UNRESOLVED symbol, the address of the variable will then
8252 be determined from the minimal symbol table whenever the variable
8253 is referenced.
8254 The address for the partial symbol table entry is not
8255 used by GDB, but it comes in handy for debugging partial symbol
8256 table building. */
8257
8258 if (pdi->d.locdesc || pdi->has_type)
8259 add_psymbol_to_list (actual_name,
8260 built_actual_name != NULL,
8261 VAR_DOMAIN, LOC_STATIC,
8262 SECT_OFF_TEXT (objfile),
8263 psymbol_placement::GLOBAL,
8264 addr, cu->language, objfile);
8265 }
8266 else
8267 {
8268 int has_loc = pdi->d.locdesc != NULL;
8269
8270 /* Static Variable. Skip symbols whose value we cannot know (those
8271 without location descriptors or constant values). */
8272 if (!has_loc && !pdi->has_const_value)
8273 return;
8274
8275 add_psymbol_to_list (actual_name,
8276 built_actual_name != NULL,
8277 VAR_DOMAIN, LOC_STATIC,
8278 SECT_OFF_TEXT (objfile),
8279 psymbol_placement::STATIC,
8280 has_loc ? addr : 0,
8281 cu->language, objfile);
8282 }
8283 break;
8284 case DW_TAG_typedef:
8285 case DW_TAG_base_type:
8286 case DW_TAG_subrange_type:
8287 add_psymbol_to_list (actual_name,
8288 built_actual_name != NULL,
8289 VAR_DOMAIN, LOC_TYPEDEF, -1,
8290 psymbol_placement::STATIC,
8291 0, cu->language, objfile);
8292 break;
8293 case DW_TAG_imported_declaration:
8294 case DW_TAG_namespace:
8295 add_psymbol_to_list (actual_name,
8296 built_actual_name != NULL,
8297 VAR_DOMAIN, LOC_TYPEDEF, -1,
8298 psymbol_placement::GLOBAL,
8299 0, cu->language, objfile);
8300 break;
8301 case DW_TAG_module:
8302 /* With Fortran 77 there might be a "BLOCK DATA" module
8303 available without any name. If so, we skip the module as it
8304 doesn't bring any value. */
8305 if (actual_name != nullptr)
8306 add_psymbol_to_list (actual_name,
8307 built_actual_name != NULL,
8308 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8309 psymbol_placement::GLOBAL,
8310 0, cu->language, objfile);
8311 break;
8312 case DW_TAG_class_type:
8313 case DW_TAG_interface_type:
8314 case DW_TAG_structure_type:
8315 case DW_TAG_union_type:
8316 case DW_TAG_enumeration_type:
8317 /* Skip external references. The DWARF standard says in the section
8318 about "Structure, Union, and Class Type Entries": "An incomplete
8319 structure, union or class type is represented by a structure,
8320 union or class entry that does not have a byte size attribute
8321 and that has a DW_AT_declaration attribute." */
8322 if (!pdi->has_byte_size && pdi->is_declaration)
8323 return;
8324
8325 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8326 static vs. global. */
8327 add_psymbol_to_list (actual_name,
8328 built_actual_name != NULL,
8329 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8330 cu->language == language_cplus
8331 ? psymbol_placement::GLOBAL
8332 : psymbol_placement::STATIC,
8333 0, cu->language, objfile);
8334
8335 break;
8336 case DW_TAG_enumerator:
8337 add_psymbol_to_list (actual_name,
8338 built_actual_name != NULL,
8339 VAR_DOMAIN, LOC_CONST, -1,
8340 cu->language == language_cplus
8341 ? psymbol_placement::GLOBAL
8342 : psymbol_placement::STATIC,
8343 0, cu->language, objfile);
8344 break;
8345 default:
8346 break;
8347 }
8348 }
8349
8350 /* Read a partial die corresponding to a namespace; also, add a symbol
8351 corresponding to that namespace to the symbol table. NAMESPACE is
8352 the name of the enclosing namespace. */
8353
8354 static void
8355 add_partial_namespace (struct partial_die_info *pdi,
8356 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8357 int set_addrmap, struct dwarf2_cu *cu)
8358 {
8359 /* Add a symbol for the namespace. */
8360
8361 add_partial_symbol (pdi, cu);
8362
8363 /* Now scan partial symbols in that namespace. */
8364
8365 if (pdi->has_children)
8366 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8367 }
8368
8369 /* Read a partial die corresponding to a Fortran module. */
8370
8371 static void
8372 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8373 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8374 {
8375 /* Add a symbol for the namespace. */
8376
8377 add_partial_symbol (pdi, cu);
8378
8379 /* Now scan partial symbols in that module. */
8380
8381 if (pdi->has_children)
8382 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8383 }
8384
8385 /* Read a partial die corresponding to a subprogram or an inlined
8386 subprogram and create a partial symbol for that subprogram.
8387 When the CU language allows it, this routine also defines a partial
8388 symbol for each nested subprogram that this subprogram contains.
8389 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8390 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8391
8392 PDI may also be a lexical block, in which case we simply search
8393 recursively for subprograms defined inside that lexical block.
8394 Again, this is only performed when the CU language allows this
8395 type of definitions. */
8396
8397 static void
8398 add_partial_subprogram (struct partial_die_info *pdi,
8399 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8400 int set_addrmap, struct dwarf2_cu *cu)
8401 {
8402 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8403 {
8404 if (pdi->has_pc_info)
8405 {
8406 if (pdi->lowpc < *lowpc)
8407 *lowpc = pdi->lowpc;
8408 if (pdi->highpc > *highpc)
8409 *highpc = pdi->highpc;
8410 if (set_addrmap)
8411 {
8412 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8413 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8414 CORE_ADDR baseaddr;
8415 CORE_ADDR this_highpc;
8416 CORE_ADDR this_lowpc;
8417
8418 baseaddr = objfile->text_section_offset ();
8419 this_lowpc
8420 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8421 pdi->lowpc + baseaddr)
8422 - baseaddr);
8423 this_highpc
8424 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8425 pdi->highpc + baseaddr)
8426 - baseaddr);
8427 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8428 this_lowpc, this_highpc - 1,
8429 cu->per_cu->v.psymtab);
8430 }
8431 }
8432
8433 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8434 {
8435 if (!pdi->is_declaration)
8436 /* Ignore subprogram DIEs that do not have a name, they are
8437 illegal. Do not emit a complaint at this point, we will
8438 do so when we convert this psymtab into a symtab. */
8439 if (pdi->name)
8440 add_partial_symbol (pdi, cu);
8441 }
8442 }
8443
8444 if (! pdi->has_children)
8445 return;
8446
8447 if (cu->language == language_ada || cu->language == language_fortran)
8448 {
8449 pdi = pdi->die_child;
8450 while (pdi != NULL)
8451 {
8452 pdi->fixup (cu);
8453 if (pdi->tag == DW_TAG_subprogram
8454 || pdi->tag == DW_TAG_inlined_subroutine
8455 || pdi->tag == DW_TAG_lexical_block)
8456 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8457 pdi = pdi->die_sibling;
8458 }
8459 }
8460 }
8461
8462 /* Read a partial die corresponding to an enumeration type. */
8463
8464 static void
8465 add_partial_enumeration (struct partial_die_info *enum_pdi,
8466 struct dwarf2_cu *cu)
8467 {
8468 struct partial_die_info *pdi;
8469
8470 if (enum_pdi->name != NULL)
8471 add_partial_symbol (enum_pdi, cu);
8472
8473 pdi = enum_pdi->die_child;
8474 while (pdi)
8475 {
8476 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8477 complaint (_("malformed enumerator DIE ignored"));
8478 else
8479 add_partial_symbol (pdi, cu);
8480 pdi = pdi->die_sibling;
8481 }
8482 }
8483
8484 /* Return the initial uleb128 in the die at INFO_PTR. */
8485
8486 static unsigned int
8487 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8488 {
8489 unsigned int bytes_read;
8490
8491 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8492 }
8493
8494 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8495 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8496
8497 Return the corresponding abbrev, or NULL if the number is zero (indicating
8498 an empty DIE). In either case *BYTES_READ will be set to the length of
8499 the initial number. */
8500
8501 static struct abbrev_info *
8502 peek_die_abbrev (const die_reader_specs &reader,
8503 const gdb_byte *info_ptr, unsigned int *bytes_read)
8504 {
8505 dwarf2_cu *cu = reader.cu;
8506 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8507 unsigned int abbrev_number
8508 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8509
8510 if (abbrev_number == 0)
8511 return NULL;
8512
8513 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8514 if (!abbrev)
8515 {
8516 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8517 " at offset %s [in module %s]"),
8518 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8519 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8520 }
8521
8522 return abbrev;
8523 }
8524
8525 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8526 Returns a pointer to the end of a series of DIEs, terminated by an empty
8527 DIE. Any children of the skipped DIEs will also be skipped. */
8528
8529 static const gdb_byte *
8530 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8531 {
8532 while (1)
8533 {
8534 unsigned int bytes_read;
8535 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8536
8537 if (abbrev == NULL)
8538 return info_ptr + bytes_read;
8539 else
8540 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8541 }
8542 }
8543
8544 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8545 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8546 abbrev corresponding to that skipped uleb128 should be passed in
8547 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8548 children. */
8549
8550 static const gdb_byte *
8551 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8552 struct abbrev_info *abbrev)
8553 {
8554 unsigned int bytes_read;
8555 struct attribute attr;
8556 bfd *abfd = reader->abfd;
8557 struct dwarf2_cu *cu = reader->cu;
8558 const gdb_byte *buffer = reader->buffer;
8559 const gdb_byte *buffer_end = reader->buffer_end;
8560 unsigned int form, i;
8561
8562 for (i = 0; i < abbrev->num_attrs; i++)
8563 {
8564 /* The only abbrev we care about is DW_AT_sibling. */
8565 if (abbrev->attrs[i].name == DW_AT_sibling)
8566 {
8567 bool ignored;
8568 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8569 &ignored);
8570 if (attr.form == DW_FORM_ref_addr)
8571 complaint (_("ignoring absolute DW_AT_sibling"));
8572 else
8573 {
8574 sect_offset off = attr.get_ref_die_offset ();
8575 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8576
8577 if (sibling_ptr < info_ptr)
8578 complaint (_("DW_AT_sibling points backwards"));
8579 else if (sibling_ptr > reader->buffer_end)
8580 reader->die_section->overflow_complaint ();
8581 else
8582 return sibling_ptr;
8583 }
8584 }
8585
8586 /* If it isn't DW_AT_sibling, skip this attribute. */
8587 form = abbrev->attrs[i].form;
8588 skip_attribute:
8589 switch (form)
8590 {
8591 case DW_FORM_ref_addr:
8592 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8593 and later it is offset sized. */
8594 if (cu->header.version == 2)
8595 info_ptr += cu->header.addr_size;
8596 else
8597 info_ptr += cu->header.offset_size;
8598 break;
8599 case DW_FORM_GNU_ref_alt:
8600 info_ptr += cu->header.offset_size;
8601 break;
8602 case DW_FORM_addr:
8603 info_ptr += cu->header.addr_size;
8604 break;
8605 case DW_FORM_data1:
8606 case DW_FORM_ref1:
8607 case DW_FORM_flag:
8608 case DW_FORM_strx1:
8609 info_ptr += 1;
8610 break;
8611 case DW_FORM_flag_present:
8612 case DW_FORM_implicit_const:
8613 break;
8614 case DW_FORM_data2:
8615 case DW_FORM_ref2:
8616 case DW_FORM_strx2:
8617 info_ptr += 2;
8618 break;
8619 case DW_FORM_strx3:
8620 info_ptr += 3;
8621 break;
8622 case DW_FORM_data4:
8623 case DW_FORM_ref4:
8624 case DW_FORM_strx4:
8625 info_ptr += 4;
8626 break;
8627 case DW_FORM_data8:
8628 case DW_FORM_ref8:
8629 case DW_FORM_ref_sig8:
8630 info_ptr += 8;
8631 break;
8632 case DW_FORM_data16:
8633 info_ptr += 16;
8634 break;
8635 case DW_FORM_string:
8636 read_direct_string (abfd, info_ptr, &bytes_read);
8637 info_ptr += bytes_read;
8638 break;
8639 case DW_FORM_sec_offset:
8640 case DW_FORM_strp:
8641 case DW_FORM_GNU_strp_alt:
8642 info_ptr += cu->header.offset_size;
8643 break;
8644 case DW_FORM_exprloc:
8645 case DW_FORM_block:
8646 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8647 info_ptr += bytes_read;
8648 break;
8649 case DW_FORM_block1:
8650 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8651 break;
8652 case DW_FORM_block2:
8653 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8654 break;
8655 case DW_FORM_block4:
8656 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8657 break;
8658 case DW_FORM_addrx:
8659 case DW_FORM_strx:
8660 case DW_FORM_sdata:
8661 case DW_FORM_udata:
8662 case DW_FORM_ref_udata:
8663 case DW_FORM_GNU_addr_index:
8664 case DW_FORM_GNU_str_index:
8665 case DW_FORM_rnglistx:
8666 case DW_FORM_loclistx:
8667 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8668 break;
8669 case DW_FORM_indirect:
8670 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8671 info_ptr += bytes_read;
8672 /* We need to continue parsing from here, so just go back to
8673 the top. */
8674 goto skip_attribute;
8675
8676 default:
8677 error (_("Dwarf Error: Cannot handle %s "
8678 "in DWARF reader [in module %s]"),
8679 dwarf_form_name (form),
8680 bfd_get_filename (abfd));
8681 }
8682 }
8683
8684 if (abbrev->has_children)
8685 return skip_children (reader, info_ptr);
8686 else
8687 return info_ptr;
8688 }
8689
8690 /* Locate ORIG_PDI's sibling.
8691 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8692
8693 static const gdb_byte *
8694 locate_pdi_sibling (const struct die_reader_specs *reader,
8695 struct partial_die_info *orig_pdi,
8696 const gdb_byte *info_ptr)
8697 {
8698 /* Do we know the sibling already? */
8699
8700 if (orig_pdi->sibling)
8701 return orig_pdi->sibling;
8702
8703 /* Are there any children to deal with? */
8704
8705 if (!orig_pdi->has_children)
8706 return info_ptr;
8707
8708 /* Skip the children the long way. */
8709
8710 return skip_children (reader, info_ptr);
8711 }
8712
8713 /* Expand this partial symbol table into a full symbol table. SELF is
8714 not NULL. */
8715
8716 void
8717 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8718 {
8719 struct dwarf2_per_objfile *dwarf2_per_objfile
8720 = get_dwarf2_per_objfile (objfile);
8721
8722 gdb_assert (!readin);
8723 /* If this psymtab is constructed from a debug-only objfile, the
8724 has_section_at_zero flag will not necessarily be correct. We
8725 can get the correct value for this flag by looking at the data
8726 associated with the (presumably stripped) associated objfile. */
8727 if (objfile->separate_debug_objfile_backlink)
8728 {
8729 struct dwarf2_per_objfile *dpo_backlink
8730 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8731
8732 dwarf2_per_objfile->has_section_at_zero
8733 = dpo_backlink->has_section_at_zero;
8734 }
8735
8736 expand_psymtab (objfile);
8737
8738 process_cu_includes (dwarf2_per_objfile);
8739 }
8740 \f
8741 /* Reading in full CUs. */
8742
8743 /* Add PER_CU to the queue. */
8744
8745 static void
8746 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8747 enum language pretend_language)
8748 {
8749 per_cu->queued = 1;
8750 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8751 }
8752
8753 /* If PER_CU is not yet queued, add it to the queue.
8754 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8755 dependency.
8756 The result is non-zero if PER_CU was queued, otherwise the result is zero
8757 meaning either PER_CU is already queued or it is already loaded.
8758
8759 N.B. There is an invariant here that if a CU is queued then it is loaded.
8760 The caller is required to load PER_CU if we return non-zero. */
8761
8762 static int
8763 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8764 struct dwarf2_per_cu_data *per_cu,
8765 enum language pretend_language)
8766 {
8767 /* We may arrive here during partial symbol reading, if we need full
8768 DIEs to process an unusual case (e.g. template arguments). Do
8769 not queue PER_CU, just tell our caller to load its DIEs. */
8770 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8771 {
8772 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8773 return 1;
8774 return 0;
8775 }
8776
8777 /* Mark the dependence relation so that we don't flush PER_CU
8778 too early. */
8779 if (dependent_cu != NULL)
8780 dwarf2_add_dependence (dependent_cu, per_cu);
8781
8782 /* If it's already on the queue, we have nothing to do. */
8783 if (per_cu->queued)
8784 return 0;
8785
8786 /* If the compilation unit is already loaded, just mark it as
8787 used. */
8788 if (per_cu->cu != NULL)
8789 {
8790 per_cu->cu->last_used = 0;
8791 return 0;
8792 }
8793
8794 /* Add it to the queue. */
8795 queue_comp_unit (per_cu, pretend_language);
8796
8797 return 1;
8798 }
8799
8800 /* Process the queue. */
8801
8802 static void
8803 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8804 {
8805 if (dwarf_read_debug)
8806 {
8807 fprintf_unfiltered (gdb_stdlog,
8808 "Expanding one or more symtabs of objfile %s ...\n",
8809 objfile_name (dwarf2_per_objfile->objfile));
8810 }
8811
8812 /* The queue starts out with one item, but following a DIE reference
8813 may load a new CU, adding it to the end of the queue. */
8814 while (!dwarf2_per_objfile->queue.empty ())
8815 {
8816 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8817
8818 if ((dwarf2_per_objfile->using_index
8819 ? !item.per_cu->v.quick->compunit_symtab
8820 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8821 /* Skip dummy CUs. */
8822 && item.per_cu->cu != NULL)
8823 {
8824 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8825 unsigned int debug_print_threshold;
8826 char buf[100];
8827
8828 if (per_cu->is_debug_types)
8829 {
8830 struct signatured_type *sig_type =
8831 (struct signatured_type *) per_cu;
8832
8833 sprintf (buf, "TU %s at offset %s",
8834 hex_string (sig_type->signature),
8835 sect_offset_str (per_cu->sect_off));
8836 /* There can be 100s of TUs.
8837 Only print them in verbose mode. */
8838 debug_print_threshold = 2;
8839 }
8840 else
8841 {
8842 sprintf (buf, "CU at offset %s",
8843 sect_offset_str (per_cu->sect_off));
8844 debug_print_threshold = 1;
8845 }
8846
8847 if (dwarf_read_debug >= debug_print_threshold)
8848 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8849
8850 if (per_cu->is_debug_types)
8851 process_full_type_unit (per_cu, item.pretend_language);
8852 else
8853 process_full_comp_unit (per_cu, item.pretend_language);
8854
8855 if (dwarf_read_debug >= debug_print_threshold)
8856 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8857 }
8858
8859 item.per_cu->queued = 0;
8860 dwarf2_per_objfile->queue.pop ();
8861 }
8862
8863 if (dwarf_read_debug)
8864 {
8865 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8866 objfile_name (dwarf2_per_objfile->objfile));
8867 }
8868 }
8869
8870 /* Read in full symbols for PST, and anything it depends on. */
8871
8872 void
8873 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8874 {
8875 gdb_assert (!readin);
8876
8877 expand_dependencies (objfile);
8878
8879 dw2_do_instantiate_symtab (per_cu_data, false);
8880 gdb_assert (get_compunit_symtab () != nullptr);
8881 }
8882
8883 /* Trivial hash function for die_info: the hash value of a DIE
8884 is its offset in .debug_info for this objfile. */
8885
8886 static hashval_t
8887 die_hash (const void *item)
8888 {
8889 const struct die_info *die = (const struct die_info *) item;
8890
8891 return to_underlying (die->sect_off);
8892 }
8893
8894 /* Trivial comparison function for die_info structures: two DIEs
8895 are equal if they have the same offset. */
8896
8897 static int
8898 die_eq (const void *item_lhs, const void *item_rhs)
8899 {
8900 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8901 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8902
8903 return die_lhs->sect_off == die_rhs->sect_off;
8904 }
8905
8906 /* Load the DIEs associated with PER_CU into memory. */
8907
8908 static void
8909 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8910 bool skip_partial,
8911 enum language pretend_language)
8912 {
8913 gdb_assert (! this_cu->is_debug_types);
8914
8915 cutu_reader reader (this_cu, NULL, 1, skip_partial);
8916 if (reader.dummy_p)
8917 return;
8918
8919 struct dwarf2_cu *cu = reader.cu;
8920 const gdb_byte *info_ptr = reader.info_ptr;
8921
8922 gdb_assert (cu->die_hash == NULL);
8923 cu->die_hash =
8924 htab_create_alloc_ex (cu->header.length / 12,
8925 die_hash,
8926 die_eq,
8927 NULL,
8928 &cu->comp_unit_obstack,
8929 hashtab_obstack_allocate,
8930 dummy_obstack_deallocate);
8931
8932 if (reader.comp_unit_die->has_children)
8933 reader.comp_unit_die->child
8934 = read_die_and_siblings (&reader, reader.info_ptr,
8935 &info_ptr, reader.comp_unit_die);
8936 cu->dies = reader.comp_unit_die;
8937 /* comp_unit_die is not stored in die_hash, no need. */
8938
8939 /* We try not to read any attributes in this function, because not
8940 all CUs needed for references have been loaded yet, and symbol
8941 table processing isn't initialized. But we have to set the CU language,
8942 or we won't be able to build types correctly.
8943 Similarly, if we do not read the producer, we can not apply
8944 producer-specific interpretation. */
8945 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8946
8947 reader.keep ();
8948 }
8949
8950 /* Add a DIE to the delayed physname list. */
8951
8952 static void
8953 add_to_method_list (struct type *type, int fnfield_index, int index,
8954 const char *name, struct die_info *die,
8955 struct dwarf2_cu *cu)
8956 {
8957 struct delayed_method_info mi;
8958 mi.type = type;
8959 mi.fnfield_index = fnfield_index;
8960 mi.index = index;
8961 mi.name = name;
8962 mi.die = die;
8963 cu->method_list.push_back (mi);
8964 }
8965
8966 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8967 "const" / "volatile". If so, decrements LEN by the length of the
8968 modifier and return true. Otherwise return false. */
8969
8970 template<size_t N>
8971 static bool
8972 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8973 {
8974 size_t mod_len = sizeof (mod) - 1;
8975 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8976 {
8977 len -= mod_len;
8978 return true;
8979 }
8980 return false;
8981 }
8982
8983 /* Compute the physnames of any methods on the CU's method list.
8984
8985 The computation of method physnames is delayed in order to avoid the
8986 (bad) condition that one of the method's formal parameters is of an as yet
8987 incomplete type. */
8988
8989 static void
8990 compute_delayed_physnames (struct dwarf2_cu *cu)
8991 {
8992 /* Only C++ delays computing physnames. */
8993 if (cu->method_list.empty ())
8994 return;
8995 gdb_assert (cu->language == language_cplus);
8996
8997 for (const delayed_method_info &mi : cu->method_list)
8998 {
8999 const char *physname;
9000 struct fn_fieldlist *fn_flp
9001 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9002 physname = dwarf2_physname (mi.name, mi.die, cu);
9003 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9004 = physname ? physname : "";
9005
9006 /* Since there's no tag to indicate whether a method is a
9007 const/volatile overload, extract that information out of the
9008 demangled name. */
9009 if (physname != NULL)
9010 {
9011 size_t len = strlen (physname);
9012
9013 while (1)
9014 {
9015 if (physname[len] == ')') /* shortcut */
9016 break;
9017 else if (check_modifier (physname, len, " const"))
9018 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9019 else if (check_modifier (physname, len, " volatile"))
9020 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9021 else
9022 break;
9023 }
9024 }
9025 }
9026
9027 /* The list is no longer needed. */
9028 cu->method_list.clear ();
9029 }
9030
9031 /* Go objects should be embedded in a DW_TAG_module DIE,
9032 and it's not clear if/how imported objects will appear.
9033 To keep Go support simple until that's worked out,
9034 go back through what we've read and create something usable.
9035 We could do this while processing each DIE, and feels kinda cleaner,
9036 but that way is more invasive.
9037 This is to, for example, allow the user to type "p var" or "b main"
9038 without having to specify the package name, and allow lookups
9039 of module.object to work in contexts that use the expression
9040 parser. */
9041
9042 static void
9043 fixup_go_packaging (struct dwarf2_cu *cu)
9044 {
9045 gdb::unique_xmalloc_ptr<char> package_name;
9046 struct pending *list;
9047 int i;
9048
9049 for (list = *cu->get_builder ()->get_global_symbols ();
9050 list != NULL;
9051 list = list->next)
9052 {
9053 for (i = 0; i < list->nsyms; ++i)
9054 {
9055 struct symbol *sym = list->symbol[i];
9056
9057 if (sym->language () == language_go
9058 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9059 {
9060 gdb::unique_xmalloc_ptr<char> this_package_name
9061 (go_symbol_package_name (sym));
9062
9063 if (this_package_name == NULL)
9064 continue;
9065 if (package_name == NULL)
9066 package_name = std::move (this_package_name);
9067 else
9068 {
9069 struct objfile *objfile
9070 = cu->per_cu->dwarf2_per_objfile->objfile;
9071 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9072 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9073 (symbol_symtab (sym) != NULL
9074 ? symtab_to_filename_for_display
9075 (symbol_symtab (sym))
9076 : objfile_name (objfile)),
9077 this_package_name.get (), package_name.get ());
9078 }
9079 }
9080 }
9081 }
9082
9083 if (package_name != NULL)
9084 {
9085 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9086 const char *saved_package_name = objfile->intern (package_name.get ());
9087 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9088 saved_package_name);
9089 struct symbol *sym;
9090
9091 sym = allocate_symbol (objfile);
9092 sym->set_language (language_go, &objfile->objfile_obstack);
9093 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9094 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9095 e.g., "main" finds the "main" module and not C's main(). */
9096 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9097 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9098 SYMBOL_TYPE (sym) = type;
9099
9100 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9101 }
9102 }
9103
9104 /* Allocate a fully-qualified name consisting of the two parts on the
9105 obstack. */
9106
9107 static const char *
9108 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9109 {
9110 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9111 }
9112
9113 /* A helper that allocates a struct discriminant_info to attach to a
9114 union type. */
9115
9116 static struct discriminant_info *
9117 alloc_discriminant_info (struct type *type, int discriminant_index,
9118 int default_index)
9119 {
9120 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9121 gdb_assert (discriminant_index == -1
9122 || (discriminant_index >= 0
9123 && discriminant_index < TYPE_NFIELDS (type)));
9124 gdb_assert (default_index == -1
9125 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9126
9127 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9128
9129 struct discriminant_info *disc
9130 = ((struct discriminant_info *)
9131 TYPE_ZALLOC (type,
9132 offsetof (struct discriminant_info, discriminants)
9133 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9134 disc->default_index = default_index;
9135 disc->discriminant_index = discriminant_index;
9136
9137 struct dynamic_prop prop;
9138 prop.kind = PROP_UNDEFINED;
9139 prop.data.baton = disc;
9140
9141 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9142
9143 return disc;
9144 }
9145
9146 /* Some versions of rustc emitted enums in an unusual way.
9147
9148 Ordinary enums were emitted as unions. The first element of each
9149 structure in the union was named "RUST$ENUM$DISR". This element
9150 held the discriminant.
9151
9152 These versions of Rust also implemented the "non-zero"
9153 optimization. When the enum had two values, and one is empty and
9154 the other holds a pointer that cannot be zero, the pointer is used
9155 as the discriminant, with a zero value meaning the empty variant.
9156 Here, the union's first member is of the form
9157 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9158 where the fieldnos are the indices of the fields that should be
9159 traversed in order to find the field (which may be several fields deep)
9160 and the variantname is the name of the variant of the case when the
9161 field is zero.
9162
9163 This function recognizes whether TYPE is of one of these forms,
9164 and, if so, smashes it to be a variant type. */
9165
9166 static void
9167 quirk_rust_enum (struct type *type, struct objfile *objfile)
9168 {
9169 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9170
9171 /* We don't need to deal with empty enums. */
9172 if (TYPE_NFIELDS (type) == 0)
9173 return;
9174
9175 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9176 if (TYPE_NFIELDS (type) == 1
9177 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9178 {
9179 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9180
9181 /* Decode the field name to find the offset of the
9182 discriminant. */
9183 ULONGEST bit_offset = 0;
9184 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9185 while (name[0] >= '0' && name[0] <= '9')
9186 {
9187 char *tail;
9188 unsigned long index = strtoul (name, &tail, 10);
9189 name = tail;
9190 if (*name != '$'
9191 || index >= TYPE_NFIELDS (field_type)
9192 || (TYPE_FIELD_LOC_KIND (field_type, index)
9193 != FIELD_LOC_KIND_BITPOS))
9194 {
9195 complaint (_("Could not parse Rust enum encoding string \"%s\""
9196 "[in module %s]"),
9197 TYPE_FIELD_NAME (type, 0),
9198 objfile_name (objfile));
9199 return;
9200 }
9201 ++name;
9202
9203 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9204 field_type = TYPE_FIELD_TYPE (field_type, index);
9205 }
9206
9207 /* Make a union to hold the variants. */
9208 struct type *union_type = alloc_type (objfile);
9209 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9210 TYPE_NFIELDS (union_type) = 3;
9211 TYPE_FIELDS (union_type)
9212 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9213 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9214 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9215
9216 /* Put the discriminant must at index 0. */
9217 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9218 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9219 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9220 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9221
9222 /* The order of fields doesn't really matter, so put the real
9223 field at index 1 and the data-less field at index 2. */
9224 struct discriminant_info *disc
9225 = alloc_discriminant_info (union_type, 0, 1);
9226 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9227 TYPE_FIELD_NAME (union_type, 1)
9228 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9229 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9230 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9231 TYPE_FIELD_NAME (union_type, 1));
9232
9233 const char *dataless_name
9234 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9235 name);
9236 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9237 dataless_name);
9238 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9239 /* NAME points into the original discriminant name, which
9240 already has the correct lifetime. */
9241 TYPE_FIELD_NAME (union_type, 2) = name;
9242 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9243 disc->discriminants[2] = 0;
9244
9245 /* Smash this type to be a structure type. We have to do this
9246 because the type has already been recorded. */
9247 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9248 TYPE_NFIELDS (type) = 1;
9249 TYPE_FIELDS (type)
9250 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9251
9252 /* Install the variant part. */
9253 TYPE_FIELD_TYPE (type, 0) = union_type;
9254 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9255 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9256 }
9257 /* A union with a single anonymous field is probably an old-style
9258 univariant enum. */
9259 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9260 {
9261 /* Smash this type to be a structure type. We have to do this
9262 because the type has already been recorded. */
9263 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9264
9265 /* Make a union to hold the variants. */
9266 struct type *union_type = alloc_type (objfile);
9267 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9268 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9269 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9270 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9271 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9272
9273 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9274 const char *variant_name
9275 = rust_last_path_segment (TYPE_NAME (field_type));
9276 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9277 TYPE_NAME (field_type)
9278 = rust_fully_qualify (&objfile->objfile_obstack,
9279 TYPE_NAME (type), variant_name);
9280
9281 /* Install the union in the outer struct type. */
9282 TYPE_NFIELDS (type) = 1;
9283 TYPE_FIELDS (type)
9284 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9285 TYPE_FIELD_TYPE (type, 0) = union_type;
9286 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9287 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9288
9289 alloc_discriminant_info (union_type, -1, 0);
9290 }
9291 else
9292 {
9293 struct type *disr_type = nullptr;
9294 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9295 {
9296 disr_type = TYPE_FIELD_TYPE (type, i);
9297
9298 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9299 {
9300 /* All fields of a true enum will be structs. */
9301 return;
9302 }
9303 else if (TYPE_NFIELDS (disr_type) == 0)
9304 {
9305 /* Could be data-less variant, so keep going. */
9306 disr_type = nullptr;
9307 }
9308 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9309 "RUST$ENUM$DISR") != 0)
9310 {
9311 /* Not a Rust enum. */
9312 return;
9313 }
9314 else
9315 {
9316 /* Found one. */
9317 break;
9318 }
9319 }
9320
9321 /* If we got here without a discriminant, then it's probably
9322 just a union. */
9323 if (disr_type == nullptr)
9324 return;
9325
9326 /* Smash this type to be a structure type. We have to do this
9327 because the type has already been recorded. */
9328 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9329
9330 /* Make a union to hold the variants. */
9331 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9332 struct type *union_type = alloc_type (objfile);
9333 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9334 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9335 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9336 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9337 TYPE_FIELDS (union_type)
9338 = (struct field *) TYPE_ZALLOC (union_type,
9339 (TYPE_NFIELDS (union_type)
9340 * sizeof (struct field)));
9341
9342 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9343 TYPE_NFIELDS (type) * sizeof (struct field));
9344
9345 /* Install the discriminant at index 0 in the union. */
9346 TYPE_FIELD (union_type, 0) = *disr_field;
9347 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9348 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9349
9350 /* Install the union in the outer struct type. */
9351 TYPE_FIELD_TYPE (type, 0) = union_type;
9352 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9353 TYPE_NFIELDS (type) = 1;
9354
9355 /* Set the size and offset of the union type. */
9356 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9357
9358 /* We need a way to find the correct discriminant given a
9359 variant name. For convenience we build a map here. */
9360 struct type *enum_type = FIELD_TYPE (*disr_field);
9361 std::unordered_map<std::string, ULONGEST> discriminant_map;
9362 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9363 {
9364 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9365 {
9366 const char *name
9367 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9368 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9369 }
9370 }
9371
9372 int n_fields = TYPE_NFIELDS (union_type);
9373 struct discriminant_info *disc
9374 = alloc_discriminant_info (union_type, 0, -1);
9375 /* Skip the discriminant here. */
9376 for (int i = 1; i < n_fields; ++i)
9377 {
9378 /* Find the final word in the name of this variant's type.
9379 That name can be used to look up the correct
9380 discriminant. */
9381 const char *variant_name
9382 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9383 i)));
9384
9385 auto iter = discriminant_map.find (variant_name);
9386 if (iter != discriminant_map.end ())
9387 disc->discriminants[i] = iter->second;
9388
9389 /* Remove the discriminant field, if it exists. */
9390 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9391 if (TYPE_NFIELDS (sub_type) > 0)
9392 {
9393 --TYPE_NFIELDS (sub_type);
9394 ++TYPE_FIELDS (sub_type);
9395 }
9396 TYPE_FIELD_NAME (union_type, i) = variant_name;
9397 TYPE_NAME (sub_type)
9398 = rust_fully_qualify (&objfile->objfile_obstack,
9399 TYPE_NAME (type), variant_name);
9400 }
9401 }
9402 }
9403
9404 /* Rewrite some Rust unions to be structures with variants parts. */
9405
9406 static void
9407 rust_union_quirks (struct dwarf2_cu *cu)
9408 {
9409 gdb_assert (cu->language == language_rust);
9410 for (type *type_ : cu->rust_unions)
9411 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9412 /* We don't need this any more. */
9413 cu->rust_unions.clear ();
9414 }
9415
9416 /* Return the symtab for PER_CU. This works properly regardless of
9417 whether we're using the index or psymtabs. */
9418
9419 static struct compunit_symtab *
9420 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9421 {
9422 return (per_cu->dwarf2_per_objfile->using_index
9423 ? per_cu->v.quick->compunit_symtab
9424 : per_cu->v.psymtab->compunit_symtab);
9425 }
9426
9427 /* A helper function for computing the list of all symbol tables
9428 included by PER_CU. */
9429
9430 static void
9431 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9432 htab_t all_children, htab_t all_type_symtabs,
9433 struct dwarf2_per_cu_data *per_cu,
9434 struct compunit_symtab *immediate_parent)
9435 {
9436 void **slot;
9437 struct compunit_symtab *cust;
9438
9439 slot = htab_find_slot (all_children, per_cu, INSERT);
9440 if (*slot != NULL)
9441 {
9442 /* This inclusion and its children have been processed. */
9443 return;
9444 }
9445
9446 *slot = per_cu;
9447 /* Only add a CU if it has a symbol table. */
9448 cust = get_compunit_symtab (per_cu);
9449 if (cust != NULL)
9450 {
9451 /* If this is a type unit only add its symbol table if we haven't
9452 seen it yet (type unit per_cu's can share symtabs). */
9453 if (per_cu->is_debug_types)
9454 {
9455 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9456 if (*slot == NULL)
9457 {
9458 *slot = cust;
9459 result->push_back (cust);
9460 if (cust->user == NULL)
9461 cust->user = immediate_parent;
9462 }
9463 }
9464 else
9465 {
9466 result->push_back (cust);
9467 if (cust->user == NULL)
9468 cust->user = immediate_parent;
9469 }
9470 }
9471
9472 if (!per_cu->imported_symtabs_empty ())
9473 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9474 {
9475 recursively_compute_inclusions (result, all_children,
9476 all_type_symtabs, ptr, cust);
9477 }
9478 }
9479
9480 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9481 PER_CU. */
9482
9483 static void
9484 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9485 {
9486 gdb_assert (! per_cu->is_debug_types);
9487
9488 if (!per_cu->imported_symtabs_empty ())
9489 {
9490 int len;
9491 std::vector<compunit_symtab *> result_symtabs;
9492 htab_t all_children, all_type_symtabs;
9493 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9494
9495 /* If we don't have a symtab, we can just skip this case. */
9496 if (cust == NULL)
9497 return;
9498
9499 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9500 NULL, xcalloc, xfree);
9501 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9502 NULL, xcalloc, xfree);
9503
9504 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9505 {
9506 recursively_compute_inclusions (&result_symtabs, all_children,
9507 all_type_symtabs, ptr, cust);
9508 }
9509
9510 /* Now we have a transitive closure of all the included symtabs. */
9511 len = result_symtabs.size ();
9512 cust->includes
9513 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9514 struct compunit_symtab *, len + 1);
9515 memcpy (cust->includes, result_symtabs.data (),
9516 len * sizeof (compunit_symtab *));
9517 cust->includes[len] = NULL;
9518
9519 htab_delete (all_children);
9520 htab_delete (all_type_symtabs);
9521 }
9522 }
9523
9524 /* Compute the 'includes' field for the symtabs of all the CUs we just
9525 read. */
9526
9527 static void
9528 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9529 {
9530 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9531 {
9532 if (! iter->is_debug_types)
9533 compute_compunit_symtab_includes (iter);
9534 }
9535
9536 dwarf2_per_objfile->just_read_cus.clear ();
9537 }
9538
9539 /* Generate full symbol information for PER_CU, whose DIEs have
9540 already been loaded into memory. */
9541
9542 static void
9543 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9544 enum language pretend_language)
9545 {
9546 struct dwarf2_cu *cu = per_cu->cu;
9547 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9548 struct objfile *objfile = dwarf2_per_objfile->objfile;
9549 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9550 CORE_ADDR lowpc, highpc;
9551 struct compunit_symtab *cust;
9552 CORE_ADDR baseaddr;
9553 struct block *static_block;
9554 CORE_ADDR addr;
9555
9556 baseaddr = objfile->text_section_offset ();
9557
9558 /* Clear the list here in case something was left over. */
9559 cu->method_list.clear ();
9560
9561 cu->language = pretend_language;
9562 cu->language_defn = language_def (cu->language);
9563
9564 /* Do line number decoding in read_file_scope () */
9565 process_die (cu->dies, cu);
9566
9567 /* For now fudge the Go package. */
9568 if (cu->language == language_go)
9569 fixup_go_packaging (cu);
9570
9571 /* Now that we have processed all the DIEs in the CU, all the types
9572 should be complete, and it should now be safe to compute all of the
9573 physnames. */
9574 compute_delayed_physnames (cu);
9575
9576 if (cu->language == language_rust)
9577 rust_union_quirks (cu);
9578
9579 /* Some compilers don't define a DW_AT_high_pc attribute for the
9580 compilation unit. If the DW_AT_high_pc is missing, synthesize
9581 it, by scanning the DIE's below the compilation unit. */
9582 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9583
9584 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9585 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9586
9587 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9588 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9589 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9590 addrmap to help ensure it has an accurate map of pc values belonging to
9591 this comp unit. */
9592 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9593
9594 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9595 SECT_OFF_TEXT (objfile),
9596 0);
9597
9598 if (cust != NULL)
9599 {
9600 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9601
9602 /* Set symtab language to language from DW_AT_language. If the
9603 compilation is from a C file generated by language preprocessors, do
9604 not set the language if it was already deduced by start_subfile. */
9605 if (!(cu->language == language_c
9606 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9607 COMPUNIT_FILETABS (cust)->language = cu->language;
9608
9609 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9610 produce DW_AT_location with location lists but it can be possibly
9611 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9612 there were bugs in prologue debug info, fixed later in GCC-4.5
9613 by "unwind info for epilogues" patch (which is not directly related).
9614
9615 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9616 needed, it would be wrong due to missing DW_AT_producer there.
9617
9618 Still one can confuse GDB by using non-standard GCC compilation
9619 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9620 */
9621 if (cu->has_loclist && gcc_4_minor >= 5)
9622 cust->locations_valid = 1;
9623
9624 if (gcc_4_minor >= 5)
9625 cust->epilogue_unwind_valid = 1;
9626
9627 cust->call_site_htab = cu->call_site_htab;
9628 }
9629
9630 if (dwarf2_per_objfile->using_index)
9631 per_cu->v.quick->compunit_symtab = cust;
9632 else
9633 {
9634 dwarf2_psymtab *pst = per_cu->v.psymtab;
9635 pst->compunit_symtab = cust;
9636 pst->readin = true;
9637 }
9638
9639 /* Push it for inclusion processing later. */
9640 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9641
9642 /* Not needed any more. */
9643 cu->reset_builder ();
9644 }
9645
9646 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9647 already been loaded into memory. */
9648
9649 static void
9650 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9651 enum language pretend_language)
9652 {
9653 struct dwarf2_cu *cu = per_cu->cu;
9654 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9655 struct objfile *objfile = dwarf2_per_objfile->objfile;
9656 struct compunit_symtab *cust;
9657 struct signatured_type *sig_type;
9658
9659 gdb_assert (per_cu->is_debug_types);
9660 sig_type = (struct signatured_type *) per_cu;
9661
9662 /* Clear the list here in case something was left over. */
9663 cu->method_list.clear ();
9664
9665 cu->language = pretend_language;
9666 cu->language_defn = language_def (cu->language);
9667
9668 /* The symbol tables are set up in read_type_unit_scope. */
9669 process_die (cu->dies, cu);
9670
9671 /* For now fudge the Go package. */
9672 if (cu->language == language_go)
9673 fixup_go_packaging (cu);
9674
9675 /* Now that we have processed all the DIEs in the CU, all the types
9676 should be complete, and it should now be safe to compute all of the
9677 physnames. */
9678 compute_delayed_physnames (cu);
9679
9680 if (cu->language == language_rust)
9681 rust_union_quirks (cu);
9682
9683 /* TUs share symbol tables.
9684 If this is the first TU to use this symtab, complete the construction
9685 of it with end_expandable_symtab. Otherwise, complete the addition of
9686 this TU's symbols to the existing symtab. */
9687 if (sig_type->type_unit_group->compunit_symtab == NULL)
9688 {
9689 buildsym_compunit *builder = cu->get_builder ();
9690 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9691 sig_type->type_unit_group->compunit_symtab = cust;
9692
9693 if (cust != NULL)
9694 {
9695 /* Set symtab language to language from DW_AT_language. If the
9696 compilation is from a C file generated by language preprocessors,
9697 do not set the language if it was already deduced by
9698 start_subfile. */
9699 if (!(cu->language == language_c
9700 && COMPUNIT_FILETABS (cust)->language != language_c))
9701 COMPUNIT_FILETABS (cust)->language = cu->language;
9702 }
9703 }
9704 else
9705 {
9706 cu->get_builder ()->augment_type_symtab ();
9707 cust = sig_type->type_unit_group->compunit_symtab;
9708 }
9709
9710 if (dwarf2_per_objfile->using_index)
9711 per_cu->v.quick->compunit_symtab = cust;
9712 else
9713 {
9714 dwarf2_psymtab *pst = per_cu->v.psymtab;
9715 pst->compunit_symtab = cust;
9716 pst->readin = true;
9717 }
9718
9719 /* Not needed any more. */
9720 cu->reset_builder ();
9721 }
9722
9723 /* Process an imported unit DIE. */
9724
9725 static void
9726 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9727 {
9728 struct attribute *attr;
9729
9730 /* For now we don't handle imported units in type units. */
9731 if (cu->per_cu->is_debug_types)
9732 {
9733 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9734 " supported in type units [in module %s]"),
9735 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9736 }
9737
9738 attr = dwarf2_attr (die, DW_AT_import, cu);
9739 if (attr != NULL)
9740 {
9741 sect_offset sect_off = attr->get_ref_die_offset ();
9742 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9743 dwarf2_per_cu_data *per_cu
9744 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9745 cu->per_cu->dwarf2_per_objfile);
9746
9747 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9748 into another compilation unit, at root level. Regard this as a hint,
9749 and ignore it. */
9750 if (die->parent && die->parent->parent == NULL
9751 && per_cu->unit_type == DW_UT_compile
9752 && per_cu->lang == language_cplus)
9753 return;
9754
9755 /* If necessary, add it to the queue and load its DIEs. */
9756 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9757 load_full_comp_unit (per_cu, false, cu->language);
9758
9759 cu->per_cu->imported_symtabs_push (per_cu);
9760 }
9761 }
9762
9763 /* RAII object that represents a process_die scope: i.e.,
9764 starts/finishes processing a DIE. */
9765 class process_die_scope
9766 {
9767 public:
9768 process_die_scope (die_info *die, dwarf2_cu *cu)
9769 : m_die (die), m_cu (cu)
9770 {
9771 /* We should only be processing DIEs not already in process. */
9772 gdb_assert (!m_die->in_process);
9773 m_die->in_process = true;
9774 }
9775
9776 ~process_die_scope ()
9777 {
9778 m_die->in_process = false;
9779
9780 /* If we're done processing the DIE for the CU that owns the line
9781 header, we don't need the line header anymore. */
9782 if (m_cu->line_header_die_owner == m_die)
9783 {
9784 delete m_cu->line_header;
9785 m_cu->line_header = NULL;
9786 m_cu->line_header_die_owner = NULL;
9787 }
9788 }
9789
9790 private:
9791 die_info *m_die;
9792 dwarf2_cu *m_cu;
9793 };
9794
9795 /* Process a die and its children. */
9796
9797 static void
9798 process_die (struct die_info *die, struct dwarf2_cu *cu)
9799 {
9800 process_die_scope scope (die, cu);
9801
9802 switch (die->tag)
9803 {
9804 case DW_TAG_padding:
9805 break;
9806 case DW_TAG_compile_unit:
9807 case DW_TAG_partial_unit:
9808 read_file_scope (die, cu);
9809 break;
9810 case DW_TAG_type_unit:
9811 read_type_unit_scope (die, cu);
9812 break;
9813 case DW_TAG_subprogram:
9814 /* Nested subprograms in Fortran get a prefix. */
9815 if (cu->language == language_fortran
9816 && die->parent != NULL
9817 && die->parent->tag == DW_TAG_subprogram)
9818 cu->processing_has_namespace_info = true;
9819 /* Fall through. */
9820 case DW_TAG_inlined_subroutine:
9821 read_func_scope (die, cu);
9822 break;
9823 case DW_TAG_lexical_block:
9824 case DW_TAG_try_block:
9825 case DW_TAG_catch_block:
9826 read_lexical_block_scope (die, cu);
9827 break;
9828 case DW_TAG_call_site:
9829 case DW_TAG_GNU_call_site:
9830 read_call_site_scope (die, cu);
9831 break;
9832 case DW_TAG_class_type:
9833 case DW_TAG_interface_type:
9834 case DW_TAG_structure_type:
9835 case DW_TAG_union_type:
9836 process_structure_scope (die, cu);
9837 break;
9838 case DW_TAG_enumeration_type:
9839 process_enumeration_scope (die, cu);
9840 break;
9841
9842 /* These dies have a type, but processing them does not create
9843 a symbol or recurse to process the children. Therefore we can
9844 read them on-demand through read_type_die. */
9845 case DW_TAG_subroutine_type:
9846 case DW_TAG_set_type:
9847 case DW_TAG_array_type:
9848 case DW_TAG_pointer_type:
9849 case DW_TAG_ptr_to_member_type:
9850 case DW_TAG_reference_type:
9851 case DW_TAG_rvalue_reference_type:
9852 case DW_TAG_string_type:
9853 break;
9854
9855 case DW_TAG_base_type:
9856 case DW_TAG_subrange_type:
9857 case DW_TAG_typedef:
9858 /* Add a typedef symbol for the type definition, if it has a
9859 DW_AT_name. */
9860 new_symbol (die, read_type_die (die, cu), cu);
9861 break;
9862 case DW_TAG_common_block:
9863 read_common_block (die, cu);
9864 break;
9865 case DW_TAG_common_inclusion:
9866 break;
9867 case DW_TAG_namespace:
9868 cu->processing_has_namespace_info = true;
9869 read_namespace (die, cu);
9870 break;
9871 case DW_TAG_module:
9872 cu->processing_has_namespace_info = true;
9873 read_module (die, cu);
9874 break;
9875 case DW_TAG_imported_declaration:
9876 cu->processing_has_namespace_info = true;
9877 if (read_namespace_alias (die, cu))
9878 break;
9879 /* The declaration is not a global namespace alias. */
9880 /* Fall through. */
9881 case DW_TAG_imported_module:
9882 cu->processing_has_namespace_info = true;
9883 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9884 || cu->language != language_fortran))
9885 complaint (_("Tag '%s' has unexpected children"),
9886 dwarf_tag_name (die->tag));
9887 read_import_statement (die, cu);
9888 break;
9889
9890 case DW_TAG_imported_unit:
9891 process_imported_unit_die (die, cu);
9892 break;
9893
9894 case DW_TAG_variable:
9895 read_variable (die, cu);
9896 break;
9897
9898 default:
9899 new_symbol (die, NULL, cu);
9900 break;
9901 }
9902 }
9903 \f
9904 /* DWARF name computation. */
9905
9906 /* A helper function for dwarf2_compute_name which determines whether DIE
9907 needs to have the name of the scope prepended to the name listed in the
9908 die. */
9909
9910 static int
9911 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9912 {
9913 struct attribute *attr;
9914
9915 switch (die->tag)
9916 {
9917 case DW_TAG_namespace:
9918 case DW_TAG_typedef:
9919 case DW_TAG_class_type:
9920 case DW_TAG_interface_type:
9921 case DW_TAG_structure_type:
9922 case DW_TAG_union_type:
9923 case DW_TAG_enumeration_type:
9924 case DW_TAG_enumerator:
9925 case DW_TAG_subprogram:
9926 case DW_TAG_inlined_subroutine:
9927 case DW_TAG_member:
9928 case DW_TAG_imported_declaration:
9929 return 1;
9930
9931 case DW_TAG_variable:
9932 case DW_TAG_constant:
9933 /* We only need to prefix "globally" visible variables. These include
9934 any variable marked with DW_AT_external or any variable that
9935 lives in a namespace. [Variables in anonymous namespaces
9936 require prefixing, but they are not DW_AT_external.] */
9937
9938 if (dwarf2_attr (die, DW_AT_specification, cu))
9939 {
9940 struct dwarf2_cu *spec_cu = cu;
9941
9942 return die_needs_namespace (die_specification (die, &spec_cu),
9943 spec_cu);
9944 }
9945
9946 attr = dwarf2_attr (die, DW_AT_external, cu);
9947 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9948 && die->parent->tag != DW_TAG_module)
9949 return 0;
9950 /* A variable in a lexical block of some kind does not need a
9951 namespace, even though in C++ such variables may be external
9952 and have a mangled name. */
9953 if (die->parent->tag == DW_TAG_lexical_block
9954 || die->parent->tag == DW_TAG_try_block
9955 || die->parent->tag == DW_TAG_catch_block
9956 || die->parent->tag == DW_TAG_subprogram)
9957 return 0;
9958 return 1;
9959
9960 default:
9961 return 0;
9962 }
9963 }
9964
9965 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9966 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9967 defined for the given DIE. */
9968
9969 static struct attribute *
9970 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9971 {
9972 struct attribute *attr;
9973
9974 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9975 if (attr == NULL)
9976 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9977
9978 return attr;
9979 }
9980
9981 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9982 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9983 defined for the given DIE. */
9984
9985 static const char *
9986 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9987 {
9988 const char *linkage_name;
9989
9990 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9991 if (linkage_name == NULL)
9992 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9993
9994 return linkage_name;
9995 }
9996
9997 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9998 compute the physname for the object, which include a method's:
9999 - formal parameters (C++),
10000 - receiver type (Go),
10001
10002 The term "physname" is a bit confusing.
10003 For C++, for example, it is the demangled name.
10004 For Go, for example, it's the mangled name.
10005
10006 For Ada, return the DIE's linkage name rather than the fully qualified
10007 name. PHYSNAME is ignored..
10008
10009 The result is allocated on the objfile_obstack and canonicalized. */
10010
10011 static const char *
10012 dwarf2_compute_name (const char *name,
10013 struct die_info *die, struct dwarf2_cu *cu,
10014 int physname)
10015 {
10016 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10017
10018 if (name == NULL)
10019 name = dwarf2_name (die, cu);
10020
10021 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10022 but otherwise compute it by typename_concat inside GDB.
10023 FIXME: Actually this is not really true, or at least not always true.
10024 It's all very confusing. compute_and_set_names doesn't try to demangle
10025 Fortran names because there is no mangling standard. So new_symbol
10026 will set the demangled name to the result of dwarf2_full_name, and it is
10027 the demangled name that GDB uses if it exists. */
10028 if (cu->language == language_ada
10029 || (cu->language == language_fortran && physname))
10030 {
10031 /* For Ada unit, we prefer the linkage name over the name, as
10032 the former contains the exported name, which the user expects
10033 to be able to reference. Ideally, we want the user to be able
10034 to reference this entity using either natural or linkage name,
10035 but we haven't started looking at this enhancement yet. */
10036 const char *linkage_name = dw2_linkage_name (die, cu);
10037
10038 if (linkage_name != NULL)
10039 return linkage_name;
10040 }
10041
10042 /* These are the only languages we know how to qualify names in. */
10043 if (name != NULL
10044 && (cu->language == language_cplus
10045 || cu->language == language_fortran || cu->language == language_d
10046 || cu->language == language_rust))
10047 {
10048 if (die_needs_namespace (die, cu))
10049 {
10050 const char *prefix;
10051 const char *canonical_name = NULL;
10052
10053 string_file buf;
10054
10055 prefix = determine_prefix (die, cu);
10056 if (*prefix != '\0')
10057 {
10058 gdb::unique_xmalloc_ptr<char> prefixed_name
10059 (typename_concat (NULL, prefix, name, physname, cu));
10060
10061 buf.puts (prefixed_name.get ());
10062 }
10063 else
10064 buf.puts (name);
10065
10066 /* Template parameters may be specified in the DIE's DW_AT_name, or
10067 as children with DW_TAG_template_type_param or
10068 DW_TAG_value_type_param. If the latter, add them to the name
10069 here. If the name already has template parameters, then
10070 skip this step; some versions of GCC emit both, and
10071 it is more efficient to use the pre-computed name.
10072
10073 Something to keep in mind about this process: it is very
10074 unlikely, or in some cases downright impossible, to produce
10075 something that will match the mangled name of a function.
10076 If the definition of the function has the same debug info,
10077 we should be able to match up with it anyway. But fallbacks
10078 using the minimal symbol, for instance to find a method
10079 implemented in a stripped copy of libstdc++, will not work.
10080 If we do not have debug info for the definition, we will have to
10081 match them up some other way.
10082
10083 When we do name matching there is a related problem with function
10084 templates; two instantiated function templates are allowed to
10085 differ only by their return types, which we do not add here. */
10086
10087 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10088 {
10089 struct attribute *attr;
10090 struct die_info *child;
10091 int first = 1;
10092
10093 die->building_fullname = 1;
10094
10095 for (child = die->child; child != NULL; child = child->sibling)
10096 {
10097 struct type *type;
10098 LONGEST value;
10099 const gdb_byte *bytes;
10100 struct dwarf2_locexpr_baton *baton;
10101 struct value *v;
10102
10103 if (child->tag != DW_TAG_template_type_param
10104 && child->tag != DW_TAG_template_value_param)
10105 continue;
10106
10107 if (first)
10108 {
10109 buf.puts ("<");
10110 first = 0;
10111 }
10112 else
10113 buf.puts (", ");
10114
10115 attr = dwarf2_attr (child, DW_AT_type, cu);
10116 if (attr == NULL)
10117 {
10118 complaint (_("template parameter missing DW_AT_type"));
10119 buf.puts ("UNKNOWN_TYPE");
10120 continue;
10121 }
10122 type = die_type (child, cu);
10123
10124 if (child->tag == DW_TAG_template_type_param)
10125 {
10126 c_print_type (type, "", &buf, -1, 0, cu->language,
10127 &type_print_raw_options);
10128 continue;
10129 }
10130
10131 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10132 if (attr == NULL)
10133 {
10134 complaint (_("template parameter missing "
10135 "DW_AT_const_value"));
10136 buf.puts ("UNKNOWN_VALUE");
10137 continue;
10138 }
10139
10140 dwarf2_const_value_attr (attr, type, name,
10141 &cu->comp_unit_obstack, cu,
10142 &value, &bytes, &baton);
10143
10144 if (TYPE_NOSIGN (type))
10145 /* GDB prints characters as NUMBER 'CHAR'. If that's
10146 changed, this can use value_print instead. */
10147 c_printchar (value, type, &buf);
10148 else
10149 {
10150 struct value_print_options opts;
10151
10152 if (baton != NULL)
10153 v = dwarf2_evaluate_loc_desc (type, NULL,
10154 baton->data,
10155 baton->size,
10156 baton->per_cu);
10157 else if (bytes != NULL)
10158 {
10159 v = allocate_value (type);
10160 memcpy (value_contents_writeable (v), bytes,
10161 TYPE_LENGTH (type));
10162 }
10163 else
10164 v = value_from_longest (type, value);
10165
10166 /* Specify decimal so that we do not depend on
10167 the radix. */
10168 get_formatted_print_options (&opts, 'd');
10169 opts.raw = 1;
10170 value_print (v, &buf, &opts);
10171 release_value (v);
10172 }
10173 }
10174
10175 die->building_fullname = 0;
10176
10177 if (!first)
10178 {
10179 /* Close the argument list, with a space if necessary
10180 (nested templates). */
10181 if (!buf.empty () && buf.string ().back () == '>')
10182 buf.puts (" >");
10183 else
10184 buf.puts (">");
10185 }
10186 }
10187
10188 /* For C++ methods, append formal parameter type
10189 information, if PHYSNAME. */
10190
10191 if (physname && die->tag == DW_TAG_subprogram
10192 && cu->language == language_cplus)
10193 {
10194 struct type *type = read_type_die (die, cu);
10195
10196 c_type_print_args (type, &buf, 1, cu->language,
10197 &type_print_raw_options);
10198
10199 if (cu->language == language_cplus)
10200 {
10201 /* Assume that an artificial first parameter is
10202 "this", but do not crash if it is not. RealView
10203 marks unnamed (and thus unused) parameters as
10204 artificial; there is no way to differentiate
10205 the two cases. */
10206 if (TYPE_NFIELDS (type) > 0
10207 && TYPE_FIELD_ARTIFICIAL (type, 0)
10208 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10209 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10210 0))))
10211 buf.puts (" const");
10212 }
10213 }
10214
10215 const std::string &intermediate_name = buf.string ();
10216
10217 if (cu->language == language_cplus)
10218 canonical_name
10219 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10220 objfile);
10221
10222 /* If we only computed INTERMEDIATE_NAME, or if
10223 INTERMEDIATE_NAME is already canonical, then we need to
10224 intern it. */
10225 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10226 name = objfile->intern (intermediate_name);
10227 else
10228 name = canonical_name;
10229 }
10230 }
10231
10232 return name;
10233 }
10234
10235 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10236 If scope qualifiers are appropriate they will be added. The result
10237 will be allocated on the storage_obstack, or NULL if the DIE does
10238 not have a name. NAME may either be from a previous call to
10239 dwarf2_name or NULL.
10240
10241 The output string will be canonicalized (if C++). */
10242
10243 static const char *
10244 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10245 {
10246 return dwarf2_compute_name (name, die, cu, 0);
10247 }
10248
10249 /* Construct a physname for the given DIE in CU. NAME may either be
10250 from a previous call to dwarf2_name or NULL. The result will be
10251 allocated on the objfile_objstack or NULL if the DIE does not have a
10252 name.
10253
10254 The output string will be canonicalized (if C++). */
10255
10256 static const char *
10257 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10258 {
10259 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10260 const char *retval, *mangled = NULL, *canon = NULL;
10261 int need_copy = 1;
10262
10263 /* In this case dwarf2_compute_name is just a shortcut not building anything
10264 on its own. */
10265 if (!die_needs_namespace (die, cu))
10266 return dwarf2_compute_name (name, die, cu, 1);
10267
10268 mangled = dw2_linkage_name (die, cu);
10269
10270 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10271 See https://github.com/rust-lang/rust/issues/32925. */
10272 if (cu->language == language_rust && mangled != NULL
10273 && strchr (mangled, '{') != NULL)
10274 mangled = NULL;
10275
10276 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10277 has computed. */
10278 gdb::unique_xmalloc_ptr<char> demangled;
10279 if (mangled != NULL)
10280 {
10281
10282 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10283 {
10284 /* Do nothing (do not demangle the symbol name). */
10285 }
10286 else if (cu->language == language_go)
10287 {
10288 /* This is a lie, but we already lie to the caller new_symbol.
10289 new_symbol assumes we return the mangled name.
10290 This just undoes that lie until things are cleaned up. */
10291 }
10292 else
10293 {
10294 /* Use DMGL_RET_DROP for C++ template functions to suppress
10295 their return type. It is easier for GDB users to search
10296 for such functions as `name(params)' than `long name(params)'.
10297 In such case the minimal symbol names do not match the full
10298 symbol names but for template functions there is never a need
10299 to look up their definition from their declaration so
10300 the only disadvantage remains the minimal symbol variant
10301 `long name(params)' does not have the proper inferior type. */
10302 demangled.reset (gdb_demangle (mangled,
10303 (DMGL_PARAMS | DMGL_ANSI
10304 | DMGL_RET_DROP)));
10305 }
10306 if (demangled)
10307 canon = demangled.get ();
10308 else
10309 {
10310 canon = mangled;
10311 need_copy = 0;
10312 }
10313 }
10314
10315 if (canon == NULL || check_physname)
10316 {
10317 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10318
10319 if (canon != NULL && strcmp (physname, canon) != 0)
10320 {
10321 /* It may not mean a bug in GDB. The compiler could also
10322 compute DW_AT_linkage_name incorrectly. But in such case
10323 GDB would need to be bug-to-bug compatible. */
10324
10325 complaint (_("Computed physname <%s> does not match demangled <%s> "
10326 "(from linkage <%s>) - DIE at %s [in module %s]"),
10327 physname, canon, mangled, sect_offset_str (die->sect_off),
10328 objfile_name (objfile));
10329
10330 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10331 is available here - over computed PHYSNAME. It is safer
10332 against both buggy GDB and buggy compilers. */
10333
10334 retval = canon;
10335 }
10336 else
10337 {
10338 retval = physname;
10339 need_copy = 0;
10340 }
10341 }
10342 else
10343 retval = canon;
10344
10345 if (need_copy)
10346 retval = objfile->intern (retval);
10347
10348 return retval;
10349 }
10350
10351 /* Inspect DIE in CU for a namespace alias. If one exists, record
10352 a new symbol for it.
10353
10354 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10355
10356 static int
10357 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10358 {
10359 struct attribute *attr;
10360
10361 /* If the die does not have a name, this is not a namespace
10362 alias. */
10363 attr = dwarf2_attr (die, DW_AT_name, cu);
10364 if (attr != NULL)
10365 {
10366 int num;
10367 struct die_info *d = die;
10368 struct dwarf2_cu *imported_cu = cu;
10369
10370 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10371 keep inspecting DIEs until we hit the underlying import. */
10372 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10373 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10374 {
10375 attr = dwarf2_attr (d, DW_AT_import, cu);
10376 if (attr == NULL)
10377 break;
10378
10379 d = follow_die_ref (d, attr, &imported_cu);
10380 if (d->tag != DW_TAG_imported_declaration)
10381 break;
10382 }
10383
10384 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10385 {
10386 complaint (_("DIE at %s has too many recursively imported "
10387 "declarations"), sect_offset_str (d->sect_off));
10388 return 0;
10389 }
10390
10391 if (attr != NULL)
10392 {
10393 struct type *type;
10394 sect_offset sect_off = attr->get_ref_die_offset ();
10395
10396 type = get_die_type_at_offset (sect_off, cu->per_cu);
10397 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10398 {
10399 /* This declaration is a global namespace alias. Add
10400 a symbol for it whose type is the aliased namespace. */
10401 new_symbol (die, type, cu);
10402 return 1;
10403 }
10404 }
10405 }
10406
10407 return 0;
10408 }
10409
10410 /* Return the using directives repository (global or local?) to use in the
10411 current context for CU.
10412
10413 For Ada, imported declarations can materialize renamings, which *may* be
10414 global. However it is impossible (for now?) in DWARF to distinguish
10415 "external" imported declarations and "static" ones. As all imported
10416 declarations seem to be static in all other languages, make them all CU-wide
10417 global only in Ada. */
10418
10419 static struct using_direct **
10420 using_directives (struct dwarf2_cu *cu)
10421 {
10422 if (cu->language == language_ada
10423 && cu->get_builder ()->outermost_context_p ())
10424 return cu->get_builder ()->get_global_using_directives ();
10425 else
10426 return cu->get_builder ()->get_local_using_directives ();
10427 }
10428
10429 /* Read the import statement specified by the given die and record it. */
10430
10431 static void
10432 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10433 {
10434 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10435 struct attribute *import_attr;
10436 struct die_info *imported_die, *child_die;
10437 struct dwarf2_cu *imported_cu;
10438 const char *imported_name;
10439 const char *imported_name_prefix;
10440 const char *canonical_name;
10441 const char *import_alias;
10442 const char *imported_declaration = NULL;
10443 const char *import_prefix;
10444 std::vector<const char *> excludes;
10445
10446 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10447 if (import_attr == NULL)
10448 {
10449 complaint (_("Tag '%s' has no DW_AT_import"),
10450 dwarf_tag_name (die->tag));
10451 return;
10452 }
10453
10454 imported_cu = cu;
10455 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10456 imported_name = dwarf2_name (imported_die, imported_cu);
10457 if (imported_name == NULL)
10458 {
10459 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10460
10461 The import in the following code:
10462 namespace A
10463 {
10464 typedef int B;
10465 }
10466
10467 int main ()
10468 {
10469 using A::B;
10470 B b;
10471 return b;
10472 }
10473
10474 ...
10475 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10476 <52> DW_AT_decl_file : 1
10477 <53> DW_AT_decl_line : 6
10478 <54> DW_AT_import : <0x75>
10479 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10480 <59> DW_AT_name : B
10481 <5b> DW_AT_decl_file : 1
10482 <5c> DW_AT_decl_line : 2
10483 <5d> DW_AT_type : <0x6e>
10484 ...
10485 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10486 <76> DW_AT_byte_size : 4
10487 <77> DW_AT_encoding : 5 (signed)
10488
10489 imports the wrong die ( 0x75 instead of 0x58 ).
10490 This case will be ignored until the gcc bug is fixed. */
10491 return;
10492 }
10493
10494 /* Figure out the local name after import. */
10495 import_alias = dwarf2_name (die, cu);
10496
10497 /* Figure out where the statement is being imported to. */
10498 import_prefix = determine_prefix (die, cu);
10499
10500 /* Figure out what the scope of the imported die is and prepend it
10501 to the name of the imported die. */
10502 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10503
10504 if (imported_die->tag != DW_TAG_namespace
10505 && imported_die->tag != DW_TAG_module)
10506 {
10507 imported_declaration = imported_name;
10508 canonical_name = imported_name_prefix;
10509 }
10510 else if (strlen (imported_name_prefix) > 0)
10511 canonical_name = obconcat (&objfile->objfile_obstack,
10512 imported_name_prefix,
10513 (cu->language == language_d ? "." : "::"),
10514 imported_name, (char *) NULL);
10515 else
10516 canonical_name = imported_name;
10517
10518 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10519 for (child_die = die->child; child_die && child_die->tag;
10520 child_die = child_die->sibling)
10521 {
10522 /* DWARF-4: A Fortran use statement with a “rename list” may be
10523 represented by an imported module entry with an import attribute
10524 referring to the module and owned entries corresponding to those
10525 entities that are renamed as part of being imported. */
10526
10527 if (child_die->tag != DW_TAG_imported_declaration)
10528 {
10529 complaint (_("child DW_TAG_imported_declaration expected "
10530 "- DIE at %s [in module %s]"),
10531 sect_offset_str (child_die->sect_off),
10532 objfile_name (objfile));
10533 continue;
10534 }
10535
10536 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10537 if (import_attr == NULL)
10538 {
10539 complaint (_("Tag '%s' has no DW_AT_import"),
10540 dwarf_tag_name (child_die->tag));
10541 continue;
10542 }
10543
10544 imported_cu = cu;
10545 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10546 &imported_cu);
10547 imported_name = dwarf2_name (imported_die, imported_cu);
10548 if (imported_name == NULL)
10549 {
10550 complaint (_("child DW_TAG_imported_declaration has unknown "
10551 "imported name - DIE at %s [in module %s]"),
10552 sect_offset_str (child_die->sect_off),
10553 objfile_name (objfile));
10554 continue;
10555 }
10556
10557 excludes.push_back (imported_name);
10558
10559 process_die (child_die, cu);
10560 }
10561
10562 add_using_directive (using_directives (cu),
10563 import_prefix,
10564 canonical_name,
10565 import_alias,
10566 imported_declaration,
10567 excludes,
10568 0,
10569 &objfile->objfile_obstack);
10570 }
10571
10572 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10573 types, but gives them a size of zero. Starting with version 14,
10574 ICC is compatible with GCC. */
10575
10576 static bool
10577 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10578 {
10579 if (!cu->checked_producer)
10580 check_producer (cu);
10581
10582 return cu->producer_is_icc_lt_14;
10583 }
10584
10585 /* ICC generates a DW_AT_type for C void functions. This was observed on
10586 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10587 which says that void functions should not have a DW_AT_type. */
10588
10589 static bool
10590 producer_is_icc (struct dwarf2_cu *cu)
10591 {
10592 if (!cu->checked_producer)
10593 check_producer (cu);
10594
10595 return cu->producer_is_icc;
10596 }
10597
10598 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10599 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10600 this, it was first present in GCC release 4.3.0. */
10601
10602 static bool
10603 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10604 {
10605 if (!cu->checked_producer)
10606 check_producer (cu);
10607
10608 return cu->producer_is_gcc_lt_4_3;
10609 }
10610
10611 static file_and_directory
10612 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10613 {
10614 file_and_directory res;
10615
10616 /* Find the filename. Do not use dwarf2_name here, since the filename
10617 is not a source language identifier. */
10618 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10619 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10620
10621 if (res.comp_dir == NULL
10622 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10623 && IS_ABSOLUTE_PATH (res.name))
10624 {
10625 res.comp_dir_storage = ldirname (res.name);
10626 if (!res.comp_dir_storage.empty ())
10627 res.comp_dir = res.comp_dir_storage.c_str ();
10628 }
10629 if (res.comp_dir != NULL)
10630 {
10631 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10632 directory, get rid of it. */
10633 const char *cp = strchr (res.comp_dir, ':');
10634
10635 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10636 res.comp_dir = cp + 1;
10637 }
10638
10639 if (res.name == NULL)
10640 res.name = "<unknown>";
10641
10642 return res;
10643 }
10644
10645 /* Handle DW_AT_stmt_list for a compilation unit.
10646 DIE is the DW_TAG_compile_unit die for CU.
10647 COMP_DIR is the compilation directory. LOWPC is passed to
10648 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10649
10650 static void
10651 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10652 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10653 {
10654 struct dwarf2_per_objfile *dwarf2_per_objfile
10655 = cu->per_cu->dwarf2_per_objfile;
10656 struct attribute *attr;
10657 struct line_header line_header_local;
10658 hashval_t line_header_local_hash;
10659 void **slot;
10660 int decode_mapping;
10661
10662 gdb_assert (! cu->per_cu->is_debug_types);
10663
10664 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10665 if (attr == NULL)
10666 return;
10667
10668 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10669
10670 /* The line header hash table is only created if needed (it exists to
10671 prevent redundant reading of the line table for partial_units).
10672 If we're given a partial_unit, we'll need it. If we're given a
10673 compile_unit, then use the line header hash table if it's already
10674 created, but don't create one just yet. */
10675
10676 if (dwarf2_per_objfile->line_header_hash == NULL
10677 && die->tag == DW_TAG_partial_unit)
10678 {
10679 dwarf2_per_objfile->line_header_hash
10680 .reset (htab_create_alloc (127, line_header_hash_voidp,
10681 line_header_eq_voidp,
10682 free_line_header_voidp,
10683 xcalloc, xfree));
10684 }
10685
10686 line_header_local.sect_off = line_offset;
10687 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10688 line_header_local_hash = line_header_hash (&line_header_local);
10689 if (dwarf2_per_objfile->line_header_hash != NULL)
10690 {
10691 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10692 &line_header_local,
10693 line_header_local_hash, NO_INSERT);
10694
10695 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10696 is not present in *SLOT (since if there is something in *SLOT then
10697 it will be for a partial_unit). */
10698 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10699 {
10700 gdb_assert (*slot != NULL);
10701 cu->line_header = (struct line_header *) *slot;
10702 return;
10703 }
10704 }
10705
10706 /* dwarf_decode_line_header does not yet provide sufficient information.
10707 We always have to call also dwarf_decode_lines for it. */
10708 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10709 if (lh == NULL)
10710 return;
10711
10712 cu->line_header = lh.release ();
10713 cu->line_header_die_owner = die;
10714
10715 if (dwarf2_per_objfile->line_header_hash == NULL)
10716 slot = NULL;
10717 else
10718 {
10719 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10720 &line_header_local,
10721 line_header_local_hash, INSERT);
10722 gdb_assert (slot != NULL);
10723 }
10724 if (slot != NULL && *slot == NULL)
10725 {
10726 /* This newly decoded line number information unit will be owned
10727 by line_header_hash hash table. */
10728 *slot = cu->line_header;
10729 cu->line_header_die_owner = NULL;
10730 }
10731 else
10732 {
10733 /* We cannot free any current entry in (*slot) as that struct line_header
10734 may be already used by multiple CUs. Create only temporary decoded
10735 line_header for this CU - it may happen at most once for each line
10736 number information unit. And if we're not using line_header_hash
10737 then this is what we want as well. */
10738 gdb_assert (die->tag != DW_TAG_partial_unit);
10739 }
10740 decode_mapping = (die->tag != DW_TAG_partial_unit);
10741 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10742 decode_mapping);
10743
10744 }
10745
10746 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10747
10748 static void
10749 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10750 {
10751 struct dwarf2_per_objfile *dwarf2_per_objfile
10752 = cu->per_cu->dwarf2_per_objfile;
10753 struct objfile *objfile = dwarf2_per_objfile->objfile;
10754 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10755 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10756 CORE_ADDR highpc = ((CORE_ADDR) 0);
10757 struct attribute *attr;
10758 struct die_info *child_die;
10759 CORE_ADDR baseaddr;
10760
10761 prepare_one_comp_unit (cu, die, cu->language);
10762 baseaddr = objfile->text_section_offset ();
10763
10764 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10765
10766 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10767 from finish_block. */
10768 if (lowpc == ((CORE_ADDR) -1))
10769 lowpc = highpc;
10770 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10771
10772 file_and_directory fnd = find_file_and_directory (die, cu);
10773
10774 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10775 standardised yet. As a workaround for the language detection we fall
10776 back to the DW_AT_producer string. */
10777 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10778 cu->language = language_opencl;
10779
10780 /* Similar hack for Go. */
10781 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10782 set_cu_language (DW_LANG_Go, cu);
10783
10784 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10785
10786 /* Decode line number information if present. We do this before
10787 processing child DIEs, so that the line header table is available
10788 for DW_AT_decl_file. */
10789 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10790
10791 /* Process all dies in compilation unit. */
10792 if (die->child != NULL)
10793 {
10794 child_die = die->child;
10795 while (child_die && child_die->tag)
10796 {
10797 process_die (child_die, cu);
10798 child_die = child_die->sibling;
10799 }
10800 }
10801
10802 /* Decode macro information, if present. Dwarf 2 macro information
10803 refers to information in the line number info statement program
10804 header, so we can only read it if we've read the header
10805 successfully. */
10806 attr = dwarf2_attr (die, DW_AT_macros, cu);
10807 if (attr == NULL)
10808 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10809 if (attr && cu->line_header)
10810 {
10811 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10812 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10813
10814 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10815 }
10816 else
10817 {
10818 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10819 if (attr && cu->line_header)
10820 {
10821 unsigned int macro_offset = DW_UNSND (attr);
10822
10823 dwarf_decode_macros (cu, macro_offset, 0);
10824 }
10825 }
10826 }
10827
10828 void
10829 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10830 {
10831 struct type_unit_group *tu_group;
10832 int first_time;
10833 struct attribute *attr;
10834 unsigned int i;
10835 struct signatured_type *sig_type;
10836
10837 gdb_assert (per_cu->is_debug_types);
10838 sig_type = (struct signatured_type *) per_cu;
10839
10840 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10841
10842 /* If we're using .gdb_index (includes -readnow) then
10843 per_cu->type_unit_group may not have been set up yet. */
10844 if (sig_type->type_unit_group == NULL)
10845 sig_type->type_unit_group = get_type_unit_group (this, attr);
10846 tu_group = sig_type->type_unit_group;
10847
10848 /* If we've already processed this stmt_list there's no real need to
10849 do it again, we could fake it and just recreate the part we need
10850 (file name,index -> symtab mapping). If data shows this optimization
10851 is useful we can do it then. */
10852 first_time = tu_group->compunit_symtab == NULL;
10853
10854 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10855 debug info. */
10856 line_header_up lh;
10857 if (attr != NULL)
10858 {
10859 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10860 lh = dwarf_decode_line_header (line_offset, this);
10861 }
10862 if (lh == NULL)
10863 {
10864 if (first_time)
10865 start_symtab ("", NULL, 0);
10866 else
10867 {
10868 gdb_assert (tu_group->symtabs == NULL);
10869 gdb_assert (m_builder == nullptr);
10870 struct compunit_symtab *cust = tu_group->compunit_symtab;
10871 m_builder.reset (new struct buildsym_compunit
10872 (COMPUNIT_OBJFILE (cust), "",
10873 COMPUNIT_DIRNAME (cust),
10874 compunit_language (cust),
10875 0, cust));
10876 }
10877 return;
10878 }
10879
10880 line_header = lh.release ();
10881 line_header_die_owner = die;
10882
10883 if (first_time)
10884 {
10885 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10886
10887 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10888 still initializing it, and our caller (a few levels up)
10889 process_full_type_unit still needs to know if this is the first
10890 time. */
10891
10892 tu_group->symtabs
10893 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10894 struct symtab *, line_header->file_names_size ());
10895
10896 auto &file_names = line_header->file_names ();
10897 for (i = 0; i < file_names.size (); ++i)
10898 {
10899 file_entry &fe = file_names[i];
10900 dwarf2_start_subfile (this, fe.name,
10901 fe.include_dir (line_header));
10902 buildsym_compunit *b = get_builder ();
10903 if (b->get_current_subfile ()->symtab == NULL)
10904 {
10905 /* NOTE: start_subfile will recognize when it's been
10906 passed a file it has already seen. So we can't
10907 assume there's a simple mapping from
10908 cu->line_header->file_names to subfiles, plus
10909 cu->line_header->file_names may contain dups. */
10910 b->get_current_subfile ()->symtab
10911 = allocate_symtab (cust, b->get_current_subfile ()->name);
10912 }
10913
10914 fe.symtab = b->get_current_subfile ()->symtab;
10915 tu_group->symtabs[i] = fe.symtab;
10916 }
10917 }
10918 else
10919 {
10920 gdb_assert (m_builder == nullptr);
10921 struct compunit_symtab *cust = tu_group->compunit_symtab;
10922 m_builder.reset (new struct buildsym_compunit
10923 (COMPUNIT_OBJFILE (cust), "",
10924 COMPUNIT_DIRNAME (cust),
10925 compunit_language (cust),
10926 0, cust));
10927
10928 auto &file_names = line_header->file_names ();
10929 for (i = 0; i < file_names.size (); ++i)
10930 {
10931 file_entry &fe = file_names[i];
10932 fe.symtab = tu_group->symtabs[i];
10933 }
10934 }
10935
10936 /* The main symtab is allocated last. Type units don't have DW_AT_name
10937 so they don't have a "real" (so to speak) symtab anyway.
10938 There is later code that will assign the main symtab to all symbols
10939 that don't have one. We need to handle the case of a symbol with a
10940 missing symtab (DW_AT_decl_file) anyway. */
10941 }
10942
10943 /* Process DW_TAG_type_unit.
10944 For TUs we want to skip the first top level sibling if it's not the
10945 actual type being defined by this TU. In this case the first top
10946 level sibling is there to provide context only. */
10947
10948 static void
10949 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10950 {
10951 struct die_info *child_die;
10952
10953 prepare_one_comp_unit (cu, die, language_minimal);
10954
10955 /* Initialize (or reinitialize) the machinery for building symtabs.
10956 We do this before processing child DIEs, so that the line header table
10957 is available for DW_AT_decl_file. */
10958 cu->setup_type_unit_groups (die);
10959
10960 if (die->child != NULL)
10961 {
10962 child_die = die->child;
10963 while (child_die && child_die->tag)
10964 {
10965 process_die (child_die, cu);
10966 child_die = child_die->sibling;
10967 }
10968 }
10969 }
10970 \f
10971 /* DWO/DWP files.
10972
10973 http://gcc.gnu.org/wiki/DebugFission
10974 http://gcc.gnu.org/wiki/DebugFissionDWP
10975
10976 To simplify handling of both DWO files ("object" files with the DWARF info)
10977 and DWP files (a file with the DWOs packaged up into one file), we treat
10978 DWP files as having a collection of virtual DWO files. */
10979
10980 static hashval_t
10981 hash_dwo_file (const void *item)
10982 {
10983 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10984 hashval_t hash;
10985
10986 hash = htab_hash_string (dwo_file->dwo_name);
10987 if (dwo_file->comp_dir != NULL)
10988 hash += htab_hash_string (dwo_file->comp_dir);
10989 return hash;
10990 }
10991
10992 static int
10993 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10994 {
10995 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10996 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10997
10998 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10999 return 0;
11000 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11001 return lhs->comp_dir == rhs->comp_dir;
11002 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11003 }
11004
11005 /* Allocate a hash table for DWO files. */
11006
11007 static htab_up
11008 allocate_dwo_file_hash_table ()
11009 {
11010 auto delete_dwo_file = [] (void *item)
11011 {
11012 struct dwo_file *dwo_file = (struct dwo_file *) item;
11013
11014 delete dwo_file;
11015 };
11016
11017 return htab_up (htab_create_alloc (41,
11018 hash_dwo_file,
11019 eq_dwo_file,
11020 delete_dwo_file,
11021 xcalloc, xfree));
11022 }
11023
11024 /* Lookup DWO file DWO_NAME. */
11025
11026 static void **
11027 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11028 const char *dwo_name,
11029 const char *comp_dir)
11030 {
11031 struct dwo_file find_entry;
11032 void **slot;
11033
11034 if (dwarf2_per_objfile->dwo_files == NULL)
11035 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
11036
11037 find_entry.dwo_name = dwo_name;
11038 find_entry.comp_dir = comp_dir;
11039 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11040 INSERT);
11041
11042 return slot;
11043 }
11044
11045 static hashval_t
11046 hash_dwo_unit (const void *item)
11047 {
11048 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11049
11050 /* This drops the top 32 bits of the id, but is ok for a hash. */
11051 return dwo_unit->signature;
11052 }
11053
11054 static int
11055 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11056 {
11057 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11058 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11059
11060 /* The signature is assumed to be unique within the DWO file.
11061 So while object file CU dwo_id's always have the value zero,
11062 that's OK, assuming each object file DWO file has only one CU,
11063 and that's the rule for now. */
11064 return lhs->signature == rhs->signature;
11065 }
11066
11067 /* Allocate a hash table for DWO CUs,TUs.
11068 There is one of these tables for each of CUs,TUs for each DWO file. */
11069
11070 static htab_up
11071 allocate_dwo_unit_table ()
11072 {
11073 /* Start out with a pretty small number.
11074 Generally DWO files contain only one CU and maybe some TUs. */
11075 return htab_up (htab_create_alloc (3,
11076 hash_dwo_unit,
11077 eq_dwo_unit,
11078 NULL, xcalloc, xfree));
11079 }
11080
11081 /* die_reader_func for create_dwo_cu. */
11082
11083 static void
11084 create_dwo_cu_reader (const struct die_reader_specs *reader,
11085 const gdb_byte *info_ptr,
11086 struct die_info *comp_unit_die,
11087 struct dwo_file *dwo_file,
11088 struct dwo_unit *dwo_unit)
11089 {
11090 struct dwarf2_cu *cu = reader->cu;
11091 sect_offset sect_off = cu->per_cu->sect_off;
11092 struct dwarf2_section_info *section = cu->per_cu->section;
11093
11094 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11095 if (!signature.has_value ())
11096 {
11097 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11098 " its dwo_id [in module %s]"),
11099 sect_offset_str (sect_off), dwo_file->dwo_name);
11100 return;
11101 }
11102
11103 dwo_unit->dwo_file = dwo_file;
11104 dwo_unit->signature = *signature;
11105 dwo_unit->section = section;
11106 dwo_unit->sect_off = sect_off;
11107 dwo_unit->length = cu->per_cu->length;
11108
11109 if (dwarf_read_debug)
11110 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11111 sect_offset_str (sect_off),
11112 hex_string (dwo_unit->signature));
11113 }
11114
11115 /* Create the dwo_units for the CUs in a DWO_FILE.
11116 Note: This function processes DWO files only, not DWP files. */
11117
11118 static void
11119 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11120 dwarf2_cu *cu, struct dwo_file &dwo_file,
11121 dwarf2_section_info &section, htab_up &cus_htab)
11122 {
11123 struct objfile *objfile = dwarf2_per_objfile->objfile;
11124 const gdb_byte *info_ptr, *end_ptr;
11125
11126 section.read (objfile);
11127 info_ptr = section.buffer;
11128
11129 if (info_ptr == NULL)
11130 return;
11131
11132 if (dwarf_read_debug)
11133 {
11134 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11135 section.get_name (),
11136 section.get_file_name ());
11137 }
11138
11139 end_ptr = info_ptr + section.size;
11140 while (info_ptr < end_ptr)
11141 {
11142 struct dwarf2_per_cu_data per_cu;
11143 struct dwo_unit read_unit {};
11144 struct dwo_unit *dwo_unit;
11145 void **slot;
11146 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11147
11148 memset (&per_cu, 0, sizeof (per_cu));
11149 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11150 per_cu.is_debug_types = 0;
11151 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11152 per_cu.section = &section;
11153
11154 cutu_reader reader (&per_cu, cu, &dwo_file);
11155 if (!reader.dummy_p)
11156 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11157 &dwo_file, &read_unit);
11158 info_ptr += per_cu.length;
11159
11160 // If the unit could not be parsed, skip it.
11161 if (read_unit.dwo_file == NULL)
11162 continue;
11163
11164 if (cus_htab == NULL)
11165 cus_htab = allocate_dwo_unit_table ();
11166
11167 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11168 *dwo_unit = read_unit;
11169 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11170 gdb_assert (slot != NULL);
11171 if (*slot != NULL)
11172 {
11173 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11174 sect_offset dup_sect_off = dup_cu->sect_off;
11175
11176 complaint (_("debug cu entry at offset %s is duplicate to"
11177 " the entry at offset %s, signature %s"),
11178 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11179 hex_string (dwo_unit->signature));
11180 }
11181 *slot = (void *)dwo_unit;
11182 }
11183 }
11184
11185 /* DWP file .debug_{cu,tu}_index section format:
11186 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11187
11188 DWP Version 1:
11189
11190 Both index sections have the same format, and serve to map a 64-bit
11191 signature to a set of section numbers. Each section begins with a header,
11192 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11193 indexes, and a pool of 32-bit section numbers. The index sections will be
11194 aligned at 8-byte boundaries in the file.
11195
11196 The index section header consists of:
11197
11198 V, 32 bit version number
11199 -, 32 bits unused
11200 N, 32 bit number of compilation units or type units in the index
11201 M, 32 bit number of slots in the hash table
11202
11203 Numbers are recorded using the byte order of the application binary.
11204
11205 The hash table begins at offset 16 in the section, and consists of an array
11206 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11207 order of the application binary). Unused slots in the hash table are 0.
11208 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11209
11210 The parallel table begins immediately after the hash table
11211 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11212 array of 32-bit indexes (using the byte order of the application binary),
11213 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11214 table contains a 32-bit index into the pool of section numbers. For unused
11215 hash table slots, the corresponding entry in the parallel table will be 0.
11216
11217 The pool of section numbers begins immediately following the hash table
11218 (at offset 16 + 12 * M from the beginning of the section). The pool of
11219 section numbers consists of an array of 32-bit words (using the byte order
11220 of the application binary). Each item in the array is indexed starting
11221 from 0. The hash table entry provides the index of the first section
11222 number in the set. Additional section numbers in the set follow, and the
11223 set is terminated by a 0 entry (section number 0 is not used in ELF).
11224
11225 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11226 section must be the first entry in the set, and the .debug_abbrev.dwo must
11227 be the second entry. Other members of the set may follow in any order.
11228
11229 ---
11230
11231 DWP Version 2:
11232
11233 DWP Version 2 combines all the .debug_info, etc. sections into one,
11234 and the entries in the index tables are now offsets into these sections.
11235 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11236 section.
11237
11238 Index Section Contents:
11239 Header
11240 Hash Table of Signatures dwp_hash_table.hash_table
11241 Parallel Table of Indices dwp_hash_table.unit_table
11242 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11243 Table of Section Sizes dwp_hash_table.v2.sizes
11244
11245 The index section header consists of:
11246
11247 V, 32 bit version number
11248 L, 32 bit number of columns in the table of section offsets
11249 N, 32 bit number of compilation units or type units in the index
11250 M, 32 bit number of slots in the hash table
11251
11252 Numbers are recorded using the byte order of the application binary.
11253
11254 The hash table has the same format as version 1.
11255 The parallel table of indices has the same format as version 1,
11256 except that the entries are origin-1 indices into the table of sections
11257 offsets and the table of section sizes.
11258
11259 The table of offsets begins immediately following the parallel table
11260 (at offset 16 + 12 * M from the beginning of the section). The table is
11261 a two-dimensional array of 32-bit words (using the byte order of the
11262 application binary), with L columns and N+1 rows, in row-major order.
11263 Each row in the array is indexed starting from 0. The first row provides
11264 a key to the remaining rows: each column in this row provides an identifier
11265 for a debug section, and the offsets in the same column of subsequent rows
11266 refer to that section. The section identifiers are:
11267
11268 DW_SECT_INFO 1 .debug_info.dwo
11269 DW_SECT_TYPES 2 .debug_types.dwo
11270 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11271 DW_SECT_LINE 4 .debug_line.dwo
11272 DW_SECT_LOC 5 .debug_loc.dwo
11273 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11274 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11275 DW_SECT_MACRO 8 .debug_macro.dwo
11276
11277 The offsets provided by the CU and TU index sections are the base offsets
11278 for the contributions made by each CU or TU to the corresponding section
11279 in the package file. Each CU and TU header contains an abbrev_offset
11280 field, used to find the abbreviations table for that CU or TU within the
11281 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11282 be interpreted as relative to the base offset given in the index section.
11283 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11284 should be interpreted as relative to the base offset for .debug_line.dwo,
11285 and offsets into other debug sections obtained from DWARF attributes should
11286 also be interpreted as relative to the corresponding base offset.
11287
11288 The table of sizes begins immediately following the table of offsets.
11289 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11290 with L columns and N rows, in row-major order. Each row in the array is
11291 indexed starting from 1 (row 0 is shared by the two tables).
11292
11293 ---
11294
11295 Hash table lookup is handled the same in version 1 and 2:
11296
11297 We assume that N and M will not exceed 2^32 - 1.
11298 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11299
11300 Given a 64-bit compilation unit signature or a type signature S, an entry
11301 in the hash table is located as follows:
11302
11303 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11304 the low-order k bits all set to 1.
11305
11306 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11307
11308 3) If the hash table entry at index H matches the signature, use that
11309 entry. If the hash table entry at index H is unused (all zeroes),
11310 terminate the search: the signature is not present in the table.
11311
11312 4) Let H = (H + H') modulo M. Repeat at Step 3.
11313
11314 Because M > N and H' and M are relatively prime, the search is guaranteed
11315 to stop at an unused slot or find the match. */
11316
11317 /* Create a hash table to map DWO IDs to their CU/TU entry in
11318 .debug_{info,types}.dwo in DWP_FILE.
11319 Returns NULL if there isn't one.
11320 Note: This function processes DWP files only, not DWO files. */
11321
11322 static struct dwp_hash_table *
11323 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11324 struct dwp_file *dwp_file, int is_debug_types)
11325 {
11326 struct objfile *objfile = dwarf2_per_objfile->objfile;
11327 bfd *dbfd = dwp_file->dbfd.get ();
11328 const gdb_byte *index_ptr, *index_end;
11329 struct dwarf2_section_info *index;
11330 uint32_t version, nr_columns, nr_units, nr_slots;
11331 struct dwp_hash_table *htab;
11332
11333 if (is_debug_types)
11334 index = &dwp_file->sections.tu_index;
11335 else
11336 index = &dwp_file->sections.cu_index;
11337
11338 if (index->empty ())
11339 return NULL;
11340 index->read (objfile);
11341
11342 index_ptr = index->buffer;
11343 index_end = index_ptr + index->size;
11344
11345 version = read_4_bytes (dbfd, index_ptr);
11346 index_ptr += 4;
11347 if (version == 2)
11348 nr_columns = read_4_bytes (dbfd, index_ptr);
11349 else
11350 nr_columns = 0;
11351 index_ptr += 4;
11352 nr_units = read_4_bytes (dbfd, index_ptr);
11353 index_ptr += 4;
11354 nr_slots = read_4_bytes (dbfd, index_ptr);
11355 index_ptr += 4;
11356
11357 if (version != 1 && version != 2)
11358 {
11359 error (_("Dwarf Error: unsupported DWP file version (%s)"
11360 " [in module %s]"),
11361 pulongest (version), dwp_file->name);
11362 }
11363 if (nr_slots != (nr_slots & -nr_slots))
11364 {
11365 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11366 " is not power of 2 [in module %s]"),
11367 pulongest (nr_slots), dwp_file->name);
11368 }
11369
11370 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11371 htab->version = version;
11372 htab->nr_columns = nr_columns;
11373 htab->nr_units = nr_units;
11374 htab->nr_slots = nr_slots;
11375 htab->hash_table = index_ptr;
11376 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11377
11378 /* Exit early if the table is empty. */
11379 if (nr_slots == 0 || nr_units == 0
11380 || (version == 2 && nr_columns == 0))
11381 {
11382 /* All must be zero. */
11383 if (nr_slots != 0 || nr_units != 0
11384 || (version == 2 && nr_columns != 0))
11385 {
11386 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11387 " all zero [in modules %s]"),
11388 dwp_file->name);
11389 }
11390 return htab;
11391 }
11392
11393 if (version == 1)
11394 {
11395 htab->section_pool.v1.indices =
11396 htab->unit_table + sizeof (uint32_t) * nr_slots;
11397 /* It's harder to decide whether the section is too small in v1.
11398 V1 is deprecated anyway so we punt. */
11399 }
11400 else
11401 {
11402 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11403 int *ids = htab->section_pool.v2.section_ids;
11404 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11405 /* Reverse map for error checking. */
11406 int ids_seen[DW_SECT_MAX + 1];
11407 int i;
11408
11409 if (nr_columns < 2)
11410 {
11411 error (_("Dwarf Error: bad DWP hash table, too few columns"
11412 " in section table [in module %s]"),
11413 dwp_file->name);
11414 }
11415 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11416 {
11417 error (_("Dwarf Error: bad DWP hash table, too many columns"
11418 " in section table [in module %s]"),
11419 dwp_file->name);
11420 }
11421 memset (ids, 255, sizeof_ids);
11422 memset (ids_seen, 255, sizeof (ids_seen));
11423 for (i = 0; i < nr_columns; ++i)
11424 {
11425 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11426
11427 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11428 {
11429 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11430 " in section table [in module %s]"),
11431 id, dwp_file->name);
11432 }
11433 if (ids_seen[id] != -1)
11434 {
11435 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11436 " id %d in section table [in module %s]"),
11437 id, dwp_file->name);
11438 }
11439 ids_seen[id] = i;
11440 ids[i] = id;
11441 }
11442 /* Must have exactly one info or types section. */
11443 if (((ids_seen[DW_SECT_INFO] != -1)
11444 + (ids_seen[DW_SECT_TYPES] != -1))
11445 != 1)
11446 {
11447 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11448 " DWO info/types section [in module %s]"),
11449 dwp_file->name);
11450 }
11451 /* Must have an abbrev section. */
11452 if (ids_seen[DW_SECT_ABBREV] == -1)
11453 {
11454 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11455 " section [in module %s]"),
11456 dwp_file->name);
11457 }
11458 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11459 htab->section_pool.v2.sizes =
11460 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11461 * nr_units * nr_columns);
11462 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11463 * nr_units * nr_columns))
11464 > index_end)
11465 {
11466 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11467 " [in module %s]"),
11468 dwp_file->name);
11469 }
11470 }
11471
11472 return htab;
11473 }
11474
11475 /* Update SECTIONS with the data from SECTP.
11476
11477 This function is like the other "locate" section routines that are
11478 passed to bfd_map_over_sections, but in this context the sections to
11479 read comes from the DWP V1 hash table, not the full ELF section table.
11480
11481 The result is non-zero for success, or zero if an error was found. */
11482
11483 static int
11484 locate_v1_virtual_dwo_sections (asection *sectp,
11485 struct virtual_v1_dwo_sections *sections)
11486 {
11487 const struct dwop_section_names *names = &dwop_section_names;
11488
11489 if (section_is_p (sectp->name, &names->abbrev_dwo))
11490 {
11491 /* There can be only one. */
11492 if (sections->abbrev.s.section != NULL)
11493 return 0;
11494 sections->abbrev.s.section = sectp;
11495 sections->abbrev.size = bfd_section_size (sectp);
11496 }
11497 else if (section_is_p (sectp->name, &names->info_dwo)
11498 || section_is_p (sectp->name, &names->types_dwo))
11499 {
11500 /* There can be only one. */
11501 if (sections->info_or_types.s.section != NULL)
11502 return 0;
11503 sections->info_or_types.s.section = sectp;
11504 sections->info_or_types.size = bfd_section_size (sectp);
11505 }
11506 else if (section_is_p (sectp->name, &names->line_dwo))
11507 {
11508 /* There can be only one. */
11509 if (sections->line.s.section != NULL)
11510 return 0;
11511 sections->line.s.section = sectp;
11512 sections->line.size = bfd_section_size (sectp);
11513 }
11514 else if (section_is_p (sectp->name, &names->loc_dwo))
11515 {
11516 /* There can be only one. */
11517 if (sections->loc.s.section != NULL)
11518 return 0;
11519 sections->loc.s.section = sectp;
11520 sections->loc.size = bfd_section_size (sectp);
11521 }
11522 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11523 {
11524 /* There can be only one. */
11525 if (sections->macinfo.s.section != NULL)
11526 return 0;
11527 sections->macinfo.s.section = sectp;
11528 sections->macinfo.size = bfd_section_size (sectp);
11529 }
11530 else if (section_is_p (sectp->name, &names->macro_dwo))
11531 {
11532 /* There can be only one. */
11533 if (sections->macro.s.section != NULL)
11534 return 0;
11535 sections->macro.s.section = sectp;
11536 sections->macro.size = bfd_section_size (sectp);
11537 }
11538 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11539 {
11540 /* There can be only one. */
11541 if (sections->str_offsets.s.section != NULL)
11542 return 0;
11543 sections->str_offsets.s.section = sectp;
11544 sections->str_offsets.size = bfd_section_size (sectp);
11545 }
11546 else
11547 {
11548 /* No other kind of section is valid. */
11549 return 0;
11550 }
11551
11552 return 1;
11553 }
11554
11555 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11556 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11557 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11558 This is for DWP version 1 files. */
11559
11560 static struct dwo_unit *
11561 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11562 struct dwp_file *dwp_file,
11563 uint32_t unit_index,
11564 const char *comp_dir,
11565 ULONGEST signature, int is_debug_types)
11566 {
11567 struct objfile *objfile = dwarf2_per_objfile->objfile;
11568 const struct dwp_hash_table *dwp_htab =
11569 is_debug_types ? dwp_file->tus : dwp_file->cus;
11570 bfd *dbfd = dwp_file->dbfd.get ();
11571 const char *kind = is_debug_types ? "TU" : "CU";
11572 struct dwo_file *dwo_file;
11573 struct dwo_unit *dwo_unit;
11574 struct virtual_v1_dwo_sections sections;
11575 void **dwo_file_slot;
11576 int i;
11577
11578 gdb_assert (dwp_file->version == 1);
11579
11580 if (dwarf_read_debug)
11581 {
11582 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11583 kind,
11584 pulongest (unit_index), hex_string (signature),
11585 dwp_file->name);
11586 }
11587
11588 /* Fetch the sections of this DWO unit.
11589 Put a limit on the number of sections we look for so that bad data
11590 doesn't cause us to loop forever. */
11591
11592 #define MAX_NR_V1_DWO_SECTIONS \
11593 (1 /* .debug_info or .debug_types */ \
11594 + 1 /* .debug_abbrev */ \
11595 + 1 /* .debug_line */ \
11596 + 1 /* .debug_loc */ \
11597 + 1 /* .debug_str_offsets */ \
11598 + 1 /* .debug_macro or .debug_macinfo */ \
11599 + 1 /* trailing zero */)
11600
11601 memset (&sections, 0, sizeof (sections));
11602
11603 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11604 {
11605 asection *sectp;
11606 uint32_t section_nr =
11607 read_4_bytes (dbfd,
11608 dwp_htab->section_pool.v1.indices
11609 + (unit_index + i) * sizeof (uint32_t));
11610
11611 if (section_nr == 0)
11612 break;
11613 if (section_nr >= dwp_file->num_sections)
11614 {
11615 error (_("Dwarf Error: bad DWP hash table, section number too large"
11616 " [in module %s]"),
11617 dwp_file->name);
11618 }
11619
11620 sectp = dwp_file->elf_sections[section_nr];
11621 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11622 {
11623 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11624 " [in module %s]"),
11625 dwp_file->name);
11626 }
11627 }
11628
11629 if (i < 2
11630 || sections.info_or_types.empty ()
11631 || sections.abbrev.empty ())
11632 {
11633 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11634 " [in module %s]"),
11635 dwp_file->name);
11636 }
11637 if (i == MAX_NR_V1_DWO_SECTIONS)
11638 {
11639 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11640 " [in module %s]"),
11641 dwp_file->name);
11642 }
11643
11644 /* It's easier for the rest of the code if we fake a struct dwo_file and
11645 have dwo_unit "live" in that. At least for now.
11646
11647 The DWP file can be made up of a random collection of CUs and TUs.
11648 However, for each CU + set of TUs that came from the same original DWO
11649 file, we can combine them back into a virtual DWO file to save space
11650 (fewer struct dwo_file objects to allocate). Remember that for really
11651 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11652
11653 std::string virtual_dwo_name =
11654 string_printf ("virtual-dwo/%d-%d-%d-%d",
11655 sections.abbrev.get_id (),
11656 sections.line.get_id (),
11657 sections.loc.get_id (),
11658 sections.str_offsets.get_id ());
11659 /* Can we use an existing virtual DWO file? */
11660 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11661 virtual_dwo_name.c_str (),
11662 comp_dir);
11663 /* Create one if necessary. */
11664 if (*dwo_file_slot == NULL)
11665 {
11666 if (dwarf_read_debug)
11667 {
11668 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11669 virtual_dwo_name.c_str ());
11670 }
11671 dwo_file = new struct dwo_file;
11672 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11673 dwo_file->comp_dir = comp_dir;
11674 dwo_file->sections.abbrev = sections.abbrev;
11675 dwo_file->sections.line = sections.line;
11676 dwo_file->sections.loc = sections.loc;
11677 dwo_file->sections.macinfo = sections.macinfo;
11678 dwo_file->sections.macro = sections.macro;
11679 dwo_file->sections.str_offsets = sections.str_offsets;
11680 /* The "str" section is global to the entire DWP file. */
11681 dwo_file->sections.str = dwp_file->sections.str;
11682 /* The info or types section is assigned below to dwo_unit,
11683 there's no need to record it in dwo_file.
11684 Also, we can't simply record type sections in dwo_file because
11685 we record a pointer into the vector in dwo_unit. As we collect more
11686 types we'll grow the vector and eventually have to reallocate space
11687 for it, invalidating all copies of pointers into the previous
11688 contents. */
11689 *dwo_file_slot = dwo_file;
11690 }
11691 else
11692 {
11693 if (dwarf_read_debug)
11694 {
11695 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11696 virtual_dwo_name.c_str ());
11697 }
11698 dwo_file = (struct dwo_file *) *dwo_file_slot;
11699 }
11700
11701 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11702 dwo_unit->dwo_file = dwo_file;
11703 dwo_unit->signature = signature;
11704 dwo_unit->section =
11705 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11706 *dwo_unit->section = sections.info_or_types;
11707 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11708
11709 return dwo_unit;
11710 }
11711
11712 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11713 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11714 piece within that section used by a TU/CU, return a virtual section
11715 of just that piece. */
11716
11717 static struct dwarf2_section_info
11718 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11719 struct dwarf2_section_info *section,
11720 bfd_size_type offset, bfd_size_type size)
11721 {
11722 struct dwarf2_section_info result;
11723 asection *sectp;
11724
11725 gdb_assert (section != NULL);
11726 gdb_assert (!section->is_virtual);
11727
11728 memset (&result, 0, sizeof (result));
11729 result.s.containing_section = section;
11730 result.is_virtual = true;
11731
11732 if (size == 0)
11733 return result;
11734
11735 sectp = section->get_bfd_section ();
11736
11737 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11738 bounds of the real section. This is a pretty-rare event, so just
11739 flag an error (easier) instead of a warning and trying to cope. */
11740 if (sectp == NULL
11741 || offset + size > bfd_section_size (sectp))
11742 {
11743 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11744 " in section %s [in module %s]"),
11745 sectp ? bfd_section_name (sectp) : "<unknown>",
11746 objfile_name (dwarf2_per_objfile->objfile));
11747 }
11748
11749 result.virtual_offset = offset;
11750 result.size = size;
11751 return result;
11752 }
11753
11754 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11755 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11756 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11757 This is for DWP version 2 files. */
11758
11759 static struct dwo_unit *
11760 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11761 struct dwp_file *dwp_file,
11762 uint32_t unit_index,
11763 const char *comp_dir,
11764 ULONGEST signature, int is_debug_types)
11765 {
11766 struct objfile *objfile = dwarf2_per_objfile->objfile;
11767 const struct dwp_hash_table *dwp_htab =
11768 is_debug_types ? dwp_file->tus : dwp_file->cus;
11769 bfd *dbfd = dwp_file->dbfd.get ();
11770 const char *kind = is_debug_types ? "TU" : "CU";
11771 struct dwo_file *dwo_file;
11772 struct dwo_unit *dwo_unit;
11773 struct virtual_v2_dwo_sections sections;
11774 void **dwo_file_slot;
11775 int i;
11776
11777 gdb_assert (dwp_file->version == 2);
11778
11779 if (dwarf_read_debug)
11780 {
11781 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11782 kind,
11783 pulongest (unit_index), hex_string (signature),
11784 dwp_file->name);
11785 }
11786
11787 /* Fetch the section offsets of this DWO unit. */
11788
11789 memset (&sections, 0, sizeof (sections));
11790
11791 for (i = 0; i < dwp_htab->nr_columns; ++i)
11792 {
11793 uint32_t offset = read_4_bytes (dbfd,
11794 dwp_htab->section_pool.v2.offsets
11795 + (((unit_index - 1) * dwp_htab->nr_columns
11796 + i)
11797 * sizeof (uint32_t)));
11798 uint32_t size = read_4_bytes (dbfd,
11799 dwp_htab->section_pool.v2.sizes
11800 + (((unit_index - 1) * dwp_htab->nr_columns
11801 + i)
11802 * sizeof (uint32_t)));
11803
11804 switch (dwp_htab->section_pool.v2.section_ids[i])
11805 {
11806 case DW_SECT_INFO:
11807 case DW_SECT_TYPES:
11808 sections.info_or_types_offset = offset;
11809 sections.info_or_types_size = size;
11810 break;
11811 case DW_SECT_ABBREV:
11812 sections.abbrev_offset = offset;
11813 sections.abbrev_size = size;
11814 break;
11815 case DW_SECT_LINE:
11816 sections.line_offset = offset;
11817 sections.line_size = size;
11818 break;
11819 case DW_SECT_LOC:
11820 sections.loc_offset = offset;
11821 sections.loc_size = size;
11822 break;
11823 case DW_SECT_STR_OFFSETS:
11824 sections.str_offsets_offset = offset;
11825 sections.str_offsets_size = size;
11826 break;
11827 case DW_SECT_MACINFO:
11828 sections.macinfo_offset = offset;
11829 sections.macinfo_size = size;
11830 break;
11831 case DW_SECT_MACRO:
11832 sections.macro_offset = offset;
11833 sections.macro_size = size;
11834 break;
11835 }
11836 }
11837
11838 /* It's easier for the rest of the code if we fake a struct dwo_file and
11839 have dwo_unit "live" in that. At least for now.
11840
11841 The DWP file can be made up of a random collection of CUs and TUs.
11842 However, for each CU + set of TUs that came from the same original DWO
11843 file, we can combine them back into a virtual DWO file to save space
11844 (fewer struct dwo_file objects to allocate). Remember that for really
11845 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11846
11847 std::string virtual_dwo_name =
11848 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11849 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11850 (long) (sections.line_size ? sections.line_offset : 0),
11851 (long) (sections.loc_size ? sections.loc_offset : 0),
11852 (long) (sections.str_offsets_size
11853 ? sections.str_offsets_offset : 0));
11854 /* Can we use an existing virtual DWO file? */
11855 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11856 virtual_dwo_name.c_str (),
11857 comp_dir);
11858 /* Create one if necessary. */
11859 if (*dwo_file_slot == NULL)
11860 {
11861 if (dwarf_read_debug)
11862 {
11863 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11864 virtual_dwo_name.c_str ());
11865 }
11866 dwo_file = new struct dwo_file;
11867 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11868 dwo_file->comp_dir = comp_dir;
11869 dwo_file->sections.abbrev =
11870 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11871 sections.abbrev_offset, sections.abbrev_size);
11872 dwo_file->sections.line =
11873 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11874 sections.line_offset, sections.line_size);
11875 dwo_file->sections.loc =
11876 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11877 sections.loc_offset, sections.loc_size);
11878 dwo_file->sections.macinfo =
11879 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11880 sections.macinfo_offset, sections.macinfo_size);
11881 dwo_file->sections.macro =
11882 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11883 sections.macro_offset, sections.macro_size);
11884 dwo_file->sections.str_offsets =
11885 create_dwp_v2_section (dwarf2_per_objfile,
11886 &dwp_file->sections.str_offsets,
11887 sections.str_offsets_offset,
11888 sections.str_offsets_size);
11889 /* The "str" section is global to the entire DWP file. */
11890 dwo_file->sections.str = dwp_file->sections.str;
11891 /* The info or types section is assigned below to dwo_unit,
11892 there's no need to record it in dwo_file.
11893 Also, we can't simply record type sections in dwo_file because
11894 we record a pointer into the vector in dwo_unit. As we collect more
11895 types we'll grow the vector and eventually have to reallocate space
11896 for it, invalidating all copies of pointers into the previous
11897 contents. */
11898 *dwo_file_slot = dwo_file;
11899 }
11900 else
11901 {
11902 if (dwarf_read_debug)
11903 {
11904 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11905 virtual_dwo_name.c_str ());
11906 }
11907 dwo_file = (struct dwo_file *) *dwo_file_slot;
11908 }
11909
11910 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11911 dwo_unit->dwo_file = dwo_file;
11912 dwo_unit->signature = signature;
11913 dwo_unit->section =
11914 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11915 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11916 is_debug_types
11917 ? &dwp_file->sections.types
11918 : &dwp_file->sections.info,
11919 sections.info_or_types_offset,
11920 sections.info_or_types_size);
11921 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11922
11923 return dwo_unit;
11924 }
11925
11926 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11927 Returns NULL if the signature isn't found. */
11928
11929 static struct dwo_unit *
11930 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11931 struct dwp_file *dwp_file, const char *comp_dir,
11932 ULONGEST signature, int is_debug_types)
11933 {
11934 const struct dwp_hash_table *dwp_htab =
11935 is_debug_types ? dwp_file->tus : dwp_file->cus;
11936 bfd *dbfd = dwp_file->dbfd.get ();
11937 uint32_t mask = dwp_htab->nr_slots - 1;
11938 uint32_t hash = signature & mask;
11939 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11940 unsigned int i;
11941 void **slot;
11942 struct dwo_unit find_dwo_cu;
11943
11944 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11945 find_dwo_cu.signature = signature;
11946 slot = htab_find_slot (is_debug_types
11947 ? dwp_file->loaded_tus.get ()
11948 : dwp_file->loaded_cus.get (),
11949 &find_dwo_cu, INSERT);
11950
11951 if (*slot != NULL)
11952 return (struct dwo_unit *) *slot;
11953
11954 /* Use a for loop so that we don't loop forever on bad debug info. */
11955 for (i = 0; i < dwp_htab->nr_slots; ++i)
11956 {
11957 ULONGEST signature_in_table;
11958
11959 signature_in_table =
11960 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11961 if (signature_in_table == signature)
11962 {
11963 uint32_t unit_index =
11964 read_4_bytes (dbfd,
11965 dwp_htab->unit_table + hash * sizeof (uint32_t));
11966
11967 if (dwp_file->version == 1)
11968 {
11969 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11970 dwp_file, unit_index,
11971 comp_dir, signature,
11972 is_debug_types);
11973 }
11974 else
11975 {
11976 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11977 dwp_file, unit_index,
11978 comp_dir, signature,
11979 is_debug_types);
11980 }
11981 return (struct dwo_unit *) *slot;
11982 }
11983 if (signature_in_table == 0)
11984 return NULL;
11985 hash = (hash + hash2) & mask;
11986 }
11987
11988 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11989 " [in module %s]"),
11990 dwp_file->name);
11991 }
11992
11993 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11994 Open the file specified by FILE_NAME and hand it off to BFD for
11995 preliminary analysis. Return a newly initialized bfd *, which
11996 includes a canonicalized copy of FILE_NAME.
11997 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11998 SEARCH_CWD is true if the current directory is to be searched.
11999 It will be searched before debug-file-directory.
12000 If successful, the file is added to the bfd include table of the
12001 objfile's bfd (see gdb_bfd_record_inclusion).
12002 If unable to find/open the file, return NULL.
12003 NOTE: This function is derived from symfile_bfd_open. */
12004
12005 static gdb_bfd_ref_ptr
12006 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12007 const char *file_name, int is_dwp, int search_cwd)
12008 {
12009 int desc;
12010 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12011 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12012 to debug_file_directory. */
12013 const char *search_path;
12014 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12015
12016 gdb::unique_xmalloc_ptr<char> search_path_holder;
12017 if (search_cwd)
12018 {
12019 if (*debug_file_directory != '\0')
12020 {
12021 search_path_holder.reset (concat (".", dirname_separator_string,
12022 debug_file_directory,
12023 (char *) NULL));
12024 search_path = search_path_holder.get ();
12025 }
12026 else
12027 search_path = ".";
12028 }
12029 else
12030 search_path = debug_file_directory;
12031
12032 openp_flags flags = OPF_RETURN_REALPATH;
12033 if (is_dwp)
12034 flags |= OPF_SEARCH_IN_PATH;
12035
12036 gdb::unique_xmalloc_ptr<char> absolute_name;
12037 desc = openp (search_path, flags, file_name,
12038 O_RDONLY | O_BINARY, &absolute_name);
12039 if (desc < 0)
12040 return NULL;
12041
12042 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12043 gnutarget, desc));
12044 if (sym_bfd == NULL)
12045 return NULL;
12046 bfd_set_cacheable (sym_bfd.get (), 1);
12047
12048 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12049 return NULL;
12050
12051 /* Success. Record the bfd as having been included by the objfile's bfd.
12052 This is important because things like demangled_names_hash lives in the
12053 objfile's per_bfd space and may have references to things like symbol
12054 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12055 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12056
12057 return sym_bfd;
12058 }
12059
12060 /* Try to open DWO file FILE_NAME.
12061 COMP_DIR is the DW_AT_comp_dir attribute.
12062 The result is the bfd handle of the file.
12063 If there is a problem finding or opening the file, return NULL.
12064 Upon success, the canonicalized path of the file is stored in the bfd,
12065 same as symfile_bfd_open. */
12066
12067 static gdb_bfd_ref_ptr
12068 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12069 const char *file_name, const char *comp_dir)
12070 {
12071 if (IS_ABSOLUTE_PATH (file_name))
12072 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12073 0 /*is_dwp*/, 0 /*search_cwd*/);
12074
12075 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12076
12077 if (comp_dir != NULL)
12078 {
12079 gdb::unique_xmalloc_ptr<char> path_to_try
12080 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12081
12082 /* NOTE: If comp_dir is a relative path, this will also try the
12083 search path, which seems useful. */
12084 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12085 path_to_try.get (),
12086 0 /*is_dwp*/,
12087 1 /*search_cwd*/));
12088 if (abfd != NULL)
12089 return abfd;
12090 }
12091
12092 /* That didn't work, try debug-file-directory, which, despite its name,
12093 is a list of paths. */
12094
12095 if (*debug_file_directory == '\0')
12096 return NULL;
12097
12098 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12099 0 /*is_dwp*/, 1 /*search_cwd*/);
12100 }
12101
12102 /* This function is mapped across the sections and remembers the offset and
12103 size of each of the DWO debugging sections we are interested in. */
12104
12105 static void
12106 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12107 {
12108 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12109 const struct dwop_section_names *names = &dwop_section_names;
12110
12111 if (section_is_p (sectp->name, &names->abbrev_dwo))
12112 {
12113 dwo_sections->abbrev.s.section = sectp;
12114 dwo_sections->abbrev.size = bfd_section_size (sectp);
12115 }
12116 else if (section_is_p (sectp->name, &names->info_dwo))
12117 {
12118 dwo_sections->info.s.section = sectp;
12119 dwo_sections->info.size = bfd_section_size (sectp);
12120 }
12121 else if (section_is_p (sectp->name, &names->line_dwo))
12122 {
12123 dwo_sections->line.s.section = sectp;
12124 dwo_sections->line.size = bfd_section_size (sectp);
12125 }
12126 else if (section_is_p (sectp->name, &names->loc_dwo))
12127 {
12128 dwo_sections->loc.s.section = sectp;
12129 dwo_sections->loc.size = bfd_section_size (sectp);
12130 }
12131 else if (section_is_p (sectp->name, &names->loclists_dwo))
12132 {
12133 dwo_sections->loclists.s.section = sectp;
12134 dwo_sections->loclists.size = bfd_section_size (sectp);
12135 }
12136 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12137 {
12138 dwo_sections->macinfo.s.section = sectp;
12139 dwo_sections->macinfo.size = bfd_section_size (sectp);
12140 }
12141 else if (section_is_p (sectp->name, &names->macro_dwo))
12142 {
12143 dwo_sections->macro.s.section = sectp;
12144 dwo_sections->macro.size = bfd_section_size (sectp);
12145 }
12146 else if (section_is_p (sectp->name, &names->str_dwo))
12147 {
12148 dwo_sections->str.s.section = sectp;
12149 dwo_sections->str.size = bfd_section_size (sectp);
12150 }
12151 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12152 {
12153 dwo_sections->str_offsets.s.section = sectp;
12154 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12155 }
12156 else if (section_is_p (sectp->name, &names->types_dwo))
12157 {
12158 struct dwarf2_section_info type_section;
12159
12160 memset (&type_section, 0, sizeof (type_section));
12161 type_section.s.section = sectp;
12162 type_section.size = bfd_section_size (sectp);
12163 dwo_sections->types.push_back (type_section);
12164 }
12165 }
12166
12167 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12168 by PER_CU. This is for the non-DWP case.
12169 The result is NULL if DWO_NAME can't be found. */
12170
12171 static struct dwo_file *
12172 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12173 const char *dwo_name, const char *comp_dir)
12174 {
12175 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12176
12177 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12178 if (dbfd == NULL)
12179 {
12180 if (dwarf_read_debug)
12181 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12182 return NULL;
12183 }
12184
12185 dwo_file_up dwo_file (new struct dwo_file);
12186 dwo_file->dwo_name = dwo_name;
12187 dwo_file->comp_dir = comp_dir;
12188 dwo_file->dbfd = std::move (dbfd);
12189
12190 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12191 &dwo_file->sections);
12192
12193 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12194 dwo_file->sections.info, dwo_file->cus);
12195
12196 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12197 dwo_file->sections.types, dwo_file->tus);
12198
12199 if (dwarf_read_debug)
12200 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12201
12202 return dwo_file.release ();
12203 }
12204
12205 /* This function is mapped across the sections and remembers the offset and
12206 size of each of the DWP debugging sections common to version 1 and 2 that
12207 we are interested in. */
12208
12209 static void
12210 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12211 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->str_dwo))
12224 {
12225 dwp_file->sections.str.s.section = sectp;
12226 dwp_file->sections.str.size = bfd_section_size (sectp);
12227 }
12228 else if (section_is_p (sectp->name, &names->cu_index))
12229 {
12230 dwp_file->sections.cu_index.s.section = sectp;
12231 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12232 }
12233 else if (section_is_p (sectp->name, &names->tu_index))
12234 {
12235 dwp_file->sections.tu_index.s.section = sectp;
12236 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12237 }
12238 }
12239
12240 /* This function is mapped across the sections and remembers the offset and
12241 size of each of the DWP version 2 debugging sections that we are interested
12242 in. This is split into a separate function because we don't know if we
12243 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12244
12245 static void
12246 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12247 {
12248 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12249 const struct dwop_section_names *names = &dwop_section_names;
12250 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12251
12252 /* Record the ELF section number for later lookup: this is what the
12253 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12254 gdb_assert (elf_section_nr < dwp_file->num_sections);
12255 dwp_file->elf_sections[elf_section_nr] = sectp;
12256
12257 /* Look for specific sections that we need. */
12258 if (section_is_p (sectp->name, &names->abbrev_dwo))
12259 {
12260 dwp_file->sections.abbrev.s.section = sectp;
12261 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12262 }
12263 else if (section_is_p (sectp->name, &names->info_dwo))
12264 {
12265 dwp_file->sections.info.s.section = sectp;
12266 dwp_file->sections.info.size = bfd_section_size (sectp);
12267 }
12268 else if (section_is_p (sectp->name, &names->line_dwo))
12269 {
12270 dwp_file->sections.line.s.section = sectp;
12271 dwp_file->sections.line.size = bfd_section_size (sectp);
12272 }
12273 else if (section_is_p (sectp->name, &names->loc_dwo))
12274 {
12275 dwp_file->sections.loc.s.section = sectp;
12276 dwp_file->sections.loc.size = bfd_section_size (sectp);
12277 }
12278 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12279 {
12280 dwp_file->sections.macinfo.s.section = sectp;
12281 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12282 }
12283 else if (section_is_p (sectp->name, &names->macro_dwo))
12284 {
12285 dwp_file->sections.macro.s.section = sectp;
12286 dwp_file->sections.macro.size = bfd_section_size (sectp);
12287 }
12288 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12289 {
12290 dwp_file->sections.str_offsets.s.section = sectp;
12291 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12292 }
12293 else if (section_is_p (sectp->name, &names->types_dwo))
12294 {
12295 dwp_file->sections.types.s.section = sectp;
12296 dwp_file->sections.types.size = bfd_section_size (sectp);
12297 }
12298 }
12299
12300 /* Hash function for dwp_file loaded CUs/TUs. */
12301
12302 static hashval_t
12303 hash_dwp_loaded_cutus (const void *item)
12304 {
12305 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12306
12307 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12308 return dwo_unit->signature;
12309 }
12310
12311 /* Equality function for dwp_file loaded CUs/TUs. */
12312
12313 static int
12314 eq_dwp_loaded_cutus (const void *a, const void *b)
12315 {
12316 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12317 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12318
12319 return dua->signature == dub->signature;
12320 }
12321
12322 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12323
12324 static htab_up
12325 allocate_dwp_loaded_cutus_table ()
12326 {
12327 return htab_up (htab_create_alloc (3,
12328 hash_dwp_loaded_cutus,
12329 eq_dwp_loaded_cutus,
12330 NULL, xcalloc, xfree));
12331 }
12332
12333 /* Try to open DWP file FILE_NAME.
12334 The result is the bfd handle of the file.
12335 If there is a problem finding or opening the file, return NULL.
12336 Upon success, the canonicalized path of the file is stored in the bfd,
12337 same as symfile_bfd_open. */
12338
12339 static gdb_bfd_ref_ptr
12340 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12341 const char *file_name)
12342 {
12343 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12344 1 /*is_dwp*/,
12345 1 /*search_cwd*/));
12346 if (abfd != NULL)
12347 return abfd;
12348
12349 /* Work around upstream bug 15652.
12350 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12351 [Whether that's a "bug" is debatable, but it is getting in our way.]
12352 We have no real idea where the dwp file is, because gdb's realpath-ing
12353 of the executable's path may have discarded the needed info.
12354 [IWBN if the dwp file name was recorded in the executable, akin to
12355 .gnu_debuglink, but that doesn't exist yet.]
12356 Strip the directory from FILE_NAME and search again. */
12357 if (*debug_file_directory != '\0')
12358 {
12359 /* Don't implicitly search the current directory here.
12360 If the user wants to search "." to handle this case,
12361 it must be added to debug-file-directory. */
12362 return try_open_dwop_file (dwarf2_per_objfile,
12363 lbasename (file_name), 1 /*is_dwp*/,
12364 0 /*search_cwd*/);
12365 }
12366
12367 return NULL;
12368 }
12369
12370 /* Initialize the use of the DWP file for the current objfile.
12371 By convention the name of the DWP file is ${objfile}.dwp.
12372 The result is NULL if it can't be found. */
12373
12374 static std::unique_ptr<struct dwp_file>
12375 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12376 {
12377 struct objfile *objfile = dwarf2_per_objfile->objfile;
12378
12379 /* Try to find first .dwp for the binary file before any symbolic links
12380 resolving. */
12381
12382 /* If the objfile is a debug file, find the name of the real binary
12383 file and get the name of dwp file from there. */
12384 std::string dwp_name;
12385 if (objfile->separate_debug_objfile_backlink != NULL)
12386 {
12387 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12388 const char *backlink_basename = lbasename (backlink->original_name);
12389
12390 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12391 }
12392 else
12393 dwp_name = objfile->original_name;
12394
12395 dwp_name += ".dwp";
12396
12397 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12398 if (dbfd == NULL
12399 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12400 {
12401 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12402 dwp_name = objfile_name (objfile);
12403 dwp_name += ".dwp";
12404 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12405 }
12406
12407 if (dbfd == NULL)
12408 {
12409 if (dwarf_read_debug)
12410 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12411 return std::unique_ptr<dwp_file> ();
12412 }
12413
12414 const char *name = bfd_get_filename (dbfd.get ());
12415 std::unique_ptr<struct dwp_file> dwp_file
12416 (new struct dwp_file (name, std::move (dbfd)));
12417
12418 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12419 dwp_file->elf_sections =
12420 OBSTACK_CALLOC (&objfile->objfile_obstack,
12421 dwp_file->num_sections, asection *);
12422
12423 bfd_map_over_sections (dwp_file->dbfd.get (),
12424 dwarf2_locate_common_dwp_sections,
12425 dwp_file.get ());
12426
12427 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12428 0);
12429
12430 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12431 1);
12432
12433 /* The DWP file version is stored in the hash table. Oh well. */
12434 if (dwp_file->cus && dwp_file->tus
12435 && dwp_file->cus->version != dwp_file->tus->version)
12436 {
12437 /* Technically speaking, we should try to limp along, but this is
12438 pretty bizarre. We use pulongest here because that's the established
12439 portability solution (e.g, we cannot use %u for uint32_t). */
12440 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12441 " TU version %s [in DWP file %s]"),
12442 pulongest (dwp_file->cus->version),
12443 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12444 }
12445
12446 if (dwp_file->cus)
12447 dwp_file->version = dwp_file->cus->version;
12448 else if (dwp_file->tus)
12449 dwp_file->version = dwp_file->tus->version;
12450 else
12451 dwp_file->version = 2;
12452
12453 if (dwp_file->version == 2)
12454 bfd_map_over_sections (dwp_file->dbfd.get (),
12455 dwarf2_locate_v2_dwp_sections,
12456 dwp_file.get ());
12457
12458 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12459 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12460
12461 if (dwarf_read_debug)
12462 {
12463 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12464 fprintf_unfiltered (gdb_stdlog,
12465 " %s CUs, %s TUs\n",
12466 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12467 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12468 }
12469
12470 return dwp_file;
12471 }
12472
12473 /* Wrapper around open_and_init_dwp_file, only open it once. */
12474
12475 static struct dwp_file *
12476 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12477 {
12478 if (! dwarf2_per_objfile->dwp_checked)
12479 {
12480 dwarf2_per_objfile->dwp_file
12481 = open_and_init_dwp_file (dwarf2_per_objfile);
12482 dwarf2_per_objfile->dwp_checked = 1;
12483 }
12484 return dwarf2_per_objfile->dwp_file.get ();
12485 }
12486
12487 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12488 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12489 or in the DWP file for the objfile, referenced by THIS_UNIT.
12490 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12491 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12492
12493 This is called, for example, when wanting to read a variable with a
12494 complex location. Therefore we don't want to do file i/o for every call.
12495 Therefore we don't want to look for a DWO file on every call.
12496 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12497 then we check if we've already seen DWO_NAME, and only THEN do we check
12498 for a DWO file.
12499
12500 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12501 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12502
12503 static struct dwo_unit *
12504 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12505 const char *dwo_name, const char *comp_dir,
12506 ULONGEST signature, int is_debug_types)
12507 {
12508 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12509 struct objfile *objfile = dwarf2_per_objfile->objfile;
12510 const char *kind = is_debug_types ? "TU" : "CU";
12511 void **dwo_file_slot;
12512 struct dwo_file *dwo_file;
12513 struct dwp_file *dwp_file;
12514
12515 /* First see if there's a DWP file.
12516 If we have a DWP file but didn't find the DWO inside it, don't
12517 look for the original DWO file. It makes gdb behave differently
12518 depending on whether one is debugging in the build tree. */
12519
12520 dwp_file = get_dwp_file (dwarf2_per_objfile);
12521 if (dwp_file != NULL)
12522 {
12523 const struct dwp_hash_table *dwp_htab =
12524 is_debug_types ? dwp_file->tus : dwp_file->cus;
12525
12526 if (dwp_htab != NULL)
12527 {
12528 struct dwo_unit *dwo_cutu =
12529 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12530 signature, is_debug_types);
12531
12532 if (dwo_cutu != NULL)
12533 {
12534 if (dwarf_read_debug)
12535 {
12536 fprintf_unfiltered (gdb_stdlog,
12537 "Virtual DWO %s %s found: @%s\n",
12538 kind, hex_string (signature),
12539 host_address_to_string (dwo_cutu));
12540 }
12541 return dwo_cutu;
12542 }
12543 }
12544 }
12545 else
12546 {
12547 /* No DWP file, look for the DWO file. */
12548
12549 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12550 dwo_name, comp_dir);
12551 if (*dwo_file_slot == NULL)
12552 {
12553 /* Read in the file and build a table of the CUs/TUs it contains. */
12554 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12555 }
12556 /* NOTE: This will be NULL if unable to open the file. */
12557 dwo_file = (struct dwo_file *) *dwo_file_slot;
12558
12559 if (dwo_file != NULL)
12560 {
12561 struct dwo_unit *dwo_cutu = NULL;
12562
12563 if (is_debug_types && dwo_file->tus)
12564 {
12565 struct dwo_unit find_dwo_cutu;
12566
12567 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12568 find_dwo_cutu.signature = signature;
12569 dwo_cutu
12570 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12571 &find_dwo_cutu);
12572 }
12573 else if (!is_debug_types && dwo_file->cus)
12574 {
12575 struct dwo_unit find_dwo_cutu;
12576
12577 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12578 find_dwo_cutu.signature = signature;
12579 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12580 &find_dwo_cutu);
12581 }
12582
12583 if (dwo_cutu != NULL)
12584 {
12585 if (dwarf_read_debug)
12586 {
12587 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12588 kind, dwo_name, hex_string (signature),
12589 host_address_to_string (dwo_cutu));
12590 }
12591 return dwo_cutu;
12592 }
12593 }
12594 }
12595
12596 /* We didn't find it. This could mean a dwo_id mismatch, or
12597 someone deleted the DWO/DWP file, or the search path isn't set up
12598 correctly to find the file. */
12599
12600 if (dwarf_read_debug)
12601 {
12602 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12603 kind, dwo_name, hex_string (signature));
12604 }
12605
12606 /* This is a warning and not a complaint because it can be caused by
12607 pilot error (e.g., user accidentally deleting the DWO). */
12608 {
12609 /* Print the name of the DWP file if we looked there, helps the user
12610 better diagnose the problem. */
12611 std::string dwp_text;
12612
12613 if (dwp_file != NULL)
12614 dwp_text = string_printf (" [in DWP file %s]",
12615 lbasename (dwp_file->name));
12616
12617 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12618 " [in module %s]"),
12619 kind, dwo_name, hex_string (signature),
12620 dwp_text.c_str (),
12621 this_unit->is_debug_types ? "TU" : "CU",
12622 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12623 }
12624 return NULL;
12625 }
12626
12627 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12628 See lookup_dwo_cutu_unit for details. */
12629
12630 static struct dwo_unit *
12631 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12632 const char *dwo_name, const char *comp_dir,
12633 ULONGEST signature)
12634 {
12635 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12636 }
12637
12638 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12639 See lookup_dwo_cutu_unit for details. */
12640
12641 static struct dwo_unit *
12642 lookup_dwo_type_unit (struct signatured_type *this_tu,
12643 const char *dwo_name, const char *comp_dir)
12644 {
12645 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12646 }
12647
12648 /* Traversal function for queue_and_load_all_dwo_tus. */
12649
12650 static int
12651 queue_and_load_dwo_tu (void **slot, void *info)
12652 {
12653 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12654 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12655 ULONGEST signature = dwo_unit->signature;
12656 struct signatured_type *sig_type =
12657 lookup_dwo_signatured_type (per_cu->cu, signature);
12658
12659 if (sig_type != NULL)
12660 {
12661 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12662
12663 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12664 a real dependency of PER_CU on SIG_TYPE. That is detected later
12665 while processing PER_CU. */
12666 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12667 load_full_type_unit (sig_cu);
12668 per_cu->imported_symtabs_push (sig_cu);
12669 }
12670
12671 return 1;
12672 }
12673
12674 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12675 The DWO may have the only definition of the type, though it may not be
12676 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12677 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12678
12679 static void
12680 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12681 {
12682 struct dwo_unit *dwo_unit;
12683 struct dwo_file *dwo_file;
12684
12685 gdb_assert (!per_cu->is_debug_types);
12686 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12687 gdb_assert (per_cu->cu != NULL);
12688
12689 dwo_unit = per_cu->cu->dwo_unit;
12690 gdb_assert (dwo_unit != NULL);
12691
12692 dwo_file = dwo_unit->dwo_file;
12693 if (dwo_file->tus != NULL)
12694 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12695 per_cu);
12696 }
12697
12698 /* Read in various DIEs. */
12699
12700 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12701 Inherit only the children of the DW_AT_abstract_origin DIE not being
12702 already referenced by DW_AT_abstract_origin from the children of the
12703 current DIE. */
12704
12705 static void
12706 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12707 {
12708 struct die_info *child_die;
12709 sect_offset *offsetp;
12710 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12711 struct die_info *origin_die;
12712 /* Iterator of the ORIGIN_DIE children. */
12713 struct die_info *origin_child_die;
12714 struct attribute *attr;
12715 struct dwarf2_cu *origin_cu;
12716 struct pending **origin_previous_list_in_scope;
12717
12718 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12719 if (!attr)
12720 return;
12721
12722 /* Note that following die references may follow to a die in a
12723 different cu. */
12724
12725 origin_cu = cu;
12726 origin_die = follow_die_ref (die, attr, &origin_cu);
12727
12728 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12729 symbols in. */
12730 origin_previous_list_in_scope = origin_cu->list_in_scope;
12731 origin_cu->list_in_scope = cu->list_in_scope;
12732
12733 if (die->tag != origin_die->tag
12734 && !(die->tag == DW_TAG_inlined_subroutine
12735 && origin_die->tag == DW_TAG_subprogram))
12736 complaint (_("DIE %s and its abstract origin %s have different tags"),
12737 sect_offset_str (die->sect_off),
12738 sect_offset_str (origin_die->sect_off));
12739
12740 std::vector<sect_offset> offsets;
12741
12742 for (child_die = die->child;
12743 child_die && child_die->tag;
12744 child_die = child_die->sibling)
12745 {
12746 struct die_info *child_origin_die;
12747 struct dwarf2_cu *child_origin_cu;
12748
12749 /* We are trying to process concrete instance entries:
12750 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12751 it's not relevant to our analysis here. i.e. detecting DIEs that are
12752 present in the abstract instance but not referenced in the concrete
12753 one. */
12754 if (child_die->tag == DW_TAG_call_site
12755 || child_die->tag == DW_TAG_GNU_call_site)
12756 continue;
12757
12758 /* For each CHILD_DIE, find the corresponding child of
12759 ORIGIN_DIE. If there is more than one layer of
12760 DW_AT_abstract_origin, follow them all; there shouldn't be,
12761 but GCC versions at least through 4.4 generate this (GCC PR
12762 40573). */
12763 child_origin_die = child_die;
12764 child_origin_cu = cu;
12765 while (1)
12766 {
12767 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12768 child_origin_cu);
12769 if (attr == NULL)
12770 break;
12771 child_origin_die = follow_die_ref (child_origin_die, attr,
12772 &child_origin_cu);
12773 }
12774
12775 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12776 counterpart may exist. */
12777 if (child_origin_die != child_die)
12778 {
12779 if (child_die->tag != child_origin_die->tag
12780 && !(child_die->tag == DW_TAG_inlined_subroutine
12781 && child_origin_die->tag == DW_TAG_subprogram))
12782 complaint (_("Child DIE %s and its abstract origin %s have "
12783 "different tags"),
12784 sect_offset_str (child_die->sect_off),
12785 sect_offset_str (child_origin_die->sect_off));
12786 if (child_origin_die->parent != origin_die)
12787 complaint (_("Child DIE %s and its abstract origin %s have "
12788 "different parents"),
12789 sect_offset_str (child_die->sect_off),
12790 sect_offset_str (child_origin_die->sect_off));
12791 else
12792 offsets.push_back (child_origin_die->sect_off);
12793 }
12794 }
12795 std::sort (offsets.begin (), offsets.end ());
12796 sect_offset *offsets_end = offsets.data () + offsets.size ();
12797 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12798 if (offsetp[-1] == *offsetp)
12799 complaint (_("Multiple children of DIE %s refer "
12800 "to DIE %s as their abstract origin"),
12801 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12802
12803 offsetp = offsets.data ();
12804 origin_child_die = origin_die->child;
12805 while (origin_child_die && origin_child_die->tag)
12806 {
12807 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12808 while (offsetp < offsets_end
12809 && *offsetp < origin_child_die->sect_off)
12810 offsetp++;
12811 if (offsetp >= offsets_end
12812 || *offsetp > origin_child_die->sect_off)
12813 {
12814 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12815 Check whether we're already processing ORIGIN_CHILD_DIE.
12816 This can happen with mutually referenced abstract_origins.
12817 PR 16581. */
12818 if (!origin_child_die->in_process)
12819 process_die (origin_child_die, origin_cu);
12820 }
12821 origin_child_die = origin_child_die->sibling;
12822 }
12823 origin_cu->list_in_scope = origin_previous_list_in_scope;
12824
12825 if (cu != origin_cu)
12826 compute_delayed_physnames (origin_cu);
12827 }
12828
12829 static void
12830 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12831 {
12832 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12833 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12834 struct context_stack *newobj;
12835 CORE_ADDR lowpc;
12836 CORE_ADDR highpc;
12837 struct die_info *child_die;
12838 struct attribute *attr, *call_line, *call_file;
12839 const char *name;
12840 CORE_ADDR baseaddr;
12841 struct block *block;
12842 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12843 std::vector<struct symbol *> template_args;
12844 struct template_symbol *templ_func = NULL;
12845
12846 if (inlined_func)
12847 {
12848 /* If we do not have call site information, we can't show the
12849 caller of this inlined function. That's too confusing, so
12850 only use the scope for local variables. */
12851 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12852 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12853 if (call_line == NULL || call_file == NULL)
12854 {
12855 read_lexical_block_scope (die, cu);
12856 return;
12857 }
12858 }
12859
12860 baseaddr = objfile->text_section_offset ();
12861
12862 name = dwarf2_name (die, cu);
12863
12864 /* Ignore functions with missing or empty names. These are actually
12865 illegal according to the DWARF standard. */
12866 if (name == NULL)
12867 {
12868 complaint (_("missing name for subprogram DIE at %s"),
12869 sect_offset_str (die->sect_off));
12870 return;
12871 }
12872
12873 /* Ignore functions with missing or invalid low and high pc attributes. */
12874 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12875 <= PC_BOUNDS_INVALID)
12876 {
12877 attr = dwarf2_attr (die, DW_AT_external, cu);
12878 if (!attr || !DW_UNSND (attr))
12879 complaint (_("cannot get low and high bounds "
12880 "for subprogram DIE at %s"),
12881 sect_offset_str (die->sect_off));
12882 return;
12883 }
12884
12885 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12886 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12887
12888 /* If we have any template arguments, then we must allocate a
12889 different sort of symbol. */
12890 for (child_die = die->child; child_die; child_die = child_die->sibling)
12891 {
12892 if (child_die->tag == DW_TAG_template_type_param
12893 || child_die->tag == DW_TAG_template_value_param)
12894 {
12895 templ_func = allocate_template_symbol (objfile);
12896 templ_func->subclass = SYMBOL_TEMPLATE;
12897 break;
12898 }
12899 }
12900
12901 newobj = cu->get_builder ()->push_context (0, lowpc);
12902 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12903 (struct symbol *) templ_func);
12904
12905 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12906 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12907 cu->language);
12908
12909 /* If there is a location expression for DW_AT_frame_base, record
12910 it. */
12911 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12912 if (attr != nullptr)
12913 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12914
12915 /* If there is a location for the static link, record it. */
12916 newobj->static_link = NULL;
12917 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12918 if (attr != nullptr)
12919 {
12920 newobj->static_link
12921 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12922 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12923 cu->per_cu->addr_type ());
12924 }
12925
12926 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12927
12928 if (die->child != NULL)
12929 {
12930 child_die = die->child;
12931 while (child_die && child_die->tag)
12932 {
12933 if (child_die->tag == DW_TAG_template_type_param
12934 || child_die->tag == DW_TAG_template_value_param)
12935 {
12936 struct symbol *arg = new_symbol (child_die, NULL, cu);
12937
12938 if (arg != NULL)
12939 template_args.push_back (arg);
12940 }
12941 else
12942 process_die (child_die, cu);
12943 child_die = child_die->sibling;
12944 }
12945 }
12946
12947 inherit_abstract_dies (die, cu);
12948
12949 /* If we have a DW_AT_specification, we might need to import using
12950 directives from the context of the specification DIE. See the
12951 comment in determine_prefix. */
12952 if (cu->language == language_cplus
12953 && dwarf2_attr (die, DW_AT_specification, cu))
12954 {
12955 struct dwarf2_cu *spec_cu = cu;
12956 struct die_info *spec_die = die_specification (die, &spec_cu);
12957
12958 while (spec_die)
12959 {
12960 child_die = spec_die->child;
12961 while (child_die && child_die->tag)
12962 {
12963 if (child_die->tag == DW_TAG_imported_module)
12964 process_die (child_die, spec_cu);
12965 child_die = child_die->sibling;
12966 }
12967
12968 /* In some cases, GCC generates specification DIEs that
12969 themselves contain DW_AT_specification attributes. */
12970 spec_die = die_specification (spec_die, &spec_cu);
12971 }
12972 }
12973
12974 struct context_stack cstk = cu->get_builder ()->pop_context ();
12975 /* Make a block for the local symbols within. */
12976 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12977 cstk.static_link, lowpc, highpc);
12978
12979 /* For C++, set the block's scope. */
12980 if ((cu->language == language_cplus
12981 || cu->language == language_fortran
12982 || cu->language == language_d
12983 || cu->language == language_rust)
12984 && cu->processing_has_namespace_info)
12985 block_set_scope (block, determine_prefix (die, cu),
12986 &objfile->objfile_obstack);
12987
12988 /* If we have address ranges, record them. */
12989 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12990
12991 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12992
12993 /* Attach template arguments to function. */
12994 if (!template_args.empty ())
12995 {
12996 gdb_assert (templ_func != NULL);
12997
12998 templ_func->n_template_arguments = template_args.size ();
12999 templ_func->template_arguments
13000 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13001 templ_func->n_template_arguments);
13002 memcpy (templ_func->template_arguments,
13003 template_args.data (),
13004 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13005
13006 /* Make sure that the symtab is set on the new symbols. Even
13007 though they don't appear in this symtab directly, other parts
13008 of gdb assume that symbols do, and this is reasonably
13009 true. */
13010 for (symbol *sym : template_args)
13011 symbol_set_symtab (sym, symbol_symtab (templ_func));
13012 }
13013
13014 /* In C++, we can have functions nested inside functions (e.g., when
13015 a function declares a class that has methods). This means that
13016 when we finish processing a function scope, we may need to go
13017 back to building a containing block's symbol lists. */
13018 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13019 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13020
13021 /* If we've finished processing a top-level function, subsequent
13022 symbols go in the file symbol list. */
13023 if (cu->get_builder ()->outermost_context_p ())
13024 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13025 }
13026
13027 /* Process all the DIES contained within a lexical block scope. Start
13028 a new scope, process the dies, and then close the scope. */
13029
13030 static void
13031 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13032 {
13033 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13034 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13035 CORE_ADDR lowpc, highpc;
13036 struct die_info *child_die;
13037 CORE_ADDR baseaddr;
13038
13039 baseaddr = objfile->text_section_offset ();
13040
13041 /* Ignore blocks with missing or invalid low and high pc attributes. */
13042 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13043 as multiple lexical blocks? Handling children in a sane way would
13044 be nasty. Might be easier to properly extend generic blocks to
13045 describe ranges. */
13046 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13047 {
13048 case PC_BOUNDS_NOT_PRESENT:
13049 /* DW_TAG_lexical_block has no attributes, process its children as if
13050 there was no wrapping by that DW_TAG_lexical_block.
13051 GCC does no longer produces such DWARF since GCC r224161. */
13052 for (child_die = die->child;
13053 child_die != NULL && child_die->tag;
13054 child_die = child_die->sibling)
13055 process_die (child_die, cu);
13056 return;
13057 case PC_BOUNDS_INVALID:
13058 return;
13059 }
13060 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13061 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13062
13063 cu->get_builder ()->push_context (0, lowpc);
13064 if (die->child != NULL)
13065 {
13066 child_die = die->child;
13067 while (child_die && child_die->tag)
13068 {
13069 process_die (child_die, cu);
13070 child_die = child_die->sibling;
13071 }
13072 }
13073 inherit_abstract_dies (die, cu);
13074 struct context_stack cstk = cu->get_builder ()->pop_context ();
13075
13076 if (*cu->get_builder ()->get_local_symbols () != NULL
13077 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13078 {
13079 struct block *block
13080 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13081 cstk.start_addr, highpc);
13082
13083 /* Note that recording ranges after traversing children, as we
13084 do here, means that recording a parent's ranges entails
13085 walking across all its children's ranges as they appear in
13086 the address map, which is quadratic behavior.
13087
13088 It would be nicer to record the parent's ranges before
13089 traversing its children, simply overriding whatever you find
13090 there. But since we don't even decide whether to create a
13091 block until after we've traversed its children, that's hard
13092 to do. */
13093 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13094 }
13095 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13096 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13097 }
13098
13099 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13100
13101 static void
13102 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13103 {
13104 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13105 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13106 CORE_ADDR pc, baseaddr;
13107 struct attribute *attr;
13108 struct call_site *call_site, call_site_local;
13109 void **slot;
13110 int nparams;
13111 struct die_info *child_die;
13112
13113 baseaddr = objfile->text_section_offset ();
13114
13115 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13116 if (attr == NULL)
13117 {
13118 /* This was a pre-DWARF-5 GNU extension alias
13119 for DW_AT_call_return_pc. */
13120 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13121 }
13122 if (!attr)
13123 {
13124 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13125 "DIE %s [in module %s]"),
13126 sect_offset_str (die->sect_off), objfile_name (objfile));
13127 return;
13128 }
13129 pc = attr->value_as_address () + baseaddr;
13130 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13131
13132 if (cu->call_site_htab == NULL)
13133 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13134 NULL, &objfile->objfile_obstack,
13135 hashtab_obstack_allocate, NULL);
13136 call_site_local.pc = pc;
13137 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13138 if (*slot != NULL)
13139 {
13140 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13141 "DIE %s [in module %s]"),
13142 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13143 objfile_name (objfile));
13144 return;
13145 }
13146
13147 /* Count parameters at the caller. */
13148
13149 nparams = 0;
13150 for (child_die = die->child; child_die && child_die->tag;
13151 child_die = child_die->sibling)
13152 {
13153 if (child_die->tag != DW_TAG_call_site_parameter
13154 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13155 {
13156 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13157 "DW_TAG_call_site child DIE %s [in module %s]"),
13158 child_die->tag, sect_offset_str (child_die->sect_off),
13159 objfile_name (objfile));
13160 continue;
13161 }
13162
13163 nparams++;
13164 }
13165
13166 call_site
13167 = ((struct call_site *)
13168 obstack_alloc (&objfile->objfile_obstack,
13169 sizeof (*call_site)
13170 + (sizeof (*call_site->parameter) * (nparams - 1))));
13171 *slot = call_site;
13172 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13173 call_site->pc = pc;
13174
13175 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13176 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13177 {
13178 struct die_info *func_die;
13179
13180 /* Skip also over DW_TAG_inlined_subroutine. */
13181 for (func_die = die->parent;
13182 func_die && func_die->tag != DW_TAG_subprogram
13183 && func_die->tag != DW_TAG_subroutine_type;
13184 func_die = func_die->parent);
13185
13186 /* DW_AT_call_all_calls is a superset
13187 of DW_AT_call_all_tail_calls. */
13188 if (func_die
13189 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13190 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13191 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13192 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13193 {
13194 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13195 not complete. But keep CALL_SITE for look ups via call_site_htab,
13196 both the initial caller containing the real return address PC and
13197 the final callee containing the current PC of a chain of tail
13198 calls do not need to have the tail call list complete. But any
13199 function candidate for a virtual tail call frame searched via
13200 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13201 determined unambiguously. */
13202 }
13203 else
13204 {
13205 struct type *func_type = NULL;
13206
13207 if (func_die)
13208 func_type = get_die_type (func_die, cu);
13209 if (func_type != NULL)
13210 {
13211 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13212
13213 /* Enlist this call site to the function. */
13214 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13215 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13216 }
13217 else
13218 complaint (_("Cannot find function owning DW_TAG_call_site "
13219 "DIE %s [in module %s]"),
13220 sect_offset_str (die->sect_off), objfile_name (objfile));
13221 }
13222 }
13223
13224 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13225 if (attr == NULL)
13226 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13227 if (attr == NULL)
13228 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13229 if (attr == NULL)
13230 {
13231 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13232 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13233 }
13234 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13235 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13236 /* Keep NULL DWARF_BLOCK. */;
13237 else if (attr->form_is_block ())
13238 {
13239 struct dwarf2_locexpr_baton *dlbaton;
13240
13241 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13242 dlbaton->data = DW_BLOCK (attr)->data;
13243 dlbaton->size = DW_BLOCK (attr)->size;
13244 dlbaton->per_cu = cu->per_cu;
13245
13246 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13247 }
13248 else if (attr->form_is_ref ())
13249 {
13250 struct dwarf2_cu *target_cu = cu;
13251 struct die_info *target_die;
13252
13253 target_die = follow_die_ref (die, attr, &target_cu);
13254 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13255 if (die_is_declaration (target_die, target_cu))
13256 {
13257 const char *target_physname;
13258
13259 /* Prefer the mangled name; otherwise compute the demangled one. */
13260 target_physname = dw2_linkage_name (target_die, target_cu);
13261 if (target_physname == NULL)
13262 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13263 if (target_physname == NULL)
13264 complaint (_("DW_AT_call_target target DIE has invalid "
13265 "physname, for referencing DIE %s [in module %s]"),
13266 sect_offset_str (die->sect_off), objfile_name (objfile));
13267 else
13268 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13269 }
13270 else
13271 {
13272 CORE_ADDR lowpc;
13273
13274 /* DW_AT_entry_pc should be preferred. */
13275 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13276 <= PC_BOUNDS_INVALID)
13277 complaint (_("DW_AT_call_target target DIE has invalid "
13278 "low pc, for referencing DIE %s [in module %s]"),
13279 sect_offset_str (die->sect_off), objfile_name (objfile));
13280 else
13281 {
13282 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13283 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13284 }
13285 }
13286 }
13287 else
13288 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13289 "block nor reference, for DIE %s [in module %s]"),
13290 sect_offset_str (die->sect_off), objfile_name (objfile));
13291
13292 call_site->per_cu = cu->per_cu;
13293
13294 for (child_die = die->child;
13295 child_die && child_die->tag;
13296 child_die = child_die->sibling)
13297 {
13298 struct call_site_parameter *parameter;
13299 struct attribute *loc, *origin;
13300
13301 if (child_die->tag != DW_TAG_call_site_parameter
13302 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13303 {
13304 /* Already printed the complaint above. */
13305 continue;
13306 }
13307
13308 gdb_assert (call_site->parameter_count < nparams);
13309 parameter = &call_site->parameter[call_site->parameter_count];
13310
13311 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13312 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13313 register is contained in DW_AT_call_value. */
13314
13315 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13316 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13317 if (origin == NULL)
13318 {
13319 /* This was a pre-DWARF-5 GNU extension alias
13320 for DW_AT_call_parameter. */
13321 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13322 }
13323 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13324 {
13325 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13326
13327 sect_offset sect_off = origin->get_ref_die_offset ();
13328 if (!cu->header.offset_in_cu_p (sect_off))
13329 {
13330 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13331 binding can be done only inside one CU. Such referenced DIE
13332 therefore cannot be even moved to DW_TAG_partial_unit. */
13333 complaint (_("DW_AT_call_parameter offset is not in CU for "
13334 "DW_TAG_call_site child DIE %s [in module %s]"),
13335 sect_offset_str (child_die->sect_off),
13336 objfile_name (objfile));
13337 continue;
13338 }
13339 parameter->u.param_cu_off
13340 = (cu_offset) (sect_off - cu->header.sect_off);
13341 }
13342 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13343 {
13344 complaint (_("No DW_FORM_block* DW_AT_location for "
13345 "DW_TAG_call_site child DIE %s [in module %s]"),
13346 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13347 continue;
13348 }
13349 else
13350 {
13351 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13352 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13353 if (parameter->u.dwarf_reg != -1)
13354 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13355 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13356 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13357 &parameter->u.fb_offset))
13358 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13359 else
13360 {
13361 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13362 "for DW_FORM_block* DW_AT_location is supported for "
13363 "DW_TAG_call_site child DIE %s "
13364 "[in module %s]"),
13365 sect_offset_str (child_die->sect_off),
13366 objfile_name (objfile));
13367 continue;
13368 }
13369 }
13370
13371 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13372 if (attr == NULL)
13373 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13374 if (attr == NULL || !attr->form_is_block ())
13375 {
13376 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13377 "DW_TAG_call_site child DIE %s [in module %s]"),
13378 sect_offset_str (child_die->sect_off),
13379 objfile_name (objfile));
13380 continue;
13381 }
13382 parameter->value = DW_BLOCK (attr)->data;
13383 parameter->value_size = DW_BLOCK (attr)->size;
13384
13385 /* Parameters are not pre-cleared by memset above. */
13386 parameter->data_value = NULL;
13387 parameter->data_value_size = 0;
13388 call_site->parameter_count++;
13389
13390 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13391 if (attr == NULL)
13392 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13393 if (attr != nullptr)
13394 {
13395 if (!attr->form_is_block ())
13396 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13397 "DW_TAG_call_site child DIE %s [in module %s]"),
13398 sect_offset_str (child_die->sect_off),
13399 objfile_name (objfile));
13400 else
13401 {
13402 parameter->data_value = DW_BLOCK (attr)->data;
13403 parameter->data_value_size = DW_BLOCK (attr)->size;
13404 }
13405 }
13406 }
13407 }
13408
13409 /* Helper function for read_variable. If DIE represents a virtual
13410 table, then return the type of the concrete object that is
13411 associated with the virtual table. Otherwise, return NULL. */
13412
13413 static struct type *
13414 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13415 {
13416 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13417 if (attr == NULL)
13418 return NULL;
13419
13420 /* Find the type DIE. */
13421 struct die_info *type_die = NULL;
13422 struct dwarf2_cu *type_cu = cu;
13423
13424 if (attr->form_is_ref ())
13425 type_die = follow_die_ref (die, attr, &type_cu);
13426 if (type_die == NULL)
13427 return NULL;
13428
13429 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13430 return NULL;
13431 return die_containing_type (type_die, type_cu);
13432 }
13433
13434 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13435
13436 static void
13437 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13438 {
13439 struct rust_vtable_symbol *storage = NULL;
13440
13441 if (cu->language == language_rust)
13442 {
13443 struct type *containing_type = rust_containing_type (die, cu);
13444
13445 if (containing_type != NULL)
13446 {
13447 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13448
13449 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13450 initialize_objfile_symbol (storage);
13451 storage->concrete_type = containing_type;
13452 storage->subclass = SYMBOL_RUST_VTABLE;
13453 }
13454 }
13455
13456 struct symbol *res = new_symbol (die, NULL, cu, storage);
13457 struct attribute *abstract_origin
13458 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13459 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13460 if (res == NULL && loc && abstract_origin)
13461 {
13462 /* We have a variable without a name, but with a location and an abstract
13463 origin. This may be a concrete instance of an abstract variable
13464 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13465 later. */
13466 struct dwarf2_cu *origin_cu = cu;
13467 struct die_info *origin_die
13468 = follow_die_ref (die, abstract_origin, &origin_cu);
13469 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13470 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13471 }
13472 }
13473
13474 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13475 reading .debug_rnglists.
13476 Callback's type should be:
13477 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13478 Return true if the attributes are present and valid, otherwise,
13479 return false. */
13480
13481 template <typename Callback>
13482 static bool
13483 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13484 Callback &&callback)
13485 {
13486 struct dwarf2_per_objfile *dwarf2_per_objfile
13487 = cu->per_cu->dwarf2_per_objfile;
13488 struct objfile *objfile = dwarf2_per_objfile->objfile;
13489 bfd *obfd = objfile->obfd;
13490 /* Base address selection entry. */
13491 gdb::optional<CORE_ADDR> base;
13492 const gdb_byte *buffer;
13493 CORE_ADDR baseaddr;
13494 bool overflow = false;
13495
13496 base = cu->base_address;
13497
13498 dwarf2_per_objfile->rnglists.read (objfile);
13499 if (offset >= dwarf2_per_objfile->rnglists.size)
13500 {
13501 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13502 offset);
13503 return false;
13504 }
13505 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13506
13507 baseaddr = objfile->text_section_offset ();
13508
13509 while (1)
13510 {
13511 /* Initialize it due to a false compiler warning. */
13512 CORE_ADDR range_beginning = 0, range_end = 0;
13513 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13514 + dwarf2_per_objfile->rnglists.size);
13515 unsigned int bytes_read;
13516
13517 if (buffer == buf_end)
13518 {
13519 overflow = true;
13520 break;
13521 }
13522 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13523 switch (rlet)
13524 {
13525 case DW_RLE_end_of_list:
13526 break;
13527 case DW_RLE_base_address:
13528 if (buffer + cu->header.addr_size > buf_end)
13529 {
13530 overflow = true;
13531 break;
13532 }
13533 base = cu->header.read_address (obfd, buffer, &bytes_read);
13534 buffer += bytes_read;
13535 break;
13536 case DW_RLE_start_length:
13537 if (buffer + cu->header.addr_size > buf_end)
13538 {
13539 overflow = true;
13540 break;
13541 }
13542 range_beginning = cu->header.read_address (obfd, buffer,
13543 &bytes_read);
13544 buffer += bytes_read;
13545 range_end = (range_beginning
13546 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13547 buffer += bytes_read;
13548 if (buffer > buf_end)
13549 {
13550 overflow = true;
13551 break;
13552 }
13553 break;
13554 case DW_RLE_offset_pair:
13555 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13556 buffer += bytes_read;
13557 if (buffer > buf_end)
13558 {
13559 overflow = true;
13560 break;
13561 }
13562 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13563 buffer += bytes_read;
13564 if (buffer > buf_end)
13565 {
13566 overflow = true;
13567 break;
13568 }
13569 break;
13570 case DW_RLE_start_end:
13571 if (buffer + 2 * cu->header.addr_size > buf_end)
13572 {
13573 overflow = true;
13574 break;
13575 }
13576 range_beginning = cu->header.read_address (obfd, buffer,
13577 &bytes_read);
13578 buffer += bytes_read;
13579 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13580 buffer += bytes_read;
13581 break;
13582 default:
13583 complaint (_("Invalid .debug_rnglists data (no base address)"));
13584 return false;
13585 }
13586 if (rlet == DW_RLE_end_of_list || overflow)
13587 break;
13588 if (rlet == DW_RLE_base_address)
13589 continue;
13590
13591 if (!base.has_value ())
13592 {
13593 /* We have no valid base address for the ranges
13594 data. */
13595 complaint (_("Invalid .debug_rnglists data (no base address)"));
13596 return false;
13597 }
13598
13599 if (range_beginning > range_end)
13600 {
13601 /* Inverted range entries are invalid. */
13602 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13603 return false;
13604 }
13605
13606 /* Empty range entries have no effect. */
13607 if (range_beginning == range_end)
13608 continue;
13609
13610 range_beginning += *base;
13611 range_end += *base;
13612
13613 /* A not-uncommon case of bad debug info.
13614 Don't pollute the addrmap with bad data. */
13615 if (range_beginning + baseaddr == 0
13616 && !dwarf2_per_objfile->has_section_at_zero)
13617 {
13618 complaint (_(".debug_rnglists entry has start address of zero"
13619 " [in module %s]"), objfile_name (objfile));
13620 continue;
13621 }
13622
13623 callback (range_beginning, range_end);
13624 }
13625
13626 if (overflow)
13627 {
13628 complaint (_("Offset %d is not terminated "
13629 "for DW_AT_ranges attribute"),
13630 offset);
13631 return false;
13632 }
13633
13634 return true;
13635 }
13636
13637 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13638 Callback's type should be:
13639 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13640 Return 1 if the attributes are present and valid, otherwise, return 0. */
13641
13642 template <typename Callback>
13643 static int
13644 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13645 Callback &&callback)
13646 {
13647 struct dwarf2_per_objfile *dwarf2_per_objfile
13648 = cu->per_cu->dwarf2_per_objfile;
13649 struct objfile *objfile = dwarf2_per_objfile->objfile;
13650 struct comp_unit_head *cu_header = &cu->header;
13651 bfd *obfd = objfile->obfd;
13652 unsigned int addr_size = cu_header->addr_size;
13653 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13654 /* Base address selection entry. */
13655 gdb::optional<CORE_ADDR> base;
13656 unsigned int dummy;
13657 const gdb_byte *buffer;
13658 CORE_ADDR baseaddr;
13659
13660 if (cu_header->version >= 5)
13661 return dwarf2_rnglists_process (offset, cu, callback);
13662
13663 base = cu->base_address;
13664
13665 dwarf2_per_objfile->ranges.read (objfile);
13666 if (offset >= dwarf2_per_objfile->ranges.size)
13667 {
13668 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13669 offset);
13670 return 0;
13671 }
13672 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13673
13674 baseaddr = objfile->text_section_offset ();
13675
13676 while (1)
13677 {
13678 CORE_ADDR range_beginning, range_end;
13679
13680 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13681 buffer += addr_size;
13682 range_end = cu->header.read_address (obfd, buffer, &dummy);
13683 buffer += addr_size;
13684 offset += 2 * addr_size;
13685
13686 /* An end of list marker is a pair of zero addresses. */
13687 if (range_beginning == 0 && range_end == 0)
13688 /* Found the end of list entry. */
13689 break;
13690
13691 /* Each base address selection entry is a pair of 2 values.
13692 The first is the largest possible address, the second is
13693 the base address. Check for a base address here. */
13694 if ((range_beginning & mask) == mask)
13695 {
13696 /* If we found the largest possible address, then we already
13697 have the base address in range_end. */
13698 base = range_end;
13699 continue;
13700 }
13701
13702 if (!base.has_value ())
13703 {
13704 /* We have no valid base address for the ranges
13705 data. */
13706 complaint (_("Invalid .debug_ranges data (no base address)"));
13707 return 0;
13708 }
13709
13710 if (range_beginning > range_end)
13711 {
13712 /* Inverted range entries are invalid. */
13713 complaint (_("Invalid .debug_ranges data (inverted range)"));
13714 return 0;
13715 }
13716
13717 /* Empty range entries have no effect. */
13718 if (range_beginning == range_end)
13719 continue;
13720
13721 range_beginning += *base;
13722 range_end += *base;
13723
13724 /* A not-uncommon case of bad debug info.
13725 Don't pollute the addrmap with bad data. */
13726 if (range_beginning + baseaddr == 0
13727 && !dwarf2_per_objfile->has_section_at_zero)
13728 {
13729 complaint (_(".debug_ranges entry has start address of zero"
13730 " [in module %s]"), objfile_name (objfile));
13731 continue;
13732 }
13733
13734 callback (range_beginning, range_end);
13735 }
13736
13737 return 1;
13738 }
13739
13740 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13741 Return 1 if the attributes are present and valid, otherwise, return 0.
13742 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13743
13744 static int
13745 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13746 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13747 dwarf2_psymtab *ranges_pst)
13748 {
13749 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13750 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13751 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13752 int low_set = 0;
13753 CORE_ADDR low = 0;
13754 CORE_ADDR high = 0;
13755 int retval;
13756
13757 retval = dwarf2_ranges_process (offset, cu,
13758 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13759 {
13760 if (ranges_pst != NULL)
13761 {
13762 CORE_ADDR lowpc;
13763 CORE_ADDR highpc;
13764
13765 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13766 range_beginning + baseaddr)
13767 - baseaddr);
13768 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13769 range_end + baseaddr)
13770 - baseaddr);
13771 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13772 lowpc, highpc - 1, ranges_pst);
13773 }
13774
13775 /* FIXME: This is recording everything as a low-high
13776 segment of consecutive addresses. We should have a
13777 data structure for discontiguous block ranges
13778 instead. */
13779 if (! low_set)
13780 {
13781 low = range_beginning;
13782 high = range_end;
13783 low_set = 1;
13784 }
13785 else
13786 {
13787 if (range_beginning < low)
13788 low = range_beginning;
13789 if (range_end > high)
13790 high = range_end;
13791 }
13792 });
13793 if (!retval)
13794 return 0;
13795
13796 if (! low_set)
13797 /* If the first entry is an end-of-list marker, the range
13798 describes an empty scope, i.e. no instructions. */
13799 return 0;
13800
13801 if (low_return)
13802 *low_return = low;
13803 if (high_return)
13804 *high_return = high;
13805 return 1;
13806 }
13807
13808 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13809 definition for the return value. *LOWPC and *HIGHPC are set iff
13810 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13811
13812 static enum pc_bounds_kind
13813 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13814 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13815 dwarf2_psymtab *pst)
13816 {
13817 struct dwarf2_per_objfile *dwarf2_per_objfile
13818 = cu->per_cu->dwarf2_per_objfile;
13819 struct attribute *attr;
13820 struct attribute *attr_high;
13821 CORE_ADDR low = 0;
13822 CORE_ADDR high = 0;
13823 enum pc_bounds_kind ret;
13824
13825 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13826 if (attr_high)
13827 {
13828 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13829 if (attr != nullptr)
13830 {
13831 low = attr->value_as_address ();
13832 high = attr_high->value_as_address ();
13833 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13834 high += low;
13835 }
13836 else
13837 /* Found high w/o low attribute. */
13838 return PC_BOUNDS_INVALID;
13839
13840 /* Found consecutive range of addresses. */
13841 ret = PC_BOUNDS_HIGH_LOW;
13842 }
13843 else
13844 {
13845 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13846 if (attr != NULL)
13847 {
13848 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13849 We take advantage of the fact that DW_AT_ranges does not appear
13850 in DW_TAG_compile_unit of DWO files. */
13851 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13852 unsigned int ranges_offset = (DW_UNSND (attr)
13853 + (need_ranges_base
13854 ? cu->ranges_base
13855 : 0));
13856
13857 /* Value of the DW_AT_ranges attribute is the offset in the
13858 .debug_ranges section. */
13859 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13860 return PC_BOUNDS_INVALID;
13861 /* Found discontinuous range of addresses. */
13862 ret = PC_BOUNDS_RANGES;
13863 }
13864 else
13865 return PC_BOUNDS_NOT_PRESENT;
13866 }
13867
13868 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13869 if (high <= low)
13870 return PC_BOUNDS_INVALID;
13871
13872 /* When using the GNU linker, .gnu.linkonce. sections are used to
13873 eliminate duplicate copies of functions and vtables and such.
13874 The linker will arbitrarily choose one and discard the others.
13875 The AT_*_pc values for such functions refer to local labels in
13876 these sections. If the section from that file was discarded, the
13877 labels are not in the output, so the relocs get a value of 0.
13878 If this is a discarded function, mark the pc bounds as invalid,
13879 so that GDB will ignore it. */
13880 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13881 return PC_BOUNDS_INVALID;
13882
13883 *lowpc = low;
13884 if (highpc)
13885 *highpc = high;
13886 return ret;
13887 }
13888
13889 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13890 its low and high PC addresses. Do nothing if these addresses could not
13891 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13892 and HIGHPC to the high address if greater than HIGHPC. */
13893
13894 static void
13895 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13896 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13897 struct dwarf2_cu *cu)
13898 {
13899 CORE_ADDR low, high;
13900 struct die_info *child = die->child;
13901
13902 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13903 {
13904 *lowpc = std::min (*lowpc, low);
13905 *highpc = std::max (*highpc, high);
13906 }
13907
13908 /* If the language does not allow nested subprograms (either inside
13909 subprograms or lexical blocks), we're done. */
13910 if (cu->language != language_ada)
13911 return;
13912
13913 /* Check all the children of the given DIE. If it contains nested
13914 subprograms, then check their pc bounds. Likewise, we need to
13915 check lexical blocks as well, as they may also contain subprogram
13916 definitions. */
13917 while (child && child->tag)
13918 {
13919 if (child->tag == DW_TAG_subprogram
13920 || child->tag == DW_TAG_lexical_block)
13921 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13922 child = child->sibling;
13923 }
13924 }
13925
13926 /* Get the low and high pc's represented by the scope DIE, and store
13927 them in *LOWPC and *HIGHPC. If the correct values can't be
13928 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13929
13930 static void
13931 get_scope_pc_bounds (struct die_info *die,
13932 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13933 struct dwarf2_cu *cu)
13934 {
13935 CORE_ADDR best_low = (CORE_ADDR) -1;
13936 CORE_ADDR best_high = (CORE_ADDR) 0;
13937 CORE_ADDR current_low, current_high;
13938
13939 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13940 >= PC_BOUNDS_RANGES)
13941 {
13942 best_low = current_low;
13943 best_high = current_high;
13944 }
13945 else
13946 {
13947 struct die_info *child = die->child;
13948
13949 while (child && child->tag)
13950 {
13951 switch (child->tag) {
13952 case DW_TAG_subprogram:
13953 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13954 break;
13955 case DW_TAG_namespace:
13956 case DW_TAG_module:
13957 /* FIXME: carlton/2004-01-16: Should we do this for
13958 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13959 that current GCC's always emit the DIEs corresponding
13960 to definitions of methods of classes as children of a
13961 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13962 the DIEs giving the declarations, which could be
13963 anywhere). But I don't see any reason why the
13964 standards says that they have to be there. */
13965 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13966
13967 if (current_low != ((CORE_ADDR) -1))
13968 {
13969 best_low = std::min (best_low, current_low);
13970 best_high = std::max (best_high, current_high);
13971 }
13972 break;
13973 default:
13974 /* Ignore. */
13975 break;
13976 }
13977
13978 child = child->sibling;
13979 }
13980 }
13981
13982 *lowpc = best_low;
13983 *highpc = best_high;
13984 }
13985
13986 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13987 in DIE. */
13988
13989 static void
13990 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13991 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13992 {
13993 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13994 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13995 struct attribute *attr;
13996 struct attribute *attr_high;
13997
13998 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13999 if (attr_high)
14000 {
14001 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14002 if (attr != nullptr)
14003 {
14004 CORE_ADDR low = attr->value_as_address ();
14005 CORE_ADDR high = attr_high->value_as_address ();
14006
14007 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14008 high += low;
14009
14010 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14011 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14012 cu->get_builder ()->record_block_range (block, low, high - 1);
14013 }
14014 }
14015
14016 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14017 if (attr != nullptr)
14018 {
14019 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14020 We take advantage of the fact that DW_AT_ranges does not appear
14021 in DW_TAG_compile_unit of DWO files. */
14022 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14023
14024 /* The value of the DW_AT_ranges attribute is the offset of the
14025 address range list in the .debug_ranges section. */
14026 unsigned long offset = (DW_UNSND (attr)
14027 + (need_ranges_base ? cu->ranges_base : 0));
14028
14029 std::vector<blockrange> blockvec;
14030 dwarf2_ranges_process (offset, cu,
14031 [&] (CORE_ADDR start, CORE_ADDR end)
14032 {
14033 start += baseaddr;
14034 end += baseaddr;
14035 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14036 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14037 cu->get_builder ()->record_block_range (block, start, end - 1);
14038 blockvec.emplace_back (start, end);
14039 });
14040
14041 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14042 }
14043 }
14044
14045 /* Check whether the producer field indicates either of GCC < 4.6, or the
14046 Intel C/C++ compiler, and cache the result in CU. */
14047
14048 static void
14049 check_producer (struct dwarf2_cu *cu)
14050 {
14051 int major, minor;
14052
14053 if (cu->producer == NULL)
14054 {
14055 /* For unknown compilers expect their behavior is DWARF version
14056 compliant.
14057
14058 GCC started to support .debug_types sections by -gdwarf-4 since
14059 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14060 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14061 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14062 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14063 }
14064 else if (producer_is_gcc (cu->producer, &major, &minor))
14065 {
14066 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14067 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14068 }
14069 else if (producer_is_icc (cu->producer, &major, &minor))
14070 {
14071 cu->producer_is_icc = true;
14072 cu->producer_is_icc_lt_14 = major < 14;
14073 }
14074 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14075 cu->producer_is_codewarrior = true;
14076 else
14077 {
14078 /* For other non-GCC compilers, expect their behavior is DWARF version
14079 compliant. */
14080 }
14081
14082 cu->checked_producer = true;
14083 }
14084
14085 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14086 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14087 during 4.6.0 experimental. */
14088
14089 static bool
14090 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14091 {
14092 if (!cu->checked_producer)
14093 check_producer (cu);
14094
14095 return cu->producer_is_gxx_lt_4_6;
14096 }
14097
14098
14099 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14100 with incorrect is_stmt attributes. */
14101
14102 static bool
14103 producer_is_codewarrior (struct dwarf2_cu *cu)
14104 {
14105 if (!cu->checked_producer)
14106 check_producer (cu);
14107
14108 return cu->producer_is_codewarrior;
14109 }
14110
14111 /* Return the default accessibility type if it is not overridden by
14112 DW_AT_accessibility. */
14113
14114 static enum dwarf_access_attribute
14115 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14116 {
14117 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14118 {
14119 /* The default DWARF 2 accessibility for members is public, the default
14120 accessibility for inheritance is private. */
14121
14122 if (die->tag != DW_TAG_inheritance)
14123 return DW_ACCESS_public;
14124 else
14125 return DW_ACCESS_private;
14126 }
14127 else
14128 {
14129 /* DWARF 3+ defines the default accessibility a different way. The same
14130 rules apply now for DW_TAG_inheritance as for the members and it only
14131 depends on the container kind. */
14132
14133 if (die->parent->tag == DW_TAG_class_type)
14134 return DW_ACCESS_private;
14135 else
14136 return DW_ACCESS_public;
14137 }
14138 }
14139
14140 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14141 offset. If the attribute was not found return 0, otherwise return
14142 1. If it was found but could not properly be handled, set *OFFSET
14143 to 0. */
14144
14145 static int
14146 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14147 LONGEST *offset)
14148 {
14149 struct attribute *attr;
14150
14151 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14152 if (attr != NULL)
14153 {
14154 *offset = 0;
14155
14156 /* Note that we do not check for a section offset first here.
14157 This is because DW_AT_data_member_location is new in DWARF 4,
14158 so if we see it, we can assume that a constant form is really
14159 a constant and not a section offset. */
14160 if (attr->form_is_constant ())
14161 *offset = attr->constant_value (0);
14162 else if (attr->form_is_section_offset ())
14163 dwarf2_complex_location_expr_complaint ();
14164 else if (attr->form_is_block ())
14165 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14166 else
14167 dwarf2_complex_location_expr_complaint ();
14168
14169 return 1;
14170 }
14171
14172 return 0;
14173 }
14174
14175 /* Add an aggregate field to the field list. */
14176
14177 static void
14178 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14179 struct dwarf2_cu *cu)
14180 {
14181 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14182 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14183 struct nextfield *new_field;
14184 struct attribute *attr;
14185 struct field *fp;
14186 const char *fieldname = "";
14187
14188 if (die->tag == DW_TAG_inheritance)
14189 {
14190 fip->baseclasses.emplace_back ();
14191 new_field = &fip->baseclasses.back ();
14192 }
14193 else
14194 {
14195 fip->fields.emplace_back ();
14196 new_field = &fip->fields.back ();
14197 }
14198
14199 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14200 if (attr != nullptr)
14201 new_field->accessibility = DW_UNSND (attr);
14202 else
14203 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14204 if (new_field->accessibility != DW_ACCESS_public)
14205 fip->non_public_fields = 1;
14206
14207 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14208 if (attr != nullptr)
14209 new_field->virtuality = DW_UNSND (attr);
14210 else
14211 new_field->virtuality = DW_VIRTUALITY_none;
14212
14213 fp = &new_field->field;
14214
14215 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14216 {
14217 LONGEST offset;
14218
14219 /* Data member other than a C++ static data member. */
14220
14221 /* Get type of field. */
14222 fp->type = die_type (die, cu);
14223
14224 SET_FIELD_BITPOS (*fp, 0);
14225
14226 /* Get bit size of field (zero if none). */
14227 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14228 if (attr != nullptr)
14229 {
14230 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14231 }
14232 else
14233 {
14234 FIELD_BITSIZE (*fp) = 0;
14235 }
14236
14237 /* Get bit offset of field. */
14238 if (handle_data_member_location (die, cu, &offset))
14239 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14240 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14241 if (attr != nullptr)
14242 {
14243 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14244 {
14245 /* For big endian bits, the DW_AT_bit_offset gives the
14246 additional bit offset from the MSB of the containing
14247 anonymous object to the MSB of the field. We don't
14248 have to do anything special since we don't need to
14249 know the size of the anonymous object. */
14250 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14251 }
14252 else
14253 {
14254 /* For little endian bits, compute the bit offset to the
14255 MSB of the anonymous object, subtract off the number of
14256 bits from the MSB of the field to the MSB of the
14257 object, and then subtract off the number of bits of
14258 the field itself. The result is the bit offset of
14259 the LSB of the field. */
14260 int anonymous_size;
14261 int bit_offset = DW_UNSND (attr);
14262
14263 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14264 if (attr != nullptr)
14265 {
14266 /* The size of the anonymous object containing
14267 the bit field is explicit, so use the
14268 indicated size (in bytes). */
14269 anonymous_size = DW_UNSND (attr);
14270 }
14271 else
14272 {
14273 /* The size of the anonymous object containing
14274 the bit field must be inferred from the type
14275 attribute of the data member containing the
14276 bit field. */
14277 anonymous_size = TYPE_LENGTH (fp->type);
14278 }
14279 SET_FIELD_BITPOS (*fp,
14280 (FIELD_BITPOS (*fp)
14281 + anonymous_size * bits_per_byte
14282 - bit_offset - FIELD_BITSIZE (*fp)));
14283 }
14284 }
14285 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14286 if (attr != NULL)
14287 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14288 + attr->constant_value (0)));
14289
14290 /* Get name of field. */
14291 fieldname = dwarf2_name (die, cu);
14292 if (fieldname == NULL)
14293 fieldname = "";
14294
14295 /* The name is already allocated along with this objfile, so we don't
14296 need to duplicate it for the type. */
14297 fp->name = fieldname;
14298
14299 /* Change accessibility for artificial fields (e.g. virtual table
14300 pointer or virtual base class pointer) to private. */
14301 if (dwarf2_attr (die, DW_AT_artificial, cu))
14302 {
14303 FIELD_ARTIFICIAL (*fp) = 1;
14304 new_field->accessibility = DW_ACCESS_private;
14305 fip->non_public_fields = 1;
14306 }
14307 }
14308 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14309 {
14310 /* C++ static member. */
14311
14312 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14313 is a declaration, but all versions of G++ as of this writing
14314 (so through at least 3.2.1) incorrectly generate
14315 DW_TAG_variable tags. */
14316
14317 const char *physname;
14318
14319 /* Get name of field. */
14320 fieldname = dwarf2_name (die, cu);
14321 if (fieldname == NULL)
14322 return;
14323
14324 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14325 if (attr
14326 /* Only create a symbol if this is an external value.
14327 new_symbol checks this and puts the value in the global symbol
14328 table, which we want. If it is not external, new_symbol
14329 will try to put the value in cu->list_in_scope which is wrong. */
14330 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14331 {
14332 /* A static const member, not much different than an enum as far as
14333 we're concerned, except that we can support more types. */
14334 new_symbol (die, NULL, cu);
14335 }
14336
14337 /* Get physical name. */
14338 physname = dwarf2_physname (fieldname, die, cu);
14339
14340 /* The name is already allocated along with this objfile, so we don't
14341 need to duplicate it for the type. */
14342 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14343 FIELD_TYPE (*fp) = die_type (die, cu);
14344 FIELD_NAME (*fp) = fieldname;
14345 }
14346 else if (die->tag == DW_TAG_inheritance)
14347 {
14348 LONGEST offset;
14349
14350 /* C++ base class field. */
14351 if (handle_data_member_location (die, cu, &offset))
14352 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14353 FIELD_BITSIZE (*fp) = 0;
14354 FIELD_TYPE (*fp) = die_type (die, cu);
14355 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14356 }
14357 else if (die->tag == DW_TAG_variant_part)
14358 {
14359 /* process_structure_scope will treat this DIE as a union. */
14360 process_structure_scope (die, cu);
14361
14362 /* The variant part is relative to the start of the enclosing
14363 structure. */
14364 SET_FIELD_BITPOS (*fp, 0);
14365 fp->type = get_die_type (die, cu);
14366 fp->artificial = 1;
14367 fp->name = "<<variant>>";
14368
14369 /* Normally a DW_TAG_variant_part won't have a size, but our
14370 representation requires one, so set it to the maximum of the
14371 child sizes, being sure to account for the offset at which
14372 each child is seen. */
14373 if (TYPE_LENGTH (fp->type) == 0)
14374 {
14375 unsigned max = 0;
14376 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14377 {
14378 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14379 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14380 if (len > max)
14381 max = len;
14382 }
14383 TYPE_LENGTH (fp->type) = max;
14384 }
14385 }
14386 else
14387 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14388 }
14389
14390 /* Can the type given by DIE define another type? */
14391
14392 static bool
14393 type_can_define_types (const struct die_info *die)
14394 {
14395 switch (die->tag)
14396 {
14397 case DW_TAG_typedef:
14398 case DW_TAG_class_type:
14399 case DW_TAG_structure_type:
14400 case DW_TAG_union_type:
14401 case DW_TAG_enumeration_type:
14402 return true;
14403
14404 default:
14405 return false;
14406 }
14407 }
14408
14409 /* Add a type definition defined in the scope of the FIP's class. */
14410
14411 static void
14412 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14413 struct dwarf2_cu *cu)
14414 {
14415 struct decl_field fp;
14416 memset (&fp, 0, sizeof (fp));
14417
14418 gdb_assert (type_can_define_types (die));
14419
14420 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14421 fp.name = dwarf2_name (die, cu);
14422 fp.type = read_type_die (die, cu);
14423
14424 /* Save accessibility. */
14425 enum dwarf_access_attribute accessibility;
14426 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14427 if (attr != NULL)
14428 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14429 else
14430 accessibility = dwarf2_default_access_attribute (die, cu);
14431 switch (accessibility)
14432 {
14433 case DW_ACCESS_public:
14434 /* The assumed value if neither private nor protected. */
14435 break;
14436 case DW_ACCESS_private:
14437 fp.is_private = 1;
14438 break;
14439 case DW_ACCESS_protected:
14440 fp.is_protected = 1;
14441 break;
14442 default:
14443 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14444 }
14445
14446 if (die->tag == DW_TAG_typedef)
14447 fip->typedef_field_list.push_back (fp);
14448 else
14449 fip->nested_types_list.push_back (fp);
14450 }
14451
14452 /* Create the vector of fields, and attach it to the type. */
14453
14454 static void
14455 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14456 struct dwarf2_cu *cu)
14457 {
14458 int nfields = fip->nfields ();
14459
14460 /* Record the field count, allocate space for the array of fields,
14461 and create blank accessibility bitfields if necessary. */
14462 TYPE_NFIELDS (type) = nfields;
14463 TYPE_FIELDS (type) = (struct field *)
14464 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14465
14466 if (fip->non_public_fields && cu->language != language_ada)
14467 {
14468 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14469
14470 TYPE_FIELD_PRIVATE_BITS (type) =
14471 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14472 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14473
14474 TYPE_FIELD_PROTECTED_BITS (type) =
14475 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14476 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14477
14478 TYPE_FIELD_IGNORE_BITS (type) =
14479 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14480 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14481 }
14482
14483 /* If the type has baseclasses, allocate and clear a bit vector for
14484 TYPE_FIELD_VIRTUAL_BITS. */
14485 if (!fip->baseclasses.empty () && cu->language != language_ada)
14486 {
14487 int num_bytes = B_BYTES (fip->baseclasses.size ());
14488 unsigned char *pointer;
14489
14490 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14491 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14492 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14493 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14494 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14495 }
14496
14497 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14498 {
14499 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14500
14501 for (int index = 0; index < nfields; ++index)
14502 {
14503 struct nextfield &field = fip->fields[index];
14504
14505 if (field.variant.is_discriminant)
14506 di->discriminant_index = index;
14507 else if (field.variant.default_branch)
14508 di->default_index = index;
14509 else
14510 di->discriminants[index] = field.variant.discriminant_value;
14511 }
14512 }
14513
14514 /* Copy the saved-up fields into the field vector. */
14515 for (int i = 0; i < nfields; ++i)
14516 {
14517 struct nextfield &field
14518 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14519 : fip->fields[i - fip->baseclasses.size ()]);
14520
14521 TYPE_FIELD (type, i) = field.field;
14522 switch (field.accessibility)
14523 {
14524 case DW_ACCESS_private:
14525 if (cu->language != language_ada)
14526 SET_TYPE_FIELD_PRIVATE (type, i);
14527 break;
14528
14529 case DW_ACCESS_protected:
14530 if (cu->language != language_ada)
14531 SET_TYPE_FIELD_PROTECTED (type, i);
14532 break;
14533
14534 case DW_ACCESS_public:
14535 break;
14536
14537 default:
14538 /* Unknown accessibility. Complain and treat it as public. */
14539 {
14540 complaint (_("unsupported accessibility %d"),
14541 field.accessibility);
14542 }
14543 break;
14544 }
14545 if (i < fip->baseclasses.size ())
14546 {
14547 switch (field.virtuality)
14548 {
14549 case DW_VIRTUALITY_virtual:
14550 case DW_VIRTUALITY_pure_virtual:
14551 if (cu->language == language_ada)
14552 error (_("unexpected virtuality in component of Ada type"));
14553 SET_TYPE_FIELD_VIRTUAL (type, i);
14554 break;
14555 }
14556 }
14557 }
14558 }
14559
14560 /* Return true if this member function is a constructor, false
14561 otherwise. */
14562
14563 static int
14564 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14565 {
14566 const char *fieldname;
14567 const char *type_name;
14568 int len;
14569
14570 if (die->parent == NULL)
14571 return 0;
14572
14573 if (die->parent->tag != DW_TAG_structure_type
14574 && die->parent->tag != DW_TAG_union_type
14575 && die->parent->tag != DW_TAG_class_type)
14576 return 0;
14577
14578 fieldname = dwarf2_name (die, cu);
14579 type_name = dwarf2_name (die->parent, cu);
14580 if (fieldname == NULL || type_name == NULL)
14581 return 0;
14582
14583 len = strlen (fieldname);
14584 return (strncmp (fieldname, type_name, len) == 0
14585 && (type_name[len] == '\0' || type_name[len] == '<'));
14586 }
14587
14588 /* Check if the given VALUE is a recognized enum
14589 dwarf_defaulted_attribute constant according to DWARF5 spec,
14590 Table 7.24. */
14591
14592 static bool
14593 is_valid_DW_AT_defaulted (ULONGEST value)
14594 {
14595 switch (value)
14596 {
14597 case DW_DEFAULTED_no:
14598 case DW_DEFAULTED_in_class:
14599 case DW_DEFAULTED_out_of_class:
14600 return true;
14601 }
14602
14603 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14604 return false;
14605 }
14606
14607 /* Add a member function to the proper fieldlist. */
14608
14609 static void
14610 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14611 struct type *type, struct dwarf2_cu *cu)
14612 {
14613 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14614 struct attribute *attr;
14615 int i;
14616 struct fnfieldlist *flp = nullptr;
14617 struct fn_field *fnp;
14618 const char *fieldname;
14619 struct type *this_type;
14620 enum dwarf_access_attribute accessibility;
14621
14622 if (cu->language == language_ada)
14623 error (_("unexpected member function in Ada type"));
14624
14625 /* Get name of member function. */
14626 fieldname = dwarf2_name (die, cu);
14627 if (fieldname == NULL)
14628 return;
14629
14630 /* Look up member function name in fieldlist. */
14631 for (i = 0; i < fip->fnfieldlists.size (); i++)
14632 {
14633 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14634 {
14635 flp = &fip->fnfieldlists[i];
14636 break;
14637 }
14638 }
14639
14640 /* Create a new fnfieldlist if necessary. */
14641 if (flp == nullptr)
14642 {
14643 fip->fnfieldlists.emplace_back ();
14644 flp = &fip->fnfieldlists.back ();
14645 flp->name = fieldname;
14646 i = fip->fnfieldlists.size () - 1;
14647 }
14648
14649 /* Create a new member function field and add it to the vector of
14650 fnfieldlists. */
14651 flp->fnfields.emplace_back ();
14652 fnp = &flp->fnfields.back ();
14653
14654 /* Delay processing of the physname until later. */
14655 if (cu->language == language_cplus)
14656 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14657 die, cu);
14658 else
14659 {
14660 const char *physname = dwarf2_physname (fieldname, die, cu);
14661 fnp->physname = physname ? physname : "";
14662 }
14663
14664 fnp->type = alloc_type (objfile);
14665 this_type = read_type_die (die, cu);
14666 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14667 {
14668 int nparams = TYPE_NFIELDS (this_type);
14669
14670 /* TYPE is the domain of this method, and THIS_TYPE is the type
14671 of the method itself (TYPE_CODE_METHOD). */
14672 smash_to_method_type (fnp->type, type,
14673 TYPE_TARGET_TYPE (this_type),
14674 TYPE_FIELDS (this_type),
14675 TYPE_NFIELDS (this_type),
14676 TYPE_VARARGS (this_type));
14677
14678 /* Handle static member functions.
14679 Dwarf2 has no clean way to discern C++ static and non-static
14680 member functions. G++ helps GDB by marking the first
14681 parameter for non-static member functions (which is the this
14682 pointer) as artificial. We obtain this information from
14683 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14684 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14685 fnp->voffset = VOFFSET_STATIC;
14686 }
14687 else
14688 complaint (_("member function type missing for '%s'"),
14689 dwarf2_full_name (fieldname, die, cu));
14690
14691 /* Get fcontext from DW_AT_containing_type if present. */
14692 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14693 fnp->fcontext = die_containing_type (die, cu);
14694
14695 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14696 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14697
14698 /* Get accessibility. */
14699 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14700 if (attr != nullptr)
14701 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14702 else
14703 accessibility = dwarf2_default_access_attribute (die, cu);
14704 switch (accessibility)
14705 {
14706 case DW_ACCESS_private:
14707 fnp->is_private = 1;
14708 break;
14709 case DW_ACCESS_protected:
14710 fnp->is_protected = 1;
14711 break;
14712 }
14713
14714 /* Check for artificial methods. */
14715 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14716 if (attr && DW_UNSND (attr) != 0)
14717 fnp->is_artificial = 1;
14718
14719 /* Check for defaulted methods. */
14720 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14721 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14722 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14723
14724 /* Check for deleted methods. */
14725 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14726 if (attr != nullptr && DW_UNSND (attr) != 0)
14727 fnp->is_deleted = 1;
14728
14729 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14730
14731 /* Get index in virtual function table if it is a virtual member
14732 function. For older versions of GCC, this is an offset in the
14733 appropriate virtual table, as specified by DW_AT_containing_type.
14734 For everyone else, it is an expression to be evaluated relative
14735 to the object address. */
14736
14737 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14738 if (attr != nullptr)
14739 {
14740 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14741 {
14742 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14743 {
14744 /* Old-style GCC. */
14745 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14746 }
14747 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14748 || (DW_BLOCK (attr)->size > 1
14749 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14750 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14751 {
14752 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14753 if ((fnp->voffset % cu->header.addr_size) != 0)
14754 dwarf2_complex_location_expr_complaint ();
14755 else
14756 fnp->voffset /= cu->header.addr_size;
14757 fnp->voffset += 2;
14758 }
14759 else
14760 dwarf2_complex_location_expr_complaint ();
14761
14762 if (!fnp->fcontext)
14763 {
14764 /* If there is no `this' field and no DW_AT_containing_type,
14765 we cannot actually find a base class context for the
14766 vtable! */
14767 if (TYPE_NFIELDS (this_type) == 0
14768 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14769 {
14770 complaint (_("cannot determine context for virtual member "
14771 "function \"%s\" (offset %s)"),
14772 fieldname, sect_offset_str (die->sect_off));
14773 }
14774 else
14775 {
14776 fnp->fcontext
14777 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14778 }
14779 }
14780 }
14781 else if (attr->form_is_section_offset ())
14782 {
14783 dwarf2_complex_location_expr_complaint ();
14784 }
14785 else
14786 {
14787 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14788 fieldname);
14789 }
14790 }
14791 else
14792 {
14793 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14794 if (attr && DW_UNSND (attr))
14795 {
14796 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14797 complaint (_("Member function \"%s\" (offset %s) is virtual "
14798 "but the vtable offset is not specified"),
14799 fieldname, sect_offset_str (die->sect_off));
14800 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14801 TYPE_CPLUS_DYNAMIC (type) = 1;
14802 }
14803 }
14804 }
14805
14806 /* Create the vector of member function fields, and attach it to the type. */
14807
14808 static void
14809 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14810 struct dwarf2_cu *cu)
14811 {
14812 if (cu->language == language_ada)
14813 error (_("unexpected member functions in Ada type"));
14814
14815 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14816 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14817 TYPE_ALLOC (type,
14818 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14819
14820 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14821 {
14822 struct fnfieldlist &nf = fip->fnfieldlists[i];
14823 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14824
14825 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14826 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14827 fn_flp->fn_fields = (struct fn_field *)
14828 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14829
14830 for (int k = 0; k < nf.fnfields.size (); ++k)
14831 fn_flp->fn_fields[k] = nf.fnfields[k];
14832 }
14833
14834 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14835 }
14836
14837 /* Returns non-zero if NAME is the name of a vtable member in CU's
14838 language, zero otherwise. */
14839 static int
14840 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14841 {
14842 static const char vptr[] = "_vptr";
14843
14844 /* Look for the C++ form of the vtable. */
14845 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14846 return 1;
14847
14848 return 0;
14849 }
14850
14851 /* GCC outputs unnamed structures that are really pointers to member
14852 functions, with the ABI-specified layout. If TYPE describes
14853 such a structure, smash it into a member function type.
14854
14855 GCC shouldn't do this; it should just output pointer to member DIEs.
14856 This is GCC PR debug/28767. */
14857
14858 static void
14859 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14860 {
14861 struct type *pfn_type, *self_type, *new_type;
14862
14863 /* Check for a structure with no name and two children. */
14864 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14865 return;
14866
14867 /* Check for __pfn and __delta members. */
14868 if (TYPE_FIELD_NAME (type, 0) == NULL
14869 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14870 || TYPE_FIELD_NAME (type, 1) == NULL
14871 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14872 return;
14873
14874 /* Find the type of the method. */
14875 pfn_type = TYPE_FIELD_TYPE (type, 0);
14876 if (pfn_type == NULL
14877 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14878 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14879 return;
14880
14881 /* Look for the "this" argument. */
14882 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14883 if (TYPE_NFIELDS (pfn_type) == 0
14884 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14885 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14886 return;
14887
14888 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14889 new_type = alloc_type (objfile);
14890 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14891 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14892 TYPE_VARARGS (pfn_type));
14893 smash_to_methodptr_type (type, new_type);
14894 }
14895
14896 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14897 appropriate error checking and issuing complaints if there is a
14898 problem. */
14899
14900 static ULONGEST
14901 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14902 {
14903 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14904
14905 if (attr == nullptr)
14906 return 0;
14907
14908 if (!attr->form_is_constant ())
14909 {
14910 complaint (_("DW_AT_alignment must have constant form"
14911 " - DIE at %s [in module %s]"),
14912 sect_offset_str (die->sect_off),
14913 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14914 return 0;
14915 }
14916
14917 ULONGEST align;
14918 if (attr->form == DW_FORM_sdata)
14919 {
14920 LONGEST val = DW_SND (attr);
14921 if (val < 0)
14922 {
14923 complaint (_("DW_AT_alignment value must not be negative"
14924 " - DIE at %s [in module %s]"),
14925 sect_offset_str (die->sect_off),
14926 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14927 return 0;
14928 }
14929 align = val;
14930 }
14931 else
14932 align = DW_UNSND (attr);
14933
14934 if (align == 0)
14935 {
14936 complaint (_("DW_AT_alignment value must not be zero"
14937 " - DIE at %s [in module %s]"),
14938 sect_offset_str (die->sect_off),
14939 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14940 return 0;
14941 }
14942 if ((align & (align - 1)) != 0)
14943 {
14944 complaint (_("DW_AT_alignment value must be a power of 2"
14945 " - DIE at %s [in module %s]"),
14946 sect_offset_str (die->sect_off),
14947 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14948 return 0;
14949 }
14950
14951 return align;
14952 }
14953
14954 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14955 the alignment for TYPE. */
14956
14957 static void
14958 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14959 struct type *type)
14960 {
14961 if (!set_type_align (type, get_alignment (cu, die)))
14962 complaint (_("DW_AT_alignment value too large"
14963 " - DIE at %s [in module %s]"),
14964 sect_offset_str (die->sect_off),
14965 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14966 }
14967
14968 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14969 constant for a type, according to DWARF5 spec, Table 5.5. */
14970
14971 static bool
14972 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14973 {
14974 switch (value)
14975 {
14976 case DW_CC_normal:
14977 case DW_CC_pass_by_reference:
14978 case DW_CC_pass_by_value:
14979 return true;
14980
14981 default:
14982 complaint (_("unrecognized DW_AT_calling_convention value "
14983 "(%s) for a type"), pulongest (value));
14984 return false;
14985 }
14986 }
14987
14988 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14989 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14990 also according to GNU-specific values (see include/dwarf2.h). */
14991
14992 static bool
14993 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14994 {
14995 switch (value)
14996 {
14997 case DW_CC_normal:
14998 case DW_CC_program:
14999 case DW_CC_nocall:
15000 return true;
15001
15002 case DW_CC_GNU_renesas_sh:
15003 case DW_CC_GNU_borland_fastcall_i386:
15004 case DW_CC_GDB_IBM_OpenCL:
15005 return true;
15006
15007 default:
15008 complaint (_("unrecognized DW_AT_calling_convention value "
15009 "(%s) for a subroutine"), pulongest (value));
15010 return false;
15011 }
15012 }
15013
15014 /* Called when we find the DIE that starts a structure or union scope
15015 (definition) to create a type for the structure or union. Fill in
15016 the type's name and general properties; the members will not be
15017 processed until process_structure_scope. A symbol table entry for
15018 the type will also not be done until process_structure_scope (assuming
15019 the type has a name).
15020
15021 NOTE: we need to call these functions regardless of whether or not the
15022 DIE has a DW_AT_name attribute, since it might be an anonymous
15023 structure or union. This gets the type entered into our set of
15024 user defined types. */
15025
15026 static struct type *
15027 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15028 {
15029 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15030 struct type *type;
15031 struct attribute *attr;
15032 const char *name;
15033
15034 /* If the definition of this type lives in .debug_types, read that type.
15035 Don't follow DW_AT_specification though, that will take us back up
15036 the chain and we want to go down. */
15037 attr = die->attr (DW_AT_signature);
15038 if (attr != nullptr)
15039 {
15040 type = get_DW_AT_signature_type (die, attr, cu);
15041
15042 /* The type's CU may not be the same as CU.
15043 Ensure TYPE is recorded with CU in die_type_hash. */
15044 return set_die_type (die, type, cu);
15045 }
15046
15047 type = alloc_type (objfile);
15048 INIT_CPLUS_SPECIFIC (type);
15049
15050 name = dwarf2_name (die, cu);
15051 if (name != NULL)
15052 {
15053 if (cu->language == language_cplus
15054 || cu->language == language_d
15055 || cu->language == language_rust)
15056 {
15057 const char *full_name = dwarf2_full_name (name, die, cu);
15058
15059 /* dwarf2_full_name might have already finished building the DIE's
15060 type. If so, there is no need to continue. */
15061 if (get_die_type (die, cu) != NULL)
15062 return get_die_type (die, cu);
15063
15064 TYPE_NAME (type) = full_name;
15065 }
15066 else
15067 {
15068 /* The name is already allocated along with this objfile, so
15069 we don't need to duplicate it for the type. */
15070 TYPE_NAME (type) = name;
15071 }
15072 }
15073
15074 if (die->tag == DW_TAG_structure_type)
15075 {
15076 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15077 }
15078 else if (die->tag == DW_TAG_union_type)
15079 {
15080 TYPE_CODE (type) = TYPE_CODE_UNION;
15081 }
15082 else if (die->tag == DW_TAG_variant_part)
15083 {
15084 TYPE_CODE (type) = TYPE_CODE_UNION;
15085 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15086 }
15087 else
15088 {
15089 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15090 }
15091
15092 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15093 TYPE_DECLARED_CLASS (type) = 1;
15094
15095 /* Store the calling convention in the type if it's available in
15096 the die. Otherwise the calling convention remains set to
15097 the default value DW_CC_normal. */
15098 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15099 if (attr != nullptr
15100 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15101 {
15102 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15103 TYPE_CPLUS_CALLING_CONVENTION (type)
15104 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15105 }
15106
15107 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15108 if (attr != nullptr)
15109 {
15110 if (attr->form_is_constant ())
15111 TYPE_LENGTH (type) = DW_UNSND (attr);
15112 else
15113 {
15114 /* For the moment, dynamic type sizes are not supported
15115 by GDB's struct type. The actual size is determined
15116 on-demand when resolving the type of a given object,
15117 so set the type's length to zero for now. Otherwise,
15118 we record an expression as the length, and that expression
15119 could lead to a very large value, which could eventually
15120 lead to us trying to allocate that much memory when creating
15121 a value of that type. */
15122 TYPE_LENGTH (type) = 0;
15123 }
15124 }
15125 else
15126 {
15127 TYPE_LENGTH (type) = 0;
15128 }
15129
15130 maybe_set_alignment (cu, die, type);
15131
15132 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15133 {
15134 /* ICC<14 does not output the required DW_AT_declaration on
15135 incomplete types, but gives them a size of zero. */
15136 TYPE_STUB (type) = 1;
15137 }
15138 else
15139 TYPE_STUB_SUPPORTED (type) = 1;
15140
15141 if (die_is_declaration (die, cu))
15142 TYPE_STUB (type) = 1;
15143 else if (attr == NULL && die->child == NULL
15144 && producer_is_realview (cu->producer))
15145 /* RealView does not output the required DW_AT_declaration
15146 on incomplete types. */
15147 TYPE_STUB (type) = 1;
15148
15149 /* We need to add the type field to the die immediately so we don't
15150 infinitely recurse when dealing with pointers to the structure
15151 type within the structure itself. */
15152 set_die_type (die, type, cu);
15153
15154 /* set_die_type should be already done. */
15155 set_descriptive_type (type, die, cu);
15156
15157 return type;
15158 }
15159
15160 /* A helper for process_structure_scope that handles a single member
15161 DIE. */
15162
15163 static void
15164 handle_struct_member_die (struct die_info *child_die, struct type *type,
15165 struct field_info *fi,
15166 std::vector<struct symbol *> *template_args,
15167 struct dwarf2_cu *cu)
15168 {
15169 if (child_die->tag == DW_TAG_member
15170 || child_die->tag == DW_TAG_variable
15171 || child_die->tag == DW_TAG_variant_part)
15172 {
15173 /* NOTE: carlton/2002-11-05: A C++ static data member
15174 should be a DW_TAG_member that is a declaration, but
15175 all versions of G++ as of this writing (so through at
15176 least 3.2.1) incorrectly generate DW_TAG_variable
15177 tags for them instead. */
15178 dwarf2_add_field (fi, child_die, cu);
15179 }
15180 else if (child_die->tag == DW_TAG_subprogram)
15181 {
15182 /* Rust doesn't have member functions in the C++ sense.
15183 However, it does emit ordinary functions as children
15184 of a struct DIE. */
15185 if (cu->language == language_rust)
15186 read_func_scope (child_die, cu);
15187 else
15188 {
15189 /* C++ member function. */
15190 dwarf2_add_member_fn (fi, child_die, type, cu);
15191 }
15192 }
15193 else if (child_die->tag == DW_TAG_inheritance)
15194 {
15195 /* C++ base class field. */
15196 dwarf2_add_field (fi, child_die, cu);
15197 }
15198 else if (type_can_define_types (child_die))
15199 dwarf2_add_type_defn (fi, child_die, cu);
15200 else if (child_die->tag == DW_TAG_template_type_param
15201 || child_die->tag == DW_TAG_template_value_param)
15202 {
15203 struct symbol *arg = new_symbol (child_die, NULL, cu);
15204
15205 if (arg != NULL)
15206 template_args->push_back (arg);
15207 }
15208 else if (child_die->tag == DW_TAG_variant)
15209 {
15210 /* In a variant we want to get the discriminant and also add a
15211 field for our sole member child. */
15212 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15213
15214 for (die_info *variant_child = child_die->child;
15215 variant_child != NULL;
15216 variant_child = variant_child->sibling)
15217 {
15218 if (variant_child->tag == DW_TAG_member)
15219 {
15220 handle_struct_member_die (variant_child, type, fi,
15221 template_args, cu);
15222 /* Only handle the one. */
15223 break;
15224 }
15225 }
15226
15227 /* We don't handle this but we might as well report it if we see
15228 it. */
15229 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15230 complaint (_("DW_AT_discr_list is not supported yet"
15231 " - DIE at %s [in module %s]"),
15232 sect_offset_str (child_die->sect_off),
15233 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15234
15235 /* The first field was just added, so we can stash the
15236 discriminant there. */
15237 gdb_assert (!fi->fields.empty ());
15238 if (discr == NULL)
15239 fi->fields.back ().variant.default_branch = true;
15240 else
15241 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15242 }
15243 }
15244
15245 /* Finish creating a structure or union type, including filling in
15246 its members and creating a symbol for it. */
15247
15248 static void
15249 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15250 {
15251 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15252 struct die_info *child_die;
15253 struct type *type;
15254
15255 type = get_die_type (die, cu);
15256 if (type == NULL)
15257 type = read_structure_type (die, cu);
15258
15259 /* When reading a DW_TAG_variant_part, we need to notice when we
15260 read the discriminant member, so we can record it later in the
15261 discriminant_info. */
15262 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15263 sect_offset discr_offset {};
15264 bool has_template_parameters = false;
15265
15266 if (is_variant_part)
15267 {
15268 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15269 if (discr == NULL)
15270 {
15271 /* Maybe it's a univariant form, an extension we support.
15272 In this case arrange not to check the offset. */
15273 is_variant_part = false;
15274 }
15275 else if (discr->form_is_ref ())
15276 {
15277 struct dwarf2_cu *target_cu = cu;
15278 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15279
15280 discr_offset = target_die->sect_off;
15281 }
15282 else
15283 {
15284 complaint (_("DW_AT_discr does not have DIE reference form"
15285 " - DIE at %s [in module %s]"),
15286 sect_offset_str (die->sect_off),
15287 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15288 is_variant_part = false;
15289 }
15290 }
15291
15292 if (die->child != NULL && ! die_is_declaration (die, cu))
15293 {
15294 struct field_info fi;
15295 std::vector<struct symbol *> template_args;
15296
15297 child_die = die->child;
15298
15299 while (child_die && child_die->tag)
15300 {
15301 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15302
15303 if (is_variant_part && discr_offset == child_die->sect_off)
15304 fi.fields.back ().variant.is_discriminant = true;
15305
15306 child_die = child_die->sibling;
15307 }
15308
15309 /* Attach template arguments to type. */
15310 if (!template_args.empty ())
15311 {
15312 has_template_parameters = true;
15313 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15314 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15315 TYPE_TEMPLATE_ARGUMENTS (type)
15316 = XOBNEWVEC (&objfile->objfile_obstack,
15317 struct symbol *,
15318 TYPE_N_TEMPLATE_ARGUMENTS (type));
15319 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15320 template_args.data (),
15321 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15322 * sizeof (struct symbol *)));
15323 }
15324
15325 /* Attach fields and member functions to the type. */
15326 if (fi.nfields () > 0)
15327 dwarf2_attach_fields_to_type (&fi, type, cu);
15328 if (!fi.fnfieldlists.empty ())
15329 {
15330 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15331
15332 /* Get the type which refers to the base class (possibly this
15333 class itself) which contains the vtable pointer for the current
15334 class from the DW_AT_containing_type attribute. This use of
15335 DW_AT_containing_type is a GNU extension. */
15336
15337 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15338 {
15339 struct type *t = die_containing_type (die, cu);
15340
15341 set_type_vptr_basetype (type, t);
15342 if (type == t)
15343 {
15344 int i;
15345
15346 /* Our own class provides vtbl ptr. */
15347 for (i = TYPE_NFIELDS (t) - 1;
15348 i >= TYPE_N_BASECLASSES (t);
15349 --i)
15350 {
15351 const char *fieldname = TYPE_FIELD_NAME (t, i);
15352
15353 if (is_vtable_name (fieldname, cu))
15354 {
15355 set_type_vptr_fieldno (type, i);
15356 break;
15357 }
15358 }
15359
15360 /* Complain if virtual function table field not found. */
15361 if (i < TYPE_N_BASECLASSES (t))
15362 complaint (_("virtual function table pointer "
15363 "not found when defining class '%s'"),
15364 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15365 }
15366 else
15367 {
15368 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15369 }
15370 }
15371 else if (cu->producer
15372 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15373 {
15374 /* The IBM XLC compiler does not provide direct indication
15375 of the containing type, but the vtable pointer is
15376 always named __vfp. */
15377
15378 int i;
15379
15380 for (i = TYPE_NFIELDS (type) - 1;
15381 i >= TYPE_N_BASECLASSES (type);
15382 --i)
15383 {
15384 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15385 {
15386 set_type_vptr_fieldno (type, i);
15387 set_type_vptr_basetype (type, type);
15388 break;
15389 }
15390 }
15391 }
15392 }
15393
15394 /* Copy fi.typedef_field_list linked list elements content into the
15395 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15396 if (!fi.typedef_field_list.empty ())
15397 {
15398 int count = fi.typedef_field_list.size ();
15399
15400 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15401 TYPE_TYPEDEF_FIELD_ARRAY (type)
15402 = ((struct decl_field *)
15403 TYPE_ALLOC (type,
15404 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15405 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15406
15407 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15408 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15409 }
15410
15411 /* Copy fi.nested_types_list linked list elements content into the
15412 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15413 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15414 {
15415 int count = fi.nested_types_list.size ();
15416
15417 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15418 TYPE_NESTED_TYPES_ARRAY (type)
15419 = ((struct decl_field *)
15420 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15421 TYPE_NESTED_TYPES_COUNT (type) = count;
15422
15423 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15424 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15425 }
15426 }
15427
15428 quirk_gcc_member_function_pointer (type, objfile);
15429 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15430 cu->rust_unions.push_back (type);
15431
15432 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15433 snapshots) has been known to create a die giving a declaration
15434 for a class that has, as a child, a die giving a definition for a
15435 nested class. So we have to process our children even if the
15436 current die is a declaration. Normally, of course, a declaration
15437 won't have any children at all. */
15438
15439 child_die = die->child;
15440
15441 while (child_die != NULL && child_die->tag)
15442 {
15443 if (child_die->tag == DW_TAG_member
15444 || child_die->tag == DW_TAG_variable
15445 || child_die->tag == DW_TAG_inheritance
15446 || child_die->tag == DW_TAG_template_value_param
15447 || child_die->tag == DW_TAG_template_type_param)
15448 {
15449 /* Do nothing. */
15450 }
15451 else
15452 process_die (child_die, cu);
15453
15454 child_die = child_die->sibling;
15455 }
15456
15457 /* Do not consider external references. According to the DWARF standard,
15458 these DIEs are identified by the fact that they have no byte_size
15459 attribute, and a declaration attribute. */
15460 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15461 || !die_is_declaration (die, cu))
15462 {
15463 struct symbol *sym = new_symbol (die, type, cu);
15464
15465 if (has_template_parameters)
15466 {
15467 struct symtab *symtab;
15468 if (sym != nullptr)
15469 symtab = symbol_symtab (sym);
15470 else if (cu->line_header != nullptr)
15471 {
15472 /* Any related symtab will do. */
15473 symtab
15474 = cu->line_header->file_names ()[0].symtab;
15475 }
15476 else
15477 {
15478 symtab = nullptr;
15479 complaint (_("could not find suitable "
15480 "symtab for template parameter"
15481 " - DIE at %s [in module %s]"),
15482 sect_offset_str (die->sect_off),
15483 objfile_name (objfile));
15484 }
15485
15486 if (symtab != nullptr)
15487 {
15488 /* Make sure that the symtab is set on the new symbols.
15489 Even though they don't appear in this symtab directly,
15490 other parts of gdb assume that symbols do, and this is
15491 reasonably true. */
15492 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15493 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15494 }
15495 }
15496 }
15497 }
15498
15499 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15500 update TYPE using some information only available in DIE's children. */
15501
15502 static void
15503 update_enumeration_type_from_children (struct die_info *die,
15504 struct type *type,
15505 struct dwarf2_cu *cu)
15506 {
15507 struct die_info *child_die;
15508 int unsigned_enum = 1;
15509 int flag_enum = 1;
15510
15511 auto_obstack obstack;
15512
15513 for (child_die = die->child;
15514 child_die != NULL && child_die->tag;
15515 child_die = child_die->sibling)
15516 {
15517 struct attribute *attr;
15518 LONGEST value;
15519 const gdb_byte *bytes;
15520 struct dwarf2_locexpr_baton *baton;
15521 const char *name;
15522
15523 if (child_die->tag != DW_TAG_enumerator)
15524 continue;
15525
15526 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15527 if (attr == NULL)
15528 continue;
15529
15530 name = dwarf2_name (child_die, cu);
15531 if (name == NULL)
15532 name = "<anonymous enumerator>";
15533
15534 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15535 &value, &bytes, &baton);
15536 if (value < 0)
15537 {
15538 unsigned_enum = 0;
15539 flag_enum = 0;
15540 }
15541 else
15542 {
15543 if (count_one_bits_ll (value) >= 2)
15544 flag_enum = 0;
15545 }
15546
15547 /* If we already know that the enum type is neither unsigned, nor
15548 a flag type, no need to look at the rest of the enumerates. */
15549 if (!unsigned_enum && !flag_enum)
15550 break;
15551 }
15552
15553 if (unsigned_enum)
15554 TYPE_UNSIGNED (type) = 1;
15555 if (flag_enum)
15556 TYPE_FLAG_ENUM (type) = 1;
15557 }
15558
15559 /* Given a DW_AT_enumeration_type die, set its type. We do not
15560 complete the type's fields yet, or create any symbols. */
15561
15562 static struct type *
15563 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15564 {
15565 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15566 struct type *type;
15567 struct attribute *attr;
15568 const char *name;
15569
15570 /* If the definition of this type lives in .debug_types, read that type.
15571 Don't follow DW_AT_specification though, that will take us back up
15572 the chain and we want to go down. */
15573 attr = die->attr (DW_AT_signature);
15574 if (attr != nullptr)
15575 {
15576 type = get_DW_AT_signature_type (die, attr, cu);
15577
15578 /* The type's CU may not be the same as CU.
15579 Ensure TYPE is recorded with CU in die_type_hash. */
15580 return set_die_type (die, type, cu);
15581 }
15582
15583 type = alloc_type (objfile);
15584
15585 TYPE_CODE (type) = TYPE_CODE_ENUM;
15586 name = dwarf2_full_name (NULL, die, cu);
15587 if (name != NULL)
15588 TYPE_NAME (type) = name;
15589
15590 attr = dwarf2_attr (die, DW_AT_type, cu);
15591 if (attr != NULL)
15592 {
15593 struct type *underlying_type = die_type (die, cu);
15594
15595 TYPE_TARGET_TYPE (type) = underlying_type;
15596 }
15597
15598 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15599 if (attr != nullptr)
15600 {
15601 TYPE_LENGTH (type) = DW_UNSND (attr);
15602 }
15603 else
15604 {
15605 TYPE_LENGTH (type) = 0;
15606 }
15607
15608 maybe_set_alignment (cu, die, type);
15609
15610 /* The enumeration DIE can be incomplete. In Ada, any type can be
15611 declared as private in the package spec, and then defined only
15612 inside the package body. Such types are known as Taft Amendment
15613 Types. When another package uses such a type, an incomplete DIE
15614 may be generated by the compiler. */
15615 if (die_is_declaration (die, cu))
15616 TYPE_STUB (type) = 1;
15617
15618 /* Finish the creation of this type by using the enum's children.
15619 We must call this even when the underlying type has been provided
15620 so that we can determine if we're looking at a "flag" enum. */
15621 update_enumeration_type_from_children (die, type, cu);
15622
15623 /* If this type has an underlying type that is not a stub, then we
15624 may use its attributes. We always use the "unsigned" attribute
15625 in this situation, because ordinarily we guess whether the type
15626 is unsigned -- but the guess can be wrong and the underlying type
15627 can tell us the reality. However, we defer to a local size
15628 attribute if one exists, because this lets the compiler override
15629 the underlying type if needed. */
15630 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15631 {
15632 struct type *underlying_type = TYPE_TARGET_TYPE (type);
15633 underlying_type = check_typedef (underlying_type);
15634 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
15635 if (TYPE_LENGTH (type) == 0)
15636 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
15637 if (TYPE_RAW_ALIGN (type) == 0
15638 && TYPE_RAW_ALIGN (underlying_type) != 0)
15639 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
15640 }
15641
15642 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15643
15644 return set_die_type (die, type, cu);
15645 }
15646
15647 /* Given a pointer to a die which begins an enumeration, process all
15648 the dies that define the members of the enumeration, and create the
15649 symbol for the enumeration type.
15650
15651 NOTE: We reverse the order of the element list. */
15652
15653 static void
15654 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15655 {
15656 struct type *this_type;
15657
15658 this_type = get_die_type (die, cu);
15659 if (this_type == NULL)
15660 this_type = read_enumeration_type (die, cu);
15661
15662 if (die->child != NULL)
15663 {
15664 struct die_info *child_die;
15665 struct symbol *sym;
15666 std::vector<struct field> fields;
15667 const char *name;
15668
15669 child_die = die->child;
15670 while (child_die && child_die->tag)
15671 {
15672 if (child_die->tag != DW_TAG_enumerator)
15673 {
15674 process_die (child_die, cu);
15675 }
15676 else
15677 {
15678 name = dwarf2_name (child_die, cu);
15679 if (name)
15680 {
15681 sym = new_symbol (child_die, this_type, cu);
15682
15683 fields.emplace_back ();
15684 struct field &field = fields.back ();
15685
15686 FIELD_NAME (field) = sym->linkage_name ();
15687 FIELD_TYPE (field) = NULL;
15688 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15689 FIELD_BITSIZE (field) = 0;
15690 }
15691 }
15692
15693 child_die = child_die->sibling;
15694 }
15695
15696 if (!fields.empty ())
15697 {
15698 TYPE_NFIELDS (this_type) = fields.size ();
15699 TYPE_FIELDS (this_type) = (struct field *)
15700 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15701 memcpy (TYPE_FIELDS (this_type), fields.data (),
15702 sizeof (struct field) * fields.size ());
15703 }
15704 }
15705
15706 /* If we are reading an enum from a .debug_types unit, and the enum
15707 is a declaration, and the enum is not the signatured type in the
15708 unit, then we do not want to add a symbol for it. Adding a
15709 symbol would in some cases obscure the true definition of the
15710 enum, giving users an incomplete type when the definition is
15711 actually available. Note that we do not want to do this for all
15712 enums which are just declarations, because C++0x allows forward
15713 enum declarations. */
15714 if (cu->per_cu->is_debug_types
15715 && die_is_declaration (die, cu))
15716 {
15717 struct signatured_type *sig_type;
15718
15719 sig_type = (struct signatured_type *) cu->per_cu;
15720 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15721 if (sig_type->type_offset_in_section != die->sect_off)
15722 return;
15723 }
15724
15725 new_symbol (die, this_type, cu);
15726 }
15727
15728 /* Extract all information from a DW_TAG_array_type DIE and put it in
15729 the DIE's type field. For now, this only handles one dimensional
15730 arrays. */
15731
15732 static struct type *
15733 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15734 {
15735 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15736 struct die_info *child_die;
15737 struct type *type;
15738 struct type *element_type, *range_type, *index_type;
15739 struct attribute *attr;
15740 const char *name;
15741 struct dynamic_prop *byte_stride_prop = NULL;
15742 unsigned int bit_stride = 0;
15743
15744 element_type = die_type (die, cu);
15745
15746 /* The die_type call above may have already set the type for this DIE. */
15747 type = get_die_type (die, cu);
15748 if (type)
15749 return type;
15750
15751 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15752 if (attr != NULL)
15753 {
15754 int stride_ok;
15755 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
15756
15757 byte_stride_prop
15758 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15759 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15760 prop_type);
15761 if (!stride_ok)
15762 {
15763 complaint (_("unable to read array DW_AT_byte_stride "
15764 " - DIE at %s [in module %s]"),
15765 sect_offset_str (die->sect_off),
15766 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15767 /* Ignore this attribute. We will likely not be able to print
15768 arrays of this type correctly, but there is little we can do
15769 to help if we cannot read the attribute's value. */
15770 byte_stride_prop = NULL;
15771 }
15772 }
15773
15774 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15775 if (attr != NULL)
15776 bit_stride = DW_UNSND (attr);
15777
15778 /* Irix 6.2 native cc creates array types without children for
15779 arrays with unspecified length. */
15780 if (die->child == NULL)
15781 {
15782 index_type = objfile_type (objfile)->builtin_int;
15783 range_type = create_static_range_type (NULL, index_type, 0, -1);
15784 type = create_array_type_with_stride (NULL, element_type, range_type,
15785 byte_stride_prop, bit_stride);
15786 return set_die_type (die, type, cu);
15787 }
15788
15789 std::vector<struct type *> range_types;
15790 child_die = die->child;
15791 while (child_die && child_die->tag)
15792 {
15793 if (child_die->tag == DW_TAG_subrange_type)
15794 {
15795 struct type *child_type = read_type_die (child_die, cu);
15796
15797 if (child_type != NULL)
15798 {
15799 /* The range type was succesfully read. Save it for the
15800 array type creation. */
15801 range_types.push_back (child_type);
15802 }
15803 }
15804 child_die = child_die->sibling;
15805 }
15806
15807 /* Dwarf2 dimensions are output from left to right, create the
15808 necessary array types in backwards order. */
15809
15810 type = element_type;
15811
15812 if (read_array_order (die, cu) == DW_ORD_col_major)
15813 {
15814 int i = 0;
15815
15816 while (i < range_types.size ())
15817 type = create_array_type_with_stride (NULL, type, range_types[i++],
15818 byte_stride_prop, bit_stride);
15819 }
15820 else
15821 {
15822 size_t ndim = range_types.size ();
15823 while (ndim-- > 0)
15824 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15825 byte_stride_prop, bit_stride);
15826 }
15827
15828 /* Understand Dwarf2 support for vector types (like they occur on
15829 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15830 array type. This is not part of the Dwarf2/3 standard yet, but a
15831 custom vendor extension. The main difference between a regular
15832 array and the vector variant is that vectors are passed by value
15833 to functions. */
15834 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15835 if (attr != nullptr)
15836 make_vector_type (type);
15837
15838 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15839 implementation may choose to implement triple vectors using this
15840 attribute. */
15841 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15842 if (attr != nullptr)
15843 {
15844 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15845 TYPE_LENGTH (type) = DW_UNSND (attr);
15846 else
15847 complaint (_("DW_AT_byte_size for array type smaller "
15848 "than the total size of elements"));
15849 }
15850
15851 name = dwarf2_name (die, cu);
15852 if (name)
15853 TYPE_NAME (type) = name;
15854
15855 maybe_set_alignment (cu, die, type);
15856
15857 /* Install the type in the die. */
15858 set_die_type (die, type, cu);
15859
15860 /* set_die_type should be already done. */
15861 set_descriptive_type (type, die, cu);
15862
15863 return type;
15864 }
15865
15866 static enum dwarf_array_dim_ordering
15867 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15868 {
15869 struct attribute *attr;
15870
15871 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15872
15873 if (attr != nullptr)
15874 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15875
15876 /* GNU F77 is a special case, as at 08/2004 array type info is the
15877 opposite order to the dwarf2 specification, but data is still
15878 laid out as per normal fortran.
15879
15880 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15881 version checking. */
15882
15883 if (cu->language == language_fortran
15884 && cu->producer && strstr (cu->producer, "GNU F77"))
15885 {
15886 return DW_ORD_row_major;
15887 }
15888
15889 switch (cu->language_defn->la_array_ordering)
15890 {
15891 case array_column_major:
15892 return DW_ORD_col_major;
15893 case array_row_major:
15894 default:
15895 return DW_ORD_row_major;
15896 };
15897 }
15898
15899 /* Extract all information from a DW_TAG_set_type DIE and put it in
15900 the DIE's type field. */
15901
15902 static struct type *
15903 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15904 {
15905 struct type *domain_type, *set_type;
15906 struct attribute *attr;
15907
15908 domain_type = die_type (die, cu);
15909
15910 /* The die_type call above may have already set the type for this DIE. */
15911 set_type = get_die_type (die, cu);
15912 if (set_type)
15913 return set_type;
15914
15915 set_type = create_set_type (NULL, domain_type);
15916
15917 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15918 if (attr != nullptr)
15919 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15920
15921 maybe_set_alignment (cu, die, set_type);
15922
15923 return set_die_type (die, set_type, cu);
15924 }
15925
15926 /* A helper for read_common_block that creates a locexpr baton.
15927 SYM is the symbol which we are marking as computed.
15928 COMMON_DIE is the DIE for the common block.
15929 COMMON_LOC is the location expression attribute for the common
15930 block itself.
15931 MEMBER_LOC is the location expression attribute for the particular
15932 member of the common block that we are processing.
15933 CU is the CU from which the above come. */
15934
15935 static void
15936 mark_common_block_symbol_computed (struct symbol *sym,
15937 struct die_info *common_die,
15938 struct attribute *common_loc,
15939 struct attribute *member_loc,
15940 struct dwarf2_cu *cu)
15941 {
15942 struct dwarf2_per_objfile *dwarf2_per_objfile
15943 = cu->per_cu->dwarf2_per_objfile;
15944 struct objfile *objfile = dwarf2_per_objfile->objfile;
15945 struct dwarf2_locexpr_baton *baton;
15946 gdb_byte *ptr;
15947 unsigned int cu_off;
15948 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15949 LONGEST offset = 0;
15950
15951 gdb_assert (common_loc && member_loc);
15952 gdb_assert (common_loc->form_is_block ());
15953 gdb_assert (member_loc->form_is_block ()
15954 || member_loc->form_is_constant ());
15955
15956 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15957 baton->per_cu = cu->per_cu;
15958 gdb_assert (baton->per_cu);
15959
15960 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15961
15962 if (member_loc->form_is_constant ())
15963 {
15964 offset = member_loc->constant_value (0);
15965 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15966 }
15967 else
15968 baton->size += DW_BLOCK (member_loc)->size;
15969
15970 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15971 baton->data = ptr;
15972
15973 *ptr++ = DW_OP_call4;
15974 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15975 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15976 ptr += 4;
15977
15978 if (member_loc->form_is_constant ())
15979 {
15980 *ptr++ = DW_OP_addr;
15981 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15982 ptr += cu->header.addr_size;
15983 }
15984 else
15985 {
15986 /* We have to copy the data here, because DW_OP_call4 will only
15987 use a DW_AT_location attribute. */
15988 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15989 ptr += DW_BLOCK (member_loc)->size;
15990 }
15991
15992 *ptr++ = DW_OP_plus;
15993 gdb_assert (ptr - baton->data == baton->size);
15994
15995 SYMBOL_LOCATION_BATON (sym) = baton;
15996 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
15997 }
15998
15999 /* Create appropriate locally-scoped variables for all the
16000 DW_TAG_common_block entries. Also create a struct common_block
16001 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16002 is used to separate the common blocks name namespace from regular
16003 variable names. */
16004
16005 static void
16006 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16007 {
16008 struct attribute *attr;
16009
16010 attr = dwarf2_attr (die, DW_AT_location, cu);
16011 if (attr != nullptr)
16012 {
16013 /* Support the .debug_loc offsets. */
16014 if (attr->form_is_block ())
16015 {
16016 /* Ok. */
16017 }
16018 else if (attr->form_is_section_offset ())
16019 {
16020 dwarf2_complex_location_expr_complaint ();
16021 attr = NULL;
16022 }
16023 else
16024 {
16025 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16026 "common block member");
16027 attr = NULL;
16028 }
16029 }
16030
16031 if (die->child != NULL)
16032 {
16033 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16034 struct die_info *child_die;
16035 size_t n_entries = 0, size;
16036 struct common_block *common_block;
16037 struct symbol *sym;
16038
16039 for (child_die = die->child;
16040 child_die && child_die->tag;
16041 child_die = child_die->sibling)
16042 ++n_entries;
16043
16044 size = (sizeof (struct common_block)
16045 + (n_entries - 1) * sizeof (struct symbol *));
16046 common_block
16047 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16048 size);
16049 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16050 common_block->n_entries = 0;
16051
16052 for (child_die = die->child;
16053 child_die && child_die->tag;
16054 child_die = child_die->sibling)
16055 {
16056 /* Create the symbol in the DW_TAG_common_block block in the current
16057 symbol scope. */
16058 sym = new_symbol (child_die, NULL, cu);
16059 if (sym != NULL)
16060 {
16061 struct attribute *member_loc;
16062
16063 common_block->contents[common_block->n_entries++] = sym;
16064
16065 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16066 cu);
16067 if (member_loc)
16068 {
16069 /* GDB has handled this for a long time, but it is
16070 not specified by DWARF. It seems to have been
16071 emitted by gfortran at least as recently as:
16072 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16073 complaint (_("Variable in common block has "
16074 "DW_AT_data_member_location "
16075 "- DIE at %s [in module %s]"),
16076 sect_offset_str (child_die->sect_off),
16077 objfile_name (objfile));
16078
16079 if (member_loc->form_is_section_offset ())
16080 dwarf2_complex_location_expr_complaint ();
16081 else if (member_loc->form_is_constant ()
16082 || member_loc->form_is_block ())
16083 {
16084 if (attr != nullptr)
16085 mark_common_block_symbol_computed (sym, die, attr,
16086 member_loc, cu);
16087 }
16088 else
16089 dwarf2_complex_location_expr_complaint ();
16090 }
16091 }
16092 }
16093
16094 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16095 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16096 }
16097 }
16098
16099 /* Create a type for a C++ namespace. */
16100
16101 static struct type *
16102 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16103 {
16104 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16105 const char *previous_prefix, *name;
16106 int is_anonymous;
16107 struct type *type;
16108
16109 /* For extensions, reuse the type of the original namespace. */
16110 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16111 {
16112 struct die_info *ext_die;
16113 struct dwarf2_cu *ext_cu = cu;
16114
16115 ext_die = dwarf2_extension (die, &ext_cu);
16116 type = read_type_die (ext_die, ext_cu);
16117
16118 /* EXT_CU may not be the same as CU.
16119 Ensure TYPE is recorded with CU in die_type_hash. */
16120 return set_die_type (die, type, cu);
16121 }
16122
16123 name = namespace_name (die, &is_anonymous, cu);
16124
16125 /* Now build the name of the current namespace. */
16126
16127 previous_prefix = determine_prefix (die, cu);
16128 if (previous_prefix[0] != '\0')
16129 name = typename_concat (&objfile->objfile_obstack,
16130 previous_prefix, name, 0, cu);
16131
16132 /* Create the type. */
16133 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16134
16135 return set_die_type (die, type, cu);
16136 }
16137
16138 /* Read a namespace scope. */
16139
16140 static void
16141 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16142 {
16143 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16144 int is_anonymous;
16145
16146 /* Add a symbol associated to this if we haven't seen the namespace
16147 before. Also, add a using directive if it's an anonymous
16148 namespace. */
16149
16150 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16151 {
16152 struct type *type;
16153
16154 type = read_type_die (die, cu);
16155 new_symbol (die, type, cu);
16156
16157 namespace_name (die, &is_anonymous, cu);
16158 if (is_anonymous)
16159 {
16160 const char *previous_prefix = determine_prefix (die, cu);
16161
16162 std::vector<const char *> excludes;
16163 add_using_directive (using_directives (cu),
16164 previous_prefix, TYPE_NAME (type), NULL,
16165 NULL, excludes, 0, &objfile->objfile_obstack);
16166 }
16167 }
16168
16169 if (die->child != NULL)
16170 {
16171 struct die_info *child_die = die->child;
16172
16173 while (child_die && child_die->tag)
16174 {
16175 process_die (child_die, cu);
16176 child_die = child_die->sibling;
16177 }
16178 }
16179 }
16180
16181 /* Read a Fortran module as type. This DIE can be only a declaration used for
16182 imported module. Still we need that type as local Fortran "use ... only"
16183 declaration imports depend on the created type in determine_prefix. */
16184
16185 static struct type *
16186 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16187 {
16188 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16189 const char *module_name;
16190 struct type *type;
16191
16192 module_name = dwarf2_name (die, cu);
16193 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16194
16195 return set_die_type (die, type, cu);
16196 }
16197
16198 /* Read a Fortran module. */
16199
16200 static void
16201 read_module (struct die_info *die, struct dwarf2_cu *cu)
16202 {
16203 struct die_info *child_die = die->child;
16204 struct type *type;
16205
16206 type = read_type_die (die, cu);
16207 new_symbol (die, type, cu);
16208
16209 while (child_die && child_die->tag)
16210 {
16211 process_die (child_die, cu);
16212 child_die = child_die->sibling;
16213 }
16214 }
16215
16216 /* Return the name of the namespace represented by DIE. Set
16217 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16218 namespace. */
16219
16220 static const char *
16221 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16222 {
16223 struct die_info *current_die;
16224 const char *name = NULL;
16225
16226 /* Loop through the extensions until we find a name. */
16227
16228 for (current_die = die;
16229 current_die != NULL;
16230 current_die = dwarf2_extension (die, &cu))
16231 {
16232 /* We don't use dwarf2_name here so that we can detect the absence
16233 of a name -> anonymous namespace. */
16234 name = dwarf2_string_attr (die, DW_AT_name, cu);
16235
16236 if (name != NULL)
16237 break;
16238 }
16239
16240 /* Is it an anonymous namespace? */
16241
16242 *is_anonymous = (name == NULL);
16243 if (*is_anonymous)
16244 name = CP_ANONYMOUS_NAMESPACE_STR;
16245
16246 return name;
16247 }
16248
16249 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16250 the user defined type vector. */
16251
16252 static struct type *
16253 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16254 {
16255 struct gdbarch *gdbarch
16256 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16257 struct comp_unit_head *cu_header = &cu->header;
16258 struct type *type;
16259 struct attribute *attr_byte_size;
16260 struct attribute *attr_address_class;
16261 int byte_size, addr_class;
16262 struct type *target_type;
16263
16264 target_type = die_type (die, cu);
16265
16266 /* The die_type call above may have already set the type for this DIE. */
16267 type = get_die_type (die, cu);
16268 if (type)
16269 return type;
16270
16271 type = lookup_pointer_type (target_type);
16272
16273 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16274 if (attr_byte_size)
16275 byte_size = DW_UNSND (attr_byte_size);
16276 else
16277 byte_size = cu_header->addr_size;
16278
16279 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16280 if (attr_address_class)
16281 addr_class = DW_UNSND (attr_address_class);
16282 else
16283 addr_class = DW_ADDR_none;
16284
16285 ULONGEST alignment = get_alignment (cu, die);
16286
16287 /* If the pointer size, alignment, or address class is different
16288 than the default, create a type variant marked as such and set
16289 the length accordingly. */
16290 if (TYPE_LENGTH (type) != byte_size
16291 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16292 && alignment != TYPE_RAW_ALIGN (type))
16293 || addr_class != DW_ADDR_none)
16294 {
16295 if (gdbarch_address_class_type_flags_p (gdbarch))
16296 {
16297 int type_flags;
16298
16299 type_flags = gdbarch_address_class_type_flags
16300 (gdbarch, byte_size, addr_class);
16301 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16302 == 0);
16303 type = make_type_with_address_space (type, type_flags);
16304 }
16305 else if (TYPE_LENGTH (type) != byte_size)
16306 {
16307 complaint (_("invalid pointer size %d"), byte_size);
16308 }
16309 else if (TYPE_RAW_ALIGN (type) != alignment)
16310 {
16311 complaint (_("Invalid DW_AT_alignment"
16312 " - DIE at %s [in module %s]"),
16313 sect_offset_str (die->sect_off),
16314 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16315 }
16316 else
16317 {
16318 /* Should we also complain about unhandled address classes? */
16319 }
16320 }
16321
16322 TYPE_LENGTH (type) = byte_size;
16323 set_type_align (type, alignment);
16324 return set_die_type (die, type, cu);
16325 }
16326
16327 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16328 the user defined type vector. */
16329
16330 static struct type *
16331 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16332 {
16333 struct type *type;
16334 struct type *to_type;
16335 struct type *domain;
16336
16337 to_type = die_type (die, cu);
16338 domain = die_containing_type (die, cu);
16339
16340 /* The calls above may have already set the type for this DIE. */
16341 type = get_die_type (die, cu);
16342 if (type)
16343 return type;
16344
16345 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16346 type = lookup_methodptr_type (to_type);
16347 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16348 {
16349 struct type *new_type
16350 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16351
16352 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16353 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16354 TYPE_VARARGS (to_type));
16355 type = lookup_methodptr_type (new_type);
16356 }
16357 else
16358 type = lookup_memberptr_type (to_type, domain);
16359
16360 return set_die_type (die, type, cu);
16361 }
16362
16363 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16364 the user defined type vector. */
16365
16366 static struct type *
16367 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16368 enum type_code refcode)
16369 {
16370 struct comp_unit_head *cu_header = &cu->header;
16371 struct type *type, *target_type;
16372 struct attribute *attr;
16373
16374 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16375
16376 target_type = die_type (die, cu);
16377
16378 /* The die_type call above may have already set the type for this DIE. */
16379 type = get_die_type (die, cu);
16380 if (type)
16381 return type;
16382
16383 type = lookup_reference_type (target_type, refcode);
16384 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16385 if (attr != nullptr)
16386 {
16387 TYPE_LENGTH (type) = DW_UNSND (attr);
16388 }
16389 else
16390 {
16391 TYPE_LENGTH (type) = cu_header->addr_size;
16392 }
16393 maybe_set_alignment (cu, die, type);
16394 return set_die_type (die, type, cu);
16395 }
16396
16397 /* Add the given cv-qualifiers to the element type of the array. GCC
16398 outputs DWARF type qualifiers that apply to an array, not the
16399 element type. But GDB relies on the array element type to carry
16400 the cv-qualifiers. This mimics section 6.7.3 of the C99
16401 specification. */
16402
16403 static struct type *
16404 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16405 struct type *base_type, int cnst, int voltl)
16406 {
16407 struct type *el_type, *inner_array;
16408
16409 base_type = copy_type (base_type);
16410 inner_array = base_type;
16411
16412 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16413 {
16414 TYPE_TARGET_TYPE (inner_array) =
16415 copy_type (TYPE_TARGET_TYPE (inner_array));
16416 inner_array = TYPE_TARGET_TYPE (inner_array);
16417 }
16418
16419 el_type = TYPE_TARGET_TYPE (inner_array);
16420 cnst |= TYPE_CONST (el_type);
16421 voltl |= TYPE_VOLATILE (el_type);
16422 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16423
16424 return set_die_type (die, base_type, cu);
16425 }
16426
16427 static struct type *
16428 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16429 {
16430 struct type *base_type, *cv_type;
16431
16432 base_type = die_type (die, cu);
16433
16434 /* The die_type call above may have already set the type for this DIE. */
16435 cv_type = get_die_type (die, cu);
16436 if (cv_type)
16437 return cv_type;
16438
16439 /* In case the const qualifier is applied to an array type, the element type
16440 is so qualified, not the array type (section 6.7.3 of C99). */
16441 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16442 return add_array_cv_type (die, cu, base_type, 1, 0);
16443
16444 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16445 return set_die_type (die, cv_type, cu);
16446 }
16447
16448 static struct type *
16449 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16450 {
16451 struct type *base_type, *cv_type;
16452
16453 base_type = die_type (die, cu);
16454
16455 /* The die_type call above may have already set the type for this DIE. */
16456 cv_type = get_die_type (die, cu);
16457 if (cv_type)
16458 return cv_type;
16459
16460 /* In case the volatile qualifier is applied to an array type, the
16461 element type is so qualified, not the array type (section 6.7.3
16462 of C99). */
16463 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16464 return add_array_cv_type (die, cu, base_type, 0, 1);
16465
16466 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16467 return set_die_type (die, cv_type, cu);
16468 }
16469
16470 /* Handle DW_TAG_restrict_type. */
16471
16472 static struct type *
16473 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16474 {
16475 struct type *base_type, *cv_type;
16476
16477 base_type = die_type (die, cu);
16478
16479 /* The die_type call above may have already set the type for this DIE. */
16480 cv_type = get_die_type (die, cu);
16481 if (cv_type)
16482 return cv_type;
16483
16484 cv_type = make_restrict_type (base_type);
16485 return set_die_type (die, cv_type, cu);
16486 }
16487
16488 /* Handle DW_TAG_atomic_type. */
16489
16490 static struct type *
16491 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16492 {
16493 struct type *base_type, *cv_type;
16494
16495 base_type = die_type (die, cu);
16496
16497 /* The die_type call above may have already set the type for this DIE. */
16498 cv_type = get_die_type (die, cu);
16499 if (cv_type)
16500 return cv_type;
16501
16502 cv_type = make_atomic_type (base_type);
16503 return set_die_type (die, cv_type, cu);
16504 }
16505
16506 /* Extract all information from a DW_TAG_string_type DIE and add to
16507 the user defined type vector. It isn't really a user defined type,
16508 but it behaves like one, with other DIE's using an AT_user_def_type
16509 attribute to reference it. */
16510
16511 static struct type *
16512 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16513 {
16514 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16515 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16516 struct type *type, *range_type, *index_type, *char_type;
16517 struct attribute *attr;
16518 struct dynamic_prop prop;
16519 bool length_is_constant = true;
16520 LONGEST length;
16521
16522 /* There are a couple of places where bit sizes might be made use of
16523 when parsing a DW_TAG_string_type, however, no producer that we know
16524 of make use of these. Handling bit sizes that are a multiple of the
16525 byte size is easy enough, but what about other bit sizes? Lets deal
16526 with that problem when we have to. Warn about these attributes being
16527 unsupported, then parse the type and ignore them like we always
16528 have. */
16529 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16530 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16531 {
16532 static bool warning_printed = false;
16533 if (!warning_printed)
16534 {
16535 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16536 "currently supported on DW_TAG_string_type."));
16537 warning_printed = true;
16538 }
16539 }
16540
16541 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16542 if (attr != nullptr && !attr->form_is_constant ())
16543 {
16544 /* The string length describes the location at which the length of
16545 the string can be found. The size of the length field can be
16546 specified with one of the attributes below. */
16547 struct type *prop_type;
16548 struct attribute *len
16549 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16550 if (len == nullptr)
16551 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16552 if (len != nullptr && len->form_is_constant ())
16553 {
16554 /* Pass 0 as the default as we know this attribute is constant
16555 and the default value will not be returned. */
16556 LONGEST sz = len->constant_value (0);
16557 prop_type = cu->per_cu->int_type (sz, true);
16558 }
16559 else
16560 {
16561 /* If the size is not specified then we assume it is the size of
16562 an address on this target. */
16563 prop_type = cu->per_cu->addr_sized_int_type (true);
16564 }
16565
16566 /* Convert the attribute into a dynamic property. */
16567 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16568 length = 1;
16569 else
16570 length_is_constant = false;
16571 }
16572 else if (attr != nullptr)
16573 {
16574 /* This DW_AT_string_length just contains the length with no
16575 indirection. There's no need to create a dynamic property in this
16576 case. Pass 0 for the default value as we know it will not be
16577 returned in this case. */
16578 length = attr->constant_value (0);
16579 }
16580 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16581 {
16582 /* We don't currently support non-constant byte sizes for strings. */
16583 length = attr->constant_value (1);
16584 }
16585 else
16586 {
16587 /* Use 1 as a fallback length if we have nothing else. */
16588 length = 1;
16589 }
16590
16591 index_type = objfile_type (objfile)->builtin_int;
16592 if (length_is_constant)
16593 range_type = create_static_range_type (NULL, index_type, 1, length);
16594 else
16595 {
16596 struct dynamic_prop low_bound;
16597
16598 low_bound.kind = PROP_CONST;
16599 low_bound.data.const_val = 1;
16600 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16601 }
16602 char_type = language_string_char_type (cu->language_defn, gdbarch);
16603 type = create_string_type (NULL, char_type, range_type);
16604
16605 return set_die_type (die, type, cu);
16606 }
16607
16608 /* Assuming that DIE corresponds to a function, returns nonzero
16609 if the function is prototyped. */
16610
16611 static int
16612 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16613 {
16614 struct attribute *attr;
16615
16616 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16617 if (attr && (DW_UNSND (attr) != 0))
16618 return 1;
16619
16620 /* The DWARF standard implies that the DW_AT_prototyped attribute
16621 is only meaningful for C, but the concept also extends to other
16622 languages that allow unprototyped functions (Eg: Objective C).
16623 For all other languages, assume that functions are always
16624 prototyped. */
16625 if (cu->language != language_c
16626 && cu->language != language_objc
16627 && cu->language != language_opencl)
16628 return 1;
16629
16630 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16631 prototyped and unprototyped functions; default to prototyped,
16632 since that is more common in modern code (and RealView warns
16633 about unprototyped functions). */
16634 if (producer_is_realview (cu->producer))
16635 return 1;
16636
16637 return 0;
16638 }
16639
16640 /* Handle DIES due to C code like:
16641
16642 struct foo
16643 {
16644 int (*funcp)(int a, long l);
16645 int b;
16646 };
16647
16648 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16649
16650 static struct type *
16651 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16652 {
16653 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16654 struct type *type; /* Type that this function returns. */
16655 struct type *ftype; /* Function that returns above type. */
16656 struct attribute *attr;
16657
16658 type = die_type (die, cu);
16659
16660 /* The die_type call above may have already set the type for this DIE. */
16661 ftype = get_die_type (die, cu);
16662 if (ftype)
16663 return ftype;
16664
16665 ftype = lookup_function_type (type);
16666
16667 if (prototyped_function_p (die, cu))
16668 TYPE_PROTOTYPED (ftype) = 1;
16669
16670 /* Store the calling convention in the type if it's available in
16671 the subroutine die. Otherwise set the calling convention to
16672 the default value DW_CC_normal. */
16673 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16674 if (attr != nullptr
16675 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16676 TYPE_CALLING_CONVENTION (ftype)
16677 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16678 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16679 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16680 else
16681 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16682
16683 /* Record whether the function returns normally to its caller or not
16684 if the DWARF producer set that information. */
16685 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16686 if (attr && (DW_UNSND (attr) != 0))
16687 TYPE_NO_RETURN (ftype) = 1;
16688
16689 /* We need to add the subroutine type to the die immediately so
16690 we don't infinitely recurse when dealing with parameters
16691 declared as the same subroutine type. */
16692 set_die_type (die, ftype, cu);
16693
16694 if (die->child != NULL)
16695 {
16696 struct type *void_type = objfile_type (objfile)->builtin_void;
16697 struct die_info *child_die;
16698 int nparams, iparams;
16699
16700 /* Count the number of parameters.
16701 FIXME: GDB currently ignores vararg functions, but knows about
16702 vararg member functions. */
16703 nparams = 0;
16704 child_die = die->child;
16705 while (child_die && child_die->tag)
16706 {
16707 if (child_die->tag == DW_TAG_formal_parameter)
16708 nparams++;
16709 else if (child_die->tag == DW_TAG_unspecified_parameters)
16710 TYPE_VARARGS (ftype) = 1;
16711 child_die = child_die->sibling;
16712 }
16713
16714 /* Allocate storage for parameters and fill them in. */
16715 TYPE_NFIELDS (ftype) = nparams;
16716 TYPE_FIELDS (ftype) = (struct field *)
16717 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16718
16719 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16720 even if we error out during the parameters reading below. */
16721 for (iparams = 0; iparams < nparams; iparams++)
16722 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16723
16724 iparams = 0;
16725 child_die = die->child;
16726 while (child_die && child_die->tag)
16727 {
16728 if (child_die->tag == DW_TAG_formal_parameter)
16729 {
16730 struct type *arg_type;
16731
16732 /* DWARF version 2 has no clean way to discern C++
16733 static and non-static member functions. G++ helps
16734 GDB by marking the first parameter for non-static
16735 member functions (which is the this pointer) as
16736 artificial. We pass this information to
16737 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16738
16739 DWARF version 3 added DW_AT_object_pointer, which GCC
16740 4.5 does not yet generate. */
16741 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16742 if (attr != nullptr)
16743 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16744 else
16745 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16746 arg_type = die_type (child_die, cu);
16747
16748 /* RealView does not mark THIS as const, which the testsuite
16749 expects. GCC marks THIS as const in method definitions,
16750 but not in the class specifications (GCC PR 43053). */
16751 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16752 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16753 {
16754 int is_this = 0;
16755 struct dwarf2_cu *arg_cu = cu;
16756 const char *name = dwarf2_name (child_die, cu);
16757
16758 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16759 if (attr != nullptr)
16760 {
16761 /* If the compiler emits this, use it. */
16762 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16763 is_this = 1;
16764 }
16765 else if (name && strcmp (name, "this") == 0)
16766 /* Function definitions will have the argument names. */
16767 is_this = 1;
16768 else if (name == NULL && iparams == 0)
16769 /* Declarations may not have the names, so like
16770 elsewhere in GDB, assume an artificial first
16771 argument is "this". */
16772 is_this = 1;
16773
16774 if (is_this)
16775 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16776 arg_type, 0);
16777 }
16778
16779 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16780 iparams++;
16781 }
16782 child_die = child_die->sibling;
16783 }
16784 }
16785
16786 return ftype;
16787 }
16788
16789 static struct type *
16790 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16791 {
16792 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16793 const char *name = NULL;
16794 struct type *this_type, *target_type;
16795
16796 name = dwarf2_full_name (NULL, die, cu);
16797 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16798 TYPE_TARGET_STUB (this_type) = 1;
16799 set_die_type (die, this_type, cu);
16800 target_type = die_type (die, cu);
16801 if (target_type != this_type)
16802 TYPE_TARGET_TYPE (this_type) = target_type;
16803 else
16804 {
16805 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16806 spec and cause infinite loops in GDB. */
16807 complaint (_("Self-referential DW_TAG_typedef "
16808 "- DIE at %s [in module %s]"),
16809 sect_offset_str (die->sect_off), objfile_name (objfile));
16810 TYPE_TARGET_TYPE (this_type) = NULL;
16811 }
16812 if (name == NULL)
16813 {
16814 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16815 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16816 Handle these by just returning the target type, rather than
16817 constructing an anonymous typedef type and trying to handle this
16818 elsewhere. */
16819 set_die_type (die, target_type, cu);
16820 return target_type;
16821 }
16822 return this_type;
16823 }
16824
16825 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16826 (which may be different from NAME) to the architecture back-end to allow
16827 it to guess the correct format if necessary. */
16828
16829 static struct type *
16830 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16831 const char *name_hint, enum bfd_endian byte_order)
16832 {
16833 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16834 const struct floatformat **format;
16835 struct type *type;
16836
16837 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16838 if (format)
16839 type = init_float_type (objfile, bits, name, format, byte_order);
16840 else
16841 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16842
16843 return type;
16844 }
16845
16846 /* Allocate an integer type of size BITS and name NAME. */
16847
16848 static struct type *
16849 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16850 int bits, int unsigned_p, const char *name)
16851 {
16852 struct type *type;
16853
16854 /* Versions of Intel's C Compiler generate an integer type called "void"
16855 instead of using DW_TAG_unspecified_type. This has been seen on
16856 at least versions 14, 17, and 18. */
16857 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16858 && strcmp (name, "void") == 0)
16859 type = objfile_type (objfile)->builtin_void;
16860 else
16861 type = init_integer_type (objfile, bits, unsigned_p, name);
16862
16863 return type;
16864 }
16865
16866 /* Initialise and return a floating point type of size BITS suitable for
16867 use as a component of a complex number. The NAME_HINT is passed through
16868 when initialising the floating point type and is the name of the complex
16869 type.
16870
16871 As DWARF doesn't currently provide an explicit name for the components
16872 of a complex number, but it can be helpful to have these components
16873 named, we try to select a suitable name based on the size of the
16874 component. */
16875 static struct type *
16876 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16877 struct objfile *objfile,
16878 int bits, const char *name_hint,
16879 enum bfd_endian byte_order)
16880 {
16881 gdbarch *gdbarch = get_objfile_arch (objfile);
16882 struct type *tt = nullptr;
16883
16884 /* Try to find a suitable floating point builtin type of size BITS.
16885 We're going to use the name of this type as the name for the complex
16886 target type that we are about to create. */
16887 switch (cu->language)
16888 {
16889 case language_fortran:
16890 switch (bits)
16891 {
16892 case 32:
16893 tt = builtin_f_type (gdbarch)->builtin_real;
16894 break;
16895 case 64:
16896 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16897 break;
16898 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16899 case 128:
16900 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16901 break;
16902 }
16903 break;
16904 default:
16905 switch (bits)
16906 {
16907 case 32:
16908 tt = builtin_type (gdbarch)->builtin_float;
16909 break;
16910 case 64:
16911 tt = builtin_type (gdbarch)->builtin_double;
16912 break;
16913 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16914 case 128:
16915 tt = builtin_type (gdbarch)->builtin_long_double;
16916 break;
16917 }
16918 break;
16919 }
16920
16921 /* If the type we found doesn't match the size we were looking for, then
16922 pretend we didn't find a type at all, the complex target type we
16923 create will then be nameless. */
16924 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
16925 tt = nullptr;
16926
16927 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
16928 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
16929 }
16930
16931 /* Find a representation of a given base type and install
16932 it in the TYPE field of the die. */
16933
16934 static struct type *
16935 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16936 {
16937 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16938 struct type *type;
16939 struct attribute *attr;
16940 int encoding = 0, bits = 0;
16941 const char *name;
16942 gdbarch *arch;
16943
16944 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16945 if (attr != nullptr)
16946 encoding = DW_UNSND (attr);
16947 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16948 if (attr != nullptr)
16949 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16950 name = dwarf2_name (die, cu);
16951 if (!name)
16952 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
16953
16954 arch = get_objfile_arch (objfile);
16955 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16956
16957 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16958 if (attr)
16959 {
16960 int endianity = DW_UNSND (attr);
16961
16962 switch (endianity)
16963 {
16964 case DW_END_big:
16965 byte_order = BFD_ENDIAN_BIG;
16966 break;
16967 case DW_END_little:
16968 byte_order = BFD_ENDIAN_LITTLE;
16969 break;
16970 default:
16971 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16972 break;
16973 }
16974 }
16975
16976 switch (encoding)
16977 {
16978 case DW_ATE_address:
16979 /* Turn DW_ATE_address into a void * pointer. */
16980 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16981 type = init_pointer_type (objfile, bits, name, type);
16982 break;
16983 case DW_ATE_boolean:
16984 type = init_boolean_type (objfile, bits, 1, name);
16985 break;
16986 case DW_ATE_complex_float:
16987 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16988 byte_order);
16989 if (TYPE_CODE (type) == TYPE_CODE_ERROR)
16990 {
16991 if (name == nullptr)
16992 {
16993 struct obstack *obstack
16994 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
16995 name = obconcat (obstack, "_Complex ", TYPE_NAME (type),
16996 nullptr);
16997 }
16998 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16999 }
17000 else
17001 type = init_complex_type (name, type);
17002 break;
17003 case DW_ATE_decimal_float:
17004 type = init_decfloat_type (objfile, bits, name);
17005 break;
17006 case DW_ATE_float:
17007 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17008 break;
17009 case DW_ATE_signed:
17010 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17011 break;
17012 case DW_ATE_unsigned:
17013 if (cu->language == language_fortran
17014 && name
17015 && startswith (name, "character("))
17016 type = init_character_type (objfile, bits, 1, name);
17017 else
17018 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17019 break;
17020 case DW_ATE_signed_char:
17021 if (cu->language == language_ada || cu->language == language_m2
17022 || cu->language == language_pascal
17023 || cu->language == language_fortran)
17024 type = init_character_type (objfile, bits, 0, name);
17025 else
17026 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17027 break;
17028 case DW_ATE_unsigned_char:
17029 if (cu->language == language_ada || cu->language == language_m2
17030 || cu->language == language_pascal
17031 || cu->language == language_fortran
17032 || cu->language == language_rust)
17033 type = init_character_type (objfile, bits, 1, name);
17034 else
17035 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17036 break;
17037 case DW_ATE_UTF:
17038 {
17039 if (bits == 16)
17040 type = builtin_type (arch)->builtin_char16;
17041 else if (bits == 32)
17042 type = builtin_type (arch)->builtin_char32;
17043 else
17044 {
17045 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17046 bits);
17047 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17048 }
17049 return set_die_type (die, type, cu);
17050 }
17051 break;
17052
17053 default:
17054 complaint (_("unsupported DW_AT_encoding: '%s'"),
17055 dwarf_type_encoding_name (encoding));
17056 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17057 break;
17058 }
17059
17060 if (name && strcmp (name, "char") == 0)
17061 TYPE_NOSIGN (type) = 1;
17062
17063 maybe_set_alignment (cu, die, type);
17064
17065 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17066
17067 return set_die_type (die, type, cu);
17068 }
17069
17070 /* Parse dwarf attribute if it's a block, reference or constant and put the
17071 resulting value of the attribute into struct bound_prop.
17072 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17073
17074 static int
17075 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17076 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17077 struct type *default_type)
17078 {
17079 struct dwarf2_property_baton *baton;
17080 struct obstack *obstack
17081 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17082
17083 gdb_assert (default_type != NULL);
17084
17085 if (attr == NULL || prop == NULL)
17086 return 0;
17087
17088 if (attr->form_is_block ())
17089 {
17090 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17091 baton->property_type = default_type;
17092 baton->locexpr.per_cu = cu->per_cu;
17093 baton->locexpr.size = DW_BLOCK (attr)->size;
17094 baton->locexpr.data = DW_BLOCK (attr)->data;
17095 switch (attr->name)
17096 {
17097 case DW_AT_string_length:
17098 baton->locexpr.is_reference = true;
17099 break;
17100 default:
17101 baton->locexpr.is_reference = false;
17102 break;
17103 }
17104 prop->data.baton = baton;
17105 prop->kind = PROP_LOCEXPR;
17106 gdb_assert (prop->data.baton != NULL);
17107 }
17108 else if (attr->form_is_ref ())
17109 {
17110 struct dwarf2_cu *target_cu = cu;
17111 struct die_info *target_die;
17112 struct attribute *target_attr;
17113
17114 target_die = follow_die_ref (die, attr, &target_cu);
17115 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17116 if (target_attr == NULL)
17117 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17118 target_cu);
17119 if (target_attr == NULL)
17120 return 0;
17121
17122 switch (target_attr->name)
17123 {
17124 case DW_AT_location:
17125 if (target_attr->form_is_section_offset ())
17126 {
17127 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17128 baton->property_type = die_type (target_die, target_cu);
17129 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17130 prop->data.baton = baton;
17131 prop->kind = PROP_LOCLIST;
17132 gdb_assert (prop->data.baton != NULL);
17133 }
17134 else if (target_attr->form_is_block ())
17135 {
17136 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17137 baton->property_type = die_type (target_die, target_cu);
17138 baton->locexpr.per_cu = cu->per_cu;
17139 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17140 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17141 baton->locexpr.is_reference = true;
17142 prop->data.baton = baton;
17143 prop->kind = PROP_LOCEXPR;
17144 gdb_assert (prop->data.baton != NULL);
17145 }
17146 else
17147 {
17148 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17149 "dynamic property");
17150 return 0;
17151 }
17152 break;
17153 case DW_AT_data_member_location:
17154 {
17155 LONGEST offset;
17156
17157 if (!handle_data_member_location (target_die, target_cu,
17158 &offset))
17159 return 0;
17160
17161 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17162 baton->property_type = read_type_die (target_die->parent,
17163 target_cu);
17164 baton->offset_info.offset = offset;
17165 baton->offset_info.type = die_type (target_die, target_cu);
17166 prop->data.baton = baton;
17167 prop->kind = PROP_ADDR_OFFSET;
17168 break;
17169 }
17170 }
17171 }
17172 else if (attr->form_is_constant ())
17173 {
17174 prop->data.const_val = attr->constant_value (0);
17175 prop->kind = PROP_CONST;
17176 }
17177 else
17178 {
17179 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17180 dwarf2_name (die, cu));
17181 return 0;
17182 }
17183
17184 return 1;
17185 }
17186
17187 /* See read.h. */
17188
17189 struct type *
17190 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17191 {
17192 struct objfile *objfile = dwarf2_per_objfile->objfile;
17193 struct type *int_type;
17194
17195 /* Helper macro to examine the various builtin types. */
17196 #define TRY_TYPE(F) \
17197 int_type = (unsigned_p \
17198 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17199 : objfile_type (objfile)->builtin_ ## F); \
17200 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17201 return int_type
17202
17203 TRY_TYPE (char);
17204 TRY_TYPE (short);
17205 TRY_TYPE (int);
17206 TRY_TYPE (long);
17207 TRY_TYPE (long_long);
17208
17209 #undef TRY_TYPE
17210
17211 gdb_assert_not_reached ("unable to find suitable integer type");
17212 }
17213
17214 /* See read.h. */
17215
17216 struct type *
17217 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17218 {
17219 int addr_size = this->addr_size ();
17220 return int_type (addr_size, unsigned_p);
17221 }
17222
17223 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17224 present (which is valid) then compute the default type based on the
17225 compilation units address size. */
17226
17227 static struct type *
17228 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17229 {
17230 struct type *index_type = die_type (die, cu);
17231
17232 /* Dwarf-2 specifications explicitly allows to create subrange types
17233 without specifying a base type.
17234 In that case, the base type must be set to the type of
17235 the lower bound, upper bound or count, in that order, if any of these
17236 three attributes references an object that has a type.
17237 If no base type is found, the Dwarf-2 specifications say that
17238 a signed integer type of size equal to the size of an address should
17239 be used.
17240 For the following C code: `extern char gdb_int [];'
17241 GCC produces an empty range DIE.
17242 FIXME: muller/2010-05-28: Possible references to object for low bound,
17243 high bound or count are not yet handled by this code. */
17244 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17245 index_type = cu->per_cu->addr_sized_int_type (false);
17246
17247 return index_type;
17248 }
17249
17250 /* Read the given DW_AT_subrange DIE. */
17251
17252 static struct type *
17253 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17254 {
17255 struct type *base_type, *orig_base_type;
17256 struct type *range_type;
17257 struct attribute *attr;
17258 struct dynamic_prop low, high;
17259 int low_default_is_valid;
17260 int high_bound_is_count = 0;
17261 const char *name;
17262 ULONGEST negative_mask;
17263
17264 orig_base_type = read_subrange_index_type (die, cu);
17265
17266 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17267 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17268 creating the range type, but we use the result of check_typedef
17269 when examining properties of the type. */
17270 base_type = check_typedef (orig_base_type);
17271
17272 /* The die_type call above may have already set the type for this DIE. */
17273 range_type = get_die_type (die, cu);
17274 if (range_type)
17275 return range_type;
17276
17277 low.kind = PROP_CONST;
17278 high.kind = PROP_CONST;
17279 high.data.const_val = 0;
17280
17281 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17282 omitting DW_AT_lower_bound. */
17283 switch (cu->language)
17284 {
17285 case language_c:
17286 case language_cplus:
17287 low.data.const_val = 0;
17288 low_default_is_valid = 1;
17289 break;
17290 case language_fortran:
17291 low.data.const_val = 1;
17292 low_default_is_valid = 1;
17293 break;
17294 case language_d:
17295 case language_objc:
17296 case language_rust:
17297 low.data.const_val = 0;
17298 low_default_is_valid = (cu->header.version >= 4);
17299 break;
17300 case language_ada:
17301 case language_m2:
17302 case language_pascal:
17303 low.data.const_val = 1;
17304 low_default_is_valid = (cu->header.version >= 4);
17305 break;
17306 default:
17307 low.data.const_val = 0;
17308 low_default_is_valid = 0;
17309 break;
17310 }
17311
17312 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17313 if (attr != nullptr)
17314 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17315 else if (!low_default_is_valid)
17316 complaint (_("Missing DW_AT_lower_bound "
17317 "- DIE at %s [in module %s]"),
17318 sect_offset_str (die->sect_off),
17319 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17320
17321 struct attribute *attr_ub, *attr_count;
17322 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17323 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17324 {
17325 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17326 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17327 {
17328 /* If bounds are constant do the final calculation here. */
17329 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17330 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17331 else
17332 high_bound_is_count = 1;
17333 }
17334 else
17335 {
17336 if (attr_ub != NULL)
17337 complaint (_("Unresolved DW_AT_upper_bound "
17338 "- DIE at %s [in module %s]"),
17339 sect_offset_str (die->sect_off),
17340 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17341 if (attr_count != NULL)
17342 complaint (_("Unresolved DW_AT_count "
17343 "- DIE at %s [in module %s]"),
17344 sect_offset_str (die->sect_off),
17345 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17346 }
17347 }
17348
17349 LONGEST bias = 0;
17350 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17351 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17352 bias = bias_attr->constant_value (0);
17353
17354 /* Normally, the DWARF producers are expected to use a signed
17355 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17356 But this is unfortunately not always the case, as witnessed
17357 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17358 is used instead. To work around that ambiguity, we treat
17359 the bounds as signed, and thus sign-extend their values, when
17360 the base type is signed. */
17361 negative_mask =
17362 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17363 if (low.kind == PROP_CONST
17364 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17365 low.data.const_val |= negative_mask;
17366 if (high.kind == PROP_CONST
17367 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17368 high.data.const_val |= negative_mask;
17369
17370 /* Check for bit and byte strides. */
17371 struct dynamic_prop byte_stride_prop;
17372 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17373 if (attr_byte_stride != nullptr)
17374 {
17375 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17376 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17377 prop_type);
17378 }
17379
17380 struct dynamic_prop bit_stride_prop;
17381 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17382 if (attr_bit_stride != nullptr)
17383 {
17384 /* It only makes sense to have either a bit or byte stride. */
17385 if (attr_byte_stride != nullptr)
17386 {
17387 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17388 "- DIE at %s [in module %s]"),
17389 sect_offset_str (die->sect_off),
17390 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17391 attr_bit_stride = nullptr;
17392 }
17393 else
17394 {
17395 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17396 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17397 prop_type);
17398 }
17399 }
17400
17401 if (attr_byte_stride != nullptr
17402 || attr_bit_stride != nullptr)
17403 {
17404 bool byte_stride_p = (attr_byte_stride != nullptr);
17405 struct dynamic_prop *stride
17406 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17407
17408 range_type
17409 = create_range_type_with_stride (NULL, orig_base_type, &low,
17410 &high, bias, stride, byte_stride_p);
17411 }
17412 else
17413 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17414
17415 if (high_bound_is_count)
17416 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17417
17418 /* Ada expects an empty array on no boundary attributes. */
17419 if (attr == NULL && cu->language != language_ada)
17420 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17421
17422 name = dwarf2_name (die, cu);
17423 if (name)
17424 TYPE_NAME (range_type) = name;
17425
17426 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17427 if (attr != nullptr)
17428 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17429
17430 maybe_set_alignment (cu, die, range_type);
17431
17432 set_die_type (die, range_type, cu);
17433
17434 /* set_die_type should be already done. */
17435 set_descriptive_type (range_type, die, cu);
17436
17437 return range_type;
17438 }
17439
17440 static struct type *
17441 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17442 {
17443 struct type *type;
17444
17445 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17446 NULL);
17447 TYPE_NAME (type) = dwarf2_name (die, cu);
17448
17449 /* In Ada, an unspecified type is typically used when the description
17450 of the type is deferred to a different unit. When encountering
17451 such a type, we treat it as a stub, and try to resolve it later on,
17452 when needed. */
17453 if (cu->language == language_ada)
17454 TYPE_STUB (type) = 1;
17455
17456 return set_die_type (die, type, cu);
17457 }
17458
17459 /* Read a single die and all its descendents. Set the die's sibling
17460 field to NULL; set other fields in the die correctly, and set all
17461 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17462 location of the info_ptr after reading all of those dies. PARENT
17463 is the parent of the die in question. */
17464
17465 static struct die_info *
17466 read_die_and_children (const struct die_reader_specs *reader,
17467 const gdb_byte *info_ptr,
17468 const gdb_byte **new_info_ptr,
17469 struct die_info *parent)
17470 {
17471 struct die_info *die;
17472 const gdb_byte *cur_ptr;
17473
17474 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17475 if (die == NULL)
17476 {
17477 *new_info_ptr = cur_ptr;
17478 return NULL;
17479 }
17480 store_in_ref_table (die, reader->cu);
17481
17482 if (die->has_children)
17483 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17484 else
17485 {
17486 die->child = NULL;
17487 *new_info_ptr = cur_ptr;
17488 }
17489
17490 die->sibling = NULL;
17491 die->parent = parent;
17492 return die;
17493 }
17494
17495 /* Read a die, all of its descendents, and all of its siblings; set
17496 all of the fields of all of the dies correctly. Arguments are as
17497 in read_die_and_children. */
17498
17499 static struct die_info *
17500 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17501 const gdb_byte *info_ptr,
17502 const gdb_byte **new_info_ptr,
17503 struct die_info *parent)
17504 {
17505 struct die_info *first_die, *last_sibling;
17506 const gdb_byte *cur_ptr;
17507
17508 cur_ptr = info_ptr;
17509 first_die = last_sibling = NULL;
17510
17511 while (1)
17512 {
17513 struct die_info *die
17514 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17515
17516 if (die == NULL)
17517 {
17518 *new_info_ptr = cur_ptr;
17519 return first_die;
17520 }
17521
17522 if (!first_die)
17523 first_die = die;
17524 else
17525 last_sibling->sibling = die;
17526
17527 last_sibling = die;
17528 }
17529 }
17530
17531 /* Read a die, all of its descendents, and all of its siblings; set
17532 all of the fields of all of the dies correctly. Arguments are as
17533 in read_die_and_children.
17534 This the main entry point for reading a DIE and all its children. */
17535
17536 static struct die_info *
17537 read_die_and_siblings (const struct die_reader_specs *reader,
17538 const gdb_byte *info_ptr,
17539 const gdb_byte **new_info_ptr,
17540 struct die_info *parent)
17541 {
17542 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17543 new_info_ptr, parent);
17544
17545 if (dwarf_die_debug)
17546 {
17547 fprintf_unfiltered (gdb_stdlog,
17548 "Read die from %s@0x%x of %s:\n",
17549 reader->die_section->get_name (),
17550 (unsigned) (info_ptr - reader->die_section->buffer),
17551 bfd_get_filename (reader->abfd));
17552 dump_die (die, dwarf_die_debug);
17553 }
17554
17555 return die;
17556 }
17557
17558 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17559 attributes.
17560 The caller is responsible for filling in the extra attributes
17561 and updating (*DIEP)->num_attrs.
17562 Set DIEP to point to a newly allocated die with its information,
17563 except for its child, sibling, and parent fields. */
17564
17565 static const gdb_byte *
17566 read_full_die_1 (const struct die_reader_specs *reader,
17567 struct die_info **diep, const gdb_byte *info_ptr,
17568 int num_extra_attrs)
17569 {
17570 unsigned int abbrev_number, bytes_read, i;
17571 struct abbrev_info *abbrev;
17572 struct die_info *die;
17573 struct dwarf2_cu *cu = reader->cu;
17574 bfd *abfd = reader->abfd;
17575
17576 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17577 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17578 info_ptr += bytes_read;
17579 if (!abbrev_number)
17580 {
17581 *diep = NULL;
17582 return info_ptr;
17583 }
17584
17585 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17586 if (!abbrev)
17587 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17588 abbrev_number,
17589 bfd_get_filename (abfd));
17590
17591 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17592 die->sect_off = sect_off;
17593 die->tag = abbrev->tag;
17594 die->abbrev = abbrev_number;
17595 die->has_children = abbrev->has_children;
17596
17597 /* Make the result usable.
17598 The caller needs to update num_attrs after adding the extra
17599 attributes. */
17600 die->num_attrs = abbrev->num_attrs;
17601
17602 std::vector<int> indexes_that_need_reprocess;
17603 for (i = 0; i < abbrev->num_attrs; ++i)
17604 {
17605 bool need_reprocess;
17606 info_ptr =
17607 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17608 info_ptr, &need_reprocess);
17609 if (need_reprocess)
17610 indexes_that_need_reprocess.push_back (i);
17611 }
17612
17613 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
17614 if (attr != nullptr)
17615 cu->str_offsets_base = DW_UNSND (attr);
17616
17617 attr = die->attr (DW_AT_loclists_base);
17618 if (attr != nullptr)
17619 cu->loclist_base = DW_UNSND (attr);
17620
17621 auto maybe_addr_base = die->addr_base ();
17622 if (maybe_addr_base.has_value ())
17623 cu->addr_base = *maybe_addr_base;
17624 for (int index : indexes_that_need_reprocess)
17625 read_attribute_reprocess (reader, &die->attrs[index]);
17626 *diep = die;
17627 return info_ptr;
17628 }
17629
17630 /* Read a die and all its attributes.
17631 Set DIEP to point to a newly allocated die with its information,
17632 except for its child, sibling, and parent fields. */
17633
17634 static const gdb_byte *
17635 read_full_die (const struct die_reader_specs *reader,
17636 struct die_info **diep, const gdb_byte *info_ptr)
17637 {
17638 const gdb_byte *result;
17639
17640 result = read_full_die_1 (reader, diep, info_ptr, 0);
17641
17642 if (dwarf_die_debug)
17643 {
17644 fprintf_unfiltered (gdb_stdlog,
17645 "Read die from %s@0x%x of %s:\n",
17646 reader->die_section->get_name (),
17647 (unsigned) (info_ptr - reader->die_section->buffer),
17648 bfd_get_filename (reader->abfd));
17649 dump_die (*diep, dwarf_die_debug);
17650 }
17651
17652 return result;
17653 }
17654 \f
17655
17656 /* Returns nonzero if TAG represents a type that we might generate a partial
17657 symbol for. */
17658
17659 static int
17660 is_type_tag_for_partial (int tag)
17661 {
17662 switch (tag)
17663 {
17664 #if 0
17665 /* Some types that would be reasonable to generate partial symbols for,
17666 that we don't at present. */
17667 case DW_TAG_array_type:
17668 case DW_TAG_file_type:
17669 case DW_TAG_ptr_to_member_type:
17670 case DW_TAG_set_type:
17671 case DW_TAG_string_type:
17672 case DW_TAG_subroutine_type:
17673 #endif
17674 case DW_TAG_base_type:
17675 case DW_TAG_class_type:
17676 case DW_TAG_interface_type:
17677 case DW_TAG_enumeration_type:
17678 case DW_TAG_structure_type:
17679 case DW_TAG_subrange_type:
17680 case DW_TAG_typedef:
17681 case DW_TAG_union_type:
17682 return 1;
17683 default:
17684 return 0;
17685 }
17686 }
17687
17688 /* Load all DIEs that are interesting for partial symbols into memory. */
17689
17690 static struct partial_die_info *
17691 load_partial_dies (const struct die_reader_specs *reader,
17692 const gdb_byte *info_ptr, int building_psymtab)
17693 {
17694 struct dwarf2_cu *cu = reader->cu;
17695 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17696 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17697 unsigned int bytes_read;
17698 unsigned int load_all = 0;
17699 int nesting_level = 1;
17700
17701 parent_die = NULL;
17702 last_die = NULL;
17703
17704 gdb_assert (cu->per_cu != NULL);
17705 if (cu->per_cu->load_all_dies)
17706 load_all = 1;
17707
17708 cu->partial_dies
17709 = htab_create_alloc_ex (cu->header.length / 12,
17710 partial_die_hash,
17711 partial_die_eq,
17712 NULL,
17713 &cu->comp_unit_obstack,
17714 hashtab_obstack_allocate,
17715 dummy_obstack_deallocate);
17716
17717 while (1)
17718 {
17719 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17720
17721 /* A NULL abbrev means the end of a series of children. */
17722 if (abbrev == NULL)
17723 {
17724 if (--nesting_level == 0)
17725 return first_die;
17726
17727 info_ptr += bytes_read;
17728 last_die = parent_die;
17729 parent_die = parent_die->die_parent;
17730 continue;
17731 }
17732
17733 /* Check for template arguments. We never save these; if
17734 they're seen, we just mark the parent, and go on our way. */
17735 if (parent_die != NULL
17736 && cu->language == language_cplus
17737 && (abbrev->tag == DW_TAG_template_type_param
17738 || abbrev->tag == DW_TAG_template_value_param))
17739 {
17740 parent_die->has_template_arguments = 1;
17741
17742 if (!load_all)
17743 {
17744 /* We don't need a partial DIE for the template argument. */
17745 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17746 continue;
17747 }
17748 }
17749
17750 /* We only recurse into c++ subprograms looking for template arguments.
17751 Skip their other children. */
17752 if (!load_all
17753 && cu->language == language_cplus
17754 && parent_die != NULL
17755 && parent_die->tag == DW_TAG_subprogram)
17756 {
17757 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17758 continue;
17759 }
17760
17761 /* Check whether this DIE is interesting enough to save. Normally
17762 we would not be interested in members here, but there may be
17763 later variables referencing them via DW_AT_specification (for
17764 static members). */
17765 if (!load_all
17766 && !is_type_tag_for_partial (abbrev->tag)
17767 && abbrev->tag != DW_TAG_constant
17768 && abbrev->tag != DW_TAG_enumerator
17769 && abbrev->tag != DW_TAG_subprogram
17770 && abbrev->tag != DW_TAG_inlined_subroutine
17771 && abbrev->tag != DW_TAG_lexical_block
17772 && abbrev->tag != DW_TAG_variable
17773 && abbrev->tag != DW_TAG_namespace
17774 && abbrev->tag != DW_TAG_module
17775 && abbrev->tag != DW_TAG_member
17776 && abbrev->tag != DW_TAG_imported_unit
17777 && abbrev->tag != DW_TAG_imported_declaration)
17778 {
17779 /* Otherwise we skip to the next sibling, if any. */
17780 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17781 continue;
17782 }
17783
17784 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17785 abbrev);
17786
17787 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
17788
17789 /* This two-pass algorithm for processing partial symbols has a
17790 high cost in cache pressure. Thus, handle some simple cases
17791 here which cover the majority of C partial symbols. DIEs
17792 which neither have specification tags in them, nor could have
17793 specification tags elsewhere pointing at them, can simply be
17794 processed and discarded.
17795
17796 This segment is also optional; scan_partial_symbols and
17797 add_partial_symbol will handle these DIEs if we chain
17798 them in normally. When compilers which do not emit large
17799 quantities of duplicate debug information are more common,
17800 this code can probably be removed. */
17801
17802 /* Any complete simple types at the top level (pretty much all
17803 of them, for a language without namespaces), can be processed
17804 directly. */
17805 if (parent_die == NULL
17806 && pdi.has_specification == 0
17807 && pdi.is_declaration == 0
17808 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17809 || pdi.tag == DW_TAG_base_type
17810 || pdi.tag == DW_TAG_subrange_type))
17811 {
17812 if (building_psymtab && pdi.name != NULL)
17813 add_psymbol_to_list (pdi.name, false,
17814 VAR_DOMAIN, LOC_TYPEDEF, -1,
17815 psymbol_placement::STATIC,
17816 0, cu->language, objfile);
17817 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17818 continue;
17819 }
17820
17821 /* The exception for DW_TAG_typedef with has_children above is
17822 a workaround of GCC PR debug/47510. In the case of this complaint
17823 type_name_or_error will error on such types later.
17824
17825 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17826 it could not find the child DIEs referenced later, this is checked
17827 above. In correct DWARF DW_TAG_typedef should have no children. */
17828
17829 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
17830 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17831 "- DIE at %s [in module %s]"),
17832 sect_offset_str (pdi.sect_off), objfile_name (objfile));
17833
17834 /* If we're at the second level, and we're an enumerator, and
17835 our parent has no specification (meaning possibly lives in a
17836 namespace elsewhere), then we can add the partial symbol now
17837 instead of queueing it. */
17838 if (pdi.tag == DW_TAG_enumerator
17839 && parent_die != NULL
17840 && parent_die->die_parent == NULL
17841 && parent_die->tag == DW_TAG_enumeration_type
17842 && parent_die->has_specification == 0)
17843 {
17844 if (pdi.name == NULL)
17845 complaint (_("malformed enumerator DIE ignored"));
17846 else if (building_psymtab)
17847 add_psymbol_to_list (pdi.name, false,
17848 VAR_DOMAIN, LOC_CONST, -1,
17849 cu->language == language_cplus
17850 ? psymbol_placement::GLOBAL
17851 : psymbol_placement::STATIC,
17852 0, cu->language, objfile);
17853
17854 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17855 continue;
17856 }
17857
17858 struct partial_die_info *part_die
17859 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
17860
17861 /* We'll save this DIE so link it in. */
17862 part_die->die_parent = parent_die;
17863 part_die->die_sibling = NULL;
17864 part_die->die_child = NULL;
17865
17866 if (last_die && last_die == parent_die)
17867 last_die->die_child = part_die;
17868 else if (last_die)
17869 last_die->die_sibling = part_die;
17870
17871 last_die = part_die;
17872
17873 if (first_die == NULL)
17874 first_die = part_die;
17875
17876 /* Maybe add the DIE to the hash table. Not all DIEs that we
17877 find interesting need to be in the hash table, because we
17878 also have the parent/sibling/child chains; only those that we
17879 might refer to by offset later during partial symbol reading.
17880
17881 For now this means things that might have be the target of a
17882 DW_AT_specification, DW_AT_abstract_origin, or
17883 DW_AT_extension. DW_AT_extension will refer only to
17884 namespaces; DW_AT_abstract_origin refers to functions (and
17885 many things under the function DIE, but we do not recurse
17886 into function DIEs during partial symbol reading) and
17887 possibly variables as well; DW_AT_specification refers to
17888 declarations. Declarations ought to have the DW_AT_declaration
17889 flag. It happens that GCC forgets to put it in sometimes, but
17890 only for functions, not for types.
17891
17892 Adding more things than necessary to the hash table is harmless
17893 except for the performance cost. Adding too few will result in
17894 wasted time in find_partial_die, when we reread the compilation
17895 unit with load_all_dies set. */
17896
17897 if (load_all
17898 || abbrev->tag == DW_TAG_constant
17899 || abbrev->tag == DW_TAG_subprogram
17900 || abbrev->tag == DW_TAG_variable
17901 || abbrev->tag == DW_TAG_namespace
17902 || part_die->is_declaration)
17903 {
17904 void **slot;
17905
17906 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17907 to_underlying (part_die->sect_off),
17908 INSERT);
17909 *slot = part_die;
17910 }
17911
17912 /* For some DIEs we want to follow their children (if any). For C
17913 we have no reason to follow the children of structures; for other
17914 languages we have to, so that we can get at method physnames
17915 to infer fully qualified class names, for DW_AT_specification,
17916 and for C++ template arguments. For C++, we also look one level
17917 inside functions to find template arguments (if the name of the
17918 function does not already contain the template arguments).
17919
17920 For Ada and Fortran, we need to scan the children of subprograms
17921 and lexical blocks as well because these languages allow the
17922 definition of nested entities that could be interesting for the
17923 debugger, such as nested subprograms for instance. */
17924 if (last_die->has_children
17925 && (load_all
17926 || last_die->tag == DW_TAG_namespace
17927 || last_die->tag == DW_TAG_module
17928 || last_die->tag == DW_TAG_enumeration_type
17929 || (cu->language == language_cplus
17930 && last_die->tag == DW_TAG_subprogram
17931 && (last_die->name == NULL
17932 || strchr (last_die->name, '<') == NULL))
17933 || (cu->language != language_c
17934 && (last_die->tag == DW_TAG_class_type
17935 || last_die->tag == DW_TAG_interface_type
17936 || last_die->tag == DW_TAG_structure_type
17937 || last_die->tag == DW_TAG_union_type))
17938 || ((cu->language == language_ada
17939 || cu->language == language_fortran)
17940 && (last_die->tag == DW_TAG_subprogram
17941 || last_die->tag == DW_TAG_lexical_block))))
17942 {
17943 nesting_level++;
17944 parent_die = last_die;
17945 continue;
17946 }
17947
17948 /* Otherwise we skip to the next sibling, if any. */
17949 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17950
17951 /* Back to the top, do it again. */
17952 }
17953 }
17954
17955 partial_die_info::partial_die_info (sect_offset sect_off_,
17956 struct abbrev_info *abbrev)
17957 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17958 {
17959 }
17960
17961 /* Read a minimal amount of information into the minimal die structure.
17962 INFO_PTR should point just after the initial uleb128 of a DIE. */
17963
17964 const gdb_byte *
17965 partial_die_info::read (const struct die_reader_specs *reader,
17966 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
17967 {
17968 struct dwarf2_cu *cu = reader->cu;
17969 struct dwarf2_per_objfile *dwarf2_per_objfile
17970 = cu->per_cu->dwarf2_per_objfile;
17971 unsigned int i;
17972 int has_low_pc_attr = 0;
17973 int has_high_pc_attr = 0;
17974 int high_pc_relative = 0;
17975
17976 for (i = 0; i < abbrev.num_attrs; ++i)
17977 {
17978 attribute attr;
17979 bool need_reprocess;
17980 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
17981 info_ptr, &need_reprocess);
17982 /* String and address offsets that need to do the reprocessing have
17983 already been read at this point, so there is no need to wait until
17984 the loop terminates to do the reprocessing. */
17985 if (need_reprocess)
17986 read_attribute_reprocess (reader, &attr);
17987 /* Store the data if it is of an attribute we want to keep in a
17988 partial symbol table. */
17989 switch (attr.name)
17990 {
17991 case DW_AT_name:
17992 switch (tag)
17993 {
17994 case DW_TAG_compile_unit:
17995 case DW_TAG_partial_unit:
17996 case DW_TAG_type_unit:
17997 /* Compilation units have a DW_AT_name that is a filename, not
17998 a source language identifier. */
17999 case DW_TAG_enumeration_type:
18000 case DW_TAG_enumerator:
18001 /* These tags always have simple identifiers already; no need
18002 to canonicalize them. */
18003 name = DW_STRING (&attr);
18004 break;
18005 default:
18006 {
18007 struct objfile *objfile = dwarf2_per_objfile->objfile;
18008
18009 name
18010 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18011 }
18012 break;
18013 }
18014 break;
18015 case DW_AT_linkage_name:
18016 case DW_AT_MIPS_linkage_name:
18017 /* Note that both forms of linkage name might appear. We
18018 assume they will be the same, and we only store the last
18019 one we see. */
18020 linkage_name = DW_STRING (&attr);
18021 break;
18022 case DW_AT_low_pc:
18023 has_low_pc_attr = 1;
18024 lowpc = attr.value_as_address ();
18025 break;
18026 case DW_AT_high_pc:
18027 has_high_pc_attr = 1;
18028 highpc = attr.value_as_address ();
18029 if (cu->header.version >= 4 && attr.form_is_constant ())
18030 high_pc_relative = 1;
18031 break;
18032 case DW_AT_location:
18033 /* Support the .debug_loc offsets. */
18034 if (attr.form_is_block ())
18035 {
18036 d.locdesc = DW_BLOCK (&attr);
18037 }
18038 else if (attr.form_is_section_offset ())
18039 {
18040 dwarf2_complex_location_expr_complaint ();
18041 }
18042 else
18043 {
18044 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18045 "partial symbol information");
18046 }
18047 break;
18048 case DW_AT_external:
18049 is_external = DW_UNSND (&attr);
18050 break;
18051 case DW_AT_declaration:
18052 is_declaration = DW_UNSND (&attr);
18053 break;
18054 case DW_AT_type:
18055 has_type = 1;
18056 break;
18057 case DW_AT_abstract_origin:
18058 case DW_AT_specification:
18059 case DW_AT_extension:
18060 has_specification = 1;
18061 spec_offset = attr.get_ref_die_offset ();
18062 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18063 || cu->per_cu->is_dwz);
18064 break;
18065 case DW_AT_sibling:
18066 /* Ignore absolute siblings, they might point outside of
18067 the current compile unit. */
18068 if (attr.form == DW_FORM_ref_addr)
18069 complaint (_("ignoring absolute DW_AT_sibling"));
18070 else
18071 {
18072 const gdb_byte *buffer = reader->buffer;
18073 sect_offset off = attr.get_ref_die_offset ();
18074 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18075
18076 if (sibling_ptr < info_ptr)
18077 complaint (_("DW_AT_sibling points backwards"));
18078 else if (sibling_ptr > reader->buffer_end)
18079 reader->die_section->overflow_complaint ();
18080 else
18081 sibling = sibling_ptr;
18082 }
18083 break;
18084 case DW_AT_byte_size:
18085 has_byte_size = 1;
18086 break;
18087 case DW_AT_const_value:
18088 has_const_value = 1;
18089 break;
18090 case DW_AT_calling_convention:
18091 /* DWARF doesn't provide a way to identify a program's source-level
18092 entry point. DW_AT_calling_convention attributes are only meant
18093 to describe functions' calling conventions.
18094
18095 However, because it's a necessary piece of information in
18096 Fortran, and before DWARF 4 DW_CC_program was the only
18097 piece of debugging information whose definition refers to
18098 a 'main program' at all, several compilers marked Fortran
18099 main programs with DW_CC_program --- even when those
18100 functions use the standard calling conventions.
18101
18102 Although DWARF now specifies a way to provide this
18103 information, we support this practice for backward
18104 compatibility. */
18105 if (DW_UNSND (&attr) == DW_CC_program
18106 && cu->language == language_fortran)
18107 main_subprogram = 1;
18108 break;
18109 case DW_AT_inline:
18110 if (DW_UNSND (&attr) == DW_INL_inlined
18111 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18112 may_be_inlined = 1;
18113 break;
18114
18115 case DW_AT_import:
18116 if (tag == DW_TAG_imported_unit)
18117 {
18118 d.sect_off = attr.get_ref_die_offset ();
18119 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18120 || cu->per_cu->is_dwz);
18121 }
18122 break;
18123
18124 case DW_AT_main_subprogram:
18125 main_subprogram = DW_UNSND (&attr);
18126 break;
18127
18128 case DW_AT_ranges:
18129 {
18130 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18131 but that requires a full DIE, so instead we just
18132 reimplement it. */
18133 int need_ranges_base = tag != DW_TAG_compile_unit;
18134 unsigned int ranges_offset = (DW_UNSND (&attr)
18135 + (need_ranges_base
18136 ? cu->ranges_base
18137 : 0));
18138
18139 /* Value of the DW_AT_ranges attribute is the offset in the
18140 .debug_ranges section. */
18141 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18142 nullptr))
18143 has_pc_info = 1;
18144 }
18145 break;
18146
18147 default:
18148 break;
18149 }
18150 }
18151
18152 /* For Ada, if both the name and the linkage name appear, we prefer
18153 the latter. This lets "catch exception" work better, regardless
18154 of the order in which the name and linkage name were emitted.
18155 Really, though, this is just a workaround for the fact that gdb
18156 doesn't store both the name and the linkage name. */
18157 if (cu->language == language_ada && linkage_name != nullptr)
18158 name = linkage_name;
18159
18160 if (high_pc_relative)
18161 highpc += lowpc;
18162
18163 if (has_low_pc_attr && has_high_pc_attr)
18164 {
18165 /* When using the GNU linker, .gnu.linkonce. sections are used to
18166 eliminate duplicate copies of functions and vtables and such.
18167 The linker will arbitrarily choose one and discard the others.
18168 The AT_*_pc values for such functions refer to local labels in
18169 these sections. If the section from that file was discarded, the
18170 labels are not in the output, so the relocs get a value of 0.
18171 If this is a discarded function, mark the pc bounds as invalid,
18172 so that GDB will ignore it. */
18173 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18174 {
18175 struct objfile *objfile = dwarf2_per_objfile->objfile;
18176 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18177
18178 complaint (_("DW_AT_low_pc %s is zero "
18179 "for DIE at %s [in module %s]"),
18180 paddress (gdbarch, lowpc),
18181 sect_offset_str (sect_off),
18182 objfile_name (objfile));
18183 }
18184 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18185 else if (lowpc >= highpc)
18186 {
18187 struct objfile *objfile = dwarf2_per_objfile->objfile;
18188 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18189
18190 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18191 "for DIE at %s [in module %s]"),
18192 paddress (gdbarch, lowpc),
18193 paddress (gdbarch, highpc),
18194 sect_offset_str (sect_off),
18195 objfile_name (objfile));
18196 }
18197 else
18198 has_pc_info = 1;
18199 }
18200
18201 return info_ptr;
18202 }
18203
18204 /* Find a cached partial DIE at OFFSET in CU. */
18205
18206 struct partial_die_info *
18207 dwarf2_cu::find_partial_die (sect_offset sect_off)
18208 {
18209 struct partial_die_info *lookup_die = NULL;
18210 struct partial_die_info part_die (sect_off);
18211
18212 lookup_die = ((struct partial_die_info *)
18213 htab_find_with_hash (partial_dies, &part_die,
18214 to_underlying (sect_off)));
18215
18216 return lookup_die;
18217 }
18218
18219 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18220 except in the case of .debug_types DIEs which do not reference
18221 outside their CU (they do however referencing other types via
18222 DW_FORM_ref_sig8). */
18223
18224 static const struct cu_partial_die_info
18225 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18226 {
18227 struct dwarf2_per_objfile *dwarf2_per_objfile
18228 = cu->per_cu->dwarf2_per_objfile;
18229 struct objfile *objfile = dwarf2_per_objfile->objfile;
18230 struct dwarf2_per_cu_data *per_cu = NULL;
18231 struct partial_die_info *pd = NULL;
18232
18233 if (offset_in_dwz == cu->per_cu->is_dwz
18234 && cu->header.offset_in_cu_p (sect_off))
18235 {
18236 pd = cu->find_partial_die (sect_off);
18237 if (pd != NULL)
18238 return { cu, pd };
18239 /* We missed recording what we needed.
18240 Load all dies and try again. */
18241 per_cu = cu->per_cu;
18242 }
18243 else
18244 {
18245 /* TUs don't reference other CUs/TUs (except via type signatures). */
18246 if (cu->per_cu->is_debug_types)
18247 {
18248 error (_("Dwarf Error: Type Unit at offset %s contains"
18249 " external reference to offset %s [in module %s].\n"),
18250 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18251 bfd_get_filename (objfile->obfd));
18252 }
18253 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18254 dwarf2_per_objfile);
18255
18256 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18257 load_partial_comp_unit (per_cu);
18258
18259 per_cu->cu->last_used = 0;
18260 pd = per_cu->cu->find_partial_die (sect_off);
18261 }
18262
18263 /* If we didn't find it, and not all dies have been loaded,
18264 load them all and try again. */
18265
18266 if (pd == NULL && per_cu->load_all_dies == 0)
18267 {
18268 per_cu->load_all_dies = 1;
18269
18270 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18271 THIS_CU->cu may already be in use. So we can't just free it and
18272 replace its DIEs with the ones we read in. Instead, we leave those
18273 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18274 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18275 set. */
18276 load_partial_comp_unit (per_cu);
18277
18278 pd = per_cu->cu->find_partial_die (sect_off);
18279 }
18280
18281 if (pd == NULL)
18282 internal_error (__FILE__, __LINE__,
18283 _("could not find partial DIE %s "
18284 "in cache [from module %s]\n"),
18285 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18286 return { per_cu->cu, pd };
18287 }
18288
18289 /* See if we can figure out if the class lives in a namespace. We do
18290 this by looking for a member function; its demangled name will
18291 contain namespace info, if there is any. */
18292
18293 static void
18294 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18295 struct dwarf2_cu *cu)
18296 {
18297 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18298 what template types look like, because the demangler
18299 frequently doesn't give the same name as the debug info. We
18300 could fix this by only using the demangled name to get the
18301 prefix (but see comment in read_structure_type). */
18302
18303 struct partial_die_info *real_pdi;
18304 struct partial_die_info *child_pdi;
18305
18306 /* If this DIE (this DIE's specification, if any) has a parent, then
18307 we should not do this. We'll prepend the parent's fully qualified
18308 name when we create the partial symbol. */
18309
18310 real_pdi = struct_pdi;
18311 while (real_pdi->has_specification)
18312 {
18313 auto res = find_partial_die (real_pdi->spec_offset,
18314 real_pdi->spec_is_dwz, cu);
18315 real_pdi = res.pdi;
18316 cu = res.cu;
18317 }
18318
18319 if (real_pdi->die_parent != NULL)
18320 return;
18321
18322 for (child_pdi = struct_pdi->die_child;
18323 child_pdi != NULL;
18324 child_pdi = child_pdi->die_sibling)
18325 {
18326 if (child_pdi->tag == DW_TAG_subprogram
18327 && child_pdi->linkage_name != NULL)
18328 {
18329 gdb::unique_xmalloc_ptr<char> actual_class_name
18330 (language_class_name_from_physname (cu->language_defn,
18331 child_pdi->linkage_name));
18332 if (actual_class_name != NULL)
18333 {
18334 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18335 struct_pdi->name = objfile->intern (actual_class_name.get ());
18336 }
18337 break;
18338 }
18339 }
18340 }
18341
18342 /* Return true if a DIE with TAG may have the DW_AT_const_value
18343 attribute. */
18344
18345 static bool
18346 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18347 {
18348 switch (tag)
18349 {
18350 case DW_TAG_constant:
18351 case DW_TAG_enumerator:
18352 case DW_TAG_formal_parameter:
18353 case DW_TAG_template_value_param:
18354 case DW_TAG_variable:
18355 return true;
18356 }
18357
18358 return false;
18359 }
18360
18361 void
18362 partial_die_info::fixup (struct dwarf2_cu *cu)
18363 {
18364 /* Once we've fixed up a die, there's no point in doing so again.
18365 This also avoids a memory leak if we were to call
18366 guess_partial_die_structure_name multiple times. */
18367 if (fixup_called)
18368 return;
18369
18370 /* If we found a reference attribute and the DIE has no name, try
18371 to find a name in the referred to DIE. */
18372
18373 if (name == NULL && has_specification)
18374 {
18375 struct partial_die_info *spec_die;
18376
18377 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18378 spec_die = res.pdi;
18379 cu = res.cu;
18380
18381 spec_die->fixup (cu);
18382
18383 if (spec_die->name)
18384 {
18385 name = spec_die->name;
18386
18387 /* Copy DW_AT_external attribute if it is set. */
18388 if (spec_die->is_external)
18389 is_external = spec_die->is_external;
18390 }
18391 }
18392
18393 if (!has_const_value && has_specification
18394 && can_have_DW_AT_const_value_p (tag))
18395 {
18396 struct partial_die_info *spec_die;
18397
18398 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18399 spec_die = res.pdi;
18400 cu = res.cu;
18401
18402 spec_die->fixup (cu);
18403
18404 if (spec_die->has_const_value)
18405 {
18406 /* Copy DW_AT_const_value attribute if it is set. */
18407 has_const_value = spec_die->has_const_value;
18408 }
18409 }
18410
18411 /* Set default names for some unnamed DIEs. */
18412
18413 if (name == NULL && tag == DW_TAG_namespace)
18414 name = CP_ANONYMOUS_NAMESPACE_STR;
18415
18416 /* If there is no parent die to provide a namespace, and there are
18417 children, see if we can determine the namespace from their linkage
18418 name. */
18419 if (cu->language == language_cplus
18420 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18421 && die_parent == NULL
18422 && has_children
18423 && (tag == DW_TAG_class_type
18424 || tag == DW_TAG_structure_type
18425 || tag == DW_TAG_union_type))
18426 guess_partial_die_structure_name (this, cu);
18427
18428 /* GCC might emit a nameless struct or union that has a linkage
18429 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18430 if (name == NULL
18431 && (tag == DW_TAG_class_type
18432 || tag == DW_TAG_interface_type
18433 || tag == DW_TAG_structure_type
18434 || tag == DW_TAG_union_type)
18435 && linkage_name != NULL)
18436 {
18437 gdb::unique_xmalloc_ptr<char> demangled
18438 (gdb_demangle (linkage_name, DMGL_TYPES));
18439 if (demangled != nullptr)
18440 {
18441 const char *base;
18442
18443 /* Strip any leading namespaces/classes, keep only the base name.
18444 DW_AT_name for named DIEs does not contain the prefixes. */
18445 base = strrchr (demangled.get (), ':');
18446 if (base && base > demangled.get () && base[-1] == ':')
18447 base++;
18448 else
18449 base = demangled.get ();
18450
18451 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18452 name = objfile->intern (base);
18453 }
18454 }
18455
18456 fixup_called = 1;
18457 }
18458
18459 /* Read the .debug_loclists header contents from the given SECTION in the
18460 HEADER. */
18461 static void
18462 read_loclist_header (struct loclist_header *header,
18463 struct dwarf2_section_info *section)
18464 {
18465 unsigned int bytes_read;
18466 bfd *abfd = section->get_bfd_owner ();
18467 const gdb_byte *info_ptr = section->buffer;
18468 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18469 info_ptr += bytes_read;
18470 header->version = read_2_bytes (abfd, info_ptr);
18471 info_ptr += 2;
18472 header->addr_size = read_1_byte (abfd, info_ptr);
18473 info_ptr += 1;
18474 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18475 info_ptr += 1;
18476 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18477 }
18478
18479 /* Return the DW_AT_loclists_base value for the CU. */
18480 static ULONGEST
18481 lookup_loclist_base (struct dwarf2_cu *cu)
18482 {
18483 /* For the .dwo unit, the loclist_base points to the first offset following
18484 the header. The header consists of the following entities-
18485 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18486 bit format)
18487 2. version (2 bytes)
18488 3. address size (1 byte)
18489 4. segment selector size (1 byte)
18490 5. offset entry count (4 bytes)
18491 These sizes are derived as per the DWARFv5 standard. */
18492 if (cu->dwo_unit != nullptr)
18493 {
18494 if (cu->header.initial_length_size == 4)
18495 return LOCLIST_HEADER_SIZE32;
18496 return LOCLIST_HEADER_SIZE64;
18497 }
18498 return cu->loclist_base;
18499 }
18500
18501 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18502 array of offsets in the .debug_loclists section. */
18503 static CORE_ADDR
18504 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18505 {
18506 struct dwarf2_per_objfile *dwarf2_per_objfile
18507 = cu->per_cu->dwarf2_per_objfile;
18508 struct objfile *objfile = dwarf2_per_objfile->objfile;
18509 bfd *abfd = objfile->obfd;
18510 ULONGEST loclist_base = lookup_loclist_base (cu);
18511 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18512
18513 section->read (objfile);
18514 if (section->buffer == NULL)
18515 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18516 "section [in module %s]"), objfile_name (objfile));
18517 struct loclist_header header;
18518 read_loclist_header (&header, section);
18519 if (loclist_index >= header.offset_entry_count)
18520 complaint (_("DW_FORM_loclistx pointing outside of "
18521 ".debug_loclists offset array [in module %s]"),
18522 objfile_name (objfile));
18523 if (loclist_base + loclist_index * cu->header.offset_size
18524 >= section->size)
18525 complaint (_("DW_FORM_loclistx pointing outside of "
18526 ".debug_loclists section [in module %s]"),
18527 objfile_name (objfile));
18528 const gdb_byte *info_ptr
18529 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18530
18531 if (cu->header.offset_size == 4)
18532 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18533 else
18534 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18535 }
18536
18537 /* Process the attributes that had to be skipped in the first round. These
18538 attributes are the ones that need str_offsets_base or addr_base attributes.
18539 They could not have been processed in the first round, because at the time
18540 the values of str_offsets_base or addr_base may not have been known. */
18541 static void
18542 read_attribute_reprocess (const struct die_reader_specs *reader,
18543 struct attribute *attr)
18544 {
18545 struct dwarf2_cu *cu = reader->cu;
18546 switch (attr->form)
18547 {
18548 case DW_FORM_addrx:
18549 case DW_FORM_GNU_addr_index:
18550 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18551 break;
18552 case DW_FORM_loclistx:
18553 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18554 break;
18555 case DW_FORM_strx:
18556 case DW_FORM_strx1:
18557 case DW_FORM_strx2:
18558 case DW_FORM_strx3:
18559 case DW_FORM_strx4:
18560 case DW_FORM_GNU_str_index:
18561 {
18562 unsigned int str_index = DW_UNSND (attr);
18563 if (reader->dwo_file != NULL)
18564 {
18565 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18566 DW_STRING_IS_CANONICAL (attr) = 0;
18567 }
18568 else
18569 {
18570 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18571 DW_STRING_IS_CANONICAL (attr) = 0;
18572 }
18573 break;
18574 }
18575 default:
18576 gdb_assert_not_reached (_("Unexpected DWARF form."));
18577 }
18578 }
18579
18580 /* Read an attribute value described by an attribute form. */
18581
18582 static const gdb_byte *
18583 read_attribute_value (const struct die_reader_specs *reader,
18584 struct attribute *attr, unsigned form,
18585 LONGEST implicit_const, const gdb_byte *info_ptr,
18586 bool *need_reprocess)
18587 {
18588 struct dwarf2_cu *cu = reader->cu;
18589 struct dwarf2_per_objfile *dwarf2_per_objfile
18590 = cu->per_cu->dwarf2_per_objfile;
18591 struct objfile *objfile = dwarf2_per_objfile->objfile;
18592 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18593 bfd *abfd = reader->abfd;
18594 struct comp_unit_head *cu_header = &cu->header;
18595 unsigned int bytes_read;
18596 struct dwarf_block *blk;
18597 *need_reprocess = false;
18598
18599 attr->form = (enum dwarf_form) form;
18600 switch (form)
18601 {
18602 case DW_FORM_ref_addr:
18603 if (cu->header.version == 2)
18604 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18605 &bytes_read);
18606 else
18607 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18608 &bytes_read);
18609 info_ptr += bytes_read;
18610 break;
18611 case DW_FORM_GNU_ref_alt:
18612 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18613 info_ptr += bytes_read;
18614 break;
18615 case DW_FORM_addr:
18616 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18617 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18618 info_ptr += bytes_read;
18619 break;
18620 case DW_FORM_block2:
18621 blk = dwarf_alloc_block (cu);
18622 blk->size = read_2_bytes (abfd, info_ptr);
18623 info_ptr += 2;
18624 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18625 info_ptr += blk->size;
18626 DW_BLOCK (attr) = blk;
18627 break;
18628 case DW_FORM_block4:
18629 blk = dwarf_alloc_block (cu);
18630 blk->size = read_4_bytes (abfd, info_ptr);
18631 info_ptr += 4;
18632 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18633 info_ptr += blk->size;
18634 DW_BLOCK (attr) = blk;
18635 break;
18636 case DW_FORM_data2:
18637 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18638 info_ptr += 2;
18639 break;
18640 case DW_FORM_data4:
18641 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18642 info_ptr += 4;
18643 break;
18644 case DW_FORM_data8:
18645 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18646 info_ptr += 8;
18647 break;
18648 case DW_FORM_data16:
18649 blk = dwarf_alloc_block (cu);
18650 blk->size = 16;
18651 blk->data = read_n_bytes (abfd, info_ptr, 16);
18652 info_ptr += 16;
18653 DW_BLOCK (attr) = blk;
18654 break;
18655 case DW_FORM_sec_offset:
18656 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18657 info_ptr += bytes_read;
18658 break;
18659 case DW_FORM_loclistx:
18660 {
18661 *need_reprocess = true;
18662 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18663 info_ptr += bytes_read;
18664 }
18665 break;
18666 case DW_FORM_string:
18667 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18668 DW_STRING_IS_CANONICAL (attr) = 0;
18669 info_ptr += bytes_read;
18670 break;
18671 case DW_FORM_strp:
18672 if (!cu->per_cu->is_dwz)
18673 {
18674 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18675 abfd, info_ptr, cu_header,
18676 &bytes_read);
18677 DW_STRING_IS_CANONICAL (attr) = 0;
18678 info_ptr += bytes_read;
18679 break;
18680 }
18681 /* FALLTHROUGH */
18682 case DW_FORM_line_strp:
18683 if (!cu->per_cu->is_dwz)
18684 {
18685 DW_STRING (attr)
18686 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
18687 &bytes_read);
18688 DW_STRING_IS_CANONICAL (attr) = 0;
18689 info_ptr += bytes_read;
18690 break;
18691 }
18692 /* FALLTHROUGH */
18693 case DW_FORM_GNU_strp_alt:
18694 {
18695 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18696 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18697 &bytes_read);
18698
18699 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
18700 DW_STRING_IS_CANONICAL (attr) = 0;
18701 info_ptr += bytes_read;
18702 }
18703 break;
18704 case DW_FORM_exprloc:
18705 case DW_FORM_block:
18706 blk = dwarf_alloc_block (cu);
18707 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18708 info_ptr += bytes_read;
18709 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18710 info_ptr += blk->size;
18711 DW_BLOCK (attr) = blk;
18712 break;
18713 case DW_FORM_block1:
18714 blk = dwarf_alloc_block (cu);
18715 blk->size = read_1_byte (abfd, info_ptr);
18716 info_ptr += 1;
18717 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18718 info_ptr += blk->size;
18719 DW_BLOCK (attr) = blk;
18720 break;
18721 case DW_FORM_data1:
18722 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18723 info_ptr += 1;
18724 break;
18725 case DW_FORM_flag:
18726 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18727 info_ptr += 1;
18728 break;
18729 case DW_FORM_flag_present:
18730 DW_UNSND (attr) = 1;
18731 break;
18732 case DW_FORM_sdata:
18733 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18734 info_ptr += bytes_read;
18735 break;
18736 case DW_FORM_udata:
18737 case DW_FORM_rnglistx:
18738 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18739 info_ptr += bytes_read;
18740 break;
18741 case DW_FORM_ref1:
18742 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18743 + read_1_byte (abfd, info_ptr));
18744 info_ptr += 1;
18745 break;
18746 case DW_FORM_ref2:
18747 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18748 + read_2_bytes (abfd, info_ptr));
18749 info_ptr += 2;
18750 break;
18751 case DW_FORM_ref4:
18752 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18753 + read_4_bytes (abfd, info_ptr));
18754 info_ptr += 4;
18755 break;
18756 case DW_FORM_ref8:
18757 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18758 + read_8_bytes (abfd, info_ptr));
18759 info_ptr += 8;
18760 break;
18761 case DW_FORM_ref_sig8:
18762 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18763 info_ptr += 8;
18764 break;
18765 case DW_FORM_ref_udata:
18766 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18767 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18768 info_ptr += bytes_read;
18769 break;
18770 case DW_FORM_indirect:
18771 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18772 info_ptr += bytes_read;
18773 if (form == DW_FORM_implicit_const)
18774 {
18775 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18776 info_ptr += bytes_read;
18777 }
18778 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18779 info_ptr, need_reprocess);
18780 break;
18781 case DW_FORM_implicit_const:
18782 DW_SND (attr) = implicit_const;
18783 break;
18784 case DW_FORM_addrx:
18785 case DW_FORM_GNU_addr_index:
18786 *need_reprocess = true;
18787 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18788 info_ptr += bytes_read;
18789 break;
18790 case DW_FORM_strx:
18791 case DW_FORM_strx1:
18792 case DW_FORM_strx2:
18793 case DW_FORM_strx3:
18794 case DW_FORM_strx4:
18795 case DW_FORM_GNU_str_index:
18796 {
18797 ULONGEST str_index;
18798 if (form == DW_FORM_strx1)
18799 {
18800 str_index = read_1_byte (abfd, info_ptr);
18801 info_ptr += 1;
18802 }
18803 else if (form == DW_FORM_strx2)
18804 {
18805 str_index = read_2_bytes (abfd, info_ptr);
18806 info_ptr += 2;
18807 }
18808 else if (form == DW_FORM_strx3)
18809 {
18810 str_index = read_3_bytes (abfd, info_ptr);
18811 info_ptr += 3;
18812 }
18813 else if (form == DW_FORM_strx4)
18814 {
18815 str_index = read_4_bytes (abfd, info_ptr);
18816 info_ptr += 4;
18817 }
18818 else
18819 {
18820 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18821 info_ptr += bytes_read;
18822 }
18823 *need_reprocess = true;
18824 DW_UNSND (attr) = str_index;
18825 }
18826 break;
18827 default:
18828 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
18829 dwarf_form_name (form),
18830 bfd_get_filename (abfd));
18831 }
18832
18833 /* Super hack. */
18834 if (cu->per_cu->is_dwz && attr->form_is_ref ())
18835 attr->form = DW_FORM_GNU_ref_alt;
18836
18837 /* We have seen instances where the compiler tried to emit a byte
18838 size attribute of -1 which ended up being encoded as an unsigned
18839 0xffffffff. Although 0xffffffff is technically a valid size value,
18840 an object of this size seems pretty unlikely so we can relatively
18841 safely treat these cases as if the size attribute was invalid and
18842 treat them as zero by default. */
18843 if (attr->name == DW_AT_byte_size
18844 && form == DW_FORM_data4
18845 && DW_UNSND (attr) >= 0xffffffff)
18846 {
18847 complaint
18848 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
18849 hex_string (DW_UNSND (attr)));
18850 DW_UNSND (attr) = 0;
18851 }
18852
18853 return info_ptr;
18854 }
18855
18856 /* Read an attribute described by an abbreviated attribute. */
18857
18858 static const gdb_byte *
18859 read_attribute (const struct die_reader_specs *reader,
18860 struct attribute *attr, struct attr_abbrev *abbrev,
18861 const gdb_byte *info_ptr, bool *need_reprocess)
18862 {
18863 attr->name = abbrev->name;
18864 return read_attribute_value (reader, attr, abbrev->form,
18865 abbrev->implicit_const, info_ptr,
18866 need_reprocess);
18867 }
18868
18869 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18870
18871 static const char *
18872 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18873 LONGEST str_offset)
18874 {
18875 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
18876 str_offset, "DW_FORM_strp");
18877 }
18878
18879 /* Return pointer to string at .debug_str offset as read from BUF.
18880 BUF is assumed to be in a compilation unit described by CU_HEADER.
18881 Return *BYTES_READ_PTR count of bytes read from BUF. */
18882
18883 static const char *
18884 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18885 const gdb_byte *buf,
18886 const struct comp_unit_head *cu_header,
18887 unsigned int *bytes_read_ptr)
18888 {
18889 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18890
18891 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
18892 }
18893
18894 /* See read.h. */
18895
18896 const char *
18897 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
18898 const struct comp_unit_head *cu_header,
18899 unsigned int *bytes_read_ptr)
18900 {
18901 bfd *abfd = objfile->obfd;
18902 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18903
18904 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
18905 }
18906
18907 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18908 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
18909 ADDR_SIZE is the size of addresses from the CU header. */
18910
18911 static CORE_ADDR
18912 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18913 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18914 int addr_size)
18915 {
18916 struct objfile *objfile = dwarf2_per_objfile->objfile;
18917 bfd *abfd = objfile->obfd;
18918 const gdb_byte *info_ptr;
18919 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
18920
18921 dwarf2_per_objfile->addr.read (objfile);
18922 if (dwarf2_per_objfile->addr.buffer == NULL)
18923 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18924 objfile_name (objfile));
18925 if (addr_base_or_zero + addr_index * addr_size
18926 >= dwarf2_per_objfile->addr.size)
18927 error (_("DW_FORM_addr_index pointing outside of "
18928 ".debug_addr section [in module %s]"),
18929 objfile_name (objfile));
18930 info_ptr = (dwarf2_per_objfile->addr.buffer
18931 + addr_base_or_zero + addr_index * addr_size);
18932 if (addr_size == 4)
18933 return bfd_get_32 (abfd, info_ptr);
18934 else
18935 return bfd_get_64 (abfd, info_ptr);
18936 }
18937
18938 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18939
18940 static CORE_ADDR
18941 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18942 {
18943 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18944 cu->addr_base, cu->header.addr_size);
18945 }
18946
18947 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18948
18949 static CORE_ADDR
18950 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18951 unsigned int *bytes_read)
18952 {
18953 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
18954 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18955
18956 return read_addr_index (cu, addr_index);
18957 }
18958
18959 /* See read.h. */
18960
18961 CORE_ADDR
18962 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
18963 {
18964 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
18965 struct dwarf2_cu *cu = per_cu->cu;
18966 gdb::optional<ULONGEST> addr_base;
18967 int addr_size;
18968
18969 /* We need addr_base and addr_size.
18970 If we don't have PER_CU->cu, we have to get it.
18971 Nasty, but the alternative is storing the needed info in PER_CU,
18972 which at this point doesn't seem justified: it's not clear how frequently
18973 it would get used and it would increase the size of every PER_CU.
18974 Entry points like dwarf2_per_cu_addr_size do a similar thing
18975 so we're not in uncharted territory here.
18976 Alas we need to be a bit more complicated as addr_base is contained
18977 in the DIE.
18978
18979 We don't need to read the entire CU(/TU).
18980 We just need the header and top level die.
18981
18982 IWBN to use the aging mechanism to let us lazily later discard the CU.
18983 For now we skip this optimization. */
18984
18985 if (cu != NULL)
18986 {
18987 addr_base = cu->addr_base;
18988 addr_size = cu->header.addr_size;
18989 }
18990 else
18991 {
18992 cutu_reader reader (per_cu, NULL, 0, false);
18993 addr_base = reader.cu->addr_base;
18994 addr_size = reader.cu->header.addr_size;
18995 }
18996
18997 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18998 addr_size);
18999 }
19000
19001 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19002 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19003 DWO file. */
19004
19005 static const char *
19006 read_str_index (struct dwarf2_cu *cu,
19007 struct dwarf2_section_info *str_section,
19008 struct dwarf2_section_info *str_offsets_section,
19009 ULONGEST str_offsets_base, ULONGEST str_index)
19010 {
19011 struct dwarf2_per_objfile *dwarf2_per_objfile
19012 = cu->per_cu->dwarf2_per_objfile;
19013 struct objfile *objfile = dwarf2_per_objfile->objfile;
19014 const char *objf_name = objfile_name (objfile);
19015 bfd *abfd = objfile->obfd;
19016 const gdb_byte *info_ptr;
19017 ULONGEST str_offset;
19018 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19019
19020 str_section->read (objfile);
19021 str_offsets_section->read (objfile);
19022 if (str_section->buffer == NULL)
19023 error (_("%s used without %s section"
19024 " in CU at offset %s [in module %s]"),
19025 form_name, str_section->get_name (),
19026 sect_offset_str (cu->header.sect_off), objf_name);
19027 if (str_offsets_section->buffer == NULL)
19028 error (_("%s used without %s section"
19029 " in CU at offset %s [in module %s]"),
19030 form_name, str_section->get_name (),
19031 sect_offset_str (cu->header.sect_off), objf_name);
19032 info_ptr = (str_offsets_section->buffer
19033 + str_offsets_base
19034 + str_index * cu->header.offset_size);
19035 if (cu->header.offset_size == 4)
19036 str_offset = bfd_get_32 (abfd, info_ptr);
19037 else
19038 str_offset = bfd_get_64 (abfd, info_ptr);
19039 if (str_offset >= str_section->size)
19040 error (_("Offset from %s pointing outside of"
19041 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19042 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19043 return (const char *) (str_section->buffer + str_offset);
19044 }
19045
19046 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19047
19048 static const char *
19049 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19050 {
19051 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19052 ? reader->cu->header.addr_size : 0;
19053 return read_str_index (reader->cu,
19054 &reader->dwo_file->sections.str,
19055 &reader->dwo_file->sections.str_offsets,
19056 str_offsets_base, str_index);
19057 }
19058
19059 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19060
19061 static const char *
19062 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19063 {
19064 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19065 const char *objf_name = objfile_name (objfile);
19066 static const char form_name[] = "DW_FORM_GNU_str_index";
19067 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19068
19069 if (!cu->str_offsets_base.has_value ())
19070 error (_("%s used in Fission stub without %s"
19071 " in CU at offset 0x%lx [in module %s]"),
19072 form_name, str_offsets_attr_name,
19073 (long) cu->header.offset_size, objf_name);
19074
19075 return read_str_index (cu,
19076 &cu->per_cu->dwarf2_per_objfile->str,
19077 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19078 *cu->str_offsets_base, str_index);
19079 }
19080
19081 /* Return the length of an LEB128 number in BUF. */
19082
19083 static int
19084 leb128_size (const gdb_byte *buf)
19085 {
19086 const gdb_byte *begin = buf;
19087 gdb_byte byte;
19088
19089 while (1)
19090 {
19091 byte = *buf++;
19092 if ((byte & 128) == 0)
19093 return buf - begin;
19094 }
19095 }
19096
19097 static void
19098 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19099 {
19100 switch (lang)
19101 {
19102 case DW_LANG_C89:
19103 case DW_LANG_C99:
19104 case DW_LANG_C11:
19105 case DW_LANG_C:
19106 case DW_LANG_UPC:
19107 cu->language = language_c;
19108 break;
19109 case DW_LANG_Java:
19110 case DW_LANG_C_plus_plus:
19111 case DW_LANG_C_plus_plus_11:
19112 case DW_LANG_C_plus_plus_14:
19113 cu->language = language_cplus;
19114 break;
19115 case DW_LANG_D:
19116 cu->language = language_d;
19117 break;
19118 case DW_LANG_Fortran77:
19119 case DW_LANG_Fortran90:
19120 case DW_LANG_Fortran95:
19121 case DW_LANG_Fortran03:
19122 case DW_LANG_Fortran08:
19123 cu->language = language_fortran;
19124 break;
19125 case DW_LANG_Go:
19126 cu->language = language_go;
19127 break;
19128 case DW_LANG_Mips_Assembler:
19129 cu->language = language_asm;
19130 break;
19131 case DW_LANG_Ada83:
19132 case DW_LANG_Ada95:
19133 cu->language = language_ada;
19134 break;
19135 case DW_LANG_Modula2:
19136 cu->language = language_m2;
19137 break;
19138 case DW_LANG_Pascal83:
19139 cu->language = language_pascal;
19140 break;
19141 case DW_LANG_ObjC:
19142 cu->language = language_objc;
19143 break;
19144 case DW_LANG_Rust:
19145 case DW_LANG_Rust_old:
19146 cu->language = language_rust;
19147 break;
19148 case DW_LANG_Cobol74:
19149 case DW_LANG_Cobol85:
19150 default:
19151 cu->language = language_minimal;
19152 break;
19153 }
19154 cu->language_defn = language_def (cu->language);
19155 }
19156
19157 /* Return the named attribute or NULL if not there. */
19158
19159 static struct attribute *
19160 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19161 {
19162 for (;;)
19163 {
19164 unsigned int i;
19165 struct attribute *spec = NULL;
19166
19167 for (i = 0; i < die->num_attrs; ++i)
19168 {
19169 if (die->attrs[i].name == name)
19170 return &die->attrs[i];
19171 if (die->attrs[i].name == DW_AT_specification
19172 || die->attrs[i].name == DW_AT_abstract_origin)
19173 spec = &die->attrs[i];
19174 }
19175
19176 if (!spec)
19177 break;
19178
19179 die = follow_die_ref (die, spec, &cu);
19180 }
19181
19182 return NULL;
19183 }
19184
19185 /* Return the string associated with a string-typed attribute, or NULL if it
19186 is either not found or is of an incorrect type. */
19187
19188 static const char *
19189 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19190 {
19191 struct attribute *attr;
19192 const char *str = NULL;
19193
19194 attr = dwarf2_attr (die, name, cu);
19195
19196 if (attr != NULL)
19197 {
19198 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19199 || attr->form == DW_FORM_string
19200 || attr->form == DW_FORM_strx
19201 || attr->form == DW_FORM_strx1
19202 || attr->form == DW_FORM_strx2
19203 || attr->form == DW_FORM_strx3
19204 || attr->form == DW_FORM_strx4
19205 || attr->form == DW_FORM_GNU_str_index
19206 || attr->form == DW_FORM_GNU_strp_alt)
19207 str = DW_STRING (attr);
19208 else
19209 complaint (_("string type expected for attribute %s for "
19210 "DIE at %s in module %s"),
19211 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19212 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19213 }
19214
19215 return str;
19216 }
19217
19218 /* Return the dwo name or NULL if not present. If present, it is in either
19219 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19220 static const char *
19221 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19222 {
19223 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19224 if (dwo_name == nullptr)
19225 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19226 return dwo_name;
19227 }
19228
19229 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19230 and holds a non-zero value. This function should only be used for
19231 DW_FORM_flag or DW_FORM_flag_present attributes. */
19232
19233 static int
19234 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19235 {
19236 struct attribute *attr = dwarf2_attr (die, name, cu);
19237
19238 return (attr && DW_UNSND (attr));
19239 }
19240
19241 static int
19242 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19243 {
19244 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19245 which value is non-zero. However, we have to be careful with
19246 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19247 (via dwarf2_flag_true_p) follows this attribute. So we may
19248 end up accidently finding a declaration attribute that belongs
19249 to a different DIE referenced by the specification attribute,
19250 even though the given DIE does not have a declaration attribute. */
19251 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19252 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19253 }
19254
19255 /* Return the die giving the specification for DIE, if there is
19256 one. *SPEC_CU is the CU containing DIE on input, and the CU
19257 containing the return value on output. If there is no
19258 specification, but there is an abstract origin, that is
19259 returned. */
19260
19261 static struct die_info *
19262 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19263 {
19264 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19265 *spec_cu);
19266
19267 if (spec_attr == NULL)
19268 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19269
19270 if (spec_attr == NULL)
19271 return NULL;
19272 else
19273 return follow_die_ref (die, spec_attr, spec_cu);
19274 }
19275
19276 /* Stub for free_line_header to match void * callback types. */
19277
19278 static void
19279 free_line_header_voidp (void *arg)
19280 {
19281 struct line_header *lh = (struct line_header *) arg;
19282
19283 delete lh;
19284 }
19285
19286 /* A convenience function to find the proper .debug_line section for a CU. */
19287
19288 static struct dwarf2_section_info *
19289 get_debug_line_section (struct dwarf2_cu *cu)
19290 {
19291 struct dwarf2_section_info *section;
19292 struct dwarf2_per_objfile *dwarf2_per_objfile
19293 = cu->per_cu->dwarf2_per_objfile;
19294
19295 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19296 DWO file. */
19297 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19298 section = &cu->dwo_unit->dwo_file->sections.line;
19299 else if (cu->per_cu->is_dwz)
19300 {
19301 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19302
19303 section = &dwz->line;
19304 }
19305 else
19306 section = &dwarf2_per_objfile->line;
19307
19308 return section;
19309 }
19310
19311 /* Read the statement program header starting at OFFSET in
19312 .debug_line, or .debug_line.dwo. Return a pointer
19313 to a struct line_header, allocated using xmalloc.
19314 Returns NULL if there is a problem reading the header, e.g., if it
19315 has a version we don't understand.
19316
19317 NOTE: the strings in the include directory and file name tables of
19318 the returned object point into the dwarf line section buffer,
19319 and must not be freed. */
19320
19321 static line_header_up
19322 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19323 {
19324 struct dwarf2_section_info *section;
19325 struct dwarf2_per_objfile *dwarf2_per_objfile
19326 = cu->per_cu->dwarf2_per_objfile;
19327
19328 section = get_debug_line_section (cu);
19329 section->read (dwarf2_per_objfile->objfile);
19330 if (section->buffer == NULL)
19331 {
19332 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19333 complaint (_("missing .debug_line.dwo section"));
19334 else
19335 complaint (_("missing .debug_line section"));
19336 return 0;
19337 }
19338
19339 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19340 dwarf2_per_objfile, section,
19341 &cu->header);
19342 }
19343
19344 /* Subroutine of dwarf_decode_lines to simplify it.
19345 Return the file name of the psymtab for the given file_entry.
19346 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19347 If space for the result is malloc'd, *NAME_HOLDER will be set.
19348 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19349
19350 static const char *
19351 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19352 const dwarf2_psymtab *pst,
19353 const char *comp_dir,
19354 gdb::unique_xmalloc_ptr<char> *name_holder)
19355 {
19356 const char *include_name = fe.name;
19357 const char *include_name_to_compare = include_name;
19358 const char *pst_filename;
19359 int file_is_pst;
19360
19361 const char *dir_name = fe.include_dir (lh);
19362
19363 gdb::unique_xmalloc_ptr<char> hold_compare;
19364 if (!IS_ABSOLUTE_PATH (include_name)
19365 && (dir_name != NULL || comp_dir != NULL))
19366 {
19367 /* Avoid creating a duplicate psymtab for PST.
19368 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19369 Before we do the comparison, however, we need to account
19370 for DIR_NAME and COMP_DIR.
19371 First prepend dir_name (if non-NULL). If we still don't
19372 have an absolute path prepend comp_dir (if non-NULL).
19373 However, the directory we record in the include-file's
19374 psymtab does not contain COMP_DIR (to match the
19375 corresponding symtab(s)).
19376
19377 Example:
19378
19379 bash$ cd /tmp
19380 bash$ gcc -g ./hello.c
19381 include_name = "hello.c"
19382 dir_name = "."
19383 DW_AT_comp_dir = comp_dir = "/tmp"
19384 DW_AT_name = "./hello.c"
19385
19386 */
19387
19388 if (dir_name != NULL)
19389 {
19390 name_holder->reset (concat (dir_name, SLASH_STRING,
19391 include_name, (char *) NULL));
19392 include_name = name_holder->get ();
19393 include_name_to_compare = include_name;
19394 }
19395 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19396 {
19397 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19398 include_name, (char *) NULL));
19399 include_name_to_compare = hold_compare.get ();
19400 }
19401 }
19402
19403 pst_filename = pst->filename;
19404 gdb::unique_xmalloc_ptr<char> copied_name;
19405 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19406 {
19407 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19408 pst_filename, (char *) NULL));
19409 pst_filename = copied_name.get ();
19410 }
19411
19412 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19413
19414 if (file_is_pst)
19415 return NULL;
19416 return include_name;
19417 }
19418
19419 /* State machine to track the state of the line number program. */
19420
19421 class lnp_state_machine
19422 {
19423 public:
19424 /* Initialize a machine state for the start of a line number
19425 program. */
19426 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19427 bool record_lines_p);
19428
19429 file_entry *current_file ()
19430 {
19431 /* lh->file_names is 0-based, but the file name numbers in the
19432 statement program are 1-based. */
19433 return m_line_header->file_name_at (m_file);
19434 }
19435
19436 /* Record the line in the state machine. END_SEQUENCE is true if
19437 we're processing the end of a sequence. */
19438 void record_line (bool end_sequence);
19439
19440 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19441 nop-out rest of the lines in this sequence. */
19442 void check_line_address (struct dwarf2_cu *cu,
19443 const gdb_byte *line_ptr,
19444 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19445
19446 void handle_set_discriminator (unsigned int discriminator)
19447 {
19448 m_discriminator = discriminator;
19449 m_line_has_non_zero_discriminator |= discriminator != 0;
19450 }
19451
19452 /* Handle DW_LNE_set_address. */
19453 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19454 {
19455 m_op_index = 0;
19456 address += baseaddr;
19457 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19458 }
19459
19460 /* Handle DW_LNS_advance_pc. */
19461 void handle_advance_pc (CORE_ADDR adjust);
19462
19463 /* Handle a special opcode. */
19464 void handle_special_opcode (unsigned char op_code);
19465
19466 /* Handle DW_LNS_advance_line. */
19467 void handle_advance_line (int line_delta)
19468 {
19469 advance_line (line_delta);
19470 }
19471
19472 /* Handle DW_LNS_set_file. */
19473 void handle_set_file (file_name_index file);
19474
19475 /* Handle DW_LNS_negate_stmt. */
19476 void handle_negate_stmt ()
19477 {
19478 m_is_stmt = !m_is_stmt;
19479 }
19480
19481 /* Handle DW_LNS_const_add_pc. */
19482 void handle_const_add_pc ();
19483
19484 /* Handle DW_LNS_fixed_advance_pc. */
19485 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19486 {
19487 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19488 m_op_index = 0;
19489 }
19490
19491 /* Handle DW_LNS_copy. */
19492 void handle_copy ()
19493 {
19494 record_line (false);
19495 m_discriminator = 0;
19496 }
19497
19498 /* Handle DW_LNE_end_sequence. */
19499 void handle_end_sequence ()
19500 {
19501 m_currently_recording_lines = true;
19502 }
19503
19504 private:
19505 /* Advance the line by LINE_DELTA. */
19506 void advance_line (int line_delta)
19507 {
19508 m_line += line_delta;
19509
19510 if (line_delta != 0)
19511 m_line_has_non_zero_discriminator = m_discriminator != 0;
19512 }
19513
19514 struct dwarf2_cu *m_cu;
19515
19516 gdbarch *m_gdbarch;
19517
19518 /* True if we're recording lines.
19519 Otherwise we're building partial symtabs and are just interested in
19520 finding include files mentioned by the line number program. */
19521 bool m_record_lines_p;
19522
19523 /* The line number header. */
19524 line_header *m_line_header;
19525
19526 /* These are part of the standard DWARF line number state machine,
19527 and initialized according to the DWARF spec. */
19528
19529 unsigned char m_op_index = 0;
19530 /* The line table index of the current file. */
19531 file_name_index m_file = 1;
19532 unsigned int m_line = 1;
19533
19534 /* These are initialized in the constructor. */
19535
19536 CORE_ADDR m_address;
19537 bool m_is_stmt;
19538 unsigned int m_discriminator;
19539
19540 /* Additional bits of state we need to track. */
19541
19542 /* The last file that we called dwarf2_start_subfile for.
19543 This is only used for TLLs. */
19544 unsigned int m_last_file = 0;
19545 /* The last file a line number was recorded for. */
19546 struct subfile *m_last_subfile = NULL;
19547
19548 /* When true, record the lines we decode. */
19549 bool m_currently_recording_lines = false;
19550
19551 /* The last line number that was recorded, used to coalesce
19552 consecutive entries for the same line. This can happen, for
19553 example, when discriminators are present. PR 17276. */
19554 unsigned int m_last_line = 0;
19555 bool m_line_has_non_zero_discriminator = false;
19556 };
19557
19558 void
19559 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19560 {
19561 CORE_ADDR addr_adj = (((m_op_index + adjust)
19562 / m_line_header->maximum_ops_per_instruction)
19563 * m_line_header->minimum_instruction_length);
19564 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19565 m_op_index = ((m_op_index + adjust)
19566 % m_line_header->maximum_ops_per_instruction);
19567 }
19568
19569 void
19570 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19571 {
19572 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19573 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19574 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19575 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19576 / m_line_header->maximum_ops_per_instruction)
19577 * m_line_header->minimum_instruction_length);
19578 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19579 m_op_index = ((m_op_index + adj_opcode_d)
19580 % m_line_header->maximum_ops_per_instruction);
19581
19582 int line_delta = m_line_header->line_base + adj_opcode_r;
19583 advance_line (line_delta);
19584 record_line (false);
19585 m_discriminator = 0;
19586 }
19587
19588 void
19589 lnp_state_machine::handle_set_file (file_name_index file)
19590 {
19591 m_file = file;
19592
19593 const file_entry *fe = current_file ();
19594 if (fe == NULL)
19595 dwarf2_debug_line_missing_file_complaint ();
19596 else if (m_record_lines_p)
19597 {
19598 const char *dir = fe->include_dir (m_line_header);
19599
19600 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19601 m_line_has_non_zero_discriminator = m_discriminator != 0;
19602 dwarf2_start_subfile (m_cu, fe->name, dir);
19603 }
19604 }
19605
19606 void
19607 lnp_state_machine::handle_const_add_pc ()
19608 {
19609 CORE_ADDR adjust
19610 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19611
19612 CORE_ADDR addr_adj
19613 = (((m_op_index + adjust)
19614 / m_line_header->maximum_ops_per_instruction)
19615 * m_line_header->minimum_instruction_length);
19616
19617 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19618 m_op_index = ((m_op_index + adjust)
19619 % m_line_header->maximum_ops_per_instruction);
19620 }
19621
19622 /* Return non-zero if we should add LINE to the line number table.
19623 LINE is the line to add, LAST_LINE is the last line that was added,
19624 LAST_SUBFILE is the subfile for LAST_LINE.
19625 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19626 had a non-zero discriminator.
19627
19628 We have to be careful in the presence of discriminators.
19629 E.g., for this line:
19630
19631 for (i = 0; i < 100000; i++);
19632
19633 clang can emit four line number entries for that one line,
19634 each with a different discriminator.
19635 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19636
19637 However, we want gdb to coalesce all four entries into one.
19638 Otherwise the user could stepi into the middle of the line and
19639 gdb would get confused about whether the pc really was in the
19640 middle of the line.
19641
19642 Things are further complicated by the fact that two consecutive
19643 line number entries for the same line is a heuristic used by gcc
19644 to denote the end of the prologue. So we can't just discard duplicate
19645 entries, we have to be selective about it. The heuristic we use is
19646 that we only collapse consecutive entries for the same line if at least
19647 one of those entries has a non-zero discriminator. PR 17276.
19648
19649 Note: Addresses in the line number state machine can never go backwards
19650 within one sequence, thus this coalescing is ok. */
19651
19652 static int
19653 dwarf_record_line_p (struct dwarf2_cu *cu,
19654 unsigned int line, unsigned int last_line,
19655 int line_has_non_zero_discriminator,
19656 struct subfile *last_subfile)
19657 {
19658 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19659 return 1;
19660 if (line != last_line)
19661 return 1;
19662 /* Same line for the same file that we've seen already.
19663 As a last check, for pr 17276, only record the line if the line
19664 has never had a non-zero discriminator. */
19665 if (!line_has_non_zero_discriminator)
19666 return 1;
19667 return 0;
19668 }
19669
19670 /* Use the CU's builder to record line number LINE beginning at
19671 address ADDRESS in the line table of subfile SUBFILE. */
19672
19673 static void
19674 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19675 unsigned int line, CORE_ADDR address, bool is_stmt,
19676 struct dwarf2_cu *cu)
19677 {
19678 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19679
19680 if (dwarf_line_debug)
19681 {
19682 fprintf_unfiltered (gdb_stdlog,
19683 "Recording line %u, file %s, address %s\n",
19684 line, lbasename (subfile->name),
19685 paddress (gdbarch, address));
19686 }
19687
19688 if (cu != nullptr)
19689 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
19690 }
19691
19692 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19693 Mark the end of a set of line number records.
19694 The arguments are the same as for dwarf_record_line_1.
19695 If SUBFILE is NULL the request is ignored. */
19696
19697 static void
19698 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19699 CORE_ADDR address, struct dwarf2_cu *cu)
19700 {
19701 if (subfile == NULL)
19702 return;
19703
19704 if (dwarf_line_debug)
19705 {
19706 fprintf_unfiltered (gdb_stdlog,
19707 "Finishing current line, file %s, address %s\n",
19708 lbasename (subfile->name),
19709 paddress (gdbarch, address));
19710 }
19711
19712 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
19713 }
19714
19715 void
19716 lnp_state_machine::record_line (bool end_sequence)
19717 {
19718 if (dwarf_line_debug)
19719 {
19720 fprintf_unfiltered (gdb_stdlog,
19721 "Processing actual line %u: file %u,"
19722 " address %s, is_stmt %u, discrim %u%s\n",
19723 m_line, m_file,
19724 paddress (m_gdbarch, m_address),
19725 m_is_stmt, m_discriminator,
19726 (end_sequence ? "\t(end sequence)" : ""));
19727 }
19728
19729 file_entry *fe = current_file ();
19730
19731 if (fe == NULL)
19732 dwarf2_debug_line_missing_file_complaint ();
19733 /* For now we ignore lines not starting on an instruction boundary.
19734 But not when processing end_sequence for compatibility with the
19735 previous version of the code. */
19736 else if (m_op_index == 0 || end_sequence)
19737 {
19738 fe->included_p = 1;
19739 if (m_record_lines_p)
19740 {
19741 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
19742 || end_sequence)
19743 {
19744 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19745 m_currently_recording_lines ? m_cu : nullptr);
19746 }
19747
19748 if (!end_sequence)
19749 {
19750 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
19751
19752 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
19753 m_line_has_non_zero_discriminator,
19754 m_last_subfile))
19755 {
19756 buildsym_compunit *builder = m_cu->get_builder ();
19757 dwarf_record_line_1 (m_gdbarch,
19758 builder->get_current_subfile (),
19759 m_line, m_address, is_stmt,
19760 m_currently_recording_lines ? m_cu : nullptr);
19761 }
19762 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19763 m_last_line = m_line;
19764 }
19765 }
19766 }
19767 }
19768
19769 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
19770 line_header *lh, bool record_lines_p)
19771 {
19772 m_cu = cu;
19773 m_gdbarch = arch;
19774 m_record_lines_p = record_lines_p;
19775 m_line_header = lh;
19776
19777 m_currently_recording_lines = true;
19778
19779 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19780 was a line entry for it so that the backend has a chance to adjust it
19781 and also record it in case it needs it. This is currently used by MIPS
19782 code, cf. `mips_adjust_dwarf2_line'. */
19783 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19784 m_is_stmt = lh->default_is_stmt;
19785 m_discriminator = 0;
19786 }
19787
19788 void
19789 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19790 const gdb_byte *line_ptr,
19791 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
19792 {
19793 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
19794 the pc range of the CU. However, we restrict the test to only ADDRESS
19795 values of zero to preserve GDB's previous behaviour which is to handle
19796 the specific case of a function being GC'd by the linker. */
19797
19798 if (address == 0 && address < unrelocated_lowpc)
19799 {
19800 /* This line table is for a function which has been
19801 GCd by the linker. Ignore it. PR gdb/12528 */
19802
19803 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19804 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19805
19806 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
19807 line_offset, objfile_name (objfile));
19808 m_currently_recording_lines = false;
19809 /* Note: m_currently_recording_lines is left as false until we see
19810 DW_LNE_end_sequence. */
19811 }
19812 }
19813
19814 /* Subroutine of dwarf_decode_lines to simplify it.
19815 Process the line number information in LH.
19816 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19817 program in order to set included_p for every referenced header. */
19818
19819 static void
19820 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19821 const int decode_for_pst_p, CORE_ADDR lowpc)
19822 {
19823 const gdb_byte *line_ptr, *extended_end;
19824 const gdb_byte *line_end;
19825 unsigned int bytes_read, extended_len;
19826 unsigned char op_code, extended_op;
19827 CORE_ADDR baseaddr;
19828 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19829 bfd *abfd = objfile->obfd;
19830 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19831 /* True if we're recording line info (as opposed to building partial
19832 symtabs and just interested in finding include files mentioned by
19833 the line number program). */
19834 bool record_lines_p = !decode_for_pst_p;
19835
19836 baseaddr = objfile->text_section_offset ();
19837
19838 line_ptr = lh->statement_program_start;
19839 line_end = lh->statement_program_end;
19840
19841 /* Read the statement sequences until there's nothing left. */
19842 while (line_ptr < line_end)
19843 {
19844 /* The DWARF line number program state machine. Reset the state
19845 machine at the start of each sequence. */
19846 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
19847 bool end_sequence = false;
19848
19849 if (record_lines_p)
19850 {
19851 /* Start a subfile for the current file of the state
19852 machine. */
19853 const file_entry *fe = state_machine.current_file ();
19854
19855 if (fe != NULL)
19856 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
19857 }
19858
19859 /* Decode the table. */
19860 while (line_ptr < line_end && !end_sequence)
19861 {
19862 op_code = read_1_byte (abfd, line_ptr);
19863 line_ptr += 1;
19864
19865 if (op_code >= lh->opcode_base)
19866 {
19867 /* Special opcode. */
19868 state_machine.handle_special_opcode (op_code);
19869 }
19870 else switch (op_code)
19871 {
19872 case DW_LNS_extended_op:
19873 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19874 &bytes_read);
19875 line_ptr += bytes_read;
19876 extended_end = line_ptr + extended_len;
19877 extended_op = read_1_byte (abfd, line_ptr);
19878 line_ptr += 1;
19879 switch (extended_op)
19880 {
19881 case DW_LNE_end_sequence:
19882 state_machine.handle_end_sequence ();
19883 end_sequence = true;
19884 break;
19885 case DW_LNE_set_address:
19886 {
19887 CORE_ADDR address
19888 = cu->header.read_address (abfd, line_ptr, &bytes_read);
19889 line_ptr += bytes_read;
19890
19891 state_machine.check_line_address (cu, line_ptr,
19892 lowpc - baseaddr, address);
19893 state_machine.handle_set_address (baseaddr, address);
19894 }
19895 break;
19896 case DW_LNE_define_file:
19897 {
19898 const char *cur_file;
19899 unsigned int mod_time, length;
19900 dir_index dindex;
19901
19902 cur_file = read_direct_string (abfd, line_ptr,
19903 &bytes_read);
19904 line_ptr += bytes_read;
19905 dindex = (dir_index)
19906 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19907 line_ptr += bytes_read;
19908 mod_time =
19909 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19910 line_ptr += bytes_read;
19911 length =
19912 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19913 line_ptr += bytes_read;
19914 lh->add_file_name (cur_file, dindex, mod_time, length);
19915 }
19916 break;
19917 case DW_LNE_set_discriminator:
19918 {
19919 /* The discriminator is not interesting to the
19920 debugger; just ignore it. We still need to
19921 check its value though:
19922 if there are consecutive entries for the same
19923 (non-prologue) line we want to coalesce them.
19924 PR 17276. */
19925 unsigned int discr
19926 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19927 line_ptr += bytes_read;
19928
19929 state_machine.handle_set_discriminator (discr);
19930 }
19931 break;
19932 default:
19933 complaint (_("mangled .debug_line section"));
19934 return;
19935 }
19936 /* Make sure that we parsed the extended op correctly. If e.g.
19937 we expected a different address size than the producer used,
19938 we may have read the wrong number of bytes. */
19939 if (line_ptr != extended_end)
19940 {
19941 complaint (_("mangled .debug_line section"));
19942 return;
19943 }
19944 break;
19945 case DW_LNS_copy:
19946 state_machine.handle_copy ();
19947 break;
19948 case DW_LNS_advance_pc:
19949 {
19950 CORE_ADDR adjust
19951 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19952 line_ptr += bytes_read;
19953
19954 state_machine.handle_advance_pc (adjust);
19955 }
19956 break;
19957 case DW_LNS_advance_line:
19958 {
19959 int line_delta
19960 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
19961 line_ptr += bytes_read;
19962
19963 state_machine.handle_advance_line (line_delta);
19964 }
19965 break;
19966 case DW_LNS_set_file:
19967 {
19968 file_name_index file
19969 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19970 &bytes_read);
19971 line_ptr += bytes_read;
19972
19973 state_machine.handle_set_file (file);
19974 }
19975 break;
19976 case DW_LNS_set_column:
19977 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19978 line_ptr += bytes_read;
19979 break;
19980 case DW_LNS_negate_stmt:
19981 state_machine.handle_negate_stmt ();
19982 break;
19983 case DW_LNS_set_basic_block:
19984 break;
19985 /* Add to the address register of the state machine the
19986 address increment value corresponding to special opcode
19987 255. I.e., this value is scaled by the minimum
19988 instruction length since special opcode 255 would have
19989 scaled the increment. */
19990 case DW_LNS_const_add_pc:
19991 state_machine.handle_const_add_pc ();
19992 break;
19993 case DW_LNS_fixed_advance_pc:
19994 {
19995 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
19996 line_ptr += 2;
19997
19998 state_machine.handle_fixed_advance_pc (addr_adj);
19999 }
20000 break;
20001 default:
20002 {
20003 /* Unknown standard opcode, ignore it. */
20004 int i;
20005
20006 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20007 {
20008 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20009 line_ptr += bytes_read;
20010 }
20011 }
20012 }
20013 }
20014
20015 if (!end_sequence)
20016 dwarf2_debug_line_missing_end_sequence_complaint ();
20017
20018 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20019 in which case we still finish recording the last line). */
20020 state_machine.record_line (true);
20021 }
20022 }
20023
20024 /* Decode the Line Number Program (LNP) for the given line_header
20025 structure and CU. The actual information extracted and the type
20026 of structures created from the LNP depends on the value of PST.
20027
20028 1. If PST is NULL, then this procedure uses the data from the program
20029 to create all necessary symbol tables, and their linetables.
20030
20031 2. If PST is not NULL, this procedure reads the program to determine
20032 the list of files included by the unit represented by PST, and
20033 builds all the associated partial symbol tables.
20034
20035 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20036 It is used for relative paths in the line table.
20037 NOTE: When processing partial symtabs (pst != NULL),
20038 comp_dir == pst->dirname.
20039
20040 NOTE: It is important that psymtabs have the same file name (via strcmp)
20041 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20042 symtab we don't use it in the name of the psymtabs we create.
20043 E.g. expand_line_sal requires this when finding psymtabs to expand.
20044 A good testcase for this is mb-inline.exp.
20045
20046 LOWPC is the lowest address in CU (or 0 if not known).
20047
20048 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20049 for its PC<->lines mapping information. Otherwise only the filename
20050 table is read in. */
20051
20052 static void
20053 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20054 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20055 CORE_ADDR lowpc, int decode_mapping)
20056 {
20057 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20058 const int decode_for_pst_p = (pst != NULL);
20059
20060 if (decode_mapping)
20061 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20062
20063 if (decode_for_pst_p)
20064 {
20065 /* Now that we're done scanning the Line Header Program, we can
20066 create the psymtab of each included file. */
20067 for (auto &file_entry : lh->file_names ())
20068 if (file_entry.included_p == 1)
20069 {
20070 gdb::unique_xmalloc_ptr<char> name_holder;
20071 const char *include_name =
20072 psymtab_include_file_name (lh, file_entry, pst,
20073 comp_dir, &name_holder);
20074 if (include_name != NULL)
20075 dwarf2_create_include_psymtab (include_name, pst, objfile);
20076 }
20077 }
20078 else
20079 {
20080 /* Make sure a symtab is created for every file, even files
20081 which contain only variables (i.e. no code with associated
20082 line numbers). */
20083 buildsym_compunit *builder = cu->get_builder ();
20084 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20085
20086 for (auto &fe : lh->file_names ())
20087 {
20088 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20089 if (builder->get_current_subfile ()->symtab == NULL)
20090 {
20091 builder->get_current_subfile ()->symtab
20092 = allocate_symtab (cust,
20093 builder->get_current_subfile ()->name);
20094 }
20095 fe.symtab = builder->get_current_subfile ()->symtab;
20096 }
20097 }
20098 }
20099
20100 /* Start a subfile for DWARF. FILENAME is the name of the file and
20101 DIRNAME the name of the source directory which contains FILENAME
20102 or NULL if not known.
20103 This routine tries to keep line numbers from identical absolute and
20104 relative file names in a common subfile.
20105
20106 Using the `list' example from the GDB testsuite, which resides in
20107 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20108 of /srcdir/list0.c yields the following debugging information for list0.c:
20109
20110 DW_AT_name: /srcdir/list0.c
20111 DW_AT_comp_dir: /compdir
20112 files.files[0].name: list0.h
20113 files.files[0].dir: /srcdir
20114 files.files[1].name: list0.c
20115 files.files[1].dir: /srcdir
20116
20117 The line number information for list0.c has to end up in a single
20118 subfile, so that `break /srcdir/list0.c:1' works as expected.
20119 start_subfile will ensure that this happens provided that we pass the
20120 concatenation of files.files[1].dir and files.files[1].name as the
20121 subfile's name. */
20122
20123 static void
20124 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20125 const char *dirname)
20126 {
20127 gdb::unique_xmalloc_ptr<char> copy;
20128
20129 /* In order not to lose the line information directory,
20130 we concatenate it to the filename when it makes sense.
20131 Note that the Dwarf3 standard says (speaking of filenames in line
20132 information): ``The directory index is ignored for file names
20133 that represent full path names''. Thus ignoring dirname in the
20134 `else' branch below isn't an issue. */
20135
20136 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20137 {
20138 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20139 filename = copy.get ();
20140 }
20141
20142 cu->get_builder ()->start_subfile (filename);
20143 }
20144
20145 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20146 buildsym_compunit constructor. */
20147
20148 struct compunit_symtab *
20149 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20150 CORE_ADDR low_pc)
20151 {
20152 gdb_assert (m_builder == nullptr);
20153
20154 m_builder.reset (new struct buildsym_compunit
20155 (per_cu->dwarf2_per_objfile->objfile,
20156 name, comp_dir, language, low_pc));
20157
20158 list_in_scope = get_builder ()->get_file_symbols ();
20159
20160 get_builder ()->record_debugformat ("DWARF 2");
20161 get_builder ()->record_producer (producer);
20162
20163 processing_has_namespace_info = false;
20164
20165 return get_builder ()->get_compunit_symtab ();
20166 }
20167
20168 static void
20169 var_decode_location (struct attribute *attr, struct symbol *sym,
20170 struct dwarf2_cu *cu)
20171 {
20172 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20173 struct comp_unit_head *cu_header = &cu->header;
20174
20175 /* NOTE drow/2003-01-30: There used to be a comment and some special
20176 code here to turn a symbol with DW_AT_external and a
20177 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20178 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20179 with some versions of binutils) where shared libraries could have
20180 relocations against symbols in their debug information - the
20181 minimal symbol would have the right address, but the debug info
20182 would not. It's no longer necessary, because we will explicitly
20183 apply relocations when we read in the debug information now. */
20184
20185 /* A DW_AT_location attribute with no contents indicates that a
20186 variable has been optimized away. */
20187 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20188 {
20189 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20190 return;
20191 }
20192
20193 /* Handle one degenerate form of location expression specially, to
20194 preserve GDB's previous behavior when section offsets are
20195 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20196 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20197
20198 if (attr->form_is_block ()
20199 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20200 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20201 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20202 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20203 && (DW_BLOCK (attr)->size
20204 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20205 {
20206 unsigned int dummy;
20207
20208 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20209 SET_SYMBOL_VALUE_ADDRESS
20210 (sym, cu->header.read_address (objfile->obfd,
20211 DW_BLOCK (attr)->data + 1,
20212 &dummy));
20213 else
20214 SET_SYMBOL_VALUE_ADDRESS
20215 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20216 &dummy));
20217 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20218 fixup_symbol_section (sym, objfile);
20219 SET_SYMBOL_VALUE_ADDRESS
20220 (sym,
20221 SYMBOL_VALUE_ADDRESS (sym)
20222 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20223 return;
20224 }
20225
20226 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20227 expression evaluator, and use LOC_COMPUTED only when necessary
20228 (i.e. when the value of a register or memory location is
20229 referenced, or a thread-local block, etc.). Then again, it might
20230 not be worthwhile. I'm assuming that it isn't unless performance
20231 or memory numbers show me otherwise. */
20232
20233 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20234
20235 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20236 cu->has_loclist = true;
20237 }
20238
20239 /* Given a pointer to a DWARF information entry, figure out if we need
20240 to make a symbol table entry for it, and if so, create a new entry
20241 and return a pointer to it.
20242 If TYPE is NULL, determine symbol type from the die, otherwise
20243 used the passed type.
20244 If SPACE is not NULL, use it to hold the new symbol. If it is
20245 NULL, allocate a new symbol on the objfile's obstack. */
20246
20247 static struct symbol *
20248 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20249 struct symbol *space)
20250 {
20251 struct dwarf2_per_objfile *dwarf2_per_objfile
20252 = cu->per_cu->dwarf2_per_objfile;
20253 struct objfile *objfile = dwarf2_per_objfile->objfile;
20254 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20255 struct symbol *sym = NULL;
20256 const char *name;
20257 struct attribute *attr = NULL;
20258 struct attribute *attr2 = NULL;
20259 CORE_ADDR baseaddr;
20260 struct pending **list_to_add = NULL;
20261
20262 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20263
20264 baseaddr = objfile->text_section_offset ();
20265
20266 name = dwarf2_name (die, cu);
20267 if (name)
20268 {
20269 const char *linkagename;
20270 int suppress_add = 0;
20271
20272 if (space)
20273 sym = space;
20274 else
20275 sym = allocate_symbol (objfile);
20276 OBJSTAT (objfile, n_syms++);
20277
20278 /* Cache this symbol's name and the name's demangled form (if any). */
20279 sym->set_language (cu->language, &objfile->objfile_obstack);
20280 linkagename = dwarf2_physname (name, die, cu);
20281 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20282
20283 /* Fortran does not have mangling standard and the mangling does differ
20284 between gfortran, iFort etc. */
20285 if (cu->language == language_fortran
20286 && symbol_get_demangled_name (sym) == NULL)
20287 symbol_set_demangled_name (sym,
20288 dwarf2_full_name (name, die, cu),
20289 NULL);
20290
20291 /* Default assumptions.
20292 Use the passed type or decode it from the die. */
20293 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20294 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20295 if (type != NULL)
20296 SYMBOL_TYPE (sym) = type;
20297 else
20298 SYMBOL_TYPE (sym) = die_type (die, cu);
20299 attr = dwarf2_attr (die,
20300 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20301 cu);
20302 if (attr != nullptr)
20303 {
20304 SYMBOL_LINE (sym) = DW_UNSND (attr);
20305 }
20306
20307 attr = dwarf2_attr (die,
20308 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20309 cu);
20310 if (attr != nullptr)
20311 {
20312 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20313 struct file_entry *fe;
20314
20315 if (cu->line_header != NULL)
20316 fe = cu->line_header->file_name_at (file_index);
20317 else
20318 fe = NULL;
20319
20320 if (fe == NULL)
20321 complaint (_("file index out of range"));
20322 else
20323 symbol_set_symtab (sym, fe->symtab);
20324 }
20325
20326 switch (die->tag)
20327 {
20328 case DW_TAG_label:
20329 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20330 if (attr != nullptr)
20331 {
20332 CORE_ADDR addr;
20333
20334 addr = attr->value_as_address ();
20335 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20336 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20337 }
20338 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20339 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20340 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20341 add_symbol_to_list (sym, cu->list_in_scope);
20342 break;
20343 case DW_TAG_subprogram:
20344 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20345 finish_block. */
20346 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20347 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20348 if ((attr2 && (DW_UNSND (attr2) != 0))
20349 || cu->language == language_ada
20350 || cu->language == language_fortran)
20351 {
20352 /* Subprograms marked external are stored as a global symbol.
20353 Ada and Fortran subprograms, whether marked external or
20354 not, are always stored as a global symbol, because we want
20355 to be able to access them globally. For instance, we want
20356 to be able to break on a nested subprogram without having
20357 to specify the context. */
20358 list_to_add = cu->get_builder ()->get_global_symbols ();
20359 }
20360 else
20361 {
20362 list_to_add = cu->list_in_scope;
20363 }
20364 break;
20365 case DW_TAG_inlined_subroutine:
20366 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20367 finish_block. */
20368 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20369 SYMBOL_INLINED (sym) = 1;
20370 list_to_add = cu->list_in_scope;
20371 break;
20372 case DW_TAG_template_value_param:
20373 suppress_add = 1;
20374 /* Fall through. */
20375 case DW_TAG_constant:
20376 case DW_TAG_variable:
20377 case DW_TAG_member:
20378 /* Compilation with minimal debug info may result in
20379 variables with missing type entries. Change the
20380 misleading `void' type to something sensible. */
20381 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20382 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20383
20384 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20385 /* In the case of DW_TAG_member, we should only be called for
20386 static const members. */
20387 if (die->tag == DW_TAG_member)
20388 {
20389 /* dwarf2_add_field uses die_is_declaration,
20390 so we do the same. */
20391 gdb_assert (die_is_declaration (die, cu));
20392 gdb_assert (attr);
20393 }
20394 if (attr != nullptr)
20395 {
20396 dwarf2_const_value (attr, sym, cu);
20397 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20398 if (!suppress_add)
20399 {
20400 if (attr2 && (DW_UNSND (attr2) != 0))
20401 list_to_add = cu->get_builder ()->get_global_symbols ();
20402 else
20403 list_to_add = cu->list_in_scope;
20404 }
20405 break;
20406 }
20407 attr = dwarf2_attr (die, DW_AT_location, cu);
20408 if (attr != nullptr)
20409 {
20410 var_decode_location (attr, sym, cu);
20411 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20412
20413 /* Fortran explicitly imports any global symbols to the local
20414 scope by DW_TAG_common_block. */
20415 if (cu->language == language_fortran && die->parent
20416 && die->parent->tag == DW_TAG_common_block)
20417 attr2 = NULL;
20418
20419 if (SYMBOL_CLASS (sym) == LOC_STATIC
20420 && SYMBOL_VALUE_ADDRESS (sym) == 0
20421 && !dwarf2_per_objfile->has_section_at_zero)
20422 {
20423 /* When a static variable is eliminated by the linker,
20424 the corresponding debug information is not stripped
20425 out, but the variable address is set to null;
20426 do not add such variables into symbol table. */
20427 }
20428 else if (attr2 && (DW_UNSND (attr2) != 0))
20429 {
20430 if (SYMBOL_CLASS (sym) == LOC_STATIC
20431 && (objfile->flags & OBJF_MAINLINE) == 0
20432 && dwarf2_per_objfile->can_copy)
20433 {
20434 /* A global static variable might be subject to
20435 copy relocation. We first check for a local
20436 minsym, though, because maybe the symbol was
20437 marked hidden, in which case this would not
20438 apply. */
20439 bound_minimal_symbol found
20440 = (lookup_minimal_symbol_linkage
20441 (sym->linkage_name (), objfile));
20442 if (found.minsym != nullptr)
20443 sym->maybe_copied = 1;
20444 }
20445
20446 /* A variable with DW_AT_external is never static,
20447 but it may be block-scoped. */
20448 list_to_add
20449 = ((cu->list_in_scope
20450 == cu->get_builder ()->get_file_symbols ())
20451 ? cu->get_builder ()->get_global_symbols ()
20452 : cu->list_in_scope);
20453 }
20454 else
20455 list_to_add = cu->list_in_scope;
20456 }
20457 else
20458 {
20459 /* We do not know the address of this symbol.
20460 If it is an external symbol and we have type information
20461 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20462 The address of the variable will then be determined from
20463 the minimal symbol table whenever the variable is
20464 referenced. */
20465 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20466
20467 /* Fortran explicitly imports any global symbols to the local
20468 scope by DW_TAG_common_block. */
20469 if (cu->language == language_fortran && die->parent
20470 && die->parent->tag == DW_TAG_common_block)
20471 {
20472 /* SYMBOL_CLASS doesn't matter here because
20473 read_common_block is going to reset it. */
20474 if (!suppress_add)
20475 list_to_add = cu->list_in_scope;
20476 }
20477 else if (attr2 && (DW_UNSND (attr2) != 0)
20478 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20479 {
20480 /* A variable with DW_AT_external is never static, but it
20481 may be block-scoped. */
20482 list_to_add
20483 = ((cu->list_in_scope
20484 == cu->get_builder ()->get_file_symbols ())
20485 ? cu->get_builder ()->get_global_symbols ()
20486 : cu->list_in_scope);
20487
20488 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20489 }
20490 else if (!die_is_declaration (die, cu))
20491 {
20492 /* Use the default LOC_OPTIMIZED_OUT class. */
20493 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20494 if (!suppress_add)
20495 list_to_add = cu->list_in_scope;
20496 }
20497 }
20498 break;
20499 case DW_TAG_formal_parameter:
20500 {
20501 /* If we are inside a function, mark this as an argument. If
20502 not, we might be looking at an argument to an inlined function
20503 when we do not have enough information to show inlined frames;
20504 pretend it's a local variable in that case so that the user can
20505 still see it. */
20506 struct context_stack *curr
20507 = cu->get_builder ()->get_current_context_stack ();
20508 if (curr != nullptr && curr->name != nullptr)
20509 SYMBOL_IS_ARGUMENT (sym) = 1;
20510 attr = dwarf2_attr (die, DW_AT_location, cu);
20511 if (attr != nullptr)
20512 {
20513 var_decode_location (attr, sym, cu);
20514 }
20515 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20516 if (attr != nullptr)
20517 {
20518 dwarf2_const_value (attr, sym, cu);
20519 }
20520
20521 list_to_add = cu->list_in_scope;
20522 }
20523 break;
20524 case DW_TAG_unspecified_parameters:
20525 /* From varargs functions; gdb doesn't seem to have any
20526 interest in this information, so just ignore it for now.
20527 (FIXME?) */
20528 break;
20529 case DW_TAG_template_type_param:
20530 suppress_add = 1;
20531 /* Fall through. */
20532 case DW_TAG_class_type:
20533 case DW_TAG_interface_type:
20534 case DW_TAG_structure_type:
20535 case DW_TAG_union_type:
20536 case DW_TAG_set_type:
20537 case DW_TAG_enumeration_type:
20538 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20539 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20540
20541 {
20542 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20543 really ever be static objects: otherwise, if you try
20544 to, say, break of a class's method and you're in a file
20545 which doesn't mention that class, it won't work unless
20546 the check for all static symbols in lookup_symbol_aux
20547 saves you. See the OtherFileClass tests in
20548 gdb.c++/namespace.exp. */
20549
20550 if (!suppress_add)
20551 {
20552 buildsym_compunit *builder = cu->get_builder ();
20553 list_to_add
20554 = (cu->list_in_scope == builder->get_file_symbols ()
20555 && cu->language == language_cplus
20556 ? builder->get_global_symbols ()
20557 : cu->list_in_scope);
20558
20559 /* The semantics of C++ state that "struct foo {
20560 ... }" also defines a typedef for "foo". */
20561 if (cu->language == language_cplus
20562 || cu->language == language_ada
20563 || cu->language == language_d
20564 || cu->language == language_rust)
20565 {
20566 /* The symbol's name is already allocated along
20567 with this objfile, so we don't need to
20568 duplicate it for the type. */
20569 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20570 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20571 }
20572 }
20573 }
20574 break;
20575 case DW_TAG_typedef:
20576 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20577 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20578 list_to_add = cu->list_in_scope;
20579 break;
20580 case DW_TAG_base_type:
20581 case DW_TAG_subrange_type:
20582 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20583 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20584 list_to_add = cu->list_in_scope;
20585 break;
20586 case DW_TAG_enumerator:
20587 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20588 if (attr != nullptr)
20589 {
20590 dwarf2_const_value (attr, sym, cu);
20591 }
20592 {
20593 /* NOTE: carlton/2003-11-10: See comment above in the
20594 DW_TAG_class_type, etc. block. */
20595
20596 list_to_add
20597 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20598 && cu->language == language_cplus
20599 ? cu->get_builder ()->get_global_symbols ()
20600 : cu->list_in_scope);
20601 }
20602 break;
20603 case DW_TAG_imported_declaration:
20604 case DW_TAG_namespace:
20605 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20606 list_to_add = cu->get_builder ()->get_global_symbols ();
20607 break;
20608 case DW_TAG_module:
20609 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20610 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20611 list_to_add = cu->get_builder ()->get_global_symbols ();
20612 break;
20613 case DW_TAG_common_block:
20614 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20615 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20616 add_symbol_to_list (sym, cu->list_in_scope);
20617 break;
20618 default:
20619 /* Not a tag we recognize. Hopefully we aren't processing
20620 trash data, but since we must specifically ignore things
20621 we don't recognize, there is nothing else we should do at
20622 this point. */
20623 complaint (_("unsupported tag: '%s'"),
20624 dwarf_tag_name (die->tag));
20625 break;
20626 }
20627
20628 if (suppress_add)
20629 {
20630 sym->hash_next = objfile->template_symbols;
20631 objfile->template_symbols = sym;
20632 list_to_add = NULL;
20633 }
20634
20635 if (list_to_add != NULL)
20636 add_symbol_to_list (sym, list_to_add);
20637
20638 /* For the benefit of old versions of GCC, check for anonymous
20639 namespaces based on the demangled name. */
20640 if (!cu->processing_has_namespace_info
20641 && cu->language == language_cplus)
20642 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20643 }
20644 return (sym);
20645 }
20646
20647 /* Given an attr with a DW_FORM_dataN value in host byte order,
20648 zero-extend it as appropriate for the symbol's type. The DWARF
20649 standard (v4) is not entirely clear about the meaning of using
20650 DW_FORM_dataN for a constant with a signed type, where the type is
20651 wider than the data. The conclusion of a discussion on the DWARF
20652 list was that this is unspecified. We choose to always zero-extend
20653 because that is the interpretation long in use by GCC. */
20654
20655 static gdb_byte *
20656 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20657 struct dwarf2_cu *cu, LONGEST *value, int bits)
20658 {
20659 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20660 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20661 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20662 LONGEST l = DW_UNSND (attr);
20663
20664 if (bits < sizeof (*value) * 8)
20665 {
20666 l &= ((LONGEST) 1 << bits) - 1;
20667 *value = l;
20668 }
20669 else if (bits == sizeof (*value) * 8)
20670 *value = l;
20671 else
20672 {
20673 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20674 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20675 return bytes;
20676 }
20677
20678 return NULL;
20679 }
20680
20681 /* Read a constant value from an attribute. Either set *VALUE, or if
20682 the value does not fit in *VALUE, set *BYTES - either already
20683 allocated on the objfile obstack, or newly allocated on OBSTACK,
20684 or, set *BATON, if we translated the constant to a location
20685 expression. */
20686
20687 static void
20688 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20689 const char *name, struct obstack *obstack,
20690 struct dwarf2_cu *cu,
20691 LONGEST *value, const gdb_byte **bytes,
20692 struct dwarf2_locexpr_baton **baton)
20693 {
20694 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20695 struct comp_unit_head *cu_header = &cu->header;
20696 struct dwarf_block *blk;
20697 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20698 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20699
20700 *value = 0;
20701 *bytes = NULL;
20702 *baton = NULL;
20703
20704 switch (attr->form)
20705 {
20706 case DW_FORM_addr:
20707 case DW_FORM_addrx:
20708 case DW_FORM_GNU_addr_index:
20709 {
20710 gdb_byte *data;
20711
20712 if (TYPE_LENGTH (type) != cu_header->addr_size)
20713 dwarf2_const_value_length_mismatch_complaint (name,
20714 cu_header->addr_size,
20715 TYPE_LENGTH (type));
20716 /* Symbols of this form are reasonably rare, so we just
20717 piggyback on the existing location code rather than writing
20718 a new implementation of symbol_computed_ops. */
20719 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20720 (*baton)->per_cu = cu->per_cu;
20721 gdb_assert ((*baton)->per_cu);
20722
20723 (*baton)->size = 2 + cu_header->addr_size;
20724 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20725 (*baton)->data = data;
20726
20727 data[0] = DW_OP_addr;
20728 store_unsigned_integer (&data[1], cu_header->addr_size,
20729 byte_order, DW_ADDR (attr));
20730 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20731 }
20732 break;
20733 case DW_FORM_string:
20734 case DW_FORM_strp:
20735 case DW_FORM_strx:
20736 case DW_FORM_GNU_str_index:
20737 case DW_FORM_GNU_strp_alt:
20738 /* DW_STRING is already allocated on the objfile obstack, point
20739 directly to it. */
20740 *bytes = (const gdb_byte *) DW_STRING (attr);
20741 break;
20742 case DW_FORM_block1:
20743 case DW_FORM_block2:
20744 case DW_FORM_block4:
20745 case DW_FORM_block:
20746 case DW_FORM_exprloc:
20747 case DW_FORM_data16:
20748 blk = DW_BLOCK (attr);
20749 if (TYPE_LENGTH (type) != blk->size)
20750 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20751 TYPE_LENGTH (type));
20752 *bytes = blk->data;
20753 break;
20754
20755 /* The DW_AT_const_value attributes are supposed to carry the
20756 symbol's value "represented as it would be on the target
20757 architecture." By the time we get here, it's already been
20758 converted to host endianness, so we just need to sign- or
20759 zero-extend it as appropriate. */
20760 case DW_FORM_data1:
20761 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
20762 break;
20763 case DW_FORM_data2:
20764 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
20765 break;
20766 case DW_FORM_data4:
20767 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
20768 break;
20769 case DW_FORM_data8:
20770 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
20771 break;
20772
20773 case DW_FORM_sdata:
20774 case DW_FORM_implicit_const:
20775 *value = DW_SND (attr);
20776 break;
20777
20778 case DW_FORM_udata:
20779 *value = DW_UNSND (attr);
20780 break;
20781
20782 default:
20783 complaint (_("unsupported const value attribute form: '%s'"),
20784 dwarf_form_name (attr->form));
20785 *value = 0;
20786 break;
20787 }
20788 }
20789
20790
20791 /* Copy constant value from an attribute to a symbol. */
20792
20793 static void
20794 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
20795 struct dwarf2_cu *cu)
20796 {
20797 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20798 LONGEST value;
20799 const gdb_byte *bytes;
20800 struct dwarf2_locexpr_baton *baton;
20801
20802 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
20803 sym->print_name (),
20804 &objfile->objfile_obstack, cu,
20805 &value, &bytes, &baton);
20806
20807 if (baton != NULL)
20808 {
20809 SYMBOL_LOCATION_BATON (sym) = baton;
20810 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
20811 }
20812 else if (bytes != NULL)
20813 {
20814 SYMBOL_VALUE_BYTES (sym) = bytes;
20815 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
20816 }
20817 else
20818 {
20819 SYMBOL_VALUE (sym) = value;
20820 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
20821 }
20822 }
20823
20824 /* Return the type of the die in question using its DW_AT_type attribute. */
20825
20826 static struct type *
20827 die_type (struct die_info *die, struct dwarf2_cu *cu)
20828 {
20829 struct attribute *type_attr;
20830
20831 type_attr = dwarf2_attr (die, DW_AT_type, cu);
20832 if (!type_attr)
20833 {
20834 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20835 /* A missing DW_AT_type represents a void type. */
20836 return objfile_type (objfile)->builtin_void;
20837 }
20838
20839 return lookup_die_type (die, type_attr, cu);
20840 }
20841
20842 /* True iff CU's producer generates GNAT Ada auxiliary information
20843 that allows to find parallel types through that information instead
20844 of having to do expensive parallel lookups by type name. */
20845
20846 static int
20847 need_gnat_info (struct dwarf2_cu *cu)
20848 {
20849 /* Assume that the Ada compiler was GNAT, which always produces
20850 the auxiliary information. */
20851 return (cu->language == language_ada);
20852 }
20853
20854 /* Return the auxiliary type of the die in question using its
20855 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20856 attribute is not present. */
20857
20858 static struct type *
20859 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20860 {
20861 struct attribute *type_attr;
20862
20863 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20864 if (!type_attr)
20865 return NULL;
20866
20867 return lookup_die_type (die, type_attr, cu);
20868 }
20869
20870 /* If DIE has a descriptive_type attribute, then set the TYPE's
20871 descriptive type accordingly. */
20872
20873 static void
20874 set_descriptive_type (struct type *type, struct die_info *die,
20875 struct dwarf2_cu *cu)
20876 {
20877 struct type *descriptive_type = die_descriptive_type (die, cu);
20878
20879 if (descriptive_type)
20880 {
20881 ALLOCATE_GNAT_AUX_TYPE (type);
20882 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20883 }
20884 }
20885
20886 /* Return the containing type of the die in question using its
20887 DW_AT_containing_type attribute. */
20888
20889 static struct type *
20890 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
20891 {
20892 struct attribute *type_attr;
20893 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20894
20895 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
20896 if (!type_attr)
20897 error (_("Dwarf Error: Problem turning containing type into gdb type "
20898 "[in module %s]"), objfile_name (objfile));
20899
20900 return lookup_die_type (die, type_attr, cu);
20901 }
20902
20903 /* Return an error marker type to use for the ill formed type in DIE/CU. */
20904
20905 static struct type *
20906 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20907 {
20908 struct dwarf2_per_objfile *dwarf2_per_objfile
20909 = cu->per_cu->dwarf2_per_objfile;
20910 struct objfile *objfile = dwarf2_per_objfile->objfile;
20911 char *saved;
20912
20913 std::string message
20914 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
20915 objfile_name (objfile),
20916 sect_offset_str (cu->header.sect_off),
20917 sect_offset_str (die->sect_off));
20918 saved = obstack_strdup (&objfile->objfile_obstack, message);
20919
20920 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
20921 }
20922
20923 /* Look up the type of DIE in CU using its type attribute ATTR.
20924 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20925 DW_AT_containing_type.
20926 If there is no type substitute an error marker. */
20927
20928 static struct type *
20929 lookup_die_type (struct die_info *die, const struct attribute *attr,
20930 struct dwarf2_cu *cu)
20931 {
20932 struct dwarf2_per_objfile *dwarf2_per_objfile
20933 = cu->per_cu->dwarf2_per_objfile;
20934 struct objfile *objfile = dwarf2_per_objfile->objfile;
20935 struct type *this_type;
20936
20937 gdb_assert (attr->name == DW_AT_type
20938 || attr->name == DW_AT_GNAT_descriptive_type
20939 || attr->name == DW_AT_containing_type);
20940
20941 /* First see if we have it cached. */
20942
20943 if (attr->form == DW_FORM_GNU_ref_alt)
20944 {
20945 struct dwarf2_per_cu_data *per_cu;
20946 sect_offset sect_off = attr->get_ref_die_offset ();
20947
20948 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
20949 dwarf2_per_objfile);
20950 this_type = get_die_type_at_offset (sect_off, per_cu);
20951 }
20952 else if (attr->form_is_ref ())
20953 {
20954 sect_offset sect_off = attr->get_ref_die_offset ();
20955
20956 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
20957 }
20958 else if (attr->form == DW_FORM_ref_sig8)
20959 {
20960 ULONGEST signature = DW_SIGNATURE (attr);
20961
20962 return get_signatured_type (die, signature, cu);
20963 }
20964 else
20965 {
20966 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
20967 " at %s [in module %s]"),
20968 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
20969 objfile_name (objfile));
20970 return build_error_marker_type (cu, die);
20971 }
20972
20973 /* If not cached we need to read it in. */
20974
20975 if (this_type == NULL)
20976 {
20977 struct die_info *type_die = NULL;
20978 struct dwarf2_cu *type_cu = cu;
20979
20980 if (attr->form_is_ref ())
20981 type_die = follow_die_ref (die, attr, &type_cu);
20982 if (type_die == NULL)
20983 return build_error_marker_type (cu, die);
20984 /* If we find the type now, it's probably because the type came
20985 from an inter-CU reference and the type's CU got expanded before
20986 ours. */
20987 this_type = read_type_die (type_die, type_cu);
20988 }
20989
20990 /* If we still don't have a type use an error marker. */
20991
20992 if (this_type == NULL)
20993 return build_error_marker_type (cu, die);
20994
20995 return this_type;
20996 }
20997
20998 /* Return the type in DIE, CU.
20999 Returns NULL for invalid types.
21000
21001 This first does a lookup in die_type_hash,
21002 and only reads the die in if necessary.
21003
21004 NOTE: This can be called when reading in partial or full symbols. */
21005
21006 static struct type *
21007 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21008 {
21009 struct type *this_type;
21010
21011 this_type = get_die_type (die, cu);
21012 if (this_type)
21013 return this_type;
21014
21015 return read_type_die_1 (die, cu);
21016 }
21017
21018 /* Read the type in DIE, CU.
21019 Returns NULL for invalid types. */
21020
21021 static struct type *
21022 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21023 {
21024 struct type *this_type = NULL;
21025
21026 switch (die->tag)
21027 {
21028 case DW_TAG_class_type:
21029 case DW_TAG_interface_type:
21030 case DW_TAG_structure_type:
21031 case DW_TAG_union_type:
21032 this_type = read_structure_type (die, cu);
21033 break;
21034 case DW_TAG_enumeration_type:
21035 this_type = read_enumeration_type (die, cu);
21036 break;
21037 case DW_TAG_subprogram:
21038 case DW_TAG_subroutine_type:
21039 case DW_TAG_inlined_subroutine:
21040 this_type = read_subroutine_type (die, cu);
21041 break;
21042 case DW_TAG_array_type:
21043 this_type = read_array_type (die, cu);
21044 break;
21045 case DW_TAG_set_type:
21046 this_type = read_set_type (die, cu);
21047 break;
21048 case DW_TAG_pointer_type:
21049 this_type = read_tag_pointer_type (die, cu);
21050 break;
21051 case DW_TAG_ptr_to_member_type:
21052 this_type = read_tag_ptr_to_member_type (die, cu);
21053 break;
21054 case DW_TAG_reference_type:
21055 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21056 break;
21057 case DW_TAG_rvalue_reference_type:
21058 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21059 break;
21060 case DW_TAG_const_type:
21061 this_type = read_tag_const_type (die, cu);
21062 break;
21063 case DW_TAG_volatile_type:
21064 this_type = read_tag_volatile_type (die, cu);
21065 break;
21066 case DW_TAG_restrict_type:
21067 this_type = read_tag_restrict_type (die, cu);
21068 break;
21069 case DW_TAG_string_type:
21070 this_type = read_tag_string_type (die, cu);
21071 break;
21072 case DW_TAG_typedef:
21073 this_type = read_typedef (die, cu);
21074 break;
21075 case DW_TAG_subrange_type:
21076 this_type = read_subrange_type (die, cu);
21077 break;
21078 case DW_TAG_base_type:
21079 this_type = read_base_type (die, cu);
21080 break;
21081 case DW_TAG_unspecified_type:
21082 this_type = read_unspecified_type (die, cu);
21083 break;
21084 case DW_TAG_namespace:
21085 this_type = read_namespace_type (die, cu);
21086 break;
21087 case DW_TAG_module:
21088 this_type = read_module_type (die, cu);
21089 break;
21090 case DW_TAG_atomic_type:
21091 this_type = read_tag_atomic_type (die, cu);
21092 break;
21093 default:
21094 complaint (_("unexpected tag in read_type_die: '%s'"),
21095 dwarf_tag_name (die->tag));
21096 break;
21097 }
21098
21099 return this_type;
21100 }
21101
21102 /* See if we can figure out if the class lives in a namespace. We do
21103 this by looking for a member function; its demangled name will
21104 contain namespace info, if there is any.
21105 Return the computed name or NULL.
21106 Space for the result is allocated on the objfile's obstack.
21107 This is the full-die version of guess_partial_die_structure_name.
21108 In this case we know DIE has no useful parent. */
21109
21110 static const char *
21111 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21112 {
21113 struct die_info *spec_die;
21114 struct dwarf2_cu *spec_cu;
21115 struct die_info *child;
21116 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21117
21118 spec_cu = cu;
21119 spec_die = die_specification (die, &spec_cu);
21120 if (spec_die != NULL)
21121 {
21122 die = spec_die;
21123 cu = spec_cu;
21124 }
21125
21126 for (child = die->child;
21127 child != NULL;
21128 child = child->sibling)
21129 {
21130 if (child->tag == DW_TAG_subprogram)
21131 {
21132 const char *linkage_name = dw2_linkage_name (child, cu);
21133
21134 if (linkage_name != NULL)
21135 {
21136 gdb::unique_xmalloc_ptr<char> actual_name
21137 (language_class_name_from_physname (cu->language_defn,
21138 linkage_name));
21139 const char *name = NULL;
21140
21141 if (actual_name != NULL)
21142 {
21143 const char *die_name = dwarf2_name (die, cu);
21144
21145 if (die_name != NULL
21146 && strcmp (die_name, actual_name.get ()) != 0)
21147 {
21148 /* Strip off the class name from the full name.
21149 We want the prefix. */
21150 int die_name_len = strlen (die_name);
21151 int actual_name_len = strlen (actual_name.get ());
21152 const char *ptr = actual_name.get ();
21153
21154 /* Test for '::' as a sanity check. */
21155 if (actual_name_len > die_name_len + 2
21156 && ptr[actual_name_len - die_name_len - 1] == ':')
21157 name = obstack_strndup (
21158 &objfile->per_bfd->storage_obstack,
21159 ptr, actual_name_len - die_name_len - 2);
21160 }
21161 }
21162 return name;
21163 }
21164 }
21165 }
21166
21167 return NULL;
21168 }
21169
21170 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21171 prefix part in such case. See
21172 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21173
21174 static const char *
21175 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21176 {
21177 struct attribute *attr;
21178 const char *base;
21179
21180 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21181 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21182 return NULL;
21183
21184 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21185 return NULL;
21186
21187 attr = dw2_linkage_name_attr (die, cu);
21188 if (attr == NULL || DW_STRING (attr) == NULL)
21189 return NULL;
21190
21191 /* dwarf2_name had to be already called. */
21192 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21193
21194 /* Strip the base name, keep any leading namespaces/classes. */
21195 base = strrchr (DW_STRING (attr), ':');
21196 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21197 return "";
21198
21199 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21200 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21201 DW_STRING (attr),
21202 &base[-1] - DW_STRING (attr));
21203 }
21204
21205 /* Return the name of the namespace/class that DIE is defined within,
21206 or "" if we can't tell. The caller should not xfree the result.
21207
21208 For example, if we're within the method foo() in the following
21209 code:
21210
21211 namespace N {
21212 class C {
21213 void foo () {
21214 }
21215 };
21216 }
21217
21218 then determine_prefix on foo's die will return "N::C". */
21219
21220 static const char *
21221 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21222 {
21223 struct dwarf2_per_objfile *dwarf2_per_objfile
21224 = cu->per_cu->dwarf2_per_objfile;
21225 struct die_info *parent, *spec_die;
21226 struct dwarf2_cu *spec_cu;
21227 struct type *parent_type;
21228 const char *retval;
21229
21230 if (cu->language != language_cplus
21231 && cu->language != language_fortran && cu->language != language_d
21232 && cu->language != language_rust)
21233 return "";
21234
21235 retval = anonymous_struct_prefix (die, cu);
21236 if (retval)
21237 return retval;
21238
21239 /* We have to be careful in the presence of DW_AT_specification.
21240 For example, with GCC 3.4, given the code
21241
21242 namespace N {
21243 void foo() {
21244 // Definition of N::foo.
21245 }
21246 }
21247
21248 then we'll have a tree of DIEs like this:
21249
21250 1: DW_TAG_compile_unit
21251 2: DW_TAG_namespace // N
21252 3: DW_TAG_subprogram // declaration of N::foo
21253 4: DW_TAG_subprogram // definition of N::foo
21254 DW_AT_specification // refers to die #3
21255
21256 Thus, when processing die #4, we have to pretend that we're in
21257 the context of its DW_AT_specification, namely the contex of die
21258 #3. */
21259 spec_cu = cu;
21260 spec_die = die_specification (die, &spec_cu);
21261 if (spec_die == NULL)
21262 parent = die->parent;
21263 else
21264 {
21265 parent = spec_die->parent;
21266 cu = spec_cu;
21267 }
21268
21269 if (parent == NULL)
21270 return "";
21271 else if (parent->building_fullname)
21272 {
21273 const char *name;
21274 const char *parent_name;
21275
21276 /* It has been seen on RealView 2.2 built binaries,
21277 DW_TAG_template_type_param types actually _defined_ as
21278 children of the parent class:
21279
21280 enum E {};
21281 template class <class Enum> Class{};
21282 Class<enum E> class_e;
21283
21284 1: DW_TAG_class_type (Class)
21285 2: DW_TAG_enumeration_type (E)
21286 3: DW_TAG_enumerator (enum1:0)
21287 3: DW_TAG_enumerator (enum2:1)
21288 ...
21289 2: DW_TAG_template_type_param
21290 DW_AT_type DW_FORM_ref_udata (E)
21291
21292 Besides being broken debug info, it can put GDB into an
21293 infinite loop. Consider:
21294
21295 When we're building the full name for Class<E>, we'll start
21296 at Class, and go look over its template type parameters,
21297 finding E. We'll then try to build the full name of E, and
21298 reach here. We're now trying to build the full name of E,
21299 and look over the parent DIE for containing scope. In the
21300 broken case, if we followed the parent DIE of E, we'd again
21301 find Class, and once again go look at its template type
21302 arguments, etc., etc. Simply don't consider such parent die
21303 as source-level parent of this die (it can't be, the language
21304 doesn't allow it), and break the loop here. */
21305 name = dwarf2_name (die, cu);
21306 parent_name = dwarf2_name (parent, cu);
21307 complaint (_("template param type '%s' defined within parent '%s'"),
21308 name ? name : "<unknown>",
21309 parent_name ? parent_name : "<unknown>");
21310 return "";
21311 }
21312 else
21313 switch (parent->tag)
21314 {
21315 case DW_TAG_namespace:
21316 parent_type = read_type_die (parent, cu);
21317 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21318 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21319 Work around this problem here. */
21320 if (cu->language == language_cplus
21321 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21322 return "";
21323 /* We give a name to even anonymous namespaces. */
21324 return TYPE_NAME (parent_type);
21325 case DW_TAG_class_type:
21326 case DW_TAG_interface_type:
21327 case DW_TAG_structure_type:
21328 case DW_TAG_union_type:
21329 case DW_TAG_module:
21330 parent_type = read_type_die (parent, cu);
21331 if (TYPE_NAME (parent_type) != NULL)
21332 return TYPE_NAME (parent_type);
21333 else
21334 /* An anonymous structure is only allowed non-static data
21335 members; no typedefs, no member functions, et cetera.
21336 So it does not need a prefix. */
21337 return "";
21338 case DW_TAG_compile_unit:
21339 case DW_TAG_partial_unit:
21340 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21341 if (cu->language == language_cplus
21342 && !dwarf2_per_objfile->types.empty ()
21343 && die->child != NULL
21344 && (die->tag == DW_TAG_class_type
21345 || die->tag == DW_TAG_structure_type
21346 || die->tag == DW_TAG_union_type))
21347 {
21348 const char *name = guess_full_die_structure_name (die, cu);
21349 if (name != NULL)
21350 return name;
21351 }
21352 return "";
21353 case DW_TAG_subprogram:
21354 /* Nested subroutines in Fortran get a prefix with the name
21355 of the parent's subroutine. */
21356 if (cu->language == language_fortran)
21357 {
21358 if ((die->tag == DW_TAG_subprogram)
21359 && (dwarf2_name (parent, cu) != NULL))
21360 return dwarf2_name (parent, cu);
21361 }
21362 return determine_prefix (parent, cu);
21363 case DW_TAG_enumeration_type:
21364 parent_type = read_type_die (parent, cu);
21365 if (TYPE_DECLARED_CLASS (parent_type))
21366 {
21367 if (TYPE_NAME (parent_type) != NULL)
21368 return TYPE_NAME (parent_type);
21369 return "";
21370 }
21371 /* Fall through. */
21372 default:
21373 return determine_prefix (parent, cu);
21374 }
21375 }
21376
21377 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21378 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21379 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21380 an obconcat, otherwise allocate storage for the result. The CU argument is
21381 used to determine the language and hence, the appropriate separator. */
21382
21383 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21384
21385 static char *
21386 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21387 int physname, struct dwarf2_cu *cu)
21388 {
21389 const char *lead = "";
21390 const char *sep;
21391
21392 if (suffix == NULL || suffix[0] == '\0'
21393 || prefix == NULL || prefix[0] == '\0')
21394 sep = "";
21395 else if (cu->language == language_d)
21396 {
21397 /* For D, the 'main' function could be defined in any module, but it
21398 should never be prefixed. */
21399 if (strcmp (suffix, "D main") == 0)
21400 {
21401 prefix = "";
21402 sep = "";
21403 }
21404 else
21405 sep = ".";
21406 }
21407 else if (cu->language == language_fortran && physname)
21408 {
21409 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21410 DW_AT_MIPS_linkage_name is preferred and used instead. */
21411
21412 lead = "__";
21413 sep = "_MOD_";
21414 }
21415 else
21416 sep = "::";
21417
21418 if (prefix == NULL)
21419 prefix = "";
21420 if (suffix == NULL)
21421 suffix = "";
21422
21423 if (obs == NULL)
21424 {
21425 char *retval
21426 = ((char *)
21427 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21428
21429 strcpy (retval, lead);
21430 strcat (retval, prefix);
21431 strcat (retval, sep);
21432 strcat (retval, suffix);
21433 return retval;
21434 }
21435 else
21436 {
21437 /* We have an obstack. */
21438 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21439 }
21440 }
21441
21442 /* Get name of a die, return NULL if not found. */
21443
21444 static const char *
21445 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21446 struct objfile *objfile)
21447 {
21448 if (name && cu->language == language_cplus)
21449 {
21450 std::string canon_name = cp_canonicalize_string (name);
21451
21452 if (!canon_name.empty ())
21453 {
21454 if (canon_name != name)
21455 name = objfile->intern (canon_name);
21456 }
21457 }
21458
21459 return name;
21460 }
21461
21462 /* Get name of a die, return NULL if not found.
21463 Anonymous namespaces are converted to their magic string. */
21464
21465 static const char *
21466 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21467 {
21468 struct attribute *attr;
21469 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21470
21471 attr = dwarf2_attr (die, DW_AT_name, cu);
21472 if ((!attr || !DW_STRING (attr))
21473 && die->tag != DW_TAG_namespace
21474 && die->tag != DW_TAG_class_type
21475 && die->tag != DW_TAG_interface_type
21476 && die->tag != DW_TAG_structure_type
21477 && die->tag != DW_TAG_union_type)
21478 return NULL;
21479
21480 switch (die->tag)
21481 {
21482 case DW_TAG_compile_unit:
21483 case DW_TAG_partial_unit:
21484 /* Compilation units have a DW_AT_name that is a filename, not
21485 a source language identifier. */
21486 case DW_TAG_enumeration_type:
21487 case DW_TAG_enumerator:
21488 /* These tags always have simple identifiers already; no need
21489 to canonicalize them. */
21490 return DW_STRING (attr);
21491
21492 case DW_TAG_namespace:
21493 if (attr != NULL && DW_STRING (attr) != NULL)
21494 return DW_STRING (attr);
21495 return CP_ANONYMOUS_NAMESPACE_STR;
21496
21497 case DW_TAG_class_type:
21498 case DW_TAG_interface_type:
21499 case DW_TAG_structure_type:
21500 case DW_TAG_union_type:
21501 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21502 structures or unions. These were of the form "._%d" in GCC 4.1,
21503 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21504 and GCC 4.4. We work around this problem by ignoring these. */
21505 if (attr && DW_STRING (attr)
21506 && (startswith (DW_STRING (attr), "._")
21507 || startswith (DW_STRING (attr), "<anonymous")))
21508 return NULL;
21509
21510 /* GCC might emit a nameless typedef that has a linkage name. See
21511 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21512 if (!attr || DW_STRING (attr) == NULL)
21513 {
21514 attr = dw2_linkage_name_attr (die, cu);
21515 if (attr == NULL || DW_STRING (attr) == NULL)
21516 return NULL;
21517
21518 /* Avoid demangling DW_STRING (attr) the second time on a second
21519 call for the same DIE. */
21520 if (!DW_STRING_IS_CANONICAL (attr))
21521 {
21522 gdb::unique_xmalloc_ptr<char> demangled
21523 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21524 if (demangled == nullptr)
21525 return nullptr;
21526
21527 DW_STRING (attr) = objfile->intern (demangled.get ());
21528 DW_STRING_IS_CANONICAL (attr) = 1;
21529 }
21530
21531 /* Strip any leading namespaces/classes, keep only the base name.
21532 DW_AT_name for named DIEs does not contain the prefixes. */
21533 const char *base = strrchr (DW_STRING (attr), ':');
21534 if (base && base > DW_STRING (attr) && base[-1] == ':')
21535 return &base[1];
21536 else
21537 return DW_STRING (attr);
21538 }
21539 break;
21540
21541 default:
21542 break;
21543 }
21544
21545 if (!DW_STRING_IS_CANONICAL (attr))
21546 {
21547 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21548 objfile);
21549 DW_STRING_IS_CANONICAL (attr) = 1;
21550 }
21551 return DW_STRING (attr);
21552 }
21553
21554 /* Return the die that this die in an extension of, or NULL if there
21555 is none. *EXT_CU is the CU containing DIE on input, and the CU
21556 containing the return value on output. */
21557
21558 static struct die_info *
21559 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21560 {
21561 struct attribute *attr;
21562
21563 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21564 if (attr == NULL)
21565 return NULL;
21566
21567 return follow_die_ref (die, attr, ext_cu);
21568 }
21569
21570 static void
21571 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21572 {
21573 unsigned int i;
21574
21575 print_spaces (indent, f);
21576 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21577 dwarf_tag_name (die->tag), die->abbrev,
21578 sect_offset_str (die->sect_off));
21579
21580 if (die->parent != NULL)
21581 {
21582 print_spaces (indent, f);
21583 fprintf_unfiltered (f, " parent at offset: %s\n",
21584 sect_offset_str (die->parent->sect_off));
21585 }
21586
21587 print_spaces (indent, f);
21588 fprintf_unfiltered (f, " has children: %s\n",
21589 dwarf_bool_name (die->child != NULL));
21590
21591 print_spaces (indent, f);
21592 fprintf_unfiltered (f, " attributes:\n");
21593
21594 for (i = 0; i < die->num_attrs; ++i)
21595 {
21596 print_spaces (indent, f);
21597 fprintf_unfiltered (f, " %s (%s) ",
21598 dwarf_attr_name (die->attrs[i].name),
21599 dwarf_form_name (die->attrs[i].form));
21600
21601 switch (die->attrs[i].form)
21602 {
21603 case DW_FORM_addr:
21604 case DW_FORM_addrx:
21605 case DW_FORM_GNU_addr_index:
21606 fprintf_unfiltered (f, "address: ");
21607 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21608 break;
21609 case DW_FORM_block2:
21610 case DW_FORM_block4:
21611 case DW_FORM_block:
21612 case DW_FORM_block1:
21613 fprintf_unfiltered (f, "block: size %s",
21614 pulongest (DW_BLOCK (&die->attrs[i])->size));
21615 break;
21616 case DW_FORM_exprloc:
21617 fprintf_unfiltered (f, "expression: size %s",
21618 pulongest (DW_BLOCK (&die->attrs[i])->size));
21619 break;
21620 case DW_FORM_data16:
21621 fprintf_unfiltered (f, "constant of 16 bytes");
21622 break;
21623 case DW_FORM_ref_addr:
21624 fprintf_unfiltered (f, "ref address: ");
21625 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21626 break;
21627 case DW_FORM_GNU_ref_alt:
21628 fprintf_unfiltered (f, "alt ref address: ");
21629 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21630 break;
21631 case DW_FORM_ref1:
21632 case DW_FORM_ref2:
21633 case DW_FORM_ref4:
21634 case DW_FORM_ref8:
21635 case DW_FORM_ref_udata:
21636 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21637 (long) (DW_UNSND (&die->attrs[i])));
21638 break;
21639 case DW_FORM_data1:
21640 case DW_FORM_data2:
21641 case DW_FORM_data4:
21642 case DW_FORM_data8:
21643 case DW_FORM_udata:
21644 case DW_FORM_sdata:
21645 fprintf_unfiltered (f, "constant: %s",
21646 pulongest (DW_UNSND (&die->attrs[i])));
21647 break;
21648 case DW_FORM_sec_offset:
21649 fprintf_unfiltered (f, "section offset: %s",
21650 pulongest (DW_UNSND (&die->attrs[i])));
21651 break;
21652 case DW_FORM_ref_sig8:
21653 fprintf_unfiltered (f, "signature: %s",
21654 hex_string (DW_SIGNATURE (&die->attrs[i])));
21655 break;
21656 case DW_FORM_string:
21657 case DW_FORM_strp:
21658 case DW_FORM_line_strp:
21659 case DW_FORM_strx:
21660 case DW_FORM_GNU_str_index:
21661 case DW_FORM_GNU_strp_alt:
21662 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21663 DW_STRING (&die->attrs[i])
21664 ? DW_STRING (&die->attrs[i]) : "",
21665 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21666 break;
21667 case DW_FORM_flag:
21668 if (DW_UNSND (&die->attrs[i]))
21669 fprintf_unfiltered (f, "flag: TRUE");
21670 else
21671 fprintf_unfiltered (f, "flag: FALSE");
21672 break;
21673 case DW_FORM_flag_present:
21674 fprintf_unfiltered (f, "flag: TRUE");
21675 break;
21676 case DW_FORM_indirect:
21677 /* The reader will have reduced the indirect form to
21678 the "base form" so this form should not occur. */
21679 fprintf_unfiltered (f,
21680 "unexpected attribute form: DW_FORM_indirect");
21681 break;
21682 case DW_FORM_implicit_const:
21683 fprintf_unfiltered (f, "constant: %s",
21684 plongest (DW_SND (&die->attrs[i])));
21685 break;
21686 default:
21687 fprintf_unfiltered (f, "unsupported attribute form: %d.",
21688 die->attrs[i].form);
21689 break;
21690 }
21691 fprintf_unfiltered (f, "\n");
21692 }
21693 }
21694
21695 static void
21696 dump_die_for_error (struct die_info *die)
21697 {
21698 dump_die_shallow (gdb_stderr, 0, die);
21699 }
21700
21701 static void
21702 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21703 {
21704 int indent = level * 4;
21705
21706 gdb_assert (die != NULL);
21707
21708 if (level >= max_level)
21709 return;
21710
21711 dump_die_shallow (f, indent, die);
21712
21713 if (die->child != NULL)
21714 {
21715 print_spaces (indent, f);
21716 fprintf_unfiltered (f, " Children:");
21717 if (level + 1 < max_level)
21718 {
21719 fprintf_unfiltered (f, "\n");
21720 dump_die_1 (f, level + 1, max_level, die->child);
21721 }
21722 else
21723 {
21724 fprintf_unfiltered (f,
21725 " [not printed, max nesting level reached]\n");
21726 }
21727 }
21728
21729 if (die->sibling != NULL && level > 0)
21730 {
21731 dump_die_1 (f, level, max_level, die->sibling);
21732 }
21733 }
21734
21735 /* This is called from the pdie macro in gdbinit.in.
21736 It's not static so gcc will keep a copy callable from gdb. */
21737
21738 void
21739 dump_die (struct die_info *die, int max_level)
21740 {
21741 dump_die_1 (gdb_stdlog, 0, max_level, die);
21742 }
21743
21744 static void
21745 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
21746 {
21747 void **slot;
21748
21749 slot = htab_find_slot_with_hash (cu->die_hash, die,
21750 to_underlying (die->sect_off),
21751 INSERT);
21752
21753 *slot = die;
21754 }
21755
21756 /* Follow reference or signature attribute ATTR of SRC_DIE.
21757 On entry *REF_CU is the CU of SRC_DIE.
21758 On exit *REF_CU is the CU of the result. */
21759
21760 static struct die_info *
21761 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
21762 struct dwarf2_cu **ref_cu)
21763 {
21764 struct die_info *die;
21765
21766 if (attr->form_is_ref ())
21767 die = follow_die_ref (src_die, attr, ref_cu);
21768 else if (attr->form == DW_FORM_ref_sig8)
21769 die = follow_die_sig (src_die, attr, ref_cu);
21770 else
21771 {
21772 dump_die_for_error (src_die);
21773 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
21774 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
21775 }
21776
21777 return die;
21778 }
21779
21780 /* Follow reference OFFSET.
21781 On entry *REF_CU is the CU of the source die referencing OFFSET.
21782 On exit *REF_CU is the CU of the result.
21783 Returns NULL if OFFSET is invalid. */
21784
21785 static struct die_info *
21786 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
21787 struct dwarf2_cu **ref_cu)
21788 {
21789 struct die_info temp_die;
21790 struct dwarf2_cu *target_cu, *cu = *ref_cu;
21791 struct dwarf2_per_objfile *dwarf2_per_objfile
21792 = cu->per_cu->dwarf2_per_objfile;
21793
21794 gdb_assert (cu->per_cu != NULL);
21795
21796 target_cu = cu;
21797
21798 if (cu->per_cu->is_debug_types)
21799 {
21800 /* .debug_types CUs cannot reference anything outside their CU.
21801 If they need to, they have to reference a signatured type via
21802 DW_FORM_ref_sig8. */
21803 if (!cu->header.offset_in_cu_p (sect_off))
21804 return NULL;
21805 }
21806 else if (offset_in_dwz != cu->per_cu->is_dwz
21807 || !cu->header.offset_in_cu_p (sect_off))
21808 {
21809 struct dwarf2_per_cu_data *per_cu;
21810
21811 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
21812 dwarf2_per_objfile);
21813
21814 /* If necessary, add it to the queue and load its DIEs. */
21815 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
21816 load_full_comp_unit (per_cu, false, cu->language);
21817
21818 target_cu = per_cu->cu;
21819 }
21820 else if (cu->dies == NULL)
21821 {
21822 /* We're loading full DIEs during partial symbol reading. */
21823 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
21824 load_full_comp_unit (cu->per_cu, false, language_minimal);
21825 }
21826
21827 *ref_cu = target_cu;
21828 temp_die.sect_off = sect_off;
21829
21830 if (target_cu != cu)
21831 target_cu->ancestor = cu;
21832
21833 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
21834 &temp_die,
21835 to_underlying (sect_off));
21836 }
21837
21838 /* Follow reference attribute ATTR of SRC_DIE.
21839 On entry *REF_CU is the CU of SRC_DIE.
21840 On exit *REF_CU is the CU of the result. */
21841
21842 static struct die_info *
21843 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
21844 struct dwarf2_cu **ref_cu)
21845 {
21846 sect_offset sect_off = attr->get_ref_die_offset ();
21847 struct dwarf2_cu *cu = *ref_cu;
21848 struct die_info *die;
21849
21850 die = follow_die_offset (sect_off,
21851 (attr->form == DW_FORM_GNU_ref_alt
21852 || cu->per_cu->is_dwz),
21853 ref_cu);
21854 if (!die)
21855 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
21856 "at %s [in module %s]"),
21857 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
21858 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
21859
21860 return die;
21861 }
21862
21863 /* See read.h. */
21864
21865 struct dwarf2_locexpr_baton
21866 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
21867 dwarf2_per_cu_data *per_cu,
21868 CORE_ADDR (*get_frame_pc) (void *baton),
21869 void *baton, bool resolve_abstract_p)
21870 {
21871 struct dwarf2_cu *cu;
21872 struct die_info *die;
21873 struct attribute *attr;
21874 struct dwarf2_locexpr_baton retval;
21875 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
21876 struct objfile *objfile = dwarf2_per_objfile->objfile;
21877
21878 if (per_cu->cu == NULL)
21879 load_cu (per_cu, false);
21880 cu = per_cu->cu;
21881 if (cu == NULL)
21882 {
21883 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21884 Instead just throw an error, not much else we can do. */
21885 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21886 sect_offset_str (sect_off), objfile_name (objfile));
21887 }
21888
21889 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21890 if (!die)
21891 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21892 sect_offset_str (sect_off), objfile_name (objfile));
21893
21894 attr = dwarf2_attr (die, DW_AT_location, cu);
21895 if (!attr && resolve_abstract_p
21896 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
21897 != dwarf2_per_objfile->abstract_to_concrete.end ()))
21898 {
21899 CORE_ADDR pc = (*get_frame_pc) (baton);
21900 CORE_ADDR baseaddr = objfile->text_section_offset ();
21901 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21902
21903 for (const auto &cand_off
21904 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
21905 {
21906 struct dwarf2_cu *cand_cu = cu;
21907 struct die_info *cand
21908 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
21909 if (!cand
21910 || !cand->parent
21911 || cand->parent->tag != DW_TAG_subprogram)
21912 continue;
21913
21914 CORE_ADDR pc_low, pc_high;
21915 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
21916 if (pc_low == ((CORE_ADDR) -1))
21917 continue;
21918 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
21919 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
21920 if (!(pc_low <= pc && pc < pc_high))
21921 continue;
21922
21923 die = cand;
21924 attr = dwarf2_attr (die, DW_AT_location, cu);
21925 break;
21926 }
21927 }
21928
21929 if (!attr)
21930 {
21931 /* DWARF: "If there is no such attribute, then there is no effect.".
21932 DATA is ignored if SIZE is 0. */
21933
21934 retval.data = NULL;
21935 retval.size = 0;
21936 }
21937 else if (attr->form_is_section_offset ())
21938 {
21939 struct dwarf2_loclist_baton loclist_baton;
21940 CORE_ADDR pc = (*get_frame_pc) (baton);
21941 size_t size;
21942
21943 fill_in_loclist_baton (cu, &loclist_baton, attr);
21944
21945 retval.data = dwarf2_find_location_expression (&loclist_baton,
21946 &size, pc);
21947 retval.size = size;
21948 }
21949 else
21950 {
21951 if (!attr->form_is_block ())
21952 error (_("Dwarf Error: DIE at %s referenced in module %s "
21953 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
21954 sect_offset_str (sect_off), objfile_name (objfile));
21955
21956 retval.data = DW_BLOCK (attr)->data;
21957 retval.size = DW_BLOCK (attr)->size;
21958 }
21959 retval.per_cu = cu->per_cu;
21960
21961 age_cached_comp_units (dwarf2_per_objfile);
21962
21963 return retval;
21964 }
21965
21966 /* See read.h. */
21967
21968 struct dwarf2_locexpr_baton
21969 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
21970 dwarf2_per_cu_data *per_cu,
21971 CORE_ADDR (*get_frame_pc) (void *baton),
21972 void *baton)
21973 {
21974 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
21975
21976 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
21977 }
21978
21979 /* Write a constant of a given type as target-ordered bytes into
21980 OBSTACK. */
21981
21982 static const gdb_byte *
21983 write_constant_as_bytes (struct obstack *obstack,
21984 enum bfd_endian byte_order,
21985 struct type *type,
21986 ULONGEST value,
21987 LONGEST *len)
21988 {
21989 gdb_byte *result;
21990
21991 *len = TYPE_LENGTH (type);
21992 result = (gdb_byte *) obstack_alloc (obstack, *len);
21993 store_unsigned_integer (result, *len, byte_order, value);
21994
21995 return result;
21996 }
21997
21998 /* See read.h. */
21999
22000 const gdb_byte *
22001 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22002 dwarf2_per_cu_data *per_cu,
22003 obstack *obstack,
22004 LONGEST *len)
22005 {
22006 struct dwarf2_cu *cu;
22007 struct die_info *die;
22008 struct attribute *attr;
22009 const gdb_byte *result = NULL;
22010 struct type *type;
22011 LONGEST value;
22012 enum bfd_endian byte_order;
22013 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22014
22015 if (per_cu->cu == NULL)
22016 load_cu (per_cu, false);
22017 cu = per_cu->cu;
22018 if (cu == NULL)
22019 {
22020 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22021 Instead just throw an error, not much else we can do. */
22022 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22023 sect_offset_str (sect_off), objfile_name (objfile));
22024 }
22025
22026 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22027 if (!die)
22028 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22029 sect_offset_str (sect_off), objfile_name (objfile));
22030
22031 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22032 if (attr == NULL)
22033 return NULL;
22034
22035 byte_order = (bfd_big_endian (objfile->obfd)
22036 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22037
22038 switch (attr->form)
22039 {
22040 case DW_FORM_addr:
22041 case DW_FORM_addrx:
22042 case DW_FORM_GNU_addr_index:
22043 {
22044 gdb_byte *tem;
22045
22046 *len = cu->header.addr_size;
22047 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22048 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22049 result = tem;
22050 }
22051 break;
22052 case DW_FORM_string:
22053 case DW_FORM_strp:
22054 case DW_FORM_strx:
22055 case DW_FORM_GNU_str_index:
22056 case DW_FORM_GNU_strp_alt:
22057 /* DW_STRING is already allocated on the objfile obstack, point
22058 directly to it. */
22059 result = (const gdb_byte *) DW_STRING (attr);
22060 *len = strlen (DW_STRING (attr));
22061 break;
22062 case DW_FORM_block1:
22063 case DW_FORM_block2:
22064 case DW_FORM_block4:
22065 case DW_FORM_block:
22066 case DW_FORM_exprloc:
22067 case DW_FORM_data16:
22068 result = DW_BLOCK (attr)->data;
22069 *len = DW_BLOCK (attr)->size;
22070 break;
22071
22072 /* The DW_AT_const_value attributes are supposed to carry the
22073 symbol's value "represented as it would be on the target
22074 architecture." By the time we get here, it's already been
22075 converted to host endianness, so we just need to sign- or
22076 zero-extend it as appropriate. */
22077 case DW_FORM_data1:
22078 type = die_type (die, cu);
22079 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22080 if (result == NULL)
22081 result = write_constant_as_bytes (obstack, byte_order,
22082 type, value, len);
22083 break;
22084 case DW_FORM_data2:
22085 type = die_type (die, cu);
22086 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22087 if (result == NULL)
22088 result = write_constant_as_bytes (obstack, byte_order,
22089 type, value, len);
22090 break;
22091 case DW_FORM_data4:
22092 type = die_type (die, cu);
22093 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22094 if (result == NULL)
22095 result = write_constant_as_bytes (obstack, byte_order,
22096 type, value, len);
22097 break;
22098 case DW_FORM_data8:
22099 type = die_type (die, cu);
22100 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22101 if (result == NULL)
22102 result = write_constant_as_bytes (obstack, byte_order,
22103 type, value, len);
22104 break;
22105
22106 case DW_FORM_sdata:
22107 case DW_FORM_implicit_const:
22108 type = die_type (die, cu);
22109 result = write_constant_as_bytes (obstack, byte_order,
22110 type, DW_SND (attr), len);
22111 break;
22112
22113 case DW_FORM_udata:
22114 type = die_type (die, cu);
22115 result = write_constant_as_bytes (obstack, byte_order,
22116 type, DW_UNSND (attr), len);
22117 break;
22118
22119 default:
22120 complaint (_("unsupported const value attribute form: '%s'"),
22121 dwarf_form_name (attr->form));
22122 break;
22123 }
22124
22125 return result;
22126 }
22127
22128 /* See read.h. */
22129
22130 struct type *
22131 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22132 dwarf2_per_cu_data *per_cu)
22133 {
22134 struct dwarf2_cu *cu;
22135 struct die_info *die;
22136
22137 if (per_cu->cu == NULL)
22138 load_cu (per_cu, false);
22139 cu = per_cu->cu;
22140 if (!cu)
22141 return NULL;
22142
22143 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22144 if (!die)
22145 return NULL;
22146
22147 return die_type (die, cu);
22148 }
22149
22150 /* See read.h. */
22151
22152 struct type *
22153 dwarf2_get_die_type (cu_offset die_offset,
22154 struct dwarf2_per_cu_data *per_cu)
22155 {
22156 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22157 return get_die_type_at_offset (die_offset_sect, per_cu);
22158 }
22159
22160 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22161 On entry *REF_CU is the CU of SRC_DIE.
22162 On exit *REF_CU is the CU of the result.
22163 Returns NULL if the referenced DIE isn't found. */
22164
22165 static struct die_info *
22166 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22167 struct dwarf2_cu **ref_cu)
22168 {
22169 struct die_info temp_die;
22170 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22171 struct die_info *die;
22172
22173 /* While it might be nice to assert sig_type->type == NULL here,
22174 we can get here for DW_AT_imported_declaration where we need
22175 the DIE not the type. */
22176
22177 /* If necessary, add it to the queue and load its DIEs. */
22178
22179 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22180 read_signatured_type (sig_type);
22181
22182 sig_cu = sig_type->per_cu.cu;
22183 gdb_assert (sig_cu != NULL);
22184 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22185 temp_die.sect_off = sig_type->type_offset_in_section;
22186 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22187 to_underlying (temp_die.sect_off));
22188 if (die)
22189 {
22190 struct dwarf2_per_objfile *dwarf2_per_objfile
22191 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22192
22193 /* For .gdb_index version 7 keep track of included TUs.
22194 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22195 if (dwarf2_per_objfile->index_table != NULL
22196 && dwarf2_per_objfile->index_table->version <= 7)
22197 {
22198 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22199 }
22200
22201 *ref_cu = sig_cu;
22202 if (sig_cu != cu)
22203 sig_cu->ancestor = cu;
22204
22205 return die;
22206 }
22207
22208 return NULL;
22209 }
22210
22211 /* Follow signatured type referenced by ATTR in SRC_DIE.
22212 On entry *REF_CU is the CU of SRC_DIE.
22213 On exit *REF_CU is the CU of the result.
22214 The result is the DIE of the type.
22215 If the referenced type cannot be found an error is thrown. */
22216
22217 static struct die_info *
22218 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22219 struct dwarf2_cu **ref_cu)
22220 {
22221 ULONGEST signature = DW_SIGNATURE (attr);
22222 struct signatured_type *sig_type;
22223 struct die_info *die;
22224
22225 gdb_assert (attr->form == DW_FORM_ref_sig8);
22226
22227 sig_type = lookup_signatured_type (*ref_cu, signature);
22228 /* sig_type will be NULL if the signatured type is missing from
22229 the debug info. */
22230 if (sig_type == NULL)
22231 {
22232 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22233 " from DIE at %s [in module %s]"),
22234 hex_string (signature), sect_offset_str (src_die->sect_off),
22235 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22236 }
22237
22238 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22239 if (die == NULL)
22240 {
22241 dump_die_for_error (src_die);
22242 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22243 " from DIE at %s [in module %s]"),
22244 hex_string (signature), sect_offset_str (src_die->sect_off),
22245 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22246 }
22247
22248 return die;
22249 }
22250
22251 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22252 reading in and processing the type unit if necessary. */
22253
22254 static struct type *
22255 get_signatured_type (struct die_info *die, ULONGEST signature,
22256 struct dwarf2_cu *cu)
22257 {
22258 struct dwarf2_per_objfile *dwarf2_per_objfile
22259 = cu->per_cu->dwarf2_per_objfile;
22260 struct signatured_type *sig_type;
22261 struct dwarf2_cu *type_cu;
22262 struct die_info *type_die;
22263 struct type *type;
22264
22265 sig_type = lookup_signatured_type (cu, signature);
22266 /* sig_type will be NULL if the signatured type is missing from
22267 the debug info. */
22268 if (sig_type == NULL)
22269 {
22270 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22271 " from DIE at %s [in module %s]"),
22272 hex_string (signature), sect_offset_str (die->sect_off),
22273 objfile_name (dwarf2_per_objfile->objfile));
22274 return build_error_marker_type (cu, die);
22275 }
22276
22277 /* If we already know the type we're done. */
22278 if (sig_type->type != NULL)
22279 return sig_type->type;
22280
22281 type_cu = cu;
22282 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22283 if (type_die != NULL)
22284 {
22285 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22286 is created. This is important, for example, because for c++ classes
22287 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22288 type = read_type_die (type_die, type_cu);
22289 if (type == NULL)
22290 {
22291 complaint (_("Dwarf Error: Cannot build signatured type %s"
22292 " referenced from DIE at %s [in module %s]"),
22293 hex_string (signature), sect_offset_str (die->sect_off),
22294 objfile_name (dwarf2_per_objfile->objfile));
22295 type = build_error_marker_type (cu, die);
22296 }
22297 }
22298 else
22299 {
22300 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22301 " from DIE at %s [in module %s]"),
22302 hex_string (signature), sect_offset_str (die->sect_off),
22303 objfile_name (dwarf2_per_objfile->objfile));
22304 type = build_error_marker_type (cu, die);
22305 }
22306 sig_type->type = type;
22307
22308 return type;
22309 }
22310
22311 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22312 reading in and processing the type unit if necessary. */
22313
22314 static struct type *
22315 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22316 struct dwarf2_cu *cu) /* ARI: editCase function */
22317 {
22318 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22319 if (attr->form_is_ref ())
22320 {
22321 struct dwarf2_cu *type_cu = cu;
22322 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22323
22324 return read_type_die (type_die, type_cu);
22325 }
22326 else if (attr->form == DW_FORM_ref_sig8)
22327 {
22328 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22329 }
22330 else
22331 {
22332 struct dwarf2_per_objfile *dwarf2_per_objfile
22333 = cu->per_cu->dwarf2_per_objfile;
22334
22335 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22336 " at %s [in module %s]"),
22337 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22338 objfile_name (dwarf2_per_objfile->objfile));
22339 return build_error_marker_type (cu, die);
22340 }
22341 }
22342
22343 /* Load the DIEs associated with type unit PER_CU into memory. */
22344
22345 static void
22346 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22347 {
22348 struct signatured_type *sig_type;
22349
22350 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22351 gdb_assert (! per_cu->type_unit_group_p ());
22352
22353 /* We have the per_cu, but we need the signatured_type.
22354 Fortunately this is an easy translation. */
22355 gdb_assert (per_cu->is_debug_types);
22356 sig_type = (struct signatured_type *) per_cu;
22357
22358 gdb_assert (per_cu->cu == NULL);
22359
22360 read_signatured_type (sig_type);
22361
22362 gdb_assert (per_cu->cu != NULL);
22363 }
22364
22365 /* Read in a signatured type and build its CU and DIEs.
22366 If the type is a stub for the real type in a DWO file,
22367 read in the real type from the DWO file as well. */
22368
22369 static void
22370 read_signatured_type (struct signatured_type *sig_type)
22371 {
22372 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22373
22374 gdb_assert (per_cu->is_debug_types);
22375 gdb_assert (per_cu->cu == NULL);
22376
22377 cutu_reader reader (per_cu, NULL, 0, false);
22378
22379 if (!reader.dummy_p)
22380 {
22381 struct dwarf2_cu *cu = reader.cu;
22382 const gdb_byte *info_ptr = reader.info_ptr;
22383
22384 gdb_assert (cu->die_hash == NULL);
22385 cu->die_hash =
22386 htab_create_alloc_ex (cu->header.length / 12,
22387 die_hash,
22388 die_eq,
22389 NULL,
22390 &cu->comp_unit_obstack,
22391 hashtab_obstack_allocate,
22392 dummy_obstack_deallocate);
22393
22394 if (reader.comp_unit_die->has_children)
22395 reader.comp_unit_die->child
22396 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22397 reader.comp_unit_die);
22398 cu->dies = reader.comp_unit_die;
22399 /* comp_unit_die is not stored in die_hash, no need. */
22400
22401 /* We try not to read any attributes in this function, because
22402 not all CUs needed for references have been loaded yet, and
22403 symbol table processing isn't initialized. But we have to
22404 set the CU language, or we won't be able to build types
22405 correctly. Similarly, if we do not read the producer, we can
22406 not apply producer-specific interpretation. */
22407 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22408
22409 reader.keep ();
22410 }
22411
22412 sig_type->per_cu.tu_read = 1;
22413 }
22414
22415 /* Decode simple location descriptions.
22416 Given a pointer to a dwarf block that defines a location, compute
22417 the location and return the value.
22418
22419 NOTE drow/2003-11-18: This function is called in two situations
22420 now: for the address of static or global variables (partial symbols
22421 only) and for offsets into structures which are expected to be
22422 (more or less) constant. The partial symbol case should go away,
22423 and only the constant case should remain. That will let this
22424 function complain more accurately. A few special modes are allowed
22425 without complaint for global variables (for instance, global
22426 register values and thread-local values).
22427
22428 A location description containing no operations indicates that the
22429 object is optimized out. The return value is 0 for that case.
22430 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22431 callers will only want a very basic result and this can become a
22432 complaint.
22433
22434 Note that stack[0] is unused except as a default error return. */
22435
22436 static CORE_ADDR
22437 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22438 {
22439 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22440 size_t i;
22441 size_t size = blk->size;
22442 const gdb_byte *data = blk->data;
22443 CORE_ADDR stack[64];
22444 int stacki;
22445 unsigned int bytes_read, unsnd;
22446 gdb_byte op;
22447
22448 i = 0;
22449 stacki = 0;
22450 stack[stacki] = 0;
22451 stack[++stacki] = 0;
22452
22453 while (i < size)
22454 {
22455 op = data[i++];
22456 switch (op)
22457 {
22458 case DW_OP_lit0:
22459 case DW_OP_lit1:
22460 case DW_OP_lit2:
22461 case DW_OP_lit3:
22462 case DW_OP_lit4:
22463 case DW_OP_lit5:
22464 case DW_OP_lit6:
22465 case DW_OP_lit7:
22466 case DW_OP_lit8:
22467 case DW_OP_lit9:
22468 case DW_OP_lit10:
22469 case DW_OP_lit11:
22470 case DW_OP_lit12:
22471 case DW_OP_lit13:
22472 case DW_OP_lit14:
22473 case DW_OP_lit15:
22474 case DW_OP_lit16:
22475 case DW_OP_lit17:
22476 case DW_OP_lit18:
22477 case DW_OP_lit19:
22478 case DW_OP_lit20:
22479 case DW_OP_lit21:
22480 case DW_OP_lit22:
22481 case DW_OP_lit23:
22482 case DW_OP_lit24:
22483 case DW_OP_lit25:
22484 case DW_OP_lit26:
22485 case DW_OP_lit27:
22486 case DW_OP_lit28:
22487 case DW_OP_lit29:
22488 case DW_OP_lit30:
22489 case DW_OP_lit31:
22490 stack[++stacki] = op - DW_OP_lit0;
22491 break;
22492
22493 case DW_OP_reg0:
22494 case DW_OP_reg1:
22495 case DW_OP_reg2:
22496 case DW_OP_reg3:
22497 case DW_OP_reg4:
22498 case DW_OP_reg5:
22499 case DW_OP_reg6:
22500 case DW_OP_reg7:
22501 case DW_OP_reg8:
22502 case DW_OP_reg9:
22503 case DW_OP_reg10:
22504 case DW_OP_reg11:
22505 case DW_OP_reg12:
22506 case DW_OP_reg13:
22507 case DW_OP_reg14:
22508 case DW_OP_reg15:
22509 case DW_OP_reg16:
22510 case DW_OP_reg17:
22511 case DW_OP_reg18:
22512 case DW_OP_reg19:
22513 case DW_OP_reg20:
22514 case DW_OP_reg21:
22515 case DW_OP_reg22:
22516 case DW_OP_reg23:
22517 case DW_OP_reg24:
22518 case DW_OP_reg25:
22519 case DW_OP_reg26:
22520 case DW_OP_reg27:
22521 case DW_OP_reg28:
22522 case DW_OP_reg29:
22523 case DW_OP_reg30:
22524 case DW_OP_reg31:
22525 stack[++stacki] = op - DW_OP_reg0;
22526 if (i < size)
22527 dwarf2_complex_location_expr_complaint ();
22528 break;
22529
22530 case DW_OP_regx:
22531 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22532 i += bytes_read;
22533 stack[++stacki] = unsnd;
22534 if (i < size)
22535 dwarf2_complex_location_expr_complaint ();
22536 break;
22537
22538 case DW_OP_addr:
22539 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22540 &bytes_read);
22541 i += bytes_read;
22542 break;
22543
22544 case DW_OP_const1u:
22545 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22546 i += 1;
22547 break;
22548
22549 case DW_OP_const1s:
22550 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22551 i += 1;
22552 break;
22553
22554 case DW_OP_const2u:
22555 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22556 i += 2;
22557 break;
22558
22559 case DW_OP_const2s:
22560 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22561 i += 2;
22562 break;
22563
22564 case DW_OP_const4u:
22565 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22566 i += 4;
22567 break;
22568
22569 case DW_OP_const4s:
22570 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22571 i += 4;
22572 break;
22573
22574 case DW_OP_const8u:
22575 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22576 i += 8;
22577 break;
22578
22579 case DW_OP_constu:
22580 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22581 &bytes_read);
22582 i += bytes_read;
22583 break;
22584
22585 case DW_OP_consts:
22586 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22587 i += bytes_read;
22588 break;
22589
22590 case DW_OP_dup:
22591 stack[stacki + 1] = stack[stacki];
22592 stacki++;
22593 break;
22594
22595 case DW_OP_plus:
22596 stack[stacki - 1] += stack[stacki];
22597 stacki--;
22598 break;
22599
22600 case DW_OP_plus_uconst:
22601 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22602 &bytes_read);
22603 i += bytes_read;
22604 break;
22605
22606 case DW_OP_minus:
22607 stack[stacki - 1] -= stack[stacki];
22608 stacki--;
22609 break;
22610
22611 case DW_OP_deref:
22612 /* If we're not the last op, then we definitely can't encode
22613 this using GDB's address_class enum. This is valid for partial
22614 global symbols, although the variable's address will be bogus
22615 in the psymtab. */
22616 if (i < size)
22617 dwarf2_complex_location_expr_complaint ();
22618 break;
22619
22620 case DW_OP_GNU_push_tls_address:
22621 case DW_OP_form_tls_address:
22622 /* The top of the stack has the offset from the beginning
22623 of the thread control block at which the variable is located. */
22624 /* Nothing should follow this operator, so the top of stack would
22625 be returned. */
22626 /* This is valid for partial global symbols, but the variable's
22627 address will be bogus in the psymtab. Make it always at least
22628 non-zero to not look as a variable garbage collected by linker
22629 which have DW_OP_addr 0. */
22630 if (i < size)
22631 dwarf2_complex_location_expr_complaint ();
22632 stack[stacki]++;
22633 break;
22634
22635 case DW_OP_GNU_uninit:
22636 break;
22637
22638 case DW_OP_addrx:
22639 case DW_OP_GNU_addr_index:
22640 case DW_OP_GNU_const_index:
22641 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22642 &bytes_read);
22643 i += bytes_read;
22644 break;
22645
22646 default:
22647 {
22648 const char *name = get_DW_OP_name (op);
22649
22650 if (name)
22651 complaint (_("unsupported stack op: '%s'"),
22652 name);
22653 else
22654 complaint (_("unsupported stack op: '%02x'"),
22655 op);
22656 }
22657
22658 return (stack[stacki]);
22659 }
22660
22661 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22662 outside of the allocated space. Also enforce minimum>0. */
22663 if (stacki >= ARRAY_SIZE (stack) - 1)
22664 {
22665 complaint (_("location description stack overflow"));
22666 return 0;
22667 }
22668
22669 if (stacki <= 0)
22670 {
22671 complaint (_("location description stack underflow"));
22672 return 0;
22673 }
22674 }
22675 return (stack[stacki]);
22676 }
22677
22678 /* memory allocation interface */
22679
22680 static struct dwarf_block *
22681 dwarf_alloc_block (struct dwarf2_cu *cu)
22682 {
22683 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
22684 }
22685
22686 static struct die_info *
22687 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
22688 {
22689 struct die_info *die;
22690 size_t size = sizeof (struct die_info);
22691
22692 if (num_attrs > 1)
22693 size += (num_attrs - 1) * sizeof (struct attribute);
22694
22695 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
22696 memset (die, 0, sizeof (struct die_info));
22697 return (die);
22698 }
22699
22700 \f
22701
22702 /* Macro support. */
22703
22704 /* An overload of dwarf_decode_macros that finds the correct section
22705 and ensures it is read in before calling the other overload. */
22706
22707 static void
22708 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22709 int section_is_gnu)
22710 {
22711 struct dwarf2_per_objfile *dwarf2_per_objfile
22712 = cu->per_cu->dwarf2_per_objfile;
22713 struct objfile *objfile = dwarf2_per_objfile->objfile;
22714 const struct line_header *lh = cu->line_header;
22715 unsigned int offset_size = cu->header.offset_size;
22716 struct dwarf2_section_info *section;
22717 const char *section_name;
22718
22719 if (cu->dwo_unit != nullptr)
22720 {
22721 if (section_is_gnu)
22722 {
22723 section = &cu->dwo_unit->dwo_file->sections.macro;
22724 section_name = ".debug_macro.dwo";
22725 }
22726 else
22727 {
22728 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22729 section_name = ".debug_macinfo.dwo";
22730 }
22731 }
22732 else
22733 {
22734 if (section_is_gnu)
22735 {
22736 section = &dwarf2_per_objfile->macro;
22737 section_name = ".debug_macro";
22738 }
22739 else
22740 {
22741 section = &dwarf2_per_objfile->macinfo;
22742 section_name = ".debug_macinfo";
22743 }
22744 }
22745
22746 section->read (objfile);
22747 if (section->buffer == nullptr)
22748 {
22749 complaint (_("missing %s section"), section_name);
22750 return;
22751 }
22752
22753 buildsym_compunit *builder = cu->get_builder ();
22754
22755 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
22756 offset_size, offset, section_is_gnu);
22757 }
22758
22759 /* Return the .debug_loc section to use for CU.
22760 For DWO files use .debug_loc.dwo. */
22761
22762 static struct dwarf2_section_info *
22763 cu_debug_loc_section (struct dwarf2_cu *cu)
22764 {
22765 struct dwarf2_per_objfile *dwarf2_per_objfile
22766 = cu->per_cu->dwarf2_per_objfile;
22767
22768 if (cu->dwo_unit)
22769 {
22770 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22771
22772 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22773 }
22774 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22775 : &dwarf2_per_objfile->loc);
22776 }
22777
22778 /* A helper function that fills in a dwarf2_loclist_baton. */
22779
22780 static void
22781 fill_in_loclist_baton (struct dwarf2_cu *cu,
22782 struct dwarf2_loclist_baton *baton,
22783 const struct attribute *attr)
22784 {
22785 struct dwarf2_per_objfile *dwarf2_per_objfile
22786 = cu->per_cu->dwarf2_per_objfile;
22787 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22788
22789 section->read (dwarf2_per_objfile->objfile);
22790
22791 baton->per_cu = cu->per_cu;
22792 gdb_assert (baton->per_cu);
22793 /* We don't know how long the location list is, but make sure we
22794 don't run off the edge of the section. */
22795 baton->size = section->size - DW_UNSND (attr);
22796 baton->data = section->buffer + DW_UNSND (attr);
22797 if (cu->base_address.has_value ())
22798 baton->base_address = *cu->base_address;
22799 else
22800 baton->base_address = 0;
22801 baton->from_dwo = cu->dwo_unit != NULL;
22802 }
22803
22804 static void
22805 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
22806 struct dwarf2_cu *cu, int is_block)
22807 {
22808 struct dwarf2_per_objfile *dwarf2_per_objfile
22809 = cu->per_cu->dwarf2_per_objfile;
22810 struct objfile *objfile = dwarf2_per_objfile->objfile;
22811 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22812
22813 if (attr->form_is_section_offset ()
22814 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
22815 the section. If so, fall through to the complaint in the
22816 other branch. */
22817 && DW_UNSND (attr) < section->get_size (objfile))
22818 {
22819 struct dwarf2_loclist_baton *baton;
22820
22821 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
22822
22823 fill_in_loclist_baton (cu, baton, attr);
22824
22825 if (!cu->base_address.has_value ())
22826 complaint (_("Location list used without "
22827 "specifying the CU base address."));
22828
22829 SYMBOL_ACLASS_INDEX (sym) = (is_block
22830 ? dwarf2_loclist_block_index
22831 : dwarf2_loclist_index);
22832 SYMBOL_LOCATION_BATON (sym) = baton;
22833 }
22834 else
22835 {
22836 struct dwarf2_locexpr_baton *baton;
22837
22838 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
22839 baton->per_cu = cu->per_cu;
22840 gdb_assert (baton->per_cu);
22841
22842 if (attr->form_is_block ())
22843 {
22844 /* Note that we're just copying the block's data pointer
22845 here, not the actual data. We're still pointing into the
22846 info_buffer for SYM's objfile; right now we never release
22847 that buffer, but when we do clean up properly this may
22848 need to change. */
22849 baton->size = DW_BLOCK (attr)->size;
22850 baton->data = DW_BLOCK (attr)->data;
22851 }
22852 else
22853 {
22854 dwarf2_invalid_attrib_class_complaint ("location description",
22855 sym->natural_name ());
22856 baton->size = 0;
22857 }
22858
22859 SYMBOL_ACLASS_INDEX (sym) = (is_block
22860 ? dwarf2_locexpr_block_index
22861 : dwarf2_locexpr_index);
22862 SYMBOL_LOCATION_BATON (sym) = baton;
22863 }
22864 }
22865
22866 /* See read.h. */
22867
22868 struct objfile *
22869 dwarf2_per_cu_data::objfile () const
22870 {
22871 struct objfile *objfile = dwarf2_per_objfile->objfile;
22872
22873 /* Return the master objfile, so that we can report and look up the
22874 correct file containing this variable. */
22875 if (objfile->separate_debug_objfile_backlink)
22876 objfile = objfile->separate_debug_objfile_backlink;
22877
22878 return objfile;
22879 }
22880
22881 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22882 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22883 CU_HEADERP first. */
22884
22885 static const struct comp_unit_head *
22886 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22887 const struct dwarf2_per_cu_data *per_cu)
22888 {
22889 const gdb_byte *info_ptr;
22890
22891 if (per_cu->cu)
22892 return &per_cu->cu->header;
22893
22894 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
22895
22896 memset (cu_headerp, 0, sizeof (*cu_headerp));
22897 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22898 rcuh_kind::COMPILE);
22899
22900 return cu_headerp;
22901 }
22902
22903 /* See read.h. */
22904
22905 int
22906 dwarf2_per_cu_data::addr_size () const
22907 {
22908 struct comp_unit_head cu_header_local;
22909 const struct comp_unit_head *cu_headerp;
22910
22911 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22912
22913 return cu_headerp->addr_size;
22914 }
22915
22916 /* See read.h. */
22917
22918 int
22919 dwarf2_per_cu_data::offset_size () const
22920 {
22921 struct comp_unit_head cu_header_local;
22922 const struct comp_unit_head *cu_headerp;
22923
22924 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22925
22926 return cu_headerp->offset_size;
22927 }
22928
22929 /* See read.h. */
22930
22931 int
22932 dwarf2_per_cu_data::ref_addr_size () const
22933 {
22934 struct comp_unit_head cu_header_local;
22935 const struct comp_unit_head *cu_headerp;
22936
22937 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22938
22939 if (cu_headerp->version == 2)
22940 return cu_headerp->addr_size;
22941 else
22942 return cu_headerp->offset_size;
22943 }
22944
22945 /* See read.h. */
22946
22947 CORE_ADDR
22948 dwarf2_per_cu_data::text_offset () const
22949 {
22950 struct objfile *objfile = dwarf2_per_objfile->objfile;
22951
22952 return objfile->text_section_offset ();
22953 }
22954
22955 /* See read.h. */
22956
22957 struct type *
22958 dwarf2_per_cu_data::addr_type () const
22959 {
22960 struct objfile *objfile = dwarf2_per_objfile->objfile;
22961 struct type *void_type = objfile_type (objfile)->builtin_void;
22962 struct type *addr_type = lookup_pointer_type (void_type);
22963 int addr_size = this->addr_size ();
22964
22965 if (TYPE_LENGTH (addr_type) == addr_size)
22966 return addr_type;
22967
22968 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
22969 return addr_type;
22970 }
22971
22972 /* A helper function for dwarf2_find_containing_comp_unit that returns
22973 the index of the result, and that searches a vector. It will
22974 return a result even if the offset in question does not actually
22975 occur in any CU. This is separate so that it can be unit
22976 tested. */
22977
22978 static int
22979 dwarf2_find_containing_comp_unit
22980 (sect_offset sect_off,
22981 unsigned int offset_in_dwz,
22982 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
22983 {
22984 int low, high;
22985
22986 low = 0;
22987 high = all_comp_units.size () - 1;
22988 while (high > low)
22989 {
22990 struct dwarf2_per_cu_data *mid_cu;
22991 int mid = low + (high - low) / 2;
22992
22993 mid_cu = all_comp_units[mid];
22994 if (mid_cu->is_dwz > offset_in_dwz
22995 || (mid_cu->is_dwz == offset_in_dwz
22996 && mid_cu->sect_off + mid_cu->length > sect_off))
22997 high = mid;
22998 else
22999 low = mid + 1;
23000 }
23001 gdb_assert (low == high);
23002 return low;
23003 }
23004
23005 /* Locate the .debug_info compilation unit from CU's objfile which contains
23006 the DIE at OFFSET. Raises an error on failure. */
23007
23008 static struct dwarf2_per_cu_data *
23009 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23010 unsigned int offset_in_dwz,
23011 struct dwarf2_per_objfile *dwarf2_per_objfile)
23012 {
23013 int low
23014 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23015 dwarf2_per_objfile->all_comp_units);
23016 struct dwarf2_per_cu_data *this_cu
23017 = dwarf2_per_objfile->all_comp_units[low];
23018
23019 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23020 {
23021 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23022 error (_("Dwarf Error: could not find partial DIE containing "
23023 "offset %s [in module %s]"),
23024 sect_offset_str (sect_off),
23025 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23026
23027 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
23028 <= sect_off);
23029 return dwarf2_per_objfile->all_comp_units[low-1];
23030 }
23031 else
23032 {
23033 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
23034 && sect_off >= this_cu->sect_off + this_cu->length)
23035 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23036 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23037 return this_cu;
23038 }
23039 }
23040
23041 #if GDB_SELF_TEST
23042
23043 namespace selftests {
23044 namespace find_containing_comp_unit {
23045
23046 static void
23047 run_test ()
23048 {
23049 struct dwarf2_per_cu_data one {};
23050 struct dwarf2_per_cu_data two {};
23051 struct dwarf2_per_cu_data three {};
23052 struct dwarf2_per_cu_data four {};
23053
23054 one.length = 5;
23055 two.sect_off = sect_offset (one.length);
23056 two.length = 7;
23057
23058 three.length = 5;
23059 three.is_dwz = 1;
23060 four.sect_off = sect_offset (three.length);
23061 four.length = 7;
23062 four.is_dwz = 1;
23063
23064 std::vector<dwarf2_per_cu_data *> units;
23065 units.push_back (&one);
23066 units.push_back (&two);
23067 units.push_back (&three);
23068 units.push_back (&four);
23069
23070 int result;
23071
23072 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23073 SELF_CHECK (units[result] == &one);
23074 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23075 SELF_CHECK (units[result] == &one);
23076 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23077 SELF_CHECK (units[result] == &two);
23078
23079 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23080 SELF_CHECK (units[result] == &three);
23081 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23082 SELF_CHECK (units[result] == &three);
23083 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23084 SELF_CHECK (units[result] == &four);
23085 }
23086
23087 }
23088 }
23089
23090 #endif /* GDB_SELF_TEST */
23091
23092 /* Initialize dwarf2_cu CU, owned by PER_CU. */
23093
23094 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
23095 : per_cu (per_cu_),
23096 mark (false),
23097 has_loclist (false),
23098 checked_producer (false),
23099 producer_is_gxx_lt_4_6 (false),
23100 producer_is_gcc_lt_4_3 (false),
23101 producer_is_icc (false),
23102 producer_is_icc_lt_14 (false),
23103 producer_is_codewarrior (false),
23104 processing_has_namespace_info (false)
23105 {
23106 per_cu->cu = this;
23107 }
23108
23109 /* Destroy a dwarf2_cu. */
23110
23111 dwarf2_cu::~dwarf2_cu ()
23112 {
23113 per_cu->cu = NULL;
23114 }
23115
23116 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23117
23118 static void
23119 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23120 enum language pretend_language)
23121 {
23122 struct attribute *attr;
23123
23124 /* Set the language we're debugging. */
23125 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23126 if (attr != nullptr)
23127 set_cu_language (DW_UNSND (attr), cu);
23128 else
23129 {
23130 cu->language = pretend_language;
23131 cu->language_defn = language_def (cu->language);
23132 }
23133
23134 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23135 }
23136
23137 /* Increase the age counter on each cached compilation unit, and free
23138 any that are too old. */
23139
23140 static void
23141 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23142 {
23143 struct dwarf2_per_cu_data *per_cu, **last_chain;
23144
23145 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23146 per_cu = dwarf2_per_objfile->read_in_chain;
23147 while (per_cu != NULL)
23148 {
23149 per_cu->cu->last_used ++;
23150 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23151 dwarf2_mark (per_cu->cu);
23152 per_cu = per_cu->cu->read_in_chain;
23153 }
23154
23155 per_cu = dwarf2_per_objfile->read_in_chain;
23156 last_chain = &dwarf2_per_objfile->read_in_chain;
23157 while (per_cu != NULL)
23158 {
23159 struct dwarf2_per_cu_data *next_cu;
23160
23161 next_cu = per_cu->cu->read_in_chain;
23162
23163 if (!per_cu->cu->mark)
23164 {
23165 delete per_cu->cu;
23166 *last_chain = next_cu;
23167 }
23168 else
23169 last_chain = &per_cu->cu->read_in_chain;
23170
23171 per_cu = next_cu;
23172 }
23173 }
23174
23175 /* Remove a single compilation unit from the cache. */
23176
23177 static void
23178 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23179 {
23180 struct dwarf2_per_cu_data *per_cu, **last_chain;
23181 struct dwarf2_per_objfile *dwarf2_per_objfile
23182 = target_per_cu->dwarf2_per_objfile;
23183
23184 per_cu = dwarf2_per_objfile->read_in_chain;
23185 last_chain = &dwarf2_per_objfile->read_in_chain;
23186 while (per_cu != NULL)
23187 {
23188 struct dwarf2_per_cu_data *next_cu;
23189
23190 next_cu = per_cu->cu->read_in_chain;
23191
23192 if (per_cu == target_per_cu)
23193 {
23194 delete per_cu->cu;
23195 per_cu->cu = NULL;
23196 *last_chain = next_cu;
23197 break;
23198 }
23199 else
23200 last_chain = &per_cu->cu->read_in_chain;
23201
23202 per_cu = next_cu;
23203 }
23204 }
23205
23206 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23207 We store these in a hash table separate from the DIEs, and preserve them
23208 when the DIEs are flushed out of cache.
23209
23210 The CU "per_cu" pointer is needed because offset alone is not enough to
23211 uniquely identify the type. A file may have multiple .debug_types sections,
23212 or the type may come from a DWO file. Furthermore, while it's more logical
23213 to use per_cu->section+offset, with Fission the section with the data is in
23214 the DWO file but we don't know that section at the point we need it.
23215 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23216 because we can enter the lookup routine, get_die_type_at_offset, from
23217 outside this file, and thus won't necessarily have PER_CU->cu.
23218 Fortunately, PER_CU is stable for the life of the objfile. */
23219
23220 struct dwarf2_per_cu_offset_and_type
23221 {
23222 const struct dwarf2_per_cu_data *per_cu;
23223 sect_offset sect_off;
23224 struct type *type;
23225 };
23226
23227 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23228
23229 static hashval_t
23230 per_cu_offset_and_type_hash (const void *item)
23231 {
23232 const struct dwarf2_per_cu_offset_and_type *ofs
23233 = (const struct dwarf2_per_cu_offset_and_type *) item;
23234
23235 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23236 }
23237
23238 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23239
23240 static int
23241 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23242 {
23243 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23244 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23245 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23246 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23247
23248 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23249 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23250 }
23251
23252 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23253 table if necessary. For convenience, return TYPE.
23254
23255 The DIEs reading must have careful ordering to:
23256 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23257 reading current DIE.
23258 * Not trying to dereference contents of still incompletely read in types
23259 while reading in other DIEs.
23260 * Enable referencing still incompletely read in types just by a pointer to
23261 the type without accessing its fields.
23262
23263 Therefore caller should follow these rules:
23264 * Try to fetch any prerequisite types we may need to build this DIE type
23265 before building the type and calling set_die_type.
23266 * After building type call set_die_type for current DIE as soon as
23267 possible before fetching more types to complete the current type.
23268 * Make the type as complete as possible before fetching more types. */
23269
23270 static struct type *
23271 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23272 {
23273 struct dwarf2_per_objfile *dwarf2_per_objfile
23274 = cu->per_cu->dwarf2_per_objfile;
23275 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23276 struct objfile *objfile = dwarf2_per_objfile->objfile;
23277 struct attribute *attr;
23278 struct dynamic_prop prop;
23279
23280 /* For Ada types, make sure that the gnat-specific data is always
23281 initialized (if not already set). There are a few types where
23282 we should not be doing so, because the type-specific area is
23283 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23284 where the type-specific area is used to store the floatformat).
23285 But this is not a problem, because the gnat-specific information
23286 is actually not needed for these types. */
23287 if (need_gnat_info (cu)
23288 && TYPE_CODE (type) != TYPE_CODE_FUNC
23289 && TYPE_CODE (type) != TYPE_CODE_FLT
23290 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23291 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23292 && TYPE_CODE (type) != TYPE_CODE_METHOD
23293 && !HAVE_GNAT_AUX_INFO (type))
23294 INIT_GNAT_SPECIFIC (type);
23295
23296 /* Read DW_AT_allocated and set in type. */
23297 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23298 if (attr != NULL && attr->form_is_block ())
23299 {
23300 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23301 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23302 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
23303 }
23304 else if (attr != NULL)
23305 {
23306 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23307 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23308 sect_offset_str (die->sect_off));
23309 }
23310
23311 /* Read DW_AT_associated and set in type. */
23312 attr = dwarf2_attr (die, DW_AT_associated, cu);
23313 if (attr != NULL && attr->form_is_block ())
23314 {
23315 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23316 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23317 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
23318 }
23319 else if (attr != NULL)
23320 {
23321 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23322 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23323 sect_offset_str (die->sect_off));
23324 }
23325
23326 /* Read DW_AT_data_location and set in type. */
23327 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23328 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23329 cu->per_cu->addr_type ()))
23330 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
23331
23332 if (dwarf2_per_objfile->die_type_hash == NULL)
23333 dwarf2_per_objfile->die_type_hash
23334 = htab_up (htab_create_alloc (127,
23335 per_cu_offset_and_type_hash,
23336 per_cu_offset_and_type_eq,
23337 NULL, xcalloc, xfree));
23338
23339 ofs.per_cu = cu->per_cu;
23340 ofs.sect_off = die->sect_off;
23341 ofs.type = type;
23342 slot = (struct dwarf2_per_cu_offset_and_type **)
23343 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23344 if (*slot)
23345 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23346 sect_offset_str (die->sect_off));
23347 *slot = XOBNEW (&objfile->objfile_obstack,
23348 struct dwarf2_per_cu_offset_and_type);
23349 **slot = ofs;
23350 return type;
23351 }
23352
23353 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23354 or return NULL if the die does not have a saved type. */
23355
23356 static struct type *
23357 get_die_type_at_offset (sect_offset sect_off,
23358 struct dwarf2_per_cu_data *per_cu)
23359 {
23360 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23361 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23362
23363 if (dwarf2_per_objfile->die_type_hash == NULL)
23364 return NULL;
23365
23366 ofs.per_cu = per_cu;
23367 ofs.sect_off = sect_off;
23368 slot = ((struct dwarf2_per_cu_offset_and_type *)
23369 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23370 if (slot)
23371 return slot->type;
23372 else
23373 return NULL;
23374 }
23375
23376 /* Look up the type for DIE in CU in die_type_hash,
23377 or return NULL if DIE does not have a saved type. */
23378
23379 static struct type *
23380 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23381 {
23382 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23383 }
23384
23385 /* Add a dependence relationship from CU to REF_PER_CU. */
23386
23387 static void
23388 dwarf2_add_dependence (struct dwarf2_cu *cu,
23389 struct dwarf2_per_cu_data *ref_per_cu)
23390 {
23391 void **slot;
23392
23393 if (cu->dependencies == NULL)
23394 cu->dependencies
23395 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23396 NULL, &cu->comp_unit_obstack,
23397 hashtab_obstack_allocate,
23398 dummy_obstack_deallocate);
23399
23400 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23401 if (*slot == NULL)
23402 *slot = ref_per_cu;
23403 }
23404
23405 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23406 Set the mark field in every compilation unit in the
23407 cache that we must keep because we are keeping CU. */
23408
23409 static int
23410 dwarf2_mark_helper (void **slot, void *data)
23411 {
23412 struct dwarf2_per_cu_data *per_cu;
23413
23414 per_cu = (struct dwarf2_per_cu_data *) *slot;
23415
23416 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23417 reading of the chain. As such dependencies remain valid it is not much
23418 useful to track and undo them during QUIT cleanups. */
23419 if (per_cu->cu == NULL)
23420 return 1;
23421
23422 if (per_cu->cu->mark)
23423 return 1;
23424 per_cu->cu->mark = true;
23425
23426 if (per_cu->cu->dependencies != NULL)
23427 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23428
23429 return 1;
23430 }
23431
23432 /* Set the mark field in CU and in every other compilation unit in the
23433 cache that we must keep because we are keeping CU. */
23434
23435 static void
23436 dwarf2_mark (struct dwarf2_cu *cu)
23437 {
23438 if (cu->mark)
23439 return;
23440 cu->mark = true;
23441 if (cu->dependencies != NULL)
23442 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23443 }
23444
23445 static void
23446 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23447 {
23448 while (per_cu)
23449 {
23450 per_cu->cu->mark = false;
23451 per_cu = per_cu->cu->read_in_chain;
23452 }
23453 }
23454
23455 /* Trivial hash function for partial_die_info: the hash value of a DIE
23456 is its offset in .debug_info for this objfile. */
23457
23458 static hashval_t
23459 partial_die_hash (const void *item)
23460 {
23461 const struct partial_die_info *part_die
23462 = (const struct partial_die_info *) item;
23463
23464 return to_underlying (part_die->sect_off);
23465 }
23466
23467 /* Trivial comparison function for partial_die_info structures: two DIEs
23468 are equal if they have the same offset. */
23469
23470 static int
23471 partial_die_eq (const void *item_lhs, const void *item_rhs)
23472 {
23473 const struct partial_die_info *part_die_lhs
23474 = (const struct partial_die_info *) item_lhs;
23475 const struct partial_die_info *part_die_rhs
23476 = (const struct partial_die_info *) item_rhs;
23477
23478 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23479 }
23480
23481 struct cmd_list_element *set_dwarf_cmdlist;
23482 struct cmd_list_element *show_dwarf_cmdlist;
23483
23484 static void
23485 show_check_physname (struct ui_file *file, int from_tty,
23486 struct cmd_list_element *c, const char *value)
23487 {
23488 fprintf_filtered (file,
23489 _("Whether to check \"physname\" is %s.\n"),
23490 value);
23491 }
23492
23493 void _initialize_dwarf2_read ();
23494 void
23495 _initialize_dwarf2_read ()
23496 {
23497 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
23498 Set DWARF specific variables.\n\
23499 Configure DWARF variables such as the cache size."),
23500 &set_dwarf_cmdlist, "maintenance set dwarf ",
23501 0/*allow-unknown*/, &maintenance_set_cmdlist);
23502
23503 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
23504 Show DWARF specific variables.\n\
23505 Show DWARF variables such as the cache size."),
23506 &show_dwarf_cmdlist, "maintenance show dwarf ",
23507 0/*allow-unknown*/, &maintenance_show_cmdlist);
23508
23509 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23510 &dwarf_max_cache_age, _("\
23511 Set the upper bound on the age of cached DWARF compilation units."), _("\
23512 Show the upper bound on the age of cached DWARF compilation units."), _("\
23513 A higher limit means that cached compilation units will be stored\n\
23514 in memory longer, and more total memory will be used. Zero disables\n\
23515 caching, which can slow down startup."),
23516 NULL,
23517 show_dwarf_max_cache_age,
23518 &set_dwarf_cmdlist,
23519 &show_dwarf_cmdlist);
23520
23521 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23522 Set debugging of the DWARF reader."), _("\
23523 Show debugging of the DWARF reader."), _("\
23524 When enabled (non-zero), debugging messages are printed during DWARF\n\
23525 reading and symtab expansion. A value of 1 (one) provides basic\n\
23526 information. A value greater than 1 provides more verbose information."),
23527 NULL,
23528 NULL,
23529 &setdebuglist, &showdebuglist);
23530
23531 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23532 Set debugging of the DWARF DIE reader."), _("\
23533 Show debugging of the DWARF DIE reader."), _("\
23534 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23535 The value is the maximum depth to print."),
23536 NULL,
23537 NULL,
23538 &setdebuglist, &showdebuglist);
23539
23540 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23541 Set debugging of the dwarf line reader."), _("\
23542 Show debugging of the dwarf line reader."), _("\
23543 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23544 A value of 1 (one) provides basic information.\n\
23545 A value greater than 1 provides more verbose information."),
23546 NULL,
23547 NULL,
23548 &setdebuglist, &showdebuglist);
23549
23550 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23551 Set cross-checking of \"physname\" code against demangler."), _("\
23552 Show cross-checking of \"physname\" code against demangler."), _("\
23553 When enabled, GDB's internal \"physname\" code is checked against\n\
23554 the demangler."),
23555 NULL, show_check_physname,
23556 &setdebuglist, &showdebuglist);
23557
23558 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23559 no_class, &use_deprecated_index_sections, _("\
23560 Set whether to use deprecated gdb_index sections."), _("\
23561 Show whether to use deprecated gdb_index sections."), _("\
23562 When enabled, deprecated .gdb_index sections are used anyway.\n\
23563 Normally they are ignored either because of a missing feature or\n\
23564 performance issue.\n\
23565 Warning: This option must be enabled before gdb reads the file."),
23566 NULL,
23567 NULL,
23568 &setlist, &showlist);
23569
23570 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23571 &dwarf2_locexpr_funcs);
23572 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23573 &dwarf2_loclist_funcs);
23574
23575 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23576 &dwarf2_block_frame_base_locexpr_funcs);
23577 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23578 &dwarf2_block_frame_base_loclist_funcs);
23579
23580 #if GDB_SELF_TEST
23581 selftests::register_test ("dw2_expand_symtabs_matching",
23582 selftests::dw2_expand_symtabs_matching::run_test);
23583 selftests::register_test ("dwarf2_find_containing_comp_unit",
23584 selftests::find_containing_comp_unit::run_test);
23585 #endif
23586 }