gdb: Convert language la_class_name_from_physname field to a method
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "dwarf2/stringify.h"
44 #include "bfd.h"
45 #include "elf-bfd.h"
46 #include "symtab.h"
47 #include "gdbtypes.h"
48 #include "objfiles.h"
49 #include "dwarf2.h"
50 #include "buildsym.h"
51 #include "demangle.h"
52 #include "gdb-demangle.h"
53 #include "filenames.h" /* for DOSish file names */
54 #include "language.h"
55 #include "complaints.h"
56 #include "dwarf2/expr.h"
57 #include "dwarf2/loc.h"
58 #include "cp-support.h"
59 #include "hashtab.h"
60 #include "command.h"
61 #include "gdbcmd.h"
62 #include "block.h"
63 #include "addrmap.h"
64 #include "typeprint.h"
65 #include "psympriv.h"
66 #include "c-lang.h"
67 #include "go-lang.h"
68 #include "valprint.h"
69 #include "gdbcore.h" /* for gnutarget */
70 #include "gdb/gdb-index.h"
71 #include "gdb_bfd.h"
72 #include "f-lang.h"
73 #include "source.h"
74 #include "build-id.h"
75 #include "namespace.h"
76 #include "gdbsupport/function-view.h"
77 #include "gdbsupport/gdb_optional.h"
78 #include "gdbsupport/underlying.h"
79 #include "gdbsupport/hash_enum.h"
80 #include "filename-seen-cache.h"
81 #include "producer.h"
82 #include <fcntl.h>
83 #include <algorithm>
84 #include <unordered_map>
85 #include "gdbsupport/selftest.h"
86 #include "rust-lang.h"
87 #include "gdbsupport/pathstuff.h"
88 #include "count-one-bits.h"
89 #include "debuginfod-support.h"
90
91 /* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94 static unsigned int dwarf_read_debug = 0;
95
96 /* When non-zero, dump DIEs after they are read in. */
97 static unsigned int dwarf_die_debug = 0;
98
99 /* When non-zero, dump line number entries as they are read in. */
100 unsigned int dwarf_line_debug = 0;
101
102 /* When true, cross-check physname against demangler. */
103 static bool check_physname = false;
104
105 /* When true, do not reject deprecated .gdb_index sections. */
106 static bool use_deprecated_index_sections = false;
107
108 /* This is used to store the data that is always per objfile. */
109 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
110
111 /* These are used to store the dwarf2_per_bfd objects.
112
113 objfiles having the same BFD, which doesn't require relocations, are going to
114 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
115
116 Other objfiles are not going to share a dwarf2_per_bfd with any other
117 objfiles, so they'll have their own version kept in the _objfile_data_key
118 version. */
119 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
120 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
121
122 /* The "aclass" indices for various kinds of computed DWARF symbols. */
123
124 static int dwarf2_locexpr_index;
125 static int dwarf2_loclist_index;
126 static int dwarf2_locexpr_block_index;
127 static int dwarf2_loclist_block_index;
128
129 /* Size of .debug_loclists section header for 32-bit DWARF format. */
130 #define LOCLIST_HEADER_SIZE32 12
131
132 /* Size of .debug_loclists section header for 64-bit DWARF format. */
133 #define LOCLIST_HEADER_SIZE64 20
134
135 /* An index into a (C++) symbol name component in a symbol name as
136 recorded in the mapped_index's symbol table. For each C++ symbol
137 in the symbol table, we record one entry for the start of each
138 component in the symbol in a table of name components, and then
139 sort the table, in order to be able to binary search symbol names,
140 ignoring leading namespaces, both completion and regular look up.
141 For example, for symbol "A::B::C", we'll have an entry that points
142 to "A::B::C", another that points to "B::C", and another for "C".
143 Note that function symbols in GDB index have no parameter
144 information, just the function/method names. You can convert a
145 name_component to a "const char *" using the
146 'mapped_index::symbol_name_at(offset_type)' method. */
147
148 struct name_component
149 {
150 /* Offset in the symbol name where the component starts. Stored as
151 a (32-bit) offset instead of a pointer to save memory and improve
152 locality on 64-bit architectures. */
153 offset_type name_offset;
154
155 /* The symbol's index in the symbol and constant pool tables of a
156 mapped_index. */
157 offset_type idx;
158 };
159
160 /* Base class containing bits shared by both .gdb_index and
161 .debug_name indexes. */
162
163 struct mapped_index_base
164 {
165 mapped_index_base () = default;
166 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
167
168 /* The name_component table (a sorted vector). See name_component's
169 description above. */
170 std::vector<name_component> name_components;
171
172 /* How NAME_COMPONENTS is sorted. */
173 enum case_sensitivity name_components_casing;
174
175 /* Return the number of names in the symbol table. */
176 virtual size_t symbol_name_count () const = 0;
177
178 /* Get the name of the symbol at IDX in the symbol table. */
179 virtual const char *symbol_name_at
180 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
181
182 /* Return whether the name at IDX in the symbol table should be
183 ignored. */
184 virtual bool symbol_name_slot_invalid (offset_type idx) const
185 {
186 return false;
187 }
188
189 /* Build the symbol name component sorted vector, if we haven't
190 yet. */
191 void build_name_components (dwarf2_per_objfile *per_objfile);
192
193 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
194 possible matches for LN_NO_PARAMS in the name component
195 vector. */
196 std::pair<std::vector<name_component>::const_iterator,
197 std::vector<name_component>::const_iterator>
198 find_name_components_bounds (const lookup_name_info &ln_no_params,
199 enum language lang,
200 dwarf2_per_objfile *per_objfile) const;
201
202 /* Prevent deleting/destroying via a base class pointer. */
203 protected:
204 ~mapped_index_base() = default;
205 };
206
207 /* A description of the mapped index. The file format is described in
208 a comment by the code that writes the index. */
209 struct mapped_index final : public mapped_index_base
210 {
211 /* A slot/bucket in the symbol table hash. */
212 struct symbol_table_slot
213 {
214 const offset_type name;
215 const offset_type vec;
216 };
217
218 /* Index data format version. */
219 int version = 0;
220
221 /* The address table data. */
222 gdb::array_view<const gdb_byte> address_table;
223
224 /* The symbol table, implemented as a hash table. */
225 gdb::array_view<symbol_table_slot> symbol_table;
226
227 /* A pointer to the constant pool. */
228 const char *constant_pool = nullptr;
229
230 bool symbol_name_slot_invalid (offset_type idx) const override
231 {
232 const auto &bucket = this->symbol_table[idx];
233 return bucket.name == 0 && bucket.vec == 0;
234 }
235
236 /* Convenience method to get at the name of the symbol at IDX in the
237 symbol table. */
238 const char *symbol_name_at
239 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
240 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
241
242 size_t symbol_name_count () const override
243 { return this->symbol_table.size (); }
244 };
245
246 /* A description of the mapped .debug_names.
247 Uninitialized map has CU_COUNT 0. */
248 struct mapped_debug_names final : public mapped_index_base
249 {
250 bfd_endian dwarf5_byte_order;
251 bool dwarf5_is_dwarf64;
252 bool augmentation_is_gdb;
253 uint8_t offset_size;
254 uint32_t cu_count = 0;
255 uint32_t tu_count, bucket_count, name_count;
256 const gdb_byte *cu_table_reordered, *tu_table_reordered;
257 const uint32_t *bucket_table_reordered, *hash_table_reordered;
258 const gdb_byte *name_table_string_offs_reordered;
259 const gdb_byte *name_table_entry_offs_reordered;
260 const gdb_byte *entry_pool;
261
262 struct index_val
263 {
264 ULONGEST dwarf_tag;
265 struct attr
266 {
267 /* Attribute name DW_IDX_*. */
268 ULONGEST dw_idx;
269
270 /* Attribute form DW_FORM_*. */
271 ULONGEST form;
272
273 /* Value if FORM is DW_FORM_implicit_const. */
274 LONGEST implicit_const;
275 };
276 std::vector<attr> attr_vec;
277 };
278
279 std::unordered_map<ULONGEST, index_val> abbrev_map;
280
281 const char *namei_to_name
282 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
283
284 /* Implementation of the mapped_index_base virtual interface, for
285 the name_components cache. */
286
287 const char *symbol_name_at
288 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
289 { return namei_to_name (idx, per_objfile); }
290
291 size_t symbol_name_count () const override
292 { return this->name_count; }
293 };
294
295 /* See dwarf2read.h. */
296
297 dwarf2_per_objfile *
298 get_dwarf2_per_objfile (struct objfile *objfile)
299 {
300 return dwarf2_objfile_data_key.get (objfile);
301 }
302
303 /* Default names of the debugging sections. */
304
305 /* Note that if the debugging section has been compressed, it might
306 have a name like .zdebug_info. */
307
308 static const struct dwarf2_debug_sections dwarf2_elf_names =
309 {
310 { ".debug_info", ".zdebug_info" },
311 { ".debug_abbrev", ".zdebug_abbrev" },
312 { ".debug_line", ".zdebug_line" },
313 { ".debug_loc", ".zdebug_loc" },
314 { ".debug_loclists", ".zdebug_loclists" },
315 { ".debug_macinfo", ".zdebug_macinfo" },
316 { ".debug_macro", ".zdebug_macro" },
317 { ".debug_str", ".zdebug_str" },
318 { ".debug_str_offsets", ".zdebug_str_offsets" },
319 { ".debug_line_str", ".zdebug_line_str" },
320 { ".debug_ranges", ".zdebug_ranges" },
321 { ".debug_rnglists", ".zdebug_rnglists" },
322 { ".debug_types", ".zdebug_types" },
323 { ".debug_addr", ".zdebug_addr" },
324 { ".debug_frame", ".zdebug_frame" },
325 { ".eh_frame", NULL },
326 { ".gdb_index", ".zgdb_index" },
327 { ".debug_names", ".zdebug_names" },
328 { ".debug_aranges", ".zdebug_aranges" },
329 23
330 };
331
332 /* List of DWO/DWP sections. */
333
334 static const struct dwop_section_names
335 {
336 struct dwarf2_section_names abbrev_dwo;
337 struct dwarf2_section_names info_dwo;
338 struct dwarf2_section_names line_dwo;
339 struct dwarf2_section_names loc_dwo;
340 struct dwarf2_section_names loclists_dwo;
341 struct dwarf2_section_names macinfo_dwo;
342 struct dwarf2_section_names macro_dwo;
343 struct dwarf2_section_names str_dwo;
344 struct dwarf2_section_names str_offsets_dwo;
345 struct dwarf2_section_names types_dwo;
346 struct dwarf2_section_names cu_index;
347 struct dwarf2_section_names tu_index;
348 }
349 dwop_section_names =
350 {
351 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
352 { ".debug_info.dwo", ".zdebug_info.dwo" },
353 { ".debug_line.dwo", ".zdebug_line.dwo" },
354 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
355 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
356 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
357 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
358 { ".debug_str.dwo", ".zdebug_str.dwo" },
359 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
360 { ".debug_types.dwo", ".zdebug_types.dwo" },
361 { ".debug_cu_index", ".zdebug_cu_index" },
362 { ".debug_tu_index", ".zdebug_tu_index" },
363 };
364
365 /* local data types */
366
367 /* The location list section (.debug_loclists) begins with a header,
368 which contains the following information. */
369 struct loclist_header
370 {
371 /* A 4-byte or 12-byte length containing the length of the
372 set of entries for this compilation unit, not including the
373 length field itself. */
374 unsigned int length;
375
376 /* A 2-byte version identifier. */
377 short version;
378
379 /* A 1-byte unsigned integer containing the size in bytes of an address on
380 the target system. */
381 unsigned char addr_size;
382
383 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
384 on the target system. */
385 unsigned char segment_collector_size;
386
387 /* A 4-byte count of the number of offsets that follow the header. */
388 unsigned int offset_entry_count;
389 };
390
391 /* Type used for delaying computation of method physnames.
392 See comments for compute_delayed_physnames. */
393 struct delayed_method_info
394 {
395 /* The type to which the method is attached, i.e., its parent class. */
396 struct type *type;
397
398 /* The index of the method in the type's function fieldlists. */
399 int fnfield_index;
400
401 /* The index of the method in the fieldlist. */
402 int index;
403
404 /* The name of the DIE. */
405 const char *name;
406
407 /* The DIE associated with this method. */
408 struct die_info *die;
409 };
410
411 /* Internal state when decoding a particular compilation unit. */
412 struct dwarf2_cu
413 {
414 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
415 dwarf2_per_objfile *per_objfile);
416
417 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
418
419 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
420 Create the set of symtabs used by this TU, or if this TU is sharing
421 symtabs with another TU and the symtabs have already been created
422 then restore those symtabs in the line header.
423 We don't need the pc/line-number mapping for type units. */
424 void setup_type_unit_groups (struct die_info *die);
425
426 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
427 buildsym_compunit constructor. */
428 struct compunit_symtab *start_symtab (const char *name,
429 const char *comp_dir,
430 CORE_ADDR low_pc);
431
432 /* Reset the builder. */
433 void reset_builder () { m_builder.reset (); }
434
435 /* Return a type that is a generic pointer type, the size of which
436 matches the address size given in the compilation unit header for
437 this CU. */
438 struct type *addr_type () const;
439
440 /* Find an integer type the same size as the address size given in
441 the compilation unit header for this CU. UNSIGNED_P controls if
442 the integer is unsigned or not. */
443 struct type *addr_sized_int_type (bool unsigned_p) const;
444
445 /* The header of the compilation unit. */
446 struct comp_unit_head header {};
447
448 /* Base address of this compilation unit. */
449 gdb::optional<CORE_ADDR> base_address;
450
451 /* The language we are debugging. */
452 enum language language = language_unknown;
453 const struct language_defn *language_defn = nullptr;
454
455 const char *producer = nullptr;
456
457 private:
458 /* The symtab builder for this CU. This is only non-NULL when full
459 symbols are being read. */
460 std::unique_ptr<buildsym_compunit> m_builder;
461
462 public:
463 /* The generic symbol table building routines have separate lists for
464 file scope symbols and all all other scopes (local scopes). So
465 we need to select the right one to pass to add_symbol_to_list().
466 We do it by keeping a pointer to the correct list in list_in_scope.
467
468 FIXME: The original dwarf code just treated the file scope as the
469 first local scope, and all other local scopes as nested local
470 scopes, and worked fine. Check to see if we really need to
471 distinguish these in buildsym.c. */
472 struct pending **list_in_scope = nullptr;
473
474 /* Hash table holding all the loaded partial DIEs
475 with partial_die->offset.SECT_OFF as hash. */
476 htab_t partial_dies = nullptr;
477
478 /* Storage for things with the same lifetime as this read-in compilation
479 unit, including partial DIEs. */
480 auto_obstack comp_unit_obstack;
481
482 /* Backlink to our per_cu entry. */
483 struct dwarf2_per_cu_data *per_cu;
484
485 /* The dwarf2_per_objfile that owns this. */
486 dwarf2_per_objfile *per_objfile;
487
488 /* How many compilation units ago was this CU last referenced? */
489 int last_used = 0;
490
491 /* A hash table of DIE cu_offset for following references with
492 die_info->offset.sect_off as hash. */
493 htab_t die_hash = nullptr;
494
495 /* Full DIEs if read in. */
496 struct die_info *dies = nullptr;
497
498 /* A set of pointers to dwarf2_per_cu_data objects for compilation
499 units referenced by this one. Only set during full symbol processing;
500 partial symbol tables do not have dependencies. */
501 htab_t dependencies = nullptr;
502
503 /* Header data from the line table, during full symbol processing. */
504 struct line_header *line_header = nullptr;
505 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
506 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
507 this is the DW_TAG_compile_unit die for this CU. We'll hold on
508 to the line header as long as this DIE is being processed. See
509 process_die_scope. */
510 die_info *line_header_die_owner = nullptr;
511
512 /* A list of methods which need to have physnames computed
513 after all type information has been read. */
514 std::vector<delayed_method_info> method_list;
515
516 /* To be copied to symtab->call_site_htab. */
517 htab_t call_site_htab = nullptr;
518
519 /* Non-NULL if this CU came from a DWO file.
520 There is an invariant here that is important to remember:
521 Except for attributes copied from the top level DIE in the "main"
522 (or "stub") file in preparation for reading the DWO file
523 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
524 Either there isn't a DWO file (in which case this is NULL and the point
525 is moot), or there is and either we're not going to read it (in which
526 case this is NULL) or there is and we are reading it (in which case this
527 is non-NULL). */
528 struct dwo_unit *dwo_unit = nullptr;
529
530 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
531 Note this value comes from the Fission stub CU/TU's DIE. */
532 gdb::optional<ULONGEST> addr_base;
533
534 /* The DW_AT_rnglists_base attribute if present.
535 Note this value comes from the Fission stub CU/TU's DIE.
536 Also note that the value is zero in the non-DWO case so this value can
537 be used without needing to know whether DWO files are in use or not.
538 N.B. This does not apply to DW_AT_ranges appearing in
539 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
540 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
541 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
542 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
543 ULONGEST ranges_base = 0;
544
545 /* The DW_AT_loclists_base attribute if present. */
546 ULONGEST loclist_base = 0;
547
548 /* When reading debug info generated by older versions of rustc, we
549 have to rewrite some union types to be struct types with a
550 variant part. This rewriting must be done after the CU is fully
551 read in, because otherwise at the point of rewriting some struct
552 type might not have been fully processed. So, we keep a list of
553 all such types here and process them after expansion. */
554 std::vector<struct type *> rust_unions;
555
556 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
557 files, the value is implicitly zero. For DWARF 5 version DWO files, the
558 value is often implicit and is the size of the header of
559 .debug_str_offsets section (8 or 4, depending on the address size). */
560 gdb::optional<ULONGEST> str_offsets_base;
561
562 /* Mark used when releasing cached dies. */
563 bool mark : 1;
564
565 /* This CU references .debug_loc. See the symtab->locations_valid field.
566 This test is imperfect as there may exist optimized debug code not using
567 any location list and still facing inlining issues if handled as
568 unoptimized code. For a future better test see GCC PR other/32998. */
569 bool has_loclist : 1;
570
571 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
572 if all the producer_is_* fields are valid. This information is cached
573 because profiling CU expansion showed excessive time spent in
574 producer_is_gxx_lt_4_6. */
575 bool checked_producer : 1;
576 bool producer_is_gxx_lt_4_6 : 1;
577 bool producer_is_gcc_lt_4_3 : 1;
578 bool producer_is_icc : 1;
579 bool producer_is_icc_lt_14 : 1;
580 bool producer_is_codewarrior : 1;
581
582 /* When true, the file that we're processing is known to have
583 debugging info for C++ namespaces. GCC 3.3.x did not produce
584 this information, but later versions do. */
585
586 bool processing_has_namespace_info : 1;
587
588 struct partial_die_info *find_partial_die (sect_offset sect_off);
589
590 /* If this CU was inherited by another CU (via specification,
591 abstract_origin, etc), this is the ancestor CU. */
592 dwarf2_cu *ancestor;
593
594 /* Get the buildsym_compunit for this CU. */
595 buildsym_compunit *get_builder ()
596 {
597 /* If this CU has a builder associated with it, use that. */
598 if (m_builder != nullptr)
599 return m_builder.get ();
600
601 /* Otherwise, search ancestors for a valid builder. */
602 if (ancestor != nullptr)
603 return ancestor->get_builder ();
604
605 return nullptr;
606 }
607 };
608
609 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
610 This includes type_unit_group and quick_file_names. */
611
612 struct stmt_list_hash
613 {
614 /* The DWO unit this table is from or NULL if there is none. */
615 struct dwo_unit *dwo_unit;
616
617 /* Offset in .debug_line or .debug_line.dwo. */
618 sect_offset line_sect_off;
619 };
620
621 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
622 an object of this type. This contains elements of type unit groups
623 that can be shared across objfiles. The non-shareable parts are in
624 type_unit_group_unshareable. */
625
626 struct type_unit_group
627 {
628 /* dwarf2read.c's main "handle" on a TU symtab.
629 To simplify things we create an artificial CU that "includes" all the
630 type units using this stmt_list so that the rest of the code still has
631 a "per_cu" handle on the symtab. */
632 struct dwarf2_per_cu_data per_cu;
633
634 /* The TUs that share this DW_AT_stmt_list entry.
635 This is added to while parsing type units to build partial symtabs,
636 and is deleted afterwards and not used again. */
637 std::vector<signatured_type *> *tus;
638
639 /* The data used to construct the hash key. */
640 struct stmt_list_hash hash;
641 };
642
643 /* These sections are what may appear in a (real or virtual) DWO file. */
644
645 struct dwo_sections
646 {
647 struct dwarf2_section_info abbrev;
648 struct dwarf2_section_info line;
649 struct dwarf2_section_info loc;
650 struct dwarf2_section_info loclists;
651 struct dwarf2_section_info macinfo;
652 struct dwarf2_section_info macro;
653 struct dwarf2_section_info str;
654 struct dwarf2_section_info str_offsets;
655 /* In the case of a virtual DWO file, these two are unused. */
656 struct dwarf2_section_info info;
657 std::vector<dwarf2_section_info> types;
658 };
659
660 /* CUs/TUs in DWP/DWO files. */
661
662 struct dwo_unit
663 {
664 /* Backlink to the containing struct dwo_file. */
665 struct dwo_file *dwo_file;
666
667 /* The "id" that distinguishes this CU/TU.
668 .debug_info calls this "dwo_id", .debug_types calls this "signature".
669 Since signatures came first, we stick with it for consistency. */
670 ULONGEST signature;
671
672 /* The section this CU/TU lives in, in the DWO file. */
673 struct dwarf2_section_info *section;
674
675 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
676 sect_offset sect_off;
677 unsigned int length;
678
679 /* For types, offset in the type's DIE of the type defined by this TU. */
680 cu_offset type_offset_in_tu;
681 };
682
683 /* include/dwarf2.h defines the DWP section codes.
684 It defines a max value but it doesn't define a min value, which we
685 use for error checking, so provide one. */
686
687 enum dwp_v2_section_ids
688 {
689 DW_SECT_MIN = 1
690 };
691
692 /* Data for one DWO file.
693
694 This includes virtual DWO files (a virtual DWO file is a DWO file as it
695 appears in a DWP file). DWP files don't really have DWO files per se -
696 comdat folding of types "loses" the DWO file they came from, and from
697 a high level view DWP files appear to contain a mass of random types.
698 However, to maintain consistency with the non-DWP case we pretend DWP
699 files contain virtual DWO files, and we assign each TU with one virtual
700 DWO file (generally based on the line and abbrev section offsets -
701 a heuristic that seems to work in practice). */
702
703 struct dwo_file
704 {
705 dwo_file () = default;
706 DISABLE_COPY_AND_ASSIGN (dwo_file);
707
708 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
709 For virtual DWO files the name is constructed from the section offsets
710 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
711 from related CU+TUs. */
712 const char *dwo_name = nullptr;
713
714 /* The DW_AT_comp_dir attribute. */
715 const char *comp_dir = nullptr;
716
717 /* The bfd, when the file is open. Otherwise this is NULL.
718 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
719 gdb_bfd_ref_ptr dbfd;
720
721 /* The sections that make up this DWO file.
722 Remember that for virtual DWO files in DWP V2, these are virtual
723 sections (for lack of a better name). */
724 struct dwo_sections sections {};
725
726 /* The CUs in the file.
727 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
728 an extension to handle LLVM's Link Time Optimization output (where
729 multiple source files may be compiled into a single object/dwo pair). */
730 htab_up cus;
731
732 /* Table of TUs in the file.
733 Each element is a struct dwo_unit. */
734 htab_up tus;
735 };
736
737 /* These sections are what may appear in a DWP file. */
738
739 struct dwp_sections
740 {
741 /* These are used by both DWP version 1 and 2. */
742 struct dwarf2_section_info str;
743 struct dwarf2_section_info cu_index;
744 struct dwarf2_section_info tu_index;
745
746 /* These are only used by DWP version 2 files.
747 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
748 sections are referenced by section number, and are not recorded here.
749 In DWP version 2 there is at most one copy of all these sections, each
750 section being (effectively) comprised of the concatenation of all of the
751 individual sections that exist in the version 1 format.
752 To keep the code simple we treat each of these concatenated pieces as a
753 section itself (a virtual section?). */
754 struct dwarf2_section_info abbrev;
755 struct dwarf2_section_info info;
756 struct dwarf2_section_info line;
757 struct dwarf2_section_info loc;
758 struct dwarf2_section_info macinfo;
759 struct dwarf2_section_info macro;
760 struct dwarf2_section_info str_offsets;
761 struct dwarf2_section_info types;
762 };
763
764 /* These sections are what may appear in a virtual DWO file in DWP version 1.
765 A virtual DWO file is a DWO file as it appears in a DWP file. */
766
767 struct virtual_v1_dwo_sections
768 {
769 struct dwarf2_section_info abbrev;
770 struct dwarf2_section_info line;
771 struct dwarf2_section_info loc;
772 struct dwarf2_section_info macinfo;
773 struct dwarf2_section_info macro;
774 struct dwarf2_section_info str_offsets;
775 /* Each DWP hash table entry records one CU or one TU.
776 That is recorded here, and copied to dwo_unit.section. */
777 struct dwarf2_section_info info_or_types;
778 };
779
780 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
781 In version 2, the sections of the DWO files are concatenated together
782 and stored in one section of that name. Thus each ELF section contains
783 several "virtual" sections. */
784
785 struct virtual_v2_dwo_sections
786 {
787 bfd_size_type abbrev_offset;
788 bfd_size_type abbrev_size;
789
790 bfd_size_type line_offset;
791 bfd_size_type line_size;
792
793 bfd_size_type loc_offset;
794 bfd_size_type loc_size;
795
796 bfd_size_type macinfo_offset;
797 bfd_size_type macinfo_size;
798
799 bfd_size_type macro_offset;
800 bfd_size_type macro_size;
801
802 bfd_size_type str_offsets_offset;
803 bfd_size_type str_offsets_size;
804
805 /* Each DWP hash table entry records one CU or one TU.
806 That is recorded here, and copied to dwo_unit.section. */
807 bfd_size_type info_or_types_offset;
808 bfd_size_type info_or_types_size;
809 };
810
811 /* Contents of DWP hash tables. */
812
813 struct dwp_hash_table
814 {
815 uint32_t version, nr_columns;
816 uint32_t nr_units, nr_slots;
817 const gdb_byte *hash_table, *unit_table;
818 union
819 {
820 struct
821 {
822 const gdb_byte *indices;
823 } v1;
824 struct
825 {
826 /* This is indexed by column number and gives the id of the section
827 in that column. */
828 #define MAX_NR_V2_DWO_SECTIONS \
829 (1 /* .debug_info or .debug_types */ \
830 + 1 /* .debug_abbrev */ \
831 + 1 /* .debug_line */ \
832 + 1 /* .debug_loc */ \
833 + 1 /* .debug_str_offsets */ \
834 + 1 /* .debug_macro or .debug_macinfo */)
835 int section_ids[MAX_NR_V2_DWO_SECTIONS];
836 const gdb_byte *offsets;
837 const gdb_byte *sizes;
838 } v2;
839 } section_pool;
840 };
841
842 /* Data for one DWP file. */
843
844 struct dwp_file
845 {
846 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
847 : name (name_),
848 dbfd (std::move (abfd))
849 {
850 }
851
852 /* Name of the file. */
853 const char *name;
854
855 /* File format version. */
856 int version = 0;
857
858 /* The bfd. */
859 gdb_bfd_ref_ptr dbfd;
860
861 /* Section info for this file. */
862 struct dwp_sections sections {};
863
864 /* Table of CUs in the file. */
865 const struct dwp_hash_table *cus = nullptr;
866
867 /* Table of TUs in the file. */
868 const struct dwp_hash_table *tus = nullptr;
869
870 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
871 htab_up loaded_cus;
872 htab_up loaded_tus;
873
874 /* Table to map ELF section numbers to their sections.
875 This is only needed for the DWP V1 file format. */
876 unsigned int num_sections = 0;
877 asection **elf_sections = nullptr;
878 };
879
880 /* Struct used to pass misc. parameters to read_die_and_children, et
881 al. which are used for both .debug_info and .debug_types dies.
882 All parameters here are unchanging for the life of the call. This
883 struct exists to abstract away the constant parameters of die reading. */
884
885 struct die_reader_specs
886 {
887 /* The bfd of die_section. */
888 bfd* abfd;
889
890 /* The CU of the DIE we are parsing. */
891 struct dwarf2_cu *cu;
892
893 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
894 struct dwo_file *dwo_file;
895
896 /* The section the die comes from.
897 This is either .debug_info or .debug_types, or the .dwo variants. */
898 struct dwarf2_section_info *die_section;
899
900 /* die_section->buffer. */
901 const gdb_byte *buffer;
902
903 /* The end of the buffer. */
904 const gdb_byte *buffer_end;
905
906 /* The abbreviation table to use when reading the DIEs. */
907 struct abbrev_table *abbrev_table;
908 };
909
910 /* A subclass of die_reader_specs that holds storage and has complex
911 constructor and destructor behavior. */
912
913 class cutu_reader : public die_reader_specs
914 {
915 public:
916
917 cutu_reader (dwarf2_per_cu_data *this_cu,
918 dwarf2_per_objfile *per_objfile,
919 struct abbrev_table *abbrev_table,
920 dwarf2_cu *existing_cu,
921 bool skip_partial);
922
923 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
924 dwarf2_per_objfile *per_objfile,
925 struct dwarf2_cu *parent_cu = nullptr,
926 struct dwo_file *dwo_file = nullptr);
927
928 DISABLE_COPY_AND_ASSIGN (cutu_reader);
929
930 const gdb_byte *info_ptr = nullptr;
931 struct die_info *comp_unit_die = nullptr;
932 bool dummy_p = false;
933
934 /* Release the new CU, putting it on the chain. This cannot be done
935 for dummy CUs. */
936 void keep ();
937
938 private:
939 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
940 dwarf2_per_objfile *per_objfile,
941 dwarf2_cu *existing_cu);
942
943 struct dwarf2_per_cu_data *m_this_cu;
944 std::unique_ptr<dwarf2_cu> m_new_cu;
945
946 /* The ordinary abbreviation table. */
947 abbrev_table_up m_abbrev_table_holder;
948
949 /* The DWO abbreviation table. */
950 abbrev_table_up m_dwo_abbrev_table;
951 };
952
953 /* When we construct a partial symbol table entry we only
954 need this much information. */
955 struct partial_die_info : public allocate_on_obstack
956 {
957 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
958
959 /* Disable assign but still keep copy ctor, which is needed
960 load_partial_dies. */
961 partial_die_info& operator=(const partial_die_info& rhs) = delete;
962
963 /* Adjust the partial die before generating a symbol for it. This
964 function may set the is_external flag or change the DIE's
965 name. */
966 void fixup (struct dwarf2_cu *cu);
967
968 /* Read a minimal amount of information into the minimal die
969 structure. */
970 const gdb_byte *read (const struct die_reader_specs *reader,
971 const struct abbrev_info &abbrev,
972 const gdb_byte *info_ptr);
973
974 /* Compute the name of this partial DIE. This memoizes the
975 result, so it is safe to call multiple times. */
976 const char *name (dwarf2_cu *cu);
977
978 /* Offset of this DIE. */
979 const sect_offset sect_off;
980
981 /* DWARF-2 tag for this DIE. */
982 const ENUM_BITFIELD(dwarf_tag) tag : 16;
983
984 /* Assorted flags describing the data found in this DIE. */
985 const unsigned int has_children : 1;
986
987 unsigned int is_external : 1;
988 unsigned int is_declaration : 1;
989 unsigned int has_type : 1;
990 unsigned int has_specification : 1;
991 unsigned int has_pc_info : 1;
992 unsigned int may_be_inlined : 1;
993
994 /* This DIE has been marked DW_AT_main_subprogram. */
995 unsigned int main_subprogram : 1;
996
997 /* Flag set if the SCOPE field of this structure has been
998 computed. */
999 unsigned int scope_set : 1;
1000
1001 /* Flag set if the DIE has a byte_size attribute. */
1002 unsigned int has_byte_size : 1;
1003
1004 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1005 unsigned int has_const_value : 1;
1006
1007 /* Flag set if any of the DIE's children are template arguments. */
1008 unsigned int has_template_arguments : 1;
1009
1010 /* Flag set if fixup has been called on this die. */
1011 unsigned int fixup_called : 1;
1012
1013 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1014 unsigned int is_dwz : 1;
1015
1016 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1017 unsigned int spec_is_dwz : 1;
1018
1019 unsigned int canonical_name : 1;
1020
1021 /* The name of this DIE. Normally the value of DW_AT_name, but
1022 sometimes a default name for unnamed DIEs. */
1023 const char *raw_name = nullptr;
1024
1025 /* The linkage name, if present. */
1026 const char *linkage_name = nullptr;
1027
1028 /* The scope to prepend to our children. This is generally
1029 allocated on the comp_unit_obstack, so will disappear
1030 when this compilation unit leaves the cache. */
1031 const char *scope = nullptr;
1032
1033 /* Some data associated with the partial DIE. The tag determines
1034 which field is live. */
1035 union
1036 {
1037 /* The location description associated with this DIE, if any. */
1038 struct dwarf_block *locdesc;
1039 /* The offset of an import, for DW_TAG_imported_unit. */
1040 sect_offset sect_off;
1041 } d {};
1042
1043 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1044 CORE_ADDR lowpc = 0;
1045 CORE_ADDR highpc = 0;
1046
1047 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1048 DW_AT_sibling, if any. */
1049 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1050 could return DW_AT_sibling values to its caller load_partial_dies. */
1051 const gdb_byte *sibling = nullptr;
1052
1053 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1054 DW_AT_specification (or DW_AT_abstract_origin or
1055 DW_AT_extension). */
1056 sect_offset spec_offset {};
1057
1058 /* Pointers to this DIE's parent, first child, and next sibling,
1059 if any. */
1060 struct partial_die_info *die_parent = nullptr;
1061 struct partial_die_info *die_child = nullptr;
1062 struct partial_die_info *die_sibling = nullptr;
1063
1064 friend struct partial_die_info *
1065 dwarf2_cu::find_partial_die (sect_offset sect_off);
1066
1067 private:
1068 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1069 partial_die_info (sect_offset sect_off)
1070 : partial_die_info (sect_off, DW_TAG_padding, 0)
1071 {
1072 }
1073
1074 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1075 int has_children_)
1076 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1077 {
1078 is_external = 0;
1079 is_declaration = 0;
1080 has_type = 0;
1081 has_specification = 0;
1082 has_pc_info = 0;
1083 may_be_inlined = 0;
1084 main_subprogram = 0;
1085 scope_set = 0;
1086 has_byte_size = 0;
1087 has_const_value = 0;
1088 has_template_arguments = 0;
1089 fixup_called = 0;
1090 is_dwz = 0;
1091 spec_is_dwz = 0;
1092 canonical_name = 0;
1093 }
1094 };
1095
1096 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1097 but this would require a corresponding change in unpack_field_as_long
1098 and friends. */
1099 static int bits_per_byte = 8;
1100
1101 struct variant_part_builder;
1102
1103 /* When reading a variant, we track a bit more information about the
1104 field, and store it in an object of this type. */
1105
1106 struct variant_field
1107 {
1108 int first_field = -1;
1109 int last_field = -1;
1110
1111 /* A variant can contain other variant parts. */
1112 std::vector<variant_part_builder> variant_parts;
1113
1114 /* If we see a DW_TAG_variant, then this will be set if this is the
1115 default branch. */
1116 bool default_branch = false;
1117 /* If we see a DW_AT_discr_value, then this will be the discriminant
1118 value. */
1119 ULONGEST discriminant_value = 0;
1120 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1121 data. */
1122 struct dwarf_block *discr_list_data = nullptr;
1123 };
1124
1125 /* This represents a DW_TAG_variant_part. */
1126
1127 struct variant_part_builder
1128 {
1129 /* The offset of the discriminant field. */
1130 sect_offset discriminant_offset {};
1131
1132 /* Variants that are direct children of this variant part. */
1133 std::vector<variant_field> variants;
1134
1135 /* True if we're currently reading a variant. */
1136 bool processing_variant = false;
1137 };
1138
1139 struct nextfield
1140 {
1141 int accessibility = 0;
1142 int virtuality = 0;
1143 /* Variant parts need to find the discriminant, which is a DIE
1144 reference. We track the section offset of each field to make
1145 this link. */
1146 sect_offset offset;
1147 struct field field {};
1148 };
1149
1150 struct fnfieldlist
1151 {
1152 const char *name = nullptr;
1153 std::vector<struct fn_field> fnfields;
1154 };
1155
1156 /* The routines that read and process dies for a C struct or C++ class
1157 pass lists of data member fields and lists of member function fields
1158 in an instance of a field_info structure, as defined below. */
1159 struct field_info
1160 {
1161 /* List of data member and baseclasses fields. */
1162 std::vector<struct nextfield> fields;
1163 std::vector<struct nextfield> baseclasses;
1164
1165 /* Set if the accessibility of one of the fields is not public. */
1166 int non_public_fields = 0;
1167
1168 /* Member function fieldlist array, contains name of possibly overloaded
1169 member function, number of overloaded member functions and a pointer
1170 to the head of the member function field chain. */
1171 std::vector<struct fnfieldlist> fnfieldlists;
1172
1173 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1174 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1175 std::vector<struct decl_field> typedef_field_list;
1176
1177 /* Nested types defined by this class and the number of elements in this
1178 list. */
1179 std::vector<struct decl_field> nested_types_list;
1180
1181 /* If non-null, this is the variant part we are currently
1182 reading. */
1183 variant_part_builder *current_variant_part = nullptr;
1184 /* This holds all the top-level variant parts attached to the type
1185 we're reading. */
1186 std::vector<variant_part_builder> variant_parts;
1187
1188 /* Return the total number of fields (including baseclasses). */
1189 int nfields () const
1190 {
1191 return fields.size () + baseclasses.size ();
1192 }
1193 };
1194
1195 /* Loaded secondary compilation units are kept in memory until they
1196 have not been referenced for the processing of this many
1197 compilation units. Set this to zero to disable caching. Cache
1198 sizes of up to at least twenty will improve startup time for
1199 typical inter-CU-reference binaries, at an obvious memory cost. */
1200 static int dwarf_max_cache_age = 5;
1201 static void
1202 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1203 struct cmd_list_element *c, const char *value)
1204 {
1205 fprintf_filtered (file, _("The upper bound on the age of cached "
1206 "DWARF compilation units is %s.\n"),
1207 value);
1208 }
1209 \f
1210 /* local function prototypes */
1211
1212 static void dwarf2_find_base_address (struct die_info *die,
1213 struct dwarf2_cu *cu);
1214
1215 static dwarf2_psymtab *create_partial_symtab
1216 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1217 const char *name);
1218
1219 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1220 const gdb_byte *info_ptr,
1221 struct die_info *type_unit_die);
1222
1223 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1224
1225 static void scan_partial_symbols (struct partial_die_info *,
1226 CORE_ADDR *, CORE_ADDR *,
1227 int, struct dwarf2_cu *);
1228
1229 static void add_partial_symbol (struct partial_die_info *,
1230 struct dwarf2_cu *);
1231
1232 static void add_partial_namespace (struct partial_die_info *pdi,
1233 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1234 int set_addrmap, struct dwarf2_cu *cu);
1235
1236 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1237 CORE_ADDR *highpc, int set_addrmap,
1238 struct dwarf2_cu *cu);
1239
1240 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1241 struct dwarf2_cu *cu);
1242
1243 static void add_partial_subprogram (struct partial_die_info *pdi,
1244 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1245 int need_pc, struct dwarf2_cu *cu);
1246
1247 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1248
1249 static struct partial_die_info *load_partial_dies
1250 (const struct die_reader_specs *, const gdb_byte *, int);
1251
1252 /* A pair of partial_die_info and compilation unit. */
1253 struct cu_partial_die_info
1254 {
1255 /* The compilation unit of the partial_die_info. */
1256 struct dwarf2_cu *cu;
1257 /* A partial_die_info. */
1258 struct partial_die_info *pdi;
1259
1260 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1261 : cu (cu),
1262 pdi (pdi)
1263 { /* Nothing. */ }
1264
1265 private:
1266 cu_partial_die_info () = delete;
1267 };
1268
1269 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1270 struct dwarf2_cu *);
1271
1272 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1273 struct attribute *, struct attr_abbrev *,
1274 const gdb_byte *, bool *need_reprocess);
1275
1276 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1277 struct attribute *attr);
1278
1279 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1280
1281 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1282 dwarf2_section_info *, sect_offset);
1283
1284 static const char *read_indirect_string
1285 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1286 const struct comp_unit_head *, unsigned int *);
1287
1288 static const char *read_indirect_string_at_offset
1289 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1290
1291 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1292 const gdb_byte *,
1293 unsigned int *);
1294
1295 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1296 ULONGEST str_index);
1297
1298 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1299 ULONGEST str_index);
1300
1301 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1302
1303 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1304 struct dwarf2_cu *);
1305
1306 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1307 struct dwarf2_cu *cu);
1308
1309 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1310
1311 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1312 struct dwarf2_cu *cu);
1313
1314 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1315
1316 static struct die_info *die_specification (struct die_info *die,
1317 struct dwarf2_cu **);
1318
1319 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1320 struct dwarf2_cu *cu);
1321
1322 static void dwarf_decode_lines (struct line_header *, const char *,
1323 struct dwarf2_cu *, dwarf2_psymtab *,
1324 CORE_ADDR, int decode_mapping);
1325
1326 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1327 const char *);
1328
1329 static struct symbol *new_symbol (struct die_info *, struct type *,
1330 struct dwarf2_cu *, struct symbol * = NULL);
1331
1332 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1333 struct dwarf2_cu *);
1334
1335 static void dwarf2_const_value_attr (const struct attribute *attr,
1336 struct type *type,
1337 const char *name,
1338 struct obstack *obstack,
1339 struct dwarf2_cu *cu, LONGEST *value,
1340 const gdb_byte **bytes,
1341 struct dwarf2_locexpr_baton **baton);
1342
1343 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1344
1345 static int need_gnat_info (struct dwarf2_cu *);
1346
1347 static struct type *die_descriptive_type (struct die_info *,
1348 struct dwarf2_cu *);
1349
1350 static void set_descriptive_type (struct type *, struct die_info *,
1351 struct dwarf2_cu *);
1352
1353 static struct type *die_containing_type (struct die_info *,
1354 struct dwarf2_cu *);
1355
1356 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1357 struct dwarf2_cu *);
1358
1359 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1360
1361 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1362
1363 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1364
1365 static char *typename_concat (struct obstack *obs, const char *prefix,
1366 const char *suffix, int physname,
1367 struct dwarf2_cu *cu);
1368
1369 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1370
1371 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1372
1373 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1374
1375 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1376
1377 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1378
1379 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1380
1381 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1382 struct dwarf2_cu *, dwarf2_psymtab *);
1383
1384 /* Return the .debug_loclists section to use for cu. */
1385 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1386
1387 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1388 values. Keep the items ordered with increasing constraints compliance. */
1389 enum pc_bounds_kind
1390 {
1391 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1392 PC_BOUNDS_NOT_PRESENT,
1393
1394 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1395 were present but they do not form a valid range of PC addresses. */
1396 PC_BOUNDS_INVALID,
1397
1398 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1399 PC_BOUNDS_RANGES,
1400
1401 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1402 PC_BOUNDS_HIGH_LOW,
1403 };
1404
1405 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1406 CORE_ADDR *, CORE_ADDR *,
1407 struct dwarf2_cu *,
1408 dwarf2_psymtab *);
1409
1410 static void get_scope_pc_bounds (struct die_info *,
1411 CORE_ADDR *, CORE_ADDR *,
1412 struct dwarf2_cu *);
1413
1414 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1415 CORE_ADDR, struct dwarf2_cu *);
1416
1417 static void dwarf2_add_field (struct field_info *, struct die_info *,
1418 struct dwarf2_cu *);
1419
1420 static void dwarf2_attach_fields_to_type (struct field_info *,
1421 struct type *, struct dwarf2_cu *);
1422
1423 static void dwarf2_add_member_fn (struct field_info *,
1424 struct die_info *, struct type *,
1425 struct dwarf2_cu *);
1426
1427 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1428 struct type *,
1429 struct dwarf2_cu *);
1430
1431 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1432
1433 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1434
1435 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1436
1437 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1438
1439 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1440
1441 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1442
1443 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1444
1445 static struct type *read_module_type (struct die_info *die,
1446 struct dwarf2_cu *cu);
1447
1448 static const char *namespace_name (struct die_info *die,
1449 int *is_anonymous, struct dwarf2_cu *);
1450
1451 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1452
1453 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1454 bool * = nullptr);
1455
1456 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1457 struct dwarf2_cu *);
1458
1459 static struct die_info *read_die_and_siblings_1
1460 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1461 struct die_info *);
1462
1463 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1464 const gdb_byte *info_ptr,
1465 const gdb_byte **new_info_ptr,
1466 struct die_info *parent);
1467
1468 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1469 struct die_info **, const gdb_byte *,
1470 int);
1471
1472 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1473 struct die_info **, const gdb_byte *);
1474
1475 static void process_die (struct die_info *, struct dwarf2_cu *);
1476
1477 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1478 struct objfile *);
1479
1480 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1481
1482 static const char *dwarf2_full_name (const char *name,
1483 struct die_info *die,
1484 struct dwarf2_cu *cu);
1485
1486 static const char *dwarf2_physname (const char *name, struct die_info *die,
1487 struct dwarf2_cu *cu);
1488
1489 static struct die_info *dwarf2_extension (struct die_info *die,
1490 struct dwarf2_cu **);
1491
1492 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1493
1494 static void dump_die_for_error (struct die_info *);
1495
1496 static void dump_die_1 (struct ui_file *, int level, int max_level,
1497 struct die_info *);
1498
1499 /*static*/ void dump_die (struct die_info *, int max_level);
1500
1501 static void store_in_ref_table (struct die_info *,
1502 struct dwarf2_cu *);
1503
1504 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1505 const struct attribute *,
1506 struct dwarf2_cu **);
1507
1508 static struct die_info *follow_die_ref (struct die_info *,
1509 const struct attribute *,
1510 struct dwarf2_cu **);
1511
1512 static struct die_info *follow_die_sig (struct die_info *,
1513 const struct attribute *,
1514 struct dwarf2_cu **);
1515
1516 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1517 struct dwarf2_cu *);
1518
1519 static struct type *get_DW_AT_signature_type (struct die_info *,
1520 const struct attribute *,
1521 struct dwarf2_cu *);
1522
1523 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1524 dwarf2_per_objfile *per_objfile);
1525
1526 static void read_signatured_type (signatured_type *sig_type,
1527 dwarf2_per_objfile *per_objfile);
1528
1529 static int attr_to_dynamic_prop (const struct attribute *attr,
1530 struct die_info *die, struct dwarf2_cu *cu,
1531 struct dynamic_prop *prop, struct type *type);
1532
1533 /* memory allocation interface */
1534
1535 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1536
1537 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1538
1539 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1540
1541 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1542 struct dwarf2_loclist_baton *baton,
1543 const struct attribute *attr);
1544
1545 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1546 struct symbol *sym,
1547 struct dwarf2_cu *cu,
1548 int is_block);
1549
1550 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1551 const gdb_byte *info_ptr,
1552 struct abbrev_info *abbrev);
1553
1554 static hashval_t partial_die_hash (const void *item);
1555
1556 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1557
1558 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1559 (sect_offset sect_off, unsigned int offset_in_dwz,
1560 dwarf2_per_objfile *per_objfile);
1561
1562 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1563 struct die_info *comp_unit_die,
1564 enum language pretend_language);
1565
1566 static struct type *set_die_type (struct die_info *, struct type *,
1567 struct dwarf2_cu *);
1568
1569 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1570
1571 static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1572
1573 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1574 dwarf2_per_objfile *per_objfile,
1575 bool skip_partial,
1576 enum language pretend_language);
1577
1578 static void process_full_comp_unit (dwarf2_cu *cu,
1579 enum language pretend_language);
1580
1581 static void process_full_type_unit (dwarf2_cu *cu,
1582 enum language pretend_language);
1583
1584 static void dwarf2_add_dependence (struct dwarf2_cu *,
1585 struct dwarf2_per_cu_data *);
1586
1587 static void dwarf2_mark (struct dwarf2_cu *);
1588
1589 static struct type *get_die_type_at_offset (sect_offset,
1590 dwarf2_per_cu_data *per_cu,
1591 dwarf2_per_objfile *per_objfile);
1592
1593 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1594
1595 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1596 dwarf2_per_objfile *per_objfile,
1597 enum language pretend_language);
1598
1599 static void process_queue (dwarf2_per_objfile *per_objfile);
1600
1601 /* Class, the destructor of which frees all allocated queue entries. This
1602 will only have work to do if an error was thrown while processing the
1603 dwarf. If no error was thrown then the queue entries should have all
1604 been processed, and freed, as we went along. */
1605
1606 class dwarf2_queue_guard
1607 {
1608 public:
1609 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1610 : m_per_objfile (per_objfile)
1611 {
1612 }
1613
1614 /* Free any entries remaining on the queue. There should only be
1615 entries left if we hit an error while processing the dwarf. */
1616 ~dwarf2_queue_guard ()
1617 {
1618 /* Ensure that no memory is allocated by the queue. */
1619 std::queue<dwarf2_queue_item> empty;
1620 std::swap (m_per_objfile->per_bfd->queue, empty);
1621 }
1622
1623 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1624
1625 private:
1626 dwarf2_per_objfile *m_per_objfile;
1627 };
1628
1629 dwarf2_queue_item::~dwarf2_queue_item ()
1630 {
1631 /* Anything still marked queued is likely to be in an
1632 inconsistent state, so discard it. */
1633 if (per_cu->queued)
1634 {
1635 per_objfile->remove_cu (per_cu);
1636 per_cu->queued = 0;
1637 }
1638 }
1639
1640 /* The return type of find_file_and_directory. Note, the enclosed
1641 string pointers are only valid while this object is valid. */
1642
1643 struct file_and_directory
1644 {
1645 /* The filename. This is never NULL. */
1646 const char *name;
1647
1648 /* The compilation directory. NULL if not known. If we needed to
1649 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1650 points directly to the DW_AT_comp_dir string attribute owned by
1651 the obstack that owns the DIE. */
1652 const char *comp_dir;
1653
1654 /* If we needed to build a new string for comp_dir, this is what
1655 owns the storage. */
1656 std::string comp_dir_storage;
1657 };
1658
1659 static file_and_directory find_file_and_directory (struct die_info *die,
1660 struct dwarf2_cu *cu);
1661
1662 static htab_up allocate_signatured_type_table ();
1663
1664 static htab_up allocate_dwo_unit_table ();
1665
1666 static struct dwo_unit *lookup_dwo_unit_in_dwp
1667 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1668 const char *comp_dir, ULONGEST signature, int is_debug_types);
1669
1670 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1671
1672 static struct dwo_unit *lookup_dwo_comp_unit
1673 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1674 ULONGEST signature);
1675
1676 static struct dwo_unit *lookup_dwo_type_unit
1677 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1678
1679 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1680
1681 /* A unique pointer to a dwo_file. */
1682
1683 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1684
1685 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1686
1687 static void check_producer (struct dwarf2_cu *cu);
1688
1689 static void free_line_header_voidp (void *arg);
1690 \f
1691 /* Various complaints about symbol reading that don't abort the process. */
1692
1693 static void
1694 dwarf2_debug_line_missing_file_complaint (void)
1695 {
1696 complaint (_(".debug_line section has line data without a file"));
1697 }
1698
1699 static void
1700 dwarf2_debug_line_missing_end_sequence_complaint (void)
1701 {
1702 complaint (_(".debug_line section has line "
1703 "program sequence without an end"));
1704 }
1705
1706 static void
1707 dwarf2_complex_location_expr_complaint (void)
1708 {
1709 complaint (_("location expression too complex"));
1710 }
1711
1712 static void
1713 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1714 int arg3)
1715 {
1716 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1717 arg1, arg2, arg3);
1718 }
1719
1720 static void
1721 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1722 {
1723 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1724 arg1, arg2);
1725 }
1726
1727 /* Hash function for line_header_hash. */
1728
1729 static hashval_t
1730 line_header_hash (const struct line_header *ofs)
1731 {
1732 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1733 }
1734
1735 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1736
1737 static hashval_t
1738 line_header_hash_voidp (const void *item)
1739 {
1740 const struct line_header *ofs = (const struct line_header *) item;
1741
1742 return line_header_hash (ofs);
1743 }
1744
1745 /* Equality function for line_header_hash. */
1746
1747 static int
1748 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1749 {
1750 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1751 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1752
1753 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1754 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1755 }
1756
1757 \f
1758
1759 /* See declaration. */
1760
1761 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1762 bool can_copy_)
1763 : obfd (obfd),
1764 can_copy (can_copy_)
1765 {
1766 if (names == NULL)
1767 names = &dwarf2_elf_names;
1768
1769 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1770 locate_sections (obfd, sec, *names);
1771 }
1772
1773 dwarf2_per_bfd::~dwarf2_per_bfd ()
1774 {
1775 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1776 per_cu->imported_symtabs_free ();
1777
1778 for (signatured_type *sig_type : all_type_units)
1779 sig_type->per_cu.imported_symtabs_free ();
1780
1781 /* Everything else should be on this->obstack. */
1782 }
1783
1784 /* See read.h. */
1785
1786 void
1787 dwarf2_per_objfile::remove_all_cus ()
1788 {
1789 for (auto pair : m_dwarf2_cus)
1790 delete pair.second;
1791
1792 m_dwarf2_cus.clear ();
1793 }
1794
1795 /* A helper class that calls free_cached_comp_units on
1796 destruction. */
1797
1798 class free_cached_comp_units
1799 {
1800 public:
1801
1802 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1803 : m_per_objfile (per_objfile)
1804 {
1805 }
1806
1807 ~free_cached_comp_units ()
1808 {
1809 m_per_objfile->remove_all_cus ();
1810 }
1811
1812 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1813
1814 private:
1815
1816 dwarf2_per_objfile *m_per_objfile;
1817 };
1818
1819 /* See read.h. */
1820
1821 bool
1822 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1823 {
1824 gdb_assert (per_cu->index < this->m_symtabs.size ());
1825
1826 return this->m_symtabs[per_cu->index] != nullptr;
1827 }
1828
1829 /* See read.h. */
1830
1831 compunit_symtab *
1832 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1833 {
1834 gdb_assert (per_cu->index < this->m_symtabs.size ());
1835
1836 return this->m_symtabs[per_cu->index];
1837 }
1838
1839 /* See read.h. */
1840
1841 void
1842 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1843 compunit_symtab *symtab)
1844 {
1845 gdb_assert (per_cu->index < this->m_symtabs.size ());
1846 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1847
1848 this->m_symtabs[per_cu->index] = symtab;
1849 }
1850
1851 /* Try to locate the sections we need for DWARF 2 debugging
1852 information and return true if we have enough to do something.
1853 NAMES points to the dwarf2 section names, or is NULL if the standard
1854 ELF names are used. CAN_COPY is true for formats where symbol
1855 interposition is possible and so symbol values must follow copy
1856 relocation rules. */
1857
1858 int
1859 dwarf2_has_info (struct objfile *objfile,
1860 const struct dwarf2_debug_sections *names,
1861 bool can_copy)
1862 {
1863 if (objfile->flags & OBJF_READNEVER)
1864 return 0;
1865
1866 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1867
1868 if (per_objfile == NULL)
1869 {
1870 dwarf2_per_bfd *per_bfd;
1871
1872 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
1873 doesn't require relocations and if there aren't partial symbols
1874 from some other reader. */
1875 if (!objfile_has_partial_symbols (objfile)
1876 && !gdb_bfd_requires_relocations (objfile->obfd))
1877 {
1878 /* See if one has been created for this BFD yet. */
1879 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1880
1881 if (per_bfd == nullptr)
1882 {
1883 /* No, create it now. */
1884 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1885 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1886 }
1887 }
1888 else
1889 {
1890 /* No sharing possible, create one specifically for this objfile. */
1891 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1892 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1893 }
1894
1895 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1896 }
1897
1898 return (!per_objfile->per_bfd->info.is_virtual
1899 && per_objfile->per_bfd->info.s.section != NULL
1900 && !per_objfile->per_bfd->abbrev.is_virtual
1901 && per_objfile->per_bfd->abbrev.s.section != NULL);
1902 }
1903
1904 /* When loading sections, we look either for uncompressed section or for
1905 compressed section names. */
1906
1907 static int
1908 section_is_p (const char *section_name,
1909 const struct dwarf2_section_names *names)
1910 {
1911 if (names->normal != NULL
1912 && strcmp (section_name, names->normal) == 0)
1913 return 1;
1914 if (names->compressed != NULL
1915 && strcmp (section_name, names->compressed) == 0)
1916 return 1;
1917 return 0;
1918 }
1919
1920 /* See declaration. */
1921
1922 void
1923 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1924 const dwarf2_debug_sections &names)
1925 {
1926 flagword aflag = bfd_section_flags (sectp);
1927
1928 if ((aflag & SEC_HAS_CONTENTS) == 0)
1929 {
1930 }
1931 else if (elf_section_data (sectp)->this_hdr.sh_size
1932 > bfd_get_file_size (abfd))
1933 {
1934 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1935 warning (_("Discarding section %s which has a section size (%s"
1936 ") larger than the file size [in module %s]"),
1937 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1938 bfd_get_filename (abfd));
1939 }
1940 else if (section_is_p (sectp->name, &names.info))
1941 {
1942 this->info.s.section = sectp;
1943 this->info.size = bfd_section_size (sectp);
1944 }
1945 else if (section_is_p (sectp->name, &names.abbrev))
1946 {
1947 this->abbrev.s.section = sectp;
1948 this->abbrev.size = bfd_section_size (sectp);
1949 }
1950 else if (section_is_p (sectp->name, &names.line))
1951 {
1952 this->line.s.section = sectp;
1953 this->line.size = bfd_section_size (sectp);
1954 }
1955 else if (section_is_p (sectp->name, &names.loc))
1956 {
1957 this->loc.s.section = sectp;
1958 this->loc.size = bfd_section_size (sectp);
1959 }
1960 else if (section_is_p (sectp->name, &names.loclists))
1961 {
1962 this->loclists.s.section = sectp;
1963 this->loclists.size = bfd_section_size (sectp);
1964 }
1965 else if (section_is_p (sectp->name, &names.macinfo))
1966 {
1967 this->macinfo.s.section = sectp;
1968 this->macinfo.size = bfd_section_size (sectp);
1969 }
1970 else if (section_is_p (sectp->name, &names.macro))
1971 {
1972 this->macro.s.section = sectp;
1973 this->macro.size = bfd_section_size (sectp);
1974 }
1975 else if (section_is_p (sectp->name, &names.str))
1976 {
1977 this->str.s.section = sectp;
1978 this->str.size = bfd_section_size (sectp);
1979 }
1980 else if (section_is_p (sectp->name, &names.str_offsets))
1981 {
1982 this->str_offsets.s.section = sectp;
1983 this->str_offsets.size = bfd_section_size (sectp);
1984 }
1985 else if (section_is_p (sectp->name, &names.line_str))
1986 {
1987 this->line_str.s.section = sectp;
1988 this->line_str.size = bfd_section_size (sectp);
1989 }
1990 else if (section_is_p (sectp->name, &names.addr))
1991 {
1992 this->addr.s.section = sectp;
1993 this->addr.size = bfd_section_size (sectp);
1994 }
1995 else if (section_is_p (sectp->name, &names.frame))
1996 {
1997 this->frame.s.section = sectp;
1998 this->frame.size = bfd_section_size (sectp);
1999 }
2000 else if (section_is_p (sectp->name, &names.eh_frame))
2001 {
2002 this->eh_frame.s.section = sectp;
2003 this->eh_frame.size = bfd_section_size (sectp);
2004 }
2005 else if (section_is_p (sectp->name, &names.ranges))
2006 {
2007 this->ranges.s.section = sectp;
2008 this->ranges.size = bfd_section_size (sectp);
2009 }
2010 else if (section_is_p (sectp->name, &names.rnglists))
2011 {
2012 this->rnglists.s.section = sectp;
2013 this->rnglists.size = bfd_section_size (sectp);
2014 }
2015 else if (section_is_p (sectp->name, &names.types))
2016 {
2017 struct dwarf2_section_info type_section;
2018
2019 memset (&type_section, 0, sizeof (type_section));
2020 type_section.s.section = sectp;
2021 type_section.size = bfd_section_size (sectp);
2022
2023 this->types.push_back (type_section);
2024 }
2025 else if (section_is_p (sectp->name, &names.gdb_index))
2026 {
2027 this->gdb_index.s.section = sectp;
2028 this->gdb_index.size = bfd_section_size (sectp);
2029 }
2030 else if (section_is_p (sectp->name, &names.debug_names))
2031 {
2032 this->debug_names.s.section = sectp;
2033 this->debug_names.size = bfd_section_size (sectp);
2034 }
2035 else if (section_is_p (sectp->name, &names.debug_aranges))
2036 {
2037 this->debug_aranges.s.section = sectp;
2038 this->debug_aranges.size = bfd_section_size (sectp);
2039 }
2040
2041 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2042 && bfd_section_vma (sectp) == 0)
2043 this->has_section_at_zero = true;
2044 }
2045
2046 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2047 SECTION_NAME. */
2048
2049 void
2050 dwarf2_get_section_info (struct objfile *objfile,
2051 enum dwarf2_section_enum sect,
2052 asection **sectp, const gdb_byte **bufp,
2053 bfd_size_type *sizep)
2054 {
2055 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2056 struct dwarf2_section_info *info;
2057
2058 /* We may see an objfile without any DWARF, in which case we just
2059 return nothing. */
2060 if (per_objfile == NULL)
2061 {
2062 *sectp = NULL;
2063 *bufp = NULL;
2064 *sizep = 0;
2065 return;
2066 }
2067 switch (sect)
2068 {
2069 case DWARF2_DEBUG_FRAME:
2070 info = &per_objfile->per_bfd->frame;
2071 break;
2072 case DWARF2_EH_FRAME:
2073 info = &per_objfile->per_bfd->eh_frame;
2074 break;
2075 default:
2076 gdb_assert_not_reached ("unexpected section");
2077 }
2078
2079 info->read (objfile);
2080
2081 *sectp = info->get_bfd_section ();
2082 *bufp = info->buffer;
2083 *sizep = info->size;
2084 }
2085
2086 /* A helper function to find the sections for a .dwz file. */
2087
2088 static void
2089 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2090 {
2091 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2092
2093 /* Note that we only support the standard ELF names, because .dwz
2094 is ELF-only (at the time of writing). */
2095 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2096 {
2097 dwz_file->abbrev.s.section = sectp;
2098 dwz_file->abbrev.size = bfd_section_size (sectp);
2099 }
2100 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2101 {
2102 dwz_file->info.s.section = sectp;
2103 dwz_file->info.size = bfd_section_size (sectp);
2104 }
2105 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2106 {
2107 dwz_file->str.s.section = sectp;
2108 dwz_file->str.size = bfd_section_size (sectp);
2109 }
2110 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2111 {
2112 dwz_file->line.s.section = sectp;
2113 dwz_file->line.size = bfd_section_size (sectp);
2114 }
2115 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2116 {
2117 dwz_file->macro.s.section = sectp;
2118 dwz_file->macro.size = bfd_section_size (sectp);
2119 }
2120 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2121 {
2122 dwz_file->gdb_index.s.section = sectp;
2123 dwz_file->gdb_index.size = bfd_section_size (sectp);
2124 }
2125 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2126 {
2127 dwz_file->debug_names.s.section = sectp;
2128 dwz_file->debug_names.size = bfd_section_size (sectp);
2129 }
2130 }
2131
2132 /* See dwarf2read.h. */
2133
2134 struct dwz_file *
2135 dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
2136 {
2137 const char *filename;
2138 bfd_size_type buildid_len_arg;
2139 size_t buildid_len;
2140 bfd_byte *buildid;
2141
2142 if (per_bfd->dwz_file != NULL)
2143 return per_bfd->dwz_file.get ();
2144
2145 bfd_set_error (bfd_error_no_error);
2146 gdb::unique_xmalloc_ptr<char> data
2147 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2148 &buildid_len_arg, &buildid));
2149 if (data == NULL)
2150 {
2151 if (bfd_get_error () == bfd_error_no_error)
2152 return NULL;
2153 error (_("could not read '.gnu_debugaltlink' section: %s"),
2154 bfd_errmsg (bfd_get_error ()));
2155 }
2156
2157 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2158
2159 buildid_len = (size_t) buildid_len_arg;
2160
2161 filename = data.get ();
2162
2163 std::string abs_storage;
2164 if (!IS_ABSOLUTE_PATH (filename))
2165 {
2166 gdb::unique_xmalloc_ptr<char> abs
2167 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2168
2169 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2170 filename = abs_storage.c_str ();
2171 }
2172
2173 /* First try the file name given in the section. If that doesn't
2174 work, try to use the build-id instead. */
2175 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
2176 if (dwz_bfd != NULL)
2177 {
2178 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2179 dwz_bfd.reset (nullptr);
2180 }
2181
2182 if (dwz_bfd == NULL)
2183 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2184
2185 if (dwz_bfd == nullptr)
2186 {
2187 gdb::unique_xmalloc_ptr<char> alt_filename;
2188 const char *origname = bfd_get_filename (per_bfd->obfd);
2189
2190 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2191 buildid_len,
2192 origname,
2193 &alt_filename));
2194
2195 if (fd.get () >= 0)
2196 {
2197 /* File successfully retrieved from server. */
2198 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2199
2200 if (dwz_bfd == nullptr)
2201 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2202 alt_filename.get ());
2203 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2204 dwz_bfd.reset (nullptr);
2205 }
2206 }
2207
2208 if (dwz_bfd == NULL)
2209 error (_("could not find '.gnu_debugaltlink' file for %s"),
2210 bfd_get_filename (per_bfd->obfd));
2211
2212 std::unique_ptr<struct dwz_file> result
2213 (new struct dwz_file (std::move (dwz_bfd)));
2214
2215 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2216 result.get ());
2217
2218 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2219 per_bfd->dwz_file = std::move (result);
2220 return per_bfd->dwz_file.get ();
2221 }
2222 \f
2223 /* DWARF quick_symbols_functions support. */
2224
2225 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2226 unique line tables, so we maintain a separate table of all .debug_line
2227 derived entries to support the sharing.
2228 All the quick functions need is the list of file names. We discard the
2229 line_header when we're done and don't need to record it here. */
2230 struct quick_file_names
2231 {
2232 /* The data used to construct the hash key. */
2233 struct stmt_list_hash hash;
2234
2235 /* The number of entries in file_names, real_names. */
2236 unsigned int num_file_names;
2237
2238 /* The file names from the line table, after being run through
2239 file_full_name. */
2240 const char **file_names;
2241
2242 /* The file names from the line table after being run through
2243 gdb_realpath. These are computed lazily. */
2244 const char **real_names;
2245 };
2246
2247 /* When using the index (and thus not using psymtabs), each CU has an
2248 object of this type. This is used to hold information needed by
2249 the various "quick" methods. */
2250 struct dwarf2_per_cu_quick_data
2251 {
2252 /* The file table. This can be NULL if there was no file table
2253 or it's currently not read in.
2254 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2255 struct quick_file_names *file_names;
2256
2257 /* A temporary mark bit used when iterating over all CUs in
2258 expand_symtabs_matching. */
2259 unsigned int mark : 1;
2260
2261 /* True if we've tried to read the file table and found there isn't one.
2262 There will be no point in trying to read it again next time. */
2263 unsigned int no_file_data : 1;
2264 };
2265
2266 /* Utility hash function for a stmt_list_hash. */
2267
2268 static hashval_t
2269 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2270 {
2271 hashval_t v = 0;
2272
2273 if (stmt_list_hash->dwo_unit != NULL)
2274 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2275 v += to_underlying (stmt_list_hash->line_sect_off);
2276 return v;
2277 }
2278
2279 /* Utility equality function for a stmt_list_hash. */
2280
2281 static int
2282 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2283 const struct stmt_list_hash *rhs)
2284 {
2285 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2286 return 0;
2287 if (lhs->dwo_unit != NULL
2288 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2289 return 0;
2290
2291 return lhs->line_sect_off == rhs->line_sect_off;
2292 }
2293
2294 /* Hash function for a quick_file_names. */
2295
2296 static hashval_t
2297 hash_file_name_entry (const void *e)
2298 {
2299 const struct quick_file_names *file_data
2300 = (const struct quick_file_names *) e;
2301
2302 return hash_stmt_list_entry (&file_data->hash);
2303 }
2304
2305 /* Equality function for a quick_file_names. */
2306
2307 static int
2308 eq_file_name_entry (const void *a, const void *b)
2309 {
2310 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2311 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2312
2313 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2314 }
2315
2316 /* Delete function for a quick_file_names. */
2317
2318 static void
2319 delete_file_name_entry (void *e)
2320 {
2321 struct quick_file_names *file_data = (struct quick_file_names *) e;
2322 int i;
2323
2324 for (i = 0; i < file_data->num_file_names; ++i)
2325 {
2326 xfree ((void*) file_data->file_names[i]);
2327 if (file_data->real_names)
2328 xfree ((void*) file_data->real_names[i]);
2329 }
2330
2331 /* The space for the struct itself lives on the obstack, so we don't
2332 free it here. */
2333 }
2334
2335 /* Create a quick_file_names hash table. */
2336
2337 static htab_up
2338 create_quick_file_names_table (unsigned int nr_initial_entries)
2339 {
2340 return htab_up (htab_create_alloc (nr_initial_entries,
2341 hash_file_name_entry, eq_file_name_entry,
2342 delete_file_name_entry, xcalloc, xfree));
2343 }
2344
2345 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2346 function is unrelated to symtabs, symtab would have to be created afterwards.
2347 You should call age_cached_comp_units after processing the CU. */
2348
2349 static dwarf2_cu *
2350 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2351 bool skip_partial)
2352 {
2353 if (per_cu->is_debug_types)
2354 load_full_type_unit (per_cu, per_objfile);
2355 else
2356 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
2357
2358 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2359 if (cu == nullptr)
2360 return nullptr; /* Dummy CU. */
2361
2362 dwarf2_find_base_address (cu->dies, cu);
2363
2364 return cu;
2365 }
2366
2367 /* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2368
2369 static void
2370 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2371 dwarf2_per_objfile *per_objfile, bool skip_partial)
2372 {
2373 /* Skip type_unit_groups, reading the type units they contain
2374 is handled elsewhere. */
2375 if (per_cu->type_unit_group_p ())
2376 return;
2377
2378 /* The destructor of dwarf2_queue_guard frees any entries left on
2379 the queue. After this point we're guaranteed to leave this function
2380 with the dwarf queue empty. */
2381 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2382
2383 if (!per_objfile->symtab_set_p (per_cu))
2384 {
2385 queue_comp_unit (per_cu, per_objfile, language_minimal);
2386 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2387
2388 /* If we just loaded a CU from a DWO, and we're working with an index
2389 that may badly handle TUs, load all the TUs in that DWO as well.
2390 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2391 if (!per_cu->is_debug_types
2392 && cu != NULL
2393 && cu->dwo_unit != NULL
2394 && per_objfile->per_bfd->index_table != NULL
2395 && per_objfile->per_bfd->index_table->version <= 7
2396 /* DWP files aren't supported yet. */
2397 && get_dwp_file (per_objfile) == NULL)
2398 queue_and_load_all_dwo_tus (cu);
2399 }
2400
2401 process_queue (per_objfile);
2402
2403 /* Age the cache, releasing compilation units that have not
2404 been used recently. */
2405 per_objfile->age_comp_units ();
2406 }
2407
2408 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2409 the per-objfile for which this symtab is instantiated.
2410
2411 Returns the resulting symbol table. */
2412
2413 static struct compunit_symtab *
2414 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2415 dwarf2_per_objfile *per_objfile,
2416 bool skip_partial)
2417 {
2418 gdb_assert (per_objfile->per_bfd->using_index);
2419
2420 if (!per_objfile->symtab_set_p (per_cu))
2421 {
2422 free_cached_comp_units freer (per_objfile);
2423 scoped_restore decrementer = increment_reading_symtab ();
2424 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2425 process_cu_includes (per_objfile);
2426 }
2427
2428 return per_objfile->get_symtab (per_cu);
2429 }
2430
2431 /* See declaration. */
2432
2433 dwarf2_per_cu_data *
2434 dwarf2_per_bfd::get_cutu (int index)
2435 {
2436 if (index >= this->all_comp_units.size ())
2437 {
2438 index -= this->all_comp_units.size ();
2439 gdb_assert (index < this->all_type_units.size ());
2440 return &this->all_type_units[index]->per_cu;
2441 }
2442
2443 return this->all_comp_units[index];
2444 }
2445
2446 /* See declaration. */
2447
2448 dwarf2_per_cu_data *
2449 dwarf2_per_bfd::get_cu (int index)
2450 {
2451 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2452
2453 return this->all_comp_units[index];
2454 }
2455
2456 /* See declaration. */
2457
2458 signatured_type *
2459 dwarf2_per_bfd::get_tu (int index)
2460 {
2461 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2462
2463 return this->all_type_units[index];
2464 }
2465
2466 /* See read.h. */
2467
2468 dwarf2_per_cu_data *
2469 dwarf2_per_bfd::allocate_per_cu ()
2470 {
2471 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2472 result->per_bfd = this;
2473 result->index = m_num_psymtabs++;
2474 return result;
2475 }
2476
2477 /* See read.h. */
2478
2479 signatured_type *
2480 dwarf2_per_bfd::allocate_signatured_type ()
2481 {
2482 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2483 result->per_cu.per_bfd = this;
2484 result->per_cu.index = m_num_psymtabs++;
2485 return result;
2486 }
2487
2488 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2489 obstack, and constructed with the specified field values. */
2490
2491 static dwarf2_per_cu_data *
2492 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2493 struct dwarf2_section_info *section,
2494 int is_dwz,
2495 sect_offset sect_off, ULONGEST length)
2496 {
2497 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
2498 the_cu->sect_off = sect_off;
2499 the_cu->length = length;
2500 the_cu->section = section;
2501 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2502 struct dwarf2_per_cu_quick_data);
2503 the_cu->is_dwz = is_dwz;
2504 return the_cu;
2505 }
2506
2507 /* A helper for create_cus_from_index that handles a given list of
2508 CUs. */
2509
2510 static void
2511 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2512 const gdb_byte *cu_list, offset_type n_elements,
2513 struct dwarf2_section_info *section,
2514 int is_dwz)
2515 {
2516 for (offset_type i = 0; i < n_elements; i += 2)
2517 {
2518 gdb_static_assert (sizeof (ULONGEST) >= 8);
2519
2520 sect_offset sect_off
2521 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2522 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2523 cu_list += 2 * 8;
2524
2525 dwarf2_per_cu_data *per_cu
2526 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2527 length);
2528 per_bfd->all_comp_units.push_back (per_cu);
2529 }
2530 }
2531
2532 /* Read the CU list from the mapped index, and use it to create all
2533 the CU objects for PER_BFD. */
2534
2535 static void
2536 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2537 const gdb_byte *cu_list, offset_type cu_list_elements,
2538 const gdb_byte *dwz_list, offset_type dwz_elements)
2539 {
2540 gdb_assert (per_bfd->all_comp_units.empty ());
2541 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2542
2543 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2544 &per_bfd->info, 0);
2545
2546 if (dwz_elements == 0)
2547 return;
2548
2549 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2550 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2551 &dwz->info, 1);
2552 }
2553
2554 /* Create the signatured type hash table from the index. */
2555
2556 static void
2557 create_signatured_type_table_from_index
2558 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2559 const gdb_byte *bytes, offset_type elements)
2560 {
2561 gdb_assert (per_bfd->all_type_units.empty ());
2562 per_bfd->all_type_units.reserve (elements / 3);
2563
2564 htab_up sig_types_hash = allocate_signatured_type_table ();
2565
2566 for (offset_type i = 0; i < elements; i += 3)
2567 {
2568 struct signatured_type *sig_type;
2569 ULONGEST signature;
2570 void **slot;
2571 cu_offset type_offset_in_tu;
2572
2573 gdb_static_assert (sizeof (ULONGEST) >= 8);
2574 sect_offset sect_off
2575 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2576 type_offset_in_tu
2577 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2578 BFD_ENDIAN_LITTLE);
2579 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2580 bytes += 3 * 8;
2581
2582 sig_type = per_bfd->allocate_signatured_type ();
2583 sig_type->signature = signature;
2584 sig_type->type_offset_in_tu = type_offset_in_tu;
2585 sig_type->per_cu.is_debug_types = 1;
2586 sig_type->per_cu.section = section;
2587 sig_type->per_cu.sect_off = sect_off;
2588 sig_type->per_cu.v.quick
2589 = OBSTACK_ZALLOC (&per_bfd->obstack,
2590 struct dwarf2_per_cu_quick_data);
2591
2592 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2593 *slot = sig_type;
2594
2595 per_bfd->all_type_units.push_back (sig_type);
2596 }
2597
2598 per_bfd->signatured_types = std::move (sig_types_hash);
2599 }
2600
2601 /* Create the signatured type hash table from .debug_names. */
2602
2603 static void
2604 create_signatured_type_table_from_debug_names
2605 (dwarf2_per_objfile *per_objfile,
2606 const mapped_debug_names &map,
2607 struct dwarf2_section_info *section,
2608 struct dwarf2_section_info *abbrev_section)
2609 {
2610 struct objfile *objfile = per_objfile->objfile;
2611
2612 section->read (objfile);
2613 abbrev_section->read (objfile);
2614
2615 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2616 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2617
2618 htab_up sig_types_hash = allocate_signatured_type_table ();
2619
2620 for (uint32_t i = 0; i < map.tu_count; ++i)
2621 {
2622 struct signatured_type *sig_type;
2623 void **slot;
2624
2625 sect_offset sect_off
2626 = (sect_offset) (extract_unsigned_integer
2627 (map.tu_table_reordered + i * map.offset_size,
2628 map.offset_size,
2629 map.dwarf5_byte_order));
2630
2631 comp_unit_head cu_header;
2632 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2633 abbrev_section,
2634 section->buffer + to_underlying (sect_off),
2635 rcuh_kind::TYPE);
2636
2637 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
2638 sig_type->signature = cu_header.signature;
2639 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2640 sig_type->per_cu.is_debug_types = 1;
2641 sig_type->per_cu.section = section;
2642 sig_type->per_cu.sect_off = sect_off;
2643 sig_type->per_cu.v.quick
2644 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2645 struct dwarf2_per_cu_quick_data);
2646
2647 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2648 *slot = sig_type;
2649
2650 per_objfile->per_bfd->all_type_units.push_back (sig_type);
2651 }
2652
2653 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2654 }
2655
2656 /* Read the address map data from the mapped index, and use it to
2657 populate the objfile's psymtabs_addrmap. */
2658
2659 static void
2660 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2661 struct mapped_index *index)
2662 {
2663 struct objfile *objfile = per_objfile->objfile;
2664 struct gdbarch *gdbarch = objfile->arch ();
2665 const gdb_byte *iter, *end;
2666 struct addrmap *mutable_map;
2667 CORE_ADDR baseaddr;
2668
2669 auto_obstack temp_obstack;
2670
2671 mutable_map = addrmap_create_mutable (&temp_obstack);
2672
2673 iter = index->address_table.data ();
2674 end = iter + index->address_table.size ();
2675
2676 baseaddr = objfile->text_section_offset ();
2677
2678 while (iter < end)
2679 {
2680 ULONGEST hi, lo, cu_index;
2681 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2682 iter += 8;
2683 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2684 iter += 8;
2685 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2686 iter += 4;
2687
2688 if (lo > hi)
2689 {
2690 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2691 hex_string (lo), hex_string (hi));
2692 continue;
2693 }
2694
2695 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
2696 {
2697 complaint (_(".gdb_index address table has invalid CU number %u"),
2698 (unsigned) cu_index);
2699 continue;
2700 }
2701
2702 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2703 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2704 addrmap_set_empty (mutable_map, lo, hi - 1,
2705 per_objfile->per_bfd->get_cu (cu_index));
2706 }
2707
2708 objfile->partial_symtabs->psymtabs_addrmap
2709 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2710 }
2711
2712 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2713 populate the objfile's psymtabs_addrmap. */
2714
2715 static void
2716 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2717 struct dwarf2_section_info *section)
2718 {
2719 struct objfile *objfile = per_objfile->objfile;
2720 bfd *abfd = objfile->obfd;
2721 struct gdbarch *gdbarch = objfile->arch ();
2722 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2723
2724 auto_obstack temp_obstack;
2725 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2726
2727 std::unordered_map<sect_offset,
2728 dwarf2_per_cu_data *,
2729 gdb::hash_enum<sect_offset>>
2730 debug_info_offset_to_per_cu;
2731 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
2732 {
2733 const auto insertpair
2734 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2735 if (!insertpair.second)
2736 {
2737 warning (_("Section .debug_aranges in %s has duplicate "
2738 "debug_info_offset %s, ignoring .debug_aranges."),
2739 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2740 return;
2741 }
2742 }
2743
2744 section->read (objfile);
2745
2746 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2747
2748 const gdb_byte *addr = section->buffer;
2749
2750 while (addr < section->buffer + section->size)
2751 {
2752 const gdb_byte *const entry_addr = addr;
2753 unsigned int bytes_read;
2754
2755 const LONGEST entry_length = read_initial_length (abfd, addr,
2756 &bytes_read);
2757 addr += bytes_read;
2758
2759 const gdb_byte *const entry_end = addr + entry_length;
2760 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2761 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2762 if (addr + entry_length > section->buffer + section->size)
2763 {
2764 warning (_("Section .debug_aranges in %s entry at offset %s "
2765 "length %s exceeds section length %s, "
2766 "ignoring .debug_aranges."),
2767 objfile_name (objfile),
2768 plongest (entry_addr - section->buffer),
2769 plongest (bytes_read + entry_length),
2770 pulongest (section->size));
2771 return;
2772 }
2773
2774 /* The version number. */
2775 const uint16_t version = read_2_bytes (abfd, addr);
2776 addr += 2;
2777 if (version != 2)
2778 {
2779 warning (_("Section .debug_aranges in %s entry at offset %s "
2780 "has unsupported version %d, ignoring .debug_aranges."),
2781 objfile_name (objfile),
2782 plongest (entry_addr - section->buffer), version);
2783 return;
2784 }
2785
2786 const uint64_t debug_info_offset
2787 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2788 addr += offset_size;
2789 const auto per_cu_it
2790 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2791 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2792 {
2793 warning (_("Section .debug_aranges in %s entry at offset %s "
2794 "debug_info_offset %s does not exists, "
2795 "ignoring .debug_aranges."),
2796 objfile_name (objfile),
2797 plongest (entry_addr - section->buffer),
2798 pulongest (debug_info_offset));
2799 return;
2800 }
2801 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2802
2803 const uint8_t address_size = *addr++;
2804 if (address_size < 1 || address_size > 8)
2805 {
2806 warning (_("Section .debug_aranges in %s entry at offset %s "
2807 "address_size %u is invalid, ignoring .debug_aranges."),
2808 objfile_name (objfile),
2809 plongest (entry_addr - section->buffer), address_size);
2810 return;
2811 }
2812
2813 const uint8_t segment_selector_size = *addr++;
2814 if (segment_selector_size != 0)
2815 {
2816 warning (_("Section .debug_aranges in %s entry at offset %s "
2817 "segment_selector_size %u is not supported, "
2818 "ignoring .debug_aranges."),
2819 objfile_name (objfile),
2820 plongest (entry_addr - section->buffer),
2821 segment_selector_size);
2822 return;
2823 }
2824
2825 /* Must pad to an alignment boundary that is twice the address
2826 size. It is undocumented by the DWARF standard but GCC does
2827 use it. */
2828 for (size_t padding = ((-(addr - section->buffer))
2829 & (2 * address_size - 1));
2830 padding > 0; padding--)
2831 if (*addr++ != 0)
2832 {
2833 warning (_("Section .debug_aranges in %s entry at offset %s "
2834 "padding is not zero, ignoring .debug_aranges."),
2835 objfile_name (objfile),
2836 plongest (entry_addr - section->buffer));
2837 return;
2838 }
2839
2840 for (;;)
2841 {
2842 if (addr + 2 * address_size > entry_end)
2843 {
2844 warning (_("Section .debug_aranges in %s entry at offset %s "
2845 "address list is not properly terminated, "
2846 "ignoring .debug_aranges."),
2847 objfile_name (objfile),
2848 plongest (entry_addr - section->buffer));
2849 return;
2850 }
2851 ULONGEST start = extract_unsigned_integer (addr, address_size,
2852 dwarf5_byte_order);
2853 addr += address_size;
2854 ULONGEST length = extract_unsigned_integer (addr, address_size,
2855 dwarf5_byte_order);
2856 addr += address_size;
2857 if (start == 0 && length == 0)
2858 break;
2859 if (start == 0 && !per_objfile->per_bfd->has_section_at_zero)
2860 {
2861 /* Symbol was eliminated due to a COMDAT group. */
2862 continue;
2863 }
2864 ULONGEST end = start + length;
2865 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2866 - baseaddr);
2867 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2868 - baseaddr);
2869 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2870 }
2871 }
2872
2873 objfile->partial_symtabs->psymtabs_addrmap
2874 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2875 }
2876
2877 /* Find a slot in the mapped index INDEX for the object named NAME.
2878 If NAME is found, set *VEC_OUT to point to the CU vector in the
2879 constant pool and return true. If NAME cannot be found, return
2880 false. */
2881
2882 static bool
2883 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2884 offset_type **vec_out)
2885 {
2886 offset_type hash;
2887 offset_type slot, step;
2888 int (*cmp) (const char *, const char *);
2889
2890 gdb::unique_xmalloc_ptr<char> without_params;
2891 if (current_language->la_language == language_cplus
2892 || current_language->la_language == language_fortran
2893 || current_language->la_language == language_d)
2894 {
2895 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2896 not contain any. */
2897
2898 if (strchr (name, '(') != NULL)
2899 {
2900 without_params = cp_remove_params (name);
2901
2902 if (without_params != NULL)
2903 name = without_params.get ();
2904 }
2905 }
2906
2907 /* Index version 4 did not support case insensitive searches. But the
2908 indices for case insensitive languages are built in lowercase, therefore
2909 simulate our NAME being searched is also lowercased. */
2910 hash = mapped_index_string_hash ((index->version == 4
2911 && case_sensitivity == case_sensitive_off
2912 ? 5 : index->version),
2913 name);
2914
2915 slot = hash & (index->symbol_table.size () - 1);
2916 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2917 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2918
2919 for (;;)
2920 {
2921 const char *str;
2922
2923 const auto &bucket = index->symbol_table[slot];
2924 if (bucket.name == 0 && bucket.vec == 0)
2925 return false;
2926
2927 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2928 if (!cmp (name, str))
2929 {
2930 *vec_out = (offset_type *) (index->constant_pool
2931 + MAYBE_SWAP (bucket.vec));
2932 return true;
2933 }
2934
2935 slot = (slot + step) & (index->symbol_table.size () - 1);
2936 }
2937 }
2938
2939 /* A helper function that reads the .gdb_index from BUFFER and fills
2940 in MAP. FILENAME is the name of the file containing the data;
2941 it is used for error reporting. DEPRECATED_OK is true if it is
2942 ok to use deprecated sections.
2943
2944 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2945 out parameters that are filled in with information about the CU and
2946 TU lists in the section.
2947
2948 Returns true if all went well, false otherwise. */
2949
2950 static bool
2951 read_gdb_index_from_buffer (const char *filename,
2952 bool deprecated_ok,
2953 gdb::array_view<const gdb_byte> buffer,
2954 struct mapped_index *map,
2955 const gdb_byte **cu_list,
2956 offset_type *cu_list_elements,
2957 const gdb_byte **types_list,
2958 offset_type *types_list_elements)
2959 {
2960 const gdb_byte *addr = &buffer[0];
2961
2962 /* Version check. */
2963 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2964 /* Versions earlier than 3 emitted every copy of a psymbol. This
2965 causes the index to behave very poorly for certain requests. Version 3
2966 contained incomplete addrmap. So, it seems better to just ignore such
2967 indices. */
2968 if (version < 4)
2969 {
2970 static int warning_printed = 0;
2971 if (!warning_printed)
2972 {
2973 warning (_("Skipping obsolete .gdb_index section in %s."),
2974 filename);
2975 warning_printed = 1;
2976 }
2977 return 0;
2978 }
2979 /* Index version 4 uses a different hash function than index version
2980 5 and later.
2981
2982 Versions earlier than 6 did not emit psymbols for inlined
2983 functions. Using these files will cause GDB not to be able to
2984 set breakpoints on inlined functions by name, so we ignore these
2985 indices unless the user has done
2986 "set use-deprecated-index-sections on". */
2987 if (version < 6 && !deprecated_ok)
2988 {
2989 static int warning_printed = 0;
2990 if (!warning_printed)
2991 {
2992 warning (_("\
2993 Skipping deprecated .gdb_index section in %s.\n\
2994 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2995 to use the section anyway."),
2996 filename);
2997 warning_printed = 1;
2998 }
2999 return 0;
3000 }
3001 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3002 of the TU (for symbols coming from TUs),
3003 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3004 Plus gold-generated indices can have duplicate entries for global symbols,
3005 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3006 These are just performance bugs, and we can't distinguish gdb-generated
3007 indices from gold-generated ones, so issue no warning here. */
3008
3009 /* Indexes with higher version than the one supported by GDB may be no
3010 longer backward compatible. */
3011 if (version > 8)
3012 return 0;
3013
3014 map->version = version;
3015
3016 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3017
3018 int i = 0;
3019 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3020 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3021 / 8);
3022 ++i;
3023
3024 *types_list = addr + MAYBE_SWAP (metadata[i]);
3025 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3026 - MAYBE_SWAP (metadata[i]))
3027 / 8);
3028 ++i;
3029
3030 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3031 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3032 map->address_table
3033 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3034 ++i;
3035
3036 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3037 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3038 map->symbol_table
3039 = gdb::array_view<mapped_index::symbol_table_slot>
3040 ((mapped_index::symbol_table_slot *) symbol_table,
3041 (mapped_index::symbol_table_slot *) symbol_table_end);
3042
3043 ++i;
3044 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3045
3046 return 1;
3047 }
3048
3049 /* Callback types for dwarf2_read_gdb_index. */
3050
3051 typedef gdb::function_view
3052 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3053 get_gdb_index_contents_ftype;
3054 typedef gdb::function_view
3055 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3056 get_gdb_index_contents_dwz_ftype;
3057
3058 /* Read .gdb_index. If everything went ok, initialize the "quick"
3059 elements of all the CUs and return 1. Otherwise, return 0. */
3060
3061 static int
3062 dwarf2_read_gdb_index
3063 (dwarf2_per_objfile *per_objfile,
3064 get_gdb_index_contents_ftype get_gdb_index_contents,
3065 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3066 {
3067 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3068 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3069 struct dwz_file *dwz;
3070 struct objfile *objfile = per_objfile->objfile;
3071 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3072
3073 gdb::array_view<const gdb_byte> main_index_contents
3074 = get_gdb_index_contents (objfile, per_bfd);
3075
3076 if (main_index_contents.empty ())
3077 return 0;
3078
3079 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3080 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3081 use_deprecated_index_sections,
3082 main_index_contents, map.get (), &cu_list,
3083 &cu_list_elements, &types_list,
3084 &types_list_elements))
3085 return 0;
3086
3087 /* Don't use the index if it's empty. */
3088 if (map->symbol_table.empty ())
3089 return 0;
3090
3091 /* If there is a .dwz file, read it so we can get its CU list as
3092 well. */
3093 dwz = dwarf2_get_dwz_file (per_bfd);
3094 if (dwz != NULL)
3095 {
3096 struct mapped_index dwz_map;
3097 const gdb_byte *dwz_types_ignore;
3098 offset_type dwz_types_elements_ignore;
3099
3100 gdb::array_view<const gdb_byte> dwz_index_content
3101 = get_gdb_index_contents_dwz (objfile, dwz);
3102
3103 if (dwz_index_content.empty ())
3104 return 0;
3105
3106 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3107 1, dwz_index_content, &dwz_map,
3108 &dwz_list, &dwz_list_elements,
3109 &dwz_types_ignore,
3110 &dwz_types_elements_ignore))
3111 {
3112 warning (_("could not read '.gdb_index' section from %s; skipping"),
3113 bfd_get_filename (dwz->dwz_bfd.get ()));
3114 return 0;
3115 }
3116 }
3117
3118 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3119 dwz_list_elements);
3120
3121 if (types_list_elements)
3122 {
3123 /* We can only handle a single .debug_types when we have an
3124 index. */
3125 if (per_bfd->types.size () != 1)
3126 return 0;
3127
3128 dwarf2_section_info *section = &per_bfd->types[0];
3129
3130 create_signatured_type_table_from_index (per_bfd, section, types_list,
3131 types_list_elements);
3132 }
3133
3134 create_addrmap_from_index (per_objfile, map.get ());
3135
3136 per_bfd->index_table = std::move (map);
3137 per_bfd->using_index = 1;
3138 per_bfd->quick_file_names_table =
3139 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3140
3141 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
3142 objfiles using the same BFD. */
3143 gdb_assert (per_bfd->partial_symtabs == nullptr);
3144 per_bfd->partial_symtabs = objfile->partial_symtabs;
3145
3146 return 1;
3147 }
3148
3149 /* die_reader_func for dw2_get_file_names. */
3150
3151 static void
3152 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3153 const gdb_byte *info_ptr,
3154 struct die_info *comp_unit_die)
3155 {
3156 struct dwarf2_cu *cu = reader->cu;
3157 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3158 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3159 struct dwarf2_per_cu_data *lh_cu;
3160 struct attribute *attr;
3161 void **slot;
3162 struct quick_file_names *qfn;
3163
3164 gdb_assert (! this_cu->is_debug_types);
3165
3166 /* Our callers never want to match partial units -- instead they
3167 will match the enclosing full CU. */
3168 if (comp_unit_die->tag == DW_TAG_partial_unit)
3169 {
3170 this_cu->v.quick->no_file_data = 1;
3171 return;
3172 }
3173
3174 lh_cu = this_cu;
3175 slot = NULL;
3176
3177 line_header_up lh;
3178 sect_offset line_offset {};
3179
3180 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3181 if (attr != nullptr)
3182 {
3183 struct quick_file_names find_entry;
3184
3185 line_offset = (sect_offset) DW_UNSND (attr);
3186
3187 /* We may have already read in this line header (TU line header sharing).
3188 If we have we're done. */
3189 find_entry.hash.dwo_unit = cu->dwo_unit;
3190 find_entry.hash.line_sect_off = line_offset;
3191 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3192 &find_entry, INSERT);
3193 if (*slot != NULL)
3194 {
3195 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3196 return;
3197 }
3198
3199 lh = dwarf_decode_line_header (line_offset, cu);
3200 }
3201 if (lh == NULL)
3202 {
3203 lh_cu->v.quick->no_file_data = 1;
3204 return;
3205 }
3206
3207 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3208 qfn->hash.dwo_unit = cu->dwo_unit;
3209 qfn->hash.line_sect_off = line_offset;
3210 gdb_assert (slot != NULL);
3211 *slot = qfn;
3212
3213 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3214
3215 int offset = 0;
3216 if (strcmp (fnd.name, "<unknown>") != 0)
3217 ++offset;
3218
3219 qfn->num_file_names = offset + lh->file_names_size ();
3220 qfn->file_names =
3221 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3222 qfn->num_file_names);
3223 if (offset != 0)
3224 qfn->file_names[0] = xstrdup (fnd.name);
3225 for (int i = 0; i < lh->file_names_size (); ++i)
3226 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3227 fnd.comp_dir).release ();
3228 qfn->real_names = NULL;
3229
3230 lh_cu->v.quick->file_names = qfn;
3231 }
3232
3233 /* A helper for the "quick" functions which attempts to read the line
3234 table for THIS_CU. */
3235
3236 static struct quick_file_names *
3237 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3238 dwarf2_per_objfile *per_objfile)
3239 {
3240 /* This should never be called for TUs. */
3241 gdb_assert (! this_cu->is_debug_types);
3242 /* Nor type unit groups. */
3243 gdb_assert (! this_cu->type_unit_group_p ());
3244
3245 if (this_cu->v.quick->file_names != NULL)
3246 return this_cu->v.quick->file_names;
3247 /* If we know there is no line data, no point in looking again. */
3248 if (this_cu->v.quick->no_file_data)
3249 return NULL;
3250
3251 cutu_reader reader (this_cu, per_objfile);
3252 if (!reader.dummy_p)
3253 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3254
3255 if (this_cu->v.quick->no_file_data)
3256 return NULL;
3257 return this_cu->v.quick->file_names;
3258 }
3259
3260 /* A helper for the "quick" functions which computes and caches the
3261 real path for a given file name from the line table. */
3262
3263 static const char *
3264 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3265 struct quick_file_names *qfn, int index)
3266 {
3267 if (qfn->real_names == NULL)
3268 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3269 qfn->num_file_names, const char *);
3270
3271 if (qfn->real_names[index] == NULL)
3272 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3273
3274 return qfn->real_names[index];
3275 }
3276
3277 static struct symtab *
3278 dw2_find_last_source_symtab (struct objfile *objfile)
3279 {
3280 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3281 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3282 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3283
3284 if (cust == NULL)
3285 return NULL;
3286
3287 return compunit_primary_filetab (cust);
3288 }
3289
3290 /* Traversal function for dw2_forget_cached_source_info. */
3291
3292 static int
3293 dw2_free_cached_file_names (void **slot, void *info)
3294 {
3295 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3296
3297 if (file_data->real_names)
3298 {
3299 int i;
3300
3301 for (i = 0; i < file_data->num_file_names; ++i)
3302 {
3303 xfree ((void*) file_data->real_names[i]);
3304 file_data->real_names[i] = NULL;
3305 }
3306 }
3307
3308 return 1;
3309 }
3310
3311 static void
3312 dw2_forget_cached_source_info (struct objfile *objfile)
3313 {
3314 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3315
3316 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3317 dw2_free_cached_file_names, NULL);
3318 }
3319
3320 /* Helper function for dw2_map_symtabs_matching_filename that expands
3321 the symtabs and calls the iterator. */
3322
3323 static int
3324 dw2_map_expand_apply (struct objfile *objfile,
3325 struct dwarf2_per_cu_data *per_cu,
3326 const char *name, const char *real_path,
3327 gdb::function_view<bool (symtab *)> callback)
3328 {
3329 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3330
3331 /* Don't visit already-expanded CUs. */
3332 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3333 if (per_objfile->symtab_set_p (per_cu))
3334 return 0;
3335
3336 /* This may expand more than one symtab, and we want to iterate over
3337 all of them. */
3338 dw2_instantiate_symtab (per_cu, per_objfile, false);
3339
3340 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3341 last_made, callback);
3342 }
3343
3344 /* Implementation of the map_symtabs_matching_filename method. */
3345
3346 static bool
3347 dw2_map_symtabs_matching_filename
3348 (struct objfile *objfile, const char *name, const char *real_path,
3349 gdb::function_view<bool (symtab *)> callback)
3350 {
3351 const char *name_basename = lbasename (name);
3352 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3353
3354 /* The rule is CUs specify all the files, including those used by
3355 any TU, so there's no need to scan TUs here. */
3356
3357 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3358 {
3359 /* We only need to look at symtabs not already expanded. */
3360 if (per_objfile->symtab_set_p (per_cu))
3361 continue;
3362
3363 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3364 if (file_data == NULL)
3365 continue;
3366
3367 for (int j = 0; j < file_data->num_file_names; ++j)
3368 {
3369 const char *this_name = file_data->file_names[j];
3370 const char *this_real_name;
3371
3372 if (compare_filenames_for_search (this_name, name))
3373 {
3374 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3375 callback))
3376 return true;
3377 continue;
3378 }
3379
3380 /* Before we invoke realpath, which can get expensive when many
3381 files are involved, do a quick comparison of the basenames. */
3382 if (! basenames_may_differ
3383 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3384 continue;
3385
3386 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
3387 if (compare_filenames_for_search (this_real_name, name))
3388 {
3389 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3390 callback))
3391 return true;
3392 continue;
3393 }
3394
3395 if (real_path != NULL)
3396 {
3397 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3398 gdb_assert (IS_ABSOLUTE_PATH (name));
3399 if (this_real_name != NULL
3400 && FILENAME_CMP (real_path, this_real_name) == 0)
3401 {
3402 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3403 callback))
3404 return true;
3405 continue;
3406 }
3407 }
3408 }
3409 }
3410
3411 return false;
3412 }
3413
3414 /* Struct used to manage iterating over all CUs looking for a symbol. */
3415
3416 struct dw2_symtab_iterator
3417 {
3418 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3419 dwarf2_per_objfile *per_objfile;
3420 /* If set, only look for symbols that match that block. Valid values are
3421 GLOBAL_BLOCK and STATIC_BLOCK. */
3422 gdb::optional<block_enum> block_index;
3423 /* The kind of symbol we're looking for. */
3424 domain_enum domain;
3425 /* The list of CUs from the index entry of the symbol,
3426 or NULL if not found. */
3427 offset_type *vec;
3428 /* The next element in VEC to look at. */
3429 int next;
3430 /* The number of elements in VEC, or zero if there is no match. */
3431 int length;
3432 /* Have we seen a global version of the symbol?
3433 If so we can ignore all further global instances.
3434 This is to work around gold/15646, inefficient gold-generated
3435 indices. */
3436 int global_seen;
3437 };
3438
3439 /* Initialize the index symtab iterator ITER, common part. */
3440
3441 static void
3442 dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3443 dwarf2_per_objfile *per_objfile,
3444 gdb::optional<block_enum> block_index,
3445 domain_enum domain)
3446 {
3447 iter->per_objfile = per_objfile;
3448 iter->block_index = block_index;
3449 iter->domain = domain;
3450 iter->next = 0;
3451 iter->global_seen = 0;
3452 iter->vec = NULL;
3453 iter->length = 0;
3454 }
3455
3456 /* Initialize the index symtab iterator ITER, const char *NAME variant. */
3457
3458 static void
3459 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3460 dwarf2_per_objfile *per_objfile,
3461 gdb::optional<block_enum> block_index,
3462 domain_enum domain,
3463 const char *name)
3464 {
3465 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3466
3467 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3468 /* index is NULL if OBJF_READNOW. */
3469 if (index == NULL)
3470 return;
3471
3472 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3473 iter->length = MAYBE_SWAP (*iter->vec);
3474 }
3475
3476 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3477
3478 static void
3479 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3480 dwarf2_per_objfile *per_objfile,
3481 gdb::optional<block_enum> block_index,
3482 domain_enum domain, offset_type namei)
3483 {
3484 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3485
3486 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3487 /* index is NULL if OBJF_READNOW. */
3488 if (index == NULL)
3489 return;
3490
3491 gdb_assert (!index->symbol_name_slot_invalid (namei));
3492 const auto &bucket = index->symbol_table[namei];
3493
3494 iter->vec = (offset_type *) (index->constant_pool
3495 + MAYBE_SWAP (bucket.vec));
3496 iter->length = MAYBE_SWAP (*iter->vec);
3497 }
3498
3499 /* Return the next matching CU or NULL if there are no more. */
3500
3501 static struct dwarf2_per_cu_data *
3502 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3503 {
3504 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3505
3506 for ( ; iter->next < iter->length; ++iter->next)
3507 {
3508 offset_type cu_index_and_attrs =
3509 MAYBE_SWAP (iter->vec[iter->next + 1]);
3510 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3511 gdb_index_symbol_kind symbol_kind =
3512 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3513 /* Only check the symbol attributes if they're present.
3514 Indices prior to version 7 don't record them,
3515 and indices >= 7 may elide them for certain symbols
3516 (gold does this). */
3517 int attrs_valid =
3518 (per_objfile->per_bfd->index_table->version >= 7
3519 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3520
3521 /* Don't crash on bad data. */
3522 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3523 + per_objfile->per_bfd->all_type_units.size ()))
3524 {
3525 complaint (_(".gdb_index entry has bad CU index"
3526 " [in module %s]"), objfile_name (per_objfile->objfile));
3527 continue;
3528 }
3529
3530 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3531
3532 /* Skip if already read in. */
3533 if (per_objfile->symtab_set_p (per_cu))
3534 continue;
3535
3536 /* Check static vs global. */
3537 if (attrs_valid)
3538 {
3539 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3540
3541 if (iter->block_index.has_value ())
3542 {
3543 bool want_static = *iter->block_index == STATIC_BLOCK;
3544
3545 if (is_static != want_static)
3546 continue;
3547 }
3548
3549 /* Work around gold/15646. */
3550 if (!is_static
3551 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3552 {
3553 if (iter->global_seen)
3554 continue;
3555
3556 iter->global_seen = 1;
3557 }
3558 }
3559
3560 /* Only check the symbol's kind if it has one. */
3561 if (attrs_valid)
3562 {
3563 switch (iter->domain)
3564 {
3565 case VAR_DOMAIN:
3566 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3567 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3568 /* Some types are also in VAR_DOMAIN. */
3569 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3570 continue;
3571 break;
3572 case STRUCT_DOMAIN:
3573 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3574 continue;
3575 break;
3576 case LABEL_DOMAIN:
3577 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3578 continue;
3579 break;
3580 case MODULE_DOMAIN:
3581 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3582 continue;
3583 break;
3584 default:
3585 break;
3586 }
3587 }
3588
3589 ++iter->next;
3590 return per_cu;
3591 }
3592
3593 return NULL;
3594 }
3595
3596 static struct compunit_symtab *
3597 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3598 const char *name, domain_enum domain)
3599 {
3600 struct compunit_symtab *stab_best = NULL;
3601 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3602
3603 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3604
3605 struct dw2_symtab_iterator iter;
3606 struct dwarf2_per_cu_data *per_cu;
3607
3608 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
3609
3610 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3611 {
3612 struct symbol *sym, *with_opaque = NULL;
3613 struct compunit_symtab *stab
3614 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3615 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3616 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3617
3618 sym = block_find_symbol (block, name, domain,
3619 block_find_non_opaque_type_preferred,
3620 &with_opaque);
3621
3622 /* Some caution must be observed with overloaded functions
3623 and methods, since the index will not contain any overload
3624 information (but NAME might contain it). */
3625
3626 if (sym != NULL
3627 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3628 return stab;
3629 if (with_opaque != NULL
3630 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3631 stab_best = stab;
3632
3633 /* Keep looking through other CUs. */
3634 }
3635
3636 return stab_best;
3637 }
3638
3639 static void
3640 dw2_print_stats (struct objfile *objfile)
3641 {
3642 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3643 int total = (per_objfile->per_bfd->all_comp_units.size ()
3644 + per_objfile->per_bfd->all_type_units.size ());
3645 int count = 0;
3646
3647 for (int i = 0; i < total; ++i)
3648 {
3649 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3650
3651 if (!per_objfile->symtab_set_p (per_cu))
3652 ++count;
3653 }
3654 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3655 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3656 }
3657
3658 /* This dumps minimal information about the index.
3659 It is called via "mt print objfiles".
3660 One use is to verify .gdb_index has been loaded by the
3661 gdb.dwarf2/gdb-index.exp testcase. */
3662
3663 static void
3664 dw2_dump (struct objfile *objfile)
3665 {
3666 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3667
3668 gdb_assert (per_objfile->per_bfd->using_index);
3669 printf_filtered (".gdb_index:");
3670 if (per_objfile->per_bfd->index_table != NULL)
3671 {
3672 printf_filtered (" version %d\n",
3673 per_objfile->per_bfd->index_table->version);
3674 }
3675 else
3676 printf_filtered (" faked for \"readnow\"\n");
3677 printf_filtered ("\n");
3678 }
3679
3680 static void
3681 dw2_expand_symtabs_for_function (struct objfile *objfile,
3682 const char *func_name)
3683 {
3684 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3685
3686 struct dw2_symtab_iterator iter;
3687 struct dwarf2_per_cu_data *per_cu;
3688
3689 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
3690
3691 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3692 dw2_instantiate_symtab (per_cu, per_objfile, false);
3693
3694 }
3695
3696 static void
3697 dw2_expand_all_symtabs (struct objfile *objfile)
3698 {
3699 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3700 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3701 + per_objfile->per_bfd->all_type_units.size ());
3702
3703 for (int i = 0; i < total_units; ++i)
3704 {
3705 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3706
3707 /* We don't want to directly expand a partial CU, because if we
3708 read it with the wrong language, then assertion failures can
3709 be triggered later on. See PR symtab/23010. So, tell
3710 dw2_instantiate_symtab to skip partial CUs -- any important
3711 partial CU will be read via DW_TAG_imported_unit anyway. */
3712 dw2_instantiate_symtab (per_cu, per_objfile, true);
3713 }
3714 }
3715
3716 static void
3717 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3718 const char *fullname)
3719 {
3720 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3721
3722 /* We don't need to consider type units here.
3723 This is only called for examining code, e.g. expand_line_sal.
3724 There can be an order of magnitude (or more) more type units
3725 than comp units, and we avoid them if we can. */
3726
3727 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3728 {
3729 /* We only need to look at symtabs not already expanded. */
3730 if (per_objfile->symtab_set_p (per_cu))
3731 continue;
3732
3733 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3734 if (file_data == NULL)
3735 continue;
3736
3737 for (int j = 0; j < file_data->num_file_names; ++j)
3738 {
3739 const char *this_fullname = file_data->file_names[j];
3740
3741 if (filename_cmp (this_fullname, fullname) == 0)
3742 {
3743 dw2_instantiate_symtab (per_cu, per_objfile, false);
3744 break;
3745 }
3746 }
3747 }
3748 }
3749
3750 static void
3751 dw2_expand_symtabs_matching_symbol
3752 (mapped_index_base &index,
3753 const lookup_name_info &lookup_name_in,
3754 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3755 enum search_domain kind,
3756 gdb::function_view<bool (offset_type)> match_callback,
3757 dwarf2_per_objfile *per_objfile);
3758
3759 static void
3760 dw2_expand_symtabs_matching_one
3761 (dwarf2_per_cu_data *per_cu,
3762 dwarf2_per_objfile *per_objfile,
3763 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3764 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3765
3766 static void
3767 dw2_map_matching_symbols
3768 (struct objfile *objfile,
3769 const lookup_name_info &name, domain_enum domain,
3770 int global,
3771 gdb::function_view<symbol_found_callback_ftype> callback,
3772 symbol_compare_ftype *ordered_compare)
3773 {
3774 /* Used for Ada. */
3775 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3776
3777 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3778
3779 if (per_objfile->per_bfd->index_table != nullptr)
3780 {
3781 mapped_index &index = *per_objfile->per_bfd->index_table;
3782
3783 const char *match_name = name.ada ().lookup_name ().c_str ();
3784 auto matcher = [&] (const char *symname)
3785 {
3786 if (ordered_compare == nullptr)
3787 return true;
3788 return ordered_compare (symname, match_name) == 0;
3789 };
3790
3791 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3792 [&] (offset_type namei)
3793 {
3794 struct dw2_symtab_iterator iter;
3795 struct dwarf2_per_cu_data *per_cu;
3796
3797 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3798 namei);
3799 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3800 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3801 nullptr);
3802 return true;
3803 }, per_objfile);
3804 }
3805 else
3806 {
3807 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3808 proceed assuming all symtabs have been read in. */
3809 }
3810
3811 for (compunit_symtab *cust : objfile->compunits ())
3812 {
3813 const struct block *block;
3814
3815 if (cust == NULL)
3816 continue;
3817 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3818 if (!iterate_over_symbols_terminated (block, name,
3819 domain, callback))
3820 return;
3821 }
3822 }
3823
3824 /* Starting from a search name, return the string that finds the upper
3825 bound of all strings that start with SEARCH_NAME in a sorted name
3826 list. Returns the empty string to indicate that the upper bound is
3827 the end of the list. */
3828
3829 static std::string
3830 make_sort_after_prefix_name (const char *search_name)
3831 {
3832 /* When looking to complete "func", we find the upper bound of all
3833 symbols that start with "func" by looking for where we'd insert
3834 the closest string that would follow "func" in lexicographical
3835 order. Usually, that's "func"-with-last-character-incremented,
3836 i.e. "fund". Mind non-ASCII characters, though. Usually those
3837 will be UTF-8 multi-byte sequences, but we can't be certain.
3838 Especially mind the 0xff character, which is a valid character in
3839 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3840 rule out compilers allowing it in identifiers. Note that
3841 conveniently, strcmp/strcasecmp are specified to compare
3842 characters interpreted as unsigned char. So what we do is treat
3843 the whole string as a base 256 number composed of a sequence of
3844 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3845 to 0, and carries 1 to the following more-significant position.
3846 If the very first character in SEARCH_NAME ends up incremented
3847 and carries/overflows, then the upper bound is the end of the
3848 list. The string after the empty string is also the empty
3849 string.
3850
3851 Some examples of this operation:
3852
3853 SEARCH_NAME => "+1" RESULT
3854
3855 "abc" => "abd"
3856 "ab\xff" => "ac"
3857 "\xff" "a" "\xff" => "\xff" "b"
3858 "\xff" => ""
3859 "\xff\xff" => ""
3860 "" => ""
3861
3862 Then, with these symbols for example:
3863
3864 func
3865 func1
3866 fund
3867
3868 completing "func" looks for symbols between "func" and
3869 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3870 which finds "func" and "func1", but not "fund".
3871
3872 And with:
3873
3874 funcÿ (Latin1 'ÿ' [0xff])
3875 funcÿ1
3876 fund
3877
3878 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3879 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3880
3881 And with:
3882
3883 ÿÿ (Latin1 'ÿ' [0xff])
3884 ÿÿ1
3885
3886 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3887 the end of the list.
3888 */
3889 std::string after = search_name;
3890 while (!after.empty () && (unsigned char) after.back () == 0xff)
3891 after.pop_back ();
3892 if (!after.empty ())
3893 after.back () = (unsigned char) after.back () + 1;
3894 return after;
3895 }
3896
3897 /* See declaration. */
3898
3899 std::pair<std::vector<name_component>::const_iterator,
3900 std::vector<name_component>::const_iterator>
3901 mapped_index_base::find_name_components_bounds
3902 (const lookup_name_info &lookup_name_without_params, language lang,
3903 dwarf2_per_objfile *per_objfile) const
3904 {
3905 auto *name_cmp
3906 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3907
3908 const char *lang_name
3909 = lookup_name_without_params.language_lookup_name (lang);
3910
3911 /* Comparison function object for lower_bound that matches against a
3912 given symbol name. */
3913 auto lookup_compare_lower = [&] (const name_component &elem,
3914 const char *name)
3915 {
3916 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3917 const char *elem_name = elem_qualified + elem.name_offset;
3918 return name_cmp (elem_name, name) < 0;
3919 };
3920
3921 /* Comparison function object for upper_bound that matches against a
3922 given symbol name. */
3923 auto lookup_compare_upper = [&] (const char *name,
3924 const name_component &elem)
3925 {
3926 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3927 const char *elem_name = elem_qualified + elem.name_offset;
3928 return name_cmp (name, elem_name) < 0;
3929 };
3930
3931 auto begin = this->name_components.begin ();
3932 auto end = this->name_components.end ();
3933
3934 /* Find the lower bound. */
3935 auto lower = [&] ()
3936 {
3937 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3938 return begin;
3939 else
3940 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3941 } ();
3942
3943 /* Find the upper bound. */
3944 auto upper = [&] ()
3945 {
3946 if (lookup_name_without_params.completion_mode ())
3947 {
3948 /* In completion mode, we want UPPER to point past all
3949 symbols names that have the same prefix. I.e., with
3950 these symbols, and completing "func":
3951
3952 function << lower bound
3953 function1
3954 other_function << upper bound
3955
3956 We find the upper bound by looking for the insertion
3957 point of "func"-with-last-character-incremented,
3958 i.e. "fund". */
3959 std::string after = make_sort_after_prefix_name (lang_name);
3960 if (after.empty ())
3961 return end;
3962 return std::lower_bound (lower, end, after.c_str (),
3963 lookup_compare_lower);
3964 }
3965 else
3966 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3967 } ();
3968
3969 return {lower, upper};
3970 }
3971
3972 /* See declaration. */
3973
3974 void
3975 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3976 {
3977 if (!this->name_components.empty ())
3978 return;
3979
3980 this->name_components_casing = case_sensitivity;
3981 auto *name_cmp
3982 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3983
3984 /* The code below only knows how to break apart components of C++
3985 symbol names (and other languages that use '::' as
3986 namespace/module separator) and Ada symbol names. */
3987 auto count = this->symbol_name_count ();
3988 for (offset_type idx = 0; idx < count; idx++)
3989 {
3990 if (this->symbol_name_slot_invalid (idx))
3991 continue;
3992
3993 const char *name = this->symbol_name_at (idx, per_objfile);
3994
3995 /* Add each name component to the name component table. */
3996 unsigned int previous_len = 0;
3997
3998 if (strstr (name, "::") != nullptr)
3999 {
4000 for (unsigned int current_len = cp_find_first_component (name);
4001 name[current_len] != '\0';
4002 current_len += cp_find_first_component (name + current_len))
4003 {
4004 gdb_assert (name[current_len] == ':');
4005 this->name_components.push_back ({previous_len, idx});
4006 /* Skip the '::'. */
4007 current_len += 2;
4008 previous_len = current_len;
4009 }
4010 }
4011 else
4012 {
4013 /* Handle the Ada encoded (aka mangled) form here. */
4014 for (const char *iter = strstr (name, "__");
4015 iter != nullptr;
4016 iter = strstr (iter, "__"))
4017 {
4018 this->name_components.push_back ({previous_len, idx});
4019 iter += 2;
4020 previous_len = iter - name;
4021 }
4022 }
4023
4024 this->name_components.push_back ({previous_len, idx});
4025 }
4026
4027 /* Sort name_components elements by name. */
4028 auto name_comp_compare = [&] (const name_component &left,
4029 const name_component &right)
4030 {
4031 const char *left_qualified
4032 = this->symbol_name_at (left.idx, per_objfile);
4033 const char *right_qualified
4034 = this->symbol_name_at (right.idx, per_objfile);
4035
4036 const char *left_name = left_qualified + left.name_offset;
4037 const char *right_name = right_qualified + right.name_offset;
4038
4039 return name_cmp (left_name, right_name) < 0;
4040 };
4041
4042 std::sort (this->name_components.begin (),
4043 this->name_components.end (),
4044 name_comp_compare);
4045 }
4046
4047 /* Helper for dw2_expand_symtabs_matching that works with a
4048 mapped_index_base instead of the containing objfile. This is split
4049 to a separate function in order to be able to unit test the
4050 name_components matching using a mock mapped_index_base. For each
4051 symbol name that matches, calls MATCH_CALLBACK, passing it the
4052 symbol's index in the mapped_index_base symbol table. */
4053
4054 static void
4055 dw2_expand_symtabs_matching_symbol
4056 (mapped_index_base &index,
4057 const lookup_name_info &lookup_name_in,
4058 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4059 enum search_domain kind,
4060 gdb::function_view<bool (offset_type)> match_callback,
4061 dwarf2_per_objfile *per_objfile)
4062 {
4063 lookup_name_info lookup_name_without_params
4064 = lookup_name_in.make_ignore_params ();
4065
4066 /* Build the symbol name component sorted vector, if we haven't
4067 yet. */
4068 index.build_name_components (per_objfile);
4069
4070 /* The same symbol may appear more than once in the range though.
4071 E.g., if we're looking for symbols that complete "w", and we have
4072 a symbol named "w1::w2", we'll find the two name components for
4073 that same symbol in the range. To be sure we only call the
4074 callback once per symbol, we first collect the symbol name
4075 indexes that matched in a temporary vector and ignore
4076 duplicates. */
4077 std::vector<offset_type> matches;
4078
4079 struct name_and_matcher
4080 {
4081 symbol_name_matcher_ftype *matcher;
4082 const char *name;
4083
4084 bool operator== (const name_and_matcher &other) const
4085 {
4086 return matcher == other.matcher && strcmp (name, other.name) == 0;
4087 }
4088 };
4089
4090 /* A vector holding all the different symbol name matchers, for all
4091 languages. */
4092 std::vector<name_and_matcher> matchers;
4093
4094 for (int i = 0; i < nr_languages; i++)
4095 {
4096 enum language lang_e = (enum language) i;
4097
4098 const language_defn *lang = language_def (lang_e);
4099 symbol_name_matcher_ftype *name_matcher
4100 = get_symbol_name_matcher (lang, lookup_name_without_params);
4101
4102 name_and_matcher key {
4103 name_matcher,
4104 lookup_name_without_params.language_lookup_name (lang_e)
4105 };
4106
4107 /* Don't insert the same comparison routine more than once.
4108 Note that we do this linear walk. This is not a problem in
4109 practice because the number of supported languages is
4110 low. */
4111 if (std::find (matchers.begin (), matchers.end (), key)
4112 != matchers.end ())
4113 continue;
4114 matchers.push_back (std::move (key));
4115
4116 auto bounds
4117 = index.find_name_components_bounds (lookup_name_without_params,
4118 lang_e, per_objfile);
4119
4120 /* Now for each symbol name in range, check to see if we have a name
4121 match, and if so, call the MATCH_CALLBACK callback. */
4122
4123 for (; bounds.first != bounds.second; ++bounds.first)
4124 {
4125 const char *qualified
4126 = index.symbol_name_at (bounds.first->idx, per_objfile);
4127
4128 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4129 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4130 continue;
4131
4132 matches.push_back (bounds.first->idx);
4133 }
4134 }
4135
4136 std::sort (matches.begin (), matches.end ());
4137
4138 /* Finally call the callback, once per match. */
4139 ULONGEST prev = -1;
4140 for (offset_type idx : matches)
4141 {
4142 if (prev != idx)
4143 {
4144 if (!match_callback (idx))
4145 break;
4146 prev = idx;
4147 }
4148 }
4149
4150 /* Above we use a type wider than idx's for 'prev', since 0 and
4151 (offset_type)-1 are both possible values. */
4152 static_assert (sizeof (prev) > sizeof (offset_type), "");
4153 }
4154
4155 #if GDB_SELF_TEST
4156
4157 namespace selftests { namespace dw2_expand_symtabs_matching {
4158
4159 /* A mock .gdb_index/.debug_names-like name index table, enough to
4160 exercise dw2_expand_symtabs_matching_symbol, which works with the
4161 mapped_index_base interface. Builds an index from the symbol list
4162 passed as parameter to the constructor. */
4163 class mock_mapped_index : public mapped_index_base
4164 {
4165 public:
4166 mock_mapped_index (gdb::array_view<const char *> symbols)
4167 : m_symbol_table (symbols)
4168 {}
4169
4170 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4171
4172 /* Return the number of names in the symbol table. */
4173 size_t symbol_name_count () const override
4174 {
4175 return m_symbol_table.size ();
4176 }
4177
4178 /* Get the name of the symbol at IDX in the symbol table. */
4179 const char *symbol_name_at
4180 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
4181 {
4182 return m_symbol_table[idx];
4183 }
4184
4185 private:
4186 gdb::array_view<const char *> m_symbol_table;
4187 };
4188
4189 /* Convenience function that converts a NULL pointer to a "<null>"
4190 string, to pass to print routines. */
4191
4192 static const char *
4193 string_or_null (const char *str)
4194 {
4195 return str != NULL ? str : "<null>";
4196 }
4197
4198 /* Check if a lookup_name_info built from
4199 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4200 index. EXPECTED_LIST is the list of expected matches, in expected
4201 matching order. If no match expected, then an empty list is
4202 specified. Returns true on success. On failure prints a warning
4203 indicating the file:line that failed, and returns false. */
4204
4205 static bool
4206 check_match (const char *file, int line,
4207 mock_mapped_index &mock_index,
4208 const char *name, symbol_name_match_type match_type,
4209 bool completion_mode,
4210 std::initializer_list<const char *> expected_list,
4211 dwarf2_per_objfile *per_objfile)
4212 {
4213 lookup_name_info lookup_name (name, match_type, completion_mode);
4214
4215 bool matched = true;
4216
4217 auto mismatch = [&] (const char *expected_str,
4218 const char *got)
4219 {
4220 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4221 "expected=\"%s\", got=\"%s\"\n"),
4222 file, line,
4223 (match_type == symbol_name_match_type::FULL
4224 ? "FULL" : "WILD"),
4225 name, string_or_null (expected_str), string_or_null (got));
4226 matched = false;
4227 };
4228
4229 auto expected_it = expected_list.begin ();
4230 auto expected_end = expected_list.end ();
4231
4232 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4233 NULL, ALL_DOMAIN,
4234 [&] (offset_type idx)
4235 {
4236 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
4237 const char *expected_str
4238 = expected_it == expected_end ? NULL : *expected_it++;
4239
4240 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4241 mismatch (expected_str, matched_name);
4242 return true;
4243 }, per_objfile);
4244
4245 const char *expected_str
4246 = expected_it == expected_end ? NULL : *expected_it++;
4247 if (expected_str != NULL)
4248 mismatch (expected_str, NULL);
4249
4250 return matched;
4251 }
4252
4253 /* The symbols added to the mock mapped_index for testing (in
4254 canonical form). */
4255 static const char *test_symbols[] = {
4256 "function",
4257 "std::bar",
4258 "std::zfunction",
4259 "std::zfunction2",
4260 "w1::w2",
4261 "ns::foo<char*>",
4262 "ns::foo<int>",
4263 "ns::foo<long>",
4264 "ns2::tmpl<int>::foo2",
4265 "(anonymous namespace)::A::B::C",
4266
4267 /* These are used to check that the increment-last-char in the
4268 matching algorithm for completion doesn't match "t1_fund" when
4269 completing "t1_func". */
4270 "t1_func",
4271 "t1_func1",
4272 "t1_fund",
4273 "t1_fund1",
4274
4275 /* A UTF-8 name with multi-byte sequences to make sure that
4276 cp-name-parser understands this as a single identifier ("função"
4277 is "function" in PT). */
4278 u8"u8função",
4279
4280 /* \377 (0xff) is Latin1 'ÿ'. */
4281 "yfunc\377",
4282
4283 /* \377 (0xff) is Latin1 'ÿ'. */
4284 "\377",
4285 "\377\377123",
4286
4287 /* A name with all sorts of complications. Starts with "z" to make
4288 it easier for the completion tests below. */
4289 #define Z_SYM_NAME \
4290 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4291 "::tuple<(anonymous namespace)::ui*, " \
4292 "std::default_delete<(anonymous namespace)::ui>, void>"
4293
4294 Z_SYM_NAME
4295 };
4296
4297 /* Returns true if the mapped_index_base::find_name_component_bounds
4298 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4299 in completion mode. */
4300
4301 static bool
4302 check_find_bounds_finds (mapped_index_base &index,
4303 const char *search_name,
4304 gdb::array_view<const char *> expected_syms,
4305 dwarf2_per_objfile *per_objfile)
4306 {
4307 lookup_name_info lookup_name (search_name,
4308 symbol_name_match_type::FULL, true);
4309
4310 auto bounds = index.find_name_components_bounds (lookup_name,
4311 language_cplus,
4312 per_objfile);
4313
4314 size_t distance = std::distance (bounds.first, bounds.second);
4315 if (distance != expected_syms.size ())
4316 return false;
4317
4318 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4319 {
4320 auto nc_elem = bounds.first + exp_elem;
4321 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4322 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4323 return false;
4324 }
4325
4326 return true;
4327 }
4328
4329 /* Test the lower-level mapped_index::find_name_component_bounds
4330 method. */
4331
4332 static void
4333 test_mapped_index_find_name_component_bounds ()
4334 {
4335 mock_mapped_index mock_index (test_symbols);
4336
4337 mock_index.build_name_components (NULL /* per_objfile */);
4338
4339 /* Test the lower-level mapped_index::find_name_component_bounds
4340 method in completion mode. */
4341 {
4342 static const char *expected_syms[] = {
4343 "t1_func",
4344 "t1_func1",
4345 };
4346
4347 SELF_CHECK (check_find_bounds_finds
4348 (mock_index, "t1_func", expected_syms,
4349 NULL /* per_objfile */));
4350 }
4351
4352 /* Check that the increment-last-char in the name matching algorithm
4353 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4354 {
4355 static const char *expected_syms1[] = {
4356 "\377",
4357 "\377\377123",
4358 };
4359 SELF_CHECK (check_find_bounds_finds
4360 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4361
4362 static const char *expected_syms2[] = {
4363 "\377\377123",
4364 };
4365 SELF_CHECK (check_find_bounds_finds
4366 (mock_index, "\377\377", expected_syms2,
4367 NULL /* per_objfile */));
4368 }
4369 }
4370
4371 /* Test dw2_expand_symtabs_matching_symbol. */
4372
4373 static void
4374 test_dw2_expand_symtabs_matching_symbol ()
4375 {
4376 mock_mapped_index mock_index (test_symbols);
4377
4378 /* We let all tests run until the end even if some fails, for debug
4379 convenience. */
4380 bool any_mismatch = false;
4381
4382 /* Create the expected symbols list (an initializer_list). Needed
4383 because lists have commas, and we need to pass them to CHECK,
4384 which is a macro. */
4385 #define EXPECT(...) { __VA_ARGS__ }
4386
4387 /* Wrapper for check_match that passes down the current
4388 __FILE__/__LINE__. */
4389 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4390 any_mismatch |= !check_match (__FILE__, __LINE__, \
4391 mock_index, \
4392 NAME, MATCH_TYPE, COMPLETION_MODE, \
4393 EXPECTED_LIST, NULL)
4394
4395 /* Identity checks. */
4396 for (const char *sym : test_symbols)
4397 {
4398 /* Should be able to match all existing symbols. */
4399 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4400 EXPECT (sym));
4401
4402 /* Should be able to match all existing symbols with
4403 parameters. */
4404 std::string with_params = std::string (sym) + "(int)";
4405 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4406 EXPECT (sym));
4407
4408 /* Should be able to match all existing symbols with
4409 parameters and qualifiers. */
4410 with_params = std::string (sym) + " ( int ) const";
4411 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4412 EXPECT (sym));
4413
4414 /* This should really find sym, but cp-name-parser.y doesn't
4415 know about lvalue/rvalue qualifiers yet. */
4416 with_params = std::string (sym) + " ( int ) &&";
4417 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4418 {});
4419 }
4420
4421 /* Check that the name matching algorithm for completion doesn't get
4422 confused with Latin1 'ÿ' / 0xff. */
4423 {
4424 static const char str[] = "\377";
4425 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4426 EXPECT ("\377", "\377\377123"));
4427 }
4428
4429 /* Check that the increment-last-char in the matching algorithm for
4430 completion doesn't match "t1_fund" when completing "t1_func". */
4431 {
4432 static const char str[] = "t1_func";
4433 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4434 EXPECT ("t1_func", "t1_func1"));
4435 }
4436
4437 /* Check that completion mode works at each prefix of the expected
4438 symbol name. */
4439 {
4440 static const char str[] = "function(int)";
4441 size_t len = strlen (str);
4442 std::string lookup;
4443
4444 for (size_t i = 1; i < len; i++)
4445 {
4446 lookup.assign (str, i);
4447 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4448 EXPECT ("function"));
4449 }
4450 }
4451
4452 /* While "w" is a prefix of both components, the match function
4453 should still only be called once. */
4454 {
4455 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4456 EXPECT ("w1::w2"));
4457 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4458 EXPECT ("w1::w2"));
4459 }
4460
4461 /* Same, with a "complicated" symbol. */
4462 {
4463 static const char str[] = Z_SYM_NAME;
4464 size_t len = strlen (str);
4465 std::string lookup;
4466
4467 for (size_t i = 1; i < len; i++)
4468 {
4469 lookup.assign (str, i);
4470 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4471 EXPECT (Z_SYM_NAME));
4472 }
4473 }
4474
4475 /* In FULL mode, an incomplete symbol doesn't match. */
4476 {
4477 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4478 {});
4479 }
4480
4481 /* A complete symbol with parameters matches any overload, since the
4482 index has no overload info. */
4483 {
4484 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4485 EXPECT ("std::zfunction", "std::zfunction2"));
4486 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4487 EXPECT ("std::zfunction", "std::zfunction2"));
4488 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4489 EXPECT ("std::zfunction", "std::zfunction2"));
4490 }
4491
4492 /* Check that whitespace is ignored appropriately. A symbol with a
4493 template argument list. */
4494 {
4495 static const char expected[] = "ns::foo<int>";
4496 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4497 EXPECT (expected));
4498 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4499 EXPECT (expected));
4500 }
4501
4502 /* Check that whitespace is ignored appropriately. A symbol with a
4503 template argument list that includes a pointer. */
4504 {
4505 static const char expected[] = "ns::foo<char*>";
4506 /* Try both completion and non-completion modes. */
4507 static const bool completion_mode[2] = {false, true};
4508 for (size_t i = 0; i < 2; i++)
4509 {
4510 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4511 completion_mode[i], EXPECT (expected));
4512 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4513 completion_mode[i], EXPECT (expected));
4514
4515 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4516 completion_mode[i], EXPECT (expected));
4517 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4518 completion_mode[i], EXPECT (expected));
4519 }
4520 }
4521
4522 {
4523 /* Check method qualifiers are ignored. */
4524 static const char expected[] = "ns::foo<char*>";
4525 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4526 symbol_name_match_type::FULL, true, EXPECT (expected));
4527 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4528 symbol_name_match_type::FULL, true, EXPECT (expected));
4529 CHECK_MATCH ("foo < char * > ( int ) const",
4530 symbol_name_match_type::WILD, true, EXPECT (expected));
4531 CHECK_MATCH ("foo < char * > ( int ) &&",
4532 symbol_name_match_type::WILD, true, EXPECT (expected));
4533 }
4534
4535 /* Test lookup names that don't match anything. */
4536 {
4537 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4538 {});
4539
4540 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4541 {});
4542 }
4543
4544 /* Some wild matching tests, exercising "(anonymous namespace)",
4545 which should not be confused with a parameter list. */
4546 {
4547 static const char *syms[] = {
4548 "A::B::C",
4549 "B::C",
4550 "C",
4551 "A :: B :: C ( int )",
4552 "B :: C ( int )",
4553 "C ( int )",
4554 };
4555
4556 for (const char *s : syms)
4557 {
4558 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4559 EXPECT ("(anonymous namespace)::A::B::C"));
4560 }
4561 }
4562
4563 {
4564 static const char expected[] = "ns2::tmpl<int>::foo2";
4565 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4566 EXPECT (expected));
4567 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4568 EXPECT (expected));
4569 }
4570
4571 SELF_CHECK (!any_mismatch);
4572
4573 #undef EXPECT
4574 #undef CHECK_MATCH
4575 }
4576
4577 static void
4578 run_test ()
4579 {
4580 test_mapped_index_find_name_component_bounds ();
4581 test_dw2_expand_symtabs_matching_symbol ();
4582 }
4583
4584 }} // namespace selftests::dw2_expand_symtabs_matching
4585
4586 #endif /* GDB_SELF_TEST */
4587
4588 /* If FILE_MATCHER is NULL or if PER_CU has
4589 dwarf2_per_cu_quick_data::MARK set (see
4590 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4591 EXPANSION_NOTIFY on it. */
4592
4593 static void
4594 dw2_expand_symtabs_matching_one
4595 (dwarf2_per_cu_data *per_cu,
4596 dwarf2_per_objfile *per_objfile,
4597 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4598 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4599 {
4600 if (file_matcher == NULL || per_cu->v.quick->mark)
4601 {
4602 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4603
4604 compunit_symtab *symtab
4605 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4606 gdb_assert (symtab != nullptr);
4607
4608 if (expansion_notify != NULL && symtab_was_null)
4609 expansion_notify (symtab);
4610 }
4611 }
4612
4613 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4614 matched, to expand corresponding CUs that were marked. IDX is the
4615 index of the symbol name that matched. */
4616
4617 static void
4618 dw2_expand_marked_cus
4619 (dwarf2_per_objfile *per_objfile, offset_type idx,
4620 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4621 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4622 search_domain kind)
4623 {
4624 offset_type *vec, vec_len, vec_idx;
4625 bool global_seen = false;
4626 mapped_index &index = *per_objfile->per_bfd->index_table;
4627
4628 vec = (offset_type *) (index.constant_pool
4629 + MAYBE_SWAP (index.symbol_table[idx].vec));
4630 vec_len = MAYBE_SWAP (vec[0]);
4631 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4632 {
4633 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4634 /* This value is only valid for index versions >= 7. */
4635 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4636 gdb_index_symbol_kind symbol_kind =
4637 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4638 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4639 /* Only check the symbol attributes if they're present.
4640 Indices prior to version 7 don't record them,
4641 and indices >= 7 may elide them for certain symbols
4642 (gold does this). */
4643 int attrs_valid =
4644 (index.version >= 7
4645 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4646
4647 /* Work around gold/15646. */
4648 if (attrs_valid
4649 && !is_static
4650 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4651 {
4652 if (global_seen)
4653 continue;
4654
4655 global_seen = true;
4656 }
4657
4658 /* Only check the symbol's kind if it has one. */
4659 if (attrs_valid)
4660 {
4661 switch (kind)
4662 {
4663 case VARIABLES_DOMAIN:
4664 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4665 continue;
4666 break;
4667 case FUNCTIONS_DOMAIN:
4668 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4669 continue;
4670 break;
4671 case TYPES_DOMAIN:
4672 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4673 continue;
4674 break;
4675 case MODULES_DOMAIN:
4676 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4677 continue;
4678 break;
4679 default:
4680 break;
4681 }
4682 }
4683
4684 /* Don't crash on bad data. */
4685 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4686 + per_objfile->per_bfd->all_type_units.size ()))
4687 {
4688 complaint (_(".gdb_index entry has bad CU index"
4689 " [in module %s]"), objfile_name (per_objfile->objfile));
4690 continue;
4691 }
4692
4693 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4694 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4695 expansion_notify);
4696 }
4697 }
4698
4699 /* If FILE_MATCHER is non-NULL, set all the
4700 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4701 that match FILE_MATCHER. */
4702
4703 static void
4704 dw_expand_symtabs_matching_file_matcher
4705 (dwarf2_per_objfile *per_objfile,
4706 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4707 {
4708 if (file_matcher == NULL)
4709 return;
4710
4711 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4712 htab_eq_pointer,
4713 NULL, xcalloc, xfree));
4714 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4715 htab_eq_pointer,
4716 NULL, xcalloc, xfree));
4717
4718 /* The rule is CUs specify all the files, including those used by
4719 any TU, so there's no need to scan TUs here. */
4720
4721 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4722 {
4723 QUIT;
4724
4725 per_cu->v.quick->mark = 0;
4726
4727 /* We only need to look at symtabs not already expanded. */
4728 if (per_objfile->symtab_set_p (per_cu))
4729 continue;
4730
4731 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4732 if (file_data == NULL)
4733 continue;
4734
4735 if (htab_find (visited_not_found.get (), file_data) != NULL)
4736 continue;
4737 else if (htab_find (visited_found.get (), file_data) != NULL)
4738 {
4739 per_cu->v.quick->mark = 1;
4740 continue;
4741 }
4742
4743 for (int j = 0; j < file_data->num_file_names; ++j)
4744 {
4745 const char *this_real_name;
4746
4747 if (file_matcher (file_data->file_names[j], false))
4748 {
4749 per_cu->v.quick->mark = 1;
4750 break;
4751 }
4752
4753 /* Before we invoke realpath, which can get expensive when many
4754 files are involved, do a quick comparison of the basenames. */
4755 if (!basenames_may_differ
4756 && !file_matcher (lbasename (file_data->file_names[j]),
4757 true))
4758 continue;
4759
4760 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4761 if (file_matcher (this_real_name, false))
4762 {
4763 per_cu->v.quick->mark = 1;
4764 break;
4765 }
4766 }
4767
4768 void **slot = htab_find_slot (per_cu->v.quick->mark
4769 ? visited_found.get ()
4770 : visited_not_found.get (),
4771 file_data, INSERT);
4772 *slot = file_data;
4773 }
4774 }
4775
4776 static void
4777 dw2_expand_symtabs_matching
4778 (struct objfile *objfile,
4779 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4780 const lookup_name_info *lookup_name,
4781 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4782 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4783 enum search_domain kind)
4784 {
4785 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4786
4787 /* index_table is NULL if OBJF_READNOW. */
4788 if (!per_objfile->per_bfd->index_table)
4789 return;
4790
4791 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4792
4793 if (symbol_matcher == NULL && lookup_name == NULL)
4794 {
4795 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4796 {
4797 QUIT;
4798
4799 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4800 file_matcher, expansion_notify);
4801 }
4802 return;
4803 }
4804
4805 mapped_index &index = *per_objfile->per_bfd->index_table;
4806
4807 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4808 symbol_matcher,
4809 kind, [&] (offset_type idx)
4810 {
4811 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4812 kind);
4813 return true;
4814 }, per_objfile);
4815 }
4816
4817 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4818 symtab. */
4819
4820 static struct compunit_symtab *
4821 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4822 CORE_ADDR pc)
4823 {
4824 int i;
4825
4826 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4827 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4828 return cust;
4829
4830 if (cust->includes == NULL)
4831 return NULL;
4832
4833 for (i = 0; cust->includes[i]; ++i)
4834 {
4835 struct compunit_symtab *s = cust->includes[i];
4836
4837 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4838 if (s != NULL)
4839 return s;
4840 }
4841
4842 return NULL;
4843 }
4844
4845 static struct compunit_symtab *
4846 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4847 struct bound_minimal_symbol msymbol,
4848 CORE_ADDR pc,
4849 struct obj_section *section,
4850 int warn_if_readin)
4851 {
4852 struct dwarf2_per_cu_data *data;
4853 struct compunit_symtab *result;
4854
4855 if (!objfile->partial_symtabs->psymtabs_addrmap)
4856 return NULL;
4857
4858 CORE_ADDR baseaddr = objfile->text_section_offset ();
4859 data = (struct dwarf2_per_cu_data *) addrmap_find
4860 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4861 if (!data)
4862 return NULL;
4863
4864 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4865 if (warn_if_readin && per_objfile->symtab_set_p (data))
4866 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4867 paddress (objfile->arch (), pc));
4868
4869 result = recursively_find_pc_sect_compunit_symtab
4870 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4871
4872 gdb_assert (result != NULL);
4873 return result;
4874 }
4875
4876 static void
4877 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4878 void *data, int need_fullname)
4879 {
4880 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4881
4882 if (!per_objfile->per_bfd->filenames_cache)
4883 {
4884 per_objfile->per_bfd->filenames_cache.emplace ();
4885
4886 htab_up visited (htab_create_alloc (10,
4887 htab_hash_pointer, htab_eq_pointer,
4888 NULL, xcalloc, xfree));
4889
4890 /* The rule is CUs specify all the files, including those used
4891 by any TU, so there's no need to scan TUs here. We can
4892 ignore file names coming from already-expanded CUs. */
4893
4894 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4895 {
4896 if (per_objfile->symtab_set_p (per_cu))
4897 {
4898 void **slot = htab_find_slot (visited.get (),
4899 per_cu->v.quick->file_names,
4900 INSERT);
4901
4902 *slot = per_cu->v.quick->file_names;
4903 }
4904 }
4905
4906 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4907 {
4908 /* We only need to look at symtabs not already expanded. */
4909 if (per_objfile->symtab_set_p (per_cu))
4910 continue;
4911
4912 quick_file_names *file_data
4913 = dw2_get_file_names (per_cu, per_objfile);
4914 if (file_data == NULL)
4915 continue;
4916
4917 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4918 if (*slot)
4919 {
4920 /* Already visited. */
4921 continue;
4922 }
4923 *slot = file_data;
4924
4925 for (int j = 0; j < file_data->num_file_names; ++j)
4926 {
4927 const char *filename = file_data->file_names[j];
4928 per_objfile->per_bfd->filenames_cache->seen (filename);
4929 }
4930 }
4931 }
4932
4933 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4934 {
4935 gdb::unique_xmalloc_ptr<char> this_real_name;
4936
4937 if (need_fullname)
4938 this_real_name = gdb_realpath (filename);
4939 (*fun) (filename, this_real_name.get (), data);
4940 });
4941 }
4942
4943 static int
4944 dw2_has_symbols (struct objfile *objfile)
4945 {
4946 return 1;
4947 }
4948
4949 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4950 {
4951 dw2_has_symbols,
4952 dw2_find_last_source_symtab,
4953 dw2_forget_cached_source_info,
4954 dw2_map_symtabs_matching_filename,
4955 dw2_lookup_symbol,
4956 NULL,
4957 dw2_print_stats,
4958 dw2_dump,
4959 dw2_expand_symtabs_for_function,
4960 dw2_expand_all_symtabs,
4961 dw2_expand_symtabs_with_fullname,
4962 dw2_map_matching_symbols,
4963 dw2_expand_symtabs_matching,
4964 dw2_find_pc_sect_compunit_symtab,
4965 NULL,
4966 dw2_map_symbol_filenames
4967 };
4968
4969 /* DWARF-5 debug_names reader. */
4970
4971 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4972 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4973
4974 /* A helper function that reads the .debug_names section in SECTION
4975 and fills in MAP. FILENAME is the name of the file containing the
4976 section; it is used for error reporting.
4977
4978 Returns true if all went well, false otherwise. */
4979
4980 static bool
4981 read_debug_names_from_section (struct objfile *objfile,
4982 const char *filename,
4983 struct dwarf2_section_info *section,
4984 mapped_debug_names &map)
4985 {
4986 if (section->empty ())
4987 return false;
4988
4989 /* Older elfutils strip versions could keep the section in the main
4990 executable while splitting it for the separate debug info file. */
4991 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4992 return false;
4993
4994 section->read (objfile);
4995
4996 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4997
4998 const gdb_byte *addr = section->buffer;
4999
5000 bfd *const abfd = section->get_bfd_owner ();
5001
5002 unsigned int bytes_read;
5003 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5004 addr += bytes_read;
5005
5006 map.dwarf5_is_dwarf64 = bytes_read != 4;
5007 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5008 if (bytes_read + length != section->size)
5009 {
5010 /* There may be multiple per-CU indices. */
5011 warning (_("Section .debug_names in %s length %s does not match "
5012 "section length %s, ignoring .debug_names."),
5013 filename, plongest (bytes_read + length),
5014 pulongest (section->size));
5015 return false;
5016 }
5017
5018 /* The version number. */
5019 uint16_t version = read_2_bytes (abfd, addr);
5020 addr += 2;
5021 if (version != 5)
5022 {
5023 warning (_("Section .debug_names in %s has unsupported version %d, "
5024 "ignoring .debug_names."),
5025 filename, version);
5026 return false;
5027 }
5028
5029 /* Padding. */
5030 uint16_t padding = read_2_bytes (abfd, addr);
5031 addr += 2;
5032 if (padding != 0)
5033 {
5034 warning (_("Section .debug_names in %s has unsupported padding %d, "
5035 "ignoring .debug_names."),
5036 filename, padding);
5037 return false;
5038 }
5039
5040 /* comp_unit_count - The number of CUs in the CU list. */
5041 map.cu_count = read_4_bytes (abfd, addr);
5042 addr += 4;
5043
5044 /* local_type_unit_count - The number of TUs in the local TU
5045 list. */
5046 map.tu_count = read_4_bytes (abfd, addr);
5047 addr += 4;
5048
5049 /* foreign_type_unit_count - The number of TUs in the foreign TU
5050 list. */
5051 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5052 addr += 4;
5053 if (foreign_tu_count != 0)
5054 {
5055 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5056 "ignoring .debug_names."),
5057 filename, static_cast<unsigned long> (foreign_tu_count));
5058 return false;
5059 }
5060
5061 /* bucket_count - The number of hash buckets in the hash lookup
5062 table. */
5063 map.bucket_count = read_4_bytes (abfd, addr);
5064 addr += 4;
5065
5066 /* name_count - The number of unique names in the index. */
5067 map.name_count = read_4_bytes (abfd, addr);
5068 addr += 4;
5069
5070 /* abbrev_table_size - The size in bytes of the abbreviations
5071 table. */
5072 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5073 addr += 4;
5074
5075 /* augmentation_string_size - The size in bytes of the augmentation
5076 string. This value is rounded up to a multiple of 4. */
5077 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5078 addr += 4;
5079 map.augmentation_is_gdb = ((augmentation_string_size
5080 == sizeof (dwarf5_augmentation))
5081 && memcmp (addr, dwarf5_augmentation,
5082 sizeof (dwarf5_augmentation)) == 0);
5083 augmentation_string_size += (-augmentation_string_size) & 3;
5084 addr += augmentation_string_size;
5085
5086 /* List of CUs */
5087 map.cu_table_reordered = addr;
5088 addr += map.cu_count * map.offset_size;
5089
5090 /* List of Local TUs */
5091 map.tu_table_reordered = addr;
5092 addr += map.tu_count * map.offset_size;
5093
5094 /* Hash Lookup Table */
5095 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5096 addr += map.bucket_count * 4;
5097 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5098 addr += map.name_count * 4;
5099
5100 /* Name Table */
5101 map.name_table_string_offs_reordered = addr;
5102 addr += map.name_count * map.offset_size;
5103 map.name_table_entry_offs_reordered = addr;
5104 addr += map.name_count * map.offset_size;
5105
5106 const gdb_byte *abbrev_table_start = addr;
5107 for (;;)
5108 {
5109 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5110 addr += bytes_read;
5111 if (index_num == 0)
5112 break;
5113
5114 const auto insertpair
5115 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5116 if (!insertpair.second)
5117 {
5118 warning (_("Section .debug_names in %s has duplicate index %s, "
5119 "ignoring .debug_names."),
5120 filename, pulongest (index_num));
5121 return false;
5122 }
5123 mapped_debug_names::index_val &indexval = insertpair.first->second;
5124 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5125 addr += bytes_read;
5126
5127 for (;;)
5128 {
5129 mapped_debug_names::index_val::attr attr;
5130 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5131 addr += bytes_read;
5132 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5133 addr += bytes_read;
5134 if (attr.form == DW_FORM_implicit_const)
5135 {
5136 attr.implicit_const = read_signed_leb128 (abfd, addr,
5137 &bytes_read);
5138 addr += bytes_read;
5139 }
5140 if (attr.dw_idx == 0 && attr.form == 0)
5141 break;
5142 indexval.attr_vec.push_back (std::move (attr));
5143 }
5144 }
5145 if (addr != abbrev_table_start + abbrev_table_size)
5146 {
5147 warning (_("Section .debug_names in %s has abbreviation_table "
5148 "of size %s vs. written as %u, ignoring .debug_names."),
5149 filename, plongest (addr - abbrev_table_start),
5150 abbrev_table_size);
5151 return false;
5152 }
5153 map.entry_pool = addr;
5154
5155 return true;
5156 }
5157
5158 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5159 list. */
5160
5161 static void
5162 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
5163 const mapped_debug_names &map,
5164 dwarf2_section_info &section,
5165 bool is_dwz)
5166 {
5167 if (!map.augmentation_is_gdb)
5168 {
5169 for (uint32_t i = 0; i < map.cu_count; ++i)
5170 {
5171 sect_offset sect_off
5172 = (sect_offset) (extract_unsigned_integer
5173 (map.cu_table_reordered + i * map.offset_size,
5174 map.offset_size,
5175 map.dwarf5_byte_order));
5176 /* We don't know the length of the CU, because the CU list in a
5177 .debug_names index can be incomplete, so we can't use the start of
5178 the next CU as end of this CU. We create the CUs here with length 0,
5179 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5180 dwarf2_per_cu_data *per_cu
5181 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5182 per_bfd->all_comp_units.push_back (per_cu);
5183 }
5184 }
5185
5186 sect_offset sect_off_prev;
5187 for (uint32_t i = 0; i <= map.cu_count; ++i)
5188 {
5189 sect_offset sect_off_next;
5190 if (i < map.cu_count)
5191 {
5192 sect_off_next
5193 = (sect_offset) (extract_unsigned_integer
5194 (map.cu_table_reordered + i * map.offset_size,
5195 map.offset_size,
5196 map.dwarf5_byte_order));
5197 }
5198 else
5199 sect_off_next = (sect_offset) section.size;
5200 if (i >= 1)
5201 {
5202 const ULONGEST length = sect_off_next - sect_off_prev;
5203 dwarf2_per_cu_data *per_cu
5204 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5205 sect_off_prev, length);
5206 per_bfd->all_comp_units.push_back (per_cu);
5207 }
5208 sect_off_prev = sect_off_next;
5209 }
5210 }
5211
5212 /* Read the CU list from the mapped index, and use it to create all
5213 the CU objects for this dwarf2_per_objfile. */
5214
5215 static void
5216 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5217 const mapped_debug_names &map,
5218 const mapped_debug_names &dwz_map)
5219 {
5220 gdb_assert (per_bfd->all_comp_units.empty ());
5221 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5222
5223 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5224 false /* is_dwz */);
5225
5226 if (dwz_map.cu_count == 0)
5227 return;
5228
5229 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5230 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5231 true /* is_dwz */);
5232 }
5233
5234 /* Read .debug_names. If everything went ok, initialize the "quick"
5235 elements of all the CUs and return true. Otherwise, return false. */
5236
5237 static bool
5238 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
5239 {
5240 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5241 mapped_debug_names dwz_map;
5242 struct objfile *objfile = per_objfile->objfile;
5243 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5244
5245 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5246 &per_objfile->per_bfd->debug_names, *map))
5247 return false;
5248
5249 /* Don't use the index if it's empty. */
5250 if (map->name_count == 0)
5251 return false;
5252
5253 /* If there is a .dwz file, read it so we can get its CU list as
5254 well. */
5255 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5256 if (dwz != NULL)
5257 {
5258 if (!read_debug_names_from_section (objfile,
5259 bfd_get_filename (dwz->dwz_bfd.get ()),
5260 &dwz->debug_names, dwz_map))
5261 {
5262 warning (_("could not read '.debug_names' section from %s; skipping"),
5263 bfd_get_filename (dwz->dwz_bfd.get ()));
5264 return false;
5265 }
5266 }
5267
5268 create_cus_from_debug_names (per_bfd, *map, dwz_map);
5269
5270 if (map->tu_count != 0)
5271 {
5272 /* We can only handle a single .debug_types when we have an
5273 index. */
5274 if (per_bfd->types.size () != 1)
5275 return false;
5276
5277 dwarf2_section_info *section = &per_bfd->types[0];
5278
5279 create_signatured_type_table_from_debug_names
5280 (per_objfile, *map, section, &per_bfd->abbrev);
5281 }
5282
5283 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
5284
5285 per_bfd->debug_names_table = std::move (map);
5286 per_bfd->using_index = 1;
5287 per_bfd->quick_file_names_table =
5288 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
5289
5290 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
5291 objfiles using the same BFD. */
5292 gdb_assert (per_bfd->partial_symtabs == nullptr);
5293 per_bfd->partial_symtabs = objfile->partial_symtabs;
5294
5295 return true;
5296 }
5297
5298 /* Type used to manage iterating over all CUs looking for a symbol for
5299 .debug_names. */
5300
5301 class dw2_debug_names_iterator
5302 {
5303 public:
5304 dw2_debug_names_iterator (const mapped_debug_names &map,
5305 gdb::optional<block_enum> block_index,
5306 domain_enum domain,
5307 const char *name, dwarf2_per_objfile *per_objfile)
5308 : m_map (map), m_block_index (block_index), m_domain (domain),
5309 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5310 m_per_objfile (per_objfile)
5311 {}
5312
5313 dw2_debug_names_iterator (const mapped_debug_names &map,
5314 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
5315 : m_map (map),
5316 m_search (search),
5317 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5318 m_per_objfile (per_objfile)
5319 {}
5320
5321 dw2_debug_names_iterator (const mapped_debug_names &map,
5322 block_enum block_index, domain_enum domain,
5323 uint32_t namei, dwarf2_per_objfile *per_objfile)
5324 : m_map (map), m_block_index (block_index), m_domain (domain),
5325 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5326 m_per_objfile (per_objfile)
5327 {}
5328
5329 /* Return the next matching CU or NULL if there are no more. */
5330 dwarf2_per_cu_data *next ();
5331
5332 private:
5333 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5334 const char *name,
5335 dwarf2_per_objfile *per_objfile);
5336 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5337 uint32_t namei,
5338 dwarf2_per_objfile *per_objfile);
5339
5340 /* The internalized form of .debug_names. */
5341 const mapped_debug_names &m_map;
5342
5343 /* If set, only look for symbols that match that block. Valid values are
5344 GLOBAL_BLOCK and STATIC_BLOCK. */
5345 const gdb::optional<block_enum> m_block_index;
5346
5347 /* The kind of symbol we're looking for. */
5348 const domain_enum m_domain = UNDEF_DOMAIN;
5349 const search_domain m_search = ALL_DOMAIN;
5350
5351 /* The list of CUs from the index entry of the symbol, or NULL if
5352 not found. */
5353 const gdb_byte *m_addr;
5354
5355 dwarf2_per_objfile *m_per_objfile;
5356 };
5357
5358 const char *
5359 mapped_debug_names::namei_to_name
5360 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5361 {
5362 const ULONGEST namei_string_offs
5363 = extract_unsigned_integer ((name_table_string_offs_reordered
5364 + namei * offset_size),
5365 offset_size,
5366 dwarf5_byte_order);
5367 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5368 }
5369
5370 /* Find a slot in .debug_names for the object named NAME. If NAME is
5371 found, return pointer to its pool data. If NAME cannot be found,
5372 return NULL. */
5373
5374 const gdb_byte *
5375 dw2_debug_names_iterator::find_vec_in_debug_names
5376 (const mapped_debug_names &map, const char *name,
5377 dwarf2_per_objfile *per_objfile)
5378 {
5379 int (*cmp) (const char *, const char *);
5380
5381 gdb::unique_xmalloc_ptr<char> without_params;
5382 if (current_language->la_language == language_cplus
5383 || current_language->la_language == language_fortran
5384 || current_language->la_language == language_d)
5385 {
5386 /* NAME is already canonical. Drop any qualifiers as
5387 .debug_names does not contain any. */
5388
5389 if (strchr (name, '(') != NULL)
5390 {
5391 without_params = cp_remove_params (name);
5392 if (without_params != NULL)
5393 name = without_params.get ();
5394 }
5395 }
5396
5397 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5398
5399 const uint32_t full_hash = dwarf5_djb_hash (name);
5400 uint32_t namei
5401 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5402 (map.bucket_table_reordered
5403 + (full_hash % map.bucket_count)), 4,
5404 map.dwarf5_byte_order);
5405 if (namei == 0)
5406 return NULL;
5407 --namei;
5408 if (namei >= map.name_count)
5409 {
5410 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5411 "[in module %s]"),
5412 namei, map.name_count,
5413 objfile_name (per_objfile->objfile));
5414 return NULL;
5415 }
5416
5417 for (;;)
5418 {
5419 const uint32_t namei_full_hash
5420 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5421 (map.hash_table_reordered + namei), 4,
5422 map.dwarf5_byte_order);
5423 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5424 return NULL;
5425
5426 if (full_hash == namei_full_hash)
5427 {
5428 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5429
5430 #if 0 /* An expensive sanity check. */
5431 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5432 {
5433 complaint (_("Wrong .debug_names hash for string at index %u "
5434 "[in module %s]"),
5435 namei, objfile_name (dwarf2_per_objfile->objfile));
5436 return NULL;
5437 }
5438 #endif
5439
5440 if (cmp (namei_string, name) == 0)
5441 {
5442 const ULONGEST namei_entry_offs
5443 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5444 + namei * map.offset_size),
5445 map.offset_size, map.dwarf5_byte_order);
5446 return map.entry_pool + namei_entry_offs;
5447 }
5448 }
5449
5450 ++namei;
5451 if (namei >= map.name_count)
5452 return NULL;
5453 }
5454 }
5455
5456 const gdb_byte *
5457 dw2_debug_names_iterator::find_vec_in_debug_names
5458 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5459 {
5460 if (namei >= map.name_count)
5461 {
5462 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5463 "[in module %s]"),
5464 namei, map.name_count,
5465 objfile_name (per_objfile->objfile));
5466 return NULL;
5467 }
5468
5469 const ULONGEST namei_entry_offs
5470 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5471 + namei * map.offset_size),
5472 map.offset_size, map.dwarf5_byte_order);
5473 return map.entry_pool + namei_entry_offs;
5474 }
5475
5476 /* See dw2_debug_names_iterator. */
5477
5478 dwarf2_per_cu_data *
5479 dw2_debug_names_iterator::next ()
5480 {
5481 if (m_addr == NULL)
5482 return NULL;
5483
5484 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5485 struct objfile *objfile = m_per_objfile->objfile;
5486 bfd *const abfd = objfile->obfd;
5487
5488 again:
5489
5490 unsigned int bytes_read;
5491 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5492 m_addr += bytes_read;
5493 if (abbrev == 0)
5494 return NULL;
5495
5496 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5497 if (indexval_it == m_map.abbrev_map.cend ())
5498 {
5499 complaint (_("Wrong .debug_names undefined abbrev code %s "
5500 "[in module %s]"),
5501 pulongest (abbrev), objfile_name (objfile));
5502 return NULL;
5503 }
5504 const mapped_debug_names::index_val &indexval = indexval_it->second;
5505 enum class symbol_linkage {
5506 unknown,
5507 static_,
5508 extern_,
5509 } symbol_linkage_ = symbol_linkage::unknown;
5510 dwarf2_per_cu_data *per_cu = NULL;
5511 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5512 {
5513 ULONGEST ull;
5514 switch (attr.form)
5515 {
5516 case DW_FORM_implicit_const:
5517 ull = attr.implicit_const;
5518 break;
5519 case DW_FORM_flag_present:
5520 ull = 1;
5521 break;
5522 case DW_FORM_udata:
5523 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5524 m_addr += bytes_read;
5525 break;
5526 case DW_FORM_ref4:
5527 ull = read_4_bytes (abfd, m_addr);
5528 m_addr += 4;
5529 break;
5530 case DW_FORM_ref8:
5531 ull = read_8_bytes (abfd, m_addr);
5532 m_addr += 8;
5533 break;
5534 case DW_FORM_ref_sig8:
5535 ull = read_8_bytes (abfd, m_addr);
5536 m_addr += 8;
5537 break;
5538 default:
5539 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5540 dwarf_form_name (attr.form),
5541 objfile_name (objfile));
5542 return NULL;
5543 }
5544 switch (attr.dw_idx)
5545 {
5546 case DW_IDX_compile_unit:
5547 /* Don't crash on bad data. */
5548 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
5549 {
5550 complaint (_(".debug_names entry has bad CU index %s"
5551 " [in module %s]"),
5552 pulongest (ull),
5553 objfile_name (objfile));
5554 continue;
5555 }
5556 per_cu = per_bfd->get_cutu (ull);
5557 break;
5558 case DW_IDX_type_unit:
5559 /* Don't crash on bad data. */
5560 if (ull >= per_bfd->all_type_units.size ())
5561 {
5562 complaint (_(".debug_names entry has bad TU index %s"
5563 " [in module %s]"),
5564 pulongest (ull),
5565 objfile_name (objfile));
5566 continue;
5567 }
5568 per_cu = &per_bfd->get_tu (ull)->per_cu;
5569 break;
5570 case DW_IDX_die_offset:
5571 /* In a per-CU index (as opposed to a per-module index), index
5572 entries without CU attribute implicitly refer to the single CU. */
5573 if (per_cu == NULL)
5574 per_cu = per_bfd->get_cu (0);
5575 break;
5576 case DW_IDX_GNU_internal:
5577 if (!m_map.augmentation_is_gdb)
5578 break;
5579 symbol_linkage_ = symbol_linkage::static_;
5580 break;
5581 case DW_IDX_GNU_external:
5582 if (!m_map.augmentation_is_gdb)
5583 break;
5584 symbol_linkage_ = symbol_linkage::extern_;
5585 break;
5586 }
5587 }
5588
5589 /* Skip if already read in. */
5590 if (m_per_objfile->symtab_set_p (per_cu))
5591 goto again;
5592
5593 /* Check static vs global. */
5594 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5595 {
5596 const bool want_static = *m_block_index == STATIC_BLOCK;
5597 const bool symbol_is_static =
5598 symbol_linkage_ == symbol_linkage::static_;
5599 if (want_static != symbol_is_static)
5600 goto again;
5601 }
5602
5603 /* Match dw2_symtab_iter_next, symbol_kind
5604 and debug_names::psymbol_tag. */
5605 switch (m_domain)
5606 {
5607 case VAR_DOMAIN:
5608 switch (indexval.dwarf_tag)
5609 {
5610 case DW_TAG_variable:
5611 case DW_TAG_subprogram:
5612 /* Some types are also in VAR_DOMAIN. */
5613 case DW_TAG_typedef:
5614 case DW_TAG_structure_type:
5615 break;
5616 default:
5617 goto again;
5618 }
5619 break;
5620 case STRUCT_DOMAIN:
5621 switch (indexval.dwarf_tag)
5622 {
5623 case DW_TAG_typedef:
5624 case DW_TAG_structure_type:
5625 break;
5626 default:
5627 goto again;
5628 }
5629 break;
5630 case LABEL_DOMAIN:
5631 switch (indexval.dwarf_tag)
5632 {
5633 case 0:
5634 case DW_TAG_variable:
5635 break;
5636 default:
5637 goto again;
5638 }
5639 break;
5640 case MODULE_DOMAIN:
5641 switch (indexval.dwarf_tag)
5642 {
5643 case DW_TAG_module:
5644 break;
5645 default:
5646 goto again;
5647 }
5648 break;
5649 default:
5650 break;
5651 }
5652
5653 /* Match dw2_expand_symtabs_matching, symbol_kind and
5654 debug_names::psymbol_tag. */
5655 switch (m_search)
5656 {
5657 case VARIABLES_DOMAIN:
5658 switch (indexval.dwarf_tag)
5659 {
5660 case DW_TAG_variable:
5661 break;
5662 default:
5663 goto again;
5664 }
5665 break;
5666 case FUNCTIONS_DOMAIN:
5667 switch (indexval.dwarf_tag)
5668 {
5669 case DW_TAG_subprogram:
5670 break;
5671 default:
5672 goto again;
5673 }
5674 break;
5675 case TYPES_DOMAIN:
5676 switch (indexval.dwarf_tag)
5677 {
5678 case DW_TAG_typedef:
5679 case DW_TAG_structure_type:
5680 break;
5681 default:
5682 goto again;
5683 }
5684 break;
5685 case MODULES_DOMAIN:
5686 switch (indexval.dwarf_tag)
5687 {
5688 case DW_TAG_module:
5689 break;
5690 default:
5691 goto again;
5692 }
5693 default:
5694 break;
5695 }
5696
5697 return per_cu;
5698 }
5699
5700 static struct compunit_symtab *
5701 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5702 const char *name, domain_enum domain)
5703 {
5704 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5705
5706 const auto &mapp = per_objfile->per_bfd->debug_names_table;
5707 if (!mapp)
5708 {
5709 /* index is NULL if OBJF_READNOW. */
5710 return NULL;
5711 }
5712 const auto &map = *mapp;
5713
5714 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
5715
5716 struct compunit_symtab *stab_best = NULL;
5717 struct dwarf2_per_cu_data *per_cu;
5718 while ((per_cu = iter.next ()) != NULL)
5719 {
5720 struct symbol *sym, *with_opaque = NULL;
5721 compunit_symtab *stab
5722 = dw2_instantiate_symtab (per_cu, per_objfile, false);
5723 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5724 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5725
5726 sym = block_find_symbol (block, name, domain,
5727 block_find_non_opaque_type_preferred,
5728 &with_opaque);
5729
5730 /* Some caution must be observed with overloaded functions and
5731 methods, since the index will not contain any overload
5732 information (but NAME might contain it). */
5733
5734 if (sym != NULL
5735 && strcmp_iw (sym->search_name (), name) == 0)
5736 return stab;
5737 if (with_opaque != NULL
5738 && strcmp_iw (with_opaque->search_name (), name) == 0)
5739 stab_best = stab;
5740
5741 /* Keep looking through other CUs. */
5742 }
5743
5744 return stab_best;
5745 }
5746
5747 /* This dumps minimal information about .debug_names. It is called
5748 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5749 uses this to verify that .debug_names has been loaded. */
5750
5751 static void
5752 dw2_debug_names_dump (struct objfile *objfile)
5753 {
5754 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5755
5756 gdb_assert (per_objfile->per_bfd->using_index);
5757 printf_filtered (".debug_names:");
5758 if (per_objfile->per_bfd->debug_names_table)
5759 printf_filtered (" exists\n");
5760 else
5761 printf_filtered (" faked for \"readnow\"\n");
5762 printf_filtered ("\n");
5763 }
5764
5765 static void
5766 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5767 const char *func_name)
5768 {
5769 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5770
5771 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5772 if (per_objfile->per_bfd->debug_names_table)
5773 {
5774 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5775
5776 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
5777 per_objfile);
5778
5779 struct dwarf2_per_cu_data *per_cu;
5780 while ((per_cu = iter.next ()) != NULL)
5781 dw2_instantiate_symtab (per_cu, per_objfile, false);
5782 }
5783 }
5784
5785 static void
5786 dw2_debug_names_map_matching_symbols
5787 (struct objfile *objfile,
5788 const lookup_name_info &name, domain_enum domain,
5789 int global,
5790 gdb::function_view<symbol_found_callback_ftype> callback,
5791 symbol_compare_ftype *ordered_compare)
5792 {
5793 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5794
5795 /* debug_names_table is NULL if OBJF_READNOW. */
5796 if (!per_objfile->per_bfd->debug_names_table)
5797 return;
5798
5799 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5800 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5801
5802 const char *match_name = name.ada ().lookup_name ().c_str ();
5803 auto matcher = [&] (const char *symname)
5804 {
5805 if (ordered_compare == nullptr)
5806 return true;
5807 return ordered_compare (symname, match_name) == 0;
5808 };
5809
5810 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5811 [&] (offset_type namei)
5812 {
5813 /* The name was matched, now expand corresponding CUs that were
5814 marked. */
5815 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
5816 per_objfile);
5817
5818 struct dwarf2_per_cu_data *per_cu;
5819 while ((per_cu = iter.next ()) != NULL)
5820 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5821 nullptr);
5822 return true;
5823 }, per_objfile);
5824
5825 /* It's a shame we couldn't do this inside the
5826 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5827 that have already been expanded. Instead, this loop matches what
5828 the psymtab code does. */
5829 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5830 {
5831 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
5832 if (symtab != nullptr)
5833 {
5834 const struct block *block
5835 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5836 if (!iterate_over_symbols_terminated (block, name,
5837 domain, callback))
5838 break;
5839 }
5840 }
5841 }
5842
5843 static void
5844 dw2_debug_names_expand_symtabs_matching
5845 (struct objfile *objfile,
5846 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5847 const lookup_name_info *lookup_name,
5848 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5849 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5850 enum search_domain kind)
5851 {
5852 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5853
5854 /* debug_names_table is NULL if OBJF_READNOW. */
5855 if (!per_objfile->per_bfd->debug_names_table)
5856 return;
5857
5858 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5859
5860 if (symbol_matcher == NULL && lookup_name == NULL)
5861 {
5862 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5863 {
5864 QUIT;
5865
5866 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5867 expansion_notify);
5868 }
5869 return;
5870 }
5871
5872 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5873
5874 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5875 symbol_matcher,
5876 kind, [&] (offset_type namei)
5877 {
5878 /* The name was matched, now expand corresponding CUs that were
5879 marked. */
5880 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
5881
5882 struct dwarf2_per_cu_data *per_cu;
5883 while ((per_cu = iter.next ()) != NULL)
5884 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5885 expansion_notify);
5886 return true;
5887 }, per_objfile);
5888 }
5889
5890 const struct quick_symbol_functions dwarf2_debug_names_functions =
5891 {
5892 dw2_has_symbols,
5893 dw2_find_last_source_symtab,
5894 dw2_forget_cached_source_info,
5895 dw2_map_symtabs_matching_filename,
5896 dw2_debug_names_lookup_symbol,
5897 NULL,
5898 dw2_print_stats,
5899 dw2_debug_names_dump,
5900 dw2_debug_names_expand_symtabs_for_function,
5901 dw2_expand_all_symtabs,
5902 dw2_expand_symtabs_with_fullname,
5903 dw2_debug_names_map_matching_symbols,
5904 dw2_debug_names_expand_symtabs_matching,
5905 dw2_find_pc_sect_compunit_symtab,
5906 NULL,
5907 dw2_map_symbol_filenames
5908 };
5909
5910 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5911 to either a dwarf2_per_bfd or dwz_file object. */
5912
5913 template <typename T>
5914 static gdb::array_view<const gdb_byte>
5915 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5916 {
5917 dwarf2_section_info *section = &section_owner->gdb_index;
5918
5919 if (section->empty ())
5920 return {};
5921
5922 /* Older elfutils strip versions could keep the section in the main
5923 executable while splitting it for the separate debug info file. */
5924 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5925 return {};
5926
5927 section->read (obj);
5928
5929 /* dwarf2_section_info::size is a bfd_size_type, while
5930 gdb::array_view works with size_t. On 32-bit hosts, with
5931 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5932 is 32-bit. So we need an explicit narrowing conversion here.
5933 This is fine, because it's impossible to allocate or mmap an
5934 array/buffer larger than what size_t can represent. */
5935 return gdb::make_array_view (section->buffer, section->size);
5936 }
5937
5938 /* Lookup the index cache for the contents of the index associated to
5939 DWARF2_OBJ. */
5940
5941 static gdb::array_view<const gdb_byte>
5942 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5943 {
5944 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5945 if (build_id == nullptr)
5946 return {};
5947
5948 return global_index_cache.lookup_gdb_index (build_id,
5949 &dwarf2_per_bfd->index_cache_res);
5950 }
5951
5952 /* Same as the above, but for DWZ. */
5953
5954 static gdb::array_view<const gdb_byte>
5955 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5956 {
5957 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5958 if (build_id == nullptr)
5959 return {};
5960
5961 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5962 }
5963
5964 /* See symfile.h. */
5965
5966 bool
5967 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5968 {
5969 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5970 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5971
5972 /* If we're about to read full symbols, don't bother with the
5973 indices. In this case we also don't care if some other debug
5974 format is making psymtabs, because they are all about to be
5975 expanded anyway. */
5976 if ((objfile->flags & OBJF_READNOW))
5977 {
5978 /* When using READNOW, the using_index flag (set below) indicates that
5979 PER_BFD was already initialized, when we loaded some other objfile. */
5980 if (per_bfd->using_index)
5981 {
5982 *index_kind = dw_index_kind::GDB_INDEX;
5983 per_objfile->resize_symtabs ();
5984 return true;
5985 }
5986
5987 per_bfd->using_index = 1;
5988 create_all_comp_units (per_objfile);
5989 create_all_type_units (per_objfile);
5990 per_bfd->quick_file_names_table
5991 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5992 per_objfile->resize_symtabs ();
5993
5994 for (int i = 0; i < (per_bfd->all_comp_units.size ()
5995 + per_bfd->all_type_units.size ()); ++i)
5996 {
5997 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
5998
5999 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6000 struct dwarf2_per_cu_quick_data);
6001 }
6002
6003 /* Return 1 so that gdb sees the "quick" functions. However,
6004 these functions will be no-ops because we will have expanded
6005 all symtabs. */
6006 *index_kind = dw_index_kind::GDB_INDEX;
6007 return true;
6008 }
6009
6010 /* Was a debug names index already read when we processed an objfile sharing
6011 PER_BFD? */
6012 if (per_bfd->debug_names_table != nullptr)
6013 {
6014 *index_kind = dw_index_kind::DEBUG_NAMES;
6015 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
6016 per_objfile->resize_symtabs ();
6017 return true;
6018 }
6019
6020 /* Was a GDB index already read when we processed an objfile sharing
6021 PER_BFD? */
6022 if (per_bfd->index_table != nullptr)
6023 {
6024 *index_kind = dw_index_kind::GDB_INDEX;
6025 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
6026 per_objfile->resize_symtabs ();
6027 return true;
6028 }
6029
6030 if (dwarf2_read_debug_names (per_objfile))
6031 {
6032 *index_kind = dw_index_kind::DEBUG_NAMES;
6033 per_objfile->resize_symtabs ();
6034 return true;
6035 }
6036
6037 if (dwarf2_read_gdb_index (per_objfile,
6038 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
6039 get_gdb_index_contents_from_section<dwz_file>))
6040 {
6041 *index_kind = dw_index_kind::GDB_INDEX;
6042 per_objfile->resize_symtabs ();
6043 return true;
6044 }
6045
6046 /* ... otherwise, try to find the index in the index cache. */
6047 if (dwarf2_read_gdb_index (per_objfile,
6048 get_gdb_index_contents_from_cache,
6049 get_gdb_index_contents_from_cache_dwz))
6050 {
6051 global_index_cache.hit ();
6052 *index_kind = dw_index_kind::GDB_INDEX;
6053 per_objfile->resize_symtabs ();
6054 return true;
6055 }
6056
6057 global_index_cache.miss ();
6058 return false;
6059 }
6060
6061 \f
6062
6063 /* Build a partial symbol table. */
6064
6065 void
6066 dwarf2_build_psymtabs (struct objfile *objfile)
6067 {
6068 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6069 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6070
6071 if (per_bfd->partial_symtabs != nullptr)
6072 {
6073 /* Partial symbols were already read, so now we can simply
6074 attach them. */
6075 objfile->partial_symtabs = per_bfd->partial_symtabs;
6076 per_objfile->resize_symtabs ();
6077 return;
6078 }
6079
6080 init_psymbol_list (objfile, 1024);
6081
6082 try
6083 {
6084 /* This isn't really ideal: all the data we allocate on the
6085 objfile's obstack is still uselessly kept around. However,
6086 freeing it seems unsafe. */
6087 psymtab_discarder psymtabs (objfile);
6088 dwarf2_build_psymtabs_hard (per_objfile);
6089 psymtabs.keep ();
6090
6091 per_objfile->resize_symtabs ();
6092
6093 /* (maybe) store an index in the cache. */
6094 global_index_cache.store (per_objfile);
6095 }
6096 catch (const gdb_exception_error &except)
6097 {
6098 exception_print (gdb_stderr, except);
6099 }
6100
6101 /* Finish by setting the local reference to partial symtabs, so that
6102 we don't try to read them again if reading another objfile with the same
6103 BFD. If we can't in fact share, this won't make a difference anyway as
6104 the dwarf2_per_bfd object won't be shared. */
6105 per_bfd->partial_symtabs = objfile->partial_symtabs;
6106 }
6107
6108 /* Find the base address of the compilation unit for range lists and
6109 location lists. It will normally be specified by DW_AT_low_pc.
6110 In DWARF-3 draft 4, the base address could be overridden by
6111 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6112 compilation units with discontinuous ranges. */
6113
6114 static void
6115 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6116 {
6117 struct attribute *attr;
6118
6119 cu->base_address.reset ();
6120
6121 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6122 if (attr != nullptr)
6123 cu->base_address = attr->value_as_address ();
6124 else
6125 {
6126 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6127 if (attr != nullptr)
6128 cu->base_address = attr->value_as_address ();
6129 }
6130 }
6131
6132 /* Helper function that returns the proper abbrev section for
6133 THIS_CU. */
6134
6135 static struct dwarf2_section_info *
6136 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6137 {
6138 struct dwarf2_section_info *abbrev;
6139 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6140
6141 if (this_cu->is_dwz)
6142 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6143 else
6144 abbrev = &per_bfd->abbrev;
6145
6146 return abbrev;
6147 }
6148
6149 /* Fetch the abbreviation table offset from a comp or type unit header. */
6150
6151 static sect_offset
6152 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
6153 struct dwarf2_section_info *section,
6154 sect_offset sect_off)
6155 {
6156 bfd *abfd = section->get_bfd_owner ();
6157 const gdb_byte *info_ptr;
6158 unsigned int initial_length_size, offset_size;
6159 uint16_t version;
6160
6161 section->read (per_objfile->objfile);
6162 info_ptr = section->buffer + to_underlying (sect_off);
6163 read_initial_length (abfd, info_ptr, &initial_length_size);
6164 offset_size = initial_length_size == 4 ? 4 : 8;
6165 info_ptr += initial_length_size;
6166
6167 version = read_2_bytes (abfd, info_ptr);
6168 info_ptr += 2;
6169 if (version >= 5)
6170 {
6171 /* Skip unit type and address size. */
6172 info_ptr += 2;
6173 }
6174
6175 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6176 }
6177
6178 /* A partial symtab that is used only for include files. */
6179 struct dwarf2_include_psymtab : public partial_symtab
6180 {
6181 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6182 : partial_symtab (filename, objfile)
6183 {
6184 }
6185
6186 void read_symtab (struct objfile *objfile) override
6187 {
6188 /* It's an include file, no symbols to read for it.
6189 Everything is in the includer symtab. */
6190
6191 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6192 expansion of the includer psymtab. We use the dependencies[0] field to
6193 model the includer. But if we go the regular route of calling
6194 expand_psymtab here, and having expand_psymtab call expand_dependencies
6195 to expand the includer, we'll only use expand_psymtab on the includer
6196 (making it a non-toplevel psymtab), while if we expand the includer via
6197 another path, we'll use read_symtab (making it a toplevel psymtab).
6198 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6199 psymtab, and trigger read_symtab on the includer here directly. */
6200 includer ()->read_symtab (objfile);
6201 }
6202
6203 void expand_psymtab (struct objfile *objfile) override
6204 {
6205 /* This is not called by read_symtab, and should not be called by any
6206 expand_dependencies. */
6207 gdb_assert (false);
6208 }
6209
6210 bool readin_p (struct objfile *objfile) const override
6211 {
6212 return includer ()->readin_p (objfile);
6213 }
6214
6215 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6216 {
6217 return nullptr;
6218 }
6219
6220 private:
6221 partial_symtab *includer () const
6222 {
6223 /* An include psymtab has exactly one dependency: the psymtab that
6224 includes it. */
6225 gdb_assert (this->number_of_dependencies == 1);
6226 return this->dependencies[0];
6227 }
6228 };
6229
6230 /* Allocate a new partial symtab for file named NAME and mark this new
6231 partial symtab as being an include of PST. */
6232
6233 static void
6234 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6235 struct objfile *objfile)
6236 {
6237 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6238
6239 if (!IS_ABSOLUTE_PATH (subpst->filename))
6240 subpst->dirname = pst->dirname;
6241
6242 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6243 subpst->dependencies[0] = pst;
6244 subpst->number_of_dependencies = 1;
6245 }
6246
6247 /* Read the Line Number Program data and extract the list of files
6248 included by the source file represented by PST. Build an include
6249 partial symtab for each of these included files. */
6250
6251 static void
6252 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6253 struct die_info *die,
6254 dwarf2_psymtab *pst)
6255 {
6256 line_header_up lh;
6257 struct attribute *attr;
6258
6259 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6260 if (attr != nullptr)
6261 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6262 if (lh == NULL)
6263 return; /* No linetable, so no includes. */
6264
6265 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6266 that we pass in the raw text_low here; that is ok because we're
6267 only decoding the line table to make include partial symtabs, and
6268 so the addresses aren't really used. */
6269 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6270 pst->raw_text_low (), 1);
6271 }
6272
6273 static hashval_t
6274 hash_signatured_type (const void *item)
6275 {
6276 const struct signatured_type *sig_type
6277 = (const struct signatured_type *) item;
6278
6279 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6280 return sig_type->signature;
6281 }
6282
6283 static int
6284 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6285 {
6286 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6287 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6288
6289 return lhs->signature == rhs->signature;
6290 }
6291
6292 /* Allocate a hash table for signatured types. */
6293
6294 static htab_up
6295 allocate_signatured_type_table ()
6296 {
6297 return htab_up (htab_create_alloc (41,
6298 hash_signatured_type,
6299 eq_signatured_type,
6300 NULL, xcalloc, xfree));
6301 }
6302
6303 /* A helper function to add a signatured type CU to a table. */
6304
6305 static int
6306 add_signatured_type_cu_to_table (void **slot, void *datum)
6307 {
6308 struct signatured_type *sigt = (struct signatured_type *) *slot;
6309 std::vector<signatured_type *> *all_type_units
6310 = (std::vector<signatured_type *> *) datum;
6311
6312 all_type_units->push_back (sigt);
6313
6314 return 1;
6315 }
6316
6317 /* A helper for create_debug_types_hash_table. Read types from SECTION
6318 and fill them into TYPES_HTAB. It will process only type units,
6319 therefore DW_UT_type. */
6320
6321 static void
6322 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
6323 struct dwo_file *dwo_file,
6324 dwarf2_section_info *section, htab_up &types_htab,
6325 rcuh_kind section_kind)
6326 {
6327 struct objfile *objfile = per_objfile->objfile;
6328 struct dwarf2_section_info *abbrev_section;
6329 bfd *abfd;
6330 const gdb_byte *info_ptr, *end_ptr;
6331
6332 abbrev_section = (dwo_file != NULL
6333 ? &dwo_file->sections.abbrev
6334 : &per_objfile->per_bfd->abbrev);
6335
6336 if (dwarf_read_debug)
6337 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6338 section->get_name (),
6339 abbrev_section->get_file_name ());
6340
6341 section->read (objfile);
6342 info_ptr = section->buffer;
6343
6344 if (info_ptr == NULL)
6345 return;
6346
6347 /* We can't set abfd until now because the section may be empty or
6348 not present, in which case the bfd is unknown. */
6349 abfd = section->get_bfd_owner ();
6350
6351 /* We don't use cutu_reader here because we don't need to read
6352 any dies: the signature is in the header. */
6353
6354 end_ptr = info_ptr + section->size;
6355 while (info_ptr < end_ptr)
6356 {
6357 struct signatured_type *sig_type;
6358 struct dwo_unit *dwo_tu;
6359 void **slot;
6360 const gdb_byte *ptr = info_ptr;
6361 struct comp_unit_head header;
6362 unsigned int length;
6363
6364 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6365
6366 /* Initialize it due to a false compiler warning. */
6367 header.signature = -1;
6368 header.type_cu_offset_in_tu = (cu_offset) -1;
6369
6370 /* We need to read the type's signature in order to build the hash
6371 table, but we don't need anything else just yet. */
6372
6373 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6374 abbrev_section, ptr, section_kind);
6375
6376 length = header.get_length ();
6377
6378 /* Skip dummy type units. */
6379 if (ptr >= info_ptr + length
6380 || peek_abbrev_code (abfd, ptr) == 0
6381 || header.unit_type != DW_UT_type)
6382 {
6383 info_ptr += length;
6384 continue;
6385 }
6386
6387 if (types_htab == NULL)
6388 {
6389 if (dwo_file)
6390 types_htab = allocate_dwo_unit_table ();
6391 else
6392 types_htab = allocate_signatured_type_table ();
6393 }
6394
6395 if (dwo_file)
6396 {
6397 sig_type = NULL;
6398 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6399 dwo_tu->dwo_file = dwo_file;
6400 dwo_tu->signature = header.signature;
6401 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6402 dwo_tu->section = section;
6403 dwo_tu->sect_off = sect_off;
6404 dwo_tu->length = length;
6405 }
6406 else
6407 {
6408 /* N.B.: type_offset is not usable if this type uses a DWO file.
6409 The real type_offset is in the DWO file. */
6410 dwo_tu = NULL;
6411 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6412 sig_type->signature = header.signature;
6413 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6414 sig_type->per_cu.is_debug_types = 1;
6415 sig_type->per_cu.section = section;
6416 sig_type->per_cu.sect_off = sect_off;
6417 sig_type->per_cu.length = length;
6418 }
6419
6420 slot = htab_find_slot (types_htab.get (),
6421 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6422 INSERT);
6423 gdb_assert (slot != NULL);
6424 if (*slot != NULL)
6425 {
6426 sect_offset dup_sect_off;
6427
6428 if (dwo_file)
6429 {
6430 const struct dwo_unit *dup_tu
6431 = (const struct dwo_unit *) *slot;
6432
6433 dup_sect_off = dup_tu->sect_off;
6434 }
6435 else
6436 {
6437 const struct signatured_type *dup_tu
6438 = (const struct signatured_type *) *slot;
6439
6440 dup_sect_off = dup_tu->per_cu.sect_off;
6441 }
6442
6443 complaint (_("debug type entry at offset %s is duplicate to"
6444 " the entry at offset %s, signature %s"),
6445 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6446 hex_string (header.signature));
6447 }
6448 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6449
6450 if (dwarf_read_debug > 1)
6451 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6452 sect_offset_str (sect_off),
6453 hex_string (header.signature));
6454
6455 info_ptr += length;
6456 }
6457 }
6458
6459 /* Create the hash table of all entries in the .debug_types
6460 (or .debug_types.dwo) section(s).
6461 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6462 otherwise it is NULL.
6463
6464 The result is a pointer to the hash table or NULL if there are no types.
6465
6466 Note: This function processes DWO files only, not DWP files. */
6467
6468 static void
6469 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6470 struct dwo_file *dwo_file,
6471 gdb::array_view<dwarf2_section_info> type_sections,
6472 htab_up &types_htab)
6473 {
6474 for (dwarf2_section_info &section : type_sections)
6475 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6476 rcuh_kind::TYPE);
6477 }
6478
6479 /* Create the hash table of all entries in the .debug_types section,
6480 and initialize all_type_units.
6481 The result is zero if there is an error (e.g. missing .debug_types section),
6482 otherwise non-zero. */
6483
6484 static int
6485 create_all_type_units (dwarf2_per_objfile *per_objfile)
6486 {
6487 htab_up types_htab;
6488
6489 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6490 types_htab, rcuh_kind::COMPILE);
6491 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6492 types_htab);
6493 if (types_htab == NULL)
6494 {
6495 per_objfile->per_bfd->signatured_types = NULL;
6496 return 0;
6497 }
6498
6499 per_objfile->per_bfd->signatured_types = std::move (types_htab);
6500
6501 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6502 per_objfile->per_bfd->all_type_units.reserve
6503 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
6504
6505 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
6506 add_signatured_type_cu_to_table,
6507 &per_objfile->per_bfd->all_type_units);
6508
6509 return 1;
6510 }
6511
6512 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6513 If SLOT is non-NULL, it is the entry to use in the hash table.
6514 Otherwise we find one. */
6515
6516 static struct signatured_type *
6517 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6518 {
6519 if (per_objfile->per_bfd->all_type_units.size ()
6520 == per_objfile->per_bfd->all_type_units.capacity ())
6521 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6522
6523 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6524
6525 per_objfile->resize_symtabs ();
6526
6527 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6528 sig_type->signature = sig;
6529 sig_type->per_cu.is_debug_types = 1;
6530 if (per_objfile->per_bfd->using_index)
6531 {
6532 sig_type->per_cu.v.quick =
6533 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6534 struct dwarf2_per_cu_quick_data);
6535 }
6536
6537 if (slot == NULL)
6538 {
6539 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6540 sig_type, INSERT);
6541 }
6542 gdb_assert (*slot == NULL);
6543 *slot = sig_type;
6544 /* The rest of sig_type must be filled in by the caller. */
6545 return sig_type;
6546 }
6547
6548 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6549 Fill in SIG_ENTRY with DWO_ENTRY. */
6550
6551 static void
6552 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6553 struct signatured_type *sig_entry,
6554 struct dwo_unit *dwo_entry)
6555 {
6556 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6557
6558 /* Make sure we're not clobbering something we don't expect to. */
6559 gdb_assert (! sig_entry->per_cu.queued);
6560 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6561 if (per_bfd->using_index)
6562 {
6563 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6564 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6565 }
6566 else
6567 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6568 gdb_assert (sig_entry->signature == dwo_entry->signature);
6569 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6570 gdb_assert (sig_entry->type_unit_group == NULL);
6571 gdb_assert (sig_entry->dwo_unit == NULL);
6572
6573 sig_entry->per_cu.section = dwo_entry->section;
6574 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6575 sig_entry->per_cu.length = dwo_entry->length;
6576 sig_entry->per_cu.reading_dwo_directly = 1;
6577 sig_entry->per_cu.per_bfd = per_bfd;
6578 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6579 sig_entry->dwo_unit = dwo_entry;
6580 }
6581
6582 /* Subroutine of lookup_signatured_type.
6583 If we haven't read the TU yet, create the signatured_type data structure
6584 for a TU to be read in directly from a DWO file, bypassing the stub.
6585 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6586 using .gdb_index, then when reading a CU we want to stay in the DWO file
6587 containing that CU. Otherwise we could end up reading several other DWO
6588 files (due to comdat folding) to process the transitive closure of all the
6589 mentioned TUs, and that can be slow. The current DWO file will have every
6590 type signature that it needs.
6591 We only do this for .gdb_index because in the psymtab case we already have
6592 to read all the DWOs to build the type unit groups. */
6593
6594 static struct signatured_type *
6595 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6596 {
6597 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6598 struct dwo_file *dwo_file;
6599 struct dwo_unit find_dwo_entry, *dwo_entry;
6600 struct signatured_type find_sig_entry, *sig_entry;
6601 void **slot;
6602
6603 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6604
6605 /* If TU skeletons have been removed then we may not have read in any
6606 TUs yet. */
6607 if (per_objfile->per_bfd->signatured_types == NULL)
6608 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6609
6610 /* We only ever need to read in one copy of a signatured type.
6611 Use the global signatured_types array to do our own comdat-folding
6612 of types. If this is the first time we're reading this TU, and
6613 the TU has an entry in .gdb_index, replace the recorded data from
6614 .gdb_index with this TU. */
6615
6616 find_sig_entry.signature = sig;
6617 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6618 &find_sig_entry, INSERT);
6619 sig_entry = (struct signatured_type *) *slot;
6620
6621 /* We can get here with the TU already read, *or* in the process of being
6622 read. Don't reassign the global entry to point to this DWO if that's
6623 the case. Also note that if the TU is already being read, it may not
6624 have come from a DWO, the program may be a mix of Fission-compiled
6625 code and non-Fission-compiled code. */
6626
6627 /* Have we already tried to read this TU?
6628 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6629 needn't exist in the global table yet). */
6630 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6631 return sig_entry;
6632
6633 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6634 dwo_unit of the TU itself. */
6635 dwo_file = cu->dwo_unit->dwo_file;
6636
6637 /* Ok, this is the first time we're reading this TU. */
6638 if (dwo_file->tus == NULL)
6639 return NULL;
6640 find_dwo_entry.signature = sig;
6641 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6642 &find_dwo_entry);
6643 if (dwo_entry == NULL)
6644 return NULL;
6645
6646 /* If the global table doesn't have an entry for this TU, add one. */
6647 if (sig_entry == NULL)
6648 sig_entry = add_type_unit (per_objfile, sig, slot);
6649
6650 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6651 sig_entry->per_cu.tu_read = 1;
6652 return sig_entry;
6653 }
6654
6655 /* Subroutine of lookup_signatured_type.
6656 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6657 then try the DWP file. If the TU stub (skeleton) has been removed then
6658 it won't be in .gdb_index. */
6659
6660 static struct signatured_type *
6661 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6662 {
6663 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6664 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6665 struct dwo_unit *dwo_entry;
6666 struct signatured_type find_sig_entry, *sig_entry;
6667 void **slot;
6668
6669 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6670 gdb_assert (dwp_file != NULL);
6671
6672 /* If TU skeletons have been removed then we may not have read in any
6673 TUs yet. */
6674 if (per_objfile->per_bfd->signatured_types == NULL)
6675 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6676
6677 find_sig_entry.signature = sig;
6678 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6679 &find_sig_entry, INSERT);
6680 sig_entry = (struct signatured_type *) *slot;
6681
6682 /* Have we already tried to read this TU?
6683 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6684 needn't exist in the global table yet). */
6685 if (sig_entry != NULL)
6686 return sig_entry;
6687
6688 if (dwp_file->tus == NULL)
6689 return NULL;
6690 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6691 1 /* is_debug_types */);
6692 if (dwo_entry == NULL)
6693 return NULL;
6694
6695 sig_entry = add_type_unit (per_objfile, sig, slot);
6696 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6697
6698 return sig_entry;
6699 }
6700
6701 /* Lookup a signature based type for DW_FORM_ref_sig8.
6702 Returns NULL if signature SIG is not present in the table.
6703 It is up to the caller to complain about this. */
6704
6705 static struct signatured_type *
6706 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6707 {
6708 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6709
6710 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6711 {
6712 /* We're in a DWO/DWP file, and we're using .gdb_index.
6713 These cases require special processing. */
6714 if (get_dwp_file (per_objfile) == NULL)
6715 return lookup_dwo_signatured_type (cu, sig);
6716 else
6717 return lookup_dwp_signatured_type (cu, sig);
6718 }
6719 else
6720 {
6721 struct signatured_type find_entry, *entry;
6722
6723 if (per_objfile->per_bfd->signatured_types == NULL)
6724 return NULL;
6725 find_entry.signature = sig;
6726 entry = ((struct signatured_type *)
6727 htab_find (per_objfile->per_bfd->signatured_types.get (),
6728 &find_entry));
6729 return entry;
6730 }
6731 }
6732
6733 /* Low level DIE reading support. */
6734
6735 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6736
6737 static void
6738 init_cu_die_reader (struct die_reader_specs *reader,
6739 struct dwarf2_cu *cu,
6740 struct dwarf2_section_info *section,
6741 struct dwo_file *dwo_file,
6742 struct abbrev_table *abbrev_table)
6743 {
6744 gdb_assert (section->readin && section->buffer != NULL);
6745 reader->abfd = section->get_bfd_owner ();
6746 reader->cu = cu;
6747 reader->dwo_file = dwo_file;
6748 reader->die_section = section;
6749 reader->buffer = section->buffer;
6750 reader->buffer_end = section->buffer + section->size;
6751 reader->abbrev_table = abbrev_table;
6752 }
6753
6754 /* Subroutine of cutu_reader to simplify it.
6755 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6756 There's just a lot of work to do, and cutu_reader is big enough
6757 already.
6758
6759 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6760 from it to the DIE in the DWO. If NULL we are skipping the stub.
6761 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6762 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6763 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6764 STUB_COMP_DIR may be non-NULL.
6765 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6766 are filled in with the info of the DIE from the DWO file.
6767 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6768 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6769 kept around for at least as long as *RESULT_READER.
6770
6771 The result is non-zero if a valid (non-dummy) DIE was found. */
6772
6773 static int
6774 read_cutu_die_from_dwo (dwarf2_cu *cu,
6775 struct dwo_unit *dwo_unit,
6776 struct die_info *stub_comp_unit_die,
6777 const char *stub_comp_dir,
6778 struct die_reader_specs *result_reader,
6779 const gdb_byte **result_info_ptr,
6780 struct die_info **result_comp_unit_die,
6781 abbrev_table_up *result_dwo_abbrev_table)
6782 {
6783 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6784 dwarf2_per_cu_data *per_cu = cu->per_cu;
6785 struct objfile *objfile = per_objfile->objfile;
6786 bfd *abfd;
6787 const gdb_byte *begin_info_ptr, *info_ptr;
6788 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6789 int i,num_extra_attrs;
6790 struct dwarf2_section_info *dwo_abbrev_section;
6791 struct die_info *comp_unit_die;
6792
6793 /* At most one of these may be provided. */
6794 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6795
6796 /* These attributes aren't processed until later:
6797 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6798 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6799 referenced later. However, these attributes are found in the stub
6800 which we won't have later. In order to not impose this complication
6801 on the rest of the code, we read them here and copy them to the
6802 DWO CU/TU die. */
6803
6804 stmt_list = NULL;
6805 low_pc = NULL;
6806 high_pc = NULL;
6807 ranges = NULL;
6808 comp_dir = NULL;
6809
6810 if (stub_comp_unit_die != NULL)
6811 {
6812 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6813 DWO file. */
6814 if (!per_cu->is_debug_types)
6815 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6816 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6817 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6818 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6819 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6820
6821 cu->addr_base = stub_comp_unit_die->addr_base ();
6822
6823 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6824 here (if needed). We need the value before we can process
6825 DW_AT_ranges. */
6826 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6827 }
6828 else if (stub_comp_dir != NULL)
6829 {
6830 /* Reconstruct the comp_dir attribute to simplify the code below. */
6831 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6832 comp_dir->name = DW_AT_comp_dir;
6833 comp_dir->form = DW_FORM_string;
6834 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6835 DW_STRING (comp_dir) = stub_comp_dir;
6836 }
6837
6838 /* Set up for reading the DWO CU/TU. */
6839 cu->dwo_unit = dwo_unit;
6840 dwarf2_section_info *section = dwo_unit->section;
6841 section->read (objfile);
6842 abfd = section->get_bfd_owner ();
6843 begin_info_ptr = info_ptr = (section->buffer
6844 + to_underlying (dwo_unit->sect_off));
6845 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6846
6847 if (per_cu->is_debug_types)
6848 {
6849 signatured_type *sig_type = (struct signatured_type *) per_cu;
6850
6851 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6852 section, dwo_abbrev_section,
6853 info_ptr, rcuh_kind::TYPE);
6854 /* This is not an assert because it can be caused by bad debug info. */
6855 if (sig_type->signature != cu->header.signature)
6856 {
6857 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6858 " TU at offset %s [in module %s]"),
6859 hex_string (sig_type->signature),
6860 hex_string (cu->header.signature),
6861 sect_offset_str (dwo_unit->sect_off),
6862 bfd_get_filename (abfd));
6863 }
6864 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6865 /* For DWOs coming from DWP files, we don't know the CU length
6866 nor the type's offset in the TU until now. */
6867 dwo_unit->length = cu->header.get_length ();
6868 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6869
6870 /* Establish the type offset that can be used to lookup the type.
6871 For DWO files, we don't know it until now. */
6872 sig_type->type_offset_in_section
6873 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6874 }
6875 else
6876 {
6877 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6878 section, dwo_abbrev_section,
6879 info_ptr, rcuh_kind::COMPILE);
6880 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6881 /* For DWOs coming from DWP files, we don't know the CU length
6882 until now. */
6883 dwo_unit->length = cu->header.get_length ();
6884 }
6885
6886 *result_dwo_abbrev_table
6887 = abbrev_table::read (objfile, dwo_abbrev_section,
6888 cu->header.abbrev_sect_off);
6889 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6890 result_dwo_abbrev_table->get ());
6891
6892 /* Read in the die, but leave space to copy over the attributes
6893 from the stub. This has the benefit of simplifying the rest of
6894 the code - all the work to maintain the illusion of a single
6895 DW_TAG_{compile,type}_unit DIE is done here. */
6896 num_extra_attrs = ((stmt_list != NULL)
6897 + (low_pc != NULL)
6898 + (high_pc != NULL)
6899 + (ranges != NULL)
6900 + (comp_dir != NULL));
6901 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6902 num_extra_attrs);
6903
6904 /* Copy over the attributes from the stub to the DIE we just read in. */
6905 comp_unit_die = *result_comp_unit_die;
6906 i = comp_unit_die->num_attrs;
6907 if (stmt_list != NULL)
6908 comp_unit_die->attrs[i++] = *stmt_list;
6909 if (low_pc != NULL)
6910 comp_unit_die->attrs[i++] = *low_pc;
6911 if (high_pc != NULL)
6912 comp_unit_die->attrs[i++] = *high_pc;
6913 if (ranges != NULL)
6914 comp_unit_die->attrs[i++] = *ranges;
6915 if (comp_dir != NULL)
6916 comp_unit_die->attrs[i++] = *comp_dir;
6917 comp_unit_die->num_attrs += num_extra_attrs;
6918
6919 if (dwarf_die_debug)
6920 {
6921 fprintf_unfiltered (gdb_stdlog,
6922 "Read die from %s@0x%x of %s:\n",
6923 section->get_name (),
6924 (unsigned) (begin_info_ptr - section->buffer),
6925 bfd_get_filename (abfd));
6926 dump_die (comp_unit_die, dwarf_die_debug);
6927 }
6928
6929 /* Skip dummy compilation units. */
6930 if (info_ptr >= begin_info_ptr + dwo_unit->length
6931 || peek_abbrev_code (abfd, info_ptr) == 0)
6932 return 0;
6933
6934 *result_info_ptr = info_ptr;
6935 return 1;
6936 }
6937
6938 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6939 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6940 signature is part of the header. */
6941 static gdb::optional<ULONGEST>
6942 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6943 {
6944 if (cu->header.version >= 5)
6945 return cu->header.signature;
6946 struct attribute *attr;
6947 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6948 if (attr == nullptr)
6949 return gdb::optional<ULONGEST> ();
6950 return DW_UNSND (attr);
6951 }
6952
6953 /* Subroutine of cutu_reader to simplify it.
6954 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6955 Returns NULL if the specified DWO unit cannot be found. */
6956
6957 static struct dwo_unit *
6958 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6959 {
6960 dwarf2_per_cu_data *per_cu = cu->per_cu;
6961 struct dwo_unit *dwo_unit;
6962 const char *comp_dir;
6963
6964 gdb_assert (cu != NULL);
6965
6966 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6967 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6968 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6969
6970 if (per_cu->is_debug_types)
6971 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6972 else
6973 {
6974 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6975
6976 if (!signature.has_value ())
6977 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6978 " [in module %s]"),
6979 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6980
6981 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6982 }
6983
6984 return dwo_unit;
6985 }
6986
6987 /* Subroutine of cutu_reader to simplify it.
6988 See it for a description of the parameters.
6989 Read a TU directly from a DWO file, bypassing the stub. */
6990
6991 void
6992 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6993 dwarf2_per_objfile *per_objfile,
6994 dwarf2_cu *existing_cu)
6995 {
6996 struct signatured_type *sig_type;
6997
6998 /* Verify we can do the following downcast, and that we have the
6999 data we need. */
7000 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7001 sig_type = (struct signatured_type *) this_cu;
7002 gdb_assert (sig_type->dwo_unit != NULL);
7003
7004 dwarf2_cu *cu;
7005
7006 if (existing_cu != nullptr)
7007 {
7008 cu = existing_cu;
7009 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
7010 /* There's no need to do the rereading_dwo_cu handling that
7011 cutu_reader does since we don't read the stub. */
7012 }
7013 else
7014 {
7015 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7016 in per_objfile yet. */
7017 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7018 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7019 cu = m_new_cu.get ();
7020 }
7021
7022 /* A future optimization, if needed, would be to use an existing
7023 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7024 could share abbrev tables. */
7025
7026 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
7027 NULL /* stub_comp_unit_die */,
7028 sig_type->dwo_unit->dwo_file->comp_dir,
7029 this, &info_ptr,
7030 &comp_unit_die,
7031 &m_dwo_abbrev_table) == 0)
7032 {
7033 /* Dummy die. */
7034 dummy_p = true;
7035 }
7036 }
7037
7038 /* Initialize a CU (or TU) and read its DIEs.
7039 If the CU defers to a DWO file, read the DWO file as well.
7040
7041 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7042 Otherwise the table specified in the comp unit header is read in and used.
7043 This is an optimization for when we already have the abbrev table.
7044
7045 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7046 allocated. */
7047
7048 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7049 dwarf2_per_objfile *per_objfile,
7050 struct abbrev_table *abbrev_table,
7051 dwarf2_cu *existing_cu,
7052 bool skip_partial)
7053 : die_reader_specs {},
7054 m_this_cu (this_cu)
7055 {
7056 struct objfile *objfile = per_objfile->objfile;
7057 struct dwarf2_section_info *section = this_cu->section;
7058 bfd *abfd = section->get_bfd_owner ();
7059 const gdb_byte *begin_info_ptr;
7060 struct signatured_type *sig_type = NULL;
7061 struct dwarf2_section_info *abbrev_section;
7062 /* Non-zero if CU currently points to a DWO file and we need to
7063 reread it. When this happens we need to reread the skeleton die
7064 before we can reread the DWO file (this only applies to CUs, not TUs). */
7065 int rereading_dwo_cu = 0;
7066
7067 if (dwarf_die_debug)
7068 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7069 this_cu->is_debug_types ? "type" : "comp",
7070 sect_offset_str (this_cu->sect_off));
7071
7072 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7073 file (instead of going through the stub), short-circuit all of this. */
7074 if (this_cu->reading_dwo_directly)
7075 {
7076 /* Narrow down the scope of possibilities to have to understand. */
7077 gdb_assert (this_cu->is_debug_types);
7078 gdb_assert (abbrev_table == NULL);
7079 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
7080 return;
7081 }
7082
7083 /* This is cheap if the section is already read in. */
7084 section->read (objfile);
7085
7086 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7087
7088 abbrev_section = get_abbrev_section_for_cu (this_cu);
7089
7090 dwarf2_cu *cu;
7091
7092 if (existing_cu != nullptr)
7093 {
7094 cu = existing_cu;
7095 /* If this CU is from a DWO file we need to start over, we need to
7096 refetch the attributes from the skeleton CU.
7097 This could be optimized by retrieving those attributes from when we
7098 were here the first time: the previous comp_unit_die was stored in
7099 comp_unit_obstack. But there's no data yet that we need this
7100 optimization. */
7101 if (cu->dwo_unit != NULL)
7102 rereading_dwo_cu = 1;
7103 }
7104 else
7105 {
7106 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7107 in per_objfile yet. */
7108 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7109 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7110 cu = m_new_cu.get ();
7111 }
7112
7113 /* Get the header. */
7114 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7115 {
7116 /* We already have the header, there's no need to read it in again. */
7117 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7118 }
7119 else
7120 {
7121 if (this_cu->is_debug_types)
7122 {
7123 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7124 section, abbrev_section,
7125 info_ptr, rcuh_kind::TYPE);
7126
7127 /* Since per_cu is the first member of struct signatured_type,
7128 we can go from a pointer to one to a pointer to the other. */
7129 sig_type = (struct signatured_type *) this_cu;
7130 gdb_assert (sig_type->signature == cu->header.signature);
7131 gdb_assert (sig_type->type_offset_in_tu
7132 == cu->header.type_cu_offset_in_tu);
7133 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7134
7135 /* LENGTH has not been set yet for type units if we're
7136 using .gdb_index. */
7137 this_cu->length = cu->header.get_length ();
7138
7139 /* Establish the type offset that can be used to lookup the type. */
7140 sig_type->type_offset_in_section =
7141 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7142
7143 this_cu->dwarf_version = cu->header.version;
7144 }
7145 else
7146 {
7147 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7148 section, abbrev_section,
7149 info_ptr,
7150 rcuh_kind::COMPILE);
7151
7152 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7153 if (this_cu->length == 0)
7154 this_cu->length = cu->header.get_length ();
7155 else
7156 gdb_assert (this_cu->length == cu->header.get_length ());
7157 this_cu->dwarf_version = cu->header.version;
7158 }
7159 }
7160
7161 /* Skip dummy compilation units. */
7162 if (info_ptr >= begin_info_ptr + this_cu->length
7163 || peek_abbrev_code (abfd, info_ptr) == 0)
7164 {
7165 dummy_p = true;
7166 return;
7167 }
7168
7169 /* If we don't have them yet, read the abbrevs for this compilation unit.
7170 And if we need to read them now, make sure they're freed when we're
7171 done. */
7172 if (abbrev_table != NULL)
7173 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7174 else
7175 {
7176 m_abbrev_table_holder
7177 = abbrev_table::read (objfile, abbrev_section,
7178 cu->header.abbrev_sect_off);
7179 abbrev_table = m_abbrev_table_holder.get ();
7180 }
7181
7182 /* Read the top level CU/TU die. */
7183 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7184 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7185
7186 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7187 {
7188 dummy_p = true;
7189 return;
7190 }
7191
7192 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7193 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7194 table from the DWO file and pass the ownership over to us. It will be
7195 referenced from READER, so we must make sure to free it after we're done
7196 with READER.
7197
7198 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7199 DWO CU, that this test will fail (the attribute will not be present). */
7200 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7201 if (dwo_name != nullptr)
7202 {
7203 struct dwo_unit *dwo_unit;
7204 struct die_info *dwo_comp_unit_die;
7205
7206 if (comp_unit_die->has_children)
7207 {
7208 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7209 " has children (offset %s) [in module %s]"),
7210 sect_offset_str (this_cu->sect_off),
7211 bfd_get_filename (abfd));
7212 }
7213 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7214 if (dwo_unit != NULL)
7215 {
7216 if (read_cutu_die_from_dwo (cu, dwo_unit,
7217 comp_unit_die, NULL,
7218 this, &info_ptr,
7219 &dwo_comp_unit_die,
7220 &m_dwo_abbrev_table) == 0)
7221 {
7222 /* Dummy die. */
7223 dummy_p = true;
7224 return;
7225 }
7226 comp_unit_die = dwo_comp_unit_die;
7227 }
7228 else
7229 {
7230 /* Yikes, we couldn't find the rest of the DIE, we only have
7231 the stub. A complaint has already been logged. There's
7232 not much more we can do except pass on the stub DIE to
7233 die_reader_func. We don't want to throw an error on bad
7234 debug info. */
7235 }
7236 }
7237 }
7238
7239 void
7240 cutu_reader::keep ()
7241 {
7242 /* Done, clean up. */
7243 gdb_assert (!dummy_p);
7244 if (m_new_cu != NULL)
7245 {
7246 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7247 now. */
7248 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7249 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7250 }
7251 }
7252
7253 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7254 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7255 assumed to have already done the lookup to find the DWO file).
7256
7257 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7258 THIS_CU->is_debug_types, but nothing else.
7259
7260 We fill in THIS_CU->length.
7261
7262 THIS_CU->cu is always freed when done.
7263 This is done in order to not leave THIS_CU->cu in a state where we have
7264 to care whether it refers to the "main" CU or the DWO CU.
7265
7266 When parent_cu is passed, it is used to provide a default value for
7267 str_offsets_base and addr_base from the parent. */
7268
7269 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7270 dwarf2_per_objfile *per_objfile,
7271 struct dwarf2_cu *parent_cu,
7272 struct dwo_file *dwo_file)
7273 : die_reader_specs {},
7274 m_this_cu (this_cu)
7275 {
7276 struct objfile *objfile = per_objfile->objfile;
7277 struct dwarf2_section_info *section = this_cu->section;
7278 bfd *abfd = section->get_bfd_owner ();
7279 struct dwarf2_section_info *abbrev_section;
7280 const gdb_byte *begin_info_ptr, *info_ptr;
7281
7282 if (dwarf_die_debug)
7283 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7284 this_cu->is_debug_types ? "type" : "comp",
7285 sect_offset_str (this_cu->sect_off));
7286
7287 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7288
7289 abbrev_section = (dwo_file != NULL
7290 ? &dwo_file->sections.abbrev
7291 : get_abbrev_section_for_cu (this_cu));
7292
7293 /* This is cheap if the section is already read in. */
7294 section->read (objfile);
7295
7296 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7297
7298 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7299 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7300 section, abbrev_section, info_ptr,
7301 (this_cu->is_debug_types
7302 ? rcuh_kind::TYPE
7303 : rcuh_kind::COMPILE));
7304
7305 if (parent_cu != nullptr)
7306 {
7307 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7308 m_new_cu->addr_base = parent_cu->addr_base;
7309 }
7310 this_cu->length = m_new_cu->header.get_length ();
7311
7312 /* Skip dummy compilation units. */
7313 if (info_ptr >= begin_info_ptr + this_cu->length
7314 || peek_abbrev_code (abfd, info_ptr) == 0)
7315 {
7316 dummy_p = true;
7317 return;
7318 }
7319
7320 m_abbrev_table_holder
7321 = abbrev_table::read (objfile, abbrev_section,
7322 m_new_cu->header.abbrev_sect_off);
7323
7324 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7325 m_abbrev_table_holder.get ());
7326 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7327 }
7328
7329 \f
7330 /* Type Unit Groups.
7331
7332 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7333 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7334 so that all types coming from the same compilation (.o file) are grouped
7335 together. A future step could be to put the types in the same symtab as
7336 the CU the types ultimately came from. */
7337
7338 static hashval_t
7339 hash_type_unit_group (const void *item)
7340 {
7341 const struct type_unit_group *tu_group
7342 = (const struct type_unit_group *) item;
7343
7344 return hash_stmt_list_entry (&tu_group->hash);
7345 }
7346
7347 static int
7348 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7349 {
7350 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7351 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7352
7353 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7354 }
7355
7356 /* Allocate a hash table for type unit groups. */
7357
7358 static htab_up
7359 allocate_type_unit_groups_table ()
7360 {
7361 return htab_up (htab_create_alloc (3,
7362 hash_type_unit_group,
7363 eq_type_unit_group,
7364 NULL, xcalloc, xfree));
7365 }
7366
7367 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7368 partial symtabs. We combine several TUs per psymtab to not let the size
7369 of any one psymtab grow too big. */
7370 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7371 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7372
7373 /* Helper routine for get_type_unit_group.
7374 Create the type_unit_group object used to hold one or more TUs. */
7375
7376 static struct type_unit_group *
7377 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7378 {
7379 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7380 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7381 struct dwarf2_per_cu_data *per_cu;
7382 struct type_unit_group *tu_group;
7383
7384 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
7385 per_cu = &tu_group->per_cu;
7386 per_cu->per_bfd = per_bfd;
7387
7388 if (per_bfd->using_index)
7389 {
7390 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7391 struct dwarf2_per_cu_quick_data);
7392 }
7393 else
7394 {
7395 unsigned int line_offset = to_underlying (line_offset_struct);
7396 dwarf2_psymtab *pst;
7397 std::string name;
7398
7399 /* Give the symtab a useful name for debug purposes. */
7400 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7401 name = string_printf ("<type_units_%d>",
7402 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7403 else
7404 name = string_printf ("<type_units_at_0x%x>", line_offset);
7405
7406 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
7407 pst->anonymous = true;
7408 }
7409
7410 tu_group->hash.dwo_unit = cu->dwo_unit;
7411 tu_group->hash.line_sect_off = line_offset_struct;
7412
7413 return tu_group;
7414 }
7415
7416 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7417 STMT_LIST is a DW_AT_stmt_list attribute. */
7418
7419 static struct type_unit_group *
7420 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7421 {
7422 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7423 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7424 struct type_unit_group *tu_group;
7425 void **slot;
7426 unsigned int line_offset;
7427 struct type_unit_group type_unit_group_for_lookup;
7428
7429 if (per_objfile->per_bfd->type_unit_groups == NULL)
7430 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7431
7432 /* Do we need to create a new group, or can we use an existing one? */
7433
7434 if (stmt_list)
7435 {
7436 line_offset = DW_UNSND (stmt_list);
7437 ++tu_stats->nr_symtab_sharers;
7438 }
7439 else
7440 {
7441 /* Ugh, no stmt_list. Rare, but we have to handle it.
7442 We can do various things here like create one group per TU or
7443 spread them over multiple groups to split up the expansion work.
7444 To avoid worst case scenarios (too many groups or too large groups)
7445 we, umm, group them in bunches. */
7446 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7447 | (tu_stats->nr_stmt_less_type_units
7448 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7449 ++tu_stats->nr_stmt_less_type_units;
7450 }
7451
7452 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7453 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7454 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7455 &type_unit_group_for_lookup, INSERT);
7456 if (*slot != NULL)
7457 {
7458 tu_group = (struct type_unit_group *) *slot;
7459 gdb_assert (tu_group != NULL);
7460 }
7461 else
7462 {
7463 sect_offset line_offset_struct = (sect_offset) line_offset;
7464 tu_group = create_type_unit_group (cu, line_offset_struct);
7465 *slot = tu_group;
7466 ++tu_stats->nr_symtabs;
7467 }
7468
7469 return tu_group;
7470 }
7471 \f
7472 /* Partial symbol tables. */
7473
7474 /* Create a psymtab named NAME and assign it to PER_CU.
7475
7476 The caller must fill in the following details:
7477 dirname, textlow, texthigh. */
7478
7479 static dwarf2_psymtab *
7480 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7481 dwarf2_per_objfile *per_objfile,
7482 const char *name)
7483 {
7484 struct objfile *objfile = per_objfile->objfile;
7485 dwarf2_psymtab *pst;
7486
7487 pst = new dwarf2_psymtab (name, objfile, per_cu);
7488
7489 pst->psymtabs_addrmap_supported = true;
7490
7491 /* This is the glue that links PST into GDB's symbol API. */
7492 per_cu->v.psymtab = pst;
7493
7494 return pst;
7495 }
7496
7497 /* DIE reader function for process_psymtab_comp_unit. */
7498
7499 static void
7500 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7501 const gdb_byte *info_ptr,
7502 struct die_info *comp_unit_die,
7503 enum language pretend_language)
7504 {
7505 struct dwarf2_cu *cu = reader->cu;
7506 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7507 struct objfile *objfile = per_objfile->objfile;
7508 struct gdbarch *gdbarch = objfile->arch ();
7509 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7510 CORE_ADDR baseaddr;
7511 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7512 dwarf2_psymtab *pst;
7513 enum pc_bounds_kind cu_bounds_kind;
7514 const char *filename;
7515
7516 gdb_assert (! per_cu->is_debug_types);
7517
7518 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7519
7520 /* Allocate a new partial symbol table structure. */
7521 gdb::unique_xmalloc_ptr<char> debug_filename;
7522 static const char artificial[] = "<artificial>";
7523 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7524 if (filename == NULL)
7525 filename = "";
7526 else if (strcmp (filename, artificial) == 0)
7527 {
7528 debug_filename.reset (concat (artificial, "@",
7529 sect_offset_str (per_cu->sect_off),
7530 (char *) NULL));
7531 filename = debug_filename.get ();
7532 }
7533
7534 pst = create_partial_symtab (per_cu, per_objfile, filename);
7535
7536 /* This must be done before calling dwarf2_build_include_psymtabs. */
7537 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7538
7539 baseaddr = objfile->text_section_offset ();
7540
7541 dwarf2_find_base_address (comp_unit_die, cu);
7542
7543 /* Possibly set the default values of LOWPC and HIGHPC from
7544 `DW_AT_ranges'. */
7545 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7546 &best_highpc, cu, pst);
7547 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7548 {
7549 CORE_ADDR low
7550 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7551 - baseaddr);
7552 CORE_ADDR high
7553 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7554 - baseaddr - 1);
7555 /* Store the contiguous range if it is not empty; it can be
7556 empty for CUs with no code. */
7557 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7558 low, high, pst);
7559 }
7560
7561 /* Check if comp unit has_children.
7562 If so, read the rest of the partial symbols from this comp unit.
7563 If not, there's no more debug_info for this comp unit. */
7564 if (comp_unit_die->has_children)
7565 {
7566 struct partial_die_info *first_die;
7567 CORE_ADDR lowpc, highpc;
7568
7569 lowpc = ((CORE_ADDR) -1);
7570 highpc = ((CORE_ADDR) 0);
7571
7572 first_die = load_partial_dies (reader, info_ptr, 1);
7573
7574 scan_partial_symbols (first_die, &lowpc, &highpc,
7575 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7576
7577 /* If we didn't find a lowpc, set it to highpc to avoid
7578 complaints from `maint check'. */
7579 if (lowpc == ((CORE_ADDR) -1))
7580 lowpc = highpc;
7581
7582 /* If the compilation unit didn't have an explicit address range,
7583 then use the information extracted from its child dies. */
7584 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7585 {
7586 best_lowpc = lowpc;
7587 best_highpc = highpc;
7588 }
7589 }
7590 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7591 best_lowpc + baseaddr)
7592 - baseaddr);
7593 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7594 best_highpc + baseaddr)
7595 - baseaddr);
7596
7597 end_psymtab_common (objfile, pst);
7598
7599 if (!cu->per_cu->imported_symtabs_empty ())
7600 {
7601 int i;
7602 int len = cu->per_cu->imported_symtabs_size ();
7603
7604 /* Fill in 'dependencies' here; we fill in 'users' in a
7605 post-pass. */
7606 pst->number_of_dependencies = len;
7607 pst->dependencies
7608 = objfile->partial_symtabs->allocate_dependencies (len);
7609 for (i = 0; i < len; ++i)
7610 {
7611 pst->dependencies[i]
7612 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7613 }
7614
7615 cu->per_cu->imported_symtabs_free ();
7616 }
7617
7618 /* Get the list of files included in the current compilation unit,
7619 and build a psymtab for each of them. */
7620 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7621
7622 if (dwarf_read_debug)
7623 fprintf_unfiltered (gdb_stdlog,
7624 "Psymtab for %s unit @%s: %s - %s"
7625 ", %d global, %d static syms\n",
7626 per_cu->is_debug_types ? "type" : "comp",
7627 sect_offset_str (per_cu->sect_off),
7628 paddress (gdbarch, pst->text_low (objfile)),
7629 paddress (gdbarch, pst->text_high (objfile)),
7630 pst->n_global_syms, pst->n_static_syms);
7631 }
7632
7633 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7634 Process compilation unit THIS_CU for a psymtab. */
7635
7636 static void
7637 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7638 dwarf2_per_objfile *per_objfile,
7639 bool want_partial_unit,
7640 enum language pretend_language)
7641 {
7642 /* If this compilation unit was already read in, free the
7643 cached copy in order to read it in again. This is
7644 necessary because we skipped some symbols when we first
7645 read in the compilation unit (see load_partial_dies).
7646 This problem could be avoided, but the benefit is unclear. */
7647 per_objfile->remove_cu (this_cu);
7648
7649 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7650
7651 switch (reader.comp_unit_die->tag)
7652 {
7653 case DW_TAG_compile_unit:
7654 this_cu->unit_type = DW_UT_compile;
7655 break;
7656 case DW_TAG_partial_unit:
7657 this_cu->unit_type = DW_UT_partial;
7658 break;
7659 default:
7660 abort ();
7661 }
7662
7663 if (reader.dummy_p)
7664 {
7665 /* Nothing. */
7666 }
7667 else if (this_cu->is_debug_types)
7668 build_type_psymtabs_reader (&reader, reader.info_ptr,
7669 reader.comp_unit_die);
7670 else if (want_partial_unit
7671 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7672 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7673 reader.comp_unit_die,
7674 pretend_language);
7675
7676 this_cu->lang = reader.cu->language;
7677
7678 /* Age out any secondary CUs. */
7679 per_objfile->age_comp_units ();
7680 }
7681
7682 /* Reader function for build_type_psymtabs. */
7683
7684 static void
7685 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7686 const gdb_byte *info_ptr,
7687 struct die_info *type_unit_die)
7688 {
7689 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7690 struct objfile *objfile = per_objfile->objfile;
7691 struct dwarf2_cu *cu = reader->cu;
7692 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7693 struct signatured_type *sig_type;
7694 struct type_unit_group *tu_group;
7695 struct attribute *attr;
7696 struct partial_die_info *first_die;
7697 CORE_ADDR lowpc, highpc;
7698 dwarf2_psymtab *pst;
7699
7700 gdb_assert (per_cu->is_debug_types);
7701 sig_type = (struct signatured_type *) per_cu;
7702
7703 if (! type_unit_die->has_children)
7704 return;
7705
7706 attr = type_unit_die->attr (DW_AT_stmt_list);
7707 tu_group = get_type_unit_group (cu, attr);
7708
7709 if (tu_group->tus == nullptr)
7710 tu_group->tus = new std::vector<signatured_type *>;
7711 tu_group->tus->push_back (sig_type);
7712
7713 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7714 pst = create_partial_symtab (per_cu, per_objfile, "");
7715 pst->anonymous = true;
7716
7717 first_die = load_partial_dies (reader, info_ptr, 1);
7718
7719 lowpc = (CORE_ADDR) -1;
7720 highpc = (CORE_ADDR) 0;
7721 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7722
7723 end_psymtab_common (objfile, pst);
7724 }
7725
7726 /* Struct used to sort TUs by their abbreviation table offset. */
7727
7728 struct tu_abbrev_offset
7729 {
7730 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7731 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7732 {}
7733
7734 signatured_type *sig_type;
7735 sect_offset abbrev_offset;
7736 };
7737
7738 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7739
7740 static bool
7741 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7742 const struct tu_abbrev_offset &b)
7743 {
7744 return a.abbrev_offset < b.abbrev_offset;
7745 }
7746
7747 /* Efficiently read all the type units.
7748 This does the bulk of the work for build_type_psymtabs.
7749
7750 The efficiency is because we sort TUs by the abbrev table they use and
7751 only read each abbrev table once. In one program there are 200K TUs
7752 sharing 8K abbrev tables.
7753
7754 The main purpose of this function is to support building the
7755 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7756 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7757 can collapse the search space by grouping them by stmt_list.
7758 The savings can be significant, in the same program from above the 200K TUs
7759 share 8K stmt_list tables.
7760
7761 FUNC is expected to call get_type_unit_group, which will create the
7762 struct type_unit_group if necessary and add it to
7763 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7764
7765 static void
7766 build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
7767 {
7768 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7769 abbrev_table_up abbrev_table;
7770 sect_offset abbrev_offset;
7771
7772 /* It's up to the caller to not call us multiple times. */
7773 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7774
7775 if (per_objfile->per_bfd->all_type_units.empty ())
7776 return;
7777
7778 /* TUs typically share abbrev tables, and there can be way more TUs than
7779 abbrev tables. Sort by abbrev table to reduce the number of times we
7780 read each abbrev table in.
7781 Alternatives are to punt or to maintain a cache of abbrev tables.
7782 This is simpler and efficient enough for now.
7783
7784 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7785 symtab to use). Typically TUs with the same abbrev offset have the same
7786 stmt_list value too so in practice this should work well.
7787
7788 The basic algorithm here is:
7789
7790 sort TUs by abbrev table
7791 for each TU with same abbrev table:
7792 read abbrev table if first user
7793 read TU top level DIE
7794 [IWBN if DWO skeletons had DW_AT_stmt_list]
7795 call FUNC */
7796
7797 if (dwarf_read_debug)
7798 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7799
7800 /* Sort in a separate table to maintain the order of all_type_units
7801 for .gdb_index: TU indices directly index all_type_units. */
7802 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7803 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
7804
7805 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
7806 sorted_by_abbrev.emplace_back
7807 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
7808 sig_type->per_cu.sect_off));
7809
7810 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7811 sort_tu_by_abbrev_offset);
7812
7813 abbrev_offset = (sect_offset) ~(unsigned) 0;
7814
7815 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7816 {
7817 /* Switch to the next abbrev table if necessary. */
7818 if (abbrev_table == NULL
7819 || tu.abbrev_offset != abbrev_offset)
7820 {
7821 abbrev_offset = tu.abbrev_offset;
7822 abbrev_table =
7823 abbrev_table::read (per_objfile->objfile,
7824 &per_objfile->per_bfd->abbrev, abbrev_offset);
7825 ++tu_stats->nr_uniq_abbrev_tables;
7826 }
7827
7828 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
7829 abbrev_table.get (), nullptr, false);
7830 if (!reader.dummy_p)
7831 build_type_psymtabs_reader (&reader, reader.info_ptr,
7832 reader.comp_unit_die);
7833 }
7834 }
7835
7836 /* Print collected type unit statistics. */
7837
7838 static void
7839 print_tu_stats (dwarf2_per_objfile *per_objfile)
7840 {
7841 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7842
7843 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7844 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7845 per_objfile->per_bfd->all_type_units.size ());
7846 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7847 tu_stats->nr_uniq_abbrev_tables);
7848 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7849 tu_stats->nr_symtabs);
7850 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7851 tu_stats->nr_symtab_sharers);
7852 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7853 tu_stats->nr_stmt_less_type_units);
7854 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7855 tu_stats->nr_all_type_units_reallocs);
7856 }
7857
7858 /* Traversal function for build_type_psymtabs. */
7859
7860 static int
7861 build_type_psymtab_dependencies (void **slot, void *info)
7862 {
7863 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7864 struct objfile *objfile = per_objfile->objfile;
7865 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7866 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7867 dwarf2_psymtab *pst = per_cu->v.psymtab;
7868 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7869 int i;
7870
7871 gdb_assert (len > 0);
7872 gdb_assert (per_cu->type_unit_group_p ());
7873
7874 pst->number_of_dependencies = len;
7875 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7876 for (i = 0; i < len; ++i)
7877 {
7878 struct signatured_type *iter = tu_group->tus->at (i);
7879 gdb_assert (iter->per_cu.is_debug_types);
7880 pst->dependencies[i] = iter->per_cu.v.psymtab;
7881 iter->type_unit_group = tu_group;
7882 }
7883
7884 delete tu_group->tus;
7885 tu_group->tus = nullptr;
7886
7887 return 1;
7888 }
7889
7890 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7891 Build partial symbol tables for the .debug_types comp-units. */
7892
7893 static void
7894 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7895 {
7896 if (! create_all_type_units (per_objfile))
7897 return;
7898
7899 build_type_psymtabs_1 (per_objfile);
7900 }
7901
7902 /* Traversal function for process_skeletonless_type_unit.
7903 Read a TU in a DWO file and build partial symbols for it. */
7904
7905 static int
7906 process_skeletonless_type_unit (void **slot, void *info)
7907 {
7908 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7909 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7910 struct signatured_type find_entry, *entry;
7911
7912 /* If this TU doesn't exist in the global table, add it and read it in. */
7913
7914 if (per_objfile->per_bfd->signatured_types == NULL)
7915 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7916
7917 find_entry.signature = dwo_unit->signature;
7918 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7919 &find_entry, INSERT);
7920 /* If we've already seen this type there's nothing to do. What's happening
7921 is we're doing our own version of comdat-folding here. */
7922 if (*slot != NULL)
7923 return 1;
7924
7925 /* This does the job that create_all_type_units would have done for
7926 this TU. */
7927 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7928 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7929 *slot = entry;
7930
7931 /* This does the job that build_type_psymtabs_1 would have done. */
7932 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
7933 if (!reader.dummy_p)
7934 build_type_psymtabs_reader (&reader, reader.info_ptr,
7935 reader.comp_unit_die);
7936
7937 return 1;
7938 }
7939
7940 /* Traversal function for process_skeletonless_type_units. */
7941
7942 static int
7943 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7944 {
7945 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7946
7947 if (dwo_file->tus != NULL)
7948 htab_traverse_noresize (dwo_file->tus.get (),
7949 process_skeletonless_type_unit, info);
7950
7951 return 1;
7952 }
7953
7954 /* Scan all TUs of DWO files, verifying we've processed them.
7955 This is needed in case a TU was emitted without its skeleton.
7956 Note: This can't be done until we know what all the DWO files are. */
7957
7958 static void
7959 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
7960 {
7961 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7962 if (get_dwp_file (per_objfile) == NULL
7963 && per_objfile->per_bfd->dwo_files != NULL)
7964 {
7965 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7966 process_dwo_file_for_skeletonless_type_units,
7967 per_objfile);
7968 }
7969 }
7970
7971 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7972
7973 static void
7974 set_partial_user (dwarf2_per_objfile *per_objfile)
7975 {
7976 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
7977 {
7978 dwarf2_psymtab *pst = per_cu->v.psymtab;
7979
7980 if (pst == NULL)
7981 continue;
7982
7983 for (int j = 0; j < pst->number_of_dependencies; ++j)
7984 {
7985 /* Set the 'user' field only if it is not already set. */
7986 if (pst->dependencies[j]->user == NULL)
7987 pst->dependencies[j]->user = pst;
7988 }
7989 }
7990 }
7991
7992 /* Build the partial symbol table by doing a quick pass through the
7993 .debug_info and .debug_abbrev sections. */
7994
7995 static void
7996 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7997 {
7998 struct objfile *objfile = per_objfile->objfile;
7999
8000 if (dwarf_read_debug)
8001 {
8002 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8003 objfile_name (objfile));
8004 }
8005
8006 scoped_restore restore_reading_psyms
8007 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
8008 true);
8009
8010 per_objfile->per_bfd->info.read (objfile);
8011
8012 /* Any cached compilation units will be linked by the per-objfile
8013 read_in_chain. Make sure to free them when we're done. */
8014 free_cached_comp_units freer (per_objfile);
8015
8016 build_type_psymtabs (per_objfile);
8017
8018 create_all_comp_units (per_objfile);
8019
8020 /* Create a temporary address map on a temporary obstack. We later
8021 copy this to the final obstack. */
8022 auto_obstack temp_obstack;
8023
8024 scoped_restore save_psymtabs_addrmap
8025 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8026 addrmap_create_mutable (&temp_obstack));
8027
8028 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8029 {
8030 if (per_cu->v.psymtab != NULL)
8031 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8032 continue;
8033 process_psymtab_comp_unit (per_cu, per_objfile, false,
8034 language_minimal);
8035 }
8036
8037 /* This has to wait until we read the CUs, we need the list of DWOs. */
8038 process_skeletonless_type_units (per_objfile);
8039
8040 /* Now that all TUs have been processed we can fill in the dependencies. */
8041 if (per_objfile->per_bfd->type_unit_groups != NULL)
8042 {
8043 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8044 build_type_psymtab_dependencies, per_objfile);
8045 }
8046
8047 if (dwarf_read_debug)
8048 print_tu_stats (per_objfile);
8049
8050 set_partial_user (per_objfile);
8051
8052 objfile->partial_symtabs->psymtabs_addrmap
8053 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8054 objfile->partial_symtabs->obstack ());
8055 /* At this point we want to keep the address map. */
8056 save_psymtabs_addrmap.release ();
8057
8058 if (dwarf_read_debug)
8059 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8060 objfile_name (objfile));
8061 }
8062
8063 /* Load the partial DIEs for a secondary CU into memory.
8064 This is also used when rereading a primary CU with load_all_dies. */
8065
8066 static void
8067 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8068 dwarf2_per_objfile *per_objfile,
8069 dwarf2_cu *existing_cu)
8070 {
8071 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
8072
8073 if (!reader.dummy_p)
8074 {
8075 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8076 language_minimal);
8077
8078 /* Check if comp unit has_children.
8079 If so, read the rest of the partial symbols from this comp unit.
8080 If not, there's no more debug_info for this comp unit. */
8081 if (reader.comp_unit_die->has_children)
8082 load_partial_dies (&reader, reader.info_ptr, 0);
8083
8084 reader.keep ();
8085 }
8086 }
8087
8088 static void
8089 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
8090 struct dwarf2_section_info *section,
8091 struct dwarf2_section_info *abbrev_section,
8092 unsigned int is_dwz)
8093 {
8094 const gdb_byte *info_ptr;
8095 struct objfile *objfile = per_objfile->objfile;
8096
8097 if (dwarf_read_debug)
8098 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8099 section->get_name (),
8100 section->get_file_name ());
8101
8102 section->read (objfile);
8103
8104 info_ptr = section->buffer;
8105
8106 while (info_ptr < section->buffer + section->size)
8107 {
8108 struct dwarf2_per_cu_data *this_cu;
8109
8110 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8111
8112 comp_unit_head cu_header;
8113 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
8114 abbrev_section, info_ptr,
8115 rcuh_kind::COMPILE);
8116
8117 /* Save the compilation unit for later lookup. */
8118 if (cu_header.unit_type != DW_UT_type)
8119 this_cu = per_objfile->per_bfd->allocate_per_cu ();
8120 else
8121 {
8122 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
8123 sig_type->signature = cu_header.signature;
8124 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8125 this_cu = &sig_type->per_cu;
8126 }
8127 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8128 this_cu->sect_off = sect_off;
8129 this_cu->length = cu_header.length + cu_header.initial_length_size;
8130 this_cu->is_dwz = is_dwz;
8131 this_cu->section = section;
8132
8133 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8134
8135 info_ptr = info_ptr + this_cu->length;
8136 }
8137 }
8138
8139 /* Create a list of all compilation units in OBJFILE.
8140 This is only done for -readnow and building partial symtabs. */
8141
8142 static void
8143 create_all_comp_units (dwarf2_per_objfile *per_objfile)
8144 {
8145 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8146 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8147 &per_objfile->per_bfd->abbrev, 0);
8148
8149 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8150 if (dwz != NULL)
8151 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
8152 }
8153
8154 /* Process all loaded DIEs for compilation unit CU, starting at
8155 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8156 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8157 DW_AT_ranges). See the comments of add_partial_subprogram on how
8158 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8159
8160 static void
8161 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8162 CORE_ADDR *highpc, int set_addrmap,
8163 struct dwarf2_cu *cu)
8164 {
8165 struct partial_die_info *pdi;
8166
8167 /* Now, march along the PDI's, descending into ones which have
8168 interesting children but skipping the children of the other ones,
8169 until we reach the end of the compilation unit. */
8170
8171 pdi = first_die;
8172
8173 while (pdi != NULL)
8174 {
8175 pdi->fixup (cu);
8176
8177 /* Anonymous namespaces or modules have no name but have interesting
8178 children, so we need to look at them. Ditto for anonymous
8179 enums. */
8180
8181 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
8182 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8183 || pdi->tag == DW_TAG_imported_unit
8184 || pdi->tag == DW_TAG_inlined_subroutine)
8185 {
8186 switch (pdi->tag)
8187 {
8188 case DW_TAG_subprogram:
8189 case DW_TAG_inlined_subroutine:
8190 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8191 if (cu->language == language_cplus)
8192 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8193 set_addrmap, cu);
8194 break;
8195 case DW_TAG_constant:
8196 case DW_TAG_variable:
8197 case DW_TAG_typedef:
8198 case DW_TAG_union_type:
8199 if (!pdi->is_declaration
8200 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8201 {
8202 add_partial_symbol (pdi, cu);
8203 }
8204 break;
8205 case DW_TAG_class_type:
8206 case DW_TAG_interface_type:
8207 case DW_TAG_structure_type:
8208 if (!pdi->is_declaration)
8209 {
8210 add_partial_symbol (pdi, cu);
8211 }
8212 if ((cu->language == language_rust
8213 || cu->language == language_cplus) && pdi->has_children)
8214 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8215 set_addrmap, cu);
8216 break;
8217 case DW_TAG_enumeration_type:
8218 if (!pdi->is_declaration)
8219 add_partial_enumeration (pdi, cu);
8220 break;
8221 case DW_TAG_base_type:
8222 case DW_TAG_subrange_type:
8223 /* File scope base type definitions are added to the partial
8224 symbol table. */
8225 add_partial_symbol (pdi, cu);
8226 break;
8227 case DW_TAG_namespace:
8228 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8229 break;
8230 case DW_TAG_module:
8231 if (!pdi->is_declaration)
8232 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8233 break;
8234 case DW_TAG_imported_unit:
8235 {
8236 struct dwarf2_per_cu_data *per_cu;
8237
8238 /* For now we don't handle imported units in type units. */
8239 if (cu->per_cu->is_debug_types)
8240 {
8241 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8242 " supported in type units [in module %s]"),
8243 objfile_name (cu->per_objfile->objfile));
8244 }
8245
8246 per_cu = dwarf2_find_containing_comp_unit
8247 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8248
8249 /* Go read the partial unit, if needed. */
8250 if (per_cu->v.psymtab == NULL)
8251 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8252 cu->language);
8253
8254 cu->per_cu->imported_symtabs_push (per_cu);
8255 }
8256 break;
8257 case DW_TAG_imported_declaration:
8258 add_partial_symbol (pdi, cu);
8259 break;
8260 default:
8261 break;
8262 }
8263 }
8264
8265 /* If the die has a sibling, skip to the sibling. */
8266
8267 pdi = pdi->die_sibling;
8268 }
8269 }
8270
8271 /* Functions used to compute the fully scoped name of a partial DIE.
8272
8273 Normally, this is simple. For C++, the parent DIE's fully scoped
8274 name is concatenated with "::" and the partial DIE's name.
8275 Enumerators are an exception; they use the scope of their parent
8276 enumeration type, i.e. the name of the enumeration type is not
8277 prepended to the enumerator.
8278
8279 There are two complexities. One is DW_AT_specification; in this
8280 case "parent" means the parent of the target of the specification,
8281 instead of the direct parent of the DIE. The other is compilers
8282 which do not emit DW_TAG_namespace; in this case we try to guess
8283 the fully qualified name of structure types from their members'
8284 linkage names. This must be done using the DIE's children rather
8285 than the children of any DW_AT_specification target. We only need
8286 to do this for structures at the top level, i.e. if the target of
8287 any DW_AT_specification (if any; otherwise the DIE itself) does not
8288 have a parent. */
8289
8290 /* Compute the scope prefix associated with PDI's parent, in
8291 compilation unit CU. The result will be allocated on CU's
8292 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8293 field. NULL is returned if no prefix is necessary. */
8294 static const char *
8295 partial_die_parent_scope (struct partial_die_info *pdi,
8296 struct dwarf2_cu *cu)
8297 {
8298 const char *grandparent_scope;
8299 struct partial_die_info *parent, *real_pdi;
8300
8301 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8302 then this means the parent of the specification DIE. */
8303
8304 real_pdi = pdi;
8305 while (real_pdi->has_specification)
8306 {
8307 auto res = find_partial_die (real_pdi->spec_offset,
8308 real_pdi->spec_is_dwz, cu);
8309 real_pdi = res.pdi;
8310 cu = res.cu;
8311 }
8312
8313 parent = real_pdi->die_parent;
8314 if (parent == NULL)
8315 return NULL;
8316
8317 if (parent->scope_set)
8318 return parent->scope;
8319
8320 parent->fixup (cu);
8321
8322 grandparent_scope = partial_die_parent_scope (parent, cu);
8323
8324 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8325 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8326 Work around this problem here. */
8327 if (cu->language == language_cplus
8328 && parent->tag == DW_TAG_namespace
8329 && strcmp (parent->name (cu), "::") == 0
8330 && grandparent_scope == NULL)
8331 {
8332 parent->scope = NULL;
8333 parent->scope_set = 1;
8334 return NULL;
8335 }
8336
8337 /* Nested subroutines in Fortran get a prefix. */
8338 if (pdi->tag == DW_TAG_enumerator)
8339 /* Enumerators should not get the name of the enumeration as a prefix. */
8340 parent->scope = grandparent_scope;
8341 else if (parent->tag == DW_TAG_namespace
8342 || parent->tag == DW_TAG_module
8343 || parent->tag == DW_TAG_structure_type
8344 || parent->tag == DW_TAG_class_type
8345 || parent->tag == DW_TAG_interface_type
8346 || parent->tag == DW_TAG_union_type
8347 || parent->tag == DW_TAG_enumeration_type
8348 || (cu->language == language_fortran
8349 && parent->tag == DW_TAG_subprogram
8350 && pdi->tag == DW_TAG_subprogram))
8351 {
8352 if (grandparent_scope == NULL)
8353 parent->scope = parent->name (cu);
8354 else
8355 parent->scope = typename_concat (&cu->comp_unit_obstack,
8356 grandparent_scope,
8357 parent->name (cu), 0, cu);
8358 }
8359 else
8360 {
8361 /* FIXME drow/2004-04-01: What should we be doing with
8362 function-local names? For partial symbols, we should probably be
8363 ignoring them. */
8364 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8365 dwarf_tag_name (parent->tag),
8366 sect_offset_str (pdi->sect_off));
8367 parent->scope = grandparent_scope;
8368 }
8369
8370 parent->scope_set = 1;
8371 return parent->scope;
8372 }
8373
8374 /* Return the fully scoped name associated with PDI, from compilation unit
8375 CU. The result will be allocated with malloc. */
8376
8377 static gdb::unique_xmalloc_ptr<char>
8378 partial_die_full_name (struct partial_die_info *pdi,
8379 struct dwarf2_cu *cu)
8380 {
8381 const char *parent_scope;
8382
8383 /* If this is a template instantiation, we can not work out the
8384 template arguments from partial DIEs. So, unfortunately, we have
8385 to go through the full DIEs. At least any work we do building
8386 types here will be reused if full symbols are loaded later. */
8387 if (pdi->has_template_arguments)
8388 {
8389 pdi->fixup (cu);
8390
8391 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8392 {
8393 struct die_info *die;
8394 struct attribute attr;
8395 struct dwarf2_cu *ref_cu = cu;
8396
8397 /* DW_FORM_ref_addr is using section offset. */
8398 attr.name = (enum dwarf_attribute) 0;
8399 attr.form = DW_FORM_ref_addr;
8400 attr.u.unsnd = to_underlying (pdi->sect_off);
8401 die = follow_die_ref (NULL, &attr, &ref_cu);
8402
8403 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8404 }
8405 }
8406
8407 parent_scope = partial_die_parent_scope (pdi, cu);
8408 if (parent_scope == NULL)
8409 return NULL;
8410 else
8411 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8412 pdi->name (cu),
8413 0, cu));
8414 }
8415
8416 static void
8417 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8418 {
8419 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8420 struct objfile *objfile = per_objfile->objfile;
8421 struct gdbarch *gdbarch = objfile->arch ();
8422 CORE_ADDR addr = 0;
8423 const char *actual_name = NULL;
8424 CORE_ADDR baseaddr;
8425
8426 baseaddr = objfile->text_section_offset ();
8427
8428 gdb::unique_xmalloc_ptr<char> built_actual_name
8429 = partial_die_full_name (pdi, cu);
8430 if (built_actual_name != NULL)
8431 actual_name = built_actual_name.get ();
8432
8433 if (actual_name == NULL)
8434 actual_name = pdi->name (cu);
8435
8436 partial_symbol psymbol;
8437 memset (&psymbol, 0, sizeof (psymbol));
8438 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8439 psymbol.ginfo.section = -1;
8440
8441 /* The code below indicates that the psymbol should be installed by
8442 setting this. */
8443 gdb::optional<psymbol_placement> where;
8444
8445 switch (pdi->tag)
8446 {
8447 case DW_TAG_inlined_subroutine:
8448 case DW_TAG_subprogram:
8449 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8450 - baseaddr);
8451 if (pdi->is_external
8452 || cu->language == language_ada
8453 || (cu->language == language_fortran
8454 && pdi->die_parent != NULL
8455 && pdi->die_parent->tag == DW_TAG_subprogram))
8456 {
8457 /* Normally, only "external" DIEs are part of the global scope.
8458 But in Ada and Fortran, we want to be able to access nested
8459 procedures globally. So all Ada and Fortran subprograms are
8460 stored in the global scope. */
8461 where = psymbol_placement::GLOBAL;
8462 }
8463 else
8464 where = psymbol_placement::STATIC;
8465
8466 psymbol.domain = VAR_DOMAIN;
8467 psymbol.aclass = LOC_BLOCK;
8468 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8469 psymbol.ginfo.value.address = addr;
8470
8471 if (pdi->main_subprogram && actual_name != NULL)
8472 set_objfile_main_name (objfile, actual_name, cu->language);
8473 break;
8474 case DW_TAG_constant:
8475 psymbol.domain = VAR_DOMAIN;
8476 psymbol.aclass = LOC_STATIC;
8477 where = (pdi->is_external
8478 ? psymbol_placement::GLOBAL
8479 : psymbol_placement::STATIC);
8480 break;
8481 case DW_TAG_variable:
8482 if (pdi->d.locdesc)
8483 addr = decode_locdesc (pdi->d.locdesc, cu);
8484
8485 if (pdi->d.locdesc
8486 && addr == 0
8487 && !per_objfile->per_bfd->has_section_at_zero)
8488 {
8489 /* A global or static variable may also have been stripped
8490 out by the linker if unused, in which case its address
8491 will be nullified; do not add such variables into partial
8492 symbol table then. */
8493 }
8494 else if (pdi->is_external)
8495 {
8496 /* Global Variable.
8497 Don't enter into the minimal symbol tables as there is
8498 a minimal symbol table entry from the ELF symbols already.
8499 Enter into partial symbol table if it has a location
8500 descriptor or a type.
8501 If the location descriptor is missing, new_symbol will create
8502 a LOC_UNRESOLVED symbol, the address of the variable will then
8503 be determined from the minimal symbol table whenever the variable
8504 is referenced.
8505 The address for the partial symbol table entry is not
8506 used by GDB, but it comes in handy for debugging partial symbol
8507 table building. */
8508
8509 if (pdi->d.locdesc || pdi->has_type)
8510 {
8511 psymbol.domain = VAR_DOMAIN;
8512 psymbol.aclass = LOC_STATIC;
8513 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8514 psymbol.ginfo.value.address = addr;
8515 where = psymbol_placement::GLOBAL;
8516 }
8517 }
8518 else
8519 {
8520 int has_loc = pdi->d.locdesc != NULL;
8521
8522 /* Static Variable. Skip symbols whose value we cannot know (those
8523 without location descriptors or constant values). */
8524 if (!has_loc && !pdi->has_const_value)
8525 return;
8526
8527 psymbol.domain = VAR_DOMAIN;
8528 psymbol.aclass = LOC_STATIC;
8529 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8530 if (has_loc)
8531 psymbol.ginfo.value.address = addr;
8532 where = psymbol_placement::STATIC;
8533 }
8534 break;
8535 case DW_TAG_typedef:
8536 case DW_TAG_base_type:
8537 case DW_TAG_subrange_type:
8538 psymbol.domain = VAR_DOMAIN;
8539 psymbol.aclass = LOC_TYPEDEF;
8540 where = psymbol_placement::STATIC;
8541 break;
8542 case DW_TAG_imported_declaration:
8543 case DW_TAG_namespace:
8544 psymbol.domain = VAR_DOMAIN;
8545 psymbol.aclass = LOC_TYPEDEF;
8546 where = psymbol_placement::GLOBAL;
8547 break;
8548 case DW_TAG_module:
8549 /* With Fortran 77 there might be a "BLOCK DATA" module
8550 available without any name. If so, we skip the module as it
8551 doesn't bring any value. */
8552 if (actual_name != nullptr)
8553 {
8554 psymbol.domain = MODULE_DOMAIN;
8555 psymbol.aclass = LOC_TYPEDEF;
8556 where = psymbol_placement::GLOBAL;
8557 }
8558 break;
8559 case DW_TAG_class_type:
8560 case DW_TAG_interface_type:
8561 case DW_TAG_structure_type:
8562 case DW_TAG_union_type:
8563 case DW_TAG_enumeration_type:
8564 /* Skip external references. The DWARF standard says in the section
8565 about "Structure, Union, and Class Type Entries": "An incomplete
8566 structure, union or class type is represented by a structure,
8567 union or class entry that does not have a byte size attribute
8568 and that has a DW_AT_declaration attribute." */
8569 if (!pdi->has_byte_size && pdi->is_declaration)
8570 return;
8571
8572 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8573 static vs. global. */
8574 psymbol.domain = STRUCT_DOMAIN;
8575 psymbol.aclass = LOC_TYPEDEF;
8576 where = (cu->language == language_cplus
8577 ? psymbol_placement::GLOBAL
8578 : psymbol_placement::STATIC);
8579 break;
8580 case DW_TAG_enumerator:
8581 psymbol.domain = VAR_DOMAIN;
8582 psymbol.aclass = LOC_CONST;
8583 where = (cu->language == language_cplus
8584 ? psymbol_placement::GLOBAL
8585 : psymbol_placement::STATIC);
8586 break;
8587 default:
8588 break;
8589 }
8590
8591 if (where.has_value ())
8592 {
8593 if (built_actual_name != nullptr)
8594 actual_name = objfile->intern (actual_name);
8595 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8596 psymbol.ginfo.set_linkage_name (actual_name);
8597 else
8598 {
8599 psymbol.ginfo.set_demangled_name (actual_name,
8600 &objfile->objfile_obstack);
8601 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8602 }
8603 add_psymbol_to_list (psymbol, *where, objfile);
8604 }
8605 }
8606
8607 /* Read a partial die corresponding to a namespace; also, add a symbol
8608 corresponding to that namespace to the symbol table. NAMESPACE is
8609 the name of the enclosing namespace. */
8610
8611 static void
8612 add_partial_namespace (struct partial_die_info *pdi,
8613 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8614 int set_addrmap, struct dwarf2_cu *cu)
8615 {
8616 /* Add a symbol for the namespace. */
8617
8618 add_partial_symbol (pdi, cu);
8619
8620 /* Now scan partial symbols in that namespace. */
8621
8622 if (pdi->has_children)
8623 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8624 }
8625
8626 /* Read a partial die corresponding to a Fortran module. */
8627
8628 static void
8629 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8630 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8631 {
8632 /* Add a symbol for the namespace. */
8633
8634 add_partial_symbol (pdi, cu);
8635
8636 /* Now scan partial symbols in that module. */
8637
8638 if (pdi->has_children)
8639 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8640 }
8641
8642 /* Read a partial die corresponding to a subprogram or an inlined
8643 subprogram and create a partial symbol for that subprogram.
8644 When the CU language allows it, this routine also defines a partial
8645 symbol for each nested subprogram that this subprogram contains.
8646 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8647 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8648
8649 PDI may also be a lexical block, in which case we simply search
8650 recursively for subprograms defined inside that lexical block.
8651 Again, this is only performed when the CU language allows this
8652 type of definitions. */
8653
8654 static void
8655 add_partial_subprogram (struct partial_die_info *pdi,
8656 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8657 int set_addrmap, struct dwarf2_cu *cu)
8658 {
8659 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8660 {
8661 if (pdi->has_pc_info)
8662 {
8663 if (pdi->lowpc < *lowpc)
8664 *lowpc = pdi->lowpc;
8665 if (pdi->highpc > *highpc)
8666 *highpc = pdi->highpc;
8667 if (set_addrmap)
8668 {
8669 struct objfile *objfile = cu->per_objfile->objfile;
8670 struct gdbarch *gdbarch = objfile->arch ();
8671 CORE_ADDR baseaddr;
8672 CORE_ADDR this_highpc;
8673 CORE_ADDR this_lowpc;
8674
8675 baseaddr = objfile->text_section_offset ();
8676 this_lowpc
8677 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8678 pdi->lowpc + baseaddr)
8679 - baseaddr);
8680 this_highpc
8681 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8682 pdi->highpc + baseaddr)
8683 - baseaddr);
8684 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8685 this_lowpc, this_highpc - 1,
8686 cu->per_cu->v.psymtab);
8687 }
8688 }
8689
8690 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8691 {
8692 if (!pdi->is_declaration)
8693 /* Ignore subprogram DIEs that do not have a name, they are
8694 illegal. Do not emit a complaint at this point, we will
8695 do so when we convert this psymtab into a symtab. */
8696 if (pdi->name (cu))
8697 add_partial_symbol (pdi, cu);
8698 }
8699 }
8700
8701 if (! pdi->has_children)
8702 return;
8703
8704 if (cu->language == language_ada || cu->language == language_fortran)
8705 {
8706 pdi = pdi->die_child;
8707 while (pdi != NULL)
8708 {
8709 pdi->fixup (cu);
8710 if (pdi->tag == DW_TAG_subprogram
8711 || pdi->tag == DW_TAG_inlined_subroutine
8712 || pdi->tag == DW_TAG_lexical_block)
8713 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8714 pdi = pdi->die_sibling;
8715 }
8716 }
8717 }
8718
8719 /* Read a partial die corresponding to an enumeration type. */
8720
8721 static void
8722 add_partial_enumeration (struct partial_die_info *enum_pdi,
8723 struct dwarf2_cu *cu)
8724 {
8725 struct partial_die_info *pdi;
8726
8727 if (enum_pdi->name (cu) != NULL)
8728 add_partial_symbol (enum_pdi, cu);
8729
8730 pdi = enum_pdi->die_child;
8731 while (pdi)
8732 {
8733 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8734 complaint (_("malformed enumerator DIE ignored"));
8735 else
8736 add_partial_symbol (pdi, cu);
8737 pdi = pdi->die_sibling;
8738 }
8739 }
8740
8741 /* Return the initial uleb128 in the die at INFO_PTR. */
8742
8743 static unsigned int
8744 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8745 {
8746 unsigned int bytes_read;
8747
8748 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8749 }
8750
8751 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8752 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8753
8754 Return the corresponding abbrev, or NULL if the number is zero (indicating
8755 an empty DIE). In either case *BYTES_READ will be set to the length of
8756 the initial number. */
8757
8758 static struct abbrev_info *
8759 peek_die_abbrev (const die_reader_specs &reader,
8760 const gdb_byte *info_ptr, unsigned int *bytes_read)
8761 {
8762 dwarf2_cu *cu = reader.cu;
8763 bfd *abfd = cu->per_objfile->objfile->obfd;
8764 unsigned int abbrev_number
8765 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8766
8767 if (abbrev_number == 0)
8768 return NULL;
8769
8770 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8771 if (!abbrev)
8772 {
8773 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8774 " at offset %s [in module %s]"),
8775 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8776 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8777 }
8778
8779 return abbrev;
8780 }
8781
8782 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8783 Returns a pointer to the end of a series of DIEs, terminated by an empty
8784 DIE. Any children of the skipped DIEs will also be skipped. */
8785
8786 static const gdb_byte *
8787 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8788 {
8789 while (1)
8790 {
8791 unsigned int bytes_read;
8792 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8793
8794 if (abbrev == NULL)
8795 return info_ptr + bytes_read;
8796 else
8797 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8798 }
8799 }
8800
8801 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8802 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8803 abbrev corresponding to that skipped uleb128 should be passed in
8804 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8805 children. */
8806
8807 static const gdb_byte *
8808 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8809 struct abbrev_info *abbrev)
8810 {
8811 unsigned int bytes_read;
8812 struct attribute attr;
8813 bfd *abfd = reader->abfd;
8814 struct dwarf2_cu *cu = reader->cu;
8815 const gdb_byte *buffer = reader->buffer;
8816 const gdb_byte *buffer_end = reader->buffer_end;
8817 unsigned int form, i;
8818
8819 for (i = 0; i < abbrev->num_attrs; i++)
8820 {
8821 /* The only abbrev we care about is DW_AT_sibling. */
8822 if (abbrev->attrs[i].name == DW_AT_sibling)
8823 {
8824 bool ignored;
8825 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8826 &ignored);
8827 if (attr.form == DW_FORM_ref_addr)
8828 complaint (_("ignoring absolute DW_AT_sibling"));
8829 else
8830 {
8831 sect_offset off = attr.get_ref_die_offset ();
8832 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8833
8834 if (sibling_ptr < info_ptr)
8835 complaint (_("DW_AT_sibling points backwards"));
8836 else if (sibling_ptr > reader->buffer_end)
8837 reader->die_section->overflow_complaint ();
8838 else
8839 return sibling_ptr;
8840 }
8841 }
8842
8843 /* If it isn't DW_AT_sibling, skip this attribute. */
8844 form = abbrev->attrs[i].form;
8845 skip_attribute:
8846 switch (form)
8847 {
8848 case DW_FORM_ref_addr:
8849 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8850 and later it is offset sized. */
8851 if (cu->header.version == 2)
8852 info_ptr += cu->header.addr_size;
8853 else
8854 info_ptr += cu->header.offset_size;
8855 break;
8856 case DW_FORM_GNU_ref_alt:
8857 info_ptr += cu->header.offset_size;
8858 break;
8859 case DW_FORM_addr:
8860 info_ptr += cu->header.addr_size;
8861 break;
8862 case DW_FORM_data1:
8863 case DW_FORM_ref1:
8864 case DW_FORM_flag:
8865 case DW_FORM_strx1:
8866 info_ptr += 1;
8867 break;
8868 case DW_FORM_flag_present:
8869 case DW_FORM_implicit_const:
8870 break;
8871 case DW_FORM_data2:
8872 case DW_FORM_ref2:
8873 case DW_FORM_strx2:
8874 info_ptr += 2;
8875 break;
8876 case DW_FORM_strx3:
8877 info_ptr += 3;
8878 break;
8879 case DW_FORM_data4:
8880 case DW_FORM_ref4:
8881 case DW_FORM_strx4:
8882 info_ptr += 4;
8883 break;
8884 case DW_FORM_data8:
8885 case DW_FORM_ref8:
8886 case DW_FORM_ref_sig8:
8887 info_ptr += 8;
8888 break;
8889 case DW_FORM_data16:
8890 info_ptr += 16;
8891 break;
8892 case DW_FORM_string:
8893 read_direct_string (abfd, info_ptr, &bytes_read);
8894 info_ptr += bytes_read;
8895 break;
8896 case DW_FORM_sec_offset:
8897 case DW_FORM_strp:
8898 case DW_FORM_GNU_strp_alt:
8899 info_ptr += cu->header.offset_size;
8900 break;
8901 case DW_FORM_exprloc:
8902 case DW_FORM_block:
8903 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8904 info_ptr += bytes_read;
8905 break;
8906 case DW_FORM_block1:
8907 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8908 break;
8909 case DW_FORM_block2:
8910 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8911 break;
8912 case DW_FORM_block4:
8913 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8914 break;
8915 case DW_FORM_addrx:
8916 case DW_FORM_strx:
8917 case DW_FORM_sdata:
8918 case DW_FORM_udata:
8919 case DW_FORM_ref_udata:
8920 case DW_FORM_GNU_addr_index:
8921 case DW_FORM_GNU_str_index:
8922 case DW_FORM_rnglistx:
8923 case DW_FORM_loclistx:
8924 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8925 break;
8926 case DW_FORM_indirect:
8927 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8928 info_ptr += bytes_read;
8929 /* We need to continue parsing from here, so just go back to
8930 the top. */
8931 goto skip_attribute;
8932
8933 default:
8934 error (_("Dwarf Error: Cannot handle %s "
8935 "in DWARF reader [in module %s]"),
8936 dwarf_form_name (form),
8937 bfd_get_filename (abfd));
8938 }
8939 }
8940
8941 if (abbrev->has_children)
8942 return skip_children (reader, info_ptr);
8943 else
8944 return info_ptr;
8945 }
8946
8947 /* Locate ORIG_PDI's sibling.
8948 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8949
8950 static const gdb_byte *
8951 locate_pdi_sibling (const struct die_reader_specs *reader,
8952 struct partial_die_info *orig_pdi,
8953 const gdb_byte *info_ptr)
8954 {
8955 /* Do we know the sibling already? */
8956
8957 if (orig_pdi->sibling)
8958 return orig_pdi->sibling;
8959
8960 /* Are there any children to deal with? */
8961
8962 if (!orig_pdi->has_children)
8963 return info_ptr;
8964
8965 /* Skip the children the long way. */
8966
8967 return skip_children (reader, info_ptr);
8968 }
8969
8970 /* Expand this partial symbol table into a full symbol table. SELF is
8971 not NULL. */
8972
8973 void
8974 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8975 {
8976 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8977
8978 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
8979
8980 /* If this psymtab is constructed from a debug-only objfile, the
8981 has_section_at_zero flag will not necessarily be correct. We
8982 can get the correct value for this flag by looking at the data
8983 associated with the (presumably stripped) associated objfile. */
8984 if (objfile->separate_debug_objfile_backlink)
8985 {
8986 dwarf2_per_objfile *per_objfile_backlink
8987 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8988
8989 per_objfile->per_bfd->has_section_at_zero
8990 = per_objfile_backlink->per_bfd->has_section_at_zero;
8991 }
8992
8993 expand_psymtab (objfile);
8994
8995 process_cu_includes (per_objfile);
8996 }
8997 \f
8998 /* Reading in full CUs. */
8999
9000 /* Add PER_CU to the queue. */
9001
9002 static void
9003 queue_comp_unit (dwarf2_per_cu_data *per_cu,
9004 dwarf2_per_objfile *per_objfile,
9005 enum language pretend_language)
9006 {
9007 per_cu->queued = 1;
9008 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
9009 }
9010
9011 /* If PER_CU is not yet queued, add it to the queue.
9012 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9013 dependency.
9014 The result is non-zero if PER_CU was queued, otherwise the result is zero
9015 meaning either PER_CU is already queued or it is already loaded.
9016
9017 N.B. There is an invariant here that if a CU is queued then it is loaded.
9018 The caller is required to load PER_CU if we return non-zero. */
9019
9020 static int
9021 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9022 dwarf2_per_cu_data *per_cu,
9023 dwarf2_per_objfile *per_objfile,
9024 enum language pretend_language)
9025 {
9026 /* We may arrive here during partial symbol reading, if we need full
9027 DIEs to process an unusual case (e.g. template arguments). Do
9028 not queue PER_CU, just tell our caller to load its DIEs. */
9029 if (per_cu->per_bfd->reading_partial_symbols)
9030 {
9031 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9032
9033 if (cu == NULL || cu->dies == NULL)
9034 return 1;
9035 return 0;
9036 }
9037
9038 /* Mark the dependence relation so that we don't flush PER_CU
9039 too early. */
9040 if (dependent_cu != NULL)
9041 dwarf2_add_dependence (dependent_cu, per_cu);
9042
9043 /* If it's already on the queue, we have nothing to do. */
9044 if (per_cu->queued)
9045 return 0;
9046
9047 /* If the compilation unit is already loaded, just mark it as
9048 used. */
9049 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9050 if (cu != nullptr)
9051 {
9052 cu->last_used = 0;
9053 return 0;
9054 }
9055
9056 /* Add it to the queue. */
9057 queue_comp_unit (per_cu, per_objfile, pretend_language);
9058
9059 return 1;
9060 }
9061
9062 /* Process the queue. */
9063
9064 static void
9065 process_queue (dwarf2_per_objfile *per_objfile)
9066 {
9067 if (dwarf_read_debug)
9068 {
9069 fprintf_unfiltered (gdb_stdlog,
9070 "Expanding one or more symtabs of objfile %s ...\n",
9071 objfile_name (per_objfile->objfile));
9072 }
9073
9074 /* The queue starts out with one item, but following a DIE reference
9075 may load a new CU, adding it to the end of the queue. */
9076 while (!per_objfile->per_bfd->queue.empty ())
9077 {
9078 dwarf2_queue_item &item = per_objfile->per_bfd->queue.front ();
9079 dwarf2_per_cu_data *per_cu = item.per_cu;
9080
9081 if (!per_objfile->symtab_set_p (per_cu))
9082 {
9083 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9084
9085 /* Skip dummy CUs. */
9086 if (cu != nullptr)
9087 {
9088 unsigned int debug_print_threshold;
9089 char buf[100];
9090
9091 if (per_cu->is_debug_types)
9092 {
9093 struct signatured_type *sig_type =
9094 (struct signatured_type *) per_cu;
9095
9096 sprintf (buf, "TU %s at offset %s",
9097 hex_string (sig_type->signature),
9098 sect_offset_str (per_cu->sect_off));
9099 /* There can be 100s of TUs.
9100 Only print them in verbose mode. */
9101 debug_print_threshold = 2;
9102 }
9103 else
9104 {
9105 sprintf (buf, "CU at offset %s",
9106 sect_offset_str (per_cu->sect_off));
9107 debug_print_threshold = 1;
9108 }
9109
9110 if (dwarf_read_debug >= debug_print_threshold)
9111 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9112
9113 if (per_cu->is_debug_types)
9114 process_full_type_unit (cu, item.pretend_language);
9115 else
9116 process_full_comp_unit (cu, item.pretend_language);
9117
9118 if (dwarf_read_debug >= debug_print_threshold)
9119 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9120 }
9121 }
9122
9123 per_cu->queued = 0;
9124 per_objfile->per_bfd->queue.pop ();
9125 }
9126
9127 if (dwarf_read_debug)
9128 {
9129 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9130 objfile_name (per_objfile->objfile));
9131 }
9132 }
9133
9134 /* Read in full symbols for PST, and anything it depends on. */
9135
9136 void
9137 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9138 {
9139 gdb_assert (!readin_p (objfile));
9140
9141 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9142 free_cached_comp_units freer (per_objfile);
9143 expand_dependencies (objfile);
9144
9145 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9146 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9147 }
9148
9149 /* See psympriv.h. */
9150
9151 bool
9152 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9153 {
9154 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9155 return per_objfile->symtab_set_p (per_cu_data);
9156 }
9157
9158 /* See psympriv.h. */
9159
9160 compunit_symtab *
9161 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9162 {
9163 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9164 return per_objfile->get_symtab (per_cu_data);
9165 }
9166
9167 /* Trivial hash function for die_info: the hash value of a DIE
9168 is its offset in .debug_info for this objfile. */
9169
9170 static hashval_t
9171 die_hash (const void *item)
9172 {
9173 const struct die_info *die = (const struct die_info *) item;
9174
9175 return to_underlying (die->sect_off);
9176 }
9177
9178 /* Trivial comparison function for die_info structures: two DIEs
9179 are equal if they have the same offset. */
9180
9181 static int
9182 die_eq (const void *item_lhs, const void *item_rhs)
9183 {
9184 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9185 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9186
9187 return die_lhs->sect_off == die_rhs->sect_off;
9188 }
9189
9190 /* Load the DIEs associated with PER_CU into memory. */
9191
9192 static void
9193 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9194 dwarf2_per_objfile *per_objfile,
9195 bool skip_partial,
9196 enum language pretend_language)
9197 {
9198 gdb_assert (! this_cu->is_debug_types);
9199
9200 dwarf2_cu *existing_cu = per_objfile->get_cu (this_cu);
9201 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9202 if (reader.dummy_p)
9203 return;
9204
9205 struct dwarf2_cu *cu = reader.cu;
9206 const gdb_byte *info_ptr = reader.info_ptr;
9207
9208 gdb_assert (cu->die_hash == NULL);
9209 cu->die_hash =
9210 htab_create_alloc_ex (cu->header.length / 12,
9211 die_hash,
9212 die_eq,
9213 NULL,
9214 &cu->comp_unit_obstack,
9215 hashtab_obstack_allocate,
9216 dummy_obstack_deallocate);
9217
9218 if (reader.comp_unit_die->has_children)
9219 reader.comp_unit_die->child
9220 = read_die_and_siblings (&reader, reader.info_ptr,
9221 &info_ptr, reader.comp_unit_die);
9222 cu->dies = reader.comp_unit_die;
9223 /* comp_unit_die is not stored in die_hash, no need. */
9224
9225 /* We try not to read any attributes in this function, because not
9226 all CUs needed for references have been loaded yet, and symbol
9227 table processing isn't initialized. But we have to set the CU language,
9228 or we won't be able to build types correctly.
9229 Similarly, if we do not read the producer, we can not apply
9230 producer-specific interpretation. */
9231 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9232
9233 reader.keep ();
9234 }
9235
9236 /* Add a DIE to the delayed physname list. */
9237
9238 static void
9239 add_to_method_list (struct type *type, int fnfield_index, int index,
9240 const char *name, struct die_info *die,
9241 struct dwarf2_cu *cu)
9242 {
9243 struct delayed_method_info mi;
9244 mi.type = type;
9245 mi.fnfield_index = fnfield_index;
9246 mi.index = index;
9247 mi.name = name;
9248 mi.die = die;
9249 cu->method_list.push_back (mi);
9250 }
9251
9252 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9253 "const" / "volatile". If so, decrements LEN by the length of the
9254 modifier and return true. Otherwise return false. */
9255
9256 template<size_t N>
9257 static bool
9258 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9259 {
9260 size_t mod_len = sizeof (mod) - 1;
9261 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9262 {
9263 len -= mod_len;
9264 return true;
9265 }
9266 return false;
9267 }
9268
9269 /* Compute the physnames of any methods on the CU's method list.
9270
9271 The computation of method physnames is delayed in order to avoid the
9272 (bad) condition that one of the method's formal parameters is of an as yet
9273 incomplete type. */
9274
9275 static void
9276 compute_delayed_physnames (struct dwarf2_cu *cu)
9277 {
9278 /* Only C++ delays computing physnames. */
9279 if (cu->method_list.empty ())
9280 return;
9281 gdb_assert (cu->language == language_cplus);
9282
9283 for (const delayed_method_info &mi : cu->method_list)
9284 {
9285 const char *physname;
9286 struct fn_fieldlist *fn_flp
9287 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9288 physname = dwarf2_physname (mi.name, mi.die, cu);
9289 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9290 = physname ? physname : "";
9291
9292 /* Since there's no tag to indicate whether a method is a
9293 const/volatile overload, extract that information out of the
9294 demangled name. */
9295 if (physname != NULL)
9296 {
9297 size_t len = strlen (physname);
9298
9299 while (1)
9300 {
9301 if (physname[len] == ')') /* shortcut */
9302 break;
9303 else if (check_modifier (physname, len, " const"))
9304 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9305 else if (check_modifier (physname, len, " volatile"))
9306 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9307 else
9308 break;
9309 }
9310 }
9311 }
9312
9313 /* The list is no longer needed. */
9314 cu->method_list.clear ();
9315 }
9316
9317 /* Go objects should be embedded in a DW_TAG_module DIE,
9318 and it's not clear if/how imported objects will appear.
9319 To keep Go support simple until that's worked out,
9320 go back through what we've read and create something usable.
9321 We could do this while processing each DIE, and feels kinda cleaner,
9322 but that way is more invasive.
9323 This is to, for example, allow the user to type "p var" or "b main"
9324 without having to specify the package name, and allow lookups
9325 of module.object to work in contexts that use the expression
9326 parser. */
9327
9328 static void
9329 fixup_go_packaging (struct dwarf2_cu *cu)
9330 {
9331 gdb::unique_xmalloc_ptr<char> package_name;
9332 struct pending *list;
9333 int i;
9334
9335 for (list = *cu->get_builder ()->get_global_symbols ();
9336 list != NULL;
9337 list = list->next)
9338 {
9339 for (i = 0; i < list->nsyms; ++i)
9340 {
9341 struct symbol *sym = list->symbol[i];
9342
9343 if (sym->language () == language_go
9344 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9345 {
9346 gdb::unique_xmalloc_ptr<char> this_package_name
9347 (go_symbol_package_name (sym));
9348
9349 if (this_package_name == NULL)
9350 continue;
9351 if (package_name == NULL)
9352 package_name = std::move (this_package_name);
9353 else
9354 {
9355 struct objfile *objfile = cu->per_objfile->objfile;
9356 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9357 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9358 (symbol_symtab (sym) != NULL
9359 ? symtab_to_filename_for_display
9360 (symbol_symtab (sym))
9361 : objfile_name (objfile)),
9362 this_package_name.get (), package_name.get ());
9363 }
9364 }
9365 }
9366 }
9367
9368 if (package_name != NULL)
9369 {
9370 struct objfile *objfile = cu->per_objfile->objfile;
9371 const char *saved_package_name = objfile->intern (package_name.get ());
9372 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9373 saved_package_name);
9374 struct symbol *sym;
9375
9376 sym = new (&objfile->objfile_obstack) symbol;
9377 sym->set_language (language_go, &objfile->objfile_obstack);
9378 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9379 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9380 e.g., "main" finds the "main" module and not C's main(). */
9381 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9382 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9383 SYMBOL_TYPE (sym) = type;
9384
9385 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9386 }
9387 }
9388
9389 /* Allocate a fully-qualified name consisting of the two parts on the
9390 obstack. */
9391
9392 static const char *
9393 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9394 {
9395 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9396 }
9397
9398 /* A helper that allocates a variant part to attach to a Rust enum
9399 type. OBSTACK is where the results should be allocated. TYPE is
9400 the type we're processing. DISCRIMINANT_INDEX is the index of the
9401 discriminant. It must be the index of one of the fields of TYPE.
9402 DEFAULT_INDEX is the index of the default field; or -1 if there is
9403 no default. RANGES is indexed by "effective" field number (the
9404 field index, but omitting the discriminant and default fields) and
9405 must hold the discriminant values used by the variants. Note that
9406 RANGES must have a lifetime at least as long as OBSTACK -- either
9407 already allocated on it, or static. */
9408
9409 static void
9410 alloc_rust_variant (struct obstack *obstack, struct type *type,
9411 int discriminant_index, int default_index,
9412 gdb::array_view<discriminant_range> ranges)
9413 {
9414 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9415 must be handled by the caller. */
9416 gdb_assert (discriminant_index >= 0
9417 && discriminant_index < type->num_fields ());
9418 gdb_assert (default_index == -1
9419 || (default_index >= 0 && default_index < type->num_fields ()));
9420
9421 /* We have one variant for each non-discriminant field. */
9422 int n_variants = type->num_fields () - 1;
9423
9424 variant *variants = new (obstack) variant[n_variants];
9425 int var_idx = 0;
9426 int range_idx = 0;
9427 for (int i = 0; i < type->num_fields (); ++i)
9428 {
9429 if (i == discriminant_index)
9430 continue;
9431
9432 variants[var_idx].first_field = i;
9433 variants[var_idx].last_field = i + 1;
9434
9435 /* The default field does not need a range, but other fields do.
9436 We skipped the discriminant above. */
9437 if (i != default_index)
9438 {
9439 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9440 ++range_idx;
9441 }
9442
9443 ++var_idx;
9444 }
9445
9446 gdb_assert (range_idx == ranges.size ());
9447 gdb_assert (var_idx == n_variants);
9448
9449 variant_part *part = new (obstack) variant_part;
9450 part->discriminant_index = discriminant_index;
9451 part->is_unsigned = TYPE_UNSIGNED (type->field (discriminant_index).type ());
9452 part->variants = gdb::array_view<variant> (variants, n_variants);
9453
9454 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9455 gdb::array_view<variant_part> *prop_value
9456 = new (storage) gdb::array_view<variant_part> (part, 1);
9457
9458 struct dynamic_prop prop;
9459 prop.kind = PROP_VARIANT_PARTS;
9460 prop.data.variant_parts = prop_value;
9461
9462 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9463 }
9464
9465 /* Some versions of rustc emitted enums in an unusual way.
9466
9467 Ordinary enums were emitted as unions. The first element of each
9468 structure in the union was named "RUST$ENUM$DISR". This element
9469 held the discriminant.
9470
9471 These versions of Rust also implemented the "non-zero"
9472 optimization. When the enum had two values, and one is empty and
9473 the other holds a pointer that cannot be zero, the pointer is used
9474 as the discriminant, with a zero value meaning the empty variant.
9475 Here, the union's first member is of the form
9476 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9477 where the fieldnos are the indices of the fields that should be
9478 traversed in order to find the field (which may be several fields deep)
9479 and the variantname is the name of the variant of the case when the
9480 field is zero.
9481
9482 This function recognizes whether TYPE is of one of these forms,
9483 and, if so, smashes it to be a variant type. */
9484
9485 static void
9486 quirk_rust_enum (struct type *type, struct objfile *objfile)
9487 {
9488 gdb_assert (type->code () == TYPE_CODE_UNION);
9489
9490 /* We don't need to deal with empty enums. */
9491 if (type->num_fields () == 0)
9492 return;
9493
9494 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9495 if (type->num_fields () == 1
9496 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9497 {
9498 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9499
9500 /* Decode the field name to find the offset of the
9501 discriminant. */
9502 ULONGEST bit_offset = 0;
9503 struct type *field_type = type->field (0).type ();
9504 while (name[0] >= '0' && name[0] <= '9')
9505 {
9506 char *tail;
9507 unsigned long index = strtoul (name, &tail, 10);
9508 name = tail;
9509 if (*name != '$'
9510 || index >= field_type->num_fields ()
9511 || (TYPE_FIELD_LOC_KIND (field_type, index)
9512 != FIELD_LOC_KIND_BITPOS))
9513 {
9514 complaint (_("Could not parse Rust enum encoding string \"%s\""
9515 "[in module %s]"),
9516 TYPE_FIELD_NAME (type, 0),
9517 objfile_name (objfile));
9518 return;
9519 }
9520 ++name;
9521
9522 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9523 field_type = field_type->field (index).type ();
9524 }
9525
9526 /* Smash this type to be a structure type. We have to do this
9527 because the type has already been recorded. */
9528 type->set_code (TYPE_CODE_STRUCT);
9529 type->set_num_fields (3);
9530 /* Save the field we care about. */
9531 struct field saved_field = type->field (0);
9532 type->set_fields
9533 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9534
9535 /* Put the discriminant at index 0. */
9536 type->field (0).set_type (field_type);
9537 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9538 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9539 SET_FIELD_BITPOS (type->field (0), bit_offset);
9540
9541 /* The order of fields doesn't really matter, so put the real
9542 field at index 1 and the data-less field at index 2. */
9543 type->field (1) = saved_field;
9544 TYPE_FIELD_NAME (type, 1)
9545 = rust_last_path_segment (type->field (1).type ()->name ());
9546 type->field (1).type ()->set_name
9547 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9548 TYPE_FIELD_NAME (type, 1)));
9549
9550 const char *dataless_name
9551 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9552 name);
9553 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9554 dataless_name);
9555 type->field (2).set_type (dataless_type);
9556 /* NAME points into the original discriminant name, which
9557 already has the correct lifetime. */
9558 TYPE_FIELD_NAME (type, 2) = name;
9559 SET_FIELD_BITPOS (type->field (2), 0);
9560
9561 /* Indicate that this is a variant type. */
9562 static discriminant_range ranges[1] = { { 0, 0 } };
9563 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9564 }
9565 /* A union with a single anonymous field is probably an old-style
9566 univariant enum. */
9567 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9568 {
9569 /* Smash this type to be a structure type. We have to do this
9570 because the type has already been recorded. */
9571 type->set_code (TYPE_CODE_STRUCT);
9572
9573 struct type *field_type = type->field (0).type ();
9574 const char *variant_name
9575 = rust_last_path_segment (field_type->name ());
9576 TYPE_FIELD_NAME (type, 0) = variant_name;
9577 field_type->set_name
9578 (rust_fully_qualify (&objfile->objfile_obstack,
9579 type->name (), variant_name));
9580 }
9581 else
9582 {
9583 struct type *disr_type = nullptr;
9584 for (int i = 0; i < type->num_fields (); ++i)
9585 {
9586 disr_type = type->field (i).type ();
9587
9588 if (disr_type->code () != TYPE_CODE_STRUCT)
9589 {
9590 /* All fields of a true enum will be structs. */
9591 return;
9592 }
9593 else if (disr_type->num_fields () == 0)
9594 {
9595 /* Could be data-less variant, so keep going. */
9596 disr_type = nullptr;
9597 }
9598 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9599 "RUST$ENUM$DISR") != 0)
9600 {
9601 /* Not a Rust enum. */
9602 return;
9603 }
9604 else
9605 {
9606 /* Found one. */
9607 break;
9608 }
9609 }
9610
9611 /* If we got here without a discriminant, then it's probably
9612 just a union. */
9613 if (disr_type == nullptr)
9614 return;
9615
9616 /* Smash this type to be a structure type. We have to do this
9617 because the type has already been recorded. */
9618 type->set_code (TYPE_CODE_STRUCT);
9619
9620 /* Make space for the discriminant field. */
9621 struct field *disr_field = &disr_type->field (0);
9622 field *new_fields
9623 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9624 * sizeof (struct field)));
9625 memcpy (new_fields + 1, type->fields (),
9626 type->num_fields () * sizeof (struct field));
9627 type->set_fields (new_fields);
9628 type->set_num_fields (type->num_fields () + 1);
9629
9630 /* Install the discriminant at index 0 in the union. */
9631 type->field (0) = *disr_field;
9632 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9633 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9634
9635 /* We need a way to find the correct discriminant given a
9636 variant name. For convenience we build a map here. */
9637 struct type *enum_type = disr_field->type ();
9638 std::unordered_map<std::string, ULONGEST> discriminant_map;
9639 for (int i = 0; i < enum_type->num_fields (); ++i)
9640 {
9641 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9642 {
9643 const char *name
9644 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9645 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9646 }
9647 }
9648
9649 int n_fields = type->num_fields ();
9650 /* We don't need a range entry for the discriminant, but we do
9651 need one for every other field, as there is no default
9652 variant. */
9653 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9654 discriminant_range,
9655 n_fields - 1);
9656 /* Skip the discriminant here. */
9657 for (int i = 1; i < n_fields; ++i)
9658 {
9659 /* Find the final word in the name of this variant's type.
9660 That name can be used to look up the correct
9661 discriminant. */
9662 const char *variant_name
9663 = rust_last_path_segment (type->field (i).type ()->name ());
9664
9665 auto iter = discriminant_map.find (variant_name);
9666 if (iter != discriminant_map.end ())
9667 {
9668 ranges[i].low = iter->second;
9669 ranges[i].high = iter->second;
9670 }
9671
9672 /* Remove the discriminant field, if it exists. */
9673 struct type *sub_type = type->field (i).type ();
9674 if (sub_type->num_fields () > 0)
9675 {
9676 sub_type->set_num_fields (sub_type->num_fields () - 1);
9677 sub_type->set_fields (sub_type->fields () + 1);
9678 }
9679 TYPE_FIELD_NAME (type, i) = variant_name;
9680 sub_type->set_name
9681 (rust_fully_qualify (&objfile->objfile_obstack,
9682 type->name (), variant_name));
9683 }
9684
9685 /* Indicate that this is a variant type. */
9686 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9687 gdb::array_view<discriminant_range> (ranges,
9688 n_fields - 1));
9689 }
9690 }
9691
9692 /* Rewrite some Rust unions to be structures with variants parts. */
9693
9694 static void
9695 rust_union_quirks (struct dwarf2_cu *cu)
9696 {
9697 gdb_assert (cu->language == language_rust);
9698 for (type *type_ : cu->rust_unions)
9699 quirk_rust_enum (type_, cu->per_objfile->objfile);
9700 /* We don't need this any more. */
9701 cu->rust_unions.clear ();
9702 }
9703
9704 /* See read.h. */
9705
9706 type_unit_group_unshareable *
9707 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9708 {
9709 auto iter = this->m_type_units.find (tu_group);
9710 if (iter != this->m_type_units.end ())
9711 return iter->second.get ();
9712
9713 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9714 type_unit_group_unshareable *result = uniq.get ();
9715 this->m_type_units[tu_group] = std::move (uniq);
9716 return result;
9717 }
9718
9719 struct type *
9720 dwarf2_per_objfile::get_type_for_signatured_type
9721 (signatured_type *sig_type) const
9722 {
9723 auto iter = this->m_type_map.find (sig_type);
9724 if (iter == this->m_type_map.end ())
9725 return nullptr;
9726
9727 return iter->second;
9728 }
9729
9730 void dwarf2_per_objfile::set_type_for_signatured_type
9731 (signatured_type *sig_type, struct type *type)
9732 {
9733 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9734
9735 this->m_type_map[sig_type] = type;
9736 }
9737
9738 /* A helper function for computing the list of all symbol tables
9739 included by PER_CU. */
9740
9741 static void
9742 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9743 htab_t all_children, htab_t all_type_symtabs,
9744 dwarf2_per_cu_data *per_cu,
9745 dwarf2_per_objfile *per_objfile,
9746 struct compunit_symtab *immediate_parent)
9747 {
9748 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9749 if (*slot != NULL)
9750 {
9751 /* This inclusion and its children have been processed. */
9752 return;
9753 }
9754
9755 *slot = per_cu;
9756
9757 /* Only add a CU if it has a symbol table. */
9758 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9759 if (cust != NULL)
9760 {
9761 /* If this is a type unit only add its symbol table if we haven't
9762 seen it yet (type unit per_cu's can share symtabs). */
9763 if (per_cu->is_debug_types)
9764 {
9765 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9766 if (*slot == NULL)
9767 {
9768 *slot = cust;
9769 result->push_back (cust);
9770 if (cust->user == NULL)
9771 cust->user = immediate_parent;
9772 }
9773 }
9774 else
9775 {
9776 result->push_back (cust);
9777 if (cust->user == NULL)
9778 cust->user = immediate_parent;
9779 }
9780 }
9781
9782 if (!per_cu->imported_symtabs_empty ())
9783 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9784 {
9785 recursively_compute_inclusions (result, all_children,
9786 all_type_symtabs, ptr, per_objfile,
9787 cust);
9788 }
9789 }
9790
9791 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9792 PER_CU. */
9793
9794 static void
9795 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9796 dwarf2_per_objfile *per_objfile)
9797 {
9798 gdb_assert (! per_cu->is_debug_types);
9799
9800 if (!per_cu->imported_symtabs_empty ())
9801 {
9802 int len;
9803 std::vector<compunit_symtab *> result_symtabs;
9804 htab_t all_children, all_type_symtabs;
9805 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9806
9807 /* If we don't have a symtab, we can just skip this case. */
9808 if (cust == NULL)
9809 return;
9810
9811 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9812 NULL, xcalloc, xfree);
9813 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9814 NULL, xcalloc, xfree);
9815
9816 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9817 {
9818 recursively_compute_inclusions (&result_symtabs, all_children,
9819 all_type_symtabs, ptr, per_objfile,
9820 cust);
9821 }
9822
9823 /* Now we have a transitive closure of all the included symtabs. */
9824 len = result_symtabs.size ();
9825 cust->includes
9826 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9827 struct compunit_symtab *, len + 1);
9828 memcpy (cust->includes, result_symtabs.data (),
9829 len * sizeof (compunit_symtab *));
9830 cust->includes[len] = NULL;
9831
9832 htab_delete (all_children);
9833 htab_delete (all_type_symtabs);
9834 }
9835 }
9836
9837 /* Compute the 'includes' field for the symtabs of all the CUs we just
9838 read. */
9839
9840 static void
9841 process_cu_includes (dwarf2_per_objfile *per_objfile)
9842 {
9843 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9844 {
9845 if (! iter->is_debug_types)
9846 compute_compunit_symtab_includes (iter, per_objfile);
9847 }
9848
9849 per_objfile->per_bfd->just_read_cus.clear ();
9850 }
9851
9852 /* Generate full symbol information for CU, whose DIEs have
9853 already been loaded into memory. */
9854
9855 static void
9856 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9857 {
9858 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9859 struct objfile *objfile = per_objfile->objfile;
9860 struct gdbarch *gdbarch = objfile->arch ();
9861 CORE_ADDR lowpc, highpc;
9862 struct compunit_symtab *cust;
9863 CORE_ADDR baseaddr;
9864 struct block *static_block;
9865 CORE_ADDR addr;
9866
9867 baseaddr = objfile->text_section_offset ();
9868
9869 /* Clear the list here in case something was left over. */
9870 cu->method_list.clear ();
9871
9872 cu->language = pretend_language;
9873 cu->language_defn = language_def (cu->language);
9874
9875 /* Do line number decoding in read_file_scope () */
9876 process_die (cu->dies, cu);
9877
9878 /* For now fudge the Go package. */
9879 if (cu->language == language_go)
9880 fixup_go_packaging (cu);
9881
9882 /* Now that we have processed all the DIEs in the CU, all the types
9883 should be complete, and it should now be safe to compute all of the
9884 physnames. */
9885 compute_delayed_physnames (cu);
9886
9887 if (cu->language == language_rust)
9888 rust_union_quirks (cu);
9889
9890 /* Some compilers don't define a DW_AT_high_pc attribute for the
9891 compilation unit. If the DW_AT_high_pc is missing, synthesize
9892 it, by scanning the DIE's below the compilation unit. */
9893 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9894
9895 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9896 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9897
9898 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9899 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9900 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9901 addrmap to help ensure it has an accurate map of pc values belonging to
9902 this comp unit. */
9903 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9904
9905 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9906 SECT_OFF_TEXT (objfile),
9907 0);
9908
9909 if (cust != NULL)
9910 {
9911 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9912
9913 /* Set symtab language to language from DW_AT_language. If the
9914 compilation is from a C file generated by language preprocessors, do
9915 not set the language if it was already deduced by start_subfile. */
9916 if (!(cu->language == language_c
9917 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9918 COMPUNIT_FILETABS (cust)->language = cu->language;
9919
9920 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9921 produce DW_AT_location with location lists but it can be possibly
9922 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9923 there were bugs in prologue debug info, fixed later in GCC-4.5
9924 by "unwind info for epilogues" patch (which is not directly related).
9925
9926 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9927 needed, it would be wrong due to missing DW_AT_producer there.
9928
9929 Still one can confuse GDB by using non-standard GCC compilation
9930 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9931 */
9932 if (cu->has_loclist && gcc_4_minor >= 5)
9933 cust->locations_valid = 1;
9934
9935 if (gcc_4_minor >= 5)
9936 cust->epilogue_unwind_valid = 1;
9937
9938 cust->call_site_htab = cu->call_site_htab;
9939 }
9940
9941 per_objfile->set_symtab (cu->per_cu, cust);
9942
9943 /* Push it for inclusion processing later. */
9944 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9945
9946 /* Not needed any more. */
9947 cu->reset_builder ();
9948 }
9949
9950 /* Generate full symbol information for type unit CU, whose DIEs have
9951 already been loaded into memory. */
9952
9953 static void
9954 process_full_type_unit (dwarf2_cu *cu,
9955 enum language pretend_language)
9956 {
9957 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9958 struct objfile *objfile = per_objfile->objfile;
9959 struct compunit_symtab *cust;
9960 struct signatured_type *sig_type;
9961
9962 gdb_assert (cu->per_cu->is_debug_types);
9963 sig_type = (struct signatured_type *) cu->per_cu;
9964
9965 /* Clear the list here in case something was left over. */
9966 cu->method_list.clear ();
9967
9968 cu->language = pretend_language;
9969 cu->language_defn = language_def (cu->language);
9970
9971 /* The symbol tables are set up in read_type_unit_scope. */
9972 process_die (cu->dies, cu);
9973
9974 /* For now fudge the Go package. */
9975 if (cu->language == language_go)
9976 fixup_go_packaging (cu);
9977
9978 /* Now that we have processed all the DIEs in the CU, all the types
9979 should be complete, and it should now be safe to compute all of the
9980 physnames. */
9981 compute_delayed_physnames (cu);
9982
9983 if (cu->language == language_rust)
9984 rust_union_quirks (cu);
9985
9986 /* TUs share symbol tables.
9987 If this is the first TU to use this symtab, complete the construction
9988 of it with end_expandable_symtab. Otherwise, complete the addition of
9989 this TU's symbols to the existing symtab. */
9990 type_unit_group_unshareable *tug_unshare =
9991 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9992 if (tug_unshare->compunit_symtab == NULL)
9993 {
9994 buildsym_compunit *builder = cu->get_builder ();
9995 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9996 tug_unshare->compunit_symtab = cust;
9997
9998 if (cust != NULL)
9999 {
10000 /* Set symtab language to language from DW_AT_language. If the
10001 compilation is from a C file generated by language preprocessors,
10002 do not set the language if it was already deduced by
10003 start_subfile. */
10004 if (!(cu->language == language_c
10005 && COMPUNIT_FILETABS (cust)->language != language_c))
10006 COMPUNIT_FILETABS (cust)->language = cu->language;
10007 }
10008 }
10009 else
10010 {
10011 cu->get_builder ()->augment_type_symtab ();
10012 cust = tug_unshare->compunit_symtab;
10013 }
10014
10015 per_objfile->set_symtab (cu->per_cu, cust);
10016
10017 /* Not needed any more. */
10018 cu->reset_builder ();
10019 }
10020
10021 /* Process an imported unit DIE. */
10022
10023 static void
10024 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10025 {
10026 struct attribute *attr;
10027
10028 /* For now we don't handle imported units in type units. */
10029 if (cu->per_cu->is_debug_types)
10030 {
10031 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10032 " supported in type units [in module %s]"),
10033 objfile_name (cu->per_objfile->objfile));
10034 }
10035
10036 attr = dwarf2_attr (die, DW_AT_import, cu);
10037 if (attr != NULL)
10038 {
10039 sect_offset sect_off = attr->get_ref_die_offset ();
10040 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10041 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10042 dwarf2_per_cu_data *per_cu
10043 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
10044
10045 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10046 into another compilation unit, at root level. Regard this as a hint,
10047 and ignore it. */
10048 if (die->parent && die->parent->parent == NULL
10049 && per_cu->unit_type == DW_UT_compile
10050 && per_cu->lang == language_cplus)
10051 return;
10052
10053 /* If necessary, add it to the queue and load its DIEs. */
10054 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
10055 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
10056
10057 cu->per_cu->imported_symtabs_push (per_cu);
10058 }
10059 }
10060
10061 /* RAII object that represents a process_die scope: i.e.,
10062 starts/finishes processing a DIE. */
10063 class process_die_scope
10064 {
10065 public:
10066 process_die_scope (die_info *die, dwarf2_cu *cu)
10067 : m_die (die), m_cu (cu)
10068 {
10069 /* We should only be processing DIEs not already in process. */
10070 gdb_assert (!m_die->in_process);
10071 m_die->in_process = true;
10072 }
10073
10074 ~process_die_scope ()
10075 {
10076 m_die->in_process = false;
10077
10078 /* If we're done processing the DIE for the CU that owns the line
10079 header, we don't need the line header anymore. */
10080 if (m_cu->line_header_die_owner == m_die)
10081 {
10082 delete m_cu->line_header;
10083 m_cu->line_header = NULL;
10084 m_cu->line_header_die_owner = NULL;
10085 }
10086 }
10087
10088 private:
10089 die_info *m_die;
10090 dwarf2_cu *m_cu;
10091 };
10092
10093 /* Process a die and its children. */
10094
10095 static void
10096 process_die (struct die_info *die, struct dwarf2_cu *cu)
10097 {
10098 process_die_scope scope (die, cu);
10099
10100 switch (die->tag)
10101 {
10102 case DW_TAG_padding:
10103 break;
10104 case DW_TAG_compile_unit:
10105 case DW_TAG_partial_unit:
10106 read_file_scope (die, cu);
10107 break;
10108 case DW_TAG_type_unit:
10109 read_type_unit_scope (die, cu);
10110 break;
10111 case DW_TAG_subprogram:
10112 /* Nested subprograms in Fortran get a prefix. */
10113 if (cu->language == language_fortran
10114 && die->parent != NULL
10115 && die->parent->tag == DW_TAG_subprogram)
10116 cu->processing_has_namespace_info = true;
10117 /* Fall through. */
10118 case DW_TAG_inlined_subroutine:
10119 read_func_scope (die, cu);
10120 break;
10121 case DW_TAG_lexical_block:
10122 case DW_TAG_try_block:
10123 case DW_TAG_catch_block:
10124 read_lexical_block_scope (die, cu);
10125 break;
10126 case DW_TAG_call_site:
10127 case DW_TAG_GNU_call_site:
10128 read_call_site_scope (die, cu);
10129 break;
10130 case DW_TAG_class_type:
10131 case DW_TAG_interface_type:
10132 case DW_TAG_structure_type:
10133 case DW_TAG_union_type:
10134 process_structure_scope (die, cu);
10135 break;
10136 case DW_TAG_enumeration_type:
10137 process_enumeration_scope (die, cu);
10138 break;
10139
10140 /* These dies have a type, but processing them does not create
10141 a symbol or recurse to process the children. Therefore we can
10142 read them on-demand through read_type_die. */
10143 case DW_TAG_subroutine_type:
10144 case DW_TAG_set_type:
10145 case DW_TAG_array_type:
10146 case DW_TAG_pointer_type:
10147 case DW_TAG_ptr_to_member_type:
10148 case DW_TAG_reference_type:
10149 case DW_TAG_rvalue_reference_type:
10150 case DW_TAG_string_type:
10151 break;
10152
10153 case DW_TAG_base_type:
10154 case DW_TAG_subrange_type:
10155 case DW_TAG_typedef:
10156 /* Add a typedef symbol for the type definition, if it has a
10157 DW_AT_name. */
10158 new_symbol (die, read_type_die (die, cu), cu);
10159 break;
10160 case DW_TAG_common_block:
10161 read_common_block (die, cu);
10162 break;
10163 case DW_TAG_common_inclusion:
10164 break;
10165 case DW_TAG_namespace:
10166 cu->processing_has_namespace_info = true;
10167 read_namespace (die, cu);
10168 break;
10169 case DW_TAG_module:
10170 cu->processing_has_namespace_info = true;
10171 read_module (die, cu);
10172 break;
10173 case DW_TAG_imported_declaration:
10174 cu->processing_has_namespace_info = true;
10175 if (read_namespace_alias (die, cu))
10176 break;
10177 /* The declaration is not a global namespace alias. */
10178 /* Fall through. */
10179 case DW_TAG_imported_module:
10180 cu->processing_has_namespace_info = true;
10181 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10182 || cu->language != language_fortran))
10183 complaint (_("Tag '%s' has unexpected children"),
10184 dwarf_tag_name (die->tag));
10185 read_import_statement (die, cu);
10186 break;
10187
10188 case DW_TAG_imported_unit:
10189 process_imported_unit_die (die, cu);
10190 break;
10191
10192 case DW_TAG_variable:
10193 read_variable (die, cu);
10194 break;
10195
10196 default:
10197 new_symbol (die, NULL, cu);
10198 break;
10199 }
10200 }
10201 \f
10202 /* DWARF name computation. */
10203
10204 /* A helper function for dwarf2_compute_name which determines whether DIE
10205 needs to have the name of the scope prepended to the name listed in the
10206 die. */
10207
10208 static int
10209 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10210 {
10211 struct attribute *attr;
10212
10213 switch (die->tag)
10214 {
10215 case DW_TAG_namespace:
10216 case DW_TAG_typedef:
10217 case DW_TAG_class_type:
10218 case DW_TAG_interface_type:
10219 case DW_TAG_structure_type:
10220 case DW_TAG_union_type:
10221 case DW_TAG_enumeration_type:
10222 case DW_TAG_enumerator:
10223 case DW_TAG_subprogram:
10224 case DW_TAG_inlined_subroutine:
10225 case DW_TAG_member:
10226 case DW_TAG_imported_declaration:
10227 return 1;
10228
10229 case DW_TAG_variable:
10230 case DW_TAG_constant:
10231 /* We only need to prefix "globally" visible variables. These include
10232 any variable marked with DW_AT_external or any variable that
10233 lives in a namespace. [Variables in anonymous namespaces
10234 require prefixing, but they are not DW_AT_external.] */
10235
10236 if (dwarf2_attr (die, DW_AT_specification, cu))
10237 {
10238 struct dwarf2_cu *spec_cu = cu;
10239
10240 return die_needs_namespace (die_specification (die, &spec_cu),
10241 spec_cu);
10242 }
10243
10244 attr = dwarf2_attr (die, DW_AT_external, cu);
10245 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10246 && die->parent->tag != DW_TAG_module)
10247 return 0;
10248 /* A variable in a lexical block of some kind does not need a
10249 namespace, even though in C++ such variables may be external
10250 and have a mangled name. */
10251 if (die->parent->tag == DW_TAG_lexical_block
10252 || die->parent->tag == DW_TAG_try_block
10253 || die->parent->tag == DW_TAG_catch_block
10254 || die->parent->tag == DW_TAG_subprogram)
10255 return 0;
10256 return 1;
10257
10258 default:
10259 return 0;
10260 }
10261 }
10262
10263 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10264 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10265 defined for the given DIE. */
10266
10267 static struct attribute *
10268 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10269 {
10270 struct attribute *attr;
10271
10272 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10273 if (attr == NULL)
10274 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10275
10276 return attr;
10277 }
10278
10279 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10280 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10281 defined for the given DIE. */
10282
10283 static const char *
10284 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10285 {
10286 const char *linkage_name;
10287
10288 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10289 if (linkage_name == NULL)
10290 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10291
10292 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10293 See https://github.com/rust-lang/rust/issues/32925. */
10294 if (cu->language == language_rust && linkage_name != NULL
10295 && strchr (linkage_name, '{') != NULL)
10296 linkage_name = NULL;
10297
10298 return linkage_name;
10299 }
10300
10301 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10302 compute the physname for the object, which include a method's:
10303 - formal parameters (C++),
10304 - receiver type (Go),
10305
10306 The term "physname" is a bit confusing.
10307 For C++, for example, it is the demangled name.
10308 For Go, for example, it's the mangled name.
10309
10310 For Ada, return the DIE's linkage name rather than the fully qualified
10311 name. PHYSNAME is ignored..
10312
10313 The result is allocated on the objfile->per_bfd's obstack and
10314 canonicalized. */
10315
10316 static const char *
10317 dwarf2_compute_name (const char *name,
10318 struct die_info *die, struct dwarf2_cu *cu,
10319 int physname)
10320 {
10321 struct objfile *objfile = cu->per_objfile->objfile;
10322
10323 if (name == NULL)
10324 name = dwarf2_name (die, cu);
10325
10326 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10327 but otherwise compute it by typename_concat inside GDB.
10328 FIXME: Actually this is not really true, or at least not always true.
10329 It's all very confusing. compute_and_set_names doesn't try to demangle
10330 Fortran names because there is no mangling standard. So new_symbol
10331 will set the demangled name to the result of dwarf2_full_name, and it is
10332 the demangled name that GDB uses if it exists. */
10333 if (cu->language == language_ada
10334 || (cu->language == language_fortran && physname))
10335 {
10336 /* For Ada unit, we prefer the linkage name over the name, as
10337 the former contains the exported name, which the user expects
10338 to be able to reference. Ideally, we want the user to be able
10339 to reference this entity using either natural or linkage name,
10340 but we haven't started looking at this enhancement yet. */
10341 const char *linkage_name = dw2_linkage_name (die, cu);
10342
10343 if (linkage_name != NULL)
10344 return linkage_name;
10345 }
10346
10347 /* These are the only languages we know how to qualify names in. */
10348 if (name != NULL
10349 && (cu->language == language_cplus
10350 || cu->language == language_fortran || cu->language == language_d
10351 || cu->language == language_rust))
10352 {
10353 if (die_needs_namespace (die, cu))
10354 {
10355 const char *prefix;
10356 const char *canonical_name = NULL;
10357
10358 string_file buf;
10359
10360 prefix = determine_prefix (die, cu);
10361 if (*prefix != '\0')
10362 {
10363 gdb::unique_xmalloc_ptr<char> prefixed_name
10364 (typename_concat (NULL, prefix, name, physname, cu));
10365
10366 buf.puts (prefixed_name.get ());
10367 }
10368 else
10369 buf.puts (name);
10370
10371 /* Template parameters may be specified in the DIE's DW_AT_name, or
10372 as children with DW_TAG_template_type_param or
10373 DW_TAG_value_type_param. If the latter, add them to the name
10374 here. If the name already has template parameters, then
10375 skip this step; some versions of GCC emit both, and
10376 it is more efficient to use the pre-computed name.
10377
10378 Something to keep in mind about this process: it is very
10379 unlikely, or in some cases downright impossible, to produce
10380 something that will match the mangled name of a function.
10381 If the definition of the function has the same debug info,
10382 we should be able to match up with it anyway. But fallbacks
10383 using the minimal symbol, for instance to find a method
10384 implemented in a stripped copy of libstdc++, will not work.
10385 If we do not have debug info for the definition, we will have to
10386 match them up some other way.
10387
10388 When we do name matching there is a related problem with function
10389 templates; two instantiated function templates are allowed to
10390 differ only by their return types, which we do not add here. */
10391
10392 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10393 {
10394 struct attribute *attr;
10395 struct die_info *child;
10396 int first = 1;
10397
10398 die->building_fullname = 1;
10399
10400 for (child = die->child; child != NULL; child = child->sibling)
10401 {
10402 struct type *type;
10403 LONGEST value;
10404 const gdb_byte *bytes;
10405 struct dwarf2_locexpr_baton *baton;
10406 struct value *v;
10407
10408 if (child->tag != DW_TAG_template_type_param
10409 && child->tag != DW_TAG_template_value_param)
10410 continue;
10411
10412 if (first)
10413 {
10414 buf.puts ("<");
10415 first = 0;
10416 }
10417 else
10418 buf.puts (", ");
10419
10420 attr = dwarf2_attr (child, DW_AT_type, cu);
10421 if (attr == NULL)
10422 {
10423 complaint (_("template parameter missing DW_AT_type"));
10424 buf.puts ("UNKNOWN_TYPE");
10425 continue;
10426 }
10427 type = die_type (child, cu);
10428
10429 if (child->tag == DW_TAG_template_type_param)
10430 {
10431 c_print_type (type, "", &buf, -1, 0, cu->language,
10432 &type_print_raw_options);
10433 continue;
10434 }
10435
10436 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10437 if (attr == NULL)
10438 {
10439 complaint (_("template parameter missing "
10440 "DW_AT_const_value"));
10441 buf.puts ("UNKNOWN_VALUE");
10442 continue;
10443 }
10444
10445 dwarf2_const_value_attr (attr, type, name,
10446 &cu->comp_unit_obstack, cu,
10447 &value, &bytes, &baton);
10448
10449 if (TYPE_NOSIGN (type))
10450 /* GDB prints characters as NUMBER 'CHAR'. If that's
10451 changed, this can use value_print instead. */
10452 c_printchar (value, type, &buf);
10453 else
10454 {
10455 struct value_print_options opts;
10456
10457 if (baton != NULL)
10458 v = dwarf2_evaluate_loc_desc (type, NULL,
10459 baton->data,
10460 baton->size,
10461 baton->per_cu,
10462 baton->per_objfile);
10463 else if (bytes != NULL)
10464 {
10465 v = allocate_value (type);
10466 memcpy (value_contents_writeable (v), bytes,
10467 TYPE_LENGTH (type));
10468 }
10469 else
10470 v = value_from_longest (type, value);
10471
10472 /* Specify decimal so that we do not depend on
10473 the radix. */
10474 get_formatted_print_options (&opts, 'd');
10475 opts.raw = 1;
10476 value_print (v, &buf, &opts);
10477 release_value (v);
10478 }
10479 }
10480
10481 die->building_fullname = 0;
10482
10483 if (!first)
10484 {
10485 /* Close the argument list, with a space if necessary
10486 (nested templates). */
10487 if (!buf.empty () && buf.string ().back () == '>')
10488 buf.puts (" >");
10489 else
10490 buf.puts (">");
10491 }
10492 }
10493
10494 /* For C++ methods, append formal parameter type
10495 information, if PHYSNAME. */
10496
10497 if (physname && die->tag == DW_TAG_subprogram
10498 && cu->language == language_cplus)
10499 {
10500 struct type *type = read_type_die (die, cu);
10501
10502 c_type_print_args (type, &buf, 1, cu->language,
10503 &type_print_raw_options);
10504
10505 if (cu->language == language_cplus)
10506 {
10507 /* Assume that an artificial first parameter is
10508 "this", but do not crash if it is not. RealView
10509 marks unnamed (and thus unused) parameters as
10510 artificial; there is no way to differentiate
10511 the two cases. */
10512 if (type->num_fields () > 0
10513 && TYPE_FIELD_ARTIFICIAL (type, 0)
10514 && type->field (0).type ()->code () == TYPE_CODE_PTR
10515 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10516 buf.puts (" const");
10517 }
10518 }
10519
10520 const std::string &intermediate_name = buf.string ();
10521
10522 if (cu->language == language_cplus)
10523 canonical_name
10524 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10525 objfile);
10526
10527 /* If we only computed INTERMEDIATE_NAME, or if
10528 INTERMEDIATE_NAME is already canonical, then we need to
10529 intern it. */
10530 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10531 name = objfile->intern (intermediate_name);
10532 else
10533 name = canonical_name;
10534 }
10535 }
10536
10537 return name;
10538 }
10539
10540 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10541 If scope qualifiers are appropriate they will be added. The result
10542 will be allocated on the storage_obstack, or NULL if the DIE does
10543 not have a name. NAME may either be from a previous call to
10544 dwarf2_name or NULL.
10545
10546 The output string will be canonicalized (if C++). */
10547
10548 static const char *
10549 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10550 {
10551 return dwarf2_compute_name (name, die, cu, 0);
10552 }
10553
10554 /* Construct a physname for the given DIE in CU. NAME may either be
10555 from a previous call to dwarf2_name or NULL. The result will be
10556 allocated on the objfile_objstack or NULL if the DIE does not have a
10557 name.
10558
10559 The output string will be canonicalized (if C++). */
10560
10561 static const char *
10562 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10563 {
10564 struct objfile *objfile = cu->per_objfile->objfile;
10565 const char *retval, *mangled = NULL, *canon = NULL;
10566 int need_copy = 1;
10567
10568 /* In this case dwarf2_compute_name is just a shortcut not building anything
10569 on its own. */
10570 if (!die_needs_namespace (die, cu))
10571 return dwarf2_compute_name (name, die, cu, 1);
10572
10573 if (cu->language != language_rust)
10574 mangled = dw2_linkage_name (die, cu);
10575
10576 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10577 has computed. */
10578 gdb::unique_xmalloc_ptr<char> demangled;
10579 if (mangled != NULL)
10580 {
10581
10582 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10583 {
10584 /* Do nothing (do not demangle the symbol name). */
10585 }
10586 else if (cu->language == language_go)
10587 {
10588 /* This is a lie, but we already lie to the caller new_symbol.
10589 new_symbol assumes we return the mangled name.
10590 This just undoes that lie until things are cleaned up. */
10591 }
10592 else
10593 {
10594 /* Use DMGL_RET_DROP for C++ template functions to suppress
10595 their return type. It is easier for GDB users to search
10596 for such functions as `name(params)' than `long name(params)'.
10597 In such case the minimal symbol names do not match the full
10598 symbol names but for template functions there is never a need
10599 to look up their definition from their declaration so
10600 the only disadvantage remains the minimal symbol variant
10601 `long name(params)' does not have the proper inferior type. */
10602 demangled.reset (gdb_demangle (mangled,
10603 (DMGL_PARAMS | DMGL_ANSI
10604 | DMGL_RET_DROP)));
10605 }
10606 if (demangled)
10607 canon = demangled.get ();
10608 else
10609 {
10610 canon = mangled;
10611 need_copy = 0;
10612 }
10613 }
10614
10615 if (canon == NULL || check_physname)
10616 {
10617 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10618
10619 if (canon != NULL && strcmp (physname, canon) != 0)
10620 {
10621 /* It may not mean a bug in GDB. The compiler could also
10622 compute DW_AT_linkage_name incorrectly. But in such case
10623 GDB would need to be bug-to-bug compatible. */
10624
10625 complaint (_("Computed physname <%s> does not match demangled <%s> "
10626 "(from linkage <%s>) - DIE at %s [in module %s]"),
10627 physname, canon, mangled, sect_offset_str (die->sect_off),
10628 objfile_name (objfile));
10629
10630 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10631 is available here - over computed PHYSNAME. It is safer
10632 against both buggy GDB and buggy compilers. */
10633
10634 retval = canon;
10635 }
10636 else
10637 {
10638 retval = physname;
10639 need_copy = 0;
10640 }
10641 }
10642 else
10643 retval = canon;
10644
10645 if (need_copy)
10646 retval = objfile->intern (retval);
10647
10648 return retval;
10649 }
10650
10651 /* Inspect DIE in CU for a namespace alias. If one exists, record
10652 a new symbol for it.
10653
10654 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10655
10656 static int
10657 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10658 {
10659 struct attribute *attr;
10660
10661 /* If the die does not have a name, this is not a namespace
10662 alias. */
10663 attr = dwarf2_attr (die, DW_AT_name, cu);
10664 if (attr != NULL)
10665 {
10666 int num;
10667 struct die_info *d = die;
10668 struct dwarf2_cu *imported_cu = cu;
10669
10670 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10671 keep inspecting DIEs until we hit the underlying import. */
10672 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10673 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10674 {
10675 attr = dwarf2_attr (d, DW_AT_import, cu);
10676 if (attr == NULL)
10677 break;
10678
10679 d = follow_die_ref (d, attr, &imported_cu);
10680 if (d->tag != DW_TAG_imported_declaration)
10681 break;
10682 }
10683
10684 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10685 {
10686 complaint (_("DIE at %s has too many recursively imported "
10687 "declarations"), sect_offset_str (d->sect_off));
10688 return 0;
10689 }
10690
10691 if (attr != NULL)
10692 {
10693 struct type *type;
10694 sect_offset sect_off = attr->get_ref_die_offset ();
10695
10696 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10697 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10698 {
10699 /* This declaration is a global namespace alias. Add
10700 a symbol for it whose type is the aliased namespace. */
10701 new_symbol (die, type, cu);
10702 return 1;
10703 }
10704 }
10705 }
10706
10707 return 0;
10708 }
10709
10710 /* Return the using directives repository (global or local?) to use in the
10711 current context for CU.
10712
10713 For Ada, imported declarations can materialize renamings, which *may* be
10714 global. However it is impossible (for now?) in DWARF to distinguish
10715 "external" imported declarations and "static" ones. As all imported
10716 declarations seem to be static in all other languages, make them all CU-wide
10717 global only in Ada. */
10718
10719 static struct using_direct **
10720 using_directives (struct dwarf2_cu *cu)
10721 {
10722 if (cu->language == language_ada
10723 && cu->get_builder ()->outermost_context_p ())
10724 return cu->get_builder ()->get_global_using_directives ();
10725 else
10726 return cu->get_builder ()->get_local_using_directives ();
10727 }
10728
10729 /* Read the import statement specified by the given die and record it. */
10730
10731 static void
10732 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10733 {
10734 struct objfile *objfile = cu->per_objfile->objfile;
10735 struct attribute *import_attr;
10736 struct die_info *imported_die, *child_die;
10737 struct dwarf2_cu *imported_cu;
10738 const char *imported_name;
10739 const char *imported_name_prefix;
10740 const char *canonical_name;
10741 const char *import_alias;
10742 const char *imported_declaration = NULL;
10743 const char *import_prefix;
10744 std::vector<const char *> excludes;
10745
10746 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10747 if (import_attr == NULL)
10748 {
10749 complaint (_("Tag '%s' has no DW_AT_import"),
10750 dwarf_tag_name (die->tag));
10751 return;
10752 }
10753
10754 imported_cu = cu;
10755 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10756 imported_name = dwarf2_name (imported_die, imported_cu);
10757 if (imported_name == NULL)
10758 {
10759 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10760
10761 The import in the following code:
10762 namespace A
10763 {
10764 typedef int B;
10765 }
10766
10767 int main ()
10768 {
10769 using A::B;
10770 B b;
10771 return b;
10772 }
10773
10774 ...
10775 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10776 <52> DW_AT_decl_file : 1
10777 <53> DW_AT_decl_line : 6
10778 <54> DW_AT_import : <0x75>
10779 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10780 <59> DW_AT_name : B
10781 <5b> DW_AT_decl_file : 1
10782 <5c> DW_AT_decl_line : 2
10783 <5d> DW_AT_type : <0x6e>
10784 ...
10785 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10786 <76> DW_AT_byte_size : 4
10787 <77> DW_AT_encoding : 5 (signed)
10788
10789 imports the wrong die ( 0x75 instead of 0x58 ).
10790 This case will be ignored until the gcc bug is fixed. */
10791 return;
10792 }
10793
10794 /* Figure out the local name after import. */
10795 import_alias = dwarf2_name (die, cu);
10796
10797 /* Figure out where the statement is being imported to. */
10798 import_prefix = determine_prefix (die, cu);
10799
10800 /* Figure out what the scope of the imported die is and prepend it
10801 to the name of the imported die. */
10802 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10803
10804 if (imported_die->tag != DW_TAG_namespace
10805 && imported_die->tag != DW_TAG_module)
10806 {
10807 imported_declaration = imported_name;
10808 canonical_name = imported_name_prefix;
10809 }
10810 else if (strlen (imported_name_prefix) > 0)
10811 canonical_name = obconcat (&objfile->objfile_obstack,
10812 imported_name_prefix,
10813 (cu->language == language_d ? "." : "::"),
10814 imported_name, (char *) NULL);
10815 else
10816 canonical_name = imported_name;
10817
10818 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10819 for (child_die = die->child; child_die && child_die->tag;
10820 child_die = child_die->sibling)
10821 {
10822 /* DWARF-4: A Fortran use statement with a “rename list” may be
10823 represented by an imported module entry with an import attribute
10824 referring to the module and owned entries corresponding to those
10825 entities that are renamed as part of being imported. */
10826
10827 if (child_die->tag != DW_TAG_imported_declaration)
10828 {
10829 complaint (_("child DW_TAG_imported_declaration expected "
10830 "- DIE at %s [in module %s]"),
10831 sect_offset_str (child_die->sect_off),
10832 objfile_name (objfile));
10833 continue;
10834 }
10835
10836 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10837 if (import_attr == NULL)
10838 {
10839 complaint (_("Tag '%s' has no DW_AT_import"),
10840 dwarf_tag_name (child_die->tag));
10841 continue;
10842 }
10843
10844 imported_cu = cu;
10845 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10846 &imported_cu);
10847 imported_name = dwarf2_name (imported_die, imported_cu);
10848 if (imported_name == NULL)
10849 {
10850 complaint (_("child DW_TAG_imported_declaration has unknown "
10851 "imported name - DIE at %s [in module %s]"),
10852 sect_offset_str (child_die->sect_off),
10853 objfile_name (objfile));
10854 continue;
10855 }
10856
10857 excludes.push_back (imported_name);
10858
10859 process_die (child_die, cu);
10860 }
10861
10862 add_using_directive (using_directives (cu),
10863 import_prefix,
10864 canonical_name,
10865 import_alias,
10866 imported_declaration,
10867 excludes,
10868 0,
10869 &objfile->objfile_obstack);
10870 }
10871
10872 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10873 types, but gives them a size of zero. Starting with version 14,
10874 ICC is compatible with GCC. */
10875
10876 static bool
10877 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10878 {
10879 if (!cu->checked_producer)
10880 check_producer (cu);
10881
10882 return cu->producer_is_icc_lt_14;
10883 }
10884
10885 /* ICC generates a DW_AT_type for C void functions. This was observed on
10886 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10887 which says that void functions should not have a DW_AT_type. */
10888
10889 static bool
10890 producer_is_icc (struct dwarf2_cu *cu)
10891 {
10892 if (!cu->checked_producer)
10893 check_producer (cu);
10894
10895 return cu->producer_is_icc;
10896 }
10897
10898 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10899 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10900 this, it was first present in GCC release 4.3.0. */
10901
10902 static bool
10903 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10904 {
10905 if (!cu->checked_producer)
10906 check_producer (cu);
10907
10908 return cu->producer_is_gcc_lt_4_3;
10909 }
10910
10911 static file_and_directory
10912 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10913 {
10914 file_and_directory res;
10915
10916 /* Find the filename. Do not use dwarf2_name here, since the filename
10917 is not a source language identifier. */
10918 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10919 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10920
10921 if (res.comp_dir == NULL
10922 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10923 && IS_ABSOLUTE_PATH (res.name))
10924 {
10925 res.comp_dir_storage = ldirname (res.name);
10926 if (!res.comp_dir_storage.empty ())
10927 res.comp_dir = res.comp_dir_storage.c_str ();
10928 }
10929 if (res.comp_dir != NULL)
10930 {
10931 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10932 directory, get rid of it. */
10933 const char *cp = strchr (res.comp_dir, ':');
10934
10935 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10936 res.comp_dir = cp + 1;
10937 }
10938
10939 if (res.name == NULL)
10940 res.name = "<unknown>";
10941
10942 return res;
10943 }
10944
10945 /* Handle DW_AT_stmt_list for a compilation unit.
10946 DIE is the DW_TAG_compile_unit die for CU.
10947 COMP_DIR is the compilation directory. LOWPC is passed to
10948 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10949
10950 static void
10951 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10952 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10953 {
10954 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10955 struct attribute *attr;
10956 struct line_header line_header_local;
10957 hashval_t line_header_local_hash;
10958 void **slot;
10959 int decode_mapping;
10960
10961 gdb_assert (! cu->per_cu->is_debug_types);
10962
10963 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10964 if (attr == NULL)
10965 return;
10966
10967 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10968
10969 /* The line header hash table is only created if needed (it exists to
10970 prevent redundant reading of the line table for partial_units).
10971 If we're given a partial_unit, we'll need it. If we're given a
10972 compile_unit, then use the line header hash table if it's already
10973 created, but don't create one just yet. */
10974
10975 if (per_objfile->line_header_hash == NULL
10976 && die->tag == DW_TAG_partial_unit)
10977 {
10978 per_objfile->line_header_hash
10979 .reset (htab_create_alloc (127, line_header_hash_voidp,
10980 line_header_eq_voidp,
10981 free_line_header_voidp,
10982 xcalloc, xfree));
10983 }
10984
10985 line_header_local.sect_off = line_offset;
10986 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10987 line_header_local_hash = line_header_hash (&line_header_local);
10988 if (per_objfile->line_header_hash != NULL)
10989 {
10990 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10991 &line_header_local,
10992 line_header_local_hash, NO_INSERT);
10993
10994 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10995 is not present in *SLOT (since if there is something in *SLOT then
10996 it will be for a partial_unit). */
10997 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10998 {
10999 gdb_assert (*slot != NULL);
11000 cu->line_header = (struct line_header *) *slot;
11001 return;
11002 }
11003 }
11004
11005 /* dwarf_decode_line_header does not yet provide sufficient information.
11006 We always have to call also dwarf_decode_lines for it. */
11007 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11008 if (lh == NULL)
11009 return;
11010
11011 cu->line_header = lh.release ();
11012 cu->line_header_die_owner = die;
11013
11014 if (per_objfile->line_header_hash == NULL)
11015 slot = NULL;
11016 else
11017 {
11018 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11019 &line_header_local,
11020 line_header_local_hash, INSERT);
11021 gdb_assert (slot != NULL);
11022 }
11023 if (slot != NULL && *slot == NULL)
11024 {
11025 /* This newly decoded line number information unit will be owned
11026 by line_header_hash hash table. */
11027 *slot = cu->line_header;
11028 cu->line_header_die_owner = NULL;
11029 }
11030 else
11031 {
11032 /* We cannot free any current entry in (*slot) as that struct line_header
11033 may be already used by multiple CUs. Create only temporary decoded
11034 line_header for this CU - it may happen at most once for each line
11035 number information unit. And if we're not using line_header_hash
11036 then this is what we want as well. */
11037 gdb_assert (die->tag != DW_TAG_partial_unit);
11038 }
11039 decode_mapping = (die->tag != DW_TAG_partial_unit);
11040 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11041 decode_mapping);
11042
11043 }
11044
11045 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11046
11047 static void
11048 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11049 {
11050 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11051 struct objfile *objfile = per_objfile->objfile;
11052 struct gdbarch *gdbarch = objfile->arch ();
11053 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11054 CORE_ADDR highpc = ((CORE_ADDR) 0);
11055 struct attribute *attr;
11056 struct die_info *child_die;
11057 CORE_ADDR baseaddr;
11058
11059 prepare_one_comp_unit (cu, die, cu->language);
11060 baseaddr = objfile->text_section_offset ();
11061
11062 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11063
11064 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11065 from finish_block. */
11066 if (lowpc == ((CORE_ADDR) -1))
11067 lowpc = highpc;
11068 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11069
11070 file_and_directory fnd = find_file_and_directory (die, cu);
11071
11072 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11073 standardised yet. As a workaround for the language detection we fall
11074 back to the DW_AT_producer string. */
11075 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11076 cu->language = language_opencl;
11077
11078 /* Similar hack for Go. */
11079 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11080 set_cu_language (DW_LANG_Go, cu);
11081
11082 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11083
11084 /* Decode line number information if present. We do this before
11085 processing child DIEs, so that the line header table is available
11086 for DW_AT_decl_file. */
11087 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11088
11089 /* Process all dies in compilation unit. */
11090 if (die->child != NULL)
11091 {
11092 child_die = die->child;
11093 while (child_die && child_die->tag)
11094 {
11095 process_die (child_die, cu);
11096 child_die = child_die->sibling;
11097 }
11098 }
11099
11100 /* Decode macro information, if present. Dwarf 2 macro information
11101 refers to information in the line number info statement program
11102 header, so we can only read it if we've read the header
11103 successfully. */
11104 attr = dwarf2_attr (die, DW_AT_macros, cu);
11105 if (attr == NULL)
11106 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11107 if (attr && cu->line_header)
11108 {
11109 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11110 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11111
11112 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11113 }
11114 else
11115 {
11116 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11117 if (attr && cu->line_header)
11118 {
11119 unsigned int macro_offset = DW_UNSND (attr);
11120
11121 dwarf_decode_macros (cu, macro_offset, 0);
11122 }
11123 }
11124 }
11125
11126 void
11127 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11128 {
11129 struct type_unit_group *tu_group;
11130 int first_time;
11131 struct attribute *attr;
11132 unsigned int i;
11133 struct signatured_type *sig_type;
11134
11135 gdb_assert (per_cu->is_debug_types);
11136 sig_type = (struct signatured_type *) per_cu;
11137
11138 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11139
11140 /* If we're using .gdb_index (includes -readnow) then
11141 per_cu->type_unit_group may not have been set up yet. */
11142 if (sig_type->type_unit_group == NULL)
11143 sig_type->type_unit_group = get_type_unit_group (this, attr);
11144 tu_group = sig_type->type_unit_group;
11145
11146 /* If we've already processed this stmt_list there's no real need to
11147 do it again, we could fake it and just recreate the part we need
11148 (file name,index -> symtab mapping). If data shows this optimization
11149 is useful we can do it then. */
11150 type_unit_group_unshareable *tug_unshare
11151 = per_objfile->get_type_unit_group_unshareable (tu_group);
11152 first_time = tug_unshare->compunit_symtab == NULL;
11153
11154 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11155 debug info. */
11156 line_header_up lh;
11157 if (attr != NULL)
11158 {
11159 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11160 lh = dwarf_decode_line_header (line_offset, this);
11161 }
11162 if (lh == NULL)
11163 {
11164 if (first_time)
11165 start_symtab ("", NULL, 0);
11166 else
11167 {
11168 gdb_assert (tug_unshare->symtabs == NULL);
11169 gdb_assert (m_builder == nullptr);
11170 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11171 m_builder.reset (new struct buildsym_compunit
11172 (COMPUNIT_OBJFILE (cust), "",
11173 COMPUNIT_DIRNAME (cust),
11174 compunit_language (cust),
11175 0, cust));
11176 list_in_scope = get_builder ()->get_file_symbols ();
11177 }
11178 return;
11179 }
11180
11181 line_header = lh.release ();
11182 line_header_die_owner = die;
11183
11184 if (first_time)
11185 {
11186 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11187
11188 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11189 still initializing it, and our caller (a few levels up)
11190 process_full_type_unit still needs to know if this is the first
11191 time. */
11192
11193 tug_unshare->symtabs
11194 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11195 struct symtab *, line_header->file_names_size ());
11196
11197 auto &file_names = line_header->file_names ();
11198 for (i = 0; i < file_names.size (); ++i)
11199 {
11200 file_entry &fe = file_names[i];
11201 dwarf2_start_subfile (this, fe.name,
11202 fe.include_dir (line_header));
11203 buildsym_compunit *b = get_builder ();
11204 if (b->get_current_subfile ()->symtab == NULL)
11205 {
11206 /* NOTE: start_subfile will recognize when it's been
11207 passed a file it has already seen. So we can't
11208 assume there's a simple mapping from
11209 cu->line_header->file_names to subfiles, plus
11210 cu->line_header->file_names may contain dups. */
11211 b->get_current_subfile ()->symtab
11212 = allocate_symtab (cust, b->get_current_subfile ()->name);
11213 }
11214
11215 fe.symtab = b->get_current_subfile ()->symtab;
11216 tug_unshare->symtabs[i] = fe.symtab;
11217 }
11218 }
11219 else
11220 {
11221 gdb_assert (m_builder == nullptr);
11222 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11223 m_builder.reset (new struct buildsym_compunit
11224 (COMPUNIT_OBJFILE (cust), "",
11225 COMPUNIT_DIRNAME (cust),
11226 compunit_language (cust),
11227 0, cust));
11228 list_in_scope = get_builder ()->get_file_symbols ();
11229
11230 auto &file_names = line_header->file_names ();
11231 for (i = 0; i < file_names.size (); ++i)
11232 {
11233 file_entry &fe = file_names[i];
11234 fe.symtab = tug_unshare->symtabs[i];
11235 }
11236 }
11237
11238 /* The main symtab is allocated last. Type units don't have DW_AT_name
11239 so they don't have a "real" (so to speak) symtab anyway.
11240 There is later code that will assign the main symtab to all symbols
11241 that don't have one. We need to handle the case of a symbol with a
11242 missing symtab (DW_AT_decl_file) anyway. */
11243 }
11244
11245 /* Process DW_TAG_type_unit.
11246 For TUs we want to skip the first top level sibling if it's not the
11247 actual type being defined by this TU. In this case the first top
11248 level sibling is there to provide context only. */
11249
11250 static void
11251 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11252 {
11253 struct die_info *child_die;
11254
11255 prepare_one_comp_unit (cu, die, language_minimal);
11256
11257 /* Initialize (or reinitialize) the machinery for building symtabs.
11258 We do this before processing child DIEs, so that the line header table
11259 is available for DW_AT_decl_file. */
11260 cu->setup_type_unit_groups (die);
11261
11262 if (die->child != NULL)
11263 {
11264 child_die = die->child;
11265 while (child_die && child_die->tag)
11266 {
11267 process_die (child_die, cu);
11268 child_die = child_die->sibling;
11269 }
11270 }
11271 }
11272 \f
11273 /* DWO/DWP files.
11274
11275 http://gcc.gnu.org/wiki/DebugFission
11276 http://gcc.gnu.org/wiki/DebugFissionDWP
11277
11278 To simplify handling of both DWO files ("object" files with the DWARF info)
11279 and DWP files (a file with the DWOs packaged up into one file), we treat
11280 DWP files as having a collection of virtual DWO files. */
11281
11282 static hashval_t
11283 hash_dwo_file (const void *item)
11284 {
11285 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11286 hashval_t hash;
11287
11288 hash = htab_hash_string (dwo_file->dwo_name);
11289 if (dwo_file->comp_dir != NULL)
11290 hash += htab_hash_string (dwo_file->comp_dir);
11291 return hash;
11292 }
11293
11294 static int
11295 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11296 {
11297 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11298 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11299
11300 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11301 return 0;
11302 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11303 return lhs->comp_dir == rhs->comp_dir;
11304 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11305 }
11306
11307 /* Allocate a hash table for DWO files. */
11308
11309 static htab_up
11310 allocate_dwo_file_hash_table ()
11311 {
11312 auto delete_dwo_file = [] (void *item)
11313 {
11314 struct dwo_file *dwo_file = (struct dwo_file *) item;
11315
11316 delete dwo_file;
11317 };
11318
11319 return htab_up (htab_create_alloc (41,
11320 hash_dwo_file,
11321 eq_dwo_file,
11322 delete_dwo_file,
11323 xcalloc, xfree));
11324 }
11325
11326 /* Lookup DWO file DWO_NAME. */
11327
11328 static void **
11329 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11330 const char *dwo_name,
11331 const char *comp_dir)
11332 {
11333 struct dwo_file find_entry;
11334 void **slot;
11335
11336 if (per_objfile->per_bfd->dwo_files == NULL)
11337 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11338
11339 find_entry.dwo_name = dwo_name;
11340 find_entry.comp_dir = comp_dir;
11341 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11342 INSERT);
11343
11344 return slot;
11345 }
11346
11347 static hashval_t
11348 hash_dwo_unit (const void *item)
11349 {
11350 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11351
11352 /* This drops the top 32 bits of the id, but is ok for a hash. */
11353 return dwo_unit->signature;
11354 }
11355
11356 static int
11357 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11358 {
11359 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11360 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11361
11362 /* The signature is assumed to be unique within the DWO file.
11363 So while object file CU dwo_id's always have the value zero,
11364 that's OK, assuming each object file DWO file has only one CU,
11365 and that's the rule for now. */
11366 return lhs->signature == rhs->signature;
11367 }
11368
11369 /* Allocate a hash table for DWO CUs,TUs.
11370 There is one of these tables for each of CUs,TUs for each DWO file. */
11371
11372 static htab_up
11373 allocate_dwo_unit_table ()
11374 {
11375 /* Start out with a pretty small number.
11376 Generally DWO files contain only one CU and maybe some TUs. */
11377 return htab_up (htab_create_alloc (3,
11378 hash_dwo_unit,
11379 eq_dwo_unit,
11380 NULL, xcalloc, xfree));
11381 }
11382
11383 /* die_reader_func for create_dwo_cu. */
11384
11385 static void
11386 create_dwo_cu_reader (const struct die_reader_specs *reader,
11387 const gdb_byte *info_ptr,
11388 struct die_info *comp_unit_die,
11389 struct dwo_file *dwo_file,
11390 struct dwo_unit *dwo_unit)
11391 {
11392 struct dwarf2_cu *cu = reader->cu;
11393 sect_offset sect_off = cu->per_cu->sect_off;
11394 struct dwarf2_section_info *section = cu->per_cu->section;
11395
11396 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11397 if (!signature.has_value ())
11398 {
11399 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11400 " its dwo_id [in module %s]"),
11401 sect_offset_str (sect_off), dwo_file->dwo_name);
11402 return;
11403 }
11404
11405 dwo_unit->dwo_file = dwo_file;
11406 dwo_unit->signature = *signature;
11407 dwo_unit->section = section;
11408 dwo_unit->sect_off = sect_off;
11409 dwo_unit->length = cu->per_cu->length;
11410
11411 if (dwarf_read_debug)
11412 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11413 sect_offset_str (sect_off),
11414 hex_string (dwo_unit->signature));
11415 }
11416
11417 /* Create the dwo_units for the CUs in a DWO_FILE.
11418 Note: This function processes DWO files only, not DWP files. */
11419
11420 static void
11421 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11422 dwarf2_cu *cu, struct dwo_file &dwo_file,
11423 dwarf2_section_info &section, htab_up &cus_htab)
11424 {
11425 struct objfile *objfile = per_objfile->objfile;
11426 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11427 const gdb_byte *info_ptr, *end_ptr;
11428
11429 section.read (objfile);
11430 info_ptr = section.buffer;
11431
11432 if (info_ptr == NULL)
11433 return;
11434
11435 if (dwarf_read_debug)
11436 {
11437 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11438 section.get_name (),
11439 section.get_file_name ());
11440 }
11441
11442 end_ptr = info_ptr + section.size;
11443 while (info_ptr < end_ptr)
11444 {
11445 struct dwarf2_per_cu_data per_cu;
11446 struct dwo_unit read_unit {};
11447 struct dwo_unit *dwo_unit;
11448 void **slot;
11449 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11450
11451 memset (&per_cu, 0, sizeof (per_cu));
11452 per_cu.per_bfd = per_bfd;
11453 per_cu.is_debug_types = 0;
11454 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11455 per_cu.section = &section;
11456
11457 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11458 if (!reader.dummy_p)
11459 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11460 &dwo_file, &read_unit);
11461 info_ptr += per_cu.length;
11462
11463 // If the unit could not be parsed, skip it.
11464 if (read_unit.dwo_file == NULL)
11465 continue;
11466
11467 if (cus_htab == NULL)
11468 cus_htab = allocate_dwo_unit_table ();
11469
11470 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11471 struct dwo_unit);
11472 *dwo_unit = read_unit;
11473 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11474 gdb_assert (slot != NULL);
11475 if (*slot != NULL)
11476 {
11477 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11478 sect_offset dup_sect_off = dup_cu->sect_off;
11479
11480 complaint (_("debug cu entry at offset %s is duplicate to"
11481 " the entry at offset %s, signature %s"),
11482 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11483 hex_string (dwo_unit->signature));
11484 }
11485 *slot = (void *)dwo_unit;
11486 }
11487 }
11488
11489 /* DWP file .debug_{cu,tu}_index section format:
11490 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11491
11492 DWP Version 1:
11493
11494 Both index sections have the same format, and serve to map a 64-bit
11495 signature to a set of section numbers. Each section begins with a header,
11496 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11497 indexes, and a pool of 32-bit section numbers. The index sections will be
11498 aligned at 8-byte boundaries in the file.
11499
11500 The index section header consists of:
11501
11502 V, 32 bit version number
11503 -, 32 bits unused
11504 N, 32 bit number of compilation units or type units in the index
11505 M, 32 bit number of slots in the hash table
11506
11507 Numbers are recorded using the byte order of the application binary.
11508
11509 The hash table begins at offset 16 in the section, and consists of an array
11510 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11511 order of the application binary). Unused slots in the hash table are 0.
11512 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11513
11514 The parallel table begins immediately after the hash table
11515 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11516 array of 32-bit indexes (using the byte order of the application binary),
11517 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11518 table contains a 32-bit index into the pool of section numbers. For unused
11519 hash table slots, the corresponding entry in the parallel table will be 0.
11520
11521 The pool of section numbers begins immediately following the hash table
11522 (at offset 16 + 12 * M from the beginning of the section). The pool of
11523 section numbers consists of an array of 32-bit words (using the byte order
11524 of the application binary). Each item in the array is indexed starting
11525 from 0. The hash table entry provides the index of the first section
11526 number in the set. Additional section numbers in the set follow, and the
11527 set is terminated by a 0 entry (section number 0 is not used in ELF).
11528
11529 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11530 section must be the first entry in the set, and the .debug_abbrev.dwo must
11531 be the second entry. Other members of the set may follow in any order.
11532
11533 ---
11534
11535 DWP Version 2:
11536
11537 DWP Version 2 combines all the .debug_info, etc. sections into one,
11538 and the entries in the index tables are now offsets into these sections.
11539 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11540 section.
11541
11542 Index Section Contents:
11543 Header
11544 Hash Table of Signatures dwp_hash_table.hash_table
11545 Parallel Table of Indices dwp_hash_table.unit_table
11546 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11547 Table of Section Sizes dwp_hash_table.v2.sizes
11548
11549 The index section header consists of:
11550
11551 V, 32 bit version number
11552 L, 32 bit number of columns in the table of section offsets
11553 N, 32 bit number of compilation units or type units in the index
11554 M, 32 bit number of slots in the hash table
11555
11556 Numbers are recorded using the byte order of the application binary.
11557
11558 The hash table has the same format as version 1.
11559 The parallel table of indices has the same format as version 1,
11560 except that the entries are origin-1 indices into the table of sections
11561 offsets and the table of section sizes.
11562
11563 The table of offsets begins immediately following the parallel table
11564 (at offset 16 + 12 * M from the beginning of the section). The table is
11565 a two-dimensional array of 32-bit words (using the byte order of the
11566 application binary), with L columns and N+1 rows, in row-major order.
11567 Each row in the array is indexed starting from 0. The first row provides
11568 a key to the remaining rows: each column in this row provides an identifier
11569 for a debug section, and the offsets in the same column of subsequent rows
11570 refer to that section. The section identifiers are:
11571
11572 DW_SECT_INFO 1 .debug_info.dwo
11573 DW_SECT_TYPES 2 .debug_types.dwo
11574 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11575 DW_SECT_LINE 4 .debug_line.dwo
11576 DW_SECT_LOC 5 .debug_loc.dwo
11577 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11578 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11579 DW_SECT_MACRO 8 .debug_macro.dwo
11580
11581 The offsets provided by the CU and TU index sections are the base offsets
11582 for the contributions made by each CU or TU to the corresponding section
11583 in the package file. Each CU and TU header contains an abbrev_offset
11584 field, used to find the abbreviations table for that CU or TU within the
11585 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11586 be interpreted as relative to the base offset given in the index section.
11587 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11588 should be interpreted as relative to the base offset for .debug_line.dwo,
11589 and offsets into other debug sections obtained from DWARF attributes should
11590 also be interpreted as relative to the corresponding base offset.
11591
11592 The table of sizes begins immediately following the table of offsets.
11593 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11594 with L columns and N rows, in row-major order. Each row in the array is
11595 indexed starting from 1 (row 0 is shared by the two tables).
11596
11597 ---
11598
11599 Hash table lookup is handled the same in version 1 and 2:
11600
11601 We assume that N and M will not exceed 2^32 - 1.
11602 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11603
11604 Given a 64-bit compilation unit signature or a type signature S, an entry
11605 in the hash table is located as follows:
11606
11607 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11608 the low-order k bits all set to 1.
11609
11610 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11611
11612 3) If the hash table entry at index H matches the signature, use that
11613 entry. If the hash table entry at index H is unused (all zeroes),
11614 terminate the search: the signature is not present in the table.
11615
11616 4) Let H = (H + H') modulo M. Repeat at Step 3.
11617
11618 Because M > N and H' and M are relatively prime, the search is guaranteed
11619 to stop at an unused slot or find the match. */
11620
11621 /* Create a hash table to map DWO IDs to their CU/TU entry in
11622 .debug_{info,types}.dwo in DWP_FILE.
11623 Returns NULL if there isn't one.
11624 Note: This function processes DWP files only, not DWO files. */
11625
11626 static struct dwp_hash_table *
11627 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11628 struct dwp_file *dwp_file, int is_debug_types)
11629 {
11630 struct objfile *objfile = per_objfile->objfile;
11631 bfd *dbfd = dwp_file->dbfd.get ();
11632 const gdb_byte *index_ptr, *index_end;
11633 struct dwarf2_section_info *index;
11634 uint32_t version, nr_columns, nr_units, nr_slots;
11635 struct dwp_hash_table *htab;
11636
11637 if (is_debug_types)
11638 index = &dwp_file->sections.tu_index;
11639 else
11640 index = &dwp_file->sections.cu_index;
11641
11642 if (index->empty ())
11643 return NULL;
11644 index->read (objfile);
11645
11646 index_ptr = index->buffer;
11647 index_end = index_ptr + index->size;
11648
11649 version = read_4_bytes (dbfd, index_ptr);
11650 index_ptr += 4;
11651 if (version == 2)
11652 nr_columns = read_4_bytes (dbfd, index_ptr);
11653 else
11654 nr_columns = 0;
11655 index_ptr += 4;
11656 nr_units = read_4_bytes (dbfd, index_ptr);
11657 index_ptr += 4;
11658 nr_slots = read_4_bytes (dbfd, index_ptr);
11659 index_ptr += 4;
11660
11661 if (version != 1 && version != 2)
11662 {
11663 error (_("Dwarf Error: unsupported DWP file version (%s)"
11664 " [in module %s]"),
11665 pulongest (version), dwp_file->name);
11666 }
11667 if (nr_slots != (nr_slots & -nr_slots))
11668 {
11669 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11670 " is not power of 2 [in module %s]"),
11671 pulongest (nr_slots), dwp_file->name);
11672 }
11673
11674 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11675 htab->version = version;
11676 htab->nr_columns = nr_columns;
11677 htab->nr_units = nr_units;
11678 htab->nr_slots = nr_slots;
11679 htab->hash_table = index_ptr;
11680 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11681
11682 /* Exit early if the table is empty. */
11683 if (nr_slots == 0 || nr_units == 0
11684 || (version == 2 && nr_columns == 0))
11685 {
11686 /* All must be zero. */
11687 if (nr_slots != 0 || nr_units != 0
11688 || (version == 2 && nr_columns != 0))
11689 {
11690 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11691 " all zero [in modules %s]"),
11692 dwp_file->name);
11693 }
11694 return htab;
11695 }
11696
11697 if (version == 1)
11698 {
11699 htab->section_pool.v1.indices =
11700 htab->unit_table + sizeof (uint32_t) * nr_slots;
11701 /* It's harder to decide whether the section is too small in v1.
11702 V1 is deprecated anyway so we punt. */
11703 }
11704 else
11705 {
11706 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11707 int *ids = htab->section_pool.v2.section_ids;
11708 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11709 /* Reverse map for error checking. */
11710 int ids_seen[DW_SECT_MAX + 1];
11711 int i;
11712
11713 if (nr_columns < 2)
11714 {
11715 error (_("Dwarf Error: bad DWP hash table, too few columns"
11716 " in section table [in module %s]"),
11717 dwp_file->name);
11718 }
11719 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11720 {
11721 error (_("Dwarf Error: bad DWP hash table, too many columns"
11722 " in section table [in module %s]"),
11723 dwp_file->name);
11724 }
11725 memset (ids, 255, sizeof_ids);
11726 memset (ids_seen, 255, sizeof (ids_seen));
11727 for (i = 0; i < nr_columns; ++i)
11728 {
11729 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11730
11731 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11732 {
11733 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11734 " in section table [in module %s]"),
11735 id, dwp_file->name);
11736 }
11737 if (ids_seen[id] != -1)
11738 {
11739 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11740 " id %d in section table [in module %s]"),
11741 id, dwp_file->name);
11742 }
11743 ids_seen[id] = i;
11744 ids[i] = id;
11745 }
11746 /* Must have exactly one info or types section. */
11747 if (((ids_seen[DW_SECT_INFO] != -1)
11748 + (ids_seen[DW_SECT_TYPES] != -1))
11749 != 1)
11750 {
11751 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11752 " DWO info/types section [in module %s]"),
11753 dwp_file->name);
11754 }
11755 /* Must have an abbrev section. */
11756 if (ids_seen[DW_SECT_ABBREV] == -1)
11757 {
11758 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11759 " section [in module %s]"),
11760 dwp_file->name);
11761 }
11762 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11763 htab->section_pool.v2.sizes =
11764 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11765 * nr_units * nr_columns);
11766 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11767 * nr_units * nr_columns))
11768 > index_end)
11769 {
11770 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11771 " [in module %s]"),
11772 dwp_file->name);
11773 }
11774 }
11775
11776 return htab;
11777 }
11778
11779 /* Update SECTIONS with the data from SECTP.
11780
11781 This function is like the other "locate" section routines that are
11782 passed to bfd_map_over_sections, but in this context the sections to
11783 read comes from the DWP V1 hash table, not the full ELF section table.
11784
11785 The result is non-zero for success, or zero if an error was found. */
11786
11787 static int
11788 locate_v1_virtual_dwo_sections (asection *sectp,
11789 struct virtual_v1_dwo_sections *sections)
11790 {
11791 const struct dwop_section_names *names = &dwop_section_names;
11792
11793 if (section_is_p (sectp->name, &names->abbrev_dwo))
11794 {
11795 /* There can be only one. */
11796 if (sections->abbrev.s.section != NULL)
11797 return 0;
11798 sections->abbrev.s.section = sectp;
11799 sections->abbrev.size = bfd_section_size (sectp);
11800 }
11801 else if (section_is_p (sectp->name, &names->info_dwo)
11802 || section_is_p (sectp->name, &names->types_dwo))
11803 {
11804 /* There can be only one. */
11805 if (sections->info_or_types.s.section != NULL)
11806 return 0;
11807 sections->info_or_types.s.section = sectp;
11808 sections->info_or_types.size = bfd_section_size (sectp);
11809 }
11810 else if (section_is_p (sectp->name, &names->line_dwo))
11811 {
11812 /* There can be only one. */
11813 if (sections->line.s.section != NULL)
11814 return 0;
11815 sections->line.s.section = sectp;
11816 sections->line.size = bfd_section_size (sectp);
11817 }
11818 else if (section_is_p (sectp->name, &names->loc_dwo))
11819 {
11820 /* There can be only one. */
11821 if (sections->loc.s.section != NULL)
11822 return 0;
11823 sections->loc.s.section = sectp;
11824 sections->loc.size = bfd_section_size (sectp);
11825 }
11826 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11827 {
11828 /* There can be only one. */
11829 if (sections->macinfo.s.section != NULL)
11830 return 0;
11831 sections->macinfo.s.section = sectp;
11832 sections->macinfo.size = bfd_section_size (sectp);
11833 }
11834 else if (section_is_p (sectp->name, &names->macro_dwo))
11835 {
11836 /* There can be only one. */
11837 if (sections->macro.s.section != NULL)
11838 return 0;
11839 sections->macro.s.section = sectp;
11840 sections->macro.size = bfd_section_size (sectp);
11841 }
11842 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11843 {
11844 /* There can be only one. */
11845 if (sections->str_offsets.s.section != NULL)
11846 return 0;
11847 sections->str_offsets.s.section = sectp;
11848 sections->str_offsets.size = bfd_section_size (sectp);
11849 }
11850 else
11851 {
11852 /* No other kind of section is valid. */
11853 return 0;
11854 }
11855
11856 return 1;
11857 }
11858
11859 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11860 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11861 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11862 This is for DWP version 1 files. */
11863
11864 static struct dwo_unit *
11865 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
11866 struct dwp_file *dwp_file,
11867 uint32_t unit_index,
11868 const char *comp_dir,
11869 ULONGEST signature, int is_debug_types)
11870 {
11871 const struct dwp_hash_table *dwp_htab =
11872 is_debug_types ? dwp_file->tus : dwp_file->cus;
11873 bfd *dbfd = dwp_file->dbfd.get ();
11874 const char *kind = is_debug_types ? "TU" : "CU";
11875 struct dwo_file *dwo_file;
11876 struct dwo_unit *dwo_unit;
11877 struct virtual_v1_dwo_sections sections;
11878 void **dwo_file_slot;
11879 int i;
11880
11881 gdb_assert (dwp_file->version == 1);
11882
11883 if (dwarf_read_debug)
11884 {
11885 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11886 kind,
11887 pulongest (unit_index), hex_string (signature),
11888 dwp_file->name);
11889 }
11890
11891 /* Fetch the sections of this DWO unit.
11892 Put a limit on the number of sections we look for so that bad data
11893 doesn't cause us to loop forever. */
11894
11895 #define MAX_NR_V1_DWO_SECTIONS \
11896 (1 /* .debug_info or .debug_types */ \
11897 + 1 /* .debug_abbrev */ \
11898 + 1 /* .debug_line */ \
11899 + 1 /* .debug_loc */ \
11900 + 1 /* .debug_str_offsets */ \
11901 + 1 /* .debug_macro or .debug_macinfo */ \
11902 + 1 /* trailing zero */)
11903
11904 memset (&sections, 0, sizeof (sections));
11905
11906 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11907 {
11908 asection *sectp;
11909 uint32_t section_nr =
11910 read_4_bytes (dbfd,
11911 dwp_htab->section_pool.v1.indices
11912 + (unit_index + i) * sizeof (uint32_t));
11913
11914 if (section_nr == 0)
11915 break;
11916 if (section_nr >= dwp_file->num_sections)
11917 {
11918 error (_("Dwarf Error: bad DWP hash table, section number too large"
11919 " [in module %s]"),
11920 dwp_file->name);
11921 }
11922
11923 sectp = dwp_file->elf_sections[section_nr];
11924 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11925 {
11926 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11927 " [in module %s]"),
11928 dwp_file->name);
11929 }
11930 }
11931
11932 if (i < 2
11933 || sections.info_or_types.empty ()
11934 || sections.abbrev.empty ())
11935 {
11936 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11937 " [in module %s]"),
11938 dwp_file->name);
11939 }
11940 if (i == MAX_NR_V1_DWO_SECTIONS)
11941 {
11942 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11943 " [in module %s]"),
11944 dwp_file->name);
11945 }
11946
11947 /* It's easier for the rest of the code if we fake a struct dwo_file and
11948 have dwo_unit "live" in that. At least for now.
11949
11950 The DWP file can be made up of a random collection of CUs and TUs.
11951 However, for each CU + set of TUs that came from the same original DWO
11952 file, we can combine them back into a virtual DWO file to save space
11953 (fewer struct dwo_file objects to allocate). Remember that for really
11954 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11955
11956 std::string virtual_dwo_name =
11957 string_printf ("virtual-dwo/%d-%d-%d-%d",
11958 sections.abbrev.get_id (),
11959 sections.line.get_id (),
11960 sections.loc.get_id (),
11961 sections.str_offsets.get_id ());
11962 /* Can we use an existing virtual DWO file? */
11963 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11964 comp_dir);
11965 /* Create one if necessary. */
11966 if (*dwo_file_slot == NULL)
11967 {
11968 if (dwarf_read_debug)
11969 {
11970 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11971 virtual_dwo_name.c_str ());
11972 }
11973 dwo_file = new struct dwo_file;
11974 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11975 dwo_file->comp_dir = comp_dir;
11976 dwo_file->sections.abbrev = sections.abbrev;
11977 dwo_file->sections.line = sections.line;
11978 dwo_file->sections.loc = sections.loc;
11979 dwo_file->sections.macinfo = sections.macinfo;
11980 dwo_file->sections.macro = sections.macro;
11981 dwo_file->sections.str_offsets = sections.str_offsets;
11982 /* The "str" section is global to the entire DWP file. */
11983 dwo_file->sections.str = dwp_file->sections.str;
11984 /* The info or types section is assigned below to dwo_unit,
11985 there's no need to record it in dwo_file.
11986 Also, we can't simply record type sections in dwo_file because
11987 we record a pointer into the vector in dwo_unit. As we collect more
11988 types we'll grow the vector and eventually have to reallocate space
11989 for it, invalidating all copies of pointers into the previous
11990 contents. */
11991 *dwo_file_slot = dwo_file;
11992 }
11993 else
11994 {
11995 if (dwarf_read_debug)
11996 {
11997 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11998 virtual_dwo_name.c_str ());
11999 }
12000 dwo_file = (struct dwo_file *) *dwo_file_slot;
12001 }
12002
12003 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12004 dwo_unit->dwo_file = dwo_file;
12005 dwo_unit->signature = signature;
12006 dwo_unit->section =
12007 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12008 *dwo_unit->section = sections.info_or_types;
12009 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12010
12011 return dwo_unit;
12012 }
12013
12014 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12015 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12016 piece within that section used by a TU/CU, return a virtual section
12017 of just that piece. */
12018
12019 static struct dwarf2_section_info
12020 create_dwp_v2_section (dwarf2_per_objfile *per_objfile,
12021 struct dwarf2_section_info *section,
12022 bfd_size_type offset, bfd_size_type size)
12023 {
12024 struct dwarf2_section_info result;
12025 asection *sectp;
12026
12027 gdb_assert (section != NULL);
12028 gdb_assert (!section->is_virtual);
12029
12030 memset (&result, 0, sizeof (result));
12031 result.s.containing_section = section;
12032 result.is_virtual = true;
12033
12034 if (size == 0)
12035 return result;
12036
12037 sectp = section->get_bfd_section ();
12038
12039 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12040 bounds of the real section. This is a pretty-rare event, so just
12041 flag an error (easier) instead of a warning and trying to cope. */
12042 if (sectp == NULL
12043 || offset + size > bfd_section_size (sectp))
12044 {
12045 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12046 " in section %s [in module %s]"),
12047 sectp ? bfd_section_name (sectp) : "<unknown>",
12048 objfile_name (per_objfile->objfile));
12049 }
12050
12051 result.virtual_offset = offset;
12052 result.size = size;
12053 return result;
12054 }
12055
12056 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12057 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12058 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12059 This is for DWP version 2 files. */
12060
12061 static struct dwo_unit *
12062 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
12063 struct dwp_file *dwp_file,
12064 uint32_t unit_index,
12065 const char *comp_dir,
12066 ULONGEST signature, int is_debug_types)
12067 {
12068 const struct dwp_hash_table *dwp_htab =
12069 is_debug_types ? dwp_file->tus : dwp_file->cus;
12070 bfd *dbfd = dwp_file->dbfd.get ();
12071 const char *kind = is_debug_types ? "TU" : "CU";
12072 struct dwo_file *dwo_file;
12073 struct dwo_unit *dwo_unit;
12074 struct virtual_v2_dwo_sections sections;
12075 void **dwo_file_slot;
12076 int i;
12077
12078 gdb_assert (dwp_file->version == 2);
12079
12080 if (dwarf_read_debug)
12081 {
12082 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12083 kind,
12084 pulongest (unit_index), hex_string (signature),
12085 dwp_file->name);
12086 }
12087
12088 /* Fetch the section offsets of this DWO unit. */
12089
12090 memset (&sections, 0, sizeof (sections));
12091
12092 for (i = 0; i < dwp_htab->nr_columns; ++i)
12093 {
12094 uint32_t offset = read_4_bytes (dbfd,
12095 dwp_htab->section_pool.v2.offsets
12096 + (((unit_index - 1) * dwp_htab->nr_columns
12097 + i)
12098 * sizeof (uint32_t)));
12099 uint32_t size = read_4_bytes (dbfd,
12100 dwp_htab->section_pool.v2.sizes
12101 + (((unit_index - 1) * dwp_htab->nr_columns
12102 + i)
12103 * sizeof (uint32_t)));
12104
12105 switch (dwp_htab->section_pool.v2.section_ids[i])
12106 {
12107 case DW_SECT_INFO:
12108 case DW_SECT_TYPES:
12109 sections.info_or_types_offset = offset;
12110 sections.info_or_types_size = size;
12111 break;
12112 case DW_SECT_ABBREV:
12113 sections.abbrev_offset = offset;
12114 sections.abbrev_size = size;
12115 break;
12116 case DW_SECT_LINE:
12117 sections.line_offset = offset;
12118 sections.line_size = size;
12119 break;
12120 case DW_SECT_LOC:
12121 sections.loc_offset = offset;
12122 sections.loc_size = size;
12123 break;
12124 case DW_SECT_STR_OFFSETS:
12125 sections.str_offsets_offset = offset;
12126 sections.str_offsets_size = size;
12127 break;
12128 case DW_SECT_MACINFO:
12129 sections.macinfo_offset = offset;
12130 sections.macinfo_size = size;
12131 break;
12132 case DW_SECT_MACRO:
12133 sections.macro_offset = offset;
12134 sections.macro_size = size;
12135 break;
12136 }
12137 }
12138
12139 /* It's easier for the rest of the code if we fake a struct dwo_file and
12140 have dwo_unit "live" in that. At least for now.
12141
12142 The DWP file can be made up of a random collection of CUs and TUs.
12143 However, for each CU + set of TUs that came from the same original DWO
12144 file, we can combine them back into a virtual DWO file to save space
12145 (fewer struct dwo_file objects to allocate). Remember that for really
12146 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12147
12148 std::string virtual_dwo_name =
12149 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12150 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12151 (long) (sections.line_size ? sections.line_offset : 0),
12152 (long) (sections.loc_size ? sections.loc_offset : 0),
12153 (long) (sections.str_offsets_size
12154 ? sections.str_offsets_offset : 0));
12155 /* Can we use an existing virtual DWO file? */
12156 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12157 comp_dir);
12158 /* Create one if necessary. */
12159 if (*dwo_file_slot == NULL)
12160 {
12161 if (dwarf_read_debug)
12162 {
12163 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12164 virtual_dwo_name.c_str ());
12165 }
12166 dwo_file = new struct dwo_file;
12167 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12168 dwo_file->comp_dir = comp_dir;
12169 dwo_file->sections.abbrev =
12170 create_dwp_v2_section (per_objfile, &dwp_file->sections.abbrev,
12171 sections.abbrev_offset, sections.abbrev_size);
12172 dwo_file->sections.line =
12173 create_dwp_v2_section (per_objfile, &dwp_file->sections.line,
12174 sections.line_offset, sections.line_size);
12175 dwo_file->sections.loc =
12176 create_dwp_v2_section (per_objfile, &dwp_file->sections.loc,
12177 sections.loc_offset, sections.loc_size);
12178 dwo_file->sections.macinfo =
12179 create_dwp_v2_section (per_objfile, &dwp_file->sections.macinfo,
12180 sections.macinfo_offset, sections.macinfo_size);
12181 dwo_file->sections.macro =
12182 create_dwp_v2_section (per_objfile, &dwp_file->sections.macro,
12183 sections.macro_offset, sections.macro_size);
12184 dwo_file->sections.str_offsets =
12185 create_dwp_v2_section (per_objfile,
12186 &dwp_file->sections.str_offsets,
12187 sections.str_offsets_offset,
12188 sections.str_offsets_size);
12189 /* The "str" section is global to the entire DWP file. */
12190 dwo_file->sections.str = dwp_file->sections.str;
12191 /* The info or types section is assigned below to dwo_unit,
12192 there's no need to record it in dwo_file.
12193 Also, we can't simply record type sections in dwo_file because
12194 we record a pointer into the vector in dwo_unit. As we collect more
12195 types we'll grow the vector and eventually have to reallocate space
12196 for it, invalidating all copies of pointers into the previous
12197 contents. */
12198 *dwo_file_slot = dwo_file;
12199 }
12200 else
12201 {
12202 if (dwarf_read_debug)
12203 {
12204 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12205 virtual_dwo_name.c_str ());
12206 }
12207 dwo_file = (struct dwo_file *) *dwo_file_slot;
12208 }
12209
12210 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12211 dwo_unit->dwo_file = dwo_file;
12212 dwo_unit->signature = signature;
12213 dwo_unit->section =
12214 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12215 *dwo_unit->section = create_dwp_v2_section (per_objfile,
12216 is_debug_types
12217 ? &dwp_file->sections.types
12218 : &dwp_file->sections.info,
12219 sections.info_or_types_offset,
12220 sections.info_or_types_size);
12221 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12222
12223 return dwo_unit;
12224 }
12225
12226 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12227 Returns NULL if the signature isn't found. */
12228
12229 static struct dwo_unit *
12230 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12231 struct dwp_file *dwp_file, const char *comp_dir,
12232 ULONGEST signature, int is_debug_types)
12233 {
12234 const struct dwp_hash_table *dwp_htab =
12235 is_debug_types ? dwp_file->tus : dwp_file->cus;
12236 bfd *dbfd = dwp_file->dbfd.get ();
12237 uint32_t mask = dwp_htab->nr_slots - 1;
12238 uint32_t hash = signature & mask;
12239 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12240 unsigned int i;
12241 void **slot;
12242 struct dwo_unit find_dwo_cu;
12243
12244 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12245 find_dwo_cu.signature = signature;
12246 slot = htab_find_slot (is_debug_types
12247 ? dwp_file->loaded_tus.get ()
12248 : dwp_file->loaded_cus.get (),
12249 &find_dwo_cu, INSERT);
12250
12251 if (*slot != NULL)
12252 return (struct dwo_unit *) *slot;
12253
12254 /* Use a for loop so that we don't loop forever on bad debug info. */
12255 for (i = 0; i < dwp_htab->nr_slots; ++i)
12256 {
12257 ULONGEST signature_in_table;
12258
12259 signature_in_table =
12260 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12261 if (signature_in_table == signature)
12262 {
12263 uint32_t unit_index =
12264 read_4_bytes (dbfd,
12265 dwp_htab->unit_table + hash * sizeof (uint32_t));
12266
12267 if (dwp_file->version == 1)
12268 {
12269 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12270 unit_index, comp_dir,
12271 signature, is_debug_types);
12272 }
12273 else
12274 {
12275 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12276 unit_index, comp_dir,
12277 signature, is_debug_types);
12278 }
12279 return (struct dwo_unit *) *slot;
12280 }
12281 if (signature_in_table == 0)
12282 return NULL;
12283 hash = (hash + hash2) & mask;
12284 }
12285
12286 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12287 " [in module %s]"),
12288 dwp_file->name);
12289 }
12290
12291 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12292 Open the file specified by FILE_NAME and hand it off to BFD for
12293 preliminary analysis. Return a newly initialized bfd *, which
12294 includes a canonicalized copy of FILE_NAME.
12295 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12296 SEARCH_CWD is true if the current directory is to be searched.
12297 It will be searched before debug-file-directory.
12298 If successful, the file is added to the bfd include table of the
12299 objfile's bfd (see gdb_bfd_record_inclusion).
12300 If unable to find/open the file, return NULL.
12301 NOTE: This function is derived from symfile_bfd_open. */
12302
12303 static gdb_bfd_ref_ptr
12304 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12305 const char *file_name, int is_dwp, int search_cwd)
12306 {
12307 int desc;
12308 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12309 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12310 to debug_file_directory. */
12311 const char *search_path;
12312 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12313
12314 gdb::unique_xmalloc_ptr<char> search_path_holder;
12315 if (search_cwd)
12316 {
12317 if (*debug_file_directory != '\0')
12318 {
12319 search_path_holder.reset (concat (".", dirname_separator_string,
12320 debug_file_directory,
12321 (char *) NULL));
12322 search_path = search_path_holder.get ();
12323 }
12324 else
12325 search_path = ".";
12326 }
12327 else
12328 search_path = debug_file_directory;
12329
12330 openp_flags flags = OPF_RETURN_REALPATH;
12331 if (is_dwp)
12332 flags |= OPF_SEARCH_IN_PATH;
12333
12334 gdb::unique_xmalloc_ptr<char> absolute_name;
12335 desc = openp (search_path, flags, file_name,
12336 O_RDONLY | O_BINARY, &absolute_name);
12337 if (desc < 0)
12338 return NULL;
12339
12340 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12341 gnutarget, desc));
12342 if (sym_bfd == NULL)
12343 return NULL;
12344 bfd_set_cacheable (sym_bfd.get (), 1);
12345
12346 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12347 return NULL;
12348
12349 /* Success. Record the bfd as having been included by the objfile's bfd.
12350 This is important because things like demangled_names_hash lives in the
12351 objfile's per_bfd space and may have references to things like symbol
12352 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12353 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12354
12355 return sym_bfd;
12356 }
12357
12358 /* Try to open DWO file FILE_NAME.
12359 COMP_DIR is the DW_AT_comp_dir attribute.
12360 The result is the bfd handle of the file.
12361 If there is a problem finding or opening the file, return NULL.
12362 Upon success, the canonicalized path of the file is stored in the bfd,
12363 same as symfile_bfd_open. */
12364
12365 static gdb_bfd_ref_ptr
12366 open_dwo_file (dwarf2_per_objfile *per_objfile,
12367 const char *file_name, const char *comp_dir)
12368 {
12369 if (IS_ABSOLUTE_PATH (file_name))
12370 return try_open_dwop_file (per_objfile, file_name,
12371 0 /*is_dwp*/, 0 /*search_cwd*/);
12372
12373 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12374
12375 if (comp_dir != NULL)
12376 {
12377 gdb::unique_xmalloc_ptr<char> path_to_try
12378 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12379
12380 /* NOTE: If comp_dir is a relative path, this will also try the
12381 search path, which seems useful. */
12382 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12383 0 /*is_dwp*/,
12384 1 /*search_cwd*/));
12385 if (abfd != NULL)
12386 return abfd;
12387 }
12388
12389 /* That didn't work, try debug-file-directory, which, despite its name,
12390 is a list of paths. */
12391
12392 if (*debug_file_directory == '\0')
12393 return NULL;
12394
12395 return try_open_dwop_file (per_objfile, file_name,
12396 0 /*is_dwp*/, 1 /*search_cwd*/);
12397 }
12398
12399 /* This function is mapped across the sections and remembers the offset and
12400 size of each of the DWO debugging sections we are interested in. */
12401
12402 static void
12403 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12404 {
12405 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12406 const struct dwop_section_names *names = &dwop_section_names;
12407
12408 if (section_is_p (sectp->name, &names->abbrev_dwo))
12409 {
12410 dwo_sections->abbrev.s.section = sectp;
12411 dwo_sections->abbrev.size = bfd_section_size (sectp);
12412 }
12413 else if (section_is_p (sectp->name, &names->info_dwo))
12414 {
12415 dwo_sections->info.s.section = sectp;
12416 dwo_sections->info.size = bfd_section_size (sectp);
12417 }
12418 else if (section_is_p (sectp->name, &names->line_dwo))
12419 {
12420 dwo_sections->line.s.section = sectp;
12421 dwo_sections->line.size = bfd_section_size (sectp);
12422 }
12423 else if (section_is_p (sectp->name, &names->loc_dwo))
12424 {
12425 dwo_sections->loc.s.section = sectp;
12426 dwo_sections->loc.size = bfd_section_size (sectp);
12427 }
12428 else if (section_is_p (sectp->name, &names->loclists_dwo))
12429 {
12430 dwo_sections->loclists.s.section = sectp;
12431 dwo_sections->loclists.size = bfd_section_size (sectp);
12432 }
12433 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12434 {
12435 dwo_sections->macinfo.s.section = sectp;
12436 dwo_sections->macinfo.size = bfd_section_size (sectp);
12437 }
12438 else if (section_is_p (sectp->name, &names->macro_dwo))
12439 {
12440 dwo_sections->macro.s.section = sectp;
12441 dwo_sections->macro.size = bfd_section_size (sectp);
12442 }
12443 else if (section_is_p (sectp->name, &names->str_dwo))
12444 {
12445 dwo_sections->str.s.section = sectp;
12446 dwo_sections->str.size = bfd_section_size (sectp);
12447 }
12448 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12449 {
12450 dwo_sections->str_offsets.s.section = sectp;
12451 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12452 }
12453 else if (section_is_p (sectp->name, &names->types_dwo))
12454 {
12455 struct dwarf2_section_info type_section;
12456
12457 memset (&type_section, 0, sizeof (type_section));
12458 type_section.s.section = sectp;
12459 type_section.size = bfd_section_size (sectp);
12460 dwo_sections->types.push_back (type_section);
12461 }
12462 }
12463
12464 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12465 by PER_CU. This is for the non-DWP case.
12466 The result is NULL if DWO_NAME can't be found. */
12467
12468 static struct dwo_file *
12469 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12470 const char *comp_dir)
12471 {
12472 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12473
12474 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12475 if (dbfd == NULL)
12476 {
12477 if (dwarf_read_debug)
12478 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12479 return NULL;
12480 }
12481
12482 dwo_file_up dwo_file (new struct dwo_file);
12483 dwo_file->dwo_name = dwo_name;
12484 dwo_file->comp_dir = comp_dir;
12485 dwo_file->dbfd = std::move (dbfd);
12486
12487 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12488 &dwo_file->sections);
12489
12490 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12491 dwo_file->cus);
12492
12493 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12494 dwo_file->sections.types, dwo_file->tus);
12495
12496 if (dwarf_read_debug)
12497 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12498
12499 return dwo_file.release ();
12500 }
12501
12502 /* This function is mapped across the sections and remembers the offset and
12503 size of each of the DWP debugging sections common to version 1 and 2 that
12504 we are interested in. */
12505
12506 static void
12507 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12508 void *dwp_file_ptr)
12509 {
12510 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12511 const struct dwop_section_names *names = &dwop_section_names;
12512 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12513
12514 /* Record the ELF section number for later lookup: this is what the
12515 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12516 gdb_assert (elf_section_nr < dwp_file->num_sections);
12517 dwp_file->elf_sections[elf_section_nr] = sectp;
12518
12519 /* Look for specific sections that we need. */
12520 if (section_is_p (sectp->name, &names->str_dwo))
12521 {
12522 dwp_file->sections.str.s.section = sectp;
12523 dwp_file->sections.str.size = bfd_section_size (sectp);
12524 }
12525 else if (section_is_p (sectp->name, &names->cu_index))
12526 {
12527 dwp_file->sections.cu_index.s.section = sectp;
12528 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12529 }
12530 else if (section_is_p (sectp->name, &names->tu_index))
12531 {
12532 dwp_file->sections.tu_index.s.section = sectp;
12533 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12534 }
12535 }
12536
12537 /* This function is mapped across the sections and remembers the offset and
12538 size of each of the DWP version 2 debugging sections that we are interested
12539 in. This is split into a separate function because we don't know if we
12540 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12541
12542 static void
12543 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12544 {
12545 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12546 const struct dwop_section_names *names = &dwop_section_names;
12547 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12548
12549 /* Record the ELF section number for later lookup: this is what the
12550 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12551 gdb_assert (elf_section_nr < dwp_file->num_sections);
12552 dwp_file->elf_sections[elf_section_nr] = sectp;
12553
12554 /* Look for specific sections that we need. */
12555 if (section_is_p (sectp->name, &names->abbrev_dwo))
12556 {
12557 dwp_file->sections.abbrev.s.section = sectp;
12558 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12559 }
12560 else if (section_is_p (sectp->name, &names->info_dwo))
12561 {
12562 dwp_file->sections.info.s.section = sectp;
12563 dwp_file->sections.info.size = bfd_section_size (sectp);
12564 }
12565 else if (section_is_p (sectp->name, &names->line_dwo))
12566 {
12567 dwp_file->sections.line.s.section = sectp;
12568 dwp_file->sections.line.size = bfd_section_size (sectp);
12569 }
12570 else if (section_is_p (sectp->name, &names->loc_dwo))
12571 {
12572 dwp_file->sections.loc.s.section = sectp;
12573 dwp_file->sections.loc.size = bfd_section_size (sectp);
12574 }
12575 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12576 {
12577 dwp_file->sections.macinfo.s.section = sectp;
12578 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12579 }
12580 else if (section_is_p (sectp->name, &names->macro_dwo))
12581 {
12582 dwp_file->sections.macro.s.section = sectp;
12583 dwp_file->sections.macro.size = bfd_section_size (sectp);
12584 }
12585 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12586 {
12587 dwp_file->sections.str_offsets.s.section = sectp;
12588 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12589 }
12590 else if (section_is_p (sectp->name, &names->types_dwo))
12591 {
12592 dwp_file->sections.types.s.section = sectp;
12593 dwp_file->sections.types.size = bfd_section_size (sectp);
12594 }
12595 }
12596
12597 /* Hash function for dwp_file loaded CUs/TUs. */
12598
12599 static hashval_t
12600 hash_dwp_loaded_cutus (const void *item)
12601 {
12602 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12603
12604 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12605 return dwo_unit->signature;
12606 }
12607
12608 /* Equality function for dwp_file loaded CUs/TUs. */
12609
12610 static int
12611 eq_dwp_loaded_cutus (const void *a, const void *b)
12612 {
12613 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12614 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12615
12616 return dua->signature == dub->signature;
12617 }
12618
12619 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12620
12621 static htab_up
12622 allocate_dwp_loaded_cutus_table ()
12623 {
12624 return htab_up (htab_create_alloc (3,
12625 hash_dwp_loaded_cutus,
12626 eq_dwp_loaded_cutus,
12627 NULL, xcalloc, xfree));
12628 }
12629
12630 /* Try to open DWP file FILE_NAME.
12631 The result is the bfd handle of the file.
12632 If there is a problem finding or opening the file, return NULL.
12633 Upon success, the canonicalized path of the file is stored in the bfd,
12634 same as symfile_bfd_open. */
12635
12636 static gdb_bfd_ref_ptr
12637 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
12638 {
12639 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
12640 1 /*is_dwp*/,
12641 1 /*search_cwd*/));
12642 if (abfd != NULL)
12643 return abfd;
12644
12645 /* Work around upstream bug 15652.
12646 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12647 [Whether that's a "bug" is debatable, but it is getting in our way.]
12648 We have no real idea where the dwp file is, because gdb's realpath-ing
12649 of the executable's path may have discarded the needed info.
12650 [IWBN if the dwp file name was recorded in the executable, akin to
12651 .gnu_debuglink, but that doesn't exist yet.]
12652 Strip the directory from FILE_NAME and search again. */
12653 if (*debug_file_directory != '\0')
12654 {
12655 /* Don't implicitly search the current directory here.
12656 If the user wants to search "." to handle this case,
12657 it must be added to debug-file-directory. */
12658 return try_open_dwop_file (per_objfile, lbasename (file_name),
12659 1 /*is_dwp*/,
12660 0 /*search_cwd*/);
12661 }
12662
12663 return NULL;
12664 }
12665
12666 /* Initialize the use of the DWP file for the current objfile.
12667 By convention the name of the DWP file is ${objfile}.dwp.
12668 The result is NULL if it can't be found. */
12669
12670 static std::unique_ptr<struct dwp_file>
12671 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
12672 {
12673 struct objfile *objfile = per_objfile->objfile;
12674
12675 /* Try to find first .dwp for the binary file before any symbolic links
12676 resolving. */
12677
12678 /* If the objfile is a debug file, find the name of the real binary
12679 file and get the name of dwp file from there. */
12680 std::string dwp_name;
12681 if (objfile->separate_debug_objfile_backlink != NULL)
12682 {
12683 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12684 const char *backlink_basename = lbasename (backlink->original_name);
12685
12686 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12687 }
12688 else
12689 dwp_name = objfile->original_name;
12690
12691 dwp_name += ".dwp";
12692
12693 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
12694 if (dbfd == NULL
12695 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12696 {
12697 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12698 dwp_name = objfile_name (objfile);
12699 dwp_name += ".dwp";
12700 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
12701 }
12702
12703 if (dbfd == NULL)
12704 {
12705 if (dwarf_read_debug)
12706 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12707 return std::unique_ptr<dwp_file> ();
12708 }
12709
12710 const char *name = bfd_get_filename (dbfd.get ());
12711 std::unique_ptr<struct dwp_file> dwp_file
12712 (new struct dwp_file (name, std::move (dbfd)));
12713
12714 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12715 dwp_file->elf_sections =
12716 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
12717 dwp_file->num_sections, asection *);
12718
12719 bfd_map_over_sections (dwp_file->dbfd.get (),
12720 dwarf2_locate_common_dwp_sections,
12721 dwp_file.get ());
12722
12723 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
12724
12725 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
12726
12727 /* The DWP file version is stored in the hash table. Oh well. */
12728 if (dwp_file->cus && dwp_file->tus
12729 && dwp_file->cus->version != dwp_file->tus->version)
12730 {
12731 /* Technically speaking, we should try to limp along, but this is
12732 pretty bizarre. We use pulongest here because that's the established
12733 portability solution (e.g, we cannot use %u for uint32_t). */
12734 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12735 " TU version %s [in DWP file %s]"),
12736 pulongest (dwp_file->cus->version),
12737 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12738 }
12739
12740 if (dwp_file->cus)
12741 dwp_file->version = dwp_file->cus->version;
12742 else if (dwp_file->tus)
12743 dwp_file->version = dwp_file->tus->version;
12744 else
12745 dwp_file->version = 2;
12746
12747 if (dwp_file->version == 2)
12748 bfd_map_over_sections (dwp_file->dbfd.get (),
12749 dwarf2_locate_v2_dwp_sections,
12750 dwp_file.get ());
12751
12752 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12753 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12754
12755 if (dwarf_read_debug)
12756 {
12757 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12758 fprintf_unfiltered (gdb_stdlog,
12759 " %s CUs, %s TUs\n",
12760 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12761 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12762 }
12763
12764 return dwp_file;
12765 }
12766
12767 /* Wrapper around open_and_init_dwp_file, only open it once. */
12768
12769 static struct dwp_file *
12770 get_dwp_file (dwarf2_per_objfile *per_objfile)
12771 {
12772 if (!per_objfile->per_bfd->dwp_checked)
12773 {
12774 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12775 per_objfile->per_bfd->dwp_checked = 1;
12776 }
12777 return per_objfile->per_bfd->dwp_file.get ();
12778 }
12779
12780 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12781 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12782 or in the DWP file for the objfile, referenced by THIS_UNIT.
12783 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12784 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12785
12786 This is called, for example, when wanting to read a variable with a
12787 complex location. Therefore we don't want to do file i/o for every call.
12788 Therefore we don't want to look for a DWO file on every call.
12789 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12790 then we check if we've already seen DWO_NAME, and only THEN do we check
12791 for a DWO file.
12792
12793 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12794 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12795
12796 static struct dwo_unit *
12797 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12798 ULONGEST signature, int is_debug_types)
12799 {
12800 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12801 struct objfile *objfile = per_objfile->objfile;
12802 const char *kind = is_debug_types ? "TU" : "CU";
12803 void **dwo_file_slot;
12804 struct dwo_file *dwo_file;
12805 struct dwp_file *dwp_file;
12806
12807 /* First see if there's a DWP file.
12808 If we have a DWP file but didn't find the DWO inside it, don't
12809 look for the original DWO file. It makes gdb behave differently
12810 depending on whether one is debugging in the build tree. */
12811
12812 dwp_file = get_dwp_file (per_objfile);
12813 if (dwp_file != NULL)
12814 {
12815 const struct dwp_hash_table *dwp_htab =
12816 is_debug_types ? dwp_file->tus : dwp_file->cus;
12817
12818 if (dwp_htab != NULL)
12819 {
12820 struct dwo_unit *dwo_cutu =
12821 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12822 is_debug_types);
12823
12824 if (dwo_cutu != NULL)
12825 {
12826 if (dwarf_read_debug)
12827 {
12828 fprintf_unfiltered (gdb_stdlog,
12829 "Virtual DWO %s %s found: @%s\n",
12830 kind, hex_string (signature),
12831 host_address_to_string (dwo_cutu));
12832 }
12833 return dwo_cutu;
12834 }
12835 }
12836 }
12837 else
12838 {
12839 /* No DWP file, look for the DWO file. */
12840
12841 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
12842 if (*dwo_file_slot == NULL)
12843 {
12844 /* Read in the file and build a table of the CUs/TUs it contains. */
12845 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12846 }
12847 /* NOTE: This will be NULL if unable to open the file. */
12848 dwo_file = (struct dwo_file *) *dwo_file_slot;
12849
12850 if (dwo_file != NULL)
12851 {
12852 struct dwo_unit *dwo_cutu = NULL;
12853
12854 if (is_debug_types && dwo_file->tus)
12855 {
12856 struct dwo_unit find_dwo_cutu;
12857
12858 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12859 find_dwo_cutu.signature = signature;
12860 dwo_cutu
12861 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12862 &find_dwo_cutu);
12863 }
12864 else if (!is_debug_types && dwo_file->cus)
12865 {
12866 struct dwo_unit find_dwo_cutu;
12867
12868 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12869 find_dwo_cutu.signature = signature;
12870 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12871 &find_dwo_cutu);
12872 }
12873
12874 if (dwo_cutu != NULL)
12875 {
12876 if (dwarf_read_debug)
12877 {
12878 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12879 kind, dwo_name, hex_string (signature),
12880 host_address_to_string (dwo_cutu));
12881 }
12882 return dwo_cutu;
12883 }
12884 }
12885 }
12886
12887 /* We didn't find it. This could mean a dwo_id mismatch, or
12888 someone deleted the DWO/DWP file, or the search path isn't set up
12889 correctly to find the file. */
12890
12891 if (dwarf_read_debug)
12892 {
12893 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12894 kind, dwo_name, hex_string (signature));
12895 }
12896
12897 /* This is a warning and not a complaint because it can be caused by
12898 pilot error (e.g., user accidentally deleting the DWO). */
12899 {
12900 /* Print the name of the DWP file if we looked there, helps the user
12901 better diagnose the problem. */
12902 std::string dwp_text;
12903
12904 if (dwp_file != NULL)
12905 dwp_text = string_printf (" [in DWP file %s]",
12906 lbasename (dwp_file->name));
12907
12908 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12909 " [in module %s]"),
12910 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12911 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12912 }
12913 return NULL;
12914 }
12915
12916 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12917 See lookup_dwo_cutu_unit for details. */
12918
12919 static struct dwo_unit *
12920 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12921 ULONGEST signature)
12922 {
12923 gdb_assert (!cu->per_cu->is_debug_types);
12924
12925 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12926 }
12927
12928 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12929 See lookup_dwo_cutu_unit for details. */
12930
12931 static struct dwo_unit *
12932 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12933 {
12934 gdb_assert (cu->per_cu->is_debug_types);
12935
12936 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12937
12938 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12939 }
12940
12941 /* Traversal function for queue_and_load_all_dwo_tus. */
12942
12943 static int
12944 queue_and_load_dwo_tu (void **slot, void *info)
12945 {
12946 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12947 dwarf2_cu *cu = (dwarf2_cu *) info;
12948 ULONGEST signature = dwo_unit->signature;
12949 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12950
12951 if (sig_type != NULL)
12952 {
12953 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12954
12955 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12956 a real dependency of PER_CU on SIG_TYPE. That is detected later
12957 while processing PER_CU. */
12958 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
12959 load_full_type_unit (sig_cu, cu->per_objfile);
12960 cu->per_cu->imported_symtabs_push (sig_cu);
12961 }
12962
12963 return 1;
12964 }
12965
12966 /* Queue all TUs contained in the DWO of CU to be read in.
12967 The DWO may have the only definition of the type, though it may not be
12968 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12969 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12970
12971 static void
12972 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
12973 {
12974 struct dwo_unit *dwo_unit;
12975 struct dwo_file *dwo_file;
12976
12977 gdb_assert (cu != nullptr);
12978 gdb_assert (!cu->per_cu->is_debug_types);
12979 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
12980
12981 dwo_unit = cu->dwo_unit;
12982 gdb_assert (dwo_unit != NULL);
12983
12984 dwo_file = dwo_unit->dwo_file;
12985 if (dwo_file->tus != NULL)
12986 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
12987 }
12988
12989 /* Read in various DIEs. */
12990
12991 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12992 Inherit only the children of the DW_AT_abstract_origin DIE not being
12993 already referenced by DW_AT_abstract_origin from the children of the
12994 current DIE. */
12995
12996 static void
12997 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12998 {
12999 struct die_info *child_die;
13000 sect_offset *offsetp;
13001 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13002 struct die_info *origin_die;
13003 /* Iterator of the ORIGIN_DIE children. */
13004 struct die_info *origin_child_die;
13005 struct attribute *attr;
13006 struct dwarf2_cu *origin_cu;
13007 struct pending **origin_previous_list_in_scope;
13008
13009 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13010 if (!attr)
13011 return;
13012
13013 /* Note that following die references may follow to a die in a
13014 different cu. */
13015
13016 origin_cu = cu;
13017 origin_die = follow_die_ref (die, attr, &origin_cu);
13018
13019 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13020 symbols in. */
13021 origin_previous_list_in_scope = origin_cu->list_in_scope;
13022 origin_cu->list_in_scope = cu->list_in_scope;
13023
13024 if (die->tag != origin_die->tag
13025 && !(die->tag == DW_TAG_inlined_subroutine
13026 && origin_die->tag == DW_TAG_subprogram))
13027 complaint (_("DIE %s and its abstract origin %s have different tags"),
13028 sect_offset_str (die->sect_off),
13029 sect_offset_str (origin_die->sect_off));
13030
13031 std::vector<sect_offset> offsets;
13032
13033 for (child_die = die->child;
13034 child_die && child_die->tag;
13035 child_die = child_die->sibling)
13036 {
13037 struct die_info *child_origin_die;
13038 struct dwarf2_cu *child_origin_cu;
13039
13040 /* We are trying to process concrete instance entries:
13041 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13042 it's not relevant to our analysis here. i.e. detecting DIEs that are
13043 present in the abstract instance but not referenced in the concrete
13044 one. */
13045 if (child_die->tag == DW_TAG_call_site
13046 || child_die->tag == DW_TAG_GNU_call_site)
13047 continue;
13048
13049 /* For each CHILD_DIE, find the corresponding child of
13050 ORIGIN_DIE. If there is more than one layer of
13051 DW_AT_abstract_origin, follow them all; there shouldn't be,
13052 but GCC versions at least through 4.4 generate this (GCC PR
13053 40573). */
13054 child_origin_die = child_die;
13055 child_origin_cu = cu;
13056 while (1)
13057 {
13058 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13059 child_origin_cu);
13060 if (attr == NULL)
13061 break;
13062 child_origin_die = follow_die_ref (child_origin_die, attr,
13063 &child_origin_cu);
13064 }
13065
13066 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13067 counterpart may exist. */
13068 if (child_origin_die != child_die)
13069 {
13070 if (child_die->tag != child_origin_die->tag
13071 && !(child_die->tag == DW_TAG_inlined_subroutine
13072 && child_origin_die->tag == DW_TAG_subprogram))
13073 complaint (_("Child DIE %s and its abstract origin %s have "
13074 "different tags"),
13075 sect_offset_str (child_die->sect_off),
13076 sect_offset_str (child_origin_die->sect_off));
13077 if (child_origin_die->parent != origin_die)
13078 complaint (_("Child DIE %s and its abstract origin %s have "
13079 "different parents"),
13080 sect_offset_str (child_die->sect_off),
13081 sect_offset_str (child_origin_die->sect_off));
13082 else
13083 offsets.push_back (child_origin_die->sect_off);
13084 }
13085 }
13086 std::sort (offsets.begin (), offsets.end ());
13087 sect_offset *offsets_end = offsets.data () + offsets.size ();
13088 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13089 if (offsetp[-1] == *offsetp)
13090 complaint (_("Multiple children of DIE %s refer "
13091 "to DIE %s as their abstract origin"),
13092 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13093
13094 offsetp = offsets.data ();
13095 origin_child_die = origin_die->child;
13096 while (origin_child_die && origin_child_die->tag)
13097 {
13098 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13099 while (offsetp < offsets_end
13100 && *offsetp < origin_child_die->sect_off)
13101 offsetp++;
13102 if (offsetp >= offsets_end
13103 || *offsetp > origin_child_die->sect_off)
13104 {
13105 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13106 Check whether we're already processing ORIGIN_CHILD_DIE.
13107 This can happen with mutually referenced abstract_origins.
13108 PR 16581. */
13109 if (!origin_child_die->in_process)
13110 process_die (origin_child_die, origin_cu);
13111 }
13112 origin_child_die = origin_child_die->sibling;
13113 }
13114 origin_cu->list_in_scope = origin_previous_list_in_scope;
13115
13116 if (cu != origin_cu)
13117 compute_delayed_physnames (origin_cu);
13118 }
13119
13120 static void
13121 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13122 {
13123 struct objfile *objfile = cu->per_objfile->objfile;
13124 struct gdbarch *gdbarch = objfile->arch ();
13125 struct context_stack *newobj;
13126 CORE_ADDR lowpc;
13127 CORE_ADDR highpc;
13128 struct die_info *child_die;
13129 struct attribute *attr, *call_line, *call_file;
13130 const char *name;
13131 CORE_ADDR baseaddr;
13132 struct block *block;
13133 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13134 std::vector<struct symbol *> template_args;
13135 struct template_symbol *templ_func = NULL;
13136
13137 if (inlined_func)
13138 {
13139 /* If we do not have call site information, we can't show the
13140 caller of this inlined function. That's too confusing, so
13141 only use the scope for local variables. */
13142 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13143 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13144 if (call_line == NULL || call_file == NULL)
13145 {
13146 read_lexical_block_scope (die, cu);
13147 return;
13148 }
13149 }
13150
13151 baseaddr = objfile->text_section_offset ();
13152
13153 name = dwarf2_name (die, cu);
13154
13155 /* Ignore functions with missing or empty names. These are actually
13156 illegal according to the DWARF standard. */
13157 if (name == NULL)
13158 {
13159 complaint (_("missing name for subprogram DIE at %s"),
13160 sect_offset_str (die->sect_off));
13161 return;
13162 }
13163
13164 /* Ignore functions with missing or invalid low and high pc attributes. */
13165 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13166 <= PC_BOUNDS_INVALID)
13167 {
13168 attr = dwarf2_attr (die, DW_AT_external, cu);
13169 if (!attr || !DW_UNSND (attr))
13170 complaint (_("cannot get low and high bounds "
13171 "for subprogram DIE at %s"),
13172 sect_offset_str (die->sect_off));
13173 return;
13174 }
13175
13176 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13177 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13178
13179 /* If we have any template arguments, then we must allocate a
13180 different sort of symbol. */
13181 for (child_die = die->child; child_die; child_die = child_die->sibling)
13182 {
13183 if (child_die->tag == DW_TAG_template_type_param
13184 || child_die->tag == DW_TAG_template_value_param)
13185 {
13186 templ_func = new (&objfile->objfile_obstack) template_symbol;
13187 templ_func->subclass = SYMBOL_TEMPLATE;
13188 break;
13189 }
13190 }
13191
13192 newobj = cu->get_builder ()->push_context (0, lowpc);
13193 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13194 (struct symbol *) templ_func);
13195
13196 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13197 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13198 cu->language);
13199
13200 /* If there is a location expression for DW_AT_frame_base, record
13201 it. */
13202 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13203 if (attr != nullptr)
13204 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13205
13206 /* If there is a location for the static link, record it. */
13207 newobj->static_link = NULL;
13208 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13209 if (attr != nullptr)
13210 {
13211 newobj->static_link
13212 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13213 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13214 cu->addr_type ());
13215 }
13216
13217 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13218
13219 if (die->child != NULL)
13220 {
13221 child_die = die->child;
13222 while (child_die && child_die->tag)
13223 {
13224 if (child_die->tag == DW_TAG_template_type_param
13225 || child_die->tag == DW_TAG_template_value_param)
13226 {
13227 struct symbol *arg = new_symbol (child_die, NULL, cu);
13228
13229 if (arg != NULL)
13230 template_args.push_back (arg);
13231 }
13232 else
13233 process_die (child_die, cu);
13234 child_die = child_die->sibling;
13235 }
13236 }
13237
13238 inherit_abstract_dies (die, cu);
13239
13240 /* If we have a DW_AT_specification, we might need to import using
13241 directives from the context of the specification DIE. See the
13242 comment in determine_prefix. */
13243 if (cu->language == language_cplus
13244 && dwarf2_attr (die, DW_AT_specification, cu))
13245 {
13246 struct dwarf2_cu *spec_cu = cu;
13247 struct die_info *spec_die = die_specification (die, &spec_cu);
13248
13249 while (spec_die)
13250 {
13251 child_die = spec_die->child;
13252 while (child_die && child_die->tag)
13253 {
13254 if (child_die->tag == DW_TAG_imported_module)
13255 process_die (child_die, spec_cu);
13256 child_die = child_die->sibling;
13257 }
13258
13259 /* In some cases, GCC generates specification DIEs that
13260 themselves contain DW_AT_specification attributes. */
13261 spec_die = die_specification (spec_die, &spec_cu);
13262 }
13263 }
13264
13265 struct context_stack cstk = cu->get_builder ()->pop_context ();
13266 /* Make a block for the local symbols within. */
13267 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13268 cstk.static_link, lowpc, highpc);
13269
13270 /* For C++, set the block's scope. */
13271 if ((cu->language == language_cplus
13272 || cu->language == language_fortran
13273 || cu->language == language_d
13274 || cu->language == language_rust)
13275 && cu->processing_has_namespace_info)
13276 block_set_scope (block, determine_prefix (die, cu),
13277 &objfile->objfile_obstack);
13278
13279 /* If we have address ranges, record them. */
13280 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13281
13282 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13283
13284 /* Attach template arguments to function. */
13285 if (!template_args.empty ())
13286 {
13287 gdb_assert (templ_func != NULL);
13288
13289 templ_func->n_template_arguments = template_args.size ();
13290 templ_func->template_arguments
13291 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13292 templ_func->n_template_arguments);
13293 memcpy (templ_func->template_arguments,
13294 template_args.data (),
13295 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13296
13297 /* Make sure that the symtab is set on the new symbols. Even
13298 though they don't appear in this symtab directly, other parts
13299 of gdb assume that symbols do, and this is reasonably
13300 true. */
13301 for (symbol *sym : template_args)
13302 symbol_set_symtab (sym, symbol_symtab (templ_func));
13303 }
13304
13305 /* In C++, we can have functions nested inside functions (e.g., when
13306 a function declares a class that has methods). This means that
13307 when we finish processing a function scope, we may need to go
13308 back to building a containing block's symbol lists. */
13309 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13310 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13311
13312 /* If we've finished processing a top-level function, subsequent
13313 symbols go in the file symbol list. */
13314 if (cu->get_builder ()->outermost_context_p ())
13315 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13316 }
13317
13318 /* Process all the DIES contained within a lexical block scope. Start
13319 a new scope, process the dies, and then close the scope. */
13320
13321 static void
13322 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13323 {
13324 struct objfile *objfile = cu->per_objfile->objfile;
13325 struct gdbarch *gdbarch = objfile->arch ();
13326 CORE_ADDR lowpc, highpc;
13327 struct die_info *child_die;
13328 CORE_ADDR baseaddr;
13329
13330 baseaddr = objfile->text_section_offset ();
13331
13332 /* Ignore blocks with missing or invalid low and high pc attributes. */
13333 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13334 as multiple lexical blocks? Handling children in a sane way would
13335 be nasty. Might be easier to properly extend generic blocks to
13336 describe ranges. */
13337 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13338 {
13339 case PC_BOUNDS_NOT_PRESENT:
13340 /* DW_TAG_lexical_block has no attributes, process its children as if
13341 there was no wrapping by that DW_TAG_lexical_block.
13342 GCC does no longer produces such DWARF since GCC r224161. */
13343 for (child_die = die->child;
13344 child_die != NULL && child_die->tag;
13345 child_die = child_die->sibling)
13346 {
13347 /* We might already be processing this DIE. This can happen
13348 in an unusual circumstance -- where a subroutine A
13349 appears lexically in another subroutine B, but A actually
13350 inlines B. The recursion is broken here, rather than in
13351 inherit_abstract_dies, because it seems better to simply
13352 drop concrete children here. */
13353 if (!child_die->in_process)
13354 process_die (child_die, cu);
13355 }
13356 return;
13357 case PC_BOUNDS_INVALID:
13358 return;
13359 }
13360 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13361 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13362
13363 cu->get_builder ()->push_context (0, lowpc);
13364 if (die->child != NULL)
13365 {
13366 child_die = die->child;
13367 while (child_die && child_die->tag)
13368 {
13369 process_die (child_die, cu);
13370 child_die = child_die->sibling;
13371 }
13372 }
13373 inherit_abstract_dies (die, cu);
13374 struct context_stack cstk = cu->get_builder ()->pop_context ();
13375
13376 if (*cu->get_builder ()->get_local_symbols () != NULL
13377 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13378 {
13379 struct block *block
13380 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13381 cstk.start_addr, highpc);
13382
13383 /* Note that recording ranges after traversing children, as we
13384 do here, means that recording a parent's ranges entails
13385 walking across all its children's ranges as they appear in
13386 the address map, which is quadratic behavior.
13387
13388 It would be nicer to record the parent's ranges before
13389 traversing its children, simply overriding whatever you find
13390 there. But since we don't even decide whether to create a
13391 block until after we've traversed its children, that's hard
13392 to do. */
13393 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13394 }
13395 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13396 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13397 }
13398
13399 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13400
13401 static void
13402 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13403 {
13404 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13405 struct objfile *objfile = per_objfile->objfile;
13406 struct gdbarch *gdbarch = objfile->arch ();
13407 CORE_ADDR pc, baseaddr;
13408 struct attribute *attr;
13409 struct call_site *call_site, call_site_local;
13410 void **slot;
13411 int nparams;
13412 struct die_info *child_die;
13413
13414 baseaddr = objfile->text_section_offset ();
13415
13416 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13417 if (attr == NULL)
13418 {
13419 /* This was a pre-DWARF-5 GNU extension alias
13420 for DW_AT_call_return_pc. */
13421 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13422 }
13423 if (!attr)
13424 {
13425 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13426 "DIE %s [in module %s]"),
13427 sect_offset_str (die->sect_off), objfile_name (objfile));
13428 return;
13429 }
13430 pc = attr->value_as_address () + baseaddr;
13431 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13432
13433 if (cu->call_site_htab == NULL)
13434 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13435 NULL, &objfile->objfile_obstack,
13436 hashtab_obstack_allocate, NULL);
13437 call_site_local.pc = pc;
13438 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13439 if (*slot != NULL)
13440 {
13441 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13442 "DIE %s [in module %s]"),
13443 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13444 objfile_name (objfile));
13445 return;
13446 }
13447
13448 /* Count parameters at the caller. */
13449
13450 nparams = 0;
13451 for (child_die = die->child; child_die && child_die->tag;
13452 child_die = child_die->sibling)
13453 {
13454 if (child_die->tag != DW_TAG_call_site_parameter
13455 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13456 {
13457 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13458 "DW_TAG_call_site child DIE %s [in module %s]"),
13459 child_die->tag, sect_offset_str (child_die->sect_off),
13460 objfile_name (objfile));
13461 continue;
13462 }
13463
13464 nparams++;
13465 }
13466
13467 call_site
13468 = ((struct call_site *)
13469 obstack_alloc (&objfile->objfile_obstack,
13470 sizeof (*call_site)
13471 + (sizeof (*call_site->parameter) * (nparams - 1))));
13472 *slot = call_site;
13473 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13474 call_site->pc = pc;
13475
13476 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13477 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13478 {
13479 struct die_info *func_die;
13480
13481 /* Skip also over DW_TAG_inlined_subroutine. */
13482 for (func_die = die->parent;
13483 func_die && func_die->tag != DW_TAG_subprogram
13484 && func_die->tag != DW_TAG_subroutine_type;
13485 func_die = func_die->parent);
13486
13487 /* DW_AT_call_all_calls is a superset
13488 of DW_AT_call_all_tail_calls. */
13489 if (func_die
13490 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13491 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13492 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13493 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13494 {
13495 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13496 not complete. But keep CALL_SITE for look ups via call_site_htab,
13497 both the initial caller containing the real return address PC and
13498 the final callee containing the current PC of a chain of tail
13499 calls do not need to have the tail call list complete. But any
13500 function candidate for a virtual tail call frame searched via
13501 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13502 determined unambiguously. */
13503 }
13504 else
13505 {
13506 struct type *func_type = NULL;
13507
13508 if (func_die)
13509 func_type = get_die_type (func_die, cu);
13510 if (func_type != NULL)
13511 {
13512 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13513
13514 /* Enlist this call site to the function. */
13515 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13516 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13517 }
13518 else
13519 complaint (_("Cannot find function owning DW_TAG_call_site "
13520 "DIE %s [in module %s]"),
13521 sect_offset_str (die->sect_off), objfile_name (objfile));
13522 }
13523 }
13524
13525 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13526 if (attr == NULL)
13527 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13528 if (attr == NULL)
13529 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13530 if (attr == NULL)
13531 {
13532 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13533 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13534 }
13535 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13536 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13537 /* Keep NULL DWARF_BLOCK. */;
13538 else if (attr->form_is_block ())
13539 {
13540 struct dwarf2_locexpr_baton *dlbaton;
13541
13542 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13543 dlbaton->data = DW_BLOCK (attr)->data;
13544 dlbaton->size = DW_BLOCK (attr)->size;
13545 dlbaton->per_objfile = per_objfile;
13546 dlbaton->per_cu = cu->per_cu;
13547
13548 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13549 }
13550 else if (attr->form_is_ref ())
13551 {
13552 struct dwarf2_cu *target_cu = cu;
13553 struct die_info *target_die;
13554
13555 target_die = follow_die_ref (die, attr, &target_cu);
13556 gdb_assert (target_cu->per_objfile->objfile == objfile);
13557 if (die_is_declaration (target_die, target_cu))
13558 {
13559 const char *target_physname;
13560
13561 /* Prefer the mangled name; otherwise compute the demangled one. */
13562 target_physname = dw2_linkage_name (target_die, target_cu);
13563 if (target_physname == NULL)
13564 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13565 if (target_physname == NULL)
13566 complaint (_("DW_AT_call_target target DIE has invalid "
13567 "physname, for referencing DIE %s [in module %s]"),
13568 sect_offset_str (die->sect_off), objfile_name (objfile));
13569 else
13570 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13571 }
13572 else
13573 {
13574 CORE_ADDR lowpc;
13575
13576 /* DW_AT_entry_pc should be preferred. */
13577 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13578 <= PC_BOUNDS_INVALID)
13579 complaint (_("DW_AT_call_target target DIE has invalid "
13580 "low pc, for referencing DIE %s [in module %s]"),
13581 sect_offset_str (die->sect_off), objfile_name (objfile));
13582 else
13583 {
13584 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13585 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13586 }
13587 }
13588 }
13589 else
13590 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13591 "block nor reference, for DIE %s [in module %s]"),
13592 sect_offset_str (die->sect_off), objfile_name (objfile));
13593
13594 call_site->per_cu = cu->per_cu;
13595 call_site->per_objfile = per_objfile;
13596
13597 for (child_die = die->child;
13598 child_die && child_die->tag;
13599 child_die = child_die->sibling)
13600 {
13601 struct call_site_parameter *parameter;
13602 struct attribute *loc, *origin;
13603
13604 if (child_die->tag != DW_TAG_call_site_parameter
13605 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13606 {
13607 /* Already printed the complaint above. */
13608 continue;
13609 }
13610
13611 gdb_assert (call_site->parameter_count < nparams);
13612 parameter = &call_site->parameter[call_site->parameter_count];
13613
13614 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13615 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13616 register is contained in DW_AT_call_value. */
13617
13618 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13619 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13620 if (origin == NULL)
13621 {
13622 /* This was a pre-DWARF-5 GNU extension alias
13623 for DW_AT_call_parameter. */
13624 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13625 }
13626 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13627 {
13628 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13629
13630 sect_offset sect_off = origin->get_ref_die_offset ();
13631 if (!cu->header.offset_in_cu_p (sect_off))
13632 {
13633 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13634 binding can be done only inside one CU. Such referenced DIE
13635 therefore cannot be even moved to DW_TAG_partial_unit. */
13636 complaint (_("DW_AT_call_parameter offset is not in CU for "
13637 "DW_TAG_call_site child DIE %s [in module %s]"),
13638 sect_offset_str (child_die->sect_off),
13639 objfile_name (objfile));
13640 continue;
13641 }
13642 parameter->u.param_cu_off
13643 = (cu_offset) (sect_off - cu->header.sect_off);
13644 }
13645 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13646 {
13647 complaint (_("No DW_FORM_block* DW_AT_location for "
13648 "DW_TAG_call_site child DIE %s [in module %s]"),
13649 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13650 continue;
13651 }
13652 else
13653 {
13654 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13655 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13656 if (parameter->u.dwarf_reg != -1)
13657 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13658 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13659 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13660 &parameter->u.fb_offset))
13661 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13662 else
13663 {
13664 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13665 "for DW_FORM_block* DW_AT_location is supported for "
13666 "DW_TAG_call_site child DIE %s "
13667 "[in module %s]"),
13668 sect_offset_str (child_die->sect_off),
13669 objfile_name (objfile));
13670 continue;
13671 }
13672 }
13673
13674 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13675 if (attr == NULL)
13676 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13677 if (attr == NULL || !attr->form_is_block ())
13678 {
13679 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13680 "DW_TAG_call_site child DIE %s [in module %s]"),
13681 sect_offset_str (child_die->sect_off),
13682 objfile_name (objfile));
13683 continue;
13684 }
13685 parameter->value = DW_BLOCK (attr)->data;
13686 parameter->value_size = DW_BLOCK (attr)->size;
13687
13688 /* Parameters are not pre-cleared by memset above. */
13689 parameter->data_value = NULL;
13690 parameter->data_value_size = 0;
13691 call_site->parameter_count++;
13692
13693 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13694 if (attr == NULL)
13695 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13696 if (attr != nullptr)
13697 {
13698 if (!attr->form_is_block ())
13699 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13700 "DW_TAG_call_site child DIE %s [in module %s]"),
13701 sect_offset_str (child_die->sect_off),
13702 objfile_name (objfile));
13703 else
13704 {
13705 parameter->data_value = DW_BLOCK (attr)->data;
13706 parameter->data_value_size = DW_BLOCK (attr)->size;
13707 }
13708 }
13709 }
13710 }
13711
13712 /* Helper function for read_variable. If DIE represents a virtual
13713 table, then return the type of the concrete object that is
13714 associated with the virtual table. Otherwise, return NULL. */
13715
13716 static struct type *
13717 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13718 {
13719 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13720 if (attr == NULL)
13721 return NULL;
13722
13723 /* Find the type DIE. */
13724 struct die_info *type_die = NULL;
13725 struct dwarf2_cu *type_cu = cu;
13726
13727 if (attr->form_is_ref ())
13728 type_die = follow_die_ref (die, attr, &type_cu);
13729 if (type_die == NULL)
13730 return NULL;
13731
13732 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13733 return NULL;
13734 return die_containing_type (type_die, type_cu);
13735 }
13736
13737 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13738
13739 static void
13740 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13741 {
13742 struct rust_vtable_symbol *storage = NULL;
13743
13744 if (cu->language == language_rust)
13745 {
13746 struct type *containing_type = rust_containing_type (die, cu);
13747
13748 if (containing_type != NULL)
13749 {
13750 struct objfile *objfile = cu->per_objfile->objfile;
13751
13752 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13753 storage->concrete_type = containing_type;
13754 storage->subclass = SYMBOL_RUST_VTABLE;
13755 }
13756 }
13757
13758 struct symbol *res = new_symbol (die, NULL, cu, storage);
13759 struct attribute *abstract_origin
13760 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13761 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13762 if (res == NULL && loc && abstract_origin)
13763 {
13764 /* We have a variable without a name, but with a location and an abstract
13765 origin. This may be a concrete instance of an abstract variable
13766 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13767 later. */
13768 struct dwarf2_cu *origin_cu = cu;
13769 struct die_info *origin_die
13770 = follow_die_ref (die, abstract_origin, &origin_cu);
13771 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13772 per_objfile->per_bfd->abstract_to_concrete
13773 [origin_die->sect_off].push_back (die->sect_off);
13774 }
13775 }
13776
13777 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13778 reading .debug_rnglists.
13779 Callback's type should be:
13780 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13781 Return true if the attributes are present and valid, otherwise,
13782 return false. */
13783
13784 template <typename Callback>
13785 static bool
13786 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13787 Callback &&callback)
13788 {
13789 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13790 struct objfile *objfile = per_objfile->objfile;
13791 bfd *obfd = objfile->obfd;
13792 /* Base address selection entry. */
13793 gdb::optional<CORE_ADDR> base;
13794 const gdb_byte *buffer;
13795 CORE_ADDR baseaddr;
13796 bool overflow = false;
13797
13798 base = cu->base_address;
13799
13800 per_objfile->per_bfd->rnglists.read (objfile);
13801 if (offset >= per_objfile->per_bfd->rnglists.size)
13802 {
13803 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13804 offset);
13805 return false;
13806 }
13807 buffer = per_objfile->per_bfd->rnglists.buffer + offset;
13808
13809 baseaddr = objfile->text_section_offset ();
13810
13811 while (1)
13812 {
13813 /* Initialize it due to a false compiler warning. */
13814 CORE_ADDR range_beginning = 0, range_end = 0;
13815 const gdb_byte *buf_end = (per_objfile->per_bfd->rnglists.buffer
13816 + per_objfile->per_bfd->rnglists.size);
13817 unsigned int bytes_read;
13818
13819 if (buffer == buf_end)
13820 {
13821 overflow = true;
13822 break;
13823 }
13824 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13825 switch (rlet)
13826 {
13827 case DW_RLE_end_of_list:
13828 break;
13829 case DW_RLE_base_address:
13830 if (buffer + cu->header.addr_size > buf_end)
13831 {
13832 overflow = true;
13833 break;
13834 }
13835 base = cu->header.read_address (obfd, buffer, &bytes_read);
13836 buffer += bytes_read;
13837 break;
13838 case DW_RLE_start_length:
13839 if (buffer + cu->header.addr_size > buf_end)
13840 {
13841 overflow = true;
13842 break;
13843 }
13844 range_beginning = cu->header.read_address (obfd, buffer,
13845 &bytes_read);
13846 buffer += bytes_read;
13847 range_end = (range_beginning
13848 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13849 buffer += bytes_read;
13850 if (buffer > buf_end)
13851 {
13852 overflow = true;
13853 break;
13854 }
13855 break;
13856 case DW_RLE_offset_pair:
13857 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13858 buffer += bytes_read;
13859 if (buffer > buf_end)
13860 {
13861 overflow = true;
13862 break;
13863 }
13864 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13865 buffer += bytes_read;
13866 if (buffer > buf_end)
13867 {
13868 overflow = true;
13869 break;
13870 }
13871 break;
13872 case DW_RLE_start_end:
13873 if (buffer + 2 * cu->header.addr_size > buf_end)
13874 {
13875 overflow = true;
13876 break;
13877 }
13878 range_beginning = cu->header.read_address (obfd, buffer,
13879 &bytes_read);
13880 buffer += bytes_read;
13881 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13882 buffer += bytes_read;
13883 break;
13884 default:
13885 complaint (_("Invalid .debug_rnglists data (no base address)"));
13886 return false;
13887 }
13888 if (rlet == DW_RLE_end_of_list || overflow)
13889 break;
13890 if (rlet == DW_RLE_base_address)
13891 continue;
13892
13893 if (!base.has_value ())
13894 {
13895 /* We have no valid base address for the ranges
13896 data. */
13897 complaint (_("Invalid .debug_rnglists data (no base address)"));
13898 return false;
13899 }
13900
13901 if (range_beginning > range_end)
13902 {
13903 /* Inverted range entries are invalid. */
13904 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13905 return false;
13906 }
13907
13908 /* Empty range entries have no effect. */
13909 if (range_beginning == range_end)
13910 continue;
13911
13912 range_beginning += *base;
13913 range_end += *base;
13914
13915 /* A not-uncommon case of bad debug info.
13916 Don't pollute the addrmap with bad data. */
13917 if (range_beginning + baseaddr == 0
13918 && !per_objfile->per_bfd->has_section_at_zero)
13919 {
13920 complaint (_(".debug_rnglists entry has start address of zero"
13921 " [in module %s]"), objfile_name (objfile));
13922 continue;
13923 }
13924
13925 callback (range_beginning, range_end);
13926 }
13927
13928 if (overflow)
13929 {
13930 complaint (_("Offset %d is not terminated "
13931 "for DW_AT_ranges attribute"),
13932 offset);
13933 return false;
13934 }
13935
13936 return true;
13937 }
13938
13939 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13940 Callback's type should be:
13941 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13942 Return 1 if the attributes are present and valid, otherwise, return 0. */
13943
13944 template <typename Callback>
13945 static int
13946 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13947 Callback &&callback)
13948 {
13949 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13950 struct objfile *objfile = per_objfile->objfile;
13951 struct comp_unit_head *cu_header = &cu->header;
13952 bfd *obfd = objfile->obfd;
13953 unsigned int addr_size = cu_header->addr_size;
13954 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13955 /* Base address selection entry. */
13956 gdb::optional<CORE_ADDR> base;
13957 unsigned int dummy;
13958 const gdb_byte *buffer;
13959 CORE_ADDR baseaddr;
13960
13961 if (cu_header->version >= 5)
13962 return dwarf2_rnglists_process (offset, cu, callback);
13963
13964 base = cu->base_address;
13965
13966 per_objfile->per_bfd->ranges.read (objfile);
13967 if (offset >= per_objfile->per_bfd->ranges.size)
13968 {
13969 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13970 offset);
13971 return 0;
13972 }
13973 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13974
13975 baseaddr = objfile->text_section_offset ();
13976
13977 while (1)
13978 {
13979 CORE_ADDR range_beginning, range_end;
13980
13981 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13982 buffer += addr_size;
13983 range_end = cu->header.read_address (obfd, buffer, &dummy);
13984 buffer += addr_size;
13985 offset += 2 * addr_size;
13986
13987 /* An end of list marker is a pair of zero addresses. */
13988 if (range_beginning == 0 && range_end == 0)
13989 /* Found the end of list entry. */
13990 break;
13991
13992 /* Each base address selection entry is a pair of 2 values.
13993 The first is the largest possible address, the second is
13994 the base address. Check for a base address here. */
13995 if ((range_beginning & mask) == mask)
13996 {
13997 /* If we found the largest possible address, then we already
13998 have the base address in range_end. */
13999 base = range_end;
14000 continue;
14001 }
14002
14003 if (!base.has_value ())
14004 {
14005 /* We have no valid base address for the ranges
14006 data. */
14007 complaint (_("Invalid .debug_ranges data (no base address)"));
14008 return 0;
14009 }
14010
14011 if (range_beginning > range_end)
14012 {
14013 /* Inverted range entries are invalid. */
14014 complaint (_("Invalid .debug_ranges data (inverted range)"));
14015 return 0;
14016 }
14017
14018 /* Empty range entries have no effect. */
14019 if (range_beginning == range_end)
14020 continue;
14021
14022 range_beginning += *base;
14023 range_end += *base;
14024
14025 /* A not-uncommon case of bad debug info.
14026 Don't pollute the addrmap with bad data. */
14027 if (range_beginning + baseaddr == 0
14028 && !per_objfile->per_bfd->has_section_at_zero)
14029 {
14030 complaint (_(".debug_ranges entry has start address of zero"
14031 " [in module %s]"), objfile_name (objfile));
14032 continue;
14033 }
14034
14035 callback (range_beginning, range_end);
14036 }
14037
14038 return 1;
14039 }
14040
14041 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14042 Return 1 if the attributes are present and valid, otherwise, return 0.
14043 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14044
14045 static int
14046 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14047 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14048 dwarf2_psymtab *ranges_pst)
14049 {
14050 struct objfile *objfile = cu->per_objfile->objfile;
14051 struct gdbarch *gdbarch = objfile->arch ();
14052 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14053 int low_set = 0;
14054 CORE_ADDR low = 0;
14055 CORE_ADDR high = 0;
14056 int retval;
14057
14058 retval = dwarf2_ranges_process (offset, cu,
14059 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14060 {
14061 if (ranges_pst != NULL)
14062 {
14063 CORE_ADDR lowpc;
14064 CORE_ADDR highpc;
14065
14066 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14067 range_beginning + baseaddr)
14068 - baseaddr);
14069 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14070 range_end + baseaddr)
14071 - baseaddr);
14072 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14073 lowpc, highpc - 1, ranges_pst);
14074 }
14075
14076 /* FIXME: This is recording everything as a low-high
14077 segment of consecutive addresses. We should have a
14078 data structure for discontiguous block ranges
14079 instead. */
14080 if (! low_set)
14081 {
14082 low = range_beginning;
14083 high = range_end;
14084 low_set = 1;
14085 }
14086 else
14087 {
14088 if (range_beginning < low)
14089 low = range_beginning;
14090 if (range_end > high)
14091 high = range_end;
14092 }
14093 });
14094 if (!retval)
14095 return 0;
14096
14097 if (! low_set)
14098 /* If the first entry is an end-of-list marker, the range
14099 describes an empty scope, i.e. no instructions. */
14100 return 0;
14101
14102 if (low_return)
14103 *low_return = low;
14104 if (high_return)
14105 *high_return = high;
14106 return 1;
14107 }
14108
14109 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14110 definition for the return value. *LOWPC and *HIGHPC are set iff
14111 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14112
14113 static enum pc_bounds_kind
14114 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14115 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14116 dwarf2_psymtab *pst)
14117 {
14118 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14119 struct attribute *attr;
14120 struct attribute *attr_high;
14121 CORE_ADDR low = 0;
14122 CORE_ADDR high = 0;
14123 enum pc_bounds_kind ret;
14124
14125 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14126 if (attr_high)
14127 {
14128 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14129 if (attr != nullptr)
14130 {
14131 low = attr->value_as_address ();
14132 high = attr_high->value_as_address ();
14133 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14134 high += low;
14135 }
14136 else
14137 /* Found high w/o low attribute. */
14138 return PC_BOUNDS_INVALID;
14139
14140 /* Found consecutive range of addresses. */
14141 ret = PC_BOUNDS_HIGH_LOW;
14142 }
14143 else
14144 {
14145 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14146 if (attr != NULL)
14147 {
14148 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14149 We take advantage of the fact that DW_AT_ranges does not appear
14150 in DW_TAG_compile_unit of DWO files. */
14151 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14152 unsigned int ranges_offset = (DW_UNSND (attr)
14153 + (need_ranges_base
14154 ? cu->ranges_base
14155 : 0));
14156
14157 /* Value of the DW_AT_ranges attribute is the offset in the
14158 .debug_ranges section. */
14159 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14160 return PC_BOUNDS_INVALID;
14161 /* Found discontinuous range of addresses. */
14162 ret = PC_BOUNDS_RANGES;
14163 }
14164 else
14165 return PC_BOUNDS_NOT_PRESENT;
14166 }
14167
14168 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14169 if (high <= low)
14170 return PC_BOUNDS_INVALID;
14171
14172 /* When using the GNU linker, .gnu.linkonce. sections are used to
14173 eliminate duplicate copies of functions and vtables and such.
14174 The linker will arbitrarily choose one and discard the others.
14175 The AT_*_pc values for such functions refer to local labels in
14176 these sections. If the section from that file was discarded, the
14177 labels are not in the output, so the relocs get a value of 0.
14178 If this is a discarded function, mark the pc bounds as invalid,
14179 so that GDB will ignore it. */
14180 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14181 return PC_BOUNDS_INVALID;
14182
14183 *lowpc = low;
14184 if (highpc)
14185 *highpc = high;
14186 return ret;
14187 }
14188
14189 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14190 its low and high PC addresses. Do nothing if these addresses could not
14191 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14192 and HIGHPC to the high address if greater than HIGHPC. */
14193
14194 static void
14195 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14196 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14197 struct dwarf2_cu *cu)
14198 {
14199 CORE_ADDR low, high;
14200 struct die_info *child = die->child;
14201
14202 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14203 {
14204 *lowpc = std::min (*lowpc, low);
14205 *highpc = std::max (*highpc, high);
14206 }
14207
14208 /* If the language does not allow nested subprograms (either inside
14209 subprograms or lexical blocks), we're done. */
14210 if (cu->language != language_ada)
14211 return;
14212
14213 /* Check all the children of the given DIE. If it contains nested
14214 subprograms, then check their pc bounds. Likewise, we need to
14215 check lexical blocks as well, as they may also contain subprogram
14216 definitions. */
14217 while (child && child->tag)
14218 {
14219 if (child->tag == DW_TAG_subprogram
14220 || child->tag == DW_TAG_lexical_block)
14221 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14222 child = child->sibling;
14223 }
14224 }
14225
14226 /* Get the low and high pc's represented by the scope DIE, and store
14227 them in *LOWPC and *HIGHPC. If the correct values can't be
14228 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14229
14230 static void
14231 get_scope_pc_bounds (struct die_info *die,
14232 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14233 struct dwarf2_cu *cu)
14234 {
14235 CORE_ADDR best_low = (CORE_ADDR) -1;
14236 CORE_ADDR best_high = (CORE_ADDR) 0;
14237 CORE_ADDR current_low, current_high;
14238
14239 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14240 >= PC_BOUNDS_RANGES)
14241 {
14242 best_low = current_low;
14243 best_high = current_high;
14244 }
14245 else
14246 {
14247 struct die_info *child = die->child;
14248
14249 while (child && child->tag)
14250 {
14251 switch (child->tag) {
14252 case DW_TAG_subprogram:
14253 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14254 break;
14255 case DW_TAG_namespace:
14256 case DW_TAG_module:
14257 /* FIXME: carlton/2004-01-16: Should we do this for
14258 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14259 that current GCC's always emit the DIEs corresponding
14260 to definitions of methods of classes as children of a
14261 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14262 the DIEs giving the declarations, which could be
14263 anywhere). But I don't see any reason why the
14264 standards says that they have to be there. */
14265 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14266
14267 if (current_low != ((CORE_ADDR) -1))
14268 {
14269 best_low = std::min (best_low, current_low);
14270 best_high = std::max (best_high, current_high);
14271 }
14272 break;
14273 default:
14274 /* Ignore. */
14275 break;
14276 }
14277
14278 child = child->sibling;
14279 }
14280 }
14281
14282 *lowpc = best_low;
14283 *highpc = best_high;
14284 }
14285
14286 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14287 in DIE. */
14288
14289 static void
14290 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14291 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14292 {
14293 struct objfile *objfile = cu->per_objfile->objfile;
14294 struct gdbarch *gdbarch = objfile->arch ();
14295 struct attribute *attr;
14296 struct attribute *attr_high;
14297
14298 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14299 if (attr_high)
14300 {
14301 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14302 if (attr != nullptr)
14303 {
14304 CORE_ADDR low = attr->value_as_address ();
14305 CORE_ADDR high = attr_high->value_as_address ();
14306
14307 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14308 high += low;
14309
14310 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14311 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14312 cu->get_builder ()->record_block_range (block, low, high - 1);
14313 }
14314 }
14315
14316 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14317 if (attr != nullptr)
14318 {
14319 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14320 We take advantage of the fact that DW_AT_ranges does not appear
14321 in DW_TAG_compile_unit of DWO files. */
14322 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14323
14324 /* The value of the DW_AT_ranges attribute is the offset of the
14325 address range list in the .debug_ranges section. */
14326 unsigned long offset = (DW_UNSND (attr)
14327 + (need_ranges_base ? cu->ranges_base : 0));
14328
14329 std::vector<blockrange> blockvec;
14330 dwarf2_ranges_process (offset, cu,
14331 [&] (CORE_ADDR start, CORE_ADDR end)
14332 {
14333 start += baseaddr;
14334 end += baseaddr;
14335 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14336 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14337 cu->get_builder ()->record_block_range (block, start, end - 1);
14338 blockvec.emplace_back (start, end);
14339 });
14340
14341 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14342 }
14343 }
14344
14345 /* Check whether the producer field indicates either of GCC < 4.6, or the
14346 Intel C/C++ compiler, and cache the result in CU. */
14347
14348 static void
14349 check_producer (struct dwarf2_cu *cu)
14350 {
14351 int major, minor;
14352
14353 if (cu->producer == NULL)
14354 {
14355 /* For unknown compilers expect their behavior is DWARF version
14356 compliant.
14357
14358 GCC started to support .debug_types sections by -gdwarf-4 since
14359 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14360 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14361 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14362 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14363 }
14364 else if (producer_is_gcc (cu->producer, &major, &minor))
14365 {
14366 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14367 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14368 }
14369 else if (producer_is_icc (cu->producer, &major, &minor))
14370 {
14371 cu->producer_is_icc = true;
14372 cu->producer_is_icc_lt_14 = major < 14;
14373 }
14374 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14375 cu->producer_is_codewarrior = true;
14376 else
14377 {
14378 /* For other non-GCC compilers, expect their behavior is DWARF version
14379 compliant. */
14380 }
14381
14382 cu->checked_producer = true;
14383 }
14384
14385 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14386 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14387 during 4.6.0 experimental. */
14388
14389 static bool
14390 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14391 {
14392 if (!cu->checked_producer)
14393 check_producer (cu);
14394
14395 return cu->producer_is_gxx_lt_4_6;
14396 }
14397
14398
14399 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14400 with incorrect is_stmt attributes. */
14401
14402 static bool
14403 producer_is_codewarrior (struct dwarf2_cu *cu)
14404 {
14405 if (!cu->checked_producer)
14406 check_producer (cu);
14407
14408 return cu->producer_is_codewarrior;
14409 }
14410
14411 /* Return the default accessibility type if it is not overridden by
14412 DW_AT_accessibility. */
14413
14414 static enum dwarf_access_attribute
14415 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14416 {
14417 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14418 {
14419 /* The default DWARF 2 accessibility for members is public, the default
14420 accessibility for inheritance is private. */
14421
14422 if (die->tag != DW_TAG_inheritance)
14423 return DW_ACCESS_public;
14424 else
14425 return DW_ACCESS_private;
14426 }
14427 else
14428 {
14429 /* DWARF 3+ defines the default accessibility a different way. The same
14430 rules apply now for DW_TAG_inheritance as for the members and it only
14431 depends on the container kind. */
14432
14433 if (die->parent->tag == DW_TAG_class_type)
14434 return DW_ACCESS_private;
14435 else
14436 return DW_ACCESS_public;
14437 }
14438 }
14439
14440 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14441 offset. If the attribute was not found return 0, otherwise return
14442 1. If it was found but could not properly be handled, set *OFFSET
14443 to 0. */
14444
14445 static int
14446 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14447 LONGEST *offset)
14448 {
14449 struct attribute *attr;
14450
14451 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14452 if (attr != NULL)
14453 {
14454 *offset = 0;
14455
14456 /* Note that we do not check for a section offset first here.
14457 This is because DW_AT_data_member_location is new in DWARF 4,
14458 so if we see it, we can assume that a constant form is really
14459 a constant and not a section offset. */
14460 if (attr->form_is_constant ())
14461 *offset = attr->constant_value (0);
14462 else if (attr->form_is_section_offset ())
14463 dwarf2_complex_location_expr_complaint ();
14464 else if (attr->form_is_block ())
14465 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14466 else
14467 dwarf2_complex_location_expr_complaint ();
14468
14469 return 1;
14470 }
14471
14472 return 0;
14473 }
14474
14475 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14476
14477 static void
14478 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14479 struct field *field)
14480 {
14481 struct attribute *attr;
14482
14483 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14484 if (attr != NULL)
14485 {
14486 if (attr->form_is_constant ())
14487 {
14488 LONGEST offset = attr->constant_value (0);
14489 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14490 }
14491 else if (attr->form_is_section_offset ())
14492 dwarf2_complex_location_expr_complaint ();
14493 else if (attr->form_is_block ())
14494 {
14495 bool handled;
14496 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14497 if (handled)
14498 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14499 else
14500 {
14501 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14502 struct objfile *objfile = per_objfile->objfile;
14503 struct dwarf2_locexpr_baton *dlbaton
14504 = XOBNEW (&objfile->objfile_obstack,
14505 struct dwarf2_locexpr_baton);
14506 dlbaton->data = DW_BLOCK (attr)->data;
14507 dlbaton->size = DW_BLOCK (attr)->size;
14508 /* When using this baton, we want to compute the address
14509 of the field, not the value. This is why
14510 is_reference is set to false here. */
14511 dlbaton->is_reference = false;
14512 dlbaton->per_objfile = per_objfile;
14513 dlbaton->per_cu = cu->per_cu;
14514
14515 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14516 }
14517 }
14518 else
14519 dwarf2_complex_location_expr_complaint ();
14520 }
14521 }
14522
14523 /* Add an aggregate field to the field list. */
14524
14525 static void
14526 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14527 struct dwarf2_cu *cu)
14528 {
14529 struct objfile *objfile = cu->per_objfile->objfile;
14530 struct gdbarch *gdbarch = objfile->arch ();
14531 struct nextfield *new_field;
14532 struct attribute *attr;
14533 struct field *fp;
14534 const char *fieldname = "";
14535
14536 if (die->tag == DW_TAG_inheritance)
14537 {
14538 fip->baseclasses.emplace_back ();
14539 new_field = &fip->baseclasses.back ();
14540 }
14541 else
14542 {
14543 fip->fields.emplace_back ();
14544 new_field = &fip->fields.back ();
14545 }
14546
14547 new_field->offset = die->sect_off;
14548
14549 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14550 if (attr != nullptr)
14551 new_field->accessibility = DW_UNSND (attr);
14552 else
14553 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14554 if (new_field->accessibility != DW_ACCESS_public)
14555 fip->non_public_fields = 1;
14556
14557 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14558 if (attr != nullptr)
14559 new_field->virtuality = DW_UNSND (attr);
14560 else
14561 new_field->virtuality = DW_VIRTUALITY_none;
14562
14563 fp = &new_field->field;
14564
14565 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14566 {
14567 /* Data member other than a C++ static data member. */
14568
14569 /* Get type of field. */
14570 fp->set_type (die_type (die, cu));
14571
14572 SET_FIELD_BITPOS (*fp, 0);
14573
14574 /* Get bit size of field (zero if none). */
14575 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14576 if (attr != nullptr)
14577 {
14578 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14579 }
14580 else
14581 {
14582 FIELD_BITSIZE (*fp) = 0;
14583 }
14584
14585 /* Get bit offset of field. */
14586 handle_data_member_location (die, cu, fp);
14587 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14588 if (attr != nullptr)
14589 {
14590 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14591 {
14592 /* For big endian bits, the DW_AT_bit_offset gives the
14593 additional bit offset from the MSB of the containing
14594 anonymous object to the MSB of the field. We don't
14595 have to do anything special since we don't need to
14596 know the size of the anonymous object. */
14597 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14598 }
14599 else
14600 {
14601 /* For little endian bits, compute the bit offset to the
14602 MSB of the anonymous object, subtract off the number of
14603 bits from the MSB of the field to the MSB of the
14604 object, and then subtract off the number of bits of
14605 the field itself. The result is the bit offset of
14606 the LSB of the field. */
14607 int anonymous_size;
14608 int bit_offset = DW_UNSND (attr);
14609
14610 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14611 if (attr != nullptr)
14612 {
14613 /* The size of the anonymous object containing
14614 the bit field is explicit, so use the
14615 indicated size (in bytes). */
14616 anonymous_size = DW_UNSND (attr);
14617 }
14618 else
14619 {
14620 /* The size of the anonymous object containing
14621 the bit field must be inferred from the type
14622 attribute of the data member containing the
14623 bit field. */
14624 anonymous_size = TYPE_LENGTH (fp->type ());
14625 }
14626 SET_FIELD_BITPOS (*fp,
14627 (FIELD_BITPOS (*fp)
14628 + anonymous_size * bits_per_byte
14629 - bit_offset - FIELD_BITSIZE (*fp)));
14630 }
14631 }
14632 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14633 if (attr != NULL)
14634 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14635 + attr->constant_value (0)));
14636
14637 /* Get name of field. */
14638 fieldname = dwarf2_name (die, cu);
14639 if (fieldname == NULL)
14640 fieldname = "";
14641
14642 /* The name is already allocated along with this objfile, so we don't
14643 need to duplicate it for the type. */
14644 fp->name = fieldname;
14645
14646 /* Change accessibility for artificial fields (e.g. virtual table
14647 pointer or virtual base class pointer) to private. */
14648 if (dwarf2_attr (die, DW_AT_artificial, cu))
14649 {
14650 FIELD_ARTIFICIAL (*fp) = 1;
14651 new_field->accessibility = DW_ACCESS_private;
14652 fip->non_public_fields = 1;
14653 }
14654 }
14655 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14656 {
14657 /* C++ static member. */
14658
14659 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14660 is a declaration, but all versions of G++ as of this writing
14661 (so through at least 3.2.1) incorrectly generate
14662 DW_TAG_variable tags. */
14663
14664 const char *physname;
14665
14666 /* Get name of field. */
14667 fieldname = dwarf2_name (die, cu);
14668 if (fieldname == NULL)
14669 return;
14670
14671 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14672 if (attr
14673 /* Only create a symbol if this is an external value.
14674 new_symbol checks this and puts the value in the global symbol
14675 table, which we want. If it is not external, new_symbol
14676 will try to put the value in cu->list_in_scope which is wrong. */
14677 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14678 {
14679 /* A static const member, not much different than an enum as far as
14680 we're concerned, except that we can support more types. */
14681 new_symbol (die, NULL, cu);
14682 }
14683
14684 /* Get physical name. */
14685 physname = dwarf2_physname (fieldname, die, cu);
14686
14687 /* The name is already allocated along with this objfile, so we don't
14688 need to duplicate it for the type. */
14689 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14690 fp->set_type (die_type (die, cu));
14691 FIELD_NAME (*fp) = fieldname;
14692 }
14693 else if (die->tag == DW_TAG_inheritance)
14694 {
14695 /* C++ base class field. */
14696 handle_data_member_location (die, cu, fp);
14697 FIELD_BITSIZE (*fp) = 0;
14698 fp->set_type (die_type (die, cu));
14699 FIELD_NAME (*fp) = fp->type ()->name ();
14700 }
14701 else
14702 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14703 }
14704
14705 /* Can the type given by DIE define another type? */
14706
14707 static bool
14708 type_can_define_types (const struct die_info *die)
14709 {
14710 switch (die->tag)
14711 {
14712 case DW_TAG_typedef:
14713 case DW_TAG_class_type:
14714 case DW_TAG_structure_type:
14715 case DW_TAG_union_type:
14716 case DW_TAG_enumeration_type:
14717 return true;
14718
14719 default:
14720 return false;
14721 }
14722 }
14723
14724 /* Add a type definition defined in the scope of the FIP's class. */
14725
14726 static void
14727 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14728 struct dwarf2_cu *cu)
14729 {
14730 struct decl_field fp;
14731 memset (&fp, 0, sizeof (fp));
14732
14733 gdb_assert (type_can_define_types (die));
14734
14735 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14736 fp.name = dwarf2_name (die, cu);
14737 fp.type = read_type_die (die, cu);
14738
14739 /* Save accessibility. */
14740 enum dwarf_access_attribute accessibility;
14741 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14742 if (attr != NULL)
14743 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14744 else
14745 accessibility = dwarf2_default_access_attribute (die, cu);
14746 switch (accessibility)
14747 {
14748 case DW_ACCESS_public:
14749 /* The assumed value if neither private nor protected. */
14750 break;
14751 case DW_ACCESS_private:
14752 fp.is_private = 1;
14753 break;
14754 case DW_ACCESS_protected:
14755 fp.is_protected = 1;
14756 break;
14757 default:
14758 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14759 }
14760
14761 if (die->tag == DW_TAG_typedef)
14762 fip->typedef_field_list.push_back (fp);
14763 else
14764 fip->nested_types_list.push_back (fp);
14765 }
14766
14767 /* A convenience typedef that's used when finding the discriminant
14768 field for a variant part. */
14769 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14770 offset_map_type;
14771
14772 /* Compute the discriminant range for a given variant. OBSTACK is
14773 where the results will be stored. VARIANT is the variant to
14774 process. IS_UNSIGNED indicates whether the discriminant is signed
14775 or unsigned. */
14776
14777 static const gdb::array_view<discriminant_range>
14778 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14779 bool is_unsigned)
14780 {
14781 std::vector<discriminant_range> ranges;
14782
14783 if (variant.default_branch)
14784 return {};
14785
14786 if (variant.discr_list_data == nullptr)
14787 {
14788 discriminant_range r
14789 = {variant.discriminant_value, variant.discriminant_value};
14790 ranges.push_back (r);
14791 }
14792 else
14793 {
14794 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14795 variant.discr_list_data->size);
14796 while (!data.empty ())
14797 {
14798 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14799 {
14800 complaint (_("invalid discriminant marker: %d"), data[0]);
14801 break;
14802 }
14803 bool is_range = data[0] == DW_DSC_range;
14804 data = data.slice (1);
14805
14806 ULONGEST low, high;
14807 unsigned int bytes_read;
14808
14809 if (data.empty ())
14810 {
14811 complaint (_("DW_AT_discr_list missing low value"));
14812 break;
14813 }
14814 if (is_unsigned)
14815 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14816 else
14817 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14818 &bytes_read);
14819 data = data.slice (bytes_read);
14820
14821 if (is_range)
14822 {
14823 if (data.empty ())
14824 {
14825 complaint (_("DW_AT_discr_list missing high value"));
14826 break;
14827 }
14828 if (is_unsigned)
14829 high = read_unsigned_leb128 (nullptr, data.data (),
14830 &bytes_read);
14831 else
14832 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14833 &bytes_read);
14834 data = data.slice (bytes_read);
14835 }
14836 else
14837 high = low;
14838
14839 ranges.push_back ({ low, high });
14840 }
14841 }
14842
14843 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14844 ranges.size ());
14845 std::copy (ranges.begin (), ranges.end (), result);
14846 return gdb::array_view<discriminant_range> (result, ranges.size ());
14847 }
14848
14849 static const gdb::array_view<variant_part> create_variant_parts
14850 (struct obstack *obstack,
14851 const offset_map_type &offset_map,
14852 struct field_info *fi,
14853 const std::vector<variant_part_builder> &variant_parts);
14854
14855 /* Fill in a "struct variant" for a given variant field. RESULT is
14856 the variant to fill in. OBSTACK is where any needed allocations
14857 will be done. OFFSET_MAP holds the mapping from section offsets to
14858 fields for the type. FI describes the fields of the type we're
14859 processing. FIELD is the variant field we're converting. */
14860
14861 static void
14862 create_one_variant (variant &result, struct obstack *obstack,
14863 const offset_map_type &offset_map,
14864 struct field_info *fi, const variant_field &field)
14865 {
14866 result.discriminants = convert_variant_range (obstack, field, false);
14867 result.first_field = field.first_field + fi->baseclasses.size ();
14868 result.last_field = field.last_field + fi->baseclasses.size ();
14869 result.parts = create_variant_parts (obstack, offset_map, fi,
14870 field.variant_parts);
14871 }
14872
14873 /* Fill in a "struct variant_part" for a given variant part. RESULT
14874 is the variant part to fill in. OBSTACK is where any needed
14875 allocations will be done. OFFSET_MAP holds the mapping from
14876 section offsets to fields for the type. FI describes the fields of
14877 the type we're processing. BUILDER is the variant part to be
14878 converted. */
14879
14880 static void
14881 create_one_variant_part (variant_part &result,
14882 struct obstack *obstack,
14883 const offset_map_type &offset_map,
14884 struct field_info *fi,
14885 const variant_part_builder &builder)
14886 {
14887 auto iter = offset_map.find (builder.discriminant_offset);
14888 if (iter == offset_map.end ())
14889 {
14890 result.discriminant_index = -1;
14891 /* Doesn't matter. */
14892 result.is_unsigned = false;
14893 }
14894 else
14895 {
14896 result.discriminant_index = iter->second;
14897 result.is_unsigned
14898 = TYPE_UNSIGNED (fi->fields[result.discriminant_index].field.type ());
14899 }
14900
14901 size_t n = builder.variants.size ();
14902 variant *output = new (obstack) variant[n];
14903 for (size_t i = 0; i < n; ++i)
14904 create_one_variant (output[i], obstack, offset_map, fi,
14905 builder.variants[i]);
14906
14907 result.variants = gdb::array_view<variant> (output, n);
14908 }
14909
14910 /* Create a vector of variant parts that can be attached to a type.
14911 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14912 holds the mapping from section offsets to fields for the type. FI
14913 describes the fields of the type we're processing. VARIANT_PARTS
14914 is the vector to convert. */
14915
14916 static const gdb::array_view<variant_part>
14917 create_variant_parts (struct obstack *obstack,
14918 const offset_map_type &offset_map,
14919 struct field_info *fi,
14920 const std::vector<variant_part_builder> &variant_parts)
14921 {
14922 if (variant_parts.empty ())
14923 return {};
14924
14925 size_t n = variant_parts.size ();
14926 variant_part *result = new (obstack) variant_part[n];
14927 for (size_t i = 0; i < n; ++i)
14928 create_one_variant_part (result[i], obstack, offset_map, fi,
14929 variant_parts[i]);
14930
14931 return gdb::array_view<variant_part> (result, n);
14932 }
14933
14934 /* Compute the variant part vector for FIP, attaching it to TYPE when
14935 done. */
14936
14937 static void
14938 add_variant_property (struct field_info *fip, struct type *type,
14939 struct dwarf2_cu *cu)
14940 {
14941 /* Map section offsets of fields to their field index. Note the
14942 field index here does not take the number of baseclasses into
14943 account. */
14944 offset_map_type offset_map;
14945 for (int i = 0; i < fip->fields.size (); ++i)
14946 offset_map[fip->fields[i].offset] = i;
14947
14948 struct objfile *objfile = cu->per_objfile->objfile;
14949 gdb::array_view<variant_part> parts
14950 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14951 fip->variant_parts);
14952
14953 struct dynamic_prop prop;
14954 prop.kind = PROP_VARIANT_PARTS;
14955 prop.data.variant_parts
14956 = ((gdb::array_view<variant_part> *)
14957 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14958
14959 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14960 }
14961
14962 /* Create the vector of fields, and attach it to the type. */
14963
14964 static void
14965 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14966 struct dwarf2_cu *cu)
14967 {
14968 int nfields = fip->nfields ();
14969
14970 /* Record the field count, allocate space for the array of fields,
14971 and create blank accessibility bitfields if necessary. */
14972 type->set_num_fields (nfields);
14973 type->set_fields
14974 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14975
14976 if (fip->non_public_fields && cu->language != language_ada)
14977 {
14978 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14979
14980 TYPE_FIELD_PRIVATE_BITS (type) =
14981 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14982 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14983
14984 TYPE_FIELD_PROTECTED_BITS (type) =
14985 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14986 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14987
14988 TYPE_FIELD_IGNORE_BITS (type) =
14989 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14990 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14991 }
14992
14993 /* If the type has baseclasses, allocate and clear a bit vector for
14994 TYPE_FIELD_VIRTUAL_BITS. */
14995 if (!fip->baseclasses.empty () && cu->language != language_ada)
14996 {
14997 int num_bytes = B_BYTES (fip->baseclasses.size ());
14998 unsigned char *pointer;
14999
15000 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15001 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15002 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15003 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15004 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15005 }
15006
15007 if (!fip->variant_parts.empty ())
15008 add_variant_property (fip, type, cu);
15009
15010 /* Copy the saved-up fields into the field vector. */
15011 for (int i = 0; i < nfields; ++i)
15012 {
15013 struct nextfield &field
15014 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15015 : fip->fields[i - fip->baseclasses.size ()]);
15016
15017 type->field (i) = field.field;
15018 switch (field.accessibility)
15019 {
15020 case DW_ACCESS_private:
15021 if (cu->language != language_ada)
15022 SET_TYPE_FIELD_PRIVATE (type, i);
15023 break;
15024
15025 case DW_ACCESS_protected:
15026 if (cu->language != language_ada)
15027 SET_TYPE_FIELD_PROTECTED (type, i);
15028 break;
15029
15030 case DW_ACCESS_public:
15031 break;
15032
15033 default:
15034 /* Unknown accessibility. Complain and treat it as public. */
15035 {
15036 complaint (_("unsupported accessibility %d"),
15037 field.accessibility);
15038 }
15039 break;
15040 }
15041 if (i < fip->baseclasses.size ())
15042 {
15043 switch (field.virtuality)
15044 {
15045 case DW_VIRTUALITY_virtual:
15046 case DW_VIRTUALITY_pure_virtual:
15047 if (cu->language == language_ada)
15048 error (_("unexpected virtuality in component of Ada type"));
15049 SET_TYPE_FIELD_VIRTUAL (type, i);
15050 break;
15051 }
15052 }
15053 }
15054 }
15055
15056 /* Return true if this member function is a constructor, false
15057 otherwise. */
15058
15059 static int
15060 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15061 {
15062 const char *fieldname;
15063 const char *type_name;
15064 int len;
15065
15066 if (die->parent == NULL)
15067 return 0;
15068
15069 if (die->parent->tag != DW_TAG_structure_type
15070 && die->parent->tag != DW_TAG_union_type
15071 && die->parent->tag != DW_TAG_class_type)
15072 return 0;
15073
15074 fieldname = dwarf2_name (die, cu);
15075 type_name = dwarf2_name (die->parent, cu);
15076 if (fieldname == NULL || type_name == NULL)
15077 return 0;
15078
15079 len = strlen (fieldname);
15080 return (strncmp (fieldname, type_name, len) == 0
15081 && (type_name[len] == '\0' || type_name[len] == '<'));
15082 }
15083
15084 /* Check if the given VALUE is a recognized enum
15085 dwarf_defaulted_attribute constant according to DWARF5 spec,
15086 Table 7.24. */
15087
15088 static bool
15089 is_valid_DW_AT_defaulted (ULONGEST value)
15090 {
15091 switch (value)
15092 {
15093 case DW_DEFAULTED_no:
15094 case DW_DEFAULTED_in_class:
15095 case DW_DEFAULTED_out_of_class:
15096 return true;
15097 }
15098
15099 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15100 return false;
15101 }
15102
15103 /* Add a member function to the proper fieldlist. */
15104
15105 static void
15106 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15107 struct type *type, struct dwarf2_cu *cu)
15108 {
15109 struct objfile *objfile = cu->per_objfile->objfile;
15110 struct attribute *attr;
15111 int i;
15112 struct fnfieldlist *flp = nullptr;
15113 struct fn_field *fnp;
15114 const char *fieldname;
15115 struct type *this_type;
15116 enum dwarf_access_attribute accessibility;
15117
15118 if (cu->language == language_ada)
15119 error (_("unexpected member function in Ada type"));
15120
15121 /* Get name of member function. */
15122 fieldname = dwarf2_name (die, cu);
15123 if (fieldname == NULL)
15124 return;
15125
15126 /* Look up member function name in fieldlist. */
15127 for (i = 0; i < fip->fnfieldlists.size (); i++)
15128 {
15129 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15130 {
15131 flp = &fip->fnfieldlists[i];
15132 break;
15133 }
15134 }
15135
15136 /* Create a new fnfieldlist if necessary. */
15137 if (flp == nullptr)
15138 {
15139 fip->fnfieldlists.emplace_back ();
15140 flp = &fip->fnfieldlists.back ();
15141 flp->name = fieldname;
15142 i = fip->fnfieldlists.size () - 1;
15143 }
15144
15145 /* Create a new member function field and add it to the vector of
15146 fnfieldlists. */
15147 flp->fnfields.emplace_back ();
15148 fnp = &flp->fnfields.back ();
15149
15150 /* Delay processing of the physname until later. */
15151 if (cu->language == language_cplus)
15152 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15153 die, cu);
15154 else
15155 {
15156 const char *physname = dwarf2_physname (fieldname, die, cu);
15157 fnp->physname = physname ? physname : "";
15158 }
15159
15160 fnp->type = alloc_type (objfile);
15161 this_type = read_type_die (die, cu);
15162 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15163 {
15164 int nparams = this_type->num_fields ();
15165
15166 /* TYPE is the domain of this method, and THIS_TYPE is the type
15167 of the method itself (TYPE_CODE_METHOD). */
15168 smash_to_method_type (fnp->type, type,
15169 TYPE_TARGET_TYPE (this_type),
15170 this_type->fields (),
15171 this_type->num_fields (),
15172 TYPE_VARARGS (this_type));
15173
15174 /* Handle static member functions.
15175 Dwarf2 has no clean way to discern C++ static and non-static
15176 member functions. G++ helps GDB by marking the first
15177 parameter for non-static member functions (which is the this
15178 pointer) as artificial. We obtain this information from
15179 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15180 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15181 fnp->voffset = VOFFSET_STATIC;
15182 }
15183 else
15184 complaint (_("member function type missing for '%s'"),
15185 dwarf2_full_name (fieldname, die, cu));
15186
15187 /* Get fcontext from DW_AT_containing_type if present. */
15188 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15189 fnp->fcontext = die_containing_type (die, cu);
15190
15191 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15192 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15193
15194 /* Get accessibility. */
15195 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15196 if (attr != nullptr)
15197 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15198 else
15199 accessibility = dwarf2_default_access_attribute (die, cu);
15200 switch (accessibility)
15201 {
15202 case DW_ACCESS_private:
15203 fnp->is_private = 1;
15204 break;
15205 case DW_ACCESS_protected:
15206 fnp->is_protected = 1;
15207 break;
15208 }
15209
15210 /* Check for artificial methods. */
15211 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15212 if (attr && DW_UNSND (attr) != 0)
15213 fnp->is_artificial = 1;
15214
15215 /* Check for defaulted methods. */
15216 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15217 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15218 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15219
15220 /* Check for deleted methods. */
15221 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15222 if (attr != nullptr && DW_UNSND (attr) != 0)
15223 fnp->is_deleted = 1;
15224
15225 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15226
15227 /* Get index in virtual function table if it is a virtual member
15228 function. For older versions of GCC, this is an offset in the
15229 appropriate virtual table, as specified by DW_AT_containing_type.
15230 For everyone else, it is an expression to be evaluated relative
15231 to the object address. */
15232
15233 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15234 if (attr != nullptr)
15235 {
15236 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15237 {
15238 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15239 {
15240 /* Old-style GCC. */
15241 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15242 }
15243 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15244 || (DW_BLOCK (attr)->size > 1
15245 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15246 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15247 {
15248 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15249 if ((fnp->voffset % cu->header.addr_size) != 0)
15250 dwarf2_complex_location_expr_complaint ();
15251 else
15252 fnp->voffset /= cu->header.addr_size;
15253 fnp->voffset += 2;
15254 }
15255 else
15256 dwarf2_complex_location_expr_complaint ();
15257
15258 if (!fnp->fcontext)
15259 {
15260 /* If there is no `this' field and no DW_AT_containing_type,
15261 we cannot actually find a base class context for the
15262 vtable! */
15263 if (this_type->num_fields () == 0
15264 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15265 {
15266 complaint (_("cannot determine context for virtual member "
15267 "function \"%s\" (offset %s)"),
15268 fieldname, sect_offset_str (die->sect_off));
15269 }
15270 else
15271 {
15272 fnp->fcontext
15273 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15274 }
15275 }
15276 }
15277 else if (attr->form_is_section_offset ())
15278 {
15279 dwarf2_complex_location_expr_complaint ();
15280 }
15281 else
15282 {
15283 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15284 fieldname);
15285 }
15286 }
15287 else
15288 {
15289 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15290 if (attr && DW_UNSND (attr))
15291 {
15292 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15293 complaint (_("Member function \"%s\" (offset %s) is virtual "
15294 "but the vtable offset is not specified"),
15295 fieldname, sect_offset_str (die->sect_off));
15296 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15297 TYPE_CPLUS_DYNAMIC (type) = 1;
15298 }
15299 }
15300 }
15301
15302 /* Create the vector of member function fields, and attach it to the type. */
15303
15304 static void
15305 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15306 struct dwarf2_cu *cu)
15307 {
15308 if (cu->language == language_ada)
15309 error (_("unexpected member functions in Ada type"));
15310
15311 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15312 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15313 TYPE_ALLOC (type,
15314 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15315
15316 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15317 {
15318 struct fnfieldlist &nf = fip->fnfieldlists[i];
15319 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15320
15321 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15322 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15323 fn_flp->fn_fields = (struct fn_field *)
15324 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15325
15326 for (int k = 0; k < nf.fnfields.size (); ++k)
15327 fn_flp->fn_fields[k] = nf.fnfields[k];
15328 }
15329
15330 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15331 }
15332
15333 /* Returns non-zero if NAME is the name of a vtable member in CU's
15334 language, zero otherwise. */
15335 static int
15336 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15337 {
15338 static const char vptr[] = "_vptr";
15339
15340 /* Look for the C++ form of the vtable. */
15341 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15342 return 1;
15343
15344 return 0;
15345 }
15346
15347 /* GCC outputs unnamed structures that are really pointers to member
15348 functions, with the ABI-specified layout. If TYPE describes
15349 such a structure, smash it into a member function type.
15350
15351 GCC shouldn't do this; it should just output pointer to member DIEs.
15352 This is GCC PR debug/28767. */
15353
15354 static void
15355 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15356 {
15357 struct type *pfn_type, *self_type, *new_type;
15358
15359 /* Check for a structure with no name and two children. */
15360 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15361 return;
15362
15363 /* Check for __pfn and __delta members. */
15364 if (TYPE_FIELD_NAME (type, 0) == NULL
15365 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15366 || TYPE_FIELD_NAME (type, 1) == NULL
15367 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15368 return;
15369
15370 /* Find the type of the method. */
15371 pfn_type = type->field (0).type ();
15372 if (pfn_type == NULL
15373 || pfn_type->code () != TYPE_CODE_PTR
15374 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15375 return;
15376
15377 /* Look for the "this" argument. */
15378 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15379 if (pfn_type->num_fields () == 0
15380 /* || pfn_type->field (0).type () == NULL */
15381 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15382 return;
15383
15384 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15385 new_type = alloc_type (objfile);
15386 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15387 pfn_type->fields (), pfn_type->num_fields (),
15388 TYPE_VARARGS (pfn_type));
15389 smash_to_methodptr_type (type, new_type);
15390 }
15391
15392 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15393 appropriate error checking and issuing complaints if there is a
15394 problem. */
15395
15396 static ULONGEST
15397 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15398 {
15399 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15400
15401 if (attr == nullptr)
15402 return 0;
15403
15404 if (!attr->form_is_constant ())
15405 {
15406 complaint (_("DW_AT_alignment must have constant form"
15407 " - DIE at %s [in module %s]"),
15408 sect_offset_str (die->sect_off),
15409 objfile_name (cu->per_objfile->objfile));
15410 return 0;
15411 }
15412
15413 ULONGEST align;
15414 if (attr->form == DW_FORM_sdata)
15415 {
15416 LONGEST val = DW_SND (attr);
15417 if (val < 0)
15418 {
15419 complaint (_("DW_AT_alignment value must not be negative"
15420 " - DIE at %s [in module %s]"),
15421 sect_offset_str (die->sect_off),
15422 objfile_name (cu->per_objfile->objfile));
15423 return 0;
15424 }
15425 align = val;
15426 }
15427 else
15428 align = DW_UNSND (attr);
15429
15430 if (align == 0)
15431 {
15432 complaint (_("DW_AT_alignment value must not be zero"
15433 " - DIE at %s [in module %s]"),
15434 sect_offset_str (die->sect_off),
15435 objfile_name (cu->per_objfile->objfile));
15436 return 0;
15437 }
15438 if ((align & (align - 1)) != 0)
15439 {
15440 complaint (_("DW_AT_alignment value must be a power of 2"
15441 " - DIE at %s [in module %s]"),
15442 sect_offset_str (die->sect_off),
15443 objfile_name (cu->per_objfile->objfile));
15444 return 0;
15445 }
15446
15447 return align;
15448 }
15449
15450 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15451 the alignment for TYPE. */
15452
15453 static void
15454 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15455 struct type *type)
15456 {
15457 if (!set_type_align (type, get_alignment (cu, die)))
15458 complaint (_("DW_AT_alignment value too large"
15459 " - DIE at %s [in module %s]"),
15460 sect_offset_str (die->sect_off),
15461 objfile_name (cu->per_objfile->objfile));
15462 }
15463
15464 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15465 constant for a type, according to DWARF5 spec, Table 5.5. */
15466
15467 static bool
15468 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15469 {
15470 switch (value)
15471 {
15472 case DW_CC_normal:
15473 case DW_CC_pass_by_reference:
15474 case DW_CC_pass_by_value:
15475 return true;
15476
15477 default:
15478 complaint (_("unrecognized DW_AT_calling_convention value "
15479 "(%s) for a type"), pulongest (value));
15480 return false;
15481 }
15482 }
15483
15484 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15485 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15486 also according to GNU-specific values (see include/dwarf2.h). */
15487
15488 static bool
15489 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15490 {
15491 switch (value)
15492 {
15493 case DW_CC_normal:
15494 case DW_CC_program:
15495 case DW_CC_nocall:
15496 return true;
15497
15498 case DW_CC_GNU_renesas_sh:
15499 case DW_CC_GNU_borland_fastcall_i386:
15500 case DW_CC_GDB_IBM_OpenCL:
15501 return true;
15502
15503 default:
15504 complaint (_("unrecognized DW_AT_calling_convention value "
15505 "(%s) for a subroutine"), pulongest (value));
15506 return false;
15507 }
15508 }
15509
15510 /* Called when we find the DIE that starts a structure or union scope
15511 (definition) to create a type for the structure or union. Fill in
15512 the type's name and general properties; the members will not be
15513 processed until process_structure_scope. A symbol table entry for
15514 the type will also not be done until process_structure_scope (assuming
15515 the type has a name).
15516
15517 NOTE: we need to call these functions regardless of whether or not the
15518 DIE has a DW_AT_name attribute, since it might be an anonymous
15519 structure or union. This gets the type entered into our set of
15520 user defined types. */
15521
15522 static struct type *
15523 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15524 {
15525 struct objfile *objfile = cu->per_objfile->objfile;
15526 struct type *type;
15527 struct attribute *attr;
15528 const char *name;
15529
15530 /* If the definition of this type lives in .debug_types, read that type.
15531 Don't follow DW_AT_specification though, that will take us back up
15532 the chain and we want to go down. */
15533 attr = die->attr (DW_AT_signature);
15534 if (attr != nullptr)
15535 {
15536 type = get_DW_AT_signature_type (die, attr, cu);
15537
15538 /* The type's CU may not be the same as CU.
15539 Ensure TYPE is recorded with CU in die_type_hash. */
15540 return set_die_type (die, type, cu);
15541 }
15542
15543 type = alloc_type (objfile);
15544 INIT_CPLUS_SPECIFIC (type);
15545
15546 name = dwarf2_name (die, cu);
15547 if (name != NULL)
15548 {
15549 if (cu->language == language_cplus
15550 || cu->language == language_d
15551 || cu->language == language_rust)
15552 {
15553 const char *full_name = dwarf2_full_name (name, die, cu);
15554
15555 /* dwarf2_full_name might have already finished building the DIE's
15556 type. If so, there is no need to continue. */
15557 if (get_die_type (die, cu) != NULL)
15558 return get_die_type (die, cu);
15559
15560 type->set_name (full_name);
15561 }
15562 else
15563 {
15564 /* The name is already allocated along with this objfile, so
15565 we don't need to duplicate it for the type. */
15566 type->set_name (name);
15567 }
15568 }
15569
15570 if (die->tag == DW_TAG_structure_type)
15571 {
15572 type->set_code (TYPE_CODE_STRUCT);
15573 }
15574 else if (die->tag == DW_TAG_union_type)
15575 {
15576 type->set_code (TYPE_CODE_UNION);
15577 }
15578 else
15579 {
15580 type->set_code (TYPE_CODE_STRUCT);
15581 }
15582
15583 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15584 TYPE_DECLARED_CLASS (type) = 1;
15585
15586 /* Store the calling convention in the type if it's available in
15587 the die. Otherwise the calling convention remains set to
15588 the default value DW_CC_normal. */
15589 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15590 if (attr != nullptr
15591 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15592 {
15593 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15594 TYPE_CPLUS_CALLING_CONVENTION (type)
15595 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15596 }
15597
15598 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15599 if (attr != nullptr)
15600 {
15601 if (attr->form_is_constant ())
15602 TYPE_LENGTH (type) = DW_UNSND (attr);
15603 else
15604 {
15605 struct dynamic_prop prop;
15606 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15607 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15608 TYPE_LENGTH (type) = 0;
15609 }
15610 }
15611 else
15612 {
15613 TYPE_LENGTH (type) = 0;
15614 }
15615
15616 maybe_set_alignment (cu, die, type);
15617
15618 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15619 {
15620 /* ICC<14 does not output the required DW_AT_declaration on
15621 incomplete types, but gives them a size of zero. */
15622 TYPE_STUB (type) = 1;
15623 }
15624 else
15625 TYPE_STUB_SUPPORTED (type) = 1;
15626
15627 if (die_is_declaration (die, cu))
15628 TYPE_STUB (type) = 1;
15629 else if (attr == NULL && die->child == NULL
15630 && producer_is_realview (cu->producer))
15631 /* RealView does not output the required DW_AT_declaration
15632 on incomplete types. */
15633 TYPE_STUB (type) = 1;
15634
15635 /* We need to add the type field to the die immediately so we don't
15636 infinitely recurse when dealing with pointers to the structure
15637 type within the structure itself. */
15638 set_die_type (die, type, cu);
15639
15640 /* set_die_type should be already done. */
15641 set_descriptive_type (type, die, cu);
15642
15643 return type;
15644 }
15645
15646 static void handle_struct_member_die
15647 (struct die_info *child_die,
15648 struct type *type,
15649 struct field_info *fi,
15650 std::vector<struct symbol *> *template_args,
15651 struct dwarf2_cu *cu);
15652
15653 /* A helper for handle_struct_member_die that handles
15654 DW_TAG_variant_part. */
15655
15656 static void
15657 handle_variant_part (struct die_info *die, struct type *type,
15658 struct field_info *fi,
15659 std::vector<struct symbol *> *template_args,
15660 struct dwarf2_cu *cu)
15661 {
15662 variant_part_builder *new_part;
15663 if (fi->current_variant_part == nullptr)
15664 {
15665 fi->variant_parts.emplace_back ();
15666 new_part = &fi->variant_parts.back ();
15667 }
15668 else if (!fi->current_variant_part->processing_variant)
15669 {
15670 complaint (_("nested DW_TAG_variant_part seen "
15671 "- DIE at %s [in module %s]"),
15672 sect_offset_str (die->sect_off),
15673 objfile_name (cu->per_objfile->objfile));
15674 return;
15675 }
15676 else
15677 {
15678 variant_field &current = fi->current_variant_part->variants.back ();
15679 current.variant_parts.emplace_back ();
15680 new_part = &current.variant_parts.back ();
15681 }
15682
15683 /* When we recurse, we want callees to add to this new variant
15684 part. */
15685 scoped_restore save_current_variant_part
15686 = make_scoped_restore (&fi->current_variant_part, new_part);
15687
15688 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15689 if (discr == NULL)
15690 {
15691 /* It's a univariant form, an extension we support. */
15692 }
15693 else if (discr->form_is_ref ())
15694 {
15695 struct dwarf2_cu *target_cu = cu;
15696 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15697
15698 new_part->discriminant_offset = target_die->sect_off;
15699 }
15700 else
15701 {
15702 complaint (_("DW_AT_discr does not have DIE reference form"
15703 " - DIE at %s [in module %s]"),
15704 sect_offset_str (die->sect_off),
15705 objfile_name (cu->per_objfile->objfile));
15706 }
15707
15708 for (die_info *child_die = die->child;
15709 child_die != NULL;
15710 child_die = child_die->sibling)
15711 handle_struct_member_die (child_die, type, fi, template_args, cu);
15712 }
15713
15714 /* A helper for handle_struct_member_die that handles
15715 DW_TAG_variant. */
15716
15717 static void
15718 handle_variant (struct die_info *die, struct type *type,
15719 struct field_info *fi,
15720 std::vector<struct symbol *> *template_args,
15721 struct dwarf2_cu *cu)
15722 {
15723 if (fi->current_variant_part == nullptr)
15724 {
15725 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15726 "- DIE at %s [in module %s]"),
15727 sect_offset_str (die->sect_off),
15728 objfile_name (cu->per_objfile->objfile));
15729 return;
15730 }
15731 if (fi->current_variant_part->processing_variant)
15732 {
15733 complaint (_("nested DW_TAG_variant seen "
15734 "- DIE at %s [in module %s]"),
15735 sect_offset_str (die->sect_off),
15736 objfile_name (cu->per_objfile->objfile));
15737 return;
15738 }
15739
15740 scoped_restore save_processing_variant
15741 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15742 true);
15743
15744 fi->current_variant_part->variants.emplace_back ();
15745 variant_field &variant = fi->current_variant_part->variants.back ();
15746 variant.first_field = fi->fields.size ();
15747
15748 /* In a variant we want to get the discriminant and also add a
15749 field for our sole member child. */
15750 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15751 if (discr == nullptr)
15752 {
15753 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15754 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15755 variant.default_branch = true;
15756 else
15757 variant.discr_list_data = DW_BLOCK (discr);
15758 }
15759 else
15760 variant.discriminant_value = DW_UNSND (discr);
15761
15762 for (die_info *variant_child = die->child;
15763 variant_child != NULL;
15764 variant_child = variant_child->sibling)
15765 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15766
15767 variant.last_field = fi->fields.size ();
15768 }
15769
15770 /* A helper for process_structure_scope that handles a single member
15771 DIE. */
15772
15773 static void
15774 handle_struct_member_die (struct die_info *child_die, struct type *type,
15775 struct field_info *fi,
15776 std::vector<struct symbol *> *template_args,
15777 struct dwarf2_cu *cu)
15778 {
15779 if (child_die->tag == DW_TAG_member
15780 || child_die->tag == DW_TAG_variable)
15781 {
15782 /* NOTE: carlton/2002-11-05: A C++ static data member
15783 should be a DW_TAG_member that is a declaration, but
15784 all versions of G++ as of this writing (so through at
15785 least 3.2.1) incorrectly generate DW_TAG_variable
15786 tags for them instead. */
15787 dwarf2_add_field (fi, child_die, cu);
15788 }
15789 else if (child_die->tag == DW_TAG_subprogram)
15790 {
15791 /* Rust doesn't have member functions in the C++ sense.
15792 However, it does emit ordinary functions as children
15793 of a struct DIE. */
15794 if (cu->language == language_rust)
15795 read_func_scope (child_die, cu);
15796 else
15797 {
15798 /* C++ member function. */
15799 dwarf2_add_member_fn (fi, child_die, type, cu);
15800 }
15801 }
15802 else if (child_die->tag == DW_TAG_inheritance)
15803 {
15804 /* C++ base class field. */
15805 dwarf2_add_field (fi, child_die, cu);
15806 }
15807 else if (type_can_define_types (child_die))
15808 dwarf2_add_type_defn (fi, child_die, cu);
15809 else if (child_die->tag == DW_TAG_template_type_param
15810 || child_die->tag == DW_TAG_template_value_param)
15811 {
15812 struct symbol *arg = new_symbol (child_die, NULL, cu);
15813
15814 if (arg != NULL)
15815 template_args->push_back (arg);
15816 }
15817 else if (child_die->tag == DW_TAG_variant_part)
15818 handle_variant_part (child_die, type, fi, template_args, cu);
15819 else if (child_die->tag == DW_TAG_variant)
15820 handle_variant (child_die, type, fi, template_args, cu);
15821 }
15822
15823 /* Finish creating a structure or union type, including filling in
15824 its members and creating a symbol for it. */
15825
15826 static void
15827 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15828 {
15829 struct objfile *objfile = cu->per_objfile->objfile;
15830 struct die_info *child_die;
15831 struct type *type;
15832
15833 type = get_die_type (die, cu);
15834 if (type == NULL)
15835 type = read_structure_type (die, cu);
15836
15837 bool has_template_parameters = false;
15838 if (die->child != NULL && ! die_is_declaration (die, cu))
15839 {
15840 struct field_info fi;
15841 std::vector<struct symbol *> template_args;
15842
15843 child_die = die->child;
15844
15845 while (child_die && child_die->tag)
15846 {
15847 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15848 child_die = child_die->sibling;
15849 }
15850
15851 /* Attach template arguments to type. */
15852 if (!template_args.empty ())
15853 {
15854 has_template_parameters = true;
15855 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15856 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15857 TYPE_TEMPLATE_ARGUMENTS (type)
15858 = XOBNEWVEC (&objfile->objfile_obstack,
15859 struct symbol *,
15860 TYPE_N_TEMPLATE_ARGUMENTS (type));
15861 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15862 template_args.data (),
15863 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15864 * sizeof (struct symbol *)));
15865 }
15866
15867 /* Attach fields and member functions to the type. */
15868 if (fi.nfields () > 0)
15869 dwarf2_attach_fields_to_type (&fi, type, cu);
15870 if (!fi.fnfieldlists.empty ())
15871 {
15872 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15873
15874 /* Get the type which refers to the base class (possibly this
15875 class itself) which contains the vtable pointer for the current
15876 class from the DW_AT_containing_type attribute. This use of
15877 DW_AT_containing_type is a GNU extension. */
15878
15879 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15880 {
15881 struct type *t = die_containing_type (die, cu);
15882
15883 set_type_vptr_basetype (type, t);
15884 if (type == t)
15885 {
15886 int i;
15887
15888 /* Our own class provides vtbl ptr. */
15889 for (i = t->num_fields () - 1;
15890 i >= TYPE_N_BASECLASSES (t);
15891 --i)
15892 {
15893 const char *fieldname = TYPE_FIELD_NAME (t, i);
15894
15895 if (is_vtable_name (fieldname, cu))
15896 {
15897 set_type_vptr_fieldno (type, i);
15898 break;
15899 }
15900 }
15901
15902 /* Complain if virtual function table field not found. */
15903 if (i < TYPE_N_BASECLASSES (t))
15904 complaint (_("virtual function table pointer "
15905 "not found when defining class '%s'"),
15906 type->name () ? type->name () : "");
15907 }
15908 else
15909 {
15910 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15911 }
15912 }
15913 else if (cu->producer
15914 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15915 {
15916 /* The IBM XLC compiler does not provide direct indication
15917 of the containing type, but the vtable pointer is
15918 always named __vfp. */
15919
15920 int i;
15921
15922 for (i = type->num_fields () - 1;
15923 i >= TYPE_N_BASECLASSES (type);
15924 --i)
15925 {
15926 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15927 {
15928 set_type_vptr_fieldno (type, i);
15929 set_type_vptr_basetype (type, type);
15930 break;
15931 }
15932 }
15933 }
15934 }
15935
15936 /* Copy fi.typedef_field_list linked list elements content into the
15937 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15938 if (!fi.typedef_field_list.empty ())
15939 {
15940 int count = fi.typedef_field_list.size ();
15941
15942 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15943 TYPE_TYPEDEF_FIELD_ARRAY (type)
15944 = ((struct decl_field *)
15945 TYPE_ALLOC (type,
15946 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15947 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15948
15949 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15950 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15951 }
15952
15953 /* Copy fi.nested_types_list linked list elements content into the
15954 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15955 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15956 {
15957 int count = fi.nested_types_list.size ();
15958
15959 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15960 TYPE_NESTED_TYPES_ARRAY (type)
15961 = ((struct decl_field *)
15962 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15963 TYPE_NESTED_TYPES_COUNT (type) = count;
15964
15965 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15966 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15967 }
15968 }
15969
15970 quirk_gcc_member_function_pointer (type, objfile);
15971 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15972 cu->rust_unions.push_back (type);
15973
15974 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15975 snapshots) has been known to create a die giving a declaration
15976 for a class that has, as a child, a die giving a definition for a
15977 nested class. So we have to process our children even if the
15978 current die is a declaration. Normally, of course, a declaration
15979 won't have any children at all. */
15980
15981 child_die = die->child;
15982
15983 while (child_die != NULL && child_die->tag)
15984 {
15985 if (child_die->tag == DW_TAG_member
15986 || child_die->tag == DW_TAG_variable
15987 || child_die->tag == DW_TAG_inheritance
15988 || child_die->tag == DW_TAG_template_value_param
15989 || child_die->tag == DW_TAG_template_type_param)
15990 {
15991 /* Do nothing. */
15992 }
15993 else
15994 process_die (child_die, cu);
15995
15996 child_die = child_die->sibling;
15997 }
15998
15999 /* Do not consider external references. According to the DWARF standard,
16000 these DIEs are identified by the fact that they have no byte_size
16001 attribute, and a declaration attribute. */
16002 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16003 || !die_is_declaration (die, cu)
16004 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16005 {
16006 struct symbol *sym = new_symbol (die, type, cu);
16007
16008 if (has_template_parameters)
16009 {
16010 struct symtab *symtab;
16011 if (sym != nullptr)
16012 symtab = symbol_symtab (sym);
16013 else if (cu->line_header != nullptr)
16014 {
16015 /* Any related symtab will do. */
16016 symtab
16017 = cu->line_header->file_names ()[0].symtab;
16018 }
16019 else
16020 {
16021 symtab = nullptr;
16022 complaint (_("could not find suitable "
16023 "symtab for template parameter"
16024 " - DIE at %s [in module %s]"),
16025 sect_offset_str (die->sect_off),
16026 objfile_name (objfile));
16027 }
16028
16029 if (symtab != nullptr)
16030 {
16031 /* Make sure that the symtab is set on the new symbols.
16032 Even though they don't appear in this symtab directly,
16033 other parts of gdb assume that symbols do, and this is
16034 reasonably true. */
16035 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16036 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16037 }
16038 }
16039 }
16040 }
16041
16042 /* Assuming DIE is an enumeration type, and TYPE is its associated
16043 type, update TYPE using some information only available in DIE's
16044 children. In particular, the fields are computed. */
16045
16046 static void
16047 update_enumeration_type_from_children (struct die_info *die,
16048 struct type *type,
16049 struct dwarf2_cu *cu)
16050 {
16051 struct die_info *child_die;
16052 int unsigned_enum = 1;
16053 int flag_enum = 1;
16054
16055 auto_obstack obstack;
16056 std::vector<struct field> fields;
16057
16058 for (child_die = die->child;
16059 child_die != NULL && child_die->tag;
16060 child_die = child_die->sibling)
16061 {
16062 struct attribute *attr;
16063 LONGEST value;
16064 const gdb_byte *bytes;
16065 struct dwarf2_locexpr_baton *baton;
16066 const char *name;
16067
16068 if (child_die->tag != DW_TAG_enumerator)
16069 continue;
16070
16071 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16072 if (attr == NULL)
16073 continue;
16074
16075 name = dwarf2_name (child_die, cu);
16076 if (name == NULL)
16077 name = "<anonymous enumerator>";
16078
16079 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16080 &value, &bytes, &baton);
16081 if (value < 0)
16082 {
16083 unsigned_enum = 0;
16084 flag_enum = 0;
16085 }
16086 else
16087 {
16088 if (count_one_bits_ll (value) >= 2)
16089 flag_enum = 0;
16090 }
16091
16092 fields.emplace_back ();
16093 struct field &field = fields.back ();
16094 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16095 SET_FIELD_ENUMVAL (field, value);
16096 }
16097
16098 if (!fields.empty ())
16099 {
16100 type->set_num_fields (fields.size ());
16101 type->set_fields
16102 ((struct field *)
16103 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16104 memcpy (type->fields (), fields.data (),
16105 sizeof (struct field) * fields.size ());
16106 }
16107
16108 if (unsigned_enum)
16109 TYPE_UNSIGNED (type) = 1;
16110 if (flag_enum)
16111 TYPE_FLAG_ENUM (type) = 1;
16112 }
16113
16114 /* Given a DW_AT_enumeration_type die, set its type. We do not
16115 complete the type's fields yet, or create any symbols. */
16116
16117 static struct type *
16118 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16119 {
16120 struct objfile *objfile = cu->per_objfile->objfile;
16121 struct type *type;
16122 struct attribute *attr;
16123 const char *name;
16124
16125 /* If the definition of this type lives in .debug_types, read that type.
16126 Don't follow DW_AT_specification though, that will take us back up
16127 the chain and we want to go down. */
16128 attr = die->attr (DW_AT_signature);
16129 if (attr != nullptr)
16130 {
16131 type = get_DW_AT_signature_type (die, attr, cu);
16132
16133 /* The type's CU may not be the same as CU.
16134 Ensure TYPE is recorded with CU in die_type_hash. */
16135 return set_die_type (die, type, cu);
16136 }
16137
16138 type = alloc_type (objfile);
16139
16140 type->set_code (TYPE_CODE_ENUM);
16141 name = dwarf2_full_name (NULL, die, cu);
16142 if (name != NULL)
16143 type->set_name (name);
16144
16145 attr = dwarf2_attr (die, DW_AT_type, cu);
16146 if (attr != NULL)
16147 {
16148 struct type *underlying_type = die_type (die, cu);
16149
16150 TYPE_TARGET_TYPE (type) = underlying_type;
16151 }
16152
16153 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16154 if (attr != nullptr)
16155 {
16156 TYPE_LENGTH (type) = DW_UNSND (attr);
16157 }
16158 else
16159 {
16160 TYPE_LENGTH (type) = 0;
16161 }
16162
16163 maybe_set_alignment (cu, die, type);
16164
16165 /* The enumeration DIE can be incomplete. In Ada, any type can be
16166 declared as private in the package spec, and then defined only
16167 inside the package body. Such types are known as Taft Amendment
16168 Types. When another package uses such a type, an incomplete DIE
16169 may be generated by the compiler. */
16170 if (die_is_declaration (die, cu))
16171 TYPE_STUB (type) = 1;
16172
16173 /* If this type has an underlying type that is not a stub, then we
16174 may use its attributes. We always use the "unsigned" attribute
16175 in this situation, because ordinarily we guess whether the type
16176 is unsigned -- but the guess can be wrong and the underlying type
16177 can tell us the reality. However, we defer to a local size
16178 attribute if one exists, because this lets the compiler override
16179 the underlying type if needed. */
16180 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16181 {
16182 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16183 underlying_type = check_typedef (underlying_type);
16184 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
16185 if (TYPE_LENGTH (type) == 0)
16186 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16187 if (TYPE_RAW_ALIGN (type) == 0
16188 && TYPE_RAW_ALIGN (underlying_type) != 0)
16189 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16190 }
16191
16192 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16193
16194 set_die_type (die, type, cu);
16195
16196 /* Finish the creation of this type by using the enum's children.
16197 Note that, as usual, this must come after set_die_type to avoid
16198 infinite recursion when trying to compute the names of the
16199 enumerators. */
16200 update_enumeration_type_from_children (die, type, cu);
16201
16202 return type;
16203 }
16204
16205 /* Given a pointer to a die which begins an enumeration, process all
16206 the dies that define the members of the enumeration, and create the
16207 symbol for the enumeration type.
16208
16209 NOTE: We reverse the order of the element list. */
16210
16211 static void
16212 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16213 {
16214 struct type *this_type;
16215
16216 this_type = get_die_type (die, cu);
16217 if (this_type == NULL)
16218 this_type = read_enumeration_type (die, cu);
16219
16220 if (die->child != NULL)
16221 {
16222 struct die_info *child_die;
16223 const char *name;
16224
16225 child_die = die->child;
16226 while (child_die && child_die->tag)
16227 {
16228 if (child_die->tag != DW_TAG_enumerator)
16229 {
16230 process_die (child_die, cu);
16231 }
16232 else
16233 {
16234 name = dwarf2_name (child_die, cu);
16235 if (name)
16236 new_symbol (child_die, this_type, cu);
16237 }
16238
16239 child_die = child_die->sibling;
16240 }
16241 }
16242
16243 /* If we are reading an enum from a .debug_types unit, and the enum
16244 is a declaration, and the enum is not the signatured type in the
16245 unit, then we do not want to add a symbol for it. Adding a
16246 symbol would in some cases obscure the true definition of the
16247 enum, giving users an incomplete type when the definition is
16248 actually available. Note that we do not want to do this for all
16249 enums which are just declarations, because C++0x allows forward
16250 enum declarations. */
16251 if (cu->per_cu->is_debug_types
16252 && die_is_declaration (die, cu))
16253 {
16254 struct signatured_type *sig_type;
16255
16256 sig_type = (struct signatured_type *) cu->per_cu;
16257 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16258 if (sig_type->type_offset_in_section != die->sect_off)
16259 return;
16260 }
16261
16262 new_symbol (die, this_type, cu);
16263 }
16264
16265 /* Extract all information from a DW_TAG_array_type DIE and put it in
16266 the DIE's type field. For now, this only handles one dimensional
16267 arrays. */
16268
16269 static struct type *
16270 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16271 {
16272 struct objfile *objfile = cu->per_objfile->objfile;
16273 struct die_info *child_die;
16274 struct type *type;
16275 struct type *element_type, *range_type, *index_type;
16276 struct attribute *attr;
16277 const char *name;
16278 struct dynamic_prop *byte_stride_prop = NULL;
16279 unsigned int bit_stride = 0;
16280
16281 element_type = die_type (die, cu);
16282
16283 /* The die_type call above may have already set the type for this DIE. */
16284 type = get_die_type (die, cu);
16285 if (type)
16286 return type;
16287
16288 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16289 if (attr != NULL)
16290 {
16291 int stride_ok;
16292 struct type *prop_type = cu->addr_sized_int_type (false);
16293
16294 byte_stride_prop
16295 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16296 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16297 prop_type);
16298 if (!stride_ok)
16299 {
16300 complaint (_("unable to read array DW_AT_byte_stride "
16301 " - DIE at %s [in module %s]"),
16302 sect_offset_str (die->sect_off),
16303 objfile_name (cu->per_objfile->objfile));
16304 /* Ignore this attribute. We will likely not be able to print
16305 arrays of this type correctly, but there is little we can do
16306 to help if we cannot read the attribute's value. */
16307 byte_stride_prop = NULL;
16308 }
16309 }
16310
16311 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16312 if (attr != NULL)
16313 bit_stride = DW_UNSND (attr);
16314
16315 /* Irix 6.2 native cc creates array types without children for
16316 arrays with unspecified length. */
16317 if (die->child == NULL)
16318 {
16319 index_type = objfile_type (objfile)->builtin_int;
16320 range_type = create_static_range_type (NULL, index_type, 0, -1);
16321 type = create_array_type_with_stride (NULL, element_type, range_type,
16322 byte_stride_prop, bit_stride);
16323 return set_die_type (die, type, cu);
16324 }
16325
16326 std::vector<struct type *> range_types;
16327 child_die = die->child;
16328 while (child_die && child_die->tag)
16329 {
16330 if (child_die->tag == DW_TAG_subrange_type)
16331 {
16332 struct type *child_type = read_type_die (child_die, cu);
16333
16334 if (child_type != NULL)
16335 {
16336 /* The range type was succesfully read. Save it for the
16337 array type creation. */
16338 range_types.push_back (child_type);
16339 }
16340 }
16341 child_die = child_die->sibling;
16342 }
16343
16344 /* Dwarf2 dimensions are output from left to right, create the
16345 necessary array types in backwards order. */
16346
16347 type = element_type;
16348
16349 if (read_array_order (die, cu) == DW_ORD_col_major)
16350 {
16351 int i = 0;
16352
16353 while (i < range_types.size ())
16354 type = create_array_type_with_stride (NULL, type, range_types[i++],
16355 byte_stride_prop, bit_stride);
16356 }
16357 else
16358 {
16359 size_t ndim = range_types.size ();
16360 while (ndim-- > 0)
16361 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16362 byte_stride_prop, bit_stride);
16363 }
16364
16365 /* Understand Dwarf2 support for vector types (like they occur on
16366 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16367 array type. This is not part of the Dwarf2/3 standard yet, but a
16368 custom vendor extension. The main difference between a regular
16369 array and the vector variant is that vectors are passed by value
16370 to functions. */
16371 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16372 if (attr != nullptr)
16373 make_vector_type (type);
16374
16375 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16376 implementation may choose to implement triple vectors using this
16377 attribute. */
16378 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16379 if (attr != nullptr)
16380 {
16381 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16382 TYPE_LENGTH (type) = DW_UNSND (attr);
16383 else
16384 complaint (_("DW_AT_byte_size for array type smaller "
16385 "than the total size of elements"));
16386 }
16387
16388 name = dwarf2_name (die, cu);
16389 if (name)
16390 type->set_name (name);
16391
16392 maybe_set_alignment (cu, die, type);
16393
16394 /* Install the type in the die. */
16395 set_die_type (die, type, cu);
16396
16397 /* set_die_type should be already done. */
16398 set_descriptive_type (type, die, cu);
16399
16400 return type;
16401 }
16402
16403 static enum dwarf_array_dim_ordering
16404 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16405 {
16406 struct attribute *attr;
16407
16408 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16409
16410 if (attr != nullptr)
16411 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16412
16413 /* GNU F77 is a special case, as at 08/2004 array type info is the
16414 opposite order to the dwarf2 specification, but data is still
16415 laid out as per normal fortran.
16416
16417 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16418 version checking. */
16419
16420 if (cu->language == language_fortran
16421 && cu->producer && strstr (cu->producer, "GNU F77"))
16422 {
16423 return DW_ORD_row_major;
16424 }
16425
16426 switch (cu->language_defn->la_array_ordering)
16427 {
16428 case array_column_major:
16429 return DW_ORD_col_major;
16430 case array_row_major:
16431 default:
16432 return DW_ORD_row_major;
16433 };
16434 }
16435
16436 /* Extract all information from a DW_TAG_set_type DIE and put it in
16437 the DIE's type field. */
16438
16439 static struct type *
16440 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16441 {
16442 struct type *domain_type, *set_type;
16443 struct attribute *attr;
16444
16445 domain_type = die_type (die, cu);
16446
16447 /* The die_type call above may have already set the type for this DIE. */
16448 set_type = get_die_type (die, cu);
16449 if (set_type)
16450 return set_type;
16451
16452 set_type = create_set_type (NULL, domain_type);
16453
16454 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16455 if (attr != nullptr)
16456 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16457
16458 maybe_set_alignment (cu, die, set_type);
16459
16460 return set_die_type (die, set_type, cu);
16461 }
16462
16463 /* A helper for read_common_block that creates a locexpr baton.
16464 SYM is the symbol which we are marking as computed.
16465 COMMON_DIE is the DIE for the common block.
16466 COMMON_LOC is the location expression attribute for the common
16467 block itself.
16468 MEMBER_LOC is the location expression attribute for the particular
16469 member of the common block that we are processing.
16470 CU is the CU from which the above come. */
16471
16472 static void
16473 mark_common_block_symbol_computed (struct symbol *sym,
16474 struct die_info *common_die,
16475 struct attribute *common_loc,
16476 struct attribute *member_loc,
16477 struct dwarf2_cu *cu)
16478 {
16479 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16480 struct objfile *objfile = per_objfile->objfile;
16481 struct dwarf2_locexpr_baton *baton;
16482 gdb_byte *ptr;
16483 unsigned int cu_off;
16484 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16485 LONGEST offset = 0;
16486
16487 gdb_assert (common_loc && member_loc);
16488 gdb_assert (common_loc->form_is_block ());
16489 gdb_assert (member_loc->form_is_block ()
16490 || member_loc->form_is_constant ());
16491
16492 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16493 baton->per_objfile = per_objfile;
16494 baton->per_cu = cu->per_cu;
16495 gdb_assert (baton->per_cu);
16496
16497 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16498
16499 if (member_loc->form_is_constant ())
16500 {
16501 offset = member_loc->constant_value (0);
16502 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16503 }
16504 else
16505 baton->size += DW_BLOCK (member_loc)->size;
16506
16507 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16508 baton->data = ptr;
16509
16510 *ptr++ = DW_OP_call4;
16511 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16512 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16513 ptr += 4;
16514
16515 if (member_loc->form_is_constant ())
16516 {
16517 *ptr++ = DW_OP_addr;
16518 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16519 ptr += cu->header.addr_size;
16520 }
16521 else
16522 {
16523 /* We have to copy the data here, because DW_OP_call4 will only
16524 use a DW_AT_location attribute. */
16525 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16526 ptr += DW_BLOCK (member_loc)->size;
16527 }
16528
16529 *ptr++ = DW_OP_plus;
16530 gdb_assert (ptr - baton->data == baton->size);
16531
16532 SYMBOL_LOCATION_BATON (sym) = baton;
16533 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16534 }
16535
16536 /* Create appropriate locally-scoped variables for all the
16537 DW_TAG_common_block entries. Also create a struct common_block
16538 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16539 is used to separate the common blocks name namespace from regular
16540 variable names. */
16541
16542 static void
16543 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16544 {
16545 struct attribute *attr;
16546
16547 attr = dwarf2_attr (die, DW_AT_location, cu);
16548 if (attr != nullptr)
16549 {
16550 /* Support the .debug_loc offsets. */
16551 if (attr->form_is_block ())
16552 {
16553 /* Ok. */
16554 }
16555 else if (attr->form_is_section_offset ())
16556 {
16557 dwarf2_complex_location_expr_complaint ();
16558 attr = NULL;
16559 }
16560 else
16561 {
16562 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16563 "common block member");
16564 attr = NULL;
16565 }
16566 }
16567
16568 if (die->child != NULL)
16569 {
16570 struct objfile *objfile = cu->per_objfile->objfile;
16571 struct die_info *child_die;
16572 size_t n_entries = 0, size;
16573 struct common_block *common_block;
16574 struct symbol *sym;
16575
16576 for (child_die = die->child;
16577 child_die && child_die->tag;
16578 child_die = child_die->sibling)
16579 ++n_entries;
16580
16581 size = (sizeof (struct common_block)
16582 + (n_entries - 1) * sizeof (struct symbol *));
16583 common_block
16584 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16585 size);
16586 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16587 common_block->n_entries = 0;
16588
16589 for (child_die = die->child;
16590 child_die && child_die->tag;
16591 child_die = child_die->sibling)
16592 {
16593 /* Create the symbol in the DW_TAG_common_block block in the current
16594 symbol scope. */
16595 sym = new_symbol (child_die, NULL, cu);
16596 if (sym != NULL)
16597 {
16598 struct attribute *member_loc;
16599
16600 common_block->contents[common_block->n_entries++] = sym;
16601
16602 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16603 cu);
16604 if (member_loc)
16605 {
16606 /* GDB has handled this for a long time, but it is
16607 not specified by DWARF. It seems to have been
16608 emitted by gfortran at least as recently as:
16609 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16610 complaint (_("Variable in common block has "
16611 "DW_AT_data_member_location "
16612 "- DIE at %s [in module %s]"),
16613 sect_offset_str (child_die->sect_off),
16614 objfile_name (objfile));
16615
16616 if (member_loc->form_is_section_offset ())
16617 dwarf2_complex_location_expr_complaint ();
16618 else if (member_loc->form_is_constant ()
16619 || member_loc->form_is_block ())
16620 {
16621 if (attr != nullptr)
16622 mark_common_block_symbol_computed (sym, die, attr,
16623 member_loc, cu);
16624 }
16625 else
16626 dwarf2_complex_location_expr_complaint ();
16627 }
16628 }
16629 }
16630
16631 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16632 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16633 }
16634 }
16635
16636 /* Create a type for a C++ namespace. */
16637
16638 static struct type *
16639 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16640 {
16641 struct objfile *objfile = cu->per_objfile->objfile;
16642 const char *previous_prefix, *name;
16643 int is_anonymous;
16644 struct type *type;
16645
16646 /* For extensions, reuse the type of the original namespace. */
16647 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16648 {
16649 struct die_info *ext_die;
16650 struct dwarf2_cu *ext_cu = cu;
16651
16652 ext_die = dwarf2_extension (die, &ext_cu);
16653 type = read_type_die (ext_die, ext_cu);
16654
16655 /* EXT_CU may not be the same as CU.
16656 Ensure TYPE is recorded with CU in die_type_hash. */
16657 return set_die_type (die, type, cu);
16658 }
16659
16660 name = namespace_name (die, &is_anonymous, cu);
16661
16662 /* Now build the name of the current namespace. */
16663
16664 previous_prefix = determine_prefix (die, cu);
16665 if (previous_prefix[0] != '\0')
16666 name = typename_concat (&objfile->objfile_obstack,
16667 previous_prefix, name, 0, cu);
16668
16669 /* Create the type. */
16670 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16671
16672 return set_die_type (die, type, cu);
16673 }
16674
16675 /* Read a namespace scope. */
16676
16677 static void
16678 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16679 {
16680 struct objfile *objfile = cu->per_objfile->objfile;
16681 int is_anonymous;
16682
16683 /* Add a symbol associated to this if we haven't seen the namespace
16684 before. Also, add a using directive if it's an anonymous
16685 namespace. */
16686
16687 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16688 {
16689 struct type *type;
16690
16691 type = read_type_die (die, cu);
16692 new_symbol (die, type, cu);
16693
16694 namespace_name (die, &is_anonymous, cu);
16695 if (is_anonymous)
16696 {
16697 const char *previous_prefix = determine_prefix (die, cu);
16698
16699 std::vector<const char *> excludes;
16700 add_using_directive (using_directives (cu),
16701 previous_prefix, type->name (), NULL,
16702 NULL, excludes, 0, &objfile->objfile_obstack);
16703 }
16704 }
16705
16706 if (die->child != NULL)
16707 {
16708 struct die_info *child_die = die->child;
16709
16710 while (child_die && child_die->tag)
16711 {
16712 process_die (child_die, cu);
16713 child_die = child_die->sibling;
16714 }
16715 }
16716 }
16717
16718 /* Read a Fortran module as type. This DIE can be only a declaration used for
16719 imported module. Still we need that type as local Fortran "use ... only"
16720 declaration imports depend on the created type in determine_prefix. */
16721
16722 static struct type *
16723 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16724 {
16725 struct objfile *objfile = cu->per_objfile->objfile;
16726 const char *module_name;
16727 struct type *type;
16728
16729 module_name = dwarf2_name (die, cu);
16730 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16731
16732 return set_die_type (die, type, cu);
16733 }
16734
16735 /* Read a Fortran module. */
16736
16737 static void
16738 read_module (struct die_info *die, struct dwarf2_cu *cu)
16739 {
16740 struct die_info *child_die = die->child;
16741 struct type *type;
16742
16743 type = read_type_die (die, cu);
16744 new_symbol (die, type, cu);
16745
16746 while (child_die && child_die->tag)
16747 {
16748 process_die (child_die, cu);
16749 child_die = child_die->sibling;
16750 }
16751 }
16752
16753 /* Return the name of the namespace represented by DIE. Set
16754 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16755 namespace. */
16756
16757 static const char *
16758 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16759 {
16760 struct die_info *current_die;
16761 const char *name = NULL;
16762
16763 /* Loop through the extensions until we find a name. */
16764
16765 for (current_die = die;
16766 current_die != NULL;
16767 current_die = dwarf2_extension (die, &cu))
16768 {
16769 /* We don't use dwarf2_name here so that we can detect the absence
16770 of a name -> anonymous namespace. */
16771 name = dwarf2_string_attr (die, DW_AT_name, cu);
16772
16773 if (name != NULL)
16774 break;
16775 }
16776
16777 /* Is it an anonymous namespace? */
16778
16779 *is_anonymous = (name == NULL);
16780 if (*is_anonymous)
16781 name = CP_ANONYMOUS_NAMESPACE_STR;
16782
16783 return name;
16784 }
16785
16786 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16787 the user defined type vector. */
16788
16789 static struct type *
16790 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16791 {
16792 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16793 struct comp_unit_head *cu_header = &cu->header;
16794 struct type *type;
16795 struct attribute *attr_byte_size;
16796 struct attribute *attr_address_class;
16797 int byte_size, addr_class;
16798 struct type *target_type;
16799
16800 target_type = die_type (die, cu);
16801
16802 /* The die_type call above may have already set the type for this DIE. */
16803 type = get_die_type (die, cu);
16804 if (type)
16805 return type;
16806
16807 type = lookup_pointer_type (target_type);
16808
16809 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16810 if (attr_byte_size)
16811 byte_size = DW_UNSND (attr_byte_size);
16812 else
16813 byte_size = cu_header->addr_size;
16814
16815 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16816 if (attr_address_class)
16817 addr_class = DW_UNSND (attr_address_class);
16818 else
16819 addr_class = DW_ADDR_none;
16820
16821 ULONGEST alignment = get_alignment (cu, die);
16822
16823 /* If the pointer size, alignment, or address class is different
16824 than the default, create a type variant marked as such and set
16825 the length accordingly. */
16826 if (TYPE_LENGTH (type) != byte_size
16827 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16828 && alignment != TYPE_RAW_ALIGN (type))
16829 || addr_class != DW_ADDR_none)
16830 {
16831 if (gdbarch_address_class_type_flags_p (gdbarch))
16832 {
16833 int type_flags;
16834
16835 type_flags = gdbarch_address_class_type_flags
16836 (gdbarch, byte_size, addr_class);
16837 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16838 == 0);
16839 type = make_type_with_address_space (type, type_flags);
16840 }
16841 else if (TYPE_LENGTH (type) != byte_size)
16842 {
16843 complaint (_("invalid pointer size %d"), byte_size);
16844 }
16845 else if (TYPE_RAW_ALIGN (type) != alignment)
16846 {
16847 complaint (_("Invalid DW_AT_alignment"
16848 " - DIE at %s [in module %s]"),
16849 sect_offset_str (die->sect_off),
16850 objfile_name (cu->per_objfile->objfile));
16851 }
16852 else
16853 {
16854 /* Should we also complain about unhandled address classes? */
16855 }
16856 }
16857
16858 TYPE_LENGTH (type) = byte_size;
16859 set_type_align (type, alignment);
16860 return set_die_type (die, type, cu);
16861 }
16862
16863 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16864 the user defined type vector. */
16865
16866 static struct type *
16867 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16868 {
16869 struct type *type;
16870 struct type *to_type;
16871 struct type *domain;
16872
16873 to_type = die_type (die, cu);
16874 domain = die_containing_type (die, cu);
16875
16876 /* The calls above may have already set the type for this DIE. */
16877 type = get_die_type (die, cu);
16878 if (type)
16879 return type;
16880
16881 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16882 type = lookup_methodptr_type (to_type);
16883 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16884 {
16885 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16886
16887 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16888 to_type->fields (), to_type->num_fields (),
16889 TYPE_VARARGS (to_type));
16890 type = lookup_methodptr_type (new_type);
16891 }
16892 else
16893 type = lookup_memberptr_type (to_type, domain);
16894
16895 return set_die_type (die, type, cu);
16896 }
16897
16898 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16899 the user defined type vector. */
16900
16901 static struct type *
16902 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16903 enum type_code refcode)
16904 {
16905 struct comp_unit_head *cu_header = &cu->header;
16906 struct type *type, *target_type;
16907 struct attribute *attr;
16908
16909 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16910
16911 target_type = die_type (die, cu);
16912
16913 /* The die_type call above may have already set the type for this DIE. */
16914 type = get_die_type (die, cu);
16915 if (type)
16916 return type;
16917
16918 type = lookup_reference_type (target_type, refcode);
16919 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16920 if (attr != nullptr)
16921 {
16922 TYPE_LENGTH (type) = DW_UNSND (attr);
16923 }
16924 else
16925 {
16926 TYPE_LENGTH (type) = cu_header->addr_size;
16927 }
16928 maybe_set_alignment (cu, die, type);
16929 return set_die_type (die, type, cu);
16930 }
16931
16932 /* Add the given cv-qualifiers to the element type of the array. GCC
16933 outputs DWARF type qualifiers that apply to an array, not the
16934 element type. But GDB relies on the array element type to carry
16935 the cv-qualifiers. This mimics section 6.7.3 of the C99
16936 specification. */
16937
16938 static struct type *
16939 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16940 struct type *base_type, int cnst, int voltl)
16941 {
16942 struct type *el_type, *inner_array;
16943
16944 base_type = copy_type (base_type);
16945 inner_array = base_type;
16946
16947 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16948 {
16949 TYPE_TARGET_TYPE (inner_array) =
16950 copy_type (TYPE_TARGET_TYPE (inner_array));
16951 inner_array = TYPE_TARGET_TYPE (inner_array);
16952 }
16953
16954 el_type = TYPE_TARGET_TYPE (inner_array);
16955 cnst |= TYPE_CONST (el_type);
16956 voltl |= TYPE_VOLATILE (el_type);
16957 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16958
16959 return set_die_type (die, base_type, cu);
16960 }
16961
16962 static struct type *
16963 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16964 {
16965 struct type *base_type, *cv_type;
16966
16967 base_type = die_type (die, cu);
16968
16969 /* The die_type call above may have already set the type for this DIE. */
16970 cv_type = get_die_type (die, cu);
16971 if (cv_type)
16972 return cv_type;
16973
16974 /* In case the const qualifier is applied to an array type, the element type
16975 is so qualified, not the array type (section 6.7.3 of C99). */
16976 if (base_type->code () == TYPE_CODE_ARRAY)
16977 return add_array_cv_type (die, cu, base_type, 1, 0);
16978
16979 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16980 return set_die_type (die, cv_type, cu);
16981 }
16982
16983 static struct type *
16984 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16985 {
16986 struct type *base_type, *cv_type;
16987
16988 base_type = die_type (die, cu);
16989
16990 /* The die_type call above may have already set the type for this DIE. */
16991 cv_type = get_die_type (die, cu);
16992 if (cv_type)
16993 return cv_type;
16994
16995 /* In case the volatile qualifier is applied to an array type, the
16996 element type is so qualified, not the array type (section 6.7.3
16997 of C99). */
16998 if (base_type->code () == TYPE_CODE_ARRAY)
16999 return add_array_cv_type (die, cu, base_type, 0, 1);
17000
17001 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17002 return set_die_type (die, cv_type, cu);
17003 }
17004
17005 /* Handle DW_TAG_restrict_type. */
17006
17007 static struct type *
17008 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17009 {
17010 struct type *base_type, *cv_type;
17011
17012 base_type = die_type (die, cu);
17013
17014 /* The die_type call above may have already set the type for this DIE. */
17015 cv_type = get_die_type (die, cu);
17016 if (cv_type)
17017 return cv_type;
17018
17019 cv_type = make_restrict_type (base_type);
17020 return set_die_type (die, cv_type, cu);
17021 }
17022
17023 /* Handle DW_TAG_atomic_type. */
17024
17025 static struct type *
17026 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17027 {
17028 struct type *base_type, *cv_type;
17029
17030 base_type = die_type (die, cu);
17031
17032 /* The die_type call above may have already set the type for this DIE. */
17033 cv_type = get_die_type (die, cu);
17034 if (cv_type)
17035 return cv_type;
17036
17037 cv_type = make_atomic_type (base_type);
17038 return set_die_type (die, cv_type, cu);
17039 }
17040
17041 /* Extract all information from a DW_TAG_string_type DIE and add to
17042 the user defined type vector. It isn't really a user defined type,
17043 but it behaves like one, with other DIE's using an AT_user_def_type
17044 attribute to reference it. */
17045
17046 static struct type *
17047 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17048 {
17049 struct objfile *objfile = cu->per_objfile->objfile;
17050 struct gdbarch *gdbarch = objfile->arch ();
17051 struct type *type, *range_type, *index_type, *char_type;
17052 struct attribute *attr;
17053 struct dynamic_prop prop;
17054 bool length_is_constant = true;
17055 LONGEST length;
17056
17057 /* There are a couple of places where bit sizes might be made use of
17058 when parsing a DW_TAG_string_type, however, no producer that we know
17059 of make use of these. Handling bit sizes that are a multiple of the
17060 byte size is easy enough, but what about other bit sizes? Lets deal
17061 with that problem when we have to. Warn about these attributes being
17062 unsupported, then parse the type and ignore them like we always
17063 have. */
17064 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17065 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17066 {
17067 static bool warning_printed = false;
17068 if (!warning_printed)
17069 {
17070 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17071 "currently supported on DW_TAG_string_type."));
17072 warning_printed = true;
17073 }
17074 }
17075
17076 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17077 if (attr != nullptr && !attr->form_is_constant ())
17078 {
17079 /* The string length describes the location at which the length of
17080 the string can be found. The size of the length field can be
17081 specified with one of the attributes below. */
17082 struct type *prop_type;
17083 struct attribute *len
17084 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17085 if (len == nullptr)
17086 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17087 if (len != nullptr && len->form_is_constant ())
17088 {
17089 /* Pass 0 as the default as we know this attribute is constant
17090 and the default value will not be returned. */
17091 LONGEST sz = len->constant_value (0);
17092 prop_type = cu->per_objfile->int_type (sz, true);
17093 }
17094 else
17095 {
17096 /* If the size is not specified then we assume it is the size of
17097 an address on this target. */
17098 prop_type = cu->addr_sized_int_type (true);
17099 }
17100
17101 /* Convert the attribute into a dynamic property. */
17102 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17103 length = 1;
17104 else
17105 length_is_constant = false;
17106 }
17107 else if (attr != nullptr)
17108 {
17109 /* This DW_AT_string_length just contains the length with no
17110 indirection. There's no need to create a dynamic property in this
17111 case. Pass 0 for the default value as we know it will not be
17112 returned in this case. */
17113 length = attr->constant_value (0);
17114 }
17115 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17116 {
17117 /* We don't currently support non-constant byte sizes for strings. */
17118 length = attr->constant_value (1);
17119 }
17120 else
17121 {
17122 /* Use 1 as a fallback length if we have nothing else. */
17123 length = 1;
17124 }
17125
17126 index_type = objfile_type (objfile)->builtin_int;
17127 if (length_is_constant)
17128 range_type = create_static_range_type (NULL, index_type, 1, length);
17129 else
17130 {
17131 struct dynamic_prop low_bound;
17132
17133 low_bound.kind = PROP_CONST;
17134 low_bound.data.const_val = 1;
17135 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17136 }
17137 char_type = language_string_char_type (cu->language_defn, gdbarch);
17138 type = create_string_type (NULL, char_type, range_type);
17139
17140 return set_die_type (die, type, cu);
17141 }
17142
17143 /* Assuming that DIE corresponds to a function, returns nonzero
17144 if the function is prototyped. */
17145
17146 static int
17147 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17148 {
17149 struct attribute *attr;
17150
17151 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17152 if (attr && (DW_UNSND (attr) != 0))
17153 return 1;
17154
17155 /* The DWARF standard implies that the DW_AT_prototyped attribute
17156 is only meaningful for C, but the concept also extends to other
17157 languages that allow unprototyped functions (Eg: Objective C).
17158 For all other languages, assume that functions are always
17159 prototyped. */
17160 if (cu->language != language_c
17161 && cu->language != language_objc
17162 && cu->language != language_opencl)
17163 return 1;
17164
17165 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17166 prototyped and unprototyped functions; default to prototyped,
17167 since that is more common in modern code (and RealView warns
17168 about unprototyped functions). */
17169 if (producer_is_realview (cu->producer))
17170 return 1;
17171
17172 return 0;
17173 }
17174
17175 /* Handle DIES due to C code like:
17176
17177 struct foo
17178 {
17179 int (*funcp)(int a, long l);
17180 int b;
17181 };
17182
17183 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17184
17185 static struct type *
17186 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17187 {
17188 struct objfile *objfile = cu->per_objfile->objfile;
17189 struct type *type; /* Type that this function returns. */
17190 struct type *ftype; /* Function that returns above type. */
17191 struct attribute *attr;
17192
17193 type = die_type (die, cu);
17194
17195 /* The die_type call above may have already set the type for this DIE. */
17196 ftype = get_die_type (die, cu);
17197 if (ftype)
17198 return ftype;
17199
17200 ftype = lookup_function_type (type);
17201
17202 if (prototyped_function_p (die, cu))
17203 TYPE_PROTOTYPED (ftype) = 1;
17204
17205 /* Store the calling convention in the type if it's available in
17206 the subroutine die. Otherwise set the calling convention to
17207 the default value DW_CC_normal. */
17208 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17209 if (attr != nullptr
17210 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17211 TYPE_CALLING_CONVENTION (ftype)
17212 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17213 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17214 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17215 else
17216 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17217
17218 /* Record whether the function returns normally to its caller or not
17219 if the DWARF producer set that information. */
17220 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17221 if (attr && (DW_UNSND (attr) != 0))
17222 TYPE_NO_RETURN (ftype) = 1;
17223
17224 /* We need to add the subroutine type to the die immediately so
17225 we don't infinitely recurse when dealing with parameters
17226 declared as the same subroutine type. */
17227 set_die_type (die, ftype, cu);
17228
17229 if (die->child != NULL)
17230 {
17231 struct type *void_type = objfile_type (objfile)->builtin_void;
17232 struct die_info *child_die;
17233 int nparams, iparams;
17234
17235 /* Count the number of parameters.
17236 FIXME: GDB currently ignores vararg functions, but knows about
17237 vararg member functions. */
17238 nparams = 0;
17239 child_die = die->child;
17240 while (child_die && child_die->tag)
17241 {
17242 if (child_die->tag == DW_TAG_formal_parameter)
17243 nparams++;
17244 else if (child_die->tag == DW_TAG_unspecified_parameters)
17245 TYPE_VARARGS (ftype) = 1;
17246 child_die = child_die->sibling;
17247 }
17248
17249 /* Allocate storage for parameters and fill them in. */
17250 ftype->set_num_fields (nparams);
17251 ftype->set_fields
17252 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17253
17254 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17255 even if we error out during the parameters reading below. */
17256 for (iparams = 0; iparams < nparams; iparams++)
17257 ftype->field (iparams).set_type (void_type);
17258
17259 iparams = 0;
17260 child_die = die->child;
17261 while (child_die && child_die->tag)
17262 {
17263 if (child_die->tag == DW_TAG_formal_parameter)
17264 {
17265 struct type *arg_type;
17266
17267 /* DWARF version 2 has no clean way to discern C++
17268 static and non-static member functions. G++ helps
17269 GDB by marking the first parameter for non-static
17270 member functions (which is the this pointer) as
17271 artificial. We pass this information to
17272 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17273
17274 DWARF version 3 added DW_AT_object_pointer, which GCC
17275 4.5 does not yet generate. */
17276 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17277 if (attr != nullptr)
17278 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17279 else
17280 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17281 arg_type = die_type (child_die, cu);
17282
17283 /* RealView does not mark THIS as const, which the testsuite
17284 expects. GCC marks THIS as const in method definitions,
17285 but not in the class specifications (GCC PR 43053). */
17286 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17287 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17288 {
17289 int is_this = 0;
17290 struct dwarf2_cu *arg_cu = cu;
17291 const char *name = dwarf2_name (child_die, cu);
17292
17293 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17294 if (attr != nullptr)
17295 {
17296 /* If the compiler emits this, use it. */
17297 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17298 is_this = 1;
17299 }
17300 else if (name && strcmp (name, "this") == 0)
17301 /* Function definitions will have the argument names. */
17302 is_this = 1;
17303 else if (name == NULL && iparams == 0)
17304 /* Declarations may not have the names, so like
17305 elsewhere in GDB, assume an artificial first
17306 argument is "this". */
17307 is_this = 1;
17308
17309 if (is_this)
17310 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17311 arg_type, 0);
17312 }
17313
17314 ftype->field (iparams).set_type (arg_type);
17315 iparams++;
17316 }
17317 child_die = child_die->sibling;
17318 }
17319 }
17320
17321 return ftype;
17322 }
17323
17324 static struct type *
17325 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17326 {
17327 struct objfile *objfile = cu->per_objfile->objfile;
17328 const char *name = NULL;
17329 struct type *this_type, *target_type;
17330
17331 name = dwarf2_full_name (NULL, die, cu);
17332 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17333 TYPE_TARGET_STUB (this_type) = 1;
17334 set_die_type (die, this_type, cu);
17335 target_type = die_type (die, cu);
17336 if (target_type != this_type)
17337 TYPE_TARGET_TYPE (this_type) = target_type;
17338 else
17339 {
17340 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17341 spec and cause infinite loops in GDB. */
17342 complaint (_("Self-referential DW_TAG_typedef "
17343 "- DIE at %s [in module %s]"),
17344 sect_offset_str (die->sect_off), objfile_name (objfile));
17345 TYPE_TARGET_TYPE (this_type) = NULL;
17346 }
17347 if (name == NULL)
17348 {
17349 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17350 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17351 Handle these by just returning the target type, rather than
17352 constructing an anonymous typedef type and trying to handle this
17353 elsewhere. */
17354 set_die_type (die, target_type, cu);
17355 return target_type;
17356 }
17357 return this_type;
17358 }
17359
17360 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17361 (which may be different from NAME) to the architecture back-end to allow
17362 it to guess the correct format if necessary. */
17363
17364 static struct type *
17365 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17366 const char *name_hint, enum bfd_endian byte_order)
17367 {
17368 struct gdbarch *gdbarch = objfile->arch ();
17369 const struct floatformat **format;
17370 struct type *type;
17371
17372 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17373 if (format)
17374 type = init_float_type (objfile, bits, name, format, byte_order);
17375 else
17376 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17377
17378 return type;
17379 }
17380
17381 /* Allocate an integer type of size BITS and name NAME. */
17382
17383 static struct type *
17384 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17385 int bits, int unsigned_p, const char *name)
17386 {
17387 struct type *type;
17388
17389 /* Versions of Intel's C Compiler generate an integer type called "void"
17390 instead of using DW_TAG_unspecified_type. This has been seen on
17391 at least versions 14, 17, and 18. */
17392 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17393 && strcmp (name, "void") == 0)
17394 type = objfile_type (objfile)->builtin_void;
17395 else
17396 type = init_integer_type (objfile, bits, unsigned_p, name);
17397
17398 return type;
17399 }
17400
17401 /* Initialise and return a floating point type of size BITS suitable for
17402 use as a component of a complex number. The NAME_HINT is passed through
17403 when initialising the floating point type and is the name of the complex
17404 type.
17405
17406 As DWARF doesn't currently provide an explicit name for the components
17407 of a complex number, but it can be helpful to have these components
17408 named, we try to select a suitable name based on the size of the
17409 component. */
17410 static struct type *
17411 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17412 struct objfile *objfile,
17413 int bits, const char *name_hint,
17414 enum bfd_endian byte_order)
17415 {
17416 gdbarch *gdbarch = objfile->arch ();
17417 struct type *tt = nullptr;
17418
17419 /* Try to find a suitable floating point builtin type of size BITS.
17420 We're going to use the name of this type as the name for the complex
17421 target type that we are about to create. */
17422 switch (cu->language)
17423 {
17424 case language_fortran:
17425 switch (bits)
17426 {
17427 case 32:
17428 tt = builtin_f_type (gdbarch)->builtin_real;
17429 break;
17430 case 64:
17431 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17432 break;
17433 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17434 case 128:
17435 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17436 break;
17437 }
17438 break;
17439 default:
17440 switch (bits)
17441 {
17442 case 32:
17443 tt = builtin_type (gdbarch)->builtin_float;
17444 break;
17445 case 64:
17446 tt = builtin_type (gdbarch)->builtin_double;
17447 break;
17448 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17449 case 128:
17450 tt = builtin_type (gdbarch)->builtin_long_double;
17451 break;
17452 }
17453 break;
17454 }
17455
17456 /* If the type we found doesn't match the size we were looking for, then
17457 pretend we didn't find a type at all, the complex target type we
17458 create will then be nameless. */
17459 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17460 tt = nullptr;
17461
17462 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17463 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17464 }
17465
17466 /* Find a representation of a given base type and install
17467 it in the TYPE field of the die. */
17468
17469 static struct type *
17470 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17471 {
17472 struct objfile *objfile = cu->per_objfile->objfile;
17473 struct type *type;
17474 struct attribute *attr;
17475 int encoding = 0, bits = 0;
17476 const char *name;
17477 gdbarch *arch;
17478
17479 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17480 if (attr != nullptr)
17481 encoding = DW_UNSND (attr);
17482 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17483 if (attr != nullptr)
17484 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17485 name = dwarf2_name (die, cu);
17486 if (!name)
17487 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17488
17489 arch = objfile->arch ();
17490 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17491
17492 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17493 if (attr)
17494 {
17495 int endianity = DW_UNSND (attr);
17496
17497 switch (endianity)
17498 {
17499 case DW_END_big:
17500 byte_order = BFD_ENDIAN_BIG;
17501 break;
17502 case DW_END_little:
17503 byte_order = BFD_ENDIAN_LITTLE;
17504 break;
17505 default:
17506 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17507 break;
17508 }
17509 }
17510
17511 switch (encoding)
17512 {
17513 case DW_ATE_address:
17514 /* Turn DW_ATE_address into a void * pointer. */
17515 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17516 type = init_pointer_type (objfile, bits, name, type);
17517 break;
17518 case DW_ATE_boolean:
17519 type = init_boolean_type (objfile, bits, 1, name);
17520 break;
17521 case DW_ATE_complex_float:
17522 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17523 byte_order);
17524 if (type->code () == TYPE_CODE_ERROR)
17525 {
17526 if (name == nullptr)
17527 {
17528 struct obstack *obstack
17529 = &cu->per_objfile->objfile->objfile_obstack;
17530 name = obconcat (obstack, "_Complex ", type->name (),
17531 nullptr);
17532 }
17533 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17534 }
17535 else
17536 type = init_complex_type (name, type);
17537 break;
17538 case DW_ATE_decimal_float:
17539 type = init_decfloat_type (objfile, bits, name);
17540 break;
17541 case DW_ATE_float:
17542 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17543 break;
17544 case DW_ATE_signed:
17545 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17546 break;
17547 case DW_ATE_unsigned:
17548 if (cu->language == language_fortran
17549 && name
17550 && startswith (name, "character("))
17551 type = init_character_type (objfile, bits, 1, name);
17552 else
17553 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17554 break;
17555 case DW_ATE_signed_char:
17556 if (cu->language == language_ada || cu->language == language_m2
17557 || cu->language == language_pascal
17558 || cu->language == language_fortran)
17559 type = init_character_type (objfile, bits, 0, name);
17560 else
17561 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17562 break;
17563 case DW_ATE_unsigned_char:
17564 if (cu->language == language_ada || cu->language == language_m2
17565 || cu->language == language_pascal
17566 || cu->language == language_fortran
17567 || cu->language == language_rust)
17568 type = init_character_type (objfile, bits, 1, name);
17569 else
17570 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17571 break;
17572 case DW_ATE_UTF:
17573 {
17574 if (bits == 16)
17575 type = builtin_type (arch)->builtin_char16;
17576 else if (bits == 32)
17577 type = builtin_type (arch)->builtin_char32;
17578 else
17579 {
17580 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17581 bits);
17582 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17583 }
17584 return set_die_type (die, type, cu);
17585 }
17586 break;
17587
17588 default:
17589 complaint (_("unsupported DW_AT_encoding: '%s'"),
17590 dwarf_type_encoding_name (encoding));
17591 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17592 break;
17593 }
17594
17595 if (name && strcmp (name, "char") == 0)
17596 TYPE_NOSIGN (type) = 1;
17597
17598 maybe_set_alignment (cu, die, type);
17599
17600 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17601
17602 return set_die_type (die, type, cu);
17603 }
17604
17605 /* Parse dwarf attribute if it's a block, reference or constant and put the
17606 resulting value of the attribute into struct bound_prop.
17607 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17608
17609 static int
17610 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17611 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17612 struct type *default_type)
17613 {
17614 struct dwarf2_property_baton *baton;
17615 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17616 struct objfile *objfile = per_objfile->objfile;
17617 struct obstack *obstack = &objfile->objfile_obstack;
17618
17619 gdb_assert (default_type != NULL);
17620
17621 if (attr == NULL || prop == NULL)
17622 return 0;
17623
17624 if (attr->form_is_block ())
17625 {
17626 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17627 baton->property_type = default_type;
17628 baton->locexpr.per_cu = cu->per_cu;
17629 baton->locexpr.per_objfile = per_objfile;
17630 baton->locexpr.size = DW_BLOCK (attr)->size;
17631 baton->locexpr.data = DW_BLOCK (attr)->data;
17632 switch (attr->name)
17633 {
17634 case DW_AT_string_length:
17635 baton->locexpr.is_reference = true;
17636 break;
17637 default:
17638 baton->locexpr.is_reference = false;
17639 break;
17640 }
17641 prop->data.baton = baton;
17642 prop->kind = PROP_LOCEXPR;
17643 gdb_assert (prop->data.baton != NULL);
17644 }
17645 else if (attr->form_is_ref ())
17646 {
17647 struct dwarf2_cu *target_cu = cu;
17648 struct die_info *target_die;
17649 struct attribute *target_attr;
17650
17651 target_die = follow_die_ref (die, attr, &target_cu);
17652 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17653 if (target_attr == NULL)
17654 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17655 target_cu);
17656 if (target_attr == NULL)
17657 return 0;
17658
17659 switch (target_attr->name)
17660 {
17661 case DW_AT_location:
17662 if (target_attr->form_is_section_offset ())
17663 {
17664 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17665 baton->property_type = die_type (target_die, target_cu);
17666 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17667 prop->data.baton = baton;
17668 prop->kind = PROP_LOCLIST;
17669 gdb_assert (prop->data.baton != NULL);
17670 }
17671 else if (target_attr->form_is_block ())
17672 {
17673 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17674 baton->property_type = die_type (target_die, target_cu);
17675 baton->locexpr.per_cu = cu->per_cu;
17676 baton->locexpr.per_objfile = per_objfile;
17677 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17678 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17679 baton->locexpr.is_reference = true;
17680 prop->data.baton = baton;
17681 prop->kind = PROP_LOCEXPR;
17682 gdb_assert (prop->data.baton != NULL);
17683 }
17684 else
17685 {
17686 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17687 "dynamic property");
17688 return 0;
17689 }
17690 break;
17691 case DW_AT_data_member_location:
17692 {
17693 LONGEST offset;
17694
17695 if (!handle_data_member_location (target_die, target_cu,
17696 &offset))
17697 return 0;
17698
17699 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17700 baton->property_type = read_type_die (target_die->parent,
17701 target_cu);
17702 baton->offset_info.offset = offset;
17703 baton->offset_info.type = die_type (target_die, target_cu);
17704 prop->data.baton = baton;
17705 prop->kind = PROP_ADDR_OFFSET;
17706 break;
17707 }
17708 }
17709 }
17710 else if (attr->form_is_constant ())
17711 {
17712 prop->data.const_val = attr->constant_value (0);
17713 prop->kind = PROP_CONST;
17714 }
17715 else
17716 {
17717 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17718 dwarf2_name (die, cu));
17719 return 0;
17720 }
17721
17722 return 1;
17723 }
17724
17725 /* See read.h. */
17726
17727 struct type *
17728 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
17729 {
17730 struct type *int_type;
17731
17732 /* Helper macro to examine the various builtin types. */
17733 #define TRY_TYPE(F) \
17734 int_type = (unsigned_p \
17735 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17736 : objfile_type (objfile)->builtin_ ## F); \
17737 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17738 return int_type
17739
17740 TRY_TYPE (char);
17741 TRY_TYPE (short);
17742 TRY_TYPE (int);
17743 TRY_TYPE (long);
17744 TRY_TYPE (long_long);
17745
17746 #undef TRY_TYPE
17747
17748 gdb_assert_not_reached ("unable to find suitable integer type");
17749 }
17750
17751 /* See read.h. */
17752
17753 struct type *
17754 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
17755 {
17756 int addr_size = this->per_cu->addr_size ();
17757 return this->per_objfile->int_type (addr_size, unsigned_p);
17758 }
17759
17760 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17761 present (which is valid) then compute the default type based on the
17762 compilation units address size. */
17763
17764 static struct type *
17765 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17766 {
17767 struct type *index_type = die_type (die, cu);
17768
17769 /* Dwarf-2 specifications explicitly allows to create subrange types
17770 without specifying a base type.
17771 In that case, the base type must be set to the type of
17772 the lower bound, upper bound or count, in that order, if any of these
17773 three attributes references an object that has a type.
17774 If no base type is found, the Dwarf-2 specifications say that
17775 a signed integer type of size equal to the size of an address should
17776 be used.
17777 For the following C code: `extern char gdb_int [];'
17778 GCC produces an empty range DIE.
17779 FIXME: muller/2010-05-28: Possible references to object for low bound,
17780 high bound or count are not yet handled by this code. */
17781 if (index_type->code () == TYPE_CODE_VOID)
17782 index_type = cu->addr_sized_int_type (false);
17783
17784 return index_type;
17785 }
17786
17787 /* Read the given DW_AT_subrange DIE. */
17788
17789 static struct type *
17790 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17791 {
17792 struct type *base_type, *orig_base_type;
17793 struct type *range_type;
17794 struct attribute *attr;
17795 struct dynamic_prop low, high;
17796 int low_default_is_valid;
17797 int high_bound_is_count = 0;
17798 const char *name;
17799 ULONGEST negative_mask;
17800
17801 orig_base_type = read_subrange_index_type (die, cu);
17802
17803 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17804 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17805 creating the range type, but we use the result of check_typedef
17806 when examining properties of the type. */
17807 base_type = check_typedef (orig_base_type);
17808
17809 /* The die_type call above may have already set the type for this DIE. */
17810 range_type = get_die_type (die, cu);
17811 if (range_type)
17812 return range_type;
17813
17814 low.kind = PROP_CONST;
17815 high.kind = PROP_CONST;
17816 high.data.const_val = 0;
17817
17818 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17819 omitting DW_AT_lower_bound. */
17820 switch (cu->language)
17821 {
17822 case language_c:
17823 case language_cplus:
17824 low.data.const_val = 0;
17825 low_default_is_valid = 1;
17826 break;
17827 case language_fortran:
17828 low.data.const_val = 1;
17829 low_default_is_valid = 1;
17830 break;
17831 case language_d:
17832 case language_objc:
17833 case language_rust:
17834 low.data.const_val = 0;
17835 low_default_is_valid = (cu->header.version >= 4);
17836 break;
17837 case language_ada:
17838 case language_m2:
17839 case language_pascal:
17840 low.data.const_val = 1;
17841 low_default_is_valid = (cu->header.version >= 4);
17842 break;
17843 default:
17844 low.data.const_val = 0;
17845 low_default_is_valid = 0;
17846 break;
17847 }
17848
17849 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17850 if (attr != nullptr)
17851 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17852 else if (!low_default_is_valid)
17853 complaint (_("Missing DW_AT_lower_bound "
17854 "- DIE at %s [in module %s]"),
17855 sect_offset_str (die->sect_off),
17856 objfile_name (cu->per_objfile->objfile));
17857
17858 struct attribute *attr_ub, *attr_count;
17859 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17860 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17861 {
17862 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17863 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17864 {
17865 /* If bounds are constant do the final calculation here. */
17866 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17867 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17868 else
17869 high_bound_is_count = 1;
17870 }
17871 else
17872 {
17873 if (attr_ub != NULL)
17874 complaint (_("Unresolved DW_AT_upper_bound "
17875 "- DIE at %s [in module %s]"),
17876 sect_offset_str (die->sect_off),
17877 objfile_name (cu->per_objfile->objfile));
17878 if (attr_count != NULL)
17879 complaint (_("Unresolved DW_AT_count "
17880 "- DIE at %s [in module %s]"),
17881 sect_offset_str (die->sect_off),
17882 objfile_name (cu->per_objfile->objfile));
17883 }
17884 }
17885
17886 LONGEST bias = 0;
17887 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17888 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17889 bias = bias_attr->constant_value (0);
17890
17891 /* Normally, the DWARF producers are expected to use a signed
17892 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17893 But this is unfortunately not always the case, as witnessed
17894 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17895 is used instead. To work around that ambiguity, we treat
17896 the bounds as signed, and thus sign-extend their values, when
17897 the base type is signed. */
17898 negative_mask =
17899 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17900 if (low.kind == PROP_CONST
17901 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17902 low.data.const_val |= negative_mask;
17903 if (high.kind == PROP_CONST
17904 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17905 high.data.const_val |= negative_mask;
17906
17907 /* Check for bit and byte strides. */
17908 struct dynamic_prop byte_stride_prop;
17909 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17910 if (attr_byte_stride != nullptr)
17911 {
17912 struct type *prop_type = cu->addr_sized_int_type (false);
17913 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17914 prop_type);
17915 }
17916
17917 struct dynamic_prop bit_stride_prop;
17918 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17919 if (attr_bit_stride != nullptr)
17920 {
17921 /* It only makes sense to have either a bit or byte stride. */
17922 if (attr_byte_stride != nullptr)
17923 {
17924 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17925 "- DIE at %s [in module %s]"),
17926 sect_offset_str (die->sect_off),
17927 objfile_name (cu->per_objfile->objfile));
17928 attr_bit_stride = nullptr;
17929 }
17930 else
17931 {
17932 struct type *prop_type = cu->addr_sized_int_type (false);
17933 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17934 prop_type);
17935 }
17936 }
17937
17938 if (attr_byte_stride != nullptr
17939 || attr_bit_stride != nullptr)
17940 {
17941 bool byte_stride_p = (attr_byte_stride != nullptr);
17942 struct dynamic_prop *stride
17943 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17944
17945 range_type
17946 = create_range_type_with_stride (NULL, orig_base_type, &low,
17947 &high, bias, stride, byte_stride_p);
17948 }
17949 else
17950 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17951
17952 if (high_bound_is_count)
17953 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17954
17955 /* Ada expects an empty array on no boundary attributes. */
17956 if (attr == NULL && cu->language != language_ada)
17957 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17958
17959 name = dwarf2_name (die, cu);
17960 if (name)
17961 range_type->set_name (name);
17962
17963 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17964 if (attr != nullptr)
17965 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17966
17967 maybe_set_alignment (cu, die, range_type);
17968
17969 set_die_type (die, range_type, cu);
17970
17971 /* set_die_type should be already done. */
17972 set_descriptive_type (range_type, die, cu);
17973
17974 return range_type;
17975 }
17976
17977 static struct type *
17978 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17979 {
17980 struct type *type;
17981
17982 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17983 type->set_name (dwarf2_name (die, cu));
17984
17985 /* In Ada, an unspecified type is typically used when the description
17986 of the type is deferred to a different unit. When encountering
17987 such a type, we treat it as a stub, and try to resolve it later on,
17988 when needed. */
17989 if (cu->language == language_ada)
17990 TYPE_STUB (type) = 1;
17991
17992 return set_die_type (die, type, cu);
17993 }
17994
17995 /* Read a single die and all its descendents. Set the die's sibling
17996 field to NULL; set other fields in the die correctly, and set all
17997 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17998 location of the info_ptr after reading all of those dies. PARENT
17999 is the parent of the die in question. */
18000
18001 static struct die_info *
18002 read_die_and_children (const struct die_reader_specs *reader,
18003 const gdb_byte *info_ptr,
18004 const gdb_byte **new_info_ptr,
18005 struct die_info *parent)
18006 {
18007 struct die_info *die;
18008 const gdb_byte *cur_ptr;
18009
18010 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18011 if (die == NULL)
18012 {
18013 *new_info_ptr = cur_ptr;
18014 return NULL;
18015 }
18016 store_in_ref_table (die, reader->cu);
18017
18018 if (die->has_children)
18019 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18020 else
18021 {
18022 die->child = NULL;
18023 *new_info_ptr = cur_ptr;
18024 }
18025
18026 die->sibling = NULL;
18027 die->parent = parent;
18028 return die;
18029 }
18030
18031 /* Read a die, all of its descendents, and all of its siblings; set
18032 all of the fields of all of the dies correctly. Arguments are as
18033 in read_die_and_children. */
18034
18035 static struct die_info *
18036 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18037 const gdb_byte *info_ptr,
18038 const gdb_byte **new_info_ptr,
18039 struct die_info *parent)
18040 {
18041 struct die_info *first_die, *last_sibling;
18042 const gdb_byte *cur_ptr;
18043
18044 cur_ptr = info_ptr;
18045 first_die = last_sibling = NULL;
18046
18047 while (1)
18048 {
18049 struct die_info *die
18050 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18051
18052 if (die == NULL)
18053 {
18054 *new_info_ptr = cur_ptr;
18055 return first_die;
18056 }
18057
18058 if (!first_die)
18059 first_die = die;
18060 else
18061 last_sibling->sibling = die;
18062
18063 last_sibling = die;
18064 }
18065 }
18066
18067 /* Read a die, all of its descendents, and all of its siblings; set
18068 all of the fields of all of the dies correctly. Arguments are as
18069 in read_die_and_children.
18070 This the main entry point for reading a DIE and all its children. */
18071
18072 static struct die_info *
18073 read_die_and_siblings (const struct die_reader_specs *reader,
18074 const gdb_byte *info_ptr,
18075 const gdb_byte **new_info_ptr,
18076 struct die_info *parent)
18077 {
18078 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18079 new_info_ptr, parent);
18080
18081 if (dwarf_die_debug)
18082 {
18083 fprintf_unfiltered (gdb_stdlog,
18084 "Read die from %s@0x%x of %s:\n",
18085 reader->die_section->get_name (),
18086 (unsigned) (info_ptr - reader->die_section->buffer),
18087 bfd_get_filename (reader->abfd));
18088 dump_die (die, dwarf_die_debug);
18089 }
18090
18091 return die;
18092 }
18093
18094 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18095 attributes.
18096 The caller is responsible for filling in the extra attributes
18097 and updating (*DIEP)->num_attrs.
18098 Set DIEP to point to a newly allocated die with its information,
18099 except for its child, sibling, and parent fields. */
18100
18101 static const gdb_byte *
18102 read_full_die_1 (const struct die_reader_specs *reader,
18103 struct die_info **diep, const gdb_byte *info_ptr,
18104 int num_extra_attrs)
18105 {
18106 unsigned int abbrev_number, bytes_read, i;
18107 struct abbrev_info *abbrev;
18108 struct die_info *die;
18109 struct dwarf2_cu *cu = reader->cu;
18110 bfd *abfd = reader->abfd;
18111
18112 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18113 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18114 info_ptr += bytes_read;
18115 if (!abbrev_number)
18116 {
18117 *diep = NULL;
18118 return info_ptr;
18119 }
18120
18121 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18122 if (!abbrev)
18123 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18124 abbrev_number,
18125 bfd_get_filename (abfd));
18126
18127 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18128 die->sect_off = sect_off;
18129 die->tag = abbrev->tag;
18130 die->abbrev = abbrev_number;
18131 die->has_children = abbrev->has_children;
18132
18133 /* Make the result usable.
18134 The caller needs to update num_attrs after adding the extra
18135 attributes. */
18136 die->num_attrs = abbrev->num_attrs;
18137
18138 std::vector<int> indexes_that_need_reprocess;
18139 for (i = 0; i < abbrev->num_attrs; ++i)
18140 {
18141 bool need_reprocess;
18142 info_ptr =
18143 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18144 info_ptr, &need_reprocess);
18145 if (need_reprocess)
18146 indexes_that_need_reprocess.push_back (i);
18147 }
18148
18149 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18150 if (attr != nullptr)
18151 cu->str_offsets_base = DW_UNSND (attr);
18152
18153 attr = die->attr (DW_AT_loclists_base);
18154 if (attr != nullptr)
18155 cu->loclist_base = DW_UNSND (attr);
18156
18157 auto maybe_addr_base = die->addr_base ();
18158 if (maybe_addr_base.has_value ())
18159 cu->addr_base = *maybe_addr_base;
18160 for (int index : indexes_that_need_reprocess)
18161 read_attribute_reprocess (reader, &die->attrs[index]);
18162 *diep = die;
18163 return info_ptr;
18164 }
18165
18166 /* Read a die and all its attributes.
18167 Set DIEP to point to a newly allocated die with its information,
18168 except for its child, sibling, and parent fields. */
18169
18170 static const gdb_byte *
18171 read_full_die (const struct die_reader_specs *reader,
18172 struct die_info **diep, const gdb_byte *info_ptr)
18173 {
18174 const gdb_byte *result;
18175
18176 result = read_full_die_1 (reader, diep, info_ptr, 0);
18177
18178 if (dwarf_die_debug)
18179 {
18180 fprintf_unfiltered (gdb_stdlog,
18181 "Read die from %s@0x%x of %s:\n",
18182 reader->die_section->get_name (),
18183 (unsigned) (info_ptr - reader->die_section->buffer),
18184 bfd_get_filename (reader->abfd));
18185 dump_die (*diep, dwarf_die_debug);
18186 }
18187
18188 return result;
18189 }
18190 \f
18191
18192 /* Returns nonzero if TAG represents a type that we might generate a partial
18193 symbol for. */
18194
18195 static int
18196 is_type_tag_for_partial (int tag)
18197 {
18198 switch (tag)
18199 {
18200 #if 0
18201 /* Some types that would be reasonable to generate partial symbols for,
18202 that we don't at present. */
18203 case DW_TAG_array_type:
18204 case DW_TAG_file_type:
18205 case DW_TAG_ptr_to_member_type:
18206 case DW_TAG_set_type:
18207 case DW_TAG_string_type:
18208 case DW_TAG_subroutine_type:
18209 #endif
18210 case DW_TAG_base_type:
18211 case DW_TAG_class_type:
18212 case DW_TAG_interface_type:
18213 case DW_TAG_enumeration_type:
18214 case DW_TAG_structure_type:
18215 case DW_TAG_subrange_type:
18216 case DW_TAG_typedef:
18217 case DW_TAG_union_type:
18218 return 1;
18219 default:
18220 return 0;
18221 }
18222 }
18223
18224 /* Load all DIEs that are interesting for partial symbols into memory. */
18225
18226 static struct partial_die_info *
18227 load_partial_dies (const struct die_reader_specs *reader,
18228 const gdb_byte *info_ptr, int building_psymtab)
18229 {
18230 struct dwarf2_cu *cu = reader->cu;
18231 struct objfile *objfile = cu->per_objfile->objfile;
18232 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18233 unsigned int bytes_read;
18234 unsigned int load_all = 0;
18235 int nesting_level = 1;
18236
18237 parent_die = NULL;
18238 last_die = NULL;
18239
18240 gdb_assert (cu->per_cu != NULL);
18241 if (cu->per_cu->load_all_dies)
18242 load_all = 1;
18243
18244 cu->partial_dies
18245 = htab_create_alloc_ex (cu->header.length / 12,
18246 partial_die_hash,
18247 partial_die_eq,
18248 NULL,
18249 &cu->comp_unit_obstack,
18250 hashtab_obstack_allocate,
18251 dummy_obstack_deallocate);
18252
18253 while (1)
18254 {
18255 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18256
18257 /* A NULL abbrev means the end of a series of children. */
18258 if (abbrev == NULL)
18259 {
18260 if (--nesting_level == 0)
18261 return first_die;
18262
18263 info_ptr += bytes_read;
18264 last_die = parent_die;
18265 parent_die = parent_die->die_parent;
18266 continue;
18267 }
18268
18269 /* Check for template arguments. We never save these; if
18270 they're seen, we just mark the parent, and go on our way. */
18271 if (parent_die != NULL
18272 && cu->language == language_cplus
18273 && (abbrev->tag == DW_TAG_template_type_param
18274 || abbrev->tag == DW_TAG_template_value_param))
18275 {
18276 parent_die->has_template_arguments = 1;
18277
18278 if (!load_all)
18279 {
18280 /* We don't need a partial DIE for the template argument. */
18281 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18282 continue;
18283 }
18284 }
18285
18286 /* We only recurse into c++ subprograms looking for template arguments.
18287 Skip their other children. */
18288 if (!load_all
18289 && cu->language == language_cplus
18290 && parent_die != NULL
18291 && parent_die->tag == DW_TAG_subprogram
18292 && abbrev->tag != DW_TAG_inlined_subroutine)
18293 {
18294 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18295 continue;
18296 }
18297
18298 /* Check whether this DIE is interesting enough to save. Normally
18299 we would not be interested in members here, but there may be
18300 later variables referencing them via DW_AT_specification (for
18301 static members). */
18302 if (!load_all
18303 && !is_type_tag_for_partial (abbrev->tag)
18304 && abbrev->tag != DW_TAG_constant
18305 && abbrev->tag != DW_TAG_enumerator
18306 && abbrev->tag != DW_TAG_subprogram
18307 && abbrev->tag != DW_TAG_inlined_subroutine
18308 && abbrev->tag != DW_TAG_lexical_block
18309 && abbrev->tag != DW_TAG_variable
18310 && abbrev->tag != DW_TAG_namespace
18311 && abbrev->tag != DW_TAG_module
18312 && abbrev->tag != DW_TAG_member
18313 && abbrev->tag != DW_TAG_imported_unit
18314 && abbrev->tag != DW_TAG_imported_declaration)
18315 {
18316 /* Otherwise we skip to the next sibling, if any. */
18317 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18318 continue;
18319 }
18320
18321 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18322 abbrev);
18323
18324 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18325
18326 /* This two-pass algorithm for processing partial symbols has a
18327 high cost in cache pressure. Thus, handle some simple cases
18328 here which cover the majority of C partial symbols. DIEs
18329 which neither have specification tags in them, nor could have
18330 specification tags elsewhere pointing at them, can simply be
18331 processed and discarded.
18332
18333 This segment is also optional; scan_partial_symbols and
18334 add_partial_symbol will handle these DIEs if we chain
18335 them in normally. When compilers which do not emit large
18336 quantities of duplicate debug information are more common,
18337 this code can probably be removed. */
18338
18339 /* Any complete simple types at the top level (pretty much all
18340 of them, for a language without namespaces), can be processed
18341 directly. */
18342 if (parent_die == NULL
18343 && pdi.has_specification == 0
18344 && pdi.is_declaration == 0
18345 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18346 || pdi.tag == DW_TAG_base_type
18347 || pdi.tag == DW_TAG_subrange_type))
18348 {
18349 if (building_psymtab && pdi.raw_name != NULL)
18350 add_partial_symbol (&pdi, cu);
18351
18352 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18353 continue;
18354 }
18355
18356 /* The exception for DW_TAG_typedef with has_children above is
18357 a workaround of GCC PR debug/47510. In the case of this complaint
18358 type_name_or_error will error on such types later.
18359
18360 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18361 it could not find the child DIEs referenced later, this is checked
18362 above. In correct DWARF DW_TAG_typedef should have no children. */
18363
18364 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18365 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18366 "- DIE at %s [in module %s]"),
18367 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18368
18369 /* If we're at the second level, and we're an enumerator, and
18370 our parent has no specification (meaning possibly lives in a
18371 namespace elsewhere), then we can add the partial symbol now
18372 instead of queueing it. */
18373 if (pdi.tag == DW_TAG_enumerator
18374 && parent_die != NULL
18375 && parent_die->die_parent == NULL
18376 && parent_die->tag == DW_TAG_enumeration_type
18377 && parent_die->has_specification == 0)
18378 {
18379 if (pdi.raw_name == NULL)
18380 complaint (_("malformed enumerator DIE ignored"));
18381 else if (building_psymtab)
18382 add_partial_symbol (&pdi, cu);
18383
18384 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18385 continue;
18386 }
18387
18388 struct partial_die_info *part_die
18389 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18390
18391 /* We'll save this DIE so link it in. */
18392 part_die->die_parent = parent_die;
18393 part_die->die_sibling = NULL;
18394 part_die->die_child = NULL;
18395
18396 if (last_die && last_die == parent_die)
18397 last_die->die_child = part_die;
18398 else if (last_die)
18399 last_die->die_sibling = part_die;
18400
18401 last_die = part_die;
18402
18403 if (first_die == NULL)
18404 first_die = part_die;
18405
18406 /* Maybe add the DIE to the hash table. Not all DIEs that we
18407 find interesting need to be in the hash table, because we
18408 also have the parent/sibling/child chains; only those that we
18409 might refer to by offset later during partial symbol reading.
18410
18411 For now this means things that might have be the target of a
18412 DW_AT_specification, DW_AT_abstract_origin, or
18413 DW_AT_extension. DW_AT_extension will refer only to
18414 namespaces; DW_AT_abstract_origin refers to functions (and
18415 many things under the function DIE, but we do not recurse
18416 into function DIEs during partial symbol reading) and
18417 possibly variables as well; DW_AT_specification refers to
18418 declarations. Declarations ought to have the DW_AT_declaration
18419 flag. It happens that GCC forgets to put it in sometimes, but
18420 only for functions, not for types.
18421
18422 Adding more things than necessary to the hash table is harmless
18423 except for the performance cost. Adding too few will result in
18424 wasted time in find_partial_die, when we reread the compilation
18425 unit with load_all_dies set. */
18426
18427 if (load_all
18428 || abbrev->tag == DW_TAG_constant
18429 || abbrev->tag == DW_TAG_subprogram
18430 || abbrev->tag == DW_TAG_variable
18431 || abbrev->tag == DW_TAG_namespace
18432 || part_die->is_declaration)
18433 {
18434 void **slot;
18435
18436 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18437 to_underlying (part_die->sect_off),
18438 INSERT);
18439 *slot = part_die;
18440 }
18441
18442 /* For some DIEs we want to follow their children (if any). For C
18443 we have no reason to follow the children of structures; for other
18444 languages we have to, so that we can get at method physnames
18445 to infer fully qualified class names, for DW_AT_specification,
18446 and for C++ template arguments. For C++, we also look one level
18447 inside functions to find template arguments (if the name of the
18448 function does not already contain the template arguments).
18449
18450 For Ada and Fortran, we need to scan the children of subprograms
18451 and lexical blocks as well because these languages allow the
18452 definition of nested entities that could be interesting for the
18453 debugger, such as nested subprograms for instance. */
18454 if (last_die->has_children
18455 && (load_all
18456 || last_die->tag == DW_TAG_namespace
18457 || last_die->tag == DW_TAG_module
18458 || last_die->tag == DW_TAG_enumeration_type
18459 || (cu->language == language_cplus
18460 && last_die->tag == DW_TAG_subprogram
18461 && (last_die->raw_name == NULL
18462 || strchr (last_die->raw_name, '<') == NULL))
18463 || (cu->language != language_c
18464 && (last_die->tag == DW_TAG_class_type
18465 || last_die->tag == DW_TAG_interface_type
18466 || last_die->tag == DW_TAG_structure_type
18467 || last_die->tag == DW_TAG_union_type))
18468 || ((cu->language == language_ada
18469 || cu->language == language_fortran)
18470 && (last_die->tag == DW_TAG_subprogram
18471 || last_die->tag == DW_TAG_lexical_block))))
18472 {
18473 nesting_level++;
18474 parent_die = last_die;
18475 continue;
18476 }
18477
18478 /* Otherwise we skip to the next sibling, if any. */
18479 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18480
18481 /* Back to the top, do it again. */
18482 }
18483 }
18484
18485 partial_die_info::partial_die_info (sect_offset sect_off_,
18486 struct abbrev_info *abbrev)
18487 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18488 {
18489 }
18490
18491 /* See class definition. */
18492
18493 const char *
18494 partial_die_info::name (dwarf2_cu *cu)
18495 {
18496 if (!canonical_name && raw_name != nullptr)
18497 {
18498 struct objfile *objfile = cu->per_objfile->objfile;
18499 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
18500 canonical_name = 1;
18501 }
18502
18503 return raw_name;
18504 }
18505
18506 /* Read a minimal amount of information into the minimal die structure.
18507 INFO_PTR should point just after the initial uleb128 of a DIE. */
18508
18509 const gdb_byte *
18510 partial_die_info::read (const struct die_reader_specs *reader,
18511 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18512 {
18513 struct dwarf2_cu *cu = reader->cu;
18514 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18515 unsigned int i;
18516 int has_low_pc_attr = 0;
18517 int has_high_pc_attr = 0;
18518 int high_pc_relative = 0;
18519
18520 for (i = 0; i < abbrev.num_attrs; ++i)
18521 {
18522 attribute attr;
18523 bool need_reprocess;
18524 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18525 info_ptr, &need_reprocess);
18526 /* String and address offsets that need to do the reprocessing have
18527 already been read at this point, so there is no need to wait until
18528 the loop terminates to do the reprocessing. */
18529 if (need_reprocess)
18530 read_attribute_reprocess (reader, &attr);
18531 /* Store the data if it is of an attribute we want to keep in a
18532 partial symbol table. */
18533 switch (attr.name)
18534 {
18535 case DW_AT_name:
18536 switch (tag)
18537 {
18538 case DW_TAG_compile_unit:
18539 case DW_TAG_partial_unit:
18540 case DW_TAG_type_unit:
18541 /* Compilation units have a DW_AT_name that is a filename, not
18542 a source language identifier. */
18543 case DW_TAG_enumeration_type:
18544 case DW_TAG_enumerator:
18545 /* These tags always have simple identifiers already; no need
18546 to canonicalize them. */
18547 canonical_name = 1;
18548 raw_name = DW_STRING (&attr);
18549 break;
18550 default:
18551 canonical_name = 0;
18552 raw_name = DW_STRING (&attr);
18553 break;
18554 }
18555 break;
18556 case DW_AT_linkage_name:
18557 case DW_AT_MIPS_linkage_name:
18558 /* Note that both forms of linkage name might appear. We
18559 assume they will be the same, and we only store the last
18560 one we see. */
18561 linkage_name = attr.value_as_string ();
18562 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18563 See https://github.com/rust-lang/rust/issues/32925. */
18564 if (cu->language == language_rust && linkage_name != NULL
18565 && strchr (linkage_name, '{') != NULL)
18566 linkage_name = NULL;
18567 break;
18568 case DW_AT_low_pc:
18569 has_low_pc_attr = 1;
18570 lowpc = attr.value_as_address ();
18571 break;
18572 case DW_AT_high_pc:
18573 has_high_pc_attr = 1;
18574 highpc = attr.value_as_address ();
18575 if (cu->header.version >= 4 && attr.form_is_constant ())
18576 high_pc_relative = 1;
18577 break;
18578 case DW_AT_location:
18579 /* Support the .debug_loc offsets. */
18580 if (attr.form_is_block ())
18581 {
18582 d.locdesc = DW_BLOCK (&attr);
18583 }
18584 else if (attr.form_is_section_offset ())
18585 {
18586 dwarf2_complex_location_expr_complaint ();
18587 }
18588 else
18589 {
18590 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18591 "partial symbol information");
18592 }
18593 break;
18594 case DW_AT_external:
18595 is_external = DW_UNSND (&attr);
18596 break;
18597 case DW_AT_declaration:
18598 is_declaration = DW_UNSND (&attr);
18599 break;
18600 case DW_AT_type:
18601 has_type = 1;
18602 break;
18603 case DW_AT_abstract_origin:
18604 case DW_AT_specification:
18605 case DW_AT_extension:
18606 has_specification = 1;
18607 spec_offset = attr.get_ref_die_offset ();
18608 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18609 || cu->per_cu->is_dwz);
18610 break;
18611 case DW_AT_sibling:
18612 /* Ignore absolute siblings, they might point outside of
18613 the current compile unit. */
18614 if (attr.form == DW_FORM_ref_addr)
18615 complaint (_("ignoring absolute DW_AT_sibling"));
18616 else
18617 {
18618 const gdb_byte *buffer = reader->buffer;
18619 sect_offset off = attr.get_ref_die_offset ();
18620 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18621
18622 if (sibling_ptr < info_ptr)
18623 complaint (_("DW_AT_sibling points backwards"));
18624 else if (sibling_ptr > reader->buffer_end)
18625 reader->die_section->overflow_complaint ();
18626 else
18627 sibling = sibling_ptr;
18628 }
18629 break;
18630 case DW_AT_byte_size:
18631 has_byte_size = 1;
18632 break;
18633 case DW_AT_const_value:
18634 has_const_value = 1;
18635 break;
18636 case DW_AT_calling_convention:
18637 /* DWARF doesn't provide a way to identify a program's source-level
18638 entry point. DW_AT_calling_convention attributes are only meant
18639 to describe functions' calling conventions.
18640
18641 However, because it's a necessary piece of information in
18642 Fortran, and before DWARF 4 DW_CC_program was the only
18643 piece of debugging information whose definition refers to
18644 a 'main program' at all, several compilers marked Fortran
18645 main programs with DW_CC_program --- even when those
18646 functions use the standard calling conventions.
18647
18648 Although DWARF now specifies a way to provide this
18649 information, we support this practice for backward
18650 compatibility. */
18651 if (DW_UNSND (&attr) == DW_CC_program
18652 && cu->language == language_fortran)
18653 main_subprogram = 1;
18654 break;
18655 case DW_AT_inline:
18656 if (DW_UNSND (&attr) == DW_INL_inlined
18657 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18658 may_be_inlined = 1;
18659 break;
18660
18661 case DW_AT_import:
18662 if (tag == DW_TAG_imported_unit)
18663 {
18664 d.sect_off = attr.get_ref_die_offset ();
18665 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18666 || cu->per_cu->is_dwz);
18667 }
18668 break;
18669
18670 case DW_AT_main_subprogram:
18671 main_subprogram = DW_UNSND (&attr);
18672 break;
18673
18674 case DW_AT_ranges:
18675 {
18676 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18677 but that requires a full DIE, so instead we just
18678 reimplement it. */
18679 int need_ranges_base = tag != DW_TAG_compile_unit;
18680 unsigned int ranges_offset = (DW_UNSND (&attr)
18681 + (need_ranges_base
18682 ? cu->ranges_base
18683 : 0));
18684
18685 /* Value of the DW_AT_ranges attribute is the offset in the
18686 .debug_ranges section. */
18687 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18688 nullptr))
18689 has_pc_info = 1;
18690 }
18691 break;
18692
18693 default:
18694 break;
18695 }
18696 }
18697
18698 /* For Ada, if both the name and the linkage name appear, we prefer
18699 the latter. This lets "catch exception" work better, regardless
18700 of the order in which the name and linkage name were emitted.
18701 Really, though, this is just a workaround for the fact that gdb
18702 doesn't store both the name and the linkage name. */
18703 if (cu->language == language_ada && linkage_name != nullptr)
18704 raw_name = linkage_name;
18705
18706 if (high_pc_relative)
18707 highpc += lowpc;
18708
18709 if (has_low_pc_attr && has_high_pc_attr)
18710 {
18711 /* When using the GNU linker, .gnu.linkonce. sections are used to
18712 eliminate duplicate copies of functions and vtables and such.
18713 The linker will arbitrarily choose one and discard the others.
18714 The AT_*_pc values for such functions refer to local labels in
18715 these sections. If the section from that file was discarded, the
18716 labels are not in the output, so the relocs get a value of 0.
18717 If this is a discarded function, mark the pc bounds as invalid,
18718 so that GDB will ignore it. */
18719 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
18720 {
18721 struct objfile *objfile = per_objfile->objfile;
18722 struct gdbarch *gdbarch = objfile->arch ();
18723
18724 complaint (_("DW_AT_low_pc %s is zero "
18725 "for DIE at %s [in module %s]"),
18726 paddress (gdbarch, lowpc),
18727 sect_offset_str (sect_off),
18728 objfile_name (objfile));
18729 }
18730 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18731 else if (lowpc >= highpc)
18732 {
18733 struct objfile *objfile = per_objfile->objfile;
18734 struct gdbarch *gdbarch = objfile->arch ();
18735
18736 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18737 "for DIE at %s [in module %s]"),
18738 paddress (gdbarch, lowpc),
18739 paddress (gdbarch, highpc),
18740 sect_offset_str (sect_off),
18741 objfile_name (objfile));
18742 }
18743 else
18744 has_pc_info = 1;
18745 }
18746
18747 return info_ptr;
18748 }
18749
18750 /* Find a cached partial DIE at OFFSET in CU. */
18751
18752 struct partial_die_info *
18753 dwarf2_cu::find_partial_die (sect_offset sect_off)
18754 {
18755 struct partial_die_info *lookup_die = NULL;
18756 struct partial_die_info part_die (sect_off);
18757
18758 lookup_die = ((struct partial_die_info *)
18759 htab_find_with_hash (partial_dies, &part_die,
18760 to_underlying (sect_off)));
18761
18762 return lookup_die;
18763 }
18764
18765 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18766 except in the case of .debug_types DIEs which do not reference
18767 outside their CU (they do however referencing other types via
18768 DW_FORM_ref_sig8). */
18769
18770 static const struct cu_partial_die_info
18771 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18772 {
18773 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18774 struct objfile *objfile = per_objfile->objfile;
18775 struct partial_die_info *pd = NULL;
18776
18777 if (offset_in_dwz == cu->per_cu->is_dwz
18778 && cu->header.offset_in_cu_p (sect_off))
18779 {
18780 pd = cu->find_partial_die (sect_off);
18781 if (pd != NULL)
18782 return { cu, pd };
18783 /* We missed recording what we needed.
18784 Load all dies and try again. */
18785 }
18786 else
18787 {
18788 /* TUs don't reference other CUs/TUs (except via type signatures). */
18789 if (cu->per_cu->is_debug_types)
18790 {
18791 error (_("Dwarf Error: Type Unit at offset %s contains"
18792 " external reference to offset %s [in module %s].\n"),
18793 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18794 bfd_get_filename (objfile->obfd));
18795 }
18796 dwarf2_per_cu_data *per_cu
18797 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18798 per_objfile);
18799
18800 cu = per_objfile->get_cu (per_cu);
18801 if (cu == NULL || cu->partial_dies == NULL)
18802 load_partial_comp_unit (per_cu, per_objfile, nullptr);
18803
18804 cu = per_objfile->get_cu (per_cu);
18805
18806 cu->last_used = 0;
18807 pd = cu->find_partial_die (sect_off);
18808 }
18809
18810 /* If we didn't find it, and not all dies have been loaded,
18811 load them all and try again. */
18812
18813 if (pd == NULL && cu->per_cu->load_all_dies == 0)
18814 {
18815 cu->per_cu->load_all_dies = 1;
18816
18817 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18818 THIS_CU->cu may already be in use. So we can't just free it and
18819 replace its DIEs with the ones we read in. Instead, we leave those
18820 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18821 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18822 set. */
18823 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
18824
18825 pd = cu->find_partial_die (sect_off);
18826 }
18827
18828 if (pd == NULL)
18829 internal_error (__FILE__, __LINE__,
18830 _("could not find partial DIE %s "
18831 "in cache [from module %s]\n"),
18832 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18833 return { cu, pd };
18834 }
18835
18836 /* See if we can figure out if the class lives in a namespace. We do
18837 this by looking for a member function; its demangled name will
18838 contain namespace info, if there is any. */
18839
18840 static void
18841 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18842 struct dwarf2_cu *cu)
18843 {
18844 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18845 what template types look like, because the demangler
18846 frequently doesn't give the same name as the debug info. We
18847 could fix this by only using the demangled name to get the
18848 prefix (but see comment in read_structure_type). */
18849
18850 struct partial_die_info *real_pdi;
18851 struct partial_die_info *child_pdi;
18852
18853 /* If this DIE (this DIE's specification, if any) has a parent, then
18854 we should not do this. We'll prepend the parent's fully qualified
18855 name when we create the partial symbol. */
18856
18857 real_pdi = struct_pdi;
18858 while (real_pdi->has_specification)
18859 {
18860 auto res = find_partial_die (real_pdi->spec_offset,
18861 real_pdi->spec_is_dwz, cu);
18862 real_pdi = res.pdi;
18863 cu = res.cu;
18864 }
18865
18866 if (real_pdi->die_parent != NULL)
18867 return;
18868
18869 for (child_pdi = struct_pdi->die_child;
18870 child_pdi != NULL;
18871 child_pdi = child_pdi->die_sibling)
18872 {
18873 if (child_pdi->tag == DW_TAG_subprogram
18874 && child_pdi->linkage_name != NULL)
18875 {
18876 gdb::unique_xmalloc_ptr<char> actual_class_name
18877 (cu->language_defn->class_name_from_physname
18878 (child_pdi->linkage_name));
18879 if (actual_class_name != NULL)
18880 {
18881 struct objfile *objfile = cu->per_objfile->objfile;
18882 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
18883 struct_pdi->canonical_name = 1;
18884 }
18885 break;
18886 }
18887 }
18888 }
18889
18890 /* Return true if a DIE with TAG may have the DW_AT_const_value
18891 attribute. */
18892
18893 static bool
18894 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18895 {
18896 switch (tag)
18897 {
18898 case DW_TAG_constant:
18899 case DW_TAG_enumerator:
18900 case DW_TAG_formal_parameter:
18901 case DW_TAG_template_value_param:
18902 case DW_TAG_variable:
18903 return true;
18904 }
18905
18906 return false;
18907 }
18908
18909 void
18910 partial_die_info::fixup (struct dwarf2_cu *cu)
18911 {
18912 /* Once we've fixed up a die, there's no point in doing so again.
18913 This also avoids a memory leak if we were to call
18914 guess_partial_die_structure_name multiple times. */
18915 if (fixup_called)
18916 return;
18917
18918 /* If we found a reference attribute and the DIE has no name, try
18919 to find a name in the referred to DIE. */
18920
18921 if (raw_name == NULL && has_specification)
18922 {
18923 struct partial_die_info *spec_die;
18924
18925 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18926 spec_die = res.pdi;
18927 cu = res.cu;
18928
18929 spec_die->fixup (cu);
18930
18931 if (spec_die->raw_name)
18932 {
18933 raw_name = spec_die->raw_name;
18934 canonical_name = spec_die->canonical_name;
18935
18936 /* Copy DW_AT_external attribute if it is set. */
18937 if (spec_die->is_external)
18938 is_external = spec_die->is_external;
18939 }
18940 }
18941
18942 if (!has_const_value && has_specification
18943 && can_have_DW_AT_const_value_p (tag))
18944 {
18945 struct partial_die_info *spec_die;
18946
18947 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18948 spec_die = res.pdi;
18949 cu = res.cu;
18950
18951 spec_die->fixup (cu);
18952
18953 if (spec_die->has_const_value)
18954 {
18955 /* Copy DW_AT_const_value attribute if it is set. */
18956 has_const_value = spec_die->has_const_value;
18957 }
18958 }
18959
18960 /* Set default names for some unnamed DIEs. */
18961
18962 if (raw_name == NULL && tag == DW_TAG_namespace)
18963 {
18964 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
18965 canonical_name = 1;
18966 }
18967
18968 /* If there is no parent die to provide a namespace, and there are
18969 children, see if we can determine the namespace from their linkage
18970 name. */
18971 if (cu->language == language_cplus
18972 && !cu->per_objfile->per_bfd->types.empty ()
18973 && die_parent == NULL
18974 && has_children
18975 && (tag == DW_TAG_class_type
18976 || tag == DW_TAG_structure_type
18977 || tag == DW_TAG_union_type))
18978 guess_partial_die_structure_name (this, cu);
18979
18980 /* GCC might emit a nameless struct or union that has a linkage
18981 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18982 if (raw_name == NULL
18983 && (tag == DW_TAG_class_type
18984 || tag == DW_TAG_interface_type
18985 || tag == DW_TAG_structure_type
18986 || tag == DW_TAG_union_type)
18987 && linkage_name != NULL)
18988 {
18989 gdb::unique_xmalloc_ptr<char> demangled
18990 (gdb_demangle (linkage_name, DMGL_TYPES));
18991 if (demangled != nullptr)
18992 {
18993 const char *base;
18994
18995 /* Strip any leading namespaces/classes, keep only the base name.
18996 DW_AT_name for named DIEs does not contain the prefixes. */
18997 base = strrchr (demangled.get (), ':');
18998 if (base && base > demangled.get () && base[-1] == ':')
18999 base++;
19000 else
19001 base = demangled.get ();
19002
19003 struct objfile *objfile = cu->per_objfile->objfile;
19004 raw_name = objfile->intern (base);
19005 canonical_name = 1;
19006 }
19007 }
19008
19009 fixup_called = 1;
19010 }
19011
19012 /* Read the .debug_loclists header contents from the given SECTION in the
19013 HEADER. */
19014 static void
19015 read_loclist_header (struct loclist_header *header,
19016 struct dwarf2_section_info *section)
19017 {
19018 unsigned int bytes_read;
19019 bfd *abfd = section->get_bfd_owner ();
19020 const gdb_byte *info_ptr = section->buffer;
19021 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19022 info_ptr += bytes_read;
19023 header->version = read_2_bytes (abfd, info_ptr);
19024 info_ptr += 2;
19025 header->addr_size = read_1_byte (abfd, info_ptr);
19026 info_ptr += 1;
19027 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19028 info_ptr += 1;
19029 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19030 }
19031
19032 /* Return the DW_AT_loclists_base value for the CU. */
19033 static ULONGEST
19034 lookup_loclist_base (struct dwarf2_cu *cu)
19035 {
19036 /* For the .dwo unit, the loclist_base points to the first offset following
19037 the header. The header consists of the following entities-
19038 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19039 bit format)
19040 2. version (2 bytes)
19041 3. address size (1 byte)
19042 4. segment selector size (1 byte)
19043 5. offset entry count (4 bytes)
19044 These sizes are derived as per the DWARFv5 standard. */
19045 if (cu->dwo_unit != nullptr)
19046 {
19047 if (cu->header.initial_length_size == 4)
19048 return LOCLIST_HEADER_SIZE32;
19049 return LOCLIST_HEADER_SIZE64;
19050 }
19051 return cu->loclist_base;
19052 }
19053
19054 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19055 array of offsets in the .debug_loclists section. */
19056 static CORE_ADDR
19057 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19058 {
19059 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19060 struct objfile *objfile = per_objfile->objfile;
19061 bfd *abfd = objfile->obfd;
19062 ULONGEST loclist_base = lookup_loclist_base (cu);
19063 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19064
19065 section->read (objfile);
19066 if (section->buffer == NULL)
19067 complaint (_("DW_FORM_loclistx used without .debug_loclists "
19068 "section [in module %s]"), objfile_name (objfile));
19069 struct loclist_header header;
19070 read_loclist_header (&header, section);
19071 if (loclist_index >= header.offset_entry_count)
19072 complaint (_("DW_FORM_loclistx pointing outside of "
19073 ".debug_loclists offset array [in module %s]"),
19074 objfile_name (objfile));
19075 if (loclist_base + loclist_index * cu->header.offset_size
19076 >= section->size)
19077 complaint (_("DW_FORM_loclistx pointing outside of "
19078 ".debug_loclists section [in module %s]"),
19079 objfile_name (objfile));
19080 const gdb_byte *info_ptr
19081 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
19082
19083 if (cu->header.offset_size == 4)
19084 return bfd_get_32 (abfd, info_ptr) + loclist_base;
19085 else
19086 return bfd_get_64 (abfd, info_ptr) + loclist_base;
19087 }
19088
19089 /* Process the attributes that had to be skipped in the first round. These
19090 attributes are the ones that need str_offsets_base or addr_base attributes.
19091 They could not have been processed in the first round, because at the time
19092 the values of str_offsets_base or addr_base may not have been known. */
19093 static void
19094 read_attribute_reprocess (const struct die_reader_specs *reader,
19095 struct attribute *attr)
19096 {
19097 struct dwarf2_cu *cu = reader->cu;
19098 switch (attr->form)
19099 {
19100 case DW_FORM_addrx:
19101 case DW_FORM_GNU_addr_index:
19102 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19103 break;
19104 case DW_FORM_loclistx:
19105 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19106 break;
19107 case DW_FORM_strx:
19108 case DW_FORM_strx1:
19109 case DW_FORM_strx2:
19110 case DW_FORM_strx3:
19111 case DW_FORM_strx4:
19112 case DW_FORM_GNU_str_index:
19113 {
19114 unsigned int str_index = DW_UNSND (attr);
19115 if (reader->dwo_file != NULL)
19116 {
19117 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19118 DW_STRING_IS_CANONICAL (attr) = 0;
19119 }
19120 else
19121 {
19122 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19123 DW_STRING_IS_CANONICAL (attr) = 0;
19124 }
19125 break;
19126 }
19127 default:
19128 gdb_assert_not_reached (_("Unexpected DWARF form."));
19129 }
19130 }
19131
19132 /* Read an attribute value described by an attribute form. */
19133
19134 static const gdb_byte *
19135 read_attribute_value (const struct die_reader_specs *reader,
19136 struct attribute *attr, unsigned form,
19137 LONGEST implicit_const, const gdb_byte *info_ptr,
19138 bool *need_reprocess)
19139 {
19140 struct dwarf2_cu *cu = reader->cu;
19141 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19142 struct objfile *objfile = per_objfile->objfile;
19143 bfd *abfd = reader->abfd;
19144 struct comp_unit_head *cu_header = &cu->header;
19145 unsigned int bytes_read;
19146 struct dwarf_block *blk;
19147 *need_reprocess = false;
19148
19149 attr->form = (enum dwarf_form) form;
19150 switch (form)
19151 {
19152 case DW_FORM_ref_addr:
19153 if (cu->header.version == 2)
19154 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19155 &bytes_read);
19156 else
19157 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19158 &bytes_read);
19159 info_ptr += bytes_read;
19160 break;
19161 case DW_FORM_GNU_ref_alt:
19162 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19163 info_ptr += bytes_read;
19164 break;
19165 case DW_FORM_addr:
19166 {
19167 struct gdbarch *gdbarch = objfile->arch ();
19168 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19169 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19170 info_ptr += bytes_read;
19171 }
19172 break;
19173 case DW_FORM_block2:
19174 blk = dwarf_alloc_block (cu);
19175 blk->size = read_2_bytes (abfd, info_ptr);
19176 info_ptr += 2;
19177 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19178 info_ptr += blk->size;
19179 DW_BLOCK (attr) = blk;
19180 break;
19181 case DW_FORM_block4:
19182 blk = dwarf_alloc_block (cu);
19183 blk->size = read_4_bytes (abfd, info_ptr);
19184 info_ptr += 4;
19185 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19186 info_ptr += blk->size;
19187 DW_BLOCK (attr) = blk;
19188 break;
19189 case DW_FORM_data2:
19190 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19191 info_ptr += 2;
19192 break;
19193 case DW_FORM_data4:
19194 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19195 info_ptr += 4;
19196 break;
19197 case DW_FORM_data8:
19198 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19199 info_ptr += 8;
19200 break;
19201 case DW_FORM_data16:
19202 blk = dwarf_alloc_block (cu);
19203 blk->size = 16;
19204 blk->data = read_n_bytes (abfd, info_ptr, 16);
19205 info_ptr += 16;
19206 DW_BLOCK (attr) = blk;
19207 break;
19208 case DW_FORM_sec_offset:
19209 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19210 info_ptr += bytes_read;
19211 break;
19212 case DW_FORM_loclistx:
19213 {
19214 *need_reprocess = true;
19215 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19216 info_ptr += bytes_read;
19217 }
19218 break;
19219 case DW_FORM_string:
19220 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19221 DW_STRING_IS_CANONICAL (attr) = 0;
19222 info_ptr += bytes_read;
19223 break;
19224 case DW_FORM_strp:
19225 if (!cu->per_cu->is_dwz)
19226 {
19227 DW_STRING (attr) = read_indirect_string (per_objfile,
19228 abfd, info_ptr, cu_header,
19229 &bytes_read);
19230 DW_STRING_IS_CANONICAL (attr) = 0;
19231 info_ptr += bytes_read;
19232 break;
19233 }
19234 /* FALLTHROUGH */
19235 case DW_FORM_line_strp:
19236 if (!cu->per_cu->is_dwz)
19237 {
19238 DW_STRING (attr) = per_objfile->read_line_string (info_ptr, cu_header,
19239 &bytes_read);
19240 DW_STRING_IS_CANONICAL (attr) = 0;
19241 info_ptr += bytes_read;
19242 break;
19243 }
19244 /* FALLTHROUGH */
19245 case DW_FORM_GNU_strp_alt:
19246 {
19247 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
19248 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19249 &bytes_read);
19250
19251 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
19252 DW_STRING_IS_CANONICAL (attr) = 0;
19253 info_ptr += bytes_read;
19254 }
19255 break;
19256 case DW_FORM_exprloc:
19257 case DW_FORM_block:
19258 blk = dwarf_alloc_block (cu);
19259 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19260 info_ptr += bytes_read;
19261 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19262 info_ptr += blk->size;
19263 DW_BLOCK (attr) = blk;
19264 break;
19265 case DW_FORM_block1:
19266 blk = dwarf_alloc_block (cu);
19267 blk->size = read_1_byte (abfd, info_ptr);
19268 info_ptr += 1;
19269 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19270 info_ptr += blk->size;
19271 DW_BLOCK (attr) = blk;
19272 break;
19273 case DW_FORM_data1:
19274 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19275 info_ptr += 1;
19276 break;
19277 case DW_FORM_flag:
19278 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19279 info_ptr += 1;
19280 break;
19281 case DW_FORM_flag_present:
19282 DW_UNSND (attr) = 1;
19283 break;
19284 case DW_FORM_sdata:
19285 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19286 info_ptr += bytes_read;
19287 break;
19288 case DW_FORM_udata:
19289 case DW_FORM_rnglistx:
19290 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19291 info_ptr += bytes_read;
19292 break;
19293 case DW_FORM_ref1:
19294 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19295 + read_1_byte (abfd, info_ptr));
19296 info_ptr += 1;
19297 break;
19298 case DW_FORM_ref2:
19299 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19300 + read_2_bytes (abfd, info_ptr));
19301 info_ptr += 2;
19302 break;
19303 case DW_FORM_ref4:
19304 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19305 + read_4_bytes (abfd, info_ptr));
19306 info_ptr += 4;
19307 break;
19308 case DW_FORM_ref8:
19309 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19310 + read_8_bytes (abfd, info_ptr));
19311 info_ptr += 8;
19312 break;
19313 case DW_FORM_ref_sig8:
19314 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19315 info_ptr += 8;
19316 break;
19317 case DW_FORM_ref_udata:
19318 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19319 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19320 info_ptr += bytes_read;
19321 break;
19322 case DW_FORM_indirect:
19323 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19324 info_ptr += bytes_read;
19325 if (form == DW_FORM_implicit_const)
19326 {
19327 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19328 info_ptr += bytes_read;
19329 }
19330 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19331 info_ptr, need_reprocess);
19332 break;
19333 case DW_FORM_implicit_const:
19334 DW_SND (attr) = implicit_const;
19335 break;
19336 case DW_FORM_addrx:
19337 case DW_FORM_GNU_addr_index:
19338 *need_reprocess = true;
19339 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19340 info_ptr += bytes_read;
19341 break;
19342 case DW_FORM_strx:
19343 case DW_FORM_strx1:
19344 case DW_FORM_strx2:
19345 case DW_FORM_strx3:
19346 case DW_FORM_strx4:
19347 case DW_FORM_GNU_str_index:
19348 {
19349 ULONGEST str_index;
19350 if (form == DW_FORM_strx1)
19351 {
19352 str_index = read_1_byte (abfd, info_ptr);
19353 info_ptr += 1;
19354 }
19355 else if (form == DW_FORM_strx2)
19356 {
19357 str_index = read_2_bytes (abfd, info_ptr);
19358 info_ptr += 2;
19359 }
19360 else if (form == DW_FORM_strx3)
19361 {
19362 str_index = read_3_bytes (abfd, info_ptr);
19363 info_ptr += 3;
19364 }
19365 else if (form == DW_FORM_strx4)
19366 {
19367 str_index = read_4_bytes (abfd, info_ptr);
19368 info_ptr += 4;
19369 }
19370 else
19371 {
19372 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19373 info_ptr += bytes_read;
19374 }
19375 *need_reprocess = true;
19376 DW_UNSND (attr) = str_index;
19377 }
19378 break;
19379 default:
19380 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19381 dwarf_form_name (form),
19382 bfd_get_filename (abfd));
19383 }
19384
19385 /* Super hack. */
19386 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19387 attr->form = DW_FORM_GNU_ref_alt;
19388
19389 /* We have seen instances where the compiler tried to emit a byte
19390 size attribute of -1 which ended up being encoded as an unsigned
19391 0xffffffff. Although 0xffffffff is technically a valid size value,
19392 an object of this size seems pretty unlikely so we can relatively
19393 safely treat these cases as if the size attribute was invalid and
19394 treat them as zero by default. */
19395 if (attr->name == DW_AT_byte_size
19396 && form == DW_FORM_data4
19397 && DW_UNSND (attr) >= 0xffffffff)
19398 {
19399 complaint
19400 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19401 hex_string (DW_UNSND (attr)));
19402 DW_UNSND (attr) = 0;
19403 }
19404
19405 return info_ptr;
19406 }
19407
19408 /* Read an attribute described by an abbreviated attribute. */
19409
19410 static const gdb_byte *
19411 read_attribute (const struct die_reader_specs *reader,
19412 struct attribute *attr, struct attr_abbrev *abbrev,
19413 const gdb_byte *info_ptr, bool *need_reprocess)
19414 {
19415 attr->name = abbrev->name;
19416 return read_attribute_value (reader, attr, abbrev->form,
19417 abbrev->implicit_const, info_ptr,
19418 need_reprocess);
19419 }
19420
19421 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19422
19423 static const char *
19424 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
19425 LONGEST str_offset)
19426 {
19427 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
19428 str_offset, "DW_FORM_strp");
19429 }
19430
19431 /* Return pointer to string at .debug_str offset as read from BUF.
19432 BUF is assumed to be in a compilation unit described by CU_HEADER.
19433 Return *BYTES_READ_PTR count of bytes read from BUF. */
19434
19435 static const char *
19436 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
19437 const gdb_byte *buf,
19438 const struct comp_unit_head *cu_header,
19439 unsigned int *bytes_read_ptr)
19440 {
19441 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19442
19443 return read_indirect_string_at_offset (per_objfile, str_offset);
19444 }
19445
19446 /* See read.h. */
19447
19448 const char *
19449 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19450 const struct comp_unit_head *cu_header,
19451 unsigned int *bytes_read_ptr)
19452 {
19453 bfd *abfd = objfile->obfd;
19454 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19455
19456 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19457 }
19458
19459 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19460 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19461 ADDR_SIZE is the size of addresses from the CU header. */
19462
19463 static CORE_ADDR
19464 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
19465 gdb::optional<ULONGEST> addr_base, int addr_size)
19466 {
19467 struct objfile *objfile = per_objfile->objfile;
19468 bfd *abfd = objfile->obfd;
19469 const gdb_byte *info_ptr;
19470 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19471
19472 per_objfile->per_bfd->addr.read (objfile);
19473 if (per_objfile->per_bfd->addr.buffer == NULL)
19474 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19475 objfile_name (objfile));
19476 if (addr_base_or_zero + addr_index * addr_size
19477 >= per_objfile->per_bfd->addr.size)
19478 error (_("DW_FORM_addr_index pointing outside of "
19479 ".debug_addr section [in module %s]"),
19480 objfile_name (objfile));
19481 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
19482 + addr_index * addr_size);
19483 if (addr_size == 4)
19484 return bfd_get_32 (abfd, info_ptr);
19485 else
19486 return bfd_get_64 (abfd, info_ptr);
19487 }
19488
19489 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19490
19491 static CORE_ADDR
19492 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19493 {
19494 return read_addr_index_1 (cu->per_objfile, addr_index,
19495 cu->addr_base, cu->header.addr_size);
19496 }
19497
19498 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19499
19500 static CORE_ADDR
19501 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19502 unsigned int *bytes_read)
19503 {
19504 bfd *abfd = cu->per_objfile->objfile->obfd;
19505 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19506
19507 return read_addr_index (cu, addr_index);
19508 }
19509
19510 /* See read.h. */
19511
19512 CORE_ADDR
19513 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19514 dwarf2_per_objfile *per_objfile,
19515 unsigned int addr_index)
19516 {
19517 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
19518 gdb::optional<ULONGEST> addr_base;
19519 int addr_size;
19520
19521 /* We need addr_base and addr_size.
19522 If we don't have PER_CU->cu, we have to get it.
19523 Nasty, but the alternative is storing the needed info in PER_CU,
19524 which at this point doesn't seem justified: it's not clear how frequently
19525 it would get used and it would increase the size of every PER_CU.
19526 Entry points like dwarf2_per_cu_addr_size do a similar thing
19527 so we're not in uncharted territory here.
19528 Alas we need to be a bit more complicated as addr_base is contained
19529 in the DIE.
19530
19531 We don't need to read the entire CU(/TU).
19532 We just need the header and top level die.
19533
19534 IWBN to use the aging mechanism to let us lazily later discard the CU.
19535 For now we skip this optimization. */
19536
19537 if (cu != NULL)
19538 {
19539 addr_base = cu->addr_base;
19540 addr_size = cu->header.addr_size;
19541 }
19542 else
19543 {
19544 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
19545 addr_base = reader.cu->addr_base;
19546 addr_size = reader.cu->header.addr_size;
19547 }
19548
19549 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
19550 }
19551
19552 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19553 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19554 DWO file. */
19555
19556 static const char *
19557 read_str_index (struct dwarf2_cu *cu,
19558 struct dwarf2_section_info *str_section,
19559 struct dwarf2_section_info *str_offsets_section,
19560 ULONGEST str_offsets_base, ULONGEST str_index)
19561 {
19562 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19563 struct objfile *objfile = per_objfile->objfile;
19564 const char *objf_name = objfile_name (objfile);
19565 bfd *abfd = objfile->obfd;
19566 const gdb_byte *info_ptr;
19567 ULONGEST str_offset;
19568 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19569
19570 str_section->read (objfile);
19571 str_offsets_section->read (objfile);
19572 if (str_section->buffer == NULL)
19573 error (_("%s used without %s section"
19574 " in CU at offset %s [in module %s]"),
19575 form_name, str_section->get_name (),
19576 sect_offset_str (cu->header.sect_off), objf_name);
19577 if (str_offsets_section->buffer == NULL)
19578 error (_("%s used without %s section"
19579 " in CU at offset %s [in module %s]"),
19580 form_name, str_section->get_name (),
19581 sect_offset_str (cu->header.sect_off), objf_name);
19582 info_ptr = (str_offsets_section->buffer
19583 + str_offsets_base
19584 + str_index * cu->header.offset_size);
19585 if (cu->header.offset_size == 4)
19586 str_offset = bfd_get_32 (abfd, info_ptr);
19587 else
19588 str_offset = bfd_get_64 (abfd, info_ptr);
19589 if (str_offset >= str_section->size)
19590 error (_("Offset from %s pointing outside of"
19591 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19592 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19593 return (const char *) (str_section->buffer + str_offset);
19594 }
19595
19596 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19597
19598 static const char *
19599 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19600 {
19601 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19602 ? reader->cu->header.addr_size : 0;
19603 return read_str_index (reader->cu,
19604 &reader->dwo_file->sections.str,
19605 &reader->dwo_file->sections.str_offsets,
19606 str_offsets_base, str_index);
19607 }
19608
19609 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19610
19611 static const char *
19612 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19613 {
19614 struct objfile *objfile = cu->per_objfile->objfile;
19615 const char *objf_name = objfile_name (objfile);
19616 static const char form_name[] = "DW_FORM_GNU_str_index";
19617 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19618
19619 if (!cu->str_offsets_base.has_value ())
19620 error (_("%s used in Fission stub without %s"
19621 " in CU at offset 0x%lx [in module %s]"),
19622 form_name, str_offsets_attr_name,
19623 (long) cu->header.offset_size, objf_name);
19624
19625 return read_str_index (cu,
19626 &cu->per_objfile->per_bfd->str,
19627 &cu->per_objfile->per_bfd->str_offsets,
19628 *cu->str_offsets_base, str_index);
19629 }
19630
19631 /* Return the length of an LEB128 number in BUF. */
19632
19633 static int
19634 leb128_size (const gdb_byte *buf)
19635 {
19636 const gdb_byte *begin = buf;
19637 gdb_byte byte;
19638
19639 while (1)
19640 {
19641 byte = *buf++;
19642 if ((byte & 128) == 0)
19643 return buf - begin;
19644 }
19645 }
19646
19647 static void
19648 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19649 {
19650 switch (lang)
19651 {
19652 case DW_LANG_C89:
19653 case DW_LANG_C99:
19654 case DW_LANG_C11:
19655 case DW_LANG_C:
19656 case DW_LANG_UPC:
19657 cu->language = language_c;
19658 break;
19659 case DW_LANG_Java:
19660 case DW_LANG_C_plus_plus:
19661 case DW_LANG_C_plus_plus_11:
19662 case DW_LANG_C_plus_plus_14:
19663 cu->language = language_cplus;
19664 break;
19665 case DW_LANG_D:
19666 cu->language = language_d;
19667 break;
19668 case DW_LANG_Fortran77:
19669 case DW_LANG_Fortran90:
19670 case DW_LANG_Fortran95:
19671 case DW_LANG_Fortran03:
19672 case DW_LANG_Fortran08:
19673 cu->language = language_fortran;
19674 break;
19675 case DW_LANG_Go:
19676 cu->language = language_go;
19677 break;
19678 case DW_LANG_Mips_Assembler:
19679 cu->language = language_asm;
19680 break;
19681 case DW_LANG_Ada83:
19682 case DW_LANG_Ada95:
19683 cu->language = language_ada;
19684 break;
19685 case DW_LANG_Modula2:
19686 cu->language = language_m2;
19687 break;
19688 case DW_LANG_Pascal83:
19689 cu->language = language_pascal;
19690 break;
19691 case DW_LANG_ObjC:
19692 cu->language = language_objc;
19693 break;
19694 case DW_LANG_Rust:
19695 case DW_LANG_Rust_old:
19696 cu->language = language_rust;
19697 break;
19698 case DW_LANG_Cobol74:
19699 case DW_LANG_Cobol85:
19700 default:
19701 cu->language = language_minimal;
19702 break;
19703 }
19704 cu->language_defn = language_def (cu->language);
19705 }
19706
19707 /* Return the named attribute or NULL if not there. */
19708
19709 static struct attribute *
19710 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19711 {
19712 for (;;)
19713 {
19714 unsigned int i;
19715 struct attribute *spec = NULL;
19716
19717 for (i = 0; i < die->num_attrs; ++i)
19718 {
19719 if (die->attrs[i].name == name)
19720 return &die->attrs[i];
19721 if (die->attrs[i].name == DW_AT_specification
19722 || die->attrs[i].name == DW_AT_abstract_origin)
19723 spec = &die->attrs[i];
19724 }
19725
19726 if (!spec)
19727 break;
19728
19729 die = follow_die_ref (die, spec, &cu);
19730 }
19731
19732 return NULL;
19733 }
19734
19735 /* Return the string associated with a string-typed attribute, or NULL if it
19736 is either not found or is of an incorrect type. */
19737
19738 static const char *
19739 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19740 {
19741 struct attribute *attr;
19742 const char *str = NULL;
19743
19744 attr = dwarf2_attr (die, name, cu);
19745
19746 if (attr != NULL)
19747 {
19748 str = attr->value_as_string ();
19749 if (str == nullptr)
19750 complaint (_("string type expected for attribute %s for "
19751 "DIE at %s in module %s"),
19752 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19753 objfile_name (cu->per_objfile->objfile));
19754 }
19755
19756 return str;
19757 }
19758
19759 /* Return the dwo name or NULL if not present. If present, it is in either
19760 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19761 static const char *
19762 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19763 {
19764 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19765 if (dwo_name == nullptr)
19766 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19767 return dwo_name;
19768 }
19769
19770 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19771 and holds a non-zero value. This function should only be used for
19772 DW_FORM_flag or DW_FORM_flag_present attributes. */
19773
19774 static int
19775 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19776 {
19777 struct attribute *attr = dwarf2_attr (die, name, cu);
19778
19779 return (attr && DW_UNSND (attr));
19780 }
19781
19782 static int
19783 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19784 {
19785 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19786 which value is non-zero. However, we have to be careful with
19787 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19788 (via dwarf2_flag_true_p) follows this attribute. So we may
19789 end up accidently finding a declaration attribute that belongs
19790 to a different DIE referenced by the specification attribute,
19791 even though the given DIE does not have a declaration attribute. */
19792 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19793 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19794 }
19795
19796 /* Return the die giving the specification for DIE, if there is
19797 one. *SPEC_CU is the CU containing DIE on input, and the CU
19798 containing the return value on output. If there is no
19799 specification, but there is an abstract origin, that is
19800 returned. */
19801
19802 static struct die_info *
19803 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19804 {
19805 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19806 *spec_cu);
19807
19808 if (spec_attr == NULL)
19809 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19810
19811 if (spec_attr == NULL)
19812 return NULL;
19813 else
19814 return follow_die_ref (die, spec_attr, spec_cu);
19815 }
19816
19817 /* Stub for free_line_header to match void * callback types. */
19818
19819 static void
19820 free_line_header_voidp (void *arg)
19821 {
19822 struct line_header *lh = (struct line_header *) arg;
19823
19824 delete lh;
19825 }
19826
19827 /* A convenience function to find the proper .debug_line section for a CU. */
19828
19829 static struct dwarf2_section_info *
19830 get_debug_line_section (struct dwarf2_cu *cu)
19831 {
19832 struct dwarf2_section_info *section;
19833 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19834
19835 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19836 DWO file. */
19837 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19838 section = &cu->dwo_unit->dwo_file->sections.line;
19839 else if (cu->per_cu->is_dwz)
19840 {
19841 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
19842
19843 section = &dwz->line;
19844 }
19845 else
19846 section = &per_objfile->per_bfd->line;
19847
19848 return section;
19849 }
19850
19851 /* Read the statement program header starting at OFFSET in
19852 .debug_line, or .debug_line.dwo. Return a pointer
19853 to a struct line_header, allocated using xmalloc.
19854 Returns NULL if there is a problem reading the header, e.g., if it
19855 has a version we don't understand.
19856
19857 NOTE: the strings in the include directory and file name tables of
19858 the returned object point into the dwarf line section buffer,
19859 and must not be freed. */
19860
19861 static line_header_up
19862 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19863 {
19864 struct dwarf2_section_info *section;
19865 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19866
19867 section = get_debug_line_section (cu);
19868 section->read (per_objfile->objfile);
19869 if (section->buffer == NULL)
19870 {
19871 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19872 complaint (_("missing .debug_line.dwo section"));
19873 else
19874 complaint (_("missing .debug_line section"));
19875 return 0;
19876 }
19877
19878 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19879 per_objfile, section, &cu->header);
19880 }
19881
19882 /* Subroutine of dwarf_decode_lines to simplify it.
19883 Return the file name of the psymtab for the given file_entry.
19884 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19885 If space for the result is malloc'd, *NAME_HOLDER will be set.
19886 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19887
19888 static const char *
19889 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19890 const dwarf2_psymtab *pst,
19891 const char *comp_dir,
19892 gdb::unique_xmalloc_ptr<char> *name_holder)
19893 {
19894 const char *include_name = fe.name;
19895 const char *include_name_to_compare = include_name;
19896 const char *pst_filename;
19897 int file_is_pst;
19898
19899 const char *dir_name = fe.include_dir (lh);
19900
19901 gdb::unique_xmalloc_ptr<char> hold_compare;
19902 if (!IS_ABSOLUTE_PATH (include_name)
19903 && (dir_name != NULL || comp_dir != NULL))
19904 {
19905 /* Avoid creating a duplicate psymtab for PST.
19906 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19907 Before we do the comparison, however, we need to account
19908 for DIR_NAME and COMP_DIR.
19909 First prepend dir_name (if non-NULL). If we still don't
19910 have an absolute path prepend comp_dir (if non-NULL).
19911 However, the directory we record in the include-file's
19912 psymtab does not contain COMP_DIR (to match the
19913 corresponding symtab(s)).
19914
19915 Example:
19916
19917 bash$ cd /tmp
19918 bash$ gcc -g ./hello.c
19919 include_name = "hello.c"
19920 dir_name = "."
19921 DW_AT_comp_dir = comp_dir = "/tmp"
19922 DW_AT_name = "./hello.c"
19923
19924 */
19925
19926 if (dir_name != NULL)
19927 {
19928 name_holder->reset (concat (dir_name, SLASH_STRING,
19929 include_name, (char *) NULL));
19930 include_name = name_holder->get ();
19931 include_name_to_compare = include_name;
19932 }
19933 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19934 {
19935 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19936 include_name, (char *) NULL));
19937 include_name_to_compare = hold_compare.get ();
19938 }
19939 }
19940
19941 pst_filename = pst->filename;
19942 gdb::unique_xmalloc_ptr<char> copied_name;
19943 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19944 {
19945 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19946 pst_filename, (char *) NULL));
19947 pst_filename = copied_name.get ();
19948 }
19949
19950 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19951
19952 if (file_is_pst)
19953 return NULL;
19954 return include_name;
19955 }
19956
19957 /* State machine to track the state of the line number program. */
19958
19959 class lnp_state_machine
19960 {
19961 public:
19962 /* Initialize a machine state for the start of a line number
19963 program. */
19964 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19965 bool record_lines_p);
19966
19967 file_entry *current_file ()
19968 {
19969 /* lh->file_names is 0-based, but the file name numbers in the
19970 statement program are 1-based. */
19971 return m_line_header->file_name_at (m_file);
19972 }
19973
19974 /* Record the line in the state machine. END_SEQUENCE is true if
19975 we're processing the end of a sequence. */
19976 void record_line (bool end_sequence);
19977
19978 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19979 nop-out rest of the lines in this sequence. */
19980 void check_line_address (struct dwarf2_cu *cu,
19981 const gdb_byte *line_ptr,
19982 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19983
19984 void handle_set_discriminator (unsigned int discriminator)
19985 {
19986 m_discriminator = discriminator;
19987 m_line_has_non_zero_discriminator |= discriminator != 0;
19988 }
19989
19990 /* Handle DW_LNE_set_address. */
19991 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19992 {
19993 m_op_index = 0;
19994 address += baseaddr;
19995 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19996 }
19997
19998 /* Handle DW_LNS_advance_pc. */
19999 void handle_advance_pc (CORE_ADDR adjust);
20000
20001 /* Handle a special opcode. */
20002 void handle_special_opcode (unsigned char op_code);
20003
20004 /* Handle DW_LNS_advance_line. */
20005 void handle_advance_line (int line_delta)
20006 {
20007 advance_line (line_delta);
20008 }
20009
20010 /* Handle DW_LNS_set_file. */
20011 void handle_set_file (file_name_index file);
20012
20013 /* Handle DW_LNS_negate_stmt. */
20014 void handle_negate_stmt ()
20015 {
20016 m_is_stmt = !m_is_stmt;
20017 }
20018
20019 /* Handle DW_LNS_const_add_pc. */
20020 void handle_const_add_pc ();
20021
20022 /* Handle DW_LNS_fixed_advance_pc. */
20023 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20024 {
20025 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20026 m_op_index = 0;
20027 }
20028
20029 /* Handle DW_LNS_copy. */
20030 void handle_copy ()
20031 {
20032 record_line (false);
20033 m_discriminator = 0;
20034 }
20035
20036 /* Handle DW_LNE_end_sequence. */
20037 void handle_end_sequence ()
20038 {
20039 m_currently_recording_lines = true;
20040 }
20041
20042 private:
20043 /* Advance the line by LINE_DELTA. */
20044 void advance_line (int line_delta)
20045 {
20046 m_line += line_delta;
20047
20048 if (line_delta != 0)
20049 m_line_has_non_zero_discriminator = m_discriminator != 0;
20050 }
20051
20052 struct dwarf2_cu *m_cu;
20053
20054 gdbarch *m_gdbarch;
20055
20056 /* True if we're recording lines.
20057 Otherwise we're building partial symtabs and are just interested in
20058 finding include files mentioned by the line number program. */
20059 bool m_record_lines_p;
20060
20061 /* The line number header. */
20062 line_header *m_line_header;
20063
20064 /* These are part of the standard DWARF line number state machine,
20065 and initialized according to the DWARF spec. */
20066
20067 unsigned char m_op_index = 0;
20068 /* The line table index of the current file. */
20069 file_name_index m_file = 1;
20070 unsigned int m_line = 1;
20071
20072 /* These are initialized in the constructor. */
20073
20074 CORE_ADDR m_address;
20075 bool m_is_stmt;
20076 unsigned int m_discriminator;
20077
20078 /* Additional bits of state we need to track. */
20079
20080 /* The last file that we called dwarf2_start_subfile for.
20081 This is only used for TLLs. */
20082 unsigned int m_last_file = 0;
20083 /* The last file a line number was recorded for. */
20084 struct subfile *m_last_subfile = NULL;
20085
20086 /* The address of the last line entry. */
20087 CORE_ADDR m_last_address;
20088
20089 /* Set to true when a previous line at the same address (using
20090 m_last_address) had m_is_stmt true. This is reset to false when a
20091 line entry at a new address (m_address different to m_last_address) is
20092 processed. */
20093 bool m_stmt_at_address = false;
20094
20095 /* When true, record the lines we decode. */
20096 bool m_currently_recording_lines = false;
20097
20098 /* The last line number that was recorded, used to coalesce
20099 consecutive entries for the same line. This can happen, for
20100 example, when discriminators are present. PR 17276. */
20101 unsigned int m_last_line = 0;
20102 bool m_line_has_non_zero_discriminator = false;
20103 };
20104
20105 void
20106 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20107 {
20108 CORE_ADDR addr_adj = (((m_op_index + adjust)
20109 / m_line_header->maximum_ops_per_instruction)
20110 * m_line_header->minimum_instruction_length);
20111 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20112 m_op_index = ((m_op_index + adjust)
20113 % m_line_header->maximum_ops_per_instruction);
20114 }
20115
20116 void
20117 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20118 {
20119 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20120 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20121 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20122 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20123 / m_line_header->maximum_ops_per_instruction)
20124 * m_line_header->minimum_instruction_length);
20125 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20126 m_op_index = ((m_op_index + adj_opcode_d)
20127 % m_line_header->maximum_ops_per_instruction);
20128
20129 int line_delta = m_line_header->line_base + adj_opcode_r;
20130 advance_line (line_delta);
20131 record_line (false);
20132 m_discriminator = 0;
20133 }
20134
20135 void
20136 lnp_state_machine::handle_set_file (file_name_index file)
20137 {
20138 m_file = file;
20139
20140 const file_entry *fe = current_file ();
20141 if (fe == NULL)
20142 dwarf2_debug_line_missing_file_complaint ();
20143 else if (m_record_lines_p)
20144 {
20145 const char *dir = fe->include_dir (m_line_header);
20146
20147 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20148 m_line_has_non_zero_discriminator = m_discriminator != 0;
20149 dwarf2_start_subfile (m_cu, fe->name, dir);
20150 }
20151 }
20152
20153 void
20154 lnp_state_machine::handle_const_add_pc ()
20155 {
20156 CORE_ADDR adjust
20157 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20158
20159 CORE_ADDR addr_adj
20160 = (((m_op_index + adjust)
20161 / m_line_header->maximum_ops_per_instruction)
20162 * m_line_header->minimum_instruction_length);
20163
20164 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20165 m_op_index = ((m_op_index + adjust)
20166 % m_line_header->maximum_ops_per_instruction);
20167 }
20168
20169 /* Return non-zero if we should add LINE to the line number table.
20170 LINE is the line to add, LAST_LINE is the last line that was added,
20171 LAST_SUBFILE is the subfile for LAST_LINE.
20172 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20173 had a non-zero discriminator.
20174
20175 We have to be careful in the presence of discriminators.
20176 E.g., for this line:
20177
20178 for (i = 0; i < 100000; i++);
20179
20180 clang can emit four line number entries for that one line,
20181 each with a different discriminator.
20182 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20183
20184 However, we want gdb to coalesce all four entries into one.
20185 Otherwise the user could stepi into the middle of the line and
20186 gdb would get confused about whether the pc really was in the
20187 middle of the line.
20188
20189 Things are further complicated by the fact that two consecutive
20190 line number entries for the same line is a heuristic used by gcc
20191 to denote the end of the prologue. So we can't just discard duplicate
20192 entries, we have to be selective about it. The heuristic we use is
20193 that we only collapse consecutive entries for the same line if at least
20194 one of those entries has a non-zero discriminator. PR 17276.
20195
20196 Note: Addresses in the line number state machine can never go backwards
20197 within one sequence, thus this coalescing is ok. */
20198
20199 static int
20200 dwarf_record_line_p (struct dwarf2_cu *cu,
20201 unsigned int line, unsigned int last_line,
20202 int line_has_non_zero_discriminator,
20203 struct subfile *last_subfile)
20204 {
20205 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20206 return 1;
20207 if (line != last_line)
20208 return 1;
20209 /* Same line for the same file that we've seen already.
20210 As a last check, for pr 17276, only record the line if the line
20211 has never had a non-zero discriminator. */
20212 if (!line_has_non_zero_discriminator)
20213 return 1;
20214 return 0;
20215 }
20216
20217 /* Use the CU's builder to record line number LINE beginning at
20218 address ADDRESS in the line table of subfile SUBFILE. */
20219
20220 static void
20221 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20222 unsigned int line, CORE_ADDR address, bool is_stmt,
20223 struct dwarf2_cu *cu)
20224 {
20225 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20226
20227 if (dwarf_line_debug)
20228 {
20229 fprintf_unfiltered (gdb_stdlog,
20230 "Recording line %u, file %s, address %s\n",
20231 line, lbasename (subfile->name),
20232 paddress (gdbarch, address));
20233 }
20234
20235 if (cu != nullptr)
20236 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20237 }
20238
20239 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20240 Mark the end of a set of line number records.
20241 The arguments are the same as for dwarf_record_line_1.
20242 If SUBFILE is NULL the request is ignored. */
20243
20244 static void
20245 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20246 CORE_ADDR address, struct dwarf2_cu *cu)
20247 {
20248 if (subfile == NULL)
20249 return;
20250
20251 if (dwarf_line_debug)
20252 {
20253 fprintf_unfiltered (gdb_stdlog,
20254 "Finishing current line, file %s, address %s\n",
20255 lbasename (subfile->name),
20256 paddress (gdbarch, address));
20257 }
20258
20259 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20260 }
20261
20262 void
20263 lnp_state_machine::record_line (bool end_sequence)
20264 {
20265 if (dwarf_line_debug)
20266 {
20267 fprintf_unfiltered (gdb_stdlog,
20268 "Processing actual line %u: file %u,"
20269 " address %s, is_stmt %u, discrim %u%s\n",
20270 m_line, m_file,
20271 paddress (m_gdbarch, m_address),
20272 m_is_stmt, m_discriminator,
20273 (end_sequence ? "\t(end sequence)" : ""));
20274 }
20275
20276 file_entry *fe = current_file ();
20277
20278 if (fe == NULL)
20279 dwarf2_debug_line_missing_file_complaint ();
20280 /* For now we ignore lines not starting on an instruction boundary.
20281 But not when processing end_sequence for compatibility with the
20282 previous version of the code. */
20283 else if (m_op_index == 0 || end_sequence)
20284 {
20285 fe->included_p = 1;
20286 if (m_record_lines_p)
20287 {
20288 /* When we switch files we insert an end maker in the first file,
20289 switch to the second file and add a new line entry. The
20290 problem is that the end marker inserted in the first file will
20291 discard any previous line entries at the same address. If the
20292 line entries in the first file are marked as is-stmt, while
20293 the new line in the second file is non-stmt, then this means
20294 the end marker will discard is-stmt lines so we can have a
20295 non-stmt line. This means that there are less addresses at
20296 which the user can insert a breakpoint.
20297
20298 To improve this we track the last address in m_last_address,
20299 and whether we have seen an is-stmt at this address. Then
20300 when switching files, if we have seen a stmt at the current
20301 address, and we are switching to create a non-stmt line, then
20302 discard the new line. */
20303 bool file_changed
20304 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
20305 bool ignore_this_line
20306 = (file_changed && !end_sequence && m_last_address == m_address
20307 && !m_is_stmt && m_stmt_at_address);
20308
20309 if ((file_changed && !ignore_this_line) || end_sequence)
20310 {
20311 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20312 m_currently_recording_lines ? m_cu : nullptr);
20313 }
20314
20315 if (!end_sequence && !ignore_this_line)
20316 {
20317 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20318
20319 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20320 m_line_has_non_zero_discriminator,
20321 m_last_subfile))
20322 {
20323 buildsym_compunit *builder = m_cu->get_builder ();
20324 dwarf_record_line_1 (m_gdbarch,
20325 builder->get_current_subfile (),
20326 m_line, m_address, is_stmt,
20327 m_currently_recording_lines ? m_cu : nullptr);
20328 }
20329 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20330 m_last_line = m_line;
20331 }
20332 }
20333 }
20334
20335 /* Track whether we have seen any m_is_stmt true at m_address in case we
20336 have multiple line table entries all at m_address. */
20337 if (m_last_address != m_address)
20338 {
20339 m_stmt_at_address = false;
20340 m_last_address = m_address;
20341 }
20342 m_stmt_at_address |= m_is_stmt;
20343 }
20344
20345 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20346 line_header *lh, bool record_lines_p)
20347 {
20348 m_cu = cu;
20349 m_gdbarch = arch;
20350 m_record_lines_p = record_lines_p;
20351 m_line_header = lh;
20352
20353 m_currently_recording_lines = true;
20354
20355 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20356 was a line entry for it so that the backend has a chance to adjust it
20357 and also record it in case it needs it. This is currently used by MIPS
20358 code, cf. `mips_adjust_dwarf2_line'. */
20359 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20360 m_is_stmt = lh->default_is_stmt;
20361 m_discriminator = 0;
20362
20363 m_last_address = m_address;
20364 m_stmt_at_address = false;
20365 }
20366
20367 void
20368 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20369 const gdb_byte *line_ptr,
20370 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20371 {
20372 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20373 the pc range of the CU. However, we restrict the test to only ADDRESS
20374 values of zero to preserve GDB's previous behaviour which is to handle
20375 the specific case of a function being GC'd by the linker. */
20376
20377 if (address == 0 && address < unrelocated_lowpc)
20378 {
20379 /* This line table is for a function which has been
20380 GCd by the linker. Ignore it. PR gdb/12528 */
20381
20382 struct objfile *objfile = cu->per_objfile->objfile;
20383 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20384
20385 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20386 line_offset, objfile_name (objfile));
20387 m_currently_recording_lines = false;
20388 /* Note: m_currently_recording_lines is left as false until we see
20389 DW_LNE_end_sequence. */
20390 }
20391 }
20392
20393 /* Subroutine of dwarf_decode_lines to simplify it.
20394 Process the line number information in LH.
20395 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20396 program in order to set included_p for every referenced header. */
20397
20398 static void
20399 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20400 const int decode_for_pst_p, CORE_ADDR lowpc)
20401 {
20402 const gdb_byte *line_ptr, *extended_end;
20403 const gdb_byte *line_end;
20404 unsigned int bytes_read, extended_len;
20405 unsigned char op_code, extended_op;
20406 CORE_ADDR baseaddr;
20407 struct objfile *objfile = cu->per_objfile->objfile;
20408 bfd *abfd = objfile->obfd;
20409 struct gdbarch *gdbarch = objfile->arch ();
20410 /* True if we're recording line info (as opposed to building partial
20411 symtabs and just interested in finding include files mentioned by
20412 the line number program). */
20413 bool record_lines_p = !decode_for_pst_p;
20414
20415 baseaddr = objfile->text_section_offset ();
20416
20417 line_ptr = lh->statement_program_start;
20418 line_end = lh->statement_program_end;
20419
20420 /* Read the statement sequences until there's nothing left. */
20421 while (line_ptr < line_end)
20422 {
20423 /* The DWARF line number program state machine. Reset the state
20424 machine at the start of each sequence. */
20425 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20426 bool end_sequence = false;
20427
20428 if (record_lines_p)
20429 {
20430 /* Start a subfile for the current file of the state
20431 machine. */
20432 const file_entry *fe = state_machine.current_file ();
20433
20434 if (fe != NULL)
20435 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20436 }
20437
20438 /* Decode the table. */
20439 while (line_ptr < line_end && !end_sequence)
20440 {
20441 op_code = read_1_byte (abfd, line_ptr);
20442 line_ptr += 1;
20443
20444 if (op_code >= lh->opcode_base)
20445 {
20446 /* Special opcode. */
20447 state_machine.handle_special_opcode (op_code);
20448 }
20449 else switch (op_code)
20450 {
20451 case DW_LNS_extended_op:
20452 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20453 &bytes_read);
20454 line_ptr += bytes_read;
20455 extended_end = line_ptr + extended_len;
20456 extended_op = read_1_byte (abfd, line_ptr);
20457 line_ptr += 1;
20458 switch (extended_op)
20459 {
20460 case DW_LNE_end_sequence:
20461 state_machine.handle_end_sequence ();
20462 end_sequence = true;
20463 break;
20464 case DW_LNE_set_address:
20465 {
20466 CORE_ADDR address
20467 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20468 line_ptr += bytes_read;
20469
20470 state_machine.check_line_address (cu, line_ptr,
20471 lowpc - baseaddr, address);
20472 state_machine.handle_set_address (baseaddr, address);
20473 }
20474 break;
20475 case DW_LNE_define_file:
20476 {
20477 const char *cur_file;
20478 unsigned int mod_time, length;
20479 dir_index dindex;
20480
20481 cur_file = read_direct_string (abfd, line_ptr,
20482 &bytes_read);
20483 line_ptr += bytes_read;
20484 dindex = (dir_index)
20485 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20486 line_ptr += bytes_read;
20487 mod_time =
20488 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20489 line_ptr += bytes_read;
20490 length =
20491 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20492 line_ptr += bytes_read;
20493 lh->add_file_name (cur_file, dindex, mod_time, length);
20494 }
20495 break;
20496 case DW_LNE_set_discriminator:
20497 {
20498 /* The discriminator is not interesting to the
20499 debugger; just ignore it. We still need to
20500 check its value though:
20501 if there are consecutive entries for the same
20502 (non-prologue) line we want to coalesce them.
20503 PR 17276. */
20504 unsigned int discr
20505 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20506 line_ptr += bytes_read;
20507
20508 state_machine.handle_set_discriminator (discr);
20509 }
20510 break;
20511 default:
20512 complaint (_("mangled .debug_line section"));
20513 return;
20514 }
20515 /* Make sure that we parsed the extended op correctly. If e.g.
20516 we expected a different address size than the producer used,
20517 we may have read the wrong number of bytes. */
20518 if (line_ptr != extended_end)
20519 {
20520 complaint (_("mangled .debug_line section"));
20521 return;
20522 }
20523 break;
20524 case DW_LNS_copy:
20525 state_machine.handle_copy ();
20526 break;
20527 case DW_LNS_advance_pc:
20528 {
20529 CORE_ADDR adjust
20530 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20531 line_ptr += bytes_read;
20532
20533 state_machine.handle_advance_pc (adjust);
20534 }
20535 break;
20536 case DW_LNS_advance_line:
20537 {
20538 int line_delta
20539 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20540 line_ptr += bytes_read;
20541
20542 state_machine.handle_advance_line (line_delta);
20543 }
20544 break;
20545 case DW_LNS_set_file:
20546 {
20547 file_name_index file
20548 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20549 &bytes_read);
20550 line_ptr += bytes_read;
20551
20552 state_machine.handle_set_file (file);
20553 }
20554 break;
20555 case DW_LNS_set_column:
20556 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20557 line_ptr += bytes_read;
20558 break;
20559 case DW_LNS_negate_stmt:
20560 state_machine.handle_negate_stmt ();
20561 break;
20562 case DW_LNS_set_basic_block:
20563 break;
20564 /* Add to the address register of the state machine the
20565 address increment value corresponding to special opcode
20566 255. I.e., this value is scaled by the minimum
20567 instruction length since special opcode 255 would have
20568 scaled the increment. */
20569 case DW_LNS_const_add_pc:
20570 state_machine.handle_const_add_pc ();
20571 break;
20572 case DW_LNS_fixed_advance_pc:
20573 {
20574 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20575 line_ptr += 2;
20576
20577 state_machine.handle_fixed_advance_pc (addr_adj);
20578 }
20579 break;
20580 default:
20581 {
20582 /* Unknown standard opcode, ignore it. */
20583 int i;
20584
20585 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20586 {
20587 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20588 line_ptr += bytes_read;
20589 }
20590 }
20591 }
20592 }
20593
20594 if (!end_sequence)
20595 dwarf2_debug_line_missing_end_sequence_complaint ();
20596
20597 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20598 in which case we still finish recording the last line). */
20599 state_machine.record_line (true);
20600 }
20601 }
20602
20603 /* Decode the Line Number Program (LNP) for the given line_header
20604 structure and CU. The actual information extracted and the type
20605 of structures created from the LNP depends on the value of PST.
20606
20607 1. If PST is NULL, then this procedure uses the data from the program
20608 to create all necessary symbol tables, and their linetables.
20609
20610 2. If PST is not NULL, this procedure reads the program to determine
20611 the list of files included by the unit represented by PST, and
20612 builds all the associated partial symbol tables.
20613
20614 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20615 It is used for relative paths in the line table.
20616 NOTE: When processing partial symtabs (pst != NULL),
20617 comp_dir == pst->dirname.
20618
20619 NOTE: It is important that psymtabs have the same file name (via strcmp)
20620 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20621 symtab we don't use it in the name of the psymtabs we create.
20622 E.g. expand_line_sal requires this when finding psymtabs to expand.
20623 A good testcase for this is mb-inline.exp.
20624
20625 LOWPC is the lowest address in CU (or 0 if not known).
20626
20627 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20628 for its PC<->lines mapping information. Otherwise only the filename
20629 table is read in. */
20630
20631 static void
20632 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20633 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20634 CORE_ADDR lowpc, int decode_mapping)
20635 {
20636 struct objfile *objfile = cu->per_objfile->objfile;
20637 const int decode_for_pst_p = (pst != NULL);
20638
20639 if (decode_mapping)
20640 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20641
20642 if (decode_for_pst_p)
20643 {
20644 /* Now that we're done scanning the Line Header Program, we can
20645 create the psymtab of each included file. */
20646 for (auto &file_entry : lh->file_names ())
20647 if (file_entry.included_p == 1)
20648 {
20649 gdb::unique_xmalloc_ptr<char> name_holder;
20650 const char *include_name =
20651 psymtab_include_file_name (lh, file_entry, pst,
20652 comp_dir, &name_holder);
20653 if (include_name != NULL)
20654 dwarf2_create_include_psymtab (include_name, pst, objfile);
20655 }
20656 }
20657 else
20658 {
20659 /* Make sure a symtab is created for every file, even files
20660 which contain only variables (i.e. no code with associated
20661 line numbers). */
20662 buildsym_compunit *builder = cu->get_builder ();
20663 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20664
20665 for (auto &fe : lh->file_names ())
20666 {
20667 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20668 if (builder->get_current_subfile ()->symtab == NULL)
20669 {
20670 builder->get_current_subfile ()->symtab
20671 = allocate_symtab (cust,
20672 builder->get_current_subfile ()->name);
20673 }
20674 fe.symtab = builder->get_current_subfile ()->symtab;
20675 }
20676 }
20677 }
20678
20679 /* Start a subfile for DWARF. FILENAME is the name of the file and
20680 DIRNAME the name of the source directory which contains FILENAME
20681 or NULL if not known.
20682 This routine tries to keep line numbers from identical absolute and
20683 relative file names in a common subfile.
20684
20685 Using the `list' example from the GDB testsuite, which resides in
20686 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20687 of /srcdir/list0.c yields the following debugging information for list0.c:
20688
20689 DW_AT_name: /srcdir/list0.c
20690 DW_AT_comp_dir: /compdir
20691 files.files[0].name: list0.h
20692 files.files[0].dir: /srcdir
20693 files.files[1].name: list0.c
20694 files.files[1].dir: /srcdir
20695
20696 The line number information for list0.c has to end up in a single
20697 subfile, so that `break /srcdir/list0.c:1' works as expected.
20698 start_subfile will ensure that this happens provided that we pass the
20699 concatenation of files.files[1].dir and files.files[1].name as the
20700 subfile's name. */
20701
20702 static void
20703 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20704 const char *dirname)
20705 {
20706 gdb::unique_xmalloc_ptr<char> copy;
20707
20708 /* In order not to lose the line information directory,
20709 we concatenate it to the filename when it makes sense.
20710 Note that the Dwarf3 standard says (speaking of filenames in line
20711 information): ``The directory index is ignored for file names
20712 that represent full path names''. Thus ignoring dirname in the
20713 `else' branch below isn't an issue. */
20714
20715 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20716 {
20717 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20718 filename = copy.get ();
20719 }
20720
20721 cu->get_builder ()->start_subfile (filename);
20722 }
20723
20724 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20725 buildsym_compunit constructor. */
20726
20727 struct compunit_symtab *
20728 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20729 CORE_ADDR low_pc)
20730 {
20731 gdb_assert (m_builder == nullptr);
20732
20733 m_builder.reset (new struct buildsym_compunit
20734 (this->per_objfile->objfile,
20735 name, comp_dir, language, low_pc));
20736
20737 list_in_scope = get_builder ()->get_file_symbols ();
20738
20739 get_builder ()->record_debugformat ("DWARF 2");
20740 get_builder ()->record_producer (producer);
20741
20742 processing_has_namespace_info = false;
20743
20744 return get_builder ()->get_compunit_symtab ();
20745 }
20746
20747 static void
20748 var_decode_location (struct attribute *attr, struct symbol *sym,
20749 struct dwarf2_cu *cu)
20750 {
20751 struct objfile *objfile = cu->per_objfile->objfile;
20752 struct comp_unit_head *cu_header = &cu->header;
20753
20754 /* NOTE drow/2003-01-30: There used to be a comment and some special
20755 code here to turn a symbol with DW_AT_external and a
20756 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20757 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20758 with some versions of binutils) where shared libraries could have
20759 relocations against symbols in their debug information - the
20760 minimal symbol would have the right address, but the debug info
20761 would not. It's no longer necessary, because we will explicitly
20762 apply relocations when we read in the debug information now. */
20763
20764 /* A DW_AT_location attribute with no contents indicates that a
20765 variable has been optimized away. */
20766 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20767 {
20768 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20769 return;
20770 }
20771
20772 /* Handle one degenerate form of location expression specially, to
20773 preserve GDB's previous behavior when section offsets are
20774 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20775 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20776
20777 if (attr->form_is_block ()
20778 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20779 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20780 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20781 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20782 && (DW_BLOCK (attr)->size
20783 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20784 {
20785 unsigned int dummy;
20786
20787 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20788 SET_SYMBOL_VALUE_ADDRESS
20789 (sym, cu->header.read_address (objfile->obfd,
20790 DW_BLOCK (attr)->data + 1,
20791 &dummy));
20792 else
20793 SET_SYMBOL_VALUE_ADDRESS
20794 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20795 &dummy));
20796 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20797 fixup_symbol_section (sym, objfile);
20798 SET_SYMBOL_VALUE_ADDRESS
20799 (sym,
20800 SYMBOL_VALUE_ADDRESS (sym)
20801 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20802 return;
20803 }
20804
20805 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20806 expression evaluator, and use LOC_COMPUTED only when necessary
20807 (i.e. when the value of a register or memory location is
20808 referenced, or a thread-local block, etc.). Then again, it might
20809 not be worthwhile. I'm assuming that it isn't unless performance
20810 or memory numbers show me otherwise. */
20811
20812 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20813
20814 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20815 cu->has_loclist = true;
20816 }
20817
20818 /* Given a pointer to a DWARF information entry, figure out if we need
20819 to make a symbol table entry for it, and if so, create a new entry
20820 and return a pointer to it.
20821 If TYPE is NULL, determine symbol type from the die, otherwise
20822 used the passed type.
20823 If SPACE is not NULL, use it to hold the new symbol. If it is
20824 NULL, allocate a new symbol on the objfile's obstack. */
20825
20826 static struct symbol *
20827 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20828 struct symbol *space)
20829 {
20830 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20831 struct objfile *objfile = per_objfile->objfile;
20832 struct gdbarch *gdbarch = objfile->arch ();
20833 struct symbol *sym = NULL;
20834 const char *name;
20835 struct attribute *attr = NULL;
20836 struct attribute *attr2 = NULL;
20837 CORE_ADDR baseaddr;
20838 struct pending **list_to_add = NULL;
20839
20840 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20841
20842 baseaddr = objfile->text_section_offset ();
20843
20844 name = dwarf2_name (die, cu);
20845 if (name)
20846 {
20847 int suppress_add = 0;
20848
20849 if (space)
20850 sym = space;
20851 else
20852 sym = new (&objfile->objfile_obstack) symbol;
20853 OBJSTAT (objfile, n_syms++);
20854
20855 /* Cache this symbol's name and the name's demangled form (if any). */
20856 sym->set_language (cu->language, &objfile->objfile_obstack);
20857 /* Fortran does not have mangling standard and the mangling does differ
20858 between gfortran, iFort etc. */
20859 const char *physname
20860 = (cu->language == language_fortran
20861 ? dwarf2_full_name (name, die, cu)
20862 : dwarf2_physname (name, die, cu));
20863 const char *linkagename = dw2_linkage_name (die, cu);
20864
20865 if (linkagename == nullptr || cu->language == language_ada)
20866 sym->set_linkage_name (physname);
20867 else
20868 {
20869 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20870 sym->set_linkage_name (linkagename);
20871 }
20872
20873 /* Default assumptions.
20874 Use the passed type or decode it from the die. */
20875 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20876 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20877 if (type != NULL)
20878 SYMBOL_TYPE (sym) = type;
20879 else
20880 SYMBOL_TYPE (sym) = die_type (die, cu);
20881 attr = dwarf2_attr (die,
20882 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20883 cu);
20884 if (attr != nullptr)
20885 {
20886 SYMBOL_LINE (sym) = DW_UNSND (attr);
20887 }
20888
20889 attr = dwarf2_attr (die,
20890 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20891 cu);
20892 if (attr != nullptr)
20893 {
20894 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20895 struct file_entry *fe;
20896
20897 if (cu->line_header != NULL)
20898 fe = cu->line_header->file_name_at (file_index);
20899 else
20900 fe = NULL;
20901
20902 if (fe == NULL)
20903 complaint (_("file index out of range"));
20904 else
20905 symbol_set_symtab (sym, fe->symtab);
20906 }
20907
20908 switch (die->tag)
20909 {
20910 case DW_TAG_label:
20911 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20912 if (attr != nullptr)
20913 {
20914 CORE_ADDR addr;
20915
20916 addr = attr->value_as_address ();
20917 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20918 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20919 }
20920 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20921 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20922 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20923 add_symbol_to_list (sym, cu->list_in_scope);
20924 break;
20925 case DW_TAG_subprogram:
20926 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20927 finish_block. */
20928 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20929 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20930 if ((attr2 && (DW_UNSND (attr2) != 0))
20931 || cu->language == language_ada
20932 || cu->language == language_fortran)
20933 {
20934 /* Subprograms marked external are stored as a global symbol.
20935 Ada and Fortran subprograms, whether marked external or
20936 not, are always stored as a global symbol, because we want
20937 to be able to access them globally. For instance, we want
20938 to be able to break on a nested subprogram without having
20939 to specify the context. */
20940 list_to_add = cu->get_builder ()->get_global_symbols ();
20941 }
20942 else
20943 {
20944 list_to_add = cu->list_in_scope;
20945 }
20946 break;
20947 case DW_TAG_inlined_subroutine:
20948 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20949 finish_block. */
20950 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20951 SYMBOL_INLINED (sym) = 1;
20952 list_to_add = cu->list_in_scope;
20953 break;
20954 case DW_TAG_template_value_param:
20955 suppress_add = 1;
20956 /* Fall through. */
20957 case DW_TAG_constant:
20958 case DW_TAG_variable:
20959 case DW_TAG_member:
20960 /* Compilation with minimal debug info may result in
20961 variables with missing type entries. Change the
20962 misleading `void' type to something sensible. */
20963 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
20964 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20965
20966 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20967 /* In the case of DW_TAG_member, we should only be called for
20968 static const members. */
20969 if (die->tag == DW_TAG_member)
20970 {
20971 /* dwarf2_add_field uses die_is_declaration,
20972 so we do the same. */
20973 gdb_assert (die_is_declaration (die, cu));
20974 gdb_assert (attr);
20975 }
20976 if (attr != nullptr)
20977 {
20978 dwarf2_const_value (attr, sym, cu);
20979 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20980 if (!suppress_add)
20981 {
20982 if (attr2 && (DW_UNSND (attr2) != 0))
20983 list_to_add = cu->get_builder ()->get_global_symbols ();
20984 else
20985 list_to_add = cu->list_in_scope;
20986 }
20987 break;
20988 }
20989 attr = dwarf2_attr (die, DW_AT_location, cu);
20990 if (attr != nullptr)
20991 {
20992 var_decode_location (attr, sym, cu);
20993 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20994
20995 /* Fortran explicitly imports any global symbols to the local
20996 scope by DW_TAG_common_block. */
20997 if (cu->language == language_fortran && die->parent
20998 && die->parent->tag == DW_TAG_common_block)
20999 attr2 = NULL;
21000
21001 if (SYMBOL_CLASS (sym) == LOC_STATIC
21002 && SYMBOL_VALUE_ADDRESS (sym) == 0
21003 && !per_objfile->per_bfd->has_section_at_zero)
21004 {
21005 /* When a static variable is eliminated by the linker,
21006 the corresponding debug information is not stripped
21007 out, but the variable address is set to null;
21008 do not add such variables into symbol table. */
21009 }
21010 else if (attr2 && (DW_UNSND (attr2) != 0))
21011 {
21012 if (SYMBOL_CLASS (sym) == LOC_STATIC
21013 && (objfile->flags & OBJF_MAINLINE) == 0
21014 && per_objfile->per_bfd->can_copy)
21015 {
21016 /* A global static variable might be subject to
21017 copy relocation. We first check for a local
21018 minsym, though, because maybe the symbol was
21019 marked hidden, in which case this would not
21020 apply. */
21021 bound_minimal_symbol found
21022 = (lookup_minimal_symbol_linkage
21023 (sym->linkage_name (), objfile));
21024 if (found.minsym != nullptr)
21025 sym->maybe_copied = 1;
21026 }
21027
21028 /* A variable with DW_AT_external is never static,
21029 but it may be block-scoped. */
21030 list_to_add
21031 = ((cu->list_in_scope
21032 == cu->get_builder ()->get_file_symbols ())
21033 ? cu->get_builder ()->get_global_symbols ()
21034 : cu->list_in_scope);
21035 }
21036 else
21037 list_to_add = cu->list_in_scope;
21038 }
21039 else
21040 {
21041 /* We do not know the address of this symbol.
21042 If it is an external symbol and we have type information
21043 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21044 The address of the variable will then be determined from
21045 the minimal symbol table whenever the variable is
21046 referenced. */
21047 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21048
21049 /* Fortran explicitly imports any global symbols to the local
21050 scope by DW_TAG_common_block. */
21051 if (cu->language == language_fortran && die->parent
21052 && die->parent->tag == DW_TAG_common_block)
21053 {
21054 /* SYMBOL_CLASS doesn't matter here because
21055 read_common_block is going to reset it. */
21056 if (!suppress_add)
21057 list_to_add = cu->list_in_scope;
21058 }
21059 else if (attr2 && (DW_UNSND (attr2) != 0)
21060 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21061 {
21062 /* A variable with DW_AT_external is never static, but it
21063 may be block-scoped. */
21064 list_to_add
21065 = ((cu->list_in_scope
21066 == cu->get_builder ()->get_file_symbols ())
21067 ? cu->get_builder ()->get_global_symbols ()
21068 : cu->list_in_scope);
21069
21070 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21071 }
21072 else if (!die_is_declaration (die, cu))
21073 {
21074 /* Use the default LOC_OPTIMIZED_OUT class. */
21075 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21076 if (!suppress_add)
21077 list_to_add = cu->list_in_scope;
21078 }
21079 }
21080 break;
21081 case DW_TAG_formal_parameter:
21082 {
21083 /* If we are inside a function, mark this as an argument. If
21084 not, we might be looking at an argument to an inlined function
21085 when we do not have enough information to show inlined frames;
21086 pretend it's a local variable in that case so that the user can
21087 still see it. */
21088 struct context_stack *curr
21089 = cu->get_builder ()->get_current_context_stack ();
21090 if (curr != nullptr && curr->name != nullptr)
21091 SYMBOL_IS_ARGUMENT (sym) = 1;
21092 attr = dwarf2_attr (die, DW_AT_location, cu);
21093 if (attr != nullptr)
21094 {
21095 var_decode_location (attr, sym, cu);
21096 }
21097 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21098 if (attr != nullptr)
21099 {
21100 dwarf2_const_value (attr, sym, cu);
21101 }
21102
21103 list_to_add = cu->list_in_scope;
21104 }
21105 break;
21106 case DW_TAG_unspecified_parameters:
21107 /* From varargs functions; gdb doesn't seem to have any
21108 interest in this information, so just ignore it for now.
21109 (FIXME?) */
21110 break;
21111 case DW_TAG_template_type_param:
21112 suppress_add = 1;
21113 /* Fall through. */
21114 case DW_TAG_class_type:
21115 case DW_TAG_interface_type:
21116 case DW_TAG_structure_type:
21117 case DW_TAG_union_type:
21118 case DW_TAG_set_type:
21119 case DW_TAG_enumeration_type:
21120 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21121 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21122
21123 {
21124 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21125 really ever be static objects: otherwise, if you try
21126 to, say, break of a class's method and you're in a file
21127 which doesn't mention that class, it won't work unless
21128 the check for all static symbols in lookup_symbol_aux
21129 saves you. See the OtherFileClass tests in
21130 gdb.c++/namespace.exp. */
21131
21132 if (!suppress_add)
21133 {
21134 buildsym_compunit *builder = cu->get_builder ();
21135 list_to_add
21136 = (cu->list_in_scope == builder->get_file_symbols ()
21137 && cu->language == language_cplus
21138 ? builder->get_global_symbols ()
21139 : cu->list_in_scope);
21140
21141 /* The semantics of C++ state that "struct foo {
21142 ... }" also defines a typedef for "foo". */
21143 if (cu->language == language_cplus
21144 || cu->language == language_ada
21145 || cu->language == language_d
21146 || cu->language == language_rust)
21147 {
21148 /* The symbol's name is already allocated along
21149 with this objfile, so we don't need to
21150 duplicate it for the type. */
21151 if (SYMBOL_TYPE (sym)->name () == 0)
21152 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21153 }
21154 }
21155 }
21156 break;
21157 case DW_TAG_typedef:
21158 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21159 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21160 list_to_add = cu->list_in_scope;
21161 break;
21162 case DW_TAG_base_type:
21163 case DW_TAG_subrange_type:
21164 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21165 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21166 list_to_add = cu->list_in_scope;
21167 break;
21168 case DW_TAG_enumerator:
21169 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21170 if (attr != nullptr)
21171 {
21172 dwarf2_const_value (attr, sym, cu);
21173 }
21174 {
21175 /* NOTE: carlton/2003-11-10: See comment above in the
21176 DW_TAG_class_type, etc. block. */
21177
21178 list_to_add
21179 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21180 && cu->language == language_cplus
21181 ? cu->get_builder ()->get_global_symbols ()
21182 : cu->list_in_scope);
21183 }
21184 break;
21185 case DW_TAG_imported_declaration:
21186 case DW_TAG_namespace:
21187 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21188 list_to_add = cu->get_builder ()->get_global_symbols ();
21189 break;
21190 case DW_TAG_module:
21191 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21192 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21193 list_to_add = cu->get_builder ()->get_global_symbols ();
21194 break;
21195 case DW_TAG_common_block:
21196 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21197 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21198 add_symbol_to_list (sym, cu->list_in_scope);
21199 break;
21200 default:
21201 /* Not a tag we recognize. Hopefully we aren't processing
21202 trash data, but since we must specifically ignore things
21203 we don't recognize, there is nothing else we should do at
21204 this point. */
21205 complaint (_("unsupported tag: '%s'"),
21206 dwarf_tag_name (die->tag));
21207 break;
21208 }
21209
21210 if (suppress_add)
21211 {
21212 sym->hash_next = objfile->template_symbols;
21213 objfile->template_symbols = sym;
21214 list_to_add = NULL;
21215 }
21216
21217 if (list_to_add != NULL)
21218 add_symbol_to_list (sym, list_to_add);
21219
21220 /* For the benefit of old versions of GCC, check for anonymous
21221 namespaces based on the demangled name. */
21222 if (!cu->processing_has_namespace_info
21223 && cu->language == language_cplus)
21224 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21225 }
21226 return (sym);
21227 }
21228
21229 /* Given an attr with a DW_FORM_dataN value in host byte order,
21230 zero-extend it as appropriate for the symbol's type. The DWARF
21231 standard (v4) is not entirely clear about the meaning of using
21232 DW_FORM_dataN for a constant with a signed type, where the type is
21233 wider than the data. The conclusion of a discussion on the DWARF
21234 list was that this is unspecified. We choose to always zero-extend
21235 because that is the interpretation long in use by GCC. */
21236
21237 static gdb_byte *
21238 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21239 struct dwarf2_cu *cu, LONGEST *value, int bits)
21240 {
21241 struct objfile *objfile = cu->per_objfile->objfile;
21242 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21243 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21244 LONGEST l = DW_UNSND (attr);
21245
21246 if (bits < sizeof (*value) * 8)
21247 {
21248 l &= ((LONGEST) 1 << bits) - 1;
21249 *value = l;
21250 }
21251 else if (bits == sizeof (*value) * 8)
21252 *value = l;
21253 else
21254 {
21255 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21256 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21257 return bytes;
21258 }
21259
21260 return NULL;
21261 }
21262
21263 /* Read a constant value from an attribute. Either set *VALUE, or if
21264 the value does not fit in *VALUE, set *BYTES - either already
21265 allocated on the objfile obstack, or newly allocated on OBSTACK,
21266 or, set *BATON, if we translated the constant to a location
21267 expression. */
21268
21269 static void
21270 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21271 const char *name, struct obstack *obstack,
21272 struct dwarf2_cu *cu,
21273 LONGEST *value, const gdb_byte **bytes,
21274 struct dwarf2_locexpr_baton **baton)
21275 {
21276 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21277 struct objfile *objfile = per_objfile->objfile;
21278 struct comp_unit_head *cu_header = &cu->header;
21279 struct dwarf_block *blk;
21280 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21281 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21282
21283 *value = 0;
21284 *bytes = NULL;
21285 *baton = NULL;
21286
21287 switch (attr->form)
21288 {
21289 case DW_FORM_addr:
21290 case DW_FORM_addrx:
21291 case DW_FORM_GNU_addr_index:
21292 {
21293 gdb_byte *data;
21294
21295 if (TYPE_LENGTH (type) != cu_header->addr_size)
21296 dwarf2_const_value_length_mismatch_complaint (name,
21297 cu_header->addr_size,
21298 TYPE_LENGTH (type));
21299 /* Symbols of this form are reasonably rare, so we just
21300 piggyback on the existing location code rather than writing
21301 a new implementation of symbol_computed_ops. */
21302 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21303 (*baton)->per_objfile = per_objfile;
21304 (*baton)->per_cu = cu->per_cu;
21305 gdb_assert ((*baton)->per_cu);
21306
21307 (*baton)->size = 2 + cu_header->addr_size;
21308 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21309 (*baton)->data = data;
21310
21311 data[0] = DW_OP_addr;
21312 store_unsigned_integer (&data[1], cu_header->addr_size,
21313 byte_order, DW_ADDR (attr));
21314 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21315 }
21316 break;
21317 case DW_FORM_string:
21318 case DW_FORM_strp:
21319 case DW_FORM_strx:
21320 case DW_FORM_GNU_str_index:
21321 case DW_FORM_GNU_strp_alt:
21322 /* DW_STRING is already allocated on the objfile obstack, point
21323 directly to it. */
21324 *bytes = (const gdb_byte *) DW_STRING (attr);
21325 break;
21326 case DW_FORM_block1:
21327 case DW_FORM_block2:
21328 case DW_FORM_block4:
21329 case DW_FORM_block:
21330 case DW_FORM_exprloc:
21331 case DW_FORM_data16:
21332 blk = DW_BLOCK (attr);
21333 if (TYPE_LENGTH (type) != blk->size)
21334 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21335 TYPE_LENGTH (type));
21336 *bytes = blk->data;
21337 break;
21338
21339 /* The DW_AT_const_value attributes are supposed to carry the
21340 symbol's value "represented as it would be on the target
21341 architecture." By the time we get here, it's already been
21342 converted to host endianness, so we just need to sign- or
21343 zero-extend it as appropriate. */
21344 case DW_FORM_data1:
21345 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21346 break;
21347 case DW_FORM_data2:
21348 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21349 break;
21350 case DW_FORM_data4:
21351 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21352 break;
21353 case DW_FORM_data8:
21354 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21355 break;
21356
21357 case DW_FORM_sdata:
21358 case DW_FORM_implicit_const:
21359 *value = DW_SND (attr);
21360 break;
21361
21362 case DW_FORM_udata:
21363 *value = DW_UNSND (attr);
21364 break;
21365
21366 default:
21367 complaint (_("unsupported const value attribute form: '%s'"),
21368 dwarf_form_name (attr->form));
21369 *value = 0;
21370 break;
21371 }
21372 }
21373
21374
21375 /* Copy constant value from an attribute to a symbol. */
21376
21377 static void
21378 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21379 struct dwarf2_cu *cu)
21380 {
21381 struct objfile *objfile = cu->per_objfile->objfile;
21382 LONGEST value;
21383 const gdb_byte *bytes;
21384 struct dwarf2_locexpr_baton *baton;
21385
21386 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21387 sym->print_name (),
21388 &objfile->objfile_obstack, cu,
21389 &value, &bytes, &baton);
21390
21391 if (baton != NULL)
21392 {
21393 SYMBOL_LOCATION_BATON (sym) = baton;
21394 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21395 }
21396 else if (bytes != NULL)
21397 {
21398 SYMBOL_VALUE_BYTES (sym) = bytes;
21399 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21400 }
21401 else
21402 {
21403 SYMBOL_VALUE (sym) = value;
21404 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21405 }
21406 }
21407
21408 /* Return the type of the die in question using its DW_AT_type attribute. */
21409
21410 static struct type *
21411 die_type (struct die_info *die, struct dwarf2_cu *cu)
21412 {
21413 struct attribute *type_attr;
21414
21415 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21416 if (!type_attr)
21417 {
21418 struct objfile *objfile = cu->per_objfile->objfile;
21419 /* A missing DW_AT_type represents a void type. */
21420 return objfile_type (objfile)->builtin_void;
21421 }
21422
21423 return lookup_die_type (die, type_attr, cu);
21424 }
21425
21426 /* True iff CU's producer generates GNAT Ada auxiliary information
21427 that allows to find parallel types through that information instead
21428 of having to do expensive parallel lookups by type name. */
21429
21430 static int
21431 need_gnat_info (struct dwarf2_cu *cu)
21432 {
21433 /* Assume that the Ada compiler was GNAT, which always produces
21434 the auxiliary information. */
21435 return (cu->language == language_ada);
21436 }
21437
21438 /* Return the auxiliary type of the die in question using its
21439 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21440 attribute is not present. */
21441
21442 static struct type *
21443 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21444 {
21445 struct attribute *type_attr;
21446
21447 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21448 if (!type_attr)
21449 return NULL;
21450
21451 return lookup_die_type (die, type_attr, cu);
21452 }
21453
21454 /* If DIE has a descriptive_type attribute, then set the TYPE's
21455 descriptive type accordingly. */
21456
21457 static void
21458 set_descriptive_type (struct type *type, struct die_info *die,
21459 struct dwarf2_cu *cu)
21460 {
21461 struct type *descriptive_type = die_descriptive_type (die, cu);
21462
21463 if (descriptive_type)
21464 {
21465 ALLOCATE_GNAT_AUX_TYPE (type);
21466 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21467 }
21468 }
21469
21470 /* Return the containing type of the die in question using its
21471 DW_AT_containing_type attribute. */
21472
21473 static struct type *
21474 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21475 {
21476 struct attribute *type_attr;
21477 struct objfile *objfile = cu->per_objfile->objfile;
21478
21479 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21480 if (!type_attr)
21481 error (_("Dwarf Error: Problem turning containing type into gdb type "
21482 "[in module %s]"), objfile_name (objfile));
21483
21484 return lookup_die_type (die, type_attr, cu);
21485 }
21486
21487 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21488
21489 static struct type *
21490 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21491 {
21492 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21493 struct objfile *objfile = per_objfile->objfile;
21494 char *saved;
21495
21496 std::string message
21497 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21498 objfile_name (objfile),
21499 sect_offset_str (cu->header.sect_off),
21500 sect_offset_str (die->sect_off));
21501 saved = obstack_strdup (&objfile->objfile_obstack, message);
21502
21503 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21504 }
21505
21506 /* Look up the type of DIE in CU using its type attribute ATTR.
21507 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21508 DW_AT_containing_type.
21509 If there is no type substitute an error marker. */
21510
21511 static struct type *
21512 lookup_die_type (struct die_info *die, const struct attribute *attr,
21513 struct dwarf2_cu *cu)
21514 {
21515 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21516 struct objfile *objfile = per_objfile->objfile;
21517 struct type *this_type;
21518
21519 gdb_assert (attr->name == DW_AT_type
21520 || attr->name == DW_AT_GNAT_descriptive_type
21521 || attr->name == DW_AT_containing_type);
21522
21523 /* First see if we have it cached. */
21524
21525 if (attr->form == DW_FORM_GNU_ref_alt)
21526 {
21527 struct dwarf2_per_cu_data *per_cu;
21528 sect_offset sect_off = attr->get_ref_die_offset ();
21529
21530 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
21531 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
21532 }
21533 else if (attr->form_is_ref ())
21534 {
21535 sect_offset sect_off = attr->get_ref_die_offset ();
21536
21537 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
21538 }
21539 else if (attr->form == DW_FORM_ref_sig8)
21540 {
21541 ULONGEST signature = DW_SIGNATURE (attr);
21542
21543 return get_signatured_type (die, signature, cu);
21544 }
21545 else
21546 {
21547 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21548 " at %s [in module %s]"),
21549 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21550 objfile_name (objfile));
21551 return build_error_marker_type (cu, die);
21552 }
21553
21554 /* If not cached we need to read it in. */
21555
21556 if (this_type == NULL)
21557 {
21558 struct die_info *type_die = NULL;
21559 struct dwarf2_cu *type_cu = cu;
21560
21561 if (attr->form_is_ref ())
21562 type_die = follow_die_ref (die, attr, &type_cu);
21563 if (type_die == NULL)
21564 return build_error_marker_type (cu, die);
21565 /* If we find the type now, it's probably because the type came
21566 from an inter-CU reference and the type's CU got expanded before
21567 ours. */
21568 this_type = read_type_die (type_die, type_cu);
21569 }
21570
21571 /* If we still don't have a type use an error marker. */
21572
21573 if (this_type == NULL)
21574 return build_error_marker_type (cu, die);
21575
21576 return this_type;
21577 }
21578
21579 /* Return the type in DIE, CU.
21580 Returns NULL for invalid types.
21581
21582 This first does a lookup in die_type_hash,
21583 and only reads the die in if necessary.
21584
21585 NOTE: This can be called when reading in partial or full symbols. */
21586
21587 static struct type *
21588 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21589 {
21590 struct type *this_type;
21591
21592 this_type = get_die_type (die, cu);
21593 if (this_type)
21594 return this_type;
21595
21596 return read_type_die_1 (die, cu);
21597 }
21598
21599 /* Read the type in DIE, CU.
21600 Returns NULL for invalid types. */
21601
21602 static struct type *
21603 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21604 {
21605 struct type *this_type = NULL;
21606
21607 switch (die->tag)
21608 {
21609 case DW_TAG_class_type:
21610 case DW_TAG_interface_type:
21611 case DW_TAG_structure_type:
21612 case DW_TAG_union_type:
21613 this_type = read_structure_type (die, cu);
21614 break;
21615 case DW_TAG_enumeration_type:
21616 this_type = read_enumeration_type (die, cu);
21617 break;
21618 case DW_TAG_subprogram:
21619 case DW_TAG_subroutine_type:
21620 case DW_TAG_inlined_subroutine:
21621 this_type = read_subroutine_type (die, cu);
21622 break;
21623 case DW_TAG_array_type:
21624 this_type = read_array_type (die, cu);
21625 break;
21626 case DW_TAG_set_type:
21627 this_type = read_set_type (die, cu);
21628 break;
21629 case DW_TAG_pointer_type:
21630 this_type = read_tag_pointer_type (die, cu);
21631 break;
21632 case DW_TAG_ptr_to_member_type:
21633 this_type = read_tag_ptr_to_member_type (die, cu);
21634 break;
21635 case DW_TAG_reference_type:
21636 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21637 break;
21638 case DW_TAG_rvalue_reference_type:
21639 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21640 break;
21641 case DW_TAG_const_type:
21642 this_type = read_tag_const_type (die, cu);
21643 break;
21644 case DW_TAG_volatile_type:
21645 this_type = read_tag_volatile_type (die, cu);
21646 break;
21647 case DW_TAG_restrict_type:
21648 this_type = read_tag_restrict_type (die, cu);
21649 break;
21650 case DW_TAG_string_type:
21651 this_type = read_tag_string_type (die, cu);
21652 break;
21653 case DW_TAG_typedef:
21654 this_type = read_typedef (die, cu);
21655 break;
21656 case DW_TAG_subrange_type:
21657 this_type = read_subrange_type (die, cu);
21658 break;
21659 case DW_TAG_base_type:
21660 this_type = read_base_type (die, cu);
21661 break;
21662 case DW_TAG_unspecified_type:
21663 this_type = read_unspecified_type (die, cu);
21664 break;
21665 case DW_TAG_namespace:
21666 this_type = read_namespace_type (die, cu);
21667 break;
21668 case DW_TAG_module:
21669 this_type = read_module_type (die, cu);
21670 break;
21671 case DW_TAG_atomic_type:
21672 this_type = read_tag_atomic_type (die, cu);
21673 break;
21674 default:
21675 complaint (_("unexpected tag in read_type_die: '%s'"),
21676 dwarf_tag_name (die->tag));
21677 break;
21678 }
21679
21680 return this_type;
21681 }
21682
21683 /* See if we can figure out if the class lives in a namespace. We do
21684 this by looking for a member function; its demangled name will
21685 contain namespace info, if there is any.
21686 Return the computed name or NULL.
21687 Space for the result is allocated on the objfile's obstack.
21688 This is the full-die version of guess_partial_die_structure_name.
21689 In this case we know DIE has no useful parent. */
21690
21691 static const char *
21692 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21693 {
21694 struct die_info *spec_die;
21695 struct dwarf2_cu *spec_cu;
21696 struct die_info *child;
21697 struct objfile *objfile = cu->per_objfile->objfile;
21698
21699 spec_cu = cu;
21700 spec_die = die_specification (die, &spec_cu);
21701 if (spec_die != NULL)
21702 {
21703 die = spec_die;
21704 cu = spec_cu;
21705 }
21706
21707 for (child = die->child;
21708 child != NULL;
21709 child = child->sibling)
21710 {
21711 if (child->tag == DW_TAG_subprogram)
21712 {
21713 const char *linkage_name = dw2_linkage_name (child, cu);
21714
21715 if (linkage_name != NULL)
21716 {
21717 gdb::unique_xmalloc_ptr<char> actual_name
21718 (cu->language_defn->class_name_from_physname (linkage_name));
21719 const char *name = NULL;
21720
21721 if (actual_name != NULL)
21722 {
21723 const char *die_name = dwarf2_name (die, cu);
21724
21725 if (die_name != NULL
21726 && strcmp (die_name, actual_name.get ()) != 0)
21727 {
21728 /* Strip off the class name from the full name.
21729 We want the prefix. */
21730 int die_name_len = strlen (die_name);
21731 int actual_name_len = strlen (actual_name.get ());
21732 const char *ptr = actual_name.get ();
21733
21734 /* Test for '::' as a sanity check. */
21735 if (actual_name_len > die_name_len + 2
21736 && ptr[actual_name_len - die_name_len - 1] == ':')
21737 name = obstack_strndup (
21738 &objfile->per_bfd->storage_obstack,
21739 ptr, actual_name_len - die_name_len - 2);
21740 }
21741 }
21742 return name;
21743 }
21744 }
21745 }
21746
21747 return NULL;
21748 }
21749
21750 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21751 prefix part in such case. See
21752 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21753
21754 static const char *
21755 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21756 {
21757 struct attribute *attr;
21758 const char *base;
21759
21760 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21761 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21762 return NULL;
21763
21764 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21765 return NULL;
21766
21767 attr = dw2_linkage_name_attr (die, cu);
21768 if (attr == NULL || DW_STRING (attr) == NULL)
21769 return NULL;
21770
21771 /* dwarf2_name had to be already called. */
21772 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21773
21774 /* Strip the base name, keep any leading namespaces/classes. */
21775 base = strrchr (DW_STRING (attr), ':');
21776 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21777 return "";
21778
21779 struct objfile *objfile = cu->per_objfile->objfile;
21780 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21781 DW_STRING (attr),
21782 &base[-1] - DW_STRING (attr));
21783 }
21784
21785 /* Return the name of the namespace/class that DIE is defined within,
21786 or "" if we can't tell. The caller should not xfree the result.
21787
21788 For example, if we're within the method foo() in the following
21789 code:
21790
21791 namespace N {
21792 class C {
21793 void foo () {
21794 }
21795 };
21796 }
21797
21798 then determine_prefix on foo's die will return "N::C". */
21799
21800 static const char *
21801 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21802 {
21803 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21804 struct die_info *parent, *spec_die;
21805 struct dwarf2_cu *spec_cu;
21806 struct type *parent_type;
21807 const char *retval;
21808
21809 if (cu->language != language_cplus
21810 && cu->language != language_fortran && cu->language != language_d
21811 && cu->language != language_rust)
21812 return "";
21813
21814 retval = anonymous_struct_prefix (die, cu);
21815 if (retval)
21816 return retval;
21817
21818 /* We have to be careful in the presence of DW_AT_specification.
21819 For example, with GCC 3.4, given the code
21820
21821 namespace N {
21822 void foo() {
21823 // Definition of N::foo.
21824 }
21825 }
21826
21827 then we'll have a tree of DIEs like this:
21828
21829 1: DW_TAG_compile_unit
21830 2: DW_TAG_namespace // N
21831 3: DW_TAG_subprogram // declaration of N::foo
21832 4: DW_TAG_subprogram // definition of N::foo
21833 DW_AT_specification // refers to die #3
21834
21835 Thus, when processing die #4, we have to pretend that we're in
21836 the context of its DW_AT_specification, namely the contex of die
21837 #3. */
21838 spec_cu = cu;
21839 spec_die = die_specification (die, &spec_cu);
21840 if (spec_die == NULL)
21841 parent = die->parent;
21842 else
21843 {
21844 parent = spec_die->parent;
21845 cu = spec_cu;
21846 }
21847
21848 if (parent == NULL)
21849 return "";
21850 else if (parent->building_fullname)
21851 {
21852 const char *name;
21853 const char *parent_name;
21854
21855 /* It has been seen on RealView 2.2 built binaries,
21856 DW_TAG_template_type_param types actually _defined_ as
21857 children of the parent class:
21858
21859 enum E {};
21860 template class <class Enum> Class{};
21861 Class<enum E> class_e;
21862
21863 1: DW_TAG_class_type (Class)
21864 2: DW_TAG_enumeration_type (E)
21865 3: DW_TAG_enumerator (enum1:0)
21866 3: DW_TAG_enumerator (enum2:1)
21867 ...
21868 2: DW_TAG_template_type_param
21869 DW_AT_type DW_FORM_ref_udata (E)
21870
21871 Besides being broken debug info, it can put GDB into an
21872 infinite loop. Consider:
21873
21874 When we're building the full name for Class<E>, we'll start
21875 at Class, and go look over its template type parameters,
21876 finding E. We'll then try to build the full name of E, and
21877 reach here. We're now trying to build the full name of E,
21878 and look over the parent DIE for containing scope. In the
21879 broken case, if we followed the parent DIE of E, we'd again
21880 find Class, and once again go look at its template type
21881 arguments, etc., etc. Simply don't consider such parent die
21882 as source-level parent of this die (it can't be, the language
21883 doesn't allow it), and break the loop here. */
21884 name = dwarf2_name (die, cu);
21885 parent_name = dwarf2_name (parent, cu);
21886 complaint (_("template param type '%s' defined within parent '%s'"),
21887 name ? name : "<unknown>",
21888 parent_name ? parent_name : "<unknown>");
21889 return "";
21890 }
21891 else
21892 switch (parent->tag)
21893 {
21894 case DW_TAG_namespace:
21895 parent_type = read_type_die (parent, cu);
21896 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21897 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21898 Work around this problem here. */
21899 if (cu->language == language_cplus
21900 && strcmp (parent_type->name (), "::") == 0)
21901 return "";
21902 /* We give a name to even anonymous namespaces. */
21903 return parent_type->name ();
21904 case DW_TAG_class_type:
21905 case DW_TAG_interface_type:
21906 case DW_TAG_structure_type:
21907 case DW_TAG_union_type:
21908 case DW_TAG_module:
21909 parent_type = read_type_die (parent, cu);
21910 if (parent_type->name () != NULL)
21911 return parent_type->name ();
21912 else
21913 /* An anonymous structure is only allowed non-static data
21914 members; no typedefs, no member functions, et cetera.
21915 So it does not need a prefix. */
21916 return "";
21917 case DW_TAG_compile_unit:
21918 case DW_TAG_partial_unit:
21919 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21920 if (cu->language == language_cplus
21921 && !per_objfile->per_bfd->types.empty ()
21922 && die->child != NULL
21923 && (die->tag == DW_TAG_class_type
21924 || die->tag == DW_TAG_structure_type
21925 || die->tag == DW_TAG_union_type))
21926 {
21927 const char *name = guess_full_die_structure_name (die, cu);
21928 if (name != NULL)
21929 return name;
21930 }
21931 return "";
21932 case DW_TAG_subprogram:
21933 /* Nested subroutines in Fortran get a prefix with the name
21934 of the parent's subroutine. */
21935 if (cu->language == language_fortran)
21936 {
21937 if ((die->tag == DW_TAG_subprogram)
21938 && (dwarf2_name (parent, cu) != NULL))
21939 return dwarf2_name (parent, cu);
21940 }
21941 return determine_prefix (parent, cu);
21942 case DW_TAG_enumeration_type:
21943 parent_type = read_type_die (parent, cu);
21944 if (TYPE_DECLARED_CLASS (parent_type))
21945 {
21946 if (parent_type->name () != NULL)
21947 return parent_type->name ();
21948 return "";
21949 }
21950 /* Fall through. */
21951 default:
21952 return determine_prefix (parent, cu);
21953 }
21954 }
21955
21956 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21957 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21958 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21959 an obconcat, otherwise allocate storage for the result. The CU argument is
21960 used to determine the language and hence, the appropriate separator. */
21961
21962 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21963
21964 static char *
21965 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21966 int physname, struct dwarf2_cu *cu)
21967 {
21968 const char *lead = "";
21969 const char *sep;
21970
21971 if (suffix == NULL || suffix[0] == '\0'
21972 || prefix == NULL || prefix[0] == '\0')
21973 sep = "";
21974 else if (cu->language == language_d)
21975 {
21976 /* For D, the 'main' function could be defined in any module, but it
21977 should never be prefixed. */
21978 if (strcmp (suffix, "D main") == 0)
21979 {
21980 prefix = "";
21981 sep = "";
21982 }
21983 else
21984 sep = ".";
21985 }
21986 else if (cu->language == language_fortran && physname)
21987 {
21988 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21989 DW_AT_MIPS_linkage_name is preferred and used instead. */
21990
21991 lead = "__";
21992 sep = "_MOD_";
21993 }
21994 else
21995 sep = "::";
21996
21997 if (prefix == NULL)
21998 prefix = "";
21999 if (suffix == NULL)
22000 suffix = "";
22001
22002 if (obs == NULL)
22003 {
22004 char *retval
22005 = ((char *)
22006 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22007
22008 strcpy (retval, lead);
22009 strcat (retval, prefix);
22010 strcat (retval, sep);
22011 strcat (retval, suffix);
22012 return retval;
22013 }
22014 else
22015 {
22016 /* We have an obstack. */
22017 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22018 }
22019 }
22020
22021 /* Get name of a die, return NULL if not found. */
22022
22023 static const char *
22024 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22025 struct objfile *objfile)
22026 {
22027 if (name && cu->language == language_cplus)
22028 {
22029 gdb::unique_xmalloc_ptr<char> canon_name
22030 = cp_canonicalize_string (name);
22031
22032 if (canon_name != nullptr)
22033 name = objfile->intern (canon_name.get ());
22034 }
22035
22036 return name;
22037 }
22038
22039 /* Get name of a die, return NULL if not found.
22040 Anonymous namespaces are converted to their magic string. */
22041
22042 static const char *
22043 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22044 {
22045 struct attribute *attr;
22046 struct objfile *objfile = cu->per_objfile->objfile;
22047
22048 attr = dwarf2_attr (die, DW_AT_name, cu);
22049 if ((!attr || !DW_STRING (attr))
22050 && die->tag != DW_TAG_namespace
22051 && die->tag != DW_TAG_class_type
22052 && die->tag != DW_TAG_interface_type
22053 && die->tag != DW_TAG_structure_type
22054 && die->tag != DW_TAG_union_type)
22055 return NULL;
22056
22057 switch (die->tag)
22058 {
22059 case DW_TAG_compile_unit:
22060 case DW_TAG_partial_unit:
22061 /* Compilation units have a DW_AT_name that is a filename, not
22062 a source language identifier. */
22063 case DW_TAG_enumeration_type:
22064 case DW_TAG_enumerator:
22065 /* These tags always have simple identifiers already; no need
22066 to canonicalize them. */
22067 return DW_STRING (attr);
22068
22069 case DW_TAG_namespace:
22070 if (attr != NULL && DW_STRING (attr) != NULL)
22071 return DW_STRING (attr);
22072 return CP_ANONYMOUS_NAMESPACE_STR;
22073
22074 case DW_TAG_class_type:
22075 case DW_TAG_interface_type:
22076 case DW_TAG_structure_type:
22077 case DW_TAG_union_type:
22078 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22079 structures or unions. These were of the form "._%d" in GCC 4.1,
22080 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22081 and GCC 4.4. We work around this problem by ignoring these. */
22082 if (attr && DW_STRING (attr)
22083 && (startswith (DW_STRING (attr), "._")
22084 || startswith (DW_STRING (attr), "<anonymous")))
22085 return NULL;
22086
22087 /* GCC might emit a nameless typedef that has a linkage name. See
22088 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22089 if (!attr || DW_STRING (attr) == NULL)
22090 {
22091 attr = dw2_linkage_name_attr (die, cu);
22092 if (attr == NULL || DW_STRING (attr) == NULL)
22093 return NULL;
22094
22095 /* Avoid demangling DW_STRING (attr) the second time on a second
22096 call for the same DIE. */
22097 if (!DW_STRING_IS_CANONICAL (attr))
22098 {
22099 gdb::unique_xmalloc_ptr<char> demangled
22100 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
22101 if (demangled == nullptr)
22102 return nullptr;
22103
22104 DW_STRING (attr) = objfile->intern (demangled.get ());
22105 DW_STRING_IS_CANONICAL (attr) = 1;
22106 }
22107
22108 /* Strip any leading namespaces/classes, keep only the base name.
22109 DW_AT_name for named DIEs does not contain the prefixes. */
22110 const char *base = strrchr (DW_STRING (attr), ':');
22111 if (base && base > DW_STRING (attr) && base[-1] == ':')
22112 return &base[1];
22113 else
22114 return DW_STRING (attr);
22115 }
22116 break;
22117
22118 default:
22119 break;
22120 }
22121
22122 if (!DW_STRING_IS_CANONICAL (attr))
22123 {
22124 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22125 objfile);
22126 DW_STRING_IS_CANONICAL (attr) = 1;
22127 }
22128 return DW_STRING (attr);
22129 }
22130
22131 /* Return the die that this die in an extension of, or NULL if there
22132 is none. *EXT_CU is the CU containing DIE on input, and the CU
22133 containing the return value on output. */
22134
22135 static struct die_info *
22136 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22137 {
22138 struct attribute *attr;
22139
22140 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22141 if (attr == NULL)
22142 return NULL;
22143
22144 return follow_die_ref (die, attr, ext_cu);
22145 }
22146
22147 static void
22148 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22149 {
22150 unsigned int i;
22151
22152 print_spaces (indent, f);
22153 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22154 dwarf_tag_name (die->tag), die->abbrev,
22155 sect_offset_str (die->sect_off));
22156
22157 if (die->parent != NULL)
22158 {
22159 print_spaces (indent, f);
22160 fprintf_unfiltered (f, " parent at offset: %s\n",
22161 sect_offset_str (die->parent->sect_off));
22162 }
22163
22164 print_spaces (indent, f);
22165 fprintf_unfiltered (f, " has children: %s\n",
22166 dwarf_bool_name (die->child != NULL));
22167
22168 print_spaces (indent, f);
22169 fprintf_unfiltered (f, " attributes:\n");
22170
22171 for (i = 0; i < die->num_attrs; ++i)
22172 {
22173 print_spaces (indent, f);
22174 fprintf_unfiltered (f, " %s (%s) ",
22175 dwarf_attr_name (die->attrs[i].name),
22176 dwarf_form_name (die->attrs[i].form));
22177
22178 switch (die->attrs[i].form)
22179 {
22180 case DW_FORM_addr:
22181 case DW_FORM_addrx:
22182 case DW_FORM_GNU_addr_index:
22183 fprintf_unfiltered (f, "address: ");
22184 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22185 break;
22186 case DW_FORM_block2:
22187 case DW_FORM_block4:
22188 case DW_FORM_block:
22189 case DW_FORM_block1:
22190 fprintf_unfiltered (f, "block: size %s",
22191 pulongest (DW_BLOCK (&die->attrs[i])->size));
22192 break;
22193 case DW_FORM_exprloc:
22194 fprintf_unfiltered (f, "expression: size %s",
22195 pulongest (DW_BLOCK (&die->attrs[i])->size));
22196 break;
22197 case DW_FORM_data16:
22198 fprintf_unfiltered (f, "constant of 16 bytes");
22199 break;
22200 case DW_FORM_ref_addr:
22201 fprintf_unfiltered (f, "ref address: ");
22202 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22203 break;
22204 case DW_FORM_GNU_ref_alt:
22205 fprintf_unfiltered (f, "alt ref address: ");
22206 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22207 break;
22208 case DW_FORM_ref1:
22209 case DW_FORM_ref2:
22210 case DW_FORM_ref4:
22211 case DW_FORM_ref8:
22212 case DW_FORM_ref_udata:
22213 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22214 (long) (DW_UNSND (&die->attrs[i])));
22215 break;
22216 case DW_FORM_data1:
22217 case DW_FORM_data2:
22218 case DW_FORM_data4:
22219 case DW_FORM_data8:
22220 case DW_FORM_udata:
22221 case DW_FORM_sdata:
22222 fprintf_unfiltered (f, "constant: %s",
22223 pulongest (DW_UNSND (&die->attrs[i])));
22224 break;
22225 case DW_FORM_sec_offset:
22226 fprintf_unfiltered (f, "section offset: %s",
22227 pulongest (DW_UNSND (&die->attrs[i])));
22228 break;
22229 case DW_FORM_ref_sig8:
22230 fprintf_unfiltered (f, "signature: %s",
22231 hex_string (DW_SIGNATURE (&die->attrs[i])));
22232 break;
22233 case DW_FORM_string:
22234 case DW_FORM_strp:
22235 case DW_FORM_line_strp:
22236 case DW_FORM_strx:
22237 case DW_FORM_GNU_str_index:
22238 case DW_FORM_GNU_strp_alt:
22239 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22240 DW_STRING (&die->attrs[i])
22241 ? DW_STRING (&die->attrs[i]) : "",
22242 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22243 break;
22244 case DW_FORM_flag:
22245 if (DW_UNSND (&die->attrs[i]))
22246 fprintf_unfiltered (f, "flag: TRUE");
22247 else
22248 fprintf_unfiltered (f, "flag: FALSE");
22249 break;
22250 case DW_FORM_flag_present:
22251 fprintf_unfiltered (f, "flag: TRUE");
22252 break;
22253 case DW_FORM_indirect:
22254 /* The reader will have reduced the indirect form to
22255 the "base form" so this form should not occur. */
22256 fprintf_unfiltered (f,
22257 "unexpected attribute form: DW_FORM_indirect");
22258 break;
22259 case DW_FORM_implicit_const:
22260 fprintf_unfiltered (f, "constant: %s",
22261 plongest (DW_SND (&die->attrs[i])));
22262 break;
22263 default:
22264 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22265 die->attrs[i].form);
22266 break;
22267 }
22268 fprintf_unfiltered (f, "\n");
22269 }
22270 }
22271
22272 static void
22273 dump_die_for_error (struct die_info *die)
22274 {
22275 dump_die_shallow (gdb_stderr, 0, die);
22276 }
22277
22278 static void
22279 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22280 {
22281 int indent = level * 4;
22282
22283 gdb_assert (die != NULL);
22284
22285 if (level >= max_level)
22286 return;
22287
22288 dump_die_shallow (f, indent, die);
22289
22290 if (die->child != NULL)
22291 {
22292 print_spaces (indent, f);
22293 fprintf_unfiltered (f, " Children:");
22294 if (level + 1 < max_level)
22295 {
22296 fprintf_unfiltered (f, "\n");
22297 dump_die_1 (f, level + 1, max_level, die->child);
22298 }
22299 else
22300 {
22301 fprintf_unfiltered (f,
22302 " [not printed, max nesting level reached]\n");
22303 }
22304 }
22305
22306 if (die->sibling != NULL && level > 0)
22307 {
22308 dump_die_1 (f, level, max_level, die->sibling);
22309 }
22310 }
22311
22312 /* This is called from the pdie macro in gdbinit.in.
22313 It's not static so gcc will keep a copy callable from gdb. */
22314
22315 void
22316 dump_die (struct die_info *die, int max_level)
22317 {
22318 dump_die_1 (gdb_stdlog, 0, max_level, die);
22319 }
22320
22321 static void
22322 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22323 {
22324 void **slot;
22325
22326 slot = htab_find_slot_with_hash (cu->die_hash, die,
22327 to_underlying (die->sect_off),
22328 INSERT);
22329
22330 *slot = die;
22331 }
22332
22333 /* Follow reference or signature attribute ATTR of SRC_DIE.
22334 On entry *REF_CU is the CU of SRC_DIE.
22335 On exit *REF_CU is the CU of the result. */
22336
22337 static struct die_info *
22338 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22339 struct dwarf2_cu **ref_cu)
22340 {
22341 struct die_info *die;
22342
22343 if (attr->form_is_ref ())
22344 die = follow_die_ref (src_die, attr, ref_cu);
22345 else if (attr->form == DW_FORM_ref_sig8)
22346 die = follow_die_sig (src_die, attr, ref_cu);
22347 else
22348 {
22349 dump_die_for_error (src_die);
22350 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22351 objfile_name ((*ref_cu)->per_objfile->objfile));
22352 }
22353
22354 return die;
22355 }
22356
22357 /* Follow reference OFFSET.
22358 On entry *REF_CU is the CU of the source die referencing OFFSET.
22359 On exit *REF_CU is the CU of the result.
22360 Returns NULL if OFFSET is invalid. */
22361
22362 static struct die_info *
22363 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22364 struct dwarf2_cu **ref_cu)
22365 {
22366 struct die_info temp_die;
22367 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22368 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22369
22370 gdb_assert (cu->per_cu != NULL);
22371
22372 target_cu = cu;
22373
22374 if (cu->per_cu->is_debug_types)
22375 {
22376 /* .debug_types CUs cannot reference anything outside their CU.
22377 If they need to, they have to reference a signatured type via
22378 DW_FORM_ref_sig8. */
22379 if (!cu->header.offset_in_cu_p (sect_off))
22380 return NULL;
22381 }
22382 else if (offset_in_dwz != cu->per_cu->is_dwz
22383 || !cu->header.offset_in_cu_p (sect_off))
22384 {
22385 struct dwarf2_per_cu_data *per_cu;
22386
22387 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22388 per_objfile);
22389
22390 /* If necessary, add it to the queue and load its DIEs. */
22391 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
22392 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
22393
22394 target_cu = per_objfile->get_cu (per_cu);
22395 }
22396 else if (cu->dies == NULL)
22397 {
22398 /* We're loading full DIEs during partial symbol reading. */
22399 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
22400 load_full_comp_unit (cu->per_cu, per_objfile, false, language_minimal);
22401 }
22402
22403 *ref_cu = target_cu;
22404 temp_die.sect_off = sect_off;
22405
22406 if (target_cu != cu)
22407 target_cu->ancestor = cu;
22408
22409 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22410 &temp_die,
22411 to_underlying (sect_off));
22412 }
22413
22414 /* Follow reference attribute ATTR of SRC_DIE.
22415 On entry *REF_CU is the CU of SRC_DIE.
22416 On exit *REF_CU is the CU of the result. */
22417
22418 static struct die_info *
22419 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22420 struct dwarf2_cu **ref_cu)
22421 {
22422 sect_offset sect_off = attr->get_ref_die_offset ();
22423 struct dwarf2_cu *cu = *ref_cu;
22424 struct die_info *die;
22425
22426 die = follow_die_offset (sect_off,
22427 (attr->form == DW_FORM_GNU_ref_alt
22428 || cu->per_cu->is_dwz),
22429 ref_cu);
22430 if (!die)
22431 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22432 "at %s [in module %s]"),
22433 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22434 objfile_name (cu->per_objfile->objfile));
22435
22436 return die;
22437 }
22438
22439 /* See read.h. */
22440
22441 struct dwarf2_locexpr_baton
22442 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22443 dwarf2_per_cu_data *per_cu,
22444 dwarf2_per_objfile *per_objfile,
22445 CORE_ADDR (*get_frame_pc) (void *baton),
22446 void *baton, bool resolve_abstract_p)
22447 {
22448 struct die_info *die;
22449 struct attribute *attr;
22450 struct dwarf2_locexpr_baton retval;
22451 struct objfile *objfile = per_objfile->objfile;
22452
22453 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22454 if (cu == nullptr)
22455 cu = load_cu (per_cu, per_objfile, false);
22456
22457 if (cu == nullptr)
22458 {
22459 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22460 Instead just throw an error, not much else we can do. */
22461 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22462 sect_offset_str (sect_off), objfile_name (objfile));
22463 }
22464
22465 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22466 if (!die)
22467 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22468 sect_offset_str (sect_off), objfile_name (objfile));
22469
22470 attr = dwarf2_attr (die, DW_AT_location, cu);
22471 if (!attr && resolve_abstract_p
22472 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22473 != per_objfile->per_bfd->abstract_to_concrete.end ()))
22474 {
22475 CORE_ADDR pc = (*get_frame_pc) (baton);
22476 CORE_ADDR baseaddr = objfile->text_section_offset ();
22477 struct gdbarch *gdbarch = objfile->arch ();
22478
22479 for (const auto &cand_off
22480 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22481 {
22482 struct dwarf2_cu *cand_cu = cu;
22483 struct die_info *cand
22484 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22485 if (!cand
22486 || !cand->parent
22487 || cand->parent->tag != DW_TAG_subprogram)
22488 continue;
22489
22490 CORE_ADDR pc_low, pc_high;
22491 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22492 if (pc_low == ((CORE_ADDR) -1))
22493 continue;
22494 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22495 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22496 if (!(pc_low <= pc && pc < pc_high))
22497 continue;
22498
22499 die = cand;
22500 attr = dwarf2_attr (die, DW_AT_location, cu);
22501 break;
22502 }
22503 }
22504
22505 if (!attr)
22506 {
22507 /* DWARF: "If there is no such attribute, then there is no effect.".
22508 DATA is ignored if SIZE is 0. */
22509
22510 retval.data = NULL;
22511 retval.size = 0;
22512 }
22513 else if (attr->form_is_section_offset ())
22514 {
22515 struct dwarf2_loclist_baton loclist_baton;
22516 CORE_ADDR pc = (*get_frame_pc) (baton);
22517 size_t size;
22518
22519 fill_in_loclist_baton (cu, &loclist_baton, attr);
22520
22521 retval.data = dwarf2_find_location_expression (&loclist_baton,
22522 &size, pc);
22523 retval.size = size;
22524 }
22525 else
22526 {
22527 if (!attr->form_is_block ())
22528 error (_("Dwarf Error: DIE at %s referenced in module %s "
22529 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22530 sect_offset_str (sect_off), objfile_name (objfile));
22531
22532 retval.data = DW_BLOCK (attr)->data;
22533 retval.size = DW_BLOCK (attr)->size;
22534 }
22535 retval.per_objfile = per_objfile;
22536 retval.per_cu = cu->per_cu;
22537
22538 per_objfile->age_comp_units ();
22539
22540 return retval;
22541 }
22542
22543 /* See read.h. */
22544
22545 struct dwarf2_locexpr_baton
22546 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22547 dwarf2_per_cu_data *per_cu,
22548 dwarf2_per_objfile *per_objfile,
22549 CORE_ADDR (*get_frame_pc) (void *baton),
22550 void *baton)
22551 {
22552 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22553
22554 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22555 get_frame_pc, baton);
22556 }
22557
22558 /* Write a constant of a given type as target-ordered bytes into
22559 OBSTACK. */
22560
22561 static const gdb_byte *
22562 write_constant_as_bytes (struct obstack *obstack,
22563 enum bfd_endian byte_order,
22564 struct type *type,
22565 ULONGEST value,
22566 LONGEST *len)
22567 {
22568 gdb_byte *result;
22569
22570 *len = TYPE_LENGTH (type);
22571 result = (gdb_byte *) obstack_alloc (obstack, *len);
22572 store_unsigned_integer (result, *len, byte_order, value);
22573
22574 return result;
22575 }
22576
22577 /* See read.h. */
22578
22579 const gdb_byte *
22580 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22581 dwarf2_per_cu_data *per_cu,
22582 dwarf2_per_objfile *per_objfile,
22583 obstack *obstack,
22584 LONGEST *len)
22585 {
22586 struct die_info *die;
22587 struct attribute *attr;
22588 const gdb_byte *result = NULL;
22589 struct type *type;
22590 LONGEST value;
22591 enum bfd_endian byte_order;
22592 struct objfile *objfile = per_objfile->objfile;
22593
22594 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22595 if (cu == nullptr)
22596 cu = load_cu (per_cu, per_objfile, false);
22597
22598 if (cu == nullptr)
22599 {
22600 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22601 Instead just throw an error, not much else we can do. */
22602 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22603 sect_offset_str (sect_off), objfile_name (objfile));
22604 }
22605
22606 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22607 if (!die)
22608 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22609 sect_offset_str (sect_off), objfile_name (objfile));
22610
22611 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22612 if (attr == NULL)
22613 return NULL;
22614
22615 byte_order = (bfd_big_endian (objfile->obfd)
22616 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22617
22618 switch (attr->form)
22619 {
22620 case DW_FORM_addr:
22621 case DW_FORM_addrx:
22622 case DW_FORM_GNU_addr_index:
22623 {
22624 gdb_byte *tem;
22625
22626 *len = cu->header.addr_size;
22627 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22628 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22629 result = tem;
22630 }
22631 break;
22632 case DW_FORM_string:
22633 case DW_FORM_strp:
22634 case DW_FORM_strx:
22635 case DW_FORM_GNU_str_index:
22636 case DW_FORM_GNU_strp_alt:
22637 /* DW_STRING is already allocated on the objfile obstack, point
22638 directly to it. */
22639 result = (const gdb_byte *) DW_STRING (attr);
22640 *len = strlen (DW_STRING (attr));
22641 break;
22642 case DW_FORM_block1:
22643 case DW_FORM_block2:
22644 case DW_FORM_block4:
22645 case DW_FORM_block:
22646 case DW_FORM_exprloc:
22647 case DW_FORM_data16:
22648 result = DW_BLOCK (attr)->data;
22649 *len = DW_BLOCK (attr)->size;
22650 break;
22651
22652 /* The DW_AT_const_value attributes are supposed to carry the
22653 symbol's value "represented as it would be on the target
22654 architecture." By the time we get here, it's already been
22655 converted to host endianness, so we just need to sign- or
22656 zero-extend it as appropriate. */
22657 case DW_FORM_data1:
22658 type = die_type (die, cu);
22659 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22660 if (result == NULL)
22661 result = write_constant_as_bytes (obstack, byte_order,
22662 type, value, len);
22663 break;
22664 case DW_FORM_data2:
22665 type = die_type (die, cu);
22666 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22667 if (result == NULL)
22668 result = write_constant_as_bytes (obstack, byte_order,
22669 type, value, len);
22670 break;
22671 case DW_FORM_data4:
22672 type = die_type (die, cu);
22673 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22674 if (result == NULL)
22675 result = write_constant_as_bytes (obstack, byte_order,
22676 type, value, len);
22677 break;
22678 case DW_FORM_data8:
22679 type = die_type (die, cu);
22680 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22681 if (result == NULL)
22682 result = write_constant_as_bytes (obstack, byte_order,
22683 type, value, len);
22684 break;
22685
22686 case DW_FORM_sdata:
22687 case DW_FORM_implicit_const:
22688 type = die_type (die, cu);
22689 result = write_constant_as_bytes (obstack, byte_order,
22690 type, DW_SND (attr), len);
22691 break;
22692
22693 case DW_FORM_udata:
22694 type = die_type (die, cu);
22695 result = write_constant_as_bytes (obstack, byte_order,
22696 type, DW_UNSND (attr), len);
22697 break;
22698
22699 default:
22700 complaint (_("unsupported const value attribute form: '%s'"),
22701 dwarf_form_name (attr->form));
22702 break;
22703 }
22704
22705 return result;
22706 }
22707
22708 /* See read.h. */
22709
22710 struct type *
22711 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22712 dwarf2_per_cu_data *per_cu,
22713 dwarf2_per_objfile *per_objfile)
22714 {
22715 struct die_info *die;
22716
22717 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22718 if (cu == nullptr)
22719 cu = load_cu (per_cu, per_objfile, false);
22720
22721 if (cu == nullptr)
22722 return nullptr;
22723
22724 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22725 if (!die)
22726 return NULL;
22727
22728 return die_type (die, cu);
22729 }
22730
22731 /* See read.h. */
22732
22733 struct type *
22734 dwarf2_get_die_type (cu_offset die_offset,
22735 dwarf2_per_cu_data *per_cu,
22736 dwarf2_per_objfile *per_objfile)
22737 {
22738 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22739 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
22740 }
22741
22742 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22743 On entry *REF_CU is the CU of SRC_DIE.
22744 On exit *REF_CU is the CU of the result.
22745 Returns NULL if the referenced DIE isn't found. */
22746
22747 static struct die_info *
22748 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22749 struct dwarf2_cu **ref_cu)
22750 {
22751 struct die_info temp_die;
22752 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22753 struct die_info *die;
22754 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
22755
22756
22757 /* While it might be nice to assert sig_type->type == NULL here,
22758 we can get here for DW_AT_imported_declaration where we need
22759 the DIE not the type. */
22760
22761 /* If necessary, add it to the queue and load its DIEs. */
22762
22763 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
22764 language_minimal))
22765 read_signatured_type (sig_type, per_objfile);
22766
22767 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
22768 gdb_assert (sig_cu != NULL);
22769 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22770 temp_die.sect_off = sig_type->type_offset_in_section;
22771 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22772 to_underlying (temp_die.sect_off));
22773 if (die)
22774 {
22775 /* For .gdb_index version 7 keep track of included TUs.
22776 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22777 if (per_objfile->per_bfd->index_table != NULL
22778 && per_objfile->per_bfd->index_table->version <= 7)
22779 {
22780 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22781 }
22782
22783 *ref_cu = sig_cu;
22784 if (sig_cu != cu)
22785 sig_cu->ancestor = cu;
22786
22787 return die;
22788 }
22789
22790 return NULL;
22791 }
22792
22793 /* Follow signatured type referenced by ATTR in SRC_DIE.
22794 On entry *REF_CU is the CU of SRC_DIE.
22795 On exit *REF_CU is the CU of the result.
22796 The result is the DIE of the type.
22797 If the referenced type cannot be found an error is thrown. */
22798
22799 static struct die_info *
22800 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22801 struct dwarf2_cu **ref_cu)
22802 {
22803 ULONGEST signature = DW_SIGNATURE (attr);
22804 struct signatured_type *sig_type;
22805 struct die_info *die;
22806
22807 gdb_assert (attr->form == DW_FORM_ref_sig8);
22808
22809 sig_type = lookup_signatured_type (*ref_cu, signature);
22810 /* sig_type will be NULL if the signatured type is missing from
22811 the debug info. */
22812 if (sig_type == NULL)
22813 {
22814 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22815 " from DIE at %s [in module %s]"),
22816 hex_string (signature), sect_offset_str (src_die->sect_off),
22817 objfile_name ((*ref_cu)->per_objfile->objfile));
22818 }
22819
22820 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22821 if (die == NULL)
22822 {
22823 dump_die_for_error (src_die);
22824 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22825 " from DIE at %s [in module %s]"),
22826 hex_string (signature), sect_offset_str (src_die->sect_off),
22827 objfile_name ((*ref_cu)->per_objfile->objfile));
22828 }
22829
22830 return die;
22831 }
22832
22833 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22834 reading in and processing the type unit if necessary. */
22835
22836 static struct type *
22837 get_signatured_type (struct die_info *die, ULONGEST signature,
22838 struct dwarf2_cu *cu)
22839 {
22840 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22841 struct signatured_type *sig_type;
22842 struct dwarf2_cu *type_cu;
22843 struct die_info *type_die;
22844 struct type *type;
22845
22846 sig_type = lookup_signatured_type (cu, signature);
22847 /* sig_type will be NULL if the signatured type is missing from
22848 the debug info. */
22849 if (sig_type == NULL)
22850 {
22851 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22852 " from DIE at %s [in module %s]"),
22853 hex_string (signature), sect_offset_str (die->sect_off),
22854 objfile_name (per_objfile->objfile));
22855 return build_error_marker_type (cu, die);
22856 }
22857
22858 /* If we already know the type we're done. */
22859 type = per_objfile->get_type_for_signatured_type (sig_type);
22860 if (type != nullptr)
22861 return type;
22862
22863 type_cu = cu;
22864 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22865 if (type_die != NULL)
22866 {
22867 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22868 is created. This is important, for example, because for c++ classes
22869 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22870 type = read_type_die (type_die, type_cu);
22871 if (type == NULL)
22872 {
22873 complaint (_("Dwarf Error: Cannot build signatured type %s"
22874 " referenced from DIE at %s [in module %s]"),
22875 hex_string (signature), sect_offset_str (die->sect_off),
22876 objfile_name (per_objfile->objfile));
22877 type = build_error_marker_type (cu, die);
22878 }
22879 }
22880 else
22881 {
22882 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22883 " from DIE at %s [in module %s]"),
22884 hex_string (signature), sect_offset_str (die->sect_off),
22885 objfile_name (per_objfile->objfile));
22886 type = build_error_marker_type (cu, die);
22887 }
22888
22889 per_objfile->set_type_for_signatured_type (sig_type, type);
22890
22891 return type;
22892 }
22893
22894 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22895 reading in and processing the type unit if necessary. */
22896
22897 static struct type *
22898 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22899 struct dwarf2_cu *cu) /* ARI: editCase function */
22900 {
22901 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22902 if (attr->form_is_ref ())
22903 {
22904 struct dwarf2_cu *type_cu = cu;
22905 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22906
22907 return read_type_die (type_die, type_cu);
22908 }
22909 else if (attr->form == DW_FORM_ref_sig8)
22910 {
22911 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22912 }
22913 else
22914 {
22915 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22916
22917 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22918 " at %s [in module %s]"),
22919 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22920 objfile_name (per_objfile->objfile));
22921 return build_error_marker_type (cu, die);
22922 }
22923 }
22924
22925 /* Load the DIEs associated with type unit PER_CU into memory. */
22926
22927 static void
22928 load_full_type_unit (dwarf2_per_cu_data *per_cu,
22929 dwarf2_per_objfile *per_objfile)
22930 {
22931 struct signatured_type *sig_type;
22932
22933 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22934 gdb_assert (! per_cu->type_unit_group_p ());
22935
22936 /* We have the per_cu, but we need the signatured_type.
22937 Fortunately this is an easy translation. */
22938 gdb_assert (per_cu->is_debug_types);
22939 sig_type = (struct signatured_type *) per_cu;
22940
22941 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
22942
22943 read_signatured_type (sig_type, per_objfile);
22944
22945 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
22946 }
22947
22948 /* Read in a signatured type and build its CU and DIEs.
22949 If the type is a stub for the real type in a DWO file,
22950 read in the real type from the DWO file as well. */
22951
22952 static void
22953 read_signatured_type (signatured_type *sig_type,
22954 dwarf2_per_objfile *per_objfile)
22955 {
22956 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22957
22958 gdb_assert (per_cu->is_debug_types);
22959 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
22960
22961 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
22962
22963 if (!reader.dummy_p)
22964 {
22965 struct dwarf2_cu *cu = reader.cu;
22966 const gdb_byte *info_ptr = reader.info_ptr;
22967
22968 gdb_assert (cu->die_hash == NULL);
22969 cu->die_hash =
22970 htab_create_alloc_ex (cu->header.length / 12,
22971 die_hash,
22972 die_eq,
22973 NULL,
22974 &cu->comp_unit_obstack,
22975 hashtab_obstack_allocate,
22976 dummy_obstack_deallocate);
22977
22978 if (reader.comp_unit_die->has_children)
22979 reader.comp_unit_die->child
22980 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22981 reader.comp_unit_die);
22982 cu->dies = reader.comp_unit_die;
22983 /* comp_unit_die is not stored in die_hash, no need. */
22984
22985 /* We try not to read any attributes in this function, because
22986 not all CUs needed for references have been loaded yet, and
22987 symbol table processing isn't initialized. But we have to
22988 set the CU language, or we won't be able to build types
22989 correctly. Similarly, if we do not read the producer, we can
22990 not apply producer-specific interpretation. */
22991 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22992
22993 reader.keep ();
22994 }
22995
22996 sig_type->per_cu.tu_read = 1;
22997 }
22998
22999 /* Decode simple location descriptions.
23000 Given a pointer to a dwarf block that defines a location, compute
23001 the location and return the value. If COMPUTED is non-null, it is
23002 set to true to indicate that decoding was successful, and false
23003 otherwise. If COMPUTED is null, then this function may emit a
23004 complaint. */
23005
23006 static CORE_ADDR
23007 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
23008 {
23009 struct objfile *objfile = cu->per_objfile->objfile;
23010 size_t i;
23011 size_t size = blk->size;
23012 const gdb_byte *data = blk->data;
23013 CORE_ADDR stack[64];
23014 int stacki;
23015 unsigned int bytes_read, unsnd;
23016 gdb_byte op;
23017
23018 if (computed != nullptr)
23019 *computed = false;
23020
23021 i = 0;
23022 stacki = 0;
23023 stack[stacki] = 0;
23024 stack[++stacki] = 0;
23025
23026 while (i < size)
23027 {
23028 op = data[i++];
23029 switch (op)
23030 {
23031 case DW_OP_lit0:
23032 case DW_OP_lit1:
23033 case DW_OP_lit2:
23034 case DW_OP_lit3:
23035 case DW_OP_lit4:
23036 case DW_OP_lit5:
23037 case DW_OP_lit6:
23038 case DW_OP_lit7:
23039 case DW_OP_lit8:
23040 case DW_OP_lit9:
23041 case DW_OP_lit10:
23042 case DW_OP_lit11:
23043 case DW_OP_lit12:
23044 case DW_OP_lit13:
23045 case DW_OP_lit14:
23046 case DW_OP_lit15:
23047 case DW_OP_lit16:
23048 case DW_OP_lit17:
23049 case DW_OP_lit18:
23050 case DW_OP_lit19:
23051 case DW_OP_lit20:
23052 case DW_OP_lit21:
23053 case DW_OP_lit22:
23054 case DW_OP_lit23:
23055 case DW_OP_lit24:
23056 case DW_OP_lit25:
23057 case DW_OP_lit26:
23058 case DW_OP_lit27:
23059 case DW_OP_lit28:
23060 case DW_OP_lit29:
23061 case DW_OP_lit30:
23062 case DW_OP_lit31:
23063 stack[++stacki] = op - DW_OP_lit0;
23064 break;
23065
23066 case DW_OP_reg0:
23067 case DW_OP_reg1:
23068 case DW_OP_reg2:
23069 case DW_OP_reg3:
23070 case DW_OP_reg4:
23071 case DW_OP_reg5:
23072 case DW_OP_reg6:
23073 case DW_OP_reg7:
23074 case DW_OP_reg8:
23075 case DW_OP_reg9:
23076 case DW_OP_reg10:
23077 case DW_OP_reg11:
23078 case DW_OP_reg12:
23079 case DW_OP_reg13:
23080 case DW_OP_reg14:
23081 case DW_OP_reg15:
23082 case DW_OP_reg16:
23083 case DW_OP_reg17:
23084 case DW_OP_reg18:
23085 case DW_OP_reg19:
23086 case DW_OP_reg20:
23087 case DW_OP_reg21:
23088 case DW_OP_reg22:
23089 case DW_OP_reg23:
23090 case DW_OP_reg24:
23091 case DW_OP_reg25:
23092 case DW_OP_reg26:
23093 case DW_OP_reg27:
23094 case DW_OP_reg28:
23095 case DW_OP_reg29:
23096 case DW_OP_reg30:
23097 case DW_OP_reg31:
23098 stack[++stacki] = op - DW_OP_reg0;
23099 if (i < size)
23100 {
23101 if (computed == nullptr)
23102 dwarf2_complex_location_expr_complaint ();
23103 else
23104 return 0;
23105 }
23106 break;
23107
23108 case DW_OP_regx:
23109 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23110 i += bytes_read;
23111 stack[++stacki] = unsnd;
23112 if (i < size)
23113 {
23114 if (computed == nullptr)
23115 dwarf2_complex_location_expr_complaint ();
23116 else
23117 return 0;
23118 }
23119 break;
23120
23121 case DW_OP_addr:
23122 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23123 &bytes_read);
23124 i += bytes_read;
23125 break;
23126
23127 case DW_OP_const1u:
23128 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23129 i += 1;
23130 break;
23131
23132 case DW_OP_const1s:
23133 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23134 i += 1;
23135 break;
23136
23137 case DW_OP_const2u:
23138 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23139 i += 2;
23140 break;
23141
23142 case DW_OP_const2s:
23143 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23144 i += 2;
23145 break;
23146
23147 case DW_OP_const4u:
23148 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23149 i += 4;
23150 break;
23151
23152 case DW_OP_const4s:
23153 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23154 i += 4;
23155 break;
23156
23157 case DW_OP_const8u:
23158 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23159 i += 8;
23160 break;
23161
23162 case DW_OP_constu:
23163 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23164 &bytes_read);
23165 i += bytes_read;
23166 break;
23167
23168 case DW_OP_consts:
23169 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23170 i += bytes_read;
23171 break;
23172
23173 case DW_OP_dup:
23174 stack[stacki + 1] = stack[stacki];
23175 stacki++;
23176 break;
23177
23178 case DW_OP_plus:
23179 stack[stacki - 1] += stack[stacki];
23180 stacki--;
23181 break;
23182
23183 case DW_OP_plus_uconst:
23184 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23185 &bytes_read);
23186 i += bytes_read;
23187 break;
23188
23189 case DW_OP_minus:
23190 stack[stacki - 1] -= stack[stacki];
23191 stacki--;
23192 break;
23193
23194 case DW_OP_deref:
23195 /* If we're not the last op, then we definitely can't encode
23196 this using GDB's address_class enum. This is valid for partial
23197 global symbols, although the variable's address will be bogus
23198 in the psymtab. */
23199 if (i < size)
23200 {
23201 if (computed == nullptr)
23202 dwarf2_complex_location_expr_complaint ();
23203 else
23204 return 0;
23205 }
23206 break;
23207
23208 case DW_OP_GNU_push_tls_address:
23209 case DW_OP_form_tls_address:
23210 /* The top of the stack has the offset from the beginning
23211 of the thread control block at which the variable is located. */
23212 /* Nothing should follow this operator, so the top of stack would
23213 be returned. */
23214 /* This is valid for partial global symbols, but the variable's
23215 address will be bogus in the psymtab. Make it always at least
23216 non-zero to not look as a variable garbage collected by linker
23217 which have DW_OP_addr 0. */
23218 if (i < size)
23219 {
23220 if (computed == nullptr)
23221 dwarf2_complex_location_expr_complaint ();
23222 else
23223 return 0;
23224 }
23225 stack[stacki]++;
23226 break;
23227
23228 case DW_OP_GNU_uninit:
23229 if (computed != nullptr)
23230 return 0;
23231 break;
23232
23233 case DW_OP_addrx:
23234 case DW_OP_GNU_addr_index:
23235 case DW_OP_GNU_const_index:
23236 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23237 &bytes_read);
23238 i += bytes_read;
23239 break;
23240
23241 default:
23242 if (computed == nullptr)
23243 {
23244 const char *name = get_DW_OP_name (op);
23245
23246 if (name)
23247 complaint (_("unsupported stack op: '%s'"),
23248 name);
23249 else
23250 complaint (_("unsupported stack op: '%02x'"),
23251 op);
23252 }
23253
23254 return (stack[stacki]);
23255 }
23256
23257 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23258 outside of the allocated space. Also enforce minimum>0. */
23259 if (stacki >= ARRAY_SIZE (stack) - 1)
23260 {
23261 if (computed == nullptr)
23262 complaint (_("location description stack overflow"));
23263 return 0;
23264 }
23265
23266 if (stacki <= 0)
23267 {
23268 if (computed == nullptr)
23269 complaint (_("location description stack underflow"));
23270 return 0;
23271 }
23272 }
23273
23274 if (computed != nullptr)
23275 *computed = true;
23276 return (stack[stacki]);
23277 }
23278
23279 /* memory allocation interface */
23280
23281 static struct dwarf_block *
23282 dwarf_alloc_block (struct dwarf2_cu *cu)
23283 {
23284 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23285 }
23286
23287 static struct die_info *
23288 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23289 {
23290 struct die_info *die;
23291 size_t size = sizeof (struct die_info);
23292
23293 if (num_attrs > 1)
23294 size += (num_attrs - 1) * sizeof (struct attribute);
23295
23296 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23297 memset (die, 0, sizeof (struct die_info));
23298 return (die);
23299 }
23300
23301 \f
23302
23303 /* Macro support. */
23304
23305 /* An overload of dwarf_decode_macros that finds the correct section
23306 and ensures it is read in before calling the other overload. */
23307
23308 static void
23309 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23310 int section_is_gnu)
23311 {
23312 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23313 struct objfile *objfile = per_objfile->objfile;
23314 const struct line_header *lh = cu->line_header;
23315 unsigned int offset_size = cu->header.offset_size;
23316 struct dwarf2_section_info *section;
23317 const char *section_name;
23318
23319 if (cu->dwo_unit != nullptr)
23320 {
23321 if (section_is_gnu)
23322 {
23323 section = &cu->dwo_unit->dwo_file->sections.macro;
23324 section_name = ".debug_macro.dwo";
23325 }
23326 else
23327 {
23328 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23329 section_name = ".debug_macinfo.dwo";
23330 }
23331 }
23332 else
23333 {
23334 if (section_is_gnu)
23335 {
23336 section = &per_objfile->per_bfd->macro;
23337 section_name = ".debug_macro";
23338 }
23339 else
23340 {
23341 section = &per_objfile->per_bfd->macinfo;
23342 section_name = ".debug_macinfo";
23343 }
23344 }
23345
23346 section->read (objfile);
23347 if (section->buffer == nullptr)
23348 {
23349 complaint (_("missing %s section"), section_name);
23350 return;
23351 }
23352
23353 buildsym_compunit *builder = cu->get_builder ();
23354
23355 dwarf_decode_macros (per_objfile, builder, section, lh,
23356 offset_size, offset, section_is_gnu);
23357 }
23358
23359 /* Return the .debug_loc section to use for CU.
23360 For DWO files use .debug_loc.dwo. */
23361
23362 static struct dwarf2_section_info *
23363 cu_debug_loc_section (struct dwarf2_cu *cu)
23364 {
23365 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23366
23367 if (cu->dwo_unit)
23368 {
23369 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23370
23371 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23372 }
23373 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
23374 : &per_objfile->per_bfd->loc);
23375 }
23376
23377 /* A helper function that fills in a dwarf2_loclist_baton. */
23378
23379 static void
23380 fill_in_loclist_baton (struct dwarf2_cu *cu,
23381 struct dwarf2_loclist_baton *baton,
23382 const struct attribute *attr)
23383 {
23384 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23385 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23386
23387 section->read (per_objfile->objfile);
23388
23389 baton->per_objfile = per_objfile;
23390 baton->per_cu = cu->per_cu;
23391 gdb_assert (baton->per_cu);
23392 /* We don't know how long the location list is, but make sure we
23393 don't run off the edge of the section. */
23394 baton->size = section->size - DW_UNSND (attr);
23395 baton->data = section->buffer + DW_UNSND (attr);
23396 if (cu->base_address.has_value ())
23397 baton->base_address = *cu->base_address;
23398 else
23399 baton->base_address = 0;
23400 baton->from_dwo = cu->dwo_unit != NULL;
23401 }
23402
23403 static void
23404 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23405 struct dwarf2_cu *cu, int is_block)
23406 {
23407 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23408 struct objfile *objfile = per_objfile->objfile;
23409 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23410
23411 if (attr->form_is_section_offset ()
23412 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23413 the section. If so, fall through to the complaint in the
23414 other branch. */
23415 && DW_UNSND (attr) < section->get_size (objfile))
23416 {
23417 struct dwarf2_loclist_baton *baton;
23418
23419 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23420
23421 fill_in_loclist_baton (cu, baton, attr);
23422
23423 if (!cu->base_address.has_value ())
23424 complaint (_("Location list used without "
23425 "specifying the CU base address."));
23426
23427 SYMBOL_ACLASS_INDEX (sym) = (is_block
23428 ? dwarf2_loclist_block_index
23429 : dwarf2_loclist_index);
23430 SYMBOL_LOCATION_BATON (sym) = baton;
23431 }
23432 else
23433 {
23434 struct dwarf2_locexpr_baton *baton;
23435
23436 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23437 baton->per_objfile = per_objfile;
23438 baton->per_cu = cu->per_cu;
23439 gdb_assert (baton->per_cu);
23440
23441 if (attr->form_is_block ())
23442 {
23443 /* Note that we're just copying the block's data pointer
23444 here, not the actual data. We're still pointing into the
23445 info_buffer for SYM's objfile; right now we never release
23446 that buffer, but when we do clean up properly this may
23447 need to change. */
23448 baton->size = DW_BLOCK (attr)->size;
23449 baton->data = DW_BLOCK (attr)->data;
23450 }
23451 else
23452 {
23453 dwarf2_invalid_attrib_class_complaint ("location description",
23454 sym->natural_name ());
23455 baton->size = 0;
23456 }
23457
23458 SYMBOL_ACLASS_INDEX (sym) = (is_block
23459 ? dwarf2_locexpr_block_index
23460 : dwarf2_locexpr_index);
23461 SYMBOL_LOCATION_BATON (sym) = baton;
23462 }
23463 }
23464
23465 /* See read.h. */
23466
23467 const comp_unit_head *
23468 dwarf2_per_cu_data::get_header () const
23469 {
23470 if (!m_header_read_in)
23471 {
23472 const gdb_byte *info_ptr
23473 = this->section->buffer + to_underlying (this->sect_off);
23474
23475 memset (&m_header, 0, sizeof (m_header));
23476
23477 read_comp_unit_head (&m_header, info_ptr, this->section,
23478 rcuh_kind::COMPILE);
23479 }
23480
23481 return &m_header;
23482 }
23483
23484 /* See read.h. */
23485
23486 int
23487 dwarf2_per_cu_data::addr_size () const
23488 {
23489 return this->get_header ()->addr_size;
23490 }
23491
23492 /* See read.h. */
23493
23494 int
23495 dwarf2_per_cu_data::offset_size () const
23496 {
23497 return this->get_header ()->offset_size;
23498 }
23499
23500 /* See read.h. */
23501
23502 int
23503 dwarf2_per_cu_data::ref_addr_size () const
23504 {
23505 const comp_unit_head *header = this->get_header ();
23506
23507 if (header->version == 2)
23508 return header->addr_size;
23509 else
23510 return header->offset_size;
23511 }
23512
23513 /* See read.h. */
23514
23515 struct type *
23516 dwarf2_cu::addr_type () const
23517 {
23518 struct objfile *objfile = this->per_objfile->objfile;
23519 struct type *void_type = objfile_type (objfile)->builtin_void;
23520 struct type *addr_type = lookup_pointer_type (void_type);
23521 int addr_size = this->per_cu->addr_size ();
23522
23523 if (TYPE_LENGTH (addr_type) == addr_size)
23524 return addr_type;
23525
23526 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23527 return addr_type;
23528 }
23529
23530 /* A helper function for dwarf2_find_containing_comp_unit that returns
23531 the index of the result, and that searches a vector. It will
23532 return a result even if the offset in question does not actually
23533 occur in any CU. This is separate so that it can be unit
23534 tested. */
23535
23536 static int
23537 dwarf2_find_containing_comp_unit
23538 (sect_offset sect_off,
23539 unsigned int offset_in_dwz,
23540 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23541 {
23542 int low, high;
23543
23544 low = 0;
23545 high = all_comp_units.size () - 1;
23546 while (high > low)
23547 {
23548 struct dwarf2_per_cu_data *mid_cu;
23549 int mid = low + (high - low) / 2;
23550
23551 mid_cu = all_comp_units[mid];
23552 if (mid_cu->is_dwz > offset_in_dwz
23553 || (mid_cu->is_dwz == offset_in_dwz
23554 && mid_cu->sect_off + mid_cu->length > sect_off))
23555 high = mid;
23556 else
23557 low = mid + 1;
23558 }
23559 gdb_assert (low == high);
23560 return low;
23561 }
23562
23563 /* Locate the .debug_info compilation unit from CU's objfile which contains
23564 the DIE at OFFSET. Raises an error on failure. */
23565
23566 static struct dwarf2_per_cu_data *
23567 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23568 unsigned int offset_in_dwz,
23569 dwarf2_per_objfile *per_objfile)
23570 {
23571 int low = dwarf2_find_containing_comp_unit
23572 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
23573 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
23574
23575 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23576 {
23577 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23578 error (_("Dwarf Error: could not find partial DIE containing "
23579 "offset %s [in module %s]"),
23580 sect_offset_str (sect_off),
23581 bfd_get_filename (per_objfile->objfile->obfd));
23582
23583 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
23584 <= sect_off);
23585 return per_objfile->per_bfd->all_comp_units[low-1];
23586 }
23587 else
23588 {
23589 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
23590 && sect_off >= this_cu->sect_off + this_cu->length)
23591 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23592 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23593 return this_cu;
23594 }
23595 }
23596
23597 #if GDB_SELF_TEST
23598
23599 namespace selftests {
23600 namespace find_containing_comp_unit {
23601
23602 static void
23603 run_test ()
23604 {
23605 struct dwarf2_per_cu_data one {};
23606 struct dwarf2_per_cu_data two {};
23607 struct dwarf2_per_cu_data three {};
23608 struct dwarf2_per_cu_data four {};
23609
23610 one.length = 5;
23611 two.sect_off = sect_offset (one.length);
23612 two.length = 7;
23613
23614 three.length = 5;
23615 three.is_dwz = 1;
23616 four.sect_off = sect_offset (three.length);
23617 four.length = 7;
23618 four.is_dwz = 1;
23619
23620 std::vector<dwarf2_per_cu_data *> units;
23621 units.push_back (&one);
23622 units.push_back (&two);
23623 units.push_back (&three);
23624 units.push_back (&four);
23625
23626 int result;
23627
23628 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23629 SELF_CHECK (units[result] == &one);
23630 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23631 SELF_CHECK (units[result] == &one);
23632 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23633 SELF_CHECK (units[result] == &two);
23634
23635 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23636 SELF_CHECK (units[result] == &three);
23637 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23638 SELF_CHECK (units[result] == &three);
23639 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23640 SELF_CHECK (units[result] == &four);
23641 }
23642
23643 }
23644 }
23645
23646 #endif /* GDB_SELF_TEST */
23647
23648 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
23649
23650 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23651 dwarf2_per_objfile *per_objfile)
23652 : per_cu (per_cu),
23653 per_objfile (per_objfile),
23654 mark (false),
23655 has_loclist (false),
23656 checked_producer (false),
23657 producer_is_gxx_lt_4_6 (false),
23658 producer_is_gcc_lt_4_3 (false),
23659 producer_is_icc (false),
23660 producer_is_icc_lt_14 (false),
23661 producer_is_codewarrior (false),
23662 processing_has_namespace_info (false)
23663 {
23664 }
23665
23666 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23667
23668 static void
23669 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23670 enum language pretend_language)
23671 {
23672 struct attribute *attr;
23673
23674 /* Set the language we're debugging. */
23675 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23676 if (attr != nullptr)
23677 set_cu_language (DW_UNSND (attr), cu);
23678 else
23679 {
23680 cu->language = pretend_language;
23681 cu->language_defn = language_def (cu->language);
23682 }
23683
23684 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23685 }
23686
23687 /* See read.h. */
23688
23689 dwarf2_cu *
23690 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
23691 {
23692 auto it = m_dwarf2_cus.find (per_cu);
23693 if (it == m_dwarf2_cus.end ())
23694 return nullptr;
23695
23696 return it->second;
23697 }
23698
23699 /* See read.h. */
23700
23701 void
23702 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
23703 {
23704 gdb_assert (this->get_cu (per_cu) == nullptr);
23705
23706 m_dwarf2_cus[per_cu] = cu;
23707 }
23708
23709 /* See read.h. */
23710
23711 void
23712 dwarf2_per_objfile::age_comp_units ()
23713 {
23714 /* Start by clearing all marks. */
23715 for (auto pair : m_dwarf2_cus)
23716 pair.second->mark = false;
23717
23718 /* Traverse all CUs, mark them and their dependencies if used recently
23719 enough. */
23720 for (auto pair : m_dwarf2_cus)
23721 {
23722 dwarf2_cu *cu = pair.second;
23723
23724 cu->last_used++;
23725 if (cu->last_used <= dwarf_max_cache_age)
23726 dwarf2_mark (cu);
23727 }
23728
23729 /* Delete all CUs still not marked. */
23730 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
23731 {
23732 dwarf2_cu *cu = it->second;
23733
23734 if (!cu->mark)
23735 {
23736 delete cu;
23737 it = m_dwarf2_cus.erase (it);
23738 }
23739 else
23740 it++;
23741 }
23742 }
23743
23744 /* See read.h. */
23745
23746 void
23747 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
23748 {
23749 auto it = m_dwarf2_cus.find (per_cu);
23750 if (it == m_dwarf2_cus.end ())
23751 return;
23752
23753 delete it->second;
23754
23755 m_dwarf2_cus.erase (it);
23756 }
23757
23758 dwarf2_per_objfile::~dwarf2_per_objfile ()
23759 {
23760 remove_all_cus ();
23761 }
23762
23763 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23764 We store these in a hash table separate from the DIEs, and preserve them
23765 when the DIEs are flushed out of cache.
23766
23767 The CU "per_cu" pointer is needed because offset alone is not enough to
23768 uniquely identify the type. A file may have multiple .debug_types sections,
23769 or the type may come from a DWO file. Furthermore, while it's more logical
23770 to use per_cu->section+offset, with Fission the section with the data is in
23771 the DWO file but we don't know that section at the point we need it.
23772 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23773 because we can enter the lookup routine, get_die_type_at_offset, from
23774 outside this file, and thus won't necessarily have PER_CU->cu.
23775 Fortunately, PER_CU is stable for the life of the objfile. */
23776
23777 struct dwarf2_per_cu_offset_and_type
23778 {
23779 const struct dwarf2_per_cu_data *per_cu;
23780 sect_offset sect_off;
23781 struct type *type;
23782 };
23783
23784 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23785
23786 static hashval_t
23787 per_cu_offset_and_type_hash (const void *item)
23788 {
23789 const struct dwarf2_per_cu_offset_and_type *ofs
23790 = (const struct dwarf2_per_cu_offset_and_type *) item;
23791
23792 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23793 }
23794
23795 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23796
23797 static int
23798 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23799 {
23800 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23801 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23802 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23803 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23804
23805 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23806 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23807 }
23808
23809 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23810 table if necessary. For convenience, return TYPE.
23811
23812 The DIEs reading must have careful ordering to:
23813 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23814 reading current DIE.
23815 * Not trying to dereference contents of still incompletely read in types
23816 while reading in other DIEs.
23817 * Enable referencing still incompletely read in types just by a pointer to
23818 the type without accessing its fields.
23819
23820 Therefore caller should follow these rules:
23821 * Try to fetch any prerequisite types we may need to build this DIE type
23822 before building the type and calling set_die_type.
23823 * After building type call set_die_type for current DIE as soon as
23824 possible before fetching more types to complete the current type.
23825 * Make the type as complete as possible before fetching more types. */
23826
23827 static struct type *
23828 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23829 {
23830 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23831 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23832 struct objfile *objfile = per_objfile->objfile;
23833 struct attribute *attr;
23834 struct dynamic_prop prop;
23835
23836 /* For Ada types, make sure that the gnat-specific data is always
23837 initialized (if not already set). There are a few types where
23838 we should not be doing so, because the type-specific area is
23839 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23840 where the type-specific area is used to store the floatformat).
23841 But this is not a problem, because the gnat-specific information
23842 is actually not needed for these types. */
23843 if (need_gnat_info (cu)
23844 && type->code () != TYPE_CODE_FUNC
23845 && type->code () != TYPE_CODE_FLT
23846 && type->code () != TYPE_CODE_METHODPTR
23847 && type->code () != TYPE_CODE_MEMBERPTR
23848 && type->code () != TYPE_CODE_METHOD
23849 && !HAVE_GNAT_AUX_INFO (type))
23850 INIT_GNAT_SPECIFIC (type);
23851
23852 /* Read DW_AT_allocated and set in type. */
23853 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23854 if (attr != NULL && attr->form_is_block ())
23855 {
23856 struct type *prop_type = cu->addr_sized_int_type (false);
23857 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23858 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23859 }
23860 else if (attr != NULL)
23861 {
23862 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23863 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23864 sect_offset_str (die->sect_off));
23865 }
23866
23867 /* Read DW_AT_associated and set in type. */
23868 attr = dwarf2_attr (die, DW_AT_associated, cu);
23869 if (attr != NULL && attr->form_is_block ())
23870 {
23871 struct type *prop_type = cu->addr_sized_int_type (false);
23872 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23873 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23874 }
23875 else if (attr != NULL)
23876 {
23877 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23878 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23879 sect_offset_str (die->sect_off));
23880 }
23881
23882 /* Read DW_AT_data_location and set in type. */
23883 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23884 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
23885 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23886
23887 if (per_objfile->die_type_hash == NULL)
23888 per_objfile->die_type_hash
23889 = htab_up (htab_create_alloc (127,
23890 per_cu_offset_and_type_hash,
23891 per_cu_offset_and_type_eq,
23892 NULL, xcalloc, xfree));
23893
23894 ofs.per_cu = cu->per_cu;
23895 ofs.sect_off = die->sect_off;
23896 ofs.type = type;
23897 slot = (struct dwarf2_per_cu_offset_and_type **)
23898 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
23899 if (*slot)
23900 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23901 sect_offset_str (die->sect_off));
23902 *slot = XOBNEW (&objfile->objfile_obstack,
23903 struct dwarf2_per_cu_offset_and_type);
23904 **slot = ofs;
23905 return type;
23906 }
23907
23908 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23909 or return NULL if the die does not have a saved type. */
23910
23911 static struct type *
23912 get_die_type_at_offset (sect_offset sect_off,
23913 dwarf2_per_cu_data *per_cu,
23914 dwarf2_per_objfile *per_objfile)
23915 {
23916 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23917
23918 if (per_objfile->die_type_hash == NULL)
23919 return NULL;
23920
23921 ofs.per_cu = per_cu;
23922 ofs.sect_off = sect_off;
23923 slot = ((struct dwarf2_per_cu_offset_and_type *)
23924 htab_find (per_objfile->die_type_hash.get (), &ofs));
23925 if (slot)
23926 return slot->type;
23927 else
23928 return NULL;
23929 }
23930
23931 /* Look up the type for DIE in CU in die_type_hash,
23932 or return NULL if DIE does not have a saved type. */
23933
23934 static struct type *
23935 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23936 {
23937 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
23938 }
23939
23940 /* Add a dependence relationship from CU to REF_PER_CU. */
23941
23942 static void
23943 dwarf2_add_dependence (struct dwarf2_cu *cu,
23944 struct dwarf2_per_cu_data *ref_per_cu)
23945 {
23946 void **slot;
23947
23948 if (cu->dependencies == NULL)
23949 cu->dependencies
23950 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23951 NULL, &cu->comp_unit_obstack,
23952 hashtab_obstack_allocate,
23953 dummy_obstack_deallocate);
23954
23955 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23956 if (*slot == NULL)
23957 *slot = ref_per_cu;
23958 }
23959
23960 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23961 Set the mark field in every compilation unit in the
23962 cache that we must keep because we are keeping CU.
23963
23964 DATA is the dwarf2_per_objfile object in which to look up CUs. */
23965
23966 static int
23967 dwarf2_mark_helper (void **slot, void *data)
23968 {
23969 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
23970 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
23971 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23972
23973 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23974 reading of the chain. As such dependencies remain valid it is not much
23975 useful to track and undo them during QUIT cleanups. */
23976 if (cu == nullptr)
23977 return 1;
23978
23979 if (cu->mark)
23980 return 1;
23981
23982 cu->mark = true;
23983
23984 if (cu->dependencies != nullptr)
23985 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
23986
23987 return 1;
23988 }
23989
23990 /* Set the mark field in CU and in every other compilation unit in the
23991 cache that we must keep because we are keeping CU. */
23992
23993 static void
23994 dwarf2_mark (struct dwarf2_cu *cu)
23995 {
23996 if (cu->mark)
23997 return;
23998
23999 cu->mark = true;
24000
24001 if (cu->dependencies != nullptr)
24002 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
24003 }
24004
24005 /* Trivial hash function for partial_die_info: the hash value of a DIE
24006 is its offset in .debug_info for this objfile. */
24007
24008 static hashval_t
24009 partial_die_hash (const void *item)
24010 {
24011 const struct partial_die_info *part_die
24012 = (const struct partial_die_info *) item;
24013
24014 return to_underlying (part_die->sect_off);
24015 }
24016
24017 /* Trivial comparison function for partial_die_info structures: two DIEs
24018 are equal if they have the same offset. */
24019
24020 static int
24021 partial_die_eq (const void *item_lhs, const void *item_rhs)
24022 {
24023 const struct partial_die_info *part_die_lhs
24024 = (const struct partial_die_info *) item_lhs;
24025 const struct partial_die_info *part_die_rhs
24026 = (const struct partial_die_info *) item_rhs;
24027
24028 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24029 }
24030
24031 struct cmd_list_element *set_dwarf_cmdlist;
24032 struct cmd_list_element *show_dwarf_cmdlist;
24033
24034 static void
24035 show_check_physname (struct ui_file *file, int from_tty,
24036 struct cmd_list_element *c, const char *value)
24037 {
24038 fprintf_filtered (file,
24039 _("Whether to check \"physname\" is %s.\n"),
24040 value);
24041 }
24042
24043 void _initialize_dwarf2_read ();
24044 void
24045 _initialize_dwarf2_read ()
24046 {
24047 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
24048 Set DWARF specific variables.\n\
24049 Configure DWARF variables such as the cache size."),
24050 &set_dwarf_cmdlist, "maintenance set dwarf ",
24051 0/*allow-unknown*/, &maintenance_set_cmdlist);
24052
24053 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
24054 Show DWARF specific variables.\n\
24055 Show DWARF variables such as the cache size."),
24056 &show_dwarf_cmdlist, "maintenance show dwarf ",
24057 0/*allow-unknown*/, &maintenance_show_cmdlist);
24058
24059 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24060 &dwarf_max_cache_age, _("\
24061 Set the upper bound on the age of cached DWARF compilation units."), _("\
24062 Show the upper bound on the age of cached DWARF compilation units."), _("\
24063 A higher limit means that cached compilation units will be stored\n\
24064 in memory longer, and more total memory will be used. Zero disables\n\
24065 caching, which can slow down startup."),
24066 NULL,
24067 show_dwarf_max_cache_age,
24068 &set_dwarf_cmdlist,
24069 &show_dwarf_cmdlist);
24070
24071 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24072 Set debugging of the DWARF reader."), _("\
24073 Show debugging of the DWARF reader."), _("\
24074 When enabled (non-zero), debugging messages are printed during DWARF\n\
24075 reading and symtab expansion. A value of 1 (one) provides basic\n\
24076 information. A value greater than 1 provides more verbose information."),
24077 NULL,
24078 NULL,
24079 &setdebuglist, &showdebuglist);
24080
24081 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24082 Set debugging of the DWARF DIE reader."), _("\
24083 Show debugging of the DWARF DIE reader."), _("\
24084 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24085 The value is the maximum depth to print."),
24086 NULL,
24087 NULL,
24088 &setdebuglist, &showdebuglist);
24089
24090 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24091 Set debugging of the dwarf line reader."), _("\
24092 Show debugging of the dwarf line reader."), _("\
24093 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24094 A value of 1 (one) provides basic information.\n\
24095 A value greater than 1 provides more verbose information."),
24096 NULL,
24097 NULL,
24098 &setdebuglist, &showdebuglist);
24099
24100 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24101 Set cross-checking of \"physname\" code against demangler."), _("\
24102 Show cross-checking of \"physname\" code against demangler."), _("\
24103 When enabled, GDB's internal \"physname\" code is checked against\n\
24104 the demangler."),
24105 NULL, show_check_physname,
24106 &setdebuglist, &showdebuglist);
24107
24108 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24109 no_class, &use_deprecated_index_sections, _("\
24110 Set whether to use deprecated gdb_index sections."), _("\
24111 Show whether to use deprecated gdb_index sections."), _("\
24112 When enabled, deprecated .gdb_index sections are used anyway.\n\
24113 Normally they are ignored either because of a missing feature or\n\
24114 performance issue.\n\
24115 Warning: This option must be enabled before gdb reads the file."),
24116 NULL,
24117 NULL,
24118 &setlist, &showlist);
24119
24120 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24121 &dwarf2_locexpr_funcs);
24122 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24123 &dwarf2_loclist_funcs);
24124
24125 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24126 &dwarf2_block_frame_base_locexpr_funcs);
24127 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24128 &dwarf2_block_frame_base_loclist_funcs);
24129
24130 #if GDB_SELF_TEST
24131 selftests::register_test ("dw2_expand_symtabs_matching",
24132 selftests::dw2_expand_symtabs_matching::run_test);
24133 selftests::register_test ("dwarf2_find_containing_comp_unit",
24134 selftests::find_containing_comp_unit::run_test);
24135 #endif
24136 }
This page took 0.488516 seconds and 5 git commands to generate.