Use bfd_get_filename instead of objfile_name in lookup_dwo_unit
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "dwarf2/stringify.h"
44 #include "bfd.h"
45 #include "elf-bfd.h"
46 #include "symtab.h"
47 #include "gdbtypes.h"
48 #include "objfiles.h"
49 #include "dwarf2.h"
50 #include "buildsym.h"
51 #include "demangle.h"
52 #include "gdb-demangle.h"
53 #include "filenames.h" /* for DOSish file names */
54 #include "language.h"
55 #include "complaints.h"
56 #include "dwarf2/expr.h"
57 #include "dwarf2/loc.h"
58 #include "cp-support.h"
59 #include "hashtab.h"
60 #include "command.h"
61 #include "gdbcmd.h"
62 #include "block.h"
63 #include "addrmap.h"
64 #include "typeprint.h"
65 #include "psympriv.h"
66 #include "c-lang.h"
67 #include "go-lang.h"
68 #include "valprint.h"
69 #include "gdbcore.h" /* for gnutarget */
70 #include "gdb/gdb-index.h"
71 #include "gdb_bfd.h"
72 #include "f-lang.h"
73 #include "source.h"
74 #include "build-id.h"
75 #include "namespace.h"
76 #include "gdbsupport/function-view.h"
77 #include "gdbsupport/gdb_optional.h"
78 #include "gdbsupport/underlying.h"
79 #include "gdbsupport/hash_enum.h"
80 #include "filename-seen-cache.h"
81 #include "producer.h"
82 #include <fcntl.h>
83 #include <algorithm>
84 #include <unordered_map>
85 #include "gdbsupport/selftest.h"
86 #include "rust-lang.h"
87 #include "gdbsupport/pathstuff.h"
88 #include "count-one-bits.h"
89 #include "debuginfod-support.h"
90
91 /* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94 static unsigned int dwarf_read_debug = 0;
95
96 /* When non-zero, dump DIEs after they are read in. */
97 static unsigned int dwarf_die_debug = 0;
98
99 /* When non-zero, dump line number entries as they are read in. */
100 unsigned int dwarf_line_debug = 0;
101
102 /* When true, cross-check physname against demangler. */
103 static bool check_physname = false;
104
105 /* When true, do not reject deprecated .gdb_index sections. */
106 static bool use_deprecated_index_sections = false;
107
108 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
109
110 /* The "aclass" indices for various kinds of computed DWARF symbols. */
111
112 static int dwarf2_locexpr_index;
113 static int dwarf2_loclist_index;
114 static int dwarf2_locexpr_block_index;
115 static int dwarf2_loclist_block_index;
116
117 /* Size of .debug_loclists section header for 32-bit DWARF format. */
118 #define LOCLIST_HEADER_SIZE32 12
119
120 /* Size of .debug_loclists section header for 64-bit DWARF format. */
121 #define LOCLIST_HEADER_SIZE64 20
122
123 /* An index into a (C++) symbol name component in a symbol name as
124 recorded in the mapped_index's symbol table. For each C++ symbol
125 in the symbol table, we record one entry for the start of each
126 component in the symbol in a table of name components, and then
127 sort the table, in order to be able to binary search symbol names,
128 ignoring leading namespaces, both completion and regular look up.
129 For example, for symbol "A::B::C", we'll have an entry that points
130 to "A::B::C", another that points to "B::C", and another for "C".
131 Note that function symbols in GDB index have no parameter
132 information, just the function/method names. You can convert a
133 name_component to a "const char *" using the
134 'mapped_index::symbol_name_at(offset_type)' method. */
135
136 struct name_component
137 {
138 /* Offset in the symbol name where the component starts. Stored as
139 a (32-bit) offset instead of a pointer to save memory and improve
140 locality on 64-bit architectures. */
141 offset_type name_offset;
142
143 /* The symbol's index in the symbol and constant pool tables of a
144 mapped_index. */
145 offset_type idx;
146 };
147
148 /* Base class containing bits shared by both .gdb_index and
149 .debug_name indexes. */
150
151 struct mapped_index_base
152 {
153 mapped_index_base () = default;
154 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
155
156 /* The name_component table (a sorted vector). See name_component's
157 description above. */
158 std::vector<name_component> name_components;
159
160 /* How NAME_COMPONENTS is sorted. */
161 enum case_sensitivity name_components_casing;
162
163 /* Return the number of names in the symbol table. */
164 virtual size_t symbol_name_count () const = 0;
165
166 /* Get the name of the symbol at IDX in the symbol table. */
167 virtual const char *symbol_name_at (offset_type idx) const = 0;
168
169 /* Return whether the name at IDX in the symbol table should be
170 ignored. */
171 virtual bool symbol_name_slot_invalid (offset_type idx) const
172 {
173 return false;
174 }
175
176 /* Build the symbol name component sorted vector, if we haven't
177 yet. */
178 void build_name_components ();
179
180 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
181 possible matches for LN_NO_PARAMS in the name component
182 vector. */
183 std::pair<std::vector<name_component>::const_iterator,
184 std::vector<name_component>::const_iterator>
185 find_name_components_bounds (const lookup_name_info &ln_no_params,
186 enum language lang) const;
187
188 /* Prevent deleting/destroying via a base class pointer. */
189 protected:
190 ~mapped_index_base() = default;
191 };
192
193 /* A description of the mapped index. The file format is described in
194 a comment by the code that writes the index. */
195 struct mapped_index final : public mapped_index_base
196 {
197 /* A slot/bucket in the symbol table hash. */
198 struct symbol_table_slot
199 {
200 const offset_type name;
201 const offset_type vec;
202 };
203
204 /* Index data format version. */
205 int version = 0;
206
207 /* The address table data. */
208 gdb::array_view<const gdb_byte> address_table;
209
210 /* The symbol table, implemented as a hash table. */
211 gdb::array_view<symbol_table_slot> symbol_table;
212
213 /* A pointer to the constant pool. */
214 const char *constant_pool = nullptr;
215
216 bool symbol_name_slot_invalid (offset_type idx) const override
217 {
218 const auto &bucket = this->symbol_table[idx];
219 return bucket.name == 0 && bucket.vec == 0;
220 }
221
222 /* Convenience method to get at the name of the symbol at IDX in the
223 symbol table. */
224 const char *symbol_name_at (offset_type idx) const override
225 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
226
227 size_t symbol_name_count () const override
228 { return this->symbol_table.size (); }
229 };
230
231 /* A description of the mapped .debug_names.
232 Uninitialized map has CU_COUNT 0. */
233 struct mapped_debug_names final : public mapped_index_base
234 {
235 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
236 : dwarf2_per_objfile (dwarf2_per_objfile_)
237 {}
238
239 struct dwarf2_per_objfile *dwarf2_per_objfile;
240 bfd_endian dwarf5_byte_order;
241 bool dwarf5_is_dwarf64;
242 bool augmentation_is_gdb;
243 uint8_t offset_size;
244 uint32_t cu_count = 0;
245 uint32_t tu_count, bucket_count, name_count;
246 const gdb_byte *cu_table_reordered, *tu_table_reordered;
247 const uint32_t *bucket_table_reordered, *hash_table_reordered;
248 const gdb_byte *name_table_string_offs_reordered;
249 const gdb_byte *name_table_entry_offs_reordered;
250 const gdb_byte *entry_pool;
251
252 struct index_val
253 {
254 ULONGEST dwarf_tag;
255 struct attr
256 {
257 /* Attribute name DW_IDX_*. */
258 ULONGEST dw_idx;
259
260 /* Attribute form DW_FORM_*. */
261 ULONGEST form;
262
263 /* Value if FORM is DW_FORM_implicit_const. */
264 LONGEST implicit_const;
265 };
266 std::vector<attr> attr_vec;
267 };
268
269 std::unordered_map<ULONGEST, index_val> abbrev_map;
270
271 const char *namei_to_name (uint32_t namei) const;
272
273 /* Implementation of the mapped_index_base virtual interface, for
274 the name_components cache. */
275
276 const char *symbol_name_at (offset_type idx) const override
277 { return namei_to_name (idx); }
278
279 size_t symbol_name_count () const override
280 { return this->name_count; }
281 };
282
283 /* See dwarf2read.h. */
284
285 dwarf2_per_objfile *
286 get_dwarf2_per_objfile (struct objfile *objfile)
287 {
288 return dwarf2_objfile_data_key.get (objfile);
289 }
290
291 /* Default names of the debugging sections. */
292
293 /* Note that if the debugging section has been compressed, it might
294 have a name like .zdebug_info. */
295
296 static const struct dwarf2_debug_sections dwarf2_elf_names =
297 {
298 { ".debug_info", ".zdebug_info" },
299 { ".debug_abbrev", ".zdebug_abbrev" },
300 { ".debug_line", ".zdebug_line" },
301 { ".debug_loc", ".zdebug_loc" },
302 { ".debug_loclists", ".zdebug_loclists" },
303 { ".debug_macinfo", ".zdebug_macinfo" },
304 { ".debug_macro", ".zdebug_macro" },
305 { ".debug_str", ".zdebug_str" },
306 { ".debug_str_offsets", ".zdebug_str_offsets" },
307 { ".debug_line_str", ".zdebug_line_str" },
308 { ".debug_ranges", ".zdebug_ranges" },
309 { ".debug_rnglists", ".zdebug_rnglists" },
310 { ".debug_types", ".zdebug_types" },
311 { ".debug_addr", ".zdebug_addr" },
312 { ".debug_frame", ".zdebug_frame" },
313 { ".eh_frame", NULL },
314 { ".gdb_index", ".zgdb_index" },
315 { ".debug_names", ".zdebug_names" },
316 { ".debug_aranges", ".zdebug_aranges" },
317 23
318 };
319
320 /* List of DWO/DWP sections. */
321
322 static const struct dwop_section_names
323 {
324 struct dwarf2_section_names abbrev_dwo;
325 struct dwarf2_section_names info_dwo;
326 struct dwarf2_section_names line_dwo;
327 struct dwarf2_section_names loc_dwo;
328 struct dwarf2_section_names loclists_dwo;
329 struct dwarf2_section_names macinfo_dwo;
330 struct dwarf2_section_names macro_dwo;
331 struct dwarf2_section_names str_dwo;
332 struct dwarf2_section_names str_offsets_dwo;
333 struct dwarf2_section_names types_dwo;
334 struct dwarf2_section_names cu_index;
335 struct dwarf2_section_names tu_index;
336 }
337 dwop_section_names =
338 {
339 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
340 { ".debug_info.dwo", ".zdebug_info.dwo" },
341 { ".debug_line.dwo", ".zdebug_line.dwo" },
342 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
343 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
344 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
345 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
346 { ".debug_str.dwo", ".zdebug_str.dwo" },
347 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
348 { ".debug_types.dwo", ".zdebug_types.dwo" },
349 { ".debug_cu_index", ".zdebug_cu_index" },
350 { ".debug_tu_index", ".zdebug_tu_index" },
351 };
352
353 /* local data types */
354
355 /* The location list section (.debug_loclists) begins with a header,
356 which contains the following information. */
357 struct loclist_header
358 {
359 /* A 4-byte or 12-byte length containing the length of the
360 set of entries for this compilation unit, not including the
361 length field itself. */
362 unsigned int length;
363
364 /* A 2-byte version identifier. */
365 short version;
366
367 /* A 1-byte unsigned integer containing the size in bytes of an address on
368 the target system. */
369 unsigned char addr_size;
370
371 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
372 on the target system. */
373 unsigned char segment_collector_size;
374
375 /* A 4-byte count of the number of offsets that follow the header. */
376 unsigned int offset_entry_count;
377 };
378
379 /* Type used for delaying computation of method physnames.
380 See comments for compute_delayed_physnames. */
381 struct delayed_method_info
382 {
383 /* The type to which the method is attached, i.e., its parent class. */
384 struct type *type;
385
386 /* The index of the method in the type's function fieldlists. */
387 int fnfield_index;
388
389 /* The index of the method in the fieldlist. */
390 int index;
391
392 /* The name of the DIE. */
393 const char *name;
394
395 /* The DIE associated with this method. */
396 struct die_info *die;
397 };
398
399 /* Internal state when decoding a particular compilation unit. */
400 struct dwarf2_cu
401 {
402 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
403 dwarf2_per_objfile *per_objfile);
404 ~dwarf2_cu ();
405
406 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
407
408 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
409 Create the set of symtabs used by this TU, or if this TU is sharing
410 symtabs with another TU and the symtabs have already been created
411 then restore those symtabs in the line header.
412 We don't need the pc/line-number mapping for type units. */
413 void setup_type_unit_groups (struct die_info *die);
414
415 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
416 buildsym_compunit constructor. */
417 struct compunit_symtab *start_symtab (const char *name,
418 const char *comp_dir,
419 CORE_ADDR low_pc);
420
421 /* Reset the builder. */
422 void reset_builder () { m_builder.reset (); }
423
424 /* The header of the compilation unit. */
425 struct comp_unit_head header {};
426
427 /* Base address of this compilation unit. */
428 gdb::optional<CORE_ADDR> base_address;
429
430 /* The language we are debugging. */
431 enum language language = language_unknown;
432 const struct language_defn *language_defn = nullptr;
433
434 const char *producer = nullptr;
435
436 private:
437 /* The symtab builder for this CU. This is only non-NULL when full
438 symbols are being read. */
439 std::unique_ptr<buildsym_compunit> m_builder;
440
441 public:
442 /* The generic symbol table building routines have separate lists for
443 file scope symbols and all all other scopes (local scopes). So
444 we need to select the right one to pass to add_symbol_to_list().
445 We do it by keeping a pointer to the correct list in list_in_scope.
446
447 FIXME: The original dwarf code just treated the file scope as the
448 first local scope, and all other local scopes as nested local
449 scopes, and worked fine. Check to see if we really need to
450 distinguish these in buildsym.c. */
451 struct pending **list_in_scope = nullptr;
452
453 /* Hash table holding all the loaded partial DIEs
454 with partial_die->offset.SECT_OFF as hash. */
455 htab_t partial_dies = nullptr;
456
457 /* Storage for things with the same lifetime as this read-in compilation
458 unit, including partial DIEs. */
459 auto_obstack comp_unit_obstack;
460
461 /* When multiple dwarf2_cu structures are living in memory, this field
462 chains them all together, so that they can be released efficiently.
463 We will probably also want a generation counter so that most-recently-used
464 compilation units are cached... */
465 struct dwarf2_per_cu_data *read_in_chain = nullptr;
466
467 /* Backlink to our per_cu entry. */
468 struct dwarf2_per_cu_data *per_cu;
469
470 /* The dwarf2_per_objfile that owns this. */
471 struct dwarf2_per_objfile *per_objfile;
472
473 /* How many compilation units ago was this CU last referenced? */
474 int last_used = 0;
475
476 /* A hash table of DIE cu_offset for following references with
477 die_info->offset.sect_off as hash. */
478 htab_t die_hash = nullptr;
479
480 /* Full DIEs if read in. */
481 struct die_info *dies = nullptr;
482
483 /* A set of pointers to dwarf2_per_cu_data objects for compilation
484 units referenced by this one. Only set during full symbol processing;
485 partial symbol tables do not have dependencies. */
486 htab_t dependencies = nullptr;
487
488 /* Header data from the line table, during full symbol processing. */
489 struct line_header *line_header = nullptr;
490 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
491 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
492 this is the DW_TAG_compile_unit die for this CU. We'll hold on
493 to the line header as long as this DIE is being processed. See
494 process_die_scope. */
495 die_info *line_header_die_owner = nullptr;
496
497 /* A list of methods which need to have physnames computed
498 after all type information has been read. */
499 std::vector<delayed_method_info> method_list;
500
501 /* To be copied to symtab->call_site_htab. */
502 htab_t call_site_htab = nullptr;
503
504 /* Non-NULL if this CU came from a DWO file.
505 There is an invariant here that is important to remember:
506 Except for attributes copied from the top level DIE in the "main"
507 (or "stub") file in preparation for reading the DWO file
508 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
509 Either there isn't a DWO file (in which case this is NULL and the point
510 is moot), or there is and either we're not going to read it (in which
511 case this is NULL) or there is and we are reading it (in which case this
512 is non-NULL). */
513 struct dwo_unit *dwo_unit = nullptr;
514
515 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
516 Note this value comes from the Fission stub CU/TU's DIE. */
517 gdb::optional<ULONGEST> addr_base;
518
519 /* The DW_AT_rnglists_base attribute if present.
520 Note this value comes from the Fission stub CU/TU's DIE.
521 Also note that the value is zero in the non-DWO case so this value can
522 be used without needing to know whether DWO files are in use or not.
523 N.B. This does not apply to DW_AT_ranges appearing in
524 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
525 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
526 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
527 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
528 ULONGEST ranges_base = 0;
529
530 /* The DW_AT_loclists_base attribute if present. */
531 ULONGEST loclist_base = 0;
532
533 /* When reading debug info generated by older versions of rustc, we
534 have to rewrite some union types to be struct types with a
535 variant part. This rewriting must be done after the CU is fully
536 read in, because otherwise at the point of rewriting some struct
537 type might not have been fully processed. So, we keep a list of
538 all such types here and process them after expansion. */
539 std::vector<struct type *> rust_unions;
540
541 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
542 files, the value is implicitly zero. For DWARF 5 version DWO files, the
543 value is often implicit and is the size of the header of
544 .debug_str_offsets section (8 or 4, depending on the address size). */
545 gdb::optional<ULONGEST> str_offsets_base;
546
547 /* Mark used when releasing cached dies. */
548 bool mark : 1;
549
550 /* This CU references .debug_loc. See the symtab->locations_valid field.
551 This test is imperfect as there may exist optimized debug code not using
552 any location list and still facing inlining issues if handled as
553 unoptimized code. For a future better test see GCC PR other/32998. */
554 bool has_loclist : 1;
555
556 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
557 if all the producer_is_* fields are valid. This information is cached
558 because profiling CU expansion showed excessive time spent in
559 producer_is_gxx_lt_4_6. */
560 bool checked_producer : 1;
561 bool producer_is_gxx_lt_4_6 : 1;
562 bool producer_is_gcc_lt_4_3 : 1;
563 bool producer_is_icc : 1;
564 bool producer_is_icc_lt_14 : 1;
565 bool producer_is_codewarrior : 1;
566
567 /* When true, the file that we're processing is known to have
568 debugging info for C++ namespaces. GCC 3.3.x did not produce
569 this information, but later versions do. */
570
571 bool processing_has_namespace_info : 1;
572
573 struct partial_die_info *find_partial_die (sect_offset sect_off);
574
575 /* If this CU was inherited by another CU (via specification,
576 abstract_origin, etc), this is the ancestor CU. */
577 dwarf2_cu *ancestor;
578
579 /* Get the buildsym_compunit for this CU. */
580 buildsym_compunit *get_builder ()
581 {
582 /* If this CU has a builder associated with it, use that. */
583 if (m_builder != nullptr)
584 return m_builder.get ();
585
586 /* Otherwise, search ancestors for a valid builder. */
587 if (ancestor != nullptr)
588 return ancestor->get_builder ();
589
590 return nullptr;
591 }
592 };
593
594 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
595 This includes type_unit_group and quick_file_names. */
596
597 struct stmt_list_hash
598 {
599 /* The DWO unit this table is from or NULL if there is none. */
600 struct dwo_unit *dwo_unit;
601
602 /* Offset in .debug_line or .debug_line.dwo. */
603 sect_offset line_sect_off;
604 };
605
606 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
607 an object of this type. */
608
609 struct type_unit_group
610 {
611 /* dwarf2read.c's main "handle" on a TU symtab.
612 To simplify things we create an artificial CU that "includes" all the
613 type units using this stmt_list so that the rest of the code still has
614 a "per_cu" handle on the symtab. */
615 struct dwarf2_per_cu_data per_cu;
616
617 /* The TUs that share this DW_AT_stmt_list entry.
618 This is added to while parsing type units to build partial symtabs,
619 and is deleted afterwards and not used again. */
620 std::vector<signatured_type *> *tus;
621
622 /* The compunit symtab.
623 Type units in a group needn't all be defined in the same source file,
624 so we create an essentially anonymous symtab as the compunit symtab. */
625 struct compunit_symtab *compunit_symtab;
626
627 /* The data used to construct the hash key. */
628 struct stmt_list_hash hash;
629
630 /* The symbol tables for this TU (obtained from the files listed in
631 DW_AT_stmt_list).
632 WARNING: The order of entries here must match the order of entries
633 in the line header. After the first TU using this type_unit_group, the
634 line header for the subsequent TUs is recreated from this. This is done
635 because we need to use the same symtabs for each TU using the same
636 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
637 there's no guarantee the line header doesn't have duplicate entries. */
638 struct symtab **symtabs;
639 };
640
641 /* These sections are what may appear in a (real or virtual) DWO file. */
642
643 struct dwo_sections
644 {
645 struct dwarf2_section_info abbrev;
646 struct dwarf2_section_info line;
647 struct dwarf2_section_info loc;
648 struct dwarf2_section_info loclists;
649 struct dwarf2_section_info macinfo;
650 struct dwarf2_section_info macro;
651 struct dwarf2_section_info str;
652 struct dwarf2_section_info str_offsets;
653 /* In the case of a virtual DWO file, these two are unused. */
654 struct dwarf2_section_info info;
655 std::vector<dwarf2_section_info> types;
656 };
657
658 /* CUs/TUs in DWP/DWO files. */
659
660 struct dwo_unit
661 {
662 /* Backlink to the containing struct dwo_file. */
663 struct dwo_file *dwo_file;
664
665 /* The "id" that distinguishes this CU/TU.
666 .debug_info calls this "dwo_id", .debug_types calls this "signature".
667 Since signatures came first, we stick with it for consistency. */
668 ULONGEST signature;
669
670 /* The section this CU/TU lives in, in the DWO file. */
671 struct dwarf2_section_info *section;
672
673 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
674 sect_offset sect_off;
675 unsigned int length;
676
677 /* For types, offset in the type's DIE of the type defined by this TU. */
678 cu_offset type_offset_in_tu;
679 };
680
681 /* include/dwarf2.h defines the DWP section codes.
682 It defines a max value but it doesn't define a min value, which we
683 use for error checking, so provide one. */
684
685 enum dwp_v2_section_ids
686 {
687 DW_SECT_MIN = 1
688 };
689
690 /* Data for one DWO file.
691
692 This includes virtual DWO files (a virtual DWO file is a DWO file as it
693 appears in a DWP file). DWP files don't really have DWO files per se -
694 comdat folding of types "loses" the DWO file they came from, and from
695 a high level view DWP files appear to contain a mass of random types.
696 However, to maintain consistency with the non-DWP case we pretend DWP
697 files contain virtual DWO files, and we assign each TU with one virtual
698 DWO file (generally based on the line and abbrev section offsets -
699 a heuristic that seems to work in practice). */
700
701 struct dwo_file
702 {
703 dwo_file () = default;
704 DISABLE_COPY_AND_ASSIGN (dwo_file);
705
706 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
707 For virtual DWO files the name is constructed from the section offsets
708 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
709 from related CU+TUs. */
710 const char *dwo_name = nullptr;
711
712 /* The DW_AT_comp_dir attribute. */
713 const char *comp_dir = nullptr;
714
715 /* The bfd, when the file is open. Otherwise this is NULL.
716 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
717 gdb_bfd_ref_ptr dbfd;
718
719 /* The sections that make up this DWO file.
720 Remember that for virtual DWO files in DWP V2, these are virtual
721 sections (for lack of a better name). */
722 struct dwo_sections sections {};
723
724 /* The CUs in the file.
725 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
726 an extension to handle LLVM's Link Time Optimization output (where
727 multiple source files may be compiled into a single object/dwo pair). */
728 htab_up cus;
729
730 /* Table of TUs in the file.
731 Each element is a struct dwo_unit. */
732 htab_up tus;
733 };
734
735 /* These sections are what may appear in a DWP file. */
736
737 struct dwp_sections
738 {
739 /* These are used by both DWP version 1 and 2. */
740 struct dwarf2_section_info str;
741 struct dwarf2_section_info cu_index;
742 struct dwarf2_section_info tu_index;
743
744 /* These are only used by DWP version 2 files.
745 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
746 sections are referenced by section number, and are not recorded here.
747 In DWP version 2 there is at most one copy of all these sections, each
748 section being (effectively) comprised of the concatenation of all of the
749 individual sections that exist in the version 1 format.
750 To keep the code simple we treat each of these concatenated pieces as a
751 section itself (a virtual section?). */
752 struct dwarf2_section_info abbrev;
753 struct dwarf2_section_info info;
754 struct dwarf2_section_info line;
755 struct dwarf2_section_info loc;
756 struct dwarf2_section_info macinfo;
757 struct dwarf2_section_info macro;
758 struct dwarf2_section_info str_offsets;
759 struct dwarf2_section_info types;
760 };
761
762 /* These sections are what may appear in a virtual DWO file in DWP version 1.
763 A virtual DWO file is a DWO file as it appears in a DWP file. */
764
765 struct virtual_v1_dwo_sections
766 {
767 struct dwarf2_section_info abbrev;
768 struct dwarf2_section_info line;
769 struct dwarf2_section_info loc;
770 struct dwarf2_section_info macinfo;
771 struct dwarf2_section_info macro;
772 struct dwarf2_section_info str_offsets;
773 /* Each DWP hash table entry records one CU or one TU.
774 That is recorded here, and copied to dwo_unit.section. */
775 struct dwarf2_section_info info_or_types;
776 };
777
778 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
779 In version 2, the sections of the DWO files are concatenated together
780 and stored in one section of that name. Thus each ELF section contains
781 several "virtual" sections. */
782
783 struct virtual_v2_dwo_sections
784 {
785 bfd_size_type abbrev_offset;
786 bfd_size_type abbrev_size;
787
788 bfd_size_type line_offset;
789 bfd_size_type line_size;
790
791 bfd_size_type loc_offset;
792 bfd_size_type loc_size;
793
794 bfd_size_type macinfo_offset;
795 bfd_size_type macinfo_size;
796
797 bfd_size_type macro_offset;
798 bfd_size_type macro_size;
799
800 bfd_size_type str_offsets_offset;
801 bfd_size_type str_offsets_size;
802
803 /* Each DWP hash table entry records one CU or one TU.
804 That is recorded here, and copied to dwo_unit.section. */
805 bfd_size_type info_or_types_offset;
806 bfd_size_type info_or_types_size;
807 };
808
809 /* Contents of DWP hash tables. */
810
811 struct dwp_hash_table
812 {
813 uint32_t version, nr_columns;
814 uint32_t nr_units, nr_slots;
815 const gdb_byte *hash_table, *unit_table;
816 union
817 {
818 struct
819 {
820 const gdb_byte *indices;
821 } v1;
822 struct
823 {
824 /* This is indexed by column number and gives the id of the section
825 in that column. */
826 #define MAX_NR_V2_DWO_SECTIONS \
827 (1 /* .debug_info or .debug_types */ \
828 + 1 /* .debug_abbrev */ \
829 + 1 /* .debug_line */ \
830 + 1 /* .debug_loc */ \
831 + 1 /* .debug_str_offsets */ \
832 + 1 /* .debug_macro or .debug_macinfo */)
833 int section_ids[MAX_NR_V2_DWO_SECTIONS];
834 const gdb_byte *offsets;
835 const gdb_byte *sizes;
836 } v2;
837 } section_pool;
838 };
839
840 /* Data for one DWP file. */
841
842 struct dwp_file
843 {
844 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
845 : name (name_),
846 dbfd (std::move (abfd))
847 {
848 }
849
850 /* Name of the file. */
851 const char *name;
852
853 /* File format version. */
854 int version = 0;
855
856 /* The bfd. */
857 gdb_bfd_ref_ptr dbfd;
858
859 /* Section info for this file. */
860 struct dwp_sections sections {};
861
862 /* Table of CUs in the file. */
863 const struct dwp_hash_table *cus = nullptr;
864
865 /* Table of TUs in the file. */
866 const struct dwp_hash_table *tus = nullptr;
867
868 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
869 htab_up loaded_cus;
870 htab_up loaded_tus;
871
872 /* Table to map ELF section numbers to their sections.
873 This is only needed for the DWP V1 file format. */
874 unsigned int num_sections = 0;
875 asection **elf_sections = nullptr;
876 };
877
878 /* Struct used to pass misc. parameters to read_die_and_children, et
879 al. which are used for both .debug_info and .debug_types dies.
880 All parameters here are unchanging for the life of the call. This
881 struct exists to abstract away the constant parameters of die reading. */
882
883 struct die_reader_specs
884 {
885 /* The bfd of die_section. */
886 bfd* abfd;
887
888 /* The CU of the DIE we are parsing. */
889 struct dwarf2_cu *cu;
890
891 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
892 struct dwo_file *dwo_file;
893
894 /* The section the die comes from.
895 This is either .debug_info or .debug_types, or the .dwo variants. */
896 struct dwarf2_section_info *die_section;
897
898 /* die_section->buffer. */
899 const gdb_byte *buffer;
900
901 /* The end of the buffer. */
902 const gdb_byte *buffer_end;
903
904 /* The abbreviation table to use when reading the DIEs. */
905 struct abbrev_table *abbrev_table;
906 };
907
908 /* A subclass of die_reader_specs that holds storage and has complex
909 constructor and destructor behavior. */
910
911 class cutu_reader : public die_reader_specs
912 {
913 public:
914
915 cutu_reader (struct dwarf2_per_cu_data *this_cu,
916 struct abbrev_table *abbrev_table,
917 int use_existing_cu,
918 bool skip_partial);
919
920 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
921 struct dwarf2_cu *parent_cu = nullptr,
922 struct dwo_file *dwo_file = nullptr);
923
924 DISABLE_COPY_AND_ASSIGN (cutu_reader);
925
926 const gdb_byte *info_ptr = nullptr;
927 struct die_info *comp_unit_die = nullptr;
928 bool dummy_p = false;
929
930 /* Release the new CU, putting it on the chain. This cannot be done
931 for dummy CUs. */
932 void keep ();
933
934 private:
935 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
936 dwarf2_per_objfile *per_objfile,
937 int use_existing_cu);
938
939 struct dwarf2_per_cu_data *m_this_cu;
940 std::unique_ptr<dwarf2_cu> m_new_cu;
941
942 /* The ordinary abbreviation table. */
943 abbrev_table_up m_abbrev_table_holder;
944
945 /* The DWO abbreviation table. */
946 abbrev_table_up m_dwo_abbrev_table;
947 };
948
949 /* When we construct a partial symbol table entry we only
950 need this much information. */
951 struct partial_die_info : public allocate_on_obstack
952 {
953 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
954
955 /* Disable assign but still keep copy ctor, which is needed
956 load_partial_dies. */
957 partial_die_info& operator=(const partial_die_info& rhs) = delete;
958
959 /* Adjust the partial die before generating a symbol for it. This
960 function may set the is_external flag or change the DIE's
961 name. */
962 void fixup (struct dwarf2_cu *cu);
963
964 /* Read a minimal amount of information into the minimal die
965 structure. */
966 const gdb_byte *read (const struct die_reader_specs *reader,
967 const struct abbrev_info &abbrev,
968 const gdb_byte *info_ptr);
969
970 /* Offset of this DIE. */
971 const sect_offset sect_off;
972
973 /* DWARF-2 tag for this DIE. */
974 const ENUM_BITFIELD(dwarf_tag) tag : 16;
975
976 /* Assorted flags describing the data found in this DIE. */
977 const unsigned int has_children : 1;
978
979 unsigned int is_external : 1;
980 unsigned int is_declaration : 1;
981 unsigned int has_type : 1;
982 unsigned int has_specification : 1;
983 unsigned int has_pc_info : 1;
984 unsigned int may_be_inlined : 1;
985
986 /* This DIE has been marked DW_AT_main_subprogram. */
987 unsigned int main_subprogram : 1;
988
989 /* Flag set if the SCOPE field of this structure has been
990 computed. */
991 unsigned int scope_set : 1;
992
993 /* Flag set if the DIE has a byte_size attribute. */
994 unsigned int has_byte_size : 1;
995
996 /* Flag set if the DIE has a DW_AT_const_value attribute. */
997 unsigned int has_const_value : 1;
998
999 /* Flag set if any of the DIE's children are template arguments. */
1000 unsigned int has_template_arguments : 1;
1001
1002 /* Flag set if fixup has been called on this die. */
1003 unsigned int fixup_called : 1;
1004
1005 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1006 unsigned int is_dwz : 1;
1007
1008 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1009 unsigned int spec_is_dwz : 1;
1010
1011 /* The name of this DIE. Normally the value of DW_AT_name, but
1012 sometimes a default name for unnamed DIEs. */
1013 const char *name = nullptr;
1014
1015 /* The linkage name, if present. */
1016 const char *linkage_name = nullptr;
1017
1018 /* The scope to prepend to our children. This is generally
1019 allocated on the comp_unit_obstack, so will disappear
1020 when this compilation unit leaves the cache. */
1021 const char *scope = nullptr;
1022
1023 /* Some data associated with the partial DIE. The tag determines
1024 which field is live. */
1025 union
1026 {
1027 /* The location description associated with this DIE, if any. */
1028 struct dwarf_block *locdesc;
1029 /* The offset of an import, for DW_TAG_imported_unit. */
1030 sect_offset sect_off;
1031 } d {};
1032
1033 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1034 CORE_ADDR lowpc = 0;
1035 CORE_ADDR highpc = 0;
1036
1037 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1038 DW_AT_sibling, if any. */
1039 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1040 could return DW_AT_sibling values to its caller load_partial_dies. */
1041 const gdb_byte *sibling = nullptr;
1042
1043 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1044 DW_AT_specification (or DW_AT_abstract_origin or
1045 DW_AT_extension). */
1046 sect_offset spec_offset {};
1047
1048 /* Pointers to this DIE's parent, first child, and next sibling,
1049 if any. */
1050 struct partial_die_info *die_parent = nullptr;
1051 struct partial_die_info *die_child = nullptr;
1052 struct partial_die_info *die_sibling = nullptr;
1053
1054 friend struct partial_die_info *
1055 dwarf2_cu::find_partial_die (sect_offset sect_off);
1056
1057 private:
1058 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1059 partial_die_info (sect_offset sect_off)
1060 : partial_die_info (sect_off, DW_TAG_padding, 0)
1061 {
1062 }
1063
1064 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1065 int has_children_)
1066 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1067 {
1068 is_external = 0;
1069 is_declaration = 0;
1070 has_type = 0;
1071 has_specification = 0;
1072 has_pc_info = 0;
1073 may_be_inlined = 0;
1074 main_subprogram = 0;
1075 scope_set = 0;
1076 has_byte_size = 0;
1077 has_const_value = 0;
1078 has_template_arguments = 0;
1079 fixup_called = 0;
1080 is_dwz = 0;
1081 spec_is_dwz = 0;
1082 }
1083 };
1084
1085 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1086 but this would require a corresponding change in unpack_field_as_long
1087 and friends. */
1088 static int bits_per_byte = 8;
1089
1090 struct variant_part_builder;
1091
1092 /* When reading a variant, we track a bit more information about the
1093 field, and store it in an object of this type. */
1094
1095 struct variant_field
1096 {
1097 int first_field = -1;
1098 int last_field = -1;
1099
1100 /* A variant can contain other variant parts. */
1101 std::vector<variant_part_builder> variant_parts;
1102
1103 /* If we see a DW_TAG_variant, then this will be set if this is the
1104 default branch. */
1105 bool default_branch = false;
1106 /* If we see a DW_AT_discr_value, then this will be the discriminant
1107 value. */
1108 ULONGEST discriminant_value = 0;
1109 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1110 data. */
1111 struct dwarf_block *discr_list_data = nullptr;
1112 };
1113
1114 /* This represents a DW_TAG_variant_part. */
1115
1116 struct variant_part_builder
1117 {
1118 /* The offset of the discriminant field. */
1119 sect_offset discriminant_offset {};
1120
1121 /* Variants that are direct children of this variant part. */
1122 std::vector<variant_field> variants;
1123
1124 /* True if we're currently reading a variant. */
1125 bool processing_variant = false;
1126 };
1127
1128 struct nextfield
1129 {
1130 int accessibility = 0;
1131 int virtuality = 0;
1132 /* Variant parts need to find the discriminant, which is a DIE
1133 reference. We track the section offset of each field to make
1134 this link. */
1135 sect_offset offset;
1136 struct field field {};
1137 };
1138
1139 struct fnfieldlist
1140 {
1141 const char *name = nullptr;
1142 std::vector<struct fn_field> fnfields;
1143 };
1144
1145 /* The routines that read and process dies for a C struct or C++ class
1146 pass lists of data member fields and lists of member function fields
1147 in an instance of a field_info structure, as defined below. */
1148 struct field_info
1149 {
1150 /* List of data member and baseclasses fields. */
1151 std::vector<struct nextfield> fields;
1152 std::vector<struct nextfield> baseclasses;
1153
1154 /* Set if the accessibility of one of the fields is not public. */
1155 int non_public_fields = 0;
1156
1157 /* Member function fieldlist array, contains name of possibly overloaded
1158 member function, number of overloaded member functions and a pointer
1159 to the head of the member function field chain. */
1160 std::vector<struct fnfieldlist> fnfieldlists;
1161
1162 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1163 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1164 std::vector<struct decl_field> typedef_field_list;
1165
1166 /* Nested types defined by this class and the number of elements in this
1167 list. */
1168 std::vector<struct decl_field> nested_types_list;
1169
1170 /* If non-null, this is the variant part we are currently
1171 reading. */
1172 variant_part_builder *current_variant_part = nullptr;
1173 /* This holds all the top-level variant parts attached to the type
1174 we're reading. */
1175 std::vector<variant_part_builder> variant_parts;
1176
1177 /* Return the total number of fields (including baseclasses). */
1178 int nfields () const
1179 {
1180 return fields.size () + baseclasses.size ();
1181 }
1182 };
1183
1184 /* Loaded secondary compilation units are kept in memory until they
1185 have not been referenced for the processing of this many
1186 compilation units. Set this to zero to disable caching. Cache
1187 sizes of up to at least twenty will improve startup time for
1188 typical inter-CU-reference binaries, at an obvious memory cost. */
1189 static int dwarf_max_cache_age = 5;
1190 static void
1191 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1192 struct cmd_list_element *c, const char *value)
1193 {
1194 fprintf_filtered (file, _("The upper bound on the age of cached "
1195 "DWARF compilation units is %s.\n"),
1196 value);
1197 }
1198 \f
1199 /* local function prototypes */
1200
1201 static void dwarf2_find_base_address (struct die_info *die,
1202 struct dwarf2_cu *cu);
1203
1204 static dwarf2_psymtab *create_partial_symtab
1205 (struct dwarf2_per_cu_data *per_cu, const char *name);
1206
1207 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1208 const gdb_byte *info_ptr,
1209 struct die_info *type_unit_die);
1210
1211 static void dwarf2_build_psymtabs_hard
1212 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1213
1214 static void scan_partial_symbols (struct partial_die_info *,
1215 CORE_ADDR *, CORE_ADDR *,
1216 int, struct dwarf2_cu *);
1217
1218 static void add_partial_symbol (struct partial_die_info *,
1219 struct dwarf2_cu *);
1220
1221 static void add_partial_namespace (struct partial_die_info *pdi,
1222 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1223 int set_addrmap, struct dwarf2_cu *cu);
1224
1225 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1226 CORE_ADDR *highpc, int set_addrmap,
1227 struct dwarf2_cu *cu);
1228
1229 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1230 struct dwarf2_cu *cu);
1231
1232 static void add_partial_subprogram (struct partial_die_info *pdi,
1233 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1234 int need_pc, struct dwarf2_cu *cu);
1235
1236 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1237
1238 static struct partial_die_info *load_partial_dies
1239 (const struct die_reader_specs *, const gdb_byte *, int);
1240
1241 /* A pair of partial_die_info and compilation unit. */
1242 struct cu_partial_die_info
1243 {
1244 /* The compilation unit of the partial_die_info. */
1245 struct dwarf2_cu *cu;
1246 /* A partial_die_info. */
1247 struct partial_die_info *pdi;
1248
1249 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1250 : cu (cu),
1251 pdi (pdi)
1252 { /* Nothing. */ }
1253
1254 private:
1255 cu_partial_die_info () = delete;
1256 };
1257
1258 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1259 struct dwarf2_cu *);
1260
1261 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1262 struct attribute *, struct attr_abbrev *,
1263 const gdb_byte *, bool *need_reprocess);
1264
1265 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1266 struct attribute *attr);
1267
1268 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1269
1270 static sect_offset read_abbrev_offset
1271 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1272 struct dwarf2_section_info *, sect_offset);
1273
1274 static const char *read_indirect_string
1275 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1276 const struct comp_unit_head *, unsigned int *);
1277
1278 static const char *read_indirect_string_at_offset
1279 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1280
1281 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1282 const gdb_byte *,
1283 unsigned int *);
1284
1285 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1286 ULONGEST str_index);
1287
1288 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1289 ULONGEST str_index);
1290
1291 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1292
1293 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1294 struct dwarf2_cu *);
1295
1296 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1297 struct dwarf2_cu *cu);
1298
1299 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1300
1301 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1302 struct dwarf2_cu *cu);
1303
1304 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1305
1306 static struct die_info *die_specification (struct die_info *die,
1307 struct dwarf2_cu **);
1308
1309 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1310 struct dwarf2_cu *cu);
1311
1312 static void dwarf_decode_lines (struct line_header *, const char *,
1313 struct dwarf2_cu *, dwarf2_psymtab *,
1314 CORE_ADDR, int decode_mapping);
1315
1316 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1317 const char *);
1318
1319 static struct symbol *new_symbol (struct die_info *, struct type *,
1320 struct dwarf2_cu *, struct symbol * = NULL);
1321
1322 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1323 struct dwarf2_cu *);
1324
1325 static void dwarf2_const_value_attr (const struct attribute *attr,
1326 struct type *type,
1327 const char *name,
1328 struct obstack *obstack,
1329 struct dwarf2_cu *cu, LONGEST *value,
1330 const gdb_byte **bytes,
1331 struct dwarf2_locexpr_baton **baton);
1332
1333 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1334
1335 static int need_gnat_info (struct dwarf2_cu *);
1336
1337 static struct type *die_descriptive_type (struct die_info *,
1338 struct dwarf2_cu *);
1339
1340 static void set_descriptive_type (struct type *, struct die_info *,
1341 struct dwarf2_cu *);
1342
1343 static struct type *die_containing_type (struct die_info *,
1344 struct dwarf2_cu *);
1345
1346 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1347 struct dwarf2_cu *);
1348
1349 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1350
1351 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1352
1353 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1354
1355 static char *typename_concat (struct obstack *obs, const char *prefix,
1356 const char *suffix, int physname,
1357 struct dwarf2_cu *cu);
1358
1359 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1360
1361 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1362
1363 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1364
1365 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1366
1367 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1368
1369 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1370
1371 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1372 struct dwarf2_cu *, dwarf2_psymtab *);
1373
1374 /* Return the .debug_loclists section to use for cu. */
1375 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1376
1377 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1378 values. Keep the items ordered with increasing constraints compliance. */
1379 enum pc_bounds_kind
1380 {
1381 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1382 PC_BOUNDS_NOT_PRESENT,
1383
1384 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1385 were present but they do not form a valid range of PC addresses. */
1386 PC_BOUNDS_INVALID,
1387
1388 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1389 PC_BOUNDS_RANGES,
1390
1391 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1392 PC_BOUNDS_HIGH_LOW,
1393 };
1394
1395 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1396 CORE_ADDR *, CORE_ADDR *,
1397 struct dwarf2_cu *,
1398 dwarf2_psymtab *);
1399
1400 static void get_scope_pc_bounds (struct die_info *,
1401 CORE_ADDR *, CORE_ADDR *,
1402 struct dwarf2_cu *);
1403
1404 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1405 CORE_ADDR, struct dwarf2_cu *);
1406
1407 static void dwarf2_add_field (struct field_info *, struct die_info *,
1408 struct dwarf2_cu *);
1409
1410 static void dwarf2_attach_fields_to_type (struct field_info *,
1411 struct type *, struct dwarf2_cu *);
1412
1413 static void dwarf2_add_member_fn (struct field_info *,
1414 struct die_info *, struct type *,
1415 struct dwarf2_cu *);
1416
1417 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1418 struct type *,
1419 struct dwarf2_cu *);
1420
1421 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1422
1423 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1424
1425 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1426
1427 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1428
1429 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1430
1431 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1432
1433 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1434
1435 static struct type *read_module_type (struct die_info *die,
1436 struct dwarf2_cu *cu);
1437
1438 static const char *namespace_name (struct die_info *die,
1439 int *is_anonymous, struct dwarf2_cu *);
1440
1441 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1442
1443 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1444 bool * = nullptr);
1445
1446 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1447 struct dwarf2_cu *);
1448
1449 static struct die_info *read_die_and_siblings_1
1450 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1451 struct die_info *);
1452
1453 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1454 const gdb_byte *info_ptr,
1455 const gdb_byte **new_info_ptr,
1456 struct die_info *parent);
1457
1458 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1459 struct die_info **, const gdb_byte *,
1460 int);
1461
1462 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1463 struct die_info **, const gdb_byte *);
1464
1465 static void process_die (struct die_info *, struct dwarf2_cu *);
1466
1467 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1468 struct objfile *);
1469
1470 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1471
1472 static const char *dwarf2_full_name (const char *name,
1473 struct die_info *die,
1474 struct dwarf2_cu *cu);
1475
1476 static const char *dwarf2_physname (const char *name, struct die_info *die,
1477 struct dwarf2_cu *cu);
1478
1479 static struct die_info *dwarf2_extension (struct die_info *die,
1480 struct dwarf2_cu **);
1481
1482 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1483
1484 static void dump_die_for_error (struct die_info *);
1485
1486 static void dump_die_1 (struct ui_file *, int level, int max_level,
1487 struct die_info *);
1488
1489 /*static*/ void dump_die (struct die_info *, int max_level);
1490
1491 static void store_in_ref_table (struct die_info *,
1492 struct dwarf2_cu *);
1493
1494 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1495 const struct attribute *,
1496 struct dwarf2_cu **);
1497
1498 static struct die_info *follow_die_ref (struct die_info *,
1499 const struct attribute *,
1500 struct dwarf2_cu **);
1501
1502 static struct die_info *follow_die_sig (struct die_info *,
1503 const struct attribute *,
1504 struct dwarf2_cu **);
1505
1506 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1507 struct dwarf2_cu *);
1508
1509 static struct type *get_DW_AT_signature_type (struct die_info *,
1510 const struct attribute *,
1511 struct dwarf2_cu *);
1512
1513 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1514
1515 static void read_signatured_type (struct signatured_type *);
1516
1517 static int attr_to_dynamic_prop (const struct attribute *attr,
1518 struct die_info *die, struct dwarf2_cu *cu,
1519 struct dynamic_prop *prop, struct type *type);
1520
1521 /* memory allocation interface */
1522
1523 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1524
1525 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1526
1527 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1528
1529 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1530 struct dwarf2_loclist_baton *baton,
1531 const struct attribute *attr);
1532
1533 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1534 struct symbol *sym,
1535 struct dwarf2_cu *cu,
1536 int is_block);
1537
1538 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1539 const gdb_byte *info_ptr,
1540 struct abbrev_info *abbrev);
1541
1542 static hashval_t partial_die_hash (const void *item);
1543
1544 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1545
1546 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1547 (sect_offset sect_off, unsigned int offset_in_dwz,
1548 struct dwarf2_per_objfile *dwarf2_per_objfile);
1549
1550 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1551 struct die_info *comp_unit_die,
1552 enum language pretend_language);
1553
1554 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1555
1556 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1557
1558 static struct type *set_die_type (struct die_info *, struct type *,
1559 struct dwarf2_cu *);
1560
1561 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1562
1563 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1564
1565 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1566 enum language);
1567
1568 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1569 enum language);
1570
1571 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1572 enum language);
1573
1574 static void dwarf2_add_dependence (struct dwarf2_cu *,
1575 struct dwarf2_per_cu_data *);
1576
1577 static void dwarf2_mark (struct dwarf2_cu *);
1578
1579 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1580
1581 static struct type *get_die_type_at_offset (sect_offset,
1582 struct dwarf2_per_cu_data *);
1583
1584 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1585
1586 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1587 enum language pretend_language);
1588
1589 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1590
1591 /* Class, the destructor of which frees all allocated queue entries. This
1592 will only have work to do if an error was thrown while processing the
1593 dwarf. If no error was thrown then the queue entries should have all
1594 been processed, and freed, as we went along. */
1595
1596 class dwarf2_queue_guard
1597 {
1598 public:
1599 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1600 : m_per_objfile (per_objfile)
1601 {
1602 }
1603
1604 /* Free any entries remaining on the queue. There should only be
1605 entries left if we hit an error while processing the dwarf. */
1606 ~dwarf2_queue_guard ()
1607 {
1608 /* Ensure that no memory is allocated by the queue. */
1609 std::queue<dwarf2_queue_item> empty;
1610 std::swap (m_per_objfile->per_bfd->queue, empty);
1611 }
1612
1613 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1614
1615 private:
1616 dwarf2_per_objfile *m_per_objfile;
1617 };
1618
1619 dwarf2_queue_item::~dwarf2_queue_item ()
1620 {
1621 /* Anything still marked queued is likely to be in an
1622 inconsistent state, so discard it. */
1623 if (per_cu->queued)
1624 {
1625 if (per_cu->cu != NULL)
1626 free_one_cached_comp_unit (per_cu);
1627 per_cu->queued = 0;
1628 }
1629 }
1630
1631 /* The return type of find_file_and_directory. Note, the enclosed
1632 string pointers are only valid while this object is valid. */
1633
1634 struct file_and_directory
1635 {
1636 /* The filename. This is never NULL. */
1637 const char *name;
1638
1639 /* The compilation directory. NULL if not known. If we needed to
1640 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1641 points directly to the DW_AT_comp_dir string attribute owned by
1642 the obstack that owns the DIE. */
1643 const char *comp_dir;
1644
1645 /* If we needed to build a new string for comp_dir, this is what
1646 owns the storage. */
1647 std::string comp_dir_storage;
1648 };
1649
1650 static file_and_directory find_file_and_directory (struct die_info *die,
1651 struct dwarf2_cu *cu);
1652
1653 static htab_up allocate_signatured_type_table ();
1654
1655 static htab_up allocate_dwo_unit_table ();
1656
1657 static struct dwo_unit *lookup_dwo_unit_in_dwp
1658 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1659 struct dwp_file *dwp_file, const char *comp_dir,
1660 ULONGEST signature, int is_debug_types);
1661
1662 static struct dwp_file *get_dwp_file
1663 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1664
1665 static struct dwo_unit *lookup_dwo_comp_unit
1666 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1667
1668 static struct dwo_unit *lookup_dwo_type_unit
1669 (struct signatured_type *, const char *, const char *);
1670
1671 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1672
1673 /* A unique pointer to a dwo_file. */
1674
1675 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1676
1677 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1678
1679 static void check_producer (struct dwarf2_cu *cu);
1680
1681 static void free_line_header_voidp (void *arg);
1682 \f
1683 /* Various complaints about symbol reading that don't abort the process. */
1684
1685 static void
1686 dwarf2_debug_line_missing_file_complaint (void)
1687 {
1688 complaint (_(".debug_line section has line data without a file"));
1689 }
1690
1691 static void
1692 dwarf2_debug_line_missing_end_sequence_complaint (void)
1693 {
1694 complaint (_(".debug_line section has line "
1695 "program sequence without an end"));
1696 }
1697
1698 static void
1699 dwarf2_complex_location_expr_complaint (void)
1700 {
1701 complaint (_("location expression too complex"));
1702 }
1703
1704 static void
1705 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1706 int arg3)
1707 {
1708 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1709 arg1, arg2, arg3);
1710 }
1711
1712 static void
1713 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1714 {
1715 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1716 arg1, arg2);
1717 }
1718
1719 /* Hash function for line_header_hash. */
1720
1721 static hashval_t
1722 line_header_hash (const struct line_header *ofs)
1723 {
1724 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1725 }
1726
1727 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1728
1729 static hashval_t
1730 line_header_hash_voidp (const void *item)
1731 {
1732 const struct line_header *ofs = (const struct line_header *) item;
1733
1734 return line_header_hash (ofs);
1735 }
1736
1737 /* Equality function for line_header_hash. */
1738
1739 static int
1740 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1741 {
1742 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1743 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1744
1745 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1746 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1747 }
1748
1749 \f
1750
1751 /* See declaration. */
1752
1753 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1754 bool can_copy_)
1755 : obfd (obfd),
1756 can_copy (can_copy_)
1757 {
1758 if (names == NULL)
1759 names = &dwarf2_elf_names;
1760
1761 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1762 locate_sections (obfd, sec, *names);
1763 }
1764
1765 dwarf2_per_bfd::~dwarf2_per_bfd ()
1766 {
1767 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1768 free_cached_comp_units ();
1769
1770 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1771 per_cu->imported_symtabs_free ();
1772
1773 for (signatured_type *sig_type : all_type_units)
1774 sig_type->per_cu.imported_symtabs_free ();
1775
1776 /* Everything else should be on this->obstack. */
1777 }
1778
1779 /* See declaration. */
1780
1781 void
1782 dwarf2_per_bfd::free_cached_comp_units ()
1783 {
1784 dwarf2_per_cu_data *per_cu = read_in_chain;
1785 dwarf2_per_cu_data **last_chain = &read_in_chain;
1786 while (per_cu != NULL)
1787 {
1788 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1789
1790 delete per_cu->cu;
1791 *last_chain = next_cu;
1792 per_cu = next_cu;
1793 }
1794 }
1795
1796 /* A helper class that calls free_cached_comp_units on
1797 destruction. */
1798
1799 class free_cached_comp_units
1800 {
1801 public:
1802
1803 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1804 : m_per_objfile (per_objfile)
1805 {
1806 }
1807
1808 ~free_cached_comp_units ()
1809 {
1810 m_per_objfile->per_bfd->free_cached_comp_units ();
1811 }
1812
1813 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1814
1815 private:
1816
1817 dwarf2_per_objfile *m_per_objfile;
1818 };
1819
1820 /* See read.h. */
1821
1822 bool
1823 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1824 {
1825 gdb_assert (per_cu->index < this->m_symtabs.size ());
1826
1827 return this->m_symtabs[per_cu->index] != nullptr;
1828 }
1829
1830 /* See read.h. */
1831
1832 compunit_symtab *
1833 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1834 {
1835 gdb_assert (per_cu->index < this->m_symtabs.size ());
1836
1837 return this->m_symtabs[per_cu->index];
1838 }
1839
1840 /* See read.h. */
1841
1842 void
1843 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1844 compunit_symtab *symtab)
1845 {
1846 gdb_assert (per_cu->index < this->m_symtabs.size ());
1847 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1848
1849 this->m_symtabs[per_cu->index] = symtab;
1850 }
1851
1852 /* Try to locate the sections we need for DWARF 2 debugging
1853 information and return true if we have enough to do something.
1854 NAMES points to the dwarf2 section names, or is NULL if the standard
1855 ELF names are used. CAN_COPY is true for formats where symbol
1856 interposition is possible and so symbol values must follow copy
1857 relocation rules. */
1858
1859 int
1860 dwarf2_has_info (struct objfile *objfile,
1861 const struct dwarf2_debug_sections *names,
1862 bool can_copy)
1863 {
1864 if (objfile->flags & OBJF_READNEVER)
1865 return 0;
1866
1867 struct dwarf2_per_objfile *dwarf2_per_objfile
1868 = get_dwarf2_per_objfile (objfile);
1869
1870 if (dwarf2_per_objfile == NULL)
1871 {
1872 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1873 sharing yet). */
1874 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1875
1876 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1877 }
1878
1879 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1880 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1881 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1882 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
1883 }
1884
1885 /* When loading sections, we look either for uncompressed section or for
1886 compressed section names. */
1887
1888 static int
1889 section_is_p (const char *section_name,
1890 const struct dwarf2_section_names *names)
1891 {
1892 if (names->normal != NULL
1893 && strcmp (section_name, names->normal) == 0)
1894 return 1;
1895 if (names->compressed != NULL
1896 && strcmp (section_name, names->compressed) == 0)
1897 return 1;
1898 return 0;
1899 }
1900
1901 /* See declaration. */
1902
1903 void
1904 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1905 const dwarf2_debug_sections &names)
1906 {
1907 flagword aflag = bfd_section_flags (sectp);
1908
1909 if ((aflag & SEC_HAS_CONTENTS) == 0)
1910 {
1911 }
1912 else if (elf_section_data (sectp)->this_hdr.sh_size
1913 > bfd_get_file_size (abfd))
1914 {
1915 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1916 warning (_("Discarding section %s which has a section size (%s"
1917 ") larger than the file size [in module %s]"),
1918 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1919 bfd_get_filename (abfd));
1920 }
1921 else if (section_is_p (sectp->name, &names.info))
1922 {
1923 this->info.s.section = sectp;
1924 this->info.size = bfd_section_size (sectp);
1925 }
1926 else if (section_is_p (sectp->name, &names.abbrev))
1927 {
1928 this->abbrev.s.section = sectp;
1929 this->abbrev.size = bfd_section_size (sectp);
1930 }
1931 else if (section_is_p (sectp->name, &names.line))
1932 {
1933 this->line.s.section = sectp;
1934 this->line.size = bfd_section_size (sectp);
1935 }
1936 else if (section_is_p (sectp->name, &names.loc))
1937 {
1938 this->loc.s.section = sectp;
1939 this->loc.size = bfd_section_size (sectp);
1940 }
1941 else if (section_is_p (sectp->name, &names.loclists))
1942 {
1943 this->loclists.s.section = sectp;
1944 this->loclists.size = bfd_section_size (sectp);
1945 }
1946 else if (section_is_p (sectp->name, &names.macinfo))
1947 {
1948 this->macinfo.s.section = sectp;
1949 this->macinfo.size = bfd_section_size (sectp);
1950 }
1951 else if (section_is_p (sectp->name, &names.macro))
1952 {
1953 this->macro.s.section = sectp;
1954 this->macro.size = bfd_section_size (sectp);
1955 }
1956 else if (section_is_p (sectp->name, &names.str))
1957 {
1958 this->str.s.section = sectp;
1959 this->str.size = bfd_section_size (sectp);
1960 }
1961 else if (section_is_p (sectp->name, &names.str_offsets))
1962 {
1963 this->str_offsets.s.section = sectp;
1964 this->str_offsets.size = bfd_section_size (sectp);
1965 }
1966 else if (section_is_p (sectp->name, &names.line_str))
1967 {
1968 this->line_str.s.section = sectp;
1969 this->line_str.size = bfd_section_size (sectp);
1970 }
1971 else if (section_is_p (sectp->name, &names.addr))
1972 {
1973 this->addr.s.section = sectp;
1974 this->addr.size = bfd_section_size (sectp);
1975 }
1976 else if (section_is_p (sectp->name, &names.frame))
1977 {
1978 this->frame.s.section = sectp;
1979 this->frame.size = bfd_section_size (sectp);
1980 }
1981 else if (section_is_p (sectp->name, &names.eh_frame))
1982 {
1983 this->eh_frame.s.section = sectp;
1984 this->eh_frame.size = bfd_section_size (sectp);
1985 }
1986 else if (section_is_p (sectp->name, &names.ranges))
1987 {
1988 this->ranges.s.section = sectp;
1989 this->ranges.size = bfd_section_size (sectp);
1990 }
1991 else if (section_is_p (sectp->name, &names.rnglists))
1992 {
1993 this->rnglists.s.section = sectp;
1994 this->rnglists.size = bfd_section_size (sectp);
1995 }
1996 else if (section_is_p (sectp->name, &names.types))
1997 {
1998 struct dwarf2_section_info type_section;
1999
2000 memset (&type_section, 0, sizeof (type_section));
2001 type_section.s.section = sectp;
2002 type_section.size = bfd_section_size (sectp);
2003
2004 this->types.push_back (type_section);
2005 }
2006 else if (section_is_p (sectp->name, &names.gdb_index))
2007 {
2008 this->gdb_index.s.section = sectp;
2009 this->gdb_index.size = bfd_section_size (sectp);
2010 }
2011 else if (section_is_p (sectp->name, &names.debug_names))
2012 {
2013 this->debug_names.s.section = sectp;
2014 this->debug_names.size = bfd_section_size (sectp);
2015 }
2016 else if (section_is_p (sectp->name, &names.debug_aranges))
2017 {
2018 this->debug_aranges.s.section = sectp;
2019 this->debug_aranges.size = bfd_section_size (sectp);
2020 }
2021
2022 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2023 && bfd_section_vma (sectp) == 0)
2024 this->has_section_at_zero = true;
2025 }
2026
2027 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2028 SECTION_NAME. */
2029
2030 void
2031 dwarf2_get_section_info (struct objfile *objfile,
2032 enum dwarf2_section_enum sect,
2033 asection **sectp, const gdb_byte **bufp,
2034 bfd_size_type *sizep)
2035 {
2036 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2037 struct dwarf2_section_info *info;
2038
2039 /* We may see an objfile without any DWARF, in which case we just
2040 return nothing. */
2041 if (data == NULL)
2042 {
2043 *sectp = NULL;
2044 *bufp = NULL;
2045 *sizep = 0;
2046 return;
2047 }
2048 switch (sect)
2049 {
2050 case DWARF2_DEBUG_FRAME:
2051 info = &data->per_bfd->frame;
2052 break;
2053 case DWARF2_EH_FRAME:
2054 info = &data->per_bfd->eh_frame;
2055 break;
2056 default:
2057 gdb_assert_not_reached ("unexpected section");
2058 }
2059
2060 info->read (objfile);
2061
2062 *sectp = info->get_bfd_section ();
2063 *bufp = info->buffer;
2064 *sizep = info->size;
2065 }
2066
2067 /* A helper function to find the sections for a .dwz file. */
2068
2069 static void
2070 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2071 {
2072 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2073
2074 /* Note that we only support the standard ELF names, because .dwz
2075 is ELF-only (at the time of writing). */
2076 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2077 {
2078 dwz_file->abbrev.s.section = sectp;
2079 dwz_file->abbrev.size = bfd_section_size (sectp);
2080 }
2081 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2082 {
2083 dwz_file->info.s.section = sectp;
2084 dwz_file->info.size = bfd_section_size (sectp);
2085 }
2086 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2087 {
2088 dwz_file->str.s.section = sectp;
2089 dwz_file->str.size = bfd_section_size (sectp);
2090 }
2091 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2092 {
2093 dwz_file->line.s.section = sectp;
2094 dwz_file->line.size = bfd_section_size (sectp);
2095 }
2096 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2097 {
2098 dwz_file->macro.s.section = sectp;
2099 dwz_file->macro.size = bfd_section_size (sectp);
2100 }
2101 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2102 {
2103 dwz_file->gdb_index.s.section = sectp;
2104 dwz_file->gdb_index.size = bfd_section_size (sectp);
2105 }
2106 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2107 {
2108 dwz_file->debug_names.s.section = sectp;
2109 dwz_file->debug_names.size = bfd_section_size (sectp);
2110 }
2111 }
2112
2113 /* See dwarf2read.h. */
2114
2115 struct dwz_file *
2116 dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
2117 {
2118 const char *filename;
2119 bfd_size_type buildid_len_arg;
2120 size_t buildid_len;
2121 bfd_byte *buildid;
2122
2123 if (per_bfd->dwz_file != NULL)
2124 return per_bfd->dwz_file.get ();
2125
2126 bfd_set_error (bfd_error_no_error);
2127 gdb::unique_xmalloc_ptr<char> data
2128 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2129 &buildid_len_arg, &buildid));
2130 if (data == NULL)
2131 {
2132 if (bfd_get_error () == bfd_error_no_error)
2133 return NULL;
2134 error (_("could not read '.gnu_debugaltlink' section: %s"),
2135 bfd_errmsg (bfd_get_error ()));
2136 }
2137
2138 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2139
2140 buildid_len = (size_t) buildid_len_arg;
2141
2142 filename = data.get ();
2143
2144 std::string abs_storage;
2145 if (!IS_ABSOLUTE_PATH (filename))
2146 {
2147 gdb::unique_xmalloc_ptr<char> abs
2148 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2149
2150 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2151 filename = abs_storage.c_str ();
2152 }
2153
2154 /* First try the file name given in the section. If that doesn't
2155 work, try to use the build-id instead. */
2156 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
2157 if (dwz_bfd != NULL)
2158 {
2159 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2160 dwz_bfd.reset (nullptr);
2161 }
2162
2163 if (dwz_bfd == NULL)
2164 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2165
2166 if (dwz_bfd == nullptr)
2167 {
2168 gdb::unique_xmalloc_ptr<char> alt_filename;
2169 const char *origname = bfd_get_filename (per_bfd->obfd);
2170
2171 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2172 buildid_len,
2173 origname,
2174 &alt_filename));
2175
2176 if (fd.get () >= 0)
2177 {
2178 /* File successfully retrieved from server. */
2179 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2180
2181 if (dwz_bfd == nullptr)
2182 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2183 alt_filename.get ());
2184 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2185 dwz_bfd.reset (nullptr);
2186 }
2187 }
2188
2189 if (dwz_bfd == NULL)
2190 error (_("could not find '.gnu_debugaltlink' file for %s"),
2191 bfd_get_filename (per_bfd->obfd));
2192
2193 std::unique_ptr<struct dwz_file> result
2194 (new struct dwz_file (std::move (dwz_bfd)));
2195
2196 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2197 result.get ());
2198
2199 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2200 per_bfd->dwz_file = std::move (result);
2201 return per_bfd->dwz_file.get ();
2202 }
2203 \f
2204 /* DWARF quick_symbols_functions support. */
2205
2206 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2207 unique line tables, so we maintain a separate table of all .debug_line
2208 derived entries to support the sharing.
2209 All the quick functions need is the list of file names. We discard the
2210 line_header when we're done and don't need to record it here. */
2211 struct quick_file_names
2212 {
2213 /* The data used to construct the hash key. */
2214 struct stmt_list_hash hash;
2215
2216 /* The number of entries in file_names, real_names. */
2217 unsigned int num_file_names;
2218
2219 /* The file names from the line table, after being run through
2220 file_full_name. */
2221 const char **file_names;
2222
2223 /* The file names from the line table after being run through
2224 gdb_realpath. These are computed lazily. */
2225 const char **real_names;
2226 };
2227
2228 /* When using the index (and thus not using psymtabs), each CU has an
2229 object of this type. This is used to hold information needed by
2230 the various "quick" methods. */
2231 struct dwarf2_per_cu_quick_data
2232 {
2233 /* The file table. This can be NULL if there was no file table
2234 or it's currently not read in.
2235 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2236 struct quick_file_names *file_names;
2237
2238 /* A temporary mark bit used when iterating over all CUs in
2239 expand_symtabs_matching. */
2240 unsigned int mark : 1;
2241
2242 /* True if we've tried to read the file table and found there isn't one.
2243 There will be no point in trying to read it again next time. */
2244 unsigned int no_file_data : 1;
2245 };
2246
2247 /* Utility hash function for a stmt_list_hash. */
2248
2249 static hashval_t
2250 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2251 {
2252 hashval_t v = 0;
2253
2254 if (stmt_list_hash->dwo_unit != NULL)
2255 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2256 v += to_underlying (stmt_list_hash->line_sect_off);
2257 return v;
2258 }
2259
2260 /* Utility equality function for a stmt_list_hash. */
2261
2262 static int
2263 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2264 const struct stmt_list_hash *rhs)
2265 {
2266 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2267 return 0;
2268 if (lhs->dwo_unit != NULL
2269 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2270 return 0;
2271
2272 return lhs->line_sect_off == rhs->line_sect_off;
2273 }
2274
2275 /* Hash function for a quick_file_names. */
2276
2277 static hashval_t
2278 hash_file_name_entry (const void *e)
2279 {
2280 const struct quick_file_names *file_data
2281 = (const struct quick_file_names *) e;
2282
2283 return hash_stmt_list_entry (&file_data->hash);
2284 }
2285
2286 /* Equality function for a quick_file_names. */
2287
2288 static int
2289 eq_file_name_entry (const void *a, const void *b)
2290 {
2291 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2292 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2293
2294 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2295 }
2296
2297 /* Delete function for a quick_file_names. */
2298
2299 static void
2300 delete_file_name_entry (void *e)
2301 {
2302 struct quick_file_names *file_data = (struct quick_file_names *) e;
2303 int i;
2304
2305 for (i = 0; i < file_data->num_file_names; ++i)
2306 {
2307 xfree ((void*) file_data->file_names[i]);
2308 if (file_data->real_names)
2309 xfree ((void*) file_data->real_names[i]);
2310 }
2311
2312 /* The space for the struct itself lives on the obstack, so we don't
2313 free it here. */
2314 }
2315
2316 /* Create a quick_file_names hash table. */
2317
2318 static htab_up
2319 create_quick_file_names_table (unsigned int nr_initial_entries)
2320 {
2321 return htab_up (htab_create_alloc (nr_initial_entries,
2322 hash_file_name_entry, eq_file_name_entry,
2323 delete_file_name_entry, xcalloc, xfree));
2324 }
2325
2326 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2327 have to be created afterwards. You should call age_cached_comp_units after
2328 processing PER_CU->CU. dw2_setup must have been already called. */
2329
2330 static void
2331 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2332 {
2333 if (per_cu->is_debug_types)
2334 load_full_type_unit (per_cu);
2335 else
2336 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2337
2338 if (per_cu->cu == NULL)
2339 return; /* Dummy CU. */
2340
2341 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2342 }
2343
2344 /* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2345
2346 static void
2347 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2348 dwarf2_per_objfile *dwarf2_per_objfile,
2349 bool skip_partial)
2350 {
2351 /* Skip type_unit_groups, reading the type units they contain
2352 is handled elsewhere. */
2353 if (per_cu->type_unit_group_p ())
2354 return;
2355
2356 /* The destructor of dwarf2_queue_guard frees any entries left on
2357 the queue. After this point we're guaranteed to leave this function
2358 with the dwarf queue empty. */
2359 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2360
2361 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2362 {
2363 queue_comp_unit (per_cu, language_minimal);
2364 load_cu (per_cu, skip_partial);
2365
2366 /* If we just loaded a CU from a DWO, and we're working with an index
2367 that may badly handle TUs, load all the TUs in that DWO as well.
2368 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2369 if (!per_cu->is_debug_types
2370 && per_cu->cu != NULL
2371 && per_cu->cu->dwo_unit != NULL
2372 && dwarf2_per_objfile->per_bfd->index_table != NULL
2373 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
2374 /* DWP files aren't supported yet. */
2375 && get_dwp_file (dwarf2_per_objfile) == NULL)
2376 queue_and_load_all_dwo_tus (per_cu);
2377 }
2378
2379 process_queue (dwarf2_per_objfile);
2380
2381 /* Age the cache, releasing compilation units that have not
2382 been used recently. */
2383 age_cached_comp_units (dwarf2_per_objfile);
2384 }
2385
2386 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2387 the per-objfile for which this symtab is instantiated.
2388
2389 Returns the resulting symbol table. */
2390
2391 static struct compunit_symtab *
2392 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2393 dwarf2_per_objfile *dwarf2_per_objfile,
2394 bool skip_partial)
2395 {
2396 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
2397
2398 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2399 {
2400 free_cached_comp_units freer (dwarf2_per_objfile);
2401 scoped_restore decrementer = increment_reading_symtab ();
2402 dw2_do_instantiate_symtab (per_cu, dwarf2_per_objfile, skip_partial);
2403 process_cu_includes (dwarf2_per_objfile);
2404 }
2405
2406 return dwarf2_per_objfile->get_symtab (per_cu);
2407 }
2408
2409 /* See declaration. */
2410
2411 dwarf2_per_cu_data *
2412 dwarf2_per_bfd::get_cutu (int index)
2413 {
2414 if (index >= this->all_comp_units.size ())
2415 {
2416 index -= this->all_comp_units.size ();
2417 gdb_assert (index < this->all_type_units.size ());
2418 return &this->all_type_units[index]->per_cu;
2419 }
2420
2421 return this->all_comp_units[index];
2422 }
2423
2424 /* See declaration. */
2425
2426 dwarf2_per_cu_data *
2427 dwarf2_per_bfd::get_cu (int index)
2428 {
2429 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2430
2431 return this->all_comp_units[index];
2432 }
2433
2434 /* See declaration. */
2435
2436 signatured_type *
2437 dwarf2_per_bfd::get_tu (int index)
2438 {
2439 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2440
2441 return this->all_type_units[index];
2442 }
2443
2444 /* See read.h. */
2445
2446 dwarf2_per_cu_data *
2447 dwarf2_per_bfd::allocate_per_cu ()
2448 {
2449 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2450 result->per_bfd = this;
2451 result->index = m_num_psymtabs++;
2452 return result;
2453 }
2454
2455 /* See read.h. */
2456
2457 signatured_type *
2458 dwarf2_per_bfd::allocate_signatured_type ()
2459 {
2460 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2461 result->per_cu.per_bfd = this;
2462 result->per_cu.index = m_num_psymtabs++;
2463 return result;
2464 }
2465
2466 /* Return a new dwarf2_per_cu_data allocated on the dwarf2_per_objfile
2467 obstack, and constructed with the specified field values. */
2468
2469 static dwarf2_per_cu_data *
2470 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2471 struct dwarf2_section_info *section,
2472 int is_dwz,
2473 sect_offset sect_off, ULONGEST length)
2474 {
2475 dwarf2_per_cu_data *the_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
2476 the_cu->sect_off = sect_off;
2477 the_cu->length = length;
2478 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2479 the_cu->section = section;
2480 the_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2481 struct dwarf2_per_cu_quick_data);
2482 the_cu->is_dwz = is_dwz;
2483 return the_cu;
2484 }
2485
2486 /* A helper for create_cus_from_index that handles a given list of
2487 CUs. */
2488
2489 static void
2490 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2491 const gdb_byte *cu_list, offset_type n_elements,
2492 struct dwarf2_section_info *section,
2493 int is_dwz)
2494 {
2495 for (offset_type i = 0; i < n_elements; i += 2)
2496 {
2497 gdb_static_assert (sizeof (ULONGEST) >= 8);
2498
2499 sect_offset sect_off
2500 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2501 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2502 cu_list += 2 * 8;
2503
2504 dwarf2_per_cu_data *per_cu
2505 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2506 sect_off, length);
2507 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
2508 }
2509 }
2510
2511 /* Read the CU list from the mapped index, and use it to create all
2512 the CU objects for this objfile. */
2513
2514 static void
2515 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2516 const gdb_byte *cu_list, offset_type cu_list_elements,
2517 const gdb_byte *dwz_list, offset_type dwz_elements)
2518 {
2519 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
2520 dwarf2_per_objfile->per_bfd->all_comp_units.reserve
2521 ((cu_list_elements + dwz_elements) / 2);
2522
2523 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2524 &dwarf2_per_objfile->per_bfd->info, 0);
2525
2526 if (dwz_elements == 0)
2527 return;
2528
2529 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
2530 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2531 &dwz->info, 1);
2532 }
2533
2534 /* Create the signatured type hash table from the index. */
2535
2536 static void
2537 create_signatured_type_table_from_index
2538 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2539 struct dwarf2_section_info *section,
2540 const gdb_byte *bytes,
2541 offset_type elements)
2542 {
2543 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2544 dwarf2_per_objfile->per_bfd->all_type_units.reserve (elements / 3);
2545
2546 htab_up sig_types_hash = allocate_signatured_type_table ();
2547
2548 for (offset_type i = 0; i < elements; i += 3)
2549 {
2550 struct signatured_type *sig_type;
2551 ULONGEST signature;
2552 void **slot;
2553 cu_offset type_offset_in_tu;
2554
2555 gdb_static_assert (sizeof (ULONGEST) >= 8);
2556 sect_offset sect_off
2557 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2558 type_offset_in_tu
2559 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2560 BFD_ENDIAN_LITTLE);
2561 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2562 bytes += 3 * 8;
2563
2564 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2565 sig_type->signature = signature;
2566 sig_type->type_offset_in_tu = type_offset_in_tu;
2567 sig_type->per_cu.is_debug_types = 1;
2568 sig_type->per_cu.section = section;
2569 sig_type->per_cu.sect_off = sect_off;
2570 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2571 sig_type->per_cu.v.quick
2572 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2573 struct dwarf2_per_cu_quick_data);
2574
2575 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2576 *slot = sig_type;
2577
2578 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2579 }
2580
2581 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2582 }
2583
2584 /* Create the signatured type hash table from .debug_names. */
2585
2586 static void
2587 create_signatured_type_table_from_debug_names
2588 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2589 const mapped_debug_names &map,
2590 struct dwarf2_section_info *section,
2591 struct dwarf2_section_info *abbrev_section)
2592 {
2593 struct objfile *objfile = dwarf2_per_objfile->objfile;
2594
2595 section->read (objfile);
2596 abbrev_section->read (objfile);
2597
2598 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2599 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2600
2601 htab_up sig_types_hash = allocate_signatured_type_table ();
2602
2603 for (uint32_t i = 0; i < map.tu_count; ++i)
2604 {
2605 struct signatured_type *sig_type;
2606 void **slot;
2607
2608 sect_offset sect_off
2609 = (sect_offset) (extract_unsigned_integer
2610 (map.tu_table_reordered + i * map.offset_size,
2611 map.offset_size,
2612 map.dwarf5_byte_order));
2613
2614 comp_unit_head cu_header;
2615 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2616 abbrev_section,
2617 section->buffer + to_underlying (sect_off),
2618 rcuh_kind::TYPE);
2619
2620 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2621 sig_type->signature = cu_header.signature;
2622 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2623 sig_type->per_cu.is_debug_types = 1;
2624 sig_type->per_cu.section = section;
2625 sig_type->per_cu.sect_off = sect_off;
2626 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2627 sig_type->per_cu.v.quick
2628 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2629 struct dwarf2_per_cu_quick_data);
2630
2631 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2632 *slot = sig_type;
2633
2634 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2635 }
2636
2637 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2638 }
2639
2640 /* Read the address map data from the mapped index, and use it to
2641 populate the objfile's psymtabs_addrmap. */
2642
2643 static void
2644 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2645 struct mapped_index *index)
2646 {
2647 struct objfile *objfile = dwarf2_per_objfile->objfile;
2648 struct gdbarch *gdbarch = objfile->arch ();
2649 const gdb_byte *iter, *end;
2650 struct addrmap *mutable_map;
2651 CORE_ADDR baseaddr;
2652
2653 auto_obstack temp_obstack;
2654
2655 mutable_map = addrmap_create_mutable (&temp_obstack);
2656
2657 iter = index->address_table.data ();
2658 end = iter + index->address_table.size ();
2659
2660 baseaddr = objfile->text_section_offset ();
2661
2662 while (iter < end)
2663 {
2664 ULONGEST hi, lo, cu_index;
2665 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2666 iter += 8;
2667 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2668 iter += 8;
2669 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2670 iter += 4;
2671
2672 if (lo > hi)
2673 {
2674 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2675 hex_string (lo), hex_string (hi));
2676 continue;
2677 }
2678
2679 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
2680 {
2681 complaint (_(".gdb_index address table has invalid CU number %u"),
2682 (unsigned) cu_index);
2683 continue;
2684 }
2685
2686 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2687 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2688 addrmap_set_empty (mutable_map, lo, hi - 1,
2689 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
2690 }
2691
2692 objfile->partial_symtabs->psymtabs_addrmap
2693 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2694 }
2695
2696 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2697 populate the objfile's psymtabs_addrmap. */
2698
2699 static void
2700 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2701 struct dwarf2_section_info *section)
2702 {
2703 struct objfile *objfile = dwarf2_per_objfile->objfile;
2704 bfd *abfd = objfile->obfd;
2705 struct gdbarch *gdbarch = objfile->arch ();
2706 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2707
2708 auto_obstack temp_obstack;
2709 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2710
2711 std::unordered_map<sect_offset,
2712 dwarf2_per_cu_data *,
2713 gdb::hash_enum<sect_offset>>
2714 debug_info_offset_to_per_cu;
2715 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
2716 {
2717 const auto insertpair
2718 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2719 if (!insertpair.second)
2720 {
2721 warning (_("Section .debug_aranges in %s has duplicate "
2722 "debug_info_offset %s, ignoring .debug_aranges."),
2723 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2724 return;
2725 }
2726 }
2727
2728 section->read (objfile);
2729
2730 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2731
2732 const gdb_byte *addr = section->buffer;
2733
2734 while (addr < section->buffer + section->size)
2735 {
2736 const gdb_byte *const entry_addr = addr;
2737 unsigned int bytes_read;
2738
2739 const LONGEST entry_length = read_initial_length (abfd, addr,
2740 &bytes_read);
2741 addr += bytes_read;
2742
2743 const gdb_byte *const entry_end = addr + entry_length;
2744 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2745 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2746 if (addr + entry_length > section->buffer + section->size)
2747 {
2748 warning (_("Section .debug_aranges in %s entry at offset %s "
2749 "length %s exceeds section length %s, "
2750 "ignoring .debug_aranges."),
2751 objfile_name (objfile),
2752 plongest (entry_addr - section->buffer),
2753 plongest (bytes_read + entry_length),
2754 pulongest (section->size));
2755 return;
2756 }
2757
2758 /* The version number. */
2759 const uint16_t version = read_2_bytes (abfd, addr);
2760 addr += 2;
2761 if (version != 2)
2762 {
2763 warning (_("Section .debug_aranges in %s entry at offset %s "
2764 "has unsupported version %d, ignoring .debug_aranges."),
2765 objfile_name (objfile),
2766 plongest (entry_addr - section->buffer), version);
2767 return;
2768 }
2769
2770 const uint64_t debug_info_offset
2771 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2772 addr += offset_size;
2773 const auto per_cu_it
2774 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2775 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2776 {
2777 warning (_("Section .debug_aranges in %s entry at offset %s "
2778 "debug_info_offset %s does not exists, "
2779 "ignoring .debug_aranges."),
2780 objfile_name (objfile),
2781 plongest (entry_addr - section->buffer),
2782 pulongest (debug_info_offset));
2783 return;
2784 }
2785 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2786
2787 const uint8_t address_size = *addr++;
2788 if (address_size < 1 || address_size > 8)
2789 {
2790 warning (_("Section .debug_aranges in %s entry at offset %s "
2791 "address_size %u is invalid, ignoring .debug_aranges."),
2792 objfile_name (objfile),
2793 plongest (entry_addr - section->buffer), address_size);
2794 return;
2795 }
2796
2797 const uint8_t segment_selector_size = *addr++;
2798 if (segment_selector_size != 0)
2799 {
2800 warning (_("Section .debug_aranges in %s entry at offset %s "
2801 "segment_selector_size %u is not supported, "
2802 "ignoring .debug_aranges."),
2803 objfile_name (objfile),
2804 plongest (entry_addr - section->buffer),
2805 segment_selector_size);
2806 return;
2807 }
2808
2809 /* Must pad to an alignment boundary that is twice the address
2810 size. It is undocumented by the DWARF standard but GCC does
2811 use it. */
2812 for (size_t padding = ((-(addr - section->buffer))
2813 & (2 * address_size - 1));
2814 padding > 0; padding--)
2815 if (*addr++ != 0)
2816 {
2817 warning (_("Section .debug_aranges in %s entry at offset %s "
2818 "padding is not zero, ignoring .debug_aranges."),
2819 objfile_name (objfile),
2820 plongest (entry_addr - section->buffer));
2821 return;
2822 }
2823
2824 for (;;)
2825 {
2826 if (addr + 2 * address_size > entry_end)
2827 {
2828 warning (_("Section .debug_aranges in %s entry at offset %s "
2829 "address list is not properly terminated, "
2830 "ignoring .debug_aranges."),
2831 objfile_name (objfile),
2832 plongest (entry_addr - section->buffer));
2833 return;
2834 }
2835 ULONGEST start = extract_unsigned_integer (addr, address_size,
2836 dwarf5_byte_order);
2837 addr += address_size;
2838 ULONGEST length = extract_unsigned_integer (addr, address_size,
2839 dwarf5_byte_order);
2840 addr += address_size;
2841 if (start == 0 && length == 0)
2842 break;
2843 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
2844 {
2845 /* Symbol was eliminated due to a COMDAT group. */
2846 continue;
2847 }
2848 ULONGEST end = start + length;
2849 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2850 - baseaddr);
2851 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2852 - baseaddr);
2853 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2854 }
2855 }
2856
2857 objfile->partial_symtabs->psymtabs_addrmap
2858 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2859 }
2860
2861 /* Find a slot in the mapped index INDEX for the object named NAME.
2862 If NAME is found, set *VEC_OUT to point to the CU vector in the
2863 constant pool and return true. If NAME cannot be found, return
2864 false. */
2865
2866 static bool
2867 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2868 offset_type **vec_out)
2869 {
2870 offset_type hash;
2871 offset_type slot, step;
2872 int (*cmp) (const char *, const char *);
2873
2874 gdb::unique_xmalloc_ptr<char> without_params;
2875 if (current_language->la_language == language_cplus
2876 || current_language->la_language == language_fortran
2877 || current_language->la_language == language_d)
2878 {
2879 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2880 not contain any. */
2881
2882 if (strchr (name, '(') != NULL)
2883 {
2884 without_params = cp_remove_params (name);
2885
2886 if (without_params != NULL)
2887 name = without_params.get ();
2888 }
2889 }
2890
2891 /* Index version 4 did not support case insensitive searches. But the
2892 indices for case insensitive languages are built in lowercase, therefore
2893 simulate our NAME being searched is also lowercased. */
2894 hash = mapped_index_string_hash ((index->version == 4
2895 && case_sensitivity == case_sensitive_off
2896 ? 5 : index->version),
2897 name);
2898
2899 slot = hash & (index->symbol_table.size () - 1);
2900 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2901 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2902
2903 for (;;)
2904 {
2905 const char *str;
2906
2907 const auto &bucket = index->symbol_table[slot];
2908 if (bucket.name == 0 && bucket.vec == 0)
2909 return false;
2910
2911 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2912 if (!cmp (name, str))
2913 {
2914 *vec_out = (offset_type *) (index->constant_pool
2915 + MAYBE_SWAP (bucket.vec));
2916 return true;
2917 }
2918
2919 slot = (slot + step) & (index->symbol_table.size () - 1);
2920 }
2921 }
2922
2923 /* A helper function that reads the .gdb_index from BUFFER and fills
2924 in MAP. FILENAME is the name of the file containing the data;
2925 it is used for error reporting. DEPRECATED_OK is true if it is
2926 ok to use deprecated sections.
2927
2928 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2929 out parameters that are filled in with information about the CU and
2930 TU lists in the section.
2931
2932 Returns true if all went well, false otherwise. */
2933
2934 static bool
2935 read_gdb_index_from_buffer (const char *filename,
2936 bool deprecated_ok,
2937 gdb::array_view<const gdb_byte> buffer,
2938 struct mapped_index *map,
2939 const gdb_byte **cu_list,
2940 offset_type *cu_list_elements,
2941 const gdb_byte **types_list,
2942 offset_type *types_list_elements)
2943 {
2944 const gdb_byte *addr = &buffer[0];
2945
2946 /* Version check. */
2947 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2948 /* Versions earlier than 3 emitted every copy of a psymbol. This
2949 causes the index to behave very poorly for certain requests. Version 3
2950 contained incomplete addrmap. So, it seems better to just ignore such
2951 indices. */
2952 if (version < 4)
2953 {
2954 static int warning_printed = 0;
2955 if (!warning_printed)
2956 {
2957 warning (_("Skipping obsolete .gdb_index section in %s."),
2958 filename);
2959 warning_printed = 1;
2960 }
2961 return 0;
2962 }
2963 /* Index version 4 uses a different hash function than index version
2964 5 and later.
2965
2966 Versions earlier than 6 did not emit psymbols for inlined
2967 functions. Using these files will cause GDB not to be able to
2968 set breakpoints on inlined functions by name, so we ignore these
2969 indices unless the user has done
2970 "set use-deprecated-index-sections on". */
2971 if (version < 6 && !deprecated_ok)
2972 {
2973 static int warning_printed = 0;
2974 if (!warning_printed)
2975 {
2976 warning (_("\
2977 Skipping deprecated .gdb_index section in %s.\n\
2978 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2979 to use the section anyway."),
2980 filename);
2981 warning_printed = 1;
2982 }
2983 return 0;
2984 }
2985 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2986 of the TU (for symbols coming from TUs),
2987 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2988 Plus gold-generated indices can have duplicate entries for global symbols,
2989 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2990 These are just performance bugs, and we can't distinguish gdb-generated
2991 indices from gold-generated ones, so issue no warning here. */
2992
2993 /* Indexes with higher version than the one supported by GDB may be no
2994 longer backward compatible. */
2995 if (version > 8)
2996 return 0;
2997
2998 map->version = version;
2999
3000 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3001
3002 int i = 0;
3003 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3004 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3005 / 8);
3006 ++i;
3007
3008 *types_list = addr + MAYBE_SWAP (metadata[i]);
3009 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3010 - MAYBE_SWAP (metadata[i]))
3011 / 8);
3012 ++i;
3013
3014 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3015 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3016 map->address_table
3017 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3018 ++i;
3019
3020 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3021 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3022 map->symbol_table
3023 = gdb::array_view<mapped_index::symbol_table_slot>
3024 ((mapped_index::symbol_table_slot *) symbol_table,
3025 (mapped_index::symbol_table_slot *) symbol_table_end);
3026
3027 ++i;
3028 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3029
3030 return 1;
3031 }
3032
3033 /* Callback types for dwarf2_read_gdb_index. */
3034
3035 typedef gdb::function_view
3036 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3037 get_gdb_index_contents_ftype;
3038 typedef gdb::function_view
3039 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3040 get_gdb_index_contents_dwz_ftype;
3041
3042 /* Read .gdb_index. If everything went ok, initialize the "quick"
3043 elements of all the CUs and return 1. Otherwise, return 0. */
3044
3045 static int
3046 dwarf2_read_gdb_index
3047 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3048 get_gdb_index_contents_ftype get_gdb_index_contents,
3049 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3050 {
3051 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3052 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3053 struct dwz_file *dwz;
3054 struct objfile *objfile = dwarf2_per_objfile->objfile;
3055
3056 gdb::array_view<const gdb_byte> main_index_contents
3057 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
3058
3059 if (main_index_contents.empty ())
3060 return 0;
3061
3062 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3063 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3064 use_deprecated_index_sections,
3065 main_index_contents, map.get (), &cu_list,
3066 &cu_list_elements, &types_list,
3067 &types_list_elements))
3068 return 0;
3069
3070 /* Don't use the index if it's empty. */
3071 if (map->symbol_table.empty ())
3072 return 0;
3073
3074 /* If there is a .dwz file, read it so we can get its CU list as
3075 well. */
3076 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
3077 if (dwz != NULL)
3078 {
3079 struct mapped_index dwz_map;
3080 const gdb_byte *dwz_types_ignore;
3081 offset_type dwz_types_elements_ignore;
3082
3083 gdb::array_view<const gdb_byte> dwz_index_content
3084 = get_gdb_index_contents_dwz (objfile, dwz);
3085
3086 if (dwz_index_content.empty ())
3087 return 0;
3088
3089 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3090 1, dwz_index_content, &dwz_map,
3091 &dwz_list, &dwz_list_elements,
3092 &dwz_types_ignore,
3093 &dwz_types_elements_ignore))
3094 {
3095 warning (_("could not read '.gdb_index' section from %s; skipping"),
3096 bfd_get_filename (dwz->dwz_bfd.get ()));
3097 return 0;
3098 }
3099 }
3100
3101 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3102 dwz_list, dwz_list_elements);
3103
3104 if (types_list_elements)
3105 {
3106 /* We can only handle a single .debug_types when we have an
3107 index. */
3108 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
3109 return 0;
3110
3111 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
3112
3113 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3114 types_list, types_list_elements);
3115 }
3116
3117 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3118
3119 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3120 dwarf2_per_objfile->per_bfd->using_index = 1;
3121 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3122 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
3123
3124 return 1;
3125 }
3126
3127 /* die_reader_func for dw2_get_file_names. */
3128
3129 static void
3130 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3131 const gdb_byte *info_ptr,
3132 struct die_info *comp_unit_die)
3133 {
3134 struct dwarf2_cu *cu = reader->cu;
3135 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3136 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3137 struct dwarf2_per_cu_data *lh_cu;
3138 struct attribute *attr;
3139 void **slot;
3140 struct quick_file_names *qfn;
3141
3142 gdb_assert (! this_cu->is_debug_types);
3143
3144 /* Our callers never want to match partial units -- instead they
3145 will match the enclosing full CU. */
3146 if (comp_unit_die->tag == DW_TAG_partial_unit)
3147 {
3148 this_cu->v.quick->no_file_data = 1;
3149 return;
3150 }
3151
3152 lh_cu = this_cu;
3153 slot = NULL;
3154
3155 line_header_up lh;
3156 sect_offset line_offset {};
3157
3158 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3159 if (attr != nullptr)
3160 {
3161 struct quick_file_names find_entry;
3162
3163 line_offset = (sect_offset) DW_UNSND (attr);
3164
3165 /* We may have already read in this line header (TU line header sharing).
3166 If we have we're done. */
3167 find_entry.hash.dwo_unit = cu->dwo_unit;
3168 find_entry.hash.line_sect_off = line_offset;
3169 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3170 &find_entry, INSERT);
3171 if (*slot != NULL)
3172 {
3173 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3174 return;
3175 }
3176
3177 lh = dwarf_decode_line_header (line_offset, cu);
3178 }
3179 if (lh == NULL)
3180 {
3181 lh_cu->v.quick->no_file_data = 1;
3182 return;
3183 }
3184
3185 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
3186 qfn->hash.dwo_unit = cu->dwo_unit;
3187 qfn->hash.line_sect_off = line_offset;
3188 gdb_assert (slot != NULL);
3189 *slot = qfn;
3190
3191 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3192
3193 int offset = 0;
3194 if (strcmp (fnd.name, "<unknown>") != 0)
3195 ++offset;
3196
3197 qfn->num_file_names = offset + lh->file_names_size ();
3198 qfn->file_names =
3199 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
3200 qfn->num_file_names);
3201 if (offset != 0)
3202 qfn->file_names[0] = xstrdup (fnd.name);
3203 for (int i = 0; i < lh->file_names_size (); ++i)
3204 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3205 fnd.comp_dir).release ();
3206 qfn->real_names = NULL;
3207
3208 lh_cu->v.quick->file_names = qfn;
3209 }
3210
3211 /* A helper for the "quick" functions which attempts to read the line
3212 table for THIS_CU. */
3213
3214 static struct quick_file_names *
3215 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3216 {
3217 /* This should never be called for TUs. */
3218 gdb_assert (! this_cu->is_debug_types);
3219 /* Nor type unit groups. */
3220 gdb_assert (! this_cu->type_unit_group_p ());
3221
3222 if (this_cu->v.quick->file_names != NULL)
3223 return this_cu->v.quick->file_names;
3224 /* If we know there is no line data, no point in looking again. */
3225 if (this_cu->v.quick->no_file_data)
3226 return NULL;
3227
3228 cutu_reader reader (this_cu);
3229 if (!reader.dummy_p)
3230 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3231
3232 if (this_cu->v.quick->no_file_data)
3233 return NULL;
3234 return this_cu->v.quick->file_names;
3235 }
3236
3237 /* A helper for the "quick" functions which computes and caches the
3238 real path for a given file name from the line table. */
3239
3240 static const char *
3241 dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
3242 struct quick_file_names *qfn, int index)
3243 {
3244 if (qfn->real_names == NULL)
3245 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
3246 qfn->num_file_names, const char *);
3247
3248 if (qfn->real_names[index] == NULL)
3249 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3250
3251 return qfn->real_names[index];
3252 }
3253
3254 static struct symtab *
3255 dw2_find_last_source_symtab (struct objfile *objfile)
3256 {
3257 struct dwarf2_per_objfile *dwarf2_per_objfile
3258 = get_dwarf2_per_objfile (objfile);
3259 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
3260 compunit_symtab *cust
3261 = dw2_instantiate_symtab (dwarf_cu, dwarf2_per_objfile, false);
3262
3263 if (cust == NULL)
3264 return NULL;
3265
3266 return compunit_primary_filetab (cust);
3267 }
3268
3269 /* Traversal function for dw2_forget_cached_source_info. */
3270
3271 static int
3272 dw2_free_cached_file_names (void **slot, void *info)
3273 {
3274 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3275
3276 if (file_data->real_names)
3277 {
3278 int i;
3279
3280 for (i = 0; i < file_data->num_file_names; ++i)
3281 {
3282 xfree ((void*) file_data->real_names[i]);
3283 file_data->real_names[i] = NULL;
3284 }
3285 }
3286
3287 return 1;
3288 }
3289
3290 static void
3291 dw2_forget_cached_source_info (struct objfile *objfile)
3292 {
3293 struct dwarf2_per_objfile *dwarf2_per_objfile
3294 = get_dwarf2_per_objfile (objfile);
3295
3296 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3297 dw2_free_cached_file_names, NULL);
3298 }
3299
3300 /* Helper function for dw2_map_symtabs_matching_filename that expands
3301 the symtabs and calls the iterator. */
3302
3303 static int
3304 dw2_map_expand_apply (struct objfile *objfile,
3305 struct dwarf2_per_cu_data *per_cu,
3306 const char *name, const char *real_path,
3307 gdb::function_view<bool (symtab *)> callback)
3308 {
3309 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3310
3311 /* Don't visit already-expanded CUs. */
3312 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3313 if (per_objfile->symtab_set_p (per_cu))
3314 return 0;
3315
3316 /* This may expand more than one symtab, and we want to iterate over
3317 all of them. */
3318 dw2_instantiate_symtab (per_cu, per_objfile, false);
3319
3320 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3321 last_made, callback);
3322 }
3323
3324 /* Implementation of the map_symtabs_matching_filename method. */
3325
3326 static bool
3327 dw2_map_symtabs_matching_filename
3328 (struct objfile *objfile, const char *name, const char *real_path,
3329 gdb::function_view<bool (symtab *)> callback)
3330 {
3331 const char *name_basename = lbasename (name);
3332 struct dwarf2_per_objfile *dwarf2_per_objfile
3333 = get_dwarf2_per_objfile (objfile);
3334
3335 /* The rule is CUs specify all the files, including those used by
3336 any TU, so there's no need to scan TUs here. */
3337
3338 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3339 {
3340 /* We only need to look at symtabs not already expanded. */
3341 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3342 continue;
3343
3344 quick_file_names *file_data = dw2_get_file_names (per_cu);
3345 if (file_data == NULL)
3346 continue;
3347
3348 for (int j = 0; j < file_data->num_file_names; ++j)
3349 {
3350 const char *this_name = file_data->file_names[j];
3351 const char *this_real_name;
3352
3353 if (compare_filenames_for_search (this_name, name))
3354 {
3355 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3356 callback))
3357 return true;
3358 continue;
3359 }
3360
3361 /* Before we invoke realpath, which can get expensive when many
3362 files are involved, do a quick comparison of the basenames. */
3363 if (! basenames_may_differ
3364 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3365 continue;
3366
3367 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3368 file_data, j);
3369 if (compare_filenames_for_search (this_real_name, name))
3370 {
3371 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3372 callback))
3373 return true;
3374 continue;
3375 }
3376
3377 if (real_path != NULL)
3378 {
3379 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3380 gdb_assert (IS_ABSOLUTE_PATH (name));
3381 if (this_real_name != NULL
3382 && FILENAME_CMP (real_path, this_real_name) == 0)
3383 {
3384 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3385 callback))
3386 return true;
3387 continue;
3388 }
3389 }
3390 }
3391 }
3392
3393 return false;
3394 }
3395
3396 /* Struct used to manage iterating over all CUs looking for a symbol. */
3397
3398 struct dw2_symtab_iterator
3399 {
3400 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3401 struct dwarf2_per_objfile *dwarf2_per_objfile;
3402 /* If set, only look for symbols that match that block. Valid values are
3403 GLOBAL_BLOCK and STATIC_BLOCK. */
3404 gdb::optional<block_enum> block_index;
3405 /* The kind of symbol we're looking for. */
3406 domain_enum domain;
3407 /* The list of CUs from the index entry of the symbol,
3408 or NULL if not found. */
3409 offset_type *vec;
3410 /* The next element in VEC to look at. */
3411 int next;
3412 /* The number of elements in VEC, or zero if there is no match. */
3413 int length;
3414 /* Have we seen a global version of the symbol?
3415 If so we can ignore all further global instances.
3416 This is to work around gold/15646, inefficient gold-generated
3417 indices. */
3418 int global_seen;
3419 };
3420
3421 /* Initialize the index symtab iterator ITER. */
3422
3423 static void
3424 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3425 struct dwarf2_per_objfile *dwarf2_per_objfile,
3426 gdb::optional<block_enum> block_index,
3427 domain_enum domain,
3428 const char *name)
3429 {
3430 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3431 iter->block_index = block_index;
3432 iter->domain = domain;
3433 iter->next = 0;
3434 iter->global_seen = 0;
3435
3436 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
3437
3438 /* index is NULL if OBJF_READNOW. */
3439 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3440 iter->length = MAYBE_SWAP (*iter->vec);
3441 else
3442 {
3443 iter->vec = NULL;
3444 iter->length = 0;
3445 }
3446 }
3447
3448 /* Return the next matching CU or NULL if there are no more. */
3449
3450 static struct dwarf2_per_cu_data *
3451 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3452 {
3453 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3454
3455 for ( ; iter->next < iter->length; ++iter->next)
3456 {
3457 offset_type cu_index_and_attrs =
3458 MAYBE_SWAP (iter->vec[iter->next + 1]);
3459 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3460 gdb_index_symbol_kind symbol_kind =
3461 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3462 /* Only check the symbol attributes if they're present.
3463 Indices prior to version 7 don't record them,
3464 and indices >= 7 may elide them for certain symbols
3465 (gold does this). */
3466 int attrs_valid =
3467 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
3468 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3469
3470 /* Don't crash on bad data. */
3471 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3472 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3473 {
3474 complaint (_(".gdb_index entry has bad CU index"
3475 " [in module %s]"),
3476 objfile_name (dwarf2_per_objfile->objfile));
3477 continue;
3478 }
3479
3480 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3481
3482 /* Skip if already read in. */
3483 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3484 continue;
3485
3486 /* Check static vs global. */
3487 if (attrs_valid)
3488 {
3489 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3490
3491 if (iter->block_index.has_value ())
3492 {
3493 bool want_static = *iter->block_index == STATIC_BLOCK;
3494
3495 if (is_static != want_static)
3496 continue;
3497 }
3498
3499 /* Work around gold/15646. */
3500 if (!is_static && iter->global_seen)
3501 continue;
3502 if (!is_static)
3503 iter->global_seen = 1;
3504 }
3505
3506 /* Only check the symbol's kind if it has one. */
3507 if (attrs_valid)
3508 {
3509 switch (iter->domain)
3510 {
3511 case VAR_DOMAIN:
3512 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3513 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3514 /* Some types are also in VAR_DOMAIN. */
3515 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3516 continue;
3517 break;
3518 case STRUCT_DOMAIN:
3519 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3520 continue;
3521 break;
3522 case LABEL_DOMAIN:
3523 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3524 continue;
3525 break;
3526 case MODULE_DOMAIN:
3527 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3528 continue;
3529 break;
3530 default:
3531 break;
3532 }
3533 }
3534
3535 ++iter->next;
3536 return per_cu;
3537 }
3538
3539 return NULL;
3540 }
3541
3542 static struct compunit_symtab *
3543 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3544 const char *name, domain_enum domain)
3545 {
3546 struct compunit_symtab *stab_best = NULL;
3547 struct dwarf2_per_objfile *dwarf2_per_objfile
3548 = get_dwarf2_per_objfile (objfile);
3549
3550 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3551
3552 struct dw2_symtab_iterator iter;
3553 struct dwarf2_per_cu_data *per_cu;
3554
3555 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3556
3557 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3558 {
3559 struct symbol *sym, *with_opaque = NULL;
3560 struct compunit_symtab *stab
3561 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3562 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3563 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3564
3565 sym = block_find_symbol (block, name, domain,
3566 block_find_non_opaque_type_preferred,
3567 &with_opaque);
3568
3569 /* Some caution must be observed with overloaded functions
3570 and methods, since the index will not contain any overload
3571 information (but NAME might contain it). */
3572
3573 if (sym != NULL
3574 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3575 return stab;
3576 if (with_opaque != NULL
3577 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3578 stab_best = stab;
3579
3580 /* Keep looking through other CUs. */
3581 }
3582
3583 return stab_best;
3584 }
3585
3586 static void
3587 dw2_print_stats (struct objfile *objfile)
3588 {
3589 struct dwarf2_per_objfile *dwarf2_per_objfile
3590 = get_dwarf2_per_objfile (objfile);
3591 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3592 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3593 int count = 0;
3594
3595 for (int i = 0; i < total; ++i)
3596 {
3597 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3598
3599 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
3600 ++count;
3601 }
3602 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3603 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3604 }
3605
3606 /* This dumps minimal information about the index.
3607 It is called via "mt print objfiles".
3608 One use is to verify .gdb_index has been loaded by the
3609 gdb.dwarf2/gdb-index.exp testcase. */
3610
3611 static void
3612 dw2_dump (struct objfile *objfile)
3613 {
3614 struct dwarf2_per_objfile *dwarf2_per_objfile
3615 = get_dwarf2_per_objfile (objfile);
3616
3617 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
3618 printf_filtered (".gdb_index:");
3619 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
3620 {
3621 printf_filtered (" version %d\n",
3622 dwarf2_per_objfile->per_bfd->index_table->version);
3623 }
3624 else
3625 printf_filtered (" faked for \"readnow\"\n");
3626 printf_filtered ("\n");
3627 }
3628
3629 static void
3630 dw2_expand_symtabs_for_function (struct objfile *objfile,
3631 const char *func_name)
3632 {
3633 struct dwarf2_per_objfile *dwarf2_per_objfile
3634 = get_dwarf2_per_objfile (objfile);
3635
3636 struct dw2_symtab_iterator iter;
3637 struct dwarf2_per_cu_data *per_cu;
3638
3639 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3640
3641 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3642 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3643
3644 }
3645
3646 static void
3647 dw2_expand_all_symtabs (struct objfile *objfile)
3648 {
3649 struct dwarf2_per_objfile *dwarf2_per_objfile
3650 = get_dwarf2_per_objfile (objfile);
3651 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3652 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3653
3654 for (int i = 0; i < total_units; ++i)
3655 {
3656 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3657
3658 /* We don't want to directly expand a partial CU, because if we
3659 read it with the wrong language, then assertion failures can
3660 be triggered later on. See PR symtab/23010. So, tell
3661 dw2_instantiate_symtab to skip partial CUs -- any important
3662 partial CU will be read via DW_TAG_imported_unit anyway. */
3663 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, true);
3664 }
3665 }
3666
3667 static void
3668 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3669 const char *fullname)
3670 {
3671 struct dwarf2_per_objfile *dwarf2_per_objfile
3672 = get_dwarf2_per_objfile (objfile);
3673
3674 /* We don't need to consider type units here.
3675 This is only called for examining code, e.g. expand_line_sal.
3676 There can be an order of magnitude (or more) more type units
3677 than comp units, and we avoid them if we can. */
3678
3679 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3680 {
3681 /* We only need to look at symtabs not already expanded. */
3682 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3683 continue;
3684
3685 quick_file_names *file_data = dw2_get_file_names (per_cu);
3686 if (file_data == NULL)
3687 continue;
3688
3689 for (int j = 0; j < file_data->num_file_names; ++j)
3690 {
3691 const char *this_fullname = file_data->file_names[j];
3692
3693 if (filename_cmp (this_fullname, fullname) == 0)
3694 {
3695 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3696 break;
3697 }
3698 }
3699 }
3700 }
3701
3702 static void
3703 dw2_expand_symtabs_matching_symbol
3704 (mapped_index_base &index,
3705 const lookup_name_info &lookup_name_in,
3706 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3707 enum search_domain kind,
3708 gdb::function_view<bool (offset_type)> match_callback);
3709
3710 static void
3711 dw2_expand_symtabs_matching_one
3712 (dwarf2_per_cu_data *per_cu,
3713 dwarf2_per_objfile *per_objfile,
3714 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3715 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3716
3717 static void
3718 dw2_map_matching_symbols
3719 (struct objfile *objfile,
3720 const lookup_name_info &name, domain_enum domain,
3721 int global,
3722 gdb::function_view<symbol_found_callback_ftype> callback,
3723 symbol_compare_ftype *ordered_compare)
3724 {
3725 /* Used for Ada. */
3726 struct dwarf2_per_objfile *dwarf2_per_objfile
3727 = get_dwarf2_per_objfile (objfile);
3728
3729 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3730
3731 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
3732 {
3733 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3734 here though if the current language is Ada for a non-Ada objfile
3735 using GNU index. */
3736 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3737
3738 const char *match_name = name.ada ().lookup_name ().c_str ();
3739 auto matcher = [&] (const char *symname)
3740 {
3741 if (ordered_compare == nullptr)
3742 return true;
3743 return ordered_compare (symname, match_name) == 0;
3744 };
3745
3746 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3747 [&] (offset_type namei)
3748 {
3749 struct dw2_symtab_iterator iter;
3750 struct dwarf2_per_cu_data *per_cu;
3751
3752 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3753 match_name);
3754 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3755 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
3756 nullptr);
3757 return true;
3758 });
3759 }
3760 else
3761 {
3762 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3763 proceed assuming all symtabs have been read in. */
3764 }
3765
3766 for (compunit_symtab *cust : objfile->compunits ())
3767 {
3768 const struct block *block;
3769
3770 if (cust == NULL)
3771 continue;
3772 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3773 if (!iterate_over_symbols_terminated (block, name,
3774 domain, callback))
3775 return;
3776 }
3777 }
3778
3779 /* Starting from a search name, return the string that finds the upper
3780 bound of all strings that start with SEARCH_NAME in a sorted name
3781 list. Returns the empty string to indicate that the upper bound is
3782 the end of the list. */
3783
3784 static std::string
3785 make_sort_after_prefix_name (const char *search_name)
3786 {
3787 /* When looking to complete "func", we find the upper bound of all
3788 symbols that start with "func" by looking for where we'd insert
3789 the closest string that would follow "func" in lexicographical
3790 order. Usually, that's "func"-with-last-character-incremented,
3791 i.e. "fund". Mind non-ASCII characters, though. Usually those
3792 will be UTF-8 multi-byte sequences, but we can't be certain.
3793 Especially mind the 0xff character, which is a valid character in
3794 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3795 rule out compilers allowing it in identifiers. Note that
3796 conveniently, strcmp/strcasecmp are specified to compare
3797 characters interpreted as unsigned char. So what we do is treat
3798 the whole string as a base 256 number composed of a sequence of
3799 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3800 to 0, and carries 1 to the following more-significant position.
3801 If the very first character in SEARCH_NAME ends up incremented
3802 and carries/overflows, then the upper bound is the end of the
3803 list. The string after the empty string is also the empty
3804 string.
3805
3806 Some examples of this operation:
3807
3808 SEARCH_NAME => "+1" RESULT
3809
3810 "abc" => "abd"
3811 "ab\xff" => "ac"
3812 "\xff" "a" "\xff" => "\xff" "b"
3813 "\xff" => ""
3814 "\xff\xff" => ""
3815 "" => ""
3816
3817 Then, with these symbols for example:
3818
3819 func
3820 func1
3821 fund
3822
3823 completing "func" looks for symbols between "func" and
3824 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3825 which finds "func" and "func1", but not "fund".
3826
3827 And with:
3828
3829 funcÿ (Latin1 'ÿ' [0xff])
3830 funcÿ1
3831 fund
3832
3833 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3834 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3835
3836 And with:
3837
3838 ÿÿ (Latin1 'ÿ' [0xff])
3839 ÿÿ1
3840
3841 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3842 the end of the list.
3843 */
3844 std::string after = search_name;
3845 while (!after.empty () && (unsigned char) after.back () == 0xff)
3846 after.pop_back ();
3847 if (!after.empty ())
3848 after.back () = (unsigned char) after.back () + 1;
3849 return after;
3850 }
3851
3852 /* See declaration. */
3853
3854 std::pair<std::vector<name_component>::const_iterator,
3855 std::vector<name_component>::const_iterator>
3856 mapped_index_base::find_name_components_bounds
3857 (const lookup_name_info &lookup_name_without_params, language lang) const
3858 {
3859 auto *name_cmp
3860 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3861
3862 const char *lang_name
3863 = lookup_name_without_params.language_lookup_name (lang);
3864
3865 /* Comparison function object for lower_bound that matches against a
3866 given symbol name. */
3867 auto lookup_compare_lower = [&] (const name_component &elem,
3868 const char *name)
3869 {
3870 const char *elem_qualified = this->symbol_name_at (elem.idx);
3871 const char *elem_name = elem_qualified + elem.name_offset;
3872 return name_cmp (elem_name, name) < 0;
3873 };
3874
3875 /* Comparison function object for upper_bound that matches against a
3876 given symbol name. */
3877 auto lookup_compare_upper = [&] (const char *name,
3878 const name_component &elem)
3879 {
3880 const char *elem_qualified = this->symbol_name_at (elem.idx);
3881 const char *elem_name = elem_qualified + elem.name_offset;
3882 return name_cmp (name, elem_name) < 0;
3883 };
3884
3885 auto begin = this->name_components.begin ();
3886 auto end = this->name_components.end ();
3887
3888 /* Find the lower bound. */
3889 auto lower = [&] ()
3890 {
3891 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3892 return begin;
3893 else
3894 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3895 } ();
3896
3897 /* Find the upper bound. */
3898 auto upper = [&] ()
3899 {
3900 if (lookup_name_without_params.completion_mode ())
3901 {
3902 /* In completion mode, we want UPPER to point past all
3903 symbols names that have the same prefix. I.e., with
3904 these symbols, and completing "func":
3905
3906 function << lower bound
3907 function1
3908 other_function << upper bound
3909
3910 We find the upper bound by looking for the insertion
3911 point of "func"-with-last-character-incremented,
3912 i.e. "fund". */
3913 std::string after = make_sort_after_prefix_name (lang_name);
3914 if (after.empty ())
3915 return end;
3916 return std::lower_bound (lower, end, after.c_str (),
3917 lookup_compare_lower);
3918 }
3919 else
3920 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3921 } ();
3922
3923 return {lower, upper};
3924 }
3925
3926 /* See declaration. */
3927
3928 void
3929 mapped_index_base::build_name_components ()
3930 {
3931 if (!this->name_components.empty ())
3932 return;
3933
3934 this->name_components_casing = case_sensitivity;
3935 auto *name_cmp
3936 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3937
3938 /* The code below only knows how to break apart components of C++
3939 symbol names (and other languages that use '::' as
3940 namespace/module separator) and Ada symbol names. */
3941 auto count = this->symbol_name_count ();
3942 for (offset_type idx = 0; idx < count; idx++)
3943 {
3944 if (this->symbol_name_slot_invalid (idx))
3945 continue;
3946
3947 const char *name = this->symbol_name_at (idx);
3948
3949 /* Add each name component to the name component table. */
3950 unsigned int previous_len = 0;
3951
3952 if (strstr (name, "::") != nullptr)
3953 {
3954 for (unsigned int current_len = cp_find_first_component (name);
3955 name[current_len] != '\0';
3956 current_len += cp_find_first_component (name + current_len))
3957 {
3958 gdb_assert (name[current_len] == ':');
3959 this->name_components.push_back ({previous_len, idx});
3960 /* Skip the '::'. */
3961 current_len += 2;
3962 previous_len = current_len;
3963 }
3964 }
3965 else
3966 {
3967 /* Handle the Ada encoded (aka mangled) form here. */
3968 for (const char *iter = strstr (name, "__");
3969 iter != nullptr;
3970 iter = strstr (iter, "__"))
3971 {
3972 this->name_components.push_back ({previous_len, idx});
3973 iter += 2;
3974 previous_len = iter - name;
3975 }
3976 }
3977
3978 this->name_components.push_back ({previous_len, idx});
3979 }
3980
3981 /* Sort name_components elements by name. */
3982 auto name_comp_compare = [&] (const name_component &left,
3983 const name_component &right)
3984 {
3985 const char *left_qualified = this->symbol_name_at (left.idx);
3986 const char *right_qualified = this->symbol_name_at (right.idx);
3987
3988 const char *left_name = left_qualified + left.name_offset;
3989 const char *right_name = right_qualified + right.name_offset;
3990
3991 return name_cmp (left_name, right_name) < 0;
3992 };
3993
3994 std::sort (this->name_components.begin (),
3995 this->name_components.end (),
3996 name_comp_compare);
3997 }
3998
3999 /* Helper for dw2_expand_symtabs_matching that works with a
4000 mapped_index_base instead of the containing objfile. This is split
4001 to a separate function in order to be able to unit test the
4002 name_components matching using a mock mapped_index_base. For each
4003 symbol name that matches, calls MATCH_CALLBACK, passing it the
4004 symbol's index in the mapped_index_base symbol table. */
4005
4006 static void
4007 dw2_expand_symtabs_matching_symbol
4008 (mapped_index_base &index,
4009 const lookup_name_info &lookup_name_in,
4010 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4011 enum search_domain kind,
4012 gdb::function_view<bool (offset_type)> match_callback)
4013 {
4014 lookup_name_info lookup_name_without_params
4015 = lookup_name_in.make_ignore_params ();
4016
4017 /* Build the symbol name component sorted vector, if we haven't
4018 yet. */
4019 index.build_name_components ();
4020
4021 /* The same symbol may appear more than once in the range though.
4022 E.g., if we're looking for symbols that complete "w", and we have
4023 a symbol named "w1::w2", we'll find the two name components for
4024 that same symbol in the range. To be sure we only call the
4025 callback once per symbol, we first collect the symbol name
4026 indexes that matched in a temporary vector and ignore
4027 duplicates. */
4028 std::vector<offset_type> matches;
4029
4030 struct name_and_matcher
4031 {
4032 symbol_name_matcher_ftype *matcher;
4033 const char *name;
4034
4035 bool operator== (const name_and_matcher &other) const
4036 {
4037 return matcher == other.matcher && strcmp (name, other.name) == 0;
4038 }
4039 };
4040
4041 /* A vector holding all the different symbol name matchers, for all
4042 languages. */
4043 std::vector<name_and_matcher> matchers;
4044
4045 for (int i = 0; i < nr_languages; i++)
4046 {
4047 enum language lang_e = (enum language) i;
4048
4049 const language_defn *lang = language_def (lang_e);
4050 symbol_name_matcher_ftype *name_matcher
4051 = get_symbol_name_matcher (lang, lookup_name_without_params);
4052
4053 name_and_matcher key {
4054 name_matcher,
4055 lookup_name_without_params.language_lookup_name (lang_e)
4056 };
4057
4058 /* Don't insert the same comparison routine more than once.
4059 Note that we do this linear walk. This is not a problem in
4060 practice because the number of supported languages is
4061 low. */
4062 if (std::find (matchers.begin (), matchers.end (), key)
4063 != matchers.end ())
4064 continue;
4065 matchers.push_back (std::move (key));
4066
4067 auto bounds
4068 = index.find_name_components_bounds (lookup_name_without_params,
4069 lang_e);
4070
4071 /* Now for each symbol name in range, check to see if we have a name
4072 match, and if so, call the MATCH_CALLBACK callback. */
4073
4074 for (; bounds.first != bounds.second; ++bounds.first)
4075 {
4076 const char *qualified = index.symbol_name_at (bounds.first->idx);
4077
4078 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4079 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4080 continue;
4081
4082 matches.push_back (bounds.first->idx);
4083 }
4084 }
4085
4086 std::sort (matches.begin (), matches.end ());
4087
4088 /* Finally call the callback, once per match. */
4089 ULONGEST prev = -1;
4090 for (offset_type idx : matches)
4091 {
4092 if (prev != idx)
4093 {
4094 if (!match_callback (idx))
4095 break;
4096 prev = idx;
4097 }
4098 }
4099
4100 /* Above we use a type wider than idx's for 'prev', since 0 and
4101 (offset_type)-1 are both possible values. */
4102 static_assert (sizeof (prev) > sizeof (offset_type), "");
4103 }
4104
4105 #if GDB_SELF_TEST
4106
4107 namespace selftests { namespace dw2_expand_symtabs_matching {
4108
4109 /* A mock .gdb_index/.debug_names-like name index table, enough to
4110 exercise dw2_expand_symtabs_matching_symbol, which works with the
4111 mapped_index_base interface. Builds an index from the symbol list
4112 passed as parameter to the constructor. */
4113 class mock_mapped_index : public mapped_index_base
4114 {
4115 public:
4116 mock_mapped_index (gdb::array_view<const char *> symbols)
4117 : m_symbol_table (symbols)
4118 {}
4119
4120 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4121
4122 /* Return the number of names in the symbol table. */
4123 size_t symbol_name_count () const override
4124 {
4125 return m_symbol_table.size ();
4126 }
4127
4128 /* Get the name of the symbol at IDX in the symbol table. */
4129 const char *symbol_name_at (offset_type idx) const override
4130 {
4131 return m_symbol_table[idx];
4132 }
4133
4134 private:
4135 gdb::array_view<const char *> m_symbol_table;
4136 };
4137
4138 /* Convenience function that converts a NULL pointer to a "<null>"
4139 string, to pass to print routines. */
4140
4141 static const char *
4142 string_or_null (const char *str)
4143 {
4144 return str != NULL ? str : "<null>";
4145 }
4146
4147 /* Check if a lookup_name_info built from
4148 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4149 index. EXPECTED_LIST is the list of expected matches, in expected
4150 matching order. If no match expected, then an empty list is
4151 specified. Returns true on success. On failure prints a warning
4152 indicating the file:line that failed, and returns false. */
4153
4154 static bool
4155 check_match (const char *file, int line,
4156 mock_mapped_index &mock_index,
4157 const char *name, symbol_name_match_type match_type,
4158 bool completion_mode,
4159 std::initializer_list<const char *> expected_list)
4160 {
4161 lookup_name_info lookup_name (name, match_type, completion_mode);
4162
4163 bool matched = true;
4164
4165 auto mismatch = [&] (const char *expected_str,
4166 const char *got)
4167 {
4168 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4169 "expected=\"%s\", got=\"%s\"\n"),
4170 file, line,
4171 (match_type == symbol_name_match_type::FULL
4172 ? "FULL" : "WILD"),
4173 name, string_or_null (expected_str), string_or_null (got));
4174 matched = false;
4175 };
4176
4177 auto expected_it = expected_list.begin ();
4178 auto expected_end = expected_list.end ();
4179
4180 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4181 NULL, ALL_DOMAIN,
4182 [&] (offset_type idx)
4183 {
4184 const char *matched_name = mock_index.symbol_name_at (idx);
4185 const char *expected_str
4186 = expected_it == expected_end ? NULL : *expected_it++;
4187
4188 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4189 mismatch (expected_str, matched_name);
4190 return true;
4191 });
4192
4193 const char *expected_str
4194 = expected_it == expected_end ? NULL : *expected_it++;
4195 if (expected_str != NULL)
4196 mismatch (expected_str, NULL);
4197
4198 return matched;
4199 }
4200
4201 /* The symbols added to the mock mapped_index for testing (in
4202 canonical form). */
4203 static const char *test_symbols[] = {
4204 "function",
4205 "std::bar",
4206 "std::zfunction",
4207 "std::zfunction2",
4208 "w1::w2",
4209 "ns::foo<char*>",
4210 "ns::foo<int>",
4211 "ns::foo<long>",
4212 "ns2::tmpl<int>::foo2",
4213 "(anonymous namespace)::A::B::C",
4214
4215 /* These are used to check that the increment-last-char in the
4216 matching algorithm for completion doesn't match "t1_fund" when
4217 completing "t1_func". */
4218 "t1_func",
4219 "t1_func1",
4220 "t1_fund",
4221 "t1_fund1",
4222
4223 /* A UTF-8 name with multi-byte sequences to make sure that
4224 cp-name-parser understands this as a single identifier ("função"
4225 is "function" in PT). */
4226 u8"u8função",
4227
4228 /* \377 (0xff) is Latin1 'ÿ'. */
4229 "yfunc\377",
4230
4231 /* \377 (0xff) is Latin1 'ÿ'. */
4232 "\377",
4233 "\377\377123",
4234
4235 /* A name with all sorts of complications. Starts with "z" to make
4236 it easier for the completion tests below. */
4237 #define Z_SYM_NAME \
4238 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4239 "::tuple<(anonymous namespace)::ui*, " \
4240 "std::default_delete<(anonymous namespace)::ui>, void>"
4241
4242 Z_SYM_NAME
4243 };
4244
4245 /* Returns true if the mapped_index_base::find_name_component_bounds
4246 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4247 in completion mode. */
4248
4249 static bool
4250 check_find_bounds_finds (mapped_index_base &index,
4251 const char *search_name,
4252 gdb::array_view<const char *> expected_syms)
4253 {
4254 lookup_name_info lookup_name (search_name,
4255 symbol_name_match_type::FULL, true);
4256
4257 auto bounds = index.find_name_components_bounds (lookup_name,
4258 language_cplus);
4259
4260 size_t distance = std::distance (bounds.first, bounds.second);
4261 if (distance != expected_syms.size ())
4262 return false;
4263
4264 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4265 {
4266 auto nc_elem = bounds.first + exp_elem;
4267 const char *qualified = index.symbol_name_at (nc_elem->idx);
4268 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4269 return false;
4270 }
4271
4272 return true;
4273 }
4274
4275 /* Test the lower-level mapped_index::find_name_component_bounds
4276 method. */
4277
4278 static void
4279 test_mapped_index_find_name_component_bounds ()
4280 {
4281 mock_mapped_index mock_index (test_symbols);
4282
4283 mock_index.build_name_components ();
4284
4285 /* Test the lower-level mapped_index::find_name_component_bounds
4286 method in completion mode. */
4287 {
4288 static const char *expected_syms[] = {
4289 "t1_func",
4290 "t1_func1",
4291 };
4292
4293 SELF_CHECK (check_find_bounds_finds (mock_index,
4294 "t1_func", expected_syms));
4295 }
4296
4297 /* Check that the increment-last-char in the name matching algorithm
4298 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4299 {
4300 static const char *expected_syms1[] = {
4301 "\377",
4302 "\377\377123",
4303 };
4304 SELF_CHECK (check_find_bounds_finds (mock_index,
4305 "\377", expected_syms1));
4306
4307 static const char *expected_syms2[] = {
4308 "\377\377123",
4309 };
4310 SELF_CHECK (check_find_bounds_finds (mock_index,
4311 "\377\377", expected_syms2));
4312 }
4313 }
4314
4315 /* Test dw2_expand_symtabs_matching_symbol. */
4316
4317 static void
4318 test_dw2_expand_symtabs_matching_symbol ()
4319 {
4320 mock_mapped_index mock_index (test_symbols);
4321
4322 /* We let all tests run until the end even if some fails, for debug
4323 convenience. */
4324 bool any_mismatch = false;
4325
4326 /* Create the expected symbols list (an initializer_list). Needed
4327 because lists have commas, and we need to pass them to CHECK,
4328 which is a macro. */
4329 #define EXPECT(...) { __VA_ARGS__ }
4330
4331 /* Wrapper for check_match that passes down the current
4332 __FILE__/__LINE__. */
4333 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4334 any_mismatch |= !check_match (__FILE__, __LINE__, \
4335 mock_index, \
4336 NAME, MATCH_TYPE, COMPLETION_MODE, \
4337 EXPECTED_LIST)
4338
4339 /* Identity checks. */
4340 for (const char *sym : test_symbols)
4341 {
4342 /* Should be able to match all existing symbols. */
4343 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4344 EXPECT (sym));
4345
4346 /* Should be able to match all existing symbols with
4347 parameters. */
4348 std::string with_params = std::string (sym) + "(int)";
4349 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4350 EXPECT (sym));
4351
4352 /* Should be able to match all existing symbols with
4353 parameters and qualifiers. */
4354 with_params = std::string (sym) + " ( int ) const";
4355 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4356 EXPECT (sym));
4357
4358 /* This should really find sym, but cp-name-parser.y doesn't
4359 know about lvalue/rvalue qualifiers yet. */
4360 with_params = std::string (sym) + " ( int ) &&";
4361 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4362 {});
4363 }
4364
4365 /* Check that the name matching algorithm for completion doesn't get
4366 confused with Latin1 'ÿ' / 0xff. */
4367 {
4368 static const char str[] = "\377";
4369 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4370 EXPECT ("\377", "\377\377123"));
4371 }
4372
4373 /* Check that the increment-last-char in the matching algorithm for
4374 completion doesn't match "t1_fund" when completing "t1_func". */
4375 {
4376 static const char str[] = "t1_func";
4377 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4378 EXPECT ("t1_func", "t1_func1"));
4379 }
4380
4381 /* Check that completion mode works at each prefix of the expected
4382 symbol name. */
4383 {
4384 static const char str[] = "function(int)";
4385 size_t len = strlen (str);
4386 std::string lookup;
4387
4388 for (size_t i = 1; i < len; i++)
4389 {
4390 lookup.assign (str, i);
4391 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4392 EXPECT ("function"));
4393 }
4394 }
4395
4396 /* While "w" is a prefix of both components, the match function
4397 should still only be called once. */
4398 {
4399 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4400 EXPECT ("w1::w2"));
4401 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4402 EXPECT ("w1::w2"));
4403 }
4404
4405 /* Same, with a "complicated" symbol. */
4406 {
4407 static const char str[] = Z_SYM_NAME;
4408 size_t len = strlen (str);
4409 std::string lookup;
4410
4411 for (size_t i = 1; i < len; i++)
4412 {
4413 lookup.assign (str, i);
4414 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4415 EXPECT (Z_SYM_NAME));
4416 }
4417 }
4418
4419 /* In FULL mode, an incomplete symbol doesn't match. */
4420 {
4421 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4422 {});
4423 }
4424
4425 /* A complete symbol with parameters matches any overload, since the
4426 index has no overload info. */
4427 {
4428 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4429 EXPECT ("std::zfunction", "std::zfunction2"));
4430 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4431 EXPECT ("std::zfunction", "std::zfunction2"));
4432 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4433 EXPECT ("std::zfunction", "std::zfunction2"));
4434 }
4435
4436 /* Check that whitespace is ignored appropriately. A symbol with a
4437 template argument list. */
4438 {
4439 static const char expected[] = "ns::foo<int>";
4440 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4441 EXPECT (expected));
4442 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4443 EXPECT (expected));
4444 }
4445
4446 /* Check that whitespace is ignored appropriately. A symbol with a
4447 template argument list that includes a pointer. */
4448 {
4449 static const char expected[] = "ns::foo<char*>";
4450 /* Try both completion and non-completion modes. */
4451 static const bool completion_mode[2] = {false, true};
4452 for (size_t i = 0; i < 2; i++)
4453 {
4454 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4455 completion_mode[i], EXPECT (expected));
4456 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4457 completion_mode[i], EXPECT (expected));
4458
4459 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4460 completion_mode[i], EXPECT (expected));
4461 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4462 completion_mode[i], EXPECT (expected));
4463 }
4464 }
4465
4466 {
4467 /* Check method qualifiers are ignored. */
4468 static const char expected[] = "ns::foo<char*>";
4469 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4470 symbol_name_match_type::FULL, true, EXPECT (expected));
4471 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4472 symbol_name_match_type::FULL, true, EXPECT (expected));
4473 CHECK_MATCH ("foo < char * > ( int ) const",
4474 symbol_name_match_type::WILD, true, EXPECT (expected));
4475 CHECK_MATCH ("foo < char * > ( int ) &&",
4476 symbol_name_match_type::WILD, true, EXPECT (expected));
4477 }
4478
4479 /* Test lookup names that don't match anything. */
4480 {
4481 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4482 {});
4483
4484 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4485 {});
4486 }
4487
4488 /* Some wild matching tests, exercising "(anonymous namespace)",
4489 which should not be confused with a parameter list. */
4490 {
4491 static const char *syms[] = {
4492 "A::B::C",
4493 "B::C",
4494 "C",
4495 "A :: B :: C ( int )",
4496 "B :: C ( int )",
4497 "C ( int )",
4498 };
4499
4500 for (const char *s : syms)
4501 {
4502 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4503 EXPECT ("(anonymous namespace)::A::B::C"));
4504 }
4505 }
4506
4507 {
4508 static const char expected[] = "ns2::tmpl<int>::foo2";
4509 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4510 EXPECT (expected));
4511 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4512 EXPECT (expected));
4513 }
4514
4515 SELF_CHECK (!any_mismatch);
4516
4517 #undef EXPECT
4518 #undef CHECK_MATCH
4519 }
4520
4521 static void
4522 run_test ()
4523 {
4524 test_mapped_index_find_name_component_bounds ();
4525 test_dw2_expand_symtabs_matching_symbol ();
4526 }
4527
4528 }} // namespace selftests::dw2_expand_symtabs_matching
4529
4530 #endif /* GDB_SELF_TEST */
4531
4532 /* If FILE_MATCHER is NULL or if PER_CU has
4533 dwarf2_per_cu_quick_data::MARK set (see
4534 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4535 EXPANSION_NOTIFY on it. */
4536
4537 static void
4538 dw2_expand_symtabs_matching_one
4539 (dwarf2_per_cu_data *per_cu,
4540 dwarf2_per_objfile *per_objfile,
4541 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4542 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4543 {
4544 if (file_matcher == NULL || per_cu->v.quick->mark)
4545 {
4546 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4547
4548 compunit_symtab *symtab
4549 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4550 gdb_assert (symtab != nullptr);
4551
4552 if (expansion_notify != NULL && symtab_was_null)
4553 expansion_notify (symtab);
4554 }
4555 }
4556
4557 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4558 matched, to expand corresponding CUs that were marked. IDX is the
4559 index of the symbol name that matched. */
4560
4561 static void
4562 dw2_expand_marked_cus
4563 (dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4564 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4565 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4566 search_domain kind)
4567 {
4568 offset_type *vec, vec_len, vec_idx;
4569 bool global_seen = false;
4570 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4571
4572 vec = (offset_type *) (index.constant_pool
4573 + MAYBE_SWAP (index.symbol_table[idx].vec));
4574 vec_len = MAYBE_SWAP (vec[0]);
4575 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4576 {
4577 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4578 /* This value is only valid for index versions >= 7. */
4579 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4580 gdb_index_symbol_kind symbol_kind =
4581 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4582 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4583 /* Only check the symbol attributes if they're present.
4584 Indices prior to version 7 don't record them,
4585 and indices >= 7 may elide them for certain symbols
4586 (gold does this). */
4587 int attrs_valid =
4588 (index.version >= 7
4589 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4590
4591 /* Work around gold/15646. */
4592 if (attrs_valid)
4593 {
4594 if (!is_static && global_seen)
4595 continue;
4596 if (!is_static)
4597 global_seen = true;
4598 }
4599
4600 /* Only check the symbol's kind if it has one. */
4601 if (attrs_valid)
4602 {
4603 switch (kind)
4604 {
4605 case VARIABLES_DOMAIN:
4606 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4607 continue;
4608 break;
4609 case FUNCTIONS_DOMAIN:
4610 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4611 continue;
4612 break;
4613 case TYPES_DOMAIN:
4614 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4615 continue;
4616 break;
4617 case MODULES_DOMAIN:
4618 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4619 continue;
4620 break;
4621 default:
4622 break;
4623 }
4624 }
4625
4626 /* Don't crash on bad data. */
4627 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4628 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
4629 {
4630 complaint (_(".gdb_index entry has bad CU index"
4631 " [in module %s]"),
4632 objfile_name (dwarf2_per_objfile->objfile));
4633 continue;
4634 }
4635
4636 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
4637 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, file_matcher,
4638 expansion_notify);
4639 }
4640 }
4641
4642 /* If FILE_MATCHER is non-NULL, set all the
4643 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4644 that match FILE_MATCHER. */
4645
4646 static void
4647 dw_expand_symtabs_matching_file_matcher
4648 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4649 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4650 {
4651 if (file_matcher == NULL)
4652 return;
4653
4654 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4655 htab_eq_pointer,
4656 NULL, xcalloc, xfree));
4657 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4658 htab_eq_pointer,
4659 NULL, xcalloc, xfree));
4660
4661 /* The rule is CUs specify all the files, including those used by
4662 any TU, so there's no need to scan TUs here. */
4663
4664 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4665 {
4666 QUIT;
4667
4668 per_cu->v.quick->mark = 0;
4669
4670 /* We only need to look at symtabs not already expanded. */
4671 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4672 continue;
4673
4674 quick_file_names *file_data = dw2_get_file_names (per_cu);
4675 if (file_data == NULL)
4676 continue;
4677
4678 if (htab_find (visited_not_found.get (), file_data) != NULL)
4679 continue;
4680 else if (htab_find (visited_found.get (), file_data) != NULL)
4681 {
4682 per_cu->v.quick->mark = 1;
4683 continue;
4684 }
4685
4686 for (int j = 0; j < file_data->num_file_names; ++j)
4687 {
4688 const char *this_real_name;
4689
4690 if (file_matcher (file_data->file_names[j], false))
4691 {
4692 per_cu->v.quick->mark = 1;
4693 break;
4694 }
4695
4696 /* Before we invoke realpath, which can get expensive when many
4697 files are involved, do a quick comparison of the basenames. */
4698 if (!basenames_may_differ
4699 && !file_matcher (lbasename (file_data->file_names[j]),
4700 true))
4701 continue;
4702
4703 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4704 file_data, j);
4705 if (file_matcher (this_real_name, false))
4706 {
4707 per_cu->v.quick->mark = 1;
4708 break;
4709 }
4710 }
4711
4712 void **slot = htab_find_slot (per_cu->v.quick->mark
4713 ? visited_found.get ()
4714 : visited_not_found.get (),
4715 file_data, INSERT);
4716 *slot = file_data;
4717 }
4718 }
4719
4720 static void
4721 dw2_expand_symtabs_matching
4722 (struct objfile *objfile,
4723 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4724 const lookup_name_info *lookup_name,
4725 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4726 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4727 enum search_domain kind)
4728 {
4729 struct dwarf2_per_objfile *dwarf2_per_objfile
4730 = get_dwarf2_per_objfile (objfile);
4731
4732 /* index_table is NULL if OBJF_READNOW. */
4733 if (!dwarf2_per_objfile->per_bfd->index_table)
4734 return;
4735
4736 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4737
4738 if (symbol_matcher == NULL && lookup_name == NULL)
4739 {
4740 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4741 {
4742 QUIT;
4743
4744 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
4745 file_matcher, expansion_notify);
4746 }
4747 return;
4748 }
4749
4750 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4751
4752 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4753 symbol_matcher,
4754 kind, [&] (offset_type idx)
4755 {
4756 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4757 expansion_notify, kind);
4758 return true;
4759 });
4760 }
4761
4762 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4763 symtab. */
4764
4765 static struct compunit_symtab *
4766 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4767 CORE_ADDR pc)
4768 {
4769 int i;
4770
4771 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4772 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4773 return cust;
4774
4775 if (cust->includes == NULL)
4776 return NULL;
4777
4778 for (i = 0; cust->includes[i]; ++i)
4779 {
4780 struct compunit_symtab *s = cust->includes[i];
4781
4782 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4783 if (s != NULL)
4784 return s;
4785 }
4786
4787 return NULL;
4788 }
4789
4790 static struct compunit_symtab *
4791 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4792 struct bound_minimal_symbol msymbol,
4793 CORE_ADDR pc,
4794 struct obj_section *section,
4795 int warn_if_readin)
4796 {
4797 struct dwarf2_per_cu_data *data;
4798 struct compunit_symtab *result;
4799
4800 if (!objfile->partial_symtabs->psymtabs_addrmap)
4801 return NULL;
4802
4803 CORE_ADDR baseaddr = objfile->text_section_offset ();
4804 data = (struct dwarf2_per_cu_data *) addrmap_find
4805 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4806 if (!data)
4807 return NULL;
4808
4809 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4810 if (warn_if_readin && per_objfile->symtab_set_p (data))
4811 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4812 paddress (objfile->arch (), pc));
4813
4814 result = recursively_find_pc_sect_compunit_symtab
4815 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4816
4817 gdb_assert (result != NULL);
4818 return result;
4819 }
4820
4821 static void
4822 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4823 void *data, int need_fullname)
4824 {
4825 struct dwarf2_per_objfile *dwarf2_per_objfile
4826 = get_dwarf2_per_objfile (objfile);
4827
4828 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
4829 {
4830 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
4831
4832 htab_up visited (htab_create_alloc (10,
4833 htab_hash_pointer, htab_eq_pointer,
4834 NULL, xcalloc, xfree));
4835
4836 /* The rule is CUs specify all the files, including those used
4837 by any TU, so there's no need to scan TUs here. We can
4838 ignore file names coming from already-expanded CUs. */
4839
4840 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4841 {
4842 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4843 {
4844 void **slot = htab_find_slot (visited.get (),
4845 per_cu->v.quick->file_names,
4846 INSERT);
4847
4848 *slot = per_cu->v.quick->file_names;
4849 }
4850 }
4851
4852 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4853 {
4854 /* We only need to look at symtabs not already expanded. */
4855 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4856 continue;
4857
4858 quick_file_names *file_data = dw2_get_file_names (per_cu);
4859 if (file_data == NULL)
4860 continue;
4861
4862 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4863 if (*slot)
4864 {
4865 /* Already visited. */
4866 continue;
4867 }
4868 *slot = file_data;
4869
4870 for (int j = 0; j < file_data->num_file_names; ++j)
4871 {
4872 const char *filename = file_data->file_names[j];
4873 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
4874 }
4875 }
4876 }
4877
4878 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4879 {
4880 gdb::unique_xmalloc_ptr<char> this_real_name;
4881
4882 if (need_fullname)
4883 this_real_name = gdb_realpath (filename);
4884 (*fun) (filename, this_real_name.get (), data);
4885 });
4886 }
4887
4888 static int
4889 dw2_has_symbols (struct objfile *objfile)
4890 {
4891 return 1;
4892 }
4893
4894 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4895 {
4896 dw2_has_symbols,
4897 dw2_find_last_source_symtab,
4898 dw2_forget_cached_source_info,
4899 dw2_map_symtabs_matching_filename,
4900 dw2_lookup_symbol,
4901 NULL,
4902 dw2_print_stats,
4903 dw2_dump,
4904 dw2_expand_symtabs_for_function,
4905 dw2_expand_all_symtabs,
4906 dw2_expand_symtabs_with_fullname,
4907 dw2_map_matching_symbols,
4908 dw2_expand_symtabs_matching,
4909 dw2_find_pc_sect_compunit_symtab,
4910 NULL,
4911 dw2_map_symbol_filenames
4912 };
4913
4914 /* DWARF-5 debug_names reader. */
4915
4916 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4917 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4918
4919 /* A helper function that reads the .debug_names section in SECTION
4920 and fills in MAP. FILENAME is the name of the file containing the
4921 section; it is used for error reporting.
4922
4923 Returns true if all went well, false otherwise. */
4924
4925 static bool
4926 read_debug_names_from_section (struct objfile *objfile,
4927 const char *filename,
4928 struct dwarf2_section_info *section,
4929 mapped_debug_names &map)
4930 {
4931 if (section->empty ())
4932 return false;
4933
4934 /* Older elfutils strip versions could keep the section in the main
4935 executable while splitting it for the separate debug info file. */
4936 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4937 return false;
4938
4939 section->read (objfile);
4940
4941 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4942
4943 const gdb_byte *addr = section->buffer;
4944
4945 bfd *const abfd = section->get_bfd_owner ();
4946
4947 unsigned int bytes_read;
4948 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4949 addr += bytes_read;
4950
4951 map.dwarf5_is_dwarf64 = bytes_read != 4;
4952 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4953 if (bytes_read + length != section->size)
4954 {
4955 /* There may be multiple per-CU indices. */
4956 warning (_("Section .debug_names in %s length %s does not match "
4957 "section length %s, ignoring .debug_names."),
4958 filename, plongest (bytes_read + length),
4959 pulongest (section->size));
4960 return false;
4961 }
4962
4963 /* The version number. */
4964 uint16_t version = read_2_bytes (abfd, addr);
4965 addr += 2;
4966 if (version != 5)
4967 {
4968 warning (_("Section .debug_names in %s has unsupported version %d, "
4969 "ignoring .debug_names."),
4970 filename, version);
4971 return false;
4972 }
4973
4974 /* Padding. */
4975 uint16_t padding = read_2_bytes (abfd, addr);
4976 addr += 2;
4977 if (padding != 0)
4978 {
4979 warning (_("Section .debug_names in %s has unsupported padding %d, "
4980 "ignoring .debug_names."),
4981 filename, padding);
4982 return false;
4983 }
4984
4985 /* comp_unit_count - The number of CUs in the CU list. */
4986 map.cu_count = read_4_bytes (abfd, addr);
4987 addr += 4;
4988
4989 /* local_type_unit_count - The number of TUs in the local TU
4990 list. */
4991 map.tu_count = read_4_bytes (abfd, addr);
4992 addr += 4;
4993
4994 /* foreign_type_unit_count - The number of TUs in the foreign TU
4995 list. */
4996 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4997 addr += 4;
4998 if (foreign_tu_count != 0)
4999 {
5000 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5001 "ignoring .debug_names."),
5002 filename, static_cast<unsigned long> (foreign_tu_count));
5003 return false;
5004 }
5005
5006 /* bucket_count - The number of hash buckets in the hash lookup
5007 table. */
5008 map.bucket_count = read_4_bytes (abfd, addr);
5009 addr += 4;
5010
5011 /* name_count - The number of unique names in the index. */
5012 map.name_count = read_4_bytes (abfd, addr);
5013 addr += 4;
5014
5015 /* abbrev_table_size - The size in bytes of the abbreviations
5016 table. */
5017 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5018 addr += 4;
5019
5020 /* augmentation_string_size - The size in bytes of the augmentation
5021 string. This value is rounded up to a multiple of 4. */
5022 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5023 addr += 4;
5024 map.augmentation_is_gdb = ((augmentation_string_size
5025 == sizeof (dwarf5_augmentation))
5026 && memcmp (addr, dwarf5_augmentation,
5027 sizeof (dwarf5_augmentation)) == 0);
5028 augmentation_string_size += (-augmentation_string_size) & 3;
5029 addr += augmentation_string_size;
5030
5031 /* List of CUs */
5032 map.cu_table_reordered = addr;
5033 addr += map.cu_count * map.offset_size;
5034
5035 /* List of Local TUs */
5036 map.tu_table_reordered = addr;
5037 addr += map.tu_count * map.offset_size;
5038
5039 /* Hash Lookup Table */
5040 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5041 addr += map.bucket_count * 4;
5042 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5043 addr += map.name_count * 4;
5044
5045 /* Name Table */
5046 map.name_table_string_offs_reordered = addr;
5047 addr += map.name_count * map.offset_size;
5048 map.name_table_entry_offs_reordered = addr;
5049 addr += map.name_count * map.offset_size;
5050
5051 const gdb_byte *abbrev_table_start = addr;
5052 for (;;)
5053 {
5054 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5055 addr += bytes_read;
5056 if (index_num == 0)
5057 break;
5058
5059 const auto insertpair
5060 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5061 if (!insertpair.second)
5062 {
5063 warning (_("Section .debug_names in %s has duplicate index %s, "
5064 "ignoring .debug_names."),
5065 filename, pulongest (index_num));
5066 return false;
5067 }
5068 mapped_debug_names::index_val &indexval = insertpair.first->second;
5069 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5070 addr += bytes_read;
5071
5072 for (;;)
5073 {
5074 mapped_debug_names::index_val::attr attr;
5075 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5076 addr += bytes_read;
5077 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5078 addr += bytes_read;
5079 if (attr.form == DW_FORM_implicit_const)
5080 {
5081 attr.implicit_const = read_signed_leb128 (abfd, addr,
5082 &bytes_read);
5083 addr += bytes_read;
5084 }
5085 if (attr.dw_idx == 0 && attr.form == 0)
5086 break;
5087 indexval.attr_vec.push_back (std::move (attr));
5088 }
5089 }
5090 if (addr != abbrev_table_start + abbrev_table_size)
5091 {
5092 warning (_("Section .debug_names in %s has abbreviation_table "
5093 "of size %s vs. written as %u, ignoring .debug_names."),
5094 filename, plongest (addr - abbrev_table_start),
5095 abbrev_table_size);
5096 return false;
5097 }
5098 map.entry_pool = addr;
5099
5100 return true;
5101 }
5102
5103 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5104 list. */
5105
5106 static void
5107 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5108 const mapped_debug_names &map,
5109 dwarf2_section_info &section,
5110 bool is_dwz)
5111 {
5112 if (!map.augmentation_is_gdb)
5113 {
5114 for (uint32_t i = 0; i < map.cu_count; ++i)
5115 {
5116 sect_offset sect_off
5117 = (sect_offset) (extract_unsigned_integer
5118 (map.cu_table_reordered + i * map.offset_size,
5119 map.offset_size,
5120 map.dwarf5_byte_order));
5121 /* We don't know the length of the CU, because the CU list in a
5122 .debug_names index can be incomplete, so we can't use the start of
5123 the next CU as end of this CU. We create the CUs here with length 0,
5124 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5125 dwarf2_per_cu_data *per_cu
5126 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5127 sect_off, 0);
5128 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
5129 }
5130 }
5131
5132 sect_offset sect_off_prev;
5133 for (uint32_t i = 0; i <= map.cu_count; ++i)
5134 {
5135 sect_offset sect_off_next;
5136 if (i < map.cu_count)
5137 {
5138 sect_off_next
5139 = (sect_offset) (extract_unsigned_integer
5140 (map.cu_table_reordered + i * map.offset_size,
5141 map.offset_size,
5142 map.dwarf5_byte_order));
5143 }
5144 else
5145 sect_off_next = (sect_offset) section.size;
5146 if (i >= 1)
5147 {
5148 const ULONGEST length = sect_off_next - sect_off_prev;
5149 dwarf2_per_cu_data *per_cu
5150 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5151 sect_off_prev, length);
5152 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
5153 }
5154 sect_off_prev = sect_off_next;
5155 }
5156 }
5157
5158 /* Read the CU list from the mapped index, and use it to create all
5159 the CU objects for this dwarf2_per_objfile. */
5160
5161 static void
5162 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5163 const mapped_debug_names &map,
5164 const mapped_debug_names &dwz_map)
5165 {
5166 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
5167 dwarf2_per_objfile->per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5168
5169 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5170 dwarf2_per_objfile->per_bfd->info,
5171 false /* is_dwz */);
5172
5173 if (dwz_map.cu_count == 0)
5174 return;
5175
5176 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
5177 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5178 true /* is_dwz */);
5179 }
5180
5181 /* Read .debug_names. If everything went ok, initialize the "quick"
5182 elements of all the CUs and return true. Otherwise, return false. */
5183
5184 static bool
5185 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5186 {
5187 std::unique_ptr<mapped_debug_names> map
5188 (new mapped_debug_names (dwarf2_per_objfile));
5189 mapped_debug_names dwz_map (dwarf2_per_objfile);
5190 struct objfile *objfile = dwarf2_per_objfile->objfile;
5191
5192 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5193 &dwarf2_per_objfile->per_bfd->debug_names,
5194 *map))
5195 return false;
5196
5197 /* Don't use the index if it's empty. */
5198 if (map->name_count == 0)
5199 return false;
5200
5201 /* If there is a .dwz file, read it so we can get its CU list as
5202 well. */
5203 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
5204 if (dwz != NULL)
5205 {
5206 if (!read_debug_names_from_section (objfile,
5207 bfd_get_filename (dwz->dwz_bfd.get ()),
5208 &dwz->debug_names, dwz_map))
5209 {
5210 warning (_("could not read '.debug_names' section from %s; skipping"),
5211 bfd_get_filename (dwz->dwz_bfd.get ()));
5212 return false;
5213 }
5214 }
5215
5216 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5217
5218 if (map->tu_count != 0)
5219 {
5220 /* We can only handle a single .debug_types when we have an
5221 index. */
5222 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
5223 return false;
5224
5225 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
5226
5227 create_signatured_type_table_from_debug_names
5228 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
5229 }
5230
5231 create_addrmap_from_aranges (dwarf2_per_objfile,
5232 &dwarf2_per_objfile->per_bfd->debug_aranges);
5233
5234 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5235 dwarf2_per_objfile->per_bfd->using_index = 1;
5236 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5237 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5238
5239 return true;
5240 }
5241
5242 /* Type used to manage iterating over all CUs looking for a symbol for
5243 .debug_names. */
5244
5245 class dw2_debug_names_iterator
5246 {
5247 public:
5248 dw2_debug_names_iterator (const mapped_debug_names &map,
5249 gdb::optional<block_enum> block_index,
5250 domain_enum domain,
5251 const char *name)
5252 : m_map (map), m_block_index (block_index), m_domain (domain),
5253 m_addr (find_vec_in_debug_names (map, name))
5254 {}
5255
5256 dw2_debug_names_iterator (const mapped_debug_names &map,
5257 search_domain search, uint32_t namei)
5258 : m_map (map),
5259 m_search (search),
5260 m_addr (find_vec_in_debug_names (map, namei))
5261 {}
5262
5263 dw2_debug_names_iterator (const mapped_debug_names &map,
5264 block_enum block_index, domain_enum domain,
5265 uint32_t namei)
5266 : m_map (map), m_block_index (block_index), m_domain (domain),
5267 m_addr (find_vec_in_debug_names (map, namei))
5268 {}
5269
5270 /* Return the next matching CU or NULL if there are no more. */
5271 dwarf2_per_cu_data *next ();
5272
5273 private:
5274 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5275 const char *name);
5276 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5277 uint32_t namei);
5278
5279 /* The internalized form of .debug_names. */
5280 const mapped_debug_names &m_map;
5281
5282 /* If set, only look for symbols that match that block. Valid values are
5283 GLOBAL_BLOCK and STATIC_BLOCK. */
5284 const gdb::optional<block_enum> m_block_index;
5285
5286 /* The kind of symbol we're looking for. */
5287 const domain_enum m_domain = UNDEF_DOMAIN;
5288 const search_domain m_search = ALL_DOMAIN;
5289
5290 /* The list of CUs from the index entry of the symbol, or NULL if
5291 not found. */
5292 const gdb_byte *m_addr;
5293 };
5294
5295 const char *
5296 mapped_debug_names::namei_to_name (uint32_t namei) const
5297 {
5298 const ULONGEST namei_string_offs
5299 = extract_unsigned_integer ((name_table_string_offs_reordered
5300 + namei * offset_size),
5301 offset_size,
5302 dwarf5_byte_order);
5303 return read_indirect_string_at_offset (dwarf2_per_objfile,
5304 namei_string_offs);
5305 }
5306
5307 /* Find a slot in .debug_names for the object named NAME. If NAME is
5308 found, return pointer to its pool data. If NAME cannot be found,
5309 return NULL. */
5310
5311 const gdb_byte *
5312 dw2_debug_names_iterator::find_vec_in_debug_names
5313 (const mapped_debug_names &map, const char *name)
5314 {
5315 int (*cmp) (const char *, const char *);
5316
5317 gdb::unique_xmalloc_ptr<char> without_params;
5318 if (current_language->la_language == language_cplus
5319 || current_language->la_language == language_fortran
5320 || current_language->la_language == language_d)
5321 {
5322 /* NAME is already canonical. Drop any qualifiers as
5323 .debug_names does not contain any. */
5324
5325 if (strchr (name, '(') != NULL)
5326 {
5327 without_params = cp_remove_params (name);
5328 if (without_params != NULL)
5329 name = without_params.get ();
5330 }
5331 }
5332
5333 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5334
5335 const uint32_t full_hash = dwarf5_djb_hash (name);
5336 uint32_t namei
5337 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5338 (map.bucket_table_reordered
5339 + (full_hash % map.bucket_count)), 4,
5340 map.dwarf5_byte_order);
5341 if (namei == 0)
5342 return NULL;
5343 --namei;
5344 if (namei >= map.name_count)
5345 {
5346 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5347 "[in module %s]"),
5348 namei, map.name_count,
5349 objfile_name (map.dwarf2_per_objfile->objfile));
5350 return NULL;
5351 }
5352
5353 for (;;)
5354 {
5355 const uint32_t namei_full_hash
5356 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5357 (map.hash_table_reordered + namei), 4,
5358 map.dwarf5_byte_order);
5359 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5360 return NULL;
5361
5362 if (full_hash == namei_full_hash)
5363 {
5364 const char *const namei_string = map.namei_to_name (namei);
5365
5366 #if 0 /* An expensive sanity check. */
5367 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5368 {
5369 complaint (_("Wrong .debug_names hash for string at index %u "
5370 "[in module %s]"),
5371 namei, objfile_name (dwarf2_per_objfile->objfile));
5372 return NULL;
5373 }
5374 #endif
5375
5376 if (cmp (namei_string, name) == 0)
5377 {
5378 const ULONGEST namei_entry_offs
5379 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5380 + namei * map.offset_size),
5381 map.offset_size, map.dwarf5_byte_order);
5382 return map.entry_pool + namei_entry_offs;
5383 }
5384 }
5385
5386 ++namei;
5387 if (namei >= map.name_count)
5388 return NULL;
5389 }
5390 }
5391
5392 const gdb_byte *
5393 dw2_debug_names_iterator::find_vec_in_debug_names
5394 (const mapped_debug_names &map, uint32_t namei)
5395 {
5396 if (namei >= map.name_count)
5397 {
5398 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5399 "[in module %s]"),
5400 namei, map.name_count,
5401 objfile_name (map.dwarf2_per_objfile->objfile));
5402 return NULL;
5403 }
5404
5405 const ULONGEST namei_entry_offs
5406 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5407 + namei * map.offset_size),
5408 map.offset_size, map.dwarf5_byte_order);
5409 return map.entry_pool + namei_entry_offs;
5410 }
5411
5412 /* See dw2_debug_names_iterator. */
5413
5414 dwarf2_per_cu_data *
5415 dw2_debug_names_iterator::next ()
5416 {
5417 if (m_addr == NULL)
5418 return NULL;
5419
5420 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5421 struct objfile *objfile = dwarf2_per_objfile->objfile;
5422 bfd *const abfd = objfile->obfd;
5423
5424 again:
5425
5426 unsigned int bytes_read;
5427 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5428 m_addr += bytes_read;
5429 if (abbrev == 0)
5430 return NULL;
5431
5432 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5433 if (indexval_it == m_map.abbrev_map.cend ())
5434 {
5435 complaint (_("Wrong .debug_names undefined abbrev code %s "
5436 "[in module %s]"),
5437 pulongest (abbrev), objfile_name (objfile));
5438 return NULL;
5439 }
5440 const mapped_debug_names::index_val &indexval = indexval_it->second;
5441 enum class symbol_linkage {
5442 unknown,
5443 static_,
5444 extern_,
5445 } symbol_linkage_ = symbol_linkage::unknown;
5446 dwarf2_per_cu_data *per_cu = NULL;
5447 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5448 {
5449 ULONGEST ull;
5450 switch (attr.form)
5451 {
5452 case DW_FORM_implicit_const:
5453 ull = attr.implicit_const;
5454 break;
5455 case DW_FORM_flag_present:
5456 ull = 1;
5457 break;
5458 case DW_FORM_udata:
5459 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5460 m_addr += bytes_read;
5461 break;
5462 case DW_FORM_ref4:
5463 ull = read_4_bytes (abfd, m_addr);
5464 m_addr += 4;
5465 break;
5466 case DW_FORM_ref8:
5467 ull = read_8_bytes (abfd, m_addr);
5468 m_addr += 8;
5469 break;
5470 case DW_FORM_ref_sig8:
5471 ull = read_8_bytes (abfd, m_addr);
5472 m_addr += 8;
5473 break;
5474 default:
5475 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5476 dwarf_form_name (attr.form),
5477 objfile_name (objfile));
5478 return NULL;
5479 }
5480 switch (attr.dw_idx)
5481 {
5482 case DW_IDX_compile_unit:
5483 /* Don't crash on bad data. */
5484 if (ull >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
5485 {
5486 complaint (_(".debug_names entry has bad CU index %s"
5487 " [in module %s]"),
5488 pulongest (ull),
5489 objfile_name (dwarf2_per_objfile->objfile));
5490 continue;
5491 }
5492 per_cu = dwarf2_per_objfile->per_bfd->get_cutu (ull);
5493 break;
5494 case DW_IDX_type_unit:
5495 /* Don't crash on bad data. */
5496 if (ull >= dwarf2_per_objfile->per_bfd->all_type_units.size ())
5497 {
5498 complaint (_(".debug_names entry has bad TU index %s"
5499 " [in module %s]"),
5500 pulongest (ull),
5501 objfile_name (dwarf2_per_objfile->objfile));
5502 continue;
5503 }
5504 per_cu = &dwarf2_per_objfile->per_bfd->get_tu (ull)->per_cu;
5505 break;
5506 case DW_IDX_die_offset:
5507 /* In a per-CU index (as opposed to a per-module index), index
5508 entries without CU attribute implicitly refer to the single CU. */
5509 if (per_cu == NULL)
5510 per_cu = dwarf2_per_objfile->per_bfd->get_cu (0);
5511 break;
5512 case DW_IDX_GNU_internal:
5513 if (!m_map.augmentation_is_gdb)
5514 break;
5515 symbol_linkage_ = symbol_linkage::static_;
5516 break;
5517 case DW_IDX_GNU_external:
5518 if (!m_map.augmentation_is_gdb)
5519 break;
5520 symbol_linkage_ = symbol_linkage::extern_;
5521 break;
5522 }
5523 }
5524
5525 /* Skip if already read in. */
5526 if (dwarf2_per_objfile->symtab_set_p (per_cu))
5527 goto again;
5528
5529 /* Check static vs global. */
5530 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5531 {
5532 const bool want_static = *m_block_index == STATIC_BLOCK;
5533 const bool symbol_is_static =
5534 symbol_linkage_ == symbol_linkage::static_;
5535 if (want_static != symbol_is_static)
5536 goto again;
5537 }
5538
5539 /* Match dw2_symtab_iter_next, symbol_kind
5540 and debug_names::psymbol_tag. */
5541 switch (m_domain)
5542 {
5543 case VAR_DOMAIN:
5544 switch (indexval.dwarf_tag)
5545 {
5546 case DW_TAG_variable:
5547 case DW_TAG_subprogram:
5548 /* Some types are also in VAR_DOMAIN. */
5549 case DW_TAG_typedef:
5550 case DW_TAG_structure_type:
5551 break;
5552 default:
5553 goto again;
5554 }
5555 break;
5556 case STRUCT_DOMAIN:
5557 switch (indexval.dwarf_tag)
5558 {
5559 case DW_TAG_typedef:
5560 case DW_TAG_structure_type:
5561 break;
5562 default:
5563 goto again;
5564 }
5565 break;
5566 case LABEL_DOMAIN:
5567 switch (indexval.dwarf_tag)
5568 {
5569 case 0:
5570 case DW_TAG_variable:
5571 break;
5572 default:
5573 goto again;
5574 }
5575 break;
5576 case MODULE_DOMAIN:
5577 switch (indexval.dwarf_tag)
5578 {
5579 case DW_TAG_module:
5580 break;
5581 default:
5582 goto again;
5583 }
5584 break;
5585 default:
5586 break;
5587 }
5588
5589 /* Match dw2_expand_symtabs_matching, symbol_kind and
5590 debug_names::psymbol_tag. */
5591 switch (m_search)
5592 {
5593 case VARIABLES_DOMAIN:
5594 switch (indexval.dwarf_tag)
5595 {
5596 case DW_TAG_variable:
5597 break;
5598 default:
5599 goto again;
5600 }
5601 break;
5602 case FUNCTIONS_DOMAIN:
5603 switch (indexval.dwarf_tag)
5604 {
5605 case DW_TAG_subprogram:
5606 break;
5607 default:
5608 goto again;
5609 }
5610 break;
5611 case TYPES_DOMAIN:
5612 switch (indexval.dwarf_tag)
5613 {
5614 case DW_TAG_typedef:
5615 case DW_TAG_structure_type:
5616 break;
5617 default:
5618 goto again;
5619 }
5620 break;
5621 case MODULES_DOMAIN:
5622 switch (indexval.dwarf_tag)
5623 {
5624 case DW_TAG_module:
5625 break;
5626 default:
5627 goto again;
5628 }
5629 default:
5630 break;
5631 }
5632
5633 return per_cu;
5634 }
5635
5636 static struct compunit_symtab *
5637 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5638 const char *name, domain_enum domain)
5639 {
5640 struct dwarf2_per_objfile *dwarf2_per_objfile
5641 = get_dwarf2_per_objfile (objfile);
5642
5643 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
5644 if (!mapp)
5645 {
5646 /* index is NULL if OBJF_READNOW. */
5647 return NULL;
5648 }
5649 const auto &map = *mapp;
5650
5651 dw2_debug_names_iterator iter (map, block_index, domain, name);
5652
5653 struct compunit_symtab *stab_best = NULL;
5654 struct dwarf2_per_cu_data *per_cu;
5655 while ((per_cu = iter.next ()) != NULL)
5656 {
5657 struct symbol *sym, *with_opaque = NULL;
5658 compunit_symtab *stab
5659 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5660 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5661 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5662
5663 sym = block_find_symbol (block, name, domain,
5664 block_find_non_opaque_type_preferred,
5665 &with_opaque);
5666
5667 /* Some caution must be observed with overloaded functions and
5668 methods, since the index will not contain any overload
5669 information (but NAME might contain it). */
5670
5671 if (sym != NULL
5672 && strcmp_iw (sym->search_name (), name) == 0)
5673 return stab;
5674 if (with_opaque != NULL
5675 && strcmp_iw (with_opaque->search_name (), name) == 0)
5676 stab_best = stab;
5677
5678 /* Keep looking through other CUs. */
5679 }
5680
5681 return stab_best;
5682 }
5683
5684 /* This dumps minimal information about .debug_names. It is called
5685 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5686 uses this to verify that .debug_names has been loaded. */
5687
5688 static void
5689 dw2_debug_names_dump (struct objfile *objfile)
5690 {
5691 struct dwarf2_per_objfile *dwarf2_per_objfile
5692 = get_dwarf2_per_objfile (objfile);
5693
5694 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
5695 printf_filtered (".debug_names:");
5696 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5697 printf_filtered (" exists\n");
5698 else
5699 printf_filtered (" faked for \"readnow\"\n");
5700 printf_filtered ("\n");
5701 }
5702
5703 static void
5704 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5705 const char *func_name)
5706 {
5707 struct dwarf2_per_objfile *dwarf2_per_objfile
5708 = get_dwarf2_per_objfile (objfile);
5709
5710 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5711 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5712 {
5713 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5714
5715 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5716
5717 struct dwarf2_per_cu_data *per_cu;
5718 while ((per_cu = iter.next ()) != NULL)
5719 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5720 }
5721 }
5722
5723 static void
5724 dw2_debug_names_map_matching_symbols
5725 (struct objfile *objfile,
5726 const lookup_name_info &name, domain_enum domain,
5727 int global,
5728 gdb::function_view<symbol_found_callback_ftype> callback,
5729 symbol_compare_ftype *ordered_compare)
5730 {
5731 struct dwarf2_per_objfile *dwarf2_per_objfile
5732 = get_dwarf2_per_objfile (objfile);
5733
5734 /* debug_names_table is NULL if OBJF_READNOW. */
5735 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5736 return;
5737
5738 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5739 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5740
5741 const char *match_name = name.ada ().lookup_name ().c_str ();
5742 auto matcher = [&] (const char *symname)
5743 {
5744 if (ordered_compare == nullptr)
5745 return true;
5746 return ordered_compare (symname, match_name) == 0;
5747 };
5748
5749 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5750 [&] (offset_type namei)
5751 {
5752 /* The name was matched, now expand corresponding CUs that were
5753 marked. */
5754 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5755
5756 struct dwarf2_per_cu_data *per_cu;
5757 while ((per_cu = iter.next ()) != NULL)
5758 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
5759 nullptr);
5760 return true;
5761 });
5762
5763 /* It's a shame we couldn't do this inside the
5764 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5765 that have already been expanded. Instead, this loop matches what
5766 the psymtab code does. */
5767 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5768 {
5769 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5770 if (symtab != nullptr)
5771 {
5772 const struct block *block
5773 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5774 if (!iterate_over_symbols_terminated (block, name,
5775 domain, callback))
5776 break;
5777 }
5778 }
5779 }
5780
5781 static void
5782 dw2_debug_names_expand_symtabs_matching
5783 (struct objfile *objfile,
5784 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5785 const lookup_name_info *lookup_name,
5786 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5787 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5788 enum search_domain kind)
5789 {
5790 struct dwarf2_per_objfile *dwarf2_per_objfile
5791 = get_dwarf2_per_objfile (objfile);
5792
5793 /* debug_names_table is NULL if OBJF_READNOW. */
5794 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5795 return;
5796
5797 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5798
5799 if (symbol_matcher == NULL && lookup_name == NULL)
5800 {
5801 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5802 {
5803 QUIT;
5804
5805 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5806 file_matcher, expansion_notify);
5807 }
5808 return;
5809 }
5810
5811 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5812
5813 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5814 symbol_matcher,
5815 kind, [&] (offset_type namei)
5816 {
5817 /* The name was matched, now expand corresponding CUs that were
5818 marked. */
5819 dw2_debug_names_iterator iter (map, kind, namei);
5820
5821 struct dwarf2_per_cu_data *per_cu;
5822 while ((per_cu = iter.next ()) != NULL)
5823 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5824 file_matcher, expansion_notify);
5825 return true;
5826 });
5827 }
5828
5829 const struct quick_symbol_functions dwarf2_debug_names_functions =
5830 {
5831 dw2_has_symbols,
5832 dw2_find_last_source_symtab,
5833 dw2_forget_cached_source_info,
5834 dw2_map_symtabs_matching_filename,
5835 dw2_debug_names_lookup_symbol,
5836 NULL,
5837 dw2_print_stats,
5838 dw2_debug_names_dump,
5839 dw2_debug_names_expand_symtabs_for_function,
5840 dw2_expand_all_symtabs,
5841 dw2_expand_symtabs_with_fullname,
5842 dw2_debug_names_map_matching_symbols,
5843 dw2_debug_names_expand_symtabs_matching,
5844 dw2_find_pc_sect_compunit_symtab,
5845 NULL,
5846 dw2_map_symbol_filenames
5847 };
5848
5849 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5850 to either a dwarf2_per_bfd or dwz_file object. */
5851
5852 template <typename T>
5853 static gdb::array_view<const gdb_byte>
5854 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5855 {
5856 dwarf2_section_info *section = &section_owner->gdb_index;
5857
5858 if (section->empty ())
5859 return {};
5860
5861 /* Older elfutils strip versions could keep the section in the main
5862 executable while splitting it for the separate debug info file. */
5863 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5864 return {};
5865
5866 section->read (obj);
5867
5868 /* dwarf2_section_info::size is a bfd_size_type, while
5869 gdb::array_view works with size_t. On 32-bit hosts, with
5870 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5871 is 32-bit. So we need an explicit narrowing conversion here.
5872 This is fine, because it's impossible to allocate or mmap an
5873 array/buffer larger than what size_t can represent. */
5874 return gdb::make_array_view (section->buffer, section->size);
5875 }
5876
5877 /* Lookup the index cache for the contents of the index associated to
5878 DWARF2_OBJ. */
5879
5880 static gdb::array_view<const gdb_byte>
5881 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5882 {
5883 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5884 if (build_id == nullptr)
5885 return {};
5886
5887 return global_index_cache.lookup_gdb_index (build_id,
5888 &dwarf2_per_bfd->index_cache_res);
5889 }
5890
5891 /* Same as the above, but for DWZ. */
5892
5893 static gdb::array_view<const gdb_byte>
5894 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5895 {
5896 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5897 if (build_id == nullptr)
5898 return {};
5899
5900 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5901 }
5902
5903 /* See symfile.h. */
5904
5905 bool
5906 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5907 {
5908 struct dwarf2_per_objfile *dwarf2_per_objfile
5909 = get_dwarf2_per_objfile (objfile);
5910
5911 /* If we're about to read full symbols, don't bother with the
5912 indices. In this case we also don't care if some other debug
5913 format is making psymtabs, because they are all about to be
5914 expanded anyway. */
5915 if ((objfile->flags & OBJF_READNOW))
5916 {
5917 dwarf2_per_objfile->per_bfd->using_index = 1;
5918 create_all_comp_units (dwarf2_per_objfile);
5919 create_all_type_units (dwarf2_per_objfile);
5920 dwarf2_per_objfile->per_bfd->quick_file_names_table
5921 = create_quick_file_names_table
5922 (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5923 dwarf2_per_objfile->resize_symtabs ();
5924
5925 for (int i = 0; i < (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
5926 + dwarf2_per_objfile->per_bfd->all_type_units.size ()); ++i)
5927 {
5928 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
5929
5930 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
5931 struct dwarf2_per_cu_quick_data);
5932 }
5933
5934 /* Return 1 so that gdb sees the "quick" functions. However,
5935 these functions will be no-ops because we will have expanded
5936 all symtabs. */
5937 *index_kind = dw_index_kind::GDB_INDEX;
5938 return true;
5939 }
5940
5941 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5942 {
5943 *index_kind = dw_index_kind::DEBUG_NAMES;
5944 dwarf2_per_objfile->resize_symtabs ();
5945 return true;
5946 }
5947
5948 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5949 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5950 get_gdb_index_contents_from_section<dwz_file>))
5951 {
5952 *index_kind = dw_index_kind::GDB_INDEX;
5953 dwarf2_per_objfile->resize_symtabs ();
5954 return true;
5955 }
5956
5957 /* ... otherwise, try to find the index in the index cache. */
5958 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5959 get_gdb_index_contents_from_cache,
5960 get_gdb_index_contents_from_cache_dwz))
5961 {
5962 global_index_cache.hit ();
5963 *index_kind = dw_index_kind::GDB_INDEX;
5964 dwarf2_per_objfile->resize_symtabs ();
5965 return true;
5966 }
5967
5968 global_index_cache.miss ();
5969 return false;
5970 }
5971
5972 \f
5973
5974 /* Build a partial symbol table. */
5975
5976 void
5977 dwarf2_build_psymtabs (struct objfile *objfile)
5978 {
5979 struct dwarf2_per_objfile *dwarf2_per_objfile
5980 = get_dwarf2_per_objfile (objfile);
5981
5982 init_psymbol_list (objfile, 1024);
5983
5984 try
5985 {
5986 /* This isn't really ideal: all the data we allocate on the
5987 objfile's obstack is still uselessly kept around. However,
5988 freeing it seems unsafe. */
5989 psymtab_discarder psymtabs (objfile);
5990 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5991 psymtabs.keep ();
5992
5993 dwarf2_per_objfile->resize_symtabs ();
5994
5995 /* (maybe) store an index in the cache. */
5996 global_index_cache.store (dwarf2_per_objfile);
5997 }
5998 catch (const gdb_exception_error &except)
5999 {
6000 exception_print (gdb_stderr, except);
6001 }
6002 }
6003
6004 /* Find the base address of the compilation unit for range lists and
6005 location lists. It will normally be specified by DW_AT_low_pc.
6006 In DWARF-3 draft 4, the base address could be overridden by
6007 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6008 compilation units with discontinuous ranges. */
6009
6010 static void
6011 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6012 {
6013 struct attribute *attr;
6014
6015 cu->base_address.reset ();
6016
6017 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6018 if (attr != nullptr)
6019 cu->base_address = attr->value_as_address ();
6020 else
6021 {
6022 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6023 if (attr != nullptr)
6024 cu->base_address = attr->value_as_address ();
6025 }
6026 }
6027
6028 /* Helper function that returns the proper abbrev section for
6029 THIS_CU. */
6030
6031 static struct dwarf2_section_info *
6032 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6033 {
6034 struct dwarf2_section_info *abbrev;
6035 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6036
6037 if (this_cu->is_dwz)
6038 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6039 else
6040 abbrev = &per_bfd->abbrev;
6041
6042 return abbrev;
6043 }
6044
6045 /* Fetch the abbreviation table offset from a comp or type unit header. */
6046
6047 static sect_offset
6048 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6049 struct dwarf2_section_info *section,
6050 sect_offset sect_off)
6051 {
6052 bfd *abfd = section->get_bfd_owner ();
6053 const gdb_byte *info_ptr;
6054 unsigned int initial_length_size, offset_size;
6055 uint16_t version;
6056
6057 section->read (dwarf2_per_objfile->objfile);
6058 info_ptr = section->buffer + to_underlying (sect_off);
6059 read_initial_length (abfd, info_ptr, &initial_length_size);
6060 offset_size = initial_length_size == 4 ? 4 : 8;
6061 info_ptr += initial_length_size;
6062
6063 version = read_2_bytes (abfd, info_ptr);
6064 info_ptr += 2;
6065 if (version >= 5)
6066 {
6067 /* Skip unit type and address size. */
6068 info_ptr += 2;
6069 }
6070
6071 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6072 }
6073
6074 /* A partial symtab that is used only for include files. */
6075 struct dwarf2_include_psymtab : public partial_symtab
6076 {
6077 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6078 : partial_symtab (filename, objfile)
6079 {
6080 }
6081
6082 void read_symtab (struct objfile *objfile) override
6083 {
6084 /* It's an include file, no symbols to read for it.
6085 Everything is in the includer symtab. */
6086
6087 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6088 expansion of the includer psymtab. We use the dependencies[0] field to
6089 model the includer. But if we go the regular route of calling
6090 expand_psymtab here, and having expand_psymtab call expand_dependencies
6091 to expand the includer, we'll only use expand_psymtab on the includer
6092 (making it a non-toplevel psymtab), while if we expand the includer via
6093 another path, we'll use read_symtab (making it a toplevel psymtab).
6094 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6095 psymtab, and trigger read_symtab on the includer here directly. */
6096 includer ()->read_symtab (objfile);
6097 }
6098
6099 void expand_psymtab (struct objfile *objfile) override
6100 {
6101 /* This is not called by read_symtab, and should not be called by any
6102 expand_dependencies. */
6103 gdb_assert (false);
6104 }
6105
6106 bool readin_p (struct objfile *objfile) const override
6107 {
6108 return includer ()->readin_p (objfile);
6109 }
6110
6111 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6112 {
6113 return nullptr;
6114 }
6115
6116 private:
6117 partial_symtab *includer () const
6118 {
6119 /* An include psymtab has exactly one dependency: the psymtab that
6120 includes it. */
6121 gdb_assert (this->number_of_dependencies == 1);
6122 return this->dependencies[0];
6123 }
6124 };
6125
6126 /* Allocate a new partial symtab for file named NAME and mark this new
6127 partial symtab as being an include of PST. */
6128
6129 static void
6130 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6131 struct objfile *objfile)
6132 {
6133 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6134
6135 if (!IS_ABSOLUTE_PATH (subpst->filename))
6136 subpst->dirname = pst->dirname;
6137
6138 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6139 subpst->dependencies[0] = pst;
6140 subpst->number_of_dependencies = 1;
6141 }
6142
6143 /* Read the Line Number Program data and extract the list of files
6144 included by the source file represented by PST. Build an include
6145 partial symtab for each of these included files. */
6146
6147 static void
6148 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6149 struct die_info *die,
6150 dwarf2_psymtab *pst)
6151 {
6152 line_header_up lh;
6153 struct attribute *attr;
6154
6155 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6156 if (attr != nullptr)
6157 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6158 if (lh == NULL)
6159 return; /* No linetable, so no includes. */
6160
6161 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6162 that we pass in the raw text_low here; that is ok because we're
6163 only decoding the line table to make include partial symtabs, and
6164 so the addresses aren't really used. */
6165 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6166 pst->raw_text_low (), 1);
6167 }
6168
6169 static hashval_t
6170 hash_signatured_type (const void *item)
6171 {
6172 const struct signatured_type *sig_type
6173 = (const struct signatured_type *) item;
6174
6175 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6176 return sig_type->signature;
6177 }
6178
6179 static int
6180 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6181 {
6182 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6183 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6184
6185 return lhs->signature == rhs->signature;
6186 }
6187
6188 /* Allocate a hash table for signatured types. */
6189
6190 static htab_up
6191 allocate_signatured_type_table ()
6192 {
6193 return htab_up (htab_create_alloc (41,
6194 hash_signatured_type,
6195 eq_signatured_type,
6196 NULL, xcalloc, xfree));
6197 }
6198
6199 /* A helper function to add a signatured type CU to a table. */
6200
6201 static int
6202 add_signatured_type_cu_to_table (void **slot, void *datum)
6203 {
6204 struct signatured_type *sigt = (struct signatured_type *) *slot;
6205 std::vector<signatured_type *> *all_type_units
6206 = (std::vector<signatured_type *> *) datum;
6207
6208 all_type_units->push_back (sigt);
6209
6210 return 1;
6211 }
6212
6213 /* A helper for create_debug_types_hash_table. Read types from SECTION
6214 and fill them into TYPES_HTAB. It will process only type units,
6215 therefore DW_UT_type. */
6216
6217 static void
6218 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6219 struct dwo_file *dwo_file,
6220 dwarf2_section_info *section, htab_up &types_htab,
6221 rcuh_kind section_kind)
6222 {
6223 struct objfile *objfile = dwarf2_per_objfile->objfile;
6224 struct dwarf2_section_info *abbrev_section;
6225 bfd *abfd;
6226 const gdb_byte *info_ptr, *end_ptr;
6227
6228 abbrev_section = (dwo_file != NULL
6229 ? &dwo_file->sections.abbrev
6230 : &dwarf2_per_objfile->per_bfd->abbrev);
6231
6232 if (dwarf_read_debug)
6233 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6234 section->get_name (),
6235 abbrev_section->get_file_name ());
6236
6237 section->read (objfile);
6238 info_ptr = section->buffer;
6239
6240 if (info_ptr == NULL)
6241 return;
6242
6243 /* We can't set abfd until now because the section may be empty or
6244 not present, in which case the bfd is unknown. */
6245 abfd = section->get_bfd_owner ();
6246
6247 /* We don't use cutu_reader here because we don't need to read
6248 any dies: the signature is in the header. */
6249
6250 end_ptr = info_ptr + section->size;
6251 while (info_ptr < end_ptr)
6252 {
6253 struct signatured_type *sig_type;
6254 struct dwo_unit *dwo_tu;
6255 void **slot;
6256 const gdb_byte *ptr = info_ptr;
6257 struct comp_unit_head header;
6258 unsigned int length;
6259
6260 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6261
6262 /* Initialize it due to a false compiler warning. */
6263 header.signature = -1;
6264 header.type_cu_offset_in_tu = (cu_offset) -1;
6265
6266 /* We need to read the type's signature in order to build the hash
6267 table, but we don't need anything else just yet. */
6268
6269 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6270 abbrev_section, ptr, section_kind);
6271
6272 length = header.get_length ();
6273
6274 /* Skip dummy type units. */
6275 if (ptr >= info_ptr + length
6276 || peek_abbrev_code (abfd, ptr) == 0
6277 || header.unit_type != DW_UT_type)
6278 {
6279 info_ptr += length;
6280 continue;
6281 }
6282
6283 if (types_htab == NULL)
6284 {
6285 if (dwo_file)
6286 types_htab = allocate_dwo_unit_table ();
6287 else
6288 types_htab = allocate_signatured_type_table ();
6289 }
6290
6291 if (dwo_file)
6292 {
6293 sig_type = NULL;
6294 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6295 struct dwo_unit);
6296 dwo_tu->dwo_file = dwo_file;
6297 dwo_tu->signature = header.signature;
6298 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6299 dwo_tu->section = section;
6300 dwo_tu->sect_off = sect_off;
6301 dwo_tu->length = length;
6302 }
6303 else
6304 {
6305 /* N.B.: type_offset is not usable if this type uses a DWO file.
6306 The real type_offset is in the DWO file. */
6307 dwo_tu = NULL;
6308 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6309 sig_type->signature = header.signature;
6310 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6311 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6312 sig_type->per_cu.is_debug_types = 1;
6313 sig_type->per_cu.section = section;
6314 sig_type->per_cu.sect_off = sect_off;
6315 sig_type->per_cu.length = length;
6316 }
6317
6318 slot = htab_find_slot (types_htab.get (),
6319 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6320 INSERT);
6321 gdb_assert (slot != NULL);
6322 if (*slot != NULL)
6323 {
6324 sect_offset dup_sect_off;
6325
6326 if (dwo_file)
6327 {
6328 const struct dwo_unit *dup_tu
6329 = (const struct dwo_unit *) *slot;
6330
6331 dup_sect_off = dup_tu->sect_off;
6332 }
6333 else
6334 {
6335 const struct signatured_type *dup_tu
6336 = (const struct signatured_type *) *slot;
6337
6338 dup_sect_off = dup_tu->per_cu.sect_off;
6339 }
6340
6341 complaint (_("debug type entry at offset %s is duplicate to"
6342 " the entry at offset %s, signature %s"),
6343 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6344 hex_string (header.signature));
6345 }
6346 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6347
6348 if (dwarf_read_debug > 1)
6349 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6350 sect_offset_str (sect_off),
6351 hex_string (header.signature));
6352
6353 info_ptr += length;
6354 }
6355 }
6356
6357 /* Create the hash table of all entries in the .debug_types
6358 (or .debug_types.dwo) section(s).
6359 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6360 otherwise it is NULL.
6361
6362 The result is a pointer to the hash table or NULL if there are no types.
6363
6364 Note: This function processes DWO files only, not DWP files. */
6365
6366 static void
6367 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6368 struct dwo_file *dwo_file,
6369 gdb::array_view<dwarf2_section_info> type_sections,
6370 htab_up &types_htab)
6371 {
6372 for (dwarf2_section_info &section : type_sections)
6373 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6374 types_htab, rcuh_kind::TYPE);
6375 }
6376
6377 /* Create the hash table of all entries in the .debug_types section,
6378 and initialize all_type_units.
6379 The result is zero if there is an error (e.g. missing .debug_types section),
6380 otherwise non-zero. */
6381
6382 static int
6383 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6384 {
6385 htab_up types_htab;
6386
6387 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6388 &dwarf2_per_objfile->per_bfd->info, types_htab,
6389 rcuh_kind::COMPILE);
6390 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6391 dwarf2_per_objfile->per_bfd->types, types_htab);
6392 if (types_htab == NULL)
6393 {
6394 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
6395 return 0;
6396 }
6397
6398 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
6399
6400 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6401 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6402 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
6403
6404 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6405 add_signatured_type_cu_to_table,
6406 &dwarf2_per_objfile->per_bfd->all_type_units);
6407
6408 return 1;
6409 }
6410
6411 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6412 If SLOT is non-NULL, it is the entry to use in the hash table.
6413 Otherwise we find one. */
6414
6415 static struct signatured_type *
6416 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6417 void **slot)
6418 {
6419 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6420 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6421 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6422
6423 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6424
6425 dwarf2_per_objfile->resize_symtabs ();
6426
6427 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6428 sig_type->signature = sig;
6429 sig_type->per_cu.is_debug_types = 1;
6430 if (dwarf2_per_objfile->per_bfd->using_index)
6431 {
6432 sig_type->per_cu.v.quick =
6433 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6434 struct dwarf2_per_cu_quick_data);
6435 }
6436
6437 if (slot == NULL)
6438 {
6439 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6440 sig_type, INSERT);
6441 }
6442 gdb_assert (*slot == NULL);
6443 *slot = sig_type;
6444 /* The rest of sig_type must be filled in by the caller. */
6445 return sig_type;
6446 }
6447
6448 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6449 Fill in SIG_ENTRY with DWO_ENTRY. */
6450
6451 static void
6452 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6453 struct signatured_type *sig_entry,
6454 struct dwo_unit *dwo_entry)
6455 {
6456 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6457
6458 /* Make sure we're not clobbering something we don't expect to. */
6459 gdb_assert (! sig_entry->per_cu.queued);
6460 gdb_assert (sig_entry->per_cu.cu == NULL);
6461 if (per_bfd->using_index)
6462 {
6463 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6464 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6465 }
6466 else
6467 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6468 gdb_assert (sig_entry->signature == dwo_entry->signature);
6469 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6470 gdb_assert (sig_entry->type_unit_group == NULL);
6471 gdb_assert (sig_entry->dwo_unit == NULL);
6472
6473 sig_entry->per_cu.section = dwo_entry->section;
6474 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6475 sig_entry->per_cu.length = dwo_entry->length;
6476 sig_entry->per_cu.reading_dwo_directly = 1;
6477 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6478 sig_entry->per_cu.per_bfd = per_bfd;
6479 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6480 sig_entry->dwo_unit = dwo_entry;
6481 }
6482
6483 /* Subroutine of lookup_signatured_type.
6484 If we haven't read the TU yet, create the signatured_type data structure
6485 for a TU to be read in directly from a DWO file, bypassing the stub.
6486 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6487 using .gdb_index, then when reading a CU we want to stay in the DWO file
6488 containing that CU. Otherwise we could end up reading several other DWO
6489 files (due to comdat folding) to process the transitive closure of all the
6490 mentioned TUs, and that can be slow. The current DWO file will have every
6491 type signature that it needs.
6492 We only do this for .gdb_index because in the psymtab case we already have
6493 to read all the DWOs to build the type unit groups. */
6494
6495 static struct signatured_type *
6496 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6497 {
6498 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6499 struct dwo_file *dwo_file;
6500 struct dwo_unit find_dwo_entry, *dwo_entry;
6501 struct signatured_type find_sig_entry, *sig_entry;
6502 void **slot;
6503
6504 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6505
6506 /* If TU skeletons have been removed then we may not have read in any
6507 TUs yet. */
6508 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6509 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6510
6511 /* We only ever need to read in one copy of a signatured type.
6512 Use the global signatured_types array to do our own comdat-folding
6513 of types. If this is the first time we're reading this TU, and
6514 the TU has an entry in .gdb_index, replace the recorded data from
6515 .gdb_index with this TU. */
6516
6517 find_sig_entry.signature = sig;
6518 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6519 &find_sig_entry, INSERT);
6520 sig_entry = (struct signatured_type *) *slot;
6521
6522 /* We can get here with the TU already read, *or* in the process of being
6523 read. Don't reassign the global entry to point to this DWO if that's
6524 the case. Also note that if the TU is already being read, it may not
6525 have come from a DWO, the program may be a mix of Fission-compiled
6526 code and non-Fission-compiled code. */
6527
6528 /* Have we already tried to read this TU?
6529 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6530 needn't exist in the global table yet). */
6531 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6532 return sig_entry;
6533
6534 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6535 dwo_unit of the TU itself. */
6536 dwo_file = cu->dwo_unit->dwo_file;
6537
6538 /* Ok, this is the first time we're reading this TU. */
6539 if (dwo_file->tus == NULL)
6540 return NULL;
6541 find_dwo_entry.signature = sig;
6542 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6543 &find_dwo_entry);
6544 if (dwo_entry == NULL)
6545 return NULL;
6546
6547 /* If the global table doesn't have an entry for this TU, add one. */
6548 if (sig_entry == NULL)
6549 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6550
6551 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6552 sig_entry->per_cu.tu_read = 1;
6553 return sig_entry;
6554 }
6555
6556 /* Subroutine of lookup_signatured_type.
6557 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6558 then try the DWP file. If the TU stub (skeleton) has been removed then
6559 it won't be in .gdb_index. */
6560
6561 static struct signatured_type *
6562 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6563 {
6564 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6565 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6566 struct dwo_unit *dwo_entry;
6567 struct signatured_type find_sig_entry, *sig_entry;
6568 void **slot;
6569
6570 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6571 gdb_assert (dwp_file != NULL);
6572
6573 /* If TU skeletons have been removed then we may not have read in any
6574 TUs yet. */
6575 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6576 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6577
6578 find_sig_entry.signature = sig;
6579 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6580 &find_sig_entry, INSERT);
6581 sig_entry = (struct signatured_type *) *slot;
6582
6583 /* Have we already tried to read this TU?
6584 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6585 needn't exist in the global table yet). */
6586 if (sig_entry != NULL)
6587 return sig_entry;
6588
6589 if (dwp_file->tus == NULL)
6590 return NULL;
6591 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6592 sig, 1 /* is_debug_types */);
6593 if (dwo_entry == NULL)
6594 return NULL;
6595
6596 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6597 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6598
6599 return sig_entry;
6600 }
6601
6602 /* Lookup a signature based type for DW_FORM_ref_sig8.
6603 Returns NULL if signature SIG is not present in the table.
6604 It is up to the caller to complain about this. */
6605
6606 static struct signatured_type *
6607 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6608 {
6609 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6610
6611 if (cu->dwo_unit
6612 && dwarf2_per_objfile->per_bfd->using_index)
6613 {
6614 /* We're in a DWO/DWP file, and we're using .gdb_index.
6615 These cases require special processing. */
6616 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6617 return lookup_dwo_signatured_type (cu, sig);
6618 else
6619 return lookup_dwp_signatured_type (cu, sig);
6620 }
6621 else
6622 {
6623 struct signatured_type find_entry, *entry;
6624
6625 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6626 return NULL;
6627 find_entry.signature = sig;
6628 entry = ((struct signatured_type *)
6629 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6630 &find_entry));
6631 return entry;
6632 }
6633 }
6634
6635 /* Low level DIE reading support. */
6636
6637 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6638
6639 static void
6640 init_cu_die_reader (struct die_reader_specs *reader,
6641 struct dwarf2_cu *cu,
6642 struct dwarf2_section_info *section,
6643 struct dwo_file *dwo_file,
6644 struct abbrev_table *abbrev_table)
6645 {
6646 gdb_assert (section->readin && section->buffer != NULL);
6647 reader->abfd = section->get_bfd_owner ();
6648 reader->cu = cu;
6649 reader->dwo_file = dwo_file;
6650 reader->die_section = section;
6651 reader->buffer = section->buffer;
6652 reader->buffer_end = section->buffer + section->size;
6653 reader->abbrev_table = abbrev_table;
6654 }
6655
6656 /* Subroutine of cutu_reader to simplify it.
6657 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6658 There's just a lot of work to do, and cutu_reader is big enough
6659 already.
6660
6661 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6662 from it to the DIE in the DWO. If NULL we are skipping the stub.
6663 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6664 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6665 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6666 STUB_COMP_DIR may be non-NULL.
6667 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6668 are filled in with the info of the DIE from the DWO file.
6669 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6670 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6671 kept around for at least as long as *RESULT_READER.
6672
6673 The result is non-zero if a valid (non-dummy) DIE was found. */
6674
6675 static int
6676 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6677 struct dwo_unit *dwo_unit,
6678 struct die_info *stub_comp_unit_die,
6679 const char *stub_comp_dir,
6680 struct die_reader_specs *result_reader,
6681 const gdb_byte **result_info_ptr,
6682 struct die_info **result_comp_unit_die,
6683 abbrev_table_up *result_dwo_abbrev_table)
6684 {
6685 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6686 struct objfile *objfile = dwarf2_per_objfile->objfile;
6687 struct dwarf2_cu *cu = this_cu->cu;
6688 bfd *abfd;
6689 const gdb_byte *begin_info_ptr, *info_ptr;
6690 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6691 int i,num_extra_attrs;
6692 struct dwarf2_section_info *dwo_abbrev_section;
6693 struct die_info *comp_unit_die;
6694
6695 /* At most one of these may be provided. */
6696 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6697
6698 /* These attributes aren't processed until later:
6699 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6700 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6701 referenced later. However, these attributes are found in the stub
6702 which we won't have later. In order to not impose this complication
6703 on the rest of the code, we read them here and copy them to the
6704 DWO CU/TU die. */
6705
6706 stmt_list = NULL;
6707 low_pc = NULL;
6708 high_pc = NULL;
6709 ranges = NULL;
6710 comp_dir = NULL;
6711
6712 if (stub_comp_unit_die != NULL)
6713 {
6714 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6715 DWO file. */
6716 if (! this_cu->is_debug_types)
6717 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6718 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6719 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6720 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6721 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6722
6723 cu->addr_base = stub_comp_unit_die->addr_base ();
6724
6725 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6726 here (if needed). We need the value before we can process
6727 DW_AT_ranges. */
6728 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6729 }
6730 else if (stub_comp_dir != NULL)
6731 {
6732 /* Reconstruct the comp_dir attribute to simplify the code below. */
6733 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6734 comp_dir->name = DW_AT_comp_dir;
6735 comp_dir->form = DW_FORM_string;
6736 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6737 DW_STRING (comp_dir) = stub_comp_dir;
6738 }
6739
6740 /* Set up for reading the DWO CU/TU. */
6741 cu->dwo_unit = dwo_unit;
6742 dwarf2_section_info *section = dwo_unit->section;
6743 section->read (objfile);
6744 abfd = section->get_bfd_owner ();
6745 begin_info_ptr = info_ptr = (section->buffer
6746 + to_underlying (dwo_unit->sect_off));
6747 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6748
6749 if (this_cu->is_debug_types)
6750 {
6751 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6752
6753 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6754 &cu->header, section,
6755 dwo_abbrev_section,
6756 info_ptr, rcuh_kind::TYPE);
6757 /* This is not an assert because it can be caused by bad debug info. */
6758 if (sig_type->signature != cu->header.signature)
6759 {
6760 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6761 " TU at offset %s [in module %s]"),
6762 hex_string (sig_type->signature),
6763 hex_string (cu->header.signature),
6764 sect_offset_str (dwo_unit->sect_off),
6765 bfd_get_filename (abfd));
6766 }
6767 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6768 /* For DWOs coming from DWP files, we don't know the CU length
6769 nor the type's offset in the TU until now. */
6770 dwo_unit->length = cu->header.get_length ();
6771 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6772
6773 /* Establish the type offset that can be used to lookup the type.
6774 For DWO files, we don't know it until now. */
6775 sig_type->type_offset_in_section
6776 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6777 }
6778 else
6779 {
6780 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6781 &cu->header, section,
6782 dwo_abbrev_section,
6783 info_ptr, rcuh_kind::COMPILE);
6784 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6785 /* For DWOs coming from DWP files, we don't know the CU length
6786 until now. */
6787 dwo_unit->length = cu->header.get_length ();
6788 }
6789
6790 *result_dwo_abbrev_table
6791 = abbrev_table::read (objfile, dwo_abbrev_section,
6792 cu->header.abbrev_sect_off);
6793 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6794 result_dwo_abbrev_table->get ());
6795
6796 /* Read in the die, but leave space to copy over the attributes
6797 from the stub. This has the benefit of simplifying the rest of
6798 the code - all the work to maintain the illusion of a single
6799 DW_TAG_{compile,type}_unit DIE is done here. */
6800 num_extra_attrs = ((stmt_list != NULL)
6801 + (low_pc != NULL)
6802 + (high_pc != NULL)
6803 + (ranges != NULL)
6804 + (comp_dir != NULL));
6805 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6806 num_extra_attrs);
6807
6808 /* Copy over the attributes from the stub to the DIE we just read in. */
6809 comp_unit_die = *result_comp_unit_die;
6810 i = comp_unit_die->num_attrs;
6811 if (stmt_list != NULL)
6812 comp_unit_die->attrs[i++] = *stmt_list;
6813 if (low_pc != NULL)
6814 comp_unit_die->attrs[i++] = *low_pc;
6815 if (high_pc != NULL)
6816 comp_unit_die->attrs[i++] = *high_pc;
6817 if (ranges != NULL)
6818 comp_unit_die->attrs[i++] = *ranges;
6819 if (comp_dir != NULL)
6820 comp_unit_die->attrs[i++] = *comp_dir;
6821 comp_unit_die->num_attrs += num_extra_attrs;
6822
6823 if (dwarf_die_debug)
6824 {
6825 fprintf_unfiltered (gdb_stdlog,
6826 "Read die from %s@0x%x of %s:\n",
6827 section->get_name (),
6828 (unsigned) (begin_info_ptr - section->buffer),
6829 bfd_get_filename (abfd));
6830 dump_die (comp_unit_die, dwarf_die_debug);
6831 }
6832
6833 /* Skip dummy compilation units. */
6834 if (info_ptr >= begin_info_ptr + dwo_unit->length
6835 || peek_abbrev_code (abfd, info_ptr) == 0)
6836 return 0;
6837
6838 *result_info_ptr = info_ptr;
6839 return 1;
6840 }
6841
6842 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6843 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6844 signature is part of the header. */
6845 static gdb::optional<ULONGEST>
6846 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6847 {
6848 if (cu->header.version >= 5)
6849 return cu->header.signature;
6850 struct attribute *attr;
6851 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6852 if (attr == nullptr)
6853 return gdb::optional<ULONGEST> ();
6854 return DW_UNSND (attr);
6855 }
6856
6857 /* Subroutine of cutu_reader to simplify it.
6858 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6859 Returns NULL if the specified DWO unit cannot be found. */
6860
6861 static struct dwo_unit *
6862 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6863 struct die_info *comp_unit_die,
6864 const char *dwo_name)
6865 {
6866 struct dwarf2_cu *cu = this_cu->cu;
6867 struct dwo_unit *dwo_unit;
6868 const char *comp_dir;
6869
6870 gdb_assert (cu != NULL);
6871
6872 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6873 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6874 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6875
6876 if (this_cu->is_debug_types)
6877 {
6878 struct signatured_type *sig_type;
6879
6880 /* Since this_cu is the first member of struct signatured_type,
6881 we can go from a pointer to one to a pointer to the other. */
6882 sig_type = (struct signatured_type *) this_cu;
6883 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6884 }
6885 else
6886 {
6887 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6888 if (!signature.has_value ())
6889 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6890 " [in module %s]"),
6891 dwo_name, bfd_get_filename (this_cu->per_bfd->obfd));
6892 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6893 *signature);
6894 }
6895
6896 return dwo_unit;
6897 }
6898
6899 /* Subroutine of cutu_reader to simplify it.
6900 See it for a description of the parameters.
6901 Read a TU directly from a DWO file, bypassing the stub. */
6902
6903 void
6904 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6905 dwarf2_per_objfile *per_objfile,
6906 int use_existing_cu)
6907 {
6908 struct signatured_type *sig_type;
6909
6910 /* Verify we can do the following downcast, and that we have the
6911 data we need. */
6912 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6913 sig_type = (struct signatured_type *) this_cu;
6914 gdb_assert (sig_type->dwo_unit != NULL);
6915
6916 if (use_existing_cu && this_cu->cu != NULL)
6917 {
6918 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6919 /* There's no need to do the rereading_dwo_cu handling that
6920 cutu_reader does since we don't read the stub. */
6921 }
6922 else
6923 {
6924 /* If !use_existing_cu, this_cu->cu must be NULL. */
6925 gdb_assert (this_cu->cu == NULL);
6926 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6927 }
6928
6929 /* A future optimization, if needed, would be to use an existing
6930 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6931 could share abbrev tables. */
6932
6933 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6934 NULL /* stub_comp_unit_die */,
6935 sig_type->dwo_unit->dwo_file->comp_dir,
6936 this, &info_ptr,
6937 &comp_unit_die,
6938 &m_dwo_abbrev_table) == 0)
6939 {
6940 /* Dummy die. */
6941 dummy_p = true;
6942 }
6943 }
6944
6945 /* Initialize a CU (or TU) and read its DIEs.
6946 If the CU defers to a DWO file, read the DWO file as well.
6947
6948 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6949 Otherwise the table specified in the comp unit header is read in and used.
6950 This is an optimization for when we already have the abbrev table.
6951
6952 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6953 Otherwise, a new CU is allocated with xmalloc. */
6954
6955 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6956 struct abbrev_table *abbrev_table,
6957 int use_existing_cu,
6958 bool skip_partial)
6959 : die_reader_specs {},
6960 m_this_cu (this_cu)
6961 {
6962 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6963 struct objfile *objfile = dwarf2_per_objfile->objfile;
6964 struct dwarf2_section_info *section = this_cu->section;
6965 bfd *abfd = section->get_bfd_owner ();
6966 struct dwarf2_cu *cu;
6967 const gdb_byte *begin_info_ptr;
6968 struct signatured_type *sig_type = NULL;
6969 struct dwarf2_section_info *abbrev_section;
6970 /* Non-zero if CU currently points to a DWO file and we need to
6971 reread it. When this happens we need to reread the skeleton die
6972 before we can reread the DWO file (this only applies to CUs, not TUs). */
6973 int rereading_dwo_cu = 0;
6974
6975 if (dwarf_die_debug)
6976 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6977 this_cu->is_debug_types ? "type" : "comp",
6978 sect_offset_str (this_cu->sect_off));
6979
6980 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6981 file (instead of going through the stub), short-circuit all of this. */
6982 if (this_cu->reading_dwo_directly)
6983 {
6984 /* Narrow down the scope of possibilities to have to understand. */
6985 gdb_assert (this_cu->is_debug_types);
6986 gdb_assert (abbrev_table == NULL);
6987 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, use_existing_cu);
6988 return;
6989 }
6990
6991 /* This is cheap if the section is already read in. */
6992 section->read (objfile);
6993
6994 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6995
6996 abbrev_section = get_abbrev_section_for_cu (this_cu);
6997
6998 if (use_existing_cu && this_cu->cu != NULL)
6999 {
7000 cu = this_cu->cu;
7001 /* If this CU is from a DWO file we need to start over, we need to
7002 refetch the attributes from the skeleton CU.
7003 This could be optimized by retrieving those attributes from when we
7004 were here the first time: the previous comp_unit_die was stored in
7005 comp_unit_obstack. But there's no data yet that we need this
7006 optimization. */
7007 if (cu->dwo_unit != NULL)
7008 rereading_dwo_cu = 1;
7009 }
7010 else
7011 {
7012 /* If !use_existing_cu, this_cu->cu must be NULL. */
7013 gdb_assert (this_cu->cu == NULL);
7014 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7015 cu = m_new_cu.get ();
7016 }
7017
7018 /* Get the header. */
7019 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7020 {
7021 /* We already have the header, there's no need to read it in again. */
7022 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7023 }
7024 else
7025 {
7026 if (this_cu->is_debug_types)
7027 {
7028 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7029 &cu->header, section,
7030 abbrev_section, info_ptr,
7031 rcuh_kind::TYPE);
7032
7033 /* Since per_cu is the first member of struct signatured_type,
7034 we can go from a pointer to one to a pointer to the other. */
7035 sig_type = (struct signatured_type *) this_cu;
7036 gdb_assert (sig_type->signature == cu->header.signature);
7037 gdb_assert (sig_type->type_offset_in_tu
7038 == cu->header.type_cu_offset_in_tu);
7039 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7040
7041 /* LENGTH has not been set yet for type units if we're
7042 using .gdb_index. */
7043 this_cu->length = cu->header.get_length ();
7044
7045 /* Establish the type offset that can be used to lookup the type. */
7046 sig_type->type_offset_in_section =
7047 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7048
7049 this_cu->dwarf_version = cu->header.version;
7050 }
7051 else
7052 {
7053 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7054 &cu->header, section,
7055 abbrev_section,
7056 info_ptr,
7057 rcuh_kind::COMPILE);
7058
7059 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7060 if (this_cu->length == 0)
7061 this_cu->length = cu->header.get_length ();
7062 else
7063 gdb_assert (this_cu->length == cu->header.get_length ());
7064 this_cu->dwarf_version = cu->header.version;
7065 }
7066 }
7067
7068 /* Skip dummy compilation units. */
7069 if (info_ptr >= begin_info_ptr + this_cu->length
7070 || peek_abbrev_code (abfd, info_ptr) == 0)
7071 {
7072 dummy_p = true;
7073 return;
7074 }
7075
7076 /* If we don't have them yet, read the abbrevs for this compilation unit.
7077 And if we need to read them now, make sure they're freed when we're
7078 done. */
7079 if (abbrev_table != NULL)
7080 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7081 else
7082 {
7083 m_abbrev_table_holder
7084 = abbrev_table::read (objfile, abbrev_section,
7085 cu->header.abbrev_sect_off);
7086 abbrev_table = m_abbrev_table_holder.get ();
7087 }
7088
7089 /* Read the top level CU/TU die. */
7090 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7091 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7092
7093 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7094 {
7095 dummy_p = true;
7096 return;
7097 }
7098
7099 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7100 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7101 table from the DWO file and pass the ownership over to us. It will be
7102 referenced from READER, so we must make sure to free it after we're done
7103 with READER.
7104
7105 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7106 DWO CU, that this test will fail (the attribute will not be present). */
7107 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7108 if (dwo_name != nullptr)
7109 {
7110 struct dwo_unit *dwo_unit;
7111 struct die_info *dwo_comp_unit_die;
7112
7113 if (comp_unit_die->has_children)
7114 {
7115 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7116 " has children (offset %s) [in module %s]"),
7117 sect_offset_str (this_cu->sect_off),
7118 bfd_get_filename (abfd));
7119 }
7120 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
7121 if (dwo_unit != NULL)
7122 {
7123 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7124 comp_unit_die, NULL,
7125 this, &info_ptr,
7126 &dwo_comp_unit_die,
7127 &m_dwo_abbrev_table) == 0)
7128 {
7129 /* Dummy die. */
7130 dummy_p = true;
7131 return;
7132 }
7133 comp_unit_die = dwo_comp_unit_die;
7134 }
7135 else
7136 {
7137 /* Yikes, we couldn't find the rest of the DIE, we only have
7138 the stub. A complaint has already been logged. There's
7139 not much more we can do except pass on the stub DIE to
7140 die_reader_func. We don't want to throw an error on bad
7141 debug info. */
7142 }
7143 }
7144 }
7145
7146 void
7147 cutu_reader::keep ()
7148 {
7149 /* Done, clean up. */
7150 gdb_assert (!dummy_p);
7151 if (m_new_cu != NULL)
7152 {
7153 struct dwarf2_per_objfile *dwarf2_per_objfile
7154 = m_this_cu->dwarf2_per_objfile;
7155 /* Link this CU into read_in_chain. */
7156 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->per_bfd->read_in_chain;
7157 dwarf2_per_objfile->per_bfd->read_in_chain = m_this_cu;
7158 /* The chain owns it now. */
7159 m_new_cu.release ();
7160 }
7161 }
7162
7163 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7164 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7165 assumed to have already done the lookup to find the DWO file).
7166
7167 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7168 THIS_CU->is_debug_types, but nothing else.
7169
7170 We fill in THIS_CU->length.
7171
7172 THIS_CU->cu is always freed when done.
7173 This is done in order to not leave THIS_CU->cu in a state where we have
7174 to care whether it refers to the "main" CU or the DWO CU.
7175
7176 When parent_cu is passed, it is used to provide a default value for
7177 str_offsets_base and addr_base from the parent. */
7178
7179 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7180 struct dwarf2_cu *parent_cu,
7181 struct dwo_file *dwo_file)
7182 : die_reader_specs {},
7183 m_this_cu (this_cu)
7184 {
7185 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7186 struct objfile *objfile = dwarf2_per_objfile->objfile;
7187 struct dwarf2_section_info *section = this_cu->section;
7188 bfd *abfd = section->get_bfd_owner ();
7189 struct dwarf2_section_info *abbrev_section;
7190 const gdb_byte *begin_info_ptr, *info_ptr;
7191
7192 if (dwarf_die_debug)
7193 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7194 this_cu->is_debug_types ? "type" : "comp",
7195 sect_offset_str (this_cu->sect_off));
7196
7197 gdb_assert (this_cu->cu == NULL);
7198
7199 abbrev_section = (dwo_file != NULL
7200 ? &dwo_file->sections.abbrev
7201 : get_abbrev_section_for_cu (this_cu));
7202
7203 /* This is cheap if the section is already read in. */
7204 section->read (objfile);
7205
7206 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7207
7208 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7209 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7210 &m_new_cu->header, section,
7211 abbrev_section, info_ptr,
7212 (this_cu->is_debug_types
7213 ? rcuh_kind::TYPE
7214 : rcuh_kind::COMPILE));
7215
7216 if (parent_cu != nullptr)
7217 {
7218 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7219 m_new_cu->addr_base = parent_cu->addr_base;
7220 }
7221 this_cu->length = m_new_cu->header.get_length ();
7222
7223 /* Skip dummy compilation units. */
7224 if (info_ptr >= begin_info_ptr + this_cu->length
7225 || peek_abbrev_code (abfd, info_ptr) == 0)
7226 {
7227 dummy_p = true;
7228 return;
7229 }
7230
7231 m_abbrev_table_holder
7232 = abbrev_table::read (objfile, abbrev_section,
7233 m_new_cu->header.abbrev_sect_off);
7234
7235 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7236 m_abbrev_table_holder.get ());
7237 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7238 }
7239
7240 \f
7241 /* Type Unit Groups.
7242
7243 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7244 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7245 so that all types coming from the same compilation (.o file) are grouped
7246 together. A future step could be to put the types in the same symtab as
7247 the CU the types ultimately came from. */
7248
7249 static hashval_t
7250 hash_type_unit_group (const void *item)
7251 {
7252 const struct type_unit_group *tu_group
7253 = (const struct type_unit_group *) item;
7254
7255 return hash_stmt_list_entry (&tu_group->hash);
7256 }
7257
7258 static int
7259 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7260 {
7261 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7262 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7263
7264 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7265 }
7266
7267 /* Allocate a hash table for type unit groups. */
7268
7269 static htab_up
7270 allocate_type_unit_groups_table ()
7271 {
7272 return htab_up (htab_create_alloc (3,
7273 hash_type_unit_group,
7274 eq_type_unit_group,
7275 NULL, xcalloc, xfree));
7276 }
7277
7278 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7279 partial symtabs. We combine several TUs per psymtab to not let the size
7280 of any one psymtab grow too big. */
7281 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7282 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7283
7284 /* Helper routine for get_type_unit_group.
7285 Create the type_unit_group object used to hold one or more TUs. */
7286
7287 static struct type_unit_group *
7288 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7289 {
7290 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7291 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
7292 struct dwarf2_per_cu_data *per_cu;
7293 struct type_unit_group *tu_group;
7294
7295 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
7296 struct type_unit_group);
7297 per_cu = &tu_group->per_cu;
7298 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7299 per_cu->per_bfd = per_bfd;
7300
7301 if (per_bfd->using_index)
7302 {
7303 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7304 struct dwarf2_per_cu_quick_data);
7305 }
7306 else
7307 {
7308 unsigned int line_offset = to_underlying (line_offset_struct);
7309 dwarf2_psymtab *pst;
7310 std::string name;
7311
7312 /* Give the symtab a useful name for debug purposes. */
7313 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7314 name = string_printf ("<type_units_%d>",
7315 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7316 else
7317 name = string_printf ("<type_units_at_0x%x>", line_offset);
7318
7319 pst = create_partial_symtab (per_cu, name.c_str ());
7320 pst->anonymous = true;
7321 }
7322
7323 tu_group->hash.dwo_unit = cu->dwo_unit;
7324 tu_group->hash.line_sect_off = line_offset_struct;
7325
7326 return tu_group;
7327 }
7328
7329 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7330 STMT_LIST is a DW_AT_stmt_list attribute. */
7331
7332 static struct type_unit_group *
7333 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7334 {
7335 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7336 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7337 struct type_unit_group *tu_group;
7338 void **slot;
7339 unsigned int line_offset;
7340 struct type_unit_group type_unit_group_for_lookup;
7341
7342 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7343 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7344
7345 /* Do we need to create a new group, or can we use an existing one? */
7346
7347 if (stmt_list)
7348 {
7349 line_offset = DW_UNSND (stmt_list);
7350 ++tu_stats->nr_symtab_sharers;
7351 }
7352 else
7353 {
7354 /* Ugh, no stmt_list. Rare, but we have to handle it.
7355 We can do various things here like create one group per TU or
7356 spread them over multiple groups to split up the expansion work.
7357 To avoid worst case scenarios (too many groups or too large groups)
7358 we, umm, group them in bunches. */
7359 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7360 | (tu_stats->nr_stmt_less_type_units
7361 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7362 ++tu_stats->nr_stmt_less_type_units;
7363 }
7364
7365 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7366 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7367 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7368 &type_unit_group_for_lookup, INSERT);
7369 if (*slot != NULL)
7370 {
7371 tu_group = (struct type_unit_group *) *slot;
7372 gdb_assert (tu_group != NULL);
7373 }
7374 else
7375 {
7376 sect_offset line_offset_struct = (sect_offset) line_offset;
7377 tu_group = create_type_unit_group (cu, line_offset_struct);
7378 *slot = tu_group;
7379 ++tu_stats->nr_symtabs;
7380 }
7381
7382 return tu_group;
7383 }
7384 \f
7385 /* Partial symbol tables. */
7386
7387 /* Create a psymtab named NAME and assign it to PER_CU.
7388
7389 The caller must fill in the following details:
7390 dirname, textlow, texthigh. */
7391
7392 static dwarf2_psymtab *
7393 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7394 {
7395 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7396 dwarf2_psymtab *pst;
7397
7398 pst = new dwarf2_psymtab (name, objfile, per_cu);
7399
7400 pst->psymtabs_addrmap_supported = true;
7401
7402 /* This is the glue that links PST into GDB's symbol API. */
7403 per_cu->v.psymtab = pst;
7404
7405 return pst;
7406 }
7407
7408 /* DIE reader function for process_psymtab_comp_unit. */
7409
7410 static void
7411 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7412 const gdb_byte *info_ptr,
7413 struct die_info *comp_unit_die,
7414 enum language pretend_language)
7415 {
7416 struct dwarf2_cu *cu = reader->cu;
7417 struct objfile *objfile = cu->per_objfile->objfile;
7418 struct gdbarch *gdbarch = objfile->arch ();
7419 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7420 CORE_ADDR baseaddr;
7421 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7422 dwarf2_psymtab *pst;
7423 enum pc_bounds_kind cu_bounds_kind;
7424 const char *filename;
7425
7426 gdb_assert (! per_cu->is_debug_types);
7427
7428 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7429
7430 /* Allocate a new partial symbol table structure. */
7431 gdb::unique_xmalloc_ptr<char> debug_filename;
7432 static const char artificial[] = "<artificial>";
7433 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7434 if (filename == NULL)
7435 filename = "";
7436 else if (strcmp (filename, artificial) == 0)
7437 {
7438 debug_filename.reset (concat (artificial, "@",
7439 sect_offset_str (per_cu->sect_off),
7440 (char *) NULL));
7441 filename = debug_filename.get ();
7442 }
7443
7444 pst = create_partial_symtab (per_cu, filename);
7445
7446 /* This must be done before calling dwarf2_build_include_psymtabs. */
7447 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7448
7449 baseaddr = objfile->text_section_offset ();
7450
7451 dwarf2_find_base_address (comp_unit_die, cu);
7452
7453 /* Possibly set the default values of LOWPC and HIGHPC from
7454 `DW_AT_ranges'. */
7455 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7456 &best_highpc, cu, pst);
7457 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7458 {
7459 CORE_ADDR low
7460 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7461 - baseaddr);
7462 CORE_ADDR high
7463 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7464 - baseaddr - 1);
7465 /* Store the contiguous range if it is not empty; it can be
7466 empty for CUs with no code. */
7467 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7468 low, high, pst);
7469 }
7470
7471 /* Check if comp unit has_children.
7472 If so, read the rest of the partial symbols from this comp unit.
7473 If not, there's no more debug_info for this comp unit. */
7474 if (comp_unit_die->has_children)
7475 {
7476 struct partial_die_info *first_die;
7477 CORE_ADDR lowpc, highpc;
7478
7479 lowpc = ((CORE_ADDR) -1);
7480 highpc = ((CORE_ADDR) 0);
7481
7482 first_die = load_partial_dies (reader, info_ptr, 1);
7483
7484 scan_partial_symbols (first_die, &lowpc, &highpc,
7485 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7486
7487 /* If we didn't find a lowpc, set it to highpc to avoid
7488 complaints from `maint check'. */
7489 if (lowpc == ((CORE_ADDR) -1))
7490 lowpc = highpc;
7491
7492 /* If the compilation unit didn't have an explicit address range,
7493 then use the information extracted from its child dies. */
7494 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7495 {
7496 best_lowpc = lowpc;
7497 best_highpc = highpc;
7498 }
7499 }
7500 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7501 best_lowpc + baseaddr)
7502 - baseaddr);
7503 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7504 best_highpc + baseaddr)
7505 - baseaddr);
7506
7507 end_psymtab_common (objfile, pst);
7508
7509 if (!cu->per_cu->imported_symtabs_empty ())
7510 {
7511 int i;
7512 int len = cu->per_cu->imported_symtabs_size ();
7513
7514 /* Fill in 'dependencies' here; we fill in 'users' in a
7515 post-pass. */
7516 pst->number_of_dependencies = len;
7517 pst->dependencies
7518 = objfile->partial_symtabs->allocate_dependencies (len);
7519 for (i = 0; i < len; ++i)
7520 {
7521 pst->dependencies[i]
7522 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7523 }
7524
7525 cu->per_cu->imported_symtabs_free ();
7526 }
7527
7528 /* Get the list of files included in the current compilation unit,
7529 and build a psymtab for each of them. */
7530 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7531
7532 if (dwarf_read_debug)
7533 fprintf_unfiltered (gdb_stdlog,
7534 "Psymtab for %s unit @%s: %s - %s"
7535 ", %d global, %d static syms\n",
7536 per_cu->is_debug_types ? "type" : "comp",
7537 sect_offset_str (per_cu->sect_off),
7538 paddress (gdbarch, pst->text_low (objfile)),
7539 paddress (gdbarch, pst->text_high (objfile)),
7540 pst->n_global_syms, pst->n_static_syms);
7541 }
7542
7543 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7544 Process compilation unit THIS_CU for a psymtab. */
7545
7546 static void
7547 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7548 bool want_partial_unit,
7549 enum language pretend_language)
7550 {
7551 /* If this compilation unit was already read in, free the
7552 cached copy in order to read it in again. This is
7553 necessary because we skipped some symbols when we first
7554 read in the compilation unit (see load_partial_dies).
7555 This problem could be avoided, but the benefit is unclear. */
7556 if (this_cu->cu != NULL)
7557 free_one_cached_comp_unit (this_cu);
7558
7559 cutu_reader reader (this_cu, NULL, 0, false);
7560
7561 switch (reader.comp_unit_die->tag)
7562 {
7563 case DW_TAG_compile_unit:
7564 this_cu->unit_type = DW_UT_compile;
7565 break;
7566 case DW_TAG_partial_unit:
7567 this_cu->unit_type = DW_UT_partial;
7568 break;
7569 default:
7570 abort ();
7571 }
7572
7573 if (reader.dummy_p)
7574 {
7575 /* Nothing. */
7576 }
7577 else if (this_cu->is_debug_types)
7578 build_type_psymtabs_reader (&reader, reader.info_ptr,
7579 reader.comp_unit_die);
7580 else if (want_partial_unit
7581 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7582 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7583 reader.comp_unit_die,
7584 pretend_language);
7585
7586 this_cu->lang = this_cu->cu->language;
7587
7588 /* Age out any secondary CUs. */
7589 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7590 }
7591
7592 /* Reader function for build_type_psymtabs. */
7593
7594 static void
7595 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7596 const gdb_byte *info_ptr,
7597 struct die_info *type_unit_die)
7598 {
7599 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
7600 struct objfile *objfile = dwarf2_per_objfile->objfile;
7601 struct dwarf2_cu *cu = reader->cu;
7602 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7603 struct signatured_type *sig_type;
7604 struct type_unit_group *tu_group;
7605 struct attribute *attr;
7606 struct partial_die_info *first_die;
7607 CORE_ADDR lowpc, highpc;
7608 dwarf2_psymtab *pst;
7609
7610 gdb_assert (per_cu->is_debug_types);
7611 sig_type = (struct signatured_type *) per_cu;
7612
7613 if (! type_unit_die->has_children)
7614 return;
7615
7616 attr = type_unit_die->attr (DW_AT_stmt_list);
7617 tu_group = get_type_unit_group (cu, attr);
7618
7619 if (tu_group->tus == nullptr)
7620 tu_group->tus = new std::vector<signatured_type *>;
7621 tu_group->tus->push_back (sig_type);
7622
7623 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7624 pst = create_partial_symtab (per_cu, "");
7625 pst->anonymous = true;
7626
7627 first_die = load_partial_dies (reader, info_ptr, 1);
7628
7629 lowpc = (CORE_ADDR) -1;
7630 highpc = (CORE_ADDR) 0;
7631 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7632
7633 end_psymtab_common (objfile, pst);
7634 }
7635
7636 /* Struct used to sort TUs by their abbreviation table offset. */
7637
7638 struct tu_abbrev_offset
7639 {
7640 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7641 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7642 {}
7643
7644 signatured_type *sig_type;
7645 sect_offset abbrev_offset;
7646 };
7647
7648 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7649
7650 static bool
7651 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7652 const struct tu_abbrev_offset &b)
7653 {
7654 return a.abbrev_offset < b.abbrev_offset;
7655 }
7656
7657 /* Efficiently read all the type units.
7658 This does the bulk of the work for build_type_psymtabs.
7659
7660 The efficiency is because we sort TUs by the abbrev table they use and
7661 only read each abbrev table once. In one program there are 200K TUs
7662 sharing 8K abbrev tables.
7663
7664 The main purpose of this function is to support building the
7665 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7666 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7667 can collapse the search space by grouping them by stmt_list.
7668 The savings can be significant, in the same program from above the 200K TUs
7669 share 8K stmt_list tables.
7670
7671 FUNC is expected to call get_type_unit_group, which will create the
7672 struct type_unit_group if necessary and add it to
7673 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7674
7675 static void
7676 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7677 {
7678 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7679 abbrev_table_up abbrev_table;
7680 sect_offset abbrev_offset;
7681
7682 /* It's up to the caller to not call us multiple times. */
7683 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
7684
7685 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
7686 return;
7687
7688 /* TUs typically share abbrev tables, and there can be way more TUs than
7689 abbrev tables. Sort by abbrev table to reduce the number of times we
7690 read each abbrev table in.
7691 Alternatives are to punt or to maintain a cache of abbrev tables.
7692 This is simpler and efficient enough for now.
7693
7694 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7695 symtab to use). Typically TUs with the same abbrev offset have the same
7696 stmt_list value too so in practice this should work well.
7697
7698 The basic algorithm here is:
7699
7700 sort TUs by abbrev table
7701 for each TU with same abbrev table:
7702 read abbrev table if first user
7703 read TU top level DIE
7704 [IWBN if DWO skeletons had DW_AT_stmt_list]
7705 call FUNC */
7706
7707 if (dwarf_read_debug)
7708 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7709
7710 /* Sort in a separate table to maintain the order of all_type_units
7711 for .gdb_index: TU indices directly index all_type_units. */
7712 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7713 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
7714
7715 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
7716 sorted_by_abbrev.emplace_back
7717 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7718 sig_type->per_cu.section,
7719 sig_type->per_cu.sect_off));
7720
7721 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7722 sort_tu_by_abbrev_offset);
7723
7724 abbrev_offset = (sect_offset) ~(unsigned) 0;
7725
7726 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7727 {
7728 /* Switch to the next abbrev table if necessary. */
7729 if (abbrev_table == NULL
7730 || tu.abbrev_offset != abbrev_offset)
7731 {
7732 abbrev_offset = tu.abbrev_offset;
7733 abbrev_table =
7734 abbrev_table::read (dwarf2_per_objfile->objfile,
7735 &dwarf2_per_objfile->per_bfd->abbrev,
7736 abbrev_offset);
7737 ++tu_stats->nr_uniq_abbrev_tables;
7738 }
7739
7740 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7741 0, false);
7742 if (!reader.dummy_p)
7743 build_type_psymtabs_reader (&reader, reader.info_ptr,
7744 reader.comp_unit_die);
7745 }
7746 }
7747
7748 /* Print collected type unit statistics. */
7749
7750 static void
7751 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7752 {
7753 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7754
7755 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7756 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7757 dwarf2_per_objfile->per_bfd->all_type_units.size ());
7758 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7759 tu_stats->nr_uniq_abbrev_tables);
7760 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7761 tu_stats->nr_symtabs);
7762 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7763 tu_stats->nr_symtab_sharers);
7764 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7765 tu_stats->nr_stmt_less_type_units);
7766 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7767 tu_stats->nr_all_type_units_reallocs);
7768 }
7769
7770 /* Traversal function for build_type_psymtabs. */
7771
7772 static int
7773 build_type_psymtab_dependencies (void **slot, void *info)
7774 {
7775 struct dwarf2_per_objfile *dwarf2_per_objfile
7776 = (struct dwarf2_per_objfile *) info;
7777 struct objfile *objfile = dwarf2_per_objfile->objfile;
7778 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7779 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7780 dwarf2_psymtab *pst = per_cu->v.psymtab;
7781 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7782 int i;
7783
7784 gdb_assert (len > 0);
7785 gdb_assert (per_cu->type_unit_group_p ());
7786
7787 pst->number_of_dependencies = len;
7788 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7789 for (i = 0; i < len; ++i)
7790 {
7791 struct signatured_type *iter = tu_group->tus->at (i);
7792 gdb_assert (iter->per_cu.is_debug_types);
7793 pst->dependencies[i] = iter->per_cu.v.psymtab;
7794 iter->type_unit_group = tu_group;
7795 }
7796
7797 delete tu_group->tus;
7798 tu_group->tus = nullptr;
7799
7800 return 1;
7801 }
7802
7803 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7804 Build partial symbol tables for the .debug_types comp-units. */
7805
7806 static void
7807 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7808 {
7809 if (! create_all_type_units (dwarf2_per_objfile))
7810 return;
7811
7812 build_type_psymtabs_1 (dwarf2_per_objfile);
7813 }
7814
7815 /* Traversal function for process_skeletonless_type_unit.
7816 Read a TU in a DWO file and build partial symbols for it. */
7817
7818 static int
7819 process_skeletonless_type_unit (void **slot, void *info)
7820 {
7821 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7822 struct dwarf2_per_objfile *dwarf2_per_objfile
7823 = (struct dwarf2_per_objfile *) info;
7824 struct signatured_type find_entry, *entry;
7825
7826 /* If this TU doesn't exist in the global table, add it and read it in. */
7827
7828 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7829 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7830
7831 find_entry.signature = dwo_unit->signature;
7832 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
7833 &find_entry, INSERT);
7834 /* If we've already seen this type there's nothing to do. What's happening
7835 is we're doing our own version of comdat-folding here. */
7836 if (*slot != NULL)
7837 return 1;
7838
7839 /* This does the job that create_all_type_units would have done for
7840 this TU. */
7841 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7842 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7843 *slot = entry;
7844
7845 /* This does the job that build_type_psymtabs_1 would have done. */
7846 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7847 if (!reader.dummy_p)
7848 build_type_psymtabs_reader (&reader, reader.info_ptr,
7849 reader.comp_unit_die);
7850
7851 return 1;
7852 }
7853
7854 /* Traversal function for process_skeletonless_type_units. */
7855
7856 static int
7857 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7858 {
7859 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7860
7861 if (dwo_file->tus != NULL)
7862 htab_traverse_noresize (dwo_file->tus.get (),
7863 process_skeletonless_type_unit, info);
7864
7865 return 1;
7866 }
7867
7868 /* Scan all TUs of DWO files, verifying we've processed them.
7869 This is needed in case a TU was emitted without its skeleton.
7870 Note: This can't be done until we know what all the DWO files are. */
7871
7872 static void
7873 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7874 {
7875 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7876 if (get_dwp_file (dwarf2_per_objfile) == NULL
7877 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
7878 {
7879 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
7880 process_dwo_file_for_skeletonless_type_units,
7881 dwarf2_per_objfile);
7882 }
7883 }
7884
7885 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7886
7887 static void
7888 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7889 {
7890 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7891 {
7892 dwarf2_psymtab *pst = per_cu->v.psymtab;
7893
7894 if (pst == NULL)
7895 continue;
7896
7897 for (int j = 0; j < pst->number_of_dependencies; ++j)
7898 {
7899 /* Set the 'user' field only if it is not already set. */
7900 if (pst->dependencies[j]->user == NULL)
7901 pst->dependencies[j]->user = pst;
7902 }
7903 }
7904 }
7905
7906 /* Build the partial symbol table by doing a quick pass through the
7907 .debug_info and .debug_abbrev sections. */
7908
7909 static void
7910 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7911 {
7912 struct objfile *objfile = dwarf2_per_objfile->objfile;
7913
7914 if (dwarf_read_debug)
7915 {
7916 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7917 objfile_name (objfile));
7918 }
7919
7920 scoped_restore restore_reading_psyms
7921 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
7922 true);
7923
7924 dwarf2_per_objfile->per_bfd->info.read (objfile);
7925
7926 /* Any cached compilation units will be linked by the per-objfile
7927 read_in_chain. Make sure to free them when we're done. */
7928 free_cached_comp_units freer (dwarf2_per_objfile);
7929
7930 build_type_psymtabs (dwarf2_per_objfile);
7931
7932 create_all_comp_units (dwarf2_per_objfile);
7933
7934 /* Create a temporary address map on a temporary obstack. We later
7935 copy this to the final obstack. */
7936 auto_obstack temp_obstack;
7937
7938 scoped_restore save_psymtabs_addrmap
7939 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7940 addrmap_create_mutable (&temp_obstack));
7941
7942 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7943 {
7944 if (per_cu->v.psymtab != NULL)
7945 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7946 continue;
7947 process_psymtab_comp_unit (per_cu, false, language_minimal);
7948 }
7949
7950 /* This has to wait until we read the CUs, we need the list of DWOs. */
7951 process_skeletonless_type_units (dwarf2_per_objfile);
7952
7953 /* Now that all TUs have been processed we can fill in the dependencies. */
7954 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
7955 {
7956 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7957 build_type_psymtab_dependencies, dwarf2_per_objfile);
7958 }
7959
7960 if (dwarf_read_debug)
7961 print_tu_stats (dwarf2_per_objfile);
7962
7963 set_partial_user (dwarf2_per_objfile);
7964
7965 objfile->partial_symtabs->psymtabs_addrmap
7966 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7967 objfile->partial_symtabs->obstack ());
7968 /* At this point we want to keep the address map. */
7969 save_psymtabs_addrmap.release ();
7970
7971 if (dwarf_read_debug)
7972 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7973 objfile_name (objfile));
7974 }
7975
7976 /* Load the partial DIEs for a secondary CU into memory.
7977 This is also used when rereading a primary CU with load_all_dies. */
7978
7979 static void
7980 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7981 {
7982 cutu_reader reader (this_cu, NULL, 1, false);
7983
7984 if (!reader.dummy_p)
7985 {
7986 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7987 language_minimal);
7988
7989 /* Check if comp unit has_children.
7990 If so, read the rest of the partial symbols from this comp unit.
7991 If not, there's no more debug_info for this comp unit. */
7992 if (reader.comp_unit_die->has_children)
7993 load_partial_dies (&reader, reader.info_ptr, 0);
7994
7995 reader.keep ();
7996 }
7997 }
7998
7999 static void
8000 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8001 struct dwarf2_section_info *section,
8002 struct dwarf2_section_info *abbrev_section,
8003 unsigned int is_dwz)
8004 {
8005 const gdb_byte *info_ptr;
8006 struct objfile *objfile = dwarf2_per_objfile->objfile;
8007
8008 if (dwarf_read_debug)
8009 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8010 section->get_name (),
8011 section->get_file_name ());
8012
8013 section->read (objfile);
8014
8015 info_ptr = section->buffer;
8016
8017 while (info_ptr < section->buffer + section->size)
8018 {
8019 struct dwarf2_per_cu_data *this_cu;
8020
8021 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8022
8023 comp_unit_head cu_header;
8024 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8025 abbrev_section, info_ptr,
8026 rcuh_kind::COMPILE);
8027
8028 /* Save the compilation unit for later lookup. */
8029 if (cu_header.unit_type != DW_UT_type)
8030 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
8031 else
8032 {
8033 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
8034 sig_type->signature = cu_header.signature;
8035 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8036 this_cu = &sig_type->per_cu;
8037 }
8038 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8039 this_cu->sect_off = sect_off;
8040 this_cu->length = cu_header.length + cu_header.initial_length_size;
8041 this_cu->is_dwz = is_dwz;
8042 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8043 this_cu->section = section;
8044
8045 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8046
8047 info_ptr = info_ptr + this_cu->length;
8048 }
8049 }
8050
8051 /* Create a list of all compilation units in OBJFILE.
8052 This is only done for -readnow and building partial symtabs. */
8053
8054 static void
8055 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8056 {
8057 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8058 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8059 &dwarf2_per_objfile->per_bfd->abbrev, 0);
8060
8061 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
8062 if (dwz != NULL)
8063 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8064 1);
8065 }
8066
8067 /* Process all loaded DIEs for compilation unit CU, starting at
8068 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8069 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8070 DW_AT_ranges). See the comments of add_partial_subprogram on how
8071 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8072
8073 static void
8074 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8075 CORE_ADDR *highpc, int set_addrmap,
8076 struct dwarf2_cu *cu)
8077 {
8078 struct partial_die_info *pdi;
8079
8080 /* Now, march along the PDI's, descending into ones which have
8081 interesting children but skipping the children of the other ones,
8082 until we reach the end of the compilation unit. */
8083
8084 pdi = first_die;
8085
8086 while (pdi != NULL)
8087 {
8088 pdi->fixup (cu);
8089
8090 /* Anonymous namespaces or modules have no name but have interesting
8091 children, so we need to look at them. Ditto for anonymous
8092 enums. */
8093
8094 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8095 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8096 || pdi->tag == DW_TAG_imported_unit
8097 || pdi->tag == DW_TAG_inlined_subroutine)
8098 {
8099 switch (pdi->tag)
8100 {
8101 case DW_TAG_subprogram:
8102 case DW_TAG_inlined_subroutine:
8103 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8104 break;
8105 case DW_TAG_constant:
8106 case DW_TAG_variable:
8107 case DW_TAG_typedef:
8108 case DW_TAG_union_type:
8109 if (!pdi->is_declaration
8110 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8111 {
8112 add_partial_symbol (pdi, cu);
8113 }
8114 break;
8115 case DW_TAG_class_type:
8116 case DW_TAG_interface_type:
8117 case DW_TAG_structure_type:
8118 if (!pdi->is_declaration)
8119 {
8120 add_partial_symbol (pdi, cu);
8121 }
8122 if ((cu->language == language_rust
8123 || cu->language == language_cplus) && pdi->has_children)
8124 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8125 set_addrmap, cu);
8126 break;
8127 case DW_TAG_enumeration_type:
8128 if (!pdi->is_declaration)
8129 add_partial_enumeration (pdi, cu);
8130 break;
8131 case DW_TAG_base_type:
8132 case DW_TAG_subrange_type:
8133 /* File scope base type definitions are added to the partial
8134 symbol table. */
8135 add_partial_symbol (pdi, cu);
8136 break;
8137 case DW_TAG_namespace:
8138 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8139 break;
8140 case DW_TAG_module:
8141 if (!pdi->is_declaration)
8142 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8143 break;
8144 case DW_TAG_imported_unit:
8145 {
8146 struct dwarf2_per_cu_data *per_cu;
8147
8148 /* For now we don't handle imported units in type units. */
8149 if (cu->per_cu->is_debug_types)
8150 {
8151 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8152 " supported in type units [in module %s]"),
8153 objfile_name (cu->per_objfile->objfile));
8154 }
8155
8156 per_cu = dwarf2_find_containing_comp_unit
8157 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8158
8159 /* Go read the partial unit, if needed. */
8160 if (per_cu->v.psymtab == NULL)
8161 process_psymtab_comp_unit (per_cu, true, cu->language);
8162
8163 cu->per_cu->imported_symtabs_push (per_cu);
8164 }
8165 break;
8166 case DW_TAG_imported_declaration:
8167 add_partial_symbol (pdi, cu);
8168 break;
8169 default:
8170 break;
8171 }
8172 }
8173
8174 /* If the die has a sibling, skip to the sibling. */
8175
8176 pdi = pdi->die_sibling;
8177 }
8178 }
8179
8180 /* Functions used to compute the fully scoped name of a partial DIE.
8181
8182 Normally, this is simple. For C++, the parent DIE's fully scoped
8183 name is concatenated with "::" and the partial DIE's name.
8184 Enumerators are an exception; they use the scope of their parent
8185 enumeration type, i.e. the name of the enumeration type is not
8186 prepended to the enumerator.
8187
8188 There are two complexities. One is DW_AT_specification; in this
8189 case "parent" means the parent of the target of the specification,
8190 instead of the direct parent of the DIE. The other is compilers
8191 which do not emit DW_TAG_namespace; in this case we try to guess
8192 the fully qualified name of structure types from their members'
8193 linkage names. This must be done using the DIE's children rather
8194 than the children of any DW_AT_specification target. We only need
8195 to do this for structures at the top level, i.e. if the target of
8196 any DW_AT_specification (if any; otherwise the DIE itself) does not
8197 have a parent. */
8198
8199 /* Compute the scope prefix associated with PDI's parent, in
8200 compilation unit CU. The result will be allocated on CU's
8201 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8202 field. NULL is returned if no prefix is necessary. */
8203 static const char *
8204 partial_die_parent_scope (struct partial_die_info *pdi,
8205 struct dwarf2_cu *cu)
8206 {
8207 const char *grandparent_scope;
8208 struct partial_die_info *parent, *real_pdi;
8209
8210 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8211 then this means the parent of the specification DIE. */
8212
8213 real_pdi = pdi;
8214 while (real_pdi->has_specification)
8215 {
8216 auto res = find_partial_die (real_pdi->spec_offset,
8217 real_pdi->spec_is_dwz, cu);
8218 real_pdi = res.pdi;
8219 cu = res.cu;
8220 }
8221
8222 parent = real_pdi->die_parent;
8223 if (parent == NULL)
8224 return NULL;
8225
8226 if (parent->scope_set)
8227 return parent->scope;
8228
8229 parent->fixup (cu);
8230
8231 grandparent_scope = partial_die_parent_scope (parent, cu);
8232
8233 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8234 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8235 Work around this problem here. */
8236 if (cu->language == language_cplus
8237 && parent->tag == DW_TAG_namespace
8238 && strcmp (parent->name, "::") == 0
8239 && grandparent_scope == NULL)
8240 {
8241 parent->scope = NULL;
8242 parent->scope_set = 1;
8243 return NULL;
8244 }
8245
8246 /* Nested subroutines in Fortran get a prefix. */
8247 if (pdi->tag == DW_TAG_enumerator)
8248 /* Enumerators should not get the name of the enumeration as a prefix. */
8249 parent->scope = grandparent_scope;
8250 else if (parent->tag == DW_TAG_namespace
8251 || parent->tag == DW_TAG_module
8252 || parent->tag == DW_TAG_structure_type
8253 || parent->tag == DW_TAG_class_type
8254 || parent->tag == DW_TAG_interface_type
8255 || parent->tag == DW_TAG_union_type
8256 || parent->tag == DW_TAG_enumeration_type
8257 || (cu->language == language_fortran
8258 && parent->tag == DW_TAG_subprogram
8259 && pdi->tag == DW_TAG_subprogram))
8260 {
8261 if (grandparent_scope == NULL)
8262 parent->scope = parent->name;
8263 else
8264 parent->scope = typename_concat (&cu->comp_unit_obstack,
8265 grandparent_scope,
8266 parent->name, 0, cu);
8267 }
8268 else
8269 {
8270 /* FIXME drow/2004-04-01: What should we be doing with
8271 function-local names? For partial symbols, we should probably be
8272 ignoring them. */
8273 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8274 dwarf_tag_name (parent->tag),
8275 sect_offset_str (pdi->sect_off));
8276 parent->scope = grandparent_scope;
8277 }
8278
8279 parent->scope_set = 1;
8280 return parent->scope;
8281 }
8282
8283 /* Return the fully scoped name associated with PDI, from compilation unit
8284 CU. The result will be allocated with malloc. */
8285
8286 static gdb::unique_xmalloc_ptr<char>
8287 partial_die_full_name (struct partial_die_info *pdi,
8288 struct dwarf2_cu *cu)
8289 {
8290 const char *parent_scope;
8291
8292 /* If this is a template instantiation, we can not work out the
8293 template arguments from partial DIEs. So, unfortunately, we have
8294 to go through the full DIEs. At least any work we do building
8295 types here will be reused if full symbols are loaded later. */
8296 if (pdi->has_template_arguments)
8297 {
8298 pdi->fixup (cu);
8299
8300 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8301 {
8302 struct die_info *die;
8303 struct attribute attr;
8304 struct dwarf2_cu *ref_cu = cu;
8305
8306 /* DW_FORM_ref_addr is using section offset. */
8307 attr.name = (enum dwarf_attribute) 0;
8308 attr.form = DW_FORM_ref_addr;
8309 attr.u.unsnd = to_underlying (pdi->sect_off);
8310 die = follow_die_ref (NULL, &attr, &ref_cu);
8311
8312 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8313 }
8314 }
8315
8316 parent_scope = partial_die_parent_scope (pdi, cu);
8317 if (parent_scope == NULL)
8318 return NULL;
8319 else
8320 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8321 pdi->name, 0, cu));
8322 }
8323
8324 static void
8325 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8326 {
8327 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
8328 struct objfile *objfile = dwarf2_per_objfile->objfile;
8329 struct gdbarch *gdbarch = objfile->arch ();
8330 CORE_ADDR addr = 0;
8331 const char *actual_name = NULL;
8332 CORE_ADDR baseaddr;
8333
8334 baseaddr = objfile->text_section_offset ();
8335
8336 gdb::unique_xmalloc_ptr<char> built_actual_name
8337 = partial_die_full_name (pdi, cu);
8338 if (built_actual_name != NULL)
8339 actual_name = built_actual_name.get ();
8340
8341 if (actual_name == NULL)
8342 actual_name = pdi->name;
8343
8344 partial_symbol psymbol;
8345 memset (&psymbol, 0, sizeof (psymbol));
8346 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8347 psymbol.ginfo.section = -1;
8348
8349 /* The code below indicates that the psymbol should be installed by
8350 setting this. */
8351 gdb::optional<psymbol_placement> where;
8352
8353 switch (pdi->tag)
8354 {
8355 case DW_TAG_inlined_subroutine:
8356 case DW_TAG_subprogram:
8357 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8358 - baseaddr);
8359 if (pdi->is_external
8360 || cu->language == language_ada
8361 || (cu->language == language_fortran
8362 && pdi->die_parent != NULL
8363 && pdi->die_parent->tag == DW_TAG_subprogram))
8364 {
8365 /* Normally, only "external" DIEs are part of the global scope.
8366 But in Ada and Fortran, we want to be able to access nested
8367 procedures globally. So all Ada and Fortran subprograms are
8368 stored in the global scope. */
8369 where = psymbol_placement::GLOBAL;
8370 }
8371 else
8372 where = psymbol_placement::STATIC;
8373
8374 psymbol.domain = VAR_DOMAIN;
8375 psymbol.aclass = LOC_BLOCK;
8376 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8377 psymbol.ginfo.value.address = addr;
8378
8379 if (pdi->main_subprogram && actual_name != NULL)
8380 set_objfile_main_name (objfile, actual_name, cu->language);
8381 break;
8382 case DW_TAG_constant:
8383 psymbol.domain = VAR_DOMAIN;
8384 psymbol.aclass = LOC_STATIC;
8385 where = (pdi->is_external
8386 ? psymbol_placement::GLOBAL
8387 : psymbol_placement::STATIC);
8388 break;
8389 case DW_TAG_variable:
8390 if (pdi->d.locdesc)
8391 addr = decode_locdesc (pdi->d.locdesc, cu);
8392
8393 if (pdi->d.locdesc
8394 && addr == 0
8395 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
8396 {
8397 /* A global or static variable may also have been stripped
8398 out by the linker if unused, in which case its address
8399 will be nullified; do not add such variables into partial
8400 symbol table then. */
8401 }
8402 else if (pdi->is_external)
8403 {
8404 /* Global Variable.
8405 Don't enter into the minimal symbol tables as there is
8406 a minimal symbol table entry from the ELF symbols already.
8407 Enter into partial symbol table if it has a location
8408 descriptor or a type.
8409 If the location descriptor is missing, new_symbol will create
8410 a LOC_UNRESOLVED symbol, the address of the variable will then
8411 be determined from the minimal symbol table whenever the variable
8412 is referenced.
8413 The address for the partial symbol table entry is not
8414 used by GDB, but it comes in handy for debugging partial symbol
8415 table building. */
8416
8417 if (pdi->d.locdesc || pdi->has_type)
8418 {
8419 psymbol.domain = VAR_DOMAIN;
8420 psymbol.aclass = LOC_STATIC;
8421 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8422 psymbol.ginfo.value.address = addr;
8423 where = psymbol_placement::GLOBAL;
8424 }
8425 }
8426 else
8427 {
8428 int has_loc = pdi->d.locdesc != NULL;
8429
8430 /* Static Variable. Skip symbols whose value we cannot know (those
8431 without location descriptors or constant values). */
8432 if (!has_loc && !pdi->has_const_value)
8433 return;
8434
8435 psymbol.domain = VAR_DOMAIN;
8436 psymbol.aclass = LOC_STATIC;
8437 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8438 if (has_loc)
8439 psymbol.ginfo.value.address = addr;
8440 where = psymbol_placement::STATIC;
8441 }
8442 break;
8443 case DW_TAG_typedef:
8444 case DW_TAG_base_type:
8445 case DW_TAG_subrange_type:
8446 psymbol.domain = VAR_DOMAIN;
8447 psymbol.aclass = LOC_TYPEDEF;
8448 where = psymbol_placement::STATIC;
8449 break;
8450 case DW_TAG_imported_declaration:
8451 case DW_TAG_namespace:
8452 psymbol.domain = VAR_DOMAIN;
8453 psymbol.aclass = LOC_TYPEDEF;
8454 where = psymbol_placement::GLOBAL;
8455 break;
8456 case DW_TAG_module:
8457 /* With Fortran 77 there might be a "BLOCK DATA" module
8458 available without any name. If so, we skip the module as it
8459 doesn't bring any value. */
8460 if (actual_name != nullptr)
8461 {
8462 psymbol.domain = MODULE_DOMAIN;
8463 psymbol.aclass = LOC_TYPEDEF;
8464 where = psymbol_placement::GLOBAL;
8465 }
8466 break;
8467 case DW_TAG_class_type:
8468 case DW_TAG_interface_type:
8469 case DW_TAG_structure_type:
8470 case DW_TAG_union_type:
8471 case DW_TAG_enumeration_type:
8472 /* Skip external references. The DWARF standard says in the section
8473 about "Structure, Union, and Class Type Entries": "An incomplete
8474 structure, union or class type is represented by a structure,
8475 union or class entry that does not have a byte size attribute
8476 and that has a DW_AT_declaration attribute." */
8477 if (!pdi->has_byte_size && pdi->is_declaration)
8478 return;
8479
8480 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8481 static vs. global. */
8482 psymbol.domain = STRUCT_DOMAIN;
8483 psymbol.aclass = LOC_TYPEDEF;
8484 where = (cu->language == language_cplus
8485 ? psymbol_placement::GLOBAL
8486 : psymbol_placement::STATIC);
8487 break;
8488 case DW_TAG_enumerator:
8489 psymbol.domain = VAR_DOMAIN;
8490 psymbol.aclass = LOC_CONST;
8491 where = (cu->language == language_cplus
8492 ? psymbol_placement::GLOBAL
8493 : psymbol_placement::STATIC);
8494 break;
8495 default:
8496 break;
8497 }
8498
8499 if (where.has_value ())
8500 {
8501 if (built_actual_name != nullptr)
8502 actual_name = objfile->intern (actual_name);
8503 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8504 psymbol.ginfo.set_linkage_name (actual_name);
8505 else
8506 {
8507 psymbol.ginfo.set_demangled_name (actual_name,
8508 &objfile->objfile_obstack);
8509 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8510 }
8511 add_psymbol_to_list (psymbol, *where, objfile);
8512 }
8513 }
8514
8515 /* Read a partial die corresponding to a namespace; also, add a symbol
8516 corresponding to that namespace to the symbol table. NAMESPACE is
8517 the name of the enclosing namespace. */
8518
8519 static void
8520 add_partial_namespace (struct partial_die_info *pdi,
8521 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8522 int set_addrmap, struct dwarf2_cu *cu)
8523 {
8524 /* Add a symbol for the namespace. */
8525
8526 add_partial_symbol (pdi, cu);
8527
8528 /* Now scan partial symbols in that namespace. */
8529
8530 if (pdi->has_children)
8531 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8532 }
8533
8534 /* Read a partial die corresponding to a Fortran module. */
8535
8536 static void
8537 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8538 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8539 {
8540 /* Add a symbol for the namespace. */
8541
8542 add_partial_symbol (pdi, cu);
8543
8544 /* Now scan partial symbols in that module. */
8545
8546 if (pdi->has_children)
8547 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8548 }
8549
8550 /* Read a partial die corresponding to a subprogram or an inlined
8551 subprogram and create a partial symbol for that subprogram.
8552 When the CU language allows it, this routine also defines a partial
8553 symbol for each nested subprogram that this subprogram contains.
8554 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8555 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8556
8557 PDI may also be a lexical block, in which case we simply search
8558 recursively for subprograms defined inside that lexical block.
8559 Again, this is only performed when the CU language allows this
8560 type of definitions. */
8561
8562 static void
8563 add_partial_subprogram (struct partial_die_info *pdi,
8564 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8565 int set_addrmap, struct dwarf2_cu *cu)
8566 {
8567 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8568 {
8569 if (pdi->has_pc_info)
8570 {
8571 if (pdi->lowpc < *lowpc)
8572 *lowpc = pdi->lowpc;
8573 if (pdi->highpc > *highpc)
8574 *highpc = pdi->highpc;
8575 if (set_addrmap)
8576 {
8577 struct objfile *objfile = cu->per_objfile->objfile;
8578 struct gdbarch *gdbarch = objfile->arch ();
8579 CORE_ADDR baseaddr;
8580 CORE_ADDR this_highpc;
8581 CORE_ADDR this_lowpc;
8582
8583 baseaddr = objfile->text_section_offset ();
8584 this_lowpc
8585 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8586 pdi->lowpc + baseaddr)
8587 - baseaddr);
8588 this_highpc
8589 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8590 pdi->highpc + baseaddr)
8591 - baseaddr);
8592 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8593 this_lowpc, this_highpc - 1,
8594 cu->per_cu->v.psymtab);
8595 }
8596 }
8597
8598 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8599 {
8600 if (!pdi->is_declaration)
8601 /* Ignore subprogram DIEs that do not have a name, they are
8602 illegal. Do not emit a complaint at this point, we will
8603 do so when we convert this psymtab into a symtab. */
8604 if (pdi->name)
8605 add_partial_symbol (pdi, cu);
8606 }
8607 }
8608
8609 if (! pdi->has_children)
8610 return;
8611
8612 if (cu->language == language_ada || cu->language == language_fortran)
8613 {
8614 pdi = pdi->die_child;
8615 while (pdi != NULL)
8616 {
8617 pdi->fixup (cu);
8618 if (pdi->tag == DW_TAG_subprogram
8619 || pdi->tag == DW_TAG_inlined_subroutine
8620 || pdi->tag == DW_TAG_lexical_block)
8621 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8622 pdi = pdi->die_sibling;
8623 }
8624 }
8625 }
8626
8627 /* Read a partial die corresponding to an enumeration type. */
8628
8629 static void
8630 add_partial_enumeration (struct partial_die_info *enum_pdi,
8631 struct dwarf2_cu *cu)
8632 {
8633 struct partial_die_info *pdi;
8634
8635 if (enum_pdi->name != NULL)
8636 add_partial_symbol (enum_pdi, cu);
8637
8638 pdi = enum_pdi->die_child;
8639 while (pdi)
8640 {
8641 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8642 complaint (_("malformed enumerator DIE ignored"));
8643 else
8644 add_partial_symbol (pdi, cu);
8645 pdi = pdi->die_sibling;
8646 }
8647 }
8648
8649 /* Return the initial uleb128 in the die at INFO_PTR. */
8650
8651 static unsigned int
8652 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8653 {
8654 unsigned int bytes_read;
8655
8656 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8657 }
8658
8659 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8660 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8661
8662 Return the corresponding abbrev, or NULL if the number is zero (indicating
8663 an empty DIE). In either case *BYTES_READ will be set to the length of
8664 the initial number. */
8665
8666 static struct abbrev_info *
8667 peek_die_abbrev (const die_reader_specs &reader,
8668 const gdb_byte *info_ptr, unsigned int *bytes_read)
8669 {
8670 dwarf2_cu *cu = reader.cu;
8671 bfd *abfd = cu->per_objfile->objfile->obfd;
8672 unsigned int abbrev_number
8673 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8674
8675 if (abbrev_number == 0)
8676 return NULL;
8677
8678 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8679 if (!abbrev)
8680 {
8681 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8682 " at offset %s [in module %s]"),
8683 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8684 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8685 }
8686
8687 return abbrev;
8688 }
8689
8690 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8691 Returns a pointer to the end of a series of DIEs, terminated by an empty
8692 DIE. Any children of the skipped DIEs will also be skipped. */
8693
8694 static const gdb_byte *
8695 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8696 {
8697 while (1)
8698 {
8699 unsigned int bytes_read;
8700 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8701
8702 if (abbrev == NULL)
8703 return info_ptr + bytes_read;
8704 else
8705 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8706 }
8707 }
8708
8709 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8710 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8711 abbrev corresponding to that skipped uleb128 should be passed in
8712 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8713 children. */
8714
8715 static const gdb_byte *
8716 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8717 struct abbrev_info *abbrev)
8718 {
8719 unsigned int bytes_read;
8720 struct attribute attr;
8721 bfd *abfd = reader->abfd;
8722 struct dwarf2_cu *cu = reader->cu;
8723 const gdb_byte *buffer = reader->buffer;
8724 const gdb_byte *buffer_end = reader->buffer_end;
8725 unsigned int form, i;
8726
8727 for (i = 0; i < abbrev->num_attrs; i++)
8728 {
8729 /* The only abbrev we care about is DW_AT_sibling. */
8730 if (abbrev->attrs[i].name == DW_AT_sibling)
8731 {
8732 bool ignored;
8733 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8734 &ignored);
8735 if (attr.form == DW_FORM_ref_addr)
8736 complaint (_("ignoring absolute DW_AT_sibling"));
8737 else
8738 {
8739 sect_offset off = attr.get_ref_die_offset ();
8740 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8741
8742 if (sibling_ptr < info_ptr)
8743 complaint (_("DW_AT_sibling points backwards"));
8744 else if (sibling_ptr > reader->buffer_end)
8745 reader->die_section->overflow_complaint ();
8746 else
8747 return sibling_ptr;
8748 }
8749 }
8750
8751 /* If it isn't DW_AT_sibling, skip this attribute. */
8752 form = abbrev->attrs[i].form;
8753 skip_attribute:
8754 switch (form)
8755 {
8756 case DW_FORM_ref_addr:
8757 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8758 and later it is offset sized. */
8759 if (cu->header.version == 2)
8760 info_ptr += cu->header.addr_size;
8761 else
8762 info_ptr += cu->header.offset_size;
8763 break;
8764 case DW_FORM_GNU_ref_alt:
8765 info_ptr += cu->header.offset_size;
8766 break;
8767 case DW_FORM_addr:
8768 info_ptr += cu->header.addr_size;
8769 break;
8770 case DW_FORM_data1:
8771 case DW_FORM_ref1:
8772 case DW_FORM_flag:
8773 case DW_FORM_strx1:
8774 info_ptr += 1;
8775 break;
8776 case DW_FORM_flag_present:
8777 case DW_FORM_implicit_const:
8778 break;
8779 case DW_FORM_data2:
8780 case DW_FORM_ref2:
8781 case DW_FORM_strx2:
8782 info_ptr += 2;
8783 break;
8784 case DW_FORM_strx3:
8785 info_ptr += 3;
8786 break;
8787 case DW_FORM_data4:
8788 case DW_FORM_ref4:
8789 case DW_FORM_strx4:
8790 info_ptr += 4;
8791 break;
8792 case DW_FORM_data8:
8793 case DW_FORM_ref8:
8794 case DW_FORM_ref_sig8:
8795 info_ptr += 8;
8796 break;
8797 case DW_FORM_data16:
8798 info_ptr += 16;
8799 break;
8800 case DW_FORM_string:
8801 read_direct_string (abfd, info_ptr, &bytes_read);
8802 info_ptr += bytes_read;
8803 break;
8804 case DW_FORM_sec_offset:
8805 case DW_FORM_strp:
8806 case DW_FORM_GNU_strp_alt:
8807 info_ptr += cu->header.offset_size;
8808 break;
8809 case DW_FORM_exprloc:
8810 case DW_FORM_block:
8811 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8812 info_ptr += bytes_read;
8813 break;
8814 case DW_FORM_block1:
8815 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8816 break;
8817 case DW_FORM_block2:
8818 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8819 break;
8820 case DW_FORM_block4:
8821 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8822 break;
8823 case DW_FORM_addrx:
8824 case DW_FORM_strx:
8825 case DW_FORM_sdata:
8826 case DW_FORM_udata:
8827 case DW_FORM_ref_udata:
8828 case DW_FORM_GNU_addr_index:
8829 case DW_FORM_GNU_str_index:
8830 case DW_FORM_rnglistx:
8831 case DW_FORM_loclistx:
8832 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8833 break;
8834 case DW_FORM_indirect:
8835 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8836 info_ptr += bytes_read;
8837 /* We need to continue parsing from here, so just go back to
8838 the top. */
8839 goto skip_attribute;
8840
8841 default:
8842 error (_("Dwarf Error: Cannot handle %s "
8843 "in DWARF reader [in module %s]"),
8844 dwarf_form_name (form),
8845 bfd_get_filename (abfd));
8846 }
8847 }
8848
8849 if (abbrev->has_children)
8850 return skip_children (reader, info_ptr);
8851 else
8852 return info_ptr;
8853 }
8854
8855 /* Locate ORIG_PDI's sibling.
8856 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8857
8858 static const gdb_byte *
8859 locate_pdi_sibling (const struct die_reader_specs *reader,
8860 struct partial_die_info *orig_pdi,
8861 const gdb_byte *info_ptr)
8862 {
8863 /* Do we know the sibling already? */
8864
8865 if (orig_pdi->sibling)
8866 return orig_pdi->sibling;
8867
8868 /* Are there any children to deal with? */
8869
8870 if (!orig_pdi->has_children)
8871 return info_ptr;
8872
8873 /* Skip the children the long way. */
8874
8875 return skip_children (reader, info_ptr);
8876 }
8877
8878 /* Expand this partial symbol table into a full symbol table. SELF is
8879 not NULL. */
8880
8881 void
8882 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8883 {
8884 struct dwarf2_per_objfile *dwarf2_per_objfile
8885 = get_dwarf2_per_objfile (objfile);
8886
8887 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8888
8889 /* If this psymtab is constructed from a debug-only objfile, the
8890 has_section_at_zero flag will not necessarily be correct. We
8891 can get the correct value for this flag by looking at the data
8892 associated with the (presumably stripped) associated objfile. */
8893 if (objfile->separate_debug_objfile_backlink)
8894 {
8895 struct dwarf2_per_objfile *dpo_backlink
8896 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8897
8898 dwarf2_per_objfile->per_bfd->has_section_at_zero
8899 = dpo_backlink->per_bfd->has_section_at_zero;
8900 }
8901
8902 expand_psymtab (objfile);
8903
8904 process_cu_includes (dwarf2_per_objfile);
8905 }
8906 \f
8907 /* Reading in full CUs. */
8908
8909 /* Add PER_CU to the queue. */
8910
8911 static void
8912 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8913 enum language pretend_language)
8914 {
8915 per_cu->queued = 1;
8916 per_cu->per_bfd->queue.emplace (per_cu, pretend_language);
8917 }
8918
8919 /* If PER_CU is not yet queued, add it to the queue.
8920 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8921 dependency.
8922 The result is non-zero if PER_CU was queued, otherwise the result is zero
8923 meaning either PER_CU is already queued or it is already loaded.
8924
8925 N.B. There is an invariant here that if a CU is queued then it is loaded.
8926 The caller is required to load PER_CU if we return non-zero. */
8927
8928 static int
8929 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8930 struct dwarf2_per_cu_data *per_cu,
8931 enum language pretend_language)
8932 {
8933 /* We may arrive here during partial symbol reading, if we need full
8934 DIEs to process an unusual case (e.g. template arguments). Do
8935 not queue PER_CU, just tell our caller to load its DIEs. */
8936 if (per_cu->per_bfd->reading_partial_symbols)
8937 {
8938 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8939 return 1;
8940 return 0;
8941 }
8942
8943 /* Mark the dependence relation so that we don't flush PER_CU
8944 too early. */
8945 if (dependent_cu != NULL)
8946 dwarf2_add_dependence (dependent_cu, per_cu);
8947
8948 /* If it's already on the queue, we have nothing to do. */
8949 if (per_cu->queued)
8950 return 0;
8951
8952 /* If the compilation unit is already loaded, just mark it as
8953 used. */
8954 if (per_cu->cu != NULL)
8955 {
8956 per_cu->cu->last_used = 0;
8957 return 0;
8958 }
8959
8960 /* Add it to the queue. */
8961 queue_comp_unit (per_cu, pretend_language);
8962
8963 return 1;
8964 }
8965
8966 /* Process the queue. */
8967
8968 static void
8969 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8970 {
8971 if (dwarf_read_debug)
8972 {
8973 fprintf_unfiltered (gdb_stdlog,
8974 "Expanding one or more symtabs of objfile %s ...\n",
8975 objfile_name (dwarf2_per_objfile->objfile));
8976 }
8977
8978 /* The queue starts out with one item, but following a DIE reference
8979 may load a new CU, adding it to the end of the queue. */
8980 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
8981 {
8982 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
8983
8984 if (!dwarf2_per_objfile->symtab_set_p (item.per_cu)
8985 /* Skip dummy CUs. */
8986 && item.per_cu->cu != NULL)
8987 {
8988 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8989 unsigned int debug_print_threshold;
8990 char buf[100];
8991
8992 if (per_cu->is_debug_types)
8993 {
8994 struct signatured_type *sig_type =
8995 (struct signatured_type *) per_cu;
8996
8997 sprintf (buf, "TU %s at offset %s",
8998 hex_string (sig_type->signature),
8999 sect_offset_str (per_cu->sect_off));
9000 /* There can be 100s of TUs.
9001 Only print them in verbose mode. */
9002 debug_print_threshold = 2;
9003 }
9004 else
9005 {
9006 sprintf (buf, "CU at offset %s",
9007 sect_offset_str (per_cu->sect_off));
9008 debug_print_threshold = 1;
9009 }
9010
9011 if (dwarf_read_debug >= debug_print_threshold)
9012 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9013
9014 if (per_cu->is_debug_types)
9015 process_full_type_unit (per_cu, item.pretend_language);
9016 else
9017 process_full_comp_unit (per_cu, item.pretend_language);
9018
9019 if (dwarf_read_debug >= debug_print_threshold)
9020 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9021 }
9022
9023 item.per_cu->queued = 0;
9024 dwarf2_per_objfile->per_bfd->queue.pop ();
9025 }
9026
9027 if (dwarf_read_debug)
9028 {
9029 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9030 objfile_name (dwarf2_per_objfile->objfile));
9031 }
9032 }
9033
9034 /* Read in full symbols for PST, and anything it depends on. */
9035
9036 void
9037 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9038 {
9039 gdb_assert (!readin_p (objfile));
9040
9041 expand_dependencies (objfile);
9042
9043 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9044 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9045 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9046 }
9047
9048 /* See psympriv.h. */
9049
9050 bool
9051 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9052 {
9053 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9054 return per_objfile->symtab_set_p (per_cu_data);
9055 }
9056
9057 /* See psympriv.h. */
9058
9059 compunit_symtab *
9060 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9061 {
9062 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9063 return per_objfile->get_symtab (per_cu_data);
9064 }
9065
9066 /* Trivial hash function for die_info: the hash value of a DIE
9067 is its offset in .debug_info for this objfile. */
9068
9069 static hashval_t
9070 die_hash (const void *item)
9071 {
9072 const struct die_info *die = (const struct die_info *) item;
9073
9074 return to_underlying (die->sect_off);
9075 }
9076
9077 /* Trivial comparison function for die_info structures: two DIEs
9078 are equal if they have the same offset. */
9079
9080 static int
9081 die_eq (const void *item_lhs, const void *item_rhs)
9082 {
9083 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9084 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9085
9086 return die_lhs->sect_off == die_rhs->sect_off;
9087 }
9088
9089 /* Load the DIEs associated with PER_CU into memory. */
9090
9091 static void
9092 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9093 bool skip_partial,
9094 enum language pretend_language)
9095 {
9096 gdb_assert (! this_cu->is_debug_types);
9097
9098 cutu_reader reader (this_cu, NULL, 1, skip_partial);
9099 if (reader.dummy_p)
9100 return;
9101
9102 struct dwarf2_cu *cu = reader.cu;
9103 const gdb_byte *info_ptr = reader.info_ptr;
9104
9105 gdb_assert (cu->die_hash == NULL);
9106 cu->die_hash =
9107 htab_create_alloc_ex (cu->header.length / 12,
9108 die_hash,
9109 die_eq,
9110 NULL,
9111 &cu->comp_unit_obstack,
9112 hashtab_obstack_allocate,
9113 dummy_obstack_deallocate);
9114
9115 if (reader.comp_unit_die->has_children)
9116 reader.comp_unit_die->child
9117 = read_die_and_siblings (&reader, reader.info_ptr,
9118 &info_ptr, reader.comp_unit_die);
9119 cu->dies = reader.comp_unit_die;
9120 /* comp_unit_die is not stored in die_hash, no need. */
9121
9122 /* We try not to read any attributes in this function, because not
9123 all CUs needed for references have been loaded yet, and symbol
9124 table processing isn't initialized. But we have to set the CU language,
9125 or we won't be able to build types correctly.
9126 Similarly, if we do not read the producer, we can not apply
9127 producer-specific interpretation. */
9128 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9129
9130 reader.keep ();
9131 }
9132
9133 /* Add a DIE to the delayed physname list. */
9134
9135 static void
9136 add_to_method_list (struct type *type, int fnfield_index, int index,
9137 const char *name, struct die_info *die,
9138 struct dwarf2_cu *cu)
9139 {
9140 struct delayed_method_info mi;
9141 mi.type = type;
9142 mi.fnfield_index = fnfield_index;
9143 mi.index = index;
9144 mi.name = name;
9145 mi.die = die;
9146 cu->method_list.push_back (mi);
9147 }
9148
9149 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9150 "const" / "volatile". If so, decrements LEN by the length of the
9151 modifier and return true. Otherwise return false. */
9152
9153 template<size_t N>
9154 static bool
9155 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9156 {
9157 size_t mod_len = sizeof (mod) - 1;
9158 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9159 {
9160 len -= mod_len;
9161 return true;
9162 }
9163 return false;
9164 }
9165
9166 /* Compute the physnames of any methods on the CU's method list.
9167
9168 The computation of method physnames is delayed in order to avoid the
9169 (bad) condition that one of the method's formal parameters is of an as yet
9170 incomplete type. */
9171
9172 static void
9173 compute_delayed_physnames (struct dwarf2_cu *cu)
9174 {
9175 /* Only C++ delays computing physnames. */
9176 if (cu->method_list.empty ())
9177 return;
9178 gdb_assert (cu->language == language_cplus);
9179
9180 for (const delayed_method_info &mi : cu->method_list)
9181 {
9182 const char *physname;
9183 struct fn_fieldlist *fn_flp
9184 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9185 physname = dwarf2_physname (mi.name, mi.die, cu);
9186 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9187 = physname ? physname : "";
9188
9189 /* Since there's no tag to indicate whether a method is a
9190 const/volatile overload, extract that information out of the
9191 demangled name. */
9192 if (physname != NULL)
9193 {
9194 size_t len = strlen (physname);
9195
9196 while (1)
9197 {
9198 if (physname[len] == ')') /* shortcut */
9199 break;
9200 else if (check_modifier (physname, len, " const"))
9201 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9202 else if (check_modifier (physname, len, " volatile"))
9203 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9204 else
9205 break;
9206 }
9207 }
9208 }
9209
9210 /* The list is no longer needed. */
9211 cu->method_list.clear ();
9212 }
9213
9214 /* Go objects should be embedded in a DW_TAG_module DIE,
9215 and it's not clear if/how imported objects will appear.
9216 To keep Go support simple until that's worked out,
9217 go back through what we've read and create something usable.
9218 We could do this while processing each DIE, and feels kinda cleaner,
9219 but that way is more invasive.
9220 This is to, for example, allow the user to type "p var" or "b main"
9221 without having to specify the package name, and allow lookups
9222 of module.object to work in contexts that use the expression
9223 parser. */
9224
9225 static void
9226 fixup_go_packaging (struct dwarf2_cu *cu)
9227 {
9228 gdb::unique_xmalloc_ptr<char> package_name;
9229 struct pending *list;
9230 int i;
9231
9232 for (list = *cu->get_builder ()->get_global_symbols ();
9233 list != NULL;
9234 list = list->next)
9235 {
9236 for (i = 0; i < list->nsyms; ++i)
9237 {
9238 struct symbol *sym = list->symbol[i];
9239
9240 if (sym->language () == language_go
9241 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9242 {
9243 gdb::unique_xmalloc_ptr<char> this_package_name
9244 (go_symbol_package_name (sym));
9245
9246 if (this_package_name == NULL)
9247 continue;
9248 if (package_name == NULL)
9249 package_name = std::move (this_package_name);
9250 else
9251 {
9252 struct objfile *objfile = cu->per_objfile->objfile;
9253 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9254 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9255 (symbol_symtab (sym) != NULL
9256 ? symtab_to_filename_for_display
9257 (symbol_symtab (sym))
9258 : objfile_name (objfile)),
9259 this_package_name.get (), package_name.get ());
9260 }
9261 }
9262 }
9263 }
9264
9265 if (package_name != NULL)
9266 {
9267 struct objfile *objfile = cu->per_objfile->objfile;
9268 const char *saved_package_name = objfile->intern (package_name.get ());
9269 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9270 saved_package_name);
9271 struct symbol *sym;
9272
9273 sym = new (&objfile->objfile_obstack) symbol;
9274 sym->set_language (language_go, &objfile->objfile_obstack);
9275 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9276 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9277 e.g., "main" finds the "main" module and not C's main(). */
9278 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9279 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9280 SYMBOL_TYPE (sym) = type;
9281
9282 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9283 }
9284 }
9285
9286 /* Allocate a fully-qualified name consisting of the two parts on the
9287 obstack. */
9288
9289 static const char *
9290 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9291 {
9292 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9293 }
9294
9295 /* A helper that allocates a variant part to attach to a Rust enum
9296 type. OBSTACK is where the results should be allocated. TYPE is
9297 the type we're processing. DISCRIMINANT_INDEX is the index of the
9298 discriminant. It must be the index of one of the fields of TYPE.
9299 DEFAULT_INDEX is the index of the default field; or -1 if there is
9300 no default. RANGES is indexed by "effective" field number (the
9301 field index, but omitting the discriminant and default fields) and
9302 must hold the discriminant values used by the variants. Note that
9303 RANGES must have a lifetime at least as long as OBSTACK -- either
9304 already allocated on it, or static. */
9305
9306 static void
9307 alloc_rust_variant (struct obstack *obstack, struct type *type,
9308 int discriminant_index, int default_index,
9309 gdb::array_view<discriminant_range> ranges)
9310 {
9311 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9312 must be handled by the caller. */
9313 gdb_assert (discriminant_index >= 0
9314 && discriminant_index < type->num_fields ());
9315 gdb_assert (default_index == -1
9316 || (default_index >= 0 && default_index < type->num_fields ()));
9317
9318 /* We have one variant for each non-discriminant field. */
9319 int n_variants = type->num_fields () - 1;
9320
9321 variant *variants = new (obstack) variant[n_variants];
9322 int var_idx = 0;
9323 int range_idx = 0;
9324 for (int i = 0; i < type->num_fields (); ++i)
9325 {
9326 if (i == discriminant_index)
9327 continue;
9328
9329 variants[var_idx].first_field = i;
9330 variants[var_idx].last_field = i + 1;
9331
9332 /* The default field does not need a range, but other fields do.
9333 We skipped the discriminant above. */
9334 if (i != default_index)
9335 {
9336 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9337 ++range_idx;
9338 }
9339
9340 ++var_idx;
9341 }
9342
9343 gdb_assert (range_idx == ranges.size ());
9344 gdb_assert (var_idx == n_variants);
9345
9346 variant_part *part = new (obstack) variant_part;
9347 part->discriminant_index = discriminant_index;
9348 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9349 discriminant_index));
9350 part->variants = gdb::array_view<variant> (variants, n_variants);
9351
9352 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9353 gdb::array_view<variant_part> *prop_value
9354 = new (storage) gdb::array_view<variant_part> (part, 1);
9355
9356 struct dynamic_prop prop;
9357 prop.kind = PROP_VARIANT_PARTS;
9358 prop.data.variant_parts = prop_value;
9359
9360 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9361 }
9362
9363 /* Some versions of rustc emitted enums in an unusual way.
9364
9365 Ordinary enums were emitted as unions. The first element of each
9366 structure in the union was named "RUST$ENUM$DISR". This element
9367 held the discriminant.
9368
9369 These versions of Rust also implemented the "non-zero"
9370 optimization. When the enum had two values, and one is empty and
9371 the other holds a pointer that cannot be zero, the pointer is used
9372 as the discriminant, with a zero value meaning the empty variant.
9373 Here, the union's first member is of the form
9374 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9375 where the fieldnos are the indices of the fields that should be
9376 traversed in order to find the field (which may be several fields deep)
9377 and the variantname is the name of the variant of the case when the
9378 field is zero.
9379
9380 This function recognizes whether TYPE is of one of these forms,
9381 and, if so, smashes it to be a variant type. */
9382
9383 static void
9384 quirk_rust_enum (struct type *type, struct objfile *objfile)
9385 {
9386 gdb_assert (type->code () == TYPE_CODE_UNION);
9387
9388 /* We don't need to deal with empty enums. */
9389 if (type->num_fields () == 0)
9390 return;
9391
9392 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9393 if (type->num_fields () == 1
9394 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9395 {
9396 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9397
9398 /* Decode the field name to find the offset of the
9399 discriminant. */
9400 ULONGEST bit_offset = 0;
9401 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9402 while (name[0] >= '0' && name[0] <= '9')
9403 {
9404 char *tail;
9405 unsigned long index = strtoul (name, &tail, 10);
9406 name = tail;
9407 if (*name != '$'
9408 || index >= field_type->num_fields ()
9409 || (TYPE_FIELD_LOC_KIND (field_type, index)
9410 != FIELD_LOC_KIND_BITPOS))
9411 {
9412 complaint (_("Could not parse Rust enum encoding string \"%s\""
9413 "[in module %s]"),
9414 TYPE_FIELD_NAME (type, 0),
9415 objfile_name (objfile));
9416 return;
9417 }
9418 ++name;
9419
9420 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9421 field_type = TYPE_FIELD_TYPE (field_type, index);
9422 }
9423
9424 /* Smash this type to be a structure type. We have to do this
9425 because the type has already been recorded. */
9426 type->set_code (TYPE_CODE_STRUCT);
9427 type->set_num_fields (3);
9428 /* Save the field we care about. */
9429 struct field saved_field = type->field (0);
9430 type->set_fields
9431 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9432
9433 /* Put the discriminant at index 0. */
9434 TYPE_FIELD_TYPE (type, 0) = field_type;
9435 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9436 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9437 SET_FIELD_BITPOS (type->field (0), bit_offset);
9438
9439 /* The order of fields doesn't really matter, so put the real
9440 field at index 1 and the data-less field at index 2. */
9441 type->field (1) = saved_field;
9442 TYPE_FIELD_NAME (type, 1)
9443 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
9444 TYPE_FIELD_TYPE (type, 1)->set_name
9445 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9446 TYPE_FIELD_NAME (type, 1)));
9447
9448 const char *dataless_name
9449 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9450 name);
9451 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9452 dataless_name);
9453 TYPE_FIELD_TYPE (type, 2) = dataless_type;
9454 /* NAME points into the original discriminant name, which
9455 already has the correct lifetime. */
9456 TYPE_FIELD_NAME (type, 2) = name;
9457 SET_FIELD_BITPOS (type->field (2), 0);
9458
9459 /* Indicate that this is a variant type. */
9460 static discriminant_range ranges[1] = { { 0, 0 } };
9461 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9462 }
9463 /* A union with a single anonymous field is probably an old-style
9464 univariant enum. */
9465 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9466 {
9467 /* Smash this type to be a structure type. We have to do this
9468 because the type has already been recorded. */
9469 type->set_code (TYPE_CODE_STRUCT);
9470
9471 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9472 const char *variant_name
9473 = rust_last_path_segment (field_type->name ());
9474 TYPE_FIELD_NAME (type, 0) = variant_name;
9475 field_type->set_name
9476 (rust_fully_qualify (&objfile->objfile_obstack,
9477 type->name (), variant_name));
9478 }
9479 else
9480 {
9481 struct type *disr_type = nullptr;
9482 for (int i = 0; i < type->num_fields (); ++i)
9483 {
9484 disr_type = TYPE_FIELD_TYPE (type, i);
9485
9486 if (disr_type->code () != TYPE_CODE_STRUCT)
9487 {
9488 /* All fields of a true enum will be structs. */
9489 return;
9490 }
9491 else if (disr_type->num_fields () == 0)
9492 {
9493 /* Could be data-less variant, so keep going. */
9494 disr_type = nullptr;
9495 }
9496 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9497 "RUST$ENUM$DISR") != 0)
9498 {
9499 /* Not a Rust enum. */
9500 return;
9501 }
9502 else
9503 {
9504 /* Found one. */
9505 break;
9506 }
9507 }
9508
9509 /* If we got here without a discriminant, then it's probably
9510 just a union. */
9511 if (disr_type == nullptr)
9512 return;
9513
9514 /* Smash this type to be a structure type. We have to do this
9515 because the type has already been recorded. */
9516 type->set_code (TYPE_CODE_STRUCT);
9517
9518 /* Make space for the discriminant field. */
9519 struct field *disr_field = &disr_type->field (0);
9520 field *new_fields
9521 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9522 * sizeof (struct field)));
9523 memcpy (new_fields + 1, type->fields (),
9524 type->num_fields () * sizeof (struct field));
9525 type->set_fields (new_fields);
9526 type->set_num_fields (type->num_fields () + 1);
9527
9528 /* Install the discriminant at index 0 in the union. */
9529 type->field (0) = *disr_field;
9530 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9531 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9532
9533 /* We need a way to find the correct discriminant given a
9534 variant name. For convenience we build a map here. */
9535 struct type *enum_type = FIELD_TYPE (*disr_field);
9536 std::unordered_map<std::string, ULONGEST> discriminant_map;
9537 for (int i = 0; i < enum_type->num_fields (); ++i)
9538 {
9539 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9540 {
9541 const char *name
9542 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9543 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9544 }
9545 }
9546
9547 int n_fields = type->num_fields ();
9548 /* We don't need a range entry for the discriminant, but we do
9549 need one for every other field, as there is no default
9550 variant. */
9551 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9552 discriminant_range,
9553 n_fields - 1);
9554 /* Skip the discriminant here. */
9555 for (int i = 1; i < n_fields; ++i)
9556 {
9557 /* Find the final word in the name of this variant's type.
9558 That name can be used to look up the correct
9559 discriminant. */
9560 const char *variant_name
9561 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
9562
9563 auto iter = discriminant_map.find (variant_name);
9564 if (iter != discriminant_map.end ())
9565 {
9566 ranges[i].low = iter->second;
9567 ranges[i].high = iter->second;
9568 }
9569
9570 /* Remove the discriminant field, if it exists. */
9571 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
9572 if (sub_type->num_fields () > 0)
9573 {
9574 sub_type->set_num_fields (sub_type->num_fields () - 1);
9575 sub_type->set_fields (sub_type->fields () + 1);
9576 }
9577 TYPE_FIELD_NAME (type, i) = variant_name;
9578 sub_type->set_name
9579 (rust_fully_qualify (&objfile->objfile_obstack,
9580 type->name (), variant_name));
9581 }
9582
9583 /* Indicate that this is a variant type. */
9584 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9585 gdb::array_view<discriminant_range> (ranges,
9586 n_fields - 1));
9587 }
9588 }
9589
9590 /* Rewrite some Rust unions to be structures with variants parts. */
9591
9592 static void
9593 rust_union_quirks (struct dwarf2_cu *cu)
9594 {
9595 gdb_assert (cu->language == language_rust);
9596 for (type *type_ : cu->rust_unions)
9597 quirk_rust_enum (type_, cu->per_objfile->objfile);
9598 /* We don't need this any more. */
9599 cu->rust_unions.clear ();
9600 }
9601
9602 /* A helper function for computing the list of all symbol tables
9603 included by PER_CU. */
9604
9605 static void
9606 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9607 htab_t all_children, htab_t all_type_symtabs,
9608 struct dwarf2_per_cu_data *per_cu,
9609 struct compunit_symtab *immediate_parent)
9610 {
9611 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9612 if (*slot != NULL)
9613 {
9614 /* This inclusion and its children have been processed. */
9615 return;
9616 }
9617
9618 *slot = per_cu;
9619
9620 /* Only add a CU if it has a symbol table. */
9621 compunit_symtab *cust = per_cu->dwarf2_per_objfile->get_symtab (per_cu);
9622 if (cust != NULL)
9623 {
9624 /* If this is a type unit only add its symbol table if we haven't
9625 seen it yet (type unit per_cu's can share symtabs). */
9626 if (per_cu->is_debug_types)
9627 {
9628 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9629 if (*slot == NULL)
9630 {
9631 *slot = cust;
9632 result->push_back (cust);
9633 if (cust->user == NULL)
9634 cust->user = immediate_parent;
9635 }
9636 }
9637 else
9638 {
9639 result->push_back (cust);
9640 if (cust->user == NULL)
9641 cust->user = immediate_parent;
9642 }
9643 }
9644
9645 if (!per_cu->imported_symtabs_empty ())
9646 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9647 {
9648 recursively_compute_inclusions (result, all_children,
9649 all_type_symtabs, ptr, cust);
9650 }
9651 }
9652
9653 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9654 PER_CU. */
9655
9656 static void
9657 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9658 {
9659 gdb_assert (! per_cu->is_debug_types);
9660
9661 if (!per_cu->imported_symtabs_empty ())
9662 {
9663 int len;
9664 std::vector<compunit_symtab *> result_symtabs;
9665 htab_t all_children, all_type_symtabs;
9666 compunit_symtab *cust = per_cu->dwarf2_per_objfile->get_symtab (per_cu);
9667
9668 /* If we don't have a symtab, we can just skip this case. */
9669 if (cust == NULL)
9670 return;
9671
9672 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9673 NULL, xcalloc, xfree);
9674 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9675 NULL, xcalloc, xfree);
9676
9677 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9678 {
9679 recursively_compute_inclusions (&result_symtabs, all_children,
9680 all_type_symtabs, ptr, cust);
9681 }
9682
9683 /* Now we have a transitive closure of all the included symtabs. */
9684 len = result_symtabs.size ();
9685 cust->includes
9686 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9687 struct compunit_symtab *, len + 1);
9688 memcpy (cust->includes, result_symtabs.data (),
9689 len * sizeof (compunit_symtab *));
9690 cust->includes[len] = NULL;
9691
9692 htab_delete (all_children);
9693 htab_delete (all_type_symtabs);
9694 }
9695 }
9696
9697 /* Compute the 'includes' field for the symtabs of all the CUs we just
9698 read. */
9699
9700 static void
9701 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9702 {
9703 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
9704 {
9705 if (! iter->is_debug_types)
9706 compute_compunit_symtab_includes (iter);
9707 }
9708
9709 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
9710 }
9711
9712 /* Generate full symbol information for PER_CU, whose DIEs have
9713 already been loaded into memory. */
9714
9715 static void
9716 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9717 enum language pretend_language)
9718 {
9719 struct dwarf2_cu *cu = per_cu->cu;
9720 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9721 struct objfile *objfile = dwarf2_per_objfile->objfile;
9722 struct gdbarch *gdbarch = objfile->arch ();
9723 CORE_ADDR lowpc, highpc;
9724 struct compunit_symtab *cust;
9725 CORE_ADDR baseaddr;
9726 struct block *static_block;
9727 CORE_ADDR addr;
9728
9729 baseaddr = objfile->text_section_offset ();
9730
9731 /* Clear the list here in case something was left over. */
9732 cu->method_list.clear ();
9733
9734 cu->language = pretend_language;
9735 cu->language_defn = language_def (cu->language);
9736
9737 /* Do line number decoding in read_file_scope () */
9738 process_die (cu->dies, cu);
9739
9740 /* For now fudge the Go package. */
9741 if (cu->language == language_go)
9742 fixup_go_packaging (cu);
9743
9744 /* Now that we have processed all the DIEs in the CU, all the types
9745 should be complete, and it should now be safe to compute all of the
9746 physnames. */
9747 compute_delayed_physnames (cu);
9748
9749 if (cu->language == language_rust)
9750 rust_union_quirks (cu);
9751
9752 /* Some compilers don't define a DW_AT_high_pc attribute for the
9753 compilation unit. If the DW_AT_high_pc is missing, synthesize
9754 it, by scanning the DIE's below the compilation unit. */
9755 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9756
9757 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9758 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9759
9760 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9761 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9762 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9763 addrmap to help ensure it has an accurate map of pc values belonging to
9764 this comp unit. */
9765 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9766
9767 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9768 SECT_OFF_TEXT (objfile),
9769 0);
9770
9771 if (cust != NULL)
9772 {
9773 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9774
9775 /* Set symtab language to language from DW_AT_language. If the
9776 compilation is from a C file generated by language preprocessors, do
9777 not set the language if it was already deduced by start_subfile. */
9778 if (!(cu->language == language_c
9779 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9780 COMPUNIT_FILETABS (cust)->language = cu->language;
9781
9782 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9783 produce DW_AT_location with location lists but it can be possibly
9784 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9785 there were bugs in prologue debug info, fixed later in GCC-4.5
9786 by "unwind info for epilogues" patch (which is not directly related).
9787
9788 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9789 needed, it would be wrong due to missing DW_AT_producer there.
9790
9791 Still one can confuse GDB by using non-standard GCC compilation
9792 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9793 */
9794 if (cu->has_loclist && gcc_4_minor >= 5)
9795 cust->locations_valid = 1;
9796
9797 if (gcc_4_minor >= 5)
9798 cust->epilogue_unwind_valid = 1;
9799
9800 cust->call_site_htab = cu->call_site_htab;
9801 }
9802
9803 dwarf2_per_objfile->set_symtab (per_cu, cust);
9804
9805 /* Push it for inclusion processing later. */
9806 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (per_cu);
9807
9808 /* Not needed any more. */
9809 cu->reset_builder ();
9810 }
9811
9812 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9813 already been loaded into memory. */
9814
9815 static void
9816 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9817 enum language pretend_language)
9818 {
9819 struct dwarf2_cu *cu = per_cu->cu;
9820 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9821 struct objfile *objfile = dwarf2_per_objfile->objfile;
9822 struct compunit_symtab *cust;
9823 struct signatured_type *sig_type;
9824
9825 gdb_assert (per_cu->is_debug_types);
9826 sig_type = (struct signatured_type *) per_cu;
9827
9828 /* Clear the list here in case something was left over. */
9829 cu->method_list.clear ();
9830
9831 cu->language = pretend_language;
9832 cu->language_defn = language_def (cu->language);
9833
9834 /* The symbol tables are set up in read_type_unit_scope. */
9835 process_die (cu->dies, cu);
9836
9837 /* For now fudge the Go package. */
9838 if (cu->language == language_go)
9839 fixup_go_packaging (cu);
9840
9841 /* Now that we have processed all the DIEs in the CU, all the types
9842 should be complete, and it should now be safe to compute all of the
9843 physnames. */
9844 compute_delayed_physnames (cu);
9845
9846 if (cu->language == language_rust)
9847 rust_union_quirks (cu);
9848
9849 /* TUs share symbol tables.
9850 If this is the first TU to use this symtab, complete the construction
9851 of it with end_expandable_symtab. Otherwise, complete the addition of
9852 this TU's symbols to the existing symtab. */
9853 if (sig_type->type_unit_group->compunit_symtab == NULL)
9854 {
9855 buildsym_compunit *builder = cu->get_builder ();
9856 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9857 sig_type->type_unit_group->compunit_symtab = cust;
9858
9859 if (cust != NULL)
9860 {
9861 /* Set symtab language to language from DW_AT_language. If the
9862 compilation is from a C file generated by language preprocessors,
9863 do not set the language if it was already deduced by
9864 start_subfile. */
9865 if (!(cu->language == language_c
9866 && COMPUNIT_FILETABS (cust)->language != language_c))
9867 COMPUNIT_FILETABS (cust)->language = cu->language;
9868 }
9869 }
9870 else
9871 {
9872 cu->get_builder ()->augment_type_symtab ();
9873 cust = sig_type->type_unit_group->compunit_symtab;
9874 }
9875
9876 dwarf2_per_objfile->set_symtab (per_cu, cust);
9877
9878 /* Not needed any more. */
9879 cu->reset_builder ();
9880 }
9881
9882 /* Process an imported unit DIE. */
9883
9884 static void
9885 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9886 {
9887 struct attribute *attr;
9888
9889 /* For now we don't handle imported units in type units. */
9890 if (cu->per_cu->is_debug_types)
9891 {
9892 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9893 " supported in type units [in module %s]"),
9894 objfile_name (cu->per_objfile->objfile));
9895 }
9896
9897 attr = dwarf2_attr (die, DW_AT_import, cu);
9898 if (attr != NULL)
9899 {
9900 sect_offset sect_off = attr->get_ref_die_offset ();
9901 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9902 dwarf2_per_cu_data *per_cu
9903 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->per_objfile);
9904
9905 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9906 into another compilation unit, at root level. Regard this as a hint,
9907 and ignore it. */
9908 if (die->parent && die->parent->parent == NULL
9909 && per_cu->unit_type == DW_UT_compile
9910 && per_cu->lang == language_cplus)
9911 return;
9912
9913 /* If necessary, add it to the queue and load its DIEs. */
9914 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9915 load_full_comp_unit (per_cu, false, cu->language);
9916
9917 cu->per_cu->imported_symtabs_push (per_cu);
9918 }
9919 }
9920
9921 /* RAII object that represents a process_die scope: i.e.,
9922 starts/finishes processing a DIE. */
9923 class process_die_scope
9924 {
9925 public:
9926 process_die_scope (die_info *die, dwarf2_cu *cu)
9927 : m_die (die), m_cu (cu)
9928 {
9929 /* We should only be processing DIEs not already in process. */
9930 gdb_assert (!m_die->in_process);
9931 m_die->in_process = true;
9932 }
9933
9934 ~process_die_scope ()
9935 {
9936 m_die->in_process = false;
9937
9938 /* If we're done processing the DIE for the CU that owns the line
9939 header, we don't need the line header anymore. */
9940 if (m_cu->line_header_die_owner == m_die)
9941 {
9942 delete m_cu->line_header;
9943 m_cu->line_header = NULL;
9944 m_cu->line_header_die_owner = NULL;
9945 }
9946 }
9947
9948 private:
9949 die_info *m_die;
9950 dwarf2_cu *m_cu;
9951 };
9952
9953 /* Process a die and its children. */
9954
9955 static void
9956 process_die (struct die_info *die, struct dwarf2_cu *cu)
9957 {
9958 process_die_scope scope (die, cu);
9959
9960 switch (die->tag)
9961 {
9962 case DW_TAG_padding:
9963 break;
9964 case DW_TAG_compile_unit:
9965 case DW_TAG_partial_unit:
9966 read_file_scope (die, cu);
9967 break;
9968 case DW_TAG_type_unit:
9969 read_type_unit_scope (die, cu);
9970 break;
9971 case DW_TAG_subprogram:
9972 /* Nested subprograms in Fortran get a prefix. */
9973 if (cu->language == language_fortran
9974 && die->parent != NULL
9975 && die->parent->tag == DW_TAG_subprogram)
9976 cu->processing_has_namespace_info = true;
9977 /* Fall through. */
9978 case DW_TAG_inlined_subroutine:
9979 read_func_scope (die, cu);
9980 break;
9981 case DW_TAG_lexical_block:
9982 case DW_TAG_try_block:
9983 case DW_TAG_catch_block:
9984 read_lexical_block_scope (die, cu);
9985 break;
9986 case DW_TAG_call_site:
9987 case DW_TAG_GNU_call_site:
9988 read_call_site_scope (die, cu);
9989 break;
9990 case DW_TAG_class_type:
9991 case DW_TAG_interface_type:
9992 case DW_TAG_structure_type:
9993 case DW_TAG_union_type:
9994 process_structure_scope (die, cu);
9995 break;
9996 case DW_TAG_enumeration_type:
9997 process_enumeration_scope (die, cu);
9998 break;
9999
10000 /* These dies have a type, but processing them does not create
10001 a symbol or recurse to process the children. Therefore we can
10002 read them on-demand through read_type_die. */
10003 case DW_TAG_subroutine_type:
10004 case DW_TAG_set_type:
10005 case DW_TAG_array_type:
10006 case DW_TAG_pointer_type:
10007 case DW_TAG_ptr_to_member_type:
10008 case DW_TAG_reference_type:
10009 case DW_TAG_rvalue_reference_type:
10010 case DW_TAG_string_type:
10011 break;
10012
10013 case DW_TAG_base_type:
10014 case DW_TAG_subrange_type:
10015 case DW_TAG_typedef:
10016 /* Add a typedef symbol for the type definition, if it has a
10017 DW_AT_name. */
10018 new_symbol (die, read_type_die (die, cu), cu);
10019 break;
10020 case DW_TAG_common_block:
10021 read_common_block (die, cu);
10022 break;
10023 case DW_TAG_common_inclusion:
10024 break;
10025 case DW_TAG_namespace:
10026 cu->processing_has_namespace_info = true;
10027 read_namespace (die, cu);
10028 break;
10029 case DW_TAG_module:
10030 cu->processing_has_namespace_info = true;
10031 read_module (die, cu);
10032 break;
10033 case DW_TAG_imported_declaration:
10034 cu->processing_has_namespace_info = true;
10035 if (read_namespace_alias (die, cu))
10036 break;
10037 /* The declaration is not a global namespace alias. */
10038 /* Fall through. */
10039 case DW_TAG_imported_module:
10040 cu->processing_has_namespace_info = true;
10041 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10042 || cu->language != language_fortran))
10043 complaint (_("Tag '%s' has unexpected children"),
10044 dwarf_tag_name (die->tag));
10045 read_import_statement (die, cu);
10046 break;
10047
10048 case DW_TAG_imported_unit:
10049 process_imported_unit_die (die, cu);
10050 break;
10051
10052 case DW_TAG_variable:
10053 read_variable (die, cu);
10054 break;
10055
10056 default:
10057 new_symbol (die, NULL, cu);
10058 break;
10059 }
10060 }
10061 \f
10062 /* DWARF name computation. */
10063
10064 /* A helper function for dwarf2_compute_name which determines whether DIE
10065 needs to have the name of the scope prepended to the name listed in the
10066 die. */
10067
10068 static int
10069 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10070 {
10071 struct attribute *attr;
10072
10073 switch (die->tag)
10074 {
10075 case DW_TAG_namespace:
10076 case DW_TAG_typedef:
10077 case DW_TAG_class_type:
10078 case DW_TAG_interface_type:
10079 case DW_TAG_structure_type:
10080 case DW_TAG_union_type:
10081 case DW_TAG_enumeration_type:
10082 case DW_TAG_enumerator:
10083 case DW_TAG_subprogram:
10084 case DW_TAG_inlined_subroutine:
10085 case DW_TAG_member:
10086 case DW_TAG_imported_declaration:
10087 return 1;
10088
10089 case DW_TAG_variable:
10090 case DW_TAG_constant:
10091 /* We only need to prefix "globally" visible variables. These include
10092 any variable marked with DW_AT_external or any variable that
10093 lives in a namespace. [Variables in anonymous namespaces
10094 require prefixing, but they are not DW_AT_external.] */
10095
10096 if (dwarf2_attr (die, DW_AT_specification, cu))
10097 {
10098 struct dwarf2_cu *spec_cu = cu;
10099
10100 return die_needs_namespace (die_specification (die, &spec_cu),
10101 spec_cu);
10102 }
10103
10104 attr = dwarf2_attr (die, DW_AT_external, cu);
10105 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10106 && die->parent->tag != DW_TAG_module)
10107 return 0;
10108 /* A variable in a lexical block of some kind does not need a
10109 namespace, even though in C++ such variables may be external
10110 and have a mangled name. */
10111 if (die->parent->tag == DW_TAG_lexical_block
10112 || die->parent->tag == DW_TAG_try_block
10113 || die->parent->tag == DW_TAG_catch_block
10114 || die->parent->tag == DW_TAG_subprogram)
10115 return 0;
10116 return 1;
10117
10118 default:
10119 return 0;
10120 }
10121 }
10122
10123 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10124 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10125 defined for the given DIE. */
10126
10127 static struct attribute *
10128 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10129 {
10130 struct attribute *attr;
10131
10132 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10133 if (attr == NULL)
10134 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10135
10136 return attr;
10137 }
10138
10139 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10140 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10141 defined for the given DIE. */
10142
10143 static const char *
10144 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10145 {
10146 const char *linkage_name;
10147
10148 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10149 if (linkage_name == NULL)
10150 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10151
10152 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10153 See https://github.com/rust-lang/rust/issues/32925. */
10154 if (cu->language == language_rust && linkage_name != NULL
10155 && strchr (linkage_name, '{') != NULL)
10156 linkage_name = NULL;
10157
10158 return linkage_name;
10159 }
10160
10161 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10162 compute the physname for the object, which include a method's:
10163 - formal parameters (C++),
10164 - receiver type (Go),
10165
10166 The term "physname" is a bit confusing.
10167 For C++, for example, it is the demangled name.
10168 For Go, for example, it's the mangled name.
10169
10170 For Ada, return the DIE's linkage name rather than the fully qualified
10171 name. PHYSNAME is ignored..
10172
10173 The result is allocated on the objfile->per_bfd's obstack and
10174 canonicalized. */
10175
10176 static const char *
10177 dwarf2_compute_name (const char *name,
10178 struct die_info *die, struct dwarf2_cu *cu,
10179 int physname)
10180 {
10181 struct objfile *objfile = cu->per_objfile->objfile;
10182
10183 if (name == NULL)
10184 name = dwarf2_name (die, cu);
10185
10186 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10187 but otherwise compute it by typename_concat inside GDB.
10188 FIXME: Actually this is not really true, or at least not always true.
10189 It's all very confusing. compute_and_set_names doesn't try to demangle
10190 Fortran names because there is no mangling standard. So new_symbol
10191 will set the demangled name to the result of dwarf2_full_name, and it is
10192 the demangled name that GDB uses if it exists. */
10193 if (cu->language == language_ada
10194 || (cu->language == language_fortran && physname))
10195 {
10196 /* For Ada unit, we prefer the linkage name over the name, as
10197 the former contains the exported name, which the user expects
10198 to be able to reference. Ideally, we want the user to be able
10199 to reference this entity using either natural or linkage name,
10200 but we haven't started looking at this enhancement yet. */
10201 const char *linkage_name = dw2_linkage_name (die, cu);
10202
10203 if (linkage_name != NULL)
10204 return linkage_name;
10205 }
10206
10207 /* These are the only languages we know how to qualify names in. */
10208 if (name != NULL
10209 && (cu->language == language_cplus
10210 || cu->language == language_fortran || cu->language == language_d
10211 || cu->language == language_rust))
10212 {
10213 if (die_needs_namespace (die, cu))
10214 {
10215 const char *prefix;
10216 const char *canonical_name = NULL;
10217
10218 string_file buf;
10219
10220 prefix = determine_prefix (die, cu);
10221 if (*prefix != '\0')
10222 {
10223 gdb::unique_xmalloc_ptr<char> prefixed_name
10224 (typename_concat (NULL, prefix, name, physname, cu));
10225
10226 buf.puts (prefixed_name.get ());
10227 }
10228 else
10229 buf.puts (name);
10230
10231 /* Template parameters may be specified in the DIE's DW_AT_name, or
10232 as children with DW_TAG_template_type_param or
10233 DW_TAG_value_type_param. If the latter, add them to the name
10234 here. If the name already has template parameters, then
10235 skip this step; some versions of GCC emit both, and
10236 it is more efficient to use the pre-computed name.
10237
10238 Something to keep in mind about this process: it is very
10239 unlikely, or in some cases downright impossible, to produce
10240 something that will match the mangled name of a function.
10241 If the definition of the function has the same debug info,
10242 we should be able to match up with it anyway. But fallbacks
10243 using the minimal symbol, for instance to find a method
10244 implemented in a stripped copy of libstdc++, will not work.
10245 If we do not have debug info for the definition, we will have to
10246 match them up some other way.
10247
10248 When we do name matching there is a related problem with function
10249 templates; two instantiated function templates are allowed to
10250 differ only by their return types, which we do not add here. */
10251
10252 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10253 {
10254 struct attribute *attr;
10255 struct die_info *child;
10256 int first = 1;
10257
10258 die->building_fullname = 1;
10259
10260 for (child = die->child; child != NULL; child = child->sibling)
10261 {
10262 struct type *type;
10263 LONGEST value;
10264 const gdb_byte *bytes;
10265 struct dwarf2_locexpr_baton *baton;
10266 struct value *v;
10267
10268 if (child->tag != DW_TAG_template_type_param
10269 && child->tag != DW_TAG_template_value_param)
10270 continue;
10271
10272 if (first)
10273 {
10274 buf.puts ("<");
10275 first = 0;
10276 }
10277 else
10278 buf.puts (", ");
10279
10280 attr = dwarf2_attr (child, DW_AT_type, cu);
10281 if (attr == NULL)
10282 {
10283 complaint (_("template parameter missing DW_AT_type"));
10284 buf.puts ("UNKNOWN_TYPE");
10285 continue;
10286 }
10287 type = die_type (child, cu);
10288
10289 if (child->tag == DW_TAG_template_type_param)
10290 {
10291 c_print_type (type, "", &buf, -1, 0, cu->language,
10292 &type_print_raw_options);
10293 continue;
10294 }
10295
10296 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10297 if (attr == NULL)
10298 {
10299 complaint (_("template parameter missing "
10300 "DW_AT_const_value"));
10301 buf.puts ("UNKNOWN_VALUE");
10302 continue;
10303 }
10304
10305 dwarf2_const_value_attr (attr, type, name,
10306 &cu->comp_unit_obstack, cu,
10307 &value, &bytes, &baton);
10308
10309 if (TYPE_NOSIGN (type))
10310 /* GDB prints characters as NUMBER 'CHAR'. If that's
10311 changed, this can use value_print instead. */
10312 c_printchar (value, type, &buf);
10313 else
10314 {
10315 struct value_print_options opts;
10316
10317 if (baton != NULL)
10318 v = dwarf2_evaluate_loc_desc (type, NULL,
10319 baton->data,
10320 baton->size,
10321 baton->per_cu);
10322 else if (bytes != NULL)
10323 {
10324 v = allocate_value (type);
10325 memcpy (value_contents_writeable (v), bytes,
10326 TYPE_LENGTH (type));
10327 }
10328 else
10329 v = value_from_longest (type, value);
10330
10331 /* Specify decimal so that we do not depend on
10332 the radix. */
10333 get_formatted_print_options (&opts, 'd');
10334 opts.raw = 1;
10335 value_print (v, &buf, &opts);
10336 release_value (v);
10337 }
10338 }
10339
10340 die->building_fullname = 0;
10341
10342 if (!first)
10343 {
10344 /* Close the argument list, with a space if necessary
10345 (nested templates). */
10346 if (!buf.empty () && buf.string ().back () == '>')
10347 buf.puts (" >");
10348 else
10349 buf.puts (">");
10350 }
10351 }
10352
10353 /* For C++ methods, append formal parameter type
10354 information, if PHYSNAME. */
10355
10356 if (physname && die->tag == DW_TAG_subprogram
10357 && cu->language == language_cplus)
10358 {
10359 struct type *type = read_type_die (die, cu);
10360
10361 c_type_print_args (type, &buf, 1, cu->language,
10362 &type_print_raw_options);
10363
10364 if (cu->language == language_cplus)
10365 {
10366 /* Assume that an artificial first parameter is
10367 "this", but do not crash if it is not. RealView
10368 marks unnamed (and thus unused) parameters as
10369 artificial; there is no way to differentiate
10370 the two cases. */
10371 if (type->num_fields () > 0
10372 && TYPE_FIELD_ARTIFICIAL (type, 0)
10373 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
10374 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10375 0))))
10376 buf.puts (" const");
10377 }
10378 }
10379
10380 const std::string &intermediate_name = buf.string ();
10381
10382 if (cu->language == language_cplus)
10383 canonical_name
10384 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10385 objfile);
10386
10387 /* If we only computed INTERMEDIATE_NAME, or if
10388 INTERMEDIATE_NAME is already canonical, then we need to
10389 intern it. */
10390 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10391 name = objfile->intern (intermediate_name);
10392 else
10393 name = canonical_name;
10394 }
10395 }
10396
10397 return name;
10398 }
10399
10400 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10401 If scope qualifiers are appropriate they will be added. The result
10402 will be allocated on the storage_obstack, or NULL if the DIE does
10403 not have a name. NAME may either be from a previous call to
10404 dwarf2_name or NULL.
10405
10406 The output string will be canonicalized (if C++). */
10407
10408 static const char *
10409 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10410 {
10411 return dwarf2_compute_name (name, die, cu, 0);
10412 }
10413
10414 /* Construct a physname for the given DIE in CU. NAME may either be
10415 from a previous call to dwarf2_name or NULL. The result will be
10416 allocated on the objfile_objstack or NULL if the DIE does not have a
10417 name.
10418
10419 The output string will be canonicalized (if C++). */
10420
10421 static const char *
10422 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10423 {
10424 struct objfile *objfile = cu->per_objfile->objfile;
10425 const char *retval, *mangled = NULL, *canon = NULL;
10426 int need_copy = 1;
10427
10428 /* In this case dwarf2_compute_name is just a shortcut not building anything
10429 on its own. */
10430 if (!die_needs_namespace (die, cu))
10431 return dwarf2_compute_name (name, die, cu, 1);
10432
10433 if (cu->language != language_rust)
10434 mangled = dw2_linkage_name (die, cu);
10435
10436 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10437 has computed. */
10438 gdb::unique_xmalloc_ptr<char> demangled;
10439 if (mangled != NULL)
10440 {
10441
10442 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10443 {
10444 /* Do nothing (do not demangle the symbol name). */
10445 }
10446 else if (cu->language == language_go)
10447 {
10448 /* This is a lie, but we already lie to the caller new_symbol.
10449 new_symbol assumes we return the mangled name.
10450 This just undoes that lie until things are cleaned up. */
10451 }
10452 else
10453 {
10454 /* Use DMGL_RET_DROP for C++ template functions to suppress
10455 their return type. It is easier for GDB users to search
10456 for such functions as `name(params)' than `long name(params)'.
10457 In such case the minimal symbol names do not match the full
10458 symbol names but for template functions there is never a need
10459 to look up their definition from their declaration so
10460 the only disadvantage remains the minimal symbol variant
10461 `long name(params)' does not have the proper inferior type. */
10462 demangled.reset (gdb_demangle (mangled,
10463 (DMGL_PARAMS | DMGL_ANSI
10464 | DMGL_RET_DROP)));
10465 }
10466 if (demangled)
10467 canon = demangled.get ();
10468 else
10469 {
10470 canon = mangled;
10471 need_copy = 0;
10472 }
10473 }
10474
10475 if (canon == NULL || check_physname)
10476 {
10477 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10478
10479 if (canon != NULL && strcmp (physname, canon) != 0)
10480 {
10481 /* It may not mean a bug in GDB. The compiler could also
10482 compute DW_AT_linkage_name incorrectly. But in such case
10483 GDB would need to be bug-to-bug compatible. */
10484
10485 complaint (_("Computed physname <%s> does not match demangled <%s> "
10486 "(from linkage <%s>) - DIE at %s [in module %s]"),
10487 physname, canon, mangled, sect_offset_str (die->sect_off),
10488 objfile_name (objfile));
10489
10490 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10491 is available here - over computed PHYSNAME. It is safer
10492 against both buggy GDB and buggy compilers. */
10493
10494 retval = canon;
10495 }
10496 else
10497 {
10498 retval = physname;
10499 need_copy = 0;
10500 }
10501 }
10502 else
10503 retval = canon;
10504
10505 if (need_copy)
10506 retval = objfile->intern (retval);
10507
10508 return retval;
10509 }
10510
10511 /* Inspect DIE in CU for a namespace alias. If one exists, record
10512 a new symbol for it.
10513
10514 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10515
10516 static int
10517 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10518 {
10519 struct attribute *attr;
10520
10521 /* If the die does not have a name, this is not a namespace
10522 alias. */
10523 attr = dwarf2_attr (die, DW_AT_name, cu);
10524 if (attr != NULL)
10525 {
10526 int num;
10527 struct die_info *d = die;
10528 struct dwarf2_cu *imported_cu = cu;
10529
10530 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10531 keep inspecting DIEs until we hit the underlying import. */
10532 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10533 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10534 {
10535 attr = dwarf2_attr (d, DW_AT_import, cu);
10536 if (attr == NULL)
10537 break;
10538
10539 d = follow_die_ref (d, attr, &imported_cu);
10540 if (d->tag != DW_TAG_imported_declaration)
10541 break;
10542 }
10543
10544 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10545 {
10546 complaint (_("DIE at %s has too many recursively imported "
10547 "declarations"), sect_offset_str (d->sect_off));
10548 return 0;
10549 }
10550
10551 if (attr != NULL)
10552 {
10553 struct type *type;
10554 sect_offset sect_off = attr->get_ref_die_offset ();
10555
10556 type = get_die_type_at_offset (sect_off, cu->per_cu);
10557 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10558 {
10559 /* This declaration is a global namespace alias. Add
10560 a symbol for it whose type is the aliased namespace. */
10561 new_symbol (die, type, cu);
10562 return 1;
10563 }
10564 }
10565 }
10566
10567 return 0;
10568 }
10569
10570 /* Return the using directives repository (global or local?) to use in the
10571 current context for CU.
10572
10573 For Ada, imported declarations can materialize renamings, which *may* be
10574 global. However it is impossible (for now?) in DWARF to distinguish
10575 "external" imported declarations and "static" ones. As all imported
10576 declarations seem to be static in all other languages, make them all CU-wide
10577 global only in Ada. */
10578
10579 static struct using_direct **
10580 using_directives (struct dwarf2_cu *cu)
10581 {
10582 if (cu->language == language_ada
10583 && cu->get_builder ()->outermost_context_p ())
10584 return cu->get_builder ()->get_global_using_directives ();
10585 else
10586 return cu->get_builder ()->get_local_using_directives ();
10587 }
10588
10589 /* Read the import statement specified by the given die and record it. */
10590
10591 static void
10592 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10593 {
10594 struct objfile *objfile = cu->per_objfile->objfile;
10595 struct attribute *import_attr;
10596 struct die_info *imported_die, *child_die;
10597 struct dwarf2_cu *imported_cu;
10598 const char *imported_name;
10599 const char *imported_name_prefix;
10600 const char *canonical_name;
10601 const char *import_alias;
10602 const char *imported_declaration = NULL;
10603 const char *import_prefix;
10604 std::vector<const char *> excludes;
10605
10606 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10607 if (import_attr == NULL)
10608 {
10609 complaint (_("Tag '%s' has no DW_AT_import"),
10610 dwarf_tag_name (die->tag));
10611 return;
10612 }
10613
10614 imported_cu = cu;
10615 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10616 imported_name = dwarf2_name (imported_die, imported_cu);
10617 if (imported_name == NULL)
10618 {
10619 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10620
10621 The import in the following code:
10622 namespace A
10623 {
10624 typedef int B;
10625 }
10626
10627 int main ()
10628 {
10629 using A::B;
10630 B b;
10631 return b;
10632 }
10633
10634 ...
10635 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10636 <52> DW_AT_decl_file : 1
10637 <53> DW_AT_decl_line : 6
10638 <54> DW_AT_import : <0x75>
10639 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10640 <59> DW_AT_name : B
10641 <5b> DW_AT_decl_file : 1
10642 <5c> DW_AT_decl_line : 2
10643 <5d> DW_AT_type : <0x6e>
10644 ...
10645 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10646 <76> DW_AT_byte_size : 4
10647 <77> DW_AT_encoding : 5 (signed)
10648
10649 imports the wrong die ( 0x75 instead of 0x58 ).
10650 This case will be ignored until the gcc bug is fixed. */
10651 return;
10652 }
10653
10654 /* Figure out the local name after import. */
10655 import_alias = dwarf2_name (die, cu);
10656
10657 /* Figure out where the statement is being imported to. */
10658 import_prefix = determine_prefix (die, cu);
10659
10660 /* Figure out what the scope of the imported die is and prepend it
10661 to the name of the imported die. */
10662 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10663
10664 if (imported_die->tag != DW_TAG_namespace
10665 && imported_die->tag != DW_TAG_module)
10666 {
10667 imported_declaration = imported_name;
10668 canonical_name = imported_name_prefix;
10669 }
10670 else if (strlen (imported_name_prefix) > 0)
10671 canonical_name = obconcat (&objfile->objfile_obstack,
10672 imported_name_prefix,
10673 (cu->language == language_d ? "." : "::"),
10674 imported_name, (char *) NULL);
10675 else
10676 canonical_name = imported_name;
10677
10678 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10679 for (child_die = die->child; child_die && child_die->tag;
10680 child_die = child_die->sibling)
10681 {
10682 /* DWARF-4: A Fortran use statement with a “rename list” may be
10683 represented by an imported module entry with an import attribute
10684 referring to the module and owned entries corresponding to those
10685 entities that are renamed as part of being imported. */
10686
10687 if (child_die->tag != DW_TAG_imported_declaration)
10688 {
10689 complaint (_("child DW_TAG_imported_declaration expected "
10690 "- DIE at %s [in module %s]"),
10691 sect_offset_str (child_die->sect_off),
10692 objfile_name (objfile));
10693 continue;
10694 }
10695
10696 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10697 if (import_attr == NULL)
10698 {
10699 complaint (_("Tag '%s' has no DW_AT_import"),
10700 dwarf_tag_name (child_die->tag));
10701 continue;
10702 }
10703
10704 imported_cu = cu;
10705 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10706 &imported_cu);
10707 imported_name = dwarf2_name (imported_die, imported_cu);
10708 if (imported_name == NULL)
10709 {
10710 complaint (_("child DW_TAG_imported_declaration has unknown "
10711 "imported name - DIE at %s [in module %s]"),
10712 sect_offset_str (child_die->sect_off),
10713 objfile_name (objfile));
10714 continue;
10715 }
10716
10717 excludes.push_back (imported_name);
10718
10719 process_die (child_die, cu);
10720 }
10721
10722 add_using_directive (using_directives (cu),
10723 import_prefix,
10724 canonical_name,
10725 import_alias,
10726 imported_declaration,
10727 excludes,
10728 0,
10729 &objfile->objfile_obstack);
10730 }
10731
10732 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10733 types, but gives them a size of zero. Starting with version 14,
10734 ICC is compatible with GCC. */
10735
10736 static bool
10737 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10738 {
10739 if (!cu->checked_producer)
10740 check_producer (cu);
10741
10742 return cu->producer_is_icc_lt_14;
10743 }
10744
10745 /* ICC generates a DW_AT_type for C void functions. This was observed on
10746 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10747 which says that void functions should not have a DW_AT_type. */
10748
10749 static bool
10750 producer_is_icc (struct dwarf2_cu *cu)
10751 {
10752 if (!cu->checked_producer)
10753 check_producer (cu);
10754
10755 return cu->producer_is_icc;
10756 }
10757
10758 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10759 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10760 this, it was first present in GCC release 4.3.0. */
10761
10762 static bool
10763 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10764 {
10765 if (!cu->checked_producer)
10766 check_producer (cu);
10767
10768 return cu->producer_is_gcc_lt_4_3;
10769 }
10770
10771 static file_and_directory
10772 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10773 {
10774 file_and_directory res;
10775
10776 /* Find the filename. Do not use dwarf2_name here, since the filename
10777 is not a source language identifier. */
10778 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10779 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10780
10781 if (res.comp_dir == NULL
10782 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10783 && IS_ABSOLUTE_PATH (res.name))
10784 {
10785 res.comp_dir_storage = ldirname (res.name);
10786 if (!res.comp_dir_storage.empty ())
10787 res.comp_dir = res.comp_dir_storage.c_str ();
10788 }
10789 if (res.comp_dir != NULL)
10790 {
10791 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10792 directory, get rid of it. */
10793 const char *cp = strchr (res.comp_dir, ':');
10794
10795 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10796 res.comp_dir = cp + 1;
10797 }
10798
10799 if (res.name == NULL)
10800 res.name = "<unknown>";
10801
10802 return res;
10803 }
10804
10805 /* Handle DW_AT_stmt_list for a compilation unit.
10806 DIE is the DW_TAG_compile_unit die for CU.
10807 COMP_DIR is the compilation directory. LOWPC is passed to
10808 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10809
10810 static void
10811 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10812 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10813 {
10814 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10815 struct attribute *attr;
10816 struct line_header line_header_local;
10817 hashval_t line_header_local_hash;
10818 void **slot;
10819 int decode_mapping;
10820
10821 gdb_assert (! cu->per_cu->is_debug_types);
10822
10823 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10824 if (attr == NULL)
10825 return;
10826
10827 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10828
10829 /* The line header hash table is only created if needed (it exists to
10830 prevent redundant reading of the line table for partial_units).
10831 If we're given a partial_unit, we'll need it. If we're given a
10832 compile_unit, then use the line header hash table if it's already
10833 created, but don't create one just yet. */
10834
10835 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
10836 && die->tag == DW_TAG_partial_unit)
10837 {
10838 dwarf2_per_objfile->per_bfd->line_header_hash
10839 .reset (htab_create_alloc (127, line_header_hash_voidp,
10840 line_header_eq_voidp,
10841 free_line_header_voidp,
10842 xcalloc, xfree));
10843 }
10844
10845 line_header_local.sect_off = line_offset;
10846 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10847 line_header_local_hash = line_header_hash (&line_header_local);
10848 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
10849 {
10850 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10851 &line_header_local,
10852 line_header_local_hash, NO_INSERT);
10853
10854 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10855 is not present in *SLOT (since if there is something in *SLOT then
10856 it will be for a partial_unit). */
10857 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10858 {
10859 gdb_assert (*slot != NULL);
10860 cu->line_header = (struct line_header *) *slot;
10861 return;
10862 }
10863 }
10864
10865 /* dwarf_decode_line_header does not yet provide sufficient information.
10866 We always have to call also dwarf_decode_lines for it. */
10867 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10868 if (lh == NULL)
10869 return;
10870
10871 cu->line_header = lh.release ();
10872 cu->line_header_die_owner = die;
10873
10874 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
10875 slot = NULL;
10876 else
10877 {
10878 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10879 &line_header_local,
10880 line_header_local_hash, INSERT);
10881 gdb_assert (slot != NULL);
10882 }
10883 if (slot != NULL && *slot == NULL)
10884 {
10885 /* This newly decoded line number information unit will be owned
10886 by line_header_hash hash table. */
10887 *slot = cu->line_header;
10888 cu->line_header_die_owner = NULL;
10889 }
10890 else
10891 {
10892 /* We cannot free any current entry in (*slot) as that struct line_header
10893 may be already used by multiple CUs. Create only temporary decoded
10894 line_header for this CU - it may happen at most once for each line
10895 number information unit. And if we're not using line_header_hash
10896 then this is what we want as well. */
10897 gdb_assert (die->tag != DW_TAG_partial_unit);
10898 }
10899 decode_mapping = (die->tag != DW_TAG_partial_unit);
10900 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10901 decode_mapping);
10902
10903 }
10904
10905 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10906
10907 static void
10908 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10909 {
10910 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10911 struct objfile *objfile = dwarf2_per_objfile->objfile;
10912 struct gdbarch *gdbarch = objfile->arch ();
10913 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10914 CORE_ADDR highpc = ((CORE_ADDR) 0);
10915 struct attribute *attr;
10916 struct die_info *child_die;
10917 CORE_ADDR baseaddr;
10918
10919 prepare_one_comp_unit (cu, die, cu->language);
10920 baseaddr = objfile->text_section_offset ();
10921
10922 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10923
10924 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10925 from finish_block. */
10926 if (lowpc == ((CORE_ADDR) -1))
10927 lowpc = highpc;
10928 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10929
10930 file_and_directory fnd = find_file_and_directory (die, cu);
10931
10932 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10933 standardised yet. As a workaround for the language detection we fall
10934 back to the DW_AT_producer string. */
10935 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10936 cu->language = language_opencl;
10937
10938 /* Similar hack for Go. */
10939 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10940 set_cu_language (DW_LANG_Go, cu);
10941
10942 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10943
10944 /* Decode line number information if present. We do this before
10945 processing child DIEs, so that the line header table is available
10946 for DW_AT_decl_file. */
10947 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10948
10949 /* Process all dies in compilation unit. */
10950 if (die->child != NULL)
10951 {
10952 child_die = die->child;
10953 while (child_die && child_die->tag)
10954 {
10955 process_die (child_die, cu);
10956 child_die = child_die->sibling;
10957 }
10958 }
10959
10960 /* Decode macro information, if present. Dwarf 2 macro information
10961 refers to information in the line number info statement program
10962 header, so we can only read it if we've read the header
10963 successfully. */
10964 attr = dwarf2_attr (die, DW_AT_macros, cu);
10965 if (attr == NULL)
10966 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10967 if (attr && cu->line_header)
10968 {
10969 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10970 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10971
10972 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10973 }
10974 else
10975 {
10976 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10977 if (attr && cu->line_header)
10978 {
10979 unsigned int macro_offset = DW_UNSND (attr);
10980
10981 dwarf_decode_macros (cu, macro_offset, 0);
10982 }
10983 }
10984 }
10985
10986 void
10987 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10988 {
10989 struct type_unit_group *tu_group;
10990 int first_time;
10991 struct attribute *attr;
10992 unsigned int i;
10993 struct signatured_type *sig_type;
10994
10995 gdb_assert (per_cu->is_debug_types);
10996 sig_type = (struct signatured_type *) per_cu;
10997
10998 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10999
11000 /* If we're using .gdb_index (includes -readnow) then
11001 per_cu->type_unit_group may not have been set up yet. */
11002 if (sig_type->type_unit_group == NULL)
11003 sig_type->type_unit_group = get_type_unit_group (this, attr);
11004 tu_group = sig_type->type_unit_group;
11005
11006 /* If we've already processed this stmt_list there's no real need to
11007 do it again, we could fake it and just recreate the part we need
11008 (file name,index -> symtab mapping). If data shows this optimization
11009 is useful we can do it then. */
11010 first_time = tu_group->compunit_symtab == NULL;
11011
11012 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11013 debug info. */
11014 line_header_up lh;
11015 if (attr != NULL)
11016 {
11017 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11018 lh = dwarf_decode_line_header (line_offset, this);
11019 }
11020 if (lh == NULL)
11021 {
11022 if (first_time)
11023 start_symtab ("", NULL, 0);
11024 else
11025 {
11026 gdb_assert (tu_group->symtabs == NULL);
11027 gdb_assert (m_builder == nullptr);
11028 struct compunit_symtab *cust = tu_group->compunit_symtab;
11029 m_builder.reset (new struct buildsym_compunit
11030 (COMPUNIT_OBJFILE (cust), "",
11031 COMPUNIT_DIRNAME (cust),
11032 compunit_language (cust),
11033 0, cust));
11034 list_in_scope = get_builder ()->get_file_symbols ();
11035 }
11036 return;
11037 }
11038
11039 line_header = lh.release ();
11040 line_header_die_owner = die;
11041
11042 if (first_time)
11043 {
11044 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11045
11046 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11047 still initializing it, and our caller (a few levels up)
11048 process_full_type_unit still needs to know if this is the first
11049 time. */
11050
11051 tu_group->symtabs
11052 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11053 struct symtab *, line_header->file_names_size ());
11054
11055 auto &file_names = line_header->file_names ();
11056 for (i = 0; i < file_names.size (); ++i)
11057 {
11058 file_entry &fe = file_names[i];
11059 dwarf2_start_subfile (this, fe.name,
11060 fe.include_dir (line_header));
11061 buildsym_compunit *b = get_builder ();
11062 if (b->get_current_subfile ()->symtab == NULL)
11063 {
11064 /* NOTE: start_subfile will recognize when it's been
11065 passed a file it has already seen. So we can't
11066 assume there's a simple mapping from
11067 cu->line_header->file_names to subfiles, plus
11068 cu->line_header->file_names may contain dups. */
11069 b->get_current_subfile ()->symtab
11070 = allocate_symtab (cust, b->get_current_subfile ()->name);
11071 }
11072
11073 fe.symtab = b->get_current_subfile ()->symtab;
11074 tu_group->symtabs[i] = fe.symtab;
11075 }
11076 }
11077 else
11078 {
11079 gdb_assert (m_builder == nullptr);
11080 struct compunit_symtab *cust = tu_group->compunit_symtab;
11081 m_builder.reset (new struct buildsym_compunit
11082 (COMPUNIT_OBJFILE (cust), "",
11083 COMPUNIT_DIRNAME (cust),
11084 compunit_language (cust),
11085 0, cust));
11086 list_in_scope = get_builder ()->get_file_symbols ();
11087
11088 auto &file_names = line_header->file_names ();
11089 for (i = 0; i < file_names.size (); ++i)
11090 {
11091 file_entry &fe = file_names[i];
11092 fe.symtab = tu_group->symtabs[i];
11093 }
11094 }
11095
11096 /* The main symtab is allocated last. Type units don't have DW_AT_name
11097 so they don't have a "real" (so to speak) symtab anyway.
11098 There is later code that will assign the main symtab to all symbols
11099 that don't have one. We need to handle the case of a symbol with a
11100 missing symtab (DW_AT_decl_file) anyway. */
11101 }
11102
11103 /* Process DW_TAG_type_unit.
11104 For TUs we want to skip the first top level sibling if it's not the
11105 actual type being defined by this TU. In this case the first top
11106 level sibling is there to provide context only. */
11107
11108 static void
11109 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11110 {
11111 struct die_info *child_die;
11112
11113 prepare_one_comp_unit (cu, die, language_minimal);
11114
11115 /* Initialize (or reinitialize) the machinery for building symtabs.
11116 We do this before processing child DIEs, so that the line header table
11117 is available for DW_AT_decl_file. */
11118 cu->setup_type_unit_groups (die);
11119
11120 if (die->child != NULL)
11121 {
11122 child_die = die->child;
11123 while (child_die && child_die->tag)
11124 {
11125 process_die (child_die, cu);
11126 child_die = child_die->sibling;
11127 }
11128 }
11129 }
11130 \f
11131 /* DWO/DWP files.
11132
11133 http://gcc.gnu.org/wiki/DebugFission
11134 http://gcc.gnu.org/wiki/DebugFissionDWP
11135
11136 To simplify handling of both DWO files ("object" files with the DWARF info)
11137 and DWP files (a file with the DWOs packaged up into one file), we treat
11138 DWP files as having a collection of virtual DWO files. */
11139
11140 static hashval_t
11141 hash_dwo_file (const void *item)
11142 {
11143 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11144 hashval_t hash;
11145
11146 hash = htab_hash_string (dwo_file->dwo_name);
11147 if (dwo_file->comp_dir != NULL)
11148 hash += htab_hash_string (dwo_file->comp_dir);
11149 return hash;
11150 }
11151
11152 static int
11153 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11154 {
11155 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11156 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11157
11158 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11159 return 0;
11160 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11161 return lhs->comp_dir == rhs->comp_dir;
11162 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11163 }
11164
11165 /* Allocate a hash table for DWO files. */
11166
11167 static htab_up
11168 allocate_dwo_file_hash_table ()
11169 {
11170 auto delete_dwo_file = [] (void *item)
11171 {
11172 struct dwo_file *dwo_file = (struct dwo_file *) item;
11173
11174 delete dwo_file;
11175 };
11176
11177 return htab_up (htab_create_alloc (41,
11178 hash_dwo_file,
11179 eq_dwo_file,
11180 delete_dwo_file,
11181 xcalloc, xfree));
11182 }
11183
11184 /* Lookup DWO file DWO_NAME. */
11185
11186 static void **
11187 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11188 const char *dwo_name,
11189 const char *comp_dir)
11190 {
11191 struct dwo_file find_entry;
11192 void **slot;
11193
11194 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11195 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11196
11197 find_entry.dwo_name = dwo_name;
11198 find_entry.comp_dir = comp_dir;
11199 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
11200 INSERT);
11201
11202 return slot;
11203 }
11204
11205 static hashval_t
11206 hash_dwo_unit (const void *item)
11207 {
11208 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11209
11210 /* This drops the top 32 bits of the id, but is ok for a hash. */
11211 return dwo_unit->signature;
11212 }
11213
11214 static int
11215 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11216 {
11217 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11218 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11219
11220 /* The signature is assumed to be unique within the DWO file.
11221 So while object file CU dwo_id's always have the value zero,
11222 that's OK, assuming each object file DWO file has only one CU,
11223 and that's the rule for now. */
11224 return lhs->signature == rhs->signature;
11225 }
11226
11227 /* Allocate a hash table for DWO CUs,TUs.
11228 There is one of these tables for each of CUs,TUs for each DWO file. */
11229
11230 static htab_up
11231 allocate_dwo_unit_table ()
11232 {
11233 /* Start out with a pretty small number.
11234 Generally DWO files contain only one CU and maybe some TUs. */
11235 return htab_up (htab_create_alloc (3,
11236 hash_dwo_unit,
11237 eq_dwo_unit,
11238 NULL, xcalloc, xfree));
11239 }
11240
11241 /* die_reader_func for create_dwo_cu. */
11242
11243 static void
11244 create_dwo_cu_reader (const struct die_reader_specs *reader,
11245 const gdb_byte *info_ptr,
11246 struct die_info *comp_unit_die,
11247 struct dwo_file *dwo_file,
11248 struct dwo_unit *dwo_unit)
11249 {
11250 struct dwarf2_cu *cu = reader->cu;
11251 sect_offset sect_off = cu->per_cu->sect_off;
11252 struct dwarf2_section_info *section = cu->per_cu->section;
11253
11254 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11255 if (!signature.has_value ())
11256 {
11257 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11258 " its dwo_id [in module %s]"),
11259 sect_offset_str (sect_off), dwo_file->dwo_name);
11260 return;
11261 }
11262
11263 dwo_unit->dwo_file = dwo_file;
11264 dwo_unit->signature = *signature;
11265 dwo_unit->section = section;
11266 dwo_unit->sect_off = sect_off;
11267 dwo_unit->length = cu->per_cu->length;
11268
11269 if (dwarf_read_debug)
11270 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11271 sect_offset_str (sect_off),
11272 hex_string (dwo_unit->signature));
11273 }
11274
11275 /* Create the dwo_units for the CUs in a DWO_FILE.
11276 Note: This function processes DWO files only, not DWP files. */
11277
11278 static void
11279 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11280 dwarf2_cu *cu, struct dwo_file &dwo_file,
11281 dwarf2_section_info &section, htab_up &cus_htab)
11282 {
11283 struct objfile *objfile = dwarf2_per_objfile->objfile;
11284 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
11285 const gdb_byte *info_ptr, *end_ptr;
11286
11287 section.read (objfile);
11288 info_ptr = section.buffer;
11289
11290 if (info_ptr == NULL)
11291 return;
11292
11293 if (dwarf_read_debug)
11294 {
11295 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11296 section.get_name (),
11297 section.get_file_name ());
11298 }
11299
11300 end_ptr = info_ptr + section.size;
11301 while (info_ptr < end_ptr)
11302 {
11303 struct dwarf2_per_cu_data per_cu;
11304 struct dwo_unit read_unit {};
11305 struct dwo_unit *dwo_unit;
11306 void **slot;
11307 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11308
11309 memset (&per_cu, 0, sizeof (per_cu));
11310 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11311 per_cu.per_bfd = per_bfd;
11312 per_cu.is_debug_types = 0;
11313 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11314 per_cu.section = &section;
11315
11316 cutu_reader reader (&per_cu, cu, &dwo_file);
11317 if (!reader.dummy_p)
11318 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11319 &dwo_file, &read_unit);
11320 info_ptr += per_cu.length;
11321
11322 // If the unit could not be parsed, skip it.
11323 if (read_unit.dwo_file == NULL)
11324 continue;
11325
11326 if (cus_htab == NULL)
11327 cus_htab = allocate_dwo_unit_table ();
11328
11329 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11330 struct dwo_unit);
11331 *dwo_unit = read_unit;
11332 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11333 gdb_assert (slot != NULL);
11334 if (*slot != NULL)
11335 {
11336 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11337 sect_offset dup_sect_off = dup_cu->sect_off;
11338
11339 complaint (_("debug cu entry at offset %s is duplicate to"
11340 " the entry at offset %s, signature %s"),
11341 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11342 hex_string (dwo_unit->signature));
11343 }
11344 *slot = (void *)dwo_unit;
11345 }
11346 }
11347
11348 /* DWP file .debug_{cu,tu}_index section format:
11349 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11350
11351 DWP Version 1:
11352
11353 Both index sections have the same format, and serve to map a 64-bit
11354 signature to a set of section numbers. Each section begins with a header,
11355 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11356 indexes, and a pool of 32-bit section numbers. The index sections will be
11357 aligned at 8-byte boundaries in the file.
11358
11359 The index section header consists of:
11360
11361 V, 32 bit version number
11362 -, 32 bits unused
11363 N, 32 bit number of compilation units or type units in the index
11364 M, 32 bit number of slots in the hash table
11365
11366 Numbers are recorded using the byte order of the application binary.
11367
11368 The hash table begins at offset 16 in the section, and consists of an array
11369 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11370 order of the application binary). Unused slots in the hash table are 0.
11371 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11372
11373 The parallel table begins immediately after the hash table
11374 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11375 array of 32-bit indexes (using the byte order of the application binary),
11376 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11377 table contains a 32-bit index into the pool of section numbers. For unused
11378 hash table slots, the corresponding entry in the parallel table will be 0.
11379
11380 The pool of section numbers begins immediately following the hash table
11381 (at offset 16 + 12 * M from the beginning of the section). The pool of
11382 section numbers consists of an array of 32-bit words (using the byte order
11383 of the application binary). Each item in the array is indexed starting
11384 from 0. The hash table entry provides the index of the first section
11385 number in the set. Additional section numbers in the set follow, and the
11386 set is terminated by a 0 entry (section number 0 is not used in ELF).
11387
11388 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11389 section must be the first entry in the set, and the .debug_abbrev.dwo must
11390 be the second entry. Other members of the set may follow in any order.
11391
11392 ---
11393
11394 DWP Version 2:
11395
11396 DWP Version 2 combines all the .debug_info, etc. sections into one,
11397 and the entries in the index tables are now offsets into these sections.
11398 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11399 section.
11400
11401 Index Section Contents:
11402 Header
11403 Hash Table of Signatures dwp_hash_table.hash_table
11404 Parallel Table of Indices dwp_hash_table.unit_table
11405 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11406 Table of Section Sizes dwp_hash_table.v2.sizes
11407
11408 The index section header consists of:
11409
11410 V, 32 bit version number
11411 L, 32 bit number of columns in the table of section offsets
11412 N, 32 bit number of compilation units or type units in the index
11413 M, 32 bit number of slots in the hash table
11414
11415 Numbers are recorded using the byte order of the application binary.
11416
11417 The hash table has the same format as version 1.
11418 The parallel table of indices has the same format as version 1,
11419 except that the entries are origin-1 indices into the table of sections
11420 offsets and the table of section sizes.
11421
11422 The table of offsets begins immediately following the parallel table
11423 (at offset 16 + 12 * M from the beginning of the section). The table is
11424 a two-dimensional array of 32-bit words (using the byte order of the
11425 application binary), with L columns and N+1 rows, in row-major order.
11426 Each row in the array is indexed starting from 0. The first row provides
11427 a key to the remaining rows: each column in this row provides an identifier
11428 for a debug section, and the offsets in the same column of subsequent rows
11429 refer to that section. The section identifiers are:
11430
11431 DW_SECT_INFO 1 .debug_info.dwo
11432 DW_SECT_TYPES 2 .debug_types.dwo
11433 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11434 DW_SECT_LINE 4 .debug_line.dwo
11435 DW_SECT_LOC 5 .debug_loc.dwo
11436 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11437 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11438 DW_SECT_MACRO 8 .debug_macro.dwo
11439
11440 The offsets provided by the CU and TU index sections are the base offsets
11441 for the contributions made by each CU or TU to the corresponding section
11442 in the package file. Each CU and TU header contains an abbrev_offset
11443 field, used to find the abbreviations table for that CU or TU within the
11444 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11445 be interpreted as relative to the base offset given in the index section.
11446 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11447 should be interpreted as relative to the base offset for .debug_line.dwo,
11448 and offsets into other debug sections obtained from DWARF attributes should
11449 also be interpreted as relative to the corresponding base offset.
11450
11451 The table of sizes begins immediately following the table of offsets.
11452 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11453 with L columns and N rows, in row-major order. Each row in the array is
11454 indexed starting from 1 (row 0 is shared by the two tables).
11455
11456 ---
11457
11458 Hash table lookup is handled the same in version 1 and 2:
11459
11460 We assume that N and M will not exceed 2^32 - 1.
11461 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11462
11463 Given a 64-bit compilation unit signature or a type signature S, an entry
11464 in the hash table is located as follows:
11465
11466 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11467 the low-order k bits all set to 1.
11468
11469 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11470
11471 3) If the hash table entry at index H matches the signature, use that
11472 entry. If the hash table entry at index H is unused (all zeroes),
11473 terminate the search: the signature is not present in the table.
11474
11475 4) Let H = (H + H') modulo M. Repeat at Step 3.
11476
11477 Because M > N and H' and M are relatively prime, the search is guaranteed
11478 to stop at an unused slot or find the match. */
11479
11480 /* Create a hash table to map DWO IDs to their CU/TU entry in
11481 .debug_{info,types}.dwo in DWP_FILE.
11482 Returns NULL if there isn't one.
11483 Note: This function processes DWP files only, not DWO files. */
11484
11485 static struct dwp_hash_table *
11486 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11487 struct dwp_file *dwp_file, int is_debug_types)
11488 {
11489 struct objfile *objfile = dwarf2_per_objfile->objfile;
11490 bfd *dbfd = dwp_file->dbfd.get ();
11491 const gdb_byte *index_ptr, *index_end;
11492 struct dwarf2_section_info *index;
11493 uint32_t version, nr_columns, nr_units, nr_slots;
11494 struct dwp_hash_table *htab;
11495
11496 if (is_debug_types)
11497 index = &dwp_file->sections.tu_index;
11498 else
11499 index = &dwp_file->sections.cu_index;
11500
11501 if (index->empty ())
11502 return NULL;
11503 index->read (objfile);
11504
11505 index_ptr = index->buffer;
11506 index_end = index_ptr + index->size;
11507
11508 version = read_4_bytes (dbfd, index_ptr);
11509 index_ptr += 4;
11510 if (version == 2)
11511 nr_columns = read_4_bytes (dbfd, index_ptr);
11512 else
11513 nr_columns = 0;
11514 index_ptr += 4;
11515 nr_units = read_4_bytes (dbfd, index_ptr);
11516 index_ptr += 4;
11517 nr_slots = read_4_bytes (dbfd, index_ptr);
11518 index_ptr += 4;
11519
11520 if (version != 1 && version != 2)
11521 {
11522 error (_("Dwarf Error: unsupported DWP file version (%s)"
11523 " [in module %s]"),
11524 pulongest (version), dwp_file->name);
11525 }
11526 if (nr_slots != (nr_slots & -nr_slots))
11527 {
11528 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11529 " is not power of 2 [in module %s]"),
11530 pulongest (nr_slots), dwp_file->name);
11531 }
11532
11533 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
11534 htab->version = version;
11535 htab->nr_columns = nr_columns;
11536 htab->nr_units = nr_units;
11537 htab->nr_slots = nr_slots;
11538 htab->hash_table = index_ptr;
11539 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11540
11541 /* Exit early if the table is empty. */
11542 if (nr_slots == 0 || nr_units == 0
11543 || (version == 2 && nr_columns == 0))
11544 {
11545 /* All must be zero. */
11546 if (nr_slots != 0 || nr_units != 0
11547 || (version == 2 && nr_columns != 0))
11548 {
11549 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11550 " all zero [in modules %s]"),
11551 dwp_file->name);
11552 }
11553 return htab;
11554 }
11555
11556 if (version == 1)
11557 {
11558 htab->section_pool.v1.indices =
11559 htab->unit_table + sizeof (uint32_t) * nr_slots;
11560 /* It's harder to decide whether the section is too small in v1.
11561 V1 is deprecated anyway so we punt. */
11562 }
11563 else
11564 {
11565 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11566 int *ids = htab->section_pool.v2.section_ids;
11567 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11568 /* Reverse map for error checking. */
11569 int ids_seen[DW_SECT_MAX + 1];
11570 int i;
11571
11572 if (nr_columns < 2)
11573 {
11574 error (_("Dwarf Error: bad DWP hash table, too few columns"
11575 " in section table [in module %s]"),
11576 dwp_file->name);
11577 }
11578 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11579 {
11580 error (_("Dwarf Error: bad DWP hash table, too many columns"
11581 " in section table [in module %s]"),
11582 dwp_file->name);
11583 }
11584 memset (ids, 255, sizeof_ids);
11585 memset (ids_seen, 255, sizeof (ids_seen));
11586 for (i = 0; i < nr_columns; ++i)
11587 {
11588 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11589
11590 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11591 {
11592 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11593 " in section table [in module %s]"),
11594 id, dwp_file->name);
11595 }
11596 if (ids_seen[id] != -1)
11597 {
11598 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11599 " id %d in section table [in module %s]"),
11600 id, dwp_file->name);
11601 }
11602 ids_seen[id] = i;
11603 ids[i] = id;
11604 }
11605 /* Must have exactly one info or types section. */
11606 if (((ids_seen[DW_SECT_INFO] != -1)
11607 + (ids_seen[DW_SECT_TYPES] != -1))
11608 != 1)
11609 {
11610 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11611 " DWO info/types section [in module %s]"),
11612 dwp_file->name);
11613 }
11614 /* Must have an abbrev section. */
11615 if (ids_seen[DW_SECT_ABBREV] == -1)
11616 {
11617 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11618 " section [in module %s]"),
11619 dwp_file->name);
11620 }
11621 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11622 htab->section_pool.v2.sizes =
11623 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11624 * nr_units * nr_columns);
11625 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11626 * nr_units * nr_columns))
11627 > index_end)
11628 {
11629 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11630 " [in module %s]"),
11631 dwp_file->name);
11632 }
11633 }
11634
11635 return htab;
11636 }
11637
11638 /* Update SECTIONS with the data from SECTP.
11639
11640 This function is like the other "locate" section routines that are
11641 passed to bfd_map_over_sections, but in this context the sections to
11642 read comes from the DWP V1 hash table, not the full ELF section table.
11643
11644 The result is non-zero for success, or zero if an error was found. */
11645
11646 static int
11647 locate_v1_virtual_dwo_sections (asection *sectp,
11648 struct virtual_v1_dwo_sections *sections)
11649 {
11650 const struct dwop_section_names *names = &dwop_section_names;
11651
11652 if (section_is_p (sectp->name, &names->abbrev_dwo))
11653 {
11654 /* There can be only one. */
11655 if (sections->abbrev.s.section != NULL)
11656 return 0;
11657 sections->abbrev.s.section = sectp;
11658 sections->abbrev.size = bfd_section_size (sectp);
11659 }
11660 else if (section_is_p (sectp->name, &names->info_dwo)
11661 || section_is_p (sectp->name, &names->types_dwo))
11662 {
11663 /* There can be only one. */
11664 if (sections->info_or_types.s.section != NULL)
11665 return 0;
11666 sections->info_or_types.s.section = sectp;
11667 sections->info_or_types.size = bfd_section_size (sectp);
11668 }
11669 else if (section_is_p (sectp->name, &names->line_dwo))
11670 {
11671 /* There can be only one. */
11672 if (sections->line.s.section != NULL)
11673 return 0;
11674 sections->line.s.section = sectp;
11675 sections->line.size = bfd_section_size (sectp);
11676 }
11677 else if (section_is_p (sectp->name, &names->loc_dwo))
11678 {
11679 /* There can be only one. */
11680 if (sections->loc.s.section != NULL)
11681 return 0;
11682 sections->loc.s.section = sectp;
11683 sections->loc.size = bfd_section_size (sectp);
11684 }
11685 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11686 {
11687 /* There can be only one. */
11688 if (sections->macinfo.s.section != NULL)
11689 return 0;
11690 sections->macinfo.s.section = sectp;
11691 sections->macinfo.size = bfd_section_size (sectp);
11692 }
11693 else if (section_is_p (sectp->name, &names->macro_dwo))
11694 {
11695 /* There can be only one. */
11696 if (sections->macro.s.section != NULL)
11697 return 0;
11698 sections->macro.s.section = sectp;
11699 sections->macro.size = bfd_section_size (sectp);
11700 }
11701 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11702 {
11703 /* There can be only one. */
11704 if (sections->str_offsets.s.section != NULL)
11705 return 0;
11706 sections->str_offsets.s.section = sectp;
11707 sections->str_offsets.size = bfd_section_size (sectp);
11708 }
11709 else
11710 {
11711 /* No other kind of section is valid. */
11712 return 0;
11713 }
11714
11715 return 1;
11716 }
11717
11718 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11719 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11720 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11721 This is for DWP version 1 files. */
11722
11723 static struct dwo_unit *
11724 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11725 struct dwp_file *dwp_file,
11726 uint32_t unit_index,
11727 const char *comp_dir,
11728 ULONGEST signature, int is_debug_types)
11729 {
11730 const struct dwp_hash_table *dwp_htab =
11731 is_debug_types ? dwp_file->tus : dwp_file->cus;
11732 bfd *dbfd = dwp_file->dbfd.get ();
11733 const char *kind = is_debug_types ? "TU" : "CU";
11734 struct dwo_file *dwo_file;
11735 struct dwo_unit *dwo_unit;
11736 struct virtual_v1_dwo_sections sections;
11737 void **dwo_file_slot;
11738 int i;
11739
11740 gdb_assert (dwp_file->version == 1);
11741
11742 if (dwarf_read_debug)
11743 {
11744 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11745 kind,
11746 pulongest (unit_index), hex_string (signature),
11747 dwp_file->name);
11748 }
11749
11750 /* Fetch the sections of this DWO unit.
11751 Put a limit on the number of sections we look for so that bad data
11752 doesn't cause us to loop forever. */
11753
11754 #define MAX_NR_V1_DWO_SECTIONS \
11755 (1 /* .debug_info or .debug_types */ \
11756 + 1 /* .debug_abbrev */ \
11757 + 1 /* .debug_line */ \
11758 + 1 /* .debug_loc */ \
11759 + 1 /* .debug_str_offsets */ \
11760 + 1 /* .debug_macro or .debug_macinfo */ \
11761 + 1 /* trailing zero */)
11762
11763 memset (&sections, 0, sizeof (sections));
11764
11765 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11766 {
11767 asection *sectp;
11768 uint32_t section_nr =
11769 read_4_bytes (dbfd,
11770 dwp_htab->section_pool.v1.indices
11771 + (unit_index + i) * sizeof (uint32_t));
11772
11773 if (section_nr == 0)
11774 break;
11775 if (section_nr >= dwp_file->num_sections)
11776 {
11777 error (_("Dwarf Error: bad DWP hash table, section number too large"
11778 " [in module %s]"),
11779 dwp_file->name);
11780 }
11781
11782 sectp = dwp_file->elf_sections[section_nr];
11783 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11784 {
11785 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11786 " [in module %s]"),
11787 dwp_file->name);
11788 }
11789 }
11790
11791 if (i < 2
11792 || sections.info_or_types.empty ()
11793 || sections.abbrev.empty ())
11794 {
11795 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11796 " [in module %s]"),
11797 dwp_file->name);
11798 }
11799 if (i == MAX_NR_V1_DWO_SECTIONS)
11800 {
11801 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11802 " [in module %s]"),
11803 dwp_file->name);
11804 }
11805
11806 /* It's easier for the rest of the code if we fake a struct dwo_file and
11807 have dwo_unit "live" in that. At least for now.
11808
11809 The DWP file can be made up of a random collection of CUs and TUs.
11810 However, for each CU + set of TUs that came from the same original DWO
11811 file, we can combine them back into a virtual DWO file to save space
11812 (fewer struct dwo_file objects to allocate). Remember that for really
11813 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11814
11815 std::string virtual_dwo_name =
11816 string_printf ("virtual-dwo/%d-%d-%d-%d",
11817 sections.abbrev.get_id (),
11818 sections.line.get_id (),
11819 sections.loc.get_id (),
11820 sections.str_offsets.get_id ());
11821 /* Can we use an existing virtual DWO file? */
11822 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11823 virtual_dwo_name.c_str (),
11824 comp_dir);
11825 /* Create one if necessary. */
11826 if (*dwo_file_slot == NULL)
11827 {
11828 if (dwarf_read_debug)
11829 {
11830 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11831 virtual_dwo_name.c_str ());
11832 }
11833 dwo_file = new struct dwo_file;
11834 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
11835 dwo_file->comp_dir = comp_dir;
11836 dwo_file->sections.abbrev = sections.abbrev;
11837 dwo_file->sections.line = sections.line;
11838 dwo_file->sections.loc = sections.loc;
11839 dwo_file->sections.macinfo = sections.macinfo;
11840 dwo_file->sections.macro = sections.macro;
11841 dwo_file->sections.str_offsets = sections.str_offsets;
11842 /* The "str" section is global to the entire DWP file. */
11843 dwo_file->sections.str = dwp_file->sections.str;
11844 /* The info or types section is assigned below to dwo_unit,
11845 there's no need to record it in dwo_file.
11846 Also, we can't simply record type sections in dwo_file because
11847 we record a pointer into the vector in dwo_unit. As we collect more
11848 types we'll grow the vector and eventually have to reallocate space
11849 for it, invalidating all copies of pointers into the previous
11850 contents. */
11851 *dwo_file_slot = dwo_file;
11852 }
11853 else
11854 {
11855 if (dwarf_read_debug)
11856 {
11857 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11858 virtual_dwo_name.c_str ());
11859 }
11860 dwo_file = (struct dwo_file *) *dwo_file_slot;
11861 }
11862
11863 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
11864 dwo_unit->dwo_file = dwo_file;
11865 dwo_unit->signature = signature;
11866 dwo_unit->section =
11867 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11868 *dwo_unit->section = sections.info_or_types;
11869 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11870
11871 return dwo_unit;
11872 }
11873
11874 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11875 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11876 piece within that section used by a TU/CU, return a virtual section
11877 of just that piece. */
11878
11879 static struct dwarf2_section_info
11880 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11881 struct dwarf2_section_info *section,
11882 bfd_size_type offset, bfd_size_type size)
11883 {
11884 struct dwarf2_section_info result;
11885 asection *sectp;
11886
11887 gdb_assert (section != NULL);
11888 gdb_assert (!section->is_virtual);
11889
11890 memset (&result, 0, sizeof (result));
11891 result.s.containing_section = section;
11892 result.is_virtual = true;
11893
11894 if (size == 0)
11895 return result;
11896
11897 sectp = section->get_bfd_section ();
11898
11899 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11900 bounds of the real section. This is a pretty-rare event, so just
11901 flag an error (easier) instead of a warning and trying to cope. */
11902 if (sectp == NULL
11903 || offset + size > bfd_section_size (sectp))
11904 {
11905 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11906 " in section %s [in module %s]"),
11907 sectp ? bfd_section_name (sectp) : "<unknown>",
11908 objfile_name (dwarf2_per_objfile->objfile));
11909 }
11910
11911 result.virtual_offset = offset;
11912 result.size = size;
11913 return result;
11914 }
11915
11916 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11917 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11918 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11919 This is for DWP version 2 files. */
11920
11921 static struct dwo_unit *
11922 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11923 struct dwp_file *dwp_file,
11924 uint32_t unit_index,
11925 const char *comp_dir,
11926 ULONGEST signature, int is_debug_types)
11927 {
11928 const struct dwp_hash_table *dwp_htab =
11929 is_debug_types ? dwp_file->tus : dwp_file->cus;
11930 bfd *dbfd = dwp_file->dbfd.get ();
11931 const char *kind = is_debug_types ? "TU" : "CU";
11932 struct dwo_file *dwo_file;
11933 struct dwo_unit *dwo_unit;
11934 struct virtual_v2_dwo_sections sections;
11935 void **dwo_file_slot;
11936 int i;
11937
11938 gdb_assert (dwp_file->version == 2);
11939
11940 if (dwarf_read_debug)
11941 {
11942 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11943 kind,
11944 pulongest (unit_index), hex_string (signature),
11945 dwp_file->name);
11946 }
11947
11948 /* Fetch the section offsets of this DWO unit. */
11949
11950 memset (&sections, 0, sizeof (sections));
11951
11952 for (i = 0; i < dwp_htab->nr_columns; ++i)
11953 {
11954 uint32_t offset = read_4_bytes (dbfd,
11955 dwp_htab->section_pool.v2.offsets
11956 + (((unit_index - 1) * dwp_htab->nr_columns
11957 + i)
11958 * sizeof (uint32_t)));
11959 uint32_t size = read_4_bytes (dbfd,
11960 dwp_htab->section_pool.v2.sizes
11961 + (((unit_index - 1) * dwp_htab->nr_columns
11962 + i)
11963 * sizeof (uint32_t)));
11964
11965 switch (dwp_htab->section_pool.v2.section_ids[i])
11966 {
11967 case DW_SECT_INFO:
11968 case DW_SECT_TYPES:
11969 sections.info_or_types_offset = offset;
11970 sections.info_or_types_size = size;
11971 break;
11972 case DW_SECT_ABBREV:
11973 sections.abbrev_offset = offset;
11974 sections.abbrev_size = size;
11975 break;
11976 case DW_SECT_LINE:
11977 sections.line_offset = offset;
11978 sections.line_size = size;
11979 break;
11980 case DW_SECT_LOC:
11981 sections.loc_offset = offset;
11982 sections.loc_size = size;
11983 break;
11984 case DW_SECT_STR_OFFSETS:
11985 sections.str_offsets_offset = offset;
11986 sections.str_offsets_size = size;
11987 break;
11988 case DW_SECT_MACINFO:
11989 sections.macinfo_offset = offset;
11990 sections.macinfo_size = size;
11991 break;
11992 case DW_SECT_MACRO:
11993 sections.macro_offset = offset;
11994 sections.macro_size = size;
11995 break;
11996 }
11997 }
11998
11999 /* It's easier for the rest of the code if we fake a struct dwo_file and
12000 have dwo_unit "live" in that. At least for now.
12001
12002 The DWP file can be made up of a random collection of CUs and TUs.
12003 However, for each CU + set of TUs that came from the same original DWO
12004 file, we can combine them back into a virtual DWO file to save space
12005 (fewer struct dwo_file objects to allocate). Remember that for really
12006 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12007
12008 std::string virtual_dwo_name =
12009 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12010 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12011 (long) (sections.line_size ? sections.line_offset : 0),
12012 (long) (sections.loc_size ? sections.loc_offset : 0),
12013 (long) (sections.str_offsets_size
12014 ? sections.str_offsets_offset : 0));
12015 /* Can we use an existing virtual DWO file? */
12016 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12017 virtual_dwo_name.c_str (),
12018 comp_dir);
12019 /* Create one if necessary. */
12020 if (*dwo_file_slot == NULL)
12021 {
12022 if (dwarf_read_debug)
12023 {
12024 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12025 virtual_dwo_name.c_str ());
12026 }
12027 dwo_file = new struct dwo_file;
12028 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
12029 dwo_file->comp_dir = comp_dir;
12030 dwo_file->sections.abbrev =
12031 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12032 sections.abbrev_offset, sections.abbrev_size);
12033 dwo_file->sections.line =
12034 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12035 sections.line_offset, sections.line_size);
12036 dwo_file->sections.loc =
12037 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12038 sections.loc_offset, sections.loc_size);
12039 dwo_file->sections.macinfo =
12040 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12041 sections.macinfo_offset, sections.macinfo_size);
12042 dwo_file->sections.macro =
12043 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12044 sections.macro_offset, sections.macro_size);
12045 dwo_file->sections.str_offsets =
12046 create_dwp_v2_section (dwarf2_per_objfile,
12047 &dwp_file->sections.str_offsets,
12048 sections.str_offsets_offset,
12049 sections.str_offsets_size);
12050 /* The "str" section is global to the entire DWP file. */
12051 dwo_file->sections.str = dwp_file->sections.str;
12052 /* The info or types section is assigned below to dwo_unit,
12053 there's no need to record it in dwo_file.
12054 Also, we can't simply record type sections in dwo_file because
12055 we record a pointer into the vector in dwo_unit. As we collect more
12056 types we'll grow the vector and eventually have to reallocate space
12057 for it, invalidating all copies of pointers into the previous
12058 contents. */
12059 *dwo_file_slot = dwo_file;
12060 }
12061 else
12062 {
12063 if (dwarf_read_debug)
12064 {
12065 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12066 virtual_dwo_name.c_str ());
12067 }
12068 dwo_file = (struct dwo_file *) *dwo_file_slot;
12069 }
12070
12071 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
12072 dwo_unit->dwo_file = dwo_file;
12073 dwo_unit->signature = signature;
12074 dwo_unit->section =
12075 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12076 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12077 is_debug_types
12078 ? &dwp_file->sections.types
12079 : &dwp_file->sections.info,
12080 sections.info_or_types_offset,
12081 sections.info_or_types_size);
12082 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12083
12084 return dwo_unit;
12085 }
12086
12087 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12088 Returns NULL if the signature isn't found. */
12089
12090 static struct dwo_unit *
12091 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12092 struct dwp_file *dwp_file, const char *comp_dir,
12093 ULONGEST signature, int is_debug_types)
12094 {
12095 const struct dwp_hash_table *dwp_htab =
12096 is_debug_types ? dwp_file->tus : dwp_file->cus;
12097 bfd *dbfd = dwp_file->dbfd.get ();
12098 uint32_t mask = dwp_htab->nr_slots - 1;
12099 uint32_t hash = signature & mask;
12100 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12101 unsigned int i;
12102 void **slot;
12103 struct dwo_unit find_dwo_cu;
12104
12105 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12106 find_dwo_cu.signature = signature;
12107 slot = htab_find_slot (is_debug_types
12108 ? dwp_file->loaded_tus.get ()
12109 : dwp_file->loaded_cus.get (),
12110 &find_dwo_cu, INSERT);
12111
12112 if (*slot != NULL)
12113 return (struct dwo_unit *) *slot;
12114
12115 /* Use a for loop so that we don't loop forever on bad debug info. */
12116 for (i = 0; i < dwp_htab->nr_slots; ++i)
12117 {
12118 ULONGEST signature_in_table;
12119
12120 signature_in_table =
12121 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12122 if (signature_in_table == signature)
12123 {
12124 uint32_t unit_index =
12125 read_4_bytes (dbfd,
12126 dwp_htab->unit_table + hash * sizeof (uint32_t));
12127
12128 if (dwp_file->version == 1)
12129 {
12130 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12131 dwp_file, unit_index,
12132 comp_dir, signature,
12133 is_debug_types);
12134 }
12135 else
12136 {
12137 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12138 dwp_file, unit_index,
12139 comp_dir, signature,
12140 is_debug_types);
12141 }
12142 return (struct dwo_unit *) *slot;
12143 }
12144 if (signature_in_table == 0)
12145 return NULL;
12146 hash = (hash + hash2) & mask;
12147 }
12148
12149 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12150 " [in module %s]"),
12151 dwp_file->name);
12152 }
12153
12154 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12155 Open the file specified by FILE_NAME and hand it off to BFD for
12156 preliminary analysis. Return a newly initialized bfd *, which
12157 includes a canonicalized copy of FILE_NAME.
12158 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12159 SEARCH_CWD is true if the current directory is to be searched.
12160 It will be searched before debug-file-directory.
12161 If successful, the file is added to the bfd include table of the
12162 objfile's bfd (see gdb_bfd_record_inclusion).
12163 If unable to find/open the file, return NULL.
12164 NOTE: This function is derived from symfile_bfd_open. */
12165
12166 static gdb_bfd_ref_ptr
12167 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12168 const char *file_name, int is_dwp, int search_cwd)
12169 {
12170 int desc;
12171 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12172 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12173 to debug_file_directory. */
12174 const char *search_path;
12175 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12176
12177 gdb::unique_xmalloc_ptr<char> search_path_holder;
12178 if (search_cwd)
12179 {
12180 if (*debug_file_directory != '\0')
12181 {
12182 search_path_holder.reset (concat (".", dirname_separator_string,
12183 debug_file_directory,
12184 (char *) NULL));
12185 search_path = search_path_holder.get ();
12186 }
12187 else
12188 search_path = ".";
12189 }
12190 else
12191 search_path = debug_file_directory;
12192
12193 openp_flags flags = OPF_RETURN_REALPATH;
12194 if (is_dwp)
12195 flags |= OPF_SEARCH_IN_PATH;
12196
12197 gdb::unique_xmalloc_ptr<char> absolute_name;
12198 desc = openp (search_path, flags, file_name,
12199 O_RDONLY | O_BINARY, &absolute_name);
12200 if (desc < 0)
12201 return NULL;
12202
12203 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12204 gnutarget, desc));
12205 if (sym_bfd == NULL)
12206 return NULL;
12207 bfd_set_cacheable (sym_bfd.get (), 1);
12208
12209 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12210 return NULL;
12211
12212 /* Success. Record the bfd as having been included by the objfile's bfd.
12213 This is important because things like demangled_names_hash lives in the
12214 objfile's per_bfd space and may have references to things like symbol
12215 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12216 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12217
12218 return sym_bfd;
12219 }
12220
12221 /* Try to open DWO file FILE_NAME.
12222 COMP_DIR is the DW_AT_comp_dir attribute.
12223 The result is the bfd handle of the file.
12224 If there is a problem finding or opening the file, return NULL.
12225 Upon success, the canonicalized path of the file is stored in the bfd,
12226 same as symfile_bfd_open. */
12227
12228 static gdb_bfd_ref_ptr
12229 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12230 const char *file_name, const char *comp_dir)
12231 {
12232 if (IS_ABSOLUTE_PATH (file_name))
12233 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12234 0 /*is_dwp*/, 0 /*search_cwd*/);
12235
12236 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12237
12238 if (comp_dir != NULL)
12239 {
12240 gdb::unique_xmalloc_ptr<char> path_to_try
12241 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12242
12243 /* NOTE: If comp_dir is a relative path, this will also try the
12244 search path, which seems useful. */
12245 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12246 path_to_try.get (),
12247 0 /*is_dwp*/,
12248 1 /*search_cwd*/));
12249 if (abfd != NULL)
12250 return abfd;
12251 }
12252
12253 /* That didn't work, try debug-file-directory, which, despite its name,
12254 is a list of paths. */
12255
12256 if (*debug_file_directory == '\0')
12257 return NULL;
12258
12259 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12260 0 /*is_dwp*/, 1 /*search_cwd*/);
12261 }
12262
12263 /* This function is mapped across the sections and remembers the offset and
12264 size of each of the DWO debugging sections we are interested in. */
12265
12266 static void
12267 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12268 {
12269 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12270 const struct dwop_section_names *names = &dwop_section_names;
12271
12272 if (section_is_p (sectp->name, &names->abbrev_dwo))
12273 {
12274 dwo_sections->abbrev.s.section = sectp;
12275 dwo_sections->abbrev.size = bfd_section_size (sectp);
12276 }
12277 else if (section_is_p (sectp->name, &names->info_dwo))
12278 {
12279 dwo_sections->info.s.section = sectp;
12280 dwo_sections->info.size = bfd_section_size (sectp);
12281 }
12282 else if (section_is_p (sectp->name, &names->line_dwo))
12283 {
12284 dwo_sections->line.s.section = sectp;
12285 dwo_sections->line.size = bfd_section_size (sectp);
12286 }
12287 else if (section_is_p (sectp->name, &names->loc_dwo))
12288 {
12289 dwo_sections->loc.s.section = sectp;
12290 dwo_sections->loc.size = bfd_section_size (sectp);
12291 }
12292 else if (section_is_p (sectp->name, &names->loclists_dwo))
12293 {
12294 dwo_sections->loclists.s.section = sectp;
12295 dwo_sections->loclists.size = bfd_section_size (sectp);
12296 }
12297 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12298 {
12299 dwo_sections->macinfo.s.section = sectp;
12300 dwo_sections->macinfo.size = bfd_section_size (sectp);
12301 }
12302 else if (section_is_p (sectp->name, &names->macro_dwo))
12303 {
12304 dwo_sections->macro.s.section = sectp;
12305 dwo_sections->macro.size = bfd_section_size (sectp);
12306 }
12307 else if (section_is_p (sectp->name, &names->str_dwo))
12308 {
12309 dwo_sections->str.s.section = sectp;
12310 dwo_sections->str.size = bfd_section_size (sectp);
12311 }
12312 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12313 {
12314 dwo_sections->str_offsets.s.section = sectp;
12315 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12316 }
12317 else if (section_is_p (sectp->name, &names->types_dwo))
12318 {
12319 struct dwarf2_section_info type_section;
12320
12321 memset (&type_section, 0, sizeof (type_section));
12322 type_section.s.section = sectp;
12323 type_section.size = bfd_section_size (sectp);
12324 dwo_sections->types.push_back (type_section);
12325 }
12326 }
12327
12328 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12329 by PER_CU. This is for the non-DWP case.
12330 The result is NULL if DWO_NAME can't be found. */
12331
12332 static struct dwo_file *
12333 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12334 const char *dwo_name, const char *comp_dir)
12335 {
12336 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12337
12338 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12339 if (dbfd == NULL)
12340 {
12341 if (dwarf_read_debug)
12342 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12343 return NULL;
12344 }
12345
12346 dwo_file_up dwo_file (new struct dwo_file);
12347 dwo_file->dwo_name = dwo_name;
12348 dwo_file->comp_dir = comp_dir;
12349 dwo_file->dbfd = std::move (dbfd);
12350
12351 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12352 &dwo_file->sections);
12353
12354 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12355 dwo_file->sections.info, dwo_file->cus);
12356
12357 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12358 dwo_file->sections.types, dwo_file->tus);
12359
12360 if (dwarf_read_debug)
12361 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12362
12363 return dwo_file.release ();
12364 }
12365
12366 /* This function is mapped across the sections and remembers the offset and
12367 size of each of the DWP debugging sections common to version 1 and 2 that
12368 we are interested in. */
12369
12370 static void
12371 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12372 void *dwp_file_ptr)
12373 {
12374 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12375 const struct dwop_section_names *names = &dwop_section_names;
12376 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12377
12378 /* Record the ELF section number for later lookup: this is what the
12379 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12380 gdb_assert (elf_section_nr < dwp_file->num_sections);
12381 dwp_file->elf_sections[elf_section_nr] = sectp;
12382
12383 /* Look for specific sections that we need. */
12384 if (section_is_p (sectp->name, &names->str_dwo))
12385 {
12386 dwp_file->sections.str.s.section = sectp;
12387 dwp_file->sections.str.size = bfd_section_size (sectp);
12388 }
12389 else if (section_is_p (sectp->name, &names->cu_index))
12390 {
12391 dwp_file->sections.cu_index.s.section = sectp;
12392 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12393 }
12394 else if (section_is_p (sectp->name, &names->tu_index))
12395 {
12396 dwp_file->sections.tu_index.s.section = sectp;
12397 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12398 }
12399 }
12400
12401 /* This function is mapped across the sections and remembers the offset and
12402 size of each of the DWP version 2 debugging sections that we are interested
12403 in. This is split into a separate function because we don't know if we
12404 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12405
12406 static void
12407 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12408 {
12409 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12410 const struct dwop_section_names *names = &dwop_section_names;
12411 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12412
12413 /* Record the ELF section number for later lookup: this is what the
12414 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12415 gdb_assert (elf_section_nr < dwp_file->num_sections);
12416 dwp_file->elf_sections[elf_section_nr] = sectp;
12417
12418 /* Look for specific sections that we need. */
12419 if (section_is_p (sectp->name, &names->abbrev_dwo))
12420 {
12421 dwp_file->sections.abbrev.s.section = sectp;
12422 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12423 }
12424 else if (section_is_p (sectp->name, &names->info_dwo))
12425 {
12426 dwp_file->sections.info.s.section = sectp;
12427 dwp_file->sections.info.size = bfd_section_size (sectp);
12428 }
12429 else if (section_is_p (sectp->name, &names->line_dwo))
12430 {
12431 dwp_file->sections.line.s.section = sectp;
12432 dwp_file->sections.line.size = bfd_section_size (sectp);
12433 }
12434 else if (section_is_p (sectp->name, &names->loc_dwo))
12435 {
12436 dwp_file->sections.loc.s.section = sectp;
12437 dwp_file->sections.loc.size = bfd_section_size (sectp);
12438 }
12439 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12440 {
12441 dwp_file->sections.macinfo.s.section = sectp;
12442 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12443 }
12444 else if (section_is_p (sectp->name, &names->macro_dwo))
12445 {
12446 dwp_file->sections.macro.s.section = sectp;
12447 dwp_file->sections.macro.size = bfd_section_size (sectp);
12448 }
12449 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12450 {
12451 dwp_file->sections.str_offsets.s.section = sectp;
12452 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12453 }
12454 else if (section_is_p (sectp->name, &names->types_dwo))
12455 {
12456 dwp_file->sections.types.s.section = sectp;
12457 dwp_file->sections.types.size = bfd_section_size (sectp);
12458 }
12459 }
12460
12461 /* Hash function for dwp_file loaded CUs/TUs. */
12462
12463 static hashval_t
12464 hash_dwp_loaded_cutus (const void *item)
12465 {
12466 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12467
12468 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12469 return dwo_unit->signature;
12470 }
12471
12472 /* Equality function for dwp_file loaded CUs/TUs. */
12473
12474 static int
12475 eq_dwp_loaded_cutus (const void *a, const void *b)
12476 {
12477 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12478 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12479
12480 return dua->signature == dub->signature;
12481 }
12482
12483 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12484
12485 static htab_up
12486 allocate_dwp_loaded_cutus_table ()
12487 {
12488 return htab_up (htab_create_alloc (3,
12489 hash_dwp_loaded_cutus,
12490 eq_dwp_loaded_cutus,
12491 NULL, xcalloc, xfree));
12492 }
12493
12494 /* Try to open DWP file FILE_NAME.
12495 The result is the bfd handle of the file.
12496 If there is a problem finding or opening the file, return NULL.
12497 Upon success, the canonicalized path of the file is stored in the bfd,
12498 same as symfile_bfd_open. */
12499
12500 static gdb_bfd_ref_ptr
12501 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12502 const char *file_name)
12503 {
12504 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12505 1 /*is_dwp*/,
12506 1 /*search_cwd*/));
12507 if (abfd != NULL)
12508 return abfd;
12509
12510 /* Work around upstream bug 15652.
12511 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12512 [Whether that's a "bug" is debatable, but it is getting in our way.]
12513 We have no real idea where the dwp file is, because gdb's realpath-ing
12514 of the executable's path may have discarded the needed info.
12515 [IWBN if the dwp file name was recorded in the executable, akin to
12516 .gnu_debuglink, but that doesn't exist yet.]
12517 Strip the directory from FILE_NAME and search again. */
12518 if (*debug_file_directory != '\0')
12519 {
12520 /* Don't implicitly search the current directory here.
12521 If the user wants to search "." to handle this case,
12522 it must be added to debug-file-directory. */
12523 return try_open_dwop_file (dwarf2_per_objfile,
12524 lbasename (file_name), 1 /*is_dwp*/,
12525 0 /*search_cwd*/);
12526 }
12527
12528 return NULL;
12529 }
12530
12531 /* Initialize the use of the DWP file for the current objfile.
12532 By convention the name of the DWP file is ${objfile}.dwp.
12533 The result is NULL if it can't be found. */
12534
12535 static std::unique_ptr<struct dwp_file>
12536 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12537 {
12538 struct objfile *objfile = dwarf2_per_objfile->objfile;
12539
12540 /* Try to find first .dwp for the binary file before any symbolic links
12541 resolving. */
12542
12543 /* If the objfile is a debug file, find the name of the real binary
12544 file and get the name of dwp file from there. */
12545 std::string dwp_name;
12546 if (objfile->separate_debug_objfile_backlink != NULL)
12547 {
12548 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12549 const char *backlink_basename = lbasename (backlink->original_name);
12550
12551 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12552 }
12553 else
12554 dwp_name = objfile->original_name;
12555
12556 dwp_name += ".dwp";
12557
12558 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12559 if (dbfd == NULL
12560 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12561 {
12562 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12563 dwp_name = objfile_name (objfile);
12564 dwp_name += ".dwp";
12565 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12566 }
12567
12568 if (dbfd == NULL)
12569 {
12570 if (dwarf_read_debug)
12571 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12572 return std::unique_ptr<dwp_file> ();
12573 }
12574
12575 const char *name = bfd_get_filename (dbfd.get ());
12576 std::unique_ptr<struct dwp_file> dwp_file
12577 (new struct dwp_file (name, std::move (dbfd)));
12578
12579 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12580 dwp_file->elf_sections =
12581 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
12582 dwp_file->num_sections, asection *);
12583
12584 bfd_map_over_sections (dwp_file->dbfd.get (),
12585 dwarf2_locate_common_dwp_sections,
12586 dwp_file.get ());
12587
12588 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12589 0);
12590
12591 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12592 1);
12593
12594 /* The DWP file version is stored in the hash table. Oh well. */
12595 if (dwp_file->cus && dwp_file->tus
12596 && dwp_file->cus->version != dwp_file->tus->version)
12597 {
12598 /* Technically speaking, we should try to limp along, but this is
12599 pretty bizarre. We use pulongest here because that's the established
12600 portability solution (e.g, we cannot use %u for uint32_t). */
12601 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12602 " TU version %s [in DWP file %s]"),
12603 pulongest (dwp_file->cus->version),
12604 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12605 }
12606
12607 if (dwp_file->cus)
12608 dwp_file->version = dwp_file->cus->version;
12609 else if (dwp_file->tus)
12610 dwp_file->version = dwp_file->tus->version;
12611 else
12612 dwp_file->version = 2;
12613
12614 if (dwp_file->version == 2)
12615 bfd_map_over_sections (dwp_file->dbfd.get (),
12616 dwarf2_locate_v2_dwp_sections,
12617 dwp_file.get ());
12618
12619 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12620 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12621
12622 if (dwarf_read_debug)
12623 {
12624 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12625 fprintf_unfiltered (gdb_stdlog,
12626 " %s CUs, %s TUs\n",
12627 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12628 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12629 }
12630
12631 return dwp_file;
12632 }
12633
12634 /* Wrapper around open_and_init_dwp_file, only open it once. */
12635
12636 static struct dwp_file *
12637 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12638 {
12639 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
12640 {
12641 dwarf2_per_objfile->per_bfd->dwp_file
12642 = open_and_init_dwp_file (dwarf2_per_objfile);
12643 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
12644 }
12645 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
12646 }
12647
12648 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12649 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12650 or in the DWP file for the objfile, referenced by THIS_UNIT.
12651 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12652 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12653
12654 This is called, for example, when wanting to read a variable with a
12655 complex location. Therefore we don't want to do file i/o for every call.
12656 Therefore we don't want to look for a DWO file on every call.
12657 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12658 then we check if we've already seen DWO_NAME, and only THEN do we check
12659 for a DWO file.
12660
12661 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12662 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12663
12664 static struct dwo_unit *
12665 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12666 const char *dwo_name, const char *comp_dir,
12667 ULONGEST signature, int is_debug_types)
12668 {
12669 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12670 struct objfile *objfile = dwarf2_per_objfile->objfile;
12671 const char *kind = is_debug_types ? "TU" : "CU";
12672 void **dwo_file_slot;
12673 struct dwo_file *dwo_file;
12674 struct dwp_file *dwp_file;
12675
12676 /* First see if there's a DWP file.
12677 If we have a DWP file but didn't find the DWO inside it, don't
12678 look for the original DWO file. It makes gdb behave differently
12679 depending on whether one is debugging in the build tree. */
12680
12681 dwp_file = get_dwp_file (dwarf2_per_objfile);
12682 if (dwp_file != NULL)
12683 {
12684 const struct dwp_hash_table *dwp_htab =
12685 is_debug_types ? dwp_file->tus : dwp_file->cus;
12686
12687 if (dwp_htab != NULL)
12688 {
12689 struct dwo_unit *dwo_cutu =
12690 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12691 signature, is_debug_types);
12692
12693 if (dwo_cutu != NULL)
12694 {
12695 if (dwarf_read_debug)
12696 {
12697 fprintf_unfiltered (gdb_stdlog,
12698 "Virtual DWO %s %s found: @%s\n",
12699 kind, hex_string (signature),
12700 host_address_to_string (dwo_cutu));
12701 }
12702 return dwo_cutu;
12703 }
12704 }
12705 }
12706 else
12707 {
12708 /* No DWP file, look for the DWO file. */
12709
12710 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12711 dwo_name, comp_dir);
12712 if (*dwo_file_slot == NULL)
12713 {
12714 /* Read in the file and build a table of the CUs/TUs it contains. */
12715 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12716 }
12717 /* NOTE: This will be NULL if unable to open the file. */
12718 dwo_file = (struct dwo_file *) *dwo_file_slot;
12719
12720 if (dwo_file != NULL)
12721 {
12722 struct dwo_unit *dwo_cutu = NULL;
12723
12724 if (is_debug_types && dwo_file->tus)
12725 {
12726 struct dwo_unit find_dwo_cutu;
12727
12728 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12729 find_dwo_cutu.signature = signature;
12730 dwo_cutu
12731 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12732 &find_dwo_cutu);
12733 }
12734 else if (!is_debug_types && dwo_file->cus)
12735 {
12736 struct dwo_unit find_dwo_cutu;
12737
12738 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12739 find_dwo_cutu.signature = signature;
12740 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12741 &find_dwo_cutu);
12742 }
12743
12744 if (dwo_cutu != NULL)
12745 {
12746 if (dwarf_read_debug)
12747 {
12748 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12749 kind, dwo_name, hex_string (signature),
12750 host_address_to_string (dwo_cutu));
12751 }
12752 return dwo_cutu;
12753 }
12754 }
12755 }
12756
12757 /* We didn't find it. This could mean a dwo_id mismatch, or
12758 someone deleted the DWO/DWP file, or the search path isn't set up
12759 correctly to find the file. */
12760
12761 if (dwarf_read_debug)
12762 {
12763 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12764 kind, dwo_name, hex_string (signature));
12765 }
12766
12767 /* This is a warning and not a complaint because it can be caused by
12768 pilot error (e.g., user accidentally deleting the DWO). */
12769 {
12770 /* Print the name of the DWP file if we looked there, helps the user
12771 better diagnose the problem. */
12772 std::string dwp_text;
12773
12774 if (dwp_file != NULL)
12775 dwp_text = string_printf (" [in DWP file %s]",
12776 lbasename (dwp_file->name));
12777
12778 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12779 " [in module %s]"),
12780 kind, dwo_name, hex_string (signature),
12781 dwp_text.c_str (),
12782 this_unit->is_debug_types ? "TU" : "CU",
12783 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12784 }
12785 return NULL;
12786 }
12787
12788 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12789 See lookup_dwo_cutu_unit for details. */
12790
12791 static struct dwo_unit *
12792 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12793 const char *dwo_name, const char *comp_dir,
12794 ULONGEST signature)
12795 {
12796 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12797 }
12798
12799 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12800 See lookup_dwo_cutu_unit for details. */
12801
12802 static struct dwo_unit *
12803 lookup_dwo_type_unit (struct signatured_type *this_tu,
12804 const char *dwo_name, const char *comp_dir)
12805 {
12806 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12807 }
12808
12809 /* Traversal function for queue_and_load_all_dwo_tus. */
12810
12811 static int
12812 queue_and_load_dwo_tu (void **slot, void *info)
12813 {
12814 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12815 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12816 ULONGEST signature = dwo_unit->signature;
12817 struct signatured_type *sig_type =
12818 lookup_dwo_signatured_type (per_cu->cu, signature);
12819
12820 if (sig_type != NULL)
12821 {
12822 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12823
12824 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12825 a real dependency of PER_CU on SIG_TYPE. That is detected later
12826 while processing PER_CU. */
12827 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12828 load_full_type_unit (sig_cu);
12829 per_cu->imported_symtabs_push (sig_cu);
12830 }
12831
12832 return 1;
12833 }
12834
12835 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12836 The DWO may have the only definition of the type, though it may not be
12837 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12838 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12839
12840 static void
12841 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12842 {
12843 struct dwo_unit *dwo_unit;
12844 struct dwo_file *dwo_file;
12845
12846 gdb_assert (!per_cu->is_debug_types);
12847 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12848 gdb_assert (per_cu->cu != NULL);
12849
12850 dwo_unit = per_cu->cu->dwo_unit;
12851 gdb_assert (dwo_unit != NULL);
12852
12853 dwo_file = dwo_unit->dwo_file;
12854 if (dwo_file->tus != NULL)
12855 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12856 per_cu);
12857 }
12858
12859 /* Read in various DIEs. */
12860
12861 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12862 Inherit only the children of the DW_AT_abstract_origin DIE not being
12863 already referenced by DW_AT_abstract_origin from the children of the
12864 current DIE. */
12865
12866 static void
12867 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12868 {
12869 struct die_info *child_die;
12870 sect_offset *offsetp;
12871 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12872 struct die_info *origin_die;
12873 /* Iterator of the ORIGIN_DIE children. */
12874 struct die_info *origin_child_die;
12875 struct attribute *attr;
12876 struct dwarf2_cu *origin_cu;
12877 struct pending **origin_previous_list_in_scope;
12878
12879 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12880 if (!attr)
12881 return;
12882
12883 /* Note that following die references may follow to a die in a
12884 different cu. */
12885
12886 origin_cu = cu;
12887 origin_die = follow_die_ref (die, attr, &origin_cu);
12888
12889 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12890 symbols in. */
12891 origin_previous_list_in_scope = origin_cu->list_in_scope;
12892 origin_cu->list_in_scope = cu->list_in_scope;
12893
12894 if (die->tag != origin_die->tag
12895 && !(die->tag == DW_TAG_inlined_subroutine
12896 && origin_die->tag == DW_TAG_subprogram))
12897 complaint (_("DIE %s and its abstract origin %s have different tags"),
12898 sect_offset_str (die->sect_off),
12899 sect_offset_str (origin_die->sect_off));
12900
12901 std::vector<sect_offset> offsets;
12902
12903 for (child_die = die->child;
12904 child_die && child_die->tag;
12905 child_die = child_die->sibling)
12906 {
12907 struct die_info *child_origin_die;
12908 struct dwarf2_cu *child_origin_cu;
12909
12910 /* We are trying to process concrete instance entries:
12911 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12912 it's not relevant to our analysis here. i.e. detecting DIEs that are
12913 present in the abstract instance but not referenced in the concrete
12914 one. */
12915 if (child_die->tag == DW_TAG_call_site
12916 || child_die->tag == DW_TAG_GNU_call_site)
12917 continue;
12918
12919 /* For each CHILD_DIE, find the corresponding child of
12920 ORIGIN_DIE. If there is more than one layer of
12921 DW_AT_abstract_origin, follow them all; there shouldn't be,
12922 but GCC versions at least through 4.4 generate this (GCC PR
12923 40573). */
12924 child_origin_die = child_die;
12925 child_origin_cu = cu;
12926 while (1)
12927 {
12928 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12929 child_origin_cu);
12930 if (attr == NULL)
12931 break;
12932 child_origin_die = follow_die_ref (child_origin_die, attr,
12933 &child_origin_cu);
12934 }
12935
12936 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12937 counterpart may exist. */
12938 if (child_origin_die != child_die)
12939 {
12940 if (child_die->tag != child_origin_die->tag
12941 && !(child_die->tag == DW_TAG_inlined_subroutine
12942 && child_origin_die->tag == DW_TAG_subprogram))
12943 complaint (_("Child DIE %s and its abstract origin %s have "
12944 "different tags"),
12945 sect_offset_str (child_die->sect_off),
12946 sect_offset_str (child_origin_die->sect_off));
12947 if (child_origin_die->parent != origin_die)
12948 complaint (_("Child DIE %s and its abstract origin %s have "
12949 "different parents"),
12950 sect_offset_str (child_die->sect_off),
12951 sect_offset_str (child_origin_die->sect_off));
12952 else
12953 offsets.push_back (child_origin_die->sect_off);
12954 }
12955 }
12956 std::sort (offsets.begin (), offsets.end ());
12957 sect_offset *offsets_end = offsets.data () + offsets.size ();
12958 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12959 if (offsetp[-1] == *offsetp)
12960 complaint (_("Multiple children of DIE %s refer "
12961 "to DIE %s as their abstract origin"),
12962 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12963
12964 offsetp = offsets.data ();
12965 origin_child_die = origin_die->child;
12966 while (origin_child_die && origin_child_die->tag)
12967 {
12968 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12969 while (offsetp < offsets_end
12970 && *offsetp < origin_child_die->sect_off)
12971 offsetp++;
12972 if (offsetp >= offsets_end
12973 || *offsetp > origin_child_die->sect_off)
12974 {
12975 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12976 Check whether we're already processing ORIGIN_CHILD_DIE.
12977 This can happen with mutually referenced abstract_origins.
12978 PR 16581. */
12979 if (!origin_child_die->in_process)
12980 process_die (origin_child_die, origin_cu);
12981 }
12982 origin_child_die = origin_child_die->sibling;
12983 }
12984 origin_cu->list_in_scope = origin_previous_list_in_scope;
12985
12986 if (cu != origin_cu)
12987 compute_delayed_physnames (origin_cu);
12988 }
12989
12990 static void
12991 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12992 {
12993 struct objfile *objfile = cu->per_objfile->objfile;
12994 struct gdbarch *gdbarch = objfile->arch ();
12995 struct context_stack *newobj;
12996 CORE_ADDR lowpc;
12997 CORE_ADDR highpc;
12998 struct die_info *child_die;
12999 struct attribute *attr, *call_line, *call_file;
13000 const char *name;
13001 CORE_ADDR baseaddr;
13002 struct block *block;
13003 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13004 std::vector<struct symbol *> template_args;
13005 struct template_symbol *templ_func = NULL;
13006
13007 if (inlined_func)
13008 {
13009 /* If we do not have call site information, we can't show the
13010 caller of this inlined function. That's too confusing, so
13011 only use the scope for local variables. */
13012 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13013 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13014 if (call_line == NULL || call_file == NULL)
13015 {
13016 read_lexical_block_scope (die, cu);
13017 return;
13018 }
13019 }
13020
13021 baseaddr = objfile->text_section_offset ();
13022
13023 name = dwarf2_name (die, cu);
13024
13025 /* Ignore functions with missing or empty names. These are actually
13026 illegal according to the DWARF standard. */
13027 if (name == NULL)
13028 {
13029 complaint (_("missing name for subprogram DIE at %s"),
13030 sect_offset_str (die->sect_off));
13031 return;
13032 }
13033
13034 /* Ignore functions with missing or invalid low and high pc attributes. */
13035 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13036 <= PC_BOUNDS_INVALID)
13037 {
13038 attr = dwarf2_attr (die, DW_AT_external, cu);
13039 if (!attr || !DW_UNSND (attr))
13040 complaint (_("cannot get low and high bounds "
13041 "for subprogram DIE at %s"),
13042 sect_offset_str (die->sect_off));
13043 return;
13044 }
13045
13046 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13047 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13048
13049 /* If we have any template arguments, then we must allocate a
13050 different sort of symbol. */
13051 for (child_die = die->child; child_die; child_die = child_die->sibling)
13052 {
13053 if (child_die->tag == DW_TAG_template_type_param
13054 || child_die->tag == DW_TAG_template_value_param)
13055 {
13056 templ_func = new (&objfile->objfile_obstack) template_symbol;
13057 templ_func->subclass = SYMBOL_TEMPLATE;
13058 break;
13059 }
13060 }
13061
13062 newobj = cu->get_builder ()->push_context (0, lowpc);
13063 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13064 (struct symbol *) templ_func);
13065
13066 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13067 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13068 cu->language);
13069
13070 /* If there is a location expression for DW_AT_frame_base, record
13071 it. */
13072 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13073 if (attr != nullptr)
13074 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13075
13076 /* If there is a location for the static link, record it. */
13077 newobj->static_link = NULL;
13078 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13079 if (attr != nullptr)
13080 {
13081 newobj->static_link
13082 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13083 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13084 cu->per_cu->addr_type ());
13085 }
13086
13087 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13088
13089 if (die->child != NULL)
13090 {
13091 child_die = die->child;
13092 while (child_die && child_die->tag)
13093 {
13094 if (child_die->tag == DW_TAG_template_type_param
13095 || child_die->tag == DW_TAG_template_value_param)
13096 {
13097 struct symbol *arg = new_symbol (child_die, NULL, cu);
13098
13099 if (arg != NULL)
13100 template_args.push_back (arg);
13101 }
13102 else
13103 process_die (child_die, cu);
13104 child_die = child_die->sibling;
13105 }
13106 }
13107
13108 inherit_abstract_dies (die, cu);
13109
13110 /* If we have a DW_AT_specification, we might need to import using
13111 directives from the context of the specification DIE. See the
13112 comment in determine_prefix. */
13113 if (cu->language == language_cplus
13114 && dwarf2_attr (die, DW_AT_specification, cu))
13115 {
13116 struct dwarf2_cu *spec_cu = cu;
13117 struct die_info *spec_die = die_specification (die, &spec_cu);
13118
13119 while (spec_die)
13120 {
13121 child_die = spec_die->child;
13122 while (child_die && child_die->tag)
13123 {
13124 if (child_die->tag == DW_TAG_imported_module)
13125 process_die (child_die, spec_cu);
13126 child_die = child_die->sibling;
13127 }
13128
13129 /* In some cases, GCC generates specification DIEs that
13130 themselves contain DW_AT_specification attributes. */
13131 spec_die = die_specification (spec_die, &spec_cu);
13132 }
13133 }
13134
13135 struct context_stack cstk = cu->get_builder ()->pop_context ();
13136 /* Make a block for the local symbols within. */
13137 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13138 cstk.static_link, lowpc, highpc);
13139
13140 /* For C++, set the block's scope. */
13141 if ((cu->language == language_cplus
13142 || cu->language == language_fortran
13143 || cu->language == language_d
13144 || cu->language == language_rust)
13145 && cu->processing_has_namespace_info)
13146 block_set_scope (block, determine_prefix (die, cu),
13147 &objfile->objfile_obstack);
13148
13149 /* If we have address ranges, record them. */
13150 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13151
13152 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13153
13154 /* Attach template arguments to function. */
13155 if (!template_args.empty ())
13156 {
13157 gdb_assert (templ_func != NULL);
13158
13159 templ_func->n_template_arguments = template_args.size ();
13160 templ_func->template_arguments
13161 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13162 templ_func->n_template_arguments);
13163 memcpy (templ_func->template_arguments,
13164 template_args.data (),
13165 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13166
13167 /* Make sure that the symtab is set on the new symbols. Even
13168 though they don't appear in this symtab directly, other parts
13169 of gdb assume that symbols do, and this is reasonably
13170 true. */
13171 for (symbol *sym : template_args)
13172 symbol_set_symtab (sym, symbol_symtab (templ_func));
13173 }
13174
13175 /* In C++, we can have functions nested inside functions (e.g., when
13176 a function declares a class that has methods). This means that
13177 when we finish processing a function scope, we may need to go
13178 back to building a containing block's symbol lists. */
13179 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13180 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13181
13182 /* If we've finished processing a top-level function, subsequent
13183 symbols go in the file symbol list. */
13184 if (cu->get_builder ()->outermost_context_p ())
13185 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13186 }
13187
13188 /* Process all the DIES contained within a lexical block scope. Start
13189 a new scope, process the dies, and then close the scope. */
13190
13191 static void
13192 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13193 {
13194 struct objfile *objfile = cu->per_objfile->objfile;
13195 struct gdbarch *gdbarch = objfile->arch ();
13196 CORE_ADDR lowpc, highpc;
13197 struct die_info *child_die;
13198 CORE_ADDR baseaddr;
13199
13200 baseaddr = objfile->text_section_offset ();
13201
13202 /* Ignore blocks with missing or invalid low and high pc attributes. */
13203 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13204 as multiple lexical blocks? Handling children in a sane way would
13205 be nasty. Might be easier to properly extend generic blocks to
13206 describe ranges. */
13207 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13208 {
13209 case PC_BOUNDS_NOT_PRESENT:
13210 /* DW_TAG_lexical_block has no attributes, process its children as if
13211 there was no wrapping by that DW_TAG_lexical_block.
13212 GCC does no longer produces such DWARF since GCC r224161. */
13213 for (child_die = die->child;
13214 child_die != NULL && child_die->tag;
13215 child_die = child_die->sibling)
13216 {
13217 /* We might already be processing this DIE. This can happen
13218 in an unusual circumstance -- where a subroutine A
13219 appears lexically in another subroutine B, but A actually
13220 inlines B. The recursion is broken here, rather than in
13221 inherit_abstract_dies, because it seems better to simply
13222 drop concrete children here. */
13223 if (!child_die->in_process)
13224 process_die (child_die, cu);
13225 }
13226 return;
13227 case PC_BOUNDS_INVALID:
13228 return;
13229 }
13230 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13231 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13232
13233 cu->get_builder ()->push_context (0, lowpc);
13234 if (die->child != NULL)
13235 {
13236 child_die = die->child;
13237 while (child_die && child_die->tag)
13238 {
13239 process_die (child_die, cu);
13240 child_die = child_die->sibling;
13241 }
13242 }
13243 inherit_abstract_dies (die, cu);
13244 struct context_stack cstk = cu->get_builder ()->pop_context ();
13245
13246 if (*cu->get_builder ()->get_local_symbols () != NULL
13247 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13248 {
13249 struct block *block
13250 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13251 cstk.start_addr, highpc);
13252
13253 /* Note that recording ranges after traversing children, as we
13254 do here, means that recording a parent's ranges entails
13255 walking across all its children's ranges as they appear in
13256 the address map, which is quadratic behavior.
13257
13258 It would be nicer to record the parent's ranges before
13259 traversing its children, simply overriding whatever you find
13260 there. But since we don't even decide whether to create a
13261 block until after we've traversed its children, that's hard
13262 to do. */
13263 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13264 }
13265 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13266 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13267 }
13268
13269 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13270
13271 static void
13272 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13273 {
13274 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13275 struct objfile *objfile = per_objfile->objfile;
13276 struct gdbarch *gdbarch = objfile->arch ();
13277 CORE_ADDR pc, baseaddr;
13278 struct attribute *attr;
13279 struct call_site *call_site, call_site_local;
13280 void **slot;
13281 int nparams;
13282 struct die_info *child_die;
13283
13284 baseaddr = objfile->text_section_offset ();
13285
13286 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13287 if (attr == NULL)
13288 {
13289 /* This was a pre-DWARF-5 GNU extension alias
13290 for DW_AT_call_return_pc. */
13291 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13292 }
13293 if (!attr)
13294 {
13295 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13296 "DIE %s [in module %s]"),
13297 sect_offset_str (die->sect_off), objfile_name (objfile));
13298 return;
13299 }
13300 pc = attr->value_as_address () + baseaddr;
13301 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13302
13303 if (cu->call_site_htab == NULL)
13304 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13305 NULL, &objfile->objfile_obstack,
13306 hashtab_obstack_allocate, NULL);
13307 call_site_local.pc = pc;
13308 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13309 if (*slot != NULL)
13310 {
13311 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13312 "DIE %s [in module %s]"),
13313 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13314 objfile_name (objfile));
13315 return;
13316 }
13317
13318 /* Count parameters at the caller. */
13319
13320 nparams = 0;
13321 for (child_die = die->child; child_die && child_die->tag;
13322 child_die = child_die->sibling)
13323 {
13324 if (child_die->tag != DW_TAG_call_site_parameter
13325 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13326 {
13327 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13328 "DW_TAG_call_site child DIE %s [in module %s]"),
13329 child_die->tag, sect_offset_str (child_die->sect_off),
13330 objfile_name (objfile));
13331 continue;
13332 }
13333
13334 nparams++;
13335 }
13336
13337 call_site
13338 = ((struct call_site *)
13339 obstack_alloc (&objfile->objfile_obstack,
13340 sizeof (*call_site)
13341 + (sizeof (*call_site->parameter) * (nparams - 1))));
13342 *slot = call_site;
13343 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13344 call_site->pc = pc;
13345
13346 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13347 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13348 {
13349 struct die_info *func_die;
13350
13351 /* Skip also over DW_TAG_inlined_subroutine. */
13352 for (func_die = die->parent;
13353 func_die && func_die->tag != DW_TAG_subprogram
13354 && func_die->tag != DW_TAG_subroutine_type;
13355 func_die = func_die->parent);
13356
13357 /* DW_AT_call_all_calls is a superset
13358 of DW_AT_call_all_tail_calls. */
13359 if (func_die
13360 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13361 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13362 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13363 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13364 {
13365 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13366 not complete. But keep CALL_SITE for look ups via call_site_htab,
13367 both the initial caller containing the real return address PC and
13368 the final callee containing the current PC of a chain of tail
13369 calls do not need to have the tail call list complete. But any
13370 function candidate for a virtual tail call frame searched via
13371 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13372 determined unambiguously. */
13373 }
13374 else
13375 {
13376 struct type *func_type = NULL;
13377
13378 if (func_die)
13379 func_type = get_die_type (func_die, cu);
13380 if (func_type != NULL)
13381 {
13382 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13383
13384 /* Enlist this call site to the function. */
13385 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13386 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13387 }
13388 else
13389 complaint (_("Cannot find function owning DW_TAG_call_site "
13390 "DIE %s [in module %s]"),
13391 sect_offset_str (die->sect_off), objfile_name (objfile));
13392 }
13393 }
13394
13395 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13396 if (attr == NULL)
13397 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13398 if (attr == NULL)
13399 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13400 if (attr == NULL)
13401 {
13402 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13403 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13404 }
13405 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13406 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13407 /* Keep NULL DWARF_BLOCK. */;
13408 else if (attr->form_is_block ())
13409 {
13410 struct dwarf2_locexpr_baton *dlbaton;
13411
13412 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13413 dlbaton->data = DW_BLOCK (attr)->data;
13414 dlbaton->size = DW_BLOCK (attr)->size;
13415 dlbaton->per_objfile = per_objfile;
13416 dlbaton->per_cu = cu->per_cu;
13417
13418 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13419 }
13420 else if (attr->form_is_ref ())
13421 {
13422 struct dwarf2_cu *target_cu = cu;
13423 struct die_info *target_die;
13424
13425 target_die = follow_die_ref (die, attr, &target_cu);
13426 gdb_assert (target_cu->per_objfile->objfile == objfile);
13427 if (die_is_declaration (target_die, target_cu))
13428 {
13429 const char *target_physname;
13430
13431 /* Prefer the mangled name; otherwise compute the demangled one. */
13432 target_physname = dw2_linkage_name (target_die, target_cu);
13433 if (target_physname == NULL)
13434 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13435 if (target_physname == NULL)
13436 complaint (_("DW_AT_call_target target DIE has invalid "
13437 "physname, for referencing DIE %s [in module %s]"),
13438 sect_offset_str (die->sect_off), objfile_name (objfile));
13439 else
13440 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13441 }
13442 else
13443 {
13444 CORE_ADDR lowpc;
13445
13446 /* DW_AT_entry_pc should be preferred. */
13447 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13448 <= PC_BOUNDS_INVALID)
13449 complaint (_("DW_AT_call_target target DIE has invalid "
13450 "low pc, for referencing DIE %s [in module %s]"),
13451 sect_offset_str (die->sect_off), objfile_name (objfile));
13452 else
13453 {
13454 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13455 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13456 }
13457 }
13458 }
13459 else
13460 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13461 "block nor reference, for DIE %s [in module %s]"),
13462 sect_offset_str (die->sect_off), objfile_name (objfile));
13463
13464 call_site->per_cu = cu->per_cu;
13465
13466 for (child_die = die->child;
13467 child_die && child_die->tag;
13468 child_die = child_die->sibling)
13469 {
13470 struct call_site_parameter *parameter;
13471 struct attribute *loc, *origin;
13472
13473 if (child_die->tag != DW_TAG_call_site_parameter
13474 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13475 {
13476 /* Already printed the complaint above. */
13477 continue;
13478 }
13479
13480 gdb_assert (call_site->parameter_count < nparams);
13481 parameter = &call_site->parameter[call_site->parameter_count];
13482
13483 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13484 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13485 register is contained in DW_AT_call_value. */
13486
13487 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13488 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13489 if (origin == NULL)
13490 {
13491 /* This was a pre-DWARF-5 GNU extension alias
13492 for DW_AT_call_parameter. */
13493 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13494 }
13495 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13496 {
13497 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13498
13499 sect_offset sect_off = origin->get_ref_die_offset ();
13500 if (!cu->header.offset_in_cu_p (sect_off))
13501 {
13502 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13503 binding can be done only inside one CU. Such referenced DIE
13504 therefore cannot be even moved to DW_TAG_partial_unit. */
13505 complaint (_("DW_AT_call_parameter offset is not in CU for "
13506 "DW_TAG_call_site child DIE %s [in module %s]"),
13507 sect_offset_str (child_die->sect_off),
13508 objfile_name (objfile));
13509 continue;
13510 }
13511 parameter->u.param_cu_off
13512 = (cu_offset) (sect_off - cu->header.sect_off);
13513 }
13514 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13515 {
13516 complaint (_("No DW_FORM_block* DW_AT_location for "
13517 "DW_TAG_call_site child DIE %s [in module %s]"),
13518 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13519 continue;
13520 }
13521 else
13522 {
13523 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13524 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13525 if (parameter->u.dwarf_reg != -1)
13526 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13527 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13528 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13529 &parameter->u.fb_offset))
13530 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13531 else
13532 {
13533 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13534 "for DW_FORM_block* DW_AT_location is supported for "
13535 "DW_TAG_call_site child DIE %s "
13536 "[in module %s]"),
13537 sect_offset_str (child_die->sect_off),
13538 objfile_name (objfile));
13539 continue;
13540 }
13541 }
13542
13543 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13544 if (attr == NULL)
13545 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13546 if (attr == NULL || !attr->form_is_block ())
13547 {
13548 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13549 "DW_TAG_call_site child DIE %s [in module %s]"),
13550 sect_offset_str (child_die->sect_off),
13551 objfile_name (objfile));
13552 continue;
13553 }
13554 parameter->value = DW_BLOCK (attr)->data;
13555 parameter->value_size = DW_BLOCK (attr)->size;
13556
13557 /* Parameters are not pre-cleared by memset above. */
13558 parameter->data_value = NULL;
13559 parameter->data_value_size = 0;
13560 call_site->parameter_count++;
13561
13562 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13563 if (attr == NULL)
13564 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13565 if (attr != nullptr)
13566 {
13567 if (!attr->form_is_block ())
13568 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13569 "DW_TAG_call_site child DIE %s [in module %s]"),
13570 sect_offset_str (child_die->sect_off),
13571 objfile_name (objfile));
13572 else
13573 {
13574 parameter->data_value = DW_BLOCK (attr)->data;
13575 parameter->data_value_size = DW_BLOCK (attr)->size;
13576 }
13577 }
13578 }
13579 }
13580
13581 /* Helper function for read_variable. If DIE represents a virtual
13582 table, then return the type of the concrete object that is
13583 associated with the virtual table. Otherwise, return NULL. */
13584
13585 static struct type *
13586 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13587 {
13588 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13589 if (attr == NULL)
13590 return NULL;
13591
13592 /* Find the type DIE. */
13593 struct die_info *type_die = NULL;
13594 struct dwarf2_cu *type_cu = cu;
13595
13596 if (attr->form_is_ref ())
13597 type_die = follow_die_ref (die, attr, &type_cu);
13598 if (type_die == NULL)
13599 return NULL;
13600
13601 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13602 return NULL;
13603 return die_containing_type (type_die, type_cu);
13604 }
13605
13606 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13607
13608 static void
13609 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13610 {
13611 struct rust_vtable_symbol *storage = NULL;
13612
13613 if (cu->language == language_rust)
13614 {
13615 struct type *containing_type = rust_containing_type (die, cu);
13616
13617 if (containing_type != NULL)
13618 {
13619 struct objfile *objfile = cu->per_objfile->objfile;
13620
13621 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13622 storage->concrete_type = containing_type;
13623 storage->subclass = SYMBOL_RUST_VTABLE;
13624 }
13625 }
13626
13627 struct symbol *res = new_symbol (die, NULL, cu, storage);
13628 struct attribute *abstract_origin
13629 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13630 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13631 if (res == NULL && loc && abstract_origin)
13632 {
13633 /* We have a variable without a name, but with a location and an abstract
13634 origin. This may be a concrete instance of an abstract variable
13635 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13636 later. */
13637 struct dwarf2_cu *origin_cu = cu;
13638 struct die_info *origin_die
13639 = follow_die_ref (die, abstract_origin, &origin_cu);
13640 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13641 per_objfile->per_bfd->abstract_to_concrete
13642 [origin_die->sect_off].push_back (die->sect_off);
13643 }
13644 }
13645
13646 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13647 reading .debug_rnglists.
13648 Callback's type should be:
13649 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13650 Return true if the attributes are present and valid, otherwise,
13651 return false. */
13652
13653 template <typename Callback>
13654 static bool
13655 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13656 Callback &&callback)
13657 {
13658 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
13659 struct objfile *objfile = dwarf2_per_objfile->objfile;
13660 bfd *obfd = objfile->obfd;
13661 /* Base address selection entry. */
13662 gdb::optional<CORE_ADDR> base;
13663 const gdb_byte *buffer;
13664 CORE_ADDR baseaddr;
13665 bool overflow = false;
13666
13667 base = cu->base_address;
13668
13669 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13670 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
13671 {
13672 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13673 offset);
13674 return false;
13675 }
13676 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
13677
13678 baseaddr = objfile->text_section_offset ();
13679
13680 while (1)
13681 {
13682 /* Initialize it due to a false compiler warning. */
13683 CORE_ADDR range_beginning = 0, range_end = 0;
13684 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13685 + dwarf2_per_objfile->per_bfd->rnglists.size);
13686 unsigned int bytes_read;
13687
13688 if (buffer == buf_end)
13689 {
13690 overflow = true;
13691 break;
13692 }
13693 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13694 switch (rlet)
13695 {
13696 case DW_RLE_end_of_list:
13697 break;
13698 case DW_RLE_base_address:
13699 if (buffer + cu->header.addr_size > buf_end)
13700 {
13701 overflow = true;
13702 break;
13703 }
13704 base = cu->header.read_address (obfd, buffer, &bytes_read);
13705 buffer += bytes_read;
13706 break;
13707 case DW_RLE_start_length:
13708 if (buffer + cu->header.addr_size > buf_end)
13709 {
13710 overflow = true;
13711 break;
13712 }
13713 range_beginning = cu->header.read_address (obfd, buffer,
13714 &bytes_read);
13715 buffer += bytes_read;
13716 range_end = (range_beginning
13717 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13718 buffer += bytes_read;
13719 if (buffer > buf_end)
13720 {
13721 overflow = true;
13722 break;
13723 }
13724 break;
13725 case DW_RLE_offset_pair:
13726 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13727 buffer += bytes_read;
13728 if (buffer > buf_end)
13729 {
13730 overflow = true;
13731 break;
13732 }
13733 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13734 buffer += bytes_read;
13735 if (buffer > buf_end)
13736 {
13737 overflow = true;
13738 break;
13739 }
13740 break;
13741 case DW_RLE_start_end:
13742 if (buffer + 2 * cu->header.addr_size > buf_end)
13743 {
13744 overflow = true;
13745 break;
13746 }
13747 range_beginning = cu->header.read_address (obfd, buffer,
13748 &bytes_read);
13749 buffer += bytes_read;
13750 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13751 buffer += bytes_read;
13752 break;
13753 default:
13754 complaint (_("Invalid .debug_rnglists data (no base address)"));
13755 return false;
13756 }
13757 if (rlet == DW_RLE_end_of_list || overflow)
13758 break;
13759 if (rlet == DW_RLE_base_address)
13760 continue;
13761
13762 if (!base.has_value ())
13763 {
13764 /* We have no valid base address for the ranges
13765 data. */
13766 complaint (_("Invalid .debug_rnglists data (no base address)"));
13767 return false;
13768 }
13769
13770 if (range_beginning > range_end)
13771 {
13772 /* Inverted range entries are invalid. */
13773 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13774 return false;
13775 }
13776
13777 /* Empty range entries have no effect. */
13778 if (range_beginning == range_end)
13779 continue;
13780
13781 range_beginning += *base;
13782 range_end += *base;
13783
13784 /* A not-uncommon case of bad debug info.
13785 Don't pollute the addrmap with bad data. */
13786 if (range_beginning + baseaddr == 0
13787 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
13788 {
13789 complaint (_(".debug_rnglists entry has start address of zero"
13790 " [in module %s]"), objfile_name (objfile));
13791 continue;
13792 }
13793
13794 callback (range_beginning, range_end);
13795 }
13796
13797 if (overflow)
13798 {
13799 complaint (_("Offset %d is not terminated "
13800 "for DW_AT_ranges attribute"),
13801 offset);
13802 return false;
13803 }
13804
13805 return true;
13806 }
13807
13808 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13809 Callback's type should be:
13810 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13811 Return 1 if the attributes are present and valid, otherwise, return 0. */
13812
13813 template <typename Callback>
13814 static int
13815 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13816 Callback &&callback)
13817 {
13818 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13819 struct objfile *objfile = per_objfile->objfile;
13820 struct comp_unit_head *cu_header = &cu->header;
13821 bfd *obfd = objfile->obfd;
13822 unsigned int addr_size = cu_header->addr_size;
13823 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13824 /* Base address selection entry. */
13825 gdb::optional<CORE_ADDR> base;
13826 unsigned int dummy;
13827 const gdb_byte *buffer;
13828 CORE_ADDR baseaddr;
13829
13830 if (cu_header->version >= 5)
13831 return dwarf2_rnglists_process (offset, cu, callback);
13832
13833 base = cu->base_address;
13834
13835 per_objfile->per_bfd->ranges.read (objfile);
13836 if (offset >= per_objfile->per_bfd->ranges.size)
13837 {
13838 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13839 offset);
13840 return 0;
13841 }
13842 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13843
13844 baseaddr = objfile->text_section_offset ();
13845
13846 while (1)
13847 {
13848 CORE_ADDR range_beginning, range_end;
13849
13850 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13851 buffer += addr_size;
13852 range_end = cu->header.read_address (obfd, buffer, &dummy);
13853 buffer += addr_size;
13854 offset += 2 * addr_size;
13855
13856 /* An end of list marker is a pair of zero addresses. */
13857 if (range_beginning == 0 && range_end == 0)
13858 /* Found the end of list entry. */
13859 break;
13860
13861 /* Each base address selection entry is a pair of 2 values.
13862 The first is the largest possible address, the second is
13863 the base address. Check for a base address here. */
13864 if ((range_beginning & mask) == mask)
13865 {
13866 /* If we found the largest possible address, then we already
13867 have the base address in range_end. */
13868 base = range_end;
13869 continue;
13870 }
13871
13872 if (!base.has_value ())
13873 {
13874 /* We have no valid base address for the ranges
13875 data. */
13876 complaint (_("Invalid .debug_ranges data (no base address)"));
13877 return 0;
13878 }
13879
13880 if (range_beginning > range_end)
13881 {
13882 /* Inverted range entries are invalid. */
13883 complaint (_("Invalid .debug_ranges data (inverted range)"));
13884 return 0;
13885 }
13886
13887 /* Empty range entries have no effect. */
13888 if (range_beginning == range_end)
13889 continue;
13890
13891 range_beginning += *base;
13892 range_end += *base;
13893
13894 /* A not-uncommon case of bad debug info.
13895 Don't pollute the addrmap with bad data. */
13896 if (range_beginning + baseaddr == 0
13897 && !per_objfile->per_bfd->has_section_at_zero)
13898 {
13899 complaint (_(".debug_ranges entry has start address of zero"
13900 " [in module %s]"), objfile_name (objfile));
13901 continue;
13902 }
13903
13904 callback (range_beginning, range_end);
13905 }
13906
13907 return 1;
13908 }
13909
13910 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13911 Return 1 if the attributes are present and valid, otherwise, return 0.
13912 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13913
13914 static int
13915 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13916 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13917 dwarf2_psymtab *ranges_pst)
13918 {
13919 struct objfile *objfile = cu->per_objfile->objfile;
13920 struct gdbarch *gdbarch = objfile->arch ();
13921 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13922 int low_set = 0;
13923 CORE_ADDR low = 0;
13924 CORE_ADDR high = 0;
13925 int retval;
13926
13927 retval = dwarf2_ranges_process (offset, cu,
13928 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13929 {
13930 if (ranges_pst != NULL)
13931 {
13932 CORE_ADDR lowpc;
13933 CORE_ADDR highpc;
13934
13935 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13936 range_beginning + baseaddr)
13937 - baseaddr);
13938 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13939 range_end + baseaddr)
13940 - baseaddr);
13941 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13942 lowpc, highpc - 1, ranges_pst);
13943 }
13944
13945 /* FIXME: This is recording everything as a low-high
13946 segment of consecutive addresses. We should have a
13947 data structure for discontiguous block ranges
13948 instead. */
13949 if (! low_set)
13950 {
13951 low = range_beginning;
13952 high = range_end;
13953 low_set = 1;
13954 }
13955 else
13956 {
13957 if (range_beginning < low)
13958 low = range_beginning;
13959 if (range_end > high)
13960 high = range_end;
13961 }
13962 });
13963 if (!retval)
13964 return 0;
13965
13966 if (! low_set)
13967 /* If the first entry is an end-of-list marker, the range
13968 describes an empty scope, i.e. no instructions. */
13969 return 0;
13970
13971 if (low_return)
13972 *low_return = low;
13973 if (high_return)
13974 *high_return = high;
13975 return 1;
13976 }
13977
13978 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13979 definition for the return value. *LOWPC and *HIGHPC are set iff
13980 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13981
13982 static enum pc_bounds_kind
13983 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13984 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13985 dwarf2_psymtab *pst)
13986 {
13987 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
13988 struct attribute *attr;
13989 struct attribute *attr_high;
13990 CORE_ADDR low = 0;
13991 CORE_ADDR high = 0;
13992 enum pc_bounds_kind ret;
13993
13994 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13995 if (attr_high)
13996 {
13997 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13998 if (attr != nullptr)
13999 {
14000 low = attr->value_as_address ();
14001 high = attr_high->value_as_address ();
14002 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14003 high += low;
14004 }
14005 else
14006 /* Found high w/o low attribute. */
14007 return PC_BOUNDS_INVALID;
14008
14009 /* Found consecutive range of addresses. */
14010 ret = PC_BOUNDS_HIGH_LOW;
14011 }
14012 else
14013 {
14014 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14015 if (attr != NULL)
14016 {
14017 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14018 We take advantage of the fact that DW_AT_ranges does not appear
14019 in DW_TAG_compile_unit of DWO files. */
14020 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14021 unsigned int ranges_offset = (DW_UNSND (attr)
14022 + (need_ranges_base
14023 ? cu->ranges_base
14024 : 0));
14025
14026 /* Value of the DW_AT_ranges attribute is the offset in the
14027 .debug_ranges section. */
14028 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14029 return PC_BOUNDS_INVALID;
14030 /* Found discontinuous range of addresses. */
14031 ret = PC_BOUNDS_RANGES;
14032 }
14033 else
14034 return PC_BOUNDS_NOT_PRESENT;
14035 }
14036
14037 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14038 if (high <= low)
14039 return PC_BOUNDS_INVALID;
14040
14041 /* When using the GNU linker, .gnu.linkonce. sections are used to
14042 eliminate duplicate copies of functions and vtables and such.
14043 The linker will arbitrarily choose one and discard the others.
14044 The AT_*_pc values for such functions refer to local labels in
14045 these sections. If the section from that file was discarded, the
14046 labels are not in the output, so the relocs get a value of 0.
14047 If this is a discarded function, mark the pc bounds as invalid,
14048 so that GDB will ignore it. */
14049 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
14050 return PC_BOUNDS_INVALID;
14051
14052 *lowpc = low;
14053 if (highpc)
14054 *highpc = high;
14055 return ret;
14056 }
14057
14058 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14059 its low and high PC addresses. Do nothing if these addresses could not
14060 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14061 and HIGHPC to the high address if greater than HIGHPC. */
14062
14063 static void
14064 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14065 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14066 struct dwarf2_cu *cu)
14067 {
14068 CORE_ADDR low, high;
14069 struct die_info *child = die->child;
14070
14071 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14072 {
14073 *lowpc = std::min (*lowpc, low);
14074 *highpc = std::max (*highpc, high);
14075 }
14076
14077 /* If the language does not allow nested subprograms (either inside
14078 subprograms or lexical blocks), we're done. */
14079 if (cu->language != language_ada)
14080 return;
14081
14082 /* Check all the children of the given DIE. If it contains nested
14083 subprograms, then check their pc bounds. Likewise, we need to
14084 check lexical blocks as well, as they may also contain subprogram
14085 definitions. */
14086 while (child && child->tag)
14087 {
14088 if (child->tag == DW_TAG_subprogram
14089 || child->tag == DW_TAG_lexical_block)
14090 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14091 child = child->sibling;
14092 }
14093 }
14094
14095 /* Get the low and high pc's represented by the scope DIE, and store
14096 them in *LOWPC and *HIGHPC. If the correct values can't be
14097 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14098
14099 static void
14100 get_scope_pc_bounds (struct die_info *die,
14101 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14102 struct dwarf2_cu *cu)
14103 {
14104 CORE_ADDR best_low = (CORE_ADDR) -1;
14105 CORE_ADDR best_high = (CORE_ADDR) 0;
14106 CORE_ADDR current_low, current_high;
14107
14108 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14109 >= PC_BOUNDS_RANGES)
14110 {
14111 best_low = current_low;
14112 best_high = current_high;
14113 }
14114 else
14115 {
14116 struct die_info *child = die->child;
14117
14118 while (child && child->tag)
14119 {
14120 switch (child->tag) {
14121 case DW_TAG_subprogram:
14122 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14123 break;
14124 case DW_TAG_namespace:
14125 case DW_TAG_module:
14126 /* FIXME: carlton/2004-01-16: Should we do this for
14127 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14128 that current GCC's always emit the DIEs corresponding
14129 to definitions of methods of classes as children of a
14130 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14131 the DIEs giving the declarations, which could be
14132 anywhere). But I don't see any reason why the
14133 standards says that they have to be there. */
14134 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14135
14136 if (current_low != ((CORE_ADDR) -1))
14137 {
14138 best_low = std::min (best_low, current_low);
14139 best_high = std::max (best_high, current_high);
14140 }
14141 break;
14142 default:
14143 /* Ignore. */
14144 break;
14145 }
14146
14147 child = child->sibling;
14148 }
14149 }
14150
14151 *lowpc = best_low;
14152 *highpc = best_high;
14153 }
14154
14155 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14156 in DIE. */
14157
14158 static void
14159 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14160 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14161 {
14162 struct objfile *objfile = cu->per_objfile->objfile;
14163 struct gdbarch *gdbarch = objfile->arch ();
14164 struct attribute *attr;
14165 struct attribute *attr_high;
14166
14167 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14168 if (attr_high)
14169 {
14170 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14171 if (attr != nullptr)
14172 {
14173 CORE_ADDR low = attr->value_as_address ();
14174 CORE_ADDR high = attr_high->value_as_address ();
14175
14176 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14177 high += low;
14178
14179 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14180 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14181 cu->get_builder ()->record_block_range (block, low, high - 1);
14182 }
14183 }
14184
14185 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14186 if (attr != nullptr)
14187 {
14188 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14189 We take advantage of the fact that DW_AT_ranges does not appear
14190 in DW_TAG_compile_unit of DWO files. */
14191 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14192
14193 /* The value of the DW_AT_ranges attribute is the offset of the
14194 address range list in the .debug_ranges section. */
14195 unsigned long offset = (DW_UNSND (attr)
14196 + (need_ranges_base ? cu->ranges_base : 0));
14197
14198 std::vector<blockrange> blockvec;
14199 dwarf2_ranges_process (offset, cu,
14200 [&] (CORE_ADDR start, CORE_ADDR end)
14201 {
14202 start += baseaddr;
14203 end += baseaddr;
14204 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14205 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14206 cu->get_builder ()->record_block_range (block, start, end - 1);
14207 blockvec.emplace_back (start, end);
14208 });
14209
14210 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14211 }
14212 }
14213
14214 /* Check whether the producer field indicates either of GCC < 4.6, or the
14215 Intel C/C++ compiler, and cache the result in CU. */
14216
14217 static void
14218 check_producer (struct dwarf2_cu *cu)
14219 {
14220 int major, minor;
14221
14222 if (cu->producer == NULL)
14223 {
14224 /* For unknown compilers expect their behavior is DWARF version
14225 compliant.
14226
14227 GCC started to support .debug_types sections by -gdwarf-4 since
14228 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14229 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14230 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14231 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14232 }
14233 else if (producer_is_gcc (cu->producer, &major, &minor))
14234 {
14235 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14236 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14237 }
14238 else if (producer_is_icc (cu->producer, &major, &minor))
14239 {
14240 cu->producer_is_icc = true;
14241 cu->producer_is_icc_lt_14 = major < 14;
14242 }
14243 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14244 cu->producer_is_codewarrior = true;
14245 else
14246 {
14247 /* For other non-GCC compilers, expect their behavior is DWARF version
14248 compliant. */
14249 }
14250
14251 cu->checked_producer = true;
14252 }
14253
14254 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14255 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14256 during 4.6.0 experimental. */
14257
14258 static bool
14259 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14260 {
14261 if (!cu->checked_producer)
14262 check_producer (cu);
14263
14264 return cu->producer_is_gxx_lt_4_6;
14265 }
14266
14267
14268 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14269 with incorrect is_stmt attributes. */
14270
14271 static bool
14272 producer_is_codewarrior (struct dwarf2_cu *cu)
14273 {
14274 if (!cu->checked_producer)
14275 check_producer (cu);
14276
14277 return cu->producer_is_codewarrior;
14278 }
14279
14280 /* Return the default accessibility type if it is not overridden by
14281 DW_AT_accessibility. */
14282
14283 static enum dwarf_access_attribute
14284 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14285 {
14286 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14287 {
14288 /* The default DWARF 2 accessibility for members is public, the default
14289 accessibility for inheritance is private. */
14290
14291 if (die->tag != DW_TAG_inheritance)
14292 return DW_ACCESS_public;
14293 else
14294 return DW_ACCESS_private;
14295 }
14296 else
14297 {
14298 /* DWARF 3+ defines the default accessibility a different way. The same
14299 rules apply now for DW_TAG_inheritance as for the members and it only
14300 depends on the container kind. */
14301
14302 if (die->parent->tag == DW_TAG_class_type)
14303 return DW_ACCESS_private;
14304 else
14305 return DW_ACCESS_public;
14306 }
14307 }
14308
14309 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14310 offset. If the attribute was not found return 0, otherwise return
14311 1. If it was found but could not properly be handled, set *OFFSET
14312 to 0. */
14313
14314 static int
14315 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14316 LONGEST *offset)
14317 {
14318 struct attribute *attr;
14319
14320 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14321 if (attr != NULL)
14322 {
14323 *offset = 0;
14324
14325 /* Note that we do not check for a section offset first here.
14326 This is because DW_AT_data_member_location is new in DWARF 4,
14327 so if we see it, we can assume that a constant form is really
14328 a constant and not a section offset. */
14329 if (attr->form_is_constant ())
14330 *offset = attr->constant_value (0);
14331 else if (attr->form_is_section_offset ())
14332 dwarf2_complex_location_expr_complaint ();
14333 else if (attr->form_is_block ())
14334 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14335 else
14336 dwarf2_complex_location_expr_complaint ();
14337
14338 return 1;
14339 }
14340
14341 return 0;
14342 }
14343
14344 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14345
14346 static void
14347 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14348 struct field *field)
14349 {
14350 struct attribute *attr;
14351
14352 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14353 if (attr != NULL)
14354 {
14355 if (attr->form_is_constant ())
14356 {
14357 LONGEST offset = attr->constant_value (0);
14358 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14359 }
14360 else if (attr->form_is_section_offset ())
14361 dwarf2_complex_location_expr_complaint ();
14362 else if (attr->form_is_block ())
14363 {
14364 bool handled;
14365 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14366 if (handled)
14367 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14368 else
14369 {
14370 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14371 struct objfile *objfile = per_objfile->objfile;
14372 struct dwarf2_locexpr_baton *dlbaton
14373 = XOBNEW (&objfile->objfile_obstack,
14374 struct dwarf2_locexpr_baton);
14375 dlbaton->data = DW_BLOCK (attr)->data;
14376 dlbaton->size = DW_BLOCK (attr)->size;
14377 /* When using this baton, we want to compute the address
14378 of the field, not the value. This is why
14379 is_reference is set to false here. */
14380 dlbaton->is_reference = false;
14381 dlbaton->per_objfile = per_objfile;
14382 dlbaton->per_cu = cu->per_cu;
14383
14384 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14385 }
14386 }
14387 else
14388 dwarf2_complex_location_expr_complaint ();
14389 }
14390 }
14391
14392 /* Add an aggregate field to the field list. */
14393
14394 static void
14395 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14396 struct dwarf2_cu *cu)
14397 {
14398 struct objfile *objfile = cu->per_objfile->objfile;
14399 struct gdbarch *gdbarch = objfile->arch ();
14400 struct nextfield *new_field;
14401 struct attribute *attr;
14402 struct field *fp;
14403 const char *fieldname = "";
14404
14405 if (die->tag == DW_TAG_inheritance)
14406 {
14407 fip->baseclasses.emplace_back ();
14408 new_field = &fip->baseclasses.back ();
14409 }
14410 else
14411 {
14412 fip->fields.emplace_back ();
14413 new_field = &fip->fields.back ();
14414 }
14415
14416 new_field->offset = die->sect_off;
14417
14418 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14419 if (attr != nullptr)
14420 new_field->accessibility = DW_UNSND (attr);
14421 else
14422 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14423 if (new_field->accessibility != DW_ACCESS_public)
14424 fip->non_public_fields = 1;
14425
14426 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14427 if (attr != nullptr)
14428 new_field->virtuality = DW_UNSND (attr);
14429 else
14430 new_field->virtuality = DW_VIRTUALITY_none;
14431
14432 fp = &new_field->field;
14433
14434 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14435 {
14436 /* Data member other than a C++ static data member. */
14437
14438 /* Get type of field. */
14439 fp->type = die_type (die, cu);
14440
14441 SET_FIELD_BITPOS (*fp, 0);
14442
14443 /* Get bit size of field (zero if none). */
14444 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14445 if (attr != nullptr)
14446 {
14447 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14448 }
14449 else
14450 {
14451 FIELD_BITSIZE (*fp) = 0;
14452 }
14453
14454 /* Get bit offset of field. */
14455 handle_data_member_location (die, cu, fp);
14456 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14457 if (attr != nullptr)
14458 {
14459 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14460 {
14461 /* For big endian bits, the DW_AT_bit_offset gives the
14462 additional bit offset from the MSB of the containing
14463 anonymous object to the MSB of the field. We don't
14464 have to do anything special since we don't need to
14465 know the size of the anonymous object. */
14466 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14467 }
14468 else
14469 {
14470 /* For little endian bits, compute the bit offset to the
14471 MSB of the anonymous object, subtract off the number of
14472 bits from the MSB of the field to the MSB of the
14473 object, and then subtract off the number of bits of
14474 the field itself. The result is the bit offset of
14475 the LSB of the field. */
14476 int anonymous_size;
14477 int bit_offset = DW_UNSND (attr);
14478
14479 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14480 if (attr != nullptr)
14481 {
14482 /* The size of the anonymous object containing
14483 the bit field is explicit, so use the
14484 indicated size (in bytes). */
14485 anonymous_size = DW_UNSND (attr);
14486 }
14487 else
14488 {
14489 /* The size of the anonymous object containing
14490 the bit field must be inferred from the type
14491 attribute of the data member containing the
14492 bit field. */
14493 anonymous_size = TYPE_LENGTH (fp->type);
14494 }
14495 SET_FIELD_BITPOS (*fp,
14496 (FIELD_BITPOS (*fp)
14497 + anonymous_size * bits_per_byte
14498 - bit_offset - FIELD_BITSIZE (*fp)));
14499 }
14500 }
14501 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14502 if (attr != NULL)
14503 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14504 + attr->constant_value (0)));
14505
14506 /* Get name of field. */
14507 fieldname = dwarf2_name (die, cu);
14508 if (fieldname == NULL)
14509 fieldname = "";
14510
14511 /* The name is already allocated along with this objfile, so we don't
14512 need to duplicate it for the type. */
14513 fp->name = fieldname;
14514
14515 /* Change accessibility for artificial fields (e.g. virtual table
14516 pointer or virtual base class pointer) to private. */
14517 if (dwarf2_attr (die, DW_AT_artificial, cu))
14518 {
14519 FIELD_ARTIFICIAL (*fp) = 1;
14520 new_field->accessibility = DW_ACCESS_private;
14521 fip->non_public_fields = 1;
14522 }
14523 }
14524 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14525 {
14526 /* C++ static member. */
14527
14528 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14529 is a declaration, but all versions of G++ as of this writing
14530 (so through at least 3.2.1) incorrectly generate
14531 DW_TAG_variable tags. */
14532
14533 const char *physname;
14534
14535 /* Get name of field. */
14536 fieldname = dwarf2_name (die, cu);
14537 if (fieldname == NULL)
14538 return;
14539
14540 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14541 if (attr
14542 /* Only create a symbol if this is an external value.
14543 new_symbol checks this and puts the value in the global symbol
14544 table, which we want. If it is not external, new_symbol
14545 will try to put the value in cu->list_in_scope which is wrong. */
14546 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14547 {
14548 /* A static const member, not much different than an enum as far as
14549 we're concerned, except that we can support more types. */
14550 new_symbol (die, NULL, cu);
14551 }
14552
14553 /* Get physical name. */
14554 physname = dwarf2_physname (fieldname, die, cu);
14555
14556 /* The name is already allocated along with this objfile, so we don't
14557 need to duplicate it for the type. */
14558 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14559 FIELD_TYPE (*fp) = die_type (die, cu);
14560 FIELD_NAME (*fp) = fieldname;
14561 }
14562 else if (die->tag == DW_TAG_inheritance)
14563 {
14564 /* C++ base class field. */
14565 handle_data_member_location (die, cu, fp);
14566 FIELD_BITSIZE (*fp) = 0;
14567 FIELD_TYPE (*fp) = die_type (die, cu);
14568 FIELD_NAME (*fp) = fp->type->name ();
14569 }
14570 else
14571 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14572 }
14573
14574 /* Can the type given by DIE define another type? */
14575
14576 static bool
14577 type_can_define_types (const struct die_info *die)
14578 {
14579 switch (die->tag)
14580 {
14581 case DW_TAG_typedef:
14582 case DW_TAG_class_type:
14583 case DW_TAG_structure_type:
14584 case DW_TAG_union_type:
14585 case DW_TAG_enumeration_type:
14586 return true;
14587
14588 default:
14589 return false;
14590 }
14591 }
14592
14593 /* Add a type definition defined in the scope of the FIP's class. */
14594
14595 static void
14596 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14597 struct dwarf2_cu *cu)
14598 {
14599 struct decl_field fp;
14600 memset (&fp, 0, sizeof (fp));
14601
14602 gdb_assert (type_can_define_types (die));
14603
14604 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14605 fp.name = dwarf2_name (die, cu);
14606 fp.type = read_type_die (die, cu);
14607
14608 /* Save accessibility. */
14609 enum dwarf_access_attribute accessibility;
14610 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14611 if (attr != NULL)
14612 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14613 else
14614 accessibility = dwarf2_default_access_attribute (die, cu);
14615 switch (accessibility)
14616 {
14617 case DW_ACCESS_public:
14618 /* The assumed value if neither private nor protected. */
14619 break;
14620 case DW_ACCESS_private:
14621 fp.is_private = 1;
14622 break;
14623 case DW_ACCESS_protected:
14624 fp.is_protected = 1;
14625 break;
14626 default:
14627 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14628 }
14629
14630 if (die->tag == DW_TAG_typedef)
14631 fip->typedef_field_list.push_back (fp);
14632 else
14633 fip->nested_types_list.push_back (fp);
14634 }
14635
14636 /* A convenience typedef that's used when finding the discriminant
14637 field for a variant part. */
14638 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14639 offset_map_type;
14640
14641 /* Compute the discriminant range for a given variant. OBSTACK is
14642 where the results will be stored. VARIANT is the variant to
14643 process. IS_UNSIGNED indicates whether the discriminant is signed
14644 or unsigned. */
14645
14646 static const gdb::array_view<discriminant_range>
14647 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14648 bool is_unsigned)
14649 {
14650 std::vector<discriminant_range> ranges;
14651
14652 if (variant.default_branch)
14653 return {};
14654
14655 if (variant.discr_list_data == nullptr)
14656 {
14657 discriminant_range r
14658 = {variant.discriminant_value, variant.discriminant_value};
14659 ranges.push_back (r);
14660 }
14661 else
14662 {
14663 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14664 variant.discr_list_data->size);
14665 while (!data.empty ())
14666 {
14667 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14668 {
14669 complaint (_("invalid discriminant marker: %d"), data[0]);
14670 break;
14671 }
14672 bool is_range = data[0] == DW_DSC_range;
14673 data = data.slice (1);
14674
14675 ULONGEST low, high;
14676 unsigned int bytes_read;
14677
14678 if (data.empty ())
14679 {
14680 complaint (_("DW_AT_discr_list missing low value"));
14681 break;
14682 }
14683 if (is_unsigned)
14684 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14685 else
14686 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14687 &bytes_read);
14688 data = data.slice (bytes_read);
14689
14690 if (is_range)
14691 {
14692 if (data.empty ())
14693 {
14694 complaint (_("DW_AT_discr_list missing high value"));
14695 break;
14696 }
14697 if (is_unsigned)
14698 high = read_unsigned_leb128 (nullptr, data.data (),
14699 &bytes_read);
14700 else
14701 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14702 &bytes_read);
14703 data = data.slice (bytes_read);
14704 }
14705 else
14706 high = low;
14707
14708 ranges.push_back ({ low, high });
14709 }
14710 }
14711
14712 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14713 ranges.size ());
14714 std::copy (ranges.begin (), ranges.end (), result);
14715 return gdb::array_view<discriminant_range> (result, ranges.size ());
14716 }
14717
14718 static const gdb::array_view<variant_part> create_variant_parts
14719 (struct obstack *obstack,
14720 const offset_map_type &offset_map,
14721 struct field_info *fi,
14722 const std::vector<variant_part_builder> &variant_parts);
14723
14724 /* Fill in a "struct variant" for a given variant field. RESULT is
14725 the variant to fill in. OBSTACK is where any needed allocations
14726 will be done. OFFSET_MAP holds the mapping from section offsets to
14727 fields for the type. FI describes the fields of the type we're
14728 processing. FIELD is the variant field we're converting. */
14729
14730 static void
14731 create_one_variant (variant &result, struct obstack *obstack,
14732 const offset_map_type &offset_map,
14733 struct field_info *fi, const variant_field &field)
14734 {
14735 result.discriminants = convert_variant_range (obstack, field, false);
14736 result.first_field = field.first_field + fi->baseclasses.size ();
14737 result.last_field = field.last_field + fi->baseclasses.size ();
14738 result.parts = create_variant_parts (obstack, offset_map, fi,
14739 field.variant_parts);
14740 }
14741
14742 /* Fill in a "struct variant_part" for a given variant part. RESULT
14743 is the variant part to fill in. OBSTACK is where any needed
14744 allocations will be done. OFFSET_MAP holds the mapping from
14745 section offsets to fields for the type. FI describes the fields of
14746 the type we're processing. BUILDER is the variant part to be
14747 converted. */
14748
14749 static void
14750 create_one_variant_part (variant_part &result,
14751 struct obstack *obstack,
14752 const offset_map_type &offset_map,
14753 struct field_info *fi,
14754 const variant_part_builder &builder)
14755 {
14756 auto iter = offset_map.find (builder.discriminant_offset);
14757 if (iter == offset_map.end ())
14758 {
14759 result.discriminant_index = -1;
14760 /* Doesn't matter. */
14761 result.is_unsigned = false;
14762 }
14763 else
14764 {
14765 result.discriminant_index = iter->second;
14766 result.is_unsigned
14767 = TYPE_UNSIGNED (FIELD_TYPE
14768 (fi->fields[result.discriminant_index].field));
14769 }
14770
14771 size_t n = builder.variants.size ();
14772 variant *output = new (obstack) variant[n];
14773 for (size_t i = 0; i < n; ++i)
14774 create_one_variant (output[i], obstack, offset_map, fi,
14775 builder.variants[i]);
14776
14777 result.variants = gdb::array_view<variant> (output, n);
14778 }
14779
14780 /* Create a vector of variant parts that can be attached to a type.
14781 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14782 holds the mapping from section offsets to fields for the type. FI
14783 describes the fields of the type we're processing. VARIANT_PARTS
14784 is the vector to convert. */
14785
14786 static const gdb::array_view<variant_part>
14787 create_variant_parts (struct obstack *obstack,
14788 const offset_map_type &offset_map,
14789 struct field_info *fi,
14790 const std::vector<variant_part_builder> &variant_parts)
14791 {
14792 if (variant_parts.empty ())
14793 return {};
14794
14795 size_t n = variant_parts.size ();
14796 variant_part *result = new (obstack) variant_part[n];
14797 for (size_t i = 0; i < n; ++i)
14798 create_one_variant_part (result[i], obstack, offset_map, fi,
14799 variant_parts[i]);
14800
14801 return gdb::array_view<variant_part> (result, n);
14802 }
14803
14804 /* Compute the variant part vector for FIP, attaching it to TYPE when
14805 done. */
14806
14807 static void
14808 add_variant_property (struct field_info *fip, struct type *type,
14809 struct dwarf2_cu *cu)
14810 {
14811 /* Map section offsets of fields to their field index. Note the
14812 field index here does not take the number of baseclasses into
14813 account. */
14814 offset_map_type offset_map;
14815 for (int i = 0; i < fip->fields.size (); ++i)
14816 offset_map[fip->fields[i].offset] = i;
14817
14818 struct objfile *objfile = cu->per_objfile->objfile;
14819 gdb::array_view<variant_part> parts
14820 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14821 fip->variant_parts);
14822
14823 struct dynamic_prop prop;
14824 prop.kind = PROP_VARIANT_PARTS;
14825 prop.data.variant_parts
14826 = ((gdb::array_view<variant_part> *)
14827 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14828
14829 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14830 }
14831
14832 /* Create the vector of fields, and attach it to the type. */
14833
14834 static void
14835 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14836 struct dwarf2_cu *cu)
14837 {
14838 int nfields = fip->nfields ();
14839
14840 /* Record the field count, allocate space for the array of fields,
14841 and create blank accessibility bitfields if necessary. */
14842 type->set_num_fields (nfields);
14843 type->set_fields
14844 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14845
14846 if (fip->non_public_fields && cu->language != language_ada)
14847 {
14848 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14849
14850 TYPE_FIELD_PRIVATE_BITS (type) =
14851 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14852 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14853
14854 TYPE_FIELD_PROTECTED_BITS (type) =
14855 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14856 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14857
14858 TYPE_FIELD_IGNORE_BITS (type) =
14859 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14860 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14861 }
14862
14863 /* If the type has baseclasses, allocate and clear a bit vector for
14864 TYPE_FIELD_VIRTUAL_BITS. */
14865 if (!fip->baseclasses.empty () && cu->language != language_ada)
14866 {
14867 int num_bytes = B_BYTES (fip->baseclasses.size ());
14868 unsigned char *pointer;
14869
14870 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14871 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14872 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14873 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14874 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14875 }
14876
14877 if (!fip->variant_parts.empty ())
14878 add_variant_property (fip, type, cu);
14879
14880 /* Copy the saved-up fields into the field vector. */
14881 for (int i = 0; i < nfields; ++i)
14882 {
14883 struct nextfield &field
14884 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14885 : fip->fields[i - fip->baseclasses.size ()]);
14886
14887 type->field (i) = field.field;
14888 switch (field.accessibility)
14889 {
14890 case DW_ACCESS_private:
14891 if (cu->language != language_ada)
14892 SET_TYPE_FIELD_PRIVATE (type, i);
14893 break;
14894
14895 case DW_ACCESS_protected:
14896 if (cu->language != language_ada)
14897 SET_TYPE_FIELD_PROTECTED (type, i);
14898 break;
14899
14900 case DW_ACCESS_public:
14901 break;
14902
14903 default:
14904 /* Unknown accessibility. Complain and treat it as public. */
14905 {
14906 complaint (_("unsupported accessibility %d"),
14907 field.accessibility);
14908 }
14909 break;
14910 }
14911 if (i < fip->baseclasses.size ())
14912 {
14913 switch (field.virtuality)
14914 {
14915 case DW_VIRTUALITY_virtual:
14916 case DW_VIRTUALITY_pure_virtual:
14917 if (cu->language == language_ada)
14918 error (_("unexpected virtuality in component of Ada type"));
14919 SET_TYPE_FIELD_VIRTUAL (type, i);
14920 break;
14921 }
14922 }
14923 }
14924 }
14925
14926 /* Return true if this member function is a constructor, false
14927 otherwise. */
14928
14929 static int
14930 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14931 {
14932 const char *fieldname;
14933 const char *type_name;
14934 int len;
14935
14936 if (die->parent == NULL)
14937 return 0;
14938
14939 if (die->parent->tag != DW_TAG_structure_type
14940 && die->parent->tag != DW_TAG_union_type
14941 && die->parent->tag != DW_TAG_class_type)
14942 return 0;
14943
14944 fieldname = dwarf2_name (die, cu);
14945 type_name = dwarf2_name (die->parent, cu);
14946 if (fieldname == NULL || type_name == NULL)
14947 return 0;
14948
14949 len = strlen (fieldname);
14950 return (strncmp (fieldname, type_name, len) == 0
14951 && (type_name[len] == '\0' || type_name[len] == '<'));
14952 }
14953
14954 /* Check if the given VALUE is a recognized enum
14955 dwarf_defaulted_attribute constant according to DWARF5 spec,
14956 Table 7.24. */
14957
14958 static bool
14959 is_valid_DW_AT_defaulted (ULONGEST value)
14960 {
14961 switch (value)
14962 {
14963 case DW_DEFAULTED_no:
14964 case DW_DEFAULTED_in_class:
14965 case DW_DEFAULTED_out_of_class:
14966 return true;
14967 }
14968
14969 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14970 return false;
14971 }
14972
14973 /* Add a member function to the proper fieldlist. */
14974
14975 static void
14976 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14977 struct type *type, struct dwarf2_cu *cu)
14978 {
14979 struct objfile *objfile = cu->per_objfile->objfile;
14980 struct attribute *attr;
14981 int i;
14982 struct fnfieldlist *flp = nullptr;
14983 struct fn_field *fnp;
14984 const char *fieldname;
14985 struct type *this_type;
14986 enum dwarf_access_attribute accessibility;
14987
14988 if (cu->language == language_ada)
14989 error (_("unexpected member function in Ada type"));
14990
14991 /* Get name of member function. */
14992 fieldname = dwarf2_name (die, cu);
14993 if (fieldname == NULL)
14994 return;
14995
14996 /* Look up member function name in fieldlist. */
14997 for (i = 0; i < fip->fnfieldlists.size (); i++)
14998 {
14999 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15000 {
15001 flp = &fip->fnfieldlists[i];
15002 break;
15003 }
15004 }
15005
15006 /* Create a new fnfieldlist if necessary. */
15007 if (flp == nullptr)
15008 {
15009 fip->fnfieldlists.emplace_back ();
15010 flp = &fip->fnfieldlists.back ();
15011 flp->name = fieldname;
15012 i = fip->fnfieldlists.size () - 1;
15013 }
15014
15015 /* Create a new member function field and add it to the vector of
15016 fnfieldlists. */
15017 flp->fnfields.emplace_back ();
15018 fnp = &flp->fnfields.back ();
15019
15020 /* Delay processing of the physname until later. */
15021 if (cu->language == language_cplus)
15022 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15023 die, cu);
15024 else
15025 {
15026 const char *physname = dwarf2_physname (fieldname, die, cu);
15027 fnp->physname = physname ? physname : "";
15028 }
15029
15030 fnp->type = alloc_type (objfile);
15031 this_type = read_type_die (die, cu);
15032 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15033 {
15034 int nparams = this_type->num_fields ();
15035
15036 /* TYPE is the domain of this method, and THIS_TYPE is the type
15037 of the method itself (TYPE_CODE_METHOD). */
15038 smash_to_method_type (fnp->type, type,
15039 TYPE_TARGET_TYPE (this_type),
15040 this_type->fields (),
15041 this_type->num_fields (),
15042 TYPE_VARARGS (this_type));
15043
15044 /* Handle static member functions.
15045 Dwarf2 has no clean way to discern C++ static and non-static
15046 member functions. G++ helps GDB by marking the first
15047 parameter for non-static member functions (which is the this
15048 pointer) as artificial. We obtain this information from
15049 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15050 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15051 fnp->voffset = VOFFSET_STATIC;
15052 }
15053 else
15054 complaint (_("member function type missing for '%s'"),
15055 dwarf2_full_name (fieldname, die, cu));
15056
15057 /* Get fcontext from DW_AT_containing_type if present. */
15058 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15059 fnp->fcontext = die_containing_type (die, cu);
15060
15061 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15062 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15063
15064 /* Get accessibility. */
15065 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15066 if (attr != nullptr)
15067 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15068 else
15069 accessibility = dwarf2_default_access_attribute (die, cu);
15070 switch (accessibility)
15071 {
15072 case DW_ACCESS_private:
15073 fnp->is_private = 1;
15074 break;
15075 case DW_ACCESS_protected:
15076 fnp->is_protected = 1;
15077 break;
15078 }
15079
15080 /* Check for artificial methods. */
15081 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15082 if (attr && DW_UNSND (attr) != 0)
15083 fnp->is_artificial = 1;
15084
15085 /* Check for defaulted methods. */
15086 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15087 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15088 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15089
15090 /* Check for deleted methods. */
15091 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15092 if (attr != nullptr && DW_UNSND (attr) != 0)
15093 fnp->is_deleted = 1;
15094
15095 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15096
15097 /* Get index in virtual function table if it is a virtual member
15098 function. For older versions of GCC, this is an offset in the
15099 appropriate virtual table, as specified by DW_AT_containing_type.
15100 For everyone else, it is an expression to be evaluated relative
15101 to the object address. */
15102
15103 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15104 if (attr != nullptr)
15105 {
15106 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15107 {
15108 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15109 {
15110 /* Old-style GCC. */
15111 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15112 }
15113 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15114 || (DW_BLOCK (attr)->size > 1
15115 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15116 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15117 {
15118 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15119 if ((fnp->voffset % cu->header.addr_size) != 0)
15120 dwarf2_complex_location_expr_complaint ();
15121 else
15122 fnp->voffset /= cu->header.addr_size;
15123 fnp->voffset += 2;
15124 }
15125 else
15126 dwarf2_complex_location_expr_complaint ();
15127
15128 if (!fnp->fcontext)
15129 {
15130 /* If there is no `this' field and no DW_AT_containing_type,
15131 we cannot actually find a base class context for the
15132 vtable! */
15133 if (this_type->num_fields () == 0
15134 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15135 {
15136 complaint (_("cannot determine context for virtual member "
15137 "function \"%s\" (offset %s)"),
15138 fieldname, sect_offset_str (die->sect_off));
15139 }
15140 else
15141 {
15142 fnp->fcontext
15143 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15144 }
15145 }
15146 }
15147 else if (attr->form_is_section_offset ())
15148 {
15149 dwarf2_complex_location_expr_complaint ();
15150 }
15151 else
15152 {
15153 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15154 fieldname);
15155 }
15156 }
15157 else
15158 {
15159 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15160 if (attr && DW_UNSND (attr))
15161 {
15162 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15163 complaint (_("Member function \"%s\" (offset %s) is virtual "
15164 "but the vtable offset is not specified"),
15165 fieldname, sect_offset_str (die->sect_off));
15166 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15167 TYPE_CPLUS_DYNAMIC (type) = 1;
15168 }
15169 }
15170 }
15171
15172 /* Create the vector of member function fields, and attach it to the type. */
15173
15174 static void
15175 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15176 struct dwarf2_cu *cu)
15177 {
15178 if (cu->language == language_ada)
15179 error (_("unexpected member functions in Ada type"));
15180
15181 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15182 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15183 TYPE_ALLOC (type,
15184 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15185
15186 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15187 {
15188 struct fnfieldlist &nf = fip->fnfieldlists[i];
15189 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15190
15191 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15192 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15193 fn_flp->fn_fields = (struct fn_field *)
15194 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15195
15196 for (int k = 0; k < nf.fnfields.size (); ++k)
15197 fn_flp->fn_fields[k] = nf.fnfields[k];
15198 }
15199
15200 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15201 }
15202
15203 /* Returns non-zero if NAME is the name of a vtable member in CU's
15204 language, zero otherwise. */
15205 static int
15206 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15207 {
15208 static const char vptr[] = "_vptr";
15209
15210 /* Look for the C++ form of the vtable. */
15211 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15212 return 1;
15213
15214 return 0;
15215 }
15216
15217 /* GCC outputs unnamed structures that are really pointers to member
15218 functions, with the ABI-specified layout. If TYPE describes
15219 such a structure, smash it into a member function type.
15220
15221 GCC shouldn't do this; it should just output pointer to member DIEs.
15222 This is GCC PR debug/28767. */
15223
15224 static void
15225 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15226 {
15227 struct type *pfn_type, *self_type, *new_type;
15228
15229 /* Check for a structure with no name and two children. */
15230 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15231 return;
15232
15233 /* Check for __pfn and __delta members. */
15234 if (TYPE_FIELD_NAME (type, 0) == NULL
15235 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15236 || TYPE_FIELD_NAME (type, 1) == NULL
15237 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15238 return;
15239
15240 /* Find the type of the method. */
15241 pfn_type = TYPE_FIELD_TYPE (type, 0);
15242 if (pfn_type == NULL
15243 || pfn_type->code () != TYPE_CODE_PTR
15244 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15245 return;
15246
15247 /* Look for the "this" argument. */
15248 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15249 if (pfn_type->num_fields () == 0
15250 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15251 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
15252 return;
15253
15254 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15255 new_type = alloc_type (objfile);
15256 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15257 pfn_type->fields (), pfn_type->num_fields (),
15258 TYPE_VARARGS (pfn_type));
15259 smash_to_methodptr_type (type, new_type);
15260 }
15261
15262 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15263 appropriate error checking and issuing complaints if there is a
15264 problem. */
15265
15266 static ULONGEST
15267 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15268 {
15269 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15270
15271 if (attr == nullptr)
15272 return 0;
15273
15274 if (!attr->form_is_constant ())
15275 {
15276 complaint (_("DW_AT_alignment must have constant form"
15277 " - DIE at %s [in module %s]"),
15278 sect_offset_str (die->sect_off),
15279 objfile_name (cu->per_objfile->objfile));
15280 return 0;
15281 }
15282
15283 ULONGEST align;
15284 if (attr->form == DW_FORM_sdata)
15285 {
15286 LONGEST val = DW_SND (attr);
15287 if (val < 0)
15288 {
15289 complaint (_("DW_AT_alignment value must not be negative"
15290 " - DIE at %s [in module %s]"),
15291 sect_offset_str (die->sect_off),
15292 objfile_name (cu->per_objfile->objfile));
15293 return 0;
15294 }
15295 align = val;
15296 }
15297 else
15298 align = DW_UNSND (attr);
15299
15300 if (align == 0)
15301 {
15302 complaint (_("DW_AT_alignment value must not be zero"
15303 " - DIE at %s [in module %s]"),
15304 sect_offset_str (die->sect_off),
15305 objfile_name (cu->per_objfile->objfile));
15306 return 0;
15307 }
15308 if ((align & (align - 1)) != 0)
15309 {
15310 complaint (_("DW_AT_alignment value must be a power of 2"
15311 " - DIE at %s [in module %s]"),
15312 sect_offset_str (die->sect_off),
15313 objfile_name (cu->per_objfile->objfile));
15314 return 0;
15315 }
15316
15317 return align;
15318 }
15319
15320 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15321 the alignment for TYPE. */
15322
15323 static void
15324 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15325 struct type *type)
15326 {
15327 if (!set_type_align (type, get_alignment (cu, die)))
15328 complaint (_("DW_AT_alignment value too large"
15329 " - DIE at %s [in module %s]"),
15330 sect_offset_str (die->sect_off),
15331 objfile_name (cu->per_objfile->objfile));
15332 }
15333
15334 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15335 constant for a type, according to DWARF5 spec, Table 5.5. */
15336
15337 static bool
15338 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15339 {
15340 switch (value)
15341 {
15342 case DW_CC_normal:
15343 case DW_CC_pass_by_reference:
15344 case DW_CC_pass_by_value:
15345 return true;
15346
15347 default:
15348 complaint (_("unrecognized DW_AT_calling_convention value "
15349 "(%s) for a type"), pulongest (value));
15350 return false;
15351 }
15352 }
15353
15354 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15355 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15356 also according to GNU-specific values (see include/dwarf2.h). */
15357
15358 static bool
15359 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15360 {
15361 switch (value)
15362 {
15363 case DW_CC_normal:
15364 case DW_CC_program:
15365 case DW_CC_nocall:
15366 return true;
15367
15368 case DW_CC_GNU_renesas_sh:
15369 case DW_CC_GNU_borland_fastcall_i386:
15370 case DW_CC_GDB_IBM_OpenCL:
15371 return true;
15372
15373 default:
15374 complaint (_("unrecognized DW_AT_calling_convention value "
15375 "(%s) for a subroutine"), pulongest (value));
15376 return false;
15377 }
15378 }
15379
15380 /* Called when we find the DIE that starts a structure or union scope
15381 (definition) to create a type for the structure or union. Fill in
15382 the type's name and general properties; the members will not be
15383 processed until process_structure_scope. A symbol table entry for
15384 the type will also not be done until process_structure_scope (assuming
15385 the type has a name).
15386
15387 NOTE: we need to call these functions regardless of whether or not the
15388 DIE has a DW_AT_name attribute, since it might be an anonymous
15389 structure or union. This gets the type entered into our set of
15390 user defined types. */
15391
15392 static struct type *
15393 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15394 {
15395 struct objfile *objfile = cu->per_objfile->objfile;
15396 struct type *type;
15397 struct attribute *attr;
15398 const char *name;
15399
15400 /* If the definition of this type lives in .debug_types, read that type.
15401 Don't follow DW_AT_specification though, that will take us back up
15402 the chain and we want to go down. */
15403 attr = die->attr (DW_AT_signature);
15404 if (attr != nullptr)
15405 {
15406 type = get_DW_AT_signature_type (die, attr, cu);
15407
15408 /* The type's CU may not be the same as CU.
15409 Ensure TYPE is recorded with CU in die_type_hash. */
15410 return set_die_type (die, type, cu);
15411 }
15412
15413 type = alloc_type (objfile);
15414 INIT_CPLUS_SPECIFIC (type);
15415
15416 name = dwarf2_name (die, cu);
15417 if (name != NULL)
15418 {
15419 if (cu->language == language_cplus
15420 || cu->language == language_d
15421 || cu->language == language_rust)
15422 {
15423 const char *full_name = dwarf2_full_name (name, die, cu);
15424
15425 /* dwarf2_full_name might have already finished building the DIE's
15426 type. If so, there is no need to continue. */
15427 if (get_die_type (die, cu) != NULL)
15428 return get_die_type (die, cu);
15429
15430 type->set_name (full_name);
15431 }
15432 else
15433 {
15434 /* The name is already allocated along with this objfile, so
15435 we don't need to duplicate it for the type. */
15436 type->set_name (name);
15437 }
15438 }
15439
15440 if (die->tag == DW_TAG_structure_type)
15441 {
15442 type->set_code (TYPE_CODE_STRUCT);
15443 }
15444 else if (die->tag == DW_TAG_union_type)
15445 {
15446 type->set_code (TYPE_CODE_UNION);
15447 }
15448 else
15449 {
15450 type->set_code (TYPE_CODE_STRUCT);
15451 }
15452
15453 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15454 TYPE_DECLARED_CLASS (type) = 1;
15455
15456 /* Store the calling convention in the type if it's available in
15457 the die. Otherwise the calling convention remains set to
15458 the default value DW_CC_normal. */
15459 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15460 if (attr != nullptr
15461 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15462 {
15463 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15464 TYPE_CPLUS_CALLING_CONVENTION (type)
15465 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15466 }
15467
15468 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15469 if (attr != nullptr)
15470 {
15471 if (attr->form_is_constant ())
15472 TYPE_LENGTH (type) = DW_UNSND (attr);
15473 else
15474 {
15475 struct dynamic_prop prop;
15476 if (attr_to_dynamic_prop (attr, die, cu, &prop,
15477 cu->per_cu->addr_type ()))
15478 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15479 TYPE_LENGTH (type) = 0;
15480 }
15481 }
15482 else
15483 {
15484 TYPE_LENGTH (type) = 0;
15485 }
15486
15487 maybe_set_alignment (cu, die, type);
15488
15489 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15490 {
15491 /* ICC<14 does not output the required DW_AT_declaration on
15492 incomplete types, but gives them a size of zero. */
15493 TYPE_STUB (type) = 1;
15494 }
15495 else
15496 TYPE_STUB_SUPPORTED (type) = 1;
15497
15498 if (die_is_declaration (die, cu))
15499 TYPE_STUB (type) = 1;
15500 else if (attr == NULL && die->child == NULL
15501 && producer_is_realview (cu->producer))
15502 /* RealView does not output the required DW_AT_declaration
15503 on incomplete types. */
15504 TYPE_STUB (type) = 1;
15505
15506 /* We need to add the type field to the die immediately so we don't
15507 infinitely recurse when dealing with pointers to the structure
15508 type within the structure itself. */
15509 set_die_type (die, type, cu);
15510
15511 /* set_die_type should be already done. */
15512 set_descriptive_type (type, die, cu);
15513
15514 return type;
15515 }
15516
15517 static void handle_struct_member_die
15518 (struct die_info *child_die,
15519 struct type *type,
15520 struct field_info *fi,
15521 std::vector<struct symbol *> *template_args,
15522 struct dwarf2_cu *cu);
15523
15524 /* A helper for handle_struct_member_die that handles
15525 DW_TAG_variant_part. */
15526
15527 static void
15528 handle_variant_part (struct die_info *die, struct type *type,
15529 struct field_info *fi,
15530 std::vector<struct symbol *> *template_args,
15531 struct dwarf2_cu *cu)
15532 {
15533 variant_part_builder *new_part;
15534 if (fi->current_variant_part == nullptr)
15535 {
15536 fi->variant_parts.emplace_back ();
15537 new_part = &fi->variant_parts.back ();
15538 }
15539 else if (!fi->current_variant_part->processing_variant)
15540 {
15541 complaint (_("nested DW_TAG_variant_part seen "
15542 "- DIE at %s [in module %s]"),
15543 sect_offset_str (die->sect_off),
15544 objfile_name (cu->per_objfile->objfile));
15545 return;
15546 }
15547 else
15548 {
15549 variant_field &current = fi->current_variant_part->variants.back ();
15550 current.variant_parts.emplace_back ();
15551 new_part = &current.variant_parts.back ();
15552 }
15553
15554 /* When we recurse, we want callees to add to this new variant
15555 part. */
15556 scoped_restore save_current_variant_part
15557 = make_scoped_restore (&fi->current_variant_part, new_part);
15558
15559 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15560 if (discr == NULL)
15561 {
15562 /* It's a univariant form, an extension we support. */
15563 }
15564 else if (discr->form_is_ref ())
15565 {
15566 struct dwarf2_cu *target_cu = cu;
15567 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15568
15569 new_part->discriminant_offset = target_die->sect_off;
15570 }
15571 else
15572 {
15573 complaint (_("DW_AT_discr does not have DIE reference form"
15574 " - DIE at %s [in module %s]"),
15575 sect_offset_str (die->sect_off),
15576 objfile_name (cu->per_objfile->objfile));
15577 }
15578
15579 for (die_info *child_die = die->child;
15580 child_die != NULL;
15581 child_die = child_die->sibling)
15582 handle_struct_member_die (child_die, type, fi, template_args, cu);
15583 }
15584
15585 /* A helper for handle_struct_member_die that handles
15586 DW_TAG_variant. */
15587
15588 static void
15589 handle_variant (struct die_info *die, struct type *type,
15590 struct field_info *fi,
15591 std::vector<struct symbol *> *template_args,
15592 struct dwarf2_cu *cu)
15593 {
15594 if (fi->current_variant_part == nullptr)
15595 {
15596 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15597 "- DIE at %s [in module %s]"),
15598 sect_offset_str (die->sect_off),
15599 objfile_name (cu->per_objfile->objfile));
15600 return;
15601 }
15602 if (fi->current_variant_part->processing_variant)
15603 {
15604 complaint (_("nested DW_TAG_variant seen "
15605 "- DIE at %s [in module %s]"),
15606 sect_offset_str (die->sect_off),
15607 objfile_name (cu->per_objfile->objfile));
15608 return;
15609 }
15610
15611 scoped_restore save_processing_variant
15612 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15613 true);
15614
15615 fi->current_variant_part->variants.emplace_back ();
15616 variant_field &variant = fi->current_variant_part->variants.back ();
15617 variant.first_field = fi->fields.size ();
15618
15619 /* In a variant we want to get the discriminant and also add a
15620 field for our sole member child. */
15621 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15622 if (discr == nullptr)
15623 {
15624 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15625 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15626 variant.default_branch = true;
15627 else
15628 variant.discr_list_data = DW_BLOCK (discr);
15629 }
15630 else
15631 variant.discriminant_value = DW_UNSND (discr);
15632
15633 for (die_info *variant_child = die->child;
15634 variant_child != NULL;
15635 variant_child = variant_child->sibling)
15636 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15637
15638 variant.last_field = fi->fields.size ();
15639 }
15640
15641 /* A helper for process_structure_scope that handles a single member
15642 DIE. */
15643
15644 static void
15645 handle_struct_member_die (struct die_info *child_die, struct type *type,
15646 struct field_info *fi,
15647 std::vector<struct symbol *> *template_args,
15648 struct dwarf2_cu *cu)
15649 {
15650 if (child_die->tag == DW_TAG_member
15651 || child_die->tag == DW_TAG_variable)
15652 {
15653 /* NOTE: carlton/2002-11-05: A C++ static data member
15654 should be a DW_TAG_member that is a declaration, but
15655 all versions of G++ as of this writing (so through at
15656 least 3.2.1) incorrectly generate DW_TAG_variable
15657 tags for them instead. */
15658 dwarf2_add_field (fi, child_die, cu);
15659 }
15660 else if (child_die->tag == DW_TAG_subprogram)
15661 {
15662 /* Rust doesn't have member functions in the C++ sense.
15663 However, it does emit ordinary functions as children
15664 of a struct DIE. */
15665 if (cu->language == language_rust)
15666 read_func_scope (child_die, cu);
15667 else
15668 {
15669 /* C++ member function. */
15670 dwarf2_add_member_fn (fi, child_die, type, cu);
15671 }
15672 }
15673 else if (child_die->tag == DW_TAG_inheritance)
15674 {
15675 /* C++ base class field. */
15676 dwarf2_add_field (fi, child_die, cu);
15677 }
15678 else if (type_can_define_types (child_die))
15679 dwarf2_add_type_defn (fi, child_die, cu);
15680 else if (child_die->tag == DW_TAG_template_type_param
15681 || child_die->tag == DW_TAG_template_value_param)
15682 {
15683 struct symbol *arg = new_symbol (child_die, NULL, cu);
15684
15685 if (arg != NULL)
15686 template_args->push_back (arg);
15687 }
15688 else if (child_die->tag == DW_TAG_variant_part)
15689 handle_variant_part (child_die, type, fi, template_args, cu);
15690 else if (child_die->tag == DW_TAG_variant)
15691 handle_variant (child_die, type, fi, template_args, cu);
15692 }
15693
15694 /* Finish creating a structure or union type, including filling in
15695 its members and creating a symbol for it. */
15696
15697 static void
15698 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15699 {
15700 struct objfile *objfile = cu->per_objfile->objfile;
15701 struct die_info *child_die;
15702 struct type *type;
15703
15704 type = get_die_type (die, cu);
15705 if (type == NULL)
15706 type = read_structure_type (die, cu);
15707
15708 bool has_template_parameters = false;
15709 if (die->child != NULL && ! die_is_declaration (die, cu))
15710 {
15711 struct field_info fi;
15712 std::vector<struct symbol *> template_args;
15713
15714 child_die = die->child;
15715
15716 while (child_die && child_die->tag)
15717 {
15718 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15719 child_die = child_die->sibling;
15720 }
15721
15722 /* Attach template arguments to type. */
15723 if (!template_args.empty ())
15724 {
15725 has_template_parameters = true;
15726 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15727 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15728 TYPE_TEMPLATE_ARGUMENTS (type)
15729 = XOBNEWVEC (&objfile->objfile_obstack,
15730 struct symbol *,
15731 TYPE_N_TEMPLATE_ARGUMENTS (type));
15732 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15733 template_args.data (),
15734 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15735 * sizeof (struct symbol *)));
15736 }
15737
15738 /* Attach fields and member functions to the type. */
15739 if (fi.nfields () > 0)
15740 dwarf2_attach_fields_to_type (&fi, type, cu);
15741 if (!fi.fnfieldlists.empty ())
15742 {
15743 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15744
15745 /* Get the type which refers to the base class (possibly this
15746 class itself) which contains the vtable pointer for the current
15747 class from the DW_AT_containing_type attribute. This use of
15748 DW_AT_containing_type is a GNU extension. */
15749
15750 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15751 {
15752 struct type *t = die_containing_type (die, cu);
15753
15754 set_type_vptr_basetype (type, t);
15755 if (type == t)
15756 {
15757 int i;
15758
15759 /* Our own class provides vtbl ptr. */
15760 for (i = t->num_fields () - 1;
15761 i >= TYPE_N_BASECLASSES (t);
15762 --i)
15763 {
15764 const char *fieldname = TYPE_FIELD_NAME (t, i);
15765
15766 if (is_vtable_name (fieldname, cu))
15767 {
15768 set_type_vptr_fieldno (type, i);
15769 break;
15770 }
15771 }
15772
15773 /* Complain if virtual function table field not found. */
15774 if (i < TYPE_N_BASECLASSES (t))
15775 complaint (_("virtual function table pointer "
15776 "not found when defining class '%s'"),
15777 type->name () ? type->name () : "");
15778 }
15779 else
15780 {
15781 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15782 }
15783 }
15784 else if (cu->producer
15785 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15786 {
15787 /* The IBM XLC compiler does not provide direct indication
15788 of the containing type, but the vtable pointer is
15789 always named __vfp. */
15790
15791 int i;
15792
15793 for (i = type->num_fields () - 1;
15794 i >= TYPE_N_BASECLASSES (type);
15795 --i)
15796 {
15797 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15798 {
15799 set_type_vptr_fieldno (type, i);
15800 set_type_vptr_basetype (type, type);
15801 break;
15802 }
15803 }
15804 }
15805 }
15806
15807 /* Copy fi.typedef_field_list linked list elements content into the
15808 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15809 if (!fi.typedef_field_list.empty ())
15810 {
15811 int count = fi.typedef_field_list.size ();
15812
15813 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15814 TYPE_TYPEDEF_FIELD_ARRAY (type)
15815 = ((struct decl_field *)
15816 TYPE_ALLOC (type,
15817 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15818 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15819
15820 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15821 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15822 }
15823
15824 /* Copy fi.nested_types_list linked list elements content into the
15825 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15826 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15827 {
15828 int count = fi.nested_types_list.size ();
15829
15830 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15831 TYPE_NESTED_TYPES_ARRAY (type)
15832 = ((struct decl_field *)
15833 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15834 TYPE_NESTED_TYPES_COUNT (type) = count;
15835
15836 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15837 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15838 }
15839 }
15840
15841 quirk_gcc_member_function_pointer (type, objfile);
15842 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15843 cu->rust_unions.push_back (type);
15844
15845 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15846 snapshots) has been known to create a die giving a declaration
15847 for a class that has, as a child, a die giving a definition for a
15848 nested class. So we have to process our children even if the
15849 current die is a declaration. Normally, of course, a declaration
15850 won't have any children at all. */
15851
15852 child_die = die->child;
15853
15854 while (child_die != NULL && child_die->tag)
15855 {
15856 if (child_die->tag == DW_TAG_member
15857 || child_die->tag == DW_TAG_variable
15858 || child_die->tag == DW_TAG_inheritance
15859 || child_die->tag == DW_TAG_template_value_param
15860 || child_die->tag == DW_TAG_template_type_param)
15861 {
15862 /* Do nothing. */
15863 }
15864 else
15865 process_die (child_die, cu);
15866
15867 child_die = child_die->sibling;
15868 }
15869
15870 /* Do not consider external references. According to the DWARF standard,
15871 these DIEs are identified by the fact that they have no byte_size
15872 attribute, and a declaration attribute. */
15873 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15874 || !die_is_declaration (die, cu)
15875 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15876 {
15877 struct symbol *sym = new_symbol (die, type, cu);
15878
15879 if (has_template_parameters)
15880 {
15881 struct symtab *symtab;
15882 if (sym != nullptr)
15883 symtab = symbol_symtab (sym);
15884 else if (cu->line_header != nullptr)
15885 {
15886 /* Any related symtab will do. */
15887 symtab
15888 = cu->line_header->file_names ()[0].symtab;
15889 }
15890 else
15891 {
15892 symtab = nullptr;
15893 complaint (_("could not find suitable "
15894 "symtab for template parameter"
15895 " - DIE at %s [in module %s]"),
15896 sect_offset_str (die->sect_off),
15897 objfile_name (objfile));
15898 }
15899
15900 if (symtab != nullptr)
15901 {
15902 /* Make sure that the symtab is set on the new symbols.
15903 Even though they don't appear in this symtab directly,
15904 other parts of gdb assume that symbols do, and this is
15905 reasonably true. */
15906 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15907 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15908 }
15909 }
15910 }
15911 }
15912
15913 /* Assuming DIE is an enumeration type, and TYPE is its associated
15914 type, update TYPE using some information only available in DIE's
15915 children. In particular, the fields are computed. */
15916
15917 static void
15918 update_enumeration_type_from_children (struct die_info *die,
15919 struct type *type,
15920 struct dwarf2_cu *cu)
15921 {
15922 struct die_info *child_die;
15923 int unsigned_enum = 1;
15924 int flag_enum = 1;
15925
15926 auto_obstack obstack;
15927 std::vector<struct field> fields;
15928
15929 for (child_die = die->child;
15930 child_die != NULL && child_die->tag;
15931 child_die = child_die->sibling)
15932 {
15933 struct attribute *attr;
15934 LONGEST value;
15935 const gdb_byte *bytes;
15936 struct dwarf2_locexpr_baton *baton;
15937 const char *name;
15938
15939 if (child_die->tag != DW_TAG_enumerator)
15940 continue;
15941
15942 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15943 if (attr == NULL)
15944 continue;
15945
15946 name = dwarf2_name (child_die, cu);
15947 if (name == NULL)
15948 name = "<anonymous enumerator>";
15949
15950 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15951 &value, &bytes, &baton);
15952 if (value < 0)
15953 {
15954 unsigned_enum = 0;
15955 flag_enum = 0;
15956 }
15957 else
15958 {
15959 if (count_one_bits_ll (value) >= 2)
15960 flag_enum = 0;
15961 }
15962
15963 fields.emplace_back ();
15964 struct field &field = fields.back ();
15965 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
15966 SET_FIELD_ENUMVAL (field, value);
15967 }
15968
15969 if (!fields.empty ())
15970 {
15971 type->set_num_fields (fields.size ());
15972 type->set_fields
15973 ((struct field *)
15974 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
15975 memcpy (type->fields (), fields.data (),
15976 sizeof (struct field) * fields.size ());
15977 }
15978
15979 if (unsigned_enum)
15980 TYPE_UNSIGNED (type) = 1;
15981 if (flag_enum)
15982 TYPE_FLAG_ENUM (type) = 1;
15983 }
15984
15985 /* Given a DW_AT_enumeration_type die, set its type. We do not
15986 complete the type's fields yet, or create any symbols. */
15987
15988 static struct type *
15989 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15990 {
15991 struct objfile *objfile = cu->per_objfile->objfile;
15992 struct type *type;
15993 struct attribute *attr;
15994 const char *name;
15995
15996 /* If the definition of this type lives in .debug_types, read that type.
15997 Don't follow DW_AT_specification though, that will take us back up
15998 the chain and we want to go down. */
15999 attr = die->attr (DW_AT_signature);
16000 if (attr != nullptr)
16001 {
16002 type = get_DW_AT_signature_type (die, attr, cu);
16003
16004 /* The type's CU may not be the same as CU.
16005 Ensure TYPE is recorded with CU in die_type_hash. */
16006 return set_die_type (die, type, cu);
16007 }
16008
16009 type = alloc_type (objfile);
16010
16011 type->set_code (TYPE_CODE_ENUM);
16012 name = dwarf2_full_name (NULL, die, cu);
16013 if (name != NULL)
16014 type->set_name (name);
16015
16016 attr = dwarf2_attr (die, DW_AT_type, cu);
16017 if (attr != NULL)
16018 {
16019 struct type *underlying_type = die_type (die, cu);
16020
16021 TYPE_TARGET_TYPE (type) = underlying_type;
16022 }
16023
16024 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16025 if (attr != nullptr)
16026 {
16027 TYPE_LENGTH (type) = DW_UNSND (attr);
16028 }
16029 else
16030 {
16031 TYPE_LENGTH (type) = 0;
16032 }
16033
16034 maybe_set_alignment (cu, die, type);
16035
16036 /* The enumeration DIE can be incomplete. In Ada, any type can be
16037 declared as private in the package spec, and then defined only
16038 inside the package body. Such types are known as Taft Amendment
16039 Types. When another package uses such a type, an incomplete DIE
16040 may be generated by the compiler. */
16041 if (die_is_declaration (die, cu))
16042 TYPE_STUB (type) = 1;
16043
16044 /* If this type has an underlying type that is not a stub, then we
16045 may use its attributes. We always use the "unsigned" attribute
16046 in this situation, because ordinarily we guess whether the type
16047 is unsigned -- but the guess can be wrong and the underlying type
16048 can tell us the reality. However, we defer to a local size
16049 attribute if one exists, because this lets the compiler override
16050 the underlying type if needed. */
16051 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16052 {
16053 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16054 underlying_type = check_typedef (underlying_type);
16055 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
16056 if (TYPE_LENGTH (type) == 0)
16057 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16058 if (TYPE_RAW_ALIGN (type) == 0
16059 && TYPE_RAW_ALIGN (underlying_type) != 0)
16060 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16061 }
16062
16063 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16064
16065 set_die_type (die, type, cu);
16066
16067 /* Finish the creation of this type by using the enum's children.
16068 Note that, as usual, this must come after set_die_type to avoid
16069 infinite recursion when trying to compute the names of the
16070 enumerators. */
16071 update_enumeration_type_from_children (die, type, cu);
16072
16073 return type;
16074 }
16075
16076 /* Given a pointer to a die which begins an enumeration, process all
16077 the dies that define the members of the enumeration, and create the
16078 symbol for the enumeration type.
16079
16080 NOTE: We reverse the order of the element list. */
16081
16082 static void
16083 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16084 {
16085 struct type *this_type;
16086
16087 this_type = get_die_type (die, cu);
16088 if (this_type == NULL)
16089 this_type = read_enumeration_type (die, cu);
16090
16091 if (die->child != NULL)
16092 {
16093 struct die_info *child_die;
16094 const char *name;
16095
16096 child_die = die->child;
16097 while (child_die && child_die->tag)
16098 {
16099 if (child_die->tag != DW_TAG_enumerator)
16100 {
16101 process_die (child_die, cu);
16102 }
16103 else
16104 {
16105 name = dwarf2_name (child_die, cu);
16106 if (name)
16107 new_symbol (child_die, this_type, cu);
16108 }
16109
16110 child_die = child_die->sibling;
16111 }
16112 }
16113
16114 /* If we are reading an enum from a .debug_types unit, and the enum
16115 is a declaration, and the enum is not the signatured type in the
16116 unit, then we do not want to add a symbol for it. Adding a
16117 symbol would in some cases obscure the true definition of the
16118 enum, giving users an incomplete type when the definition is
16119 actually available. Note that we do not want to do this for all
16120 enums which are just declarations, because C++0x allows forward
16121 enum declarations. */
16122 if (cu->per_cu->is_debug_types
16123 && die_is_declaration (die, cu))
16124 {
16125 struct signatured_type *sig_type;
16126
16127 sig_type = (struct signatured_type *) cu->per_cu;
16128 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16129 if (sig_type->type_offset_in_section != die->sect_off)
16130 return;
16131 }
16132
16133 new_symbol (die, this_type, cu);
16134 }
16135
16136 /* Extract all information from a DW_TAG_array_type DIE and put it in
16137 the DIE's type field. For now, this only handles one dimensional
16138 arrays. */
16139
16140 static struct type *
16141 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16142 {
16143 struct objfile *objfile = cu->per_objfile->objfile;
16144 struct die_info *child_die;
16145 struct type *type;
16146 struct type *element_type, *range_type, *index_type;
16147 struct attribute *attr;
16148 const char *name;
16149 struct dynamic_prop *byte_stride_prop = NULL;
16150 unsigned int bit_stride = 0;
16151
16152 element_type = die_type (die, cu);
16153
16154 /* The die_type call above may have already set the type for this DIE. */
16155 type = get_die_type (die, cu);
16156 if (type)
16157 return type;
16158
16159 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16160 if (attr != NULL)
16161 {
16162 int stride_ok;
16163 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
16164
16165 byte_stride_prop
16166 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16167 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16168 prop_type);
16169 if (!stride_ok)
16170 {
16171 complaint (_("unable to read array DW_AT_byte_stride "
16172 " - DIE at %s [in module %s]"),
16173 sect_offset_str (die->sect_off),
16174 objfile_name (cu->per_objfile->objfile));
16175 /* Ignore this attribute. We will likely not be able to print
16176 arrays of this type correctly, but there is little we can do
16177 to help if we cannot read the attribute's value. */
16178 byte_stride_prop = NULL;
16179 }
16180 }
16181
16182 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16183 if (attr != NULL)
16184 bit_stride = DW_UNSND (attr);
16185
16186 /* Irix 6.2 native cc creates array types without children for
16187 arrays with unspecified length. */
16188 if (die->child == NULL)
16189 {
16190 index_type = objfile_type (objfile)->builtin_int;
16191 range_type = create_static_range_type (NULL, index_type, 0, -1);
16192 type = create_array_type_with_stride (NULL, element_type, range_type,
16193 byte_stride_prop, bit_stride);
16194 return set_die_type (die, type, cu);
16195 }
16196
16197 std::vector<struct type *> range_types;
16198 child_die = die->child;
16199 while (child_die && child_die->tag)
16200 {
16201 if (child_die->tag == DW_TAG_subrange_type)
16202 {
16203 struct type *child_type = read_type_die (child_die, cu);
16204
16205 if (child_type != NULL)
16206 {
16207 /* The range type was succesfully read. Save it for the
16208 array type creation. */
16209 range_types.push_back (child_type);
16210 }
16211 }
16212 child_die = child_die->sibling;
16213 }
16214
16215 /* Dwarf2 dimensions are output from left to right, create the
16216 necessary array types in backwards order. */
16217
16218 type = element_type;
16219
16220 if (read_array_order (die, cu) == DW_ORD_col_major)
16221 {
16222 int i = 0;
16223
16224 while (i < range_types.size ())
16225 type = create_array_type_with_stride (NULL, type, range_types[i++],
16226 byte_stride_prop, bit_stride);
16227 }
16228 else
16229 {
16230 size_t ndim = range_types.size ();
16231 while (ndim-- > 0)
16232 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16233 byte_stride_prop, bit_stride);
16234 }
16235
16236 /* Understand Dwarf2 support for vector types (like they occur on
16237 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16238 array type. This is not part of the Dwarf2/3 standard yet, but a
16239 custom vendor extension. The main difference between a regular
16240 array and the vector variant is that vectors are passed by value
16241 to functions. */
16242 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16243 if (attr != nullptr)
16244 make_vector_type (type);
16245
16246 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16247 implementation may choose to implement triple vectors using this
16248 attribute. */
16249 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16250 if (attr != nullptr)
16251 {
16252 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16253 TYPE_LENGTH (type) = DW_UNSND (attr);
16254 else
16255 complaint (_("DW_AT_byte_size for array type smaller "
16256 "than the total size of elements"));
16257 }
16258
16259 name = dwarf2_name (die, cu);
16260 if (name)
16261 type->set_name (name);
16262
16263 maybe_set_alignment (cu, die, type);
16264
16265 /* Install the type in the die. */
16266 set_die_type (die, type, cu);
16267
16268 /* set_die_type should be already done. */
16269 set_descriptive_type (type, die, cu);
16270
16271 return type;
16272 }
16273
16274 static enum dwarf_array_dim_ordering
16275 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16276 {
16277 struct attribute *attr;
16278
16279 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16280
16281 if (attr != nullptr)
16282 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16283
16284 /* GNU F77 is a special case, as at 08/2004 array type info is the
16285 opposite order to the dwarf2 specification, but data is still
16286 laid out as per normal fortran.
16287
16288 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16289 version checking. */
16290
16291 if (cu->language == language_fortran
16292 && cu->producer && strstr (cu->producer, "GNU F77"))
16293 {
16294 return DW_ORD_row_major;
16295 }
16296
16297 switch (cu->language_defn->la_array_ordering)
16298 {
16299 case array_column_major:
16300 return DW_ORD_col_major;
16301 case array_row_major:
16302 default:
16303 return DW_ORD_row_major;
16304 };
16305 }
16306
16307 /* Extract all information from a DW_TAG_set_type DIE and put it in
16308 the DIE's type field. */
16309
16310 static struct type *
16311 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16312 {
16313 struct type *domain_type, *set_type;
16314 struct attribute *attr;
16315
16316 domain_type = die_type (die, cu);
16317
16318 /* The die_type call above may have already set the type for this DIE. */
16319 set_type = get_die_type (die, cu);
16320 if (set_type)
16321 return set_type;
16322
16323 set_type = create_set_type (NULL, domain_type);
16324
16325 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16326 if (attr != nullptr)
16327 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16328
16329 maybe_set_alignment (cu, die, set_type);
16330
16331 return set_die_type (die, set_type, cu);
16332 }
16333
16334 /* A helper for read_common_block that creates a locexpr baton.
16335 SYM is the symbol which we are marking as computed.
16336 COMMON_DIE is the DIE for the common block.
16337 COMMON_LOC is the location expression attribute for the common
16338 block itself.
16339 MEMBER_LOC is the location expression attribute for the particular
16340 member of the common block that we are processing.
16341 CU is the CU from which the above come. */
16342
16343 static void
16344 mark_common_block_symbol_computed (struct symbol *sym,
16345 struct die_info *common_die,
16346 struct attribute *common_loc,
16347 struct attribute *member_loc,
16348 struct dwarf2_cu *cu)
16349 {
16350 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16351 struct objfile *objfile = per_objfile->objfile;
16352 struct dwarf2_locexpr_baton *baton;
16353 gdb_byte *ptr;
16354 unsigned int cu_off;
16355 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16356 LONGEST offset = 0;
16357
16358 gdb_assert (common_loc && member_loc);
16359 gdb_assert (common_loc->form_is_block ());
16360 gdb_assert (member_loc->form_is_block ()
16361 || member_loc->form_is_constant ());
16362
16363 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16364 baton->per_objfile = per_objfile;
16365 baton->per_cu = cu->per_cu;
16366 gdb_assert (baton->per_cu);
16367
16368 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16369
16370 if (member_loc->form_is_constant ())
16371 {
16372 offset = member_loc->constant_value (0);
16373 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16374 }
16375 else
16376 baton->size += DW_BLOCK (member_loc)->size;
16377
16378 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16379 baton->data = ptr;
16380
16381 *ptr++ = DW_OP_call4;
16382 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16383 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16384 ptr += 4;
16385
16386 if (member_loc->form_is_constant ())
16387 {
16388 *ptr++ = DW_OP_addr;
16389 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16390 ptr += cu->header.addr_size;
16391 }
16392 else
16393 {
16394 /* We have to copy the data here, because DW_OP_call4 will only
16395 use a DW_AT_location attribute. */
16396 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16397 ptr += DW_BLOCK (member_loc)->size;
16398 }
16399
16400 *ptr++ = DW_OP_plus;
16401 gdb_assert (ptr - baton->data == baton->size);
16402
16403 SYMBOL_LOCATION_BATON (sym) = baton;
16404 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16405 }
16406
16407 /* Create appropriate locally-scoped variables for all the
16408 DW_TAG_common_block entries. Also create a struct common_block
16409 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16410 is used to separate the common blocks name namespace from regular
16411 variable names. */
16412
16413 static void
16414 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16415 {
16416 struct attribute *attr;
16417
16418 attr = dwarf2_attr (die, DW_AT_location, cu);
16419 if (attr != nullptr)
16420 {
16421 /* Support the .debug_loc offsets. */
16422 if (attr->form_is_block ())
16423 {
16424 /* Ok. */
16425 }
16426 else if (attr->form_is_section_offset ())
16427 {
16428 dwarf2_complex_location_expr_complaint ();
16429 attr = NULL;
16430 }
16431 else
16432 {
16433 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16434 "common block member");
16435 attr = NULL;
16436 }
16437 }
16438
16439 if (die->child != NULL)
16440 {
16441 struct objfile *objfile = cu->per_objfile->objfile;
16442 struct die_info *child_die;
16443 size_t n_entries = 0, size;
16444 struct common_block *common_block;
16445 struct symbol *sym;
16446
16447 for (child_die = die->child;
16448 child_die && child_die->tag;
16449 child_die = child_die->sibling)
16450 ++n_entries;
16451
16452 size = (sizeof (struct common_block)
16453 + (n_entries - 1) * sizeof (struct symbol *));
16454 common_block
16455 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16456 size);
16457 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16458 common_block->n_entries = 0;
16459
16460 for (child_die = die->child;
16461 child_die && child_die->tag;
16462 child_die = child_die->sibling)
16463 {
16464 /* Create the symbol in the DW_TAG_common_block block in the current
16465 symbol scope. */
16466 sym = new_symbol (child_die, NULL, cu);
16467 if (sym != NULL)
16468 {
16469 struct attribute *member_loc;
16470
16471 common_block->contents[common_block->n_entries++] = sym;
16472
16473 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16474 cu);
16475 if (member_loc)
16476 {
16477 /* GDB has handled this for a long time, but it is
16478 not specified by DWARF. It seems to have been
16479 emitted by gfortran at least as recently as:
16480 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16481 complaint (_("Variable in common block has "
16482 "DW_AT_data_member_location "
16483 "- DIE at %s [in module %s]"),
16484 sect_offset_str (child_die->sect_off),
16485 objfile_name (objfile));
16486
16487 if (member_loc->form_is_section_offset ())
16488 dwarf2_complex_location_expr_complaint ();
16489 else if (member_loc->form_is_constant ()
16490 || member_loc->form_is_block ())
16491 {
16492 if (attr != nullptr)
16493 mark_common_block_symbol_computed (sym, die, attr,
16494 member_loc, cu);
16495 }
16496 else
16497 dwarf2_complex_location_expr_complaint ();
16498 }
16499 }
16500 }
16501
16502 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16503 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16504 }
16505 }
16506
16507 /* Create a type for a C++ namespace. */
16508
16509 static struct type *
16510 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16511 {
16512 struct objfile *objfile = cu->per_objfile->objfile;
16513 const char *previous_prefix, *name;
16514 int is_anonymous;
16515 struct type *type;
16516
16517 /* For extensions, reuse the type of the original namespace. */
16518 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16519 {
16520 struct die_info *ext_die;
16521 struct dwarf2_cu *ext_cu = cu;
16522
16523 ext_die = dwarf2_extension (die, &ext_cu);
16524 type = read_type_die (ext_die, ext_cu);
16525
16526 /* EXT_CU may not be the same as CU.
16527 Ensure TYPE is recorded with CU in die_type_hash. */
16528 return set_die_type (die, type, cu);
16529 }
16530
16531 name = namespace_name (die, &is_anonymous, cu);
16532
16533 /* Now build the name of the current namespace. */
16534
16535 previous_prefix = determine_prefix (die, cu);
16536 if (previous_prefix[0] != '\0')
16537 name = typename_concat (&objfile->objfile_obstack,
16538 previous_prefix, name, 0, cu);
16539
16540 /* Create the type. */
16541 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16542
16543 return set_die_type (die, type, cu);
16544 }
16545
16546 /* Read a namespace scope. */
16547
16548 static void
16549 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16550 {
16551 struct objfile *objfile = cu->per_objfile->objfile;
16552 int is_anonymous;
16553
16554 /* Add a symbol associated to this if we haven't seen the namespace
16555 before. Also, add a using directive if it's an anonymous
16556 namespace. */
16557
16558 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16559 {
16560 struct type *type;
16561
16562 type = read_type_die (die, cu);
16563 new_symbol (die, type, cu);
16564
16565 namespace_name (die, &is_anonymous, cu);
16566 if (is_anonymous)
16567 {
16568 const char *previous_prefix = determine_prefix (die, cu);
16569
16570 std::vector<const char *> excludes;
16571 add_using_directive (using_directives (cu),
16572 previous_prefix, type->name (), NULL,
16573 NULL, excludes, 0, &objfile->objfile_obstack);
16574 }
16575 }
16576
16577 if (die->child != NULL)
16578 {
16579 struct die_info *child_die = die->child;
16580
16581 while (child_die && child_die->tag)
16582 {
16583 process_die (child_die, cu);
16584 child_die = child_die->sibling;
16585 }
16586 }
16587 }
16588
16589 /* Read a Fortran module as type. This DIE can be only a declaration used for
16590 imported module. Still we need that type as local Fortran "use ... only"
16591 declaration imports depend on the created type in determine_prefix. */
16592
16593 static struct type *
16594 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16595 {
16596 struct objfile *objfile = cu->per_objfile->objfile;
16597 const char *module_name;
16598 struct type *type;
16599
16600 module_name = dwarf2_name (die, cu);
16601 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16602
16603 return set_die_type (die, type, cu);
16604 }
16605
16606 /* Read a Fortran module. */
16607
16608 static void
16609 read_module (struct die_info *die, struct dwarf2_cu *cu)
16610 {
16611 struct die_info *child_die = die->child;
16612 struct type *type;
16613
16614 type = read_type_die (die, cu);
16615 new_symbol (die, type, cu);
16616
16617 while (child_die && child_die->tag)
16618 {
16619 process_die (child_die, cu);
16620 child_die = child_die->sibling;
16621 }
16622 }
16623
16624 /* Return the name of the namespace represented by DIE. Set
16625 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16626 namespace. */
16627
16628 static const char *
16629 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16630 {
16631 struct die_info *current_die;
16632 const char *name = NULL;
16633
16634 /* Loop through the extensions until we find a name. */
16635
16636 for (current_die = die;
16637 current_die != NULL;
16638 current_die = dwarf2_extension (die, &cu))
16639 {
16640 /* We don't use dwarf2_name here so that we can detect the absence
16641 of a name -> anonymous namespace. */
16642 name = dwarf2_string_attr (die, DW_AT_name, cu);
16643
16644 if (name != NULL)
16645 break;
16646 }
16647
16648 /* Is it an anonymous namespace? */
16649
16650 *is_anonymous = (name == NULL);
16651 if (*is_anonymous)
16652 name = CP_ANONYMOUS_NAMESPACE_STR;
16653
16654 return name;
16655 }
16656
16657 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16658 the user defined type vector. */
16659
16660 static struct type *
16661 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16662 {
16663 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16664 struct comp_unit_head *cu_header = &cu->header;
16665 struct type *type;
16666 struct attribute *attr_byte_size;
16667 struct attribute *attr_address_class;
16668 int byte_size, addr_class;
16669 struct type *target_type;
16670
16671 target_type = die_type (die, cu);
16672
16673 /* The die_type call above may have already set the type for this DIE. */
16674 type = get_die_type (die, cu);
16675 if (type)
16676 return type;
16677
16678 type = lookup_pointer_type (target_type);
16679
16680 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16681 if (attr_byte_size)
16682 byte_size = DW_UNSND (attr_byte_size);
16683 else
16684 byte_size = cu_header->addr_size;
16685
16686 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16687 if (attr_address_class)
16688 addr_class = DW_UNSND (attr_address_class);
16689 else
16690 addr_class = DW_ADDR_none;
16691
16692 ULONGEST alignment = get_alignment (cu, die);
16693
16694 /* If the pointer size, alignment, or address class is different
16695 than the default, create a type variant marked as such and set
16696 the length accordingly. */
16697 if (TYPE_LENGTH (type) != byte_size
16698 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16699 && alignment != TYPE_RAW_ALIGN (type))
16700 || addr_class != DW_ADDR_none)
16701 {
16702 if (gdbarch_address_class_type_flags_p (gdbarch))
16703 {
16704 int type_flags;
16705
16706 type_flags = gdbarch_address_class_type_flags
16707 (gdbarch, byte_size, addr_class);
16708 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16709 == 0);
16710 type = make_type_with_address_space (type, type_flags);
16711 }
16712 else if (TYPE_LENGTH (type) != byte_size)
16713 {
16714 complaint (_("invalid pointer size %d"), byte_size);
16715 }
16716 else if (TYPE_RAW_ALIGN (type) != alignment)
16717 {
16718 complaint (_("Invalid DW_AT_alignment"
16719 " - DIE at %s [in module %s]"),
16720 sect_offset_str (die->sect_off),
16721 objfile_name (cu->per_objfile->objfile));
16722 }
16723 else
16724 {
16725 /* Should we also complain about unhandled address classes? */
16726 }
16727 }
16728
16729 TYPE_LENGTH (type) = byte_size;
16730 set_type_align (type, alignment);
16731 return set_die_type (die, type, cu);
16732 }
16733
16734 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16735 the user defined type vector. */
16736
16737 static struct type *
16738 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16739 {
16740 struct type *type;
16741 struct type *to_type;
16742 struct type *domain;
16743
16744 to_type = die_type (die, cu);
16745 domain = die_containing_type (die, cu);
16746
16747 /* The calls above may have already set the type for this DIE. */
16748 type = get_die_type (die, cu);
16749 if (type)
16750 return type;
16751
16752 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16753 type = lookup_methodptr_type (to_type);
16754 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16755 {
16756 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16757
16758 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16759 to_type->fields (), to_type->num_fields (),
16760 TYPE_VARARGS (to_type));
16761 type = lookup_methodptr_type (new_type);
16762 }
16763 else
16764 type = lookup_memberptr_type (to_type, domain);
16765
16766 return set_die_type (die, type, cu);
16767 }
16768
16769 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16770 the user defined type vector. */
16771
16772 static struct type *
16773 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16774 enum type_code refcode)
16775 {
16776 struct comp_unit_head *cu_header = &cu->header;
16777 struct type *type, *target_type;
16778 struct attribute *attr;
16779
16780 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16781
16782 target_type = die_type (die, cu);
16783
16784 /* The die_type call above may have already set the type for this DIE. */
16785 type = get_die_type (die, cu);
16786 if (type)
16787 return type;
16788
16789 type = lookup_reference_type (target_type, refcode);
16790 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16791 if (attr != nullptr)
16792 {
16793 TYPE_LENGTH (type) = DW_UNSND (attr);
16794 }
16795 else
16796 {
16797 TYPE_LENGTH (type) = cu_header->addr_size;
16798 }
16799 maybe_set_alignment (cu, die, type);
16800 return set_die_type (die, type, cu);
16801 }
16802
16803 /* Add the given cv-qualifiers to the element type of the array. GCC
16804 outputs DWARF type qualifiers that apply to an array, not the
16805 element type. But GDB relies on the array element type to carry
16806 the cv-qualifiers. This mimics section 6.7.3 of the C99
16807 specification. */
16808
16809 static struct type *
16810 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16811 struct type *base_type, int cnst, int voltl)
16812 {
16813 struct type *el_type, *inner_array;
16814
16815 base_type = copy_type (base_type);
16816 inner_array = base_type;
16817
16818 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16819 {
16820 TYPE_TARGET_TYPE (inner_array) =
16821 copy_type (TYPE_TARGET_TYPE (inner_array));
16822 inner_array = TYPE_TARGET_TYPE (inner_array);
16823 }
16824
16825 el_type = TYPE_TARGET_TYPE (inner_array);
16826 cnst |= TYPE_CONST (el_type);
16827 voltl |= TYPE_VOLATILE (el_type);
16828 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16829
16830 return set_die_type (die, base_type, cu);
16831 }
16832
16833 static struct type *
16834 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16835 {
16836 struct type *base_type, *cv_type;
16837
16838 base_type = die_type (die, cu);
16839
16840 /* The die_type call above may have already set the type for this DIE. */
16841 cv_type = get_die_type (die, cu);
16842 if (cv_type)
16843 return cv_type;
16844
16845 /* In case the const qualifier is applied to an array type, the element type
16846 is so qualified, not the array type (section 6.7.3 of C99). */
16847 if (base_type->code () == TYPE_CODE_ARRAY)
16848 return add_array_cv_type (die, cu, base_type, 1, 0);
16849
16850 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16851 return set_die_type (die, cv_type, cu);
16852 }
16853
16854 static struct type *
16855 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16856 {
16857 struct type *base_type, *cv_type;
16858
16859 base_type = die_type (die, cu);
16860
16861 /* The die_type call above may have already set the type for this DIE. */
16862 cv_type = get_die_type (die, cu);
16863 if (cv_type)
16864 return cv_type;
16865
16866 /* In case the volatile qualifier is applied to an array type, the
16867 element type is so qualified, not the array type (section 6.7.3
16868 of C99). */
16869 if (base_type->code () == TYPE_CODE_ARRAY)
16870 return add_array_cv_type (die, cu, base_type, 0, 1);
16871
16872 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16873 return set_die_type (die, cv_type, cu);
16874 }
16875
16876 /* Handle DW_TAG_restrict_type. */
16877
16878 static struct type *
16879 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16880 {
16881 struct type *base_type, *cv_type;
16882
16883 base_type = die_type (die, cu);
16884
16885 /* The die_type call above may have already set the type for this DIE. */
16886 cv_type = get_die_type (die, cu);
16887 if (cv_type)
16888 return cv_type;
16889
16890 cv_type = make_restrict_type (base_type);
16891 return set_die_type (die, cv_type, cu);
16892 }
16893
16894 /* Handle DW_TAG_atomic_type. */
16895
16896 static struct type *
16897 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16898 {
16899 struct type *base_type, *cv_type;
16900
16901 base_type = die_type (die, cu);
16902
16903 /* The die_type call above may have already set the type for this DIE. */
16904 cv_type = get_die_type (die, cu);
16905 if (cv_type)
16906 return cv_type;
16907
16908 cv_type = make_atomic_type (base_type);
16909 return set_die_type (die, cv_type, cu);
16910 }
16911
16912 /* Extract all information from a DW_TAG_string_type DIE and add to
16913 the user defined type vector. It isn't really a user defined type,
16914 but it behaves like one, with other DIE's using an AT_user_def_type
16915 attribute to reference it. */
16916
16917 static struct type *
16918 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16919 {
16920 struct objfile *objfile = cu->per_objfile->objfile;
16921 struct gdbarch *gdbarch = objfile->arch ();
16922 struct type *type, *range_type, *index_type, *char_type;
16923 struct attribute *attr;
16924 struct dynamic_prop prop;
16925 bool length_is_constant = true;
16926 LONGEST length;
16927
16928 /* There are a couple of places where bit sizes might be made use of
16929 when parsing a DW_TAG_string_type, however, no producer that we know
16930 of make use of these. Handling bit sizes that are a multiple of the
16931 byte size is easy enough, but what about other bit sizes? Lets deal
16932 with that problem when we have to. Warn about these attributes being
16933 unsupported, then parse the type and ignore them like we always
16934 have. */
16935 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16936 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16937 {
16938 static bool warning_printed = false;
16939 if (!warning_printed)
16940 {
16941 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16942 "currently supported on DW_TAG_string_type."));
16943 warning_printed = true;
16944 }
16945 }
16946
16947 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16948 if (attr != nullptr && !attr->form_is_constant ())
16949 {
16950 /* The string length describes the location at which the length of
16951 the string can be found. The size of the length field can be
16952 specified with one of the attributes below. */
16953 struct type *prop_type;
16954 struct attribute *len
16955 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16956 if (len == nullptr)
16957 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16958 if (len != nullptr && len->form_is_constant ())
16959 {
16960 /* Pass 0 as the default as we know this attribute is constant
16961 and the default value will not be returned. */
16962 LONGEST sz = len->constant_value (0);
16963 prop_type = cu->per_cu->int_type (sz, true);
16964 }
16965 else
16966 {
16967 /* If the size is not specified then we assume it is the size of
16968 an address on this target. */
16969 prop_type = cu->per_cu->addr_sized_int_type (true);
16970 }
16971
16972 /* Convert the attribute into a dynamic property. */
16973 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16974 length = 1;
16975 else
16976 length_is_constant = false;
16977 }
16978 else if (attr != nullptr)
16979 {
16980 /* This DW_AT_string_length just contains the length with no
16981 indirection. There's no need to create a dynamic property in this
16982 case. Pass 0 for the default value as we know it will not be
16983 returned in this case. */
16984 length = attr->constant_value (0);
16985 }
16986 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16987 {
16988 /* We don't currently support non-constant byte sizes for strings. */
16989 length = attr->constant_value (1);
16990 }
16991 else
16992 {
16993 /* Use 1 as a fallback length if we have nothing else. */
16994 length = 1;
16995 }
16996
16997 index_type = objfile_type (objfile)->builtin_int;
16998 if (length_is_constant)
16999 range_type = create_static_range_type (NULL, index_type, 1, length);
17000 else
17001 {
17002 struct dynamic_prop low_bound;
17003
17004 low_bound.kind = PROP_CONST;
17005 low_bound.data.const_val = 1;
17006 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17007 }
17008 char_type = language_string_char_type (cu->language_defn, gdbarch);
17009 type = create_string_type (NULL, char_type, range_type);
17010
17011 return set_die_type (die, type, cu);
17012 }
17013
17014 /* Assuming that DIE corresponds to a function, returns nonzero
17015 if the function is prototyped. */
17016
17017 static int
17018 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17019 {
17020 struct attribute *attr;
17021
17022 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17023 if (attr && (DW_UNSND (attr) != 0))
17024 return 1;
17025
17026 /* The DWARF standard implies that the DW_AT_prototyped attribute
17027 is only meaningful for C, but the concept also extends to other
17028 languages that allow unprototyped functions (Eg: Objective C).
17029 For all other languages, assume that functions are always
17030 prototyped. */
17031 if (cu->language != language_c
17032 && cu->language != language_objc
17033 && cu->language != language_opencl)
17034 return 1;
17035
17036 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17037 prototyped and unprototyped functions; default to prototyped,
17038 since that is more common in modern code (and RealView warns
17039 about unprototyped functions). */
17040 if (producer_is_realview (cu->producer))
17041 return 1;
17042
17043 return 0;
17044 }
17045
17046 /* Handle DIES due to C code like:
17047
17048 struct foo
17049 {
17050 int (*funcp)(int a, long l);
17051 int b;
17052 };
17053
17054 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17055
17056 static struct type *
17057 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17058 {
17059 struct objfile *objfile = cu->per_objfile->objfile;
17060 struct type *type; /* Type that this function returns. */
17061 struct type *ftype; /* Function that returns above type. */
17062 struct attribute *attr;
17063
17064 type = die_type (die, cu);
17065
17066 /* The die_type call above may have already set the type for this DIE. */
17067 ftype = get_die_type (die, cu);
17068 if (ftype)
17069 return ftype;
17070
17071 ftype = lookup_function_type (type);
17072
17073 if (prototyped_function_p (die, cu))
17074 TYPE_PROTOTYPED (ftype) = 1;
17075
17076 /* Store the calling convention in the type if it's available in
17077 the subroutine die. Otherwise set the calling convention to
17078 the default value DW_CC_normal. */
17079 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17080 if (attr != nullptr
17081 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17082 TYPE_CALLING_CONVENTION (ftype)
17083 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17084 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17085 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17086 else
17087 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17088
17089 /* Record whether the function returns normally to its caller or not
17090 if the DWARF producer set that information. */
17091 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17092 if (attr && (DW_UNSND (attr) != 0))
17093 TYPE_NO_RETURN (ftype) = 1;
17094
17095 /* We need to add the subroutine type to the die immediately so
17096 we don't infinitely recurse when dealing with parameters
17097 declared as the same subroutine type. */
17098 set_die_type (die, ftype, cu);
17099
17100 if (die->child != NULL)
17101 {
17102 struct type *void_type = objfile_type (objfile)->builtin_void;
17103 struct die_info *child_die;
17104 int nparams, iparams;
17105
17106 /* Count the number of parameters.
17107 FIXME: GDB currently ignores vararg functions, but knows about
17108 vararg member functions. */
17109 nparams = 0;
17110 child_die = die->child;
17111 while (child_die && child_die->tag)
17112 {
17113 if (child_die->tag == DW_TAG_formal_parameter)
17114 nparams++;
17115 else if (child_die->tag == DW_TAG_unspecified_parameters)
17116 TYPE_VARARGS (ftype) = 1;
17117 child_die = child_die->sibling;
17118 }
17119
17120 /* Allocate storage for parameters and fill them in. */
17121 ftype->set_num_fields (nparams);
17122 ftype->set_fields
17123 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17124
17125 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17126 even if we error out during the parameters reading below. */
17127 for (iparams = 0; iparams < nparams; iparams++)
17128 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17129
17130 iparams = 0;
17131 child_die = die->child;
17132 while (child_die && child_die->tag)
17133 {
17134 if (child_die->tag == DW_TAG_formal_parameter)
17135 {
17136 struct type *arg_type;
17137
17138 /* DWARF version 2 has no clean way to discern C++
17139 static and non-static member functions. G++ helps
17140 GDB by marking the first parameter for non-static
17141 member functions (which is the this pointer) as
17142 artificial. We pass this information to
17143 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17144
17145 DWARF version 3 added DW_AT_object_pointer, which GCC
17146 4.5 does not yet generate. */
17147 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17148 if (attr != nullptr)
17149 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17150 else
17151 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17152 arg_type = die_type (child_die, cu);
17153
17154 /* RealView does not mark THIS as const, which the testsuite
17155 expects. GCC marks THIS as const in method definitions,
17156 but not in the class specifications (GCC PR 43053). */
17157 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17158 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17159 {
17160 int is_this = 0;
17161 struct dwarf2_cu *arg_cu = cu;
17162 const char *name = dwarf2_name (child_die, cu);
17163
17164 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17165 if (attr != nullptr)
17166 {
17167 /* If the compiler emits this, use it. */
17168 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17169 is_this = 1;
17170 }
17171 else if (name && strcmp (name, "this") == 0)
17172 /* Function definitions will have the argument names. */
17173 is_this = 1;
17174 else if (name == NULL && iparams == 0)
17175 /* Declarations may not have the names, so like
17176 elsewhere in GDB, assume an artificial first
17177 argument is "this". */
17178 is_this = 1;
17179
17180 if (is_this)
17181 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17182 arg_type, 0);
17183 }
17184
17185 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17186 iparams++;
17187 }
17188 child_die = child_die->sibling;
17189 }
17190 }
17191
17192 return ftype;
17193 }
17194
17195 static struct type *
17196 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17197 {
17198 struct objfile *objfile = cu->per_objfile->objfile;
17199 const char *name = NULL;
17200 struct type *this_type, *target_type;
17201
17202 name = dwarf2_full_name (NULL, die, cu);
17203 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17204 TYPE_TARGET_STUB (this_type) = 1;
17205 set_die_type (die, this_type, cu);
17206 target_type = die_type (die, cu);
17207 if (target_type != this_type)
17208 TYPE_TARGET_TYPE (this_type) = target_type;
17209 else
17210 {
17211 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17212 spec and cause infinite loops in GDB. */
17213 complaint (_("Self-referential DW_TAG_typedef "
17214 "- DIE at %s [in module %s]"),
17215 sect_offset_str (die->sect_off), objfile_name (objfile));
17216 TYPE_TARGET_TYPE (this_type) = NULL;
17217 }
17218 if (name == NULL)
17219 {
17220 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17221 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17222 Handle these by just returning the target type, rather than
17223 constructing an anonymous typedef type and trying to handle this
17224 elsewhere. */
17225 set_die_type (die, target_type, cu);
17226 return target_type;
17227 }
17228 return this_type;
17229 }
17230
17231 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17232 (which may be different from NAME) to the architecture back-end to allow
17233 it to guess the correct format if necessary. */
17234
17235 static struct type *
17236 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17237 const char *name_hint, enum bfd_endian byte_order)
17238 {
17239 struct gdbarch *gdbarch = objfile->arch ();
17240 const struct floatformat **format;
17241 struct type *type;
17242
17243 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17244 if (format)
17245 type = init_float_type (objfile, bits, name, format, byte_order);
17246 else
17247 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17248
17249 return type;
17250 }
17251
17252 /* Allocate an integer type of size BITS and name NAME. */
17253
17254 static struct type *
17255 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17256 int bits, int unsigned_p, const char *name)
17257 {
17258 struct type *type;
17259
17260 /* Versions of Intel's C Compiler generate an integer type called "void"
17261 instead of using DW_TAG_unspecified_type. This has been seen on
17262 at least versions 14, 17, and 18. */
17263 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17264 && strcmp (name, "void") == 0)
17265 type = objfile_type (objfile)->builtin_void;
17266 else
17267 type = init_integer_type (objfile, bits, unsigned_p, name);
17268
17269 return type;
17270 }
17271
17272 /* Initialise and return a floating point type of size BITS suitable for
17273 use as a component of a complex number. The NAME_HINT is passed through
17274 when initialising the floating point type and is the name of the complex
17275 type.
17276
17277 As DWARF doesn't currently provide an explicit name for the components
17278 of a complex number, but it can be helpful to have these components
17279 named, we try to select a suitable name based on the size of the
17280 component. */
17281 static struct type *
17282 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17283 struct objfile *objfile,
17284 int bits, const char *name_hint,
17285 enum bfd_endian byte_order)
17286 {
17287 gdbarch *gdbarch = objfile->arch ();
17288 struct type *tt = nullptr;
17289
17290 /* Try to find a suitable floating point builtin type of size BITS.
17291 We're going to use the name of this type as the name for the complex
17292 target type that we are about to create. */
17293 switch (cu->language)
17294 {
17295 case language_fortran:
17296 switch (bits)
17297 {
17298 case 32:
17299 tt = builtin_f_type (gdbarch)->builtin_real;
17300 break;
17301 case 64:
17302 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17303 break;
17304 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17305 case 128:
17306 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17307 break;
17308 }
17309 break;
17310 default:
17311 switch (bits)
17312 {
17313 case 32:
17314 tt = builtin_type (gdbarch)->builtin_float;
17315 break;
17316 case 64:
17317 tt = builtin_type (gdbarch)->builtin_double;
17318 break;
17319 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17320 case 128:
17321 tt = builtin_type (gdbarch)->builtin_long_double;
17322 break;
17323 }
17324 break;
17325 }
17326
17327 /* If the type we found doesn't match the size we were looking for, then
17328 pretend we didn't find a type at all, the complex target type we
17329 create will then be nameless. */
17330 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17331 tt = nullptr;
17332
17333 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17334 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17335 }
17336
17337 /* Find a representation of a given base type and install
17338 it in the TYPE field of the die. */
17339
17340 static struct type *
17341 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17342 {
17343 struct objfile *objfile = cu->per_objfile->objfile;
17344 struct type *type;
17345 struct attribute *attr;
17346 int encoding = 0, bits = 0;
17347 const char *name;
17348 gdbarch *arch;
17349
17350 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17351 if (attr != nullptr)
17352 encoding = DW_UNSND (attr);
17353 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17354 if (attr != nullptr)
17355 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17356 name = dwarf2_name (die, cu);
17357 if (!name)
17358 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17359
17360 arch = objfile->arch ();
17361 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17362
17363 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17364 if (attr)
17365 {
17366 int endianity = DW_UNSND (attr);
17367
17368 switch (endianity)
17369 {
17370 case DW_END_big:
17371 byte_order = BFD_ENDIAN_BIG;
17372 break;
17373 case DW_END_little:
17374 byte_order = BFD_ENDIAN_LITTLE;
17375 break;
17376 default:
17377 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17378 break;
17379 }
17380 }
17381
17382 switch (encoding)
17383 {
17384 case DW_ATE_address:
17385 /* Turn DW_ATE_address into a void * pointer. */
17386 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17387 type = init_pointer_type (objfile, bits, name, type);
17388 break;
17389 case DW_ATE_boolean:
17390 type = init_boolean_type (objfile, bits, 1, name);
17391 break;
17392 case DW_ATE_complex_float:
17393 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17394 byte_order);
17395 if (type->code () == TYPE_CODE_ERROR)
17396 {
17397 if (name == nullptr)
17398 {
17399 struct obstack *obstack
17400 = &cu->per_objfile->objfile->objfile_obstack;
17401 name = obconcat (obstack, "_Complex ", type->name (),
17402 nullptr);
17403 }
17404 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17405 }
17406 else
17407 type = init_complex_type (name, type);
17408 break;
17409 case DW_ATE_decimal_float:
17410 type = init_decfloat_type (objfile, bits, name);
17411 break;
17412 case DW_ATE_float:
17413 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17414 break;
17415 case DW_ATE_signed:
17416 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17417 break;
17418 case DW_ATE_unsigned:
17419 if (cu->language == language_fortran
17420 && name
17421 && startswith (name, "character("))
17422 type = init_character_type (objfile, bits, 1, name);
17423 else
17424 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17425 break;
17426 case DW_ATE_signed_char:
17427 if (cu->language == language_ada || cu->language == language_m2
17428 || cu->language == language_pascal
17429 || cu->language == language_fortran)
17430 type = init_character_type (objfile, bits, 0, name);
17431 else
17432 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17433 break;
17434 case DW_ATE_unsigned_char:
17435 if (cu->language == language_ada || cu->language == language_m2
17436 || cu->language == language_pascal
17437 || cu->language == language_fortran
17438 || cu->language == language_rust)
17439 type = init_character_type (objfile, bits, 1, name);
17440 else
17441 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17442 break;
17443 case DW_ATE_UTF:
17444 {
17445 if (bits == 16)
17446 type = builtin_type (arch)->builtin_char16;
17447 else if (bits == 32)
17448 type = builtin_type (arch)->builtin_char32;
17449 else
17450 {
17451 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17452 bits);
17453 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17454 }
17455 return set_die_type (die, type, cu);
17456 }
17457 break;
17458
17459 default:
17460 complaint (_("unsupported DW_AT_encoding: '%s'"),
17461 dwarf_type_encoding_name (encoding));
17462 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17463 break;
17464 }
17465
17466 if (name && strcmp (name, "char") == 0)
17467 TYPE_NOSIGN (type) = 1;
17468
17469 maybe_set_alignment (cu, die, type);
17470
17471 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17472
17473 return set_die_type (die, type, cu);
17474 }
17475
17476 /* Parse dwarf attribute if it's a block, reference or constant and put the
17477 resulting value of the attribute into struct bound_prop.
17478 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17479
17480 static int
17481 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17482 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17483 struct type *default_type)
17484 {
17485 struct dwarf2_property_baton *baton;
17486 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17487 struct objfile *objfile = per_objfile->objfile;
17488 struct obstack *obstack = &objfile->objfile_obstack;
17489
17490 gdb_assert (default_type != NULL);
17491
17492 if (attr == NULL || prop == NULL)
17493 return 0;
17494
17495 if (attr->form_is_block ())
17496 {
17497 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17498 baton->property_type = default_type;
17499 baton->locexpr.per_cu = cu->per_cu;
17500 baton->locexpr.per_objfile = per_objfile;
17501 baton->locexpr.size = DW_BLOCK (attr)->size;
17502 baton->locexpr.data = DW_BLOCK (attr)->data;
17503 switch (attr->name)
17504 {
17505 case DW_AT_string_length:
17506 baton->locexpr.is_reference = true;
17507 break;
17508 default:
17509 baton->locexpr.is_reference = false;
17510 break;
17511 }
17512 prop->data.baton = baton;
17513 prop->kind = PROP_LOCEXPR;
17514 gdb_assert (prop->data.baton != NULL);
17515 }
17516 else if (attr->form_is_ref ())
17517 {
17518 struct dwarf2_cu *target_cu = cu;
17519 struct die_info *target_die;
17520 struct attribute *target_attr;
17521
17522 target_die = follow_die_ref (die, attr, &target_cu);
17523 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17524 if (target_attr == NULL)
17525 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17526 target_cu);
17527 if (target_attr == NULL)
17528 return 0;
17529
17530 switch (target_attr->name)
17531 {
17532 case DW_AT_location:
17533 if (target_attr->form_is_section_offset ())
17534 {
17535 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17536 baton->property_type = die_type (target_die, target_cu);
17537 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17538 prop->data.baton = baton;
17539 prop->kind = PROP_LOCLIST;
17540 gdb_assert (prop->data.baton != NULL);
17541 }
17542 else if (target_attr->form_is_block ())
17543 {
17544 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17545 baton->property_type = die_type (target_die, target_cu);
17546 baton->locexpr.per_cu = cu->per_cu;
17547 baton->locexpr.per_objfile = per_objfile;
17548 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17549 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17550 baton->locexpr.is_reference = true;
17551 prop->data.baton = baton;
17552 prop->kind = PROP_LOCEXPR;
17553 gdb_assert (prop->data.baton != NULL);
17554 }
17555 else
17556 {
17557 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17558 "dynamic property");
17559 return 0;
17560 }
17561 break;
17562 case DW_AT_data_member_location:
17563 {
17564 LONGEST offset;
17565
17566 if (!handle_data_member_location (target_die, target_cu,
17567 &offset))
17568 return 0;
17569
17570 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17571 baton->property_type = read_type_die (target_die->parent,
17572 target_cu);
17573 baton->offset_info.offset = offset;
17574 baton->offset_info.type = die_type (target_die, target_cu);
17575 prop->data.baton = baton;
17576 prop->kind = PROP_ADDR_OFFSET;
17577 break;
17578 }
17579 }
17580 }
17581 else if (attr->form_is_constant ())
17582 {
17583 prop->data.const_val = attr->constant_value (0);
17584 prop->kind = PROP_CONST;
17585 }
17586 else
17587 {
17588 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17589 dwarf2_name (die, cu));
17590 return 0;
17591 }
17592
17593 return 1;
17594 }
17595
17596 /* See read.h. */
17597
17598 struct type *
17599 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17600 {
17601 struct objfile *objfile = dwarf2_per_objfile->objfile;
17602 struct type *int_type;
17603
17604 /* Helper macro to examine the various builtin types. */
17605 #define TRY_TYPE(F) \
17606 int_type = (unsigned_p \
17607 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17608 : objfile_type (objfile)->builtin_ ## F); \
17609 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17610 return int_type
17611
17612 TRY_TYPE (char);
17613 TRY_TYPE (short);
17614 TRY_TYPE (int);
17615 TRY_TYPE (long);
17616 TRY_TYPE (long_long);
17617
17618 #undef TRY_TYPE
17619
17620 gdb_assert_not_reached ("unable to find suitable integer type");
17621 }
17622
17623 /* See read.h. */
17624
17625 struct type *
17626 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17627 {
17628 int addr_size = this->addr_size ();
17629 return int_type (addr_size, unsigned_p);
17630 }
17631
17632 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17633 present (which is valid) then compute the default type based on the
17634 compilation units address size. */
17635
17636 static struct type *
17637 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17638 {
17639 struct type *index_type = die_type (die, cu);
17640
17641 /* Dwarf-2 specifications explicitly allows to create subrange types
17642 without specifying a base type.
17643 In that case, the base type must be set to the type of
17644 the lower bound, upper bound or count, in that order, if any of these
17645 three attributes references an object that has a type.
17646 If no base type is found, the Dwarf-2 specifications say that
17647 a signed integer type of size equal to the size of an address should
17648 be used.
17649 For the following C code: `extern char gdb_int [];'
17650 GCC produces an empty range DIE.
17651 FIXME: muller/2010-05-28: Possible references to object for low bound,
17652 high bound or count are not yet handled by this code. */
17653 if (index_type->code () == TYPE_CODE_VOID)
17654 index_type = cu->per_cu->addr_sized_int_type (false);
17655
17656 return index_type;
17657 }
17658
17659 /* Read the given DW_AT_subrange DIE. */
17660
17661 static struct type *
17662 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17663 {
17664 struct type *base_type, *orig_base_type;
17665 struct type *range_type;
17666 struct attribute *attr;
17667 struct dynamic_prop low, high;
17668 int low_default_is_valid;
17669 int high_bound_is_count = 0;
17670 const char *name;
17671 ULONGEST negative_mask;
17672
17673 orig_base_type = read_subrange_index_type (die, cu);
17674
17675 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17676 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17677 creating the range type, but we use the result of check_typedef
17678 when examining properties of the type. */
17679 base_type = check_typedef (orig_base_type);
17680
17681 /* The die_type call above may have already set the type for this DIE. */
17682 range_type = get_die_type (die, cu);
17683 if (range_type)
17684 return range_type;
17685
17686 low.kind = PROP_CONST;
17687 high.kind = PROP_CONST;
17688 high.data.const_val = 0;
17689
17690 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17691 omitting DW_AT_lower_bound. */
17692 switch (cu->language)
17693 {
17694 case language_c:
17695 case language_cplus:
17696 low.data.const_val = 0;
17697 low_default_is_valid = 1;
17698 break;
17699 case language_fortran:
17700 low.data.const_val = 1;
17701 low_default_is_valid = 1;
17702 break;
17703 case language_d:
17704 case language_objc:
17705 case language_rust:
17706 low.data.const_val = 0;
17707 low_default_is_valid = (cu->header.version >= 4);
17708 break;
17709 case language_ada:
17710 case language_m2:
17711 case language_pascal:
17712 low.data.const_val = 1;
17713 low_default_is_valid = (cu->header.version >= 4);
17714 break;
17715 default:
17716 low.data.const_val = 0;
17717 low_default_is_valid = 0;
17718 break;
17719 }
17720
17721 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17722 if (attr != nullptr)
17723 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17724 else if (!low_default_is_valid)
17725 complaint (_("Missing DW_AT_lower_bound "
17726 "- DIE at %s [in module %s]"),
17727 sect_offset_str (die->sect_off),
17728 objfile_name (cu->per_objfile->objfile));
17729
17730 struct attribute *attr_ub, *attr_count;
17731 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17732 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17733 {
17734 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17735 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17736 {
17737 /* If bounds are constant do the final calculation here. */
17738 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17739 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17740 else
17741 high_bound_is_count = 1;
17742 }
17743 else
17744 {
17745 if (attr_ub != NULL)
17746 complaint (_("Unresolved DW_AT_upper_bound "
17747 "- DIE at %s [in module %s]"),
17748 sect_offset_str (die->sect_off),
17749 objfile_name (cu->per_objfile->objfile));
17750 if (attr_count != NULL)
17751 complaint (_("Unresolved DW_AT_count "
17752 "- DIE at %s [in module %s]"),
17753 sect_offset_str (die->sect_off),
17754 objfile_name (cu->per_objfile->objfile));
17755 }
17756 }
17757
17758 LONGEST bias = 0;
17759 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17760 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17761 bias = bias_attr->constant_value (0);
17762
17763 /* Normally, the DWARF producers are expected to use a signed
17764 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17765 But this is unfortunately not always the case, as witnessed
17766 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17767 is used instead. To work around that ambiguity, we treat
17768 the bounds as signed, and thus sign-extend their values, when
17769 the base type is signed. */
17770 negative_mask =
17771 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17772 if (low.kind == PROP_CONST
17773 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17774 low.data.const_val |= negative_mask;
17775 if (high.kind == PROP_CONST
17776 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17777 high.data.const_val |= negative_mask;
17778
17779 /* Check for bit and byte strides. */
17780 struct dynamic_prop byte_stride_prop;
17781 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17782 if (attr_byte_stride != nullptr)
17783 {
17784 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17785 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17786 prop_type);
17787 }
17788
17789 struct dynamic_prop bit_stride_prop;
17790 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17791 if (attr_bit_stride != nullptr)
17792 {
17793 /* It only makes sense to have either a bit or byte stride. */
17794 if (attr_byte_stride != nullptr)
17795 {
17796 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17797 "- DIE at %s [in module %s]"),
17798 sect_offset_str (die->sect_off),
17799 objfile_name (cu->per_objfile->objfile));
17800 attr_bit_stride = nullptr;
17801 }
17802 else
17803 {
17804 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17805 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17806 prop_type);
17807 }
17808 }
17809
17810 if (attr_byte_stride != nullptr
17811 || attr_bit_stride != nullptr)
17812 {
17813 bool byte_stride_p = (attr_byte_stride != nullptr);
17814 struct dynamic_prop *stride
17815 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17816
17817 range_type
17818 = create_range_type_with_stride (NULL, orig_base_type, &low,
17819 &high, bias, stride, byte_stride_p);
17820 }
17821 else
17822 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17823
17824 if (high_bound_is_count)
17825 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17826
17827 /* Ada expects an empty array on no boundary attributes. */
17828 if (attr == NULL && cu->language != language_ada)
17829 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17830
17831 name = dwarf2_name (die, cu);
17832 if (name)
17833 range_type->set_name (name);
17834
17835 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17836 if (attr != nullptr)
17837 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17838
17839 maybe_set_alignment (cu, die, range_type);
17840
17841 set_die_type (die, range_type, cu);
17842
17843 /* set_die_type should be already done. */
17844 set_descriptive_type (range_type, die, cu);
17845
17846 return range_type;
17847 }
17848
17849 static struct type *
17850 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17851 {
17852 struct type *type;
17853
17854 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17855 type->set_name (dwarf2_name (die, cu));
17856
17857 /* In Ada, an unspecified type is typically used when the description
17858 of the type is deferred to a different unit. When encountering
17859 such a type, we treat it as a stub, and try to resolve it later on,
17860 when needed. */
17861 if (cu->language == language_ada)
17862 TYPE_STUB (type) = 1;
17863
17864 return set_die_type (die, type, cu);
17865 }
17866
17867 /* Read a single die and all its descendents. Set the die's sibling
17868 field to NULL; set other fields in the die correctly, and set all
17869 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17870 location of the info_ptr after reading all of those dies. PARENT
17871 is the parent of the die in question. */
17872
17873 static struct die_info *
17874 read_die_and_children (const struct die_reader_specs *reader,
17875 const gdb_byte *info_ptr,
17876 const gdb_byte **new_info_ptr,
17877 struct die_info *parent)
17878 {
17879 struct die_info *die;
17880 const gdb_byte *cur_ptr;
17881
17882 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17883 if (die == NULL)
17884 {
17885 *new_info_ptr = cur_ptr;
17886 return NULL;
17887 }
17888 store_in_ref_table (die, reader->cu);
17889
17890 if (die->has_children)
17891 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17892 else
17893 {
17894 die->child = NULL;
17895 *new_info_ptr = cur_ptr;
17896 }
17897
17898 die->sibling = NULL;
17899 die->parent = parent;
17900 return die;
17901 }
17902
17903 /* Read a die, all of its descendents, and all of its siblings; set
17904 all of the fields of all of the dies correctly. Arguments are as
17905 in read_die_and_children. */
17906
17907 static struct die_info *
17908 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17909 const gdb_byte *info_ptr,
17910 const gdb_byte **new_info_ptr,
17911 struct die_info *parent)
17912 {
17913 struct die_info *first_die, *last_sibling;
17914 const gdb_byte *cur_ptr;
17915
17916 cur_ptr = info_ptr;
17917 first_die = last_sibling = NULL;
17918
17919 while (1)
17920 {
17921 struct die_info *die
17922 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17923
17924 if (die == NULL)
17925 {
17926 *new_info_ptr = cur_ptr;
17927 return first_die;
17928 }
17929
17930 if (!first_die)
17931 first_die = die;
17932 else
17933 last_sibling->sibling = die;
17934
17935 last_sibling = die;
17936 }
17937 }
17938
17939 /* Read a die, all of its descendents, and all of its siblings; set
17940 all of the fields of all of the dies correctly. Arguments are as
17941 in read_die_and_children.
17942 This the main entry point for reading a DIE and all its children. */
17943
17944 static struct die_info *
17945 read_die_and_siblings (const struct die_reader_specs *reader,
17946 const gdb_byte *info_ptr,
17947 const gdb_byte **new_info_ptr,
17948 struct die_info *parent)
17949 {
17950 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17951 new_info_ptr, parent);
17952
17953 if (dwarf_die_debug)
17954 {
17955 fprintf_unfiltered (gdb_stdlog,
17956 "Read die from %s@0x%x of %s:\n",
17957 reader->die_section->get_name (),
17958 (unsigned) (info_ptr - reader->die_section->buffer),
17959 bfd_get_filename (reader->abfd));
17960 dump_die (die, dwarf_die_debug);
17961 }
17962
17963 return die;
17964 }
17965
17966 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17967 attributes.
17968 The caller is responsible for filling in the extra attributes
17969 and updating (*DIEP)->num_attrs.
17970 Set DIEP to point to a newly allocated die with its information,
17971 except for its child, sibling, and parent fields. */
17972
17973 static const gdb_byte *
17974 read_full_die_1 (const struct die_reader_specs *reader,
17975 struct die_info **diep, const gdb_byte *info_ptr,
17976 int num_extra_attrs)
17977 {
17978 unsigned int abbrev_number, bytes_read, i;
17979 struct abbrev_info *abbrev;
17980 struct die_info *die;
17981 struct dwarf2_cu *cu = reader->cu;
17982 bfd *abfd = reader->abfd;
17983
17984 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17985 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17986 info_ptr += bytes_read;
17987 if (!abbrev_number)
17988 {
17989 *diep = NULL;
17990 return info_ptr;
17991 }
17992
17993 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17994 if (!abbrev)
17995 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17996 abbrev_number,
17997 bfd_get_filename (abfd));
17998
17999 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18000 die->sect_off = sect_off;
18001 die->tag = abbrev->tag;
18002 die->abbrev = abbrev_number;
18003 die->has_children = abbrev->has_children;
18004
18005 /* Make the result usable.
18006 The caller needs to update num_attrs after adding the extra
18007 attributes. */
18008 die->num_attrs = abbrev->num_attrs;
18009
18010 std::vector<int> indexes_that_need_reprocess;
18011 for (i = 0; i < abbrev->num_attrs; ++i)
18012 {
18013 bool need_reprocess;
18014 info_ptr =
18015 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18016 info_ptr, &need_reprocess);
18017 if (need_reprocess)
18018 indexes_that_need_reprocess.push_back (i);
18019 }
18020
18021 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18022 if (attr != nullptr)
18023 cu->str_offsets_base = DW_UNSND (attr);
18024
18025 attr = die->attr (DW_AT_loclists_base);
18026 if (attr != nullptr)
18027 cu->loclist_base = DW_UNSND (attr);
18028
18029 auto maybe_addr_base = die->addr_base ();
18030 if (maybe_addr_base.has_value ())
18031 cu->addr_base = *maybe_addr_base;
18032 for (int index : indexes_that_need_reprocess)
18033 read_attribute_reprocess (reader, &die->attrs[index]);
18034 *diep = die;
18035 return info_ptr;
18036 }
18037
18038 /* Read a die and all its attributes.
18039 Set DIEP to point to a newly allocated die with its information,
18040 except for its child, sibling, and parent fields. */
18041
18042 static const gdb_byte *
18043 read_full_die (const struct die_reader_specs *reader,
18044 struct die_info **diep, const gdb_byte *info_ptr)
18045 {
18046 const gdb_byte *result;
18047
18048 result = read_full_die_1 (reader, diep, info_ptr, 0);
18049
18050 if (dwarf_die_debug)
18051 {
18052 fprintf_unfiltered (gdb_stdlog,
18053 "Read die from %s@0x%x of %s:\n",
18054 reader->die_section->get_name (),
18055 (unsigned) (info_ptr - reader->die_section->buffer),
18056 bfd_get_filename (reader->abfd));
18057 dump_die (*diep, dwarf_die_debug);
18058 }
18059
18060 return result;
18061 }
18062 \f
18063
18064 /* Returns nonzero if TAG represents a type that we might generate a partial
18065 symbol for. */
18066
18067 static int
18068 is_type_tag_for_partial (int tag)
18069 {
18070 switch (tag)
18071 {
18072 #if 0
18073 /* Some types that would be reasonable to generate partial symbols for,
18074 that we don't at present. */
18075 case DW_TAG_array_type:
18076 case DW_TAG_file_type:
18077 case DW_TAG_ptr_to_member_type:
18078 case DW_TAG_set_type:
18079 case DW_TAG_string_type:
18080 case DW_TAG_subroutine_type:
18081 #endif
18082 case DW_TAG_base_type:
18083 case DW_TAG_class_type:
18084 case DW_TAG_interface_type:
18085 case DW_TAG_enumeration_type:
18086 case DW_TAG_structure_type:
18087 case DW_TAG_subrange_type:
18088 case DW_TAG_typedef:
18089 case DW_TAG_union_type:
18090 return 1;
18091 default:
18092 return 0;
18093 }
18094 }
18095
18096 /* Load all DIEs that are interesting for partial symbols into memory. */
18097
18098 static struct partial_die_info *
18099 load_partial_dies (const struct die_reader_specs *reader,
18100 const gdb_byte *info_ptr, int building_psymtab)
18101 {
18102 struct dwarf2_cu *cu = reader->cu;
18103 struct objfile *objfile = cu->per_objfile->objfile;
18104 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18105 unsigned int bytes_read;
18106 unsigned int load_all = 0;
18107 int nesting_level = 1;
18108
18109 parent_die = NULL;
18110 last_die = NULL;
18111
18112 gdb_assert (cu->per_cu != NULL);
18113 if (cu->per_cu->load_all_dies)
18114 load_all = 1;
18115
18116 cu->partial_dies
18117 = htab_create_alloc_ex (cu->header.length / 12,
18118 partial_die_hash,
18119 partial_die_eq,
18120 NULL,
18121 &cu->comp_unit_obstack,
18122 hashtab_obstack_allocate,
18123 dummy_obstack_deallocate);
18124
18125 while (1)
18126 {
18127 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18128
18129 /* A NULL abbrev means the end of a series of children. */
18130 if (abbrev == NULL)
18131 {
18132 if (--nesting_level == 0)
18133 return first_die;
18134
18135 info_ptr += bytes_read;
18136 last_die = parent_die;
18137 parent_die = parent_die->die_parent;
18138 continue;
18139 }
18140
18141 /* Check for template arguments. We never save these; if
18142 they're seen, we just mark the parent, and go on our way. */
18143 if (parent_die != NULL
18144 && cu->language == language_cplus
18145 && (abbrev->tag == DW_TAG_template_type_param
18146 || abbrev->tag == DW_TAG_template_value_param))
18147 {
18148 parent_die->has_template_arguments = 1;
18149
18150 if (!load_all)
18151 {
18152 /* We don't need a partial DIE for the template argument. */
18153 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18154 continue;
18155 }
18156 }
18157
18158 /* We only recurse into c++ subprograms looking for template arguments.
18159 Skip their other children. */
18160 if (!load_all
18161 && cu->language == language_cplus
18162 && parent_die != NULL
18163 && parent_die->tag == DW_TAG_subprogram)
18164 {
18165 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18166 continue;
18167 }
18168
18169 /* Check whether this DIE is interesting enough to save. Normally
18170 we would not be interested in members here, but there may be
18171 later variables referencing them via DW_AT_specification (for
18172 static members). */
18173 if (!load_all
18174 && !is_type_tag_for_partial (abbrev->tag)
18175 && abbrev->tag != DW_TAG_constant
18176 && abbrev->tag != DW_TAG_enumerator
18177 && abbrev->tag != DW_TAG_subprogram
18178 && abbrev->tag != DW_TAG_inlined_subroutine
18179 && abbrev->tag != DW_TAG_lexical_block
18180 && abbrev->tag != DW_TAG_variable
18181 && abbrev->tag != DW_TAG_namespace
18182 && abbrev->tag != DW_TAG_module
18183 && abbrev->tag != DW_TAG_member
18184 && abbrev->tag != DW_TAG_imported_unit
18185 && abbrev->tag != DW_TAG_imported_declaration)
18186 {
18187 /* Otherwise we skip to the next sibling, if any. */
18188 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18189 continue;
18190 }
18191
18192 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18193 abbrev);
18194
18195 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18196
18197 /* This two-pass algorithm for processing partial symbols has a
18198 high cost in cache pressure. Thus, handle some simple cases
18199 here which cover the majority of C partial symbols. DIEs
18200 which neither have specification tags in them, nor could have
18201 specification tags elsewhere pointing at them, can simply be
18202 processed and discarded.
18203
18204 This segment is also optional; scan_partial_symbols and
18205 add_partial_symbol will handle these DIEs if we chain
18206 them in normally. When compilers which do not emit large
18207 quantities of duplicate debug information are more common,
18208 this code can probably be removed. */
18209
18210 /* Any complete simple types at the top level (pretty much all
18211 of them, for a language without namespaces), can be processed
18212 directly. */
18213 if (parent_die == NULL
18214 && pdi.has_specification == 0
18215 && pdi.is_declaration == 0
18216 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18217 || pdi.tag == DW_TAG_base_type
18218 || pdi.tag == DW_TAG_subrange_type))
18219 {
18220 if (building_psymtab && pdi.name != NULL)
18221 add_psymbol_to_list (pdi.name, false,
18222 VAR_DOMAIN, LOC_TYPEDEF, -1,
18223 psymbol_placement::STATIC,
18224 0, cu->language, objfile);
18225 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18226 continue;
18227 }
18228
18229 /* The exception for DW_TAG_typedef with has_children above is
18230 a workaround of GCC PR debug/47510. In the case of this complaint
18231 type_name_or_error will error on such types later.
18232
18233 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18234 it could not find the child DIEs referenced later, this is checked
18235 above. In correct DWARF DW_TAG_typedef should have no children. */
18236
18237 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18238 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18239 "- DIE at %s [in module %s]"),
18240 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18241
18242 /* If we're at the second level, and we're an enumerator, and
18243 our parent has no specification (meaning possibly lives in a
18244 namespace elsewhere), then we can add the partial symbol now
18245 instead of queueing it. */
18246 if (pdi.tag == DW_TAG_enumerator
18247 && parent_die != NULL
18248 && parent_die->die_parent == NULL
18249 && parent_die->tag == DW_TAG_enumeration_type
18250 && parent_die->has_specification == 0)
18251 {
18252 if (pdi.name == NULL)
18253 complaint (_("malformed enumerator DIE ignored"));
18254 else if (building_psymtab)
18255 add_psymbol_to_list (pdi.name, false,
18256 VAR_DOMAIN, LOC_CONST, -1,
18257 cu->language == language_cplus
18258 ? psymbol_placement::GLOBAL
18259 : psymbol_placement::STATIC,
18260 0, cu->language, objfile);
18261
18262 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18263 continue;
18264 }
18265
18266 struct partial_die_info *part_die
18267 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18268
18269 /* We'll save this DIE so link it in. */
18270 part_die->die_parent = parent_die;
18271 part_die->die_sibling = NULL;
18272 part_die->die_child = NULL;
18273
18274 if (last_die && last_die == parent_die)
18275 last_die->die_child = part_die;
18276 else if (last_die)
18277 last_die->die_sibling = part_die;
18278
18279 last_die = part_die;
18280
18281 if (first_die == NULL)
18282 first_die = part_die;
18283
18284 /* Maybe add the DIE to the hash table. Not all DIEs that we
18285 find interesting need to be in the hash table, because we
18286 also have the parent/sibling/child chains; only those that we
18287 might refer to by offset later during partial symbol reading.
18288
18289 For now this means things that might have be the target of a
18290 DW_AT_specification, DW_AT_abstract_origin, or
18291 DW_AT_extension. DW_AT_extension will refer only to
18292 namespaces; DW_AT_abstract_origin refers to functions (and
18293 many things under the function DIE, but we do not recurse
18294 into function DIEs during partial symbol reading) and
18295 possibly variables as well; DW_AT_specification refers to
18296 declarations. Declarations ought to have the DW_AT_declaration
18297 flag. It happens that GCC forgets to put it in sometimes, but
18298 only for functions, not for types.
18299
18300 Adding more things than necessary to the hash table is harmless
18301 except for the performance cost. Adding too few will result in
18302 wasted time in find_partial_die, when we reread the compilation
18303 unit with load_all_dies set. */
18304
18305 if (load_all
18306 || abbrev->tag == DW_TAG_constant
18307 || abbrev->tag == DW_TAG_subprogram
18308 || abbrev->tag == DW_TAG_variable
18309 || abbrev->tag == DW_TAG_namespace
18310 || part_die->is_declaration)
18311 {
18312 void **slot;
18313
18314 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18315 to_underlying (part_die->sect_off),
18316 INSERT);
18317 *slot = part_die;
18318 }
18319
18320 /* For some DIEs we want to follow their children (if any). For C
18321 we have no reason to follow the children of structures; for other
18322 languages we have to, so that we can get at method physnames
18323 to infer fully qualified class names, for DW_AT_specification,
18324 and for C++ template arguments. For C++, we also look one level
18325 inside functions to find template arguments (if the name of the
18326 function does not already contain the template arguments).
18327
18328 For Ada and Fortran, we need to scan the children of subprograms
18329 and lexical blocks as well because these languages allow the
18330 definition of nested entities that could be interesting for the
18331 debugger, such as nested subprograms for instance. */
18332 if (last_die->has_children
18333 && (load_all
18334 || last_die->tag == DW_TAG_namespace
18335 || last_die->tag == DW_TAG_module
18336 || last_die->tag == DW_TAG_enumeration_type
18337 || (cu->language == language_cplus
18338 && last_die->tag == DW_TAG_subprogram
18339 && (last_die->name == NULL
18340 || strchr (last_die->name, '<') == NULL))
18341 || (cu->language != language_c
18342 && (last_die->tag == DW_TAG_class_type
18343 || last_die->tag == DW_TAG_interface_type
18344 || last_die->tag == DW_TAG_structure_type
18345 || last_die->tag == DW_TAG_union_type))
18346 || ((cu->language == language_ada
18347 || cu->language == language_fortran)
18348 && (last_die->tag == DW_TAG_subprogram
18349 || last_die->tag == DW_TAG_lexical_block))))
18350 {
18351 nesting_level++;
18352 parent_die = last_die;
18353 continue;
18354 }
18355
18356 /* Otherwise we skip to the next sibling, if any. */
18357 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18358
18359 /* Back to the top, do it again. */
18360 }
18361 }
18362
18363 partial_die_info::partial_die_info (sect_offset sect_off_,
18364 struct abbrev_info *abbrev)
18365 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18366 {
18367 }
18368
18369 /* Read a minimal amount of information into the minimal die structure.
18370 INFO_PTR should point just after the initial uleb128 of a DIE. */
18371
18372 const gdb_byte *
18373 partial_die_info::read (const struct die_reader_specs *reader,
18374 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18375 {
18376 struct dwarf2_cu *cu = reader->cu;
18377 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18378 unsigned int i;
18379 int has_low_pc_attr = 0;
18380 int has_high_pc_attr = 0;
18381 int high_pc_relative = 0;
18382
18383 for (i = 0; i < abbrev.num_attrs; ++i)
18384 {
18385 attribute attr;
18386 bool need_reprocess;
18387 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18388 info_ptr, &need_reprocess);
18389 /* String and address offsets that need to do the reprocessing have
18390 already been read at this point, so there is no need to wait until
18391 the loop terminates to do the reprocessing. */
18392 if (need_reprocess)
18393 read_attribute_reprocess (reader, &attr);
18394 /* Store the data if it is of an attribute we want to keep in a
18395 partial symbol table. */
18396 switch (attr.name)
18397 {
18398 case DW_AT_name:
18399 switch (tag)
18400 {
18401 case DW_TAG_compile_unit:
18402 case DW_TAG_partial_unit:
18403 case DW_TAG_type_unit:
18404 /* Compilation units have a DW_AT_name that is a filename, not
18405 a source language identifier. */
18406 case DW_TAG_enumeration_type:
18407 case DW_TAG_enumerator:
18408 /* These tags always have simple identifiers already; no need
18409 to canonicalize them. */
18410 name = DW_STRING (&attr);
18411 break;
18412 default:
18413 {
18414 struct objfile *objfile = dwarf2_per_objfile->objfile;
18415
18416 name
18417 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18418 }
18419 break;
18420 }
18421 break;
18422 case DW_AT_linkage_name:
18423 case DW_AT_MIPS_linkage_name:
18424 /* Note that both forms of linkage name might appear. We
18425 assume they will be the same, and we only store the last
18426 one we see. */
18427 linkage_name = attr.value_as_string ();
18428 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18429 See https://github.com/rust-lang/rust/issues/32925. */
18430 if (cu->language == language_rust && linkage_name != NULL
18431 && strchr (linkage_name, '{') != NULL)
18432 linkage_name = NULL;
18433 break;
18434 case DW_AT_low_pc:
18435 has_low_pc_attr = 1;
18436 lowpc = attr.value_as_address ();
18437 break;
18438 case DW_AT_high_pc:
18439 has_high_pc_attr = 1;
18440 highpc = attr.value_as_address ();
18441 if (cu->header.version >= 4 && attr.form_is_constant ())
18442 high_pc_relative = 1;
18443 break;
18444 case DW_AT_location:
18445 /* Support the .debug_loc offsets. */
18446 if (attr.form_is_block ())
18447 {
18448 d.locdesc = DW_BLOCK (&attr);
18449 }
18450 else if (attr.form_is_section_offset ())
18451 {
18452 dwarf2_complex_location_expr_complaint ();
18453 }
18454 else
18455 {
18456 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18457 "partial symbol information");
18458 }
18459 break;
18460 case DW_AT_external:
18461 is_external = DW_UNSND (&attr);
18462 break;
18463 case DW_AT_declaration:
18464 is_declaration = DW_UNSND (&attr);
18465 break;
18466 case DW_AT_type:
18467 has_type = 1;
18468 break;
18469 case DW_AT_abstract_origin:
18470 case DW_AT_specification:
18471 case DW_AT_extension:
18472 has_specification = 1;
18473 spec_offset = attr.get_ref_die_offset ();
18474 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18475 || cu->per_cu->is_dwz);
18476 break;
18477 case DW_AT_sibling:
18478 /* Ignore absolute siblings, they might point outside of
18479 the current compile unit. */
18480 if (attr.form == DW_FORM_ref_addr)
18481 complaint (_("ignoring absolute DW_AT_sibling"));
18482 else
18483 {
18484 const gdb_byte *buffer = reader->buffer;
18485 sect_offset off = attr.get_ref_die_offset ();
18486 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18487
18488 if (sibling_ptr < info_ptr)
18489 complaint (_("DW_AT_sibling points backwards"));
18490 else if (sibling_ptr > reader->buffer_end)
18491 reader->die_section->overflow_complaint ();
18492 else
18493 sibling = sibling_ptr;
18494 }
18495 break;
18496 case DW_AT_byte_size:
18497 has_byte_size = 1;
18498 break;
18499 case DW_AT_const_value:
18500 has_const_value = 1;
18501 break;
18502 case DW_AT_calling_convention:
18503 /* DWARF doesn't provide a way to identify a program's source-level
18504 entry point. DW_AT_calling_convention attributes are only meant
18505 to describe functions' calling conventions.
18506
18507 However, because it's a necessary piece of information in
18508 Fortran, and before DWARF 4 DW_CC_program was the only
18509 piece of debugging information whose definition refers to
18510 a 'main program' at all, several compilers marked Fortran
18511 main programs with DW_CC_program --- even when those
18512 functions use the standard calling conventions.
18513
18514 Although DWARF now specifies a way to provide this
18515 information, we support this practice for backward
18516 compatibility. */
18517 if (DW_UNSND (&attr) == DW_CC_program
18518 && cu->language == language_fortran)
18519 main_subprogram = 1;
18520 break;
18521 case DW_AT_inline:
18522 if (DW_UNSND (&attr) == DW_INL_inlined
18523 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18524 may_be_inlined = 1;
18525 break;
18526
18527 case DW_AT_import:
18528 if (tag == DW_TAG_imported_unit)
18529 {
18530 d.sect_off = attr.get_ref_die_offset ();
18531 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18532 || cu->per_cu->is_dwz);
18533 }
18534 break;
18535
18536 case DW_AT_main_subprogram:
18537 main_subprogram = DW_UNSND (&attr);
18538 break;
18539
18540 case DW_AT_ranges:
18541 {
18542 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18543 but that requires a full DIE, so instead we just
18544 reimplement it. */
18545 int need_ranges_base = tag != DW_TAG_compile_unit;
18546 unsigned int ranges_offset = (DW_UNSND (&attr)
18547 + (need_ranges_base
18548 ? cu->ranges_base
18549 : 0));
18550
18551 /* Value of the DW_AT_ranges attribute is the offset in the
18552 .debug_ranges section. */
18553 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18554 nullptr))
18555 has_pc_info = 1;
18556 }
18557 break;
18558
18559 default:
18560 break;
18561 }
18562 }
18563
18564 /* For Ada, if both the name and the linkage name appear, we prefer
18565 the latter. This lets "catch exception" work better, regardless
18566 of the order in which the name and linkage name were emitted.
18567 Really, though, this is just a workaround for the fact that gdb
18568 doesn't store both the name and the linkage name. */
18569 if (cu->language == language_ada && linkage_name != nullptr)
18570 name = linkage_name;
18571
18572 if (high_pc_relative)
18573 highpc += lowpc;
18574
18575 if (has_low_pc_attr && has_high_pc_attr)
18576 {
18577 /* When using the GNU linker, .gnu.linkonce. sections are used to
18578 eliminate duplicate copies of functions and vtables and such.
18579 The linker will arbitrarily choose one and discard the others.
18580 The AT_*_pc values for such functions refer to local labels in
18581 these sections. If the section from that file was discarded, the
18582 labels are not in the output, so the relocs get a value of 0.
18583 If this is a discarded function, mark the pc bounds as invalid,
18584 so that GDB will ignore it. */
18585 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
18586 {
18587 struct objfile *objfile = dwarf2_per_objfile->objfile;
18588 struct gdbarch *gdbarch = objfile->arch ();
18589
18590 complaint (_("DW_AT_low_pc %s is zero "
18591 "for DIE at %s [in module %s]"),
18592 paddress (gdbarch, lowpc),
18593 sect_offset_str (sect_off),
18594 objfile_name (objfile));
18595 }
18596 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18597 else if (lowpc >= highpc)
18598 {
18599 struct objfile *objfile = dwarf2_per_objfile->objfile;
18600 struct gdbarch *gdbarch = objfile->arch ();
18601
18602 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18603 "for DIE at %s [in module %s]"),
18604 paddress (gdbarch, lowpc),
18605 paddress (gdbarch, highpc),
18606 sect_offset_str (sect_off),
18607 objfile_name (objfile));
18608 }
18609 else
18610 has_pc_info = 1;
18611 }
18612
18613 return info_ptr;
18614 }
18615
18616 /* Find a cached partial DIE at OFFSET in CU. */
18617
18618 struct partial_die_info *
18619 dwarf2_cu::find_partial_die (sect_offset sect_off)
18620 {
18621 struct partial_die_info *lookup_die = NULL;
18622 struct partial_die_info part_die (sect_off);
18623
18624 lookup_die = ((struct partial_die_info *)
18625 htab_find_with_hash (partial_dies, &part_die,
18626 to_underlying (sect_off)));
18627
18628 return lookup_die;
18629 }
18630
18631 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18632 except in the case of .debug_types DIEs which do not reference
18633 outside their CU (they do however referencing other types via
18634 DW_FORM_ref_sig8). */
18635
18636 static const struct cu_partial_die_info
18637 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18638 {
18639 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18640 struct objfile *objfile = dwarf2_per_objfile->objfile;
18641 struct dwarf2_per_cu_data *per_cu = NULL;
18642 struct partial_die_info *pd = NULL;
18643
18644 if (offset_in_dwz == cu->per_cu->is_dwz
18645 && cu->header.offset_in_cu_p (sect_off))
18646 {
18647 pd = cu->find_partial_die (sect_off);
18648 if (pd != NULL)
18649 return { cu, pd };
18650 /* We missed recording what we needed.
18651 Load all dies and try again. */
18652 per_cu = cu->per_cu;
18653 }
18654 else
18655 {
18656 /* TUs don't reference other CUs/TUs (except via type signatures). */
18657 if (cu->per_cu->is_debug_types)
18658 {
18659 error (_("Dwarf Error: Type Unit at offset %s contains"
18660 " external reference to offset %s [in module %s].\n"),
18661 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18662 bfd_get_filename (objfile->obfd));
18663 }
18664 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18665 dwarf2_per_objfile);
18666
18667 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18668 load_partial_comp_unit (per_cu);
18669
18670 per_cu->cu->last_used = 0;
18671 pd = per_cu->cu->find_partial_die (sect_off);
18672 }
18673
18674 /* If we didn't find it, and not all dies have been loaded,
18675 load them all and try again. */
18676
18677 if (pd == NULL && per_cu->load_all_dies == 0)
18678 {
18679 per_cu->load_all_dies = 1;
18680
18681 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18682 THIS_CU->cu may already be in use. So we can't just free it and
18683 replace its DIEs with the ones we read in. Instead, we leave those
18684 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18685 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18686 set. */
18687 load_partial_comp_unit (per_cu);
18688
18689 pd = per_cu->cu->find_partial_die (sect_off);
18690 }
18691
18692 if (pd == NULL)
18693 internal_error (__FILE__, __LINE__,
18694 _("could not find partial DIE %s "
18695 "in cache [from module %s]\n"),
18696 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18697 return { per_cu->cu, pd };
18698 }
18699
18700 /* See if we can figure out if the class lives in a namespace. We do
18701 this by looking for a member function; its demangled name will
18702 contain namespace info, if there is any. */
18703
18704 static void
18705 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18706 struct dwarf2_cu *cu)
18707 {
18708 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18709 what template types look like, because the demangler
18710 frequently doesn't give the same name as the debug info. We
18711 could fix this by only using the demangled name to get the
18712 prefix (but see comment in read_structure_type). */
18713
18714 struct partial_die_info *real_pdi;
18715 struct partial_die_info *child_pdi;
18716
18717 /* If this DIE (this DIE's specification, if any) has a parent, then
18718 we should not do this. We'll prepend the parent's fully qualified
18719 name when we create the partial symbol. */
18720
18721 real_pdi = struct_pdi;
18722 while (real_pdi->has_specification)
18723 {
18724 auto res = find_partial_die (real_pdi->spec_offset,
18725 real_pdi->spec_is_dwz, cu);
18726 real_pdi = res.pdi;
18727 cu = res.cu;
18728 }
18729
18730 if (real_pdi->die_parent != NULL)
18731 return;
18732
18733 for (child_pdi = struct_pdi->die_child;
18734 child_pdi != NULL;
18735 child_pdi = child_pdi->die_sibling)
18736 {
18737 if (child_pdi->tag == DW_TAG_subprogram
18738 && child_pdi->linkage_name != NULL)
18739 {
18740 gdb::unique_xmalloc_ptr<char> actual_class_name
18741 (language_class_name_from_physname (cu->language_defn,
18742 child_pdi->linkage_name));
18743 if (actual_class_name != NULL)
18744 {
18745 struct objfile *objfile = cu->per_objfile->objfile;
18746 struct_pdi->name = objfile->intern (actual_class_name.get ());
18747 }
18748 break;
18749 }
18750 }
18751 }
18752
18753 /* Return true if a DIE with TAG may have the DW_AT_const_value
18754 attribute. */
18755
18756 static bool
18757 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18758 {
18759 switch (tag)
18760 {
18761 case DW_TAG_constant:
18762 case DW_TAG_enumerator:
18763 case DW_TAG_formal_parameter:
18764 case DW_TAG_template_value_param:
18765 case DW_TAG_variable:
18766 return true;
18767 }
18768
18769 return false;
18770 }
18771
18772 void
18773 partial_die_info::fixup (struct dwarf2_cu *cu)
18774 {
18775 /* Once we've fixed up a die, there's no point in doing so again.
18776 This also avoids a memory leak if we were to call
18777 guess_partial_die_structure_name multiple times. */
18778 if (fixup_called)
18779 return;
18780
18781 /* If we found a reference attribute and the DIE has no name, try
18782 to find a name in the referred to DIE. */
18783
18784 if (name == NULL && has_specification)
18785 {
18786 struct partial_die_info *spec_die;
18787
18788 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18789 spec_die = res.pdi;
18790 cu = res.cu;
18791
18792 spec_die->fixup (cu);
18793
18794 if (spec_die->name)
18795 {
18796 name = spec_die->name;
18797
18798 /* Copy DW_AT_external attribute if it is set. */
18799 if (spec_die->is_external)
18800 is_external = spec_die->is_external;
18801 }
18802 }
18803
18804 if (!has_const_value && has_specification
18805 && can_have_DW_AT_const_value_p (tag))
18806 {
18807 struct partial_die_info *spec_die;
18808
18809 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18810 spec_die = res.pdi;
18811 cu = res.cu;
18812
18813 spec_die->fixup (cu);
18814
18815 if (spec_die->has_const_value)
18816 {
18817 /* Copy DW_AT_const_value attribute if it is set. */
18818 has_const_value = spec_die->has_const_value;
18819 }
18820 }
18821
18822 /* Set default names for some unnamed DIEs. */
18823
18824 if (name == NULL && tag == DW_TAG_namespace)
18825 name = CP_ANONYMOUS_NAMESPACE_STR;
18826
18827 /* If there is no parent die to provide a namespace, and there are
18828 children, see if we can determine the namespace from their linkage
18829 name. */
18830 if (cu->language == language_cplus
18831 && !cu->per_objfile->per_bfd->types.empty ()
18832 && die_parent == NULL
18833 && has_children
18834 && (tag == DW_TAG_class_type
18835 || tag == DW_TAG_structure_type
18836 || tag == DW_TAG_union_type))
18837 guess_partial_die_structure_name (this, cu);
18838
18839 /* GCC might emit a nameless struct or union that has a linkage
18840 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18841 if (name == NULL
18842 && (tag == DW_TAG_class_type
18843 || tag == DW_TAG_interface_type
18844 || tag == DW_TAG_structure_type
18845 || tag == DW_TAG_union_type)
18846 && linkage_name != NULL)
18847 {
18848 gdb::unique_xmalloc_ptr<char> demangled
18849 (gdb_demangle (linkage_name, DMGL_TYPES));
18850 if (demangled != nullptr)
18851 {
18852 const char *base;
18853
18854 /* Strip any leading namespaces/classes, keep only the base name.
18855 DW_AT_name for named DIEs does not contain the prefixes. */
18856 base = strrchr (demangled.get (), ':');
18857 if (base && base > demangled.get () && base[-1] == ':')
18858 base++;
18859 else
18860 base = demangled.get ();
18861
18862 struct objfile *objfile = cu->per_objfile->objfile;
18863 name = objfile->intern (base);
18864 }
18865 }
18866
18867 fixup_called = 1;
18868 }
18869
18870 /* Read the .debug_loclists header contents from the given SECTION in the
18871 HEADER. */
18872 static void
18873 read_loclist_header (struct loclist_header *header,
18874 struct dwarf2_section_info *section)
18875 {
18876 unsigned int bytes_read;
18877 bfd *abfd = section->get_bfd_owner ();
18878 const gdb_byte *info_ptr = section->buffer;
18879 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18880 info_ptr += bytes_read;
18881 header->version = read_2_bytes (abfd, info_ptr);
18882 info_ptr += 2;
18883 header->addr_size = read_1_byte (abfd, info_ptr);
18884 info_ptr += 1;
18885 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18886 info_ptr += 1;
18887 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18888 }
18889
18890 /* Return the DW_AT_loclists_base value for the CU. */
18891 static ULONGEST
18892 lookup_loclist_base (struct dwarf2_cu *cu)
18893 {
18894 /* For the .dwo unit, the loclist_base points to the first offset following
18895 the header. The header consists of the following entities-
18896 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18897 bit format)
18898 2. version (2 bytes)
18899 3. address size (1 byte)
18900 4. segment selector size (1 byte)
18901 5. offset entry count (4 bytes)
18902 These sizes are derived as per the DWARFv5 standard. */
18903 if (cu->dwo_unit != nullptr)
18904 {
18905 if (cu->header.initial_length_size == 4)
18906 return LOCLIST_HEADER_SIZE32;
18907 return LOCLIST_HEADER_SIZE64;
18908 }
18909 return cu->loclist_base;
18910 }
18911
18912 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18913 array of offsets in the .debug_loclists section. */
18914 static CORE_ADDR
18915 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18916 {
18917 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18918 struct objfile *objfile = dwarf2_per_objfile->objfile;
18919 bfd *abfd = objfile->obfd;
18920 ULONGEST loclist_base = lookup_loclist_base (cu);
18921 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18922
18923 section->read (objfile);
18924 if (section->buffer == NULL)
18925 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18926 "section [in module %s]"), objfile_name (objfile));
18927 struct loclist_header header;
18928 read_loclist_header (&header, section);
18929 if (loclist_index >= header.offset_entry_count)
18930 complaint (_("DW_FORM_loclistx pointing outside of "
18931 ".debug_loclists offset array [in module %s]"),
18932 objfile_name (objfile));
18933 if (loclist_base + loclist_index * cu->header.offset_size
18934 >= section->size)
18935 complaint (_("DW_FORM_loclistx pointing outside of "
18936 ".debug_loclists section [in module %s]"),
18937 objfile_name (objfile));
18938 const gdb_byte *info_ptr
18939 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18940
18941 if (cu->header.offset_size == 4)
18942 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18943 else
18944 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18945 }
18946
18947 /* Process the attributes that had to be skipped in the first round. These
18948 attributes are the ones that need str_offsets_base or addr_base attributes.
18949 They could not have been processed in the first round, because at the time
18950 the values of str_offsets_base or addr_base may not have been known. */
18951 static void
18952 read_attribute_reprocess (const struct die_reader_specs *reader,
18953 struct attribute *attr)
18954 {
18955 struct dwarf2_cu *cu = reader->cu;
18956 switch (attr->form)
18957 {
18958 case DW_FORM_addrx:
18959 case DW_FORM_GNU_addr_index:
18960 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18961 break;
18962 case DW_FORM_loclistx:
18963 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18964 break;
18965 case DW_FORM_strx:
18966 case DW_FORM_strx1:
18967 case DW_FORM_strx2:
18968 case DW_FORM_strx3:
18969 case DW_FORM_strx4:
18970 case DW_FORM_GNU_str_index:
18971 {
18972 unsigned int str_index = DW_UNSND (attr);
18973 if (reader->dwo_file != NULL)
18974 {
18975 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18976 DW_STRING_IS_CANONICAL (attr) = 0;
18977 }
18978 else
18979 {
18980 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18981 DW_STRING_IS_CANONICAL (attr) = 0;
18982 }
18983 break;
18984 }
18985 default:
18986 gdb_assert_not_reached (_("Unexpected DWARF form."));
18987 }
18988 }
18989
18990 /* Read an attribute value described by an attribute form. */
18991
18992 static const gdb_byte *
18993 read_attribute_value (const struct die_reader_specs *reader,
18994 struct attribute *attr, unsigned form,
18995 LONGEST implicit_const, const gdb_byte *info_ptr,
18996 bool *need_reprocess)
18997 {
18998 struct dwarf2_cu *cu = reader->cu;
18999 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19000 struct objfile *objfile = dwarf2_per_objfile->objfile;
19001 bfd *abfd = reader->abfd;
19002 struct comp_unit_head *cu_header = &cu->header;
19003 unsigned int bytes_read;
19004 struct dwarf_block *blk;
19005 *need_reprocess = false;
19006
19007 attr->form = (enum dwarf_form) form;
19008 switch (form)
19009 {
19010 case DW_FORM_ref_addr:
19011 if (cu->header.version == 2)
19012 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19013 &bytes_read);
19014 else
19015 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19016 &bytes_read);
19017 info_ptr += bytes_read;
19018 break;
19019 case DW_FORM_GNU_ref_alt:
19020 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19021 info_ptr += bytes_read;
19022 break;
19023 case DW_FORM_addr:
19024 {
19025 struct gdbarch *gdbarch = objfile->arch ();
19026 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19027 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19028 info_ptr += bytes_read;
19029 }
19030 break;
19031 case DW_FORM_block2:
19032 blk = dwarf_alloc_block (cu);
19033 blk->size = read_2_bytes (abfd, info_ptr);
19034 info_ptr += 2;
19035 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19036 info_ptr += blk->size;
19037 DW_BLOCK (attr) = blk;
19038 break;
19039 case DW_FORM_block4:
19040 blk = dwarf_alloc_block (cu);
19041 blk->size = read_4_bytes (abfd, info_ptr);
19042 info_ptr += 4;
19043 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19044 info_ptr += blk->size;
19045 DW_BLOCK (attr) = blk;
19046 break;
19047 case DW_FORM_data2:
19048 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19049 info_ptr += 2;
19050 break;
19051 case DW_FORM_data4:
19052 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19053 info_ptr += 4;
19054 break;
19055 case DW_FORM_data8:
19056 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19057 info_ptr += 8;
19058 break;
19059 case DW_FORM_data16:
19060 blk = dwarf_alloc_block (cu);
19061 blk->size = 16;
19062 blk->data = read_n_bytes (abfd, info_ptr, 16);
19063 info_ptr += 16;
19064 DW_BLOCK (attr) = blk;
19065 break;
19066 case DW_FORM_sec_offset:
19067 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19068 info_ptr += bytes_read;
19069 break;
19070 case DW_FORM_loclistx:
19071 {
19072 *need_reprocess = true;
19073 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19074 info_ptr += bytes_read;
19075 }
19076 break;
19077 case DW_FORM_string:
19078 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19079 DW_STRING_IS_CANONICAL (attr) = 0;
19080 info_ptr += bytes_read;
19081 break;
19082 case DW_FORM_strp:
19083 if (!cu->per_cu->is_dwz)
19084 {
19085 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19086 abfd, info_ptr, cu_header,
19087 &bytes_read);
19088 DW_STRING_IS_CANONICAL (attr) = 0;
19089 info_ptr += bytes_read;
19090 break;
19091 }
19092 /* FALLTHROUGH */
19093 case DW_FORM_line_strp:
19094 if (!cu->per_cu->is_dwz)
19095 {
19096 DW_STRING (attr)
19097 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19098 &bytes_read);
19099 DW_STRING_IS_CANONICAL (attr) = 0;
19100 info_ptr += bytes_read;
19101 break;
19102 }
19103 /* FALLTHROUGH */
19104 case DW_FORM_GNU_strp_alt:
19105 {
19106 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19107 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19108 &bytes_read);
19109
19110 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
19111 DW_STRING_IS_CANONICAL (attr) = 0;
19112 info_ptr += bytes_read;
19113 }
19114 break;
19115 case DW_FORM_exprloc:
19116 case DW_FORM_block:
19117 blk = dwarf_alloc_block (cu);
19118 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19119 info_ptr += bytes_read;
19120 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19121 info_ptr += blk->size;
19122 DW_BLOCK (attr) = blk;
19123 break;
19124 case DW_FORM_block1:
19125 blk = dwarf_alloc_block (cu);
19126 blk->size = read_1_byte (abfd, info_ptr);
19127 info_ptr += 1;
19128 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19129 info_ptr += blk->size;
19130 DW_BLOCK (attr) = blk;
19131 break;
19132 case DW_FORM_data1:
19133 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19134 info_ptr += 1;
19135 break;
19136 case DW_FORM_flag:
19137 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19138 info_ptr += 1;
19139 break;
19140 case DW_FORM_flag_present:
19141 DW_UNSND (attr) = 1;
19142 break;
19143 case DW_FORM_sdata:
19144 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19145 info_ptr += bytes_read;
19146 break;
19147 case DW_FORM_udata:
19148 case DW_FORM_rnglistx:
19149 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19150 info_ptr += bytes_read;
19151 break;
19152 case DW_FORM_ref1:
19153 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19154 + read_1_byte (abfd, info_ptr));
19155 info_ptr += 1;
19156 break;
19157 case DW_FORM_ref2:
19158 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19159 + read_2_bytes (abfd, info_ptr));
19160 info_ptr += 2;
19161 break;
19162 case DW_FORM_ref4:
19163 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19164 + read_4_bytes (abfd, info_ptr));
19165 info_ptr += 4;
19166 break;
19167 case DW_FORM_ref8:
19168 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19169 + read_8_bytes (abfd, info_ptr));
19170 info_ptr += 8;
19171 break;
19172 case DW_FORM_ref_sig8:
19173 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19174 info_ptr += 8;
19175 break;
19176 case DW_FORM_ref_udata:
19177 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19178 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19179 info_ptr += bytes_read;
19180 break;
19181 case DW_FORM_indirect:
19182 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19183 info_ptr += bytes_read;
19184 if (form == DW_FORM_implicit_const)
19185 {
19186 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19187 info_ptr += bytes_read;
19188 }
19189 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19190 info_ptr, need_reprocess);
19191 break;
19192 case DW_FORM_implicit_const:
19193 DW_SND (attr) = implicit_const;
19194 break;
19195 case DW_FORM_addrx:
19196 case DW_FORM_GNU_addr_index:
19197 *need_reprocess = true;
19198 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19199 info_ptr += bytes_read;
19200 break;
19201 case DW_FORM_strx:
19202 case DW_FORM_strx1:
19203 case DW_FORM_strx2:
19204 case DW_FORM_strx3:
19205 case DW_FORM_strx4:
19206 case DW_FORM_GNU_str_index:
19207 {
19208 ULONGEST str_index;
19209 if (form == DW_FORM_strx1)
19210 {
19211 str_index = read_1_byte (abfd, info_ptr);
19212 info_ptr += 1;
19213 }
19214 else if (form == DW_FORM_strx2)
19215 {
19216 str_index = read_2_bytes (abfd, info_ptr);
19217 info_ptr += 2;
19218 }
19219 else if (form == DW_FORM_strx3)
19220 {
19221 str_index = read_3_bytes (abfd, info_ptr);
19222 info_ptr += 3;
19223 }
19224 else if (form == DW_FORM_strx4)
19225 {
19226 str_index = read_4_bytes (abfd, info_ptr);
19227 info_ptr += 4;
19228 }
19229 else
19230 {
19231 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19232 info_ptr += bytes_read;
19233 }
19234 *need_reprocess = true;
19235 DW_UNSND (attr) = str_index;
19236 }
19237 break;
19238 default:
19239 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19240 dwarf_form_name (form),
19241 bfd_get_filename (abfd));
19242 }
19243
19244 /* Super hack. */
19245 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19246 attr->form = DW_FORM_GNU_ref_alt;
19247
19248 /* We have seen instances where the compiler tried to emit a byte
19249 size attribute of -1 which ended up being encoded as an unsigned
19250 0xffffffff. Although 0xffffffff is technically a valid size value,
19251 an object of this size seems pretty unlikely so we can relatively
19252 safely treat these cases as if the size attribute was invalid and
19253 treat them as zero by default. */
19254 if (attr->name == DW_AT_byte_size
19255 && form == DW_FORM_data4
19256 && DW_UNSND (attr) >= 0xffffffff)
19257 {
19258 complaint
19259 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19260 hex_string (DW_UNSND (attr)));
19261 DW_UNSND (attr) = 0;
19262 }
19263
19264 return info_ptr;
19265 }
19266
19267 /* Read an attribute described by an abbreviated attribute. */
19268
19269 static const gdb_byte *
19270 read_attribute (const struct die_reader_specs *reader,
19271 struct attribute *attr, struct attr_abbrev *abbrev,
19272 const gdb_byte *info_ptr, bool *need_reprocess)
19273 {
19274 attr->name = abbrev->name;
19275 return read_attribute_value (reader, attr, abbrev->form,
19276 abbrev->implicit_const, info_ptr,
19277 need_reprocess);
19278 }
19279
19280 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19281
19282 static const char *
19283 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19284 LONGEST str_offset)
19285 {
19286 return dwarf2_per_objfile->per_bfd->str.read_string
19287 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
19288 }
19289
19290 /* Return pointer to string at .debug_str offset as read from BUF.
19291 BUF is assumed to be in a compilation unit described by CU_HEADER.
19292 Return *BYTES_READ_PTR count of bytes read from BUF. */
19293
19294 static const char *
19295 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19296 const gdb_byte *buf,
19297 const struct comp_unit_head *cu_header,
19298 unsigned int *bytes_read_ptr)
19299 {
19300 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19301
19302 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
19303 }
19304
19305 /* See read.h. */
19306
19307 const char *
19308 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19309 const struct comp_unit_head *cu_header,
19310 unsigned int *bytes_read_ptr)
19311 {
19312 bfd *abfd = objfile->obfd;
19313 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19314
19315 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19316 }
19317
19318 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19319 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19320 ADDR_SIZE is the size of addresses from the CU header. */
19321
19322 static CORE_ADDR
19323 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19324 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19325 int addr_size)
19326 {
19327 struct objfile *objfile = dwarf2_per_objfile->objfile;
19328 bfd *abfd = objfile->obfd;
19329 const gdb_byte *info_ptr;
19330 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19331
19332 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19333 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
19334 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19335 objfile_name (objfile));
19336 if (addr_base_or_zero + addr_index * addr_size
19337 >= dwarf2_per_objfile->per_bfd->addr.size)
19338 error (_("DW_FORM_addr_index pointing outside of "
19339 ".debug_addr section [in module %s]"),
19340 objfile_name (objfile));
19341 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
19342 + addr_base_or_zero + addr_index * addr_size);
19343 if (addr_size == 4)
19344 return bfd_get_32 (abfd, info_ptr);
19345 else
19346 return bfd_get_64 (abfd, info_ptr);
19347 }
19348
19349 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19350
19351 static CORE_ADDR
19352 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19353 {
19354 return read_addr_index_1 (cu->per_objfile, addr_index,
19355 cu->addr_base, cu->header.addr_size);
19356 }
19357
19358 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19359
19360 static CORE_ADDR
19361 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19362 unsigned int *bytes_read)
19363 {
19364 bfd *abfd = cu->per_objfile->objfile->obfd;
19365 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19366
19367 return read_addr_index (cu, addr_index);
19368 }
19369
19370 /* See read.h. */
19371
19372 CORE_ADDR
19373 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
19374 {
19375 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19376 struct dwarf2_cu *cu = per_cu->cu;
19377 gdb::optional<ULONGEST> addr_base;
19378 int addr_size;
19379
19380 /* We need addr_base and addr_size.
19381 If we don't have PER_CU->cu, we have to get it.
19382 Nasty, but the alternative is storing the needed info in PER_CU,
19383 which at this point doesn't seem justified: it's not clear how frequently
19384 it would get used and it would increase the size of every PER_CU.
19385 Entry points like dwarf2_per_cu_addr_size do a similar thing
19386 so we're not in uncharted territory here.
19387 Alas we need to be a bit more complicated as addr_base is contained
19388 in the DIE.
19389
19390 We don't need to read the entire CU(/TU).
19391 We just need the header and top level die.
19392
19393 IWBN to use the aging mechanism to let us lazily later discard the CU.
19394 For now we skip this optimization. */
19395
19396 if (cu != NULL)
19397 {
19398 addr_base = cu->addr_base;
19399 addr_size = cu->header.addr_size;
19400 }
19401 else
19402 {
19403 cutu_reader reader (per_cu, NULL, 0, false);
19404 addr_base = reader.cu->addr_base;
19405 addr_size = reader.cu->header.addr_size;
19406 }
19407
19408 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19409 addr_size);
19410 }
19411
19412 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19413 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19414 DWO file. */
19415
19416 static const char *
19417 read_str_index (struct dwarf2_cu *cu,
19418 struct dwarf2_section_info *str_section,
19419 struct dwarf2_section_info *str_offsets_section,
19420 ULONGEST str_offsets_base, ULONGEST str_index)
19421 {
19422 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19423 struct objfile *objfile = dwarf2_per_objfile->objfile;
19424 const char *objf_name = objfile_name (objfile);
19425 bfd *abfd = objfile->obfd;
19426 const gdb_byte *info_ptr;
19427 ULONGEST str_offset;
19428 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19429
19430 str_section->read (objfile);
19431 str_offsets_section->read (objfile);
19432 if (str_section->buffer == NULL)
19433 error (_("%s used without %s section"
19434 " in CU at offset %s [in module %s]"),
19435 form_name, str_section->get_name (),
19436 sect_offset_str (cu->header.sect_off), objf_name);
19437 if (str_offsets_section->buffer == NULL)
19438 error (_("%s used without %s section"
19439 " in CU at offset %s [in module %s]"),
19440 form_name, str_section->get_name (),
19441 sect_offset_str (cu->header.sect_off), objf_name);
19442 info_ptr = (str_offsets_section->buffer
19443 + str_offsets_base
19444 + str_index * cu->header.offset_size);
19445 if (cu->header.offset_size == 4)
19446 str_offset = bfd_get_32 (abfd, info_ptr);
19447 else
19448 str_offset = bfd_get_64 (abfd, info_ptr);
19449 if (str_offset >= str_section->size)
19450 error (_("Offset from %s pointing outside of"
19451 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19452 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19453 return (const char *) (str_section->buffer + str_offset);
19454 }
19455
19456 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19457
19458 static const char *
19459 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19460 {
19461 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19462 ? reader->cu->header.addr_size : 0;
19463 return read_str_index (reader->cu,
19464 &reader->dwo_file->sections.str,
19465 &reader->dwo_file->sections.str_offsets,
19466 str_offsets_base, str_index);
19467 }
19468
19469 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19470
19471 static const char *
19472 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19473 {
19474 struct objfile *objfile = cu->per_objfile->objfile;
19475 const char *objf_name = objfile_name (objfile);
19476 static const char form_name[] = "DW_FORM_GNU_str_index";
19477 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19478
19479 if (!cu->str_offsets_base.has_value ())
19480 error (_("%s used in Fission stub without %s"
19481 " in CU at offset 0x%lx [in module %s]"),
19482 form_name, str_offsets_attr_name,
19483 (long) cu->header.offset_size, objf_name);
19484
19485 return read_str_index (cu,
19486 &cu->per_objfile->per_bfd->str,
19487 &cu->per_objfile->per_bfd->str_offsets,
19488 *cu->str_offsets_base, str_index);
19489 }
19490
19491 /* Return the length of an LEB128 number in BUF. */
19492
19493 static int
19494 leb128_size (const gdb_byte *buf)
19495 {
19496 const gdb_byte *begin = buf;
19497 gdb_byte byte;
19498
19499 while (1)
19500 {
19501 byte = *buf++;
19502 if ((byte & 128) == 0)
19503 return buf - begin;
19504 }
19505 }
19506
19507 static void
19508 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19509 {
19510 switch (lang)
19511 {
19512 case DW_LANG_C89:
19513 case DW_LANG_C99:
19514 case DW_LANG_C11:
19515 case DW_LANG_C:
19516 case DW_LANG_UPC:
19517 cu->language = language_c;
19518 break;
19519 case DW_LANG_Java:
19520 case DW_LANG_C_plus_plus:
19521 case DW_LANG_C_plus_plus_11:
19522 case DW_LANG_C_plus_plus_14:
19523 cu->language = language_cplus;
19524 break;
19525 case DW_LANG_D:
19526 cu->language = language_d;
19527 break;
19528 case DW_LANG_Fortran77:
19529 case DW_LANG_Fortran90:
19530 case DW_LANG_Fortran95:
19531 case DW_LANG_Fortran03:
19532 case DW_LANG_Fortran08:
19533 cu->language = language_fortran;
19534 break;
19535 case DW_LANG_Go:
19536 cu->language = language_go;
19537 break;
19538 case DW_LANG_Mips_Assembler:
19539 cu->language = language_asm;
19540 break;
19541 case DW_LANG_Ada83:
19542 case DW_LANG_Ada95:
19543 cu->language = language_ada;
19544 break;
19545 case DW_LANG_Modula2:
19546 cu->language = language_m2;
19547 break;
19548 case DW_LANG_Pascal83:
19549 cu->language = language_pascal;
19550 break;
19551 case DW_LANG_ObjC:
19552 cu->language = language_objc;
19553 break;
19554 case DW_LANG_Rust:
19555 case DW_LANG_Rust_old:
19556 cu->language = language_rust;
19557 break;
19558 case DW_LANG_Cobol74:
19559 case DW_LANG_Cobol85:
19560 default:
19561 cu->language = language_minimal;
19562 break;
19563 }
19564 cu->language_defn = language_def (cu->language);
19565 }
19566
19567 /* Return the named attribute or NULL if not there. */
19568
19569 static struct attribute *
19570 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19571 {
19572 for (;;)
19573 {
19574 unsigned int i;
19575 struct attribute *spec = NULL;
19576
19577 for (i = 0; i < die->num_attrs; ++i)
19578 {
19579 if (die->attrs[i].name == name)
19580 return &die->attrs[i];
19581 if (die->attrs[i].name == DW_AT_specification
19582 || die->attrs[i].name == DW_AT_abstract_origin)
19583 spec = &die->attrs[i];
19584 }
19585
19586 if (!spec)
19587 break;
19588
19589 die = follow_die_ref (die, spec, &cu);
19590 }
19591
19592 return NULL;
19593 }
19594
19595 /* Return the string associated with a string-typed attribute, or NULL if it
19596 is either not found or is of an incorrect type. */
19597
19598 static const char *
19599 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19600 {
19601 struct attribute *attr;
19602 const char *str = NULL;
19603
19604 attr = dwarf2_attr (die, name, cu);
19605
19606 if (attr != NULL)
19607 {
19608 str = attr->value_as_string ();
19609 if (str == nullptr)
19610 complaint (_("string type expected for attribute %s for "
19611 "DIE at %s in module %s"),
19612 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19613 objfile_name (cu->per_objfile->objfile));
19614 }
19615
19616 return str;
19617 }
19618
19619 /* Return the dwo name or NULL if not present. If present, it is in either
19620 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19621 static const char *
19622 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19623 {
19624 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19625 if (dwo_name == nullptr)
19626 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19627 return dwo_name;
19628 }
19629
19630 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19631 and holds a non-zero value. This function should only be used for
19632 DW_FORM_flag or DW_FORM_flag_present attributes. */
19633
19634 static int
19635 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19636 {
19637 struct attribute *attr = dwarf2_attr (die, name, cu);
19638
19639 return (attr && DW_UNSND (attr));
19640 }
19641
19642 static int
19643 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19644 {
19645 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19646 which value is non-zero. However, we have to be careful with
19647 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19648 (via dwarf2_flag_true_p) follows this attribute. So we may
19649 end up accidently finding a declaration attribute that belongs
19650 to a different DIE referenced by the specification attribute,
19651 even though the given DIE does not have a declaration attribute. */
19652 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19653 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19654 }
19655
19656 /* Return the die giving the specification for DIE, if there is
19657 one. *SPEC_CU is the CU containing DIE on input, and the CU
19658 containing the return value on output. If there is no
19659 specification, but there is an abstract origin, that is
19660 returned. */
19661
19662 static struct die_info *
19663 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19664 {
19665 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19666 *spec_cu);
19667
19668 if (spec_attr == NULL)
19669 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19670
19671 if (spec_attr == NULL)
19672 return NULL;
19673 else
19674 return follow_die_ref (die, spec_attr, spec_cu);
19675 }
19676
19677 /* Stub for free_line_header to match void * callback types. */
19678
19679 static void
19680 free_line_header_voidp (void *arg)
19681 {
19682 struct line_header *lh = (struct line_header *) arg;
19683
19684 delete lh;
19685 }
19686
19687 /* A convenience function to find the proper .debug_line section for a CU. */
19688
19689 static struct dwarf2_section_info *
19690 get_debug_line_section (struct dwarf2_cu *cu)
19691 {
19692 struct dwarf2_section_info *section;
19693 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19694
19695 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19696 DWO file. */
19697 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19698 section = &cu->dwo_unit->dwo_file->sections.line;
19699 else if (cu->per_cu->is_dwz)
19700 {
19701 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19702
19703 section = &dwz->line;
19704 }
19705 else
19706 section = &dwarf2_per_objfile->per_bfd->line;
19707
19708 return section;
19709 }
19710
19711 /* Read the statement program header starting at OFFSET in
19712 .debug_line, or .debug_line.dwo. Return a pointer
19713 to a struct line_header, allocated using xmalloc.
19714 Returns NULL if there is a problem reading the header, e.g., if it
19715 has a version we don't understand.
19716
19717 NOTE: the strings in the include directory and file name tables of
19718 the returned object point into the dwarf line section buffer,
19719 and must not be freed. */
19720
19721 static line_header_up
19722 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19723 {
19724 struct dwarf2_section_info *section;
19725 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19726
19727 section = get_debug_line_section (cu);
19728 section->read (dwarf2_per_objfile->objfile);
19729 if (section->buffer == NULL)
19730 {
19731 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19732 complaint (_("missing .debug_line.dwo section"));
19733 else
19734 complaint (_("missing .debug_line section"));
19735 return 0;
19736 }
19737
19738 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19739 dwarf2_per_objfile, section,
19740 &cu->header);
19741 }
19742
19743 /* Subroutine of dwarf_decode_lines to simplify it.
19744 Return the file name of the psymtab for the given file_entry.
19745 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19746 If space for the result is malloc'd, *NAME_HOLDER will be set.
19747 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19748
19749 static const char *
19750 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19751 const dwarf2_psymtab *pst,
19752 const char *comp_dir,
19753 gdb::unique_xmalloc_ptr<char> *name_holder)
19754 {
19755 const char *include_name = fe.name;
19756 const char *include_name_to_compare = include_name;
19757 const char *pst_filename;
19758 int file_is_pst;
19759
19760 const char *dir_name = fe.include_dir (lh);
19761
19762 gdb::unique_xmalloc_ptr<char> hold_compare;
19763 if (!IS_ABSOLUTE_PATH (include_name)
19764 && (dir_name != NULL || comp_dir != NULL))
19765 {
19766 /* Avoid creating a duplicate psymtab for PST.
19767 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19768 Before we do the comparison, however, we need to account
19769 for DIR_NAME and COMP_DIR.
19770 First prepend dir_name (if non-NULL). If we still don't
19771 have an absolute path prepend comp_dir (if non-NULL).
19772 However, the directory we record in the include-file's
19773 psymtab does not contain COMP_DIR (to match the
19774 corresponding symtab(s)).
19775
19776 Example:
19777
19778 bash$ cd /tmp
19779 bash$ gcc -g ./hello.c
19780 include_name = "hello.c"
19781 dir_name = "."
19782 DW_AT_comp_dir = comp_dir = "/tmp"
19783 DW_AT_name = "./hello.c"
19784
19785 */
19786
19787 if (dir_name != NULL)
19788 {
19789 name_holder->reset (concat (dir_name, SLASH_STRING,
19790 include_name, (char *) NULL));
19791 include_name = name_holder->get ();
19792 include_name_to_compare = include_name;
19793 }
19794 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19795 {
19796 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19797 include_name, (char *) NULL));
19798 include_name_to_compare = hold_compare.get ();
19799 }
19800 }
19801
19802 pst_filename = pst->filename;
19803 gdb::unique_xmalloc_ptr<char> copied_name;
19804 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19805 {
19806 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19807 pst_filename, (char *) NULL));
19808 pst_filename = copied_name.get ();
19809 }
19810
19811 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19812
19813 if (file_is_pst)
19814 return NULL;
19815 return include_name;
19816 }
19817
19818 /* State machine to track the state of the line number program. */
19819
19820 class lnp_state_machine
19821 {
19822 public:
19823 /* Initialize a machine state for the start of a line number
19824 program. */
19825 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19826 bool record_lines_p);
19827
19828 file_entry *current_file ()
19829 {
19830 /* lh->file_names is 0-based, but the file name numbers in the
19831 statement program are 1-based. */
19832 return m_line_header->file_name_at (m_file);
19833 }
19834
19835 /* Record the line in the state machine. END_SEQUENCE is true if
19836 we're processing the end of a sequence. */
19837 void record_line (bool end_sequence);
19838
19839 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19840 nop-out rest of the lines in this sequence. */
19841 void check_line_address (struct dwarf2_cu *cu,
19842 const gdb_byte *line_ptr,
19843 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19844
19845 void handle_set_discriminator (unsigned int discriminator)
19846 {
19847 m_discriminator = discriminator;
19848 m_line_has_non_zero_discriminator |= discriminator != 0;
19849 }
19850
19851 /* Handle DW_LNE_set_address. */
19852 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19853 {
19854 m_op_index = 0;
19855 address += baseaddr;
19856 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19857 }
19858
19859 /* Handle DW_LNS_advance_pc. */
19860 void handle_advance_pc (CORE_ADDR adjust);
19861
19862 /* Handle a special opcode. */
19863 void handle_special_opcode (unsigned char op_code);
19864
19865 /* Handle DW_LNS_advance_line. */
19866 void handle_advance_line (int line_delta)
19867 {
19868 advance_line (line_delta);
19869 }
19870
19871 /* Handle DW_LNS_set_file. */
19872 void handle_set_file (file_name_index file);
19873
19874 /* Handle DW_LNS_negate_stmt. */
19875 void handle_negate_stmt ()
19876 {
19877 m_is_stmt = !m_is_stmt;
19878 }
19879
19880 /* Handle DW_LNS_const_add_pc. */
19881 void handle_const_add_pc ();
19882
19883 /* Handle DW_LNS_fixed_advance_pc. */
19884 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19885 {
19886 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19887 m_op_index = 0;
19888 }
19889
19890 /* Handle DW_LNS_copy. */
19891 void handle_copy ()
19892 {
19893 record_line (false);
19894 m_discriminator = 0;
19895 }
19896
19897 /* Handle DW_LNE_end_sequence. */
19898 void handle_end_sequence ()
19899 {
19900 m_currently_recording_lines = true;
19901 }
19902
19903 private:
19904 /* Advance the line by LINE_DELTA. */
19905 void advance_line (int line_delta)
19906 {
19907 m_line += line_delta;
19908
19909 if (line_delta != 0)
19910 m_line_has_non_zero_discriminator = m_discriminator != 0;
19911 }
19912
19913 struct dwarf2_cu *m_cu;
19914
19915 gdbarch *m_gdbarch;
19916
19917 /* True if we're recording lines.
19918 Otherwise we're building partial symtabs and are just interested in
19919 finding include files mentioned by the line number program. */
19920 bool m_record_lines_p;
19921
19922 /* The line number header. */
19923 line_header *m_line_header;
19924
19925 /* These are part of the standard DWARF line number state machine,
19926 and initialized according to the DWARF spec. */
19927
19928 unsigned char m_op_index = 0;
19929 /* The line table index of the current file. */
19930 file_name_index m_file = 1;
19931 unsigned int m_line = 1;
19932
19933 /* These are initialized in the constructor. */
19934
19935 CORE_ADDR m_address;
19936 bool m_is_stmt;
19937 unsigned int m_discriminator;
19938
19939 /* Additional bits of state we need to track. */
19940
19941 /* The last file that we called dwarf2_start_subfile for.
19942 This is only used for TLLs. */
19943 unsigned int m_last_file = 0;
19944 /* The last file a line number was recorded for. */
19945 struct subfile *m_last_subfile = NULL;
19946
19947 /* When true, record the lines we decode. */
19948 bool m_currently_recording_lines = false;
19949
19950 /* The last line number that was recorded, used to coalesce
19951 consecutive entries for the same line. This can happen, for
19952 example, when discriminators are present. PR 17276. */
19953 unsigned int m_last_line = 0;
19954 bool m_line_has_non_zero_discriminator = false;
19955 };
19956
19957 void
19958 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19959 {
19960 CORE_ADDR addr_adj = (((m_op_index + adjust)
19961 / m_line_header->maximum_ops_per_instruction)
19962 * m_line_header->minimum_instruction_length);
19963 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19964 m_op_index = ((m_op_index + adjust)
19965 % m_line_header->maximum_ops_per_instruction);
19966 }
19967
19968 void
19969 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19970 {
19971 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19972 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19973 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19974 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19975 / m_line_header->maximum_ops_per_instruction)
19976 * m_line_header->minimum_instruction_length);
19977 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19978 m_op_index = ((m_op_index + adj_opcode_d)
19979 % m_line_header->maximum_ops_per_instruction);
19980
19981 int line_delta = m_line_header->line_base + adj_opcode_r;
19982 advance_line (line_delta);
19983 record_line (false);
19984 m_discriminator = 0;
19985 }
19986
19987 void
19988 lnp_state_machine::handle_set_file (file_name_index file)
19989 {
19990 m_file = file;
19991
19992 const file_entry *fe = current_file ();
19993 if (fe == NULL)
19994 dwarf2_debug_line_missing_file_complaint ();
19995 else if (m_record_lines_p)
19996 {
19997 const char *dir = fe->include_dir (m_line_header);
19998
19999 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20000 m_line_has_non_zero_discriminator = m_discriminator != 0;
20001 dwarf2_start_subfile (m_cu, fe->name, dir);
20002 }
20003 }
20004
20005 void
20006 lnp_state_machine::handle_const_add_pc ()
20007 {
20008 CORE_ADDR adjust
20009 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20010
20011 CORE_ADDR addr_adj
20012 = (((m_op_index + adjust)
20013 / m_line_header->maximum_ops_per_instruction)
20014 * m_line_header->minimum_instruction_length);
20015
20016 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20017 m_op_index = ((m_op_index + adjust)
20018 % m_line_header->maximum_ops_per_instruction);
20019 }
20020
20021 /* Return non-zero if we should add LINE to the line number table.
20022 LINE is the line to add, LAST_LINE is the last line that was added,
20023 LAST_SUBFILE is the subfile for LAST_LINE.
20024 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20025 had a non-zero discriminator.
20026
20027 We have to be careful in the presence of discriminators.
20028 E.g., for this line:
20029
20030 for (i = 0; i < 100000; i++);
20031
20032 clang can emit four line number entries for that one line,
20033 each with a different discriminator.
20034 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20035
20036 However, we want gdb to coalesce all four entries into one.
20037 Otherwise the user could stepi into the middle of the line and
20038 gdb would get confused about whether the pc really was in the
20039 middle of the line.
20040
20041 Things are further complicated by the fact that two consecutive
20042 line number entries for the same line is a heuristic used by gcc
20043 to denote the end of the prologue. So we can't just discard duplicate
20044 entries, we have to be selective about it. The heuristic we use is
20045 that we only collapse consecutive entries for the same line if at least
20046 one of those entries has a non-zero discriminator. PR 17276.
20047
20048 Note: Addresses in the line number state machine can never go backwards
20049 within one sequence, thus this coalescing is ok. */
20050
20051 static int
20052 dwarf_record_line_p (struct dwarf2_cu *cu,
20053 unsigned int line, unsigned int last_line,
20054 int line_has_non_zero_discriminator,
20055 struct subfile *last_subfile)
20056 {
20057 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20058 return 1;
20059 if (line != last_line)
20060 return 1;
20061 /* Same line for the same file that we've seen already.
20062 As a last check, for pr 17276, only record the line if the line
20063 has never had a non-zero discriminator. */
20064 if (!line_has_non_zero_discriminator)
20065 return 1;
20066 return 0;
20067 }
20068
20069 /* Use the CU's builder to record line number LINE beginning at
20070 address ADDRESS in the line table of subfile SUBFILE. */
20071
20072 static void
20073 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20074 unsigned int line, CORE_ADDR address, bool is_stmt,
20075 struct dwarf2_cu *cu)
20076 {
20077 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20078
20079 if (dwarf_line_debug)
20080 {
20081 fprintf_unfiltered (gdb_stdlog,
20082 "Recording line %u, file %s, address %s\n",
20083 line, lbasename (subfile->name),
20084 paddress (gdbarch, address));
20085 }
20086
20087 if (cu != nullptr)
20088 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20089 }
20090
20091 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20092 Mark the end of a set of line number records.
20093 The arguments are the same as for dwarf_record_line_1.
20094 If SUBFILE is NULL the request is ignored. */
20095
20096 static void
20097 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20098 CORE_ADDR address, struct dwarf2_cu *cu)
20099 {
20100 if (subfile == NULL)
20101 return;
20102
20103 if (dwarf_line_debug)
20104 {
20105 fprintf_unfiltered (gdb_stdlog,
20106 "Finishing current line, file %s, address %s\n",
20107 lbasename (subfile->name),
20108 paddress (gdbarch, address));
20109 }
20110
20111 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20112 }
20113
20114 void
20115 lnp_state_machine::record_line (bool end_sequence)
20116 {
20117 if (dwarf_line_debug)
20118 {
20119 fprintf_unfiltered (gdb_stdlog,
20120 "Processing actual line %u: file %u,"
20121 " address %s, is_stmt %u, discrim %u%s\n",
20122 m_line, m_file,
20123 paddress (m_gdbarch, m_address),
20124 m_is_stmt, m_discriminator,
20125 (end_sequence ? "\t(end sequence)" : ""));
20126 }
20127
20128 file_entry *fe = current_file ();
20129
20130 if (fe == NULL)
20131 dwarf2_debug_line_missing_file_complaint ();
20132 /* For now we ignore lines not starting on an instruction boundary.
20133 But not when processing end_sequence for compatibility with the
20134 previous version of the code. */
20135 else if (m_op_index == 0 || end_sequence)
20136 {
20137 fe->included_p = 1;
20138 if (m_record_lines_p)
20139 {
20140 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20141 || end_sequence)
20142 {
20143 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20144 m_currently_recording_lines ? m_cu : nullptr);
20145 }
20146
20147 if (!end_sequence)
20148 {
20149 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20150
20151 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20152 m_line_has_non_zero_discriminator,
20153 m_last_subfile))
20154 {
20155 buildsym_compunit *builder = m_cu->get_builder ();
20156 dwarf_record_line_1 (m_gdbarch,
20157 builder->get_current_subfile (),
20158 m_line, m_address, is_stmt,
20159 m_currently_recording_lines ? m_cu : nullptr);
20160 }
20161 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20162 m_last_line = m_line;
20163 }
20164 }
20165 }
20166 }
20167
20168 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20169 line_header *lh, bool record_lines_p)
20170 {
20171 m_cu = cu;
20172 m_gdbarch = arch;
20173 m_record_lines_p = record_lines_p;
20174 m_line_header = lh;
20175
20176 m_currently_recording_lines = true;
20177
20178 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20179 was a line entry for it so that the backend has a chance to adjust it
20180 and also record it in case it needs it. This is currently used by MIPS
20181 code, cf. `mips_adjust_dwarf2_line'. */
20182 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20183 m_is_stmt = lh->default_is_stmt;
20184 m_discriminator = 0;
20185 }
20186
20187 void
20188 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20189 const gdb_byte *line_ptr,
20190 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20191 {
20192 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20193 the pc range of the CU. However, we restrict the test to only ADDRESS
20194 values of zero to preserve GDB's previous behaviour which is to handle
20195 the specific case of a function being GC'd by the linker. */
20196
20197 if (address == 0 && address < unrelocated_lowpc)
20198 {
20199 /* This line table is for a function which has been
20200 GCd by the linker. Ignore it. PR gdb/12528 */
20201
20202 struct objfile *objfile = cu->per_objfile->objfile;
20203 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20204
20205 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20206 line_offset, objfile_name (objfile));
20207 m_currently_recording_lines = false;
20208 /* Note: m_currently_recording_lines is left as false until we see
20209 DW_LNE_end_sequence. */
20210 }
20211 }
20212
20213 /* Subroutine of dwarf_decode_lines to simplify it.
20214 Process the line number information in LH.
20215 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20216 program in order to set included_p for every referenced header. */
20217
20218 static void
20219 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20220 const int decode_for_pst_p, CORE_ADDR lowpc)
20221 {
20222 const gdb_byte *line_ptr, *extended_end;
20223 const gdb_byte *line_end;
20224 unsigned int bytes_read, extended_len;
20225 unsigned char op_code, extended_op;
20226 CORE_ADDR baseaddr;
20227 struct objfile *objfile = cu->per_objfile->objfile;
20228 bfd *abfd = objfile->obfd;
20229 struct gdbarch *gdbarch = objfile->arch ();
20230 /* True if we're recording line info (as opposed to building partial
20231 symtabs and just interested in finding include files mentioned by
20232 the line number program). */
20233 bool record_lines_p = !decode_for_pst_p;
20234
20235 baseaddr = objfile->text_section_offset ();
20236
20237 line_ptr = lh->statement_program_start;
20238 line_end = lh->statement_program_end;
20239
20240 /* Read the statement sequences until there's nothing left. */
20241 while (line_ptr < line_end)
20242 {
20243 /* The DWARF line number program state machine. Reset the state
20244 machine at the start of each sequence. */
20245 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20246 bool end_sequence = false;
20247
20248 if (record_lines_p)
20249 {
20250 /* Start a subfile for the current file of the state
20251 machine. */
20252 const file_entry *fe = state_machine.current_file ();
20253
20254 if (fe != NULL)
20255 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20256 }
20257
20258 /* Decode the table. */
20259 while (line_ptr < line_end && !end_sequence)
20260 {
20261 op_code = read_1_byte (abfd, line_ptr);
20262 line_ptr += 1;
20263
20264 if (op_code >= lh->opcode_base)
20265 {
20266 /* Special opcode. */
20267 state_machine.handle_special_opcode (op_code);
20268 }
20269 else switch (op_code)
20270 {
20271 case DW_LNS_extended_op:
20272 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20273 &bytes_read);
20274 line_ptr += bytes_read;
20275 extended_end = line_ptr + extended_len;
20276 extended_op = read_1_byte (abfd, line_ptr);
20277 line_ptr += 1;
20278 switch (extended_op)
20279 {
20280 case DW_LNE_end_sequence:
20281 state_machine.handle_end_sequence ();
20282 end_sequence = true;
20283 break;
20284 case DW_LNE_set_address:
20285 {
20286 CORE_ADDR address
20287 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20288 line_ptr += bytes_read;
20289
20290 state_machine.check_line_address (cu, line_ptr,
20291 lowpc - baseaddr, address);
20292 state_machine.handle_set_address (baseaddr, address);
20293 }
20294 break;
20295 case DW_LNE_define_file:
20296 {
20297 const char *cur_file;
20298 unsigned int mod_time, length;
20299 dir_index dindex;
20300
20301 cur_file = read_direct_string (abfd, line_ptr,
20302 &bytes_read);
20303 line_ptr += bytes_read;
20304 dindex = (dir_index)
20305 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20306 line_ptr += bytes_read;
20307 mod_time =
20308 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20309 line_ptr += bytes_read;
20310 length =
20311 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20312 line_ptr += bytes_read;
20313 lh->add_file_name (cur_file, dindex, mod_time, length);
20314 }
20315 break;
20316 case DW_LNE_set_discriminator:
20317 {
20318 /* The discriminator is not interesting to the
20319 debugger; just ignore it. We still need to
20320 check its value though:
20321 if there are consecutive entries for the same
20322 (non-prologue) line we want to coalesce them.
20323 PR 17276. */
20324 unsigned int discr
20325 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20326 line_ptr += bytes_read;
20327
20328 state_machine.handle_set_discriminator (discr);
20329 }
20330 break;
20331 default:
20332 complaint (_("mangled .debug_line section"));
20333 return;
20334 }
20335 /* Make sure that we parsed the extended op correctly. If e.g.
20336 we expected a different address size than the producer used,
20337 we may have read the wrong number of bytes. */
20338 if (line_ptr != extended_end)
20339 {
20340 complaint (_("mangled .debug_line section"));
20341 return;
20342 }
20343 break;
20344 case DW_LNS_copy:
20345 state_machine.handle_copy ();
20346 break;
20347 case DW_LNS_advance_pc:
20348 {
20349 CORE_ADDR adjust
20350 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20351 line_ptr += bytes_read;
20352
20353 state_machine.handle_advance_pc (adjust);
20354 }
20355 break;
20356 case DW_LNS_advance_line:
20357 {
20358 int line_delta
20359 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20360 line_ptr += bytes_read;
20361
20362 state_machine.handle_advance_line (line_delta);
20363 }
20364 break;
20365 case DW_LNS_set_file:
20366 {
20367 file_name_index file
20368 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20369 &bytes_read);
20370 line_ptr += bytes_read;
20371
20372 state_machine.handle_set_file (file);
20373 }
20374 break;
20375 case DW_LNS_set_column:
20376 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20377 line_ptr += bytes_read;
20378 break;
20379 case DW_LNS_negate_stmt:
20380 state_machine.handle_negate_stmt ();
20381 break;
20382 case DW_LNS_set_basic_block:
20383 break;
20384 /* Add to the address register of the state machine the
20385 address increment value corresponding to special opcode
20386 255. I.e., this value is scaled by the minimum
20387 instruction length since special opcode 255 would have
20388 scaled the increment. */
20389 case DW_LNS_const_add_pc:
20390 state_machine.handle_const_add_pc ();
20391 break;
20392 case DW_LNS_fixed_advance_pc:
20393 {
20394 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20395 line_ptr += 2;
20396
20397 state_machine.handle_fixed_advance_pc (addr_adj);
20398 }
20399 break;
20400 default:
20401 {
20402 /* Unknown standard opcode, ignore it. */
20403 int i;
20404
20405 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20406 {
20407 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20408 line_ptr += bytes_read;
20409 }
20410 }
20411 }
20412 }
20413
20414 if (!end_sequence)
20415 dwarf2_debug_line_missing_end_sequence_complaint ();
20416
20417 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20418 in which case we still finish recording the last line). */
20419 state_machine.record_line (true);
20420 }
20421 }
20422
20423 /* Decode the Line Number Program (LNP) for the given line_header
20424 structure and CU. The actual information extracted and the type
20425 of structures created from the LNP depends on the value of PST.
20426
20427 1. If PST is NULL, then this procedure uses the data from the program
20428 to create all necessary symbol tables, and their linetables.
20429
20430 2. If PST is not NULL, this procedure reads the program to determine
20431 the list of files included by the unit represented by PST, and
20432 builds all the associated partial symbol tables.
20433
20434 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20435 It is used for relative paths in the line table.
20436 NOTE: When processing partial symtabs (pst != NULL),
20437 comp_dir == pst->dirname.
20438
20439 NOTE: It is important that psymtabs have the same file name (via strcmp)
20440 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20441 symtab we don't use it in the name of the psymtabs we create.
20442 E.g. expand_line_sal requires this when finding psymtabs to expand.
20443 A good testcase for this is mb-inline.exp.
20444
20445 LOWPC is the lowest address in CU (or 0 if not known).
20446
20447 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20448 for its PC<->lines mapping information. Otherwise only the filename
20449 table is read in. */
20450
20451 static void
20452 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20453 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20454 CORE_ADDR lowpc, int decode_mapping)
20455 {
20456 struct objfile *objfile = cu->per_objfile->objfile;
20457 const int decode_for_pst_p = (pst != NULL);
20458
20459 if (decode_mapping)
20460 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20461
20462 if (decode_for_pst_p)
20463 {
20464 /* Now that we're done scanning the Line Header Program, we can
20465 create the psymtab of each included file. */
20466 for (auto &file_entry : lh->file_names ())
20467 if (file_entry.included_p == 1)
20468 {
20469 gdb::unique_xmalloc_ptr<char> name_holder;
20470 const char *include_name =
20471 psymtab_include_file_name (lh, file_entry, pst,
20472 comp_dir, &name_holder);
20473 if (include_name != NULL)
20474 dwarf2_create_include_psymtab (include_name, pst, objfile);
20475 }
20476 }
20477 else
20478 {
20479 /* Make sure a symtab is created for every file, even files
20480 which contain only variables (i.e. no code with associated
20481 line numbers). */
20482 buildsym_compunit *builder = cu->get_builder ();
20483 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20484
20485 for (auto &fe : lh->file_names ())
20486 {
20487 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20488 if (builder->get_current_subfile ()->symtab == NULL)
20489 {
20490 builder->get_current_subfile ()->symtab
20491 = allocate_symtab (cust,
20492 builder->get_current_subfile ()->name);
20493 }
20494 fe.symtab = builder->get_current_subfile ()->symtab;
20495 }
20496 }
20497 }
20498
20499 /* Start a subfile for DWARF. FILENAME is the name of the file and
20500 DIRNAME the name of the source directory which contains FILENAME
20501 or NULL if not known.
20502 This routine tries to keep line numbers from identical absolute and
20503 relative file names in a common subfile.
20504
20505 Using the `list' example from the GDB testsuite, which resides in
20506 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20507 of /srcdir/list0.c yields the following debugging information for list0.c:
20508
20509 DW_AT_name: /srcdir/list0.c
20510 DW_AT_comp_dir: /compdir
20511 files.files[0].name: list0.h
20512 files.files[0].dir: /srcdir
20513 files.files[1].name: list0.c
20514 files.files[1].dir: /srcdir
20515
20516 The line number information for list0.c has to end up in a single
20517 subfile, so that `break /srcdir/list0.c:1' works as expected.
20518 start_subfile will ensure that this happens provided that we pass the
20519 concatenation of files.files[1].dir and files.files[1].name as the
20520 subfile's name. */
20521
20522 static void
20523 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20524 const char *dirname)
20525 {
20526 gdb::unique_xmalloc_ptr<char> copy;
20527
20528 /* In order not to lose the line information directory,
20529 we concatenate it to the filename when it makes sense.
20530 Note that the Dwarf3 standard says (speaking of filenames in line
20531 information): ``The directory index is ignored for file names
20532 that represent full path names''. Thus ignoring dirname in the
20533 `else' branch below isn't an issue. */
20534
20535 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20536 {
20537 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20538 filename = copy.get ();
20539 }
20540
20541 cu->get_builder ()->start_subfile (filename);
20542 }
20543
20544 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20545 buildsym_compunit constructor. */
20546
20547 struct compunit_symtab *
20548 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20549 CORE_ADDR low_pc)
20550 {
20551 gdb_assert (m_builder == nullptr);
20552
20553 m_builder.reset (new struct buildsym_compunit
20554 (per_cu->dwarf2_per_objfile->objfile,
20555 name, comp_dir, language, low_pc));
20556
20557 list_in_scope = get_builder ()->get_file_symbols ();
20558
20559 get_builder ()->record_debugformat ("DWARF 2");
20560 get_builder ()->record_producer (producer);
20561
20562 processing_has_namespace_info = false;
20563
20564 return get_builder ()->get_compunit_symtab ();
20565 }
20566
20567 static void
20568 var_decode_location (struct attribute *attr, struct symbol *sym,
20569 struct dwarf2_cu *cu)
20570 {
20571 struct objfile *objfile = cu->per_objfile->objfile;
20572 struct comp_unit_head *cu_header = &cu->header;
20573
20574 /* NOTE drow/2003-01-30: There used to be a comment and some special
20575 code here to turn a symbol with DW_AT_external and a
20576 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20577 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20578 with some versions of binutils) where shared libraries could have
20579 relocations against symbols in their debug information - the
20580 minimal symbol would have the right address, but the debug info
20581 would not. It's no longer necessary, because we will explicitly
20582 apply relocations when we read in the debug information now. */
20583
20584 /* A DW_AT_location attribute with no contents indicates that a
20585 variable has been optimized away. */
20586 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20587 {
20588 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20589 return;
20590 }
20591
20592 /* Handle one degenerate form of location expression specially, to
20593 preserve GDB's previous behavior when section offsets are
20594 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20595 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20596
20597 if (attr->form_is_block ()
20598 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20599 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20600 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20601 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20602 && (DW_BLOCK (attr)->size
20603 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20604 {
20605 unsigned int dummy;
20606
20607 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20608 SET_SYMBOL_VALUE_ADDRESS
20609 (sym, cu->header.read_address (objfile->obfd,
20610 DW_BLOCK (attr)->data + 1,
20611 &dummy));
20612 else
20613 SET_SYMBOL_VALUE_ADDRESS
20614 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20615 &dummy));
20616 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20617 fixup_symbol_section (sym, objfile);
20618 SET_SYMBOL_VALUE_ADDRESS
20619 (sym,
20620 SYMBOL_VALUE_ADDRESS (sym)
20621 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20622 return;
20623 }
20624
20625 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20626 expression evaluator, and use LOC_COMPUTED only when necessary
20627 (i.e. when the value of a register or memory location is
20628 referenced, or a thread-local block, etc.). Then again, it might
20629 not be worthwhile. I'm assuming that it isn't unless performance
20630 or memory numbers show me otherwise. */
20631
20632 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20633
20634 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20635 cu->has_loclist = true;
20636 }
20637
20638 /* Given a pointer to a DWARF information entry, figure out if we need
20639 to make a symbol table entry for it, and if so, create a new entry
20640 and return a pointer to it.
20641 If TYPE is NULL, determine symbol type from the die, otherwise
20642 used the passed type.
20643 If SPACE is not NULL, use it to hold the new symbol. If it is
20644 NULL, allocate a new symbol on the objfile's obstack. */
20645
20646 static struct symbol *
20647 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20648 struct symbol *space)
20649 {
20650 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20651 struct objfile *objfile = dwarf2_per_objfile->objfile;
20652 struct gdbarch *gdbarch = objfile->arch ();
20653 struct symbol *sym = NULL;
20654 const char *name;
20655 struct attribute *attr = NULL;
20656 struct attribute *attr2 = NULL;
20657 CORE_ADDR baseaddr;
20658 struct pending **list_to_add = NULL;
20659
20660 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20661
20662 baseaddr = objfile->text_section_offset ();
20663
20664 name = dwarf2_name (die, cu);
20665 if (name)
20666 {
20667 int suppress_add = 0;
20668
20669 if (space)
20670 sym = space;
20671 else
20672 sym = new (&objfile->objfile_obstack) symbol;
20673 OBJSTAT (objfile, n_syms++);
20674
20675 /* Cache this symbol's name and the name's demangled form (if any). */
20676 sym->set_language (cu->language, &objfile->objfile_obstack);
20677 /* Fortran does not have mangling standard and the mangling does differ
20678 between gfortran, iFort etc. */
20679 const char *physname
20680 = (cu->language == language_fortran
20681 ? dwarf2_full_name (name, die, cu)
20682 : dwarf2_physname (name, die, cu));
20683 const char *linkagename = dw2_linkage_name (die, cu);
20684
20685 if (linkagename == nullptr || cu->language == language_ada)
20686 sym->set_linkage_name (physname);
20687 else
20688 {
20689 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20690 sym->set_linkage_name (linkagename);
20691 }
20692
20693 /* Default assumptions.
20694 Use the passed type or decode it from the die. */
20695 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20696 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20697 if (type != NULL)
20698 SYMBOL_TYPE (sym) = type;
20699 else
20700 SYMBOL_TYPE (sym) = die_type (die, cu);
20701 attr = dwarf2_attr (die,
20702 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20703 cu);
20704 if (attr != nullptr)
20705 {
20706 SYMBOL_LINE (sym) = DW_UNSND (attr);
20707 }
20708
20709 attr = dwarf2_attr (die,
20710 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20711 cu);
20712 if (attr != nullptr)
20713 {
20714 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20715 struct file_entry *fe;
20716
20717 if (cu->line_header != NULL)
20718 fe = cu->line_header->file_name_at (file_index);
20719 else
20720 fe = NULL;
20721
20722 if (fe == NULL)
20723 complaint (_("file index out of range"));
20724 else
20725 symbol_set_symtab (sym, fe->symtab);
20726 }
20727
20728 switch (die->tag)
20729 {
20730 case DW_TAG_label:
20731 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20732 if (attr != nullptr)
20733 {
20734 CORE_ADDR addr;
20735
20736 addr = attr->value_as_address ();
20737 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20738 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20739 }
20740 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20741 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20742 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20743 add_symbol_to_list (sym, cu->list_in_scope);
20744 break;
20745 case DW_TAG_subprogram:
20746 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20747 finish_block. */
20748 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20749 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20750 if ((attr2 && (DW_UNSND (attr2) != 0))
20751 || cu->language == language_ada
20752 || cu->language == language_fortran)
20753 {
20754 /* Subprograms marked external are stored as a global symbol.
20755 Ada and Fortran subprograms, whether marked external or
20756 not, are always stored as a global symbol, because we want
20757 to be able to access them globally. For instance, we want
20758 to be able to break on a nested subprogram without having
20759 to specify the context. */
20760 list_to_add = cu->get_builder ()->get_global_symbols ();
20761 }
20762 else
20763 {
20764 list_to_add = cu->list_in_scope;
20765 }
20766 break;
20767 case DW_TAG_inlined_subroutine:
20768 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20769 finish_block. */
20770 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20771 SYMBOL_INLINED (sym) = 1;
20772 list_to_add = cu->list_in_scope;
20773 break;
20774 case DW_TAG_template_value_param:
20775 suppress_add = 1;
20776 /* Fall through. */
20777 case DW_TAG_constant:
20778 case DW_TAG_variable:
20779 case DW_TAG_member:
20780 /* Compilation with minimal debug info may result in
20781 variables with missing type entries. Change the
20782 misleading `void' type to something sensible. */
20783 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
20784 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20785
20786 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20787 /* In the case of DW_TAG_member, we should only be called for
20788 static const members. */
20789 if (die->tag == DW_TAG_member)
20790 {
20791 /* dwarf2_add_field uses die_is_declaration,
20792 so we do the same. */
20793 gdb_assert (die_is_declaration (die, cu));
20794 gdb_assert (attr);
20795 }
20796 if (attr != nullptr)
20797 {
20798 dwarf2_const_value (attr, sym, cu);
20799 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20800 if (!suppress_add)
20801 {
20802 if (attr2 && (DW_UNSND (attr2) != 0))
20803 list_to_add = cu->get_builder ()->get_global_symbols ();
20804 else
20805 list_to_add = cu->list_in_scope;
20806 }
20807 break;
20808 }
20809 attr = dwarf2_attr (die, DW_AT_location, cu);
20810 if (attr != nullptr)
20811 {
20812 var_decode_location (attr, sym, cu);
20813 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20814
20815 /* Fortran explicitly imports any global symbols to the local
20816 scope by DW_TAG_common_block. */
20817 if (cu->language == language_fortran && die->parent
20818 && die->parent->tag == DW_TAG_common_block)
20819 attr2 = NULL;
20820
20821 if (SYMBOL_CLASS (sym) == LOC_STATIC
20822 && SYMBOL_VALUE_ADDRESS (sym) == 0
20823 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
20824 {
20825 /* When a static variable is eliminated by the linker,
20826 the corresponding debug information is not stripped
20827 out, but the variable address is set to null;
20828 do not add such variables into symbol table. */
20829 }
20830 else if (attr2 && (DW_UNSND (attr2) != 0))
20831 {
20832 if (SYMBOL_CLASS (sym) == LOC_STATIC
20833 && (objfile->flags & OBJF_MAINLINE) == 0
20834 && dwarf2_per_objfile->per_bfd->can_copy)
20835 {
20836 /* A global static variable might be subject to
20837 copy relocation. We first check for a local
20838 minsym, though, because maybe the symbol was
20839 marked hidden, in which case this would not
20840 apply. */
20841 bound_minimal_symbol found
20842 = (lookup_minimal_symbol_linkage
20843 (sym->linkage_name (), objfile));
20844 if (found.minsym != nullptr)
20845 sym->maybe_copied = 1;
20846 }
20847
20848 /* A variable with DW_AT_external is never static,
20849 but it may be block-scoped. */
20850 list_to_add
20851 = ((cu->list_in_scope
20852 == cu->get_builder ()->get_file_symbols ())
20853 ? cu->get_builder ()->get_global_symbols ()
20854 : cu->list_in_scope);
20855 }
20856 else
20857 list_to_add = cu->list_in_scope;
20858 }
20859 else
20860 {
20861 /* We do not know the address of this symbol.
20862 If it is an external symbol and we have type information
20863 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20864 The address of the variable will then be determined from
20865 the minimal symbol table whenever the variable is
20866 referenced. */
20867 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20868
20869 /* Fortran explicitly imports any global symbols to the local
20870 scope by DW_TAG_common_block. */
20871 if (cu->language == language_fortran && die->parent
20872 && die->parent->tag == DW_TAG_common_block)
20873 {
20874 /* SYMBOL_CLASS doesn't matter here because
20875 read_common_block is going to reset it. */
20876 if (!suppress_add)
20877 list_to_add = cu->list_in_scope;
20878 }
20879 else if (attr2 && (DW_UNSND (attr2) != 0)
20880 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20881 {
20882 /* A variable with DW_AT_external is never static, but it
20883 may be block-scoped. */
20884 list_to_add
20885 = ((cu->list_in_scope
20886 == cu->get_builder ()->get_file_symbols ())
20887 ? cu->get_builder ()->get_global_symbols ()
20888 : cu->list_in_scope);
20889
20890 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20891 }
20892 else if (!die_is_declaration (die, cu))
20893 {
20894 /* Use the default LOC_OPTIMIZED_OUT class. */
20895 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20896 if (!suppress_add)
20897 list_to_add = cu->list_in_scope;
20898 }
20899 }
20900 break;
20901 case DW_TAG_formal_parameter:
20902 {
20903 /* If we are inside a function, mark this as an argument. If
20904 not, we might be looking at an argument to an inlined function
20905 when we do not have enough information to show inlined frames;
20906 pretend it's a local variable in that case so that the user can
20907 still see it. */
20908 struct context_stack *curr
20909 = cu->get_builder ()->get_current_context_stack ();
20910 if (curr != nullptr && curr->name != nullptr)
20911 SYMBOL_IS_ARGUMENT (sym) = 1;
20912 attr = dwarf2_attr (die, DW_AT_location, cu);
20913 if (attr != nullptr)
20914 {
20915 var_decode_location (attr, sym, cu);
20916 }
20917 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20918 if (attr != nullptr)
20919 {
20920 dwarf2_const_value (attr, sym, cu);
20921 }
20922
20923 list_to_add = cu->list_in_scope;
20924 }
20925 break;
20926 case DW_TAG_unspecified_parameters:
20927 /* From varargs functions; gdb doesn't seem to have any
20928 interest in this information, so just ignore it for now.
20929 (FIXME?) */
20930 break;
20931 case DW_TAG_template_type_param:
20932 suppress_add = 1;
20933 /* Fall through. */
20934 case DW_TAG_class_type:
20935 case DW_TAG_interface_type:
20936 case DW_TAG_structure_type:
20937 case DW_TAG_union_type:
20938 case DW_TAG_set_type:
20939 case DW_TAG_enumeration_type:
20940 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20941 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20942
20943 {
20944 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20945 really ever be static objects: otherwise, if you try
20946 to, say, break of a class's method and you're in a file
20947 which doesn't mention that class, it won't work unless
20948 the check for all static symbols in lookup_symbol_aux
20949 saves you. See the OtherFileClass tests in
20950 gdb.c++/namespace.exp. */
20951
20952 if (!suppress_add)
20953 {
20954 buildsym_compunit *builder = cu->get_builder ();
20955 list_to_add
20956 = (cu->list_in_scope == builder->get_file_symbols ()
20957 && cu->language == language_cplus
20958 ? builder->get_global_symbols ()
20959 : cu->list_in_scope);
20960
20961 /* The semantics of C++ state that "struct foo {
20962 ... }" also defines a typedef for "foo". */
20963 if (cu->language == language_cplus
20964 || cu->language == language_ada
20965 || cu->language == language_d
20966 || cu->language == language_rust)
20967 {
20968 /* The symbol's name is already allocated along
20969 with this objfile, so we don't need to
20970 duplicate it for the type. */
20971 if (SYMBOL_TYPE (sym)->name () == 0)
20972 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
20973 }
20974 }
20975 }
20976 break;
20977 case DW_TAG_typedef:
20978 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20979 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20980 list_to_add = cu->list_in_scope;
20981 break;
20982 case DW_TAG_base_type:
20983 case DW_TAG_subrange_type:
20984 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20985 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20986 list_to_add = cu->list_in_scope;
20987 break;
20988 case DW_TAG_enumerator:
20989 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20990 if (attr != nullptr)
20991 {
20992 dwarf2_const_value (attr, sym, cu);
20993 }
20994 {
20995 /* NOTE: carlton/2003-11-10: See comment above in the
20996 DW_TAG_class_type, etc. block. */
20997
20998 list_to_add
20999 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21000 && cu->language == language_cplus
21001 ? cu->get_builder ()->get_global_symbols ()
21002 : cu->list_in_scope);
21003 }
21004 break;
21005 case DW_TAG_imported_declaration:
21006 case DW_TAG_namespace:
21007 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21008 list_to_add = cu->get_builder ()->get_global_symbols ();
21009 break;
21010 case DW_TAG_module:
21011 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21012 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21013 list_to_add = cu->get_builder ()->get_global_symbols ();
21014 break;
21015 case DW_TAG_common_block:
21016 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21017 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21018 add_symbol_to_list (sym, cu->list_in_scope);
21019 break;
21020 default:
21021 /* Not a tag we recognize. Hopefully we aren't processing
21022 trash data, but since we must specifically ignore things
21023 we don't recognize, there is nothing else we should do at
21024 this point. */
21025 complaint (_("unsupported tag: '%s'"),
21026 dwarf_tag_name (die->tag));
21027 break;
21028 }
21029
21030 if (suppress_add)
21031 {
21032 sym->hash_next = objfile->template_symbols;
21033 objfile->template_symbols = sym;
21034 list_to_add = NULL;
21035 }
21036
21037 if (list_to_add != NULL)
21038 add_symbol_to_list (sym, list_to_add);
21039
21040 /* For the benefit of old versions of GCC, check for anonymous
21041 namespaces based on the demangled name. */
21042 if (!cu->processing_has_namespace_info
21043 && cu->language == language_cplus)
21044 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21045 }
21046 return (sym);
21047 }
21048
21049 /* Given an attr with a DW_FORM_dataN value in host byte order,
21050 zero-extend it as appropriate for the symbol's type. The DWARF
21051 standard (v4) is not entirely clear about the meaning of using
21052 DW_FORM_dataN for a constant with a signed type, where the type is
21053 wider than the data. The conclusion of a discussion on the DWARF
21054 list was that this is unspecified. We choose to always zero-extend
21055 because that is the interpretation long in use by GCC. */
21056
21057 static gdb_byte *
21058 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21059 struct dwarf2_cu *cu, LONGEST *value, int bits)
21060 {
21061 struct objfile *objfile = cu->per_objfile->objfile;
21062 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21063 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21064 LONGEST l = DW_UNSND (attr);
21065
21066 if (bits < sizeof (*value) * 8)
21067 {
21068 l &= ((LONGEST) 1 << bits) - 1;
21069 *value = l;
21070 }
21071 else if (bits == sizeof (*value) * 8)
21072 *value = l;
21073 else
21074 {
21075 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21076 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21077 return bytes;
21078 }
21079
21080 return NULL;
21081 }
21082
21083 /* Read a constant value from an attribute. Either set *VALUE, or if
21084 the value does not fit in *VALUE, set *BYTES - either already
21085 allocated on the objfile obstack, or newly allocated on OBSTACK,
21086 or, set *BATON, if we translated the constant to a location
21087 expression. */
21088
21089 static void
21090 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21091 const char *name, struct obstack *obstack,
21092 struct dwarf2_cu *cu,
21093 LONGEST *value, const gdb_byte **bytes,
21094 struct dwarf2_locexpr_baton **baton)
21095 {
21096 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21097 struct objfile *objfile = per_objfile->objfile;
21098 struct comp_unit_head *cu_header = &cu->header;
21099 struct dwarf_block *blk;
21100 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21101 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21102
21103 *value = 0;
21104 *bytes = NULL;
21105 *baton = NULL;
21106
21107 switch (attr->form)
21108 {
21109 case DW_FORM_addr:
21110 case DW_FORM_addrx:
21111 case DW_FORM_GNU_addr_index:
21112 {
21113 gdb_byte *data;
21114
21115 if (TYPE_LENGTH (type) != cu_header->addr_size)
21116 dwarf2_const_value_length_mismatch_complaint (name,
21117 cu_header->addr_size,
21118 TYPE_LENGTH (type));
21119 /* Symbols of this form are reasonably rare, so we just
21120 piggyback on the existing location code rather than writing
21121 a new implementation of symbol_computed_ops. */
21122 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21123 (*baton)->per_objfile = per_objfile;
21124 (*baton)->per_cu = cu->per_cu;
21125 gdb_assert ((*baton)->per_cu);
21126
21127 (*baton)->size = 2 + cu_header->addr_size;
21128 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21129 (*baton)->data = data;
21130
21131 data[0] = DW_OP_addr;
21132 store_unsigned_integer (&data[1], cu_header->addr_size,
21133 byte_order, DW_ADDR (attr));
21134 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21135 }
21136 break;
21137 case DW_FORM_string:
21138 case DW_FORM_strp:
21139 case DW_FORM_strx:
21140 case DW_FORM_GNU_str_index:
21141 case DW_FORM_GNU_strp_alt:
21142 /* DW_STRING is already allocated on the objfile obstack, point
21143 directly to it. */
21144 *bytes = (const gdb_byte *) DW_STRING (attr);
21145 break;
21146 case DW_FORM_block1:
21147 case DW_FORM_block2:
21148 case DW_FORM_block4:
21149 case DW_FORM_block:
21150 case DW_FORM_exprloc:
21151 case DW_FORM_data16:
21152 blk = DW_BLOCK (attr);
21153 if (TYPE_LENGTH (type) != blk->size)
21154 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21155 TYPE_LENGTH (type));
21156 *bytes = blk->data;
21157 break;
21158
21159 /* The DW_AT_const_value attributes are supposed to carry the
21160 symbol's value "represented as it would be on the target
21161 architecture." By the time we get here, it's already been
21162 converted to host endianness, so we just need to sign- or
21163 zero-extend it as appropriate. */
21164 case DW_FORM_data1:
21165 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21166 break;
21167 case DW_FORM_data2:
21168 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21169 break;
21170 case DW_FORM_data4:
21171 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21172 break;
21173 case DW_FORM_data8:
21174 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21175 break;
21176
21177 case DW_FORM_sdata:
21178 case DW_FORM_implicit_const:
21179 *value = DW_SND (attr);
21180 break;
21181
21182 case DW_FORM_udata:
21183 *value = DW_UNSND (attr);
21184 break;
21185
21186 default:
21187 complaint (_("unsupported const value attribute form: '%s'"),
21188 dwarf_form_name (attr->form));
21189 *value = 0;
21190 break;
21191 }
21192 }
21193
21194
21195 /* Copy constant value from an attribute to a symbol. */
21196
21197 static void
21198 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21199 struct dwarf2_cu *cu)
21200 {
21201 struct objfile *objfile = cu->per_objfile->objfile;
21202 LONGEST value;
21203 const gdb_byte *bytes;
21204 struct dwarf2_locexpr_baton *baton;
21205
21206 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21207 sym->print_name (),
21208 &objfile->objfile_obstack, cu,
21209 &value, &bytes, &baton);
21210
21211 if (baton != NULL)
21212 {
21213 SYMBOL_LOCATION_BATON (sym) = baton;
21214 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21215 }
21216 else if (bytes != NULL)
21217 {
21218 SYMBOL_VALUE_BYTES (sym) = bytes;
21219 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21220 }
21221 else
21222 {
21223 SYMBOL_VALUE (sym) = value;
21224 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21225 }
21226 }
21227
21228 /* Return the type of the die in question using its DW_AT_type attribute. */
21229
21230 static struct type *
21231 die_type (struct die_info *die, struct dwarf2_cu *cu)
21232 {
21233 struct attribute *type_attr;
21234
21235 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21236 if (!type_attr)
21237 {
21238 struct objfile *objfile = cu->per_objfile->objfile;
21239 /* A missing DW_AT_type represents a void type. */
21240 return objfile_type (objfile)->builtin_void;
21241 }
21242
21243 return lookup_die_type (die, type_attr, cu);
21244 }
21245
21246 /* True iff CU's producer generates GNAT Ada auxiliary information
21247 that allows to find parallel types through that information instead
21248 of having to do expensive parallel lookups by type name. */
21249
21250 static int
21251 need_gnat_info (struct dwarf2_cu *cu)
21252 {
21253 /* Assume that the Ada compiler was GNAT, which always produces
21254 the auxiliary information. */
21255 return (cu->language == language_ada);
21256 }
21257
21258 /* Return the auxiliary type of the die in question using its
21259 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21260 attribute is not present. */
21261
21262 static struct type *
21263 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21264 {
21265 struct attribute *type_attr;
21266
21267 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21268 if (!type_attr)
21269 return NULL;
21270
21271 return lookup_die_type (die, type_attr, cu);
21272 }
21273
21274 /* If DIE has a descriptive_type attribute, then set the TYPE's
21275 descriptive type accordingly. */
21276
21277 static void
21278 set_descriptive_type (struct type *type, struct die_info *die,
21279 struct dwarf2_cu *cu)
21280 {
21281 struct type *descriptive_type = die_descriptive_type (die, cu);
21282
21283 if (descriptive_type)
21284 {
21285 ALLOCATE_GNAT_AUX_TYPE (type);
21286 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21287 }
21288 }
21289
21290 /* Return the containing type of the die in question using its
21291 DW_AT_containing_type attribute. */
21292
21293 static struct type *
21294 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21295 {
21296 struct attribute *type_attr;
21297 struct objfile *objfile = cu->per_objfile->objfile;
21298
21299 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21300 if (!type_attr)
21301 error (_("Dwarf Error: Problem turning containing type into gdb type "
21302 "[in module %s]"), objfile_name (objfile));
21303
21304 return lookup_die_type (die, type_attr, cu);
21305 }
21306
21307 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21308
21309 static struct type *
21310 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21311 {
21312 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21313 struct objfile *objfile = dwarf2_per_objfile->objfile;
21314 char *saved;
21315
21316 std::string message
21317 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21318 objfile_name (objfile),
21319 sect_offset_str (cu->header.sect_off),
21320 sect_offset_str (die->sect_off));
21321 saved = obstack_strdup (&objfile->objfile_obstack, message);
21322
21323 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21324 }
21325
21326 /* Look up the type of DIE in CU using its type attribute ATTR.
21327 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21328 DW_AT_containing_type.
21329 If there is no type substitute an error marker. */
21330
21331 static struct type *
21332 lookup_die_type (struct die_info *die, const struct attribute *attr,
21333 struct dwarf2_cu *cu)
21334 {
21335 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21336 struct objfile *objfile = dwarf2_per_objfile->objfile;
21337 struct type *this_type;
21338
21339 gdb_assert (attr->name == DW_AT_type
21340 || attr->name == DW_AT_GNAT_descriptive_type
21341 || attr->name == DW_AT_containing_type);
21342
21343 /* First see if we have it cached. */
21344
21345 if (attr->form == DW_FORM_GNU_ref_alt)
21346 {
21347 struct dwarf2_per_cu_data *per_cu;
21348 sect_offset sect_off = attr->get_ref_die_offset ();
21349
21350 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21351 dwarf2_per_objfile);
21352 this_type = get_die_type_at_offset (sect_off, per_cu);
21353 }
21354 else if (attr->form_is_ref ())
21355 {
21356 sect_offset sect_off = attr->get_ref_die_offset ();
21357
21358 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21359 }
21360 else if (attr->form == DW_FORM_ref_sig8)
21361 {
21362 ULONGEST signature = DW_SIGNATURE (attr);
21363
21364 return get_signatured_type (die, signature, cu);
21365 }
21366 else
21367 {
21368 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21369 " at %s [in module %s]"),
21370 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21371 objfile_name (objfile));
21372 return build_error_marker_type (cu, die);
21373 }
21374
21375 /* If not cached we need to read it in. */
21376
21377 if (this_type == NULL)
21378 {
21379 struct die_info *type_die = NULL;
21380 struct dwarf2_cu *type_cu = cu;
21381
21382 if (attr->form_is_ref ())
21383 type_die = follow_die_ref (die, attr, &type_cu);
21384 if (type_die == NULL)
21385 return build_error_marker_type (cu, die);
21386 /* If we find the type now, it's probably because the type came
21387 from an inter-CU reference and the type's CU got expanded before
21388 ours. */
21389 this_type = read_type_die (type_die, type_cu);
21390 }
21391
21392 /* If we still don't have a type use an error marker. */
21393
21394 if (this_type == NULL)
21395 return build_error_marker_type (cu, die);
21396
21397 return this_type;
21398 }
21399
21400 /* Return the type in DIE, CU.
21401 Returns NULL for invalid types.
21402
21403 This first does a lookup in die_type_hash,
21404 and only reads the die in if necessary.
21405
21406 NOTE: This can be called when reading in partial or full symbols. */
21407
21408 static struct type *
21409 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21410 {
21411 struct type *this_type;
21412
21413 this_type = get_die_type (die, cu);
21414 if (this_type)
21415 return this_type;
21416
21417 return read_type_die_1 (die, cu);
21418 }
21419
21420 /* Read the type in DIE, CU.
21421 Returns NULL for invalid types. */
21422
21423 static struct type *
21424 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21425 {
21426 struct type *this_type = NULL;
21427
21428 switch (die->tag)
21429 {
21430 case DW_TAG_class_type:
21431 case DW_TAG_interface_type:
21432 case DW_TAG_structure_type:
21433 case DW_TAG_union_type:
21434 this_type = read_structure_type (die, cu);
21435 break;
21436 case DW_TAG_enumeration_type:
21437 this_type = read_enumeration_type (die, cu);
21438 break;
21439 case DW_TAG_subprogram:
21440 case DW_TAG_subroutine_type:
21441 case DW_TAG_inlined_subroutine:
21442 this_type = read_subroutine_type (die, cu);
21443 break;
21444 case DW_TAG_array_type:
21445 this_type = read_array_type (die, cu);
21446 break;
21447 case DW_TAG_set_type:
21448 this_type = read_set_type (die, cu);
21449 break;
21450 case DW_TAG_pointer_type:
21451 this_type = read_tag_pointer_type (die, cu);
21452 break;
21453 case DW_TAG_ptr_to_member_type:
21454 this_type = read_tag_ptr_to_member_type (die, cu);
21455 break;
21456 case DW_TAG_reference_type:
21457 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21458 break;
21459 case DW_TAG_rvalue_reference_type:
21460 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21461 break;
21462 case DW_TAG_const_type:
21463 this_type = read_tag_const_type (die, cu);
21464 break;
21465 case DW_TAG_volatile_type:
21466 this_type = read_tag_volatile_type (die, cu);
21467 break;
21468 case DW_TAG_restrict_type:
21469 this_type = read_tag_restrict_type (die, cu);
21470 break;
21471 case DW_TAG_string_type:
21472 this_type = read_tag_string_type (die, cu);
21473 break;
21474 case DW_TAG_typedef:
21475 this_type = read_typedef (die, cu);
21476 break;
21477 case DW_TAG_subrange_type:
21478 this_type = read_subrange_type (die, cu);
21479 break;
21480 case DW_TAG_base_type:
21481 this_type = read_base_type (die, cu);
21482 break;
21483 case DW_TAG_unspecified_type:
21484 this_type = read_unspecified_type (die, cu);
21485 break;
21486 case DW_TAG_namespace:
21487 this_type = read_namespace_type (die, cu);
21488 break;
21489 case DW_TAG_module:
21490 this_type = read_module_type (die, cu);
21491 break;
21492 case DW_TAG_atomic_type:
21493 this_type = read_tag_atomic_type (die, cu);
21494 break;
21495 default:
21496 complaint (_("unexpected tag in read_type_die: '%s'"),
21497 dwarf_tag_name (die->tag));
21498 break;
21499 }
21500
21501 return this_type;
21502 }
21503
21504 /* See if we can figure out if the class lives in a namespace. We do
21505 this by looking for a member function; its demangled name will
21506 contain namespace info, if there is any.
21507 Return the computed name or NULL.
21508 Space for the result is allocated on the objfile's obstack.
21509 This is the full-die version of guess_partial_die_structure_name.
21510 In this case we know DIE has no useful parent. */
21511
21512 static const char *
21513 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21514 {
21515 struct die_info *spec_die;
21516 struct dwarf2_cu *spec_cu;
21517 struct die_info *child;
21518 struct objfile *objfile = cu->per_objfile->objfile;
21519
21520 spec_cu = cu;
21521 spec_die = die_specification (die, &spec_cu);
21522 if (spec_die != NULL)
21523 {
21524 die = spec_die;
21525 cu = spec_cu;
21526 }
21527
21528 for (child = die->child;
21529 child != NULL;
21530 child = child->sibling)
21531 {
21532 if (child->tag == DW_TAG_subprogram)
21533 {
21534 const char *linkage_name = dw2_linkage_name (child, cu);
21535
21536 if (linkage_name != NULL)
21537 {
21538 gdb::unique_xmalloc_ptr<char> actual_name
21539 (language_class_name_from_physname (cu->language_defn,
21540 linkage_name));
21541 const char *name = NULL;
21542
21543 if (actual_name != NULL)
21544 {
21545 const char *die_name = dwarf2_name (die, cu);
21546
21547 if (die_name != NULL
21548 && strcmp (die_name, actual_name.get ()) != 0)
21549 {
21550 /* Strip off the class name from the full name.
21551 We want the prefix. */
21552 int die_name_len = strlen (die_name);
21553 int actual_name_len = strlen (actual_name.get ());
21554 const char *ptr = actual_name.get ();
21555
21556 /* Test for '::' as a sanity check. */
21557 if (actual_name_len > die_name_len + 2
21558 && ptr[actual_name_len - die_name_len - 1] == ':')
21559 name = obstack_strndup (
21560 &objfile->per_bfd->storage_obstack,
21561 ptr, actual_name_len - die_name_len - 2);
21562 }
21563 }
21564 return name;
21565 }
21566 }
21567 }
21568
21569 return NULL;
21570 }
21571
21572 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21573 prefix part in such case. See
21574 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21575
21576 static const char *
21577 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21578 {
21579 struct attribute *attr;
21580 const char *base;
21581
21582 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21583 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21584 return NULL;
21585
21586 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21587 return NULL;
21588
21589 attr = dw2_linkage_name_attr (die, cu);
21590 if (attr == NULL || DW_STRING (attr) == NULL)
21591 return NULL;
21592
21593 /* dwarf2_name had to be already called. */
21594 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21595
21596 /* Strip the base name, keep any leading namespaces/classes. */
21597 base = strrchr (DW_STRING (attr), ':');
21598 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21599 return "";
21600
21601 struct objfile *objfile = cu->per_objfile->objfile;
21602 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21603 DW_STRING (attr),
21604 &base[-1] - DW_STRING (attr));
21605 }
21606
21607 /* Return the name of the namespace/class that DIE is defined within,
21608 or "" if we can't tell. The caller should not xfree the result.
21609
21610 For example, if we're within the method foo() in the following
21611 code:
21612
21613 namespace N {
21614 class C {
21615 void foo () {
21616 }
21617 };
21618 }
21619
21620 then determine_prefix on foo's die will return "N::C". */
21621
21622 static const char *
21623 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21624 {
21625 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21626 struct die_info *parent, *spec_die;
21627 struct dwarf2_cu *spec_cu;
21628 struct type *parent_type;
21629 const char *retval;
21630
21631 if (cu->language != language_cplus
21632 && cu->language != language_fortran && cu->language != language_d
21633 && cu->language != language_rust)
21634 return "";
21635
21636 retval = anonymous_struct_prefix (die, cu);
21637 if (retval)
21638 return retval;
21639
21640 /* We have to be careful in the presence of DW_AT_specification.
21641 For example, with GCC 3.4, given the code
21642
21643 namespace N {
21644 void foo() {
21645 // Definition of N::foo.
21646 }
21647 }
21648
21649 then we'll have a tree of DIEs like this:
21650
21651 1: DW_TAG_compile_unit
21652 2: DW_TAG_namespace // N
21653 3: DW_TAG_subprogram // declaration of N::foo
21654 4: DW_TAG_subprogram // definition of N::foo
21655 DW_AT_specification // refers to die #3
21656
21657 Thus, when processing die #4, we have to pretend that we're in
21658 the context of its DW_AT_specification, namely the contex of die
21659 #3. */
21660 spec_cu = cu;
21661 spec_die = die_specification (die, &spec_cu);
21662 if (spec_die == NULL)
21663 parent = die->parent;
21664 else
21665 {
21666 parent = spec_die->parent;
21667 cu = spec_cu;
21668 }
21669
21670 if (parent == NULL)
21671 return "";
21672 else if (parent->building_fullname)
21673 {
21674 const char *name;
21675 const char *parent_name;
21676
21677 /* It has been seen on RealView 2.2 built binaries,
21678 DW_TAG_template_type_param types actually _defined_ as
21679 children of the parent class:
21680
21681 enum E {};
21682 template class <class Enum> Class{};
21683 Class<enum E> class_e;
21684
21685 1: DW_TAG_class_type (Class)
21686 2: DW_TAG_enumeration_type (E)
21687 3: DW_TAG_enumerator (enum1:0)
21688 3: DW_TAG_enumerator (enum2:1)
21689 ...
21690 2: DW_TAG_template_type_param
21691 DW_AT_type DW_FORM_ref_udata (E)
21692
21693 Besides being broken debug info, it can put GDB into an
21694 infinite loop. Consider:
21695
21696 When we're building the full name for Class<E>, we'll start
21697 at Class, and go look over its template type parameters,
21698 finding E. We'll then try to build the full name of E, and
21699 reach here. We're now trying to build the full name of E,
21700 and look over the parent DIE for containing scope. In the
21701 broken case, if we followed the parent DIE of E, we'd again
21702 find Class, and once again go look at its template type
21703 arguments, etc., etc. Simply don't consider such parent die
21704 as source-level parent of this die (it can't be, the language
21705 doesn't allow it), and break the loop here. */
21706 name = dwarf2_name (die, cu);
21707 parent_name = dwarf2_name (parent, cu);
21708 complaint (_("template param type '%s' defined within parent '%s'"),
21709 name ? name : "<unknown>",
21710 parent_name ? parent_name : "<unknown>");
21711 return "";
21712 }
21713 else
21714 switch (parent->tag)
21715 {
21716 case DW_TAG_namespace:
21717 parent_type = read_type_die (parent, cu);
21718 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21719 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21720 Work around this problem here. */
21721 if (cu->language == language_cplus
21722 && strcmp (parent_type->name (), "::") == 0)
21723 return "";
21724 /* We give a name to even anonymous namespaces. */
21725 return parent_type->name ();
21726 case DW_TAG_class_type:
21727 case DW_TAG_interface_type:
21728 case DW_TAG_structure_type:
21729 case DW_TAG_union_type:
21730 case DW_TAG_module:
21731 parent_type = read_type_die (parent, cu);
21732 if (parent_type->name () != NULL)
21733 return parent_type->name ();
21734 else
21735 /* An anonymous structure is only allowed non-static data
21736 members; no typedefs, no member functions, et cetera.
21737 So it does not need a prefix. */
21738 return "";
21739 case DW_TAG_compile_unit:
21740 case DW_TAG_partial_unit:
21741 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21742 if (cu->language == language_cplus
21743 && !dwarf2_per_objfile->per_bfd->types.empty ()
21744 && die->child != NULL
21745 && (die->tag == DW_TAG_class_type
21746 || die->tag == DW_TAG_structure_type
21747 || die->tag == DW_TAG_union_type))
21748 {
21749 const char *name = guess_full_die_structure_name (die, cu);
21750 if (name != NULL)
21751 return name;
21752 }
21753 return "";
21754 case DW_TAG_subprogram:
21755 /* Nested subroutines in Fortran get a prefix with the name
21756 of the parent's subroutine. */
21757 if (cu->language == language_fortran)
21758 {
21759 if ((die->tag == DW_TAG_subprogram)
21760 && (dwarf2_name (parent, cu) != NULL))
21761 return dwarf2_name (parent, cu);
21762 }
21763 return determine_prefix (parent, cu);
21764 case DW_TAG_enumeration_type:
21765 parent_type = read_type_die (parent, cu);
21766 if (TYPE_DECLARED_CLASS (parent_type))
21767 {
21768 if (parent_type->name () != NULL)
21769 return parent_type->name ();
21770 return "";
21771 }
21772 /* Fall through. */
21773 default:
21774 return determine_prefix (parent, cu);
21775 }
21776 }
21777
21778 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21779 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21780 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21781 an obconcat, otherwise allocate storage for the result. The CU argument is
21782 used to determine the language and hence, the appropriate separator. */
21783
21784 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21785
21786 static char *
21787 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21788 int physname, struct dwarf2_cu *cu)
21789 {
21790 const char *lead = "";
21791 const char *sep;
21792
21793 if (suffix == NULL || suffix[0] == '\0'
21794 || prefix == NULL || prefix[0] == '\0')
21795 sep = "";
21796 else if (cu->language == language_d)
21797 {
21798 /* For D, the 'main' function could be defined in any module, but it
21799 should never be prefixed. */
21800 if (strcmp (suffix, "D main") == 0)
21801 {
21802 prefix = "";
21803 sep = "";
21804 }
21805 else
21806 sep = ".";
21807 }
21808 else if (cu->language == language_fortran && physname)
21809 {
21810 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21811 DW_AT_MIPS_linkage_name is preferred and used instead. */
21812
21813 lead = "__";
21814 sep = "_MOD_";
21815 }
21816 else
21817 sep = "::";
21818
21819 if (prefix == NULL)
21820 prefix = "";
21821 if (suffix == NULL)
21822 suffix = "";
21823
21824 if (obs == NULL)
21825 {
21826 char *retval
21827 = ((char *)
21828 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21829
21830 strcpy (retval, lead);
21831 strcat (retval, prefix);
21832 strcat (retval, sep);
21833 strcat (retval, suffix);
21834 return retval;
21835 }
21836 else
21837 {
21838 /* We have an obstack. */
21839 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21840 }
21841 }
21842
21843 /* Get name of a die, return NULL if not found. */
21844
21845 static const char *
21846 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21847 struct objfile *objfile)
21848 {
21849 if (name && cu->language == language_cplus)
21850 {
21851 gdb::unique_xmalloc_ptr<char> canon_name
21852 = cp_canonicalize_string (name);
21853
21854 if (canon_name != nullptr)
21855 name = objfile->intern (canon_name.get ());
21856 }
21857
21858 return name;
21859 }
21860
21861 /* Get name of a die, return NULL if not found.
21862 Anonymous namespaces are converted to their magic string. */
21863
21864 static const char *
21865 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21866 {
21867 struct attribute *attr;
21868 struct objfile *objfile = cu->per_objfile->objfile;
21869
21870 attr = dwarf2_attr (die, DW_AT_name, cu);
21871 if ((!attr || !DW_STRING (attr))
21872 && die->tag != DW_TAG_namespace
21873 && die->tag != DW_TAG_class_type
21874 && die->tag != DW_TAG_interface_type
21875 && die->tag != DW_TAG_structure_type
21876 && die->tag != DW_TAG_union_type)
21877 return NULL;
21878
21879 switch (die->tag)
21880 {
21881 case DW_TAG_compile_unit:
21882 case DW_TAG_partial_unit:
21883 /* Compilation units have a DW_AT_name that is a filename, not
21884 a source language identifier. */
21885 case DW_TAG_enumeration_type:
21886 case DW_TAG_enumerator:
21887 /* These tags always have simple identifiers already; no need
21888 to canonicalize them. */
21889 return DW_STRING (attr);
21890
21891 case DW_TAG_namespace:
21892 if (attr != NULL && DW_STRING (attr) != NULL)
21893 return DW_STRING (attr);
21894 return CP_ANONYMOUS_NAMESPACE_STR;
21895
21896 case DW_TAG_class_type:
21897 case DW_TAG_interface_type:
21898 case DW_TAG_structure_type:
21899 case DW_TAG_union_type:
21900 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21901 structures or unions. These were of the form "._%d" in GCC 4.1,
21902 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21903 and GCC 4.4. We work around this problem by ignoring these. */
21904 if (attr && DW_STRING (attr)
21905 && (startswith (DW_STRING (attr), "._")
21906 || startswith (DW_STRING (attr), "<anonymous")))
21907 return NULL;
21908
21909 /* GCC might emit a nameless typedef that has a linkage name. See
21910 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21911 if (!attr || DW_STRING (attr) == NULL)
21912 {
21913 attr = dw2_linkage_name_attr (die, cu);
21914 if (attr == NULL || DW_STRING (attr) == NULL)
21915 return NULL;
21916
21917 /* Avoid demangling DW_STRING (attr) the second time on a second
21918 call for the same DIE. */
21919 if (!DW_STRING_IS_CANONICAL (attr))
21920 {
21921 gdb::unique_xmalloc_ptr<char> demangled
21922 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21923 if (demangled == nullptr)
21924 return nullptr;
21925
21926 DW_STRING (attr) = objfile->intern (demangled.get ());
21927 DW_STRING_IS_CANONICAL (attr) = 1;
21928 }
21929
21930 /* Strip any leading namespaces/classes, keep only the base name.
21931 DW_AT_name for named DIEs does not contain the prefixes. */
21932 const char *base = strrchr (DW_STRING (attr), ':');
21933 if (base && base > DW_STRING (attr) && base[-1] == ':')
21934 return &base[1];
21935 else
21936 return DW_STRING (attr);
21937 }
21938 break;
21939
21940 default:
21941 break;
21942 }
21943
21944 if (!DW_STRING_IS_CANONICAL (attr))
21945 {
21946 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21947 objfile);
21948 DW_STRING_IS_CANONICAL (attr) = 1;
21949 }
21950 return DW_STRING (attr);
21951 }
21952
21953 /* Return the die that this die in an extension of, or NULL if there
21954 is none. *EXT_CU is the CU containing DIE on input, and the CU
21955 containing the return value on output. */
21956
21957 static struct die_info *
21958 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21959 {
21960 struct attribute *attr;
21961
21962 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21963 if (attr == NULL)
21964 return NULL;
21965
21966 return follow_die_ref (die, attr, ext_cu);
21967 }
21968
21969 static void
21970 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21971 {
21972 unsigned int i;
21973
21974 print_spaces (indent, f);
21975 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21976 dwarf_tag_name (die->tag), die->abbrev,
21977 sect_offset_str (die->sect_off));
21978
21979 if (die->parent != NULL)
21980 {
21981 print_spaces (indent, f);
21982 fprintf_unfiltered (f, " parent at offset: %s\n",
21983 sect_offset_str (die->parent->sect_off));
21984 }
21985
21986 print_spaces (indent, f);
21987 fprintf_unfiltered (f, " has children: %s\n",
21988 dwarf_bool_name (die->child != NULL));
21989
21990 print_spaces (indent, f);
21991 fprintf_unfiltered (f, " attributes:\n");
21992
21993 for (i = 0; i < die->num_attrs; ++i)
21994 {
21995 print_spaces (indent, f);
21996 fprintf_unfiltered (f, " %s (%s) ",
21997 dwarf_attr_name (die->attrs[i].name),
21998 dwarf_form_name (die->attrs[i].form));
21999
22000 switch (die->attrs[i].form)
22001 {
22002 case DW_FORM_addr:
22003 case DW_FORM_addrx:
22004 case DW_FORM_GNU_addr_index:
22005 fprintf_unfiltered (f, "address: ");
22006 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22007 break;
22008 case DW_FORM_block2:
22009 case DW_FORM_block4:
22010 case DW_FORM_block:
22011 case DW_FORM_block1:
22012 fprintf_unfiltered (f, "block: size %s",
22013 pulongest (DW_BLOCK (&die->attrs[i])->size));
22014 break;
22015 case DW_FORM_exprloc:
22016 fprintf_unfiltered (f, "expression: size %s",
22017 pulongest (DW_BLOCK (&die->attrs[i])->size));
22018 break;
22019 case DW_FORM_data16:
22020 fprintf_unfiltered (f, "constant of 16 bytes");
22021 break;
22022 case DW_FORM_ref_addr:
22023 fprintf_unfiltered (f, "ref address: ");
22024 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22025 break;
22026 case DW_FORM_GNU_ref_alt:
22027 fprintf_unfiltered (f, "alt ref address: ");
22028 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22029 break;
22030 case DW_FORM_ref1:
22031 case DW_FORM_ref2:
22032 case DW_FORM_ref4:
22033 case DW_FORM_ref8:
22034 case DW_FORM_ref_udata:
22035 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22036 (long) (DW_UNSND (&die->attrs[i])));
22037 break;
22038 case DW_FORM_data1:
22039 case DW_FORM_data2:
22040 case DW_FORM_data4:
22041 case DW_FORM_data8:
22042 case DW_FORM_udata:
22043 case DW_FORM_sdata:
22044 fprintf_unfiltered (f, "constant: %s",
22045 pulongest (DW_UNSND (&die->attrs[i])));
22046 break;
22047 case DW_FORM_sec_offset:
22048 fprintf_unfiltered (f, "section offset: %s",
22049 pulongest (DW_UNSND (&die->attrs[i])));
22050 break;
22051 case DW_FORM_ref_sig8:
22052 fprintf_unfiltered (f, "signature: %s",
22053 hex_string (DW_SIGNATURE (&die->attrs[i])));
22054 break;
22055 case DW_FORM_string:
22056 case DW_FORM_strp:
22057 case DW_FORM_line_strp:
22058 case DW_FORM_strx:
22059 case DW_FORM_GNU_str_index:
22060 case DW_FORM_GNU_strp_alt:
22061 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22062 DW_STRING (&die->attrs[i])
22063 ? DW_STRING (&die->attrs[i]) : "",
22064 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22065 break;
22066 case DW_FORM_flag:
22067 if (DW_UNSND (&die->attrs[i]))
22068 fprintf_unfiltered (f, "flag: TRUE");
22069 else
22070 fprintf_unfiltered (f, "flag: FALSE");
22071 break;
22072 case DW_FORM_flag_present:
22073 fprintf_unfiltered (f, "flag: TRUE");
22074 break;
22075 case DW_FORM_indirect:
22076 /* The reader will have reduced the indirect form to
22077 the "base form" so this form should not occur. */
22078 fprintf_unfiltered (f,
22079 "unexpected attribute form: DW_FORM_indirect");
22080 break;
22081 case DW_FORM_implicit_const:
22082 fprintf_unfiltered (f, "constant: %s",
22083 plongest (DW_SND (&die->attrs[i])));
22084 break;
22085 default:
22086 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22087 die->attrs[i].form);
22088 break;
22089 }
22090 fprintf_unfiltered (f, "\n");
22091 }
22092 }
22093
22094 static void
22095 dump_die_for_error (struct die_info *die)
22096 {
22097 dump_die_shallow (gdb_stderr, 0, die);
22098 }
22099
22100 static void
22101 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22102 {
22103 int indent = level * 4;
22104
22105 gdb_assert (die != NULL);
22106
22107 if (level >= max_level)
22108 return;
22109
22110 dump_die_shallow (f, indent, die);
22111
22112 if (die->child != NULL)
22113 {
22114 print_spaces (indent, f);
22115 fprintf_unfiltered (f, " Children:");
22116 if (level + 1 < max_level)
22117 {
22118 fprintf_unfiltered (f, "\n");
22119 dump_die_1 (f, level + 1, max_level, die->child);
22120 }
22121 else
22122 {
22123 fprintf_unfiltered (f,
22124 " [not printed, max nesting level reached]\n");
22125 }
22126 }
22127
22128 if (die->sibling != NULL && level > 0)
22129 {
22130 dump_die_1 (f, level, max_level, die->sibling);
22131 }
22132 }
22133
22134 /* This is called from the pdie macro in gdbinit.in.
22135 It's not static so gcc will keep a copy callable from gdb. */
22136
22137 void
22138 dump_die (struct die_info *die, int max_level)
22139 {
22140 dump_die_1 (gdb_stdlog, 0, max_level, die);
22141 }
22142
22143 static void
22144 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22145 {
22146 void **slot;
22147
22148 slot = htab_find_slot_with_hash (cu->die_hash, die,
22149 to_underlying (die->sect_off),
22150 INSERT);
22151
22152 *slot = die;
22153 }
22154
22155 /* Follow reference or signature attribute ATTR of SRC_DIE.
22156 On entry *REF_CU is the CU of SRC_DIE.
22157 On exit *REF_CU is the CU of the result. */
22158
22159 static struct die_info *
22160 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22161 struct dwarf2_cu **ref_cu)
22162 {
22163 struct die_info *die;
22164
22165 if (attr->form_is_ref ())
22166 die = follow_die_ref (src_die, attr, ref_cu);
22167 else if (attr->form == DW_FORM_ref_sig8)
22168 die = follow_die_sig (src_die, attr, ref_cu);
22169 else
22170 {
22171 dump_die_for_error (src_die);
22172 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22173 objfile_name ((*ref_cu)->per_objfile->objfile));
22174 }
22175
22176 return die;
22177 }
22178
22179 /* Follow reference OFFSET.
22180 On entry *REF_CU is the CU of the source die referencing OFFSET.
22181 On exit *REF_CU is the CU of the result.
22182 Returns NULL if OFFSET is invalid. */
22183
22184 static struct die_info *
22185 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22186 struct dwarf2_cu **ref_cu)
22187 {
22188 struct die_info temp_die;
22189 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22190 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22191
22192 gdb_assert (cu->per_cu != NULL);
22193
22194 target_cu = cu;
22195
22196 if (cu->per_cu->is_debug_types)
22197 {
22198 /* .debug_types CUs cannot reference anything outside their CU.
22199 If they need to, they have to reference a signatured type via
22200 DW_FORM_ref_sig8. */
22201 if (!cu->header.offset_in_cu_p (sect_off))
22202 return NULL;
22203 }
22204 else if (offset_in_dwz != cu->per_cu->is_dwz
22205 || !cu->header.offset_in_cu_p (sect_off))
22206 {
22207 struct dwarf2_per_cu_data *per_cu;
22208
22209 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22210 dwarf2_per_objfile);
22211
22212 /* If necessary, add it to the queue and load its DIEs. */
22213 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22214 load_full_comp_unit (per_cu, false, cu->language);
22215
22216 target_cu = per_cu->cu;
22217 }
22218 else if (cu->dies == NULL)
22219 {
22220 /* We're loading full DIEs during partial symbol reading. */
22221 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
22222 load_full_comp_unit (cu->per_cu, false, language_minimal);
22223 }
22224
22225 *ref_cu = target_cu;
22226 temp_die.sect_off = sect_off;
22227
22228 if (target_cu != cu)
22229 target_cu->ancestor = cu;
22230
22231 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22232 &temp_die,
22233 to_underlying (sect_off));
22234 }
22235
22236 /* Follow reference attribute ATTR of SRC_DIE.
22237 On entry *REF_CU is the CU of SRC_DIE.
22238 On exit *REF_CU is the CU of the result. */
22239
22240 static struct die_info *
22241 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22242 struct dwarf2_cu **ref_cu)
22243 {
22244 sect_offset sect_off = attr->get_ref_die_offset ();
22245 struct dwarf2_cu *cu = *ref_cu;
22246 struct die_info *die;
22247
22248 die = follow_die_offset (sect_off,
22249 (attr->form == DW_FORM_GNU_ref_alt
22250 || cu->per_cu->is_dwz),
22251 ref_cu);
22252 if (!die)
22253 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22254 "at %s [in module %s]"),
22255 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22256 objfile_name (cu->per_objfile->objfile));
22257
22258 return die;
22259 }
22260
22261 /* See read.h. */
22262
22263 struct dwarf2_locexpr_baton
22264 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22265 dwarf2_per_cu_data *per_cu,
22266 CORE_ADDR (*get_frame_pc) (void *baton),
22267 void *baton, bool resolve_abstract_p)
22268 {
22269 struct dwarf2_cu *cu;
22270 struct die_info *die;
22271 struct attribute *attr;
22272 struct dwarf2_locexpr_baton retval;
22273 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22274 struct objfile *objfile = dwarf2_per_objfile->objfile;
22275
22276 if (per_cu->cu == NULL)
22277 load_cu (per_cu, false);
22278 cu = per_cu->cu;
22279 if (cu == NULL)
22280 {
22281 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22282 Instead just throw an error, not much else we can do. */
22283 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22284 sect_offset_str (sect_off), objfile_name (objfile));
22285 }
22286
22287 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22288 if (!die)
22289 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22290 sect_offset_str (sect_off), objfile_name (objfile));
22291
22292 attr = dwarf2_attr (die, DW_AT_location, cu);
22293 if (!attr && resolve_abstract_p
22294 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22295 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
22296 {
22297 CORE_ADDR pc = (*get_frame_pc) (baton);
22298 CORE_ADDR baseaddr = objfile->text_section_offset ();
22299 struct gdbarch *gdbarch = objfile->arch ();
22300
22301 for (const auto &cand_off
22302 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22303 {
22304 struct dwarf2_cu *cand_cu = cu;
22305 struct die_info *cand
22306 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22307 if (!cand
22308 || !cand->parent
22309 || cand->parent->tag != DW_TAG_subprogram)
22310 continue;
22311
22312 CORE_ADDR pc_low, pc_high;
22313 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22314 if (pc_low == ((CORE_ADDR) -1))
22315 continue;
22316 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22317 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22318 if (!(pc_low <= pc && pc < pc_high))
22319 continue;
22320
22321 die = cand;
22322 attr = dwarf2_attr (die, DW_AT_location, cu);
22323 break;
22324 }
22325 }
22326
22327 if (!attr)
22328 {
22329 /* DWARF: "If there is no such attribute, then there is no effect.".
22330 DATA is ignored if SIZE is 0. */
22331
22332 retval.data = NULL;
22333 retval.size = 0;
22334 }
22335 else if (attr->form_is_section_offset ())
22336 {
22337 struct dwarf2_loclist_baton loclist_baton;
22338 CORE_ADDR pc = (*get_frame_pc) (baton);
22339 size_t size;
22340
22341 fill_in_loclist_baton (cu, &loclist_baton, attr);
22342
22343 retval.data = dwarf2_find_location_expression (&loclist_baton,
22344 &size, pc);
22345 retval.size = size;
22346 }
22347 else
22348 {
22349 if (!attr->form_is_block ())
22350 error (_("Dwarf Error: DIE at %s referenced in module %s "
22351 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22352 sect_offset_str (sect_off), objfile_name (objfile));
22353
22354 retval.data = DW_BLOCK (attr)->data;
22355 retval.size = DW_BLOCK (attr)->size;
22356 }
22357 retval.per_objfile = dwarf2_per_objfile;
22358 retval.per_cu = cu->per_cu;
22359
22360 age_cached_comp_units (dwarf2_per_objfile);
22361
22362 return retval;
22363 }
22364
22365 /* See read.h. */
22366
22367 struct dwarf2_locexpr_baton
22368 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22369 dwarf2_per_cu_data *per_cu,
22370 CORE_ADDR (*get_frame_pc) (void *baton),
22371 void *baton)
22372 {
22373 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22374
22375 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22376 }
22377
22378 /* Write a constant of a given type as target-ordered bytes into
22379 OBSTACK. */
22380
22381 static const gdb_byte *
22382 write_constant_as_bytes (struct obstack *obstack,
22383 enum bfd_endian byte_order,
22384 struct type *type,
22385 ULONGEST value,
22386 LONGEST *len)
22387 {
22388 gdb_byte *result;
22389
22390 *len = TYPE_LENGTH (type);
22391 result = (gdb_byte *) obstack_alloc (obstack, *len);
22392 store_unsigned_integer (result, *len, byte_order, value);
22393
22394 return result;
22395 }
22396
22397 /* See read.h. */
22398
22399 const gdb_byte *
22400 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22401 dwarf2_per_cu_data *per_cu,
22402 obstack *obstack,
22403 LONGEST *len)
22404 {
22405 struct dwarf2_cu *cu;
22406 struct die_info *die;
22407 struct attribute *attr;
22408 const gdb_byte *result = NULL;
22409 struct type *type;
22410 LONGEST value;
22411 enum bfd_endian byte_order;
22412 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22413
22414 if (per_cu->cu == NULL)
22415 load_cu (per_cu, false);
22416 cu = per_cu->cu;
22417 if (cu == NULL)
22418 {
22419 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22420 Instead just throw an error, not much else we can do. */
22421 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22422 sect_offset_str (sect_off), objfile_name (objfile));
22423 }
22424
22425 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22426 if (!die)
22427 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22428 sect_offset_str (sect_off), objfile_name (objfile));
22429
22430 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22431 if (attr == NULL)
22432 return NULL;
22433
22434 byte_order = (bfd_big_endian (objfile->obfd)
22435 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22436
22437 switch (attr->form)
22438 {
22439 case DW_FORM_addr:
22440 case DW_FORM_addrx:
22441 case DW_FORM_GNU_addr_index:
22442 {
22443 gdb_byte *tem;
22444
22445 *len = cu->header.addr_size;
22446 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22447 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22448 result = tem;
22449 }
22450 break;
22451 case DW_FORM_string:
22452 case DW_FORM_strp:
22453 case DW_FORM_strx:
22454 case DW_FORM_GNU_str_index:
22455 case DW_FORM_GNU_strp_alt:
22456 /* DW_STRING is already allocated on the objfile obstack, point
22457 directly to it. */
22458 result = (const gdb_byte *) DW_STRING (attr);
22459 *len = strlen (DW_STRING (attr));
22460 break;
22461 case DW_FORM_block1:
22462 case DW_FORM_block2:
22463 case DW_FORM_block4:
22464 case DW_FORM_block:
22465 case DW_FORM_exprloc:
22466 case DW_FORM_data16:
22467 result = DW_BLOCK (attr)->data;
22468 *len = DW_BLOCK (attr)->size;
22469 break;
22470
22471 /* The DW_AT_const_value attributes are supposed to carry the
22472 symbol's value "represented as it would be on the target
22473 architecture." By the time we get here, it's already been
22474 converted to host endianness, so we just need to sign- or
22475 zero-extend it as appropriate. */
22476 case DW_FORM_data1:
22477 type = die_type (die, cu);
22478 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22479 if (result == NULL)
22480 result = write_constant_as_bytes (obstack, byte_order,
22481 type, value, len);
22482 break;
22483 case DW_FORM_data2:
22484 type = die_type (die, cu);
22485 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22486 if (result == NULL)
22487 result = write_constant_as_bytes (obstack, byte_order,
22488 type, value, len);
22489 break;
22490 case DW_FORM_data4:
22491 type = die_type (die, cu);
22492 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22493 if (result == NULL)
22494 result = write_constant_as_bytes (obstack, byte_order,
22495 type, value, len);
22496 break;
22497 case DW_FORM_data8:
22498 type = die_type (die, cu);
22499 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22500 if (result == NULL)
22501 result = write_constant_as_bytes (obstack, byte_order,
22502 type, value, len);
22503 break;
22504
22505 case DW_FORM_sdata:
22506 case DW_FORM_implicit_const:
22507 type = die_type (die, cu);
22508 result = write_constant_as_bytes (obstack, byte_order,
22509 type, DW_SND (attr), len);
22510 break;
22511
22512 case DW_FORM_udata:
22513 type = die_type (die, cu);
22514 result = write_constant_as_bytes (obstack, byte_order,
22515 type, DW_UNSND (attr), len);
22516 break;
22517
22518 default:
22519 complaint (_("unsupported const value attribute form: '%s'"),
22520 dwarf_form_name (attr->form));
22521 break;
22522 }
22523
22524 return result;
22525 }
22526
22527 /* See read.h. */
22528
22529 struct type *
22530 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22531 dwarf2_per_cu_data *per_cu)
22532 {
22533 struct dwarf2_cu *cu;
22534 struct die_info *die;
22535
22536 if (per_cu->cu == NULL)
22537 load_cu (per_cu, false);
22538 cu = per_cu->cu;
22539 if (!cu)
22540 return NULL;
22541
22542 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22543 if (!die)
22544 return NULL;
22545
22546 return die_type (die, cu);
22547 }
22548
22549 /* See read.h. */
22550
22551 struct type *
22552 dwarf2_get_die_type (cu_offset die_offset,
22553 struct dwarf2_per_cu_data *per_cu)
22554 {
22555 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22556 return get_die_type_at_offset (die_offset_sect, per_cu);
22557 }
22558
22559 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22560 On entry *REF_CU is the CU of SRC_DIE.
22561 On exit *REF_CU is the CU of the result.
22562 Returns NULL if the referenced DIE isn't found. */
22563
22564 static struct die_info *
22565 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22566 struct dwarf2_cu **ref_cu)
22567 {
22568 struct die_info temp_die;
22569 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22570 struct die_info *die;
22571
22572 /* While it might be nice to assert sig_type->type == NULL here,
22573 we can get here for DW_AT_imported_declaration where we need
22574 the DIE not the type. */
22575
22576 /* If necessary, add it to the queue and load its DIEs. */
22577
22578 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22579 read_signatured_type (sig_type);
22580
22581 sig_cu = sig_type->per_cu.cu;
22582 gdb_assert (sig_cu != NULL);
22583 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22584 temp_die.sect_off = sig_type->type_offset_in_section;
22585 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22586 to_underlying (temp_die.sect_off));
22587 if (die)
22588 {
22589 struct dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
22590
22591 /* For .gdb_index version 7 keep track of included TUs.
22592 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22593 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22594 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
22595 {
22596 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22597 }
22598
22599 *ref_cu = sig_cu;
22600 if (sig_cu != cu)
22601 sig_cu->ancestor = cu;
22602
22603 return die;
22604 }
22605
22606 return NULL;
22607 }
22608
22609 /* Follow signatured type referenced by ATTR in SRC_DIE.
22610 On entry *REF_CU is the CU of SRC_DIE.
22611 On exit *REF_CU is the CU of the result.
22612 The result is the DIE of the type.
22613 If the referenced type cannot be found an error is thrown. */
22614
22615 static struct die_info *
22616 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22617 struct dwarf2_cu **ref_cu)
22618 {
22619 ULONGEST signature = DW_SIGNATURE (attr);
22620 struct signatured_type *sig_type;
22621 struct die_info *die;
22622
22623 gdb_assert (attr->form == DW_FORM_ref_sig8);
22624
22625 sig_type = lookup_signatured_type (*ref_cu, signature);
22626 /* sig_type will be NULL if the signatured type is missing from
22627 the debug info. */
22628 if (sig_type == NULL)
22629 {
22630 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22631 " from DIE at %s [in module %s]"),
22632 hex_string (signature), sect_offset_str (src_die->sect_off),
22633 objfile_name ((*ref_cu)->per_objfile->objfile));
22634 }
22635
22636 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22637 if (die == NULL)
22638 {
22639 dump_die_for_error (src_die);
22640 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22641 " from DIE at %s [in module %s]"),
22642 hex_string (signature), sect_offset_str (src_die->sect_off),
22643 objfile_name ((*ref_cu)->per_objfile->objfile));
22644 }
22645
22646 return die;
22647 }
22648
22649 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22650 reading in and processing the type unit if necessary. */
22651
22652 static struct type *
22653 get_signatured_type (struct die_info *die, ULONGEST signature,
22654 struct dwarf2_cu *cu)
22655 {
22656 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22657 struct signatured_type *sig_type;
22658 struct dwarf2_cu *type_cu;
22659 struct die_info *type_die;
22660 struct type *type;
22661
22662 sig_type = lookup_signatured_type (cu, signature);
22663 /* sig_type will be NULL if the signatured type is missing from
22664 the debug info. */
22665 if (sig_type == NULL)
22666 {
22667 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22668 " from DIE at %s [in module %s]"),
22669 hex_string (signature), sect_offset_str (die->sect_off),
22670 objfile_name (dwarf2_per_objfile->objfile));
22671 return build_error_marker_type (cu, die);
22672 }
22673
22674 /* If we already know the type we're done. */
22675 if (sig_type->type != NULL)
22676 return sig_type->type;
22677
22678 type_cu = cu;
22679 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22680 if (type_die != NULL)
22681 {
22682 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22683 is created. This is important, for example, because for c++ classes
22684 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22685 type = read_type_die (type_die, type_cu);
22686 if (type == NULL)
22687 {
22688 complaint (_("Dwarf Error: Cannot build signatured type %s"
22689 " referenced from DIE at %s [in module %s]"),
22690 hex_string (signature), sect_offset_str (die->sect_off),
22691 objfile_name (dwarf2_per_objfile->objfile));
22692 type = build_error_marker_type (cu, die);
22693 }
22694 }
22695 else
22696 {
22697 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22698 " from DIE at %s [in module %s]"),
22699 hex_string (signature), sect_offset_str (die->sect_off),
22700 objfile_name (dwarf2_per_objfile->objfile));
22701 type = build_error_marker_type (cu, die);
22702 }
22703 sig_type->type = type;
22704
22705 return type;
22706 }
22707
22708 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22709 reading in and processing the type unit if necessary. */
22710
22711 static struct type *
22712 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22713 struct dwarf2_cu *cu) /* ARI: editCase function */
22714 {
22715 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22716 if (attr->form_is_ref ())
22717 {
22718 struct dwarf2_cu *type_cu = cu;
22719 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22720
22721 return read_type_die (type_die, type_cu);
22722 }
22723 else if (attr->form == DW_FORM_ref_sig8)
22724 {
22725 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22726 }
22727 else
22728 {
22729 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22730
22731 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22732 " at %s [in module %s]"),
22733 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22734 objfile_name (dwarf2_per_objfile->objfile));
22735 return build_error_marker_type (cu, die);
22736 }
22737 }
22738
22739 /* Load the DIEs associated with type unit PER_CU into memory. */
22740
22741 static void
22742 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22743 {
22744 struct signatured_type *sig_type;
22745
22746 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22747 gdb_assert (! per_cu->type_unit_group_p ());
22748
22749 /* We have the per_cu, but we need the signatured_type.
22750 Fortunately this is an easy translation. */
22751 gdb_assert (per_cu->is_debug_types);
22752 sig_type = (struct signatured_type *) per_cu;
22753
22754 gdb_assert (per_cu->cu == NULL);
22755
22756 read_signatured_type (sig_type);
22757
22758 gdb_assert (per_cu->cu != NULL);
22759 }
22760
22761 /* Read in a signatured type and build its CU and DIEs.
22762 If the type is a stub for the real type in a DWO file,
22763 read in the real type from the DWO file as well. */
22764
22765 static void
22766 read_signatured_type (struct signatured_type *sig_type)
22767 {
22768 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22769
22770 gdb_assert (per_cu->is_debug_types);
22771 gdb_assert (per_cu->cu == NULL);
22772
22773 cutu_reader reader (per_cu, NULL, 0, false);
22774
22775 if (!reader.dummy_p)
22776 {
22777 struct dwarf2_cu *cu = reader.cu;
22778 const gdb_byte *info_ptr = reader.info_ptr;
22779
22780 gdb_assert (cu->die_hash == NULL);
22781 cu->die_hash =
22782 htab_create_alloc_ex (cu->header.length / 12,
22783 die_hash,
22784 die_eq,
22785 NULL,
22786 &cu->comp_unit_obstack,
22787 hashtab_obstack_allocate,
22788 dummy_obstack_deallocate);
22789
22790 if (reader.comp_unit_die->has_children)
22791 reader.comp_unit_die->child
22792 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22793 reader.comp_unit_die);
22794 cu->dies = reader.comp_unit_die;
22795 /* comp_unit_die is not stored in die_hash, no need. */
22796
22797 /* We try not to read any attributes in this function, because
22798 not all CUs needed for references have been loaded yet, and
22799 symbol table processing isn't initialized. But we have to
22800 set the CU language, or we won't be able to build types
22801 correctly. Similarly, if we do not read the producer, we can
22802 not apply producer-specific interpretation. */
22803 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22804
22805 reader.keep ();
22806 }
22807
22808 sig_type->per_cu.tu_read = 1;
22809 }
22810
22811 /* Decode simple location descriptions.
22812 Given a pointer to a dwarf block that defines a location, compute
22813 the location and return the value. If COMPUTED is non-null, it is
22814 set to true to indicate that decoding was successful, and false
22815 otherwise. If COMPUTED is null, then this function may emit a
22816 complaint. */
22817
22818 static CORE_ADDR
22819 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22820 {
22821 struct objfile *objfile = cu->per_objfile->objfile;
22822 size_t i;
22823 size_t size = blk->size;
22824 const gdb_byte *data = blk->data;
22825 CORE_ADDR stack[64];
22826 int stacki;
22827 unsigned int bytes_read, unsnd;
22828 gdb_byte op;
22829
22830 if (computed != nullptr)
22831 *computed = false;
22832
22833 i = 0;
22834 stacki = 0;
22835 stack[stacki] = 0;
22836 stack[++stacki] = 0;
22837
22838 while (i < size)
22839 {
22840 op = data[i++];
22841 switch (op)
22842 {
22843 case DW_OP_lit0:
22844 case DW_OP_lit1:
22845 case DW_OP_lit2:
22846 case DW_OP_lit3:
22847 case DW_OP_lit4:
22848 case DW_OP_lit5:
22849 case DW_OP_lit6:
22850 case DW_OP_lit7:
22851 case DW_OP_lit8:
22852 case DW_OP_lit9:
22853 case DW_OP_lit10:
22854 case DW_OP_lit11:
22855 case DW_OP_lit12:
22856 case DW_OP_lit13:
22857 case DW_OP_lit14:
22858 case DW_OP_lit15:
22859 case DW_OP_lit16:
22860 case DW_OP_lit17:
22861 case DW_OP_lit18:
22862 case DW_OP_lit19:
22863 case DW_OP_lit20:
22864 case DW_OP_lit21:
22865 case DW_OP_lit22:
22866 case DW_OP_lit23:
22867 case DW_OP_lit24:
22868 case DW_OP_lit25:
22869 case DW_OP_lit26:
22870 case DW_OP_lit27:
22871 case DW_OP_lit28:
22872 case DW_OP_lit29:
22873 case DW_OP_lit30:
22874 case DW_OP_lit31:
22875 stack[++stacki] = op - DW_OP_lit0;
22876 break;
22877
22878 case DW_OP_reg0:
22879 case DW_OP_reg1:
22880 case DW_OP_reg2:
22881 case DW_OP_reg3:
22882 case DW_OP_reg4:
22883 case DW_OP_reg5:
22884 case DW_OP_reg6:
22885 case DW_OP_reg7:
22886 case DW_OP_reg8:
22887 case DW_OP_reg9:
22888 case DW_OP_reg10:
22889 case DW_OP_reg11:
22890 case DW_OP_reg12:
22891 case DW_OP_reg13:
22892 case DW_OP_reg14:
22893 case DW_OP_reg15:
22894 case DW_OP_reg16:
22895 case DW_OP_reg17:
22896 case DW_OP_reg18:
22897 case DW_OP_reg19:
22898 case DW_OP_reg20:
22899 case DW_OP_reg21:
22900 case DW_OP_reg22:
22901 case DW_OP_reg23:
22902 case DW_OP_reg24:
22903 case DW_OP_reg25:
22904 case DW_OP_reg26:
22905 case DW_OP_reg27:
22906 case DW_OP_reg28:
22907 case DW_OP_reg29:
22908 case DW_OP_reg30:
22909 case DW_OP_reg31:
22910 stack[++stacki] = op - DW_OP_reg0;
22911 if (i < size)
22912 {
22913 if (computed == nullptr)
22914 dwarf2_complex_location_expr_complaint ();
22915 else
22916 return 0;
22917 }
22918 break;
22919
22920 case DW_OP_regx:
22921 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22922 i += bytes_read;
22923 stack[++stacki] = unsnd;
22924 if (i < size)
22925 {
22926 if (computed == nullptr)
22927 dwarf2_complex_location_expr_complaint ();
22928 else
22929 return 0;
22930 }
22931 break;
22932
22933 case DW_OP_addr:
22934 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22935 &bytes_read);
22936 i += bytes_read;
22937 break;
22938
22939 case DW_OP_const1u:
22940 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22941 i += 1;
22942 break;
22943
22944 case DW_OP_const1s:
22945 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22946 i += 1;
22947 break;
22948
22949 case DW_OP_const2u:
22950 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22951 i += 2;
22952 break;
22953
22954 case DW_OP_const2s:
22955 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22956 i += 2;
22957 break;
22958
22959 case DW_OP_const4u:
22960 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22961 i += 4;
22962 break;
22963
22964 case DW_OP_const4s:
22965 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22966 i += 4;
22967 break;
22968
22969 case DW_OP_const8u:
22970 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22971 i += 8;
22972 break;
22973
22974 case DW_OP_constu:
22975 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22976 &bytes_read);
22977 i += bytes_read;
22978 break;
22979
22980 case DW_OP_consts:
22981 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22982 i += bytes_read;
22983 break;
22984
22985 case DW_OP_dup:
22986 stack[stacki + 1] = stack[stacki];
22987 stacki++;
22988 break;
22989
22990 case DW_OP_plus:
22991 stack[stacki - 1] += stack[stacki];
22992 stacki--;
22993 break;
22994
22995 case DW_OP_plus_uconst:
22996 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22997 &bytes_read);
22998 i += bytes_read;
22999 break;
23000
23001 case DW_OP_minus:
23002 stack[stacki - 1] -= stack[stacki];
23003 stacki--;
23004 break;
23005
23006 case DW_OP_deref:
23007 /* If we're not the last op, then we definitely can't encode
23008 this using GDB's address_class enum. This is valid for partial
23009 global symbols, although the variable's address will be bogus
23010 in the psymtab. */
23011 if (i < size)
23012 {
23013 if (computed == nullptr)
23014 dwarf2_complex_location_expr_complaint ();
23015 else
23016 return 0;
23017 }
23018 break;
23019
23020 case DW_OP_GNU_push_tls_address:
23021 case DW_OP_form_tls_address:
23022 /* The top of the stack has the offset from the beginning
23023 of the thread control block at which the variable is located. */
23024 /* Nothing should follow this operator, so the top of stack would
23025 be returned. */
23026 /* This is valid for partial global symbols, but the variable's
23027 address will be bogus in the psymtab. Make it always at least
23028 non-zero to not look as a variable garbage collected by linker
23029 which have DW_OP_addr 0. */
23030 if (i < size)
23031 {
23032 if (computed == nullptr)
23033 dwarf2_complex_location_expr_complaint ();
23034 else
23035 return 0;
23036 }
23037 stack[stacki]++;
23038 break;
23039
23040 case DW_OP_GNU_uninit:
23041 if (computed != nullptr)
23042 return 0;
23043 break;
23044
23045 case DW_OP_addrx:
23046 case DW_OP_GNU_addr_index:
23047 case DW_OP_GNU_const_index:
23048 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23049 &bytes_read);
23050 i += bytes_read;
23051 break;
23052
23053 default:
23054 if (computed == nullptr)
23055 {
23056 const char *name = get_DW_OP_name (op);
23057
23058 if (name)
23059 complaint (_("unsupported stack op: '%s'"),
23060 name);
23061 else
23062 complaint (_("unsupported stack op: '%02x'"),
23063 op);
23064 }
23065
23066 return (stack[stacki]);
23067 }
23068
23069 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23070 outside of the allocated space. Also enforce minimum>0. */
23071 if (stacki >= ARRAY_SIZE (stack) - 1)
23072 {
23073 if (computed == nullptr)
23074 complaint (_("location description stack overflow"));
23075 return 0;
23076 }
23077
23078 if (stacki <= 0)
23079 {
23080 if (computed == nullptr)
23081 complaint (_("location description stack underflow"));
23082 return 0;
23083 }
23084 }
23085
23086 if (computed != nullptr)
23087 *computed = true;
23088 return (stack[stacki]);
23089 }
23090
23091 /* memory allocation interface */
23092
23093 static struct dwarf_block *
23094 dwarf_alloc_block (struct dwarf2_cu *cu)
23095 {
23096 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23097 }
23098
23099 static struct die_info *
23100 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23101 {
23102 struct die_info *die;
23103 size_t size = sizeof (struct die_info);
23104
23105 if (num_attrs > 1)
23106 size += (num_attrs - 1) * sizeof (struct attribute);
23107
23108 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23109 memset (die, 0, sizeof (struct die_info));
23110 return (die);
23111 }
23112
23113 \f
23114
23115 /* Macro support. */
23116
23117 /* An overload of dwarf_decode_macros that finds the correct section
23118 and ensures it is read in before calling the other overload. */
23119
23120 static void
23121 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23122 int section_is_gnu)
23123 {
23124 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23125 struct objfile *objfile = dwarf2_per_objfile->objfile;
23126 const struct line_header *lh = cu->line_header;
23127 unsigned int offset_size = cu->header.offset_size;
23128 struct dwarf2_section_info *section;
23129 const char *section_name;
23130
23131 if (cu->dwo_unit != nullptr)
23132 {
23133 if (section_is_gnu)
23134 {
23135 section = &cu->dwo_unit->dwo_file->sections.macro;
23136 section_name = ".debug_macro.dwo";
23137 }
23138 else
23139 {
23140 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23141 section_name = ".debug_macinfo.dwo";
23142 }
23143 }
23144 else
23145 {
23146 if (section_is_gnu)
23147 {
23148 section = &dwarf2_per_objfile->per_bfd->macro;
23149 section_name = ".debug_macro";
23150 }
23151 else
23152 {
23153 section = &dwarf2_per_objfile->per_bfd->macinfo;
23154 section_name = ".debug_macinfo";
23155 }
23156 }
23157
23158 section->read (objfile);
23159 if (section->buffer == nullptr)
23160 {
23161 complaint (_("missing %s section"), section_name);
23162 return;
23163 }
23164
23165 buildsym_compunit *builder = cu->get_builder ();
23166
23167 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23168 offset_size, offset, section_is_gnu);
23169 }
23170
23171 /* Return the .debug_loc section to use for CU.
23172 For DWO files use .debug_loc.dwo. */
23173
23174 static struct dwarf2_section_info *
23175 cu_debug_loc_section (struct dwarf2_cu *cu)
23176 {
23177 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23178
23179 if (cu->dwo_unit)
23180 {
23181 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23182
23183 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23184 }
23185 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23186 : &dwarf2_per_objfile->per_bfd->loc);
23187 }
23188
23189 /* A helper function that fills in a dwarf2_loclist_baton. */
23190
23191 static void
23192 fill_in_loclist_baton (struct dwarf2_cu *cu,
23193 struct dwarf2_loclist_baton *baton,
23194 const struct attribute *attr)
23195 {
23196 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23197 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23198
23199 section->read (dwarf2_per_objfile->objfile);
23200
23201 baton->per_objfile = dwarf2_per_objfile;
23202 baton->per_cu = cu->per_cu;
23203 gdb_assert (baton->per_cu);
23204 /* We don't know how long the location list is, but make sure we
23205 don't run off the edge of the section. */
23206 baton->size = section->size - DW_UNSND (attr);
23207 baton->data = section->buffer + DW_UNSND (attr);
23208 if (cu->base_address.has_value ())
23209 baton->base_address = *cu->base_address;
23210 else
23211 baton->base_address = 0;
23212 baton->from_dwo = cu->dwo_unit != NULL;
23213 }
23214
23215 static void
23216 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23217 struct dwarf2_cu *cu, int is_block)
23218 {
23219 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23220 struct objfile *objfile = dwarf2_per_objfile->objfile;
23221 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23222
23223 if (attr->form_is_section_offset ()
23224 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23225 the section. If so, fall through to the complaint in the
23226 other branch. */
23227 && DW_UNSND (attr) < section->get_size (objfile))
23228 {
23229 struct dwarf2_loclist_baton *baton;
23230
23231 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23232
23233 fill_in_loclist_baton (cu, baton, attr);
23234
23235 if (!cu->base_address.has_value ())
23236 complaint (_("Location list used without "
23237 "specifying the CU base address."));
23238
23239 SYMBOL_ACLASS_INDEX (sym) = (is_block
23240 ? dwarf2_loclist_block_index
23241 : dwarf2_loclist_index);
23242 SYMBOL_LOCATION_BATON (sym) = baton;
23243 }
23244 else
23245 {
23246 struct dwarf2_locexpr_baton *baton;
23247
23248 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23249 baton->per_objfile = dwarf2_per_objfile;
23250 baton->per_cu = cu->per_cu;
23251 gdb_assert (baton->per_cu);
23252
23253 if (attr->form_is_block ())
23254 {
23255 /* Note that we're just copying the block's data pointer
23256 here, not the actual data. We're still pointing into the
23257 info_buffer for SYM's objfile; right now we never release
23258 that buffer, but when we do clean up properly this may
23259 need to change. */
23260 baton->size = DW_BLOCK (attr)->size;
23261 baton->data = DW_BLOCK (attr)->data;
23262 }
23263 else
23264 {
23265 dwarf2_invalid_attrib_class_complaint ("location description",
23266 sym->natural_name ());
23267 baton->size = 0;
23268 }
23269
23270 SYMBOL_ACLASS_INDEX (sym) = (is_block
23271 ? dwarf2_locexpr_block_index
23272 : dwarf2_locexpr_index);
23273 SYMBOL_LOCATION_BATON (sym) = baton;
23274 }
23275 }
23276
23277 /* See read.h. */
23278
23279 struct objfile *
23280 dwarf2_per_cu_data::objfile () const
23281 {
23282 struct objfile *objfile = dwarf2_per_objfile->objfile;
23283
23284 /* Return the master objfile, so that we can report and look up the
23285 correct file containing this variable. */
23286 if (objfile->separate_debug_objfile_backlink)
23287 objfile = objfile->separate_debug_objfile_backlink;
23288
23289 return objfile;
23290 }
23291
23292 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23293 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23294 CU_HEADERP first. */
23295
23296 static const struct comp_unit_head *
23297 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23298 const struct dwarf2_per_cu_data *per_cu)
23299 {
23300 const gdb_byte *info_ptr;
23301
23302 if (per_cu->cu)
23303 return &per_cu->cu->header;
23304
23305 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
23306
23307 memset (cu_headerp, 0, sizeof (*cu_headerp));
23308 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23309 rcuh_kind::COMPILE);
23310
23311 return cu_headerp;
23312 }
23313
23314 /* See read.h. */
23315
23316 int
23317 dwarf2_per_cu_data::addr_size () const
23318 {
23319 struct comp_unit_head cu_header_local;
23320 const struct comp_unit_head *cu_headerp;
23321
23322 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23323
23324 return cu_headerp->addr_size;
23325 }
23326
23327 /* See read.h. */
23328
23329 int
23330 dwarf2_per_cu_data::offset_size () const
23331 {
23332 struct comp_unit_head cu_header_local;
23333 const struct comp_unit_head *cu_headerp;
23334
23335 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23336
23337 return cu_headerp->offset_size;
23338 }
23339
23340 /* See read.h. */
23341
23342 int
23343 dwarf2_per_cu_data::ref_addr_size () const
23344 {
23345 struct comp_unit_head cu_header_local;
23346 const struct comp_unit_head *cu_headerp;
23347
23348 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23349
23350 if (cu_headerp->version == 2)
23351 return cu_headerp->addr_size;
23352 else
23353 return cu_headerp->offset_size;
23354 }
23355
23356 /* See read.h. */
23357
23358 CORE_ADDR
23359 dwarf2_per_cu_data::text_offset () const
23360 {
23361 struct objfile *objfile = dwarf2_per_objfile->objfile;
23362
23363 return objfile->text_section_offset ();
23364 }
23365
23366 /* See read.h. */
23367
23368 struct type *
23369 dwarf2_per_cu_data::addr_type () const
23370 {
23371 struct objfile *objfile = dwarf2_per_objfile->objfile;
23372 struct type *void_type = objfile_type (objfile)->builtin_void;
23373 struct type *addr_type = lookup_pointer_type (void_type);
23374 int addr_size = this->addr_size ();
23375
23376 if (TYPE_LENGTH (addr_type) == addr_size)
23377 return addr_type;
23378
23379 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23380 return addr_type;
23381 }
23382
23383 /* A helper function for dwarf2_find_containing_comp_unit that returns
23384 the index of the result, and that searches a vector. It will
23385 return a result even if the offset in question does not actually
23386 occur in any CU. This is separate so that it can be unit
23387 tested. */
23388
23389 static int
23390 dwarf2_find_containing_comp_unit
23391 (sect_offset sect_off,
23392 unsigned int offset_in_dwz,
23393 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23394 {
23395 int low, high;
23396
23397 low = 0;
23398 high = all_comp_units.size () - 1;
23399 while (high > low)
23400 {
23401 struct dwarf2_per_cu_data *mid_cu;
23402 int mid = low + (high - low) / 2;
23403
23404 mid_cu = all_comp_units[mid];
23405 if (mid_cu->is_dwz > offset_in_dwz
23406 || (mid_cu->is_dwz == offset_in_dwz
23407 && mid_cu->sect_off + mid_cu->length > sect_off))
23408 high = mid;
23409 else
23410 low = mid + 1;
23411 }
23412 gdb_assert (low == high);
23413 return low;
23414 }
23415
23416 /* Locate the .debug_info compilation unit from CU's objfile which contains
23417 the DIE at OFFSET. Raises an error on failure. */
23418
23419 static struct dwarf2_per_cu_data *
23420 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23421 unsigned int offset_in_dwz,
23422 struct dwarf2_per_objfile *dwarf2_per_objfile)
23423 {
23424 int low
23425 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23426 dwarf2_per_objfile->per_bfd->all_comp_units);
23427 struct dwarf2_per_cu_data *this_cu
23428 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
23429
23430 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23431 {
23432 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23433 error (_("Dwarf Error: could not find partial DIE containing "
23434 "offset %s [in module %s]"),
23435 sect_offset_str (sect_off),
23436 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23437
23438 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
23439 <= sect_off);
23440 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
23441 }
23442 else
23443 {
23444 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
23445 && sect_off >= this_cu->sect_off + this_cu->length)
23446 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23447 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23448 return this_cu;
23449 }
23450 }
23451
23452 #if GDB_SELF_TEST
23453
23454 namespace selftests {
23455 namespace find_containing_comp_unit {
23456
23457 static void
23458 run_test ()
23459 {
23460 struct dwarf2_per_cu_data one {};
23461 struct dwarf2_per_cu_data two {};
23462 struct dwarf2_per_cu_data three {};
23463 struct dwarf2_per_cu_data four {};
23464
23465 one.length = 5;
23466 two.sect_off = sect_offset (one.length);
23467 two.length = 7;
23468
23469 three.length = 5;
23470 three.is_dwz = 1;
23471 four.sect_off = sect_offset (three.length);
23472 four.length = 7;
23473 four.is_dwz = 1;
23474
23475 std::vector<dwarf2_per_cu_data *> units;
23476 units.push_back (&one);
23477 units.push_back (&two);
23478 units.push_back (&three);
23479 units.push_back (&four);
23480
23481 int result;
23482
23483 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23484 SELF_CHECK (units[result] == &one);
23485 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23486 SELF_CHECK (units[result] == &one);
23487 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23488 SELF_CHECK (units[result] == &two);
23489
23490 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23491 SELF_CHECK (units[result] == &three);
23492 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23493 SELF_CHECK (units[result] == &three);
23494 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23495 SELF_CHECK (units[result] == &four);
23496 }
23497
23498 }
23499 }
23500
23501 #endif /* GDB_SELF_TEST */
23502
23503 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
23504
23505 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23506 dwarf2_per_objfile *per_objfile)
23507 : per_cu (per_cu),
23508 per_objfile (per_objfile),
23509 mark (false),
23510 has_loclist (false),
23511 checked_producer (false),
23512 producer_is_gxx_lt_4_6 (false),
23513 producer_is_gcc_lt_4_3 (false),
23514 producer_is_icc (false),
23515 producer_is_icc_lt_14 (false),
23516 producer_is_codewarrior (false),
23517 processing_has_namespace_info (false)
23518 {
23519 per_cu->cu = this;
23520 }
23521
23522 /* Destroy a dwarf2_cu. */
23523
23524 dwarf2_cu::~dwarf2_cu ()
23525 {
23526 per_cu->cu = NULL;
23527 }
23528
23529 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23530
23531 static void
23532 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23533 enum language pretend_language)
23534 {
23535 struct attribute *attr;
23536
23537 /* Set the language we're debugging. */
23538 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23539 if (attr != nullptr)
23540 set_cu_language (DW_UNSND (attr), cu);
23541 else
23542 {
23543 cu->language = pretend_language;
23544 cu->language_defn = language_def (cu->language);
23545 }
23546
23547 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23548 }
23549
23550 /* Increase the age counter on each cached compilation unit, and free
23551 any that are too old. */
23552
23553 static void
23554 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23555 {
23556 struct dwarf2_per_cu_data *per_cu, **last_chain;
23557
23558 dwarf2_clear_marks (dwarf2_per_objfile->per_bfd->read_in_chain);
23559 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23560 while (per_cu != NULL)
23561 {
23562 per_cu->cu->last_used ++;
23563 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23564 dwarf2_mark (per_cu->cu);
23565 per_cu = per_cu->cu->read_in_chain;
23566 }
23567
23568 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23569 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23570 while (per_cu != NULL)
23571 {
23572 struct dwarf2_per_cu_data *next_cu;
23573
23574 next_cu = per_cu->cu->read_in_chain;
23575
23576 if (!per_cu->cu->mark)
23577 {
23578 delete per_cu->cu;
23579 *last_chain = next_cu;
23580 }
23581 else
23582 last_chain = &per_cu->cu->read_in_chain;
23583
23584 per_cu = next_cu;
23585 }
23586 }
23587
23588 /* Remove a single compilation unit from the cache. */
23589
23590 static void
23591 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23592 {
23593 struct dwarf2_per_cu_data *per_cu, **last_chain;
23594 struct dwarf2_per_objfile *dwarf2_per_objfile
23595 = target_per_cu->dwarf2_per_objfile;
23596
23597 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23598 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23599 while (per_cu != NULL)
23600 {
23601 struct dwarf2_per_cu_data *next_cu;
23602
23603 next_cu = per_cu->cu->read_in_chain;
23604
23605 if (per_cu == target_per_cu)
23606 {
23607 delete per_cu->cu;
23608 per_cu->cu = NULL;
23609 *last_chain = next_cu;
23610 break;
23611 }
23612 else
23613 last_chain = &per_cu->cu->read_in_chain;
23614
23615 per_cu = next_cu;
23616 }
23617 }
23618
23619 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23620 We store these in a hash table separate from the DIEs, and preserve them
23621 when the DIEs are flushed out of cache.
23622
23623 The CU "per_cu" pointer is needed because offset alone is not enough to
23624 uniquely identify the type. A file may have multiple .debug_types sections,
23625 or the type may come from a DWO file. Furthermore, while it's more logical
23626 to use per_cu->section+offset, with Fission the section with the data is in
23627 the DWO file but we don't know that section at the point we need it.
23628 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23629 because we can enter the lookup routine, get_die_type_at_offset, from
23630 outside this file, and thus won't necessarily have PER_CU->cu.
23631 Fortunately, PER_CU is stable for the life of the objfile. */
23632
23633 struct dwarf2_per_cu_offset_and_type
23634 {
23635 const struct dwarf2_per_cu_data *per_cu;
23636 sect_offset sect_off;
23637 struct type *type;
23638 };
23639
23640 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23641
23642 static hashval_t
23643 per_cu_offset_and_type_hash (const void *item)
23644 {
23645 const struct dwarf2_per_cu_offset_and_type *ofs
23646 = (const struct dwarf2_per_cu_offset_and_type *) item;
23647
23648 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23649 }
23650
23651 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23652
23653 static int
23654 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23655 {
23656 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23657 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23658 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23659 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23660
23661 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23662 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23663 }
23664
23665 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23666 table if necessary. For convenience, return TYPE.
23667
23668 The DIEs reading must have careful ordering to:
23669 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23670 reading current DIE.
23671 * Not trying to dereference contents of still incompletely read in types
23672 while reading in other DIEs.
23673 * Enable referencing still incompletely read in types just by a pointer to
23674 the type without accessing its fields.
23675
23676 Therefore caller should follow these rules:
23677 * Try to fetch any prerequisite types we may need to build this DIE type
23678 before building the type and calling set_die_type.
23679 * After building type call set_die_type for current DIE as soon as
23680 possible before fetching more types to complete the current type.
23681 * Make the type as complete as possible before fetching more types. */
23682
23683 static struct type *
23684 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23685 {
23686 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23687 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23688 struct objfile *objfile = dwarf2_per_objfile->objfile;
23689 struct attribute *attr;
23690 struct dynamic_prop prop;
23691
23692 /* For Ada types, make sure that the gnat-specific data is always
23693 initialized (if not already set). There are a few types where
23694 we should not be doing so, because the type-specific area is
23695 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23696 where the type-specific area is used to store the floatformat).
23697 But this is not a problem, because the gnat-specific information
23698 is actually not needed for these types. */
23699 if (need_gnat_info (cu)
23700 && type->code () != TYPE_CODE_FUNC
23701 && type->code () != TYPE_CODE_FLT
23702 && type->code () != TYPE_CODE_METHODPTR
23703 && type->code () != TYPE_CODE_MEMBERPTR
23704 && type->code () != TYPE_CODE_METHOD
23705 && !HAVE_GNAT_AUX_INFO (type))
23706 INIT_GNAT_SPECIFIC (type);
23707
23708 /* Read DW_AT_allocated and set in type. */
23709 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23710 if (attr != NULL && attr->form_is_block ())
23711 {
23712 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23713 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23714 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23715 }
23716 else if (attr != NULL)
23717 {
23718 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23719 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23720 sect_offset_str (die->sect_off));
23721 }
23722
23723 /* Read DW_AT_associated and set in type. */
23724 attr = dwarf2_attr (die, DW_AT_associated, cu);
23725 if (attr != NULL && attr->form_is_block ())
23726 {
23727 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23728 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23729 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23730 }
23731 else if (attr != NULL)
23732 {
23733 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23734 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23735 sect_offset_str (die->sect_off));
23736 }
23737
23738 /* Read DW_AT_data_location and set in type. */
23739 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23740 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23741 cu->per_cu->addr_type ()))
23742 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23743
23744 if (dwarf2_per_objfile->die_type_hash == NULL)
23745 dwarf2_per_objfile->die_type_hash
23746 = htab_up (htab_create_alloc (127,
23747 per_cu_offset_and_type_hash,
23748 per_cu_offset_and_type_eq,
23749 NULL, xcalloc, xfree));
23750
23751 ofs.per_cu = cu->per_cu;
23752 ofs.sect_off = die->sect_off;
23753 ofs.type = type;
23754 slot = (struct dwarf2_per_cu_offset_and_type **)
23755 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23756 if (*slot)
23757 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23758 sect_offset_str (die->sect_off));
23759 *slot = XOBNEW (&objfile->objfile_obstack,
23760 struct dwarf2_per_cu_offset_and_type);
23761 **slot = ofs;
23762 return type;
23763 }
23764
23765 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23766 or return NULL if the die does not have a saved type. */
23767
23768 static struct type *
23769 get_die_type_at_offset (sect_offset sect_off,
23770 struct dwarf2_per_cu_data *per_cu)
23771 {
23772 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23773 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23774
23775 if (dwarf2_per_objfile->die_type_hash == NULL)
23776 return NULL;
23777
23778 ofs.per_cu = per_cu;
23779 ofs.sect_off = sect_off;
23780 slot = ((struct dwarf2_per_cu_offset_and_type *)
23781 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23782 if (slot)
23783 return slot->type;
23784 else
23785 return NULL;
23786 }
23787
23788 /* Look up the type for DIE in CU in die_type_hash,
23789 or return NULL if DIE does not have a saved type. */
23790
23791 static struct type *
23792 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23793 {
23794 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23795 }
23796
23797 /* Add a dependence relationship from CU to REF_PER_CU. */
23798
23799 static void
23800 dwarf2_add_dependence (struct dwarf2_cu *cu,
23801 struct dwarf2_per_cu_data *ref_per_cu)
23802 {
23803 void **slot;
23804
23805 if (cu->dependencies == NULL)
23806 cu->dependencies
23807 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23808 NULL, &cu->comp_unit_obstack,
23809 hashtab_obstack_allocate,
23810 dummy_obstack_deallocate);
23811
23812 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23813 if (*slot == NULL)
23814 *slot = ref_per_cu;
23815 }
23816
23817 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23818 Set the mark field in every compilation unit in the
23819 cache that we must keep because we are keeping CU. */
23820
23821 static int
23822 dwarf2_mark_helper (void **slot, void *data)
23823 {
23824 struct dwarf2_per_cu_data *per_cu;
23825
23826 per_cu = (struct dwarf2_per_cu_data *) *slot;
23827
23828 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23829 reading of the chain. As such dependencies remain valid it is not much
23830 useful to track and undo them during QUIT cleanups. */
23831 if (per_cu->cu == NULL)
23832 return 1;
23833
23834 if (per_cu->cu->mark)
23835 return 1;
23836 per_cu->cu->mark = true;
23837
23838 if (per_cu->cu->dependencies != NULL)
23839 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23840
23841 return 1;
23842 }
23843
23844 /* Set the mark field in CU and in every other compilation unit in the
23845 cache that we must keep because we are keeping CU. */
23846
23847 static void
23848 dwarf2_mark (struct dwarf2_cu *cu)
23849 {
23850 if (cu->mark)
23851 return;
23852 cu->mark = true;
23853 if (cu->dependencies != NULL)
23854 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23855 }
23856
23857 static void
23858 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23859 {
23860 while (per_cu)
23861 {
23862 per_cu->cu->mark = false;
23863 per_cu = per_cu->cu->read_in_chain;
23864 }
23865 }
23866
23867 /* Trivial hash function for partial_die_info: the hash value of a DIE
23868 is its offset in .debug_info for this objfile. */
23869
23870 static hashval_t
23871 partial_die_hash (const void *item)
23872 {
23873 const struct partial_die_info *part_die
23874 = (const struct partial_die_info *) item;
23875
23876 return to_underlying (part_die->sect_off);
23877 }
23878
23879 /* Trivial comparison function for partial_die_info structures: two DIEs
23880 are equal if they have the same offset. */
23881
23882 static int
23883 partial_die_eq (const void *item_lhs, const void *item_rhs)
23884 {
23885 const struct partial_die_info *part_die_lhs
23886 = (const struct partial_die_info *) item_lhs;
23887 const struct partial_die_info *part_die_rhs
23888 = (const struct partial_die_info *) item_rhs;
23889
23890 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23891 }
23892
23893 struct cmd_list_element *set_dwarf_cmdlist;
23894 struct cmd_list_element *show_dwarf_cmdlist;
23895
23896 static void
23897 show_check_physname (struct ui_file *file, int from_tty,
23898 struct cmd_list_element *c, const char *value)
23899 {
23900 fprintf_filtered (file,
23901 _("Whether to check \"physname\" is %s.\n"),
23902 value);
23903 }
23904
23905 void _initialize_dwarf2_read ();
23906 void
23907 _initialize_dwarf2_read ()
23908 {
23909 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
23910 Set DWARF specific variables.\n\
23911 Configure DWARF variables such as the cache size."),
23912 &set_dwarf_cmdlist, "maintenance set dwarf ",
23913 0/*allow-unknown*/, &maintenance_set_cmdlist);
23914
23915 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
23916 Show DWARF specific variables.\n\
23917 Show DWARF variables such as the cache size."),
23918 &show_dwarf_cmdlist, "maintenance show dwarf ",
23919 0/*allow-unknown*/, &maintenance_show_cmdlist);
23920
23921 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23922 &dwarf_max_cache_age, _("\
23923 Set the upper bound on the age of cached DWARF compilation units."), _("\
23924 Show the upper bound on the age of cached DWARF compilation units."), _("\
23925 A higher limit means that cached compilation units will be stored\n\
23926 in memory longer, and more total memory will be used. Zero disables\n\
23927 caching, which can slow down startup."),
23928 NULL,
23929 show_dwarf_max_cache_age,
23930 &set_dwarf_cmdlist,
23931 &show_dwarf_cmdlist);
23932
23933 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23934 Set debugging of the DWARF reader."), _("\
23935 Show debugging of the DWARF reader."), _("\
23936 When enabled (non-zero), debugging messages are printed during DWARF\n\
23937 reading and symtab expansion. A value of 1 (one) provides basic\n\
23938 information. A value greater than 1 provides more verbose information."),
23939 NULL,
23940 NULL,
23941 &setdebuglist, &showdebuglist);
23942
23943 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23944 Set debugging of the DWARF DIE reader."), _("\
23945 Show debugging of the DWARF DIE reader."), _("\
23946 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23947 The value is the maximum depth to print."),
23948 NULL,
23949 NULL,
23950 &setdebuglist, &showdebuglist);
23951
23952 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23953 Set debugging of the dwarf line reader."), _("\
23954 Show debugging of the dwarf line reader."), _("\
23955 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23956 A value of 1 (one) provides basic information.\n\
23957 A value greater than 1 provides more verbose information."),
23958 NULL,
23959 NULL,
23960 &setdebuglist, &showdebuglist);
23961
23962 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23963 Set cross-checking of \"physname\" code against demangler."), _("\
23964 Show cross-checking of \"physname\" code against demangler."), _("\
23965 When enabled, GDB's internal \"physname\" code is checked against\n\
23966 the demangler."),
23967 NULL, show_check_physname,
23968 &setdebuglist, &showdebuglist);
23969
23970 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23971 no_class, &use_deprecated_index_sections, _("\
23972 Set whether to use deprecated gdb_index sections."), _("\
23973 Show whether to use deprecated gdb_index sections."), _("\
23974 When enabled, deprecated .gdb_index sections are used anyway.\n\
23975 Normally they are ignored either because of a missing feature or\n\
23976 performance issue.\n\
23977 Warning: This option must be enabled before gdb reads the file."),
23978 NULL,
23979 NULL,
23980 &setlist, &showlist);
23981
23982 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23983 &dwarf2_locexpr_funcs);
23984 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23985 &dwarf2_loclist_funcs);
23986
23987 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23988 &dwarf2_block_frame_base_locexpr_funcs);
23989 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23990 &dwarf2_block_frame_base_loclist_funcs);
23991
23992 #if GDB_SELF_TEST
23993 selftests::register_test ("dw2_expand_symtabs_matching",
23994 selftests::dw2_expand_symtabs_matching::run_test);
23995 selftests::register_test ("dwarf2_find_containing_comp_unit",
23996 selftests::find_containing_comp_unit::run_test);
23997 #endif
23998 }
This page took 0.515056 seconds and 5 git commands to generate.