]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
Remove reference to dwarf2_per_cu_data::dwarf2_per_objfile in queue_and_load_all_dwo_tus
[thirdparty/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "dwarf2/stringify.h"
44 #include "bfd.h"
45 #include "elf-bfd.h"
46 #include "symtab.h"
47 #include "gdbtypes.h"
48 #include "objfiles.h"
49 #include "dwarf2.h"
50 #include "buildsym.h"
51 #include "demangle.h"
52 #include "gdb-demangle.h"
53 #include "filenames.h" /* for DOSish file names */
54 #include "language.h"
55 #include "complaints.h"
56 #include "dwarf2/expr.h"
57 #include "dwarf2/loc.h"
58 #include "cp-support.h"
59 #include "hashtab.h"
60 #include "command.h"
61 #include "gdbcmd.h"
62 #include "block.h"
63 #include "addrmap.h"
64 #include "typeprint.h"
65 #include "psympriv.h"
66 #include "c-lang.h"
67 #include "go-lang.h"
68 #include "valprint.h"
69 #include "gdbcore.h" /* for gnutarget */
70 #include "gdb/gdb-index.h"
71 #include "gdb_bfd.h"
72 #include "f-lang.h"
73 #include "source.h"
74 #include "build-id.h"
75 #include "namespace.h"
76 #include "gdbsupport/function-view.h"
77 #include "gdbsupport/gdb_optional.h"
78 #include "gdbsupport/underlying.h"
79 #include "gdbsupport/hash_enum.h"
80 #include "filename-seen-cache.h"
81 #include "producer.h"
82 #include <fcntl.h>
83 #include <algorithm>
84 #include <unordered_map>
85 #include "gdbsupport/selftest.h"
86 #include "rust-lang.h"
87 #include "gdbsupport/pathstuff.h"
88 #include "count-one-bits.h"
89 #include "debuginfod-support.h"
90
91 /* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94 static unsigned int dwarf_read_debug = 0;
95
96 /* When non-zero, dump DIEs after they are read in. */
97 static unsigned int dwarf_die_debug = 0;
98
99 /* When non-zero, dump line number entries as they are read in. */
100 unsigned int dwarf_line_debug = 0;
101
102 /* When true, cross-check physname against demangler. */
103 static bool check_physname = false;
104
105 /* When true, do not reject deprecated .gdb_index sections. */
106 static bool use_deprecated_index_sections = false;
107
108 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
109
110 /* The "aclass" indices for various kinds of computed DWARF symbols. */
111
112 static int dwarf2_locexpr_index;
113 static int dwarf2_loclist_index;
114 static int dwarf2_locexpr_block_index;
115 static int dwarf2_loclist_block_index;
116
117 /* Size of .debug_loclists section header for 32-bit DWARF format. */
118 #define LOCLIST_HEADER_SIZE32 12
119
120 /* Size of .debug_loclists section header for 64-bit DWARF format. */
121 #define LOCLIST_HEADER_SIZE64 20
122
123 /* An index into a (C++) symbol name component in a symbol name as
124 recorded in the mapped_index's symbol table. For each C++ symbol
125 in the symbol table, we record one entry for the start of each
126 component in the symbol in a table of name components, and then
127 sort the table, in order to be able to binary search symbol names,
128 ignoring leading namespaces, both completion and regular look up.
129 For example, for symbol "A::B::C", we'll have an entry that points
130 to "A::B::C", another that points to "B::C", and another for "C".
131 Note that function symbols in GDB index have no parameter
132 information, just the function/method names. You can convert a
133 name_component to a "const char *" using the
134 'mapped_index::symbol_name_at(offset_type)' method. */
135
136 struct name_component
137 {
138 /* Offset in the symbol name where the component starts. Stored as
139 a (32-bit) offset instead of a pointer to save memory and improve
140 locality on 64-bit architectures. */
141 offset_type name_offset;
142
143 /* The symbol's index in the symbol and constant pool tables of a
144 mapped_index. */
145 offset_type idx;
146 };
147
148 /* Base class containing bits shared by both .gdb_index and
149 .debug_name indexes. */
150
151 struct mapped_index_base
152 {
153 mapped_index_base () = default;
154 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
155
156 /* The name_component table (a sorted vector). See name_component's
157 description above. */
158 std::vector<name_component> name_components;
159
160 /* How NAME_COMPONENTS is sorted. */
161 enum case_sensitivity name_components_casing;
162
163 /* Return the number of names in the symbol table. */
164 virtual size_t symbol_name_count () const = 0;
165
166 /* Get the name of the symbol at IDX in the symbol table. */
167 virtual const char *symbol_name_at (offset_type idx) const = 0;
168
169 /* Return whether the name at IDX in the symbol table should be
170 ignored. */
171 virtual bool symbol_name_slot_invalid (offset_type idx) const
172 {
173 return false;
174 }
175
176 /* Build the symbol name component sorted vector, if we haven't
177 yet. */
178 void build_name_components ();
179
180 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
181 possible matches for LN_NO_PARAMS in the name component
182 vector. */
183 std::pair<std::vector<name_component>::const_iterator,
184 std::vector<name_component>::const_iterator>
185 find_name_components_bounds (const lookup_name_info &ln_no_params,
186 enum language lang) const;
187
188 /* Prevent deleting/destroying via a base class pointer. */
189 protected:
190 ~mapped_index_base() = default;
191 };
192
193 /* A description of the mapped index. The file format is described in
194 a comment by the code that writes the index. */
195 struct mapped_index final : public mapped_index_base
196 {
197 /* A slot/bucket in the symbol table hash. */
198 struct symbol_table_slot
199 {
200 const offset_type name;
201 const offset_type vec;
202 };
203
204 /* Index data format version. */
205 int version = 0;
206
207 /* The address table data. */
208 gdb::array_view<const gdb_byte> address_table;
209
210 /* The symbol table, implemented as a hash table. */
211 gdb::array_view<symbol_table_slot> symbol_table;
212
213 /* A pointer to the constant pool. */
214 const char *constant_pool = nullptr;
215
216 bool symbol_name_slot_invalid (offset_type idx) const override
217 {
218 const auto &bucket = this->symbol_table[idx];
219 return bucket.name == 0 && bucket.vec == 0;
220 }
221
222 /* Convenience method to get at the name of the symbol at IDX in the
223 symbol table. */
224 const char *symbol_name_at (offset_type idx) const override
225 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
226
227 size_t symbol_name_count () const override
228 { return this->symbol_table.size (); }
229 };
230
231 /* A description of the mapped .debug_names.
232 Uninitialized map has CU_COUNT 0. */
233 struct mapped_debug_names final : public mapped_index_base
234 {
235 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
236 : dwarf2_per_objfile (dwarf2_per_objfile_)
237 {}
238
239 struct dwarf2_per_objfile *dwarf2_per_objfile;
240 bfd_endian dwarf5_byte_order;
241 bool dwarf5_is_dwarf64;
242 bool augmentation_is_gdb;
243 uint8_t offset_size;
244 uint32_t cu_count = 0;
245 uint32_t tu_count, bucket_count, name_count;
246 const gdb_byte *cu_table_reordered, *tu_table_reordered;
247 const uint32_t *bucket_table_reordered, *hash_table_reordered;
248 const gdb_byte *name_table_string_offs_reordered;
249 const gdb_byte *name_table_entry_offs_reordered;
250 const gdb_byte *entry_pool;
251
252 struct index_val
253 {
254 ULONGEST dwarf_tag;
255 struct attr
256 {
257 /* Attribute name DW_IDX_*. */
258 ULONGEST dw_idx;
259
260 /* Attribute form DW_FORM_*. */
261 ULONGEST form;
262
263 /* Value if FORM is DW_FORM_implicit_const. */
264 LONGEST implicit_const;
265 };
266 std::vector<attr> attr_vec;
267 };
268
269 std::unordered_map<ULONGEST, index_val> abbrev_map;
270
271 const char *namei_to_name (uint32_t namei) const;
272
273 /* Implementation of the mapped_index_base virtual interface, for
274 the name_components cache. */
275
276 const char *symbol_name_at (offset_type idx) const override
277 { return namei_to_name (idx); }
278
279 size_t symbol_name_count () const override
280 { return this->name_count; }
281 };
282
283 /* See dwarf2read.h. */
284
285 dwarf2_per_objfile *
286 get_dwarf2_per_objfile (struct objfile *objfile)
287 {
288 return dwarf2_objfile_data_key.get (objfile);
289 }
290
291 /* Default names of the debugging sections. */
292
293 /* Note that if the debugging section has been compressed, it might
294 have a name like .zdebug_info. */
295
296 static const struct dwarf2_debug_sections dwarf2_elf_names =
297 {
298 { ".debug_info", ".zdebug_info" },
299 { ".debug_abbrev", ".zdebug_abbrev" },
300 { ".debug_line", ".zdebug_line" },
301 { ".debug_loc", ".zdebug_loc" },
302 { ".debug_loclists", ".zdebug_loclists" },
303 { ".debug_macinfo", ".zdebug_macinfo" },
304 { ".debug_macro", ".zdebug_macro" },
305 { ".debug_str", ".zdebug_str" },
306 { ".debug_str_offsets", ".zdebug_str_offsets" },
307 { ".debug_line_str", ".zdebug_line_str" },
308 { ".debug_ranges", ".zdebug_ranges" },
309 { ".debug_rnglists", ".zdebug_rnglists" },
310 { ".debug_types", ".zdebug_types" },
311 { ".debug_addr", ".zdebug_addr" },
312 { ".debug_frame", ".zdebug_frame" },
313 { ".eh_frame", NULL },
314 { ".gdb_index", ".zgdb_index" },
315 { ".debug_names", ".zdebug_names" },
316 { ".debug_aranges", ".zdebug_aranges" },
317 23
318 };
319
320 /* List of DWO/DWP sections. */
321
322 static const struct dwop_section_names
323 {
324 struct dwarf2_section_names abbrev_dwo;
325 struct dwarf2_section_names info_dwo;
326 struct dwarf2_section_names line_dwo;
327 struct dwarf2_section_names loc_dwo;
328 struct dwarf2_section_names loclists_dwo;
329 struct dwarf2_section_names macinfo_dwo;
330 struct dwarf2_section_names macro_dwo;
331 struct dwarf2_section_names str_dwo;
332 struct dwarf2_section_names str_offsets_dwo;
333 struct dwarf2_section_names types_dwo;
334 struct dwarf2_section_names cu_index;
335 struct dwarf2_section_names tu_index;
336 }
337 dwop_section_names =
338 {
339 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
340 { ".debug_info.dwo", ".zdebug_info.dwo" },
341 { ".debug_line.dwo", ".zdebug_line.dwo" },
342 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
343 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
344 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
345 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
346 { ".debug_str.dwo", ".zdebug_str.dwo" },
347 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
348 { ".debug_types.dwo", ".zdebug_types.dwo" },
349 { ".debug_cu_index", ".zdebug_cu_index" },
350 { ".debug_tu_index", ".zdebug_tu_index" },
351 };
352
353 /* local data types */
354
355 /* The location list section (.debug_loclists) begins with a header,
356 which contains the following information. */
357 struct loclist_header
358 {
359 /* A 4-byte or 12-byte length containing the length of the
360 set of entries for this compilation unit, not including the
361 length field itself. */
362 unsigned int length;
363
364 /* A 2-byte version identifier. */
365 short version;
366
367 /* A 1-byte unsigned integer containing the size in bytes of an address on
368 the target system. */
369 unsigned char addr_size;
370
371 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
372 on the target system. */
373 unsigned char segment_collector_size;
374
375 /* A 4-byte count of the number of offsets that follow the header. */
376 unsigned int offset_entry_count;
377 };
378
379 /* Type used for delaying computation of method physnames.
380 See comments for compute_delayed_physnames. */
381 struct delayed_method_info
382 {
383 /* The type to which the method is attached, i.e., its parent class. */
384 struct type *type;
385
386 /* The index of the method in the type's function fieldlists. */
387 int fnfield_index;
388
389 /* The index of the method in the fieldlist. */
390 int index;
391
392 /* The name of the DIE. */
393 const char *name;
394
395 /* The DIE associated with this method. */
396 struct die_info *die;
397 };
398
399 /* Internal state when decoding a particular compilation unit. */
400 struct dwarf2_cu
401 {
402 explicit dwarf2_cu (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 (dwarf2_per_cu_data *this_cu,
916 dwarf2_per_objfile *per_objfile,
917 struct abbrev_table *abbrev_table,
918 int use_existing_cu,
919 bool skip_partial);
920
921 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
922 dwarf2_per_objfile *per_objfile,
923 struct dwarf2_cu *parent_cu = nullptr,
924 struct dwo_file *dwo_file = nullptr);
925
926 DISABLE_COPY_AND_ASSIGN (cutu_reader);
927
928 const gdb_byte *info_ptr = nullptr;
929 struct die_info *comp_unit_die = nullptr;
930 bool dummy_p = false;
931
932 /* Release the new CU, putting it on the chain. This cannot be done
933 for dummy CUs. */
934 void keep ();
935
936 private:
937 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
938 dwarf2_per_objfile *per_objfile,
939 int use_existing_cu);
940
941 struct dwarf2_per_cu_data *m_this_cu;
942 std::unique_ptr<dwarf2_cu> m_new_cu;
943
944 /* The ordinary abbreviation table. */
945 abbrev_table_up m_abbrev_table_holder;
946
947 /* The DWO abbreviation table. */
948 abbrev_table_up m_dwo_abbrev_table;
949 };
950
951 /* When we construct a partial symbol table entry we only
952 need this much information. */
953 struct partial_die_info : public allocate_on_obstack
954 {
955 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
956
957 /* Disable assign but still keep copy ctor, which is needed
958 load_partial_dies. */
959 partial_die_info& operator=(const partial_die_info& rhs) = delete;
960
961 /* Adjust the partial die before generating a symbol for it. This
962 function may set the is_external flag or change the DIE's
963 name. */
964 void fixup (struct dwarf2_cu *cu);
965
966 /* Read a minimal amount of information into the minimal die
967 structure. */
968 const gdb_byte *read (const struct die_reader_specs *reader,
969 const struct abbrev_info &abbrev,
970 const gdb_byte *info_ptr);
971
972 /* Offset of this DIE. */
973 const sect_offset sect_off;
974
975 /* DWARF-2 tag for this DIE. */
976 const ENUM_BITFIELD(dwarf_tag) tag : 16;
977
978 /* Assorted flags describing the data found in this DIE. */
979 const unsigned int has_children : 1;
980
981 unsigned int is_external : 1;
982 unsigned int is_declaration : 1;
983 unsigned int has_type : 1;
984 unsigned int has_specification : 1;
985 unsigned int has_pc_info : 1;
986 unsigned int may_be_inlined : 1;
987
988 /* This DIE has been marked DW_AT_main_subprogram. */
989 unsigned int main_subprogram : 1;
990
991 /* Flag set if the SCOPE field of this structure has been
992 computed. */
993 unsigned int scope_set : 1;
994
995 /* Flag set if the DIE has a byte_size attribute. */
996 unsigned int has_byte_size : 1;
997
998 /* Flag set if the DIE has a DW_AT_const_value attribute. */
999 unsigned int has_const_value : 1;
1000
1001 /* Flag set if any of the DIE's children are template arguments. */
1002 unsigned int has_template_arguments : 1;
1003
1004 /* Flag set if fixup has been called on this die. */
1005 unsigned int fixup_called : 1;
1006
1007 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1008 unsigned int is_dwz : 1;
1009
1010 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1011 unsigned int spec_is_dwz : 1;
1012
1013 /* The name of this DIE. Normally the value of DW_AT_name, but
1014 sometimes a default name for unnamed DIEs. */
1015 const char *name = nullptr;
1016
1017 /* The linkage name, if present. */
1018 const char *linkage_name = nullptr;
1019
1020 /* The scope to prepend to our children. This is generally
1021 allocated on the comp_unit_obstack, so will disappear
1022 when this compilation unit leaves the cache. */
1023 const char *scope = nullptr;
1024
1025 /* Some data associated with the partial DIE. The tag determines
1026 which field is live. */
1027 union
1028 {
1029 /* The location description associated with this DIE, if any. */
1030 struct dwarf_block *locdesc;
1031 /* The offset of an import, for DW_TAG_imported_unit. */
1032 sect_offset sect_off;
1033 } d {};
1034
1035 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1036 CORE_ADDR lowpc = 0;
1037 CORE_ADDR highpc = 0;
1038
1039 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1040 DW_AT_sibling, if any. */
1041 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1042 could return DW_AT_sibling values to its caller load_partial_dies. */
1043 const gdb_byte *sibling = nullptr;
1044
1045 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1046 DW_AT_specification (or DW_AT_abstract_origin or
1047 DW_AT_extension). */
1048 sect_offset spec_offset {};
1049
1050 /* Pointers to this DIE's parent, first child, and next sibling,
1051 if any. */
1052 struct partial_die_info *die_parent = nullptr;
1053 struct partial_die_info *die_child = nullptr;
1054 struct partial_die_info *die_sibling = nullptr;
1055
1056 friend struct partial_die_info *
1057 dwarf2_cu::find_partial_die (sect_offset sect_off);
1058
1059 private:
1060 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1061 partial_die_info (sect_offset sect_off)
1062 : partial_die_info (sect_off, DW_TAG_padding, 0)
1063 {
1064 }
1065
1066 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1067 int has_children_)
1068 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1069 {
1070 is_external = 0;
1071 is_declaration = 0;
1072 has_type = 0;
1073 has_specification = 0;
1074 has_pc_info = 0;
1075 may_be_inlined = 0;
1076 main_subprogram = 0;
1077 scope_set = 0;
1078 has_byte_size = 0;
1079 has_const_value = 0;
1080 has_template_arguments = 0;
1081 fixup_called = 0;
1082 is_dwz = 0;
1083 spec_is_dwz = 0;
1084 }
1085 };
1086
1087 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1088 but this would require a corresponding change in unpack_field_as_long
1089 and friends. */
1090 static int bits_per_byte = 8;
1091
1092 struct variant_part_builder;
1093
1094 /* When reading a variant, we track a bit more information about the
1095 field, and store it in an object of this type. */
1096
1097 struct variant_field
1098 {
1099 int first_field = -1;
1100 int last_field = -1;
1101
1102 /* A variant can contain other variant parts. */
1103 std::vector<variant_part_builder> variant_parts;
1104
1105 /* If we see a DW_TAG_variant, then this will be set if this is the
1106 default branch. */
1107 bool default_branch = false;
1108 /* If we see a DW_AT_discr_value, then this will be the discriminant
1109 value. */
1110 ULONGEST discriminant_value = 0;
1111 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1112 data. */
1113 struct dwarf_block *discr_list_data = nullptr;
1114 };
1115
1116 /* This represents a DW_TAG_variant_part. */
1117
1118 struct variant_part_builder
1119 {
1120 /* The offset of the discriminant field. */
1121 sect_offset discriminant_offset {};
1122
1123 /* Variants that are direct children of this variant part. */
1124 std::vector<variant_field> variants;
1125
1126 /* True if we're currently reading a variant. */
1127 bool processing_variant = false;
1128 };
1129
1130 struct nextfield
1131 {
1132 int accessibility = 0;
1133 int virtuality = 0;
1134 /* Variant parts need to find the discriminant, which is a DIE
1135 reference. We track the section offset of each field to make
1136 this link. */
1137 sect_offset offset;
1138 struct field field {};
1139 };
1140
1141 struct fnfieldlist
1142 {
1143 const char *name = nullptr;
1144 std::vector<struct fn_field> fnfields;
1145 };
1146
1147 /* The routines that read and process dies for a C struct or C++ class
1148 pass lists of data member fields and lists of member function fields
1149 in an instance of a field_info structure, as defined below. */
1150 struct field_info
1151 {
1152 /* List of data member and baseclasses fields. */
1153 std::vector<struct nextfield> fields;
1154 std::vector<struct nextfield> baseclasses;
1155
1156 /* Set if the accessibility of one of the fields is not public. */
1157 int non_public_fields = 0;
1158
1159 /* Member function fieldlist array, contains name of possibly overloaded
1160 member function, number of overloaded member functions and a pointer
1161 to the head of the member function field chain. */
1162 std::vector<struct fnfieldlist> fnfieldlists;
1163
1164 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1165 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1166 std::vector<struct decl_field> typedef_field_list;
1167
1168 /* Nested types defined by this class and the number of elements in this
1169 list. */
1170 std::vector<struct decl_field> nested_types_list;
1171
1172 /* If non-null, this is the variant part we are currently
1173 reading. */
1174 variant_part_builder *current_variant_part = nullptr;
1175 /* This holds all the top-level variant parts attached to the type
1176 we're reading. */
1177 std::vector<variant_part_builder> variant_parts;
1178
1179 /* Return the total number of fields (including baseclasses). */
1180 int nfields () const
1181 {
1182 return fields.size () + baseclasses.size ();
1183 }
1184 };
1185
1186 /* Loaded secondary compilation units are kept in memory until they
1187 have not been referenced for the processing of this many
1188 compilation units. Set this to zero to disable caching. Cache
1189 sizes of up to at least twenty will improve startup time for
1190 typical inter-CU-reference binaries, at an obvious memory cost. */
1191 static int dwarf_max_cache_age = 5;
1192 static void
1193 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1194 struct cmd_list_element *c, const char *value)
1195 {
1196 fprintf_filtered (file, _("The upper bound on the age of cached "
1197 "DWARF compilation units is %s.\n"),
1198 value);
1199 }
1200 \f
1201 /* local function prototypes */
1202
1203 static void dwarf2_find_base_address (struct die_info *die,
1204 struct dwarf2_cu *cu);
1205
1206 static dwarf2_psymtab *create_partial_symtab
1207 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1208 const char *name);
1209
1210 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1211 const gdb_byte *info_ptr,
1212 struct die_info *type_unit_die);
1213
1214 static void dwarf2_build_psymtabs_hard
1215 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1216
1217 static void scan_partial_symbols (struct partial_die_info *,
1218 CORE_ADDR *, CORE_ADDR *,
1219 int, struct dwarf2_cu *);
1220
1221 static void add_partial_symbol (struct partial_die_info *,
1222 struct dwarf2_cu *);
1223
1224 static void add_partial_namespace (struct partial_die_info *pdi,
1225 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1226 int set_addrmap, struct dwarf2_cu *cu);
1227
1228 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1229 CORE_ADDR *highpc, int set_addrmap,
1230 struct dwarf2_cu *cu);
1231
1232 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1233 struct dwarf2_cu *cu);
1234
1235 static void add_partial_subprogram (struct partial_die_info *pdi,
1236 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1237 int need_pc, struct dwarf2_cu *cu);
1238
1239 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1240
1241 static struct partial_die_info *load_partial_dies
1242 (const struct die_reader_specs *, const gdb_byte *, int);
1243
1244 /* A pair of partial_die_info and compilation unit. */
1245 struct cu_partial_die_info
1246 {
1247 /* The compilation unit of the partial_die_info. */
1248 struct dwarf2_cu *cu;
1249 /* A partial_die_info. */
1250 struct partial_die_info *pdi;
1251
1252 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1253 : cu (cu),
1254 pdi (pdi)
1255 { /* Nothing. */ }
1256
1257 private:
1258 cu_partial_die_info () = delete;
1259 };
1260
1261 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1262 struct dwarf2_cu *);
1263
1264 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1265 struct attribute *, struct attr_abbrev *,
1266 const gdb_byte *, bool *need_reprocess);
1267
1268 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1269 struct attribute *attr);
1270
1271 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1272
1273 static sect_offset read_abbrev_offset
1274 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1275 struct dwarf2_section_info *, sect_offset);
1276
1277 static const char *read_indirect_string
1278 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1279 const struct comp_unit_head *, unsigned int *);
1280
1281 static const char *read_indirect_string_at_offset
1282 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1283
1284 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1285 const gdb_byte *,
1286 unsigned int *);
1287
1288 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1289 ULONGEST str_index);
1290
1291 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1292 ULONGEST str_index);
1293
1294 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1295
1296 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1297 struct dwarf2_cu *);
1298
1299 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1300 struct dwarf2_cu *cu);
1301
1302 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1303
1304 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1305 struct dwarf2_cu *cu);
1306
1307 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1308
1309 static struct die_info *die_specification (struct die_info *die,
1310 struct dwarf2_cu **);
1311
1312 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1313 struct dwarf2_cu *cu);
1314
1315 static void dwarf_decode_lines (struct line_header *, const char *,
1316 struct dwarf2_cu *, dwarf2_psymtab *,
1317 CORE_ADDR, int decode_mapping);
1318
1319 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1320 const char *);
1321
1322 static struct symbol *new_symbol (struct die_info *, struct type *,
1323 struct dwarf2_cu *, struct symbol * = NULL);
1324
1325 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1326 struct dwarf2_cu *);
1327
1328 static void dwarf2_const_value_attr (const struct attribute *attr,
1329 struct type *type,
1330 const char *name,
1331 struct obstack *obstack,
1332 struct dwarf2_cu *cu, LONGEST *value,
1333 const gdb_byte **bytes,
1334 struct dwarf2_locexpr_baton **baton);
1335
1336 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1337
1338 static int need_gnat_info (struct dwarf2_cu *);
1339
1340 static struct type *die_descriptive_type (struct die_info *,
1341 struct dwarf2_cu *);
1342
1343 static void set_descriptive_type (struct type *, struct die_info *,
1344 struct dwarf2_cu *);
1345
1346 static struct type *die_containing_type (struct die_info *,
1347 struct dwarf2_cu *);
1348
1349 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1350 struct dwarf2_cu *);
1351
1352 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1353
1354 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1355
1356 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1357
1358 static char *typename_concat (struct obstack *obs, const char *prefix,
1359 const char *suffix, int physname,
1360 struct dwarf2_cu *cu);
1361
1362 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1363
1364 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1365
1366 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1367
1368 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1369
1370 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1371
1372 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1373
1374 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1375 struct dwarf2_cu *, dwarf2_psymtab *);
1376
1377 /* Return the .debug_loclists section to use for cu. */
1378 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1379
1380 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1381 values. Keep the items ordered with increasing constraints compliance. */
1382 enum pc_bounds_kind
1383 {
1384 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1385 PC_BOUNDS_NOT_PRESENT,
1386
1387 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1388 were present but they do not form a valid range of PC addresses. */
1389 PC_BOUNDS_INVALID,
1390
1391 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1392 PC_BOUNDS_RANGES,
1393
1394 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1395 PC_BOUNDS_HIGH_LOW,
1396 };
1397
1398 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1399 CORE_ADDR *, CORE_ADDR *,
1400 struct dwarf2_cu *,
1401 dwarf2_psymtab *);
1402
1403 static void get_scope_pc_bounds (struct die_info *,
1404 CORE_ADDR *, CORE_ADDR *,
1405 struct dwarf2_cu *);
1406
1407 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1408 CORE_ADDR, struct dwarf2_cu *);
1409
1410 static void dwarf2_add_field (struct field_info *, struct die_info *,
1411 struct dwarf2_cu *);
1412
1413 static void dwarf2_attach_fields_to_type (struct field_info *,
1414 struct type *, struct dwarf2_cu *);
1415
1416 static void dwarf2_add_member_fn (struct field_info *,
1417 struct die_info *, struct type *,
1418 struct dwarf2_cu *);
1419
1420 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1421 struct type *,
1422 struct dwarf2_cu *);
1423
1424 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1425
1426 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1427
1428 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1429
1430 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1431
1432 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1433
1434 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1435
1436 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1437
1438 static struct type *read_module_type (struct die_info *die,
1439 struct dwarf2_cu *cu);
1440
1441 static const char *namespace_name (struct die_info *die,
1442 int *is_anonymous, struct dwarf2_cu *);
1443
1444 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1445
1446 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1447 bool * = nullptr);
1448
1449 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1450 struct dwarf2_cu *);
1451
1452 static struct die_info *read_die_and_siblings_1
1453 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1454 struct die_info *);
1455
1456 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1457 const gdb_byte *info_ptr,
1458 const gdb_byte **new_info_ptr,
1459 struct die_info *parent);
1460
1461 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1462 struct die_info **, const gdb_byte *,
1463 int);
1464
1465 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1466 struct die_info **, const gdb_byte *);
1467
1468 static void process_die (struct die_info *, struct dwarf2_cu *);
1469
1470 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1471 struct objfile *);
1472
1473 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1474
1475 static const char *dwarf2_full_name (const char *name,
1476 struct die_info *die,
1477 struct dwarf2_cu *cu);
1478
1479 static const char *dwarf2_physname (const char *name, struct die_info *die,
1480 struct dwarf2_cu *cu);
1481
1482 static struct die_info *dwarf2_extension (struct die_info *die,
1483 struct dwarf2_cu **);
1484
1485 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1486
1487 static void dump_die_for_error (struct die_info *);
1488
1489 static void dump_die_1 (struct ui_file *, int level, int max_level,
1490 struct die_info *);
1491
1492 /*static*/ void dump_die (struct die_info *, int max_level);
1493
1494 static void store_in_ref_table (struct die_info *,
1495 struct dwarf2_cu *);
1496
1497 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1498 const struct attribute *,
1499 struct dwarf2_cu **);
1500
1501 static struct die_info *follow_die_ref (struct die_info *,
1502 const struct attribute *,
1503 struct dwarf2_cu **);
1504
1505 static struct die_info *follow_die_sig (struct die_info *,
1506 const struct attribute *,
1507 struct dwarf2_cu **);
1508
1509 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1510 struct dwarf2_cu *);
1511
1512 static struct type *get_DW_AT_signature_type (struct die_info *,
1513 const struct attribute *,
1514 struct dwarf2_cu *);
1515
1516 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1517 dwarf2_per_objfile *per_objfile);
1518
1519 static void read_signatured_type (signatured_type *sig_type,
1520 dwarf2_per_objfile *per_objfile);
1521
1522 static int attr_to_dynamic_prop (const struct attribute *attr,
1523 struct die_info *die, struct dwarf2_cu *cu,
1524 struct dynamic_prop *prop, struct type *type);
1525
1526 /* memory allocation interface */
1527
1528 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1529
1530 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1531
1532 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1533
1534 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1535 struct dwarf2_loclist_baton *baton,
1536 const struct attribute *attr);
1537
1538 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1539 struct symbol *sym,
1540 struct dwarf2_cu *cu,
1541 int is_block);
1542
1543 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1544 const gdb_byte *info_ptr,
1545 struct abbrev_info *abbrev);
1546
1547 static hashval_t partial_die_hash (const void *item);
1548
1549 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1550
1551 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1552 (sect_offset sect_off, unsigned int offset_in_dwz,
1553 struct dwarf2_per_objfile *dwarf2_per_objfile);
1554
1555 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1556 struct die_info *comp_unit_die,
1557 enum language pretend_language);
1558
1559 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1560
1561 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1562
1563 static struct type *set_die_type (struct die_info *, struct type *,
1564 struct dwarf2_cu *);
1565
1566 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1567
1568 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1569
1570 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1571 dwarf2_per_objfile *per_objfile,
1572 bool skip_partial,
1573 enum language pretend_language);
1574
1575 static void process_full_comp_unit (dwarf2_per_cu_data *per_cu,
1576 dwarf2_per_objfile *per_objfile,
1577 enum language pretend_language);
1578
1579 static void process_full_type_unit (dwarf2_per_cu_data *per_cu,
1580 dwarf2_per_objfile *per_objfile,
1581 enum language pretend_language);
1582
1583 static void dwarf2_add_dependence (struct dwarf2_cu *,
1584 struct dwarf2_per_cu_data *);
1585
1586 static void dwarf2_mark (struct dwarf2_cu *);
1587
1588 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1589
1590 static struct type *get_die_type_at_offset (sect_offset,
1591 struct dwarf2_per_cu_data *);
1592
1593 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1594
1595 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1596 enum language pretend_language);
1597
1598 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1599
1600 /* Class, the destructor of which frees all allocated queue entries. This
1601 will only have work to do if an error was thrown while processing the
1602 dwarf. If no error was thrown then the queue entries should have all
1603 been processed, and freed, as we went along. */
1604
1605 class dwarf2_queue_guard
1606 {
1607 public:
1608 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1609 : m_per_objfile (per_objfile)
1610 {
1611 }
1612
1613 /* Free any entries remaining on the queue. There should only be
1614 entries left if we hit an error while processing the dwarf. */
1615 ~dwarf2_queue_guard ()
1616 {
1617 /* Ensure that no memory is allocated by the queue. */
1618 std::queue<dwarf2_queue_item> empty;
1619 std::swap (m_per_objfile->per_bfd->queue, empty);
1620 }
1621
1622 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1623
1624 private:
1625 dwarf2_per_objfile *m_per_objfile;
1626 };
1627
1628 dwarf2_queue_item::~dwarf2_queue_item ()
1629 {
1630 /* Anything still marked queued is likely to be in an
1631 inconsistent state, so discard it. */
1632 if (per_cu->queued)
1633 {
1634 if (per_cu->cu != NULL)
1635 free_one_cached_comp_unit (per_cu);
1636 per_cu->queued = 0;
1637 }
1638 }
1639
1640 /* The return type of find_file_and_directory. Note, the enclosed
1641 string pointers are only valid while this object is valid. */
1642
1643 struct file_and_directory
1644 {
1645 /* The filename. This is never NULL. */
1646 const char *name;
1647
1648 /* The compilation directory. NULL if not known. If we needed to
1649 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1650 points directly to the DW_AT_comp_dir string attribute owned by
1651 the obstack that owns the DIE. */
1652 const char *comp_dir;
1653
1654 /* If we needed to build a new string for comp_dir, this is what
1655 owns the storage. */
1656 std::string comp_dir_storage;
1657 };
1658
1659 static file_and_directory find_file_and_directory (struct die_info *die,
1660 struct dwarf2_cu *cu);
1661
1662 static htab_up allocate_signatured_type_table ();
1663
1664 static htab_up allocate_dwo_unit_table ();
1665
1666 static struct dwo_unit *lookup_dwo_unit_in_dwp
1667 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1668 struct dwp_file *dwp_file, const char *comp_dir,
1669 ULONGEST signature, int is_debug_types);
1670
1671 static struct dwp_file *get_dwp_file
1672 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1673
1674 static struct dwo_unit *lookup_dwo_comp_unit
1675 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1676 ULONGEST signature);
1677
1678 static struct dwo_unit *lookup_dwo_type_unit
1679 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1680
1681 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1682
1683 /* A unique pointer to a dwo_file. */
1684
1685 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1686
1687 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1688
1689 static void check_producer (struct dwarf2_cu *cu);
1690
1691 static void free_line_header_voidp (void *arg);
1692 \f
1693 /* Various complaints about symbol reading that don't abort the process. */
1694
1695 static void
1696 dwarf2_debug_line_missing_file_complaint (void)
1697 {
1698 complaint (_(".debug_line section has line data without a file"));
1699 }
1700
1701 static void
1702 dwarf2_debug_line_missing_end_sequence_complaint (void)
1703 {
1704 complaint (_(".debug_line section has line "
1705 "program sequence without an end"));
1706 }
1707
1708 static void
1709 dwarf2_complex_location_expr_complaint (void)
1710 {
1711 complaint (_("location expression too complex"));
1712 }
1713
1714 static void
1715 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1716 int arg3)
1717 {
1718 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1719 arg1, arg2, arg3);
1720 }
1721
1722 static void
1723 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1724 {
1725 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1726 arg1, arg2);
1727 }
1728
1729 /* Hash function for line_header_hash. */
1730
1731 static hashval_t
1732 line_header_hash (const struct line_header *ofs)
1733 {
1734 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1735 }
1736
1737 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1738
1739 static hashval_t
1740 line_header_hash_voidp (const void *item)
1741 {
1742 const struct line_header *ofs = (const struct line_header *) item;
1743
1744 return line_header_hash (ofs);
1745 }
1746
1747 /* Equality function for line_header_hash. */
1748
1749 static int
1750 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1751 {
1752 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1753 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1754
1755 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1756 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1757 }
1758
1759 \f
1760
1761 /* See declaration. */
1762
1763 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1764 bool can_copy_)
1765 : obfd (obfd),
1766 can_copy (can_copy_)
1767 {
1768 if (names == NULL)
1769 names = &dwarf2_elf_names;
1770
1771 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1772 locate_sections (obfd, sec, *names);
1773 }
1774
1775 dwarf2_per_bfd::~dwarf2_per_bfd ()
1776 {
1777 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1778 free_cached_comp_units ();
1779
1780 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1781 per_cu->imported_symtabs_free ();
1782
1783 for (signatured_type *sig_type : all_type_units)
1784 sig_type->per_cu.imported_symtabs_free ();
1785
1786 /* Everything else should be on this->obstack. */
1787 }
1788
1789 /* See declaration. */
1790
1791 void
1792 dwarf2_per_bfd::free_cached_comp_units ()
1793 {
1794 dwarf2_per_cu_data *per_cu = read_in_chain;
1795 dwarf2_per_cu_data **last_chain = &read_in_chain;
1796 while (per_cu != NULL)
1797 {
1798 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1799
1800 delete per_cu->cu;
1801 *last_chain = next_cu;
1802 per_cu = next_cu;
1803 }
1804 }
1805
1806 /* A helper class that calls free_cached_comp_units on
1807 destruction. */
1808
1809 class free_cached_comp_units
1810 {
1811 public:
1812
1813 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1814 : m_per_objfile (per_objfile)
1815 {
1816 }
1817
1818 ~free_cached_comp_units ()
1819 {
1820 m_per_objfile->per_bfd->free_cached_comp_units ();
1821 }
1822
1823 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1824
1825 private:
1826
1827 dwarf2_per_objfile *m_per_objfile;
1828 };
1829
1830 /* See read.h. */
1831
1832 bool
1833 dwarf2_per_objfile::symtab_set_p (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] != nullptr;
1838 }
1839
1840 /* See read.h. */
1841
1842 compunit_symtab *
1843 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1844 {
1845 gdb_assert (per_cu->index < this->m_symtabs.size ());
1846
1847 return this->m_symtabs[per_cu->index];
1848 }
1849
1850 /* See read.h. */
1851
1852 void
1853 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1854 compunit_symtab *symtab)
1855 {
1856 gdb_assert (per_cu->index < this->m_symtabs.size ());
1857 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1858
1859 this->m_symtabs[per_cu->index] = symtab;
1860 }
1861
1862 /* Try to locate the sections we need for DWARF 2 debugging
1863 information and return true if we have enough to do something.
1864 NAMES points to the dwarf2 section names, or is NULL if the standard
1865 ELF names are used. CAN_COPY is true for formats where symbol
1866 interposition is possible and so symbol values must follow copy
1867 relocation rules. */
1868
1869 int
1870 dwarf2_has_info (struct objfile *objfile,
1871 const struct dwarf2_debug_sections *names,
1872 bool can_copy)
1873 {
1874 if (objfile->flags & OBJF_READNEVER)
1875 return 0;
1876
1877 struct dwarf2_per_objfile *dwarf2_per_objfile
1878 = get_dwarf2_per_objfile (objfile);
1879
1880 if (dwarf2_per_objfile == NULL)
1881 {
1882 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1883 sharing yet). */
1884 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1885
1886 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1887 }
1888
1889 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1890 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1891 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1892 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
1893 }
1894
1895 /* When loading sections, we look either for uncompressed section or for
1896 compressed section names. */
1897
1898 static int
1899 section_is_p (const char *section_name,
1900 const struct dwarf2_section_names *names)
1901 {
1902 if (names->normal != NULL
1903 && strcmp (section_name, names->normal) == 0)
1904 return 1;
1905 if (names->compressed != NULL
1906 && strcmp (section_name, names->compressed) == 0)
1907 return 1;
1908 return 0;
1909 }
1910
1911 /* See declaration. */
1912
1913 void
1914 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1915 const dwarf2_debug_sections &names)
1916 {
1917 flagword aflag = bfd_section_flags (sectp);
1918
1919 if ((aflag & SEC_HAS_CONTENTS) == 0)
1920 {
1921 }
1922 else if (elf_section_data (sectp)->this_hdr.sh_size
1923 > bfd_get_file_size (abfd))
1924 {
1925 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1926 warning (_("Discarding section %s which has a section size (%s"
1927 ") larger than the file size [in module %s]"),
1928 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1929 bfd_get_filename (abfd));
1930 }
1931 else if (section_is_p (sectp->name, &names.info))
1932 {
1933 this->info.s.section = sectp;
1934 this->info.size = bfd_section_size (sectp);
1935 }
1936 else if (section_is_p (sectp->name, &names.abbrev))
1937 {
1938 this->abbrev.s.section = sectp;
1939 this->abbrev.size = bfd_section_size (sectp);
1940 }
1941 else if (section_is_p (sectp->name, &names.line))
1942 {
1943 this->line.s.section = sectp;
1944 this->line.size = bfd_section_size (sectp);
1945 }
1946 else if (section_is_p (sectp->name, &names.loc))
1947 {
1948 this->loc.s.section = sectp;
1949 this->loc.size = bfd_section_size (sectp);
1950 }
1951 else if (section_is_p (sectp->name, &names.loclists))
1952 {
1953 this->loclists.s.section = sectp;
1954 this->loclists.size = bfd_section_size (sectp);
1955 }
1956 else if (section_is_p (sectp->name, &names.macinfo))
1957 {
1958 this->macinfo.s.section = sectp;
1959 this->macinfo.size = bfd_section_size (sectp);
1960 }
1961 else if (section_is_p (sectp->name, &names.macro))
1962 {
1963 this->macro.s.section = sectp;
1964 this->macro.size = bfd_section_size (sectp);
1965 }
1966 else if (section_is_p (sectp->name, &names.str))
1967 {
1968 this->str.s.section = sectp;
1969 this->str.size = bfd_section_size (sectp);
1970 }
1971 else if (section_is_p (sectp->name, &names.str_offsets))
1972 {
1973 this->str_offsets.s.section = sectp;
1974 this->str_offsets.size = bfd_section_size (sectp);
1975 }
1976 else if (section_is_p (sectp->name, &names.line_str))
1977 {
1978 this->line_str.s.section = sectp;
1979 this->line_str.size = bfd_section_size (sectp);
1980 }
1981 else if (section_is_p (sectp->name, &names.addr))
1982 {
1983 this->addr.s.section = sectp;
1984 this->addr.size = bfd_section_size (sectp);
1985 }
1986 else if (section_is_p (sectp->name, &names.frame))
1987 {
1988 this->frame.s.section = sectp;
1989 this->frame.size = bfd_section_size (sectp);
1990 }
1991 else if (section_is_p (sectp->name, &names.eh_frame))
1992 {
1993 this->eh_frame.s.section = sectp;
1994 this->eh_frame.size = bfd_section_size (sectp);
1995 }
1996 else if (section_is_p (sectp->name, &names.ranges))
1997 {
1998 this->ranges.s.section = sectp;
1999 this->ranges.size = bfd_section_size (sectp);
2000 }
2001 else if (section_is_p (sectp->name, &names.rnglists))
2002 {
2003 this->rnglists.s.section = sectp;
2004 this->rnglists.size = bfd_section_size (sectp);
2005 }
2006 else if (section_is_p (sectp->name, &names.types))
2007 {
2008 struct dwarf2_section_info type_section;
2009
2010 memset (&type_section, 0, sizeof (type_section));
2011 type_section.s.section = sectp;
2012 type_section.size = bfd_section_size (sectp);
2013
2014 this->types.push_back (type_section);
2015 }
2016 else if (section_is_p (sectp->name, &names.gdb_index))
2017 {
2018 this->gdb_index.s.section = sectp;
2019 this->gdb_index.size = bfd_section_size (sectp);
2020 }
2021 else if (section_is_p (sectp->name, &names.debug_names))
2022 {
2023 this->debug_names.s.section = sectp;
2024 this->debug_names.size = bfd_section_size (sectp);
2025 }
2026 else if (section_is_p (sectp->name, &names.debug_aranges))
2027 {
2028 this->debug_aranges.s.section = sectp;
2029 this->debug_aranges.size = bfd_section_size (sectp);
2030 }
2031
2032 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2033 && bfd_section_vma (sectp) == 0)
2034 this->has_section_at_zero = true;
2035 }
2036
2037 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2038 SECTION_NAME. */
2039
2040 void
2041 dwarf2_get_section_info (struct objfile *objfile,
2042 enum dwarf2_section_enum sect,
2043 asection **sectp, const gdb_byte **bufp,
2044 bfd_size_type *sizep)
2045 {
2046 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2047 struct dwarf2_section_info *info;
2048
2049 /* We may see an objfile without any DWARF, in which case we just
2050 return nothing. */
2051 if (data == NULL)
2052 {
2053 *sectp = NULL;
2054 *bufp = NULL;
2055 *sizep = 0;
2056 return;
2057 }
2058 switch (sect)
2059 {
2060 case DWARF2_DEBUG_FRAME:
2061 info = &data->per_bfd->frame;
2062 break;
2063 case DWARF2_EH_FRAME:
2064 info = &data->per_bfd->eh_frame;
2065 break;
2066 default:
2067 gdb_assert_not_reached ("unexpected section");
2068 }
2069
2070 info->read (objfile);
2071
2072 *sectp = info->get_bfd_section ();
2073 *bufp = info->buffer;
2074 *sizep = info->size;
2075 }
2076
2077 /* A helper function to find the sections for a .dwz file. */
2078
2079 static void
2080 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2081 {
2082 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2083
2084 /* Note that we only support the standard ELF names, because .dwz
2085 is ELF-only (at the time of writing). */
2086 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2087 {
2088 dwz_file->abbrev.s.section = sectp;
2089 dwz_file->abbrev.size = bfd_section_size (sectp);
2090 }
2091 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2092 {
2093 dwz_file->info.s.section = sectp;
2094 dwz_file->info.size = bfd_section_size (sectp);
2095 }
2096 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2097 {
2098 dwz_file->str.s.section = sectp;
2099 dwz_file->str.size = bfd_section_size (sectp);
2100 }
2101 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2102 {
2103 dwz_file->line.s.section = sectp;
2104 dwz_file->line.size = bfd_section_size (sectp);
2105 }
2106 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2107 {
2108 dwz_file->macro.s.section = sectp;
2109 dwz_file->macro.size = bfd_section_size (sectp);
2110 }
2111 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2112 {
2113 dwz_file->gdb_index.s.section = sectp;
2114 dwz_file->gdb_index.size = bfd_section_size (sectp);
2115 }
2116 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2117 {
2118 dwz_file->debug_names.s.section = sectp;
2119 dwz_file->debug_names.size = bfd_section_size (sectp);
2120 }
2121 }
2122
2123 /* See dwarf2read.h. */
2124
2125 struct dwz_file *
2126 dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
2127 {
2128 const char *filename;
2129 bfd_size_type buildid_len_arg;
2130 size_t buildid_len;
2131 bfd_byte *buildid;
2132
2133 if (per_bfd->dwz_file != NULL)
2134 return per_bfd->dwz_file.get ();
2135
2136 bfd_set_error (bfd_error_no_error);
2137 gdb::unique_xmalloc_ptr<char> data
2138 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2139 &buildid_len_arg, &buildid));
2140 if (data == NULL)
2141 {
2142 if (bfd_get_error () == bfd_error_no_error)
2143 return NULL;
2144 error (_("could not read '.gnu_debugaltlink' section: %s"),
2145 bfd_errmsg (bfd_get_error ()));
2146 }
2147
2148 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2149
2150 buildid_len = (size_t) buildid_len_arg;
2151
2152 filename = data.get ();
2153
2154 std::string abs_storage;
2155 if (!IS_ABSOLUTE_PATH (filename))
2156 {
2157 gdb::unique_xmalloc_ptr<char> abs
2158 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2159
2160 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2161 filename = abs_storage.c_str ();
2162 }
2163
2164 /* First try the file name given in the section. If that doesn't
2165 work, try to use the build-id instead. */
2166 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
2167 if (dwz_bfd != NULL)
2168 {
2169 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2170 dwz_bfd.reset (nullptr);
2171 }
2172
2173 if (dwz_bfd == NULL)
2174 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2175
2176 if (dwz_bfd == nullptr)
2177 {
2178 gdb::unique_xmalloc_ptr<char> alt_filename;
2179 const char *origname = bfd_get_filename (per_bfd->obfd);
2180
2181 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2182 buildid_len,
2183 origname,
2184 &alt_filename));
2185
2186 if (fd.get () >= 0)
2187 {
2188 /* File successfully retrieved from server. */
2189 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2190
2191 if (dwz_bfd == nullptr)
2192 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2193 alt_filename.get ());
2194 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2195 dwz_bfd.reset (nullptr);
2196 }
2197 }
2198
2199 if (dwz_bfd == NULL)
2200 error (_("could not find '.gnu_debugaltlink' file for %s"),
2201 bfd_get_filename (per_bfd->obfd));
2202
2203 std::unique_ptr<struct dwz_file> result
2204 (new struct dwz_file (std::move (dwz_bfd)));
2205
2206 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2207 result.get ());
2208
2209 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2210 per_bfd->dwz_file = std::move (result);
2211 return per_bfd->dwz_file.get ();
2212 }
2213 \f
2214 /* DWARF quick_symbols_functions support. */
2215
2216 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2217 unique line tables, so we maintain a separate table of all .debug_line
2218 derived entries to support the sharing.
2219 All the quick functions need is the list of file names. We discard the
2220 line_header when we're done and don't need to record it here. */
2221 struct quick_file_names
2222 {
2223 /* The data used to construct the hash key. */
2224 struct stmt_list_hash hash;
2225
2226 /* The number of entries in file_names, real_names. */
2227 unsigned int num_file_names;
2228
2229 /* The file names from the line table, after being run through
2230 file_full_name. */
2231 const char **file_names;
2232
2233 /* The file names from the line table after being run through
2234 gdb_realpath. These are computed lazily. */
2235 const char **real_names;
2236 };
2237
2238 /* When using the index (and thus not using psymtabs), each CU has an
2239 object of this type. This is used to hold information needed by
2240 the various "quick" methods. */
2241 struct dwarf2_per_cu_quick_data
2242 {
2243 /* The file table. This can be NULL if there was no file table
2244 or it's currently not read in.
2245 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2246 struct quick_file_names *file_names;
2247
2248 /* A temporary mark bit used when iterating over all CUs in
2249 expand_symtabs_matching. */
2250 unsigned int mark : 1;
2251
2252 /* True if we've tried to read the file table and found there isn't one.
2253 There will be no point in trying to read it again next time. */
2254 unsigned int no_file_data : 1;
2255 };
2256
2257 /* Utility hash function for a stmt_list_hash. */
2258
2259 static hashval_t
2260 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2261 {
2262 hashval_t v = 0;
2263
2264 if (stmt_list_hash->dwo_unit != NULL)
2265 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2266 v += to_underlying (stmt_list_hash->line_sect_off);
2267 return v;
2268 }
2269
2270 /* Utility equality function for a stmt_list_hash. */
2271
2272 static int
2273 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2274 const struct stmt_list_hash *rhs)
2275 {
2276 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2277 return 0;
2278 if (lhs->dwo_unit != NULL
2279 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2280 return 0;
2281
2282 return lhs->line_sect_off == rhs->line_sect_off;
2283 }
2284
2285 /* Hash function for a quick_file_names. */
2286
2287 static hashval_t
2288 hash_file_name_entry (const void *e)
2289 {
2290 const struct quick_file_names *file_data
2291 = (const struct quick_file_names *) e;
2292
2293 return hash_stmt_list_entry (&file_data->hash);
2294 }
2295
2296 /* Equality function for a quick_file_names. */
2297
2298 static int
2299 eq_file_name_entry (const void *a, const void *b)
2300 {
2301 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2302 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2303
2304 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2305 }
2306
2307 /* Delete function for a quick_file_names. */
2308
2309 static void
2310 delete_file_name_entry (void *e)
2311 {
2312 struct quick_file_names *file_data = (struct quick_file_names *) e;
2313 int i;
2314
2315 for (i = 0; i < file_data->num_file_names; ++i)
2316 {
2317 xfree ((void*) file_data->file_names[i]);
2318 if (file_data->real_names)
2319 xfree ((void*) file_data->real_names[i]);
2320 }
2321
2322 /* The space for the struct itself lives on the obstack, so we don't
2323 free it here. */
2324 }
2325
2326 /* Create a quick_file_names hash table. */
2327
2328 static htab_up
2329 create_quick_file_names_table (unsigned int nr_initial_entries)
2330 {
2331 return htab_up (htab_create_alloc (nr_initial_entries,
2332 hash_file_name_entry, eq_file_name_entry,
2333 delete_file_name_entry, xcalloc, xfree));
2334 }
2335
2336 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2337 function is unrelated to symtabs, symtab would have to be created afterwards.
2338 You should call age_cached_comp_units after processing the CU. */
2339
2340 static void
2341 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2342 bool skip_partial)
2343 {
2344 if (per_cu->is_debug_types)
2345 load_full_type_unit (per_cu, per_objfile);
2346 else
2347 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
2348
2349 if (per_cu->cu == NULL)
2350 return; /* Dummy CU. */
2351
2352 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2353 }
2354
2355 /* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2356
2357 static void
2358 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2359 dwarf2_per_objfile *dwarf2_per_objfile,
2360 bool skip_partial)
2361 {
2362 /* Skip type_unit_groups, reading the type units they contain
2363 is handled elsewhere. */
2364 if (per_cu->type_unit_group_p ())
2365 return;
2366
2367 /* The destructor of dwarf2_queue_guard frees any entries left on
2368 the queue. After this point we're guaranteed to leave this function
2369 with the dwarf queue empty. */
2370 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2371
2372 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2373 {
2374 queue_comp_unit (per_cu, language_minimal);
2375 load_cu (per_cu, dwarf2_per_objfile, skip_partial);
2376
2377 /* If we just loaded a CU from a DWO, and we're working with an index
2378 that may badly handle TUs, load all the TUs in that DWO as well.
2379 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2380 if (!per_cu->is_debug_types
2381 && per_cu->cu != NULL
2382 && per_cu->cu->dwo_unit != NULL
2383 && dwarf2_per_objfile->per_bfd->index_table != NULL
2384 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
2385 /* DWP files aren't supported yet. */
2386 && get_dwp_file (dwarf2_per_objfile) == NULL)
2387 queue_and_load_all_dwo_tus (per_cu);
2388 }
2389
2390 process_queue (dwarf2_per_objfile);
2391
2392 /* Age the cache, releasing compilation units that have not
2393 been used recently. */
2394 age_cached_comp_units (dwarf2_per_objfile);
2395 }
2396
2397 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2398 the per-objfile for which this symtab is instantiated.
2399
2400 Returns the resulting symbol table. */
2401
2402 static struct compunit_symtab *
2403 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2404 dwarf2_per_objfile *dwarf2_per_objfile,
2405 bool skip_partial)
2406 {
2407 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
2408
2409 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2410 {
2411 free_cached_comp_units freer (dwarf2_per_objfile);
2412 scoped_restore decrementer = increment_reading_symtab ();
2413 dw2_do_instantiate_symtab (per_cu, dwarf2_per_objfile, skip_partial);
2414 process_cu_includes (dwarf2_per_objfile);
2415 }
2416
2417 return dwarf2_per_objfile->get_symtab (per_cu);
2418 }
2419
2420 /* See declaration. */
2421
2422 dwarf2_per_cu_data *
2423 dwarf2_per_bfd::get_cutu (int index)
2424 {
2425 if (index >= this->all_comp_units.size ())
2426 {
2427 index -= this->all_comp_units.size ();
2428 gdb_assert (index < this->all_type_units.size ());
2429 return &this->all_type_units[index]->per_cu;
2430 }
2431
2432 return this->all_comp_units[index];
2433 }
2434
2435 /* See declaration. */
2436
2437 dwarf2_per_cu_data *
2438 dwarf2_per_bfd::get_cu (int index)
2439 {
2440 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2441
2442 return this->all_comp_units[index];
2443 }
2444
2445 /* See declaration. */
2446
2447 signatured_type *
2448 dwarf2_per_bfd::get_tu (int index)
2449 {
2450 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2451
2452 return this->all_type_units[index];
2453 }
2454
2455 /* See read.h. */
2456
2457 dwarf2_per_cu_data *
2458 dwarf2_per_bfd::allocate_per_cu ()
2459 {
2460 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2461 result->per_bfd = this;
2462 result->index = m_num_psymtabs++;
2463 return result;
2464 }
2465
2466 /* See read.h. */
2467
2468 signatured_type *
2469 dwarf2_per_bfd::allocate_signatured_type ()
2470 {
2471 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2472 result->per_cu.per_bfd = this;
2473 result->per_cu.index = m_num_psymtabs++;
2474 return result;
2475 }
2476
2477 /* Return a new dwarf2_per_cu_data allocated on the dwarf2_per_objfile
2478 obstack, and constructed with the specified field values. */
2479
2480 static dwarf2_per_cu_data *
2481 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2482 struct dwarf2_section_info *section,
2483 int is_dwz,
2484 sect_offset sect_off, ULONGEST length)
2485 {
2486 dwarf2_per_cu_data *the_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
2487 the_cu->sect_off = sect_off;
2488 the_cu->length = length;
2489 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2490 the_cu->section = section;
2491 the_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2492 struct dwarf2_per_cu_quick_data);
2493 the_cu->is_dwz = is_dwz;
2494 return the_cu;
2495 }
2496
2497 /* A helper for create_cus_from_index that handles a given list of
2498 CUs. */
2499
2500 static void
2501 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2502 const gdb_byte *cu_list, offset_type n_elements,
2503 struct dwarf2_section_info *section,
2504 int is_dwz)
2505 {
2506 for (offset_type i = 0; i < n_elements; i += 2)
2507 {
2508 gdb_static_assert (sizeof (ULONGEST) >= 8);
2509
2510 sect_offset sect_off
2511 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2512 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2513 cu_list += 2 * 8;
2514
2515 dwarf2_per_cu_data *per_cu
2516 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2517 sect_off, length);
2518 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
2519 }
2520 }
2521
2522 /* Read the CU list from the mapped index, and use it to create all
2523 the CU objects for this objfile. */
2524
2525 static void
2526 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2527 const gdb_byte *cu_list, offset_type cu_list_elements,
2528 const gdb_byte *dwz_list, offset_type dwz_elements)
2529 {
2530 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
2531 dwarf2_per_objfile->per_bfd->all_comp_units.reserve
2532 ((cu_list_elements + dwz_elements) / 2);
2533
2534 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2535 &dwarf2_per_objfile->per_bfd->info, 0);
2536
2537 if (dwz_elements == 0)
2538 return;
2539
2540 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
2541 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2542 &dwz->info, 1);
2543 }
2544
2545 /* Create the signatured type hash table from the index. */
2546
2547 static void
2548 create_signatured_type_table_from_index
2549 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2550 struct dwarf2_section_info *section,
2551 const gdb_byte *bytes,
2552 offset_type elements)
2553 {
2554 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2555 dwarf2_per_objfile->per_bfd->all_type_units.reserve (elements / 3);
2556
2557 htab_up sig_types_hash = allocate_signatured_type_table ();
2558
2559 for (offset_type i = 0; i < elements; i += 3)
2560 {
2561 struct signatured_type *sig_type;
2562 ULONGEST signature;
2563 void **slot;
2564 cu_offset type_offset_in_tu;
2565
2566 gdb_static_assert (sizeof (ULONGEST) >= 8);
2567 sect_offset sect_off
2568 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2569 type_offset_in_tu
2570 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2571 BFD_ENDIAN_LITTLE);
2572 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2573 bytes += 3 * 8;
2574
2575 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2576 sig_type->signature = signature;
2577 sig_type->type_offset_in_tu = type_offset_in_tu;
2578 sig_type->per_cu.is_debug_types = 1;
2579 sig_type->per_cu.section = section;
2580 sig_type->per_cu.sect_off = sect_off;
2581 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2582 sig_type->per_cu.v.quick
2583 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2584 struct dwarf2_per_cu_quick_data);
2585
2586 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2587 *slot = sig_type;
2588
2589 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2590 }
2591
2592 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2593 }
2594
2595 /* Create the signatured type hash table from .debug_names. */
2596
2597 static void
2598 create_signatured_type_table_from_debug_names
2599 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2600 const mapped_debug_names &map,
2601 struct dwarf2_section_info *section,
2602 struct dwarf2_section_info *abbrev_section)
2603 {
2604 struct objfile *objfile = dwarf2_per_objfile->objfile;
2605
2606 section->read (objfile);
2607 abbrev_section->read (objfile);
2608
2609 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2610 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2611
2612 htab_up sig_types_hash = allocate_signatured_type_table ();
2613
2614 for (uint32_t i = 0; i < map.tu_count; ++i)
2615 {
2616 struct signatured_type *sig_type;
2617 void **slot;
2618
2619 sect_offset sect_off
2620 = (sect_offset) (extract_unsigned_integer
2621 (map.tu_table_reordered + i * map.offset_size,
2622 map.offset_size,
2623 map.dwarf5_byte_order));
2624
2625 comp_unit_head cu_header;
2626 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2627 abbrev_section,
2628 section->buffer + to_underlying (sect_off),
2629 rcuh_kind::TYPE);
2630
2631 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2632 sig_type->signature = cu_header.signature;
2633 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2634 sig_type->per_cu.is_debug_types = 1;
2635 sig_type->per_cu.section = section;
2636 sig_type->per_cu.sect_off = sect_off;
2637 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2638 sig_type->per_cu.v.quick
2639 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2640 struct dwarf2_per_cu_quick_data);
2641
2642 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2643 *slot = sig_type;
2644
2645 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2646 }
2647
2648 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2649 }
2650
2651 /* Read the address map data from the mapped index, and use it to
2652 populate the objfile's psymtabs_addrmap. */
2653
2654 static void
2655 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2656 struct mapped_index *index)
2657 {
2658 struct objfile *objfile = dwarf2_per_objfile->objfile;
2659 struct gdbarch *gdbarch = objfile->arch ();
2660 const gdb_byte *iter, *end;
2661 struct addrmap *mutable_map;
2662 CORE_ADDR baseaddr;
2663
2664 auto_obstack temp_obstack;
2665
2666 mutable_map = addrmap_create_mutable (&temp_obstack);
2667
2668 iter = index->address_table.data ();
2669 end = iter + index->address_table.size ();
2670
2671 baseaddr = objfile->text_section_offset ();
2672
2673 while (iter < end)
2674 {
2675 ULONGEST hi, lo, cu_index;
2676 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2677 iter += 8;
2678 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2679 iter += 8;
2680 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2681 iter += 4;
2682
2683 if (lo > hi)
2684 {
2685 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2686 hex_string (lo), hex_string (hi));
2687 continue;
2688 }
2689
2690 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
2691 {
2692 complaint (_(".gdb_index address table has invalid CU number %u"),
2693 (unsigned) cu_index);
2694 continue;
2695 }
2696
2697 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2698 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2699 addrmap_set_empty (mutable_map, lo, hi - 1,
2700 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
2701 }
2702
2703 objfile->partial_symtabs->psymtabs_addrmap
2704 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2705 }
2706
2707 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2708 populate the objfile's psymtabs_addrmap. */
2709
2710 static void
2711 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2712 struct dwarf2_section_info *section)
2713 {
2714 struct objfile *objfile = dwarf2_per_objfile->objfile;
2715 bfd *abfd = objfile->obfd;
2716 struct gdbarch *gdbarch = objfile->arch ();
2717 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2718
2719 auto_obstack temp_obstack;
2720 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2721
2722 std::unordered_map<sect_offset,
2723 dwarf2_per_cu_data *,
2724 gdb::hash_enum<sect_offset>>
2725 debug_info_offset_to_per_cu;
2726 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
2727 {
2728 const auto insertpair
2729 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2730 if (!insertpair.second)
2731 {
2732 warning (_("Section .debug_aranges in %s has duplicate "
2733 "debug_info_offset %s, ignoring .debug_aranges."),
2734 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2735 return;
2736 }
2737 }
2738
2739 section->read (objfile);
2740
2741 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2742
2743 const gdb_byte *addr = section->buffer;
2744
2745 while (addr < section->buffer + section->size)
2746 {
2747 const gdb_byte *const entry_addr = addr;
2748 unsigned int bytes_read;
2749
2750 const LONGEST entry_length = read_initial_length (abfd, addr,
2751 &bytes_read);
2752 addr += bytes_read;
2753
2754 const gdb_byte *const entry_end = addr + entry_length;
2755 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2756 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2757 if (addr + entry_length > section->buffer + section->size)
2758 {
2759 warning (_("Section .debug_aranges in %s entry at offset %s "
2760 "length %s exceeds section length %s, "
2761 "ignoring .debug_aranges."),
2762 objfile_name (objfile),
2763 plongest (entry_addr - section->buffer),
2764 plongest (bytes_read + entry_length),
2765 pulongest (section->size));
2766 return;
2767 }
2768
2769 /* The version number. */
2770 const uint16_t version = read_2_bytes (abfd, addr);
2771 addr += 2;
2772 if (version != 2)
2773 {
2774 warning (_("Section .debug_aranges in %s entry at offset %s "
2775 "has unsupported version %d, ignoring .debug_aranges."),
2776 objfile_name (objfile),
2777 plongest (entry_addr - section->buffer), version);
2778 return;
2779 }
2780
2781 const uint64_t debug_info_offset
2782 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2783 addr += offset_size;
2784 const auto per_cu_it
2785 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2786 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2787 {
2788 warning (_("Section .debug_aranges in %s entry at offset %s "
2789 "debug_info_offset %s does not exists, "
2790 "ignoring .debug_aranges."),
2791 objfile_name (objfile),
2792 plongest (entry_addr - section->buffer),
2793 pulongest (debug_info_offset));
2794 return;
2795 }
2796 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2797
2798 const uint8_t address_size = *addr++;
2799 if (address_size < 1 || address_size > 8)
2800 {
2801 warning (_("Section .debug_aranges in %s entry at offset %s "
2802 "address_size %u is invalid, ignoring .debug_aranges."),
2803 objfile_name (objfile),
2804 plongest (entry_addr - section->buffer), address_size);
2805 return;
2806 }
2807
2808 const uint8_t segment_selector_size = *addr++;
2809 if (segment_selector_size != 0)
2810 {
2811 warning (_("Section .debug_aranges in %s entry at offset %s "
2812 "segment_selector_size %u is not supported, "
2813 "ignoring .debug_aranges."),
2814 objfile_name (objfile),
2815 plongest (entry_addr - section->buffer),
2816 segment_selector_size);
2817 return;
2818 }
2819
2820 /* Must pad to an alignment boundary that is twice the address
2821 size. It is undocumented by the DWARF standard but GCC does
2822 use it. */
2823 for (size_t padding = ((-(addr - section->buffer))
2824 & (2 * address_size - 1));
2825 padding > 0; padding--)
2826 if (*addr++ != 0)
2827 {
2828 warning (_("Section .debug_aranges in %s entry at offset %s "
2829 "padding is not zero, ignoring .debug_aranges."),
2830 objfile_name (objfile),
2831 plongest (entry_addr - section->buffer));
2832 return;
2833 }
2834
2835 for (;;)
2836 {
2837 if (addr + 2 * address_size > entry_end)
2838 {
2839 warning (_("Section .debug_aranges in %s entry at offset %s "
2840 "address list is not properly terminated, "
2841 "ignoring .debug_aranges."),
2842 objfile_name (objfile),
2843 plongest (entry_addr - section->buffer));
2844 return;
2845 }
2846 ULONGEST start = extract_unsigned_integer (addr, address_size,
2847 dwarf5_byte_order);
2848 addr += address_size;
2849 ULONGEST length = extract_unsigned_integer (addr, address_size,
2850 dwarf5_byte_order);
2851 addr += address_size;
2852 if (start == 0 && length == 0)
2853 break;
2854 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
2855 {
2856 /* Symbol was eliminated due to a COMDAT group. */
2857 continue;
2858 }
2859 ULONGEST end = start + length;
2860 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2861 - baseaddr);
2862 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2863 - baseaddr);
2864 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2865 }
2866 }
2867
2868 objfile->partial_symtabs->psymtabs_addrmap
2869 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2870 }
2871
2872 /* Find a slot in the mapped index INDEX for the object named NAME.
2873 If NAME is found, set *VEC_OUT to point to the CU vector in the
2874 constant pool and return true. If NAME cannot be found, return
2875 false. */
2876
2877 static bool
2878 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2879 offset_type **vec_out)
2880 {
2881 offset_type hash;
2882 offset_type slot, step;
2883 int (*cmp) (const char *, const char *);
2884
2885 gdb::unique_xmalloc_ptr<char> without_params;
2886 if (current_language->la_language == language_cplus
2887 || current_language->la_language == language_fortran
2888 || current_language->la_language == language_d)
2889 {
2890 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2891 not contain any. */
2892
2893 if (strchr (name, '(') != NULL)
2894 {
2895 without_params = cp_remove_params (name);
2896
2897 if (without_params != NULL)
2898 name = without_params.get ();
2899 }
2900 }
2901
2902 /* Index version 4 did not support case insensitive searches. But the
2903 indices for case insensitive languages are built in lowercase, therefore
2904 simulate our NAME being searched is also lowercased. */
2905 hash = mapped_index_string_hash ((index->version == 4
2906 && case_sensitivity == case_sensitive_off
2907 ? 5 : index->version),
2908 name);
2909
2910 slot = hash & (index->symbol_table.size () - 1);
2911 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2912 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2913
2914 for (;;)
2915 {
2916 const char *str;
2917
2918 const auto &bucket = index->symbol_table[slot];
2919 if (bucket.name == 0 && bucket.vec == 0)
2920 return false;
2921
2922 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2923 if (!cmp (name, str))
2924 {
2925 *vec_out = (offset_type *) (index->constant_pool
2926 + MAYBE_SWAP (bucket.vec));
2927 return true;
2928 }
2929
2930 slot = (slot + step) & (index->symbol_table.size () - 1);
2931 }
2932 }
2933
2934 /* A helper function that reads the .gdb_index from BUFFER and fills
2935 in MAP. FILENAME is the name of the file containing the data;
2936 it is used for error reporting. DEPRECATED_OK is true if it is
2937 ok to use deprecated sections.
2938
2939 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2940 out parameters that are filled in with information about the CU and
2941 TU lists in the section.
2942
2943 Returns true if all went well, false otherwise. */
2944
2945 static bool
2946 read_gdb_index_from_buffer (const char *filename,
2947 bool deprecated_ok,
2948 gdb::array_view<const gdb_byte> buffer,
2949 struct mapped_index *map,
2950 const gdb_byte **cu_list,
2951 offset_type *cu_list_elements,
2952 const gdb_byte **types_list,
2953 offset_type *types_list_elements)
2954 {
2955 const gdb_byte *addr = &buffer[0];
2956
2957 /* Version check. */
2958 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2959 /* Versions earlier than 3 emitted every copy of a psymbol. This
2960 causes the index to behave very poorly for certain requests. Version 3
2961 contained incomplete addrmap. So, it seems better to just ignore such
2962 indices. */
2963 if (version < 4)
2964 {
2965 static int warning_printed = 0;
2966 if (!warning_printed)
2967 {
2968 warning (_("Skipping obsolete .gdb_index section in %s."),
2969 filename);
2970 warning_printed = 1;
2971 }
2972 return 0;
2973 }
2974 /* Index version 4 uses a different hash function than index version
2975 5 and later.
2976
2977 Versions earlier than 6 did not emit psymbols for inlined
2978 functions. Using these files will cause GDB not to be able to
2979 set breakpoints on inlined functions by name, so we ignore these
2980 indices unless the user has done
2981 "set use-deprecated-index-sections on". */
2982 if (version < 6 && !deprecated_ok)
2983 {
2984 static int warning_printed = 0;
2985 if (!warning_printed)
2986 {
2987 warning (_("\
2988 Skipping deprecated .gdb_index section in %s.\n\
2989 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2990 to use the section anyway."),
2991 filename);
2992 warning_printed = 1;
2993 }
2994 return 0;
2995 }
2996 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2997 of the TU (for symbols coming from TUs),
2998 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2999 Plus gold-generated indices can have duplicate entries for global symbols,
3000 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3001 These are just performance bugs, and we can't distinguish gdb-generated
3002 indices from gold-generated ones, so issue no warning here. */
3003
3004 /* Indexes with higher version than the one supported by GDB may be no
3005 longer backward compatible. */
3006 if (version > 8)
3007 return 0;
3008
3009 map->version = version;
3010
3011 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3012
3013 int i = 0;
3014 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3015 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3016 / 8);
3017 ++i;
3018
3019 *types_list = addr + MAYBE_SWAP (metadata[i]);
3020 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3021 - MAYBE_SWAP (metadata[i]))
3022 / 8);
3023 ++i;
3024
3025 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3026 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3027 map->address_table
3028 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3029 ++i;
3030
3031 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3032 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3033 map->symbol_table
3034 = gdb::array_view<mapped_index::symbol_table_slot>
3035 ((mapped_index::symbol_table_slot *) symbol_table,
3036 (mapped_index::symbol_table_slot *) symbol_table_end);
3037
3038 ++i;
3039 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3040
3041 return 1;
3042 }
3043
3044 /* Callback types for dwarf2_read_gdb_index. */
3045
3046 typedef gdb::function_view
3047 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3048 get_gdb_index_contents_ftype;
3049 typedef gdb::function_view
3050 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3051 get_gdb_index_contents_dwz_ftype;
3052
3053 /* Read .gdb_index. If everything went ok, initialize the "quick"
3054 elements of all the CUs and return 1. Otherwise, return 0. */
3055
3056 static int
3057 dwarf2_read_gdb_index
3058 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3059 get_gdb_index_contents_ftype get_gdb_index_contents,
3060 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3061 {
3062 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3063 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3064 struct dwz_file *dwz;
3065 struct objfile *objfile = dwarf2_per_objfile->objfile;
3066
3067 gdb::array_view<const gdb_byte> main_index_contents
3068 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
3069
3070 if (main_index_contents.empty ())
3071 return 0;
3072
3073 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3074 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3075 use_deprecated_index_sections,
3076 main_index_contents, map.get (), &cu_list,
3077 &cu_list_elements, &types_list,
3078 &types_list_elements))
3079 return 0;
3080
3081 /* Don't use the index if it's empty. */
3082 if (map->symbol_table.empty ())
3083 return 0;
3084
3085 /* If there is a .dwz file, read it so we can get its CU list as
3086 well. */
3087 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
3088 if (dwz != NULL)
3089 {
3090 struct mapped_index dwz_map;
3091 const gdb_byte *dwz_types_ignore;
3092 offset_type dwz_types_elements_ignore;
3093
3094 gdb::array_view<const gdb_byte> dwz_index_content
3095 = get_gdb_index_contents_dwz (objfile, dwz);
3096
3097 if (dwz_index_content.empty ())
3098 return 0;
3099
3100 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3101 1, dwz_index_content, &dwz_map,
3102 &dwz_list, &dwz_list_elements,
3103 &dwz_types_ignore,
3104 &dwz_types_elements_ignore))
3105 {
3106 warning (_("could not read '.gdb_index' section from %s; skipping"),
3107 bfd_get_filename (dwz->dwz_bfd.get ()));
3108 return 0;
3109 }
3110 }
3111
3112 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3113 dwz_list, dwz_list_elements);
3114
3115 if (types_list_elements)
3116 {
3117 /* We can only handle a single .debug_types when we have an
3118 index. */
3119 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
3120 return 0;
3121
3122 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
3123
3124 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3125 types_list, types_list_elements);
3126 }
3127
3128 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3129
3130 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3131 dwarf2_per_objfile->per_bfd->using_index = 1;
3132 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3133 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
3134
3135 return 1;
3136 }
3137
3138 /* die_reader_func for dw2_get_file_names. */
3139
3140 static void
3141 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3142 const gdb_byte *info_ptr,
3143 struct die_info *comp_unit_die)
3144 {
3145 struct dwarf2_cu *cu = reader->cu;
3146 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3147 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3148 struct dwarf2_per_cu_data *lh_cu;
3149 struct attribute *attr;
3150 void **slot;
3151 struct quick_file_names *qfn;
3152
3153 gdb_assert (! this_cu->is_debug_types);
3154
3155 /* Our callers never want to match partial units -- instead they
3156 will match the enclosing full CU. */
3157 if (comp_unit_die->tag == DW_TAG_partial_unit)
3158 {
3159 this_cu->v.quick->no_file_data = 1;
3160 return;
3161 }
3162
3163 lh_cu = this_cu;
3164 slot = NULL;
3165
3166 line_header_up lh;
3167 sect_offset line_offset {};
3168
3169 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3170 if (attr != nullptr)
3171 {
3172 struct quick_file_names find_entry;
3173
3174 line_offset = (sect_offset) DW_UNSND (attr);
3175
3176 /* We may have already read in this line header (TU line header sharing).
3177 If we have we're done. */
3178 find_entry.hash.dwo_unit = cu->dwo_unit;
3179 find_entry.hash.line_sect_off = line_offset;
3180 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3181 &find_entry, INSERT);
3182 if (*slot != NULL)
3183 {
3184 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3185 return;
3186 }
3187
3188 lh = dwarf_decode_line_header (line_offset, cu);
3189 }
3190 if (lh == NULL)
3191 {
3192 lh_cu->v.quick->no_file_data = 1;
3193 return;
3194 }
3195
3196 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
3197 qfn->hash.dwo_unit = cu->dwo_unit;
3198 qfn->hash.line_sect_off = line_offset;
3199 gdb_assert (slot != NULL);
3200 *slot = qfn;
3201
3202 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3203
3204 int offset = 0;
3205 if (strcmp (fnd.name, "<unknown>") != 0)
3206 ++offset;
3207
3208 qfn->num_file_names = offset + lh->file_names_size ();
3209 qfn->file_names =
3210 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
3211 qfn->num_file_names);
3212 if (offset != 0)
3213 qfn->file_names[0] = xstrdup (fnd.name);
3214 for (int i = 0; i < lh->file_names_size (); ++i)
3215 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3216 fnd.comp_dir).release ();
3217 qfn->real_names = NULL;
3218
3219 lh_cu->v.quick->file_names = qfn;
3220 }
3221
3222 /* A helper for the "quick" functions which attempts to read the line
3223 table for THIS_CU. */
3224
3225 static struct quick_file_names *
3226 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3227 dwarf2_per_objfile *per_objfile)
3228 {
3229 /* This should never be called for TUs. */
3230 gdb_assert (! this_cu->is_debug_types);
3231 /* Nor type unit groups. */
3232 gdb_assert (! this_cu->type_unit_group_p ());
3233
3234 if (this_cu->v.quick->file_names != NULL)
3235 return this_cu->v.quick->file_names;
3236 /* If we know there is no line data, no point in looking again. */
3237 if (this_cu->v.quick->no_file_data)
3238 return NULL;
3239
3240 cutu_reader reader (this_cu, per_objfile);
3241 if (!reader.dummy_p)
3242 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3243
3244 if (this_cu->v.quick->no_file_data)
3245 return NULL;
3246 return this_cu->v.quick->file_names;
3247 }
3248
3249 /* A helper for the "quick" functions which computes and caches the
3250 real path for a given file name from the line table. */
3251
3252 static const char *
3253 dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
3254 struct quick_file_names *qfn, int index)
3255 {
3256 if (qfn->real_names == NULL)
3257 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
3258 qfn->num_file_names, const char *);
3259
3260 if (qfn->real_names[index] == NULL)
3261 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3262
3263 return qfn->real_names[index];
3264 }
3265
3266 static struct symtab *
3267 dw2_find_last_source_symtab (struct objfile *objfile)
3268 {
3269 struct dwarf2_per_objfile *dwarf2_per_objfile
3270 = get_dwarf2_per_objfile (objfile);
3271 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
3272 compunit_symtab *cust
3273 = dw2_instantiate_symtab (dwarf_cu, dwarf2_per_objfile, false);
3274
3275 if (cust == NULL)
3276 return NULL;
3277
3278 return compunit_primary_filetab (cust);
3279 }
3280
3281 /* Traversal function for dw2_forget_cached_source_info. */
3282
3283 static int
3284 dw2_free_cached_file_names (void **slot, void *info)
3285 {
3286 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3287
3288 if (file_data->real_names)
3289 {
3290 int i;
3291
3292 for (i = 0; i < file_data->num_file_names; ++i)
3293 {
3294 xfree ((void*) file_data->real_names[i]);
3295 file_data->real_names[i] = NULL;
3296 }
3297 }
3298
3299 return 1;
3300 }
3301
3302 static void
3303 dw2_forget_cached_source_info (struct objfile *objfile)
3304 {
3305 struct dwarf2_per_objfile *dwarf2_per_objfile
3306 = get_dwarf2_per_objfile (objfile);
3307
3308 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3309 dw2_free_cached_file_names, NULL);
3310 }
3311
3312 /* Helper function for dw2_map_symtabs_matching_filename that expands
3313 the symtabs and calls the iterator. */
3314
3315 static int
3316 dw2_map_expand_apply (struct objfile *objfile,
3317 struct dwarf2_per_cu_data *per_cu,
3318 const char *name, const char *real_path,
3319 gdb::function_view<bool (symtab *)> callback)
3320 {
3321 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3322
3323 /* Don't visit already-expanded CUs. */
3324 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3325 if (per_objfile->symtab_set_p (per_cu))
3326 return 0;
3327
3328 /* This may expand more than one symtab, and we want to iterate over
3329 all of them. */
3330 dw2_instantiate_symtab (per_cu, per_objfile, false);
3331
3332 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3333 last_made, callback);
3334 }
3335
3336 /* Implementation of the map_symtabs_matching_filename method. */
3337
3338 static bool
3339 dw2_map_symtabs_matching_filename
3340 (struct objfile *objfile, const char *name, const char *real_path,
3341 gdb::function_view<bool (symtab *)> callback)
3342 {
3343 const char *name_basename = lbasename (name);
3344 struct dwarf2_per_objfile *dwarf2_per_objfile
3345 = get_dwarf2_per_objfile (objfile);
3346
3347 /* The rule is CUs specify all the files, including those used by
3348 any TU, so there's no need to scan TUs here. */
3349
3350 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3351 {
3352 /* We only need to look at symtabs not already expanded. */
3353 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3354 continue;
3355
3356 quick_file_names *file_data
3357 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3358 if (file_data == NULL)
3359 continue;
3360
3361 for (int j = 0; j < file_data->num_file_names; ++j)
3362 {
3363 const char *this_name = file_data->file_names[j];
3364 const char *this_real_name;
3365
3366 if (compare_filenames_for_search (this_name, name))
3367 {
3368 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3369 callback))
3370 return true;
3371 continue;
3372 }
3373
3374 /* Before we invoke realpath, which can get expensive when many
3375 files are involved, do a quick comparison of the basenames. */
3376 if (! basenames_may_differ
3377 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3378 continue;
3379
3380 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3381 file_data, j);
3382 if (compare_filenames_for_search (this_real_name, name))
3383 {
3384 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3385 callback))
3386 return true;
3387 continue;
3388 }
3389
3390 if (real_path != NULL)
3391 {
3392 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3393 gdb_assert (IS_ABSOLUTE_PATH (name));
3394 if (this_real_name != NULL
3395 && FILENAME_CMP (real_path, this_real_name) == 0)
3396 {
3397 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3398 callback))
3399 return true;
3400 continue;
3401 }
3402 }
3403 }
3404 }
3405
3406 return false;
3407 }
3408
3409 /* Struct used to manage iterating over all CUs looking for a symbol. */
3410
3411 struct dw2_symtab_iterator
3412 {
3413 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3414 struct dwarf2_per_objfile *dwarf2_per_objfile;
3415 /* If set, only look for symbols that match that block. Valid values are
3416 GLOBAL_BLOCK and STATIC_BLOCK. */
3417 gdb::optional<block_enum> block_index;
3418 /* The kind of symbol we're looking for. */
3419 domain_enum domain;
3420 /* The list of CUs from the index entry of the symbol,
3421 or NULL if not found. */
3422 offset_type *vec;
3423 /* The next element in VEC to look at. */
3424 int next;
3425 /* The number of elements in VEC, or zero if there is no match. */
3426 int length;
3427 /* Have we seen a global version of the symbol?
3428 If so we can ignore all further global instances.
3429 This is to work around gold/15646, inefficient gold-generated
3430 indices. */
3431 int global_seen;
3432 };
3433
3434 /* Initialize the index symtab iterator ITER. */
3435
3436 static void
3437 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3438 struct dwarf2_per_objfile *dwarf2_per_objfile,
3439 gdb::optional<block_enum> block_index,
3440 domain_enum domain,
3441 const char *name)
3442 {
3443 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3444 iter->block_index = block_index;
3445 iter->domain = domain;
3446 iter->next = 0;
3447 iter->global_seen = 0;
3448
3449 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
3450
3451 /* index is NULL if OBJF_READNOW. */
3452 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3453 iter->length = MAYBE_SWAP (*iter->vec);
3454 else
3455 {
3456 iter->vec = NULL;
3457 iter->length = 0;
3458 }
3459 }
3460
3461 /* Return the next matching CU or NULL if there are no more. */
3462
3463 static struct dwarf2_per_cu_data *
3464 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3465 {
3466 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3467
3468 for ( ; iter->next < iter->length; ++iter->next)
3469 {
3470 offset_type cu_index_and_attrs =
3471 MAYBE_SWAP (iter->vec[iter->next + 1]);
3472 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3473 gdb_index_symbol_kind symbol_kind =
3474 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3475 /* Only check the symbol attributes if they're present.
3476 Indices prior to version 7 don't record them,
3477 and indices >= 7 may elide them for certain symbols
3478 (gold does this). */
3479 int attrs_valid =
3480 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
3481 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3482
3483 /* Don't crash on bad data. */
3484 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3485 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3486 {
3487 complaint (_(".gdb_index entry has bad CU index"
3488 " [in module %s]"),
3489 objfile_name (dwarf2_per_objfile->objfile));
3490 continue;
3491 }
3492
3493 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3494
3495 /* Skip if already read in. */
3496 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3497 continue;
3498
3499 /* Check static vs global. */
3500 if (attrs_valid)
3501 {
3502 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3503
3504 if (iter->block_index.has_value ())
3505 {
3506 bool want_static = *iter->block_index == STATIC_BLOCK;
3507
3508 if (is_static != want_static)
3509 continue;
3510 }
3511
3512 /* Work around gold/15646. */
3513 if (!is_static && iter->global_seen)
3514 continue;
3515 if (!is_static)
3516 iter->global_seen = 1;
3517 }
3518
3519 /* Only check the symbol's kind if it has one. */
3520 if (attrs_valid)
3521 {
3522 switch (iter->domain)
3523 {
3524 case VAR_DOMAIN:
3525 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3526 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3527 /* Some types are also in VAR_DOMAIN. */
3528 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3529 continue;
3530 break;
3531 case STRUCT_DOMAIN:
3532 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3533 continue;
3534 break;
3535 case LABEL_DOMAIN:
3536 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3537 continue;
3538 break;
3539 case MODULE_DOMAIN:
3540 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3541 continue;
3542 break;
3543 default:
3544 break;
3545 }
3546 }
3547
3548 ++iter->next;
3549 return per_cu;
3550 }
3551
3552 return NULL;
3553 }
3554
3555 static struct compunit_symtab *
3556 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3557 const char *name, domain_enum domain)
3558 {
3559 struct compunit_symtab *stab_best = NULL;
3560 struct dwarf2_per_objfile *dwarf2_per_objfile
3561 = get_dwarf2_per_objfile (objfile);
3562
3563 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3564
3565 struct dw2_symtab_iterator iter;
3566 struct dwarf2_per_cu_data *per_cu;
3567
3568 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3569
3570 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3571 {
3572 struct symbol *sym, *with_opaque = NULL;
3573 struct compunit_symtab *stab
3574 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3575 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3576 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3577
3578 sym = block_find_symbol (block, name, domain,
3579 block_find_non_opaque_type_preferred,
3580 &with_opaque);
3581
3582 /* Some caution must be observed with overloaded functions
3583 and methods, since the index will not contain any overload
3584 information (but NAME might contain it). */
3585
3586 if (sym != NULL
3587 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3588 return stab;
3589 if (with_opaque != NULL
3590 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3591 stab_best = stab;
3592
3593 /* Keep looking through other CUs. */
3594 }
3595
3596 return stab_best;
3597 }
3598
3599 static void
3600 dw2_print_stats (struct objfile *objfile)
3601 {
3602 struct dwarf2_per_objfile *dwarf2_per_objfile
3603 = get_dwarf2_per_objfile (objfile);
3604 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3605 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3606 int count = 0;
3607
3608 for (int i = 0; i < total; ++i)
3609 {
3610 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3611
3612 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
3613 ++count;
3614 }
3615 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3616 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3617 }
3618
3619 /* This dumps minimal information about the index.
3620 It is called via "mt print objfiles".
3621 One use is to verify .gdb_index has been loaded by the
3622 gdb.dwarf2/gdb-index.exp testcase. */
3623
3624 static void
3625 dw2_dump (struct objfile *objfile)
3626 {
3627 struct dwarf2_per_objfile *dwarf2_per_objfile
3628 = get_dwarf2_per_objfile (objfile);
3629
3630 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
3631 printf_filtered (".gdb_index:");
3632 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
3633 {
3634 printf_filtered (" version %d\n",
3635 dwarf2_per_objfile->per_bfd->index_table->version);
3636 }
3637 else
3638 printf_filtered (" faked for \"readnow\"\n");
3639 printf_filtered ("\n");
3640 }
3641
3642 static void
3643 dw2_expand_symtabs_for_function (struct objfile *objfile,
3644 const char *func_name)
3645 {
3646 struct dwarf2_per_objfile *dwarf2_per_objfile
3647 = get_dwarf2_per_objfile (objfile);
3648
3649 struct dw2_symtab_iterator iter;
3650 struct dwarf2_per_cu_data *per_cu;
3651
3652 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3653
3654 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3655 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3656
3657 }
3658
3659 static void
3660 dw2_expand_all_symtabs (struct objfile *objfile)
3661 {
3662 struct dwarf2_per_objfile *dwarf2_per_objfile
3663 = get_dwarf2_per_objfile (objfile);
3664 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3665 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3666
3667 for (int i = 0; i < total_units; ++i)
3668 {
3669 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3670
3671 /* We don't want to directly expand a partial CU, because if we
3672 read it with the wrong language, then assertion failures can
3673 be triggered later on. See PR symtab/23010. So, tell
3674 dw2_instantiate_symtab to skip partial CUs -- any important
3675 partial CU will be read via DW_TAG_imported_unit anyway. */
3676 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, true);
3677 }
3678 }
3679
3680 static void
3681 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3682 const char *fullname)
3683 {
3684 struct dwarf2_per_objfile *dwarf2_per_objfile
3685 = get_dwarf2_per_objfile (objfile);
3686
3687 /* We don't need to consider type units here.
3688 This is only called for examining code, e.g. expand_line_sal.
3689 There can be an order of magnitude (or more) more type units
3690 than comp units, and we avoid them if we can. */
3691
3692 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3693 {
3694 /* We only need to look at symtabs not already expanded. */
3695 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3696 continue;
3697
3698 quick_file_names *file_data
3699 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3700 if (file_data == NULL)
3701 continue;
3702
3703 for (int j = 0; j < file_data->num_file_names; ++j)
3704 {
3705 const char *this_fullname = file_data->file_names[j];
3706
3707 if (filename_cmp (this_fullname, fullname) == 0)
3708 {
3709 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3710 break;
3711 }
3712 }
3713 }
3714 }
3715
3716 static void
3717 dw2_expand_symtabs_matching_symbol
3718 (mapped_index_base &index,
3719 const lookup_name_info &lookup_name_in,
3720 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3721 enum search_domain kind,
3722 gdb::function_view<bool (offset_type)> match_callback);
3723
3724 static void
3725 dw2_expand_symtabs_matching_one
3726 (dwarf2_per_cu_data *per_cu,
3727 dwarf2_per_objfile *per_objfile,
3728 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3729 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3730
3731 static void
3732 dw2_map_matching_symbols
3733 (struct objfile *objfile,
3734 const lookup_name_info &name, domain_enum domain,
3735 int global,
3736 gdb::function_view<symbol_found_callback_ftype> callback,
3737 symbol_compare_ftype *ordered_compare)
3738 {
3739 /* Used for Ada. */
3740 struct dwarf2_per_objfile *dwarf2_per_objfile
3741 = get_dwarf2_per_objfile (objfile);
3742
3743 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3744
3745 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
3746 {
3747 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3748 here though if the current language is Ada for a non-Ada objfile
3749 using GNU index. */
3750 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3751
3752 const char *match_name = name.ada ().lookup_name ().c_str ();
3753 auto matcher = [&] (const char *symname)
3754 {
3755 if (ordered_compare == nullptr)
3756 return true;
3757 return ordered_compare (symname, match_name) == 0;
3758 };
3759
3760 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3761 [&] (offset_type namei)
3762 {
3763 struct dw2_symtab_iterator iter;
3764 struct dwarf2_per_cu_data *per_cu;
3765
3766 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3767 match_name);
3768 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3769 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
3770 nullptr);
3771 return true;
3772 });
3773 }
3774 else
3775 {
3776 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3777 proceed assuming all symtabs have been read in. */
3778 }
3779
3780 for (compunit_symtab *cust : objfile->compunits ())
3781 {
3782 const struct block *block;
3783
3784 if (cust == NULL)
3785 continue;
3786 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3787 if (!iterate_over_symbols_terminated (block, name,
3788 domain, callback))
3789 return;
3790 }
3791 }
3792
3793 /* Starting from a search name, return the string that finds the upper
3794 bound of all strings that start with SEARCH_NAME in a sorted name
3795 list. Returns the empty string to indicate that the upper bound is
3796 the end of the list. */
3797
3798 static std::string
3799 make_sort_after_prefix_name (const char *search_name)
3800 {
3801 /* When looking to complete "func", we find the upper bound of all
3802 symbols that start with "func" by looking for where we'd insert
3803 the closest string that would follow "func" in lexicographical
3804 order. Usually, that's "func"-with-last-character-incremented,
3805 i.e. "fund". Mind non-ASCII characters, though. Usually those
3806 will be UTF-8 multi-byte sequences, but we can't be certain.
3807 Especially mind the 0xff character, which is a valid character in
3808 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3809 rule out compilers allowing it in identifiers. Note that
3810 conveniently, strcmp/strcasecmp are specified to compare
3811 characters interpreted as unsigned char. So what we do is treat
3812 the whole string as a base 256 number composed of a sequence of
3813 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3814 to 0, and carries 1 to the following more-significant position.
3815 If the very first character in SEARCH_NAME ends up incremented
3816 and carries/overflows, then the upper bound is the end of the
3817 list. The string after the empty string is also the empty
3818 string.
3819
3820 Some examples of this operation:
3821
3822 SEARCH_NAME => "+1" RESULT
3823
3824 "abc" => "abd"
3825 "ab\xff" => "ac"
3826 "\xff" "a" "\xff" => "\xff" "b"
3827 "\xff" => ""
3828 "\xff\xff" => ""
3829 "" => ""
3830
3831 Then, with these symbols for example:
3832
3833 func
3834 func1
3835 fund
3836
3837 completing "func" looks for symbols between "func" and
3838 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3839 which finds "func" and "func1", but not "fund".
3840
3841 And with:
3842
3843 funcÿ (Latin1 'ÿ' [0xff])
3844 funcÿ1
3845 fund
3846
3847 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3848 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3849
3850 And with:
3851
3852 ÿÿ (Latin1 'ÿ' [0xff])
3853 ÿÿ1
3854
3855 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3856 the end of the list.
3857 */
3858 std::string after = search_name;
3859 while (!after.empty () && (unsigned char) after.back () == 0xff)
3860 after.pop_back ();
3861 if (!after.empty ())
3862 after.back () = (unsigned char) after.back () + 1;
3863 return after;
3864 }
3865
3866 /* See declaration. */
3867
3868 std::pair<std::vector<name_component>::const_iterator,
3869 std::vector<name_component>::const_iterator>
3870 mapped_index_base::find_name_components_bounds
3871 (const lookup_name_info &lookup_name_without_params, language lang) const
3872 {
3873 auto *name_cmp
3874 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3875
3876 const char *lang_name
3877 = lookup_name_without_params.language_lookup_name (lang);
3878
3879 /* Comparison function object for lower_bound that matches against a
3880 given symbol name. */
3881 auto lookup_compare_lower = [&] (const name_component &elem,
3882 const char *name)
3883 {
3884 const char *elem_qualified = this->symbol_name_at (elem.idx);
3885 const char *elem_name = elem_qualified + elem.name_offset;
3886 return name_cmp (elem_name, name) < 0;
3887 };
3888
3889 /* Comparison function object for upper_bound that matches against a
3890 given symbol name. */
3891 auto lookup_compare_upper = [&] (const char *name,
3892 const name_component &elem)
3893 {
3894 const char *elem_qualified = this->symbol_name_at (elem.idx);
3895 const char *elem_name = elem_qualified + elem.name_offset;
3896 return name_cmp (name, elem_name) < 0;
3897 };
3898
3899 auto begin = this->name_components.begin ();
3900 auto end = this->name_components.end ();
3901
3902 /* Find the lower bound. */
3903 auto lower = [&] ()
3904 {
3905 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3906 return begin;
3907 else
3908 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3909 } ();
3910
3911 /* Find the upper bound. */
3912 auto upper = [&] ()
3913 {
3914 if (lookup_name_without_params.completion_mode ())
3915 {
3916 /* In completion mode, we want UPPER to point past all
3917 symbols names that have the same prefix. I.e., with
3918 these symbols, and completing "func":
3919
3920 function << lower bound
3921 function1
3922 other_function << upper bound
3923
3924 We find the upper bound by looking for the insertion
3925 point of "func"-with-last-character-incremented,
3926 i.e. "fund". */
3927 std::string after = make_sort_after_prefix_name (lang_name);
3928 if (after.empty ())
3929 return end;
3930 return std::lower_bound (lower, end, after.c_str (),
3931 lookup_compare_lower);
3932 }
3933 else
3934 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3935 } ();
3936
3937 return {lower, upper};
3938 }
3939
3940 /* See declaration. */
3941
3942 void
3943 mapped_index_base::build_name_components ()
3944 {
3945 if (!this->name_components.empty ())
3946 return;
3947
3948 this->name_components_casing = case_sensitivity;
3949 auto *name_cmp
3950 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3951
3952 /* The code below only knows how to break apart components of C++
3953 symbol names (and other languages that use '::' as
3954 namespace/module separator) and Ada symbol names. */
3955 auto count = this->symbol_name_count ();
3956 for (offset_type idx = 0; idx < count; idx++)
3957 {
3958 if (this->symbol_name_slot_invalid (idx))
3959 continue;
3960
3961 const char *name = this->symbol_name_at (idx);
3962
3963 /* Add each name component to the name component table. */
3964 unsigned int previous_len = 0;
3965
3966 if (strstr (name, "::") != nullptr)
3967 {
3968 for (unsigned int current_len = cp_find_first_component (name);
3969 name[current_len] != '\0';
3970 current_len += cp_find_first_component (name + current_len))
3971 {
3972 gdb_assert (name[current_len] == ':');
3973 this->name_components.push_back ({previous_len, idx});
3974 /* Skip the '::'. */
3975 current_len += 2;
3976 previous_len = current_len;
3977 }
3978 }
3979 else
3980 {
3981 /* Handle the Ada encoded (aka mangled) form here. */
3982 for (const char *iter = strstr (name, "__");
3983 iter != nullptr;
3984 iter = strstr (iter, "__"))
3985 {
3986 this->name_components.push_back ({previous_len, idx});
3987 iter += 2;
3988 previous_len = iter - name;
3989 }
3990 }
3991
3992 this->name_components.push_back ({previous_len, idx});
3993 }
3994
3995 /* Sort name_components elements by name. */
3996 auto name_comp_compare = [&] (const name_component &left,
3997 const name_component &right)
3998 {
3999 const char *left_qualified = this->symbol_name_at (left.idx);
4000 const char *right_qualified = this->symbol_name_at (right.idx);
4001
4002 const char *left_name = left_qualified + left.name_offset;
4003 const char *right_name = right_qualified + right.name_offset;
4004
4005 return name_cmp (left_name, right_name) < 0;
4006 };
4007
4008 std::sort (this->name_components.begin (),
4009 this->name_components.end (),
4010 name_comp_compare);
4011 }
4012
4013 /* Helper for dw2_expand_symtabs_matching that works with a
4014 mapped_index_base instead of the containing objfile. This is split
4015 to a separate function in order to be able to unit test the
4016 name_components matching using a mock mapped_index_base. For each
4017 symbol name that matches, calls MATCH_CALLBACK, passing it the
4018 symbol's index in the mapped_index_base symbol table. */
4019
4020 static void
4021 dw2_expand_symtabs_matching_symbol
4022 (mapped_index_base &index,
4023 const lookup_name_info &lookup_name_in,
4024 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4025 enum search_domain kind,
4026 gdb::function_view<bool (offset_type)> match_callback)
4027 {
4028 lookup_name_info lookup_name_without_params
4029 = lookup_name_in.make_ignore_params ();
4030
4031 /* Build the symbol name component sorted vector, if we haven't
4032 yet. */
4033 index.build_name_components ();
4034
4035 /* The same symbol may appear more than once in the range though.
4036 E.g., if we're looking for symbols that complete "w", and we have
4037 a symbol named "w1::w2", we'll find the two name components for
4038 that same symbol in the range. To be sure we only call the
4039 callback once per symbol, we first collect the symbol name
4040 indexes that matched in a temporary vector and ignore
4041 duplicates. */
4042 std::vector<offset_type> matches;
4043
4044 struct name_and_matcher
4045 {
4046 symbol_name_matcher_ftype *matcher;
4047 const char *name;
4048
4049 bool operator== (const name_and_matcher &other) const
4050 {
4051 return matcher == other.matcher && strcmp (name, other.name) == 0;
4052 }
4053 };
4054
4055 /* A vector holding all the different symbol name matchers, for all
4056 languages. */
4057 std::vector<name_and_matcher> matchers;
4058
4059 for (int i = 0; i < nr_languages; i++)
4060 {
4061 enum language lang_e = (enum language) i;
4062
4063 const language_defn *lang = language_def (lang_e);
4064 symbol_name_matcher_ftype *name_matcher
4065 = get_symbol_name_matcher (lang, lookup_name_without_params);
4066
4067 name_and_matcher key {
4068 name_matcher,
4069 lookup_name_without_params.language_lookup_name (lang_e)
4070 };
4071
4072 /* Don't insert the same comparison routine more than once.
4073 Note that we do this linear walk. This is not a problem in
4074 practice because the number of supported languages is
4075 low. */
4076 if (std::find (matchers.begin (), matchers.end (), key)
4077 != matchers.end ())
4078 continue;
4079 matchers.push_back (std::move (key));
4080
4081 auto bounds
4082 = index.find_name_components_bounds (lookup_name_without_params,
4083 lang_e);
4084
4085 /* Now for each symbol name in range, check to see if we have a name
4086 match, and if so, call the MATCH_CALLBACK callback. */
4087
4088 for (; bounds.first != bounds.second; ++bounds.first)
4089 {
4090 const char *qualified = index.symbol_name_at (bounds.first->idx);
4091
4092 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4093 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4094 continue;
4095
4096 matches.push_back (bounds.first->idx);
4097 }
4098 }
4099
4100 std::sort (matches.begin (), matches.end ());
4101
4102 /* Finally call the callback, once per match. */
4103 ULONGEST prev = -1;
4104 for (offset_type idx : matches)
4105 {
4106 if (prev != idx)
4107 {
4108 if (!match_callback (idx))
4109 break;
4110 prev = idx;
4111 }
4112 }
4113
4114 /* Above we use a type wider than idx's for 'prev', since 0 and
4115 (offset_type)-1 are both possible values. */
4116 static_assert (sizeof (prev) > sizeof (offset_type), "");
4117 }
4118
4119 #if GDB_SELF_TEST
4120
4121 namespace selftests { namespace dw2_expand_symtabs_matching {
4122
4123 /* A mock .gdb_index/.debug_names-like name index table, enough to
4124 exercise dw2_expand_symtabs_matching_symbol, which works with the
4125 mapped_index_base interface. Builds an index from the symbol list
4126 passed as parameter to the constructor. */
4127 class mock_mapped_index : public mapped_index_base
4128 {
4129 public:
4130 mock_mapped_index (gdb::array_view<const char *> symbols)
4131 : m_symbol_table (symbols)
4132 {}
4133
4134 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4135
4136 /* Return the number of names in the symbol table. */
4137 size_t symbol_name_count () const override
4138 {
4139 return m_symbol_table.size ();
4140 }
4141
4142 /* Get the name of the symbol at IDX in the symbol table. */
4143 const char *symbol_name_at (offset_type idx) const override
4144 {
4145 return m_symbol_table[idx];
4146 }
4147
4148 private:
4149 gdb::array_view<const char *> m_symbol_table;
4150 };
4151
4152 /* Convenience function that converts a NULL pointer to a "<null>"
4153 string, to pass to print routines. */
4154
4155 static const char *
4156 string_or_null (const char *str)
4157 {
4158 return str != NULL ? str : "<null>";
4159 }
4160
4161 /* Check if a lookup_name_info built from
4162 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4163 index. EXPECTED_LIST is the list of expected matches, in expected
4164 matching order. If no match expected, then an empty list is
4165 specified. Returns true on success. On failure prints a warning
4166 indicating the file:line that failed, and returns false. */
4167
4168 static bool
4169 check_match (const char *file, int line,
4170 mock_mapped_index &mock_index,
4171 const char *name, symbol_name_match_type match_type,
4172 bool completion_mode,
4173 std::initializer_list<const char *> expected_list)
4174 {
4175 lookup_name_info lookup_name (name, match_type, completion_mode);
4176
4177 bool matched = true;
4178
4179 auto mismatch = [&] (const char *expected_str,
4180 const char *got)
4181 {
4182 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4183 "expected=\"%s\", got=\"%s\"\n"),
4184 file, line,
4185 (match_type == symbol_name_match_type::FULL
4186 ? "FULL" : "WILD"),
4187 name, string_or_null (expected_str), string_or_null (got));
4188 matched = false;
4189 };
4190
4191 auto expected_it = expected_list.begin ();
4192 auto expected_end = expected_list.end ();
4193
4194 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4195 NULL, ALL_DOMAIN,
4196 [&] (offset_type idx)
4197 {
4198 const char *matched_name = mock_index.symbol_name_at (idx);
4199 const char *expected_str
4200 = expected_it == expected_end ? NULL : *expected_it++;
4201
4202 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4203 mismatch (expected_str, matched_name);
4204 return true;
4205 });
4206
4207 const char *expected_str
4208 = expected_it == expected_end ? NULL : *expected_it++;
4209 if (expected_str != NULL)
4210 mismatch (expected_str, NULL);
4211
4212 return matched;
4213 }
4214
4215 /* The symbols added to the mock mapped_index for testing (in
4216 canonical form). */
4217 static const char *test_symbols[] = {
4218 "function",
4219 "std::bar",
4220 "std::zfunction",
4221 "std::zfunction2",
4222 "w1::w2",
4223 "ns::foo<char*>",
4224 "ns::foo<int>",
4225 "ns::foo<long>",
4226 "ns2::tmpl<int>::foo2",
4227 "(anonymous namespace)::A::B::C",
4228
4229 /* These are used to check that the increment-last-char in the
4230 matching algorithm for completion doesn't match "t1_fund" when
4231 completing "t1_func". */
4232 "t1_func",
4233 "t1_func1",
4234 "t1_fund",
4235 "t1_fund1",
4236
4237 /* A UTF-8 name with multi-byte sequences to make sure that
4238 cp-name-parser understands this as a single identifier ("função"
4239 is "function" in PT). */
4240 u8"u8função",
4241
4242 /* \377 (0xff) is Latin1 'ÿ'. */
4243 "yfunc\377",
4244
4245 /* \377 (0xff) is Latin1 'ÿ'. */
4246 "\377",
4247 "\377\377123",
4248
4249 /* A name with all sorts of complications. Starts with "z" to make
4250 it easier for the completion tests below. */
4251 #define Z_SYM_NAME \
4252 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4253 "::tuple<(anonymous namespace)::ui*, " \
4254 "std::default_delete<(anonymous namespace)::ui>, void>"
4255
4256 Z_SYM_NAME
4257 };
4258
4259 /* Returns true if the mapped_index_base::find_name_component_bounds
4260 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4261 in completion mode. */
4262
4263 static bool
4264 check_find_bounds_finds (mapped_index_base &index,
4265 const char *search_name,
4266 gdb::array_view<const char *> expected_syms)
4267 {
4268 lookup_name_info lookup_name (search_name,
4269 symbol_name_match_type::FULL, true);
4270
4271 auto bounds = index.find_name_components_bounds (lookup_name,
4272 language_cplus);
4273
4274 size_t distance = std::distance (bounds.first, bounds.second);
4275 if (distance != expected_syms.size ())
4276 return false;
4277
4278 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4279 {
4280 auto nc_elem = bounds.first + exp_elem;
4281 const char *qualified = index.symbol_name_at (nc_elem->idx);
4282 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4283 return false;
4284 }
4285
4286 return true;
4287 }
4288
4289 /* Test the lower-level mapped_index::find_name_component_bounds
4290 method. */
4291
4292 static void
4293 test_mapped_index_find_name_component_bounds ()
4294 {
4295 mock_mapped_index mock_index (test_symbols);
4296
4297 mock_index.build_name_components ();
4298
4299 /* Test the lower-level mapped_index::find_name_component_bounds
4300 method in completion mode. */
4301 {
4302 static const char *expected_syms[] = {
4303 "t1_func",
4304 "t1_func1",
4305 };
4306
4307 SELF_CHECK (check_find_bounds_finds (mock_index,
4308 "t1_func", expected_syms));
4309 }
4310
4311 /* Check that the increment-last-char in the name matching algorithm
4312 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4313 {
4314 static const char *expected_syms1[] = {
4315 "\377",
4316 "\377\377123",
4317 };
4318 SELF_CHECK (check_find_bounds_finds (mock_index,
4319 "\377", expected_syms1));
4320
4321 static const char *expected_syms2[] = {
4322 "\377\377123",
4323 };
4324 SELF_CHECK (check_find_bounds_finds (mock_index,
4325 "\377\377", expected_syms2));
4326 }
4327 }
4328
4329 /* Test dw2_expand_symtabs_matching_symbol. */
4330
4331 static void
4332 test_dw2_expand_symtabs_matching_symbol ()
4333 {
4334 mock_mapped_index mock_index (test_symbols);
4335
4336 /* We let all tests run until the end even if some fails, for debug
4337 convenience. */
4338 bool any_mismatch = false;
4339
4340 /* Create the expected symbols list (an initializer_list). Needed
4341 because lists have commas, and we need to pass them to CHECK,
4342 which is a macro. */
4343 #define EXPECT(...) { __VA_ARGS__ }
4344
4345 /* Wrapper for check_match that passes down the current
4346 __FILE__/__LINE__. */
4347 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4348 any_mismatch |= !check_match (__FILE__, __LINE__, \
4349 mock_index, \
4350 NAME, MATCH_TYPE, COMPLETION_MODE, \
4351 EXPECTED_LIST)
4352
4353 /* Identity checks. */
4354 for (const char *sym : test_symbols)
4355 {
4356 /* Should be able to match all existing symbols. */
4357 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4358 EXPECT (sym));
4359
4360 /* Should be able to match all existing symbols with
4361 parameters. */
4362 std::string with_params = std::string (sym) + "(int)";
4363 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4364 EXPECT (sym));
4365
4366 /* Should be able to match all existing symbols with
4367 parameters and qualifiers. */
4368 with_params = std::string (sym) + " ( int ) const";
4369 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4370 EXPECT (sym));
4371
4372 /* This should really find sym, but cp-name-parser.y doesn't
4373 know about lvalue/rvalue qualifiers yet. */
4374 with_params = std::string (sym) + " ( int ) &&";
4375 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4376 {});
4377 }
4378
4379 /* Check that the name matching algorithm for completion doesn't get
4380 confused with Latin1 'ÿ' / 0xff. */
4381 {
4382 static const char str[] = "\377";
4383 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4384 EXPECT ("\377", "\377\377123"));
4385 }
4386
4387 /* Check that the increment-last-char in the matching algorithm for
4388 completion doesn't match "t1_fund" when completing "t1_func". */
4389 {
4390 static const char str[] = "t1_func";
4391 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4392 EXPECT ("t1_func", "t1_func1"));
4393 }
4394
4395 /* Check that completion mode works at each prefix of the expected
4396 symbol name. */
4397 {
4398 static const char str[] = "function(int)";
4399 size_t len = strlen (str);
4400 std::string lookup;
4401
4402 for (size_t i = 1; i < len; i++)
4403 {
4404 lookup.assign (str, i);
4405 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4406 EXPECT ("function"));
4407 }
4408 }
4409
4410 /* While "w" is a prefix of both components, the match function
4411 should still only be called once. */
4412 {
4413 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4414 EXPECT ("w1::w2"));
4415 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4416 EXPECT ("w1::w2"));
4417 }
4418
4419 /* Same, with a "complicated" symbol. */
4420 {
4421 static const char str[] = Z_SYM_NAME;
4422 size_t len = strlen (str);
4423 std::string lookup;
4424
4425 for (size_t i = 1; i < len; i++)
4426 {
4427 lookup.assign (str, i);
4428 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4429 EXPECT (Z_SYM_NAME));
4430 }
4431 }
4432
4433 /* In FULL mode, an incomplete symbol doesn't match. */
4434 {
4435 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4436 {});
4437 }
4438
4439 /* A complete symbol with parameters matches any overload, since the
4440 index has no overload info. */
4441 {
4442 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4443 EXPECT ("std::zfunction", "std::zfunction2"));
4444 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4445 EXPECT ("std::zfunction", "std::zfunction2"));
4446 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4447 EXPECT ("std::zfunction", "std::zfunction2"));
4448 }
4449
4450 /* Check that whitespace is ignored appropriately. A symbol with a
4451 template argument list. */
4452 {
4453 static const char expected[] = "ns::foo<int>";
4454 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4455 EXPECT (expected));
4456 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4457 EXPECT (expected));
4458 }
4459
4460 /* Check that whitespace is ignored appropriately. A symbol with a
4461 template argument list that includes a pointer. */
4462 {
4463 static const char expected[] = "ns::foo<char*>";
4464 /* Try both completion and non-completion modes. */
4465 static const bool completion_mode[2] = {false, true};
4466 for (size_t i = 0; i < 2; i++)
4467 {
4468 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4469 completion_mode[i], EXPECT (expected));
4470 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4471 completion_mode[i], EXPECT (expected));
4472
4473 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4474 completion_mode[i], EXPECT (expected));
4475 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4476 completion_mode[i], EXPECT (expected));
4477 }
4478 }
4479
4480 {
4481 /* Check method qualifiers are ignored. */
4482 static const char expected[] = "ns::foo<char*>";
4483 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4484 symbol_name_match_type::FULL, true, EXPECT (expected));
4485 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4486 symbol_name_match_type::FULL, true, EXPECT (expected));
4487 CHECK_MATCH ("foo < char * > ( int ) const",
4488 symbol_name_match_type::WILD, true, EXPECT (expected));
4489 CHECK_MATCH ("foo < char * > ( int ) &&",
4490 symbol_name_match_type::WILD, true, EXPECT (expected));
4491 }
4492
4493 /* Test lookup names that don't match anything. */
4494 {
4495 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4496 {});
4497
4498 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4499 {});
4500 }
4501
4502 /* Some wild matching tests, exercising "(anonymous namespace)",
4503 which should not be confused with a parameter list. */
4504 {
4505 static const char *syms[] = {
4506 "A::B::C",
4507 "B::C",
4508 "C",
4509 "A :: B :: C ( int )",
4510 "B :: C ( int )",
4511 "C ( int )",
4512 };
4513
4514 for (const char *s : syms)
4515 {
4516 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4517 EXPECT ("(anonymous namespace)::A::B::C"));
4518 }
4519 }
4520
4521 {
4522 static const char expected[] = "ns2::tmpl<int>::foo2";
4523 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4524 EXPECT (expected));
4525 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4526 EXPECT (expected));
4527 }
4528
4529 SELF_CHECK (!any_mismatch);
4530
4531 #undef EXPECT
4532 #undef CHECK_MATCH
4533 }
4534
4535 static void
4536 run_test ()
4537 {
4538 test_mapped_index_find_name_component_bounds ();
4539 test_dw2_expand_symtabs_matching_symbol ();
4540 }
4541
4542 }} // namespace selftests::dw2_expand_symtabs_matching
4543
4544 #endif /* GDB_SELF_TEST */
4545
4546 /* If FILE_MATCHER is NULL or if PER_CU has
4547 dwarf2_per_cu_quick_data::MARK set (see
4548 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4549 EXPANSION_NOTIFY on it. */
4550
4551 static void
4552 dw2_expand_symtabs_matching_one
4553 (dwarf2_per_cu_data *per_cu,
4554 dwarf2_per_objfile *per_objfile,
4555 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4556 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4557 {
4558 if (file_matcher == NULL || per_cu->v.quick->mark)
4559 {
4560 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4561
4562 compunit_symtab *symtab
4563 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4564 gdb_assert (symtab != nullptr);
4565
4566 if (expansion_notify != NULL && symtab_was_null)
4567 expansion_notify (symtab);
4568 }
4569 }
4570
4571 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4572 matched, to expand corresponding CUs that were marked. IDX is the
4573 index of the symbol name that matched. */
4574
4575 static void
4576 dw2_expand_marked_cus
4577 (dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4578 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4579 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4580 search_domain kind)
4581 {
4582 offset_type *vec, vec_len, vec_idx;
4583 bool global_seen = false;
4584 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4585
4586 vec = (offset_type *) (index.constant_pool
4587 + MAYBE_SWAP (index.symbol_table[idx].vec));
4588 vec_len = MAYBE_SWAP (vec[0]);
4589 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4590 {
4591 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4592 /* This value is only valid for index versions >= 7. */
4593 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4594 gdb_index_symbol_kind symbol_kind =
4595 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4596 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4597 /* Only check the symbol attributes if they're present.
4598 Indices prior to version 7 don't record them,
4599 and indices >= 7 may elide them for certain symbols
4600 (gold does this). */
4601 int attrs_valid =
4602 (index.version >= 7
4603 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4604
4605 /* Work around gold/15646. */
4606 if (attrs_valid)
4607 {
4608 if (!is_static && global_seen)
4609 continue;
4610 if (!is_static)
4611 global_seen = true;
4612 }
4613
4614 /* Only check the symbol's kind if it has one. */
4615 if (attrs_valid)
4616 {
4617 switch (kind)
4618 {
4619 case VARIABLES_DOMAIN:
4620 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4621 continue;
4622 break;
4623 case FUNCTIONS_DOMAIN:
4624 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4625 continue;
4626 break;
4627 case TYPES_DOMAIN:
4628 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4629 continue;
4630 break;
4631 case MODULES_DOMAIN:
4632 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4633 continue;
4634 break;
4635 default:
4636 break;
4637 }
4638 }
4639
4640 /* Don't crash on bad data. */
4641 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4642 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
4643 {
4644 complaint (_(".gdb_index entry has bad CU index"
4645 " [in module %s]"),
4646 objfile_name (dwarf2_per_objfile->objfile));
4647 continue;
4648 }
4649
4650 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
4651 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, file_matcher,
4652 expansion_notify);
4653 }
4654 }
4655
4656 /* If FILE_MATCHER is non-NULL, set all the
4657 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4658 that match FILE_MATCHER. */
4659
4660 static void
4661 dw_expand_symtabs_matching_file_matcher
4662 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4663 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4664 {
4665 if (file_matcher == NULL)
4666 return;
4667
4668 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4669 htab_eq_pointer,
4670 NULL, xcalloc, xfree));
4671 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4672 htab_eq_pointer,
4673 NULL, xcalloc, xfree));
4674
4675 /* The rule is CUs specify all the files, including those used by
4676 any TU, so there's no need to scan TUs here. */
4677
4678 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4679 {
4680 QUIT;
4681
4682 per_cu->v.quick->mark = 0;
4683
4684 /* We only need to look at symtabs not already expanded. */
4685 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4686 continue;
4687
4688 quick_file_names *file_data
4689 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4690 if (file_data == NULL)
4691 continue;
4692
4693 if (htab_find (visited_not_found.get (), file_data) != NULL)
4694 continue;
4695 else if (htab_find (visited_found.get (), file_data) != NULL)
4696 {
4697 per_cu->v.quick->mark = 1;
4698 continue;
4699 }
4700
4701 for (int j = 0; j < file_data->num_file_names; ++j)
4702 {
4703 const char *this_real_name;
4704
4705 if (file_matcher (file_data->file_names[j], false))
4706 {
4707 per_cu->v.quick->mark = 1;
4708 break;
4709 }
4710
4711 /* Before we invoke realpath, which can get expensive when many
4712 files are involved, do a quick comparison of the basenames. */
4713 if (!basenames_may_differ
4714 && !file_matcher (lbasename (file_data->file_names[j]),
4715 true))
4716 continue;
4717
4718 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4719 file_data, j);
4720 if (file_matcher (this_real_name, false))
4721 {
4722 per_cu->v.quick->mark = 1;
4723 break;
4724 }
4725 }
4726
4727 void **slot = htab_find_slot (per_cu->v.quick->mark
4728 ? visited_found.get ()
4729 : visited_not_found.get (),
4730 file_data, INSERT);
4731 *slot = file_data;
4732 }
4733 }
4734
4735 static void
4736 dw2_expand_symtabs_matching
4737 (struct objfile *objfile,
4738 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4739 const lookup_name_info *lookup_name,
4740 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4741 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4742 enum search_domain kind)
4743 {
4744 struct dwarf2_per_objfile *dwarf2_per_objfile
4745 = get_dwarf2_per_objfile (objfile);
4746
4747 /* index_table is NULL if OBJF_READNOW. */
4748 if (!dwarf2_per_objfile->per_bfd->index_table)
4749 return;
4750
4751 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4752
4753 if (symbol_matcher == NULL && lookup_name == NULL)
4754 {
4755 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4756 {
4757 QUIT;
4758
4759 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
4760 file_matcher, expansion_notify);
4761 }
4762 return;
4763 }
4764
4765 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4766
4767 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4768 symbol_matcher,
4769 kind, [&] (offset_type idx)
4770 {
4771 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4772 expansion_notify, kind);
4773 return true;
4774 });
4775 }
4776
4777 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4778 symtab. */
4779
4780 static struct compunit_symtab *
4781 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4782 CORE_ADDR pc)
4783 {
4784 int i;
4785
4786 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4787 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4788 return cust;
4789
4790 if (cust->includes == NULL)
4791 return NULL;
4792
4793 for (i = 0; cust->includes[i]; ++i)
4794 {
4795 struct compunit_symtab *s = cust->includes[i];
4796
4797 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4798 if (s != NULL)
4799 return s;
4800 }
4801
4802 return NULL;
4803 }
4804
4805 static struct compunit_symtab *
4806 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4807 struct bound_minimal_symbol msymbol,
4808 CORE_ADDR pc,
4809 struct obj_section *section,
4810 int warn_if_readin)
4811 {
4812 struct dwarf2_per_cu_data *data;
4813 struct compunit_symtab *result;
4814
4815 if (!objfile->partial_symtabs->psymtabs_addrmap)
4816 return NULL;
4817
4818 CORE_ADDR baseaddr = objfile->text_section_offset ();
4819 data = (struct dwarf2_per_cu_data *) addrmap_find
4820 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4821 if (!data)
4822 return NULL;
4823
4824 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4825 if (warn_if_readin && per_objfile->symtab_set_p (data))
4826 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4827 paddress (objfile->arch (), pc));
4828
4829 result = recursively_find_pc_sect_compunit_symtab
4830 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4831
4832 gdb_assert (result != NULL);
4833 return result;
4834 }
4835
4836 static void
4837 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4838 void *data, int need_fullname)
4839 {
4840 struct dwarf2_per_objfile *dwarf2_per_objfile
4841 = get_dwarf2_per_objfile (objfile);
4842
4843 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
4844 {
4845 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
4846
4847 htab_up visited (htab_create_alloc (10,
4848 htab_hash_pointer, htab_eq_pointer,
4849 NULL, xcalloc, xfree));
4850
4851 /* The rule is CUs specify all the files, including those used
4852 by any TU, so there's no need to scan TUs here. We can
4853 ignore file names coming from already-expanded CUs. */
4854
4855 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4856 {
4857 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4858 {
4859 void **slot = htab_find_slot (visited.get (),
4860 per_cu->v.quick->file_names,
4861 INSERT);
4862
4863 *slot = per_cu->v.quick->file_names;
4864 }
4865 }
4866
4867 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4868 {
4869 /* We only need to look at symtabs not already expanded. */
4870 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4871 continue;
4872
4873 quick_file_names *file_data
4874 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4875 if (file_data == NULL)
4876 continue;
4877
4878 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4879 if (*slot)
4880 {
4881 /* Already visited. */
4882 continue;
4883 }
4884 *slot = file_data;
4885
4886 for (int j = 0; j < file_data->num_file_names; ++j)
4887 {
4888 const char *filename = file_data->file_names[j];
4889 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
4890 }
4891 }
4892 }
4893
4894 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4895 {
4896 gdb::unique_xmalloc_ptr<char> this_real_name;
4897
4898 if (need_fullname)
4899 this_real_name = gdb_realpath (filename);
4900 (*fun) (filename, this_real_name.get (), data);
4901 });
4902 }
4903
4904 static int
4905 dw2_has_symbols (struct objfile *objfile)
4906 {
4907 return 1;
4908 }
4909
4910 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4911 {
4912 dw2_has_symbols,
4913 dw2_find_last_source_symtab,
4914 dw2_forget_cached_source_info,
4915 dw2_map_symtabs_matching_filename,
4916 dw2_lookup_symbol,
4917 NULL,
4918 dw2_print_stats,
4919 dw2_dump,
4920 dw2_expand_symtabs_for_function,
4921 dw2_expand_all_symtabs,
4922 dw2_expand_symtabs_with_fullname,
4923 dw2_map_matching_symbols,
4924 dw2_expand_symtabs_matching,
4925 dw2_find_pc_sect_compunit_symtab,
4926 NULL,
4927 dw2_map_symbol_filenames
4928 };
4929
4930 /* DWARF-5 debug_names reader. */
4931
4932 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4933 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4934
4935 /* A helper function that reads the .debug_names section in SECTION
4936 and fills in MAP. FILENAME is the name of the file containing the
4937 section; it is used for error reporting.
4938
4939 Returns true if all went well, false otherwise. */
4940
4941 static bool
4942 read_debug_names_from_section (struct objfile *objfile,
4943 const char *filename,
4944 struct dwarf2_section_info *section,
4945 mapped_debug_names &map)
4946 {
4947 if (section->empty ())
4948 return false;
4949
4950 /* Older elfutils strip versions could keep the section in the main
4951 executable while splitting it for the separate debug info file. */
4952 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4953 return false;
4954
4955 section->read (objfile);
4956
4957 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4958
4959 const gdb_byte *addr = section->buffer;
4960
4961 bfd *const abfd = section->get_bfd_owner ();
4962
4963 unsigned int bytes_read;
4964 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4965 addr += bytes_read;
4966
4967 map.dwarf5_is_dwarf64 = bytes_read != 4;
4968 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4969 if (bytes_read + length != section->size)
4970 {
4971 /* There may be multiple per-CU indices. */
4972 warning (_("Section .debug_names in %s length %s does not match "
4973 "section length %s, ignoring .debug_names."),
4974 filename, plongest (bytes_read + length),
4975 pulongest (section->size));
4976 return false;
4977 }
4978
4979 /* The version number. */
4980 uint16_t version = read_2_bytes (abfd, addr);
4981 addr += 2;
4982 if (version != 5)
4983 {
4984 warning (_("Section .debug_names in %s has unsupported version %d, "
4985 "ignoring .debug_names."),
4986 filename, version);
4987 return false;
4988 }
4989
4990 /* Padding. */
4991 uint16_t padding = read_2_bytes (abfd, addr);
4992 addr += 2;
4993 if (padding != 0)
4994 {
4995 warning (_("Section .debug_names in %s has unsupported padding %d, "
4996 "ignoring .debug_names."),
4997 filename, padding);
4998 return false;
4999 }
5000
5001 /* comp_unit_count - The number of CUs in the CU list. */
5002 map.cu_count = read_4_bytes (abfd, addr);
5003 addr += 4;
5004
5005 /* local_type_unit_count - The number of TUs in the local TU
5006 list. */
5007 map.tu_count = read_4_bytes (abfd, addr);
5008 addr += 4;
5009
5010 /* foreign_type_unit_count - The number of TUs in the foreign TU
5011 list. */
5012 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5013 addr += 4;
5014 if (foreign_tu_count != 0)
5015 {
5016 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5017 "ignoring .debug_names."),
5018 filename, static_cast<unsigned long> (foreign_tu_count));
5019 return false;
5020 }
5021
5022 /* bucket_count - The number of hash buckets in the hash lookup
5023 table. */
5024 map.bucket_count = read_4_bytes (abfd, addr);
5025 addr += 4;
5026
5027 /* name_count - The number of unique names in the index. */
5028 map.name_count = read_4_bytes (abfd, addr);
5029 addr += 4;
5030
5031 /* abbrev_table_size - The size in bytes of the abbreviations
5032 table. */
5033 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5034 addr += 4;
5035
5036 /* augmentation_string_size - The size in bytes of the augmentation
5037 string. This value is rounded up to a multiple of 4. */
5038 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5039 addr += 4;
5040 map.augmentation_is_gdb = ((augmentation_string_size
5041 == sizeof (dwarf5_augmentation))
5042 && memcmp (addr, dwarf5_augmentation,
5043 sizeof (dwarf5_augmentation)) == 0);
5044 augmentation_string_size += (-augmentation_string_size) & 3;
5045 addr += augmentation_string_size;
5046
5047 /* List of CUs */
5048 map.cu_table_reordered = addr;
5049 addr += map.cu_count * map.offset_size;
5050
5051 /* List of Local TUs */
5052 map.tu_table_reordered = addr;
5053 addr += map.tu_count * map.offset_size;
5054
5055 /* Hash Lookup Table */
5056 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5057 addr += map.bucket_count * 4;
5058 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5059 addr += map.name_count * 4;
5060
5061 /* Name Table */
5062 map.name_table_string_offs_reordered = addr;
5063 addr += map.name_count * map.offset_size;
5064 map.name_table_entry_offs_reordered = addr;
5065 addr += map.name_count * map.offset_size;
5066
5067 const gdb_byte *abbrev_table_start = addr;
5068 for (;;)
5069 {
5070 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5071 addr += bytes_read;
5072 if (index_num == 0)
5073 break;
5074
5075 const auto insertpair
5076 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5077 if (!insertpair.second)
5078 {
5079 warning (_("Section .debug_names in %s has duplicate index %s, "
5080 "ignoring .debug_names."),
5081 filename, pulongest (index_num));
5082 return false;
5083 }
5084 mapped_debug_names::index_val &indexval = insertpair.first->second;
5085 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5086 addr += bytes_read;
5087
5088 for (;;)
5089 {
5090 mapped_debug_names::index_val::attr attr;
5091 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5092 addr += bytes_read;
5093 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5094 addr += bytes_read;
5095 if (attr.form == DW_FORM_implicit_const)
5096 {
5097 attr.implicit_const = read_signed_leb128 (abfd, addr,
5098 &bytes_read);
5099 addr += bytes_read;
5100 }
5101 if (attr.dw_idx == 0 && attr.form == 0)
5102 break;
5103 indexval.attr_vec.push_back (std::move (attr));
5104 }
5105 }
5106 if (addr != abbrev_table_start + abbrev_table_size)
5107 {
5108 warning (_("Section .debug_names in %s has abbreviation_table "
5109 "of size %s vs. written as %u, ignoring .debug_names."),
5110 filename, plongest (addr - abbrev_table_start),
5111 abbrev_table_size);
5112 return false;
5113 }
5114 map.entry_pool = addr;
5115
5116 return true;
5117 }
5118
5119 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5120 list. */
5121
5122 static void
5123 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5124 const mapped_debug_names &map,
5125 dwarf2_section_info &section,
5126 bool is_dwz)
5127 {
5128 if (!map.augmentation_is_gdb)
5129 {
5130 for (uint32_t i = 0; i < map.cu_count; ++i)
5131 {
5132 sect_offset sect_off
5133 = (sect_offset) (extract_unsigned_integer
5134 (map.cu_table_reordered + i * map.offset_size,
5135 map.offset_size,
5136 map.dwarf5_byte_order));
5137 /* We don't know the length of the CU, because the CU list in a
5138 .debug_names index can be incomplete, so we can't use the start of
5139 the next CU as end of this CU. We create the CUs here with length 0,
5140 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5141 dwarf2_per_cu_data *per_cu
5142 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5143 sect_off, 0);
5144 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
5145 }
5146 }
5147
5148 sect_offset sect_off_prev;
5149 for (uint32_t i = 0; i <= map.cu_count; ++i)
5150 {
5151 sect_offset sect_off_next;
5152 if (i < map.cu_count)
5153 {
5154 sect_off_next
5155 = (sect_offset) (extract_unsigned_integer
5156 (map.cu_table_reordered + i * map.offset_size,
5157 map.offset_size,
5158 map.dwarf5_byte_order));
5159 }
5160 else
5161 sect_off_next = (sect_offset) section.size;
5162 if (i >= 1)
5163 {
5164 const ULONGEST length = sect_off_next - sect_off_prev;
5165 dwarf2_per_cu_data *per_cu
5166 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5167 sect_off_prev, length);
5168 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
5169 }
5170 sect_off_prev = sect_off_next;
5171 }
5172 }
5173
5174 /* Read the CU list from the mapped index, and use it to create all
5175 the CU objects for this dwarf2_per_objfile. */
5176
5177 static void
5178 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5179 const mapped_debug_names &map,
5180 const mapped_debug_names &dwz_map)
5181 {
5182 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
5183 dwarf2_per_objfile->per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5184
5185 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5186 dwarf2_per_objfile->per_bfd->info,
5187 false /* is_dwz */);
5188
5189 if (dwz_map.cu_count == 0)
5190 return;
5191
5192 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
5193 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5194 true /* is_dwz */);
5195 }
5196
5197 /* Read .debug_names. If everything went ok, initialize the "quick"
5198 elements of all the CUs and return true. Otherwise, return false. */
5199
5200 static bool
5201 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5202 {
5203 std::unique_ptr<mapped_debug_names> map
5204 (new mapped_debug_names (dwarf2_per_objfile));
5205 mapped_debug_names dwz_map (dwarf2_per_objfile);
5206 struct objfile *objfile = dwarf2_per_objfile->objfile;
5207
5208 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5209 &dwarf2_per_objfile->per_bfd->debug_names,
5210 *map))
5211 return false;
5212
5213 /* Don't use the index if it's empty. */
5214 if (map->name_count == 0)
5215 return false;
5216
5217 /* If there is a .dwz file, read it so we can get its CU list as
5218 well. */
5219 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
5220 if (dwz != NULL)
5221 {
5222 if (!read_debug_names_from_section (objfile,
5223 bfd_get_filename (dwz->dwz_bfd.get ()),
5224 &dwz->debug_names, dwz_map))
5225 {
5226 warning (_("could not read '.debug_names' section from %s; skipping"),
5227 bfd_get_filename (dwz->dwz_bfd.get ()));
5228 return false;
5229 }
5230 }
5231
5232 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5233
5234 if (map->tu_count != 0)
5235 {
5236 /* We can only handle a single .debug_types when we have an
5237 index. */
5238 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
5239 return false;
5240
5241 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
5242
5243 create_signatured_type_table_from_debug_names
5244 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
5245 }
5246
5247 create_addrmap_from_aranges (dwarf2_per_objfile,
5248 &dwarf2_per_objfile->per_bfd->debug_aranges);
5249
5250 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5251 dwarf2_per_objfile->per_bfd->using_index = 1;
5252 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5253 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5254
5255 return true;
5256 }
5257
5258 /* Type used to manage iterating over all CUs looking for a symbol for
5259 .debug_names. */
5260
5261 class dw2_debug_names_iterator
5262 {
5263 public:
5264 dw2_debug_names_iterator (const mapped_debug_names &map,
5265 gdb::optional<block_enum> block_index,
5266 domain_enum domain,
5267 const char *name)
5268 : m_map (map), m_block_index (block_index), m_domain (domain),
5269 m_addr (find_vec_in_debug_names (map, name))
5270 {}
5271
5272 dw2_debug_names_iterator (const mapped_debug_names &map,
5273 search_domain search, uint32_t namei)
5274 : m_map (map),
5275 m_search (search),
5276 m_addr (find_vec_in_debug_names (map, namei))
5277 {}
5278
5279 dw2_debug_names_iterator (const mapped_debug_names &map,
5280 block_enum block_index, domain_enum domain,
5281 uint32_t namei)
5282 : m_map (map), m_block_index (block_index), m_domain (domain),
5283 m_addr (find_vec_in_debug_names (map, namei))
5284 {}
5285
5286 /* Return the next matching CU or NULL if there are no more. */
5287 dwarf2_per_cu_data *next ();
5288
5289 private:
5290 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5291 const char *name);
5292 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5293 uint32_t namei);
5294
5295 /* The internalized form of .debug_names. */
5296 const mapped_debug_names &m_map;
5297
5298 /* If set, only look for symbols that match that block. Valid values are
5299 GLOBAL_BLOCK and STATIC_BLOCK. */
5300 const gdb::optional<block_enum> m_block_index;
5301
5302 /* The kind of symbol we're looking for. */
5303 const domain_enum m_domain = UNDEF_DOMAIN;
5304 const search_domain m_search = ALL_DOMAIN;
5305
5306 /* The list of CUs from the index entry of the symbol, or NULL if
5307 not found. */
5308 const gdb_byte *m_addr;
5309 };
5310
5311 const char *
5312 mapped_debug_names::namei_to_name (uint32_t namei) const
5313 {
5314 const ULONGEST namei_string_offs
5315 = extract_unsigned_integer ((name_table_string_offs_reordered
5316 + namei * offset_size),
5317 offset_size,
5318 dwarf5_byte_order);
5319 return read_indirect_string_at_offset (dwarf2_per_objfile,
5320 namei_string_offs);
5321 }
5322
5323 /* Find a slot in .debug_names for the object named NAME. If NAME is
5324 found, return pointer to its pool data. If NAME cannot be found,
5325 return NULL. */
5326
5327 const gdb_byte *
5328 dw2_debug_names_iterator::find_vec_in_debug_names
5329 (const mapped_debug_names &map, const char *name)
5330 {
5331 int (*cmp) (const char *, const char *);
5332
5333 gdb::unique_xmalloc_ptr<char> without_params;
5334 if (current_language->la_language == language_cplus
5335 || current_language->la_language == language_fortran
5336 || current_language->la_language == language_d)
5337 {
5338 /* NAME is already canonical. Drop any qualifiers as
5339 .debug_names does not contain any. */
5340
5341 if (strchr (name, '(') != NULL)
5342 {
5343 without_params = cp_remove_params (name);
5344 if (without_params != NULL)
5345 name = without_params.get ();
5346 }
5347 }
5348
5349 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5350
5351 const uint32_t full_hash = dwarf5_djb_hash (name);
5352 uint32_t namei
5353 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5354 (map.bucket_table_reordered
5355 + (full_hash % map.bucket_count)), 4,
5356 map.dwarf5_byte_order);
5357 if (namei == 0)
5358 return NULL;
5359 --namei;
5360 if (namei >= map.name_count)
5361 {
5362 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5363 "[in module %s]"),
5364 namei, map.name_count,
5365 objfile_name (map.dwarf2_per_objfile->objfile));
5366 return NULL;
5367 }
5368
5369 for (;;)
5370 {
5371 const uint32_t namei_full_hash
5372 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5373 (map.hash_table_reordered + namei), 4,
5374 map.dwarf5_byte_order);
5375 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5376 return NULL;
5377
5378 if (full_hash == namei_full_hash)
5379 {
5380 const char *const namei_string = map.namei_to_name (namei);
5381
5382 #if 0 /* An expensive sanity check. */
5383 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5384 {
5385 complaint (_("Wrong .debug_names hash for string at index %u "
5386 "[in module %s]"),
5387 namei, objfile_name (dwarf2_per_objfile->objfile));
5388 return NULL;
5389 }
5390 #endif
5391
5392 if (cmp (namei_string, name) == 0)
5393 {
5394 const ULONGEST namei_entry_offs
5395 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5396 + namei * map.offset_size),
5397 map.offset_size, map.dwarf5_byte_order);
5398 return map.entry_pool + namei_entry_offs;
5399 }
5400 }
5401
5402 ++namei;
5403 if (namei >= map.name_count)
5404 return NULL;
5405 }
5406 }
5407
5408 const gdb_byte *
5409 dw2_debug_names_iterator::find_vec_in_debug_names
5410 (const mapped_debug_names &map, uint32_t namei)
5411 {
5412 if (namei >= map.name_count)
5413 {
5414 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5415 "[in module %s]"),
5416 namei, map.name_count,
5417 objfile_name (map.dwarf2_per_objfile->objfile));
5418 return NULL;
5419 }
5420
5421 const ULONGEST namei_entry_offs
5422 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5423 + namei * map.offset_size),
5424 map.offset_size, map.dwarf5_byte_order);
5425 return map.entry_pool + namei_entry_offs;
5426 }
5427
5428 /* See dw2_debug_names_iterator. */
5429
5430 dwarf2_per_cu_data *
5431 dw2_debug_names_iterator::next ()
5432 {
5433 if (m_addr == NULL)
5434 return NULL;
5435
5436 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5437 struct objfile *objfile = dwarf2_per_objfile->objfile;
5438 bfd *const abfd = objfile->obfd;
5439
5440 again:
5441
5442 unsigned int bytes_read;
5443 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5444 m_addr += bytes_read;
5445 if (abbrev == 0)
5446 return NULL;
5447
5448 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5449 if (indexval_it == m_map.abbrev_map.cend ())
5450 {
5451 complaint (_("Wrong .debug_names undefined abbrev code %s "
5452 "[in module %s]"),
5453 pulongest (abbrev), objfile_name (objfile));
5454 return NULL;
5455 }
5456 const mapped_debug_names::index_val &indexval = indexval_it->second;
5457 enum class symbol_linkage {
5458 unknown,
5459 static_,
5460 extern_,
5461 } symbol_linkage_ = symbol_linkage::unknown;
5462 dwarf2_per_cu_data *per_cu = NULL;
5463 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5464 {
5465 ULONGEST ull;
5466 switch (attr.form)
5467 {
5468 case DW_FORM_implicit_const:
5469 ull = attr.implicit_const;
5470 break;
5471 case DW_FORM_flag_present:
5472 ull = 1;
5473 break;
5474 case DW_FORM_udata:
5475 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5476 m_addr += bytes_read;
5477 break;
5478 case DW_FORM_ref4:
5479 ull = read_4_bytes (abfd, m_addr);
5480 m_addr += 4;
5481 break;
5482 case DW_FORM_ref8:
5483 ull = read_8_bytes (abfd, m_addr);
5484 m_addr += 8;
5485 break;
5486 case DW_FORM_ref_sig8:
5487 ull = read_8_bytes (abfd, m_addr);
5488 m_addr += 8;
5489 break;
5490 default:
5491 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5492 dwarf_form_name (attr.form),
5493 objfile_name (objfile));
5494 return NULL;
5495 }
5496 switch (attr.dw_idx)
5497 {
5498 case DW_IDX_compile_unit:
5499 /* Don't crash on bad data. */
5500 if (ull >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
5501 {
5502 complaint (_(".debug_names entry has bad CU index %s"
5503 " [in module %s]"),
5504 pulongest (ull),
5505 objfile_name (dwarf2_per_objfile->objfile));
5506 continue;
5507 }
5508 per_cu = dwarf2_per_objfile->per_bfd->get_cutu (ull);
5509 break;
5510 case DW_IDX_type_unit:
5511 /* Don't crash on bad data. */
5512 if (ull >= dwarf2_per_objfile->per_bfd->all_type_units.size ())
5513 {
5514 complaint (_(".debug_names entry has bad TU index %s"
5515 " [in module %s]"),
5516 pulongest (ull),
5517 objfile_name (dwarf2_per_objfile->objfile));
5518 continue;
5519 }
5520 per_cu = &dwarf2_per_objfile->per_bfd->get_tu (ull)->per_cu;
5521 break;
5522 case DW_IDX_die_offset:
5523 /* In a per-CU index (as opposed to a per-module index), index
5524 entries without CU attribute implicitly refer to the single CU. */
5525 if (per_cu == NULL)
5526 per_cu = dwarf2_per_objfile->per_bfd->get_cu (0);
5527 break;
5528 case DW_IDX_GNU_internal:
5529 if (!m_map.augmentation_is_gdb)
5530 break;
5531 symbol_linkage_ = symbol_linkage::static_;
5532 break;
5533 case DW_IDX_GNU_external:
5534 if (!m_map.augmentation_is_gdb)
5535 break;
5536 symbol_linkage_ = symbol_linkage::extern_;
5537 break;
5538 }
5539 }
5540
5541 /* Skip if already read in. */
5542 if (dwarf2_per_objfile->symtab_set_p (per_cu))
5543 goto again;
5544
5545 /* Check static vs global. */
5546 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5547 {
5548 const bool want_static = *m_block_index == STATIC_BLOCK;
5549 const bool symbol_is_static =
5550 symbol_linkage_ == symbol_linkage::static_;
5551 if (want_static != symbol_is_static)
5552 goto again;
5553 }
5554
5555 /* Match dw2_symtab_iter_next, symbol_kind
5556 and debug_names::psymbol_tag. */
5557 switch (m_domain)
5558 {
5559 case VAR_DOMAIN:
5560 switch (indexval.dwarf_tag)
5561 {
5562 case DW_TAG_variable:
5563 case DW_TAG_subprogram:
5564 /* Some types are also in VAR_DOMAIN. */
5565 case DW_TAG_typedef:
5566 case DW_TAG_structure_type:
5567 break;
5568 default:
5569 goto again;
5570 }
5571 break;
5572 case STRUCT_DOMAIN:
5573 switch (indexval.dwarf_tag)
5574 {
5575 case DW_TAG_typedef:
5576 case DW_TAG_structure_type:
5577 break;
5578 default:
5579 goto again;
5580 }
5581 break;
5582 case LABEL_DOMAIN:
5583 switch (indexval.dwarf_tag)
5584 {
5585 case 0:
5586 case DW_TAG_variable:
5587 break;
5588 default:
5589 goto again;
5590 }
5591 break;
5592 case MODULE_DOMAIN:
5593 switch (indexval.dwarf_tag)
5594 {
5595 case DW_TAG_module:
5596 break;
5597 default:
5598 goto again;
5599 }
5600 break;
5601 default:
5602 break;
5603 }
5604
5605 /* Match dw2_expand_symtabs_matching, symbol_kind and
5606 debug_names::psymbol_tag. */
5607 switch (m_search)
5608 {
5609 case VARIABLES_DOMAIN:
5610 switch (indexval.dwarf_tag)
5611 {
5612 case DW_TAG_variable:
5613 break;
5614 default:
5615 goto again;
5616 }
5617 break;
5618 case FUNCTIONS_DOMAIN:
5619 switch (indexval.dwarf_tag)
5620 {
5621 case DW_TAG_subprogram:
5622 break;
5623 default:
5624 goto again;
5625 }
5626 break;
5627 case TYPES_DOMAIN:
5628 switch (indexval.dwarf_tag)
5629 {
5630 case DW_TAG_typedef:
5631 case DW_TAG_structure_type:
5632 break;
5633 default:
5634 goto again;
5635 }
5636 break;
5637 case MODULES_DOMAIN:
5638 switch (indexval.dwarf_tag)
5639 {
5640 case DW_TAG_module:
5641 break;
5642 default:
5643 goto again;
5644 }
5645 default:
5646 break;
5647 }
5648
5649 return per_cu;
5650 }
5651
5652 static struct compunit_symtab *
5653 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5654 const char *name, domain_enum domain)
5655 {
5656 struct dwarf2_per_objfile *dwarf2_per_objfile
5657 = get_dwarf2_per_objfile (objfile);
5658
5659 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
5660 if (!mapp)
5661 {
5662 /* index is NULL if OBJF_READNOW. */
5663 return NULL;
5664 }
5665 const auto &map = *mapp;
5666
5667 dw2_debug_names_iterator iter (map, block_index, domain, name);
5668
5669 struct compunit_symtab *stab_best = NULL;
5670 struct dwarf2_per_cu_data *per_cu;
5671 while ((per_cu = iter.next ()) != NULL)
5672 {
5673 struct symbol *sym, *with_opaque = NULL;
5674 compunit_symtab *stab
5675 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5676 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5677 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5678
5679 sym = block_find_symbol (block, name, domain,
5680 block_find_non_opaque_type_preferred,
5681 &with_opaque);
5682
5683 /* Some caution must be observed with overloaded functions and
5684 methods, since the index will not contain any overload
5685 information (but NAME might contain it). */
5686
5687 if (sym != NULL
5688 && strcmp_iw (sym->search_name (), name) == 0)
5689 return stab;
5690 if (with_opaque != NULL
5691 && strcmp_iw (with_opaque->search_name (), name) == 0)
5692 stab_best = stab;
5693
5694 /* Keep looking through other CUs. */
5695 }
5696
5697 return stab_best;
5698 }
5699
5700 /* This dumps minimal information about .debug_names. It is called
5701 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5702 uses this to verify that .debug_names has been loaded. */
5703
5704 static void
5705 dw2_debug_names_dump (struct objfile *objfile)
5706 {
5707 struct dwarf2_per_objfile *dwarf2_per_objfile
5708 = get_dwarf2_per_objfile (objfile);
5709
5710 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
5711 printf_filtered (".debug_names:");
5712 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5713 printf_filtered (" exists\n");
5714 else
5715 printf_filtered (" faked for \"readnow\"\n");
5716 printf_filtered ("\n");
5717 }
5718
5719 static void
5720 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5721 const char *func_name)
5722 {
5723 struct dwarf2_per_objfile *dwarf2_per_objfile
5724 = get_dwarf2_per_objfile (objfile);
5725
5726 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5727 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5728 {
5729 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5730
5731 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5732
5733 struct dwarf2_per_cu_data *per_cu;
5734 while ((per_cu = iter.next ()) != NULL)
5735 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5736 }
5737 }
5738
5739 static void
5740 dw2_debug_names_map_matching_symbols
5741 (struct objfile *objfile,
5742 const lookup_name_info &name, domain_enum domain,
5743 int global,
5744 gdb::function_view<symbol_found_callback_ftype> callback,
5745 symbol_compare_ftype *ordered_compare)
5746 {
5747 struct dwarf2_per_objfile *dwarf2_per_objfile
5748 = get_dwarf2_per_objfile (objfile);
5749
5750 /* debug_names_table is NULL if OBJF_READNOW. */
5751 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5752 return;
5753
5754 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5755 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5756
5757 const char *match_name = name.ada ().lookup_name ().c_str ();
5758 auto matcher = [&] (const char *symname)
5759 {
5760 if (ordered_compare == nullptr)
5761 return true;
5762 return ordered_compare (symname, match_name) == 0;
5763 };
5764
5765 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5766 [&] (offset_type namei)
5767 {
5768 /* The name was matched, now expand corresponding CUs that were
5769 marked. */
5770 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5771
5772 struct dwarf2_per_cu_data *per_cu;
5773 while ((per_cu = iter.next ()) != NULL)
5774 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
5775 nullptr);
5776 return true;
5777 });
5778
5779 /* It's a shame we couldn't do this inside the
5780 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5781 that have already been expanded. Instead, this loop matches what
5782 the psymtab code does. */
5783 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5784 {
5785 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5786 if (symtab != nullptr)
5787 {
5788 const struct block *block
5789 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5790 if (!iterate_over_symbols_terminated (block, name,
5791 domain, callback))
5792 break;
5793 }
5794 }
5795 }
5796
5797 static void
5798 dw2_debug_names_expand_symtabs_matching
5799 (struct objfile *objfile,
5800 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5801 const lookup_name_info *lookup_name,
5802 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5803 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5804 enum search_domain kind)
5805 {
5806 struct dwarf2_per_objfile *dwarf2_per_objfile
5807 = get_dwarf2_per_objfile (objfile);
5808
5809 /* debug_names_table is NULL if OBJF_READNOW. */
5810 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5811 return;
5812
5813 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5814
5815 if (symbol_matcher == NULL && lookup_name == NULL)
5816 {
5817 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5818 {
5819 QUIT;
5820
5821 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5822 file_matcher, expansion_notify);
5823 }
5824 return;
5825 }
5826
5827 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5828
5829 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5830 symbol_matcher,
5831 kind, [&] (offset_type namei)
5832 {
5833 /* The name was matched, now expand corresponding CUs that were
5834 marked. */
5835 dw2_debug_names_iterator iter (map, kind, namei);
5836
5837 struct dwarf2_per_cu_data *per_cu;
5838 while ((per_cu = iter.next ()) != NULL)
5839 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5840 file_matcher, expansion_notify);
5841 return true;
5842 });
5843 }
5844
5845 const struct quick_symbol_functions dwarf2_debug_names_functions =
5846 {
5847 dw2_has_symbols,
5848 dw2_find_last_source_symtab,
5849 dw2_forget_cached_source_info,
5850 dw2_map_symtabs_matching_filename,
5851 dw2_debug_names_lookup_symbol,
5852 NULL,
5853 dw2_print_stats,
5854 dw2_debug_names_dump,
5855 dw2_debug_names_expand_symtabs_for_function,
5856 dw2_expand_all_symtabs,
5857 dw2_expand_symtabs_with_fullname,
5858 dw2_debug_names_map_matching_symbols,
5859 dw2_debug_names_expand_symtabs_matching,
5860 dw2_find_pc_sect_compunit_symtab,
5861 NULL,
5862 dw2_map_symbol_filenames
5863 };
5864
5865 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5866 to either a dwarf2_per_bfd or dwz_file object. */
5867
5868 template <typename T>
5869 static gdb::array_view<const gdb_byte>
5870 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5871 {
5872 dwarf2_section_info *section = &section_owner->gdb_index;
5873
5874 if (section->empty ())
5875 return {};
5876
5877 /* Older elfutils strip versions could keep the section in the main
5878 executable while splitting it for the separate debug info file. */
5879 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5880 return {};
5881
5882 section->read (obj);
5883
5884 /* dwarf2_section_info::size is a bfd_size_type, while
5885 gdb::array_view works with size_t. On 32-bit hosts, with
5886 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5887 is 32-bit. So we need an explicit narrowing conversion here.
5888 This is fine, because it's impossible to allocate or mmap an
5889 array/buffer larger than what size_t can represent. */
5890 return gdb::make_array_view (section->buffer, section->size);
5891 }
5892
5893 /* Lookup the index cache for the contents of the index associated to
5894 DWARF2_OBJ. */
5895
5896 static gdb::array_view<const gdb_byte>
5897 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5898 {
5899 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5900 if (build_id == nullptr)
5901 return {};
5902
5903 return global_index_cache.lookup_gdb_index (build_id,
5904 &dwarf2_per_bfd->index_cache_res);
5905 }
5906
5907 /* Same as the above, but for DWZ. */
5908
5909 static gdb::array_view<const gdb_byte>
5910 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5911 {
5912 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5913 if (build_id == nullptr)
5914 return {};
5915
5916 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5917 }
5918
5919 /* See symfile.h. */
5920
5921 bool
5922 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5923 {
5924 struct dwarf2_per_objfile *dwarf2_per_objfile
5925 = get_dwarf2_per_objfile (objfile);
5926
5927 /* If we're about to read full symbols, don't bother with the
5928 indices. In this case we also don't care if some other debug
5929 format is making psymtabs, because they are all about to be
5930 expanded anyway. */
5931 if ((objfile->flags & OBJF_READNOW))
5932 {
5933 dwarf2_per_objfile->per_bfd->using_index = 1;
5934 create_all_comp_units (dwarf2_per_objfile);
5935 create_all_type_units (dwarf2_per_objfile);
5936 dwarf2_per_objfile->per_bfd->quick_file_names_table
5937 = create_quick_file_names_table
5938 (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5939 dwarf2_per_objfile->resize_symtabs ();
5940
5941 for (int i = 0; i < (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
5942 + dwarf2_per_objfile->per_bfd->all_type_units.size ()); ++i)
5943 {
5944 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
5945
5946 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
5947 struct dwarf2_per_cu_quick_data);
5948 }
5949
5950 /* Return 1 so that gdb sees the "quick" functions. However,
5951 these functions will be no-ops because we will have expanded
5952 all symtabs. */
5953 *index_kind = dw_index_kind::GDB_INDEX;
5954 return true;
5955 }
5956
5957 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5958 {
5959 *index_kind = dw_index_kind::DEBUG_NAMES;
5960 dwarf2_per_objfile->resize_symtabs ();
5961 return true;
5962 }
5963
5964 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5965 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5966 get_gdb_index_contents_from_section<dwz_file>))
5967 {
5968 *index_kind = dw_index_kind::GDB_INDEX;
5969 dwarf2_per_objfile->resize_symtabs ();
5970 return true;
5971 }
5972
5973 /* ... otherwise, try to find the index in the index cache. */
5974 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5975 get_gdb_index_contents_from_cache,
5976 get_gdb_index_contents_from_cache_dwz))
5977 {
5978 global_index_cache.hit ();
5979 *index_kind = dw_index_kind::GDB_INDEX;
5980 dwarf2_per_objfile->resize_symtabs ();
5981 return true;
5982 }
5983
5984 global_index_cache.miss ();
5985 return false;
5986 }
5987
5988 \f
5989
5990 /* Build a partial symbol table. */
5991
5992 void
5993 dwarf2_build_psymtabs (struct objfile *objfile)
5994 {
5995 struct dwarf2_per_objfile *dwarf2_per_objfile
5996 = get_dwarf2_per_objfile (objfile);
5997
5998 init_psymbol_list (objfile, 1024);
5999
6000 try
6001 {
6002 /* This isn't really ideal: all the data we allocate on the
6003 objfile's obstack is still uselessly kept around. However,
6004 freeing it seems unsafe. */
6005 psymtab_discarder psymtabs (objfile);
6006 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6007 psymtabs.keep ();
6008
6009 dwarf2_per_objfile->resize_symtabs ();
6010
6011 /* (maybe) store an index in the cache. */
6012 global_index_cache.store (dwarf2_per_objfile);
6013 }
6014 catch (const gdb_exception_error &except)
6015 {
6016 exception_print (gdb_stderr, except);
6017 }
6018 }
6019
6020 /* Find the base address of the compilation unit for range lists and
6021 location lists. It will normally be specified by DW_AT_low_pc.
6022 In DWARF-3 draft 4, the base address could be overridden by
6023 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6024 compilation units with discontinuous ranges. */
6025
6026 static void
6027 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6028 {
6029 struct attribute *attr;
6030
6031 cu->base_address.reset ();
6032
6033 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6034 if (attr != nullptr)
6035 cu->base_address = attr->value_as_address ();
6036 else
6037 {
6038 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6039 if (attr != nullptr)
6040 cu->base_address = attr->value_as_address ();
6041 }
6042 }
6043
6044 /* Helper function that returns the proper abbrev section for
6045 THIS_CU. */
6046
6047 static struct dwarf2_section_info *
6048 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6049 {
6050 struct dwarf2_section_info *abbrev;
6051 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6052
6053 if (this_cu->is_dwz)
6054 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6055 else
6056 abbrev = &per_bfd->abbrev;
6057
6058 return abbrev;
6059 }
6060
6061 /* Fetch the abbreviation table offset from a comp or type unit header. */
6062
6063 static sect_offset
6064 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6065 struct dwarf2_section_info *section,
6066 sect_offset sect_off)
6067 {
6068 bfd *abfd = section->get_bfd_owner ();
6069 const gdb_byte *info_ptr;
6070 unsigned int initial_length_size, offset_size;
6071 uint16_t version;
6072
6073 section->read (dwarf2_per_objfile->objfile);
6074 info_ptr = section->buffer + to_underlying (sect_off);
6075 read_initial_length (abfd, info_ptr, &initial_length_size);
6076 offset_size = initial_length_size == 4 ? 4 : 8;
6077 info_ptr += initial_length_size;
6078
6079 version = read_2_bytes (abfd, info_ptr);
6080 info_ptr += 2;
6081 if (version >= 5)
6082 {
6083 /* Skip unit type and address size. */
6084 info_ptr += 2;
6085 }
6086
6087 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6088 }
6089
6090 /* A partial symtab that is used only for include files. */
6091 struct dwarf2_include_psymtab : public partial_symtab
6092 {
6093 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6094 : partial_symtab (filename, objfile)
6095 {
6096 }
6097
6098 void read_symtab (struct objfile *objfile) override
6099 {
6100 /* It's an include file, no symbols to read for it.
6101 Everything is in the includer symtab. */
6102
6103 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6104 expansion of the includer psymtab. We use the dependencies[0] field to
6105 model the includer. But if we go the regular route of calling
6106 expand_psymtab here, and having expand_psymtab call expand_dependencies
6107 to expand the includer, we'll only use expand_psymtab on the includer
6108 (making it a non-toplevel psymtab), while if we expand the includer via
6109 another path, we'll use read_symtab (making it a toplevel psymtab).
6110 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6111 psymtab, and trigger read_symtab on the includer here directly. */
6112 includer ()->read_symtab (objfile);
6113 }
6114
6115 void expand_psymtab (struct objfile *objfile) override
6116 {
6117 /* This is not called by read_symtab, and should not be called by any
6118 expand_dependencies. */
6119 gdb_assert (false);
6120 }
6121
6122 bool readin_p (struct objfile *objfile) const override
6123 {
6124 return includer ()->readin_p (objfile);
6125 }
6126
6127 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6128 {
6129 return nullptr;
6130 }
6131
6132 private:
6133 partial_symtab *includer () const
6134 {
6135 /* An include psymtab has exactly one dependency: the psymtab that
6136 includes it. */
6137 gdb_assert (this->number_of_dependencies == 1);
6138 return this->dependencies[0];
6139 }
6140 };
6141
6142 /* Allocate a new partial symtab for file named NAME and mark this new
6143 partial symtab as being an include of PST. */
6144
6145 static void
6146 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6147 struct objfile *objfile)
6148 {
6149 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6150
6151 if (!IS_ABSOLUTE_PATH (subpst->filename))
6152 subpst->dirname = pst->dirname;
6153
6154 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6155 subpst->dependencies[0] = pst;
6156 subpst->number_of_dependencies = 1;
6157 }
6158
6159 /* Read the Line Number Program data and extract the list of files
6160 included by the source file represented by PST. Build an include
6161 partial symtab for each of these included files. */
6162
6163 static void
6164 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6165 struct die_info *die,
6166 dwarf2_psymtab *pst)
6167 {
6168 line_header_up lh;
6169 struct attribute *attr;
6170
6171 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6172 if (attr != nullptr)
6173 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6174 if (lh == NULL)
6175 return; /* No linetable, so no includes. */
6176
6177 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6178 that we pass in the raw text_low here; that is ok because we're
6179 only decoding the line table to make include partial symtabs, and
6180 so the addresses aren't really used. */
6181 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6182 pst->raw_text_low (), 1);
6183 }
6184
6185 static hashval_t
6186 hash_signatured_type (const void *item)
6187 {
6188 const struct signatured_type *sig_type
6189 = (const struct signatured_type *) item;
6190
6191 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6192 return sig_type->signature;
6193 }
6194
6195 static int
6196 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6197 {
6198 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6199 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6200
6201 return lhs->signature == rhs->signature;
6202 }
6203
6204 /* Allocate a hash table for signatured types. */
6205
6206 static htab_up
6207 allocate_signatured_type_table ()
6208 {
6209 return htab_up (htab_create_alloc (41,
6210 hash_signatured_type,
6211 eq_signatured_type,
6212 NULL, xcalloc, xfree));
6213 }
6214
6215 /* A helper function to add a signatured type CU to a table. */
6216
6217 static int
6218 add_signatured_type_cu_to_table (void **slot, void *datum)
6219 {
6220 struct signatured_type *sigt = (struct signatured_type *) *slot;
6221 std::vector<signatured_type *> *all_type_units
6222 = (std::vector<signatured_type *> *) datum;
6223
6224 all_type_units->push_back (sigt);
6225
6226 return 1;
6227 }
6228
6229 /* A helper for create_debug_types_hash_table. Read types from SECTION
6230 and fill them into TYPES_HTAB. It will process only type units,
6231 therefore DW_UT_type. */
6232
6233 static void
6234 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6235 struct dwo_file *dwo_file,
6236 dwarf2_section_info *section, htab_up &types_htab,
6237 rcuh_kind section_kind)
6238 {
6239 struct objfile *objfile = dwarf2_per_objfile->objfile;
6240 struct dwarf2_section_info *abbrev_section;
6241 bfd *abfd;
6242 const gdb_byte *info_ptr, *end_ptr;
6243
6244 abbrev_section = (dwo_file != NULL
6245 ? &dwo_file->sections.abbrev
6246 : &dwarf2_per_objfile->per_bfd->abbrev);
6247
6248 if (dwarf_read_debug)
6249 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6250 section->get_name (),
6251 abbrev_section->get_file_name ());
6252
6253 section->read (objfile);
6254 info_ptr = section->buffer;
6255
6256 if (info_ptr == NULL)
6257 return;
6258
6259 /* We can't set abfd until now because the section may be empty or
6260 not present, in which case the bfd is unknown. */
6261 abfd = section->get_bfd_owner ();
6262
6263 /* We don't use cutu_reader here because we don't need to read
6264 any dies: the signature is in the header. */
6265
6266 end_ptr = info_ptr + section->size;
6267 while (info_ptr < end_ptr)
6268 {
6269 struct signatured_type *sig_type;
6270 struct dwo_unit *dwo_tu;
6271 void **slot;
6272 const gdb_byte *ptr = info_ptr;
6273 struct comp_unit_head header;
6274 unsigned int length;
6275
6276 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6277
6278 /* Initialize it due to a false compiler warning. */
6279 header.signature = -1;
6280 header.type_cu_offset_in_tu = (cu_offset) -1;
6281
6282 /* We need to read the type's signature in order to build the hash
6283 table, but we don't need anything else just yet. */
6284
6285 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6286 abbrev_section, ptr, section_kind);
6287
6288 length = header.get_length ();
6289
6290 /* Skip dummy type units. */
6291 if (ptr >= info_ptr + length
6292 || peek_abbrev_code (abfd, ptr) == 0
6293 || header.unit_type != DW_UT_type)
6294 {
6295 info_ptr += length;
6296 continue;
6297 }
6298
6299 if (types_htab == NULL)
6300 {
6301 if (dwo_file)
6302 types_htab = allocate_dwo_unit_table ();
6303 else
6304 types_htab = allocate_signatured_type_table ();
6305 }
6306
6307 if (dwo_file)
6308 {
6309 sig_type = NULL;
6310 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6311 struct dwo_unit);
6312 dwo_tu->dwo_file = dwo_file;
6313 dwo_tu->signature = header.signature;
6314 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6315 dwo_tu->section = section;
6316 dwo_tu->sect_off = sect_off;
6317 dwo_tu->length = length;
6318 }
6319 else
6320 {
6321 /* N.B.: type_offset is not usable if this type uses a DWO file.
6322 The real type_offset is in the DWO file. */
6323 dwo_tu = NULL;
6324 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6325 sig_type->signature = header.signature;
6326 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6327 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6328 sig_type->per_cu.is_debug_types = 1;
6329 sig_type->per_cu.section = section;
6330 sig_type->per_cu.sect_off = sect_off;
6331 sig_type->per_cu.length = length;
6332 }
6333
6334 slot = htab_find_slot (types_htab.get (),
6335 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6336 INSERT);
6337 gdb_assert (slot != NULL);
6338 if (*slot != NULL)
6339 {
6340 sect_offset dup_sect_off;
6341
6342 if (dwo_file)
6343 {
6344 const struct dwo_unit *dup_tu
6345 = (const struct dwo_unit *) *slot;
6346
6347 dup_sect_off = dup_tu->sect_off;
6348 }
6349 else
6350 {
6351 const struct signatured_type *dup_tu
6352 = (const struct signatured_type *) *slot;
6353
6354 dup_sect_off = dup_tu->per_cu.sect_off;
6355 }
6356
6357 complaint (_("debug type entry at offset %s is duplicate to"
6358 " the entry at offset %s, signature %s"),
6359 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6360 hex_string (header.signature));
6361 }
6362 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6363
6364 if (dwarf_read_debug > 1)
6365 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6366 sect_offset_str (sect_off),
6367 hex_string (header.signature));
6368
6369 info_ptr += length;
6370 }
6371 }
6372
6373 /* Create the hash table of all entries in the .debug_types
6374 (or .debug_types.dwo) section(s).
6375 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6376 otherwise it is NULL.
6377
6378 The result is a pointer to the hash table or NULL if there are no types.
6379
6380 Note: This function processes DWO files only, not DWP files. */
6381
6382 static void
6383 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6384 struct dwo_file *dwo_file,
6385 gdb::array_view<dwarf2_section_info> type_sections,
6386 htab_up &types_htab)
6387 {
6388 for (dwarf2_section_info &section : type_sections)
6389 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6390 types_htab, rcuh_kind::TYPE);
6391 }
6392
6393 /* Create the hash table of all entries in the .debug_types section,
6394 and initialize all_type_units.
6395 The result is zero if there is an error (e.g. missing .debug_types section),
6396 otherwise non-zero. */
6397
6398 static int
6399 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6400 {
6401 htab_up types_htab;
6402
6403 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6404 &dwarf2_per_objfile->per_bfd->info, types_htab,
6405 rcuh_kind::COMPILE);
6406 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6407 dwarf2_per_objfile->per_bfd->types, types_htab);
6408 if (types_htab == NULL)
6409 {
6410 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
6411 return 0;
6412 }
6413
6414 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
6415
6416 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6417 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6418 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
6419
6420 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6421 add_signatured_type_cu_to_table,
6422 &dwarf2_per_objfile->per_bfd->all_type_units);
6423
6424 return 1;
6425 }
6426
6427 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6428 If SLOT is non-NULL, it is the entry to use in the hash table.
6429 Otherwise we find one. */
6430
6431 static struct signatured_type *
6432 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6433 void **slot)
6434 {
6435 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6436 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6437 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6438
6439 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6440
6441 dwarf2_per_objfile->resize_symtabs ();
6442
6443 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6444 sig_type->signature = sig;
6445 sig_type->per_cu.is_debug_types = 1;
6446 if (dwarf2_per_objfile->per_bfd->using_index)
6447 {
6448 sig_type->per_cu.v.quick =
6449 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6450 struct dwarf2_per_cu_quick_data);
6451 }
6452
6453 if (slot == NULL)
6454 {
6455 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6456 sig_type, INSERT);
6457 }
6458 gdb_assert (*slot == NULL);
6459 *slot = sig_type;
6460 /* The rest of sig_type must be filled in by the caller. */
6461 return sig_type;
6462 }
6463
6464 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6465 Fill in SIG_ENTRY with DWO_ENTRY. */
6466
6467 static void
6468 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6469 struct signatured_type *sig_entry,
6470 struct dwo_unit *dwo_entry)
6471 {
6472 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6473
6474 /* Make sure we're not clobbering something we don't expect to. */
6475 gdb_assert (! sig_entry->per_cu.queued);
6476 gdb_assert (sig_entry->per_cu.cu == NULL);
6477 if (per_bfd->using_index)
6478 {
6479 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6480 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6481 }
6482 else
6483 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6484 gdb_assert (sig_entry->signature == dwo_entry->signature);
6485 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6486 gdb_assert (sig_entry->type_unit_group == NULL);
6487 gdb_assert (sig_entry->dwo_unit == NULL);
6488
6489 sig_entry->per_cu.section = dwo_entry->section;
6490 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6491 sig_entry->per_cu.length = dwo_entry->length;
6492 sig_entry->per_cu.reading_dwo_directly = 1;
6493 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6494 sig_entry->per_cu.per_bfd = per_bfd;
6495 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6496 sig_entry->dwo_unit = dwo_entry;
6497 }
6498
6499 /* Subroutine of lookup_signatured_type.
6500 If we haven't read the TU yet, create the signatured_type data structure
6501 for a TU to be read in directly from a DWO file, bypassing the stub.
6502 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6503 using .gdb_index, then when reading a CU we want to stay in the DWO file
6504 containing that CU. Otherwise we could end up reading several other DWO
6505 files (due to comdat folding) to process the transitive closure of all the
6506 mentioned TUs, and that can be slow. The current DWO file will have every
6507 type signature that it needs.
6508 We only do this for .gdb_index because in the psymtab case we already have
6509 to read all the DWOs to build the type unit groups. */
6510
6511 static struct signatured_type *
6512 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6513 {
6514 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6515 struct dwo_file *dwo_file;
6516 struct dwo_unit find_dwo_entry, *dwo_entry;
6517 struct signatured_type find_sig_entry, *sig_entry;
6518 void **slot;
6519
6520 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6521
6522 /* If TU skeletons have been removed then we may not have read in any
6523 TUs yet. */
6524 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6525 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6526
6527 /* We only ever need to read in one copy of a signatured type.
6528 Use the global signatured_types array to do our own comdat-folding
6529 of types. If this is the first time we're reading this TU, and
6530 the TU has an entry in .gdb_index, replace the recorded data from
6531 .gdb_index with this TU. */
6532
6533 find_sig_entry.signature = sig;
6534 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6535 &find_sig_entry, INSERT);
6536 sig_entry = (struct signatured_type *) *slot;
6537
6538 /* We can get here with the TU already read, *or* in the process of being
6539 read. Don't reassign the global entry to point to this DWO if that's
6540 the case. Also note that if the TU is already being read, it may not
6541 have come from a DWO, the program may be a mix of Fission-compiled
6542 code and non-Fission-compiled code. */
6543
6544 /* Have we already tried to read this TU?
6545 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6546 needn't exist in the global table yet). */
6547 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6548 return sig_entry;
6549
6550 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6551 dwo_unit of the TU itself. */
6552 dwo_file = cu->dwo_unit->dwo_file;
6553
6554 /* Ok, this is the first time we're reading this TU. */
6555 if (dwo_file->tus == NULL)
6556 return NULL;
6557 find_dwo_entry.signature = sig;
6558 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6559 &find_dwo_entry);
6560 if (dwo_entry == NULL)
6561 return NULL;
6562
6563 /* If the global table doesn't have an entry for this TU, add one. */
6564 if (sig_entry == NULL)
6565 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6566
6567 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6568 sig_entry->per_cu.tu_read = 1;
6569 return sig_entry;
6570 }
6571
6572 /* Subroutine of lookup_signatured_type.
6573 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6574 then try the DWP file. If the TU stub (skeleton) has been removed then
6575 it won't be in .gdb_index. */
6576
6577 static struct signatured_type *
6578 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6579 {
6580 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6581 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6582 struct dwo_unit *dwo_entry;
6583 struct signatured_type find_sig_entry, *sig_entry;
6584 void **slot;
6585
6586 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6587 gdb_assert (dwp_file != NULL);
6588
6589 /* If TU skeletons have been removed then we may not have read in any
6590 TUs yet. */
6591 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6592 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6593
6594 find_sig_entry.signature = sig;
6595 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6596 &find_sig_entry, INSERT);
6597 sig_entry = (struct signatured_type *) *slot;
6598
6599 /* Have we already tried to read this TU?
6600 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6601 needn't exist in the global table yet). */
6602 if (sig_entry != NULL)
6603 return sig_entry;
6604
6605 if (dwp_file->tus == NULL)
6606 return NULL;
6607 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6608 sig, 1 /* is_debug_types */);
6609 if (dwo_entry == NULL)
6610 return NULL;
6611
6612 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6613 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6614
6615 return sig_entry;
6616 }
6617
6618 /* Lookup a signature based type for DW_FORM_ref_sig8.
6619 Returns NULL if signature SIG is not present in the table.
6620 It is up to the caller to complain about this. */
6621
6622 static struct signatured_type *
6623 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6624 {
6625 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6626
6627 if (cu->dwo_unit
6628 && dwarf2_per_objfile->per_bfd->using_index)
6629 {
6630 /* We're in a DWO/DWP file, and we're using .gdb_index.
6631 These cases require special processing. */
6632 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6633 return lookup_dwo_signatured_type (cu, sig);
6634 else
6635 return lookup_dwp_signatured_type (cu, sig);
6636 }
6637 else
6638 {
6639 struct signatured_type find_entry, *entry;
6640
6641 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6642 return NULL;
6643 find_entry.signature = sig;
6644 entry = ((struct signatured_type *)
6645 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6646 &find_entry));
6647 return entry;
6648 }
6649 }
6650
6651 /* Low level DIE reading support. */
6652
6653 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6654
6655 static void
6656 init_cu_die_reader (struct die_reader_specs *reader,
6657 struct dwarf2_cu *cu,
6658 struct dwarf2_section_info *section,
6659 struct dwo_file *dwo_file,
6660 struct abbrev_table *abbrev_table)
6661 {
6662 gdb_assert (section->readin && section->buffer != NULL);
6663 reader->abfd = section->get_bfd_owner ();
6664 reader->cu = cu;
6665 reader->dwo_file = dwo_file;
6666 reader->die_section = section;
6667 reader->buffer = section->buffer;
6668 reader->buffer_end = section->buffer + section->size;
6669 reader->abbrev_table = abbrev_table;
6670 }
6671
6672 /* Subroutine of cutu_reader to simplify it.
6673 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6674 There's just a lot of work to do, and cutu_reader is big enough
6675 already.
6676
6677 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6678 from it to the DIE in the DWO. If NULL we are skipping the stub.
6679 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6680 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6681 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6682 STUB_COMP_DIR may be non-NULL.
6683 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6684 are filled in with the info of the DIE from the DWO file.
6685 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6686 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6687 kept around for at least as long as *RESULT_READER.
6688
6689 The result is non-zero if a valid (non-dummy) DIE was found. */
6690
6691 static int
6692 read_cutu_die_from_dwo (dwarf2_cu *cu,
6693 struct dwo_unit *dwo_unit,
6694 struct die_info *stub_comp_unit_die,
6695 const char *stub_comp_dir,
6696 struct die_reader_specs *result_reader,
6697 const gdb_byte **result_info_ptr,
6698 struct die_info **result_comp_unit_die,
6699 abbrev_table_up *result_dwo_abbrev_table)
6700 {
6701 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6702 dwarf2_per_cu_data *per_cu = cu->per_cu;
6703 struct objfile *objfile = dwarf2_per_objfile->objfile;
6704 bfd *abfd;
6705 const gdb_byte *begin_info_ptr, *info_ptr;
6706 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6707 int i,num_extra_attrs;
6708 struct dwarf2_section_info *dwo_abbrev_section;
6709 struct die_info *comp_unit_die;
6710
6711 /* At most one of these may be provided. */
6712 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6713
6714 /* These attributes aren't processed until later:
6715 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6716 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6717 referenced later. However, these attributes are found in the stub
6718 which we won't have later. In order to not impose this complication
6719 on the rest of the code, we read them here and copy them to the
6720 DWO CU/TU die. */
6721
6722 stmt_list = NULL;
6723 low_pc = NULL;
6724 high_pc = NULL;
6725 ranges = NULL;
6726 comp_dir = NULL;
6727
6728 if (stub_comp_unit_die != NULL)
6729 {
6730 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6731 DWO file. */
6732 if (!per_cu->is_debug_types)
6733 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6734 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6735 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6736 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6737 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6738
6739 cu->addr_base = stub_comp_unit_die->addr_base ();
6740
6741 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6742 here (if needed). We need the value before we can process
6743 DW_AT_ranges. */
6744 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6745 }
6746 else if (stub_comp_dir != NULL)
6747 {
6748 /* Reconstruct the comp_dir attribute to simplify the code below. */
6749 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6750 comp_dir->name = DW_AT_comp_dir;
6751 comp_dir->form = DW_FORM_string;
6752 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6753 DW_STRING (comp_dir) = stub_comp_dir;
6754 }
6755
6756 /* Set up for reading the DWO CU/TU. */
6757 cu->dwo_unit = dwo_unit;
6758 dwarf2_section_info *section = dwo_unit->section;
6759 section->read (objfile);
6760 abfd = section->get_bfd_owner ();
6761 begin_info_ptr = info_ptr = (section->buffer
6762 + to_underlying (dwo_unit->sect_off));
6763 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6764
6765 if (per_cu->is_debug_types)
6766 {
6767 signatured_type *sig_type = (struct signatured_type *) per_cu;
6768
6769 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6770 &cu->header, section,
6771 dwo_abbrev_section,
6772 info_ptr, rcuh_kind::TYPE);
6773 /* This is not an assert because it can be caused by bad debug info. */
6774 if (sig_type->signature != cu->header.signature)
6775 {
6776 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6777 " TU at offset %s [in module %s]"),
6778 hex_string (sig_type->signature),
6779 hex_string (cu->header.signature),
6780 sect_offset_str (dwo_unit->sect_off),
6781 bfd_get_filename (abfd));
6782 }
6783 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6784 /* For DWOs coming from DWP files, we don't know the CU length
6785 nor the type's offset in the TU until now. */
6786 dwo_unit->length = cu->header.get_length ();
6787 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6788
6789 /* Establish the type offset that can be used to lookup the type.
6790 For DWO files, we don't know it until now. */
6791 sig_type->type_offset_in_section
6792 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6793 }
6794 else
6795 {
6796 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6797 &cu->header, section,
6798 dwo_abbrev_section,
6799 info_ptr, rcuh_kind::COMPILE);
6800 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6801 /* For DWOs coming from DWP files, we don't know the CU length
6802 until now. */
6803 dwo_unit->length = cu->header.get_length ();
6804 }
6805
6806 *result_dwo_abbrev_table
6807 = abbrev_table::read (objfile, dwo_abbrev_section,
6808 cu->header.abbrev_sect_off);
6809 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6810 result_dwo_abbrev_table->get ());
6811
6812 /* Read in the die, but leave space to copy over the attributes
6813 from the stub. This has the benefit of simplifying the rest of
6814 the code - all the work to maintain the illusion of a single
6815 DW_TAG_{compile,type}_unit DIE is done here. */
6816 num_extra_attrs = ((stmt_list != NULL)
6817 + (low_pc != NULL)
6818 + (high_pc != NULL)
6819 + (ranges != NULL)
6820 + (comp_dir != NULL));
6821 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6822 num_extra_attrs);
6823
6824 /* Copy over the attributes from the stub to the DIE we just read in. */
6825 comp_unit_die = *result_comp_unit_die;
6826 i = comp_unit_die->num_attrs;
6827 if (stmt_list != NULL)
6828 comp_unit_die->attrs[i++] = *stmt_list;
6829 if (low_pc != NULL)
6830 comp_unit_die->attrs[i++] = *low_pc;
6831 if (high_pc != NULL)
6832 comp_unit_die->attrs[i++] = *high_pc;
6833 if (ranges != NULL)
6834 comp_unit_die->attrs[i++] = *ranges;
6835 if (comp_dir != NULL)
6836 comp_unit_die->attrs[i++] = *comp_dir;
6837 comp_unit_die->num_attrs += num_extra_attrs;
6838
6839 if (dwarf_die_debug)
6840 {
6841 fprintf_unfiltered (gdb_stdlog,
6842 "Read die from %s@0x%x of %s:\n",
6843 section->get_name (),
6844 (unsigned) (begin_info_ptr - section->buffer),
6845 bfd_get_filename (abfd));
6846 dump_die (comp_unit_die, dwarf_die_debug);
6847 }
6848
6849 /* Skip dummy compilation units. */
6850 if (info_ptr >= begin_info_ptr + dwo_unit->length
6851 || peek_abbrev_code (abfd, info_ptr) == 0)
6852 return 0;
6853
6854 *result_info_ptr = info_ptr;
6855 return 1;
6856 }
6857
6858 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6859 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6860 signature is part of the header. */
6861 static gdb::optional<ULONGEST>
6862 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6863 {
6864 if (cu->header.version >= 5)
6865 return cu->header.signature;
6866 struct attribute *attr;
6867 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6868 if (attr == nullptr)
6869 return gdb::optional<ULONGEST> ();
6870 return DW_UNSND (attr);
6871 }
6872
6873 /* Subroutine of cutu_reader to simplify it.
6874 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6875 Returns NULL if the specified DWO unit cannot be found. */
6876
6877 static struct dwo_unit *
6878 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6879 {
6880 dwarf2_per_cu_data *per_cu = cu->per_cu;
6881 struct dwo_unit *dwo_unit;
6882 const char *comp_dir;
6883
6884 gdb_assert (cu != NULL);
6885
6886 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6887 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6888 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6889
6890 if (per_cu->is_debug_types)
6891 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6892 else
6893 {
6894 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6895
6896 if (!signature.has_value ())
6897 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6898 " [in module %s]"),
6899 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6900
6901 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6902 }
6903
6904 return dwo_unit;
6905 }
6906
6907 /* Subroutine of cutu_reader to simplify it.
6908 See it for a description of the parameters.
6909 Read a TU directly from a DWO file, bypassing the stub. */
6910
6911 void
6912 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6913 dwarf2_per_objfile *per_objfile,
6914 int use_existing_cu)
6915 {
6916 struct signatured_type *sig_type;
6917
6918 /* Verify we can do the following downcast, and that we have the
6919 data we need. */
6920 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6921 sig_type = (struct signatured_type *) this_cu;
6922 gdb_assert (sig_type->dwo_unit != NULL);
6923
6924 if (use_existing_cu && this_cu->cu != NULL)
6925 {
6926 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6927 /* There's no need to do the rereading_dwo_cu handling that
6928 cutu_reader does since we don't read the stub. */
6929 }
6930 else
6931 {
6932 /* If !use_existing_cu, this_cu->cu must be NULL. */
6933 gdb_assert (this_cu->cu == NULL);
6934 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6935 }
6936
6937 /* A future optimization, if needed, would be to use an existing
6938 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6939 could share abbrev tables. */
6940
6941 if (read_cutu_die_from_dwo (this_cu->cu, sig_type->dwo_unit,
6942 NULL /* stub_comp_unit_die */,
6943 sig_type->dwo_unit->dwo_file->comp_dir,
6944 this, &info_ptr,
6945 &comp_unit_die,
6946 &m_dwo_abbrev_table) == 0)
6947 {
6948 /* Dummy die. */
6949 dummy_p = true;
6950 }
6951 }
6952
6953 /* Initialize a CU (or TU) and read its DIEs.
6954 If the CU defers to a DWO file, read the DWO file as well.
6955
6956 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6957 Otherwise the table specified in the comp unit header is read in and used.
6958 This is an optimization for when we already have the abbrev table.
6959
6960 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6961 Otherwise, a new CU is allocated with xmalloc. */
6962
6963 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6964 dwarf2_per_objfile *dwarf2_per_objfile,
6965 struct abbrev_table *abbrev_table,
6966 int use_existing_cu,
6967 bool skip_partial)
6968 : die_reader_specs {},
6969 m_this_cu (this_cu)
6970 {
6971 struct objfile *objfile = dwarf2_per_objfile->objfile;
6972 struct dwarf2_section_info *section = this_cu->section;
6973 bfd *abfd = section->get_bfd_owner ();
6974 struct dwarf2_cu *cu;
6975 const gdb_byte *begin_info_ptr;
6976 struct signatured_type *sig_type = NULL;
6977 struct dwarf2_section_info *abbrev_section;
6978 /* Non-zero if CU currently points to a DWO file and we need to
6979 reread it. When this happens we need to reread the skeleton die
6980 before we can reread the DWO file (this only applies to CUs, not TUs). */
6981 int rereading_dwo_cu = 0;
6982
6983 if (dwarf_die_debug)
6984 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6985 this_cu->is_debug_types ? "type" : "comp",
6986 sect_offset_str (this_cu->sect_off));
6987
6988 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6989 file (instead of going through the stub), short-circuit all of this. */
6990 if (this_cu->reading_dwo_directly)
6991 {
6992 /* Narrow down the scope of possibilities to have to understand. */
6993 gdb_assert (this_cu->is_debug_types);
6994 gdb_assert (abbrev_table == NULL);
6995 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, use_existing_cu);
6996 return;
6997 }
6998
6999 /* This is cheap if the section is already read in. */
7000 section->read (objfile);
7001
7002 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7003
7004 abbrev_section = get_abbrev_section_for_cu (this_cu);
7005
7006 if (use_existing_cu && this_cu->cu != NULL)
7007 {
7008 cu = this_cu->cu;
7009 /* If this CU is from a DWO file we need to start over, we need to
7010 refetch the attributes from the skeleton CU.
7011 This could be optimized by retrieving those attributes from when we
7012 were here the first time: the previous comp_unit_die was stored in
7013 comp_unit_obstack. But there's no data yet that we need this
7014 optimization. */
7015 if (cu->dwo_unit != NULL)
7016 rereading_dwo_cu = 1;
7017 }
7018 else
7019 {
7020 /* If !use_existing_cu, this_cu->cu must be NULL. */
7021 gdb_assert (this_cu->cu == NULL);
7022 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7023 cu = m_new_cu.get ();
7024 }
7025
7026 /* Get the header. */
7027 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7028 {
7029 /* We already have the header, there's no need to read it in again. */
7030 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7031 }
7032 else
7033 {
7034 if (this_cu->is_debug_types)
7035 {
7036 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7037 &cu->header, section,
7038 abbrev_section, info_ptr,
7039 rcuh_kind::TYPE);
7040
7041 /* Since per_cu is the first member of struct signatured_type,
7042 we can go from a pointer to one to a pointer to the other. */
7043 sig_type = (struct signatured_type *) this_cu;
7044 gdb_assert (sig_type->signature == cu->header.signature);
7045 gdb_assert (sig_type->type_offset_in_tu
7046 == cu->header.type_cu_offset_in_tu);
7047 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7048
7049 /* LENGTH has not been set yet for type units if we're
7050 using .gdb_index. */
7051 this_cu->length = cu->header.get_length ();
7052
7053 /* Establish the type offset that can be used to lookup the type. */
7054 sig_type->type_offset_in_section =
7055 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7056
7057 this_cu->dwarf_version = cu->header.version;
7058 }
7059 else
7060 {
7061 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7062 &cu->header, section,
7063 abbrev_section,
7064 info_ptr,
7065 rcuh_kind::COMPILE);
7066
7067 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7068 if (this_cu->length == 0)
7069 this_cu->length = cu->header.get_length ();
7070 else
7071 gdb_assert (this_cu->length == cu->header.get_length ());
7072 this_cu->dwarf_version = cu->header.version;
7073 }
7074 }
7075
7076 /* Skip dummy compilation units. */
7077 if (info_ptr >= begin_info_ptr + this_cu->length
7078 || peek_abbrev_code (abfd, info_ptr) == 0)
7079 {
7080 dummy_p = true;
7081 return;
7082 }
7083
7084 /* If we don't have them yet, read the abbrevs for this compilation unit.
7085 And if we need to read them now, make sure they're freed when we're
7086 done. */
7087 if (abbrev_table != NULL)
7088 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7089 else
7090 {
7091 m_abbrev_table_holder
7092 = abbrev_table::read (objfile, abbrev_section,
7093 cu->header.abbrev_sect_off);
7094 abbrev_table = m_abbrev_table_holder.get ();
7095 }
7096
7097 /* Read the top level CU/TU die. */
7098 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7099 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7100
7101 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7102 {
7103 dummy_p = true;
7104 return;
7105 }
7106
7107 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7108 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7109 table from the DWO file and pass the ownership over to us. It will be
7110 referenced from READER, so we must make sure to free it after we're done
7111 with READER.
7112
7113 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7114 DWO CU, that this test will fail (the attribute will not be present). */
7115 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7116 if (dwo_name != nullptr)
7117 {
7118 struct dwo_unit *dwo_unit;
7119 struct die_info *dwo_comp_unit_die;
7120
7121 if (comp_unit_die->has_children)
7122 {
7123 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7124 " has children (offset %s) [in module %s]"),
7125 sect_offset_str (this_cu->sect_off),
7126 bfd_get_filename (abfd));
7127 }
7128 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7129 if (dwo_unit != NULL)
7130 {
7131 if (read_cutu_die_from_dwo (cu, dwo_unit,
7132 comp_unit_die, NULL,
7133 this, &info_ptr,
7134 &dwo_comp_unit_die,
7135 &m_dwo_abbrev_table) == 0)
7136 {
7137 /* Dummy die. */
7138 dummy_p = true;
7139 return;
7140 }
7141 comp_unit_die = dwo_comp_unit_die;
7142 }
7143 else
7144 {
7145 /* Yikes, we couldn't find the rest of the DIE, we only have
7146 the stub. A complaint has already been logged. There's
7147 not much more we can do except pass on the stub DIE to
7148 die_reader_func. We don't want to throw an error on bad
7149 debug info. */
7150 }
7151 }
7152 }
7153
7154 void
7155 cutu_reader::keep ()
7156 {
7157 /* Done, clean up. */
7158 gdb_assert (!dummy_p);
7159 if (m_new_cu != NULL)
7160 {
7161 /* We know that m_this_cu->cu is set, since we are in the process of
7162 parsing the CU. */
7163 gdb_assert (m_this_cu->cu != nullptr);
7164 dwarf2_per_objfile *dwarf2_per_objfile = m_this_cu->cu->per_objfile;
7165
7166 /* Link this CU into read_in_chain. */
7167 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->per_bfd->read_in_chain;
7168 dwarf2_per_objfile->per_bfd->read_in_chain = m_this_cu;
7169 /* The chain owns it now. */
7170 m_new_cu.release ();
7171 }
7172 }
7173
7174 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7175 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7176 assumed to have already done the lookup to find the DWO file).
7177
7178 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7179 THIS_CU->is_debug_types, but nothing else.
7180
7181 We fill in THIS_CU->length.
7182
7183 THIS_CU->cu is always freed when done.
7184 This is done in order to not leave THIS_CU->cu in a state where we have
7185 to care whether it refers to the "main" CU or the DWO CU.
7186
7187 When parent_cu is passed, it is used to provide a default value for
7188 str_offsets_base and addr_base from the parent. */
7189
7190 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7191 dwarf2_per_objfile *dwarf2_per_objfile,
7192 struct dwarf2_cu *parent_cu,
7193 struct dwo_file *dwo_file)
7194 : die_reader_specs {},
7195 m_this_cu (this_cu)
7196 {
7197 struct objfile *objfile = dwarf2_per_objfile->objfile;
7198 struct dwarf2_section_info *section = this_cu->section;
7199 bfd *abfd = section->get_bfd_owner ();
7200 struct dwarf2_section_info *abbrev_section;
7201 const gdb_byte *begin_info_ptr, *info_ptr;
7202
7203 if (dwarf_die_debug)
7204 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7205 this_cu->is_debug_types ? "type" : "comp",
7206 sect_offset_str (this_cu->sect_off));
7207
7208 gdb_assert (this_cu->cu == NULL);
7209
7210 abbrev_section = (dwo_file != NULL
7211 ? &dwo_file->sections.abbrev
7212 : get_abbrev_section_for_cu (this_cu));
7213
7214 /* This is cheap if the section is already read in. */
7215 section->read (objfile);
7216
7217 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7218
7219 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7220 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7221 &m_new_cu->header, section,
7222 abbrev_section, info_ptr,
7223 (this_cu->is_debug_types
7224 ? rcuh_kind::TYPE
7225 : rcuh_kind::COMPILE));
7226
7227 if (parent_cu != nullptr)
7228 {
7229 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7230 m_new_cu->addr_base = parent_cu->addr_base;
7231 }
7232 this_cu->length = m_new_cu->header.get_length ();
7233
7234 /* Skip dummy compilation units. */
7235 if (info_ptr >= begin_info_ptr + this_cu->length
7236 || peek_abbrev_code (abfd, info_ptr) == 0)
7237 {
7238 dummy_p = true;
7239 return;
7240 }
7241
7242 m_abbrev_table_holder
7243 = abbrev_table::read (objfile, abbrev_section,
7244 m_new_cu->header.abbrev_sect_off);
7245
7246 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7247 m_abbrev_table_holder.get ());
7248 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7249 }
7250
7251 \f
7252 /* Type Unit Groups.
7253
7254 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7255 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7256 so that all types coming from the same compilation (.o file) are grouped
7257 together. A future step could be to put the types in the same symtab as
7258 the CU the types ultimately came from. */
7259
7260 static hashval_t
7261 hash_type_unit_group (const void *item)
7262 {
7263 const struct type_unit_group *tu_group
7264 = (const struct type_unit_group *) item;
7265
7266 return hash_stmt_list_entry (&tu_group->hash);
7267 }
7268
7269 static int
7270 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7271 {
7272 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7273 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7274
7275 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7276 }
7277
7278 /* Allocate a hash table for type unit groups. */
7279
7280 static htab_up
7281 allocate_type_unit_groups_table ()
7282 {
7283 return htab_up (htab_create_alloc (3,
7284 hash_type_unit_group,
7285 eq_type_unit_group,
7286 NULL, xcalloc, xfree));
7287 }
7288
7289 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7290 partial symtabs. We combine several TUs per psymtab to not let the size
7291 of any one psymtab grow too big. */
7292 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7293 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7294
7295 /* Helper routine for get_type_unit_group.
7296 Create the type_unit_group object used to hold one or more TUs. */
7297
7298 static struct type_unit_group *
7299 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7300 {
7301 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7302 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
7303 struct dwarf2_per_cu_data *per_cu;
7304 struct type_unit_group *tu_group;
7305
7306 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
7307 struct type_unit_group);
7308 per_cu = &tu_group->per_cu;
7309 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7310 per_cu->per_bfd = per_bfd;
7311
7312 if (per_bfd->using_index)
7313 {
7314 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7315 struct dwarf2_per_cu_quick_data);
7316 }
7317 else
7318 {
7319 unsigned int line_offset = to_underlying (line_offset_struct);
7320 dwarf2_psymtab *pst;
7321 std::string name;
7322
7323 /* Give the symtab a useful name for debug purposes. */
7324 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7325 name = string_printf ("<type_units_%d>",
7326 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7327 else
7328 name = string_printf ("<type_units_at_0x%x>", line_offset);
7329
7330 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, name.c_str ());
7331 pst->anonymous = true;
7332 }
7333
7334 tu_group->hash.dwo_unit = cu->dwo_unit;
7335 tu_group->hash.line_sect_off = line_offset_struct;
7336
7337 return tu_group;
7338 }
7339
7340 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7341 STMT_LIST is a DW_AT_stmt_list attribute. */
7342
7343 static struct type_unit_group *
7344 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7345 {
7346 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7347 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7348 struct type_unit_group *tu_group;
7349 void **slot;
7350 unsigned int line_offset;
7351 struct type_unit_group type_unit_group_for_lookup;
7352
7353 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7354 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7355
7356 /* Do we need to create a new group, or can we use an existing one? */
7357
7358 if (stmt_list)
7359 {
7360 line_offset = DW_UNSND (stmt_list);
7361 ++tu_stats->nr_symtab_sharers;
7362 }
7363 else
7364 {
7365 /* Ugh, no stmt_list. Rare, but we have to handle it.
7366 We can do various things here like create one group per TU or
7367 spread them over multiple groups to split up the expansion work.
7368 To avoid worst case scenarios (too many groups or too large groups)
7369 we, umm, group them in bunches. */
7370 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7371 | (tu_stats->nr_stmt_less_type_units
7372 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7373 ++tu_stats->nr_stmt_less_type_units;
7374 }
7375
7376 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7377 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7378 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7379 &type_unit_group_for_lookup, INSERT);
7380 if (*slot != NULL)
7381 {
7382 tu_group = (struct type_unit_group *) *slot;
7383 gdb_assert (tu_group != NULL);
7384 }
7385 else
7386 {
7387 sect_offset line_offset_struct = (sect_offset) line_offset;
7388 tu_group = create_type_unit_group (cu, line_offset_struct);
7389 *slot = tu_group;
7390 ++tu_stats->nr_symtabs;
7391 }
7392
7393 return tu_group;
7394 }
7395 \f
7396 /* Partial symbol tables. */
7397
7398 /* Create a psymtab named NAME and assign it to PER_CU.
7399
7400 The caller must fill in the following details:
7401 dirname, textlow, texthigh. */
7402
7403 static dwarf2_psymtab *
7404 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7405 dwarf2_per_objfile *per_objfile,
7406 const char *name)
7407 {
7408 struct objfile *objfile = per_objfile->objfile;
7409 dwarf2_psymtab *pst;
7410
7411 pst = new dwarf2_psymtab (name, objfile, per_cu);
7412
7413 pst->psymtabs_addrmap_supported = true;
7414
7415 /* This is the glue that links PST into GDB's symbol API. */
7416 per_cu->v.psymtab = pst;
7417
7418 return pst;
7419 }
7420
7421 /* DIE reader function for process_psymtab_comp_unit. */
7422
7423 static void
7424 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7425 const gdb_byte *info_ptr,
7426 struct die_info *comp_unit_die,
7427 enum language pretend_language)
7428 {
7429 struct dwarf2_cu *cu = reader->cu;
7430 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7431 struct objfile *objfile = per_objfile->objfile;
7432 struct gdbarch *gdbarch = objfile->arch ();
7433 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7434 CORE_ADDR baseaddr;
7435 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7436 dwarf2_psymtab *pst;
7437 enum pc_bounds_kind cu_bounds_kind;
7438 const char *filename;
7439
7440 gdb_assert (! per_cu->is_debug_types);
7441
7442 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7443
7444 /* Allocate a new partial symbol table structure. */
7445 gdb::unique_xmalloc_ptr<char> debug_filename;
7446 static const char artificial[] = "<artificial>";
7447 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7448 if (filename == NULL)
7449 filename = "";
7450 else if (strcmp (filename, artificial) == 0)
7451 {
7452 debug_filename.reset (concat (artificial, "@",
7453 sect_offset_str (per_cu->sect_off),
7454 (char *) NULL));
7455 filename = debug_filename.get ();
7456 }
7457
7458 pst = create_partial_symtab (per_cu, per_objfile, filename);
7459
7460 /* This must be done before calling dwarf2_build_include_psymtabs. */
7461 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7462
7463 baseaddr = objfile->text_section_offset ();
7464
7465 dwarf2_find_base_address (comp_unit_die, cu);
7466
7467 /* Possibly set the default values of LOWPC and HIGHPC from
7468 `DW_AT_ranges'. */
7469 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7470 &best_highpc, cu, pst);
7471 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7472 {
7473 CORE_ADDR low
7474 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7475 - baseaddr);
7476 CORE_ADDR high
7477 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7478 - baseaddr - 1);
7479 /* Store the contiguous range if it is not empty; it can be
7480 empty for CUs with no code. */
7481 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7482 low, high, pst);
7483 }
7484
7485 /* Check if comp unit has_children.
7486 If so, read the rest of the partial symbols from this comp unit.
7487 If not, there's no more debug_info for this comp unit. */
7488 if (comp_unit_die->has_children)
7489 {
7490 struct partial_die_info *first_die;
7491 CORE_ADDR lowpc, highpc;
7492
7493 lowpc = ((CORE_ADDR) -1);
7494 highpc = ((CORE_ADDR) 0);
7495
7496 first_die = load_partial_dies (reader, info_ptr, 1);
7497
7498 scan_partial_symbols (first_die, &lowpc, &highpc,
7499 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7500
7501 /* If we didn't find a lowpc, set it to highpc to avoid
7502 complaints from `maint check'. */
7503 if (lowpc == ((CORE_ADDR) -1))
7504 lowpc = highpc;
7505
7506 /* If the compilation unit didn't have an explicit address range,
7507 then use the information extracted from its child dies. */
7508 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7509 {
7510 best_lowpc = lowpc;
7511 best_highpc = highpc;
7512 }
7513 }
7514 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7515 best_lowpc + baseaddr)
7516 - baseaddr);
7517 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7518 best_highpc + baseaddr)
7519 - baseaddr);
7520
7521 end_psymtab_common (objfile, pst);
7522
7523 if (!cu->per_cu->imported_symtabs_empty ())
7524 {
7525 int i;
7526 int len = cu->per_cu->imported_symtabs_size ();
7527
7528 /* Fill in 'dependencies' here; we fill in 'users' in a
7529 post-pass. */
7530 pst->number_of_dependencies = len;
7531 pst->dependencies
7532 = objfile->partial_symtabs->allocate_dependencies (len);
7533 for (i = 0; i < len; ++i)
7534 {
7535 pst->dependencies[i]
7536 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7537 }
7538
7539 cu->per_cu->imported_symtabs_free ();
7540 }
7541
7542 /* Get the list of files included in the current compilation unit,
7543 and build a psymtab for each of them. */
7544 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7545
7546 if (dwarf_read_debug)
7547 fprintf_unfiltered (gdb_stdlog,
7548 "Psymtab for %s unit @%s: %s - %s"
7549 ", %d global, %d static syms\n",
7550 per_cu->is_debug_types ? "type" : "comp",
7551 sect_offset_str (per_cu->sect_off),
7552 paddress (gdbarch, pst->text_low (objfile)),
7553 paddress (gdbarch, pst->text_high (objfile)),
7554 pst->n_global_syms, pst->n_static_syms);
7555 }
7556
7557 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7558 Process compilation unit THIS_CU for a psymtab. */
7559
7560 static void
7561 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7562 dwarf2_per_objfile *per_objfile,
7563 bool want_partial_unit,
7564 enum language pretend_language)
7565 {
7566 /* If this compilation unit was already read in, free the
7567 cached copy in order to read it in again. This is
7568 necessary because we skipped some symbols when we first
7569 read in the compilation unit (see load_partial_dies).
7570 This problem could be avoided, but the benefit is unclear. */
7571 if (this_cu->cu != NULL)
7572 free_one_cached_comp_unit (this_cu);
7573
7574 cutu_reader reader (this_cu, per_objfile, NULL, 0, false);
7575
7576 switch (reader.comp_unit_die->tag)
7577 {
7578 case DW_TAG_compile_unit:
7579 this_cu->unit_type = DW_UT_compile;
7580 break;
7581 case DW_TAG_partial_unit:
7582 this_cu->unit_type = DW_UT_partial;
7583 break;
7584 default:
7585 abort ();
7586 }
7587
7588 if (reader.dummy_p)
7589 {
7590 /* Nothing. */
7591 }
7592 else if (this_cu->is_debug_types)
7593 build_type_psymtabs_reader (&reader, reader.info_ptr,
7594 reader.comp_unit_die);
7595 else if (want_partial_unit
7596 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7597 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7598 reader.comp_unit_die,
7599 pretend_language);
7600
7601 this_cu->lang = this_cu->cu->language;
7602
7603 /* Age out any secondary CUs. */
7604 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7605 }
7606
7607 /* Reader function for build_type_psymtabs. */
7608
7609 static void
7610 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7611 const gdb_byte *info_ptr,
7612 struct die_info *type_unit_die)
7613 {
7614 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
7615 struct objfile *objfile = dwarf2_per_objfile->objfile;
7616 struct dwarf2_cu *cu = reader->cu;
7617 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7618 struct signatured_type *sig_type;
7619 struct type_unit_group *tu_group;
7620 struct attribute *attr;
7621 struct partial_die_info *first_die;
7622 CORE_ADDR lowpc, highpc;
7623 dwarf2_psymtab *pst;
7624
7625 gdb_assert (per_cu->is_debug_types);
7626 sig_type = (struct signatured_type *) per_cu;
7627
7628 if (! type_unit_die->has_children)
7629 return;
7630
7631 attr = type_unit_die->attr (DW_AT_stmt_list);
7632 tu_group = get_type_unit_group (cu, attr);
7633
7634 if (tu_group->tus == nullptr)
7635 tu_group->tus = new std::vector<signatured_type *>;
7636 tu_group->tus->push_back (sig_type);
7637
7638 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7639 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, "");
7640 pst->anonymous = true;
7641
7642 first_die = load_partial_dies (reader, info_ptr, 1);
7643
7644 lowpc = (CORE_ADDR) -1;
7645 highpc = (CORE_ADDR) 0;
7646 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7647
7648 end_psymtab_common (objfile, pst);
7649 }
7650
7651 /* Struct used to sort TUs by their abbreviation table offset. */
7652
7653 struct tu_abbrev_offset
7654 {
7655 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7656 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7657 {}
7658
7659 signatured_type *sig_type;
7660 sect_offset abbrev_offset;
7661 };
7662
7663 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7664
7665 static bool
7666 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7667 const struct tu_abbrev_offset &b)
7668 {
7669 return a.abbrev_offset < b.abbrev_offset;
7670 }
7671
7672 /* Efficiently read all the type units.
7673 This does the bulk of the work for build_type_psymtabs.
7674
7675 The efficiency is because we sort TUs by the abbrev table they use and
7676 only read each abbrev table once. In one program there are 200K TUs
7677 sharing 8K abbrev tables.
7678
7679 The main purpose of this function is to support building the
7680 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7681 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7682 can collapse the search space by grouping them by stmt_list.
7683 The savings can be significant, in the same program from above the 200K TUs
7684 share 8K stmt_list tables.
7685
7686 FUNC is expected to call get_type_unit_group, which will create the
7687 struct type_unit_group if necessary and add it to
7688 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7689
7690 static void
7691 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7692 {
7693 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7694 abbrev_table_up abbrev_table;
7695 sect_offset abbrev_offset;
7696
7697 /* It's up to the caller to not call us multiple times. */
7698 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
7699
7700 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
7701 return;
7702
7703 /* TUs typically share abbrev tables, and there can be way more TUs than
7704 abbrev tables. Sort by abbrev table to reduce the number of times we
7705 read each abbrev table in.
7706 Alternatives are to punt or to maintain a cache of abbrev tables.
7707 This is simpler and efficient enough for now.
7708
7709 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7710 symtab to use). Typically TUs with the same abbrev offset have the same
7711 stmt_list value too so in practice this should work well.
7712
7713 The basic algorithm here is:
7714
7715 sort TUs by abbrev table
7716 for each TU with same abbrev table:
7717 read abbrev table if first user
7718 read TU top level DIE
7719 [IWBN if DWO skeletons had DW_AT_stmt_list]
7720 call FUNC */
7721
7722 if (dwarf_read_debug)
7723 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7724
7725 /* Sort in a separate table to maintain the order of all_type_units
7726 for .gdb_index: TU indices directly index all_type_units. */
7727 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7728 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
7729
7730 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
7731 sorted_by_abbrev.emplace_back
7732 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7733 sig_type->per_cu.section,
7734 sig_type->per_cu.sect_off));
7735
7736 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7737 sort_tu_by_abbrev_offset);
7738
7739 abbrev_offset = (sect_offset) ~(unsigned) 0;
7740
7741 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7742 {
7743 /* Switch to the next abbrev table if necessary. */
7744 if (abbrev_table == NULL
7745 || tu.abbrev_offset != abbrev_offset)
7746 {
7747 abbrev_offset = tu.abbrev_offset;
7748 abbrev_table =
7749 abbrev_table::read (dwarf2_per_objfile->objfile,
7750 &dwarf2_per_objfile->per_bfd->abbrev,
7751 abbrev_offset);
7752 ++tu_stats->nr_uniq_abbrev_tables;
7753 }
7754
7755 cutu_reader reader (&tu.sig_type->per_cu, dwarf2_per_objfile,
7756 abbrev_table.get (), 0, false);
7757 if (!reader.dummy_p)
7758 build_type_psymtabs_reader (&reader, reader.info_ptr,
7759 reader.comp_unit_die);
7760 }
7761 }
7762
7763 /* Print collected type unit statistics. */
7764
7765 static void
7766 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7767 {
7768 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7769
7770 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7771 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7772 dwarf2_per_objfile->per_bfd->all_type_units.size ());
7773 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7774 tu_stats->nr_uniq_abbrev_tables);
7775 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7776 tu_stats->nr_symtabs);
7777 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7778 tu_stats->nr_symtab_sharers);
7779 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7780 tu_stats->nr_stmt_less_type_units);
7781 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7782 tu_stats->nr_all_type_units_reallocs);
7783 }
7784
7785 /* Traversal function for build_type_psymtabs. */
7786
7787 static int
7788 build_type_psymtab_dependencies (void **slot, void *info)
7789 {
7790 struct dwarf2_per_objfile *dwarf2_per_objfile
7791 = (struct dwarf2_per_objfile *) info;
7792 struct objfile *objfile = dwarf2_per_objfile->objfile;
7793 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7794 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7795 dwarf2_psymtab *pst = per_cu->v.psymtab;
7796 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7797 int i;
7798
7799 gdb_assert (len > 0);
7800 gdb_assert (per_cu->type_unit_group_p ());
7801
7802 pst->number_of_dependencies = len;
7803 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7804 for (i = 0; i < len; ++i)
7805 {
7806 struct signatured_type *iter = tu_group->tus->at (i);
7807 gdb_assert (iter->per_cu.is_debug_types);
7808 pst->dependencies[i] = iter->per_cu.v.psymtab;
7809 iter->type_unit_group = tu_group;
7810 }
7811
7812 delete tu_group->tus;
7813 tu_group->tus = nullptr;
7814
7815 return 1;
7816 }
7817
7818 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7819 Build partial symbol tables for the .debug_types comp-units. */
7820
7821 static void
7822 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7823 {
7824 if (! create_all_type_units (dwarf2_per_objfile))
7825 return;
7826
7827 build_type_psymtabs_1 (dwarf2_per_objfile);
7828 }
7829
7830 /* Traversal function for process_skeletonless_type_unit.
7831 Read a TU in a DWO file and build partial symbols for it. */
7832
7833 static int
7834 process_skeletonless_type_unit (void **slot, void *info)
7835 {
7836 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7837 struct dwarf2_per_objfile *dwarf2_per_objfile
7838 = (struct dwarf2_per_objfile *) info;
7839 struct signatured_type find_entry, *entry;
7840
7841 /* If this TU doesn't exist in the global table, add it and read it in. */
7842
7843 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7844 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7845
7846 find_entry.signature = dwo_unit->signature;
7847 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
7848 &find_entry, INSERT);
7849 /* If we've already seen this type there's nothing to do. What's happening
7850 is we're doing our own version of comdat-folding here. */
7851 if (*slot != NULL)
7852 return 1;
7853
7854 /* This does the job that create_all_type_units would have done for
7855 this TU. */
7856 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7857 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7858 *slot = entry;
7859
7860 /* This does the job that build_type_psymtabs_1 would have done. */
7861 cutu_reader reader (&entry->per_cu, dwarf2_per_objfile, NULL, 0, false);
7862 if (!reader.dummy_p)
7863 build_type_psymtabs_reader (&reader, reader.info_ptr,
7864 reader.comp_unit_die);
7865
7866 return 1;
7867 }
7868
7869 /* Traversal function for process_skeletonless_type_units. */
7870
7871 static int
7872 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7873 {
7874 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7875
7876 if (dwo_file->tus != NULL)
7877 htab_traverse_noresize (dwo_file->tus.get (),
7878 process_skeletonless_type_unit, info);
7879
7880 return 1;
7881 }
7882
7883 /* Scan all TUs of DWO files, verifying we've processed them.
7884 This is needed in case a TU was emitted without its skeleton.
7885 Note: This can't be done until we know what all the DWO files are. */
7886
7887 static void
7888 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7889 {
7890 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7891 if (get_dwp_file (dwarf2_per_objfile) == NULL
7892 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
7893 {
7894 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
7895 process_dwo_file_for_skeletonless_type_units,
7896 dwarf2_per_objfile);
7897 }
7898 }
7899
7900 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7901
7902 static void
7903 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7904 {
7905 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7906 {
7907 dwarf2_psymtab *pst = per_cu->v.psymtab;
7908
7909 if (pst == NULL)
7910 continue;
7911
7912 for (int j = 0; j < pst->number_of_dependencies; ++j)
7913 {
7914 /* Set the 'user' field only if it is not already set. */
7915 if (pst->dependencies[j]->user == NULL)
7916 pst->dependencies[j]->user = pst;
7917 }
7918 }
7919 }
7920
7921 /* Build the partial symbol table by doing a quick pass through the
7922 .debug_info and .debug_abbrev sections. */
7923
7924 static void
7925 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7926 {
7927 struct objfile *objfile = dwarf2_per_objfile->objfile;
7928
7929 if (dwarf_read_debug)
7930 {
7931 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7932 objfile_name (objfile));
7933 }
7934
7935 scoped_restore restore_reading_psyms
7936 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
7937 true);
7938
7939 dwarf2_per_objfile->per_bfd->info.read (objfile);
7940
7941 /* Any cached compilation units will be linked by the per-objfile
7942 read_in_chain. Make sure to free them when we're done. */
7943 free_cached_comp_units freer (dwarf2_per_objfile);
7944
7945 build_type_psymtabs (dwarf2_per_objfile);
7946
7947 create_all_comp_units (dwarf2_per_objfile);
7948
7949 /* Create a temporary address map on a temporary obstack. We later
7950 copy this to the final obstack. */
7951 auto_obstack temp_obstack;
7952
7953 scoped_restore save_psymtabs_addrmap
7954 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7955 addrmap_create_mutable (&temp_obstack));
7956
7957 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7958 {
7959 if (per_cu->v.psymtab != NULL)
7960 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7961 continue;
7962 process_psymtab_comp_unit (per_cu, dwarf2_per_objfile, false,
7963 language_minimal);
7964 }
7965
7966 /* This has to wait until we read the CUs, we need the list of DWOs. */
7967 process_skeletonless_type_units (dwarf2_per_objfile);
7968
7969 /* Now that all TUs have been processed we can fill in the dependencies. */
7970 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
7971 {
7972 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7973 build_type_psymtab_dependencies, dwarf2_per_objfile);
7974 }
7975
7976 if (dwarf_read_debug)
7977 print_tu_stats (dwarf2_per_objfile);
7978
7979 set_partial_user (dwarf2_per_objfile);
7980
7981 objfile->partial_symtabs->psymtabs_addrmap
7982 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7983 objfile->partial_symtabs->obstack ());
7984 /* At this point we want to keep the address map. */
7985 save_psymtabs_addrmap.release ();
7986
7987 if (dwarf_read_debug)
7988 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7989 objfile_name (objfile));
7990 }
7991
7992 /* Load the partial DIEs for a secondary CU into memory.
7993 This is also used when rereading a primary CU with load_all_dies. */
7994
7995 static void
7996 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7997 dwarf2_per_objfile *per_objfile)
7998 {
7999 cutu_reader reader (this_cu, per_objfile, NULL, 1, false);
8000
8001 if (!reader.dummy_p)
8002 {
8003 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8004 language_minimal);
8005
8006 /* Check if comp unit has_children.
8007 If so, read the rest of the partial symbols from this comp unit.
8008 If not, there's no more debug_info for this comp unit. */
8009 if (reader.comp_unit_die->has_children)
8010 load_partial_dies (&reader, reader.info_ptr, 0);
8011
8012 reader.keep ();
8013 }
8014 }
8015
8016 static void
8017 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8018 struct dwarf2_section_info *section,
8019 struct dwarf2_section_info *abbrev_section,
8020 unsigned int is_dwz)
8021 {
8022 const gdb_byte *info_ptr;
8023 struct objfile *objfile = dwarf2_per_objfile->objfile;
8024
8025 if (dwarf_read_debug)
8026 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8027 section->get_name (),
8028 section->get_file_name ());
8029
8030 section->read (objfile);
8031
8032 info_ptr = section->buffer;
8033
8034 while (info_ptr < section->buffer + section->size)
8035 {
8036 struct dwarf2_per_cu_data *this_cu;
8037
8038 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8039
8040 comp_unit_head cu_header;
8041 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8042 abbrev_section, info_ptr,
8043 rcuh_kind::COMPILE);
8044
8045 /* Save the compilation unit for later lookup. */
8046 if (cu_header.unit_type != DW_UT_type)
8047 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
8048 else
8049 {
8050 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
8051 sig_type->signature = cu_header.signature;
8052 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8053 this_cu = &sig_type->per_cu;
8054 }
8055 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8056 this_cu->sect_off = sect_off;
8057 this_cu->length = cu_header.length + cu_header.initial_length_size;
8058 this_cu->is_dwz = is_dwz;
8059 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8060 this_cu->section = section;
8061
8062 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8063
8064 info_ptr = info_ptr + this_cu->length;
8065 }
8066 }
8067
8068 /* Create a list of all compilation units in OBJFILE.
8069 This is only done for -readnow and building partial symtabs. */
8070
8071 static void
8072 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8073 {
8074 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8075 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8076 &dwarf2_per_objfile->per_bfd->abbrev, 0);
8077
8078 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
8079 if (dwz != NULL)
8080 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8081 1);
8082 }
8083
8084 /* Process all loaded DIEs for compilation unit CU, starting at
8085 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8086 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8087 DW_AT_ranges). See the comments of add_partial_subprogram on how
8088 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8089
8090 static void
8091 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8092 CORE_ADDR *highpc, int set_addrmap,
8093 struct dwarf2_cu *cu)
8094 {
8095 struct partial_die_info *pdi;
8096
8097 /* Now, march along the PDI's, descending into ones which have
8098 interesting children but skipping the children of the other ones,
8099 until we reach the end of the compilation unit. */
8100
8101 pdi = first_die;
8102
8103 while (pdi != NULL)
8104 {
8105 pdi->fixup (cu);
8106
8107 /* Anonymous namespaces or modules have no name but have interesting
8108 children, so we need to look at them. Ditto for anonymous
8109 enums. */
8110
8111 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8112 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8113 || pdi->tag == DW_TAG_imported_unit
8114 || pdi->tag == DW_TAG_inlined_subroutine)
8115 {
8116 switch (pdi->tag)
8117 {
8118 case DW_TAG_subprogram:
8119 case DW_TAG_inlined_subroutine:
8120 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8121 break;
8122 case DW_TAG_constant:
8123 case DW_TAG_variable:
8124 case DW_TAG_typedef:
8125 case DW_TAG_union_type:
8126 if (!pdi->is_declaration
8127 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8128 {
8129 add_partial_symbol (pdi, cu);
8130 }
8131 break;
8132 case DW_TAG_class_type:
8133 case DW_TAG_interface_type:
8134 case DW_TAG_structure_type:
8135 if (!pdi->is_declaration)
8136 {
8137 add_partial_symbol (pdi, cu);
8138 }
8139 if ((cu->language == language_rust
8140 || cu->language == language_cplus) && pdi->has_children)
8141 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8142 set_addrmap, cu);
8143 break;
8144 case DW_TAG_enumeration_type:
8145 if (!pdi->is_declaration)
8146 add_partial_enumeration (pdi, cu);
8147 break;
8148 case DW_TAG_base_type:
8149 case DW_TAG_subrange_type:
8150 /* File scope base type definitions are added to the partial
8151 symbol table. */
8152 add_partial_symbol (pdi, cu);
8153 break;
8154 case DW_TAG_namespace:
8155 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8156 break;
8157 case DW_TAG_module:
8158 if (!pdi->is_declaration)
8159 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8160 break;
8161 case DW_TAG_imported_unit:
8162 {
8163 struct dwarf2_per_cu_data *per_cu;
8164
8165 /* For now we don't handle imported units in type units. */
8166 if (cu->per_cu->is_debug_types)
8167 {
8168 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8169 " supported in type units [in module %s]"),
8170 objfile_name (cu->per_objfile->objfile));
8171 }
8172
8173 per_cu = dwarf2_find_containing_comp_unit
8174 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8175
8176 /* Go read the partial unit, if needed. */
8177 if (per_cu->v.psymtab == NULL)
8178 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8179 cu->language);
8180
8181 cu->per_cu->imported_symtabs_push (per_cu);
8182 }
8183 break;
8184 case DW_TAG_imported_declaration:
8185 add_partial_symbol (pdi, cu);
8186 break;
8187 default:
8188 break;
8189 }
8190 }
8191
8192 /* If the die has a sibling, skip to the sibling. */
8193
8194 pdi = pdi->die_sibling;
8195 }
8196 }
8197
8198 /* Functions used to compute the fully scoped name of a partial DIE.
8199
8200 Normally, this is simple. For C++, the parent DIE's fully scoped
8201 name is concatenated with "::" and the partial DIE's name.
8202 Enumerators are an exception; they use the scope of their parent
8203 enumeration type, i.e. the name of the enumeration type is not
8204 prepended to the enumerator.
8205
8206 There are two complexities. One is DW_AT_specification; in this
8207 case "parent" means the parent of the target of the specification,
8208 instead of the direct parent of the DIE. The other is compilers
8209 which do not emit DW_TAG_namespace; in this case we try to guess
8210 the fully qualified name of structure types from their members'
8211 linkage names. This must be done using the DIE's children rather
8212 than the children of any DW_AT_specification target. We only need
8213 to do this for structures at the top level, i.e. if the target of
8214 any DW_AT_specification (if any; otherwise the DIE itself) does not
8215 have a parent. */
8216
8217 /* Compute the scope prefix associated with PDI's parent, in
8218 compilation unit CU. The result will be allocated on CU's
8219 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8220 field. NULL is returned if no prefix is necessary. */
8221 static const char *
8222 partial_die_parent_scope (struct partial_die_info *pdi,
8223 struct dwarf2_cu *cu)
8224 {
8225 const char *grandparent_scope;
8226 struct partial_die_info *parent, *real_pdi;
8227
8228 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8229 then this means the parent of the specification DIE. */
8230
8231 real_pdi = pdi;
8232 while (real_pdi->has_specification)
8233 {
8234 auto res = find_partial_die (real_pdi->spec_offset,
8235 real_pdi->spec_is_dwz, cu);
8236 real_pdi = res.pdi;
8237 cu = res.cu;
8238 }
8239
8240 parent = real_pdi->die_parent;
8241 if (parent == NULL)
8242 return NULL;
8243
8244 if (parent->scope_set)
8245 return parent->scope;
8246
8247 parent->fixup (cu);
8248
8249 grandparent_scope = partial_die_parent_scope (parent, cu);
8250
8251 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8252 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8253 Work around this problem here. */
8254 if (cu->language == language_cplus
8255 && parent->tag == DW_TAG_namespace
8256 && strcmp (parent->name, "::") == 0
8257 && grandparent_scope == NULL)
8258 {
8259 parent->scope = NULL;
8260 parent->scope_set = 1;
8261 return NULL;
8262 }
8263
8264 /* Nested subroutines in Fortran get a prefix. */
8265 if (pdi->tag == DW_TAG_enumerator)
8266 /* Enumerators should not get the name of the enumeration as a prefix. */
8267 parent->scope = grandparent_scope;
8268 else if (parent->tag == DW_TAG_namespace
8269 || parent->tag == DW_TAG_module
8270 || parent->tag == DW_TAG_structure_type
8271 || parent->tag == DW_TAG_class_type
8272 || parent->tag == DW_TAG_interface_type
8273 || parent->tag == DW_TAG_union_type
8274 || parent->tag == DW_TAG_enumeration_type
8275 || (cu->language == language_fortran
8276 && parent->tag == DW_TAG_subprogram
8277 && pdi->tag == DW_TAG_subprogram))
8278 {
8279 if (grandparent_scope == NULL)
8280 parent->scope = parent->name;
8281 else
8282 parent->scope = typename_concat (&cu->comp_unit_obstack,
8283 grandparent_scope,
8284 parent->name, 0, cu);
8285 }
8286 else
8287 {
8288 /* FIXME drow/2004-04-01: What should we be doing with
8289 function-local names? For partial symbols, we should probably be
8290 ignoring them. */
8291 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8292 dwarf_tag_name (parent->tag),
8293 sect_offset_str (pdi->sect_off));
8294 parent->scope = grandparent_scope;
8295 }
8296
8297 parent->scope_set = 1;
8298 return parent->scope;
8299 }
8300
8301 /* Return the fully scoped name associated with PDI, from compilation unit
8302 CU. The result will be allocated with malloc. */
8303
8304 static gdb::unique_xmalloc_ptr<char>
8305 partial_die_full_name (struct partial_die_info *pdi,
8306 struct dwarf2_cu *cu)
8307 {
8308 const char *parent_scope;
8309
8310 /* If this is a template instantiation, we can not work out the
8311 template arguments from partial DIEs. So, unfortunately, we have
8312 to go through the full DIEs. At least any work we do building
8313 types here will be reused if full symbols are loaded later. */
8314 if (pdi->has_template_arguments)
8315 {
8316 pdi->fixup (cu);
8317
8318 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8319 {
8320 struct die_info *die;
8321 struct attribute attr;
8322 struct dwarf2_cu *ref_cu = cu;
8323
8324 /* DW_FORM_ref_addr is using section offset. */
8325 attr.name = (enum dwarf_attribute) 0;
8326 attr.form = DW_FORM_ref_addr;
8327 attr.u.unsnd = to_underlying (pdi->sect_off);
8328 die = follow_die_ref (NULL, &attr, &ref_cu);
8329
8330 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8331 }
8332 }
8333
8334 parent_scope = partial_die_parent_scope (pdi, cu);
8335 if (parent_scope == NULL)
8336 return NULL;
8337 else
8338 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8339 pdi->name, 0, cu));
8340 }
8341
8342 static void
8343 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8344 {
8345 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
8346 struct objfile *objfile = dwarf2_per_objfile->objfile;
8347 struct gdbarch *gdbarch = objfile->arch ();
8348 CORE_ADDR addr = 0;
8349 const char *actual_name = NULL;
8350 CORE_ADDR baseaddr;
8351
8352 baseaddr = objfile->text_section_offset ();
8353
8354 gdb::unique_xmalloc_ptr<char> built_actual_name
8355 = partial_die_full_name (pdi, cu);
8356 if (built_actual_name != NULL)
8357 actual_name = built_actual_name.get ();
8358
8359 if (actual_name == NULL)
8360 actual_name = pdi->name;
8361
8362 partial_symbol psymbol;
8363 memset (&psymbol, 0, sizeof (psymbol));
8364 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8365 psymbol.ginfo.section = -1;
8366
8367 /* The code below indicates that the psymbol should be installed by
8368 setting this. */
8369 gdb::optional<psymbol_placement> where;
8370
8371 switch (pdi->tag)
8372 {
8373 case DW_TAG_inlined_subroutine:
8374 case DW_TAG_subprogram:
8375 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8376 - baseaddr);
8377 if (pdi->is_external
8378 || cu->language == language_ada
8379 || (cu->language == language_fortran
8380 && pdi->die_parent != NULL
8381 && pdi->die_parent->tag == DW_TAG_subprogram))
8382 {
8383 /* Normally, only "external" DIEs are part of the global scope.
8384 But in Ada and Fortran, we want to be able to access nested
8385 procedures globally. So all Ada and Fortran subprograms are
8386 stored in the global scope. */
8387 where = psymbol_placement::GLOBAL;
8388 }
8389 else
8390 where = psymbol_placement::STATIC;
8391
8392 psymbol.domain = VAR_DOMAIN;
8393 psymbol.aclass = LOC_BLOCK;
8394 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8395 psymbol.ginfo.value.address = addr;
8396
8397 if (pdi->main_subprogram && actual_name != NULL)
8398 set_objfile_main_name (objfile, actual_name, cu->language);
8399 break;
8400 case DW_TAG_constant:
8401 psymbol.domain = VAR_DOMAIN;
8402 psymbol.aclass = LOC_STATIC;
8403 where = (pdi->is_external
8404 ? psymbol_placement::GLOBAL
8405 : psymbol_placement::STATIC);
8406 break;
8407 case DW_TAG_variable:
8408 if (pdi->d.locdesc)
8409 addr = decode_locdesc (pdi->d.locdesc, cu);
8410
8411 if (pdi->d.locdesc
8412 && addr == 0
8413 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
8414 {
8415 /* A global or static variable may also have been stripped
8416 out by the linker if unused, in which case its address
8417 will be nullified; do not add such variables into partial
8418 symbol table then. */
8419 }
8420 else if (pdi->is_external)
8421 {
8422 /* Global Variable.
8423 Don't enter into the minimal symbol tables as there is
8424 a minimal symbol table entry from the ELF symbols already.
8425 Enter into partial symbol table if it has a location
8426 descriptor or a type.
8427 If the location descriptor is missing, new_symbol will create
8428 a LOC_UNRESOLVED symbol, the address of the variable will then
8429 be determined from the minimal symbol table whenever the variable
8430 is referenced.
8431 The address for the partial symbol table entry is not
8432 used by GDB, but it comes in handy for debugging partial symbol
8433 table building. */
8434
8435 if (pdi->d.locdesc || pdi->has_type)
8436 {
8437 psymbol.domain = VAR_DOMAIN;
8438 psymbol.aclass = LOC_STATIC;
8439 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8440 psymbol.ginfo.value.address = addr;
8441 where = psymbol_placement::GLOBAL;
8442 }
8443 }
8444 else
8445 {
8446 int has_loc = pdi->d.locdesc != NULL;
8447
8448 /* Static Variable. Skip symbols whose value we cannot know (those
8449 without location descriptors or constant values). */
8450 if (!has_loc && !pdi->has_const_value)
8451 return;
8452
8453 psymbol.domain = VAR_DOMAIN;
8454 psymbol.aclass = LOC_STATIC;
8455 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8456 if (has_loc)
8457 psymbol.ginfo.value.address = addr;
8458 where = psymbol_placement::STATIC;
8459 }
8460 break;
8461 case DW_TAG_typedef:
8462 case DW_TAG_base_type:
8463 case DW_TAG_subrange_type:
8464 psymbol.domain = VAR_DOMAIN;
8465 psymbol.aclass = LOC_TYPEDEF;
8466 where = psymbol_placement::STATIC;
8467 break;
8468 case DW_TAG_imported_declaration:
8469 case DW_TAG_namespace:
8470 psymbol.domain = VAR_DOMAIN;
8471 psymbol.aclass = LOC_TYPEDEF;
8472 where = psymbol_placement::GLOBAL;
8473 break;
8474 case DW_TAG_module:
8475 /* With Fortran 77 there might be a "BLOCK DATA" module
8476 available without any name. If so, we skip the module as it
8477 doesn't bring any value. */
8478 if (actual_name != nullptr)
8479 {
8480 psymbol.domain = MODULE_DOMAIN;
8481 psymbol.aclass = LOC_TYPEDEF;
8482 where = psymbol_placement::GLOBAL;
8483 }
8484 break;
8485 case DW_TAG_class_type:
8486 case DW_TAG_interface_type:
8487 case DW_TAG_structure_type:
8488 case DW_TAG_union_type:
8489 case DW_TAG_enumeration_type:
8490 /* Skip external references. The DWARF standard says in the section
8491 about "Structure, Union, and Class Type Entries": "An incomplete
8492 structure, union or class type is represented by a structure,
8493 union or class entry that does not have a byte size attribute
8494 and that has a DW_AT_declaration attribute." */
8495 if (!pdi->has_byte_size && pdi->is_declaration)
8496 return;
8497
8498 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8499 static vs. global. */
8500 psymbol.domain = STRUCT_DOMAIN;
8501 psymbol.aclass = LOC_TYPEDEF;
8502 where = (cu->language == language_cplus
8503 ? psymbol_placement::GLOBAL
8504 : psymbol_placement::STATIC);
8505 break;
8506 case DW_TAG_enumerator:
8507 psymbol.domain = VAR_DOMAIN;
8508 psymbol.aclass = LOC_CONST;
8509 where = (cu->language == language_cplus
8510 ? psymbol_placement::GLOBAL
8511 : psymbol_placement::STATIC);
8512 break;
8513 default:
8514 break;
8515 }
8516
8517 if (where.has_value ())
8518 {
8519 if (built_actual_name != nullptr)
8520 actual_name = objfile->intern (actual_name);
8521 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8522 psymbol.ginfo.set_linkage_name (actual_name);
8523 else
8524 {
8525 psymbol.ginfo.set_demangled_name (actual_name,
8526 &objfile->objfile_obstack);
8527 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8528 }
8529 add_psymbol_to_list (psymbol, *where, objfile);
8530 }
8531 }
8532
8533 /* Read a partial die corresponding to a namespace; also, add a symbol
8534 corresponding to that namespace to the symbol table. NAMESPACE is
8535 the name of the enclosing namespace. */
8536
8537 static void
8538 add_partial_namespace (struct partial_die_info *pdi,
8539 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8540 int set_addrmap, struct dwarf2_cu *cu)
8541 {
8542 /* Add a symbol for the namespace. */
8543
8544 add_partial_symbol (pdi, cu);
8545
8546 /* Now scan partial symbols in that namespace. */
8547
8548 if (pdi->has_children)
8549 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8550 }
8551
8552 /* Read a partial die corresponding to a Fortran module. */
8553
8554 static void
8555 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8556 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8557 {
8558 /* Add a symbol for the namespace. */
8559
8560 add_partial_symbol (pdi, cu);
8561
8562 /* Now scan partial symbols in that module. */
8563
8564 if (pdi->has_children)
8565 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8566 }
8567
8568 /* Read a partial die corresponding to a subprogram or an inlined
8569 subprogram and create a partial symbol for that subprogram.
8570 When the CU language allows it, this routine also defines a partial
8571 symbol for each nested subprogram that this subprogram contains.
8572 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8573 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8574
8575 PDI may also be a lexical block, in which case we simply search
8576 recursively for subprograms defined inside that lexical block.
8577 Again, this is only performed when the CU language allows this
8578 type of definitions. */
8579
8580 static void
8581 add_partial_subprogram (struct partial_die_info *pdi,
8582 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8583 int set_addrmap, struct dwarf2_cu *cu)
8584 {
8585 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8586 {
8587 if (pdi->has_pc_info)
8588 {
8589 if (pdi->lowpc < *lowpc)
8590 *lowpc = pdi->lowpc;
8591 if (pdi->highpc > *highpc)
8592 *highpc = pdi->highpc;
8593 if (set_addrmap)
8594 {
8595 struct objfile *objfile = cu->per_objfile->objfile;
8596 struct gdbarch *gdbarch = objfile->arch ();
8597 CORE_ADDR baseaddr;
8598 CORE_ADDR this_highpc;
8599 CORE_ADDR this_lowpc;
8600
8601 baseaddr = objfile->text_section_offset ();
8602 this_lowpc
8603 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8604 pdi->lowpc + baseaddr)
8605 - baseaddr);
8606 this_highpc
8607 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8608 pdi->highpc + baseaddr)
8609 - baseaddr);
8610 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8611 this_lowpc, this_highpc - 1,
8612 cu->per_cu->v.psymtab);
8613 }
8614 }
8615
8616 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8617 {
8618 if (!pdi->is_declaration)
8619 /* Ignore subprogram DIEs that do not have a name, they are
8620 illegal. Do not emit a complaint at this point, we will
8621 do so when we convert this psymtab into a symtab. */
8622 if (pdi->name)
8623 add_partial_symbol (pdi, cu);
8624 }
8625 }
8626
8627 if (! pdi->has_children)
8628 return;
8629
8630 if (cu->language == language_ada || cu->language == language_fortran)
8631 {
8632 pdi = pdi->die_child;
8633 while (pdi != NULL)
8634 {
8635 pdi->fixup (cu);
8636 if (pdi->tag == DW_TAG_subprogram
8637 || pdi->tag == DW_TAG_inlined_subroutine
8638 || pdi->tag == DW_TAG_lexical_block)
8639 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8640 pdi = pdi->die_sibling;
8641 }
8642 }
8643 }
8644
8645 /* Read a partial die corresponding to an enumeration type. */
8646
8647 static void
8648 add_partial_enumeration (struct partial_die_info *enum_pdi,
8649 struct dwarf2_cu *cu)
8650 {
8651 struct partial_die_info *pdi;
8652
8653 if (enum_pdi->name != NULL)
8654 add_partial_symbol (enum_pdi, cu);
8655
8656 pdi = enum_pdi->die_child;
8657 while (pdi)
8658 {
8659 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8660 complaint (_("malformed enumerator DIE ignored"));
8661 else
8662 add_partial_symbol (pdi, cu);
8663 pdi = pdi->die_sibling;
8664 }
8665 }
8666
8667 /* Return the initial uleb128 in the die at INFO_PTR. */
8668
8669 static unsigned int
8670 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8671 {
8672 unsigned int bytes_read;
8673
8674 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8675 }
8676
8677 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8678 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8679
8680 Return the corresponding abbrev, or NULL if the number is zero (indicating
8681 an empty DIE). In either case *BYTES_READ will be set to the length of
8682 the initial number. */
8683
8684 static struct abbrev_info *
8685 peek_die_abbrev (const die_reader_specs &reader,
8686 const gdb_byte *info_ptr, unsigned int *bytes_read)
8687 {
8688 dwarf2_cu *cu = reader.cu;
8689 bfd *abfd = cu->per_objfile->objfile->obfd;
8690 unsigned int abbrev_number
8691 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8692
8693 if (abbrev_number == 0)
8694 return NULL;
8695
8696 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8697 if (!abbrev)
8698 {
8699 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8700 " at offset %s [in module %s]"),
8701 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8702 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8703 }
8704
8705 return abbrev;
8706 }
8707
8708 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8709 Returns a pointer to the end of a series of DIEs, terminated by an empty
8710 DIE. Any children of the skipped DIEs will also be skipped. */
8711
8712 static const gdb_byte *
8713 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8714 {
8715 while (1)
8716 {
8717 unsigned int bytes_read;
8718 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8719
8720 if (abbrev == NULL)
8721 return info_ptr + bytes_read;
8722 else
8723 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8724 }
8725 }
8726
8727 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8728 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8729 abbrev corresponding to that skipped uleb128 should be passed in
8730 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8731 children. */
8732
8733 static const gdb_byte *
8734 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8735 struct abbrev_info *abbrev)
8736 {
8737 unsigned int bytes_read;
8738 struct attribute attr;
8739 bfd *abfd = reader->abfd;
8740 struct dwarf2_cu *cu = reader->cu;
8741 const gdb_byte *buffer = reader->buffer;
8742 const gdb_byte *buffer_end = reader->buffer_end;
8743 unsigned int form, i;
8744
8745 for (i = 0; i < abbrev->num_attrs; i++)
8746 {
8747 /* The only abbrev we care about is DW_AT_sibling. */
8748 if (abbrev->attrs[i].name == DW_AT_sibling)
8749 {
8750 bool ignored;
8751 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8752 &ignored);
8753 if (attr.form == DW_FORM_ref_addr)
8754 complaint (_("ignoring absolute DW_AT_sibling"));
8755 else
8756 {
8757 sect_offset off = attr.get_ref_die_offset ();
8758 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8759
8760 if (sibling_ptr < info_ptr)
8761 complaint (_("DW_AT_sibling points backwards"));
8762 else if (sibling_ptr > reader->buffer_end)
8763 reader->die_section->overflow_complaint ();
8764 else
8765 return sibling_ptr;
8766 }
8767 }
8768
8769 /* If it isn't DW_AT_sibling, skip this attribute. */
8770 form = abbrev->attrs[i].form;
8771 skip_attribute:
8772 switch (form)
8773 {
8774 case DW_FORM_ref_addr:
8775 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8776 and later it is offset sized. */
8777 if (cu->header.version == 2)
8778 info_ptr += cu->header.addr_size;
8779 else
8780 info_ptr += cu->header.offset_size;
8781 break;
8782 case DW_FORM_GNU_ref_alt:
8783 info_ptr += cu->header.offset_size;
8784 break;
8785 case DW_FORM_addr:
8786 info_ptr += cu->header.addr_size;
8787 break;
8788 case DW_FORM_data1:
8789 case DW_FORM_ref1:
8790 case DW_FORM_flag:
8791 case DW_FORM_strx1:
8792 info_ptr += 1;
8793 break;
8794 case DW_FORM_flag_present:
8795 case DW_FORM_implicit_const:
8796 break;
8797 case DW_FORM_data2:
8798 case DW_FORM_ref2:
8799 case DW_FORM_strx2:
8800 info_ptr += 2;
8801 break;
8802 case DW_FORM_strx3:
8803 info_ptr += 3;
8804 break;
8805 case DW_FORM_data4:
8806 case DW_FORM_ref4:
8807 case DW_FORM_strx4:
8808 info_ptr += 4;
8809 break;
8810 case DW_FORM_data8:
8811 case DW_FORM_ref8:
8812 case DW_FORM_ref_sig8:
8813 info_ptr += 8;
8814 break;
8815 case DW_FORM_data16:
8816 info_ptr += 16;
8817 break;
8818 case DW_FORM_string:
8819 read_direct_string (abfd, info_ptr, &bytes_read);
8820 info_ptr += bytes_read;
8821 break;
8822 case DW_FORM_sec_offset:
8823 case DW_FORM_strp:
8824 case DW_FORM_GNU_strp_alt:
8825 info_ptr += cu->header.offset_size;
8826 break;
8827 case DW_FORM_exprloc:
8828 case DW_FORM_block:
8829 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8830 info_ptr += bytes_read;
8831 break;
8832 case DW_FORM_block1:
8833 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8834 break;
8835 case DW_FORM_block2:
8836 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8837 break;
8838 case DW_FORM_block4:
8839 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8840 break;
8841 case DW_FORM_addrx:
8842 case DW_FORM_strx:
8843 case DW_FORM_sdata:
8844 case DW_FORM_udata:
8845 case DW_FORM_ref_udata:
8846 case DW_FORM_GNU_addr_index:
8847 case DW_FORM_GNU_str_index:
8848 case DW_FORM_rnglistx:
8849 case DW_FORM_loclistx:
8850 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8851 break;
8852 case DW_FORM_indirect:
8853 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8854 info_ptr += bytes_read;
8855 /* We need to continue parsing from here, so just go back to
8856 the top. */
8857 goto skip_attribute;
8858
8859 default:
8860 error (_("Dwarf Error: Cannot handle %s "
8861 "in DWARF reader [in module %s]"),
8862 dwarf_form_name (form),
8863 bfd_get_filename (abfd));
8864 }
8865 }
8866
8867 if (abbrev->has_children)
8868 return skip_children (reader, info_ptr);
8869 else
8870 return info_ptr;
8871 }
8872
8873 /* Locate ORIG_PDI's sibling.
8874 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8875
8876 static const gdb_byte *
8877 locate_pdi_sibling (const struct die_reader_specs *reader,
8878 struct partial_die_info *orig_pdi,
8879 const gdb_byte *info_ptr)
8880 {
8881 /* Do we know the sibling already? */
8882
8883 if (orig_pdi->sibling)
8884 return orig_pdi->sibling;
8885
8886 /* Are there any children to deal with? */
8887
8888 if (!orig_pdi->has_children)
8889 return info_ptr;
8890
8891 /* Skip the children the long way. */
8892
8893 return skip_children (reader, info_ptr);
8894 }
8895
8896 /* Expand this partial symbol table into a full symbol table. SELF is
8897 not NULL. */
8898
8899 void
8900 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8901 {
8902 struct dwarf2_per_objfile *dwarf2_per_objfile
8903 = get_dwarf2_per_objfile (objfile);
8904
8905 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8906
8907 /* If this psymtab is constructed from a debug-only objfile, the
8908 has_section_at_zero flag will not necessarily be correct. We
8909 can get the correct value for this flag by looking at the data
8910 associated with the (presumably stripped) associated objfile. */
8911 if (objfile->separate_debug_objfile_backlink)
8912 {
8913 struct dwarf2_per_objfile *dpo_backlink
8914 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8915
8916 dwarf2_per_objfile->per_bfd->has_section_at_zero
8917 = dpo_backlink->per_bfd->has_section_at_zero;
8918 }
8919
8920 expand_psymtab (objfile);
8921
8922 process_cu_includes (dwarf2_per_objfile);
8923 }
8924 \f
8925 /* Reading in full CUs. */
8926
8927 /* Add PER_CU to the queue. */
8928
8929 static void
8930 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8931 enum language pretend_language)
8932 {
8933 per_cu->queued = 1;
8934 per_cu->per_bfd->queue.emplace (per_cu, pretend_language);
8935 }
8936
8937 /* If PER_CU is not yet queued, add it to the queue.
8938 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8939 dependency.
8940 The result is non-zero if PER_CU was queued, otherwise the result is zero
8941 meaning either PER_CU is already queued or it is already loaded.
8942
8943 N.B. There is an invariant here that if a CU is queued then it is loaded.
8944 The caller is required to load PER_CU if we return non-zero. */
8945
8946 static int
8947 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8948 struct dwarf2_per_cu_data *per_cu,
8949 enum language pretend_language)
8950 {
8951 /* We may arrive here during partial symbol reading, if we need full
8952 DIEs to process an unusual case (e.g. template arguments). Do
8953 not queue PER_CU, just tell our caller to load its DIEs. */
8954 if (per_cu->per_bfd->reading_partial_symbols)
8955 {
8956 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8957 return 1;
8958 return 0;
8959 }
8960
8961 /* Mark the dependence relation so that we don't flush PER_CU
8962 too early. */
8963 if (dependent_cu != NULL)
8964 dwarf2_add_dependence (dependent_cu, per_cu);
8965
8966 /* If it's already on the queue, we have nothing to do. */
8967 if (per_cu->queued)
8968 return 0;
8969
8970 /* If the compilation unit is already loaded, just mark it as
8971 used. */
8972 if (per_cu->cu != NULL)
8973 {
8974 per_cu->cu->last_used = 0;
8975 return 0;
8976 }
8977
8978 /* Add it to the queue. */
8979 queue_comp_unit (per_cu, pretend_language);
8980
8981 return 1;
8982 }
8983
8984 /* Process the queue. */
8985
8986 static void
8987 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8988 {
8989 if (dwarf_read_debug)
8990 {
8991 fprintf_unfiltered (gdb_stdlog,
8992 "Expanding one or more symtabs of objfile %s ...\n",
8993 objfile_name (dwarf2_per_objfile->objfile));
8994 }
8995
8996 /* The queue starts out with one item, but following a DIE reference
8997 may load a new CU, adding it to the end of the queue. */
8998 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
8999 {
9000 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
9001
9002 if (!dwarf2_per_objfile->symtab_set_p (item.per_cu)
9003 /* Skip dummy CUs. */
9004 && item.per_cu->cu != NULL)
9005 {
9006 struct dwarf2_per_cu_data *per_cu = item.per_cu;
9007 unsigned int debug_print_threshold;
9008 char buf[100];
9009
9010 if (per_cu->is_debug_types)
9011 {
9012 struct signatured_type *sig_type =
9013 (struct signatured_type *) per_cu;
9014
9015 sprintf (buf, "TU %s at offset %s",
9016 hex_string (sig_type->signature),
9017 sect_offset_str (per_cu->sect_off));
9018 /* There can be 100s of TUs.
9019 Only print them in verbose mode. */
9020 debug_print_threshold = 2;
9021 }
9022 else
9023 {
9024 sprintf (buf, "CU at offset %s",
9025 sect_offset_str (per_cu->sect_off));
9026 debug_print_threshold = 1;
9027 }
9028
9029 if (dwarf_read_debug >= debug_print_threshold)
9030 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9031
9032 if (per_cu->is_debug_types)
9033 process_full_type_unit (per_cu, dwarf2_per_objfile,
9034 item.pretend_language);
9035 else
9036 process_full_comp_unit (per_cu, dwarf2_per_objfile,
9037 item.pretend_language);
9038
9039 if (dwarf_read_debug >= debug_print_threshold)
9040 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9041 }
9042
9043 item.per_cu->queued = 0;
9044 dwarf2_per_objfile->per_bfd->queue.pop ();
9045 }
9046
9047 if (dwarf_read_debug)
9048 {
9049 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9050 objfile_name (dwarf2_per_objfile->objfile));
9051 }
9052 }
9053
9054 /* Read in full symbols for PST, and anything it depends on. */
9055
9056 void
9057 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9058 {
9059 gdb_assert (!readin_p (objfile));
9060
9061 expand_dependencies (objfile);
9062
9063 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9064 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9065 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9066 }
9067
9068 /* See psympriv.h. */
9069
9070 bool
9071 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9072 {
9073 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9074 return per_objfile->symtab_set_p (per_cu_data);
9075 }
9076
9077 /* See psympriv.h. */
9078
9079 compunit_symtab *
9080 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9081 {
9082 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9083 return per_objfile->get_symtab (per_cu_data);
9084 }
9085
9086 /* Trivial hash function for die_info: the hash value of a DIE
9087 is its offset in .debug_info for this objfile. */
9088
9089 static hashval_t
9090 die_hash (const void *item)
9091 {
9092 const struct die_info *die = (const struct die_info *) item;
9093
9094 return to_underlying (die->sect_off);
9095 }
9096
9097 /* Trivial comparison function for die_info structures: two DIEs
9098 are equal if they have the same offset. */
9099
9100 static int
9101 die_eq (const void *item_lhs, const void *item_rhs)
9102 {
9103 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9104 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9105
9106 return die_lhs->sect_off == die_rhs->sect_off;
9107 }
9108
9109 /* Load the DIEs associated with PER_CU into memory. */
9110
9111 static void
9112 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9113 dwarf2_per_objfile *per_objfile,
9114 bool skip_partial,
9115 enum language pretend_language)
9116 {
9117 gdb_assert (! this_cu->is_debug_types);
9118
9119 cutu_reader reader (this_cu, per_objfile, NULL, 1, skip_partial);
9120 if (reader.dummy_p)
9121 return;
9122
9123 struct dwarf2_cu *cu = reader.cu;
9124 const gdb_byte *info_ptr = reader.info_ptr;
9125
9126 gdb_assert (cu->die_hash == NULL);
9127 cu->die_hash =
9128 htab_create_alloc_ex (cu->header.length / 12,
9129 die_hash,
9130 die_eq,
9131 NULL,
9132 &cu->comp_unit_obstack,
9133 hashtab_obstack_allocate,
9134 dummy_obstack_deallocate);
9135
9136 if (reader.comp_unit_die->has_children)
9137 reader.comp_unit_die->child
9138 = read_die_and_siblings (&reader, reader.info_ptr,
9139 &info_ptr, reader.comp_unit_die);
9140 cu->dies = reader.comp_unit_die;
9141 /* comp_unit_die is not stored in die_hash, no need. */
9142
9143 /* We try not to read any attributes in this function, because not
9144 all CUs needed for references have been loaded yet, and symbol
9145 table processing isn't initialized. But we have to set the CU language,
9146 or we won't be able to build types correctly.
9147 Similarly, if we do not read the producer, we can not apply
9148 producer-specific interpretation. */
9149 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9150
9151 reader.keep ();
9152 }
9153
9154 /* Add a DIE to the delayed physname list. */
9155
9156 static void
9157 add_to_method_list (struct type *type, int fnfield_index, int index,
9158 const char *name, struct die_info *die,
9159 struct dwarf2_cu *cu)
9160 {
9161 struct delayed_method_info mi;
9162 mi.type = type;
9163 mi.fnfield_index = fnfield_index;
9164 mi.index = index;
9165 mi.name = name;
9166 mi.die = die;
9167 cu->method_list.push_back (mi);
9168 }
9169
9170 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9171 "const" / "volatile". If so, decrements LEN by the length of the
9172 modifier and return true. Otherwise return false. */
9173
9174 template<size_t N>
9175 static bool
9176 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9177 {
9178 size_t mod_len = sizeof (mod) - 1;
9179 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9180 {
9181 len -= mod_len;
9182 return true;
9183 }
9184 return false;
9185 }
9186
9187 /* Compute the physnames of any methods on the CU's method list.
9188
9189 The computation of method physnames is delayed in order to avoid the
9190 (bad) condition that one of the method's formal parameters is of an as yet
9191 incomplete type. */
9192
9193 static void
9194 compute_delayed_physnames (struct dwarf2_cu *cu)
9195 {
9196 /* Only C++ delays computing physnames. */
9197 if (cu->method_list.empty ())
9198 return;
9199 gdb_assert (cu->language == language_cplus);
9200
9201 for (const delayed_method_info &mi : cu->method_list)
9202 {
9203 const char *physname;
9204 struct fn_fieldlist *fn_flp
9205 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9206 physname = dwarf2_physname (mi.name, mi.die, cu);
9207 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9208 = physname ? physname : "";
9209
9210 /* Since there's no tag to indicate whether a method is a
9211 const/volatile overload, extract that information out of the
9212 demangled name. */
9213 if (physname != NULL)
9214 {
9215 size_t len = strlen (physname);
9216
9217 while (1)
9218 {
9219 if (physname[len] == ')') /* shortcut */
9220 break;
9221 else if (check_modifier (physname, len, " const"))
9222 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9223 else if (check_modifier (physname, len, " volatile"))
9224 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9225 else
9226 break;
9227 }
9228 }
9229 }
9230
9231 /* The list is no longer needed. */
9232 cu->method_list.clear ();
9233 }
9234
9235 /* Go objects should be embedded in a DW_TAG_module DIE,
9236 and it's not clear if/how imported objects will appear.
9237 To keep Go support simple until that's worked out,
9238 go back through what we've read and create something usable.
9239 We could do this while processing each DIE, and feels kinda cleaner,
9240 but that way is more invasive.
9241 This is to, for example, allow the user to type "p var" or "b main"
9242 without having to specify the package name, and allow lookups
9243 of module.object to work in contexts that use the expression
9244 parser. */
9245
9246 static void
9247 fixup_go_packaging (struct dwarf2_cu *cu)
9248 {
9249 gdb::unique_xmalloc_ptr<char> package_name;
9250 struct pending *list;
9251 int i;
9252
9253 for (list = *cu->get_builder ()->get_global_symbols ();
9254 list != NULL;
9255 list = list->next)
9256 {
9257 for (i = 0; i < list->nsyms; ++i)
9258 {
9259 struct symbol *sym = list->symbol[i];
9260
9261 if (sym->language () == language_go
9262 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9263 {
9264 gdb::unique_xmalloc_ptr<char> this_package_name
9265 (go_symbol_package_name (sym));
9266
9267 if (this_package_name == NULL)
9268 continue;
9269 if (package_name == NULL)
9270 package_name = std::move (this_package_name);
9271 else
9272 {
9273 struct objfile *objfile = cu->per_objfile->objfile;
9274 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9275 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9276 (symbol_symtab (sym) != NULL
9277 ? symtab_to_filename_for_display
9278 (symbol_symtab (sym))
9279 : objfile_name (objfile)),
9280 this_package_name.get (), package_name.get ());
9281 }
9282 }
9283 }
9284 }
9285
9286 if (package_name != NULL)
9287 {
9288 struct objfile *objfile = cu->per_objfile->objfile;
9289 const char *saved_package_name = objfile->intern (package_name.get ());
9290 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9291 saved_package_name);
9292 struct symbol *sym;
9293
9294 sym = new (&objfile->objfile_obstack) symbol;
9295 sym->set_language (language_go, &objfile->objfile_obstack);
9296 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9297 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9298 e.g., "main" finds the "main" module and not C's main(). */
9299 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9300 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9301 SYMBOL_TYPE (sym) = type;
9302
9303 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9304 }
9305 }
9306
9307 /* Allocate a fully-qualified name consisting of the two parts on the
9308 obstack. */
9309
9310 static const char *
9311 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9312 {
9313 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9314 }
9315
9316 /* A helper that allocates a variant part to attach to a Rust enum
9317 type. OBSTACK is where the results should be allocated. TYPE is
9318 the type we're processing. DISCRIMINANT_INDEX is the index of the
9319 discriminant. It must be the index of one of the fields of TYPE.
9320 DEFAULT_INDEX is the index of the default field; or -1 if there is
9321 no default. RANGES is indexed by "effective" field number (the
9322 field index, but omitting the discriminant and default fields) and
9323 must hold the discriminant values used by the variants. Note that
9324 RANGES must have a lifetime at least as long as OBSTACK -- either
9325 already allocated on it, or static. */
9326
9327 static void
9328 alloc_rust_variant (struct obstack *obstack, struct type *type,
9329 int discriminant_index, int default_index,
9330 gdb::array_view<discriminant_range> ranges)
9331 {
9332 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9333 must be handled by the caller. */
9334 gdb_assert (discriminant_index >= 0
9335 && discriminant_index < type->num_fields ());
9336 gdb_assert (default_index == -1
9337 || (default_index >= 0 && default_index < type->num_fields ()));
9338
9339 /* We have one variant for each non-discriminant field. */
9340 int n_variants = type->num_fields () - 1;
9341
9342 variant *variants = new (obstack) variant[n_variants];
9343 int var_idx = 0;
9344 int range_idx = 0;
9345 for (int i = 0; i < type->num_fields (); ++i)
9346 {
9347 if (i == discriminant_index)
9348 continue;
9349
9350 variants[var_idx].first_field = i;
9351 variants[var_idx].last_field = i + 1;
9352
9353 /* The default field does not need a range, but other fields do.
9354 We skipped the discriminant above. */
9355 if (i != default_index)
9356 {
9357 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9358 ++range_idx;
9359 }
9360
9361 ++var_idx;
9362 }
9363
9364 gdb_assert (range_idx == ranges.size ());
9365 gdb_assert (var_idx == n_variants);
9366
9367 variant_part *part = new (obstack) variant_part;
9368 part->discriminant_index = discriminant_index;
9369 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9370 discriminant_index));
9371 part->variants = gdb::array_view<variant> (variants, n_variants);
9372
9373 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9374 gdb::array_view<variant_part> *prop_value
9375 = new (storage) gdb::array_view<variant_part> (part, 1);
9376
9377 struct dynamic_prop prop;
9378 prop.kind = PROP_VARIANT_PARTS;
9379 prop.data.variant_parts = prop_value;
9380
9381 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9382 }
9383
9384 /* Some versions of rustc emitted enums in an unusual way.
9385
9386 Ordinary enums were emitted as unions. The first element of each
9387 structure in the union was named "RUST$ENUM$DISR". This element
9388 held the discriminant.
9389
9390 These versions of Rust also implemented the "non-zero"
9391 optimization. When the enum had two values, and one is empty and
9392 the other holds a pointer that cannot be zero, the pointer is used
9393 as the discriminant, with a zero value meaning the empty variant.
9394 Here, the union's first member is of the form
9395 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9396 where the fieldnos are the indices of the fields that should be
9397 traversed in order to find the field (which may be several fields deep)
9398 and the variantname is the name of the variant of the case when the
9399 field is zero.
9400
9401 This function recognizes whether TYPE is of one of these forms,
9402 and, if so, smashes it to be a variant type. */
9403
9404 static void
9405 quirk_rust_enum (struct type *type, struct objfile *objfile)
9406 {
9407 gdb_assert (type->code () == TYPE_CODE_UNION);
9408
9409 /* We don't need to deal with empty enums. */
9410 if (type->num_fields () == 0)
9411 return;
9412
9413 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9414 if (type->num_fields () == 1
9415 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9416 {
9417 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9418
9419 /* Decode the field name to find the offset of the
9420 discriminant. */
9421 ULONGEST bit_offset = 0;
9422 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9423 while (name[0] >= '0' && name[0] <= '9')
9424 {
9425 char *tail;
9426 unsigned long index = strtoul (name, &tail, 10);
9427 name = tail;
9428 if (*name != '$'
9429 || index >= field_type->num_fields ()
9430 || (TYPE_FIELD_LOC_KIND (field_type, index)
9431 != FIELD_LOC_KIND_BITPOS))
9432 {
9433 complaint (_("Could not parse Rust enum encoding string \"%s\""
9434 "[in module %s]"),
9435 TYPE_FIELD_NAME (type, 0),
9436 objfile_name (objfile));
9437 return;
9438 }
9439 ++name;
9440
9441 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9442 field_type = TYPE_FIELD_TYPE (field_type, index);
9443 }
9444
9445 /* Smash this type to be a structure type. We have to do this
9446 because the type has already been recorded. */
9447 type->set_code (TYPE_CODE_STRUCT);
9448 type->set_num_fields (3);
9449 /* Save the field we care about. */
9450 struct field saved_field = type->field (0);
9451 type->set_fields
9452 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9453
9454 /* Put the discriminant at index 0. */
9455 TYPE_FIELD_TYPE (type, 0) = field_type;
9456 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9457 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9458 SET_FIELD_BITPOS (type->field (0), bit_offset);
9459
9460 /* The order of fields doesn't really matter, so put the real
9461 field at index 1 and the data-less field at index 2. */
9462 type->field (1) = saved_field;
9463 TYPE_FIELD_NAME (type, 1)
9464 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
9465 TYPE_FIELD_TYPE (type, 1)->set_name
9466 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9467 TYPE_FIELD_NAME (type, 1)));
9468
9469 const char *dataless_name
9470 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9471 name);
9472 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9473 dataless_name);
9474 TYPE_FIELD_TYPE (type, 2) = dataless_type;
9475 /* NAME points into the original discriminant name, which
9476 already has the correct lifetime. */
9477 TYPE_FIELD_NAME (type, 2) = name;
9478 SET_FIELD_BITPOS (type->field (2), 0);
9479
9480 /* Indicate that this is a variant type. */
9481 static discriminant_range ranges[1] = { { 0, 0 } };
9482 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9483 }
9484 /* A union with a single anonymous field is probably an old-style
9485 univariant enum. */
9486 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9487 {
9488 /* Smash this type to be a structure type. We have to do this
9489 because the type has already been recorded. */
9490 type->set_code (TYPE_CODE_STRUCT);
9491
9492 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9493 const char *variant_name
9494 = rust_last_path_segment (field_type->name ());
9495 TYPE_FIELD_NAME (type, 0) = variant_name;
9496 field_type->set_name
9497 (rust_fully_qualify (&objfile->objfile_obstack,
9498 type->name (), variant_name));
9499 }
9500 else
9501 {
9502 struct type *disr_type = nullptr;
9503 for (int i = 0; i < type->num_fields (); ++i)
9504 {
9505 disr_type = TYPE_FIELD_TYPE (type, i);
9506
9507 if (disr_type->code () != TYPE_CODE_STRUCT)
9508 {
9509 /* All fields of a true enum will be structs. */
9510 return;
9511 }
9512 else if (disr_type->num_fields () == 0)
9513 {
9514 /* Could be data-less variant, so keep going. */
9515 disr_type = nullptr;
9516 }
9517 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9518 "RUST$ENUM$DISR") != 0)
9519 {
9520 /* Not a Rust enum. */
9521 return;
9522 }
9523 else
9524 {
9525 /* Found one. */
9526 break;
9527 }
9528 }
9529
9530 /* If we got here without a discriminant, then it's probably
9531 just a union. */
9532 if (disr_type == nullptr)
9533 return;
9534
9535 /* Smash this type to be a structure type. We have to do this
9536 because the type has already been recorded. */
9537 type->set_code (TYPE_CODE_STRUCT);
9538
9539 /* Make space for the discriminant field. */
9540 struct field *disr_field = &disr_type->field (0);
9541 field *new_fields
9542 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9543 * sizeof (struct field)));
9544 memcpy (new_fields + 1, type->fields (),
9545 type->num_fields () * sizeof (struct field));
9546 type->set_fields (new_fields);
9547 type->set_num_fields (type->num_fields () + 1);
9548
9549 /* Install the discriminant at index 0 in the union. */
9550 type->field (0) = *disr_field;
9551 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9552 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9553
9554 /* We need a way to find the correct discriminant given a
9555 variant name. For convenience we build a map here. */
9556 struct type *enum_type = FIELD_TYPE (*disr_field);
9557 std::unordered_map<std::string, ULONGEST> discriminant_map;
9558 for (int i = 0; i < enum_type->num_fields (); ++i)
9559 {
9560 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9561 {
9562 const char *name
9563 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9564 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9565 }
9566 }
9567
9568 int n_fields = type->num_fields ();
9569 /* We don't need a range entry for the discriminant, but we do
9570 need one for every other field, as there is no default
9571 variant. */
9572 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9573 discriminant_range,
9574 n_fields - 1);
9575 /* Skip the discriminant here. */
9576 for (int i = 1; i < n_fields; ++i)
9577 {
9578 /* Find the final word in the name of this variant's type.
9579 That name can be used to look up the correct
9580 discriminant. */
9581 const char *variant_name
9582 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
9583
9584 auto iter = discriminant_map.find (variant_name);
9585 if (iter != discriminant_map.end ())
9586 {
9587 ranges[i].low = iter->second;
9588 ranges[i].high = iter->second;
9589 }
9590
9591 /* Remove the discriminant field, if it exists. */
9592 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
9593 if (sub_type->num_fields () > 0)
9594 {
9595 sub_type->set_num_fields (sub_type->num_fields () - 1);
9596 sub_type->set_fields (sub_type->fields () + 1);
9597 }
9598 TYPE_FIELD_NAME (type, i) = variant_name;
9599 sub_type->set_name
9600 (rust_fully_qualify (&objfile->objfile_obstack,
9601 type->name (), variant_name));
9602 }
9603
9604 /* Indicate that this is a variant type. */
9605 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9606 gdb::array_view<discriminant_range> (ranges,
9607 n_fields - 1));
9608 }
9609 }
9610
9611 /* Rewrite some Rust unions to be structures with variants parts. */
9612
9613 static void
9614 rust_union_quirks (struct dwarf2_cu *cu)
9615 {
9616 gdb_assert (cu->language == language_rust);
9617 for (type *type_ : cu->rust_unions)
9618 quirk_rust_enum (type_, cu->per_objfile->objfile);
9619 /* We don't need this any more. */
9620 cu->rust_unions.clear ();
9621 }
9622
9623 /* A helper function for computing the list of all symbol tables
9624 included by PER_CU. */
9625
9626 static void
9627 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9628 htab_t all_children, htab_t all_type_symtabs,
9629 dwarf2_per_cu_data *per_cu,
9630 dwarf2_per_objfile *per_objfile,
9631 struct compunit_symtab *immediate_parent)
9632 {
9633 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9634 if (*slot != NULL)
9635 {
9636 /* This inclusion and its children have been processed. */
9637 return;
9638 }
9639
9640 *slot = per_cu;
9641
9642 /* Only add a CU if it has a symbol table. */
9643 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9644 if (cust != NULL)
9645 {
9646 /* If this is a type unit only add its symbol table if we haven't
9647 seen it yet (type unit per_cu's can share symtabs). */
9648 if (per_cu->is_debug_types)
9649 {
9650 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9651 if (*slot == NULL)
9652 {
9653 *slot = cust;
9654 result->push_back (cust);
9655 if (cust->user == NULL)
9656 cust->user = immediate_parent;
9657 }
9658 }
9659 else
9660 {
9661 result->push_back (cust);
9662 if (cust->user == NULL)
9663 cust->user = immediate_parent;
9664 }
9665 }
9666
9667 if (!per_cu->imported_symtabs_empty ())
9668 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9669 {
9670 recursively_compute_inclusions (result, all_children,
9671 all_type_symtabs, ptr, per_objfile,
9672 cust);
9673 }
9674 }
9675
9676 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9677 PER_CU. */
9678
9679 static void
9680 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9681 dwarf2_per_objfile *per_objfile)
9682 {
9683 gdb_assert (! per_cu->is_debug_types);
9684
9685 if (!per_cu->imported_symtabs_empty ())
9686 {
9687 int len;
9688 std::vector<compunit_symtab *> result_symtabs;
9689 htab_t all_children, all_type_symtabs;
9690 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9691
9692 /* If we don't have a symtab, we can just skip this case. */
9693 if (cust == NULL)
9694 return;
9695
9696 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9697 NULL, xcalloc, xfree);
9698 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9699 NULL, xcalloc, xfree);
9700
9701 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9702 {
9703 recursively_compute_inclusions (&result_symtabs, all_children,
9704 all_type_symtabs, ptr, per_objfile,
9705 cust);
9706 }
9707
9708 /* Now we have a transitive closure of all the included symtabs. */
9709 len = result_symtabs.size ();
9710 cust->includes
9711 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9712 struct compunit_symtab *, len + 1);
9713 memcpy (cust->includes, result_symtabs.data (),
9714 len * sizeof (compunit_symtab *));
9715 cust->includes[len] = NULL;
9716
9717 htab_delete (all_children);
9718 htab_delete (all_type_symtabs);
9719 }
9720 }
9721
9722 /* Compute the 'includes' field for the symtabs of all the CUs we just
9723 read. */
9724
9725 static void
9726 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9727 {
9728 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
9729 {
9730 if (! iter->is_debug_types)
9731 compute_compunit_symtab_includes (iter, dwarf2_per_objfile);
9732 }
9733
9734 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
9735 }
9736
9737 /* Generate full symbol information for PER_CU, whose DIEs have
9738 already been loaded into memory. */
9739
9740 static void
9741 process_full_comp_unit (dwarf2_per_cu_data *per_cu,
9742 dwarf2_per_objfile *dwarf2_per_objfile,
9743 enum language pretend_language)
9744 {
9745 struct dwarf2_cu *cu = per_cu->cu;
9746 struct objfile *objfile = dwarf2_per_objfile->objfile;
9747 struct gdbarch *gdbarch = objfile->arch ();
9748 CORE_ADDR lowpc, highpc;
9749 struct compunit_symtab *cust;
9750 CORE_ADDR baseaddr;
9751 struct block *static_block;
9752 CORE_ADDR addr;
9753
9754 baseaddr = objfile->text_section_offset ();
9755
9756 /* Clear the list here in case something was left over. */
9757 cu->method_list.clear ();
9758
9759 cu->language = pretend_language;
9760 cu->language_defn = language_def (cu->language);
9761
9762 /* Do line number decoding in read_file_scope () */
9763 process_die (cu->dies, cu);
9764
9765 /* For now fudge the Go package. */
9766 if (cu->language == language_go)
9767 fixup_go_packaging (cu);
9768
9769 /* Now that we have processed all the DIEs in the CU, all the types
9770 should be complete, and it should now be safe to compute all of the
9771 physnames. */
9772 compute_delayed_physnames (cu);
9773
9774 if (cu->language == language_rust)
9775 rust_union_quirks (cu);
9776
9777 /* Some compilers don't define a DW_AT_high_pc attribute for the
9778 compilation unit. If the DW_AT_high_pc is missing, synthesize
9779 it, by scanning the DIE's below the compilation unit. */
9780 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9781
9782 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9783 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9784
9785 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9786 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9787 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9788 addrmap to help ensure it has an accurate map of pc values belonging to
9789 this comp unit. */
9790 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9791
9792 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9793 SECT_OFF_TEXT (objfile),
9794 0);
9795
9796 if (cust != NULL)
9797 {
9798 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9799
9800 /* Set symtab language to language from DW_AT_language. If the
9801 compilation is from a C file generated by language preprocessors, do
9802 not set the language if it was already deduced by start_subfile. */
9803 if (!(cu->language == language_c
9804 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9805 COMPUNIT_FILETABS (cust)->language = cu->language;
9806
9807 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9808 produce DW_AT_location with location lists but it can be possibly
9809 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9810 there were bugs in prologue debug info, fixed later in GCC-4.5
9811 by "unwind info for epilogues" patch (which is not directly related).
9812
9813 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9814 needed, it would be wrong due to missing DW_AT_producer there.
9815
9816 Still one can confuse GDB by using non-standard GCC compilation
9817 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9818 */
9819 if (cu->has_loclist && gcc_4_minor >= 5)
9820 cust->locations_valid = 1;
9821
9822 if (gcc_4_minor >= 5)
9823 cust->epilogue_unwind_valid = 1;
9824
9825 cust->call_site_htab = cu->call_site_htab;
9826 }
9827
9828 dwarf2_per_objfile->set_symtab (per_cu, cust);
9829
9830 /* Push it for inclusion processing later. */
9831 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (per_cu);
9832
9833 /* Not needed any more. */
9834 cu->reset_builder ();
9835 }
9836
9837 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9838 already been loaded into memory. */
9839
9840 static void
9841 process_full_type_unit (dwarf2_per_cu_data *per_cu,
9842 dwarf2_per_objfile *dwarf2_per_objfile,
9843 enum language pretend_language)
9844 {
9845 struct dwarf2_cu *cu = per_cu->cu;
9846 struct objfile *objfile = dwarf2_per_objfile->objfile;
9847 struct compunit_symtab *cust;
9848 struct signatured_type *sig_type;
9849
9850 gdb_assert (per_cu->is_debug_types);
9851 sig_type = (struct signatured_type *) per_cu;
9852
9853 /* Clear the list here in case something was left over. */
9854 cu->method_list.clear ();
9855
9856 cu->language = pretend_language;
9857 cu->language_defn = language_def (cu->language);
9858
9859 /* The symbol tables are set up in read_type_unit_scope. */
9860 process_die (cu->dies, cu);
9861
9862 /* For now fudge the Go package. */
9863 if (cu->language == language_go)
9864 fixup_go_packaging (cu);
9865
9866 /* Now that we have processed all the DIEs in the CU, all the types
9867 should be complete, and it should now be safe to compute all of the
9868 physnames. */
9869 compute_delayed_physnames (cu);
9870
9871 if (cu->language == language_rust)
9872 rust_union_quirks (cu);
9873
9874 /* TUs share symbol tables.
9875 If this is the first TU to use this symtab, complete the construction
9876 of it with end_expandable_symtab. Otherwise, complete the addition of
9877 this TU's symbols to the existing symtab. */
9878 if (sig_type->type_unit_group->compunit_symtab == NULL)
9879 {
9880 buildsym_compunit *builder = cu->get_builder ();
9881 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9882 sig_type->type_unit_group->compunit_symtab = cust;
9883
9884 if (cust != NULL)
9885 {
9886 /* Set symtab language to language from DW_AT_language. If the
9887 compilation is from a C file generated by language preprocessors,
9888 do not set the language if it was already deduced by
9889 start_subfile. */
9890 if (!(cu->language == language_c
9891 && COMPUNIT_FILETABS (cust)->language != language_c))
9892 COMPUNIT_FILETABS (cust)->language = cu->language;
9893 }
9894 }
9895 else
9896 {
9897 cu->get_builder ()->augment_type_symtab ();
9898 cust = sig_type->type_unit_group->compunit_symtab;
9899 }
9900
9901 dwarf2_per_objfile->set_symtab (per_cu, cust);
9902
9903 /* Not needed any more. */
9904 cu->reset_builder ();
9905 }
9906
9907 /* Process an imported unit DIE. */
9908
9909 static void
9910 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9911 {
9912 struct attribute *attr;
9913
9914 /* For now we don't handle imported units in type units. */
9915 if (cu->per_cu->is_debug_types)
9916 {
9917 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9918 " supported in type units [in module %s]"),
9919 objfile_name (cu->per_objfile->objfile));
9920 }
9921
9922 attr = dwarf2_attr (die, DW_AT_import, cu);
9923 if (attr != NULL)
9924 {
9925 sect_offset sect_off = attr->get_ref_die_offset ();
9926 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9927 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9928 dwarf2_per_cu_data *per_cu
9929 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
9930
9931 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9932 into another compilation unit, at root level. Regard this as a hint,
9933 and ignore it. */
9934 if (die->parent && die->parent->parent == NULL
9935 && per_cu->unit_type == DW_UT_compile
9936 && per_cu->lang == language_cplus)
9937 return;
9938
9939 /* If necessary, add it to the queue and load its DIEs. */
9940 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9941 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
9942
9943 cu->per_cu->imported_symtabs_push (per_cu);
9944 }
9945 }
9946
9947 /* RAII object that represents a process_die scope: i.e.,
9948 starts/finishes processing a DIE. */
9949 class process_die_scope
9950 {
9951 public:
9952 process_die_scope (die_info *die, dwarf2_cu *cu)
9953 : m_die (die), m_cu (cu)
9954 {
9955 /* We should only be processing DIEs not already in process. */
9956 gdb_assert (!m_die->in_process);
9957 m_die->in_process = true;
9958 }
9959
9960 ~process_die_scope ()
9961 {
9962 m_die->in_process = false;
9963
9964 /* If we're done processing the DIE for the CU that owns the line
9965 header, we don't need the line header anymore. */
9966 if (m_cu->line_header_die_owner == m_die)
9967 {
9968 delete m_cu->line_header;
9969 m_cu->line_header = NULL;
9970 m_cu->line_header_die_owner = NULL;
9971 }
9972 }
9973
9974 private:
9975 die_info *m_die;
9976 dwarf2_cu *m_cu;
9977 };
9978
9979 /* Process a die and its children. */
9980
9981 static void
9982 process_die (struct die_info *die, struct dwarf2_cu *cu)
9983 {
9984 process_die_scope scope (die, cu);
9985
9986 switch (die->tag)
9987 {
9988 case DW_TAG_padding:
9989 break;
9990 case DW_TAG_compile_unit:
9991 case DW_TAG_partial_unit:
9992 read_file_scope (die, cu);
9993 break;
9994 case DW_TAG_type_unit:
9995 read_type_unit_scope (die, cu);
9996 break;
9997 case DW_TAG_subprogram:
9998 /* Nested subprograms in Fortran get a prefix. */
9999 if (cu->language == language_fortran
10000 && die->parent != NULL
10001 && die->parent->tag == DW_TAG_subprogram)
10002 cu->processing_has_namespace_info = true;
10003 /* Fall through. */
10004 case DW_TAG_inlined_subroutine:
10005 read_func_scope (die, cu);
10006 break;
10007 case DW_TAG_lexical_block:
10008 case DW_TAG_try_block:
10009 case DW_TAG_catch_block:
10010 read_lexical_block_scope (die, cu);
10011 break;
10012 case DW_TAG_call_site:
10013 case DW_TAG_GNU_call_site:
10014 read_call_site_scope (die, cu);
10015 break;
10016 case DW_TAG_class_type:
10017 case DW_TAG_interface_type:
10018 case DW_TAG_structure_type:
10019 case DW_TAG_union_type:
10020 process_structure_scope (die, cu);
10021 break;
10022 case DW_TAG_enumeration_type:
10023 process_enumeration_scope (die, cu);
10024 break;
10025
10026 /* These dies have a type, but processing them does not create
10027 a symbol or recurse to process the children. Therefore we can
10028 read them on-demand through read_type_die. */
10029 case DW_TAG_subroutine_type:
10030 case DW_TAG_set_type:
10031 case DW_TAG_array_type:
10032 case DW_TAG_pointer_type:
10033 case DW_TAG_ptr_to_member_type:
10034 case DW_TAG_reference_type:
10035 case DW_TAG_rvalue_reference_type:
10036 case DW_TAG_string_type:
10037 break;
10038
10039 case DW_TAG_base_type:
10040 case DW_TAG_subrange_type:
10041 case DW_TAG_typedef:
10042 /* Add a typedef symbol for the type definition, if it has a
10043 DW_AT_name. */
10044 new_symbol (die, read_type_die (die, cu), cu);
10045 break;
10046 case DW_TAG_common_block:
10047 read_common_block (die, cu);
10048 break;
10049 case DW_TAG_common_inclusion:
10050 break;
10051 case DW_TAG_namespace:
10052 cu->processing_has_namespace_info = true;
10053 read_namespace (die, cu);
10054 break;
10055 case DW_TAG_module:
10056 cu->processing_has_namespace_info = true;
10057 read_module (die, cu);
10058 break;
10059 case DW_TAG_imported_declaration:
10060 cu->processing_has_namespace_info = true;
10061 if (read_namespace_alias (die, cu))
10062 break;
10063 /* The declaration is not a global namespace alias. */
10064 /* Fall through. */
10065 case DW_TAG_imported_module:
10066 cu->processing_has_namespace_info = true;
10067 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10068 || cu->language != language_fortran))
10069 complaint (_("Tag '%s' has unexpected children"),
10070 dwarf_tag_name (die->tag));
10071 read_import_statement (die, cu);
10072 break;
10073
10074 case DW_TAG_imported_unit:
10075 process_imported_unit_die (die, cu);
10076 break;
10077
10078 case DW_TAG_variable:
10079 read_variable (die, cu);
10080 break;
10081
10082 default:
10083 new_symbol (die, NULL, cu);
10084 break;
10085 }
10086 }
10087 \f
10088 /* DWARF name computation. */
10089
10090 /* A helper function for dwarf2_compute_name which determines whether DIE
10091 needs to have the name of the scope prepended to the name listed in the
10092 die. */
10093
10094 static int
10095 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10096 {
10097 struct attribute *attr;
10098
10099 switch (die->tag)
10100 {
10101 case DW_TAG_namespace:
10102 case DW_TAG_typedef:
10103 case DW_TAG_class_type:
10104 case DW_TAG_interface_type:
10105 case DW_TAG_structure_type:
10106 case DW_TAG_union_type:
10107 case DW_TAG_enumeration_type:
10108 case DW_TAG_enumerator:
10109 case DW_TAG_subprogram:
10110 case DW_TAG_inlined_subroutine:
10111 case DW_TAG_member:
10112 case DW_TAG_imported_declaration:
10113 return 1;
10114
10115 case DW_TAG_variable:
10116 case DW_TAG_constant:
10117 /* We only need to prefix "globally" visible variables. These include
10118 any variable marked with DW_AT_external or any variable that
10119 lives in a namespace. [Variables in anonymous namespaces
10120 require prefixing, but they are not DW_AT_external.] */
10121
10122 if (dwarf2_attr (die, DW_AT_specification, cu))
10123 {
10124 struct dwarf2_cu *spec_cu = cu;
10125
10126 return die_needs_namespace (die_specification (die, &spec_cu),
10127 spec_cu);
10128 }
10129
10130 attr = dwarf2_attr (die, DW_AT_external, cu);
10131 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10132 && die->parent->tag != DW_TAG_module)
10133 return 0;
10134 /* A variable in a lexical block of some kind does not need a
10135 namespace, even though in C++ such variables may be external
10136 and have a mangled name. */
10137 if (die->parent->tag == DW_TAG_lexical_block
10138 || die->parent->tag == DW_TAG_try_block
10139 || die->parent->tag == DW_TAG_catch_block
10140 || die->parent->tag == DW_TAG_subprogram)
10141 return 0;
10142 return 1;
10143
10144 default:
10145 return 0;
10146 }
10147 }
10148
10149 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10150 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10151 defined for the given DIE. */
10152
10153 static struct attribute *
10154 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10155 {
10156 struct attribute *attr;
10157
10158 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10159 if (attr == NULL)
10160 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10161
10162 return attr;
10163 }
10164
10165 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10166 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10167 defined for the given DIE. */
10168
10169 static const char *
10170 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10171 {
10172 const char *linkage_name;
10173
10174 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10175 if (linkage_name == NULL)
10176 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10177
10178 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10179 See https://github.com/rust-lang/rust/issues/32925. */
10180 if (cu->language == language_rust && linkage_name != NULL
10181 && strchr (linkage_name, '{') != NULL)
10182 linkage_name = NULL;
10183
10184 return linkage_name;
10185 }
10186
10187 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10188 compute the physname for the object, which include a method's:
10189 - formal parameters (C++),
10190 - receiver type (Go),
10191
10192 The term "physname" is a bit confusing.
10193 For C++, for example, it is the demangled name.
10194 For Go, for example, it's the mangled name.
10195
10196 For Ada, return the DIE's linkage name rather than the fully qualified
10197 name. PHYSNAME is ignored..
10198
10199 The result is allocated on the objfile->per_bfd's obstack and
10200 canonicalized. */
10201
10202 static const char *
10203 dwarf2_compute_name (const char *name,
10204 struct die_info *die, struct dwarf2_cu *cu,
10205 int physname)
10206 {
10207 struct objfile *objfile = cu->per_objfile->objfile;
10208
10209 if (name == NULL)
10210 name = dwarf2_name (die, cu);
10211
10212 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10213 but otherwise compute it by typename_concat inside GDB.
10214 FIXME: Actually this is not really true, or at least not always true.
10215 It's all very confusing. compute_and_set_names doesn't try to demangle
10216 Fortran names because there is no mangling standard. So new_symbol
10217 will set the demangled name to the result of dwarf2_full_name, and it is
10218 the demangled name that GDB uses if it exists. */
10219 if (cu->language == language_ada
10220 || (cu->language == language_fortran && physname))
10221 {
10222 /* For Ada unit, we prefer the linkage name over the name, as
10223 the former contains the exported name, which the user expects
10224 to be able to reference. Ideally, we want the user to be able
10225 to reference this entity using either natural or linkage name,
10226 but we haven't started looking at this enhancement yet. */
10227 const char *linkage_name = dw2_linkage_name (die, cu);
10228
10229 if (linkage_name != NULL)
10230 return linkage_name;
10231 }
10232
10233 /* These are the only languages we know how to qualify names in. */
10234 if (name != NULL
10235 && (cu->language == language_cplus
10236 || cu->language == language_fortran || cu->language == language_d
10237 || cu->language == language_rust))
10238 {
10239 if (die_needs_namespace (die, cu))
10240 {
10241 const char *prefix;
10242 const char *canonical_name = NULL;
10243
10244 string_file buf;
10245
10246 prefix = determine_prefix (die, cu);
10247 if (*prefix != '\0')
10248 {
10249 gdb::unique_xmalloc_ptr<char> prefixed_name
10250 (typename_concat (NULL, prefix, name, physname, cu));
10251
10252 buf.puts (prefixed_name.get ());
10253 }
10254 else
10255 buf.puts (name);
10256
10257 /* Template parameters may be specified in the DIE's DW_AT_name, or
10258 as children with DW_TAG_template_type_param or
10259 DW_TAG_value_type_param. If the latter, add them to the name
10260 here. If the name already has template parameters, then
10261 skip this step; some versions of GCC emit both, and
10262 it is more efficient to use the pre-computed name.
10263
10264 Something to keep in mind about this process: it is very
10265 unlikely, or in some cases downright impossible, to produce
10266 something that will match the mangled name of a function.
10267 If the definition of the function has the same debug info,
10268 we should be able to match up with it anyway. But fallbacks
10269 using the minimal symbol, for instance to find a method
10270 implemented in a stripped copy of libstdc++, will not work.
10271 If we do not have debug info for the definition, we will have to
10272 match them up some other way.
10273
10274 When we do name matching there is a related problem with function
10275 templates; two instantiated function templates are allowed to
10276 differ only by their return types, which we do not add here. */
10277
10278 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10279 {
10280 struct attribute *attr;
10281 struct die_info *child;
10282 int first = 1;
10283
10284 die->building_fullname = 1;
10285
10286 for (child = die->child; child != NULL; child = child->sibling)
10287 {
10288 struct type *type;
10289 LONGEST value;
10290 const gdb_byte *bytes;
10291 struct dwarf2_locexpr_baton *baton;
10292 struct value *v;
10293
10294 if (child->tag != DW_TAG_template_type_param
10295 && child->tag != DW_TAG_template_value_param)
10296 continue;
10297
10298 if (first)
10299 {
10300 buf.puts ("<");
10301 first = 0;
10302 }
10303 else
10304 buf.puts (", ");
10305
10306 attr = dwarf2_attr (child, DW_AT_type, cu);
10307 if (attr == NULL)
10308 {
10309 complaint (_("template parameter missing DW_AT_type"));
10310 buf.puts ("UNKNOWN_TYPE");
10311 continue;
10312 }
10313 type = die_type (child, cu);
10314
10315 if (child->tag == DW_TAG_template_type_param)
10316 {
10317 c_print_type (type, "", &buf, -1, 0, cu->language,
10318 &type_print_raw_options);
10319 continue;
10320 }
10321
10322 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10323 if (attr == NULL)
10324 {
10325 complaint (_("template parameter missing "
10326 "DW_AT_const_value"));
10327 buf.puts ("UNKNOWN_VALUE");
10328 continue;
10329 }
10330
10331 dwarf2_const_value_attr (attr, type, name,
10332 &cu->comp_unit_obstack, cu,
10333 &value, &bytes, &baton);
10334
10335 if (TYPE_NOSIGN (type))
10336 /* GDB prints characters as NUMBER 'CHAR'. If that's
10337 changed, this can use value_print instead. */
10338 c_printchar (value, type, &buf);
10339 else
10340 {
10341 struct value_print_options opts;
10342
10343 if (baton != NULL)
10344 v = dwarf2_evaluate_loc_desc (type, NULL,
10345 baton->data,
10346 baton->size,
10347 baton->per_cu);
10348 else if (bytes != NULL)
10349 {
10350 v = allocate_value (type);
10351 memcpy (value_contents_writeable (v), bytes,
10352 TYPE_LENGTH (type));
10353 }
10354 else
10355 v = value_from_longest (type, value);
10356
10357 /* Specify decimal so that we do not depend on
10358 the radix. */
10359 get_formatted_print_options (&opts, 'd');
10360 opts.raw = 1;
10361 value_print (v, &buf, &opts);
10362 release_value (v);
10363 }
10364 }
10365
10366 die->building_fullname = 0;
10367
10368 if (!first)
10369 {
10370 /* Close the argument list, with a space if necessary
10371 (nested templates). */
10372 if (!buf.empty () && buf.string ().back () == '>')
10373 buf.puts (" >");
10374 else
10375 buf.puts (">");
10376 }
10377 }
10378
10379 /* For C++ methods, append formal parameter type
10380 information, if PHYSNAME. */
10381
10382 if (physname && die->tag == DW_TAG_subprogram
10383 && cu->language == language_cplus)
10384 {
10385 struct type *type = read_type_die (die, cu);
10386
10387 c_type_print_args (type, &buf, 1, cu->language,
10388 &type_print_raw_options);
10389
10390 if (cu->language == language_cplus)
10391 {
10392 /* Assume that an artificial first parameter is
10393 "this", but do not crash if it is not. RealView
10394 marks unnamed (and thus unused) parameters as
10395 artificial; there is no way to differentiate
10396 the two cases. */
10397 if (type->num_fields () > 0
10398 && TYPE_FIELD_ARTIFICIAL (type, 0)
10399 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
10400 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10401 0))))
10402 buf.puts (" const");
10403 }
10404 }
10405
10406 const std::string &intermediate_name = buf.string ();
10407
10408 if (cu->language == language_cplus)
10409 canonical_name
10410 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10411 objfile);
10412
10413 /* If we only computed INTERMEDIATE_NAME, or if
10414 INTERMEDIATE_NAME is already canonical, then we need to
10415 intern it. */
10416 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10417 name = objfile->intern (intermediate_name);
10418 else
10419 name = canonical_name;
10420 }
10421 }
10422
10423 return name;
10424 }
10425
10426 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10427 If scope qualifiers are appropriate they will be added. The result
10428 will be allocated on the storage_obstack, or NULL if the DIE does
10429 not have a name. NAME may either be from a previous call to
10430 dwarf2_name or NULL.
10431
10432 The output string will be canonicalized (if C++). */
10433
10434 static const char *
10435 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10436 {
10437 return dwarf2_compute_name (name, die, cu, 0);
10438 }
10439
10440 /* Construct a physname for the given DIE in CU. NAME may either be
10441 from a previous call to dwarf2_name or NULL. The result will be
10442 allocated on the objfile_objstack or NULL if the DIE does not have a
10443 name.
10444
10445 The output string will be canonicalized (if C++). */
10446
10447 static const char *
10448 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10449 {
10450 struct objfile *objfile = cu->per_objfile->objfile;
10451 const char *retval, *mangled = NULL, *canon = NULL;
10452 int need_copy = 1;
10453
10454 /* In this case dwarf2_compute_name is just a shortcut not building anything
10455 on its own. */
10456 if (!die_needs_namespace (die, cu))
10457 return dwarf2_compute_name (name, die, cu, 1);
10458
10459 if (cu->language != language_rust)
10460 mangled = dw2_linkage_name (die, cu);
10461
10462 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10463 has computed. */
10464 gdb::unique_xmalloc_ptr<char> demangled;
10465 if (mangled != NULL)
10466 {
10467
10468 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10469 {
10470 /* Do nothing (do not demangle the symbol name). */
10471 }
10472 else if (cu->language == language_go)
10473 {
10474 /* This is a lie, but we already lie to the caller new_symbol.
10475 new_symbol assumes we return the mangled name.
10476 This just undoes that lie until things are cleaned up. */
10477 }
10478 else
10479 {
10480 /* Use DMGL_RET_DROP for C++ template functions to suppress
10481 their return type. It is easier for GDB users to search
10482 for such functions as `name(params)' than `long name(params)'.
10483 In such case the minimal symbol names do not match the full
10484 symbol names but for template functions there is never a need
10485 to look up their definition from their declaration so
10486 the only disadvantage remains the minimal symbol variant
10487 `long name(params)' does not have the proper inferior type. */
10488 demangled.reset (gdb_demangle (mangled,
10489 (DMGL_PARAMS | DMGL_ANSI
10490 | DMGL_RET_DROP)));
10491 }
10492 if (demangled)
10493 canon = demangled.get ();
10494 else
10495 {
10496 canon = mangled;
10497 need_copy = 0;
10498 }
10499 }
10500
10501 if (canon == NULL || check_physname)
10502 {
10503 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10504
10505 if (canon != NULL && strcmp (physname, canon) != 0)
10506 {
10507 /* It may not mean a bug in GDB. The compiler could also
10508 compute DW_AT_linkage_name incorrectly. But in such case
10509 GDB would need to be bug-to-bug compatible. */
10510
10511 complaint (_("Computed physname <%s> does not match demangled <%s> "
10512 "(from linkage <%s>) - DIE at %s [in module %s]"),
10513 physname, canon, mangled, sect_offset_str (die->sect_off),
10514 objfile_name (objfile));
10515
10516 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10517 is available here - over computed PHYSNAME. It is safer
10518 against both buggy GDB and buggy compilers. */
10519
10520 retval = canon;
10521 }
10522 else
10523 {
10524 retval = physname;
10525 need_copy = 0;
10526 }
10527 }
10528 else
10529 retval = canon;
10530
10531 if (need_copy)
10532 retval = objfile->intern (retval);
10533
10534 return retval;
10535 }
10536
10537 /* Inspect DIE in CU for a namespace alias. If one exists, record
10538 a new symbol for it.
10539
10540 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10541
10542 static int
10543 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10544 {
10545 struct attribute *attr;
10546
10547 /* If the die does not have a name, this is not a namespace
10548 alias. */
10549 attr = dwarf2_attr (die, DW_AT_name, cu);
10550 if (attr != NULL)
10551 {
10552 int num;
10553 struct die_info *d = die;
10554 struct dwarf2_cu *imported_cu = cu;
10555
10556 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10557 keep inspecting DIEs until we hit the underlying import. */
10558 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10559 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10560 {
10561 attr = dwarf2_attr (d, DW_AT_import, cu);
10562 if (attr == NULL)
10563 break;
10564
10565 d = follow_die_ref (d, attr, &imported_cu);
10566 if (d->tag != DW_TAG_imported_declaration)
10567 break;
10568 }
10569
10570 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10571 {
10572 complaint (_("DIE at %s has too many recursively imported "
10573 "declarations"), sect_offset_str (d->sect_off));
10574 return 0;
10575 }
10576
10577 if (attr != NULL)
10578 {
10579 struct type *type;
10580 sect_offset sect_off = attr->get_ref_die_offset ();
10581
10582 type = get_die_type_at_offset (sect_off, cu->per_cu);
10583 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10584 {
10585 /* This declaration is a global namespace alias. Add
10586 a symbol for it whose type is the aliased namespace. */
10587 new_symbol (die, type, cu);
10588 return 1;
10589 }
10590 }
10591 }
10592
10593 return 0;
10594 }
10595
10596 /* Return the using directives repository (global or local?) to use in the
10597 current context for CU.
10598
10599 For Ada, imported declarations can materialize renamings, which *may* be
10600 global. However it is impossible (for now?) in DWARF to distinguish
10601 "external" imported declarations and "static" ones. As all imported
10602 declarations seem to be static in all other languages, make them all CU-wide
10603 global only in Ada. */
10604
10605 static struct using_direct **
10606 using_directives (struct dwarf2_cu *cu)
10607 {
10608 if (cu->language == language_ada
10609 && cu->get_builder ()->outermost_context_p ())
10610 return cu->get_builder ()->get_global_using_directives ();
10611 else
10612 return cu->get_builder ()->get_local_using_directives ();
10613 }
10614
10615 /* Read the import statement specified by the given die and record it. */
10616
10617 static void
10618 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10619 {
10620 struct objfile *objfile = cu->per_objfile->objfile;
10621 struct attribute *import_attr;
10622 struct die_info *imported_die, *child_die;
10623 struct dwarf2_cu *imported_cu;
10624 const char *imported_name;
10625 const char *imported_name_prefix;
10626 const char *canonical_name;
10627 const char *import_alias;
10628 const char *imported_declaration = NULL;
10629 const char *import_prefix;
10630 std::vector<const char *> excludes;
10631
10632 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10633 if (import_attr == NULL)
10634 {
10635 complaint (_("Tag '%s' has no DW_AT_import"),
10636 dwarf_tag_name (die->tag));
10637 return;
10638 }
10639
10640 imported_cu = cu;
10641 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10642 imported_name = dwarf2_name (imported_die, imported_cu);
10643 if (imported_name == NULL)
10644 {
10645 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10646
10647 The import in the following code:
10648 namespace A
10649 {
10650 typedef int B;
10651 }
10652
10653 int main ()
10654 {
10655 using A::B;
10656 B b;
10657 return b;
10658 }
10659
10660 ...
10661 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10662 <52> DW_AT_decl_file : 1
10663 <53> DW_AT_decl_line : 6
10664 <54> DW_AT_import : <0x75>
10665 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10666 <59> DW_AT_name : B
10667 <5b> DW_AT_decl_file : 1
10668 <5c> DW_AT_decl_line : 2
10669 <5d> DW_AT_type : <0x6e>
10670 ...
10671 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10672 <76> DW_AT_byte_size : 4
10673 <77> DW_AT_encoding : 5 (signed)
10674
10675 imports the wrong die ( 0x75 instead of 0x58 ).
10676 This case will be ignored until the gcc bug is fixed. */
10677 return;
10678 }
10679
10680 /* Figure out the local name after import. */
10681 import_alias = dwarf2_name (die, cu);
10682
10683 /* Figure out where the statement is being imported to. */
10684 import_prefix = determine_prefix (die, cu);
10685
10686 /* Figure out what the scope of the imported die is and prepend it
10687 to the name of the imported die. */
10688 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10689
10690 if (imported_die->tag != DW_TAG_namespace
10691 && imported_die->tag != DW_TAG_module)
10692 {
10693 imported_declaration = imported_name;
10694 canonical_name = imported_name_prefix;
10695 }
10696 else if (strlen (imported_name_prefix) > 0)
10697 canonical_name = obconcat (&objfile->objfile_obstack,
10698 imported_name_prefix,
10699 (cu->language == language_d ? "." : "::"),
10700 imported_name, (char *) NULL);
10701 else
10702 canonical_name = imported_name;
10703
10704 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10705 for (child_die = die->child; child_die && child_die->tag;
10706 child_die = child_die->sibling)
10707 {
10708 /* DWARF-4: A Fortran use statement with a “rename list” may be
10709 represented by an imported module entry with an import attribute
10710 referring to the module and owned entries corresponding to those
10711 entities that are renamed as part of being imported. */
10712
10713 if (child_die->tag != DW_TAG_imported_declaration)
10714 {
10715 complaint (_("child DW_TAG_imported_declaration expected "
10716 "- DIE at %s [in module %s]"),
10717 sect_offset_str (child_die->sect_off),
10718 objfile_name (objfile));
10719 continue;
10720 }
10721
10722 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10723 if (import_attr == NULL)
10724 {
10725 complaint (_("Tag '%s' has no DW_AT_import"),
10726 dwarf_tag_name (child_die->tag));
10727 continue;
10728 }
10729
10730 imported_cu = cu;
10731 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10732 &imported_cu);
10733 imported_name = dwarf2_name (imported_die, imported_cu);
10734 if (imported_name == NULL)
10735 {
10736 complaint (_("child DW_TAG_imported_declaration has unknown "
10737 "imported name - DIE at %s [in module %s]"),
10738 sect_offset_str (child_die->sect_off),
10739 objfile_name (objfile));
10740 continue;
10741 }
10742
10743 excludes.push_back (imported_name);
10744
10745 process_die (child_die, cu);
10746 }
10747
10748 add_using_directive (using_directives (cu),
10749 import_prefix,
10750 canonical_name,
10751 import_alias,
10752 imported_declaration,
10753 excludes,
10754 0,
10755 &objfile->objfile_obstack);
10756 }
10757
10758 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10759 types, but gives them a size of zero. Starting with version 14,
10760 ICC is compatible with GCC. */
10761
10762 static bool
10763 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10764 {
10765 if (!cu->checked_producer)
10766 check_producer (cu);
10767
10768 return cu->producer_is_icc_lt_14;
10769 }
10770
10771 /* ICC generates a DW_AT_type for C void functions. This was observed on
10772 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10773 which says that void functions should not have a DW_AT_type. */
10774
10775 static bool
10776 producer_is_icc (struct dwarf2_cu *cu)
10777 {
10778 if (!cu->checked_producer)
10779 check_producer (cu);
10780
10781 return cu->producer_is_icc;
10782 }
10783
10784 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10785 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10786 this, it was first present in GCC release 4.3.0. */
10787
10788 static bool
10789 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10790 {
10791 if (!cu->checked_producer)
10792 check_producer (cu);
10793
10794 return cu->producer_is_gcc_lt_4_3;
10795 }
10796
10797 static file_and_directory
10798 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10799 {
10800 file_and_directory res;
10801
10802 /* Find the filename. Do not use dwarf2_name here, since the filename
10803 is not a source language identifier. */
10804 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10805 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10806
10807 if (res.comp_dir == NULL
10808 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10809 && IS_ABSOLUTE_PATH (res.name))
10810 {
10811 res.comp_dir_storage = ldirname (res.name);
10812 if (!res.comp_dir_storage.empty ())
10813 res.comp_dir = res.comp_dir_storage.c_str ();
10814 }
10815 if (res.comp_dir != NULL)
10816 {
10817 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10818 directory, get rid of it. */
10819 const char *cp = strchr (res.comp_dir, ':');
10820
10821 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10822 res.comp_dir = cp + 1;
10823 }
10824
10825 if (res.name == NULL)
10826 res.name = "<unknown>";
10827
10828 return res;
10829 }
10830
10831 /* Handle DW_AT_stmt_list for a compilation unit.
10832 DIE is the DW_TAG_compile_unit die for CU.
10833 COMP_DIR is the compilation directory. LOWPC is passed to
10834 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10835
10836 static void
10837 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10838 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10839 {
10840 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10841 struct attribute *attr;
10842 struct line_header line_header_local;
10843 hashval_t line_header_local_hash;
10844 void **slot;
10845 int decode_mapping;
10846
10847 gdb_assert (! cu->per_cu->is_debug_types);
10848
10849 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10850 if (attr == NULL)
10851 return;
10852
10853 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10854
10855 /* The line header hash table is only created if needed (it exists to
10856 prevent redundant reading of the line table for partial_units).
10857 If we're given a partial_unit, we'll need it. If we're given a
10858 compile_unit, then use the line header hash table if it's already
10859 created, but don't create one just yet. */
10860
10861 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
10862 && die->tag == DW_TAG_partial_unit)
10863 {
10864 dwarf2_per_objfile->per_bfd->line_header_hash
10865 .reset (htab_create_alloc (127, line_header_hash_voidp,
10866 line_header_eq_voidp,
10867 free_line_header_voidp,
10868 xcalloc, xfree));
10869 }
10870
10871 line_header_local.sect_off = line_offset;
10872 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10873 line_header_local_hash = line_header_hash (&line_header_local);
10874 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
10875 {
10876 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10877 &line_header_local,
10878 line_header_local_hash, NO_INSERT);
10879
10880 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10881 is not present in *SLOT (since if there is something in *SLOT then
10882 it will be for a partial_unit). */
10883 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10884 {
10885 gdb_assert (*slot != NULL);
10886 cu->line_header = (struct line_header *) *slot;
10887 return;
10888 }
10889 }
10890
10891 /* dwarf_decode_line_header does not yet provide sufficient information.
10892 We always have to call also dwarf_decode_lines for it. */
10893 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10894 if (lh == NULL)
10895 return;
10896
10897 cu->line_header = lh.release ();
10898 cu->line_header_die_owner = die;
10899
10900 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
10901 slot = NULL;
10902 else
10903 {
10904 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10905 &line_header_local,
10906 line_header_local_hash, INSERT);
10907 gdb_assert (slot != NULL);
10908 }
10909 if (slot != NULL && *slot == NULL)
10910 {
10911 /* This newly decoded line number information unit will be owned
10912 by line_header_hash hash table. */
10913 *slot = cu->line_header;
10914 cu->line_header_die_owner = NULL;
10915 }
10916 else
10917 {
10918 /* We cannot free any current entry in (*slot) as that struct line_header
10919 may be already used by multiple CUs. Create only temporary decoded
10920 line_header for this CU - it may happen at most once for each line
10921 number information unit. And if we're not using line_header_hash
10922 then this is what we want as well. */
10923 gdb_assert (die->tag != DW_TAG_partial_unit);
10924 }
10925 decode_mapping = (die->tag != DW_TAG_partial_unit);
10926 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10927 decode_mapping);
10928
10929 }
10930
10931 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10932
10933 static void
10934 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10935 {
10936 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10937 struct objfile *objfile = dwarf2_per_objfile->objfile;
10938 struct gdbarch *gdbarch = objfile->arch ();
10939 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10940 CORE_ADDR highpc = ((CORE_ADDR) 0);
10941 struct attribute *attr;
10942 struct die_info *child_die;
10943 CORE_ADDR baseaddr;
10944
10945 prepare_one_comp_unit (cu, die, cu->language);
10946 baseaddr = objfile->text_section_offset ();
10947
10948 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10949
10950 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10951 from finish_block. */
10952 if (lowpc == ((CORE_ADDR) -1))
10953 lowpc = highpc;
10954 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10955
10956 file_and_directory fnd = find_file_and_directory (die, cu);
10957
10958 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10959 standardised yet. As a workaround for the language detection we fall
10960 back to the DW_AT_producer string. */
10961 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10962 cu->language = language_opencl;
10963
10964 /* Similar hack for Go. */
10965 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10966 set_cu_language (DW_LANG_Go, cu);
10967
10968 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10969
10970 /* Decode line number information if present. We do this before
10971 processing child DIEs, so that the line header table is available
10972 for DW_AT_decl_file. */
10973 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10974
10975 /* Process all dies in compilation unit. */
10976 if (die->child != NULL)
10977 {
10978 child_die = die->child;
10979 while (child_die && child_die->tag)
10980 {
10981 process_die (child_die, cu);
10982 child_die = child_die->sibling;
10983 }
10984 }
10985
10986 /* Decode macro information, if present. Dwarf 2 macro information
10987 refers to information in the line number info statement program
10988 header, so we can only read it if we've read the header
10989 successfully. */
10990 attr = dwarf2_attr (die, DW_AT_macros, cu);
10991 if (attr == NULL)
10992 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10993 if (attr && cu->line_header)
10994 {
10995 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10996 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10997
10998 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10999 }
11000 else
11001 {
11002 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11003 if (attr && cu->line_header)
11004 {
11005 unsigned int macro_offset = DW_UNSND (attr);
11006
11007 dwarf_decode_macros (cu, macro_offset, 0);
11008 }
11009 }
11010 }
11011
11012 void
11013 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11014 {
11015 struct type_unit_group *tu_group;
11016 int first_time;
11017 struct attribute *attr;
11018 unsigned int i;
11019 struct signatured_type *sig_type;
11020
11021 gdb_assert (per_cu->is_debug_types);
11022 sig_type = (struct signatured_type *) per_cu;
11023
11024 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11025
11026 /* If we're using .gdb_index (includes -readnow) then
11027 per_cu->type_unit_group may not have been set up yet. */
11028 if (sig_type->type_unit_group == NULL)
11029 sig_type->type_unit_group = get_type_unit_group (this, attr);
11030 tu_group = sig_type->type_unit_group;
11031
11032 /* If we've already processed this stmt_list there's no real need to
11033 do it again, we could fake it and just recreate the part we need
11034 (file name,index -> symtab mapping). If data shows this optimization
11035 is useful we can do it then. */
11036 first_time = tu_group->compunit_symtab == NULL;
11037
11038 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11039 debug info. */
11040 line_header_up lh;
11041 if (attr != NULL)
11042 {
11043 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11044 lh = dwarf_decode_line_header (line_offset, this);
11045 }
11046 if (lh == NULL)
11047 {
11048 if (first_time)
11049 start_symtab ("", NULL, 0);
11050 else
11051 {
11052 gdb_assert (tu_group->symtabs == NULL);
11053 gdb_assert (m_builder == nullptr);
11054 struct compunit_symtab *cust = tu_group->compunit_symtab;
11055 m_builder.reset (new struct buildsym_compunit
11056 (COMPUNIT_OBJFILE (cust), "",
11057 COMPUNIT_DIRNAME (cust),
11058 compunit_language (cust),
11059 0, cust));
11060 list_in_scope = get_builder ()->get_file_symbols ();
11061 }
11062 return;
11063 }
11064
11065 line_header = lh.release ();
11066 line_header_die_owner = die;
11067
11068 if (first_time)
11069 {
11070 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11071
11072 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11073 still initializing it, and our caller (a few levels up)
11074 process_full_type_unit still needs to know if this is the first
11075 time. */
11076
11077 tu_group->symtabs
11078 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11079 struct symtab *, line_header->file_names_size ());
11080
11081 auto &file_names = line_header->file_names ();
11082 for (i = 0; i < file_names.size (); ++i)
11083 {
11084 file_entry &fe = file_names[i];
11085 dwarf2_start_subfile (this, fe.name,
11086 fe.include_dir (line_header));
11087 buildsym_compunit *b = get_builder ();
11088 if (b->get_current_subfile ()->symtab == NULL)
11089 {
11090 /* NOTE: start_subfile will recognize when it's been
11091 passed a file it has already seen. So we can't
11092 assume there's a simple mapping from
11093 cu->line_header->file_names to subfiles, plus
11094 cu->line_header->file_names may contain dups. */
11095 b->get_current_subfile ()->symtab
11096 = allocate_symtab (cust, b->get_current_subfile ()->name);
11097 }
11098
11099 fe.symtab = b->get_current_subfile ()->symtab;
11100 tu_group->symtabs[i] = fe.symtab;
11101 }
11102 }
11103 else
11104 {
11105 gdb_assert (m_builder == nullptr);
11106 struct compunit_symtab *cust = tu_group->compunit_symtab;
11107 m_builder.reset (new struct buildsym_compunit
11108 (COMPUNIT_OBJFILE (cust), "",
11109 COMPUNIT_DIRNAME (cust),
11110 compunit_language (cust),
11111 0, cust));
11112 list_in_scope = get_builder ()->get_file_symbols ();
11113
11114 auto &file_names = line_header->file_names ();
11115 for (i = 0; i < file_names.size (); ++i)
11116 {
11117 file_entry &fe = file_names[i];
11118 fe.symtab = tu_group->symtabs[i];
11119 }
11120 }
11121
11122 /* The main symtab is allocated last. Type units don't have DW_AT_name
11123 so they don't have a "real" (so to speak) symtab anyway.
11124 There is later code that will assign the main symtab to all symbols
11125 that don't have one. We need to handle the case of a symbol with a
11126 missing symtab (DW_AT_decl_file) anyway. */
11127 }
11128
11129 /* Process DW_TAG_type_unit.
11130 For TUs we want to skip the first top level sibling if it's not the
11131 actual type being defined by this TU. In this case the first top
11132 level sibling is there to provide context only. */
11133
11134 static void
11135 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11136 {
11137 struct die_info *child_die;
11138
11139 prepare_one_comp_unit (cu, die, language_minimal);
11140
11141 /* Initialize (or reinitialize) the machinery for building symtabs.
11142 We do this before processing child DIEs, so that the line header table
11143 is available for DW_AT_decl_file. */
11144 cu->setup_type_unit_groups (die);
11145
11146 if (die->child != NULL)
11147 {
11148 child_die = die->child;
11149 while (child_die && child_die->tag)
11150 {
11151 process_die (child_die, cu);
11152 child_die = child_die->sibling;
11153 }
11154 }
11155 }
11156 \f
11157 /* DWO/DWP files.
11158
11159 http://gcc.gnu.org/wiki/DebugFission
11160 http://gcc.gnu.org/wiki/DebugFissionDWP
11161
11162 To simplify handling of both DWO files ("object" files with the DWARF info)
11163 and DWP files (a file with the DWOs packaged up into one file), we treat
11164 DWP files as having a collection of virtual DWO files. */
11165
11166 static hashval_t
11167 hash_dwo_file (const void *item)
11168 {
11169 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11170 hashval_t hash;
11171
11172 hash = htab_hash_string (dwo_file->dwo_name);
11173 if (dwo_file->comp_dir != NULL)
11174 hash += htab_hash_string (dwo_file->comp_dir);
11175 return hash;
11176 }
11177
11178 static int
11179 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11180 {
11181 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11182 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11183
11184 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11185 return 0;
11186 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11187 return lhs->comp_dir == rhs->comp_dir;
11188 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11189 }
11190
11191 /* Allocate a hash table for DWO files. */
11192
11193 static htab_up
11194 allocate_dwo_file_hash_table ()
11195 {
11196 auto delete_dwo_file = [] (void *item)
11197 {
11198 struct dwo_file *dwo_file = (struct dwo_file *) item;
11199
11200 delete dwo_file;
11201 };
11202
11203 return htab_up (htab_create_alloc (41,
11204 hash_dwo_file,
11205 eq_dwo_file,
11206 delete_dwo_file,
11207 xcalloc, xfree));
11208 }
11209
11210 /* Lookup DWO file DWO_NAME. */
11211
11212 static void **
11213 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11214 const char *dwo_name,
11215 const char *comp_dir)
11216 {
11217 struct dwo_file find_entry;
11218 void **slot;
11219
11220 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11221 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11222
11223 find_entry.dwo_name = dwo_name;
11224 find_entry.comp_dir = comp_dir;
11225 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
11226 INSERT);
11227
11228 return slot;
11229 }
11230
11231 static hashval_t
11232 hash_dwo_unit (const void *item)
11233 {
11234 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11235
11236 /* This drops the top 32 bits of the id, but is ok for a hash. */
11237 return dwo_unit->signature;
11238 }
11239
11240 static int
11241 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11242 {
11243 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11244 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11245
11246 /* The signature is assumed to be unique within the DWO file.
11247 So while object file CU dwo_id's always have the value zero,
11248 that's OK, assuming each object file DWO file has only one CU,
11249 and that's the rule for now. */
11250 return lhs->signature == rhs->signature;
11251 }
11252
11253 /* Allocate a hash table for DWO CUs,TUs.
11254 There is one of these tables for each of CUs,TUs for each DWO file. */
11255
11256 static htab_up
11257 allocate_dwo_unit_table ()
11258 {
11259 /* Start out with a pretty small number.
11260 Generally DWO files contain only one CU and maybe some TUs. */
11261 return htab_up (htab_create_alloc (3,
11262 hash_dwo_unit,
11263 eq_dwo_unit,
11264 NULL, xcalloc, xfree));
11265 }
11266
11267 /* die_reader_func for create_dwo_cu. */
11268
11269 static void
11270 create_dwo_cu_reader (const struct die_reader_specs *reader,
11271 const gdb_byte *info_ptr,
11272 struct die_info *comp_unit_die,
11273 struct dwo_file *dwo_file,
11274 struct dwo_unit *dwo_unit)
11275 {
11276 struct dwarf2_cu *cu = reader->cu;
11277 sect_offset sect_off = cu->per_cu->sect_off;
11278 struct dwarf2_section_info *section = cu->per_cu->section;
11279
11280 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11281 if (!signature.has_value ())
11282 {
11283 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11284 " its dwo_id [in module %s]"),
11285 sect_offset_str (sect_off), dwo_file->dwo_name);
11286 return;
11287 }
11288
11289 dwo_unit->dwo_file = dwo_file;
11290 dwo_unit->signature = *signature;
11291 dwo_unit->section = section;
11292 dwo_unit->sect_off = sect_off;
11293 dwo_unit->length = cu->per_cu->length;
11294
11295 if (dwarf_read_debug)
11296 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11297 sect_offset_str (sect_off),
11298 hex_string (dwo_unit->signature));
11299 }
11300
11301 /* Create the dwo_units for the CUs in a DWO_FILE.
11302 Note: This function processes DWO files only, not DWP files. */
11303
11304 static void
11305 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11306 dwarf2_cu *cu, struct dwo_file &dwo_file,
11307 dwarf2_section_info &section, htab_up &cus_htab)
11308 {
11309 struct objfile *objfile = dwarf2_per_objfile->objfile;
11310 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
11311 const gdb_byte *info_ptr, *end_ptr;
11312
11313 section.read (objfile);
11314 info_ptr = section.buffer;
11315
11316 if (info_ptr == NULL)
11317 return;
11318
11319 if (dwarf_read_debug)
11320 {
11321 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11322 section.get_name (),
11323 section.get_file_name ());
11324 }
11325
11326 end_ptr = info_ptr + section.size;
11327 while (info_ptr < end_ptr)
11328 {
11329 struct dwarf2_per_cu_data per_cu;
11330 struct dwo_unit read_unit {};
11331 struct dwo_unit *dwo_unit;
11332 void **slot;
11333 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11334
11335 memset (&per_cu, 0, sizeof (per_cu));
11336 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11337 per_cu.per_bfd = per_bfd;
11338 per_cu.is_debug_types = 0;
11339 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11340 per_cu.section = &section;
11341
11342 cutu_reader reader (&per_cu, dwarf2_per_objfile, cu, &dwo_file);
11343 if (!reader.dummy_p)
11344 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11345 &dwo_file, &read_unit);
11346 info_ptr += per_cu.length;
11347
11348 // If the unit could not be parsed, skip it.
11349 if (read_unit.dwo_file == NULL)
11350 continue;
11351
11352 if (cus_htab == NULL)
11353 cus_htab = allocate_dwo_unit_table ();
11354
11355 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11356 struct dwo_unit);
11357 *dwo_unit = read_unit;
11358 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11359 gdb_assert (slot != NULL);
11360 if (*slot != NULL)
11361 {
11362 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11363 sect_offset dup_sect_off = dup_cu->sect_off;
11364
11365 complaint (_("debug cu entry at offset %s is duplicate to"
11366 " the entry at offset %s, signature %s"),
11367 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11368 hex_string (dwo_unit->signature));
11369 }
11370 *slot = (void *)dwo_unit;
11371 }
11372 }
11373
11374 /* DWP file .debug_{cu,tu}_index section format:
11375 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11376
11377 DWP Version 1:
11378
11379 Both index sections have the same format, and serve to map a 64-bit
11380 signature to a set of section numbers. Each section begins with a header,
11381 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11382 indexes, and a pool of 32-bit section numbers. The index sections will be
11383 aligned at 8-byte boundaries in the file.
11384
11385 The index section header consists of:
11386
11387 V, 32 bit version number
11388 -, 32 bits unused
11389 N, 32 bit number of compilation units or type units in the index
11390 M, 32 bit number of slots in the hash table
11391
11392 Numbers are recorded using the byte order of the application binary.
11393
11394 The hash table begins at offset 16 in the section, and consists of an array
11395 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11396 order of the application binary). Unused slots in the hash table are 0.
11397 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11398
11399 The parallel table begins immediately after the hash table
11400 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11401 array of 32-bit indexes (using the byte order of the application binary),
11402 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11403 table contains a 32-bit index into the pool of section numbers. For unused
11404 hash table slots, the corresponding entry in the parallel table will be 0.
11405
11406 The pool of section numbers begins immediately following the hash table
11407 (at offset 16 + 12 * M from the beginning of the section). The pool of
11408 section numbers consists of an array of 32-bit words (using the byte order
11409 of the application binary). Each item in the array is indexed starting
11410 from 0. The hash table entry provides the index of the first section
11411 number in the set. Additional section numbers in the set follow, and the
11412 set is terminated by a 0 entry (section number 0 is not used in ELF).
11413
11414 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11415 section must be the first entry in the set, and the .debug_abbrev.dwo must
11416 be the second entry. Other members of the set may follow in any order.
11417
11418 ---
11419
11420 DWP Version 2:
11421
11422 DWP Version 2 combines all the .debug_info, etc. sections into one,
11423 and the entries in the index tables are now offsets into these sections.
11424 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11425 section.
11426
11427 Index Section Contents:
11428 Header
11429 Hash Table of Signatures dwp_hash_table.hash_table
11430 Parallel Table of Indices dwp_hash_table.unit_table
11431 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11432 Table of Section Sizes dwp_hash_table.v2.sizes
11433
11434 The index section header consists of:
11435
11436 V, 32 bit version number
11437 L, 32 bit number of columns in the table of section offsets
11438 N, 32 bit number of compilation units or type units in the index
11439 M, 32 bit number of slots in the hash table
11440
11441 Numbers are recorded using the byte order of the application binary.
11442
11443 The hash table has the same format as version 1.
11444 The parallel table of indices has the same format as version 1,
11445 except that the entries are origin-1 indices into the table of sections
11446 offsets and the table of section sizes.
11447
11448 The table of offsets begins immediately following the parallel table
11449 (at offset 16 + 12 * M from the beginning of the section). The table is
11450 a two-dimensional array of 32-bit words (using the byte order of the
11451 application binary), with L columns and N+1 rows, in row-major order.
11452 Each row in the array is indexed starting from 0. The first row provides
11453 a key to the remaining rows: each column in this row provides an identifier
11454 for a debug section, and the offsets in the same column of subsequent rows
11455 refer to that section. The section identifiers are:
11456
11457 DW_SECT_INFO 1 .debug_info.dwo
11458 DW_SECT_TYPES 2 .debug_types.dwo
11459 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11460 DW_SECT_LINE 4 .debug_line.dwo
11461 DW_SECT_LOC 5 .debug_loc.dwo
11462 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11463 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11464 DW_SECT_MACRO 8 .debug_macro.dwo
11465
11466 The offsets provided by the CU and TU index sections are the base offsets
11467 for the contributions made by each CU or TU to the corresponding section
11468 in the package file. Each CU and TU header contains an abbrev_offset
11469 field, used to find the abbreviations table for that CU or TU within the
11470 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11471 be interpreted as relative to the base offset given in the index section.
11472 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11473 should be interpreted as relative to the base offset for .debug_line.dwo,
11474 and offsets into other debug sections obtained from DWARF attributes should
11475 also be interpreted as relative to the corresponding base offset.
11476
11477 The table of sizes begins immediately following the table of offsets.
11478 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11479 with L columns and N rows, in row-major order. Each row in the array is
11480 indexed starting from 1 (row 0 is shared by the two tables).
11481
11482 ---
11483
11484 Hash table lookup is handled the same in version 1 and 2:
11485
11486 We assume that N and M will not exceed 2^32 - 1.
11487 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11488
11489 Given a 64-bit compilation unit signature or a type signature S, an entry
11490 in the hash table is located as follows:
11491
11492 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11493 the low-order k bits all set to 1.
11494
11495 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11496
11497 3) If the hash table entry at index H matches the signature, use that
11498 entry. If the hash table entry at index H is unused (all zeroes),
11499 terminate the search: the signature is not present in the table.
11500
11501 4) Let H = (H + H') modulo M. Repeat at Step 3.
11502
11503 Because M > N and H' and M are relatively prime, the search is guaranteed
11504 to stop at an unused slot or find the match. */
11505
11506 /* Create a hash table to map DWO IDs to their CU/TU entry in
11507 .debug_{info,types}.dwo in DWP_FILE.
11508 Returns NULL if there isn't one.
11509 Note: This function processes DWP files only, not DWO files. */
11510
11511 static struct dwp_hash_table *
11512 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11513 struct dwp_file *dwp_file, int is_debug_types)
11514 {
11515 struct objfile *objfile = dwarf2_per_objfile->objfile;
11516 bfd *dbfd = dwp_file->dbfd.get ();
11517 const gdb_byte *index_ptr, *index_end;
11518 struct dwarf2_section_info *index;
11519 uint32_t version, nr_columns, nr_units, nr_slots;
11520 struct dwp_hash_table *htab;
11521
11522 if (is_debug_types)
11523 index = &dwp_file->sections.tu_index;
11524 else
11525 index = &dwp_file->sections.cu_index;
11526
11527 if (index->empty ())
11528 return NULL;
11529 index->read (objfile);
11530
11531 index_ptr = index->buffer;
11532 index_end = index_ptr + index->size;
11533
11534 version = read_4_bytes (dbfd, index_ptr);
11535 index_ptr += 4;
11536 if (version == 2)
11537 nr_columns = read_4_bytes (dbfd, index_ptr);
11538 else
11539 nr_columns = 0;
11540 index_ptr += 4;
11541 nr_units = read_4_bytes (dbfd, index_ptr);
11542 index_ptr += 4;
11543 nr_slots = read_4_bytes (dbfd, index_ptr);
11544 index_ptr += 4;
11545
11546 if (version != 1 && version != 2)
11547 {
11548 error (_("Dwarf Error: unsupported DWP file version (%s)"
11549 " [in module %s]"),
11550 pulongest (version), dwp_file->name);
11551 }
11552 if (nr_slots != (nr_slots & -nr_slots))
11553 {
11554 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11555 " is not power of 2 [in module %s]"),
11556 pulongest (nr_slots), dwp_file->name);
11557 }
11558
11559 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
11560 htab->version = version;
11561 htab->nr_columns = nr_columns;
11562 htab->nr_units = nr_units;
11563 htab->nr_slots = nr_slots;
11564 htab->hash_table = index_ptr;
11565 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11566
11567 /* Exit early if the table is empty. */
11568 if (nr_slots == 0 || nr_units == 0
11569 || (version == 2 && nr_columns == 0))
11570 {
11571 /* All must be zero. */
11572 if (nr_slots != 0 || nr_units != 0
11573 || (version == 2 && nr_columns != 0))
11574 {
11575 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11576 " all zero [in modules %s]"),
11577 dwp_file->name);
11578 }
11579 return htab;
11580 }
11581
11582 if (version == 1)
11583 {
11584 htab->section_pool.v1.indices =
11585 htab->unit_table + sizeof (uint32_t) * nr_slots;
11586 /* It's harder to decide whether the section is too small in v1.
11587 V1 is deprecated anyway so we punt. */
11588 }
11589 else
11590 {
11591 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11592 int *ids = htab->section_pool.v2.section_ids;
11593 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11594 /* Reverse map for error checking. */
11595 int ids_seen[DW_SECT_MAX + 1];
11596 int i;
11597
11598 if (nr_columns < 2)
11599 {
11600 error (_("Dwarf Error: bad DWP hash table, too few columns"
11601 " in section table [in module %s]"),
11602 dwp_file->name);
11603 }
11604 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11605 {
11606 error (_("Dwarf Error: bad DWP hash table, too many columns"
11607 " in section table [in module %s]"),
11608 dwp_file->name);
11609 }
11610 memset (ids, 255, sizeof_ids);
11611 memset (ids_seen, 255, sizeof (ids_seen));
11612 for (i = 0; i < nr_columns; ++i)
11613 {
11614 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11615
11616 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11617 {
11618 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11619 " in section table [in module %s]"),
11620 id, dwp_file->name);
11621 }
11622 if (ids_seen[id] != -1)
11623 {
11624 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11625 " id %d in section table [in module %s]"),
11626 id, dwp_file->name);
11627 }
11628 ids_seen[id] = i;
11629 ids[i] = id;
11630 }
11631 /* Must have exactly one info or types section. */
11632 if (((ids_seen[DW_SECT_INFO] != -1)
11633 + (ids_seen[DW_SECT_TYPES] != -1))
11634 != 1)
11635 {
11636 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11637 " DWO info/types section [in module %s]"),
11638 dwp_file->name);
11639 }
11640 /* Must have an abbrev section. */
11641 if (ids_seen[DW_SECT_ABBREV] == -1)
11642 {
11643 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11644 " section [in module %s]"),
11645 dwp_file->name);
11646 }
11647 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11648 htab->section_pool.v2.sizes =
11649 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11650 * nr_units * nr_columns);
11651 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11652 * nr_units * nr_columns))
11653 > index_end)
11654 {
11655 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11656 " [in module %s]"),
11657 dwp_file->name);
11658 }
11659 }
11660
11661 return htab;
11662 }
11663
11664 /* Update SECTIONS with the data from SECTP.
11665
11666 This function is like the other "locate" section routines that are
11667 passed to bfd_map_over_sections, but in this context the sections to
11668 read comes from the DWP V1 hash table, not the full ELF section table.
11669
11670 The result is non-zero for success, or zero if an error was found. */
11671
11672 static int
11673 locate_v1_virtual_dwo_sections (asection *sectp,
11674 struct virtual_v1_dwo_sections *sections)
11675 {
11676 const struct dwop_section_names *names = &dwop_section_names;
11677
11678 if (section_is_p (sectp->name, &names->abbrev_dwo))
11679 {
11680 /* There can be only one. */
11681 if (sections->abbrev.s.section != NULL)
11682 return 0;
11683 sections->abbrev.s.section = sectp;
11684 sections->abbrev.size = bfd_section_size (sectp);
11685 }
11686 else if (section_is_p (sectp->name, &names->info_dwo)
11687 || section_is_p (sectp->name, &names->types_dwo))
11688 {
11689 /* There can be only one. */
11690 if (sections->info_or_types.s.section != NULL)
11691 return 0;
11692 sections->info_or_types.s.section = sectp;
11693 sections->info_or_types.size = bfd_section_size (sectp);
11694 }
11695 else if (section_is_p (sectp->name, &names->line_dwo))
11696 {
11697 /* There can be only one. */
11698 if (sections->line.s.section != NULL)
11699 return 0;
11700 sections->line.s.section = sectp;
11701 sections->line.size = bfd_section_size (sectp);
11702 }
11703 else if (section_is_p (sectp->name, &names->loc_dwo))
11704 {
11705 /* There can be only one. */
11706 if (sections->loc.s.section != NULL)
11707 return 0;
11708 sections->loc.s.section = sectp;
11709 sections->loc.size = bfd_section_size (sectp);
11710 }
11711 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11712 {
11713 /* There can be only one. */
11714 if (sections->macinfo.s.section != NULL)
11715 return 0;
11716 sections->macinfo.s.section = sectp;
11717 sections->macinfo.size = bfd_section_size (sectp);
11718 }
11719 else if (section_is_p (sectp->name, &names->macro_dwo))
11720 {
11721 /* There can be only one. */
11722 if (sections->macro.s.section != NULL)
11723 return 0;
11724 sections->macro.s.section = sectp;
11725 sections->macro.size = bfd_section_size (sectp);
11726 }
11727 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11728 {
11729 /* There can be only one. */
11730 if (sections->str_offsets.s.section != NULL)
11731 return 0;
11732 sections->str_offsets.s.section = sectp;
11733 sections->str_offsets.size = bfd_section_size (sectp);
11734 }
11735 else
11736 {
11737 /* No other kind of section is valid. */
11738 return 0;
11739 }
11740
11741 return 1;
11742 }
11743
11744 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11745 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11746 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11747 This is for DWP version 1 files. */
11748
11749 static struct dwo_unit *
11750 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11751 struct dwp_file *dwp_file,
11752 uint32_t unit_index,
11753 const char *comp_dir,
11754 ULONGEST signature, int is_debug_types)
11755 {
11756 const struct dwp_hash_table *dwp_htab =
11757 is_debug_types ? dwp_file->tus : dwp_file->cus;
11758 bfd *dbfd = dwp_file->dbfd.get ();
11759 const char *kind = is_debug_types ? "TU" : "CU";
11760 struct dwo_file *dwo_file;
11761 struct dwo_unit *dwo_unit;
11762 struct virtual_v1_dwo_sections sections;
11763 void **dwo_file_slot;
11764 int i;
11765
11766 gdb_assert (dwp_file->version == 1);
11767
11768 if (dwarf_read_debug)
11769 {
11770 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11771 kind,
11772 pulongest (unit_index), hex_string (signature),
11773 dwp_file->name);
11774 }
11775
11776 /* Fetch the sections of this DWO unit.
11777 Put a limit on the number of sections we look for so that bad data
11778 doesn't cause us to loop forever. */
11779
11780 #define MAX_NR_V1_DWO_SECTIONS \
11781 (1 /* .debug_info or .debug_types */ \
11782 + 1 /* .debug_abbrev */ \
11783 + 1 /* .debug_line */ \
11784 + 1 /* .debug_loc */ \
11785 + 1 /* .debug_str_offsets */ \
11786 + 1 /* .debug_macro or .debug_macinfo */ \
11787 + 1 /* trailing zero */)
11788
11789 memset (&sections, 0, sizeof (sections));
11790
11791 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11792 {
11793 asection *sectp;
11794 uint32_t section_nr =
11795 read_4_bytes (dbfd,
11796 dwp_htab->section_pool.v1.indices
11797 + (unit_index + i) * sizeof (uint32_t));
11798
11799 if (section_nr == 0)
11800 break;
11801 if (section_nr >= dwp_file->num_sections)
11802 {
11803 error (_("Dwarf Error: bad DWP hash table, section number too large"
11804 " [in module %s]"),
11805 dwp_file->name);
11806 }
11807
11808 sectp = dwp_file->elf_sections[section_nr];
11809 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11810 {
11811 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11812 " [in module %s]"),
11813 dwp_file->name);
11814 }
11815 }
11816
11817 if (i < 2
11818 || sections.info_or_types.empty ()
11819 || sections.abbrev.empty ())
11820 {
11821 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11822 " [in module %s]"),
11823 dwp_file->name);
11824 }
11825 if (i == MAX_NR_V1_DWO_SECTIONS)
11826 {
11827 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11828 " [in module %s]"),
11829 dwp_file->name);
11830 }
11831
11832 /* It's easier for the rest of the code if we fake a struct dwo_file and
11833 have dwo_unit "live" in that. At least for now.
11834
11835 The DWP file can be made up of a random collection of CUs and TUs.
11836 However, for each CU + set of TUs that came from the same original DWO
11837 file, we can combine them back into a virtual DWO file to save space
11838 (fewer struct dwo_file objects to allocate). Remember that for really
11839 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11840
11841 std::string virtual_dwo_name =
11842 string_printf ("virtual-dwo/%d-%d-%d-%d",
11843 sections.abbrev.get_id (),
11844 sections.line.get_id (),
11845 sections.loc.get_id (),
11846 sections.str_offsets.get_id ());
11847 /* Can we use an existing virtual DWO file? */
11848 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11849 virtual_dwo_name.c_str (),
11850 comp_dir);
11851 /* Create one if necessary. */
11852 if (*dwo_file_slot == NULL)
11853 {
11854 if (dwarf_read_debug)
11855 {
11856 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11857 virtual_dwo_name.c_str ());
11858 }
11859 dwo_file = new struct dwo_file;
11860 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
11861 dwo_file->comp_dir = comp_dir;
11862 dwo_file->sections.abbrev = sections.abbrev;
11863 dwo_file->sections.line = sections.line;
11864 dwo_file->sections.loc = sections.loc;
11865 dwo_file->sections.macinfo = sections.macinfo;
11866 dwo_file->sections.macro = sections.macro;
11867 dwo_file->sections.str_offsets = sections.str_offsets;
11868 /* The "str" section is global to the entire DWP file. */
11869 dwo_file->sections.str = dwp_file->sections.str;
11870 /* The info or types section is assigned below to dwo_unit,
11871 there's no need to record it in dwo_file.
11872 Also, we can't simply record type sections in dwo_file because
11873 we record a pointer into the vector in dwo_unit. As we collect more
11874 types we'll grow the vector and eventually have to reallocate space
11875 for it, invalidating all copies of pointers into the previous
11876 contents. */
11877 *dwo_file_slot = dwo_file;
11878 }
11879 else
11880 {
11881 if (dwarf_read_debug)
11882 {
11883 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11884 virtual_dwo_name.c_str ());
11885 }
11886 dwo_file = (struct dwo_file *) *dwo_file_slot;
11887 }
11888
11889 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
11890 dwo_unit->dwo_file = dwo_file;
11891 dwo_unit->signature = signature;
11892 dwo_unit->section =
11893 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11894 *dwo_unit->section = sections.info_or_types;
11895 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11896
11897 return dwo_unit;
11898 }
11899
11900 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11901 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11902 piece within that section used by a TU/CU, return a virtual section
11903 of just that piece. */
11904
11905 static struct dwarf2_section_info
11906 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11907 struct dwarf2_section_info *section,
11908 bfd_size_type offset, bfd_size_type size)
11909 {
11910 struct dwarf2_section_info result;
11911 asection *sectp;
11912
11913 gdb_assert (section != NULL);
11914 gdb_assert (!section->is_virtual);
11915
11916 memset (&result, 0, sizeof (result));
11917 result.s.containing_section = section;
11918 result.is_virtual = true;
11919
11920 if (size == 0)
11921 return result;
11922
11923 sectp = section->get_bfd_section ();
11924
11925 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11926 bounds of the real section. This is a pretty-rare event, so just
11927 flag an error (easier) instead of a warning and trying to cope. */
11928 if (sectp == NULL
11929 || offset + size > bfd_section_size (sectp))
11930 {
11931 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11932 " in section %s [in module %s]"),
11933 sectp ? bfd_section_name (sectp) : "<unknown>",
11934 objfile_name (dwarf2_per_objfile->objfile));
11935 }
11936
11937 result.virtual_offset = offset;
11938 result.size = size;
11939 return result;
11940 }
11941
11942 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11943 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11944 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11945 This is for DWP version 2 files. */
11946
11947 static struct dwo_unit *
11948 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11949 struct dwp_file *dwp_file,
11950 uint32_t unit_index,
11951 const char *comp_dir,
11952 ULONGEST signature, int is_debug_types)
11953 {
11954 const struct dwp_hash_table *dwp_htab =
11955 is_debug_types ? dwp_file->tus : dwp_file->cus;
11956 bfd *dbfd = dwp_file->dbfd.get ();
11957 const char *kind = is_debug_types ? "TU" : "CU";
11958 struct dwo_file *dwo_file;
11959 struct dwo_unit *dwo_unit;
11960 struct virtual_v2_dwo_sections sections;
11961 void **dwo_file_slot;
11962 int i;
11963
11964 gdb_assert (dwp_file->version == 2);
11965
11966 if (dwarf_read_debug)
11967 {
11968 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11969 kind,
11970 pulongest (unit_index), hex_string (signature),
11971 dwp_file->name);
11972 }
11973
11974 /* Fetch the section offsets of this DWO unit. */
11975
11976 memset (&sections, 0, sizeof (sections));
11977
11978 for (i = 0; i < dwp_htab->nr_columns; ++i)
11979 {
11980 uint32_t offset = read_4_bytes (dbfd,
11981 dwp_htab->section_pool.v2.offsets
11982 + (((unit_index - 1) * dwp_htab->nr_columns
11983 + i)
11984 * sizeof (uint32_t)));
11985 uint32_t size = read_4_bytes (dbfd,
11986 dwp_htab->section_pool.v2.sizes
11987 + (((unit_index - 1) * dwp_htab->nr_columns
11988 + i)
11989 * sizeof (uint32_t)));
11990
11991 switch (dwp_htab->section_pool.v2.section_ids[i])
11992 {
11993 case DW_SECT_INFO:
11994 case DW_SECT_TYPES:
11995 sections.info_or_types_offset = offset;
11996 sections.info_or_types_size = size;
11997 break;
11998 case DW_SECT_ABBREV:
11999 sections.abbrev_offset = offset;
12000 sections.abbrev_size = size;
12001 break;
12002 case DW_SECT_LINE:
12003 sections.line_offset = offset;
12004 sections.line_size = size;
12005 break;
12006 case DW_SECT_LOC:
12007 sections.loc_offset = offset;
12008 sections.loc_size = size;
12009 break;
12010 case DW_SECT_STR_OFFSETS:
12011 sections.str_offsets_offset = offset;
12012 sections.str_offsets_size = size;
12013 break;
12014 case DW_SECT_MACINFO:
12015 sections.macinfo_offset = offset;
12016 sections.macinfo_size = size;
12017 break;
12018 case DW_SECT_MACRO:
12019 sections.macro_offset = offset;
12020 sections.macro_size = size;
12021 break;
12022 }
12023 }
12024
12025 /* It's easier for the rest of the code if we fake a struct dwo_file and
12026 have dwo_unit "live" in that. At least for now.
12027
12028 The DWP file can be made up of a random collection of CUs and TUs.
12029 However, for each CU + set of TUs that came from the same original DWO
12030 file, we can combine them back into a virtual DWO file to save space
12031 (fewer struct dwo_file objects to allocate). Remember that for really
12032 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12033
12034 std::string virtual_dwo_name =
12035 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12036 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12037 (long) (sections.line_size ? sections.line_offset : 0),
12038 (long) (sections.loc_size ? sections.loc_offset : 0),
12039 (long) (sections.str_offsets_size
12040 ? sections.str_offsets_offset : 0));
12041 /* Can we use an existing virtual DWO file? */
12042 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12043 virtual_dwo_name.c_str (),
12044 comp_dir);
12045 /* Create one if necessary. */
12046 if (*dwo_file_slot == NULL)
12047 {
12048 if (dwarf_read_debug)
12049 {
12050 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12051 virtual_dwo_name.c_str ());
12052 }
12053 dwo_file = new struct dwo_file;
12054 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
12055 dwo_file->comp_dir = comp_dir;
12056 dwo_file->sections.abbrev =
12057 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12058 sections.abbrev_offset, sections.abbrev_size);
12059 dwo_file->sections.line =
12060 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12061 sections.line_offset, sections.line_size);
12062 dwo_file->sections.loc =
12063 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12064 sections.loc_offset, sections.loc_size);
12065 dwo_file->sections.macinfo =
12066 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12067 sections.macinfo_offset, sections.macinfo_size);
12068 dwo_file->sections.macro =
12069 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12070 sections.macro_offset, sections.macro_size);
12071 dwo_file->sections.str_offsets =
12072 create_dwp_v2_section (dwarf2_per_objfile,
12073 &dwp_file->sections.str_offsets,
12074 sections.str_offsets_offset,
12075 sections.str_offsets_size);
12076 /* The "str" section is global to the entire DWP file. */
12077 dwo_file->sections.str = dwp_file->sections.str;
12078 /* The info or types section is assigned below to dwo_unit,
12079 there's no need to record it in dwo_file.
12080 Also, we can't simply record type sections in dwo_file because
12081 we record a pointer into the vector in dwo_unit. As we collect more
12082 types we'll grow the vector and eventually have to reallocate space
12083 for it, invalidating all copies of pointers into the previous
12084 contents. */
12085 *dwo_file_slot = dwo_file;
12086 }
12087 else
12088 {
12089 if (dwarf_read_debug)
12090 {
12091 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12092 virtual_dwo_name.c_str ());
12093 }
12094 dwo_file = (struct dwo_file *) *dwo_file_slot;
12095 }
12096
12097 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
12098 dwo_unit->dwo_file = dwo_file;
12099 dwo_unit->signature = signature;
12100 dwo_unit->section =
12101 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12102 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12103 is_debug_types
12104 ? &dwp_file->sections.types
12105 : &dwp_file->sections.info,
12106 sections.info_or_types_offset,
12107 sections.info_or_types_size);
12108 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12109
12110 return dwo_unit;
12111 }
12112
12113 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12114 Returns NULL if the signature isn't found. */
12115
12116 static struct dwo_unit *
12117 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12118 struct dwp_file *dwp_file, const char *comp_dir,
12119 ULONGEST signature, int is_debug_types)
12120 {
12121 const struct dwp_hash_table *dwp_htab =
12122 is_debug_types ? dwp_file->tus : dwp_file->cus;
12123 bfd *dbfd = dwp_file->dbfd.get ();
12124 uint32_t mask = dwp_htab->nr_slots - 1;
12125 uint32_t hash = signature & mask;
12126 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12127 unsigned int i;
12128 void **slot;
12129 struct dwo_unit find_dwo_cu;
12130
12131 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12132 find_dwo_cu.signature = signature;
12133 slot = htab_find_slot (is_debug_types
12134 ? dwp_file->loaded_tus.get ()
12135 : dwp_file->loaded_cus.get (),
12136 &find_dwo_cu, INSERT);
12137
12138 if (*slot != NULL)
12139 return (struct dwo_unit *) *slot;
12140
12141 /* Use a for loop so that we don't loop forever on bad debug info. */
12142 for (i = 0; i < dwp_htab->nr_slots; ++i)
12143 {
12144 ULONGEST signature_in_table;
12145
12146 signature_in_table =
12147 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12148 if (signature_in_table == signature)
12149 {
12150 uint32_t unit_index =
12151 read_4_bytes (dbfd,
12152 dwp_htab->unit_table + hash * sizeof (uint32_t));
12153
12154 if (dwp_file->version == 1)
12155 {
12156 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12157 dwp_file, unit_index,
12158 comp_dir, signature,
12159 is_debug_types);
12160 }
12161 else
12162 {
12163 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12164 dwp_file, unit_index,
12165 comp_dir, signature,
12166 is_debug_types);
12167 }
12168 return (struct dwo_unit *) *slot;
12169 }
12170 if (signature_in_table == 0)
12171 return NULL;
12172 hash = (hash + hash2) & mask;
12173 }
12174
12175 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12176 " [in module %s]"),
12177 dwp_file->name);
12178 }
12179
12180 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12181 Open the file specified by FILE_NAME and hand it off to BFD for
12182 preliminary analysis. Return a newly initialized bfd *, which
12183 includes a canonicalized copy of FILE_NAME.
12184 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12185 SEARCH_CWD is true if the current directory is to be searched.
12186 It will be searched before debug-file-directory.
12187 If successful, the file is added to the bfd include table of the
12188 objfile's bfd (see gdb_bfd_record_inclusion).
12189 If unable to find/open the file, return NULL.
12190 NOTE: This function is derived from symfile_bfd_open. */
12191
12192 static gdb_bfd_ref_ptr
12193 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12194 const char *file_name, int is_dwp, int search_cwd)
12195 {
12196 int desc;
12197 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12198 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12199 to debug_file_directory. */
12200 const char *search_path;
12201 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12202
12203 gdb::unique_xmalloc_ptr<char> search_path_holder;
12204 if (search_cwd)
12205 {
12206 if (*debug_file_directory != '\0')
12207 {
12208 search_path_holder.reset (concat (".", dirname_separator_string,
12209 debug_file_directory,
12210 (char *) NULL));
12211 search_path = search_path_holder.get ();
12212 }
12213 else
12214 search_path = ".";
12215 }
12216 else
12217 search_path = debug_file_directory;
12218
12219 openp_flags flags = OPF_RETURN_REALPATH;
12220 if (is_dwp)
12221 flags |= OPF_SEARCH_IN_PATH;
12222
12223 gdb::unique_xmalloc_ptr<char> absolute_name;
12224 desc = openp (search_path, flags, file_name,
12225 O_RDONLY | O_BINARY, &absolute_name);
12226 if (desc < 0)
12227 return NULL;
12228
12229 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12230 gnutarget, desc));
12231 if (sym_bfd == NULL)
12232 return NULL;
12233 bfd_set_cacheable (sym_bfd.get (), 1);
12234
12235 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12236 return NULL;
12237
12238 /* Success. Record the bfd as having been included by the objfile's bfd.
12239 This is important because things like demangled_names_hash lives in the
12240 objfile's per_bfd space and may have references to things like symbol
12241 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12242 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12243
12244 return sym_bfd;
12245 }
12246
12247 /* Try to open DWO file FILE_NAME.
12248 COMP_DIR is the DW_AT_comp_dir attribute.
12249 The result is the bfd handle of the file.
12250 If there is a problem finding or opening the file, return NULL.
12251 Upon success, the canonicalized path of the file is stored in the bfd,
12252 same as symfile_bfd_open. */
12253
12254 static gdb_bfd_ref_ptr
12255 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12256 const char *file_name, const char *comp_dir)
12257 {
12258 if (IS_ABSOLUTE_PATH (file_name))
12259 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12260 0 /*is_dwp*/, 0 /*search_cwd*/);
12261
12262 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12263
12264 if (comp_dir != NULL)
12265 {
12266 gdb::unique_xmalloc_ptr<char> path_to_try
12267 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12268
12269 /* NOTE: If comp_dir is a relative path, this will also try the
12270 search path, which seems useful. */
12271 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12272 path_to_try.get (),
12273 0 /*is_dwp*/,
12274 1 /*search_cwd*/));
12275 if (abfd != NULL)
12276 return abfd;
12277 }
12278
12279 /* That didn't work, try debug-file-directory, which, despite its name,
12280 is a list of paths. */
12281
12282 if (*debug_file_directory == '\0')
12283 return NULL;
12284
12285 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12286 0 /*is_dwp*/, 1 /*search_cwd*/);
12287 }
12288
12289 /* This function is mapped across the sections and remembers the offset and
12290 size of each of the DWO debugging sections we are interested in. */
12291
12292 static void
12293 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12294 {
12295 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12296 const struct dwop_section_names *names = &dwop_section_names;
12297
12298 if (section_is_p (sectp->name, &names->abbrev_dwo))
12299 {
12300 dwo_sections->abbrev.s.section = sectp;
12301 dwo_sections->abbrev.size = bfd_section_size (sectp);
12302 }
12303 else if (section_is_p (sectp->name, &names->info_dwo))
12304 {
12305 dwo_sections->info.s.section = sectp;
12306 dwo_sections->info.size = bfd_section_size (sectp);
12307 }
12308 else if (section_is_p (sectp->name, &names->line_dwo))
12309 {
12310 dwo_sections->line.s.section = sectp;
12311 dwo_sections->line.size = bfd_section_size (sectp);
12312 }
12313 else if (section_is_p (sectp->name, &names->loc_dwo))
12314 {
12315 dwo_sections->loc.s.section = sectp;
12316 dwo_sections->loc.size = bfd_section_size (sectp);
12317 }
12318 else if (section_is_p (sectp->name, &names->loclists_dwo))
12319 {
12320 dwo_sections->loclists.s.section = sectp;
12321 dwo_sections->loclists.size = bfd_section_size (sectp);
12322 }
12323 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12324 {
12325 dwo_sections->macinfo.s.section = sectp;
12326 dwo_sections->macinfo.size = bfd_section_size (sectp);
12327 }
12328 else if (section_is_p (sectp->name, &names->macro_dwo))
12329 {
12330 dwo_sections->macro.s.section = sectp;
12331 dwo_sections->macro.size = bfd_section_size (sectp);
12332 }
12333 else if (section_is_p (sectp->name, &names->str_dwo))
12334 {
12335 dwo_sections->str.s.section = sectp;
12336 dwo_sections->str.size = bfd_section_size (sectp);
12337 }
12338 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12339 {
12340 dwo_sections->str_offsets.s.section = sectp;
12341 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12342 }
12343 else if (section_is_p (sectp->name, &names->types_dwo))
12344 {
12345 struct dwarf2_section_info type_section;
12346
12347 memset (&type_section, 0, sizeof (type_section));
12348 type_section.s.section = sectp;
12349 type_section.size = bfd_section_size (sectp);
12350 dwo_sections->types.push_back (type_section);
12351 }
12352 }
12353
12354 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12355 by PER_CU. This is for the non-DWP case.
12356 The result is NULL if DWO_NAME can't be found. */
12357
12358 static struct dwo_file *
12359 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12360 const char *comp_dir)
12361 {
12362 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
12363
12364 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12365 if (dbfd == NULL)
12366 {
12367 if (dwarf_read_debug)
12368 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12369 return NULL;
12370 }
12371
12372 dwo_file_up dwo_file (new struct dwo_file);
12373 dwo_file->dwo_name = dwo_name;
12374 dwo_file->comp_dir = comp_dir;
12375 dwo_file->dbfd = std::move (dbfd);
12376
12377 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12378 &dwo_file->sections);
12379
12380 create_cus_hash_table (dwarf2_per_objfile, cu, *dwo_file,
12381 dwo_file->sections.info, dwo_file->cus);
12382
12383 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12384 dwo_file->sections.types, dwo_file->tus);
12385
12386 if (dwarf_read_debug)
12387 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12388
12389 return dwo_file.release ();
12390 }
12391
12392 /* This function is mapped across the sections and remembers the offset and
12393 size of each of the DWP debugging sections common to version 1 and 2 that
12394 we are interested in. */
12395
12396 static void
12397 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12398 void *dwp_file_ptr)
12399 {
12400 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12401 const struct dwop_section_names *names = &dwop_section_names;
12402 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12403
12404 /* Record the ELF section number for later lookup: this is what the
12405 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12406 gdb_assert (elf_section_nr < dwp_file->num_sections);
12407 dwp_file->elf_sections[elf_section_nr] = sectp;
12408
12409 /* Look for specific sections that we need. */
12410 if (section_is_p (sectp->name, &names->str_dwo))
12411 {
12412 dwp_file->sections.str.s.section = sectp;
12413 dwp_file->sections.str.size = bfd_section_size (sectp);
12414 }
12415 else if (section_is_p (sectp->name, &names->cu_index))
12416 {
12417 dwp_file->sections.cu_index.s.section = sectp;
12418 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12419 }
12420 else if (section_is_p (sectp->name, &names->tu_index))
12421 {
12422 dwp_file->sections.tu_index.s.section = sectp;
12423 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12424 }
12425 }
12426
12427 /* This function is mapped across the sections and remembers the offset and
12428 size of each of the DWP version 2 debugging sections that we are interested
12429 in. This is split into a separate function because we don't know if we
12430 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12431
12432 static void
12433 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12434 {
12435 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12436 const struct dwop_section_names *names = &dwop_section_names;
12437 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12438
12439 /* Record the ELF section number for later lookup: this is what the
12440 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12441 gdb_assert (elf_section_nr < dwp_file->num_sections);
12442 dwp_file->elf_sections[elf_section_nr] = sectp;
12443
12444 /* Look for specific sections that we need. */
12445 if (section_is_p (sectp->name, &names->abbrev_dwo))
12446 {
12447 dwp_file->sections.abbrev.s.section = sectp;
12448 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12449 }
12450 else if (section_is_p (sectp->name, &names->info_dwo))
12451 {
12452 dwp_file->sections.info.s.section = sectp;
12453 dwp_file->sections.info.size = bfd_section_size (sectp);
12454 }
12455 else if (section_is_p (sectp->name, &names->line_dwo))
12456 {
12457 dwp_file->sections.line.s.section = sectp;
12458 dwp_file->sections.line.size = bfd_section_size (sectp);
12459 }
12460 else if (section_is_p (sectp->name, &names->loc_dwo))
12461 {
12462 dwp_file->sections.loc.s.section = sectp;
12463 dwp_file->sections.loc.size = bfd_section_size (sectp);
12464 }
12465 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12466 {
12467 dwp_file->sections.macinfo.s.section = sectp;
12468 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12469 }
12470 else if (section_is_p (sectp->name, &names->macro_dwo))
12471 {
12472 dwp_file->sections.macro.s.section = sectp;
12473 dwp_file->sections.macro.size = bfd_section_size (sectp);
12474 }
12475 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12476 {
12477 dwp_file->sections.str_offsets.s.section = sectp;
12478 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12479 }
12480 else if (section_is_p (sectp->name, &names->types_dwo))
12481 {
12482 dwp_file->sections.types.s.section = sectp;
12483 dwp_file->sections.types.size = bfd_section_size (sectp);
12484 }
12485 }
12486
12487 /* Hash function for dwp_file loaded CUs/TUs. */
12488
12489 static hashval_t
12490 hash_dwp_loaded_cutus (const void *item)
12491 {
12492 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12493
12494 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12495 return dwo_unit->signature;
12496 }
12497
12498 /* Equality function for dwp_file loaded CUs/TUs. */
12499
12500 static int
12501 eq_dwp_loaded_cutus (const void *a, const void *b)
12502 {
12503 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12504 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12505
12506 return dua->signature == dub->signature;
12507 }
12508
12509 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12510
12511 static htab_up
12512 allocate_dwp_loaded_cutus_table ()
12513 {
12514 return htab_up (htab_create_alloc (3,
12515 hash_dwp_loaded_cutus,
12516 eq_dwp_loaded_cutus,
12517 NULL, xcalloc, xfree));
12518 }
12519
12520 /* Try to open DWP file FILE_NAME.
12521 The result is the bfd handle of the file.
12522 If there is a problem finding or opening the file, return NULL.
12523 Upon success, the canonicalized path of the file is stored in the bfd,
12524 same as symfile_bfd_open. */
12525
12526 static gdb_bfd_ref_ptr
12527 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12528 const char *file_name)
12529 {
12530 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12531 1 /*is_dwp*/,
12532 1 /*search_cwd*/));
12533 if (abfd != NULL)
12534 return abfd;
12535
12536 /* Work around upstream bug 15652.
12537 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12538 [Whether that's a "bug" is debatable, but it is getting in our way.]
12539 We have no real idea where the dwp file is, because gdb's realpath-ing
12540 of the executable's path may have discarded the needed info.
12541 [IWBN if the dwp file name was recorded in the executable, akin to
12542 .gnu_debuglink, but that doesn't exist yet.]
12543 Strip the directory from FILE_NAME and search again. */
12544 if (*debug_file_directory != '\0')
12545 {
12546 /* Don't implicitly search the current directory here.
12547 If the user wants to search "." to handle this case,
12548 it must be added to debug-file-directory. */
12549 return try_open_dwop_file (dwarf2_per_objfile,
12550 lbasename (file_name), 1 /*is_dwp*/,
12551 0 /*search_cwd*/);
12552 }
12553
12554 return NULL;
12555 }
12556
12557 /* Initialize the use of the DWP file for the current objfile.
12558 By convention the name of the DWP file is ${objfile}.dwp.
12559 The result is NULL if it can't be found. */
12560
12561 static std::unique_ptr<struct dwp_file>
12562 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12563 {
12564 struct objfile *objfile = dwarf2_per_objfile->objfile;
12565
12566 /* Try to find first .dwp for the binary file before any symbolic links
12567 resolving. */
12568
12569 /* If the objfile is a debug file, find the name of the real binary
12570 file and get the name of dwp file from there. */
12571 std::string dwp_name;
12572 if (objfile->separate_debug_objfile_backlink != NULL)
12573 {
12574 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12575 const char *backlink_basename = lbasename (backlink->original_name);
12576
12577 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12578 }
12579 else
12580 dwp_name = objfile->original_name;
12581
12582 dwp_name += ".dwp";
12583
12584 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12585 if (dbfd == NULL
12586 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12587 {
12588 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12589 dwp_name = objfile_name (objfile);
12590 dwp_name += ".dwp";
12591 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12592 }
12593
12594 if (dbfd == NULL)
12595 {
12596 if (dwarf_read_debug)
12597 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12598 return std::unique_ptr<dwp_file> ();
12599 }
12600
12601 const char *name = bfd_get_filename (dbfd.get ());
12602 std::unique_ptr<struct dwp_file> dwp_file
12603 (new struct dwp_file (name, std::move (dbfd)));
12604
12605 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12606 dwp_file->elf_sections =
12607 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
12608 dwp_file->num_sections, asection *);
12609
12610 bfd_map_over_sections (dwp_file->dbfd.get (),
12611 dwarf2_locate_common_dwp_sections,
12612 dwp_file.get ());
12613
12614 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12615 0);
12616
12617 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12618 1);
12619
12620 /* The DWP file version is stored in the hash table. Oh well. */
12621 if (dwp_file->cus && dwp_file->tus
12622 && dwp_file->cus->version != dwp_file->tus->version)
12623 {
12624 /* Technically speaking, we should try to limp along, but this is
12625 pretty bizarre. We use pulongest here because that's the established
12626 portability solution (e.g, we cannot use %u for uint32_t). */
12627 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12628 " TU version %s [in DWP file %s]"),
12629 pulongest (dwp_file->cus->version),
12630 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12631 }
12632
12633 if (dwp_file->cus)
12634 dwp_file->version = dwp_file->cus->version;
12635 else if (dwp_file->tus)
12636 dwp_file->version = dwp_file->tus->version;
12637 else
12638 dwp_file->version = 2;
12639
12640 if (dwp_file->version == 2)
12641 bfd_map_over_sections (dwp_file->dbfd.get (),
12642 dwarf2_locate_v2_dwp_sections,
12643 dwp_file.get ());
12644
12645 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12646 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12647
12648 if (dwarf_read_debug)
12649 {
12650 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12651 fprintf_unfiltered (gdb_stdlog,
12652 " %s CUs, %s TUs\n",
12653 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12654 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12655 }
12656
12657 return dwp_file;
12658 }
12659
12660 /* Wrapper around open_and_init_dwp_file, only open it once. */
12661
12662 static struct dwp_file *
12663 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12664 {
12665 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
12666 {
12667 dwarf2_per_objfile->per_bfd->dwp_file
12668 = open_and_init_dwp_file (dwarf2_per_objfile);
12669 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
12670 }
12671 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
12672 }
12673
12674 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12675 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12676 or in the DWP file for the objfile, referenced by THIS_UNIT.
12677 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12678 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12679
12680 This is called, for example, when wanting to read a variable with a
12681 complex location. Therefore we don't want to do file i/o for every call.
12682 Therefore we don't want to look for a DWO file on every call.
12683 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12684 then we check if we've already seen DWO_NAME, and only THEN do we check
12685 for a DWO file.
12686
12687 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12688 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12689
12690 static struct dwo_unit *
12691 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12692 ULONGEST signature, int is_debug_types)
12693 {
12694 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
12695 struct objfile *objfile = dwarf2_per_objfile->objfile;
12696 const char *kind = is_debug_types ? "TU" : "CU";
12697 void **dwo_file_slot;
12698 struct dwo_file *dwo_file;
12699 struct dwp_file *dwp_file;
12700
12701 /* First see if there's a DWP file.
12702 If we have a DWP file but didn't find the DWO inside it, don't
12703 look for the original DWO file. It makes gdb behave differently
12704 depending on whether one is debugging in the build tree. */
12705
12706 dwp_file = get_dwp_file (dwarf2_per_objfile);
12707 if (dwp_file != NULL)
12708 {
12709 const struct dwp_hash_table *dwp_htab =
12710 is_debug_types ? dwp_file->tus : dwp_file->cus;
12711
12712 if (dwp_htab != NULL)
12713 {
12714 struct dwo_unit *dwo_cutu =
12715 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12716 signature, is_debug_types);
12717
12718 if (dwo_cutu != NULL)
12719 {
12720 if (dwarf_read_debug)
12721 {
12722 fprintf_unfiltered (gdb_stdlog,
12723 "Virtual DWO %s %s found: @%s\n",
12724 kind, hex_string (signature),
12725 host_address_to_string (dwo_cutu));
12726 }
12727 return dwo_cutu;
12728 }
12729 }
12730 }
12731 else
12732 {
12733 /* No DWP file, look for the DWO file. */
12734
12735 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12736 dwo_name, comp_dir);
12737 if (*dwo_file_slot == NULL)
12738 {
12739 /* Read in the file and build a table of the CUs/TUs it contains. */
12740 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12741 }
12742 /* NOTE: This will be NULL if unable to open the file. */
12743 dwo_file = (struct dwo_file *) *dwo_file_slot;
12744
12745 if (dwo_file != NULL)
12746 {
12747 struct dwo_unit *dwo_cutu = NULL;
12748
12749 if (is_debug_types && dwo_file->tus)
12750 {
12751 struct dwo_unit find_dwo_cutu;
12752
12753 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12754 find_dwo_cutu.signature = signature;
12755 dwo_cutu
12756 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12757 &find_dwo_cutu);
12758 }
12759 else if (!is_debug_types && dwo_file->cus)
12760 {
12761 struct dwo_unit find_dwo_cutu;
12762
12763 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12764 find_dwo_cutu.signature = signature;
12765 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12766 &find_dwo_cutu);
12767 }
12768
12769 if (dwo_cutu != NULL)
12770 {
12771 if (dwarf_read_debug)
12772 {
12773 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12774 kind, dwo_name, hex_string (signature),
12775 host_address_to_string (dwo_cutu));
12776 }
12777 return dwo_cutu;
12778 }
12779 }
12780 }
12781
12782 /* We didn't find it. This could mean a dwo_id mismatch, or
12783 someone deleted the DWO/DWP file, or the search path isn't set up
12784 correctly to find the file. */
12785
12786 if (dwarf_read_debug)
12787 {
12788 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12789 kind, dwo_name, hex_string (signature));
12790 }
12791
12792 /* This is a warning and not a complaint because it can be caused by
12793 pilot error (e.g., user accidentally deleting the DWO). */
12794 {
12795 /* Print the name of the DWP file if we looked there, helps the user
12796 better diagnose the problem. */
12797 std::string dwp_text;
12798
12799 if (dwp_file != NULL)
12800 dwp_text = string_printf (" [in DWP file %s]",
12801 lbasename (dwp_file->name));
12802
12803 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12804 " [in module %s]"),
12805 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12806 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12807 }
12808 return NULL;
12809 }
12810
12811 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12812 See lookup_dwo_cutu_unit for details. */
12813
12814 static struct dwo_unit *
12815 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12816 ULONGEST signature)
12817 {
12818 gdb_assert (!cu->per_cu->is_debug_types);
12819
12820 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12821 }
12822
12823 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12824 See lookup_dwo_cutu_unit for details. */
12825
12826 static struct dwo_unit *
12827 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12828 {
12829 gdb_assert (cu->per_cu->is_debug_types);
12830
12831 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12832
12833 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12834 }
12835
12836 /* Traversal function for queue_and_load_all_dwo_tus. */
12837
12838 static int
12839 queue_and_load_dwo_tu (void **slot, void *info)
12840 {
12841 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12842 dwarf2_cu *cu = (dwarf2_cu *) info;
12843 ULONGEST signature = dwo_unit->signature;
12844 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12845
12846 if (sig_type != NULL)
12847 {
12848 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12849
12850 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12851 a real dependency of PER_CU on SIG_TYPE. That is detected later
12852 while processing PER_CU. */
12853 if (maybe_queue_comp_unit (NULL, sig_cu, cu->language))
12854 load_full_type_unit (sig_cu, cu->per_objfile);
12855 cu->per_cu->imported_symtabs_push (sig_cu);
12856 }
12857
12858 return 1;
12859 }
12860
12861 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12862 The DWO may have the only definition of the type, though it may not be
12863 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12864 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12865
12866 static void
12867 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12868 {
12869 struct dwo_unit *dwo_unit;
12870 struct dwo_file *dwo_file;
12871
12872 gdb_assert (!per_cu->is_debug_types);
12873 gdb_assert (per_cu->cu != NULL);
12874 gdb_assert (get_dwp_file (per_cu->cu->per_objfile) == NULL);
12875
12876 dwo_unit = per_cu->cu->dwo_unit;
12877 gdb_assert (dwo_unit != NULL);
12878
12879 dwo_file = dwo_unit->dwo_file;
12880 if (dwo_file->tus != NULL)
12881 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12882 per_cu->cu);
12883 }
12884
12885 /* Read in various DIEs. */
12886
12887 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12888 Inherit only the children of the DW_AT_abstract_origin DIE not being
12889 already referenced by DW_AT_abstract_origin from the children of the
12890 current DIE. */
12891
12892 static void
12893 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12894 {
12895 struct die_info *child_die;
12896 sect_offset *offsetp;
12897 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12898 struct die_info *origin_die;
12899 /* Iterator of the ORIGIN_DIE children. */
12900 struct die_info *origin_child_die;
12901 struct attribute *attr;
12902 struct dwarf2_cu *origin_cu;
12903 struct pending **origin_previous_list_in_scope;
12904
12905 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12906 if (!attr)
12907 return;
12908
12909 /* Note that following die references may follow to a die in a
12910 different cu. */
12911
12912 origin_cu = cu;
12913 origin_die = follow_die_ref (die, attr, &origin_cu);
12914
12915 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12916 symbols in. */
12917 origin_previous_list_in_scope = origin_cu->list_in_scope;
12918 origin_cu->list_in_scope = cu->list_in_scope;
12919
12920 if (die->tag != origin_die->tag
12921 && !(die->tag == DW_TAG_inlined_subroutine
12922 && origin_die->tag == DW_TAG_subprogram))
12923 complaint (_("DIE %s and its abstract origin %s have different tags"),
12924 sect_offset_str (die->sect_off),
12925 sect_offset_str (origin_die->sect_off));
12926
12927 std::vector<sect_offset> offsets;
12928
12929 for (child_die = die->child;
12930 child_die && child_die->tag;
12931 child_die = child_die->sibling)
12932 {
12933 struct die_info *child_origin_die;
12934 struct dwarf2_cu *child_origin_cu;
12935
12936 /* We are trying to process concrete instance entries:
12937 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12938 it's not relevant to our analysis here. i.e. detecting DIEs that are
12939 present in the abstract instance but not referenced in the concrete
12940 one. */
12941 if (child_die->tag == DW_TAG_call_site
12942 || child_die->tag == DW_TAG_GNU_call_site)
12943 continue;
12944
12945 /* For each CHILD_DIE, find the corresponding child of
12946 ORIGIN_DIE. If there is more than one layer of
12947 DW_AT_abstract_origin, follow them all; there shouldn't be,
12948 but GCC versions at least through 4.4 generate this (GCC PR
12949 40573). */
12950 child_origin_die = child_die;
12951 child_origin_cu = cu;
12952 while (1)
12953 {
12954 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12955 child_origin_cu);
12956 if (attr == NULL)
12957 break;
12958 child_origin_die = follow_die_ref (child_origin_die, attr,
12959 &child_origin_cu);
12960 }
12961
12962 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12963 counterpart may exist. */
12964 if (child_origin_die != child_die)
12965 {
12966 if (child_die->tag != child_origin_die->tag
12967 && !(child_die->tag == DW_TAG_inlined_subroutine
12968 && child_origin_die->tag == DW_TAG_subprogram))
12969 complaint (_("Child DIE %s and its abstract origin %s have "
12970 "different tags"),
12971 sect_offset_str (child_die->sect_off),
12972 sect_offset_str (child_origin_die->sect_off));
12973 if (child_origin_die->parent != origin_die)
12974 complaint (_("Child DIE %s and its abstract origin %s have "
12975 "different parents"),
12976 sect_offset_str (child_die->sect_off),
12977 sect_offset_str (child_origin_die->sect_off));
12978 else
12979 offsets.push_back (child_origin_die->sect_off);
12980 }
12981 }
12982 std::sort (offsets.begin (), offsets.end ());
12983 sect_offset *offsets_end = offsets.data () + offsets.size ();
12984 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12985 if (offsetp[-1] == *offsetp)
12986 complaint (_("Multiple children of DIE %s refer "
12987 "to DIE %s as their abstract origin"),
12988 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12989
12990 offsetp = offsets.data ();
12991 origin_child_die = origin_die->child;
12992 while (origin_child_die && origin_child_die->tag)
12993 {
12994 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12995 while (offsetp < offsets_end
12996 && *offsetp < origin_child_die->sect_off)
12997 offsetp++;
12998 if (offsetp >= offsets_end
12999 || *offsetp > origin_child_die->sect_off)
13000 {
13001 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13002 Check whether we're already processing ORIGIN_CHILD_DIE.
13003 This can happen with mutually referenced abstract_origins.
13004 PR 16581. */
13005 if (!origin_child_die->in_process)
13006 process_die (origin_child_die, origin_cu);
13007 }
13008 origin_child_die = origin_child_die->sibling;
13009 }
13010 origin_cu->list_in_scope = origin_previous_list_in_scope;
13011
13012 if (cu != origin_cu)
13013 compute_delayed_physnames (origin_cu);
13014 }
13015
13016 static void
13017 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13018 {
13019 struct objfile *objfile = cu->per_objfile->objfile;
13020 struct gdbarch *gdbarch = objfile->arch ();
13021 struct context_stack *newobj;
13022 CORE_ADDR lowpc;
13023 CORE_ADDR highpc;
13024 struct die_info *child_die;
13025 struct attribute *attr, *call_line, *call_file;
13026 const char *name;
13027 CORE_ADDR baseaddr;
13028 struct block *block;
13029 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13030 std::vector<struct symbol *> template_args;
13031 struct template_symbol *templ_func = NULL;
13032
13033 if (inlined_func)
13034 {
13035 /* If we do not have call site information, we can't show the
13036 caller of this inlined function. That's too confusing, so
13037 only use the scope for local variables. */
13038 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13039 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13040 if (call_line == NULL || call_file == NULL)
13041 {
13042 read_lexical_block_scope (die, cu);
13043 return;
13044 }
13045 }
13046
13047 baseaddr = objfile->text_section_offset ();
13048
13049 name = dwarf2_name (die, cu);
13050
13051 /* Ignore functions with missing or empty names. These are actually
13052 illegal according to the DWARF standard. */
13053 if (name == NULL)
13054 {
13055 complaint (_("missing name for subprogram DIE at %s"),
13056 sect_offset_str (die->sect_off));
13057 return;
13058 }
13059
13060 /* Ignore functions with missing or invalid low and high pc attributes. */
13061 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13062 <= PC_BOUNDS_INVALID)
13063 {
13064 attr = dwarf2_attr (die, DW_AT_external, cu);
13065 if (!attr || !DW_UNSND (attr))
13066 complaint (_("cannot get low and high bounds "
13067 "for subprogram DIE at %s"),
13068 sect_offset_str (die->sect_off));
13069 return;
13070 }
13071
13072 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13073 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13074
13075 /* If we have any template arguments, then we must allocate a
13076 different sort of symbol. */
13077 for (child_die = die->child; child_die; child_die = child_die->sibling)
13078 {
13079 if (child_die->tag == DW_TAG_template_type_param
13080 || child_die->tag == DW_TAG_template_value_param)
13081 {
13082 templ_func = new (&objfile->objfile_obstack) template_symbol;
13083 templ_func->subclass = SYMBOL_TEMPLATE;
13084 break;
13085 }
13086 }
13087
13088 newobj = cu->get_builder ()->push_context (0, lowpc);
13089 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13090 (struct symbol *) templ_func);
13091
13092 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13093 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13094 cu->language);
13095
13096 /* If there is a location expression for DW_AT_frame_base, record
13097 it. */
13098 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13099 if (attr != nullptr)
13100 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13101
13102 /* If there is a location for the static link, record it. */
13103 newobj->static_link = NULL;
13104 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13105 if (attr != nullptr)
13106 {
13107 newobj->static_link
13108 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13109 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13110 cu->per_cu->addr_type ());
13111 }
13112
13113 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13114
13115 if (die->child != NULL)
13116 {
13117 child_die = die->child;
13118 while (child_die && child_die->tag)
13119 {
13120 if (child_die->tag == DW_TAG_template_type_param
13121 || child_die->tag == DW_TAG_template_value_param)
13122 {
13123 struct symbol *arg = new_symbol (child_die, NULL, cu);
13124
13125 if (arg != NULL)
13126 template_args.push_back (arg);
13127 }
13128 else
13129 process_die (child_die, cu);
13130 child_die = child_die->sibling;
13131 }
13132 }
13133
13134 inherit_abstract_dies (die, cu);
13135
13136 /* If we have a DW_AT_specification, we might need to import using
13137 directives from the context of the specification DIE. See the
13138 comment in determine_prefix. */
13139 if (cu->language == language_cplus
13140 && dwarf2_attr (die, DW_AT_specification, cu))
13141 {
13142 struct dwarf2_cu *spec_cu = cu;
13143 struct die_info *spec_die = die_specification (die, &spec_cu);
13144
13145 while (spec_die)
13146 {
13147 child_die = spec_die->child;
13148 while (child_die && child_die->tag)
13149 {
13150 if (child_die->tag == DW_TAG_imported_module)
13151 process_die (child_die, spec_cu);
13152 child_die = child_die->sibling;
13153 }
13154
13155 /* In some cases, GCC generates specification DIEs that
13156 themselves contain DW_AT_specification attributes. */
13157 spec_die = die_specification (spec_die, &spec_cu);
13158 }
13159 }
13160
13161 struct context_stack cstk = cu->get_builder ()->pop_context ();
13162 /* Make a block for the local symbols within. */
13163 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13164 cstk.static_link, lowpc, highpc);
13165
13166 /* For C++, set the block's scope. */
13167 if ((cu->language == language_cplus
13168 || cu->language == language_fortran
13169 || cu->language == language_d
13170 || cu->language == language_rust)
13171 && cu->processing_has_namespace_info)
13172 block_set_scope (block, determine_prefix (die, cu),
13173 &objfile->objfile_obstack);
13174
13175 /* If we have address ranges, record them. */
13176 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13177
13178 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13179
13180 /* Attach template arguments to function. */
13181 if (!template_args.empty ())
13182 {
13183 gdb_assert (templ_func != NULL);
13184
13185 templ_func->n_template_arguments = template_args.size ();
13186 templ_func->template_arguments
13187 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13188 templ_func->n_template_arguments);
13189 memcpy (templ_func->template_arguments,
13190 template_args.data (),
13191 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13192
13193 /* Make sure that the symtab is set on the new symbols. Even
13194 though they don't appear in this symtab directly, other parts
13195 of gdb assume that symbols do, and this is reasonably
13196 true. */
13197 for (symbol *sym : template_args)
13198 symbol_set_symtab (sym, symbol_symtab (templ_func));
13199 }
13200
13201 /* In C++, we can have functions nested inside functions (e.g., when
13202 a function declares a class that has methods). This means that
13203 when we finish processing a function scope, we may need to go
13204 back to building a containing block's symbol lists. */
13205 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13206 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13207
13208 /* If we've finished processing a top-level function, subsequent
13209 symbols go in the file symbol list. */
13210 if (cu->get_builder ()->outermost_context_p ())
13211 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13212 }
13213
13214 /* Process all the DIES contained within a lexical block scope. Start
13215 a new scope, process the dies, and then close the scope. */
13216
13217 static void
13218 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13219 {
13220 struct objfile *objfile = cu->per_objfile->objfile;
13221 struct gdbarch *gdbarch = objfile->arch ();
13222 CORE_ADDR lowpc, highpc;
13223 struct die_info *child_die;
13224 CORE_ADDR baseaddr;
13225
13226 baseaddr = objfile->text_section_offset ();
13227
13228 /* Ignore blocks with missing or invalid low and high pc attributes. */
13229 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13230 as multiple lexical blocks? Handling children in a sane way would
13231 be nasty. Might be easier to properly extend generic blocks to
13232 describe ranges. */
13233 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13234 {
13235 case PC_BOUNDS_NOT_PRESENT:
13236 /* DW_TAG_lexical_block has no attributes, process its children as if
13237 there was no wrapping by that DW_TAG_lexical_block.
13238 GCC does no longer produces such DWARF since GCC r224161. */
13239 for (child_die = die->child;
13240 child_die != NULL && child_die->tag;
13241 child_die = child_die->sibling)
13242 {
13243 /* We might already be processing this DIE. This can happen
13244 in an unusual circumstance -- where a subroutine A
13245 appears lexically in another subroutine B, but A actually
13246 inlines B. The recursion is broken here, rather than in
13247 inherit_abstract_dies, because it seems better to simply
13248 drop concrete children here. */
13249 if (!child_die->in_process)
13250 process_die (child_die, cu);
13251 }
13252 return;
13253 case PC_BOUNDS_INVALID:
13254 return;
13255 }
13256 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13257 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13258
13259 cu->get_builder ()->push_context (0, lowpc);
13260 if (die->child != NULL)
13261 {
13262 child_die = die->child;
13263 while (child_die && child_die->tag)
13264 {
13265 process_die (child_die, cu);
13266 child_die = child_die->sibling;
13267 }
13268 }
13269 inherit_abstract_dies (die, cu);
13270 struct context_stack cstk = cu->get_builder ()->pop_context ();
13271
13272 if (*cu->get_builder ()->get_local_symbols () != NULL
13273 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13274 {
13275 struct block *block
13276 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13277 cstk.start_addr, highpc);
13278
13279 /* Note that recording ranges after traversing children, as we
13280 do here, means that recording a parent's ranges entails
13281 walking across all its children's ranges as they appear in
13282 the address map, which is quadratic behavior.
13283
13284 It would be nicer to record the parent's ranges before
13285 traversing its children, simply overriding whatever you find
13286 there. But since we don't even decide whether to create a
13287 block until after we've traversed its children, that's hard
13288 to do. */
13289 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13290 }
13291 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13292 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13293 }
13294
13295 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13296
13297 static void
13298 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13299 {
13300 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13301 struct objfile *objfile = per_objfile->objfile;
13302 struct gdbarch *gdbarch = objfile->arch ();
13303 CORE_ADDR pc, baseaddr;
13304 struct attribute *attr;
13305 struct call_site *call_site, call_site_local;
13306 void **slot;
13307 int nparams;
13308 struct die_info *child_die;
13309
13310 baseaddr = objfile->text_section_offset ();
13311
13312 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13313 if (attr == NULL)
13314 {
13315 /* This was a pre-DWARF-5 GNU extension alias
13316 for DW_AT_call_return_pc. */
13317 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13318 }
13319 if (!attr)
13320 {
13321 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13322 "DIE %s [in module %s]"),
13323 sect_offset_str (die->sect_off), objfile_name (objfile));
13324 return;
13325 }
13326 pc = attr->value_as_address () + baseaddr;
13327 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13328
13329 if (cu->call_site_htab == NULL)
13330 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13331 NULL, &objfile->objfile_obstack,
13332 hashtab_obstack_allocate, NULL);
13333 call_site_local.pc = pc;
13334 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13335 if (*slot != NULL)
13336 {
13337 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13338 "DIE %s [in module %s]"),
13339 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13340 objfile_name (objfile));
13341 return;
13342 }
13343
13344 /* Count parameters at the caller. */
13345
13346 nparams = 0;
13347 for (child_die = die->child; child_die && child_die->tag;
13348 child_die = child_die->sibling)
13349 {
13350 if (child_die->tag != DW_TAG_call_site_parameter
13351 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13352 {
13353 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13354 "DW_TAG_call_site child DIE %s [in module %s]"),
13355 child_die->tag, sect_offset_str (child_die->sect_off),
13356 objfile_name (objfile));
13357 continue;
13358 }
13359
13360 nparams++;
13361 }
13362
13363 call_site
13364 = ((struct call_site *)
13365 obstack_alloc (&objfile->objfile_obstack,
13366 sizeof (*call_site)
13367 + (sizeof (*call_site->parameter) * (nparams - 1))));
13368 *slot = call_site;
13369 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13370 call_site->pc = pc;
13371
13372 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13373 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13374 {
13375 struct die_info *func_die;
13376
13377 /* Skip also over DW_TAG_inlined_subroutine. */
13378 for (func_die = die->parent;
13379 func_die && func_die->tag != DW_TAG_subprogram
13380 && func_die->tag != DW_TAG_subroutine_type;
13381 func_die = func_die->parent);
13382
13383 /* DW_AT_call_all_calls is a superset
13384 of DW_AT_call_all_tail_calls. */
13385 if (func_die
13386 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13387 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13388 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13389 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13390 {
13391 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13392 not complete. But keep CALL_SITE for look ups via call_site_htab,
13393 both the initial caller containing the real return address PC and
13394 the final callee containing the current PC of a chain of tail
13395 calls do not need to have the tail call list complete. But any
13396 function candidate for a virtual tail call frame searched via
13397 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13398 determined unambiguously. */
13399 }
13400 else
13401 {
13402 struct type *func_type = NULL;
13403
13404 if (func_die)
13405 func_type = get_die_type (func_die, cu);
13406 if (func_type != NULL)
13407 {
13408 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13409
13410 /* Enlist this call site to the function. */
13411 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13412 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13413 }
13414 else
13415 complaint (_("Cannot find function owning DW_TAG_call_site "
13416 "DIE %s [in module %s]"),
13417 sect_offset_str (die->sect_off), objfile_name (objfile));
13418 }
13419 }
13420
13421 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13422 if (attr == NULL)
13423 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13424 if (attr == NULL)
13425 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13426 if (attr == NULL)
13427 {
13428 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13429 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13430 }
13431 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13432 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13433 /* Keep NULL DWARF_BLOCK. */;
13434 else if (attr->form_is_block ())
13435 {
13436 struct dwarf2_locexpr_baton *dlbaton;
13437
13438 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13439 dlbaton->data = DW_BLOCK (attr)->data;
13440 dlbaton->size = DW_BLOCK (attr)->size;
13441 dlbaton->per_objfile = per_objfile;
13442 dlbaton->per_cu = cu->per_cu;
13443
13444 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13445 }
13446 else if (attr->form_is_ref ())
13447 {
13448 struct dwarf2_cu *target_cu = cu;
13449 struct die_info *target_die;
13450
13451 target_die = follow_die_ref (die, attr, &target_cu);
13452 gdb_assert (target_cu->per_objfile->objfile == objfile);
13453 if (die_is_declaration (target_die, target_cu))
13454 {
13455 const char *target_physname;
13456
13457 /* Prefer the mangled name; otherwise compute the demangled one. */
13458 target_physname = dw2_linkage_name (target_die, target_cu);
13459 if (target_physname == NULL)
13460 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13461 if (target_physname == NULL)
13462 complaint (_("DW_AT_call_target target DIE has invalid "
13463 "physname, for referencing DIE %s [in module %s]"),
13464 sect_offset_str (die->sect_off), objfile_name (objfile));
13465 else
13466 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13467 }
13468 else
13469 {
13470 CORE_ADDR lowpc;
13471
13472 /* DW_AT_entry_pc should be preferred. */
13473 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13474 <= PC_BOUNDS_INVALID)
13475 complaint (_("DW_AT_call_target target DIE has invalid "
13476 "low pc, for referencing DIE %s [in module %s]"),
13477 sect_offset_str (die->sect_off), objfile_name (objfile));
13478 else
13479 {
13480 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13481 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13482 }
13483 }
13484 }
13485 else
13486 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13487 "block nor reference, for DIE %s [in module %s]"),
13488 sect_offset_str (die->sect_off), objfile_name (objfile));
13489
13490 call_site->per_cu = cu->per_cu;
13491
13492 for (child_die = die->child;
13493 child_die && child_die->tag;
13494 child_die = child_die->sibling)
13495 {
13496 struct call_site_parameter *parameter;
13497 struct attribute *loc, *origin;
13498
13499 if (child_die->tag != DW_TAG_call_site_parameter
13500 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13501 {
13502 /* Already printed the complaint above. */
13503 continue;
13504 }
13505
13506 gdb_assert (call_site->parameter_count < nparams);
13507 parameter = &call_site->parameter[call_site->parameter_count];
13508
13509 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13510 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13511 register is contained in DW_AT_call_value. */
13512
13513 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13514 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13515 if (origin == NULL)
13516 {
13517 /* This was a pre-DWARF-5 GNU extension alias
13518 for DW_AT_call_parameter. */
13519 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13520 }
13521 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13522 {
13523 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13524
13525 sect_offset sect_off = origin->get_ref_die_offset ();
13526 if (!cu->header.offset_in_cu_p (sect_off))
13527 {
13528 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13529 binding can be done only inside one CU. Such referenced DIE
13530 therefore cannot be even moved to DW_TAG_partial_unit. */
13531 complaint (_("DW_AT_call_parameter offset is not in CU for "
13532 "DW_TAG_call_site child DIE %s [in module %s]"),
13533 sect_offset_str (child_die->sect_off),
13534 objfile_name (objfile));
13535 continue;
13536 }
13537 parameter->u.param_cu_off
13538 = (cu_offset) (sect_off - cu->header.sect_off);
13539 }
13540 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13541 {
13542 complaint (_("No DW_FORM_block* DW_AT_location for "
13543 "DW_TAG_call_site child DIE %s [in module %s]"),
13544 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13545 continue;
13546 }
13547 else
13548 {
13549 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13550 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13551 if (parameter->u.dwarf_reg != -1)
13552 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13553 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13554 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13555 &parameter->u.fb_offset))
13556 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13557 else
13558 {
13559 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13560 "for DW_FORM_block* DW_AT_location is supported for "
13561 "DW_TAG_call_site child DIE %s "
13562 "[in module %s]"),
13563 sect_offset_str (child_die->sect_off),
13564 objfile_name (objfile));
13565 continue;
13566 }
13567 }
13568
13569 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13570 if (attr == NULL)
13571 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13572 if (attr == NULL || !attr->form_is_block ())
13573 {
13574 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13575 "DW_TAG_call_site child DIE %s [in module %s]"),
13576 sect_offset_str (child_die->sect_off),
13577 objfile_name (objfile));
13578 continue;
13579 }
13580 parameter->value = DW_BLOCK (attr)->data;
13581 parameter->value_size = DW_BLOCK (attr)->size;
13582
13583 /* Parameters are not pre-cleared by memset above. */
13584 parameter->data_value = NULL;
13585 parameter->data_value_size = 0;
13586 call_site->parameter_count++;
13587
13588 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13589 if (attr == NULL)
13590 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13591 if (attr != nullptr)
13592 {
13593 if (!attr->form_is_block ())
13594 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13595 "DW_TAG_call_site child DIE %s [in module %s]"),
13596 sect_offset_str (child_die->sect_off),
13597 objfile_name (objfile));
13598 else
13599 {
13600 parameter->data_value = DW_BLOCK (attr)->data;
13601 parameter->data_value_size = DW_BLOCK (attr)->size;
13602 }
13603 }
13604 }
13605 }
13606
13607 /* Helper function for read_variable. If DIE represents a virtual
13608 table, then return the type of the concrete object that is
13609 associated with the virtual table. Otherwise, return NULL. */
13610
13611 static struct type *
13612 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13613 {
13614 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13615 if (attr == NULL)
13616 return NULL;
13617
13618 /* Find the type DIE. */
13619 struct die_info *type_die = NULL;
13620 struct dwarf2_cu *type_cu = cu;
13621
13622 if (attr->form_is_ref ())
13623 type_die = follow_die_ref (die, attr, &type_cu);
13624 if (type_die == NULL)
13625 return NULL;
13626
13627 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13628 return NULL;
13629 return die_containing_type (type_die, type_cu);
13630 }
13631
13632 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13633
13634 static void
13635 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13636 {
13637 struct rust_vtable_symbol *storage = NULL;
13638
13639 if (cu->language == language_rust)
13640 {
13641 struct type *containing_type = rust_containing_type (die, cu);
13642
13643 if (containing_type != NULL)
13644 {
13645 struct objfile *objfile = cu->per_objfile->objfile;
13646
13647 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13648 storage->concrete_type = containing_type;
13649 storage->subclass = SYMBOL_RUST_VTABLE;
13650 }
13651 }
13652
13653 struct symbol *res = new_symbol (die, NULL, cu, storage);
13654 struct attribute *abstract_origin
13655 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13656 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13657 if (res == NULL && loc && abstract_origin)
13658 {
13659 /* We have a variable without a name, but with a location and an abstract
13660 origin. This may be a concrete instance of an abstract variable
13661 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13662 later. */
13663 struct dwarf2_cu *origin_cu = cu;
13664 struct die_info *origin_die
13665 = follow_die_ref (die, abstract_origin, &origin_cu);
13666 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13667 per_objfile->per_bfd->abstract_to_concrete
13668 [origin_die->sect_off].push_back (die->sect_off);
13669 }
13670 }
13671
13672 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13673 reading .debug_rnglists.
13674 Callback's type should be:
13675 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13676 Return true if the attributes are present and valid, otherwise,
13677 return false. */
13678
13679 template <typename Callback>
13680 static bool
13681 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13682 Callback &&callback)
13683 {
13684 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
13685 struct objfile *objfile = dwarf2_per_objfile->objfile;
13686 bfd *obfd = objfile->obfd;
13687 /* Base address selection entry. */
13688 gdb::optional<CORE_ADDR> base;
13689 const gdb_byte *buffer;
13690 CORE_ADDR baseaddr;
13691 bool overflow = false;
13692
13693 base = cu->base_address;
13694
13695 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13696 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
13697 {
13698 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13699 offset);
13700 return false;
13701 }
13702 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
13703
13704 baseaddr = objfile->text_section_offset ();
13705
13706 while (1)
13707 {
13708 /* Initialize it due to a false compiler warning. */
13709 CORE_ADDR range_beginning = 0, range_end = 0;
13710 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13711 + dwarf2_per_objfile->per_bfd->rnglists.size);
13712 unsigned int bytes_read;
13713
13714 if (buffer == buf_end)
13715 {
13716 overflow = true;
13717 break;
13718 }
13719 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13720 switch (rlet)
13721 {
13722 case DW_RLE_end_of_list:
13723 break;
13724 case DW_RLE_base_address:
13725 if (buffer + cu->header.addr_size > buf_end)
13726 {
13727 overflow = true;
13728 break;
13729 }
13730 base = cu->header.read_address (obfd, buffer, &bytes_read);
13731 buffer += bytes_read;
13732 break;
13733 case DW_RLE_start_length:
13734 if (buffer + cu->header.addr_size > buf_end)
13735 {
13736 overflow = true;
13737 break;
13738 }
13739 range_beginning = cu->header.read_address (obfd, buffer,
13740 &bytes_read);
13741 buffer += bytes_read;
13742 range_end = (range_beginning
13743 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13744 buffer += bytes_read;
13745 if (buffer > buf_end)
13746 {
13747 overflow = true;
13748 break;
13749 }
13750 break;
13751 case DW_RLE_offset_pair:
13752 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13753 buffer += bytes_read;
13754 if (buffer > buf_end)
13755 {
13756 overflow = true;
13757 break;
13758 }
13759 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13760 buffer += bytes_read;
13761 if (buffer > buf_end)
13762 {
13763 overflow = true;
13764 break;
13765 }
13766 break;
13767 case DW_RLE_start_end:
13768 if (buffer + 2 * cu->header.addr_size > buf_end)
13769 {
13770 overflow = true;
13771 break;
13772 }
13773 range_beginning = cu->header.read_address (obfd, buffer,
13774 &bytes_read);
13775 buffer += bytes_read;
13776 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13777 buffer += bytes_read;
13778 break;
13779 default:
13780 complaint (_("Invalid .debug_rnglists data (no base address)"));
13781 return false;
13782 }
13783 if (rlet == DW_RLE_end_of_list || overflow)
13784 break;
13785 if (rlet == DW_RLE_base_address)
13786 continue;
13787
13788 if (!base.has_value ())
13789 {
13790 /* We have no valid base address for the ranges
13791 data. */
13792 complaint (_("Invalid .debug_rnglists data (no base address)"));
13793 return false;
13794 }
13795
13796 if (range_beginning > range_end)
13797 {
13798 /* Inverted range entries are invalid. */
13799 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13800 return false;
13801 }
13802
13803 /* Empty range entries have no effect. */
13804 if (range_beginning == range_end)
13805 continue;
13806
13807 range_beginning += *base;
13808 range_end += *base;
13809
13810 /* A not-uncommon case of bad debug info.
13811 Don't pollute the addrmap with bad data. */
13812 if (range_beginning + baseaddr == 0
13813 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
13814 {
13815 complaint (_(".debug_rnglists entry has start address of zero"
13816 " [in module %s]"), objfile_name (objfile));
13817 continue;
13818 }
13819
13820 callback (range_beginning, range_end);
13821 }
13822
13823 if (overflow)
13824 {
13825 complaint (_("Offset %d is not terminated "
13826 "for DW_AT_ranges attribute"),
13827 offset);
13828 return false;
13829 }
13830
13831 return true;
13832 }
13833
13834 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13835 Callback's type should be:
13836 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13837 Return 1 if the attributes are present and valid, otherwise, return 0. */
13838
13839 template <typename Callback>
13840 static int
13841 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13842 Callback &&callback)
13843 {
13844 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13845 struct objfile *objfile = per_objfile->objfile;
13846 struct comp_unit_head *cu_header = &cu->header;
13847 bfd *obfd = objfile->obfd;
13848 unsigned int addr_size = cu_header->addr_size;
13849 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13850 /* Base address selection entry. */
13851 gdb::optional<CORE_ADDR> base;
13852 unsigned int dummy;
13853 const gdb_byte *buffer;
13854 CORE_ADDR baseaddr;
13855
13856 if (cu_header->version >= 5)
13857 return dwarf2_rnglists_process (offset, cu, callback);
13858
13859 base = cu->base_address;
13860
13861 per_objfile->per_bfd->ranges.read (objfile);
13862 if (offset >= per_objfile->per_bfd->ranges.size)
13863 {
13864 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13865 offset);
13866 return 0;
13867 }
13868 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13869
13870 baseaddr = objfile->text_section_offset ();
13871
13872 while (1)
13873 {
13874 CORE_ADDR range_beginning, range_end;
13875
13876 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13877 buffer += addr_size;
13878 range_end = cu->header.read_address (obfd, buffer, &dummy);
13879 buffer += addr_size;
13880 offset += 2 * addr_size;
13881
13882 /* An end of list marker is a pair of zero addresses. */
13883 if (range_beginning == 0 && range_end == 0)
13884 /* Found the end of list entry. */
13885 break;
13886
13887 /* Each base address selection entry is a pair of 2 values.
13888 The first is the largest possible address, the second is
13889 the base address. Check for a base address here. */
13890 if ((range_beginning & mask) == mask)
13891 {
13892 /* If we found the largest possible address, then we already
13893 have the base address in range_end. */
13894 base = range_end;
13895 continue;
13896 }
13897
13898 if (!base.has_value ())
13899 {
13900 /* We have no valid base address for the ranges
13901 data. */
13902 complaint (_("Invalid .debug_ranges data (no base address)"));
13903 return 0;
13904 }
13905
13906 if (range_beginning > range_end)
13907 {
13908 /* Inverted range entries are invalid. */
13909 complaint (_("Invalid .debug_ranges data (inverted range)"));
13910 return 0;
13911 }
13912
13913 /* Empty range entries have no effect. */
13914 if (range_beginning == range_end)
13915 continue;
13916
13917 range_beginning += *base;
13918 range_end += *base;
13919
13920 /* A not-uncommon case of bad debug info.
13921 Don't pollute the addrmap with bad data. */
13922 if (range_beginning + baseaddr == 0
13923 && !per_objfile->per_bfd->has_section_at_zero)
13924 {
13925 complaint (_(".debug_ranges entry has start address of zero"
13926 " [in module %s]"), objfile_name (objfile));
13927 continue;
13928 }
13929
13930 callback (range_beginning, range_end);
13931 }
13932
13933 return 1;
13934 }
13935
13936 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13937 Return 1 if the attributes are present and valid, otherwise, return 0.
13938 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13939
13940 static int
13941 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13942 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13943 dwarf2_psymtab *ranges_pst)
13944 {
13945 struct objfile *objfile = cu->per_objfile->objfile;
13946 struct gdbarch *gdbarch = objfile->arch ();
13947 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13948 int low_set = 0;
13949 CORE_ADDR low = 0;
13950 CORE_ADDR high = 0;
13951 int retval;
13952
13953 retval = dwarf2_ranges_process (offset, cu,
13954 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13955 {
13956 if (ranges_pst != NULL)
13957 {
13958 CORE_ADDR lowpc;
13959 CORE_ADDR highpc;
13960
13961 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13962 range_beginning + baseaddr)
13963 - baseaddr);
13964 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13965 range_end + baseaddr)
13966 - baseaddr);
13967 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13968 lowpc, highpc - 1, ranges_pst);
13969 }
13970
13971 /* FIXME: This is recording everything as a low-high
13972 segment of consecutive addresses. We should have a
13973 data structure for discontiguous block ranges
13974 instead. */
13975 if (! low_set)
13976 {
13977 low = range_beginning;
13978 high = range_end;
13979 low_set = 1;
13980 }
13981 else
13982 {
13983 if (range_beginning < low)
13984 low = range_beginning;
13985 if (range_end > high)
13986 high = range_end;
13987 }
13988 });
13989 if (!retval)
13990 return 0;
13991
13992 if (! low_set)
13993 /* If the first entry is an end-of-list marker, the range
13994 describes an empty scope, i.e. no instructions. */
13995 return 0;
13996
13997 if (low_return)
13998 *low_return = low;
13999 if (high_return)
14000 *high_return = high;
14001 return 1;
14002 }
14003
14004 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14005 definition for the return value. *LOWPC and *HIGHPC are set iff
14006 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14007
14008 static enum pc_bounds_kind
14009 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14010 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14011 dwarf2_psymtab *pst)
14012 {
14013 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
14014 struct attribute *attr;
14015 struct attribute *attr_high;
14016 CORE_ADDR low = 0;
14017 CORE_ADDR high = 0;
14018 enum pc_bounds_kind ret;
14019
14020 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14021 if (attr_high)
14022 {
14023 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14024 if (attr != nullptr)
14025 {
14026 low = attr->value_as_address ();
14027 high = attr_high->value_as_address ();
14028 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14029 high += low;
14030 }
14031 else
14032 /* Found high w/o low attribute. */
14033 return PC_BOUNDS_INVALID;
14034
14035 /* Found consecutive range of addresses. */
14036 ret = PC_BOUNDS_HIGH_LOW;
14037 }
14038 else
14039 {
14040 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14041 if (attr != NULL)
14042 {
14043 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14044 We take advantage of the fact that DW_AT_ranges does not appear
14045 in DW_TAG_compile_unit of DWO files. */
14046 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14047 unsigned int ranges_offset = (DW_UNSND (attr)
14048 + (need_ranges_base
14049 ? cu->ranges_base
14050 : 0));
14051
14052 /* Value of the DW_AT_ranges attribute is the offset in the
14053 .debug_ranges section. */
14054 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14055 return PC_BOUNDS_INVALID;
14056 /* Found discontinuous range of addresses. */
14057 ret = PC_BOUNDS_RANGES;
14058 }
14059 else
14060 return PC_BOUNDS_NOT_PRESENT;
14061 }
14062
14063 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14064 if (high <= low)
14065 return PC_BOUNDS_INVALID;
14066
14067 /* When using the GNU linker, .gnu.linkonce. sections are used to
14068 eliminate duplicate copies of functions and vtables and such.
14069 The linker will arbitrarily choose one and discard the others.
14070 The AT_*_pc values for such functions refer to local labels in
14071 these sections. If the section from that file was discarded, the
14072 labels are not in the output, so the relocs get a value of 0.
14073 If this is a discarded function, mark the pc bounds as invalid,
14074 so that GDB will ignore it. */
14075 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
14076 return PC_BOUNDS_INVALID;
14077
14078 *lowpc = low;
14079 if (highpc)
14080 *highpc = high;
14081 return ret;
14082 }
14083
14084 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14085 its low and high PC addresses. Do nothing if these addresses could not
14086 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14087 and HIGHPC to the high address if greater than HIGHPC. */
14088
14089 static void
14090 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14091 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14092 struct dwarf2_cu *cu)
14093 {
14094 CORE_ADDR low, high;
14095 struct die_info *child = die->child;
14096
14097 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14098 {
14099 *lowpc = std::min (*lowpc, low);
14100 *highpc = std::max (*highpc, high);
14101 }
14102
14103 /* If the language does not allow nested subprograms (either inside
14104 subprograms or lexical blocks), we're done. */
14105 if (cu->language != language_ada)
14106 return;
14107
14108 /* Check all the children of the given DIE. If it contains nested
14109 subprograms, then check their pc bounds. Likewise, we need to
14110 check lexical blocks as well, as they may also contain subprogram
14111 definitions. */
14112 while (child && child->tag)
14113 {
14114 if (child->tag == DW_TAG_subprogram
14115 || child->tag == DW_TAG_lexical_block)
14116 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14117 child = child->sibling;
14118 }
14119 }
14120
14121 /* Get the low and high pc's represented by the scope DIE, and store
14122 them in *LOWPC and *HIGHPC. If the correct values can't be
14123 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14124
14125 static void
14126 get_scope_pc_bounds (struct die_info *die,
14127 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14128 struct dwarf2_cu *cu)
14129 {
14130 CORE_ADDR best_low = (CORE_ADDR) -1;
14131 CORE_ADDR best_high = (CORE_ADDR) 0;
14132 CORE_ADDR current_low, current_high;
14133
14134 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14135 >= PC_BOUNDS_RANGES)
14136 {
14137 best_low = current_low;
14138 best_high = current_high;
14139 }
14140 else
14141 {
14142 struct die_info *child = die->child;
14143
14144 while (child && child->tag)
14145 {
14146 switch (child->tag) {
14147 case DW_TAG_subprogram:
14148 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14149 break;
14150 case DW_TAG_namespace:
14151 case DW_TAG_module:
14152 /* FIXME: carlton/2004-01-16: Should we do this for
14153 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14154 that current GCC's always emit the DIEs corresponding
14155 to definitions of methods of classes as children of a
14156 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14157 the DIEs giving the declarations, which could be
14158 anywhere). But I don't see any reason why the
14159 standards says that they have to be there. */
14160 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14161
14162 if (current_low != ((CORE_ADDR) -1))
14163 {
14164 best_low = std::min (best_low, current_low);
14165 best_high = std::max (best_high, current_high);
14166 }
14167 break;
14168 default:
14169 /* Ignore. */
14170 break;
14171 }
14172
14173 child = child->sibling;
14174 }
14175 }
14176
14177 *lowpc = best_low;
14178 *highpc = best_high;
14179 }
14180
14181 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14182 in DIE. */
14183
14184 static void
14185 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14186 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14187 {
14188 struct objfile *objfile = cu->per_objfile->objfile;
14189 struct gdbarch *gdbarch = objfile->arch ();
14190 struct attribute *attr;
14191 struct attribute *attr_high;
14192
14193 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14194 if (attr_high)
14195 {
14196 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14197 if (attr != nullptr)
14198 {
14199 CORE_ADDR low = attr->value_as_address ();
14200 CORE_ADDR high = attr_high->value_as_address ();
14201
14202 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14203 high += low;
14204
14205 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14206 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14207 cu->get_builder ()->record_block_range (block, low, high - 1);
14208 }
14209 }
14210
14211 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14212 if (attr != nullptr)
14213 {
14214 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14215 We take advantage of the fact that DW_AT_ranges does not appear
14216 in DW_TAG_compile_unit of DWO files. */
14217 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14218
14219 /* The value of the DW_AT_ranges attribute is the offset of the
14220 address range list in the .debug_ranges section. */
14221 unsigned long offset = (DW_UNSND (attr)
14222 + (need_ranges_base ? cu->ranges_base : 0));
14223
14224 std::vector<blockrange> blockvec;
14225 dwarf2_ranges_process (offset, cu,
14226 [&] (CORE_ADDR start, CORE_ADDR end)
14227 {
14228 start += baseaddr;
14229 end += baseaddr;
14230 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14231 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14232 cu->get_builder ()->record_block_range (block, start, end - 1);
14233 blockvec.emplace_back (start, end);
14234 });
14235
14236 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14237 }
14238 }
14239
14240 /* Check whether the producer field indicates either of GCC < 4.6, or the
14241 Intel C/C++ compiler, and cache the result in CU. */
14242
14243 static void
14244 check_producer (struct dwarf2_cu *cu)
14245 {
14246 int major, minor;
14247
14248 if (cu->producer == NULL)
14249 {
14250 /* For unknown compilers expect their behavior is DWARF version
14251 compliant.
14252
14253 GCC started to support .debug_types sections by -gdwarf-4 since
14254 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14255 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14256 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14257 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14258 }
14259 else if (producer_is_gcc (cu->producer, &major, &minor))
14260 {
14261 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14262 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14263 }
14264 else if (producer_is_icc (cu->producer, &major, &minor))
14265 {
14266 cu->producer_is_icc = true;
14267 cu->producer_is_icc_lt_14 = major < 14;
14268 }
14269 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14270 cu->producer_is_codewarrior = true;
14271 else
14272 {
14273 /* For other non-GCC compilers, expect their behavior is DWARF version
14274 compliant. */
14275 }
14276
14277 cu->checked_producer = true;
14278 }
14279
14280 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14281 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14282 during 4.6.0 experimental. */
14283
14284 static bool
14285 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14286 {
14287 if (!cu->checked_producer)
14288 check_producer (cu);
14289
14290 return cu->producer_is_gxx_lt_4_6;
14291 }
14292
14293
14294 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14295 with incorrect is_stmt attributes. */
14296
14297 static bool
14298 producer_is_codewarrior (struct dwarf2_cu *cu)
14299 {
14300 if (!cu->checked_producer)
14301 check_producer (cu);
14302
14303 return cu->producer_is_codewarrior;
14304 }
14305
14306 /* Return the default accessibility type if it is not overridden by
14307 DW_AT_accessibility. */
14308
14309 static enum dwarf_access_attribute
14310 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14311 {
14312 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14313 {
14314 /* The default DWARF 2 accessibility for members is public, the default
14315 accessibility for inheritance is private. */
14316
14317 if (die->tag != DW_TAG_inheritance)
14318 return DW_ACCESS_public;
14319 else
14320 return DW_ACCESS_private;
14321 }
14322 else
14323 {
14324 /* DWARF 3+ defines the default accessibility a different way. The same
14325 rules apply now for DW_TAG_inheritance as for the members and it only
14326 depends on the container kind. */
14327
14328 if (die->parent->tag == DW_TAG_class_type)
14329 return DW_ACCESS_private;
14330 else
14331 return DW_ACCESS_public;
14332 }
14333 }
14334
14335 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14336 offset. If the attribute was not found return 0, otherwise return
14337 1. If it was found but could not properly be handled, set *OFFSET
14338 to 0. */
14339
14340 static int
14341 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14342 LONGEST *offset)
14343 {
14344 struct attribute *attr;
14345
14346 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14347 if (attr != NULL)
14348 {
14349 *offset = 0;
14350
14351 /* Note that we do not check for a section offset first here.
14352 This is because DW_AT_data_member_location is new in DWARF 4,
14353 so if we see it, we can assume that a constant form is really
14354 a constant and not a section offset. */
14355 if (attr->form_is_constant ())
14356 *offset = attr->constant_value (0);
14357 else if (attr->form_is_section_offset ())
14358 dwarf2_complex_location_expr_complaint ();
14359 else if (attr->form_is_block ())
14360 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14361 else
14362 dwarf2_complex_location_expr_complaint ();
14363
14364 return 1;
14365 }
14366
14367 return 0;
14368 }
14369
14370 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14371
14372 static void
14373 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14374 struct field *field)
14375 {
14376 struct attribute *attr;
14377
14378 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14379 if (attr != NULL)
14380 {
14381 if (attr->form_is_constant ())
14382 {
14383 LONGEST offset = attr->constant_value (0);
14384 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14385 }
14386 else if (attr->form_is_section_offset ())
14387 dwarf2_complex_location_expr_complaint ();
14388 else if (attr->form_is_block ())
14389 {
14390 bool handled;
14391 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14392 if (handled)
14393 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14394 else
14395 {
14396 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14397 struct objfile *objfile = per_objfile->objfile;
14398 struct dwarf2_locexpr_baton *dlbaton
14399 = XOBNEW (&objfile->objfile_obstack,
14400 struct dwarf2_locexpr_baton);
14401 dlbaton->data = DW_BLOCK (attr)->data;
14402 dlbaton->size = DW_BLOCK (attr)->size;
14403 /* When using this baton, we want to compute the address
14404 of the field, not the value. This is why
14405 is_reference is set to false here. */
14406 dlbaton->is_reference = false;
14407 dlbaton->per_objfile = per_objfile;
14408 dlbaton->per_cu = cu->per_cu;
14409
14410 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14411 }
14412 }
14413 else
14414 dwarf2_complex_location_expr_complaint ();
14415 }
14416 }
14417
14418 /* Add an aggregate field to the field list. */
14419
14420 static void
14421 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14422 struct dwarf2_cu *cu)
14423 {
14424 struct objfile *objfile = cu->per_objfile->objfile;
14425 struct gdbarch *gdbarch = objfile->arch ();
14426 struct nextfield *new_field;
14427 struct attribute *attr;
14428 struct field *fp;
14429 const char *fieldname = "";
14430
14431 if (die->tag == DW_TAG_inheritance)
14432 {
14433 fip->baseclasses.emplace_back ();
14434 new_field = &fip->baseclasses.back ();
14435 }
14436 else
14437 {
14438 fip->fields.emplace_back ();
14439 new_field = &fip->fields.back ();
14440 }
14441
14442 new_field->offset = die->sect_off;
14443
14444 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14445 if (attr != nullptr)
14446 new_field->accessibility = DW_UNSND (attr);
14447 else
14448 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14449 if (new_field->accessibility != DW_ACCESS_public)
14450 fip->non_public_fields = 1;
14451
14452 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14453 if (attr != nullptr)
14454 new_field->virtuality = DW_UNSND (attr);
14455 else
14456 new_field->virtuality = DW_VIRTUALITY_none;
14457
14458 fp = &new_field->field;
14459
14460 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14461 {
14462 /* Data member other than a C++ static data member. */
14463
14464 /* Get type of field. */
14465 fp->type = die_type (die, cu);
14466
14467 SET_FIELD_BITPOS (*fp, 0);
14468
14469 /* Get bit size of field (zero if none). */
14470 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14471 if (attr != nullptr)
14472 {
14473 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14474 }
14475 else
14476 {
14477 FIELD_BITSIZE (*fp) = 0;
14478 }
14479
14480 /* Get bit offset of field. */
14481 handle_data_member_location (die, cu, fp);
14482 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14483 if (attr != nullptr)
14484 {
14485 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14486 {
14487 /* For big endian bits, the DW_AT_bit_offset gives the
14488 additional bit offset from the MSB of the containing
14489 anonymous object to the MSB of the field. We don't
14490 have to do anything special since we don't need to
14491 know the size of the anonymous object. */
14492 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14493 }
14494 else
14495 {
14496 /* For little endian bits, compute the bit offset to the
14497 MSB of the anonymous object, subtract off the number of
14498 bits from the MSB of the field to the MSB of the
14499 object, and then subtract off the number of bits of
14500 the field itself. The result is the bit offset of
14501 the LSB of the field. */
14502 int anonymous_size;
14503 int bit_offset = DW_UNSND (attr);
14504
14505 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14506 if (attr != nullptr)
14507 {
14508 /* The size of the anonymous object containing
14509 the bit field is explicit, so use the
14510 indicated size (in bytes). */
14511 anonymous_size = DW_UNSND (attr);
14512 }
14513 else
14514 {
14515 /* The size of the anonymous object containing
14516 the bit field must be inferred from the type
14517 attribute of the data member containing the
14518 bit field. */
14519 anonymous_size = TYPE_LENGTH (fp->type);
14520 }
14521 SET_FIELD_BITPOS (*fp,
14522 (FIELD_BITPOS (*fp)
14523 + anonymous_size * bits_per_byte
14524 - bit_offset - FIELD_BITSIZE (*fp)));
14525 }
14526 }
14527 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14528 if (attr != NULL)
14529 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14530 + attr->constant_value (0)));
14531
14532 /* Get name of field. */
14533 fieldname = dwarf2_name (die, cu);
14534 if (fieldname == NULL)
14535 fieldname = "";
14536
14537 /* The name is already allocated along with this objfile, so we don't
14538 need to duplicate it for the type. */
14539 fp->name = fieldname;
14540
14541 /* Change accessibility for artificial fields (e.g. virtual table
14542 pointer or virtual base class pointer) to private. */
14543 if (dwarf2_attr (die, DW_AT_artificial, cu))
14544 {
14545 FIELD_ARTIFICIAL (*fp) = 1;
14546 new_field->accessibility = DW_ACCESS_private;
14547 fip->non_public_fields = 1;
14548 }
14549 }
14550 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14551 {
14552 /* C++ static member. */
14553
14554 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14555 is a declaration, but all versions of G++ as of this writing
14556 (so through at least 3.2.1) incorrectly generate
14557 DW_TAG_variable tags. */
14558
14559 const char *physname;
14560
14561 /* Get name of field. */
14562 fieldname = dwarf2_name (die, cu);
14563 if (fieldname == NULL)
14564 return;
14565
14566 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14567 if (attr
14568 /* Only create a symbol if this is an external value.
14569 new_symbol checks this and puts the value in the global symbol
14570 table, which we want. If it is not external, new_symbol
14571 will try to put the value in cu->list_in_scope which is wrong. */
14572 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14573 {
14574 /* A static const member, not much different than an enum as far as
14575 we're concerned, except that we can support more types. */
14576 new_symbol (die, NULL, cu);
14577 }
14578
14579 /* Get physical name. */
14580 physname = dwarf2_physname (fieldname, die, cu);
14581
14582 /* The name is already allocated along with this objfile, so we don't
14583 need to duplicate it for the type. */
14584 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14585 FIELD_TYPE (*fp) = die_type (die, cu);
14586 FIELD_NAME (*fp) = fieldname;
14587 }
14588 else if (die->tag == DW_TAG_inheritance)
14589 {
14590 /* C++ base class field. */
14591 handle_data_member_location (die, cu, fp);
14592 FIELD_BITSIZE (*fp) = 0;
14593 FIELD_TYPE (*fp) = die_type (die, cu);
14594 FIELD_NAME (*fp) = fp->type->name ();
14595 }
14596 else
14597 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14598 }
14599
14600 /* Can the type given by DIE define another type? */
14601
14602 static bool
14603 type_can_define_types (const struct die_info *die)
14604 {
14605 switch (die->tag)
14606 {
14607 case DW_TAG_typedef:
14608 case DW_TAG_class_type:
14609 case DW_TAG_structure_type:
14610 case DW_TAG_union_type:
14611 case DW_TAG_enumeration_type:
14612 return true;
14613
14614 default:
14615 return false;
14616 }
14617 }
14618
14619 /* Add a type definition defined in the scope of the FIP's class. */
14620
14621 static void
14622 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14623 struct dwarf2_cu *cu)
14624 {
14625 struct decl_field fp;
14626 memset (&fp, 0, sizeof (fp));
14627
14628 gdb_assert (type_can_define_types (die));
14629
14630 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14631 fp.name = dwarf2_name (die, cu);
14632 fp.type = read_type_die (die, cu);
14633
14634 /* Save accessibility. */
14635 enum dwarf_access_attribute accessibility;
14636 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14637 if (attr != NULL)
14638 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14639 else
14640 accessibility = dwarf2_default_access_attribute (die, cu);
14641 switch (accessibility)
14642 {
14643 case DW_ACCESS_public:
14644 /* The assumed value if neither private nor protected. */
14645 break;
14646 case DW_ACCESS_private:
14647 fp.is_private = 1;
14648 break;
14649 case DW_ACCESS_protected:
14650 fp.is_protected = 1;
14651 break;
14652 default:
14653 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14654 }
14655
14656 if (die->tag == DW_TAG_typedef)
14657 fip->typedef_field_list.push_back (fp);
14658 else
14659 fip->nested_types_list.push_back (fp);
14660 }
14661
14662 /* A convenience typedef that's used when finding the discriminant
14663 field for a variant part. */
14664 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14665 offset_map_type;
14666
14667 /* Compute the discriminant range for a given variant. OBSTACK is
14668 where the results will be stored. VARIANT is the variant to
14669 process. IS_UNSIGNED indicates whether the discriminant is signed
14670 or unsigned. */
14671
14672 static const gdb::array_view<discriminant_range>
14673 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14674 bool is_unsigned)
14675 {
14676 std::vector<discriminant_range> ranges;
14677
14678 if (variant.default_branch)
14679 return {};
14680
14681 if (variant.discr_list_data == nullptr)
14682 {
14683 discriminant_range r
14684 = {variant.discriminant_value, variant.discriminant_value};
14685 ranges.push_back (r);
14686 }
14687 else
14688 {
14689 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14690 variant.discr_list_data->size);
14691 while (!data.empty ())
14692 {
14693 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14694 {
14695 complaint (_("invalid discriminant marker: %d"), data[0]);
14696 break;
14697 }
14698 bool is_range = data[0] == DW_DSC_range;
14699 data = data.slice (1);
14700
14701 ULONGEST low, high;
14702 unsigned int bytes_read;
14703
14704 if (data.empty ())
14705 {
14706 complaint (_("DW_AT_discr_list missing low value"));
14707 break;
14708 }
14709 if (is_unsigned)
14710 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14711 else
14712 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14713 &bytes_read);
14714 data = data.slice (bytes_read);
14715
14716 if (is_range)
14717 {
14718 if (data.empty ())
14719 {
14720 complaint (_("DW_AT_discr_list missing high value"));
14721 break;
14722 }
14723 if (is_unsigned)
14724 high = read_unsigned_leb128 (nullptr, data.data (),
14725 &bytes_read);
14726 else
14727 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14728 &bytes_read);
14729 data = data.slice (bytes_read);
14730 }
14731 else
14732 high = low;
14733
14734 ranges.push_back ({ low, high });
14735 }
14736 }
14737
14738 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14739 ranges.size ());
14740 std::copy (ranges.begin (), ranges.end (), result);
14741 return gdb::array_view<discriminant_range> (result, ranges.size ());
14742 }
14743
14744 static const gdb::array_view<variant_part> create_variant_parts
14745 (struct obstack *obstack,
14746 const offset_map_type &offset_map,
14747 struct field_info *fi,
14748 const std::vector<variant_part_builder> &variant_parts);
14749
14750 /* Fill in a "struct variant" for a given variant field. RESULT is
14751 the variant to fill in. OBSTACK is where any needed allocations
14752 will be done. OFFSET_MAP holds the mapping from section offsets to
14753 fields for the type. FI describes the fields of the type we're
14754 processing. FIELD is the variant field we're converting. */
14755
14756 static void
14757 create_one_variant (variant &result, struct obstack *obstack,
14758 const offset_map_type &offset_map,
14759 struct field_info *fi, const variant_field &field)
14760 {
14761 result.discriminants = convert_variant_range (obstack, field, false);
14762 result.first_field = field.first_field + fi->baseclasses.size ();
14763 result.last_field = field.last_field + fi->baseclasses.size ();
14764 result.parts = create_variant_parts (obstack, offset_map, fi,
14765 field.variant_parts);
14766 }
14767
14768 /* Fill in a "struct variant_part" for a given variant part. RESULT
14769 is the variant part to fill in. OBSTACK is where any needed
14770 allocations will be done. OFFSET_MAP holds the mapping from
14771 section offsets to fields for the type. FI describes the fields of
14772 the type we're processing. BUILDER is the variant part to be
14773 converted. */
14774
14775 static void
14776 create_one_variant_part (variant_part &result,
14777 struct obstack *obstack,
14778 const offset_map_type &offset_map,
14779 struct field_info *fi,
14780 const variant_part_builder &builder)
14781 {
14782 auto iter = offset_map.find (builder.discriminant_offset);
14783 if (iter == offset_map.end ())
14784 {
14785 result.discriminant_index = -1;
14786 /* Doesn't matter. */
14787 result.is_unsigned = false;
14788 }
14789 else
14790 {
14791 result.discriminant_index = iter->second;
14792 result.is_unsigned
14793 = TYPE_UNSIGNED (FIELD_TYPE
14794 (fi->fields[result.discriminant_index].field));
14795 }
14796
14797 size_t n = builder.variants.size ();
14798 variant *output = new (obstack) variant[n];
14799 for (size_t i = 0; i < n; ++i)
14800 create_one_variant (output[i], obstack, offset_map, fi,
14801 builder.variants[i]);
14802
14803 result.variants = gdb::array_view<variant> (output, n);
14804 }
14805
14806 /* Create a vector of variant parts that can be attached to a type.
14807 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14808 holds the mapping from section offsets to fields for the type. FI
14809 describes the fields of the type we're processing. VARIANT_PARTS
14810 is the vector to convert. */
14811
14812 static const gdb::array_view<variant_part>
14813 create_variant_parts (struct obstack *obstack,
14814 const offset_map_type &offset_map,
14815 struct field_info *fi,
14816 const std::vector<variant_part_builder> &variant_parts)
14817 {
14818 if (variant_parts.empty ())
14819 return {};
14820
14821 size_t n = variant_parts.size ();
14822 variant_part *result = new (obstack) variant_part[n];
14823 for (size_t i = 0; i < n; ++i)
14824 create_one_variant_part (result[i], obstack, offset_map, fi,
14825 variant_parts[i]);
14826
14827 return gdb::array_view<variant_part> (result, n);
14828 }
14829
14830 /* Compute the variant part vector for FIP, attaching it to TYPE when
14831 done. */
14832
14833 static void
14834 add_variant_property (struct field_info *fip, struct type *type,
14835 struct dwarf2_cu *cu)
14836 {
14837 /* Map section offsets of fields to their field index. Note the
14838 field index here does not take the number of baseclasses into
14839 account. */
14840 offset_map_type offset_map;
14841 for (int i = 0; i < fip->fields.size (); ++i)
14842 offset_map[fip->fields[i].offset] = i;
14843
14844 struct objfile *objfile = cu->per_objfile->objfile;
14845 gdb::array_view<variant_part> parts
14846 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14847 fip->variant_parts);
14848
14849 struct dynamic_prop prop;
14850 prop.kind = PROP_VARIANT_PARTS;
14851 prop.data.variant_parts
14852 = ((gdb::array_view<variant_part> *)
14853 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14854
14855 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14856 }
14857
14858 /* Create the vector of fields, and attach it to the type. */
14859
14860 static void
14861 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14862 struct dwarf2_cu *cu)
14863 {
14864 int nfields = fip->nfields ();
14865
14866 /* Record the field count, allocate space for the array of fields,
14867 and create blank accessibility bitfields if necessary. */
14868 type->set_num_fields (nfields);
14869 type->set_fields
14870 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14871
14872 if (fip->non_public_fields && cu->language != language_ada)
14873 {
14874 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14875
14876 TYPE_FIELD_PRIVATE_BITS (type) =
14877 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14878 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14879
14880 TYPE_FIELD_PROTECTED_BITS (type) =
14881 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14882 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14883
14884 TYPE_FIELD_IGNORE_BITS (type) =
14885 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14886 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14887 }
14888
14889 /* If the type has baseclasses, allocate and clear a bit vector for
14890 TYPE_FIELD_VIRTUAL_BITS. */
14891 if (!fip->baseclasses.empty () && cu->language != language_ada)
14892 {
14893 int num_bytes = B_BYTES (fip->baseclasses.size ());
14894 unsigned char *pointer;
14895
14896 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14897 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14898 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14899 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14900 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14901 }
14902
14903 if (!fip->variant_parts.empty ())
14904 add_variant_property (fip, type, cu);
14905
14906 /* Copy the saved-up fields into the field vector. */
14907 for (int i = 0; i < nfields; ++i)
14908 {
14909 struct nextfield &field
14910 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14911 : fip->fields[i - fip->baseclasses.size ()]);
14912
14913 type->field (i) = field.field;
14914 switch (field.accessibility)
14915 {
14916 case DW_ACCESS_private:
14917 if (cu->language != language_ada)
14918 SET_TYPE_FIELD_PRIVATE (type, i);
14919 break;
14920
14921 case DW_ACCESS_protected:
14922 if (cu->language != language_ada)
14923 SET_TYPE_FIELD_PROTECTED (type, i);
14924 break;
14925
14926 case DW_ACCESS_public:
14927 break;
14928
14929 default:
14930 /* Unknown accessibility. Complain and treat it as public. */
14931 {
14932 complaint (_("unsupported accessibility %d"),
14933 field.accessibility);
14934 }
14935 break;
14936 }
14937 if (i < fip->baseclasses.size ())
14938 {
14939 switch (field.virtuality)
14940 {
14941 case DW_VIRTUALITY_virtual:
14942 case DW_VIRTUALITY_pure_virtual:
14943 if (cu->language == language_ada)
14944 error (_("unexpected virtuality in component of Ada type"));
14945 SET_TYPE_FIELD_VIRTUAL (type, i);
14946 break;
14947 }
14948 }
14949 }
14950 }
14951
14952 /* Return true if this member function is a constructor, false
14953 otherwise. */
14954
14955 static int
14956 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14957 {
14958 const char *fieldname;
14959 const char *type_name;
14960 int len;
14961
14962 if (die->parent == NULL)
14963 return 0;
14964
14965 if (die->parent->tag != DW_TAG_structure_type
14966 && die->parent->tag != DW_TAG_union_type
14967 && die->parent->tag != DW_TAG_class_type)
14968 return 0;
14969
14970 fieldname = dwarf2_name (die, cu);
14971 type_name = dwarf2_name (die->parent, cu);
14972 if (fieldname == NULL || type_name == NULL)
14973 return 0;
14974
14975 len = strlen (fieldname);
14976 return (strncmp (fieldname, type_name, len) == 0
14977 && (type_name[len] == '\0' || type_name[len] == '<'));
14978 }
14979
14980 /* Check if the given VALUE is a recognized enum
14981 dwarf_defaulted_attribute constant according to DWARF5 spec,
14982 Table 7.24. */
14983
14984 static bool
14985 is_valid_DW_AT_defaulted (ULONGEST value)
14986 {
14987 switch (value)
14988 {
14989 case DW_DEFAULTED_no:
14990 case DW_DEFAULTED_in_class:
14991 case DW_DEFAULTED_out_of_class:
14992 return true;
14993 }
14994
14995 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14996 return false;
14997 }
14998
14999 /* Add a member function to the proper fieldlist. */
15000
15001 static void
15002 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15003 struct type *type, struct dwarf2_cu *cu)
15004 {
15005 struct objfile *objfile = cu->per_objfile->objfile;
15006 struct attribute *attr;
15007 int i;
15008 struct fnfieldlist *flp = nullptr;
15009 struct fn_field *fnp;
15010 const char *fieldname;
15011 struct type *this_type;
15012 enum dwarf_access_attribute accessibility;
15013
15014 if (cu->language == language_ada)
15015 error (_("unexpected member function in Ada type"));
15016
15017 /* Get name of member function. */
15018 fieldname = dwarf2_name (die, cu);
15019 if (fieldname == NULL)
15020 return;
15021
15022 /* Look up member function name in fieldlist. */
15023 for (i = 0; i < fip->fnfieldlists.size (); i++)
15024 {
15025 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15026 {
15027 flp = &fip->fnfieldlists[i];
15028 break;
15029 }
15030 }
15031
15032 /* Create a new fnfieldlist if necessary. */
15033 if (flp == nullptr)
15034 {
15035 fip->fnfieldlists.emplace_back ();
15036 flp = &fip->fnfieldlists.back ();
15037 flp->name = fieldname;
15038 i = fip->fnfieldlists.size () - 1;
15039 }
15040
15041 /* Create a new member function field and add it to the vector of
15042 fnfieldlists. */
15043 flp->fnfields.emplace_back ();
15044 fnp = &flp->fnfields.back ();
15045
15046 /* Delay processing of the physname until later. */
15047 if (cu->language == language_cplus)
15048 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15049 die, cu);
15050 else
15051 {
15052 const char *physname = dwarf2_physname (fieldname, die, cu);
15053 fnp->physname = physname ? physname : "";
15054 }
15055
15056 fnp->type = alloc_type (objfile);
15057 this_type = read_type_die (die, cu);
15058 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15059 {
15060 int nparams = this_type->num_fields ();
15061
15062 /* TYPE is the domain of this method, and THIS_TYPE is the type
15063 of the method itself (TYPE_CODE_METHOD). */
15064 smash_to_method_type (fnp->type, type,
15065 TYPE_TARGET_TYPE (this_type),
15066 this_type->fields (),
15067 this_type->num_fields (),
15068 TYPE_VARARGS (this_type));
15069
15070 /* Handle static member functions.
15071 Dwarf2 has no clean way to discern C++ static and non-static
15072 member functions. G++ helps GDB by marking the first
15073 parameter for non-static member functions (which is the this
15074 pointer) as artificial. We obtain this information from
15075 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15076 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15077 fnp->voffset = VOFFSET_STATIC;
15078 }
15079 else
15080 complaint (_("member function type missing for '%s'"),
15081 dwarf2_full_name (fieldname, die, cu));
15082
15083 /* Get fcontext from DW_AT_containing_type if present. */
15084 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15085 fnp->fcontext = die_containing_type (die, cu);
15086
15087 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15088 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15089
15090 /* Get accessibility. */
15091 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15092 if (attr != nullptr)
15093 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15094 else
15095 accessibility = dwarf2_default_access_attribute (die, cu);
15096 switch (accessibility)
15097 {
15098 case DW_ACCESS_private:
15099 fnp->is_private = 1;
15100 break;
15101 case DW_ACCESS_protected:
15102 fnp->is_protected = 1;
15103 break;
15104 }
15105
15106 /* Check for artificial methods. */
15107 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15108 if (attr && DW_UNSND (attr) != 0)
15109 fnp->is_artificial = 1;
15110
15111 /* Check for defaulted methods. */
15112 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15113 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15114 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15115
15116 /* Check for deleted methods. */
15117 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15118 if (attr != nullptr && DW_UNSND (attr) != 0)
15119 fnp->is_deleted = 1;
15120
15121 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15122
15123 /* Get index in virtual function table if it is a virtual member
15124 function. For older versions of GCC, this is an offset in the
15125 appropriate virtual table, as specified by DW_AT_containing_type.
15126 For everyone else, it is an expression to be evaluated relative
15127 to the object address. */
15128
15129 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15130 if (attr != nullptr)
15131 {
15132 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15133 {
15134 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15135 {
15136 /* Old-style GCC. */
15137 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15138 }
15139 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15140 || (DW_BLOCK (attr)->size > 1
15141 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15142 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15143 {
15144 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15145 if ((fnp->voffset % cu->header.addr_size) != 0)
15146 dwarf2_complex_location_expr_complaint ();
15147 else
15148 fnp->voffset /= cu->header.addr_size;
15149 fnp->voffset += 2;
15150 }
15151 else
15152 dwarf2_complex_location_expr_complaint ();
15153
15154 if (!fnp->fcontext)
15155 {
15156 /* If there is no `this' field and no DW_AT_containing_type,
15157 we cannot actually find a base class context for the
15158 vtable! */
15159 if (this_type->num_fields () == 0
15160 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15161 {
15162 complaint (_("cannot determine context for virtual member "
15163 "function \"%s\" (offset %s)"),
15164 fieldname, sect_offset_str (die->sect_off));
15165 }
15166 else
15167 {
15168 fnp->fcontext
15169 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15170 }
15171 }
15172 }
15173 else if (attr->form_is_section_offset ())
15174 {
15175 dwarf2_complex_location_expr_complaint ();
15176 }
15177 else
15178 {
15179 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15180 fieldname);
15181 }
15182 }
15183 else
15184 {
15185 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15186 if (attr && DW_UNSND (attr))
15187 {
15188 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15189 complaint (_("Member function \"%s\" (offset %s) is virtual "
15190 "but the vtable offset is not specified"),
15191 fieldname, sect_offset_str (die->sect_off));
15192 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15193 TYPE_CPLUS_DYNAMIC (type) = 1;
15194 }
15195 }
15196 }
15197
15198 /* Create the vector of member function fields, and attach it to the type. */
15199
15200 static void
15201 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15202 struct dwarf2_cu *cu)
15203 {
15204 if (cu->language == language_ada)
15205 error (_("unexpected member functions in Ada type"));
15206
15207 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15208 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15209 TYPE_ALLOC (type,
15210 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15211
15212 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15213 {
15214 struct fnfieldlist &nf = fip->fnfieldlists[i];
15215 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15216
15217 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15218 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15219 fn_flp->fn_fields = (struct fn_field *)
15220 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15221
15222 for (int k = 0; k < nf.fnfields.size (); ++k)
15223 fn_flp->fn_fields[k] = nf.fnfields[k];
15224 }
15225
15226 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15227 }
15228
15229 /* Returns non-zero if NAME is the name of a vtable member in CU's
15230 language, zero otherwise. */
15231 static int
15232 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15233 {
15234 static const char vptr[] = "_vptr";
15235
15236 /* Look for the C++ form of the vtable. */
15237 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15238 return 1;
15239
15240 return 0;
15241 }
15242
15243 /* GCC outputs unnamed structures that are really pointers to member
15244 functions, with the ABI-specified layout. If TYPE describes
15245 such a structure, smash it into a member function type.
15246
15247 GCC shouldn't do this; it should just output pointer to member DIEs.
15248 This is GCC PR debug/28767. */
15249
15250 static void
15251 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15252 {
15253 struct type *pfn_type, *self_type, *new_type;
15254
15255 /* Check for a structure with no name and two children. */
15256 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15257 return;
15258
15259 /* Check for __pfn and __delta members. */
15260 if (TYPE_FIELD_NAME (type, 0) == NULL
15261 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15262 || TYPE_FIELD_NAME (type, 1) == NULL
15263 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15264 return;
15265
15266 /* Find the type of the method. */
15267 pfn_type = TYPE_FIELD_TYPE (type, 0);
15268 if (pfn_type == NULL
15269 || pfn_type->code () != TYPE_CODE_PTR
15270 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15271 return;
15272
15273 /* Look for the "this" argument. */
15274 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15275 if (pfn_type->num_fields () == 0
15276 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15277 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
15278 return;
15279
15280 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15281 new_type = alloc_type (objfile);
15282 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15283 pfn_type->fields (), pfn_type->num_fields (),
15284 TYPE_VARARGS (pfn_type));
15285 smash_to_methodptr_type (type, new_type);
15286 }
15287
15288 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15289 appropriate error checking and issuing complaints if there is a
15290 problem. */
15291
15292 static ULONGEST
15293 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15294 {
15295 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15296
15297 if (attr == nullptr)
15298 return 0;
15299
15300 if (!attr->form_is_constant ())
15301 {
15302 complaint (_("DW_AT_alignment must have constant form"
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
15309 ULONGEST align;
15310 if (attr->form == DW_FORM_sdata)
15311 {
15312 LONGEST val = DW_SND (attr);
15313 if (val < 0)
15314 {
15315 complaint (_("DW_AT_alignment value must not be negative"
15316 " - DIE at %s [in module %s]"),
15317 sect_offset_str (die->sect_off),
15318 objfile_name (cu->per_objfile->objfile));
15319 return 0;
15320 }
15321 align = val;
15322 }
15323 else
15324 align = DW_UNSND (attr);
15325
15326 if (align == 0)
15327 {
15328 complaint (_("DW_AT_alignment value must not be zero"
15329 " - DIE at %s [in module %s]"),
15330 sect_offset_str (die->sect_off),
15331 objfile_name (cu->per_objfile->objfile));
15332 return 0;
15333 }
15334 if ((align & (align - 1)) != 0)
15335 {
15336 complaint (_("DW_AT_alignment value must be a power of 2"
15337 " - DIE at %s [in module %s]"),
15338 sect_offset_str (die->sect_off),
15339 objfile_name (cu->per_objfile->objfile));
15340 return 0;
15341 }
15342
15343 return align;
15344 }
15345
15346 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15347 the alignment for TYPE. */
15348
15349 static void
15350 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15351 struct type *type)
15352 {
15353 if (!set_type_align (type, get_alignment (cu, die)))
15354 complaint (_("DW_AT_alignment value too large"
15355 " - DIE at %s [in module %s]"),
15356 sect_offset_str (die->sect_off),
15357 objfile_name (cu->per_objfile->objfile));
15358 }
15359
15360 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15361 constant for a type, according to DWARF5 spec, Table 5.5. */
15362
15363 static bool
15364 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15365 {
15366 switch (value)
15367 {
15368 case DW_CC_normal:
15369 case DW_CC_pass_by_reference:
15370 case DW_CC_pass_by_value:
15371 return true;
15372
15373 default:
15374 complaint (_("unrecognized DW_AT_calling_convention value "
15375 "(%s) for a type"), pulongest (value));
15376 return false;
15377 }
15378 }
15379
15380 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15381 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15382 also according to GNU-specific values (see include/dwarf2.h). */
15383
15384 static bool
15385 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15386 {
15387 switch (value)
15388 {
15389 case DW_CC_normal:
15390 case DW_CC_program:
15391 case DW_CC_nocall:
15392 return true;
15393
15394 case DW_CC_GNU_renesas_sh:
15395 case DW_CC_GNU_borland_fastcall_i386:
15396 case DW_CC_GDB_IBM_OpenCL:
15397 return true;
15398
15399 default:
15400 complaint (_("unrecognized DW_AT_calling_convention value "
15401 "(%s) for a subroutine"), pulongest (value));
15402 return false;
15403 }
15404 }
15405
15406 /* Called when we find the DIE that starts a structure or union scope
15407 (definition) to create a type for the structure or union. Fill in
15408 the type's name and general properties; the members will not be
15409 processed until process_structure_scope. A symbol table entry for
15410 the type will also not be done until process_structure_scope (assuming
15411 the type has a name).
15412
15413 NOTE: we need to call these functions regardless of whether or not the
15414 DIE has a DW_AT_name attribute, since it might be an anonymous
15415 structure or union. This gets the type entered into our set of
15416 user defined types. */
15417
15418 static struct type *
15419 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15420 {
15421 struct objfile *objfile = cu->per_objfile->objfile;
15422 struct type *type;
15423 struct attribute *attr;
15424 const char *name;
15425
15426 /* If the definition of this type lives in .debug_types, read that type.
15427 Don't follow DW_AT_specification though, that will take us back up
15428 the chain and we want to go down. */
15429 attr = die->attr (DW_AT_signature);
15430 if (attr != nullptr)
15431 {
15432 type = get_DW_AT_signature_type (die, attr, cu);
15433
15434 /* The type's CU may not be the same as CU.
15435 Ensure TYPE is recorded with CU in die_type_hash. */
15436 return set_die_type (die, type, cu);
15437 }
15438
15439 type = alloc_type (objfile);
15440 INIT_CPLUS_SPECIFIC (type);
15441
15442 name = dwarf2_name (die, cu);
15443 if (name != NULL)
15444 {
15445 if (cu->language == language_cplus
15446 || cu->language == language_d
15447 || cu->language == language_rust)
15448 {
15449 const char *full_name = dwarf2_full_name (name, die, cu);
15450
15451 /* dwarf2_full_name might have already finished building the DIE's
15452 type. If so, there is no need to continue. */
15453 if (get_die_type (die, cu) != NULL)
15454 return get_die_type (die, cu);
15455
15456 type->set_name (full_name);
15457 }
15458 else
15459 {
15460 /* The name is already allocated along with this objfile, so
15461 we don't need to duplicate it for the type. */
15462 type->set_name (name);
15463 }
15464 }
15465
15466 if (die->tag == DW_TAG_structure_type)
15467 {
15468 type->set_code (TYPE_CODE_STRUCT);
15469 }
15470 else if (die->tag == DW_TAG_union_type)
15471 {
15472 type->set_code (TYPE_CODE_UNION);
15473 }
15474 else
15475 {
15476 type->set_code (TYPE_CODE_STRUCT);
15477 }
15478
15479 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15480 TYPE_DECLARED_CLASS (type) = 1;
15481
15482 /* Store the calling convention in the type if it's available in
15483 the die. Otherwise the calling convention remains set to
15484 the default value DW_CC_normal. */
15485 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15486 if (attr != nullptr
15487 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15488 {
15489 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15490 TYPE_CPLUS_CALLING_CONVENTION (type)
15491 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15492 }
15493
15494 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15495 if (attr != nullptr)
15496 {
15497 if (attr->form_is_constant ())
15498 TYPE_LENGTH (type) = DW_UNSND (attr);
15499 else
15500 {
15501 struct dynamic_prop prop;
15502 if (attr_to_dynamic_prop (attr, die, cu, &prop,
15503 cu->per_cu->addr_type ()))
15504 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15505 TYPE_LENGTH (type) = 0;
15506 }
15507 }
15508 else
15509 {
15510 TYPE_LENGTH (type) = 0;
15511 }
15512
15513 maybe_set_alignment (cu, die, type);
15514
15515 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15516 {
15517 /* ICC<14 does not output the required DW_AT_declaration on
15518 incomplete types, but gives them a size of zero. */
15519 TYPE_STUB (type) = 1;
15520 }
15521 else
15522 TYPE_STUB_SUPPORTED (type) = 1;
15523
15524 if (die_is_declaration (die, cu))
15525 TYPE_STUB (type) = 1;
15526 else if (attr == NULL && die->child == NULL
15527 && producer_is_realview (cu->producer))
15528 /* RealView does not output the required DW_AT_declaration
15529 on incomplete types. */
15530 TYPE_STUB (type) = 1;
15531
15532 /* We need to add the type field to the die immediately so we don't
15533 infinitely recurse when dealing with pointers to the structure
15534 type within the structure itself. */
15535 set_die_type (die, type, cu);
15536
15537 /* set_die_type should be already done. */
15538 set_descriptive_type (type, die, cu);
15539
15540 return type;
15541 }
15542
15543 static void handle_struct_member_die
15544 (struct die_info *child_die,
15545 struct type *type,
15546 struct field_info *fi,
15547 std::vector<struct symbol *> *template_args,
15548 struct dwarf2_cu *cu);
15549
15550 /* A helper for handle_struct_member_die that handles
15551 DW_TAG_variant_part. */
15552
15553 static void
15554 handle_variant_part (struct die_info *die, struct type *type,
15555 struct field_info *fi,
15556 std::vector<struct symbol *> *template_args,
15557 struct dwarf2_cu *cu)
15558 {
15559 variant_part_builder *new_part;
15560 if (fi->current_variant_part == nullptr)
15561 {
15562 fi->variant_parts.emplace_back ();
15563 new_part = &fi->variant_parts.back ();
15564 }
15565 else if (!fi->current_variant_part->processing_variant)
15566 {
15567 complaint (_("nested DW_TAG_variant_part seen "
15568 "- DIE at %s [in module %s]"),
15569 sect_offset_str (die->sect_off),
15570 objfile_name (cu->per_objfile->objfile));
15571 return;
15572 }
15573 else
15574 {
15575 variant_field &current = fi->current_variant_part->variants.back ();
15576 current.variant_parts.emplace_back ();
15577 new_part = &current.variant_parts.back ();
15578 }
15579
15580 /* When we recurse, we want callees to add to this new variant
15581 part. */
15582 scoped_restore save_current_variant_part
15583 = make_scoped_restore (&fi->current_variant_part, new_part);
15584
15585 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15586 if (discr == NULL)
15587 {
15588 /* It's a univariant form, an extension we support. */
15589 }
15590 else if (discr->form_is_ref ())
15591 {
15592 struct dwarf2_cu *target_cu = cu;
15593 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15594
15595 new_part->discriminant_offset = target_die->sect_off;
15596 }
15597 else
15598 {
15599 complaint (_("DW_AT_discr does not have DIE reference form"
15600 " - DIE at %s [in module %s]"),
15601 sect_offset_str (die->sect_off),
15602 objfile_name (cu->per_objfile->objfile));
15603 }
15604
15605 for (die_info *child_die = die->child;
15606 child_die != NULL;
15607 child_die = child_die->sibling)
15608 handle_struct_member_die (child_die, type, fi, template_args, cu);
15609 }
15610
15611 /* A helper for handle_struct_member_die that handles
15612 DW_TAG_variant. */
15613
15614 static void
15615 handle_variant (struct die_info *die, struct type *type,
15616 struct field_info *fi,
15617 std::vector<struct symbol *> *template_args,
15618 struct dwarf2_cu *cu)
15619 {
15620 if (fi->current_variant_part == nullptr)
15621 {
15622 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15623 "- DIE at %s [in module %s]"),
15624 sect_offset_str (die->sect_off),
15625 objfile_name (cu->per_objfile->objfile));
15626 return;
15627 }
15628 if (fi->current_variant_part->processing_variant)
15629 {
15630 complaint (_("nested DW_TAG_variant seen "
15631 "- DIE at %s [in module %s]"),
15632 sect_offset_str (die->sect_off),
15633 objfile_name (cu->per_objfile->objfile));
15634 return;
15635 }
15636
15637 scoped_restore save_processing_variant
15638 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15639 true);
15640
15641 fi->current_variant_part->variants.emplace_back ();
15642 variant_field &variant = fi->current_variant_part->variants.back ();
15643 variant.first_field = fi->fields.size ();
15644
15645 /* In a variant we want to get the discriminant and also add a
15646 field for our sole member child. */
15647 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15648 if (discr == nullptr)
15649 {
15650 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15651 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15652 variant.default_branch = true;
15653 else
15654 variant.discr_list_data = DW_BLOCK (discr);
15655 }
15656 else
15657 variant.discriminant_value = DW_UNSND (discr);
15658
15659 for (die_info *variant_child = die->child;
15660 variant_child != NULL;
15661 variant_child = variant_child->sibling)
15662 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15663
15664 variant.last_field = fi->fields.size ();
15665 }
15666
15667 /* A helper for process_structure_scope that handles a single member
15668 DIE. */
15669
15670 static void
15671 handle_struct_member_die (struct die_info *child_die, struct type *type,
15672 struct field_info *fi,
15673 std::vector<struct symbol *> *template_args,
15674 struct dwarf2_cu *cu)
15675 {
15676 if (child_die->tag == DW_TAG_member
15677 || child_die->tag == DW_TAG_variable)
15678 {
15679 /* NOTE: carlton/2002-11-05: A C++ static data member
15680 should be a DW_TAG_member that is a declaration, but
15681 all versions of G++ as of this writing (so through at
15682 least 3.2.1) incorrectly generate DW_TAG_variable
15683 tags for them instead. */
15684 dwarf2_add_field (fi, child_die, cu);
15685 }
15686 else if (child_die->tag == DW_TAG_subprogram)
15687 {
15688 /* Rust doesn't have member functions in the C++ sense.
15689 However, it does emit ordinary functions as children
15690 of a struct DIE. */
15691 if (cu->language == language_rust)
15692 read_func_scope (child_die, cu);
15693 else
15694 {
15695 /* C++ member function. */
15696 dwarf2_add_member_fn (fi, child_die, type, cu);
15697 }
15698 }
15699 else if (child_die->tag == DW_TAG_inheritance)
15700 {
15701 /* C++ base class field. */
15702 dwarf2_add_field (fi, child_die, cu);
15703 }
15704 else if (type_can_define_types (child_die))
15705 dwarf2_add_type_defn (fi, child_die, cu);
15706 else if (child_die->tag == DW_TAG_template_type_param
15707 || child_die->tag == DW_TAG_template_value_param)
15708 {
15709 struct symbol *arg = new_symbol (child_die, NULL, cu);
15710
15711 if (arg != NULL)
15712 template_args->push_back (arg);
15713 }
15714 else if (child_die->tag == DW_TAG_variant_part)
15715 handle_variant_part (child_die, type, fi, template_args, cu);
15716 else if (child_die->tag == DW_TAG_variant)
15717 handle_variant (child_die, type, fi, template_args, cu);
15718 }
15719
15720 /* Finish creating a structure or union type, including filling in
15721 its members and creating a symbol for it. */
15722
15723 static void
15724 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15725 {
15726 struct objfile *objfile = cu->per_objfile->objfile;
15727 struct die_info *child_die;
15728 struct type *type;
15729
15730 type = get_die_type (die, cu);
15731 if (type == NULL)
15732 type = read_structure_type (die, cu);
15733
15734 bool has_template_parameters = false;
15735 if (die->child != NULL && ! die_is_declaration (die, cu))
15736 {
15737 struct field_info fi;
15738 std::vector<struct symbol *> template_args;
15739
15740 child_die = die->child;
15741
15742 while (child_die && child_die->tag)
15743 {
15744 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15745 child_die = child_die->sibling;
15746 }
15747
15748 /* Attach template arguments to type. */
15749 if (!template_args.empty ())
15750 {
15751 has_template_parameters = true;
15752 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15753 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15754 TYPE_TEMPLATE_ARGUMENTS (type)
15755 = XOBNEWVEC (&objfile->objfile_obstack,
15756 struct symbol *,
15757 TYPE_N_TEMPLATE_ARGUMENTS (type));
15758 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15759 template_args.data (),
15760 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15761 * sizeof (struct symbol *)));
15762 }
15763
15764 /* Attach fields and member functions to the type. */
15765 if (fi.nfields () > 0)
15766 dwarf2_attach_fields_to_type (&fi, type, cu);
15767 if (!fi.fnfieldlists.empty ())
15768 {
15769 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15770
15771 /* Get the type which refers to the base class (possibly this
15772 class itself) which contains the vtable pointer for the current
15773 class from the DW_AT_containing_type attribute. This use of
15774 DW_AT_containing_type is a GNU extension. */
15775
15776 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15777 {
15778 struct type *t = die_containing_type (die, cu);
15779
15780 set_type_vptr_basetype (type, t);
15781 if (type == t)
15782 {
15783 int i;
15784
15785 /* Our own class provides vtbl ptr. */
15786 for (i = t->num_fields () - 1;
15787 i >= TYPE_N_BASECLASSES (t);
15788 --i)
15789 {
15790 const char *fieldname = TYPE_FIELD_NAME (t, i);
15791
15792 if (is_vtable_name (fieldname, cu))
15793 {
15794 set_type_vptr_fieldno (type, i);
15795 break;
15796 }
15797 }
15798
15799 /* Complain if virtual function table field not found. */
15800 if (i < TYPE_N_BASECLASSES (t))
15801 complaint (_("virtual function table pointer "
15802 "not found when defining class '%s'"),
15803 type->name () ? type->name () : "");
15804 }
15805 else
15806 {
15807 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15808 }
15809 }
15810 else if (cu->producer
15811 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15812 {
15813 /* The IBM XLC compiler does not provide direct indication
15814 of the containing type, but the vtable pointer is
15815 always named __vfp. */
15816
15817 int i;
15818
15819 for (i = type->num_fields () - 1;
15820 i >= TYPE_N_BASECLASSES (type);
15821 --i)
15822 {
15823 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15824 {
15825 set_type_vptr_fieldno (type, i);
15826 set_type_vptr_basetype (type, type);
15827 break;
15828 }
15829 }
15830 }
15831 }
15832
15833 /* Copy fi.typedef_field_list linked list elements content into the
15834 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15835 if (!fi.typedef_field_list.empty ())
15836 {
15837 int count = fi.typedef_field_list.size ();
15838
15839 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15840 TYPE_TYPEDEF_FIELD_ARRAY (type)
15841 = ((struct decl_field *)
15842 TYPE_ALLOC (type,
15843 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15844 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15845
15846 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15847 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15848 }
15849
15850 /* Copy fi.nested_types_list linked list elements content into the
15851 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15852 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15853 {
15854 int count = fi.nested_types_list.size ();
15855
15856 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15857 TYPE_NESTED_TYPES_ARRAY (type)
15858 = ((struct decl_field *)
15859 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15860 TYPE_NESTED_TYPES_COUNT (type) = count;
15861
15862 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15863 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15864 }
15865 }
15866
15867 quirk_gcc_member_function_pointer (type, objfile);
15868 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15869 cu->rust_unions.push_back (type);
15870
15871 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15872 snapshots) has been known to create a die giving a declaration
15873 for a class that has, as a child, a die giving a definition for a
15874 nested class. So we have to process our children even if the
15875 current die is a declaration. Normally, of course, a declaration
15876 won't have any children at all. */
15877
15878 child_die = die->child;
15879
15880 while (child_die != NULL && child_die->tag)
15881 {
15882 if (child_die->tag == DW_TAG_member
15883 || child_die->tag == DW_TAG_variable
15884 || child_die->tag == DW_TAG_inheritance
15885 || child_die->tag == DW_TAG_template_value_param
15886 || child_die->tag == DW_TAG_template_type_param)
15887 {
15888 /* Do nothing. */
15889 }
15890 else
15891 process_die (child_die, cu);
15892
15893 child_die = child_die->sibling;
15894 }
15895
15896 /* Do not consider external references. According to the DWARF standard,
15897 these DIEs are identified by the fact that they have no byte_size
15898 attribute, and a declaration attribute. */
15899 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15900 || !die_is_declaration (die, cu)
15901 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15902 {
15903 struct symbol *sym = new_symbol (die, type, cu);
15904
15905 if (has_template_parameters)
15906 {
15907 struct symtab *symtab;
15908 if (sym != nullptr)
15909 symtab = symbol_symtab (sym);
15910 else if (cu->line_header != nullptr)
15911 {
15912 /* Any related symtab will do. */
15913 symtab
15914 = cu->line_header->file_names ()[0].symtab;
15915 }
15916 else
15917 {
15918 symtab = nullptr;
15919 complaint (_("could not find suitable "
15920 "symtab for template parameter"
15921 " - DIE at %s [in module %s]"),
15922 sect_offset_str (die->sect_off),
15923 objfile_name (objfile));
15924 }
15925
15926 if (symtab != nullptr)
15927 {
15928 /* Make sure that the symtab is set on the new symbols.
15929 Even though they don't appear in this symtab directly,
15930 other parts of gdb assume that symbols do, and this is
15931 reasonably true. */
15932 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15933 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15934 }
15935 }
15936 }
15937 }
15938
15939 /* Assuming DIE is an enumeration type, and TYPE is its associated
15940 type, update TYPE using some information only available in DIE's
15941 children. In particular, the fields are computed. */
15942
15943 static void
15944 update_enumeration_type_from_children (struct die_info *die,
15945 struct type *type,
15946 struct dwarf2_cu *cu)
15947 {
15948 struct die_info *child_die;
15949 int unsigned_enum = 1;
15950 int flag_enum = 1;
15951
15952 auto_obstack obstack;
15953 std::vector<struct field> fields;
15954
15955 for (child_die = die->child;
15956 child_die != NULL && child_die->tag;
15957 child_die = child_die->sibling)
15958 {
15959 struct attribute *attr;
15960 LONGEST value;
15961 const gdb_byte *bytes;
15962 struct dwarf2_locexpr_baton *baton;
15963 const char *name;
15964
15965 if (child_die->tag != DW_TAG_enumerator)
15966 continue;
15967
15968 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15969 if (attr == NULL)
15970 continue;
15971
15972 name = dwarf2_name (child_die, cu);
15973 if (name == NULL)
15974 name = "<anonymous enumerator>";
15975
15976 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15977 &value, &bytes, &baton);
15978 if (value < 0)
15979 {
15980 unsigned_enum = 0;
15981 flag_enum = 0;
15982 }
15983 else
15984 {
15985 if (count_one_bits_ll (value) >= 2)
15986 flag_enum = 0;
15987 }
15988
15989 fields.emplace_back ();
15990 struct field &field = fields.back ();
15991 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
15992 SET_FIELD_ENUMVAL (field, value);
15993 }
15994
15995 if (!fields.empty ())
15996 {
15997 type->set_num_fields (fields.size ());
15998 type->set_fields
15999 ((struct field *)
16000 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16001 memcpy (type->fields (), fields.data (),
16002 sizeof (struct field) * fields.size ());
16003 }
16004
16005 if (unsigned_enum)
16006 TYPE_UNSIGNED (type) = 1;
16007 if (flag_enum)
16008 TYPE_FLAG_ENUM (type) = 1;
16009 }
16010
16011 /* Given a DW_AT_enumeration_type die, set its type. We do not
16012 complete the type's fields yet, or create any symbols. */
16013
16014 static struct type *
16015 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16016 {
16017 struct objfile *objfile = cu->per_objfile->objfile;
16018 struct type *type;
16019 struct attribute *attr;
16020 const char *name;
16021
16022 /* If the definition of this type lives in .debug_types, read that type.
16023 Don't follow DW_AT_specification though, that will take us back up
16024 the chain and we want to go down. */
16025 attr = die->attr (DW_AT_signature);
16026 if (attr != nullptr)
16027 {
16028 type = get_DW_AT_signature_type (die, attr, cu);
16029
16030 /* The type's CU may not be the same as CU.
16031 Ensure TYPE is recorded with CU in die_type_hash. */
16032 return set_die_type (die, type, cu);
16033 }
16034
16035 type = alloc_type (objfile);
16036
16037 type->set_code (TYPE_CODE_ENUM);
16038 name = dwarf2_full_name (NULL, die, cu);
16039 if (name != NULL)
16040 type->set_name (name);
16041
16042 attr = dwarf2_attr (die, DW_AT_type, cu);
16043 if (attr != NULL)
16044 {
16045 struct type *underlying_type = die_type (die, cu);
16046
16047 TYPE_TARGET_TYPE (type) = underlying_type;
16048 }
16049
16050 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16051 if (attr != nullptr)
16052 {
16053 TYPE_LENGTH (type) = DW_UNSND (attr);
16054 }
16055 else
16056 {
16057 TYPE_LENGTH (type) = 0;
16058 }
16059
16060 maybe_set_alignment (cu, die, type);
16061
16062 /* The enumeration DIE can be incomplete. In Ada, any type can be
16063 declared as private in the package spec, and then defined only
16064 inside the package body. Such types are known as Taft Amendment
16065 Types. When another package uses such a type, an incomplete DIE
16066 may be generated by the compiler. */
16067 if (die_is_declaration (die, cu))
16068 TYPE_STUB (type) = 1;
16069
16070 /* If this type has an underlying type that is not a stub, then we
16071 may use its attributes. We always use the "unsigned" attribute
16072 in this situation, because ordinarily we guess whether the type
16073 is unsigned -- but the guess can be wrong and the underlying type
16074 can tell us the reality. However, we defer to a local size
16075 attribute if one exists, because this lets the compiler override
16076 the underlying type if needed. */
16077 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16078 {
16079 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16080 underlying_type = check_typedef (underlying_type);
16081 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
16082 if (TYPE_LENGTH (type) == 0)
16083 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16084 if (TYPE_RAW_ALIGN (type) == 0
16085 && TYPE_RAW_ALIGN (underlying_type) != 0)
16086 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16087 }
16088
16089 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16090
16091 set_die_type (die, type, cu);
16092
16093 /* Finish the creation of this type by using the enum's children.
16094 Note that, as usual, this must come after set_die_type to avoid
16095 infinite recursion when trying to compute the names of the
16096 enumerators. */
16097 update_enumeration_type_from_children (die, type, cu);
16098
16099 return type;
16100 }
16101
16102 /* Given a pointer to a die which begins an enumeration, process all
16103 the dies that define the members of the enumeration, and create the
16104 symbol for the enumeration type.
16105
16106 NOTE: We reverse the order of the element list. */
16107
16108 static void
16109 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16110 {
16111 struct type *this_type;
16112
16113 this_type = get_die_type (die, cu);
16114 if (this_type == NULL)
16115 this_type = read_enumeration_type (die, cu);
16116
16117 if (die->child != NULL)
16118 {
16119 struct die_info *child_die;
16120 const char *name;
16121
16122 child_die = die->child;
16123 while (child_die && child_die->tag)
16124 {
16125 if (child_die->tag != DW_TAG_enumerator)
16126 {
16127 process_die (child_die, cu);
16128 }
16129 else
16130 {
16131 name = dwarf2_name (child_die, cu);
16132 if (name)
16133 new_symbol (child_die, this_type, cu);
16134 }
16135
16136 child_die = child_die->sibling;
16137 }
16138 }
16139
16140 /* If we are reading an enum from a .debug_types unit, and the enum
16141 is a declaration, and the enum is not the signatured type in the
16142 unit, then we do not want to add a symbol for it. Adding a
16143 symbol would in some cases obscure the true definition of the
16144 enum, giving users an incomplete type when the definition is
16145 actually available. Note that we do not want to do this for all
16146 enums which are just declarations, because C++0x allows forward
16147 enum declarations. */
16148 if (cu->per_cu->is_debug_types
16149 && die_is_declaration (die, cu))
16150 {
16151 struct signatured_type *sig_type;
16152
16153 sig_type = (struct signatured_type *) cu->per_cu;
16154 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16155 if (sig_type->type_offset_in_section != die->sect_off)
16156 return;
16157 }
16158
16159 new_symbol (die, this_type, cu);
16160 }
16161
16162 /* Extract all information from a DW_TAG_array_type DIE and put it in
16163 the DIE's type field. For now, this only handles one dimensional
16164 arrays. */
16165
16166 static struct type *
16167 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16168 {
16169 struct objfile *objfile = cu->per_objfile->objfile;
16170 struct die_info *child_die;
16171 struct type *type;
16172 struct type *element_type, *range_type, *index_type;
16173 struct attribute *attr;
16174 const char *name;
16175 struct dynamic_prop *byte_stride_prop = NULL;
16176 unsigned int bit_stride = 0;
16177
16178 element_type = die_type (die, cu);
16179
16180 /* The die_type call above may have already set the type for this DIE. */
16181 type = get_die_type (die, cu);
16182 if (type)
16183 return type;
16184
16185 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16186 if (attr != NULL)
16187 {
16188 int stride_ok;
16189 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
16190
16191 byte_stride_prop
16192 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16193 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16194 prop_type);
16195 if (!stride_ok)
16196 {
16197 complaint (_("unable to read array DW_AT_byte_stride "
16198 " - DIE at %s [in module %s]"),
16199 sect_offset_str (die->sect_off),
16200 objfile_name (cu->per_objfile->objfile));
16201 /* Ignore this attribute. We will likely not be able to print
16202 arrays of this type correctly, but there is little we can do
16203 to help if we cannot read the attribute's value. */
16204 byte_stride_prop = NULL;
16205 }
16206 }
16207
16208 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16209 if (attr != NULL)
16210 bit_stride = DW_UNSND (attr);
16211
16212 /* Irix 6.2 native cc creates array types without children for
16213 arrays with unspecified length. */
16214 if (die->child == NULL)
16215 {
16216 index_type = objfile_type (objfile)->builtin_int;
16217 range_type = create_static_range_type (NULL, index_type, 0, -1);
16218 type = create_array_type_with_stride (NULL, element_type, range_type,
16219 byte_stride_prop, bit_stride);
16220 return set_die_type (die, type, cu);
16221 }
16222
16223 std::vector<struct type *> range_types;
16224 child_die = die->child;
16225 while (child_die && child_die->tag)
16226 {
16227 if (child_die->tag == DW_TAG_subrange_type)
16228 {
16229 struct type *child_type = read_type_die (child_die, cu);
16230
16231 if (child_type != NULL)
16232 {
16233 /* The range type was succesfully read. Save it for the
16234 array type creation. */
16235 range_types.push_back (child_type);
16236 }
16237 }
16238 child_die = child_die->sibling;
16239 }
16240
16241 /* Dwarf2 dimensions are output from left to right, create the
16242 necessary array types in backwards order. */
16243
16244 type = element_type;
16245
16246 if (read_array_order (die, cu) == DW_ORD_col_major)
16247 {
16248 int i = 0;
16249
16250 while (i < range_types.size ())
16251 type = create_array_type_with_stride (NULL, type, range_types[i++],
16252 byte_stride_prop, bit_stride);
16253 }
16254 else
16255 {
16256 size_t ndim = range_types.size ();
16257 while (ndim-- > 0)
16258 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16259 byte_stride_prop, bit_stride);
16260 }
16261
16262 /* Understand Dwarf2 support for vector types (like they occur on
16263 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16264 array type. This is not part of the Dwarf2/3 standard yet, but a
16265 custom vendor extension. The main difference between a regular
16266 array and the vector variant is that vectors are passed by value
16267 to functions. */
16268 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16269 if (attr != nullptr)
16270 make_vector_type (type);
16271
16272 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16273 implementation may choose to implement triple vectors using this
16274 attribute. */
16275 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16276 if (attr != nullptr)
16277 {
16278 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16279 TYPE_LENGTH (type) = DW_UNSND (attr);
16280 else
16281 complaint (_("DW_AT_byte_size for array type smaller "
16282 "than the total size of elements"));
16283 }
16284
16285 name = dwarf2_name (die, cu);
16286 if (name)
16287 type->set_name (name);
16288
16289 maybe_set_alignment (cu, die, type);
16290
16291 /* Install the type in the die. */
16292 set_die_type (die, type, cu);
16293
16294 /* set_die_type should be already done. */
16295 set_descriptive_type (type, die, cu);
16296
16297 return type;
16298 }
16299
16300 static enum dwarf_array_dim_ordering
16301 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16302 {
16303 struct attribute *attr;
16304
16305 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16306
16307 if (attr != nullptr)
16308 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16309
16310 /* GNU F77 is a special case, as at 08/2004 array type info is the
16311 opposite order to the dwarf2 specification, but data is still
16312 laid out as per normal fortran.
16313
16314 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16315 version checking. */
16316
16317 if (cu->language == language_fortran
16318 && cu->producer && strstr (cu->producer, "GNU F77"))
16319 {
16320 return DW_ORD_row_major;
16321 }
16322
16323 switch (cu->language_defn->la_array_ordering)
16324 {
16325 case array_column_major:
16326 return DW_ORD_col_major;
16327 case array_row_major:
16328 default:
16329 return DW_ORD_row_major;
16330 };
16331 }
16332
16333 /* Extract all information from a DW_TAG_set_type DIE and put it in
16334 the DIE's type field. */
16335
16336 static struct type *
16337 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16338 {
16339 struct type *domain_type, *set_type;
16340 struct attribute *attr;
16341
16342 domain_type = die_type (die, cu);
16343
16344 /* The die_type call above may have already set the type for this DIE. */
16345 set_type = get_die_type (die, cu);
16346 if (set_type)
16347 return set_type;
16348
16349 set_type = create_set_type (NULL, domain_type);
16350
16351 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16352 if (attr != nullptr)
16353 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16354
16355 maybe_set_alignment (cu, die, set_type);
16356
16357 return set_die_type (die, set_type, cu);
16358 }
16359
16360 /* A helper for read_common_block that creates a locexpr baton.
16361 SYM is the symbol which we are marking as computed.
16362 COMMON_DIE is the DIE for the common block.
16363 COMMON_LOC is the location expression attribute for the common
16364 block itself.
16365 MEMBER_LOC is the location expression attribute for the particular
16366 member of the common block that we are processing.
16367 CU is the CU from which the above come. */
16368
16369 static void
16370 mark_common_block_symbol_computed (struct symbol *sym,
16371 struct die_info *common_die,
16372 struct attribute *common_loc,
16373 struct attribute *member_loc,
16374 struct dwarf2_cu *cu)
16375 {
16376 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16377 struct objfile *objfile = per_objfile->objfile;
16378 struct dwarf2_locexpr_baton *baton;
16379 gdb_byte *ptr;
16380 unsigned int cu_off;
16381 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16382 LONGEST offset = 0;
16383
16384 gdb_assert (common_loc && member_loc);
16385 gdb_assert (common_loc->form_is_block ());
16386 gdb_assert (member_loc->form_is_block ()
16387 || member_loc->form_is_constant ());
16388
16389 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16390 baton->per_objfile = per_objfile;
16391 baton->per_cu = cu->per_cu;
16392 gdb_assert (baton->per_cu);
16393
16394 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16395
16396 if (member_loc->form_is_constant ())
16397 {
16398 offset = member_loc->constant_value (0);
16399 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16400 }
16401 else
16402 baton->size += DW_BLOCK (member_loc)->size;
16403
16404 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16405 baton->data = ptr;
16406
16407 *ptr++ = DW_OP_call4;
16408 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16409 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16410 ptr += 4;
16411
16412 if (member_loc->form_is_constant ())
16413 {
16414 *ptr++ = DW_OP_addr;
16415 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16416 ptr += cu->header.addr_size;
16417 }
16418 else
16419 {
16420 /* We have to copy the data here, because DW_OP_call4 will only
16421 use a DW_AT_location attribute. */
16422 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16423 ptr += DW_BLOCK (member_loc)->size;
16424 }
16425
16426 *ptr++ = DW_OP_plus;
16427 gdb_assert (ptr - baton->data == baton->size);
16428
16429 SYMBOL_LOCATION_BATON (sym) = baton;
16430 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16431 }
16432
16433 /* Create appropriate locally-scoped variables for all the
16434 DW_TAG_common_block entries. Also create a struct common_block
16435 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16436 is used to separate the common blocks name namespace from regular
16437 variable names. */
16438
16439 static void
16440 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16441 {
16442 struct attribute *attr;
16443
16444 attr = dwarf2_attr (die, DW_AT_location, cu);
16445 if (attr != nullptr)
16446 {
16447 /* Support the .debug_loc offsets. */
16448 if (attr->form_is_block ())
16449 {
16450 /* Ok. */
16451 }
16452 else if (attr->form_is_section_offset ())
16453 {
16454 dwarf2_complex_location_expr_complaint ();
16455 attr = NULL;
16456 }
16457 else
16458 {
16459 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16460 "common block member");
16461 attr = NULL;
16462 }
16463 }
16464
16465 if (die->child != NULL)
16466 {
16467 struct objfile *objfile = cu->per_objfile->objfile;
16468 struct die_info *child_die;
16469 size_t n_entries = 0, size;
16470 struct common_block *common_block;
16471 struct symbol *sym;
16472
16473 for (child_die = die->child;
16474 child_die && child_die->tag;
16475 child_die = child_die->sibling)
16476 ++n_entries;
16477
16478 size = (sizeof (struct common_block)
16479 + (n_entries - 1) * sizeof (struct symbol *));
16480 common_block
16481 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16482 size);
16483 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16484 common_block->n_entries = 0;
16485
16486 for (child_die = die->child;
16487 child_die && child_die->tag;
16488 child_die = child_die->sibling)
16489 {
16490 /* Create the symbol in the DW_TAG_common_block block in the current
16491 symbol scope. */
16492 sym = new_symbol (child_die, NULL, cu);
16493 if (sym != NULL)
16494 {
16495 struct attribute *member_loc;
16496
16497 common_block->contents[common_block->n_entries++] = sym;
16498
16499 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16500 cu);
16501 if (member_loc)
16502 {
16503 /* GDB has handled this for a long time, but it is
16504 not specified by DWARF. It seems to have been
16505 emitted by gfortran at least as recently as:
16506 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16507 complaint (_("Variable in common block has "
16508 "DW_AT_data_member_location "
16509 "- DIE at %s [in module %s]"),
16510 sect_offset_str (child_die->sect_off),
16511 objfile_name (objfile));
16512
16513 if (member_loc->form_is_section_offset ())
16514 dwarf2_complex_location_expr_complaint ();
16515 else if (member_loc->form_is_constant ()
16516 || member_loc->form_is_block ())
16517 {
16518 if (attr != nullptr)
16519 mark_common_block_symbol_computed (sym, die, attr,
16520 member_loc, cu);
16521 }
16522 else
16523 dwarf2_complex_location_expr_complaint ();
16524 }
16525 }
16526 }
16527
16528 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16529 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16530 }
16531 }
16532
16533 /* Create a type for a C++ namespace. */
16534
16535 static struct type *
16536 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16537 {
16538 struct objfile *objfile = cu->per_objfile->objfile;
16539 const char *previous_prefix, *name;
16540 int is_anonymous;
16541 struct type *type;
16542
16543 /* For extensions, reuse the type of the original namespace. */
16544 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16545 {
16546 struct die_info *ext_die;
16547 struct dwarf2_cu *ext_cu = cu;
16548
16549 ext_die = dwarf2_extension (die, &ext_cu);
16550 type = read_type_die (ext_die, ext_cu);
16551
16552 /* EXT_CU may not be the same as CU.
16553 Ensure TYPE is recorded with CU in die_type_hash. */
16554 return set_die_type (die, type, cu);
16555 }
16556
16557 name = namespace_name (die, &is_anonymous, cu);
16558
16559 /* Now build the name of the current namespace. */
16560
16561 previous_prefix = determine_prefix (die, cu);
16562 if (previous_prefix[0] != '\0')
16563 name = typename_concat (&objfile->objfile_obstack,
16564 previous_prefix, name, 0, cu);
16565
16566 /* Create the type. */
16567 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16568
16569 return set_die_type (die, type, cu);
16570 }
16571
16572 /* Read a namespace scope. */
16573
16574 static void
16575 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16576 {
16577 struct objfile *objfile = cu->per_objfile->objfile;
16578 int is_anonymous;
16579
16580 /* Add a symbol associated to this if we haven't seen the namespace
16581 before. Also, add a using directive if it's an anonymous
16582 namespace. */
16583
16584 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16585 {
16586 struct type *type;
16587
16588 type = read_type_die (die, cu);
16589 new_symbol (die, type, cu);
16590
16591 namespace_name (die, &is_anonymous, cu);
16592 if (is_anonymous)
16593 {
16594 const char *previous_prefix = determine_prefix (die, cu);
16595
16596 std::vector<const char *> excludes;
16597 add_using_directive (using_directives (cu),
16598 previous_prefix, type->name (), NULL,
16599 NULL, excludes, 0, &objfile->objfile_obstack);
16600 }
16601 }
16602
16603 if (die->child != NULL)
16604 {
16605 struct die_info *child_die = die->child;
16606
16607 while (child_die && child_die->tag)
16608 {
16609 process_die (child_die, cu);
16610 child_die = child_die->sibling;
16611 }
16612 }
16613 }
16614
16615 /* Read a Fortran module as type. This DIE can be only a declaration used for
16616 imported module. Still we need that type as local Fortran "use ... only"
16617 declaration imports depend on the created type in determine_prefix. */
16618
16619 static struct type *
16620 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16621 {
16622 struct objfile *objfile = cu->per_objfile->objfile;
16623 const char *module_name;
16624 struct type *type;
16625
16626 module_name = dwarf2_name (die, cu);
16627 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16628
16629 return set_die_type (die, type, cu);
16630 }
16631
16632 /* Read a Fortran module. */
16633
16634 static void
16635 read_module (struct die_info *die, struct dwarf2_cu *cu)
16636 {
16637 struct die_info *child_die = die->child;
16638 struct type *type;
16639
16640 type = read_type_die (die, cu);
16641 new_symbol (die, type, cu);
16642
16643 while (child_die && child_die->tag)
16644 {
16645 process_die (child_die, cu);
16646 child_die = child_die->sibling;
16647 }
16648 }
16649
16650 /* Return the name of the namespace represented by DIE. Set
16651 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16652 namespace. */
16653
16654 static const char *
16655 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16656 {
16657 struct die_info *current_die;
16658 const char *name = NULL;
16659
16660 /* Loop through the extensions until we find a name. */
16661
16662 for (current_die = die;
16663 current_die != NULL;
16664 current_die = dwarf2_extension (die, &cu))
16665 {
16666 /* We don't use dwarf2_name here so that we can detect the absence
16667 of a name -> anonymous namespace. */
16668 name = dwarf2_string_attr (die, DW_AT_name, cu);
16669
16670 if (name != NULL)
16671 break;
16672 }
16673
16674 /* Is it an anonymous namespace? */
16675
16676 *is_anonymous = (name == NULL);
16677 if (*is_anonymous)
16678 name = CP_ANONYMOUS_NAMESPACE_STR;
16679
16680 return name;
16681 }
16682
16683 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16684 the user defined type vector. */
16685
16686 static struct type *
16687 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16688 {
16689 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16690 struct comp_unit_head *cu_header = &cu->header;
16691 struct type *type;
16692 struct attribute *attr_byte_size;
16693 struct attribute *attr_address_class;
16694 int byte_size, addr_class;
16695 struct type *target_type;
16696
16697 target_type = die_type (die, cu);
16698
16699 /* The die_type call above may have already set the type for this DIE. */
16700 type = get_die_type (die, cu);
16701 if (type)
16702 return type;
16703
16704 type = lookup_pointer_type (target_type);
16705
16706 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16707 if (attr_byte_size)
16708 byte_size = DW_UNSND (attr_byte_size);
16709 else
16710 byte_size = cu_header->addr_size;
16711
16712 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16713 if (attr_address_class)
16714 addr_class = DW_UNSND (attr_address_class);
16715 else
16716 addr_class = DW_ADDR_none;
16717
16718 ULONGEST alignment = get_alignment (cu, die);
16719
16720 /* If the pointer size, alignment, or address class is different
16721 than the default, create a type variant marked as such and set
16722 the length accordingly. */
16723 if (TYPE_LENGTH (type) != byte_size
16724 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16725 && alignment != TYPE_RAW_ALIGN (type))
16726 || addr_class != DW_ADDR_none)
16727 {
16728 if (gdbarch_address_class_type_flags_p (gdbarch))
16729 {
16730 int type_flags;
16731
16732 type_flags = gdbarch_address_class_type_flags
16733 (gdbarch, byte_size, addr_class);
16734 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16735 == 0);
16736 type = make_type_with_address_space (type, type_flags);
16737 }
16738 else if (TYPE_LENGTH (type) != byte_size)
16739 {
16740 complaint (_("invalid pointer size %d"), byte_size);
16741 }
16742 else if (TYPE_RAW_ALIGN (type) != alignment)
16743 {
16744 complaint (_("Invalid DW_AT_alignment"
16745 " - DIE at %s [in module %s]"),
16746 sect_offset_str (die->sect_off),
16747 objfile_name (cu->per_objfile->objfile));
16748 }
16749 else
16750 {
16751 /* Should we also complain about unhandled address classes? */
16752 }
16753 }
16754
16755 TYPE_LENGTH (type) = byte_size;
16756 set_type_align (type, alignment);
16757 return set_die_type (die, type, cu);
16758 }
16759
16760 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16761 the user defined type vector. */
16762
16763 static struct type *
16764 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16765 {
16766 struct type *type;
16767 struct type *to_type;
16768 struct type *domain;
16769
16770 to_type = die_type (die, cu);
16771 domain = die_containing_type (die, cu);
16772
16773 /* The calls above may have already set the type for this DIE. */
16774 type = get_die_type (die, cu);
16775 if (type)
16776 return type;
16777
16778 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16779 type = lookup_methodptr_type (to_type);
16780 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16781 {
16782 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16783
16784 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16785 to_type->fields (), to_type->num_fields (),
16786 TYPE_VARARGS (to_type));
16787 type = lookup_methodptr_type (new_type);
16788 }
16789 else
16790 type = lookup_memberptr_type (to_type, domain);
16791
16792 return set_die_type (die, type, cu);
16793 }
16794
16795 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16796 the user defined type vector. */
16797
16798 static struct type *
16799 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16800 enum type_code refcode)
16801 {
16802 struct comp_unit_head *cu_header = &cu->header;
16803 struct type *type, *target_type;
16804 struct attribute *attr;
16805
16806 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16807
16808 target_type = die_type (die, cu);
16809
16810 /* The die_type call above may have already set the type for this DIE. */
16811 type = get_die_type (die, cu);
16812 if (type)
16813 return type;
16814
16815 type = lookup_reference_type (target_type, refcode);
16816 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16817 if (attr != nullptr)
16818 {
16819 TYPE_LENGTH (type) = DW_UNSND (attr);
16820 }
16821 else
16822 {
16823 TYPE_LENGTH (type) = cu_header->addr_size;
16824 }
16825 maybe_set_alignment (cu, die, type);
16826 return set_die_type (die, type, cu);
16827 }
16828
16829 /* Add the given cv-qualifiers to the element type of the array. GCC
16830 outputs DWARF type qualifiers that apply to an array, not the
16831 element type. But GDB relies on the array element type to carry
16832 the cv-qualifiers. This mimics section 6.7.3 of the C99
16833 specification. */
16834
16835 static struct type *
16836 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16837 struct type *base_type, int cnst, int voltl)
16838 {
16839 struct type *el_type, *inner_array;
16840
16841 base_type = copy_type (base_type);
16842 inner_array = base_type;
16843
16844 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16845 {
16846 TYPE_TARGET_TYPE (inner_array) =
16847 copy_type (TYPE_TARGET_TYPE (inner_array));
16848 inner_array = TYPE_TARGET_TYPE (inner_array);
16849 }
16850
16851 el_type = TYPE_TARGET_TYPE (inner_array);
16852 cnst |= TYPE_CONST (el_type);
16853 voltl |= TYPE_VOLATILE (el_type);
16854 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16855
16856 return set_die_type (die, base_type, cu);
16857 }
16858
16859 static struct type *
16860 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16861 {
16862 struct type *base_type, *cv_type;
16863
16864 base_type = die_type (die, cu);
16865
16866 /* The die_type call above may have already set the type for this DIE. */
16867 cv_type = get_die_type (die, cu);
16868 if (cv_type)
16869 return cv_type;
16870
16871 /* In case the const qualifier is applied to an array type, the element type
16872 is so qualified, not the array type (section 6.7.3 of C99). */
16873 if (base_type->code () == TYPE_CODE_ARRAY)
16874 return add_array_cv_type (die, cu, base_type, 1, 0);
16875
16876 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16877 return set_die_type (die, cv_type, cu);
16878 }
16879
16880 static struct type *
16881 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16882 {
16883 struct type *base_type, *cv_type;
16884
16885 base_type = die_type (die, cu);
16886
16887 /* The die_type call above may have already set the type for this DIE. */
16888 cv_type = get_die_type (die, cu);
16889 if (cv_type)
16890 return cv_type;
16891
16892 /* In case the volatile qualifier is applied to an array type, the
16893 element type is so qualified, not the array type (section 6.7.3
16894 of C99). */
16895 if (base_type->code () == TYPE_CODE_ARRAY)
16896 return add_array_cv_type (die, cu, base_type, 0, 1);
16897
16898 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16899 return set_die_type (die, cv_type, cu);
16900 }
16901
16902 /* Handle DW_TAG_restrict_type. */
16903
16904 static struct type *
16905 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16906 {
16907 struct type *base_type, *cv_type;
16908
16909 base_type = die_type (die, cu);
16910
16911 /* The die_type call above may have already set the type for this DIE. */
16912 cv_type = get_die_type (die, cu);
16913 if (cv_type)
16914 return cv_type;
16915
16916 cv_type = make_restrict_type (base_type);
16917 return set_die_type (die, cv_type, cu);
16918 }
16919
16920 /* Handle DW_TAG_atomic_type. */
16921
16922 static struct type *
16923 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16924 {
16925 struct type *base_type, *cv_type;
16926
16927 base_type = die_type (die, cu);
16928
16929 /* The die_type call above may have already set the type for this DIE. */
16930 cv_type = get_die_type (die, cu);
16931 if (cv_type)
16932 return cv_type;
16933
16934 cv_type = make_atomic_type (base_type);
16935 return set_die_type (die, cv_type, cu);
16936 }
16937
16938 /* Extract all information from a DW_TAG_string_type DIE and add to
16939 the user defined type vector. It isn't really a user defined type,
16940 but it behaves like one, with other DIE's using an AT_user_def_type
16941 attribute to reference it. */
16942
16943 static struct type *
16944 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16945 {
16946 struct objfile *objfile = cu->per_objfile->objfile;
16947 struct gdbarch *gdbarch = objfile->arch ();
16948 struct type *type, *range_type, *index_type, *char_type;
16949 struct attribute *attr;
16950 struct dynamic_prop prop;
16951 bool length_is_constant = true;
16952 LONGEST length;
16953
16954 /* There are a couple of places where bit sizes might be made use of
16955 when parsing a DW_TAG_string_type, however, no producer that we know
16956 of make use of these. Handling bit sizes that are a multiple of the
16957 byte size is easy enough, but what about other bit sizes? Lets deal
16958 with that problem when we have to. Warn about these attributes being
16959 unsupported, then parse the type and ignore them like we always
16960 have. */
16961 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16962 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16963 {
16964 static bool warning_printed = false;
16965 if (!warning_printed)
16966 {
16967 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16968 "currently supported on DW_TAG_string_type."));
16969 warning_printed = true;
16970 }
16971 }
16972
16973 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16974 if (attr != nullptr && !attr->form_is_constant ())
16975 {
16976 /* The string length describes the location at which the length of
16977 the string can be found. The size of the length field can be
16978 specified with one of the attributes below. */
16979 struct type *prop_type;
16980 struct attribute *len
16981 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16982 if (len == nullptr)
16983 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16984 if (len != nullptr && len->form_is_constant ())
16985 {
16986 /* Pass 0 as the default as we know this attribute is constant
16987 and the default value will not be returned. */
16988 LONGEST sz = len->constant_value (0);
16989 prop_type = cu->per_cu->int_type (sz, true);
16990 }
16991 else
16992 {
16993 /* If the size is not specified then we assume it is the size of
16994 an address on this target. */
16995 prop_type = cu->per_cu->addr_sized_int_type (true);
16996 }
16997
16998 /* Convert the attribute into a dynamic property. */
16999 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17000 length = 1;
17001 else
17002 length_is_constant = false;
17003 }
17004 else if (attr != nullptr)
17005 {
17006 /* This DW_AT_string_length just contains the length with no
17007 indirection. There's no need to create a dynamic property in this
17008 case. Pass 0 for the default value as we know it will not be
17009 returned in this case. */
17010 length = attr->constant_value (0);
17011 }
17012 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17013 {
17014 /* We don't currently support non-constant byte sizes for strings. */
17015 length = attr->constant_value (1);
17016 }
17017 else
17018 {
17019 /* Use 1 as a fallback length if we have nothing else. */
17020 length = 1;
17021 }
17022
17023 index_type = objfile_type (objfile)->builtin_int;
17024 if (length_is_constant)
17025 range_type = create_static_range_type (NULL, index_type, 1, length);
17026 else
17027 {
17028 struct dynamic_prop low_bound;
17029
17030 low_bound.kind = PROP_CONST;
17031 low_bound.data.const_val = 1;
17032 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17033 }
17034 char_type = language_string_char_type (cu->language_defn, gdbarch);
17035 type = create_string_type (NULL, char_type, range_type);
17036
17037 return set_die_type (die, type, cu);
17038 }
17039
17040 /* Assuming that DIE corresponds to a function, returns nonzero
17041 if the function is prototyped. */
17042
17043 static int
17044 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17045 {
17046 struct attribute *attr;
17047
17048 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17049 if (attr && (DW_UNSND (attr) != 0))
17050 return 1;
17051
17052 /* The DWARF standard implies that the DW_AT_prototyped attribute
17053 is only meaningful for C, but the concept also extends to other
17054 languages that allow unprototyped functions (Eg: Objective C).
17055 For all other languages, assume that functions are always
17056 prototyped. */
17057 if (cu->language != language_c
17058 && cu->language != language_objc
17059 && cu->language != language_opencl)
17060 return 1;
17061
17062 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17063 prototyped and unprototyped functions; default to prototyped,
17064 since that is more common in modern code (and RealView warns
17065 about unprototyped functions). */
17066 if (producer_is_realview (cu->producer))
17067 return 1;
17068
17069 return 0;
17070 }
17071
17072 /* Handle DIES due to C code like:
17073
17074 struct foo
17075 {
17076 int (*funcp)(int a, long l);
17077 int b;
17078 };
17079
17080 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17081
17082 static struct type *
17083 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17084 {
17085 struct objfile *objfile = cu->per_objfile->objfile;
17086 struct type *type; /* Type that this function returns. */
17087 struct type *ftype; /* Function that returns above type. */
17088 struct attribute *attr;
17089
17090 type = die_type (die, cu);
17091
17092 /* The die_type call above may have already set the type for this DIE. */
17093 ftype = get_die_type (die, cu);
17094 if (ftype)
17095 return ftype;
17096
17097 ftype = lookup_function_type (type);
17098
17099 if (prototyped_function_p (die, cu))
17100 TYPE_PROTOTYPED (ftype) = 1;
17101
17102 /* Store the calling convention in the type if it's available in
17103 the subroutine die. Otherwise set the calling convention to
17104 the default value DW_CC_normal. */
17105 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17106 if (attr != nullptr
17107 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17108 TYPE_CALLING_CONVENTION (ftype)
17109 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17110 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17111 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17112 else
17113 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17114
17115 /* Record whether the function returns normally to its caller or not
17116 if the DWARF producer set that information. */
17117 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17118 if (attr && (DW_UNSND (attr) != 0))
17119 TYPE_NO_RETURN (ftype) = 1;
17120
17121 /* We need to add the subroutine type to the die immediately so
17122 we don't infinitely recurse when dealing with parameters
17123 declared as the same subroutine type. */
17124 set_die_type (die, ftype, cu);
17125
17126 if (die->child != NULL)
17127 {
17128 struct type *void_type = objfile_type (objfile)->builtin_void;
17129 struct die_info *child_die;
17130 int nparams, iparams;
17131
17132 /* Count the number of parameters.
17133 FIXME: GDB currently ignores vararg functions, but knows about
17134 vararg member functions. */
17135 nparams = 0;
17136 child_die = die->child;
17137 while (child_die && child_die->tag)
17138 {
17139 if (child_die->tag == DW_TAG_formal_parameter)
17140 nparams++;
17141 else if (child_die->tag == DW_TAG_unspecified_parameters)
17142 TYPE_VARARGS (ftype) = 1;
17143 child_die = child_die->sibling;
17144 }
17145
17146 /* Allocate storage for parameters and fill them in. */
17147 ftype->set_num_fields (nparams);
17148 ftype->set_fields
17149 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17150
17151 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17152 even if we error out during the parameters reading below. */
17153 for (iparams = 0; iparams < nparams; iparams++)
17154 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17155
17156 iparams = 0;
17157 child_die = die->child;
17158 while (child_die && child_die->tag)
17159 {
17160 if (child_die->tag == DW_TAG_formal_parameter)
17161 {
17162 struct type *arg_type;
17163
17164 /* DWARF version 2 has no clean way to discern C++
17165 static and non-static member functions. G++ helps
17166 GDB by marking the first parameter for non-static
17167 member functions (which is the this pointer) as
17168 artificial. We pass this information to
17169 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17170
17171 DWARF version 3 added DW_AT_object_pointer, which GCC
17172 4.5 does not yet generate. */
17173 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17174 if (attr != nullptr)
17175 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17176 else
17177 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17178 arg_type = die_type (child_die, cu);
17179
17180 /* RealView does not mark THIS as const, which the testsuite
17181 expects. GCC marks THIS as const in method definitions,
17182 but not in the class specifications (GCC PR 43053). */
17183 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17184 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17185 {
17186 int is_this = 0;
17187 struct dwarf2_cu *arg_cu = cu;
17188 const char *name = dwarf2_name (child_die, cu);
17189
17190 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17191 if (attr != nullptr)
17192 {
17193 /* If the compiler emits this, use it. */
17194 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17195 is_this = 1;
17196 }
17197 else if (name && strcmp (name, "this") == 0)
17198 /* Function definitions will have the argument names. */
17199 is_this = 1;
17200 else if (name == NULL && iparams == 0)
17201 /* Declarations may not have the names, so like
17202 elsewhere in GDB, assume an artificial first
17203 argument is "this". */
17204 is_this = 1;
17205
17206 if (is_this)
17207 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17208 arg_type, 0);
17209 }
17210
17211 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17212 iparams++;
17213 }
17214 child_die = child_die->sibling;
17215 }
17216 }
17217
17218 return ftype;
17219 }
17220
17221 static struct type *
17222 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17223 {
17224 struct objfile *objfile = cu->per_objfile->objfile;
17225 const char *name = NULL;
17226 struct type *this_type, *target_type;
17227
17228 name = dwarf2_full_name (NULL, die, cu);
17229 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17230 TYPE_TARGET_STUB (this_type) = 1;
17231 set_die_type (die, this_type, cu);
17232 target_type = die_type (die, cu);
17233 if (target_type != this_type)
17234 TYPE_TARGET_TYPE (this_type) = target_type;
17235 else
17236 {
17237 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17238 spec and cause infinite loops in GDB. */
17239 complaint (_("Self-referential DW_TAG_typedef "
17240 "- DIE at %s [in module %s]"),
17241 sect_offset_str (die->sect_off), objfile_name (objfile));
17242 TYPE_TARGET_TYPE (this_type) = NULL;
17243 }
17244 if (name == NULL)
17245 {
17246 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17247 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17248 Handle these by just returning the target type, rather than
17249 constructing an anonymous typedef type and trying to handle this
17250 elsewhere. */
17251 set_die_type (die, target_type, cu);
17252 return target_type;
17253 }
17254 return this_type;
17255 }
17256
17257 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17258 (which may be different from NAME) to the architecture back-end to allow
17259 it to guess the correct format if necessary. */
17260
17261 static struct type *
17262 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17263 const char *name_hint, enum bfd_endian byte_order)
17264 {
17265 struct gdbarch *gdbarch = objfile->arch ();
17266 const struct floatformat **format;
17267 struct type *type;
17268
17269 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17270 if (format)
17271 type = init_float_type (objfile, bits, name, format, byte_order);
17272 else
17273 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17274
17275 return type;
17276 }
17277
17278 /* Allocate an integer type of size BITS and name NAME. */
17279
17280 static struct type *
17281 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17282 int bits, int unsigned_p, const char *name)
17283 {
17284 struct type *type;
17285
17286 /* Versions of Intel's C Compiler generate an integer type called "void"
17287 instead of using DW_TAG_unspecified_type. This has been seen on
17288 at least versions 14, 17, and 18. */
17289 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17290 && strcmp (name, "void") == 0)
17291 type = objfile_type (objfile)->builtin_void;
17292 else
17293 type = init_integer_type (objfile, bits, unsigned_p, name);
17294
17295 return type;
17296 }
17297
17298 /* Initialise and return a floating point type of size BITS suitable for
17299 use as a component of a complex number. The NAME_HINT is passed through
17300 when initialising the floating point type and is the name of the complex
17301 type.
17302
17303 As DWARF doesn't currently provide an explicit name for the components
17304 of a complex number, but it can be helpful to have these components
17305 named, we try to select a suitable name based on the size of the
17306 component. */
17307 static struct type *
17308 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17309 struct objfile *objfile,
17310 int bits, const char *name_hint,
17311 enum bfd_endian byte_order)
17312 {
17313 gdbarch *gdbarch = objfile->arch ();
17314 struct type *tt = nullptr;
17315
17316 /* Try to find a suitable floating point builtin type of size BITS.
17317 We're going to use the name of this type as the name for the complex
17318 target type that we are about to create. */
17319 switch (cu->language)
17320 {
17321 case language_fortran:
17322 switch (bits)
17323 {
17324 case 32:
17325 tt = builtin_f_type (gdbarch)->builtin_real;
17326 break;
17327 case 64:
17328 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17329 break;
17330 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17331 case 128:
17332 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17333 break;
17334 }
17335 break;
17336 default:
17337 switch (bits)
17338 {
17339 case 32:
17340 tt = builtin_type (gdbarch)->builtin_float;
17341 break;
17342 case 64:
17343 tt = builtin_type (gdbarch)->builtin_double;
17344 break;
17345 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17346 case 128:
17347 tt = builtin_type (gdbarch)->builtin_long_double;
17348 break;
17349 }
17350 break;
17351 }
17352
17353 /* If the type we found doesn't match the size we were looking for, then
17354 pretend we didn't find a type at all, the complex target type we
17355 create will then be nameless. */
17356 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17357 tt = nullptr;
17358
17359 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17360 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17361 }
17362
17363 /* Find a representation of a given base type and install
17364 it in the TYPE field of the die. */
17365
17366 static struct type *
17367 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17368 {
17369 struct objfile *objfile = cu->per_objfile->objfile;
17370 struct type *type;
17371 struct attribute *attr;
17372 int encoding = 0, bits = 0;
17373 const char *name;
17374 gdbarch *arch;
17375
17376 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17377 if (attr != nullptr)
17378 encoding = DW_UNSND (attr);
17379 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17380 if (attr != nullptr)
17381 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17382 name = dwarf2_name (die, cu);
17383 if (!name)
17384 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17385
17386 arch = objfile->arch ();
17387 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17388
17389 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17390 if (attr)
17391 {
17392 int endianity = DW_UNSND (attr);
17393
17394 switch (endianity)
17395 {
17396 case DW_END_big:
17397 byte_order = BFD_ENDIAN_BIG;
17398 break;
17399 case DW_END_little:
17400 byte_order = BFD_ENDIAN_LITTLE;
17401 break;
17402 default:
17403 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17404 break;
17405 }
17406 }
17407
17408 switch (encoding)
17409 {
17410 case DW_ATE_address:
17411 /* Turn DW_ATE_address into a void * pointer. */
17412 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17413 type = init_pointer_type (objfile, bits, name, type);
17414 break;
17415 case DW_ATE_boolean:
17416 type = init_boolean_type (objfile, bits, 1, name);
17417 break;
17418 case DW_ATE_complex_float:
17419 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17420 byte_order);
17421 if (type->code () == TYPE_CODE_ERROR)
17422 {
17423 if (name == nullptr)
17424 {
17425 struct obstack *obstack
17426 = &cu->per_objfile->objfile->objfile_obstack;
17427 name = obconcat (obstack, "_Complex ", type->name (),
17428 nullptr);
17429 }
17430 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17431 }
17432 else
17433 type = init_complex_type (name, type);
17434 break;
17435 case DW_ATE_decimal_float:
17436 type = init_decfloat_type (objfile, bits, name);
17437 break;
17438 case DW_ATE_float:
17439 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17440 break;
17441 case DW_ATE_signed:
17442 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17443 break;
17444 case DW_ATE_unsigned:
17445 if (cu->language == language_fortran
17446 && name
17447 && startswith (name, "character("))
17448 type = init_character_type (objfile, bits, 1, name);
17449 else
17450 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17451 break;
17452 case DW_ATE_signed_char:
17453 if (cu->language == language_ada || cu->language == language_m2
17454 || cu->language == language_pascal
17455 || cu->language == language_fortran)
17456 type = init_character_type (objfile, bits, 0, name);
17457 else
17458 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17459 break;
17460 case DW_ATE_unsigned_char:
17461 if (cu->language == language_ada || cu->language == language_m2
17462 || cu->language == language_pascal
17463 || cu->language == language_fortran
17464 || cu->language == language_rust)
17465 type = init_character_type (objfile, bits, 1, name);
17466 else
17467 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17468 break;
17469 case DW_ATE_UTF:
17470 {
17471 if (bits == 16)
17472 type = builtin_type (arch)->builtin_char16;
17473 else if (bits == 32)
17474 type = builtin_type (arch)->builtin_char32;
17475 else
17476 {
17477 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17478 bits);
17479 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17480 }
17481 return set_die_type (die, type, cu);
17482 }
17483 break;
17484
17485 default:
17486 complaint (_("unsupported DW_AT_encoding: '%s'"),
17487 dwarf_type_encoding_name (encoding));
17488 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17489 break;
17490 }
17491
17492 if (name && strcmp (name, "char") == 0)
17493 TYPE_NOSIGN (type) = 1;
17494
17495 maybe_set_alignment (cu, die, type);
17496
17497 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17498
17499 return set_die_type (die, type, cu);
17500 }
17501
17502 /* Parse dwarf attribute if it's a block, reference or constant and put the
17503 resulting value of the attribute into struct bound_prop.
17504 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17505
17506 static int
17507 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17508 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17509 struct type *default_type)
17510 {
17511 struct dwarf2_property_baton *baton;
17512 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17513 struct objfile *objfile = per_objfile->objfile;
17514 struct obstack *obstack = &objfile->objfile_obstack;
17515
17516 gdb_assert (default_type != NULL);
17517
17518 if (attr == NULL || prop == NULL)
17519 return 0;
17520
17521 if (attr->form_is_block ())
17522 {
17523 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17524 baton->property_type = default_type;
17525 baton->locexpr.per_cu = cu->per_cu;
17526 baton->locexpr.per_objfile = per_objfile;
17527 baton->locexpr.size = DW_BLOCK (attr)->size;
17528 baton->locexpr.data = DW_BLOCK (attr)->data;
17529 switch (attr->name)
17530 {
17531 case DW_AT_string_length:
17532 baton->locexpr.is_reference = true;
17533 break;
17534 default:
17535 baton->locexpr.is_reference = false;
17536 break;
17537 }
17538 prop->data.baton = baton;
17539 prop->kind = PROP_LOCEXPR;
17540 gdb_assert (prop->data.baton != NULL);
17541 }
17542 else if (attr->form_is_ref ())
17543 {
17544 struct dwarf2_cu *target_cu = cu;
17545 struct die_info *target_die;
17546 struct attribute *target_attr;
17547
17548 target_die = follow_die_ref (die, attr, &target_cu);
17549 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17550 if (target_attr == NULL)
17551 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17552 target_cu);
17553 if (target_attr == NULL)
17554 return 0;
17555
17556 switch (target_attr->name)
17557 {
17558 case DW_AT_location:
17559 if (target_attr->form_is_section_offset ())
17560 {
17561 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17562 baton->property_type = die_type (target_die, target_cu);
17563 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17564 prop->data.baton = baton;
17565 prop->kind = PROP_LOCLIST;
17566 gdb_assert (prop->data.baton != NULL);
17567 }
17568 else if (target_attr->form_is_block ())
17569 {
17570 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17571 baton->property_type = die_type (target_die, target_cu);
17572 baton->locexpr.per_cu = cu->per_cu;
17573 baton->locexpr.per_objfile = per_objfile;
17574 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17575 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17576 baton->locexpr.is_reference = true;
17577 prop->data.baton = baton;
17578 prop->kind = PROP_LOCEXPR;
17579 gdb_assert (prop->data.baton != NULL);
17580 }
17581 else
17582 {
17583 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17584 "dynamic property");
17585 return 0;
17586 }
17587 break;
17588 case DW_AT_data_member_location:
17589 {
17590 LONGEST offset;
17591
17592 if (!handle_data_member_location (target_die, target_cu,
17593 &offset))
17594 return 0;
17595
17596 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17597 baton->property_type = read_type_die (target_die->parent,
17598 target_cu);
17599 baton->offset_info.offset = offset;
17600 baton->offset_info.type = die_type (target_die, target_cu);
17601 prop->data.baton = baton;
17602 prop->kind = PROP_ADDR_OFFSET;
17603 break;
17604 }
17605 }
17606 }
17607 else if (attr->form_is_constant ())
17608 {
17609 prop->data.const_val = attr->constant_value (0);
17610 prop->kind = PROP_CONST;
17611 }
17612 else
17613 {
17614 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17615 dwarf2_name (die, cu));
17616 return 0;
17617 }
17618
17619 return 1;
17620 }
17621
17622 /* See read.h. */
17623
17624 struct type *
17625 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17626 {
17627 struct objfile *objfile = dwarf2_per_objfile->objfile;
17628 struct type *int_type;
17629
17630 /* Helper macro to examine the various builtin types. */
17631 #define TRY_TYPE(F) \
17632 int_type = (unsigned_p \
17633 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17634 : objfile_type (objfile)->builtin_ ## F); \
17635 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17636 return int_type
17637
17638 TRY_TYPE (char);
17639 TRY_TYPE (short);
17640 TRY_TYPE (int);
17641 TRY_TYPE (long);
17642 TRY_TYPE (long_long);
17643
17644 #undef TRY_TYPE
17645
17646 gdb_assert_not_reached ("unable to find suitable integer type");
17647 }
17648
17649 /* See read.h. */
17650
17651 struct type *
17652 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17653 {
17654 int addr_size = this->addr_size ();
17655 return int_type (addr_size, unsigned_p);
17656 }
17657
17658 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17659 present (which is valid) then compute the default type based on the
17660 compilation units address size. */
17661
17662 static struct type *
17663 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17664 {
17665 struct type *index_type = die_type (die, cu);
17666
17667 /* Dwarf-2 specifications explicitly allows to create subrange types
17668 without specifying a base type.
17669 In that case, the base type must be set to the type of
17670 the lower bound, upper bound or count, in that order, if any of these
17671 three attributes references an object that has a type.
17672 If no base type is found, the Dwarf-2 specifications say that
17673 a signed integer type of size equal to the size of an address should
17674 be used.
17675 For the following C code: `extern char gdb_int [];'
17676 GCC produces an empty range DIE.
17677 FIXME: muller/2010-05-28: Possible references to object for low bound,
17678 high bound or count are not yet handled by this code. */
17679 if (index_type->code () == TYPE_CODE_VOID)
17680 index_type = cu->per_cu->addr_sized_int_type (false);
17681
17682 return index_type;
17683 }
17684
17685 /* Read the given DW_AT_subrange DIE. */
17686
17687 static struct type *
17688 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17689 {
17690 struct type *base_type, *orig_base_type;
17691 struct type *range_type;
17692 struct attribute *attr;
17693 struct dynamic_prop low, high;
17694 int low_default_is_valid;
17695 int high_bound_is_count = 0;
17696 const char *name;
17697 ULONGEST negative_mask;
17698
17699 orig_base_type = read_subrange_index_type (die, cu);
17700
17701 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17702 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17703 creating the range type, but we use the result of check_typedef
17704 when examining properties of the type. */
17705 base_type = check_typedef (orig_base_type);
17706
17707 /* The die_type call above may have already set the type for this DIE. */
17708 range_type = get_die_type (die, cu);
17709 if (range_type)
17710 return range_type;
17711
17712 low.kind = PROP_CONST;
17713 high.kind = PROP_CONST;
17714 high.data.const_val = 0;
17715
17716 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17717 omitting DW_AT_lower_bound. */
17718 switch (cu->language)
17719 {
17720 case language_c:
17721 case language_cplus:
17722 low.data.const_val = 0;
17723 low_default_is_valid = 1;
17724 break;
17725 case language_fortran:
17726 low.data.const_val = 1;
17727 low_default_is_valid = 1;
17728 break;
17729 case language_d:
17730 case language_objc:
17731 case language_rust:
17732 low.data.const_val = 0;
17733 low_default_is_valid = (cu->header.version >= 4);
17734 break;
17735 case language_ada:
17736 case language_m2:
17737 case language_pascal:
17738 low.data.const_val = 1;
17739 low_default_is_valid = (cu->header.version >= 4);
17740 break;
17741 default:
17742 low.data.const_val = 0;
17743 low_default_is_valid = 0;
17744 break;
17745 }
17746
17747 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17748 if (attr != nullptr)
17749 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17750 else if (!low_default_is_valid)
17751 complaint (_("Missing DW_AT_lower_bound "
17752 "- DIE at %s [in module %s]"),
17753 sect_offset_str (die->sect_off),
17754 objfile_name (cu->per_objfile->objfile));
17755
17756 struct attribute *attr_ub, *attr_count;
17757 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17758 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17759 {
17760 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17761 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17762 {
17763 /* If bounds are constant do the final calculation here. */
17764 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17765 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17766 else
17767 high_bound_is_count = 1;
17768 }
17769 else
17770 {
17771 if (attr_ub != NULL)
17772 complaint (_("Unresolved DW_AT_upper_bound "
17773 "- DIE at %s [in module %s]"),
17774 sect_offset_str (die->sect_off),
17775 objfile_name (cu->per_objfile->objfile));
17776 if (attr_count != NULL)
17777 complaint (_("Unresolved DW_AT_count "
17778 "- DIE at %s [in module %s]"),
17779 sect_offset_str (die->sect_off),
17780 objfile_name (cu->per_objfile->objfile));
17781 }
17782 }
17783
17784 LONGEST bias = 0;
17785 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17786 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17787 bias = bias_attr->constant_value (0);
17788
17789 /* Normally, the DWARF producers are expected to use a signed
17790 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17791 But this is unfortunately not always the case, as witnessed
17792 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17793 is used instead. To work around that ambiguity, we treat
17794 the bounds as signed, and thus sign-extend their values, when
17795 the base type is signed. */
17796 negative_mask =
17797 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17798 if (low.kind == PROP_CONST
17799 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17800 low.data.const_val |= negative_mask;
17801 if (high.kind == PROP_CONST
17802 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17803 high.data.const_val |= negative_mask;
17804
17805 /* Check for bit and byte strides. */
17806 struct dynamic_prop byte_stride_prop;
17807 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17808 if (attr_byte_stride != nullptr)
17809 {
17810 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17811 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17812 prop_type);
17813 }
17814
17815 struct dynamic_prop bit_stride_prop;
17816 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17817 if (attr_bit_stride != nullptr)
17818 {
17819 /* It only makes sense to have either a bit or byte stride. */
17820 if (attr_byte_stride != nullptr)
17821 {
17822 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17823 "- DIE at %s [in module %s]"),
17824 sect_offset_str (die->sect_off),
17825 objfile_name (cu->per_objfile->objfile));
17826 attr_bit_stride = nullptr;
17827 }
17828 else
17829 {
17830 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17831 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17832 prop_type);
17833 }
17834 }
17835
17836 if (attr_byte_stride != nullptr
17837 || attr_bit_stride != nullptr)
17838 {
17839 bool byte_stride_p = (attr_byte_stride != nullptr);
17840 struct dynamic_prop *stride
17841 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17842
17843 range_type
17844 = create_range_type_with_stride (NULL, orig_base_type, &low,
17845 &high, bias, stride, byte_stride_p);
17846 }
17847 else
17848 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17849
17850 if (high_bound_is_count)
17851 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17852
17853 /* Ada expects an empty array on no boundary attributes. */
17854 if (attr == NULL && cu->language != language_ada)
17855 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17856
17857 name = dwarf2_name (die, cu);
17858 if (name)
17859 range_type->set_name (name);
17860
17861 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17862 if (attr != nullptr)
17863 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17864
17865 maybe_set_alignment (cu, die, range_type);
17866
17867 set_die_type (die, range_type, cu);
17868
17869 /* set_die_type should be already done. */
17870 set_descriptive_type (range_type, die, cu);
17871
17872 return range_type;
17873 }
17874
17875 static struct type *
17876 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17877 {
17878 struct type *type;
17879
17880 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17881 type->set_name (dwarf2_name (die, cu));
17882
17883 /* In Ada, an unspecified type is typically used when the description
17884 of the type is deferred to a different unit. When encountering
17885 such a type, we treat it as a stub, and try to resolve it later on,
17886 when needed. */
17887 if (cu->language == language_ada)
17888 TYPE_STUB (type) = 1;
17889
17890 return set_die_type (die, type, cu);
17891 }
17892
17893 /* Read a single die and all its descendents. Set the die's sibling
17894 field to NULL; set other fields in the die correctly, and set all
17895 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17896 location of the info_ptr after reading all of those dies. PARENT
17897 is the parent of the die in question. */
17898
17899 static struct die_info *
17900 read_die_and_children (const struct die_reader_specs *reader,
17901 const gdb_byte *info_ptr,
17902 const gdb_byte **new_info_ptr,
17903 struct die_info *parent)
17904 {
17905 struct die_info *die;
17906 const gdb_byte *cur_ptr;
17907
17908 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17909 if (die == NULL)
17910 {
17911 *new_info_ptr = cur_ptr;
17912 return NULL;
17913 }
17914 store_in_ref_table (die, reader->cu);
17915
17916 if (die->has_children)
17917 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17918 else
17919 {
17920 die->child = NULL;
17921 *new_info_ptr = cur_ptr;
17922 }
17923
17924 die->sibling = NULL;
17925 die->parent = parent;
17926 return die;
17927 }
17928
17929 /* Read a die, all of its descendents, and all of its siblings; set
17930 all of the fields of all of the dies correctly. Arguments are as
17931 in read_die_and_children. */
17932
17933 static struct die_info *
17934 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17935 const gdb_byte *info_ptr,
17936 const gdb_byte **new_info_ptr,
17937 struct die_info *parent)
17938 {
17939 struct die_info *first_die, *last_sibling;
17940 const gdb_byte *cur_ptr;
17941
17942 cur_ptr = info_ptr;
17943 first_die = last_sibling = NULL;
17944
17945 while (1)
17946 {
17947 struct die_info *die
17948 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17949
17950 if (die == NULL)
17951 {
17952 *new_info_ptr = cur_ptr;
17953 return first_die;
17954 }
17955
17956 if (!first_die)
17957 first_die = die;
17958 else
17959 last_sibling->sibling = die;
17960
17961 last_sibling = die;
17962 }
17963 }
17964
17965 /* Read a die, all of its descendents, and all of its siblings; set
17966 all of the fields of all of the dies correctly. Arguments are as
17967 in read_die_and_children.
17968 This the main entry point for reading a DIE and all its children. */
17969
17970 static struct die_info *
17971 read_die_and_siblings (const struct die_reader_specs *reader,
17972 const gdb_byte *info_ptr,
17973 const gdb_byte **new_info_ptr,
17974 struct die_info *parent)
17975 {
17976 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17977 new_info_ptr, parent);
17978
17979 if (dwarf_die_debug)
17980 {
17981 fprintf_unfiltered (gdb_stdlog,
17982 "Read die from %s@0x%x of %s:\n",
17983 reader->die_section->get_name (),
17984 (unsigned) (info_ptr - reader->die_section->buffer),
17985 bfd_get_filename (reader->abfd));
17986 dump_die (die, dwarf_die_debug);
17987 }
17988
17989 return die;
17990 }
17991
17992 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17993 attributes.
17994 The caller is responsible for filling in the extra attributes
17995 and updating (*DIEP)->num_attrs.
17996 Set DIEP to point to a newly allocated die with its information,
17997 except for its child, sibling, and parent fields. */
17998
17999 static const gdb_byte *
18000 read_full_die_1 (const struct die_reader_specs *reader,
18001 struct die_info **diep, const gdb_byte *info_ptr,
18002 int num_extra_attrs)
18003 {
18004 unsigned int abbrev_number, bytes_read, i;
18005 struct abbrev_info *abbrev;
18006 struct die_info *die;
18007 struct dwarf2_cu *cu = reader->cu;
18008 bfd *abfd = reader->abfd;
18009
18010 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18011 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18012 info_ptr += bytes_read;
18013 if (!abbrev_number)
18014 {
18015 *diep = NULL;
18016 return info_ptr;
18017 }
18018
18019 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18020 if (!abbrev)
18021 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18022 abbrev_number,
18023 bfd_get_filename (abfd));
18024
18025 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18026 die->sect_off = sect_off;
18027 die->tag = abbrev->tag;
18028 die->abbrev = abbrev_number;
18029 die->has_children = abbrev->has_children;
18030
18031 /* Make the result usable.
18032 The caller needs to update num_attrs after adding the extra
18033 attributes. */
18034 die->num_attrs = abbrev->num_attrs;
18035
18036 std::vector<int> indexes_that_need_reprocess;
18037 for (i = 0; i < abbrev->num_attrs; ++i)
18038 {
18039 bool need_reprocess;
18040 info_ptr =
18041 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18042 info_ptr, &need_reprocess);
18043 if (need_reprocess)
18044 indexes_that_need_reprocess.push_back (i);
18045 }
18046
18047 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18048 if (attr != nullptr)
18049 cu->str_offsets_base = DW_UNSND (attr);
18050
18051 attr = die->attr (DW_AT_loclists_base);
18052 if (attr != nullptr)
18053 cu->loclist_base = DW_UNSND (attr);
18054
18055 auto maybe_addr_base = die->addr_base ();
18056 if (maybe_addr_base.has_value ())
18057 cu->addr_base = *maybe_addr_base;
18058 for (int index : indexes_that_need_reprocess)
18059 read_attribute_reprocess (reader, &die->attrs[index]);
18060 *diep = die;
18061 return info_ptr;
18062 }
18063
18064 /* Read a die and all its attributes.
18065 Set DIEP to point to a newly allocated die with its information,
18066 except for its child, sibling, and parent fields. */
18067
18068 static const gdb_byte *
18069 read_full_die (const struct die_reader_specs *reader,
18070 struct die_info **diep, const gdb_byte *info_ptr)
18071 {
18072 const gdb_byte *result;
18073
18074 result = read_full_die_1 (reader, diep, info_ptr, 0);
18075
18076 if (dwarf_die_debug)
18077 {
18078 fprintf_unfiltered (gdb_stdlog,
18079 "Read die from %s@0x%x of %s:\n",
18080 reader->die_section->get_name (),
18081 (unsigned) (info_ptr - reader->die_section->buffer),
18082 bfd_get_filename (reader->abfd));
18083 dump_die (*diep, dwarf_die_debug);
18084 }
18085
18086 return result;
18087 }
18088 \f
18089
18090 /* Returns nonzero if TAG represents a type that we might generate a partial
18091 symbol for. */
18092
18093 static int
18094 is_type_tag_for_partial (int tag)
18095 {
18096 switch (tag)
18097 {
18098 #if 0
18099 /* Some types that would be reasonable to generate partial symbols for,
18100 that we don't at present. */
18101 case DW_TAG_array_type:
18102 case DW_TAG_file_type:
18103 case DW_TAG_ptr_to_member_type:
18104 case DW_TAG_set_type:
18105 case DW_TAG_string_type:
18106 case DW_TAG_subroutine_type:
18107 #endif
18108 case DW_TAG_base_type:
18109 case DW_TAG_class_type:
18110 case DW_TAG_interface_type:
18111 case DW_TAG_enumeration_type:
18112 case DW_TAG_structure_type:
18113 case DW_TAG_subrange_type:
18114 case DW_TAG_typedef:
18115 case DW_TAG_union_type:
18116 return 1;
18117 default:
18118 return 0;
18119 }
18120 }
18121
18122 /* Load all DIEs that are interesting for partial symbols into memory. */
18123
18124 static struct partial_die_info *
18125 load_partial_dies (const struct die_reader_specs *reader,
18126 const gdb_byte *info_ptr, int building_psymtab)
18127 {
18128 struct dwarf2_cu *cu = reader->cu;
18129 struct objfile *objfile = cu->per_objfile->objfile;
18130 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18131 unsigned int bytes_read;
18132 unsigned int load_all = 0;
18133 int nesting_level = 1;
18134
18135 parent_die = NULL;
18136 last_die = NULL;
18137
18138 gdb_assert (cu->per_cu != NULL);
18139 if (cu->per_cu->load_all_dies)
18140 load_all = 1;
18141
18142 cu->partial_dies
18143 = htab_create_alloc_ex (cu->header.length / 12,
18144 partial_die_hash,
18145 partial_die_eq,
18146 NULL,
18147 &cu->comp_unit_obstack,
18148 hashtab_obstack_allocate,
18149 dummy_obstack_deallocate);
18150
18151 while (1)
18152 {
18153 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18154
18155 /* A NULL abbrev means the end of a series of children. */
18156 if (abbrev == NULL)
18157 {
18158 if (--nesting_level == 0)
18159 return first_die;
18160
18161 info_ptr += bytes_read;
18162 last_die = parent_die;
18163 parent_die = parent_die->die_parent;
18164 continue;
18165 }
18166
18167 /* Check for template arguments. We never save these; if
18168 they're seen, we just mark the parent, and go on our way. */
18169 if (parent_die != NULL
18170 && cu->language == language_cplus
18171 && (abbrev->tag == DW_TAG_template_type_param
18172 || abbrev->tag == DW_TAG_template_value_param))
18173 {
18174 parent_die->has_template_arguments = 1;
18175
18176 if (!load_all)
18177 {
18178 /* We don't need a partial DIE for the template argument. */
18179 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18180 continue;
18181 }
18182 }
18183
18184 /* We only recurse into c++ subprograms looking for template arguments.
18185 Skip their other children. */
18186 if (!load_all
18187 && cu->language == language_cplus
18188 && parent_die != NULL
18189 && parent_die->tag == DW_TAG_subprogram)
18190 {
18191 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18192 continue;
18193 }
18194
18195 /* Check whether this DIE is interesting enough to save. Normally
18196 we would not be interested in members here, but there may be
18197 later variables referencing them via DW_AT_specification (for
18198 static members). */
18199 if (!load_all
18200 && !is_type_tag_for_partial (abbrev->tag)
18201 && abbrev->tag != DW_TAG_constant
18202 && abbrev->tag != DW_TAG_enumerator
18203 && abbrev->tag != DW_TAG_subprogram
18204 && abbrev->tag != DW_TAG_inlined_subroutine
18205 && abbrev->tag != DW_TAG_lexical_block
18206 && abbrev->tag != DW_TAG_variable
18207 && abbrev->tag != DW_TAG_namespace
18208 && abbrev->tag != DW_TAG_module
18209 && abbrev->tag != DW_TAG_member
18210 && abbrev->tag != DW_TAG_imported_unit
18211 && abbrev->tag != DW_TAG_imported_declaration)
18212 {
18213 /* Otherwise we skip to the next sibling, if any. */
18214 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18215 continue;
18216 }
18217
18218 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18219 abbrev);
18220
18221 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18222
18223 /* This two-pass algorithm for processing partial symbols has a
18224 high cost in cache pressure. Thus, handle some simple cases
18225 here which cover the majority of C partial symbols. DIEs
18226 which neither have specification tags in them, nor could have
18227 specification tags elsewhere pointing at them, can simply be
18228 processed and discarded.
18229
18230 This segment is also optional; scan_partial_symbols and
18231 add_partial_symbol will handle these DIEs if we chain
18232 them in normally. When compilers which do not emit large
18233 quantities of duplicate debug information are more common,
18234 this code can probably be removed. */
18235
18236 /* Any complete simple types at the top level (pretty much all
18237 of them, for a language without namespaces), can be processed
18238 directly. */
18239 if (parent_die == NULL
18240 && pdi.has_specification == 0
18241 && pdi.is_declaration == 0
18242 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18243 || pdi.tag == DW_TAG_base_type
18244 || pdi.tag == DW_TAG_subrange_type))
18245 {
18246 if (building_psymtab && pdi.name != NULL)
18247 add_psymbol_to_list (pdi.name, false,
18248 VAR_DOMAIN, LOC_TYPEDEF, -1,
18249 psymbol_placement::STATIC,
18250 0, cu->language, objfile);
18251 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18252 continue;
18253 }
18254
18255 /* The exception for DW_TAG_typedef with has_children above is
18256 a workaround of GCC PR debug/47510. In the case of this complaint
18257 type_name_or_error will error on such types later.
18258
18259 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18260 it could not find the child DIEs referenced later, this is checked
18261 above. In correct DWARF DW_TAG_typedef should have no children. */
18262
18263 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18264 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18265 "- DIE at %s [in module %s]"),
18266 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18267
18268 /* If we're at the second level, and we're an enumerator, and
18269 our parent has no specification (meaning possibly lives in a
18270 namespace elsewhere), then we can add the partial symbol now
18271 instead of queueing it. */
18272 if (pdi.tag == DW_TAG_enumerator
18273 && parent_die != NULL
18274 && parent_die->die_parent == NULL
18275 && parent_die->tag == DW_TAG_enumeration_type
18276 && parent_die->has_specification == 0)
18277 {
18278 if (pdi.name == NULL)
18279 complaint (_("malformed enumerator DIE ignored"));
18280 else if (building_psymtab)
18281 add_psymbol_to_list (pdi.name, false,
18282 VAR_DOMAIN, LOC_CONST, -1,
18283 cu->language == language_cplus
18284 ? psymbol_placement::GLOBAL
18285 : psymbol_placement::STATIC,
18286 0, cu->language, objfile);
18287
18288 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18289 continue;
18290 }
18291
18292 struct partial_die_info *part_die
18293 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18294
18295 /* We'll save this DIE so link it in. */
18296 part_die->die_parent = parent_die;
18297 part_die->die_sibling = NULL;
18298 part_die->die_child = NULL;
18299
18300 if (last_die && last_die == parent_die)
18301 last_die->die_child = part_die;
18302 else if (last_die)
18303 last_die->die_sibling = part_die;
18304
18305 last_die = part_die;
18306
18307 if (first_die == NULL)
18308 first_die = part_die;
18309
18310 /* Maybe add the DIE to the hash table. Not all DIEs that we
18311 find interesting need to be in the hash table, because we
18312 also have the parent/sibling/child chains; only those that we
18313 might refer to by offset later during partial symbol reading.
18314
18315 For now this means things that might have be the target of a
18316 DW_AT_specification, DW_AT_abstract_origin, or
18317 DW_AT_extension. DW_AT_extension will refer only to
18318 namespaces; DW_AT_abstract_origin refers to functions (and
18319 many things under the function DIE, but we do not recurse
18320 into function DIEs during partial symbol reading) and
18321 possibly variables as well; DW_AT_specification refers to
18322 declarations. Declarations ought to have the DW_AT_declaration
18323 flag. It happens that GCC forgets to put it in sometimes, but
18324 only for functions, not for types.
18325
18326 Adding more things than necessary to the hash table is harmless
18327 except for the performance cost. Adding too few will result in
18328 wasted time in find_partial_die, when we reread the compilation
18329 unit with load_all_dies set. */
18330
18331 if (load_all
18332 || abbrev->tag == DW_TAG_constant
18333 || abbrev->tag == DW_TAG_subprogram
18334 || abbrev->tag == DW_TAG_variable
18335 || abbrev->tag == DW_TAG_namespace
18336 || part_die->is_declaration)
18337 {
18338 void **slot;
18339
18340 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18341 to_underlying (part_die->sect_off),
18342 INSERT);
18343 *slot = part_die;
18344 }
18345
18346 /* For some DIEs we want to follow their children (if any). For C
18347 we have no reason to follow the children of structures; for other
18348 languages we have to, so that we can get at method physnames
18349 to infer fully qualified class names, for DW_AT_specification,
18350 and for C++ template arguments. For C++, we also look one level
18351 inside functions to find template arguments (if the name of the
18352 function does not already contain the template arguments).
18353
18354 For Ada and Fortran, we need to scan the children of subprograms
18355 and lexical blocks as well because these languages allow the
18356 definition of nested entities that could be interesting for the
18357 debugger, such as nested subprograms for instance. */
18358 if (last_die->has_children
18359 && (load_all
18360 || last_die->tag == DW_TAG_namespace
18361 || last_die->tag == DW_TAG_module
18362 || last_die->tag == DW_TAG_enumeration_type
18363 || (cu->language == language_cplus
18364 && last_die->tag == DW_TAG_subprogram
18365 && (last_die->name == NULL
18366 || strchr (last_die->name, '<') == NULL))
18367 || (cu->language != language_c
18368 && (last_die->tag == DW_TAG_class_type
18369 || last_die->tag == DW_TAG_interface_type
18370 || last_die->tag == DW_TAG_structure_type
18371 || last_die->tag == DW_TAG_union_type))
18372 || ((cu->language == language_ada
18373 || cu->language == language_fortran)
18374 && (last_die->tag == DW_TAG_subprogram
18375 || last_die->tag == DW_TAG_lexical_block))))
18376 {
18377 nesting_level++;
18378 parent_die = last_die;
18379 continue;
18380 }
18381
18382 /* Otherwise we skip to the next sibling, if any. */
18383 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18384
18385 /* Back to the top, do it again. */
18386 }
18387 }
18388
18389 partial_die_info::partial_die_info (sect_offset sect_off_,
18390 struct abbrev_info *abbrev)
18391 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18392 {
18393 }
18394
18395 /* Read a minimal amount of information into the minimal die structure.
18396 INFO_PTR should point just after the initial uleb128 of a DIE. */
18397
18398 const gdb_byte *
18399 partial_die_info::read (const struct die_reader_specs *reader,
18400 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18401 {
18402 struct dwarf2_cu *cu = reader->cu;
18403 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18404 unsigned int i;
18405 int has_low_pc_attr = 0;
18406 int has_high_pc_attr = 0;
18407 int high_pc_relative = 0;
18408
18409 for (i = 0; i < abbrev.num_attrs; ++i)
18410 {
18411 attribute attr;
18412 bool need_reprocess;
18413 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18414 info_ptr, &need_reprocess);
18415 /* String and address offsets that need to do the reprocessing have
18416 already been read at this point, so there is no need to wait until
18417 the loop terminates to do the reprocessing. */
18418 if (need_reprocess)
18419 read_attribute_reprocess (reader, &attr);
18420 /* Store the data if it is of an attribute we want to keep in a
18421 partial symbol table. */
18422 switch (attr.name)
18423 {
18424 case DW_AT_name:
18425 switch (tag)
18426 {
18427 case DW_TAG_compile_unit:
18428 case DW_TAG_partial_unit:
18429 case DW_TAG_type_unit:
18430 /* Compilation units have a DW_AT_name that is a filename, not
18431 a source language identifier. */
18432 case DW_TAG_enumeration_type:
18433 case DW_TAG_enumerator:
18434 /* These tags always have simple identifiers already; no need
18435 to canonicalize them. */
18436 name = DW_STRING (&attr);
18437 break;
18438 default:
18439 {
18440 struct objfile *objfile = dwarf2_per_objfile->objfile;
18441
18442 name
18443 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18444 }
18445 break;
18446 }
18447 break;
18448 case DW_AT_linkage_name:
18449 case DW_AT_MIPS_linkage_name:
18450 /* Note that both forms of linkage name might appear. We
18451 assume they will be the same, and we only store the last
18452 one we see. */
18453 linkage_name = attr.value_as_string ();
18454 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18455 See https://github.com/rust-lang/rust/issues/32925. */
18456 if (cu->language == language_rust && linkage_name != NULL
18457 && strchr (linkage_name, '{') != NULL)
18458 linkage_name = NULL;
18459 break;
18460 case DW_AT_low_pc:
18461 has_low_pc_attr = 1;
18462 lowpc = attr.value_as_address ();
18463 break;
18464 case DW_AT_high_pc:
18465 has_high_pc_attr = 1;
18466 highpc = attr.value_as_address ();
18467 if (cu->header.version >= 4 && attr.form_is_constant ())
18468 high_pc_relative = 1;
18469 break;
18470 case DW_AT_location:
18471 /* Support the .debug_loc offsets. */
18472 if (attr.form_is_block ())
18473 {
18474 d.locdesc = DW_BLOCK (&attr);
18475 }
18476 else if (attr.form_is_section_offset ())
18477 {
18478 dwarf2_complex_location_expr_complaint ();
18479 }
18480 else
18481 {
18482 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18483 "partial symbol information");
18484 }
18485 break;
18486 case DW_AT_external:
18487 is_external = DW_UNSND (&attr);
18488 break;
18489 case DW_AT_declaration:
18490 is_declaration = DW_UNSND (&attr);
18491 break;
18492 case DW_AT_type:
18493 has_type = 1;
18494 break;
18495 case DW_AT_abstract_origin:
18496 case DW_AT_specification:
18497 case DW_AT_extension:
18498 has_specification = 1;
18499 spec_offset = attr.get_ref_die_offset ();
18500 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18501 || cu->per_cu->is_dwz);
18502 break;
18503 case DW_AT_sibling:
18504 /* Ignore absolute siblings, they might point outside of
18505 the current compile unit. */
18506 if (attr.form == DW_FORM_ref_addr)
18507 complaint (_("ignoring absolute DW_AT_sibling"));
18508 else
18509 {
18510 const gdb_byte *buffer = reader->buffer;
18511 sect_offset off = attr.get_ref_die_offset ();
18512 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18513
18514 if (sibling_ptr < info_ptr)
18515 complaint (_("DW_AT_sibling points backwards"));
18516 else if (sibling_ptr > reader->buffer_end)
18517 reader->die_section->overflow_complaint ();
18518 else
18519 sibling = sibling_ptr;
18520 }
18521 break;
18522 case DW_AT_byte_size:
18523 has_byte_size = 1;
18524 break;
18525 case DW_AT_const_value:
18526 has_const_value = 1;
18527 break;
18528 case DW_AT_calling_convention:
18529 /* DWARF doesn't provide a way to identify a program's source-level
18530 entry point. DW_AT_calling_convention attributes are only meant
18531 to describe functions' calling conventions.
18532
18533 However, because it's a necessary piece of information in
18534 Fortran, and before DWARF 4 DW_CC_program was the only
18535 piece of debugging information whose definition refers to
18536 a 'main program' at all, several compilers marked Fortran
18537 main programs with DW_CC_program --- even when those
18538 functions use the standard calling conventions.
18539
18540 Although DWARF now specifies a way to provide this
18541 information, we support this practice for backward
18542 compatibility. */
18543 if (DW_UNSND (&attr) == DW_CC_program
18544 && cu->language == language_fortran)
18545 main_subprogram = 1;
18546 break;
18547 case DW_AT_inline:
18548 if (DW_UNSND (&attr) == DW_INL_inlined
18549 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18550 may_be_inlined = 1;
18551 break;
18552
18553 case DW_AT_import:
18554 if (tag == DW_TAG_imported_unit)
18555 {
18556 d.sect_off = attr.get_ref_die_offset ();
18557 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18558 || cu->per_cu->is_dwz);
18559 }
18560 break;
18561
18562 case DW_AT_main_subprogram:
18563 main_subprogram = DW_UNSND (&attr);
18564 break;
18565
18566 case DW_AT_ranges:
18567 {
18568 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18569 but that requires a full DIE, so instead we just
18570 reimplement it. */
18571 int need_ranges_base = tag != DW_TAG_compile_unit;
18572 unsigned int ranges_offset = (DW_UNSND (&attr)
18573 + (need_ranges_base
18574 ? cu->ranges_base
18575 : 0));
18576
18577 /* Value of the DW_AT_ranges attribute is the offset in the
18578 .debug_ranges section. */
18579 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18580 nullptr))
18581 has_pc_info = 1;
18582 }
18583 break;
18584
18585 default:
18586 break;
18587 }
18588 }
18589
18590 /* For Ada, if both the name and the linkage name appear, we prefer
18591 the latter. This lets "catch exception" work better, regardless
18592 of the order in which the name and linkage name were emitted.
18593 Really, though, this is just a workaround for the fact that gdb
18594 doesn't store both the name and the linkage name. */
18595 if (cu->language == language_ada && linkage_name != nullptr)
18596 name = linkage_name;
18597
18598 if (high_pc_relative)
18599 highpc += lowpc;
18600
18601 if (has_low_pc_attr && has_high_pc_attr)
18602 {
18603 /* When using the GNU linker, .gnu.linkonce. sections are used to
18604 eliminate duplicate copies of functions and vtables and such.
18605 The linker will arbitrarily choose one and discard the others.
18606 The AT_*_pc values for such functions refer to local labels in
18607 these sections. If the section from that file was discarded, the
18608 labels are not in the output, so the relocs get a value of 0.
18609 If this is a discarded function, mark the pc bounds as invalid,
18610 so that GDB will ignore it. */
18611 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
18612 {
18613 struct objfile *objfile = dwarf2_per_objfile->objfile;
18614 struct gdbarch *gdbarch = objfile->arch ();
18615
18616 complaint (_("DW_AT_low_pc %s is zero "
18617 "for DIE at %s [in module %s]"),
18618 paddress (gdbarch, lowpc),
18619 sect_offset_str (sect_off),
18620 objfile_name (objfile));
18621 }
18622 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18623 else if (lowpc >= highpc)
18624 {
18625 struct objfile *objfile = dwarf2_per_objfile->objfile;
18626 struct gdbarch *gdbarch = objfile->arch ();
18627
18628 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18629 "for DIE at %s [in module %s]"),
18630 paddress (gdbarch, lowpc),
18631 paddress (gdbarch, highpc),
18632 sect_offset_str (sect_off),
18633 objfile_name (objfile));
18634 }
18635 else
18636 has_pc_info = 1;
18637 }
18638
18639 return info_ptr;
18640 }
18641
18642 /* Find a cached partial DIE at OFFSET in CU. */
18643
18644 struct partial_die_info *
18645 dwarf2_cu::find_partial_die (sect_offset sect_off)
18646 {
18647 struct partial_die_info *lookup_die = NULL;
18648 struct partial_die_info part_die (sect_off);
18649
18650 lookup_die = ((struct partial_die_info *)
18651 htab_find_with_hash (partial_dies, &part_die,
18652 to_underlying (sect_off)));
18653
18654 return lookup_die;
18655 }
18656
18657 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18658 except in the case of .debug_types DIEs which do not reference
18659 outside their CU (they do however referencing other types via
18660 DW_FORM_ref_sig8). */
18661
18662 static const struct cu_partial_die_info
18663 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18664 {
18665 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18666 struct objfile *objfile = dwarf2_per_objfile->objfile;
18667 struct dwarf2_per_cu_data *per_cu = NULL;
18668 struct partial_die_info *pd = NULL;
18669
18670 if (offset_in_dwz == cu->per_cu->is_dwz
18671 && cu->header.offset_in_cu_p (sect_off))
18672 {
18673 pd = cu->find_partial_die (sect_off);
18674 if (pd != NULL)
18675 return { cu, pd };
18676 /* We missed recording what we needed.
18677 Load all dies and try again. */
18678 per_cu = cu->per_cu;
18679 }
18680 else
18681 {
18682 /* TUs don't reference other CUs/TUs (except via type signatures). */
18683 if (cu->per_cu->is_debug_types)
18684 {
18685 error (_("Dwarf Error: Type Unit at offset %s contains"
18686 " external reference to offset %s [in module %s].\n"),
18687 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18688 bfd_get_filename (objfile->obfd));
18689 }
18690 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18691 dwarf2_per_objfile);
18692
18693 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18694 load_partial_comp_unit (per_cu, cu->per_objfile);
18695
18696 per_cu->cu->last_used = 0;
18697 pd = per_cu->cu->find_partial_die (sect_off);
18698 }
18699
18700 /* If we didn't find it, and not all dies have been loaded,
18701 load them all and try again. */
18702
18703 if (pd == NULL && per_cu->load_all_dies == 0)
18704 {
18705 per_cu->load_all_dies = 1;
18706
18707 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18708 THIS_CU->cu may already be in use. So we can't just free it and
18709 replace its DIEs with the ones we read in. Instead, we leave those
18710 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18711 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18712 set. */
18713 load_partial_comp_unit (per_cu, cu->per_objfile);
18714
18715 pd = per_cu->cu->find_partial_die (sect_off);
18716 }
18717
18718 if (pd == NULL)
18719 internal_error (__FILE__, __LINE__,
18720 _("could not find partial DIE %s "
18721 "in cache [from module %s]\n"),
18722 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18723 return { per_cu->cu, pd };
18724 }
18725
18726 /* See if we can figure out if the class lives in a namespace. We do
18727 this by looking for a member function; its demangled name will
18728 contain namespace info, if there is any. */
18729
18730 static void
18731 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18732 struct dwarf2_cu *cu)
18733 {
18734 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18735 what template types look like, because the demangler
18736 frequently doesn't give the same name as the debug info. We
18737 could fix this by only using the demangled name to get the
18738 prefix (but see comment in read_structure_type). */
18739
18740 struct partial_die_info *real_pdi;
18741 struct partial_die_info *child_pdi;
18742
18743 /* If this DIE (this DIE's specification, if any) has a parent, then
18744 we should not do this. We'll prepend the parent's fully qualified
18745 name when we create the partial symbol. */
18746
18747 real_pdi = struct_pdi;
18748 while (real_pdi->has_specification)
18749 {
18750 auto res = find_partial_die (real_pdi->spec_offset,
18751 real_pdi->spec_is_dwz, cu);
18752 real_pdi = res.pdi;
18753 cu = res.cu;
18754 }
18755
18756 if (real_pdi->die_parent != NULL)
18757 return;
18758
18759 for (child_pdi = struct_pdi->die_child;
18760 child_pdi != NULL;
18761 child_pdi = child_pdi->die_sibling)
18762 {
18763 if (child_pdi->tag == DW_TAG_subprogram
18764 && child_pdi->linkage_name != NULL)
18765 {
18766 gdb::unique_xmalloc_ptr<char> actual_class_name
18767 (language_class_name_from_physname (cu->language_defn,
18768 child_pdi->linkage_name));
18769 if (actual_class_name != NULL)
18770 {
18771 struct objfile *objfile = cu->per_objfile->objfile;
18772 struct_pdi->name = objfile->intern (actual_class_name.get ());
18773 }
18774 break;
18775 }
18776 }
18777 }
18778
18779 /* Return true if a DIE with TAG may have the DW_AT_const_value
18780 attribute. */
18781
18782 static bool
18783 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18784 {
18785 switch (tag)
18786 {
18787 case DW_TAG_constant:
18788 case DW_TAG_enumerator:
18789 case DW_TAG_formal_parameter:
18790 case DW_TAG_template_value_param:
18791 case DW_TAG_variable:
18792 return true;
18793 }
18794
18795 return false;
18796 }
18797
18798 void
18799 partial_die_info::fixup (struct dwarf2_cu *cu)
18800 {
18801 /* Once we've fixed up a die, there's no point in doing so again.
18802 This also avoids a memory leak if we were to call
18803 guess_partial_die_structure_name multiple times. */
18804 if (fixup_called)
18805 return;
18806
18807 /* If we found a reference attribute and the DIE has no name, try
18808 to find a name in the referred to DIE. */
18809
18810 if (name == NULL && has_specification)
18811 {
18812 struct partial_die_info *spec_die;
18813
18814 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18815 spec_die = res.pdi;
18816 cu = res.cu;
18817
18818 spec_die->fixup (cu);
18819
18820 if (spec_die->name)
18821 {
18822 name = spec_die->name;
18823
18824 /* Copy DW_AT_external attribute if it is set. */
18825 if (spec_die->is_external)
18826 is_external = spec_die->is_external;
18827 }
18828 }
18829
18830 if (!has_const_value && has_specification
18831 && can_have_DW_AT_const_value_p (tag))
18832 {
18833 struct partial_die_info *spec_die;
18834
18835 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18836 spec_die = res.pdi;
18837 cu = res.cu;
18838
18839 spec_die->fixup (cu);
18840
18841 if (spec_die->has_const_value)
18842 {
18843 /* Copy DW_AT_const_value attribute if it is set. */
18844 has_const_value = spec_die->has_const_value;
18845 }
18846 }
18847
18848 /* Set default names for some unnamed DIEs. */
18849
18850 if (name == NULL && tag == DW_TAG_namespace)
18851 name = CP_ANONYMOUS_NAMESPACE_STR;
18852
18853 /* If there is no parent die to provide a namespace, and there are
18854 children, see if we can determine the namespace from their linkage
18855 name. */
18856 if (cu->language == language_cplus
18857 && !cu->per_objfile->per_bfd->types.empty ()
18858 && die_parent == NULL
18859 && has_children
18860 && (tag == DW_TAG_class_type
18861 || tag == DW_TAG_structure_type
18862 || tag == DW_TAG_union_type))
18863 guess_partial_die_structure_name (this, cu);
18864
18865 /* GCC might emit a nameless struct or union that has a linkage
18866 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18867 if (name == NULL
18868 && (tag == DW_TAG_class_type
18869 || tag == DW_TAG_interface_type
18870 || tag == DW_TAG_structure_type
18871 || tag == DW_TAG_union_type)
18872 && linkage_name != NULL)
18873 {
18874 gdb::unique_xmalloc_ptr<char> demangled
18875 (gdb_demangle (linkage_name, DMGL_TYPES));
18876 if (demangled != nullptr)
18877 {
18878 const char *base;
18879
18880 /* Strip any leading namespaces/classes, keep only the base name.
18881 DW_AT_name for named DIEs does not contain the prefixes. */
18882 base = strrchr (demangled.get (), ':');
18883 if (base && base > demangled.get () && base[-1] == ':')
18884 base++;
18885 else
18886 base = demangled.get ();
18887
18888 struct objfile *objfile = cu->per_objfile->objfile;
18889 name = objfile->intern (base);
18890 }
18891 }
18892
18893 fixup_called = 1;
18894 }
18895
18896 /* Read the .debug_loclists header contents from the given SECTION in the
18897 HEADER. */
18898 static void
18899 read_loclist_header (struct loclist_header *header,
18900 struct dwarf2_section_info *section)
18901 {
18902 unsigned int bytes_read;
18903 bfd *abfd = section->get_bfd_owner ();
18904 const gdb_byte *info_ptr = section->buffer;
18905 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18906 info_ptr += bytes_read;
18907 header->version = read_2_bytes (abfd, info_ptr);
18908 info_ptr += 2;
18909 header->addr_size = read_1_byte (abfd, info_ptr);
18910 info_ptr += 1;
18911 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18912 info_ptr += 1;
18913 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18914 }
18915
18916 /* Return the DW_AT_loclists_base value for the CU. */
18917 static ULONGEST
18918 lookup_loclist_base (struct dwarf2_cu *cu)
18919 {
18920 /* For the .dwo unit, the loclist_base points to the first offset following
18921 the header. The header consists of the following entities-
18922 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18923 bit format)
18924 2. version (2 bytes)
18925 3. address size (1 byte)
18926 4. segment selector size (1 byte)
18927 5. offset entry count (4 bytes)
18928 These sizes are derived as per the DWARFv5 standard. */
18929 if (cu->dwo_unit != nullptr)
18930 {
18931 if (cu->header.initial_length_size == 4)
18932 return LOCLIST_HEADER_SIZE32;
18933 return LOCLIST_HEADER_SIZE64;
18934 }
18935 return cu->loclist_base;
18936 }
18937
18938 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18939 array of offsets in the .debug_loclists section. */
18940 static CORE_ADDR
18941 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18942 {
18943 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18944 struct objfile *objfile = dwarf2_per_objfile->objfile;
18945 bfd *abfd = objfile->obfd;
18946 ULONGEST loclist_base = lookup_loclist_base (cu);
18947 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18948
18949 section->read (objfile);
18950 if (section->buffer == NULL)
18951 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18952 "section [in module %s]"), objfile_name (objfile));
18953 struct loclist_header header;
18954 read_loclist_header (&header, section);
18955 if (loclist_index >= header.offset_entry_count)
18956 complaint (_("DW_FORM_loclistx pointing outside of "
18957 ".debug_loclists offset array [in module %s]"),
18958 objfile_name (objfile));
18959 if (loclist_base + loclist_index * cu->header.offset_size
18960 >= section->size)
18961 complaint (_("DW_FORM_loclistx pointing outside of "
18962 ".debug_loclists section [in module %s]"),
18963 objfile_name (objfile));
18964 const gdb_byte *info_ptr
18965 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18966
18967 if (cu->header.offset_size == 4)
18968 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18969 else
18970 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18971 }
18972
18973 /* Process the attributes that had to be skipped in the first round. These
18974 attributes are the ones that need str_offsets_base or addr_base attributes.
18975 They could not have been processed in the first round, because at the time
18976 the values of str_offsets_base or addr_base may not have been known. */
18977 static void
18978 read_attribute_reprocess (const struct die_reader_specs *reader,
18979 struct attribute *attr)
18980 {
18981 struct dwarf2_cu *cu = reader->cu;
18982 switch (attr->form)
18983 {
18984 case DW_FORM_addrx:
18985 case DW_FORM_GNU_addr_index:
18986 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18987 break;
18988 case DW_FORM_loclistx:
18989 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18990 break;
18991 case DW_FORM_strx:
18992 case DW_FORM_strx1:
18993 case DW_FORM_strx2:
18994 case DW_FORM_strx3:
18995 case DW_FORM_strx4:
18996 case DW_FORM_GNU_str_index:
18997 {
18998 unsigned int str_index = DW_UNSND (attr);
18999 if (reader->dwo_file != NULL)
19000 {
19001 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19002 DW_STRING_IS_CANONICAL (attr) = 0;
19003 }
19004 else
19005 {
19006 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19007 DW_STRING_IS_CANONICAL (attr) = 0;
19008 }
19009 break;
19010 }
19011 default:
19012 gdb_assert_not_reached (_("Unexpected DWARF form."));
19013 }
19014 }
19015
19016 /* Read an attribute value described by an attribute form. */
19017
19018 static const gdb_byte *
19019 read_attribute_value (const struct die_reader_specs *reader,
19020 struct attribute *attr, unsigned form,
19021 LONGEST implicit_const, const gdb_byte *info_ptr,
19022 bool *need_reprocess)
19023 {
19024 struct dwarf2_cu *cu = reader->cu;
19025 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19026 struct objfile *objfile = dwarf2_per_objfile->objfile;
19027 bfd *abfd = reader->abfd;
19028 struct comp_unit_head *cu_header = &cu->header;
19029 unsigned int bytes_read;
19030 struct dwarf_block *blk;
19031 *need_reprocess = false;
19032
19033 attr->form = (enum dwarf_form) form;
19034 switch (form)
19035 {
19036 case DW_FORM_ref_addr:
19037 if (cu->header.version == 2)
19038 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19039 &bytes_read);
19040 else
19041 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19042 &bytes_read);
19043 info_ptr += bytes_read;
19044 break;
19045 case DW_FORM_GNU_ref_alt:
19046 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19047 info_ptr += bytes_read;
19048 break;
19049 case DW_FORM_addr:
19050 {
19051 struct gdbarch *gdbarch = objfile->arch ();
19052 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19053 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19054 info_ptr += bytes_read;
19055 }
19056 break;
19057 case DW_FORM_block2:
19058 blk = dwarf_alloc_block (cu);
19059 blk->size = read_2_bytes (abfd, info_ptr);
19060 info_ptr += 2;
19061 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19062 info_ptr += blk->size;
19063 DW_BLOCK (attr) = blk;
19064 break;
19065 case DW_FORM_block4:
19066 blk = dwarf_alloc_block (cu);
19067 blk->size = read_4_bytes (abfd, info_ptr);
19068 info_ptr += 4;
19069 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19070 info_ptr += blk->size;
19071 DW_BLOCK (attr) = blk;
19072 break;
19073 case DW_FORM_data2:
19074 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19075 info_ptr += 2;
19076 break;
19077 case DW_FORM_data4:
19078 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19079 info_ptr += 4;
19080 break;
19081 case DW_FORM_data8:
19082 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19083 info_ptr += 8;
19084 break;
19085 case DW_FORM_data16:
19086 blk = dwarf_alloc_block (cu);
19087 blk->size = 16;
19088 blk->data = read_n_bytes (abfd, info_ptr, 16);
19089 info_ptr += 16;
19090 DW_BLOCK (attr) = blk;
19091 break;
19092 case DW_FORM_sec_offset:
19093 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19094 info_ptr += bytes_read;
19095 break;
19096 case DW_FORM_loclistx:
19097 {
19098 *need_reprocess = true;
19099 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19100 info_ptr += bytes_read;
19101 }
19102 break;
19103 case DW_FORM_string:
19104 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19105 DW_STRING_IS_CANONICAL (attr) = 0;
19106 info_ptr += bytes_read;
19107 break;
19108 case DW_FORM_strp:
19109 if (!cu->per_cu->is_dwz)
19110 {
19111 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19112 abfd, info_ptr, cu_header,
19113 &bytes_read);
19114 DW_STRING_IS_CANONICAL (attr) = 0;
19115 info_ptr += bytes_read;
19116 break;
19117 }
19118 /* FALLTHROUGH */
19119 case DW_FORM_line_strp:
19120 if (!cu->per_cu->is_dwz)
19121 {
19122 DW_STRING (attr)
19123 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19124 &bytes_read);
19125 DW_STRING_IS_CANONICAL (attr) = 0;
19126 info_ptr += bytes_read;
19127 break;
19128 }
19129 /* FALLTHROUGH */
19130 case DW_FORM_GNU_strp_alt:
19131 {
19132 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19133 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19134 &bytes_read);
19135
19136 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
19137 DW_STRING_IS_CANONICAL (attr) = 0;
19138 info_ptr += bytes_read;
19139 }
19140 break;
19141 case DW_FORM_exprloc:
19142 case DW_FORM_block:
19143 blk = dwarf_alloc_block (cu);
19144 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19145 info_ptr += bytes_read;
19146 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19147 info_ptr += blk->size;
19148 DW_BLOCK (attr) = blk;
19149 break;
19150 case DW_FORM_block1:
19151 blk = dwarf_alloc_block (cu);
19152 blk->size = read_1_byte (abfd, info_ptr);
19153 info_ptr += 1;
19154 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19155 info_ptr += blk->size;
19156 DW_BLOCK (attr) = blk;
19157 break;
19158 case DW_FORM_data1:
19159 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19160 info_ptr += 1;
19161 break;
19162 case DW_FORM_flag:
19163 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19164 info_ptr += 1;
19165 break;
19166 case DW_FORM_flag_present:
19167 DW_UNSND (attr) = 1;
19168 break;
19169 case DW_FORM_sdata:
19170 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19171 info_ptr += bytes_read;
19172 break;
19173 case DW_FORM_udata:
19174 case DW_FORM_rnglistx:
19175 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19176 info_ptr += bytes_read;
19177 break;
19178 case DW_FORM_ref1:
19179 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19180 + read_1_byte (abfd, info_ptr));
19181 info_ptr += 1;
19182 break;
19183 case DW_FORM_ref2:
19184 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19185 + read_2_bytes (abfd, info_ptr));
19186 info_ptr += 2;
19187 break;
19188 case DW_FORM_ref4:
19189 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19190 + read_4_bytes (abfd, info_ptr));
19191 info_ptr += 4;
19192 break;
19193 case DW_FORM_ref8:
19194 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19195 + read_8_bytes (abfd, info_ptr));
19196 info_ptr += 8;
19197 break;
19198 case DW_FORM_ref_sig8:
19199 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19200 info_ptr += 8;
19201 break;
19202 case DW_FORM_ref_udata:
19203 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19204 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19205 info_ptr += bytes_read;
19206 break;
19207 case DW_FORM_indirect:
19208 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19209 info_ptr += bytes_read;
19210 if (form == DW_FORM_implicit_const)
19211 {
19212 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19213 info_ptr += bytes_read;
19214 }
19215 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19216 info_ptr, need_reprocess);
19217 break;
19218 case DW_FORM_implicit_const:
19219 DW_SND (attr) = implicit_const;
19220 break;
19221 case DW_FORM_addrx:
19222 case DW_FORM_GNU_addr_index:
19223 *need_reprocess = true;
19224 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19225 info_ptr += bytes_read;
19226 break;
19227 case DW_FORM_strx:
19228 case DW_FORM_strx1:
19229 case DW_FORM_strx2:
19230 case DW_FORM_strx3:
19231 case DW_FORM_strx4:
19232 case DW_FORM_GNU_str_index:
19233 {
19234 ULONGEST str_index;
19235 if (form == DW_FORM_strx1)
19236 {
19237 str_index = read_1_byte (abfd, info_ptr);
19238 info_ptr += 1;
19239 }
19240 else if (form == DW_FORM_strx2)
19241 {
19242 str_index = read_2_bytes (abfd, info_ptr);
19243 info_ptr += 2;
19244 }
19245 else if (form == DW_FORM_strx3)
19246 {
19247 str_index = read_3_bytes (abfd, info_ptr);
19248 info_ptr += 3;
19249 }
19250 else if (form == DW_FORM_strx4)
19251 {
19252 str_index = read_4_bytes (abfd, info_ptr);
19253 info_ptr += 4;
19254 }
19255 else
19256 {
19257 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19258 info_ptr += bytes_read;
19259 }
19260 *need_reprocess = true;
19261 DW_UNSND (attr) = str_index;
19262 }
19263 break;
19264 default:
19265 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19266 dwarf_form_name (form),
19267 bfd_get_filename (abfd));
19268 }
19269
19270 /* Super hack. */
19271 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19272 attr->form = DW_FORM_GNU_ref_alt;
19273
19274 /* We have seen instances where the compiler tried to emit a byte
19275 size attribute of -1 which ended up being encoded as an unsigned
19276 0xffffffff. Although 0xffffffff is technically a valid size value,
19277 an object of this size seems pretty unlikely so we can relatively
19278 safely treat these cases as if the size attribute was invalid and
19279 treat them as zero by default. */
19280 if (attr->name == DW_AT_byte_size
19281 && form == DW_FORM_data4
19282 && DW_UNSND (attr) >= 0xffffffff)
19283 {
19284 complaint
19285 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19286 hex_string (DW_UNSND (attr)));
19287 DW_UNSND (attr) = 0;
19288 }
19289
19290 return info_ptr;
19291 }
19292
19293 /* Read an attribute described by an abbreviated attribute. */
19294
19295 static const gdb_byte *
19296 read_attribute (const struct die_reader_specs *reader,
19297 struct attribute *attr, struct attr_abbrev *abbrev,
19298 const gdb_byte *info_ptr, bool *need_reprocess)
19299 {
19300 attr->name = abbrev->name;
19301 return read_attribute_value (reader, attr, abbrev->form,
19302 abbrev->implicit_const, info_ptr,
19303 need_reprocess);
19304 }
19305
19306 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19307
19308 static const char *
19309 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19310 LONGEST str_offset)
19311 {
19312 return dwarf2_per_objfile->per_bfd->str.read_string
19313 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
19314 }
19315
19316 /* Return pointer to string at .debug_str offset as read from BUF.
19317 BUF is assumed to be in a compilation unit described by CU_HEADER.
19318 Return *BYTES_READ_PTR count of bytes read from BUF. */
19319
19320 static const char *
19321 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19322 const gdb_byte *buf,
19323 const struct comp_unit_head *cu_header,
19324 unsigned int *bytes_read_ptr)
19325 {
19326 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19327
19328 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
19329 }
19330
19331 /* See read.h. */
19332
19333 const char *
19334 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19335 const struct comp_unit_head *cu_header,
19336 unsigned int *bytes_read_ptr)
19337 {
19338 bfd *abfd = objfile->obfd;
19339 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19340
19341 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19342 }
19343
19344 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19345 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19346 ADDR_SIZE is the size of addresses from the CU header. */
19347
19348 static CORE_ADDR
19349 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19350 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19351 int addr_size)
19352 {
19353 struct objfile *objfile = dwarf2_per_objfile->objfile;
19354 bfd *abfd = objfile->obfd;
19355 const gdb_byte *info_ptr;
19356 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19357
19358 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19359 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
19360 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19361 objfile_name (objfile));
19362 if (addr_base_or_zero + addr_index * addr_size
19363 >= dwarf2_per_objfile->per_bfd->addr.size)
19364 error (_("DW_FORM_addr_index pointing outside of "
19365 ".debug_addr section [in module %s]"),
19366 objfile_name (objfile));
19367 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
19368 + addr_base_or_zero + addr_index * addr_size);
19369 if (addr_size == 4)
19370 return bfd_get_32 (abfd, info_ptr);
19371 else
19372 return bfd_get_64 (abfd, info_ptr);
19373 }
19374
19375 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19376
19377 static CORE_ADDR
19378 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19379 {
19380 return read_addr_index_1 (cu->per_objfile, addr_index,
19381 cu->addr_base, cu->header.addr_size);
19382 }
19383
19384 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19385
19386 static CORE_ADDR
19387 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19388 unsigned int *bytes_read)
19389 {
19390 bfd *abfd = cu->per_objfile->objfile->obfd;
19391 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19392
19393 return read_addr_index (cu, addr_index);
19394 }
19395
19396 /* See read.h. */
19397
19398 CORE_ADDR
19399 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
19400 {
19401 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19402 struct dwarf2_cu *cu = per_cu->cu;
19403 gdb::optional<ULONGEST> addr_base;
19404 int addr_size;
19405
19406 /* We need addr_base and addr_size.
19407 If we don't have PER_CU->cu, we have to get it.
19408 Nasty, but the alternative is storing the needed info in PER_CU,
19409 which at this point doesn't seem justified: it's not clear how frequently
19410 it would get used and it would increase the size of every PER_CU.
19411 Entry points like dwarf2_per_cu_addr_size do a similar thing
19412 so we're not in uncharted territory here.
19413 Alas we need to be a bit more complicated as addr_base is contained
19414 in the DIE.
19415
19416 We don't need to read the entire CU(/TU).
19417 We just need the header and top level die.
19418
19419 IWBN to use the aging mechanism to let us lazily later discard the CU.
19420 For now we skip this optimization. */
19421
19422 if (cu != NULL)
19423 {
19424 addr_base = cu->addr_base;
19425 addr_size = cu->header.addr_size;
19426 }
19427 else
19428 {
19429 cutu_reader reader (per_cu, dwarf2_per_objfile, NULL, 0, false);
19430 addr_base = reader.cu->addr_base;
19431 addr_size = reader.cu->header.addr_size;
19432 }
19433
19434 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19435 addr_size);
19436 }
19437
19438 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19439 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19440 DWO file. */
19441
19442 static const char *
19443 read_str_index (struct dwarf2_cu *cu,
19444 struct dwarf2_section_info *str_section,
19445 struct dwarf2_section_info *str_offsets_section,
19446 ULONGEST str_offsets_base, ULONGEST str_index)
19447 {
19448 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19449 struct objfile *objfile = dwarf2_per_objfile->objfile;
19450 const char *objf_name = objfile_name (objfile);
19451 bfd *abfd = objfile->obfd;
19452 const gdb_byte *info_ptr;
19453 ULONGEST str_offset;
19454 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19455
19456 str_section->read (objfile);
19457 str_offsets_section->read (objfile);
19458 if (str_section->buffer == NULL)
19459 error (_("%s used without %s section"
19460 " in CU at offset %s [in module %s]"),
19461 form_name, str_section->get_name (),
19462 sect_offset_str (cu->header.sect_off), objf_name);
19463 if (str_offsets_section->buffer == NULL)
19464 error (_("%s used without %s section"
19465 " in CU at offset %s [in module %s]"),
19466 form_name, str_section->get_name (),
19467 sect_offset_str (cu->header.sect_off), objf_name);
19468 info_ptr = (str_offsets_section->buffer
19469 + str_offsets_base
19470 + str_index * cu->header.offset_size);
19471 if (cu->header.offset_size == 4)
19472 str_offset = bfd_get_32 (abfd, info_ptr);
19473 else
19474 str_offset = bfd_get_64 (abfd, info_ptr);
19475 if (str_offset >= str_section->size)
19476 error (_("Offset from %s pointing outside of"
19477 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19478 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19479 return (const char *) (str_section->buffer + str_offset);
19480 }
19481
19482 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19483
19484 static const char *
19485 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19486 {
19487 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19488 ? reader->cu->header.addr_size : 0;
19489 return read_str_index (reader->cu,
19490 &reader->dwo_file->sections.str,
19491 &reader->dwo_file->sections.str_offsets,
19492 str_offsets_base, str_index);
19493 }
19494
19495 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19496
19497 static const char *
19498 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19499 {
19500 struct objfile *objfile = cu->per_objfile->objfile;
19501 const char *objf_name = objfile_name (objfile);
19502 static const char form_name[] = "DW_FORM_GNU_str_index";
19503 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19504
19505 if (!cu->str_offsets_base.has_value ())
19506 error (_("%s used in Fission stub without %s"
19507 " in CU at offset 0x%lx [in module %s]"),
19508 form_name, str_offsets_attr_name,
19509 (long) cu->header.offset_size, objf_name);
19510
19511 return read_str_index (cu,
19512 &cu->per_objfile->per_bfd->str,
19513 &cu->per_objfile->per_bfd->str_offsets,
19514 *cu->str_offsets_base, str_index);
19515 }
19516
19517 /* Return the length of an LEB128 number in BUF. */
19518
19519 static int
19520 leb128_size (const gdb_byte *buf)
19521 {
19522 const gdb_byte *begin = buf;
19523 gdb_byte byte;
19524
19525 while (1)
19526 {
19527 byte = *buf++;
19528 if ((byte & 128) == 0)
19529 return buf - begin;
19530 }
19531 }
19532
19533 static void
19534 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19535 {
19536 switch (lang)
19537 {
19538 case DW_LANG_C89:
19539 case DW_LANG_C99:
19540 case DW_LANG_C11:
19541 case DW_LANG_C:
19542 case DW_LANG_UPC:
19543 cu->language = language_c;
19544 break;
19545 case DW_LANG_Java:
19546 case DW_LANG_C_plus_plus:
19547 case DW_LANG_C_plus_plus_11:
19548 case DW_LANG_C_plus_plus_14:
19549 cu->language = language_cplus;
19550 break;
19551 case DW_LANG_D:
19552 cu->language = language_d;
19553 break;
19554 case DW_LANG_Fortran77:
19555 case DW_LANG_Fortran90:
19556 case DW_LANG_Fortran95:
19557 case DW_LANG_Fortran03:
19558 case DW_LANG_Fortran08:
19559 cu->language = language_fortran;
19560 break;
19561 case DW_LANG_Go:
19562 cu->language = language_go;
19563 break;
19564 case DW_LANG_Mips_Assembler:
19565 cu->language = language_asm;
19566 break;
19567 case DW_LANG_Ada83:
19568 case DW_LANG_Ada95:
19569 cu->language = language_ada;
19570 break;
19571 case DW_LANG_Modula2:
19572 cu->language = language_m2;
19573 break;
19574 case DW_LANG_Pascal83:
19575 cu->language = language_pascal;
19576 break;
19577 case DW_LANG_ObjC:
19578 cu->language = language_objc;
19579 break;
19580 case DW_LANG_Rust:
19581 case DW_LANG_Rust_old:
19582 cu->language = language_rust;
19583 break;
19584 case DW_LANG_Cobol74:
19585 case DW_LANG_Cobol85:
19586 default:
19587 cu->language = language_minimal;
19588 break;
19589 }
19590 cu->language_defn = language_def (cu->language);
19591 }
19592
19593 /* Return the named attribute or NULL if not there. */
19594
19595 static struct attribute *
19596 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19597 {
19598 for (;;)
19599 {
19600 unsigned int i;
19601 struct attribute *spec = NULL;
19602
19603 for (i = 0; i < die->num_attrs; ++i)
19604 {
19605 if (die->attrs[i].name == name)
19606 return &die->attrs[i];
19607 if (die->attrs[i].name == DW_AT_specification
19608 || die->attrs[i].name == DW_AT_abstract_origin)
19609 spec = &die->attrs[i];
19610 }
19611
19612 if (!spec)
19613 break;
19614
19615 die = follow_die_ref (die, spec, &cu);
19616 }
19617
19618 return NULL;
19619 }
19620
19621 /* Return the string associated with a string-typed attribute, or NULL if it
19622 is either not found or is of an incorrect type. */
19623
19624 static const char *
19625 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19626 {
19627 struct attribute *attr;
19628 const char *str = NULL;
19629
19630 attr = dwarf2_attr (die, name, cu);
19631
19632 if (attr != NULL)
19633 {
19634 str = attr->value_as_string ();
19635 if (str == nullptr)
19636 complaint (_("string type expected for attribute %s for "
19637 "DIE at %s in module %s"),
19638 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19639 objfile_name (cu->per_objfile->objfile));
19640 }
19641
19642 return str;
19643 }
19644
19645 /* Return the dwo name or NULL if not present. If present, it is in either
19646 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19647 static const char *
19648 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19649 {
19650 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19651 if (dwo_name == nullptr)
19652 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19653 return dwo_name;
19654 }
19655
19656 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19657 and holds a non-zero value. This function should only be used for
19658 DW_FORM_flag or DW_FORM_flag_present attributes. */
19659
19660 static int
19661 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19662 {
19663 struct attribute *attr = dwarf2_attr (die, name, cu);
19664
19665 return (attr && DW_UNSND (attr));
19666 }
19667
19668 static int
19669 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19670 {
19671 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19672 which value is non-zero. However, we have to be careful with
19673 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19674 (via dwarf2_flag_true_p) follows this attribute. So we may
19675 end up accidently finding a declaration attribute that belongs
19676 to a different DIE referenced by the specification attribute,
19677 even though the given DIE does not have a declaration attribute. */
19678 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19679 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19680 }
19681
19682 /* Return the die giving the specification for DIE, if there is
19683 one. *SPEC_CU is the CU containing DIE on input, and the CU
19684 containing the return value on output. If there is no
19685 specification, but there is an abstract origin, that is
19686 returned. */
19687
19688 static struct die_info *
19689 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19690 {
19691 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19692 *spec_cu);
19693
19694 if (spec_attr == NULL)
19695 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19696
19697 if (spec_attr == NULL)
19698 return NULL;
19699 else
19700 return follow_die_ref (die, spec_attr, spec_cu);
19701 }
19702
19703 /* Stub for free_line_header to match void * callback types. */
19704
19705 static void
19706 free_line_header_voidp (void *arg)
19707 {
19708 struct line_header *lh = (struct line_header *) arg;
19709
19710 delete lh;
19711 }
19712
19713 /* A convenience function to find the proper .debug_line section for a CU. */
19714
19715 static struct dwarf2_section_info *
19716 get_debug_line_section (struct dwarf2_cu *cu)
19717 {
19718 struct dwarf2_section_info *section;
19719 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19720
19721 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19722 DWO file. */
19723 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19724 section = &cu->dwo_unit->dwo_file->sections.line;
19725 else if (cu->per_cu->is_dwz)
19726 {
19727 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19728
19729 section = &dwz->line;
19730 }
19731 else
19732 section = &dwarf2_per_objfile->per_bfd->line;
19733
19734 return section;
19735 }
19736
19737 /* Read the statement program header starting at OFFSET in
19738 .debug_line, or .debug_line.dwo. Return a pointer
19739 to a struct line_header, allocated using xmalloc.
19740 Returns NULL if there is a problem reading the header, e.g., if it
19741 has a version we don't understand.
19742
19743 NOTE: the strings in the include directory and file name tables of
19744 the returned object point into the dwarf line section buffer,
19745 and must not be freed. */
19746
19747 static line_header_up
19748 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19749 {
19750 struct dwarf2_section_info *section;
19751 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19752
19753 section = get_debug_line_section (cu);
19754 section->read (dwarf2_per_objfile->objfile);
19755 if (section->buffer == NULL)
19756 {
19757 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19758 complaint (_("missing .debug_line.dwo section"));
19759 else
19760 complaint (_("missing .debug_line section"));
19761 return 0;
19762 }
19763
19764 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19765 dwarf2_per_objfile, section,
19766 &cu->header);
19767 }
19768
19769 /* Subroutine of dwarf_decode_lines to simplify it.
19770 Return the file name of the psymtab for the given file_entry.
19771 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19772 If space for the result is malloc'd, *NAME_HOLDER will be set.
19773 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19774
19775 static const char *
19776 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19777 const dwarf2_psymtab *pst,
19778 const char *comp_dir,
19779 gdb::unique_xmalloc_ptr<char> *name_holder)
19780 {
19781 const char *include_name = fe.name;
19782 const char *include_name_to_compare = include_name;
19783 const char *pst_filename;
19784 int file_is_pst;
19785
19786 const char *dir_name = fe.include_dir (lh);
19787
19788 gdb::unique_xmalloc_ptr<char> hold_compare;
19789 if (!IS_ABSOLUTE_PATH (include_name)
19790 && (dir_name != NULL || comp_dir != NULL))
19791 {
19792 /* Avoid creating a duplicate psymtab for PST.
19793 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19794 Before we do the comparison, however, we need to account
19795 for DIR_NAME and COMP_DIR.
19796 First prepend dir_name (if non-NULL). If we still don't
19797 have an absolute path prepend comp_dir (if non-NULL).
19798 However, the directory we record in the include-file's
19799 psymtab does not contain COMP_DIR (to match the
19800 corresponding symtab(s)).
19801
19802 Example:
19803
19804 bash$ cd /tmp
19805 bash$ gcc -g ./hello.c
19806 include_name = "hello.c"
19807 dir_name = "."
19808 DW_AT_comp_dir = comp_dir = "/tmp"
19809 DW_AT_name = "./hello.c"
19810
19811 */
19812
19813 if (dir_name != NULL)
19814 {
19815 name_holder->reset (concat (dir_name, SLASH_STRING,
19816 include_name, (char *) NULL));
19817 include_name = name_holder->get ();
19818 include_name_to_compare = include_name;
19819 }
19820 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19821 {
19822 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19823 include_name, (char *) NULL));
19824 include_name_to_compare = hold_compare.get ();
19825 }
19826 }
19827
19828 pst_filename = pst->filename;
19829 gdb::unique_xmalloc_ptr<char> copied_name;
19830 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19831 {
19832 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19833 pst_filename, (char *) NULL));
19834 pst_filename = copied_name.get ();
19835 }
19836
19837 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19838
19839 if (file_is_pst)
19840 return NULL;
19841 return include_name;
19842 }
19843
19844 /* State machine to track the state of the line number program. */
19845
19846 class lnp_state_machine
19847 {
19848 public:
19849 /* Initialize a machine state for the start of a line number
19850 program. */
19851 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19852 bool record_lines_p);
19853
19854 file_entry *current_file ()
19855 {
19856 /* lh->file_names is 0-based, but the file name numbers in the
19857 statement program are 1-based. */
19858 return m_line_header->file_name_at (m_file);
19859 }
19860
19861 /* Record the line in the state machine. END_SEQUENCE is true if
19862 we're processing the end of a sequence. */
19863 void record_line (bool end_sequence);
19864
19865 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19866 nop-out rest of the lines in this sequence. */
19867 void check_line_address (struct dwarf2_cu *cu,
19868 const gdb_byte *line_ptr,
19869 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19870
19871 void handle_set_discriminator (unsigned int discriminator)
19872 {
19873 m_discriminator = discriminator;
19874 m_line_has_non_zero_discriminator |= discriminator != 0;
19875 }
19876
19877 /* Handle DW_LNE_set_address. */
19878 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19879 {
19880 m_op_index = 0;
19881 address += baseaddr;
19882 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19883 }
19884
19885 /* Handle DW_LNS_advance_pc. */
19886 void handle_advance_pc (CORE_ADDR adjust);
19887
19888 /* Handle a special opcode. */
19889 void handle_special_opcode (unsigned char op_code);
19890
19891 /* Handle DW_LNS_advance_line. */
19892 void handle_advance_line (int line_delta)
19893 {
19894 advance_line (line_delta);
19895 }
19896
19897 /* Handle DW_LNS_set_file. */
19898 void handle_set_file (file_name_index file);
19899
19900 /* Handle DW_LNS_negate_stmt. */
19901 void handle_negate_stmt ()
19902 {
19903 m_is_stmt = !m_is_stmt;
19904 }
19905
19906 /* Handle DW_LNS_const_add_pc. */
19907 void handle_const_add_pc ();
19908
19909 /* Handle DW_LNS_fixed_advance_pc. */
19910 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19911 {
19912 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19913 m_op_index = 0;
19914 }
19915
19916 /* Handle DW_LNS_copy. */
19917 void handle_copy ()
19918 {
19919 record_line (false);
19920 m_discriminator = 0;
19921 }
19922
19923 /* Handle DW_LNE_end_sequence. */
19924 void handle_end_sequence ()
19925 {
19926 m_currently_recording_lines = true;
19927 }
19928
19929 private:
19930 /* Advance the line by LINE_DELTA. */
19931 void advance_line (int line_delta)
19932 {
19933 m_line += line_delta;
19934
19935 if (line_delta != 0)
19936 m_line_has_non_zero_discriminator = m_discriminator != 0;
19937 }
19938
19939 struct dwarf2_cu *m_cu;
19940
19941 gdbarch *m_gdbarch;
19942
19943 /* True if we're recording lines.
19944 Otherwise we're building partial symtabs and are just interested in
19945 finding include files mentioned by the line number program. */
19946 bool m_record_lines_p;
19947
19948 /* The line number header. */
19949 line_header *m_line_header;
19950
19951 /* These are part of the standard DWARF line number state machine,
19952 and initialized according to the DWARF spec. */
19953
19954 unsigned char m_op_index = 0;
19955 /* The line table index of the current file. */
19956 file_name_index m_file = 1;
19957 unsigned int m_line = 1;
19958
19959 /* These are initialized in the constructor. */
19960
19961 CORE_ADDR m_address;
19962 bool m_is_stmt;
19963 unsigned int m_discriminator;
19964
19965 /* Additional bits of state we need to track. */
19966
19967 /* The last file that we called dwarf2_start_subfile for.
19968 This is only used for TLLs. */
19969 unsigned int m_last_file = 0;
19970 /* The last file a line number was recorded for. */
19971 struct subfile *m_last_subfile = NULL;
19972
19973 /* When true, record the lines we decode. */
19974 bool m_currently_recording_lines = false;
19975
19976 /* The last line number that was recorded, used to coalesce
19977 consecutive entries for the same line. This can happen, for
19978 example, when discriminators are present. PR 17276. */
19979 unsigned int m_last_line = 0;
19980 bool m_line_has_non_zero_discriminator = false;
19981 };
19982
19983 void
19984 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19985 {
19986 CORE_ADDR addr_adj = (((m_op_index + adjust)
19987 / m_line_header->maximum_ops_per_instruction)
19988 * m_line_header->minimum_instruction_length);
19989 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19990 m_op_index = ((m_op_index + adjust)
19991 % m_line_header->maximum_ops_per_instruction);
19992 }
19993
19994 void
19995 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19996 {
19997 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19998 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19999 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20000 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20001 / m_line_header->maximum_ops_per_instruction)
20002 * m_line_header->minimum_instruction_length);
20003 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20004 m_op_index = ((m_op_index + adj_opcode_d)
20005 % m_line_header->maximum_ops_per_instruction);
20006
20007 int line_delta = m_line_header->line_base + adj_opcode_r;
20008 advance_line (line_delta);
20009 record_line (false);
20010 m_discriminator = 0;
20011 }
20012
20013 void
20014 lnp_state_machine::handle_set_file (file_name_index file)
20015 {
20016 m_file = file;
20017
20018 const file_entry *fe = current_file ();
20019 if (fe == NULL)
20020 dwarf2_debug_line_missing_file_complaint ();
20021 else if (m_record_lines_p)
20022 {
20023 const char *dir = fe->include_dir (m_line_header);
20024
20025 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20026 m_line_has_non_zero_discriminator = m_discriminator != 0;
20027 dwarf2_start_subfile (m_cu, fe->name, dir);
20028 }
20029 }
20030
20031 void
20032 lnp_state_machine::handle_const_add_pc ()
20033 {
20034 CORE_ADDR adjust
20035 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20036
20037 CORE_ADDR addr_adj
20038 = (((m_op_index + adjust)
20039 / m_line_header->maximum_ops_per_instruction)
20040 * m_line_header->minimum_instruction_length);
20041
20042 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20043 m_op_index = ((m_op_index + adjust)
20044 % m_line_header->maximum_ops_per_instruction);
20045 }
20046
20047 /* Return non-zero if we should add LINE to the line number table.
20048 LINE is the line to add, LAST_LINE is the last line that was added,
20049 LAST_SUBFILE is the subfile for LAST_LINE.
20050 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20051 had a non-zero discriminator.
20052
20053 We have to be careful in the presence of discriminators.
20054 E.g., for this line:
20055
20056 for (i = 0; i < 100000; i++);
20057
20058 clang can emit four line number entries for that one line,
20059 each with a different discriminator.
20060 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20061
20062 However, we want gdb to coalesce all four entries into one.
20063 Otherwise the user could stepi into the middle of the line and
20064 gdb would get confused about whether the pc really was in the
20065 middle of the line.
20066
20067 Things are further complicated by the fact that two consecutive
20068 line number entries for the same line is a heuristic used by gcc
20069 to denote the end of the prologue. So we can't just discard duplicate
20070 entries, we have to be selective about it. The heuristic we use is
20071 that we only collapse consecutive entries for the same line if at least
20072 one of those entries has a non-zero discriminator. PR 17276.
20073
20074 Note: Addresses in the line number state machine can never go backwards
20075 within one sequence, thus this coalescing is ok. */
20076
20077 static int
20078 dwarf_record_line_p (struct dwarf2_cu *cu,
20079 unsigned int line, unsigned int last_line,
20080 int line_has_non_zero_discriminator,
20081 struct subfile *last_subfile)
20082 {
20083 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20084 return 1;
20085 if (line != last_line)
20086 return 1;
20087 /* Same line for the same file that we've seen already.
20088 As a last check, for pr 17276, only record the line if the line
20089 has never had a non-zero discriminator. */
20090 if (!line_has_non_zero_discriminator)
20091 return 1;
20092 return 0;
20093 }
20094
20095 /* Use the CU's builder to record line number LINE beginning at
20096 address ADDRESS in the line table of subfile SUBFILE. */
20097
20098 static void
20099 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20100 unsigned int line, CORE_ADDR address, bool is_stmt,
20101 struct dwarf2_cu *cu)
20102 {
20103 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20104
20105 if (dwarf_line_debug)
20106 {
20107 fprintf_unfiltered (gdb_stdlog,
20108 "Recording line %u, file %s, address %s\n",
20109 line, lbasename (subfile->name),
20110 paddress (gdbarch, address));
20111 }
20112
20113 if (cu != nullptr)
20114 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20115 }
20116
20117 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20118 Mark the end of a set of line number records.
20119 The arguments are the same as for dwarf_record_line_1.
20120 If SUBFILE is NULL the request is ignored. */
20121
20122 static void
20123 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20124 CORE_ADDR address, struct dwarf2_cu *cu)
20125 {
20126 if (subfile == NULL)
20127 return;
20128
20129 if (dwarf_line_debug)
20130 {
20131 fprintf_unfiltered (gdb_stdlog,
20132 "Finishing current line, file %s, address %s\n",
20133 lbasename (subfile->name),
20134 paddress (gdbarch, address));
20135 }
20136
20137 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20138 }
20139
20140 void
20141 lnp_state_machine::record_line (bool end_sequence)
20142 {
20143 if (dwarf_line_debug)
20144 {
20145 fprintf_unfiltered (gdb_stdlog,
20146 "Processing actual line %u: file %u,"
20147 " address %s, is_stmt %u, discrim %u%s\n",
20148 m_line, m_file,
20149 paddress (m_gdbarch, m_address),
20150 m_is_stmt, m_discriminator,
20151 (end_sequence ? "\t(end sequence)" : ""));
20152 }
20153
20154 file_entry *fe = current_file ();
20155
20156 if (fe == NULL)
20157 dwarf2_debug_line_missing_file_complaint ();
20158 /* For now we ignore lines not starting on an instruction boundary.
20159 But not when processing end_sequence for compatibility with the
20160 previous version of the code. */
20161 else if (m_op_index == 0 || end_sequence)
20162 {
20163 fe->included_p = 1;
20164 if (m_record_lines_p)
20165 {
20166 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20167 || end_sequence)
20168 {
20169 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20170 m_currently_recording_lines ? m_cu : nullptr);
20171 }
20172
20173 if (!end_sequence)
20174 {
20175 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20176
20177 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20178 m_line_has_non_zero_discriminator,
20179 m_last_subfile))
20180 {
20181 buildsym_compunit *builder = m_cu->get_builder ();
20182 dwarf_record_line_1 (m_gdbarch,
20183 builder->get_current_subfile (),
20184 m_line, m_address, is_stmt,
20185 m_currently_recording_lines ? m_cu : nullptr);
20186 }
20187 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20188 m_last_line = m_line;
20189 }
20190 }
20191 }
20192 }
20193
20194 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20195 line_header *lh, bool record_lines_p)
20196 {
20197 m_cu = cu;
20198 m_gdbarch = arch;
20199 m_record_lines_p = record_lines_p;
20200 m_line_header = lh;
20201
20202 m_currently_recording_lines = true;
20203
20204 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20205 was a line entry for it so that the backend has a chance to adjust it
20206 and also record it in case it needs it. This is currently used by MIPS
20207 code, cf. `mips_adjust_dwarf2_line'. */
20208 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20209 m_is_stmt = lh->default_is_stmt;
20210 m_discriminator = 0;
20211 }
20212
20213 void
20214 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20215 const gdb_byte *line_ptr,
20216 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20217 {
20218 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20219 the pc range of the CU. However, we restrict the test to only ADDRESS
20220 values of zero to preserve GDB's previous behaviour which is to handle
20221 the specific case of a function being GC'd by the linker. */
20222
20223 if (address == 0 && address < unrelocated_lowpc)
20224 {
20225 /* This line table is for a function which has been
20226 GCd by the linker. Ignore it. PR gdb/12528 */
20227
20228 struct objfile *objfile = cu->per_objfile->objfile;
20229 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20230
20231 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20232 line_offset, objfile_name (objfile));
20233 m_currently_recording_lines = false;
20234 /* Note: m_currently_recording_lines is left as false until we see
20235 DW_LNE_end_sequence. */
20236 }
20237 }
20238
20239 /* Subroutine of dwarf_decode_lines to simplify it.
20240 Process the line number information in LH.
20241 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20242 program in order to set included_p for every referenced header. */
20243
20244 static void
20245 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20246 const int decode_for_pst_p, CORE_ADDR lowpc)
20247 {
20248 const gdb_byte *line_ptr, *extended_end;
20249 const gdb_byte *line_end;
20250 unsigned int bytes_read, extended_len;
20251 unsigned char op_code, extended_op;
20252 CORE_ADDR baseaddr;
20253 struct objfile *objfile = cu->per_objfile->objfile;
20254 bfd *abfd = objfile->obfd;
20255 struct gdbarch *gdbarch = objfile->arch ();
20256 /* True if we're recording line info (as opposed to building partial
20257 symtabs and just interested in finding include files mentioned by
20258 the line number program). */
20259 bool record_lines_p = !decode_for_pst_p;
20260
20261 baseaddr = objfile->text_section_offset ();
20262
20263 line_ptr = lh->statement_program_start;
20264 line_end = lh->statement_program_end;
20265
20266 /* Read the statement sequences until there's nothing left. */
20267 while (line_ptr < line_end)
20268 {
20269 /* The DWARF line number program state machine. Reset the state
20270 machine at the start of each sequence. */
20271 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20272 bool end_sequence = false;
20273
20274 if (record_lines_p)
20275 {
20276 /* Start a subfile for the current file of the state
20277 machine. */
20278 const file_entry *fe = state_machine.current_file ();
20279
20280 if (fe != NULL)
20281 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20282 }
20283
20284 /* Decode the table. */
20285 while (line_ptr < line_end && !end_sequence)
20286 {
20287 op_code = read_1_byte (abfd, line_ptr);
20288 line_ptr += 1;
20289
20290 if (op_code >= lh->opcode_base)
20291 {
20292 /* Special opcode. */
20293 state_machine.handle_special_opcode (op_code);
20294 }
20295 else switch (op_code)
20296 {
20297 case DW_LNS_extended_op:
20298 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20299 &bytes_read);
20300 line_ptr += bytes_read;
20301 extended_end = line_ptr + extended_len;
20302 extended_op = read_1_byte (abfd, line_ptr);
20303 line_ptr += 1;
20304 switch (extended_op)
20305 {
20306 case DW_LNE_end_sequence:
20307 state_machine.handle_end_sequence ();
20308 end_sequence = true;
20309 break;
20310 case DW_LNE_set_address:
20311 {
20312 CORE_ADDR address
20313 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20314 line_ptr += bytes_read;
20315
20316 state_machine.check_line_address (cu, line_ptr,
20317 lowpc - baseaddr, address);
20318 state_machine.handle_set_address (baseaddr, address);
20319 }
20320 break;
20321 case DW_LNE_define_file:
20322 {
20323 const char *cur_file;
20324 unsigned int mod_time, length;
20325 dir_index dindex;
20326
20327 cur_file = read_direct_string (abfd, line_ptr,
20328 &bytes_read);
20329 line_ptr += bytes_read;
20330 dindex = (dir_index)
20331 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20332 line_ptr += bytes_read;
20333 mod_time =
20334 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20335 line_ptr += bytes_read;
20336 length =
20337 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20338 line_ptr += bytes_read;
20339 lh->add_file_name (cur_file, dindex, mod_time, length);
20340 }
20341 break;
20342 case DW_LNE_set_discriminator:
20343 {
20344 /* The discriminator is not interesting to the
20345 debugger; just ignore it. We still need to
20346 check its value though:
20347 if there are consecutive entries for the same
20348 (non-prologue) line we want to coalesce them.
20349 PR 17276. */
20350 unsigned int discr
20351 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20352 line_ptr += bytes_read;
20353
20354 state_machine.handle_set_discriminator (discr);
20355 }
20356 break;
20357 default:
20358 complaint (_("mangled .debug_line section"));
20359 return;
20360 }
20361 /* Make sure that we parsed the extended op correctly. If e.g.
20362 we expected a different address size than the producer used,
20363 we may have read the wrong number of bytes. */
20364 if (line_ptr != extended_end)
20365 {
20366 complaint (_("mangled .debug_line section"));
20367 return;
20368 }
20369 break;
20370 case DW_LNS_copy:
20371 state_machine.handle_copy ();
20372 break;
20373 case DW_LNS_advance_pc:
20374 {
20375 CORE_ADDR adjust
20376 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20377 line_ptr += bytes_read;
20378
20379 state_machine.handle_advance_pc (adjust);
20380 }
20381 break;
20382 case DW_LNS_advance_line:
20383 {
20384 int line_delta
20385 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20386 line_ptr += bytes_read;
20387
20388 state_machine.handle_advance_line (line_delta);
20389 }
20390 break;
20391 case DW_LNS_set_file:
20392 {
20393 file_name_index file
20394 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20395 &bytes_read);
20396 line_ptr += bytes_read;
20397
20398 state_machine.handle_set_file (file);
20399 }
20400 break;
20401 case DW_LNS_set_column:
20402 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20403 line_ptr += bytes_read;
20404 break;
20405 case DW_LNS_negate_stmt:
20406 state_machine.handle_negate_stmt ();
20407 break;
20408 case DW_LNS_set_basic_block:
20409 break;
20410 /* Add to the address register of the state machine the
20411 address increment value corresponding to special opcode
20412 255. I.e., this value is scaled by the minimum
20413 instruction length since special opcode 255 would have
20414 scaled the increment. */
20415 case DW_LNS_const_add_pc:
20416 state_machine.handle_const_add_pc ();
20417 break;
20418 case DW_LNS_fixed_advance_pc:
20419 {
20420 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20421 line_ptr += 2;
20422
20423 state_machine.handle_fixed_advance_pc (addr_adj);
20424 }
20425 break;
20426 default:
20427 {
20428 /* Unknown standard opcode, ignore it. */
20429 int i;
20430
20431 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20432 {
20433 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20434 line_ptr += bytes_read;
20435 }
20436 }
20437 }
20438 }
20439
20440 if (!end_sequence)
20441 dwarf2_debug_line_missing_end_sequence_complaint ();
20442
20443 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20444 in which case we still finish recording the last line). */
20445 state_machine.record_line (true);
20446 }
20447 }
20448
20449 /* Decode the Line Number Program (LNP) for the given line_header
20450 structure and CU. The actual information extracted and the type
20451 of structures created from the LNP depends on the value of PST.
20452
20453 1. If PST is NULL, then this procedure uses the data from the program
20454 to create all necessary symbol tables, and their linetables.
20455
20456 2. If PST is not NULL, this procedure reads the program to determine
20457 the list of files included by the unit represented by PST, and
20458 builds all the associated partial symbol tables.
20459
20460 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20461 It is used for relative paths in the line table.
20462 NOTE: When processing partial symtabs (pst != NULL),
20463 comp_dir == pst->dirname.
20464
20465 NOTE: It is important that psymtabs have the same file name (via strcmp)
20466 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20467 symtab we don't use it in the name of the psymtabs we create.
20468 E.g. expand_line_sal requires this when finding psymtabs to expand.
20469 A good testcase for this is mb-inline.exp.
20470
20471 LOWPC is the lowest address in CU (or 0 if not known).
20472
20473 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20474 for its PC<->lines mapping information. Otherwise only the filename
20475 table is read in. */
20476
20477 static void
20478 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20479 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20480 CORE_ADDR lowpc, int decode_mapping)
20481 {
20482 struct objfile *objfile = cu->per_objfile->objfile;
20483 const int decode_for_pst_p = (pst != NULL);
20484
20485 if (decode_mapping)
20486 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20487
20488 if (decode_for_pst_p)
20489 {
20490 /* Now that we're done scanning the Line Header Program, we can
20491 create the psymtab of each included file. */
20492 for (auto &file_entry : lh->file_names ())
20493 if (file_entry.included_p == 1)
20494 {
20495 gdb::unique_xmalloc_ptr<char> name_holder;
20496 const char *include_name =
20497 psymtab_include_file_name (lh, file_entry, pst,
20498 comp_dir, &name_holder);
20499 if (include_name != NULL)
20500 dwarf2_create_include_psymtab (include_name, pst, objfile);
20501 }
20502 }
20503 else
20504 {
20505 /* Make sure a symtab is created for every file, even files
20506 which contain only variables (i.e. no code with associated
20507 line numbers). */
20508 buildsym_compunit *builder = cu->get_builder ();
20509 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20510
20511 for (auto &fe : lh->file_names ())
20512 {
20513 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20514 if (builder->get_current_subfile ()->symtab == NULL)
20515 {
20516 builder->get_current_subfile ()->symtab
20517 = allocate_symtab (cust,
20518 builder->get_current_subfile ()->name);
20519 }
20520 fe.symtab = builder->get_current_subfile ()->symtab;
20521 }
20522 }
20523 }
20524
20525 /* Start a subfile for DWARF. FILENAME is the name of the file and
20526 DIRNAME the name of the source directory which contains FILENAME
20527 or NULL if not known.
20528 This routine tries to keep line numbers from identical absolute and
20529 relative file names in a common subfile.
20530
20531 Using the `list' example from the GDB testsuite, which resides in
20532 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20533 of /srcdir/list0.c yields the following debugging information for list0.c:
20534
20535 DW_AT_name: /srcdir/list0.c
20536 DW_AT_comp_dir: /compdir
20537 files.files[0].name: list0.h
20538 files.files[0].dir: /srcdir
20539 files.files[1].name: list0.c
20540 files.files[1].dir: /srcdir
20541
20542 The line number information for list0.c has to end up in a single
20543 subfile, so that `break /srcdir/list0.c:1' works as expected.
20544 start_subfile will ensure that this happens provided that we pass the
20545 concatenation of files.files[1].dir and files.files[1].name as the
20546 subfile's name. */
20547
20548 static void
20549 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20550 const char *dirname)
20551 {
20552 gdb::unique_xmalloc_ptr<char> copy;
20553
20554 /* In order not to lose the line information directory,
20555 we concatenate it to the filename when it makes sense.
20556 Note that the Dwarf3 standard says (speaking of filenames in line
20557 information): ``The directory index is ignored for file names
20558 that represent full path names''. Thus ignoring dirname in the
20559 `else' branch below isn't an issue. */
20560
20561 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20562 {
20563 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20564 filename = copy.get ();
20565 }
20566
20567 cu->get_builder ()->start_subfile (filename);
20568 }
20569
20570 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20571 buildsym_compunit constructor. */
20572
20573 struct compunit_symtab *
20574 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20575 CORE_ADDR low_pc)
20576 {
20577 gdb_assert (m_builder == nullptr);
20578
20579 m_builder.reset (new struct buildsym_compunit
20580 (per_cu->dwarf2_per_objfile->objfile,
20581 name, comp_dir, language, low_pc));
20582
20583 list_in_scope = get_builder ()->get_file_symbols ();
20584
20585 get_builder ()->record_debugformat ("DWARF 2");
20586 get_builder ()->record_producer (producer);
20587
20588 processing_has_namespace_info = false;
20589
20590 return get_builder ()->get_compunit_symtab ();
20591 }
20592
20593 static void
20594 var_decode_location (struct attribute *attr, struct symbol *sym,
20595 struct dwarf2_cu *cu)
20596 {
20597 struct objfile *objfile = cu->per_objfile->objfile;
20598 struct comp_unit_head *cu_header = &cu->header;
20599
20600 /* NOTE drow/2003-01-30: There used to be a comment and some special
20601 code here to turn a symbol with DW_AT_external and a
20602 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20603 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20604 with some versions of binutils) where shared libraries could have
20605 relocations against symbols in their debug information - the
20606 minimal symbol would have the right address, but the debug info
20607 would not. It's no longer necessary, because we will explicitly
20608 apply relocations when we read in the debug information now. */
20609
20610 /* A DW_AT_location attribute with no contents indicates that a
20611 variable has been optimized away. */
20612 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20613 {
20614 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20615 return;
20616 }
20617
20618 /* Handle one degenerate form of location expression specially, to
20619 preserve GDB's previous behavior when section offsets are
20620 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20621 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20622
20623 if (attr->form_is_block ()
20624 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20625 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20626 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20627 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20628 && (DW_BLOCK (attr)->size
20629 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20630 {
20631 unsigned int dummy;
20632
20633 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20634 SET_SYMBOL_VALUE_ADDRESS
20635 (sym, cu->header.read_address (objfile->obfd,
20636 DW_BLOCK (attr)->data + 1,
20637 &dummy));
20638 else
20639 SET_SYMBOL_VALUE_ADDRESS
20640 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20641 &dummy));
20642 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20643 fixup_symbol_section (sym, objfile);
20644 SET_SYMBOL_VALUE_ADDRESS
20645 (sym,
20646 SYMBOL_VALUE_ADDRESS (sym)
20647 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20648 return;
20649 }
20650
20651 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20652 expression evaluator, and use LOC_COMPUTED only when necessary
20653 (i.e. when the value of a register or memory location is
20654 referenced, or a thread-local block, etc.). Then again, it might
20655 not be worthwhile. I'm assuming that it isn't unless performance
20656 or memory numbers show me otherwise. */
20657
20658 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20659
20660 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20661 cu->has_loclist = true;
20662 }
20663
20664 /* Given a pointer to a DWARF information entry, figure out if we need
20665 to make a symbol table entry for it, and if so, create a new entry
20666 and return a pointer to it.
20667 If TYPE is NULL, determine symbol type from the die, otherwise
20668 used the passed type.
20669 If SPACE is not NULL, use it to hold the new symbol. If it is
20670 NULL, allocate a new symbol on the objfile's obstack. */
20671
20672 static struct symbol *
20673 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20674 struct symbol *space)
20675 {
20676 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20677 struct objfile *objfile = dwarf2_per_objfile->objfile;
20678 struct gdbarch *gdbarch = objfile->arch ();
20679 struct symbol *sym = NULL;
20680 const char *name;
20681 struct attribute *attr = NULL;
20682 struct attribute *attr2 = NULL;
20683 CORE_ADDR baseaddr;
20684 struct pending **list_to_add = NULL;
20685
20686 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20687
20688 baseaddr = objfile->text_section_offset ();
20689
20690 name = dwarf2_name (die, cu);
20691 if (name)
20692 {
20693 int suppress_add = 0;
20694
20695 if (space)
20696 sym = space;
20697 else
20698 sym = new (&objfile->objfile_obstack) symbol;
20699 OBJSTAT (objfile, n_syms++);
20700
20701 /* Cache this symbol's name and the name's demangled form (if any). */
20702 sym->set_language (cu->language, &objfile->objfile_obstack);
20703 /* Fortran does not have mangling standard and the mangling does differ
20704 between gfortran, iFort etc. */
20705 const char *physname
20706 = (cu->language == language_fortran
20707 ? dwarf2_full_name (name, die, cu)
20708 : dwarf2_physname (name, die, cu));
20709 const char *linkagename = dw2_linkage_name (die, cu);
20710
20711 if (linkagename == nullptr || cu->language == language_ada)
20712 sym->set_linkage_name (physname);
20713 else
20714 {
20715 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20716 sym->set_linkage_name (linkagename);
20717 }
20718
20719 /* Default assumptions.
20720 Use the passed type or decode it from the die. */
20721 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20722 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20723 if (type != NULL)
20724 SYMBOL_TYPE (sym) = type;
20725 else
20726 SYMBOL_TYPE (sym) = die_type (die, cu);
20727 attr = dwarf2_attr (die,
20728 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20729 cu);
20730 if (attr != nullptr)
20731 {
20732 SYMBOL_LINE (sym) = DW_UNSND (attr);
20733 }
20734
20735 attr = dwarf2_attr (die,
20736 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20737 cu);
20738 if (attr != nullptr)
20739 {
20740 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20741 struct file_entry *fe;
20742
20743 if (cu->line_header != NULL)
20744 fe = cu->line_header->file_name_at (file_index);
20745 else
20746 fe = NULL;
20747
20748 if (fe == NULL)
20749 complaint (_("file index out of range"));
20750 else
20751 symbol_set_symtab (sym, fe->symtab);
20752 }
20753
20754 switch (die->tag)
20755 {
20756 case DW_TAG_label:
20757 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20758 if (attr != nullptr)
20759 {
20760 CORE_ADDR addr;
20761
20762 addr = attr->value_as_address ();
20763 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20764 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20765 }
20766 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20767 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20768 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20769 add_symbol_to_list (sym, cu->list_in_scope);
20770 break;
20771 case DW_TAG_subprogram:
20772 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20773 finish_block. */
20774 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20775 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20776 if ((attr2 && (DW_UNSND (attr2) != 0))
20777 || cu->language == language_ada
20778 || cu->language == language_fortran)
20779 {
20780 /* Subprograms marked external are stored as a global symbol.
20781 Ada and Fortran subprograms, whether marked external or
20782 not, are always stored as a global symbol, because we want
20783 to be able to access them globally. For instance, we want
20784 to be able to break on a nested subprogram without having
20785 to specify the context. */
20786 list_to_add = cu->get_builder ()->get_global_symbols ();
20787 }
20788 else
20789 {
20790 list_to_add = cu->list_in_scope;
20791 }
20792 break;
20793 case DW_TAG_inlined_subroutine:
20794 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20795 finish_block. */
20796 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20797 SYMBOL_INLINED (sym) = 1;
20798 list_to_add = cu->list_in_scope;
20799 break;
20800 case DW_TAG_template_value_param:
20801 suppress_add = 1;
20802 /* Fall through. */
20803 case DW_TAG_constant:
20804 case DW_TAG_variable:
20805 case DW_TAG_member:
20806 /* Compilation with minimal debug info may result in
20807 variables with missing type entries. Change the
20808 misleading `void' type to something sensible. */
20809 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
20810 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20811
20812 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20813 /* In the case of DW_TAG_member, we should only be called for
20814 static const members. */
20815 if (die->tag == DW_TAG_member)
20816 {
20817 /* dwarf2_add_field uses die_is_declaration,
20818 so we do the same. */
20819 gdb_assert (die_is_declaration (die, cu));
20820 gdb_assert (attr);
20821 }
20822 if (attr != nullptr)
20823 {
20824 dwarf2_const_value (attr, sym, cu);
20825 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20826 if (!suppress_add)
20827 {
20828 if (attr2 && (DW_UNSND (attr2) != 0))
20829 list_to_add = cu->get_builder ()->get_global_symbols ();
20830 else
20831 list_to_add = cu->list_in_scope;
20832 }
20833 break;
20834 }
20835 attr = dwarf2_attr (die, DW_AT_location, cu);
20836 if (attr != nullptr)
20837 {
20838 var_decode_location (attr, sym, cu);
20839 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20840
20841 /* Fortran explicitly imports any global symbols to the local
20842 scope by DW_TAG_common_block. */
20843 if (cu->language == language_fortran && die->parent
20844 && die->parent->tag == DW_TAG_common_block)
20845 attr2 = NULL;
20846
20847 if (SYMBOL_CLASS (sym) == LOC_STATIC
20848 && SYMBOL_VALUE_ADDRESS (sym) == 0
20849 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
20850 {
20851 /* When a static variable is eliminated by the linker,
20852 the corresponding debug information is not stripped
20853 out, but the variable address is set to null;
20854 do not add such variables into symbol table. */
20855 }
20856 else if (attr2 && (DW_UNSND (attr2) != 0))
20857 {
20858 if (SYMBOL_CLASS (sym) == LOC_STATIC
20859 && (objfile->flags & OBJF_MAINLINE) == 0
20860 && dwarf2_per_objfile->per_bfd->can_copy)
20861 {
20862 /* A global static variable might be subject to
20863 copy relocation. We first check for a local
20864 minsym, though, because maybe the symbol was
20865 marked hidden, in which case this would not
20866 apply. */
20867 bound_minimal_symbol found
20868 = (lookup_minimal_symbol_linkage
20869 (sym->linkage_name (), objfile));
20870 if (found.minsym != nullptr)
20871 sym->maybe_copied = 1;
20872 }
20873
20874 /* A variable with DW_AT_external is never static,
20875 but it may be block-scoped. */
20876 list_to_add
20877 = ((cu->list_in_scope
20878 == cu->get_builder ()->get_file_symbols ())
20879 ? cu->get_builder ()->get_global_symbols ()
20880 : cu->list_in_scope);
20881 }
20882 else
20883 list_to_add = cu->list_in_scope;
20884 }
20885 else
20886 {
20887 /* We do not know the address of this symbol.
20888 If it is an external symbol and we have type information
20889 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20890 The address of the variable will then be determined from
20891 the minimal symbol table whenever the variable is
20892 referenced. */
20893 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20894
20895 /* Fortran explicitly imports any global symbols to the local
20896 scope by DW_TAG_common_block. */
20897 if (cu->language == language_fortran && die->parent
20898 && die->parent->tag == DW_TAG_common_block)
20899 {
20900 /* SYMBOL_CLASS doesn't matter here because
20901 read_common_block is going to reset it. */
20902 if (!suppress_add)
20903 list_to_add = cu->list_in_scope;
20904 }
20905 else if (attr2 && (DW_UNSND (attr2) != 0)
20906 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20907 {
20908 /* A variable with DW_AT_external is never static, but it
20909 may be block-scoped. */
20910 list_to_add
20911 = ((cu->list_in_scope
20912 == cu->get_builder ()->get_file_symbols ())
20913 ? cu->get_builder ()->get_global_symbols ()
20914 : cu->list_in_scope);
20915
20916 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20917 }
20918 else if (!die_is_declaration (die, cu))
20919 {
20920 /* Use the default LOC_OPTIMIZED_OUT class. */
20921 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20922 if (!suppress_add)
20923 list_to_add = cu->list_in_scope;
20924 }
20925 }
20926 break;
20927 case DW_TAG_formal_parameter:
20928 {
20929 /* If we are inside a function, mark this as an argument. If
20930 not, we might be looking at an argument to an inlined function
20931 when we do not have enough information to show inlined frames;
20932 pretend it's a local variable in that case so that the user can
20933 still see it. */
20934 struct context_stack *curr
20935 = cu->get_builder ()->get_current_context_stack ();
20936 if (curr != nullptr && curr->name != nullptr)
20937 SYMBOL_IS_ARGUMENT (sym) = 1;
20938 attr = dwarf2_attr (die, DW_AT_location, cu);
20939 if (attr != nullptr)
20940 {
20941 var_decode_location (attr, sym, cu);
20942 }
20943 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20944 if (attr != nullptr)
20945 {
20946 dwarf2_const_value (attr, sym, cu);
20947 }
20948
20949 list_to_add = cu->list_in_scope;
20950 }
20951 break;
20952 case DW_TAG_unspecified_parameters:
20953 /* From varargs functions; gdb doesn't seem to have any
20954 interest in this information, so just ignore it for now.
20955 (FIXME?) */
20956 break;
20957 case DW_TAG_template_type_param:
20958 suppress_add = 1;
20959 /* Fall through. */
20960 case DW_TAG_class_type:
20961 case DW_TAG_interface_type:
20962 case DW_TAG_structure_type:
20963 case DW_TAG_union_type:
20964 case DW_TAG_set_type:
20965 case DW_TAG_enumeration_type:
20966 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20967 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20968
20969 {
20970 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20971 really ever be static objects: otherwise, if you try
20972 to, say, break of a class's method and you're in a file
20973 which doesn't mention that class, it won't work unless
20974 the check for all static symbols in lookup_symbol_aux
20975 saves you. See the OtherFileClass tests in
20976 gdb.c++/namespace.exp. */
20977
20978 if (!suppress_add)
20979 {
20980 buildsym_compunit *builder = cu->get_builder ();
20981 list_to_add
20982 = (cu->list_in_scope == builder->get_file_symbols ()
20983 && cu->language == language_cplus
20984 ? builder->get_global_symbols ()
20985 : cu->list_in_scope);
20986
20987 /* The semantics of C++ state that "struct foo {
20988 ... }" also defines a typedef for "foo". */
20989 if (cu->language == language_cplus
20990 || cu->language == language_ada
20991 || cu->language == language_d
20992 || cu->language == language_rust)
20993 {
20994 /* The symbol's name is already allocated along
20995 with this objfile, so we don't need to
20996 duplicate it for the type. */
20997 if (SYMBOL_TYPE (sym)->name () == 0)
20998 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
20999 }
21000 }
21001 }
21002 break;
21003 case DW_TAG_typedef:
21004 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21005 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21006 list_to_add = cu->list_in_scope;
21007 break;
21008 case DW_TAG_base_type:
21009 case DW_TAG_subrange_type:
21010 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21011 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21012 list_to_add = cu->list_in_scope;
21013 break;
21014 case DW_TAG_enumerator:
21015 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21016 if (attr != nullptr)
21017 {
21018 dwarf2_const_value (attr, sym, cu);
21019 }
21020 {
21021 /* NOTE: carlton/2003-11-10: See comment above in the
21022 DW_TAG_class_type, etc. block. */
21023
21024 list_to_add
21025 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21026 && cu->language == language_cplus
21027 ? cu->get_builder ()->get_global_symbols ()
21028 : cu->list_in_scope);
21029 }
21030 break;
21031 case DW_TAG_imported_declaration:
21032 case DW_TAG_namespace:
21033 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21034 list_to_add = cu->get_builder ()->get_global_symbols ();
21035 break;
21036 case DW_TAG_module:
21037 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21038 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21039 list_to_add = cu->get_builder ()->get_global_symbols ();
21040 break;
21041 case DW_TAG_common_block:
21042 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21043 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21044 add_symbol_to_list (sym, cu->list_in_scope);
21045 break;
21046 default:
21047 /* Not a tag we recognize. Hopefully we aren't processing
21048 trash data, but since we must specifically ignore things
21049 we don't recognize, there is nothing else we should do at
21050 this point. */
21051 complaint (_("unsupported tag: '%s'"),
21052 dwarf_tag_name (die->tag));
21053 break;
21054 }
21055
21056 if (suppress_add)
21057 {
21058 sym->hash_next = objfile->template_symbols;
21059 objfile->template_symbols = sym;
21060 list_to_add = NULL;
21061 }
21062
21063 if (list_to_add != NULL)
21064 add_symbol_to_list (sym, list_to_add);
21065
21066 /* For the benefit of old versions of GCC, check for anonymous
21067 namespaces based on the demangled name. */
21068 if (!cu->processing_has_namespace_info
21069 && cu->language == language_cplus)
21070 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21071 }
21072 return (sym);
21073 }
21074
21075 /* Given an attr with a DW_FORM_dataN value in host byte order,
21076 zero-extend it as appropriate for the symbol's type. The DWARF
21077 standard (v4) is not entirely clear about the meaning of using
21078 DW_FORM_dataN for a constant with a signed type, where the type is
21079 wider than the data. The conclusion of a discussion on the DWARF
21080 list was that this is unspecified. We choose to always zero-extend
21081 because that is the interpretation long in use by GCC. */
21082
21083 static gdb_byte *
21084 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21085 struct dwarf2_cu *cu, LONGEST *value, int bits)
21086 {
21087 struct objfile *objfile = cu->per_objfile->objfile;
21088 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21089 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21090 LONGEST l = DW_UNSND (attr);
21091
21092 if (bits < sizeof (*value) * 8)
21093 {
21094 l &= ((LONGEST) 1 << bits) - 1;
21095 *value = l;
21096 }
21097 else if (bits == sizeof (*value) * 8)
21098 *value = l;
21099 else
21100 {
21101 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21102 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21103 return bytes;
21104 }
21105
21106 return NULL;
21107 }
21108
21109 /* Read a constant value from an attribute. Either set *VALUE, or if
21110 the value does not fit in *VALUE, set *BYTES - either already
21111 allocated on the objfile obstack, or newly allocated on OBSTACK,
21112 or, set *BATON, if we translated the constant to a location
21113 expression. */
21114
21115 static void
21116 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21117 const char *name, struct obstack *obstack,
21118 struct dwarf2_cu *cu,
21119 LONGEST *value, const gdb_byte **bytes,
21120 struct dwarf2_locexpr_baton **baton)
21121 {
21122 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21123 struct objfile *objfile = per_objfile->objfile;
21124 struct comp_unit_head *cu_header = &cu->header;
21125 struct dwarf_block *blk;
21126 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21127 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21128
21129 *value = 0;
21130 *bytes = NULL;
21131 *baton = NULL;
21132
21133 switch (attr->form)
21134 {
21135 case DW_FORM_addr:
21136 case DW_FORM_addrx:
21137 case DW_FORM_GNU_addr_index:
21138 {
21139 gdb_byte *data;
21140
21141 if (TYPE_LENGTH (type) != cu_header->addr_size)
21142 dwarf2_const_value_length_mismatch_complaint (name,
21143 cu_header->addr_size,
21144 TYPE_LENGTH (type));
21145 /* Symbols of this form are reasonably rare, so we just
21146 piggyback on the existing location code rather than writing
21147 a new implementation of symbol_computed_ops. */
21148 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21149 (*baton)->per_objfile = per_objfile;
21150 (*baton)->per_cu = cu->per_cu;
21151 gdb_assert ((*baton)->per_cu);
21152
21153 (*baton)->size = 2 + cu_header->addr_size;
21154 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21155 (*baton)->data = data;
21156
21157 data[0] = DW_OP_addr;
21158 store_unsigned_integer (&data[1], cu_header->addr_size,
21159 byte_order, DW_ADDR (attr));
21160 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21161 }
21162 break;
21163 case DW_FORM_string:
21164 case DW_FORM_strp:
21165 case DW_FORM_strx:
21166 case DW_FORM_GNU_str_index:
21167 case DW_FORM_GNU_strp_alt:
21168 /* DW_STRING is already allocated on the objfile obstack, point
21169 directly to it. */
21170 *bytes = (const gdb_byte *) DW_STRING (attr);
21171 break;
21172 case DW_FORM_block1:
21173 case DW_FORM_block2:
21174 case DW_FORM_block4:
21175 case DW_FORM_block:
21176 case DW_FORM_exprloc:
21177 case DW_FORM_data16:
21178 blk = DW_BLOCK (attr);
21179 if (TYPE_LENGTH (type) != blk->size)
21180 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21181 TYPE_LENGTH (type));
21182 *bytes = blk->data;
21183 break;
21184
21185 /* The DW_AT_const_value attributes are supposed to carry the
21186 symbol's value "represented as it would be on the target
21187 architecture." By the time we get here, it's already been
21188 converted to host endianness, so we just need to sign- or
21189 zero-extend it as appropriate. */
21190 case DW_FORM_data1:
21191 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21192 break;
21193 case DW_FORM_data2:
21194 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21195 break;
21196 case DW_FORM_data4:
21197 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21198 break;
21199 case DW_FORM_data8:
21200 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21201 break;
21202
21203 case DW_FORM_sdata:
21204 case DW_FORM_implicit_const:
21205 *value = DW_SND (attr);
21206 break;
21207
21208 case DW_FORM_udata:
21209 *value = DW_UNSND (attr);
21210 break;
21211
21212 default:
21213 complaint (_("unsupported const value attribute form: '%s'"),
21214 dwarf_form_name (attr->form));
21215 *value = 0;
21216 break;
21217 }
21218 }
21219
21220
21221 /* Copy constant value from an attribute to a symbol. */
21222
21223 static void
21224 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21225 struct dwarf2_cu *cu)
21226 {
21227 struct objfile *objfile = cu->per_objfile->objfile;
21228 LONGEST value;
21229 const gdb_byte *bytes;
21230 struct dwarf2_locexpr_baton *baton;
21231
21232 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21233 sym->print_name (),
21234 &objfile->objfile_obstack, cu,
21235 &value, &bytes, &baton);
21236
21237 if (baton != NULL)
21238 {
21239 SYMBOL_LOCATION_BATON (sym) = baton;
21240 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21241 }
21242 else if (bytes != NULL)
21243 {
21244 SYMBOL_VALUE_BYTES (sym) = bytes;
21245 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21246 }
21247 else
21248 {
21249 SYMBOL_VALUE (sym) = value;
21250 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21251 }
21252 }
21253
21254 /* Return the type of the die in question using its DW_AT_type attribute. */
21255
21256 static struct type *
21257 die_type (struct die_info *die, struct dwarf2_cu *cu)
21258 {
21259 struct attribute *type_attr;
21260
21261 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21262 if (!type_attr)
21263 {
21264 struct objfile *objfile = cu->per_objfile->objfile;
21265 /* A missing DW_AT_type represents a void type. */
21266 return objfile_type (objfile)->builtin_void;
21267 }
21268
21269 return lookup_die_type (die, type_attr, cu);
21270 }
21271
21272 /* True iff CU's producer generates GNAT Ada auxiliary information
21273 that allows to find parallel types through that information instead
21274 of having to do expensive parallel lookups by type name. */
21275
21276 static int
21277 need_gnat_info (struct dwarf2_cu *cu)
21278 {
21279 /* Assume that the Ada compiler was GNAT, which always produces
21280 the auxiliary information. */
21281 return (cu->language == language_ada);
21282 }
21283
21284 /* Return the auxiliary type of the die in question using its
21285 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21286 attribute is not present. */
21287
21288 static struct type *
21289 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21290 {
21291 struct attribute *type_attr;
21292
21293 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21294 if (!type_attr)
21295 return NULL;
21296
21297 return lookup_die_type (die, type_attr, cu);
21298 }
21299
21300 /* If DIE has a descriptive_type attribute, then set the TYPE's
21301 descriptive type accordingly. */
21302
21303 static void
21304 set_descriptive_type (struct type *type, struct die_info *die,
21305 struct dwarf2_cu *cu)
21306 {
21307 struct type *descriptive_type = die_descriptive_type (die, cu);
21308
21309 if (descriptive_type)
21310 {
21311 ALLOCATE_GNAT_AUX_TYPE (type);
21312 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21313 }
21314 }
21315
21316 /* Return the containing type of the die in question using its
21317 DW_AT_containing_type attribute. */
21318
21319 static struct type *
21320 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21321 {
21322 struct attribute *type_attr;
21323 struct objfile *objfile = cu->per_objfile->objfile;
21324
21325 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21326 if (!type_attr)
21327 error (_("Dwarf Error: Problem turning containing type into gdb type "
21328 "[in module %s]"), objfile_name (objfile));
21329
21330 return lookup_die_type (die, type_attr, cu);
21331 }
21332
21333 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21334
21335 static struct type *
21336 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21337 {
21338 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21339 struct objfile *objfile = dwarf2_per_objfile->objfile;
21340 char *saved;
21341
21342 std::string message
21343 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21344 objfile_name (objfile),
21345 sect_offset_str (cu->header.sect_off),
21346 sect_offset_str (die->sect_off));
21347 saved = obstack_strdup (&objfile->objfile_obstack, message);
21348
21349 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21350 }
21351
21352 /* Look up the type of DIE in CU using its type attribute ATTR.
21353 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21354 DW_AT_containing_type.
21355 If there is no type substitute an error marker. */
21356
21357 static struct type *
21358 lookup_die_type (struct die_info *die, const struct attribute *attr,
21359 struct dwarf2_cu *cu)
21360 {
21361 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21362 struct objfile *objfile = dwarf2_per_objfile->objfile;
21363 struct type *this_type;
21364
21365 gdb_assert (attr->name == DW_AT_type
21366 || attr->name == DW_AT_GNAT_descriptive_type
21367 || attr->name == DW_AT_containing_type);
21368
21369 /* First see if we have it cached. */
21370
21371 if (attr->form == DW_FORM_GNU_ref_alt)
21372 {
21373 struct dwarf2_per_cu_data *per_cu;
21374 sect_offset sect_off = attr->get_ref_die_offset ();
21375
21376 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21377 dwarf2_per_objfile);
21378 this_type = get_die_type_at_offset (sect_off, per_cu);
21379 }
21380 else if (attr->form_is_ref ())
21381 {
21382 sect_offset sect_off = attr->get_ref_die_offset ();
21383
21384 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21385 }
21386 else if (attr->form == DW_FORM_ref_sig8)
21387 {
21388 ULONGEST signature = DW_SIGNATURE (attr);
21389
21390 return get_signatured_type (die, signature, cu);
21391 }
21392 else
21393 {
21394 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21395 " at %s [in module %s]"),
21396 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21397 objfile_name (objfile));
21398 return build_error_marker_type (cu, die);
21399 }
21400
21401 /* If not cached we need to read it in. */
21402
21403 if (this_type == NULL)
21404 {
21405 struct die_info *type_die = NULL;
21406 struct dwarf2_cu *type_cu = cu;
21407
21408 if (attr->form_is_ref ())
21409 type_die = follow_die_ref (die, attr, &type_cu);
21410 if (type_die == NULL)
21411 return build_error_marker_type (cu, die);
21412 /* If we find the type now, it's probably because the type came
21413 from an inter-CU reference and the type's CU got expanded before
21414 ours. */
21415 this_type = read_type_die (type_die, type_cu);
21416 }
21417
21418 /* If we still don't have a type use an error marker. */
21419
21420 if (this_type == NULL)
21421 return build_error_marker_type (cu, die);
21422
21423 return this_type;
21424 }
21425
21426 /* Return the type in DIE, CU.
21427 Returns NULL for invalid types.
21428
21429 This first does a lookup in die_type_hash,
21430 and only reads the die in if necessary.
21431
21432 NOTE: This can be called when reading in partial or full symbols. */
21433
21434 static struct type *
21435 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21436 {
21437 struct type *this_type;
21438
21439 this_type = get_die_type (die, cu);
21440 if (this_type)
21441 return this_type;
21442
21443 return read_type_die_1 (die, cu);
21444 }
21445
21446 /* Read the type in DIE, CU.
21447 Returns NULL for invalid types. */
21448
21449 static struct type *
21450 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21451 {
21452 struct type *this_type = NULL;
21453
21454 switch (die->tag)
21455 {
21456 case DW_TAG_class_type:
21457 case DW_TAG_interface_type:
21458 case DW_TAG_structure_type:
21459 case DW_TAG_union_type:
21460 this_type = read_structure_type (die, cu);
21461 break;
21462 case DW_TAG_enumeration_type:
21463 this_type = read_enumeration_type (die, cu);
21464 break;
21465 case DW_TAG_subprogram:
21466 case DW_TAG_subroutine_type:
21467 case DW_TAG_inlined_subroutine:
21468 this_type = read_subroutine_type (die, cu);
21469 break;
21470 case DW_TAG_array_type:
21471 this_type = read_array_type (die, cu);
21472 break;
21473 case DW_TAG_set_type:
21474 this_type = read_set_type (die, cu);
21475 break;
21476 case DW_TAG_pointer_type:
21477 this_type = read_tag_pointer_type (die, cu);
21478 break;
21479 case DW_TAG_ptr_to_member_type:
21480 this_type = read_tag_ptr_to_member_type (die, cu);
21481 break;
21482 case DW_TAG_reference_type:
21483 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21484 break;
21485 case DW_TAG_rvalue_reference_type:
21486 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21487 break;
21488 case DW_TAG_const_type:
21489 this_type = read_tag_const_type (die, cu);
21490 break;
21491 case DW_TAG_volatile_type:
21492 this_type = read_tag_volatile_type (die, cu);
21493 break;
21494 case DW_TAG_restrict_type:
21495 this_type = read_tag_restrict_type (die, cu);
21496 break;
21497 case DW_TAG_string_type:
21498 this_type = read_tag_string_type (die, cu);
21499 break;
21500 case DW_TAG_typedef:
21501 this_type = read_typedef (die, cu);
21502 break;
21503 case DW_TAG_subrange_type:
21504 this_type = read_subrange_type (die, cu);
21505 break;
21506 case DW_TAG_base_type:
21507 this_type = read_base_type (die, cu);
21508 break;
21509 case DW_TAG_unspecified_type:
21510 this_type = read_unspecified_type (die, cu);
21511 break;
21512 case DW_TAG_namespace:
21513 this_type = read_namespace_type (die, cu);
21514 break;
21515 case DW_TAG_module:
21516 this_type = read_module_type (die, cu);
21517 break;
21518 case DW_TAG_atomic_type:
21519 this_type = read_tag_atomic_type (die, cu);
21520 break;
21521 default:
21522 complaint (_("unexpected tag in read_type_die: '%s'"),
21523 dwarf_tag_name (die->tag));
21524 break;
21525 }
21526
21527 return this_type;
21528 }
21529
21530 /* See if we can figure out if the class lives in a namespace. We do
21531 this by looking for a member function; its demangled name will
21532 contain namespace info, if there is any.
21533 Return the computed name or NULL.
21534 Space for the result is allocated on the objfile's obstack.
21535 This is the full-die version of guess_partial_die_structure_name.
21536 In this case we know DIE has no useful parent. */
21537
21538 static const char *
21539 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21540 {
21541 struct die_info *spec_die;
21542 struct dwarf2_cu *spec_cu;
21543 struct die_info *child;
21544 struct objfile *objfile = cu->per_objfile->objfile;
21545
21546 spec_cu = cu;
21547 spec_die = die_specification (die, &spec_cu);
21548 if (spec_die != NULL)
21549 {
21550 die = spec_die;
21551 cu = spec_cu;
21552 }
21553
21554 for (child = die->child;
21555 child != NULL;
21556 child = child->sibling)
21557 {
21558 if (child->tag == DW_TAG_subprogram)
21559 {
21560 const char *linkage_name = dw2_linkage_name (child, cu);
21561
21562 if (linkage_name != NULL)
21563 {
21564 gdb::unique_xmalloc_ptr<char> actual_name
21565 (language_class_name_from_physname (cu->language_defn,
21566 linkage_name));
21567 const char *name = NULL;
21568
21569 if (actual_name != NULL)
21570 {
21571 const char *die_name = dwarf2_name (die, cu);
21572
21573 if (die_name != NULL
21574 && strcmp (die_name, actual_name.get ()) != 0)
21575 {
21576 /* Strip off the class name from the full name.
21577 We want the prefix. */
21578 int die_name_len = strlen (die_name);
21579 int actual_name_len = strlen (actual_name.get ());
21580 const char *ptr = actual_name.get ();
21581
21582 /* Test for '::' as a sanity check. */
21583 if (actual_name_len > die_name_len + 2
21584 && ptr[actual_name_len - die_name_len - 1] == ':')
21585 name = obstack_strndup (
21586 &objfile->per_bfd->storage_obstack,
21587 ptr, actual_name_len - die_name_len - 2);
21588 }
21589 }
21590 return name;
21591 }
21592 }
21593 }
21594
21595 return NULL;
21596 }
21597
21598 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21599 prefix part in such case. See
21600 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21601
21602 static const char *
21603 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21604 {
21605 struct attribute *attr;
21606 const char *base;
21607
21608 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21609 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21610 return NULL;
21611
21612 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21613 return NULL;
21614
21615 attr = dw2_linkage_name_attr (die, cu);
21616 if (attr == NULL || DW_STRING (attr) == NULL)
21617 return NULL;
21618
21619 /* dwarf2_name had to be already called. */
21620 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21621
21622 /* Strip the base name, keep any leading namespaces/classes. */
21623 base = strrchr (DW_STRING (attr), ':');
21624 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21625 return "";
21626
21627 struct objfile *objfile = cu->per_objfile->objfile;
21628 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21629 DW_STRING (attr),
21630 &base[-1] - DW_STRING (attr));
21631 }
21632
21633 /* Return the name of the namespace/class that DIE is defined within,
21634 or "" if we can't tell. The caller should not xfree the result.
21635
21636 For example, if we're within the method foo() in the following
21637 code:
21638
21639 namespace N {
21640 class C {
21641 void foo () {
21642 }
21643 };
21644 }
21645
21646 then determine_prefix on foo's die will return "N::C". */
21647
21648 static const char *
21649 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21650 {
21651 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21652 struct die_info *parent, *spec_die;
21653 struct dwarf2_cu *spec_cu;
21654 struct type *parent_type;
21655 const char *retval;
21656
21657 if (cu->language != language_cplus
21658 && cu->language != language_fortran && cu->language != language_d
21659 && cu->language != language_rust)
21660 return "";
21661
21662 retval = anonymous_struct_prefix (die, cu);
21663 if (retval)
21664 return retval;
21665
21666 /* We have to be careful in the presence of DW_AT_specification.
21667 For example, with GCC 3.4, given the code
21668
21669 namespace N {
21670 void foo() {
21671 // Definition of N::foo.
21672 }
21673 }
21674
21675 then we'll have a tree of DIEs like this:
21676
21677 1: DW_TAG_compile_unit
21678 2: DW_TAG_namespace // N
21679 3: DW_TAG_subprogram // declaration of N::foo
21680 4: DW_TAG_subprogram // definition of N::foo
21681 DW_AT_specification // refers to die #3
21682
21683 Thus, when processing die #4, we have to pretend that we're in
21684 the context of its DW_AT_specification, namely the contex of die
21685 #3. */
21686 spec_cu = cu;
21687 spec_die = die_specification (die, &spec_cu);
21688 if (spec_die == NULL)
21689 parent = die->parent;
21690 else
21691 {
21692 parent = spec_die->parent;
21693 cu = spec_cu;
21694 }
21695
21696 if (parent == NULL)
21697 return "";
21698 else if (parent->building_fullname)
21699 {
21700 const char *name;
21701 const char *parent_name;
21702
21703 /* It has been seen on RealView 2.2 built binaries,
21704 DW_TAG_template_type_param types actually _defined_ as
21705 children of the parent class:
21706
21707 enum E {};
21708 template class <class Enum> Class{};
21709 Class<enum E> class_e;
21710
21711 1: DW_TAG_class_type (Class)
21712 2: DW_TAG_enumeration_type (E)
21713 3: DW_TAG_enumerator (enum1:0)
21714 3: DW_TAG_enumerator (enum2:1)
21715 ...
21716 2: DW_TAG_template_type_param
21717 DW_AT_type DW_FORM_ref_udata (E)
21718
21719 Besides being broken debug info, it can put GDB into an
21720 infinite loop. Consider:
21721
21722 When we're building the full name for Class<E>, we'll start
21723 at Class, and go look over its template type parameters,
21724 finding E. We'll then try to build the full name of E, and
21725 reach here. We're now trying to build the full name of E,
21726 and look over the parent DIE for containing scope. In the
21727 broken case, if we followed the parent DIE of E, we'd again
21728 find Class, and once again go look at its template type
21729 arguments, etc., etc. Simply don't consider such parent die
21730 as source-level parent of this die (it can't be, the language
21731 doesn't allow it), and break the loop here. */
21732 name = dwarf2_name (die, cu);
21733 parent_name = dwarf2_name (parent, cu);
21734 complaint (_("template param type '%s' defined within parent '%s'"),
21735 name ? name : "<unknown>",
21736 parent_name ? parent_name : "<unknown>");
21737 return "";
21738 }
21739 else
21740 switch (parent->tag)
21741 {
21742 case DW_TAG_namespace:
21743 parent_type = read_type_die (parent, cu);
21744 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21745 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21746 Work around this problem here. */
21747 if (cu->language == language_cplus
21748 && strcmp (parent_type->name (), "::") == 0)
21749 return "";
21750 /* We give a name to even anonymous namespaces. */
21751 return parent_type->name ();
21752 case DW_TAG_class_type:
21753 case DW_TAG_interface_type:
21754 case DW_TAG_structure_type:
21755 case DW_TAG_union_type:
21756 case DW_TAG_module:
21757 parent_type = read_type_die (parent, cu);
21758 if (parent_type->name () != NULL)
21759 return parent_type->name ();
21760 else
21761 /* An anonymous structure is only allowed non-static data
21762 members; no typedefs, no member functions, et cetera.
21763 So it does not need a prefix. */
21764 return "";
21765 case DW_TAG_compile_unit:
21766 case DW_TAG_partial_unit:
21767 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21768 if (cu->language == language_cplus
21769 && !dwarf2_per_objfile->per_bfd->types.empty ()
21770 && die->child != NULL
21771 && (die->tag == DW_TAG_class_type
21772 || die->tag == DW_TAG_structure_type
21773 || die->tag == DW_TAG_union_type))
21774 {
21775 const char *name = guess_full_die_structure_name (die, cu);
21776 if (name != NULL)
21777 return name;
21778 }
21779 return "";
21780 case DW_TAG_subprogram:
21781 /* Nested subroutines in Fortran get a prefix with the name
21782 of the parent's subroutine. */
21783 if (cu->language == language_fortran)
21784 {
21785 if ((die->tag == DW_TAG_subprogram)
21786 && (dwarf2_name (parent, cu) != NULL))
21787 return dwarf2_name (parent, cu);
21788 }
21789 return determine_prefix (parent, cu);
21790 case DW_TAG_enumeration_type:
21791 parent_type = read_type_die (parent, cu);
21792 if (TYPE_DECLARED_CLASS (parent_type))
21793 {
21794 if (parent_type->name () != NULL)
21795 return parent_type->name ();
21796 return "";
21797 }
21798 /* Fall through. */
21799 default:
21800 return determine_prefix (parent, cu);
21801 }
21802 }
21803
21804 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21805 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21806 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21807 an obconcat, otherwise allocate storage for the result. The CU argument is
21808 used to determine the language and hence, the appropriate separator. */
21809
21810 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21811
21812 static char *
21813 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21814 int physname, struct dwarf2_cu *cu)
21815 {
21816 const char *lead = "";
21817 const char *sep;
21818
21819 if (suffix == NULL || suffix[0] == '\0'
21820 || prefix == NULL || prefix[0] == '\0')
21821 sep = "";
21822 else if (cu->language == language_d)
21823 {
21824 /* For D, the 'main' function could be defined in any module, but it
21825 should never be prefixed. */
21826 if (strcmp (suffix, "D main") == 0)
21827 {
21828 prefix = "";
21829 sep = "";
21830 }
21831 else
21832 sep = ".";
21833 }
21834 else if (cu->language == language_fortran && physname)
21835 {
21836 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21837 DW_AT_MIPS_linkage_name is preferred and used instead. */
21838
21839 lead = "__";
21840 sep = "_MOD_";
21841 }
21842 else
21843 sep = "::";
21844
21845 if (prefix == NULL)
21846 prefix = "";
21847 if (suffix == NULL)
21848 suffix = "";
21849
21850 if (obs == NULL)
21851 {
21852 char *retval
21853 = ((char *)
21854 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21855
21856 strcpy (retval, lead);
21857 strcat (retval, prefix);
21858 strcat (retval, sep);
21859 strcat (retval, suffix);
21860 return retval;
21861 }
21862 else
21863 {
21864 /* We have an obstack. */
21865 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21866 }
21867 }
21868
21869 /* Get name of a die, return NULL if not found. */
21870
21871 static const char *
21872 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21873 struct objfile *objfile)
21874 {
21875 if (name && cu->language == language_cplus)
21876 {
21877 gdb::unique_xmalloc_ptr<char> canon_name
21878 = cp_canonicalize_string (name);
21879
21880 if (canon_name != nullptr)
21881 name = objfile->intern (canon_name.get ());
21882 }
21883
21884 return name;
21885 }
21886
21887 /* Get name of a die, return NULL if not found.
21888 Anonymous namespaces are converted to their magic string. */
21889
21890 static const char *
21891 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21892 {
21893 struct attribute *attr;
21894 struct objfile *objfile = cu->per_objfile->objfile;
21895
21896 attr = dwarf2_attr (die, DW_AT_name, cu);
21897 if ((!attr || !DW_STRING (attr))
21898 && die->tag != DW_TAG_namespace
21899 && die->tag != DW_TAG_class_type
21900 && die->tag != DW_TAG_interface_type
21901 && die->tag != DW_TAG_structure_type
21902 && die->tag != DW_TAG_union_type)
21903 return NULL;
21904
21905 switch (die->tag)
21906 {
21907 case DW_TAG_compile_unit:
21908 case DW_TAG_partial_unit:
21909 /* Compilation units have a DW_AT_name that is a filename, not
21910 a source language identifier. */
21911 case DW_TAG_enumeration_type:
21912 case DW_TAG_enumerator:
21913 /* These tags always have simple identifiers already; no need
21914 to canonicalize them. */
21915 return DW_STRING (attr);
21916
21917 case DW_TAG_namespace:
21918 if (attr != NULL && DW_STRING (attr) != NULL)
21919 return DW_STRING (attr);
21920 return CP_ANONYMOUS_NAMESPACE_STR;
21921
21922 case DW_TAG_class_type:
21923 case DW_TAG_interface_type:
21924 case DW_TAG_structure_type:
21925 case DW_TAG_union_type:
21926 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21927 structures or unions. These were of the form "._%d" in GCC 4.1,
21928 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21929 and GCC 4.4. We work around this problem by ignoring these. */
21930 if (attr && DW_STRING (attr)
21931 && (startswith (DW_STRING (attr), "._")
21932 || startswith (DW_STRING (attr), "<anonymous")))
21933 return NULL;
21934
21935 /* GCC might emit a nameless typedef that has a linkage name. See
21936 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21937 if (!attr || DW_STRING (attr) == NULL)
21938 {
21939 attr = dw2_linkage_name_attr (die, cu);
21940 if (attr == NULL || DW_STRING (attr) == NULL)
21941 return NULL;
21942
21943 /* Avoid demangling DW_STRING (attr) the second time on a second
21944 call for the same DIE. */
21945 if (!DW_STRING_IS_CANONICAL (attr))
21946 {
21947 gdb::unique_xmalloc_ptr<char> demangled
21948 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21949 if (demangled == nullptr)
21950 return nullptr;
21951
21952 DW_STRING (attr) = objfile->intern (demangled.get ());
21953 DW_STRING_IS_CANONICAL (attr) = 1;
21954 }
21955
21956 /* Strip any leading namespaces/classes, keep only the base name.
21957 DW_AT_name for named DIEs does not contain the prefixes. */
21958 const char *base = strrchr (DW_STRING (attr), ':');
21959 if (base && base > DW_STRING (attr) && base[-1] == ':')
21960 return &base[1];
21961 else
21962 return DW_STRING (attr);
21963 }
21964 break;
21965
21966 default:
21967 break;
21968 }
21969
21970 if (!DW_STRING_IS_CANONICAL (attr))
21971 {
21972 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21973 objfile);
21974 DW_STRING_IS_CANONICAL (attr) = 1;
21975 }
21976 return DW_STRING (attr);
21977 }
21978
21979 /* Return the die that this die in an extension of, or NULL if there
21980 is none. *EXT_CU is the CU containing DIE on input, and the CU
21981 containing the return value on output. */
21982
21983 static struct die_info *
21984 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21985 {
21986 struct attribute *attr;
21987
21988 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21989 if (attr == NULL)
21990 return NULL;
21991
21992 return follow_die_ref (die, attr, ext_cu);
21993 }
21994
21995 static void
21996 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21997 {
21998 unsigned int i;
21999
22000 print_spaces (indent, f);
22001 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22002 dwarf_tag_name (die->tag), die->abbrev,
22003 sect_offset_str (die->sect_off));
22004
22005 if (die->parent != NULL)
22006 {
22007 print_spaces (indent, f);
22008 fprintf_unfiltered (f, " parent at offset: %s\n",
22009 sect_offset_str (die->parent->sect_off));
22010 }
22011
22012 print_spaces (indent, f);
22013 fprintf_unfiltered (f, " has children: %s\n",
22014 dwarf_bool_name (die->child != NULL));
22015
22016 print_spaces (indent, f);
22017 fprintf_unfiltered (f, " attributes:\n");
22018
22019 for (i = 0; i < die->num_attrs; ++i)
22020 {
22021 print_spaces (indent, f);
22022 fprintf_unfiltered (f, " %s (%s) ",
22023 dwarf_attr_name (die->attrs[i].name),
22024 dwarf_form_name (die->attrs[i].form));
22025
22026 switch (die->attrs[i].form)
22027 {
22028 case DW_FORM_addr:
22029 case DW_FORM_addrx:
22030 case DW_FORM_GNU_addr_index:
22031 fprintf_unfiltered (f, "address: ");
22032 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22033 break;
22034 case DW_FORM_block2:
22035 case DW_FORM_block4:
22036 case DW_FORM_block:
22037 case DW_FORM_block1:
22038 fprintf_unfiltered (f, "block: size %s",
22039 pulongest (DW_BLOCK (&die->attrs[i])->size));
22040 break;
22041 case DW_FORM_exprloc:
22042 fprintf_unfiltered (f, "expression: size %s",
22043 pulongest (DW_BLOCK (&die->attrs[i])->size));
22044 break;
22045 case DW_FORM_data16:
22046 fprintf_unfiltered (f, "constant of 16 bytes");
22047 break;
22048 case DW_FORM_ref_addr:
22049 fprintf_unfiltered (f, "ref address: ");
22050 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22051 break;
22052 case DW_FORM_GNU_ref_alt:
22053 fprintf_unfiltered (f, "alt ref address: ");
22054 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22055 break;
22056 case DW_FORM_ref1:
22057 case DW_FORM_ref2:
22058 case DW_FORM_ref4:
22059 case DW_FORM_ref8:
22060 case DW_FORM_ref_udata:
22061 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22062 (long) (DW_UNSND (&die->attrs[i])));
22063 break;
22064 case DW_FORM_data1:
22065 case DW_FORM_data2:
22066 case DW_FORM_data4:
22067 case DW_FORM_data8:
22068 case DW_FORM_udata:
22069 case DW_FORM_sdata:
22070 fprintf_unfiltered (f, "constant: %s",
22071 pulongest (DW_UNSND (&die->attrs[i])));
22072 break;
22073 case DW_FORM_sec_offset:
22074 fprintf_unfiltered (f, "section offset: %s",
22075 pulongest (DW_UNSND (&die->attrs[i])));
22076 break;
22077 case DW_FORM_ref_sig8:
22078 fprintf_unfiltered (f, "signature: %s",
22079 hex_string (DW_SIGNATURE (&die->attrs[i])));
22080 break;
22081 case DW_FORM_string:
22082 case DW_FORM_strp:
22083 case DW_FORM_line_strp:
22084 case DW_FORM_strx:
22085 case DW_FORM_GNU_str_index:
22086 case DW_FORM_GNU_strp_alt:
22087 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22088 DW_STRING (&die->attrs[i])
22089 ? DW_STRING (&die->attrs[i]) : "",
22090 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22091 break;
22092 case DW_FORM_flag:
22093 if (DW_UNSND (&die->attrs[i]))
22094 fprintf_unfiltered (f, "flag: TRUE");
22095 else
22096 fprintf_unfiltered (f, "flag: FALSE");
22097 break;
22098 case DW_FORM_flag_present:
22099 fprintf_unfiltered (f, "flag: TRUE");
22100 break;
22101 case DW_FORM_indirect:
22102 /* The reader will have reduced the indirect form to
22103 the "base form" so this form should not occur. */
22104 fprintf_unfiltered (f,
22105 "unexpected attribute form: DW_FORM_indirect");
22106 break;
22107 case DW_FORM_implicit_const:
22108 fprintf_unfiltered (f, "constant: %s",
22109 plongest (DW_SND (&die->attrs[i])));
22110 break;
22111 default:
22112 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22113 die->attrs[i].form);
22114 break;
22115 }
22116 fprintf_unfiltered (f, "\n");
22117 }
22118 }
22119
22120 static void
22121 dump_die_for_error (struct die_info *die)
22122 {
22123 dump_die_shallow (gdb_stderr, 0, die);
22124 }
22125
22126 static void
22127 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22128 {
22129 int indent = level * 4;
22130
22131 gdb_assert (die != NULL);
22132
22133 if (level >= max_level)
22134 return;
22135
22136 dump_die_shallow (f, indent, die);
22137
22138 if (die->child != NULL)
22139 {
22140 print_spaces (indent, f);
22141 fprintf_unfiltered (f, " Children:");
22142 if (level + 1 < max_level)
22143 {
22144 fprintf_unfiltered (f, "\n");
22145 dump_die_1 (f, level + 1, max_level, die->child);
22146 }
22147 else
22148 {
22149 fprintf_unfiltered (f,
22150 " [not printed, max nesting level reached]\n");
22151 }
22152 }
22153
22154 if (die->sibling != NULL && level > 0)
22155 {
22156 dump_die_1 (f, level, max_level, die->sibling);
22157 }
22158 }
22159
22160 /* This is called from the pdie macro in gdbinit.in.
22161 It's not static so gcc will keep a copy callable from gdb. */
22162
22163 void
22164 dump_die (struct die_info *die, int max_level)
22165 {
22166 dump_die_1 (gdb_stdlog, 0, max_level, die);
22167 }
22168
22169 static void
22170 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22171 {
22172 void **slot;
22173
22174 slot = htab_find_slot_with_hash (cu->die_hash, die,
22175 to_underlying (die->sect_off),
22176 INSERT);
22177
22178 *slot = die;
22179 }
22180
22181 /* Follow reference or signature attribute ATTR of SRC_DIE.
22182 On entry *REF_CU is the CU of SRC_DIE.
22183 On exit *REF_CU is the CU of the result. */
22184
22185 static struct die_info *
22186 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22187 struct dwarf2_cu **ref_cu)
22188 {
22189 struct die_info *die;
22190
22191 if (attr->form_is_ref ())
22192 die = follow_die_ref (src_die, attr, ref_cu);
22193 else if (attr->form == DW_FORM_ref_sig8)
22194 die = follow_die_sig (src_die, attr, ref_cu);
22195 else
22196 {
22197 dump_die_for_error (src_die);
22198 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22199 objfile_name ((*ref_cu)->per_objfile->objfile));
22200 }
22201
22202 return die;
22203 }
22204
22205 /* Follow reference OFFSET.
22206 On entry *REF_CU is the CU of the source die referencing OFFSET.
22207 On exit *REF_CU is the CU of the result.
22208 Returns NULL if OFFSET is invalid. */
22209
22210 static struct die_info *
22211 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22212 struct dwarf2_cu **ref_cu)
22213 {
22214 struct die_info temp_die;
22215 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22216 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22217
22218 gdb_assert (cu->per_cu != NULL);
22219
22220 target_cu = cu;
22221
22222 if (cu->per_cu->is_debug_types)
22223 {
22224 /* .debug_types CUs cannot reference anything outside their CU.
22225 If they need to, they have to reference a signatured type via
22226 DW_FORM_ref_sig8. */
22227 if (!cu->header.offset_in_cu_p (sect_off))
22228 return NULL;
22229 }
22230 else if (offset_in_dwz != cu->per_cu->is_dwz
22231 || !cu->header.offset_in_cu_p (sect_off))
22232 {
22233 struct dwarf2_per_cu_data *per_cu;
22234
22235 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22236 dwarf2_per_objfile);
22237
22238 /* If necessary, add it to the queue and load its DIEs. */
22239 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22240 load_full_comp_unit (per_cu, dwarf2_per_objfile, false, cu->language);
22241
22242 target_cu = per_cu->cu;
22243 }
22244 else if (cu->dies == NULL)
22245 {
22246 /* We're loading full DIEs during partial symbol reading. */
22247 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
22248 load_full_comp_unit (cu->per_cu, dwarf2_per_objfile, false,
22249 language_minimal);
22250 }
22251
22252 *ref_cu = target_cu;
22253 temp_die.sect_off = sect_off;
22254
22255 if (target_cu != cu)
22256 target_cu->ancestor = cu;
22257
22258 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22259 &temp_die,
22260 to_underlying (sect_off));
22261 }
22262
22263 /* Follow reference attribute ATTR of SRC_DIE.
22264 On entry *REF_CU is the CU of SRC_DIE.
22265 On exit *REF_CU is the CU of the result. */
22266
22267 static struct die_info *
22268 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22269 struct dwarf2_cu **ref_cu)
22270 {
22271 sect_offset sect_off = attr->get_ref_die_offset ();
22272 struct dwarf2_cu *cu = *ref_cu;
22273 struct die_info *die;
22274
22275 die = follow_die_offset (sect_off,
22276 (attr->form == DW_FORM_GNU_ref_alt
22277 || cu->per_cu->is_dwz),
22278 ref_cu);
22279 if (!die)
22280 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22281 "at %s [in module %s]"),
22282 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22283 objfile_name (cu->per_objfile->objfile));
22284
22285 return die;
22286 }
22287
22288 /* See read.h. */
22289
22290 struct dwarf2_locexpr_baton
22291 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22292 dwarf2_per_cu_data *per_cu,
22293 CORE_ADDR (*get_frame_pc) (void *baton),
22294 void *baton, bool resolve_abstract_p)
22295 {
22296 struct dwarf2_cu *cu;
22297 struct die_info *die;
22298 struct attribute *attr;
22299 struct dwarf2_locexpr_baton retval;
22300 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22301 struct objfile *objfile = dwarf2_per_objfile->objfile;
22302
22303 if (per_cu->cu == NULL)
22304 load_cu (per_cu, dwarf2_per_objfile, false);
22305 cu = per_cu->cu;
22306 if (cu == NULL)
22307 {
22308 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22309 Instead just throw an error, not much else we can do. */
22310 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22311 sect_offset_str (sect_off), objfile_name (objfile));
22312 }
22313
22314 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22315 if (!die)
22316 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22317 sect_offset_str (sect_off), objfile_name (objfile));
22318
22319 attr = dwarf2_attr (die, DW_AT_location, cu);
22320 if (!attr && resolve_abstract_p
22321 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22322 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
22323 {
22324 CORE_ADDR pc = (*get_frame_pc) (baton);
22325 CORE_ADDR baseaddr = objfile->text_section_offset ();
22326 struct gdbarch *gdbarch = objfile->arch ();
22327
22328 for (const auto &cand_off
22329 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22330 {
22331 struct dwarf2_cu *cand_cu = cu;
22332 struct die_info *cand
22333 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22334 if (!cand
22335 || !cand->parent
22336 || cand->parent->tag != DW_TAG_subprogram)
22337 continue;
22338
22339 CORE_ADDR pc_low, pc_high;
22340 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22341 if (pc_low == ((CORE_ADDR) -1))
22342 continue;
22343 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22344 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22345 if (!(pc_low <= pc && pc < pc_high))
22346 continue;
22347
22348 die = cand;
22349 attr = dwarf2_attr (die, DW_AT_location, cu);
22350 break;
22351 }
22352 }
22353
22354 if (!attr)
22355 {
22356 /* DWARF: "If there is no such attribute, then there is no effect.".
22357 DATA is ignored if SIZE is 0. */
22358
22359 retval.data = NULL;
22360 retval.size = 0;
22361 }
22362 else if (attr->form_is_section_offset ())
22363 {
22364 struct dwarf2_loclist_baton loclist_baton;
22365 CORE_ADDR pc = (*get_frame_pc) (baton);
22366 size_t size;
22367
22368 fill_in_loclist_baton (cu, &loclist_baton, attr);
22369
22370 retval.data = dwarf2_find_location_expression (&loclist_baton,
22371 &size, pc);
22372 retval.size = size;
22373 }
22374 else
22375 {
22376 if (!attr->form_is_block ())
22377 error (_("Dwarf Error: DIE at %s referenced in module %s "
22378 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22379 sect_offset_str (sect_off), objfile_name (objfile));
22380
22381 retval.data = DW_BLOCK (attr)->data;
22382 retval.size = DW_BLOCK (attr)->size;
22383 }
22384 retval.per_objfile = dwarf2_per_objfile;
22385 retval.per_cu = cu->per_cu;
22386
22387 age_cached_comp_units (dwarf2_per_objfile);
22388
22389 return retval;
22390 }
22391
22392 /* See read.h. */
22393
22394 struct dwarf2_locexpr_baton
22395 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22396 dwarf2_per_cu_data *per_cu,
22397 CORE_ADDR (*get_frame_pc) (void *baton),
22398 void *baton)
22399 {
22400 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22401
22402 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22403 }
22404
22405 /* Write a constant of a given type as target-ordered bytes into
22406 OBSTACK. */
22407
22408 static const gdb_byte *
22409 write_constant_as_bytes (struct obstack *obstack,
22410 enum bfd_endian byte_order,
22411 struct type *type,
22412 ULONGEST value,
22413 LONGEST *len)
22414 {
22415 gdb_byte *result;
22416
22417 *len = TYPE_LENGTH (type);
22418 result = (gdb_byte *) obstack_alloc (obstack, *len);
22419 store_unsigned_integer (result, *len, byte_order, value);
22420
22421 return result;
22422 }
22423
22424 /* See read.h. */
22425
22426 const gdb_byte *
22427 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22428 dwarf2_per_cu_data *per_cu,
22429 obstack *obstack,
22430 LONGEST *len)
22431 {
22432 struct dwarf2_cu *cu;
22433 struct die_info *die;
22434 struct attribute *attr;
22435 const gdb_byte *result = NULL;
22436 struct type *type;
22437 LONGEST value;
22438 enum bfd_endian byte_order;
22439 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22440
22441 if (per_cu->cu == NULL)
22442 load_cu (per_cu, per_cu->dwarf2_per_objfile, false);
22443 cu = per_cu->cu;
22444 if (cu == NULL)
22445 {
22446 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22447 Instead just throw an error, not much else we can do. */
22448 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22449 sect_offset_str (sect_off), objfile_name (objfile));
22450 }
22451
22452 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22453 if (!die)
22454 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22455 sect_offset_str (sect_off), objfile_name (objfile));
22456
22457 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22458 if (attr == NULL)
22459 return NULL;
22460
22461 byte_order = (bfd_big_endian (objfile->obfd)
22462 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22463
22464 switch (attr->form)
22465 {
22466 case DW_FORM_addr:
22467 case DW_FORM_addrx:
22468 case DW_FORM_GNU_addr_index:
22469 {
22470 gdb_byte *tem;
22471
22472 *len = cu->header.addr_size;
22473 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22474 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22475 result = tem;
22476 }
22477 break;
22478 case DW_FORM_string:
22479 case DW_FORM_strp:
22480 case DW_FORM_strx:
22481 case DW_FORM_GNU_str_index:
22482 case DW_FORM_GNU_strp_alt:
22483 /* DW_STRING is already allocated on the objfile obstack, point
22484 directly to it. */
22485 result = (const gdb_byte *) DW_STRING (attr);
22486 *len = strlen (DW_STRING (attr));
22487 break;
22488 case DW_FORM_block1:
22489 case DW_FORM_block2:
22490 case DW_FORM_block4:
22491 case DW_FORM_block:
22492 case DW_FORM_exprloc:
22493 case DW_FORM_data16:
22494 result = DW_BLOCK (attr)->data;
22495 *len = DW_BLOCK (attr)->size;
22496 break;
22497
22498 /* The DW_AT_const_value attributes are supposed to carry the
22499 symbol's value "represented as it would be on the target
22500 architecture." By the time we get here, it's already been
22501 converted to host endianness, so we just need to sign- or
22502 zero-extend it as appropriate. */
22503 case DW_FORM_data1:
22504 type = die_type (die, cu);
22505 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22506 if (result == NULL)
22507 result = write_constant_as_bytes (obstack, byte_order,
22508 type, value, len);
22509 break;
22510 case DW_FORM_data2:
22511 type = die_type (die, cu);
22512 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22513 if (result == NULL)
22514 result = write_constant_as_bytes (obstack, byte_order,
22515 type, value, len);
22516 break;
22517 case DW_FORM_data4:
22518 type = die_type (die, cu);
22519 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22520 if (result == NULL)
22521 result = write_constant_as_bytes (obstack, byte_order,
22522 type, value, len);
22523 break;
22524 case DW_FORM_data8:
22525 type = die_type (die, cu);
22526 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22527 if (result == NULL)
22528 result = write_constant_as_bytes (obstack, byte_order,
22529 type, value, len);
22530 break;
22531
22532 case DW_FORM_sdata:
22533 case DW_FORM_implicit_const:
22534 type = die_type (die, cu);
22535 result = write_constant_as_bytes (obstack, byte_order,
22536 type, DW_SND (attr), len);
22537 break;
22538
22539 case DW_FORM_udata:
22540 type = die_type (die, cu);
22541 result = write_constant_as_bytes (obstack, byte_order,
22542 type, DW_UNSND (attr), len);
22543 break;
22544
22545 default:
22546 complaint (_("unsupported const value attribute form: '%s'"),
22547 dwarf_form_name (attr->form));
22548 break;
22549 }
22550
22551 return result;
22552 }
22553
22554 /* See read.h. */
22555
22556 struct type *
22557 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22558 dwarf2_per_cu_data *per_cu)
22559 {
22560 struct dwarf2_cu *cu;
22561 struct die_info *die;
22562
22563 if (per_cu->cu == NULL)
22564 load_cu (per_cu, per_cu->dwarf2_per_objfile, false);
22565 cu = per_cu->cu;
22566 if (!cu)
22567 return NULL;
22568
22569 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22570 if (!die)
22571 return NULL;
22572
22573 return die_type (die, cu);
22574 }
22575
22576 /* See read.h. */
22577
22578 struct type *
22579 dwarf2_get_die_type (cu_offset die_offset,
22580 struct dwarf2_per_cu_data *per_cu)
22581 {
22582 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22583 return get_die_type_at_offset (die_offset_sect, per_cu);
22584 }
22585
22586 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22587 On entry *REF_CU is the CU of SRC_DIE.
22588 On exit *REF_CU is the CU of the result.
22589 Returns NULL if the referenced DIE isn't found. */
22590
22591 static struct die_info *
22592 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22593 struct dwarf2_cu **ref_cu)
22594 {
22595 struct die_info temp_die;
22596 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22597 struct die_info *die;
22598
22599 /* While it might be nice to assert sig_type->type == NULL here,
22600 we can get here for DW_AT_imported_declaration where we need
22601 the DIE not the type. */
22602
22603 /* If necessary, add it to the queue and load its DIEs. */
22604
22605 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22606 read_signatured_type (sig_type, (*ref_cu)->per_objfile);
22607
22608 sig_cu = sig_type->per_cu.cu;
22609 gdb_assert (sig_cu != NULL);
22610 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22611 temp_die.sect_off = sig_type->type_offset_in_section;
22612 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22613 to_underlying (temp_die.sect_off));
22614 if (die)
22615 {
22616 struct dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
22617
22618 /* For .gdb_index version 7 keep track of included TUs.
22619 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22620 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22621 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
22622 {
22623 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22624 }
22625
22626 *ref_cu = sig_cu;
22627 if (sig_cu != cu)
22628 sig_cu->ancestor = cu;
22629
22630 return die;
22631 }
22632
22633 return NULL;
22634 }
22635
22636 /* Follow signatured type referenced by ATTR in SRC_DIE.
22637 On entry *REF_CU is the CU of SRC_DIE.
22638 On exit *REF_CU is the CU of the result.
22639 The result is the DIE of the type.
22640 If the referenced type cannot be found an error is thrown. */
22641
22642 static struct die_info *
22643 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22644 struct dwarf2_cu **ref_cu)
22645 {
22646 ULONGEST signature = DW_SIGNATURE (attr);
22647 struct signatured_type *sig_type;
22648 struct die_info *die;
22649
22650 gdb_assert (attr->form == DW_FORM_ref_sig8);
22651
22652 sig_type = lookup_signatured_type (*ref_cu, signature);
22653 /* sig_type will be NULL if the signatured type is missing from
22654 the debug info. */
22655 if (sig_type == NULL)
22656 {
22657 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22658 " from DIE at %s [in module %s]"),
22659 hex_string (signature), sect_offset_str (src_die->sect_off),
22660 objfile_name ((*ref_cu)->per_objfile->objfile));
22661 }
22662
22663 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22664 if (die == NULL)
22665 {
22666 dump_die_for_error (src_die);
22667 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22668 " from DIE at %s [in module %s]"),
22669 hex_string (signature), sect_offset_str (src_die->sect_off),
22670 objfile_name ((*ref_cu)->per_objfile->objfile));
22671 }
22672
22673 return die;
22674 }
22675
22676 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22677 reading in and processing the type unit if necessary. */
22678
22679 static struct type *
22680 get_signatured_type (struct die_info *die, ULONGEST signature,
22681 struct dwarf2_cu *cu)
22682 {
22683 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22684 struct signatured_type *sig_type;
22685 struct dwarf2_cu *type_cu;
22686 struct die_info *type_die;
22687 struct type *type;
22688
22689 sig_type = lookup_signatured_type (cu, signature);
22690 /* sig_type will be NULL if the signatured type is missing from
22691 the debug info. */
22692 if (sig_type == NULL)
22693 {
22694 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22695 " from DIE at %s [in module %s]"),
22696 hex_string (signature), sect_offset_str (die->sect_off),
22697 objfile_name (dwarf2_per_objfile->objfile));
22698 return build_error_marker_type (cu, die);
22699 }
22700
22701 /* If we already know the type we're done. */
22702 if (sig_type->type != NULL)
22703 return sig_type->type;
22704
22705 type_cu = cu;
22706 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22707 if (type_die != NULL)
22708 {
22709 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22710 is created. This is important, for example, because for c++ classes
22711 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22712 type = read_type_die (type_die, type_cu);
22713 if (type == NULL)
22714 {
22715 complaint (_("Dwarf Error: Cannot build signatured type %s"
22716 " referenced from DIE at %s [in module %s]"),
22717 hex_string (signature), sect_offset_str (die->sect_off),
22718 objfile_name (dwarf2_per_objfile->objfile));
22719 type = build_error_marker_type (cu, die);
22720 }
22721 }
22722 else
22723 {
22724 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22725 " from DIE at %s [in module %s]"),
22726 hex_string (signature), sect_offset_str (die->sect_off),
22727 objfile_name (dwarf2_per_objfile->objfile));
22728 type = build_error_marker_type (cu, die);
22729 }
22730 sig_type->type = type;
22731
22732 return type;
22733 }
22734
22735 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22736 reading in and processing the type unit if necessary. */
22737
22738 static struct type *
22739 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22740 struct dwarf2_cu *cu) /* ARI: editCase function */
22741 {
22742 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22743 if (attr->form_is_ref ())
22744 {
22745 struct dwarf2_cu *type_cu = cu;
22746 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22747
22748 return read_type_die (type_die, type_cu);
22749 }
22750 else if (attr->form == DW_FORM_ref_sig8)
22751 {
22752 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22753 }
22754 else
22755 {
22756 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22757
22758 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22759 " at %s [in module %s]"),
22760 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22761 objfile_name (dwarf2_per_objfile->objfile));
22762 return build_error_marker_type (cu, die);
22763 }
22764 }
22765
22766 /* Load the DIEs associated with type unit PER_CU into memory. */
22767
22768 static void
22769 load_full_type_unit (dwarf2_per_cu_data *per_cu,
22770 dwarf2_per_objfile *per_objfile)
22771 {
22772 struct signatured_type *sig_type;
22773
22774 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22775 gdb_assert (! per_cu->type_unit_group_p ());
22776
22777 /* We have the per_cu, but we need the signatured_type.
22778 Fortunately this is an easy translation. */
22779 gdb_assert (per_cu->is_debug_types);
22780 sig_type = (struct signatured_type *) per_cu;
22781
22782 gdb_assert (per_cu->cu == NULL);
22783
22784 read_signatured_type (sig_type, per_objfile);
22785
22786 gdb_assert (per_cu->cu != NULL);
22787 }
22788
22789 /* Read in a signatured type and build its CU and DIEs.
22790 If the type is a stub for the real type in a DWO file,
22791 read in the real type from the DWO file as well. */
22792
22793 static void
22794 read_signatured_type (signatured_type *sig_type,
22795 dwarf2_per_objfile *per_objfile)
22796 {
22797 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22798
22799 gdb_assert (per_cu->is_debug_types);
22800 gdb_assert (per_cu->cu == NULL);
22801
22802 cutu_reader reader (per_cu, per_objfile, NULL, 0, false);
22803
22804 if (!reader.dummy_p)
22805 {
22806 struct dwarf2_cu *cu = reader.cu;
22807 const gdb_byte *info_ptr = reader.info_ptr;
22808
22809 gdb_assert (cu->die_hash == NULL);
22810 cu->die_hash =
22811 htab_create_alloc_ex (cu->header.length / 12,
22812 die_hash,
22813 die_eq,
22814 NULL,
22815 &cu->comp_unit_obstack,
22816 hashtab_obstack_allocate,
22817 dummy_obstack_deallocate);
22818
22819 if (reader.comp_unit_die->has_children)
22820 reader.comp_unit_die->child
22821 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22822 reader.comp_unit_die);
22823 cu->dies = reader.comp_unit_die;
22824 /* comp_unit_die is not stored in die_hash, no need. */
22825
22826 /* We try not to read any attributes in this function, because
22827 not all CUs needed for references have been loaded yet, and
22828 symbol table processing isn't initialized. But we have to
22829 set the CU language, or we won't be able to build types
22830 correctly. Similarly, if we do not read the producer, we can
22831 not apply producer-specific interpretation. */
22832 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22833
22834 reader.keep ();
22835 }
22836
22837 sig_type->per_cu.tu_read = 1;
22838 }
22839
22840 /* Decode simple location descriptions.
22841 Given a pointer to a dwarf block that defines a location, compute
22842 the location and return the value. If COMPUTED is non-null, it is
22843 set to true to indicate that decoding was successful, and false
22844 otherwise. If COMPUTED is null, then this function may emit a
22845 complaint. */
22846
22847 static CORE_ADDR
22848 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22849 {
22850 struct objfile *objfile = cu->per_objfile->objfile;
22851 size_t i;
22852 size_t size = blk->size;
22853 const gdb_byte *data = blk->data;
22854 CORE_ADDR stack[64];
22855 int stacki;
22856 unsigned int bytes_read, unsnd;
22857 gdb_byte op;
22858
22859 if (computed != nullptr)
22860 *computed = false;
22861
22862 i = 0;
22863 stacki = 0;
22864 stack[stacki] = 0;
22865 stack[++stacki] = 0;
22866
22867 while (i < size)
22868 {
22869 op = data[i++];
22870 switch (op)
22871 {
22872 case DW_OP_lit0:
22873 case DW_OP_lit1:
22874 case DW_OP_lit2:
22875 case DW_OP_lit3:
22876 case DW_OP_lit4:
22877 case DW_OP_lit5:
22878 case DW_OP_lit6:
22879 case DW_OP_lit7:
22880 case DW_OP_lit8:
22881 case DW_OP_lit9:
22882 case DW_OP_lit10:
22883 case DW_OP_lit11:
22884 case DW_OP_lit12:
22885 case DW_OP_lit13:
22886 case DW_OP_lit14:
22887 case DW_OP_lit15:
22888 case DW_OP_lit16:
22889 case DW_OP_lit17:
22890 case DW_OP_lit18:
22891 case DW_OP_lit19:
22892 case DW_OP_lit20:
22893 case DW_OP_lit21:
22894 case DW_OP_lit22:
22895 case DW_OP_lit23:
22896 case DW_OP_lit24:
22897 case DW_OP_lit25:
22898 case DW_OP_lit26:
22899 case DW_OP_lit27:
22900 case DW_OP_lit28:
22901 case DW_OP_lit29:
22902 case DW_OP_lit30:
22903 case DW_OP_lit31:
22904 stack[++stacki] = op - DW_OP_lit0;
22905 break;
22906
22907 case DW_OP_reg0:
22908 case DW_OP_reg1:
22909 case DW_OP_reg2:
22910 case DW_OP_reg3:
22911 case DW_OP_reg4:
22912 case DW_OP_reg5:
22913 case DW_OP_reg6:
22914 case DW_OP_reg7:
22915 case DW_OP_reg8:
22916 case DW_OP_reg9:
22917 case DW_OP_reg10:
22918 case DW_OP_reg11:
22919 case DW_OP_reg12:
22920 case DW_OP_reg13:
22921 case DW_OP_reg14:
22922 case DW_OP_reg15:
22923 case DW_OP_reg16:
22924 case DW_OP_reg17:
22925 case DW_OP_reg18:
22926 case DW_OP_reg19:
22927 case DW_OP_reg20:
22928 case DW_OP_reg21:
22929 case DW_OP_reg22:
22930 case DW_OP_reg23:
22931 case DW_OP_reg24:
22932 case DW_OP_reg25:
22933 case DW_OP_reg26:
22934 case DW_OP_reg27:
22935 case DW_OP_reg28:
22936 case DW_OP_reg29:
22937 case DW_OP_reg30:
22938 case DW_OP_reg31:
22939 stack[++stacki] = op - DW_OP_reg0;
22940 if (i < size)
22941 {
22942 if (computed == nullptr)
22943 dwarf2_complex_location_expr_complaint ();
22944 else
22945 return 0;
22946 }
22947 break;
22948
22949 case DW_OP_regx:
22950 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22951 i += bytes_read;
22952 stack[++stacki] = unsnd;
22953 if (i < size)
22954 {
22955 if (computed == nullptr)
22956 dwarf2_complex_location_expr_complaint ();
22957 else
22958 return 0;
22959 }
22960 break;
22961
22962 case DW_OP_addr:
22963 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22964 &bytes_read);
22965 i += bytes_read;
22966 break;
22967
22968 case DW_OP_const1u:
22969 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22970 i += 1;
22971 break;
22972
22973 case DW_OP_const1s:
22974 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22975 i += 1;
22976 break;
22977
22978 case DW_OP_const2u:
22979 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22980 i += 2;
22981 break;
22982
22983 case DW_OP_const2s:
22984 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22985 i += 2;
22986 break;
22987
22988 case DW_OP_const4u:
22989 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22990 i += 4;
22991 break;
22992
22993 case DW_OP_const4s:
22994 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22995 i += 4;
22996 break;
22997
22998 case DW_OP_const8u:
22999 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23000 i += 8;
23001 break;
23002
23003 case DW_OP_constu:
23004 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23005 &bytes_read);
23006 i += bytes_read;
23007 break;
23008
23009 case DW_OP_consts:
23010 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23011 i += bytes_read;
23012 break;
23013
23014 case DW_OP_dup:
23015 stack[stacki + 1] = stack[stacki];
23016 stacki++;
23017 break;
23018
23019 case DW_OP_plus:
23020 stack[stacki - 1] += stack[stacki];
23021 stacki--;
23022 break;
23023
23024 case DW_OP_plus_uconst:
23025 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23026 &bytes_read);
23027 i += bytes_read;
23028 break;
23029
23030 case DW_OP_minus:
23031 stack[stacki - 1] -= stack[stacki];
23032 stacki--;
23033 break;
23034
23035 case DW_OP_deref:
23036 /* If we're not the last op, then we definitely can't encode
23037 this using GDB's address_class enum. This is valid for partial
23038 global symbols, although the variable's address will be bogus
23039 in the psymtab. */
23040 if (i < size)
23041 {
23042 if (computed == nullptr)
23043 dwarf2_complex_location_expr_complaint ();
23044 else
23045 return 0;
23046 }
23047 break;
23048
23049 case DW_OP_GNU_push_tls_address:
23050 case DW_OP_form_tls_address:
23051 /* The top of the stack has the offset from the beginning
23052 of the thread control block at which the variable is located. */
23053 /* Nothing should follow this operator, so the top of stack would
23054 be returned. */
23055 /* This is valid for partial global symbols, but the variable's
23056 address will be bogus in the psymtab. Make it always at least
23057 non-zero to not look as a variable garbage collected by linker
23058 which have DW_OP_addr 0. */
23059 if (i < size)
23060 {
23061 if (computed == nullptr)
23062 dwarf2_complex_location_expr_complaint ();
23063 else
23064 return 0;
23065 }
23066 stack[stacki]++;
23067 break;
23068
23069 case DW_OP_GNU_uninit:
23070 if (computed != nullptr)
23071 return 0;
23072 break;
23073
23074 case DW_OP_addrx:
23075 case DW_OP_GNU_addr_index:
23076 case DW_OP_GNU_const_index:
23077 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23078 &bytes_read);
23079 i += bytes_read;
23080 break;
23081
23082 default:
23083 if (computed == nullptr)
23084 {
23085 const char *name = get_DW_OP_name (op);
23086
23087 if (name)
23088 complaint (_("unsupported stack op: '%s'"),
23089 name);
23090 else
23091 complaint (_("unsupported stack op: '%02x'"),
23092 op);
23093 }
23094
23095 return (stack[stacki]);
23096 }
23097
23098 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23099 outside of the allocated space. Also enforce minimum>0. */
23100 if (stacki >= ARRAY_SIZE (stack) - 1)
23101 {
23102 if (computed == nullptr)
23103 complaint (_("location description stack overflow"));
23104 return 0;
23105 }
23106
23107 if (stacki <= 0)
23108 {
23109 if (computed == nullptr)
23110 complaint (_("location description stack underflow"));
23111 return 0;
23112 }
23113 }
23114
23115 if (computed != nullptr)
23116 *computed = true;
23117 return (stack[stacki]);
23118 }
23119
23120 /* memory allocation interface */
23121
23122 static struct dwarf_block *
23123 dwarf_alloc_block (struct dwarf2_cu *cu)
23124 {
23125 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23126 }
23127
23128 static struct die_info *
23129 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23130 {
23131 struct die_info *die;
23132 size_t size = sizeof (struct die_info);
23133
23134 if (num_attrs > 1)
23135 size += (num_attrs - 1) * sizeof (struct attribute);
23136
23137 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23138 memset (die, 0, sizeof (struct die_info));
23139 return (die);
23140 }
23141
23142 \f
23143
23144 /* Macro support. */
23145
23146 /* An overload of dwarf_decode_macros that finds the correct section
23147 and ensures it is read in before calling the other overload. */
23148
23149 static void
23150 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23151 int section_is_gnu)
23152 {
23153 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23154 struct objfile *objfile = dwarf2_per_objfile->objfile;
23155 const struct line_header *lh = cu->line_header;
23156 unsigned int offset_size = cu->header.offset_size;
23157 struct dwarf2_section_info *section;
23158 const char *section_name;
23159
23160 if (cu->dwo_unit != nullptr)
23161 {
23162 if (section_is_gnu)
23163 {
23164 section = &cu->dwo_unit->dwo_file->sections.macro;
23165 section_name = ".debug_macro.dwo";
23166 }
23167 else
23168 {
23169 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23170 section_name = ".debug_macinfo.dwo";
23171 }
23172 }
23173 else
23174 {
23175 if (section_is_gnu)
23176 {
23177 section = &dwarf2_per_objfile->per_bfd->macro;
23178 section_name = ".debug_macro";
23179 }
23180 else
23181 {
23182 section = &dwarf2_per_objfile->per_bfd->macinfo;
23183 section_name = ".debug_macinfo";
23184 }
23185 }
23186
23187 section->read (objfile);
23188 if (section->buffer == nullptr)
23189 {
23190 complaint (_("missing %s section"), section_name);
23191 return;
23192 }
23193
23194 buildsym_compunit *builder = cu->get_builder ();
23195
23196 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23197 offset_size, offset, section_is_gnu);
23198 }
23199
23200 /* Return the .debug_loc section to use for CU.
23201 For DWO files use .debug_loc.dwo. */
23202
23203 static struct dwarf2_section_info *
23204 cu_debug_loc_section (struct dwarf2_cu *cu)
23205 {
23206 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23207
23208 if (cu->dwo_unit)
23209 {
23210 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23211
23212 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23213 }
23214 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23215 : &dwarf2_per_objfile->per_bfd->loc);
23216 }
23217
23218 /* A helper function that fills in a dwarf2_loclist_baton. */
23219
23220 static void
23221 fill_in_loclist_baton (struct dwarf2_cu *cu,
23222 struct dwarf2_loclist_baton *baton,
23223 const struct attribute *attr)
23224 {
23225 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23226 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23227
23228 section->read (dwarf2_per_objfile->objfile);
23229
23230 baton->per_objfile = dwarf2_per_objfile;
23231 baton->per_cu = cu->per_cu;
23232 gdb_assert (baton->per_cu);
23233 /* We don't know how long the location list is, but make sure we
23234 don't run off the edge of the section. */
23235 baton->size = section->size - DW_UNSND (attr);
23236 baton->data = section->buffer + DW_UNSND (attr);
23237 if (cu->base_address.has_value ())
23238 baton->base_address = *cu->base_address;
23239 else
23240 baton->base_address = 0;
23241 baton->from_dwo = cu->dwo_unit != NULL;
23242 }
23243
23244 static void
23245 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23246 struct dwarf2_cu *cu, int is_block)
23247 {
23248 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23249 struct objfile *objfile = dwarf2_per_objfile->objfile;
23250 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23251
23252 if (attr->form_is_section_offset ()
23253 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23254 the section. If so, fall through to the complaint in the
23255 other branch. */
23256 && DW_UNSND (attr) < section->get_size (objfile))
23257 {
23258 struct dwarf2_loclist_baton *baton;
23259
23260 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23261
23262 fill_in_loclist_baton (cu, baton, attr);
23263
23264 if (!cu->base_address.has_value ())
23265 complaint (_("Location list used without "
23266 "specifying the CU base address."));
23267
23268 SYMBOL_ACLASS_INDEX (sym) = (is_block
23269 ? dwarf2_loclist_block_index
23270 : dwarf2_loclist_index);
23271 SYMBOL_LOCATION_BATON (sym) = baton;
23272 }
23273 else
23274 {
23275 struct dwarf2_locexpr_baton *baton;
23276
23277 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23278 baton->per_objfile = dwarf2_per_objfile;
23279 baton->per_cu = cu->per_cu;
23280 gdb_assert (baton->per_cu);
23281
23282 if (attr->form_is_block ())
23283 {
23284 /* Note that we're just copying the block's data pointer
23285 here, not the actual data. We're still pointing into the
23286 info_buffer for SYM's objfile; right now we never release
23287 that buffer, but when we do clean up properly this may
23288 need to change. */
23289 baton->size = DW_BLOCK (attr)->size;
23290 baton->data = DW_BLOCK (attr)->data;
23291 }
23292 else
23293 {
23294 dwarf2_invalid_attrib_class_complaint ("location description",
23295 sym->natural_name ());
23296 baton->size = 0;
23297 }
23298
23299 SYMBOL_ACLASS_INDEX (sym) = (is_block
23300 ? dwarf2_locexpr_block_index
23301 : dwarf2_locexpr_index);
23302 SYMBOL_LOCATION_BATON (sym) = baton;
23303 }
23304 }
23305
23306 /* See read.h. */
23307
23308 struct objfile *
23309 dwarf2_per_cu_data::objfile () const
23310 {
23311 struct objfile *objfile = dwarf2_per_objfile->objfile;
23312
23313 /* Return the master objfile, so that we can report and look up the
23314 correct file containing this variable. */
23315 if (objfile->separate_debug_objfile_backlink)
23316 objfile = objfile->separate_debug_objfile_backlink;
23317
23318 return objfile;
23319 }
23320
23321 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23322 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23323 CU_HEADERP first. */
23324
23325 static const struct comp_unit_head *
23326 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23327 const struct dwarf2_per_cu_data *per_cu)
23328 {
23329 const gdb_byte *info_ptr;
23330
23331 if (per_cu->cu)
23332 return &per_cu->cu->header;
23333
23334 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
23335
23336 memset (cu_headerp, 0, sizeof (*cu_headerp));
23337 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23338 rcuh_kind::COMPILE);
23339
23340 return cu_headerp;
23341 }
23342
23343 /* See read.h. */
23344
23345 int
23346 dwarf2_per_cu_data::addr_size () const
23347 {
23348 struct comp_unit_head cu_header_local;
23349 const struct comp_unit_head *cu_headerp;
23350
23351 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23352
23353 return cu_headerp->addr_size;
23354 }
23355
23356 /* See read.h. */
23357
23358 int
23359 dwarf2_per_cu_data::offset_size () const
23360 {
23361 struct comp_unit_head cu_header_local;
23362 const struct comp_unit_head *cu_headerp;
23363
23364 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23365
23366 return cu_headerp->offset_size;
23367 }
23368
23369 /* See read.h. */
23370
23371 int
23372 dwarf2_per_cu_data::ref_addr_size () const
23373 {
23374 struct comp_unit_head cu_header_local;
23375 const struct comp_unit_head *cu_headerp;
23376
23377 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23378
23379 if (cu_headerp->version == 2)
23380 return cu_headerp->addr_size;
23381 else
23382 return cu_headerp->offset_size;
23383 }
23384
23385 /* See read.h. */
23386
23387 CORE_ADDR
23388 dwarf2_per_cu_data::text_offset () const
23389 {
23390 struct objfile *objfile = dwarf2_per_objfile->objfile;
23391
23392 return objfile->text_section_offset ();
23393 }
23394
23395 /* See read.h. */
23396
23397 struct type *
23398 dwarf2_per_cu_data::addr_type () const
23399 {
23400 struct objfile *objfile = dwarf2_per_objfile->objfile;
23401 struct type *void_type = objfile_type (objfile)->builtin_void;
23402 struct type *addr_type = lookup_pointer_type (void_type);
23403 int addr_size = this->addr_size ();
23404
23405 if (TYPE_LENGTH (addr_type) == addr_size)
23406 return addr_type;
23407
23408 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23409 return addr_type;
23410 }
23411
23412 /* A helper function for dwarf2_find_containing_comp_unit that returns
23413 the index of the result, and that searches a vector. It will
23414 return a result even if the offset in question does not actually
23415 occur in any CU. This is separate so that it can be unit
23416 tested. */
23417
23418 static int
23419 dwarf2_find_containing_comp_unit
23420 (sect_offset sect_off,
23421 unsigned int offset_in_dwz,
23422 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23423 {
23424 int low, high;
23425
23426 low = 0;
23427 high = all_comp_units.size () - 1;
23428 while (high > low)
23429 {
23430 struct dwarf2_per_cu_data *mid_cu;
23431 int mid = low + (high - low) / 2;
23432
23433 mid_cu = all_comp_units[mid];
23434 if (mid_cu->is_dwz > offset_in_dwz
23435 || (mid_cu->is_dwz == offset_in_dwz
23436 && mid_cu->sect_off + mid_cu->length > sect_off))
23437 high = mid;
23438 else
23439 low = mid + 1;
23440 }
23441 gdb_assert (low == high);
23442 return low;
23443 }
23444
23445 /* Locate the .debug_info compilation unit from CU's objfile which contains
23446 the DIE at OFFSET. Raises an error on failure. */
23447
23448 static struct dwarf2_per_cu_data *
23449 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23450 unsigned int offset_in_dwz,
23451 struct dwarf2_per_objfile *dwarf2_per_objfile)
23452 {
23453 int low
23454 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23455 dwarf2_per_objfile->per_bfd->all_comp_units);
23456 struct dwarf2_per_cu_data *this_cu
23457 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
23458
23459 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23460 {
23461 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23462 error (_("Dwarf Error: could not find partial DIE containing "
23463 "offset %s [in module %s]"),
23464 sect_offset_str (sect_off),
23465 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23466
23467 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
23468 <= sect_off);
23469 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
23470 }
23471 else
23472 {
23473 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
23474 && sect_off >= this_cu->sect_off + this_cu->length)
23475 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23476 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23477 return this_cu;
23478 }
23479 }
23480
23481 #if GDB_SELF_TEST
23482
23483 namespace selftests {
23484 namespace find_containing_comp_unit {
23485
23486 static void
23487 run_test ()
23488 {
23489 struct dwarf2_per_cu_data one {};
23490 struct dwarf2_per_cu_data two {};
23491 struct dwarf2_per_cu_data three {};
23492 struct dwarf2_per_cu_data four {};
23493
23494 one.length = 5;
23495 two.sect_off = sect_offset (one.length);
23496 two.length = 7;
23497
23498 three.length = 5;
23499 three.is_dwz = 1;
23500 four.sect_off = sect_offset (three.length);
23501 four.length = 7;
23502 four.is_dwz = 1;
23503
23504 std::vector<dwarf2_per_cu_data *> units;
23505 units.push_back (&one);
23506 units.push_back (&two);
23507 units.push_back (&three);
23508 units.push_back (&four);
23509
23510 int result;
23511
23512 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23513 SELF_CHECK (units[result] == &one);
23514 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23515 SELF_CHECK (units[result] == &one);
23516 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23517 SELF_CHECK (units[result] == &two);
23518
23519 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23520 SELF_CHECK (units[result] == &three);
23521 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23522 SELF_CHECK (units[result] == &three);
23523 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23524 SELF_CHECK (units[result] == &four);
23525 }
23526
23527 }
23528 }
23529
23530 #endif /* GDB_SELF_TEST */
23531
23532 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
23533
23534 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23535 dwarf2_per_objfile *per_objfile)
23536 : per_cu (per_cu),
23537 per_objfile (per_objfile),
23538 mark (false),
23539 has_loclist (false),
23540 checked_producer (false),
23541 producer_is_gxx_lt_4_6 (false),
23542 producer_is_gcc_lt_4_3 (false),
23543 producer_is_icc (false),
23544 producer_is_icc_lt_14 (false),
23545 producer_is_codewarrior (false),
23546 processing_has_namespace_info (false)
23547 {
23548 per_cu->cu = this;
23549 }
23550
23551 /* Destroy a dwarf2_cu. */
23552
23553 dwarf2_cu::~dwarf2_cu ()
23554 {
23555 per_cu->cu = NULL;
23556 }
23557
23558 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23559
23560 static void
23561 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23562 enum language pretend_language)
23563 {
23564 struct attribute *attr;
23565
23566 /* Set the language we're debugging. */
23567 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23568 if (attr != nullptr)
23569 set_cu_language (DW_UNSND (attr), cu);
23570 else
23571 {
23572 cu->language = pretend_language;
23573 cu->language_defn = language_def (cu->language);
23574 }
23575
23576 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23577 }
23578
23579 /* Increase the age counter on each cached compilation unit, and free
23580 any that are too old. */
23581
23582 static void
23583 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23584 {
23585 struct dwarf2_per_cu_data *per_cu, **last_chain;
23586
23587 dwarf2_clear_marks (dwarf2_per_objfile->per_bfd->read_in_chain);
23588 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23589 while (per_cu != NULL)
23590 {
23591 per_cu->cu->last_used ++;
23592 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23593 dwarf2_mark (per_cu->cu);
23594 per_cu = per_cu->cu->read_in_chain;
23595 }
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->cu->mark)
23606 {
23607 delete per_cu->cu;
23608 *last_chain = next_cu;
23609 }
23610 else
23611 last_chain = &per_cu->cu->read_in_chain;
23612
23613 per_cu = next_cu;
23614 }
23615 }
23616
23617 /* Remove a single compilation unit from the cache. */
23618
23619 static void
23620 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23621 {
23622 struct dwarf2_per_cu_data *per_cu, **last_chain;
23623 struct dwarf2_per_objfile *dwarf2_per_objfile
23624 = target_per_cu->dwarf2_per_objfile;
23625
23626 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23627 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23628 while (per_cu != NULL)
23629 {
23630 struct dwarf2_per_cu_data *next_cu;
23631
23632 next_cu = per_cu->cu->read_in_chain;
23633
23634 if (per_cu == target_per_cu)
23635 {
23636 delete per_cu->cu;
23637 per_cu->cu = NULL;
23638 *last_chain = next_cu;
23639 break;
23640 }
23641 else
23642 last_chain = &per_cu->cu->read_in_chain;
23643
23644 per_cu = next_cu;
23645 }
23646 }
23647
23648 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23649 We store these in a hash table separate from the DIEs, and preserve them
23650 when the DIEs are flushed out of cache.
23651
23652 The CU "per_cu" pointer is needed because offset alone is not enough to
23653 uniquely identify the type. A file may have multiple .debug_types sections,
23654 or the type may come from a DWO file. Furthermore, while it's more logical
23655 to use per_cu->section+offset, with Fission the section with the data is in
23656 the DWO file but we don't know that section at the point we need it.
23657 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23658 because we can enter the lookup routine, get_die_type_at_offset, from
23659 outside this file, and thus won't necessarily have PER_CU->cu.
23660 Fortunately, PER_CU is stable for the life of the objfile. */
23661
23662 struct dwarf2_per_cu_offset_and_type
23663 {
23664 const struct dwarf2_per_cu_data *per_cu;
23665 sect_offset sect_off;
23666 struct type *type;
23667 };
23668
23669 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23670
23671 static hashval_t
23672 per_cu_offset_and_type_hash (const void *item)
23673 {
23674 const struct dwarf2_per_cu_offset_and_type *ofs
23675 = (const struct dwarf2_per_cu_offset_and_type *) item;
23676
23677 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23678 }
23679
23680 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23681
23682 static int
23683 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23684 {
23685 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23686 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23687 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23688 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23689
23690 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23691 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23692 }
23693
23694 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23695 table if necessary. For convenience, return TYPE.
23696
23697 The DIEs reading must have careful ordering to:
23698 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23699 reading current DIE.
23700 * Not trying to dereference contents of still incompletely read in types
23701 while reading in other DIEs.
23702 * Enable referencing still incompletely read in types just by a pointer to
23703 the type without accessing its fields.
23704
23705 Therefore caller should follow these rules:
23706 * Try to fetch any prerequisite types we may need to build this DIE type
23707 before building the type and calling set_die_type.
23708 * After building type call set_die_type for current DIE as soon as
23709 possible before fetching more types to complete the current type.
23710 * Make the type as complete as possible before fetching more types. */
23711
23712 static struct type *
23713 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23714 {
23715 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23716 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23717 struct objfile *objfile = dwarf2_per_objfile->objfile;
23718 struct attribute *attr;
23719 struct dynamic_prop prop;
23720
23721 /* For Ada types, make sure that the gnat-specific data is always
23722 initialized (if not already set). There are a few types where
23723 we should not be doing so, because the type-specific area is
23724 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23725 where the type-specific area is used to store the floatformat).
23726 But this is not a problem, because the gnat-specific information
23727 is actually not needed for these types. */
23728 if (need_gnat_info (cu)
23729 && type->code () != TYPE_CODE_FUNC
23730 && type->code () != TYPE_CODE_FLT
23731 && type->code () != TYPE_CODE_METHODPTR
23732 && type->code () != TYPE_CODE_MEMBERPTR
23733 && type->code () != TYPE_CODE_METHOD
23734 && !HAVE_GNAT_AUX_INFO (type))
23735 INIT_GNAT_SPECIFIC (type);
23736
23737 /* Read DW_AT_allocated and set in type. */
23738 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23739 if (attr != NULL && attr->form_is_block ())
23740 {
23741 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23742 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23743 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23744 }
23745 else if (attr != NULL)
23746 {
23747 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23748 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23749 sect_offset_str (die->sect_off));
23750 }
23751
23752 /* Read DW_AT_associated and set in type. */
23753 attr = dwarf2_attr (die, DW_AT_associated, cu);
23754 if (attr != NULL && attr->form_is_block ())
23755 {
23756 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23757 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23758 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23759 }
23760 else if (attr != NULL)
23761 {
23762 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23763 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23764 sect_offset_str (die->sect_off));
23765 }
23766
23767 /* Read DW_AT_data_location and set in type. */
23768 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23769 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23770 cu->per_cu->addr_type ()))
23771 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23772
23773 if (dwarf2_per_objfile->die_type_hash == NULL)
23774 dwarf2_per_objfile->die_type_hash
23775 = htab_up (htab_create_alloc (127,
23776 per_cu_offset_and_type_hash,
23777 per_cu_offset_and_type_eq,
23778 NULL, xcalloc, xfree));
23779
23780 ofs.per_cu = cu->per_cu;
23781 ofs.sect_off = die->sect_off;
23782 ofs.type = type;
23783 slot = (struct dwarf2_per_cu_offset_and_type **)
23784 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23785 if (*slot)
23786 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23787 sect_offset_str (die->sect_off));
23788 *slot = XOBNEW (&objfile->objfile_obstack,
23789 struct dwarf2_per_cu_offset_and_type);
23790 **slot = ofs;
23791 return type;
23792 }
23793
23794 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23795 or return NULL if the die does not have a saved type. */
23796
23797 static struct type *
23798 get_die_type_at_offset (sect_offset sect_off,
23799 struct dwarf2_per_cu_data *per_cu)
23800 {
23801 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23802 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23803
23804 if (dwarf2_per_objfile->die_type_hash == NULL)
23805 return NULL;
23806
23807 ofs.per_cu = per_cu;
23808 ofs.sect_off = sect_off;
23809 slot = ((struct dwarf2_per_cu_offset_and_type *)
23810 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23811 if (slot)
23812 return slot->type;
23813 else
23814 return NULL;
23815 }
23816
23817 /* Look up the type for DIE in CU in die_type_hash,
23818 or return NULL if DIE does not have a saved type. */
23819
23820 static struct type *
23821 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23822 {
23823 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23824 }
23825
23826 /* Add a dependence relationship from CU to REF_PER_CU. */
23827
23828 static void
23829 dwarf2_add_dependence (struct dwarf2_cu *cu,
23830 struct dwarf2_per_cu_data *ref_per_cu)
23831 {
23832 void **slot;
23833
23834 if (cu->dependencies == NULL)
23835 cu->dependencies
23836 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23837 NULL, &cu->comp_unit_obstack,
23838 hashtab_obstack_allocate,
23839 dummy_obstack_deallocate);
23840
23841 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23842 if (*slot == NULL)
23843 *slot = ref_per_cu;
23844 }
23845
23846 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23847 Set the mark field in every compilation unit in the
23848 cache that we must keep because we are keeping CU. */
23849
23850 static int
23851 dwarf2_mark_helper (void **slot, void *data)
23852 {
23853 struct dwarf2_per_cu_data *per_cu;
23854
23855 per_cu = (struct dwarf2_per_cu_data *) *slot;
23856
23857 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23858 reading of the chain. As such dependencies remain valid it is not much
23859 useful to track and undo them during QUIT cleanups. */
23860 if (per_cu->cu == NULL)
23861 return 1;
23862
23863 if (per_cu->cu->mark)
23864 return 1;
23865 per_cu->cu->mark = true;
23866
23867 if (per_cu->cu->dependencies != NULL)
23868 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23869
23870 return 1;
23871 }
23872
23873 /* Set the mark field in CU and in every other compilation unit in the
23874 cache that we must keep because we are keeping CU. */
23875
23876 static void
23877 dwarf2_mark (struct dwarf2_cu *cu)
23878 {
23879 if (cu->mark)
23880 return;
23881 cu->mark = true;
23882 if (cu->dependencies != NULL)
23883 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23884 }
23885
23886 static void
23887 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23888 {
23889 while (per_cu)
23890 {
23891 per_cu->cu->mark = false;
23892 per_cu = per_cu->cu->read_in_chain;
23893 }
23894 }
23895
23896 /* Trivial hash function for partial_die_info: the hash value of a DIE
23897 is its offset in .debug_info for this objfile. */
23898
23899 static hashval_t
23900 partial_die_hash (const void *item)
23901 {
23902 const struct partial_die_info *part_die
23903 = (const struct partial_die_info *) item;
23904
23905 return to_underlying (part_die->sect_off);
23906 }
23907
23908 /* Trivial comparison function for partial_die_info structures: two DIEs
23909 are equal if they have the same offset. */
23910
23911 static int
23912 partial_die_eq (const void *item_lhs, const void *item_rhs)
23913 {
23914 const struct partial_die_info *part_die_lhs
23915 = (const struct partial_die_info *) item_lhs;
23916 const struct partial_die_info *part_die_rhs
23917 = (const struct partial_die_info *) item_rhs;
23918
23919 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23920 }
23921
23922 struct cmd_list_element *set_dwarf_cmdlist;
23923 struct cmd_list_element *show_dwarf_cmdlist;
23924
23925 static void
23926 show_check_physname (struct ui_file *file, int from_tty,
23927 struct cmd_list_element *c, const char *value)
23928 {
23929 fprintf_filtered (file,
23930 _("Whether to check \"physname\" is %s.\n"),
23931 value);
23932 }
23933
23934 void _initialize_dwarf2_read ();
23935 void
23936 _initialize_dwarf2_read ()
23937 {
23938 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
23939 Set DWARF specific variables.\n\
23940 Configure DWARF variables such as the cache size."),
23941 &set_dwarf_cmdlist, "maintenance set dwarf ",
23942 0/*allow-unknown*/, &maintenance_set_cmdlist);
23943
23944 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
23945 Show DWARF specific variables.\n\
23946 Show DWARF variables such as the cache size."),
23947 &show_dwarf_cmdlist, "maintenance show dwarf ",
23948 0/*allow-unknown*/, &maintenance_show_cmdlist);
23949
23950 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23951 &dwarf_max_cache_age, _("\
23952 Set the upper bound on the age of cached DWARF compilation units."), _("\
23953 Show the upper bound on the age of cached DWARF compilation units."), _("\
23954 A higher limit means that cached compilation units will be stored\n\
23955 in memory longer, and more total memory will be used. Zero disables\n\
23956 caching, which can slow down startup."),
23957 NULL,
23958 show_dwarf_max_cache_age,
23959 &set_dwarf_cmdlist,
23960 &show_dwarf_cmdlist);
23961
23962 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23963 Set debugging of the DWARF reader."), _("\
23964 Show debugging of the DWARF reader."), _("\
23965 When enabled (non-zero), debugging messages are printed during DWARF\n\
23966 reading and symtab expansion. A value of 1 (one) provides basic\n\
23967 information. A value greater than 1 provides more verbose information."),
23968 NULL,
23969 NULL,
23970 &setdebuglist, &showdebuglist);
23971
23972 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23973 Set debugging of the DWARF DIE reader."), _("\
23974 Show debugging of the DWARF DIE reader."), _("\
23975 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23976 The value is the maximum depth to print."),
23977 NULL,
23978 NULL,
23979 &setdebuglist, &showdebuglist);
23980
23981 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23982 Set debugging of the dwarf line reader."), _("\
23983 Show debugging of the dwarf line reader."), _("\
23984 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23985 A value of 1 (one) provides basic information.\n\
23986 A value greater than 1 provides more verbose information."),
23987 NULL,
23988 NULL,
23989 &setdebuglist, &showdebuglist);
23990
23991 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23992 Set cross-checking of \"physname\" code against demangler."), _("\
23993 Show cross-checking of \"physname\" code against demangler."), _("\
23994 When enabled, GDB's internal \"physname\" code is checked against\n\
23995 the demangler."),
23996 NULL, show_check_physname,
23997 &setdebuglist, &showdebuglist);
23998
23999 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24000 no_class, &use_deprecated_index_sections, _("\
24001 Set whether to use deprecated gdb_index sections."), _("\
24002 Show whether to use deprecated gdb_index sections."), _("\
24003 When enabled, deprecated .gdb_index sections are used anyway.\n\
24004 Normally they are ignored either because of a missing feature or\n\
24005 performance issue.\n\
24006 Warning: This option must be enabled before gdb reads the file."),
24007 NULL,
24008 NULL,
24009 &setlist, &showlist);
24010
24011 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24012 &dwarf2_locexpr_funcs);
24013 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24014 &dwarf2_loclist_funcs);
24015
24016 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24017 &dwarf2_block_frame_base_locexpr_funcs);
24018 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24019 &dwarf2_block_frame_base_loclist_funcs);
24020
24021 #if GDB_SELF_TEST
24022 selftests::register_test ("dw2_expand_symtabs_matching",
24023 selftests::dw2_expand_symtabs_matching::run_test);
24024 selftests::register_test ("dwarf2_find_containing_comp_unit",
24025 selftests::find_containing_comp_unit::run_test);
24026 #endif
24027 }