]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
06e843aeb82917ca83db7caa9b2cf6e9afa3d62a
[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 /* Return a type that is a generic pointer type, the size of which
425 matches the address size given in the compilation unit header for
426 this CU. */
427 struct type *addr_type () const;
428
429 /* Find an integer type the same size as the address size given in
430 the compilation unit header for this CU. UNSIGNED_P controls if
431 the integer is unsigned or not. */
432 struct type *addr_sized_int_type (bool unsigned_p) const;
433
434 /* The header of the compilation unit. */
435 struct comp_unit_head header {};
436
437 /* Base address of this compilation unit. */
438 gdb::optional<CORE_ADDR> base_address;
439
440 /* The language we are debugging. */
441 enum language language = language_unknown;
442 const struct language_defn *language_defn = nullptr;
443
444 const char *producer = nullptr;
445
446 private:
447 /* The symtab builder for this CU. This is only non-NULL when full
448 symbols are being read. */
449 std::unique_ptr<buildsym_compunit> m_builder;
450
451 public:
452 /* The generic symbol table building routines have separate lists for
453 file scope symbols and all all other scopes (local scopes). So
454 we need to select the right one to pass to add_symbol_to_list().
455 We do it by keeping a pointer to the correct list in list_in_scope.
456
457 FIXME: The original dwarf code just treated the file scope as the
458 first local scope, and all other local scopes as nested local
459 scopes, and worked fine. Check to see if we really need to
460 distinguish these in buildsym.c. */
461 struct pending **list_in_scope = nullptr;
462
463 /* Hash table holding all the loaded partial DIEs
464 with partial_die->offset.SECT_OFF as hash. */
465 htab_t partial_dies = nullptr;
466
467 /* Storage for things with the same lifetime as this read-in compilation
468 unit, including partial DIEs. */
469 auto_obstack comp_unit_obstack;
470
471 /* When multiple dwarf2_cu structures are living in memory, this field
472 chains them all together, so that they can be released efficiently.
473 We will probably also want a generation counter so that most-recently-used
474 compilation units are cached... */
475 struct dwarf2_per_cu_data *read_in_chain = nullptr;
476
477 /* Backlink to our per_cu entry. */
478 struct dwarf2_per_cu_data *per_cu;
479
480 /* The dwarf2_per_objfile that owns this. */
481 struct dwarf2_per_objfile *per_objfile;
482
483 /* How many compilation units ago was this CU last referenced? */
484 int last_used = 0;
485
486 /* A hash table of DIE cu_offset for following references with
487 die_info->offset.sect_off as hash. */
488 htab_t die_hash = nullptr;
489
490 /* Full DIEs if read in. */
491 struct die_info *dies = nullptr;
492
493 /* A set of pointers to dwarf2_per_cu_data objects for compilation
494 units referenced by this one. Only set during full symbol processing;
495 partial symbol tables do not have dependencies. */
496 htab_t dependencies = nullptr;
497
498 /* Header data from the line table, during full symbol processing. */
499 struct line_header *line_header = nullptr;
500 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
501 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
502 this is the DW_TAG_compile_unit die for this CU. We'll hold on
503 to the line header as long as this DIE is being processed. See
504 process_die_scope. */
505 die_info *line_header_die_owner = nullptr;
506
507 /* A list of methods which need to have physnames computed
508 after all type information has been read. */
509 std::vector<delayed_method_info> method_list;
510
511 /* To be copied to symtab->call_site_htab. */
512 htab_t call_site_htab = nullptr;
513
514 /* Non-NULL if this CU came from a DWO file.
515 There is an invariant here that is important to remember:
516 Except for attributes copied from the top level DIE in the "main"
517 (or "stub") file in preparation for reading the DWO file
518 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
519 Either there isn't a DWO file (in which case this is NULL and the point
520 is moot), or there is and either we're not going to read it (in which
521 case this is NULL) or there is and we are reading it (in which case this
522 is non-NULL). */
523 struct dwo_unit *dwo_unit = nullptr;
524
525 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
526 Note this value comes from the Fission stub CU/TU's DIE. */
527 gdb::optional<ULONGEST> addr_base;
528
529 /* The DW_AT_rnglists_base attribute if present.
530 Note this value comes from the Fission stub CU/TU's DIE.
531 Also note that the value is zero in the non-DWO case so this value can
532 be used without needing to know whether DWO files are in use or not.
533 N.B. This does not apply to DW_AT_ranges appearing in
534 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
535 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
536 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
537 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
538 ULONGEST ranges_base = 0;
539
540 /* The DW_AT_loclists_base attribute if present. */
541 ULONGEST loclist_base = 0;
542
543 /* When reading debug info generated by older versions of rustc, we
544 have to rewrite some union types to be struct types with a
545 variant part. This rewriting must be done after the CU is fully
546 read in, because otherwise at the point of rewriting some struct
547 type might not have been fully processed. So, we keep a list of
548 all such types here and process them after expansion. */
549 std::vector<struct type *> rust_unions;
550
551 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
552 files, the value is implicitly zero. For DWARF 5 version DWO files, the
553 value is often implicit and is the size of the header of
554 .debug_str_offsets section (8 or 4, depending on the address size). */
555 gdb::optional<ULONGEST> str_offsets_base;
556
557 /* Mark used when releasing cached dies. */
558 bool mark : 1;
559
560 /* This CU references .debug_loc. See the symtab->locations_valid field.
561 This test is imperfect as there may exist optimized debug code not using
562 any location list and still facing inlining issues if handled as
563 unoptimized code. For a future better test see GCC PR other/32998. */
564 bool has_loclist : 1;
565
566 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
567 if all the producer_is_* fields are valid. This information is cached
568 because profiling CU expansion showed excessive time spent in
569 producer_is_gxx_lt_4_6. */
570 bool checked_producer : 1;
571 bool producer_is_gxx_lt_4_6 : 1;
572 bool producer_is_gcc_lt_4_3 : 1;
573 bool producer_is_icc : 1;
574 bool producer_is_icc_lt_14 : 1;
575 bool producer_is_codewarrior : 1;
576
577 /* When true, the file that we're processing is known to have
578 debugging info for C++ namespaces. GCC 3.3.x did not produce
579 this information, but later versions do. */
580
581 bool processing_has_namespace_info : 1;
582
583 struct partial_die_info *find_partial_die (sect_offset sect_off);
584
585 /* If this CU was inherited by another CU (via specification,
586 abstract_origin, etc), this is the ancestor CU. */
587 dwarf2_cu *ancestor;
588
589 /* Get the buildsym_compunit for this CU. */
590 buildsym_compunit *get_builder ()
591 {
592 /* If this CU has a builder associated with it, use that. */
593 if (m_builder != nullptr)
594 return m_builder.get ();
595
596 /* Otherwise, search ancestors for a valid builder. */
597 if (ancestor != nullptr)
598 return ancestor->get_builder ();
599
600 return nullptr;
601 }
602 };
603
604 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
605 This includes type_unit_group and quick_file_names. */
606
607 struct stmt_list_hash
608 {
609 /* The DWO unit this table is from or NULL if there is none. */
610 struct dwo_unit *dwo_unit;
611
612 /* Offset in .debug_line or .debug_line.dwo. */
613 sect_offset line_sect_off;
614 };
615
616 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
617 an object of this type. */
618
619 struct type_unit_group
620 {
621 /* dwarf2read.c's main "handle" on a TU symtab.
622 To simplify things we create an artificial CU that "includes" all the
623 type units using this stmt_list so that the rest of the code still has
624 a "per_cu" handle on the symtab. */
625 struct dwarf2_per_cu_data per_cu;
626
627 /* The TUs that share this DW_AT_stmt_list entry.
628 This is added to while parsing type units to build partial symtabs,
629 and is deleted afterwards and not used again. */
630 std::vector<signatured_type *> *tus;
631
632 /* The compunit symtab.
633 Type units in a group needn't all be defined in the same source file,
634 so we create an essentially anonymous symtab as the compunit symtab. */
635 struct compunit_symtab *compunit_symtab;
636
637 /* The data used to construct the hash key. */
638 struct stmt_list_hash hash;
639
640 /* The symbol tables for this TU (obtained from the files listed in
641 DW_AT_stmt_list).
642 WARNING: The order of entries here must match the order of entries
643 in the line header. After the first TU using this type_unit_group, the
644 line header for the subsequent TUs is recreated from this. This is done
645 because we need to use the same symtabs for each TU using the same
646 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
647 there's no guarantee the line header doesn't have duplicate entries. */
648 struct symtab **symtabs;
649 };
650
651 /* These sections are what may appear in a (real or virtual) DWO file. */
652
653 struct dwo_sections
654 {
655 struct dwarf2_section_info abbrev;
656 struct dwarf2_section_info line;
657 struct dwarf2_section_info loc;
658 struct dwarf2_section_info loclists;
659 struct dwarf2_section_info macinfo;
660 struct dwarf2_section_info macro;
661 struct dwarf2_section_info str;
662 struct dwarf2_section_info str_offsets;
663 /* In the case of a virtual DWO file, these two are unused. */
664 struct dwarf2_section_info info;
665 std::vector<dwarf2_section_info> types;
666 };
667
668 /* CUs/TUs in DWP/DWO files. */
669
670 struct dwo_unit
671 {
672 /* Backlink to the containing struct dwo_file. */
673 struct dwo_file *dwo_file;
674
675 /* The "id" that distinguishes this CU/TU.
676 .debug_info calls this "dwo_id", .debug_types calls this "signature".
677 Since signatures came first, we stick with it for consistency. */
678 ULONGEST signature;
679
680 /* The section this CU/TU lives in, in the DWO file. */
681 struct dwarf2_section_info *section;
682
683 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
684 sect_offset sect_off;
685 unsigned int length;
686
687 /* For types, offset in the type's DIE of the type defined by this TU. */
688 cu_offset type_offset_in_tu;
689 };
690
691 /* include/dwarf2.h defines the DWP section codes.
692 It defines a max value but it doesn't define a min value, which we
693 use for error checking, so provide one. */
694
695 enum dwp_v2_section_ids
696 {
697 DW_SECT_MIN = 1
698 };
699
700 /* Data for one DWO file.
701
702 This includes virtual DWO files (a virtual DWO file is a DWO file as it
703 appears in a DWP file). DWP files don't really have DWO files per se -
704 comdat folding of types "loses" the DWO file they came from, and from
705 a high level view DWP files appear to contain a mass of random types.
706 However, to maintain consistency with the non-DWP case we pretend DWP
707 files contain virtual DWO files, and we assign each TU with one virtual
708 DWO file (generally based on the line and abbrev section offsets -
709 a heuristic that seems to work in practice). */
710
711 struct dwo_file
712 {
713 dwo_file () = default;
714 DISABLE_COPY_AND_ASSIGN (dwo_file);
715
716 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
717 For virtual DWO files the name is constructed from the section offsets
718 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
719 from related CU+TUs. */
720 const char *dwo_name = nullptr;
721
722 /* The DW_AT_comp_dir attribute. */
723 const char *comp_dir = nullptr;
724
725 /* The bfd, when the file is open. Otherwise this is NULL.
726 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
727 gdb_bfd_ref_ptr dbfd;
728
729 /* The sections that make up this DWO file.
730 Remember that for virtual DWO files in DWP V2, these are virtual
731 sections (for lack of a better name). */
732 struct dwo_sections sections {};
733
734 /* The CUs in the file.
735 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
736 an extension to handle LLVM's Link Time Optimization output (where
737 multiple source files may be compiled into a single object/dwo pair). */
738 htab_up cus;
739
740 /* Table of TUs in the file.
741 Each element is a struct dwo_unit. */
742 htab_up tus;
743 };
744
745 /* These sections are what may appear in a DWP file. */
746
747 struct dwp_sections
748 {
749 /* These are used by both DWP version 1 and 2. */
750 struct dwarf2_section_info str;
751 struct dwarf2_section_info cu_index;
752 struct dwarf2_section_info tu_index;
753
754 /* These are only used by DWP version 2 files.
755 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
756 sections are referenced by section number, and are not recorded here.
757 In DWP version 2 there is at most one copy of all these sections, each
758 section being (effectively) comprised of the concatenation of all of the
759 individual sections that exist in the version 1 format.
760 To keep the code simple we treat each of these concatenated pieces as a
761 section itself (a virtual section?). */
762 struct dwarf2_section_info abbrev;
763 struct dwarf2_section_info info;
764 struct dwarf2_section_info line;
765 struct dwarf2_section_info loc;
766 struct dwarf2_section_info macinfo;
767 struct dwarf2_section_info macro;
768 struct dwarf2_section_info str_offsets;
769 struct dwarf2_section_info types;
770 };
771
772 /* These sections are what may appear in a virtual DWO file in DWP version 1.
773 A virtual DWO file is a DWO file as it appears in a DWP file. */
774
775 struct virtual_v1_dwo_sections
776 {
777 struct dwarf2_section_info abbrev;
778 struct dwarf2_section_info line;
779 struct dwarf2_section_info loc;
780 struct dwarf2_section_info macinfo;
781 struct dwarf2_section_info macro;
782 struct dwarf2_section_info str_offsets;
783 /* Each DWP hash table entry records one CU or one TU.
784 That is recorded here, and copied to dwo_unit.section. */
785 struct dwarf2_section_info info_or_types;
786 };
787
788 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
789 In version 2, the sections of the DWO files are concatenated together
790 and stored in one section of that name. Thus each ELF section contains
791 several "virtual" sections. */
792
793 struct virtual_v2_dwo_sections
794 {
795 bfd_size_type abbrev_offset;
796 bfd_size_type abbrev_size;
797
798 bfd_size_type line_offset;
799 bfd_size_type line_size;
800
801 bfd_size_type loc_offset;
802 bfd_size_type loc_size;
803
804 bfd_size_type macinfo_offset;
805 bfd_size_type macinfo_size;
806
807 bfd_size_type macro_offset;
808 bfd_size_type macro_size;
809
810 bfd_size_type str_offsets_offset;
811 bfd_size_type str_offsets_size;
812
813 /* Each DWP hash table entry records one CU or one TU.
814 That is recorded here, and copied to dwo_unit.section. */
815 bfd_size_type info_or_types_offset;
816 bfd_size_type info_or_types_size;
817 };
818
819 /* Contents of DWP hash tables. */
820
821 struct dwp_hash_table
822 {
823 uint32_t version, nr_columns;
824 uint32_t nr_units, nr_slots;
825 const gdb_byte *hash_table, *unit_table;
826 union
827 {
828 struct
829 {
830 const gdb_byte *indices;
831 } v1;
832 struct
833 {
834 /* This is indexed by column number and gives the id of the section
835 in that column. */
836 #define MAX_NR_V2_DWO_SECTIONS \
837 (1 /* .debug_info or .debug_types */ \
838 + 1 /* .debug_abbrev */ \
839 + 1 /* .debug_line */ \
840 + 1 /* .debug_loc */ \
841 + 1 /* .debug_str_offsets */ \
842 + 1 /* .debug_macro or .debug_macinfo */)
843 int section_ids[MAX_NR_V2_DWO_SECTIONS];
844 const gdb_byte *offsets;
845 const gdb_byte *sizes;
846 } v2;
847 } section_pool;
848 };
849
850 /* Data for one DWP file. */
851
852 struct dwp_file
853 {
854 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
855 : name (name_),
856 dbfd (std::move (abfd))
857 {
858 }
859
860 /* Name of the file. */
861 const char *name;
862
863 /* File format version. */
864 int version = 0;
865
866 /* The bfd. */
867 gdb_bfd_ref_ptr dbfd;
868
869 /* Section info for this file. */
870 struct dwp_sections sections {};
871
872 /* Table of CUs in the file. */
873 const struct dwp_hash_table *cus = nullptr;
874
875 /* Table of TUs in the file. */
876 const struct dwp_hash_table *tus = nullptr;
877
878 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
879 htab_up loaded_cus;
880 htab_up loaded_tus;
881
882 /* Table to map ELF section numbers to their sections.
883 This is only needed for the DWP V1 file format. */
884 unsigned int num_sections = 0;
885 asection **elf_sections = nullptr;
886 };
887
888 /* Struct used to pass misc. parameters to read_die_and_children, et
889 al. which are used for both .debug_info and .debug_types dies.
890 All parameters here are unchanging for the life of the call. This
891 struct exists to abstract away the constant parameters of die reading. */
892
893 struct die_reader_specs
894 {
895 /* The bfd of die_section. */
896 bfd* abfd;
897
898 /* The CU of the DIE we are parsing. */
899 struct dwarf2_cu *cu;
900
901 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
902 struct dwo_file *dwo_file;
903
904 /* The section the die comes from.
905 This is either .debug_info or .debug_types, or the .dwo variants. */
906 struct dwarf2_section_info *die_section;
907
908 /* die_section->buffer. */
909 const gdb_byte *buffer;
910
911 /* The end of the buffer. */
912 const gdb_byte *buffer_end;
913
914 /* The abbreviation table to use when reading the DIEs. */
915 struct abbrev_table *abbrev_table;
916 };
917
918 /* A subclass of die_reader_specs that holds storage and has complex
919 constructor and destructor behavior. */
920
921 class cutu_reader : public die_reader_specs
922 {
923 public:
924
925 cutu_reader (dwarf2_per_cu_data *this_cu,
926 dwarf2_per_objfile *per_objfile,
927 struct abbrev_table *abbrev_table,
928 int use_existing_cu,
929 bool skip_partial);
930
931 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
932 dwarf2_per_objfile *per_objfile,
933 struct dwarf2_cu *parent_cu = nullptr,
934 struct dwo_file *dwo_file = nullptr);
935
936 DISABLE_COPY_AND_ASSIGN (cutu_reader);
937
938 const gdb_byte *info_ptr = nullptr;
939 struct die_info *comp_unit_die = nullptr;
940 bool dummy_p = false;
941
942 /* Release the new CU, putting it on the chain. This cannot be done
943 for dummy CUs. */
944 void keep ();
945
946 private:
947 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
948 dwarf2_per_objfile *per_objfile,
949 int use_existing_cu);
950
951 struct dwarf2_per_cu_data *m_this_cu;
952 std::unique_ptr<dwarf2_cu> m_new_cu;
953
954 /* The ordinary abbreviation table. */
955 abbrev_table_up m_abbrev_table_holder;
956
957 /* The DWO abbreviation table. */
958 abbrev_table_up m_dwo_abbrev_table;
959 };
960
961 /* When we construct a partial symbol table entry we only
962 need this much information. */
963 struct partial_die_info : public allocate_on_obstack
964 {
965 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
966
967 /* Disable assign but still keep copy ctor, which is needed
968 load_partial_dies. */
969 partial_die_info& operator=(const partial_die_info& rhs) = delete;
970
971 /* Adjust the partial die before generating a symbol for it. This
972 function may set the is_external flag or change the DIE's
973 name. */
974 void fixup (struct dwarf2_cu *cu);
975
976 /* Read a minimal amount of information into the minimal die
977 structure. */
978 const gdb_byte *read (const struct die_reader_specs *reader,
979 const struct abbrev_info &abbrev,
980 const gdb_byte *info_ptr);
981
982 /* Offset of this DIE. */
983 const sect_offset sect_off;
984
985 /* DWARF-2 tag for this DIE. */
986 const ENUM_BITFIELD(dwarf_tag) tag : 16;
987
988 /* Assorted flags describing the data found in this DIE. */
989 const unsigned int has_children : 1;
990
991 unsigned int is_external : 1;
992 unsigned int is_declaration : 1;
993 unsigned int has_type : 1;
994 unsigned int has_specification : 1;
995 unsigned int has_pc_info : 1;
996 unsigned int may_be_inlined : 1;
997
998 /* This DIE has been marked DW_AT_main_subprogram. */
999 unsigned int main_subprogram : 1;
1000
1001 /* Flag set if the SCOPE field of this structure has been
1002 computed. */
1003 unsigned int scope_set : 1;
1004
1005 /* Flag set if the DIE has a byte_size attribute. */
1006 unsigned int has_byte_size : 1;
1007
1008 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1009 unsigned int has_const_value : 1;
1010
1011 /* Flag set if any of the DIE's children are template arguments. */
1012 unsigned int has_template_arguments : 1;
1013
1014 /* Flag set if fixup has been called on this die. */
1015 unsigned int fixup_called : 1;
1016
1017 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1018 unsigned int is_dwz : 1;
1019
1020 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1021 unsigned int spec_is_dwz : 1;
1022
1023 /* The name of this DIE. Normally the value of DW_AT_name, but
1024 sometimes a default name for unnamed DIEs. */
1025 const char *name = nullptr;
1026
1027 /* The linkage name, if present. */
1028 const char *linkage_name = nullptr;
1029
1030 /* The scope to prepend to our children. This is generally
1031 allocated on the comp_unit_obstack, so will disappear
1032 when this compilation unit leaves the cache. */
1033 const char *scope = nullptr;
1034
1035 /* Some data associated with the partial DIE. The tag determines
1036 which field is live. */
1037 union
1038 {
1039 /* The location description associated with this DIE, if any. */
1040 struct dwarf_block *locdesc;
1041 /* The offset of an import, for DW_TAG_imported_unit. */
1042 sect_offset sect_off;
1043 } d {};
1044
1045 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1046 CORE_ADDR lowpc = 0;
1047 CORE_ADDR highpc = 0;
1048
1049 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1050 DW_AT_sibling, if any. */
1051 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1052 could return DW_AT_sibling values to its caller load_partial_dies. */
1053 const gdb_byte *sibling = nullptr;
1054
1055 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1056 DW_AT_specification (or DW_AT_abstract_origin or
1057 DW_AT_extension). */
1058 sect_offset spec_offset {};
1059
1060 /* Pointers to this DIE's parent, first child, and next sibling,
1061 if any. */
1062 struct partial_die_info *die_parent = nullptr;
1063 struct partial_die_info *die_child = nullptr;
1064 struct partial_die_info *die_sibling = nullptr;
1065
1066 friend struct partial_die_info *
1067 dwarf2_cu::find_partial_die (sect_offset sect_off);
1068
1069 private:
1070 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1071 partial_die_info (sect_offset sect_off)
1072 : partial_die_info (sect_off, DW_TAG_padding, 0)
1073 {
1074 }
1075
1076 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1077 int has_children_)
1078 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1079 {
1080 is_external = 0;
1081 is_declaration = 0;
1082 has_type = 0;
1083 has_specification = 0;
1084 has_pc_info = 0;
1085 may_be_inlined = 0;
1086 main_subprogram = 0;
1087 scope_set = 0;
1088 has_byte_size = 0;
1089 has_const_value = 0;
1090 has_template_arguments = 0;
1091 fixup_called = 0;
1092 is_dwz = 0;
1093 spec_is_dwz = 0;
1094 }
1095 };
1096
1097 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1098 but this would require a corresponding change in unpack_field_as_long
1099 and friends. */
1100 static int bits_per_byte = 8;
1101
1102 struct variant_part_builder;
1103
1104 /* When reading a variant, we track a bit more information about the
1105 field, and store it in an object of this type. */
1106
1107 struct variant_field
1108 {
1109 int first_field = -1;
1110 int last_field = -1;
1111
1112 /* A variant can contain other variant parts. */
1113 std::vector<variant_part_builder> variant_parts;
1114
1115 /* If we see a DW_TAG_variant, then this will be set if this is the
1116 default branch. */
1117 bool default_branch = false;
1118 /* If we see a DW_AT_discr_value, then this will be the discriminant
1119 value. */
1120 ULONGEST discriminant_value = 0;
1121 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1122 data. */
1123 struct dwarf_block *discr_list_data = nullptr;
1124 };
1125
1126 /* This represents a DW_TAG_variant_part. */
1127
1128 struct variant_part_builder
1129 {
1130 /* The offset of the discriminant field. */
1131 sect_offset discriminant_offset {};
1132
1133 /* Variants that are direct children of this variant part. */
1134 std::vector<variant_field> variants;
1135
1136 /* True if we're currently reading a variant. */
1137 bool processing_variant = false;
1138 };
1139
1140 struct nextfield
1141 {
1142 int accessibility = 0;
1143 int virtuality = 0;
1144 /* Variant parts need to find the discriminant, which is a DIE
1145 reference. We track the section offset of each field to make
1146 this link. */
1147 sect_offset offset;
1148 struct field field {};
1149 };
1150
1151 struct fnfieldlist
1152 {
1153 const char *name = nullptr;
1154 std::vector<struct fn_field> fnfields;
1155 };
1156
1157 /* The routines that read and process dies for a C struct or C++ class
1158 pass lists of data member fields and lists of member function fields
1159 in an instance of a field_info structure, as defined below. */
1160 struct field_info
1161 {
1162 /* List of data member and baseclasses fields. */
1163 std::vector<struct nextfield> fields;
1164 std::vector<struct nextfield> baseclasses;
1165
1166 /* Set if the accessibility of one of the fields is not public. */
1167 int non_public_fields = 0;
1168
1169 /* Member function fieldlist array, contains name of possibly overloaded
1170 member function, number of overloaded member functions and a pointer
1171 to the head of the member function field chain. */
1172 std::vector<struct fnfieldlist> fnfieldlists;
1173
1174 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1175 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1176 std::vector<struct decl_field> typedef_field_list;
1177
1178 /* Nested types defined by this class and the number of elements in this
1179 list. */
1180 std::vector<struct decl_field> nested_types_list;
1181
1182 /* If non-null, this is the variant part we are currently
1183 reading. */
1184 variant_part_builder *current_variant_part = nullptr;
1185 /* This holds all the top-level variant parts attached to the type
1186 we're reading. */
1187 std::vector<variant_part_builder> variant_parts;
1188
1189 /* Return the total number of fields (including baseclasses). */
1190 int nfields () const
1191 {
1192 return fields.size () + baseclasses.size ();
1193 }
1194 };
1195
1196 /* Loaded secondary compilation units are kept in memory until they
1197 have not been referenced for the processing of this many
1198 compilation units. Set this to zero to disable caching. Cache
1199 sizes of up to at least twenty will improve startup time for
1200 typical inter-CU-reference binaries, at an obvious memory cost. */
1201 static int dwarf_max_cache_age = 5;
1202 static void
1203 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1204 struct cmd_list_element *c, const char *value)
1205 {
1206 fprintf_filtered (file, _("The upper bound on the age of cached "
1207 "DWARF compilation units is %s.\n"),
1208 value);
1209 }
1210 \f
1211 /* local function prototypes */
1212
1213 static void dwarf2_find_base_address (struct die_info *die,
1214 struct dwarf2_cu *cu);
1215
1216 static dwarf2_psymtab *create_partial_symtab
1217 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1218 const char *name);
1219
1220 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1221 const gdb_byte *info_ptr,
1222 struct die_info *type_unit_die);
1223
1224 static void dwarf2_build_psymtabs_hard
1225 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1226
1227 static void scan_partial_symbols (struct partial_die_info *,
1228 CORE_ADDR *, CORE_ADDR *,
1229 int, struct dwarf2_cu *);
1230
1231 static void add_partial_symbol (struct partial_die_info *,
1232 struct dwarf2_cu *);
1233
1234 static void add_partial_namespace (struct partial_die_info *pdi,
1235 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1236 int set_addrmap, struct dwarf2_cu *cu);
1237
1238 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1239 CORE_ADDR *highpc, int set_addrmap,
1240 struct dwarf2_cu *cu);
1241
1242 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1243 struct dwarf2_cu *cu);
1244
1245 static void add_partial_subprogram (struct partial_die_info *pdi,
1246 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1247 int need_pc, struct dwarf2_cu *cu);
1248
1249 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1250
1251 static struct partial_die_info *load_partial_dies
1252 (const struct die_reader_specs *, const gdb_byte *, int);
1253
1254 /* A pair of partial_die_info and compilation unit. */
1255 struct cu_partial_die_info
1256 {
1257 /* The compilation unit of the partial_die_info. */
1258 struct dwarf2_cu *cu;
1259 /* A partial_die_info. */
1260 struct partial_die_info *pdi;
1261
1262 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1263 : cu (cu),
1264 pdi (pdi)
1265 { /* Nothing. */ }
1266
1267 private:
1268 cu_partial_die_info () = delete;
1269 };
1270
1271 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1272 struct dwarf2_cu *);
1273
1274 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1275 struct attribute *, struct attr_abbrev *,
1276 const gdb_byte *, bool *need_reprocess);
1277
1278 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1279 struct attribute *attr);
1280
1281 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1282
1283 static sect_offset read_abbrev_offset
1284 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1285 struct dwarf2_section_info *, sect_offset);
1286
1287 static const char *read_indirect_string
1288 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1289 const struct comp_unit_head *, unsigned int *);
1290
1291 static const char *read_indirect_string_at_offset
1292 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1293
1294 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1295 const gdb_byte *,
1296 unsigned int *);
1297
1298 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1299 ULONGEST str_index);
1300
1301 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1302 ULONGEST str_index);
1303
1304 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1305
1306 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1307 struct dwarf2_cu *);
1308
1309 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1310 struct dwarf2_cu *cu);
1311
1312 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1313
1314 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1315 struct dwarf2_cu *cu);
1316
1317 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1318
1319 static struct die_info *die_specification (struct die_info *die,
1320 struct dwarf2_cu **);
1321
1322 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1323 struct dwarf2_cu *cu);
1324
1325 static void dwarf_decode_lines (struct line_header *, const char *,
1326 struct dwarf2_cu *, dwarf2_psymtab *,
1327 CORE_ADDR, int decode_mapping);
1328
1329 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1330 const char *);
1331
1332 static struct symbol *new_symbol (struct die_info *, struct type *,
1333 struct dwarf2_cu *, struct symbol * = NULL);
1334
1335 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1336 struct dwarf2_cu *);
1337
1338 static void dwarf2_const_value_attr (const struct attribute *attr,
1339 struct type *type,
1340 const char *name,
1341 struct obstack *obstack,
1342 struct dwarf2_cu *cu, LONGEST *value,
1343 const gdb_byte **bytes,
1344 struct dwarf2_locexpr_baton **baton);
1345
1346 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1347
1348 static int need_gnat_info (struct dwarf2_cu *);
1349
1350 static struct type *die_descriptive_type (struct die_info *,
1351 struct dwarf2_cu *);
1352
1353 static void set_descriptive_type (struct type *, struct die_info *,
1354 struct dwarf2_cu *);
1355
1356 static struct type *die_containing_type (struct die_info *,
1357 struct dwarf2_cu *);
1358
1359 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1360 struct dwarf2_cu *);
1361
1362 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1363
1364 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1365
1366 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1367
1368 static char *typename_concat (struct obstack *obs, const char *prefix,
1369 const char *suffix, int physname,
1370 struct dwarf2_cu *cu);
1371
1372 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1373
1374 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1375
1376 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1377
1378 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1379
1380 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1381
1382 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1383
1384 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1385 struct dwarf2_cu *, dwarf2_psymtab *);
1386
1387 /* Return the .debug_loclists section to use for cu. */
1388 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1389
1390 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1391 values. Keep the items ordered with increasing constraints compliance. */
1392 enum pc_bounds_kind
1393 {
1394 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1395 PC_BOUNDS_NOT_PRESENT,
1396
1397 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1398 were present but they do not form a valid range of PC addresses. */
1399 PC_BOUNDS_INVALID,
1400
1401 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1402 PC_BOUNDS_RANGES,
1403
1404 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1405 PC_BOUNDS_HIGH_LOW,
1406 };
1407
1408 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1409 CORE_ADDR *, CORE_ADDR *,
1410 struct dwarf2_cu *,
1411 dwarf2_psymtab *);
1412
1413 static void get_scope_pc_bounds (struct die_info *,
1414 CORE_ADDR *, CORE_ADDR *,
1415 struct dwarf2_cu *);
1416
1417 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1418 CORE_ADDR, struct dwarf2_cu *);
1419
1420 static void dwarf2_add_field (struct field_info *, struct die_info *,
1421 struct dwarf2_cu *);
1422
1423 static void dwarf2_attach_fields_to_type (struct field_info *,
1424 struct type *, struct dwarf2_cu *);
1425
1426 static void dwarf2_add_member_fn (struct field_info *,
1427 struct die_info *, struct type *,
1428 struct dwarf2_cu *);
1429
1430 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1431 struct type *,
1432 struct dwarf2_cu *);
1433
1434 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1435
1436 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1437
1438 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1439
1440 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1441
1442 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1443
1444 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1445
1446 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1447
1448 static struct type *read_module_type (struct die_info *die,
1449 struct dwarf2_cu *cu);
1450
1451 static const char *namespace_name (struct die_info *die,
1452 int *is_anonymous, struct dwarf2_cu *);
1453
1454 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1455
1456 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1457 bool * = nullptr);
1458
1459 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1460 struct dwarf2_cu *);
1461
1462 static struct die_info *read_die_and_siblings_1
1463 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1464 struct die_info *);
1465
1466 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1467 const gdb_byte *info_ptr,
1468 const gdb_byte **new_info_ptr,
1469 struct die_info *parent);
1470
1471 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1472 struct die_info **, const gdb_byte *,
1473 int);
1474
1475 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1476 struct die_info **, const gdb_byte *);
1477
1478 static void process_die (struct die_info *, struct dwarf2_cu *);
1479
1480 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1481 struct objfile *);
1482
1483 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1484
1485 static const char *dwarf2_full_name (const char *name,
1486 struct die_info *die,
1487 struct dwarf2_cu *cu);
1488
1489 static const char *dwarf2_physname (const char *name, struct die_info *die,
1490 struct dwarf2_cu *cu);
1491
1492 static struct die_info *dwarf2_extension (struct die_info *die,
1493 struct dwarf2_cu **);
1494
1495 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1496
1497 static void dump_die_for_error (struct die_info *);
1498
1499 static void dump_die_1 (struct ui_file *, int level, int max_level,
1500 struct die_info *);
1501
1502 /*static*/ void dump_die (struct die_info *, int max_level);
1503
1504 static void store_in_ref_table (struct die_info *,
1505 struct dwarf2_cu *);
1506
1507 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1508 const struct attribute *,
1509 struct dwarf2_cu **);
1510
1511 static struct die_info *follow_die_ref (struct die_info *,
1512 const struct attribute *,
1513 struct dwarf2_cu **);
1514
1515 static struct die_info *follow_die_sig (struct die_info *,
1516 const struct attribute *,
1517 struct dwarf2_cu **);
1518
1519 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1520 struct dwarf2_cu *);
1521
1522 static struct type *get_DW_AT_signature_type (struct die_info *,
1523 const struct attribute *,
1524 struct dwarf2_cu *);
1525
1526 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1527 dwarf2_per_objfile *per_objfile);
1528
1529 static void read_signatured_type (signatured_type *sig_type,
1530 dwarf2_per_objfile *per_objfile);
1531
1532 static int attr_to_dynamic_prop (const struct attribute *attr,
1533 struct die_info *die, struct dwarf2_cu *cu,
1534 struct dynamic_prop *prop, struct type *type);
1535
1536 /* memory allocation interface */
1537
1538 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1539
1540 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1541
1542 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1543
1544 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1545 struct dwarf2_loclist_baton *baton,
1546 const struct attribute *attr);
1547
1548 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1549 struct symbol *sym,
1550 struct dwarf2_cu *cu,
1551 int is_block);
1552
1553 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1554 const gdb_byte *info_ptr,
1555 struct abbrev_info *abbrev);
1556
1557 static hashval_t partial_die_hash (const void *item);
1558
1559 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1560
1561 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1562 (sect_offset sect_off, unsigned int offset_in_dwz,
1563 struct dwarf2_per_objfile *dwarf2_per_objfile);
1564
1565 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1566 struct die_info *comp_unit_die,
1567 enum language pretend_language);
1568
1569 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1570
1571 static void free_one_cached_comp_unit (dwarf2_per_cu_data *target_per_cu,
1572 dwarf2_per_objfile *per_objfile);
1573
1574 static struct type *set_die_type (struct die_info *, struct type *,
1575 struct dwarf2_cu *);
1576
1577 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1578
1579 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1580
1581 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1582 dwarf2_per_objfile *per_objfile,
1583 bool skip_partial,
1584 enum language pretend_language);
1585
1586 static void process_full_comp_unit (dwarf2_per_cu_data *per_cu,
1587 dwarf2_per_objfile *per_objfile,
1588 enum language pretend_language);
1589
1590 static void process_full_type_unit (dwarf2_per_cu_data *per_cu,
1591 dwarf2_per_objfile *per_objfile,
1592 enum language pretend_language);
1593
1594 static void dwarf2_add_dependence (struct dwarf2_cu *,
1595 struct dwarf2_per_cu_data *);
1596
1597 static void dwarf2_mark (struct dwarf2_cu *);
1598
1599 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1600
1601 static struct type *get_die_type_at_offset (sect_offset,
1602 dwarf2_per_cu_data *per_cu,
1603 dwarf2_per_objfile *per_objfile);
1604
1605 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1606
1607 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1608 dwarf2_per_objfile *per_objfile,
1609 enum language pretend_language);
1610
1611 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1612
1613 /* Class, the destructor of which frees all allocated queue entries. This
1614 will only have work to do if an error was thrown while processing the
1615 dwarf. If no error was thrown then the queue entries should have all
1616 been processed, and freed, as we went along. */
1617
1618 class dwarf2_queue_guard
1619 {
1620 public:
1621 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1622 : m_per_objfile (per_objfile)
1623 {
1624 }
1625
1626 /* Free any entries remaining on the queue. There should only be
1627 entries left if we hit an error while processing the dwarf. */
1628 ~dwarf2_queue_guard ()
1629 {
1630 /* Ensure that no memory is allocated by the queue. */
1631 std::queue<dwarf2_queue_item> empty;
1632 std::swap (m_per_objfile->per_bfd->queue, empty);
1633 }
1634
1635 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1636
1637 private:
1638 dwarf2_per_objfile *m_per_objfile;
1639 };
1640
1641 dwarf2_queue_item::~dwarf2_queue_item ()
1642 {
1643 /* Anything still marked queued is likely to be in an
1644 inconsistent state, so discard it. */
1645 if (per_cu->queued)
1646 {
1647 if (per_cu->cu != NULL)
1648 free_one_cached_comp_unit (per_cu, per_objfile);
1649 per_cu->queued = 0;
1650 }
1651 }
1652
1653 /* The return type of find_file_and_directory. Note, the enclosed
1654 string pointers are only valid while this object is valid. */
1655
1656 struct file_and_directory
1657 {
1658 /* The filename. This is never NULL. */
1659 const char *name;
1660
1661 /* The compilation directory. NULL if not known. If we needed to
1662 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1663 points directly to the DW_AT_comp_dir string attribute owned by
1664 the obstack that owns the DIE. */
1665 const char *comp_dir;
1666
1667 /* If we needed to build a new string for comp_dir, this is what
1668 owns the storage. */
1669 std::string comp_dir_storage;
1670 };
1671
1672 static file_and_directory find_file_and_directory (struct die_info *die,
1673 struct dwarf2_cu *cu);
1674
1675 static htab_up allocate_signatured_type_table ();
1676
1677 static htab_up allocate_dwo_unit_table ();
1678
1679 static struct dwo_unit *lookup_dwo_unit_in_dwp
1680 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1681 struct dwp_file *dwp_file, const char *comp_dir,
1682 ULONGEST signature, int is_debug_types);
1683
1684 static struct dwp_file *get_dwp_file
1685 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1686
1687 static struct dwo_unit *lookup_dwo_comp_unit
1688 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1689 ULONGEST signature);
1690
1691 static struct dwo_unit *lookup_dwo_type_unit
1692 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1693
1694 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1695
1696 /* A unique pointer to a dwo_file. */
1697
1698 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1699
1700 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1701
1702 static void check_producer (struct dwarf2_cu *cu);
1703
1704 static void free_line_header_voidp (void *arg);
1705 \f
1706 /* Various complaints about symbol reading that don't abort the process. */
1707
1708 static void
1709 dwarf2_debug_line_missing_file_complaint (void)
1710 {
1711 complaint (_(".debug_line section has line data without a file"));
1712 }
1713
1714 static void
1715 dwarf2_debug_line_missing_end_sequence_complaint (void)
1716 {
1717 complaint (_(".debug_line section has line "
1718 "program sequence without an end"));
1719 }
1720
1721 static void
1722 dwarf2_complex_location_expr_complaint (void)
1723 {
1724 complaint (_("location expression too complex"));
1725 }
1726
1727 static void
1728 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1729 int arg3)
1730 {
1731 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1732 arg1, arg2, arg3);
1733 }
1734
1735 static void
1736 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1737 {
1738 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1739 arg1, arg2);
1740 }
1741
1742 /* Hash function for line_header_hash. */
1743
1744 static hashval_t
1745 line_header_hash (const struct line_header *ofs)
1746 {
1747 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1748 }
1749
1750 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1751
1752 static hashval_t
1753 line_header_hash_voidp (const void *item)
1754 {
1755 const struct line_header *ofs = (const struct line_header *) item;
1756
1757 return line_header_hash (ofs);
1758 }
1759
1760 /* Equality function for line_header_hash. */
1761
1762 static int
1763 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1764 {
1765 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1766 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1767
1768 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1769 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1770 }
1771
1772 \f
1773
1774 /* See declaration. */
1775
1776 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1777 bool can_copy_)
1778 : obfd (obfd),
1779 can_copy (can_copy_)
1780 {
1781 if (names == NULL)
1782 names = &dwarf2_elf_names;
1783
1784 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1785 locate_sections (obfd, sec, *names);
1786 }
1787
1788 dwarf2_per_bfd::~dwarf2_per_bfd ()
1789 {
1790 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1791 free_cached_comp_units ();
1792
1793 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1794 per_cu->imported_symtabs_free ();
1795
1796 for (signatured_type *sig_type : all_type_units)
1797 sig_type->per_cu.imported_symtabs_free ();
1798
1799 /* Everything else should be on this->obstack. */
1800 }
1801
1802 /* See declaration. */
1803
1804 void
1805 dwarf2_per_bfd::free_cached_comp_units ()
1806 {
1807 dwarf2_per_cu_data *per_cu = read_in_chain;
1808 dwarf2_per_cu_data **last_chain = &read_in_chain;
1809 while (per_cu != NULL)
1810 {
1811 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1812
1813 delete per_cu->cu;
1814 *last_chain = next_cu;
1815 per_cu = next_cu;
1816 }
1817 }
1818
1819 /* A helper class that calls free_cached_comp_units on
1820 destruction. */
1821
1822 class free_cached_comp_units
1823 {
1824 public:
1825
1826 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1827 : m_per_objfile (per_objfile)
1828 {
1829 }
1830
1831 ~free_cached_comp_units ()
1832 {
1833 m_per_objfile->per_bfd->free_cached_comp_units ();
1834 }
1835
1836 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1837
1838 private:
1839
1840 dwarf2_per_objfile *m_per_objfile;
1841 };
1842
1843 /* See read.h. */
1844
1845 bool
1846 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1847 {
1848 gdb_assert (per_cu->index < this->m_symtabs.size ());
1849
1850 return this->m_symtabs[per_cu->index] != nullptr;
1851 }
1852
1853 /* See read.h. */
1854
1855 compunit_symtab *
1856 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1857 {
1858 gdb_assert (per_cu->index < this->m_symtabs.size ());
1859
1860 return this->m_symtabs[per_cu->index];
1861 }
1862
1863 /* See read.h. */
1864
1865 void
1866 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1867 compunit_symtab *symtab)
1868 {
1869 gdb_assert (per_cu->index < this->m_symtabs.size ());
1870 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1871
1872 this->m_symtabs[per_cu->index] = symtab;
1873 }
1874
1875 /* Try to locate the sections we need for DWARF 2 debugging
1876 information and return true if we have enough to do something.
1877 NAMES points to the dwarf2 section names, or is NULL if the standard
1878 ELF names are used. CAN_COPY is true for formats where symbol
1879 interposition is possible and so symbol values must follow copy
1880 relocation rules. */
1881
1882 int
1883 dwarf2_has_info (struct objfile *objfile,
1884 const struct dwarf2_debug_sections *names,
1885 bool can_copy)
1886 {
1887 if (objfile->flags & OBJF_READNEVER)
1888 return 0;
1889
1890 struct dwarf2_per_objfile *dwarf2_per_objfile
1891 = get_dwarf2_per_objfile (objfile);
1892
1893 if (dwarf2_per_objfile == NULL)
1894 {
1895 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1896 sharing yet). */
1897 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1898
1899 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1900 }
1901
1902 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1903 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1904 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1905 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
1906 }
1907
1908 /* When loading sections, we look either for uncompressed section or for
1909 compressed section names. */
1910
1911 static int
1912 section_is_p (const char *section_name,
1913 const struct dwarf2_section_names *names)
1914 {
1915 if (names->normal != NULL
1916 && strcmp (section_name, names->normal) == 0)
1917 return 1;
1918 if (names->compressed != NULL
1919 && strcmp (section_name, names->compressed) == 0)
1920 return 1;
1921 return 0;
1922 }
1923
1924 /* See declaration. */
1925
1926 void
1927 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1928 const dwarf2_debug_sections &names)
1929 {
1930 flagword aflag = bfd_section_flags (sectp);
1931
1932 if ((aflag & SEC_HAS_CONTENTS) == 0)
1933 {
1934 }
1935 else if (elf_section_data (sectp)->this_hdr.sh_size
1936 > bfd_get_file_size (abfd))
1937 {
1938 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1939 warning (_("Discarding section %s which has a section size (%s"
1940 ") larger than the file size [in module %s]"),
1941 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1942 bfd_get_filename (abfd));
1943 }
1944 else if (section_is_p (sectp->name, &names.info))
1945 {
1946 this->info.s.section = sectp;
1947 this->info.size = bfd_section_size (sectp);
1948 }
1949 else if (section_is_p (sectp->name, &names.abbrev))
1950 {
1951 this->abbrev.s.section = sectp;
1952 this->abbrev.size = bfd_section_size (sectp);
1953 }
1954 else if (section_is_p (sectp->name, &names.line))
1955 {
1956 this->line.s.section = sectp;
1957 this->line.size = bfd_section_size (sectp);
1958 }
1959 else if (section_is_p (sectp->name, &names.loc))
1960 {
1961 this->loc.s.section = sectp;
1962 this->loc.size = bfd_section_size (sectp);
1963 }
1964 else if (section_is_p (sectp->name, &names.loclists))
1965 {
1966 this->loclists.s.section = sectp;
1967 this->loclists.size = bfd_section_size (sectp);
1968 }
1969 else if (section_is_p (sectp->name, &names.macinfo))
1970 {
1971 this->macinfo.s.section = sectp;
1972 this->macinfo.size = bfd_section_size (sectp);
1973 }
1974 else if (section_is_p (sectp->name, &names.macro))
1975 {
1976 this->macro.s.section = sectp;
1977 this->macro.size = bfd_section_size (sectp);
1978 }
1979 else if (section_is_p (sectp->name, &names.str))
1980 {
1981 this->str.s.section = sectp;
1982 this->str.size = bfd_section_size (sectp);
1983 }
1984 else if (section_is_p (sectp->name, &names.str_offsets))
1985 {
1986 this->str_offsets.s.section = sectp;
1987 this->str_offsets.size = bfd_section_size (sectp);
1988 }
1989 else if (section_is_p (sectp->name, &names.line_str))
1990 {
1991 this->line_str.s.section = sectp;
1992 this->line_str.size = bfd_section_size (sectp);
1993 }
1994 else if (section_is_p (sectp->name, &names.addr))
1995 {
1996 this->addr.s.section = sectp;
1997 this->addr.size = bfd_section_size (sectp);
1998 }
1999 else if (section_is_p (sectp->name, &names.frame))
2000 {
2001 this->frame.s.section = sectp;
2002 this->frame.size = bfd_section_size (sectp);
2003 }
2004 else if (section_is_p (sectp->name, &names.eh_frame))
2005 {
2006 this->eh_frame.s.section = sectp;
2007 this->eh_frame.size = bfd_section_size (sectp);
2008 }
2009 else if (section_is_p (sectp->name, &names.ranges))
2010 {
2011 this->ranges.s.section = sectp;
2012 this->ranges.size = bfd_section_size (sectp);
2013 }
2014 else if (section_is_p (sectp->name, &names.rnglists))
2015 {
2016 this->rnglists.s.section = sectp;
2017 this->rnglists.size = bfd_section_size (sectp);
2018 }
2019 else if (section_is_p (sectp->name, &names.types))
2020 {
2021 struct dwarf2_section_info type_section;
2022
2023 memset (&type_section, 0, sizeof (type_section));
2024 type_section.s.section = sectp;
2025 type_section.size = bfd_section_size (sectp);
2026
2027 this->types.push_back (type_section);
2028 }
2029 else if (section_is_p (sectp->name, &names.gdb_index))
2030 {
2031 this->gdb_index.s.section = sectp;
2032 this->gdb_index.size = bfd_section_size (sectp);
2033 }
2034 else if (section_is_p (sectp->name, &names.debug_names))
2035 {
2036 this->debug_names.s.section = sectp;
2037 this->debug_names.size = bfd_section_size (sectp);
2038 }
2039 else if (section_is_p (sectp->name, &names.debug_aranges))
2040 {
2041 this->debug_aranges.s.section = sectp;
2042 this->debug_aranges.size = bfd_section_size (sectp);
2043 }
2044
2045 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2046 && bfd_section_vma (sectp) == 0)
2047 this->has_section_at_zero = true;
2048 }
2049
2050 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2051 SECTION_NAME. */
2052
2053 void
2054 dwarf2_get_section_info (struct objfile *objfile,
2055 enum dwarf2_section_enum sect,
2056 asection **sectp, const gdb_byte **bufp,
2057 bfd_size_type *sizep)
2058 {
2059 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2060 struct dwarf2_section_info *info;
2061
2062 /* We may see an objfile without any DWARF, in which case we just
2063 return nothing. */
2064 if (data == NULL)
2065 {
2066 *sectp = NULL;
2067 *bufp = NULL;
2068 *sizep = 0;
2069 return;
2070 }
2071 switch (sect)
2072 {
2073 case DWARF2_DEBUG_FRAME:
2074 info = &data->per_bfd->frame;
2075 break;
2076 case DWARF2_EH_FRAME:
2077 info = &data->per_bfd->eh_frame;
2078 break;
2079 default:
2080 gdb_assert_not_reached ("unexpected section");
2081 }
2082
2083 info->read (objfile);
2084
2085 *sectp = info->get_bfd_section ();
2086 *bufp = info->buffer;
2087 *sizep = info->size;
2088 }
2089
2090 /* A helper function to find the sections for a .dwz file. */
2091
2092 static void
2093 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2094 {
2095 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2096
2097 /* Note that we only support the standard ELF names, because .dwz
2098 is ELF-only (at the time of writing). */
2099 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2100 {
2101 dwz_file->abbrev.s.section = sectp;
2102 dwz_file->abbrev.size = bfd_section_size (sectp);
2103 }
2104 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2105 {
2106 dwz_file->info.s.section = sectp;
2107 dwz_file->info.size = bfd_section_size (sectp);
2108 }
2109 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2110 {
2111 dwz_file->str.s.section = sectp;
2112 dwz_file->str.size = bfd_section_size (sectp);
2113 }
2114 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2115 {
2116 dwz_file->line.s.section = sectp;
2117 dwz_file->line.size = bfd_section_size (sectp);
2118 }
2119 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2120 {
2121 dwz_file->macro.s.section = sectp;
2122 dwz_file->macro.size = bfd_section_size (sectp);
2123 }
2124 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2125 {
2126 dwz_file->gdb_index.s.section = sectp;
2127 dwz_file->gdb_index.size = bfd_section_size (sectp);
2128 }
2129 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2130 {
2131 dwz_file->debug_names.s.section = sectp;
2132 dwz_file->debug_names.size = bfd_section_size (sectp);
2133 }
2134 }
2135
2136 /* See dwarf2read.h. */
2137
2138 struct dwz_file *
2139 dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
2140 {
2141 const char *filename;
2142 bfd_size_type buildid_len_arg;
2143 size_t buildid_len;
2144 bfd_byte *buildid;
2145
2146 if (per_bfd->dwz_file != NULL)
2147 return per_bfd->dwz_file.get ();
2148
2149 bfd_set_error (bfd_error_no_error);
2150 gdb::unique_xmalloc_ptr<char> data
2151 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2152 &buildid_len_arg, &buildid));
2153 if (data == NULL)
2154 {
2155 if (bfd_get_error () == bfd_error_no_error)
2156 return NULL;
2157 error (_("could not read '.gnu_debugaltlink' section: %s"),
2158 bfd_errmsg (bfd_get_error ()));
2159 }
2160
2161 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2162
2163 buildid_len = (size_t) buildid_len_arg;
2164
2165 filename = data.get ();
2166
2167 std::string abs_storage;
2168 if (!IS_ABSOLUTE_PATH (filename))
2169 {
2170 gdb::unique_xmalloc_ptr<char> abs
2171 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2172
2173 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2174 filename = abs_storage.c_str ();
2175 }
2176
2177 /* First try the file name given in the section. If that doesn't
2178 work, try to use the build-id instead. */
2179 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
2180 if (dwz_bfd != NULL)
2181 {
2182 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2183 dwz_bfd.reset (nullptr);
2184 }
2185
2186 if (dwz_bfd == NULL)
2187 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2188
2189 if (dwz_bfd == nullptr)
2190 {
2191 gdb::unique_xmalloc_ptr<char> alt_filename;
2192 const char *origname = bfd_get_filename (per_bfd->obfd);
2193
2194 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2195 buildid_len,
2196 origname,
2197 &alt_filename));
2198
2199 if (fd.get () >= 0)
2200 {
2201 /* File successfully retrieved from server. */
2202 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2203
2204 if (dwz_bfd == nullptr)
2205 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2206 alt_filename.get ());
2207 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2208 dwz_bfd.reset (nullptr);
2209 }
2210 }
2211
2212 if (dwz_bfd == NULL)
2213 error (_("could not find '.gnu_debugaltlink' file for %s"),
2214 bfd_get_filename (per_bfd->obfd));
2215
2216 std::unique_ptr<struct dwz_file> result
2217 (new struct dwz_file (std::move (dwz_bfd)));
2218
2219 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2220 result.get ());
2221
2222 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2223 per_bfd->dwz_file = std::move (result);
2224 return per_bfd->dwz_file.get ();
2225 }
2226 \f
2227 /* DWARF quick_symbols_functions support. */
2228
2229 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2230 unique line tables, so we maintain a separate table of all .debug_line
2231 derived entries to support the sharing.
2232 All the quick functions need is the list of file names. We discard the
2233 line_header when we're done and don't need to record it here. */
2234 struct quick_file_names
2235 {
2236 /* The data used to construct the hash key. */
2237 struct stmt_list_hash hash;
2238
2239 /* The number of entries in file_names, real_names. */
2240 unsigned int num_file_names;
2241
2242 /* The file names from the line table, after being run through
2243 file_full_name. */
2244 const char **file_names;
2245
2246 /* The file names from the line table after being run through
2247 gdb_realpath. These are computed lazily. */
2248 const char **real_names;
2249 };
2250
2251 /* When using the index (and thus not using psymtabs), each CU has an
2252 object of this type. This is used to hold information needed by
2253 the various "quick" methods. */
2254 struct dwarf2_per_cu_quick_data
2255 {
2256 /* The file table. This can be NULL if there was no file table
2257 or it's currently not read in.
2258 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2259 struct quick_file_names *file_names;
2260
2261 /* A temporary mark bit used when iterating over all CUs in
2262 expand_symtabs_matching. */
2263 unsigned int mark : 1;
2264
2265 /* True if we've tried to read the file table and found there isn't one.
2266 There will be no point in trying to read it again next time. */
2267 unsigned int no_file_data : 1;
2268 };
2269
2270 /* Utility hash function for a stmt_list_hash. */
2271
2272 static hashval_t
2273 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2274 {
2275 hashval_t v = 0;
2276
2277 if (stmt_list_hash->dwo_unit != NULL)
2278 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2279 v += to_underlying (stmt_list_hash->line_sect_off);
2280 return v;
2281 }
2282
2283 /* Utility equality function for a stmt_list_hash. */
2284
2285 static int
2286 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2287 const struct stmt_list_hash *rhs)
2288 {
2289 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2290 return 0;
2291 if (lhs->dwo_unit != NULL
2292 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2293 return 0;
2294
2295 return lhs->line_sect_off == rhs->line_sect_off;
2296 }
2297
2298 /* Hash function for a quick_file_names. */
2299
2300 static hashval_t
2301 hash_file_name_entry (const void *e)
2302 {
2303 const struct quick_file_names *file_data
2304 = (const struct quick_file_names *) e;
2305
2306 return hash_stmt_list_entry (&file_data->hash);
2307 }
2308
2309 /* Equality function for a quick_file_names. */
2310
2311 static int
2312 eq_file_name_entry (const void *a, const void *b)
2313 {
2314 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2315 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2316
2317 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2318 }
2319
2320 /* Delete function for a quick_file_names. */
2321
2322 static void
2323 delete_file_name_entry (void *e)
2324 {
2325 struct quick_file_names *file_data = (struct quick_file_names *) e;
2326 int i;
2327
2328 for (i = 0; i < file_data->num_file_names; ++i)
2329 {
2330 xfree ((void*) file_data->file_names[i]);
2331 if (file_data->real_names)
2332 xfree ((void*) file_data->real_names[i]);
2333 }
2334
2335 /* The space for the struct itself lives on the obstack, so we don't
2336 free it here. */
2337 }
2338
2339 /* Create a quick_file_names hash table. */
2340
2341 static htab_up
2342 create_quick_file_names_table (unsigned int nr_initial_entries)
2343 {
2344 return htab_up (htab_create_alloc (nr_initial_entries,
2345 hash_file_name_entry, eq_file_name_entry,
2346 delete_file_name_entry, xcalloc, xfree));
2347 }
2348
2349 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2350 function is unrelated to symtabs, symtab would have to be created afterwards.
2351 You should call age_cached_comp_units after processing the CU. */
2352
2353 static void
2354 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2355 bool skip_partial)
2356 {
2357 if (per_cu->is_debug_types)
2358 load_full_type_unit (per_cu, per_objfile);
2359 else
2360 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
2361
2362 if (per_cu->cu == NULL)
2363 return; /* Dummy CU. */
2364
2365 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2366 }
2367
2368 /* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2369
2370 static void
2371 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2372 dwarf2_per_objfile *dwarf2_per_objfile,
2373 bool skip_partial)
2374 {
2375 /* Skip type_unit_groups, reading the type units they contain
2376 is handled elsewhere. */
2377 if (per_cu->type_unit_group_p ())
2378 return;
2379
2380 /* The destructor of dwarf2_queue_guard frees any entries left on
2381 the queue. After this point we're guaranteed to leave this function
2382 with the dwarf queue empty. */
2383 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2384
2385 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2386 {
2387 queue_comp_unit (per_cu, dwarf2_per_objfile, language_minimal);
2388 load_cu (per_cu, dwarf2_per_objfile, skip_partial);
2389
2390 /* If we just loaded a CU from a DWO, and we're working with an index
2391 that may badly handle TUs, load all the TUs in that DWO as well.
2392 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2393 if (!per_cu->is_debug_types
2394 && per_cu->cu != NULL
2395 && per_cu->cu->dwo_unit != NULL
2396 && dwarf2_per_objfile->per_bfd->index_table != NULL
2397 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
2398 /* DWP files aren't supported yet. */
2399 && get_dwp_file (dwarf2_per_objfile) == NULL)
2400 queue_and_load_all_dwo_tus (per_cu);
2401 }
2402
2403 process_queue (dwarf2_per_objfile);
2404
2405 /* Age the cache, releasing compilation units that have not
2406 been used recently. */
2407 age_cached_comp_units (dwarf2_per_objfile);
2408 }
2409
2410 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2411 the per-objfile for which this symtab is instantiated.
2412
2413 Returns the resulting symbol table. */
2414
2415 static struct compunit_symtab *
2416 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2417 dwarf2_per_objfile *dwarf2_per_objfile,
2418 bool skip_partial)
2419 {
2420 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
2421
2422 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2423 {
2424 free_cached_comp_units freer (dwarf2_per_objfile);
2425 scoped_restore decrementer = increment_reading_symtab ();
2426 dw2_do_instantiate_symtab (per_cu, dwarf2_per_objfile, skip_partial);
2427 process_cu_includes (dwarf2_per_objfile);
2428 }
2429
2430 return dwarf2_per_objfile->get_symtab (per_cu);
2431 }
2432
2433 /* See declaration. */
2434
2435 dwarf2_per_cu_data *
2436 dwarf2_per_bfd::get_cutu (int index)
2437 {
2438 if (index >= this->all_comp_units.size ())
2439 {
2440 index -= this->all_comp_units.size ();
2441 gdb_assert (index < this->all_type_units.size ());
2442 return &this->all_type_units[index]->per_cu;
2443 }
2444
2445 return this->all_comp_units[index];
2446 }
2447
2448 /* See declaration. */
2449
2450 dwarf2_per_cu_data *
2451 dwarf2_per_bfd::get_cu (int index)
2452 {
2453 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2454
2455 return this->all_comp_units[index];
2456 }
2457
2458 /* See declaration. */
2459
2460 signatured_type *
2461 dwarf2_per_bfd::get_tu (int index)
2462 {
2463 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2464
2465 return this->all_type_units[index];
2466 }
2467
2468 /* See read.h. */
2469
2470 dwarf2_per_cu_data *
2471 dwarf2_per_bfd::allocate_per_cu ()
2472 {
2473 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2474 result->per_bfd = this;
2475 result->index = m_num_psymtabs++;
2476 return result;
2477 }
2478
2479 /* See read.h. */
2480
2481 signatured_type *
2482 dwarf2_per_bfd::allocate_signatured_type ()
2483 {
2484 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2485 result->per_cu.per_bfd = this;
2486 result->per_cu.index = m_num_psymtabs++;
2487 return result;
2488 }
2489
2490 /* Return a new dwarf2_per_cu_data allocated on the dwarf2_per_objfile
2491 obstack, and constructed with the specified field values. */
2492
2493 static dwarf2_per_cu_data *
2494 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2495 struct dwarf2_section_info *section,
2496 int is_dwz,
2497 sect_offset sect_off, ULONGEST length)
2498 {
2499 dwarf2_per_cu_data *the_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
2500 the_cu->sect_off = sect_off;
2501 the_cu->length = length;
2502 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2503 the_cu->section = section;
2504 the_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2505 struct dwarf2_per_cu_quick_data);
2506 the_cu->is_dwz = is_dwz;
2507 return the_cu;
2508 }
2509
2510 /* A helper for create_cus_from_index that handles a given list of
2511 CUs. */
2512
2513 static void
2514 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2515 const gdb_byte *cu_list, offset_type n_elements,
2516 struct dwarf2_section_info *section,
2517 int is_dwz)
2518 {
2519 for (offset_type i = 0; i < n_elements; i += 2)
2520 {
2521 gdb_static_assert (sizeof (ULONGEST) >= 8);
2522
2523 sect_offset sect_off
2524 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2525 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2526 cu_list += 2 * 8;
2527
2528 dwarf2_per_cu_data *per_cu
2529 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2530 sect_off, length);
2531 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
2532 }
2533 }
2534
2535 /* Read the CU list from the mapped index, and use it to create all
2536 the CU objects for this objfile. */
2537
2538 static void
2539 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2540 const gdb_byte *cu_list, offset_type cu_list_elements,
2541 const gdb_byte *dwz_list, offset_type dwz_elements)
2542 {
2543 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
2544 dwarf2_per_objfile->per_bfd->all_comp_units.reserve
2545 ((cu_list_elements + dwz_elements) / 2);
2546
2547 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2548 &dwarf2_per_objfile->per_bfd->info, 0);
2549
2550 if (dwz_elements == 0)
2551 return;
2552
2553 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
2554 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2555 &dwz->info, 1);
2556 }
2557
2558 /* Create the signatured type hash table from the index. */
2559
2560 static void
2561 create_signatured_type_table_from_index
2562 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2563 struct dwarf2_section_info *section,
2564 const gdb_byte *bytes,
2565 offset_type elements)
2566 {
2567 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2568 dwarf2_per_objfile->per_bfd->all_type_units.reserve (elements / 3);
2569
2570 htab_up sig_types_hash = allocate_signatured_type_table ();
2571
2572 for (offset_type i = 0; i < elements; i += 3)
2573 {
2574 struct signatured_type *sig_type;
2575 ULONGEST signature;
2576 void **slot;
2577 cu_offset type_offset_in_tu;
2578
2579 gdb_static_assert (sizeof (ULONGEST) >= 8);
2580 sect_offset sect_off
2581 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2582 type_offset_in_tu
2583 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2584 BFD_ENDIAN_LITTLE);
2585 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2586 bytes += 3 * 8;
2587
2588 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2589 sig_type->signature = signature;
2590 sig_type->type_offset_in_tu = type_offset_in_tu;
2591 sig_type->per_cu.is_debug_types = 1;
2592 sig_type->per_cu.section = section;
2593 sig_type->per_cu.sect_off = sect_off;
2594 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2595 sig_type->per_cu.v.quick
2596 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2597 struct dwarf2_per_cu_quick_data);
2598
2599 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2600 *slot = sig_type;
2601
2602 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2603 }
2604
2605 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2606 }
2607
2608 /* Create the signatured type hash table from .debug_names. */
2609
2610 static void
2611 create_signatured_type_table_from_debug_names
2612 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2613 const mapped_debug_names &map,
2614 struct dwarf2_section_info *section,
2615 struct dwarf2_section_info *abbrev_section)
2616 {
2617 struct objfile *objfile = dwarf2_per_objfile->objfile;
2618
2619 section->read (objfile);
2620 abbrev_section->read (objfile);
2621
2622 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2623 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2624
2625 htab_up sig_types_hash = allocate_signatured_type_table ();
2626
2627 for (uint32_t i = 0; i < map.tu_count; ++i)
2628 {
2629 struct signatured_type *sig_type;
2630 void **slot;
2631
2632 sect_offset sect_off
2633 = (sect_offset) (extract_unsigned_integer
2634 (map.tu_table_reordered + i * map.offset_size,
2635 map.offset_size,
2636 map.dwarf5_byte_order));
2637
2638 comp_unit_head cu_header;
2639 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2640 abbrev_section,
2641 section->buffer + to_underlying (sect_off),
2642 rcuh_kind::TYPE);
2643
2644 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2645 sig_type->signature = cu_header.signature;
2646 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2647 sig_type->per_cu.is_debug_types = 1;
2648 sig_type->per_cu.section = section;
2649 sig_type->per_cu.sect_off = sect_off;
2650 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2651 sig_type->per_cu.v.quick
2652 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2653 struct dwarf2_per_cu_quick_data);
2654
2655 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2656 *slot = sig_type;
2657
2658 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2659 }
2660
2661 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2662 }
2663
2664 /* Read the address map data from the mapped index, and use it to
2665 populate the objfile's psymtabs_addrmap. */
2666
2667 static void
2668 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2669 struct mapped_index *index)
2670 {
2671 struct objfile *objfile = dwarf2_per_objfile->objfile;
2672 struct gdbarch *gdbarch = objfile->arch ();
2673 const gdb_byte *iter, *end;
2674 struct addrmap *mutable_map;
2675 CORE_ADDR baseaddr;
2676
2677 auto_obstack temp_obstack;
2678
2679 mutable_map = addrmap_create_mutable (&temp_obstack);
2680
2681 iter = index->address_table.data ();
2682 end = iter + index->address_table.size ();
2683
2684 baseaddr = objfile->text_section_offset ();
2685
2686 while (iter < end)
2687 {
2688 ULONGEST hi, lo, cu_index;
2689 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2690 iter += 8;
2691 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2692 iter += 8;
2693 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2694 iter += 4;
2695
2696 if (lo > hi)
2697 {
2698 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2699 hex_string (lo), hex_string (hi));
2700 continue;
2701 }
2702
2703 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
2704 {
2705 complaint (_(".gdb_index address table has invalid CU number %u"),
2706 (unsigned) cu_index);
2707 continue;
2708 }
2709
2710 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2711 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2712 addrmap_set_empty (mutable_map, lo, hi - 1,
2713 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
2714 }
2715
2716 objfile->partial_symtabs->psymtabs_addrmap
2717 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2718 }
2719
2720 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2721 populate the objfile's psymtabs_addrmap. */
2722
2723 static void
2724 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2725 struct dwarf2_section_info *section)
2726 {
2727 struct objfile *objfile = dwarf2_per_objfile->objfile;
2728 bfd *abfd = objfile->obfd;
2729 struct gdbarch *gdbarch = objfile->arch ();
2730 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2731
2732 auto_obstack temp_obstack;
2733 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2734
2735 std::unordered_map<sect_offset,
2736 dwarf2_per_cu_data *,
2737 gdb::hash_enum<sect_offset>>
2738 debug_info_offset_to_per_cu;
2739 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
2740 {
2741 const auto insertpair
2742 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2743 if (!insertpair.second)
2744 {
2745 warning (_("Section .debug_aranges in %s has duplicate "
2746 "debug_info_offset %s, ignoring .debug_aranges."),
2747 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2748 return;
2749 }
2750 }
2751
2752 section->read (objfile);
2753
2754 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2755
2756 const gdb_byte *addr = section->buffer;
2757
2758 while (addr < section->buffer + section->size)
2759 {
2760 const gdb_byte *const entry_addr = addr;
2761 unsigned int bytes_read;
2762
2763 const LONGEST entry_length = read_initial_length (abfd, addr,
2764 &bytes_read);
2765 addr += bytes_read;
2766
2767 const gdb_byte *const entry_end = addr + entry_length;
2768 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2769 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2770 if (addr + entry_length > section->buffer + section->size)
2771 {
2772 warning (_("Section .debug_aranges in %s entry at offset %s "
2773 "length %s exceeds section length %s, "
2774 "ignoring .debug_aranges."),
2775 objfile_name (objfile),
2776 plongest (entry_addr - section->buffer),
2777 plongest (bytes_read + entry_length),
2778 pulongest (section->size));
2779 return;
2780 }
2781
2782 /* The version number. */
2783 const uint16_t version = read_2_bytes (abfd, addr);
2784 addr += 2;
2785 if (version != 2)
2786 {
2787 warning (_("Section .debug_aranges in %s entry at offset %s "
2788 "has unsupported version %d, ignoring .debug_aranges."),
2789 objfile_name (objfile),
2790 plongest (entry_addr - section->buffer), version);
2791 return;
2792 }
2793
2794 const uint64_t debug_info_offset
2795 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2796 addr += offset_size;
2797 const auto per_cu_it
2798 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2799 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2800 {
2801 warning (_("Section .debug_aranges in %s entry at offset %s "
2802 "debug_info_offset %s does not exists, "
2803 "ignoring .debug_aranges."),
2804 objfile_name (objfile),
2805 plongest (entry_addr - section->buffer),
2806 pulongest (debug_info_offset));
2807 return;
2808 }
2809 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2810
2811 const uint8_t address_size = *addr++;
2812 if (address_size < 1 || address_size > 8)
2813 {
2814 warning (_("Section .debug_aranges in %s entry at offset %s "
2815 "address_size %u is invalid, ignoring .debug_aranges."),
2816 objfile_name (objfile),
2817 plongest (entry_addr - section->buffer), address_size);
2818 return;
2819 }
2820
2821 const uint8_t segment_selector_size = *addr++;
2822 if (segment_selector_size != 0)
2823 {
2824 warning (_("Section .debug_aranges in %s entry at offset %s "
2825 "segment_selector_size %u is not supported, "
2826 "ignoring .debug_aranges."),
2827 objfile_name (objfile),
2828 plongest (entry_addr - section->buffer),
2829 segment_selector_size);
2830 return;
2831 }
2832
2833 /* Must pad to an alignment boundary that is twice the address
2834 size. It is undocumented by the DWARF standard but GCC does
2835 use it. */
2836 for (size_t padding = ((-(addr - section->buffer))
2837 & (2 * address_size - 1));
2838 padding > 0; padding--)
2839 if (*addr++ != 0)
2840 {
2841 warning (_("Section .debug_aranges in %s entry at offset %s "
2842 "padding is not zero, ignoring .debug_aranges."),
2843 objfile_name (objfile),
2844 plongest (entry_addr - section->buffer));
2845 return;
2846 }
2847
2848 for (;;)
2849 {
2850 if (addr + 2 * address_size > entry_end)
2851 {
2852 warning (_("Section .debug_aranges in %s entry at offset %s "
2853 "address list is not properly terminated, "
2854 "ignoring .debug_aranges."),
2855 objfile_name (objfile),
2856 plongest (entry_addr - section->buffer));
2857 return;
2858 }
2859 ULONGEST start = extract_unsigned_integer (addr, address_size,
2860 dwarf5_byte_order);
2861 addr += address_size;
2862 ULONGEST length = extract_unsigned_integer (addr, address_size,
2863 dwarf5_byte_order);
2864 addr += address_size;
2865 if (start == 0 && length == 0)
2866 break;
2867 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
2868 {
2869 /* Symbol was eliminated due to a COMDAT group. */
2870 continue;
2871 }
2872 ULONGEST end = start + length;
2873 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2874 - baseaddr);
2875 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2876 - baseaddr);
2877 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2878 }
2879 }
2880
2881 objfile->partial_symtabs->psymtabs_addrmap
2882 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2883 }
2884
2885 /* Find a slot in the mapped index INDEX for the object named NAME.
2886 If NAME is found, set *VEC_OUT to point to the CU vector in the
2887 constant pool and return true. If NAME cannot be found, return
2888 false. */
2889
2890 static bool
2891 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2892 offset_type **vec_out)
2893 {
2894 offset_type hash;
2895 offset_type slot, step;
2896 int (*cmp) (const char *, const char *);
2897
2898 gdb::unique_xmalloc_ptr<char> without_params;
2899 if (current_language->la_language == language_cplus
2900 || current_language->la_language == language_fortran
2901 || current_language->la_language == language_d)
2902 {
2903 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2904 not contain any. */
2905
2906 if (strchr (name, '(') != NULL)
2907 {
2908 without_params = cp_remove_params (name);
2909
2910 if (without_params != NULL)
2911 name = without_params.get ();
2912 }
2913 }
2914
2915 /* Index version 4 did not support case insensitive searches. But the
2916 indices for case insensitive languages are built in lowercase, therefore
2917 simulate our NAME being searched is also lowercased. */
2918 hash = mapped_index_string_hash ((index->version == 4
2919 && case_sensitivity == case_sensitive_off
2920 ? 5 : index->version),
2921 name);
2922
2923 slot = hash & (index->symbol_table.size () - 1);
2924 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2925 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2926
2927 for (;;)
2928 {
2929 const char *str;
2930
2931 const auto &bucket = index->symbol_table[slot];
2932 if (bucket.name == 0 && bucket.vec == 0)
2933 return false;
2934
2935 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2936 if (!cmp (name, str))
2937 {
2938 *vec_out = (offset_type *) (index->constant_pool
2939 + MAYBE_SWAP (bucket.vec));
2940 return true;
2941 }
2942
2943 slot = (slot + step) & (index->symbol_table.size () - 1);
2944 }
2945 }
2946
2947 /* A helper function that reads the .gdb_index from BUFFER and fills
2948 in MAP. FILENAME is the name of the file containing the data;
2949 it is used for error reporting. DEPRECATED_OK is true if it is
2950 ok to use deprecated sections.
2951
2952 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2953 out parameters that are filled in with information about the CU and
2954 TU lists in the section.
2955
2956 Returns true if all went well, false otherwise. */
2957
2958 static bool
2959 read_gdb_index_from_buffer (const char *filename,
2960 bool deprecated_ok,
2961 gdb::array_view<const gdb_byte> buffer,
2962 struct mapped_index *map,
2963 const gdb_byte **cu_list,
2964 offset_type *cu_list_elements,
2965 const gdb_byte **types_list,
2966 offset_type *types_list_elements)
2967 {
2968 const gdb_byte *addr = &buffer[0];
2969
2970 /* Version check. */
2971 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2972 /* Versions earlier than 3 emitted every copy of a psymbol. This
2973 causes the index to behave very poorly for certain requests. Version 3
2974 contained incomplete addrmap. So, it seems better to just ignore such
2975 indices. */
2976 if (version < 4)
2977 {
2978 static int warning_printed = 0;
2979 if (!warning_printed)
2980 {
2981 warning (_("Skipping obsolete .gdb_index section in %s."),
2982 filename);
2983 warning_printed = 1;
2984 }
2985 return 0;
2986 }
2987 /* Index version 4 uses a different hash function than index version
2988 5 and later.
2989
2990 Versions earlier than 6 did not emit psymbols for inlined
2991 functions. Using these files will cause GDB not to be able to
2992 set breakpoints on inlined functions by name, so we ignore these
2993 indices unless the user has done
2994 "set use-deprecated-index-sections on". */
2995 if (version < 6 && !deprecated_ok)
2996 {
2997 static int warning_printed = 0;
2998 if (!warning_printed)
2999 {
3000 warning (_("\
3001 Skipping deprecated .gdb_index section in %s.\n\
3002 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3003 to use the section anyway."),
3004 filename);
3005 warning_printed = 1;
3006 }
3007 return 0;
3008 }
3009 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3010 of the TU (for symbols coming from TUs),
3011 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3012 Plus gold-generated indices can have duplicate entries for global symbols,
3013 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3014 These are just performance bugs, and we can't distinguish gdb-generated
3015 indices from gold-generated ones, so issue no warning here. */
3016
3017 /* Indexes with higher version than the one supported by GDB may be no
3018 longer backward compatible. */
3019 if (version > 8)
3020 return 0;
3021
3022 map->version = version;
3023
3024 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3025
3026 int i = 0;
3027 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3028 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3029 / 8);
3030 ++i;
3031
3032 *types_list = addr + MAYBE_SWAP (metadata[i]);
3033 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3034 - MAYBE_SWAP (metadata[i]))
3035 / 8);
3036 ++i;
3037
3038 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3039 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3040 map->address_table
3041 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3042 ++i;
3043
3044 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3045 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3046 map->symbol_table
3047 = gdb::array_view<mapped_index::symbol_table_slot>
3048 ((mapped_index::symbol_table_slot *) symbol_table,
3049 (mapped_index::symbol_table_slot *) symbol_table_end);
3050
3051 ++i;
3052 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3053
3054 return 1;
3055 }
3056
3057 /* Callback types for dwarf2_read_gdb_index. */
3058
3059 typedef gdb::function_view
3060 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3061 get_gdb_index_contents_ftype;
3062 typedef gdb::function_view
3063 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3064 get_gdb_index_contents_dwz_ftype;
3065
3066 /* Read .gdb_index. If everything went ok, initialize the "quick"
3067 elements of all the CUs and return 1. Otherwise, return 0. */
3068
3069 static int
3070 dwarf2_read_gdb_index
3071 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3072 get_gdb_index_contents_ftype get_gdb_index_contents,
3073 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3074 {
3075 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3076 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3077 struct dwz_file *dwz;
3078 struct objfile *objfile = dwarf2_per_objfile->objfile;
3079
3080 gdb::array_view<const gdb_byte> main_index_contents
3081 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
3082
3083 if (main_index_contents.empty ())
3084 return 0;
3085
3086 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3087 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3088 use_deprecated_index_sections,
3089 main_index_contents, map.get (), &cu_list,
3090 &cu_list_elements, &types_list,
3091 &types_list_elements))
3092 return 0;
3093
3094 /* Don't use the index if it's empty. */
3095 if (map->symbol_table.empty ())
3096 return 0;
3097
3098 /* If there is a .dwz file, read it so we can get its CU list as
3099 well. */
3100 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
3101 if (dwz != NULL)
3102 {
3103 struct mapped_index dwz_map;
3104 const gdb_byte *dwz_types_ignore;
3105 offset_type dwz_types_elements_ignore;
3106
3107 gdb::array_view<const gdb_byte> dwz_index_content
3108 = get_gdb_index_contents_dwz (objfile, dwz);
3109
3110 if (dwz_index_content.empty ())
3111 return 0;
3112
3113 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3114 1, dwz_index_content, &dwz_map,
3115 &dwz_list, &dwz_list_elements,
3116 &dwz_types_ignore,
3117 &dwz_types_elements_ignore))
3118 {
3119 warning (_("could not read '.gdb_index' section from %s; skipping"),
3120 bfd_get_filename (dwz->dwz_bfd.get ()));
3121 return 0;
3122 }
3123 }
3124
3125 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3126 dwz_list, dwz_list_elements);
3127
3128 if (types_list_elements)
3129 {
3130 /* We can only handle a single .debug_types when we have an
3131 index. */
3132 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
3133 return 0;
3134
3135 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
3136
3137 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3138 types_list, types_list_elements);
3139 }
3140
3141 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3142
3143 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3144 dwarf2_per_objfile->per_bfd->using_index = 1;
3145 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3146 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
3147
3148 return 1;
3149 }
3150
3151 /* die_reader_func for dw2_get_file_names. */
3152
3153 static void
3154 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3155 const gdb_byte *info_ptr,
3156 struct die_info *comp_unit_die)
3157 {
3158 struct dwarf2_cu *cu = reader->cu;
3159 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3160 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3161 struct dwarf2_per_cu_data *lh_cu;
3162 struct attribute *attr;
3163 void **slot;
3164 struct quick_file_names *qfn;
3165
3166 gdb_assert (! this_cu->is_debug_types);
3167
3168 /* Our callers never want to match partial units -- instead they
3169 will match the enclosing full CU. */
3170 if (comp_unit_die->tag == DW_TAG_partial_unit)
3171 {
3172 this_cu->v.quick->no_file_data = 1;
3173 return;
3174 }
3175
3176 lh_cu = this_cu;
3177 slot = NULL;
3178
3179 line_header_up lh;
3180 sect_offset line_offset {};
3181
3182 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3183 if (attr != nullptr)
3184 {
3185 struct quick_file_names find_entry;
3186
3187 line_offset = (sect_offset) DW_UNSND (attr);
3188
3189 /* We may have already read in this line header (TU line header sharing).
3190 If we have we're done. */
3191 find_entry.hash.dwo_unit = cu->dwo_unit;
3192 find_entry.hash.line_sect_off = line_offset;
3193 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3194 &find_entry, INSERT);
3195 if (*slot != NULL)
3196 {
3197 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3198 return;
3199 }
3200
3201 lh = dwarf_decode_line_header (line_offset, cu);
3202 }
3203 if (lh == NULL)
3204 {
3205 lh_cu->v.quick->no_file_data = 1;
3206 return;
3207 }
3208
3209 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
3210 qfn->hash.dwo_unit = cu->dwo_unit;
3211 qfn->hash.line_sect_off = line_offset;
3212 gdb_assert (slot != NULL);
3213 *slot = qfn;
3214
3215 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3216
3217 int offset = 0;
3218 if (strcmp (fnd.name, "<unknown>") != 0)
3219 ++offset;
3220
3221 qfn->num_file_names = offset + lh->file_names_size ();
3222 qfn->file_names =
3223 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
3224 qfn->num_file_names);
3225 if (offset != 0)
3226 qfn->file_names[0] = xstrdup (fnd.name);
3227 for (int i = 0; i < lh->file_names_size (); ++i)
3228 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3229 fnd.comp_dir).release ();
3230 qfn->real_names = NULL;
3231
3232 lh_cu->v.quick->file_names = qfn;
3233 }
3234
3235 /* A helper for the "quick" functions which attempts to read the line
3236 table for THIS_CU. */
3237
3238 static struct quick_file_names *
3239 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3240 dwarf2_per_objfile *per_objfile)
3241 {
3242 /* This should never be called for TUs. */
3243 gdb_assert (! this_cu->is_debug_types);
3244 /* Nor type unit groups. */
3245 gdb_assert (! this_cu->type_unit_group_p ());
3246
3247 if (this_cu->v.quick->file_names != NULL)
3248 return this_cu->v.quick->file_names;
3249 /* If we know there is no line data, no point in looking again. */
3250 if (this_cu->v.quick->no_file_data)
3251 return NULL;
3252
3253 cutu_reader reader (this_cu, per_objfile);
3254 if (!reader.dummy_p)
3255 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3256
3257 if (this_cu->v.quick->no_file_data)
3258 return NULL;
3259 return this_cu->v.quick->file_names;
3260 }
3261
3262 /* A helper for the "quick" functions which computes and caches the
3263 real path for a given file name from the line table. */
3264
3265 static const char *
3266 dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
3267 struct quick_file_names *qfn, int index)
3268 {
3269 if (qfn->real_names == NULL)
3270 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
3271 qfn->num_file_names, const char *);
3272
3273 if (qfn->real_names[index] == NULL)
3274 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3275
3276 return qfn->real_names[index];
3277 }
3278
3279 static struct symtab *
3280 dw2_find_last_source_symtab (struct objfile *objfile)
3281 {
3282 struct dwarf2_per_objfile *dwarf2_per_objfile
3283 = get_dwarf2_per_objfile (objfile);
3284 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
3285 compunit_symtab *cust
3286 = dw2_instantiate_symtab (dwarf_cu, dwarf2_per_objfile, false);
3287
3288 if (cust == NULL)
3289 return NULL;
3290
3291 return compunit_primary_filetab (cust);
3292 }
3293
3294 /* Traversal function for dw2_forget_cached_source_info. */
3295
3296 static int
3297 dw2_free_cached_file_names (void **slot, void *info)
3298 {
3299 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3300
3301 if (file_data->real_names)
3302 {
3303 int i;
3304
3305 for (i = 0; i < file_data->num_file_names; ++i)
3306 {
3307 xfree ((void*) file_data->real_names[i]);
3308 file_data->real_names[i] = NULL;
3309 }
3310 }
3311
3312 return 1;
3313 }
3314
3315 static void
3316 dw2_forget_cached_source_info (struct objfile *objfile)
3317 {
3318 struct dwarf2_per_objfile *dwarf2_per_objfile
3319 = get_dwarf2_per_objfile (objfile);
3320
3321 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3322 dw2_free_cached_file_names, NULL);
3323 }
3324
3325 /* Helper function for dw2_map_symtabs_matching_filename that expands
3326 the symtabs and calls the iterator. */
3327
3328 static int
3329 dw2_map_expand_apply (struct objfile *objfile,
3330 struct dwarf2_per_cu_data *per_cu,
3331 const char *name, const char *real_path,
3332 gdb::function_view<bool (symtab *)> callback)
3333 {
3334 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3335
3336 /* Don't visit already-expanded CUs. */
3337 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3338 if (per_objfile->symtab_set_p (per_cu))
3339 return 0;
3340
3341 /* This may expand more than one symtab, and we want to iterate over
3342 all of them. */
3343 dw2_instantiate_symtab (per_cu, per_objfile, false);
3344
3345 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3346 last_made, callback);
3347 }
3348
3349 /* Implementation of the map_symtabs_matching_filename method. */
3350
3351 static bool
3352 dw2_map_symtabs_matching_filename
3353 (struct objfile *objfile, const char *name, const char *real_path,
3354 gdb::function_view<bool (symtab *)> callback)
3355 {
3356 const char *name_basename = lbasename (name);
3357 struct dwarf2_per_objfile *dwarf2_per_objfile
3358 = get_dwarf2_per_objfile (objfile);
3359
3360 /* The rule is CUs specify all the files, including those used by
3361 any TU, so there's no need to scan TUs here. */
3362
3363 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3364 {
3365 /* We only need to look at symtabs not already expanded. */
3366 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3367 continue;
3368
3369 quick_file_names *file_data
3370 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3371 if (file_data == NULL)
3372 continue;
3373
3374 for (int j = 0; j < file_data->num_file_names; ++j)
3375 {
3376 const char *this_name = file_data->file_names[j];
3377 const char *this_real_name;
3378
3379 if (compare_filenames_for_search (this_name, name))
3380 {
3381 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3382 callback))
3383 return true;
3384 continue;
3385 }
3386
3387 /* Before we invoke realpath, which can get expensive when many
3388 files are involved, do a quick comparison of the basenames. */
3389 if (! basenames_may_differ
3390 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3391 continue;
3392
3393 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3394 file_data, j);
3395 if (compare_filenames_for_search (this_real_name, name))
3396 {
3397 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3398 callback))
3399 return true;
3400 continue;
3401 }
3402
3403 if (real_path != NULL)
3404 {
3405 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3406 gdb_assert (IS_ABSOLUTE_PATH (name));
3407 if (this_real_name != NULL
3408 && FILENAME_CMP (real_path, this_real_name) == 0)
3409 {
3410 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3411 callback))
3412 return true;
3413 continue;
3414 }
3415 }
3416 }
3417 }
3418
3419 return false;
3420 }
3421
3422 /* Struct used to manage iterating over all CUs looking for a symbol. */
3423
3424 struct dw2_symtab_iterator
3425 {
3426 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3427 struct dwarf2_per_objfile *dwarf2_per_objfile;
3428 /* If set, only look for symbols that match that block. Valid values are
3429 GLOBAL_BLOCK and STATIC_BLOCK. */
3430 gdb::optional<block_enum> block_index;
3431 /* The kind of symbol we're looking for. */
3432 domain_enum domain;
3433 /* The list of CUs from the index entry of the symbol,
3434 or NULL if not found. */
3435 offset_type *vec;
3436 /* The next element in VEC to look at. */
3437 int next;
3438 /* The number of elements in VEC, or zero if there is no match. */
3439 int length;
3440 /* Have we seen a global version of the symbol?
3441 If so we can ignore all further global instances.
3442 This is to work around gold/15646, inefficient gold-generated
3443 indices. */
3444 int global_seen;
3445 };
3446
3447 /* Initialize the index symtab iterator ITER. */
3448
3449 static void
3450 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3451 struct dwarf2_per_objfile *dwarf2_per_objfile,
3452 gdb::optional<block_enum> block_index,
3453 domain_enum domain,
3454 const char *name)
3455 {
3456 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3457 iter->block_index = block_index;
3458 iter->domain = domain;
3459 iter->next = 0;
3460 iter->global_seen = 0;
3461
3462 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
3463
3464 /* index is NULL if OBJF_READNOW. */
3465 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3466 iter->length = MAYBE_SWAP (*iter->vec);
3467 else
3468 {
3469 iter->vec = NULL;
3470 iter->length = 0;
3471 }
3472 }
3473
3474 /* Return the next matching CU or NULL if there are no more. */
3475
3476 static struct dwarf2_per_cu_data *
3477 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3478 {
3479 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3480
3481 for ( ; iter->next < iter->length; ++iter->next)
3482 {
3483 offset_type cu_index_and_attrs =
3484 MAYBE_SWAP (iter->vec[iter->next + 1]);
3485 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3486 gdb_index_symbol_kind symbol_kind =
3487 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3488 /* Only check the symbol attributes if they're present.
3489 Indices prior to version 7 don't record them,
3490 and indices >= 7 may elide them for certain symbols
3491 (gold does this). */
3492 int attrs_valid =
3493 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
3494 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3495
3496 /* Don't crash on bad data. */
3497 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3498 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3499 {
3500 complaint (_(".gdb_index entry has bad CU index"
3501 " [in module %s]"),
3502 objfile_name (dwarf2_per_objfile->objfile));
3503 continue;
3504 }
3505
3506 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3507
3508 /* Skip if already read in. */
3509 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3510 continue;
3511
3512 /* Check static vs global. */
3513 if (attrs_valid)
3514 {
3515 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3516
3517 if (iter->block_index.has_value ())
3518 {
3519 bool want_static = *iter->block_index == STATIC_BLOCK;
3520
3521 if (is_static != want_static)
3522 continue;
3523 }
3524
3525 /* Work around gold/15646. */
3526 if (!is_static && iter->global_seen)
3527 continue;
3528 if (!is_static)
3529 iter->global_seen = 1;
3530 }
3531
3532 /* Only check the symbol's kind if it has one. */
3533 if (attrs_valid)
3534 {
3535 switch (iter->domain)
3536 {
3537 case VAR_DOMAIN:
3538 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3539 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3540 /* Some types are also in VAR_DOMAIN. */
3541 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3542 continue;
3543 break;
3544 case STRUCT_DOMAIN:
3545 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3546 continue;
3547 break;
3548 case LABEL_DOMAIN:
3549 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3550 continue;
3551 break;
3552 case MODULE_DOMAIN:
3553 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3554 continue;
3555 break;
3556 default:
3557 break;
3558 }
3559 }
3560
3561 ++iter->next;
3562 return per_cu;
3563 }
3564
3565 return NULL;
3566 }
3567
3568 static struct compunit_symtab *
3569 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3570 const char *name, domain_enum domain)
3571 {
3572 struct compunit_symtab *stab_best = NULL;
3573 struct dwarf2_per_objfile *dwarf2_per_objfile
3574 = get_dwarf2_per_objfile (objfile);
3575
3576 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3577
3578 struct dw2_symtab_iterator iter;
3579 struct dwarf2_per_cu_data *per_cu;
3580
3581 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3582
3583 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3584 {
3585 struct symbol *sym, *with_opaque = NULL;
3586 struct compunit_symtab *stab
3587 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3588 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3589 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3590
3591 sym = block_find_symbol (block, name, domain,
3592 block_find_non_opaque_type_preferred,
3593 &with_opaque);
3594
3595 /* Some caution must be observed with overloaded functions
3596 and methods, since the index will not contain any overload
3597 information (but NAME might contain it). */
3598
3599 if (sym != NULL
3600 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3601 return stab;
3602 if (with_opaque != NULL
3603 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3604 stab_best = stab;
3605
3606 /* Keep looking through other CUs. */
3607 }
3608
3609 return stab_best;
3610 }
3611
3612 static void
3613 dw2_print_stats (struct objfile *objfile)
3614 {
3615 struct dwarf2_per_objfile *dwarf2_per_objfile
3616 = get_dwarf2_per_objfile (objfile);
3617 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3618 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3619 int count = 0;
3620
3621 for (int i = 0; i < total; ++i)
3622 {
3623 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3624
3625 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
3626 ++count;
3627 }
3628 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3629 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3630 }
3631
3632 /* This dumps minimal information about the index.
3633 It is called via "mt print objfiles".
3634 One use is to verify .gdb_index has been loaded by the
3635 gdb.dwarf2/gdb-index.exp testcase. */
3636
3637 static void
3638 dw2_dump (struct objfile *objfile)
3639 {
3640 struct dwarf2_per_objfile *dwarf2_per_objfile
3641 = get_dwarf2_per_objfile (objfile);
3642
3643 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
3644 printf_filtered (".gdb_index:");
3645 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
3646 {
3647 printf_filtered (" version %d\n",
3648 dwarf2_per_objfile->per_bfd->index_table->version);
3649 }
3650 else
3651 printf_filtered (" faked for \"readnow\"\n");
3652 printf_filtered ("\n");
3653 }
3654
3655 static void
3656 dw2_expand_symtabs_for_function (struct objfile *objfile,
3657 const char *func_name)
3658 {
3659 struct dwarf2_per_objfile *dwarf2_per_objfile
3660 = get_dwarf2_per_objfile (objfile);
3661
3662 struct dw2_symtab_iterator iter;
3663 struct dwarf2_per_cu_data *per_cu;
3664
3665 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3666
3667 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3668 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3669
3670 }
3671
3672 static void
3673 dw2_expand_all_symtabs (struct objfile *objfile)
3674 {
3675 struct dwarf2_per_objfile *dwarf2_per_objfile
3676 = get_dwarf2_per_objfile (objfile);
3677 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3678 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3679
3680 for (int i = 0; i < total_units; ++i)
3681 {
3682 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3683
3684 /* We don't want to directly expand a partial CU, because if we
3685 read it with the wrong language, then assertion failures can
3686 be triggered later on. See PR symtab/23010. So, tell
3687 dw2_instantiate_symtab to skip partial CUs -- any important
3688 partial CU will be read via DW_TAG_imported_unit anyway. */
3689 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, true);
3690 }
3691 }
3692
3693 static void
3694 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3695 const char *fullname)
3696 {
3697 struct dwarf2_per_objfile *dwarf2_per_objfile
3698 = get_dwarf2_per_objfile (objfile);
3699
3700 /* We don't need to consider type units here.
3701 This is only called for examining code, e.g. expand_line_sal.
3702 There can be an order of magnitude (or more) more type units
3703 than comp units, and we avoid them if we can. */
3704
3705 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3706 {
3707 /* We only need to look at symtabs not already expanded. */
3708 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3709 continue;
3710
3711 quick_file_names *file_data
3712 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3713 if (file_data == NULL)
3714 continue;
3715
3716 for (int j = 0; j < file_data->num_file_names; ++j)
3717 {
3718 const char *this_fullname = file_data->file_names[j];
3719
3720 if (filename_cmp (this_fullname, fullname) == 0)
3721 {
3722 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3723 break;
3724 }
3725 }
3726 }
3727 }
3728
3729 static void
3730 dw2_expand_symtabs_matching_symbol
3731 (mapped_index_base &index,
3732 const lookup_name_info &lookup_name_in,
3733 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3734 enum search_domain kind,
3735 gdb::function_view<bool (offset_type)> match_callback);
3736
3737 static void
3738 dw2_expand_symtabs_matching_one
3739 (dwarf2_per_cu_data *per_cu,
3740 dwarf2_per_objfile *per_objfile,
3741 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3742 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3743
3744 static void
3745 dw2_map_matching_symbols
3746 (struct objfile *objfile,
3747 const lookup_name_info &name, domain_enum domain,
3748 int global,
3749 gdb::function_view<symbol_found_callback_ftype> callback,
3750 symbol_compare_ftype *ordered_compare)
3751 {
3752 /* Used for Ada. */
3753 struct dwarf2_per_objfile *dwarf2_per_objfile
3754 = get_dwarf2_per_objfile (objfile);
3755
3756 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3757
3758 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
3759 {
3760 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3761 here though if the current language is Ada for a non-Ada objfile
3762 using GNU index. */
3763 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3764
3765 const char *match_name = name.ada ().lookup_name ().c_str ();
3766 auto matcher = [&] (const char *symname)
3767 {
3768 if (ordered_compare == nullptr)
3769 return true;
3770 return ordered_compare (symname, match_name) == 0;
3771 };
3772
3773 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3774 [&] (offset_type namei)
3775 {
3776 struct dw2_symtab_iterator iter;
3777 struct dwarf2_per_cu_data *per_cu;
3778
3779 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3780 match_name);
3781 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3782 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
3783 nullptr);
3784 return true;
3785 });
3786 }
3787 else
3788 {
3789 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3790 proceed assuming all symtabs have been read in. */
3791 }
3792
3793 for (compunit_symtab *cust : objfile->compunits ())
3794 {
3795 const struct block *block;
3796
3797 if (cust == NULL)
3798 continue;
3799 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3800 if (!iterate_over_symbols_terminated (block, name,
3801 domain, callback))
3802 return;
3803 }
3804 }
3805
3806 /* Starting from a search name, return the string that finds the upper
3807 bound of all strings that start with SEARCH_NAME in a sorted name
3808 list. Returns the empty string to indicate that the upper bound is
3809 the end of the list. */
3810
3811 static std::string
3812 make_sort_after_prefix_name (const char *search_name)
3813 {
3814 /* When looking to complete "func", we find the upper bound of all
3815 symbols that start with "func" by looking for where we'd insert
3816 the closest string that would follow "func" in lexicographical
3817 order. Usually, that's "func"-with-last-character-incremented,
3818 i.e. "fund". Mind non-ASCII characters, though. Usually those
3819 will be UTF-8 multi-byte sequences, but we can't be certain.
3820 Especially mind the 0xff character, which is a valid character in
3821 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3822 rule out compilers allowing it in identifiers. Note that
3823 conveniently, strcmp/strcasecmp are specified to compare
3824 characters interpreted as unsigned char. So what we do is treat
3825 the whole string as a base 256 number composed of a sequence of
3826 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3827 to 0, and carries 1 to the following more-significant position.
3828 If the very first character in SEARCH_NAME ends up incremented
3829 and carries/overflows, then the upper bound is the end of the
3830 list. The string after the empty string is also the empty
3831 string.
3832
3833 Some examples of this operation:
3834
3835 SEARCH_NAME => "+1" RESULT
3836
3837 "abc" => "abd"
3838 "ab\xff" => "ac"
3839 "\xff" "a" "\xff" => "\xff" "b"
3840 "\xff" => ""
3841 "\xff\xff" => ""
3842 "" => ""
3843
3844 Then, with these symbols for example:
3845
3846 func
3847 func1
3848 fund
3849
3850 completing "func" looks for symbols between "func" and
3851 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3852 which finds "func" and "func1", but not "fund".
3853
3854 And with:
3855
3856 funcÿ (Latin1 'ÿ' [0xff])
3857 funcÿ1
3858 fund
3859
3860 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3861 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3862
3863 And with:
3864
3865 ÿÿ (Latin1 'ÿ' [0xff])
3866 ÿÿ1
3867
3868 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3869 the end of the list.
3870 */
3871 std::string after = search_name;
3872 while (!after.empty () && (unsigned char) after.back () == 0xff)
3873 after.pop_back ();
3874 if (!after.empty ())
3875 after.back () = (unsigned char) after.back () + 1;
3876 return after;
3877 }
3878
3879 /* See declaration. */
3880
3881 std::pair<std::vector<name_component>::const_iterator,
3882 std::vector<name_component>::const_iterator>
3883 mapped_index_base::find_name_components_bounds
3884 (const lookup_name_info &lookup_name_without_params, language lang) const
3885 {
3886 auto *name_cmp
3887 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3888
3889 const char *lang_name
3890 = lookup_name_without_params.language_lookup_name (lang);
3891
3892 /* Comparison function object for lower_bound that matches against a
3893 given symbol name. */
3894 auto lookup_compare_lower = [&] (const name_component &elem,
3895 const char *name)
3896 {
3897 const char *elem_qualified = this->symbol_name_at (elem.idx);
3898 const char *elem_name = elem_qualified + elem.name_offset;
3899 return name_cmp (elem_name, name) < 0;
3900 };
3901
3902 /* Comparison function object for upper_bound that matches against a
3903 given symbol name. */
3904 auto lookup_compare_upper = [&] (const char *name,
3905 const name_component &elem)
3906 {
3907 const char *elem_qualified = this->symbol_name_at (elem.idx);
3908 const char *elem_name = elem_qualified + elem.name_offset;
3909 return name_cmp (name, elem_name) < 0;
3910 };
3911
3912 auto begin = this->name_components.begin ();
3913 auto end = this->name_components.end ();
3914
3915 /* Find the lower bound. */
3916 auto lower = [&] ()
3917 {
3918 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3919 return begin;
3920 else
3921 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3922 } ();
3923
3924 /* Find the upper bound. */
3925 auto upper = [&] ()
3926 {
3927 if (lookup_name_without_params.completion_mode ())
3928 {
3929 /* In completion mode, we want UPPER to point past all
3930 symbols names that have the same prefix. I.e., with
3931 these symbols, and completing "func":
3932
3933 function << lower bound
3934 function1
3935 other_function << upper bound
3936
3937 We find the upper bound by looking for the insertion
3938 point of "func"-with-last-character-incremented,
3939 i.e. "fund". */
3940 std::string after = make_sort_after_prefix_name (lang_name);
3941 if (after.empty ())
3942 return end;
3943 return std::lower_bound (lower, end, after.c_str (),
3944 lookup_compare_lower);
3945 }
3946 else
3947 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3948 } ();
3949
3950 return {lower, upper};
3951 }
3952
3953 /* See declaration. */
3954
3955 void
3956 mapped_index_base::build_name_components ()
3957 {
3958 if (!this->name_components.empty ())
3959 return;
3960
3961 this->name_components_casing = case_sensitivity;
3962 auto *name_cmp
3963 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3964
3965 /* The code below only knows how to break apart components of C++
3966 symbol names (and other languages that use '::' as
3967 namespace/module separator) and Ada symbol names. */
3968 auto count = this->symbol_name_count ();
3969 for (offset_type idx = 0; idx < count; idx++)
3970 {
3971 if (this->symbol_name_slot_invalid (idx))
3972 continue;
3973
3974 const char *name = this->symbol_name_at (idx);
3975
3976 /* Add each name component to the name component table. */
3977 unsigned int previous_len = 0;
3978
3979 if (strstr (name, "::") != nullptr)
3980 {
3981 for (unsigned int current_len = cp_find_first_component (name);
3982 name[current_len] != '\0';
3983 current_len += cp_find_first_component (name + current_len))
3984 {
3985 gdb_assert (name[current_len] == ':');
3986 this->name_components.push_back ({previous_len, idx});
3987 /* Skip the '::'. */
3988 current_len += 2;
3989 previous_len = current_len;
3990 }
3991 }
3992 else
3993 {
3994 /* Handle the Ada encoded (aka mangled) form here. */
3995 for (const char *iter = strstr (name, "__");
3996 iter != nullptr;
3997 iter = strstr (iter, "__"))
3998 {
3999 this->name_components.push_back ({previous_len, idx});
4000 iter += 2;
4001 previous_len = iter - name;
4002 }
4003 }
4004
4005 this->name_components.push_back ({previous_len, idx});
4006 }
4007
4008 /* Sort name_components elements by name. */
4009 auto name_comp_compare = [&] (const name_component &left,
4010 const name_component &right)
4011 {
4012 const char *left_qualified = this->symbol_name_at (left.idx);
4013 const char *right_qualified = this->symbol_name_at (right.idx);
4014
4015 const char *left_name = left_qualified + left.name_offset;
4016 const char *right_name = right_qualified + right.name_offset;
4017
4018 return name_cmp (left_name, right_name) < 0;
4019 };
4020
4021 std::sort (this->name_components.begin (),
4022 this->name_components.end (),
4023 name_comp_compare);
4024 }
4025
4026 /* Helper for dw2_expand_symtabs_matching that works with a
4027 mapped_index_base instead of the containing objfile. This is split
4028 to a separate function in order to be able to unit test the
4029 name_components matching using a mock mapped_index_base. For each
4030 symbol name that matches, calls MATCH_CALLBACK, passing it the
4031 symbol's index in the mapped_index_base symbol table. */
4032
4033 static void
4034 dw2_expand_symtabs_matching_symbol
4035 (mapped_index_base &index,
4036 const lookup_name_info &lookup_name_in,
4037 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4038 enum search_domain kind,
4039 gdb::function_view<bool (offset_type)> match_callback)
4040 {
4041 lookup_name_info lookup_name_without_params
4042 = lookup_name_in.make_ignore_params ();
4043
4044 /* Build the symbol name component sorted vector, if we haven't
4045 yet. */
4046 index.build_name_components ();
4047
4048 /* The same symbol may appear more than once in the range though.
4049 E.g., if we're looking for symbols that complete "w", and we have
4050 a symbol named "w1::w2", we'll find the two name components for
4051 that same symbol in the range. To be sure we only call the
4052 callback once per symbol, we first collect the symbol name
4053 indexes that matched in a temporary vector and ignore
4054 duplicates. */
4055 std::vector<offset_type> matches;
4056
4057 struct name_and_matcher
4058 {
4059 symbol_name_matcher_ftype *matcher;
4060 const char *name;
4061
4062 bool operator== (const name_and_matcher &other) const
4063 {
4064 return matcher == other.matcher && strcmp (name, other.name) == 0;
4065 }
4066 };
4067
4068 /* A vector holding all the different symbol name matchers, for all
4069 languages. */
4070 std::vector<name_and_matcher> matchers;
4071
4072 for (int i = 0; i < nr_languages; i++)
4073 {
4074 enum language lang_e = (enum language) i;
4075
4076 const language_defn *lang = language_def (lang_e);
4077 symbol_name_matcher_ftype *name_matcher
4078 = get_symbol_name_matcher (lang, lookup_name_without_params);
4079
4080 name_and_matcher key {
4081 name_matcher,
4082 lookup_name_without_params.language_lookup_name (lang_e)
4083 };
4084
4085 /* Don't insert the same comparison routine more than once.
4086 Note that we do this linear walk. This is not a problem in
4087 practice because the number of supported languages is
4088 low. */
4089 if (std::find (matchers.begin (), matchers.end (), key)
4090 != matchers.end ())
4091 continue;
4092 matchers.push_back (std::move (key));
4093
4094 auto bounds
4095 = index.find_name_components_bounds (lookup_name_without_params,
4096 lang_e);
4097
4098 /* Now for each symbol name in range, check to see if we have a name
4099 match, and if so, call the MATCH_CALLBACK callback. */
4100
4101 for (; bounds.first != bounds.second; ++bounds.first)
4102 {
4103 const char *qualified = index.symbol_name_at (bounds.first->idx);
4104
4105 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4106 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4107 continue;
4108
4109 matches.push_back (bounds.first->idx);
4110 }
4111 }
4112
4113 std::sort (matches.begin (), matches.end ());
4114
4115 /* Finally call the callback, once per match. */
4116 ULONGEST prev = -1;
4117 for (offset_type idx : matches)
4118 {
4119 if (prev != idx)
4120 {
4121 if (!match_callback (idx))
4122 break;
4123 prev = idx;
4124 }
4125 }
4126
4127 /* Above we use a type wider than idx's for 'prev', since 0 and
4128 (offset_type)-1 are both possible values. */
4129 static_assert (sizeof (prev) > sizeof (offset_type), "");
4130 }
4131
4132 #if GDB_SELF_TEST
4133
4134 namespace selftests { namespace dw2_expand_symtabs_matching {
4135
4136 /* A mock .gdb_index/.debug_names-like name index table, enough to
4137 exercise dw2_expand_symtabs_matching_symbol, which works with the
4138 mapped_index_base interface. Builds an index from the symbol list
4139 passed as parameter to the constructor. */
4140 class mock_mapped_index : public mapped_index_base
4141 {
4142 public:
4143 mock_mapped_index (gdb::array_view<const char *> symbols)
4144 : m_symbol_table (symbols)
4145 {}
4146
4147 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4148
4149 /* Return the number of names in the symbol table. */
4150 size_t symbol_name_count () const override
4151 {
4152 return m_symbol_table.size ();
4153 }
4154
4155 /* Get the name of the symbol at IDX in the symbol table. */
4156 const char *symbol_name_at (offset_type idx) const override
4157 {
4158 return m_symbol_table[idx];
4159 }
4160
4161 private:
4162 gdb::array_view<const char *> m_symbol_table;
4163 };
4164
4165 /* Convenience function that converts a NULL pointer to a "<null>"
4166 string, to pass to print routines. */
4167
4168 static const char *
4169 string_or_null (const char *str)
4170 {
4171 return str != NULL ? str : "<null>";
4172 }
4173
4174 /* Check if a lookup_name_info built from
4175 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4176 index. EXPECTED_LIST is the list of expected matches, in expected
4177 matching order. If no match expected, then an empty list is
4178 specified. Returns true on success. On failure prints a warning
4179 indicating the file:line that failed, and returns false. */
4180
4181 static bool
4182 check_match (const char *file, int line,
4183 mock_mapped_index &mock_index,
4184 const char *name, symbol_name_match_type match_type,
4185 bool completion_mode,
4186 std::initializer_list<const char *> expected_list)
4187 {
4188 lookup_name_info lookup_name (name, match_type, completion_mode);
4189
4190 bool matched = true;
4191
4192 auto mismatch = [&] (const char *expected_str,
4193 const char *got)
4194 {
4195 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4196 "expected=\"%s\", got=\"%s\"\n"),
4197 file, line,
4198 (match_type == symbol_name_match_type::FULL
4199 ? "FULL" : "WILD"),
4200 name, string_or_null (expected_str), string_or_null (got));
4201 matched = false;
4202 };
4203
4204 auto expected_it = expected_list.begin ();
4205 auto expected_end = expected_list.end ();
4206
4207 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4208 NULL, ALL_DOMAIN,
4209 [&] (offset_type idx)
4210 {
4211 const char *matched_name = mock_index.symbol_name_at (idx);
4212 const char *expected_str
4213 = expected_it == expected_end ? NULL : *expected_it++;
4214
4215 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4216 mismatch (expected_str, matched_name);
4217 return true;
4218 });
4219
4220 const char *expected_str
4221 = expected_it == expected_end ? NULL : *expected_it++;
4222 if (expected_str != NULL)
4223 mismatch (expected_str, NULL);
4224
4225 return matched;
4226 }
4227
4228 /* The symbols added to the mock mapped_index for testing (in
4229 canonical form). */
4230 static const char *test_symbols[] = {
4231 "function",
4232 "std::bar",
4233 "std::zfunction",
4234 "std::zfunction2",
4235 "w1::w2",
4236 "ns::foo<char*>",
4237 "ns::foo<int>",
4238 "ns::foo<long>",
4239 "ns2::tmpl<int>::foo2",
4240 "(anonymous namespace)::A::B::C",
4241
4242 /* These are used to check that the increment-last-char in the
4243 matching algorithm for completion doesn't match "t1_fund" when
4244 completing "t1_func". */
4245 "t1_func",
4246 "t1_func1",
4247 "t1_fund",
4248 "t1_fund1",
4249
4250 /* A UTF-8 name with multi-byte sequences to make sure that
4251 cp-name-parser understands this as a single identifier ("função"
4252 is "function" in PT). */
4253 u8"u8função",
4254
4255 /* \377 (0xff) is Latin1 'ÿ'. */
4256 "yfunc\377",
4257
4258 /* \377 (0xff) is Latin1 'ÿ'. */
4259 "\377",
4260 "\377\377123",
4261
4262 /* A name with all sorts of complications. Starts with "z" to make
4263 it easier for the completion tests below. */
4264 #define Z_SYM_NAME \
4265 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4266 "::tuple<(anonymous namespace)::ui*, " \
4267 "std::default_delete<(anonymous namespace)::ui>, void>"
4268
4269 Z_SYM_NAME
4270 };
4271
4272 /* Returns true if the mapped_index_base::find_name_component_bounds
4273 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4274 in completion mode. */
4275
4276 static bool
4277 check_find_bounds_finds (mapped_index_base &index,
4278 const char *search_name,
4279 gdb::array_view<const char *> expected_syms)
4280 {
4281 lookup_name_info lookup_name (search_name,
4282 symbol_name_match_type::FULL, true);
4283
4284 auto bounds = index.find_name_components_bounds (lookup_name,
4285 language_cplus);
4286
4287 size_t distance = std::distance (bounds.first, bounds.second);
4288 if (distance != expected_syms.size ())
4289 return false;
4290
4291 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4292 {
4293 auto nc_elem = bounds.first + exp_elem;
4294 const char *qualified = index.symbol_name_at (nc_elem->idx);
4295 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4296 return false;
4297 }
4298
4299 return true;
4300 }
4301
4302 /* Test the lower-level mapped_index::find_name_component_bounds
4303 method. */
4304
4305 static void
4306 test_mapped_index_find_name_component_bounds ()
4307 {
4308 mock_mapped_index mock_index (test_symbols);
4309
4310 mock_index.build_name_components ();
4311
4312 /* Test the lower-level mapped_index::find_name_component_bounds
4313 method in completion mode. */
4314 {
4315 static const char *expected_syms[] = {
4316 "t1_func",
4317 "t1_func1",
4318 };
4319
4320 SELF_CHECK (check_find_bounds_finds (mock_index,
4321 "t1_func", expected_syms));
4322 }
4323
4324 /* Check that the increment-last-char in the name matching algorithm
4325 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4326 {
4327 static const char *expected_syms1[] = {
4328 "\377",
4329 "\377\377123",
4330 };
4331 SELF_CHECK (check_find_bounds_finds (mock_index,
4332 "\377", expected_syms1));
4333
4334 static const char *expected_syms2[] = {
4335 "\377\377123",
4336 };
4337 SELF_CHECK (check_find_bounds_finds (mock_index,
4338 "\377\377", expected_syms2));
4339 }
4340 }
4341
4342 /* Test dw2_expand_symtabs_matching_symbol. */
4343
4344 static void
4345 test_dw2_expand_symtabs_matching_symbol ()
4346 {
4347 mock_mapped_index mock_index (test_symbols);
4348
4349 /* We let all tests run until the end even if some fails, for debug
4350 convenience. */
4351 bool any_mismatch = false;
4352
4353 /* Create the expected symbols list (an initializer_list). Needed
4354 because lists have commas, and we need to pass them to CHECK,
4355 which is a macro. */
4356 #define EXPECT(...) { __VA_ARGS__ }
4357
4358 /* Wrapper for check_match that passes down the current
4359 __FILE__/__LINE__. */
4360 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4361 any_mismatch |= !check_match (__FILE__, __LINE__, \
4362 mock_index, \
4363 NAME, MATCH_TYPE, COMPLETION_MODE, \
4364 EXPECTED_LIST)
4365
4366 /* Identity checks. */
4367 for (const char *sym : test_symbols)
4368 {
4369 /* Should be able to match all existing symbols. */
4370 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4371 EXPECT (sym));
4372
4373 /* Should be able to match all existing symbols with
4374 parameters. */
4375 std::string with_params = std::string (sym) + "(int)";
4376 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4377 EXPECT (sym));
4378
4379 /* Should be able to match all existing symbols with
4380 parameters and qualifiers. */
4381 with_params = std::string (sym) + " ( int ) const";
4382 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4383 EXPECT (sym));
4384
4385 /* This should really find sym, but cp-name-parser.y doesn't
4386 know about lvalue/rvalue qualifiers yet. */
4387 with_params = std::string (sym) + " ( int ) &&";
4388 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4389 {});
4390 }
4391
4392 /* Check that the name matching algorithm for completion doesn't get
4393 confused with Latin1 'ÿ' / 0xff. */
4394 {
4395 static const char str[] = "\377";
4396 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4397 EXPECT ("\377", "\377\377123"));
4398 }
4399
4400 /* Check that the increment-last-char in the matching algorithm for
4401 completion doesn't match "t1_fund" when completing "t1_func". */
4402 {
4403 static const char str[] = "t1_func";
4404 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4405 EXPECT ("t1_func", "t1_func1"));
4406 }
4407
4408 /* Check that completion mode works at each prefix of the expected
4409 symbol name. */
4410 {
4411 static const char str[] = "function(int)";
4412 size_t len = strlen (str);
4413 std::string lookup;
4414
4415 for (size_t i = 1; i < len; i++)
4416 {
4417 lookup.assign (str, i);
4418 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4419 EXPECT ("function"));
4420 }
4421 }
4422
4423 /* While "w" is a prefix of both components, the match function
4424 should still only be called once. */
4425 {
4426 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4427 EXPECT ("w1::w2"));
4428 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4429 EXPECT ("w1::w2"));
4430 }
4431
4432 /* Same, with a "complicated" symbol. */
4433 {
4434 static const char str[] = Z_SYM_NAME;
4435 size_t len = strlen (str);
4436 std::string lookup;
4437
4438 for (size_t i = 1; i < len; i++)
4439 {
4440 lookup.assign (str, i);
4441 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4442 EXPECT (Z_SYM_NAME));
4443 }
4444 }
4445
4446 /* In FULL mode, an incomplete symbol doesn't match. */
4447 {
4448 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4449 {});
4450 }
4451
4452 /* A complete symbol with parameters matches any overload, since the
4453 index has no overload info. */
4454 {
4455 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4456 EXPECT ("std::zfunction", "std::zfunction2"));
4457 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4458 EXPECT ("std::zfunction", "std::zfunction2"));
4459 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4460 EXPECT ("std::zfunction", "std::zfunction2"));
4461 }
4462
4463 /* Check that whitespace is ignored appropriately. A symbol with a
4464 template argument list. */
4465 {
4466 static const char expected[] = "ns::foo<int>";
4467 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4468 EXPECT (expected));
4469 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4470 EXPECT (expected));
4471 }
4472
4473 /* Check that whitespace is ignored appropriately. A symbol with a
4474 template argument list that includes a pointer. */
4475 {
4476 static const char expected[] = "ns::foo<char*>";
4477 /* Try both completion and non-completion modes. */
4478 static const bool completion_mode[2] = {false, true};
4479 for (size_t i = 0; i < 2; i++)
4480 {
4481 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4482 completion_mode[i], EXPECT (expected));
4483 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4484 completion_mode[i], EXPECT (expected));
4485
4486 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4487 completion_mode[i], EXPECT (expected));
4488 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4489 completion_mode[i], EXPECT (expected));
4490 }
4491 }
4492
4493 {
4494 /* Check method qualifiers are ignored. */
4495 static const char expected[] = "ns::foo<char*>";
4496 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4497 symbol_name_match_type::FULL, true, EXPECT (expected));
4498 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4499 symbol_name_match_type::FULL, true, EXPECT (expected));
4500 CHECK_MATCH ("foo < char * > ( int ) const",
4501 symbol_name_match_type::WILD, true, EXPECT (expected));
4502 CHECK_MATCH ("foo < char * > ( int ) &&",
4503 symbol_name_match_type::WILD, true, EXPECT (expected));
4504 }
4505
4506 /* Test lookup names that don't match anything. */
4507 {
4508 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4509 {});
4510
4511 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4512 {});
4513 }
4514
4515 /* Some wild matching tests, exercising "(anonymous namespace)",
4516 which should not be confused with a parameter list. */
4517 {
4518 static const char *syms[] = {
4519 "A::B::C",
4520 "B::C",
4521 "C",
4522 "A :: B :: C ( int )",
4523 "B :: C ( int )",
4524 "C ( int )",
4525 };
4526
4527 for (const char *s : syms)
4528 {
4529 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4530 EXPECT ("(anonymous namespace)::A::B::C"));
4531 }
4532 }
4533
4534 {
4535 static const char expected[] = "ns2::tmpl<int>::foo2";
4536 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4537 EXPECT (expected));
4538 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4539 EXPECT (expected));
4540 }
4541
4542 SELF_CHECK (!any_mismatch);
4543
4544 #undef EXPECT
4545 #undef CHECK_MATCH
4546 }
4547
4548 static void
4549 run_test ()
4550 {
4551 test_mapped_index_find_name_component_bounds ();
4552 test_dw2_expand_symtabs_matching_symbol ();
4553 }
4554
4555 }} // namespace selftests::dw2_expand_symtabs_matching
4556
4557 #endif /* GDB_SELF_TEST */
4558
4559 /* If FILE_MATCHER is NULL or if PER_CU has
4560 dwarf2_per_cu_quick_data::MARK set (see
4561 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4562 EXPANSION_NOTIFY on it. */
4563
4564 static void
4565 dw2_expand_symtabs_matching_one
4566 (dwarf2_per_cu_data *per_cu,
4567 dwarf2_per_objfile *per_objfile,
4568 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4569 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4570 {
4571 if (file_matcher == NULL || per_cu->v.quick->mark)
4572 {
4573 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4574
4575 compunit_symtab *symtab
4576 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4577 gdb_assert (symtab != nullptr);
4578
4579 if (expansion_notify != NULL && symtab_was_null)
4580 expansion_notify (symtab);
4581 }
4582 }
4583
4584 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4585 matched, to expand corresponding CUs that were marked. IDX is the
4586 index of the symbol name that matched. */
4587
4588 static void
4589 dw2_expand_marked_cus
4590 (dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4591 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4592 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4593 search_domain kind)
4594 {
4595 offset_type *vec, vec_len, vec_idx;
4596 bool global_seen = false;
4597 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4598
4599 vec = (offset_type *) (index.constant_pool
4600 + MAYBE_SWAP (index.symbol_table[idx].vec));
4601 vec_len = MAYBE_SWAP (vec[0]);
4602 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4603 {
4604 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4605 /* This value is only valid for index versions >= 7. */
4606 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4607 gdb_index_symbol_kind symbol_kind =
4608 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4609 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4610 /* Only check the symbol attributes if they're present.
4611 Indices prior to version 7 don't record them,
4612 and indices >= 7 may elide them for certain symbols
4613 (gold does this). */
4614 int attrs_valid =
4615 (index.version >= 7
4616 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4617
4618 /* Work around gold/15646. */
4619 if (attrs_valid)
4620 {
4621 if (!is_static && global_seen)
4622 continue;
4623 if (!is_static)
4624 global_seen = true;
4625 }
4626
4627 /* Only check the symbol's kind if it has one. */
4628 if (attrs_valid)
4629 {
4630 switch (kind)
4631 {
4632 case VARIABLES_DOMAIN:
4633 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4634 continue;
4635 break;
4636 case FUNCTIONS_DOMAIN:
4637 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4638 continue;
4639 break;
4640 case TYPES_DOMAIN:
4641 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4642 continue;
4643 break;
4644 case MODULES_DOMAIN:
4645 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4646 continue;
4647 break;
4648 default:
4649 break;
4650 }
4651 }
4652
4653 /* Don't crash on bad data. */
4654 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4655 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
4656 {
4657 complaint (_(".gdb_index entry has bad CU index"
4658 " [in module %s]"),
4659 objfile_name (dwarf2_per_objfile->objfile));
4660 continue;
4661 }
4662
4663 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
4664 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, file_matcher,
4665 expansion_notify);
4666 }
4667 }
4668
4669 /* If FILE_MATCHER is non-NULL, set all the
4670 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4671 that match FILE_MATCHER. */
4672
4673 static void
4674 dw_expand_symtabs_matching_file_matcher
4675 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4676 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4677 {
4678 if (file_matcher == NULL)
4679 return;
4680
4681 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4682 htab_eq_pointer,
4683 NULL, xcalloc, xfree));
4684 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4685 htab_eq_pointer,
4686 NULL, xcalloc, xfree));
4687
4688 /* The rule is CUs specify all the files, including those used by
4689 any TU, so there's no need to scan TUs here. */
4690
4691 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4692 {
4693 QUIT;
4694
4695 per_cu->v.quick->mark = 0;
4696
4697 /* We only need to look at symtabs not already expanded. */
4698 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4699 continue;
4700
4701 quick_file_names *file_data
4702 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4703 if (file_data == NULL)
4704 continue;
4705
4706 if (htab_find (visited_not_found.get (), file_data) != NULL)
4707 continue;
4708 else if (htab_find (visited_found.get (), file_data) != NULL)
4709 {
4710 per_cu->v.quick->mark = 1;
4711 continue;
4712 }
4713
4714 for (int j = 0; j < file_data->num_file_names; ++j)
4715 {
4716 const char *this_real_name;
4717
4718 if (file_matcher (file_data->file_names[j], false))
4719 {
4720 per_cu->v.quick->mark = 1;
4721 break;
4722 }
4723
4724 /* Before we invoke realpath, which can get expensive when many
4725 files are involved, do a quick comparison of the basenames. */
4726 if (!basenames_may_differ
4727 && !file_matcher (lbasename (file_data->file_names[j]),
4728 true))
4729 continue;
4730
4731 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4732 file_data, j);
4733 if (file_matcher (this_real_name, false))
4734 {
4735 per_cu->v.quick->mark = 1;
4736 break;
4737 }
4738 }
4739
4740 void **slot = htab_find_slot (per_cu->v.quick->mark
4741 ? visited_found.get ()
4742 : visited_not_found.get (),
4743 file_data, INSERT);
4744 *slot = file_data;
4745 }
4746 }
4747
4748 static void
4749 dw2_expand_symtabs_matching
4750 (struct objfile *objfile,
4751 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4752 const lookup_name_info *lookup_name,
4753 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4754 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4755 enum search_domain kind)
4756 {
4757 struct dwarf2_per_objfile *dwarf2_per_objfile
4758 = get_dwarf2_per_objfile (objfile);
4759
4760 /* index_table is NULL if OBJF_READNOW. */
4761 if (!dwarf2_per_objfile->per_bfd->index_table)
4762 return;
4763
4764 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4765
4766 if (symbol_matcher == NULL && lookup_name == NULL)
4767 {
4768 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4769 {
4770 QUIT;
4771
4772 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
4773 file_matcher, expansion_notify);
4774 }
4775 return;
4776 }
4777
4778 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4779
4780 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4781 symbol_matcher,
4782 kind, [&] (offset_type idx)
4783 {
4784 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4785 expansion_notify, kind);
4786 return true;
4787 });
4788 }
4789
4790 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4791 symtab. */
4792
4793 static struct compunit_symtab *
4794 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4795 CORE_ADDR pc)
4796 {
4797 int i;
4798
4799 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4800 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4801 return cust;
4802
4803 if (cust->includes == NULL)
4804 return NULL;
4805
4806 for (i = 0; cust->includes[i]; ++i)
4807 {
4808 struct compunit_symtab *s = cust->includes[i];
4809
4810 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4811 if (s != NULL)
4812 return s;
4813 }
4814
4815 return NULL;
4816 }
4817
4818 static struct compunit_symtab *
4819 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4820 struct bound_minimal_symbol msymbol,
4821 CORE_ADDR pc,
4822 struct obj_section *section,
4823 int warn_if_readin)
4824 {
4825 struct dwarf2_per_cu_data *data;
4826 struct compunit_symtab *result;
4827
4828 if (!objfile->partial_symtabs->psymtabs_addrmap)
4829 return NULL;
4830
4831 CORE_ADDR baseaddr = objfile->text_section_offset ();
4832 data = (struct dwarf2_per_cu_data *) addrmap_find
4833 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4834 if (!data)
4835 return NULL;
4836
4837 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4838 if (warn_if_readin && per_objfile->symtab_set_p (data))
4839 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4840 paddress (objfile->arch (), pc));
4841
4842 result = recursively_find_pc_sect_compunit_symtab
4843 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4844
4845 gdb_assert (result != NULL);
4846 return result;
4847 }
4848
4849 static void
4850 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4851 void *data, int need_fullname)
4852 {
4853 struct dwarf2_per_objfile *dwarf2_per_objfile
4854 = get_dwarf2_per_objfile (objfile);
4855
4856 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
4857 {
4858 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
4859
4860 htab_up visited (htab_create_alloc (10,
4861 htab_hash_pointer, htab_eq_pointer,
4862 NULL, xcalloc, xfree));
4863
4864 /* The rule is CUs specify all the files, including those used
4865 by any TU, so there's no need to scan TUs here. We can
4866 ignore file names coming from already-expanded CUs. */
4867
4868 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4869 {
4870 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4871 {
4872 void **slot = htab_find_slot (visited.get (),
4873 per_cu->v.quick->file_names,
4874 INSERT);
4875
4876 *slot = per_cu->v.quick->file_names;
4877 }
4878 }
4879
4880 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4881 {
4882 /* We only need to look at symtabs not already expanded. */
4883 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4884 continue;
4885
4886 quick_file_names *file_data
4887 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4888 if (file_data == NULL)
4889 continue;
4890
4891 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4892 if (*slot)
4893 {
4894 /* Already visited. */
4895 continue;
4896 }
4897 *slot = file_data;
4898
4899 for (int j = 0; j < file_data->num_file_names; ++j)
4900 {
4901 const char *filename = file_data->file_names[j];
4902 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
4903 }
4904 }
4905 }
4906
4907 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4908 {
4909 gdb::unique_xmalloc_ptr<char> this_real_name;
4910
4911 if (need_fullname)
4912 this_real_name = gdb_realpath (filename);
4913 (*fun) (filename, this_real_name.get (), data);
4914 });
4915 }
4916
4917 static int
4918 dw2_has_symbols (struct objfile *objfile)
4919 {
4920 return 1;
4921 }
4922
4923 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4924 {
4925 dw2_has_symbols,
4926 dw2_find_last_source_symtab,
4927 dw2_forget_cached_source_info,
4928 dw2_map_symtabs_matching_filename,
4929 dw2_lookup_symbol,
4930 NULL,
4931 dw2_print_stats,
4932 dw2_dump,
4933 dw2_expand_symtabs_for_function,
4934 dw2_expand_all_symtabs,
4935 dw2_expand_symtabs_with_fullname,
4936 dw2_map_matching_symbols,
4937 dw2_expand_symtabs_matching,
4938 dw2_find_pc_sect_compunit_symtab,
4939 NULL,
4940 dw2_map_symbol_filenames
4941 };
4942
4943 /* DWARF-5 debug_names reader. */
4944
4945 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4946 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4947
4948 /* A helper function that reads the .debug_names section in SECTION
4949 and fills in MAP. FILENAME is the name of the file containing the
4950 section; it is used for error reporting.
4951
4952 Returns true if all went well, false otherwise. */
4953
4954 static bool
4955 read_debug_names_from_section (struct objfile *objfile,
4956 const char *filename,
4957 struct dwarf2_section_info *section,
4958 mapped_debug_names &map)
4959 {
4960 if (section->empty ())
4961 return false;
4962
4963 /* Older elfutils strip versions could keep the section in the main
4964 executable while splitting it for the separate debug info file. */
4965 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4966 return false;
4967
4968 section->read (objfile);
4969
4970 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4971
4972 const gdb_byte *addr = section->buffer;
4973
4974 bfd *const abfd = section->get_bfd_owner ();
4975
4976 unsigned int bytes_read;
4977 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4978 addr += bytes_read;
4979
4980 map.dwarf5_is_dwarf64 = bytes_read != 4;
4981 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4982 if (bytes_read + length != section->size)
4983 {
4984 /* There may be multiple per-CU indices. */
4985 warning (_("Section .debug_names in %s length %s does not match "
4986 "section length %s, ignoring .debug_names."),
4987 filename, plongest (bytes_read + length),
4988 pulongest (section->size));
4989 return false;
4990 }
4991
4992 /* The version number. */
4993 uint16_t version = read_2_bytes (abfd, addr);
4994 addr += 2;
4995 if (version != 5)
4996 {
4997 warning (_("Section .debug_names in %s has unsupported version %d, "
4998 "ignoring .debug_names."),
4999 filename, version);
5000 return false;
5001 }
5002
5003 /* Padding. */
5004 uint16_t padding = read_2_bytes (abfd, addr);
5005 addr += 2;
5006 if (padding != 0)
5007 {
5008 warning (_("Section .debug_names in %s has unsupported padding %d, "
5009 "ignoring .debug_names."),
5010 filename, padding);
5011 return false;
5012 }
5013
5014 /* comp_unit_count - The number of CUs in the CU list. */
5015 map.cu_count = read_4_bytes (abfd, addr);
5016 addr += 4;
5017
5018 /* local_type_unit_count - The number of TUs in the local TU
5019 list. */
5020 map.tu_count = read_4_bytes (abfd, addr);
5021 addr += 4;
5022
5023 /* foreign_type_unit_count - The number of TUs in the foreign TU
5024 list. */
5025 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5026 addr += 4;
5027 if (foreign_tu_count != 0)
5028 {
5029 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5030 "ignoring .debug_names."),
5031 filename, static_cast<unsigned long> (foreign_tu_count));
5032 return false;
5033 }
5034
5035 /* bucket_count - The number of hash buckets in the hash lookup
5036 table. */
5037 map.bucket_count = read_4_bytes (abfd, addr);
5038 addr += 4;
5039
5040 /* name_count - The number of unique names in the index. */
5041 map.name_count = read_4_bytes (abfd, addr);
5042 addr += 4;
5043
5044 /* abbrev_table_size - The size in bytes of the abbreviations
5045 table. */
5046 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5047 addr += 4;
5048
5049 /* augmentation_string_size - The size in bytes of the augmentation
5050 string. This value is rounded up to a multiple of 4. */
5051 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5052 addr += 4;
5053 map.augmentation_is_gdb = ((augmentation_string_size
5054 == sizeof (dwarf5_augmentation))
5055 && memcmp (addr, dwarf5_augmentation,
5056 sizeof (dwarf5_augmentation)) == 0);
5057 augmentation_string_size += (-augmentation_string_size) & 3;
5058 addr += augmentation_string_size;
5059
5060 /* List of CUs */
5061 map.cu_table_reordered = addr;
5062 addr += map.cu_count * map.offset_size;
5063
5064 /* List of Local TUs */
5065 map.tu_table_reordered = addr;
5066 addr += map.tu_count * map.offset_size;
5067
5068 /* Hash Lookup Table */
5069 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5070 addr += map.bucket_count * 4;
5071 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5072 addr += map.name_count * 4;
5073
5074 /* Name Table */
5075 map.name_table_string_offs_reordered = addr;
5076 addr += map.name_count * map.offset_size;
5077 map.name_table_entry_offs_reordered = addr;
5078 addr += map.name_count * map.offset_size;
5079
5080 const gdb_byte *abbrev_table_start = addr;
5081 for (;;)
5082 {
5083 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5084 addr += bytes_read;
5085 if (index_num == 0)
5086 break;
5087
5088 const auto insertpair
5089 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5090 if (!insertpair.second)
5091 {
5092 warning (_("Section .debug_names in %s has duplicate index %s, "
5093 "ignoring .debug_names."),
5094 filename, pulongest (index_num));
5095 return false;
5096 }
5097 mapped_debug_names::index_val &indexval = insertpair.first->second;
5098 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5099 addr += bytes_read;
5100
5101 for (;;)
5102 {
5103 mapped_debug_names::index_val::attr attr;
5104 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5105 addr += bytes_read;
5106 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5107 addr += bytes_read;
5108 if (attr.form == DW_FORM_implicit_const)
5109 {
5110 attr.implicit_const = read_signed_leb128 (abfd, addr,
5111 &bytes_read);
5112 addr += bytes_read;
5113 }
5114 if (attr.dw_idx == 0 && attr.form == 0)
5115 break;
5116 indexval.attr_vec.push_back (std::move (attr));
5117 }
5118 }
5119 if (addr != abbrev_table_start + abbrev_table_size)
5120 {
5121 warning (_("Section .debug_names in %s has abbreviation_table "
5122 "of size %s vs. written as %u, ignoring .debug_names."),
5123 filename, plongest (addr - abbrev_table_start),
5124 abbrev_table_size);
5125 return false;
5126 }
5127 map.entry_pool = addr;
5128
5129 return true;
5130 }
5131
5132 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5133 list. */
5134
5135 static void
5136 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5137 const mapped_debug_names &map,
5138 dwarf2_section_info &section,
5139 bool is_dwz)
5140 {
5141 if (!map.augmentation_is_gdb)
5142 {
5143 for (uint32_t i = 0; i < map.cu_count; ++i)
5144 {
5145 sect_offset sect_off
5146 = (sect_offset) (extract_unsigned_integer
5147 (map.cu_table_reordered + i * map.offset_size,
5148 map.offset_size,
5149 map.dwarf5_byte_order));
5150 /* We don't know the length of the CU, because the CU list in a
5151 .debug_names index can be incomplete, so we can't use the start of
5152 the next CU as end of this CU. We create the CUs here with length 0,
5153 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5154 dwarf2_per_cu_data *per_cu
5155 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5156 sect_off, 0);
5157 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
5158 }
5159 }
5160
5161 sect_offset sect_off_prev;
5162 for (uint32_t i = 0; i <= map.cu_count; ++i)
5163 {
5164 sect_offset sect_off_next;
5165 if (i < map.cu_count)
5166 {
5167 sect_off_next
5168 = (sect_offset) (extract_unsigned_integer
5169 (map.cu_table_reordered + i * map.offset_size,
5170 map.offset_size,
5171 map.dwarf5_byte_order));
5172 }
5173 else
5174 sect_off_next = (sect_offset) section.size;
5175 if (i >= 1)
5176 {
5177 const ULONGEST length = sect_off_next - sect_off_prev;
5178 dwarf2_per_cu_data *per_cu
5179 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5180 sect_off_prev, length);
5181 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
5182 }
5183 sect_off_prev = sect_off_next;
5184 }
5185 }
5186
5187 /* Read the CU list from the mapped index, and use it to create all
5188 the CU objects for this dwarf2_per_objfile. */
5189
5190 static void
5191 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5192 const mapped_debug_names &map,
5193 const mapped_debug_names &dwz_map)
5194 {
5195 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
5196 dwarf2_per_objfile->per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5197
5198 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5199 dwarf2_per_objfile->per_bfd->info,
5200 false /* is_dwz */);
5201
5202 if (dwz_map.cu_count == 0)
5203 return;
5204
5205 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
5206 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5207 true /* is_dwz */);
5208 }
5209
5210 /* Read .debug_names. If everything went ok, initialize the "quick"
5211 elements of all the CUs and return true. Otherwise, return false. */
5212
5213 static bool
5214 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5215 {
5216 std::unique_ptr<mapped_debug_names> map
5217 (new mapped_debug_names (dwarf2_per_objfile));
5218 mapped_debug_names dwz_map (dwarf2_per_objfile);
5219 struct objfile *objfile = dwarf2_per_objfile->objfile;
5220
5221 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5222 &dwarf2_per_objfile->per_bfd->debug_names,
5223 *map))
5224 return false;
5225
5226 /* Don't use the index if it's empty. */
5227 if (map->name_count == 0)
5228 return false;
5229
5230 /* If there is a .dwz file, read it so we can get its CU list as
5231 well. */
5232 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
5233 if (dwz != NULL)
5234 {
5235 if (!read_debug_names_from_section (objfile,
5236 bfd_get_filename (dwz->dwz_bfd.get ()),
5237 &dwz->debug_names, dwz_map))
5238 {
5239 warning (_("could not read '.debug_names' section from %s; skipping"),
5240 bfd_get_filename (dwz->dwz_bfd.get ()));
5241 return false;
5242 }
5243 }
5244
5245 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5246
5247 if (map->tu_count != 0)
5248 {
5249 /* We can only handle a single .debug_types when we have an
5250 index. */
5251 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
5252 return false;
5253
5254 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
5255
5256 create_signatured_type_table_from_debug_names
5257 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
5258 }
5259
5260 create_addrmap_from_aranges (dwarf2_per_objfile,
5261 &dwarf2_per_objfile->per_bfd->debug_aranges);
5262
5263 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5264 dwarf2_per_objfile->per_bfd->using_index = 1;
5265 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5266 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5267
5268 return true;
5269 }
5270
5271 /* Type used to manage iterating over all CUs looking for a symbol for
5272 .debug_names. */
5273
5274 class dw2_debug_names_iterator
5275 {
5276 public:
5277 dw2_debug_names_iterator (const mapped_debug_names &map,
5278 gdb::optional<block_enum> block_index,
5279 domain_enum domain,
5280 const char *name)
5281 : m_map (map), m_block_index (block_index), m_domain (domain),
5282 m_addr (find_vec_in_debug_names (map, name))
5283 {}
5284
5285 dw2_debug_names_iterator (const mapped_debug_names &map,
5286 search_domain search, uint32_t namei)
5287 : m_map (map),
5288 m_search (search),
5289 m_addr (find_vec_in_debug_names (map, namei))
5290 {}
5291
5292 dw2_debug_names_iterator (const mapped_debug_names &map,
5293 block_enum block_index, domain_enum domain,
5294 uint32_t namei)
5295 : m_map (map), m_block_index (block_index), m_domain (domain),
5296 m_addr (find_vec_in_debug_names (map, namei))
5297 {}
5298
5299 /* Return the next matching CU or NULL if there are no more. */
5300 dwarf2_per_cu_data *next ();
5301
5302 private:
5303 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5304 const char *name);
5305 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5306 uint32_t namei);
5307
5308 /* The internalized form of .debug_names. */
5309 const mapped_debug_names &m_map;
5310
5311 /* If set, only look for symbols that match that block. Valid values are
5312 GLOBAL_BLOCK and STATIC_BLOCK. */
5313 const gdb::optional<block_enum> m_block_index;
5314
5315 /* The kind of symbol we're looking for. */
5316 const domain_enum m_domain = UNDEF_DOMAIN;
5317 const search_domain m_search = ALL_DOMAIN;
5318
5319 /* The list of CUs from the index entry of the symbol, or NULL if
5320 not found. */
5321 const gdb_byte *m_addr;
5322 };
5323
5324 const char *
5325 mapped_debug_names::namei_to_name (uint32_t namei) const
5326 {
5327 const ULONGEST namei_string_offs
5328 = extract_unsigned_integer ((name_table_string_offs_reordered
5329 + namei * offset_size),
5330 offset_size,
5331 dwarf5_byte_order);
5332 return read_indirect_string_at_offset (dwarf2_per_objfile,
5333 namei_string_offs);
5334 }
5335
5336 /* Find a slot in .debug_names for the object named NAME. If NAME is
5337 found, return pointer to its pool data. If NAME cannot be found,
5338 return NULL. */
5339
5340 const gdb_byte *
5341 dw2_debug_names_iterator::find_vec_in_debug_names
5342 (const mapped_debug_names &map, const char *name)
5343 {
5344 int (*cmp) (const char *, const char *);
5345
5346 gdb::unique_xmalloc_ptr<char> without_params;
5347 if (current_language->la_language == language_cplus
5348 || current_language->la_language == language_fortran
5349 || current_language->la_language == language_d)
5350 {
5351 /* NAME is already canonical. Drop any qualifiers as
5352 .debug_names does not contain any. */
5353
5354 if (strchr (name, '(') != NULL)
5355 {
5356 without_params = cp_remove_params (name);
5357 if (without_params != NULL)
5358 name = without_params.get ();
5359 }
5360 }
5361
5362 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5363
5364 const uint32_t full_hash = dwarf5_djb_hash (name);
5365 uint32_t namei
5366 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5367 (map.bucket_table_reordered
5368 + (full_hash % map.bucket_count)), 4,
5369 map.dwarf5_byte_order);
5370 if (namei == 0)
5371 return NULL;
5372 --namei;
5373 if (namei >= map.name_count)
5374 {
5375 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5376 "[in module %s]"),
5377 namei, map.name_count,
5378 objfile_name (map.dwarf2_per_objfile->objfile));
5379 return NULL;
5380 }
5381
5382 for (;;)
5383 {
5384 const uint32_t namei_full_hash
5385 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5386 (map.hash_table_reordered + namei), 4,
5387 map.dwarf5_byte_order);
5388 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5389 return NULL;
5390
5391 if (full_hash == namei_full_hash)
5392 {
5393 const char *const namei_string = map.namei_to_name (namei);
5394
5395 #if 0 /* An expensive sanity check. */
5396 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5397 {
5398 complaint (_("Wrong .debug_names hash for string at index %u "
5399 "[in module %s]"),
5400 namei, objfile_name (dwarf2_per_objfile->objfile));
5401 return NULL;
5402 }
5403 #endif
5404
5405 if (cmp (namei_string, name) == 0)
5406 {
5407 const ULONGEST namei_entry_offs
5408 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5409 + namei * map.offset_size),
5410 map.offset_size, map.dwarf5_byte_order);
5411 return map.entry_pool + namei_entry_offs;
5412 }
5413 }
5414
5415 ++namei;
5416 if (namei >= map.name_count)
5417 return NULL;
5418 }
5419 }
5420
5421 const gdb_byte *
5422 dw2_debug_names_iterator::find_vec_in_debug_names
5423 (const mapped_debug_names &map, uint32_t namei)
5424 {
5425 if (namei >= map.name_count)
5426 {
5427 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5428 "[in module %s]"),
5429 namei, map.name_count,
5430 objfile_name (map.dwarf2_per_objfile->objfile));
5431 return NULL;
5432 }
5433
5434 const ULONGEST namei_entry_offs
5435 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5436 + namei * map.offset_size),
5437 map.offset_size, map.dwarf5_byte_order);
5438 return map.entry_pool + namei_entry_offs;
5439 }
5440
5441 /* See dw2_debug_names_iterator. */
5442
5443 dwarf2_per_cu_data *
5444 dw2_debug_names_iterator::next ()
5445 {
5446 if (m_addr == NULL)
5447 return NULL;
5448
5449 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5450 struct objfile *objfile = dwarf2_per_objfile->objfile;
5451 bfd *const abfd = objfile->obfd;
5452
5453 again:
5454
5455 unsigned int bytes_read;
5456 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5457 m_addr += bytes_read;
5458 if (abbrev == 0)
5459 return NULL;
5460
5461 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5462 if (indexval_it == m_map.abbrev_map.cend ())
5463 {
5464 complaint (_("Wrong .debug_names undefined abbrev code %s "
5465 "[in module %s]"),
5466 pulongest (abbrev), objfile_name (objfile));
5467 return NULL;
5468 }
5469 const mapped_debug_names::index_val &indexval = indexval_it->second;
5470 enum class symbol_linkage {
5471 unknown,
5472 static_,
5473 extern_,
5474 } symbol_linkage_ = symbol_linkage::unknown;
5475 dwarf2_per_cu_data *per_cu = NULL;
5476 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5477 {
5478 ULONGEST ull;
5479 switch (attr.form)
5480 {
5481 case DW_FORM_implicit_const:
5482 ull = attr.implicit_const;
5483 break;
5484 case DW_FORM_flag_present:
5485 ull = 1;
5486 break;
5487 case DW_FORM_udata:
5488 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5489 m_addr += bytes_read;
5490 break;
5491 case DW_FORM_ref4:
5492 ull = read_4_bytes (abfd, m_addr);
5493 m_addr += 4;
5494 break;
5495 case DW_FORM_ref8:
5496 ull = read_8_bytes (abfd, m_addr);
5497 m_addr += 8;
5498 break;
5499 case DW_FORM_ref_sig8:
5500 ull = read_8_bytes (abfd, m_addr);
5501 m_addr += 8;
5502 break;
5503 default:
5504 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5505 dwarf_form_name (attr.form),
5506 objfile_name (objfile));
5507 return NULL;
5508 }
5509 switch (attr.dw_idx)
5510 {
5511 case DW_IDX_compile_unit:
5512 /* Don't crash on bad data. */
5513 if (ull >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
5514 {
5515 complaint (_(".debug_names entry has bad CU index %s"
5516 " [in module %s]"),
5517 pulongest (ull),
5518 objfile_name (dwarf2_per_objfile->objfile));
5519 continue;
5520 }
5521 per_cu = dwarf2_per_objfile->per_bfd->get_cutu (ull);
5522 break;
5523 case DW_IDX_type_unit:
5524 /* Don't crash on bad data. */
5525 if (ull >= dwarf2_per_objfile->per_bfd->all_type_units.size ())
5526 {
5527 complaint (_(".debug_names entry has bad TU index %s"
5528 " [in module %s]"),
5529 pulongest (ull),
5530 objfile_name (dwarf2_per_objfile->objfile));
5531 continue;
5532 }
5533 per_cu = &dwarf2_per_objfile->per_bfd->get_tu (ull)->per_cu;
5534 break;
5535 case DW_IDX_die_offset:
5536 /* In a per-CU index (as opposed to a per-module index), index
5537 entries without CU attribute implicitly refer to the single CU. */
5538 if (per_cu == NULL)
5539 per_cu = dwarf2_per_objfile->per_bfd->get_cu (0);
5540 break;
5541 case DW_IDX_GNU_internal:
5542 if (!m_map.augmentation_is_gdb)
5543 break;
5544 symbol_linkage_ = symbol_linkage::static_;
5545 break;
5546 case DW_IDX_GNU_external:
5547 if (!m_map.augmentation_is_gdb)
5548 break;
5549 symbol_linkage_ = symbol_linkage::extern_;
5550 break;
5551 }
5552 }
5553
5554 /* Skip if already read in. */
5555 if (dwarf2_per_objfile->symtab_set_p (per_cu))
5556 goto again;
5557
5558 /* Check static vs global. */
5559 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5560 {
5561 const bool want_static = *m_block_index == STATIC_BLOCK;
5562 const bool symbol_is_static =
5563 symbol_linkage_ == symbol_linkage::static_;
5564 if (want_static != symbol_is_static)
5565 goto again;
5566 }
5567
5568 /* Match dw2_symtab_iter_next, symbol_kind
5569 and debug_names::psymbol_tag. */
5570 switch (m_domain)
5571 {
5572 case VAR_DOMAIN:
5573 switch (indexval.dwarf_tag)
5574 {
5575 case DW_TAG_variable:
5576 case DW_TAG_subprogram:
5577 /* Some types are also in VAR_DOMAIN. */
5578 case DW_TAG_typedef:
5579 case DW_TAG_structure_type:
5580 break;
5581 default:
5582 goto again;
5583 }
5584 break;
5585 case STRUCT_DOMAIN:
5586 switch (indexval.dwarf_tag)
5587 {
5588 case DW_TAG_typedef:
5589 case DW_TAG_structure_type:
5590 break;
5591 default:
5592 goto again;
5593 }
5594 break;
5595 case LABEL_DOMAIN:
5596 switch (indexval.dwarf_tag)
5597 {
5598 case 0:
5599 case DW_TAG_variable:
5600 break;
5601 default:
5602 goto again;
5603 }
5604 break;
5605 case MODULE_DOMAIN:
5606 switch (indexval.dwarf_tag)
5607 {
5608 case DW_TAG_module:
5609 break;
5610 default:
5611 goto again;
5612 }
5613 break;
5614 default:
5615 break;
5616 }
5617
5618 /* Match dw2_expand_symtabs_matching, symbol_kind and
5619 debug_names::psymbol_tag. */
5620 switch (m_search)
5621 {
5622 case VARIABLES_DOMAIN:
5623 switch (indexval.dwarf_tag)
5624 {
5625 case DW_TAG_variable:
5626 break;
5627 default:
5628 goto again;
5629 }
5630 break;
5631 case FUNCTIONS_DOMAIN:
5632 switch (indexval.dwarf_tag)
5633 {
5634 case DW_TAG_subprogram:
5635 break;
5636 default:
5637 goto again;
5638 }
5639 break;
5640 case TYPES_DOMAIN:
5641 switch (indexval.dwarf_tag)
5642 {
5643 case DW_TAG_typedef:
5644 case DW_TAG_structure_type:
5645 break;
5646 default:
5647 goto again;
5648 }
5649 break;
5650 case MODULES_DOMAIN:
5651 switch (indexval.dwarf_tag)
5652 {
5653 case DW_TAG_module:
5654 break;
5655 default:
5656 goto again;
5657 }
5658 default:
5659 break;
5660 }
5661
5662 return per_cu;
5663 }
5664
5665 static struct compunit_symtab *
5666 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5667 const char *name, domain_enum domain)
5668 {
5669 struct dwarf2_per_objfile *dwarf2_per_objfile
5670 = get_dwarf2_per_objfile (objfile);
5671
5672 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
5673 if (!mapp)
5674 {
5675 /* index is NULL if OBJF_READNOW. */
5676 return NULL;
5677 }
5678 const auto &map = *mapp;
5679
5680 dw2_debug_names_iterator iter (map, block_index, domain, name);
5681
5682 struct compunit_symtab *stab_best = NULL;
5683 struct dwarf2_per_cu_data *per_cu;
5684 while ((per_cu = iter.next ()) != NULL)
5685 {
5686 struct symbol *sym, *with_opaque = NULL;
5687 compunit_symtab *stab
5688 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5689 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5690 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5691
5692 sym = block_find_symbol (block, name, domain,
5693 block_find_non_opaque_type_preferred,
5694 &with_opaque);
5695
5696 /* Some caution must be observed with overloaded functions and
5697 methods, since the index will not contain any overload
5698 information (but NAME might contain it). */
5699
5700 if (sym != NULL
5701 && strcmp_iw (sym->search_name (), name) == 0)
5702 return stab;
5703 if (with_opaque != NULL
5704 && strcmp_iw (with_opaque->search_name (), name) == 0)
5705 stab_best = stab;
5706
5707 /* Keep looking through other CUs. */
5708 }
5709
5710 return stab_best;
5711 }
5712
5713 /* This dumps minimal information about .debug_names. It is called
5714 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5715 uses this to verify that .debug_names has been loaded. */
5716
5717 static void
5718 dw2_debug_names_dump (struct objfile *objfile)
5719 {
5720 struct dwarf2_per_objfile *dwarf2_per_objfile
5721 = get_dwarf2_per_objfile (objfile);
5722
5723 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
5724 printf_filtered (".debug_names:");
5725 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5726 printf_filtered (" exists\n");
5727 else
5728 printf_filtered (" faked for \"readnow\"\n");
5729 printf_filtered ("\n");
5730 }
5731
5732 static void
5733 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5734 const char *func_name)
5735 {
5736 struct dwarf2_per_objfile *dwarf2_per_objfile
5737 = get_dwarf2_per_objfile (objfile);
5738
5739 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5740 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5741 {
5742 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5743
5744 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5745
5746 struct dwarf2_per_cu_data *per_cu;
5747 while ((per_cu = iter.next ()) != NULL)
5748 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5749 }
5750 }
5751
5752 static void
5753 dw2_debug_names_map_matching_symbols
5754 (struct objfile *objfile,
5755 const lookup_name_info &name, domain_enum domain,
5756 int global,
5757 gdb::function_view<symbol_found_callback_ftype> callback,
5758 symbol_compare_ftype *ordered_compare)
5759 {
5760 struct dwarf2_per_objfile *dwarf2_per_objfile
5761 = get_dwarf2_per_objfile (objfile);
5762
5763 /* debug_names_table is NULL if OBJF_READNOW. */
5764 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5765 return;
5766
5767 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5768 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5769
5770 const char *match_name = name.ada ().lookup_name ().c_str ();
5771 auto matcher = [&] (const char *symname)
5772 {
5773 if (ordered_compare == nullptr)
5774 return true;
5775 return ordered_compare (symname, match_name) == 0;
5776 };
5777
5778 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5779 [&] (offset_type namei)
5780 {
5781 /* The name was matched, now expand corresponding CUs that were
5782 marked. */
5783 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5784
5785 struct dwarf2_per_cu_data *per_cu;
5786 while ((per_cu = iter.next ()) != NULL)
5787 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
5788 nullptr);
5789 return true;
5790 });
5791
5792 /* It's a shame we couldn't do this inside the
5793 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5794 that have already been expanded. Instead, this loop matches what
5795 the psymtab code does. */
5796 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5797 {
5798 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5799 if (symtab != nullptr)
5800 {
5801 const struct block *block
5802 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5803 if (!iterate_over_symbols_terminated (block, name,
5804 domain, callback))
5805 break;
5806 }
5807 }
5808 }
5809
5810 static void
5811 dw2_debug_names_expand_symtabs_matching
5812 (struct objfile *objfile,
5813 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5814 const lookup_name_info *lookup_name,
5815 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5816 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5817 enum search_domain kind)
5818 {
5819 struct dwarf2_per_objfile *dwarf2_per_objfile
5820 = get_dwarf2_per_objfile (objfile);
5821
5822 /* debug_names_table is NULL if OBJF_READNOW. */
5823 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5824 return;
5825
5826 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5827
5828 if (symbol_matcher == NULL && lookup_name == NULL)
5829 {
5830 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5831 {
5832 QUIT;
5833
5834 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5835 file_matcher, expansion_notify);
5836 }
5837 return;
5838 }
5839
5840 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5841
5842 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5843 symbol_matcher,
5844 kind, [&] (offset_type namei)
5845 {
5846 /* The name was matched, now expand corresponding CUs that were
5847 marked. */
5848 dw2_debug_names_iterator iter (map, kind, namei);
5849
5850 struct dwarf2_per_cu_data *per_cu;
5851 while ((per_cu = iter.next ()) != NULL)
5852 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5853 file_matcher, expansion_notify);
5854 return true;
5855 });
5856 }
5857
5858 const struct quick_symbol_functions dwarf2_debug_names_functions =
5859 {
5860 dw2_has_symbols,
5861 dw2_find_last_source_symtab,
5862 dw2_forget_cached_source_info,
5863 dw2_map_symtabs_matching_filename,
5864 dw2_debug_names_lookup_symbol,
5865 NULL,
5866 dw2_print_stats,
5867 dw2_debug_names_dump,
5868 dw2_debug_names_expand_symtabs_for_function,
5869 dw2_expand_all_symtabs,
5870 dw2_expand_symtabs_with_fullname,
5871 dw2_debug_names_map_matching_symbols,
5872 dw2_debug_names_expand_symtabs_matching,
5873 dw2_find_pc_sect_compunit_symtab,
5874 NULL,
5875 dw2_map_symbol_filenames
5876 };
5877
5878 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5879 to either a dwarf2_per_bfd or dwz_file object. */
5880
5881 template <typename T>
5882 static gdb::array_view<const gdb_byte>
5883 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5884 {
5885 dwarf2_section_info *section = &section_owner->gdb_index;
5886
5887 if (section->empty ())
5888 return {};
5889
5890 /* Older elfutils strip versions could keep the section in the main
5891 executable while splitting it for the separate debug info file. */
5892 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5893 return {};
5894
5895 section->read (obj);
5896
5897 /* dwarf2_section_info::size is a bfd_size_type, while
5898 gdb::array_view works with size_t. On 32-bit hosts, with
5899 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5900 is 32-bit. So we need an explicit narrowing conversion here.
5901 This is fine, because it's impossible to allocate or mmap an
5902 array/buffer larger than what size_t can represent. */
5903 return gdb::make_array_view (section->buffer, section->size);
5904 }
5905
5906 /* Lookup the index cache for the contents of the index associated to
5907 DWARF2_OBJ. */
5908
5909 static gdb::array_view<const gdb_byte>
5910 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5911 {
5912 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5913 if (build_id == nullptr)
5914 return {};
5915
5916 return global_index_cache.lookup_gdb_index (build_id,
5917 &dwarf2_per_bfd->index_cache_res);
5918 }
5919
5920 /* Same as the above, but for DWZ. */
5921
5922 static gdb::array_view<const gdb_byte>
5923 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5924 {
5925 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5926 if (build_id == nullptr)
5927 return {};
5928
5929 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5930 }
5931
5932 /* See symfile.h. */
5933
5934 bool
5935 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5936 {
5937 struct dwarf2_per_objfile *dwarf2_per_objfile
5938 = get_dwarf2_per_objfile (objfile);
5939
5940 /* If we're about to read full symbols, don't bother with the
5941 indices. In this case we also don't care if some other debug
5942 format is making psymtabs, because they are all about to be
5943 expanded anyway. */
5944 if ((objfile->flags & OBJF_READNOW))
5945 {
5946 dwarf2_per_objfile->per_bfd->using_index = 1;
5947 create_all_comp_units (dwarf2_per_objfile);
5948 create_all_type_units (dwarf2_per_objfile);
5949 dwarf2_per_objfile->per_bfd->quick_file_names_table
5950 = create_quick_file_names_table
5951 (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5952 dwarf2_per_objfile->resize_symtabs ();
5953
5954 for (int i = 0; i < (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
5955 + dwarf2_per_objfile->per_bfd->all_type_units.size ()); ++i)
5956 {
5957 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
5958
5959 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
5960 struct dwarf2_per_cu_quick_data);
5961 }
5962
5963 /* Return 1 so that gdb sees the "quick" functions. However,
5964 these functions will be no-ops because we will have expanded
5965 all symtabs. */
5966 *index_kind = dw_index_kind::GDB_INDEX;
5967 return true;
5968 }
5969
5970 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5971 {
5972 *index_kind = dw_index_kind::DEBUG_NAMES;
5973 dwarf2_per_objfile->resize_symtabs ();
5974 return true;
5975 }
5976
5977 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5978 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5979 get_gdb_index_contents_from_section<dwz_file>))
5980 {
5981 *index_kind = dw_index_kind::GDB_INDEX;
5982 dwarf2_per_objfile->resize_symtabs ();
5983 return true;
5984 }
5985
5986 /* ... otherwise, try to find the index in the index cache. */
5987 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5988 get_gdb_index_contents_from_cache,
5989 get_gdb_index_contents_from_cache_dwz))
5990 {
5991 global_index_cache.hit ();
5992 *index_kind = dw_index_kind::GDB_INDEX;
5993 dwarf2_per_objfile->resize_symtabs ();
5994 return true;
5995 }
5996
5997 global_index_cache.miss ();
5998 return false;
5999 }
6000
6001 \f
6002
6003 /* Build a partial symbol table. */
6004
6005 void
6006 dwarf2_build_psymtabs (struct objfile *objfile)
6007 {
6008 struct dwarf2_per_objfile *dwarf2_per_objfile
6009 = get_dwarf2_per_objfile (objfile);
6010
6011 init_psymbol_list (objfile, 1024);
6012
6013 try
6014 {
6015 /* This isn't really ideal: all the data we allocate on the
6016 objfile's obstack is still uselessly kept around. However,
6017 freeing it seems unsafe. */
6018 psymtab_discarder psymtabs (objfile);
6019 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6020 psymtabs.keep ();
6021
6022 dwarf2_per_objfile->resize_symtabs ();
6023
6024 /* (maybe) store an index in the cache. */
6025 global_index_cache.store (dwarf2_per_objfile);
6026 }
6027 catch (const gdb_exception_error &except)
6028 {
6029 exception_print (gdb_stderr, except);
6030 }
6031 }
6032
6033 /* Find the base address of the compilation unit for range lists and
6034 location lists. It will normally be specified by DW_AT_low_pc.
6035 In DWARF-3 draft 4, the base address could be overridden by
6036 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6037 compilation units with discontinuous ranges. */
6038
6039 static void
6040 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6041 {
6042 struct attribute *attr;
6043
6044 cu->base_address.reset ();
6045
6046 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6047 if (attr != nullptr)
6048 cu->base_address = attr->value_as_address ();
6049 else
6050 {
6051 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6052 if (attr != nullptr)
6053 cu->base_address = attr->value_as_address ();
6054 }
6055 }
6056
6057 /* Helper function that returns the proper abbrev section for
6058 THIS_CU. */
6059
6060 static struct dwarf2_section_info *
6061 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6062 {
6063 struct dwarf2_section_info *abbrev;
6064 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6065
6066 if (this_cu->is_dwz)
6067 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6068 else
6069 abbrev = &per_bfd->abbrev;
6070
6071 return abbrev;
6072 }
6073
6074 /* Fetch the abbreviation table offset from a comp or type unit header. */
6075
6076 static sect_offset
6077 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6078 struct dwarf2_section_info *section,
6079 sect_offset sect_off)
6080 {
6081 bfd *abfd = section->get_bfd_owner ();
6082 const gdb_byte *info_ptr;
6083 unsigned int initial_length_size, offset_size;
6084 uint16_t version;
6085
6086 section->read (dwarf2_per_objfile->objfile);
6087 info_ptr = section->buffer + to_underlying (sect_off);
6088 read_initial_length (abfd, info_ptr, &initial_length_size);
6089 offset_size = initial_length_size == 4 ? 4 : 8;
6090 info_ptr += initial_length_size;
6091
6092 version = read_2_bytes (abfd, info_ptr);
6093 info_ptr += 2;
6094 if (version >= 5)
6095 {
6096 /* Skip unit type and address size. */
6097 info_ptr += 2;
6098 }
6099
6100 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6101 }
6102
6103 /* A partial symtab that is used only for include files. */
6104 struct dwarf2_include_psymtab : public partial_symtab
6105 {
6106 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6107 : partial_symtab (filename, objfile)
6108 {
6109 }
6110
6111 void read_symtab (struct objfile *objfile) override
6112 {
6113 /* It's an include file, no symbols to read for it.
6114 Everything is in the includer symtab. */
6115
6116 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6117 expansion of the includer psymtab. We use the dependencies[0] field to
6118 model the includer. But if we go the regular route of calling
6119 expand_psymtab here, and having expand_psymtab call expand_dependencies
6120 to expand the includer, we'll only use expand_psymtab on the includer
6121 (making it a non-toplevel psymtab), while if we expand the includer via
6122 another path, we'll use read_symtab (making it a toplevel psymtab).
6123 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6124 psymtab, and trigger read_symtab on the includer here directly. */
6125 includer ()->read_symtab (objfile);
6126 }
6127
6128 void expand_psymtab (struct objfile *objfile) override
6129 {
6130 /* This is not called by read_symtab, and should not be called by any
6131 expand_dependencies. */
6132 gdb_assert (false);
6133 }
6134
6135 bool readin_p (struct objfile *objfile) const override
6136 {
6137 return includer ()->readin_p (objfile);
6138 }
6139
6140 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6141 {
6142 return nullptr;
6143 }
6144
6145 private:
6146 partial_symtab *includer () const
6147 {
6148 /* An include psymtab has exactly one dependency: the psymtab that
6149 includes it. */
6150 gdb_assert (this->number_of_dependencies == 1);
6151 return this->dependencies[0];
6152 }
6153 };
6154
6155 /* Allocate a new partial symtab for file named NAME and mark this new
6156 partial symtab as being an include of PST. */
6157
6158 static void
6159 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6160 struct objfile *objfile)
6161 {
6162 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6163
6164 if (!IS_ABSOLUTE_PATH (subpst->filename))
6165 subpst->dirname = pst->dirname;
6166
6167 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6168 subpst->dependencies[0] = pst;
6169 subpst->number_of_dependencies = 1;
6170 }
6171
6172 /* Read the Line Number Program data and extract the list of files
6173 included by the source file represented by PST. Build an include
6174 partial symtab for each of these included files. */
6175
6176 static void
6177 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6178 struct die_info *die,
6179 dwarf2_psymtab *pst)
6180 {
6181 line_header_up lh;
6182 struct attribute *attr;
6183
6184 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6185 if (attr != nullptr)
6186 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6187 if (lh == NULL)
6188 return; /* No linetable, so no includes. */
6189
6190 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6191 that we pass in the raw text_low here; that is ok because we're
6192 only decoding the line table to make include partial symtabs, and
6193 so the addresses aren't really used. */
6194 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6195 pst->raw_text_low (), 1);
6196 }
6197
6198 static hashval_t
6199 hash_signatured_type (const void *item)
6200 {
6201 const struct signatured_type *sig_type
6202 = (const struct signatured_type *) item;
6203
6204 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6205 return sig_type->signature;
6206 }
6207
6208 static int
6209 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6210 {
6211 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6212 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6213
6214 return lhs->signature == rhs->signature;
6215 }
6216
6217 /* Allocate a hash table for signatured types. */
6218
6219 static htab_up
6220 allocate_signatured_type_table ()
6221 {
6222 return htab_up (htab_create_alloc (41,
6223 hash_signatured_type,
6224 eq_signatured_type,
6225 NULL, xcalloc, xfree));
6226 }
6227
6228 /* A helper function to add a signatured type CU to a table. */
6229
6230 static int
6231 add_signatured_type_cu_to_table (void **slot, void *datum)
6232 {
6233 struct signatured_type *sigt = (struct signatured_type *) *slot;
6234 std::vector<signatured_type *> *all_type_units
6235 = (std::vector<signatured_type *> *) datum;
6236
6237 all_type_units->push_back (sigt);
6238
6239 return 1;
6240 }
6241
6242 /* A helper for create_debug_types_hash_table. Read types from SECTION
6243 and fill them into TYPES_HTAB. It will process only type units,
6244 therefore DW_UT_type. */
6245
6246 static void
6247 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6248 struct dwo_file *dwo_file,
6249 dwarf2_section_info *section, htab_up &types_htab,
6250 rcuh_kind section_kind)
6251 {
6252 struct objfile *objfile = dwarf2_per_objfile->objfile;
6253 struct dwarf2_section_info *abbrev_section;
6254 bfd *abfd;
6255 const gdb_byte *info_ptr, *end_ptr;
6256
6257 abbrev_section = (dwo_file != NULL
6258 ? &dwo_file->sections.abbrev
6259 : &dwarf2_per_objfile->per_bfd->abbrev);
6260
6261 if (dwarf_read_debug)
6262 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6263 section->get_name (),
6264 abbrev_section->get_file_name ());
6265
6266 section->read (objfile);
6267 info_ptr = section->buffer;
6268
6269 if (info_ptr == NULL)
6270 return;
6271
6272 /* We can't set abfd until now because the section may be empty or
6273 not present, in which case the bfd is unknown. */
6274 abfd = section->get_bfd_owner ();
6275
6276 /* We don't use cutu_reader here because we don't need to read
6277 any dies: the signature is in the header. */
6278
6279 end_ptr = info_ptr + section->size;
6280 while (info_ptr < end_ptr)
6281 {
6282 struct signatured_type *sig_type;
6283 struct dwo_unit *dwo_tu;
6284 void **slot;
6285 const gdb_byte *ptr = info_ptr;
6286 struct comp_unit_head header;
6287 unsigned int length;
6288
6289 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6290
6291 /* Initialize it due to a false compiler warning. */
6292 header.signature = -1;
6293 header.type_cu_offset_in_tu = (cu_offset) -1;
6294
6295 /* We need to read the type's signature in order to build the hash
6296 table, but we don't need anything else just yet. */
6297
6298 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6299 abbrev_section, ptr, section_kind);
6300
6301 length = header.get_length ();
6302
6303 /* Skip dummy type units. */
6304 if (ptr >= info_ptr + length
6305 || peek_abbrev_code (abfd, ptr) == 0
6306 || header.unit_type != DW_UT_type)
6307 {
6308 info_ptr += length;
6309 continue;
6310 }
6311
6312 if (types_htab == NULL)
6313 {
6314 if (dwo_file)
6315 types_htab = allocate_dwo_unit_table ();
6316 else
6317 types_htab = allocate_signatured_type_table ();
6318 }
6319
6320 if (dwo_file)
6321 {
6322 sig_type = NULL;
6323 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6324 struct dwo_unit);
6325 dwo_tu->dwo_file = dwo_file;
6326 dwo_tu->signature = header.signature;
6327 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6328 dwo_tu->section = section;
6329 dwo_tu->sect_off = sect_off;
6330 dwo_tu->length = length;
6331 }
6332 else
6333 {
6334 /* N.B.: type_offset is not usable if this type uses a DWO file.
6335 The real type_offset is in the DWO file. */
6336 dwo_tu = NULL;
6337 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6338 sig_type->signature = header.signature;
6339 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6340 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6341 sig_type->per_cu.is_debug_types = 1;
6342 sig_type->per_cu.section = section;
6343 sig_type->per_cu.sect_off = sect_off;
6344 sig_type->per_cu.length = length;
6345 }
6346
6347 slot = htab_find_slot (types_htab.get (),
6348 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6349 INSERT);
6350 gdb_assert (slot != NULL);
6351 if (*slot != NULL)
6352 {
6353 sect_offset dup_sect_off;
6354
6355 if (dwo_file)
6356 {
6357 const struct dwo_unit *dup_tu
6358 = (const struct dwo_unit *) *slot;
6359
6360 dup_sect_off = dup_tu->sect_off;
6361 }
6362 else
6363 {
6364 const struct signatured_type *dup_tu
6365 = (const struct signatured_type *) *slot;
6366
6367 dup_sect_off = dup_tu->per_cu.sect_off;
6368 }
6369
6370 complaint (_("debug type entry at offset %s is duplicate to"
6371 " the entry at offset %s, signature %s"),
6372 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6373 hex_string (header.signature));
6374 }
6375 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6376
6377 if (dwarf_read_debug > 1)
6378 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6379 sect_offset_str (sect_off),
6380 hex_string (header.signature));
6381
6382 info_ptr += length;
6383 }
6384 }
6385
6386 /* Create the hash table of all entries in the .debug_types
6387 (or .debug_types.dwo) section(s).
6388 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6389 otherwise it is NULL.
6390
6391 The result is a pointer to the hash table or NULL if there are no types.
6392
6393 Note: This function processes DWO files only, not DWP files. */
6394
6395 static void
6396 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6397 struct dwo_file *dwo_file,
6398 gdb::array_view<dwarf2_section_info> type_sections,
6399 htab_up &types_htab)
6400 {
6401 for (dwarf2_section_info &section : type_sections)
6402 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6403 types_htab, rcuh_kind::TYPE);
6404 }
6405
6406 /* Create the hash table of all entries in the .debug_types section,
6407 and initialize all_type_units.
6408 The result is zero if there is an error (e.g. missing .debug_types section),
6409 otherwise non-zero. */
6410
6411 static int
6412 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6413 {
6414 htab_up types_htab;
6415
6416 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6417 &dwarf2_per_objfile->per_bfd->info, types_htab,
6418 rcuh_kind::COMPILE);
6419 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6420 dwarf2_per_objfile->per_bfd->types, types_htab);
6421 if (types_htab == NULL)
6422 {
6423 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
6424 return 0;
6425 }
6426
6427 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
6428
6429 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6430 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6431 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
6432
6433 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6434 add_signatured_type_cu_to_table,
6435 &dwarf2_per_objfile->per_bfd->all_type_units);
6436
6437 return 1;
6438 }
6439
6440 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6441 If SLOT is non-NULL, it is the entry to use in the hash table.
6442 Otherwise we find one. */
6443
6444 static struct signatured_type *
6445 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6446 void **slot)
6447 {
6448 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6449 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6450 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6451
6452 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6453
6454 dwarf2_per_objfile->resize_symtabs ();
6455
6456 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6457 sig_type->signature = sig;
6458 sig_type->per_cu.is_debug_types = 1;
6459 if (dwarf2_per_objfile->per_bfd->using_index)
6460 {
6461 sig_type->per_cu.v.quick =
6462 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6463 struct dwarf2_per_cu_quick_data);
6464 }
6465
6466 if (slot == NULL)
6467 {
6468 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6469 sig_type, INSERT);
6470 }
6471 gdb_assert (*slot == NULL);
6472 *slot = sig_type;
6473 /* The rest of sig_type must be filled in by the caller. */
6474 return sig_type;
6475 }
6476
6477 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6478 Fill in SIG_ENTRY with DWO_ENTRY. */
6479
6480 static void
6481 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6482 struct signatured_type *sig_entry,
6483 struct dwo_unit *dwo_entry)
6484 {
6485 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6486
6487 /* Make sure we're not clobbering something we don't expect to. */
6488 gdb_assert (! sig_entry->per_cu.queued);
6489 gdb_assert (sig_entry->per_cu.cu == NULL);
6490 if (per_bfd->using_index)
6491 {
6492 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6493 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6494 }
6495 else
6496 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6497 gdb_assert (sig_entry->signature == dwo_entry->signature);
6498 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6499 gdb_assert (sig_entry->type_unit_group == NULL);
6500 gdb_assert (sig_entry->dwo_unit == NULL);
6501
6502 sig_entry->per_cu.section = dwo_entry->section;
6503 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6504 sig_entry->per_cu.length = dwo_entry->length;
6505 sig_entry->per_cu.reading_dwo_directly = 1;
6506 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6507 sig_entry->per_cu.per_bfd = per_bfd;
6508 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6509 sig_entry->dwo_unit = dwo_entry;
6510 }
6511
6512 /* Subroutine of lookup_signatured_type.
6513 If we haven't read the TU yet, create the signatured_type data structure
6514 for a TU to be read in directly from a DWO file, bypassing the stub.
6515 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6516 using .gdb_index, then when reading a CU we want to stay in the DWO file
6517 containing that CU. Otherwise we could end up reading several other DWO
6518 files (due to comdat folding) to process the transitive closure of all the
6519 mentioned TUs, and that can be slow. The current DWO file will have every
6520 type signature that it needs.
6521 We only do this for .gdb_index because in the psymtab case we already have
6522 to read all the DWOs to build the type unit groups. */
6523
6524 static struct signatured_type *
6525 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6526 {
6527 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6528 struct dwo_file *dwo_file;
6529 struct dwo_unit find_dwo_entry, *dwo_entry;
6530 struct signatured_type find_sig_entry, *sig_entry;
6531 void **slot;
6532
6533 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6534
6535 /* If TU skeletons have been removed then we may not have read in any
6536 TUs yet. */
6537 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6538 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6539
6540 /* We only ever need to read in one copy of a signatured type.
6541 Use the global signatured_types array to do our own comdat-folding
6542 of types. If this is the first time we're reading this TU, and
6543 the TU has an entry in .gdb_index, replace the recorded data from
6544 .gdb_index with this TU. */
6545
6546 find_sig_entry.signature = sig;
6547 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6548 &find_sig_entry, INSERT);
6549 sig_entry = (struct signatured_type *) *slot;
6550
6551 /* We can get here with the TU already read, *or* in the process of being
6552 read. Don't reassign the global entry to point to this DWO if that's
6553 the case. Also note that if the TU is already being read, it may not
6554 have come from a DWO, the program may be a mix of Fission-compiled
6555 code and non-Fission-compiled code. */
6556
6557 /* Have we already tried to read this TU?
6558 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6559 needn't exist in the global table yet). */
6560 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6561 return sig_entry;
6562
6563 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6564 dwo_unit of the TU itself. */
6565 dwo_file = cu->dwo_unit->dwo_file;
6566
6567 /* Ok, this is the first time we're reading this TU. */
6568 if (dwo_file->tus == NULL)
6569 return NULL;
6570 find_dwo_entry.signature = sig;
6571 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6572 &find_dwo_entry);
6573 if (dwo_entry == NULL)
6574 return NULL;
6575
6576 /* If the global table doesn't have an entry for this TU, add one. */
6577 if (sig_entry == NULL)
6578 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6579
6580 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6581 sig_entry->per_cu.tu_read = 1;
6582 return sig_entry;
6583 }
6584
6585 /* Subroutine of lookup_signatured_type.
6586 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6587 then try the DWP file. If the TU stub (skeleton) has been removed then
6588 it won't be in .gdb_index. */
6589
6590 static struct signatured_type *
6591 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6592 {
6593 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6594 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6595 struct dwo_unit *dwo_entry;
6596 struct signatured_type find_sig_entry, *sig_entry;
6597 void **slot;
6598
6599 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6600 gdb_assert (dwp_file != NULL);
6601
6602 /* If TU skeletons have been removed then we may not have read in any
6603 TUs yet. */
6604 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6605 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6606
6607 find_sig_entry.signature = sig;
6608 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6609 &find_sig_entry, INSERT);
6610 sig_entry = (struct signatured_type *) *slot;
6611
6612 /* Have we already tried to read this TU?
6613 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6614 needn't exist in the global table yet). */
6615 if (sig_entry != NULL)
6616 return sig_entry;
6617
6618 if (dwp_file->tus == NULL)
6619 return NULL;
6620 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6621 sig, 1 /* is_debug_types */);
6622 if (dwo_entry == NULL)
6623 return NULL;
6624
6625 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6626 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6627
6628 return sig_entry;
6629 }
6630
6631 /* Lookup a signature based type for DW_FORM_ref_sig8.
6632 Returns NULL if signature SIG is not present in the table.
6633 It is up to the caller to complain about this. */
6634
6635 static struct signatured_type *
6636 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6637 {
6638 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6639
6640 if (cu->dwo_unit
6641 && dwarf2_per_objfile->per_bfd->using_index)
6642 {
6643 /* We're in a DWO/DWP file, and we're using .gdb_index.
6644 These cases require special processing. */
6645 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6646 return lookup_dwo_signatured_type (cu, sig);
6647 else
6648 return lookup_dwp_signatured_type (cu, sig);
6649 }
6650 else
6651 {
6652 struct signatured_type find_entry, *entry;
6653
6654 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6655 return NULL;
6656 find_entry.signature = sig;
6657 entry = ((struct signatured_type *)
6658 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6659 &find_entry));
6660 return entry;
6661 }
6662 }
6663
6664 /* Low level DIE reading support. */
6665
6666 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6667
6668 static void
6669 init_cu_die_reader (struct die_reader_specs *reader,
6670 struct dwarf2_cu *cu,
6671 struct dwarf2_section_info *section,
6672 struct dwo_file *dwo_file,
6673 struct abbrev_table *abbrev_table)
6674 {
6675 gdb_assert (section->readin && section->buffer != NULL);
6676 reader->abfd = section->get_bfd_owner ();
6677 reader->cu = cu;
6678 reader->dwo_file = dwo_file;
6679 reader->die_section = section;
6680 reader->buffer = section->buffer;
6681 reader->buffer_end = section->buffer + section->size;
6682 reader->abbrev_table = abbrev_table;
6683 }
6684
6685 /* Subroutine of cutu_reader to simplify it.
6686 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6687 There's just a lot of work to do, and cutu_reader is big enough
6688 already.
6689
6690 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6691 from it to the DIE in the DWO. If NULL we are skipping the stub.
6692 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6693 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6694 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6695 STUB_COMP_DIR may be non-NULL.
6696 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6697 are filled in with the info of the DIE from the DWO file.
6698 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6699 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6700 kept around for at least as long as *RESULT_READER.
6701
6702 The result is non-zero if a valid (non-dummy) DIE was found. */
6703
6704 static int
6705 read_cutu_die_from_dwo (dwarf2_cu *cu,
6706 struct dwo_unit *dwo_unit,
6707 struct die_info *stub_comp_unit_die,
6708 const char *stub_comp_dir,
6709 struct die_reader_specs *result_reader,
6710 const gdb_byte **result_info_ptr,
6711 struct die_info **result_comp_unit_die,
6712 abbrev_table_up *result_dwo_abbrev_table)
6713 {
6714 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6715 dwarf2_per_cu_data *per_cu = cu->per_cu;
6716 struct objfile *objfile = dwarf2_per_objfile->objfile;
6717 bfd *abfd;
6718 const gdb_byte *begin_info_ptr, *info_ptr;
6719 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6720 int i,num_extra_attrs;
6721 struct dwarf2_section_info *dwo_abbrev_section;
6722 struct die_info *comp_unit_die;
6723
6724 /* At most one of these may be provided. */
6725 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6726
6727 /* These attributes aren't processed until later:
6728 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6729 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6730 referenced later. However, these attributes are found in the stub
6731 which we won't have later. In order to not impose this complication
6732 on the rest of the code, we read them here and copy them to the
6733 DWO CU/TU die. */
6734
6735 stmt_list = NULL;
6736 low_pc = NULL;
6737 high_pc = NULL;
6738 ranges = NULL;
6739 comp_dir = NULL;
6740
6741 if (stub_comp_unit_die != NULL)
6742 {
6743 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6744 DWO file. */
6745 if (!per_cu->is_debug_types)
6746 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6747 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6748 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6749 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6750 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6751
6752 cu->addr_base = stub_comp_unit_die->addr_base ();
6753
6754 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6755 here (if needed). We need the value before we can process
6756 DW_AT_ranges. */
6757 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6758 }
6759 else if (stub_comp_dir != NULL)
6760 {
6761 /* Reconstruct the comp_dir attribute to simplify the code below. */
6762 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6763 comp_dir->name = DW_AT_comp_dir;
6764 comp_dir->form = DW_FORM_string;
6765 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6766 DW_STRING (comp_dir) = stub_comp_dir;
6767 }
6768
6769 /* Set up for reading the DWO CU/TU. */
6770 cu->dwo_unit = dwo_unit;
6771 dwarf2_section_info *section = dwo_unit->section;
6772 section->read (objfile);
6773 abfd = section->get_bfd_owner ();
6774 begin_info_ptr = info_ptr = (section->buffer
6775 + to_underlying (dwo_unit->sect_off));
6776 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6777
6778 if (per_cu->is_debug_types)
6779 {
6780 signatured_type *sig_type = (struct signatured_type *) per_cu;
6781
6782 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6783 &cu->header, section,
6784 dwo_abbrev_section,
6785 info_ptr, rcuh_kind::TYPE);
6786 /* This is not an assert because it can be caused by bad debug info. */
6787 if (sig_type->signature != cu->header.signature)
6788 {
6789 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6790 " TU at offset %s [in module %s]"),
6791 hex_string (sig_type->signature),
6792 hex_string (cu->header.signature),
6793 sect_offset_str (dwo_unit->sect_off),
6794 bfd_get_filename (abfd));
6795 }
6796 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6797 /* For DWOs coming from DWP files, we don't know the CU length
6798 nor the type's offset in the TU until now. */
6799 dwo_unit->length = cu->header.get_length ();
6800 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6801
6802 /* Establish the type offset that can be used to lookup the type.
6803 For DWO files, we don't know it until now. */
6804 sig_type->type_offset_in_section
6805 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6806 }
6807 else
6808 {
6809 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6810 &cu->header, section,
6811 dwo_abbrev_section,
6812 info_ptr, rcuh_kind::COMPILE);
6813 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6814 /* For DWOs coming from DWP files, we don't know the CU length
6815 until now. */
6816 dwo_unit->length = cu->header.get_length ();
6817 }
6818
6819 *result_dwo_abbrev_table
6820 = abbrev_table::read (objfile, dwo_abbrev_section,
6821 cu->header.abbrev_sect_off);
6822 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6823 result_dwo_abbrev_table->get ());
6824
6825 /* Read in the die, but leave space to copy over the attributes
6826 from the stub. This has the benefit of simplifying the rest of
6827 the code - all the work to maintain the illusion of a single
6828 DW_TAG_{compile,type}_unit DIE is done here. */
6829 num_extra_attrs = ((stmt_list != NULL)
6830 + (low_pc != NULL)
6831 + (high_pc != NULL)
6832 + (ranges != NULL)
6833 + (comp_dir != NULL));
6834 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6835 num_extra_attrs);
6836
6837 /* Copy over the attributes from the stub to the DIE we just read in. */
6838 comp_unit_die = *result_comp_unit_die;
6839 i = comp_unit_die->num_attrs;
6840 if (stmt_list != NULL)
6841 comp_unit_die->attrs[i++] = *stmt_list;
6842 if (low_pc != NULL)
6843 comp_unit_die->attrs[i++] = *low_pc;
6844 if (high_pc != NULL)
6845 comp_unit_die->attrs[i++] = *high_pc;
6846 if (ranges != NULL)
6847 comp_unit_die->attrs[i++] = *ranges;
6848 if (comp_dir != NULL)
6849 comp_unit_die->attrs[i++] = *comp_dir;
6850 comp_unit_die->num_attrs += num_extra_attrs;
6851
6852 if (dwarf_die_debug)
6853 {
6854 fprintf_unfiltered (gdb_stdlog,
6855 "Read die from %s@0x%x of %s:\n",
6856 section->get_name (),
6857 (unsigned) (begin_info_ptr - section->buffer),
6858 bfd_get_filename (abfd));
6859 dump_die (comp_unit_die, dwarf_die_debug);
6860 }
6861
6862 /* Skip dummy compilation units. */
6863 if (info_ptr >= begin_info_ptr + dwo_unit->length
6864 || peek_abbrev_code (abfd, info_ptr) == 0)
6865 return 0;
6866
6867 *result_info_ptr = info_ptr;
6868 return 1;
6869 }
6870
6871 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6872 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6873 signature is part of the header. */
6874 static gdb::optional<ULONGEST>
6875 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6876 {
6877 if (cu->header.version >= 5)
6878 return cu->header.signature;
6879 struct attribute *attr;
6880 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6881 if (attr == nullptr)
6882 return gdb::optional<ULONGEST> ();
6883 return DW_UNSND (attr);
6884 }
6885
6886 /* Subroutine of cutu_reader to simplify it.
6887 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6888 Returns NULL if the specified DWO unit cannot be found. */
6889
6890 static struct dwo_unit *
6891 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6892 {
6893 dwarf2_per_cu_data *per_cu = cu->per_cu;
6894 struct dwo_unit *dwo_unit;
6895 const char *comp_dir;
6896
6897 gdb_assert (cu != NULL);
6898
6899 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6900 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6901 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6902
6903 if (per_cu->is_debug_types)
6904 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6905 else
6906 {
6907 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6908
6909 if (!signature.has_value ())
6910 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6911 " [in module %s]"),
6912 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6913
6914 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6915 }
6916
6917 return dwo_unit;
6918 }
6919
6920 /* Subroutine of cutu_reader to simplify it.
6921 See it for a description of the parameters.
6922 Read a TU directly from a DWO file, bypassing the stub. */
6923
6924 void
6925 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6926 dwarf2_per_objfile *per_objfile,
6927 int use_existing_cu)
6928 {
6929 struct signatured_type *sig_type;
6930
6931 /* Verify we can do the following downcast, and that we have the
6932 data we need. */
6933 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6934 sig_type = (struct signatured_type *) this_cu;
6935 gdb_assert (sig_type->dwo_unit != NULL);
6936
6937 if (use_existing_cu && this_cu->cu != NULL)
6938 {
6939 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6940 /* There's no need to do the rereading_dwo_cu handling that
6941 cutu_reader does since we don't read the stub. */
6942 }
6943 else
6944 {
6945 /* If !use_existing_cu, this_cu->cu must be NULL. */
6946 gdb_assert (this_cu->cu == NULL);
6947 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6948 }
6949
6950 /* A future optimization, if needed, would be to use an existing
6951 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6952 could share abbrev tables. */
6953
6954 if (read_cutu_die_from_dwo (this_cu->cu, sig_type->dwo_unit,
6955 NULL /* stub_comp_unit_die */,
6956 sig_type->dwo_unit->dwo_file->comp_dir,
6957 this, &info_ptr,
6958 &comp_unit_die,
6959 &m_dwo_abbrev_table) == 0)
6960 {
6961 /* Dummy die. */
6962 dummy_p = true;
6963 }
6964 }
6965
6966 /* Initialize a CU (or TU) and read its DIEs.
6967 If the CU defers to a DWO file, read the DWO file as well.
6968
6969 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6970 Otherwise the table specified in the comp unit header is read in and used.
6971 This is an optimization for when we already have the abbrev table.
6972
6973 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6974 Otherwise, a new CU is allocated with xmalloc. */
6975
6976 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6977 dwarf2_per_objfile *dwarf2_per_objfile,
6978 struct abbrev_table *abbrev_table,
6979 int use_existing_cu,
6980 bool skip_partial)
6981 : die_reader_specs {},
6982 m_this_cu (this_cu)
6983 {
6984 struct objfile *objfile = dwarf2_per_objfile->objfile;
6985 struct dwarf2_section_info *section = this_cu->section;
6986 bfd *abfd = section->get_bfd_owner ();
6987 struct dwarf2_cu *cu;
6988 const gdb_byte *begin_info_ptr;
6989 struct signatured_type *sig_type = NULL;
6990 struct dwarf2_section_info *abbrev_section;
6991 /* Non-zero if CU currently points to a DWO file and we need to
6992 reread it. When this happens we need to reread the skeleton die
6993 before we can reread the DWO file (this only applies to CUs, not TUs). */
6994 int rereading_dwo_cu = 0;
6995
6996 if (dwarf_die_debug)
6997 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6998 this_cu->is_debug_types ? "type" : "comp",
6999 sect_offset_str (this_cu->sect_off));
7000
7001 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7002 file (instead of going through the stub), short-circuit all of this. */
7003 if (this_cu->reading_dwo_directly)
7004 {
7005 /* Narrow down the scope of possibilities to have to understand. */
7006 gdb_assert (this_cu->is_debug_types);
7007 gdb_assert (abbrev_table == NULL);
7008 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, use_existing_cu);
7009 return;
7010 }
7011
7012 /* This is cheap if the section is already read in. */
7013 section->read (objfile);
7014
7015 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7016
7017 abbrev_section = get_abbrev_section_for_cu (this_cu);
7018
7019 if (use_existing_cu && this_cu->cu != NULL)
7020 {
7021 cu = this_cu->cu;
7022 /* If this CU is from a DWO file we need to start over, we need to
7023 refetch the attributes from the skeleton CU.
7024 This could be optimized by retrieving those attributes from when we
7025 were here the first time: the previous comp_unit_die was stored in
7026 comp_unit_obstack. But there's no data yet that we need this
7027 optimization. */
7028 if (cu->dwo_unit != NULL)
7029 rereading_dwo_cu = 1;
7030 }
7031 else
7032 {
7033 /* If !use_existing_cu, this_cu->cu must be NULL. */
7034 gdb_assert (this_cu->cu == NULL);
7035 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7036 cu = m_new_cu.get ();
7037 }
7038
7039 /* Get the header. */
7040 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7041 {
7042 /* We already have the header, there's no need to read it in again. */
7043 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7044 }
7045 else
7046 {
7047 if (this_cu->is_debug_types)
7048 {
7049 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7050 &cu->header, section,
7051 abbrev_section, info_ptr,
7052 rcuh_kind::TYPE);
7053
7054 /* Since per_cu is the first member of struct signatured_type,
7055 we can go from a pointer to one to a pointer to the other. */
7056 sig_type = (struct signatured_type *) this_cu;
7057 gdb_assert (sig_type->signature == cu->header.signature);
7058 gdb_assert (sig_type->type_offset_in_tu
7059 == cu->header.type_cu_offset_in_tu);
7060 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7061
7062 /* LENGTH has not been set yet for type units if we're
7063 using .gdb_index. */
7064 this_cu->length = cu->header.get_length ();
7065
7066 /* Establish the type offset that can be used to lookup the type. */
7067 sig_type->type_offset_in_section =
7068 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7069
7070 this_cu->dwarf_version = cu->header.version;
7071 }
7072 else
7073 {
7074 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7075 &cu->header, section,
7076 abbrev_section,
7077 info_ptr,
7078 rcuh_kind::COMPILE);
7079
7080 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7081 if (this_cu->length == 0)
7082 this_cu->length = cu->header.get_length ();
7083 else
7084 gdb_assert (this_cu->length == cu->header.get_length ());
7085 this_cu->dwarf_version = cu->header.version;
7086 }
7087 }
7088
7089 /* Skip dummy compilation units. */
7090 if (info_ptr >= begin_info_ptr + this_cu->length
7091 || peek_abbrev_code (abfd, info_ptr) == 0)
7092 {
7093 dummy_p = true;
7094 return;
7095 }
7096
7097 /* If we don't have them yet, read the abbrevs for this compilation unit.
7098 And if we need to read them now, make sure they're freed when we're
7099 done. */
7100 if (abbrev_table != NULL)
7101 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7102 else
7103 {
7104 m_abbrev_table_holder
7105 = abbrev_table::read (objfile, abbrev_section,
7106 cu->header.abbrev_sect_off);
7107 abbrev_table = m_abbrev_table_holder.get ();
7108 }
7109
7110 /* Read the top level CU/TU die. */
7111 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7112 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7113
7114 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7115 {
7116 dummy_p = true;
7117 return;
7118 }
7119
7120 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7121 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7122 table from the DWO file and pass the ownership over to us. It will be
7123 referenced from READER, so we must make sure to free it after we're done
7124 with READER.
7125
7126 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7127 DWO CU, that this test will fail (the attribute will not be present). */
7128 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7129 if (dwo_name != nullptr)
7130 {
7131 struct dwo_unit *dwo_unit;
7132 struct die_info *dwo_comp_unit_die;
7133
7134 if (comp_unit_die->has_children)
7135 {
7136 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7137 " has children (offset %s) [in module %s]"),
7138 sect_offset_str (this_cu->sect_off),
7139 bfd_get_filename (abfd));
7140 }
7141 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7142 if (dwo_unit != NULL)
7143 {
7144 if (read_cutu_die_from_dwo (cu, dwo_unit,
7145 comp_unit_die, NULL,
7146 this, &info_ptr,
7147 &dwo_comp_unit_die,
7148 &m_dwo_abbrev_table) == 0)
7149 {
7150 /* Dummy die. */
7151 dummy_p = true;
7152 return;
7153 }
7154 comp_unit_die = dwo_comp_unit_die;
7155 }
7156 else
7157 {
7158 /* Yikes, we couldn't find the rest of the DIE, we only have
7159 the stub. A complaint has already been logged. There's
7160 not much more we can do except pass on the stub DIE to
7161 die_reader_func. We don't want to throw an error on bad
7162 debug info. */
7163 }
7164 }
7165 }
7166
7167 void
7168 cutu_reader::keep ()
7169 {
7170 /* Done, clean up. */
7171 gdb_assert (!dummy_p);
7172 if (m_new_cu != NULL)
7173 {
7174 /* We know that m_this_cu->cu is set, since we are in the process of
7175 parsing the CU. */
7176 gdb_assert (m_this_cu->cu != nullptr);
7177 dwarf2_per_objfile *dwarf2_per_objfile = m_this_cu->cu->per_objfile;
7178
7179 /* Link this CU into read_in_chain. */
7180 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->per_bfd->read_in_chain;
7181 dwarf2_per_objfile->per_bfd->read_in_chain = m_this_cu;
7182 /* The chain owns it now. */
7183 m_new_cu.release ();
7184 }
7185 }
7186
7187 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7188 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7189 assumed to have already done the lookup to find the DWO file).
7190
7191 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7192 THIS_CU->is_debug_types, but nothing else.
7193
7194 We fill in THIS_CU->length.
7195
7196 THIS_CU->cu is always freed when done.
7197 This is done in order to not leave THIS_CU->cu in a state where we have
7198 to care whether it refers to the "main" CU or the DWO CU.
7199
7200 When parent_cu is passed, it is used to provide a default value for
7201 str_offsets_base and addr_base from the parent. */
7202
7203 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7204 dwarf2_per_objfile *dwarf2_per_objfile,
7205 struct dwarf2_cu *parent_cu,
7206 struct dwo_file *dwo_file)
7207 : die_reader_specs {},
7208 m_this_cu (this_cu)
7209 {
7210 struct objfile *objfile = dwarf2_per_objfile->objfile;
7211 struct dwarf2_section_info *section = this_cu->section;
7212 bfd *abfd = section->get_bfd_owner ();
7213 struct dwarf2_section_info *abbrev_section;
7214 const gdb_byte *begin_info_ptr, *info_ptr;
7215
7216 if (dwarf_die_debug)
7217 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7218 this_cu->is_debug_types ? "type" : "comp",
7219 sect_offset_str (this_cu->sect_off));
7220
7221 gdb_assert (this_cu->cu == NULL);
7222
7223 abbrev_section = (dwo_file != NULL
7224 ? &dwo_file->sections.abbrev
7225 : get_abbrev_section_for_cu (this_cu));
7226
7227 /* This is cheap if the section is already read in. */
7228 section->read (objfile);
7229
7230 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7231
7232 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7233 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7234 &m_new_cu->header, section,
7235 abbrev_section, info_ptr,
7236 (this_cu->is_debug_types
7237 ? rcuh_kind::TYPE
7238 : rcuh_kind::COMPILE));
7239
7240 if (parent_cu != nullptr)
7241 {
7242 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7243 m_new_cu->addr_base = parent_cu->addr_base;
7244 }
7245 this_cu->length = m_new_cu->header.get_length ();
7246
7247 /* Skip dummy compilation units. */
7248 if (info_ptr >= begin_info_ptr + this_cu->length
7249 || peek_abbrev_code (abfd, info_ptr) == 0)
7250 {
7251 dummy_p = true;
7252 return;
7253 }
7254
7255 m_abbrev_table_holder
7256 = abbrev_table::read (objfile, abbrev_section,
7257 m_new_cu->header.abbrev_sect_off);
7258
7259 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7260 m_abbrev_table_holder.get ());
7261 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7262 }
7263
7264 \f
7265 /* Type Unit Groups.
7266
7267 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7268 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7269 so that all types coming from the same compilation (.o file) are grouped
7270 together. A future step could be to put the types in the same symtab as
7271 the CU the types ultimately came from. */
7272
7273 static hashval_t
7274 hash_type_unit_group (const void *item)
7275 {
7276 const struct type_unit_group *tu_group
7277 = (const struct type_unit_group *) item;
7278
7279 return hash_stmt_list_entry (&tu_group->hash);
7280 }
7281
7282 static int
7283 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7284 {
7285 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7286 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7287
7288 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7289 }
7290
7291 /* Allocate a hash table for type unit groups. */
7292
7293 static htab_up
7294 allocate_type_unit_groups_table ()
7295 {
7296 return htab_up (htab_create_alloc (3,
7297 hash_type_unit_group,
7298 eq_type_unit_group,
7299 NULL, xcalloc, xfree));
7300 }
7301
7302 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7303 partial symtabs. We combine several TUs per psymtab to not let the size
7304 of any one psymtab grow too big. */
7305 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7306 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7307
7308 /* Helper routine for get_type_unit_group.
7309 Create the type_unit_group object used to hold one or more TUs. */
7310
7311 static struct type_unit_group *
7312 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7313 {
7314 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7315 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
7316 struct dwarf2_per_cu_data *per_cu;
7317 struct type_unit_group *tu_group;
7318
7319 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
7320 struct type_unit_group);
7321 per_cu = &tu_group->per_cu;
7322 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7323 per_cu->per_bfd = per_bfd;
7324
7325 if (per_bfd->using_index)
7326 {
7327 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7328 struct dwarf2_per_cu_quick_data);
7329 }
7330 else
7331 {
7332 unsigned int line_offset = to_underlying (line_offset_struct);
7333 dwarf2_psymtab *pst;
7334 std::string name;
7335
7336 /* Give the symtab a useful name for debug purposes. */
7337 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7338 name = string_printf ("<type_units_%d>",
7339 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7340 else
7341 name = string_printf ("<type_units_at_0x%x>", line_offset);
7342
7343 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, name.c_str ());
7344 pst->anonymous = true;
7345 }
7346
7347 tu_group->hash.dwo_unit = cu->dwo_unit;
7348 tu_group->hash.line_sect_off = line_offset_struct;
7349
7350 return tu_group;
7351 }
7352
7353 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7354 STMT_LIST is a DW_AT_stmt_list attribute. */
7355
7356 static struct type_unit_group *
7357 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7358 {
7359 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7360 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7361 struct type_unit_group *tu_group;
7362 void **slot;
7363 unsigned int line_offset;
7364 struct type_unit_group type_unit_group_for_lookup;
7365
7366 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7367 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7368
7369 /* Do we need to create a new group, or can we use an existing one? */
7370
7371 if (stmt_list)
7372 {
7373 line_offset = DW_UNSND (stmt_list);
7374 ++tu_stats->nr_symtab_sharers;
7375 }
7376 else
7377 {
7378 /* Ugh, no stmt_list. Rare, but we have to handle it.
7379 We can do various things here like create one group per TU or
7380 spread them over multiple groups to split up the expansion work.
7381 To avoid worst case scenarios (too many groups or too large groups)
7382 we, umm, group them in bunches. */
7383 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7384 | (tu_stats->nr_stmt_less_type_units
7385 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7386 ++tu_stats->nr_stmt_less_type_units;
7387 }
7388
7389 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7390 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7391 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7392 &type_unit_group_for_lookup, INSERT);
7393 if (*slot != NULL)
7394 {
7395 tu_group = (struct type_unit_group *) *slot;
7396 gdb_assert (tu_group != NULL);
7397 }
7398 else
7399 {
7400 sect_offset line_offset_struct = (sect_offset) line_offset;
7401 tu_group = create_type_unit_group (cu, line_offset_struct);
7402 *slot = tu_group;
7403 ++tu_stats->nr_symtabs;
7404 }
7405
7406 return tu_group;
7407 }
7408 \f
7409 /* Partial symbol tables. */
7410
7411 /* Create a psymtab named NAME and assign it to PER_CU.
7412
7413 The caller must fill in the following details:
7414 dirname, textlow, texthigh. */
7415
7416 static dwarf2_psymtab *
7417 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7418 dwarf2_per_objfile *per_objfile,
7419 const char *name)
7420 {
7421 struct objfile *objfile = per_objfile->objfile;
7422 dwarf2_psymtab *pst;
7423
7424 pst = new dwarf2_psymtab (name, objfile, per_cu);
7425
7426 pst->psymtabs_addrmap_supported = true;
7427
7428 /* This is the glue that links PST into GDB's symbol API. */
7429 per_cu->v.psymtab = pst;
7430
7431 return pst;
7432 }
7433
7434 /* DIE reader function for process_psymtab_comp_unit. */
7435
7436 static void
7437 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7438 const gdb_byte *info_ptr,
7439 struct die_info *comp_unit_die,
7440 enum language pretend_language)
7441 {
7442 struct dwarf2_cu *cu = reader->cu;
7443 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7444 struct objfile *objfile = per_objfile->objfile;
7445 struct gdbarch *gdbarch = objfile->arch ();
7446 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7447 CORE_ADDR baseaddr;
7448 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7449 dwarf2_psymtab *pst;
7450 enum pc_bounds_kind cu_bounds_kind;
7451 const char *filename;
7452
7453 gdb_assert (! per_cu->is_debug_types);
7454
7455 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7456
7457 /* Allocate a new partial symbol table structure. */
7458 gdb::unique_xmalloc_ptr<char> debug_filename;
7459 static const char artificial[] = "<artificial>";
7460 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7461 if (filename == NULL)
7462 filename = "";
7463 else if (strcmp (filename, artificial) == 0)
7464 {
7465 debug_filename.reset (concat (artificial, "@",
7466 sect_offset_str (per_cu->sect_off),
7467 (char *) NULL));
7468 filename = debug_filename.get ();
7469 }
7470
7471 pst = create_partial_symtab (per_cu, per_objfile, filename);
7472
7473 /* This must be done before calling dwarf2_build_include_psymtabs. */
7474 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7475
7476 baseaddr = objfile->text_section_offset ();
7477
7478 dwarf2_find_base_address (comp_unit_die, cu);
7479
7480 /* Possibly set the default values of LOWPC and HIGHPC from
7481 `DW_AT_ranges'. */
7482 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7483 &best_highpc, cu, pst);
7484 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7485 {
7486 CORE_ADDR low
7487 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7488 - baseaddr);
7489 CORE_ADDR high
7490 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7491 - baseaddr - 1);
7492 /* Store the contiguous range if it is not empty; it can be
7493 empty for CUs with no code. */
7494 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7495 low, high, pst);
7496 }
7497
7498 /* Check if comp unit has_children.
7499 If so, read the rest of the partial symbols from this comp unit.
7500 If not, there's no more debug_info for this comp unit. */
7501 if (comp_unit_die->has_children)
7502 {
7503 struct partial_die_info *first_die;
7504 CORE_ADDR lowpc, highpc;
7505
7506 lowpc = ((CORE_ADDR) -1);
7507 highpc = ((CORE_ADDR) 0);
7508
7509 first_die = load_partial_dies (reader, info_ptr, 1);
7510
7511 scan_partial_symbols (first_die, &lowpc, &highpc,
7512 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7513
7514 /* If we didn't find a lowpc, set it to highpc to avoid
7515 complaints from `maint check'. */
7516 if (lowpc == ((CORE_ADDR) -1))
7517 lowpc = highpc;
7518
7519 /* If the compilation unit didn't have an explicit address range,
7520 then use the information extracted from its child dies. */
7521 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7522 {
7523 best_lowpc = lowpc;
7524 best_highpc = highpc;
7525 }
7526 }
7527 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7528 best_lowpc + baseaddr)
7529 - baseaddr);
7530 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7531 best_highpc + baseaddr)
7532 - baseaddr);
7533
7534 end_psymtab_common (objfile, pst);
7535
7536 if (!cu->per_cu->imported_symtabs_empty ())
7537 {
7538 int i;
7539 int len = cu->per_cu->imported_symtabs_size ();
7540
7541 /* Fill in 'dependencies' here; we fill in 'users' in a
7542 post-pass. */
7543 pst->number_of_dependencies = len;
7544 pst->dependencies
7545 = objfile->partial_symtabs->allocate_dependencies (len);
7546 for (i = 0; i < len; ++i)
7547 {
7548 pst->dependencies[i]
7549 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7550 }
7551
7552 cu->per_cu->imported_symtabs_free ();
7553 }
7554
7555 /* Get the list of files included in the current compilation unit,
7556 and build a psymtab for each of them. */
7557 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7558
7559 if (dwarf_read_debug)
7560 fprintf_unfiltered (gdb_stdlog,
7561 "Psymtab for %s unit @%s: %s - %s"
7562 ", %d global, %d static syms\n",
7563 per_cu->is_debug_types ? "type" : "comp",
7564 sect_offset_str (per_cu->sect_off),
7565 paddress (gdbarch, pst->text_low (objfile)),
7566 paddress (gdbarch, pst->text_high (objfile)),
7567 pst->n_global_syms, pst->n_static_syms);
7568 }
7569
7570 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7571 Process compilation unit THIS_CU for a psymtab. */
7572
7573 static void
7574 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7575 dwarf2_per_objfile *per_objfile,
7576 bool want_partial_unit,
7577 enum language pretend_language)
7578 {
7579 /* If this compilation unit was already read in, free the
7580 cached copy in order to read it in again. This is
7581 necessary because we skipped some symbols when we first
7582 read in the compilation unit (see load_partial_dies).
7583 This problem could be avoided, but the benefit is unclear. */
7584 if (this_cu->cu != NULL)
7585 free_one_cached_comp_unit (this_cu, per_objfile);
7586
7587 cutu_reader reader (this_cu, per_objfile, NULL, 0, false);
7588
7589 switch (reader.comp_unit_die->tag)
7590 {
7591 case DW_TAG_compile_unit:
7592 this_cu->unit_type = DW_UT_compile;
7593 break;
7594 case DW_TAG_partial_unit:
7595 this_cu->unit_type = DW_UT_partial;
7596 break;
7597 default:
7598 abort ();
7599 }
7600
7601 if (reader.dummy_p)
7602 {
7603 /* Nothing. */
7604 }
7605 else if (this_cu->is_debug_types)
7606 build_type_psymtabs_reader (&reader, reader.info_ptr,
7607 reader.comp_unit_die);
7608 else if (want_partial_unit
7609 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7610 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7611 reader.comp_unit_die,
7612 pretend_language);
7613
7614 this_cu->lang = this_cu->cu->language;
7615
7616 /* Age out any secondary CUs. */
7617 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7618 }
7619
7620 /* Reader function for build_type_psymtabs. */
7621
7622 static void
7623 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7624 const gdb_byte *info_ptr,
7625 struct die_info *type_unit_die)
7626 {
7627 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
7628 struct objfile *objfile = dwarf2_per_objfile->objfile;
7629 struct dwarf2_cu *cu = reader->cu;
7630 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7631 struct signatured_type *sig_type;
7632 struct type_unit_group *tu_group;
7633 struct attribute *attr;
7634 struct partial_die_info *first_die;
7635 CORE_ADDR lowpc, highpc;
7636 dwarf2_psymtab *pst;
7637
7638 gdb_assert (per_cu->is_debug_types);
7639 sig_type = (struct signatured_type *) per_cu;
7640
7641 if (! type_unit_die->has_children)
7642 return;
7643
7644 attr = type_unit_die->attr (DW_AT_stmt_list);
7645 tu_group = get_type_unit_group (cu, attr);
7646
7647 if (tu_group->tus == nullptr)
7648 tu_group->tus = new std::vector<signatured_type *>;
7649 tu_group->tus->push_back (sig_type);
7650
7651 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7652 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, "");
7653 pst->anonymous = true;
7654
7655 first_die = load_partial_dies (reader, info_ptr, 1);
7656
7657 lowpc = (CORE_ADDR) -1;
7658 highpc = (CORE_ADDR) 0;
7659 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7660
7661 end_psymtab_common (objfile, pst);
7662 }
7663
7664 /* Struct used to sort TUs by their abbreviation table offset. */
7665
7666 struct tu_abbrev_offset
7667 {
7668 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7669 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7670 {}
7671
7672 signatured_type *sig_type;
7673 sect_offset abbrev_offset;
7674 };
7675
7676 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7677
7678 static bool
7679 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7680 const struct tu_abbrev_offset &b)
7681 {
7682 return a.abbrev_offset < b.abbrev_offset;
7683 }
7684
7685 /* Efficiently read all the type units.
7686 This does the bulk of the work for build_type_psymtabs.
7687
7688 The efficiency is because we sort TUs by the abbrev table they use and
7689 only read each abbrev table once. In one program there are 200K TUs
7690 sharing 8K abbrev tables.
7691
7692 The main purpose of this function is to support building the
7693 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7694 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7695 can collapse the search space by grouping them by stmt_list.
7696 The savings can be significant, in the same program from above the 200K TUs
7697 share 8K stmt_list tables.
7698
7699 FUNC is expected to call get_type_unit_group, which will create the
7700 struct type_unit_group if necessary and add it to
7701 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7702
7703 static void
7704 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7705 {
7706 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7707 abbrev_table_up abbrev_table;
7708 sect_offset abbrev_offset;
7709
7710 /* It's up to the caller to not call us multiple times. */
7711 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
7712
7713 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
7714 return;
7715
7716 /* TUs typically share abbrev tables, and there can be way more TUs than
7717 abbrev tables. Sort by abbrev table to reduce the number of times we
7718 read each abbrev table in.
7719 Alternatives are to punt or to maintain a cache of abbrev tables.
7720 This is simpler and efficient enough for now.
7721
7722 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7723 symtab to use). Typically TUs with the same abbrev offset have the same
7724 stmt_list value too so in practice this should work well.
7725
7726 The basic algorithm here is:
7727
7728 sort TUs by abbrev table
7729 for each TU with same abbrev table:
7730 read abbrev table if first user
7731 read TU top level DIE
7732 [IWBN if DWO skeletons had DW_AT_stmt_list]
7733 call FUNC */
7734
7735 if (dwarf_read_debug)
7736 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7737
7738 /* Sort in a separate table to maintain the order of all_type_units
7739 for .gdb_index: TU indices directly index all_type_units. */
7740 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7741 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
7742
7743 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
7744 sorted_by_abbrev.emplace_back
7745 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7746 sig_type->per_cu.section,
7747 sig_type->per_cu.sect_off));
7748
7749 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7750 sort_tu_by_abbrev_offset);
7751
7752 abbrev_offset = (sect_offset) ~(unsigned) 0;
7753
7754 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7755 {
7756 /* Switch to the next abbrev table if necessary. */
7757 if (abbrev_table == NULL
7758 || tu.abbrev_offset != abbrev_offset)
7759 {
7760 abbrev_offset = tu.abbrev_offset;
7761 abbrev_table =
7762 abbrev_table::read (dwarf2_per_objfile->objfile,
7763 &dwarf2_per_objfile->per_bfd->abbrev,
7764 abbrev_offset);
7765 ++tu_stats->nr_uniq_abbrev_tables;
7766 }
7767
7768 cutu_reader reader (&tu.sig_type->per_cu, dwarf2_per_objfile,
7769 abbrev_table.get (), 0, false);
7770 if (!reader.dummy_p)
7771 build_type_psymtabs_reader (&reader, reader.info_ptr,
7772 reader.comp_unit_die);
7773 }
7774 }
7775
7776 /* Print collected type unit statistics. */
7777
7778 static void
7779 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7780 {
7781 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7782
7783 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7784 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7785 dwarf2_per_objfile->per_bfd->all_type_units.size ());
7786 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7787 tu_stats->nr_uniq_abbrev_tables);
7788 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7789 tu_stats->nr_symtabs);
7790 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7791 tu_stats->nr_symtab_sharers);
7792 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7793 tu_stats->nr_stmt_less_type_units);
7794 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7795 tu_stats->nr_all_type_units_reallocs);
7796 }
7797
7798 /* Traversal function for build_type_psymtabs. */
7799
7800 static int
7801 build_type_psymtab_dependencies (void **slot, void *info)
7802 {
7803 struct dwarf2_per_objfile *dwarf2_per_objfile
7804 = (struct dwarf2_per_objfile *) info;
7805 struct objfile *objfile = dwarf2_per_objfile->objfile;
7806 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7807 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7808 dwarf2_psymtab *pst = per_cu->v.psymtab;
7809 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7810 int i;
7811
7812 gdb_assert (len > 0);
7813 gdb_assert (per_cu->type_unit_group_p ());
7814
7815 pst->number_of_dependencies = len;
7816 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7817 for (i = 0; i < len; ++i)
7818 {
7819 struct signatured_type *iter = tu_group->tus->at (i);
7820 gdb_assert (iter->per_cu.is_debug_types);
7821 pst->dependencies[i] = iter->per_cu.v.psymtab;
7822 iter->type_unit_group = tu_group;
7823 }
7824
7825 delete tu_group->tus;
7826 tu_group->tus = nullptr;
7827
7828 return 1;
7829 }
7830
7831 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7832 Build partial symbol tables for the .debug_types comp-units. */
7833
7834 static void
7835 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7836 {
7837 if (! create_all_type_units (dwarf2_per_objfile))
7838 return;
7839
7840 build_type_psymtabs_1 (dwarf2_per_objfile);
7841 }
7842
7843 /* Traversal function for process_skeletonless_type_unit.
7844 Read a TU in a DWO file and build partial symbols for it. */
7845
7846 static int
7847 process_skeletonless_type_unit (void **slot, void *info)
7848 {
7849 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7850 struct dwarf2_per_objfile *dwarf2_per_objfile
7851 = (struct dwarf2_per_objfile *) info;
7852 struct signatured_type find_entry, *entry;
7853
7854 /* If this TU doesn't exist in the global table, add it and read it in. */
7855
7856 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7857 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7858
7859 find_entry.signature = dwo_unit->signature;
7860 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
7861 &find_entry, INSERT);
7862 /* If we've already seen this type there's nothing to do. What's happening
7863 is we're doing our own version of comdat-folding here. */
7864 if (*slot != NULL)
7865 return 1;
7866
7867 /* This does the job that create_all_type_units would have done for
7868 this TU. */
7869 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7870 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7871 *slot = entry;
7872
7873 /* This does the job that build_type_psymtabs_1 would have done. */
7874 cutu_reader reader (&entry->per_cu, dwarf2_per_objfile, NULL, 0, false);
7875 if (!reader.dummy_p)
7876 build_type_psymtabs_reader (&reader, reader.info_ptr,
7877 reader.comp_unit_die);
7878
7879 return 1;
7880 }
7881
7882 /* Traversal function for process_skeletonless_type_units. */
7883
7884 static int
7885 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7886 {
7887 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7888
7889 if (dwo_file->tus != NULL)
7890 htab_traverse_noresize (dwo_file->tus.get (),
7891 process_skeletonless_type_unit, info);
7892
7893 return 1;
7894 }
7895
7896 /* Scan all TUs of DWO files, verifying we've processed them.
7897 This is needed in case a TU was emitted without its skeleton.
7898 Note: This can't be done until we know what all the DWO files are. */
7899
7900 static void
7901 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7902 {
7903 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7904 if (get_dwp_file (dwarf2_per_objfile) == NULL
7905 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
7906 {
7907 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
7908 process_dwo_file_for_skeletonless_type_units,
7909 dwarf2_per_objfile);
7910 }
7911 }
7912
7913 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7914
7915 static void
7916 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7917 {
7918 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7919 {
7920 dwarf2_psymtab *pst = per_cu->v.psymtab;
7921
7922 if (pst == NULL)
7923 continue;
7924
7925 for (int j = 0; j < pst->number_of_dependencies; ++j)
7926 {
7927 /* Set the 'user' field only if it is not already set. */
7928 if (pst->dependencies[j]->user == NULL)
7929 pst->dependencies[j]->user = pst;
7930 }
7931 }
7932 }
7933
7934 /* Build the partial symbol table by doing a quick pass through the
7935 .debug_info and .debug_abbrev sections. */
7936
7937 static void
7938 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7939 {
7940 struct objfile *objfile = dwarf2_per_objfile->objfile;
7941
7942 if (dwarf_read_debug)
7943 {
7944 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7945 objfile_name (objfile));
7946 }
7947
7948 scoped_restore restore_reading_psyms
7949 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
7950 true);
7951
7952 dwarf2_per_objfile->per_bfd->info.read (objfile);
7953
7954 /* Any cached compilation units will be linked by the per-objfile
7955 read_in_chain. Make sure to free them when we're done. */
7956 free_cached_comp_units freer (dwarf2_per_objfile);
7957
7958 build_type_psymtabs (dwarf2_per_objfile);
7959
7960 create_all_comp_units (dwarf2_per_objfile);
7961
7962 /* Create a temporary address map on a temporary obstack. We later
7963 copy this to the final obstack. */
7964 auto_obstack temp_obstack;
7965
7966 scoped_restore save_psymtabs_addrmap
7967 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7968 addrmap_create_mutable (&temp_obstack));
7969
7970 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7971 {
7972 if (per_cu->v.psymtab != NULL)
7973 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7974 continue;
7975 process_psymtab_comp_unit (per_cu, dwarf2_per_objfile, false,
7976 language_minimal);
7977 }
7978
7979 /* This has to wait until we read the CUs, we need the list of DWOs. */
7980 process_skeletonless_type_units (dwarf2_per_objfile);
7981
7982 /* Now that all TUs have been processed we can fill in the dependencies. */
7983 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
7984 {
7985 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7986 build_type_psymtab_dependencies, dwarf2_per_objfile);
7987 }
7988
7989 if (dwarf_read_debug)
7990 print_tu_stats (dwarf2_per_objfile);
7991
7992 set_partial_user (dwarf2_per_objfile);
7993
7994 objfile->partial_symtabs->psymtabs_addrmap
7995 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7996 objfile->partial_symtabs->obstack ());
7997 /* At this point we want to keep the address map. */
7998 save_psymtabs_addrmap.release ();
7999
8000 if (dwarf_read_debug)
8001 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8002 objfile_name (objfile));
8003 }
8004
8005 /* Load the partial DIEs for a secondary CU into memory.
8006 This is also used when rereading a primary CU with load_all_dies. */
8007
8008 static void
8009 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8010 dwarf2_per_objfile *per_objfile)
8011 {
8012 cutu_reader reader (this_cu, per_objfile, NULL, 1, false);
8013
8014 if (!reader.dummy_p)
8015 {
8016 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8017 language_minimal);
8018
8019 /* Check if comp unit has_children.
8020 If so, read the rest of the partial symbols from this comp unit.
8021 If not, there's no more debug_info for this comp unit. */
8022 if (reader.comp_unit_die->has_children)
8023 load_partial_dies (&reader, reader.info_ptr, 0);
8024
8025 reader.keep ();
8026 }
8027 }
8028
8029 static void
8030 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8031 struct dwarf2_section_info *section,
8032 struct dwarf2_section_info *abbrev_section,
8033 unsigned int is_dwz)
8034 {
8035 const gdb_byte *info_ptr;
8036 struct objfile *objfile = dwarf2_per_objfile->objfile;
8037
8038 if (dwarf_read_debug)
8039 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8040 section->get_name (),
8041 section->get_file_name ());
8042
8043 section->read (objfile);
8044
8045 info_ptr = section->buffer;
8046
8047 while (info_ptr < section->buffer + section->size)
8048 {
8049 struct dwarf2_per_cu_data *this_cu;
8050
8051 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8052
8053 comp_unit_head cu_header;
8054 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8055 abbrev_section, info_ptr,
8056 rcuh_kind::COMPILE);
8057
8058 /* Save the compilation unit for later lookup. */
8059 if (cu_header.unit_type != DW_UT_type)
8060 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
8061 else
8062 {
8063 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
8064 sig_type->signature = cu_header.signature;
8065 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8066 this_cu = &sig_type->per_cu;
8067 }
8068 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8069 this_cu->sect_off = sect_off;
8070 this_cu->length = cu_header.length + cu_header.initial_length_size;
8071 this_cu->is_dwz = is_dwz;
8072 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8073 this_cu->section = section;
8074
8075 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8076
8077 info_ptr = info_ptr + this_cu->length;
8078 }
8079 }
8080
8081 /* Create a list of all compilation units in OBJFILE.
8082 This is only done for -readnow and building partial symtabs. */
8083
8084 static void
8085 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8086 {
8087 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8088 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8089 &dwarf2_per_objfile->per_bfd->abbrev, 0);
8090
8091 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
8092 if (dwz != NULL)
8093 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8094 1);
8095 }
8096
8097 /* Process all loaded DIEs for compilation unit CU, starting at
8098 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8099 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8100 DW_AT_ranges). See the comments of add_partial_subprogram on how
8101 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8102
8103 static void
8104 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8105 CORE_ADDR *highpc, int set_addrmap,
8106 struct dwarf2_cu *cu)
8107 {
8108 struct partial_die_info *pdi;
8109
8110 /* Now, march along the PDI's, descending into ones which have
8111 interesting children but skipping the children of the other ones,
8112 until we reach the end of the compilation unit. */
8113
8114 pdi = first_die;
8115
8116 while (pdi != NULL)
8117 {
8118 pdi->fixup (cu);
8119
8120 /* Anonymous namespaces or modules have no name but have interesting
8121 children, so we need to look at them. Ditto for anonymous
8122 enums. */
8123
8124 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8125 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8126 || pdi->tag == DW_TAG_imported_unit
8127 || pdi->tag == DW_TAG_inlined_subroutine)
8128 {
8129 switch (pdi->tag)
8130 {
8131 case DW_TAG_subprogram:
8132 case DW_TAG_inlined_subroutine:
8133 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8134 break;
8135 case DW_TAG_constant:
8136 case DW_TAG_variable:
8137 case DW_TAG_typedef:
8138 case DW_TAG_union_type:
8139 if (!pdi->is_declaration
8140 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8141 {
8142 add_partial_symbol (pdi, cu);
8143 }
8144 break;
8145 case DW_TAG_class_type:
8146 case DW_TAG_interface_type:
8147 case DW_TAG_structure_type:
8148 if (!pdi->is_declaration)
8149 {
8150 add_partial_symbol (pdi, cu);
8151 }
8152 if ((cu->language == language_rust
8153 || cu->language == language_cplus) && pdi->has_children)
8154 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8155 set_addrmap, cu);
8156 break;
8157 case DW_TAG_enumeration_type:
8158 if (!pdi->is_declaration)
8159 add_partial_enumeration (pdi, cu);
8160 break;
8161 case DW_TAG_base_type:
8162 case DW_TAG_subrange_type:
8163 /* File scope base type definitions are added to the partial
8164 symbol table. */
8165 add_partial_symbol (pdi, cu);
8166 break;
8167 case DW_TAG_namespace:
8168 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8169 break;
8170 case DW_TAG_module:
8171 if (!pdi->is_declaration)
8172 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8173 break;
8174 case DW_TAG_imported_unit:
8175 {
8176 struct dwarf2_per_cu_data *per_cu;
8177
8178 /* For now we don't handle imported units in type units. */
8179 if (cu->per_cu->is_debug_types)
8180 {
8181 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8182 " supported in type units [in module %s]"),
8183 objfile_name (cu->per_objfile->objfile));
8184 }
8185
8186 per_cu = dwarf2_find_containing_comp_unit
8187 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8188
8189 /* Go read the partial unit, if needed. */
8190 if (per_cu->v.psymtab == NULL)
8191 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8192 cu->language);
8193
8194 cu->per_cu->imported_symtabs_push (per_cu);
8195 }
8196 break;
8197 case DW_TAG_imported_declaration:
8198 add_partial_symbol (pdi, cu);
8199 break;
8200 default:
8201 break;
8202 }
8203 }
8204
8205 /* If the die has a sibling, skip to the sibling. */
8206
8207 pdi = pdi->die_sibling;
8208 }
8209 }
8210
8211 /* Functions used to compute the fully scoped name of a partial DIE.
8212
8213 Normally, this is simple. For C++, the parent DIE's fully scoped
8214 name is concatenated with "::" and the partial DIE's name.
8215 Enumerators are an exception; they use the scope of their parent
8216 enumeration type, i.e. the name of the enumeration type is not
8217 prepended to the enumerator.
8218
8219 There are two complexities. One is DW_AT_specification; in this
8220 case "parent" means the parent of the target of the specification,
8221 instead of the direct parent of the DIE. The other is compilers
8222 which do not emit DW_TAG_namespace; in this case we try to guess
8223 the fully qualified name of structure types from their members'
8224 linkage names. This must be done using the DIE's children rather
8225 than the children of any DW_AT_specification target. We only need
8226 to do this for structures at the top level, i.e. if the target of
8227 any DW_AT_specification (if any; otherwise the DIE itself) does not
8228 have a parent. */
8229
8230 /* Compute the scope prefix associated with PDI's parent, in
8231 compilation unit CU. The result will be allocated on CU's
8232 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8233 field. NULL is returned if no prefix is necessary. */
8234 static const char *
8235 partial_die_parent_scope (struct partial_die_info *pdi,
8236 struct dwarf2_cu *cu)
8237 {
8238 const char *grandparent_scope;
8239 struct partial_die_info *parent, *real_pdi;
8240
8241 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8242 then this means the parent of the specification DIE. */
8243
8244 real_pdi = pdi;
8245 while (real_pdi->has_specification)
8246 {
8247 auto res = find_partial_die (real_pdi->spec_offset,
8248 real_pdi->spec_is_dwz, cu);
8249 real_pdi = res.pdi;
8250 cu = res.cu;
8251 }
8252
8253 parent = real_pdi->die_parent;
8254 if (parent == NULL)
8255 return NULL;
8256
8257 if (parent->scope_set)
8258 return parent->scope;
8259
8260 parent->fixup (cu);
8261
8262 grandparent_scope = partial_die_parent_scope (parent, cu);
8263
8264 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8265 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8266 Work around this problem here. */
8267 if (cu->language == language_cplus
8268 && parent->tag == DW_TAG_namespace
8269 && strcmp (parent->name, "::") == 0
8270 && grandparent_scope == NULL)
8271 {
8272 parent->scope = NULL;
8273 parent->scope_set = 1;
8274 return NULL;
8275 }
8276
8277 /* Nested subroutines in Fortran get a prefix. */
8278 if (pdi->tag == DW_TAG_enumerator)
8279 /* Enumerators should not get the name of the enumeration as a prefix. */
8280 parent->scope = grandparent_scope;
8281 else if (parent->tag == DW_TAG_namespace
8282 || parent->tag == DW_TAG_module
8283 || parent->tag == DW_TAG_structure_type
8284 || parent->tag == DW_TAG_class_type
8285 || parent->tag == DW_TAG_interface_type
8286 || parent->tag == DW_TAG_union_type
8287 || parent->tag == DW_TAG_enumeration_type
8288 || (cu->language == language_fortran
8289 && parent->tag == DW_TAG_subprogram
8290 && pdi->tag == DW_TAG_subprogram))
8291 {
8292 if (grandparent_scope == NULL)
8293 parent->scope = parent->name;
8294 else
8295 parent->scope = typename_concat (&cu->comp_unit_obstack,
8296 grandparent_scope,
8297 parent->name, 0, cu);
8298 }
8299 else
8300 {
8301 /* FIXME drow/2004-04-01: What should we be doing with
8302 function-local names? For partial symbols, we should probably be
8303 ignoring them. */
8304 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8305 dwarf_tag_name (parent->tag),
8306 sect_offset_str (pdi->sect_off));
8307 parent->scope = grandparent_scope;
8308 }
8309
8310 parent->scope_set = 1;
8311 return parent->scope;
8312 }
8313
8314 /* Return the fully scoped name associated with PDI, from compilation unit
8315 CU. The result will be allocated with malloc. */
8316
8317 static gdb::unique_xmalloc_ptr<char>
8318 partial_die_full_name (struct partial_die_info *pdi,
8319 struct dwarf2_cu *cu)
8320 {
8321 const char *parent_scope;
8322
8323 /* If this is a template instantiation, we can not work out the
8324 template arguments from partial DIEs. So, unfortunately, we have
8325 to go through the full DIEs. At least any work we do building
8326 types here will be reused if full symbols are loaded later. */
8327 if (pdi->has_template_arguments)
8328 {
8329 pdi->fixup (cu);
8330
8331 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8332 {
8333 struct die_info *die;
8334 struct attribute attr;
8335 struct dwarf2_cu *ref_cu = cu;
8336
8337 /* DW_FORM_ref_addr is using section offset. */
8338 attr.name = (enum dwarf_attribute) 0;
8339 attr.form = DW_FORM_ref_addr;
8340 attr.u.unsnd = to_underlying (pdi->sect_off);
8341 die = follow_die_ref (NULL, &attr, &ref_cu);
8342
8343 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8344 }
8345 }
8346
8347 parent_scope = partial_die_parent_scope (pdi, cu);
8348 if (parent_scope == NULL)
8349 return NULL;
8350 else
8351 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8352 pdi->name, 0, cu));
8353 }
8354
8355 static void
8356 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8357 {
8358 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
8359 struct objfile *objfile = dwarf2_per_objfile->objfile;
8360 struct gdbarch *gdbarch = objfile->arch ();
8361 CORE_ADDR addr = 0;
8362 const char *actual_name = NULL;
8363 CORE_ADDR baseaddr;
8364
8365 baseaddr = objfile->text_section_offset ();
8366
8367 gdb::unique_xmalloc_ptr<char> built_actual_name
8368 = partial_die_full_name (pdi, cu);
8369 if (built_actual_name != NULL)
8370 actual_name = built_actual_name.get ();
8371
8372 if (actual_name == NULL)
8373 actual_name = pdi->name;
8374
8375 partial_symbol psymbol;
8376 memset (&psymbol, 0, sizeof (psymbol));
8377 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8378 psymbol.ginfo.section = -1;
8379
8380 /* The code below indicates that the psymbol should be installed by
8381 setting this. */
8382 gdb::optional<psymbol_placement> where;
8383
8384 switch (pdi->tag)
8385 {
8386 case DW_TAG_inlined_subroutine:
8387 case DW_TAG_subprogram:
8388 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8389 - baseaddr);
8390 if (pdi->is_external
8391 || cu->language == language_ada
8392 || (cu->language == language_fortran
8393 && pdi->die_parent != NULL
8394 && pdi->die_parent->tag == DW_TAG_subprogram))
8395 {
8396 /* Normally, only "external" DIEs are part of the global scope.
8397 But in Ada and Fortran, we want to be able to access nested
8398 procedures globally. So all Ada and Fortran subprograms are
8399 stored in the global scope. */
8400 where = psymbol_placement::GLOBAL;
8401 }
8402 else
8403 where = psymbol_placement::STATIC;
8404
8405 psymbol.domain = VAR_DOMAIN;
8406 psymbol.aclass = LOC_BLOCK;
8407 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8408 psymbol.ginfo.value.address = addr;
8409
8410 if (pdi->main_subprogram && actual_name != NULL)
8411 set_objfile_main_name (objfile, actual_name, cu->language);
8412 break;
8413 case DW_TAG_constant:
8414 psymbol.domain = VAR_DOMAIN;
8415 psymbol.aclass = LOC_STATIC;
8416 where = (pdi->is_external
8417 ? psymbol_placement::GLOBAL
8418 : psymbol_placement::STATIC);
8419 break;
8420 case DW_TAG_variable:
8421 if (pdi->d.locdesc)
8422 addr = decode_locdesc (pdi->d.locdesc, cu);
8423
8424 if (pdi->d.locdesc
8425 && addr == 0
8426 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
8427 {
8428 /* A global or static variable may also have been stripped
8429 out by the linker if unused, in which case its address
8430 will be nullified; do not add such variables into partial
8431 symbol table then. */
8432 }
8433 else if (pdi->is_external)
8434 {
8435 /* Global Variable.
8436 Don't enter into the minimal symbol tables as there is
8437 a minimal symbol table entry from the ELF symbols already.
8438 Enter into partial symbol table if it has a location
8439 descriptor or a type.
8440 If the location descriptor is missing, new_symbol will create
8441 a LOC_UNRESOLVED symbol, the address of the variable will then
8442 be determined from the minimal symbol table whenever the variable
8443 is referenced.
8444 The address for the partial symbol table entry is not
8445 used by GDB, but it comes in handy for debugging partial symbol
8446 table building. */
8447
8448 if (pdi->d.locdesc || pdi->has_type)
8449 {
8450 psymbol.domain = VAR_DOMAIN;
8451 psymbol.aclass = LOC_STATIC;
8452 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8453 psymbol.ginfo.value.address = addr;
8454 where = psymbol_placement::GLOBAL;
8455 }
8456 }
8457 else
8458 {
8459 int has_loc = pdi->d.locdesc != NULL;
8460
8461 /* Static Variable. Skip symbols whose value we cannot know (those
8462 without location descriptors or constant values). */
8463 if (!has_loc && !pdi->has_const_value)
8464 return;
8465
8466 psymbol.domain = VAR_DOMAIN;
8467 psymbol.aclass = LOC_STATIC;
8468 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8469 if (has_loc)
8470 psymbol.ginfo.value.address = addr;
8471 where = psymbol_placement::STATIC;
8472 }
8473 break;
8474 case DW_TAG_typedef:
8475 case DW_TAG_base_type:
8476 case DW_TAG_subrange_type:
8477 psymbol.domain = VAR_DOMAIN;
8478 psymbol.aclass = LOC_TYPEDEF;
8479 where = psymbol_placement::STATIC;
8480 break;
8481 case DW_TAG_imported_declaration:
8482 case DW_TAG_namespace:
8483 psymbol.domain = VAR_DOMAIN;
8484 psymbol.aclass = LOC_TYPEDEF;
8485 where = psymbol_placement::GLOBAL;
8486 break;
8487 case DW_TAG_module:
8488 /* With Fortran 77 there might be a "BLOCK DATA" module
8489 available without any name. If so, we skip the module as it
8490 doesn't bring any value. */
8491 if (actual_name != nullptr)
8492 {
8493 psymbol.domain = MODULE_DOMAIN;
8494 psymbol.aclass = LOC_TYPEDEF;
8495 where = psymbol_placement::GLOBAL;
8496 }
8497 break;
8498 case DW_TAG_class_type:
8499 case DW_TAG_interface_type:
8500 case DW_TAG_structure_type:
8501 case DW_TAG_union_type:
8502 case DW_TAG_enumeration_type:
8503 /* Skip external references. The DWARF standard says in the section
8504 about "Structure, Union, and Class Type Entries": "An incomplete
8505 structure, union or class type is represented by a structure,
8506 union or class entry that does not have a byte size attribute
8507 and that has a DW_AT_declaration attribute." */
8508 if (!pdi->has_byte_size && pdi->is_declaration)
8509 return;
8510
8511 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8512 static vs. global. */
8513 psymbol.domain = STRUCT_DOMAIN;
8514 psymbol.aclass = LOC_TYPEDEF;
8515 where = (cu->language == language_cplus
8516 ? psymbol_placement::GLOBAL
8517 : psymbol_placement::STATIC);
8518 break;
8519 case DW_TAG_enumerator:
8520 psymbol.domain = VAR_DOMAIN;
8521 psymbol.aclass = LOC_CONST;
8522 where = (cu->language == language_cplus
8523 ? psymbol_placement::GLOBAL
8524 : psymbol_placement::STATIC);
8525 break;
8526 default:
8527 break;
8528 }
8529
8530 if (where.has_value ())
8531 {
8532 if (built_actual_name != nullptr)
8533 actual_name = objfile->intern (actual_name);
8534 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8535 psymbol.ginfo.set_linkage_name (actual_name);
8536 else
8537 {
8538 psymbol.ginfo.set_demangled_name (actual_name,
8539 &objfile->objfile_obstack);
8540 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8541 }
8542 add_psymbol_to_list (psymbol, *where, objfile);
8543 }
8544 }
8545
8546 /* Read a partial die corresponding to a namespace; also, add a symbol
8547 corresponding to that namespace to the symbol table. NAMESPACE is
8548 the name of the enclosing namespace. */
8549
8550 static void
8551 add_partial_namespace (struct partial_die_info *pdi,
8552 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8553 int set_addrmap, struct dwarf2_cu *cu)
8554 {
8555 /* Add a symbol for the namespace. */
8556
8557 add_partial_symbol (pdi, cu);
8558
8559 /* Now scan partial symbols in that namespace. */
8560
8561 if (pdi->has_children)
8562 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8563 }
8564
8565 /* Read a partial die corresponding to a Fortran module. */
8566
8567 static void
8568 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8569 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8570 {
8571 /* Add a symbol for the namespace. */
8572
8573 add_partial_symbol (pdi, cu);
8574
8575 /* Now scan partial symbols in that module. */
8576
8577 if (pdi->has_children)
8578 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8579 }
8580
8581 /* Read a partial die corresponding to a subprogram or an inlined
8582 subprogram and create a partial symbol for that subprogram.
8583 When the CU language allows it, this routine also defines a partial
8584 symbol for each nested subprogram that this subprogram contains.
8585 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8586 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8587
8588 PDI may also be a lexical block, in which case we simply search
8589 recursively for subprograms defined inside that lexical block.
8590 Again, this is only performed when the CU language allows this
8591 type of definitions. */
8592
8593 static void
8594 add_partial_subprogram (struct partial_die_info *pdi,
8595 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8596 int set_addrmap, struct dwarf2_cu *cu)
8597 {
8598 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8599 {
8600 if (pdi->has_pc_info)
8601 {
8602 if (pdi->lowpc < *lowpc)
8603 *lowpc = pdi->lowpc;
8604 if (pdi->highpc > *highpc)
8605 *highpc = pdi->highpc;
8606 if (set_addrmap)
8607 {
8608 struct objfile *objfile = cu->per_objfile->objfile;
8609 struct gdbarch *gdbarch = objfile->arch ();
8610 CORE_ADDR baseaddr;
8611 CORE_ADDR this_highpc;
8612 CORE_ADDR this_lowpc;
8613
8614 baseaddr = objfile->text_section_offset ();
8615 this_lowpc
8616 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8617 pdi->lowpc + baseaddr)
8618 - baseaddr);
8619 this_highpc
8620 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8621 pdi->highpc + baseaddr)
8622 - baseaddr);
8623 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8624 this_lowpc, this_highpc - 1,
8625 cu->per_cu->v.psymtab);
8626 }
8627 }
8628
8629 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8630 {
8631 if (!pdi->is_declaration)
8632 /* Ignore subprogram DIEs that do not have a name, they are
8633 illegal. Do not emit a complaint at this point, we will
8634 do so when we convert this psymtab into a symtab. */
8635 if (pdi->name)
8636 add_partial_symbol (pdi, cu);
8637 }
8638 }
8639
8640 if (! pdi->has_children)
8641 return;
8642
8643 if (cu->language == language_ada || cu->language == language_fortran)
8644 {
8645 pdi = pdi->die_child;
8646 while (pdi != NULL)
8647 {
8648 pdi->fixup (cu);
8649 if (pdi->tag == DW_TAG_subprogram
8650 || pdi->tag == DW_TAG_inlined_subroutine
8651 || pdi->tag == DW_TAG_lexical_block)
8652 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8653 pdi = pdi->die_sibling;
8654 }
8655 }
8656 }
8657
8658 /* Read a partial die corresponding to an enumeration type. */
8659
8660 static void
8661 add_partial_enumeration (struct partial_die_info *enum_pdi,
8662 struct dwarf2_cu *cu)
8663 {
8664 struct partial_die_info *pdi;
8665
8666 if (enum_pdi->name != NULL)
8667 add_partial_symbol (enum_pdi, cu);
8668
8669 pdi = enum_pdi->die_child;
8670 while (pdi)
8671 {
8672 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8673 complaint (_("malformed enumerator DIE ignored"));
8674 else
8675 add_partial_symbol (pdi, cu);
8676 pdi = pdi->die_sibling;
8677 }
8678 }
8679
8680 /* Return the initial uleb128 in the die at INFO_PTR. */
8681
8682 static unsigned int
8683 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8684 {
8685 unsigned int bytes_read;
8686
8687 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8688 }
8689
8690 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8691 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8692
8693 Return the corresponding abbrev, or NULL if the number is zero (indicating
8694 an empty DIE). In either case *BYTES_READ will be set to the length of
8695 the initial number. */
8696
8697 static struct abbrev_info *
8698 peek_die_abbrev (const die_reader_specs &reader,
8699 const gdb_byte *info_ptr, unsigned int *bytes_read)
8700 {
8701 dwarf2_cu *cu = reader.cu;
8702 bfd *abfd = cu->per_objfile->objfile->obfd;
8703 unsigned int abbrev_number
8704 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8705
8706 if (abbrev_number == 0)
8707 return NULL;
8708
8709 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8710 if (!abbrev)
8711 {
8712 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8713 " at offset %s [in module %s]"),
8714 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8715 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8716 }
8717
8718 return abbrev;
8719 }
8720
8721 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8722 Returns a pointer to the end of a series of DIEs, terminated by an empty
8723 DIE. Any children of the skipped DIEs will also be skipped. */
8724
8725 static const gdb_byte *
8726 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8727 {
8728 while (1)
8729 {
8730 unsigned int bytes_read;
8731 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8732
8733 if (abbrev == NULL)
8734 return info_ptr + bytes_read;
8735 else
8736 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8737 }
8738 }
8739
8740 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8741 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8742 abbrev corresponding to that skipped uleb128 should be passed in
8743 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8744 children. */
8745
8746 static const gdb_byte *
8747 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8748 struct abbrev_info *abbrev)
8749 {
8750 unsigned int bytes_read;
8751 struct attribute attr;
8752 bfd *abfd = reader->abfd;
8753 struct dwarf2_cu *cu = reader->cu;
8754 const gdb_byte *buffer = reader->buffer;
8755 const gdb_byte *buffer_end = reader->buffer_end;
8756 unsigned int form, i;
8757
8758 for (i = 0; i < abbrev->num_attrs; i++)
8759 {
8760 /* The only abbrev we care about is DW_AT_sibling. */
8761 if (abbrev->attrs[i].name == DW_AT_sibling)
8762 {
8763 bool ignored;
8764 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8765 &ignored);
8766 if (attr.form == DW_FORM_ref_addr)
8767 complaint (_("ignoring absolute DW_AT_sibling"));
8768 else
8769 {
8770 sect_offset off = attr.get_ref_die_offset ();
8771 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8772
8773 if (sibling_ptr < info_ptr)
8774 complaint (_("DW_AT_sibling points backwards"));
8775 else if (sibling_ptr > reader->buffer_end)
8776 reader->die_section->overflow_complaint ();
8777 else
8778 return sibling_ptr;
8779 }
8780 }
8781
8782 /* If it isn't DW_AT_sibling, skip this attribute. */
8783 form = abbrev->attrs[i].form;
8784 skip_attribute:
8785 switch (form)
8786 {
8787 case DW_FORM_ref_addr:
8788 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8789 and later it is offset sized. */
8790 if (cu->header.version == 2)
8791 info_ptr += cu->header.addr_size;
8792 else
8793 info_ptr += cu->header.offset_size;
8794 break;
8795 case DW_FORM_GNU_ref_alt:
8796 info_ptr += cu->header.offset_size;
8797 break;
8798 case DW_FORM_addr:
8799 info_ptr += cu->header.addr_size;
8800 break;
8801 case DW_FORM_data1:
8802 case DW_FORM_ref1:
8803 case DW_FORM_flag:
8804 case DW_FORM_strx1:
8805 info_ptr += 1;
8806 break;
8807 case DW_FORM_flag_present:
8808 case DW_FORM_implicit_const:
8809 break;
8810 case DW_FORM_data2:
8811 case DW_FORM_ref2:
8812 case DW_FORM_strx2:
8813 info_ptr += 2;
8814 break;
8815 case DW_FORM_strx3:
8816 info_ptr += 3;
8817 break;
8818 case DW_FORM_data4:
8819 case DW_FORM_ref4:
8820 case DW_FORM_strx4:
8821 info_ptr += 4;
8822 break;
8823 case DW_FORM_data8:
8824 case DW_FORM_ref8:
8825 case DW_FORM_ref_sig8:
8826 info_ptr += 8;
8827 break;
8828 case DW_FORM_data16:
8829 info_ptr += 16;
8830 break;
8831 case DW_FORM_string:
8832 read_direct_string (abfd, info_ptr, &bytes_read);
8833 info_ptr += bytes_read;
8834 break;
8835 case DW_FORM_sec_offset:
8836 case DW_FORM_strp:
8837 case DW_FORM_GNU_strp_alt:
8838 info_ptr += cu->header.offset_size;
8839 break;
8840 case DW_FORM_exprloc:
8841 case DW_FORM_block:
8842 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8843 info_ptr += bytes_read;
8844 break;
8845 case DW_FORM_block1:
8846 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8847 break;
8848 case DW_FORM_block2:
8849 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8850 break;
8851 case DW_FORM_block4:
8852 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8853 break;
8854 case DW_FORM_addrx:
8855 case DW_FORM_strx:
8856 case DW_FORM_sdata:
8857 case DW_FORM_udata:
8858 case DW_FORM_ref_udata:
8859 case DW_FORM_GNU_addr_index:
8860 case DW_FORM_GNU_str_index:
8861 case DW_FORM_rnglistx:
8862 case DW_FORM_loclistx:
8863 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8864 break;
8865 case DW_FORM_indirect:
8866 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8867 info_ptr += bytes_read;
8868 /* We need to continue parsing from here, so just go back to
8869 the top. */
8870 goto skip_attribute;
8871
8872 default:
8873 error (_("Dwarf Error: Cannot handle %s "
8874 "in DWARF reader [in module %s]"),
8875 dwarf_form_name (form),
8876 bfd_get_filename (abfd));
8877 }
8878 }
8879
8880 if (abbrev->has_children)
8881 return skip_children (reader, info_ptr);
8882 else
8883 return info_ptr;
8884 }
8885
8886 /* Locate ORIG_PDI's sibling.
8887 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8888
8889 static const gdb_byte *
8890 locate_pdi_sibling (const struct die_reader_specs *reader,
8891 struct partial_die_info *orig_pdi,
8892 const gdb_byte *info_ptr)
8893 {
8894 /* Do we know the sibling already? */
8895
8896 if (orig_pdi->sibling)
8897 return orig_pdi->sibling;
8898
8899 /* Are there any children to deal with? */
8900
8901 if (!orig_pdi->has_children)
8902 return info_ptr;
8903
8904 /* Skip the children the long way. */
8905
8906 return skip_children (reader, info_ptr);
8907 }
8908
8909 /* Expand this partial symbol table into a full symbol table. SELF is
8910 not NULL. */
8911
8912 void
8913 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8914 {
8915 struct dwarf2_per_objfile *dwarf2_per_objfile
8916 = get_dwarf2_per_objfile (objfile);
8917
8918 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8919
8920 /* If this psymtab is constructed from a debug-only objfile, the
8921 has_section_at_zero flag will not necessarily be correct. We
8922 can get the correct value for this flag by looking at the data
8923 associated with the (presumably stripped) associated objfile. */
8924 if (objfile->separate_debug_objfile_backlink)
8925 {
8926 struct dwarf2_per_objfile *dpo_backlink
8927 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8928
8929 dwarf2_per_objfile->per_bfd->has_section_at_zero
8930 = dpo_backlink->per_bfd->has_section_at_zero;
8931 }
8932
8933 expand_psymtab (objfile);
8934
8935 process_cu_includes (dwarf2_per_objfile);
8936 }
8937 \f
8938 /* Reading in full CUs. */
8939
8940 /* Add PER_CU to the queue. */
8941
8942 static void
8943 queue_comp_unit (dwarf2_per_cu_data *per_cu,
8944 dwarf2_per_objfile *per_objfile,
8945 enum language pretend_language)
8946 {
8947 per_cu->queued = 1;
8948 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
8949 }
8950
8951 /* If PER_CU is not yet queued, add it to the queue.
8952 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8953 dependency.
8954 The result is non-zero if PER_CU was queued, otherwise the result is zero
8955 meaning either PER_CU is already queued or it is already loaded.
8956
8957 N.B. There is an invariant here that if a CU is queued then it is loaded.
8958 The caller is required to load PER_CU if we return non-zero. */
8959
8960 static int
8961 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8962 dwarf2_per_cu_data *per_cu,
8963 dwarf2_per_objfile *per_objfile,
8964 enum language pretend_language)
8965 {
8966 /* We may arrive here during partial symbol reading, if we need full
8967 DIEs to process an unusual case (e.g. template arguments). Do
8968 not queue PER_CU, just tell our caller to load its DIEs. */
8969 if (per_cu->per_bfd->reading_partial_symbols)
8970 {
8971 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8972 return 1;
8973 return 0;
8974 }
8975
8976 /* Mark the dependence relation so that we don't flush PER_CU
8977 too early. */
8978 if (dependent_cu != NULL)
8979 dwarf2_add_dependence (dependent_cu, per_cu);
8980
8981 /* If it's already on the queue, we have nothing to do. */
8982 if (per_cu->queued)
8983 return 0;
8984
8985 /* If the compilation unit is already loaded, just mark it as
8986 used. */
8987 if (per_cu->cu != NULL)
8988 {
8989 per_cu->cu->last_used = 0;
8990 return 0;
8991 }
8992
8993 /* Add it to the queue. */
8994 queue_comp_unit (per_cu, per_objfile, pretend_language);
8995
8996 return 1;
8997 }
8998
8999 /* Process the queue. */
9000
9001 static void
9002 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9003 {
9004 if (dwarf_read_debug)
9005 {
9006 fprintf_unfiltered (gdb_stdlog,
9007 "Expanding one or more symtabs of objfile %s ...\n",
9008 objfile_name (dwarf2_per_objfile->objfile));
9009 }
9010
9011 /* The queue starts out with one item, but following a DIE reference
9012 may load a new CU, adding it to the end of the queue. */
9013 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
9014 {
9015 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
9016
9017 if (!dwarf2_per_objfile->symtab_set_p (item.per_cu)
9018 /* Skip dummy CUs. */
9019 && item.per_cu->cu != NULL)
9020 {
9021 struct dwarf2_per_cu_data *per_cu = item.per_cu;
9022 unsigned int debug_print_threshold;
9023 char buf[100];
9024
9025 if (per_cu->is_debug_types)
9026 {
9027 struct signatured_type *sig_type =
9028 (struct signatured_type *) per_cu;
9029
9030 sprintf (buf, "TU %s at offset %s",
9031 hex_string (sig_type->signature),
9032 sect_offset_str (per_cu->sect_off));
9033 /* There can be 100s of TUs.
9034 Only print them in verbose mode. */
9035 debug_print_threshold = 2;
9036 }
9037 else
9038 {
9039 sprintf (buf, "CU at offset %s",
9040 sect_offset_str (per_cu->sect_off));
9041 debug_print_threshold = 1;
9042 }
9043
9044 if (dwarf_read_debug >= debug_print_threshold)
9045 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9046
9047 if (per_cu->is_debug_types)
9048 process_full_type_unit (per_cu, dwarf2_per_objfile,
9049 item.pretend_language);
9050 else
9051 process_full_comp_unit (per_cu, dwarf2_per_objfile,
9052 item.pretend_language);
9053
9054 if (dwarf_read_debug >= debug_print_threshold)
9055 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9056 }
9057
9058 item.per_cu->queued = 0;
9059 dwarf2_per_objfile->per_bfd->queue.pop ();
9060 }
9061
9062 if (dwarf_read_debug)
9063 {
9064 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9065 objfile_name (dwarf2_per_objfile->objfile));
9066 }
9067 }
9068
9069 /* Read in full symbols for PST, and anything it depends on. */
9070
9071 void
9072 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9073 {
9074 gdb_assert (!readin_p (objfile));
9075
9076 expand_dependencies (objfile);
9077
9078 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9079 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9080 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9081 }
9082
9083 /* See psympriv.h. */
9084
9085 bool
9086 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9087 {
9088 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9089 return per_objfile->symtab_set_p (per_cu_data);
9090 }
9091
9092 /* See psympriv.h. */
9093
9094 compunit_symtab *
9095 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9096 {
9097 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9098 return per_objfile->get_symtab (per_cu_data);
9099 }
9100
9101 /* Trivial hash function for die_info: the hash value of a DIE
9102 is its offset in .debug_info for this objfile. */
9103
9104 static hashval_t
9105 die_hash (const void *item)
9106 {
9107 const struct die_info *die = (const struct die_info *) item;
9108
9109 return to_underlying (die->sect_off);
9110 }
9111
9112 /* Trivial comparison function for die_info structures: two DIEs
9113 are equal if they have the same offset. */
9114
9115 static int
9116 die_eq (const void *item_lhs, const void *item_rhs)
9117 {
9118 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9119 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9120
9121 return die_lhs->sect_off == die_rhs->sect_off;
9122 }
9123
9124 /* Load the DIEs associated with PER_CU into memory. */
9125
9126 static void
9127 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9128 dwarf2_per_objfile *per_objfile,
9129 bool skip_partial,
9130 enum language pretend_language)
9131 {
9132 gdb_assert (! this_cu->is_debug_types);
9133
9134 cutu_reader reader (this_cu, per_objfile, NULL, 1, skip_partial);
9135 if (reader.dummy_p)
9136 return;
9137
9138 struct dwarf2_cu *cu = reader.cu;
9139 const gdb_byte *info_ptr = reader.info_ptr;
9140
9141 gdb_assert (cu->die_hash == NULL);
9142 cu->die_hash =
9143 htab_create_alloc_ex (cu->header.length / 12,
9144 die_hash,
9145 die_eq,
9146 NULL,
9147 &cu->comp_unit_obstack,
9148 hashtab_obstack_allocate,
9149 dummy_obstack_deallocate);
9150
9151 if (reader.comp_unit_die->has_children)
9152 reader.comp_unit_die->child
9153 = read_die_and_siblings (&reader, reader.info_ptr,
9154 &info_ptr, reader.comp_unit_die);
9155 cu->dies = reader.comp_unit_die;
9156 /* comp_unit_die is not stored in die_hash, no need. */
9157
9158 /* We try not to read any attributes in this function, because not
9159 all CUs needed for references have been loaded yet, and symbol
9160 table processing isn't initialized. But we have to set the CU language,
9161 or we won't be able to build types correctly.
9162 Similarly, if we do not read the producer, we can not apply
9163 producer-specific interpretation. */
9164 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9165
9166 reader.keep ();
9167 }
9168
9169 /* Add a DIE to the delayed physname list. */
9170
9171 static void
9172 add_to_method_list (struct type *type, int fnfield_index, int index,
9173 const char *name, struct die_info *die,
9174 struct dwarf2_cu *cu)
9175 {
9176 struct delayed_method_info mi;
9177 mi.type = type;
9178 mi.fnfield_index = fnfield_index;
9179 mi.index = index;
9180 mi.name = name;
9181 mi.die = die;
9182 cu->method_list.push_back (mi);
9183 }
9184
9185 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9186 "const" / "volatile". If so, decrements LEN by the length of the
9187 modifier and return true. Otherwise return false. */
9188
9189 template<size_t N>
9190 static bool
9191 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9192 {
9193 size_t mod_len = sizeof (mod) - 1;
9194 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9195 {
9196 len -= mod_len;
9197 return true;
9198 }
9199 return false;
9200 }
9201
9202 /* Compute the physnames of any methods on the CU's method list.
9203
9204 The computation of method physnames is delayed in order to avoid the
9205 (bad) condition that one of the method's formal parameters is of an as yet
9206 incomplete type. */
9207
9208 static void
9209 compute_delayed_physnames (struct dwarf2_cu *cu)
9210 {
9211 /* Only C++ delays computing physnames. */
9212 if (cu->method_list.empty ())
9213 return;
9214 gdb_assert (cu->language == language_cplus);
9215
9216 for (const delayed_method_info &mi : cu->method_list)
9217 {
9218 const char *physname;
9219 struct fn_fieldlist *fn_flp
9220 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9221 physname = dwarf2_physname (mi.name, mi.die, cu);
9222 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9223 = physname ? physname : "";
9224
9225 /* Since there's no tag to indicate whether a method is a
9226 const/volatile overload, extract that information out of the
9227 demangled name. */
9228 if (physname != NULL)
9229 {
9230 size_t len = strlen (physname);
9231
9232 while (1)
9233 {
9234 if (physname[len] == ')') /* shortcut */
9235 break;
9236 else if (check_modifier (physname, len, " const"))
9237 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9238 else if (check_modifier (physname, len, " volatile"))
9239 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9240 else
9241 break;
9242 }
9243 }
9244 }
9245
9246 /* The list is no longer needed. */
9247 cu->method_list.clear ();
9248 }
9249
9250 /* Go objects should be embedded in a DW_TAG_module DIE,
9251 and it's not clear if/how imported objects will appear.
9252 To keep Go support simple until that's worked out,
9253 go back through what we've read and create something usable.
9254 We could do this while processing each DIE, and feels kinda cleaner,
9255 but that way is more invasive.
9256 This is to, for example, allow the user to type "p var" or "b main"
9257 without having to specify the package name, and allow lookups
9258 of module.object to work in contexts that use the expression
9259 parser. */
9260
9261 static void
9262 fixup_go_packaging (struct dwarf2_cu *cu)
9263 {
9264 gdb::unique_xmalloc_ptr<char> package_name;
9265 struct pending *list;
9266 int i;
9267
9268 for (list = *cu->get_builder ()->get_global_symbols ();
9269 list != NULL;
9270 list = list->next)
9271 {
9272 for (i = 0; i < list->nsyms; ++i)
9273 {
9274 struct symbol *sym = list->symbol[i];
9275
9276 if (sym->language () == language_go
9277 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9278 {
9279 gdb::unique_xmalloc_ptr<char> this_package_name
9280 (go_symbol_package_name (sym));
9281
9282 if (this_package_name == NULL)
9283 continue;
9284 if (package_name == NULL)
9285 package_name = std::move (this_package_name);
9286 else
9287 {
9288 struct objfile *objfile = cu->per_objfile->objfile;
9289 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9290 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9291 (symbol_symtab (sym) != NULL
9292 ? symtab_to_filename_for_display
9293 (symbol_symtab (sym))
9294 : objfile_name (objfile)),
9295 this_package_name.get (), package_name.get ());
9296 }
9297 }
9298 }
9299 }
9300
9301 if (package_name != NULL)
9302 {
9303 struct objfile *objfile = cu->per_objfile->objfile;
9304 const char *saved_package_name = objfile->intern (package_name.get ());
9305 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9306 saved_package_name);
9307 struct symbol *sym;
9308
9309 sym = new (&objfile->objfile_obstack) symbol;
9310 sym->set_language (language_go, &objfile->objfile_obstack);
9311 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9312 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9313 e.g., "main" finds the "main" module and not C's main(). */
9314 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9315 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9316 SYMBOL_TYPE (sym) = type;
9317
9318 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9319 }
9320 }
9321
9322 /* Allocate a fully-qualified name consisting of the two parts on the
9323 obstack. */
9324
9325 static const char *
9326 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9327 {
9328 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9329 }
9330
9331 /* A helper that allocates a variant part to attach to a Rust enum
9332 type. OBSTACK is where the results should be allocated. TYPE is
9333 the type we're processing. DISCRIMINANT_INDEX is the index of the
9334 discriminant. It must be the index of one of the fields of TYPE.
9335 DEFAULT_INDEX is the index of the default field; or -1 if there is
9336 no default. RANGES is indexed by "effective" field number (the
9337 field index, but omitting the discriminant and default fields) and
9338 must hold the discriminant values used by the variants. Note that
9339 RANGES must have a lifetime at least as long as OBSTACK -- either
9340 already allocated on it, or static. */
9341
9342 static void
9343 alloc_rust_variant (struct obstack *obstack, struct type *type,
9344 int discriminant_index, int default_index,
9345 gdb::array_view<discriminant_range> ranges)
9346 {
9347 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9348 must be handled by the caller. */
9349 gdb_assert (discriminant_index >= 0
9350 && discriminant_index < type->num_fields ());
9351 gdb_assert (default_index == -1
9352 || (default_index >= 0 && default_index < type->num_fields ()));
9353
9354 /* We have one variant for each non-discriminant field. */
9355 int n_variants = type->num_fields () - 1;
9356
9357 variant *variants = new (obstack) variant[n_variants];
9358 int var_idx = 0;
9359 int range_idx = 0;
9360 for (int i = 0; i < type->num_fields (); ++i)
9361 {
9362 if (i == discriminant_index)
9363 continue;
9364
9365 variants[var_idx].first_field = i;
9366 variants[var_idx].last_field = i + 1;
9367
9368 /* The default field does not need a range, but other fields do.
9369 We skipped the discriminant above. */
9370 if (i != default_index)
9371 {
9372 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9373 ++range_idx;
9374 }
9375
9376 ++var_idx;
9377 }
9378
9379 gdb_assert (range_idx == ranges.size ());
9380 gdb_assert (var_idx == n_variants);
9381
9382 variant_part *part = new (obstack) variant_part;
9383 part->discriminant_index = discriminant_index;
9384 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9385 discriminant_index));
9386 part->variants = gdb::array_view<variant> (variants, n_variants);
9387
9388 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9389 gdb::array_view<variant_part> *prop_value
9390 = new (storage) gdb::array_view<variant_part> (part, 1);
9391
9392 struct dynamic_prop prop;
9393 prop.kind = PROP_VARIANT_PARTS;
9394 prop.data.variant_parts = prop_value;
9395
9396 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9397 }
9398
9399 /* Some versions of rustc emitted enums in an unusual way.
9400
9401 Ordinary enums were emitted as unions. The first element of each
9402 structure in the union was named "RUST$ENUM$DISR". This element
9403 held the discriminant.
9404
9405 These versions of Rust also implemented the "non-zero"
9406 optimization. When the enum had two values, and one is empty and
9407 the other holds a pointer that cannot be zero, the pointer is used
9408 as the discriminant, with a zero value meaning the empty variant.
9409 Here, the union's first member is of the form
9410 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9411 where the fieldnos are the indices of the fields that should be
9412 traversed in order to find the field (which may be several fields deep)
9413 and the variantname is the name of the variant of the case when the
9414 field is zero.
9415
9416 This function recognizes whether TYPE is of one of these forms,
9417 and, if so, smashes it to be a variant type. */
9418
9419 static void
9420 quirk_rust_enum (struct type *type, struct objfile *objfile)
9421 {
9422 gdb_assert (type->code () == TYPE_CODE_UNION);
9423
9424 /* We don't need to deal with empty enums. */
9425 if (type->num_fields () == 0)
9426 return;
9427
9428 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9429 if (type->num_fields () == 1
9430 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9431 {
9432 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9433
9434 /* Decode the field name to find the offset of the
9435 discriminant. */
9436 ULONGEST bit_offset = 0;
9437 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9438 while (name[0] >= '0' && name[0] <= '9')
9439 {
9440 char *tail;
9441 unsigned long index = strtoul (name, &tail, 10);
9442 name = tail;
9443 if (*name != '$'
9444 || index >= field_type->num_fields ()
9445 || (TYPE_FIELD_LOC_KIND (field_type, index)
9446 != FIELD_LOC_KIND_BITPOS))
9447 {
9448 complaint (_("Could not parse Rust enum encoding string \"%s\""
9449 "[in module %s]"),
9450 TYPE_FIELD_NAME (type, 0),
9451 objfile_name (objfile));
9452 return;
9453 }
9454 ++name;
9455
9456 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9457 field_type = TYPE_FIELD_TYPE (field_type, index);
9458 }
9459
9460 /* Smash this type to be a structure type. We have to do this
9461 because the type has already been recorded. */
9462 type->set_code (TYPE_CODE_STRUCT);
9463 type->set_num_fields (3);
9464 /* Save the field we care about. */
9465 struct field saved_field = type->field (0);
9466 type->set_fields
9467 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9468
9469 /* Put the discriminant at index 0. */
9470 TYPE_FIELD_TYPE (type, 0) = field_type;
9471 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9472 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9473 SET_FIELD_BITPOS (type->field (0), bit_offset);
9474
9475 /* The order of fields doesn't really matter, so put the real
9476 field at index 1 and the data-less field at index 2. */
9477 type->field (1) = saved_field;
9478 TYPE_FIELD_NAME (type, 1)
9479 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
9480 TYPE_FIELD_TYPE (type, 1)->set_name
9481 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9482 TYPE_FIELD_NAME (type, 1)));
9483
9484 const char *dataless_name
9485 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9486 name);
9487 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9488 dataless_name);
9489 TYPE_FIELD_TYPE (type, 2) = dataless_type;
9490 /* NAME points into the original discriminant name, which
9491 already has the correct lifetime. */
9492 TYPE_FIELD_NAME (type, 2) = name;
9493 SET_FIELD_BITPOS (type->field (2), 0);
9494
9495 /* Indicate that this is a variant type. */
9496 static discriminant_range ranges[1] = { { 0, 0 } };
9497 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9498 }
9499 /* A union with a single anonymous field is probably an old-style
9500 univariant enum. */
9501 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9502 {
9503 /* Smash this type to be a structure type. We have to do this
9504 because the type has already been recorded. */
9505 type->set_code (TYPE_CODE_STRUCT);
9506
9507 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9508 const char *variant_name
9509 = rust_last_path_segment (field_type->name ());
9510 TYPE_FIELD_NAME (type, 0) = variant_name;
9511 field_type->set_name
9512 (rust_fully_qualify (&objfile->objfile_obstack,
9513 type->name (), variant_name));
9514 }
9515 else
9516 {
9517 struct type *disr_type = nullptr;
9518 for (int i = 0; i < type->num_fields (); ++i)
9519 {
9520 disr_type = TYPE_FIELD_TYPE (type, i);
9521
9522 if (disr_type->code () != TYPE_CODE_STRUCT)
9523 {
9524 /* All fields of a true enum will be structs. */
9525 return;
9526 }
9527 else if (disr_type->num_fields () == 0)
9528 {
9529 /* Could be data-less variant, so keep going. */
9530 disr_type = nullptr;
9531 }
9532 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9533 "RUST$ENUM$DISR") != 0)
9534 {
9535 /* Not a Rust enum. */
9536 return;
9537 }
9538 else
9539 {
9540 /* Found one. */
9541 break;
9542 }
9543 }
9544
9545 /* If we got here without a discriminant, then it's probably
9546 just a union. */
9547 if (disr_type == nullptr)
9548 return;
9549
9550 /* Smash this type to be a structure type. We have to do this
9551 because the type has already been recorded. */
9552 type->set_code (TYPE_CODE_STRUCT);
9553
9554 /* Make space for the discriminant field. */
9555 struct field *disr_field = &disr_type->field (0);
9556 field *new_fields
9557 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9558 * sizeof (struct field)));
9559 memcpy (new_fields + 1, type->fields (),
9560 type->num_fields () * sizeof (struct field));
9561 type->set_fields (new_fields);
9562 type->set_num_fields (type->num_fields () + 1);
9563
9564 /* Install the discriminant at index 0 in the union. */
9565 type->field (0) = *disr_field;
9566 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9567 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9568
9569 /* We need a way to find the correct discriminant given a
9570 variant name. For convenience we build a map here. */
9571 struct type *enum_type = FIELD_TYPE (*disr_field);
9572 std::unordered_map<std::string, ULONGEST> discriminant_map;
9573 for (int i = 0; i < enum_type->num_fields (); ++i)
9574 {
9575 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9576 {
9577 const char *name
9578 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9579 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9580 }
9581 }
9582
9583 int n_fields = type->num_fields ();
9584 /* We don't need a range entry for the discriminant, but we do
9585 need one for every other field, as there is no default
9586 variant. */
9587 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9588 discriminant_range,
9589 n_fields - 1);
9590 /* Skip the discriminant here. */
9591 for (int i = 1; i < n_fields; ++i)
9592 {
9593 /* Find the final word in the name of this variant's type.
9594 That name can be used to look up the correct
9595 discriminant. */
9596 const char *variant_name
9597 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
9598
9599 auto iter = discriminant_map.find (variant_name);
9600 if (iter != discriminant_map.end ())
9601 {
9602 ranges[i].low = iter->second;
9603 ranges[i].high = iter->second;
9604 }
9605
9606 /* Remove the discriminant field, if it exists. */
9607 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
9608 if (sub_type->num_fields () > 0)
9609 {
9610 sub_type->set_num_fields (sub_type->num_fields () - 1);
9611 sub_type->set_fields (sub_type->fields () + 1);
9612 }
9613 TYPE_FIELD_NAME (type, i) = variant_name;
9614 sub_type->set_name
9615 (rust_fully_qualify (&objfile->objfile_obstack,
9616 type->name (), variant_name));
9617 }
9618
9619 /* Indicate that this is a variant type. */
9620 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9621 gdb::array_view<discriminant_range> (ranges,
9622 n_fields - 1));
9623 }
9624 }
9625
9626 /* Rewrite some Rust unions to be structures with variants parts. */
9627
9628 static void
9629 rust_union_quirks (struct dwarf2_cu *cu)
9630 {
9631 gdb_assert (cu->language == language_rust);
9632 for (type *type_ : cu->rust_unions)
9633 quirk_rust_enum (type_, cu->per_objfile->objfile);
9634 /* We don't need this any more. */
9635 cu->rust_unions.clear ();
9636 }
9637
9638 /* A helper function for computing the list of all symbol tables
9639 included by PER_CU. */
9640
9641 static void
9642 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9643 htab_t all_children, htab_t all_type_symtabs,
9644 dwarf2_per_cu_data *per_cu,
9645 dwarf2_per_objfile *per_objfile,
9646 struct compunit_symtab *immediate_parent)
9647 {
9648 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9649 if (*slot != NULL)
9650 {
9651 /* This inclusion and its children have been processed. */
9652 return;
9653 }
9654
9655 *slot = per_cu;
9656
9657 /* Only add a CU if it has a symbol table. */
9658 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9659 if (cust != NULL)
9660 {
9661 /* If this is a type unit only add its symbol table if we haven't
9662 seen it yet (type unit per_cu's can share symtabs). */
9663 if (per_cu->is_debug_types)
9664 {
9665 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9666 if (*slot == NULL)
9667 {
9668 *slot = cust;
9669 result->push_back (cust);
9670 if (cust->user == NULL)
9671 cust->user = immediate_parent;
9672 }
9673 }
9674 else
9675 {
9676 result->push_back (cust);
9677 if (cust->user == NULL)
9678 cust->user = immediate_parent;
9679 }
9680 }
9681
9682 if (!per_cu->imported_symtabs_empty ())
9683 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9684 {
9685 recursively_compute_inclusions (result, all_children,
9686 all_type_symtabs, ptr, per_objfile,
9687 cust);
9688 }
9689 }
9690
9691 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9692 PER_CU. */
9693
9694 static void
9695 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9696 dwarf2_per_objfile *per_objfile)
9697 {
9698 gdb_assert (! per_cu->is_debug_types);
9699
9700 if (!per_cu->imported_symtabs_empty ())
9701 {
9702 int len;
9703 std::vector<compunit_symtab *> result_symtabs;
9704 htab_t all_children, all_type_symtabs;
9705 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9706
9707 /* If we don't have a symtab, we can just skip this case. */
9708 if (cust == NULL)
9709 return;
9710
9711 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9712 NULL, xcalloc, xfree);
9713 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9714 NULL, xcalloc, xfree);
9715
9716 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9717 {
9718 recursively_compute_inclusions (&result_symtabs, all_children,
9719 all_type_symtabs, ptr, per_objfile,
9720 cust);
9721 }
9722
9723 /* Now we have a transitive closure of all the included symtabs. */
9724 len = result_symtabs.size ();
9725 cust->includes
9726 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9727 struct compunit_symtab *, len + 1);
9728 memcpy (cust->includes, result_symtabs.data (),
9729 len * sizeof (compunit_symtab *));
9730 cust->includes[len] = NULL;
9731
9732 htab_delete (all_children);
9733 htab_delete (all_type_symtabs);
9734 }
9735 }
9736
9737 /* Compute the 'includes' field for the symtabs of all the CUs we just
9738 read. */
9739
9740 static void
9741 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9742 {
9743 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
9744 {
9745 if (! iter->is_debug_types)
9746 compute_compunit_symtab_includes (iter, dwarf2_per_objfile);
9747 }
9748
9749 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
9750 }
9751
9752 /* Generate full symbol information for PER_CU, whose DIEs have
9753 already been loaded into memory. */
9754
9755 static void
9756 process_full_comp_unit (dwarf2_per_cu_data *per_cu,
9757 dwarf2_per_objfile *dwarf2_per_objfile,
9758 enum language pretend_language)
9759 {
9760 struct dwarf2_cu *cu = per_cu->cu;
9761 struct objfile *objfile = dwarf2_per_objfile->objfile;
9762 struct gdbarch *gdbarch = objfile->arch ();
9763 CORE_ADDR lowpc, highpc;
9764 struct compunit_symtab *cust;
9765 CORE_ADDR baseaddr;
9766 struct block *static_block;
9767 CORE_ADDR addr;
9768
9769 baseaddr = objfile->text_section_offset ();
9770
9771 /* Clear the list here in case something was left over. */
9772 cu->method_list.clear ();
9773
9774 cu->language = pretend_language;
9775 cu->language_defn = language_def (cu->language);
9776
9777 /* Do line number decoding in read_file_scope () */
9778 process_die (cu->dies, cu);
9779
9780 /* For now fudge the Go package. */
9781 if (cu->language == language_go)
9782 fixup_go_packaging (cu);
9783
9784 /* Now that we have processed all the DIEs in the CU, all the types
9785 should be complete, and it should now be safe to compute all of the
9786 physnames. */
9787 compute_delayed_physnames (cu);
9788
9789 if (cu->language == language_rust)
9790 rust_union_quirks (cu);
9791
9792 /* Some compilers don't define a DW_AT_high_pc attribute for the
9793 compilation unit. If the DW_AT_high_pc is missing, synthesize
9794 it, by scanning the DIE's below the compilation unit. */
9795 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9796
9797 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9798 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9799
9800 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9801 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9802 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9803 addrmap to help ensure it has an accurate map of pc values belonging to
9804 this comp unit. */
9805 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9806
9807 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9808 SECT_OFF_TEXT (objfile),
9809 0);
9810
9811 if (cust != NULL)
9812 {
9813 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9814
9815 /* Set symtab language to language from DW_AT_language. If the
9816 compilation is from a C file generated by language preprocessors, do
9817 not set the language if it was already deduced by start_subfile. */
9818 if (!(cu->language == language_c
9819 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9820 COMPUNIT_FILETABS (cust)->language = cu->language;
9821
9822 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9823 produce DW_AT_location with location lists but it can be possibly
9824 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9825 there were bugs in prologue debug info, fixed later in GCC-4.5
9826 by "unwind info for epilogues" patch (which is not directly related).
9827
9828 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9829 needed, it would be wrong due to missing DW_AT_producer there.
9830
9831 Still one can confuse GDB by using non-standard GCC compilation
9832 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9833 */
9834 if (cu->has_loclist && gcc_4_minor >= 5)
9835 cust->locations_valid = 1;
9836
9837 if (gcc_4_minor >= 5)
9838 cust->epilogue_unwind_valid = 1;
9839
9840 cust->call_site_htab = cu->call_site_htab;
9841 }
9842
9843 dwarf2_per_objfile->set_symtab (per_cu, cust);
9844
9845 /* Push it for inclusion processing later. */
9846 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (per_cu);
9847
9848 /* Not needed any more. */
9849 cu->reset_builder ();
9850 }
9851
9852 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9853 already been loaded into memory. */
9854
9855 static void
9856 process_full_type_unit (dwarf2_per_cu_data *per_cu,
9857 dwarf2_per_objfile *dwarf2_per_objfile,
9858 enum language pretend_language)
9859 {
9860 struct dwarf2_cu *cu = per_cu->cu;
9861 struct objfile *objfile = dwarf2_per_objfile->objfile;
9862 struct compunit_symtab *cust;
9863 struct signatured_type *sig_type;
9864
9865 gdb_assert (per_cu->is_debug_types);
9866 sig_type = (struct signatured_type *) per_cu;
9867
9868 /* Clear the list here in case something was left over. */
9869 cu->method_list.clear ();
9870
9871 cu->language = pretend_language;
9872 cu->language_defn = language_def (cu->language);
9873
9874 /* The symbol tables are set up in read_type_unit_scope. */
9875 process_die (cu->dies, cu);
9876
9877 /* For now fudge the Go package. */
9878 if (cu->language == language_go)
9879 fixup_go_packaging (cu);
9880
9881 /* Now that we have processed all the DIEs in the CU, all the types
9882 should be complete, and it should now be safe to compute all of the
9883 physnames. */
9884 compute_delayed_physnames (cu);
9885
9886 if (cu->language == language_rust)
9887 rust_union_quirks (cu);
9888
9889 /* TUs share symbol tables.
9890 If this is the first TU to use this symtab, complete the construction
9891 of it with end_expandable_symtab. Otherwise, complete the addition of
9892 this TU's symbols to the existing symtab. */
9893 if (sig_type->type_unit_group->compunit_symtab == NULL)
9894 {
9895 buildsym_compunit *builder = cu->get_builder ();
9896 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9897 sig_type->type_unit_group->compunit_symtab = cust;
9898
9899 if (cust != NULL)
9900 {
9901 /* Set symtab language to language from DW_AT_language. If the
9902 compilation is from a C file generated by language preprocessors,
9903 do not set the language if it was already deduced by
9904 start_subfile. */
9905 if (!(cu->language == language_c
9906 && COMPUNIT_FILETABS (cust)->language != language_c))
9907 COMPUNIT_FILETABS (cust)->language = cu->language;
9908 }
9909 }
9910 else
9911 {
9912 cu->get_builder ()->augment_type_symtab ();
9913 cust = sig_type->type_unit_group->compunit_symtab;
9914 }
9915
9916 dwarf2_per_objfile->set_symtab (per_cu, cust);
9917
9918 /* Not needed any more. */
9919 cu->reset_builder ();
9920 }
9921
9922 /* Process an imported unit DIE. */
9923
9924 static void
9925 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9926 {
9927 struct attribute *attr;
9928
9929 /* For now we don't handle imported units in type units. */
9930 if (cu->per_cu->is_debug_types)
9931 {
9932 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9933 " supported in type units [in module %s]"),
9934 objfile_name (cu->per_objfile->objfile));
9935 }
9936
9937 attr = dwarf2_attr (die, DW_AT_import, cu);
9938 if (attr != NULL)
9939 {
9940 sect_offset sect_off = attr->get_ref_die_offset ();
9941 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9942 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9943 dwarf2_per_cu_data *per_cu
9944 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
9945
9946 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9947 into another compilation unit, at root level. Regard this as a hint,
9948 and ignore it. */
9949 if (die->parent && die->parent->parent == NULL
9950 && per_cu->unit_type == DW_UT_compile
9951 && per_cu->lang == language_cplus)
9952 return;
9953
9954 /* If necessary, add it to the queue and load its DIEs. */
9955 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
9956 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
9957
9958 cu->per_cu->imported_symtabs_push (per_cu);
9959 }
9960 }
9961
9962 /* RAII object that represents a process_die scope: i.e.,
9963 starts/finishes processing a DIE. */
9964 class process_die_scope
9965 {
9966 public:
9967 process_die_scope (die_info *die, dwarf2_cu *cu)
9968 : m_die (die), m_cu (cu)
9969 {
9970 /* We should only be processing DIEs not already in process. */
9971 gdb_assert (!m_die->in_process);
9972 m_die->in_process = true;
9973 }
9974
9975 ~process_die_scope ()
9976 {
9977 m_die->in_process = false;
9978
9979 /* If we're done processing the DIE for the CU that owns the line
9980 header, we don't need the line header anymore. */
9981 if (m_cu->line_header_die_owner == m_die)
9982 {
9983 delete m_cu->line_header;
9984 m_cu->line_header = NULL;
9985 m_cu->line_header_die_owner = NULL;
9986 }
9987 }
9988
9989 private:
9990 die_info *m_die;
9991 dwarf2_cu *m_cu;
9992 };
9993
9994 /* Process a die and its children. */
9995
9996 static void
9997 process_die (struct die_info *die, struct dwarf2_cu *cu)
9998 {
9999 process_die_scope scope (die, cu);
10000
10001 switch (die->tag)
10002 {
10003 case DW_TAG_padding:
10004 break;
10005 case DW_TAG_compile_unit:
10006 case DW_TAG_partial_unit:
10007 read_file_scope (die, cu);
10008 break;
10009 case DW_TAG_type_unit:
10010 read_type_unit_scope (die, cu);
10011 break;
10012 case DW_TAG_subprogram:
10013 /* Nested subprograms in Fortran get a prefix. */
10014 if (cu->language == language_fortran
10015 && die->parent != NULL
10016 && die->parent->tag == DW_TAG_subprogram)
10017 cu->processing_has_namespace_info = true;
10018 /* Fall through. */
10019 case DW_TAG_inlined_subroutine:
10020 read_func_scope (die, cu);
10021 break;
10022 case DW_TAG_lexical_block:
10023 case DW_TAG_try_block:
10024 case DW_TAG_catch_block:
10025 read_lexical_block_scope (die, cu);
10026 break;
10027 case DW_TAG_call_site:
10028 case DW_TAG_GNU_call_site:
10029 read_call_site_scope (die, cu);
10030 break;
10031 case DW_TAG_class_type:
10032 case DW_TAG_interface_type:
10033 case DW_TAG_structure_type:
10034 case DW_TAG_union_type:
10035 process_structure_scope (die, cu);
10036 break;
10037 case DW_TAG_enumeration_type:
10038 process_enumeration_scope (die, cu);
10039 break;
10040
10041 /* These dies have a type, but processing them does not create
10042 a symbol or recurse to process the children. Therefore we can
10043 read them on-demand through read_type_die. */
10044 case DW_TAG_subroutine_type:
10045 case DW_TAG_set_type:
10046 case DW_TAG_array_type:
10047 case DW_TAG_pointer_type:
10048 case DW_TAG_ptr_to_member_type:
10049 case DW_TAG_reference_type:
10050 case DW_TAG_rvalue_reference_type:
10051 case DW_TAG_string_type:
10052 break;
10053
10054 case DW_TAG_base_type:
10055 case DW_TAG_subrange_type:
10056 case DW_TAG_typedef:
10057 /* Add a typedef symbol for the type definition, if it has a
10058 DW_AT_name. */
10059 new_symbol (die, read_type_die (die, cu), cu);
10060 break;
10061 case DW_TAG_common_block:
10062 read_common_block (die, cu);
10063 break;
10064 case DW_TAG_common_inclusion:
10065 break;
10066 case DW_TAG_namespace:
10067 cu->processing_has_namespace_info = true;
10068 read_namespace (die, cu);
10069 break;
10070 case DW_TAG_module:
10071 cu->processing_has_namespace_info = true;
10072 read_module (die, cu);
10073 break;
10074 case DW_TAG_imported_declaration:
10075 cu->processing_has_namespace_info = true;
10076 if (read_namespace_alias (die, cu))
10077 break;
10078 /* The declaration is not a global namespace alias. */
10079 /* Fall through. */
10080 case DW_TAG_imported_module:
10081 cu->processing_has_namespace_info = true;
10082 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10083 || cu->language != language_fortran))
10084 complaint (_("Tag '%s' has unexpected children"),
10085 dwarf_tag_name (die->tag));
10086 read_import_statement (die, cu);
10087 break;
10088
10089 case DW_TAG_imported_unit:
10090 process_imported_unit_die (die, cu);
10091 break;
10092
10093 case DW_TAG_variable:
10094 read_variable (die, cu);
10095 break;
10096
10097 default:
10098 new_symbol (die, NULL, cu);
10099 break;
10100 }
10101 }
10102 \f
10103 /* DWARF name computation. */
10104
10105 /* A helper function for dwarf2_compute_name which determines whether DIE
10106 needs to have the name of the scope prepended to the name listed in the
10107 die. */
10108
10109 static int
10110 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10111 {
10112 struct attribute *attr;
10113
10114 switch (die->tag)
10115 {
10116 case DW_TAG_namespace:
10117 case DW_TAG_typedef:
10118 case DW_TAG_class_type:
10119 case DW_TAG_interface_type:
10120 case DW_TAG_structure_type:
10121 case DW_TAG_union_type:
10122 case DW_TAG_enumeration_type:
10123 case DW_TAG_enumerator:
10124 case DW_TAG_subprogram:
10125 case DW_TAG_inlined_subroutine:
10126 case DW_TAG_member:
10127 case DW_TAG_imported_declaration:
10128 return 1;
10129
10130 case DW_TAG_variable:
10131 case DW_TAG_constant:
10132 /* We only need to prefix "globally" visible variables. These include
10133 any variable marked with DW_AT_external or any variable that
10134 lives in a namespace. [Variables in anonymous namespaces
10135 require prefixing, but they are not DW_AT_external.] */
10136
10137 if (dwarf2_attr (die, DW_AT_specification, cu))
10138 {
10139 struct dwarf2_cu *spec_cu = cu;
10140
10141 return die_needs_namespace (die_specification (die, &spec_cu),
10142 spec_cu);
10143 }
10144
10145 attr = dwarf2_attr (die, DW_AT_external, cu);
10146 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10147 && die->parent->tag != DW_TAG_module)
10148 return 0;
10149 /* A variable in a lexical block of some kind does not need a
10150 namespace, even though in C++ such variables may be external
10151 and have a mangled name. */
10152 if (die->parent->tag == DW_TAG_lexical_block
10153 || die->parent->tag == DW_TAG_try_block
10154 || die->parent->tag == DW_TAG_catch_block
10155 || die->parent->tag == DW_TAG_subprogram)
10156 return 0;
10157 return 1;
10158
10159 default:
10160 return 0;
10161 }
10162 }
10163
10164 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10165 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10166 defined for the given DIE. */
10167
10168 static struct attribute *
10169 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10170 {
10171 struct attribute *attr;
10172
10173 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10174 if (attr == NULL)
10175 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10176
10177 return attr;
10178 }
10179
10180 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10181 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10182 defined for the given DIE. */
10183
10184 static const char *
10185 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10186 {
10187 const char *linkage_name;
10188
10189 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10190 if (linkage_name == NULL)
10191 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10192
10193 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10194 See https://github.com/rust-lang/rust/issues/32925. */
10195 if (cu->language == language_rust && linkage_name != NULL
10196 && strchr (linkage_name, '{') != NULL)
10197 linkage_name = NULL;
10198
10199 return linkage_name;
10200 }
10201
10202 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10203 compute the physname for the object, which include a method's:
10204 - formal parameters (C++),
10205 - receiver type (Go),
10206
10207 The term "physname" is a bit confusing.
10208 For C++, for example, it is the demangled name.
10209 For Go, for example, it's the mangled name.
10210
10211 For Ada, return the DIE's linkage name rather than the fully qualified
10212 name. PHYSNAME is ignored..
10213
10214 The result is allocated on the objfile->per_bfd's obstack and
10215 canonicalized. */
10216
10217 static const char *
10218 dwarf2_compute_name (const char *name,
10219 struct die_info *die, struct dwarf2_cu *cu,
10220 int physname)
10221 {
10222 struct objfile *objfile = cu->per_objfile->objfile;
10223
10224 if (name == NULL)
10225 name = dwarf2_name (die, cu);
10226
10227 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10228 but otherwise compute it by typename_concat inside GDB.
10229 FIXME: Actually this is not really true, or at least not always true.
10230 It's all very confusing. compute_and_set_names doesn't try to demangle
10231 Fortran names because there is no mangling standard. So new_symbol
10232 will set the demangled name to the result of dwarf2_full_name, and it is
10233 the demangled name that GDB uses if it exists. */
10234 if (cu->language == language_ada
10235 || (cu->language == language_fortran && physname))
10236 {
10237 /* For Ada unit, we prefer the linkage name over the name, as
10238 the former contains the exported name, which the user expects
10239 to be able to reference. Ideally, we want the user to be able
10240 to reference this entity using either natural or linkage name,
10241 but we haven't started looking at this enhancement yet. */
10242 const char *linkage_name = dw2_linkage_name (die, cu);
10243
10244 if (linkage_name != NULL)
10245 return linkage_name;
10246 }
10247
10248 /* These are the only languages we know how to qualify names in. */
10249 if (name != NULL
10250 && (cu->language == language_cplus
10251 || cu->language == language_fortran || cu->language == language_d
10252 || cu->language == language_rust))
10253 {
10254 if (die_needs_namespace (die, cu))
10255 {
10256 const char *prefix;
10257 const char *canonical_name = NULL;
10258
10259 string_file buf;
10260
10261 prefix = determine_prefix (die, cu);
10262 if (*prefix != '\0')
10263 {
10264 gdb::unique_xmalloc_ptr<char> prefixed_name
10265 (typename_concat (NULL, prefix, name, physname, cu));
10266
10267 buf.puts (prefixed_name.get ());
10268 }
10269 else
10270 buf.puts (name);
10271
10272 /* Template parameters may be specified in the DIE's DW_AT_name, or
10273 as children with DW_TAG_template_type_param or
10274 DW_TAG_value_type_param. If the latter, add them to the name
10275 here. If the name already has template parameters, then
10276 skip this step; some versions of GCC emit both, and
10277 it is more efficient to use the pre-computed name.
10278
10279 Something to keep in mind about this process: it is very
10280 unlikely, or in some cases downright impossible, to produce
10281 something that will match the mangled name of a function.
10282 If the definition of the function has the same debug info,
10283 we should be able to match up with it anyway. But fallbacks
10284 using the minimal symbol, for instance to find a method
10285 implemented in a stripped copy of libstdc++, will not work.
10286 If we do not have debug info for the definition, we will have to
10287 match them up some other way.
10288
10289 When we do name matching there is a related problem with function
10290 templates; two instantiated function templates are allowed to
10291 differ only by their return types, which we do not add here. */
10292
10293 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10294 {
10295 struct attribute *attr;
10296 struct die_info *child;
10297 int first = 1;
10298
10299 die->building_fullname = 1;
10300
10301 for (child = die->child; child != NULL; child = child->sibling)
10302 {
10303 struct type *type;
10304 LONGEST value;
10305 const gdb_byte *bytes;
10306 struct dwarf2_locexpr_baton *baton;
10307 struct value *v;
10308
10309 if (child->tag != DW_TAG_template_type_param
10310 && child->tag != DW_TAG_template_value_param)
10311 continue;
10312
10313 if (first)
10314 {
10315 buf.puts ("<");
10316 first = 0;
10317 }
10318 else
10319 buf.puts (", ");
10320
10321 attr = dwarf2_attr (child, DW_AT_type, cu);
10322 if (attr == NULL)
10323 {
10324 complaint (_("template parameter missing DW_AT_type"));
10325 buf.puts ("UNKNOWN_TYPE");
10326 continue;
10327 }
10328 type = die_type (child, cu);
10329
10330 if (child->tag == DW_TAG_template_type_param)
10331 {
10332 c_print_type (type, "", &buf, -1, 0, cu->language,
10333 &type_print_raw_options);
10334 continue;
10335 }
10336
10337 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10338 if (attr == NULL)
10339 {
10340 complaint (_("template parameter missing "
10341 "DW_AT_const_value"));
10342 buf.puts ("UNKNOWN_VALUE");
10343 continue;
10344 }
10345
10346 dwarf2_const_value_attr (attr, type, name,
10347 &cu->comp_unit_obstack, cu,
10348 &value, &bytes, &baton);
10349
10350 if (TYPE_NOSIGN (type))
10351 /* GDB prints characters as NUMBER 'CHAR'. If that's
10352 changed, this can use value_print instead. */
10353 c_printchar (value, type, &buf);
10354 else
10355 {
10356 struct value_print_options opts;
10357
10358 if (baton != NULL)
10359 v = dwarf2_evaluate_loc_desc (type, NULL,
10360 baton->data,
10361 baton->size,
10362 baton->per_cu,
10363 baton->per_objfile);
10364 else if (bytes != NULL)
10365 {
10366 v = allocate_value (type);
10367 memcpy (value_contents_writeable (v), bytes,
10368 TYPE_LENGTH (type));
10369 }
10370 else
10371 v = value_from_longest (type, value);
10372
10373 /* Specify decimal so that we do not depend on
10374 the radix. */
10375 get_formatted_print_options (&opts, 'd');
10376 opts.raw = 1;
10377 value_print (v, &buf, &opts);
10378 release_value (v);
10379 }
10380 }
10381
10382 die->building_fullname = 0;
10383
10384 if (!first)
10385 {
10386 /* Close the argument list, with a space if necessary
10387 (nested templates). */
10388 if (!buf.empty () && buf.string ().back () == '>')
10389 buf.puts (" >");
10390 else
10391 buf.puts (">");
10392 }
10393 }
10394
10395 /* For C++ methods, append formal parameter type
10396 information, if PHYSNAME. */
10397
10398 if (physname && die->tag == DW_TAG_subprogram
10399 && cu->language == language_cplus)
10400 {
10401 struct type *type = read_type_die (die, cu);
10402
10403 c_type_print_args (type, &buf, 1, cu->language,
10404 &type_print_raw_options);
10405
10406 if (cu->language == language_cplus)
10407 {
10408 /* Assume that an artificial first parameter is
10409 "this", but do not crash if it is not. RealView
10410 marks unnamed (and thus unused) parameters as
10411 artificial; there is no way to differentiate
10412 the two cases. */
10413 if (type->num_fields () > 0
10414 && TYPE_FIELD_ARTIFICIAL (type, 0)
10415 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
10416 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10417 0))))
10418 buf.puts (" const");
10419 }
10420 }
10421
10422 const std::string &intermediate_name = buf.string ();
10423
10424 if (cu->language == language_cplus)
10425 canonical_name
10426 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10427 objfile);
10428
10429 /* If we only computed INTERMEDIATE_NAME, or if
10430 INTERMEDIATE_NAME is already canonical, then we need to
10431 intern it. */
10432 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10433 name = objfile->intern (intermediate_name);
10434 else
10435 name = canonical_name;
10436 }
10437 }
10438
10439 return name;
10440 }
10441
10442 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10443 If scope qualifiers are appropriate they will be added. The result
10444 will be allocated on the storage_obstack, or NULL if the DIE does
10445 not have a name. NAME may either be from a previous call to
10446 dwarf2_name or NULL.
10447
10448 The output string will be canonicalized (if C++). */
10449
10450 static const char *
10451 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10452 {
10453 return dwarf2_compute_name (name, die, cu, 0);
10454 }
10455
10456 /* Construct a physname for the given DIE in CU. NAME may either be
10457 from a previous call to dwarf2_name or NULL. The result will be
10458 allocated on the objfile_objstack or NULL if the DIE does not have a
10459 name.
10460
10461 The output string will be canonicalized (if C++). */
10462
10463 static const char *
10464 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10465 {
10466 struct objfile *objfile = cu->per_objfile->objfile;
10467 const char *retval, *mangled = NULL, *canon = NULL;
10468 int need_copy = 1;
10469
10470 /* In this case dwarf2_compute_name is just a shortcut not building anything
10471 on its own. */
10472 if (!die_needs_namespace (die, cu))
10473 return dwarf2_compute_name (name, die, cu, 1);
10474
10475 if (cu->language != language_rust)
10476 mangled = dw2_linkage_name (die, cu);
10477
10478 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10479 has computed. */
10480 gdb::unique_xmalloc_ptr<char> demangled;
10481 if (mangled != NULL)
10482 {
10483
10484 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10485 {
10486 /* Do nothing (do not demangle the symbol name). */
10487 }
10488 else if (cu->language == language_go)
10489 {
10490 /* This is a lie, but we already lie to the caller new_symbol.
10491 new_symbol assumes we return the mangled name.
10492 This just undoes that lie until things are cleaned up. */
10493 }
10494 else
10495 {
10496 /* Use DMGL_RET_DROP for C++ template functions to suppress
10497 their return type. It is easier for GDB users to search
10498 for such functions as `name(params)' than `long name(params)'.
10499 In such case the minimal symbol names do not match the full
10500 symbol names but for template functions there is never a need
10501 to look up their definition from their declaration so
10502 the only disadvantage remains the minimal symbol variant
10503 `long name(params)' does not have the proper inferior type. */
10504 demangled.reset (gdb_demangle (mangled,
10505 (DMGL_PARAMS | DMGL_ANSI
10506 | DMGL_RET_DROP)));
10507 }
10508 if (demangled)
10509 canon = demangled.get ();
10510 else
10511 {
10512 canon = mangled;
10513 need_copy = 0;
10514 }
10515 }
10516
10517 if (canon == NULL || check_physname)
10518 {
10519 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10520
10521 if (canon != NULL && strcmp (physname, canon) != 0)
10522 {
10523 /* It may not mean a bug in GDB. The compiler could also
10524 compute DW_AT_linkage_name incorrectly. But in such case
10525 GDB would need to be bug-to-bug compatible. */
10526
10527 complaint (_("Computed physname <%s> does not match demangled <%s> "
10528 "(from linkage <%s>) - DIE at %s [in module %s]"),
10529 physname, canon, mangled, sect_offset_str (die->sect_off),
10530 objfile_name (objfile));
10531
10532 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10533 is available here - over computed PHYSNAME. It is safer
10534 against both buggy GDB and buggy compilers. */
10535
10536 retval = canon;
10537 }
10538 else
10539 {
10540 retval = physname;
10541 need_copy = 0;
10542 }
10543 }
10544 else
10545 retval = canon;
10546
10547 if (need_copy)
10548 retval = objfile->intern (retval);
10549
10550 return retval;
10551 }
10552
10553 /* Inspect DIE in CU for a namespace alias. If one exists, record
10554 a new symbol for it.
10555
10556 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10557
10558 static int
10559 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10560 {
10561 struct attribute *attr;
10562
10563 /* If the die does not have a name, this is not a namespace
10564 alias. */
10565 attr = dwarf2_attr (die, DW_AT_name, cu);
10566 if (attr != NULL)
10567 {
10568 int num;
10569 struct die_info *d = die;
10570 struct dwarf2_cu *imported_cu = cu;
10571
10572 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10573 keep inspecting DIEs until we hit the underlying import. */
10574 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10575 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10576 {
10577 attr = dwarf2_attr (d, DW_AT_import, cu);
10578 if (attr == NULL)
10579 break;
10580
10581 d = follow_die_ref (d, attr, &imported_cu);
10582 if (d->tag != DW_TAG_imported_declaration)
10583 break;
10584 }
10585
10586 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10587 {
10588 complaint (_("DIE at %s has too many recursively imported "
10589 "declarations"), sect_offset_str (d->sect_off));
10590 return 0;
10591 }
10592
10593 if (attr != NULL)
10594 {
10595 struct type *type;
10596 sect_offset sect_off = attr->get_ref_die_offset ();
10597
10598 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10599 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10600 {
10601 /* This declaration is a global namespace alias. Add
10602 a symbol for it whose type is the aliased namespace. */
10603 new_symbol (die, type, cu);
10604 return 1;
10605 }
10606 }
10607 }
10608
10609 return 0;
10610 }
10611
10612 /* Return the using directives repository (global or local?) to use in the
10613 current context for CU.
10614
10615 For Ada, imported declarations can materialize renamings, which *may* be
10616 global. However it is impossible (for now?) in DWARF to distinguish
10617 "external" imported declarations and "static" ones. As all imported
10618 declarations seem to be static in all other languages, make them all CU-wide
10619 global only in Ada. */
10620
10621 static struct using_direct **
10622 using_directives (struct dwarf2_cu *cu)
10623 {
10624 if (cu->language == language_ada
10625 && cu->get_builder ()->outermost_context_p ())
10626 return cu->get_builder ()->get_global_using_directives ();
10627 else
10628 return cu->get_builder ()->get_local_using_directives ();
10629 }
10630
10631 /* Read the import statement specified by the given die and record it. */
10632
10633 static void
10634 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10635 {
10636 struct objfile *objfile = cu->per_objfile->objfile;
10637 struct attribute *import_attr;
10638 struct die_info *imported_die, *child_die;
10639 struct dwarf2_cu *imported_cu;
10640 const char *imported_name;
10641 const char *imported_name_prefix;
10642 const char *canonical_name;
10643 const char *import_alias;
10644 const char *imported_declaration = NULL;
10645 const char *import_prefix;
10646 std::vector<const char *> excludes;
10647
10648 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10649 if (import_attr == NULL)
10650 {
10651 complaint (_("Tag '%s' has no DW_AT_import"),
10652 dwarf_tag_name (die->tag));
10653 return;
10654 }
10655
10656 imported_cu = cu;
10657 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10658 imported_name = dwarf2_name (imported_die, imported_cu);
10659 if (imported_name == NULL)
10660 {
10661 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10662
10663 The import in the following code:
10664 namespace A
10665 {
10666 typedef int B;
10667 }
10668
10669 int main ()
10670 {
10671 using A::B;
10672 B b;
10673 return b;
10674 }
10675
10676 ...
10677 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10678 <52> DW_AT_decl_file : 1
10679 <53> DW_AT_decl_line : 6
10680 <54> DW_AT_import : <0x75>
10681 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10682 <59> DW_AT_name : B
10683 <5b> DW_AT_decl_file : 1
10684 <5c> DW_AT_decl_line : 2
10685 <5d> DW_AT_type : <0x6e>
10686 ...
10687 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10688 <76> DW_AT_byte_size : 4
10689 <77> DW_AT_encoding : 5 (signed)
10690
10691 imports the wrong die ( 0x75 instead of 0x58 ).
10692 This case will be ignored until the gcc bug is fixed. */
10693 return;
10694 }
10695
10696 /* Figure out the local name after import. */
10697 import_alias = dwarf2_name (die, cu);
10698
10699 /* Figure out where the statement is being imported to. */
10700 import_prefix = determine_prefix (die, cu);
10701
10702 /* Figure out what the scope of the imported die is and prepend it
10703 to the name of the imported die. */
10704 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10705
10706 if (imported_die->tag != DW_TAG_namespace
10707 && imported_die->tag != DW_TAG_module)
10708 {
10709 imported_declaration = imported_name;
10710 canonical_name = imported_name_prefix;
10711 }
10712 else if (strlen (imported_name_prefix) > 0)
10713 canonical_name = obconcat (&objfile->objfile_obstack,
10714 imported_name_prefix,
10715 (cu->language == language_d ? "." : "::"),
10716 imported_name, (char *) NULL);
10717 else
10718 canonical_name = imported_name;
10719
10720 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10721 for (child_die = die->child; child_die && child_die->tag;
10722 child_die = child_die->sibling)
10723 {
10724 /* DWARF-4: A Fortran use statement with a “rename list” may be
10725 represented by an imported module entry with an import attribute
10726 referring to the module and owned entries corresponding to those
10727 entities that are renamed as part of being imported. */
10728
10729 if (child_die->tag != DW_TAG_imported_declaration)
10730 {
10731 complaint (_("child DW_TAG_imported_declaration expected "
10732 "- DIE at %s [in module %s]"),
10733 sect_offset_str (child_die->sect_off),
10734 objfile_name (objfile));
10735 continue;
10736 }
10737
10738 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10739 if (import_attr == NULL)
10740 {
10741 complaint (_("Tag '%s' has no DW_AT_import"),
10742 dwarf_tag_name (child_die->tag));
10743 continue;
10744 }
10745
10746 imported_cu = cu;
10747 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10748 &imported_cu);
10749 imported_name = dwarf2_name (imported_die, imported_cu);
10750 if (imported_name == NULL)
10751 {
10752 complaint (_("child DW_TAG_imported_declaration has unknown "
10753 "imported name - DIE at %s [in module %s]"),
10754 sect_offset_str (child_die->sect_off),
10755 objfile_name (objfile));
10756 continue;
10757 }
10758
10759 excludes.push_back (imported_name);
10760
10761 process_die (child_die, cu);
10762 }
10763
10764 add_using_directive (using_directives (cu),
10765 import_prefix,
10766 canonical_name,
10767 import_alias,
10768 imported_declaration,
10769 excludes,
10770 0,
10771 &objfile->objfile_obstack);
10772 }
10773
10774 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10775 types, but gives them a size of zero. Starting with version 14,
10776 ICC is compatible with GCC. */
10777
10778 static bool
10779 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10780 {
10781 if (!cu->checked_producer)
10782 check_producer (cu);
10783
10784 return cu->producer_is_icc_lt_14;
10785 }
10786
10787 /* ICC generates a DW_AT_type for C void functions. This was observed on
10788 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10789 which says that void functions should not have a DW_AT_type. */
10790
10791 static bool
10792 producer_is_icc (struct dwarf2_cu *cu)
10793 {
10794 if (!cu->checked_producer)
10795 check_producer (cu);
10796
10797 return cu->producer_is_icc;
10798 }
10799
10800 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10801 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10802 this, it was first present in GCC release 4.3.0. */
10803
10804 static bool
10805 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10806 {
10807 if (!cu->checked_producer)
10808 check_producer (cu);
10809
10810 return cu->producer_is_gcc_lt_4_3;
10811 }
10812
10813 static file_and_directory
10814 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10815 {
10816 file_and_directory res;
10817
10818 /* Find the filename. Do not use dwarf2_name here, since the filename
10819 is not a source language identifier. */
10820 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10821 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10822
10823 if (res.comp_dir == NULL
10824 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10825 && IS_ABSOLUTE_PATH (res.name))
10826 {
10827 res.comp_dir_storage = ldirname (res.name);
10828 if (!res.comp_dir_storage.empty ())
10829 res.comp_dir = res.comp_dir_storage.c_str ();
10830 }
10831 if (res.comp_dir != NULL)
10832 {
10833 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10834 directory, get rid of it. */
10835 const char *cp = strchr (res.comp_dir, ':');
10836
10837 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10838 res.comp_dir = cp + 1;
10839 }
10840
10841 if (res.name == NULL)
10842 res.name = "<unknown>";
10843
10844 return res;
10845 }
10846
10847 /* Handle DW_AT_stmt_list for a compilation unit.
10848 DIE is the DW_TAG_compile_unit die for CU.
10849 COMP_DIR is the compilation directory. LOWPC is passed to
10850 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10851
10852 static void
10853 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10854 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10855 {
10856 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10857 struct attribute *attr;
10858 struct line_header line_header_local;
10859 hashval_t line_header_local_hash;
10860 void **slot;
10861 int decode_mapping;
10862
10863 gdb_assert (! cu->per_cu->is_debug_types);
10864
10865 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10866 if (attr == NULL)
10867 return;
10868
10869 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10870
10871 /* The line header hash table is only created if needed (it exists to
10872 prevent redundant reading of the line table for partial_units).
10873 If we're given a partial_unit, we'll need it. If we're given a
10874 compile_unit, then use the line header hash table if it's already
10875 created, but don't create one just yet. */
10876
10877 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
10878 && die->tag == DW_TAG_partial_unit)
10879 {
10880 dwarf2_per_objfile->per_bfd->line_header_hash
10881 .reset (htab_create_alloc (127, line_header_hash_voidp,
10882 line_header_eq_voidp,
10883 free_line_header_voidp,
10884 xcalloc, xfree));
10885 }
10886
10887 line_header_local.sect_off = line_offset;
10888 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10889 line_header_local_hash = line_header_hash (&line_header_local);
10890 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
10891 {
10892 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10893 &line_header_local,
10894 line_header_local_hash, NO_INSERT);
10895
10896 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10897 is not present in *SLOT (since if there is something in *SLOT then
10898 it will be for a partial_unit). */
10899 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10900 {
10901 gdb_assert (*slot != NULL);
10902 cu->line_header = (struct line_header *) *slot;
10903 return;
10904 }
10905 }
10906
10907 /* dwarf_decode_line_header does not yet provide sufficient information.
10908 We always have to call also dwarf_decode_lines for it. */
10909 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10910 if (lh == NULL)
10911 return;
10912
10913 cu->line_header = lh.release ();
10914 cu->line_header_die_owner = die;
10915
10916 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
10917 slot = NULL;
10918 else
10919 {
10920 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10921 &line_header_local,
10922 line_header_local_hash, INSERT);
10923 gdb_assert (slot != NULL);
10924 }
10925 if (slot != NULL && *slot == NULL)
10926 {
10927 /* This newly decoded line number information unit will be owned
10928 by line_header_hash hash table. */
10929 *slot = cu->line_header;
10930 cu->line_header_die_owner = NULL;
10931 }
10932 else
10933 {
10934 /* We cannot free any current entry in (*slot) as that struct line_header
10935 may be already used by multiple CUs. Create only temporary decoded
10936 line_header for this CU - it may happen at most once for each line
10937 number information unit. And if we're not using line_header_hash
10938 then this is what we want as well. */
10939 gdb_assert (die->tag != DW_TAG_partial_unit);
10940 }
10941 decode_mapping = (die->tag != DW_TAG_partial_unit);
10942 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10943 decode_mapping);
10944
10945 }
10946
10947 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10948
10949 static void
10950 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10951 {
10952 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10953 struct objfile *objfile = dwarf2_per_objfile->objfile;
10954 struct gdbarch *gdbarch = objfile->arch ();
10955 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10956 CORE_ADDR highpc = ((CORE_ADDR) 0);
10957 struct attribute *attr;
10958 struct die_info *child_die;
10959 CORE_ADDR baseaddr;
10960
10961 prepare_one_comp_unit (cu, die, cu->language);
10962 baseaddr = objfile->text_section_offset ();
10963
10964 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10965
10966 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10967 from finish_block. */
10968 if (lowpc == ((CORE_ADDR) -1))
10969 lowpc = highpc;
10970 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10971
10972 file_and_directory fnd = find_file_and_directory (die, cu);
10973
10974 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10975 standardised yet. As a workaround for the language detection we fall
10976 back to the DW_AT_producer string. */
10977 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10978 cu->language = language_opencl;
10979
10980 /* Similar hack for Go. */
10981 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10982 set_cu_language (DW_LANG_Go, cu);
10983
10984 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10985
10986 /* Decode line number information if present. We do this before
10987 processing child DIEs, so that the line header table is available
10988 for DW_AT_decl_file. */
10989 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10990
10991 /* Process all dies in compilation unit. */
10992 if (die->child != NULL)
10993 {
10994 child_die = die->child;
10995 while (child_die && child_die->tag)
10996 {
10997 process_die (child_die, cu);
10998 child_die = child_die->sibling;
10999 }
11000 }
11001
11002 /* Decode macro information, if present. Dwarf 2 macro information
11003 refers to information in the line number info statement program
11004 header, so we can only read it if we've read the header
11005 successfully. */
11006 attr = dwarf2_attr (die, DW_AT_macros, cu);
11007 if (attr == NULL)
11008 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11009 if (attr && cu->line_header)
11010 {
11011 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11012 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11013
11014 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11015 }
11016 else
11017 {
11018 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11019 if (attr && cu->line_header)
11020 {
11021 unsigned int macro_offset = DW_UNSND (attr);
11022
11023 dwarf_decode_macros (cu, macro_offset, 0);
11024 }
11025 }
11026 }
11027
11028 void
11029 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11030 {
11031 struct type_unit_group *tu_group;
11032 int first_time;
11033 struct attribute *attr;
11034 unsigned int i;
11035 struct signatured_type *sig_type;
11036
11037 gdb_assert (per_cu->is_debug_types);
11038 sig_type = (struct signatured_type *) per_cu;
11039
11040 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11041
11042 /* If we're using .gdb_index (includes -readnow) then
11043 per_cu->type_unit_group may not have been set up yet. */
11044 if (sig_type->type_unit_group == NULL)
11045 sig_type->type_unit_group = get_type_unit_group (this, attr);
11046 tu_group = sig_type->type_unit_group;
11047
11048 /* If we've already processed this stmt_list there's no real need to
11049 do it again, we could fake it and just recreate the part we need
11050 (file name,index -> symtab mapping). If data shows this optimization
11051 is useful we can do it then. */
11052 first_time = tu_group->compunit_symtab == NULL;
11053
11054 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11055 debug info. */
11056 line_header_up lh;
11057 if (attr != NULL)
11058 {
11059 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11060 lh = dwarf_decode_line_header (line_offset, this);
11061 }
11062 if (lh == NULL)
11063 {
11064 if (first_time)
11065 start_symtab ("", NULL, 0);
11066 else
11067 {
11068 gdb_assert (tu_group->symtabs == NULL);
11069 gdb_assert (m_builder == nullptr);
11070 struct compunit_symtab *cust = tu_group->compunit_symtab;
11071 m_builder.reset (new struct buildsym_compunit
11072 (COMPUNIT_OBJFILE (cust), "",
11073 COMPUNIT_DIRNAME (cust),
11074 compunit_language (cust),
11075 0, cust));
11076 list_in_scope = get_builder ()->get_file_symbols ();
11077 }
11078 return;
11079 }
11080
11081 line_header = lh.release ();
11082 line_header_die_owner = die;
11083
11084 if (first_time)
11085 {
11086 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11087
11088 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11089 still initializing it, and our caller (a few levels up)
11090 process_full_type_unit still needs to know if this is the first
11091 time. */
11092
11093 tu_group->symtabs
11094 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11095 struct symtab *, line_header->file_names_size ());
11096
11097 auto &file_names = line_header->file_names ();
11098 for (i = 0; i < file_names.size (); ++i)
11099 {
11100 file_entry &fe = file_names[i];
11101 dwarf2_start_subfile (this, fe.name,
11102 fe.include_dir (line_header));
11103 buildsym_compunit *b = get_builder ();
11104 if (b->get_current_subfile ()->symtab == NULL)
11105 {
11106 /* NOTE: start_subfile will recognize when it's been
11107 passed a file it has already seen. So we can't
11108 assume there's a simple mapping from
11109 cu->line_header->file_names to subfiles, plus
11110 cu->line_header->file_names may contain dups. */
11111 b->get_current_subfile ()->symtab
11112 = allocate_symtab (cust, b->get_current_subfile ()->name);
11113 }
11114
11115 fe.symtab = b->get_current_subfile ()->symtab;
11116 tu_group->symtabs[i] = fe.symtab;
11117 }
11118 }
11119 else
11120 {
11121 gdb_assert (m_builder == nullptr);
11122 struct compunit_symtab *cust = tu_group->compunit_symtab;
11123 m_builder.reset (new struct buildsym_compunit
11124 (COMPUNIT_OBJFILE (cust), "",
11125 COMPUNIT_DIRNAME (cust),
11126 compunit_language (cust),
11127 0, cust));
11128 list_in_scope = get_builder ()->get_file_symbols ();
11129
11130 auto &file_names = line_header->file_names ();
11131 for (i = 0; i < file_names.size (); ++i)
11132 {
11133 file_entry &fe = file_names[i];
11134 fe.symtab = tu_group->symtabs[i];
11135 }
11136 }
11137
11138 /* The main symtab is allocated last. Type units don't have DW_AT_name
11139 so they don't have a "real" (so to speak) symtab anyway.
11140 There is later code that will assign the main symtab to all symbols
11141 that don't have one. We need to handle the case of a symbol with a
11142 missing symtab (DW_AT_decl_file) anyway. */
11143 }
11144
11145 /* Process DW_TAG_type_unit.
11146 For TUs we want to skip the first top level sibling if it's not the
11147 actual type being defined by this TU. In this case the first top
11148 level sibling is there to provide context only. */
11149
11150 static void
11151 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11152 {
11153 struct die_info *child_die;
11154
11155 prepare_one_comp_unit (cu, die, language_minimal);
11156
11157 /* Initialize (or reinitialize) the machinery for building symtabs.
11158 We do this before processing child DIEs, so that the line header table
11159 is available for DW_AT_decl_file. */
11160 cu->setup_type_unit_groups (die);
11161
11162 if (die->child != NULL)
11163 {
11164 child_die = die->child;
11165 while (child_die && child_die->tag)
11166 {
11167 process_die (child_die, cu);
11168 child_die = child_die->sibling;
11169 }
11170 }
11171 }
11172 \f
11173 /* DWO/DWP files.
11174
11175 http://gcc.gnu.org/wiki/DebugFission
11176 http://gcc.gnu.org/wiki/DebugFissionDWP
11177
11178 To simplify handling of both DWO files ("object" files with the DWARF info)
11179 and DWP files (a file with the DWOs packaged up into one file), we treat
11180 DWP files as having a collection of virtual DWO files. */
11181
11182 static hashval_t
11183 hash_dwo_file (const void *item)
11184 {
11185 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11186 hashval_t hash;
11187
11188 hash = htab_hash_string (dwo_file->dwo_name);
11189 if (dwo_file->comp_dir != NULL)
11190 hash += htab_hash_string (dwo_file->comp_dir);
11191 return hash;
11192 }
11193
11194 static int
11195 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11196 {
11197 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11198 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11199
11200 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11201 return 0;
11202 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11203 return lhs->comp_dir == rhs->comp_dir;
11204 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11205 }
11206
11207 /* Allocate a hash table for DWO files. */
11208
11209 static htab_up
11210 allocate_dwo_file_hash_table ()
11211 {
11212 auto delete_dwo_file = [] (void *item)
11213 {
11214 struct dwo_file *dwo_file = (struct dwo_file *) item;
11215
11216 delete dwo_file;
11217 };
11218
11219 return htab_up (htab_create_alloc (41,
11220 hash_dwo_file,
11221 eq_dwo_file,
11222 delete_dwo_file,
11223 xcalloc, xfree));
11224 }
11225
11226 /* Lookup DWO file DWO_NAME. */
11227
11228 static void **
11229 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11230 const char *dwo_name,
11231 const char *comp_dir)
11232 {
11233 struct dwo_file find_entry;
11234 void **slot;
11235
11236 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11237 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11238
11239 find_entry.dwo_name = dwo_name;
11240 find_entry.comp_dir = comp_dir;
11241 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
11242 INSERT);
11243
11244 return slot;
11245 }
11246
11247 static hashval_t
11248 hash_dwo_unit (const void *item)
11249 {
11250 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11251
11252 /* This drops the top 32 bits of the id, but is ok for a hash. */
11253 return dwo_unit->signature;
11254 }
11255
11256 static int
11257 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11258 {
11259 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11260 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11261
11262 /* The signature is assumed to be unique within the DWO file.
11263 So while object file CU dwo_id's always have the value zero,
11264 that's OK, assuming each object file DWO file has only one CU,
11265 and that's the rule for now. */
11266 return lhs->signature == rhs->signature;
11267 }
11268
11269 /* Allocate a hash table for DWO CUs,TUs.
11270 There is one of these tables for each of CUs,TUs for each DWO file. */
11271
11272 static htab_up
11273 allocate_dwo_unit_table ()
11274 {
11275 /* Start out with a pretty small number.
11276 Generally DWO files contain only one CU and maybe some TUs. */
11277 return htab_up (htab_create_alloc (3,
11278 hash_dwo_unit,
11279 eq_dwo_unit,
11280 NULL, xcalloc, xfree));
11281 }
11282
11283 /* die_reader_func for create_dwo_cu. */
11284
11285 static void
11286 create_dwo_cu_reader (const struct die_reader_specs *reader,
11287 const gdb_byte *info_ptr,
11288 struct die_info *comp_unit_die,
11289 struct dwo_file *dwo_file,
11290 struct dwo_unit *dwo_unit)
11291 {
11292 struct dwarf2_cu *cu = reader->cu;
11293 sect_offset sect_off = cu->per_cu->sect_off;
11294 struct dwarf2_section_info *section = cu->per_cu->section;
11295
11296 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11297 if (!signature.has_value ())
11298 {
11299 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11300 " its dwo_id [in module %s]"),
11301 sect_offset_str (sect_off), dwo_file->dwo_name);
11302 return;
11303 }
11304
11305 dwo_unit->dwo_file = dwo_file;
11306 dwo_unit->signature = *signature;
11307 dwo_unit->section = section;
11308 dwo_unit->sect_off = sect_off;
11309 dwo_unit->length = cu->per_cu->length;
11310
11311 if (dwarf_read_debug)
11312 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11313 sect_offset_str (sect_off),
11314 hex_string (dwo_unit->signature));
11315 }
11316
11317 /* Create the dwo_units for the CUs in a DWO_FILE.
11318 Note: This function processes DWO files only, not DWP files. */
11319
11320 static void
11321 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11322 dwarf2_cu *cu, struct dwo_file &dwo_file,
11323 dwarf2_section_info &section, htab_up &cus_htab)
11324 {
11325 struct objfile *objfile = dwarf2_per_objfile->objfile;
11326 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
11327 const gdb_byte *info_ptr, *end_ptr;
11328
11329 section.read (objfile);
11330 info_ptr = section.buffer;
11331
11332 if (info_ptr == NULL)
11333 return;
11334
11335 if (dwarf_read_debug)
11336 {
11337 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11338 section.get_name (),
11339 section.get_file_name ());
11340 }
11341
11342 end_ptr = info_ptr + section.size;
11343 while (info_ptr < end_ptr)
11344 {
11345 struct dwarf2_per_cu_data per_cu;
11346 struct dwo_unit read_unit {};
11347 struct dwo_unit *dwo_unit;
11348 void **slot;
11349 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11350
11351 memset (&per_cu, 0, sizeof (per_cu));
11352 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11353 per_cu.per_bfd = per_bfd;
11354 per_cu.is_debug_types = 0;
11355 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11356 per_cu.section = &section;
11357
11358 cutu_reader reader (&per_cu, dwarf2_per_objfile, cu, &dwo_file);
11359 if (!reader.dummy_p)
11360 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11361 &dwo_file, &read_unit);
11362 info_ptr += per_cu.length;
11363
11364 // If the unit could not be parsed, skip it.
11365 if (read_unit.dwo_file == NULL)
11366 continue;
11367
11368 if (cus_htab == NULL)
11369 cus_htab = allocate_dwo_unit_table ();
11370
11371 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11372 struct dwo_unit);
11373 *dwo_unit = read_unit;
11374 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11375 gdb_assert (slot != NULL);
11376 if (*slot != NULL)
11377 {
11378 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11379 sect_offset dup_sect_off = dup_cu->sect_off;
11380
11381 complaint (_("debug cu entry at offset %s is duplicate to"
11382 " the entry at offset %s, signature %s"),
11383 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11384 hex_string (dwo_unit->signature));
11385 }
11386 *slot = (void *)dwo_unit;
11387 }
11388 }
11389
11390 /* DWP file .debug_{cu,tu}_index section format:
11391 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11392
11393 DWP Version 1:
11394
11395 Both index sections have the same format, and serve to map a 64-bit
11396 signature to a set of section numbers. Each section begins with a header,
11397 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11398 indexes, and a pool of 32-bit section numbers. The index sections will be
11399 aligned at 8-byte boundaries in the file.
11400
11401 The index section header consists of:
11402
11403 V, 32 bit version number
11404 -, 32 bits unused
11405 N, 32 bit number of compilation units or type units in the index
11406 M, 32 bit number of slots in the hash table
11407
11408 Numbers are recorded using the byte order of the application binary.
11409
11410 The hash table begins at offset 16 in the section, and consists of an array
11411 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11412 order of the application binary). Unused slots in the hash table are 0.
11413 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11414
11415 The parallel table begins immediately after the hash table
11416 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11417 array of 32-bit indexes (using the byte order of the application binary),
11418 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11419 table contains a 32-bit index into the pool of section numbers. For unused
11420 hash table slots, the corresponding entry in the parallel table will be 0.
11421
11422 The pool of section numbers begins immediately following the hash table
11423 (at offset 16 + 12 * M from the beginning of the section). The pool of
11424 section numbers consists of an array of 32-bit words (using the byte order
11425 of the application binary). Each item in the array is indexed starting
11426 from 0. The hash table entry provides the index of the first section
11427 number in the set. Additional section numbers in the set follow, and the
11428 set is terminated by a 0 entry (section number 0 is not used in ELF).
11429
11430 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11431 section must be the first entry in the set, and the .debug_abbrev.dwo must
11432 be the second entry. Other members of the set may follow in any order.
11433
11434 ---
11435
11436 DWP Version 2:
11437
11438 DWP Version 2 combines all the .debug_info, etc. sections into one,
11439 and the entries in the index tables are now offsets into these sections.
11440 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11441 section.
11442
11443 Index Section Contents:
11444 Header
11445 Hash Table of Signatures dwp_hash_table.hash_table
11446 Parallel Table of Indices dwp_hash_table.unit_table
11447 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11448 Table of Section Sizes dwp_hash_table.v2.sizes
11449
11450 The index section header consists of:
11451
11452 V, 32 bit version number
11453 L, 32 bit number of columns in the table of section offsets
11454 N, 32 bit number of compilation units or type units in the index
11455 M, 32 bit number of slots in the hash table
11456
11457 Numbers are recorded using the byte order of the application binary.
11458
11459 The hash table has the same format as version 1.
11460 The parallel table of indices has the same format as version 1,
11461 except that the entries are origin-1 indices into the table of sections
11462 offsets and the table of section sizes.
11463
11464 The table of offsets begins immediately following the parallel table
11465 (at offset 16 + 12 * M from the beginning of the section). The table is
11466 a two-dimensional array of 32-bit words (using the byte order of the
11467 application binary), with L columns and N+1 rows, in row-major order.
11468 Each row in the array is indexed starting from 0. The first row provides
11469 a key to the remaining rows: each column in this row provides an identifier
11470 for a debug section, and the offsets in the same column of subsequent rows
11471 refer to that section. The section identifiers are:
11472
11473 DW_SECT_INFO 1 .debug_info.dwo
11474 DW_SECT_TYPES 2 .debug_types.dwo
11475 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11476 DW_SECT_LINE 4 .debug_line.dwo
11477 DW_SECT_LOC 5 .debug_loc.dwo
11478 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11479 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11480 DW_SECT_MACRO 8 .debug_macro.dwo
11481
11482 The offsets provided by the CU and TU index sections are the base offsets
11483 for the contributions made by each CU or TU to the corresponding section
11484 in the package file. Each CU and TU header contains an abbrev_offset
11485 field, used to find the abbreviations table for that CU or TU within the
11486 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11487 be interpreted as relative to the base offset given in the index section.
11488 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11489 should be interpreted as relative to the base offset for .debug_line.dwo,
11490 and offsets into other debug sections obtained from DWARF attributes should
11491 also be interpreted as relative to the corresponding base offset.
11492
11493 The table of sizes begins immediately following the table of offsets.
11494 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11495 with L columns and N rows, in row-major order. Each row in the array is
11496 indexed starting from 1 (row 0 is shared by the two tables).
11497
11498 ---
11499
11500 Hash table lookup is handled the same in version 1 and 2:
11501
11502 We assume that N and M will not exceed 2^32 - 1.
11503 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11504
11505 Given a 64-bit compilation unit signature or a type signature S, an entry
11506 in the hash table is located as follows:
11507
11508 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11509 the low-order k bits all set to 1.
11510
11511 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11512
11513 3) If the hash table entry at index H matches the signature, use that
11514 entry. If the hash table entry at index H is unused (all zeroes),
11515 terminate the search: the signature is not present in the table.
11516
11517 4) Let H = (H + H') modulo M. Repeat at Step 3.
11518
11519 Because M > N and H' and M are relatively prime, the search is guaranteed
11520 to stop at an unused slot or find the match. */
11521
11522 /* Create a hash table to map DWO IDs to their CU/TU entry in
11523 .debug_{info,types}.dwo in DWP_FILE.
11524 Returns NULL if there isn't one.
11525 Note: This function processes DWP files only, not DWO files. */
11526
11527 static struct dwp_hash_table *
11528 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11529 struct dwp_file *dwp_file, int is_debug_types)
11530 {
11531 struct objfile *objfile = dwarf2_per_objfile->objfile;
11532 bfd *dbfd = dwp_file->dbfd.get ();
11533 const gdb_byte *index_ptr, *index_end;
11534 struct dwarf2_section_info *index;
11535 uint32_t version, nr_columns, nr_units, nr_slots;
11536 struct dwp_hash_table *htab;
11537
11538 if (is_debug_types)
11539 index = &dwp_file->sections.tu_index;
11540 else
11541 index = &dwp_file->sections.cu_index;
11542
11543 if (index->empty ())
11544 return NULL;
11545 index->read (objfile);
11546
11547 index_ptr = index->buffer;
11548 index_end = index_ptr + index->size;
11549
11550 version = read_4_bytes (dbfd, index_ptr);
11551 index_ptr += 4;
11552 if (version == 2)
11553 nr_columns = read_4_bytes (dbfd, index_ptr);
11554 else
11555 nr_columns = 0;
11556 index_ptr += 4;
11557 nr_units = read_4_bytes (dbfd, index_ptr);
11558 index_ptr += 4;
11559 nr_slots = read_4_bytes (dbfd, index_ptr);
11560 index_ptr += 4;
11561
11562 if (version != 1 && version != 2)
11563 {
11564 error (_("Dwarf Error: unsupported DWP file version (%s)"
11565 " [in module %s]"),
11566 pulongest (version), dwp_file->name);
11567 }
11568 if (nr_slots != (nr_slots & -nr_slots))
11569 {
11570 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11571 " is not power of 2 [in module %s]"),
11572 pulongest (nr_slots), dwp_file->name);
11573 }
11574
11575 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
11576 htab->version = version;
11577 htab->nr_columns = nr_columns;
11578 htab->nr_units = nr_units;
11579 htab->nr_slots = nr_slots;
11580 htab->hash_table = index_ptr;
11581 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11582
11583 /* Exit early if the table is empty. */
11584 if (nr_slots == 0 || nr_units == 0
11585 || (version == 2 && nr_columns == 0))
11586 {
11587 /* All must be zero. */
11588 if (nr_slots != 0 || nr_units != 0
11589 || (version == 2 && nr_columns != 0))
11590 {
11591 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11592 " all zero [in modules %s]"),
11593 dwp_file->name);
11594 }
11595 return htab;
11596 }
11597
11598 if (version == 1)
11599 {
11600 htab->section_pool.v1.indices =
11601 htab->unit_table + sizeof (uint32_t) * nr_slots;
11602 /* It's harder to decide whether the section is too small in v1.
11603 V1 is deprecated anyway so we punt. */
11604 }
11605 else
11606 {
11607 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11608 int *ids = htab->section_pool.v2.section_ids;
11609 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11610 /* Reverse map for error checking. */
11611 int ids_seen[DW_SECT_MAX + 1];
11612 int i;
11613
11614 if (nr_columns < 2)
11615 {
11616 error (_("Dwarf Error: bad DWP hash table, too few columns"
11617 " in section table [in module %s]"),
11618 dwp_file->name);
11619 }
11620 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11621 {
11622 error (_("Dwarf Error: bad DWP hash table, too many columns"
11623 " in section table [in module %s]"),
11624 dwp_file->name);
11625 }
11626 memset (ids, 255, sizeof_ids);
11627 memset (ids_seen, 255, sizeof (ids_seen));
11628 for (i = 0; i < nr_columns; ++i)
11629 {
11630 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11631
11632 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11633 {
11634 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11635 " in section table [in module %s]"),
11636 id, dwp_file->name);
11637 }
11638 if (ids_seen[id] != -1)
11639 {
11640 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11641 " id %d in section table [in module %s]"),
11642 id, dwp_file->name);
11643 }
11644 ids_seen[id] = i;
11645 ids[i] = id;
11646 }
11647 /* Must have exactly one info or types section. */
11648 if (((ids_seen[DW_SECT_INFO] != -1)
11649 + (ids_seen[DW_SECT_TYPES] != -1))
11650 != 1)
11651 {
11652 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11653 " DWO info/types section [in module %s]"),
11654 dwp_file->name);
11655 }
11656 /* Must have an abbrev section. */
11657 if (ids_seen[DW_SECT_ABBREV] == -1)
11658 {
11659 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11660 " section [in module %s]"),
11661 dwp_file->name);
11662 }
11663 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11664 htab->section_pool.v2.sizes =
11665 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11666 * nr_units * nr_columns);
11667 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11668 * nr_units * nr_columns))
11669 > index_end)
11670 {
11671 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11672 " [in module %s]"),
11673 dwp_file->name);
11674 }
11675 }
11676
11677 return htab;
11678 }
11679
11680 /* Update SECTIONS with the data from SECTP.
11681
11682 This function is like the other "locate" section routines that are
11683 passed to bfd_map_over_sections, but in this context the sections to
11684 read comes from the DWP V1 hash table, not the full ELF section table.
11685
11686 The result is non-zero for success, or zero if an error was found. */
11687
11688 static int
11689 locate_v1_virtual_dwo_sections (asection *sectp,
11690 struct virtual_v1_dwo_sections *sections)
11691 {
11692 const struct dwop_section_names *names = &dwop_section_names;
11693
11694 if (section_is_p (sectp->name, &names->abbrev_dwo))
11695 {
11696 /* There can be only one. */
11697 if (sections->abbrev.s.section != NULL)
11698 return 0;
11699 sections->abbrev.s.section = sectp;
11700 sections->abbrev.size = bfd_section_size (sectp);
11701 }
11702 else if (section_is_p (sectp->name, &names->info_dwo)
11703 || section_is_p (sectp->name, &names->types_dwo))
11704 {
11705 /* There can be only one. */
11706 if (sections->info_or_types.s.section != NULL)
11707 return 0;
11708 sections->info_or_types.s.section = sectp;
11709 sections->info_or_types.size = bfd_section_size (sectp);
11710 }
11711 else if (section_is_p (sectp->name, &names->line_dwo))
11712 {
11713 /* There can be only one. */
11714 if (sections->line.s.section != NULL)
11715 return 0;
11716 sections->line.s.section = sectp;
11717 sections->line.size = bfd_section_size (sectp);
11718 }
11719 else if (section_is_p (sectp->name, &names->loc_dwo))
11720 {
11721 /* There can be only one. */
11722 if (sections->loc.s.section != NULL)
11723 return 0;
11724 sections->loc.s.section = sectp;
11725 sections->loc.size = bfd_section_size (sectp);
11726 }
11727 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11728 {
11729 /* There can be only one. */
11730 if (sections->macinfo.s.section != NULL)
11731 return 0;
11732 sections->macinfo.s.section = sectp;
11733 sections->macinfo.size = bfd_section_size (sectp);
11734 }
11735 else if (section_is_p (sectp->name, &names->macro_dwo))
11736 {
11737 /* There can be only one. */
11738 if (sections->macro.s.section != NULL)
11739 return 0;
11740 sections->macro.s.section = sectp;
11741 sections->macro.size = bfd_section_size (sectp);
11742 }
11743 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11744 {
11745 /* There can be only one. */
11746 if (sections->str_offsets.s.section != NULL)
11747 return 0;
11748 sections->str_offsets.s.section = sectp;
11749 sections->str_offsets.size = bfd_section_size (sectp);
11750 }
11751 else
11752 {
11753 /* No other kind of section is valid. */
11754 return 0;
11755 }
11756
11757 return 1;
11758 }
11759
11760 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11761 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11762 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11763 This is for DWP version 1 files. */
11764
11765 static struct dwo_unit *
11766 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11767 struct dwp_file *dwp_file,
11768 uint32_t unit_index,
11769 const char *comp_dir,
11770 ULONGEST signature, int is_debug_types)
11771 {
11772 const struct dwp_hash_table *dwp_htab =
11773 is_debug_types ? dwp_file->tus : dwp_file->cus;
11774 bfd *dbfd = dwp_file->dbfd.get ();
11775 const char *kind = is_debug_types ? "TU" : "CU";
11776 struct dwo_file *dwo_file;
11777 struct dwo_unit *dwo_unit;
11778 struct virtual_v1_dwo_sections sections;
11779 void **dwo_file_slot;
11780 int i;
11781
11782 gdb_assert (dwp_file->version == 1);
11783
11784 if (dwarf_read_debug)
11785 {
11786 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11787 kind,
11788 pulongest (unit_index), hex_string (signature),
11789 dwp_file->name);
11790 }
11791
11792 /* Fetch the sections of this DWO unit.
11793 Put a limit on the number of sections we look for so that bad data
11794 doesn't cause us to loop forever. */
11795
11796 #define MAX_NR_V1_DWO_SECTIONS \
11797 (1 /* .debug_info or .debug_types */ \
11798 + 1 /* .debug_abbrev */ \
11799 + 1 /* .debug_line */ \
11800 + 1 /* .debug_loc */ \
11801 + 1 /* .debug_str_offsets */ \
11802 + 1 /* .debug_macro or .debug_macinfo */ \
11803 + 1 /* trailing zero */)
11804
11805 memset (&sections, 0, sizeof (sections));
11806
11807 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11808 {
11809 asection *sectp;
11810 uint32_t section_nr =
11811 read_4_bytes (dbfd,
11812 dwp_htab->section_pool.v1.indices
11813 + (unit_index + i) * sizeof (uint32_t));
11814
11815 if (section_nr == 0)
11816 break;
11817 if (section_nr >= dwp_file->num_sections)
11818 {
11819 error (_("Dwarf Error: bad DWP hash table, section number too large"
11820 " [in module %s]"),
11821 dwp_file->name);
11822 }
11823
11824 sectp = dwp_file->elf_sections[section_nr];
11825 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11826 {
11827 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11828 " [in module %s]"),
11829 dwp_file->name);
11830 }
11831 }
11832
11833 if (i < 2
11834 || sections.info_or_types.empty ()
11835 || sections.abbrev.empty ())
11836 {
11837 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11838 " [in module %s]"),
11839 dwp_file->name);
11840 }
11841 if (i == MAX_NR_V1_DWO_SECTIONS)
11842 {
11843 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11844 " [in module %s]"),
11845 dwp_file->name);
11846 }
11847
11848 /* It's easier for the rest of the code if we fake a struct dwo_file and
11849 have dwo_unit "live" in that. At least for now.
11850
11851 The DWP file can be made up of a random collection of CUs and TUs.
11852 However, for each CU + set of TUs that came from the same original DWO
11853 file, we can combine them back into a virtual DWO file to save space
11854 (fewer struct dwo_file objects to allocate). Remember that for really
11855 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11856
11857 std::string virtual_dwo_name =
11858 string_printf ("virtual-dwo/%d-%d-%d-%d",
11859 sections.abbrev.get_id (),
11860 sections.line.get_id (),
11861 sections.loc.get_id (),
11862 sections.str_offsets.get_id ());
11863 /* Can we use an existing virtual DWO file? */
11864 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11865 virtual_dwo_name.c_str (),
11866 comp_dir);
11867 /* Create one if necessary. */
11868 if (*dwo_file_slot == NULL)
11869 {
11870 if (dwarf_read_debug)
11871 {
11872 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11873 virtual_dwo_name.c_str ());
11874 }
11875 dwo_file = new struct dwo_file;
11876 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
11877 dwo_file->comp_dir = comp_dir;
11878 dwo_file->sections.abbrev = sections.abbrev;
11879 dwo_file->sections.line = sections.line;
11880 dwo_file->sections.loc = sections.loc;
11881 dwo_file->sections.macinfo = sections.macinfo;
11882 dwo_file->sections.macro = sections.macro;
11883 dwo_file->sections.str_offsets = sections.str_offsets;
11884 /* The "str" section is global to the entire DWP file. */
11885 dwo_file->sections.str = dwp_file->sections.str;
11886 /* The info or types section is assigned below to dwo_unit,
11887 there's no need to record it in dwo_file.
11888 Also, we can't simply record type sections in dwo_file because
11889 we record a pointer into the vector in dwo_unit. As we collect more
11890 types we'll grow the vector and eventually have to reallocate space
11891 for it, invalidating all copies of pointers into the previous
11892 contents. */
11893 *dwo_file_slot = dwo_file;
11894 }
11895 else
11896 {
11897 if (dwarf_read_debug)
11898 {
11899 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11900 virtual_dwo_name.c_str ());
11901 }
11902 dwo_file = (struct dwo_file *) *dwo_file_slot;
11903 }
11904
11905 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
11906 dwo_unit->dwo_file = dwo_file;
11907 dwo_unit->signature = signature;
11908 dwo_unit->section =
11909 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11910 *dwo_unit->section = sections.info_or_types;
11911 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11912
11913 return dwo_unit;
11914 }
11915
11916 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11917 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11918 piece within that section used by a TU/CU, return a virtual section
11919 of just that piece. */
11920
11921 static struct dwarf2_section_info
11922 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11923 struct dwarf2_section_info *section,
11924 bfd_size_type offset, bfd_size_type size)
11925 {
11926 struct dwarf2_section_info result;
11927 asection *sectp;
11928
11929 gdb_assert (section != NULL);
11930 gdb_assert (!section->is_virtual);
11931
11932 memset (&result, 0, sizeof (result));
11933 result.s.containing_section = section;
11934 result.is_virtual = true;
11935
11936 if (size == 0)
11937 return result;
11938
11939 sectp = section->get_bfd_section ();
11940
11941 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11942 bounds of the real section. This is a pretty-rare event, so just
11943 flag an error (easier) instead of a warning and trying to cope. */
11944 if (sectp == NULL
11945 || offset + size > bfd_section_size (sectp))
11946 {
11947 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11948 " in section %s [in module %s]"),
11949 sectp ? bfd_section_name (sectp) : "<unknown>",
11950 objfile_name (dwarf2_per_objfile->objfile));
11951 }
11952
11953 result.virtual_offset = offset;
11954 result.size = size;
11955 return result;
11956 }
11957
11958 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11959 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11960 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11961 This is for DWP version 2 files. */
11962
11963 static struct dwo_unit *
11964 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11965 struct dwp_file *dwp_file,
11966 uint32_t unit_index,
11967 const char *comp_dir,
11968 ULONGEST signature, int is_debug_types)
11969 {
11970 const struct dwp_hash_table *dwp_htab =
11971 is_debug_types ? dwp_file->tus : dwp_file->cus;
11972 bfd *dbfd = dwp_file->dbfd.get ();
11973 const char *kind = is_debug_types ? "TU" : "CU";
11974 struct dwo_file *dwo_file;
11975 struct dwo_unit *dwo_unit;
11976 struct virtual_v2_dwo_sections sections;
11977 void **dwo_file_slot;
11978 int i;
11979
11980 gdb_assert (dwp_file->version == 2);
11981
11982 if (dwarf_read_debug)
11983 {
11984 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11985 kind,
11986 pulongest (unit_index), hex_string (signature),
11987 dwp_file->name);
11988 }
11989
11990 /* Fetch the section offsets of this DWO unit. */
11991
11992 memset (&sections, 0, sizeof (sections));
11993
11994 for (i = 0; i < dwp_htab->nr_columns; ++i)
11995 {
11996 uint32_t offset = read_4_bytes (dbfd,
11997 dwp_htab->section_pool.v2.offsets
11998 + (((unit_index - 1) * dwp_htab->nr_columns
11999 + i)
12000 * sizeof (uint32_t)));
12001 uint32_t size = read_4_bytes (dbfd,
12002 dwp_htab->section_pool.v2.sizes
12003 + (((unit_index - 1) * dwp_htab->nr_columns
12004 + i)
12005 * sizeof (uint32_t)));
12006
12007 switch (dwp_htab->section_pool.v2.section_ids[i])
12008 {
12009 case DW_SECT_INFO:
12010 case DW_SECT_TYPES:
12011 sections.info_or_types_offset = offset;
12012 sections.info_or_types_size = size;
12013 break;
12014 case DW_SECT_ABBREV:
12015 sections.abbrev_offset = offset;
12016 sections.abbrev_size = size;
12017 break;
12018 case DW_SECT_LINE:
12019 sections.line_offset = offset;
12020 sections.line_size = size;
12021 break;
12022 case DW_SECT_LOC:
12023 sections.loc_offset = offset;
12024 sections.loc_size = size;
12025 break;
12026 case DW_SECT_STR_OFFSETS:
12027 sections.str_offsets_offset = offset;
12028 sections.str_offsets_size = size;
12029 break;
12030 case DW_SECT_MACINFO:
12031 sections.macinfo_offset = offset;
12032 sections.macinfo_size = size;
12033 break;
12034 case DW_SECT_MACRO:
12035 sections.macro_offset = offset;
12036 sections.macro_size = size;
12037 break;
12038 }
12039 }
12040
12041 /* It's easier for the rest of the code if we fake a struct dwo_file and
12042 have dwo_unit "live" in that. At least for now.
12043
12044 The DWP file can be made up of a random collection of CUs and TUs.
12045 However, for each CU + set of TUs that came from the same original DWO
12046 file, we can combine them back into a virtual DWO file to save space
12047 (fewer struct dwo_file objects to allocate). Remember that for really
12048 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12049
12050 std::string virtual_dwo_name =
12051 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12052 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12053 (long) (sections.line_size ? sections.line_offset : 0),
12054 (long) (sections.loc_size ? sections.loc_offset : 0),
12055 (long) (sections.str_offsets_size
12056 ? sections.str_offsets_offset : 0));
12057 /* Can we use an existing virtual DWO file? */
12058 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12059 virtual_dwo_name.c_str (),
12060 comp_dir);
12061 /* Create one if necessary. */
12062 if (*dwo_file_slot == NULL)
12063 {
12064 if (dwarf_read_debug)
12065 {
12066 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12067 virtual_dwo_name.c_str ());
12068 }
12069 dwo_file = new struct dwo_file;
12070 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
12071 dwo_file->comp_dir = comp_dir;
12072 dwo_file->sections.abbrev =
12073 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12074 sections.abbrev_offset, sections.abbrev_size);
12075 dwo_file->sections.line =
12076 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12077 sections.line_offset, sections.line_size);
12078 dwo_file->sections.loc =
12079 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12080 sections.loc_offset, sections.loc_size);
12081 dwo_file->sections.macinfo =
12082 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12083 sections.macinfo_offset, sections.macinfo_size);
12084 dwo_file->sections.macro =
12085 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12086 sections.macro_offset, sections.macro_size);
12087 dwo_file->sections.str_offsets =
12088 create_dwp_v2_section (dwarf2_per_objfile,
12089 &dwp_file->sections.str_offsets,
12090 sections.str_offsets_offset,
12091 sections.str_offsets_size);
12092 /* The "str" section is global to the entire DWP file. */
12093 dwo_file->sections.str = dwp_file->sections.str;
12094 /* The info or types section is assigned below to dwo_unit,
12095 there's no need to record it in dwo_file.
12096 Also, we can't simply record type sections in dwo_file because
12097 we record a pointer into the vector in dwo_unit. As we collect more
12098 types we'll grow the vector and eventually have to reallocate space
12099 for it, invalidating all copies of pointers into the previous
12100 contents. */
12101 *dwo_file_slot = dwo_file;
12102 }
12103 else
12104 {
12105 if (dwarf_read_debug)
12106 {
12107 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12108 virtual_dwo_name.c_str ());
12109 }
12110 dwo_file = (struct dwo_file *) *dwo_file_slot;
12111 }
12112
12113 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
12114 dwo_unit->dwo_file = dwo_file;
12115 dwo_unit->signature = signature;
12116 dwo_unit->section =
12117 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12118 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12119 is_debug_types
12120 ? &dwp_file->sections.types
12121 : &dwp_file->sections.info,
12122 sections.info_or_types_offset,
12123 sections.info_or_types_size);
12124 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12125
12126 return dwo_unit;
12127 }
12128
12129 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12130 Returns NULL if the signature isn't found. */
12131
12132 static struct dwo_unit *
12133 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12134 struct dwp_file *dwp_file, const char *comp_dir,
12135 ULONGEST signature, int is_debug_types)
12136 {
12137 const struct dwp_hash_table *dwp_htab =
12138 is_debug_types ? dwp_file->tus : dwp_file->cus;
12139 bfd *dbfd = dwp_file->dbfd.get ();
12140 uint32_t mask = dwp_htab->nr_slots - 1;
12141 uint32_t hash = signature & mask;
12142 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12143 unsigned int i;
12144 void **slot;
12145 struct dwo_unit find_dwo_cu;
12146
12147 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12148 find_dwo_cu.signature = signature;
12149 slot = htab_find_slot (is_debug_types
12150 ? dwp_file->loaded_tus.get ()
12151 : dwp_file->loaded_cus.get (),
12152 &find_dwo_cu, INSERT);
12153
12154 if (*slot != NULL)
12155 return (struct dwo_unit *) *slot;
12156
12157 /* Use a for loop so that we don't loop forever on bad debug info. */
12158 for (i = 0; i < dwp_htab->nr_slots; ++i)
12159 {
12160 ULONGEST signature_in_table;
12161
12162 signature_in_table =
12163 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12164 if (signature_in_table == signature)
12165 {
12166 uint32_t unit_index =
12167 read_4_bytes (dbfd,
12168 dwp_htab->unit_table + hash * sizeof (uint32_t));
12169
12170 if (dwp_file->version == 1)
12171 {
12172 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12173 dwp_file, unit_index,
12174 comp_dir, signature,
12175 is_debug_types);
12176 }
12177 else
12178 {
12179 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12180 dwp_file, unit_index,
12181 comp_dir, signature,
12182 is_debug_types);
12183 }
12184 return (struct dwo_unit *) *slot;
12185 }
12186 if (signature_in_table == 0)
12187 return NULL;
12188 hash = (hash + hash2) & mask;
12189 }
12190
12191 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12192 " [in module %s]"),
12193 dwp_file->name);
12194 }
12195
12196 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12197 Open the file specified by FILE_NAME and hand it off to BFD for
12198 preliminary analysis. Return a newly initialized bfd *, which
12199 includes a canonicalized copy of FILE_NAME.
12200 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12201 SEARCH_CWD is true if the current directory is to be searched.
12202 It will be searched before debug-file-directory.
12203 If successful, the file is added to the bfd include table of the
12204 objfile's bfd (see gdb_bfd_record_inclusion).
12205 If unable to find/open the file, return NULL.
12206 NOTE: This function is derived from symfile_bfd_open. */
12207
12208 static gdb_bfd_ref_ptr
12209 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12210 const char *file_name, int is_dwp, int search_cwd)
12211 {
12212 int desc;
12213 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12214 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12215 to debug_file_directory. */
12216 const char *search_path;
12217 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12218
12219 gdb::unique_xmalloc_ptr<char> search_path_holder;
12220 if (search_cwd)
12221 {
12222 if (*debug_file_directory != '\0')
12223 {
12224 search_path_holder.reset (concat (".", dirname_separator_string,
12225 debug_file_directory,
12226 (char *) NULL));
12227 search_path = search_path_holder.get ();
12228 }
12229 else
12230 search_path = ".";
12231 }
12232 else
12233 search_path = debug_file_directory;
12234
12235 openp_flags flags = OPF_RETURN_REALPATH;
12236 if (is_dwp)
12237 flags |= OPF_SEARCH_IN_PATH;
12238
12239 gdb::unique_xmalloc_ptr<char> absolute_name;
12240 desc = openp (search_path, flags, file_name,
12241 O_RDONLY | O_BINARY, &absolute_name);
12242 if (desc < 0)
12243 return NULL;
12244
12245 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12246 gnutarget, desc));
12247 if (sym_bfd == NULL)
12248 return NULL;
12249 bfd_set_cacheable (sym_bfd.get (), 1);
12250
12251 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12252 return NULL;
12253
12254 /* Success. Record the bfd as having been included by the objfile's bfd.
12255 This is important because things like demangled_names_hash lives in the
12256 objfile's per_bfd space and may have references to things like symbol
12257 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12258 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12259
12260 return sym_bfd;
12261 }
12262
12263 /* Try to open DWO file FILE_NAME.
12264 COMP_DIR is the DW_AT_comp_dir attribute.
12265 The result is the bfd handle of the file.
12266 If there is a problem finding or opening the file, return NULL.
12267 Upon success, the canonicalized path of the file is stored in the bfd,
12268 same as symfile_bfd_open. */
12269
12270 static gdb_bfd_ref_ptr
12271 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12272 const char *file_name, const char *comp_dir)
12273 {
12274 if (IS_ABSOLUTE_PATH (file_name))
12275 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12276 0 /*is_dwp*/, 0 /*search_cwd*/);
12277
12278 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12279
12280 if (comp_dir != NULL)
12281 {
12282 gdb::unique_xmalloc_ptr<char> path_to_try
12283 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12284
12285 /* NOTE: If comp_dir is a relative path, this will also try the
12286 search path, which seems useful. */
12287 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12288 path_to_try.get (),
12289 0 /*is_dwp*/,
12290 1 /*search_cwd*/));
12291 if (abfd != NULL)
12292 return abfd;
12293 }
12294
12295 /* That didn't work, try debug-file-directory, which, despite its name,
12296 is a list of paths. */
12297
12298 if (*debug_file_directory == '\0')
12299 return NULL;
12300
12301 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12302 0 /*is_dwp*/, 1 /*search_cwd*/);
12303 }
12304
12305 /* This function is mapped across the sections and remembers the offset and
12306 size of each of the DWO debugging sections we are interested in. */
12307
12308 static void
12309 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12310 {
12311 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12312 const struct dwop_section_names *names = &dwop_section_names;
12313
12314 if (section_is_p (sectp->name, &names->abbrev_dwo))
12315 {
12316 dwo_sections->abbrev.s.section = sectp;
12317 dwo_sections->abbrev.size = bfd_section_size (sectp);
12318 }
12319 else if (section_is_p (sectp->name, &names->info_dwo))
12320 {
12321 dwo_sections->info.s.section = sectp;
12322 dwo_sections->info.size = bfd_section_size (sectp);
12323 }
12324 else if (section_is_p (sectp->name, &names->line_dwo))
12325 {
12326 dwo_sections->line.s.section = sectp;
12327 dwo_sections->line.size = bfd_section_size (sectp);
12328 }
12329 else if (section_is_p (sectp->name, &names->loc_dwo))
12330 {
12331 dwo_sections->loc.s.section = sectp;
12332 dwo_sections->loc.size = bfd_section_size (sectp);
12333 }
12334 else if (section_is_p (sectp->name, &names->loclists_dwo))
12335 {
12336 dwo_sections->loclists.s.section = sectp;
12337 dwo_sections->loclists.size = bfd_section_size (sectp);
12338 }
12339 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12340 {
12341 dwo_sections->macinfo.s.section = sectp;
12342 dwo_sections->macinfo.size = bfd_section_size (sectp);
12343 }
12344 else if (section_is_p (sectp->name, &names->macro_dwo))
12345 {
12346 dwo_sections->macro.s.section = sectp;
12347 dwo_sections->macro.size = bfd_section_size (sectp);
12348 }
12349 else if (section_is_p (sectp->name, &names->str_dwo))
12350 {
12351 dwo_sections->str.s.section = sectp;
12352 dwo_sections->str.size = bfd_section_size (sectp);
12353 }
12354 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12355 {
12356 dwo_sections->str_offsets.s.section = sectp;
12357 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12358 }
12359 else if (section_is_p (sectp->name, &names->types_dwo))
12360 {
12361 struct dwarf2_section_info type_section;
12362
12363 memset (&type_section, 0, sizeof (type_section));
12364 type_section.s.section = sectp;
12365 type_section.size = bfd_section_size (sectp);
12366 dwo_sections->types.push_back (type_section);
12367 }
12368 }
12369
12370 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12371 by PER_CU. This is for the non-DWP case.
12372 The result is NULL if DWO_NAME can't be found. */
12373
12374 static struct dwo_file *
12375 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12376 const char *comp_dir)
12377 {
12378 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
12379
12380 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12381 if (dbfd == NULL)
12382 {
12383 if (dwarf_read_debug)
12384 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12385 return NULL;
12386 }
12387
12388 dwo_file_up dwo_file (new struct dwo_file);
12389 dwo_file->dwo_name = dwo_name;
12390 dwo_file->comp_dir = comp_dir;
12391 dwo_file->dbfd = std::move (dbfd);
12392
12393 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12394 &dwo_file->sections);
12395
12396 create_cus_hash_table (dwarf2_per_objfile, cu, *dwo_file,
12397 dwo_file->sections.info, dwo_file->cus);
12398
12399 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12400 dwo_file->sections.types, dwo_file->tus);
12401
12402 if (dwarf_read_debug)
12403 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12404
12405 return dwo_file.release ();
12406 }
12407
12408 /* This function is mapped across the sections and remembers the offset and
12409 size of each of the DWP debugging sections common to version 1 and 2 that
12410 we are interested in. */
12411
12412 static void
12413 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12414 void *dwp_file_ptr)
12415 {
12416 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12417 const struct dwop_section_names *names = &dwop_section_names;
12418 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12419
12420 /* Record the ELF section number for later lookup: this is what the
12421 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12422 gdb_assert (elf_section_nr < dwp_file->num_sections);
12423 dwp_file->elf_sections[elf_section_nr] = sectp;
12424
12425 /* Look for specific sections that we need. */
12426 if (section_is_p (sectp->name, &names->str_dwo))
12427 {
12428 dwp_file->sections.str.s.section = sectp;
12429 dwp_file->sections.str.size = bfd_section_size (sectp);
12430 }
12431 else if (section_is_p (sectp->name, &names->cu_index))
12432 {
12433 dwp_file->sections.cu_index.s.section = sectp;
12434 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12435 }
12436 else if (section_is_p (sectp->name, &names->tu_index))
12437 {
12438 dwp_file->sections.tu_index.s.section = sectp;
12439 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12440 }
12441 }
12442
12443 /* This function is mapped across the sections and remembers the offset and
12444 size of each of the DWP version 2 debugging sections that we are interested
12445 in. This is split into a separate function because we don't know if we
12446 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12447
12448 static void
12449 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12450 {
12451 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12452 const struct dwop_section_names *names = &dwop_section_names;
12453 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12454
12455 /* Record the ELF section number for later lookup: this is what the
12456 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12457 gdb_assert (elf_section_nr < dwp_file->num_sections);
12458 dwp_file->elf_sections[elf_section_nr] = sectp;
12459
12460 /* Look for specific sections that we need. */
12461 if (section_is_p (sectp->name, &names->abbrev_dwo))
12462 {
12463 dwp_file->sections.abbrev.s.section = sectp;
12464 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12465 }
12466 else if (section_is_p (sectp->name, &names->info_dwo))
12467 {
12468 dwp_file->sections.info.s.section = sectp;
12469 dwp_file->sections.info.size = bfd_section_size (sectp);
12470 }
12471 else if (section_is_p (sectp->name, &names->line_dwo))
12472 {
12473 dwp_file->sections.line.s.section = sectp;
12474 dwp_file->sections.line.size = bfd_section_size (sectp);
12475 }
12476 else if (section_is_p (sectp->name, &names->loc_dwo))
12477 {
12478 dwp_file->sections.loc.s.section = sectp;
12479 dwp_file->sections.loc.size = bfd_section_size (sectp);
12480 }
12481 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12482 {
12483 dwp_file->sections.macinfo.s.section = sectp;
12484 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12485 }
12486 else if (section_is_p (sectp->name, &names->macro_dwo))
12487 {
12488 dwp_file->sections.macro.s.section = sectp;
12489 dwp_file->sections.macro.size = bfd_section_size (sectp);
12490 }
12491 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12492 {
12493 dwp_file->sections.str_offsets.s.section = sectp;
12494 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12495 }
12496 else if (section_is_p (sectp->name, &names->types_dwo))
12497 {
12498 dwp_file->sections.types.s.section = sectp;
12499 dwp_file->sections.types.size = bfd_section_size (sectp);
12500 }
12501 }
12502
12503 /* Hash function for dwp_file loaded CUs/TUs. */
12504
12505 static hashval_t
12506 hash_dwp_loaded_cutus (const void *item)
12507 {
12508 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12509
12510 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12511 return dwo_unit->signature;
12512 }
12513
12514 /* Equality function for dwp_file loaded CUs/TUs. */
12515
12516 static int
12517 eq_dwp_loaded_cutus (const void *a, const void *b)
12518 {
12519 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12520 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12521
12522 return dua->signature == dub->signature;
12523 }
12524
12525 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12526
12527 static htab_up
12528 allocate_dwp_loaded_cutus_table ()
12529 {
12530 return htab_up (htab_create_alloc (3,
12531 hash_dwp_loaded_cutus,
12532 eq_dwp_loaded_cutus,
12533 NULL, xcalloc, xfree));
12534 }
12535
12536 /* Try to open DWP file FILE_NAME.
12537 The result is the bfd handle of the file.
12538 If there is a problem finding or opening the file, return NULL.
12539 Upon success, the canonicalized path of the file is stored in the bfd,
12540 same as symfile_bfd_open. */
12541
12542 static gdb_bfd_ref_ptr
12543 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12544 const char *file_name)
12545 {
12546 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12547 1 /*is_dwp*/,
12548 1 /*search_cwd*/));
12549 if (abfd != NULL)
12550 return abfd;
12551
12552 /* Work around upstream bug 15652.
12553 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12554 [Whether that's a "bug" is debatable, but it is getting in our way.]
12555 We have no real idea where the dwp file is, because gdb's realpath-ing
12556 of the executable's path may have discarded the needed info.
12557 [IWBN if the dwp file name was recorded in the executable, akin to
12558 .gnu_debuglink, but that doesn't exist yet.]
12559 Strip the directory from FILE_NAME and search again. */
12560 if (*debug_file_directory != '\0')
12561 {
12562 /* Don't implicitly search the current directory here.
12563 If the user wants to search "." to handle this case,
12564 it must be added to debug-file-directory. */
12565 return try_open_dwop_file (dwarf2_per_objfile,
12566 lbasename (file_name), 1 /*is_dwp*/,
12567 0 /*search_cwd*/);
12568 }
12569
12570 return NULL;
12571 }
12572
12573 /* Initialize the use of the DWP file for the current objfile.
12574 By convention the name of the DWP file is ${objfile}.dwp.
12575 The result is NULL if it can't be found. */
12576
12577 static std::unique_ptr<struct dwp_file>
12578 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12579 {
12580 struct objfile *objfile = dwarf2_per_objfile->objfile;
12581
12582 /* Try to find first .dwp for the binary file before any symbolic links
12583 resolving. */
12584
12585 /* If the objfile is a debug file, find the name of the real binary
12586 file and get the name of dwp file from there. */
12587 std::string dwp_name;
12588 if (objfile->separate_debug_objfile_backlink != NULL)
12589 {
12590 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12591 const char *backlink_basename = lbasename (backlink->original_name);
12592
12593 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12594 }
12595 else
12596 dwp_name = objfile->original_name;
12597
12598 dwp_name += ".dwp";
12599
12600 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12601 if (dbfd == NULL
12602 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12603 {
12604 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12605 dwp_name = objfile_name (objfile);
12606 dwp_name += ".dwp";
12607 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12608 }
12609
12610 if (dbfd == NULL)
12611 {
12612 if (dwarf_read_debug)
12613 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12614 return std::unique_ptr<dwp_file> ();
12615 }
12616
12617 const char *name = bfd_get_filename (dbfd.get ());
12618 std::unique_ptr<struct dwp_file> dwp_file
12619 (new struct dwp_file (name, std::move (dbfd)));
12620
12621 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12622 dwp_file->elf_sections =
12623 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
12624 dwp_file->num_sections, asection *);
12625
12626 bfd_map_over_sections (dwp_file->dbfd.get (),
12627 dwarf2_locate_common_dwp_sections,
12628 dwp_file.get ());
12629
12630 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12631 0);
12632
12633 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12634 1);
12635
12636 /* The DWP file version is stored in the hash table. Oh well. */
12637 if (dwp_file->cus && dwp_file->tus
12638 && dwp_file->cus->version != dwp_file->tus->version)
12639 {
12640 /* Technically speaking, we should try to limp along, but this is
12641 pretty bizarre. We use pulongest here because that's the established
12642 portability solution (e.g, we cannot use %u for uint32_t). */
12643 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12644 " TU version %s [in DWP file %s]"),
12645 pulongest (dwp_file->cus->version),
12646 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12647 }
12648
12649 if (dwp_file->cus)
12650 dwp_file->version = dwp_file->cus->version;
12651 else if (dwp_file->tus)
12652 dwp_file->version = dwp_file->tus->version;
12653 else
12654 dwp_file->version = 2;
12655
12656 if (dwp_file->version == 2)
12657 bfd_map_over_sections (dwp_file->dbfd.get (),
12658 dwarf2_locate_v2_dwp_sections,
12659 dwp_file.get ());
12660
12661 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12662 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12663
12664 if (dwarf_read_debug)
12665 {
12666 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12667 fprintf_unfiltered (gdb_stdlog,
12668 " %s CUs, %s TUs\n",
12669 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12670 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12671 }
12672
12673 return dwp_file;
12674 }
12675
12676 /* Wrapper around open_and_init_dwp_file, only open it once. */
12677
12678 static struct dwp_file *
12679 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12680 {
12681 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
12682 {
12683 dwarf2_per_objfile->per_bfd->dwp_file
12684 = open_and_init_dwp_file (dwarf2_per_objfile);
12685 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
12686 }
12687 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
12688 }
12689
12690 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12691 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12692 or in the DWP file for the objfile, referenced by THIS_UNIT.
12693 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12694 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12695
12696 This is called, for example, when wanting to read a variable with a
12697 complex location. Therefore we don't want to do file i/o for every call.
12698 Therefore we don't want to look for a DWO file on every call.
12699 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12700 then we check if we've already seen DWO_NAME, and only THEN do we check
12701 for a DWO file.
12702
12703 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12704 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12705
12706 static struct dwo_unit *
12707 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12708 ULONGEST signature, int is_debug_types)
12709 {
12710 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
12711 struct objfile *objfile = dwarf2_per_objfile->objfile;
12712 const char *kind = is_debug_types ? "TU" : "CU";
12713 void **dwo_file_slot;
12714 struct dwo_file *dwo_file;
12715 struct dwp_file *dwp_file;
12716
12717 /* First see if there's a DWP file.
12718 If we have a DWP file but didn't find the DWO inside it, don't
12719 look for the original DWO file. It makes gdb behave differently
12720 depending on whether one is debugging in the build tree. */
12721
12722 dwp_file = get_dwp_file (dwarf2_per_objfile);
12723 if (dwp_file != NULL)
12724 {
12725 const struct dwp_hash_table *dwp_htab =
12726 is_debug_types ? dwp_file->tus : dwp_file->cus;
12727
12728 if (dwp_htab != NULL)
12729 {
12730 struct dwo_unit *dwo_cutu =
12731 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12732 signature, is_debug_types);
12733
12734 if (dwo_cutu != NULL)
12735 {
12736 if (dwarf_read_debug)
12737 {
12738 fprintf_unfiltered (gdb_stdlog,
12739 "Virtual DWO %s %s found: @%s\n",
12740 kind, hex_string (signature),
12741 host_address_to_string (dwo_cutu));
12742 }
12743 return dwo_cutu;
12744 }
12745 }
12746 }
12747 else
12748 {
12749 /* No DWP file, look for the DWO file. */
12750
12751 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12752 dwo_name, comp_dir);
12753 if (*dwo_file_slot == NULL)
12754 {
12755 /* Read in the file and build a table of the CUs/TUs it contains. */
12756 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12757 }
12758 /* NOTE: This will be NULL if unable to open the file. */
12759 dwo_file = (struct dwo_file *) *dwo_file_slot;
12760
12761 if (dwo_file != NULL)
12762 {
12763 struct dwo_unit *dwo_cutu = NULL;
12764
12765 if (is_debug_types && dwo_file->tus)
12766 {
12767 struct dwo_unit find_dwo_cutu;
12768
12769 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12770 find_dwo_cutu.signature = signature;
12771 dwo_cutu
12772 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12773 &find_dwo_cutu);
12774 }
12775 else if (!is_debug_types && dwo_file->cus)
12776 {
12777 struct dwo_unit find_dwo_cutu;
12778
12779 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12780 find_dwo_cutu.signature = signature;
12781 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12782 &find_dwo_cutu);
12783 }
12784
12785 if (dwo_cutu != NULL)
12786 {
12787 if (dwarf_read_debug)
12788 {
12789 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12790 kind, dwo_name, hex_string (signature),
12791 host_address_to_string (dwo_cutu));
12792 }
12793 return dwo_cutu;
12794 }
12795 }
12796 }
12797
12798 /* We didn't find it. This could mean a dwo_id mismatch, or
12799 someone deleted the DWO/DWP file, or the search path isn't set up
12800 correctly to find the file. */
12801
12802 if (dwarf_read_debug)
12803 {
12804 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12805 kind, dwo_name, hex_string (signature));
12806 }
12807
12808 /* This is a warning and not a complaint because it can be caused by
12809 pilot error (e.g., user accidentally deleting the DWO). */
12810 {
12811 /* Print the name of the DWP file if we looked there, helps the user
12812 better diagnose the problem. */
12813 std::string dwp_text;
12814
12815 if (dwp_file != NULL)
12816 dwp_text = string_printf (" [in DWP file %s]",
12817 lbasename (dwp_file->name));
12818
12819 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12820 " [in module %s]"),
12821 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12822 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12823 }
12824 return NULL;
12825 }
12826
12827 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12828 See lookup_dwo_cutu_unit for details. */
12829
12830 static struct dwo_unit *
12831 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12832 ULONGEST signature)
12833 {
12834 gdb_assert (!cu->per_cu->is_debug_types);
12835
12836 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12837 }
12838
12839 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12840 See lookup_dwo_cutu_unit for details. */
12841
12842 static struct dwo_unit *
12843 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12844 {
12845 gdb_assert (cu->per_cu->is_debug_types);
12846
12847 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12848
12849 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12850 }
12851
12852 /* Traversal function for queue_and_load_all_dwo_tus. */
12853
12854 static int
12855 queue_and_load_dwo_tu (void **slot, void *info)
12856 {
12857 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12858 dwarf2_cu *cu = (dwarf2_cu *) info;
12859 ULONGEST signature = dwo_unit->signature;
12860 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12861
12862 if (sig_type != NULL)
12863 {
12864 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12865
12866 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12867 a real dependency of PER_CU on SIG_TYPE. That is detected later
12868 while processing PER_CU. */
12869 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
12870 load_full_type_unit (sig_cu, cu->per_objfile);
12871 cu->per_cu->imported_symtabs_push (sig_cu);
12872 }
12873
12874 return 1;
12875 }
12876
12877 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12878 The DWO may have the only definition of the type, though it may not be
12879 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12880 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12881
12882 static void
12883 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12884 {
12885 struct dwo_unit *dwo_unit;
12886 struct dwo_file *dwo_file;
12887
12888 gdb_assert (!per_cu->is_debug_types);
12889 gdb_assert (per_cu->cu != NULL);
12890 gdb_assert (get_dwp_file (per_cu->cu->per_objfile) == NULL);
12891
12892 dwo_unit = per_cu->cu->dwo_unit;
12893 gdb_assert (dwo_unit != NULL);
12894
12895 dwo_file = dwo_unit->dwo_file;
12896 if (dwo_file->tus != NULL)
12897 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12898 per_cu->cu);
12899 }
12900
12901 /* Read in various DIEs. */
12902
12903 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12904 Inherit only the children of the DW_AT_abstract_origin DIE not being
12905 already referenced by DW_AT_abstract_origin from the children of the
12906 current DIE. */
12907
12908 static void
12909 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12910 {
12911 struct die_info *child_die;
12912 sect_offset *offsetp;
12913 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12914 struct die_info *origin_die;
12915 /* Iterator of the ORIGIN_DIE children. */
12916 struct die_info *origin_child_die;
12917 struct attribute *attr;
12918 struct dwarf2_cu *origin_cu;
12919 struct pending **origin_previous_list_in_scope;
12920
12921 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12922 if (!attr)
12923 return;
12924
12925 /* Note that following die references may follow to a die in a
12926 different cu. */
12927
12928 origin_cu = cu;
12929 origin_die = follow_die_ref (die, attr, &origin_cu);
12930
12931 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12932 symbols in. */
12933 origin_previous_list_in_scope = origin_cu->list_in_scope;
12934 origin_cu->list_in_scope = cu->list_in_scope;
12935
12936 if (die->tag != origin_die->tag
12937 && !(die->tag == DW_TAG_inlined_subroutine
12938 && origin_die->tag == DW_TAG_subprogram))
12939 complaint (_("DIE %s and its abstract origin %s have different tags"),
12940 sect_offset_str (die->sect_off),
12941 sect_offset_str (origin_die->sect_off));
12942
12943 std::vector<sect_offset> offsets;
12944
12945 for (child_die = die->child;
12946 child_die && child_die->tag;
12947 child_die = child_die->sibling)
12948 {
12949 struct die_info *child_origin_die;
12950 struct dwarf2_cu *child_origin_cu;
12951
12952 /* We are trying to process concrete instance entries:
12953 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12954 it's not relevant to our analysis here. i.e. detecting DIEs that are
12955 present in the abstract instance but not referenced in the concrete
12956 one. */
12957 if (child_die->tag == DW_TAG_call_site
12958 || child_die->tag == DW_TAG_GNU_call_site)
12959 continue;
12960
12961 /* For each CHILD_DIE, find the corresponding child of
12962 ORIGIN_DIE. If there is more than one layer of
12963 DW_AT_abstract_origin, follow them all; there shouldn't be,
12964 but GCC versions at least through 4.4 generate this (GCC PR
12965 40573). */
12966 child_origin_die = child_die;
12967 child_origin_cu = cu;
12968 while (1)
12969 {
12970 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12971 child_origin_cu);
12972 if (attr == NULL)
12973 break;
12974 child_origin_die = follow_die_ref (child_origin_die, attr,
12975 &child_origin_cu);
12976 }
12977
12978 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12979 counterpart may exist. */
12980 if (child_origin_die != child_die)
12981 {
12982 if (child_die->tag != child_origin_die->tag
12983 && !(child_die->tag == DW_TAG_inlined_subroutine
12984 && child_origin_die->tag == DW_TAG_subprogram))
12985 complaint (_("Child DIE %s and its abstract origin %s have "
12986 "different tags"),
12987 sect_offset_str (child_die->sect_off),
12988 sect_offset_str (child_origin_die->sect_off));
12989 if (child_origin_die->parent != origin_die)
12990 complaint (_("Child DIE %s and its abstract origin %s have "
12991 "different parents"),
12992 sect_offset_str (child_die->sect_off),
12993 sect_offset_str (child_origin_die->sect_off));
12994 else
12995 offsets.push_back (child_origin_die->sect_off);
12996 }
12997 }
12998 std::sort (offsets.begin (), offsets.end ());
12999 sect_offset *offsets_end = offsets.data () + offsets.size ();
13000 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13001 if (offsetp[-1] == *offsetp)
13002 complaint (_("Multiple children of DIE %s refer "
13003 "to DIE %s as their abstract origin"),
13004 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13005
13006 offsetp = offsets.data ();
13007 origin_child_die = origin_die->child;
13008 while (origin_child_die && origin_child_die->tag)
13009 {
13010 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13011 while (offsetp < offsets_end
13012 && *offsetp < origin_child_die->sect_off)
13013 offsetp++;
13014 if (offsetp >= offsets_end
13015 || *offsetp > origin_child_die->sect_off)
13016 {
13017 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13018 Check whether we're already processing ORIGIN_CHILD_DIE.
13019 This can happen with mutually referenced abstract_origins.
13020 PR 16581. */
13021 if (!origin_child_die->in_process)
13022 process_die (origin_child_die, origin_cu);
13023 }
13024 origin_child_die = origin_child_die->sibling;
13025 }
13026 origin_cu->list_in_scope = origin_previous_list_in_scope;
13027
13028 if (cu != origin_cu)
13029 compute_delayed_physnames (origin_cu);
13030 }
13031
13032 static void
13033 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13034 {
13035 struct objfile *objfile = cu->per_objfile->objfile;
13036 struct gdbarch *gdbarch = objfile->arch ();
13037 struct context_stack *newobj;
13038 CORE_ADDR lowpc;
13039 CORE_ADDR highpc;
13040 struct die_info *child_die;
13041 struct attribute *attr, *call_line, *call_file;
13042 const char *name;
13043 CORE_ADDR baseaddr;
13044 struct block *block;
13045 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13046 std::vector<struct symbol *> template_args;
13047 struct template_symbol *templ_func = NULL;
13048
13049 if (inlined_func)
13050 {
13051 /* If we do not have call site information, we can't show the
13052 caller of this inlined function. That's too confusing, so
13053 only use the scope for local variables. */
13054 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13055 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13056 if (call_line == NULL || call_file == NULL)
13057 {
13058 read_lexical_block_scope (die, cu);
13059 return;
13060 }
13061 }
13062
13063 baseaddr = objfile->text_section_offset ();
13064
13065 name = dwarf2_name (die, cu);
13066
13067 /* Ignore functions with missing or empty names. These are actually
13068 illegal according to the DWARF standard. */
13069 if (name == NULL)
13070 {
13071 complaint (_("missing name for subprogram DIE at %s"),
13072 sect_offset_str (die->sect_off));
13073 return;
13074 }
13075
13076 /* Ignore functions with missing or invalid low and high pc attributes. */
13077 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13078 <= PC_BOUNDS_INVALID)
13079 {
13080 attr = dwarf2_attr (die, DW_AT_external, cu);
13081 if (!attr || !DW_UNSND (attr))
13082 complaint (_("cannot get low and high bounds "
13083 "for subprogram DIE at %s"),
13084 sect_offset_str (die->sect_off));
13085 return;
13086 }
13087
13088 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13089 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13090
13091 /* If we have any template arguments, then we must allocate a
13092 different sort of symbol. */
13093 for (child_die = die->child; child_die; child_die = child_die->sibling)
13094 {
13095 if (child_die->tag == DW_TAG_template_type_param
13096 || child_die->tag == DW_TAG_template_value_param)
13097 {
13098 templ_func = new (&objfile->objfile_obstack) template_symbol;
13099 templ_func->subclass = SYMBOL_TEMPLATE;
13100 break;
13101 }
13102 }
13103
13104 newobj = cu->get_builder ()->push_context (0, lowpc);
13105 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13106 (struct symbol *) templ_func);
13107
13108 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13109 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13110 cu->language);
13111
13112 /* If there is a location expression for DW_AT_frame_base, record
13113 it. */
13114 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13115 if (attr != nullptr)
13116 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13117
13118 /* If there is a location for the static link, record it. */
13119 newobj->static_link = NULL;
13120 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13121 if (attr != nullptr)
13122 {
13123 newobj->static_link
13124 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13125 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13126 cu->addr_type ());
13127 }
13128
13129 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13130
13131 if (die->child != NULL)
13132 {
13133 child_die = die->child;
13134 while (child_die && child_die->tag)
13135 {
13136 if (child_die->tag == DW_TAG_template_type_param
13137 || child_die->tag == DW_TAG_template_value_param)
13138 {
13139 struct symbol *arg = new_symbol (child_die, NULL, cu);
13140
13141 if (arg != NULL)
13142 template_args.push_back (arg);
13143 }
13144 else
13145 process_die (child_die, cu);
13146 child_die = child_die->sibling;
13147 }
13148 }
13149
13150 inherit_abstract_dies (die, cu);
13151
13152 /* If we have a DW_AT_specification, we might need to import using
13153 directives from the context of the specification DIE. See the
13154 comment in determine_prefix. */
13155 if (cu->language == language_cplus
13156 && dwarf2_attr (die, DW_AT_specification, cu))
13157 {
13158 struct dwarf2_cu *spec_cu = cu;
13159 struct die_info *spec_die = die_specification (die, &spec_cu);
13160
13161 while (spec_die)
13162 {
13163 child_die = spec_die->child;
13164 while (child_die && child_die->tag)
13165 {
13166 if (child_die->tag == DW_TAG_imported_module)
13167 process_die (child_die, spec_cu);
13168 child_die = child_die->sibling;
13169 }
13170
13171 /* In some cases, GCC generates specification DIEs that
13172 themselves contain DW_AT_specification attributes. */
13173 spec_die = die_specification (spec_die, &spec_cu);
13174 }
13175 }
13176
13177 struct context_stack cstk = cu->get_builder ()->pop_context ();
13178 /* Make a block for the local symbols within. */
13179 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13180 cstk.static_link, lowpc, highpc);
13181
13182 /* For C++, set the block's scope. */
13183 if ((cu->language == language_cplus
13184 || cu->language == language_fortran
13185 || cu->language == language_d
13186 || cu->language == language_rust)
13187 && cu->processing_has_namespace_info)
13188 block_set_scope (block, determine_prefix (die, cu),
13189 &objfile->objfile_obstack);
13190
13191 /* If we have address ranges, record them. */
13192 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13193
13194 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13195
13196 /* Attach template arguments to function. */
13197 if (!template_args.empty ())
13198 {
13199 gdb_assert (templ_func != NULL);
13200
13201 templ_func->n_template_arguments = template_args.size ();
13202 templ_func->template_arguments
13203 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13204 templ_func->n_template_arguments);
13205 memcpy (templ_func->template_arguments,
13206 template_args.data (),
13207 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13208
13209 /* Make sure that the symtab is set on the new symbols. Even
13210 though they don't appear in this symtab directly, other parts
13211 of gdb assume that symbols do, and this is reasonably
13212 true. */
13213 for (symbol *sym : template_args)
13214 symbol_set_symtab (sym, symbol_symtab (templ_func));
13215 }
13216
13217 /* In C++, we can have functions nested inside functions (e.g., when
13218 a function declares a class that has methods). This means that
13219 when we finish processing a function scope, we may need to go
13220 back to building a containing block's symbol lists. */
13221 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13222 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13223
13224 /* If we've finished processing a top-level function, subsequent
13225 symbols go in the file symbol list. */
13226 if (cu->get_builder ()->outermost_context_p ())
13227 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13228 }
13229
13230 /* Process all the DIES contained within a lexical block scope. Start
13231 a new scope, process the dies, and then close the scope. */
13232
13233 static void
13234 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13235 {
13236 struct objfile *objfile = cu->per_objfile->objfile;
13237 struct gdbarch *gdbarch = objfile->arch ();
13238 CORE_ADDR lowpc, highpc;
13239 struct die_info *child_die;
13240 CORE_ADDR baseaddr;
13241
13242 baseaddr = objfile->text_section_offset ();
13243
13244 /* Ignore blocks with missing or invalid low and high pc attributes. */
13245 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13246 as multiple lexical blocks? Handling children in a sane way would
13247 be nasty. Might be easier to properly extend generic blocks to
13248 describe ranges. */
13249 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13250 {
13251 case PC_BOUNDS_NOT_PRESENT:
13252 /* DW_TAG_lexical_block has no attributes, process its children as if
13253 there was no wrapping by that DW_TAG_lexical_block.
13254 GCC does no longer produces such DWARF since GCC r224161. */
13255 for (child_die = die->child;
13256 child_die != NULL && child_die->tag;
13257 child_die = child_die->sibling)
13258 {
13259 /* We might already be processing this DIE. This can happen
13260 in an unusual circumstance -- where a subroutine A
13261 appears lexically in another subroutine B, but A actually
13262 inlines B. The recursion is broken here, rather than in
13263 inherit_abstract_dies, because it seems better to simply
13264 drop concrete children here. */
13265 if (!child_die->in_process)
13266 process_die (child_die, cu);
13267 }
13268 return;
13269 case PC_BOUNDS_INVALID:
13270 return;
13271 }
13272 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13273 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13274
13275 cu->get_builder ()->push_context (0, lowpc);
13276 if (die->child != NULL)
13277 {
13278 child_die = die->child;
13279 while (child_die && child_die->tag)
13280 {
13281 process_die (child_die, cu);
13282 child_die = child_die->sibling;
13283 }
13284 }
13285 inherit_abstract_dies (die, cu);
13286 struct context_stack cstk = cu->get_builder ()->pop_context ();
13287
13288 if (*cu->get_builder ()->get_local_symbols () != NULL
13289 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13290 {
13291 struct block *block
13292 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13293 cstk.start_addr, highpc);
13294
13295 /* Note that recording ranges after traversing children, as we
13296 do here, means that recording a parent's ranges entails
13297 walking across all its children's ranges as they appear in
13298 the address map, which is quadratic behavior.
13299
13300 It would be nicer to record the parent's ranges before
13301 traversing its children, simply overriding whatever you find
13302 there. But since we don't even decide whether to create a
13303 block until after we've traversed its children, that's hard
13304 to do. */
13305 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13306 }
13307 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13308 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13309 }
13310
13311 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13312
13313 static void
13314 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13315 {
13316 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13317 struct objfile *objfile = per_objfile->objfile;
13318 struct gdbarch *gdbarch = objfile->arch ();
13319 CORE_ADDR pc, baseaddr;
13320 struct attribute *attr;
13321 struct call_site *call_site, call_site_local;
13322 void **slot;
13323 int nparams;
13324 struct die_info *child_die;
13325
13326 baseaddr = objfile->text_section_offset ();
13327
13328 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13329 if (attr == NULL)
13330 {
13331 /* This was a pre-DWARF-5 GNU extension alias
13332 for DW_AT_call_return_pc. */
13333 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13334 }
13335 if (!attr)
13336 {
13337 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13338 "DIE %s [in module %s]"),
13339 sect_offset_str (die->sect_off), objfile_name (objfile));
13340 return;
13341 }
13342 pc = attr->value_as_address () + baseaddr;
13343 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13344
13345 if (cu->call_site_htab == NULL)
13346 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13347 NULL, &objfile->objfile_obstack,
13348 hashtab_obstack_allocate, NULL);
13349 call_site_local.pc = pc;
13350 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13351 if (*slot != NULL)
13352 {
13353 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13354 "DIE %s [in module %s]"),
13355 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13356 objfile_name (objfile));
13357 return;
13358 }
13359
13360 /* Count parameters at the caller. */
13361
13362 nparams = 0;
13363 for (child_die = die->child; child_die && child_die->tag;
13364 child_die = child_die->sibling)
13365 {
13366 if (child_die->tag != DW_TAG_call_site_parameter
13367 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13368 {
13369 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13370 "DW_TAG_call_site child DIE %s [in module %s]"),
13371 child_die->tag, sect_offset_str (child_die->sect_off),
13372 objfile_name (objfile));
13373 continue;
13374 }
13375
13376 nparams++;
13377 }
13378
13379 call_site
13380 = ((struct call_site *)
13381 obstack_alloc (&objfile->objfile_obstack,
13382 sizeof (*call_site)
13383 + (sizeof (*call_site->parameter) * (nparams - 1))));
13384 *slot = call_site;
13385 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13386 call_site->pc = pc;
13387
13388 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13389 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13390 {
13391 struct die_info *func_die;
13392
13393 /* Skip also over DW_TAG_inlined_subroutine. */
13394 for (func_die = die->parent;
13395 func_die && func_die->tag != DW_TAG_subprogram
13396 && func_die->tag != DW_TAG_subroutine_type;
13397 func_die = func_die->parent);
13398
13399 /* DW_AT_call_all_calls is a superset
13400 of DW_AT_call_all_tail_calls. */
13401 if (func_die
13402 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13403 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13404 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13405 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13406 {
13407 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13408 not complete. But keep CALL_SITE for look ups via call_site_htab,
13409 both the initial caller containing the real return address PC and
13410 the final callee containing the current PC of a chain of tail
13411 calls do not need to have the tail call list complete. But any
13412 function candidate for a virtual tail call frame searched via
13413 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13414 determined unambiguously. */
13415 }
13416 else
13417 {
13418 struct type *func_type = NULL;
13419
13420 if (func_die)
13421 func_type = get_die_type (func_die, cu);
13422 if (func_type != NULL)
13423 {
13424 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13425
13426 /* Enlist this call site to the function. */
13427 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13428 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13429 }
13430 else
13431 complaint (_("Cannot find function owning DW_TAG_call_site "
13432 "DIE %s [in module %s]"),
13433 sect_offset_str (die->sect_off), objfile_name (objfile));
13434 }
13435 }
13436
13437 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13438 if (attr == NULL)
13439 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13440 if (attr == NULL)
13441 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13442 if (attr == NULL)
13443 {
13444 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13445 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13446 }
13447 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13448 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13449 /* Keep NULL DWARF_BLOCK. */;
13450 else if (attr->form_is_block ())
13451 {
13452 struct dwarf2_locexpr_baton *dlbaton;
13453
13454 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13455 dlbaton->data = DW_BLOCK (attr)->data;
13456 dlbaton->size = DW_BLOCK (attr)->size;
13457 dlbaton->per_objfile = per_objfile;
13458 dlbaton->per_cu = cu->per_cu;
13459
13460 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13461 }
13462 else if (attr->form_is_ref ())
13463 {
13464 struct dwarf2_cu *target_cu = cu;
13465 struct die_info *target_die;
13466
13467 target_die = follow_die_ref (die, attr, &target_cu);
13468 gdb_assert (target_cu->per_objfile->objfile == objfile);
13469 if (die_is_declaration (target_die, target_cu))
13470 {
13471 const char *target_physname;
13472
13473 /* Prefer the mangled name; otherwise compute the demangled one. */
13474 target_physname = dw2_linkage_name (target_die, target_cu);
13475 if (target_physname == NULL)
13476 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13477 if (target_physname == NULL)
13478 complaint (_("DW_AT_call_target target DIE has invalid "
13479 "physname, for referencing DIE %s [in module %s]"),
13480 sect_offset_str (die->sect_off), objfile_name (objfile));
13481 else
13482 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13483 }
13484 else
13485 {
13486 CORE_ADDR lowpc;
13487
13488 /* DW_AT_entry_pc should be preferred. */
13489 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13490 <= PC_BOUNDS_INVALID)
13491 complaint (_("DW_AT_call_target target DIE has invalid "
13492 "low pc, for referencing DIE %s [in module %s]"),
13493 sect_offset_str (die->sect_off), objfile_name (objfile));
13494 else
13495 {
13496 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13497 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13498 }
13499 }
13500 }
13501 else
13502 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13503 "block nor reference, for DIE %s [in module %s]"),
13504 sect_offset_str (die->sect_off), objfile_name (objfile));
13505
13506 call_site->per_cu = cu->per_cu;
13507 call_site->per_objfile = per_objfile;
13508
13509 for (child_die = die->child;
13510 child_die && child_die->tag;
13511 child_die = child_die->sibling)
13512 {
13513 struct call_site_parameter *parameter;
13514 struct attribute *loc, *origin;
13515
13516 if (child_die->tag != DW_TAG_call_site_parameter
13517 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13518 {
13519 /* Already printed the complaint above. */
13520 continue;
13521 }
13522
13523 gdb_assert (call_site->parameter_count < nparams);
13524 parameter = &call_site->parameter[call_site->parameter_count];
13525
13526 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13527 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13528 register is contained in DW_AT_call_value. */
13529
13530 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13531 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13532 if (origin == NULL)
13533 {
13534 /* This was a pre-DWARF-5 GNU extension alias
13535 for DW_AT_call_parameter. */
13536 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13537 }
13538 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13539 {
13540 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13541
13542 sect_offset sect_off = origin->get_ref_die_offset ();
13543 if (!cu->header.offset_in_cu_p (sect_off))
13544 {
13545 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13546 binding can be done only inside one CU. Such referenced DIE
13547 therefore cannot be even moved to DW_TAG_partial_unit. */
13548 complaint (_("DW_AT_call_parameter offset is not in CU for "
13549 "DW_TAG_call_site child DIE %s [in module %s]"),
13550 sect_offset_str (child_die->sect_off),
13551 objfile_name (objfile));
13552 continue;
13553 }
13554 parameter->u.param_cu_off
13555 = (cu_offset) (sect_off - cu->header.sect_off);
13556 }
13557 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13558 {
13559 complaint (_("No DW_FORM_block* DW_AT_location for "
13560 "DW_TAG_call_site child DIE %s [in module %s]"),
13561 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13562 continue;
13563 }
13564 else
13565 {
13566 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13567 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13568 if (parameter->u.dwarf_reg != -1)
13569 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13570 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13571 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13572 &parameter->u.fb_offset))
13573 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13574 else
13575 {
13576 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13577 "for DW_FORM_block* DW_AT_location is supported for "
13578 "DW_TAG_call_site child DIE %s "
13579 "[in module %s]"),
13580 sect_offset_str (child_die->sect_off),
13581 objfile_name (objfile));
13582 continue;
13583 }
13584 }
13585
13586 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13587 if (attr == NULL)
13588 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13589 if (attr == NULL || !attr->form_is_block ())
13590 {
13591 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13592 "DW_TAG_call_site child DIE %s [in module %s]"),
13593 sect_offset_str (child_die->sect_off),
13594 objfile_name (objfile));
13595 continue;
13596 }
13597 parameter->value = DW_BLOCK (attr)->data;
13598 parameter->value_size = DW_BLOCK (attr)->size;
13599
13600 /* Parameters are not pre-cleared by memset above. */
13601 parameter->data_value = NULL;
13602 parameter->data_value_size = 0;
13603 call_site->parameter_count++;
13604
13605 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13606 if (attr == NULL)
13607 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13608 if (attr != nullptr)
13609 {
13610 if (!attr->form_is_block ())
13611 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13612 "DW_TAG_call_site child DIE %s [in module %s]"),
13613 sect_offset_str (child_die->sect_off),
13614 objfile_name (objfile));
13615 else
13616 {
13617 parameter->data_value = DW_BLOCK (attr)->data;
13618 parameter->data_value_size = DW_BLOCK (attr)->size;
13619 }
13620 }
13621 }
13622 }
13623
13624 /* Helper function for read_variable. If DIE represents a virtual
13625 table, then return the type of the concrete object that is
13626 associated with the virtual table. Otherwise, return NULL. */
13627
13628 static struct type *
13629 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13630 {
13631 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13632 if (attr == NULL)
13633 return NULL;
13634
13635 /* Find the type DIE. */
13636 struct die_info *type_die = NULL;
13637 struct dwarf2_cu *type_cu = cu;
13638
13639 if (attr->form_is_ref ())
13640 type_die = follow_die_ref (die, attr, &type_cu);
13641 if (type_die == NULL)
13642 return NULL;
13643
13644 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13645 return NULL;
13646 return die_containing_type (type_die, type_cu);
13647 }
13648
13649 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13650
13651 static void
13652 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13653 {
13654 struct rust_vtable_symbol *storage = NULL;
13655
13656 if (cu->language == language_rust)
13657 {
13658 struct type *containing_type = rust_containing_type (die, cu);
13659
13660 if (containing_type != NULL)
13661 {
13662 struct objfile *objfile = cu->per_objfile->objfile;
13663
13664 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13665 storage->concrete_type = containing_type;
13666 storage->subclass = SYMBOL_RUST_VTABLE;
13667 }
13668 }
13669
13670 struct symbol *res = new_symbol (die, NULL, cu, storage);
13671 struct attribute *abstract_origin
13672 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13673 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13674 if (res == NULL && loc && abstract_origin)
13675 {
13676 /* We have a variable without a name, but with a location and an abstract
13677 origin. This may be a concrete instance of an abstract variable
13678 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13679 later. */
13680 struct dwarf2_cu *origin_cu = cu;
13681 struct die_info *origin_die
13682 = follow_die_ref (die, abstract_origin, &origin_cu);
13683 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13684 per_objfile->per_bfd->abstract_to_concrete
13685 [origin_die->sect_off].push_back (die->sect_off);
13686 }
13687 }
13688
13689 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13690 reading .debug_rnglists.
13691 Callback's type should be:
13692 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13693 Return true if the attributes are present and valid, otherwise,
13694 return false. */
13695
13696 template <typename Callback>
13697 static bool
13698 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13699 Callback &&callback)
13700 {
13701 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
13702 struct objfile *objfile = dwarf2_per_objfile->objfile;
13703 bfd *obfd = objfile->obfd;
13704 /* Base address selection entry. */
13705 gdb::optional<CORE_ADDR> base;
13706 const gdb_byte *buffer;
13707 CORE_ADDR baseaddr;
13708 bool overflow = false;
13709
13710 base = cu->base_address;
13711
13712 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13713 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
13714 {
13715 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13716 offset);
13717 return false;
13718 }
13719 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
13720
13721 baseaddr = objfile->text_section_offset ();
13722
13723 while (1)
13724 {
13725 /* Initialize it due to a false compiler warning. */
13726 CORE_ADDR range_beginning = 0, range_end = 0;
13727 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13728 + dwarf2_per_objfile->per_bfd->rnglists.size);
13729 unsigned int bytes_read;
13730
13731 if (buffer == buf_end)
13732 {
13733 overflow = true;
13734 break;
13735 }
13736 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13737 switch (rlet)
13738 {
13739 case DW_RLE_end_of_list:
13740 break;
13741 case DW_RLE_base_address:
13742 if (buffer + cu->header.addr_size > buf_end)
13743 {
13744 overflow = true;
13745 break;
13746 }
13747 base = cu->header.read_address (obfd, buffer, &bytes_read);
13748 buffer += bytes_read;
13749 break;
13750 case DW_RLE_start_length:
13751 if (buffer + cu->header.addr_size > buf_end)
13752 {
13753 overflow = true;
13754 break;
13755 }
13756 range_beginning = cu->header.read_address (obfd, buffer,
13757 &bytes_read);
13758 buffer += bytes_read;
13759 range_end = (range_beginning
13760 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13761 buffer += bytes_read;
13762 if (buffer > buf_end)
13763 {
13764 overflow = true;
13765 break;
13766 }
13767 break;
13768 case DW_RLE_offset_pair:
13769 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13770 buffer += bytes_read;
13771 if (buffer > buf_end)
13772 {
13773 overflow = true;
13774 break;
13775 }
13776 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13777 buffer += bytes_read;
13778 if (buffer > buf_end)
13779 {
13780 overflow = true;
13781 break;
13782 }
13783 break;
13784 case DW_RLE_start_end:
13785 if (buffer + 2 * cu->header.addr_size > buf_end)
13786 {
13787 overflow = true;
13788 break;
13789 }
13790 range_beginning = cu->header.read_address (obfd, buffer,
13791 &bytes_read);
13792 buffer += bytes_read;
13793 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13794 buffer += bytes_read;
13795 break;
13796 default:
13797 complaint (_("Invalid .debug_rnglists data (no base address)"));
13798 return false;
13799 }
13800 if (rlet == DW_RLE_end_of_list || overflow)
13801 break;
13802 if (rlet == DW_RLE_base_address)
13803 continue;
13804
13805 if (!base.has_value ())
13806 {
13807 /* We have no valid base address for the ranges
13808 data. */
13809 complaint (_("Invalid .debug_rnglists data (no base address)"));
13810 return false;
13811 }
13812
13813 if (range_beginning > range_end)
13814 {
13815 /* Inverted range entries are invalid. */
13816 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13817 return false;
13818 }
13819
13820 /* Empty range entries have no effect. */
13821 if (range_beginning == range_end)
13822 continue;
13823
13824 range_beginning += *base;
13825 range_end += *base;
13826
13827 /* A not-uncommon case of bad debug info.
13828 Don't pollute the addrmap with bad data. */
13829 if (range_beginning + baseaddr == 0
13830 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
13831 {
13832 complaint (_(".debug_rnglists entry has start address of zero"
13833 " [in module %s]"), objfile_name (objfile));
13834 continue;
13835 }
13836
13837 callback (range_beginning, range_end);
13838 }
13839
13840 if (overflow)
13841 {
13842 complaint (_("Offset %d is not terminated "
13843 "for DW_AT_ranges attribute"),
13844 offset);
13845 return false;
13846 }
13847
13848 return true;
13849 }
13850
13851 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13852 Callback's type should be:
13853 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13854 Return 1 if the attributes are present and valid, otherwise, return 0. */
13855
13856 template <typename Callback>
13857 static int
13858 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13859 Callback &&callback)
13860 {
13861 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13862 struct objfile *objfile = per_objfile->objfile;
13863 struct comp_unit_head *cu_header = &cu->header;
13864 bfd *obfd = objfile->obfd;
13865 unsigned int addr_size = cu_header->addr_size;
13866 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13867 /* Base address selection entry. */
13868 gdb::optional<CORE_ADDR> base;
13869 unsigned int dummy;
13870 const gdb_byte *buffer;
13871 CORE_ADDR baseaddr;
13872
13873 if (cu_header->version >= 5)
13874 return dwarf2_rnglists_process (offset, cu, callback);
13875
13876 base = cu->base_address;
13877
13878 per_objfile->per_bfd->ranges.read (objfile);
13879 if (offset >= per_objfile->per_bfd->ranges.size)
13880 {
13881 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13882 offset);
13883 return 0;
13884 }
13885 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13886
13887 baseaddr = objfile->text_section_offset ();
13888
13889 while (1)
13890 {
13891 CORE_ADDR range_beginning, range_end;
13892
13893 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13894 buffer += addr_size;
13895 range_end = cu->header.read_address (obfd, buffer, &dummy);
13896 buffer += addr_size;
13897 offset += 2 * addr_size;
13898
13899 /* An end of list marker is a pair of zero addresses. */
13900 if (range_beginning == 0 && range_end == 0)
13901 /* Found the end of list entry. */
13902 break;
13903
13904 /* Each base address selection entry is a pair of 2 values.
13905 The first is the largest possible address, the second is
13906 the base address. Check for a base address here. */
13907 if ((range_beginning & mask) == mask)
13908 {
13909 /* If we found the largest possible address, then we already
13910 have the base address in range_end. */
13911 base = range_end;
13912 continue;
13913 }
13914
13915 if (!base.has_value ())
13916 {
13917 /* We have no valid base address for the ranges
13918 data. */
13919 complaint (_("Invalid .debug_ranges data (no base address)"));
13920 return 0;
13921 }
13922
13923 if (range_beginning > range_end)
13924 {
13925 /* Inverted range entries are invalid. */
13926 complaint (_("Invalid .debug_ranges data (inverted range)"));
13927 return 0;
13928 }
13929
13930 /* Empty range entries have no effect. */
13931 if (range_beginning == range_end)
13932 continue;
13933
13934 range_beginning += *base;
13935 range_end += *base;
13936
13937 /* A not-uncommon case of bad debug info.
13938 Don't pollute the addrmap with bad data. */
13939 if (range_beginning + baseaddr == 0
13940 && !per_objfile->per_bfd->has_section_at_zero)
13941 {
13942 complaint (_(".debug_ranges entry has start address of zero"
13943 " [in module %s]"), objfile_name (objfile));
13944 continue;
13945 }
13946
13947 callback (range_beginning, range_end);
13948 }
13949
13950 return 1;
13951 }
13952
13953 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13954 Return 1 if the attributes are present and valid, otherwise, return 0.
13955 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13956
13957 static int
13958 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13959 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13960 dwarf2_psymtab *ranges_pst)
13961 {
13962 struct objfile *objfile = cu->per_objfile->objfile;
13963 struct gdbarch *gdbarch = objfile->arch ();
13964 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13965 int low_set = 0;
13966 CORE_ADDR low = 0;
13967 CORE_ADDR high = 0;
13968 int retval;
13969
13970 retval = dwarf2_ranges_process (offset, cu,
13971 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13972 {
13973 if (ranges_pst != NULL)
13974 {
13975 CORE_ADDR lowpc;
13976 CORE_ADDR highpc;
13977
13978 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13979 range_beginning + baseaddr)
13980 - baseaddr);
13981 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13982 range_end + baseaddr)
13983 - baseaddr);
13984 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13985 lowpc, highpc - 1, ranges_pst);
13986 }
13987
13988 /* FIXME: This is recording everything as a low-high
13989 segment of consecutive addresses. We should have a
13990 data structure for discontiguous block ranges
13991 instead. */
13992 if (! low_set)
13993 {
13994 low = range_beginning;
13995 high = range_end;
13996 low_set = 1;
13997 }
13998 else
13999 {
14000 if (range_beginning < low)
14001 low = range_beginning;
14002 if (range_end > high)
14003 high = range_end;
14004 }
14005 });
14006 if (!retval)
14007 return 0;
14008
14009 if (! low_set)
14010 /* If the first entry is an end-of-list marker, the range
14011 describes an empty scope, i.e. no instructions. */
14012 return 0;
14013
14014 if (low_return)
14015 *low_return = low;
14016 if (high_return)
14017 *high_return = high;
14018 return 1;
14019 }
14020
14021 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14022 definition for the return value. *LOWPC and *HIGHPC are set iff
14023 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14024
14025 static enum pc_bounds_kind
14026 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14027 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14028 dwarf2_psymtab *pst)
14029 {
14030 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
14031 struct attribute *attr;
14032 struct attribute *attr_high;
14033 CORE_ADDR low = 0;
14034 CORE_ADDR high = 0;
14035 enum pc_bounds_kind ret;
14036
14037 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14038 if (attr_high)
14039 {
14040 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14041 if (attr != nullptr)
14042 {
14043 low = attr->value_as_address ();
14044 high = attr_high->value_as_address ();
14045 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14046 high += low;
14047 }
14048 else
14049 /* Found high w/o low attribute. */
14050 return PC_BOUNDS_INVALID;
14051
14052 /* Found consecutive range of addresses. */
14053 ret = PC_BOUNDS_HIGH_LOW;
14054 }
14055 else
14056 {
14057 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14058 if (attr != NULL)
14059 {
14060 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14061 We take advantage of the fact that DW_AT_ranges does not appear
14062 in DW_TAG_compile_unit of DWO files. */
14063 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14064 unsigned int ranges_offset = (DW_UNSND (attr)
14065 + (need_ranges_base
14066 ? cu->ranges_base
14067 : 0));
14068
14069 /* Value of the DW_AT_ranges attribute is the offset in the
14070 .debug_ranges section. */
14071 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14072 return PC_BOUNDS_INVALID;
14073 /* Found discontinuous range of addresses. */
14074 ret = PC_BOUNDS_RANGES;
14075 }
14076 else
14077 return PC_BOUNDS_NOT_PRESENT;
14078 }
14079
14080 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14081 if (high <= low)
14082 return PC_BOUNDS_INVALID;
14083
14084 /* When using the GNU linker, .gnu.linkonce. sections are used to
14085 eliminate duplicate copies of functions and vtables and such.
14086 The linker will arbitrarily choose one and discard the others.
14087 The AT_*_pc values for such functions refer to local labels in
14088 these sections. If the section from that file was discarded, the
14089 labels are not in the output, so the relocs get a value of 0.
14090 If this is a discarded function, mark the pc bounds as invalid,
14091 so that GDB will ignore it. */
14092 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
14093 return PC_BOUNDS_INVALID;
14094
14095 *lowpc = low;
14096 if (highpc)
14097 *highpc = high;
14098 return ret;
14099 }
14100
14101 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14102 its low and high PC addresses. Do nothing if these addresses could not
14103 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14104 and HIGHPC to the high address if greater than HIGHPC. */
14105
14106 static void
14107 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14108 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14109 struct dwarf2_cu *cu)
14110 {
14111 CORE_ADDR low, high;
14112 struct die_info *child = die->child;
14113
14114 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14115 {
14116 *lowpc = std::min (*lowpc, low);
14117 *highpc = std::max (*highpc, high);
14118 }
14119
14120 /* If the language does not allow nested subprograms (either inside
14121 subprograms or lexical blocks), we're done. */
14122 if (cu->language != language_ada)
14123 return;
14124
14125 /* Check all the children of the given DIE. If it contains nested
14126 subprograms, then check their pc bounds. Likewise, we need to
14127 check lexical blocks as well, as they may also contain subprogram
14128 definitions. */
14129 while (child && child->tag)
14130 {
14131 if (child->tag == DW_TAG_subprogram
14132 || child->tag == DW_TAG_lexical_block)
14133 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14134 child = child->sibling;
14135 }
14136 }
14137
14138 /* Get the low and high pc's represented by the scope DIE, and store
14139 them in *LOWPC and *HIGHPC. If the correct values can't be
14140 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14141
14142 static void
14143 get_scope_pc_bounds (struct die_info *die,
14144 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14145 struct dwarf2_cu *cu)
14146 {
14147 CORE_ADDR best_low = (CORE_ADDR) -1;
14148 CORE_ADDR best_high = (CORE_ADDR) 0;
14149 CORE_ADDR current_low, current_high;
14150
14151 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14152 >= PC_BOUNDS_RANGES)
14153 {
14154 best_low = current_low;
14155 best_high = current_high;
14156 }
14157 else
14158 {
14159 struct die_info *child = die->child;
14160
14161 while (child && child->tag)
14162 {
14163 switch (child->tag) {
14164 case DW_TAG_subprogram:
14165 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14166 break;
14167 case DW_TAG_namespace:
14168 case DW_TAG_module:
14169 /* FIXME: carlton/2004-01-16: Should we do this for
14170 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14171 that current GCC's always emit the DIEs corresponding
14172 to definitions of methods of classes as children of a
14173 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14174 the DIEs giving the declarations, which could be
14175 anywhere). But I don't see any reason why the
14176 standards says that they have to be there. */
14177 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14178
14179 if (current_low != ((CORE_ADDR) -1))
14180 {
14181 best_low = std::min (best_low, current_low);
14182 best_high = std::max (best_high, current_high);
14183 }
14184 break;
14185 default:
14186 /* Ignore. */
14187 break;
14188 }
14189
14190 child = child->sibling;
14191 }
14192 }
14193
14194 *lowpc = best_low;
14195 *highpc = best_high;
14196 }
14197
14198 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14199 in DIE. */
14200
14201 static void
14202 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14203 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14204 {
14205 struct objfile *objfile = cu->per_objfile->objfile;
14206 struct gdbarch *gdbarch = objfile->arch ();
14207 struct attribute *attr;
14208 struct attribute *attr_high;
14209
14210 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14211 if (attr_high)
14212 {
14213 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14214 if (attr != nullptr)
14215 {
14216 CORE_ADDR low = attr->value_as_address ();
14217 CORE_ADDR high = attr_high->value_as_address ();
14218
14219 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14220 high += low;
14221
14222 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14223 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14224 cu->get_builder ()->record_block_range (block, low, high - 1);
14225 }
14226 }
14227
14228 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14229 if (attr != nullptr)
14230 {
14231 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14232 We take advantage of the fact that DW_AT_ranges does not appear
14233 in DW_TAG_compile_unit of DWO files. */
14234 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14235
14236 /* The value of the DW_AT_ranges attribute is the offset of the
14237 address range list in the .debug_ranges section. */
14238 unsigned long offset = (DW_UNSND (attr)
14239 + (need_ranges_base ? cu->ranges_base : 0));
14240
14241 std::vector<blockrange> blockvec;
14242 dwarf2_ranges_process (offset, cu,
14243 [&] (CORE_ADDR start, CORE_ADDR end)
14244 {
14245 start += baseaddr;
14246 end += baseaddr;
14247 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14248 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14249 cu->get_builder ()->record_block_range (block, start, end - 1);
14250 blockvec.emplace_back (start, end);
14251 });
14252
14253 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14254 }
14255 }
14256
14257 /* Check whether the producer field indicates either of GCC < 4.6, or the
14258 Intel C/C++ compiler, and cache the result in CU. */
14259
14260 static void
14261 check_producer (struct dwarf2_cu *cu)
14262 {
14263 int major, minor;
14264
14265 if (cu->producer == NULL)
14266 {
14267 /* For unknown compilers expect their behavior is DWARF version
14268 compliant.
14269
14270 GCC started to support .debug_types sections by -gdwarf-4 since
14271 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14272 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14273 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14274 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14275 }
14276 else if (producer_is_gcc (cu->producer, &major, &minor))
14277 {
14278 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14279 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14280 }
14281 else if (producer_is_icc (cu->producer, &major, &minor))
14282 {
14283 cu->producer_is_icc = true;
14284 cu->producer_is_icc_lt_14 = major < 14;
14285 }
14286 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14287 cu->producer_is_codewarrior = true;
14288 else
14289 {
14290 /* For other non-GCC compilers, expect their behavior is DWARF version
14291 compliant. */
14292 }
14293
14294 cu->checked_producer = true;
14295 }
14296
14297 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14298 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14299 during 4.6.0 experimental. */
14300
14301 static bool
14302 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14303 {
14304 if (!cu->checked_producer)
14305 check_producer (cu);
14306
14307 return cu->producer_is_gxx_lt_4_6;
14308 }
14309
14310
14311 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14312 with incorrect is_stmt attributes. */
14313
14314 static bool
14315 producer_is_codewarrior (struct dwarf2_cu *cu)
14316 {
14317 if (!cu->checked_producer)
14318 check_producer (cu);
14319
14320 return cu->producer_is_codewarrior;
14321 }
14322
14323 /* Return the default accessibility type if it is not overridden by
14324 DW_AT_accessibility. */
14325
14326 static enum dwarf_access_attribute
14327 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14328 {
14329 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14330 {
14331 /* The default DWARF 2 accessibility for members is public, the default
14332 accessibility for inheritance is private. */
14333
14334 if (die->tag != DW_TAG_inheritance)
14335 return DW_ACCESS_public;
14336 else
14337 return DW_ACCESS_private;
14338 }
14339 else
14340 {
14341 /* DWARF 3+ defines the default accessibility a different way. The same
14342 rules apply now for DW_TAG_inheritance as for the members and it only
14343 depends on the container kind. */
14344
14345 if (die->parent->tag == DW_TAG_class_type)
14346 return DW_ACCESS_private;
14347 else
14348 return DW_ACCESS_public;
14349 }
14350 }
14351
14352 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14353 offset. If the attribute was not found return 0, otherwise return
14354 1. If it was found but could not properly be handled, set *OFFSET
14355 to 0. */
14356
14357 static int
14358 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14359 LONGEST *offset)
14360 {
14361 struct attribute *attr;
14362
14363 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14364 if (attr != NULL)
14365 {
14366 *offset = 0;
14367
14368 /* Note that we do not check for a section offset first here.
14369 This is because DW_AT_data_member_location is new in DWARF 4,
14370 so if we see it, we can assume that a constant form is really
14371 a constant and not a section offset. */
14372 if (attr->form_is_constant ())
14373 *offset = attr->constant_value (0);
14374 else if (attr->form_is_section_offset ())
14375 dwarf2_complex_location_expr_complaint ();
14376 else if (attr->form_is_block ())
14377 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14378 else
14379 dwarf2_complex_location_expr_complaint ();
14380
14381 return 1;
14382 }
14383
14384 return 0;
14385 }
14386
14387 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14388
14389 static void
14390 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14391 struct field *field)
14392 {
14393 struct attribute *attr;
14394
14395 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14396 if (attr != NULL)
14397 {
14398 if (attr->form_is_constant ())
14399 {
14400 LONGEST offset = attr->constant_value (0);
14401 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14402 }
14403 else if (attr->form_is_section_offset ())
14404 dwarf2_complex_location_expr_complaint ();
14405 else if (attr->form_is_block ())
14406 {
14407 bool handled;
14408 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14409 if (handled)
14410 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14411 else
14412 {
14413 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14414 struct objfile *objfile = per_objfile->objfile;
14415 struct dwarf2_locexpr_baton *dlbaton
14416 = XOBNEW (&objfile->objfile_obstack,
14417 struct dwarf2_locexpr_baton);
14418 dlbaton->data = DW_BLOCK (attr)->data;
14419 dlbaton->size = DW_BLOCK (attr)->size;
14420 /* When using this baton, we want to compute the address
14421 of the field, not the value. This is why
14422 is_reference is set to false here. */
14423 dlbaton->is_reference = false;
14424 dlbaton->per_objfile = per_objfile;
14425 dlbaton->per_cu = cu->per_cu;
14426
14427 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14428 }
14429 }
14430 else
14431 dwarf2_complex_location_expr_complaint ();
14432 }
14433 }
14434
14435 /* Add an aggregate field to the field list. */
14436
14437 static void
14438 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14439 struct dwarf2_cu *cu)
14440 {
14441 struct objfile *objfile = cu->per_objfile->objfile;
14442 struct gdbarch *gdbarch = objfile->arch ();
14443 struct nextfield *new_field;
14444 struct attribute *attr;
14445 struct field *fp;
14446 const char *fieldname = "";
14447
14448 if (die->tag == DW_TAG_inheritance)
14449 {
14450 fip->baseclasses.emplace_back ();
14451 new_field = &fip->baseclasses.back ();
14452 }
14453 else
14454 {
14455 fip->fields.emplace_back ();
14456 new_field = &fip->fields.back ();
14457 }
14458
14459 new_field->offset = die->sect_off;
14460
14461 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14462 if (attr != nullptr)
14463 new_field->accessibility = DW_UNSND (attr);
14464 else
14465 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14466 if (new_field->accessibility != DW_ACCESS_public)
14467 fip->non_public_fields = 1;
14468
14469 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14470 if (attr != nullptr)
14471 new_field->virtuality = DW_UNSND (attr);
14472 else
14473 new_field->virtuality = DW_VIRTUALITY_none;
14474
14475 fp = &new_field->field;
14476
14477 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14478 {
14479 /* Data member other than a C++ static data member. */
14480
14481 /* Get type of field. */
14482 fp->type = die_type (die, cu);
14483
14484 SET_FIELD_BITPOS (*fp, 0);
14485
14486 /* Get bit size of field (zero if none). */
14487 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14488 if (attr != nullptr)
14489 {
14490 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14491 }
14492 else
14493 {
14494 FIELD_BITSIZE (*fp) = 0;
14495 }
14496
14497 /* Get bit offset of field. */
14498 handle_data_member_location (die, cu, fp);
14499 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14500 if (attr != nullptr)
14501 {
14502 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14503 {
14504 /* For big endian bits, the DW_AT_bit_offset gives the
14505 additional bit offset from the MSB of the containing
14506 anonymous object to the MSB of the field. We don't
14507 have to do anything special since we don't need to
14508 know the size of the anonymous object. */
14509 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14510 }
14511 else
14512 {
14513 /* For little endian bits, compute the bit offset to the
14514 MSB of the anonymous object, subtract off the number of
14515 bits from the MSB of the field to the MSB of the
14516 object, and then subtract off the number of bits of
14517 the field itself. The result is the bit offset of
14518 the LSB of the field. */
14519 int anonymous_size;
14520 int bit_offset = DW_UNSND (attr);
14521
14522 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14523 if (attr != nullptr)
14524 {
14525 /* The size of the anonymous object containing
14526 the bit field is explicit, so use the
14527 indicated size (in bytes). */
14528 anonymous_size = DW_UNSND (attr);
14529 }
14530 else
14531 {
14532 /* The size of the anonymous object containing
14533 the bit field must be inferred from the type
14534 attribute of the data member containing the
14535 bit field. */
14536 anonymous_size = TYPE_LENGTH (fp->type);
14537 }
14538 SET_FIELD_BITPOS (*fp,
14539 (FIELD_BITPOS (*fp)
14540 + anonymous_size * bits_per_byte
14541 - bit_offset - FIELD_BITSIZE (*fp)));
14542 }
14543 }
14544 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14545 if (attr != NULL)
14546 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14547 + attr->constant_value (0)));
14548
14549 /* Get name of field. */
14550 fieldname = dwarf2_name (die, cu);
14551 if (fieldname == NULL)
14552 fieldname = "";
14553
14554 /* The name is already allocated along with this objfile, so we don't
14555 need to duplicate it for the type. */
14556 fp->name = fieldname;
14557
14558 /* Change accessibility for artificial fields (e.g. virtual table
14559 pointer or virtual base class pointer) to private. */
14560 if (dwarf2_attr (die, DW_AT_artificial, cu))
14561 {
14562 FIELD_ARTIFICIAL (*fp) = 1;
14563 new_field->accessibility = DW_ACCESS_private;
14564 fip->non_public_fields = 1;
14565 }
14566 }
14567 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14568 {
14569 /* C++ static member. */
14570
14571 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14572 is a declaration, but all versions of G++ as of this writing
14573 (so through at least 3.2.1) incorrectly generate
14574 DW_TAG_variable tags. */
14575
14576 const char *physname;
14577
14578 /* Get name of field. */
14579 fieldname = dwarf2_name (die, cu);
14580 if (fieldname == NULL)
14581 return;
14582
14583 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14584 if (attr
14585 /* Only create a symbol if this is an external value.
14586 new_symbol checks this and puts the value in the global symbol
14587 table, which we want. If it is not external, new_symbol
14588 will try to put the value in cu->list_in_scope which is wrong. */
14589 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14590 {
14591 /* A static const member, not much different than an enum as far as
14592 we're concerned, except that we can support more types. */
14593 new_symbol (die, NULL, cu);
14594 }
14595
14596 /* Get physical name. */
14597 physname = dwarf2_physname (fieldname, die, cu);
14598
14599 /* The name is already allocated along with this objfile, so we don't
14600 need to duplicate it for the type. */
14601 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14602 FIELD_TYPE (*fp) = die_type (die, cu);
14603 FIELD_NAME (*fp) = fieldname;
14604 }
14605 else if (die->tag == DW_TAG_inheritance)
14606 {
14607 /* C++ base class field. */
14608 handle_data_member_location (die, cu, fp);
14609 FIELD_BITSIZE (*fp) = 0;
14610 FIELD_TYPE (*fp) = die_type (die, cu);
14611 FIELD_NAME (*fp) = fp->type->name ();
14612 }
14613 else
14614 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14615 }
14616
14617 /* Can the type given by DIE define another type? */
14618
14619 static bool
14620 type_can_define_types (const struct die_info *die)
14621 {
14622 switch (die->tag)
14623 {
14624 case DW_TAG_typedef:
14625 case DW_TAG_class_type:
14626 case DW_TAG_structure_type:
14627 case DW_TAG_union_type:
14628 case DW_TAG_enumeration_type:
14629 return true;
14630
14631 default:
14632 return false;
14633 }
14634 }
14635
14636 /* Add a type definition defined in the scope of the FIP's class. */
14637
14638 static void
14639 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14640 struct dwarf2_cu *cu)
14641 {
14642 struct decl_field fp;
14643 memset (&fp, 0, sizeof (fp));
14644
14645 gdb_assert (type_can_define_types (die));
14646
14647 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14648 fp.name = dwarf2_name (die, cu);
14649 fp.type = read_type_die (die, cu);
14650
14651 /* Save accessibility. */
14652 enum dwarf_access_attribute accessibility;
14653 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14654 if (attr != NULL)
14655 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14656 else
14657 accessibility = dwarf2_default_access_attribute (die, cu);
14658 switch (accessibility)
14659 {
14660 case DW_ACCESS_public:
14661 /* The assumed value if neither private nor protected. */
14662 break;
14663 case DW_ACCESS_private:
14664 fp.is_private = 1;
14665 break;
14666 case DW_ACCESS_protected:
14667 fp.is_protected = 1;
14668 break;
14669 default:
14670 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14671 }
14672
14673 if (die->tag == DW_TAG_typedef)
14674 fip->typedef_field_list.push_back (fp);
14675 else
14676 fip->nested_types_list.push_back (fp);
14677 }
14678
14679 /* A convenience typedef that's used when finding the discriminant
14680 field for a variant part. */
14681 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14682 offset_map_type;
14683
14684 /* Compute the discriminant range for a given variant. OBSTACK is
14685 where the results will be stored. VARIANT is the variant to
14686 process. IS_UNSIGNED indicates whether the discriminant is signed
14687 or unsigned. */
14688
14689 static const gdb::array_view<discriminant_range>
14690 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14691 bool is_unsigned)
14692 {
14693 std::vector<discriminant_range> ranges;
14694
14695 if (variant.default_branch)
14696 return {};
14697
14698 if (variant.discr_list_data == nullptr)
14699 {
14700 discriminant_range r
14701 = {variant.discriminant_value, variant.discriminant_value};
14702 ranges.push_back (r);
14703 }
14704 else
14705 {
14706 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14707 variant.discr_list_data->size);
14708 while (!data.empty ())
14709 {
14710 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14711 {
14712 complaint (_("invalid discriminant marker: %d"), data[0]);
14713 break;
14714 }
14715 bool is_range = data[0] == DW_DSC_range;
14716 data = data.slice (1);
14717
14718 ULONGEST low, high;
14719 unsigned int bytes_read;
14720
14721 if (data.empty ())
14722 {
14723 complaint (_("DW_AT_discr_list missing low value"));
14724 break;
14725 }
14726 if (is_unsigned)
14727 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14728 else
14729 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14730 &bytes_read);
14731 data = data.slice (bytes_read);
14732
14733 if (is_range)
14734 {
14735 if (data.empty ())
14736 {
14737 complaint (_("DW_AT_discr_list missing high value"));
14738 break;
14739 }
14740 if (is_unsigned)
14741 high = read_unsigned_leb128 (nullptr, data.data (),
14742 &bytes_read);
14743 else
14744 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14745 &bytes_read);
14746 data = data.slice (bytes_read);
14747 }
14748 else
14749 high = low;
14750
14751 ranges.push_back ({ low, high });
14752 }
14753 }
14754
14755 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14756 ranges.size ());
14757 std::copy (ranges.begin (), ranges.end (), result);
14758 return gdb::array_view<discriminant_range> (result, ranges.size ());
14759 }
14760
14761 static const gdb::array_view<variant_part> create_variant_parts
14762 (struct obstack *obstack,
14763 const offset_map_type &offset_map,
14764 struct field_info *fi,
14765 const std::vector<variant_part_builder> &variant_parts);
14766
14767 /* Fill in a "struct variant" for a given variant field. RESULT is
14768 the variant to fill in. OBSTACK is where any needed allocations
14769 will be done. OFFSET_MAP holds the mapping from section offsets to
14770 fields for the type. FI describes the fields of the type we're
14771 processing. FIELD is the variant field we're converting. */
14772
14773 static void
14774 create_one_variant (variant &result, struct obstack *obstack,
14775 const offset_map_type &offset_map,
14776 struct field_info *fi, const variant_field &field)
14777 {
14778 result.discriminants = convert_variant_range (obstack, field, false);
14779 result.first_field = field.first_field + fi->baseclasses.size ();
14780 result.last_field = field.last_field + fi->baseclasses.size ();
14781 result.parts = create_variant_parts (obstack, offset_map, fi,
14782 field.variant_parts);
14783 }
14784
14785 /* Fill in a "struct variant_part" for a given variant part. RESULT
14786 is the variant part to fill in. OBSTACK is where any needed
14787 allocations will be done. OFFSET_MAP holds the mapping from
14788 section offsets to fields for the type. FI describes the fields of
14789 the type we're processing. BUILDER is the variant part to be
14790 converted. */
14791
14792 static void
14793 create_one_variant_part (variant_part &result,
14794 struct obstack *obstack,
14795 const offset_map_type &offset_map,
14796 struct field_info *fi,
14797 const variant_part_builder &builder)
14798 {
14799 auto iter = offset_map.find (builder.discriminant_offset);
14800 if (iter == offset_map.end ())
14801 {
14802 result.discriminant_index = -1;
14803 /* Doesn't matter. */
14804 result.is_unsigned = false;
14805 }
14806 else
14807 {
14808 result.discriminant_index = iter->second;
14809 result.is_unsigned
14810 = TYPE_UNSIGNED (FIELD_TYPE
14811 (fi->fields[result.discriminant_index].field));
14812 }
14813
14814 size_t n = builder.variants.size ();
14815 variant *output = new (obstack) variant[n];
14816 for (size_t i = 0; i < n; ++i)
14817 create_one_variant (output[i], obstack, offset_map, fi,
14818 builder.variants[i]);
14819
14820 result.variants = gdb::array_view<variant> (output, n);
14821 }
14822
14823 /* Create a vector of variant parts that can be attached to a type.
14824 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14825 holds the mapping from section offsets to fields for the type. FI
14826 describes the fields of the type we're processing. VARIANT_PARTS
14827 is the vector to convert. */
14828
14829 static const gdb::array_view<variant_part>
14830 create_variant_parts (struct obstack *obstack,
14831 const offset_map_type &offset_map,
14832 struct field_info *fi,
14833 const std::vector<variant_part_builder> &variant_parts)
14834 {
14835 if (variant_parts.empty ())
14836 return {};
14837
14838 size_t n = variant_parts.size ();
14839 variant_part *result = new (obstack) variant_part[n];
14840 for (size_t i = 0; i < n; ++i)
14841 create_one_variant_part (result[i], obstack, offset_map, fi,
14842 variant_parts[i]);
14843
14844 return gdb::array_view<variant_part> (result, n);
14845 }
14846
14847 /* Compute the variant part vector for FIP, attaching it to TYPE when
14848 done. */
14849
14850 static void
14851 add_variant_property (struct field_info *fip, struct type *type,
14852 struct dwarf2_cu *cu)
14853 {
14854 /* Map section offsets of fields to their field index. Note the
14855 field index here does not take the number of baseclasses into
14856 account. */
14857 offset_map_type offset_map;
14858 for (int i = 0; i < fip->fields.size (); ++i)
14859 offset_map[fip->fields[i].offset] = i;
14860
14861 struct objfile *objfile = cu->per_objfile->objfile;
14862 gdb::array_view<variant_part> parts
14863 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14864 fip->variant_parts);
14865
14866 struct dynamic_prop prop;
14867 prop.kind = PROP_VARIANT_PARTS;
14868 prop.data.variant_parts
14869 = ((gdb::array_view<variant_part> *)
14870 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14871
14872 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14873 }
14874
14875 /* Create the vector of fields, and attach it to the type. */
14876
14877 static void
14878 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14879 struct dwarf2_cu *cu)
14880 {
14881 int nfields = fip->nfields ();
14882
14883 /* Record the field count, allocate space for the array of fields,
14884 and create blank accessibility bitfields if necessary. */
14885 type->set_num_fields (nfields);
14886 type->set_fields
14887 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14888
14889 if (fip->non_public_fields && cu->language != language_ada)
14890 {
14891 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14892
14893 TYPE_FIELD_PRIVATE_BITS (type) =
14894 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14895 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14896
14897 TYPE_FIELD_PROTECTED_BITS (type) =
14898 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14899 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14900
14901 TYPE_FIELD_IGNORE_BITS (type) =
14902 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14903 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14904 }
14905
14906 /* If the type has baseclasses, allocate and clear a bit vector for
14907 TYPE_FIELD_VIRTUAL_BITS. */
14908 if (!fip->baseclasses.empty () && cu->language != language_ada)
14909 {
14910 int num_bytes = B_BYTES (fip->baseclasses.size ());
14911 unsigned char *pointer;
14912
14913 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14914 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14915 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14916 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14917 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14918 }
14919
14920 if (!fip->variant_parts.empty ())
14921 add_variant_property (fip, type, cu);
14922
14923 /* Copy the saved-up fields into the field vector. */
14924 for (int i = 0; i < nfields; ++i)
14925 {
14926 struct nextfield &field
14927 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14928 : fip->fields[i - fip->baseclasses.size ()]);
14929
14930 type->field (i) = field.field;
14931 switch (field.accessibility)
14932 {
14933 case DW_ACCESS_private:
14934 if (cu->language != language_ada)
14935 SET_TYPE_FIELD_PRIVATE (type, i);
14936 break;
14937
14938 case DW_ACCESS_protected:
14939 if (cu->language != language_ada)
14940 SET_TYPE_FIELD_PROTECTED (type, i);
14941 break;
14942
14943 case DW_ACCESS_public:
14944 break;
14945
14946 default:
14947 /* Unknown accessibility. Complain and treat it as public. */
14948 {
14949 complaint (_("unsupported accessibility %d"),
14950 field.accessibility);
14951 }
14952 break;
14953 }
14954 if (i < fip->baseclasses.size ())
14955 {
14956 switch (field.virtuality)
14957 {
14958 case DW_VIRTUALITY_virtual:
14959 case DW_VIRTUALITY_pure_virtual:
14960 if (cu->language == language_ada)
14961 error (_("unexpected virtuality in component of Ada type"));
14962 SET_TYPE_FIELD_VIRTUAL (type, i);
14963 break;
14964 }
14965 }
14966 }
14967 }
14968
14969 /* Return true if this member function is a constructor, false
14970 otherwise. */
14971
14972 static int
14973 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14974 {
14975 const char *fieldname;
14976 const char *type_name;
14977 int len;
14978
14979 if (die->parent == NULL)
14980 return 0;
14981
14982 if (die->parent->tag != DW_TAG_structure_type
14983 && die->parent->tag != DW_TAG_union_type
14984 && die->parent->tag != DW_TAG_class_type)
14985 return 0;
14986
14987 fieldname = dwarf2_name (die, cu);
14988 type_name = dwarf2_name (die->parent, cu);
14989 if (fieldname == NULL || type_name == NULL)
14990 return 0;
14991
14992 len = strlen (fieldname);
14993 return (strncmp (fieldname, type_name, len) == 0
14994 && (type_name[len] == '\0' || type_name[len] == '<'));
14995 }
14996
14997 /* Check if the given VALUE is a recognized enum
14998 dwarf_defaulted_attribute constant according to DWARF5 spec,
14999 Table 7.24. */
15000
15001 static bool
15002 is_valid_DW_AT_defaulted (ULONGEST value)
15003 {
15004 switch (value)
15005 {
15006 case DW_DEFAULTED_no:
15007 case DW_DEFAULTED_in_class:
15008 case DW_DEFAULTED_out_of_class:
15009 return true;
15010 }
15011
15012 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15013 return false;
15014 }
15015
15016 /* Add a member function to the proper fieldlist. */
15017
15018 static void
15019 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15020 struct type *type, struct dwarf2_cu *cu)
15021 {
15022 struct objfile *objfile = cu->per_objfile->objfile;
15023 struct attribute *attr;
15024 int i;
15025 struct fnfieldlist *flp = nullptr;
15026 struct fn_field *fnp;
15027 const char *fieldname;
15028 struct type *this_type;
15029 enum dwarf_access_attribute accessibility;
15030
15031 if (cu->language == language_ada)
15032 error (_("unexpected member function in Ada type"));
15033
15034 /* Get name of member function. */
15035 fieldname = dwarf2_name (die, cu);
15036 if (fieldname == NULL)
15037 return;
15038
15039 /* Look up member function name in fieldlist. */
15040 for (i = 0; i < fip->fnfieldlists.size (); i++)
15041 {
15042 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15043 {
15044 flp = &fip->fnfieldlists[i];
15045 break;
15046 }
15047 }
15048
15049 /* Create a new fnfieldlist if necessary. */
15050 if (flp == nullptr)
15051 {
15052 fip->fnfieldlists.emplace_back ();
15053 flp = &fip->fnfieldlists.back ();
15054 flp->name = fieldname;
15055 i = fip->fnfieldlists.size () - 1;
15056 }
15057
15058 /* Create a new member function field and add it to the vector of
15059 fnfieldlists. */
15060 flp->fnfields.emplace_back ();
15061 fnp = &flp->fnfields.back ();
15062
15063 /* Delay processing of the physname until later. */
15064 if (cu->language == language_cplus)
15065 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15066 die, cu);
15067 else
15068 {
15069 const char *physname = dwarf2_physname (fieldname, die, cu);
15070 fnp->physname = physname ? physname : "";
15071 }
15072
15073 fnp->type = alloc_type (objfile);
15074 this_type = read_type_die (die, cu);
15075 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15076 {
15077 int nparams = this_type->num_fields ();
15078
15079 /* TYPE is the domain of this method, and THIS_TYPE is the type
15080 of the method itself (TYPE_CODE_METHOD). */
15081 smash_to_method_type (fnp->type, type,
15082 TYPE_TARGET_TYPE (this_type),
15083 this_type->fields (),
15084 this_type->num_fields (),
15085 TYPE_VARARGS (this_type));
15086
15087 /* Handle static member functions.
15088 Dwarf2 has no clean way to discern C++ static and non-static
15089 member functions. G++ helps GDB by marking the first
15090 parameter for non-static member functions (which is the this
15091 pointer) as artificial. We obtain this information from
15092 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15093 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15094 fnp->voffset = VOFFSET_STATIC;
15095 }
15096 else
15097 complaint (_("member function type missing for '%s'"),
15098 dwarf2_full_name (fieldname, die, cu));
15099
15100 /* Get fcontext from DW_AT_containing_type if present. */
15101 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15102 fnp->fcontext = die_containing_type (die, cu);
15103
15104 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15105 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15106
15107 /* Get accessibility. */
15108 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15109 if (attr != nullptr)
15110 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15111 else
15112 accessibility = dwarf2_default_access_attribute (die, cu);
15113 switch (accessibility)
15114 {
15115 case DW_ACCESS_private:
15116 fnp->is_private = 1;
15117 break;
15118 case DW_ACCESS_protected:
15119 fnp->is_protected = 1;
15120 break;
15121 }
15122
15123 /* Check for artificial methods. */
15124 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15125 if (attr && DW_UNSND (attr) != 0)
15126 fnp->is_artificial = 1;
15127
15128 /* Check for defaulted methods. */
15129 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15130 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15131 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15132
15133 /* Check for deleted methods. */
15134 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15135 if (attr != nullptr && DW_UNSND (attr) != 0)
15136 fnp->is_deleted = 1;
15137
15138 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15139
15140 /* Get index in virtual function table if it is a virtual member
15141 function. For older versions of GCC, this is an offset in the
15142 appropriate virtual table, as specified by DW_AT_containing_type.
15143 For everyone else, it is an expression to be evaluated relative
15144 to the object address. */
15145
15146 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15147 if (attr != nullptr)
15148 {
15149 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15150 {
15151 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15152 {
15153 /* Old-style GCC. */
15154 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15155 }
15156 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15157 || (DW_BLOCK (attr)->size > 1
15158 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15159 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15160 {
15161 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15162 if ((fnp->voffset % cu->header.addr_size) != 0)
15163 dwarf2_complex_location_expr_complaint ();
15164 else
15165 fnp->voffset /= cu->header.addr_size;
15166 fnp->voffset += 2;
15167 }
15168 else
15169 dwarf2_complex_location_expr_complaint ();
15170
15171 if (!fnp->fcontext)
15172 {
15173 /* If there is no `this' field and no DW_AT_containing_type,
15174 we cannot actually find a base class context for the
15175 vtable! */
15176 if (this_type->num_fields () == 0
15177 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15178 {
15179 complaint (_("cannot determine context for virtual member "
15180 "function \"%s\" (offset %s)"),
15181 fieldname, sect_offset_str (die->sect_off));
15182 }
15183 else
15184 {
15185 fnp->fcontext
15186 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15187 }
15188 }
15189 }
15190 else if (attr->form_is_section_offset ())
15191 {
15192 dwarf2_complex_location_expr_complaint ();
15193 }
15194 else
15195 {
15196 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15197 fieldname);
15198 }
15199 }
15200 else
15201 {
15202 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15203 if (attr && DW_UNSND (attr))
15204 {
15205 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15206 complaint (_("Member function \"%s\" (offset %s) is virtual "
15207 "but the vtable offset is not specified"),
15208 fieldname, sect_offset_str (die->sect_off));
15209 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15210 TYPE_CPLUS_DYNAMIC (type) = 1;
15211 }
15212 }
15213 }
15214
15215 /* Create the vector of member function fields, and attach it to the type. */
15216
15217 static void
15218 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15219 struct dwarf2_cu *cu)
15220 {
15221 if (cu->language == language_ada)
15222 error (_("unexpected member functions in Ada type"));
15223
15224 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15225 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15226 TYPE_ALLOC (type,
15227 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15228
15229 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15230 {
15231 struct fnfieldlist &nf = fip->fnfieldlists[i];
15232 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15233
15234 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15235 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15236 fn_flp->fn_fields = (struct fn_field *)
15237 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15238
15239 for (int k = 0; k < nf.fnfields.size (); ++k)
15240 fn_flp->fn_fields[k] = nf.fnfields[k];
15241 }
15242
15243 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15244 }
15245
15246 /* Returns non-zero if NAME is the name of a vtable member in CU's
15247 language, zero otherwise. */
15248 static int
15249 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15250 {
15251 static const char vptr[] = "_vptr";
15252
15253 /* Look for the C++ form of the vtable. */
15254 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15255 return 1;
15256
15257 return 0;
15258 }
15259
15260 /* GCC outputs unnamed structures that are really pointers to member
15261 functions, with the ABI-specified layout. If TYPE describes
15262 such a structure, smash it into a member function type.
15263
15264 GCC shouldn't do this; it should just output pointer to member DIEs.
15265 This is GCC PR debug/28767. */
15266
15267 static void
15268 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15269 {
15270 struct type *pfn_type, *self_type, *new_type;
15271
15272 /* Check for a structure with no name and two children. */
15273 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15274 return;
15275
15276 /* Check for __pfn and __delta members. */
15277 if (TYPE_FIELD_NAME (type, 0) == NULL
15278 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15279 || TYPE_FIELD_NAME (type, 1) == NULL
15280 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15281 return;
15282
15283 /* Find the type of the method. */
15284 pfn_type = TYPE_FIELD_TYPE (type, 0);
15285 if (pfn_type == NULL
15286 || pfn_type->code () != TYPE_CODE_PTR
15287 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15288 return;
15289
15290 /* Look for the "this" argument. */
15291 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15292 if (pfn_type->num_fields () == 0
15293 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15294 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
15295 return;
15296
15297 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15298 new_type = alloc_type (objfile);
15299 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15300 pfn_type->fields (), pfn_type->num_fields (),
15301 TYPE_VARARGS (pfn_type));
15302 smash_to_methodptr_type (type, new_type);
15303 }
15304
15305 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15306 appropriate error checking and issuing complaints if there is a
15307 problem. */
15308
15309 static ULONGEST
15310 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15311 {
15312 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15313
15314 if (attr == nullptr)
15315 return 0;
15316
15317 if (!attr->form_is_constant ())
15318 {
15319 complaint (_("DW_AT_alignment must have constant form"
15320 " - DIE at %s [in module %s]"),
15321 sect_offset_str (die->sect_off),
15322 objfile_name (cu->per_objfile->objfile));
15323 return 0;
15324 }
15325
15326 ULONGEST align;
15327 if (attr->form == DW_FORM_sdata)
15328 {
15329 LONGEST val = DW_SND (attr);
15330 if (val < 0)
15331 {
15332 complaint (_("DW_AT_alignment value must not be negative"
15333 " - DIE at %s [in module %s]"),
15334 sect_offset_str (die->sect_off),
15335 objfile_name (cu->per_objfile->objfile));
15336 return 0;
15337 }
15338 align = val;
15339 }
15340 else
15341 align = DW_UNSND (attr);
15342
15343 if (align == 0)
15344 {
15345 complaint (_("DW_AT_alignment value must not be zero"
15346 " - DIE at %s [in module %s]"),
15347 sect_offset_str (die->sect_off),
15348 objfile_name (cu->per_objfile->objfile));
15349 return 0;
15350 }
15351 if ((align & (align - 1)) != 0)
15352 {
15353 complaint (_("DW_AT_alignment value must be a power of 2"
15354 " - DIE at %s [in module %s]"),
15355 sect_offset_str (die->sect_off),
15356 objfile_name (cu->per_objfile->objfile));
15357 return 0;
15358 }
15359
15360 return align;
15361 }
15362
15363 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15364 the alignment for TYPE. */
15365
15366 static void
15367 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15368 struct type *type)
15369 {
15370 if (!set_type_align (type, get_alignment (cu, die)))
15371 complaint (_("DW_AT_alignment value too large"
15372 " - DIE at %s [in module %s]"),
15373 sect_offset_str (die->sect_off),
15374 objfile_name (cu->per_objfile->objfile));
15375 }
15376
15377 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15378 constant for a type, according to DWARF5 spec, Table 5.5. */
15379
15380 static bool
15381 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15382 {
15383 switch (value)
15384 {
15385 case DW_CC_normal:
15386 case DW_CC_pass_by_reference:
15387 case DW_CC_pass_by_value:
15388 return true;
15389
15390 default:
15391 complaint (_("unrecognized DW_AT_calling_convention value "
15392 "(%s) for a type"), pulongest (value));
15393 return false;
15394 }
15395 }
15396
15397 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15398 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15399 also according to GNU-specific values (see include/dwarf2.h). */
15400
15401 static bool
15402 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15403 {
15404 switch (value)
15405 {
15406 case DW_CC_normal:
15407 case DW_CC_program:
15408 case DW_CC_nocall:
15409 return true;
15410
15411 case DW_CC_GNU_renesas_sh:
15412 case DW_CC_GNU_borland_fastcall_i386:
15413 case DW_CC_GDB_IBM_OpenCL:
15414 return true;
15415
15416 default:
15417 complaint (_("unrecognized DW_AT_calling_convention value "
15418 "(%s) for a subroutine"), pulongest (value));
15419 return false;
15420 }
15421 }
15422
15423 /* Called when we find the DIE that starts a structure or union scope
15424 (definition) to create a type for the structure or union. Fill in
15425 the type's name and general properties; the members will not be
15426 processed until process_structure_scope. A symbol table entry for
15427 the type will also not be done until process_structure_scope (assuming
15428 the type has a name).
15429
15430 NOTE: we need to call these functions regardless of whether or not the
15431 DIE has a DW_AT_name attribute, since it might be an anonymous
15432 structure or union. This gets the type entered into our set of
15433 user defined types. */
15434
15435 static struct type *
15436 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15437 {
15438 struct objfile *objfile = cu->per_objfile->objfile;
15439 struct type *type;
15440 struct attribute *attr;
15441 const char *name;
15442
15443 /* If the definition of this type lives in .debug_types, read that type.
15444 Don't follow DW_AT_specification though, that will take us back up
15445 the chain and we want to go down. */
15446 attr = die->attr (DW_AT_signature);
15447 if (attr != nullptr)
15448 {
15449 type = get_DW_AT_signature_type (die, attr, cu);
15450
15451 /* The type's CU may not be the same as CU.
15452 Ensure TYPE is recorded with CU in die_type_hash. */
15453 return set_die_type (die, type, cu);
15454 }
15455
15456 type = alloc_type (objfile);
15457 INIT_CPLUS_SPECIFIC (type);
15458
15459 name = dwarf2_name (die, cu);
15460 if (name != NULL)
15461 {
15462 if (cu->language == language_cplus
15463 || cu->language == language_d
15464 || cu->language == language_rust)
15465 {
15466 const char *full_name = dwarf2_full_name (name, die, cu);
15467
15468 /* dwarf2_full_name might have already finished building the DIE's
15469 type. If so, there is no need to continue. */
15470 if (get_die_type (die, cu) != NULL)
15471 return get_die_type (die, cu);
15472
15473 type->set_name (full_name);
15474 }
15475 else
15476 {
15477 /* The name is already allocated along with this objfile, so
15478 we don't need to duplicate it for the type. */
15479 type->set_name (name);
15480 }
15481 }
15482
15483 if (die->tag == DW_TAG_structure_type)
15484 {
15485 type->set_code (TYPE_CODE_STRUCT);
15486 }
15487 else if (die->tag == DW_TAG_union_type)
15488 {
15489 type->set_code (TYPE_CODE_UNION);
15490 }
15491 else
15492 {
15493 type->set_code (TYPE_CODE_STRUCT);
15494 }
15495
15496 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15497 TYPE_DECLARED_CLASS (type) = 1;
15498
15499 /* Store the calling convention in the type if it's available in
15500 the die. Otherwise the calling convention remains set to
15501 the default value DW_CC_normal. */
15502 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15503 if (attr != nullptr
15504 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15505 {
15506 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15507 TYPE_CPLUS_CALLING_CONVENTION (type)
15508 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15509 }
15510
15511 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15512 if (attr != nullptr)
15513 {
15514 if (attr->form_is_constant ())
15515 TYPE_LENGTH (type) = DW_UNSND (attr);
15516 else
15517 {
15518 struct dynamic_prop prop;
15519 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15520 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15521 TYPE_LENGTH (type) = 0;
15522 }
15523 }
15524 else
15525 {
15526 TYPE_LENGTH (type) = 0;
15527 }
15528
15529 maybe_set_alignment (cu, die, type);
15530
15531 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15532 {
15533 /* ICC<14 does not output the required DW_AT_declaration on
15534 incomplete types, but gives them a size of zero. */
15535 TYPE_STUB (type) = 1;
15536 }
15537 else
15538 TYPE_STUB_SUPPORTED (type) = 1;
15539
15540 if (die_is_declaration (die, cu))
15541 TYPE_STUB (type) = 1;
15542 else if (attr == NULL && die->child == NULL
15543 && producer_is_realview (cu->producer))
15544 /* RealView does not output the required DW_AT_declaration
15545 on incomplete types. */
15546 TYPE_STUB (type) = 1;
15547
15548 /* We need to add the type field to the die immediately so we don't
15549 infinitely recurse when dealing with pointers to the structure
15550 type within the structure itself. */
15551 set_die_type (die, type, cu);
15552
15553 /* set_die_type should be already done. */
15554 set_descriptive_type (type, die, cu);
15555
15556 return type;
15557 }
15558
15559 static void handle_struct_member_die
15560 (struct die_info *child_die,
15561 struct type *type,
15562 struct field_info *fi,
15563 std::vector<struct symbol *> *template_args,
15564 struct dwarf2_cu *cu);
15565
15566 /* A helper for handle_struct_member_die that handles
15567 DW_TAG_variant_part. */
15568
15569 static void
15570 handle_variant_part (struct die_info *die, struct type *type,
15571 struct field_info *fi,
15572 std::vector<struct symbol *> *template_args,
15573 struct dwarf2_cu *cu)
15574 {
15575 variant_part_builder *new_part;
15576 if (fi->current_variant_part == nullptr)
15577 {
15578 fi->variant_parts.emplace_back ();
15579 new_part = &fi->variant_parts.back ();
15580 }
15581 else if (!fi->current_variant_part->processing_variant)
15582 {
15583 complaint (_("nested DW_TAG_variant_part seen "
15584 "- DIE at %s [in module %s]"),
15585 sect_offset_str (die->sect_off),
15586 objfile_name (cu->per_objfile->objfile));
15587 return;
15588 }
15589 else
15590 {
15591 variant_field &current = fi->current_variant_part->variants.back ();
15592 current.variant_parts.emplace_back ();
15593 new_part = &current.variant_parts.back ();
15594 }
15595
15596 /* When we recurse, we want callees to add to this new variant
15597 part. */
15598 scoped_restore save_current_variant_part
15599 = make_scoped_restore (&fi->current_variant_part, new_part);
15600
15601 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15602 if (discr == NULL)
15603 {
15604 /* It's a univariant form, an extension we support. */
15605 }
15606 else if (discr->form_is_ref ())
15607 {
15608 struct dwarf2_cu *target_cu = cu;
15609 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15610
15611 new_part->discriminant_offset = target_die->sect_off;
15612 }
15613 else
15614 {
15615 complaint (_("DW_AT_discr does not have DIE reference form"
15616 " - DIE at %s [in module %s]"),
15617 sect_offset_str (die->sect_off),
15618 objfile_name (cu->per_objfile->objfile));
15619 }
15620
15621 for (die_info *child_die = die->child;
15622 child_die != NULL;
15623 child_die = child_die->sibling)
15624 handle_struct_member_die (child_die, type, fi, template_args, cu);
15625 }
15626
15627 /* A helper for handle_struct_member_die that handles
15628 DW_TAG_variant. */
15629
15630 static void
15631 handle_variant (struct die_info *die, struct type *type,
15632 struct field_info *fi,
15633 std::vector<struct symbol *> *template_args,
15634 struct dwarf2_cu *cu)
15635 {
15636 if (fi->current_variant_part == nullptr)
15637 {
15638 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15639 "- DIE at %s [in module %s]"),
15640 sect_offset_str (die->sect_off),
15641 objfile_name (cu->per_objfile->objfile));
15642 return;
15643 }
15644 if (fi->current_variant_part->processing_variant)
15645 {
15646 complaint (_("nested DW_TAG_variant seen "
15647 "- DIE at %s [in module %s]"),
15648 sect_offset_str (die->sect_off),
15649 objfile_name (cu->per_objfile->objfile));
15650 return;
15651 }
15652
15653 scoped_restore save_processing_variant
15654 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15655 true);
15656
15657 fi->current_variant_part->variants.emplace_back ();
15658 variant_field &variant = fi->current_variant_part->variants.back ();
15659 variant.first_field = fi->fields.size ();
15660
15661 /* In a variant we want to get the discriminant and also add a
15662 field for our sole member child. */
15663 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15664 if (discr == nullptr)
15665 {
15666 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15667 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15668 variant.default_branch = true;
15669 else
15670 variant.discr_list_data = DW_BLOCK (discr);
15671 }
15672 else
15673 variant.discriminant_value = DW_UNSND (discr);
15674
15675 for (die_info *variant_child = die->child;
15676 variant_child != NULL;
15677 variant_child = variant_child->sibling)
15678 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15679
15680 variant.last_field = fi->fields.size ();
15681 }
15682
15683 /* A helper for process_structure_scope that handles a single member
15684 DIE. */
15685
15686 static void
15687 handle_struct_member_die (struct die_info *child_die, struct type *type,
15688 struct field_info *fi,
15689 std::vector<struct symbol *> *template_args,
15690 struct dwarf2_cu *cu)
15691 {
15692 if (child_die->tag == DW_TAG_member
15693 || child_die->tag == DW_TAG_variable)
15694 {
15695 /* NOTE: carlton/2002-11-05: A C++ static data member
15696 should be a DW_TAG_member that is a declaration, but
15697 all versions of G++ as of this writing (so through at
15698 least 3.2.1) incorrectly generate DW_TAG_variable
15699 tags for them instead. */
15700 dwarf2_add_field (fi, child_die, cu);
15701 }
15702 else if (child_die->tag == DW_TAG_subprogram)
15703 {
15704 /* Rust doesn't have member functions in the C++ sense.
15705 However, it does emit ordinary functions as children
15706 of a struct DIE. */
15707 if (cu->language == language_rust)
15708 read_func_scope (child_die, cu);
15709 else
15710 {
15711 /* C++ member function. */
15712 dwarf2_add_member_fn (fi, child_die, type, cu);
15713 }
15714 }
15715 else if (child_die->tag == DW_TAG_inheritance)
15716 {
15717 /* C++ base class field. */
15718 dwarf2_add_field (fi, child_die, cu);
15719 }
15720 else if (type_can_define_types (child_die))
15721 dwarf2_add_type_defn (fi, child_die, cu);
15722 else if (child_die->tag == DW_TAG_template_type_param
15723 || child_die->tag == DW_TAG_template_value_param)
15724 {
15725 struct symbol *arg = new_symbol (child_die, NULL, cu);
15726
15727 if (arg != NULL)
15728 template_args->push_back (arg);
15729 }
15730 else if (child_die->tag == DW_TAG_variant_part)
15731 handle_variant_part (child_die, type, fi, template_args, cu);
15732 else if (child_die->tag == DW_TAG_variant)
15733 handle_variant (child_die, type, fi, template_args, cu);
15734 }
15735
15736 /* Finish creating a structure or union type, including filling in
15737 its members and creating a symbol for it. */
15738
15739 static void
15740 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15741 {
15742 struct objfile *objfile = cu->per_objfile->objfile;
15743 struct die_info *child_die;
15744 struct type *type;
15745
15746 type = get_die_type (die, cu);
15747 if (type == NULL)
15748 type = read_structure_type (die, cu);
15749
15750 bool has_template_parameters = false;
15751 if (die->child != NULL && ! die_is_declaration (die, cu))
15752 {
15753 struct field_info fi;
15754 std::vector<struct symbol *> template_args;
15755
15756 child_die = die->child;
15757
15758 while (child_die && child_die->tag)
15759 {
15760 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15761 child_die = child_die->sibling;
15762 }
15763
15764 /* Attach template arguments to type. */
15765 if (!template_args.empty ())
15766 {
15767 has_template_parameters = true;
15768 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15769 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15770 TYPE_TEMPLATE_ARGUMENTS (type)
15771 = XOBNEWVEC (&objfile->objfile_obstack,
15772 struct symbol *,
15773 TYPE_N_TEMPLATE_ARGUMENTS (type));
15774 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15775 template_args.data (),
15776 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15777 * sizeof (struct symbol *)));
15778 }
15779
15780 /* Attach fields and member functions to the type. */
15781 if (fi.nfields () > 0)
15782 dwarf2_attach_fields_to_type (&fi, type, cu);
15783 if (!fi.fnfieldlists.empty ())
15784 {
15785 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15786
15787 /* Get the type which refers to the base class (possibly this
15788 class itself) which contains the vtable pointer for the current
15789 class from the DW_AT_containing_type attribute. This use of
15790 DW_AT_containing_type is a GNU extension. */
15791
15792 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15793 {
15794 struct type *t = die_containing_type (die, cu);
15795
15796 set_type_vptr_basetype (type, t);
15797 if (type == t)
15798 {
15799 int i;
15800
15801 /* Our own class provides vtbl ptr. */
15802 for (i = t->num_fields () - 1;
15803 i >= TYPE_N_BASECLASSES (t);
15804 --i)
15805 {
15806 const char *fieldname = TYPE_FIELD_NAME (t, i);
15807
15808 if (is_vtable_name (fieldname, cu))
15809 {
15810 set_type_vptr_fieldno (type, i);
15811 break;
15812 }
15813 }
15814
15815 /* Complain if virtual function table field not found. */
15816 if (i < TYPE_N_BASECLASSES (t))
15817 complaint (_("virtual function table pointer "
15818 "not found when defining class '%s'"),
15819 type->name () ? type->name () : "");
15820 }
15821 else
15822 {
15823 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15824 }
15825 }
15826 else if (cu->producer
15827 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15828 {
15829 /* The IBM XLC compiler does not provide direct indication
15830 of the containing type, but the vtable pointer is
15831 always named __vfp. */
15832
15833 int i;
15834
15835 for (i = type->num_fields () - 1;
15836 i >= TYPE_N_BASECLASSES (type);
15837 --i)
15838 {
15839 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15840 {
15841 set_type_vptr_fieldno (type, i);
15842 set_type_vptr_basetype (type, type);
15843 break;
15844 }
15845 }
15846 }
15847 }
15848
15849 /* Copy fi.typedef_field_list linked list elements content into the
15850 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15851 if (!fi.typedef_field_list.empty ())
15852 {
15853 int count = fi.typedef_field_list.size ();
15854
15855 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15856 TYPE_TYPEDEF_FIELD_ARRAY (type)
15857 = ((struct decl_field *)
15858 TYPE_ALLOC (type,
15859 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15860 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15861
15862 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15863 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15864 }
15865
15866 /* Copy fi.nested_types_list linked list elements content into the
15867 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15868 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15869 {
15870 int count = fi.nested_types_list.size ();
15871
15872 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15873 TYPE_NESTED_TYPES_ARRAY (type)
15874 = ((struct decl_field *)
15875 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15876 TYPE_NESTED_TYPES_COUNT (type) = count;
15877
15878 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15879 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15880 }
15881 }
15882
15883 quirk_gcc_member_function_pointer (type, objfile);
15884 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15885 cu->rust_unions.push_back (type);
15886
15887 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15888 snapshots) has been known to create a die giving a declaration
15889 for a class that has, as a child, a die giving a definition for a
15890 nested class. So we have to process our children even if the
15891 current die is a declaration. Normally, of course, a declaration
15892 won't have any children at all. */
15893
15894 child_die = die->child;
15895
15896 while (child_die != NULL && child_die->tag)
15897 {
15898 if (child_die->tag == DW_TAG_member
15899 || child_die->tag == DW_TAG_variable
15900 || child_die->tag == DW_TAG_inheritance
15901 || child_die->tag == DW_TAG_template_value_param
15902 || child_die->tag == DW_TAG_template_type_param)
15903 {
15904 /* Do nothing. */
15905 }
15906 else
15907 process_die (child_die, cu);
15908
15909 child_die = child_die->sibling;
15910 }
15911
15912 /* Do not consider external references. According to the DWARF standard,
15913 these DIEs are identified by the fact that they have no byte_size
15914 attribute, and a declaration attribute. */
15915 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15916 || !die_is_declaration (die, cu)
15917 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15918 {
15919 struct symbol *sym = new_symbol (die, type, cu);
15920
15921 if (has_template_parameters)
15922 {
15923 struct symtab *symtab;
15924 if (sym != nullptr)
15925 symtab = symbol_symtab (sym);
15926 else if (cu->line_header != nullptr)
15927 {
15928 /* Any related symtab will do. */
15929 symtab
15930 = cu->line_header->file_names ()[0].symtab;
15931 }
15932 else
15933 {
15934 symtab = nullptr;
15935 complaint (_("could not find suitable "
15936 "symtab for template parameter"
15937 " - DIE at %s [in module %s]"),
15938 sect_offset_str (die->sect_off),
15939 objfile_name (objfile));
15940 }
15941
15942 if (symtab != nullptr)
15943 {
15944 /* Make sure that the symtab is set on the new symbols.
15945 Even though they don't appear in this symtab directly,
15946 other parts of gdb assume that symbols do, and this is
15947 reasonably true. */
15948 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15949 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15950 }
15951 }
15952 }
15953 }
15954
15955 /* Assuming DIE is an enumeration type, and TYPE is its associated
15956 type, update TYPE using some information only available in DIE's
15957 children. In particular, the fields are computed. */
15958
15959 static void
15960 update_enumeration_type_from_children (struct die_info *die,
15961 struct type *type,
15962 struct dwarf2_cu *cu)
15963 {
15964 struct die_info *child_die;
15965 int unsigned_enum = 1;
15966 int flag_enum = 1;
15967
15968 auto_obstack obstack;
15969 std::vector<struct field> fields;
15970
15971 for (child_die = die->child;
15972 child_die != NULL && child_die->tag;
15973 child_die = child_die->sibling)
15974 {
15975 struct attribute *attr;
15976 LONGEST value;
15977 const gdb_byte *bytes;
15978 struct dwarf2_locexpr_baton *baton;
15979 const char *name;
15980
15981 if (child_die->tag != DW_TAG_enumerator)
15982 continue;
15983
15984 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15985 if (attr == NULL)
15986 continue;
15987
15988 name = dwarf2_name (child_die, cu);
15989 if (name == NULL)
15990 name = "<anonymous enumerator>";
15991
15992 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15993 &value, &bytes, &baton);
15994 if (value < 0)
15995 {
15996 unsigned_enum = 0;
15997 flag_enum = 0;
15998 }
15999 else
16000 {
16001 if (count_one_bits_ll (value) >= 2)
16002 flag_enum = 0;
16003 }
16004
16005 fields.emplace_back ();
16006 struct field &field = fields.back ();
16007 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16008 SET_FIELD_ENUMVAL (field, value);
16009 }
16010
16011 if (!fields.empty ())
16012 {
16013 type->set_num_fields (fields.size ());
16014 type->set_fields
16015 ((struct field *)
16016 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16017 memcpy (type->fields (), fields.data (),
16018 sizeof (struct field) * fields.size ());
16019 }
16020
16021 if (unsigned_enum)
16022 TYPE_UNSIGNED (type) = 1;
16023 if (flag_enum)
16024 TYPE_FLAG_ENUM (type) = 1;
16025 }
16026
16027 /* Given a DW_AT_enumeration_type die, set its type. We do not
16028 complete the type's fields yet, or create any symbols. */
16029
16030 static struct type *
16031 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16032 {
16033 struct objfile *objfile = cu->per_objfile->objfile;
16034 struct type *type;
16035 struct attribute *attr;
16036 const char *name;
16037
16038 /* If the definition of this type lives in .debug_types, read that type.
16039 Don't follow DW_AT_specification though, that will take us back up
16040 the chain and we want to go down. */
16041 attr = die->attr (DW_AT_signature);
16042 if (attr != nullptr)
16043 {
16044 type = get_DW_AT_signature_type (die, attr, cu);
16045
16046 /* The type's CU may not be the same as CU.
16047 Ensure TYPE is recorded with CU in die_type_hash. */
16048 return set_die_type (die, type, cu);
16049 }
16050
16051 type = alloc_type (objfile);
16052
16053 type->set_code (TYPE_CODE_ENUM);
16054 name = dwarf2_full_name (NULL, die, cu);
16055 if (name != NULL)
16056 type->set_name (name);
16057
16058 attr = dwarf2_attr (die, DW_AT_type, cu);
16059 if (attr != NULL)
16060 {
16061 struct type *underlying_type = die_type (die, cu);
16062
16063 TYPE_TARGET_TYPE (type) = underlying_type;
16064 }
16065
16066 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16067 if (attr != nullptr)
16068 {
16069 TYPE_LENGTH (type) = DW_UNSND (attr);
16070 }
16071 else
16072 {
16073 TYPE_LENGTH (type) = 0;
16074 }
16075
16076 maybe_set_alignment (cu, die, type);
16077
16078 /* The enumeration DIE can be incomplete. In Ada, any type can be
16079 declared as private in the package spec, and then defined only
16080 inside the package body. Such types are known as Taft Amendment
16081 Types. When another package uses such a type, an incomplete DIE
16082 may be generated by the compiler. */
16083 if (die_is_declaration (die, cu))
16084 TYPE_STUB (type) = 1;
16085
16086 /* If this type has an underlying type that is not a stub, then we
16087 may use its attributes. We always use the "unsigned" attribute
16088 in this situation, because ordinarily we guess whether the type
16089 is unsigned -- but the guess can be wrong and the underlying type
16090 can tell us the reality. However, we defer to a local size
16091 attribute if one exists, because this lets the compiler override
16092 the underlying type if needed. */
16093 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16094 {
16095 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16096 underlying_type = check_typedef (underlying_type);
16097 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
16098 if (TYPE_LENGTH (type) == 0)
16099 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16100 if (TYPE_RAW_ALIGN (type) == 0
16101 && TYPE_RAW_ALIGN (underlying_type) != 0)
16102 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16103 }
16104
16105 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16106
16107 set_die_type (die, type, cu);
16108
16109 /* Finish the creation of this type by using the enum's children.
16110 Note that, as usual, this must come after set_die_type to avoid
16111 infinite recursion when trying to compute the names of the
16112 enumerators. */
16113 update_enumeration_type_from_children (die, type, cu);
16114
16115 return type;
16116 }
16117
16118 /* Given a pointer to a die which begins an enumeration, process all
16119 the dies that define the members of the enumeration, and create the
16120 symbol for the enumeration type.
16121
16122 NOTE: We reverse the order of the element list. */
16123
16124 static void
16125 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16126 {
16127 struct type *this_type;
16128
16129 this_type = get_die_type (die, cu);
16130 if (this_type == NULL)
16131 this_type = read_enumeration_type (die, cu);
16132
16133 if (die->child != NULL)
16134 {
16135 struct die_info *child_die;
16136 const char *name;
16137
16138 child_die = die->child;
16139 while (child_die && child_die->tag)
16140 {
16141 if (child_die->tag != DW_TAG_enumerator)
16142 {
16143 process_die (child_die, cu);
16144 }
16145 else
16146 {
16147 name = dwarf2_name (child_die, cu);
16148 if (name)
16149 new_symbol (child_die, this_type, cu);
16150 }
16151
16152 child_die = child_die->sibling;
16153 }
16154 }
16155
16156 /* If we are reading an enum from a .debug_types unit, and the enum
16157 is a declaration, and the enum is not the signatured type in the
16158 unit, then we do not want to add a symbol for it. Adding a
16159 symbol would in some cases obscure the true definition of the
16160 enum, giving users an incomplete type when the definition is
16161 actually available. Note that we do not want to do this for all
16162 enums which are just declarations, because C++0x allows forward
16163 enum declarations. */
16164 if (cu->per_cu->is_debug_types
16165 && die_is_declaration (die, cu))
16166 {
16167 struct signatured_type *sig_type;
16168
16169 sig_type = (struct signatured_type *) cu->per_cu;
16170 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16171 if (sig_type->type_offset_in_section != die->sect_off)
16172 return;
16173 }
16174
16175 new_symbol (die, this_type, cu);
16176 }
16177
16178 /* Extract all information from a DW_TAG_array_type DIE and put it in
16179 the DIE's type field. For now, this only handles one dimensional
16180 arrays. */
16181
16182 static struct type *
16183 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16184 {
16185 struct objfile *objfile = cu->per_objfile->objfile;
16186 struct die_info *child_die;
16187 struct type *type;
16188 struct type *element_type, *range_type, *index_type;
16189 struct attribute *attr;
16190 const char *name;
16191 struct dynamic_prop *byte_stride_prop = NULL;
16192 unsigned int bit_stride = 0;
16193
16194 element_type = die_type (die, cu);
16195
16196 /* The die_type call above may have already set the type for this DIE. */
16197 type = get_die_type (die, cu);
16198 if (type)
16199 return type;
16200
16201 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16202 if (attr != NULL)
16203 {
16204 int stride_ok;
16205 struct type *prop_type = cu->addr_sized_int_type (false);
16206
16207 byte_stride_prop
16208 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16209 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16210 prop_type);
16211 if (!stride_ok)
16212 {
16213 complaint (_("unable to read array DW_AT_byte_stride "
16214 " - DIE at %s [in module %s]"),
16215 sect_offset_str (die->sect_off),
16216 objfile_name (cu->per_objfile->objfile));
16217 /* Ignore this attribute. We will likely not be able to print
16218 arrays of this type correctly, but there is little we can do
16219 to help if we cannot read the attribute's value. */
16220 byte_stride_prop = NULL;
16221 }
16222 }
16223
16224 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16225 if (attr != NULL)
16226 bit_stride = DW_UNSND (attr);
16227
16228 /* Irix 6.2 native cc creates array types without children for
16229 arrays with unspecified length. */
16230 if (die->child == NULL)
16231 {
16232 index_type = objfile_type (objfile)->builtin_int;
16233 range_type = create_static_range_type (NULL, index_type, 0, -1);
16234 type = create_array_type_with_stride (NULL, element_type, range_type,
16235 byte_stride_prop, bit_stride);
16236 return set_die_type (die, type, cu);
16237 }
16238
16239 std::vector<struct type *> range_types;
16240 child_die = die->child;
16241 while (child_die && child_die->tag)
16242 {
16243 if (child_die->tag == DW_TAG_subrange_type)
16244 {
16245 struct type *child_type = read_type_die (child_die, cu);
16246
16247 if (child_type != NULL)
16248 {
16249 /* The range type was succesfully read. Save it for the
16250 array type creation. */
16251 range_types.push_back (child_type);
16252 }
16253 }
16254 child_die = child_die->sibling;
16255 }
16256
16257 /* Dwarf2 dimensions are output from left to right, create the
16258 necessary array types in backwards order. */
16259
16260 type = element_type;
16261
16262 if (read_array_order (die, cu) == DW_ORD_col_major)
16263 {
16264 int i = 0;
16265
16266 while (i < range_types.size ())
16267 type = create_array_type_with_stride (NULL, type, range_types[i++],
16268 byte_stride_prop, bit_stride);
16269 }
16270 else
16271 {
16272 size_t ndim = range_types.size ();
16273 while (ndim-- > 0)
16274 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16275 byte_stride_prop, bit_stride);
16276 }
16277
16278 /* Understand Dwarf2 support for vector types (like they occur on
16279 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16280 array type. This is not part of the Dwarf2/3 standard yet, but a
16281 custom vendor extension. The main difference between a regular
16282 array and the vector variant is that vectors are passed by value
16283 to functions. */
16284 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16285 if (attr != nullptr)
16286 make_vector_type (type);
16287
16288 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16289 implementation may choose to implement triple vectors using this
16290 attribute. */
16291 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16292 if (attr != nullptr)
16293 {
16294 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16295 TYPE_LENGTH (type) = DW_UNSND (attr);
16296 else
16297 complaint (_("DW_AT_byte_size for array type smaller "
16298 "than the total size of elements"));
16299 }
16300
16301 name = dwarf2_name (die, cu);
16302 if (name)
16303 type->set_name (name);
16304
16305 maybe_set_alignment (cu, die, type);
16306
16307 /* Install the type in the die. */
16308 set_die_type (die, type, cu);
16309
16310 /* set_die_type should be already done. */
16311 set_descriptive_type (type, die, cu);
16312
16313 return type;
16314 }
16315
16316 static enum dwarf_array_dim_ordering
16317 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16318 {
16319 struct attribute *attr;
16320
16321 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16322
16323 if (attr != nullptr)
16324 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16325
16326 /* GNU F77 is a special case, as at 08/2004 array type info is the
16327 opposite order to the dwarf2 specification, but data is still
16328 laid out as per normal fortran.
16329
16330 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16331 version checking. */
16332
16333 if (cu->language == language_fortran
16334 && cu->producer && strstr (cu->producer, "GNU F77"))
16335 {
16336 return DW_ORD_row_major;
16337 }
16338
16339 switch (cu->language_defn->la_array_ordering)
16340 {
16341 case array_column_major:
16342 return DW_ORD_col_major;
16343 case array_row_major:
16344 default:
16345 return DW_ORD_row_major;
16346 };
16347 }
16348
16349 /* Extract all information from a DW_TAG_set_type DIE and put it in
16350 the DIE's type field. */
16351
16352 static struct type *
16353 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16354 {
16355 struct type *domain_type, *set_type;
16356 struct attribute *attr;
16357
16358 domain_type = die_type (die, cu);
16359
16360 /* The die_type call above may have already set the type for this DIE. */
16361 set_type = get_die_type (die, cu);
16362 if (set_type)
16363 return set_type;
16364
16365 set_type = create_set_type (NULL, domain_type);
16366
16367 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16368 if (attr != nullptr)
16369 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16370
16371 maybe_set_alignment (cu, die, set_type);
16372
16373 return set_die_type (die, set_type, cu);
16374 }
16375
16376 /* A helper for read_common_block that creates a locexpr baton.
16377 SYM is the symbol which we are marking as computed.
16378 COMMON_DIE is the DIE for the common block.
16379 COMMON_LOC is the location expression attribute for the common
16380 block itself.
16381 MEMBER_LOC is the location expression attribute for the particular
16382 member of the common block that we are processing.
16383 CU is the CU from which the above come. */
16384
16385 static void
16386 mark_common_block_symbol_computed (struct symbol *sym,
16387 struct die_info *common_die,
16388 struct attribute *common_loc,
16389 struct attribute *member_loc,
16390 struct dwarf2_cu *cu)
16391 {
16392 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16393 struct objfile *objfile = per_objfile->objfile;
16394 struct dwarf2_locexpr_baton *baton;
16395 gdb_byte *ptr;
16396 unsigned int cu_off;
16397 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16398 LONGEST offset = 0;
16399
16400 gdb_assert (common_loc && member_loc);
16401 gdb_assert (common_loc->form_is_block ());
16402 gdb_assert (member_loc->form_is_block ()
16403 || member_loc->form_is_constant ());
16404
16405 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16406 baton->per_objfile = per_objfile;
16407 baton->per_cu = cu->per_cu;
16408 gdb_assert (baton->per_cu);
16409
16410 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16411
16412 if (member_loc->form_is_constant ())
16413 {
16414 offset = member_loc->constant_value (0);
16415 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16416 }
16417 else
16418 baton->size += DW_BLOCK (member_loc)->size;
16419
16420 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16421 baton->data = ptr;
16422
16423 *ptr++ = DW_OP_call4;
16424 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16425 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16426 ptr += 4;
16427
16428 if (member_loc->form_is_constant ())
16429 {
16430 *ptr++ = DW_OP_addr;
16431 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16432 ptr += cu->header.addr_size;
16433 }
16434 else
16435 {
16436 /* We have to copy the data here, because DW_OP_call4 will only
16437 use a DW_AT_location attribute. */
16438 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16439 ptr += DW_BLOCK (member_loc)->size;
16440 }
16441
16442 *ptr++ = DW_OP_plus;
16443 gdb_assert (ptr - baton->data == baton->size);
16444
16445 SYMBOL_LOCATION_BATON (sym) = baton;
16446 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16447 }
16448
16449 /* Create appropriate locally-scoped variables for all the
16450 DW_TAG_common_block entries. Also create a struct common_block
16451 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16452 is used to separate the common blocks name namespace from regular
16453 variable names. */
16454
16455 static void
16456 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16457 {
16458 struct attribute *attr;
16459
16460 attr = dwarf2_attr (die, DW_AT_location, cu);
16461 if (attr != nullptr)
16462 {
16463 /* Support the .debug_loc offsets. */
16464 if (attr->form_is_block ())
16465 {
16466 /* Ok. */
16467 }
16468 else if (attr->form_is_section_offset ())
16469 {
16470 dwarf2_complex_location_expr_complaint ();
16471 attr = NULL;
16472 }
16473 else
16474 {
16475 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16476 "common block member");
16477 attr = NULL;
16478 }
16479 }
16480
16481 if (die->child != NULL)
16482 {
16483 struct objfile *objfile = cu->per_objfile->objfile;
16484 struct die_info *child_die;
16485 size_t n_entries = 0, size;
16486 struct common_block *common_block;
16487 struct symbol *sym;
16488
16489 for (child_die = die->child;
16490 child_die && child_die->tag;
16491 child_die = child_die->sibling)
16492 ++n_entries;
16493
16494 size = (sizeof (struct common_block)
16495 + (n_entries - 1) * sizeof (struct symbol *));
16496 common_block
16497 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16498 size);
16499 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16500 common_block->n_entries = 0;
16501
16502 for (child_die = die->child;
16503 child_die && child_die->tag;
16504 child_die = child_die->sibling)
16505 {
16506 /* Create the symbol in the DW_TAG_common_block block in the current
16507 symbol scope. */
16508 sym = new_symbol (child_die, NULL, cu);
16509 if (sym != NULL)
16510 {
16511 struct attribute *member_loc;
16512
16513 common_block->contents[common_block->n_entries++] = sym;
16514
16515 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16516 cu);
16517 if (member_loc)
16518 {
16519 /* GDB has handled this for a long time, but it is
16520 not specified by DWARF. It seems to have been
16521 emitted by gfortran at least as recently as:
16522 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16523 complaint (_("Variable in common block has "
16524 "DW_AT_data_member_location "
16525 "- DIE at %s [in module %s]"),
16526 sect_offset_str (child_die->sect_off),
16527 objfile_name (objfile));
16528
16529 if (member_loc->form_is_section_offset ())
16530 dwarf2_complex_location_expr_complaint ();
16531 else if (member_loc->form_is_constant ()
16532 || member_loc->form_is_block ())
16533 {
16534 if (attr != nullptr)
16535 mark_common_block_symbol_computed (sym, die, attr,
16536 member_loc, cu);
16537 }
16538 else
16539 dwarf2_complex_location_expr_complaint ();
16540 }
16541 }
16542 }
16543
16544 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16545 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16546 }
16547 }
16548
16549 /* Create a type for a C++ namespace. */
16550
16551 static struct type *
16552 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16553 {
16554 struct objfile *objfile = cu->per_objfile->objfile;
16555 const char *previous_prefix, *name;
16556 int is_anonymous;
16557 struct type *type;
16558
16559 /* For extensions, reuse the type of the original namespace. */
16560 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16561 {
16562 struct die_info *ext_die;
16563 struct dwarf2_cu *ext_cu = cu;
16564
16565 ext_die = dwarf2_extension (die, &ext_cu);
16566 type = read_type_die (ext_die, ext_cu);
16567
16568 /* EXT_CU may not be the same as CU.
16569 Ensure TYPE is recorded with CU in die_type_hash. */
16570 return set_die_type (die, type, cu);
16571 }
16572
16573 name = namespace_name (die, &is_anonymous, cu);
16574
16575 /* Now build the name of the current namespace. */
16576
16577 previous_prefix = determine_prefix (die, cu);
16578 if (previous_prefix[0] != '\0')
16579 name = typename_concat (&objfile->objfile_obstack,
16580 previous_prefix, name, 0, cu);
16581
16582 /* Create the type. */
16583 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16584
16585 return set_die_type (die, type, cu);
16586 }
16587
16588 /* Read a namespace scope. */
16589
16590 static void
16591 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16592 {
16593 struct objfile *objfile = cu->per_objfile->objfile;
16594 int is_anonymous;
16595
16596 /* Add a symbol associated to this if we haven't seen the namespace
16597 before. Also, add a using directive if it's an anonymous
16598 namespace. */
16599
16600 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16601 {
16602 struct type *type;
16603
16604 type = read_type_die (die, cu);
16605 new_symbol (die, type, cu);
16606
16607 namespace_name (die, &is_anonymous, cu);
16608 if (is_anonymous)
16609 {
16610 const char *previous_prefix = determine_prefix (die, cu);
16611
16612 std::vector<const char *> excludes;
16613 add_using_directive (using_directives (cu),
16614 previous_prefix, type->name (), NULL,
16615 NULL, excludes, 0, &objfile->objfile_obstack);
16616 }
16617 }
16618
16619 if (die->child != NULL)
16620 {
16621 struct die_info *child_die = die->child;
16622
16623 while (child_die && child_die->tag)
16624 {
16625 process_die (child_die, cu);
16626 child_die = child_die->sibling;
16627 }
16628 }
16629 }
16630
16631 /* Read a Fortran module as type. This DIE can be only a declaration used for
16632 imported module. Still we need that type as local Fortran "use ... only"
16633 declaration imports depend on the created type in determine_prefix. */
16634
16635 static struct type *
16636 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16637 {
16638 struct objfile *objfile = cu->per_objfile->objfile;
16639 const char *module_name;
16640 struct type *type;
16641
16642 module_name = dwarf2_name (die, cu);
16643 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16644
16645 return set_die_type (die, type, cu);
16646 }
16647
16648 /* Read a Fortran module. */
16649
16650 static void
16651 read_module (struct die_info *die, struct dwarf2_cu *cu)
16652 {
16653 struct die_info *child_die = die->child;
16654 struct type *type;
16655
16656 type = read_type_die (die, cu);
16657 new_symbol (die, type, cu);
16658
16659 while (child_die && child_die->tag)
16660 {
16661 process_die (child_die, cu);
16662 child_die = child_die->sibling;
16663 }
16664 }
16665
16666 /* Return the name of the namespace represented by DIE. Set
16667 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16668 namespace. */
16669
16670 static const char *
16671 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16672 {
16673 struct die_info *current_die;
16674 const char *name = NULL;
16675
16676 /* Loop through the extensions until we find a name. */
16677
16678 for (current_die = die;
16679 current_die != NULL;
16680 current_die = dwarf2_extension (die, &cu))
16681 {
16682 /* We don't use dwarf2_name here so that we can detect the absence
16683 of a name -> anonymous namespace. */
16684 name = dwarf2_string_attr (die, DW_AT_name, cu);
16685
16686 if (name != NULL)
16687 break;
16688 }
16689
16690 /* Is it an anonymous namespace? */
16691
16692 *is_anonymous = (name == NULL);
16693 if (*is_anonymous)
16694 name = CP_ANONYMOUS_NAMESPACE_STR;
16695
16696 return name;
16697 }
16698
16699 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16700 the user defined type vector. */
16701
16702 static struct type *
16703 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16704 {
16705 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16706 struct comp_unit_head *cu_header = &cu->header;
16707 struct type *type;
16708 struct attribute *attr_byte_size;
16709 struct attribute *attr_address_class;
16710 int byte_size, addr_class;
16711 struct type *target_type;
16712
16713 target_type = die_type (die, cu);
16714
16715 /* The die_type call above may have already set the type for this DIE. */
16716 type = get_die_type (die, cu);
16717 if (type)
16718 return type;
16719
16720 type = lookup_pointer_type (target_type);
16721
16722 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16723 if (attr_byte_size)
16724 byte_size = DW_UNSND (attr_byte_size);
16725 else
16726 byte_size = cu_header->addr_size;
16727
16728 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16729 if (attr_address_class)
16730 addr_class = DW_UNSND (attr_address_class);
16731 else
16732 addr_class = DW_ADDR_none;
16733
16734 ULONGEST alignment = get_alignment (cu, die);
16735
16736 /* If the pointer size, alignment, or address class is different
16737 than the default, create a type variant marked as such and set
16738 the length accordingly. */
16739 if (TYPE_LENGTH (type) != byte_size
16740 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16741 && alignment != TYPE_RAW_ALIGN (type))
16742 || addr_class != DW_ADDR_none)
16743 {
16744 if (gdbarch_address_class_type_flags_p (gdbarch))
16745 {
16746 int type_flags;
16747
16748 type_flags = gdbarch_address_class_type_flags
16749 (gdbarch, byte_size, addr_class);
16750 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16751 == 0);
16752 type = make_type_with_address_space (type, type_flags);
16753 }
16754 else if (TYPE_LENGTH (type) != byte_size)
16755 {
16756 complaint (_("invalid pointer size %d"), byte_size);
16757 }
16758 else if (TYPE_RAW_ALIGN (type) != alignment)
16759 {
16760 complaint (_("Invalid DW_AT_alignment"
16761 " - DIE at %s [in module %s]"),
16762 sect_offset_str (die->sect_off),
16763 objfile_name (cu->per_objfile->objfile));
16764 }
16765 else
16766 {
16767 /* Should we also complain about unhandled address classes? */
16768 }
16769 }
16770
16771 TYPE_LENGTH (type) = byte_size;
16772 set_type_align (type, alignment);
16773 return set_die_type (die, type, cu);
16774 }
16775
16776 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16777 the user defined type vector. */
16778
16779 static struct type *
16780 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16781 {
16782 struct type *type;
16783 struct type *to_type;
16784 struct type *domain;
16785
16786 to_type = die_type (die, cu);
16787 domain = die_containing_type (die, cu);
16788
16789 /* The calls above may have already set the type for this DIE. */
16790 type = get_die_type (die, cu);
16791 if (type)
16792 return type;
16793
16794 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16795 type = lookup_methodptr_type (to_type);
16796 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16797 {
16798 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16799
16800 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16801 to_type->fields (), to_type->num_fields (),
16802 TYPE_VARARGS (to_type));
16803 type = lookup_methodptr_type (new_type);
16804 }
16805 else
16806 type = lookup_memberptr_type (to_type, domain);
16807
16808 return set_die_type (die, type, cu);
16809 }
16810
16811 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16812 the user defined type vector. */
16813
16814 static struct type *
16815 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16816 enum type_code refcode)
16817 {
16818 struct comp_unit_head *cu_header = &cu->header;
16819 struct type *type, *target_type;
16820 struct attribute *attr;
16821
16822 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16823
16824 target_type = die_type (die, cu);
16825
16826 /* The die_type call above may have already set the type for this DIE. */
16827 type = get_die_type (die, cu);
16828 if (type)
16829 return type;
16830
16831 type = lookup_reference_type (target_type, refcode);
16832 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16833 if (attr != nullptr)
16834 {
16835 TYPE_LENGTH (type) = DW_UNSND (attr);
16836 }
16837 else
16838 {
16839 TYPE_LENGTH (type) = cu_header->addr_size;
16840 }
16841 maybe_set_alignment (cu, die, type);
16842 return set_die_type (die, type, cu);
16843 }
16844
16845 /* Add the given cv-qualifiers to the element type of the array. GCC
16846 outputs DWARF type qualifiers that apply to an array, not the
16847 element type. But GDB relies on the array element type to carry
16848 the cv-qualifiers. This mimics section 6.7.3 of the C99
16849 specification. */
16850
16851 static struct type *
16852 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16853 struct type *base_type, int cnst, int voltl)
16854 {
16855 struct type *el_type, *inner_array;
16856
16857 base_type = copy_type (base_type);
16858 inner_array = base_type;
16859
16860 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16861 {
16862 TYPE_TARGET_TYPE (inner_array) =
16863 copy_type (TYPE_TARGET_TYPE (inner_array));
16864 inner_array = TYPE_TARGET_TYPE (inner_array);
16865 }
16866
16867 el_type = TYPE_TARGET_TYPE (inner_array);
16868 cnst |= TYPE_CONST (el_type);
16869 voltl |= TYPE_VOLATILE (el_type);
16870 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16871
16872 return set_die_type (die, base_type, cu);
16873 }
16874
16875 static struct type *
16876 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16877 {
16878 struct type *base_type, *cv_type;
16879
16880 base_type = die_type (die, cu);
16881
16882 /* The die_type call above may have already set the type for this DIE. */
16883 cv_type = get_die_type (die, cu);
16884 if (cv_type)
16885 return cv_type;
16886
16887 /* In case the const qualifier is applied to an array type, the element type
16888 is so qualified, not the array type (section 6.7.3 of C99). */
16889 if (base_type->code () == TYPE_CODE_ARRAY)
16890 return add_array_cv_type (die, cu, base_type, 1, 0);
16891
16892 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16893 return set_die_type (die, cv_type, cu);
16894 }
16895
16896 static struct type *
16897 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16898 {
16899 struct type *base_type, *cv_type;
16900
16901 base_type = die_type (die, cu);
16902
16903 /* The die_type call above may have already set the type for this DIE. */
16904 cv_type = get_die_type (die, cu);
16905 if (cv_type)
16906 return cv_type;
16907
16908 /* In case the volatile qualifier is applied to an array type, the
16909 element type is so qualified, not the array type (section 6.7.3
16910 of C99). */
16911 if (base_type->code () == TYPE_CODE_ARRAY)
16912 return add_array_cv_type (die, cu, base_type, 0, 1);
16913
16914 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16915 return set_die_type (die, cv_type, cu);
16916 }
16917
16918 /* Handle DW_TAG_restrict_type. */
16919
16920 static struct type *
16921 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16922 {
16923 struct type *base_type, *cv_type;
16924
16925 base_type = die_type (die, cu);
16926
16927 /* The die_type call above may have already set the type for this DIE. */
16928 cv_type = get_die_type (die, cu);
16929 if (cv_type)
16930 return cv_type;
16931
16932 cv_type = make_restrict_type (base_type);
16933 return set_die_type (die, cv_type, cu);
16934 }
16935
16936 /* Handle DW_TAG_atomic_type. */
16937
16938 static struct type *
16939 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16940 {
16941 struct type *base_type, *cv_type;
16942
16943 base_type = die_type (die, cu);
16944
16945 /* The die_type call above may have already set the type for this DIE. */
16946 cv_type = get_die_type (die, cu);
16947 if (cv_type)
16948 return cv_type;
16949
16950 cv_type = make_atomic_type (base_type);
16951 return set_die_type (die, cv_type, cu);
16952 }
16953
16954 /* Extract all information from a DW_TAG_string_type DIE and add to
16955 the user defined type vector. It isn't really a user defined type,
16956 but it behaves like one, with other DIE's using an AT_user_def_type
16957 attribute to reference it. */
16958
16959 static struct type *
16960 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16961 {
16962 struct objfile *objfile = cu->per_objfile->objfile;
16963 struct gdbarch *gdbarch = objfile->arch ();
16964 struct type *type, *range_type, *index_type, *char_type;
16965 struct attribute *attr;
16966 struct dynamic_prop prop;
16967 bool length_is_constant = true;
16968 LONGEST length;
16969
16970 /* There are a couple of places where bit sizes might be made use of
16971 when parsing a DW_TAG_string_type, however, no producer that we know
16972 of make use of these. Handling bit sizes that are a multiple of the
16973 byte size is easy enough, but what about other bit sizes? Lets deal
16974 with that problem when we have to. Warn about these attributes being
16975 unsupported, then parse the type and ignore them like we always
16976 have. */
16977 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16978 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16979 {
16980 static bool warning_printed = false;
16981 if (!warning_printed)
16982 {
16983 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16984 "currently supported on DW_TAG_string_type."));
16985 warning_printed = true;
16986 }
16987 }
16988
16989 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16990 if (attr != nullptr && !attr->form_is_constant ())
16991 {
16992 /* The string length describes the location at which the length of
16993 the string can be found. The size of the length field can be
16994 specified with one of the attributes below. */
16995 struct type *prop_type;
16996 struct attribute *len
16997 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16998 if (len == nullptr)
16999 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17000 if (len != nullptr && len->form_is_constant ())
17001 {
17002 /* Pass 0 as the default as we know this attribute is constant
17003 and the default value will not be returned. */
17004 LONGEST sz = len->constant_value (0);
17005 prop_type = cu->per_objfile->int_type (sz, true);
17006 }
17007 else
17008 {
17009 /* If the size is not specified then we assume it is the size of
17010 an address on this target. */
17011 prop_type = cu->addr_sized_int_type (true);
17012 }
17013
17014 /* Convert the attribute into a dynamic property. */
17015 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17016 length = 1;
17017 else
17018 length_is_constant = false;
17019 }
17020 else if (attr != nullptr)
17021 {
17022 /* This DW_AT_string_length just contains the length with no
17023 indirection. There's no need to create a dynamic property in this
17024 case. Pass 0 for the default value as we know it will not be
17025 returned in this case. */
17026 length = attr->constant_value (0);
17027 }
17028 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17029 {
17030 /* We don't currently support non-constant byte sizes for strings. */
17031 length = attr->constant_value (1);
17032 }
17033 else
17034 {
17035 /* Use 1 as a fallback length if we have nothing else. */
17036 length = 1;
17037 }
17038
17039 index_type = objfile_type (objfile)->builtin_int;
17040 if (length_is_constant)
17041 range_type = create_static_range_type (NULL, index_type, 1, length);
17042 else
17043 {
17044 struct dynamic_prop low_bound;
17045
17046 low_bound.kind = PROP_CONST;
17047 low_bound.data.const_val = 1;
17048 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17049 }
17050 char_type = language_string_char_type (cu->language_defn, gdbarch);
17051 type = create_string_type (NULL, char_type, range_type);
17052
17053 return set_die_type (die, type, cu);
17054 }
17055
17056 /* Assuming that DIE corresponds to a function, returns nonzero
17057 if the function is prototyped. */
17058
17059 static int
17060 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17061 {
17062 struct attribute *attr;
17063
17064 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17065 if (attr && (DW_UNSND (attr) != 0))
17066 return 1;
17067
17068 /* The DWARF standard implies that the DW_AT_prototyped attribute
17069 is only meaningful for C, but the concept also extends to other
17070 languages that allow unprototyped functions (Eg: Objective C).
17071 For all other languages, assume that functions are always
17072 prototyped. */
17073 if (cu->language != language_c
17074 && cu->language != language_objc
17075 && cu->language != language_opencl)
17076 return 1;
17077
17078 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17079 prototyped and unprototyped functions; default to prototyped,
17080 since that is more common in modern code (and RealView warns
17081 about unprototyped functions). */
17082 if (producer_is_realview (cu->producer))
17083 return 1;
17084
17085 return 0;
17086 }
17087
17088 /* Handle DIES due to C code like:
17089
17090 struct foo
17091 {
17092 int (*funcp)(int a, long l);
17093 int b;
17094 };
17095
17096 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17097
17098 static struct type *
17099 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17100 {
17101 struct objfile *objfile = cu->per_objfile->objfile;
17102 struct type *type; /* Type that this function returns. */
17103 struct type *ftype; /* Function that returns above type. */
17104 struct attribute *attr;
17105
17106 type = die_type (die, cu);
17107
17108 /* The die_type call above may have already set the type for this DIE. */
17109 ftype = get_die_type (die, cu);
17110 if (ftype)
17111 return ftype;
17112
17113 ftype = lookup_function_type (type);
17114
17115 if (prototyped_function_p (die, cu))
17116 TYPE_PROTOTYPED (ftype) = 1;
17117
17118 /* Store the calling convention in the type if it's available in
17119 the subroutine die. Otherwise set the calling convention to
17120 the default value DW_CC_normal. */
17121 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17122 if (attr != nullptr
17123 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17124 TYPE_CALLING_CONVENTION (ftype)
17125 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17126 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17127 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17128 else
17129 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17130
17131 /* Record whether the function returns normally to its caller or not
17132 if the DWARF producer set that information. */
17133 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17134 if (attr && (DW_UNSND (attr) != 0))
17135 TYPE_NO_RETURN (ftype) = 1;
17136
17137 /* We need to add the subroutine type to the die immediately so
17138 we don't infinitely recurse when dealing with parameters
17139 declared as the same subroutine type. */
17140 set_die_type (die, ftype, cu);
17141
17142 if (die->child != NULL)
17143 {
17144 struct type *void_type = objfile_type (objfile)->builtin_void;
17145 struct die_info *child_die;
17146 int nparams, iparams;
17147
17148 /* Count the number of parameters.
17149 FIXME: GDB currently ignores vararg functions, but knows about
17150 vararg member functions. */
17151 nparams = 0;
17152 child_die = die->child;
17153 while (child_die && child_die->tag)
17154 {
17155 if (child_die->tag == DW_TAG_formal_parameter)
17156 nparams++;
17157 else if (child_die->tag == DW_TAG_unspecified_parameters)
17158 TYPE_VARARGS (ftype) = 1;
17159 child_die = child_die->sibling;
17160 }
17161
17162 /* Allocate storage for parameters and fill them in. */
17163 ftype->set_num_fields (nparams);
17164 ftype->set_fields
17165 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17166
17167 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17168 even if we error out during the parameters reading below. */
17169 for (iparams = 0; iparams < nparams; iparams++)
17170 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17171
17172 iparams = 0;
17173 child_die = die->child;
17174 while (child_die && child_die->tag)
17175 {
17176 if (child_die->tag == DW_TAG_formal_parameter)
17177 {
17178 struct type *arg_type;
17179
17180 /* DWARF version 2 has no clean way to discern C++
17181 static and non-static member functions. G++ helps
17182 GDB by marking the first parameter for non-static
17183 member functions (which is the this pointer) as
17184 artificial. We pass this information to
17185 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17186
17187 DWARF version 3 added DW_AT_object_pointer, which GCC
17188 4.5 does not yet generate. */
17189 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17190 if (attr != nullptr)
17191 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17192 else
17193 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17194 arg_type = die_type (child_die, cu);
17195
17196 /* RealView does not mark THIS as const, which the testsuite
17197 expects. GCC marks THIS as const in method definitions,
17198 but not in the class specifications (GCC PR 43053). */
17199 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17200 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17201 {
17202 int is_this = 0;
17203 struct dwarf2_cu *arg_cu = cu;
17204 const char *name = dwarf2_name (child_die, cu);
17205
17206 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17207 if (attr != nullptr)
17208 {
17209 /* If the compiler emits this, use it. */
17210 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17211 is_this = 1;
17212 }
17213 else if (name && strcmp (name, "this") == 0)
17214 /* Function definitions will have the argument names. */
17215 is_this = 1;
17216 else if (name == NULL && iparams == 0)
17217 /* Declarations may not have the names, so like
17218 elsewhere in GDB, assume an artificial first
17219 argument is "this". */
17220 is_this = 1;
17221
17222 if (is_this)
17223 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17224 arg_type, 0);
17225 }
17226
17227 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17228 iparams++;
17229 }
17230 child_die = child_die->sibling;
17231 }
17232 }
17233
17234 return ftype;
17235 }
17236
17237 static struct type *
17238 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17239 {
17240 struct objfile *objfile = cu->per_objfile->objfile;
17241 const char *name = NULL;
17242 struct type *this_type, *target_type;
17243
17244 name = dwarf2_full_name (NULL, die, cu);
17245 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17246 TYPE_TARGET_STUB (this_type) = 1;
17247 set_die_type (die, this_type, cu);
17248 target_type = die_type (die, cu);
17249 if (target_type != this_type)
17250 TYPE_TARGET_TYPE (this_type) = target_type;
17251 else
17252 {
17253 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17254 spec and cause infinite loops in GDB. */
17255 complaint (_("Self-referential DW_TAG_typedef "
17256 "- DIE at %s [in module %s]"),
17257 sect_offset_str (die->sect_off), objfile_name (objfile));
17258 TYPE_TARGET_TYPE (this_type) = NULL;
17259 }
17260 if (name == NULL)
17261 {
17262 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17263 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17264 Handle these by just returning the target type, rather than
17265 constructing an anonymous typedef type and trying to handle this
17266 elsewhere. */
17267 set_die_type (die, target_type, cu);
17268 return target_type;
17269 }
17270 return this_type;
17271 }
17272
17273 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17274 (which may be different from NAME) to the architecture back-end to allow
17275 it to guess the correct format if necessary. */
17276
17277 static struct type *
17278 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17279 const char *name_hint, enum bfd_endian byte_order)
17280 {
17281 struct gdbarch *gdbarch = objfile->arch ();
17282 const struct floatformat **format;
17283 struct type *type;
17284
17285 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17286 if (format)
17287 type = init_float_type (objfile, bits, name, format, byte_order);
17288 else
17289 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17290
17291 return type;
17292 }
17293
17294 /* Allocate an integer type of size BITS and name NAME. */
17295
17296 static struct type *
17297 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17298 int bits, int unsigned_p, const char *name)
17299 {
17300 struct type *type;
17301
17302 /* Versions of Intel's C Compiler generate an integer type called "void"
17303 instead of using DW_TAG_unspecified_type. This has been seen on
17304 at least versions 14, 17, and 18. */
17305 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17306 && strcmp (name, "void") == 0)
17307 type = objfile_type (objfile)->builtin_void;
17308 else
17309 type = init_integer_type (objfile, bits, unsigned_p, name);
17310
17311 return type;
17312 }
17313
17314 /* Initialise and return a floating point type of size BITS suitable for
17315 use as a component of a complex number. The NAME_HINT is passed through
17316 when initialising the floating point type and is the name of the complex
17317 type.
17318
17319 As DWARF doesn't currently provide an explicit name for the components
17320 of a complex number, but it can be helpful to have these components
17321 named, we try to select a suitable name based on the size of the
17322 component. */
17323 static struct type *
17324 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17325 struct objfile *objfile,
17326 int bits, const char *name_hint,
17327 enum bfd_endian byte_order)
17328 {
17329 gdbarch *gdbarch = objfile->arch ();
17330 struct type *tt = nullptr;
17331
17332 /* Try to find a suitable floating point builtin type of size BITS.
17333 We're going to use the name of this type as the name for the complex
17334 target type that we are about to create. */
17335 switch (cu->language)
17336 {
17337 case language_fortran:
17338 switch (bits)
17339 {
17340 case 32:
17341 tt = builtin_f_type (gdbarch)->builtin_real;
17342 break;
17343 case 64:
17344 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17345 break;
17346 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17347 case 128:
17348 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17349 break;
17350 }
17351 break;
17352 default:
17353 switch (bits)
17354 {
17355 case 32:
17356 tt = builtin_type (gdbarch)->builtin_float;
17357 break;
17358 case 64:
17359 tt = builtin_type (gdbarch)->builtin_double;
17360 break;
17361 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17362 case 128:
17363 tt = builtin_type (gdbarch)->builtin_long_double;
17364 break;
17365 }
17366 break;
17367 }
17368
17369 /* If the type we found doesn't match the size we were looking for, then
17370 pretend we didn't find a type at all, the complex target type we
17371 create will then be nameless. */
17372 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17373 tt = nullptr;
17374
17375 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17376 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17377 }
17378
17379 /* Find a representation of a given base type and install
17380 it in the TYPE field of the die. */
17381
17382 static struct type *
17383 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17384 {
17385 struct objfile *objfile = cu->per_objfile->objfile;
17386 struct type *type;
17387 struct attribute *attr;
17388 int encoding = 0, bits = 0;
17389 const char *name;
17390 gdbarch *arch;
17391
17392 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17393 if (attr != nullptr)
17394 encoding = DW_UNSND (attr);
17395 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17396 if (attr != nullptr)
17397 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17398 name = dwarf2_name (die, cu);
17399 if (!name)
17400 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17401
17402 arch = objfile->arch ();
17403 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17404
17405 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17406 if (attr)
17407 {
17408 int endianity = DW_UNSND (attr);
17409
17410 switch (endianity)
17411 {
17412 case DW_END_big:
17413 byte_order = BFD_ENDIAN_BIG;
17414 break;
17415 case DW_END_little:
17416 byte_order = BFD_ENDIAN_LITTLE;
17417 break;
17418 default:
17419 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17420 break;
17421 }
17422 }
17423
17424 switch (encoding)
17425 {
17426 case DW_ATE_address:
17427 /* Turn DW_ATE_address into a void * pointer. */
17428 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17429 type = init_pointer_type (objfile, bits, name, type);
17430 break;
17431 case DW_ATE_boolean:
17432 type = init_boolean_type (objfile, bits, 1, name);
17433 break;
17434 case DW_ATE_complex_float:
17435 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17436 byte_order);
17437 if (type->code () == TYPE_CODE_ERROR)
17438 {
17439 if (name == nullptr)
17440 {
17441 struct obstack *obstack
17442 = &cu->per_objfile->objfile->objfile_obstack;
17443 name = obconcat (obstack, "_Complex ", type->name (),
17444 nullptr);
17445 }
17446 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17447 }
17448 else
17449 type = init_complex_type (name, type);
17450 break;
17451 case DW_ATE_decimal_float:
17452 type = init_decfloat_type (objfile, bits, name);
17453 break;
17454 case DW_ATE_float:
17455 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17456 break;
17457 case DW_ATE_signed:
17458 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17459 break;
17460 case DW_ATE_unsigned:
17461 if (cu->language == language_fortran
17462 && name
17463 && startswith (name, "character("))
17464 type = init_character_type (objfile, bits, 1, name);
17465 else
17466 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17467 break;
17468 case DW_ATE_signed_char:
17469 if (cu->language == language_ada || cu->language == language_m2
17470 || cu->language == language_pascal
17471 || cu->language == language_fortran)
17472 type = init_character_type (objfile, bits, 0, name);
17473 else
17474 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17475 break;
17476 case DW_ATE_unsigned_char:
17477 if (cu->language == language_ada || cu->language == language_m2
17478 || cu->language == language_pascal
17479 || cu->language == language_fortran
17480 || cu->language == language_rust)
17481 type = init_character_type (objfile, bits, 1, name);
17482 else
17483 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17484 break;
17485 case DW_ATE_UTF:
17486 {
17487 if (bits == 16)
17488 type = builtin_type (arch)->builtin_char16;
17489 else if (bits == 32)
17490 type = builtin_type (arch)->builtin_char32;
17491 else
17492 {
17493 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17494 bits);
17495 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17496 }
17497 return set_die_type (die, type, cu);
17498 }
17499 break;
17500
17501 default:
17502 complaint (_("unsupported DW_AT_encoding: '%s'"),
17503 dwarf_type_encoding_name (encoding));
17504 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17505 break;
17506 }
17507
17508 if (name && strcmp (name, "char") == 0)
17509 TYPE_NOSIGN (type) = 1;
17510
17511 maybe_set_alignment (cu, die, type);
17512
17513 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17514
17515 return set_die_type (die, type, cu);
17516 }
17517
17518 /* Parse dwarf attribute if it's a block, reference or constant and put the
17519 resulting value of the attribute into struct bound_prop.
17520 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17521
17522 static int
17523 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17524 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17525 struct type *default_type)
17526 {
17527 struct dwarf2_property_baton *baton;
17528 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17529 struct objfile *objfile = per_objfile->objfile;
17530 struct obstack *obstack = &objfile->objfile_obstack;
17531
17532 gdb_assert (default_type != NULL);
17533
17534 if (attr == NULL || prop == NULL)
17535 return 0;
17536
17537 if (attr->form_is_block ())
17538 {
17539 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17540 baton->property_type = default_type;
17541 baton->locexpr.per_cu = cu->per_cu;
17542 baton->locexpr.per_objfile = per_objfile;
17543 baton->locexpr.size = DW_BLOCK (attr)->size;
17544 baton->locexpr.data = DW_BLOCK (attr)->data;
17545 switch (attr->name)
17546 {
17547 case DW_AT_string_length:
17548 baton->locexpr.is_reference = true;
17549 break;
17550 default:
17551 baton->locexpr.is_reference = false;
17552 break;
17553 }
17554 prop->data.baton = baton;
17555 prop->kind = PROP_LOCEXPR;
17556 gdb_assert (prop->data.baton != NULL);
17557 }
17558 else if (attr->form_is_ref ())
17559 {
17560 struct dwarf2_cu *target_cu = cu;
17561 struct die_info *target_die;
17562 struct attribute *target_attr;
17563
17564 target_die = follow_die_ref (die, attr, &target_cu);
17565 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17566 if (target_attr == NULL)
17567 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17568 target_cu);
17569 if (target_attr == NULL)
17570 return 0;
17571
17572 switch (target_attr->name)
17573 {
17574 case DW_AT_location:
17575 if (target_attr->form_is_section_offset ())
17576 {
17577 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17578 baton->property_type = die_type (target_die, target_cu);
17579 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17580 prop->data.baton = baton;
17581 prop->kind = PROP_LOCLIST;
17582 gdb_assert (prop->data.baton != NULL);
17583 }
17584 else if (target_attr->form_is_block ())
17585 {
17586 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17587 baton->property_type = die_type (target_die, target_cu);
17588 baton->locexpr.per_cu = cu->per_cu;
17589 baton->locexpr.per_objfile = per_objfile;
17590 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17591 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17592 baton->locexpr.is_reference = true;
17593 prop->data.baton = baton;
17594 prop->kind = PROP_LOCEXPR;
17595 gdb_assert (prop->data.baton != NULL);
17596 }
17597 else
17598 {
17599 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17600 "dynamic property");
17601 return 0;
17602 }
17603 break;
17604 case DW_AT_data_member_location:
17605 {
17606 LONGEST offset;
17607
17608 if (!handle_data_member_location (target_die, target_cu,
17609 &offset))
17610 return 0;
17611
17612 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17613 baton->property_type = read_type_die (target_die->parent,
17614 target_cu);
17615 baton->offset_info.offset = offset;
17616 baton->offset_info.type = die_type (target_die, target_cu);
17617 prop->data.baton = baton;
17618 prop->kind = PROP_ADDR_OFFSET;
17619 break;
17620 }
17621 }
17622 }
17623 else if (attr->form_is_constant ())
17624 {
17625 prop->data.const_val = attr->constant_value (0);
17626 prop->kind = PROP_CONST;
17627 }
17628 else
17629 {
17630 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17631 dwarf2_name (die, cu));
17632 return 0;
17633 }
17634
17635 return 1;
17636 }
17637
17638 /* See read.h. */
17639
17640 struct type *
17641 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
17642 {
17643 struct type *int_type;
17644
17645 /* Helper macro to examine the various builtin types. */
17646 #define TRY_TYPE(F) \
17647 int_type = (unsigned_p \
17648 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17649 : objfile_type (objfile)->builtin_ ## F); \
17650 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17651 return int_type
17652
17653 TRY_TYPE (char);
17654 TRY_TYPE (short);
17655 TRY_TYPE (int);
17656 TRY_TYPE (long);
17657 TRY_TYPE (long_long);
17658
17659 #undef TRY_TYPE
17660
17661 gdb_assert_not_reached ("unable to find suitable integer type");
17662 }
17663
17664 /* See read.h. */
17665
17666 struct type *
17667 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
17668 {
17669 int addr_size = this->per_cu->addr_size ();
17670 return this->per_objfile->int_type (addr_size, unsigned_p);
17671 }
17672
17673 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17674 present (which is valid) then compute the default type based on the
17675 compilation units address size. */
17676
17677 static struct type *
17678 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17679 {
17680 struct type *index_type = die_type (die, cu);
17681
17682 /* Dwarf-2 specifications explicitly allows to create subrange types
17683 without specifying a base type.
17684 In that case, the base type must be set to the type of
17685 the lower bound, upper bound or count, in that order, if any of these
17686 three attributes references an object that has a type.
17687 If no base type is found, the Dwarf-2 specifications say that
17688 a signed integer type of size equal to the size of an address should
17689 be used.
17690 For the following C code: `extern char gdb_int [];'
17691 GCC produces an empty range DIE.
17692 FIXME: muller/2010-05-28: Possible references to object for low bound,
17693 high bound or count are not yet handled by this code. */
17694 if (index_type->code () == TYPE_CODE_VOID)
17695 index_type = cu->addr_sized_int_type (false);
17696
17697 return index_type;
17698 }
17699
17700 /* Read the given DW_AT_subrange DIE. */
17701
17702 static struct type *
17703 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17704 {
17705 struct type *base_type, *orig_base_type;
17706 struct type *range_type;
17707 struct attribute *attr;
17708 struct dynamic_prop low, high;
17709 int low_default_is_valid;
17710 int high_bound_is_count = 0;
17711 const char *name;
17712 ULONGEST negative_mask;
17713
17714 orig_base_type = read_subrange_index_type (die, cu);
17715
17716 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17717 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17718 creating the range type, but we use the result of check_typedef
17719 when examining properties of the type. */
17720 base_type = check_typedef (orig_base_type);
17721
17722 /* The die_type call above may have already set the type for this DIE. */
17723 range_type = get_die_type (die, cu);
17724 if (range_type)
17725 return range_type;
17726
17727 low.kind = PROP_CONST;
17728 high.kind = PROP_CONST;
17729 high.data.const_val = 0;
17730
17731 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17732 omitting DW_AT_lower_bound. */
17733 switch (cu->language)
17734 {
17735 case language_c:
17736 case language_cplus:
17737 low.data.const_val = 0;
17738 low_default_is_valid = 1;
17739 break;
17740 case language_fortran:
17741 low.data.const_val = 1;
17742 low_default_is_valid = 1;
17743 break;
17744 case language_d:
17745 case language_objc:
17746 case language_rust:
17747 low.data.const_val = 0;
17748 low_default_is_valid = (cu->header.version >= 4);
17749 break;
17750 case language_ada:
17751 case language_m2:
17752 case language_pascal:
17753 low.data.const_val = 1;
17754 low_default_is_valid = (cu->header.version >= 4);
17755 break;
17756 default:
17757 low.data.const_val = 0;
17758 low_default_is_valid = 0;
17759 break;
17760 }
17761
17762 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17763 if (attr != nullptr)
17764 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17765 else if (!low_default_is_valid)
17766 complaint (_("Missing DW_AT_lower_bound "
17767 "- DIE at %s [in module %s]"),
17768 sect_offset_str (die->sect_off),
17769 objfile_name (cu->per_objfile->objfile));
17770
17771 struct attribute *attr_ub, *attr_count;
17772 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17773 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17774 {
17775 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17776 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17777 {
17778 /* If bounds are constant do the final calculation here. */
17779 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17780 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17781 else
17782 high_bound_is_count = 1;
17783 }
17784 else
17785 {
17786 if (attr_ub != NULL)
17787 complaint (_("Unresolved DW_AT_upper_bound "
17788 "- DIE at %s [in module %s]"),
17789 sect_offset_str (die->sect_off),
17790 objfile_name (cu->per_objfile->objfile));
17791 if (attr_count != NULL)
17792 complaint (_("Unresolved DW_AT_count "
17793 "- DIE at %s [in module %s]"),
17794 sect_offset_str (die->sect_off),
17795 objfile_name (cu->per_objfile->objfile));
17796 }
17797 }
17798
17799 LONGEST bias = 0;
17800 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17801 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17802 bias = bias_attr->constant_value (0);
17803
17804 /* Normally, the DWARF producers are expected to use a signed
17805 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17806 But this is unfortunately not always the case, as witnessed
17807 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17808 is used instead. To work around that ambiguity, we treat
17809 the bounds as signed, and thus sign-extend their values, when
17810 the base type is signed. */
17811 negative_mask =
17812 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17813 if (low.kind == PROP_CONST
17814 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17815 low.data.const_val |= negative_mask;
17816 if (high.kind == PROP_CONST
17817 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17818 high.data.const_val |= negative_mask;
17819
17820 /* Check for bit and byte strides. */
17821 struct dynamic_prop byte_stride_prop;
17822 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17823 if (attr_byte_stride != nullptr)
17824 {
17825 struct type *prop_type = cu->addr_sized_int_type (false);
17826 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17827 prop_type);
17828 }
17829
17830 struct dynamic_prop bit_stride_prop;
17831 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17832 if (attr_bit_stride != nullptr)
17833 {
17834 /* It only makes sense to have either a bit or byte stride. */
17835 if (attr_byte_stride != nullptr)
17836 {
17837 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17838 "- DIE at %s [in module %s]"),
17839 sect_offset_str (die->sect_off),
17840 objfile_name (cu->per_objfile->objfile));
17841 attr_bit_stride = nullptr;
17842 }
17843 else
17844 {
17845 struct type *prop_type = cu->addr_sized_int_type (false);
17846 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17847 prop_type);
17848 }
17849 }
17850
17851 if (attr_byte_stride != nullptr
17852 || attr_bit_stride != nullptr)
17853 {
17854 bool byte_stride_p = (attr_byte_stride != nullptr);
17855 struct dynamic_prop *stride
17856 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17857
17858 range_type
17859 = create_range_type_with_stride (NULL, orig_base_type, &low,
17860 &high, bias, stride, byte_stride_p);
17861 }
17862 else
17863 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17864
17865 if (high_bound_is_count)
17866 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17867
17868 /* Ada expects an empty array on no boundary attributes. */
17869 if (attr == NULL && cu->language != language_ada)
17870 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17871
17872 name = dwarf2_name (die, cu);
17873 if (name)
17874 range_type->set_name (name);
17875
17876 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17877 if (attr != nullptr)
17878 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17879
17880 maybe_set_alignment (cu, die, range_type);
17881
17882 set_die_type (die, range_type, cu);
17883
17884 /* set_die_type should be already done. */
17885 set_descriptive_type (range_type, die, cu);
17886
17887 return range_type;
17888 }
17889
17890 static struct type *
17891 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17892 {
17893 struct type *type;
17894
17895 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17896 type->set_name (dwarf2_name (die, cu));
17897
17898 /* In Ada, an unspecified type is typically used when the description
17899 of the type is deferred to a different unit. When encountering
17900 such a type, we treat it as a stub, and try to resolve it later on,
17901 when needed. */
17902 if (cu->language == language_ada)
17903 TYPE_STUB (type) = 1;
17904
17905 return set_die_type (die, type, cu);
17906 }
17907
17908 /* Read a single die and all its descendents. Set the die's sibling
17909 field to NULL; set other fields in the die correctly, and set all
17910 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17911 location of the info_ptr after reading all of those dies. PARENT
17912 is the parent of the die in question. */
17913
17914 static struct die_info *
17915 read_die_and_children (const struct die_reader_specs *reader,
17916 const gdb_byte *info_ptr,
17917 const gdb_byte **new_info_ptr,
17918 struct die_info *parent)
17919 {
17920 struct die_info *die;
17921 const gdb_byte *cur_ptr;
17922
17923 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17924 if (die == NULL)
17925 {
17926 *new_info_ptr = cur_ptr;
17927 return NULL;
17928 }
17929 store_in_ref_table (die, reader->cu);
17930
17931 if (die->has_children)
17932 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17933 else
17934 {
17935 die->child = NULL;
17936 *new_info_ptr = cur_ptr;
17937 }
17938
17939 die->sibling = NULL;
17940 die->parent = parent;
17941 return die;
17942 }
17943
17944 /* Read a die, all of its descendents, and all of its siblings; set
17945 all of the fields of all of the dies correctly. Arguments are as
17946 in read_die_and_children. */
17947
17948 static struct die_info *
17949 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17950 const gdb_byte *info_ptr,
17951 const gdb_byte **new_info_ptr,
17952 struct die_info *parent)
17953 {
17954 struct die_info *first_die, *last_sibling;
17955 const gdb_byte *cur_ptr;
17956
17957 cur_ptr = info_ptr;
17958 first_die = last_sibling = NULL;
17959
17960 while (1)
17961 {
17962 struct die_info *die
17963 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17964
17965 if (die == NULL)
17966 {
17967 *new_info_ptr = cur_ptr;
17968 return first_die;
17969 }
17970
17971 if (!first_die)
17972 first_die = die;
17973 else
17974 last_sibling->sibling = die;
17975
17976 last_sibling = die;
17977 }
17978 }
17979
17980 /* Read a die, all of its descendents, and all of its siblings; set
17981 all of the fields of all of the dies correctly. Arguments are as
17982 in read_die_and_children.
17983 This the main entry point for reading a DIE and all its children. */
17984
17985 static struct die_info *
17986 read_die_and_siblings (const struct die_reader_specs *reader,
17987 const gdb_byte *info_ptr,
17988 const gdb_byte **new_info_ptr,
17989 struct die_info *parent)
17990 {
17991 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17992 new_info_ptr, parent);
17993
17994 if (dwarf_die_debug)
17995 {
17996 fprintf_unfiltered (gdb_stdlog,
17997 "Read die from %s@0x%x of %s:\n",
17998 reader->die_section->get_name (),
17999 (unsigned) (info_ptr - reader->die_section->buffer),
18000 bfd_get_filename (reader->abfd));
18001 dump_die (die, dwarf_die_debug);
18002 }
18003
18004 return die;
18005 }
18006
18007 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18008 attributes.
18009 The caller is responsible for filling in the extra attributes
18010 and updating (*DIEP)->num_attrs.
18011 Set DIEP to point to a newly allocated die with its information,
18012 except for its child, sibling, and parent fields. */
18013
18014 static const gdb_byte *
18015 read_full_die_1 (const struct die_reader_specs *reader,
18016 struct die_info **diep, const gdb_byte *info_ptr,
18017 int num_extra_attrs)
18018 {
18019 unsigned int abbrev_number, bytes_read, i;
18020 struct abbrev_info *abbrev;
18021 struct die_info *die;
18022 struct dwarf2_cu *cu = reader->cu;
18023 bfd *abfd = reader->abfd;
18024
18025 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18026 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18027 info_ptr += bytes_read;
18028 if (!abbrev_number)
18029 {
18030 *diep = NULL;
18031 return info_ptr;
18032 }
18033
18034 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18035 if (!abbrev)
18036 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18037 abbrev_number,
18038 bfd_get_filename (abfd));
18039
18040 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18041 die->sect_off = sect_off;
18042 die->tag = abbrev->tag;
18043 die->abbrev = abbrev_number;
18044 die->has_children = abbrev->has_children;
18045
18046 /* Make the result usable.
18047 The caller needs to update num_attrs after adding the extra
18048 attributes. */
18049 die->num_attrs = abbrev->num_attrs;
18050
18051 std::vector<int> indexes_that_need_reprocess;
18052 for (i = 0; i < abbrev->num_attrs; ++i)
18053 {
18054 bool need_reprocess;
18055 info_ptr =
18056 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18057 info_ptr, &need_reprocess);
18058 if (need_reprocess)
18059 indexes_that_need_reprocess.push_back (i);
18060 }
18061
18062 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18063 if (attr != nullptr)
18064 cu->str_offsets_base = DW_UNSND (attr);
18065
18066 attr = die->attr (DW_AT_loclists_base);
18067 if (attr != nullptr)
18068 cu->loclist_base = DW_UNSND (attr);
18069
18070 auto maybe_addr_base = die->addr_base ();
18071 if (maybe_addr_base.has_value ())
18072 cu->addr_base = *maybe_addr_base;
18073 for (int index : indexes_that_need_reprocess)
18074 read_attribute_reprocess (reader, &die->attrs[index]);
18075 *diep = die;
18076 return info_ptr;
18077 }
18078
18079 /* Read a die and all its attributes.
18080 Set DIEP to point to a newly allocated die with its information,
18081 except for its child, sibling, and parent fields. */
18082
18083 static const gdb_byte *
18084 read_full_die (const struct die_reader_specs *reader,
18085 struct die_info **diep, const gdb_byte *info_ptr)
18086 {
18087 const gdb_byte *result;
18088
18089 result = read_full_die_1 (reader, diep, info_ptr, 0);
18090
18091 if (dwarf_die_debug)
18092 {
18093 fprintf_unfiltered (gdb_stdlog,
18094 "Read die from %s@0x%x of %s:\n",
18095 reader->die_section->get_name (),
18096 (unsigned) (info_ptr - reader->die_section->buffer),
18097 bfd_get_filename (reader->abfd));
18098 dump_die (*diep, dwarf_die_debug);
18099 }
18100
18101 return result;
18102 }
18103 \f
18104
18105 /* Returns nonzero if TAG represents a type that we might generate a partial
18106 symbol for. */
18107
18108 static int
18109 is_type_tag_for_partial (int tag)
18110 {
18111 switch (tag)
18112 {
18113 #if 0
18114 /* Some types that would be reasonable to generate partial symbols for,
18115 that we don't at present. */
18116 case DW_TAG_array_type:
18117 case DW_TAG_file_type:
18118 case DW_TAG_ptr_to_member_type:
18119 case DW_TAG_set_type:
18120 case DW_TAG_string_type:
18121 case DW_TAG_subroutine_type:
18122 #endif
18123 case DW_TAG_base_type:
18124 case DW_TAG_class_type:
18125 case DW_TAG_interface_type:
18126 case DW_TAG_enumeration_type:
18127 case DW_TAG_structure_type:
18128 case DW_TAG_subrange_type:
18129 case DW_TAG_typedef:
18130 case DW_TAG_union_type:
18131 return 1;
18132 default:
18133 return 0;
18134 }
18135 }
18136
18137 /* Load all DIEs that are interesting for partial symbols into memory. */
18138
18139 static struct partial_die_info *
18140 load_partial_dies (const struct die_reader_specs *reader,
18141 const gdb_byte *info_ptr, int building_psymtab)
18142 {
18143 struct dwarf2_cu *cu = reader->cu;
18144 struct objfile *objfile = cu->per_objfile->objfile;
18145 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18146 unsigned int bytes_read;
18147 unsigned int load_all = 0;
18148 int nesting_level = 1;
18149
18150 parent_die = NULL;
18151 last_die = NULL;
18152
18153 gdb_assert (cu->per_cu != NULL);
18154 if (cu->per_cu->load_all_dies)
18155 load_all = 1;
18156
18157 cu->partial_dies
18158 = htab_create_alloc_ex (cu->header.length / 12,
18159 partial_die_hash,
18160 partial_die_eq,
18161 NULL,
18162 &cu->comp_unit_obstack,
18163 hashtab_obstack_allocate,
18164 dummy_obstack_deallocate);
18165
18166 while (1)
18167 {
18168 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18169
18170 /* A NULL abbrev means the end of a series of children. */
18171 if (abbrev == NULL)
18172 {
18173 if (--nesting_level == 0)
18174 return first_die;
18175
18176 info_ptr += bytes_read;
18177 last_die = parent_die;
18178 parent_die = parent_die->die_parent;
18179 continue;
18180 }
18181
18182 /* Check for template arguments. We never save these; if
18183 they're seen, we just mark the parent, and go on our way. */
18184 if (parent_die != NULL
18185 && cu->language == language_cplus
18186 && (abbrev->tag == DW_TAG_template_type_param
18187 || abbrev->tag == DW_TAG_template_value_param))
18188 {
18189 parent_die->has_template_arguments = 1;
18190
18191 if (!load_all)
18192 {
18193 /* We don't need a partial DIE for the template argument. */
18194 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18195 continue;
18196 }
18197 }
18198
18199 /* We only recurse into c++ subprograms looking for template arguments.
18200 Skip their other children. */
18201 if (!load_all
18202 && cu->language == language_cplus
18203 && parent_die != NULL
18204 && parent_die->tag == DW_TAG_subprogram)
18205 {
18206 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18207 continue;
18208 }
18209
18210 /* Check whether this DIE is interesting enough to save. Normally
18211 we would not be interested in members here, but there may be
18212 later variables referencing them via DW_AT_specification (for
18213 static members). */
18214 if (!load_all
18215 && !is_type_tag_for_partial (abbrev->tag)
18216 && abbrev->tag != DW_TAG_constant
18217 && abbrev->tag != DW_TAG_enumerator
18218 && abbrev->tag != DW_TAG_subprogram
18219 && abbrev->tag != DW_TAG_inlined_subroutine
18220 && abbrev->tag != DW_TAG_lexical_block
18221 && abbrev->tag != DW_TAG_variable
18222 && abbrev->tag != DW_TAG_namespace
18223 && abbrev->tag != DW_TAG_module
18224 && abbrev->tag != DW_TAG_member
18225 && abbrev->tag != DW_TAG_imported_unit
18226 && abbrev->tag != DW_TAG_imported_declaration)
18227 {
18228 /* Otherwise we skip to the next sibling, if any. */
18229 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18230 continue;
18231 }
18232
18233 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18234 abbrev);
18235
18236 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18237
18238 /* This two-pass algorithm for processing partial symbols has a
18239 high cost in cache pressure. Thus, handle some simple cases
18240 here which cover the majority of C partial symbols. DIEs
18241 which neither have specification tags in them, nor could have
18242 specification tags elsewhere pointing at them, can simply be
18243 processed and discarded.
18244
18245 This segment is also optional; scan_partial_symbols and
18246 add_partial_symbol will handle these DIEs if we chain
18247 them in normally. When compilers which do not emit large
18248 quantities of duplicate debug information are more common,
18249 this code can probably be removed. */
18250
18251 /* Any complete simple types at the top level (pretty much all
18252 of them, for a language without namespaces), can be processed
18253 directly. */
18254 if (parent_die == NULL
18255 && pdi.has_specification == 0
18256 && pdi.is_declaration == 0
18257 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18258 || pdi.tag == DW_TAG_base_type
18259 || pdi.tag == DW_TAG_subrange_type))
18260 {
18261 if (building_psymtab && pdi.name != NULL)
18262 add_psymbol_to_list (pdi.name, false,
18263 VAR_DOMAIN, LOC_TYPEDEF, -1,
18264 psymbol_placement::STATIC,
18265 0, cu->language, objfile);
18266 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18267 continue;
18268 }
18269
18270 /* The exception for DW_TAG_typedef with has_children above is
18271 a workaround of GCC PR debug/47510. In the case of this complaint
18272 type_name_or_error will error on such types later.
18273
18274 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18275 it could not find the child DIEs referenced later, this is checked
18276 above. In correct DWARF DW_TAG_typedef should have no children. */
18277
18278 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18279 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18280 "- DIE at %s [in module %s]"),
18281 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18282
18283 /* If we're at the second level, and we're an enumerator, and
18284 our parent has no specification (meaning possibly lives in a
18285 namespace elsewhere), then we can add the partial symbol now
18286 instead of queueing it. */
18287 if (pdi.tag == DW_TAG_enumerator
18288 && parent_die != NULL
18289 && parent_die->die_parent == NULL
18290 && parent_die->tag == DW_TAG_enumeration_type
18291 && parent_die->has_specification == 0)
18292 {
18293 if (pdi.name == NULL)
18294 complaint (_("malformed enumerator DIE ignored"));
18295 else if (building_psymtab)
18296 add_psymbol_to_list (pdi.name, false,
18297 VAR_DOMAIN, LOC_CONST, -1,
18298 cu->language == language_cplus
18299 ? psymbol_placement::GLOBAL
18300 : psymbol_placement::STATIC,
18301 0, cu->language, objfile);
18302
18303 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18304 continue;
18305 }
18306
18307 struct partial_die_info *part_die
18308 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18309
18310 /* We'll save this DIE so link it in. */
18311 part_die->die_parent = parent_die;
18312 part_die->die_sibling = NULL;
18313 part_die->die_child = NULL;
18314
18315 if (last_die && last_die == parent_die)
18316 last_die->die_child = part_die;
18317 else if (last_die)
18318 last_die->die_sibling = part_die;
18319
18320 last_die = part_die;
18321
18322 if (first_die == NULL)
18323 first_die = part_die;
18324
18325 /* Maybe add the DIE to the hash table. Not all DIEs that we
18326 find interesting need to be in the hash table, because we
18327 also have the parent/sibling/child chains; only those that we
18328 might refer to by offset later during partial symbol reading.
18329
18330 For now this means things that might have be the target of a
18331 DW_AT_specification, DW_AT_abstract_origin, or
18332 DW_AT_extension. DW_AT_extension will refer only to
18333 namespaces; DW_AT_abstract_origin refers to functions (and
18334 many things under the function DIE, but we do not recurse
18335 into function DIEs during partial symbol reading) and
18336 possibly variables as well; DW_AT_specification refers to
18337 declarations. Declarations ought to have the DW_AT_declaration
18338 flag. It happens that GCC forgets to put it in sometimes, but
18339 only for functions, not for types.
18340
18341 Adding more things than necessary to the hash table is harmless
18342 except for the performance cost. Adding too few will result in
18343 wasted time in find_partial_die, when we reread the compilation
18344 unit with load_all_dies set. */
18345
18346 if (load_all
18347 || abbrev->tag == DW_TAG_constant
18348 || abbrev->tag == DW_TAG_subprogram
18349 || abbrev->tag == DW_TAG_variable
18350 || abbrev->tag == DW_TAG_namespace
18351 || part_die->is_declaration)
18352 {
18353 void **slot;
18354
18355 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18356 to_underlying (part_die->sect_off),
18357 INSERT);
18358 *slot = part_die;
18359 }
18360
18361 /* For some DIEs we want to follow their children (if any). For C
18362 we have no reason to follow the children of structures; for other
18363 languages we have to, so that we can get at method physnames
18364 to infer fully qualified class names, for DW_AT_specification,
18365 and for C++ template arguments. For C++, we also look one level
18366 inside functions to find template arguments (if the name of the
18367 function does not already contain the template arguments).
18368
18369 For Ada and Fortran, we need to scan the children of subprograms
18370 and lexical blocks as well because these languages allow the
18371 definition of nested entities that could be interesting for the
18372 debugger, such as nested subprograms for instance. */
18373 if (last_die->has_children
18374 && (load_all
18375 || last_die->tag == DW_TAG_namespace
18376 || last_die->tag == DW_TAG_module
18377 || last_die->tag == DW_TAG_enumeration_type
18378 || (cu->language == language_cplus
18379 && last_die->tag == DW_TAG_subprogram
18380 && (last_die->name == NULL
18381 || strchr (last_die->name, '<') == NULL))
18382 || (cu->language != language_c
18383 && (last_die->tag == DW_TAG_class_type
18384 || last_die->tag == DW_TAG_interface_type
18385 || last_die->tag == DW_TAG_structure_type
18386 || last_die->tag == DW_TAG_union_type))
18387 || ((cu->language == language_ada
18388 || cu->language == language_fortran)
18389 && (last_die->tag == DW_TAG_subprogram
18390 || last_die->tag == DW_TAG_lexical_block))))
18391 {
18392 nesting_level++;
18393 parent_die = last_die;
18394 continue;
18395 }
18396
18397 /* Otherwise we skip to the next sibling, if any. */
18398 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18399
18400 /* Back to the top, do it again. */
18401 }
18402 }
18403
18404 partial_die_info::partial_die_info (sect_offset sect_off_,
18405 struct abbrev_info *abbrev)
18406 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18407 {
18408 }
18409
18410 /* Read a minimal amount of information into the minimal die structure.
18411 INFO_PTR should point just after the initial uleb128 of a DIE. */
18412
18413 const gdb_byte *
18414 partial_die_info::read (const struct die_reader_specs *reader,
18415 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18416 {
18417 struct dwarf2_cu *cu = reader->cu;
18418 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18419 unsigned int i;
18420 int has_low_pc_attr = 0;
18421 int has_high_pc_attr = 0;
18422 int high_pc_relative = 0;
18423
18424 for (i = 0; i < abbrev.num_attrs; ++i)
18425 {
18426 attribute attr;
18427 bool need_reprocess;
18428 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18429 info_ptr, &need_reprocess);
18430 /* String and address offsets that need to do the reprocessing have
18431 already been read at this point, so there is no need to wait until
18432 the loop terminates to do the reprocessing. */
18433 if (need_reprocess)
18434 read_attribute_reprocess (reader, &attr);
18435 /* Store the data if it is of an attribute we want to keep in a
18436 partial symbol table. */
18437 switch (attr.name)
18438 {
18439 case DW_AT_name:
18440 switch (tag)
18441 {
18442 case DW_TAG_compile_unit:
18443 case DW_TAG_partial_unit:
18444 case DW_TAG_type_unit:
18445 /* Compilation units have a DW_AT_name that is a filename, not
18446 a source language identifier. */
18447 case DW_TAG_enumeration_type:
18448 case DW_TAG_enumerator:
18449 /* These tags always have simple identifiers already; no need
18450 to canonicalize them. */
18451 name = DW_STRING (&attr);
18452 break;
18453 default:
18454 {
18455 struct objfile *objfile = dwarf2_per_objfile->objfile;
18456
18457 name
18458 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18459 }
18460 break;
18461 }
18462 break;
18463 case DW_AT_linkage_name:
18464 case DW_AT_MIPS_linkage_name:
18465 /* Note that both forms of linkage name might appear. We
18466 assume they will be the same, and we only store the last
18467 one we see. */
18468 linkage_name = attr.value_as_string ();
18469 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18470 See https://github.com/rust-lang/rust/issues/32925. */
18471 if (cu->language == language_rust && linkage_name != NULL
18472 && strchr (linkage_name, '{') != NULL)
18473 linkage_name = NULL;
18474 break;
18475 case DW_AT_low_pc:
18476 has_low_pc_attr = 1;
18477 lowpc = attr.value_as_address ();
18478 break;
18479 case DW_AT_high_pc:
18480 has_high_pc_attr = 1;
18481 highpc = attr.value_as_address ();
18482 if (cu->header.version >= 4 && attr.form_is_constant ())
18483 high_pc_relative = 1;
18484 break;
18485 case DW_AT_location:
18486 /* Support the .debug_loc offsets. */
18487 if (attr.form_is_block ())
18488 {
18489 d.locdesc = DW_BLOCK (&attr);
18490 }
18491 else if (attr.form_is_section_offset ())
18492 {
18493 dwarf2_complex_location_expr_complaint ();
18494 }
18495 else
18496 {
18497 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18498 "partial symbol information");
18499 }
18500 break;
18501 case DW_AT_external:
18502 is_external = DW_UNSND (&attr);
18503 break;
18504 case DW_AT_declaration:
18505 is_declaration = DW_UNSND (&attr);
18506 break;
18507 case DW_AT_type:
18508 has_type = 1;
18509 break;
18510 case DW_AT_abstract_origin:
18511 case DW_AT_specification:
18512 case DW_AT_extension:
18513 has_specification = 1;
18514 spec_offset = attr.get_ref_die_offset ();
18515 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18516 || cu->per_cu->is_dwz);
18517 break;
18518 case DW_AT_sibling:
18519 /* Ignore absolute siblings, they might point outside of
18520 the current compile unit. */
18521 if (attr.form == DW_FORM_ref_addr)
18522 complaint (_("ignoring absolute DW_AT_sibling"));
18523 else
18524 {
18525 const gdb_byte *buffer = reader->buffer;
18526 sect_offset off = attr.get_ref_die_offset ();
18527 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18528
18529 if (sibling_ptr < info_ptr)
18530 complaint (_("DW_AT_sibling points backwards"));
18531 else if (sibling_ptr > reader->buffer_end)
18532 reader->die_section->overflow_complaint ();
18533 else
18534 sibling = sibling_ptr;
18535 }
18536 break;
18537 case DW_AT_byte_size:
18538 has_byte_size = 1;
18539 break;
18540 case DW_AT_const_value:
18541 has_const_value = 1;
18542 break;
18543 case DW_AT_calling_convention:
18544 /* DWARF doesn't provide a way to identify a program's source-level
18545 entry point. DW_AT_calling_convention attributes are only meant
18546 to describe functions' calling conventions.
18547
18548 However, because it's a necessary piece of information in
18549 Fortran, and before DWARF 4 DW_CC_program was the only
18550 piece of debugging information whose definition refers to
18551 a 'main program' at all, several compilers marked Fortran
18552 main programs with DW_CC_program --- even when those
18553 functions use the standard calling conventions.
18554
18555 Although DWARF now specifies a way to provide this
18556 information, we support this practice for backward
18557 compatibility. */
18558 if (DW_UNSND (&attr) == DW_CC_program
18559 && cu->language == language_fortran)
18560 main_subprogram = 1;
18561 break;
18562 case DW_AT_inline:
18563 if (DW_UNSND (&attr) == DW_INL_inlined
18564 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18565 may_be_inlined = 1;
18566 break;
18567
18568 case DW_AT_import:
18569 if (tag == DW_TAG_imported_unit)
18570 {
18571 d.sect_off = attr.get_ref_die_offset ();
18572 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18573 || cu->per_cu->is_dwz);
18574 }
18575 break;
18576
18577 case DW_AT_main_subprogram:
18578 main_subprogram = DW_UNSND (&attr);
18579 break;
18580
18581 case DW_AT_ranges:
18582 {
18583 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18584 but that requires a full DIE, so instead we just
18585 reimplement it. */
18586 int need_ranges_base = tag != DW_TAG_compile_unit;
18587 unsigned int ranges_offset = (DW_UNSND (&attr)
18588 + (need_ranges_base
18589 ? cu->ranges_base
18590 : 0));
18591
18592 /* Value of the DW_AT_ranges attribute is the offset in the
18593 .debug_ranges section. */
18594 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18595 nullptr))
18596 has_pc_info = 1;
18597 }
18598 break;
18599
18600 default:
18601 break;
18602 }
18603 }
18604
18605 /* For Ada, if both the name and the linkage name appear, we prefer
18606 the latter. This lets "catch exception" work better, regardless
18607 of the order in which the name and linkage name were emitted.
18608 Really, though, this is just a workaround for the fact that gdb
18609 doesn't store both the name and the linkage name. */
18610 if (cu->language == language_ada && linkage_name != nullptr)
18611 name = linkage_name;
18612
18613 if (high_pc_relative)
18614 highpc += lowpc;
18615
18616 if (has_low_pc_attr && has_high_pc_attr)
18617 {
18618 /* When using the GNU linker, .gnu.linkonce. sections are used to
18619 eliminate duplicate copies of functions and vtables and such.
18620 The linker will arbitrarily choose one and discard the others.
18621 The AT_*_pc values for such functions refer to local labels in
18622 these sections. If the section from that file was discarded, the
18623 labels are not in the output, so the relocs get a value of 0.
18624 If this is a discarded function, mark the pc bounds as invalid,
18625 so that GDB will ignore it. */
18626 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
18627 {
18628 struct objfile *objfile = dwarf2_per_objfile->objfile;
18629 struct gdbarch *gdbarch = objfile->arch ();
18630
18631 complaint (_("DW_AT_low_pc %s is zero "
18632 "for DIE at %s [in module %s]"),
18633 paddress (gdbarch, lowpc),
18634 sect_offset_str (sect_off),
18635 objfile_name (objfile));
18636 }
18637 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18638 else if (lowpc >= highpc)
18639 {
18640 struct objfile *objfile = dwarf2_per_objfile->objfile;
18641 struct gdbarch *gdbarch = objfile->arch ();
18642
18643 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18644 "for DIE at %s [in module %s]"),
18645 paddress (gdbarch, lowpc),
18646 paddress (gdbarch, highpc),
18647 sect_offset_str (sect_off),
18648 objfile_name (objfile));
18649 }
18650 else
18651 has_pc_info = 1;
18652 }
18653
18654 return info_ptr;
18655 }
18656
18657 /* Find a cached partial DIE at OFFSET in CU. */
18658
18659 struct partial_die_info *
18660 dwarf2_cu::find_partial_die (sect_offset sect_off)
18661 {
18662 struct partial_die_info *lookup_die = NULL;
18663 struct partial_die_info part_die (sect_off);
18664
18665 lookup_die = ((struct partial_die_info *)
18666 htab_find_with_hash (partial_dies, &part_die,
18667 to_underlying (sect_off)));
18668
18669 return lookup_die;
18670 }
18671
18672 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18673 except in the case of .debug_types DIEs which do not reference
18674 outside their CU (they do however referencing other types via
18675 DW_FORM_ref_sig8). */
18676
18677 static const struct cu_partial_die_info
18678 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18679 {
18680 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18681 struct objfile *objfile = dwarf2_per_objfile->objfile;
18682 struct dwarf2_per_cu_data *per_cu = NULL;
18683 struct partial_die_info *pd = NULL;
18684
18685 if (offset_in_dwz == cu->per_cu->is_dwz
18686 && cu->header.offset_in_cu_p (sect_off))
18687 {
18688 pd = cu->find_partial_die (sect_off);
18689 if (pd != NULL)
18690 return { cu, pd };
18691 /* We missed recording what we needed.
18692 Load all dies and try again. */
18693 per_cu = cu->per_cu;
18694 }
18695 else
18696 {
18697 /* TUs don't reference other CUs/TUs (except via type signatures). */
18698 if (cu->per_cu->is_debug_types)
18699 {
18700 error (_("Dwarf Error: Type Unit at offset %s contains"
18701 " external reference to offset %s [in module %s].\n"),
18702 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18703 bfd_get_filename (objfile->obfd));
18704 }
18705 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18706 dwarf2_per_objfile);
18707
18708 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18709 load_partial_comp_unit (per_cu, cu->per_objfile);
18710
18711 per_cu->cu->last_used = 0;
18712 pd = per_cu->cu->find_partial_die (sect_off);
18713 }
18714
18715 /* If we didn't find it, and not all dies have been loaded,
18716 load them all and try again. */
18717
18718 if (pd == NULL && per_cu->load_all_dies == 0)
18719 {
18720 per_cu->load_all_dies = 1;
18721
18722 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18723 THIS_CU->cu may already be in use. So we can't just free it and
18724 replace its DIEs with the ones we read in. Instead, we leave those
18725 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18726 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18727 set. */
18728 load_partial_comp_unit (per_cu, cu->per_objfile);
18729
18730 pd = per_cu->cu->find_partial_die (sect_off);
18731 }
18732
18733 if (pd == NULL)
18734 internal_error (__FILE__, __LINE__,
18735 _("could not find partial DIE %s "
18736 "in cache [from module %s]\n"),
18737 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18738 return { per_cu->cu, pd };
18739 }
18740
18741 /* See if we can figure out if the class lives in a namespace. We do
18742 this by looking for a member function; its demangled name will
18743 contain namespace info, if there is any. */
18744
18745 static void
18746 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18747 struct dwarf2_cu *cu)
18748 {
18749 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18750 what template types look like, because the demangler
18751 frequently doesn't give the same name as the debug info. We
18752 could fix this by only using the demangled name to get the
18753 prefix (but see comment in read_structure_type). */
18754
18755 struct partial_die_info *real_pdi;
18756 struct partial_die_info *child_pdi;
18757
18758 /* If this DIE (this DIE's specification, if any) has a parent, then
18759 we should not do this. We'll prepend the parent's fully qualified
18760 name when we create the partial symbol. */
18761
18762 real_pdi = struct_pdi;
18763 while (real_pdi->has_specification)
18764 {
18765 auto res = find_partial_die (real_pdi->spec_offset,
18766 real_pdi->spec_is_dwz, cu);
18767 real_pdi = res.pdi;
18768 cu = res.cu;
18769 }
18770
18771 if (real_pdi->die_parent != NULL)
18772 return;
18773
18774 for (child_pdi = struct_pdi->die_child;
18775 child_pdi != NULL;
18776 child_pdi = child_pdi->die_sibling)
18777 {
18778 if (child_pdi->tag == DW_TAG_subprogram
18779 && child_pdi->linkage_name != NULL)
18780 {
18781 gdb::unique_xmalloc_ptr<char> actual_class_name
18782 (language_class_name_from_physname (cu->language_defn,
18783 child_pdi->linkage_name));
18784 if (actual_class_name != NULL)
18785 {
18786 struct objfile *objfile = cu->per_objfile->objfile;
18787 struct_pdi->name = objfile->intern (actual_class_name.get ());
18788 }
18789 break;
18790 }
18791 }
18792 }
18793
18794 /* Return true if a DIE with TAG may have the DW_AT_const_value
18795 attribute. */
18796
18797 static bool
18798 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18799 {
18800 switch (tag)
18801 {
18802 case DW_TAG_constant:
18803 case DW_TAG_enumerator:
18804 case DW_TAG_formal_parameter:
18805 case DW_TAG_template_value_param:
18806 case DW_TAG_variable:
18807 return true;
18808 }
18809
18810 return false;
18811 }
18812
18813 void
18814 partial_die_info::fixup (struct dwarf2_cu *cu)
18815 {
18816 /* Once we've fixed up a die, there's no point in doing so again.
18817 This also avoids a memory leak if we were to call
18818 guess_partial_die_structure_name multiple times. */
18819 if (fixup_called)
18820 return;
18821
18822 /* If we found a reference attribute and the DIE has no name, try
18823 to find a name in the referred to DIE. */
18824
18825 if (name == NULL && has_specification)
18826 {
18827 struct partial_die_info *spec_die;
18828
18829 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18830 spec_die = res.pdi;
18831 cu = res.cu;
18832
18833 spec_die->fixup (cu);
18834
18835 if (spec_die->name)
18836 {
18837 name = spec_die->name;
18838
18839 /* Copy DW_AT_external attribute if it is set. */
18840 if (spec_die->is_external)
18841 is_external = spec_die->is_external;
18842 }
18843 }
18844
18845 if (!has_const_value && has_specification
18846 && can_have_DW_AT_const_value_p (tag))
18847 {
18848 struct partial_die_info *spec_die;
18849
18850 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18851 spec_die = res.pdi;
18852 cu = res.cu;
18853
18854 spec_die->fixup (cu);
18855
18856 if (spec_die->has_const_value)
18857 {
18858 /* Copy DW_AT_const_value attribute if it is set. */
18859 has_const_value = spec_die->has_const_value;
18860 }
18861 }
18862
18863 /* Set default names for some unnamed DIEs. */
18864
18865 if (name == NULL && tag == DW_TAG_namespace)
18866 name = CP_ANONYMOUS_NAMESPACE_STR;
18867
18868 /* If there is no parent die to provide a namespace, and there are
18869 children, see if we can determine the namespace from their linkage
18870 name. */
18871 if (cu->language == language_cplus
18872 && !cu->per_objfile->per_bfd->types.empty ()
18873 && die_parent == NULL
18874 && has_children
18875 && (tag == DW_TAG_class_type
18876 || tag == DW_TAG_structure_type
18877 || tag == DW_TAG_union_type))
18878 guess_partial_die_structure_name (this, cu);
18879
18880 /* GCC might emit a nameless struct or union that has a linkage
18881 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18882 if (name == NULL
18883 && (tag == DW_TAG_class_type
18884 || tag == DW_TAG_interface_type
18885 || tag == DW_TAG_structure_type
18886 || tag == DW_TAG_union_type)
18887 && linkage_name != NULL)
18888 {
18889 gdb::unique_xmalloc_ptr<char> demangled
18890 (gdb_demangle (linkage_name, DMGL_TYPES));
18891 if (demangled != nullptr)
18892 {
18893 const char *base;
18894
18895 /* Strip any leading namespaces/classes, keep only the base name.
18896 DW_AT_name for named DIEs does not contain the prefixes. */
18897 base = strrchr (demangled.get (), ':');
18898 if (base && base > demangled.get () && base[-1] == ':')
18899 base++;
18900 else
18901 base = demangled.get ();
18902
18903 struct objfile *objfile = cu->per_objfile->objfile;
18904 name = objfile->intern (base);
18905 }
18906 }
18907
18908 fixup_called = 1;
18909 }
18910
18911 /* Read the .debug_loclists header contents from the given SECTION in the
18912 HEADER. */
18913 static void
18914 read_loclist_header (struct loclist_header *header,
18915 struct dwarf2_section_info *section)
18916 {
18917 unsigned int bytes_read;
18918 bfd *abfd = section->get_bfd_owner ();
18919 const gdb_byte *info_ptr = section->buffer;
18920 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18921 info_ptr += bytes_read;
18922 header->version = read_2_bytes (abfd, info_ptr);
18923 info_ptr += 2;
18924 header->addr_size = read_1_byte (abfd, info_ptr);
18925 info_ptr += 1;
18926 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18927 info_ptr += 1;
18928 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18929 }
18930
18931 /* Return the DW_AT_loclists_base value for the CU. */
18932 static ULONGEST
18933 lookup_loclist_base (struct dwarf2_cu *cu)
18934 {
18935 /* For the .dwo unit, the loclist_base points to the first offset following
18936 the header. The header consists of the following entities-
18937 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18938 bit format)
18939 2. version (2 bytes)
18940 3. address size (1 byte)
18941 4. segment selector size (1 byte)
18942 5. offset entry count (4 bytes)
18943 These sizes are derived as per the DWARFv5 standard. */
18944 if (cu->dwo_unit != nullptr)
18945 {
18946 if (cu->header.initial_length_size == 4)
18947 return LOCLIST_HEADER_SIZE32;
18948 return LOCLIST_HEADER_SIZE64;
18949 }
18950 return cu->loclist_base;
18951 }
18952
18953 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18954 array of offsets in the .debug_loclists section. */
18955 static CORE_ADDR
18956 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18957 {
18958 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18959 struct objfile *objfile = dwarf2_per_objfile->objfile;
18960 bfd *abfd = objfile->obfd;
18961 ULONGEST loclist_base = lookup_loclist_base (cu);
18962 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18963
18964 section->read (objfile);
18965 if (section->buffer == NULL)
18966 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18967 "section [in module %s]"), objfile_name (objfile));
18968 struct loclist_header header;
18969 read_loclist_header (&header, section);
18970 if (loclist_index >= header.offset_entry_count)
18971 complaint (_("DW_FORM_loclistx pointing outside of "
18972 ".debug_loclists offset array [in module %s]"),
18973 objfile_name (objfile));
18974 if (loclist_base + loclist_index * cu->header.offset_size
18975 >= section->size)
18976 complaint (_("DW_FORM_loclistx pointing outside of "
18977 ".debug_loclists section [in module %s]"),
18978 objfile_name (objfile));
18979 const gdb_byte *info_ptr
18980 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18981
18982 if (cu->header.offset_size == 4)
18983 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18984 else
18985 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18986 }
18987
18988 /* Process the attributes that had to be skipped in the first round. These
18989 attributes are the ones that need str_offsets_base or addr_base attributes.
18990 They could not have been processed in the first round, because at the time
18991 the values of str_offsets_base or addr_base may not have been known. */
18992 static void
18993 read_attribute_reprocess (const struct die_reader_specs *reader,
18994 struct attribute *attr)
18995 {
18996 struct dwarf2_cu *cu = reader->cu;
18997 switch (attr->form)
18998 {
18999 case DW_FORM_addrx:
19000 case DW_FORM_GNU_addr_index:
19001 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19002 break;
19003 case DW_FORM_loclistx:
19004 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19005 break;
19006 case DW_FORM_strx:
19007 case DW_FORM_strx1:
19008 case DW_FORM_strx2:
19009 case DW_FORM_strx3:
19010 case DW_FORM_strx4:
19011 case DW_FORM_GNU_str_index:
19012 {
19013 unsigned int str_index = DW_UNSND (attr);
19014 if (reader->dwo_file != NULL)
19015 {
19016 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19017 DW_STRING_IS_CANONICAL (attr) = 0;
19018 }
19019 else
19020 {
19021 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19022 DW_STRING_IS_CANONICAL (attr) = 0;
19023 }
19024 break;
19025 }
19026 default:
19027 gdb_assert_not_reached (_("Unexpected DWARF form."));
19028 }
19029 }
19030
19031 /* Read an attribute value described by an attribute form. */
19032
19033 static const gdb_byte *
19034 read_attribute_value (const struct die_reader_specs *reader,
19035 struct attribute *attr, unsigned form,
19036 LONGEST implicit_const, const gdb_byte *info_ptr,
19037 bool *need_reprocess)
19038 {
19039 struct dwarf2_cu *cu = reader->cu;
19040 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19041 struct objfile *objfile = dwarf2_per_objfile->objfile;
19042 bfd *abfd = reader->abfd;
19043 struct comp_unit_head *cu_header = &cu->header;
19044 unsigned int bytes_read;
19045 struct dwarf_block *blk;
19046 *need_reprocess = false;
19047
19048 attr->form = (enum dwarf_form) form;
19049 switch (form)
19050 {
19051 case DW_FORM_ref_addr:
19052 if (cu->header.version == 2)
19053 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19054 &bytes_read);
19055 else
19056 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19057 &bytes_read);
19058 info_ptr += bytes_read;
19059 break;
19060 case DW_FORM_GNU_ref_alt:
19061 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19062 info_ptr += bytes_read;
19063 break;
19064 case DW_FORM_addr:
19065 {
19066 struct gdbarch *gdbarch = objfile->arch ();
19067 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19068 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19069 info_ptr += bytes_read;
19070 }
19071 break;
19072 case DW_FORM_block2:
19073 blk = dwarf_alloc_block (cu);
19074 blk->size = read_2_bytes (abfd, info_ptr);
19075 info_ptr += 2;
19076 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19077 info_ptr += blk->size;
19078 DW_BLOCK (attr) = blk;
19079 break;
19080 case DW_FORM_block4:
19081 blk = dwarf_alloc_block (cu);
19082 blk->size = read_4_bytes (abfd, info_ptr);
19083 info_ptr += 4;
19084 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19085 info_ptr += blk->size;
19086 DW_BLOCK (attr) = blk;
19087 break;
19088 case DW_FORM_data2:
19089 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19090 info_ptr += 2;
19091 break;
19092 case DW_FORM_data4:
19093 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19094 info_ptr += 4;
19095 break;
19096 case DW_FORM_data8:
19097 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19098 info_ptr += 8;
19099 break;
19100 case DW_FORM_data16:
19101 blk = dwarf_alloc_block (cu);
19102 blk->size = 16;
19103 blk->data = read_n_bytes (abfd, info_ptr, 16);
19104 info_ptr += 16;
19105 DW_BLOCK (attr) = blk;
19106 break;
19107 case DW_FORM_sec_offset:
19108 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19109 info_ptr += bytes_read;
19110 break;
19111 case DW_FORM_loclistx:
19112 {
19113 *need_reprocess = true;
19114 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19115 info_ptr += bytes_read;
19116 }
19117 break;
19118 case DW_FORM_string:
19119 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19120 DW_STRING_IS_CANONICAL (attr) = 0;
19121 info_ptr += bytes_read;
19122 break;
19123 case DW_FORM_strp:
19124 if (!cu->per_cu->is_dwz)
19125 {
19126 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19127 abfd, info_ptr, cu_header,
19128 &bytes_read);
19129 DW_STRING_IS_CANONICAL (attr) = 0;
19130 info_ptr += bytes_read;
19131 break;
19132 }
19133 /* FALLTHROUGH */
19134 case DW_FORM_line_strp:
19135 if (!cu->per_cu->is_dwz)
19136 {
19137 DW_STRING (attr)
19138 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19139 &bytes_read);
19140 DW_STRING_IS_CANONICAL (attr) = 0;
19141 info_ptr += bytes_read;
19142 break;
19143 }
19144 /* FALLTHROUGH */
19145 case DW_FORM_GNU_strp_alt:
19146 {
19147 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19148 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19149 &bytes_read);
19150
19151 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
19152 DW_STRING_IS_CANONICAL (attr) = 0;
19153 info_ptr += bytes_read;
19154 }
19155 break;
19156 case DW_FORM_exprloc:
19157 case DW_FORM_block:
19158 blk = dwarf_alloc_block (cu);
19159 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19160 info_ptr += bytes_read;
19161 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19162 info_ptr += blk->size;
19163 DW_BLOCK (attr) = blk;
19164 break;
19165 case DW_FORM_block1:
19166 blk = dwarf_alloc_block (cu);
19167 blk->size = read_1_byte (abfd, info_ptr);
19168 info_ptr += 1;
19169 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19170 info_ptr += blk->size;
19171 DW_BLOCK (attr) = blk;
19172 break;
19173 case DW_FORM_data1:
19174 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19175 info_ptr += 1;
19176 break;
19177 case DW_FORM_flag:
19178 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19179 info_ptr += 1;
19180 break;
19181 case DW_FORM_flag_present:
19182 DW_UNSND (attr) = 1;
19183 break;
19184 case DW_FORM_sdata:
19185 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19186 info_ptr += bytes_read;
19187 break;
19188 case DW_FORM_udata:
19189 case DW_FORM_rnglistx:
19190 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19191 info_ptr += bytes_read;
19192 break;
19193 case DW_FORM_ref1:
19194 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19195 + read_1_byte (abfd, info_ptr));
19196 info_ptr += 1;
19197 break;
19198 case DW_FORM_ref2:
19199 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19200 + read_2_bytes (abfd, info_ptr));
19201 info_ptr += 2;
19202 break;
19203 case DW_FORM_ref4:
19204 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19205 + read_4_bytes (abfd, info_ptr));
19206 info_ptr += 4;
19207 break;
19208 case DW_FORM_ref8:
19209 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19210 + read_8_bytes (abfd, info_ptr));
19211 info_ptr += 8;
19212 break;
19213 case DW_FORM_ref_sig8:
19214 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19215 info_ptr += 8;
19216 break;
19217 case DW_FORM_ref_udata:
19218 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19219 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19220 info_ptr += bytes_read;
19221 break;
19222 case DW_FORM_indirect:
19223 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19224 info_ptr += bytes_read;
19225 if (form == DW_FORM_implicit_const)
19226 {
19227 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19228 info_ptr += bytes_read;
19229 }
19230 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19231 info_ptr, need_reprocess);
19232 break;
19233 case DW_FORM_implicit_const:
19234 DW_SND (attr) = implicit_const;
19235 break;
19236 case DW_FORM_addrx:
19237 case DW_FORM_GNU_addr_index:
19238 *need_reprocess = true;
19239 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19240 info_ptr += bytes_read;
19241 break;
19242 case DW_FORM_strx:
19243 case DW_FORM_strx1:
19244 case DW_FORM_strx2:
19245 case DW_FORM_strx3:
19246 case DW_FORM_strx4:
19247 case DW_FORM_GNU_str_index:
19248 {
19249 ULONGEST str_index;
19250 if (form == DW_FORM_strx1)
19251 {
19252 str_index = read_1_byte (abfd, info_ptr);
19253 info_ptr += 1;
19254 }
19255 else if (form == DW_FORM_strx2)
19256 {
19257 str_index = read_2_bytes (abfd, info_ptr);
19258 info_ptr += 2;
19259 }
19260 else if (form == DW_FORM_strx3)
19261 {
19262 str_index = read_3_bytes (abfd, info_ptr);
19263 info_ptr += 3;
19264 }
19265 else if (form == DW_FORM_strx4)
19266 {
19267 str_index = read_4_bytes (abfd, info_ptr);
19268 info_ptr += 4;
19269 }
19270 else
19271 {
19272 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19273 info_ptr += bytes_read;
19274 }
19275 *need_reprocess = true;
19276 DW_UNSND (attr) = str_index;
19277 }
19278 break;
19279 default:
19280 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19281 dwarf_form_name (form),
19282 bfd_get_filename (abfd));
19283 }
19284
19285 /* Super hack. */
19286 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19287 attr->form = DW_FORM_GNU_ref_alt;
19288
19289 /* We have seen instances where the compiler tried to emit a byte
19290 size attribute of -1 which ended up being encoded as an unsigned
19291 0xffffffff. Although 0xffffffff is technically a valid size value,
19292 an object of this size seems pretty unlikely so we can relatively
19293 safely treat these cases as if the size attribute was invalid and
19294 treat them as zero by default. */
19295 if (attr->name == DW_AT_byte_size
19296 && form == DW_FORM_data4
19297 && DW_UNSND (attr) >= 0xffffffff)
19298 {
19299 complaint
19300 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19301 hex_string (DW_UNSND (attr)));
19302 DW_UNSND (attr) = 0;
19303 }
19304
19305 return info_ptr;
19306 }
19307
19308 /* Read an attribute described by an abbreviated attribute. */
19309
19310 static const gdb_byte *
19311 read_attribute (const struct die_reader_specs *reader,
19312 struct attribute *attr, struct attr_abbrev *abbrev,
19313 const gdb_byte *info_ptr, bool *need_reprocess)
19314 {
19315 attr->name = abbrev->name;
19316 return read_attribute_value (reader, attr, abbrev->form,
19317 abbrev->implicit_const, info_ptr,
19318 need_reprocess);
19319 }
19320
19321 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19322
19323 static const char *
19324 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19325 LONGEST str_offset)
19326 {
19327 return dwarf2_per_objfile->per_bfd->str.read_string
19328 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
19329 }
19330
19331 /* Return pointer to string at .debug_str offset as read from BUF.
19332 BUF is assumed to be in a compilation unit described by CU_HEADER.
19333 Return *BYTES_READ_PTR count of bytes read from BUF. */
19334
19335 static const char *
19336 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19337 const gdb_byte *buf,
19338 const struct comp_unit_head *cu_header,
19339 unsigned int *bytes_read_ptr)
19340 {
19341 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19342
19343 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
19344 }
19345
19346 /* See read.h. */
19347
19348 const char *
19349 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19350 const struct comp_unit_head *cu_header,
19351 unsigned int *bytes_read_ptr)
19352 {
19353 bfd *abfd = objfile->obfd;
19354 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19355
19356 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19357 }
19358
19359 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19360 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19361 ADDR_SIZE is the size of addresses from the CU header. */
19362
19363 static CORE_ADDR
19364 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19365 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19366 int addr_size)
19367 {
19368 struct objfile *objfile = dwarf2_per_objfile->objfile;
19369 bfd *abfd = objfile->obfd;
19370 const gdb_byte *info_ptr;
19371 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19372
19373 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19374 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
19375 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19376 objfile_name (objfile));
19377 if (addr_base_or_zero + addr_index * addr_size
19378 >= dwarf2_per_objfile->per_bfd->addr.size)
19379 error (_("DW_FORM_addr_index pointing outside of "
19380 ".debug_addr section [in module %s]"),
19381 objfile_name (objfile));
19382 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
19383 + addr_base_or_zero + addr_index * addr_size);
19384 if (addr_size == 4)
19385 return bfd_get_32 (abfd, info_ptr);
19386 else
19387 return bfd_get_64 (abfd, info_ptr);
19388 }
19389
19390 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19391
19392 static CORE_ADDR
19393 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19394 {
19395 return read_addr_index_1 (cu->per_objfile, addr_index,
19396 cu->addr_base, cu->header.addr_size);
19397 }
19398
19399 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19400
19401 static CORE_ADDR
19402 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19403 unsigned int *bytes_read)
19404 {
19405 bfd *abfd = cu->per_objfile->objfile->obfd;
19406 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19407
19408 return read_addr_index (cu, addr_index);
19409 }
19410
19411 /* See read.h. */
19412
19413 CORE_ADDR
19414 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19415 dwarf2_per_objfile *dwarf2_per_objfile,
19416 unsigned int addr_index)
19417 {
19418 struct dwarf2_cu *cu = per_cu->cu;
19419 gdb::optional<ULONGEST> addr_base;
19420 int addr_size;
19421
19422 /* We need addr_base and addr_size.
19423 If we don't have PER_CU->cu, we have to get it.
19424 Nasty, but the alternative is storing the needed info in PER_CU,
19425 which at this point doesn't seem justified: it's not clear how frequently
19426 it would get used and it would increase the size of every PER_CU.
19427 Entry points like dwarf2_per_cu_addr_size do a similar thing
19428 so we're not in uncharted territory here.
19429 Alas we need to be a bit more complicated as addr_base is contained
19430 in the DIE.
19431
19432 We don't need to read the entire CU(/TU).
19433 We just need the header and top level die.
19434
19435 IWBN to use the aging mechanism to let us lazily later discard the CU.
19436 For now we skip this optimization. */
19437
19438 if (cu != NULL)
19439 {
19440 addr_base = cu->addr_base;
19441 addr_size = cu->header.addr_size;
19442 }
19443 else
19444 {
19445 cutu_reader reader (per_cu, dwarf2_per_objfile, NULL, 0, false);
19446 addr_base = reader.cu->addr_base;
19447 addr_size = reader.cu->header.addr_size;
19448 }
19449
19450 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19451 addr_size);
19452 }
19453
19454 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19455 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19456 DWO file. */
19457
19458 static const char *
19459 read_str_index (struct dwarf2_cu *cu,
19460 struct dwarf2_section_info *str_section,
19461 struct dwarf2_section_info *str_offsets_section,
19462 ULONGEST str_offsets_base, ULONGEST str_index)
19463 {
19464 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19465 struct objfile *objfile = dwarf2_per_objfile->objfile;
19466 const char *objf_name = objfile_name (objfile);
19467 bfd *abfd = objfile->obfd;
19468 const gdb_byte *info_ptr;
19469 ULONGEST str_offset;
19470 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19471
19472 str_section->read (objfile);
19473 str_offsets_section->read (objfile);
19474 if (str_section->buffer == NULL)
19475 error (_("%s used without %s section"
19476 " in CU at offset %s [in module %s]"),
19477 form_name, str_section->get_name (),
19478 sect_offset_str (cu->header.sect_off), objf_name);
19479 if (str_offsets_section->buffer == NULL)
19480 error (_("%s used without %s section"
19481 " in CU at offset %s [in module %s]"),
19482 form_name, str_section->get_name (),
19483 sect_offset_str (cu->header.sect_off), objf_name);
19484 info_ptr = (str_offsets_section->buffer
19485 + str_offsets_base
19486 + str_index * cu->header.offset_size);
19487 if (cu->header.offset_size == 4)
19488 str_offset = bfd_get_32 (abfd, info_ptr);
19489 else
19490 str_offset = bfd_get_64 (abfd, info_ptr);
19491 if (str_offset >= str_section->size)
19492 error (_("Offset from %s pointing outside of"
19493 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19494 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19495 return (const char *) (str_section->buffer + str_offset);
19496 }
19497
19498 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19499
19500 static const char *
19501 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19502 {
19503 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19504 ? reader->cu->header.addr_size : 0;
19505 return read_str_index (reader->cu,
19506 &reader->dwo_file->sections.str,
19507 &reader->dwo_file->sections.str_offsets,
19508 str_offsets_base, str_index);
19509 }
19510
19511 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19512
19513 static const char *
19514 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19515 {
19516 struct objfile *objfile = cu->per_objfile->objfile;
19517 const char *objf_name = objfile_name (objfile);
19518 static const char form_name[] = "DW_FORM_GNU_str_index";
19519 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19520
19521 if (!cu->str_offsets_base.has_value ())
19522 error (_("%s used in Fission stub without %s"
19523 " in CU at offset 0x%lx [in module %s]"),
19524 form_name, str_offsets_attr_name,
19525 (long) cu->header.offset_size, objf_name);
19526
19527 return read_str_index (cu,
19528 &cu->per_objfile->per_bfd->str,
19529 &cu->per_objfile->per_bfd->str_offsets,
19530 *cu->str_offsets_base, str_index);
19531 }
19532
19533 /* Return the length of an LEB128 number in BUF. */
19534
19535 static int
19536 leb128_size (const gdb_byte *buf)
19537 {
19538 const gdb_byte *begin = buf;
19539 gdb_byte byte;
19540
19541 while (1)
19542 {
19543 byte = *buf++;
19544 if ((byte & 128) == 0)
19545 return buf - begin;
19546 }
19547 }
19548
19549 static void
19550 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19551 {
19552 switch (lang)
19553 {
19554 case DW_LANG_C89:
19555 case DW_LANG_C99:
19556 case DW_LANG_C11:
19557 case DW_LANG_C:
19558 case DW_LANG_UPC:
19559 cu->language = language_c;
19560 break;
19561 case DW_LANG_Java:
19562 case DW_LANG_C_plus_plus:
19563 case DW_LANG_C_plus_plus_11:
19564 case DW_LANG_C_plus_plus_14:
19565 cu->language = language_cplus;
19566 break;
19567 case DW_LANG_D:
19568 cu->language = language_d;
19569 break;
19570 case DW_LANG_Fortran77:
19571 case DW_LANG_Fortran90:
19572 case DW_LANG_Fortran95:
19573 case DW_LANG_Fortran03:
19574 case DW_LANG_Fortran08:
19575 cu->language = language_fortran;
19576 break;
19577 case DW_LANG_Go:
19578 cu->language = language_go;
19579 break;
19580 case DW_LANG_Mips_Assembler:
19581 cu->language = language_asm;
19582 break;
19583 case DW_LANG_Ada83:
19584 case DW_LANG_Ada95:
19585 cu->language = language_ada;
19586 break;
19587 case DW_LANG_Modula2:
19588 cu->language = language_m2;
19589 break;
19590 case DW_LANG_Pascal83:
19591 cu->language = language_pascal;
19592 break;
19593 case DW_LANG_ObjC:
19594 cu->language = language_objc;
19595 break;
19596 case DW_LANG_Rust:
19597 case DW_LANG_Rust_old:
19598 cu->language = language_rust;
19599 break;
19600 case DW_LANG_Cobol74:
19601 case DW_LANG_Cobol85:
19602 default:
19603 cu->language = language_minimal;
19604 break;
19605 }
19606 cu->language_defn = language_def (cu->language);
19607 }
19608
19609 /* Return the named attribute or NULL if not there. */
19610
19611 static struct attribute *
19612 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19613 {
19614 for (;;)
19615 {
19616 unsigned int i;
19617 struct attribute *spec = NULL;
19618
19619 for (i = 0; i < die->num_attrs; ++i)
19620 {
19621 if (die->attrs[i].name == name)
19622 return &die->attrs[i];
19623 if (die->attrs[i].name == DW_AT_specification
19624 || die->attrs[i].name == DW_AT_abstract_origin)
19625 spec = &die->attrs[i];
19626 }
19627
19628 if (!spec)
19629 break;
19630
19631 die = follow_die_ref (die, spec, &cu);
19632 }
19633
19634 return NULL;
19635 }
19636
19637 /* Return the string associated with a string-typed attribute, or NULL if it
19638 is either not found or is of an incorrect type. */
19639
19640 static const char *
19641 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19642 {
19643 struct attribute *attr;
19644 const char *str = NULL;
19645
19646 attr = dwarf2_attr (die, name, cu);
19647
19648 if (attr != NULL)
19649 {
19650 str = attr->value_as_string ();
19651 if (str == nullptr)
19652 complaint (_("string type expected for attribute %s for "
19653 "DIE at %s in module %s"),
19654 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19655 objfile_name (cu->per_objfile->objfile));
19656 }
19657
19658 return str;
19659 }
19660
19661 /* Return the dwo name or NULL if not present. If present, it is in either
19662 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19663 static const char *
19664 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19665 {
19666 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19667 if (dwo_name == nullptr)
19668 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19669 return dwo_name;
19670 }
19671
19672 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19673 and holds a non-zero value. This function should only be used for
19674 DW_FORM_flag or DW_FORM_flag_present attributes. */
19675
19676 static int
19677 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19678 {
19679 struct attribute *attr = dwarf2_attr (die, name, cu);
19680
19681 return (attr && DW_UNSND (attr));
19682 }
19683
19684 static int
19685 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19686 {
19687 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19688 which value is non-zero. However, we have to be careful with
19689 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19690 (via dwarf2_flag_true_p) follows this attribute. So we may
19691 end up accidently finding a declaration attribute that belongs
19692 to a different DIE referenced by the specification attribute,
19693 even though the given DIE does not have a declaration attribute. */
19694 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19695 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19696 }
19697
19698 /* Return the die giving the specification for DIE, if there is
19699 one. *SPEC_CU is the CU containing DIE on input, and the CU
19700 containing the return value on output. If there is no
19701 specification, but there is an abstract origin, that is
19702 returned. */
19703
19704 static struct die_info *
19705 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19706 {
19707 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19708 *spec_cu);
19709
19710 if (spec_attr == NULL)
19711 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19712
19713 if (spec_attr == NULL)
19714 return NULL;
19715 else
19716 return follow_die_ref (die, spec_attr, spec_cu);
19717 }
19718
19719 /* Stub for free_line_header to match void * callback types. */
19720
19721 static void
19722 free_line_header_voidp (void *arg)
19723 {
19724 struct line_header *lh = (struct line_header *) arg;
19725
19726 delete lh;
19727 }
19728
19729 /* A convenience function to find the proper .debug_line section for a CU. */
19730
19731 static struct dwarf2_section_info *
19732 get_debug_line_section (struct dwarf2_cu *cu)
19733 {
19734 struct dwarf2_section_info *section;
19735 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19736
19737 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19738 DWO file. */
19739 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19740 section = &cu->dwo_unit->dwo_file->sections.line;
19741 else if (cu->per_cu->is_dwz)
19742 {
19743 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19744
19745 section = &dwz->line;
19746 }
19747 else
19748 section = &dwarf2_per_objfile->per_bfd->line;
19749
19750 return section;
19751 }
19752
19753 /* Read the statement program header starting at OFFSET in
19754 .debug_line, or .debug_line.dwo. Return a pointer
19755 to a struct line_header, allocated using xmalloc.
19756 Returns NULL if there is a problem reading the header, e.g., if it
19757 has a version we don't understand.
19758
19759 NOTE: the strings in the include directory and file name tables of
19760 the returned object point into the dwarf line section buffer,
19761 and must not be freed. */
19762
19763 static line_header_up
19764 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19765 {
19766 struct dwarf2_section_info *section;
19767 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19768
19769 section = get_debug_line_section (cu);
19770 section->read (dwarf2_per_objfile->objfile);
19771 if (section->buffer == NULL)
19772 {
19773 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19774 complaint (_("missing .debug_line.dwo section"));
19775 else
19776 complaint (_("missing .debug_line section"));
19777 return 0;
19778 }
19779
19780 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19781 dwarf2_per_objfile, section,
19782 &cu->header);
19783 }
19784
19785 /* Subroutine of dwarf_decode_lines to simplify it.
19786 Return the file name of the psymtab for the given file_entry.
19787 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19788 If space for the result is malloc'd, *NAME_HOLDER will be set.
19789 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19790
19791 static const char *
19792 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19793 const dwarf2_psymtab *pst,
19794 const char *comp_dir,
19795 gdb::unique_xmalloc_ptr<char> *name_holder)
19796 {
19797 const char *include_name = fe.name;
19798 const char *include_name_to_compare = include_name;
19799 const char *pst_filename;
19800 int file_is_pst;
19801
19802 const char *dir_name = fe.include_dir (lh);
19803
19804 gdb::unique_xmalloc_ptr<char> hold_compare;
19805 if (!IS_ABSOLUTE_PATH (include_name)
19806 && (dir_name != NULL || comp_dir != NULL))
19807 {
19808 /* Avoid creating a duplicate psymtab for PST.
19809 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19810 Before we do the comparison, however, we need to account
19811 for DIR_NAME and COMP_DIR.
19812 First prepend dir_name (if non-NULL). If we still don't
19813 have an absolute path prepend comp_dir (if non-NULL).
19814 However, the directory we record in the include-file's
19815 psymtab does not contain COMP_DIR (to match the
19816 corresponding symtab(s)).
19817
19818 Example:
19819
19820 bash$ cd /tmp
19821 bash$ gcc -g ./hello.c
19822 include_name = "hello.c"
19823 dir_name = "."
19824 DW_AT_comp_dir = comp_dir = "/tmp"
19825 DW_AT_name = "./hello.c"
19826
19827 */
19828
19829 if (dir_name != NULL)
19830 {
19831 name_holder->reset (concat (dir_name, SLASH_STRING,
19832 include_name, (char *) NULL));
19833 include_name = name_holder->get ();
19834 include_name_to_compare = include_name;
19835 }
19836 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19837 {
19838 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19839 include_name, (char *) NULL));
19840 include_name_to_compare = hold_compare.get ();
19841 }
19842 }
19843
19844 pst_filename = pst->filename;
19845 gdb::unique_xmalloc_ptr<char> copied_name;
19846 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19847 {
19848 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19849 pst_filename, (char *) NULL));
19850 pst_filename = copied_name.get ();
19851 }
19852
19853 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19854
19855 if (file_is_pst)
19856 return NULL;
19857 return include_name;
19858 }
19859
19860 /* State machine to track the state of the line number program. */
19861
19862 class lnp_state_machine
19863 {
19864 public:
19865 /* Initialize a machine state for the start of a line number
19866 program. */
19867 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19868 bool record_lines_p);
19869
19870 file_entry *current_file ()
19871 {
19872 /* lh->file_names is 0-based, but the file name numbers in the
19873 statement program are 1-based. */
19874 return m_line_header->file_name_at (m_file);
19875 }
19876
19877 /* Record the line in the state machine. END_SEQUENCE is true if
19878 we're processing the end of a sequence. */
19879 void record_line (bool end_sequence);
19880
19881 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19882 nop-out rest of the lines in this sequence. */
19883 void check_line_address (struct dwarf2_cu *cu,
19884 const gdb_byte *line_ptr,
19885 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19886
19887 void handle_set_discriminator (unsigned int discriminator)
19888 {
19889 m_discriminator = discriminator;
19890 m_line_has_non_zero_discriminator |= discriminator != 0;
19891 }
19892
19893 /* Handle DW_LNE_set_address. */
19894 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19895 {
19896 m_op_index = 0;
19897 address += baseaddr;
19898 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19899 }
19900
19901 /* Handle DW_LNS_advance_pc. */
19902 void handle_advance_pc (CORE_ADDR adjust);
19903
19904 /* Handle a special opcode. */
19905 void handle_special_opcode (unsigned char op_code);
19906
19907 /* Handle DW_LNS_advance_line. */
19908 void handle_advance_line (int line_delta)
19909 {
19910 advance_line (line_delta);
19911 }
19912
19913 /* Handle DW_LNS_set_file. */
19914 void handle_set_file (file_name_index file);
19915
19916 /* Handle DW_LNS_negate_stmt. */
19917 void handle_negate_stmt ()
19918 {
19919 m_is_stmt = !m_is_stmt;
19920 }
19921
19922 /* Handle DW_LNS_const_add_pc. */
19923 void handle_const_add_pc ();
19924
19925 /* Handle DW_LNS_fixed_advance_pc. */
19926 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19927 {
19928 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19929 m_op_index = 0;
19930 }
19931
19932 /* Handle DW_LNS_copy. */
19933 void handle_copy ()
19934 {
19935 record_line (false);
19936 m_discriminator = 0;
19937 }
19938
19939 /* Handle DW_LNE_end_sequence. */
19940 void handle_end_sequence ()
19941 {
19942 m_currently_recording_lines = true;
19943 }
19944
19945 private:
19946 /* Advance the line by LINE_DELTA. */
19947 void advance_line (int line_delta)
19948 {
19949 m_line += line_delta;
19950
19951 if (line_delta != 0)
19952 m_line_has_non_zero_discriminator = m_discriminator != 0;
19953 }
19954
19955 struct dwarf2_cu *m_cu;
19956
19957 gdbarch *m_gdbarch;
19958
19959 /* True if we're recording lines.
19960 Otherwise we're building partial symtabs and are just interested in
19961 finding include files mentioned by the line number program. */
19962 bool m_record_lines_p;
19963
19964 /* The line number header. */
19965 line_header *m_line_header;
19966
19967 /* These are part of the standard DWARF line number state machine,
19968 and initialized according to the DWARF spec. */
19969
19970 unsigned char m_op_index = 0;
19971 /* The line table index of the current file. */
19972 file_name_index m_file = 1;
19973 unsigned int m_line = 1;
19974
19975 /* These are initialized in the constructor. */
19976
19977 CORE_ADDR m_address;
19978 bool m_is_stmt;
19979 unsigned int m_discriminator;
19980
19981 /* Additional bits of state we need to track. */
19982
19983 /* The last file that we called dwarf2_start_subfile for.
19984 This is only used for TLLs. */
19985 unsigned int m_last_file = 0;
19986 /* The last file a line number was recorded for. */
19987 struct subfile *m_last_subfile = NULL;
19988
19989 /* When true, record the lines we decode. */
19990 bool m_currently_recording_lines = false;
19991
19992 /* The last line number that was recorded, used to coalesce
19993 consecutive entries for the same line. This can happen, for
19994 example, when discriminators are present. PR 17276. */
19995 unsigned int m_last_line = 0;
19996 bool m_line_has_non_zero_discriminator = false;
19997 };
19998
19999 void
20000 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20001 {
20002 CORE_ADDR addr_adj = (((m_op_index + adjust)
20003 / m_line_header->maximum_ops_per_instruction)
20004 * m_line_header->minimum_instruction_length);
20005 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20006 m_op_index = ((m_op_index + adjust)
20007 % m_line_header->maximum_ops_per_instruction);
20008 }
20009
20010 void
20011 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20012 {
20013 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20014 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20015 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20016 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20017 / m_line_header->maximum_ops_per_instruction)
20018 * m_line_header->minimum_instruction_length);
20019 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20020 m_op_index = ((m_op_index + adj_opcode_d)
20021 % m_line_header->maximum_ops_per_instruction);
20022
20023 int line_delta = m_line_header->line_base + adj_opcode_r;
20024 advance_line (line_delta);
20025 record_line (false);
20026 m_discriminator = 0;
20027 }
20028
20029 void
20030 lnp_state_machine::handle_set_file (file_name_index file)
20031 {
20032 m_file = file;
20033
20034 const file_entry *fe = current_file ();
20035 if (fe == NULL)
20036 dwarf2_debug_line_missing_file_complaint ();
20037 else if (m_record_lines_p)
20038 {
20039 const char *dir = fe->include_dir (m_line_header);
20040
20041 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20042 m_line_has_non_zero_discriminator = m_discriminator != 0;
20043 dwarf2_start_subfile (m_cu, fe->name, dir);
20044 }
20045 }
20046
20047 void
20048 lnp_state_machine::handle_const_add_pc ()
20049 {
20050 CORE_ADDR adjust
20051 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20052
20053 CORE_ADDR addr_adj
20054 = (((m_op_index + adjust)
20055 / m_line_header->maximum_ops_per_instruction)
20056 * m_line_header->minimum_instruction_length);
20057
20058 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20059 m_op_index = ((m_op_index + adjust)
20060 % m_line_header->maximum_ops_per_instruction);
20061 }
20062
20063 /* Return non-zero if we should add LINE to the line number table.
20064 LINE is the line to add, LAST_LINE is the last line that was added,
20065 LAST_SUBFILE is the subfile for LAST_LINE.
20066 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20067 had a non-zero discriminator.
20068
20069 We have to be careful in the presence of discriminators.
20070 E.g., for this line:
20071
20072 for (i = 0; i < 100000; i++);
20073
20074 clang can emit four line number entries for that one line,
20075 each with a different discriminator.
20076 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20077
20078 However, we want gdb to coalesce all four entries into one.
20079 Otherwise the user could stepi into the middle of the line and
20080 gdb would get confused about whether the pc really was in the
20081 middle of the line.
20082
20083 Things are further complicated by the fact that two consecutive
20084 line number entries for the same line is a heuristic used by gcc
20085 to denote the end of the prologue. So we can't just discard duplicate
20086 entries, we have to be selective about it. The heuristic we use is
20087 that we only collapse consecutive entries for the same line if at least
20088 one of those entries has a non-zero discriminator. PR 17276.
20089
20090 Note: Addresses in the line number state machine can never go backwards
20091 within one sequence, thus this coalescing is ok. */
20092
20093 static int
20094 dwarf_record_line_p (struct dwarf2_cu *cu,
20095 unsigned int line, unsigned int last_line,
20096 int line_has_non_zero_discriminator,
20097 struct subfile *last_subfile)
20098 {
20099 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20100 return 1;
20101 if (line != last_line)
20102 return 1;
20103 /* Same line for the same file that we've seen already.
20104 As a last check, for pr 17276, only record the line if the line
20105 has never had a non-zero discriminator. */
20106 if (!line_has_non_zero_discriminator)
20107 return 1;
20108 return 0;
20109 }
20110
20111 /* Use the CU's builder to record line number LINE beginning at
20112 address ADDRESS in the line table of subfile SUBFILE. */
20113
20114 static void
20115 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20116 unsigned int line, CORE_ADDR address, bool is_stmt,
20117 struct dwarf2_cu *cu)
20118 {
20119 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20120
20121 if (dwarf_line_debug)
20122 {
20123 fprintf_unfiltered (gdb_stdlog,
20124 "Recording line %u, file %s, address %s\n",
20125 line, lbasename (subfile->name),
20126 paddress (gdbarch, address));
20127 }
20128
20129 if (cu != nullptr)
20130 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20131 }
20132
20133 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20134 Mark the end of a set of line number records.
20135 The arguments are the same as for dwarf_record_line_1.
20136 If SUBFILE is NULL the request is ignored. */
20137
20138 static void
20139 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20140 CORE_ADDR address, struct dwarf2_cu *cu)
20141 {
20142 if (subfile == NULL)
20143 return;
20144
20145 if (dwarf_line_debug)
20146 {
20147 fprintf_unfiltered (gdb_stdlog,
20148 "Finishing current line, file %s, address %s\n",
20149 lbasename (subfile->name),
20150 paddress (gdbarch, address));
20151 }
20152
20153 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20154 }
20155
20156 void
20157 lnp_state_machine::record_line (bool end_sequence)
20158 {
20159 if (dwarf_line_debug)
20160 {
20161 fprintf_unfiltered (gdb_stdlog,
20162 "Processing actual line %u: file %u,"
20163 " address %s, is_stmt %u, discrim %u%s\n",
20164 m_line, m_file,
20165 paddress (m_gdbarch, m_address),
20166 m_is_stmt, m_discriminator,
20167 (end_sequence ? "\t(end sequence)" : ""));
20168 }
20169
20170 file_entry *fe = current_file ();
20171
20172 if (fe == NULL)
20173 dwarf2_debug_line_missing_file_complaint ();
20174 /* For now we ignore lines not starting on an instruction boundary.
20175 But not when processing end_sequence for compatibility with the
20176 previous version of the code. */
20177 else if (m_op_index == 0 || end_sequence)
20178 {
20179 fe->included_p = 1;
20180 if (m_record_lines_p)
20181 {
20182 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20183 || end_sequence)
20184 {
20185 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20186 m_currently_recording_lines ? m_cu : nullptr);
20187 }
20188
20189 if (!end_sequence)
20190 {
20191 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20192
20193 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20194 m_line_has_non_zero_discriminator,
20195 m_last_subfile))
20196 {
20197 buildsym_compunit *builder = m_cu->get_builder ();
20198 dwarf_record_line_1 (m_gdbarch,
20199 builder->get_current_subfile (),
20200 m_line, m_address, is_stmt,
20201 m_currently_recording_lines ? m_cu : nullptr);
20202 }
20203 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20204 m_last_line = m_line;
20205 }
20206 }
20207 }
20208 }
20209
20210 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20211 line_header *lh, bool record_lines_p)
20212 {
20213 m_cu = cu;
20214 m_gdbarch = arch;
20215 m_record_lines_p = record_lines_p;
20216 m_line_header = lh;
20217
20218 m_currently_recording_lines = true;
20219
20220 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20221 was a line entry for it so that the backend has a chance to adjust it
20222 and also record it in case it needs it. This is currently used by MIPS
20223 code, cf. `mips_adjust_dwarf2_line'. */
20224 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20225 m_is_stmt = lh->default_is_stmt;
20226 m_discriminator = 0;
20227 }
20228
20229 void
20230 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20231 const gdb_byte *line_ptr,
20232 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20233 {
20234 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20235 the pc range of the CU. However, we restrict the test to only ADDRESS
20236 values of zero to preserve GDB's previous behaviour which is to handle
20237 the specific case of a function being GC'd by the linker. */
20238
20239 if (address == 0 && address < unrelocated_lowpc)
20240 {
20241 /* This line table is for a function which has been
20242 GCd by the linker. Ignore it. PR gdb/12528 */
20243
20244 struct objfile *objfile = cu->per_objfile->objfile;
20245 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20246
20247 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20248 line_offset, objfile_name (objfile));
20249 m_currently_recording_lines = false;
20250 /* Note: m_currently_recording_lines is left as false until we see
20251 DW_LNE_end_sequence. */
20252 }
20253 }
20254
20255 /* Subroutine of dwarf_decode_lines to simplify it.
20256 Process the line number information in LH.
20257 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20258 program in order to set included_p for every referenced header. */
20259
20260 static void
20261 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20262 const int decode_for_pst_p, CORE_ADDR lowpc)
20263 {
20264 const gdb_byte *line_ptr, *extended_end;
20265 const gdb_byte *line_end;
20266 unsigned int bytes_read, extended_len;
20267 unsigned char op_code, extended_op;
20268 CORE_ADDR baseaddr;
20269 struct objfile *objfile = cu->per_objfile->objfile;
20270 bfd *abfd = objfile->obfd;
20271 struct gdbarch *gdbarch = objfile->arch ();
20272 /* True if we're recording line info (as opposed to building partial
20273 symtabs and just interested in finding include files mentioned by
20274 the line number program). */
20275 bool record_lines_p = !decode_for_pst_p;
20276
20277 baseaddr = objfile->text_section_offset ();
20278
20279 line_ptr = lh->statement_program_start;
20280 line_end = lh->statement_program_end;
20281
20282 /* Read the statement sequences until there's nothing left. */
20283 while (line_ptr < line_end)
20284 {
20285 /* The DWARF line number program state machine. Reset the state
20286 machine at the start of each sequence. */
20287 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20288 bool end_sequence = false;
20289
20290 if (record_lines_p)
20291 {
20292 /* Start a subfile for the current file of the state
20293 machine. */
20294 const file_entry *fe = state_machine.current_file ();
20295
20296 if (fe != NULL)
20297 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20298 }
20299
20300 /* Decode the table. */
20301 while (line_ptr < line_end && !end_sequence)
20302 {
20303 op_code = read_1_byte (abfd, line_ptr);
20304 line_ptr += 1;
20305
20306 if (op_code >= lh->opcode_base)
20307 {
20308 /* Special opcode. */
20309 state_machine.handle_special_opcode (op_code);
20310 }
20311 else switch (op_code)
20312 {
20313 case DW_LNS_extended_op:
20314 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20315 &bytes_read);
20316 line_ptr += bytes_read;
20317 extended_end = line_ptr + extended_len;
20318 extended_op = read_1_byte (abfd, line_ptr);
20319 line_ptr += 1;
20320 switch (extended_op)
20321 {
20322 case DW_LNE_end_sequence:
20323 state_machine.handle_end_sequence ();
20324 end_sequence = true;
20325 break;
20326 case DW_LNE_set_address:
20327 {
20328 CORE_ADDR address
20329 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20330 line_ptr += bytes_read;
20331
20332 state_machine.check_line_address (cu, line_ptr,
20333 lowpc - baseaddr, address);
20334 state_machine.handle_set_address (baseaddr, address);
20335 }
20336 break;
20337 case DW_LNE_define_file:
20338 {
20339 const char *cur_file;
20340 unsigned int mod_time, length;
20341 dir_index dindex;
20342
20343 cur_file = read_direct_string (abfd, line_ptr,
20344 &bytes_read);
20345 line_ptr += bytes_read;
20346 dindex = (dir_index)
20347 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20348 line_ptr += bytes_read;
20349 mod_time =
20350 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20351 line_ptr += bytes_read;
20352 length =
20353 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20354 line_ptr += bytes_read;
20355 lh->add_file_name (cur_file, dindex, mod_time, length);
20356 }
20357 break;
20358 case DW_LNE_set_discriminator:
20359 {
20360 /* The discriminator is not interesting to the
20361 debugger; just ignore it. We still need to
20362 check its value though:
20363 if there are consecutive entries for the same
20364 (non-prologue) line we want to coalesce them.
20365 PR 17276. */
20366 unsigned int discr
20367 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20368 line_ptr += bytes_read;
20369
20370 state_machine.handle_set_discriminator (discr);
20371 }
20372 break;
20373 default:
20374 complaint (_("mangled .debug_line section"));
20375 return;
20376 }
20377 /* Make sure that we parsed the extended op correctly. If e.g.
20378 we expected a different address size than the producer used,
20379 we may have read the wrong number of bytes. */
20380 if (line_ptr != extended_end)
20381 {
20382 complaint (_("mangled .debug_line section"));
20383 return;
20384 }
20385 break;
20386 case DW_LNS_copy:
20387 state_machine.handle_copy ();
20388 break;
20389 case DW_LNS_advance_pc:
20390 {
20391 CORE_ADDR adjust
20392 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20393 line_ptr += bytes_read;
20394
20395 state_machine.handle_advance_pc (adjust);
20396 }
20397 break;
20398 case DW_LNS_advance_line:
20399 {
20400 int line_delta
20401 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20402 line_ptr += bytes_read;
20403
20404 state_machine.handle_advance_line (line_delta);
20405 }
20406 break;
20407 case DW_LNS_set_file:
20408 {
20409 file_name_index file
20410 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20411 &bytes_read);
20412 line_ptr += bytes_read;
20413
20414 state_machine.handle_set_file (file);
20415 }
20416 break;
20417 case DW_LNS_set_column:
20418 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20419 line_ptr += bytes_read;
20420 break;
20421 case DW_LNS_negate_stmt:
20422 state_machine.handle_negate_stmt ();
20423 break;
20424 case DW_LNS_set_basic_block:
20425 break;
20426 /* Add to the address register of the state machine the
20427 address increment value corresponding to special opcode
20428 255. I.e., this value is scaled by the minimum
20429 instruction length since special opcode 255 would have
20430 scaled the increment. */
20431 case DW_LNS_const_add_pc:
20432 state_machine.handle_const_add_pc ();
20433 break;
20434 case DW_LNS_fixed_advance_pc:
20435 {
20436 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20437 line_ptr += 2;
20438
20439 state_machine.handle_fixed_advance_pc (addr_adj);
20440 }
20441 break;
20442 default:
20443 {
20444 /* Unknown standard opcode, ignore it. */
20445 int i;
20446
20447 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20448 {
20449 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20450 line_ptr += bytes_read;
20451 }
20452 }
20453 }
20454 }
20455
20456 if (!end_sequence)
20457 dwarf2_debug_line_missing_end_sequence_complaint ();
20458
20459 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20460 in which case we still finish recording the last line). */
20461 state_machine.record_line (true);
20462 }
20463 }
20464
20465 /* Decode the Line Number Program (LNP) for the given line_header
20466 structure and CU. The actual information extracted and the type
20467 of structures created from the LNP depends on the value of PST.
20468
20469 1. If PST is NULL, then this procedure uses the data from the program
20470 to create all necessary symbol tables, and their linetables.
20471
20472 2. If PST is not NULL, this procedure reads the program to determine
20473 the list of files included by the unit represented by PST, and
20474 builds all the associated partial symbol tables.
20475
20476 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20477 It is used for relative paths in the line table.
20478 NOTE: When processing partial symtabs (pst != NULL),
20479 comp_dir == pst->dirname.
20480
20481 NOTE: It is important that psymtabs have the same file name (via strcmp)
20482 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20483 symtab we don't use it in the name of the psymtabs we create.
20484 E.g. expand_line_sal requires this when finding psymtabs to expand.
20485 A good testcase for this is mb-inline.exp.
20486
20487 LOWPC is the lowest address in CU (or 0 if not known).
20488
20489 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20490 for its PC<->lines mapping information. Otherwise only the filename
20491 table is read in. */
20492
20493 static void
20494 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20495 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20496 CORE_ADDR lowpc, int decode_mapping)
20497 {
20498 struct objfile *objfile = cu->per_objfile->objfile;
20499 const int decode_for_pst_p = (pst != NULL);
20500
20501 if (decode_mapping)
20502 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20503
20504 if (decode_for_pst_p)
20505 {
20506 /* Now that we're done scanning the Line Header Program, we can
20507 create the psymtab of each included file. */
20508 for (auto &file_entry : lh->file_names ())
20509 if (file_entry.included_p == 1)
20510 {
20511 gdb::unique_xmalloc_ptr<char> name_holder;
20512 const char *include_name =
20513 psymtab_include_file_name (lh, file_entry, pst,
20514 comp_dir, &name_holder);
20515 if (include_name != NULL)
20516 dwarf2_create_include_psymtab (include_name, pst, objfile);
20517 }
20518 }
20519 else
20520 {
20521 /* Make sure a symtab is created for every file, even files
20522 which contain only variables (i.e. no code with associated
20523 line numbers). */
20524 buildsym_compunit *builder = cu->get_builder ();
20525 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20526
20527 for (auto &fe : lh->file_names ())
20528 {
20529 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20530 if (builder->get_current_subfile ()->symtab == NULL)
20531 {
20532 builder->get_current_subfile ()->symtab
20533 = allocate_symtab (cust,
20534 builder->get_current_subfile ()->name);
20535 }
20536 fe.symtab = builder->get_current_subfile ()->symtab;
20537 }
20538 }
20539 }
20540
20541 /* Start a subfile for DWARF. FILENAME is the name of the file and
20542 DIRNAME the name of the source directory which contains FILENAME
20543 or NULL if not known.
20544 This routine tries to keep line numbers from identical absolute and
20545 relative file names in a common subfile.
20546
20547 Using the `list' example from the GDB testsuite, which resides in
20548 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20549 of /srcdir/list0.c yields the following debugging information for list0.c:
20550
20551 DW_AT_name: /srcdir/list0.c
20552 DW_AT_comp_dir: /compdir
20553 files.files[0].name: list0.h
20554 files.files[0].dir: /srcdir
20555 files.files[1].name: list0.c
20556 files.files[1].dir: /srcdir
20557
20558 The line number information for list0.c has to end up in a single
20559 subfile, so that `break /srcdir/list0.c:1' works as expected.
20560 start_subfile will ensure that this happens provided that we pass the
20561 concatenation of files.files[1].dir and files.files[1].name as the
20562 subfile's name. */
20563
20564 static void
20565 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20566 const char *dirname)
20567 {
20568 gdb::unique_xmalloc_ptr<char> copy;
20569
20570 /* In order not to lose the line information directory,
20571 we concatenate it to the filename when it makes sense.
20572 Note that the Dwarf3 standard says (speaking of filenames in line
20573 information): ``The directory index is ignored for file names
20574 that represent full path names''. Thus ignoring dirname in the
20575 `else' branch below isn't an issue. */
20576
20577 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20578 {
20579 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20580 filename = copy.get ();
20581 }
20582
20583 cu->get_builder ()->start_subfile (filename);
20584 }
20585
20586 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20587 buildsym_compunit constructor. */
20588
20589 struct compunit_symtab *
20590 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20591 CORE_ADDR low_pc)
20592 {
20593 gdb_assert (m_builder == nullptr);
20594
20595 m_builder.reset (new struct buildsym_compunit
20596 (per_cu->dwarf2_per_objfile->objfile,
20597 name, comp_dir, language, low_pc));
20598
20599 list_in_scope = get_builder ()->get_file_symbols ();
20600
20601 get_builder ()->record_debugformat ("DWARF 2");
20602 get_builder ()->record_producer (producer);
20603
20604 processing_has_namespace_info = false;
20605
20606 return get_builder ()->get_compunit_symtab ();
20607 }
20608
20609 static void
20610 var_decode_location (struct attribute *attr, struct symbol *sym,
20611 struct dwarf2_cu *cu)
20612 {
20613 struct objfile *objfile = cu->per_objfile->objfile;
20614 struct comp_unit_head *cu_header = &cu->header;
20615
20616 /* NOTE drow/2003-01-30: There used to be a comment and some special
20617 code here to turn a symbol with DW_AT_external and a
20618 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20619 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20620 with some versions of binutils) where shared libraries could have
20621 relocations against symbols in their debug information - the
20622 minimal symbol would have the right address, but the debug info
20623 would not. It's no longer necessary, because we will explicitly
20624 apply relocations when we read in the debug information now. */
20625
20626 /* A DW_AT_location attribute with no contents indicates that a
20627 variable has been optimized away. */
20628 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20629 {
20630 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20631 return;
20632 }
20633
20634 /* Handle one degenerate form of location expression specially, to
20635 preserve GDB's previous behavior when section offsets are
20636 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20637 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20638
20639 if (attr->form_is_block ()
20640 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20641 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20642 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20643 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20644 && (DW_BLOCK (attr)->size
20645 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20646 {
20647 unsigned int dummy;
20648
20649 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20650 SET_SYMBOL_VALUE_ADDRESS
20651 (sym, cu->header.read_address (objfile->obfd,
20652 DW_BLOCK (attr)->data + 1,
20653 &dummy));
20654 else
20655 SET_SYMBOL_VALUE_ADDRESS
20656 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20657 &dummy));
20658 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20659 fixup_symbol_section (sym, objfile);
20660 SET_SYMBOL_VALUE_ADDRESS
20661 (sym,
20662 SYMBOL_VALUE_ADDRESS (sym)
20663 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20664 return;
20665 }
20666
20667 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20668 expression evaluator, and use LOC_COMPUTED only when necessary
20669 (i.e. when the value of a register or memory location is
20670 referenced, or a thread-local block, etc.). Then again, it might
20671 not be worthwhile. I'm assuming that it isn't unless performance
20672 or memory numbers show me otherwise. */
20673
20674 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20675
20676 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20677 cu->has_loclist = true;
20678 }
20679
20680 /* Given a pointer to a DWARF information entry, figure out if we need
20681 to make a symbol table entry for it, and if so, create a new entry
20682 and return a pointer to it.
20683 If TYPE is NULL, determine symbol type from the die, otherwise
20684 used the passed type.
20685 If SPACE is not NULL, use it to hold the new symbol. If it is
20686 NULL, allocate a new symbol on the objfile's obstack. */
20687
20688 static struct symbol *
20689 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20690 struct symbol *space)
20691 {
20692 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20693 struct objfile *objfile = dwarf2_per_objfile->objfile;
20694 struct gdbarch *gdbarch = objfile->arch ();
20695 struct symbol *sym = NULL;
20696 const char *name;
20697 struct attribute *attr = NULL;
20698 struct attribute *attr2 = NULL;
20699 CORE_ADDR baseaddr;
20700 struct pending **list_to_add = NULL;
20701
20702 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20703
20704 baseaddr = objfile->text_section_offset ();
20705
20706 name = dwarf2_name (die, cu);
20707 if (name)
20708 {
20709 int suppress_add = 0;
20710
20711 if (space)
20712 sym = space;
20713 else
20714 sym = new (&objfile->objfile_obstack) symbol;
20715 OBJSTAT (objfile, n_syms++);
20716
20717 /* Cache this symbol's name and the name's demangled form (if any). */
20718 sym->set_language (cu->language, &objfile->objfile_obstack);
20719 /* Fortran does not have mangling standard and the mangling does differ
20720 between gfortran, iFort etc. */
20721 const char *physname
20722 = (cu->language == language_fortran
20723 ? dwarf2_full_name (name, die, cu)
20724 : dwarf2_physname (name, die, cu));
20725 const char *linkagename = dw2_linkage_name (die, cu);
20726
20727 if (linkagename == nullptr || cu->language == language_ada)
20728 sym->set_linkage_name (physname);
20729 else
20730 {
20731 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20732 sym->set_linkage_name (linkagename);
20733 }
20734
20735 /* Default assumptions.
20736 Use the passed type or decode it from the die. */
20737 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20738 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20739 if (type != NULL)
20740 SYMBOL_TYPE (sym) = type;
20741 else
20742 SYMBOL_TYPE (sym) = die_type (die, cu);
20743 attr = dwarf2_attr (die,
20744 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20745 cu);
20746 if (attr != nullptr)
20747 {
20748 SYMBOL_LINE (sym) = DW_UNSND (attr);
20749 }
20750
20751 attr = dwarf2_attr (die,
20752 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20753 cu);
20754 if (attr != nullptr)
20755 {
20756 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20757 struct file_entry *fe;
20758
20759 if (cu->line_header != NULL)
20760 fe = cu->line_header->file_name_at (file_index);
20761 else
20762 fe = NULL;
20763
20764 if (fe == NULL)
20765 complaint (_("file index out of range"));
20766 else
20767 symbol_set_symtab (sym, fe->symtab);
20768 }
20769
20770 switch (die->tag)
20771 {
20772 case DW_TAG_label:
20773 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20774 if (attr != nullptr)
20775 {
20776 CORE_ADDR addr;
20777
20778 addr = attr->value_as_address ();
20779 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20780 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20781 }
20782 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20783 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20784 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20785 add_symbol_to_list (sym, cu->list_in_scope);
20786 break;
20787 case DW_TAG_subprogram:
20788 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20789 finish_block. */
20790 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20791 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20792 if ((attr2 && (DW_UNSND (attr2) != 0))
20793 || cu->language == language_ada
20794 || cu->language == language_fortran)
20795 {
20796 /* Subprograms marked external are stored as a global symbol.
20797 Ada and Fortran subprograms, whether marked external or
20798 not, are always stored as a global symbol, because we want
20799 to be able to access them globally. For instance, we want
20800 to be able to break on a nested subprogram without having
20801 to specify the context. */
20802 list_to_add = cu->get_builder ()->get_global_symbols ();
20803 }
20804 else
20805 {
20806 list_to_add = cu->list_in_scope;
20807 }
20808 break;
20809 case DW_TAG_inlined_subroutine:
20810 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20811 finish_block. */
20812 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20813 SYMBOL_INLINED (sym) = 1;
20814 list_to_add = cu->list_in_scope;
20815 break;
20816 case DW_TAG_template_value_param:
20817 suppress_add = 1;
20818 /* Fall through. */
20819 case DW_TAG_constant:
20820 case DW_TAG_variable:
20821 case DW_TAG_member:
20822 /* Compilation with minimal debug info may result in
20823 variables with missing type entries. Change the
20824 misleading `void' type to something sensible. */
20825 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
20826 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20827
20828 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20829 /* In the case of DW_TAG_member, we should only be called for
20830 static const members. */
20831 if (die->tag == DW_TAG_member)
20832 {
20833 /* dwarf2_add_field uses die_is_declaration,
20834 so we do the same. */
20835 gdb_assert (die_is_declaration (die, cu));
20836 gdb_assert (attr);
20837 }
20838 if (attr != nullptr)
20839 {
20840 dwarf2_const_value (attr, sym, cu);
20841 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20842 if (!suppress_add)
20843 {
20844 if (attr2 && (DW_UNSND (attr2) != 0))
20845 list_to_add = cu->get_builder ()->get_global_symbols ();
20846 else
20847 list_to_add = cu->list_in_scope;
20848 }
20849 break;
20850 }
20851 attr = dwarf2_attr (die, DW_AT_location, cu);
20852 if (attr != nullptr)
20853 {
20854 var_decode_location (attr, sym, cu);
20855 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20856
20857 /* Fortran explicitly imports any global symbols to the local
20858 scope by DW_TAG_common_block. */
20859 if (cu->language == language_fortran && die->parent
20860 && die->parent->tag == DW_TAG_common_block)
20861 attr2 = NULL;
20862
20863 if (SYMBOL_CLASS (sym) == LOC_STATIC
20864 && SYMBOL_VALUE_ADDRESS (sym) == 0
20865 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
20866 {
20867 /* When a static variable is eliminated by the linker,
20868 the corresponding debug information is not stripped
20869 out, but the variable address is set to null;
20870 do not add such variables into symbol table. */
20871 }
20872 else if (attr2 && (DW_UNSND (attr2) != 0))
20873 {
20874 if (SYMBOL_CLASS (sym) == LOC_STATIC
20875 && (objfile->flags & OBJF_MAINLINE) == 0
20876 && dwarf2_per_objfile->per_bfd->can_copy)
20877 {
20878 /* A global static variable might be subject to
20879 copy relocation. We first check for a local
20880 minsym, though, because maybe the symbol was
20881 marked hidden, in which case this would not
20882 apply. */
20883 bound_minimal_symbol found
20884 = (lookup_minimal_symbol_linkage
20885 (sym->linkage_name (), objfile));
20886 if (found.minsym != nullptr)
20887 sym->maybe_copied = 1;
20888 }
20889
20890 /* A variable with DW_AT_external is never static,
20891 but it may be block-scoped. */
20892 list_to_add
20893 = ((cu->list_in_scope
20894 == cu->get_builder ()->get_file_symbols ())
20895 ? cu->get_builder ()->get_global_symbols ()
20896 : cu->list_in_scope);
20897 }
20898 else
20899 list_to_add = cu->list_in_scope;
20900 }
20901 else
20902 {
20903 /* We do not know the address of this symbol.
20904 If it is an external symbol and we have type information
20905 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20906 The address of the variable will then be determined from
20907 the minimal symbol table whenever the variable is
20908 referenced. */
20909 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20910
20911 /* Fortran explicitly imports any global symbols to the local
20912 scope by DW_TAG_common_block. */
20913 if (cu->language == language_fortran && die->parent
20914 && die->parent->tag == DW_TAG_common_block)
20915 {
20916 /* SYMBOL_CLASS doesn't matter here because
20917 read_common_block is going to reset it. */
20918 if (!suppress_add)
20919 list_to_add = cu->list_in_scope;
20920 }
20921 else if (attr2 && (DW_UNSND (attr2) != 0)
20922 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20923 {
20924 /* A variable with DW_AT_external is never static, but it
20925 may be block-scoped. */
20926 list_to_add
20927 = ((cu->list_in_scope
20928 == cu->get_builder ()->get_file_symbols ())
20929 ? cu->get_builder ()->get_global_symbols ()
20930 : cu->list_in_scope);
20931
20932 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20933 }
20934 else if (!die_is_declaration (die, cu))
20935 {
20936 /* Use the default LOC_OPTIMIZED_OUT class. */
20937 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20938 if (!suppress_add)
20939 list_to_add = cu->list_in_scope;
20940 }
20941 }
20942 break;
20943 case DW_TAG_formal_parameter:
20944 {
20945 /* If we are inside a function, mark this as an argument. If
20946 not, we might be looking at an argument to an inlined function
20947 when we do not have enough information to show inlined frames;
20948 pretend it's a local variable in that case so that the user can
20949 still see it. */
20950 struct context_stack *curr
20951 = cu->get_builder ()->get_current_context_stack ();
20952 if (curr != nullptr && curr->name != nullptr)
20953 SYMBOL_IS_ARGUMENT (sym) = 1;
20954 attr = dwarf2_attr (die, DW_AT_location, cu);
20955 if (attr != nullptr)
20956 {
20957 var_decode_location (attr, sym, cu);
20958 }
20959 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20960 if (attr != nullptr)
20961 {
20962 dwarf2_const_value (attr, sym, cu);
20963 }
20964
20965 list_to_add = cu->list_in_scope;
20966 }
20967 break;
20968 case DW_TAG_unspecified_parameters:
20969 /* From varargs functions; gdb doesn't seem to have any
20970 interest in this information, so just ignore it for now.
20971 (FIXME?) */
20972 break;
20973 case DW_TAG_template_type_param:
20974 suppress_add = 1;
20975 /* Fall through. */
20976 case DW_TAG_class_type:
20977 case DW_TAG_interface_type:
20978 case DW_TAG_structure_type:
20979 case DW_TAG_union_type:
20980 case DW_TAG_set_type:
20981 case DW_TAG_enumeration_type:
20982 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20983 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20984
20985 {
20986 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20987 really ever be static objects: otherwise, if you try
20988 to, say, break of a class's method and you're in a file
20989 which doesn't mention that class, it won't work unless
20990 the check for all static symbols in lookup_symbol_aux
20991 saves you. See the OtherFileClass tests in
20992 gdb.c++/namespace.exp. */
20993
20994 if (!suppress_add)
20995 {
20996 buildsym_compunit *builder = cu->get_builder ();
20997 list_to_add
20998 = (cu->list_in_scope == builder->get_file_symbols ()
20999 && cu->language == language_cplus
21000 ? builder->get_global_symbols ()
21001 : cu->list_in_scope);
21002
21003 /* The semantics of C++ state that "struct foo {
21004 ... }" also defines a typedef for "foo". */
21005 if (cu->language == language_cplus
21006 || cu->language == language_ada
21007 || cu->language == language_d
21008 || cu->language == language_rust)
21009 {
21010 /* The symbol's name is already allocated along
21011 with this objfile, so we don't need to
21012 duplicate it for the type. */
21013 if (SYMBOL_TYPE (sym)->name () == 0)
21014 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21015 }
21016 }
21017 }
21018 break;
21019 case DW_TAG_typedef:
21020 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21021 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21022 list_to_add = cu->list_in_scope;
21023 break;
21024 case DW_TAG_base_type:
21025 case DW_TAG_subrange_type:
21026 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21027 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21028 list_to_add = cu->list_in_scope;
21029 break;
21030 case DW_TAG_enumerator:
21031 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21032 if (attr != nullptr)
21033 {
21034 dwarf2_const_value (attr, sym, cu);
21035 }
21036 {
21037 /* NOTE: carlton/2003-11-10: See comment above in the
21038 DW_TAG_class_type, etc. block. */
21039
21040 list_to_add
21041 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21042 && cu->language == language_cplus
21043 ? cu->get_builder ()->get_global_symbols ()
21044 : cu->list_in_scope);
21045 }
21046 break;
21047 case DW_TAG_imported_declaration:
21048 case DW_TAG_namespace:
21049 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21050 list_to_add = cu->get_builder ()->get_global_symbols ();
21051 break;
21052 case DW_TAG_module:
21053 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21054 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21055 list_to_add = cu->get_builder ()->get_global_symbols ();
21056 break;
21057 case DW_TAG_common_block:
21058 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21059 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21060 add_symbol_to_list (sym, cu->list_in_scope);
21061 break;
21062 default:
21063 /* Not a tag we recognize. Hopefully we aren't processing
21064 trash data, but since we must specifically ignore things
21065 we don't recognize, there is nothing else we should do at
21066 this point. */
21067 complaint (_("unsupported tag: '%s'"),
21068 dwarf_tag_name (die->tag));
21069 break;
21070 }
21071
21072 if (suppress_add)
21073 {
21074 sym->hash_next = objfile->template_symbols;
21075 objfile->template_symbols = sym;
21076 list_to_add = NULL;
21077 }
21078
21079 if (list_to_add != NULL)
21080 add_symbol_to_list (sym, list_to_add);
21081
21082 /* For the benefit of old versions of GCC, check for anonymous
21083 namespaces based on the demangled name. */
21084 if (!cu->processing_has_namespace_info
21085 && cu->language == language_cplus)
21086 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21087 }
21088 return (sym);
21089 }
21090
21091 /* Given an attr with a DW_FORM_dataN value in host byte order,
21092 zero-extend it as appropriate for the symbol's type. The DWARF
21093 standard (v4) is not entirely clear about the meaning of using
21094 DW_FORM_dataN for a constant with a signed type, where the type is
21095 wider than the data. The conclusion of a discussion on the DWARF
21096 list was that this is unspecified. We choose to always zero-extend
21097 because that is the interpretation long in use by GCC. */
21098
21099 static gdb_byte *
21100 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21101 struct dwarf2_cu *cu, LONGEST *value, int bits)
21102 {
21103 struct objfile *objfile = cu->per_objfile->objfile;
21104 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21105 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21106 LONGEST l = DW_UNSND (attr);
21107
21108 if (bits < sizeof (*value) * 8)
21109 {
21110 l &= ((LONGEST) 1 << bits) - 1;
21111 *value = l;
21112 }
21113 else if (bits == sizeof (*value) * 8)
21114 *value = l;
21115 else
21116 {
21117 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21118 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21119 return bytes;
21120 }
21121
21122 return NULL;
21123 }
21124
21125 /* Read a constant value from an attribute. Either set *VALUE, or if
21126 the value does not fit in *VALUE, set *BYTES - either already
21127 allocated on the objfile obstack, or newly allocated on OBSTACK,
21128 or, set *BATON, if we translated the constant to a location
21129 expression. */
21130
21131 static void
21132 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21133 const char *name, struct obstack *obstack,
21134 struct dwarf2_cu *cu,
21135 LONGEST *value, const gdb_byte **bytes,
21136 struct dwarf2_locexpr_baton **baton)
21137 {
21138 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21139 struct objfile *objfile = per_objfile->objfile;
21140 struct comp_unit_head *cu_header = &cu->header;
21141 struct dwarf_block *blk;
21142 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21143 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21144
21145 *value = 0;
21146 *bytes = NULL;
21147 *baton = NULL;
21148
21149 switch (attr->form)
21150 {
21151 case DW_FORM_addr:
21152 case DW_FORM_addrx:
21153 case DW_FORM_GNU_addr_index:
21154 {
21155 gdb_byte *data;
21156
21157 if (TYPE_LENGTH (type) != cu_header->addr_size)
21158 dwarf2_const_value_length_mismatch_complaint (name,
21159 cu_header->addr_size,
21160 TYPE_LENGTH (type));
21161 /* Symbols of this form are reasonably rare, so we just
21162 piggyback on the existing location code rather than writing
21163 a new implementation of symbol_computed_ops. */
21164 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21165 (*baton)->per_objfile = per_objfile;
21166 (*baton)->per_cu = cu->per_cu;
21167 gdb_assert ((*baton)->per_cu);
21168
21169 (*baton)->size = 2 + cu_header->addr_size;
21170 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21171 (*baton)->data = data;
21172
21173 data[0] = DW_OP_addr;
21174 store_unsigned_integer (&data[1], cu_header->addr_size,
21175 byte_order, DW_ADDR (attr));
21176 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21177 }
21178 break;
21179 case DW_FORM_string:
21180 case DW_FORM_strp:
21181 case DW_FORM_strx:
21182 case DW_FORM_GNU_str_index:
21183 case DW_FORM_GNU_strp_alt:
21184 /* DW_STRING is already allocated on the objfile obstack, point
21185 directly to it. */
21186 *bytes = (const gdb_byte *) DW_STRING (attr);
21187 break;
21188 case DW_FORM_block1:
21189 case DW_FORM_block2:
21190 case DW_FORM_block4:
21191 case DW_FORM_block:
21192 case DW_FORM_exprloc:
21193 case DW_FORM_data16:
21194 blk = DW_BLOCK (attr);
21195 if (TYPE_LENGTH (type) != blk->size)
21196 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21197 TYPE_LENGTH (type));
21198 *bytes = blk->data;
21199 break;
21200
21201 /* The DW_AT_const_value attributes are supposed to carry the
21202 symbol's value "represented as it would be on the target
21203 architecture." By the time we get here, it's already been
21204 converted to host endianness, so we just need to sign- or
21205 zero-extend it as appropriate. */
21206 case DW_FORM_data1:
21207 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21208 break;
21209 case DW_FORM_data2:
21210 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21211 break;
21212 case DW_FORM_data4:
21213 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21214 break;
21215 case DW_FORM_data8:
21216 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21217 break;
21218
21219 case DW_FORM_sdata:
21220 case DW_FORM_implicit_const:
21221 *value = DW_SND (attr);
21222 break;
21223
21224 case DW_FORM_udata:
21225 *value = DW_UNSND (attr);
21226 break;
21227
21228 default:
21229 complaint (_("unsupported const value attribute form: '%s'"),
21230 dwarf_form_name (attr->form));
21231 *value = 0;
21232 break;
21233 }
21234 }
21235
21236
21237 /* Copy constant value from an attribute to a symbol. */
21238
21239 static void
21240 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21241 struct dwarf2_cu *cu)
21242 {
21243 struct objfile *objfile = cu->per_objfile->objfile;
21244 LONGEST value;
21245 const gdb_byte *bytes;
21246 struct dwarf2_locexpr_baton *baton;
21247
21248 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21249 sym->print_name (),
21250 &objfile->objfile_obstack, cu,
21251 &value, &bytes, &baton);
21252
21253 if (baton != NULL)
21254 {
21255 SYMBOL_LOCATION_BATON (sym) = baton;
21256 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21257 }
21258 else if (bytes != NULL)
21259 {
21260 SYMBOL_VALUE_BYTES (sym) = bytes;
21261 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21262 }
21263 else
21264 {
21265 SYMBOL_VALUE (sym) = value;
21266 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21267 }
21268 }
21269
21270 /* Return the type of the die in question using its DW_AT_type attribute. */
21271
21272 static struct type *
21273 die_type (struct die_info *die, struct dwarf2_cu *cu)
21274 {
21275 struct attribute *type_attr;
21276
21277 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21278 if (!type_attr)
21279 {
21280 struct objfile *objfile = cu->per_objfile->objfile;
21281 /* A missing DW_AT_type represents a void type. */
21282 return objfile_type (objfile)->builtin_void;
21283 }
21284
21285 return lookup_die_type (die, type_attr, cu);
21286 }
21287
21288 /* True iff CU's producer generates GNAT Ada auxiliary information
21289 that allows to find parallel types through that information instead
21290 of having to do expensive parallel lookups by type name. */
21291
21292 static int
21293 need_gnat_info (struct dwarf2_cu *cu)
21294 {
21295 /* Assume that the Ada compiler was GNAT, which always produces
21296 the auxiliary information. */
21297 return (cu->language == language_ada);
21298 }
21299
21300 /* Return the auxiliary type of the die in question using its
21301 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21302 attribute is not present. */
21303
21304 static struct type *
21305 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21306 {
21307 struct attribute *type_attr;
21308
21309 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21310 if (!type_attr)
21311 return NULL;
21312
21313 return lookup_die_type (die, type_attr, cu);
21314 }
21315
21316 /* If DIE has a descriptive_type attribute, then set the TYPE's
21317 descriptive type accordingly. */
21318
21319 static void
21320 set_descriptive_type (struct type *type, struct die_info *die,
21321 struct dwarf2_cu *cu)
21322 {
21323 struct type *descriptive_type = die_descriptive_type (die, cu);
21324
21325 if (descriptive_type)
21326 {
21327 ALLOCATE_GNAT_AUX_TYPE (type);
21328 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21329 }
21330 }
21331
21332 /* Return the containing type of the die in question using its
21333 DW_AT_containing_type attribute. */
21334
21335 static struct type *
21336 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21337 {
21338 struct attribute *type_attr;
21339 struct objfile *objfile = cu->per_objfile->objfile;
21340
21341 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21342 if (!type_attr)
21343 error (_("Dwarf Error: Problem turning containing type into gdb type "
21344 "[in module %s]"), objfile_name (objfile));
21345
21346 return lookup_die_type (die, type_attr, cu);
21347 }
21348
21349 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21350
21351 static struct type *
21352 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21353 {
21354 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21355 struct objfile *objfile = dwarf2_per_objfile->objfile;
21356 char *saved;
21357
21358 std::string message
21359 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21360 objfile_name (objfile),
21361 sect_offset_str (cu->header.sect_off),
21362 sect_offset_str (die->sect_off));
21363 saved = obstack_strdup (&objfile->objfile_obstack, message);
21364
21365 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21366 }
21367
21368 /* Look up the type of DIE in CU using its type attribute ATTR.
21369 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21370 DW_AT_containing_type.
21371 If there is no type substitute an error marker. */
21372
21373 static struct type *
21374 lookup_die_type (struct die_info *die, const struct attribute *attr,
21375 struct dwarf2_cu *cu)
21376 {
21377 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21378 struct objfile *objfile = dwarf2_per_objfile->objfile;
21379 struct type *this_type;
21380
21381 gdb_assert (attr->name == DW_AT_type
21382 || attr->name == DW_AT_GNAT_descriptive_type
21383 || attr->name == DW_AT_containing_type);
21384
21385 /* First see if we have it cached. */
21386
21387 if (attr->form == DW_FORM_GNU_ref_alt)
21388 {
21389 struct dwarf2_per_cu_data *per_cu;
21390 sect_offset sect_off = attr->get_ref_die_offset ();
21391
21392 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21393 dwarf2_per_objfile);
21394 this_type = get_die_type_at_offset (sect_off, per_cu, dwarf2_per_objfile);
21395 }
21396 else if (attr->form_is_ref ())
21397 {
21398 sect_offset sect_off = attr->get_ref_die_offset ();
21399
21400 this_type = get_die_type_at_offset (sect_off, cu->per_cu,
21401 dwarf2_per_objfile);
21402 }
21403 else if (attr->form == DW_FORM_ref_sig8)
21404 {
21405 ULONGEST signature = DW_SIGNATURE (attr);
21406
21407 return get_signatured_type (die, signature, cu);
21408 }
21409 else
21410 {
21411 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21412 " at %s [in module %s]"),
21413 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21414 objfile_name (objfile));
21415 return build_error_marker_type (cu, die);
21416 }
21417
21418 /* If not cached we need to read it in. */
21419
21420 if (this_type == NULL)
21421 {
21422 struct die_info *type_die = NULL;
21423 struct dwarf2_cu *type_cu = cu;
21424
21425 if (attr->form_is_ref ())
21426 type_die = follow_die_ref (die, attr, &type_cu);
21427 if (type_die == NULL)
21428 return build_error_marker_type (cu, die);
21429 /* If we find the type now, it's probably because the type came
21430 from an inter-CU reference and the type's CU got expanded before
21431 ours. */
21432 this_type = read_type_die (type_die, type_cu);
21433 }
21434
21435 /* If we still don't have a type use an error marker. */
21436
21437 if (this_type == NULL)
21438 return build_error_marker_type (cu, die);
21439
21440 return this_type;
21441 }
21442
21443 /* Return the type in DIE, CU.
21444 Returns NULL for invalid types.
21445
21446 This first does a lookup in die_type_hash,
21447 and only reads the die in if necessary.
21448
21449 NOTE: This can be called when reading in partial or full symbols. */
21450
21451 static struct type *
21452 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21453 {
21454 struct type *this_type;
21455
21456 this_type = get_die_type (die, cu);
21457 if (this_type)
21458 return this_type;
21459
21460 return read_type_die_1 (die, cu);
21461 }
21462
21463 /* Read the type in DIE, CU.
21464 Returns NULL for invalid types. */
21465
21466 static struct type *
21467 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21468 {
21469 struct type *this_type = NULL;
21470
21471 switch (die->tag)
21472 {
21473 case DW_TAG_class_type:
21474 case DW_TAG_interface_type:
21475 case DW_TAG_structure_type:
21476 case DW_TAG_union_type:
21477 this_type = read_structure_type (die, cu);
21478 break;
21479 case DW_TAG_enumeration_type:
21480 this_type = read_enumeration_type (die, cu);
21481 break;
21482 case DW_TAG_subprogram:
21483 case DW_TAG_subroutine_type:
21484 case DW_TAG_inlined_subroutine:
21485 this_type = read_subroutine_type (die, cu);
21486 break;
21487 case DW_TAG_array_type:
21488 this_type = read_array_type (die, cu);
21489 break;
21490 case DW_TAG_set_type:
21491 this_type = read_set_type (die, cu);
21492 break;
21493 case DW_TAG_pointer_type:
21494 this_type = read_tag_pointer_type (die, cu);
21495 break;
21496 case DW_TAG_ptr_to_member_type:
21497 this_type = read_tag_ptr_to_member_type (die, cu);
21498 break;
21499 case DW_TAG_reference_type:
21500 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21501 break;
21502 case DW_TAG_rvalue_reference_type:
21503 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21504 break;
21505 case DW_TAG_const_type:
21506 this_type = read_tag_const_type (die, cu);
21507 break;
21508 case DW_TAG_volatile_type:
21509 this_type = read_tag_volatile_type (die, cu);
21510 break;
21511 case DW_TAG_restrict_type:
21512 this_type = read_tag_restrict_type (die, cu);
21513 break;
21514 case DW_TAG_string_type:
21515 this_type = read_tag_string_type (die, cu);
21516 break;
21517 case DW_TAG_typedef:
21518 this_type = read_typedef (die, cu);
21519 break;
21520 case DW_TAG_subrange_type:
21521 this_type = read_subrange_type (die, cu);
21522 break;
21523 case DW_TAG_base_type:
21524 this_type = read_base_type (die, cu);
21525 break;
21526 case DW_TAG_unspecified_type:
21527 this_type = read_unspecified_type (die, cu);
21528 break;
21529 case DW_TAG_namespace:
21530 this_type = read_namespace_type (die, cu);
21531 break;
21532 case DW_TAG_module:
21533 this_type = read_module_type (die, cu);
21534 break;
21535 case DW_TAG_atomic_type:
21536 this_type = read_tag_atomic_type (die, cu);
21537 break;
21538 default:
21539 complaint (_("unexpected tag in read_type_die: '%s'"),
21540 dwarf_tag_name (die->tag));
21541 break;
21542 }
21543
21544 return this_type;
21545 }
21546
21547 /* See if we can figure out if the class lives in a namespace. We do
21548 this by looking for a member function; its demangled name will
21549 contain namespace info, if there is any.
21550 Return the computed name or NULL.
21551 Space for the result is allocated on the objfile's obstack.
21552 This is the full-die version of guess_partial_die_structure_name.
21553 In this case we know DIE has no useful parent. */
21554
21555 static const char *
21556 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21557 {
21558 struct die_info *spec_die;
21559 struct dwarf2_cu *spec_cu;
21560 struct die_info *child;
21561 struct objfile *objfile = cu->per_objfile->objfile;
21562
21563 spec_cu = cu;
21564 spec_die = die_specification (die, &spec_cu);
21565 if (spec_die != NULL)
21566 {
21567 die = spec_die;
21568 cu = spec_cu;
21569 }
21570
21571 for (child = die->child;
21572 child != NULL;
21573 child = child->sibling)
21574 {
21575 if (child->tag == DW_TAG_subprogram)
21576 {
21577 const char *linkage_name = dw2_linkage_name (child, cu);
21578
21579 if (linkage_name != NULL)
21580 {
21581 gdb::unique_xmalloc_ptr<char> actual_name
21582 (language_class_name_from_physname (cu->language_defn,
21583 linkage_name));
21584 const char *name = NULL;
21585
21586 if (actual_name != NULL)
21587 {
21588 const char *die_name = dwarf2_name (die, cu);
21589
21590 if (die_name != NULL
21591 && strcmp (die_name, actual_name.get ()) != 0)
21592 {
21593 /* Strip off the class name from the full name.
21594 We want the prefix. */
21595 int die_name_len = strlen (die_name);
21596 int actual_name_len = strlen (actual_name.get ());
21597 const char *ptr = actual_name.get ();
21598
21599 /* Test for '::' as a sanity check. */
21600 if (actual_name_len > die_name_len + 2
21601 && ptr[actual_name_len - die_name_len - 1] == ':')
21602 name = obstack_strndup (
21603 &objfile->per_bfd->storage_obstack,
21604 ptr, actual_name_len - die_name_len - 2);
21605 }
21606 }
21607 return name;
21608 }
21609 }
21610 }
21611
21612 return NULL;
21613 }
21614
21615 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21616 prefix part in such case. See
21617 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21618
21619 static const char *
21620 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21621 {
21622 struct attribute *attr;
21623 const char *base;
21624
21625 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21626 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21627 return NULL;
21628
21629 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21630 return NULL;
21631
21632 attr = dw2_linkage_name_attr (die, cu);
21633 if (attr == NULL || DW_STRING (attr) == NULL)
21634 return NULL;
21635
21636 /* dwarf2_name had to be already called. */
21637 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21638
21639 /* Strip the base name, keep any leading namespaces/classes. */
21640 base = strrchr (DW_STRING (attr), ':');
21641 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21642 return "";
21643
21644 struct objfile *objfile = cu->per_objfile->objfile;
21645 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21646 DW_STRING (attr),
21647 &base[-1] - DW_STRING (attr));
21648 }
21649
21650 /* Return the name of the namespace/class that DIE is defined within,
21651 or "" if we can't tell. The caller should not xfree the result.
21652
21653 For example, if we're within the method foo() in the following
21654 code:
21655
21656 namespace N {
21657 class C {
21658 void foo () {
21659 }
21660 };
21661 }
21662
21663 then determine_prefix on foo's die will return "N::C". */
21664
21665 static const char *
21666 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21667 {
21668 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21669 struct die_info *parent, *spec_die;
21670 struct dwarf2_cu *spec_cu;
21671 struct type *parent_type;
21672 const char *retval;
21673
21674 if (cu->language != language_cplus
21675 && cu->language != language_fortran && cu->language != language_d
21676 && cu->language != language_rust)
21677 return "";
21678
21679 retval = anonymous_struct_prefix (die, cu);
21680 if (retval)
21681 return retval;
21682
21683 /* We have to be careful in the presence of DW_AT_specification.
21684 For example, with GCC 3.4, given the code
21685
21686 namespace N {
21687 void foo() {
21688 // Definition of N::foo.
21689 }
21690 }
21691
21692 then we'll have a tree of DIEs like this:
21693
21694 1: DW_TAG_compile_unit
21695 2: DW_TAG_namespace // N
21696 3: DW_TAG_subprogram // declaration of N::foo
21697 4: DW_TAG_subprogram // definition of N::foo
21698 DW_AT_specification // refers to die #3
21699
21700 Thus, when processing die #4, we have to pretend that we're in
21701 the context of its DW_AT_specification, namely the contex of die
21702 #3. */
21703 spec_cu = cu;
21704 spec_die = die_specification (die, &spec_cu);
21705 if (spec_die == NULL)
21706 parent = die->parent;
21707 else
21708 {
21709 parent = spec_die->parent;
21710 cu = spec_cu;
21711 }
21712
21713 if (parent == NULL)
21714 return "";
21715 else if (parent->building_fullname)
21716 {
21717 const char *name;
21718 const char *parent_name;
21719
21720 /* It has been seen on RealView 2.2 built binaries,
21721 DW_TAG_template_type_param types actually _defined_ as
21722 children of the parent class:
21723
21724 enum E {};
21725 template class <class Enum> Class{};
21726 Class<enum E> class_e;
21727
21728 1: DW_TAG_class_type (Class)
21729 2: DW_TAG_enumeration_type (E)
21730 3: DW_TAG_enumerator (enum1:0)
21731 3: DW_TAG_enumerator (enum2:1)
21732 ...
21733 2: DW_TAG_template_type_param
21734 DW_AT_type DW_FORM_ref_udata (E)
21735
21736 Besides being broken debug info, it can put GDB into an
21737 infinite loop. Consider:
21738
21739 When we're building the full name for Class<E>, we'll start
21740 at Class, and go look over its template type parameters,
21741 finding E. We'll then try to build the full name of E, and
21742 reach here. We're now trying to build the full name of E,
21743 and look over the parent DIE for containing scope. In the
21744 broken case, if we followed the parent DIE of E, we'd again
21745 find Class, and once again go look at its template type
21746 arguments, etc., etc. Simply don't consider such parent die
21747 as source-level parent of this die (it can't be, the language
21748 doesn't allow it), and break the loop here. */
21749 name = dwarf2_name (die, cu);
21750 parent_name = dwarf2_name (parent, cu);
21751 complaint (_("template param type '%s' defined within parent '%s'"),
21752 name ? name : "<unknown>",
21753 parent_name ? parent_name : "<unknown>");
21754 return "";
21755 }
21756 else
21757 switch (parent->tag)
21758 {
21759 case DW_TAG_namespace:
21760 parent_type = read_type_die (parent, cu);
21761 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21762 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21763 Work around this problem here. */
21764 if (cu->language == language_cplus
21765 && strcmp (parent_type->name (), "::") == 0)
21766 return "";
21767 /* We give a name to even anonymous namespaces. */
21768 return parent_type->name ();
21769 case DW_TAG_class_type:
21770 case DW_TAG_interface_type:
21771 case DW_TAG_structure_type:
21772 case DW_TAG_union_type:
21773 case DW_TAG_module:
21774 parent_type = read_type_die (parent, cu);
21775 if (parent_type->name () != NULL)
21776 return parent_type->name ();
21777 else
21778 /* An anonymous structure is only allowed non-static data
21779 members; no typedefs, no member functions, et cetera.
21780 So it does not need a prefix. */
21781 return "";
21782 case DW_TAG_compile_unit:
21783 case DW_TAG_partial_unit:
21784 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21785 if (cu->language == language_cplus
21786 && !dwarf2_per_objfile->per_bfd->types.empty ()
21787 && die->child != NULL
21788 && (die->tag == DW_TAG_class_type
21789 || die->tag == DW_TAG_structure_type
21790 || die->tag == DW_TAG_union_type))
21791 {
21792 const char *name = guess_full_die_structure_name (die, cu);
21793 if (name != NULL)
21794 return name;
21795 }
21796 return "";
21797 case DW_TAG_subprogram:
21798 /* Nested subroutines in Fortran get a prefix with the name
21799 of the parent's subroutine. */
21800 if (cu->language == language_fortran)
21801 {
21802 if ((die->tag == DW_TAG_subprogram)
21803 && (dwarf2_name (parent, cu) != NULL))
21804 return dwarf2_name (parent, cu);
21805 }
21806 return determine_prefix (parent, cu);
21807 case DW_TAG_enumeration_type:
21808 parent_type = read_type_die (parent, cu);
21809 if (TYPE_DECLARED_CLASS (parent_type))
21810 {
21811 if (parent_type->name () != NULL)
21812 return parent_type->name ();
21813 return "";
21814 }
21815 /* Fall through. */
21816 default:
21817 return determine_prefix (parent, cu);
21818 }
21819 }
21820
21821 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21822 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21823 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21824 an obconcat, otherwise allocate storage for the result. The CU argument is
21825 used to determine the language and hence, the appropriate separator. */
21826
21827 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21828
21829 static char *
21830 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21831 int physname, struct dwarf2_cu *cu)
21832 {
21833 const char *lead = "";
21834 const char *sep;
21835
21836 if (suffix == NULL || suffix[0] == '\0'
21837 || prefix == NULL || prefix[0] == '\0')
21838 sep = "";
21839 else if (cu->language == language_d)
21840 {
21841 /* For D, the 'main' function could be defined in any module, but it
21842 should never be prefixed. */
21843 if (strcmp (suffix, "D main") == 0)
21844 {
21845 prefix = "";
21846 sep = "";
21847 }
21848 else
21849 sep = ".";
21850 }
21851 else if (cu->language == language_fortran && physname)
21852 {
21853 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21854 DW_AT_MIPS_linkage_name is preferred and used instead. */
21855
21856 lead = "__";
21857 sep = "_MOD_";
21858 }
21859 else
21860 sep = "::";
21861
21862 if (prefix == NULL)
21863 prefix = "";
21864 if (suffix == NULL)
21865 suffix = "";
21866
21867 if (obs == NULL)
21868 {
21869 char *retval
21870 = ((char *)
21871 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21872
21873 strcpy (retval, lead);
21874 strcat (retval, prefix);
21875 strcat (retval, sep);
21876 strcat (retval, suffix);
21877 return retval;
21878 }
21879 else
21880 {
21881 /* We have an obstack. */
21882 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21883 }
21884 }
21885
21886 /* Get name of a die, return NULL if not found. */
21887
21888 static const char *
21889 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21890 struct objfile *objfile)
21891 {
21892 if (name && cu->language == language_cplus)
21893 {
21894 gdb::unique_xmalloc_ptr<char> canon_name
21895 = cp_canonicalize_string (name);
21896
21897 if (canon_name != nullptr)
21898 name = objfile->intern (canon_name.get ());
21899 }
21900
21901 return name;
21902 }
21903
21904 /* Get name of a die, return NULL if not found.
21905 Anonymous namespaces are converted to their magic string. */
21906
21907 static const char *
21908 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21909 {
21910 struct attribute *attr;
21911 struct objfile *objfile = cu->per_objfile->objfile;
21912
21913 attr = dwarf2_attr (die, DW_AT_name, cu);
21914 if ((!attr || !DW_STRING (attr))
21915 && die->tag != DW_TAG_namespace
21916 && die->tag != DW_TAG_class_type
21917 && die->tag != DW_TAG_interface_type
21918 && die->tag != DW_TAG_structure_type
21919 && die->tag != DW_TAG_union_type)
21920 return NULL;
21921
21922 switch (die->tag)
21923 {
21924 case DW_TAG_compile_unit:
21925 case DW_TAG_partial_unit:
21926 /* Compilation units have a DW_AT_name that is a filename, not
21927 a source language identifier. */
21928 case DW_TAG_enumeration_type:
21929 case DW_TAG_enumerator:
21930 /* These tags always have simple identifiers already; no need
21931 to canonicalize them. */
21932 return DW_STRING (attr);
21933
21934 case DW_TAG_namespace:
21935 if (attr != NULL && DW_STRING (attr) != NULL)
21936 return DW_STRING (attr);
21937 return CP_ANONYMOUS_NAMESPACE_STR;
21938
21939 case DW_TAG_class_type:
21940 case DW_TAG_interface_type:
21941 case DW_TAG_structure_type:
21942 case DW_TAG_union_type:
21943 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21944 structures or unions. These were of the form "._%d" in GCC 4.1,
21945 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21946 and GCC 4.4. We work around this problem by ignoring these. */
21947 if (attr && DW_STRING (attr)
21948 && (startswith (DW_STRING (attr), "._")
21949 || startswith (DW_STRING (attr), "<anonymous")))
21950 return NULL;
21951
21952 /* GCC might emit a nameless typedef that has a linkage name. See
21953 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21954 if (!attr || DW_STRING (attr) == NULL)
21955 {
21956 attr = dw2_linkage_name_attr (die, cu);
21957 if (attr == NULL || DW_STRING (attr) == NULL)
21958 return NULL;
21959
21960 /* Avoid demangling DW_STRING (attr) the second time on a second
21961 call for the same DIE. */
21962 if (!DW_STRING_IS_CANONICAL (attr))
21963 {
21964 gdb::unique_xmalloc_ptr<char> demangled
21965 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21966 if (demangled == nullptr)
21967 return nullptr;
21968
21969 DW_STRING (attr) = objfile->intern (demangled.get ());
21970 DW_STRING_IS_CANONICAL (attr) = 1;
21971 }
21972
21973 /* Strip any leading namespaces/classes, keep only the base name.
21974 DW_AT_name for named DIEs does not contain the prefixes. */
21975 const char *base = strrchr (DW_STRING (attr), ':');
21976 if (base && base > DW_STRING (attr) && base[-1] == ':')
21977 return &base[1];
21978 else
21979 return DW_STRING (attr);
21980 }
21981 break;
21982
21983 default:
21984 break;
21985 }
21986
21987 if (!DW_STRING_IS_CANONICAL (attr))
21988 {
21989 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21990 objfile);
21991 DW_STRING_IS_CANONICAL (attr) = 1;
21992 }
21993 return DW_STRING (attr);
21994 }
21995
21996 /* Return the die that this die in an extension of, or NULL if there
21997 is none. *EXT_CU is the CU containing DIE on input, and the CU
21998 containing the return value on output. */
21999
22000 static struct die_info *
22001 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22002 {
22003 struct attribute *attr;
22004
22005 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22006 if (attr == NULL)
22007 return NULL;
22008
22009 return follow_die_ref (die, attr, ext_cu);
22010 }
22011
22012 static void
22013 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22014 {
22015 unsigned int i;
22016
22017 print_spaces (indent, f);
22018 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22019 dwarf_tag_name (die->tag), die->abbrev,
22020 sect_offset_str (die->sect_off));
22021
22022 if (die->parent != NULL)
22023 {
22024 print_spaces (indent, f);
22025 fprintf_unfiltered (f, " parent at offset: %s\n",
22026 sect_offset_str (die->parent->sect_off));
22027 }
22028
22029 print_spaces (indent, f);
22030 fprintf_unfiltered (f, " has children: %s\n",
22031 dwarf_bool_name (die->child != NULL));
22032
22033 print_spaces (indent, f);
22034 fprintf_unfiltered (f, " attributes:\n");
22035
22036 for (i = 0; i < die->num_attrs; ++i)
22037 {
22038 print_spaces (indent, f);
22039 fprintf_unfiltered (f, " %s (%s) ",
22040 dwarf_attr_name (die->attrs[i].name),
22041 dwarf_form_name (die->attrs[i].form));
22042
22043 switch (die->attrs[i].form)
22044 {
22045 case DW_FORM_addr:
22046 case DW_FORM_addrx:
22047 case DW_FORM_GNU_addr_index:
22048 fprintf_unfiltered (f, "address: ");
22049 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22050 break;
22051 case DW_FORM_block2:
22052 case DW_FORM_block4:
22053 case DW_FORM_block:
22054 case DW_FORM_block1:
22055 fprintf_unfiltered (f, "block: size %s",
22056 pulongest (DW_BLOCK (&die->attrs[i])->size));
22057 break;
22058 case DW_FORM_exprloc:
22059 fprintf_unfiltered (f, "expression: size %s",
22060 pulongest (DW_BLOCK (&die->attrs[i])->size));
22061 break;
22062 case DW_FORM_data16:
22063 fprintf_unfiltered (f, "constant of 16 bytes");
22064 break;
22065 case DW_FORM_ref_addr:
22066 fprintf_unfiltered (f, "ref address: ");
22067 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22068 break;
22069 case DW_FORM_GNU_ref_alt:
22070 fprintf_unfiltered (f, "alt ref address: ");
22071 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22072 break;
22073 case DW_FORM_ref1:
22074 case DW_FORM_ref2:
22075 case DW_FORM_ref4:
22076 case DW_FORM_ref8:
22077 case DW_FORM_ref_udata:
22078 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22079 (long) (DW_UNSND (&die->attrs[i])));
22080 break;
22081 case DW_FORM_data1:
22082 case DW_FORM_data2:
22083 case DW_FORM_data4:
22084 case DW_FORM_data8:
22085 case DW_FORM_udata:
22086 case DW_FORM_sdata:
22087 fprintf_unfiltered (f, "constant: %s",
22088 pulongest (DW_UNSND (&die->attrs[i])));
22089 break;
22090 case DW_FORM_sec_offset:
22091 fprintf_unfiltered (f, "section offset: %s",
22092 pulongest (DW_UNSND (&die->attrs[i])));
22093 break;
22094 case DW_FORM_ref_sig8:
22095 fprintf_unfiltered (f, "signature: %s",
22096 hex_string (DW_SIGNATURE (&die->attrs[i])));
22097 break;
22098 case DW_FORM_string:
22099 case DW_FORM_strp:
22100 case DW_FORM_line_strp:
22101 case DW_FORM_strx:
22102 case DW_FORM_GNU_str_index:
22103 case DW_FORM_GNU_strp_alt:
22104 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22105 DW_STRING (&die->attrs[i])
22106 ? DW_STRING (&die->attrs[i]) : "",
22107 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22108 break;
22109 case DW_FORM_flag:
22110 if (DW_UNSND (&die->attrs[i]))
22111 fprintf_unfiltered (f, "flag: TRUE");
22112 else
22113 fprintf_unfiltered (f, "flag: FALSE");
22114 break;
22115 case DW_FORM_flag_present:
22116 fprintf_unfiltered (f, "flag: TRUE");
22117 break;
22118 case DW_FORM_indirect:
22119 /* The reader will have reduced the indirect form to
22120 the "base form" so this form should not occur. */
22121 fprintf_unfiltered (f,
22122 "unexpected attribute form: DW_FORM_indirect");
22123 break;
22124 case DW_FORM_implicit_const:
22125 fprintf_unfiltered (f, "constant: %s",
22126 plongest (DW_SND (&die->attrs[i])));
22127 break;
22128 default:
22129 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22130 die->attrs[i].form);
22131 break;
22132 }
22133 fprintf_unfiltered (f, "\n");
22134 }
22135 }
22136
22137 static void
22138 dump_die_for_error (struct die_info *die)
22139 {
22140 dump_die_shallow (gdb_stderr, 0, die);
22141 }
22142
22143 static void
22144 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22145 {
22146 int indent = level * 4;
22147
22148 gdb_assert (die != NULL);
22149
22150 if (level >= max_level)
22151 return;
22152
22153 dump_die_shallow (f, indent, die);
22154
22155 if (die->child != NULL)
22156 {
22157 print_spaces (indent, f);
22158 fprintf_unfiltered (f, " Children:");
22159 if (level + 1 < max_level)
22160 {
22161 fprintf_unfiltered (f, "\n");
22162 dump_die_1 (f, level + 1, max_level, die->child);
22163 }
22164 else
22165 {
22166 fprintf_unfiltered (f,
22167 " [not printed, max nesting level reached]\n");
22168 }
22169 }
22170
22171 if (die->sibling != NULL && level > 0)
22172 {
22173 dump_die_1 (f, level, max_level, die->sibling);
22174 }
22175 }
22176
22177 /* This is called from the pdie macro in gdbinit.in.
22178 It's not static so gcc will keep a copy callable from gdb. */
22179
22180 void
22181 dump_die (struct die_info *die, int max_level)
22182 {
22183 dump_die_1 (gdb_stdlog, 0, max_level, die);
22184 }
22185
22186 static void
22187 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22188 {
22189 void **slot;
22190
22191 slot = htab_find_slot_with_hash (cu->die_hash, die,
22192 to_underlying (die->sect_off),
22193 INSERT);
22194
22195 *slot = die;
22196 }
22197
22198 /* Follow reference or signature attribute ATTR of SRC_DIE.
22199 On entry *REF_CU is the CU of SRC_DIE.
22200 On exit *REF_CU is the CU of the result. */
22201
22202 static struct die_info *
22203 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22204 struct dwarf2_cu **ref_cu)
22205 {
22206 struct die_info *die;
22207
22208 if (attr->form_is_ref ())
22209 die = follow_die_ref (src_die, attr, ref_cu);
22210 else if (attr->form == DW_FORM_ref_sig8)
22211 die = follow_die_sig (src_die, attr, ref_cu);
22212 else
22213 {
22214 dump_die_for_error (src_die);
22215 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22216 objfile_name ((*ref_cu)->per_objfile->objfile));
22217 }
22218
22219 return die;
22220 }
22221
22222 /* Follow reference OFFSET.
22223 On entry *REF_CU is the CU of the source die referencing OFFSET.
22224 On exit *REF_CU is the CU of the result.
22225 Returns NULL if OFFSET is invalid. */
22226
22227 static struct die_info *
22228 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22229 struct dwarf2_cu **ref_cu)
22230 {
22231 struct die_info temp_die;
22232 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22233 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22234
22235 gdb_assert (cu->per_cu != NULL);
22236
22237 target_cu = cu;
22238
22239 if (cu->per_cu->is_debug_types)
22240 {
22241 /* .debug_types CUs cannot reference anything outside their CU.
22242 If they need to, they have to reference a signatured type via
22243 DW_FORM_ref_sig8. */
22244 if (!cu->header.offset_in_cu_p (sect_off))
22245 return NULL;
22246 }
22247 else if (offset_in_dwz != cu->per_cu->is_dwz
22248 || !cu->header.offset_in_cu_p (sect_off))
22249 {
22250 struct dwarf2_per_cu_data *per_cu;
22251
22252 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22253 dwarf2_per_objfile);
22254
22255 /* If necessary, add it to the queue and load its DIEs. */
22256 if (maybe_queue_comp_unit (cu, per_cu, dwarf2_per_objfile, cu->language))
22257 load_full_comp_unit (per_cu, dwarf2_per_objfile, false, cu->language);
22258
22259 target_cu = per_cu->cu;
22260 }
22261 else if (cu->dies == NULL)
22262 {
22263 /* We're loading full DIEs during partial symbol reading. */
22264 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
22265 load_full_comp_unit (cu->per_cu, dwarf2_per_objfile, false,
22266 language_minimal);
22267 }
22268
22269 *ref_cu = target_cu;
22270 temp_die.sect_off = sect_off;
22271
22272 if (target_cu != cu)
22273 target_cu->ancestor = cu;
22274
22275 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22276 &temp_die,
22277 to_underlying (sect_off));
22278 }
22279
22280 /* Follow reference attribute ATTR of SRC_DIE.
22281 On entry *REF_CU is the CU of SRC_DIE.
22282 On exit *REF_CU is the CU of the result. */
22283
22284 static struct die_info *
22285 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22286 struct dwarf2_cu **ref_cu)
22287 {
22288 sect_offset sect_off = attr->get_ref_die_offset ();
22289 struct dwarf2_cu *cu = *ref_cu;
22290 struct die_info *die;
22291
22292 die = follow_die_offset (sect_off,
22293 (attr->form == DW_FORM_GNU_ref_alt
22294 || cu->per_cu->is_dwz),
22295 ref_cu);
22296 if (!die)
22297 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22298 "at %s [in module %s]"),
22299 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22300 objfile_name (cu->per_objfile->objfile));
22301
22302 return die;
22303 }
22304
22305 /* See read.h. */
22306
22307 struct dwarf2_locexpr_baton
22308 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22309 dwarf2_per_cu_data *per_cu,
22310 dwarf2_per_objfile *dwarf2_per_objfile,
22311 CORE_ADDR (*get_frame_pc) (void *baton),
22312 void *baton, bool resolve_abstract_p)
22313 {
22314 struct dwarf2_cu *cu;
22315 struct die_info *die;
22316 struct attribute *attr;
22317 struct dwarf2_locexpr_baton retval;
22318 struct objfile *objfile = dwarf2_per_objfile->objfile;
22319
22320 if (per_cu->cu == NULL)
22321 load_cu (per_cu, dwarf2_per_objfile, false);
22322 cu = per_cu->cu;
22323 if (cu == NULL)
22324 {
22325 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22326 Instead just throw an error, not much else we can do. */
22327 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22328 sect_offset_str (sect_off), objfile_name (objfile));
22329 }
22330
22331 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22332 if (!die)
22333 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22334 sect_offset_str (sect_off), objfile_name (objfile));
22335
22336 attr = dwarf2_attr (die, DW_AT_location, cu);
22337 if (!attr && resolve_abstract_p
22338 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22339 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
22340 {
22341 CORE_ADDR pc = (*get_frame_pc) (baton);
22342 CORE_ADDR baseaddr = objfile->text_section_offset ();
22343 struct gdbarch *gdbarch = objfile->arch ();
22344
22345 for (const auto &cand_off
22346 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22347 {
22348 struct dwarf2_cu *cand_cu = cu;
22349 struct die_info *cand
22350 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22351 if (!cand
22352 || !cand->parent
22353 || cand->parent->tag != DW_TAG_subprogram)
22354 continue;
22355
22356 CORE_ADDR pc_low, pc_high;
22357 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22358 if (pc_low == ((CORE_ADDR) -1))
22359 continue;
22360 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22361 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22362 if (!(pc_low <= pc && pc < pc_high))
22363 continue;
22364
22365 die = cand;
22366 attr = dwarf2_attr (die, DW_AT_location, cu);
22367 break;
22368 }
22369 }
22370
22371 if (!attr)
22372 {
22373 /* DWARF: "If there is no such attribute, then there is no effect.".
22374 DATA is ignored if SIZE is 0. */
22375
22376 retval.data = NULL;
22377 retval.size = 0;
22378 }
22379 else if (attr->form_is_section_offset ())
22380 {
22381 struct dwarf2_loclist_baton loclist_baton;
22382 CORE_ADDR pc = (*get_frame_pc) (baton);
22383 size_t size;
22384
22385 fill_in_loclist_baton (cu, &loclist_baton, attr);
22386
22387 retval.data = dwarf2_find_location_expression (&loclist_baton,
22388 &size, pc);
22389 retval.size = size;
22390 }
22391 else
22392 {
22393 if (!attr->form_is_block ())
22394 error (_("Dwarf Error: DIE at %s referenced in module %s "
22395 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22396 sect_offset_str (sect_off), objfile_name (objfile));
22397
22398 retval.data = DW_BLOCK (attr)->data;
22399 retval.size = DW_BLOCK (attr)->size;
22400 }
22401 retval.per_objfile = dwarf2_per_objfile;
22402 retval.per_cu = cu->per_cu;
22403
22404 age_cached_comp_units (dwarf2_per_objfile);
22405
22406 return retval;
22407 }
22408
22409 /* See read.h. */
22410
22411 struct dwarf2_locexpr_baton
22412 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22413 dwarf2_per_cu_data *per_cu,
22414 dwarf2_per_objfile *per_objfile,
22415 CORE_ADDR (*get_frame_pc) (void *baton),
22416 void *baton)
22417 {
22418 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22419
22420 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22421 get_frame_pc, baton);
22422 }
22423
22424 /* Write a constant of a given type as target-ordered bytes into
22425 OBSTACK. */
22426
22427 static const gdb_byte *
22428 write_constant_as_bytes (struct obstack *obstack,
22429 enum bfd_endian byte_order,
22430 struct type *type,
22431 ULONGEST value,
22432 LONGEST *len)
22433 {
22434 gdb_byte *result;
22435
22436 *len = TYPE_LENGTH (type);
22437 result = (gdb_byte *) obstack_alloc (obstack, *len);
22438 store_unsigned_integer (result, *len, byte_order, value);
22439
22440 return result;
22441 }
22442
22443 /* See read.h. */
22444
22445 const gdb_byte *
22446 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22447 dwarf2_per_cu_data *per_cu,
22448 dwarf2_per_objfile *per_objfile,
22449 obstack *obstack,
22450 LONGEST *len)
22451 {
22452 struct dwarf2_cu *cu;
22453 struct die_info *die;
22454 struct attribute *attr;
22455 const gdb_byte *result = NULL;
22456 struct type *type;
22457 LONGEST value;
22458 enum bfd_endian byte_order;
22459 struct objfile *objfile = per_objfile->objfile;
22460
22461 if (per_cu->cu == NULL)
22462 load_cu (per_cu, per_objfile, false);
22463 cu = per_cu->cu;
22464 if (cu == NULL)
22465 {
22466 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22467 Instead just throw an error, not much else we can do. */
22468 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22469 sect_offset_str (sect_off), objfile_name (objfile));
22470 }
22471
22472 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22473 if (!die)
22474 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22475 sect_offset_str (sect_off), objfile_name (objfile));
22476
22477 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22478 if (attr == NULL)
22479 return NULL;
22480
22481 byte_order = (bfd_big_endian (objfile->obfd)
22482 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22483
22484 switch (attr->form)
22485 {
22486 case DW_FORM_addr:
22487 case DW_FORM_addrx:
22488 case DW_FORM_GNU_addr_index:
22489 {
22490 gdb_byte *tem;
22491
22492 *len = cu->header.addr_size;
22493 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22494 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22495 result = tem;
22496 }
22497 break;
22498 case DW_FORM_string:
22499 case DW_FORM_strp:
22500 case DW_FORM_strx:
22501 case DW_FORM_GNU_str_index:
22502 case DW_FORM_GNU_strp_alt:
22503 /* DW_STRING is already allocated on the objfile obstack, point
22504 directly to it. */
22505 result = (const gdb_byte *) DW_STRING (attr);
22506 *len = strlen (DW_STRING (attr));
22507 break;
22508 case DW_FORM_block1:
22509 case DW_FORM_block2:
22510 case DW_FORM_block4:
22511 case DW_FORM_block:
22512 case DW_FORM_exprloc:
22513 case DW_FORM_data16:
22514 result = DW_BLOCK (attr)->data;
22515 *len = DW_BLOCK (attr)->size;
22516 break;
22517
22518 /* The DW_AT_const_value attributes are supposed to carry the
22519 symbol's value "represented as it would be on the target
22520 architecture." By the time we get here, it's already been
22521 converted to host endianness, so we just need to sign- or
22522 zero-extend it as appropriate. */
22523 case DW_FORM_data1:
22524 type = die_type (die, cu);
22525 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22526 if (result == NULL)
22527 result = write_constant_as_bytes (obstack, byte_order,
22528 type, value, len);
22529 break;
22530 case DW_FORM_data2:
22531 type = die_type (die, cu);
22532 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22533 if (result == NULL)
22534 result = write_constant_as_bytes (obstack, byte_order,
22535 type, value, len);
22536 break;
22537 case DW_FORM_data4:
22538 type = die_type (die, cu);
22539 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22540 if (result == NULL)
22541 result = write_constant_as_bytes (obstack, byte_order,
22542 type, value, len);
22543 break;
22544 case DW_FORM_data8:
22545 type = die_type (die, cu);
22546 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22547 if (result == NULL)
22548 result = write_constant_as_bytes (obstack, byte_order,
22549 type, value, len);
22550 break;
22551
22552 case DW_FORM_sdata:
22553 case DW_FORM_implicit_const:
22554 type = die_type (die, cu);
22555 result = write_constant_as_bytes (obstack, byte_order,
22556 type, DW_SND (attr), len);
22557 break;
22558
22559 case DW_FORM_udata:
22560 type = die_type (die, cu);
22561 result = write_constant_as_bytes (obstack, byte_order,
22562 type, DW_UNSND (attr), len);
22563 break;
22564
22565 default:
22566 complaint (_("unsupported const value attribute form: '%s'"),
22567 dwarf_form_name (attr->form));
22568 break;
22569 }
22570
22571 return result;
22572 }
22573
22574 /* See read.h. */
22575
22576 struct type *
22577 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22578 dwarf2_per_cu_data *per_cu,
22579 dwarf2_per_objfile *per_objfile)
22580 {
22581 struct dwarf2_cu *cu;
22582 struct die_info *die;
22583
22584 if (per_cu->cu == NULL)
22585 load_cu (per_cu, per_objfile, false);
22586 cu = per_cu->cu;
22587 if (!cu)
22588 return NULL;
22589
22590 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22591 if (!die)
22592 return NULL;
22593
22594 return die_type (die, cu);
22595 }
22596
22597 /* See read.h. */
22598
22599 struct type *
22600 dwarf2_get_die_type (cu_offset die_offset,
22601 dwarf2_per_cu_data *per_cu,
22602 dwarf2_per_objfile *per_objfile)
22603 {
22604 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22605 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
22606 }
22607
22608 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22609 On entry *REF_CU is the CU of SRC_DIE.
22610 On exit *REF_CU is the CU of the result.
22611 Returns NULL if the referenced DIE isn't found. */
22612
22613 static struct die_info *
22614 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22615 struct dwarf2_cu **ref_cu)
22616 {
22617 struct die_info temp_die;
22618 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22619 struct die_info *die;
22620 dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
22621
22622
22623 /* While it might be nice to assert sig_type->type == NULL here,
22624 we can get here for DW_AT_imported_declaration where we need
22625 the DIE not the type. */
22626
22627 /* If necessary, add it to the queue and load its DIEs. */
22628
22629 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, dwarf2_per_objfile,
22630 language_minimal))
22631 read_signatured_type (sig_type, dwarf2_per_objfile);
22632
22633 sig_cu = sig_type->per_cu.cu;
22634 gdb_assert (sig_cu != NULL);
22635 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22636 temp_die.sect_off = sig_type->type_offset_in_section;
22637 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22638 to_underlying (temp_die.sect_off));
22639 if (die)
22640 {
22641 /* For .gdb_index version 7 keep track of included TUs.
22642 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22643 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22644 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
22645 {
22646 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22647 }
22648
22649 *ref_cu = sig_cu;
22650 if (sig_cu != cu)
22651 sig_cu->ancestor = cu;
22652
22653 return die;
22654 }
22655
22656 return NULL;
22657 }
22658
22659 /* Follow signatured type referenced by ATTR in SRC_DIE.
22660 On entry *REF_CU is the CU of SRC_DIE.
22661 On exit *REF_CU is the CU of the result.
22662 The result is the DIE of the type.
22663 If the referenced type cannot be found an error is thrown. */
22664
22665 static struct die_info *
22666 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22667 struct dwarf2_cu **ref_cu)
22668 {
22669 ULONGEST signature = DW_SIGNATURE (attr);
22670 struct signatured_type *sig_type;
22671 struct die_info *die;
22672
22673 gdb_assert (attr->form == DW_FORM_ref_sig8);
22674
22675 sig_type = lookup_signatured_type (*ref_cu, signature);
22676 /* sig_type will be NULL if the signatured type is missing from
22677 the debug info. */
22678 if (sig_type == NULL)
22679 {
22680 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22681 " from DIE at %s [in module %s]"),
22682 hex_string (signature), sect_offset_str (src_die->sect_off),
22683 objfile_name ((*ref_cu)->per_objfile->objfile));
22684 }
22685
22686 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22687 if (die == NULL)
22688 {
22689 dump_die_for_error (src_die);
22690 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22691 " from DIE at %s [in module %s]"),
22692 hex_string (signature), sect_offset_str (src_die->sect_off),
22693 objfile_name ((*ref_cu)->per_objfile->objfile));
22694 }
22695
22696 return die;
22697 }
22698
22699 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22700 reading in and processing the type unit if necessary. */
22701
22702 static struct type *
22703 get_signatured_type (struct die_info *die, ULONGEST signature,
22704 struct dwarf2_cu *cu)
22705 {
22706 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22707 struct signatured_type *sig_type;
22708 struct dwarf2_cu *type_cu;
22709 struct die_info *type_die;
22710 struct type *type;
22711
22712 sig_type = lookup_signatured_type (cu, signature);
22713 /* sig_type will be NULL if the signatured type is missing from
22714 the debug info. */
22715 if (sig_type == NULL)
22716 {
22717 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22718 " from DIE at %s [in module %s]"),
22719 hex_string (signature), sect_offset_str (die->sect_off),
22720 objfile_name (dwarf2_per_objfile->objfile));
22721 return build_error_marker_type (cu, die);
22722 }
22723
22724 /* If we already know the type we're done. */
22725 if (sig_type->type != NULL)
22726 return sig_type->type;
22727
22728 type_cu = cu;
22729 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22730 if (type_die != NULL)
22731 {
22732 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22733 is created. This is important, for example, because for c++ classes
22734 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22735 type = read_type_die (type_die, type_cu);
22736 if (type == NULL)
22737 {
22738 complaint (_("Dwarf Error: Cannot build signatured type %s"
22739 " referenced from DIE at %s [in module %s]"),
22740 hex_string (signature), sect_offset_str (die->sect_off),
22741 objfile_name (dwarf2_per_objfile->objfile));
22742 type = build_error_marker_type (cu, die);
22743 }
22744 }
22745 else
22746 {
22747 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22748 " from DIE at %s [in module %s]"),
22749 hex_string (signature), sect_offset_str (die->sect_off),
22750 objfile_name (dwarf2_per_objfile->objfile));
22751 type = build_error_marker_type (cu, die);
22752 }
22753 sig_type->type = type;
22754
22755 return type;
22756 }
22757
22758 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22759 reading in and processing the type unit if necessary. */
22760
22761 static struct type *
22762 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22763 struct dwarf2_cu *cu) /* ARI: editCase function */
22764 {
22765 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22766 if (attr->form_is_ref ())
22767 {
22768 struct dwarf2_cu *type_cu = cu;
22769 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22770
22771 return read_type_die (type_die, type_cu);
22772 }
22773 else if (attr->form == DW_FORM_ref_sig8)
22774 {
22775 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22776 }
22777 else
22778 {
22779 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22780
22781 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22782 " at %s [in module %s]"),
22783 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22784 objfile_name (dwarf2_per_objfile->objfile));
22785 return build_error_marker_type (cu, die);
22786 }
22787 }
22788
22789 /* Load the DIEs associated with type unit PER_CU into memory. */
22790
22791 static void
22792 load_full_type_unit (dwarf2_per_cu_data *per_cu,
22793 dwarf2_per_objfile *per_objfile)
22794 {
22795 struct signatured_type *sig_type;
22796
22797 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22798 gdb_assert (! per_cu->type_unit_group_p ());
22799
22800 /* We have the per_cu, but we need the signatured_type.
22801 Fortunately this is an easy translation. */
22802 gdb_assert (per_cu->is_debug_types);
22803 sig_type = (struct signatured_type *) per_cu;
22804
22805 gdb_assert (per_cu->cu == NULL);
22806
22807 read_signatured_type (sig_type, per_objfile);
22808
22809 gdb_assert (per_cu->cu != NULL);
22810 }
22811
22812 /* Read in a signatured type and build its CU and DIEs.
22813 If the type is a stub for the real type in a DWO file,
22814 read in the real type from the DWO file as well. */
22815
22816 static void
22817 read_signatured_type (signatured_type *sig_type,
22818 dwarf2_per_objfile *per_objfile)
22819 {
22820 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22821
22822 gdb_assert (per_cu->is_debug_types);
22823 gdb_assert (per_cu->cu == NULL);
22824
22825 cutu_reader reader (per_cu, per_objfile, NULL, 0, false);
22826
22827 if (!reader.dummy_p)
22828 {
22829 struct dwarf2_cu *cu = reader.cu;
22830 const gdb_byte *info_ptr = reader.info_ptr;
22831
22832 gdb_assert (cu->die_hash == NULL);
22833 cu->die_hash =
22834 htab_create_alloc_ex (cu->header.length / 12,
22835 die_hash,
22836 die_eq,
22837 NULL,
22838 &cu->comp_unit_obstack,
22839 hashtab_obstack_allocate,
22840 dummy_obstack_deallocate);
22841
22842 if (reader.comp_unit_die->has_children)
22843 reader.comp_unit_die->child
22844 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22845 reader.comp_unit_die);
22846 cu->dies = reader.comp_unit_die;
22847 /* comp_unit_die is not stored in die_hash, no need. */
22848
22849 /* We try not to read any attributes in this function, because
22850 not all CUs needed for references have been loaded yet, and
22851 symbol table processing isn't initialized. But we have to
22852 set the CU language, or we won't be able to build types
22853 correctly. Similarly, if we do not read the producer, we can
22854 not apply producer-specific interpretation. */
22855 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22856
22857 reader.keep ();
22858 }
22859
22860 sig_type->per_cu.tu_read = 1;
22861 }
22862
22863 /* Decode simple location descriptions.
22864 Given a pointer to a dwarf block that defines a location, compute
22865 the location and return the value. If COMPUTED is non-null, it is
22866 set to true to indicate that decoding was successful, and false
22867 otherwise. If COMPUTED is null, then this function may emit a
22868 complaint. */
22869
22870 static CORE_ADDR
22871 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22872 {
22873 struct objfile *objfile = cu->per_objfile->objfile;
22874 size_t i;
22875 size_t size = blk->size;
22876 const gdb_byte *data = blk->data;
22877 CORE_ADDR stack[64];
22878 int stacki;
22879 unsigned int bytes_read, unsnd;
22880 gdb_byte op;
22881
22882 if (computed != nullptr)
22883 *computed = false;
22884
22885 i = 0;
22886 stacki = 0;
22887 stack[stacki] = 0;
22888 stack[++stacki] = 0;
22889
22890 while (i < size)
22891 {
22892 op = data[i++];
22893 switch (op)
22894 {
22895 case DW_OP_lit0:
22896 case DW_OP_lit1:
22897 case DW_OP_lit2:
22898 case DW_OP_lit3:
22899 case DW_OP_lit4:
22900 case DW_OP_lit5:
22901 case DW_OP_lit6:
22902 case DW_OP_lit7:
22903 case DW_OP_lit8:
22904 case DW_OP_lit9:
22905 case DW_OP_lit10:
22906 case DW_OP_lit11:
22907 case DW_OP_lit12:
22908 case DW_OP_lit13:
22909 case DW_OP_lit14:
22910 case DW_OP_lit15:
22911 case DW_OP_lit16:
22912 case DW_OP_lit17:
22913 case DW_OP_lit18:
22914 case DW_OP_lit19:
22915 case DW_OP_lit20:
22916 case DW_OP_lit21:
22917 case DW_OP_lit22:
22918 case DW_OP_lit23:
22919 case DW_OP_lit24:
22920 case DW_OP_lit25:
22921 case DW_OP_lit26:
22922 case DW_OP_lit27:
22923 case DW_OP_lit28:
22924 case DW_OP_lit29:
22925 case DW_OP_lit30:
22926 case DW_OP_lit31:
22927 stack[++stacki] = op - DW_OP_lit0;
22928 break;
22929
22930 case DW_OP_reg0:
22931 case DW_OP_reg1:
22932 case DW_OP_reg2:
22933 case DW_OP_reg3:
22934 case DW_OP_reg4:
22935 case DW_OP_reg5:
22936 case DW_OP_reg6:
22937 case DW_OP_reg7:
22938 case DW_OP_reg8:
22939 case DW_OP_reg9:
22940 case DW_OP_reg10:
22941 case DW_OP_reg11:
22942 case DW_OP_reg12:
22943 case DW_OP_reg13:
22944 case DW_OP_reg14:
22945 case DW_OP_reg15:
22946 case DW_OP_reg16:
22947 case DW_OP_reg17:
22948 case DW_OP_reg18:
22949 case DW_OP_reg19:
22950 case DW_OP_reg20:
22951 case DW_OP_reg21:
22952 case DW_OP_reg22:
22953 case DW_OP_reg23:
22954 case DW_OP_reg24:
22955 case DW_OP_reg25:
22956 case DW_OP_reg26:
22957 case DW_OP_reg27:
22958 case DW_OP_reg28:
22959 case DW_OP_reg29:
22960 case DW_OP_reg30:
22961 case DW_OP_reg31:
22962 stack[++stacki] = op - DW_OP_reg0;
22963 if (i < size)
22964 {
22965 if (computed == nullptr)
22966 dwarf2_complex_location_expr_complaint ();
22967 else
22968 return 0;
22969 }
22970 break;
22971
22972 case DW_OP_regx:
22973 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22974 i += bytes_read;
22975 stack[++stacki] = unsnd;
22976 if (i < size)
22977 {
22978 if (computed == nullptr)
22979 dwarf2_complex_location_expr_complaint ();
22980 else
22981 return 0;
22982 }
22983 break;
22984
22985 case DW_OP_addr:
22986 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22987 &bytes_read);
22988 i += bytes_read;
22989 break;
22990
22991 case DW_OP_const1u:
22992 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22993 i += 1;
22994 break;
22995
22996 case DW_OP_const1s:
22997 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22998 i += 1;
22999 break;
23000
23001 case DW_OP_const2u:
23002 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23003 i += 2;
23004 break;
23005
23006 case DW_OP_const2s:
23007 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23008 i += 2;
23009 break;
23010
23011 case DW_OP_const4u:
23012 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23013 i += 4;
23014 break;
23015
23016 case DW_OP_const4s:
23017 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23018 i += 4;
23019 break;
23020
23021 case DW_OP_const8u:
23022 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23023 i += 8;
23024 break;
23025
23026 case DW_OP_constu:
23027 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23028 &bytes_read);
23029 i += bytes_read;
23030 break;
23031
23032 case DW_OP_consts:
23033 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23034 i += bytes_read;
23035 break;
23036
23037 case DW_OP_dup:
23038 stack[stacki + 1] = stack[stacki];
23039 stacki++;
23040 break;
23041
23042 case DW_OP_plus:
23043 stack[stacki - 1] += stack[stacki];
23044 stacki--;
23045 break;
23046
23047 case DW_OP_plus_uconst:
23048 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23049 &bytes_read);
23050 i += bytes_read;
23051 break;
23052
23053 case DW_OP_minus:
23054 stack[stacki - 1] -= stack[stacki];
23055 stacki--;
23056 break;
23057
23058 case DW_OP_deref:
23059 /* If we're not the last op, then we definitely can't encode
23060 this using GDB's address_class enum. This is valid for partial
23061 global symbols, although the variable's address will be bogus
23062 in the psymtab. */
23063 if (i < size)
23064 {
23065 if (computed == nullptr)
23066 dwarf2_complex_location_expr_complaint ();
23067 else
23068 return 0;
23069 }
23070 break;
23071
23072 case DW_OP_GNU_push_tls_address:
23073 case DW_OP_form_tls_address:
23074 /* The top of the stack has the offset from the beginning
23075 of the thread control block at which the variable is located. */
23076 /* Nothing should follow this operator, so the top of stack would
23077 be returned. */
23078 /* This is valid for partial global symbols, but the variable's
23079 address will be bogus in the psymtab. Make it always at least
23080 non-zero to not look as a variable garbage collected by linker
23081 which have DW_OP_addr 0. */
23082 if (i < size)
23083 {
23084 if (computed == nullptr)
23085 dwarf2_complex_location_expr_complaint ();
23086 else
23087 return 0;
23088 }
23089 stack[stacki]++;
23090 break;
23091
23092 case DW_OP_GNU_uninit:
23093 if (computed != nullptr)
23094 return 0;
23095 break;
23096
23097 case DW_OP_addrx:
23098 case DW_OP_GNU_addr_index:
23099 case DW_OP_GNU_const_index:
23100 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23101 &bytes_read);
23102 i += bytes_read;
23103 break;
23104
23105 default:
23106 if (computed == nullptr)
23107 {
23108 const char *name = get_DW_OP_name (op);
23109
23110 if (name)
23111 complaint (_("unsupported stack op: '%s'"),
23112 name);
23113 else
23114 complaint (_("unsupported stack op: '%02x'"),
23115 op);
23116 }
23117
23118 return (stack[stacki]);
23119 }
23120
23121 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23122 outside of the allocated space. Also enforce minimum>0. */
23123 if (stacki >= ARRAY_SIZE (stack) - 1)
23124 {
23125 if (computed == nullptr)
23126 complaint (_("location description stack overflow"));
23127 return 0;
23128 }
23129
23130 if (stacki <= 0)
23131 {
23132 if (computed == nullptr)
23133 complaint (_("location description stack underflow"));
23134 return 0;
23135 }
23136 }
23137
23138 if (computed != nullptr)
23139 *computed = true;
23140 return (stack[stacki]);
23141 }
23142
23143 /* memory allocation interface */
23144
23145 static struct dwarf_block *
23146 dwarf_alloc_block (struct dwarf2_cu *cu)
23147 {
23148 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23149 }
23150
23151 static struct die_info *
23152 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23153 {
23154 struct die_info *die;
23155 size_t size = sizeof (struct die_info);
23156
23157 if (num_attrs > 1)
23158 size += (num_attrs - 1) * sizeof (struct attribute);
23159
23160 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23161 memset (die, 0, sizeof (struct die_info));
23162 return (die);
23163 }
23164
23165 \f
23166
23167 /* Macro support. */
23168
23169 /* An overload of dwarf_decode_macros that finds the correct section
23170 and ensures it is read in before calling the other overload. */
23171
23172 static void
23173 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23174 int section_is_gnu)
23175 {
23176 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23177 struct objfile *objfile = dwarf2_per_objfile->objfile;
23178 const struct line_header *lh = cu->line_header;
23179 unsigned int offset_size = cu->header.offset_size;
23180 struct dwarf2_section_info *section;
23181 const char *section_name;
23182
23183 if (cu->dwo_unit != nullptr)
23184 {
23185 if (section_is_gnu)
23186 {
23187 section = &cu->dwo_unit->dwo_file->sections.macro;
23188 section_name = ".debug_macro.dwo";
23189 }
23190 else
23191 {
23192 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23193 section_name = ".debug_macinfo.dwo";
23194 }
23195 }
23196 else
23197 {
23198 if (section_is_gnu)
23199 {
23200 section = &dwarf2_per_objfile->per_bfd->macro;
23201 section_name = ".debug_macro";
23202 }
23203 else
23204 {
23205 section = &dwarf2_per_objfile->per_bfd->macinfo;
23206 section_name = ".debug_macinfo";
23207 }
23208 }
23209
23210 section->read (objfile);
23211 if (section->buffer == nullptr)
23212 {
23213 complaint (_("missing %s section"), section_name);
23214 return;
23215 }
23216
23217 buildsym_compunit *builder = cu->get_builder ();
23218
23219 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23220 offset_size, offset, section_is_gnu);
23221 }
23222
23223 /* Return the .debug_loc section to use for CU.
23224 For DWO files use .debug_loc.dwo. */
23225
23226 static struct dwarf2_section_info *
23227 cu_debug_loc_section (struct dwarf2_cu *cu)
23228 {
23229 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23230
23231 if (cu->dwo_unit)
23232 {
23233 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23234
23235 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23236 }
23237 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23238 : &dwarf2_per_objfile->per_bfd->loc);
23239 }
23240
23241 /* A helper function that fills in a dwarf2_loclist_baton. */
23242
23243 static void
23244 fill_in_loclist_baton (struct dwarf2_cu *cu,
23245 struct dwarf2_loclist_baton *baton,
23246 const struct attribute *attr)
23247 {
23248 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23249 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23250
23251 section->read (dwarf2_per_objfile->objfile);
23252
23253 baton->per_objfile = dwarf2_per_objfile;
23254 baton->per_cu = cu->per_cu;
23255 gdb_assert (baton->per_cu);
23256 /* We don't know how long the location list is, but make sure we
23257 don't run off the edge of the section. */
23258 baton->size = section->size - DW_UNSND (attr);
23259 baton->data = section->buffer + DW_UNSND (attr);
23260 if (cu->base_address.has_value ())
23261 baton->base_address = *cu->base_address;
23262 else
23263 baton->base_address = 0;
23264 baton->from_dwo = cu->dwo_unit != NULL;
23265 }
23266
23267 static void
23268 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23269 struct dwarf2_cu *cu, int is_block)
23270 {
23271 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23272 struct objfile *objfile = dwarf2_per_objfile->objfile;
23273 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23274
23275 if (attr->form_is_section_offset ()
23276 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23277 the section. If so, fall through to the complaint in the
23278 other branch. */
23279 && DW_UNSND (attr) < section->get_size (objfile))
23280 {
23281 struct dwarf2_loclist_baton *baton;
23282
23283 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23284
23285 fill_in_loclist_baton (cu, baton, attr);
23286
23287 if (!cu->base_address.has_value ())
23288 complaint (_("Location list used without "
23289 "specifying the CU base address."));
23290
23291 SYMBOL_ACLASS_INDEX (sym) = (is_block
23292 ? dwarf2_loclist_block_index
23293 : dwarf2_loclist_index);
23294 SYMBOL_LOCATION_BATON (sym) = baton;
23295 }
23296 else
23297 {
23298 struct dwarf2_locexpr_baton *baton;
23299
23300 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23301 baton->per_objfile = dwarf2_per_objfile;
23302 baton->per_cu = cu->per_cu;
23303 gdb_assert (baton->per_cu);
23304
23305 if (attr->form_is_block ())
23306 {
23307 /* Note that we're just copying the block's data pointer
23308 here, not the actual data. We're still pointing into the
23309 info_buffer for SYM's objfile; right now we never release
23310 that buffer, but when we do clean up properly this may
23311 need to change. */
23312 baton->size = DW_BLOCK (attr)->size;
23313 baton->data = DW_BLOCK (attr)->data;
23314 }
23315 else
23316 {
23317 dwarf2_invalid_attrib_class_complaint ("location description",
23318 sym->natural_name ());
23319 baton->size = 0;
23320 }
23321
23322 SYMBOL_ACLASS_INDEX (sym) = (is_block
23323 ? dwarf2_locexpr_block_index
23324 : dwarf2_locexpr_index);
23325 SYMBOL_LOCATION_BATON (sym) = baton;
23326 }
23327 }
23328
23329 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23330 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23331 CU_HEADERP first. */
23332
23333 static const struct comp_unit_head *
23334 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23335 const struct dwarf2_per_cu_data *per_cu)
23336 {
23337 const gdb_byte *info_ptr;
23338
23339 if (per_cu->cu)
23340 return &per_cu->cu->header;
23341
23342 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
23343
23344 memset (cu_headerp, 0, sizeof (*cu_headerp));
23345 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23346 rcuh_kind::COMPILE);
23347
23348 return cu_headerp;
23349 }
23350
23351 /* See read.h. */
23352
23353 int
23354 dwarf2_per_cu_data::addr_size () const
23355 {
23356 struct comp_unit_head cu_header_local;
23357 const struct comp_unit_head *cu_headerp;
23358
23359 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23360
23361 return cu_headerp->addr_size;
23362 }
23363
23364 /* See read.h. */
23365
23366 int
23367 dwarf2_per_cu_data::offset_size () const
23368 {
23369 struct comp_unit_head cu_header_local;
23370 const struct comp_unit_head *cu_headerp;
23371
23372 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23373
23374 return cu_headerp->offset_size;
23375 }
23376
23377 /* See read.h. */
23378
23379 int
23380 dwarf2_per_cu_data::ref_addr_size () const
23381 {
23382 struct comp_unit_head cu_header_local;
23383 const struct comp_unit_head *cu_headerp;
23384
23385 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23386
23387 if (cu_headerp->version == 2)
23388 return cu_headerp->addr_size;
23389 else
23390 return cu_headerp->offset_size;
23391 }
23392
23393 /* See read.h. */
23394
23395 struct type *
23396 dwarf2_cu::addr_type () const
23397 {
23398 struct objfile *objfile = this->per_objfile->objfile;
23399 struct type *void_type = objfile_type (objfile)->builtin_void;
23400 struct type *addr_type = lookup_pointer_type (void_type);
23401 int addr_size = this->per_cu->addr_size ();
23402
23403 if (TYPE_LENGTH (addr_type) == addr_size)
23404 return addr_type;
23405
23406 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23407 return addr_type;
23408 }
23409
23410 /* A helper function for dwarf2_find_containing_comp_unit that returns
23411 the index of the result, and that searches a vector. It will
23412 return a result even if the offset in question does not actually
23413 occur in any CU. This is separate so that it can be unit
23414 tested. */
23415
23416 static int
23417 dwarf2_find_containing_comp_unit
23418 (sect_offset sect_off,
23419 unsigned int offset_in_dwz,
23420 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23421 {
23422 int low, high;
23423
23424 low = 0;
23425 high = all_comp_units.size () - 1;
23426 while (high > low)
23427 {
23428 struct dwarf2_per_cu_data *mid_cu;
23429 int mid = low + (high - low) / 2;
23430
23431 mid_cu = all_comp_units[mid];
23432 if (mid_cu->is_dwz > offset_in_dwz
23433 || (mid_cu->is_dwz == offset_in_dwz
23434 && mid_cu->sect_off + mid_cu->length > sect_off))
23435 high = mid;
23436 else
23437 low = mid + 1;
23438 }
23439 gdb_assert (low == high);
23440 return low;
23441 }
23442
23443 /* Locate the .debug_info compilation unit from CU's objfile which contains
23444 the DIE at OFFSET. Raises an error on failure. */
23445
23446 static struct dwarf2_per_cu_data *
23447 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23448 unsigned int offset_in_dwz,
23449 struct dwarf2_per_objfile *dwarf2_per_objfile)
23450 {
23451 int low
23452 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23453 dwarf2_per_objfile->per_bfd->all_comp_units);
23454 struct dwarf2_per_cu_data *this_cu
23455 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
23456
23457 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23458 {
23459 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23460 error (_("Dwarf Error: could not find partial DIE containing "
23461 "offset %s [in module %s]"),
23462 sect_offset_str (sect_off),
23463 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23464
23465 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
23466 <= sect_off);
23467 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
23468 }
23469 else
23470 {
23471 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
23472 && sect_off >= this_cu->sect_off + this_cu->length)
23473 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23474 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23475 return this_cu;
23476 }
23477 }
23478
23479 #if GDB_SELF_TEST
23480
23481 namespace selftests {
23482 namespace find_containing_comp_unit {
23483
23484 static void
23485 run_test ()
23486 {
23487 struct dwarf2_per_cu_data one {};
23488 struct dwarf2_per_cu_data two {};
23489 struct dwarf2_per_cu_data three {};
23490 struct dwarf2_per_cu_data four {};
23491
23492 one.length = 5;
23493 two.sect_off = sect_offset (one.length);
23494 two.length = 7;
23495
23496 three.length = 5;
23497 three.is_dwz = 1;
23498 four.sect_off = sect_offset (three.length);
23499 four.length = 7;
23500 four.is_dwz = 1;
23501
23502 std::vector<dwarf2_per_cu_data *> units;
23503 units.push_back (&one);
23504 units.push_back (&two);
23505 units.push_back (&three);
23506 units.push_back (&four);
23507
23508 int result;
23509
23510 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23511 SELF_CHECK (units[result] == &one);
23512 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23513 SELF_CHECK (units[result] == &one);
23514 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23515 SELF_CHECK (units[result] == &two);
23516
23517 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23518 SELF_CHECK (units[result] == &three);
23519 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23520 SELF_CHECK (units[result] == &three);
23521 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23522 SELF_CHECK (units[result] == &four);
23523 }
23524
23525 }
23526 }
23527
23528 #endif /* GDB_SELF_TEST */
23529
23530 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
23531
23532 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23533 dwarf2_per_objfile *per_objfile)
23534 : per_cu (per_cu),
23535 per_objfile (per_objfile),
23536 mark (false),
23537 has_loclist (false),
23538 checked_producer (false),
23539 producer_is_gxx_lt_4_6 (false),
23540 producer_is_gcc_lt_4_3 (false),
23541 producer_is_icc (false),
23542 producer_is_icc_lt_14 (false),
23543 producer_is_codewarrior (false),
23544 processing_has_namespace_info (false)
23545 {
23546 per_cu->cu = this;
23547 }
23548
23549 /* Destroy a dwarf2_cu. */
23550
23551 dwarf2_cu::~dwarf2_cu ()
23552 {
23553 per_cu->cu = NULL;
23554 }
23555
23556 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23557
23558 static void
23559 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23560 enum language pretend_language)
23561 {
23562 struct attribute *attr;
23563
23564 /* Set the language we're debugging. */
23565 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23566 if (attr != nullptr)
23567 set_cu_language (DW_UNSND (attr), cu);
23568 else
23569 {
23570 cu->language = pretend_language;
23571 cu->language_defn = language_def (cu->language);
23572 }
23573
23574 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23575 }
23576
23577 /* Increase the age counter on each cached compilation unit, and free
23578 any that are too old. */
23579
23580 static void
23581 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23582 {
23583 struct dwarf2_per_cu_data *per_cu, **last_chain;
23584
23585 dwarf2_clear_marks (dwarf2_per_objfile->per_bfd->read_in_chain);
23586 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23587 while (per_cu != NULL)
23588 {
23589 per_cu->cu->last_used ++;
23590 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23591 dwarf2_mark (per_cu->cu);
23592 per_cu = per_cu->cu->read_in_chain;
23593 }
23594
23595 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23596 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23597 while (per_cu != NULL)
23598 {
23599 struct dwarf2_per_cu_data *next_cu;
23600
23601 next_cu = per_cu->cu->read_in_chain;
23602
23603 if (!per_cu->cu->mark)
23604 {
23605 delete per_cu->cu;
23606 *last_chain = next_cu;
23607 }
23608 else
23609 last_chain = &per_cu->cu->read_in_chain;
23610
23611 per_cu = next_cu;
23612 }
23613 }
23614
23615 /* Remove a single compilation unit from the cache. */
23616
23617 static void
23618 free_one_cached_comp_unit (dwarf2_per_cu_data *target_per_cu,
23619 dwarf2_per_objfile *dwarf2_per_objfile)
23620 {
23621 struct dwarf2_per_cu_data *per_cu, **last_chain;
23622
23623 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23624 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23625 while (per_cu != NULL)
23626 {
23627 struct dwarf2_per_cu_data *next_cu;
23628
23629 next_cu = per_cu->cu->read_in_chain;
23630
23631 if (per_cu == target_per_cu)
23632 {
23633 delete per_cu->cu;
23634 per_cu->cu = NULL;
23635 *last_chain = next_cu;
23636 break;
23637 }
23638 else
23639 last_chain = &per_cu->cu->read_in_chain;
23640
23641 per_cu = next_cu;
23642 }
23643 }
23644
23645 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23646 We store these in a hash table separate from the DIEs, and preserve them
23647 when the DIEs are flushed out of cache.
23648
23649 The CU "per_cu" pointer is needed because offset alone is not enough to
23650 uniquely identify the type. A file may have multiple .debug_types sections,
23651 or the type may come from a DWO file. Furthermore, while it's more logical
23652 to use per_cu->section+offset, with Fission the section with the data is in
23653 the DWO file but we don't know that section at the point we need it.
23654 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23655 because we can enter the lookup routine, get_die_type_at_offset, from
23656 outside this file, and thus won't necessarily have PER_CU->cu.
23657 Fortunately, PER_CU is stable for the life of the objfile. */
23658
23659 struct dwarf2_per_cu_offset_and_type
23660 {
23661 const struct dwarf2_per_cu_data *per_cu;
23662 sect_offset sect_off;
23663 struct type *type;
23664 };
23665
23666 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23667
23668 static hashval_t
23669 per_cu_offset_and_type_hash (const void *item)
23670 {
23671 const struct dwarf2_per_cu_offset_and_type *ofs
23672 = (const struct dwarf2_per_cu_offset_and_type *) item;
23673
23674 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23675 }
23676
23677 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23678
23679 static int
23680 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23681 {
23682 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23683 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23684 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23685 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23686
23687 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23688 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23689 }
23690
23691 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23692 table if necessary. For convenience, return TYPE.
23693
23694 The DIEs reading must have careful ordering to:
23695 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23696 reading current DIE.
23697 * Not trying to dereference contents of still incompletely read in types
23698 while reading in other DIEs.
23699 * Enable referencing still incompletely read in types just by a pointer to
23700 the type without accessing its fields.
23701
23702 Therefore caller should follow these rules:
23703 * Try to fetch any prerequisite types we may need to build this DIE type
23704 before building the type and calling set_die_type.
23705 * After building type call set_die_type for current DIE as soon as
23706 possible before fetching more types to complete the current type.
23707 * Make the type as complete as possible before fetching more types. */
23708
23709 static struct type *
23710 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23711 {
23712 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23713 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23714 struct objfile *objfile = dwarf2_per_objfile->objfile;
23715 struct attribute *attr;
23716 struct dynamic_prop prop;
23717
23718 /* For Ada types, make sure that the gnat-specific data is always
23719 initialized (if not already set). There are a few types where
23720 we should not be doing so, because the type-specific area is
23721 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23722 where the type-specific area is used to store the floatformat).
23723 But this is not a problem, because the gnat-specific information
23724 is actually not needed for these types. */
23725 if (need_gnat_info (cu)
23726 && type->code () != TYPE_CODE_FUNC
23727 && type->code () != TYPE_CODE_FLT
23728 && type->code () != TYPE_CODE_METHODPTR
23729 && type->code () != TYPE_CODE_MEMBERPTR
23730 && type->code () != TYPE_CODE_METHOD
23731 && !HAVE_GNAT_AUX_INFO (type))
23732 INIT_GNAT_SPECIFIC (type);
23733
23734 /* Read DW_AT_allocated and set in type. */
23735 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23736 if (attr != NULL && attr->form_is_block ())
23737 {
23738 struct type *prop_type = cu->addr_sized_int_type (false);
23739 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23740 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23741 }
23742 else if (attr != NULL)
23743 {
23744 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23745 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23746 sect_offset_str (die->sect_off));
23747 }
23748
23749 /* Read DW_AT_associated and set in type. */
23750 attr = dwarf2_attr (die, DW_AT_associated, cu);
23751 if (attr != NULL && attr->form_is_block ())
23752 {
23753 struct type *prop_type = cu->addr_sized_int_type (false);
23754 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23755 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23756 }
23757 else if (attr != NULL)
23758 {
23759 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23760 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23761 sect_offset_str (die->sect_off));
23762 }
23763
23764 /* Read DW_AT_data_location and set in type. */
23765 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23766 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
23767 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23768
23769 if (dwarf2_per_objfile->die_type_hash == NULL)
23770 dwarf2_per_objfile->die_type_hash
23771 = htab_up (htab_create_alloc (127,
23772 per_cu_offset_and_type_hash,
23773 per_cu_offset_and_type_eq,
23774 NULL, xcalloc, xfree));
23775
23776 ofs.per_cu = cu->per_cu;
23777 ofs.sect_off = die->sect_off;
23778 ofs.type = type;
23779 slot = (struct dwarf2_per_cu_offset_and_type **)
23780 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23781 if (*slot)
23782 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23783 sect_offset_str (die->sect_off));
23784 *slot = XOBNEW (&objfile->objfile_obstack,
23785 struct dwarf2_per_cu_offset_and_type);
23786 **slot = ofs;
23787 return type;
23788 }
23789
23790 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23791 or return NULL if the die does not have a saved type. */
23792
23793 static struct type *
23794 get_die_type_at_offset (sect_offset sect_off,
23795 dwarf2_per_cu_data *per_cu,
23796 dwarf2_per_objfile *dwarf2_per_objfile)
23797 {
23798 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23799
23800 if (dwarf2_per_objfile->die_type_hash == NULL)
23801 return NULL;
23802
23803 ofs.per_cu = per_cu;
23804 ofs.sect_off = sect_off;
23805 slot = ((struct dwarf2_per_cu_offset_and_type *)
23806 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23807 if (slot)
23808 return slot->type;
23809 else
23810 return NULL;
23811 }
23812
23813 /* Look up the type for DIE in CU in die_type_hash,
23814 or return NULL if DIE does not have a saved type. */
23815
23816 static struct type *
23817 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23818 {
23819 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
23820 }
23821
23822 /* Add a dependence relationship from CU to REF_PER_CU. */
23823
23824 static void
23825 dwarf2_add_dependence (struct dwarf2_cu *cu,
23826 struct dwarf2_per_cu_data *ref_per_cu)
23827 {
23828 void **slot;
23829
23830 if (cu->dependencies == NULL)
23831 cu->dependencies
23832 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23833 NULL, &cu->comp_unit_obstack,
23834 hashtab_obstack_allocate,
23835 dummy_obstack_deallocate);
23836
23837 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23838 if (*slot == NULL)
23839 *slot = ref_per_cu;
23840 }
23841
23842 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23843 Set the mark field in every compilation unit in the
23844 cache that we must keep because we are keeping CU. */
23845
23846 static int
23847 dwarf2_mark_helper (void **slot, void *data)
23848 {
23849 struct dwarf2_per_cu_data *per_cu;
23850
23851 per_cu = (struct dwarf2_per_cu_data *) *slot;
23852
23853 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23854 reading of the chain. As such dependencies remain valid it is not much
23855 useful to track and undo them during QUIT cleanups. */
23856 if (per_cu->cu == NULL)
23857 return 1;
23858
23859 if (per_cu->cu->mark)
23860 return 1;
23861 per_cu->cu->mark = true;
23862
23863 if (per_cu->cu->dependencies != NULL)
23864 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23865
23866 return 1;
23867 }
23868
23869 /* Set the mark field in CU and in every other compilation unit in the
23870 cache that we must keep because we are keeping CU. */
23871
23872 static void
23873 dwarf2_mark (struct dwarf2_cu *cu)
23874 {
23875 if (cu->mark)
23876 return;
23877 cu->mark = true;
23878 if (cu->dependencies != NULL)
23879 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23880 }
23881
23882 static void
23883 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23884 {
23885 while (per_cu)
23886 {
23887 per_cu->cu->mark = false;
23888 per_cu = per_cu->cu->read_in_chain;
23889 }
23890 }
23891
23892 /* Trivial hash function for partial_die_info: the hash value of a DIE
23893 is its offset in .debug_info for this objfile. */
23894
23895 static hashval_t
23896 partial_die_hash (const void *item)
23897 {
23898 const struct partial_die_info *part_die
23899 = (const struct partial_die_info *) item;
23900
23901 return to_underlying (part_die->sect_off);
23902 }
23903
23904 /* Trivial comparison function for partial_die_info structures: two DIEs
23905 are equal if they have the same offset. */
23906
23907 static int
23908 partial_die_eq (const void *item_lhs, const void *item_rhs)
23909 {
23910 const struct partial_die_info *part_die_lhs
23911 = (const struct partial_die_info *) item_lhs;
23912 const struct partial_die_info *part_die_rhs
23913 = (const struct partial_die_info *) item_rhs;
23914
23915 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23916 }
23917
23918 struct cmd_list_element *set_dwarf_cmdlist;
23919 struct cmd_list_element *show_dwarf_cmdlist;
23920
23921 static void
23922 show_check_physname (struct ui_file *file, int from_tty,
23923 struct cmd_list_element *c, const char *value)
23924 {
23925 fprintf_filtered (file,
23926 _("Whether to check \"physname\" is %s.\n"),
23927 value);
23928 }
23929
23930 void _initialize_dwarf2_read ();
23931 void
23932 _initialize_dwarf2_read ()
23933 {
23934 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
23935 Set DWARF specific variables.\n\
23936 Configure DWARF variables such as the cache size."),
23937 &set_dwarf_cmdlist, "maintenance set dwarf ",
23938 0/*allow-unknown*/, &maintenance_set_cmdlist);
23939
23940 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
23941 Show DWARF specific variables.\n\
23942 Show DWARF variables such as the cache size."),
23943 &show_dwarf_cmdlist, "maintenance show dwarf ",
23944 0/*allow-unknown*/, &maintenance_show_cmdlist);
23945
23946 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23947 &dwarf_max_cache_age, _("\
23948 Set the upper bound on the age of cached DWARF compilation units."), _("\
23949 Show the upper bound on the age of cached DWARF compilation units."), _("\
23950 A higher limit means that cached compilation units will be stored\n\
23951 in memory longer, and more total memory will be used. Zero disables\n\
23952 caching, which can slow down startup."),
23953 NULL,
23954 show_dwarf_max_cache_age,
23955 &set_dwarf_cmdlist,
23956 &show_dwarf_cmdlist);
23957
23958 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23959 Set debugging of the DWARF reader."), _("\
23960 Show debugging of the DWARF reader."), _("\
23961 When enabled (non-zero), debugging messages are printed during DWARF\n\
23962 reading and symtab expansion. A value of 1 (one) provides basic\n\
23963 information. A value greater than 1 provides more verbose information."),
23964 NULL,
23965 NULL,
23966 &setdebuglist, &showdebuglist);
23967
23968 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23969 Set debugging of the DWARF DIE reader."), _("\
23970 Show debugging of the DWARF DIE reader."), _("\
23971 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23972 The value is the maximum depth to print."),
23973 NULL,
23974 NULL,
23975 &setdebuglist, &showdebuglist);
23976
23977 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23978 Set debugging of the dwarf line reader."), _("\
23979 Show debugging of the dwarf line reader."), _("\
23980 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23981 A value of 1 (one) provides basic information.\n\
23982 A value greater than 1 provides more verbose information."),
23983 NULL,
23984 NULL,
23985 &setdebuglist, &showdebuglist);
23986
23987 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23988 Set cross-checking of \"physname\" code against demangler."), _("\
23989 Show cross-checking of \"physname\" code against demangler."), _("\
23990 When enabled, GDB's internal \"physname\" code is checked against\n\
23991 the demangler."),
23992 NULL, show_check_physname,
23993 &setdebuglist, &showdebuglist);
23994
23995 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23996 no_class, &use_deprecated_index_sections, _("\
23997 Set whether to use deprecated gdb_index sections."), _("\
23998 Show whether to use deprecated gdb_index sections."), _("\
23999 When enabled, deprecated .gdb_index sections are used anyway.\n\
24000 Normally they are ignored either because of a missing feature or\n\
24001 performance issue.\n\
24002 Warning: This option must be enabled before gdb reads the file."),
24003 NULL,
24004 NULL,
24005 &setlist, &showlist);
24006
24007 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24008 &dwarf2_locexpr_funcs);
24009 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24010 &dwarf2_loclist_funcs);
24011
24012 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24013 &dwarf2_block_frame_base_locexpr_funcs);
24014 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24015 &dwarf2_block_frame_base_loclist_funcs);
24016
24017 #if GDB_SELF_TEST
24018 selftests::register_test ("dw2_expand_symtabs_matching",
24019 selftests::dw2_expand_symtabs_matching::run_test);
24020 selftests::register_test ("dwarf2_find_containing_comp_unit",
24021 selftests::find_containing_comp_unit::run_test);
24022 #endif
24023 }